2 * Copyright (c) 2000-2016 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 <mach/mach_types.h>
30 #include <mach/exception_types.h>
31 #include <arm/exception.h>
33 #include <arm/proc_reg.h>
34 #include <arm/thread.h>
36 #include <arm/cpu_data_internal.h>
37 #include <kdp/kdp_internal.h>
38 #include <kern/debug.h>
39 #include <IOKit/IOPlatformExpert.h>
40 #include <kern/kalloc.h>
41 #include <libkern/OSAtomic.h>
42 #include <vm/vm_map.h>
44 #if defined(HAS_APPLE_PAC)
48 #define KDP_TEST_HARNESS 0
50 #define dprintf(x) kprintf x
52 #define dprintf(x) do {} while (0)
55 void halt_all_cpus(boolean_t
);
58 int machine_trace_thread(thread_t thread
,
64 uint32_t * thread_trace_flags
);
65 int machine_trace_thread64(thread_t thread
,
71 uint32_t * thread_trace_flags
,
74 void kdp_trap(unsigned int, struct arm_saved_state
* saved_state
);
76 extern vm_offset_t
machine_trace_thread_get_kva(vm_offset_t cur_target_addr
, vm_map_t map
, uint32_t *thread_trace_flags
);
77 extern void machine_trace_thread_clear_validation_cache(void);
78 extern vm_map_t kernel_map
;
80 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
83 unsigned char * pkt
, int * len
, unsigned short * remote_port
, unsigned int exception
, unsigned int code
, unsigned int subcode
)
90 kdp_exception_t
* rq
= (kdp_exception_t
*)&aligned_pkt
;
92 bcopy((char *)pkt
, (char *)rq
, sizeof(*rq
));
93 rq
->hdr
.request
= KDP_EXCEPTION
;
95 rq
->hdr
.seq
= kdp
.exception_seq
;
97 rq
->hdr
.len
= sizeof(*rq
) + sizeof(kdp_exc_info_t
);
100 rq
->exc_info
[0].cpu
= 0;
101 rq
->exc_info
[0].exception
= exception
;
102 rq
->exc_info
[0].code
= code
;
103 rq
->exc_info
[0].subcode
= subcode
;
105 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof(kdp_exc_info_t
);
107 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
109 kdp
.exception_ack_needed
= TRUE
;
111 *remote_port
= kdp
.exception_port
;
116 kdp_exception_ack(unsigned char * pkt
, int len
)
118 kdp_exception_ack_t aligned_pkt
;
119 kdp_exception_ack_t
* rq
= (kdp_exception_ack_t
*)&aligned_pkt
;
121 if ((unsigned)len
< sizeof(*rq
)) {
125 bcopy((char *)pkt
, (char *)rq
, sizeof(*rq
));
127 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
) {
131 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
133 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
134 kdp
.exception_ack_needed
= FALSE
;
141 kdp_getintegerstate(char * out_state
)
144 struct arm_thread_state thread_state
;
145 struct arm_saved_state
*saved_state
;
147 saved_state
= kdp
.saved_state
;
149 bzero((char *) &thread_state
, sizeof(struct arm_thread_state
));
151 saved_state_to_thread_state32(saved_state
, &thread_state
);
153 bcopy((char *) &thread_state
, (char *) out_state
, sizeof(struct arm_thread_state
));
154 #elif defined(__arm64__)
155 struct arm_thread_state64 thread_state64
;
156 arm_saved_state_t
*saved_state
;
158 saved_state
= kdp
.saved_state
;
159 assert(is_saved_state64(saved_state
));
161 bzero((char *) &thread_state64
, sizeof(struct arm_thread_state64
));
163 saved_state_to_thread_state64(saved_state
, &thread_state64
);
165 bcopy((char *) &thread_state64
, (char *) out_state
, sizeof(struct arm_thread_state64
));
167 #error Unknown architecture.
172 kdp_machine_read_regs(__unused
unsigned int cpu
, unsigned int flavor
, char * data
, int * size
)
176 case ARM_THREAD_STATE
:
177 dprintf(("kdp_readregs THREAD_STATE\n"));
178 kdp_getintegerstate(data
);
179 *size
= ARM_THREAD_STATE_COUNT
* sizeof(int);
180 return KDPERR_NO_ERROR
;
181 #elif defined(__arm64__)
182 case ARM_THREAD_STATE64
:
183 dprintf(("kdp_readregs THREAD_STATE64\n"));
184 kdp_getintegerstate(data
);
185 *size
= ARM_THREAD_STATE64_COUNT
* sizeof(int);
186 return KDPERR_NO_ERROR
;
190 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
191 bzero((char *) data
, sizeof(struct arm_vfp_state
));
192 *size
= ARM_VFP_STATE_COUNT
* sizeof(int);
193 return KDPERR_NO_ERROR
;
196 dprintf(("kdp_readregs bad flavor %d\n"));
197 return KDPERR_BADFLAVOR
;
202 kdp_setintegerstate(char * state_in
)
205 struct arm_thread_state thread_state
;
206 struct arm_saved_state
*saved_state
;
208 bcopy((char *) state_in
, (char *) &thread_state
, sizeof(struct arm_thread_state
));
209 saved_state
= kdp
.saved_state
;
211 thread_state32_to_saved_state(&thread_state
, saved_state
);
212 #elif defined(__arm64__)
213 struct arm_thread_state64 thread_state64
;
214 struct arm_saved_state
*saved_state
;
216 bcopy((char *) state_in
, (char *) &thread_state64
, sizeof(struct arm_thread_state64
));
217 saved_state
= kdp
.saved_state
;
218 assert(is_saved_state64(saved_state
));
220 thread_state64_to_saved_state(&thread_state64
, saved_state
);
222 #error Unknown architecture.
227 kdp_machine_write_regs(__unused
unsigned int cpu
, unsigned int flavor
, char * data
, __unused
int * size
)
231 case ARM_THREAD_STATE
:
232 dprintf(("kdp_writeregs THREAD_STATE\n"));
233 kdp_setintegerstate(data
);
234 return KDPERR_NO_ERROR
;
235 #elif defined(__arm64__)
236 case ARM_THREAD_STATE64
:
237 dprintf(("kdp_writeregs THREAD_STATE64\n"));
238 kdp_setintegerstate(data
);
239 return KDPERR_NO_ERROR
;
243 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
244 return KDPERR_NO_ERROR
;
247 dprintf(("kdp_writeregs bad flavor %d\n"));
248 return KDPERR_BADFLAVOR
;
253 kdp_machine_hostinfo(kdp_hostinfo_t
* hostinfo
)
255 hostinfo
->cpus_mask
= 1;
256 hostinfo
->cpu_type
= slot_type(0);
257 hostinfo
->cpu_subtype
= slot_subtype(0);
260 __attribute__((noreturn
))
262 kdp_panic(const char * msg
)
264 printf("kdp panic: %s\n", msg
);
283 kdp_us_spin(int usec
)
291 Debugger("inline call to debugger(machine_startup)");
301 kdp_machine_get_breakinsn(uint8_t * bytes
, uint32_t * size
)
303 *(uint32_t *)bytes
= GDB_TRAP_INSTR1
;
304 *size
= sizeof(uint32_t);
313 kdp_machine_ioport_read(kdp_readioport_req_t
* rq
, caddr_t data
, uint16_t lcpu
)
315 #pragma unused(rq, data, lcpu)
320 kdp_machine_ioport_write(kdp_writeioport_req_t
* rq
, caddr_t data
, uint16_t lcpu
)
322 #pragma unused(rq, data, lcpu)
327 kdp_machine_msr64_read(kdp_readmsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
329 #pragma unused(rq, data, lcpu)
334 kdp_machine_msr64_write(kdp_writemsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
336 #pragma unused(rq, data, lcpu)
339 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
342 kdp_trap(unsigned int exception
, struct arm_saved_state
* saved_state
)
344 handle_debugger_trap(exception
, 0, 0, saved_state
);
347 if (saved_state
->cpsr
& PSR_TF
) {
348 unsigned short instr
= *((unsigned short *)(saved_state
->pc
));
349 if ((instr
== (GDB_TRAP_INSTR1
& 0xFFFF)) || (instr
== (GDB_TRAP_INSTR2
& 0xFFFF))) {
350 saved_state
->pc
+= 2;
353 unsigned int instr
= *((unsigned int *)(saved_state
->pc
));
354 if ((instr
== GDB_TRAP_INSTR1
) || (instr
== GDB_TRAP_INSTR2
)) {
355 saved_state
->pc
+= 4;
359 #elif defined(__arm64__)
360 assert(is_saved_state64(saved_state
));
362 uint32_t instr
= *((uint32_t *)get_saved_state_pc(saved_state
));
365 * As long as we are using the arm32 trap encoding to handling
366 * traps to the debugger, we should identify both variants and
367 * increment for both of them.
369 if ((instr
== GDB_TRAP_INSTR1
) || (instr
== GDB_TRAP_INSTR2
)) {
370 add_saved_state_pc(saved_state
, 4);
373 #error Unknown architecture.
377 #define ARM32_LR_OFFSET 4
378 #define ARM64_LR_OFFSET 8
381 * Since sizeof (struct thread_snapshot) % 4 == 2
382 * make sure the compiler does not try to use word-aligned
383 * access to this data, which can result in alignment faults
384 * that can't be emulated in KDP context.
386 typedef uint32_t uint32_align2_t
__attribute__((aligned(2)));
389 machine_trace_thread(thread_t thread
,
395 uint32_t * thread_trace_flags
)
397 uint32_align2_t
* tracebuf
= (uint32_align2_t
*)tracepos
;
399 vm_size_t framesize
= (trace_fp
? 2 : 1) * sizeof(uint32_t);
401 vm_offset_t stacklimit
= 0;
402 vm_offset_t stacklimit_bottom
= 0;
404 uint32_t short_fp
= 0;
407 vm_offset_t prevfp
= 0;
409 struct arm_saved_state
* state
;
410 vm_offset_t kern_virt_addr
= 0;
411 vm_map_t bt_vm_map
= VM_MAP_NULL
;
413 nframes
= (tracebound
> tracepos
) ? MIN(nframes
, (int)((tracebound
- tracepos
) / framesize
)) : 0;
420 /* Examine the user savearea */
421 state
= get_user_regs(thread
);
422 stacklimit
= VM_MAX_ADDRESS
;
423 stacklimit_bottom
= VM_MIN_ADDRESS
;
425 /* Fake up a stack frame for the PC */
426 *tracebuf
++ = (uint32_t)get_saved_state_pc(state
);
428 *tracebuf
++ = (uint32_t)get_saved_state_sp(state
);
431 bt_vm_map
= thread
->task
->map
;
433 #if defined(__arm64__)
434 panic("Attempted to trace kernel thread_t %p as a 32-bit context", thread
);
436 #elif defined(__arm__)
437 /* kstackptr may not always be there, so recompute it */
438 state
= &thread_get_kernel_state(thread
)->machine
;
440 stacklimit
= VM_MAX_KERNEL_ADDRESS
;
441 stacklimit_bottom
= VM_MIN_KERNEL_ADDRESS
;
442 bt_vm_map
= kernel_map
;
444 #error Unknown architecture.
448 /* Get the frame pointer */
449 fp
= get_saved_state_fp(state
);
451 /* Fill in the current link register */
452 prevlr
= (uint32_t)get_saved_state_lr(state
);
453 pc
= get_saved_state_pc(state
);
454 sp
= get_saved_state_sp(state
);
456 if (!user_p
&& !prevlr
&& !fp
&& !sp
&& !pc
) {
461 /* This is safe since we will panic above on __arm64__ if !user_p */
462 prevlr
= (uint32_t)VM_KERNEL_UNSLIDE(prevlr
);
465 for (; framecount
< nframes
; framecount
++) {
466 *tracebuf
++ = prevlr
;
468 *tracebuf
++ = (uint32_t)fp
;
475 /* Unaligned frame */
476 if (fp
& 0x0000003) {
479 /* Frame is out of range, maybe a user FP while doing kernel BT */
480 if (fp
> stacklimit
) {
483 if (fp
< stacklimit_bottom
) {
486 /* Stack grows downward */
488 boolean_t prev_in_interrupt_stack
= FALSE
;
492 * As a special case, sometimes we are backtracing out of an interrupt
493 * handler, and the stack jumps downward because of the memory allocation
494 * pattern during early boot due to KASLR.
497 int max_cpu
= ml_get_max_cpu_number();
499 for (cpu
= 0; cpu
<= max_cpu
; cpu
++) {
500 cpu_data_t
*target_cpu_datap
;
502 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
503 if (target_cpu_datap
== (cpu_data_t
*)NULL
) {
507 if (prevfp
>= (target_cpu_datap
->intstack_top
- INTSTACK_SIZE
) && prevfp
< target_cpu_datap
->intstack_top
) {
508 prev_in_interrupt_stack
= TRUE
;
513 if (prevfp
>= (target_cpu_datap
->fiqstack_top
- FIQSTACK_SIZE
) && prevfp
< target_cpu_datap
->fiqstack_top
) {
514 prev_in_interrupt_stack
= TRUE
;
517 #elif defined(__arm64__)
518 if (prevfp
>= (target_cpu_datap
->excepstack_top
- EXCEPSTACK_SIZE
) && prevfp
< target_cpu_datap
->excepstack_top
) {
519 prev_in_interrupt_stack
= TRUE
;
526 if (!prev_in_interrupt_stack
) {
527 /* Corrupt frame pointer? */
531 /* Assume there's a saved link register, and read it */
532 kern_virt_addr
= machine_trace_thread_get_kva(fp
+ ARM32_LR_OFFSET
, bt_vm_map
, thread_trace_flags
);
534 if (!kern_virt_addr
) {
535 if (thread_trace_flags
) {
536 *thread_trace_flags
|= kThreadTruncatedBT
;
541 prevlr
= *(uint32_t *)kern_virt_addr
;
543 /* This is safe since we will panic above on __arm64__ if !user_p */
544 prevlr
= (uint32_t)VM_KERNEL_UNSLIDE(prevlr
);
550 * Next frame; read the fp value into short_fp first
553 kern_virt_addr
= machine_trace_thread_get_kva(fp
, bt_vm_map
, thread_trace_flags
);
555 if (kern_virt_addr
) {
556 short_fp
= *(uint32_t *)kern_virt_addr
;
557 fp
= (vm_offset_t
) short_fp
;
560 if (thread_trace_flags
) {
561 *thread_trace_flags
|= kThreadTruncatedBT
;
565 /* Reset the target pmap */
566 machine_trace_thread_clear_validation_cache();
567 return (int)(((char *)tracebuf
) - tracepos
);
571 machine_trace_thread64(thread_t thread
,
577 uint32_t * thread_trace_flags
,
580 #pragma unused(sp_out)
582 #pragma unused(thread, tracepos, tracebound, nframes, user_p, trace_fp, thread_trace_flags)
584 #elif defined(__arm64__)
586 uint64_t * tracebuf
= (uint64_t *)tracepos
;
587 vm_size_t framesize
= (trace_fp
? 2 : 1) * sizeof(uint64_t);
589 vm_offset_t stacklimit
= 0;
590 vm_offset_t stacklimit_bottom
= 0;
595 vm_offset_t prevfp
= 0;
597 struct arm_saved_state
* state
;
598 vm_offset_t kern_virt_addr
= 0;
599 vm_map_t bt_vm_map
= VM_MAP_NULL
;
601 const boolean_t is_64bit_addr
= thread_is_64bit_addr(thread
);
603 nframes
= (tracebound
> tracepos
) ? MIN(nframes
, (int)((tracebound
- tracepos
) / framesize
)) : 0;
610 /* Examine the user savearea */
611 state
= thread
->machine
.upcb
;
612 stacklimit
= (is_64bit_addr
) ? MACH_VM_MAX_ADDRESS
: VM_MAX_ADDRESS
;
613 stacklimit_bottom
= (is_64bit_addr
) ? MACH_VM_MIN_ADDRESS
: VM_MIN_ADDRESS
;
615 /* Fake up a stack frame for the PC */
616 *tracebuf
++ = get_saved_state_pc(state
);
618 *tracebuf
++ = get_saved_state_sp(state
);
621 bt_vm_map
= thread
->task
->map
;
623 /* kstackptr may not always be there, so recompute it */
624 state
= &thread_get_kernel_state(thread
)->machine
.ss
;
625 stacklimit
= VM_MAX_KERNEL_ADDRESS
;
626 stacklimit_bottom
= VM_MIN_KERNEL_ADDRESS
;
627 bt_vm_map
= kernel_map
;
630 /* Get the frame pointer */
631 fp
= get_saved_state_fp(state
);
633 /* Fill in the current link register */
634 prevlr
= get_saved_state_lr(state
);
635 pc
= get_saved_state_pc(state
);
636 sp
= get_saved_state_sp(state
);
638 if (!user_p
&& !prevlr
&& !fp
&& !sp
&& !pc
) {
643 prevlr
= VM_KERNEL_UNSLIDE(prevlr
);
646 for (; framecount
< nframes
; framecount
++) {
647 *tracebuf
++ = prevlr
;
657 * Unaligned frame; given that the stack register must always be
658 * 16-byte aligned, we are assured 8-byte alignment of the saved
659 * frame pointer and link register.
661 if (fp
& 0x0000007) {
664 /* Frame is out of range, maybe a user FP while doing kernel BT */
665 if (fp
> stacklimit
) {
668 if (fp
< stacklimit_bottom
) {
671 /* Stack grows downward */
673 boolean_t switched_stacks
= FALSE
;
677 * As a special case, sometimes we are backtracing out of an interrupt
678 * handler, and the stack jumps downward because of the memory allocation
679 * pattern during early boot due to KASLR.
682 int max_cpu
= ml_get_max_cpu_number();
684 for (cpu
= 0; cpu
<= max_cpu
; cpu
++) {
685 cpu_data_t
*target_cpu_datap
;
687 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
688 if (target_cpu_datap
== (cpu_data_t
*)NULL
) {
692 if (prevfp
>= (target_cpu_datap
->intstack_top
- INTSTACK_SIZE
) && prevfp
< target_cpu_datap
->intstack_top
) {
693 switched_stacks
= TRUE
;
697 if (prevfp
>= (target_cpu_datap
->fiqstack_top
- FIQSTACK_SIZE
) && prevfp
< target_cpu_datap
->fiqstack_top
) {
698 switched_stacks
= TRUE
;
701 #elif defined(__arm64__)
702 if (prevfp
>= (target_cpu_datap
->excepstack_top
- EXCEPSTACK_SIZE
) && prevfp
< target_cpu_datap
->excepstack_top
) {
703 switched_stacks
= TRUE
;
711 if (!switched_stacks
) {
712 /* Corrupt frame pointer? */
717 /* Assume there's a saved link register, and read it */
718 kern_virt_addr
= machine_trace_thread_get_kva(fp
+ ARM64_LR_OFFSET
, bt_vm_map
, thread_trace_flags
);
720 if (!kern_virt_addr
) {
721 if (thread_trace_flags
) {
722 *thread_trace_flags
|= kThreadTruncatedBT
;
727 prevlr
= *(uint64_t *)kern_virt_addr
;
728 #if defined(HAS_APPLE_PAC)
729 /* return addresses on stack signed by arm64e ABI */
730 prevlr
= (uint64_t) ptrauth_strip((void *)prevlr
, ptrauth_key_return_address
);
733 prevlr
= VM_KERNEL_UNSLIDE(prevlr
);
738 kern_virt_addr
= machine_trace_thread_get_kva(fp
, bt_vm_map
, thread_trace_flags
);
740 if (kern_virt_addr
) {
741 fp
= *(uint64_t *)kern_virt_addr
;
744 if (thread_trace_flags
) {
745 *thread_trace_flags
|= kThreadTruncatedBT
;
749 /* Reset the target pmap */
750 machine_trace_thread_clear_validation_cache();
751 return (int)(((char *)tracebuf
) - tracepos
);
753 #error Unknown architecture.
758 kdp_ml_enter_debugger(void)
760 __asm__
volatile (".long 0xe7ffdefe");