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 */
56 typedef kern_return_t (*perfCallback
)(int, struct savearea_t
*, uintptr_t *, __unused
int);
57 extern perfCallback tempDTraceTrapHook
;
58 extern kern_return_t
fbt_perfCallback(int, struct savearea_t
*, uintptr_t *, __unused
int);
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
));
212 fbt
->fbtp_currentval
= fbt
->fbtp_savedval
;
215 dtrace_membar_consumer();
220 fbt_resume(void *arg
, dtrace_id_t id
, void *parg
)
222 #pragma unused(arg,id)
223 fbt_probe_t
*fbt
= parg
;
224 struct modctl
*ctl
= NULL
;
226 for (; fbt
!= NULL
; fbt
= fbt
->fbtp_next
) {
229 ASSERT(ctl
->mod_nenabled
> 0);
230 if (!ctl
->mod_loaded
|| (ctl
->mod_loadcnt
!= fbt
->fbtp_loadcnt
))
233 dtrace_casptr(&tempDTraceTrapHook
, NULL
, fbt_perfCallback
);
234 if (tempDTraceTrapHook
!= (perfCallback
)fbt_perfCallback
) {
236 cmn_err(CE_NOTE
, "fbt_resume is failing for probe %s "
237 "in module %s: tempDTraceTrapHook already occupied.",
238 fbt
->fbtp_name
, ctl
->mod_modname
);
243 (void)ml_nofault_copy( (vm_offset_t
)&fbt
->fbtp_patchval
, (vm_offset_t
)fbt
->fbtp_patchpoint
,
244 sizeof(fbt
->fbtp_patchval
));
247 fbt
->fbtp_currentval
= fbt
->fbtp_patchval
;
250 dtrace_membar_consumer();
253 #if !defined(__APPLE__)
256 fbt_getargdesc(void *arg
, dtrace_id_t id
, void *parg
, dtrace_argdesc_t
*desc
)
258 fbt_probe_t
*fbt
= parg
;
259 struct modctl
*ctl
= fbt
->fbtp_ctl
;
260 struct module *mp
= ctl
->mod_mp
;
261 ctf_file_t
*fp
= NULL
, *pfp
;
264 ctf_id_t argv
[32], type
;
265 int argc
= sizeof (argv
) / sizeof (ctf_id_t
);
268 if (!ctl
->mod_loaded
|| (ctl
->mod_loadcnt
!= fbt
->fbtp_loadcnt
))
271 if (fbt
->fbtp_roffset
!= 0 && desc
->dtargd_ndx
== 0) {
272 (void) strlcpy(desc
->dtargd_native
, "int",
273 sizeof(desc
->dtargd_native
));
277 if ((fp
= ctf_modopen(mp
, &error
)) == NULL
) {
279 * We have no CTF information for this module -- and therefore
280 * no args[] information.
286 * If we have a parent container, we must manually import it.
288 if ((parent
= ctf_parent_name(fp
)) != NULL
) {
289 struct modctl
*mp
= &modules
;
290 struct modctl
*mod
= NULL
;
293 * We must iterate over all modules to find the module that
297 if (strcmp(mp
->mod_modname
, parent
) == 0) {
301 } while ((mp
= mp
->mod_next
) != &modules
);
306 if ((pfp
= ctf_modopen(mod
->mod_mp
, &error
)) == NULL
) {
310 if (ctf_import(fp
, pfp
) != 0) {
318 if (ctf_func_info(fp
, fbt
->fbtp_symndx
, &f
) == CTF_ERR
)
321 if (fbt
->fbtp_roffset
!= 0) {
322 if (desc
->dtargd_ndx
> 1)
325 ASSERT(desc
->dtargd_ndx
== 1);
328 if (desc
->dtargd_ndx
+ 1 > f
.ctc_argc
)
331 if (ctf_func_args(fp
, fbt
->fbtp_symndx
, argc
, argv
) == CTF_ERR
)
334 type
= argv
[desc
->dtargd_ndx
];
337 if (ctf_type_name(fp
, type
, desc
->dtargd_native
,
338 DTRACE_ARGTYPELEN
) != NULL
) {
346 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
348 #endif /* __APPLE__ */
350 static dtrace_pattr_t fbt_attr
= {
351 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_ISA
},
352 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
353 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
354 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_ISA
},
355 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
358 static dtrace_pops_t fbt_pops
= {
365 #if !defined(__APPLE__)
368 NULL
, /* FIXME: where to look for xnu? */
369 #endif /* __APPLE__ */
376 fbt_cleanup(dev_info_t
*devi
)
378 dtrace_invop_remove(fbt_invop
);
379 ddi_remove_minor_node(devi
, NULL
);
380 kmem_free(fbt_probetab
, fbt_probetab_size
* sizeof (fbt_probe_t
*));
382 fbt_probetab_mask
= 0;
386 fbt_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
392 return (DDI_SUCCESS
);
394 return (DDI_FAILURE
);
397 if (fbt_probetab_size
== 0)
398 fbt_probetab_size
= FBT_PROBETAB_SIZE
;
400 fbt_probetab_mask
= fbt_probetab_size
- 1;
402 kmem_zalloc(fbt_probetab_size
* sizeof (fbt_probe_t
*), KM_SLEEP
);
404 dtrace_invop_add(fbt_invop
);
406 #if !defined(__APPLE__)
407 if (ddi_create_minor_node(devi
, "fbt", S_IFCHR
, 0,
408 DDI_PSEUDO
, NULL
) == DDI_FAILURE
||
409 dtrace_register("fbt", &fbt_attr
, DTRACE_PRIV_KERNEL
, NULL
,
410 &fbt_pops
, NULL
, &fbt_id
) != 0) {
412 return (DDI_FAILURE
);
415 if (ddi_create_minor_node(devi
, "fbt", S_IFCHR
, 0,
416 DDI_PSEUDO
, 0) == DDI_FAILURE
||
417 dtrace_register("fbt", &fbt_attr
, DTRACE_PRIV_KERNEL
, NULL
,
418 &fbt_pops
, NULL
, &fbt_id
) != 0) {
420 return (DDI_FAILURE
);
422 #endif /* __APPLE__ */
424 ddi_report_dev(devi
);
427 return (DDI_SUCCESS
);
430 static d_open_t _fbt_open
;
433 _fbt_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
435 #pragma unused(dev,flags,devtype,p)
439 #define FBT_MAJOR -24 /* let the kernel pick the device number */
442 * A struct describing which functions will get invoked for certain
445 static struct cdevsw fbt_cdevsw
=
447 _fbt_open
, /* open */
448 eno_opcl
, /* close */
449 eno_rdwrt
, /* read */
450 eno_rdwrt
, /* write */
451 eno_ioctl
, /* ioctl */
452 (stop_fcn_t
*)nulldev
, /* stop */
453 (reset_fcn_t
*)nulldev
, /* reset */
455 eno_select
, /* select */
457 eno_strat
, /* strategy */
463 int gIgnoreFBTBlacklist
= 0;
464 static int gFBTInited
= 0;
465 #undef kmem_alloc /* from its binding to dt_kmem_alloc glue */
466 #undef kmem_free /* from its binding to dt_kmem_free glue */
467 #include <vm/vm_kern.h>
474 int majdevno
= cdevsw_add(FBT_MAJOR
, &fbt_cdevsw
);
477 printf("fbt_init: failed to allocate a major number!\n");
481 PE_parse_boot_argn("IgnoreFBTBlacklist", &gIgnoreFBTBlacklist
, sizeof (gIgnoreFBTBlacklist
));
483 fbt_attach( (dev_info_t
*)(uintptr_t)majdevno
, DDI_ATTACH
);
485 gFBTInited
= 1; /* Ensure this initialization occurs just one time. */
488 panic("fbt_init: called twice!\n");