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 */
42 #include <libkern/OSAtomic.h>
44 #include <kern/thread.h>
45 #include <i386/thread.h>
46 #include <vm/vm_map.h>
47 #include <i386/pmap.h>
48 #include <kern/kalloc.h>
50 #define KDP_TEST_HARNESS 0
52 #define dprintf(x) printf x
57 extern cpu_type_t
cpuid_cputype(void);
58 extern cpu_subtype_t
cpuid_cpusubtype(void);
60 void print_saved_state(void *);
63 boolean_t
kdp_call_kdb(void);
64 void kdp_getstate(i386_thread_state_t
*);
65 void kdp_setstate(i386_thread_state_t
*);
66 void kdp_print_phys(int);
69 machine_trace_thread(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
);
72 machine_trace_thread64(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
);
75 machine_read64(addr64_t srcaddr
, caddr_t dstaddr
, uint32_t len
);
77 static void kdp_callouts(kdp_event_t event
);
83 unsigned short *remote_port
,
84 unsigned int exception
,
89 kdp_exception_t
*rq
= (kdp_exception_t
*)pkt
;
91 rq
->hdr
.request
= KDP_EXCEPTION
;
93 rq
->hdr
.seq
= kdp
.exception_seq
;
95 rq
->hdr
.len
= sizeof (*rq
);
98 rq
->exc_info
[0].cpu
= 0;
99 rq
->exc_info
[0].exception
= exception
;
100 rq
->exc_info
[0].code
= code
;
101 rq
->exc_info
[0].subcode
= subcode
;
103 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof (kdp_exc_info_t
);
105 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
107 kdp
.exception_ack_needed
= TRUE
;
109 *remote_port
= kdp
.exception_port
;
119 kdp_exception_ack_t
*rq
= (kdp_exception_ack_t
*)pkt
;
121 if (((unsigned int) len
) < sizeof (*rq
))
124 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
)
127 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
129 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
130 kdp
.exception_ack_needed
= FALSE
;
138 x86_thread_state32_t
*state
141 static x86_thread_state32_t null_state
;
142 x86_saved_state32_t
*saved_state
;
144 saved_state
= (x86_saved_state32_t
*)kdp
.saved_state
;
147 state
->eax
= saved_state
->eax
;
148 state
->ebx
= saved_state
->ebx
;
149 state
->ecx
= saved_state
->ecx
;
150 state
->edx
= saved_state
->edx
;
151 state
->edi
= saved_state
->edi
;
152 state
->esi
= saved_state
->esi
;
153 state
->ebp
= saved_state
->ebp
;
155 if ((saved_state
->cs
& SEL_PL
) == SEL_PL_K
) { /* Kernel state? */
156 if (cpu_mode_is64bit())
157 state
->esp
= (uint32_t) saved_state
->uesp
;
159 state
->esp
= ((uint32_t)saved_state
) + offsetof(x86_saved_state_t
, ss_32
) + sizeof(x86_saved_state32_t
);
160 state
->ss
= KERNEL_DS
;
162 state
->esp
= saved_state
->uesp
;
163 state
->ss
= saved_state
->ss
;
166 state
->eflags
= saved_state
->efl
;
167 state
->eip
= saved_state
->eip
;
168 state
->cs
= saved_state
->cs
;
169 state
->ds
= saved_state
->ds
;
170 state
->es
= saved_state
->es
;
171 state
->fs
= saved_state
->fs
;
172 state
->gs
= saved_state
->gs
;
178 x86_thread_state32_t
*state
181 x86_saved_state32_t
*saved_state
;
183 saved_state
= (x86_saved_state32_t
*)kdp
.saved_state
;
185 saved_state
->eax
= state
->eax
;
186 saved_state
->ebx
= state
->ebx
;
187 saved_state
->ecx
= state
->ecx
;
188 saved_state
->edx
= state
->edx
;
189 saved_state
->edi
= state
->edi
;
190 saved_state
->esi
= state
->esi
;
191 saved_state
->ebp
= state
->ebp
;
192 saved_state
->efl
= state
->eflags
;
194 saved_state
->frame
.eflags
&= ~( EFL_VM
| EFL_NT
| EFL_IOPL
| EFL_CLR
);
195 saved_state
->frame
.eflags
|= ( EFL_IF
| EFL_SET
);
197 saved_state
->eip
= state
->eip
;
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();
284 kprintf("kdp panic: %s\n", msg
);
285 __asm__
volatile("hlt");
290 kdp_machine_reboot(void)
292 printf("Attempting system restart...");
293 /* Call the platform specific restart*/
295 (*PE_halt_restart
)(kPERestartCPU
);
296 /* If we do reach this, give up */
319 kdp_us_spin(int usec
)
324 void print_saved_state(void *state
)
326 x86_saved_state32_t
*saved_state
;
330 kprintf("pc = 0x%x\n", saved_state
->eip
);
331 kprintf("cr2= 0x%x\n", saved_state
->cr2
);
332 kprintf("rp = TODO FIXME\n");
333 kprintf("sp = %p\n", saved_state
);
340 return; /* No op here. */
346 __asm__
volatile ("int $3"); /* Let the processor do the work */
350 typedef struct _cframe_t
{
351 struct _cframe_t
*prev
;
356 #include <i386/pmap.h>
357 extern pt_entry_t
*DMAP2
;
358 extern caddr_t DADDR2
;
361 kdp_print_phys(int src
)
366 *(int *) DMAP2
= 0x63 | (src
& 0xfffff000);
367 invlpg((u_int
) DADDR2
);
368 iptr
= (unsigned int *) DADDR2
;
369 for (i
= 0; i
< 100; i
++) {
370 kprintf("0x%x ", *iptr
++);
382 x86_saved_state32_t
*saved_state
,
383 kern_return_t result
,
387 unsigned int exception
, subcode
= 0, code
;
389 if (trapno
!= T_INT3
&& trapno
!= T_DEBUG
) {
390 kprintf("Debugger: Unexpected kernel trap number: "
391 "0x%x, EIP: 0x%x, CR2: 0x%x\n",
392 trapno
, saved_state
->eip
, saved_state
->cr2
);
398 kdp_callouts(KDP_EVENT_ENTER
);
400 if (saved_state
->efl
& EFL_TF
) {
401 enable_preemption_no_check();
407 exception
= EXC_ARITHMETIC
;
408 code
= EXC_I386_DIVERR
;
412 exception
= EXC_SOFTWARE
;
413 code
= EXC_I386_INTOFLT
;
416 case T_OUT_OF_BOUNDS
:
417 exception
= EXC_ARITHMETIC
;
418 code
= EXC_I386_BOUNDFLT
;
421 case T_INVALID_OPCODE
:
422 exception
= EXC_BAD_INSTRUCTION
;
423 code
= EXC_I386_INVOPFLT
;
426 case T_SEGMENT_NOT_PRESENT
:
427 exception
= EXC_BAD_INSTRUCTION
;
428 code
= EXC_I386_SEGNPFLT
;
429 subcode
= saved_state
->err
;
433 exception
= EXC_BAD_INSTRUCTION
;
434 code
= EXC_I386_STKFLT
;
435 subcode
= saved_state
->err
;
438 case T_GENERAL_PROTECTION
:
439 exception
= EXC_BAD_INSTRUCTION
;
440 code
= EXC_I386_GPFLT
;
441 subcode
= saved_state
->err
;
445 exception
= EXC_BAD_ACCESS
;
451 exception
= EXC_SOFTWARE
;
452 code
= EXC_I386_ALIGNFLT
;
457 exception
= EXC_BREAKPOINT
;
458 code
= EXC_I386_BPTFLT
;
462 exception
= EXC_BAD_INSTRUCTION
;
467 kdp_raise_exception(exception
, code
, subcode
, saved_state
);
468 /* If the instruction single step bit is set, disable kernel preemption
470 if (saved_state
->efl
& EFL_TF
) {
471 disable_preemption();
474 kdp_callouts(KDP_EVENT_EXIT
);
488 kdp_machine_get_breakinsn(
497 extern pmap_t kdp_pmap
;
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
)) {
545 /* Unaligned frame */
546 if (stackptr
& 0x0000003) {
550 if (stackptr
> stacklimit
) {
554 if (stackptr
<= prevsp
) {
558 if (kdp_machine_vm_read((mach_vm_address_t
)(stackptr
+ RETURN_OFFSET
), (caddr_t
) tracebuf
, sizeof(caddr_t
)) != sizeof(caddr_t
)) {
564 if (kdp_machine_vm_read((mach_vm_address_t
)stackptr
, (caddr_t
) &stackptr
, sizeof(caddr_t
)) != sizeof(caddr_t
)) {
572 return (uint32_t) (((char *) tracebuf
) - tracepos
);
575 #define RETURN_OFFSET64 8
576 /* Routine to encapsulate the 64-bit address read hack*/
578 machine_read64(addr64_t srcaddr
, caddr_t dstaddr
, uint32_t len
)
580 return (unsigned)kdp_machine_vm_read(srcaddr
, dstaddr
, len
);
584 machine_trace_thread64(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
)
586 uint64_t *tracebuf
= (uint64_t *)tracepos
;
588 addr64_t stackptr
= 0;
589 uint64_t stacklimit
= 0xfc000000;
591 addr64_t init_rip
= 0;
593 unsigned framesize
= 2 * sizeof(addr64_t
);
596 x86_saved_state64_t
*iss64
;
597 iss64
= USER_REGS64(thread
);
598 init_rip
= iss64
->isf
.rip
;
599 stackptr
= iss64
->rbp
;
600 stacklimit
= 0xffffffffffffffffULL
;
601 kdp_pmap
= thread
->task
->map
->pmap
;
604 *tracebuf
++ = init_rip
;
606 for (framecount
= 0; framecount
< nframes
; framecount
++) {
608 if ((uint32_t)(tracebound
- ((char *)tracebuf
)) < (4 * framesize
)) {
613 *tracebuf
++ = stackptr
;
615 if (!stackptr
|| (stackptr
== fence
)){
619 if (stackptr
& 0x0000003) {
622 if (stackptr
> stacklimit
) {
626 if (stackptr
<= prevsp
) {
630 if (machine_read64(stackptr
+ RETURN_OFFSET64
, (caddr_t
) tracebuf
, sizeof(addr64_t
)) != sizeof(addr64_t
)) {
636 if (machine_read64(stackptr
, (caddr_t
) &stackptr
, sizeof(addr64_t
)) != sizeof(addr64_t
)) {
644 return (uint32_t) (((char *) tracebuf
) - tracepos
);
647 static struct kdp_callout
{
648 struct kdp_callout
*callout_next
;
649 kdp_callout_fn_t callout_fn
;
651 } *kdp_callout_list
= NULL
;
655 * Called from kernel context to register a kdp event callout.
658 kdp_register_callout(
662 struct kdp_callout
*kcp
;
663 struct kdp_callout
*list_head
;
665 kcp
= kalloc(sizeof(*kcp
));
667 panic("kdp_register_callout() kalloc failed");
669 kcp
->callout_fn
= fn
;
670 kcp
->callout_arg
= arg
;
672 /* Lock-less list insertion using compare and exchange. */
674 list_head
= kdp_callout_list
;
675 kcp
->callout_next
= list_head
;
676 } while (!OSCompareAndSwapPtr(list_head
, kcp
, (void * volatile *)&kdp_callout_list
));
680 * Called at exception/panic time when extering or exiting kdp.
681 * We are single-threaded at this time and so we don't use locks.
684 kdp_callouts(kdp_event_t event
)
686 struct kdp_callout
*kcp
= kdp_callout_list
;
689 kcp
->callout_fn(kcp
->callout_arg
, event
);
690 kcp
= kcp
->callout_next
;
695 kdp_ml_enter_debugger(void)
697 __asm__
__volatile__("int3");