2 * Copyright (c) 2005-2008 Apple Computer, 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 #define MACH__POSIX_C_SOURCE_PRIVATE 1 /* pulls in suitable savearea from
30 * mach/ppc/thread_status.h */
31 #include <arm/caches_internal.h>
32 #include <arm/proc_reg.h>
34 #include <kern/thread.h>
35 #include <mach/thread_status.h>
37 #if __has_include(<ptrauth.h>)
42 #include <sys/malloc.h>
44 #include <sys/systm.h>
46 #include <sys/proc_internal.h>
47 #include <sys/kauth.h>
48 #include <sys/dtrace.h>
49 #include <sys/dtrace_impl.h>
50 #include <libkern/OSAtomic.h>
51 #include <kern/simple_lock.h>
52 #include <kern/sched_prim.h> /* for thread_wakeup() */
53 #include <kern/thread_call.h>
54 #include <kern/task.h>
55 #include <miscfs/devfs/devfs.h>
56 #include <mach/vm_param.h>
58 extern struct arm_saved_state
*find_kern_regs(thread_t
);
60 extern dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
61 typedef arm_saved_state_t savearea_t
;
63 extern lck_attr_t
*dtrace_lck_attr
;
64 extern lck_grp_t
*dtrace_lck_grp
;
68 struct frame
*backchain
;
73 * Atomicity and synchronization
76 dtrace_membar_producer(void)
79 __asm__
volatile ("dmb ish" : : : "memory");
81 __asm__
volatile ("nop" : : : "memory");
86 dtrace_membar_consumer(void)
89 __asm__
volatile ("dmb ish" : : : "memory");
91 __asm__
volatile ("nop" : : : "memory");
96 * Interrupt manipulation
97 * XXX dtrace_getipl() can be called from probe context.
103 * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE
104 * in osfmk/kern/cpu_data.h
106 /* return get_interrupt_level(); */
107 return ml_at_interrupt_context() ? 1 : 0;
115 decl_lck_mtx_data(static, dt_xc_lock
);
116 static uint32_t dt_xc_sync
;
118 typedef struct xcArg
{
127 xcArg_t
*pArg
= (xcArg_t
*) foo
;
129 if (pArg
->cpu
== CPU
->cpu_id
|| pArg
->cpu
== DTRACE_CPUALL
) {
130 (pArg
->f
)(pArg
->arg
);
133 if (hw_atomic_sub(&dt_xc_sync
, 1) == 0) {
134 thread_wakeup((event_t
) &dt_xc_sync
);
140 * dtrace_xcall() is not called from probe context.
143 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t f
, void *arg
)
146 /* Only one dtrace_xcall in flight allowed */
147 lck_mtx_lock(&dt_xc_lock
);
155 cpu_broadcast_xcall(&dt_xc_sync
, TRUE
, xcRemote
, (void*) &xcArg
);
157 lck_mtx_unlock(&dt_xc_lock
);
161 /* On uniprocessor systems, the cpu should always be either ourselves or all */
162 ASSERT(cpu
== CPU
->cpu_id
|| cpu
== DTRACE_CPUALL
);
173 dtrace_isa_init(void)
175 lck_mtx_init(&dt_xc_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
181 * Register definitions
193 #define ARM64_CPSR 33
199 dtrace_getreg(struct regs
* savearea
, uint_t reg
)
201 struct arm_saved_state
*regs
= (struct arm_saved_state
*) savearea
;
204 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
208 if (is_saved_state32(regs
)) {
209 // Fix special registers if user is 32 bits
229 if (!check_saved_state_reglimit(regs
, reg
)) {
230 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
234 return (uint64_t)get_saved_state_reg(regs
, reg
);
237 #define RETURN_OFFSET 4
238 #define RETURN_OFFSET64 8
241 dtrace_getustack_common(uint64_t * pcstack
, int pcstack_limit
, user_addr_t pc
,
245 boolean_t is64bit
= proc_is64bit_data(current_proc());
247 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
251 if (pcstack
!= NULL
) {
252 *pcstack
++ = (uint64_t) pc
;
254 if (pcstack_limit
<= 0) {
264 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
265 sp
= dtrace_fuword64(sp
);
267 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
268 sp
= dtrace_fuword32(sp
);
276 dtrace_getupcstack(uint64_t * pcstack
, int pcstack_limit
)
278 thread_t thread
= current_thread();
280 user_addr_t pc
, sp
, fp
;
281 volatile uint16_t *flags
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
284 if (*flags
& CPU_DTRACE_FAULT
) {
288 if (pcstack_limit
<= 0) {
293 * If there's no user context we still need to zero the stack.
295 if (thread
== NULL
) {
299 regs
= (savearea_t
*) find_user_regs(thread
);
304 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
307 if (pcstack_limit
<= 0) {
311 pc
= get_saved_state_pc(regs
);
312 sp
= get_saved_state_sp(regs
);
313 fp
= get_saved_state_fp(regs
);
315 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
316 *pcstack
++ = (uint64_t) pc
;
318 if (pcstack_limit
<= 0) {
322 pc
= get_saved_state_lr(regs
);
325 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, fp
);
328 ASSERT(n
<= pcstack_limit
);
334 while (pcstack_limit
-- > 0) {
340 dtrace_getustackdepth(void)
342 thread_t thread
= current_thread();
344 user_addr_t pc
, sp
, fp
;
347 if (thread
== NULL
) {
351 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
)) {
355 regs
= (savearea_t
*) find_user_regs(thread
);
360 pc
= get_saved_state_pc(regs
);
361 sp
= get_saved_state_sp(regs
);
362 fp
= get_saved_state_fp(regs
);
364 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
366 pc
= get_saved_state_lr(regs
);
370 * Note that unlike ppc, the arm code does not use
371 * CPU_DTRACE_USTACK_FP. This is because arm always
372 * traces from the sp, even in syscall/profile/fbt
376 n
+= dtrace_getustack_common(NULL
, 0, pc
, fp
);
382 dtrace_getufpstack(uint64_t * pcstack
, uint64_t * fpstack
, int pcstack_limit
)
384 thread_t thread
= current_thread();
385 boolean_t is64bit
= proc_is64bit_data(current_proc());
388 volatile uint16_t *flags
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
391 uintptr_t oldcontext
;
395 if (*flags
& CPU_DTRACE_FAULT
) {
399 if (pcstack_limit
<= 0) {
404 * If there's no user context we still need to zero the stack.
406 if (thread
== NULL
) {
410 regs
= (savearea_t
*) find_user_regs(thread
);
415 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
418 if (pcstack_limit
<= 0) {
422 pc
= get_saved_state_pc(regs
);
423 sp
= get_saved_state_lr(regs
);
425 #if 0 /* XXX signal stack crawl */
426 oldcontext
= lwp
->lwp_oldcontext
;
428 if (p
->p_model
== DATAMODEL_NATIVE
) {
429 s1
= sizeof(struct frame
) + 2 * sizeof(long);
430 s2
= s1
+ sizeof(siginfo_t
);
432 s1
= sizeof(struct frame32
) + 3 * sizeof(int);
433 s2
= s1
+ sizeof(siginfo32_t
);
437 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
438 *pcstack
++ = (uint64_t) pc
;
441 if (pcstack_limit
<= 0) {
446 pc
= dtrace_fuword64(sp
);
448 pc
= dtrace_fuword32(sp
);
451 while (pc
!= 0 && sp
!= 0) {
452 *pcstack
++ = (uint64_t) pc
;
455 if (pcstack_limit
<= 0) {
459 #if 0 /* XXX signal stack crawl */
460 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
461 if (p
->p_model
== DATAMODEL_NATIVE
) {
462 ucontext_t
*ucp
= (ucontext_t
*) oldcontext
;
463 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
465 sp
= dtrace_fulword(&gregs
[REG_FP
]);
466 pc
= dtrace_fulword(&gregs
[REG_PC
]);
468 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
470 ucontext_t
*ucp
= (ucontext_t
*) oldcontext
;
471 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
473 sp
= dtrace_fuword32(&gregs
[EBP
]);
474 pc
= dtrace_fuword32(&gregs
[EIP
]);
476 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
482 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
483 sp
= dtrace_fuword64(sp
);
485 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
486 sp
= dtrace_fuword32(sp
);
493 * This is totally bogus: if we faulted, we're going to clear
494 * the fault and break. This is to deal with the apparently
495 * broken Java stacks on x86.
497 if (*flags
& CPU_DTRACE_FAULT
) {
498 *flags
&= ~CPU_DTRACE_FAULT
;
505 while (pcstack_limit
-- > 0) {
512 dtrace_getpcstack(pc_t
* pcstack
, int pcstack_limit
, int aframes
,
515 struct frame
*fp
= (struct frame
*) __builtin_frame_address(0);
516 struct frame
*nextfp
, *minfp
, *stacktop
;
521 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
523 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0) {
524 stacktop
= (struct frame
*) dtrace_get_cpu_int_stack_top();
527 stacktop
= (struct frame
*) (dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
);
534 if (intrpc
!= NULL
&& depth
< pcstack_limit
) {
535 pcstack
[depth
++] = (pc_t
) intrpc
;
538 while (depth
< pcstack_limit
) {
539 nextfp
= *(struct frame
**) fp
;
540 pc
= *(uintptr_t *) (((uintptr_t) fp
) + RETURN_OFFSET64
);
542 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
545 * Hop from interrupt stack to thread stack.
547 arm_saved_state_t
*arm_kern_regs
= (arm_saved_state_t
*) find_kern_regs(current_thread());
549 nextfp
= (struct frame
*)(saved_state64(arm_kern_regs
)->fp
);
552 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
554 minfp
= (struct frame
*)kstack_base
;
555 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
560 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
565 * If this thread was on the interrupt stack, but did not
566 * take an interrupt (i.e, the idle thread), there is no
567 * explicit saved state for us to use.
574 * This is the last frame we can process; indicate
575 * that we should return after processing this frame.
582 if (--aframes
== 0 && caller
!= (uintptr_t)NULL
) {
584 * We've just run out of artificial frames,
585 * and we have a valid caller -- fill it in
588 ASSERT(depth
< pcstack_limit
);
589 pcstack
[depth
++] = (pc_t
) caller
;
590 caller
= (uintptr_t)NULL
;
593 if (depth
< pcstack_limit
) {
594 pcstack
[depth
++] = (pc_t
) pc
;
599 while (depth
< pcstack_limit
) {
600 pcstack
[depth
++] = (pc_t
) NULL
;
610 * On arm64, we support both 32bit and 64bit user processes.
611 * This routine is only called when handling 32bit processes
612 * where thumb_mode is pertinent.
613 * If this routine is called when handling 64bit processes
614 * thumb_mode should always be zero.
617 dtrace_instr_size(uint32_t instr
, int thumb_mode
)
620 uint16_t instr16
= *(uint16_t*) &instr
;
621 if (((instr16
>> 11) & 0x1F) > 0x1C) {
632 dtrace_getarg(int arg
, int aframes
, dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
634 #pragma unused(arg, aframes)
636 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
642 * A total of 8 arguments are passed via registers; any argument with
643 * index of 7 or lower is therefore in a register.
647 for (i
= 1; i
<= aframes
; ++i
) {
649 #if __has_feature(ptrauth_returns)
650 pc
= (uintptr_t)ptrauth_strip((void*)fp
->retaddr
, ptrauth_key_return_address
);
655 if (dtrace_invop_callsite_pre
!= NULL
656 && pc
> (uintptr_t) dtrace_invop_callsite_pre
657 && pc
<= (uintptr_t) dtrace_invop_callsite_post
) {
658 /* fp points to frame of dtrace_invop() activation */
659 fp
= fp
->backchain
; /* to fbt_perfCallback activation */
660 fp
= fp
->backchain
; /* to sleh_synchronous activation */
661 fp
= fp
->backchain
; /* to fleh_synchronous activation */
663 arm_saved_state_t
*tagged_regs
= (arm_saved_state_t
*) ((void*) &fp
[1]);
664 arm_saved_state64_t
*saved_state
= saved_state64(tagged_regs
);
667 /* the argument will be found in a register */
668 stack
= (uintptr_t*) &saved_state
->x
[0];
670 /* the argument will be found in the stack */
671 fp
= (struct frame
*) saved_state
->sp
;
672 stack
= (uintptr_t*) &fp
[1];
681 * We know that we did not come through a trap to get into
682 * dtrace_probe() -- We arrive here when the provider has
683 * called dtrace_probe() directly.
684 * The probe ID is the first argument to dtrace_probe().
685 * We must advance beyond that to get the argX.
687 arg
++; /* Advance past probeID */
691 * This shouldn't happen. If the argument is passed in a
692 * register then it should have been, well, passed in a
695 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
700 stack
= (uintptr_t*) &fp
[1]; /* Find marshalled arguments */
703 if (dtrace_canload((uint64_t)(stack
+ arg
), sizeof(uint64_t),
705 /* dtrace_probe arguments arg0 ... arg4 are 64bits wide */
706 val
= dtrace_load64((uint64_t)(stack
+ arg
));
713 dtrace_probe_error(dtrace_state_t
*state
, dtrace_epid_t epid
, int which
,
714 int fltoffs
, int fault
, uint64_t illval
)
718 * For the case of the error probe firing lets
719 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
721 state
->dts_arg_error_illval
= illval
;
722 dtrace_probe( dtrace_probeid_error
, (uint64_t)(uintptr_t)state
, epid
, which
, fltoffs
, fault
);
726 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
))
728 /* XXX ARMTODO check copied from ppc/x86*/
730 * "base" is the smallest toxic address in the range, "limit" is the first
731 * VALID address greater than "base".
733 func(0x0, VM_MIN_KERNEL_ADDRESS
);
734 if (VM_MAX_KERNEL_ADDRESS
< ~(uintptr_t)0) {
735 func(VM_MAX_KERNEL_ADDRESS
+ 1, ~(uintptr_t)0);
740 dtrace_flush_caches(void)
742 /* TODO There were some problems with flushing just the cache line that had been modified.
743 * For now, we'll flush the entire cache, until we figure out how to flush just the patched block.
746 InvalidatePoU_Icache();