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
);
70 void kdp_trap(unsigned int, struct arm_saved_state
* saved_state
);
72 extern vm_offset_t
machine_trace_thread_get_kva(vm_offset_t cur_target_addr
, vm_map_t map
, uint32_t *thread_trace_flags
);
73 extern void machine_trace_thread_clear_validation_cache(void);
74 extern vm_map_t kernel_map
;
76 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
79 unsigned char * pkt
, int * len
, unsigned short * remote_port
, unsigned int exception
, unsigned int code
, unsigned int subcode
)
86 kdp_exception_t
* rq
= (kdp_exception_t
*)&aligned_pkt
;
88 bcopy((char *)pkt
, (char *)rq
, sizeof(*rq
));
89 rq
->hdr
.request
= KDP_EXCEPTION
;
91 rq
->hdr
.seq
= kdp
.exception_seq
;
93 rq
->hdr
.len
= sizeof(*rq
) + sizeof(kdp_exc_info_t
);
96 rq
->exc_info
[0].cpu
= 0;
97 rq
->exc_info
[0].exception
= exception
;
98 rq
->exc_info
[0].code
= code
;
99 rq
->exc_info
[0].subcode
= subcode
;
101 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof(kdp_exc_info_t
);
103 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
105 kdp
.exception_ack_needed
= TRUE
;
107 *remote_port
= kdp
.exception_port
;
112 kdp_exception_ack(unsigned char * pkt
, int len
)
114 kdp_exception_ack_t aligned_pkt
;
115 kdp_exception_ack_t
* rq
= (kdp_exception_ack_t
*)&aligned_pkt
;
117 if ((unsigned)len
< sizeof(*rq
))
120 bcopy((char *)pkt
, (char *)rq
, sizeof(*rq
));
122 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
)
125 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
127 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
128 kdp
.exception_ack_needed
= FALSE
;
135 kdp_getintegerstate(char * out_state
)
138 struct arm_thread_state thread_state
;
139 struct arm_saved_state
*saved_state
;
141 saved_state
= kdp
.saved_state
;
143 bzero((char *) &thread_state
, sizeof(struct arm_thread_state
));
145 saved_state_to_thread_state32(saved_state
, &thread_state
);
147 bcopy((char *) &thread_state
, (char *) out_state
, sizeof(struct arm_thread_state
));
148 #elif defined(__arm64__)
149 struct arm_thread_state64 thread_state64
;
150 arm_saved_state_t
*saved_state
;
152 saved_state
= kdp
.saved_state
;
153 assert(is_saved_state64(saved_state
));
155 bzero((char *) &thread_state64
, sizeof(struct arm_thread_state64
));
157 saved_state_to_thread_state64(saved_state
, &thread_state64
);
159 bcopy((char *) &thread_state64
, (char *) out_state
, sizeof(struct arm_thread_state64
));
161 #error Unknown architecture.
166 kdp_machine_read_regs(__unused
unsigned int cpu
, unsigned int flavor
, char * data
, int * size
)
170 case ARM_THREAD_STATE
:
171 dprintf(("kdp_readregs THREAD_STATE\n"));
172 kdp_getintegerstate(data
);
173 *size
= ARM_THREAD_STATE_COUNT
* sizeof(int);
174 return KDPERR_NO_ERROR
;
175 #elif defined(__arm64__)
176 case ARM_THREAD_STATE64
:
177 dprintf(("kdp_readregs THREAD_STATE64\n"));
178 kdp_getintegerstate(data
);
179 *size
= ARM_THREAD_STATE64_COUNT
* sizeof(int);
180 return KDPERR_NO_ERROR
;
184 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
185 bzero((char *) data
, sizeof(struct arm_vfp_state
));
186 *size
= ARM_VFP_STATE_COUNT
* sizeof(int);
187 return KDPERR_NO_ERROR
;
190 dprintf(("kdp_readregs bad flavor %d\n"));
191 return KDPERR_BADFLAVOR
;
196 kdp_setintegerstate(char * state_in
)
199 struct arm_thread_state thread_state
;
200 struct arm_saved_state
*saved_state
;
202 bcopy((char *) state_in
, (char *) &thread_state
, sizeof(struct arm_thread_state
));
203 saved_state
= kdp
.saved_state
;
205 thread_state32_to_saved_state(&thread_state
, saved_state
);
206 #elif defined(__arm64__)
207 struct arm_thread_state64 thread_state64
;
208 struct arm_saved_state
*saved_state
;
210 bcopy((char *) state_in
, (char *) &thread_state64
, sizeof(struct arm_thread_state64
));
211 saved_state
= kdp
.saved_state
;
212 assert(is_saved_state64(saved_state
));
214 thread_state64_to_saved_state(&thread_state64
, saved_state
);
216 #error Unknown architecture.
221 kdp_machine_write_regs(__unused
unsigned int cpu
, unsigned int flavor
, char * data
, __unused
int * size
)
225 case ARM_THREAD_STATE
:
226 dprintf(("kdp_writeregs THREAD_STATE\n"));
227 kdp_setintegerstate(data
);
228 return KDPERR_NO_ERROR
;
229 #elif defined(__arm64__)
230 case ARM_THREAD_STATE64
:
231 dprintf(("kdp_writeregs THREAD_STATE64\n"));
232 kdp_setintegerstate(data
);
233 return KDPERR_NO_ERROR
;
237 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
238 return KDPERR_NO_ERROR
;
241 dprintf(("kdp_writeregs bad flavor %d\n"));
242 return KDPERR_BADFLAVOR
;
247 kdp_machine_hostinfo(kdp_hostinfo_t
* hostinfo
)
249 hostinfo
->cpus_mask
= 1;
250 hostinfo
->cpu_type
= slot_type(0);
251 hostinfo
->cpu_subtype
= slot_subtype(0);
254 __attribute__((noreturn
))
256 kdp_panic(const char * msg
)
258 printf("kdp panic: %s\n", msg
);
276 kdp_us_spin(int usec
)
284 Debugger("inline call to debugger(machine_startup)");
290 return (cnmaygetc());
294 kdp_machine_get_breakinsn(uint8_t * bytes
, uint32_t * size
)
296 *(uint32_t *)bytes
= GDB_TRAP_INSTR1
;
297 *size
= sizeof(uint32_t);
306 kdp_machine_ioport_read(kdp_readioport_req_t
* rq
, caddr_t data
, uint16_t lcpu
)
308 #pragma unused(rq, data, lcpu)
313 kdp_machine_ioport_write(kdp_writeioport_req_t
* rq
, caddr_t data
, uint16_t lcpu
)
315 #pragma unused(rq, data, lcpu)
320 kdp_machine_msr64_read(kdp_readmsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
322 #pragma unused(rq, data, lcpu)
327 kdp_machine_msr64_write(kdp_writemsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
329 #pragma unused(rq, data, lcpu)
332 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
335 kdp_trap(unsigned int exception
, struct arm_saved_state
* saved_state
)
337 handle_debugger_trap(exception
, 0, 0, saved_state
);
340 if (saved_state
->cpsr
& PSR_TF
) {
341 unsigned short instr
= *((unsigned short *)(saved_state
->pc
));
342 if ((instr
== (GDB_TRAP_INSTR1
& 0xFFFF)) || (instr
== (GDB_TRAP_INSTR2
& 0xFFFF)))
343 saved_state
->pc
+= 2;
345 unsigned int instr
= *((unsigned int *)(saved_state
->pc
));
346 if ((instr
== GDB_TRAP_INSTR1
) || (instr
== GDB_TRAP_INSTR2
))
347 saved_state
->pc
+= 4;
350 #elif defined(__arm64__)
351 assert(is_saved_state64(saved_state
));
353 uint32_t instr
= *((uint32_t *)get_saved_state_pc(saved_state
));
356 * As long as we are using the arm32 trap encoding to handling
357 * traps to the debugger, we should identify both variants and
358 * increment for both of them.
360 if ((instr
== GDB_TRAP_INSTR1
) || (instr
== GDB_TRAP_INSTR2
))
361 set_saved_state_pc(saved_state
, get_saved_state_pc(saved_state
) + 4);
363 #error Unknown architecture.
367 #define ARM32_LR_OFFSET 4
368 #define ARM64_LR_OFFSET 8
371 * Since sizeof (struct thread_snapshot) % 4 == 2
372 * make sure the compiler does not try to use word-aligned
373 * access to this data, which can result in alignment faults
374 * that can't be emulated in KDP context.
376 typedef uint32_t uint32_align2_t
__attribute__((aligned(2)));
379 machine_trace_thread(thread_t thread
,
385 uint32_t * thread_trace_flags
)
387 uint32_align2_t
* tracebuf
= (uint32_align2_t
*)tracepos
;
389 vm_size_t framesize
= (trace_fp
? 2 : 1) * sizeof(uint32_t);
391 vm_offset_t stacklimit
= 0;
392 vm_offset_t stacklimit_bottom
= 0;
394 uint32_t short_fp
= 0;
397 vm_offset_t prevfp
= 0;
399 struct arm_saved_state
* state
;
400 vm_offset_t kern_virt_addr
= 0;
401 vm_map_t bt_vm_map
= VM_MAP_NULL
;
403 nframes
= (tracebound
> tracepos
) ? MIN(nframes
, (int)((tracebound
- tracepos
) / framesize
)) : 0;
410 /* Examine the user savearea */
411 state
= get_user_regs(thread
);
412 stacklimit
= VM_MAX_ADDRESS
;
413 stacklimit_bottom
= VM_MIN_ADDRESS
;
415 /* Fake up a stack frame for the PC */
416 *tracebuf
++ = (uint32_t)get_saved_state_pc(state
);
418 *tracebuf
++ = (uint32_t)get_saved_state_sp(state
);
421 bt_vm_map
= thread
->task
->map
;
423 #if defined(__arm64__)
424 panic("Attempted to trace kernel thread_t %p as a 32-bit context", thread
);
426 #elif defined(__arm__)
427 /* kstackptr may not always be there, so recompute it */
428 state
= &thread_get_kernel_state(thread
)->machine
;
430 stacklimit
= VM_MAX_KERNEL_ADDRESS
;
431 stacklimit_bottom
= VM_MIN_KERNEL_ADDRESS
;
432 bt_vm_map
= kernel_map
;
434 #error Unknown architecture.
438 /* Get the frame pointer */
439 fp
= get_saved_state_fp(state
);
441 /* Fill in the current link register */
442 prevlr
= (uint32_t)get_saved_state_lr(state
);
443 pc
= get_saved_state_pc(state
);
444 sp
= get_saved_state_sp(state
);
446 if (!user_p
&& !prevlr
&& !fp
&& !sp
&& !pc
) {
451 /* This is safe since we will panic above on __arm64__ if !user_p */
452 prevlr
= (uint32_t)VM_KERNEL_UNSLIDE(prevlr
);
455 for (; framecount
< nframes
; framecount
++) {
457 *tracebuf
++ = prevlr
;
459 *tracebuf
++ = (uint32_t)fp
;
466 /* Unaligned frame */
467 if (fp
& 0x0000003) {
470 /* Frame is out of range, maybe a user FP while doing kernel BT */
471 if (fp
> stacklimit
) {
474 if (fp
< stacklimit_bottom
) {
477 /* Stack grows downward */
480 boolean_t prev_in_interrupt_stack
= FALSE
;
484 * As a special case, sometimes we are backtracing out of an interrupt
485 * handler, and the stack jumps downward because of the memory allocation
486 * pattern during early boot due to KASLR.
489 int max_cpu
= ml_get_max_cpu_number();
491 for (cpu
=0; cpu
<= max_cpu
; cpu
++) {
492 cpu_data_t
*target_cpu_datap
;
494 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
495 if(target_cpu_datap
== (cpu_data_t
*)NULL
)
498 if ((prevfp
>= (target_cpu_datap
->intstack_top
-INTSTACK_SIZE
) && prevfp
< target_cpu_datap
->intstack_top
) ||
499 (prevfp
>= (target_cpu_datap
->fiqstack_top
-PAGE_SIZE
) && prevfp
< target_cpu_datap
->fiqstack_top
)) {
500 prev_in_interrupt_stack
= TRUE
;
506 if (!prev_in_interrupt_stack
) {
507 /* Corrupt frame pointer? */
511 /* Assume there's a saved link register, and read it */
512 kern_virt_addr
= machine_trace_thread_get_kva(fp
+ ARM32_LR_OFFSET
, bt_vm_map
, thread_trace_flags
);
514 if (!kern_virt_addr
) {
515 if (thread_trace_flags
) {
516 *thread_trace_flags
|= kThreadTruncatedBT
;
521 prevlr
= *(uint32_t *)kern_virt_addr
;
523 /* This is safe since we will panic above on __arm64__ if !user_p */
524 prevlr
= (uint32_t)VM_KERNEL_UNSLIDE(prevlr
);
530 * Next frame; read the fp value into short_fp first
533 kern_virt_addr
= machine_trace_thread_get_kva(fp
, bt_vm_map
, thread_trace_flags
);
535 if (kern_virt_addr
) {
536 short_fp
= *(uint32_t *)kern_virt_addr
;
537 fp
= (vm_offset_t
) short_fp
;
540 if (thread_trace_flags
) {
541 *thread_trace_flags
|= kThreadTruncatedBT
;
546 /* Reset the target pmap */
547 machine_trace_thread_clear_validation_cache();
548 return ((int)(((char *)tracebuf
) - tracepos
));
552 machine_trace_thread64(thread_t thread
,
558 uint32_t * thread_trace_flags
)
561 #pragma unused(thread, tracepos, tracebound, nframes, user_p, trace_fp, thread_trace_flags)
563 #elif defined(__arm64__)
565 uint64_t * tracebuf
= (uint64_t *)tracepos
;
566 vm_size_t framesize
= (trace_fp
? 2 : 1) * sizeof(uint64_t);
568 vm_offset_t stacklimit
= 0;
569 vm_offset_t stacklimit_bottom
= 0;
574 vm_offset_t prevfp
= 0;
576 struct arm_saved_state
* state
;
577 vm_offset_t kern_virt_addr
= 0;
578 vm_map_t bt_vm_map
= VM_MAP_NULL
;
580 nframes
= (tracebound
> tracepos
) ? MIN(nframes
, (int)((tracebound
- tracepos
) / framesize
)) : 0;
587 /* Examine the user savearea */
588 state
= thread
->machine
.upcb
;
589 stacklimit
= MACH_VM_MAX_ADDRESS
;
590 stacklimit_bottom
= MACH_VM_MIN_ADDRESS
;
592 /* Fake up a stack frame for the PC */
593 *tracebuf
++ = get_saved_state_pc(state
);
595 *tracebuf
++ = get_saved_state_sp(state
);
598 bt_vm_map
= thread
->task
->map
;
600 /* kstackptr may not always be there, so recompute it */
601 state
= &thread_get_kernel_state(thread
)->machine
.ss
;
602 stacklimit
= VM_MAX_KERNEL_ADDRESS
;
603 stacklimit_bottom
= VM_MIN_KERNEL_ADDRESS
;
604 bt_vm_map
= kernel_map
;
607 /* Get the frame pointer */
608 fp
= get_saved_state_fp(state
);
610 /* Fill in the current link register */
611 prevlr
= get_saved_state_lr(state
);
612 pc
= get_saved_state_pc(state
);
613 sp
= get_saved_state_sp(state
);
615 if (!user_p
&& !prevlr
&& !fp
&& !sp
&& !pc
) {
620 prevlr
= VM_KERNEL_UNSLIDE(prevlr
);
623 for (; framecount
< nframes
; framecount
++) {
625 *tracebuf
++ = prevlr
;
635 * Unaligned frame; given that the stack register must always be
636 * 16-byte aligned, we are assured 8-byte alignment of the saved
637 * frame pointer and link register.
639 if (fp
& 0x0000007) {
642 /* Frame is out of range, maybe a user FP while doing kernel BT */
643 if (fp
> stacklimit
) {
646 if (fp
< stacklimit_bottom
) {
649 /* Stack grows downward */
651 boolean_t switched_stacks
= FALSE
;
655 * As a special case, sometimes we are backtracing out of an interrupt
656 * handler, and the stack jumps downward because of the memory allocation
657 * pattern during early boot due to KASLR.
660 int max_cpu
= ml_get_max_cpu_number();
662 for (cpu
=0; cpu
<= max_cpu
; cpu
++) {
663 cpu_data_t
*target_cpu_datap
;
665 target_cpu_datap
= (cpu_data_t
*)CpuDataEntries
[cpu
].cpu_data_vaddr
;
666 if(target_cpu_datap
== (cpu_data_t
*)NULL
)
669 if ((prevfp
>= (target_cpu_datap
->intstack_top
-INTSTACK_SIZE
) && prevfp
< target_cpu_datap
->intstack_top
) ||
670 (prevfp
>= (target_cpu_datap
->fiqstack_top
-PAGE_SIZE
) && prevfp
< target_cpu_datap
->fiqstack_top
)) {
671 switched_stacks
= TRUE
;
679 if (!switched_stacks
) {
680 /* Corrupt frame pointer? */
685 /* Assume there's a saved link register, and read it */
686 kern_virt_addr
= machine_trace_thread_get_kva(fp
+ ARM64_LR_OFFSET
, bt_vm_map
, thread_trace_flags
);
688 if (!kern_virt_addr
) {
689 if (thread_trace_flags
) {
690 *thread_trace_flags
|= kThreadTruncatedBT
;
695 prevlr
= *(uint64_t *)kern_virt_addr
;
697 prevlr
= VM_KERNEL_UNSLIDE(prevlr
);
702 kern_virt_addr
= machine_trace_thread_get_kva(fp
, bt_vm_map
, thread_trace_flags
);
704 if (kern_virt_addr
) {
705 fp
= *(uint64_t *)kern_virt_addr
;
708 if (thread_trace_flags
) {
709 *thread_trace_flags
|= kThreadTruncatedBT
;
714 /* Reset the target pmap */
715 machine_trace_thread_clear_validation_cache();
716 return ((int)(((char *)tracebuf
) - tracepos
));
718 #error Unknown architecture.
723 kdp_ml_enter_debugger(void)
725 __asm__
volatile(".long 0xe7ffdefe");