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
)
110 if (!mp
|| mp
->sdt_nprobes
!= 0 || (sdpd
= mp
->sdt_probes
) == NULL
)
113 for (sdpd
= mp
->sdt_probes
; sdpd
!= NULL
; sdpd
= sdpd
->sdpd_next
) {
114 const char *name
= sdpd
->sdpd_name
, *func
;
119 for (prov
= sdt_providers
; prov
->sdtp_prefix
!= NULL
; prov
++) {
120 const char *prefpart
, *prefix
= prov
->sdtp_prefix
;
122 if ((prefpart
= strstr(name
, prefix
))) {
123 name
= prefpart
+ strlen(prefix
);
128 nname
= kmem_alloc(len
= strlen(name
) + 1, KM_SLEEP
);
130 for (i
= 0, j
= 0; name
[j
] != '\0'; i
++) {
131 if (name
[j
] == '_' && name
[j
+ 1] == '_') {
135 nname
[i
] = name
[j
++];
141 sdp
= kmem_zalloc(sizeof (sdt_probe_t
), KM_SLEEP
);
142 sdp
->sdp_loadcnt
= ctl
->mod_loadcnt
;
144 sdp
->sdp_name
= nname
;
145 sdp
->sdp_namelen
= len
;
146 sdp
->sdp_provider
= prov
;
148 func
= sdpd
->sdpd_func
;
154 * We have our provider. Now create the probe.
156 if ((id
= dtrace_probe_lookup(prov
->sdtp_id
, modname
,
157 func
, nname
)) != DTRACE_IDNONE
) {
158 old
= dtrace_probe_arg(prov
->sdtp_id
, id
);
161 sdp
->sdp_next
= old
->sdp_next
;
165 sdp
->sdp_id
= dtrace_probe_create(prov
->sdtp_id
,
166 modname
, func
, nname
, SDT_AFRAMES
, sdp
);
172 printf ("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd
, sdp
, nname
, sdp
->sdp_id
);
176 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)];
177 sdt_probetab
[SDT_ADDR2NDX(sdpd
->sdpd_offset
)] = sdp
;
179 sdp
->sdp_patchval
= SDT_PATCHVAL
;
180 sdp
->sdp_patchpoint
= (sdt_instr_t
*)sdpd
->sdpd_offset
;
181 sdp
->sdp_savedval
= *sdp
->sdp_patchpoint
;
187 sdt_destroy(void *arg
, dtrace_id_t id
, void *parg
)
189 #pragma unused(arg,id)
190 sdt_probe_t
*sdp
= parg
, *old
, *last
, *hash
;
193 #if !defined(__APPLE__)
195 * APPLE NOTE: sdt probes for kexts not yet implemented
197 struct modctl
*ctl
= sdp
->sdp_ctl
;
199 if (ctl
!= NULL
&& ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
) {
200 if ((ctl
->mod_loadcnt
== sdp
->sdp_loadcnt
&&
202 ((struct module *)(ctl
->mod_mp
))->sdt_nprobes
--;
205 #endif /* __APPLE__ */
207 while (sdp
!= NULL
) {
211 * Now we need to remove this probe from the sdt_probetab.
213 ndx
= SDT_ADDR2NDX(sdp
->sdp_patchpoint
);
215 hash
= sdt_probetab
[ndx
];
217 while (hash
!= sdp
) {
218 ASSERT(hash
!= NULL
);
220 hash
= hash
->sdp_hashnext
;
224 last
->sdp_hashnext
= sdp
->sdp_hashnext
;
226 sdt_probetab
[ndx
] = sdp
->sdp_hashnext
;
229 kmem_free(sdp
->sdp_name
, sdp
->sdp_namelen
);
231 kmem_free(old
, sizeof (sdt_probe_t
));
237 sdt_enable(void *arg
, dtrace_id_t id
, void *parg
)
239 #pragma unused(arg,id)
240 sdt_probe_t
*sdp
= parg
;
241 struct modctl
*ctl
= sdp
->sdp_ctl
;
246 * If this module has disappeared since we discovered its probes,
247 * refuse to enable it.
249 if (!ctl
->mod_loaded
) {
251 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
252 "(module %s unloaded)",
253 sdp
->sdp_name
, ctl
->mod_modname
);
259 * Now check that our modctl has the expected load count. If it
260 * doesn't, this module must have been unloaded and reloaded -- and
261 * we're not going to touch it.
263 if (ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
) {
265 cmn_err(CE_NOTE
, "sdt is failing for probe %s "
266 "(module %s reloaded)",
267 sdp
->sdp_name
, ctl
->mod_modname
);
272 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
273 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
275 cmn_err(CE_NOTE
, "sdt_enable is failing for probe %s "
276 "in module %s: tempDTraceTrapHook already occupied.",
277 sdp
->sdp_name
, ctl
->mod_modname
);
282 while (sdp
!= NULL
) {
283 (void)ml_nofault_copy( (vm_offset_t
)&sdp
->sdp_patchval
, (vm_offset_t
)sdp
->sdp_patchpoint
,
284 (vm_size_t
)sizeof(sdp
->sdp_patchval
));
287 * Make the patched instruction visible via a data + instruction
288 * cache fush on platforms that need it
290 flush_dcache((vm_offset_t
)sdp
->sdp_patchpoint
,(vm_size_t
)sizeof(sdp
->sdp_patchval
), 0);
291 invalidate_icache((vm_offset_t
)sdp
->sdp_patchpoint
,(vm_size_t
)sizeof(sdp
->sdp_patchval
), 0);
302 sdt_disable(void *arg
, dtrace_id_t id
, void *parg
)
304 #pragma unused(arg,id)
305 sdt_probe_t
*sdp
= parg
;
306 struct modctl
*ctl
= sdp
->sdp_ctl
;
310 if (!ctl
->mod_loaded
|| ctl
->mod_loadcnt
!= sdp
->sdp_loadcnt
)
313 while (sdp
!= NULL
) {
314 (void)ml_nofault_copy( (vm_offset_t
)&sdp
->sdp_savedval
, (vm_offset_t
)sdp
->sdp_patchpoint
,
315 (vm_size_t
)sizeof(sdp
->sdp_savedval
));
317 * Make the patched instruction visible via a data + instruction
318 * cache flush on platforms that need it
320 flush_dcache((vm_offset_t
)sdp
->sdp_patchpoint
,(vm_size_t
)sizeof(sdp
->sdp_savedval
), 0);
321 invalidate_icache((vm_offset_t
)sdp
->sdp_patchpoint
,(vm_size_t
)sizeof(sdp
->sdp_savedval
), 0);
329 static dtrace_pops_t sdt_pops
= {
330 .dtps_provide
= NULL
,
331 .dtps_provide_module
= sdt_provide_module
,
332 .dtps_enable
= sdt_enable
,
333 .dtps_disable
= sdt_disable
,
334 .dtps_suspend
= NULL
,
336 .dtps_getargdesc
= sdt_getargdesc
,
337 .dtps_getargval
= sdt_getarg
,
338 .dtps_usermode
= NULL
,
339 .dtps_destroy
= sdt_destroy
,
344 sdt_attach(dev_info_t
*devi
)
346 sdt_provider_t
*prov
;
348 if (ddi_create_minor_node(devi
, "sdt", S_IFCHR
,
349 0, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
350 cmn_err(CE_NOTE
, "/dev/sdt couldn't create minor node");
351 ddi_remove_minor_node(devi
, NULL
);
352 return (DDI_FAILURE
);
355 if (sdt_probetab_size
== 0)
356 sdt_probetab_size
= SDT_PROBETAB_SIZE
;
358 sdt_probetab_mask
= sdt_probetab_size
- 1;
360 kmem_zalloc(sdt_probetab_size
* sizeof (sdt_probe_t
*), KM_SLEEP
);
361 dtrace_invop_add(sdt_invop
);
363 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
364 if (dtrace_register(prov
->sdtp_name
, prov
->sdtp_attr
,
365 DTRACE_PRIV_KERNEL
, NULL
,
366 &sdt_pops
, prov
, &prov
->sdtp_id
) != 0) {
367 cmn_err(CE_WARN
, "failed to register sdt provider %s",
372 return (DDI_SUCCESS
);
376 * APPLE NOTE: sdt_detach not implemented
378 #if !defined(__APPLE__)
381 sdt_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
383 sdt_provider_t
*prov
;
390 return (DDI_SUCCESS
);
393 return (DDI_FAILURE
);
396 for (prov
= sdt_providers
; prov
->sdtp_name
!= NULL
; prov
++) {
397 if (prov
->sdtp_id
!= DTRACE_PROVNONE
) {
398 if (dtrace_unregister(prov
->sdtp_id
) != 0)
399 return (DDI_FAILURE
);
401 prov
->sdtp_id
= DTRACE_PROVNONE
;
405 dtrace_invop_remove(sdt_invop
);
406 kmem_free(sdt_probetab
, sdt_probetab_size
* sizeof (sdt_probe_t
*));
408 return (DDI_SUCCESS
);
410 #endif /* __APPLE__ */
414 int _sdt_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
416 #pragma unused(dev,flags,devtype,p)
420 #define SDT_MAJOR -24 /* let the kernel pick the device number */
423 * A struct describing which functions will get invoked for certain
426 static struct cdevsw sdt_cdevsw
=
428 _sdt_open
, /* open */
429 eno_opcl
, /* close */
430 eno_rdwrt
, /* read */
431 eno_rdwrt
, /* write */
432 eno_ioctl
, /* ioctl */
433 (stop_fcn_t
*)nulldev
, /* stop */
434 (reset_fcn_t
*)nulldev
, /* reset */
436 eno_select
, /* select */
438 eno_strat
, /* strategy */
444 static struct modctl g_sdt_kernctl
;
445 static struct module g_sdt_mach_module
;
447 #include <mach-o/nlist.h>
448 #include <libkern/kernel_mach_header.h>
450 void 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
;
498 cmd
= (struct load_command
*) ((uintptr_t) cmd
+ cmd
->cmdsize
);
501 if ((orig_ts
== NULL
) || (orig_st
== NULL
) || (orig_le
== NULL
))
504 sym
= (kernel_nlist_t
*)(orig_le
->vmaddr
+ orig_st
->symoff
- orig_le
->fileoff
);
505 strings
= (char *)(orig_le
->vmaddr
+ orig_st
->stroff
- orig_le
->fileoff
);
507 for (i
= 0; i
< orig_st
->nsyms
; i
++) {
508 uint8_t n_type
= sym
[i
].n_type
& (N_TYPE
| N_EXT
);
509 char *name
= strings
+ sym
[i
].n_un
.n_strx
;
510 const char *prev_name
;
514 /* Check that the symbol is a global and that it has a name. */
515 if (((N_SECT
| N_EXT
) != n_type
&& (N_ABS
| N_EXT
) != n_type
))
518 if (0 == sym
[i
].n_un
.n_strx
) /* iff a null, "", name. */
521 /* Lop off omnipresent leading underscore. */
525 if (strncmp(name
, DTRACE_PROBE_PREFIX
, sizeof(DTRACE_PROBE_PREFIX
) - 1) == 0) {
526 sdt_probedesc_t
*sdpd
= kmem_alloc(sizeof(sdt_probedesc_t
), KM_SLEEP
);
527 int len
= strlen(name
) + 1;
529 sdpd
->sdpd_name
= kmem_alloc(len
, KM_SLEEP
);
530 strncpy(sdpd
->sdpd_name
, name
, len
); /* NUL termination is ensured. */
532 prev_name
= "<unknown>";
536 * Find the symbol immediately preceding the sdt probe site just discovered,
537 * that symbol names the function containing the sdt probe.
539 for (j
= 0; j
< orig_st
->nsyms
; j
++) {
540 uint8_t jn_type
= sym
[j
].n_type
& N_TYPE
;
541 char *jname
= strings
+ sym
[j
].n_un
.n_strx
;
543 if ((N_SECT
!= jn_type
&& N_ABS
!= jn_type
))
546 if (0 == sym
[j
].n_un
.n_strx
) /* iff a null, "", name. */
552 if (*(unsigned long *)sym
[i
].n_value
<= (unsigned long)sym
[j
].n_value
)
555 if ((unsigned long)sym
[j
].n_value
> best
) {
556 best
= (unsigned long)sym
[j
].n_value
;
561 sdpd
->sdpd_func
= kmem_alloc((len
= strlen(prev_name
) + 1), KM_SLEEP
);
562 strncpy(sdpd
->sdpd_func
, prev_name
, len
); /* NUL termination is ensured. */
564 sdpd
->sdpd_offset
= *(unsigned long *)sym
[i
].n_value
;
566 /* PR8353094 - mask off thumb-bit */
567 sdpd
->sdpd_offset
&= ~0x1U
;
568 #elif defined(__arm64__)
569 sdpd
->sdpd_offset
&= ~0x1LU
;
573 printf("sdt_init: sdpd_offset=0x%lx, n_value=0x%lx, name=%s\n",
574 sdpd
->sdpd_offset
, *(unsigned long *)sym
[i
].n_value
, name
);
577 sdpd
->sdpd_next
= g_sdt_mach_module
.sdt_probes
;
578 g_sdt_mach_module
.sdt_probes
= sdpd
;
586 void sdt_init( void )
588 int majdevno
= cdevsw_add(SDT_MAJOR
, &sdt_cdevsw
);
591 printf("sdt_init: failed to allocate a major number!\n");
595 if (dtrace_sdt_probes_restricted()) {
599 sdt_attach((dev_info_t
*)(uintptr_t)majdevno
);
606 sdt_provide_module(void *arg
, struct modctl
*ctl
)
610 ASSERT(dtrace_kernel_symbol_mode
!= DTRACE_KERNEL_SYMBOLS_NEVER
);
611 LCK_MTX_ASSERT(&mod_lock
, LCK_MTX_ASSERT_OWNED
);
613 if (MOD_SDT_DONE(ctl
))
616 if (MOD_IS_MACH_KERNEL(ctl
)) {
617 __sdt_provide_module(arg
, &g_sdt_kernctl
);
619 sdt_probedesc_t
*sdpd
= g_sdt_mach_module
.sdt_probes
;
621 sdt_probedesc_t
*this_sdpd
= sdpd
;
622 kmem_free((void *)sdpd
->sdpd_name
, strlen(sdpd
->sdpd_name
) + 1);
623 kmem_free((void *)sdpd
->sdpd_func
, strlen(sdpd
->sdpd_func
) + 1);
624 sdpd
= sdpd
->sdpd_next
;
625 kmem_free((void *)this_sdpd
, sizeof(sdt_probedesc_t
));
627 g_sdt_mach_module
.sdt_probes
= NULL
;
630 * APPLE NOTE: sdt probes for kexts not yet implemented
634 /* Need to mark this module as completed */
635 ctl
->mod_flags
|= MODCTL_SDT_PROBES_PROVIDED
;