]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/dtrace/sdt.c
xnu-6153.101.6.tar.gz
[apple/xnu.git] / bsd / dev / dtrace / sdt.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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/errno.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <sys/conf.h>
32 #include <sys/fcntl.h>
33 #include <miscfs/devfs/devfs.h>
34
35 #if CONFIG_EMBEDDED
36 #include <arm/caches_internal.h>
37 #endif
38
39 #include <sys/dtrace.h>
40 #include <sys/dtrace_impl.h>
41
42 #include <sys/dtrace_glue.h>
43
44 #include <sys/sdt_impl.h>
45 extern int dtrace_kernel_symbol_mode;
46
47 /* #include <machine/trap.h */
48 struct savearea_t; /* Used anonymously */
49
50 #if defined(__arm__)
51 typedef kern_return_t (*perfCallback)(int, struct savearea_t *, __unused int, __unused int);
52 extern perfCallback tempDTraceTrapHook;
53 extern kern_return_t fbt_perfCallback(int, struct savearea_t *, __unused int, __unused int);
54 #define SDT_PATCHVAL 0xdefc
55 #define SDT_AFRAMES 7
56 #elif defined(__arm64__)
57 typedef kern_return_t (*perfCallback)(int, struct savearea_t *, __unused int, __unused int);
58 extern perfCallback tempDTraceTrapHook;
59 extern kern_return_t fbt_perfCallback(int, struct savearea_t *, __unused int, __unused int);
60 #define SDT_PATCHVAL 0xe7eeee7e
61 #define SDT_AFRAMES 7
62 #elif defined(__x86_64__)
63 typedef kern_return_t (*perfCallback)(int, struct savearea_t *, uintptr_t *, int);
64 extern perfCallback tempDTraceTrapHook;
65 extern kern_return_t fbt_perfCallback(int, struct savearea_t *, uintptr_t *, int);
66 #define SDT_PATCHVAL 0xf0
67 #define SDT_AFRAMES 6
68 #else
69 #error Unknown architecture
70 #endif
71
72 #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */
73
74 #define DTRACE_PROBE_PREFIX "_dtrace_probe$"
75
76 static int sdt_verbose = 0;
77 sdt_probe_t **sdt_probetab;
78 int sdt_probetab_size;
79 int sdt_probetab_mask;
80
81 /*ARGSUSED*/
82 static void
83 __sdt_provide_module(void *arg, struct modctl *ctl)
84 {
85 #pragma unused(arg)
86 struct module *mp = (struct module *)ctl->mod_address;
87 char *modname = ctl->mod_modname;
88 sdt_probedesc_t *sdpd;
89 sdt_probe_t *sdp, *old;
90 sdt_provider_t *prov;
91 int len;
92
93 /*
94 * One for all, and all for one: if we haven't yet registered all of
95 * our providers, we'll refuse to provide anything.
96 */
97 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
98 if (prov->sdtp_id == DTRACE_PROVNONE) {
99 return;
100 }
101 }
102
103 if (!mp || mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL) {
104 return;
105 }
106
107 for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
108 const char *name = sdpd->sdpd_name, *func;
109 char *nname;
110 int i, j;
111 dtrace_id_t id;
112
113 for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
114 const char *prefpart, *prefix = prov->sdtp_prefix;
115
116 if ((prefpart = strstr(name, prefix))) {
117 name = prefpart + strlen(prefix);
118 break;
119 }
120 }
121
122 nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
123
124 for (i = 0, j = 0; name[j] != '\0'; i++) {
125 if (name[j] == '_' && name[j + 1] == '_') {
126 nname[i] = '-';
127 j += 2;
128 } else {
129 nname[i] = name[j++];
130 }
131 }
132
133 nname[i] = '\0';
134
135 sdp = kmem_zalloc(sizeof(sdt_probe_t), KM_SLEEP);
136 sdp->sdp_loadcnt = ctl->mod_loadcnt;
137 sdp->sdp_ctl = ctl;
138 sdp->sdp_name = nname;
139 sdp->sdp_namelen = len;
140 sdp->sdp_provider = prov;
141
142 func = sdpd->sdpd_func;
143
144 if (func == NULL) {
145 func = "<unknown>";
146 }
147
148 /*
149 * We have our provider. Now create the probe.
150 */
151 if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
152 func, nname)) != DTRACE_IDNONE) {
153 old = dtrace_probe_arg(prov->sdtp_id, id);
154 ASSERT(old != NULL);
155
156 sdp->sdp_next = old->sdp_next;
157 sdp->sdp_id = id;
158 old->sdp_next = sdp;
159 } else {
160 sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
161 modname, func, nname, SDT_AFRAMES, sdp);
162
163 mp->sdt_nprobes++;
164 }
165
166 #if 0
167 printf("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd, sdp, nname, sdp->sdp_id);
168 #endif
169
170 sdp->sdp_hashnext =
171 sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)];
172 sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp;
173
174 sdp->sdp_patchval = SDT_PATCHVAL;
175 sdp->sdp_patchpoint = (sdt_instr_t *)sdpd->sdpd_offset;
176 sdp->sdp_savedval = *sdp->sdp_patchpoint;
177 }
178 }
179
180 /*ARGSUSED*/
181 static void
182 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
183 {
184 #pragma unused(arg,id)
185 sdt_probe_t *sdp = parg, *old, *last, *hash;
186 int ndx;
187
188 #if !defined(__APPLE__)
189 /*
190 * APPLE NOTE: sdt probes for kexts not yet implemented
191 */
192 struct modctl *ctl = sdp->sdp_ctl;
193
194 if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
195 if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
196 ctl->mod_loaded)) {
197 ((struct module *)(ctl->mod_mp))->sdt_nprobes--;
198 }
199 }
200 #endif /* __APPLE__ */
201
202 while (sdp != NULL) {
203 old = sdp;
204
205 /*
206 * Now we need to remove this probe from the sdt_probetab.
207 */
208 ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint);
209 last = NULL;
210 hash = sdt_probetab[ndx];
211
212 while (hash != sdp) {
213 ASSERT(hash != NULL);
214 last = hash;
215 hash = hash->sdp_hashnext;
216 }
217
218 if (last != NULL) {
219 last->sdp_hashnext = sdp->sdp_hashnext;
220 } else {
221 sdt_probetab[ndx] = sdp->sdp_hashnext;
222 }
223
224 kmem_free(sdp->sdp_name, sdp->sdp_namelen);
225 sdp = sdp->sdp_next;
226 kmem_free(old, sizeof(sdt_probe_t));
227 }
228 }
229
230 /*ARGSUSED*/
231 static int
232 sdt_enable(void *arg, dtrace_id_t id, void *parg)
233 {
234 #pragma unused(arg,id)
235 sdt_probe_t *sdp = parg;
236 struct modctl *ctl = sdp->sdp_ctl;
237
238 ctl->mod_nenabled++;
239
240 /*
241 * If this module has disappeared since we discovered its probes,
242 * refuse to enable it.
243 */
244 if (!ctl->mod_loaded) {
245 if (sdt_verbose) {
246 cmn_err(CE_NOTE, "sdt is failing for probe %s "
247 "(module %s unloaded)",
248 sdp->sdp_name, ctl->mod_modname);
249 }
250 goto err;
251 }
252
253 /*
254 * Now check that our modctl has the expected load count. If it
255 * doesn't, this module must have been unloaded and reloaded -- and
256 * we're not going to touch it.
257 */
258 if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
259 if (sdt_verbose) {
260 cmn_err(CE_NOTE, "sdt is failing for probe %s "
261 "(module %s reloaded)",
262 sdp->sdp_name, ctl->mod_modname);
263 }
264 goto err;
265 }
266
267 dtrace_casptr(&tempDTraceTrapHook, NULL, fbt_perfCallback);
268 if (tempDTraceTrapHook != (perfCallback)fbt_perfCallback) {
269 if (sdt_verbose) {
270 cmn_err(CE_NOTE, "sdt_enable is failing for probe %s "
271 "in module %s: tempDTraceTrapHook already occupied.",
272 sdp->sdp_name, ctl->mod_modname);
273 }
274 return 0;
275 }
276
277 while (sdp != NULL) {
278 (void)ml_nofault_copy((vm_offset_t)&sdp->sdp_patchval, (vm_offset_t)sdp->sdp_patchpoint,
279 (vm_size_t)sizeof(sdp->sdp_patchval));
280
281 /*
282 * Make the patched instruction visible via a data + instruction
283 * cache fush on platforms that need it
284 */
285 flush_dcache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_patchval), 0);
286 invalidate_icache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_patchval), 0);
287
288 sdp = sdp->sdp_next;
289 }
290
291 err:
292 return 0;
293 }
294
295 /*ARGSUSED*/
296 static void
297 sdt_disable(void *arg, dtrace_id_t id, void *parg)
298 {
299 #pragma unused(arg,id)
300 sdt_probe_t *sdp = parg;
301 struct modctl *ctl = sdp->sdp_ctl;
302
303 ctl->mod_nenabled--;
304
305 if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt) {
306 goto err;
307 }
308
309 while (sdp != NULL) {
310 (void)ml_nofault_copy((vm_offset_t)&sdp->sdp_savedval, (vm_offset_t)sdp->sdp_patchpoint,
311 (vm_size_t)sizeof(sdp->sdp_savedval));
312 /*
313 * Make the patched instruction visible via a data + instruction
314 * cache flush on platforms that need it
315 */
316 flush_dcache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_savedval), 0);
317 invalidate_icache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_savedval), 0);
318 sdp = sdp->sdp_next;
319 }
320
321 err:
322 ;
323 }
324
325 static dtrace_pops_t sdt_pops = {
326 .dtps_provide = NULL,
327 .dtps_provide_module = sdt_provide_module,
328 .dtps_enable = sdt_enable,
329 .dtps_disable = sdt_disable,
330 .dtps_suspend = NULL,
331 .dtps_resume = NULL,
332 .dtps_getargdesc = sdt_getargdesc,
333 .dtps_getargval = sdt_getarg,
334 .dtps_usermode = NULL,
335 .dtps_destroy = sdt_destroy,
336 };
337
338 /*ARGSUSED*/
339 static int
340 sdt_attach(dev_info_t *devi)
341 {
342 sdt_provider_t *prov;
343
344 if (ddi_create_minor_node(devi, "sdt", S_IFCHR,
345 0, DDI_PSEUDO, 0) == DDI_FAILURE) {
346 cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node");
347 ddi_remove_minor_node(devi, NULL);
348 return DDI_FAILURE;
349 }
350
351 if (sdt_probetab_size == 0) {
352 sdt_probetab_size = SDT_PROBETAB_SIZE;
353 }
354
355 sdt_probetab_mask = sdt_probetab_size - 1;
356 sdt_probetab =
357 kmem_zalloc(sdt_probetab_size * sizeof(sdt_probe_t *), KM_SLEEP);
358 dtrace_invop_add(sdt_invop);
359
360 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
361 if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
362 DTRACE_PRIV_KERNEL, NULL,
363 &sdt_pops, prov, &prov->sdtp_id) != 0) {
364 cmn_err(CE_WARN, "failed to register sdt provider %s",
365 prov->sdtp_name);
366 }
367 }
368
369 return DDI_SUCCESS;
370 }
371
372 /*
373 * APPLE NOTE: sdt_detach not implemented
374 */
375 #if !defined(__APPLE__)
376 /*ARGSUSED*/
377 static int
378 sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
379 {
380 sdt_provider_t *prov;
381
382 switch (cmd) {
383 case DDI_DETACH:
384 break;
385
386 case DDI_SUSPEND:
387 return DDI_SUCCESS;
388
389 default:
390 return DDI_FAILURE;
391 }
392
393 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
394 if (prov->sdtp_id != DTRACE_PROVNONE) {
395 if (dtrace_unregister(prov->sdtp_id) != 0) {
396 return DDI_FAILURE;
397 }
398
399 prov->sdtp_id = DTRACE_PROVNONE;
400 }
401 }
402
403 dtrace_invop_remove(sdt_invop);
404 kmem_free(sdt_probetab, sdt_probetab_size * sizeof(sdt_probe_t *));
405
406 return DDI_SUCCESS;
407 }
408 #endif /* __APPLE__ */
409
410 d_open_t _sdt_open;
411
412 int
413 _sdt_open(dev_t dev, int flags, int devtype, struct proc *p)
414 {
415 #pragma unused(dev,flags,devtype,p)
416 return 0;
417 }
418
419 #define SDT_MAJOR -24 /* let the kernel pick the device number */
420
421 /*
422 * A struct describing which functions will get invoked for certain
423 * actions.
424 */
425 static struct cdevsw sdt_cdevsw =
426 {
427 _sdt_open, /* open */
428 eno_opcl, /* close */
429 eno_rdwrt, /* read */
430 eno_rdwrt, /* write */
431 eno_ioctl, /* ioctl */
432 (stop_fcn_t *)nulldev, /* stop */
433 (reset_fcn_t *)nulldev, /* reset */
434 NULL, /* tty's */
435 eno_select, /* select */
436 eno_mmap, /* mmap */
437 eno_strat, /* strategy */
438 eno_getc, /* getc */
439 eno_putc, /* putc */
440 0 /* type */
441 };
442
443 static struct modctl g_sdt_kernctl;
444 static struct module g_sdt_mach_module;
445
446 #include <mach-o/nlist.h>
447 #include <libkern/kernel_mach_header.h>
448
449 void
450 sdt_early_init( void )
451 {
452 if (dtrace_sdt_probes_restricted()) {
453 return;
454 }
455 if (MH_MAGIC_KERNEL != _mh_execute_header.magic) {
456 g_sdt_kernctl.mod_address = (vm_address_t)NULL;
457 g_sdt_kernctl.mod_size = 0;
458 } else {
459 kernel_mach_header_t *mh;
460 struct load_command *cmd;
461 kernel_segment_command_t *orig_ts = NULL, *orig_le = NULL;
462 struct symtab_command *orig_st = NULL;
463 kernel_nlist_t *sym = NULL;
464 char *strings;
465 unsigned int i;
466
467 g_sdt_mach_module.sdt_nprobes = 0;
468 g_sdt_mach_module.sdt_probes = NULL;
469
470 g_sdt_kernctl.mod_address = (vm_address_t)&g_sdt_mach_module;
471 g_sdt_kernctl.mod_size = 0;
472 strncpy((char *)&(g_sdt_kernctl.mod_modname), "mach_kernel", KMOD_MAX_NAME);
473
474 g_sdt_kernctl.mod_next = NULL;
475 g_sdt_kernctl.mod_stale = NULL;
476 g_sdt_kernctl.mod_id = 0;
477 g_sdt_kernctl.mod_loadcnt = 1;
478 g_sdt_kernctl.mod_loaded = 1;
479 g_sdt_kernctl.mod_flags = 0;
480 g_sdt_kernctl.mod_nenabled = 0;
481
482 mh = &_mh_execute_header;
483 cmd = (struct load_command*) &mh[1];
484 for (i = 0; i < mh->ncmds; i++) {
485 if (cmd->cmd == LC_SEGMENT_KERNEL) {
486 kernel_segment_command_t *orig_sg = (kernel_segment_command_t *) cmd;
487
488 if (LIT_STRNEQL(orig_sg->segname, SEG_TEXT)) {
489 orig_ts = orig_sg;
490 } else if (LIT_STRNEQL(orig_sg->segname, SEG_LINKEDIT)) {
491 orig_le = orig_sg;
492 } else if (LIT_STRNEQL(orig_sg->segname, "")) {
493 orig_ts = orig_sg; /* kexts have a single unnamed segment */
494 }
495 } else if (cmd->cmd == LC_SYMTAB) {
496 orig_st = (struct symtab_command *) cmd;
497 }
498
499 cmd = (struct load_command *) ((uintptr_t) cmd + cmd->cmdsize);
500 }
501
502 if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL)) {
503 return;
504 }
505
506 sym = (kernel_nlist_t *)(orig_le->vmaddr + orig_st->symoff - orig_le->fileoff);
507 strings = (char *)(orig_le->vmaddr + orig_st->stroff - orig_le->fileoff);
508
509 for (i = 0; i < orig_st->nsyms; i++) {
510 uint8_t n_type = sym[i].n_type & (N_TYPE | N_EXT);
511 char *name = strings + sym[i].n_un.n_strx;
512 const char *prev_name;
513 unsigned long best;
514 unsigned int j;
515
516 /* Check that the symbol is a global and that it has a name. */
517 if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type)) {
518 continue;
519 }
520
521 if (0 == sym[i].n_un.n_strx) { /* iff a null, "", name. */
522 continue;
523 }
524
525 /* Lop off omnipresent leading underscore. */
526 if (*name == '_') {
527 name += 1;
528 }
529
530 if (strncmp(name, DTRACE_PROBE_PREFIX, sizeof(DTRACE_PROBE_PREFIX) - 1) == 0) {
531 sdt_probedesc_t *sdpd = kmem_alloc(sizeof(sdt_probedesc_t), KM_SLEEP);
532 int len = strlen(name) + 1;
533
534 sdpd->sdpd_name = kmem_alloc(len, KM_SLEEP);
535 strncpy(sdpd->sdpd_name, name, len); /* NUL termination is ensured. */
536
537 prev_name = "<unknown>";
538 best = 0;
539
540 /*
541 * Find the symbol immediately preceding the sdt probe site just discovered,
542 * that symbol names the function containing the sdt probe.
543 */
544 for (j = 0; j < orig_st->nsyms; j++) {
545 uint8_t jn_type = sym[j].n_type & N_TYPE;
546 char *jname = strings + sym[j].n_un.n_strx;
547
548 if ((N_SECT != jn_type && N_ABS != jn_type)) {
549 continue;
550 }
551
552 if (0 == sym[j].n_un.n_strx) { /* iff a null, "", name. */
553 continue;
554 }
555
556 if (*jname == '_') {
557 jname += 1;
558 }
559
560 if (*(unsigned long *)sym[i].n_value <= (unsigned long)sym[j].n_value) {
561 continue;
562 }
563
564 if ((unsigned long)sym[j].n_value > best) {
565 best = (unsigned long)sym[j].n_value;
566 prev_name = jname;
567 }
568 }
569
570 sdpd->sdpd_func = kmem_alloc((len = strlen(prev_name) + 1), KM_SLEEP);
571 strncpy(sdpd->sdpd_func, prev_name, len); /* NUL termination is ensured. */
572
573 sdpd->sdpd_offset = *(unsigned long *)sym[i].n_value;
574 #if defined(__arm__)
575 /* PR8353094 - mask off thumb-bit */
576 sdpd->sdpd_offset &= ~0x1U;
577 #elif defined(__arm64__)
578 sdpd->sdpd_offset &= ~0x1LU;
579 #endif /* __arm__ */
580
581 #if 0
582 printf("sdt_init: sdpd_offset=0x%lx, n_value=0x%lx, name=%s\n",
583 sdpd->sdpd_offset, *(unsigned long *)sym[i].n_value, name);
584 #endif
585
586 sdpd->sdpd_next = g_sdt_mach_module.sdt_probes;
587 g_sdt_mach_module.sdt_probes = sdpd;
588 } else {
589 prev_name = name;
590 }
591 }
592 }
593 }
594
595 void
596 sdt_init( void )
597 {
598 int majdevno = cdevsw_add(SDT_MAJOR, &sdt_cdevsw);
599
600 if (majdevno < 0) {
601 printf("sdt_init: failed to allocate a major number!\n");
602 return;
603 }
604
605 if (dtrace_sdt_probes_restricted()) {
606 return;
607 }
608
609 sdt_attach((dev_info_t*)(uintptr_t)majdevno);
610 }
611
612 #undef SDT_MAJOR
613
614 /*ARGSUSED*/
615 void
616 sdt_provide_module(void *arg, struct modctl *ctl)
617 {
618 #pragma unused(arg)
619 ASSERT(ctl != NULL);
620 ASSERT(dtrace_kernel_symbol_mode != DTRACE_KERNEL_SYMBOLS_NEVER);
621 LCK_MTX_ASSERT(&mod_lock, LCK_MTX_ASSERT_OWNED);
622
623 if (MOD_SDT_DONE(ctl)) {
624 return;
625 }
626
627 if (MOD_IS_MACH_KERNEL(ctl)) {
628 __sdt_provide_module(arg, &g_sdt_kernctl);
629
630 sdt_probedesc_t *sdpd = g_sdt_mach_module.sdt_probes;
631 while (sdpd) {
632 sdt_probedesc_t *this_sdpd = sdpd;
633 kmem_free((void *)sdpd->sdpd_name, strlen(sdpd->sdpd_name) + 1);
634 kmem_free((void *)sdpd->sdpd_func, strlen(sdpd->sdpd_func) + 1);
635 sdpd = sdpd->sdpd_next;
636 kmem_free((void *)this_sdpd, sizeof(sdt_probedesc_t));
637 }
638 g_sdt_mach_module.sdt_probes = NULL;
639 } else {
640 /*
641 * APPLE NOTE: sdt probes for kexts not yet implemented
642 */
643 }
644
645 /* Need to mark this module as completed */
646 ctl->mod_flags |= MODCTL_SDT_PROBES_PROVIDED;
647 }