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/proc_reg.h>
33 #include <kern/thread.h>
34 #include <mach/thread_status.h>
38 #include <sys/malloc.h>
40 #include <sys/systm.h>
42 #include <sys/proc_internal.h>
43 #include <sys/kauth.h>
44 #include <sys/dtrace.h>
45 #include <sys/dtrace_impl.h>
46 #include <libkern/OSAtomic.h>
47 #include <kern/simple_lock.h>
48 #include <kern/sched_prim.h> /* for thread_wakeup() */
49 #include <kern/thread_call.h>
50 #include <kern/task.h>
51 #include <miscfs/devfs/devfs.h>
52 #include <mach/vm_param.h>
54 extern struct arm_saved_state
*find_kern_regs(thread_t
);
56 extern dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
57 typedef arm_saved_state_t savearea_t
;
59 extern lck_attr_t
*dtrace_lck_attr
;
60 extern lck_grp_t
*dtrace_lck_grp
;
64 struct frame
*backchain
;
69 * Atomicity and synchronization
72 dtrace_membar_producer(void)
75 __asm__
volatile("dmb ish" : : : "memory");
77 __asm__
volatile("nop" : : : "memory");
82 dtrace_membar_consumer(void)
85 __asm__
volatile("dmb ish" : : : "memory");
87 __asm__
volatile("nop" : : : "memory");
92 * Interrupt manipulation
93 * XXX dtrace_getipl() can be called from probe context.
99 * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE
100 * in osfmk/kern/cpu_data.h
102 /* return get_interrupt_level(); */
103 return (ml_at_interrupt_context() ? 1 : 0);
111 decl_lck_mtx_data(static, dt_xc_lock
);
112 static uint32_t dt_xc_sync
;
114 typedef struct xcArg
{
123 xcArg_t
*pArg
= (xcArg_t
*) foo
;
125 if (pArg
->cpu
== CPU
->cpu_id
|| pArg
->cpu
== DTRACE_CPUALL
)
126 (pArg
->f
) (pArg
->arg
);
128 if (hw_atomic_sub(&dt_xc_sync
, 1) == 0)
129 thread_wakeup((event_t
) &dt_xc_sync
);
134 * dtrace_xcall() is not called from probe context.
137 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t f
, void *arg
)
140 /* Only one dtrace_xcall in flight allowed */
141 lck_mtx_lock(&dt_xc_lock
);
149 cpu_broadcast_xcall(&dt_xc_sync
, TRUE
, xcRemote
, (void*) &xcArg
);
151 lck_mtx_unlock(&dt_xc_lock
);
155 /* On uniprocessor systems, the cpu should always be either ourselves or all */
156 ASSERT(cpu
== CPU
->cpu_id
|| cpu
== DTRACE_CPUALL
);
167 dtrace_isa_init(void)
169 lck_mtx_init(&dt_xc_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
175 * Register definitions
187 #define ARM64_CPSR 33
193 dtrace_getreg(struct regs
* savearea
, uint_t reg
)
195 struct arm_saved_state
*regs
= (struct arm_saved_state
*) savearea
;
197 if (is_saved_state32(regs
)) {
198 // Fix special registers if user is 32 bits
218 if (!check_saved_state_reglimit(regs
, reg
)) {
219 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
223 return ((uint64_t)get_saved_state_reg(regs
, reg
));
226 #define RETURN_OFFSET 4
227 #define RETURN_OFFSET64 8
230 dtrace_getustack_common(uint64_t * pcstack
, int pcstack_limit
, user_addr_t pc
,
234 boolean_t is64bit
= proc_is64bit(current_proc());
236 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
240 if (pcstack
!= NULL
) {
241 *pcstack
++ = (uint64_t) pc
;
243 if (pcstack_limit
<= 0)
251 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
252 sp
= dtrace_fuword64(sp
);
254 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
255 sp
= dtrace_fuword32(sp
);
263 dtrace_getupcstack(uint64_t * pcstack
, int pcstack_limit
)
265 thread_t thread
= current_thread();
267 user_addr_t pc
, sp
, fp
;
268 volatile uint16_t *flags
= (volatile uint16_t *) & cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
271 if (*flags
& CPU_DTRACE_FAULT
)
274 if (pcstack_limit
<= 0)
278 * If there's no user context we still need to zero the stack.
283 regs
= (savearea_t
*) find_user_regs(thread
);
287 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
290 if (pcstack_limit
<= 0)
293 pc
= get_saved_state_pc(regs
);
294 sp
= get_saved_state_sp(regs
);
295 fp
= get_saved_state_fp(regs
);
297 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
298 *pcstack
++ = (uint64_t) pc
;
300 if (pcstack_limit
<= 0)
303 pc
= get_saved_state_lr(regs
);
306 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, fp
);
309 ASSERT(n
<= pcstack_limit
);
315 while (pcstack_limit
-- > 0)
320 dtrace_getustackdepth(void)
322 thread_t thread
= current_thread();
324 user_addr_t pc
, sp
, fp
;
330 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
333 regs
= (savearea_t
*) find_user_regs(thread
);
337 pc
= get_saved_state_pc(regs
);
338 sp
= get_saved_state_sp(regs
);
339 fp
= get_saved_state_fp(regs
);
341 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
343 pc
= get_saved_state_lr(regs
);
347 * Note that unlike ppc, the arm code does not use
348 * CPU_DTRACE_USTACK_FP. This is because arm always
349 * traces from the sp, even in syscall/profile/fbt
353 n
+= dtrace_getustack_common(NULL
, 0, pc
, fp
);
359 dtrace_getufpstack(uint64_t * pcstack
, uint64_t * fpstack
, int pcstack_limit
)
361 thread_t thread
= current_thread();
362 boolean_t is64bit
= proc_is64bit(current_proc());
365 volatile uint16_t *flags
= (volatile uint16_t *) & cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
368 uintptr_t oldcontext
;
372 if (*flags
& CPU_DTRACE_FAULT
)
375 if (pcstack_limit
<= 0)
379 * If there's no user context we still need to zero the stack.
384 regs
= (savearea_t
*) find_user_regs(thread
);
388 *pcstack
++ = (uint64_t)dtrace_proc_selfpid();
391 if (pcstack_limit
<= 0)
394 pc
= get_saved_state_pc(regs
);
395 sp
= get_saved_state_lr(regs
);
397 #if 0 /* XXX signal stack crawl */
398 oldcontext
= lwp
->lwp_oldcontext
;
400 if (p
->p_model
== DATAMODEL_NATIVE
) {
401 s1
= sizeof(struct frame
) + 2 * sizeof(long);
402 s2
= s1
+ sizeof(siginfo_t
);
404 s1
= sizeof(struct frame32
) + 3 * sizeof(int);
405 s2
= s1
+ sizeof(siginfo32_t
);
409 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
410 *pcstack
++ = (uint64_t) pc
;
413 if (pcstack_limit
<= 0)
417 pc
= dtrace_fuword64(sp
);
419 pc
= dtrace_fuword32(sp
);
421 while (pc
!= 0 && sp
!= 0) {
422 *pcstack
++ = (uint64_t) pc
;
425 if (pcstack_limit
<= 0)
428 #if 0 /* XXX signal stack crawl */
429 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
430 if (p
->p_model
== DATAMODEL_NATIVE
) {
431 ucontext_t
*ucp
= (ucontext_t
*) oldcontext
;
432 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
434 sp
= dtrace_fulword(&gregs
[REG_FP
]);
435 pc
= dtrace_fulword(&gregs
[REG_PC
]);
437 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
439 ucontext_t
*ucp
= (ucontext_t
*) oldcontext
;
440 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
442 sp
= dtrace_fuword32(&gregs
[EBP
]);
443 pc
= dtrace_fuword32(&gregs
[EIP
]);
445 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
451 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
452 sp
= dtrace_fuword64(sp
);
454 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
455 sp
= dtrace_fuword32(sp
);
462 * This is totally bogus: if we faulted, we're going to clear
463 * the fault and break. This is to deal with the apparently
464 * broken Java stacks on x86.
466 if (*flags
& CPU_DTRACE_FAULT
) {
467 *flags
&= ~CPU_DTRACE_FAULT
;
474 while (pcstack_limit
-- > 0)
480 dtrace_getpcstack(pc_t
* pcstack
, int pcstack_limit
, int aframes
,
483 struct frame
*fp
= (struct frame
*) __builtin_frame_address(0);
484 struct frame
*nextfp
, *minfp
, *stacktop
;
489 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
491 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
492 stacktop
= (struct frame
*) dtrace_get_cpu_int_stack_top();
494 stacktop
= (struct frame
*) (dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
);
500 if (intrpc
!= NULL
&& depth
< pcstack_limit
)
501 pcstack
[depth
++] = (pc_t
) intrpc
;
503 while (depth
< pcstack_limit
) {
504 nextfp
= *(struct frame
**) fp
;
505 pc
= *(uintptr_t *) (((uintptr_t) fp
) + RETURN_OFFSET64
);
507 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
510 * Hop from interrupt stack to thread stack.
512 arm_saved_state_t
*arm_kern_regs
= (arm_saved_state_t
*) find_kern_regs(current_thread());
514 nextfp
= (struct frame
*)(saved_state64(arm_kern_regs
)->fp
);
517 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
519 minfp
= (struct frame
*)kstack_base
;
520 stacktop
= (struct frame
*)(kstack_base
+ kernel_stack_size
);
525 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
530 * If this thread was on the interrupt stack, but did not
531 * take an interrupt (i.e, the idle thread), there is no
532 * explicit saved state for us to use.
539 * This is the last frame we can process; indicate
540 * that we should return after processing this frame.
547 if (--aframes
== 0 && caller
!= (uintptr_t)NULL
) {
549 * We've just run out of artificial frames,
550 * and we have a valid caller -- fill it in
553 ASSERT(depth
< pcstack_limit
);
554 pcstack
[depth
++] = (pc_t
) caller
;
555 caller
= (uintptr_t)NULL
;
558 if (depth
< pcstack_limit
)
559 pcstack
[depth
++] = (pc_t
) pc
;
563 while (depth
< pcstack_limit
)
564 pcstack
[depth
++] = (pc_t
) NULL
;
573 * On arm64, we support both 32bit and 64bit user processes.
574 * This routine is only called when handling 32bit processes
575 * where thumb_mode is pertinent.
576 * If this routine is called when handling 64bit processes
577 * thumb_mode should always be zero.
580 dtrace_instr_size(uint32_t instr
, int thumb_mode
)
583 uint16_t instr16
= *(uint16_t*) &instr
;
584 if (((instr16
>> 11) & 0x1F) > 0x1C)
594 dtrace_getarg(int arg
, int aframes
, dtrace_mstate_t
*mstate
, dtrace_vstate_t
*vstate
)
596 #pragma unused(arg, aframes)
598 struct frame
*fp
= (struct frame
*)__builtin_frame_address(0);
604 * A total of 8 arguments are passed via registers; any argument with
605 * index of 7 or lower is therefore in a register.
609 for (i
= 1; i
<= aframes
; ++i
) {
613 if (dtrace_invop_callsite_pre
!= NULL
614 && pc
> (uintptr_t) dtrace_invop_callsite_pre
615 && pc
<= (uintptr_t) dtrace_invop_callsite_post
)
617 /* fp points to frame of dtrace_invop() activation */
618 fp
= fp
->backchain
; /* to fbt_perfCallback activation */
619 fp
= fp
->backchain
; /* to sleh_synchronous activation */
620 fp
= fp
->backchain
; /* to fleh_synchronous activation */
622 arm_saved_state_t
*tagged_regs
= (arm_saved_state_t
*) ((void*) &fp
[1]);
623 arm_saved_state64_t
*saved_state
= saved_state64(tagged_regs
);
626 /* the argument will be found in a register */
627 stack
= (uintptr_t*) &saved_state
->x
[0];
629 /* the argument will be found in the stack */
630 fp
= (struct frame
*) saved_state
->sp
;
631 stack
= (uintptr_t*) &fp
[1];
640 * We know that we did not come through a trap to get into
641 * dtrace_probe() -- We arrive here when the provider has
642 * called dtrace_probe() directly.
643 * The probe ID is the first argument to dtrace_probe().
644 * We must advance beyond that to get the argX.
646 arg
++; /* Advance past probeID */
650 * This shouldn't happen. If the argument is passed in a
651 * register then it should have been, well, passed in a
654 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
659 stack
= (uintptr_t*) &fp
[1]; /* Find marshalled arguments */
662 if (dtrace_canload((uint64_t)(stack
+ arg
), sizeof(uint64_t),
664 /* dtrace_probe arguments arg0 ... arg4 are 64bits wide */
665 val
= dtrace_load64((uint64_t)(stack
+ arg
));
672 dtrace_probe_error(dtrace_state_t
*state
, dtrace_epid_t epid
, int which
,
673 int fltoffs
, int fault
, uint64_t illval
)
677 * For the case of the error probe firing lets
678 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
680 state
->dts_arg_error_illval
= illval
;
681 dtrace_probe( dtrace_probeid_error
, (uint64_t)(uintptr_t)state
, epid
, which
, fltoffs
, fault
);
685 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
))
687 /* XXX ARMTODO check copied from ppc/x86*/
689 * "base" is the smallest toxic address in the range, "limit" is the first
690 * VALID address greater than "base".
692 func(0x0, VM_MIN_KERNEL_ADDRESS
);
693 if (VM_MAX_KERNEL_ADDRESS
< ~(uintptr_t)0)
694 func(VM_MAX_KERNEL_ADDRESS
+ 1, ~(uintptr_t)0);