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 /* #pragma ident "@(#)sdt.c 1.9 08/07/01 SMI" */
30 #define _KERNEL /* Solaris vs. Darwin */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
38 #include <sys/ioctl.h>
40 #include <sys/fcntl.h>
41 #include <miscfs/devfs/devfs.h>
44 #include <arm/caches_internal.h>
47 #include <sys/dtrace.h>
48 #include <sys/dtrace_impl.h>
50 #include <sys/dtrace_glue.h>
52 #include <sys/sdt_impl.h>
53 extern int dtrace_kernel_symbol_mode
;
55 /* #include <machine/trap.h */
56 struct savearea_t
; /* Used anonymously */
59 typedef kern_return_t (*perfCallback
)(int, struct savearea_t
*, __unused
int, __unused
int);
60 extern perfCallback tempDTraceTrapHook
;
61 extern kern_return_t
fbt_perfCallback(int, struct savearea_t
*, __unused
int, __unused
int);
62 #define SDT_PATCHVAL 0xdefc
64 #elif defined(__arm64__)
65 typedef kern_return_t (*perfCallback
)(int, struct savearea_t
*, __unused
int, __unused
int);
66 extern perfCallback tempDTraceTrapHook
;
67 extern kern_return_t
fbt_perfCallback(int, struct savearea_t
*, __unused
int, __unused
int);
68 #define SDT_PATCHVAL 0xe7eeee7e
70 #elif defined(__x86_64__)
71 typedef kern_return_t (*perfCallback
)(int, struct savearea_t
*, uintptr_t *, int);
72 extern perfCallback tempDTraceTrapHook
;
73 extern kern_return_t
fbt_perfCallback(int, struct savearea_t
*, uintptr_t *, int);
74 #define SDT_PATCHVAL 0xf0
77 #error Unknown architecture
80 #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */
82 #define DTRACE_PROBE_PREFIX "_dtrace_probe$"
84 static int sdt_verbose
= 0;
85 sdt_probe_t
**sdt_probetab
;
86 int sdt_probetab_size
;
87 int sdt_probetab_mask
;
91 __sdt_provide_module(void *arg
, struct modctl
*ctl
)
94 struct module *mp
= (struct module *)ctl
->mod_address
;
95 char *modname
= ctl
->mod_modname
;
96 sdt_probedesc_t
*sdpd
;
97 sdt_probe_t
*sdp
, *old
;
102 * One for all, and all for one: if we haven't yet registered all of
103 * our providers, we'll refuse to provide anything.
105 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
106 if (prov
->sdtp_id
== DTRACE_PROVNONE
) {
111 if (!mp
|| mp
->sdt_nprobes
!= 0 || (sdpd
= mp
->sdt_probes
) == NULL
) {
115 for (sdpd
= mp
->sdt_probes
; sdpd
!= NULL
; sdpd
= sdpd
->sdpd_next
) {
116 const char *name
= sdpd
->sdpd_name
, *func
;
121 for (prov
= sdt_providers
; prov
->sdtp_prefix
!= NULL
; prov
++) {
122 const char *prefpart
, *prefix
= prov
->sdtp_prefix
;
124 if ((prefpart
= strstr(name
, prefix
))) {
125 name
= prefpart
+ strlen(prefix
);
130 nname
= kmem_alloc(len
= strlen(name
) + 1, KM_SLEEP
);
132 for (i
= 0, j
= 0; name
[j
] != '\0'; i
++) {
133 if (name
[j
] == '_' && name
[j
+ 1] == '_') {
137 nname
[i
] = name
[j
++];
143 sdp
= kmem_zalloc(sizeof(sdt_probe_t
), KM_SLEEP
);
144 sdp
->sdp_loadcnt
= ctl
->mod_loadcnt
;
146 sdp
->sdp_name
= nname
;
147 sdp
->sdp_namelen
= len
;
148 sdp
->sdp_provider
= prov
;
150 func
= sdpd
->sdpd_func
;
157 * We have our provider. Now create the probe.
159 if ((id
= dtrace_probe_lookup(prov
->sdtp_id
, modname
,
160 func
, nname
)) != DTRACE_IDNONE
) {
161 old
= dtrace_probe_arg(prov
->sdtp_id
, id
);
164 sdp
->sdp_next
= old
->sdp_next
;
168 sdp
->sdp_id
= dtrace_probe_create(prov
->sdtp_id
,
169 modname
, func
, nname
, SDT_AFRAMES
, sdp
);
175 printf("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd
, sdp
, nname
, sdp
->sdp_id
);
179 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)];
180 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)] = sdp
;
182 sdp
->sdp_patchval
= SDT_PATCHVAL
;
183 sdp
->sdp_patchpoint
= (sdt_instr_t
*)sdpd
->sdpd_offset
;
184 sdp
->sdp_savedval
= *sdp
->sdp_patchpoint
;
190 sdt_destroy(void *arg
, dtrace_id_t id
, void *parg
)
192 #pragma unused(arg,id)
193 sdt_probe_t
*sdp
= parg
, *old
, *last
, *hash
;
196 #if !defined(__APPLE__)
198 * APPLE NOTE: sdt probes for kexts not yet implemented
200 struct modctl
*ctl
= sdp
->sdp_ctl
;
202 if (ctl
!= NULL
&& ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
) {
203 if ((ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
&&
205 ((struct module *)(ctl
->mod_mp
))->sdt_nprobes
--;
208 #endif /* __APPLE__ */
210 while (sdp
!= NULL
) {
214 * Now we need to remove this probe from the sdt_probetab.
216 ndx
= SDT_ADDR2NDX(sdp
->sdp_patchpoint
);
218 hash
= sdt_probetab
[ndx
];
220 while (hash
!= sdp
) {
221 ASSERT(hash
!= NULL
);
223 hash
= hash
->sdp_hashnext
;
227 last
->sdp_hashnext
= sdp
->sdp_hashnext
;
229 sdt_probetab
[ndx
] = sdp
->sdp_hashnext
;
232 kmem_free(sdp
->sdp_name
, sdp
->sdp_namelen
);
234 kmem_free(old
, sizeof(sdt_probe_t
));
240 sdt_enable(void *arg
, dtrace_id_t id
, void *parg
)
242 #pragma unused(arg,id)
243 sdt_probe_t
*sdp
= parg
;
244 struct modctl
*ctl
= sdp
->sdp_ctl
;
249 * If this module has disappeared since we discovered its probes,
250 * refuse to enable it.
252 if (!ctl
->mod_loaded
) {
254 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
255 "(module %s unloaded)",
256 sdp
->sdp_name
, ctl
->mod_modname
);
262 * Now check that our modctl has the expected load count. If it
263 * doesn't, this module must have been unloaded and reloaded -- and
264 * we're not going to touch it.
266 if (ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
) {
268 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
269 "(module %s reloaded)",
270 sdp
->sdp_name
, ctl
->mod_modname
);
275 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
276 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
278 cmn_err(CE_NOTE
, "sdt_enable is failing for probe %s "
279 "in module %s: tempDTraceTrapHook already occupied.",
280 sdp
->sdp_name
, ctl
->mod_modname
);
285 while (sdp
!= NULL
) {
286 (void)ml_nofault_copy((vm_offset_t
)&sdp
->sdp_patchval
, (vm_offset_t
)sdp
->sdp_patchpoint
,
287 (vm_size_t
)sizeof(sdp
->sdp_patchval
));
290 * Make the patched instruction visible via a data + instruction
291 * cache fush on platforms that need it
293 flush_dcache((vm_offset_t
)sdp
->sdp_patchpoint
, (vm_size_t
)sizeof(sdp
->sdp_patchval
), 0);
294 invalidate_icache((vm_offset_t
)sdp
->sdp_patchpoint
, (vm_size_t
)sizeof(sdp
->sdp_patchval
), 0);
305 sdt_disable(void *arg
, dtrace_id_t id
, void *parg
)
307 #pragma unused(arg,id)
308 sdt_probe_t
*sdp
= parg
;
309 struct modctl
*ctl
= sdp
->sdp_ctl
;
313 if (!ctl
->mod_loaded
|| ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
) {
317 while (sdp
!= NULL
) {
318 (void)ml_nofault_copy((vm_offset_t
)&sdp
->sdp_savedval
, (vm_offset_t
)sdp
->sdp_patchpoint
,
319 (vm_size_t
)sizeof(sdp
->sdp_savedval
));
321 * Make the patched instruction visible via a data + instruction
322 * cache flush on platforms that need it
324 flush_dcache((vm_offset_t
)sdp
->sdp_patchpoint
, (vm_size_t
)sizeof(sdp
->sdp_savedval
), 0);
325 invalidate_icache((vm_offset_t
)sdp
->sdp_patchpoint
, (vm_size_t
)sizeof(sdp
->sdp_savedval
), 0);
333 static dtrace_pops_t sdt_pops
= {
334 .dtps_provide
= NULL
,
335 .dtps_provide_module
= sdt_provide_module
,
336 .dtps_enable
= sdt_enable
,
337 .dtps_disable
= sdt_disable
,
338 .dtps_suspend
= NULL
,
340 .dtps_getargdesc
= sdt_getargdesc
,
341 .dtps_getargval
= sdt_getarg
,
342 .dtps_usermode
= NULL
,
343 .dtps_destroy
= sdt_destroy
,
348 sdt_attach(dev_info_t
*devi
)
350 sdt_provider_t
*prov
;
352 if (ddi_create_minor_node(devi
, "sdt", S_IFCHR
,
353 0, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
354 cmn_err(CE_NOTE
, "/dev/sdt couldn't create minor node");
355 ddi_remove_minor_node(devi
, NULL
);
359 if (sdt_probetab_size
== 0) {
360 sdt_probetab_size
= SDT_PROBETAB_SIZE
;
363 sdt_probetab_mask
= sdt_probetab_size
- 1;
365 kmem_zalloc(sdt_probetab_size
* sizeof(sdt_probe_t
*), KM_SLEEP
);
366 dtrace_invop_add(sdt_invop
);
368 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
369 if (dtrace_register(prov
->sdtp_name
, prov
->sdtp_attr
,
370 DTRACE_PRIV_KERNEL
, NULL
,
371 &sdt_pops
, prov
, &prov
->sdtp_id
) != 0) {
372 cmn_err(CE_WARN
, "failed to register sdt provider %s",
381 * APPLE NOTE: sdt_detach not implemented
383 #if !defined(__APPLE__)
386 sdt_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
388 sdt_provider_t
*prov
;
401 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
402 if (prov
->sdtp_id
!= DTRACE_PROVNONE
) {
403 if (dtrace_unregister(prov
->sdtp_id
) != 0) {
407 prov
->sdtp_id
= DTRACE_PROVNONE
;
411 dtrace_invop_remove(sdt_invop
);
412 kmem_free(sdt_probetab
, sdt_probetab_size
* sizeof(sdt_probe_t
*));
416 #endif /* __APPLE__ */
421 _sdt_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
423 #pragma unused(dev,flags,devtype,p)
427 #define SDT_MAJOR -24 /* let the kernel pick the device number */
430 * A struct describing which functions will get invoked for certain
433 static struct cdevsw sdt_cdevsw
=
435 _sdt_open
, /* open */
436 eno_opcl
, /* close */
437 eno_rdwrt
, /* read */
438 eno_rdwrt
, /* write */
439 eno_ioctl
, /* ioctl */
440 (stop_fcn_t
*)nulldev
, /* stop */
441 (reset_fcn_t
*)nulldev
, /* reset */
443 eno_select
, /* select */
445 eno_strat
, /* strategy */
451 static struct modctl g_sdt_kernctl
;
452 static struct module g_sdt_mach_module
;
454 #include <mach-o/nlist.h>
455 #include <libkern/kernel_mach_header.h>
458 sdt_early_init( void )
460 if (dtrace_sdt_probes_restricted()) {
463 if (MH_MAGIC_KERNEL
!= _mh_execute_header
.magic
) {
464 g_sdt_kernctl
.mod_address
= (vm_address_t
)NULL
;
465 g_sdt_kernctl
.mod_size
= 0;
467 kernel_mach_header_t
*mh
;
468 struct load_command
*cmd
;
469 kernel_segment_command_t
*orig_ts
= NULL
, *orig_le
= NULL
;
470 struct symtab_command
*orig_st
= NULL
;
471 kernel_nlist_t
*sym
= NULL
;
475 g_sdt_mach_module
.sdt_nprobes
= 0;
476 g_sdt_mach_module
.sdt_probes
= NULL
;
478 g_sdt_kernctl
.mod_address
= (vm_address_t
)&g_sdt_mach_module
;
479 g_sdt_kernctl
.mod_size
= 0;
480 strncpy((char *)&(g_sdt_kernctl
.mod_modname
), "mach_kernel", KMOD_MAX_NAME
);
482 g_sdt_kernctl
.mod_next
= NULL
;
483 g_sdt_kernctl
.mod_stale
= NULL
;
484 g_sdt_kernctl
.mod_id
= 0;
485 g_sdt_kernctl
.mod_loadcnt
= 1;
486 g_sdt_kernctl
.mod_loaded
= 1;
487 g_sdt_kernctl
.mod_flags
= 0;
488 g_sdt_kernctl
.mod_nenabled
= 0;
490 mh
= &_mh_execute_header
;
491 cmd
= (struct load_command
*) &mh
[1];
492 for (i
= 0; i
< mh
->ncmds
; i
++) {
493 if (cmd
->cmd
== LC_SEGMENT_KERNEL
) {
494 kernel_segment_command_t
*orig_sg
= (kernel_segment_command_t
*) cmd
;
496 if (LIT_STRNEQL(orig_sg
->segname
, SEG_TEXT
)) {
498 } else if (LIT_STRNEQL(orig_sg
->segname
, SEG_LINKEDIT
)) {
500 } else if (LIT_STRNEQL(orig_sg
->segname
, "")) {
501 orig_ts
= orig_sg
; /* kexts have a single unnamed segment */
503 } else if (cmd
->cmd
== LC_SYMTAB
) {
504 orig_st
= (struct symtab_command
*) cmd
;
507 cmd
= (struct load_command
*) ((uintptr_t) cmd
+ cmd
->cmdsize
);
510 if ((orig_ts
== NULL
) || (orig_st
== NULL
) || (orig_le
== NULL
)) {
514 sym
= (kernel_nlist_t
*)(orig_le
->vmaddr
+ orig_st
->symoff
- orig_le
->fileoff
);
515 strings
= (char *)(orig_le
->vmaddr
+ orig_st
->stroff
- orig_le
->fileoff
);
517 for (i
= 0; i
< orig_st
->nsyms
; i
++) {
518 uint8_t n_type
= sym
[i
].n_type
& (N_TYPE
| N_EXT
);
519 char *name
= strings
+ sym
[i
].n_un
.n_strx
;
520 const char *prev_name
;
524 /* Check that the symbol is a global and that it has a name. */
525 if (((N_SECT
| N_EXT
) != n_type
&& (N_ABS
| N_EXT
) != n_type
)) {
529 if (0 == sym
[i
].n_un
.n_strx
) { /* iff a null, "", name. */
533 /* Lop off omnipresent leading underscore. */
538 if (strncmp(name
, DTRACE_PROBE_PREFIX
, sizeof(DTRACE_PROBE_PREFIX
) - 1) == 0) {
539 sdt_probedesc_t
*sdpd
= kmem_alloc(sizeof(sdt_probedesc_t
), KM_SLEEP
);
540 int len
= strlen(name
) + 1;
542 sdpd
->sdpd_name
= kmem_alloc(len
, KM_SLEEP
);
543 strncpy(sdpd
->sdpd_name
, name
, len
); /* NUL termination is ensured. */
545 prev_name
= "<unknown>";
549 * Find the symbol immediately preceding the sdt probe site just discovered,
550 * that symbol names the function containing the sdt probe.
552 for (j
= 0; j
< orig_st
->nsyms
; j
++) {
553 uint8_t jn_type
= sym
[j
].n_type
& N_TYPE
;
554 char *jname
= strings
+ sym
[j
].n_un
.n_strx
;
556 if ((N_SECT
!= jn_type
&& N_ABS
!= jn_type
)) {
560 if (0 == sym
[j
].n_un
.n_strx
) { /* iff a null, "", name. */
568 if (*(unsigned long *)sym
[i
].n_value
<= (unsigned long)sym
[j
].n_value
) {
572 if ((unsigned long)sym
[j
].n_value
> best
) {
573 best
= (unsigned long)sym
[j
].n_value
;
578 sdpd
->sdpd_func
= kmem_alloc((len
= strlen(prev_name
) + 1), KM_SLEEP
);
579 strncpy(sdpd
->sdpd_func
, prev_name
, len
); /* NUL termination is ensured. */
581 sdpd
->sdpd_offset
= *(unsigned long *)sym
[i
].n_value
;
583 /* PR8353094 - mask off thumb-bit */
584 sdpd
->sdpd_offset
&= ~0x1U
;
585 #elif defined(__arm64__)
586 sdpd
->sdpd_offset
&= ~0x1LU
;
590 printf("sdt_init: sdpd_offset=0x%lx, n_value=0x%lx, name=%s\n",
591 sdpd
->sdpd_offset
, *(unsigned long *)sym
[i
].n_value
, name
);
594 sdpd
->sdpd_next
= g_sdt_mach_module
.sdt_probes
;
595 g_sdt_mach_module
.sdt_probes
= sdpd
;
606 int majdevno
= cdevsw_add(SDT_MAJOR
, &sdt_cdevsw
);
609 printf("sdt_init: failed to allocate a major number!\n");
613 if (dtrace_sdt_probes_restricted()) {
617 sdt_attach((dev_info_t
*)(uintptr_t)majdevno
);
624 sdt_provide_module(void *arg
, struct modctl
*ctl
)
628 ASSERT(dtrace_kernel_symbol_mode
!= DTRACE_KERNEL_SYMBOLS_NEVER
);
629 LCK_MTX_ASSERT(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
631 if (MOD_SDT_DONE(ctl
)) {
635 if (MOD_IS_MACH_KERNEL(ctl
)) {
636 __sdt_provide_module(arg
, &g_sdt_kernctl
);
638 sdt_probedesc_t
*sdpd
= g_sdt_mach_module
.sdt_probes
;
640 sdt_probedesc_t
*this_sdpd
= sdpd
;
641 kmem_free((void *)sdpd
->sdpd_name
, strlen(sdpd
->sdpd_name
) + 1);
642 kmem_free((void *)sdpd
->sdpd_func
, strlen(sdpd
->sdpd_func
) + 1);
643 sdpd
= sdpd
->sdpd_next
;
644 kmem_free((void *)this_sdpd
, sizeof(sdt_probedesc_t
));
646 g_sdt_mach_module
.sdt_probes
= NULL
;
649 * APPLE NOTE: sdt probes for kexts not yet implemented
653 /* Need to mark this module as completed */
654 ctl
->mod_flags
|= MODCTL_SDT_PROBES_PROVIDED
;