1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * CDDL HEADER START
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * The contents of this file are subject to the terms of the
5*c54f35caSApple OSS Distributions * Common Development and Distribution License (the "License").
6*c54f35caSApple OSS Distributions * You may not use this file except in compliance with the License.
7*c54f35caSApple OSS Distributions *
8*c54f35caSApple OSS Distributions * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*c54f35caSApple OSS Distributions * or http://www.opensolaris.org/os/licensing.
10*c54f35caSApple OSS Distributions * See the License for the specific language governing permissions
11*c54f35caSApple OSS Distributions * and limitations under the License.
12*c54f35caSApple OSS Distributions *
13*c54f35caSApple OSS Distributions * When distributing Covered Code, include this CDDL HEADER in each
14*c54f35caSApple OSS Distributions * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*c54f35caSApple OSS Distributions * If applicable, add the following below this CDDL HEADER, with the
16*c54f35caSApple OSS Distributions * fields enclosed by brackets "[]" replaced with your own identifying
17*c54f35caSApple OSS Distributions * information: Portions Copyright [yyyy] [name of copyright owner]
18*c54f35caSApple OSS Distributions *
19*c54f35caSApple OSS Distributions * CDDL HEADER END
20*c54f35caSApple OSS Distributions */
21*c54f35caSApple OSS Distributions /*
22*c54f35caSApple OSS Distributions * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23*c54f35caSApple OSS Distributions * Use is subject to license terms.
24*c54f35caSApple OSS Distributions */
25*c54f35caSApple OSS Distributions
26*c54f35caSApple OSS Distributions #include <mach-o/loader.h>
27*c54f35caSApple OSS Distributions #include <libkern/kernel_mach_header.h>
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions #include <sys/param.h>
30*c54f35caSApple OSS Distributions #include <sys/systm.h>
31*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
32*c54f35caSApple OSS Distributions #include <sys/errno.h>
33*c54f35caSApple OSS Distributions #include <sys/stat.h>
34*c54f35caSApple OSS Distributions #include <sys/ioctl.h>
35*c54f35caSApple OSS Distributions #include <sys/conf.h>
36*c54f35caSApple OSS Distributions #include <sys/fcntl.h>
37*c54f35caSApple OSS Distributions #include <miscfs/devfs/devfs.h>
38*c54f35caSApple OSS Distributions #include <pexpert/pexpert.h>
39*c54f35caSApple OSS Distributions
40*c54f35caSApple OSS Distributions #include <sys/dtrace.h>
41*c54f35caSApple OSS Distributions #include <sys/dtrace_impl.h>
42*c54f35caSApple OSS Distributions #include <sys/fbt.h>
43*c54f35caSApple OSS Distributions
44*c54f35caSApple OSS Distributions #include <sys/dtrace_glue.h>
45*c54f35caSApple OSS Distributions #include <san/kasan.h>
46*c54f35caSApple OSS Distributions
47*c54f35caSApple OSS Distributions #include <ptrauth.h>
48*c54f35caSApple OSS Distributions
49*c54f35caSApple OSS Distributions /* #include <machine/trap.h> */
50*c54f35caSApple OSS Distributions struct savearea_t; /* Used anonymously */
51*c54f35caSApple OSS Distributions
52*c54f35caSApple OSS Distributions #if defined(__arm64__)
53*c54f35caSApple OSS Distributions typedef kern_return_t (*perfCallback)(int, struct savearea_t *, __unused int, __unused int);
54*c54f35caSApple OSS Distributions extern perfCallback tempDTraceTrapHook;
55*c54f35caSApple OSS Distributions extern kern_return_t fbt_perfCallback(int, struct savearea_t *, __unused int, __unused int);
56*c54f35caSApple OSS Distributions #elif defined(__x86_64__)
57*c54f35caSApple OSS Distributions typedef kern_return_t (*perfCallback)(int, struct savearea_t *, uintptr_t *, __unused int);
58*c54f35caSApple OSS Distributions extern perfCallback tempDTraceTrapHook;
59*c54f35caSApple OSS Distributions extern kern_return_t fbt_perfCallback(int, struct savearea_t *, uintptr_t *, __unused int);
60*c54f35caSApple OSS Distributions #else
61*c54f35caSApple OSS Distributions #error Unknown architecture
62*c54f35caSApple OSS Distributions #endif
63*c54f35caSApple OSS Distributions
64*c54f35caSApple OSS Distributions __private_extern__
65*c54f35caSApple OSS Distributions void
66*c54f35caSApple OSS Distributions qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *));
67*c54f35caSApple OSS Distributions
68*c54f35caSApple OSS Distributions #define FBT_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
69*c54f35caSApple OSS Distributions #define FBT_PROBETAB_SIZE 0x8000 /* 32k entries -- 128K total */
70*c54f35caSApple OSS Distributions
71*c54f35caSApple OSS Distributions static int fbt_probetab_size;
72*c54f35caSApple OSS Distributions dtrace_provider_id_t fbt_id;
73*c54f35caSApple OSS Distributions fbt_probe_t **fbt_probetab;
74*c54f35caSApple OSS Distributions int fbt_probetab_mask;
75*c54f35caSApple OSS Distributions static int fbt_verbose = 0;
76*c54f35caSApple OSS Distributions
77*c54f35caSApple OSS Distributions extern int ignore_fbt_blacklist;
78*c54f35caSApple OSS Distributions
79*c54f35caSApple OSS Distributions extern int dtrace_kernel_symbol_mode;
80*c54f35caSApple OSS Distributions
81*c54f35caSApple OSS Distributions
82*c54f35caSApple OSS Distributions void fbt_init( void );
83*c54f35caSApple OSS Distributions
84*c54f35caSApple OSS Distributions /*ARGSUSED*/
85*c54f35caSApple OSS Distributions static void
fbt_destroy(void * arg,dtrace_id_t id,void * parg)86*c54f35caSApple OSS Distributions fbt_destroy(void *arg, dtrace_id_t id, void *parg)
87*c54f35caSApple OSS Distributions {
88*c54f35caSApple OSS Distributions #pragma unused(arg,id)
89*c54f35caSApple OSS Distributions fbt_probe_t *fbt = parg, *next, *hash, *last;
90*c54f35caSApple OSS Distributions int ndx;
91*c54f35caSApple OSS Distributions
92*c54f35caSApple OSS Distributions do {
93*c54f35caSApple OSS Distributions /*
94*c54f35caSApple OSS Distributions * Now we need to remove this probe from the fbt_probetab.
95*c54f35caSApple OSS Distributions */
96*c54f35caSApple OSS Distributions ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
97*c54f35caSApple OSS Distributions last = NULL;
98*c54f35caSApple OSS Distributions hash = fbt_probetab[ndx];
99*c54f35caSApple OSS Distributions
100*c54f35caSApple OSS Distributions while (hash != fbt) {
101*c54f35caSApple OSS Distributions ASSERT(hash != NULL);
102*c54f35caSApple OSS Distributions last = hash;
103*c54f35caSApple OSS Distributions hash = hash->fbtp_hashnext;
104*c54f35caSApple OSS Distributions }
105*c54f35caSApple OSS Distributions
106*c54f35caSApple OSS Distributions if (last != NULL) {
107*c54f35caSApple OSS Distributions last->fbtp_hashnext = fbt->fbtp_hashnext;
108*c54f35caSApple OSS Distributions } else {
109*c54f35caSApple OSS Distributions fbt_probetab[ndx] = fbt->fbtp_hashnext;
110*c54f35caSApple OSS Distributions }
111*c54f35caSApple OSS Distributions
112*c54f35caSApple OSS Distributions next = fbt->fbtp_next;
113*c54f35caSApple OSS Distributions kmem_free(fbt, sizeof(fbt_probe_t));
114*c54f35caSApple OSS Distributions
115*c54f35caSApple OSS Distributions fbt = next;
116*c54f35caSApple OSS Distributions } while (fbt != NULL);
117*c54f35caSApple OSS Distributions }
118*c54f35caSApple OSS Distributions
119*c54f35caSApple OSS Distributions /*ARGSUSED*/
120*c54f35caSApple OSS Distributions int
fbt_enable(void * arg,dtrace_id_t id,void * parg)121*c54f35caSApple OSS Distributions fbt_enable(void *arg, dtrace_id_t id, void *parg)
122*c54f35caSApple OSS Distributions {
123*c54f35caSApple OSS Distributions #pragma unused(arg,id)
124*c54f35caSApple OSS Distributions fbt_probe_t *fbt = parg;
125*c54f35caSApple OSS Distributions struct modctl *ctl = NULL;
126*c54f35caSApple OSS Distributions
127*c54f35caSApple OSS Distributions for (; fbt != NULL; fbt = fbt->fbtp_next) {
128*c54f35caSApple OSS Distributions ctl = fbt->fbtp_ctl;
129*c54f35caSApple OSS Distributions
130*c54f35caSApple OSS Distributions if (!ctl->mod_loaded) {
131*c54f35caSApple OSS Distributions if (fbt_verbose) {
132*c54f35caSApple OSS Distributions cmn_err(CE_NOTE, "fbt is failing for probe %s "
133*c54f35caSApple OSS Distributions "(module %s unloaded)",
134*c54f35caSApple OSS Distributions fbt->fbtp_name, ctl->mod_modname);
135*c54f35caSApple OSS Distributions }
136*c54f35caSApple OSS Distributions
137*c54f35caSApple OSS Distributions continue;
138*c54f35caSApple OSS Distributions }
139*c54f35caSApple OSS Distributions
140*c54f35caSApple OSS Distributions /*
141*c54f35caSApple OSS Distributions * Now check that our modctl has the expected load count. If it
142*c54f35caSApple OSS Distributions * doesn't, this module must have been unloaded and reloaded -- and
143*c54f35caSApple OSS Distributions * we're not going to touch it.
144*c54f35caSApple OSS Distributions */
145*c54f35caSApple OSS Distributions if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) {
146*c54f35caSApple OSS Distributions if (fbt_verbose) {
147*c54f35caSApple OSS Distributions cmn_err(CE_NOTE, "fbt is failing for probe %s "
148*c54f35caSApple OSS Distributions "(module %s reloaded)",
149*c54f35caSApple OSS Distributions fbt->fbtp_name, ctl->mod_modname);
150*c54f35caSApple OSS Distributions }
151*c54f35caSApple OSS Distributions
152*c54f35caSApple OSS Distributions continue;
153*c54f35caSApple OSS Distributions }
154*c54f35caSApple OSS Distributions
155*c54f35caSApple OSS Distributions dtrace_casptr(&tempDTraceTrapHook, NULL, ptrauth_nop_cast(void *, &fbt_perfCallback));
156*c54f35caSApple OSS Distributions if (tempDTraceTrapHook != (perfCallback)fbt_perfCallback) {
157*c54f35caSApple OSS Distributions if (fbt_verbose) {
158*c54f35caSApple OSS Distributions cmn_err(CE_NOTE, "fbt_enable is failing for probe %s "
159*c54f35caSApple OSS Distributions "in module %s: tempDTraceTrapHook already occupied.",
160*c54f35caSApple OSS Distributions fbt->fbtp_name, ctl->mod_modname);
161*c54f35caSApple OSS Distributions }
162*c54f35caSApple OSS Distributions continue;
163*c54f35caSApple OSS Distributions }
164*c54f35caSApple OSS Distributions
165*c54f35caSApple OSS Distributions if (fbt->fbtp_currentval != fbt->fbtp_patchval) {
166*c54f35caSApple OSS Distributions #if KASAN_CLASSIC
167*c54f35caSApple OSS Distributions /* Since dtrace probes can call into KASan and vice versa, things can get
168*c54f35caSApple OSS Distributions * very slow if we have a lot of probes. This call will disable the KASan
169*c54f35caSApple OSS Distributions * fakestack after a threshold of probes is reached. */
170*c54f35caSApple OSS Distributions kasan_fakestack_suspend();
171*c54f35caSApple OSS Distributions #endif /* KASAN_CLASSIC */
172*c54f35caSApple OSS Distributions
173*c54f35caSApple OSS Distributions (void)ml_nofault_copy((vm_offset_t)&fbt->fbtp_patchval, (vm_offset_t)fbt->fbtp_patchpoint,
174*c54f35caSApple OSS Distributions sizeof(fbt->fbtp_patchval));
175*c54f35caSApple OSS Distributions /*
176*c54f35caSApple OSS Distributions * Make the patched instruction visible via a data + instruction
177*c54f35caSApple OSS Distributions * cache flush for the platforms that need it
178*c54f35caSApple OSS Distributions */
179*c54f35caSApple OSS Distributions flush_dcache((vm_offset_t)fbt->fbtp_patchpoint, (vm_size_t)sizeof(fbt->fbtp_patchval), 0);
180*c54f35caSApple OSS Distributions invalidate_icache((vm_offset_t)fbt->fbtp_patchpoint, (vm_size_t)sizeof(fbt->fbtp_patchval), 0);
181*c54f35caSApple OSS Distributions fbt->fbtp_currentval = fbt->fbtp_patchval;
182*c54f35caSApple OSS Distributions
183*c54f35caSApple OSS Distributions ctl->mod_nenabled++;
184*c54f35caSApple OSS Distributions }
185*c54f35caSApple OSS Distributions }
186*c54f35caSApple OSS Distributions
187*c54f35caSApple OSS Distributions dtrace_membar_consumer();
188*c54f35caSApple OSS Distributions
189*c54f35caSApple OSS Distributions return 0;
190*c54f35caSApple OSS Distributions }
191*c54f35caSApple OSS Distributions
192*c54f35caSApple OSS Distributions /*ARGSUSED*/
193*c54f35caSApple OSS Distributions static void
fbt_disable(void * arg,dtrace_id_t id,void * parg)194*c54f35caSApple OSS Distributions fbt_disable(void *arg, dtrace_id_t id, void *parg)
195*c54f35caSApple OSS Distributions {
196*c54f35caSApple OSS Distributions #pragma unused(arg,id)
197*c54f35caSApple OSS Distributions fbt_probe_t *fbt = parg;
198*c54f35caSApple OSS Distributions struct modctl *ctl = NULL;
199*c54f35caSApple OSS Distributions
200*c54f35caSApple OSS Distributions for (; fbt != NULL; fbt = fbt->fbtp_next) {
201*c54f35caSApple OSS Distributions ctl = fbt->fbtp_ctl;
202*c54f35caSApple OSS Distributions
203*c54f35caSApple OSS Distributions if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt)) {
204*c54f35caSApple OSS Distributions continue;
205*c54f35caSApple OSS Distributions }
206*c54f35caSApple OSS Distributions
207*c54f35caSApple OSS Distributions if (fbt->fbtp_currentval != fbt->fbtp_savedval) {
208*c54f35caSApple OSS Distributions (void)ml_nofault_copy((vm_offset_t)&fbt->fbtp_savedval, (vm_offset_t)fbt->fbtp_patchpoint,
209*c54f35caSApple OSS Distributions sizeof(fbt->fbtp_savedval));
210*c54f35caSApple OSS Distributions /*
211*c54f35caSApple OSS Distributions * Make the patched instruction visible via a data + instruction
212*c54f35caSApple OSS Distributions * cache flush for the platforms that need it
213*c54f35caSApple OSS Distributions */
214*c54f35caSApple OSS Distributions flush_dcache((vm_offset_t)fbt->fbtp_patchpoint, (vm_size_t)sizeof(fbt->fbtp_patchval), 0);
215*c54f35caSApple OSS Distributions invalidate_icache((vm_offset_t)fbt->fbtp_patchpoint, (vm_size_t)sizeof(fbt->fbtp_patchval), 0);
216*c54f35caSApple OSS Distributions
217*c54f35caSApple OSS Distributions fbt->fbtp_currentval = fbt->fbtp_savedval;
218*c54f35caSApple OSS Distributions ASSERT(ctl->mod_nenabled > 0);
219*c54f35caSApple OSS Distributions ctl->mod_nenabled--;
220*c54f35caSApple OSS Distributions
221*c54f35caSApple OSS Distributions #if KASAN && KASAN_CLASSIC
222*c54f35caSApple OSS Distributions kasan_fakestack_resume();
223*c54f35caSApple OSS Distributions #endif /* KASAN && KASAN_CLASSIC */
224*c54f35caSApple OSS Distributions }
225*c54f35caSApple OSS Distributions }
226*c54f35caSApple OSS Distributions dtrace_membar_consumer();
227*c54f35caSApple OSS Distributions }
228*c54f35caSApple OSS Distributions
229*c54f35caSApple OSS Distributions /*ARGSUSED*/
230*c54f35caSApple OSS Distributions static void
fbt_suspend(void * arg,dtrace_id_t id,void * parg)231*c54f35caSApple OSS Distributions fbt_suspend(void *arg, dtrace_id_t id, void *parg)
232*c54f35caSApple OSS Distributions {
233*c54f35caSApple OSS Distributions #pragma unused(arg,id)
234*c54f35caSApple OSS Distributions fbt_probe_t *fbt = parg;
235*c54f35caSApple OSS Distributions struct modctl *ctl = NULL;
236*c54f35caSApple OSS Distributions
237*c54f35caSApple OSS Distributions for (; fbt != NULL; fbt = fbt->fbtp_next) {
238*c54f35caSApple OSS Distributions ctl = fbt->fbtp_ctl;
239*c54f35caSApple OSS Distributions
240*c54f35caSApple OSS Distributions ASSERT(ctl->mod_nenabled > 0);
241*c54f35caSApple OSS Distributions if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt)) {
242*c54f35caSApple OSS Distributions continue;
243*c54f35caSApple OSS Distributions }
244*c54f35caSApple OSS Distributions
245*c54f35caSApple OSS Distributions (void)ml_nofault_copy((vm_offset_t)&fbt->fbtp_savedval, (vm_offset_t)fbt->fbtp_patchpoint,
246*c54f35caSApple OSS Distributions sizeof(fbt->fbtp_savedval));
247*c54f35caSApple OSS Distributions
248*c54f35caSApple OSS Distributions /*
249*c54f35caSApple OSS Distributions * Make the patched instruction visible via a data + instruction
250*c54f35caSApple OSS Distributions * cache flush for the platforms that need it
251*c54f35caSApple OSS Distributions */
252*c54f35caSApple OSS Distributions flush_dcache((vm_offset_t)fbt->fbtp_patchpoint, (vm_size_t)sizeof(fbt->fbtp_savedval), 0);
253*c54f35caSApple OSS Distributions invalidate_icache((vm_offset_t)fbt->fbtp_patchpoint, (vm_size_t)sizeof(fbt->fbtp_savedval), 0);
254*c54f35caSApple OSS Distributions
255*c54f35caSApple OSS Distributions fbt->fbtp_currentval = fbt->fbtp_savedval;
256*c54f35caSApple OSS Distributions }
257*c54f35caSApple OSS Distributions
258*c54f35caSApple OSS Distributions dtrace_membar_consumer();
259*c54f35caSApple OSS Distributions }
260*c54f35caSApple OSS Distributions
261*c54f35caSApple OSS Distributions /*ARGSUSED*/
262*c54f35caSApple OSS Distributions static void
fbt_resume(void * arg,dtrace_id_t id,void * parg)263*c54f35caSApple OSS Distributions fbt_resume(void *arg, dtrace_id_t id, void *parg)
264*c54f35caSApple OSS Distributions {
265*c54f35caSApple OSS Distributions #pragma unused(arg,id)
266*c54f35caSApple OSS Distributions fbt_probe_t *fbt = parg;
267*c54f35caSApple OSS Distributions struct modctl *ctl = NULL;
268*c54f35caSApple OSS Distributions
269*c54f35caSApple OSS Distributions for (; fbt != NULL; fbt = fbt->fbtp_next) {
270*c54f35caSApple OSS Distributions ctl = fbt->fbtp_ctl;
271*c54f35caSApple OSS Distributions
272*c54f35caSApple OSS Distributions ASSERT(ctl->mod_nenabled > 0);
273*c54f35caSApple OSS Distributions if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt)) {
274*c54f35caSApple OSS Distributions continue;
275*c54f35caSApple OSS Distributions }
276*c54f35caSApple OSS Distributions
277*c54f35caSApple OSS Distributions dtrace_casptr(&tempDTraceTrapHook, NULL, ptrauth_nop_cast(void *, &fbt_perfCallback));
278*c54f35caSApple OSS Distributions if (tempDTraceTrapHook != (perfCallback)fbt_perfCallback) {
279*c54f35caSApple OSS Distributions if (fbt_verbose) {
280*c54f35caSApple OSS Distributions cmn_err(CE_NOTE, "fbt_resume is failing for probe %s "
281*c54f35caSApple OSS Distributions "in module %s: tempDTraceTrapHook already occupied.",
282*c54f35caSApple OSS Distributions fbt->fbtp_name, ctl->mod_modname);
283*c54f35caSApple OSS Distributions }
284*c54f35caSApple OSS Distributions return;
285*c54f35caSApple OSS Distributions }
286*c54f35caSApple OSS Distributions
287*c54f35caSApple OSS Distributions (void)ml_nofault_copy((vm_offset_t)&fbt->fbtp_patchval, (vm_offset_t)fbt->fbtp_patchpoint,
288*c54f35caSApple OSS Distributions sizeof(fbt->fbtp_patchval));
289*c54f35caSApple OSS Distributions
290*c54f35caSApple OSS Distributions /*
291*c54f35caSApple OSS Distributions * Make the patched instruction visible via a data + instruction cache flush.
292*c54f35caSApple OSS Distributions */
293*c54f35caSApple OSS Distributions flush_dcache((vm_offset_t)fbt->fbtp_patchpoint, (vm_size_t)sizeof(fbt->fbtp_patchval), 0);
294*c54f35caSApple OSS Distributions invalidate_icache((vm_offset_t)fbt->fbtp_patchpoint, (vm_size_t)sizeof(fbt->fbtp_patchval), 0);
295*c54f35caSApple OSS Distributions
296*c54f35caSApple OSS Distributions fbt->fbtp_currentval = fbt->fbtp_patchval;
297*c54f35caSApple OSS Distributions }
298*c54f35caSApple OSS Distributions
299*c54f35caSApple OSS Distributions dtrace_membar_consumer();
300*c54f35caSApple OSS Distributions }
301*c54f35caSApple OSS Distributions
302*c54f35caSApple OSS Distributions static void
fbt_provide_module_user_syms(struct modctl * ctl)303*c54f35caSApple OSS Distributions fbt_provide_module_user_syms(struct modctl *ctl)
304*c54f35caSApple OSS Distributions {
305*c54f35caSApple OSS Distributions unsigned int i;
306*c54f35caSApple OSS Distributions char *modname = ctl->mod_modname;
307*c54f35caSApple OSS Distributions
308*c54f35caSApple OSS Distributions dtrace_module_symbols_t* module_symbols = ctl->mod_user_symbols;
309*c54f35caSApple OSS Distributions if (module_symbols) {
310*c54f35caSApple OSS Distributions for (i = 0; i < module_symbols->dtmodsyms_count; i++) {
311*c54f35caSApple OSS Distributions /*
312*c54f35caSApple OSS Distributions * symbol->dtsym_addr (the symbol address) passed in from
313*c54f35caSApple OSS Distributions * user space, is already slid for both kexts and kernel.
314*c54f35caSApple OSS Distributions */
315*c54f35caSApple OSS Distributions dtrace_symbol_t* symbol = &module_symbols->dtmodsyms_symbols[i];
316*c54f35caSApple OSS Distributions
317*c54f35caSApple OSS Distributions char* name = symbol->dtsym_name;
318*c54f35caSApple OSS Distributions
319*c54f35caSApple OSS Distributions /* Lop off omnipresent leading underscore. */
320*c54f35caSApple OSS Distributions if (*name == '_') {
321*c54f35caSApple OSS Distributions name += 1;
322*c54f35caSApple OSS Distributions }
323*c54f35caSApple OSS Distributions
324*c54f35caSApple OSS Distributions if (fbt_excluded(name)) {
325*c54f35caSApple OSS Distributions continue;
326*c54f35caSApple OSS Distributions }
327*c54f35caSApple OSS Distributions
328*c54f35caSApple OSS Distributions /*
329*c54f35caSApple OSS Distributions * Ignore symbols with a null address
330*c54f35caSApple OSS Distributions */
331*c54f35caSApple OSS Distributions if (!symbol->dtsym_addr) {
332*c54f35caSApple OSS Distributions continue;
333*c54f35caSApple OSS Distributions }
334*c54f35caSApple OSS Distributions
335*c54f35caSApple OSS Distributions /*
336*c54f35caSApple OSS Distributions * Ignore symbols not part of this module
337*c54f35caSApple OSS Distributions */
338*c54f35caSApple OSS Distributions if (!dtrace_addr_in_module((void*)symbol->dtsym_addr, ctl)) {
339*c54f35caSApple OSS Distributions continue;
340*c54f35caSApple OSS Distributions }
341*c54f35caSApple OSS Distributions
342*c54f35caSApple OSS Distributions fbt_provide_probe(ctl, modname, name, (machine_inst_t*)(uintptr_t)symbol->dtsym_addr, (machine_inst_t*)(uintptr_t)(symbol->dtsym_addr + symbol->dtsym_size));
343*c54f35caSApple OSS Distributions }
344*c54f35caSApple OSS Distributions }
345*c54f35caSApple OSS Distributions }
346*c54f35caSApple OSS Distributions static void
fbt_provide_kernel_section(struct modctl * ctl,kernel_section_t * sect,kernel_nlist_t * sym,uint32_t nsyms,const char * strings)347*c54f35caSApple OSS Distributions fbt_provide_kernel_section(struct modctl *ctl, kernel_section_t *sect, kernel_nlist_t *sym, uint32_t nsyms, const char *strings)
348*c54f35caSApple OSS Distributions {
349*c54f35caSApple OSS Distributions uintptr_t sect_start = (uintptr_t)sect->addr;
350*c54f35caSApple OSS Distributions uintptr_t sect_end = (uintptr_t)sect->size + sect->addr;
351*c54f35caSApple OSS Distributions unsigned int i;
352*c54f35caSApple OSS Distributions
353*c54f35caSApple OSS Distributions if ((sect->flags & S_ATTR_PURE_INSTRUCTIONS) != S_ATTR_PURE_INSTRUCTIONS) {
354*c54f35caSApple OSS Distributions return;
355*c54f35caSApple OSS Distributions }
356*c54f35caSApple OSS Distributions
357*c54f35caSApple OSS Distributions for (i = 0; i < nsyms; i++) {
358*c54f35caSApple OSS Distributions uint8_t n_type = sym[i].n_type & (N_TYPE | N_EXT);
359*c54f35caSApple OSS Distributions const char *name = strings + sym[i].n_un.n_strx;
360*c54f35caSApple OSS Distributions uint64_t limit;
361*c54f35caSApple OSS Distributions
362*c54f35caSApple OSS Distributions if (sym[i].n_value < sect_start || sym[i].n_value > sect_end) {
363*c54f35caSApple OSS Distributions continue;
364*c54f35caSApple OSS Distributions }
365*c54f35caSApple OSS Distributions
366*c54f35caSApple OSS Distributions /* Check that the symbol is a global and that it has a name. */
367*c54f35caSApple OSS Distributions if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type)) {
368*c54f35caSApple OSS Distributions continue;
369*c54f35caSApple OSS Distributions }
370*c54f35caSApple OSS Distributions
371*c54f35caSApple OSS Distributions if (0 == sym[i].n_un.n_strx) { /* iff a null, "", name. */
372*c54f35caSApple OSS Distributions continue;
373*c54f35caSApple OSS Distributions }
374*c54f35caSApple OSS Distributions
375*c54f35caSApple OSS Distributions /* Lop off omnipresent leading underscore. */
376*c54f35caSApple OSS Distributions if (*name == '_') {
377*c54f35caSApple OSS Distributions name += 1;
378*c54f35caSApple OSS Distributions }
379*c54f35caSApple OSS Distributions
380*c54f35caSApple OSS Distributions if (fbt_excluded(name)) {
381*c54f35caSApple OSS Distributions continue;
382*c54f35caSApple OSS Distributions }
383*c54f35caSApple OSS Distributions
384*c54f35caSApple OSS Distributions /*
385*c54f35caSApple OSS Distributions * Find the function boundary by looking at either the
386*c54f35caSApple OSS Distributions * end of the section or the beginning of the next symbol
387*c54f35caSApple OSS Distributions */
388*c54f35caSApple OSS Distributions if (i == nsyms - 1) {
389*c54f35caSApple OSS Distributions limit = sect_end;
390*c54f35caSApple OSS Distributions } else {
391*c54f35caSApple OSS Distributions limit = sym[i + 1].n_value;
392*c54f35caSApple OSS Distributions }
393*c54f35caSApple OSS Distributions
394*c54f35caSApple OSS Distributions fbt_provide_probe(ctl, ctl->mod_modname, name, (machine_inst_t*)sym[i].n_value, (machine_inst_t*)limit);
395*c54f35caSApple OSS Distributions }
396*c54f35caSApple OSS Distributions }
397*c54f35caSApple OSS Distributions
398*c54f35caSApple OSS Distributions static int
fbt_sym_cmp(const void * ap,const void * bp)399*c54f35caSApple OSS Distributions fbt_sym_cmp(const void *ap, const void *bp)
400*c54f35caSApple OSS Distributions {
401*c54f35caSApple OSS Distributions return (int)(((const kernel_nlist_t*)ap)->n_value - ((const kernel_nlist_t*)bp)->n_value);
402*c54f35caSApple OSS Distributions }
403*c54f35caSApple OSS Distributions
404*c54f35caSApple OSS Distributions static void
fbt_provide_module_kernel_syms(struct modctl * ctl)405*c54f35caSApple OSS Distributions fbt_provide_module_kernel_syms(struct modctl *ctl)
406*c54f35caSApple OSS Distributions {
407*c54f35caSApple OSS Distributions kernel_mach_header_t *mh = (kernel_mach_header_t *)(ctl->mod_address);
408*c54f35caSApple OSS Distributions kernel_segment_command_t *seg;
409*c54f35caSApple OSS Distributions struct load_command *cmd;
410*c54f35caSApple OSS Distributions kernel_segment_command_t *linkedit = NULL;
411*c54f35caSApple OSS Distributions struct symtab_command *symtab = NULL;
412*c54f35caSApple OSS Distributions kernel_nlist_t *syms = NULL, *sorted_syms = NULL;
413*c54f35caSApple OSS Distributions const char *strings;
414*c54f35caSApple OSS Distributions unsigned int i;
415*c54f35caSApple OSS Distributions size_t symlen;
416*c54f35caSApple OSS Distributions
417*c54f35caSApple OSS Distributions if (mh->magic != MH_MAGIC_KERNEL) {
418*c54f35caSApple OSS Distributions return;
419*c54f35caSApple OSS Distributions }
420*c54f35caSApple OSS Distributions
421*c54f35caSApple OSS Distributions cmd = (struct load_command *) &mh[1];
422*c54f35caSApple OSS Distributions for (i = 0; i < mh->ncmds; i++) {
423*c54f35caSApple OSS Distributions if (cmd->cmd == LC_SEGMENT_KERNEL) {
424*c54f35caSApple OSS Distributions kernel_segment_command_t *orig_sg = (kernel_segment_command_t *) cmd;
425*c54f35caSApple OSS Distributions if (LIT_STRNEQL(orig_sg->segname, SEG_LINKEDIT)) {
426*c54f35caSApple OSS Distributions linkedit = orig_sg;
427*c54f35caSApple OSS Distributions }
428*c54f35caSApple OSS Distributions } else if (cmd->cmd == LC_SYMTAB) {
429*c54f35caSApple OSS Distributions symtab = (struct symtab_command *) cmd;
430*c54f35caSApple OSS Distributions }
431*c54f35caSApple OSS Distributions if (symtab && linkedit) {
432*c54f35caSApple OSS Distributions break;
433*c54f35caSApple OSS Distributions }
434*c54f35caSApple OSS Distributions cmd = (struct load_command *) ((caddr_t) cmd + cmd->cmdsize);
435*c54f35caSApple OSS Distributions }
436*c54f35caSApple OSS Distributions
437*c54f35caSApple OSS Distributions if ((symtab == NULL) || (linkedit == NULL)) {
438*c54f35caSApple OSS Distributions return;
439*c54f35caSApple OSS Distributions }
440*c54f35caSApple OSS Distributions
441*c54f35caSApple OSS Distributions syms = (kernel_nlist_t *)(linkedit->vmaddr + symtab->symoff - linkedit->fileoff);
442*c54f35caSApple OSS Distributions strings = (const char *)(linkedit->vmaddr + symtab->stroff - linkedit->fileoff);
443*c54f35caSApple OSS Distributions
444*c54f35caSApple OSS Distributions /*
445*c54f35caSApple OSS Distributions * Make a copy of the symbol table and sort it to not cross into the next function
446*c54f35caSApple OSS Distributions * when disassembling the function
447*c54f35caSApple OSS Distributions */
448*c54f35caSApple OSS Distributions symlen = sizeof(kernel_nlist_t) * symtab->nsyms;
449*c54f35caSApple OSS Distributions sorted_syms = kmem_alloc(symlen, KM_SLEEP);
450*c54f35caSApple OSS Distributions bcopy(syms, sorted_syms, symlen);
451*c54f35caSApple OSS Distributions qsort(sorted_syms, symtab->nsyms, sizeof(kernel_nlist_t), fbt_sym_cmp);
452*c54f35caSApple OSS Distributions
453*c54f35caSApple OSS Distributions for (seg = firstsegfromheader(mh); seg != NULL; seg = nextsegfromheader(mh, seg)) {
454*c54f35caSApple OSS Distributions kernel_section_t *sect = firstsect(seg);
455*c54f35caSApple OSS Distributions
456*c54f35caSApple OSS Distributions if (strcmp(seg->segname, "__KLD") == 0 || strcmp(seg->segname, "__KLDDATA") == 0) {
457*c54f35caSApple OSS Distributions continue;
458*c54f35caSApple OSS Distributions }
459*c54f35caSApple OSS Distributions
460*c54f35caSApple OSS Distributions for (sect = firstsect(seg); sect != NULL; sect = nextsect(seg, sect)) {
461*c54f35caSApple OSS Distributions fbt_provide_kernel_section(ctl, sect, sorted_syms, symtab->nsyms, strings);
462*c54f35caSApple OSS Distributions }
463*c54f35caSApple OSS Distributions }
464*c54f35caSApple OSS Distributions
465*c54f35caSApple OSS Distributions kmem_free(sorted_syms, symlen);
466*c54f35caSApple OSS Distributions }
467*c54f35caSApple OSS Distributions
468*c54f35caSApple OSS Distributions void
fbt_provide_module(void * arg,struct modctl * ctl)469*c54f35caSApple OSS Distributions fbt_provide_module(void *arg, struct modctl *ctl)
470*c54f35caSApple OSS Distributions {
471*c54f35caSApple OSS Distributions #pragma unused(arg)
472*c54f35caSApple OSS Distributions ASSERT(ctl != NULL);
473*c54f35caSApple OSS Distributions ASSERT(dtrace_kernel_symbol_mode != DTRACE_KERNEL_SYMBOLS_NEVER);
474*c54f35caSApple OSS Distributions LCK_MTX_ASSERT(&mod_lock, LCK_MTX_ASSERT_OWNED);
475*c54f35caSApple OSS Distributions
476*c54f35caSApple OSS Distributions if (dtrace_fbt_probes_restricted()) {
477*c54f35caSApple OSS Distributions return;
478*c54f35caSApple OSS Distributions }
479*c54f35caSApple OSS Distributions
480*c54f35caSApple OSS Distributions // Update the "ignore blacklist" bit
481*c54f35caSApple OSS Distributions if (ignore_fbt_blacklist) {
482*c54f35caSApple OSS Distributions ctl->mod_flags |= MODCTL_FBT_PROVIDE_BLACKLISTED_PROBES;
483*c54f35caSApple OSS Distributions }
484*c54f35caSApple OSS Distributions
485*c54f35caSApple OSS Distributions if (MOD_FBT_DONE(ctl)) {
486*c54f35caSApple OSS Distributions return;
487*c54f35caSApple OSS Distributions }
488*c54f35caSApple OSS Distributions
489*c54f35caSApple OSS Distributions if (fbt_module_excluded(ctl)) {
490*c54f35caSApple OSS Distributions ctl->mod_flags |= MODCTL_FBT_INVALID;
491*c54f35caSApple OSS Distributions return;
492*c54f35caSApple OSS Distributions }
493*c54f35caSApple OSS Distributions
494*c54f35caSApple OSS Distributions if (MOD_HAS_KERNEL_SYMBOLS(ctl)) {
495*c54f35caSApple OSS Distributions fbt_provide_module_kernel_syms(ctl);
496*c54f35caSApple OSS Distributions ctl->mod_flags |= MODCTL_FBT_PROBES_PROVIDED;
497*c54f35caSApple OSS Distributions if (MOD_FBT_PROVIDE_BLACKLISTED_PROBES(ctl)) {
498*c54f35caSApple OSS Distributions ctl->mod_flags |= MODCTL_FBT_BLACKLISTED_PROBES_PROVIDED;
499*c54f35caSApple OSS Distributions }
500*c54f35caSApple OSS Distributions return;
501*c54f35caSApple OSS Distributions }
502*c54f35caSApple OSS Distributions
503*c54f35caSApple OSS Distributions if (MOD_HAS_USERSPACE_SYMBOLS(ctl)) {
504*c54f35caSApple OSS Distributions fbt_provide_module_user_syms(ctl);
505*c54f35caSApple OSS Distributions ctl->mod_flags |= MODCTL_FBT_PROBES_PROVIDED;
506*c54f35caSApple OSS Distributions if (MOD_FBT_PROVIDE_BLACKLISTED_PROBES(ctl)) {
507*c54f35caSApple OSS Distributions ctl->mod_flags |= MODCTL_FBT_BLACKLISTED_PROBES_PROVIDED;
508*c54f35caSApple OSS Distributions }
509*c54f35caSApple OSS Distributions return;
510*c54f35caSApple OSS Distributions }
511*c54f35caSApple OSS Distributions }
512*c54f35caSApple OSS Distributions
513*c54f35caSApple OSS Distributions static dtrace_pattr_t fbt_attr = {
514*c54f35caSApple OSS Distributions { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
515*c54f35caSApple OSS Distributions { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
516*c54f35caSApple OSS Distributions { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
517*c54f35caSApple OSS Distributions { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
518*c54f35caSApple OSS Distributions { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
519*c54f35caSApple OSS Distributions };
520*c54f35caSApple OSS Distributions
521*c54f35caSApple OSS Distributions static dtrace_pops_t fbt_pops = {
522*c54f35caSApple OSS Distributions .dtps_provide = NULL,
523*c54f35caSApple OSS Distributions .dtps_provide_module = fbt_provide_module,
524*c54f35caSApple OSS Distributions .dtps_enable = fbt_enable,
525*c54f35caSApple OSS Distributions .dtps_disable = fbt_disable,
526*c54f35caSApple OSS Distributions .dtps_suspend = fbt_suspend,
527*c54f35caSApple OSS Distributions .dtps_resume = fbt_resume,
528*c54f35caSApple OSS Distributions .dtps_getargdesc = NULL, /* APPLE NOTE: fbt_getargdesc implemented in userspace */
529*c54f35caSApple OSS Distributions .dtps_getargval = NULL,
530*c54f35caSApple OSS Distributions .dtps_usermode = NULL,
531*c54f35caSApple OSS Distributions .dtps_destroy = fbt_destroy
532*c54f35caSApple OSS Distributions };
533*c54f35caSApple OSS Distributions
534*c54f35caSApple OSS Distributions static void
fbt_cleanup(dev_info_t * devi)535*c54f35caSApple OSS Distributions fbt_cleanup(dev_info_t *devi)
536*c54f35caSApple OSS Distributions {
537*c54f35caSApple OSS Distributions dtrace_invop_remove(fbt_invop);
538*c54f35caSApple OSS Distributions ddi_remove_minor_node(devi, NULL);
539*c54f35caSApple OSS Distributions kmem_free(fbt_probetab, fbt_probetab_size * sizeof(fbt_probe_t *));
540*c54f35caSApple OSS Distributions fbt_probetab = NULL;
541*c54f35caSApple OSS Distributions fbt_probetab_mask = 0;
542*c54f35caSApple OSS Distributions }
543*c54f35caSApple OSS Distributions
544*c54f35caSApple OSS Distributions static int
fbt_attach(dev_info_t * devi)545*c54f35caSApple OSS Distributions fbt_attach(dev_info_t *devi)
546*c54f35caSApple OSS Distributions {
547*c54f35caSApple OSS Distributions if (fbt_probetab_size == 0) {
548*c54f35caSApple OSS Distributions fbt_probetab_size = FBT_PROBETAB_SIZE;
549*c54f35caSApple OSS Distributions }
550*c54f35caSApple OSS Distributions
551*c54f35caSApple OSS Distributions fbt_probetab_mask = fbt_probetab_size - 1;
552*c54f35caSApple OSS Distributions fbt_probetab =
553*c54f35caSApple OSS Distributions kmem_zalloc(fbt_probetab_size * sizeof(fbt_probe_t *), KM_SLEEP);
554*c54f35caSApple OSS Distributions
555*c54f35caSApple OSS Distributions dtrace_invop_add(fbt_invop);
556*c54f35caSApple OSS Distributions
557*c54f35caSApple OSS Distributions if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
558*c54f35caSApple OSS Distributions DDI_PSEUDO, 0) == DDI_FAILURE ||
559*c54f35caSApple OSS Distributions dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, NULL,
560*c54f35caSApple OSS Distributions &fbt_pops, NULL, &fbt_id) != 0) {
561*c54f35caSApple OSS Distributions fbt_cleanup(devi);
562*c54f35caSApple OSS Distributions return DDI_FAILURE;
563*c54f35caSApple OSS Distributions }
564*c54f35caSApple OSS Distributions
565*c54f35caSApple OSS Distributions return DDI_SUCCESS;
566*c54f35caSApple OSS Distributions }
567*c54f35caSApple OSS Distributions
568*c54f35caSApple OSS Distributions static d_open_t _fbt_open;
569*c54f35caSApple OSS Distributions
570*c54f35caSApple OSS Distributions static int
_fbt_open(dev_t dev,int flags,int devtype,struct proc * p)571*c54f35caSApple OSS Distributions _fbt_open(dev_t dev, int flags, int devtype, struct proc *p)
572*c54f35caSApple OSS Distributions {
573*c54f35caSApple OSS Distributions #pragma unused(dev,flags,devtype,p)
574*c54f35caSApple OSS Distributions return 0;
575*c54f35caSApple OSS Distributions }
576*c54f35caSApple OSS Distributions
577*c54f35caSApple OSS Distributions #define FBT_MAJOR -24 /* let the kernel pick the device number */
578*c54f35caSApple OSS Distributions
579*c54f35caSApple OSS Distributions static const struct cdevsw fbt_cdevsw =
580*c54f35caSApple OSS Distributions {
581*c54f35caSApple OSS Distributions .d_open = _fbt_open,
582*c54f35caSApple OSS Distributions .d_close = eno_opcl,
583*c54f35caSApple OSS Distributions .d_read = eno_rdwrt,
584*c54f35caSApple OSS Distributions .d_write = eno_rdwrt,
585*c54f35caSApple OSS Distributions .d_ioctl = eno_ioctl,
586*c54f35caSApple OSS Distributions .d_stop = eno_stop,
587*c54f35caSApple OSS Distributions .d_reset = eno_reset,
588*c54f35caSApple OSS Distributions .d_select = eno_select,
589*c54f35caSApple OSS Distributions .d_mmap = eno_mmap,
590*c54f35caSApple OSS Distributions .d_strategy = eno_strat,
591*c54f35caSApple OSS Distributions .d_reserved_1 = eno_getc,
592*c54f35caSApple OSS Distributions .d_reserved_2 = eno_putc,
593*c54f35caSApple OSS Distributions };
594*c54f35caSApple OSS Distributions
595*c54f35caSApple OSS Distributions #undef kmem_alloc /* from its binding to dt_kmem_alloc glue */
596*c54f35caSApple OSS Distributions #undef kmem_free /* from its binding to dt_kmem_free glue */
597*c54f35caSApple OSS Distributions #include <vm/vm_kern.h>
598*c54f35caSApple OSS Distributions
599*c54f35caSApple OSS Distributions
600*c54f35caSApple OSS Distributions void
fbt_init(void)601*c54f35caSApple OSS Distributions fbt_init( void )
602*c54f35caSApple OSS Distributions {
603*c54f35caSApple OSS Distributions int majdevno = cdevsw_add(FBT_MAJOR, &fbt_cdevsw);
604*c54f35caSApple OSS Distributions
605*c54f35caSApple OSS Distributions if (majdevno < 0) {
606*c54f35caSApple OSS Distributions printf("fbt_init: failed to allocate a major number!\n");
607*c54f35caSApple OSS Distributions return;
608*c54f35caSApple OSS Distributions }
609*c54f35caSApple OSS Distributions
610*c54f35caSApple OSS Distributions fbt_blacklist_init();
611*c54f35caSApple OSS Distributions fbt_attach((dev_info_t*)(uintptr_t)majdevno);
612*c54f35caSApple OSS Distributions }
613*c54f35caSApple OSS Distributions #undef FBT_MAJOR
614