2 * Copyright (c) 2005-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <kern/thread.h>
30 #include <mach/thread_status.h>
32 typedef x86_saved_state_t savearea_t
;
36 #include <sys/malloc.h>
38 #include <sys/systm.h>
40 #include <sys/proc_internal.h>
41 #include <sys/kauth.h>
42 #include <sys/dtrace.h>
43 #include <sys/dtrace_impl.h>
44 #include <libkern/OSAtomic.h>
45 #include <kern/thread_call.h>
46 #include <kern/task.h>
47 #include <kern/sched_prim.h>
48 #include <miscfs/devfs/devfs.h>
49 #include <mach/vm_param.h>
50 #include <machine/pal_routines.h>
54 * APPLE NOTE: The regmap is used to decode which 64bit uregs[] register
55 * is being accessed when passed the 32bit uregs[] constant (based on
56 * the reg.d translator file). The dtrace_getreg() is smart enough to handle
57 * the register mappings. The register set definitions are the same as
58 * those used by the fasttrap_getreg code.
60 #include "fasttrap_regset.h"
61 static const uint8_t regmap
[19] = {
68 REG_RBP
, /* EBP, REG_FP */
71 REG_RDX
, /* EDX, REG_R1 */
73 REG_RAX
, /* EAX, REG_R0 */
74 REG_TRAPNO
, /* TRAPNO */
76 REG_RIP
, /* EIP, REG_PC */
78 REG_RFL
, /* EFL, REG_PS */
79 REG_RSP
, /* UESP, REG_SP */
83 extern dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
86 dtrace_probe_error(dtrace_state_t
*state
, dtrace_epid_t epid
, int which
,
87 int fltoffs
, int fault
, uint64_t illval
)
90 * For the case of the error probe firing lets
91 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
93 state
->dts_arg_error_illval
= illval
;
94 dtrace_probe( dtrace_probeid_error
, (uint64_t)(uintptr_t)state
, epid
, which
, fltoffs
, fault
);
98 * Atomicity and synchronization
101 dtrace_membar_producer(void)
103 __asm__
volatile("sfence");
107 dtrace_membar_consumer(void)
109 __asm__
volatile("lfence");
113 * Interrupt manipulation
114 * XXX dtrace_getipl() can be called from probe context.
120 * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE
121 * in osfmk/kern/cpu_data.h
123 /* return get_interrupt_level(); */
124 return (ml_at_interrupt_context() ? 1: 0);
130 typedef struct xcArg
{
137 xcRemote( void *foo
)
139 xcArg_t
*pArg
= (xcArg_t
*)foo
;
141 if ( pArg
->cpu
== CPU
->cpu_id
|| pArg
->cpu
== DTRACE_CPUALL
) {
142 (pArg
->f
)(pArg
->arg
);
148 * dtrace_xcall() is not called from probe context.
151 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t f
, void *arg
)
159 if (cpu
== DTRACE_CPUALL
) {
160 mp_cpus_call (CPUMASK_ALL
, ASYNC
, xcRemote
, (void*)&xcArg
);
163 mp_cpus_call (cpu_to_cpumask((cpu_t
)cpu
), ASYNC
, xcRemote
, (void*)&xcArg
);
171 dtrace_isa_init(void)
180 dtrace_getreg(struct regs
*savearea
, uint_t reg
)
182 boolean_t is64Bit
= proc_is64bit(current_proc());
183 x86_saved_state_t
*regs
= (x86_saved_state_t
*)savearea
;
186 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
199 return (uint64_t)(regs
->ss_64
.rdi
);
201 return (uint64_t)(regs
->ss_64
.rsi
);
203 return (uint64_t)(regs
->ss_64
.rdx
);
205 return (uint64_t)(regs
->ss_64
.rcx
);
207 return (uint64_t)(regs
->ss_64
.r8
);
209 return (uint64_t)(regs
->ss_64
.r9
);
211 return (uint64_t)(regs
->ss_64
.rax
);
213 return (uint64_t)(regs
->ss_64
.rbx
);
215 return (uint64_t)(regs
->ss_64
.rbp
);
217 return (uint64_t)(regs
->ss_64
.r10
);
219 return (uint64_t)(regs
->ss_64
.r11
);
221 return (uint64_t)(regs
->ss_64
.r12
);
223 return (uint64_t)(regs
->ss_64
.r13
);
225 return (uint64_t)(regs
->ss_64
.r14
);
227 return (uint64_t)(regs
->ss_64
.r15
);
229 return (uint64_t)(regs
->ss_64
.fs
);
231 return (uint64_t)(regs
->ss_64
.gs
);
233 return (uint64_t)(regs
->ss_64
.isf
.trapno
);
235 return (uint64_t)(regs
->ss_64
.isf
.err
);
237 return (uint64_t)(regs
->ss_64
.isf
.rip
);
239 return (uint64_t)(regs
->ss_64
.isf
.cs
);
241 return (uint64_t)(regs
->ss_64
.isf
.ss
);
243 return (uint64_t)(regs
->ss_64
.isf
.rflags
);
245 return (uint64_t)(regs
->ss_64
.isf
.rsp
);
249 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
253 } else { /* is 32bit user */
254 /* beyond register SS */
255 if (reg
> x86_SAVED_STATE32_COUNT
- 1) {
256 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
259 return (uint64_t)((unsigned int *)(&(regs
->ss_32
.gs
)))[reg
];
263 #define RETURN_OFFSET 4
264 #define RETURN_OFFSET64 8
267 dtrace_getustack_common(uint64_t *pcstack
, int pcstack_limit
, user_addr_t pc
,
271 volatile uint16_t *flags
=
272 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
274 uintptr_t oldcontext
= lwp
->lwp_oldcontext
; /* XXX signal stack crawl */
278 boolean_t is64Bit
= proc_is64bit(current_proc());
280 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
282 #if 0 /* XXX signal stack crawl */
283 if (p
->p_model
== DATAMODEL_NATIVE
) {
284 s1
= sizeof (struct frame
) + 2 * sizeof (long);
285 s2
= s1
+ sizeof (siginfo_t
);
287 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
288 s2
= s1
+ sizeof (siginfo32_t
);
294 if (pcstack
!= NULL
) {
295 *pcstack
++ = (uint64_t)pc
;
297 if (pcstack_limit
<= 0)
304 #if 0 /* XXX signal stack crawl */
305 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
306 if (p
->p_model
== DATAMODEL_NATIVE
) {
307 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
308 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
310 sp
= dtrace_fulword(&gregs
[REG_FP
]);
311 pc
= dtrace_fulword(&gregs
[REG_PC
]);
313 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
315 ucontext32_t
*ucp
= (ucontext32_t
*)oldcontext
;
316 greg32_t
*gregs
= ucp
->uc_mcontext
.gregs
;
318 sp
= dtrace_fuword32(&gregs
[EBP
]);
319 pc
= dtrace_fuword32(&gregs
[EIP
]);
321 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
328 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
329 sp
= dtrace_fuword64(sp
);
331 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
332 sp
= dtrace_fuword32(sp
);
338 * This is totally bogus: if we faulted, we're going to clear
339 * the fault and break. This is to deal with the apparently
340 * broken Java stacks on x86.
342 if (*flags
& CPU_DTRACE_FAULT
) {
343 *flags
&= ~CPU_DTRACE_FAULT
;
354 * The return value indicates if we've modified the stack.
357 dtrace_adjust_stack(uint64_t **pcstack
, int *pcstack_limit
, user_addr_t
*pc
,
362 boolean_t is64Bit
= proc_is64bit(current_proc());
366 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
368 * If we found ourselves in an entry probe, the frame pointer has not
369 * yet been pushed (that happens in the
370 * function prologue). The best approach is to
371 * add the current pc as a missing top of stack,
372 * and back the pc up to the caller, which is stored at the
373 * current stack pointer address since the call
374 * instruction puts it there right before
381 *pc
= dtrace_fuword64(sp
);
383 *pc
= dtrace_fuword32(sp
);
386 * We might have a top of stack override, in which case we just
387 * add that frame without question to the top. This
388 * happens in return probes where you have a valid
389 * frame pointer, but it's for the callers frame
390 * and you'd like to add the pc of the return site
393 missing_tos
= cpu_core
[CPU
->cpu_id
].cpuc_missing_tos
;
396 if (missing_tos
!= 0) {
397 if (pcstack
!= NULL
&& pcstack_limit
!= NULL
) {
399 * If the missing top of stack has been filled out, then
400 * we add it and adjust the size.
402 *(*pcstack
)++ = missing_tos
;
406 * return 1 because we would have changed the
407 * stack whether or not it was passed in. This
408 * ensures the stack count is correct
416 dtrace_getupcstack(uint64_t *pcstack
, int pcstack_limit
)
418 thread_t thread
= current_thread();
419 x86_saved_state_t
*regs
;
420 user_addr_t pc
, sp
, fp
;
421 volatile uint16_t *flags
=
422 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
424 boolean_t is64Bit
= proc_is64bit(current_proc());
426 if (*flags
& CPU_DTRACE_FAULT
)
429 if (pcstack_limit
<= 0)
433 * If there's no user context we still need to zero the stack.
438 pal_register_cache_state(thread
, VALID
);
439 regs
= (x86_saved_state_t
*)find_user_regs(thread
);
443 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
446 if (pcstack_limit
<= 0)
450 pc
= regs
->ss_64
.isf
.rip
;
451 sp
= regs
->ss_64
.isf
.rsp
;
452 fp
= regs
->ss_64
.rbp
;
454 pc
= regs
->ss_32
.eip
;
455 sp
= regs
->ss_32
.uesp
;
456 fp
= regs
->ss_32
.ebp
;
460 * The return value indicates if we've modified the stack.
461 * Since there is nothing else to fix up in either case,
462 * we can safely ignore it here.
464 (void)dtrace_adjust_stack(&pcstack
, &pcstack_limit
, &pc
, sp
);
466 if(pcstack_limit
<= 0)
470 * Note that unlike ppc, the x86 code does not use
471 * CPU_DTRACE_USTACK_FP. This is because x86 always
472 * traces from the fp, even in syscall/profile/fbt
475 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, fp
);
477 ASSERT(n
<= pcstack_limit
);
483 while (pcstack_limit
-- > 0)
488 dtrace_getustackdepth(void)
490 thread_t thread
= current_thread();
491 x86_saved_state_t
*regs
;
492 user_addr_t pc
, sp
, fp
;
494 boolean_t is64Bit
= proc_is64bit(current_proc());
499 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
502 pal_register_cache_state(thread
, VALID
);
503 regs
= (x86_saved_state_t
*)find_user_regs(thread
);
508 pc
= regs
->ss_64
.isf
.rip
;
509 sp
= regs
->ss_64
.isf
.rsp
;
510 fp
= regs
->ss_64
.rbp
;
512 pc
= regs
->ss_32
.eip
;
513 sp
= regs
->ss_32
.uesp
;
514 fp
= regs
->ss_32
.ebp
;
517 if (dtrace_adjust_stack(NULL
, NULL
, &pc
, sp
) == 1) {
519 * we would have adjusted the stack if we had
520 * supplied one (that is what rc == 1 means).
521 * Also, as a side effect, the pc might have
522 * been fixed up, which is good for calling
523 * in to dtrace_getustack_common.
529 * Note that unlike ppc, the x86 code does not use
530 * CPU_DTRACE_USTACK_FP. This is because x86 always
531 * traces from the fp, even in syscall/profile/fbt
535 n
+= dtrace_getustack_common(NULL
, 0, pc
, fp
);
541 dtrace_getufpstack(uint64_t *pcstack
, uint64_t *fpstack
, int pcstack_limit
)
543 thread_t thread
= current_thread();
546 volatile uint16_t *flags
=
547 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
549 uintptr_t oldcontext
;
552 boolean_t is64Bit
= proc_is64bit(current_proc());
554 if (*flags
& CPU_DTRACE_FAULT
)
557 if (pcstack_limit
<= 0)
561 * If there's no user context we still need to zero the stack.
566 regs
= (savearea_t
*)find_user_regs(thread
);
570 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
573 if (pcstack_limit
<= 0)
576 pc
= regs
->ss_32
.eip
;
577 sp
= regs
->ss_32
.ebp
;
579 #if 0 /* XXX signal stack crawl */
580 oldcontext
= lwp
->lwp_oldcontext
;
582 if (p
->p_model
== DATAMODEL_NATIVE
) {
583 s1
= sizeof (struct frame
) + 2 * sizeof (long);
584 s2
= s1
+ sizeof (siginfo_t
);
586 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
587 s2
= s1
+ sizeof (siginfo32_t
);
591 if(dtrace_adjust_stack(&pcstack
, &pcstack_limit
, &pc
, sp
) == 1) {
596 if (pcstack_limit
<= 0)
601 *pcstack
++ = (uint64_t)pc
;
604 if (pcstack_limit
<= 0)
610 #if 0 /* XXX signal stack crawl */
611 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
612 if (p
->p_model
== DATAMODEL_NATIVE
) {
613 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
614 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
616 sp
= dtrace_fulword(&gregs
[REG_FP
]);
617 pc
= dtrace_fulword(&gregs
[REG_PC
]);
619 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
621 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
622 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
624 sp
= dtrace_fuword32(&gregs
[EBP
]);
625 pc
= dtrace_fuword32(&gregs
[EIP
]);
627 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
634 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
635 sp
= dtrace_fuword64(sp
);
637 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
638 sp
= dtrace_fuword32(sp
);
644 * This is totally bogus: if we faulted, we're going to clear
645 * the fault and break. This is to deal with the apparently
646 * broken Java stacks on x86.
648 if (*flags
& CPU_DTRACE_FAULT
) {
649 *flags
&= ~CPU_DTRACE_FAULT
;
656 while (pcstack_limit
-- > 0)
661 dtrace_getpcstack(pc_t
*pcstack
, int pcstack_limit
, int aframes
,
664 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
665 struct frame
*nextfp
, *minfp
, *stacktop
;
669 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
672 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
673 stacktop
= (struct frame
*)dtrace_get_cpu_int_stack_top();
675 stacktop
= (struct frame
*)(dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
);
681 if (intrpc
!= NULL
&& depth
< pcstack_limit
)
682 pcstack
[depth
++] = (pc_t
)intrpc
;
684 while (depth
< pcstack_limit
) {
685 nextfp
= *(struct frame
**)fp
;
686 pc
= *(uintptr_t *)(((uintptr_t)fp
) + RETURN_OFFSET64
);
688 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
691 * Hop from interrupt stack to thread stack.
693 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
695 minfp
= (struct frame
*)kstack_base
;
696 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
702 * This is the last frame we can process; indicate
703 * that we should return after processing this frame.
709 if (--aframes
== 0 && caller
!= 0) {
711 * We've just run out of artificial frames,
712 * and we have a valid caller -- fill it in
715 ASSERT(depth
< pcstack_limit
);
716 pcstack
[depth
++] = (pc_t
)caller
;
720 if (depth
< pcstack_limit
)
721 pcstack
[depth
++] = (pc_t
)pc
;
725 while (depth
< pcstack_limit
)
726 pcstack
[depth
++] = 0;
736 struct frame
*backchain
;
741 dtrace_getarg(int arg
, int aframes
, dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
744 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
751 * A total of 6 arguments are passed via registers; any argument with
752 * index of 5 or lower is therefore in a register.
756 for (i
= 1; i
<= aframes
; i
++) {
760 if (dtrace_invop_callsite_pre
!= NULL
761 && pc
> (uintptr_t)dtrace_invop_callsite_pre
762 && pc
<= (uintptr_t)dtrace_invop_callsite_post
) {
764 * In the case of x86_64, we will use the pointer to the
765 * save area structure that was pushed when we took the
766 * trap. To get this structure, we must increment
767 * beyond the frame structure. If the
768 * argument that we're seeking is passed on the stack,
769 * we'll pull the true stack pointer out of the saved
770 * registers and decrement our argument by the number
771 * of arguments passed in registers; if the argument
772 * we're seeking is passed in regsiters, we can just
776 /* fp points to frame of dtrace_invop() activation. */
777 fp
= fp
->backchain
; /* to fbt_perfcallback() activation. */
778 fp
= fp
->backchain
; /* to kernel_trap() activation. */
779 fp
= fp
->backchain
; /* to trap_from_kernel() activation. */
781 x86_saved_state_t
*tagged_regs
= (x86_saved_state_t
*)&fp
[1];
782 x86_saved_state64_t
*saved_state
= saved_state64(tagged_regs
);
785 stack
= (uintptr_t *)(void*)&saved_state
->rdi
;
787 fp
= (struct frame
*)(saved_state
->isf
.rsp
);
788 stack
= (uintptr_t *)&fp
[1]; /* Find marshalled
797 * We know that we did not come through a trap to get into
798 * dtrace_probe() -- We arrive here when the provider has
799 * called dtrace_probe() directly.
800 * The probe ID is the first argument to dtrace_probe().
801 * We must advance beyond that to get the argX.
803 arg
++; /* Advance past probeID */
807 * This shouldn't happen. If the argument is passed in a
808 * register then it should have been, well, passed in a
811 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
816 stack
= (uintptr_t *)&fp
[1]; /* Find marshalled arguments */
819 if (dtrace_canload((uint64_t)(stack
+ arg
), sizeof(uint64_t),
821 /* dtrace_probe arguments arg0 ... arg4 are 64bits wide */
822 val
= dtrace_load64((uint64_t)(stack
+ arg
));
832 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
))
835 * "base" is the smallest toxic address in the range, "limit" is the first
836 * VALID address greater than "base".
838 func(0x0, VM_MIN_KERNEL_AND_KEXT_ADDRESS
);
839 if (VM_MAX_KERNEL_ADDRESS
< ~(uintptr_t)0)
840 func(VM_MAX_KERNEL_ADDRESS
+ 1, ~(uintptr_t)0);