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