2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 #include <mach/mach_types.h>
33 #include <mach/machine.h>
34 #include <mach/exception_types.h>
35 #include <kern/cpu_data.h>
36 #include <i386/trap.h>
38 #include <kdp/kdp_internal.h>
39 #include <mach-o/loader.h>
40 #include <mach-o/nlist.h>
41 #include <IOKit/IOPlatformExpert.h> /* for PE_halt_restart */
42 #include <kern/machine.h> /* for halt_all_cpus */
44 #include <kern/thread.h>
45 #include <i386/thread.h>
46 #include <vm/vm_map.h>
47 #include <i386/pmap.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
, uint32_t tracepos
, uint32_t tracebound
, int nframes
, boolean_t user_p
);
71 machine_trace_thread64(thread_t thread
, uint32_t tracepos
, uint32_t tracebound
, int nframes
, boolean_t user_p
);
73 extern unsigned kdp_vm_read(caddr_t src
, caddr_t dst
, unsigned len
);
79 unsigned short *remote_port
,
80 unsigned int exception
,
85 kdp_exception_t
*rq
= (kdp_exception_t
*)pkt
;
87 rq
->hdr
.request
= KDP_EXCEPTION
;
89 rq
->hdr
.seq
= kdp
.exception_seq
;
91 rq
->hdr
.len
= sizeof (*rq
);
94 rq
->exc_info
[0].cpu
= 0;
95 rq
->exc_info
[0].exception
= exception
;
96 rq
->exc_info
[0].code
= code
;
97 rq
->exc_info
[0].subcode
= subcode
;
99 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof (kdp_exc_info_t
);
101 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
103 kdp
.exception_ack_needed
= TRUE
;
105 *remote_port
= kdp
.exception_port
;
115 kdp_exception_ack_t
*rq
= (kdp_exception_ack_t
*)pkt
;
117 if (((unsigned int) len
) < sizeof (*rq
))
120 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
)
123 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
125 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
126 kdp
.exception_ack_needed
= FALSE
;
134 x86_thread_state32_t
*state
137 static x86_thread_state32_t null_state
;
138 x86_saved_state32_t
*saved_state
;
140 saved_state
= (x86_saved_state32_t
*)kdp
.saved_state
;
143 state
->eax
= saved_state
->eax
;
144 state
->ebx
= saved_state
->ebx
;
145 state
->ecx
= saved_state
->ecx
;
146 state
->edx
= saved_state
->edx
;
147 state
->edi
= saved_state
->edi
;
148 state
->esi
= saved_state
->esi
;
149 state
->ebp
= saved_state
->ebp
;
151 if ((saved_state
->cs
& 0x3) == 0){ /* Kernel State */
152 state
->esp
= (unsigned int) &saved_state
->uesp
;
153 state
->ss
= KERNEL_DS
;
155 state
->esp
= saved_state
->uesp
;
156 state
->ss
= saved_state
->ss
;
159 state
->eflags
= saved_state
->efl
;
160 state
->eip
= saved_state
->eip
;
161 state
->cs
= saved_state
->cs
;
162 state
->ds
= saved_state
->ds
;
163 state
->es
= saved_state
->es
;
164 state
->fs
= saved_state
->fs
;
165 state
->gs
= saved_state
->gs
;
171 x86_thread_state32_t
*state
174 x86_saved_state32_t
*saved_state
;
176 saved_state
= (x86_saved_state32_t
*)kdp
.saved_state
;
178 saved_state
->eax
= state
->eax
;
179 saved_state
->ebx
= state
->ebx
;
180 saved_state
->ecx
= state
->ecx
;
181 saved_state
->edx
= state
->edx
;
182 saved_state
->edi
= state
->edi
;
183 saved_state
->esi
= state
->esi
;
184 saved_state
->ebp
= state
->ebp
;
185 saved_state
->efl
= state
->eflags
;
187 saved_state
->frame
.eflags
&= ~( EFL_VM
| EFL_NT
| EFL_IOPL
| EFL_CLR
);
188 saved_state
->frame
.eflags
|= ( EFL_IF
| EFL_SET
);
190 saved_state
->eip
= state
->eip
;
191 saved_state
->fs
= state
->fs
;
192 saved_state
->gs
= state
->gs
;
197 kdp_machine_read_regs(
198 __unused
unsigned int cpu
,
199 __unused
unsigned int flavor
,
204 static struct i386_float_state null_fpstate
;
208 case OLD_i386_THREAD_STATE
:
209 case x86_THREAD_STATE32
:
210 dprintf(("kdp_readregs THREAD_STATE\n"));
211 kdp_getstate((x86_thread_state32_t
*)data
);
212 *size
= sizeof (x86_thread_state32_t
);
213 return KDPERR_NO_ERROR
;
215 case x86_FLOAT_STATE32
:
216 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
217 *(x86_float_state32_t
*)data
= null_fpstate
;
218 *size
= sizeof (x86_float_state32_t
);
219 return KDPERR_NO_ERROR
;
222 dprintf(("kdp_readregs bad flavor %d\n", flavor
));
224 return KDPERR_BADFLAVOR
;
229 kdp_machine_write_regs(
230 __unused
unsigned int cpu
,
238 case OLD_i386_THREAD_STATE
:
239 case x86_THREAD_STATE32
:
240 dprintf(("kdp_writeregs THREAD_STATE\n"));
241 kdp_setstate((x86_thread_state32_t
*)data
);
242 return KDPERR_NO_ERROR
;
244 case x86_FLOAT_STATE32
:
245 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
246 return KDPERR_NO_ERROR
;
249 dprintf(("kdp_writeregs bad flavor %d\n"));
250 return KDPERR_BADFLAVOR
;
257 kdp_machine_hostinfo(
258 kdp_hostinfo_t
*hostinfo
263 hostinfo
->cpus_mask
= 0;
265 for (i
= 0; i
< machine_info
.max_cpus
; i
++) {
266 if (cpu_data_ptr
[i
] == NULL
)
269 hostinfo
->cpus_mask
|= (1 << i
);
272 hostinfo
->cpu_type
= cpuid_cputype();
273 hostinfo
->cpu_subtype
= cpuid_cpusubtype();
281 kprintf("kdp panic: %s\n", msg
);
282 __asm__
volatile("hlt");
289 printf("Attempting system restart...");
290 /* Call the platform specific restart*/
292 (*PE_halt_restart
)(kPERestartCPU
);
293 /* If we do reach this, give up */
316 kdp_us_spin(int usec
)
321 void print_saved_state(void *state
)
323 x86_saved_state32_t
*saved_state
;
327 kprintf("pc = 0x%x\n", saved_state
->eip
);
328 kprintf("cr2= 0x%x\n", saved_state
->cr2
);
329 kprintf("rp = TODO FIXME\n");
330 kprintf("sp = 0x%x\n", saved_state
);
337 return; /* No op here. */
343 __asm__
volatile ("int $3"); /* Let the processor do the work */
347 typedef struct _cframe_t
{
348 struct _cframe_t
*prev
;
353 #include <i386/pmap.h>
354 extern pt_entry_t
*DMAP2
;
355 extern caddr_t DADDR2
;
358 kdp_print_phys(int src
)
363 *(int *) DMAP2
= 0x63 | (src
& 0xfffff000);
364 invlpg((u_int
) DADDR2
);
365 iptr
= (unsigned int *) DADDR2
;
366 for (i
= 0; i
< 100; i
++) {
367 kprintf("0x%x ", *iptr
++);
379 x86_saved_state32_t
*saved_state
,
380 kern_return_t result
,
384 unsigned int exception
, subcode
= 0, code
;
386 if (trapno
!= T_INT3
&& trapno
!= T_DEBUG
) {
387 kprintf("unexpected kernel trap 0x%x eip 0x%x cr2 0x%x \n",
388 trapno
, saved_state
->eip
, saved_state
->cr2
);
398 exception
= EXC_ARITHMETIC
;
399 code
= EXC_I386_DIVERR
;
403 exception
= EXC_SOFTWARE
;
404 code
= EXC_I386_INTOFLT
;
407 case T_OUT_OF_BOUNDS
:
408 exception
= EXC_ARITHMETIC
;
409 code
= EXC_I386_BOUNDFLT
;
412 case T_INVALID_OPCODE
:
413 exception
= EXC_BAD_INSTRUCTION
;
414 code
= EXC_I386_INVOPFLT
;
417 case T_SEGMENT_NOT_PRESENT
:
418 exception
= EXC_BAD_INSTRUCTION
;
419 code
= EXC_I386_SEGNPFLT
;
420 subcode
= saved_state
->err
;
424 exception
= EXC_BAD_INSTRUCTION
;
425 code
= EXC_I386_STKFLT
;
426 subcode
= saved_state
->err
;
429 case T_GENERAL_PROTECTION
:
430 exception
= EXC_BAD_INSTRUCTION
;
431 code
= EXC_I386_GPFLT
;
432 subcode
= saved_state
->err
;
436 exception
= EXC_BAD_ACCESS
;
442 exception
= EXC_SOFTWARE
;
443 code
= EXC_I386_ALIGNFLT
;
448 exception
= EXC_BREAKPOINT
;
449 code
= EXC_I386_BPTFLT
;
453 exception
= EXC_BAD_INSTRUCTION
;
458 kdp_raise_exception(exception
, code
, subcode
, saved_state
);
473 kdp_ml_get_breakinsn(void)
477 extern pmap_t kdp_pmap
;
479 #define RETURN_OFFSET 4
481 machine_trace_thread(thread_t thread
, uint32_t tracepos
, uint32_t tracebound
, int nframes
, boolean_t user_p
)
483 uint32_t *tracebuf
= (uint32_t *)tracepos
;
485 uint32_t stackptr
= 0;
486 uint32_t stacklimit
= 0xfc000000;
488 uint32_t init_eip
= 0;
490 uint32_t framesize
= 2 * sizeof(vm_offset_t
);
493 x86_saved_state32_t
*iss32
;
495 iss32
= USER_REGS32(thread
);
497 init_eip
= iss32
->eip
;
498 stackptr
= iss32
->ebp
;
500 /* This bound isn't useful, but it doesn't hinder us*/
501 stacklimit
= 0xffffffff;
502 kdp_pmap
= thread
->task
->map
->pmap
;
505 /*Examine the i386_saved_state at the base of the kernel stack*/
506 stackptr
= STACK_IKS(thread
->kernel_stack
)->k_ebp
;
507 init_eip
= STACK_IKS(thread
->kernel_stack
)->k_eip
;
510 *tracebuf
++ = init_eip
;
512 for (framecount
= 0; framecount
< nframes
; framecount
++) {
514 if ((tracebound
- ((uint32_t) tracebuf
)) < (4 * framesize
)) {
519 *tracebuf
++ = stackptr
;
520 /* Invalid frame, or hit fence */
521 if (!stackptr
|| (stackptr
== fence
)) {
524 /* Stack grows downward */
525 if (stackptr
< prevsp
) {
528 /* Unaligned frame */
529 if (stackptr
& 0x0000003) {
532 if (stackptr
> stacklimit
) {
536 if (kdp_vm_read((caddr_t
) (stackptr
+ RETURN_OFFSET
), (caddr_t
) tracebuf
, sizeof(caddr_t
)) != sizeof(caddr_t
)) {
542 if (kdp_vm_read((caddr_t
) stackptr
, (caddr_t
) &stackptr
, sizeof(caddr_t
)) != sizeof(caddr_t
)) {
550 return ((uint32_t) tracebuf
- tracepos
);
553 /* This is a stub until the x86 64-bit model becomes clear */
555 machine_trace_thread64(__unused thread_t thread
, __unused
uint32_t tracepos
, __unused
uint32_t tracebound
, __unused
int nframes
, __unused boolean_t user_p
) {