xref: /xnu-8792.61.2/bsd/dev/dtrace/profile_prvd.c (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <kern/cpu_data.h>
27 #include <kern/thread.h>
28 #include <kern/assert.h>
29 #include <mach/thread_status.h>
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <sys/conf.h>
37 #include <sys/fcntl.h>
38 #include <miscfs/devfs/devfs.h>
39 
40 #include <sys/dtrace.h>
41 #include <sys/dtrace_impl.h>
42 
43 #include <sys/dtrace_glue.h>
44 
45 #include <machine/pal_routines.h>
46 
47 #if defined(__x86_64__)
48 extern x86_saved_state_t *find_kern_regs(thread_t);
49 #elif defined(__arm64__)
50 extern struct arm_saved_state *find_kern_regs(thread_t);
51 #else
52 #error Unknown architecture
53 #endif
54 
55 extern void profile_init(void);
56 
57 static dtrace_provider_id_t profile_id;
58 
59 /*
60  * Regardless of platform, the stack frames look like this in the case of the
61  * profile provider:
62  *
63  *	profile_fire
64  *	cyclic_expire
65  *	cyclic_fire
66  *	[ cbe ]
67  *	[ interrupt code ]
68  *
69  * On x86, there are five frames from the generic interrupt code; further, the
70  * interrupted instruction appears as its own stack frame, giving us a total of
71  * 10.
72  *
73  * On SPARC, the picture is further complicated because the compiler
74  * optimizes away tail-calls -- so the following frames are optimized away:
75  *
76  *      profile_fire
77  *	cyclic_expire
78  *
79  * This gives three frames.  However, on DEBUG kernels, the cyclic_expire
80  * frame cannot be tail-call eliminated, yielding four frames in this case.
81  *
82  * All of the above constraints lead to the mess below.  Yes, the profile
83  * provider should ideally figure this out on-the-fly by hitting one of its own
84  * probes and then walking its own stack trace.  This is complicated, however,
85  * and the static definition doesn't seem to be overly brittle.  Still, we
86  * allow for a manual override in case we get it completely wrong.
87  */
88 
89 #if defined(__x86_64__)
90 #define PROF_ARTIFICIAL_FRAMES  9
91 #elif defined(__arm64__)
92 #define PROF_ARTIFICIAL_FRAMES 8
93 #else
94 #error Unknown architecture
95 #endif
96 
97 #define PROF_NAMELEN            15
98 
99 #define PROF_PROFILE            0
100 #define PROF_TICK               1
101 #define PROF_PREFIX_PROFILE     "profile-"
102 #define PROF_PREFIX_TICK        "tick-"
103 
104 typedef struct profile_probe {
105 	char            prof_name[PROF_NAMELEN];
106 	dtrace_id_t     prof_id;
107 	int             prof_kind;
108 	hrtime_t        prof_interval;
109 	cyclic_id_t     prof_cyclic;
110 } profile_probe_t;
111 
112 typedef struct profile_probe_percpu {
113 	hrtime_t        profc_expected;
114 	hrtime_t        profc_interval;
115 	profile_probe_t *profc_probe;
116 } profile_probe_percpu_t;
117 
118 hrtime_t        profile_interval_min = NANOSEC / 5000;          /* 5000 hz */
119 int             profile_aframes = 0;                            /* override */
120 
121 static int profile_rates[] = {
122 	97, 199, 499, 997, 1999,
123 	4001, 4999, 0, 0, 0,
124 	0, 0, 0, 0, 0,
125 	0, 0, 0, 0, 0
126 };
127 
128 static int profile_ticks[] = {
129 	1, 10, 100, 500, 1000,
130 	5000, 0, 0, 0, 0,
131 	0, 0, 0, 0, 0
132 };
133 
134 /*
135  * profile_max defines the upper bound on the number of profile probes that
136  * can exist (this is to prevent malicious or clumsy users from exhausing
137  * system resources by creating a slew of profile probes). At mod load time,
138  * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
139  * present in the profile.conf file.
140  */
141 #define PROFILE_MAX_DEFAULT     1000    /* default max. number of probes */
142 static uint32_t profile_max;            /* maximum number of profile probes */
143 static uint32_t profile_total;  /* current number of profile probes */
144 
145 static void
profile_fire(void * arg)146 profile_fire(void *arg)
147 {
148 	profile_probe_percpu_t *pcpu = arg;
149 	profile_probe_t *prof = pcpu->profc_probe;
150 	hrtime_t late;
151 
152 	late = dtrace_gethrtime() - pcpu->profc_expected;
153 	pcpu->profc_expected += pcpu->profc_interval;
154 
155 #if defined(__x86_64__)
156 	x86_saved_state_t *kern_regs = find_kern_regs(current_thread());
157 
158 	if (NULL != kern_regs) {
159 		/* Kernel was interrupted. */
160 		dtrace_probe(prof->prof_id, saved_state64(kern_regs)->isf.rip, 0x0, late, 0, 0);
161 	} else {
162 		pal_register_cache_state(current_thread(), VALID);
163 		/* Possibly a user interrupt */
164 		x86_saved_state_t   *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread());
165 
166 		if (NULL == tagged_regs) {
167 			/* Too bad, so sad, no useful interrupt state. */
168 			dtrace_probe(prof->prof_id, 0xcafebabe,
169 			    0x0, late, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */
170 		} else if (is_saved_state64(tagged_regs)) {
171 			x86_saved_state64_t *regs = saved_state64(tagged_regs);
172 
173 			dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, late, 0, 0);
174 		} else {
175 			x86_saved_state32_t *regs = saved_state32(tagged_regs);
176 
177 			dtrace_probe(prof->prof_id, 0x0, regs->eip, late, 0, 0);
178 		}
179 	}
180 #elif defined(__arm64__)
181 	{
182 		arm_saved_state_t *arm_kern_regs = (arm_saved_state_t *) find_kern_regs(current_thread());
183 
184 		// We should only come in here from interrupt context, so we should always have valid kernel regs
185 		assert(NULL != arm_kern_regs);
186 
187 		if (saved_state64(arm_kern_regs)->cpsr & 0xF) {
188 			/* Kernel was interrupted. */
189 			dtrace_probe(prof->prof_id, saved_state64(arm_kern_regs)->pc, 0x0, late, 0, 0);
190 		} else {
191 			/* Possibly a user interrupt */
192 			arm_saved_state_t   *arm_user_regs = (arm_saved_state_t *)find_user_regs(current_thread());
193 
194 			if (NULL == arm_user_regs) {
195 				/* Too bad, so sad, no useful interrupt state. */
196 				dtrace_probe(prof->prof_id, 0xcafebabe, 0x0, late, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */
197 			} else {
198 				dtrace_probe(prof->prof_id, 0x0, get_saved_state_pc(arm_user_regs), late, 0, 0);
199 			}
200 		}
201 	}
202 #else
203 #error Unknown architecture
204 #endif
205 }
206 
207 static void
profile_tick(void * arg)208 profile_tick(void *arg)
209 {
210 	profile_probe_t *prof = arg;
211 
212 #if defined(__x86_64__)
213 	x86_saved_state_t *kern_regs = find_kern_regs(current_thread());
214 
215 	if (NULL != kern_regs) {
216 		/* Kernel was interrupted. */
217 		dtrace_probe(prof->prof_id, saved_state64(kern_regs)->isf.rip, 0x0, 0, 0, 0);
218 	} else {
219 		pal_register_cache_state(current_thread(), VALID);
220 		/* Possibly a user interrupt */
221 		x86_saved_state_t   *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread());
222 
223 		if (NULL == tagged_regs) {
224 			/* Too bad, so sad, no useful interrupt state. */
225 			dtrace_probe(prof->prof_id, 0xcafebabe,
226 			    0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */
227 		} else if (is_saved_state64(tagged_regs)) {
228 			x86_saved_state64_t *regs = saved_state64(tagged_regs);
229 
230 			dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, 0, 0, 0);
231 		} else {
232 			x86_saved_state32_t *regs = saved_state32(tagged_regs);
233 
234 			dtrace_probe(prof->prof_id, 0x0, regs->eip, 0, 0, 0);
235 		}
236 	}
237 #elif defined(__arm64__)
238 	{
239 		arm_saved_state_t *arm_kern_regs = (arm_saved_state_t *) find_kern_regs(current_thread());
240 
241 		if (NULL != arm_kern_regs) {
242 			/* Kernel was interrupted. */
243 			dtrace_probe(prof->prof_id, saved_state64(arm_kern_regs)->pc, 0x0, 0, 0, 0);
244 		} else {
245 			/* Possibly a user interrupt */
246 			arm_saved_state_t   *arm_user_regs = (arm_saved_state_t *)find_user_regs(current_thread());
247 
248 			if (NULL == arm_user_regs) {
249 				/* Too bad, so sad, no useful interrupt state. */
250 				dtrace_probe(prof->prof_id, 0xcafebabe, 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */
251 			} else {
252 				dtrace_probe(prof->prof_id, 0x0, get_saved_state_pc(arm_user_regs), 0, 0, 0);
253 			}
254 		}
255 	}
256 
257 #else
258 #error Unknown architecture
259 #endif
260 }
261 
262 static void
profile_create(hrtime_t interval,const char * name,int kind)263 profile_create(hrtime_t interval, const char *name, int kind)
264 {
265 	profile_probe_t *prof;
266 
267 	if (interval < profile_interval_min) {
268 		return;
269 	}
270 
271 	if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0) {
272 		return;
273 	}
274 
275 	os_atomic_inc(&profile_total, relaxed);
276 	if (profile_total > profile_max) {
277 		os_atomic_dec(&profile_total, relaxed);
278 		return;
279 	}
280 
281 	if (PROF_TICK == kind) {
282 		prof = kmem_zalloc(sizeof(profile_probe_t), KM_SLEEP);
283 	} else {
284 		prof = kmem_zalloc(sizeof(profile_probe_t) + NCPU * sizeof(profile_probe_percpu_t), KM_SLEEP);
285 	}
286 
287 	(void) strlcpy(prof->prof_name, name, sizeof(prof->prof_name));
288 	prof->prof_interval = interval;
289 	prof->prof_cyclic = CYCLIC_NONE;
290 	prof->prof_kind = kind;
291 	prof->prof_id = dtrace_probe_create(profile_id,
292 	    NULL, NULL, name,
293 	    profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof);
294 }
295 
296 /*ARGSUSED*/
297 static void
profile_provide(void * arg,const dtrace_probedesc_t * desc)298 profile_provide(void *arg, const dtrace_probedesc_t *desc)
299 {
300 #pragma unused(arg) /* __APPLE__ */
301 	int i, j, rate, kind;
302 	hrtime_t val = 0, mult = 1, len;
303 	const char *name, *suffix = NULL;
304 
305 	const struct {
306 		const char *prefix;
307 		int kind;
308 	} types[] = {
309 		{ PROF_PREFIX_PROFILE, PROF_PROFILE },
310 		{ PROF_PREFIX_TICK, PROF_TICK },
311 		{ NULL, 0 }
312 	};
313 
314 	const struct {
315 		const char *name;
316 		hrtime_t mult;
317 	} suffixes[] = {
318 		{ "ns", NANOSEC / NANOSEC },
319 		{ "nsec", NANOSEC / NANOSEC },
320 		{ "us", NANOSEC / MICROSEC },
321 		{ "usec", NANOSEC / MICROSEC },
322 		{ "ms", NANOSEC / MILLISEC },
323 		{ "msec", NANOSEC / MILLISEC },
324 		{ "s", NANOSEC / SEC },
325 		{ "sec", NANOSEC / SEC },
326 		{ "m", NANOSEC * (hrtime_t)60 },
327 		{ "min", NANOSEC * (hrtime_t)60 },
328 		{ "h", NANOSEC * (hrtime_t)(60 * 60) },
329 		{ "hour", NANOSEC * (hrtime_t)(60 * 60) },
330 		{ "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
331 		{ "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
332 		{ "hz", 0 },
333 		{ NULL, 0 }
334 	};
335 
336 	if (desc == NULL) {
337 		char n[PROF_NAMELEN];
338 
339 		/*
340 		 * If no description was provided, provide all of our probes.
341 		 */
342 		for (i = 0; i < (int)(sizeof(profile_rates) / sizeof(int)); i++) {
343 			if ((rate = profile_rates[i]) == 0) {
344 				continue;
345 			}
346 
347 			(void) snprintf(n, PROF_NAMELEN, "%s%d",
348 			    PROF_PREFIX_PROFILE, rate);
349 			profile_create(NANOSEC / rate, n, PROF_PROFILE);
350 		}
351 
352 		for (i = 0; i < (int)(sizeof(profile_ticks) / sizeof(int)); i++) {
353 			if ((rate = profile_ticks[i]) == 0) {
354 				continue;
355 			}
356 
357 			(void) snprintf(n, PROF_NAMELEN, "%s%d",
358 			    PROF_PREFIX_TICK, rate);
359 			profile_create(NANOSEC / rate, n, PROF_TICK);
360 		}
361 
362 		return;
363 	}
364 
365 	name = desc->dtpd_name;
366 
367 	for (i = 0; types[i].prefix != NULL; i++) {
368 		len = strlen(types[i].prefix);
369 
370 		if (strncmp(name, types[i].prefix, len) != 0) {
371 			continue;
372 		}
373 		break;
374 	}
375 
376 	if (types[i].prefix == NULL) {
377 		return;
378 	}
379 
380 	kind = types[i].kind;
381 	j = strlen(name) - len;
382 
383 	/*
384 	 * We need to start before any time suffix.
385 	 */
386 	for (j = strlen(name); j >= len; j--) {
387 		if (name[j] >= '0' && name[j] <= '9') {
388 			break;
389 		}
390 		suffix = &name[j];
391 	}
392 
393 	if (!suffix) {
394 		suffix = &name[strlen(name)];
395 	}
396 
397 	/*
398 	 * Now determine the numerical value present in the probe name.
399 	 */
400 	for (; j >= len; j--) {
401 		if (name[j] < '0' || name[j] > '9') {
402 			return;
403 		}
404 
405 		val += (name[j] - '0') * mult;
406 		mult *= (hrtime_t)10;
407 	}
408 
409 	if (val == 0) {
410 		return;
411 	}
412 
413 	/*
414 	 * Look-up the suffix to determine the multiplier.
415 	 */
416 	for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
417 		/* APPLE NOTE: Darwin employs size bounded string operations */
418 		if (strncasecmp(suffixes[i].name, suffix, strlen(suffixes[i].name) + 1) == 0) {
419 			mult = suffixes[i].mult;
420 			break;
421 		}
422 	}
423 
424 	if (suffixes[i].name == NULL && *suffix != '\0') {
425 		return;
426 	}
427 
428 	if (mult == 0) {
429 		/*
430 		 * The default is frequency-per-second.
431 		 */
432 		val = NANOSEC / val;
433 	} else {
434 		val *= mult;
435 	}
436 
437 	profile_create(val, name, kind);
438 }
439 
440 /*ARGSUSED*/
441 static void
profile_destroy(void * arg,dtrace_id_t id,void * parg)442 profile_destroy(void *arg, dtrace_id_t id, void *parg)
443 {
444 #pragma unused(arg,id) /* __APPLE__ */
445 	profile_probe_t *prof = parg;
446 
447 	ASSERT(prof->prof_cyclic == CYCLIC_NONE);
448 
449 	if (prof->prof_kind == PROF_TICK) {
450 		kmem_free(prof, sizeof(profile_probe_t));
451 	} else {
452 		kmem_free(prof, sizeof(profile_probe_t) + NCPU * sizeof(profile_probe_percpu_t));
453 	}
454 
455 	ASSERT(profile_total >= 1);
456 	os_atomic_dec(&profile_total, relaxed);
457 }
458 
459 /*ARGSUSED*/
460 static void
profile_online(void * arg,dtrace_cpu_t * cpu,cyc_handler_t * hdlr,cyc_time_t * when)461 profile_online(void *arg, dtrace_cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
462 {
463 #pragma unused(cpu) /* __APPLE__ */
464 	profile_probe_t *prof = arg;
465 	profile_probe_percpu_t *pcpu;
466 
467 	pcpu = ((profile_probe_percpu_t *)(&(prof[1]))) + cpu_number();
468 	pcpu->profc_probe = prof;
469 
470 	hdlr->cyh_func = profile_fire;
471 	hdlr->cyh_arg = pcpu;
472 	hdlr->cyh_level = CY_HIGH_LEVEL;
473 
474 	when->cyt_interval = prof->prof_interval;
475 	when->cyt_when = dtrace_gethrtime() + when->cyt_interval;
476 
477 	pcpu->profc_expected = when->cyt_when;
478 	pcpu->profc_interval = when->cyt_interval;
479 }
480 
481 /*ARGSUSED*/
482 static void
profile_offline(void * arg,dtrace_cpu_t * cpu,void * oarg)483 profile_offline(void *arg, dtrace_cpu_t *cpu, void *oarg)
484 {
485 	profile_probe_percpu_t *pcpu = oarg;
486 
487 	ASSERT(pcpu->profc_probe == arg);
488 #pragma unused(pcpu,arg,cpu) /* __APPLE__ */
489 }
490 
491 /*ARGSUSED*/
492 static int
profile_enable(void * arg,dtrace_id_t id,void * parg)493 profile_enable(void *arg, dtrace_id_t id, void *parg)
494 {
495 #pragma unused(arg,id) /* __APPLE__ */
496 	profile_probe_t *prof = parg;
497 	cyc_omni_handler_t omni;
498 	cyc_handler_t hdlr;
499 	cyc_time_t when;
500 
501 	ASSERT(prof->prof_interval != 0);
502 	ASSERT(MUTEX_HELD(&cpu_lock));
503 
504 	if (prof->prof_kind == PROF_TICK) {
505 		hdlr.cyh_func = profile_tick;
506 		hdlr.cyh_arg = prof;
507 		hdlr.cyh_level = CY_HIGH_LEVEL;
508 
509 		when.cyt_interval = prof->prof_interval;
510 #if !defined(__APPLE__)
511 		when.cyt_when = dtrace_gethrtime() + when.cyt_interval;
512 #else
513 		when.cyt_when = 0;
514 #endif /* __APPLE__ */
515 	} else {
516 		ASSERT(prof->prof_kind == PROF_PROFILE);
517 		omni.cyo_online = profile_online;
518 		omni.cyo_offline = profile_offline;
519 		omni.cyo_arg = prof;
520 	}
521 
522 	if (prof->prof_kind == PROF_TICK) {
523 		prof->prof_cyclic = cyclic_timer_add(&hdlr, &when);
524 	} else {
525 		prof->prof_cyclic = (cyclic_id_t)cyclic_add_omni(&omni); /* cast puns cyclic_id_list_t with cyclic_id_t */
526 	}
527 
528 	return 0;
529 }
530 
531 /*ARGSUSED*/
532 static void
profile_disable(void * arg,dtrace_id_t id,void * parg)533 profile_disable(void *arg, dtrace_id_t id, void *parg)
534 {
535 	profile_probe_t *prof = parg;
536 
537 	ASSERT(prof->prof_cyclic != CYCLIC_NONE);
538 	ASSERT(MUTEX_HELD(&cpu_lock));
539 
540 #pragma unused(arg,id)
541 	if (prof->prof_kind == PROF_TICK) {
542 		cyclic_timer_remove(prof->prof_cyclic);
543 	} else {
544 		cyclic_remove_omni((cyclic_id_list_t)prof->prof_cyclic); /* cast puns cyclic_id_list_t with cyclic_id_t */
545 	}
546 	prof->prof_cyclic = CYCLIC_NONE;
547 }
548 
549 static uint64_t
profile_getarg(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)550 profile_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
551 {
552 #pragma unused(arg, id, parg, argno, aframes)
553 	/*
554 	 * All the required arguments for the profile probe are passed directly
555 	 * to dtrace_probe, and we do not go through dtrace_getarg which doesn't
556 	 * know how to hop to the kernel stack from the interrupt stack like
557 	 * dtrace_getpcstack
558 	 */
559 	return 0;
560 }
561 
562 static void
profile_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)563 profile_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
564 {
565 #pragma unused(arg, id)
566 	profile_probe_t *prof = parg;
567 	const char *argdesc = NULL;
568 	switch (desc->dtargd_ndx) {
569 	case 0:
570 		argdesc = "void*";
571 		break;
572 	case 1:
573 		argdesc = "user_addr_t";
574 		break;
575 	case 2:
576 		if (prof->prof_kind == PROF_PROFILE) {
577 			argdesc = "hrtime_t";
578 		}
579 		break;
580 	}
581 	if (argdesc) {
582 		strlcpy(desc->dtargd_native, argdesc, DTRACE_ARGTYPELEN);
583 	} else {
584 		desc->dtargd_ndx = DTRACE_ARGNONE;
585 	}
586 }
587 
588 /*
589  * APPLE NOTE:  profile_usermode call not supported.
590  */
591 static int
profile_usermode(void * arg,dtrace_id_t id,void * parg)592 profile_usermode(void *arg, dtrace_id_t id, void *parg)
593 {
594 #pragma unused(arg,id,parg)
595 	return 1; /* XXX_BOGUS */
596 }
597 
598 static dtrace_pattr_t profile_attr = {
599 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
600 	{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN },
601 	{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
602 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
603 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
604 };
605 
606 static dtrace_pops_t profile_pops = {
607 	.dtps_provide =         profile_provide,
608 	.dtps_provide_module =  NULL,
609 	.dtps_enable =          profile_enable,
610 	.dtps_disable =         profile_disable,
611 	.dtps_suspend =         NULL,
612 	.dtps_resume =          NULL,
613 	.dtps_getargdesc =      profile_getargdesc,
614 	.dtps_getargval =       profile_getarg,
615 	.dtps_usermode =        profile_usermode,
616 	.dtps_destroy =         profile_destroy
617 };
618 
619 static int
profile_attach(dev_info_t * devi)620 profile_attach(dev_info_t *devi)
621 {
622 	if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0,
623 	    DDI_PSEUDO, 0) == DDI_FAILURE ||
624 	    dtrace_register("profile", &profile_attr,
625 	    DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL,
626 	    &profile_pops, NULL, &profile_id) != 0) {
627 		ddi_remove_minor_node(devi, NULL);
628 		return DDI_FAILURE;
629 	}
630 
631 	profile_max = PROFILE_MAX_DEFAULT;
632 
633 	return DDI_SUCCESS;
634 }
635 
636 /*
637  * APPLE NOTE:  profile_detach not implemented
638  */
639 #if !defined(__APPLE__)
640 static int
profile_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)641 profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
642 {
643 	switch (cmd) {
644 	case DDI_DETACH:
645 		break;
646 	case DDI_SUSPEND:
647 		return DDI_SUCCESS;
648 	default:
649 		return DDI_FAILURE;
650 	}
651 
652 	if (dtrace_unregister(profile_id) != 0) {
653 		return DDI_FAILURE;
654 	}
655 
656 	ddi_remove_minor_node(devi, NULL);
657 	return DDI_SUCCESS;
658 }
659 #endif /* __APPLE__ */
660 
661 d_open_t _profile_open;
662 
663 int
_profile_open(dev_t dev,int flags,int devtype,struct proc * p)664 _profile_open(dev_t dev, int flags, int devtype, struct proc *p)
665 {
666 #pragma unused(dev,flags,devtype,p)
667 	return 0;
668 }
669 
670 #define PROFILE_MAJOR  -24 /* let the kernel pick the device number */
671 
672 static const struct cdevsw profile_cdevsw =
673 {
674 	.d_open = _profile_open,
675 	.d_close = eno_opcl,
676 	.d_read = eno_rdwrt,
677 	.d_write = eno_rdwrt,
678 	.d_ioctl = eno_ioctl,
679 	.d_stop = eno_stop,
680 	.d_reset = eno_reset,
681 	.d_select = eno_select,
682 	.d_mmap = eno_mmap,
683 	.d_strategy = eno_strat,
684 	.d_reserved_1 = eno_getc,
685 	.d_reserved_2 = eno_putc,
686 };
687 
688 void
profile_init(void)689 profile_init( void )
690 {
691 	int majdevno = cdevsw_add(PROFILE_MAJOR, &profile_cdevsw);
692 
693 	if (majdevno < 0) {
694 		printf("profile_init: failed to allocate a major number!\n");
695 		return;
696 	}
697 
698 	profile_attach((dev_info_t*)(uintptr_t)majdevno);
699 }
700 #undef PROFILE_MAJOR
701