]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/dtrace/profile_prvd.c
xnu-1228.tar.gz
[apple/xnu.git] / bsd / dev / dtrace / profile_prvd.c
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 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /* #pragma ident "@(#)profile.c 1.6 06/03/24 SMI" */
27
28 #if !defined(__APPLE__)
29 #include <sys/errno.h>
30 #include <sys/stat.h>
31 #include <sys/modctl.h>
32 #include <sys/conf.h>
33 #include <sys/systm.h>
34 #include <sys/ddi.h>
35 #include <sys/sunddi.h>
36 #include <sys/cpuvar.h>
37 #include <sys/kmem.h>
38 #include <sys/strsubr.h>
39 #include <sys/dtrace.h>
40 #include <sys/cyclic.h>
41 #include <sys/atomic.h>
42 #else
43 #ifdef KERNEL
44 #ifndef _KERNEL
45 #define _KERNEL /* Solaris vs. Darwin */
46 #endif
47 #endif
48
49 #define MACH__POSIX_C_SOURCE_PRIVATE 1 /* pulls in suitable savearea from mach/ppc/thread_status.h */
50 #include <kern/cpu_data.h>
51 #include <kern/thread.h>
52 #include <mach/thread_status.h>
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/errno.h>
57 #include <sys/stat.h>
58 #include <sys/ioctl.h>
59 #include <sys/conf.h>
60 #include <sys/fcntl.h>
61 #include <miscfs/devfs/devfs.h>
62
63 #include <sys/dtrace.h>
64 #include <sys/dtrace_impl.h>
65
66 #include <sys/dtrace_glue.h>
67
68 #if defined(__ppc__) || defined(__ppc64__)
69 extern struct savearea *find_kern_regs(thread_t);
70 #elif defined(__i386__) || defined(__x86_64__)
71 extern x86_saved_state32_t *find_kern_regs(thread_t);
72 #elif defined (__arm__)
73 extern struct arm_saved_state *find_kern_regs(thread_t);
74 #else
75 #error Unknown architecture
76 #endif
77
78 #undef ASSERT
79 #define ASSERT(x) do {} while(0)
80
81 extern void profile_init(void);
82 #endif /* __APPLE__ */
83
84 static dev_info_t *profile_devi;
85 static dtrace_provider_id_t profile_id;
86
87 /*
88 * Regardless of platform, there are five artificial frames in the case of the
89 * profile provider:
90 *
91 * profile_fire
92 * cyclic_expire
93 * cyclic_fire
94 * [ cbe ]
95 * [ locore ]
96 *
97 * On amd64, there are two frames associated with locore: one in locore, and
98 * another in common interrupt dispatch code. (i386 has not been modified to
99 * use this common layer.) Further, on i386, the interrupted instruction
100 * appears as its own stack frame. All of this means that we need to add one
101 * frame for amd64, and then take one away for both amd64 and i386.
102 *
103 * On SPARC, the picture is further complicated because the compiler
104 * optimizes away tail-calls -- so the following frames are optimized away:
105 *
106 * profile_fire
107 * cyclic_expire
108 *
109 * This gives three frames. However, on DEBUG kernels, the cyclic_expire
110 * frame cannot be tail-call eliminated, yielding four frames in this case.
111 *
112 * All of the above constraints lead to the mess below. Yes, the profile
113 * provider should ideally figure this out on-the-fly by hiting one of its own
114 * probes and then walking its own stack trace. This is complicated, however,
115 * and the static definition doesn't seem to be overly brittle. Still, we
116 * allow for a manual override in case we get it completely wrong.
117 */
118 #if !defined(__APPLE__)
119
120 #ifdef __x86_64__
121 #define PROF_ARTIFICIAL_FRAMES 7
122 #else
123 #ifdef __i386__
124 #define PROF_ARTIFICIAL_FRAMES 6
125 #else
126 #ifdef __sparc
127 #ifdef DEBUG
128 #define PROF_ARTIFICIAL_FRAMES 4
129 #else
130 #define PROF_ARTIFICIAL_FRAMES 3
131 #endif
132 #endif
133 #endif
134 #endif
135
136 #else /* is Mac OS X */
137
138 #if defined(__ppc__) || defined(__ppc64__)
139 #define PROF_ARTIFICIAL_FRAMES 8
140 #elif defined(__i386__) || defined(__x86_64__)
141 #define PROF_ARTIFICIAL_FRAMES 9
142 #elif defined(__arm__)
143 #define PROF_ARTIFICIAL_FRAMES 3 /* XXX BOGUS ARMTODO */
144 #else
145 #error Unknown architecture
146 #endif
147
148 #endif /* __APPLE__ */
149
150 #define PROF_NAMELEN 15
151
152 #define PROF_PROFILE 0
153 #define PROF_TICK 1
154 #define PROF_PREFIX_PROFILE "profile-"
155 #define PROF_PREFIX_TICK "tick-"
156
157 typedef struct profile_probe {
158 char prof_name[PROF_NAMELEN];
159 dtrace_id_t prof_id;
160 int prof_kind;
161 hrtime_t prof_interval;
162 cyclic_id_t prof_cyclic;
163 } profile_probe_t;
164
165 typedef struct profile_probe_percpu {
166 hrtime_t profc_expected;
167 hrtime_t profc_interval;
168 profile_probe_t *profc_probe;
169 } profile_probe_percpu_t;
170
171 hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */
172 int profile_aframes = 0; /* override */
173
174 static int profile_rates[] = {
175 97, 199, 499, 997, 1999,
176 4001, 4999, 0, 0, 0,
177 0, 0, 0, 0, 0,
178 0, 0, 0, 0, 0
179 };
180
181 static int profile_ticks[] = {
182 1, 10, 100, 500, 1000,
183 5000, 0, 0, 0, 0,
184 0, 0, 0, 0, 0
185 };
186
187 /*
188 * profile_max defines the upper bound on the number of profile probes that
189 * can exist (this is to prevent malicious or clumsy users from exhausing
190 * system resources by creating a slew of profile probes). At mod load time,
191 * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
192 * present in the profile.conf file.
193 */
194 #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */
195 static uint32_t profile_max; /* maximum number of profile probes */
196 static uint32_t profile_total; /* current number of profile probes */
197
198 static void
199 profile_fire(void *arg)
200 {
201 profile_probe_percpu_t *pcpu = arg;
202 profile_probe_t *prof = pcpu->profc_probe;
203 hrtime_t late;
204
205 late = dtrace_gethrtime() - pcpu->profc_expected;
206 pcpu->profc_expected += pcpu->profc_interval;
207
208 #if !defined(__APPLE__)
209 dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
210 CPU->cpu_profile_upc, late, 0, 0);
211 #else
212 #if defined(__ppc__) || defined(__ppc64__)
213 struct savearea *sv = find_kern_regs(current_thread());
214
215 if (sv) {
216 if (USERMODE(sv->save_srr1)) {
217 dtrace_probe(prof->prof_id, 0x0, sv->save_srr0, late, 0, 0);
218 } else {
219 dtrace_probe(prof->prof_id, sv->save_srr0, 0x0, late, 0, 0);
220 }
221 } else {
222 dtrace_probe(prof->prof_id, 0xcafebabe,
223 0x0, late, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */
224 }
225 #elif defined(__i386__) || defined(__x86_64__)
226 x86_saved_state32_t *kern_regs = find_kern_regs(current_thread());
227
228 if (NULL != kern_regs) {
229 /* Kernel was interrupted. */
230 dtrace_probe(prof->prof_id, kern_regs->eip, 0x0, 0, 0, 0);
231 } else {
232 /* Possibly a user interrupt */
233 x86_saved_state_t *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread());
234
235 if (NULL == tagged_regs) {
236 /* Too bad, so sad, no useful interrupt state. */
237 dtrace_probe(prof->prof_id, 0xcafebabe,
238 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */
239 } else if (is_saved_state64(tagged_regs)) {
240 x86_saved_state64_t *regs = saved_state64(tagged_regs);
241
242 dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, 0, 0, 0);
243 } else {
244 x86_saved_state32_t *regs = saved_state32(tagged_regs);
245
246 dtrace_probe(prof->prof_id, 0x0, regs->eip, 0, 0, 0);
247 }
248 }
249 #else
250 #error Unknown architecture
251 #endif
252 #endif /* __APPLE__ */
253 }
254
255 static void
256 profile_tick(void *arg)
257 {
258 profile_probe_t *prof = arg;
259
260 #if !defined(__APPLE__)
261 dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
262 CPU->cpu_profile_upc, 0, 0, 0);
263 #else
264 #if defined(__ppc__) || defined(__ppc64__)
265 struct savearea *sv = find_kern_regs(current_thread());
266
267 if (sv) {
268 if (USERMODE(sv->save_srr1)) {
269 dtrace_probe(prof->prof_id, 0x0, sv->save_srr0, 0, 0, 0);
270 } else {
271 dtrace_probe(prof->prof_id, sv->save_srr0, 0x0, 0, 0, 0);
272 }
273 } else {
274 dtrace_probe(prof->prof_id, 0xcafebabe,
275 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */
276 }
277 #elif defined(__i386__) || defined(__x86_64__)
278 x86_saved_state32_t *kern_regs = find_kern_regs(current_thread());
279
280 if (NULL != kern_regs) {
281 /* Kernel was interrupted. */
282 dtrace_probe(prof->prof_id, kern_regs->eip, 0x0, 0, 0, 0);
283 } else {
284 /* Possibly a user interrupt */
285 x86_saved_state_t *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread());
286
287 if (NULL == tagged_regs) {
288 /* Too bad, so sad, no useful interrupt state. */
289 dtrace_probe(prof->prof_id, 0xcafebabe,
290 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */
291 } else if (is_saved_state64(tagged_regs)) {
292 x86_saved_state64_t *regs = saved_state64(tagged_regs);
293
294 dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, 0, 0, 0);
295 } else {
296 x86_saved_state32_t *regs = saved_state32(tagged_regs);
297
298 dtrace_probe(prof->prof_id, 0x0, regs->eip, 0, 0, 0);
299 }
300 }
301 #else
302 #error Unknown architecture
303 #endif
304 #endif /* __APPLE__ */
305 }
306
307 static void
308 profile_create(hrtime_t interval, const char *name, int kind)
309 {
310 profile_probe_t *prof;
311
312 if (interval < profile_interval_min)
313 return;
314
315 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
316 return;
317
318 atomic_add_32(&profile_total, 1);
319 if (profile_total > profile_max) {
320 atomic_add_32(&profile_total, -1);
321 return;
322 }
323
324 #if !defined(__APPLE__)
325 prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
326 #else
327 if (PROF_TICK == kind)
328 prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
329 else
330 prof = kmem_zalloc(sizeof (profile_probe_t) + NCPU*sizeof(profile_probe_percpu_t), KM_SLEEP);
331 #endif /* __APPLE__ */
332 (void) strlcpy(prof->prof_name, name, sizeof(prof->prof_name));
333 prof->prof_interval = interval;
334 prof->prof_cyclic = CYCLIC_NONE;
335 prof->prof_kind = kind;
336 prof->prof_id = dtrace_probe_create(profile_id,
337 NULL, NULL, name,
338 profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof);
339 }
340
341 /*ARGSUSED*/
342 static void
343 profile_provide(void *arg, const dtrace_probedesc_t *desc)
344 {
345 int i, j, rate, kind;
346 hrtime_t val = 0, mult = 1, len;
347 const char *name, *suffix = NULL;
348
349 const struct {
350 char *prefix;
351 int kind;
352 } types[] = {
353 { PROF_PREFIX_PROFILE, PROF_PROFILE },
354 { PROF_PREFIX_TICK, PROF_TICK },
355 { NULL, NULL }
356 };
357
358 const struct {
359 char *name;
360 hrtime_t mult;
361 } suffixes[] = {
362 { "ns", NANOSEC / NANOSEC },
363 { "nsec", NANOSEC / NANOSEC },
364 { "us", NANOSEC / MICROSEC },
365 { "usec", NANOSEC / MICROSEC },
366 { "ms", NANOSEC / MILLISEC },
367 { "msec", NANOSEC / MILLISEC },
368 { "s", NANOSEC / SEC },
369 { "sec", NANOSEC / SEC },
370 { "m", NANOSEC * (hrtime_t)60 },
371 { "min", NANOSEC * (hrtime_t)60 },
372 { "h", NANOSEC * (hrtime_t)(60 * 60) },
373 { "hour", NANOSEC * (hrtime_t)(60 * 60) },
374 { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
375 { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
376 { "hz", 0 },
377 #if !defined(__APPLE__)
378 { NULL }
379 #else
380 { NULL, 0 }
381 #endif /* __APPLE__ */
382 };
383
384 if (desc == NULL) {
385 char n[PROF_NAMELEN];
386
387 /*
388 * If no description was provided, provide all of our probes.
389 */
390 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
391 if ((rate = profile_rates[i]) == 0)
392 continue;
393
394 (void) snprintf(n, PROF_NAMELEN, "%s%d",
395 PROF_PREFIX_PROFILE, rate);
396 profile_create(NANOSEC / rate, n, PROF_PROFILE);
397 }
398
399 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
400 if ((rate = profile_ticks[i]) == 0)
401 continue;
402
403 (void) snprintf(n, PROF_NAMELEN, "%s%d",
404 PROF_PREFIX_TICK, rate);
405 profile_create(NANOSEC / rate, n, PROF_TICK);
406 }
407
408 return;
409 }
410
411 name = desc->dtpd_name;
412
413 for (i = 0; types[i].prefix != NULL; i++) {
414 len = strlen(types[i].prefix);
415
416 if (strncmp(name, types[i].prefix, len) != 0)
417 continue;
418 break;
419 }
420
421 if (types[i].prefix == NULL)
422 return;
423
424 kind = types[i].kind;
425 j = strlen(name) - len;
426
427 /*
428 * We need to start before any time suffix.
429 */
430 for (j = strlen(name); j >= len; j--) {
431 if (name[j] >= '0' && name[j] <= '9')
432 break;
433 suffix = &name[j];
434 }
435
436 ASSERT(suffix != NULL);
437
438 /*
439 * Now determine the numerical value present in the probe name.
440 */
441 for (; j >= len; j--) {
442 if (name[j] < '0' || name[j] > '9')
443 return;
444
445 val += (name[j] - '0') * mult;
446 mult *= (hrtime_t)10;
447 }
448
449 if (val == 0)
450 return;
451
452 /*
453 * Look-up the suffix to determine the multiplier.
454 */
455 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
456 if (strcasecmp(suffixes[i].name, suffix) == 0) {
457 mult = suffixes[i].mult;
458 break;
459 }
460 }
461
462 if (suffixes[i].name == NULL && *suffix != '\0')
463 return;
464
465 if (mult == 0) {
466 /*
467 * The default is frequency-per-second.
468 */
469 val = NANOSEC / val;
470 } else {
471 val *= mult;
472 }
473
474 profile_create(val, name, kind);
475 }
476
477 /*ARGSUSED*/
478 static void
479 profile_destroy(void *arg, dtrace_id_t id, void *parg)
480 {
481 profile_probe_t *prof = parg;
482
483 ASSERT(prof->prof_cyclic == CYCLIC_NONE);
484 #if !defined(__APPLE__)
485 kmem_free(prof, sizeof (profile_probe_t));
486 #else
487 if (prof->prof_kind == PROF_TICK)
488 kmem_free(prof, sizeof (profile_probe_t));
489 else
490 kmem_free(prof, sizeof (profile_probe_t) + NCPU*sizeof(profile_probe_percpu_t));
491 #endif /* __APPLE__ */
492
493 ASSERT(profile_total >= 1);
494 atomic_add_32(&profile_total, -1);
495 }
496
497 /*ARGSUSED*/
498 static void
499 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
500 {
501 profile_probe_t *prof = arg;
502 profile_probe_percpu_t *pcpu;
503
504 #if !defined(__APPLE__)
505 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
506 #else
507 pcpu = ((profile_probe_percpu_t *)(&(prof[1]))) + cpu_number();
508 #endif /* __APPLE__ */
509 pcpu->profc_probe = prof;
510
511 hdlr->cyh_func = profile_fire;
512 hdlr->cyh_arg = pcpu;
513 hdlr->cyh_level = CY_HIGH_LEVEL;
514
515 when->cyt_interval = prof->prof_interval;
516 #if !defined(__APPLE__)
517 when->cyt_when = dtrace_gethrtime() + when->cyt_interval;
518 #else
519 when->cyt_when = 0;
520 #endif /* __APPLE__ */
521
522 pcpu->profc_expected = when->cyt_when;
523 pcpu->profc_interval = when->cyt_interval;
524 }
525
526 /*ARGSUSED*/
527 static void
528 profile_offline(void *arg, cpu_t *cpu, void *oarg)
529 {
530 profile_probe_percpu_t *pcpu = oarg;
531
532 ASSERT(pcpu->profc_probe == arg);
533 #if !defined(__APPLE__)
534 kmem_free(pcpu, sizeof (profile_probe_percpu_t));
535 #endif /* __APPLE__ */
536 }
537
538 /*ARGSUSED*/
539 static void
540 profile_enable(void *arg, dtrace_id_t id, void *parg)
541 {
542 profile_probe_t *prof = parg;
543 cyc_omni_handler_t omni;
544 cyc_handler_t hdlr;
545 cyc_time_t when;
546
547 ASSERT(prof->prof_interval != 0);
548 ASSERT(MUTEX_HELD(&cpu_lock));
549
550 if (prof->prof_kind == PROF_TICK) {
551 hdlr.cyh_func = profile_tick;
552 hdlr.cyh_arg = prof;
553 hdlr.cyh_level = CY_HIGH_LEVEL;
554
555 when.cyt_interval = prof->prof_interval;
556 #if !defined(__APPLE__)
557 when.cyt_when = dtrace_gethrtime() + when.cyt_interval;
558 #else
559 when.cyt_when = 0;
560 #endif /* __APPLE__ */
561 } else {
562 ASSERT(prof->prof_kind == PROF_PROFILE);
563 omni.cyo_online = profile_online;
564 omni.cyo_offline = profile_offline;
565 omni.cyo_arg = prof;
566 }
567
568 #if !defined(__APPLE__)
569 if (prof->prof_kind == PROF_TICK) {
570 prof->prof_cyclic = cyclic_add(&hdlr, &when);
571 } else {
572 prof->prof_cyclic = cyclic_add_omni(&omni);
573 }
574 #else
575 if (prof->prof_kind == PROF_TICK) {
576 prof->prof_cyclic = cyclic_timer_add(&hdlr, &when);
577 } else {
578 prof->prof_cyclic = (cyclic_id_t)cyclic_add_omni(&omni); /* cast puns cyclic_id_list_t with cyclic_id_t */
579 }
580 #endif /* __APPLE__ */
581 }
582
583 /*ARGSUSED*/
584 static void
585 profile_disable(void *arg, dtrace_id_t id, void *parg)
586 {
587 profile_probe_t *prof = parg;
588
589 ASSERT(prof->prof_cyclic != CYCLIC_NONE);
590 ASSERT(MUTEX_HELD(&cpu_lock));
591
592 #if !defined(__APPLE__)
593 cyclic_remove(prof->prof_cyclic);
594 #else
595 if (prof->prof_kind == PROF_TICK) {
596 cyclic_timer_remove(prof->prof_cyclic);
597 } else {
598 cyclic_remove_omni((cyclic_id_list_t)prof->prof_cyclic); /* cast puns cyclic_id_list_t with cyclic_id_t */
599 }
600 #endif /* __APPLE__ */
601 prof->prof_cyclic = CYCLIC_NONE;
602 }
603
604 #if !defined(__APPLE__)
605 /*ARGSUSED*/
606 static int
607 profile_usermode(void *arg, dtrace_id_t id, void *parg)
608 {
609 return (CPU->cpu_profile_pc == 0);
610 }
611 #else
612 static int
613 profile_usermode(void *arg, dtrace_id_t id, void *parg)
614 {
615 #pragma unused(arg,id,parg)
616 return 1; /* XXX_BOGUS */
617 }
618 #endif /* __APPLE__ */
619
620 static dtrace_pattr_t profile_attr = {
621 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
622 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN },
623 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
624 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
625 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
626 };
627
628 static dtrace_pops_t profile_pops = {
629 profile_provide,
630 NULL,
631 profile_enable,
632 profile_disable,
633 NULL,
634 NULL,
635 NULL,
636 NULL,
637 profile_usermode,
638 profile_destroy
639 };
640
641 static int
642 profile_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
643 {
644 switch (cmd) {
645 case DDI_ATTACH:
646 break;
647 case DDI_RESUME:
648 return (DDI_SUCCESS);
649 default:
650 return (DDI_FAILURE);
651 }
652
653 if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0,
654 DDI_PSEUDO, NULL) == DDI_FAILURE ||
655 dtrace_register("profile", &profile_attr,
656 DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL,
657 &profile_pops, NULL, &profile_id) != 0) {
658 ddi_remove_minor_node(devi, NULL);
659 return (DDI_FAILURE);
660 }
661
662 #if !defined(__APPLE__)
663 profile_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
664 "profile-max-probes", PROFILE_MAX_DEFAULT);
665 #else
666 profile_max = PROFILE_MAX_DEFAULT;
667 #endif /* __APPLE__ */
668
669 ddi_report_dev(devi);
670 profile_devi = devi;
671 return (DDI_SUCCESS);
672 }
673
674 #if !defined(__APPLE__)
675 static int
676 profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
677 {
678 switch (cmd) {
679 case DDI_DETACH:
680 break;
681 case DDI_SUSPEND:
682 return (DDI_SUCCESS);
683 default:
684 return (DDI_FAILURE);
685 }
686
687 if (dtrace_unregister(profile_id) != 0)
688 return (DDI_FAILURE);
689
690 ddi_remove_minor_node(devi, NULL);
691 return (DDI_SUCCESS);
692 }
693
694 /*ARGSUSED*/
695 static int
696 profile_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
697 {
698 int error;
699
700 switch (infocmd) {
701 case DDI_INFO_DEVT2DEVINFO:
702 *result = (void *)profile_devi;
703 error = DDI_SUCCESS;
704 break;
705 case DDI_INFO_DEVT2INSTANCE:
706 *result = (void *)0;
707 error = DDI_SUCCESS;
708 break;
709 default:
710 error = DDI_FAILURE;
711 }
712 return (error);
713 }
714
715 /*ARGSUSED*/
716 static int
717 profile_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
718 {
719 return (0);
720 }
721
722 static struct cb_ops profile_cb_ops = {
723 profile_open, /* open */
724 nodev, /* close */
725 nulldev, /* strategy */
726 nulldev, /* print */
727 nodev, /* dump */
728 nodev, /* read */
729 nodev, /* write */
730 nodev, /* ioctl */
731 nodev, /* devmap */
732 nodev, /* mmap */
733 nodev, /* segmap */
734 nochpoll, /* poll */
735 ddi_prop_op, /* cb_prop_op */
736 0, /* streamtab */
737 D_NEW | D_MP /* Driver compatibility flag */
738 };
739
740 static struct dev_ops profile_ops = {
741 DEVO_REV, /* devo_rev, */
742 0, /* refcnt */
743 profile_info, /* get_dev_info */
744 nulldev, /* identify */
745 nulldev, /* probe */
746 profile_attach, /* attach */
747 profile_detach, /* detach */
748 nodev, /* reset */
749 &profile_cb_ops, /* driver operations */
750 NULL, /* bus operations */
751 nodev /* dev power */
752 };
753
754 /*
755 * Module linkage information for the kernel.
756 */
757 static struct modldrv modldrv = {
758 &mod_driverops, /* module type (this is a pseudo driver) */
759 "Profile Interrupt Tracing", /* name of module */
760 &profile_ops, /* driver ops */
761 };
762
763 static struct modlinkage modlinkage = {
764 MODREV_1,
765 (void *)&modldrv,
766 NULL
767 };
768
769 int
770 _init(void)
771 {
772 return (mod_install(&modlinkage));
773 }
774
775 int
776 _info(struct modinfo *modinfop)
777 {
778 return (mod_info(&modlinkage, modinfop));
779 }
780
781 int
782 _fini(void)
783 {
784 return (mod_remove(&modlinkage));
785 }
786 #else
787 d_open_t _profile_open;
788
789 int _profile_open(dev_t dev, int flags, int devtype, struct proc *p)
790 {
791 #pragma unused(dev,flags,devtype,p)
792 return 0;
793 }
794
795 #define PROFILE_MAJOR -24 /* let the kernel pick the device number */
796
797 /*
798 * A struct describing which functions will get invoked for certain
799 * actions.
800 */
801 static struct cdevsw profile_cdevsw =
802 {
803 _profile_open, /* open */
804 eno_opcl, /* close */
805 eno_rdwrt, /* read */
806 eno_rdwrt, /* write */
807 eno_ioctl, /* ioctl */
808 (stop_fcn_t *)nulldev, /* stop */
809 (reset_fcn_t *)nulldev, /* reset */
810 NULL, /* tty's */
811 eno_select, /* select */
812 eno_mmap, /* mmap */
813 eno_strat, /* strategy */
814 eno_getc, /* getc */
815 eno_putc, /* putc */
816 0 /* type */
817 };
818
819 static int gProfileInited = 0;
820
821 void profile_init( void )
822 {
823 if (0 == gProfileInited)
824 {
825 int majdevno = cdevsw_add(PROFILE_MAJOR, &profile_cdevsw);
826
827 if (majdevno < 0) {
828 printf("profile_init: failed to allocate a major number!\n");
829 gProfileInited = 0;
830 return;
831 }
832
833 profile_attach( (dev_info_t *)majdevno, DDI_ATTACH );
834
835 gProfileInited = 1;
836 } else
837 panic("profile_init: called twice!\n");
838 }
839 #undef PROFILE_MAJOR
840 #endif /* __APPLE__ */