]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/fbt_x86.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / bsd / dev / i386 / fbt_x86.c
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, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* #pragma ident "@(#)fbt.c 1.15 05/09/19 SMI" */
28
29 #ifdef KERNEL
30 #ifndef _KERNEL
31 #define _KERNEL /* Solaris vs. Darwin */
32 #endif
33 #endif
34
35 #define MACH__POSIX_C_SOURCE_PRIVATE 1 /* pulls in suitable savearea from mach/ppc/thread_status.h */
36 #include <kern/thread.h>
37 #include <mach/thread_status.h>
38 #include <mach/vm_param.h>
39 #include <mach-o/loader.h>
40 #include <mach-o/nlist.h>
41
42 extern struct mach_header _mh_execute_header; /* the kernel's mach header */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48 #include <sys/ioctl.h>
49 #include <sys/conf.h>
50 #include <sys/fcntl.h>
51 #include <miscfs/devfs/devfs.h>
52
53 #include <sys/dtrace.h>
54 #include <sys/dtrace_impl.h>
55 #include <sys/fbt.h>
56
57 #include <sys/dtrace_glue.h>
58
59 #define DTRACE_INVOP_NOP_SKIP 1
60 #define DTRACE_INVOP_MOVL_ESP_EBP 10
61 #define DTRACE_INVOP_MOVL_ESP_EBP_SKIP 2
62 #define DTRACE_INVOP_LEAVE_SKIP 1
63
64 #define FBT_PUSHL_EBP 0x55
65 #define FBT_MOVL_ESP_EBP0_V0 0x8b
66 #define FBT_MOVL_ESP_EBP1_V0 0xec
67 #define FBT_MOVL_ESP_EBP0_V1 0x89
68 #define FBT_MOVL_ESP_EBP1_V1 0xe5
69 #define FBT_REX_RSP_RBP 0x48
70
71 #define FBT_POPL_EBP 0x5d
72 #define FBT_RET 0xc3
73 #define FBT_RET_IMM16 0xc2
74 #define FBT_LEAVE 0xc9
75 #define FBT_JMP_SHORT_REL 0xeb /* Jump short, relative, displacement relative to next instr. */
76 #define FBT_JMP_NEAR_REL 0xe9 /* Jump near, relative, displacement relative to next instr. */
77 #define FBT_JMP_FAR_ABS 0xea /* Jump far, absolute, address given in operand */
78 #define FBT_RET_LEN 1
79 #define FBT_RET_IMM16_LEN 3
80 #define FBT_JMP_SHORT_REL_LEN 2
81 #define FBT_JMP_NEAR_REL_LEN 5
82 #define FBT_JMP_FAR_ABS_LEN 5
83
84 #define FBT_PATCHVAL 0xf0
85 #define FBT_AFRAMES_ENTRY 7
86 #define FBT_AFRAMES_RETURN 6
87
88 #define FBT_ENTRY "entry"
89 #define FBT_RETURN "return"
90 #define FBT_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
91
92 extern dtrace_provider_id_t fbt_id;
93 extern fbt_probe_t **fbt_probetab;
94 extern int fbt_probetab_mask;
95
96 /*
97 * Critical routines that must not be probed. PR_5221096, PR_5379018.
98 */
99
100 static const char * critical_blacklist[] =
101 {
102 "bcopy_phys",
103 "console_cpu_alloc",
104 "console_cpu_free",
105 "cpu_IA32e_disable",
106 "cpu_IA32e_enable",
107 "cpu_control",
108 "cpu_data_alloc",
109 "cpu_desc_init",
110 "cpu_desc_init64",
111 "cpu_desc_load64",
112 "cpu_exit_wait",
113 "cpu_info",
114 "cpu_info_count",
115 "cpu_init",
116 "cpu_interrupt",
117 "cpu_machine_init",
118 "cpu_mode_init",
119 "cpu_processor_alloc",
120 "cpu_processor_free",
121 "cpu_signal_handler",
122 "cpu_sleep",
123 "cpu_start",
124 "cpu_subtype",
125 "cpu_thread_alloc",
126 "cpu_thread_halt",
127 "cpu_thread_init",
128 "cpu_threadtype",
129 "cpu_to_processor",
130 "cpu_topology_start",
131 "cpu_type",
132 "cpu_window_init",
133 "cpuid_cpu_display",
134 "handle_pending_TLB_flushes",
135 "hw_compare_and_store",
136 "machine_idle_cstate",
137 "mca_cpu_alloc",
138 "mca_cpu_init",
139 "ml_nofault_copy",
140 "pmap_cpu_alloc",
141 "pmap_cpu_free",
142 "pmap_cpu_high_map_vaddr",
143 "pmap_cpu_high_shared_remap",
144 "pmap_cpu_init",
145 "register_cpu_setup_func",
146 "unregister_cpu_setup_func"
147 };
148 #define CRITICAL_BLACKLIST_COUNT (sizeof(critical_blacklist)/sizeof(critical_blacklist[0]))
149
150 /*
151 * The transitive closure of entry points that can be reached from probe context.
152 * (Apart from routines whose names begin with dtrace_ or dtxnu_.)
153 */
154 static const char * probe_ctx_closure[] =
155 {
156 "Debugger",
157 "OSCompareAndSwap",
158 "absolutetime_to_microtime",
159 "ast_pending",
160 "clock_get_calendar_nanotime_nowait",
161 "copyin",
162 "copyin_user",
163 "copyinstr",
164 "copyout",
165 "copyoutstr",
166 "cpu_number",
167 "current_proc",
168 "current_processor",
169 "current_task",
170 "current_thread",
171 "debug_enter",
172 "find_user_regs",
173 "flush_tlb64",
174 "get_bsdtask_info",
175 "get_bsdthread_info",
176 "hw_atomic_and",
177 "kauth_cred_get",
178 "kauth_getgid",
179 "kauth_getuid",
180 "kernel_preempt_check",
181 "mach_absolute_time",
182 "max_valid_stack_address",
183 "ml_at_interrupt_context",
184 "ml_phys_write_byte_64",
185 "ml_phys_write_half_64",
186 "ml_phys_write_word_64",
187 "ml_set_interrupts_enabled",
188 "panic",
189 "pmap64_pde",
190 "pmap64_pdpt",
191 "pmap_find_phys",
192 "pmap_get_mapwindow",
193 "pmap_pde",
194 "pmap_pte",
195 "pmap_put_mapwindow",
196 "pmap_valid_page",
197 "prf",
198 "proc_is64bit",
199 "proc_selfname",
200 "proc_selfpid",
201 "psignal_lock",
202 "rtc_nanotime_load",
203 "rtc_nanotime_read",
204 "strlcpy",
205 "sync_iss_to_iks_unconditionally",
206 "timer_grab"
207 };
208 #define PROBE_CTX_CLOSURE_COUNT (sizeof(probe_ctx_closure)/sizeof(probe_ctx_closure[0]))
209
210
211 static int _cmp(const void *a, const void *b)
212 {
213 return strcmp((const char *)a, *(const char **)b);
214 }
215
216 static const void * bsearch(
217 register const void *key,
218 const void *base0,
219 size_t nmemb,
220 register size_t size,
221 register int (*compar)(const void *, const void *)) {
222
223 register const char *base = base0;
224 register size_t lim;
225 register int cmp;
226 register const void *p;
227
228 for (lim = nmemb; lim != 0; lim >>= 1) {
229 p = base + (lim >> 1) * size;
230 cmp = (*compar)(key, p);
231 if (cmp == 0)
232 return p;
233 if (cmp > 0) { /* key > p: move right */
234 base = (const char *)p + size;
235 lim--;
236 } /* else move left */
237 }
238 return (NULL);
239 }
240
241 int
242 fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
243 {
244 uintptr_t stack0 = 0, stack1 = 0, stack2 = 0, stack3 = 0, stack4 = 0;
245 fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
246
247 for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
248 if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
249
250 if (fbt->fbtp_roffset == 0) {
251 uintptr_t *stacktop;
252 if (CPU_ON_INTR(CPU))
253 stacktop = (uintptr_t *)dtrace_get_cpu_int_stack_top();
254 else
255 stacktop = (uintptr_t *)(dtrace_get_kernel_stack(current_thread()) + KERNEL_STACK_SIZE);
256
257 stack += 1; /* skip over the target's pushl'd %ebp */
258
259 if (stack <= stacktop)
260 CPU->cpu_dtrace_caller = *stack++;
261 if (stack <= stacktop)
262 stack0 = *stack++;
263 if (stack <= stacktop)
264 stack1 = *stack++;
265 if (stack <= stacktop)
266 stack2 = *stack++;
267 if (stack <= stacktop)
268 stack3 = *stack++;
269 if (stack <= stacktop)
270 stack4 = *stack++;
271
272 dtrace_probe(fbt->fbtp_id, stack0, stack1, stack2, stack3, stack4);
273 CPU->cpu_dtrace_caller = 0;
274 } else {
275 dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset, rval, 0, 0, 0);
276 CPU->cpu_dtrace_caller = 0;
277 }
278
279 return (fbt->fbtp_rval);
280 }
281 }
282
283 return (0);
284 }
285
286 #define IS_USER_TRAP(regs) (regs && (((regs)->cs & 3) != 0))
287 #define T_INVALID_OPCODE 6
288 #define FBT_EXCEPTION_CODE T_INVALID_OPCODE
289
290 kern_return_t
291 fbt_perfCallback(
292 int trapno,
293 x86_saved_state_t *tagged_regs,
294 __unused int unused1,
295 __unused int unused2)
296 {
297 kern_return_t retval = KERN_FAILURE;
298 x86_saved_state32_t *saved_state = saved_state32(tagged_regs);
299 struct x86_saved_state32_from_kernel *regs = (struct x86_saved_state32_from_kernel *)saved_state;
300
301 if (FBT_EXCEPTION_CODE == trapno && !IS_USER_TRAP(saved_state)) {
302 boolean_t oldlevel, cpu_64bit;
303 uint32_t esp_probe, *ebp, edi, fp, *pDst, delta = 0;
304 int emul;
305
306 cpu_64bit = ml_is64bit();
307 oldlevel = ml_set_interrupts_enabled(FALSE);
308
309 /* Calculate where the stack pointer was when the probe instruction "fired." */
310 if (cpu_64bit) {
311 esp_probe = saved_state->uesp; /* Easy, x86_64 establishes this value in idt64.s */
312 } else {
313 esp_probe = (uint32_t)&(regs[1]); /* Nasty, infer the location above the save area */
314 }
315
316 emul = dtrace_invop( saved_state->eip, (uintptr_t *)esp_probe, saved_state->eax );
317 __asm__ volatile(".globl _dtrace_invop_callsite");
318 __asm__ volatile("_dtrace_invop_callsite:");
319
320 switch (emul) {
321 case DTRACE_INVOP_NOP:
322 saved_state->eip += DTRACE_INVOP_NOP_SKIP; /* Skip over the patched NOP */
323 retval = KERN_SUCCESS;
324 break;
325
326 case DTRACE_INVOP_MOVL_ESP_EBP:
327 saved_state->ebp = esp_probe; /* Emulate patched movl %esp,%ebp */
328 saved_state->eip += DTRACE_INVOP_MOVL_ESP_EBP_SKIP; /* Skip over the bytes of the patched movl %esp,%ebp */
329 retval = KERN_SUCCESS;
330 break;
331
332 case DTRACE_INVOP_POPL_EBP:
333 case DTRACE_INVOP_LEAVE:
334 /*
335 * Emulate first micro-op of patched leave: movl %ebp,%esp
336 * fp points just below the return address slot for target's ret
337 * and at the slot holding the frame pointer saved by the target's prologue.
338 */
339 fp = saved_state->ebp;
340 /* Emulate second micro-op of patched leave: patched popl %ebp
341 * savearea ebp is set for the frame of the caller to target
342 * The *live* %esp will be adjusted below for pop increment(s)
343 */
344 saved_state->ebp = *(uint32_t *)fp;
345 /* Skip over the patched leave */
346 saved_state->eip += DTRACE_INVOP_LEAVE_SKIP;
347 /*
348 * Lift the stack to account for the emulated leave
349 * Account for words local in this frame
350 * (in "case DTRACE_INVOP_POPL_EBP:" this is zero.)
351 */
352 delta = ((uint32_t *)fp) - ((uint32_t *)esp_probe);
353 /* Account for popping off the ebp (just accomplished by the emulation
354 * above...)
355 */
356 delta += 1;
357
358 if (cpu_64bit)
359 saved_state->uesp += (delta << 2);
360
361 /* XXX Fragile in the extreme. Obtain the value of %edi that our caller pushed
362 * (on behalf of its caller -- trap_from_kernel()). Ultimately,
363 * trap_from_kernel's stack pointer is restored from this slot.
364 * This is sensitive to the manner in which the compiler preserves %edi,
365 * and trap_from_kernel()'s internals.
366 */
367 ebp = (uint32_t *)__builtin_frame_address(0);
368 ebp = (uint32_t *)*ebp;
369 edi = *(ebp - 1);
370 /* Shift contents of stack */
371 for (pDst = (uint32_t *)fp;
372 pDst > (((uint32_t *)edi));
373 pDst--)
374 *pDst = pDst[-delta];
375 /* Now adjust the value of %edi in our caller (kernel_trap)'s frame */
376 *(ebp - 1) = edi + (delta << 2);
377
378 retval = KERN_SUCCESS;
379 break;
380
381 default:
382 retval = KERN_FAILURE;
383 break;
384 }
385 ml_set_interrupts_enabled(oldlevel);
386 }
387
388 return retval;
389 }
390
391 /*ARGSUSED*/
392 static void
393 __fbt_provide_module(void *arg, struct modctl *ctl)
394 {
395 #pragma unused(arg)
396 struct mach_header *mh;
397 struct load_command *cmd;
398 struct segment_command *orig_ts = NULL, *orig_le = NULL;
399 struct symtab_command *orig_st = NULL;
400 struct nlist *sym = NULL;
401 char *strings;
402 uintptr_t instrLow, instrHigh;
403 char *modname;
404 unsigned int i, j;
405
406 int gIgnoreFBTBlacklist = 0;
407 PE_parse_boot_argn("IgnoreFBTBlacklist", &gIgnoreFBTBlacklist, sizeof (gIgnoreFBTBlacklist));
408
409 mh = (struct mach_header *)(ctl->address);
410 modname = ctl->mod_modname;
411
412 if (0 == ctl->address || 0 == ctl->size) /* Has the linker been jettisoned? */
413 return;
414
415 /*
416 * Employees of dtrace and their families are ineligible. Void
417 * where prohibited.
418 */
419
420 if (strcmp(modname, "com.apple.driver.dtrace") == 0)
421 return;
422
423 if (strstr(modname, "CHUD") != NULL)
424 return;
425
426 if (mh->magic != MH_MAGIC)
427 return;
428
429 cmd = (struct load_command *) &mh[1];
430 for (i = 0; i < mh->ncmds; i++) {
431 if (cmd->cmd == LC_SEGMENT) {
432 struct segment_command *orig_sg = (struct segment_command *) cmd;
433
434 if (strcmp(SEG_TEXT, orig_sg->segname) == 0)
435 orig_ts = orig_sg;
436 else if (strcmp(SEG_LINKEDIT, orig_sg->segname) == 0)
437 orig_le = orig_sg;
438 else if (strcmp("", orig_sg->segname) == 0)
439 orig_ts = orig_sg; /* kexts have a single unnamed segment */
440 }
441 else if (cmd->cmd == LC_SYMTAB)
442 orig_st = (struct symtab_command *) cmd;
443
444 cmd = (struct load_command *) ((caddr_t) cmd + cmd->cmdsize);
445 }
446
447 if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL))
448 return;
449
450 sym = (struct nlist *)orig_le->vmaddr;
451 strings = ((char *)sym) + orig_st->nsyms * sizeof(struct nlist);
452
453 /* Find extent of the TEXT section */
454 instrLow = (uintptr_t)orig_ts->vmaddr;
455 instrHigh = (uintptr_t)(orig_ts->vmaddr + orig_ts->vmsize);
456
457 for (i = 0; i < orig_st->nsyms; i++) {
458 fbt_probe_t *fbt, *retfbt;
459 machine_inst_t *instr, *limit, theInstr, i1, i2;
460 uint8_t n_type = sym[i].n_type & (N_TYPE | N_EXT);
461 char *name = strings + sym[i].n_un.n_strx;
462 int size;
463
464 /* Check that the symbol is a global and that it has a name. */
465 if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type))
466 continue;
467
468 if (0 == sym[i].n_un.n_strx) /* iff a null, "", name. */
469 continue;
470
471 /* Lop off omnipresent leading underscore. */
472 if (*name == '_')
473 name += 1;
474
475 if (strstr(name, "dtrace_") == name &&
476 strstr(name, "dtrace_safe_") != name) {
477 /*
478 * Anything beginning with "dtrace_" may be called
479 * from probe context unless it explitly indicates
480 * that it won't be called from probe context by
481 * using the prefix "dtrace_safe_".
482 */
483 continue;
484 }
485
486 if (strstr(name, "dsmos_") == name)
487 continue; /* Don't Steal Mac OS X! */
488
489 if (strstr(name, "dtxnu_") == name ||
490 strstr(name, "_dtrace") == name)
491 continue; /* Shims in dtrace.c */
492
493 if (strstr(name, "chud") == name)
494 continue; /* Professional courtesy. */
495
496 if (strstr(name, "hibernate_") == name)
497 continue; /* Let sleeping dogs lie. */
498
499 if (0 == strcmp(name, "ZN9IOService14newTemperatureElPS_") || /* IOService::newTemperature */
500 0 == strcmp(name, "ZN9IOService26temperatureCriticalForZoneEPS_")) /* IOService::temperatureCriticalForZone */
501 continue; /* Per the fire code */
502
503 /*
504 * Place no probes (illegal instructions) in the exception handling path!
505 */
506 if (0 == strcmp(name, "t_invop") ||
507 0 == strcmp(name, "enter_lohandler") ||
508 0 == strcmp(name, "lo_alltraps") ||
509 0 == strcmp(name, "kernel_trap") ||
510 0 == strcmp(name, "i386_astintr"))
511 continue;
512
513 if (0 == strcmp(name, "current_thread") ||
514 0 == strcmp(name, "ast_pending") ||
515 0 == strcmp(name, "fbt_perfCallback") ||
516 0 == strcmp(name, "machine_thread_get_kern_state") ||
517 0 == strcmp(name, "ml_set_interrupts_enabled") ||
518 0 == strcmp(name, "dtrace_invop") ||
519 0 == strcmp(name, "fbt_invop") ||
520 0 == strcmp(name, "sdt_invop") ||
521 0 == strcmp(name, "max_valid_stack_address"))
522 continue;
523
524 /*
525 * Voodoo.
526 */
527 if (strstr(name, "machine_stack_") == name ||
528 strstr(name, "mapping_") == name ||
529 0 == strcmp(name, "tmrCvt") ||
530
531 strstr(name, "tsc_") == name ||
532
533 strstr(name, "pmCPU") == name ||
534 0 == strcmp(name, "Cstate_table_set") ||
535 0 == strcmp(name, "pmKextRegister") ||
536 0 == strcmp(name, "pmSafeMode") ||
537 0 == strcmp(name, "pmUnregister") ||
538 strstr(name, "pms") == name ||
539 0 == strcmp(name, "power_management_init") ||
540 strstr(name, "usimple_") == name ||
541
542 strstr(name, "rtc_") == name ||
543 strstr(name, "_rtc_") == name ||
544 strstr(name, "rtclock_") == name ||
545 strstr(name, "clock_") == name ||
546 strstr(name, "absolutetime_to_") == name ||
547 0 == strcmp(name, "setPop") ||
548 0 == strcmp(name, "nanoseconds_to_absolutetime") ||
549 0 == strcmp(name, "nanotime_to_absolutetime") ||
550
551 strstr(name, "etimer_") == name ||
552
553 strstr(name, "commpage_") == name ||
554 strstr(name, "pmap_") == name ||
555 strstr(name, "ml_") == name ||
556 strstr(name, "PE_") == name ||
557 strstr(name, "lapic_") == name ||
558 strstr(name, "acpi_") == name)
559 continue;
560
561 /*
562 * Avoid machine_ routines. PR_5346750.
563 */
564 if (strstr(name, "machine_") == name)
565 continue;
566
567 if (0 == strcmp(name, "handle_pending_TLB_flushes"))
568 continue;
569
570 /*
571 * Place no probes on critical routines. PR_5221096
572 */
573 if (!gIgnoreFBTBlacklist &&
574 bsearch( name, critical_blacklist, CRITICAL_BLACKLIST_COUNT, sizeof(name), _cmp ) != NULL)
575 continue;
576
577 /*
578 * Place no probes that could be hit in probe context.
579 */
580 if (!gIgnoreFBTBlacklist &&
581 bsearch( name, probe_ctx_closure, PROBE_CTX_CLOSURE_COUNT, sizeof(name), _cmp ) != NULL)
582 continue;
583
584 /*
585 * Place no probes that could be hit on the way to the debugger.
586 */
587 if (strstr(name, "kdp_") == name ||
588 strstr(name, "kdb_") == name ||
589 strstr(name, "kdbg_") == name ||
590 strstr(name, "kdebug_") == name ||
591 0 == strcmp(name, "kernel_debug") ||
592 0 == strcmp(name, "Debugger") ||
593 0 == strcmp(name, "Call_DebuggerC") ||
594 0 == strcmp(name, "lock_debugger") ||
595 0 == strcmp(name, "unlock_debugger") ||
596 0 == strcmp(name, "SysChoked"))
597 continue;
598
599 /*
600 * Place no probes that could be hit on the way to a panic.
601 */
602 if (NULL != strstr(name, "panic_") ||
603 0 == strcmp(name, "panic") ||
604 0 == strcmp(name, "handleMck") ||
605 0 == strcmp(name, "unresolved_kernel_trap"))
606 continue;
607
608 if (dtrace_probe_lookup(fbt_id, modname, name, NULL) != 0)
609 continue;
610
611 for (j = 0, instr = (machine_inst_t *)sym[i].n_value, theInstr = 0;
612 (j < 4) && ((uintptr_t)instr >= instrLow) && (instrHigh > (uintptr_t)(instr + 2));
613 j++) {
614 theInstr = instr[0];
615 if (theInstr == FBT_PUSHL_EBP || theInstr == FBT_RET || theInstr == FBT_RET_IMM16)
616 break;
617
618 if ((size = dtrace_instr_size(instr)) <= 0)
619 break;
620
621 instr += size;
622 }
623
624 if (theInstr != FBT_PUSHL_EBP)
625 continue;
626
627 i1 = instr[1];
628 i2 = instr[2];
629
630 limit = (machine_inst_t *)instrHigh;
631
632 if ((i1 == FBT_MOVL_ESP_EBP0_V0 && i2 == FBT_MOVL_ESP_EBP1_V0) ||
633 (i1 == FBT_MOVL_ESP_EBP0_V1 && i2 == FBT_MOVL_ESP_EBP1_V1)) {
634 instr += 1; /* Advance to the movl %esp,%ebp */
635 theInstr = i1;
636 } else {
637 /*
638 * Sometimes, the compiler will schedule an intervening instruction
639 * in the function prologue. Example:
640 *
641 * _mach_vm_read:
642 * 000006d8 pushl %ebp
643 * 000006d9 movl $0x00000004,%edx
644 * 000006de movl %esp,%ebp
645 *
646 * Try the next instruction, to see if it is a movl %esp,%ebp
647 */
648
649 instr += 1; /* Advance past the pushl %ebp */
650 if ((size = dtrace_instr_size(instr)) <= 0)
651 continue;
652
653 instr += size;
654
655 if ((instr + 1) >= limit)
656 continue;
657
658 i1 = instr[0];
659 i2 = instr[1];
660
661 if (!(i1 == FBT_MOVL_ESP_EBP0_V0 && i2 == FBT_MOVL_ESP_EBP1_V0) &&
662 !(i1 == FBT_MOVL_ESP_EBP0_V1 && i2 == FBT_MOVL_ESP_EBP1_V1))
663 continue;
664
665 /* instr already points at the movl %esp,%ebp */
666 theInstr = i1;
667 }
668
669 fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
670 strlcpy( (char *)&(fbt->fbtp_name), name, MAX_FBTP_NAME_CHARS );
671 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, name, FBT_ENTRY, FBT_AFRAMES_ENTRY, fbt);
672 fbt->fbtp_patchpoint = instr;
673 fbt->fbtp_ctl = ctl;
674 fbt->fbtp_loadcnt = ctl->mod_loadcnt;
675 fbt->fbtp_rval = DTRACE_INVOP_MOVL_ESP_EBP;
676 fbt->fbtp_savedval = theInstr;
677 fbt->fbtp_patchval = FBT_PATCHVAL;
678
679 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
680 fbt->fbtp_symndx = i;
681 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
682
683 retfbt = NULL;
684 again:
685 if (instr >= limit)
686 continue;
687
688 /*
689 * If this disassembly fails, then we've likely walked off into
690 * a jump table or some other unsuitable area. Bail out of the
691 * disassembly now.
692 */
693 if ((size = dtrace_instr_size(instr)) <= 0)
694 continue;
695
696 /*
697 * We (desperately) want to avoid erroneously instrumenting a
698 * jump table, especially given that our markers are pretty
699 * short: two bytes on x86, and just one byte on amd64. To
700 * determine if we're looking at a true instruction sequence
701 * or an inline jump table that happens to contain the same
702 * byte sequences, we resort to some heuristic sleeze: we
703 * treat this instruction as being contained within a pointer,
704 * and see if that pointer points to within the body of the
705 * function. If it does, we refuse to instrument it.
706 */
707 for (j = 0; j < sizeof (uintptr_t); j++) {
708 uintptr_t check = (uintptr_t)instr - j;
709 uint8_t *ptr;
710
711 if (check < sym[i].n_value)
712 break;
713
714 if (check + sizeof (uintptr_t) > (uintptr_t)limit)
715 continue;
716
717 ptr = *(uint8_t **)check;
718
719 if (ptr >= (uint8_t *)sym[i].n_value && ptr < limit) {
720 instr += size;
721 goto again;
722 }
723 }
724
725 /*
726 * OK, it's an instruction.
727 */
728 theInstr = instr[0];
729
730 /* Walked onto the start of the next routine? If so, bail out of this function. */
731 if (theInstr == FBT_PUSHL_EBP)
732 continue;
733
734 if (!(size == 1 && (theInstr == FBT_POPL_EBP || theInstr == FBT_LEAVE))) {
735 instr += size;
736 goto again;
737 }
738
739 /*
740 * Found the popl %ebp; or leave.
741 */
742 machine_inst_t *patch_instr = instr;
743
744 /*
745 * Scan forward for a "ret", or "jmp".
746 */
747 instr += size;
748 if (instr >= limit)
749 continue;
750
751 size = dtrace_instr_size(instr);
752 if (size <= 0) /* Failed instruction decode? */
753 continue;
754
755 theInstr = instr[0];
756
757 if (!(size == FBT_RET_LEN && (theInstr == FBT_RET)) &&
758 !(size == FBT_RET_IMM16_LEN && (theInstr == FBT_RET_IMM16)) &&
759 !(size == FBT_JMP_SHORT_REL_LEN && (theInstr == FBT_JMP_SHORT_REL)) &&
760 !(size == FBT_JMP_NEAR_REL_LEN && (theInstr == FBT_JMP_NEAR_REL)) &&
761 !(size == FBT_JMP_FAR_ABS_LEN && (theInstr == FBT_JMP_FAR_ABS)))
762 continue;
763
764 /*
765 * popl %ebp; ret; or leave; ret; or leave; jmp tailCalledFun; -- We have a winner!
766 */
767 fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
768 strlcpy( (char *)&(fbt->fbtp_name), name, MAX_FBTP_NAME_CHARS );
769
770 if (retfbt == NULL) {
771 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
772 name, FBT_RETURN, FBT_AFRAMES_RETURN, fbt);
773 } else {
774 retfbt->fbtp_next = fbt;
775 fbt->fbtp_id = retfbt->fbtp_id;
776 }
777
778 retfbt = fbt;
779 fbt->fbtp_patchpoint = patch_instr;
780 fbt->fbtp_ctl = ctl;
781 fbt->fbtp_loadcnt = ctl->mod_loadcnt;
782
783 if (*patch_instr == FBT_POPL_EBP) {
784 fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
785 } else {
786 ASSERT(*patch_instr == FBT_LEAVE);
787 fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
788 }
789 fbt->fbtp_roffset =
790 (uintptr_t)(patch_instr - (uint8_t *)sym[i].n_value);
791
792 fbt->fbtp_savedval = *patch_instr;
793 fbt->fbtp_patchval = FBT_PATCHVAL;
794 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(patch_instr)];
795 fbt->fbtp_symndx = i;
796 fbt_probetab[FBT_ADDR2NDX(patch_instr)] = fbt;
797
798 instr += size;
799 goto again;
800 }
801 }
802
803 extern struct modctl g_fbt_kernctl;
804 #undef kmem_alloc /* from its binding to dt_kmem_alloc glue */
805 #undef kmem_free /* from its binding to dt_kmem_free glue */
806 #include <vm/vm_kern.h>
807
808 /*ARGSUSED*/
809 void
810 fbt_provide_module(void *arg, struct modctl *ctl)
811 {
812 #pragma unused(ctl)
813 __fbt_provide_module(arg, &g_fbt_kernctl);
814
815 kmem_free(kernel_map, (vm_offset_t)g_fbt_kernctl.address, round_page_32(g_fbt_kernctl.size));
816 g_fbt_kernctl.address = 0;
817 g_fbt_kernctl.size = 0;
818 }