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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* #pragma ident "@(#)fbt.c 1.18 07/01/10 SMI" */
30 #define _KERNEL /* Solaris vs. Darwin */
34 #include <mach-o/loader.h>
35 #include <libkern/kernel_mach_header.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/errno.h>
41 #include <sys/ioctl.h>
43 #include <sys/fcntl.h>
44 #include <miscfs/devfs/devfs.h>
45 #include <pexpert/pexpert.h>
47 #include <sys/dtrace.h>
48 #include <sys/dtrace_impl.h>
51 #include <sys/dtrace_glue.h>
53 /* #include <machine/trap.h> */
54 struct savearea_t
; /* Used anonymously */
55 typedef kern_return_t (*perfCallback
)(int, struct savearea_t
*, int, int);
57 #if defined (__ppc__) || defined (__ppc64__)
58 extern perfCallback tempDTraceTrapHook
, tempDTraceIntHook
;
59 extern kern_return_t
fbt_perfCallback(int, struct savearea_t
*, int, int);
60 extern kern_return_t
fbt_perfIntCallback(int, struct savearea_t
*, int, int);
62 extern perfCallback tempDTraceTrapHook
;
63 extern kern_return_t
fbt_perfCallback(int, struct savearea_t
*, int, int);
66 #define FBT_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
67 #define FBT_PROBETAB_SIZE 0x8000 /* 32k entries -- 128K total */
69 static dev_info_t
*fbt_devi
;
70 static int fbt_probetab_size
;
71 dtrace_provider_id_t fbt_id
;
72 fbt_probe_t
**fbt_probetab
;
73 int fbt_probetab_mask
;
74 static int fbt_verbose
= 0;
76 void fbt_init( void );
80 fbt_destroy(void *arg
, dtrace_id_t id
, void *parg
)
82 #pragma unused(arg,id)
83 fbt_probe_t
*fbt
= parg
, *next
, *hash
, *last
;
88 * Now we need to remove this probe from the fbt_probetab.
90 ndx
= FBT_ADDR2NDX(fbt
->fbtp_patchpoint
);
92 hash
= fbt_probetab
[ndx
];
97 hash
= hash
->fbtp_hashnext
;
101 last
->fbtp_hashnext
= fbt
->fbtp_hashnext
;
103 fbt_probetab
[ndx
] = fbt
->fbtp_hashnext
;
106 next
= fbt
->fbtp_next
;
107 kmem_free(fbt
, sizeof (fbt_probe_t
));
110 } while (fbt
!= NULL
);
115 fbt_enable(void *arg
, dtrace_id_t id
, void *parg
)
117 #pragma unused(arg,id)
118 fbt_probe_t
*fbt
= parg
;
119 struct modctl
*ctl
= fbt
->fbtp_ctl
;
121 #if defined (__ppc__) || defined (__ppc64__)
122 dtrace_casptr(&tempDTraceIntHook
, NULL
, fbt_perfIntCallback
);
123 if (tempDTraceIntHook
!= (perfCallback
)fbt_perfIntCallback
) {
125 cmn_err(CE_NOTE
, "fbt_enable is failing for probe %s "
126 "in module %s: tempDTraceIntHook already occupied.",
127 fbt
->fbtp_name
, ctl
->mod_modname
);
133 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
134 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
136 cmn_err(CE_NOTE
, "fbt_enable is failing for probe %s "
137 "in module %s: tempDTraceTrapHook already occupied.",
138 fbt
->fbtp_name
, ctl
->mod_modname
);
143 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
)
144 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_patchval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
145 sizeof(fbt
->fbtp_patchval
));
147 dtrace_membar_consumer();
152 fbt_disable(void *arg
, dtrace_id_t id
, void *parg
)
154 #pragma unused(arg,id)
155 fbt_probe_t
*fbt
= parg
;
157 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
)
158 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_savedval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
159 sizeof(fbt
->fbtp_savedval
));
161 dtrace_membar_consumer();
166 fbt_suspend(void *arg
, dtrace_id_t id
, void *parg
)
168 #pragma unused(arg,id)
169 fbt_probe_t
*fbt
= parg
;
171 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
)
172 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_savedval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
173 sizeof(fbt
->fbtp_savedval
));
175 dtrace_membar_consumer();
180 fbt_resume(void *arg
, dtrace_id_t id
, void *parg
)
182 #pragma unused(arg,id)
183 fbt_probe_t
*fbt
= parg
;
184 struct modctl
*ctl
= fbt
->fbtp_ctl
;
186 #if defined (__ppc__) || defined (__ppc64__)
187 dtrace_casptr(&tempDTraceIntHook
, NULL
, fbt_perfIntCallback
);
188 if (tempDTraceIntHook
!= (perfCallback
)fbt_perfIntCallback
) {
190 cmn_err(CE_NOTE
, "fbt_enable is failing for probe %s "
191 "in module %s: tempDTraceIntHook already occupied.",
192 fbt
->fbtp_name
, ctl
->mod_modname
);
198 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
199 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
201 cmn_err(CE_NOTE
, "fbt_resume is failing for probe %s "
202 "in module %s: tempDTraceTrapHook already occupied.",
203 fbt
->fbtp_name
, ctl
->mod_modname
);
208 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
)
209 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_patchval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
210 sizeof(fbt
->fbtp_patchval
));
212 dtrace_membar_consumer();
215 #if !defined(__APPLE__)
218 fbt_getargdesc(void *arg
, dtrace_id_t id
, void *parg
, dtrace_argdesc_t
*desc
)
220 fbt_probe_t
*fbt
= parg
;
221 struct modctl
*ctl
= fbt
->fbtp_ctl
;
222 struct module *mp
= ctl
->mod_mp
;
223 ctf_file_t
*fp
= NULL
, *pfp
;
226 ctf_id_t argv
[32], type
;
227 int argc
= sizeof (argv
) / sizeof (ctf_id_t
);
230 if (!ctl
->mod_loaded
|| (ctl
->mod_loadcnt
!= fbt
->fbtp_loadcnt
))
233 if (fbt
->fbtp_roffset
!= 0 && desc
->dtargd_ndx
== 0) {
234 (void) strlcpy(desc
->dtargd_native
, "int",
235 sizeof(desc
->dtargd_native
));
239 if ((fp
= ctf_modopen(mp
, &error
)) == NULL
) {
241 * We have no CTF information for this module -- and therefore
242 * no args[] information.
248 * If we have a parent container, we must manually import it.
250 if ((parent
= ctf_parent_name(fp
)) != NULL
) {
251 struct modctl
*mp
= &modules
;
252 struct modctl
*mod
= NULL
;
255 * We must iterate over all modules to find the module that
259 if (strcmp(mp
->mod_modname
, parent
) == 0) {
263 } while ((mp
= mp
->mod_next
) != &modules
);
268 if ((pfp
= ctf_modopen(mod
->mod_mp
, &error
)) == NULL
) {
272 if (ctf_import(fp
, pfp
) != 0) {
280 if (ctf_func_info(fp
, fbt
->fbtp_symndx
, &f
) == CTF_ERR
)
283 if (fbt
->fbtp_roffset
!= 0) {
284 if (desc
->dtargd_ndx
> 1)
287 ASSERT(desc
->dtargd_ndx
== 1);
290 if (desc
->dtargd_ndx
+ 1 > f
.ctc_argc
)
293 if (ctf_func_args(fp
, fbt
->fbtp_symndx
, argc
, argv
) == CTF_ERR
)
296 type
= argv
[desc
->dtargd_ndx
];
299 if (ctf_type_name(fp
, type
, desc
->dtargd_native
,
300 DTRACE_ARGTYPELEN
) != NULL
) {
308 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
310 #endif /* __APPLE__ */
312 static dtrace_pattr_t fbt_attr
= {
313 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_ISA
},
314 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
315 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
316 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_ISA
},
317 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
320 static dtrace_pops_t fbt_pops
= {
327 #if !defined(__APPLE__)
330 NULL
, /* FIXME: where to look for xnu? */
331 #endif /* __APPLE__ */
338 fbt_cleanup(dev_info_t
*devi
)
340 dtrace_invop_remove(fbt_invop
);
341 ddi_remove_minor_node(devi
, NULL
);
342 kmem_free(fbt_probetab
, fbt_probetab_size
* sizeof (fbt_probe_t
*));
344 fbt_probetab_mask
= 0;
348 fbt_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
354 return (DDI_SUCCESS
);
356 return (DDI_FAILURE
);
359 if (fbt_probetab_size
== 0)
360 fbt_probetab_size
= FBT_PROBETAB_SIZE
;
362 fbt_probetab_mask
= fbt_probetab_size
- 1;
364 kmem_zalloc(fbt_probetab_size
* sizeof (fbt_probe_t
*), KM_SLEEP
);
366 dtrace_invop_add(fbt_invop
);
368 #if !defined(__APPLE__)
369 if (ddi_create_minor_node(devi
, "fbt", S_IFCHR
, 0,
370 DDI_PSEUDO
, NULL
) == DDI_FAILURE
||
371 dtrace_register("fbt", &fbt_attr
, DTRACE_PRIV_KERNEL
, NULL
,
372 &fbt_pops
, NULL
, &fbt_id
) != 0) {
374 return (DDI_FAILURE
);
377 if (ddi_create_minor_node(devi
, "fbt", S_IFCHR
, 0,
378 DDI_PSEUDO
, 0) == DDI_FAILURE
||
379 dtrace_register("fbt", &fbt_attr
, DTRACE_PRIV_KERNEL
, NULL
,
380 &fbt_pops
, NULL
, &fbt_id
) != 0) {
382 return (DDI_FAILURE
);
384 #endif /* __APPLE__ */
386 ddi_report_dev(devi
);
389 return (DDI_SUCCESS
);
392 static d_open_t _fbt_open
;
395 _fbt_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
397 #pragma unused(dev,flags,devtype,p)
401 #define FBT_MAJOR -24 /* let the kernel pick the device number */
404 * A struct describing which functions will get invoked for certain
407 static struct cdevsw fbt_cdevsw
=
409 _fbt_open
, /* open */
410 eno_opcl
, /* close */
411 eno_rdwrt
, /* read */
412 eno_rdwrt
, /* write */
413 eno_ioctl
, /* ioctl */
414 (stop_fcn_t
*)nulldev
, /* stop */
415 (reset_fcn_t
*)nulldev
, /* reset */
417 eno_select
, /* select */
419 eno_strat
, /* strategy */
425 static int gDisableFBT
= 0;
426 struct modctl g_fbt_kernctl
;
427 #undef kmem_alloc /* from its binding to dt_kmem_alloc glue */
428 #undef kmem_free /* from its binding to dt_kmem_free glue */
429 #include <vm/vm_kern.h>
435 PE_parse_boot_argn("DisableFBT", &gDisableFBT
, sizeof (gDisableFBT
));
437 if (0 == gDisableFBT
)
439 int majdevno
= cdevsw_add(FBT_MAJOR
, &fbt_cdevsw
);
440 unsigned long size
= 0, header_size
, round_size
;
445 printf("fbt_init: failed to allocate a major number!\n");
450 * Capture the kernel's mach_header in its entirety and the contents of
451 * its LINKEDIT segment (and only that segment). This is sufficient to
452 * build all the fbt probes lazily the first time a client looks to
453 * the fbt provider. Remeber these on the global struct modctl g_fbt_kernctl.
455 header_size
= sizeof(kernel_mach_header_t
) + _mh_execute_header
.sizeofcmds
;
456 p
= getsegdatafromheader(&_mh_execute_header
, SEG_LINKEDIT
, &size
);
458 round_size
= round_page(header_size
+ size
);
459 /* "q" will accomodate copied kernel_mach_header_t, its load commands, and LINKEIT segment. */
460 ret
= kmem_alloc_pageable(kernel_map
, (vm_offset_t
*)&q
, round_size
);
462 if (p
&& (ret
== KERN_SUCCESS
)) {
463 kernel_segment_command_t
*sgp
;
465 bcopy( (void *)&_mh_execute_header
, q
, header_size
);
466 bcopy( p
, (char *)q
+ header_size
, size
);
468 sgp
= getsegbynamefromheader(q
, SEG_LINKEDIT
);
471 sgp
->vmaddr
= (uintptr_t)((char *)q
+ header_size
);
472 g_fbt_kernctl
.address
= (vm_address_t
)q
;
473 g_fbt_kernctl
.size
= header_size
+ size
;
475 kmem_free(kernel_map
, (vm_offset_t
)q
, round_size
);
476 g_fbt_kernctl
.address
= (vm_address_t
)NULL
;
477 g_fbt_kernctl
.size
= 0;
480 if (ret
== KERN_SUCCESS
)
481 kmem_free(kernel_map
, (vm_offset_t
)q
, round_size
);
482 g_fbt_kernctl
.address
= (vm_address_t
)NULL
;
483 g_fbt_kernctl
.size
= 0;
486 strncpy((char *)&(g_fbt_kernctl
.mod_modname
), "mach_kernel", KMOD_MAX_NAME
);
487 ((char *)&(g_fbt_kernctl
.mod_modname
))[KMOD_MAX_NAME
-1] = '\0';
489 fbt_attach( (dev_info_t
*)(uintptr_t)majdevno
, DDI_ATTACH
);
491 gDisableFBT
= 1; /* Ensure this initialization occurs just one time. */
494 printf("fbt_init: DisableFBT non-zero, no FBT probes will be provided.\n");