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>
45 #define KDP_TEST_HARNESS 0
47 #define dprintf(x) kprintf x
49 #define dprintf(x) do {} while (0)
52 void halt_all_cpus(boolean_t
);
55 int machine_trace_thread(thread_t thread
,
61 uint32_t * thread_trace_flags
);
62 int machine_trace_thread64(thread_t thread
,
68 uint32_t * thread_trace_flags
,
71 void kdp_trap(unsigned int, struct arm_saved_state
* saved_state
);
73 extern vm_offset_t
machine_trace_thread_get_kva(vm_offset_t cur_target_addr
, vm_map_t map
, uint32_t *thread_trace_flags
);
74 extern void machine_trace_thread_clear_validation_cache(void);
75 extern vm_map_t kernel_map
;
77 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
80 unsigned char * pkt
, int * len
, unsigned short * remote_port
, unsigned int exception
, unsigned int code
, unsigned int subcode
)
87 kdp_exception_t
* rq
= (kdp_exception_t
*)&aligned_pkt
;
89 bcopy((char *)pkt
, (char *)rq
, sizeof(*rq
));
90 rq
->hdr
.request
= KDP_EXCEPTION
;
92 rq
->hdr
.seq
= kdp
.exception_seq
;
94 rq
->hdr
.len
= sizeof(*rq
) + sizeof(kdp_exc_info_t
);
97 rq
->exc_info
[0].cpu
= 0;
98 rq
->exc_info
[0].exception
= exception
;
99 rq
->exc_info
[0].code
= code
;
100 rq
->exc_info
[0].subcode
= subcode
;
102 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof(kdp_exc_info_t
);
104 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
106 kdp
.exception_ack_needed
= TRUE
;
108 *remote_port
= kdp
.exception_port
;
113 kdp_exception_ack(unsigned char * pkt
, int len
)
115 kdp_exception_ack_t aligned_pkt
;
116 kdp_exception_ack_t
* rq
= (kdp_exception_ack_t
*)&aligned_pkt
;
118 if ((unsigned)len
< sizeof(*rq
)) {
122 bcopy((char *)pkt
, (char *)rq
, sizeof(*rq
));
124 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
) {
128 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
130 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
131 kdp
.exception_ack_needed
= FALSE
;
138 kdp_getintegerstate(char * out_state
)
141 struct arm_thread_state thread_state
;
142 struct arm_saved_state
*saved_state
;
144 saved_state
= kdp
.saved_state
;
146 bzero((char *) &thread_state
, sizeof(struct arm_thread_state
));
148 saved_state_to_thread_state32(saved_state
, &thread_state
);
150 bcopy((char *) &thread_state
, (char *) out_state
, sizeof(struct arm_thread_state
));
151 #elif defined(__arm64__)
152 struct arm_thread_state64 thread_state64
;
153 arm_saved_state_t
*saved_state
;
155 saved_state
= kdp
.saved_state
;
156 assert(is_saved_state64(saved_state
));
158 bzero((char *) &thread_state64
, sizeof(struct arm_thread_state64
));
160 saved_state_to_thread_state64(saved_state
, &thread_state64
);
162 bcopy((char *) &thread_state64
, (char *) out_state
, sizeof(struct arm_thread_state64
));
164 #error Unknown architecture.
169 kdp_machine_read_regs(__unused
unsigned int cpu
, unsigned int flavor
, char * data
, int * size
)
173 case ARM_THREAD_STATE
:
174 dprintf(("kdp_readregs THREAD_STATE\n"));
175 kdp_getintegerstate(data
);
176 *size
= ARM_THREAD_STATE_COUNT
* sizeof(int);
177 return KDPERR_NO_ERROR
;
178 #elif defined(__arm64__)
179 case ARM_THREAD_STATE64
:
180 dprintf(("kdp_readregs THREAD_STATE64\n"));
181 kdp_getintegerstate(data
);
182 *size
= ARM_THREAD_STATE64_COUNT
* sizeof(int);
183 return KDPERR_NO_ERROR
;
187 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
188 bzero((char *) data
, sizeof(struct arm_vfp_state
));
189 *size
= ARM_VFP_STATE_COUNT
* sizeof(int);
190 return KDPERR_NO_ERROR
;
193 dprintf(("kdp_readregs bad flavor %d\n"));
194 return KDPERR_BADFLAVOR
;
199 kdp_setintegerstate(char * state_in
)
202 struct arm_thread_state thread_state
;
203 struct arm_saved_state
*saved_state
;
205 bcopy((char *) state_in
, (char *) &thread_state
, sizeof(struct arm_thread_state
));
206 saved_state
= kdp
.saved_state
;
208 thread_state32_to_saved_state(&thread_state
, saved_state
);
209 #elif defined(__arm64__)
210 struct arm_thread_state64 thread_state64
;
211 struct arm_saved_state
*saved_state
;
213 bcopy((char *) state_in
, (char *) &thread_state64
, sizeof(struct arm_thread_state64
));
214 saved_state
= kdp
.saved_state
;
215 assert(is_saved_state64(saved_state
));
217 thread_state64_to_saved_state(&thread_state64
, saved_state
);
219 #error Unknown architecture.
224 kdp_machine_write_regs(__unused
unsigned int cpu
, unsigned int flavor
, char * data
, __unused
int * size
)
228 case ARM_THREAD_STATE
:
229 dprintf(("kdp_writeregs THREAD_STATE\n"));
230 kdp_setintegerstate(data
);
231 return KDPERR_NO_ERROR
;
232 #elif defined(__arm64__)
233 case ARM_THREAD_STATE64
:
234 dprintf(("kdp_writeregs THREAD_STATE64\n"));
235 kdp_setintegerstate(data
);
236 return KDPERR_NO_ERROR
;
240 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
241 return KDPERR_NO_ERROR
;
244 dprintf(("kdp_writeregs bad flavor %d\n"));
245 return KDPERR_BADFLAVOR
;
250 kdp_machine_hostinfo(kdp_hostinfo_t
* hostinfo
)
252 hostinfo
->cpus_mask
= 1;
253 hostinfo
->cpu_type
= slot_type(0);
254 hostinfo
->cpu_subtype
= slot_subtype(0);
257 __attribute__((noreturn
))
259 kdp_panic(const char * msg
)
261 printf("kdp panic: %s\n", msg
);
280 kdp_us_spin(int usec
)
288 Debugger("inline call to debugger(machine_startup)");
298 kdp_machine_get_breakinsn(uint8_t * bytes
, uint32_t * size
)
300 *(uint32_t *)bytes
= GDB_TRAP_INSTR1
;
301 *size
= sizeof(uint32_t);
310 kdp_machine_ioport_read(kdp_readioport_req_t
* rq
, caddr_t data
, uint16_t lcpu
)
312 #pragma unused(rq, data, lcpu)
317 kdp_machine_ioport_write(kdp_writeioport_req_t
* rq
, caddr_t data
, uint16_t lcpu
)
319 #pragma unused(rq, data, lcpu)
324 kdp_machine_msr64_read(kdp_readmsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
326 #pragma unused(rq, data, lcpu)
331 kdp_machine_msr64_write(kdp_writemsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
333 #pragma unused(rq, data, lcpu)
336 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
339 kdp_trap(unsigned int exception
, struct arm_saved_state
* saved_state
)
341 handle_debugger_trap(exception
, 0, 0, saved_state
);
344 if (saved_state
->cpsr
& PSR_TF
) {
345 unsigned short instr
= *((unsigned short *)(saved_state
->pc
));
346 if ((instr
== (GDB_TRAP_INSTR1
& 0xFFFF)) || (instr
== (GDB_TRAP_INSTR2
& 0xFFFF))) {
347 saved_state
->pc
+= 2;
350 unsigned int instr
= *((unsigned int *)(saved_state
->pc
));
351 if ((instr
== GDB_TRAP_INSTR1
) || (instr
== GDB_TRAP_INSTR2
)) {
352 saved_state
->pc
+= 4;
356 #elif defined(__arm64__)
357 assert(is_saved_state64(saved_state
));
359 uint32_t instr
= *((uint32_t *)get_saved_state_pc(saved_state
));
362 * As long as we are using the arm32 trap encoding to handling
363 * traps to the debugger, we should identify both variants and
364 * increment for both of them.
366 if ((instr
== GDB_TRAP_INSTR1
) || (instr
== GDB_TRAP_INSTR2
)) {
367 set_saved_state_pc(saved_state
, get_saved_state_pc(saved_state
) + 4);
370 #error Unknown architecture.
374 #define ARM32_LR_OFFSET 4
375 #define ARM64_LR_OFFSET 8
378 * Since sizeof (struct thread_snapshot) % 4 == 2
379 * make sure the compiler does not try to use word-aligned
380 * access to this data, which can result in alignment faults
381 * that can't be emulated in KDP context.
383 typedef uint32_t uint32_align2_t
__attribute__((aligned(2)));
386 machine_trace_thread(thread_t thread
,
392 uint32_t * thread_trace_flags
)
394 uint32_align2_t
* tracebuf
= (uint32_align2_t
*)tracepos
;
396 vm_size_t framesize
= (trace_fp
? 2 : 1) * sizeof(uint32_t);
398 vm_offset_t stacklimit
= 0;
399 vm_offset_t stacklimit_bottom
= 0;
401 uint32_t short_fp
= 0;
404 vm_offset_t prevfp
= 0;
406 struct arm_saved_state
* state
;
407 vm_offset_t kern_virt_addr
= 0;
408 vm_map_t bt_vm_map
= VM_MAP_NULL
;
410 nframes
= (tracebound
> tracepos
) ? MIN(nframes
, (int)((tracebound
- tracepos
) / framesize
)) : 0;
417 /* Examine the user savearea */
418 state
= get_user_regs(thread
);
419 stacklimit
= VM_MAX_ADDRESS
;
420 stacklimit_bottom
= VM_MIN_ADDRESS
;
422 /* Fake up a stack frame for the PC */
423 *tracebuf
++ = (uint32_t)get_saved_state_pc(state
);
425 *tracebuf
++ = (uint32_t)get_saved_state_sp(state
);
428 bt_vm_map
= thread
->task
->map
;
430 #if defined(__arm64__)
431 panic("Attempted to trace kernel thread_t %p as a 32-bit context", thread
);
433 #elif defined(__arm__)
434 /* kstackptr may not always be there, so recompute it */
435 state
= &thread_get_kernel_state(thread
)->machine
;
437 stacklimit
= VM_MAX_KERNEL_ADDRESS
;
438 stacklimit_bottom
= VM_MIN_KERNEL_ADDRESS
;
439 bt_vm_map
= kernel_map
;
441 #error Unknown architecture.
445 /* Get the frame pointer */
446 fp
= get_saved_state_fp(state
);
448 /* Fill in the current link register */
449 prevlr
= (uint32_t)get_saved_state_lr(state
);
450 pc
= get_saved_state_pc(state
);
451 sp
= get_saved_state_sp(state
);
453 if (!user_p
&& !prevlr
&& !fp
&& !sp
&& !pc
) {
458 /* This is safe since we will panic above on __arm64__ if !user_p */
459 prevlr
= (uint32_t)VM_KERNEL_UNSLIDE(prevlr
);
462 for (; framecount
< nframes
; framecount
++) {
463 *tracebuf
++ = prevlr
;
465 *tracebuf
++ = (uint32_t)fp
;
472 /* Unaligned frame */
473 if (fp
& 0x0000003) {
476 /* Frame is out of range, maybe a user FP while doing kernel BT */
477 if (fp
> stacklimit
) {
480 if (fp
< stacklimit_bottom
) {
483 /* Stack grows downward */
485 boolean_t prev_in_interrupt_stack
= FALSE
;
489 * As a special case, sometimes we are backtracing out of an interrupt
490 * handler, and the stack jumps downward because of the memory allocation
491 * pattern during early boot due to KASLR.
494 int max_cpu
= ml_get_max_cpu_number();
496 for (cpu
= 0; cpu
<= max_cpu
; cpu
++) {
497 cpu_data_t
*target_cpu_datap
;
499 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
500 if (target_cpu_datap
== (cpu_data_t
*)NULL
) {
504 if (prevfp
>= (target_cpu_datap
->intstack_top
- INTSTACK_SIZE
) && prevfp
< target_cpu_datap
->intstack_top
) {
505 prev_in_interrupt_stack
= TRUE
;
510 if (prevfp
>= (target_cpu_datap
->fiqstack_top
- FIQSTACK_SIZE
) && prevfp
< target_cpu_datap
->fiqstack_top
) {
511 prev_in_interrupt_stack
= TRUE
;
514 #elif defined(__arm64__)
515 if (prevfp
>= (target_cpu_datap
->excepstack_top
- EXCEPSTACK_SIZE
) && prevfp
< target_cpu_datap
->excepstack_top
) {
516 prev_in_interrupt_stack
= TRUE
;
523 if (!prev_in_interrupt_stack
) {
524 /* Corrupt frame pointer? */
528 /* Assume there's a saved link register, and read it */
529 kern_virt_addr
= machine_trace_thread_get_kva(fp
+ ARM32_LR_OFFSET
, bt_vm_map
, thread_trace_flags
);
531 if (!kern_virt_addr
) {
532 if (thread_trace_flags
) {
533 *thread_trace_flags
|= kThreadTruncatedBT
;
538 prevlr
= *(uint32_t *)kern_virt_addr
;
540 /* This is safe since we will panic above on __arm64__ if !user_p */
541 prevlr
= (uint32_t)VM_KERNEL_UNSLIDE(prevlr
);
547 * Next frame; read the fp value into short_fp first
550 kern_virt_addr
= machine_trace_thread_get_kva(fp
, bt_vm_map
, thread_trace_flags
);
552 if (kern_virt_addr
) {
553 short_fp
= *(uint32_t *)kern_virt_addr
;
554 fp
= (vm_offset_t
) short_fp
;
557 if (thread_trace_flags
) {
558 *thread_trace_flags
|= kThreadTruncatedBT
;
562 /* Reset the target pmap */
563 machine_trace_thread_clear_validation_cache();
564 return (int)(((char *)tracebuf
) - tracepos
);
568 machine_trace_thread64(thread_t thread
,
574 uint32_t * thread_trace_flags
,
577 #pragma unused(sp_out)
579 #pragma unused(thread, tracepos, tracebound, nframes, user_p, trace_fp, thread_trace_flags)
581 #elif defined(__arm64__)
583 uint64_t * tracebuf
= (uint64_t *)tracepos
;
584 vm_size_t framesize
= (trace_fp
? 2 : 1) * sizeof(uint64_t);
586 vm_offset_t stacklimit
= 0;
587 vm_offset_t stacklimit_bottom
= 0;
592 vm_offset_t prevfp
= 0;
594 struct arm_saved_state
* state
;
595 vm_offset_t kern_virt_addr
= 0;
596 vm_map_t bt_vm_map
= VM_MAP_NULL
;
598 const boolean_t is_64bit_addr
= thread_is_64bit_addr(thread
);
600 nframes
= (tracebound
> tracepos
) ? MIN(nframes
, (int)((tracebound
- tracepos
) / framesize
)) : 0;
607 /* Examine the user savearea */
608 state
= thread
->machine
.upcb
;
609 stacklimit
= (is_64bit_addr
) ? MACH_VM_MAX_ADDRESS
: VM_MAX_ADDRESS
;
610 stacklimit_bottom
= (is_64bit_addr
) ? MACH_VM_MIN_ADDRESS
: VM_MIN_ADDRESS
;
612 /* Fake up a stack frame for the PC */
613 *tracebuf
++ = get_saved_state_pc(state
);
615 *tracebuf
++ = get_saved_state_sp(state
);
618 bt_vm_map
= thread
->task
->map
;
620 /* kstackptr may not always be there, so recompute it */
621 state
= &thread_get_kernel_state(thread
)->machine
.ss
;
622 stacklimit
= VM_MAX_KERNEL_ADDRESS
;
623 stacklimit_bottom
= VM_MIN_KERNEL_ADDRESS
;
624 bt_vm_map
= kernel_map
;
627 /* Get the frame pointer */
628 fp
= get_saved_state_fp(state
);
630 /* Fill in the current link register */
631 prevlr
= get_saved_state_lr(state
);
632 pc
= get_saved_state_pc(state
);
633 sp
= get_saved_state_sp(state
);
635 if (!user_p
&& !prevlr
&& !fp
&& !sp
&& !pc
) {
640 prevlr
= VM_KERNEL_UNSLIDE(prevlr
);
643 for (; framecount
< nframes
; framecount
++) {
644 *tracebuf
++ = prevlr
;
654 * Unaligned frame; given that the stack register must always be
655 * 16-byte aligned, we are assured 8-byte alignment of the saved
656 * frame pointer and link register.
658 if (fp
& 0x0000007) {
661 /* Frame is out of range, maybe a user FP while doing kernel BT */
662 if (fp
> stacklimit
) {
665 if (fp
< stacklimit_bottom
) {
668 /* Stack grows downward */
670 boolean_t switched_stacks
= FALSE
;
674 * As a special case, sometimes we are backtracing out of an interrupt
675 * handler, and the stack jumps downward because of the memory allocation
676 * pattern during early boot due to KASLR.
679 int max_cpu
= ml_get_max_cpu_number();
681 for (cpu
= 0; cpu
<= max_cpu
; cpu
++) {
682 cpu_data_t
*target_cpu_datap
;
684 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
685 if (target_cpu_datap
== (cpu_data_t
*)NULL
) {
689 if (prevfp
>= (target_cpu_datap
->intstack_top
- INTSTACK_SIZE
) && prevfp
< target_cpu_datap
->intstack_top
) {
690 switched_stacks
= TRUE
;
694 if (prevfp
>= (target_cpu_datap
->fiqstack_top
- FIQSTACK_SIZE
) && prevfp
< target_cpu_datap
->fiqstack_top
) {
695 switched_stacks
= TRUE
;
698 #elif defined(__arm64__)
699 if (prevfp
>= (target_cpu_datap
->excepstack_top
- EXCEPSTACK_SIZE
) && prevfp
< target_cpu_datap
->excepstack_top
) {
700 switched_stacks
= TRUE
;
708 if (!switched_stacks
) {
709 /* Corrupt frame pointer? */
714 /* Assume there's a saved link register, and read it */
715 kern_virt_addr
= machine_trace_thread_get_kva(fp
+ ARM64_LR_OFFSET
, bt_vm_map
, thread_trace_flags
);
717 if (!kern_virt_addr
) {
718 if (thread_trace_flags
) {
719 *thread_trace_flags
|= kThreadTruncatedBT
;
724 prevlr
= *(uint64_t *)kern_virt_addr
;
726 prevlr
= VM_KERNEL_UNSLIDE(prevlr
);
731 kern_virt_addr
= machine_trace_thread_get_kva(fp
, bt_vm_map
, thread_trace_flags
);
733 if (kern_virt_addr
) {
734 fp
= *(uint64_t *)kern_virt_addr
;
737 if (thread_trace_flags
) {
738 *thread_trace_flags
|= kThreadTruncatedBT
;
742 /* Reset the target pmap */
743 machine_trace_thread_clear_validation_cache();
744 return (int)(((char *)tracebuf
) - tracepos
);
746 #error Unknown architecture.
751 kdp_ml_enter_debugger(void)
753 __asm__
volatile (".long 0xe7ffdefe");