1*1b191cb5SApple OSS Distributions // Copyright (c) 2020 Apple Inc. All rights reserved.
2*1b191cb5SApple OSS Distributions //
3*1b191cb5SApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_START@
4*1b191cb5SApple OSS Distributions //
5*1b191cb5SApple OSS Distributions // This file contains Original Code and/or Modifications of Original Code
6*1b191cb5SApple OSS Distributions // as defined in and that are subject to the Apple Public Source License
7*1b191cb5SApple OSS Distributions // Version 2.0 (the 'License'). You may not use this file except in
8*1b191cb5SApple OSS Distributions // compliance with the License. The rights granted to you under the License
9*1b191cb5SApple OSS Distributions // may not be used to create, or enable the creation or redistribution of,
10*1b191cb5SApple OSS Distributions // unlawful or unlicensed copies of an Apple operating system, or to
11*1b191cb5SApple OSS Distributions // circumvent, violate, or enable the circumvention or violation of, any
12*1b191cb5SApple OSS Distributions // terms of an Apple operating system software license agreement.
13*1b191cb5SApple OSS Distributions //
14*1b191cb5SApple OSS Distributions // Please obtain a copy of the License at
15*1b191cb5SApple OSS Distributions // http://www.opensource.apple.com/apsl/ and read it before using this file.
16*1b191cb5SApple OSS Distributions //
17*1b191cb5SApple OSS Distributions // The Original Code and all software distributed under the License are
18*1b191cb5SApple OSS Distributions // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19*1b191cb5SApple OSS Distributions // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
20*1b191cb5SApple OSS Distributions // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
21*1b191cb5SApple OSS Distributions // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
22*1b191cb5SApple OSS Distributions // Please see the License for the specific language governing rights and
23*1b191cb5SApple OSS Distributions // limitations under the License.
24*1b191cb5SApple OSS Distributions //
25*1b191cb5SApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_END@
26*1b191cb5SApple OSS Distributions
27*1b191cb5SApple OSS Distributions #include <kern/assert.h>
28*1b191cb5SApple OSS Distributions #include <kern/kalloc.h>
29*1b191cb5SApple OSS Distributions #include <kern/locks.h>
30*1b191cb5SApple OSS Distributions #include <kern/perfmon.h>
31*1b191cb5SApple OSS Distributions #include <libkern/copyio.h>
32*1b191cb5SApple OSS Distributions #include <machine/machine_routines.h>
33*1b191cb5SApple OSS Distributions #include <pexpert/pexpert.h>
34*1b191cb5SApple OSS Distributions #include <stdbool.h>
35*1b191cb5SApple OSS Distributions #include <sys/param.h> /* NULL */
36*1b191cb5SApple OSS Distributions #include <sys/stat.h> /* dev_t */
37*1b191cb5SApple OSS Distributions #include <miscfs/devfs/devfs.h> /* must come after sys/stat.h */
38*1b191cb5SApple OSS Distributions #include <sys/conf.h> /* must come after sys/stat.h */
39*1b191cb5SApple OSS Distributions #include <sys/perfmon_private.h>
40*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
41*1b191cb5SApple OSS Distributions #include <sys/sysproto.h>
42*1b191cb5SApple OSS Distributions #include <sys/systm.h>
43*1b191cb5SApple OSS Distributions #include <sys/types.h>
44*1b191cb5SApple OSS Distributions
45*1b191cb5SApple OSS Distributions static unsigned int perfmon_dev_major_sources[perfmon_kind_max] = { 0 };
46*1b191cb5SApple OSS Distributions static const unsigned int PERFMON_DEVICES_MAX = 4;
47*1b191cb5SApple OSS Distributions
48*1b191cb5SApple OSS Distributions LCK_GRP_DECLARE(perfmon_dev_lock_group, "perfmon");
49*1b191cb5SApple OSS Distributions
50*1b191cb5SApple OSS Distributions // perfmon_device corresponds to each open file descriptor for perfmon's
51*1b191cb5SApple OSS Distributions // character devices.
52*1b191cb5SApple OSS Distributions struct perfmon_device {
53*1b191cb5SApple OSS Distributions void *pmdv_copyout_buf;
54*1b191cb5SApple OSS Distributions lck_mtx_t pmdv_mutex;
55*1b191cb5SApple OSS Distributions perfmon_config_t pmdv_config;
56*1b191cb5SApple OSS Distributions bool pmdv_allocated;
57*1b191cb5SApple OSS Distributions };
58*1b191cb5SApple OSS Distributions
59*1b191cb5SApple OSS Distributions struct perfmon_device perfmon_devices[perfmon_kind_max][PERFMON_DEVICES_MAX]
60*1b191cb5SApple OSS Distributions = { 0 };
61*1b191cb5SApple OSS Distributions // perfmon_devices is protected by perfmon_devices_lock. If both a per-device
62*1b191cb5SApple OSS Distributions // mutex and the devices lock are taken together, the devices lock should be
63*1b191cb5SApple OSS Distributions // taken first.
64*1b191cb5SApple OSS Distributions LCK_MTX_DECLARE(perfmon_devices_lock, &perfmon_dev_lock_group);
65*1b191cb5SApple OSS Distributions
66*1b191cb5SApple OSS Distributions static int
perfmon_dev_get_source_index(dev_t dev)67*1b191cb5SApple OSS Distributions perfmon_dev_get_source_index(dev_t dev)
68*1b191cb5SApple OSS Distributions {
69*1b191cb5SApple OSS Distributions int dmaj = major(dev);
70*1b191cb5SApple OSS Distributions for (int i = 0; i < perfmon_kind_max; i++) {
71*1b191cb5SApple OSS Distributions if (perfmon_dev_major_sources[i] == dmaj) {
72*1b191cb5SApple OSS Distributions return i;
73*1b191cb5SApple OSS Distributions }
74*1b191cb5SApple OSS Distributions }
75*1b191cb5SApple OSS Distributions panic("perfmon: no source for major device: 0x%x", dev);
76*1b191cb5SApple OSS Distributions }
77*1b191cb5SApple OSS Distributions
78*1b191cb5SApple OSS Distributions static struct perfmon_device *
perfmon_dev_get_device(dev_t dev)79*1b191cb5SApple OSS Distributions perfmon_dev_get_device(dev_t dev)
80*1b191cb5SApple OSS Distributions {
81*1b191cb5SApple OSS Distributions int source_index = perfmon_dev_get_source_index(dev);
82*1b191cb5SApple OSS Distributions int dmin = minor(dev);
83*1b191cb5SApple OSS Distributions if (dmin >= perfmon_kind_max || dmin < 0) {
84*1b191cb5SApple OSS Distributions return NULL;
85*1b191cb5SApple OSS Distributions }
86*1b191cb5SApple OSS Distributions
87*1b191cb5SApple OSS Distributions return &perfmon_devices[source_index][dmin];
88*1b191cb5SApple OSS Distributions }
89*1b191cb5SApple OSS Distributions
90*1b191cb5SApple OSS Distributions static struct perfmon_source *
perfmon_dev_get_source(dev_t dev)91*1b191cb5SApple OSS Distributions perfmon_dev_get_source(dev_t dev)
92*1b191cb5SApple OSS Distributions {
93*1b191cb5SApple OSS Distributions return &perfmon_sources[perfmon_dev_get_source_index(dev)];
94*1b191cb5SApple OSS Distributions }
95*1b191cb5SApple OSS Distributions
96*1b191cb5SApple OSS Distributions static size_t
perfmon_device_copyout_size(struct perfmon_source * source)97*1b191cb5SApple OSS Distributions perfmon_device_copyout_size(struct perfmon_source *source)
98*1b191cb5SApple OSS Distributions {
99*1b191cb5SApple OSS Distributions struct perfmon_layout *layout = &source->ps_layout;
100*1b191cb5SApple OSS Distributions size_t counters_size = layout->pl_counter_count * layout->pl_unit_count *
101*1b191cb5SApple OSS Distributions sizeof(uint64_t);
102*1b191cb5SApple OSS Distributions size_t reg_names_size = layout->pl_reg_count * sizeof(perfmon_name_t);
103*1b191cb5SApple OSS Distributions size_t reg_values_size = layout->pl_reg_count * layout->pl_unit_count *
104*1b191cb5SApple OSS Distributions sizeof(uint64_t);
105*1b191cb5SApple OSS Distributions size_t attrs_size = layout->pl_attr_count * sizeof(struct perfmon_attr);
106*1b191cb5SApple OSS Distributions
107*1b191cb5SApple OSS Distributions return MAX(counters_size, MAX(reg_names_size,
108*1b191cb5SApple OSS Distributions MAX(attrs_size, reg_values_size)));
109*1b191cb5SApple OSS Distributions }
110*1b191cb5SApple OSS Distributions
111*1b191cb5SApple OSS Distributions static int
perfmon_dev_open(dev_t dev,int flags,int __unused devtype,proc_t __unused p)112*1b191cb5SApple OSS Distributions perfmon_dev_open(dev_t dev, int flags, int __unused devtype, proc_t __unused p)
113*1b191cb5SApple OSS Distributions {
114*1b191cb5SApple OSS Distributions lck_mtx_lock(&perfmon_devices_lock);
115*1b191cb5SApple OSS Distributions struct perfmon_device *device = perfmon_dev_get_device(dev);
116*1b191cb5SApple OSS Distributions struct perfmon_source *source = perfmon_dev_get_source(dev);
117*1b191cb5SApple OSS Distributions if (!device) {
118*1b191cb5SApple OSS Distributions return ENXIO;
119*1b191cb5SApple OSS Distributions }
120*1b191cb5SApple OSS Distributions if (((flags & O_RDWR) == O_RDWR)) {
121*1b191cb5SApple OSS Distributions if (!perfmon_acquire(source->ps_kind, "perfmon")) {
122*1b191cb5SApple OSS Distributions return ETXTBSY;
123*1b191cb5SApple OSS Distributions }
124*1b191cb5SApple OSS Distributions }
125*1b191cb5SApple OSS Distributions if (device->pmdv_allocated) {
126*1b191cb5SApple OSS Distributions return EMFILE;
127*1b191cb5SApple OSS Distributions }
128*1b191cb5SApple OSS Distributions if (!source->ps_supported) {
129*1b191cb5SApple OSS Distributions panic("perfmon: attempt to open unsupported source: 0x%x", dev);
130*1b191cb5SApple OSS Distributions }
131*1b191cb5SApple OSS Distributions device->pmdv_allocated = true;
132*1b191cb5SApple OSS Distributions device->pmdv_copyout_buf = kalloc_data(perfmon_device_copyout_size(source), Z_WAITOK);
133*1b191cb5SApple OSS Distributions if ((flags & O_RDWR) == O_RDWR) {
134*1b191cb5SApple OSS Distributions device->pmdv_config = perfmon_config_create(source);
135*1b191cb5SApple OSS Distributions }
136*1b191cb5SApple OSS Distributions lck_mtx_unlock(&perfmon_devices_lock);
137*1b191cb5SApple OSS Distributions
138*1b191cb5SApple OSS Distributions return 0;
139*1b191cb5SApple OSS Distributions }
140*1b191cb5SApple OSS Distributions
141*1b191cb5SApple OSS Distributions static int
perfmon_dev_clone(dev_t dev,int action)142*1b191cb5SApple OSS Distributions perfmon_dev_clone(dev_t dev, int action)
143*1b191cb5SApple OSS Distributions {
144*1b191cb5SApple OSS Distributions int minor = 0;
145*1b191cb5SApple OSS Distributions
146*1b191cb5SApple OSS Distributions lck_mtx_lock(&perfmon_devices_lock);
147*1b191cb5SApple OSS Distributions
148*1b191cb5SApple OSS Distributions switch (action) {
149*1b191cb5SApple OSS Distributions case DEVFS_CLONE_ALLOC:;
150*1b191cb5SApple OSS Distributions int source_index = perfmon_dev_get_source_index(dev);
151*1b191cb5SApple OSS Distributions for (unsigned int i = 0; i < PERFMON_DEVICES_MAX; i++) {
152*1b191cb5SApple OSS Distributions struct perfmon_device *device = &perfmon_devices[source_index][i];
153*1b191cb5SApple OSS Distributions if (!device->pmdv_allocated) {
154*1b191cb5SApple OSS Distributions minor = i;
155*1b191cb5SApple OSS Distributions break;
156*1b191cb5SApple OSS Distributions }
157*1b191cb5SApple OSS Distributions }
158*1b191cb5SApple OSS Distributions // Returning non-zero from the alloc action hangs devfs, so let the open
159*1b191cb5SApple OSS Distributions // handler figure out that EMFILE should be returned.
160*1b191cb5SApple OSS Distributions break;
161*1b191cb5SApple OSS Distributions case DEVFS_CLONE_FREE:
162*1b191cb5SApple OSS Distributions // Nothing to do since a device wasn't allocated until the call to open.
163*1b191cb5SApple OSS Distributions break;
164*1b191cb5SApple OSS Distributions default:
165*1b191cb5SApple OSS Distributions minor = -1;
166*1b191cb5SApple OSS Distributions break;
167*1b191cb5SApple OSS Distributions }
168*1b191cb5SApple OSS Distributions
169*1b191cb5SApple OSS Distributions lck_mtx_unlock(&perfmon_devices_lock);
170*1b191cb5SApple OSS Distributions
171*1b191cb5SApple OSS Distributions return minor;
172*1b191cb5SApple OSS Distributions }
173*1b191cb5SApple OSS Distributions
174*1b191cb5SApple OSS Distributions static int
perfmon_dev_close(dev_t dev,int __unused flags,int __unused devtype,proc_t __unused p)175*1b191cb5SApple OSS Distributions perfmon_dev_close(dev_t dev, int __unused flags, int __unused devtype,
176*1b191cb5SApple OSS Distributions proc_t __unused p)
177*1b191cb5SApple OSS Distributions {
178*1b191cb5SApple OSS Distributions lck_mtx_lock(&perfmon_devices_lock);
179*1b191cb5SApple OSS Distributions
180*1b191cb5SApple OSS Distributions struct perfmon_device *device = perfmon_dev_get_device(dev);
181*1b191cb5SApple OSS Distributions
182*1b191cb5SApple OSS Distributions lck_mtx_lock(&device->pmdv_mutex);
183*1b191cb5SApple OSS Distributions
184*1b191cb5SApple OSS Distributions if (!device->pmdv_allocated) {
185*1b191cb5SApple OSS Distributions panic("perfmon: no device allocated to close: 0x%x", dev);
186*1b191cb5SApple OSS Distributions }
187*1b191cb5SApple OSS Distributions device->pmdv_allocated = false;
188*1b191cb5SApple OSS Distributions struct perfmon_source *source = perfmon_dev_get_source(dev);
189*1b191cb5SApple OSS Distributions kfree_data(device->pmdv_copyout_buf, perfmon_device_copyout_size(source));
190*1b191cb5SApple OSS Distributions device->pmdv_copyout_buf = NULL;
191*1b191cb5SApple OSS Distributions if (device->pmdv_config) {
192*1b191cb5SApple OSS Distributions perfmon_release(source->ps_kind, "perfmon");
193*1b191cb5SApple OSS Distributions perfmon_config_destroy(device->pmdv_config);
194*1b191cb5SApple OSS Distributions device->pmdv_config = NULL;
195*1b191cb5SApple OSS Distributions }
196*1b191cb5SApple OSS Distributions
197*1b191cb5SApple OSS Distributions lck_mtx_unlock(&device->pmdv_mutex);
198*1b191cb5SApple OSS Distributions lck_mtx_unlock(&perfmon_devices_lock);
199*1b191cb5SApple OSS Distributions
200*1b191cb5SApple OSS Distributions return 0;
201*1b191cb5SApple OSS Distributions }
202*1b191cb5SApple OSS Distributions
203*1b191cb5SApple OSS Distributions static int
perfmon_dev_ioctl(dev_t dev,unsigned long cmd,char * arg,int __unused fflag,proc_t __unused p)204*1b191cb5SApple OSS Distributions perfmon_dev_ioctl(dev_t dev, unsigned long cmd, char *arg,
205*1b191cb5SApple OSS Distributions int __unused fflag, proc_t __unused p)
206*1b191cb5SApple OSS Distributions {
207*1b191cb5SApple OSS Distributions struct perfmon_device *device = perfmon_dev_get_device(dev);
208*1b191cb5SApple OSS Distributions struct perfmon_source *source = perfmon_dev_get_source(dev);
209*1b191cb5SApple OSS Distributions int ret = 0;
210*1b191cb5SApple OSS Distributions
211*1b191cb5SApple OSS Distributions lck_mtx_lock(&device->pmdv_mutex);
212*1b191cb5SApple OSS Distributions
213*1b191cb5SApple OSS Distributions unsigned short reg_count = source->ps_layout.pl_reg_count;
214*1b191cb5SApple OSS Distributions unsigned short unit_count = source->ps_layout.pl_unit_count;
215*1b191cb5SApple OSS Distributions
216*1b191cb5SApple OSS Distributions switch (cmd) {
217*1b191cb5SApple OSS Distributions case PERFMON_CTL_GET_LAYOUT:;
218*1b191cb5SApple OSS Distributions struct perfmon_layout *layout = (void *)arg;
219*1b191cb5SApple OSS Distributions *layout = source->ps_layout;
220*1b191cb5SApple OSS Distributions ret = 0;
221*1b191cb5SApple OSS Distributions break;
222*1b191cb5SApple OSS Distributions
223*1b191cb5SApple OSS Distributions case PERFMON_CTL_LIST_REGS: {
224*1b191cb5SApple OSS Distributions user_addr_t uptr = *(user_addr_t *)(void *)arg;
225*1b191cb5SApple OSS Distributions size_t names_size = reg_count * sizeof(source->ps_register_names[0]);
226*1b191cb5SApple OSS Distributions ret = copyout(source->ps_register_names, uptr, names_size);
227*1b191cb5SApple OSS Distributions break;
228*1b191cb5SApple OSS Distributions }
229*1b191cb5SApple OSS Distributions
230*1b191cb5SApple OSS Distributions case PERFMON_CTL_SAMPLE_REGS: {
231*1b191cb5SApple OSS Distributions user_addr_t uptr = *(user_addr_t *)(void *)arg;
232*1b191cb5SApple OSS Distributions uint64_t *sample_buf = device->pmdv_copyout_buf;
233*1b191cb5SApple OSS Distributions size_t sample_size = reg_count * unit_count * sizeof(sample_buf[0]);
234*1b191cb5SApple OSS Distributions perfmon_source_sample_regs(source, sample_buf, reg_count);
235*1b191cb5SApple OSS Distributions ret = copyout(sample_buf, uptr, sample_size);
236*1b191cb5SApple OSS Distributions break;
237*1b191cb5SApple OSS Distributions }
238*1b191cb5SApple OSS Distributions
239*1b191cb5SApple OSS Distributions case PERFMON_CTL_LIST_ATTRS: {
240*1b191cb5SApple OSS Distributions user_addr_t uptr = *(user_addr_t *)(void *)arg;
241*1b191cb5SApple OSS Distributions unsigned short attr_count = source->ps_layout.pl_attr_count;
242*1b191cb5SApple OSS Distributions const perfmon_name_t *attrs_buf = source->ps_attribute_names;
243*1b191cb5SApple OSS Distributions size_t attrs_size = attr_count * sizeof(attrs_buf[0]);
244*1b191cb5SApple OSS Distributions ret = copyout(attrs_buf, uptr, attrs_size);
245*1b191cb5SApple OSS Distributions break;
246*1b191cb5SApple OSS Distributions }
247*1b191cb5SApple OSS Distributions
248*1b191cb5SApple OSS Distributions case PERFMON_CTL_ADD_EVENT:
249*1b191cb5SApple OSS Distributions if (device->pmdv_config) {
250*1b191cb5SApple OSS Distributions struct perfmon_event *event = (void *)arg;
251*1b191cb5SApple OSS Distributions event->pe_name[sizeof(event->pe_name) - 1] = '\0';
252*1b191cb5SApple OSS Distributions ret = perfmon_config_add_event(device->pmdv_config, event);
253*1b191cb5SApple OSS Distributions } else {
254*1b191cb5SApple OSS Distributions ret = EBADF;
255*1b191cb5SApple OSS Distributions }
256*1b191cb5SApple OSS Distributions break;
257*1b191cb5SApple OSS Distributions
258*1b191cb5SApple OSS Distributions case PERFMON_CTL_SET_ATTR:
259*1b191cb5SApple OSS Distributions if (device->pmdv_config) {
260*1b191cb5SApple OSS Distributions struct perfmon_attr *attr = (void *)arg;
261*1b191cb5SApple OSS Distributions attr->pa_name[sizeof(attr->pa_name) - 1] = '\0';
262*1b191cb5SApple OSS Distributions ret = perfmon_config_set_attr(device->pmdv_config, attr);
263*1b191cb5SApple OSS Distributions } else {
264*1b191cb5SApple OSS Distributions ret = EBADF;
265*1b191cb5SApple OSS Distributions }
266*1b191cb5SApple OSS Distributions break;
267*1b191cb5SApple OSS Distributions
268*1b191cb5SApple OSS Distributions case PERFMON_CTL_CONFIGURE:
269*1b191cb5SApple OSS Distributions if (device->pmdv_config) {
270*1b191cb5SApple OSS Distributions ret = perfmon_configure(device->pmdv_config);
271*1b191cb5SApple OSS Distributions } else {
272*1b191cb5SApple OSS Distributions ret = EBADF;
273*1b191cb5SApple OSS Distributions }
274*1b191cb5SApple OSS Distributions break;
275*1b191cb5SApple OSS Distributions
276*1b191cb5SApple OSS Distributions case PERFMON_CTL_START:
277*1b191cb5SApple OSS Distributions ret = ENOTSUP;
278*1b191cb5SApple OSS Distributions break;
279*1b191cb5SApple OSS Distributions
280*1b191cb5SApple OSS Distributions case PERFMON_CTL_STOP:
281*1b191cb5SApple OSS Distributions ret = ENOTSUP;
282*1b191cb5SApple OSS Distributions break;
283*1b191cb5SApple OSS Distributions
284*1b191cb5SApple OSS Distributions case PERFMON_CTL_SPECIFY:;
285*1b191cb5SApple OSS Distributions struct perfmon_config *config = device->pmdv_config;
286*1b191cb5SApple OSS Distributions if (config) {
287*1b191cb5SApple OSS Distributions struct perfmon_spec *uspec = (void *)arg;
288*1b191cb5SApple OSS Distributions struct perfmon_spec *kspec = perfmon_config_specify(config);
289*1b191cb5SApple OSS Distributions if (uspec->ps_events) {
290*1b191cb5SApple OSS Distributions ret = copyout(kspec->ps_events, (user_addr_t)uspec->ps_events,
291*1b191cb5SApple OSS Distributions MIN(uspec->ps_event_count, kspec->ps_event_count));
292*1b191cb5SApple OSS Distributions if (0 == ret && uspec->ps_attrs) {
293*1b191cb5SApple OSS Distributions ret = copyout(kspec->ps_attrs, (user_addr_t)uspec->ps_attrs,
294*1b191cb5SApple OSS Distributions MIN(uspec->ps_attr_count, kspec->ps_attr_count));
295*1b191cb5SApple OSS Distributions }
296*1b191cb5SApple OSS Distributions }
297*1b191cb5SApple OSS Distributions uspec->ps_event_count = kspec->ps_event_count;
298*1b191cb5SApple OSS Distributions uspec->ps_attr_count = kspec->ps_event_count;
299*1b191cb5SApple OSS Distributions } else {
300*1b191cb5SApple OSS Distributions ret = EBADF;
301*1b191cb5SApple OSS Distributions }
302*1b191cb5SApple OSS Distributions break;
303*1b191cb5SApple OSS Distributions
304*1b191cb5SApple OSS Distributions default:
305*1b191cb5SApple OSS Distributions ret = ENOTSUP;
306*1b191cb5SApple OSS Distributions break;
307*1b191cb5SApple OSS Distributions }
308*1b191cb5SApple OSS Distributions
309*1b191cb5SApple OSS Distributions lck_mtx_unlock(&device->pmdv_mutex);
310*1b191cb5SApple OSS Distributions
311*1b191cb5SApple OSS Distributions return ret;
312*1b191cb5SApple OSS Distributions }
313*1b191cb5SApple OSS Distributions
314*1b191cb5SApple OSS Distributions static const struct cdevsw perfmon_cdevsw = {
315*1b191cb5SApple OSS Distributions .d_open = perfmon_dev_open, .d_close = perfmon_dev_close,
316*1b191cb5SApple OSS Distributions .d_ioctl = perfmon_dev_ioctl,
317*1b191cb5SApple OSS Distributions
318*1b191cb5SApple OSS Distributions .d_read = eno_rdwrt, .d_write = eno_rdwrt, .d_stop = eno_stop,
319*1b191cb5SApple OSS Distributions .d_reset = eno_reset, .d_ttys = NULL, .d_select = eno_select,
320*1b191cb5SApple OSS Distributions .d_mmap = eno_mmap, .d_strategy = eno_strat, .d_type = 0,
321*1b191cb5SApple OSS Distributions };
322*1b191cb5SApple OSS Distributions
323*1b191cb5SApple OSS Distributions int
perfmon_dev_init(void)324*1b191cb5SApple OSS Distributions perfmon_dev_init(void)
325*1b191cb5SApple OSS Distributions {
326*1b191cb5SApple OSS Distributions for (unsigned int i = 0; i < perfmon_kind_max; i++) {
327*1b191cb5SApple OSS Distributions struct perfmon_source *source = &perfmon_sources[i];
328*1b191cb5SApple OSS Distributions if (!source->ps_supported) {
329*1b191cb5SApple OSS Distributions continue;
330*1b191cb5SApple OSS Distributions }
331*1b191cb5SApple OSS Distributions
332*1b191cb5SApple OSS Distributions int dmaj = cdevsw_add(-1, &perfmon_cdevsw);
333*1b191cb5SApple OSS Distributions if (dmaj < 0) {
334*1b191cb5SApple OSS Distributions panic("perfmon: %s: cdevsw_add failed: 0x%x", source->ps_name,
335*1b191cb5SApple OSS Distributions dmaj);
336*1b191cb5SApple OSS Distributions }
337*1b191cb5SApple OSS Distributions perfmon_dev_major_sources[i] = dmaj;
338*1b191cb5SApple OSS Distributions void *node = devfs_make_node_clone(makedev(dmaj, 0), DEVFS_CHAR,
339*1b191cb5SApple OSS Distributions UID_ROOT, GID_WHEEL, 0666, perfmon_dev_clone, "perfmon_%s",
340*1b191cb5SApple OSS Distributions source->ps_name);
341*1b191cb5SApple OSS Distributions if (!node) {
342*1b191cb5SApple OSS Distributions panic("perfmon: %s: devfs_make_node_clone failed",
343*1b191cb5SApple OSS Distributions source->ps_name);
344*1b191cb5SApple OSS Distributions }
345*1b191cb5SApple OSS Distributions
346*1b191cb5SApple OSS Distributions for (size_t j = 0; j < PERFMON_DEVICES_MAX; j++) {
347*1b191cb5SApple OSS Distributions lck_mtx_init(&perfmon_devices[i][j].pmdv_mutex,
348*1b191cb5SApple OSS Distributions &perfmon_dev_lock_group, NULL);
349*1b191cb5SApple OSS Distributions }
350*1b191cb5SApple OSS Distributions }
351*1b191cb5SApple OSS Distributions
352*1b191cb5SApple OSS Distributions return 0;
353*1b191cb5SApple OSS Distributions }
354