]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
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. | |
7 | * | |
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. | |
12 | * | |
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] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
6d2010ae | 22 | * Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
2d21ac55 A |
23 | * Use is subject to license terms. |
24 | */ | |
25 | ||
2d21ac55 A |
26 | #include <sys/param.h> |
27 | #include <sys/systm.h> | |
28 | #include <sys/errno.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <sys/conf.h> | |
32 | #include <sys/fcntl.h> | |
33 | #include <miscfs/devfs/devfs.h> | |
34 | ||
f427ee49 | 35 | #if defined(__arm__) || defined(__arm64__) |
5ba3f43e | 36 | #include <arm/caches_internal.h> |
f427ee49 | 37 | #endif /* defined(__arm__) || defined(__arm64__) */ |
39236c6e | 38 | |
2d21ac55 A |
39 | #include <sys/dtrace.h> |
40 | #include <sys/dtrace_impl.h> | |
41 | ||
42 | #include <sys/dtrace_glue.h> | |
43 | ||
44 | #include <sys/sdt_impl.h> | |
6d2010ae | 45 | extern int dtrace_kernel_symbol_mode; |
2d21ac55 | 46 | |
f427ee49 A |
47 | #include <ptrauth.h> |
48 | ||
fe8ab488 | 49 | /* #include <machine/trap.h */ |
2d21ac55 | 50 | struct savearea_t; /* Used anonymously */ |
2d21ac55 | 51 | |
5ba3f43e A |
52 | #if defined(__arm__) |
53 | typedef kern_return_t (*perfCallback)(int, struct savearea_t *, __unused int, __unused int); | |
54 | extern perfCallback tempDTraceTrapHook; | |
55 | extern kern_return_t fbt_perfCallback(int, struct savearea_t *, __unused int, __unused int); | |
0a7de745 A |
56 | #define SDT_PATCHVAL 0xdefc |
57 | #define SDT_AFRAMES 7 | |
5ba3f43e A |
58 | #elif defined(__arm64__) |
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); | |
0a7de745 A |
62 | #define SDT_PATCHVAL 0xe7eeee7e |
63 | #define SDT_AFRAMES 7 | |
5ba3f43e | 64 | #elif defined(__x86_64__) |
fe8ab488 | 65 | typedef kern_return_t (*perfCallback)(int, struct savearea_t *, uintptr_t *, int); |
2d21ac55 | 66 | extern perfCallback tempDTraceTrapHook; |
fe8ab488 | 67 | extern kern_return_t fbt_perfCallback(int, struct savearea_t *, uintptr_t *, int); |
0a7de745 A |
68 | #define SDT_PATCHVAL 0xf0 |
69 | #define SDT_AFRAMES 6 | |
2d21ac55 A |
70 | #else |
71 | #error Unknown architecture | |
72 | #endif | |
73 | ||
0a7de745 | 74 | #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */ |
2d21ac55 | 75 | |
b0d623f7 | 76 | #define DTRACE_PROBE_PREFIX "_dtrace_probe$" |
b0d623f7 | 77 | |
0a7de745 A |
78 | static int sdt_verbose = 0; |
79 | sdt_probe_t **sdt_probetab; | |
80 | int sdt_probetab_size; | |
81 | int sdt_probetab_mask; | |
2d21ac55 A |
82 | |
83 | /*ARGSUSED*/ | |
84 | static void | |
85 | __sdt_provide_module(void *arg, struct modctl *ctl) | |
86 | { | |
87 | #pragma unused(arg) | |
6d2010ae | 88 | struct module *mp = (struct module *)ctl->mod_address; |
2d21ac55 A |
89 | char *modname = ctl->mod_modname; |
90 | sdt_probedesc_t *sdpd; | |
91 | sdt_probe_t *sdp, *old; | |
92 | sdt_provider_t *prov; | |
2d21ac55 A |
93 | |
94 | /* | |
95 | * One for all, and all for one: if we haven't yet registered all of | |
96 | * our providers, we'll refuse to provide anything. | |
97 | */ | |
98 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
0a7de745 | 99 | if (prov->sdtp_id == DTRACE_PROVNONE) { |
2d21ac55 | 100 | return; |
0a7de745 | 101 | } |
2d21ac55 A |
102 | } |
103 | ||
0a7de745 | 104 | if (!mp || mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL) { |
2d21ac55 | 105 | return; |
0a7de745 | 106 | } |
2d21ac55 A |
107 | |
108 | for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) { | |
f427ee49 | 109 | const char *func; |
2d21ac55 A |
110 | dtrace_id_t id; |
111 | ||
f427ee49 A |
112 | /* Validate probe's provider name. Do not provide probes for unknown providers. */ |
113 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
114 | if (strcmp(prov->sdtp_prefix, sdpd->sdpd_prov) == 0) { | |
2d21ac55 A |
115 | break; |
116 | } | |
117 | } | |
118 | ||
f427ee49 A |
119 | if (prov->sdtp_name == NULL) { |
120 | printf("Ignoring probes from unsupported provider %s\n", sdpd->sdpd_prov); | |
121 | continue; | |
2d21ac55 A |
122 | } |
123 | ||
0a7de745 | 124 | sdp = kmem_zalloc(sizeof(sdt_probe_t), KM_SLEEP); |
2d21ac55 A |
125 | sdp->sdp_loadcnt = ctl->mod_loadcnt; |
126 | sdp->sdp_ctl = ctl; | |
f427ee49 A |
127 | sdp->sdp_name = kmem_alloc(strlen(sdpd->sdpd_name) + 1, KM_SLEEP); |
128 | strncpy(sdp->sdp_name, sdpd->sdpd_name, strlen(sdpd->sdpd_name) + 1); | |
129 | sdp->sdp_namelen = strlen(sdpd->sdpd_name) + 1; | |
2d21ac55 A |
130 | sdp->sdp_provider = prov; |
131 | ||
f427ee49 | 132 | func = (sdpd->sdpd_func != NULL) ? sdpd->sdpd_func : "<unknown>"; |
2d21ac55 A |
133 | |
134 | /* | |
135 | * We have our provider. Now create the probe. | |
136 | */ | |
137 | if ((id = dtrace_probe_lookup(prov->sdtp_id, modname, | |
f427ee49 | 138 | func, sdp->sdp_name)) != DTRACE_IDNONE) { |
2d21ac55 A |
139 | old = dtrace_probe_arg(prov->sdtp_id, id); |
140 | ASSERT(old != NULL); | |
141 | ||
142 | sdp->sdp_next = old->sdp_next; | |
143 | sdp->sdp_id = id; | |
144 | old->sdp_next = sdp; | |
145 | } else { | |
146 | sdp->sdp_id = dtrace_probe_create(prov->sdtp_id, | |
f427ee49 | 147 | modname, func, sdp->sdp_name, SDT_AFRAMES, sdp); |
2d21ac55 A |
148 | |
149 | mp->sdt_nprobes++; | |
150 | } | |
151 | ||
0a7de745 | 152 | #if 0 |
f427ee49 A |
153 | printf("__sdt_provide_module: sdpd=0x%p sdp=0x%p name=%s, id=%d\n", sdpd, sdp, |
154 | sdp->sdp_name, sdp->sdp_id); | |
0a7de745 | 155 | #endif |
39236c6e | 156 | |
2d21ac55 A |
157 | sdp->sdp_hashnext = |
158 | sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)]; | |
159 | sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp; | |
160 | ||
161 | sdp->sdp_patchval = SDT_PATCHVAL; | |
162 | sdp->sdp_patchpoint = (sdt_instr_t *)sdpd->sdpd_offset; | |
163 | sdp->sdp_savedval = *sdp->sdp_patchpoint; | |
164 | } | |
165 | } | |
166 | ||
167 | /*ARGSUSED*/ | |
168 | static void | |
169 | sdt_destroy(void *arg, dtrace_id_t id, void *parg) | |
170 | { | |
171 | #pragma unused(arg,id) | |
172 | sdt_probe_t *sdp = parg, *old, *last, *hash; | |
173 | int ndx; | |
fe8ab488 | 174 | |
2d21ac55 | 175 | #if !defined(__APPLE__) |
fe8ab488 A |
176 | /* |
177 | * APPLE NOTE: sdt probes for kexts not yet implemented | |
178 | */ | |
2d21ac55 A |
179 | struct modctl *ctl = sdp->sdp_ctl; |
180 | ||
181 | if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) { | |
182 | if ((ctl->mod_loadcnt == sdp->sdp_loadcnt && | |
183 | ctl->mod_loaded)) { | |
184 | ((struct module *)(ctl->mod_mp))->sdt_nprobes--; | |
185 | } | |
186 | } | |
187 | #endif /* __APPLE__ */ | |
188 | ||
189 | while (sdp != NULL) { | |
190 | old = sdp; | |
191 | ||
192 | /* | |
193 | * Now we need to remove this probe from the sdt_probetab. | |
194 | */ | |
195 | ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint); | |
196 | last = NULL; | |
197 | hash = sdt_probetab[ndx]; | |
198 | ||
199 | while (hash != sdp) { | |
200 | ASSERT(hash != NULL); | |
201 | last = hash; | |
202 | hash = hash->sdp_hashnext; | |
203 | } | |
204 | ||
205 | if (last != NULL) { | |
206 | last->sdp_hashnext = sdp->sdp_hashnext; | |
207 | } else { | |
208 | sdt_probetab[ndx] = sdp->sdp_hashnext; | |
209 | } | |
210 | ||
211 | kmem_free(sdp->sdp_name, sdp->sdp_namelen); | |
212 | sdp = sdp->sdp_next; | |
0a7de745 | 213 | kmem_free(old, sizeof(sdt_probe_t)); |
2d21ac55 A |
214 | } |
215 | } | |
216 | ||
217 | /*ARGSUSED*/ | |
6d2010ae | 218 | static int |
2d21ac55 A |
219 | sdt_enable(void *arg, dtrace_id_t id, void *parg) |
220 | { | |
221 | #pragma unused(arg,id) | |
222 | sdt_probe_t *sdp = parg; | |
223 | struct modctl *ctl = sdp->sdp_ctl; | |
224 | ||
2d21ac55 A |
225 | ctl->mod_nenabled++; |
226 | ||
227 | /* | |
228 | * If this module has disappeared since we discovered its probes, | |
229 | * refuse to enable it. | |
230 | */ | |
231 | if (!ctl->mod_loaded) { | |
232 | if (sdt_verbose) { | |
233 | cmn_err(CE_NOTE, "sdt is failing for probe %s " | |
234 | "(module %s unloaded)", | |
235 | sdp->sdp_name, ctl->mod_modname); | |
236 | } | |
237 | goto err; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Now check that our modctl has the expected load count. If it | |
242 | * doesn't, this module must have been unloaded and reloaded -- and | |
243 | * we're not going to touch it. | |
244 | */ | |
245 | if (ctl->mod_loadcnt != sdp->sdp_loadcnt) { | |
246 | if (sdt_verbose) { | |
247 | cmn_err(CE_NOTE, "sdt is failing for probe %s " | |
248 | "(module %s reloaded)", | |
249 | sdp->sdp_name, ctl->mod_modname); | |
250 | } | |
251 | goto err; | |
252 | } | |
2d21ac55 | 253 | |
f427ee49 | 254 | dtrace_casptr(&tempDTraceTrapHook, NULL, ptrauth_nop_cast(void *, &fbt_perfCallback)); |
2d21ac55 A |
255 | if (tempDTraceTrapHook != (perfCallback)fbt_perfCallback) { |
256 | if (sdt_verbose) { | |
257 | cmn_err(CE_NOTE, "sdt_enable is failing for probe %s " | |
258 | "in module %s: tempDTraceTrapHook already occupied.", | |
259 | sdp->sdp_name, ctl->mod_modname); | |
260 | } | |
0a7de745 | 261 | return 0; |
2d21ac55 A |
262 | } |
263 | ||
264 | while (sdp != NULL) { | |
0a7de745 A |
265 | (void)ml_nofault_copy((vm_offset_t)&sdp->sdp_patchval, (vm_offset_t)sdp->sdp_patchpoint, |
266 | (vm_size_t)sizeof(sdp->sdp_patchval)); | |
39037602 A |
267 | |
268 | /* | |
269 | * Make the patched instruction visible via a data + instruction | |
270 | * cache fush on platforms that need it | |
271 | */ | |
0a7de745 A |
272 | flush_dcache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_patchval), 0); |
273 | invalidate_icache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_patchval), 0); | |
39037602 | 274 | |
2d21ac55 A |
275 | sdp = sdp->sdp_next; |
276 | } | |
6d2010ae | 277 | |
2d21ac55 | 278 | err: |
0a7de745 | 279 | return 0; |
2d21ac55 A |
280 | } |
281 | ||
282 | /*ARGSUSED*/ | |
283 | static void | |
284 | sdt_disable(void *arg, dtrace_id_t id, void *parg) | |
285 | { | |
286 | #pragma unused(arg,id) | |
287 | sdt_probe_t *sdp = parg; | |
2d21ac55 A |
288 | struct modctl *ctl = sdp->sdp_ctl; |
289 | ||
290 | ctl->mod_nenabled--; | |
291 | ||
0a7de745 | 292 | if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt) { |
2d21ac55 | 293 | goto err; |
0a7de745 | 294 | } |
2d21ac55 A |
295 | |
296 | while (sdp != NULL) { | |
0a7de745 A |
297 | (void)ml_nofault_copy((vm_offset_t)&sdp->sdp_savedval, (vm_offset_t)sdp->sdp_patchpoint, |
298 | (vm_size_t)sizeof(sdp->sdp_savedval)); | |
39037602 A |
299 | /* |
300 | * Make the patched instruction visible via a data + instruction | |
301 | * cache flush on platforms that need it | |
302 | */ | |
0a7de745 A |
303 | flush_dcache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_savedval), 0); |
304 | invalidate_icache((vm_offset_t)sdp->sdp_patchpoint, (vm_size_t)sizeof(sdp->sdp_savedval), 0); | |
2d21ac55 A |
305 | sdp = sdp->sdp_next; |
306 | } | |
307 | ||
308 | err: | |
309 | ; | |
310 | } | |
311 | ||
312 | static dtrace_pops_t sdt_pops = { | |
0a7de745 A |
313 | .dtps_provide = NULL, |
314 | .dtps_provide_module = sdt_provide_module, | |
315 | .dtps_enable = sdt_enable, | |
316 | .dtps_disable = sdt_disable, | |
317 | .dtps_suspend = NULL, | |
318 | .dtps_resume = NULL, | |
319 | .dtps_getargdesc = sdt_getargdesc, | |
320 | .dtps_getargval = sdt_getarg, | |
321 | .dtps_usermode = NULL, | |
322 | .dtps_destroy = sdt_destroy, | |
2d21ac55 A |
323 | }; |
324 | ||
325 | /*ARGSUSED*/ | |
326 | static int | |
d9a64523 | 327 | sdt_attach(dev_info_t *devi) |
2d21ac55 | 328 | { |
2d21ac55 A |
329 | sdt_provider_t *prov; |
330 | ||
331 | if (ddi_create_minor_node(devi, "sdt", S_IFCHR, | |
332 | 0, DDI_PSEUDO, 0) == DDI_FAILURE) { | |
333 | cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node"); | |
334 | ddi_remove_minor_node(devi, NULL); | |
0a7de745 | 335 | return DDI_FAILURE; |
2d21ac55 A |
336 | } |
337 | ||
0a7de745 | 338 | if (sdt_probetab_size == 0) { |
2d21ac55 | 339 | sdt_probetab_size = SDT_PROBETAB_SIZE; |
0a7de745 | 340 | } |
2d21ac55 A |
341 | |
342 | sdt_probetab_mask = sdt_probetab_size - 1; | |
343 | sdt_probetab = | |
0a7de745 | 344 | kmem_zalloc(sdt_probetab_size * sizeof(sdt_probe_t *), KM_SLEEP); |
2d21ac55 A |
345 | dtrace_invop_add(sdt_invop); |
346 | ||
347 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
348 | if (dtrace_register(prov->sdtp_name, prov->sdtp_attr, | |
349 | DTRACE_PRIV_KERNEL, NULL, | |
350 | &sdt_pops, prov, &prov->sdtp_id) != 0) { | |
351 | cmn_err(CE_WARN, "failed to register sdt provider %s", | |
352 | prov->sdtp_name); | |
353 | } | |
354 | } | |
355 | ||
0a7de745 | 356 | return DDI_SUCCESS; |
2d21ac55 A |
357 | } |
358 | ||
fe8ab488 A |
359 | /* |
360 | * APPLE NOTE: sdt_detach not implemented | |
361 | */ | |
2d21ac55 A |
362 | #if !defined(__APPLE__) |
363 | /*ARGSUSED*/ | |
364 | static int | |
365 | sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) | |
366 | { | |
367 | sdt_provider_t *prov; | |
368 | ||
369 | switch (cmd) { | |
370 | case DDI_DETACH: | |
371 | break; | |
372 | ||
373 | case DDI_SUSPEND: | |
0a7de745 | 374 | return DDI_SUCCESS; |
2d21ac55 A |
375 | |
376 | default: | |
0a7de745 | 377 | return DDI_FAILURE; |
2d21ac55 A |
378 | } |
379 | ||
380 | for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) { | |
381 | if (prov->sdtp_id != DTRACE_PROVNONE) { | |
0a7de745 A |
382 | if (dtrace_unregister(prov->sdtp_id) != 0) { |
383 | return DDI_FAILURE; | |
384 | } | |
2d21ac55 A |
385 | |
386 | prov->sdtp_id = DTRACE_PROVNONE; | |
387 | } | |
388 | } | |
389 | ||
390 | dtrace_invop_remove(sdt_invop); | |
0a7de745 | 391 | kmem_free(sdt_probetab, sdt_probetab_size * sizeof(sdt_probe_t *)); |
2d21ac55 | 392 | |
0a7de745 | 393 | return DDI_SUCCESS; |
2d21ac55 | 394 | } |
fe8ab488 | 395 | #endif /* __APPLE__ */ |
2d21ac55 | 396 | |
2d21ac55 A |
397 | d_open_t _sdt_open; |
398 | ||
0a7de745 A |
399 | int |
400 | _sdt_open(dev_t dev, int flags, int devtype, struct proc *p) | |
2d21ac55 A |
401 | { |
402 | #pragma unused(dev,flags,devtype,p) | |
403 | return 0; | |
404 | } | |
405 | ||
406 | #define SDT_MAJOR -24 /* let the kernel pick the device number */ | |
407 | ||
f427ee49 | 408 | static const struct cdevsw sdt_cdevsw = |
2d21ac55 | 409 | { |
f427ee49 A |
410 | .d_open = _sdt_open, |
411 | .d_close = eno_opcl, | |
412 | .d_read = eno_rdwrt, | |
413 | .d_write = eno_rdwrt, | |
414 | .d_ioctl = eno_ioctl, | |
415 | .d_stop = (stop_fcn_t *)nulldev, | |
416 | .d_reset = (reset_fcn_t *)nulldev, | |
417 | .d_select = eno_select, | |
418 | .d_mmap = eno_mmap, | |
419 | .d_strategy = eno_strat, | |
420 | .d_reserved_1 = eno_getc, | |
421 | .d_reserved_2 = eno_putc, | |
2d21ac55 A |
422 | }; |
423 | ||
2d21ac55 A |
424 | static struct modctl g_sdt_kernctl; |
425 | static struct module g_sdt_mach_module; | |
426 | ||
2d21ac55 | 427 | #include <mach-o/nlist.h> |
b0d623f7 | 428 | #include <libkern/kernel_mach_header.h> |
2d21ac55 | 429 | |
f427ee49 A |
430 | /* |
431 | * Represents single record in __DATA,__sdt section. | |
432 | */ | |
433 | typedef struct dtrace_sdt_def { | |
434 | uintptr_t dsd_addr; /* probe site location */ | |
435 | const char *dsd_prov; /* provider's name */ | |
436 | const char *dsd_name; /* probe's name */ | |
437 | } __attribute__((__packed__)) dtrace_sdt_def_t; | |
438 | ||
439 | /* | |
440 | * Creates a copy of name and unescapes '-' characters. | |
441 | */ | |
442 | static char * | |
443 | sdt_strdup_name(const char *name) | |
444 | { | |
445 | size_t len = strlen(name) + 1; | |
446 | size_t i, j; | |
447 | char *nname = kmem_alloc(len, KM_SLEEP); | |
448 | ||
449 | for (i = 0, j = 0; name[j] != '\0'; i++) { | |
450 | if (name[j] == '_' && name[j + 1] == '_') { | |
451 | nname[i] = '-'; | |
452 | j += 2; | |
453 | } else { | |
454 | nname[i] = name[j++]; | |
455 | } | |
456 | } | |
457 | ||
458 | nname[i] = '\0'; | |
459 | return nname; | |
460 | } | |
461 | ||
0a7de745 A |
462 | void |
463 | sdt_early_init( void ) | |
2d21ac55 | 464 | { |
d9a64523 A |
465 | if (dtrace_sdt_probes_restricted()) { |
466 | return; | |
467 | } | |
468 | if (MH_MAGIC_KERNEL != _mh_execute_header.magic) { | |
469 | g_sdt_kernctl.mod_address = (vm_address_t)NULL; | |
470 | g_sdt_kernctl.mod_size = 0; | |
471 | } else { | |
472 | kernel_mach_header_t *mh; | |
473 | struct load_command *cmd; | |
474 | kernel_segment_command_t *orig_ts = NULL, *orig_le = NULL; | |
f427ee49 | 475 | kernel_section_t *orig_dt = NULL; |
d9a64523 | 476 | struct symtab_command *orig_st = NULL; |
0a7de745 | 477 | kernel_nlist_t *sym = NULL; |
d9a64523 | 478 | char *strings; |
0a7de745 | 479 | unsigned int i; |
f427ee49 | 480 | unsigned int len; |
0a7de745 | 481 | |
d9a64523 A |
482 | g_sdt_mach_module.sdt_nprobes = 0; |
483 | g_sdt_mach_module.sdt_probes = NULL; | |
0a7de745 | 484 | |
d9a64523 A |
485 | g_sdt_kernctl.mod_address = (vm_address_t)&g_sdt_mach_module; |
486 | g_sdt_kernctl.mod_size = 0; | |
487 | strncpy((char *)&(g_sdt_kernctl.mod_modname), "mach_kernel", KMOD_MAX_NAME); | |
0a7de745 | 488 | |
d9a64523 A |
489 | g_sdt_kernctl.mod_next = NULL; |
490 | g_sdt_kernctl.mod_stale = NULL; | |
491 | g_sdt_kernctl.mod_id = 0; | |
492 | g_sdt_kernctl.mod_loadcnt = 1; | |
493 | g_sdt_kernctl.mod_loaded = 1; | |
494 | g_sdt_kernctl.mod_flags = 0; | |
495 | g_sdt_kernctl.mod_nenabled = 0; | |
0a7de745 | 496 | |
d9a64523 A |
497 | mh = &_mh_execute_header; |
498 | cmd = (struct load_command*) &mh[1]; | |
499 | for (i = 0; i < mh->ncmds; i++) { | |
500 | if (cmd->cmd == LC_SEGMENT_KERNEL) { | |
501 | kernel_segment_command_t *orig_sg = (kernel_segment_command_t *) cmd; | |
0a7de745 A |
502 | |
503 | if (LIT_STRNEQL(orig_sg->segname, SEG_TEXT)) { | |
d9a64523 | 504 | orig_ts = orig_sg; |
0a7de745 | 505 | } else if (LIT_STRNEQL(orig_sg->segname, SEG_LINKEDIT)) { |
d9a64523 | 506 | orig_le = orig_sg; |
0a7de745 | 507 | } else if (LIT_STRNEQL(orig_sg->segname, "")) { |
d9a64523 | 508 | orig_ts = orig_sg; /* kexts have a single unnamed segment */ |
0a7de745 A |
509 | } |
510 | } else if (cmd->cmd == LC_SYMTAB) { | |
d9a64523 | 511 | orig_st = (struct symtab_command *) cmd; |
0a7de745 A |
512 | } |
513 | ||
d9a64523 | 514 | cmd = (struct load_command *) ((uintptr_t) cmd + cmd->cmdsize); |
2d21ac55 | 515 | } |
0a7de745 | 516 | |
f427ee49 A |
517 | /* Locate DTrace SDT section in the object. */ |
518 | if ((orig_dt = getsectbyname("__DATA", "__sdt")) == NULL) { | |
519 | printf("DTrace section not found.\n"); | |
520 | return; | |
521 | } | |
522 | ||
0a7de745 | 523 | if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL)) { |
3e170ce0 | 524 | return; |
0a7de745 A |
525 | } |
526 | ||
d9a64523 A |
527 | sym = (kernel_nlist_t *)(orig_le->vmaddr + orig_st->symoff - orig_le->fileoff); |
528 | strings = (char *)(orig_le->vmaddr + orig_st->stroff - orig_le->fileoff); | |
0a7de745 | 529 | |
f427ee49 A |
530 | /* |
531 | * Iterate over SDT section and establish all SDT probes. | |
532 | */ | |
533 | dtrace_sdt_def_t *sdtdef = (dtrace_sdt_def_t *)(orig_dt->addr); | |
534 | for (size_t k = 0; k < orig_dt->size / sizeof(dtrace_sdt_def_t); k++, sdtdef++) { | |
535 | const char *funcname; | |
d9a64523 | 536 | unsigned long best; |
0a7de745 | 537 | |
f427ee49 | 538 | sdt_probedesc_t *sdpd = kmem_alloc(sizeof(sdt_probedesc_t), KM_SLEEP); |
0a7de745 | 539 | |
f427ee49 A |
540 | /* Unescape probe name and keep a note of the size of original memory allocation. */ |
541 | sdpd->sdpd_name = sdt_strdup_name(sdtdef->dsd_name); | |
542 | sdpd->sdpd_namelen = strlen(sdtdef->dsd_name) + 1; | |
0a7de745 | 543 | |
f427ee49 A |
544 | /* Used only for provider structure lookup so there is no need to make dynamic copy. */ |
545 | sdpd->sdpd_prov = sdtdef->dsd_prov; | |
0a7de745 | 546 | |
f427ee49 A |
547 | /* |
548 | * Find the symbol immediately preceding the sdt probe site just discovered, | |
549 | * that symbol names the function containing the sdt probe. | |
550 | */ | |
551 | funcname = "<unknown>"; | |
552 | for (i = 0; i < orig_st->nsyms; i++) { | |
553 | uint8_t jn_type = sym[i].n_type & N_TYPE; | |
554 | char *jname = strings + sym[i].n_un.n_strx; | |
0a7de745 | 555 | |
f427ee49 A |
556 | if ((N_SECT != jn_type && N_ABS != jn_type)) { |
557 | continue; | |
558 | } | |
d9a64523 | 559 | |
f427ee49 A |
560 | if (0 == sym[i].n_un.n_strx) { /* iff a null, "", name. */ |
561 | continue; | |
562 | } | |
d9a64523 | 563 | |
f427ee49 A |
564 | if (*jname == '_') { |
565 | jname += 1; | |
566 | } | |
d9a64523 | 567 | |
f427ee49 A |
568 | if (sdtdef->dsd_addr <= (unsigned long)sym[i].n_value) { |
569 | continue; | |
570 | } | |
d9a64523 | 571 | |
f427ee49 A |
572 | if ((unsigned long)sym[i].n_value > best) { |
573 | best = (unsigned long)sym[i].n_value; | |
574 | funcname = jname; | |
d9a64523 | 575 | } |
f427ee49 | 576 | } |
d9a64523 | 577 | |
f427ee49 A |
578 | len = strlen(funcname) + 1; |
579 | sdpd->sdpd_func = kmem_alloc(len, KM_SLEEP); | |
580 | strncpy(sdpd->sdpd_func, funcname, len); | |
0a7de745 | 581 | |
f427ee49 | 582 | sdpd->sdpd_offset = sdtdef->dsd_addr; |
5ba3f43e | 583 | #if defined(__arm__) |
f427ee49 A |
584 | /* PR8353094 - mask off thumb-bit */ |
585 | sdpd->sdpd_offset &= ~0x1U; | |
5ba3f43e | 586 | #elif defined(__arm64__) |
f427ee49 | 587 | sdpd->sdpd_offset &= ~0x1LU; |
5ba3f43e | 588 | #endif /* __arm__ */ |
39236c6e | 589 | |
f427ee49 A |
590 | sdpd->sdpd_next = g_sdt_mach_module.sdt_probes; |
591 | g_sdt_mach_module.sdt_probes = sdpd; | |
2d21ac55 | 592 | } |
d9a64523 A |
593 | } |
594 | } | |
595 | ||
0a7de745 A |
596 | void |
597 | sdt_init( void ) | |
d9a64523 A |
598 | { |
599 | int majdevno = cdevsw_add(SDT_MAJOR, &sdt_cdevsw); | |
0a7de745 | 600 | |
d9a64523 A |
601 | if (majdevno < 0) { |
602 | printf("sdt_init: failed to allocate a major number!\n"); | |
603 | return; | |
604 | } | |
605 | ||
606 | if (dtrace_sdt_probes_restricted()) { | |
607 | return; | |
608 | } | |
609 | ||
610 | sdt_attach((dev_info_t*)(uintptr_t)majdevno); | |
2d21ac55 | 611 | } |
b0d623f7 | 612 | |
2d21ac55 A |
613 | #undef SDT_MAJOR |
614 | ||
615 | /*ARGSUSED*/ | |
616 | void | |
617 | sdt_provide_module(void *arg, struct modctl *ctl) | |
618 | { | |
c910b4d9 | 619 | #pragma unused(arg) |
6d2010ae A |
620 | ASSERT(ctl != NULL); |
621 | ASSERT(dtrace_kernel_symbol_mode != DTRACE_KERNEL_SYMBOLS_NEVER); | |
5ba3f43e | 622 | LCK_MTX_ASSERT(&mod_lock, LCK_MTX_ASSERT_OWNED); |
0a7de745 A |
623 | |
624 | if (MOD_SDT_DONE(ctl)) { | |
6d2010ae | 625 | return; |
0a7de745 A |
626 | } |
627 | ||
6d2010ae A |
628 | if (MOD_IS_MACH_KERNEL(ctl)) { |
629 | __sdt_provide_module(arg, &g_sdt_kernctl); | |
0a7de745 | 630 | |
6d2010ae A |
631 | sdt_probedesc_t *sdpd = g_sdt_mach_module.sdt_probes; |
632 | while (sdpd) { | |
633 | sdt_probedesc_t *this_sdpd = sdpd; | |
f427ee49 | 634 | kmem_free((void *)sdpd->sdpd_name, sdpd->sdpd_namelen); |
6d2010ae A |
635 | kmem_free((void *)sdpd->sdpd_func, strlen(sdpd->sdpd_func) + 1); |
636 | sdpd = sdpd->sdpd_next; | |
637 | kmem_free((void *)this_sdpd, sizeof(sdt_probedesc_t)); | |
638 | } | |
639 | g_sdt_mach_module.sdt_probes = NULL; | |
640 | } else { | |
fe8ab488 A |
641 | /* |
642 | * APPLE NOTE: sdt probes for kexts not yet implemented | |
643 | */ | |
2d21ac55 | 644 | } |
0a7de745 | 645 | |
6d2010ae A |
646 | /* Need to mark this module as completed */ |
647 | ctl->mod_flags |= MODCTL_SDT_PROBES_PROVIDED; | |
2d21ac55 | 648 | } |