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