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 kprintf("Attempting system restart...");
294 /* Call the platform specific restart*/
296 (*PE_halt_restart
)(kPERestartCPU
);
297 /* If we do reach this, give up */
320 kdp_us_spin(int usec
)
325 void print_saved_state(void *state
)
327 x86_saved_state32_t
*saved_state
;
331 kprintf("pc = 0x%x\n", saved_state
->eip
);
332 kprintf("cr2= 0x%x\n", saved_state
->cr2
);
333 kprintf("rp = TODO FIXME\n");
334 kprintf("sp = %p\n", saved_state
);
341 return; /* No op here. */
347 __asm__
volatile ("int $3"); /* Let the processor do the work */
351 typedef struct _cframe_t
{
352 struct _cframe_t
*prev
;
357 #include <i386/pmap.h>
358 extern pt_entry_t
*DMAP2
;
359 extern caddr_t DADDR2
;
362 kdp_print_phys(int src
)
367 *(int *) DMAP2
= 0x63 | (src
& 0xfffff000);
368 invlpg((u_int
) DADDR2
);
369 iptr
= (unsigned int *) DADDR2
;
370 for (i
= 0; i
< 100; i
++) {
371 kprintf("0x%x ", *iptr
++);
383 x86_saved_state32_t
*saved_state
,
384 kern_return_t result
,
388 unsigned int exception
, subcode
= 0, code
;
390 if (trapno
!= T_INT3
&& trapno
!= T_DEBUG
) {
391 kprintf("Debugger: Unexpected kernel trap number: "
392 "0x%x, EIP: 0x%x, CR2: 0x%x\n",
393 trapno
, saved_state
->eip
, saved_state
->cr2
);
399 kdp_callouts(KDP_EVENT_ENTER
);
401 if (saved_state
->efl
& EFL_TF
) {
402 enable_preemption_no_check();
408 exception
= EXC_ARITHMETIC
;
409 code
= EXC_I386_DIVERR
;
413 exception
= EXC_SOFTWARE
;
414 code
= EXC_I386_INTOFLT
;
417 case T_OUT_OF_BOUNDS
:
418 exception
= EXC_ARITHMETIC
;
419 code
= EXC_I386_BOUNDFLT
;
422 case T_INVALID_OPCODE
:
423 exception
= EXC_BAD_INSTRUCTION
;
424 code
= EXC_I386_INVOPFLT
;
427 case T_SEGMENT_NOT_PRESENT
:
428 exception
= EXC_BAD_INSTRUCTION
;
429 code
= EXC_I386_SEGNPFLT
;
430 subcode
= saved_state
->err
;
434 exception
= EXC_BAD_INSTRUCTION
;
435 code
= EXC_I386_STKFLT
;
436 subcode
= saved_state
->err
;
439 case T_GENERAL_PROTECTION
:
440 exception
= EXC_BAD_INSTRUCTION
;
441 code
= EXC_I386_GPFLT
;
442 subcode
= saved_state
->err
;
446 exception
= EXC_BAD_ACCESS
;
452 exception
= EXC_SOFTWARE
;
453 code
= EXC_I386_ALIGNFLT
;
458 exception
= EXC_BREAKPOINT
;
459 code
= EXC_I386_BPTFLT
;
463 exception
= EXC_BAD_INSTRUCTION
;
468 kdp_raise_exception(exception
, code
, subcode
, saved_state
);
469 /* If the instruction single step bit is set, disable kernel preemption
471 if (saved_state
->efl
& EFL_TF
) {
472 disable_preemption();
475 kdp_callouts(KDP_EVENT_EXIT
);
489 kdp_machine_get_breakinsn(
498 extern pmap_t kdp_pmap
;
500 #define RETURN_OFFSET 4
502 machine_trace_thread(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
)
504 uint32_t *tracebuf
= (uint32_t *)tracepos
;
506 uint32_t stackptr
= 0;
507 uint32_t stacklimit
= 0xfc000000;
509 uint32_t init_eip
= 0;
511 uint32_t framesize
= 2 * sizeof(vm_offset_t
);
514 x86_saved_state32_t
*iss32
;
516 iss32
= USER_REGS32(thread
);
518 init_eip
= iss32
->eip
;
519 stackptr
= iss32
->ebp
;
521 /* This bound isn't useful, but it doesn't hinder us*/
522 stacklimit
= 0xffffffff;
523 kdp_pmap
= thread
->task
->map
->pmap
;
526 /*Examine the i386_saved_state at the base of the kernel stack*/
527 stackptr
= STACK_IKS(thread
->kernel_stack
)->k_ebp
;
528 init_eip
= STACK_IKS(thread
->kernel_stack
)->k_eip
;
531 *tracebuf
++ = init_eip
;
533 for (framecount
= 0; framecount
< nframes
; framecount
++) {
535 if ((uint32_t)(tracebound
- ((char *)tracebuf
)) < (4 * framesize
)) {
540 *tracebuf
++ = stackptr
;
541 /* Invalid frame, or hit fence */
542 if (!stackptr
|| (stackptr
== fence
)) {
546 /* Unaligned frame */
547 if (stackptr
& 0x0000003) {
551 if (stackptr
> stacklimit
) {
555 if (stackptr
<= prevsp
) {
559 if (kdp_machine_vm_read((mach_vm_address_t
)(stackptr
+ RETURN_OFFSET
), (caddr_t
) tracebuf
, sizeof(caddr_t
)) != sizeof(caddr_t
)) {
565 if (kdp_machine_vm_read((mach_vm_address_t
)stackptr
, (caddr_t
) &stackptr
, sizeof(caddr_t
)) != sizeof(caddr_t
)) {
573 return (uint32_t) (((char *) tracebuf
) - tracepos
);
576 #define RETURN_OFFSET64 8
577 /* Routine to encapsulate the 64-bit address read hack*/
579 machine_read64(addr64_t srcaddr
, caddr_t dstaddr
, uint32_t len
)
581 return (unsigned)kdp_machine_vm_read(srcaddr
, dstaddr
, len
);
585 machine_trace_thread64(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
)
587 uint64_t *tracebuf
= (uint64_t *)tracepos
;
589 addr64_t stackptr
= 0;
590 uint64_t stacklimit
= 0xfc000000;
592 addr64_t init_rip
= 0;
594 unsigned framesize
= 2 * sizeof(addr64_t
);
597 x86_saved_state64_t
*iss64
;
598 iss64
= USER_REGS64(thread
);
599 init_rip
= iss64
->isf
.rip
;
600 stackptr
= iss64
->rbp
;
601 stacklimit
= 0xffffffffffffffffULL
;
602 kdp_pmap
= thread
->task
->map
->pmap
;
605 *tracebuf
++ = init_rip
;
607 for (framecount
= 0; framecount
< nframes
; framecount
++) {
609 if ((uint32_t)(tracebound
- ((char *)tracebuf
)) < (4 * framesize
)) {
614 *tracebuf
++ = stackptr
;
616 if (!stackptr
|| (stackptr
== fence
)){
620 if (stackptr
& 0x0000003) {
623 if (stackptr
> stacklimit
) {
627 if (stackptr
<= prevsp
) {
631 if (machine_read64(stackptr
+ RETURN_OFFSET64
, (caddr_t
) tracebuf
, sizeof(addr64_t
)) != sizeof(addr64_t
)) {
637 if (machine_read64(stackptr
, (caddr_t
) &stackptr
, sizeof(addr64_t
)) != sizeof(addr64_t
)) {
645 return (uint32_t) (((char *) tracebuf
) - tracepos
);
648 static struct kdp_callout
{
649 struct kdp_callout
*callout_next
;
650 kdp_callout_fn_t callout_fn
;
652 } *kdp_callout_list
= NULL
;
656 * Called from kernel context to register a kdp event callout.
659 kdp_register_callout(
663 struct kdp_callout
*kcp
;
664 struct kdp_callout
*list_head
;
666 kcp
= kalloc(sizeof(*kcp
));
668 panic("kdp_register_callout() kalloc failed");
670 kcp
->callout_fn
= fn
;
671 kcp
->callout_arg
= arg
;
673 /* Lock-less list insertion using compare and exchange. */
675 list_head
= kdp_callout_list
;
676 kcp
->callout_next
= list_head
;
677 } while (!OSCompareAndSwapPtr(list_head
, kcp
, (void * volatile *)&kdp_callout_list
));
681 * Called at exception/panic time when extering or exiting kdp.
682 * We are single-threaded at this time and so we don't use locks.
685 kdp_callouts(kdp_event_t event
)
687 struct kdp_callout
*kcp
= kdp_callout_list
;
690 kcp
->callout_fn(kcp
->callout_arg
, event
);
691 kcp
= kcp
->callout_next
;
696 kdp_ml_enter_debugger(void)
698 __asm__
__volatile__("int3");