2 * Copyright (c) 2005-2018 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 #include <arm/caches_internal.h>
30 #include <kern/thread.h>
32 #if __has_include(<ptrauth.h>)
37 #include <sys/systm.h>
39 #include <sys/proc_internal.h>
40 #include <sys/kauth.h>
41 #include <sys/dtrace.h>
42 #include <sys/dtrace_impl.h>
43 #include <machine/atomic.h>
44 #include <kern/cambria_layout.h>
45 #include <kern/simple_lock.h>
46 #include <kern/sched_prim.h> /* for thread_wakeup() */
47 #include <kern/thread_call.h>
48 #include <kern/task.h>
50 extern struct arm_saved_state
*find_kern_regs(thread_t
);
52 extern dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
53 typedef arm_saved_state_t savearea_t
;
56 extern void * pmap_stacks_start
;
57 extern void * pmap_stacks_end
;
61 struct frame
*backchain
;
66 * Atomicity and synchronization
69 dtrace_membar_producer(void)
71 __asm__
volatile ("dmb ish" : : : "memory");
75 dtrace_membar_consumer(void)
77 __asm__
volatile ("dmb ish" : : : "memory");
81 * Interrupt manipulation
82 * XXX dtrace_getipl() can be called from probe context.
88 * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE
89 * in osfmk/kern/cpu_data.h
91 /* return get_interrupt_level(); */
92 return ml_at_interrupt_context() ? 1 : 0;
99 static LCK_MTX_DECLARE_ATTR(dt_xc_lock
, &dtrace_lck_grp
, &dtrace_lck_attr
);
100 static uint32_t dt_xc_sync
;
102 typedef struct xcArg
{
111 xcArg_t
*pArg
= (xcArg_t
*) foo
;
113 if (pArg
->cpu
== CPU
->cpu_id
|| pArg
->cpu
== DTRACE_CPUALL
) {
114 (pArg
->f
)(pArg
->arg
);
117 if (os_atomic_dec(&dt_xc_sync
, relaxed
) == 0) {
118 thread_wakeup((event_t
) &dt_xc_sync
);
123 * dtrace_xcall() is not called from probe context.
126 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t f
, void *arg
)
128 /* Only one dtrace_xcall in flight allowed */
129 lck_mtx_lock(&dt_xc_lock
);
137 cpu_broadcast_xcall(&dt_xc_sync
, TRUE
, xcRemote
, (void*) &xcArg
);
139 lck_mtx_unlock(&dt_xc_lock
);
145 * Register definitions
151 #define ARM64_CPSR 33
157 dtrace_getreg(struct regs
* savearea
, uint_t reg
)
159 struct arm_saved_state
*regs
= (struct arm_saved_state
*) savearea
;
162 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
166 if (!check_saved_state_reglimit(regs
, reg
)) {
167 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
171 return (uint64_t)get_saved_state_reg(regs
, reg
);
175 dtrace_getvmreg(uint_t ndx
)
178 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
182 #define RETURN_OFFSET64 8
185 dtrace_getustack_common(uint64_t * pcstack
, int pcstack_limit
, user_addr_t pc
,
188 volatile uint16_t *flags
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
191 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
195 if (pcstack
!= NULL
) {
196 *pcstack
++ = (uint64_t) pc
;
198 if (pcstack_limit
<= 0) {
207 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
208 sp
= dtrace_fuword64(sp
);
210 /* Truncate ustack if the iterator causes fault. */
211 if (*flags
& CPU_DTRACE_FAULT
) {
212 *flags
&= ~CPU_DTRACE_FAULT
;
221 dtrace_getupcstack(uint64_t * pcstack
, int pcstack_limit
)
223 thread_t thread
= current_thread();
225 user_addr_t pc
, sp
, fp
;
226 volatile uint16_t *flags
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
229 if (*flags
& CPU_DTRACE_FAULT
) {
233 if (pcstack_limit
<= 0) {
238 * If there's no user context we still need to zero the stack.
240 if (thread
== NULL
) {
244 regs
= (savearea_t
*) find_user_regs(thread
);
249 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
252 if (pcstack_limit
<= 0) {
256 pc
= get_saved_state_pc(regs
);
257 sp
= get_saved_state_sp(regs
);
260 fp
= get_saved_state_fp(regs
);
263 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
264 *pcstack
++ = (uint64_t) pc
;
266 if (pcstack_limit
<= 0) {
270 pc
= get_saved_state_lr(regs
);
273 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, fp
);
276 ASSERT(n
<= pcstack_limit
);
282 while (pcstack_limit
-- > 0) {
288 dtrace_getustackdepth(void)
290 thread_t thread
= current_thread();
292 user_addr_t pc
, sp
, fp
;
295 if (thread
== NULL
) {
299 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
)) {
303 regs
= (savearea_t
*) find_user_regs(thread
);
308 pc
= get_saved_state_pc(regs
);
309 sp
= get_saved_state_sp(regs
);
310 fp
= get_saved_state_fp(regs
);
312 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
314 pc
= get_saved_state_lr(regs
);
318 * Note that unlike ppc, the arm code does not use
319 * CPU_DTRACE_USTACK_FP. This is because arm always
320 * traces from the sp, even in syscall/profile/fbt
324 n
+= dtrace_getustack_common(NULL
, 0, pc
, fp
);
330 dtrace_getufpstack(uint64_t * pcstack
, uint64_t * fpstack
, int pcstack_limit
)
332 thread_t thread
= current_thread();
333 boolean_t is64bit
= proc_is64bit_data(current_proc());
336 volatile uint16_t *flags
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
339 if (*flags
& CPU_DTRACE_FAULT
) {
343 if (pcstack_limit
<= 0) {
348 * If there's no user context we still need to zero the stack.
350 if (thread
== NULL
) {
354 regs
= (savearea_t
*) find_user_regs(thread
);
359 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
362 if (pcstack_limit
<= 0) {
366 pc
= get_saved_state_pc(regs
);
367 sp
= get_saved_state_lr(regs
);
369 #if 0 /* XXX signal stack crawl */
370 oldcontext
= lwp
->lwp_oldcontext
;
372 if (p
->p_model
== DATAMODEL_NATIVE
) {
373 s1
= sizeof(struct frame
) + 2 * sizeof(long);
374 s2
= s1
+ sizeof(siginfo_t
);
376 s1
= sizeof(struct frame32
) + 3 * sizeof(int);
377 s2
= s1
+ sizeof(siginfo32_t
);
381 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
382 *pcstack
++ = (uint64_t) pc
;
385 if (pcstack_limit
<= 0) {
390 pc
= dtrace_fuword64(sp
);
392 pc
= dtrace_fuword32(sp
);
395 while (pc
!= 0 && sp
!= 0) {
396 *pcstack
++ = (uint64_t) pc
;
399 if (pcstack_limit
<= 0) {
403 #if 0 /* XXX signal stack crawl */
404 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
405 if (p
->p_model
== DATAMODEL_NATIVE
) {
406 ucontext_t
*ucp
= (ucontext_t
*) oldcontext
;
407 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
409 sp
= dtrace_fulword(&gregs
[REG_FP
]);
410 pc
= dtrace_fulword(&gregs
[REG_PC
]);
412 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
414 ucontext_t
*ucp
= (ucontext_t
*) oldcontext
;
415 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
417 sp
= dtrace_fuword32(&gregs
[EBP
]);
418 pc
= dtrace_fuword32(&gregs
[EIP
]);
420 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
425 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
426 sp
= dtrace_fuword64(sp
);
429 /* Truncate ustack if the iterator causes fault. */
430 if (*flags
& CPU_DTRACE_FAULT
) {
431 *flags
&= ~CPU_DTRACE_FAULT
;
437 while (pcstack_limit
-- > 0) {
443 static inline boolean_t
444 dtrace_frame_in_ppl_stack(struct frame
* fp
)
446 return ((void *)fp
>= pmap_stacks_start
) &&
447 ((void *)fp
< pmap_stacks_end
);
452 dtrace_getpcstack(pc_t
* pcstack
, int pcstack_limit
, int aframes
,
455 struct frame
*fp
= (struct frame
*) __builtin_frame_address(0);
456 struct frame
*nextfp
, *minfp
, *stacktop
;
464 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
466 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0) {
467 stacktop
= (struct frame
*) dtrace_get_cpu_int_stack_top();
470 else if ((on_ppl_stack
= dtrace_frame_in_ppl_stack(fp
))) {
471 stacktop
= (struct frame
*) pmap_stacks_end
;
475 stacktop
= (struct frame
*) (dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
);
482 if (intrpc
!= NULL
&& depth
< pcstack_limit
) {
483 pcstack
[depth
++] = (pc_t
) intrpc
;
486 while (depth
< pcstack_limit
) {
487 nextfp
= *(struct frame
**) fp
;
488 pc
= *(uintptr_t *) (((uintptr_t) fp
) + RETURN_OFFSET64
);
490 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
493 * Hop from interrupt stack to thread stack.
495 arm_saved_state_t
*arm_kern_regs
= (arm_saved_state_t
*) find_kern_regs(current_thread());
497 nextfp
= (struct frame
*)(saved_state64(arm_kern_regs
)->fp
);
500 on_ppl_stack
= dtrace_frame_in_ppl_stack(nextfp
);
503 minfp
= pmap_stacks_start
;
504 stacktop
= pmap_stacks_end
;
508 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
510 minfp
= (struct frame
*)kstack_base
;
511 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
516 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
521 * If this thread was on the interrupt stack, but did not
522 * take an interrupt (i.e, the idle thread), there is no
523 * explicit saved state for us to use.
529 if ((!on_ppl_stack
) && dtrace_frame_in_ppl_stack(nextfp
)) {
531 * We are switching from the kernel stack
535 minfp
= pmap_stacks_start
;
536 stacktop
= pmap_stacks_end
;
537 } else if (on_ppl_stack
) {
539 * We could be going from the PPL stack
540 * to the kernel stack.
542 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
544 minfp
= (struct frame
*)kstack_base
;
545 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
547 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
554 * This is the last frame we can process; indicate
555 * that we should return after processing this frame.
562 if (--aframes
== 0 && caller
!= (uintptr_t)NULL
) {
564 * We've just run out of artificial frames,
565 * and we have a valid caller -- fill it in
568 ASSERT(depth
< pcstack_limit
);
569 pcstack
[depth
++] = (pc_t
) caller
;
570 caller
= (uintptr_t)NULL
;
573 if (depth
< pcstack_limit
) {
574 pcstack
[depth
++] = (pc_t
) pc
;
579 while (depth
< pcstack_limit
) {
580 pcstack
[depth
++] = (pc_t
) NULL
;
590 dtrace_getarg(int arg
, int aframes
, dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
592 #pragma unused(arg, aframes)
594 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
600 * A total of 8 arguments are passed via registers; any argument with
601 * index of 7 or lower is therefore in a register.
605 for (i
= 1; i
<= aframes
; ++i
) {
607 #if __has_feature(ptrauth_returns)
608 pc
= (uintptr_t)ptrauth_strip((void*)fp
->retaddr
, ptrauth_key_return_address
);
613 if (dtrace_invop_callsite_pre
!= NULL
614 && pc
> (uintptr_t) dtrace_invop_callsite_pre
615 && pc
<= (uintptr_t) dtrace_invop_callsite_post
) {
616 /* fp points to frame of dtrace_invop() activation */
617 fp
= fp
->backchain
; /* to fbt_perfCallback activation */
618 fp
= fp
->backchain
; /* to sleh_synchronous activation */
619 fp
= fp
->backchain
; /* to fleh_synchronous activation */
621 arm_saved_state_t
*tagged_regs
= (arm_saved_state_t
*) ((void*) &fp
[1]);
622 arm_saved_state64_t
*saved_state
= saved_state64(tagged_regs
);
625 /* the argument will be found in a register */
626 stack
= (uintptr_t*) &saved_state
->x
[0];
628 /* the argument will be found in the stack */
629 fp
= (struct frame
*) saved_state
->sp
;
630 stack
= (uintptr_t*) &fp
[1];
639 * We know that we did not come through a trap to get into
640 * dtrace_probe() -- We arrive here when the provider has
641 * called dtrace_probe() directly.
642 * The probe ID is the first argument to dtrace_probe().
643 * We must advance beyond that to get the argX.
645 arg
++; /* Advance past probeID */
649 * This shouldn't happen. If the argument is passed in a
650 * register then it should have been, well, passed in a
653 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
658 stack
= (uintptr_t*) &fp
[1]; /* Find marshalled arguments */
661 if (dtrace_canload((uint64_t)(stack
+ arg
), sizeof(uint64_t),
663 /* dtrace_probe arguments arg0 ... arg4 are 64bits wide */
664 val
= dtrace_load64((uint64_t)(stack
+ arg
));
671 dtrace_probe_error(dtrace_state_t
*state
, dtrace_epid_t epid
, int which
,
672 int fltoffs
, int fault
, uint64_t illval
)
676 * For the case of the error probe firing lets
677 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
679 state
->dts_arg_error_illval
= illval
;
680 dtrace_probe( dtrace_probeid_error
, (uint64_t)(uintptr_t)state
, epid
, which
, fltoffs
, fault
);
684 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
))
686 /* XXX ARMTODO check copied from ppc/x86*/
688 * "base" is the smallest toxic address in the range, "limit" is the first
689 * VALID address greater than "base".
691 func(0x0, VM_MIN_KERNEL_ADDRESS
);
692 if (VM_MAX_KERNEL_ADDRESS
< ~(uintptr_t)0) {
693 func(VM_MAX_KERNEL_ADDRESS
+ 1, ~(uintptr_t)0);
698 dtrace_flush_caches(void)
700 /* TODO There were some problems with flushing just the cache line that had been modified.
701 * For now, we'll flush the entire cache, until we figure out how to flush just the patched block.
704 InvalidatePoU_Icache();