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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/errno.h>
30 #include <sys/ioctl.h>
32 #include <sys/fcntl.h>
33 #include <miscfs/devfs/devfs.h>
36 #include <arm/caches_internal.h>
39 #include <sys/dtrace.h>
40 #include <sys/dtrace_impl.h>
42 #include <sys/dtrace_glue.h>
44 #include <sys/sdt_impl.h>
45 extern int dtrace_kernel_symbol_mode
;
47 /* #include <machine/trap.h */
48 struct savearea_t
; /* Used anonymously */
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
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
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
69 #error Unknown architecture
72 #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */
74 #define DTRACE_PROBE_PREFIX "_dtrace_probe$"
76 static int sdt_verbose
= 0;
77 sdt_probe_t
**sdt_probetab
;
78 int sdt_probetab_size
;
79 int sdt_probetab_mask
;
83 __sdt_provide_module(void *arg
, struct modctl
*ctl
)
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
;
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.
97 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
98 if (prov
->sdtp_id
== DTRACE_PROVNONE
) {
103 if (!mp
|| mp
->sdt_nprobes
!= 0 || (sdpd
= mp
->sdt_probes
) == NULL
) {
107 for (sdpd
= mp
->sdt_probes
; sdpd
!= NULL
; sdpd
= sdpd
->sdpd_next
) {
108 const char *name
= sdpd
->sdpd_name
, *func
;
113 for (prov
= sdt_providers
; prov
->sdtp_prefix
!= NULL
; prov
++) {
114 const char *prefpart
, *prefix
= prov
->sdtp_prefix
;
116 if ((prefpart
= strstr(name
, prefix
))) {
117 name
= prefpart
+ strlen(prefix
);
122 nname
= kmem_alloc(len
= strlen(name
) + 1, KM_SLEEP
);
124 for (i
= 0, j
= 0; name
[j
] != '\0'; i
++) {
125 if (name
[j
] == '_' && name
[j
+ 1] == '_') {
129 nname
[i
] = name
[j
++];
135 sdp
= kmem_zalloc(sizeof(sdt_probe_t
), KM_SLEEP
);
136 sdp
->sdp_loadcnt
= ctl
->mod_loadcnt
;
138 sdp
->sdp_name
= nname
;
139 sdp
->sdp_namelen
= len
;
140 sdp
->sdp_provider
= prov
;
142 func
= sdpd
->sdpd_func
;
149 * We have our provider. Now create the probe.
151 if ((id
= dtrace_probe_lookup(prov
->sdtp_id
, modname
,
152 func
, nname
)) != DTRACE_IDNONE
) {
153 old
= dtrace_probe_arg(prov
->sdtp_id
, id
);
156 sdp
->sdp_next
= old
->sdp_next
;
160 sdp
->sdp_id
= dtrace_probe_create(prov
->sdtp_id
,
161 modname
, func
, nname
, SDT_AFRAMES
, sdp
);
167 printf("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd
, sdp
, nname
, sdp
->sdp_id
);
171 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)];
172 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)] = sdp
;
174 sdp
->sdp_patchval
= SDT_PATCHVAL
;
175 sdp
->sdp_patchpoint
= (sdt_instr_t
*)sdpd
->sdpd_offset
;
176 sdp
->sdp_savedval
= *sdp
->sdp_patchpoint
;
182 sdt_destroy(void *arg
, dtrace_id_t id
, void *parg
)
184 #pragma unused(arg,id)
185 sdt_probe_t
*sdp
= parg
, *old
, *last
, *hash
;
188 #if !defined(__APPLE__)
190 * APPLE NOTE: sdt probes for kexts not yet implemented
192 struct modctl
*ctl
= sdp
->sdp_ctl
;
194 if (ctl
!= NULL
&& ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
) {
195 if ((ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
&&
197 ((struct module *)(ctl
->mod_mp
))->sdt_nprobes
--;
200 #endif /* __APPLE__ */
202 while (sdp
!= NULL
) {
206 * Now we need to remove this probe from the sdt_probetab.
208 ndx
= SDT_ADDR2NDX(sdp
->sdp_patchpoint
);
210 hash
= sdt_probetab
[ndx
];
212 while (hash
!= sdp
) {
213 ASSERT(hash
!= NULL
);
215 hash
= hash
->sdp_hashnext
;
219 last
->sdp_hashnext
= sdp
->sdp_hashnext
;
221 sdt_probetab
[ndx
] = sdp
->sdp_hashnext
;
224 kmem_free(sdp
->sdp_name
, sdp
->sdp_namelen
);
226 kmem_free(old
, sizeof(sdt_probe_t
));
232 sdt_enable(void *arg
, dtrace_id_t id
, void *parg
)
234 #pragma unused(arg,id)
235 sdt_probe_t
*sdp
= parg
;
236 struct modctl
*ctl
= sdp
->sdp_ctl
;
241 * If this module has disappeared since we discovered its probes,
242 * refuse to enable it.
244 if (!ctl
->mod_loaded
) {
246 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
247 "(module %s unloaded)",
248 sdp
->sdp_name
, ctl
->mod_modname
);
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.
258 if (ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
) {
260 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
261 "(module %s reloaded)",
262 sdp
->sdp_name
, ctl
->mod_modname
);
267 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
268 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
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
);
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
));
282 * Make the patched instruction visible via a data + instruction
283 * cache fush on platforms that need it
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);
297 sdt_disable(void *arg
, dtrace_id_t id
, void *parg
)
299 #pragma unused(arg,id)
300 sdt_probe_t
*sdp
= parg
;
301 struct modctl
*ctl
= sdp
->sdp_ctl
;
305 if (!ctl
->mod_loaded
|| ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
) {
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
));
313 * Make the patched instruction visible via a data + instruction
314 * cache flush on platforms that need it
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);
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
,
332 .dtps_getargdesc
= sdt_getargdesc
,
333 .dtps_getargval
= sdt_getarg
,
334 .dtps_usermode
= NULL
,
335 .dtps_destroy
= sdt_destroy
,
340 sdt_attach(dev_info_t
*devi
)
342 sdt_provider_t
*prov
;
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
);
351 if (sdt_probetab_size
== 0) {
352 sdt_probetab_size
= SDT_PROBETAB_SIZE
;
355 sdt_probetab_mask
= sdt_probetab_size
- 1;
357 kmem_zalloc(sdt_probetab_size
* sizeof(sdt_probe_t
*), KM_SLEEP
);
358 dtrace_invop_add(sdt_invop
);
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",
373 * APPLE NOTE: sdt_detach not implemented
375 #if !defined(__APPLE__)
378 sdt_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
380 sdt_provider_t
*prov
;
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) {
399 prov
->sdtp_id
= DTRACE_PROVNONE
;
403 dtrace_invop_remove(sdt_invop
);
404 kmem_free(sdt_probetab
, sdt_probetab_size
* sizeof(sdt_probe_t
*));
408 #endif /* __APPLE__ */
413 _sdt_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
415 #pragma unused(dev,flags,devtype,p)
419 #define SDT_MAJOR -24 /* let the kernel pick the device number */
422 * A struct describing which functions will get invoked for certain
425 static struct cdevsw sdt_cdevsw
=
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 */
435 eno_select
, /* select */
437 eno_strat
, /* strategy */
443 static struct modctl g_sdt_kernctl
;
444 static struct module g_sdt_mach_module
;
446 #include <mach-o/nlist.h>
447 #include <libkern/kernel_mach_header.h>
450 sdt_early_init( void )
452 if (dtrace_sdt_probes_restricted()) {
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;
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
;
467 g_sdt_mach_module
.sdt_nprobes
= 0;
468 g_sdt_mach_module
.sdt_probes
= NULL
;
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
);
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;
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
;
488 if (LIT_STRNEQL(orig_sg
->segname
, SEG_TEXT
)) {
490 } else if (LIT_STRNEQL(orig_sg
->segname
, SEG_LINKEDIT
)) {
492 } else if (LIT_STRNEQL(orig_sg
->segname
, "")) {
493 orig_ts
= orig_sg
; /* kexts have a single unnamed segment */
495 } else if (cmd
->cmd
== LC_SYMTAB
) {
496 orig_st
= (struct symtab_command
*) cmd
;
499 cmd
= (struct load_command
*) ((uintptr_t) cmd
+ cmd
->cmdsize
);
502 if ((orig_ts
== NULL
) || (orig_st
== NULL
) || (orig_le
== NULL
)) {
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
);
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
;
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
)) {
521 if (0 == sym
[i
].n_un
.n_strx
) { /* iff a null, "", name. */
525 /* Lop off omnipresent leading underscore. */
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;
534 sdpd
->sdpd_name
= kmem_alloc(len
, KM_SLEEP
);
535 strncpy(sdpd
->sdpd_name
, name
, len
); /* NUL termination is ensured. */
537 prev_name
= "<unknown>";
541 * Find the symbol immediately preceding the sdt probe site just discovered,
542 * that symbol names the function containing the sdt probe.
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
;
548 if ((N_SECT
!= jn_type
&& N_ABS
!= jn_type
)) {
552 if (0 == sym
[j
].n_un
.n_strx
) { /* iff a null, "", name. */
560 if (*(unsigned long *)sym
[i
].n_value
<= (unsigned long)sym
[j
].n_value
) {
564 if ((unsigned long)sym
[j
].n_value
> best
) {
565 best
= (unsigned long)sym
[j
].n_value
;
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. */
573 sdpd
->sdpd_offset
= *(unsigned long *)sym
[i
].n_value
;
575 /* PR8353094 - mask off thumb-bit */
576 sdpd
->sdpd_offset
&= ~0x1U
;
577 #elif defined(__arm64__)
578 sdpd
->sdpd_offset
&= ~0x1LU
;
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
);
586 sdpd
->sdpd_next
= g_sdt_mach_module
.sdt_probes
;
587 g_sdt_mach_module
.sdt_probes
= sdpd
;
598 int majdevno
= cdevsw_add(SDT_MAJOR
, &sdt_cdevsw
);
601 printf("sdt_init: failed to allocate a major number!\n");
605 if (dtrace_sdt_probes_restricted()) {
609 sdt_attach((dev_info_t
*)(uintptr_t)majdevno
);
616 sdt_provide_module(void *arg
, struct modctl
*ctl
)
620 ASSERT(dtrace_kernel_symbol_mode
!= DTRACE_KERNEL_SYMBOLS_NEVER
);
621 LCK_MTX_ASSERT(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
623 if (MOD_SDT_DONE(ctl
)) {
627 if (MOD_IS_MACH_KERNEL(ctl
)) {
628 __sdt_provide_module(arg
, &g_sdt_kernctl
);
630 sdt_probedesc_t
*sdpd
= g_sdt_mach_module
.sdt_probes
;
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
));
638 g_sdt_mach_module
.sdt_probes
= NULL
;
641 * APPLE NOTE: sdt probes for kexts not yet implemented
645 /* Need to mark this module as completed */
646 ctl
->mod_flags
|= MODCTL_SDT_PROBES_PROVIDED
;