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/simple_lock.h>
45 #include <kern/sched_prim.h> /* for thread_wakeup() */
46 #include <kern/thread_call.h>
47 #include <kern/task.h>
49 extern struct arm_saved_state
*find_kern_regs(thread_t
);
51 extern dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
52 typedef arm_saved_state_t savearea_t
;
54 extern lck_attr_t
*dtrace_lck_attr
;
55 extern lck_grp_t
*dtrace_lck_grp
;
59 struct frame
*backchain
;
64 * Atomicity and synchronization
67 dtrace_membar_producer(void)
70 __asm__
volatile ("dmb ish" : : : "memory");
72 __asm__
volatile ("nop" : : : "memory");
77 dtrace_membar_consumer(void)
80 __asm__
volatile ("dmb ish" : : : "memory");
82 __asm__
volatile ("nop" : : : "memory");
87 * Interrupt manipulation
88 * XXX dtrace_getipl() can be called from probe context.
94 * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE
95 * in osfmk/kern/cpu_data.h
97 /* return get_interrupt_level(); */
98 return ml_at_interrupt_context() ? 1 : 0;
106 decl_lck_mtx_data(static, dt_xc_lock
);
107 static uint32_t dt_xc_sync
;
109 typedef struct xcArg
{
118 xcArg_t
*pArg
= (xcArg_t
*) foo
;
120 if (pArg
->cpu
== CPU
->cpu_id
|| pArg
->cpu
== DTRACE_CPUALL
) {
121 (pArg
->f
)(pArg
->arg
);
124 if (os_atomic_dec(&dt_xc_sync
, relaxed
) == 0) {
125 thread_wakeup((event_t
) &dt_xc_sync
);
131 * dtrace_xcall() is not called from probe context.
134 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t f
, void *arg
)
137 /* Only one dtrace_xcall in flight allowed */
138 lck_mtx_lock(&dt_xc_lock
);
146 cpu_broadcast_xcall(&dt_xc_sync
, TRUE
, xcRemote
, (void*) &xcArg
);
148 lck_mtx_unlock(&dt_xc_lock
);
152 /* On uniprocessor systems, the cpu should always be either ourselves or all */
153 ASSERT(cpu
== CPU
->cpu_id
|| cpu
== DTRACE_CPUALL
);
164 dtrace_isa_init(void)
166 lck_mtx_init(&dt_xc_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
172 * Register definitions
178 #define ARM64_CPSR 33
184 dtrace_getreg(struct regs
* savearea
, uint_t reg
)
186 struct arm_saved_state
*regs
= (struct arm_saved_state
*) savearea
;
189 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
193 if (!check_saved_state_reglimit(regs
, reg
)) {
194 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
198 return (uint64_t)get_saved_state_reg(regs
, reg
);
201 #define RETURN_OFFSET64 8
204 dtrace_getustack_common(uint64_t * pcstack
, int pcstack_limit
, user_addr_t pc
,
209 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
213 if (pcstack
!= NULL
) {
214 *pcstack
++ = (uint64_t) pc
;
216 if (pcstack_limit
<= 0) {
225 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
226 sp
= dtrace_fuword64(sp
);
233 dtrace_getupcstack(uint64_t * pcstack
, int pcstack_limit
)
235 thread_t thread
= current_thread();
237 user_addr_t pc
, sp
, fp
;
238 volatile uint16_t *flags
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
241 if (*flags
& CPU_DTRACE_FAULT
) {
245 if (pcstack_limit
<= 0) {
250 * If there's no user context we still need to zero the stack.
252 if (thread
== NULL
) {
256 regs
= (savearea_t
*) find_user_regs(thread
);
261 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
264 if (pcstack_limit
<= 0) {
268 pc
= get_saved_state_pc(regs
);
269 sp
= get_saved_state_sp(regs
);
270 fp
= get_saved_state_fp(regs
);
272 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
273 *pcstack
++ = (uint64_t) pc
;
275 if (pcstack_limit
<= 0) {
279 pc
= get_saved_state_lr(regs
);
282 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, fp
);
285 ASSERT(n
<= pcstack_limit
);
291 while (pcstack_limit
-- > 0) {
297 dtrace_getustackdepth(void)
299 thread_t thread
= current_thread();
301 user_addr_t pc
, sp
, fp
;
304 if (thread
== NULL
) {
308 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
)) {
312 regs
= (savearea_t
*) find_user_regs(thread
);
317 pc
= get_saved_state_pc(regs
);
318 sp
= get_saved_state_sp(regs
);
319 fp
= get_saved_state_fp(regs
);
321 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
323 pc
= get_saved_state_lr(regs
);
327 * Note that unlike ppc, the arm code does not use
328 * CPU_DTRACE_USTACK_FP. This is because arm always
329 * traces from the sp, even in syscall/profile/fbt
333 n
+= dtrace_getustack_common(NULL
, 0, pc
, fp
);
339 dtrace_getufpstack(uint64_t * pcstack
, uint64_t * fpstack
, int pcstack_limit
)
341 thread_t thread
= current_thread();
342 boolean_t is64bit
= proc_is64bit_data(current_proc());
345 volatile uint16_t *flags
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
348 if (*flags
& CPU_DTRACE_FAULT
) {
352 if (pcstack_limit
<= 0) {
357 * If there's no user context we still need to zero the stack.
359 if (thread
== NULL
) {
363 regs
= (savearea_t
*) find_user_regs(thread
);
368 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
371 if (pcstack_limit
<= 0) {
375 pc
= get_saved_state_pc(regs
);
376 sp
= get_saved_state_lr(regs
);
378 #if 0 /* XXX signal stack crawl */
379 oldcontext
= lwp
->lwp_oldcontext
;
381 if (p
->p_model
== DATAMODEL_NATIVE
) {
382 s1
= sizeof(struct frame
) + 2 * sizeof(long);
383 s2
= s1
+ sizeof(siginfo_t
);
385 s1
= sizeof(struct frame32
) + 3 * sizeof(int);
386 s2
= s1
+ sizeof(siginfo32_t
);
390 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
391 *pcstack
++ = (uint64_t) pc
;
394 if (pcstack_limit
<= 0) {
399 pc
= dtrace_fuword64(sp
);
401 pc
= dtrace_fuword32(sp
);
404 while (pc
!= 0 && sp
!= 0) {
405 *pcstack
++ = (uint64_t) pc
;
408 if (pcstack_limit
<= 0) {
412 #if 0 /* XXX signal stack crawl */
413 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
414 if (p
->p_model
== DATAMODEL_NATIVE
) {
415 ucontext_t
*ucp
= (ucontext_t
*) oldcontext
;
416 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
418 sp
= dtrace_fulword(&gregs
[REG_FP
]);
419 pc
= dtrace_fulword(&gregs
[REG_PC
]);
421 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
423 ucontext_t
*ucp
= (ucontext_t
*) oldcontext
;
424 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
426 sp
= dtrace_fuword32(&gregs
[EBP
]);
427 pc
= dtrace_fuword32(&gregs
[EIP
]);
429 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
434 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
435 sp
= dtrace_fuword64(sp
);
441 * This is totally bogus: if we faulted, we're going to clear
442 * the fault and break. This is to deal with the apparently
443 * broken Java stacks on x86.
445 if (*flags
& CPU_DTRACE_FAULT
) {
446 *flags
&= ~CPU_DTRACE_FAULT
;
453 while (pcstack_limit
-- > 0) {
460 dtrace_getpcstack(pc_t
* pcstack
, int pcstack_limit
, int aframes
,
463 struct frame
*fp
= (struct frame
*) __builtin_frame_address(0);
464 struct frame
*nextfp
, *minfp
, *stacktop
;
469 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
471 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0) {
472 stacktop
= (struct frame
*) dtrace_get_cpu_int_stack_top();
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 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
502 minfp
= (struct frame
*)kstack_base
;
503 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
508 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
513 * If this thread was on the interrupt stack, but did not
514 * take an interrupt (i.e, the idle thread), there is no
515 * explicit saved state for us to use.
522 * This is the last frame we can process; indicate
523 * that we should return after processing this frame.
530 if (--aframes
== 0 && caller
!= (uintptr_t)NULL
) {
532 * We've just run out of artificial frames,
533 * and we have a valid caller -- fill it in
536 ASSERT(depth
< pcstack_limit
);
537 pcstack
[depth
++] = (pc_t
) caller
;
538 caller
= (uintptr_t)NULL
;
541 if (depth
< pcstack_limit
) {
542 pcstack
[depth
++] = (pc_t
) pc
;
547 while (depth
< pcstack_limit
) {
548 pcstack
[depth
++] = (pc_t
) NULL
;
558 dtrace_getarg(int arg
, int aframes
, dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
560 #pragma unused(arg, aframes)
562 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
568 * A total of 8 arguments are passed via registers; any argument with
569 * index of 7 or lower is therefore in a register.
573 for (i
= 1; i
<= aframes
; ++i
) {
575 #if __has_feature(ptrauth_returns)
576 pc
= (uintptr_t)ptrauth_strip((void*)fp
->retaddr
, ptrauth_key_return_address
);
581 if (dtrace_invop_callsite_pre
!= NULL
582 && pc
> (uintptr_t) dtrace_invop_callsite_pre
583 && pc
<= (uintptr_t) dtrace_invop_callsite_post
) {
584 /* fp points to frame of dtrace_invop() activation */
585 fp
= fp
->backchain
; /* to fbt_perfCallback activation */
586 fp
= fp
->backchain
; /* to sleh_synchronous activation */
587 fp
= fp
->backchain
; /* to fleh_synchronous activation */
589 arm_saved_state_t
*tagged_regs
= (arm_saved_state_t
*) ((void*) &fp
[1]);
590 arm_saved_state64_t
*saved_state
= saved_state64(tagged_regs
);
593 /* the argument will be found in a register */
594 stack
= (uintptr_t*) &saved_state
->x
[0];
596 /* the argument will be found in the stack */
597 fp
= (struct frame
*) saved_state
->sp
;
598 stack
= (uintptr_t*) &fp
[1];
607 * We know that we did not come through a trap to get into
608 * dtrace_probe() -- We arrive here when the provider has
609 * called dtrace_probe() directly.
610 * The probe ID is the first argument to dtrace_probe().
611 * We must advance beyond that to get the argX.
613 arg
++; /* Advance past probeID */
617 * This shouldn't happen. If the argument is passed in a
618 * register then it should have been, well, passed in a
621 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
626 stack
= (uintptr_t*) &fp
[1]; /* Find marshalled arguments */
629 if (dtrace_canload((uint64_t)(stack
+ arg
), sizeof(uint64_t),
631 /* dtrace_probe arguments arg0 ... arg4 are 64bits wide */
632 val
= dtrace_load64((uint64_t)(stack
+ arg
));
639 dtrace_probe_error(dtrace_state_t
*state
, dtrace_epid_t epid
, int which
,
640 int fltoffs
, int fault
, uint64_t illval
)
644 * For the case of the error probe firing lets
645 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
647 state
->dts_arg_error_illval
= illval
;
648 dtrace_probe( dtrace_probeid_error
, (uint64_t)(uintptr_t)state
, epid
, which
, fltoffs
, fault
);
652 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
))
654 /* XXX ARMTODO check copied from ppc/x86*/
656 * "base" is the smallest toxic address in the range, "limit" is the first
657 * VALID address greater than "base".
659 func(0x0, VM_MIN_KERNEL_ADDRESS
);
660 if (VM_MAX_KERNEL_ADDRESS
< ~(uintptr_t)0) {
661 func(VM_MAX_KERNEL_ADDRESS
+ 1, ~(uintptr_t)0);
666 dtrace_flush_caches(void)
668 /* TODO There were some problems with flushing just the cache line that had been modified.
669 * For now, we'll flush the entire cache, until we figure out how to flush just the patched block.
672 InvalidatePoU_Icache();