2 * Copyright (c) 2000-2006 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@
30 #include <mach/mach_types.h>
31 #include <mach/machine.h>
32 #include <mach/exception_types.h>
33 #include <kern/cpu_data.h>
34 #include <i386/trap.h>
36 #include <kdp/kdp_internal.h>
37 #include <kdp/kdp_callout.h>
38 #include <mach-o/loader.h>
39 #include <mach-o/nlist.h>
40 #include <IOKit/IOPlatformExpert.h> /* for PE_halt_restart */
41 #include <kern/machine.h> /* for halt_all_cpus */
43 #include <kern/thread.h>
44 #include <i386/thread.h>
45 #include <vm/vm_map.h>
46 #include <i386/pmap.h>
47 #include <kern/kalloc.h>
49 #define KDP_TEST_HARNESS 0
51 #define dprintf(x) printf x
56 extern cpu_type_t
cpuid_cputype(void);
57 extern cpu_subtype_t
cpuid_cpusubtype(void);
59 void print_saved_state(void *);
62 boolean_t
kdp_call_kdb(void);
63 void kdp_getstate(i386_thread_state_t
*);
64 void kdp_setstate(i386_thread_state_t
*);
65 void kdp_print_phys(int);
68 machine_trace_thread(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
);
71 machine_trace_thread64(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
);
74 machine_read64(addr64_t srcaddr
, caddr_t dstaddr
, uint32_t len
);
76 extern unsigned kdp_vm_read(caddr_t src
, caddr_t dst
, unsigned len
);
78 static void kdp_callouts(kdp_event_t event
);
84 unsigned short *remote_port
,
85 unsigned int exception
,
90 kdp_exception_t
*rq
= (kdp_exception_t
*)pkt
;
92 rq
->hdr
.request
= KDP_EXCEPTION
;
94 rq
->hdr
.seq
= kdp
.exception_seq
;
96 rq
->hdr
.len
= sizeof (*rq
);
99 rq
->exc_info
[0].cpu
= 0;
100 rq
->exc_info
[0].exception
= exception
;
101 rq
->exc_info
[0].code
= code
;
102 rq
->exc_info
[0].subcode
= subcode
;
104 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof (kdp_exc_info_t
);
106 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
108 kdp
.exception_ack_needed
= TRUE
;
110 *remote_port
= kdp
.exception_port
;
120 kdp_exception_ack_t
*rq
= (kdp_exception_ack_t
*)pkt
;
122 if (((unsigned int) len
) < sizeof (*rq
))
125 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
;
139 x86_thread_state32_t
*state
142 static x86_thread_state32_t null_state
;
143 x86_saved_state32_t
*saved_state
;
145 saved_state
= (x86_saved_state32_t
*)kdp
.saved_state
;
148 state
->eax
= saved_state
->eax
;
149 state
->ebx
= saved_state
->ebx
;
150 state
->ecx
= saved_state
->ecx
;
151 state
->edx
= saved_state
->edx
;
152 state
->edi
= saved_state
->edi
;
153 state
->esi
= saved_state
->esi
;
154 state
->ebp
= saved_state
->ebp
;
156 if ((saved_state
->cs
& 0x3) == 0){ /* Kernel State */
157 state
->esp
= (unsigned int) &saved_state
->uesp
;
158 state
->ss
= KERNEL_DS
;
160 state
->esp
= saved_state
->uesp
;
161 state
->ss
= saved_state
->ss
;
164 state
->eflags
= saved_state
->efl
;
165 state
->eip
= saved_state
->eip
;
166 state
->cs
= saved_state
->cs
;
167 state
->ds
= saved_state
->ds
;
168 state
->es
= saved_state
->es
;
169 state
->fs
= saved_state
->fs
;
170 state
->gs
= saved_state
->gs
;
176 x86_thread_state32_t
*state
179 x86_saved_state32_t
*saved_state
;
181 saved_state
= (x86_saved_state32_t
*)kdp
.saved_state
;
183 saved_state
->eax
= state
->eax
;
184 saved_state
->ebx
= state
->ebx
;
185 saved_state
->ecx
= state
->ecx
;
186 saved_state
->edx
= state
->edx
;
187 saved_state
->edi
= state
->edi
;
188 saved_state
->esi
= state
->esi
;
189 saved_state
->ebp
= state
->ebp
;
190 saved_state
->efl
= state
->eflags
;
192 saved_state
->frame
.eflags
&= ~( EFL_VM
| EFL_NT
| EFL_IOPL
| EFL_CLR
);
193 saved_state
->frame
.eflags
|= ( EFL_IF
| EFL_SET
);
195 saved_state
->eip
= state
->eip
;
196 saved_state
->fs
= state
->fs
;
197 saved_state
->gs
= state
->gs
;
202 kdp_machine_read_regs(
203 __unused
unsigned int cpu
,
204 __unused
unsigned int flavor
,
209 static x86_float_state32_t null_fpstate
;
213 case x86_THREAD_STATE32
:
214 dprintf(("kdp_readregs THREAD_STATE\n"));
215 kdp_getstate((x86_thread_state32_t
*)data
);
216 *size
= sizeof (x86_thread_state32_t
);
217 return KDPERR_NO_ERROR
;
219 case x86_FLOAT_STATE32
:
220 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
221 *(x86_float_state32_t
*)data
= null_fpstate
;
222 *size
= sizeof (x86_float_state32_t
);
223 return KDPERR_NO_ERROR
;
226 dprintf(("kdp_readregs bad flavor %d\n", flavor
));
228 return KDPERR_BADFLAVOR
;
233 kdp_machine_write_regs(
234 __unused
unsigned int cpu
,
242 case x86_THREAD_STATE32
:
243 dprintf(("kdp_writeregs THREAD_STATE\n"));
244 kdp_setstate((x86_thread_state32_t
*)data
);
245 return KDPERR_NO_ERROR
;
247 case x86_FLOAT_STATE32
:
248 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
249 return KDPERR_NO_ERROR
;
252 dprintf(("kdp_writeregs bad flavor %d\n"));
253 return KDPERR_BADFLAVOR
;
260 kdp_machine_hostinfo(
261 kdp_hostinfo_t
*hostinfo
266 hostinfo
->cpus_mask
= 0;
268 for (i
= 0; i
< machine_info
.max_cpus
; i
++) {
269 if (cpu_data_ptr
[i
] == NULL
)
272 hostinfo
->cpus_mask
|= (1 << i
);
275 hostinfo
->cpu_type
= cpuid_cputype();
276 hostinfo
->cpu_subtype
= cpuid_cpusubtype();
281 #if CONFIG_NO_KPRINTF_STRINGS
282 __unused
const char *msg
288 kprintf("kdp panic: %s\n", msg
);
289 __asm__
volatile("hlt");
296 printf("Attempting system restart...");
297 /* Call the platform specific restart*/
299 (*PE_halt_restart
)(kPERestartCPU
);
300 /* If we do reach this, give up */
323 kdp_us_spin(int usec
)
328 void print_saved_state(void *state
)
330 x86_saved_state32_t
*saved_state
;
334 kprintf("pc = 0x%x\n", saved_state
->eip
);
335 kprintf("cr2= 0x%x\n", saved_state
->cr2
);
336 kprintf("rp = TODO FIXME\n");
337 kprintf("sp = %p\n", saved_state
);
344 return; /* No op here. */
350 __asm__
volatile ("int $3"); /* Let the processor do the work */
354 typedef struct _cframe_t
{
355 struct _cframe_t
*prev
;
360 #include <i386/pmap.h>
361 extern pt_entry_t
*DMAP2
;
362 extern caddr_t DADDR2
;
365 kdp_print_phys(int src
)
370 *(int *) DMAP2
= 0x63 | (src
& 0xfffff000);
371 invlpg((u_int
) DADDR2
);
372 iptr
= (unsigned int *) DADDR2
;
373 for (i
= 0; i
< 100; i
++) {
374 kprintf("0x%x ", *iptr
++);
386 x86_saved_state32_t
*saved_state
,
387 kern_return_t result
,
391 unsigned int exception
, subcode
= 0, code
;
393 if (trapno
!= T_INT3
&& trapno
!= T_DEBUG
) {
394 kprintf("Debugger: Unexpected kernel trap number: "
395 "0x%x, EIP: 0x%x, CR2: 0x%x\n",
396 trapno
, saved_state
->eip
, saved_state
->cr2
);
402 kdp_callouts(KDP_EVENT_ENTER
);
404 if (saved_state
->efl
& EFL_TF
) {
405 enable_preemption_no_check();
411 exception
= EXC_ARITHMETIC
;
412 code
= EXC_I386_DIVERR
;
416 exception
= EXC_SOFTWARE
;
417 code
= EXC_I386_INTOFLT
;
420 case T_OUT_OF_BOUNDS
:
421 exception
= EXC_ARITHMETIC
;
422 code
= EXC_I386_BOUNDFLT
;
425 case T_INVALID_OPCODE
:
426 exception
= EXC_BAD_INSTRUCTION
;
427 code
= EXC_I386_INVOPFLT
;
430 case T_SEGMENT_NOT_PRESENT
:
431 exception
= EXC_BAD_INSTRUCTION
;
432 code
= EXC_I386_SEGNPFLT
;
433 subcode
= saved_state
->err
;
437 exception
= EXC_BAD_INSTRUCTION
;
438 code
= EXC_I386_STKFLT
;
439 subcode
= saved_state
->err
;
442 case T_GENERAL_PROTECTION
:
443 exception
= EXC_BAD_INSTRUCTION
;
444 code
= EXC_I386_GPFLT
;
445 subcode
= saved_state
->err
;
449 exception
= EXC_BAD_ACCESS
;
455 exception
= EXC_SOFTWARE
;
456 code
= EXC_I386_ALIGNFLT
;
461 exception
= EXC_BREAKPOINT
;
462 code
= EXC_I386_BPTFLT
;
466 exception
= EXC_BAD_INSTRUCTION
;
471 kdp_raise_exception(exception
, code
, subcode
, saved_state
);
472 /* If the instruction single step bit is set, disable kernel preemption
474 if (saved_state
->efl
& EFL_TF
) {
475 disable_preemption();
478 kdp_callouts(KDP_EVENT_EXIT
);
492 kdp_ml_get_breakinsn(void)
496 extern pmap_t kdp_pmap
;
497 extern uint32_t kdp_src_high32
;
499 #define RETURN_OFFSET 4
501 machine_trace_thread(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
)
503 uint32_t *tracebuf
= (uint32_t *)tracepos
;
505 uint32_t stackptr
= 0;
506 uint32_t stacklimit
= 0xfc000000;
508 uint32_t init_eip
= 0;
510 uint32_t framesize
= 2 * sizeof(vm_offset_t
);
513 x86_saved_state32_t
*iss32
;
515 iss32
= USER_REGS32(thread
);
517 init_eip
= iss32
->eip
;
518 stackptr
= iss32
->ebp
;
520 /* This bound isn't useful, but it doesn't hinder us*/
521 stacklimit
= 0xffffffff;
522 kdp_pmap
= thread
->task
->map
->pmap
;
525 /*Examine the i386_saved_state at the base of the kernel stack*/
526 stackptr
= STACK_IKS(thread
->kernel_stack
)->k_ebp
;
527 init_eip
= STACK_IKS(thread
->kernel_stack
)->k_eip
;
530 *tracebuf
++ = init_eip
;
532 for (framecount
= 0; framecount
< nframes
; framecount
++) {
534 if ((uint32_t)(tracebound
- ((char *)tracebuf
)) < (4 * framesize
)) {
539 *tracebuf
++ = stackptr
;
540 /* Invalid frame, or hit fence */
541 if (!stackptr
|| (stackptr
== fence
)) {
544 /* Stack grows downward */
545 if (stackptr
< prevsp
) {
548 /* Unaligned frame */
549 if (stackptr
& 0x0000003) {
552 if (stackptr
> stacklimit
) {
556 if (kdp_vm_read((caddr_t
) (stackptr
+ RETURN_OFFSET
), (caddr_t
) tracebuf
, sizeof(caddr_t
)) != sizeof(caddr_t
)) {
562 if (kdp_vm_read((caddr_t
) stackptr
, (caddr_t
) &stackptr
, sizeof(caddr_t
)) != sizeof(caddr_t
)) {
570 return (uint32_t) (((char *) tracebuf
) - tracepos
);
573 #define RETURN_OFFSET64 8
574 /* Routine to encapsulate the 64-bit address read hack*/
576 machine_read64(addr64_t srcaddr
, caddr_t dstaddr
, uint32_t len
)
578 uint32_t kdp_vm_read_low32
;
581 kdp_src_high32
= srcaddr
>> 32;
582 kdp_vm_read_low32
= srcaddr
& 0x00000000FFFFFFFFUL
;
583 retval
= kdp_vm_read((caddr_t
)kdp_vm_read_low32
, dstaddr
, len
);
589 machine_trace_thread64(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
)
591 uint64_t *tracebuf
= (uint64_t *)tracepos
;
593 addr64_t stackptr
= 0;
594 uint64_t stacklimit
= 0xfc000000;
596 addr64_t init_rip
= 0;
598 unsigned framesize
= 2 * sizeof(addr64_t
);
601 x86_saved_state64_t
*iss64
;
602 iss64
= USER_REGS64(thread
);
603 init_rip
= iss64
->isf
.rip
;
604 stackptr
= iss64
->rbp
;
605 stacklimit
= 0xffffffffffffffffULL
;
606 kdp_pmap
= thread
->task
->map
->pmap
;
609 /* DRK: This would need to adapt for a 64-bit kernel, if any */
610 stackptr
= STACK_IKS(thread
->kernel_stack
)->k_ebp
;
611 init_rip
= STACK_IKS(thread
->kernel_stack
)->k_eip
;
614 *tracebuf
++ = init_rip
;
616 for (framecount
= 0; framecount
< nframes
; framecount
++) {
618 if ((uint32_t)(tracebound
- ((char *)tracebuf
)) < (4 * framesize
)) {
623 *tracebuf
++ = stackptr
;
625 if (!stackptr
|| (stackptr
== fence
)){
628 if (stackptr
< prevsp
) {
631 if (stackptr
& 0x0000003) {
634 if (stackptr
> stacklimit
) {
638 if (machine_read64(stackptr
+ RETURN_OFFSET64
, (caddr_t
) tracebuf
, sizeof(addr64_t
)) != sizeof(addr64_t
)) {
644 if (machine_read64(stackptr
, (caddr_t
) &stackptr
, sizeof(addr64_t
)) != sizeof(addr64_t
)) {
652 return (uint32_t) (((char *) tracebuf
) - tracepos
);
655 static struct kdp_callout
{
656 struct kdp_callout
*callout_next
;
657 kdp_callout_fn_t callout_fn
;
659 } *kdp_callout_list
= NULL
;
663 * Called from kernel context to register a kdp event callout.
666 kdp_register_callout(
670 struct kdp_callout
*kcp
;
671 struct kdp_callout
*list_head
;
673 kcp
= kalloc(sizeof(*kcp
));
675 panic("kdp_register_callout() kalloc failed");
677 kcp
->callout_fn
= fn
;
678 kcp
->callout_arg
= arg
;
680 /* Lock-less list insertion using compare and exchange. */
682 list_head
= kdp_callout_list
;
683 kcp
->callout_next
= list_head
;
684 } while(!atomic_cmpxchg((uint32_t *) &kdp_callout_list
,
685 (uint32_t) list_head
,
690 * Called at exception/panic time when extering or exiting kdp.
691 * We are single-threaded at this time and so we don't use locks.
694 kdp_callouts(kdp_event_t event
)
696 struct kdp_callout
*kcp
= kdp_callout_list
;
699 kcp
->callout_fn(kcp
->callout_arg
, event
);
700 kcp
= kcp
->callout_next
;
705 kdp_ml_enter_debugger(void)
707 __asm__
__volatile__("int3");