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 "@(#)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
*, uintptr_t *, int);
57 extern perfCallback tempDTraceTrapHook
;
58 extern kern_return_t
fbt_perfCallback(int, struct savearea_t
*, uintptr_t *);
60 #define FBT_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
61 #define FBT_PROBETAB_SIZE 0x8000 /* 32k entries -- 128K total */
63 static dev_info_t
*fbt_devi
;
64 static int fbt_probetab_size
;
65 dtrace_provider_id_t fbt_id
;
66 fbt_probe_t
**fbt_probetab
;
67 int fbt_probetab_mask
;
68 static int fbt_verbose
= 0;
70 void fbt_init( void );
74 fbt_destroy(void *arg
, dtrace_id_t id
, void *parg
)
76 #pragma unused(arg,id)
77 fbt_probe_t
*fbt
= parg
, *next
, *hash
, *last
;
82 * Now we need to remove this probe from the fbt_probetab.
84 ndx
= FBT_ADDR2NDX(fbt
->fbtp_patchpoint
);
86 hash
= fbt_probetab
[ndx
];
91 hash
= hash
->fbtp_hashnext
;
95 last
->fbtp_hashnext
= fbt
->fbtp_hashnext
;
97 fbt_probetab
[ndx
] = fbt
->fbtp_hashnext
;
100 next
= fbt
->fbtp_next
;
101 kmem_free(fbt
, sizeof (fbt_probe_t
));
104 } while (fbt
!= NULL
);
109 fbt_enable(void *arg
, dtrace_id_t id
, void *parg
)
111 #pragma unused(arg,id)
112 fbt_probe_t
*fbt
= parg
;
113 struct modctl
*ctl
= NULL
;
115 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
) {
119 if (!ctl
->mod_loaded
) {
121 cmn_err(CE_NOTE
, "fbt is failing for probe %s "
122 "(module %s unloaded)",
123 fbt
->fbtp_name
, ctl
->mod_modname
);
130 * Now check that our modctl has the expected load count. If it
131 * doesn't, this module must have been unloaded and reloaded -- and
132 * we're not going to touch it.
134 if (ctl
->mod_loadcnt
!= fbt
->fbtp_loadcnt
) {
136 cmn_err(CE_NOTE
, "fbt is failing for probe %s "
137 "(module %s reloaded)",
138 fbt
->fbtp_name
, ctl
->mod_modname
);
144 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
145 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
147 cmn_err(CE_NOTE
, "fbt_enable is failing for probe %s "
148 "in module %s: tempDTraceTrapHook already occupied.",
149 fbt
->fbtp_name
, ctl
->mod_modname
);
154 if (fbt
->fbtp_currentval
!= fbt
->fbtp_patchval
) {
155 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_patchval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
156 sizeof(fbt
->fbtp_patchval
));
157 fbt
->fbtp_currentval
= fbt
->fbtp_patchval
;
163 dtrace_membar_consumer();
170 fbt_disable(void *arg
, dtrace_id_t id
, void *parg
)
172 #pragma unused(arg,id)
173 fbt_probe_t
*fbt
= parg
;
174 struct modctl
*ctl
= NULL
;
176 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
) {
179 if (!ctl
->mod_loaded
|| (ctl
->mod_loadcnt
!= fbt
->fbtp_loadcnt
))
182 if (fbt
->fbtp_currentval
!= fbt
->fbtp_savedval
) {
183 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_savedval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
184 sizeof(fbt
->fbtp_savedval
));
185 fbt
->fbtp_currentval
= fbt
->fbtp_savedval
;
186 ASSERT(ctl
->mod_nenabled
> 0);
190 dtrace_membar_consumer();
195 fbt_suspend(void *arg
, dtrace_id_t id
, void *parg
)
197 #pragma unused(arg,id)
198 fbt_probe_t
*fbt
= parg
;
199 struct modctl
*ctl
= NULL
;
201 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
) {
204 ASSERT(ctl
->mod_nenabled
> 0);
205 if (!ctl
->mod_loaded
|| (ctl
->mod_loadcnt
!= fbt
->fbtp_loadcnt
))
208 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_savedval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
209 sizeof(fbt
->fbtp_savedval
));
210 fbt
->fbtp_currentval
= fbt
->fbtp_savedval
;
213 dtrace_membar_consumer();
218 fbt_resume(void *arg
, dtrace_id_t id
, void *parg
)
220 #pragma unused(arg,id)
221 fbt_probe_t
*fbt
= parg
;
222 struct modctl
*ctl
= NULL
;
224 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
) {
227 ASSERT(ctl
->mod_nenabled
> 0);
228 if (!ctl
->mod_loaded
|| (ctl
->mod_loadcnt
!= fbt
->fbtp_loadcnt
))
231 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
232 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
234 cmn_err(CE_NOTE
, "fbt_resume is failing for probe %s "
235 "in module %s: tempDTraceTrapHook already occupied.",
236 fbt
->fbtp_name
, ctl
->mod_modname
);
241 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_patchval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
242 sizeof(fbt
->fbtp_patchval
));
243 fbt
->fbtp_currentval
= fbt
->fbtp_patchval
;
246 dtrace_membar_consumer();
249 #if !defined(__APPLE__)
252 fbt_getargdesc(void *arg
, dtrace_id_t id
, void *parg
, dtrace_argdesc_t
*desc
)
254 fbt_probe_t
*fbt
= parg
;
255 struct modctl
*ctl
= fbt
->fbtp_ctl
;
256 struct module *mp
= ctl
->mod_mp
;
257 ctf_file_t
*fp
= NULL
, *pfp
;
260 ctf_id_t argv
[32], type
;
261 int argc
= sizeof (argv
) / sizeof (ctf_id_t
);
264 if (!ctl
->mod_loaded
|| (ctl
->mod_loadcnt
!= fbt
->fbtp_loadcnt
))
267 if (fbt
->fbtp_roffset
!= 0 && desc
->dtargd_ndx
== 0) {
268 (void) strlcpy(desc
->dtargd_native
, "int",
269 sizeof(desc
->dtargd_native
));
273 if ((fp
= ctf_modopen(mp
, &error
)) == NULL
) {
275 * We have no CTF information for this module -- and therefore
276 * no args[] information.
282 * If we have a parent container, we must manually import it.
284 if ((parent
= ctf_parent_name(fp
)) != NULL
) {
285 struct modctl
*mp
= &modules
;
286 struct modctl
*mod
= NULL
;
289 * We must iterate over all modules to find the module that
293 if (strcmp(mp
->mod_modname
, parent
) == 0) {
297 } while ((mp
= mp
->mod_next
) != &modules
);
302 if ((pfp
= ctf_modopen(mod
->mod_mp
, &error
)) == NULL
) {
306 if (ctf_import(fp
, pfp
) != 0) {
314 if (ctf_func_info(fp
, fbt
->fbtp_symndx
, &f
) == CTF_ERR
)
317 if (fbt
->fbtp_roffset
!= 0) {
318 if (desc
->dtargd_ndx
> 1)
321 ASSERT(desc
->dtargd_ndx
== 1);
324 if (desc
->dtargd_ndx
+ 1 > f
.ctc_argc
)
327 if (ctf_func_args(fp
, fbt
->fbtp_symndx
, argc
, argv
) == CTF_ERR
)
330 type
= argv
[desc
->dtargd_ndx
];
333 if (ctf_type_name(fp
, type
, desc
->dtargd_native
,
334 DTRACE_ARGTYPELEN
) != NULL
) {
342 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
344 #endif /* __APPLE__ */
346 static dtrace_pattr_t fbt_attr
= {
347 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_ISA
},
348 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
349 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
350 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_ISA
},
351 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
354 static dtrace_pops_t fbt_pops
= {
361 #if !defined(__APPLE__)
364 NULL
, /* FIXME: where to look for xnu? */
365 #endif /* __APPLE__ */
372 fbt_cleanup(dev_info_t
*devi
)
374 dtrace_invop_remove(fbt_invop
);
375 ddi_remove_minor_node(devi
, NULL
);
376 kmem_free(fbt_probetab
, fbt_probetab_size
* sizeof (fbt_probe_t
*));
378 fbt_probetab_mask
= 0;
382 fbt_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
388 return (DDI_SUCCESS
);
390 return (DDI_FAILURE
);
393 if (fbt_probetab_size
== 0)
394 fbt_probetab_size
= FBT_PROBETAB_SIZE
;
396 fbt_probetab_mask
= fbt_probetab_size
- 1;
398 kmem_zalloc(fbt_probetab_size
* sizeof (fbt_probe_t
*), KM_SLEEP
);
400 dtrace_invop_add(fbt_invop
);
402 #if !defined(__APPLE__)
403 if (ddi_create_minor_node(devi
, "fbt", S_IFCHR
, 0,
404 DDI_PSEUDO
, NULL
) == DDI_FAILURE
||
405 dtrace_register("fbt", &fbt_attr
, DTRACE_PRIV_KERNEL
, NULL
,
406 &fbt_pops
, NULL
, &fbt_id
) != 0) {
408 return (DDI_FAILURE
);
411 if (ddi_create_minor_node(devi
, "fbt", S_IFCHR
, 0,
412 DDI_PSEUDO
, 0) == DDI_FAILURE
||
413 dtrace_register("fbt", &fbt_attr
, DTRACE_PRIV_KERNEL
, NULL
,
414 &fbt_pops
, NULL
, &fbt_id
) != 0) {
416 return (DDI_FAILURE
);
418 #endif /* __APPLE__ */
420 ddi_report_dev(devi
);
423 return (DDI_SUCCESS
);
426 static d_open_t _fbt_open
;
429 _fbt_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
431 #pragma unused(dev,flags,devtype,p)
435 #define FBT_MAJOR -24 /* let the kernel pick the device number */
438 * A struct describing which functions will get invoked for certain
441 static struct cdevsw fbt_cdevsw
=
443 _fbt_open
, /* open */
444 eno_opcl
, /* close */
445 eno_rdwrt
, /* read */
446 eno_rdwrt
, /* write */
447 eno_ioctl
, /* ioctl */
448 (stop_fcn_t
*)nulldev
, /* stop */
449 (reset_fcn_t
*)nulldev
, /* reset */
451 eno_select
, /* select */
453 eno_strat
, /* strategy */
459 int gIgnoreFBTBlacklist
= 0;
460 static int gFBTInited
= 0;
461 #undef kmem_alloc /* from its binding to dt_kmem_alloc glue */
462 #undef kmem_free /* from its binding to dt_kmem_free glue */
463 #include <vm/vm_kern.h>
470 int majdevno
= cdevsw_add(FBT_MAJOR
, &fbt_cdevsw
);
473 printf("fbt_init: failed to allocate a major number!\n");
477 PE_parse_boot_argn("IgnoreFBTBlacklist", &gIgnoreFBTBlacklist
, sizeof (gIgnoreFBTBlacklist
));
479 fbt_attach( (dev_info_t
*)(uintptr_t)majdevno
, DDI_ATTACH
);
481 gFBTInited
= 1; /* Ensure this initialization occurs just one time. */
484 panic("fbt_init: called twice!\n");