2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <mach/mach_types.h>
24 #include <mach/machine.h>
25 #include <mach/exception_types.h>
26 #include <kern/cpu_data.h>
27 #include <i386/trap.h>
29 #include <kdp/kdp_internal.h>
31 #define KDP_TEST_HARNESS 0
33 #define dprintf(x) printf x
38 extern void kdreboot(void);
40 void print_saved_state(void *);
43 boolean_t
kdp_call_kdb(void);
44 void kdp_getstate(i386_thread_state_t
*);
45 void kdp_setstate(i386_thread_state_t
*);
46 void kdp_print_phys(int);
47 void kdp_i386_backtrace(void *, int);
50 struct i386_saved_state
*,
58 unsigned short *remote_port
,
59 unsigned int exception
,
64 kdp_exception_t
*rq
= (kdp_exception_t
*)pkt
;
66 rq
->hdr
.request
= KDP_EXCEPTION
;
68 rq
->hdr
.seq
= kdp
.exception_seq
;
70 rq
->hdr
.len
= sizeof (*rq
);
73 rq
->exc_info
[0].cpu
= 0;
74 rq
->exc_info
[0].exception
= exception
;
75 rq
->exc_info
[0].code
= code
;
76 rq
->exc_info
[0].subcode
= subcode
;
78 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof (kdp_exc_info_t
);
80 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
82 kdp
.exception_ack_needed
= TRUE
;
84 *remote_port
= kdp
.exception_port
;
94 kdp_exception_ack_t
*rq
= (kdp_exception_ack_t
*)pkt
;
96 if (((unsigned int) len
) < sizeof (*rq
))
99 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
)
102 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
104 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
105 kdp
.exception_ack_needed
= FALSE
;
113 i386_thread_state_t
*state
116 static i386_thread_state_t null_state
;
117 struct i386_saved_state
*saved_state
;
119 saved_state
= (struct i386_saved_state
*)kdp
.saved_state
;
122 state
->eax
= saved_state
->eax
;
123 state
->ebx
= saved_state
->ebx
;
124 state
->ecx
= saved_state
->ecx
;
125 state
->edx
= saved_state
->edx
;
126 state
->edi
= saved_state
->edi
;
127 state
->esi
= saved_state
->esi
;
128 state
->ebp
= saved_state
->ebp
;
130 if ((saved_state
->cs
& 0x3) == 0){ /* Kernel State */
131 state
->esp
= (unsigned int) &saved_state
->uesp
;
132 state
->ss
= KERNEL_DS
;
134 state
->esp
= saved_state
->uesp
;
135 state
->ss
= saved_state
->ss
;
138 state
->eflags
= saved_state
->efl
;
139 state
->eip
= saved_state
->eip
;
140 state
->cs
= saved_state
->cs
;
141 state
->ds
= saved_state
->ds
;
142 state
->es
= saved_state
->es
;
143 state
->fs
= saved_state
->fs
;
144 state
->gs
= saved_state
->gs
;
150 i386_thread_state_t
*state
153 struct i386_saved_state
*saved_state
;
155 saved_state
= (struct i386_saved_state
*)kdp
.saved_state
;
157 saved_state
->eax
= state
->eax
;
158 saved_state
->ebx
= state
->ebx
;
159 saved_state
->ecx
= state
->ecx
;
160 saved_state
->edx
= state
->edx
;
161 saved_state
->edi
= state
->edi
;
162 saved_state
->esi
= state
->esi
;
163 saved_state
->ebp
= state
->ebp
;
164 saved_state
->efl
= state
->eflags
;
166 saved_state
->frame
.eflags
&= ~( EFL_VM
| EFL_NT
| EFL_IOPL
| EFL_CLR
);
167 saved_state
->frame
.eflags
|= ( EFL_IF
| EFL_SET
);
169 saved_state
->eip
= state
->eip
;
170 saved_state
->fs
= state
->fs
;
171 saved_state
->gs
= state
->gs
;
176 kdp_machine_read_regs(
177 __unused
unsigned int cpu
,
178 __unused
unsigned int flavor
,
183 static i386_thread_fpstate_t null_fpstate
;
187 case i386_THREAD_STATE
:
188 dprintf(("kdp_readregs THREAD_STATE\n"));
189 kdp_getstate((i386_thread_state_t
*)data
);
190 *size
= sizeof (i386_thread_state_t
);
191 return KDPERR_NO_ERROR
;
193 case i386_THREAD_FPSTATE
:
194 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
195 *(i386_thread_fpstate_t
*)data
= null_fpstate
;
196 *size
= sizeof (i386_thread_fpstate_t
);
197 return KDPERR_NO_ERROR
;
200 dprintf(("kdp_readregs bad flavor %d\n", flavor
));
202 return KDPERR_BADFLAVOR
;
207 kdp_machine_write_regs(
208 __unused
unsigned int cpu
,
216 case i386_THREAD_STATE
:
217 dprintf(("kdp_writeregs THREAD_STATE\n"));
218 kdp_setstate((i386_thread_state_t
*)data
);
219 return KDPERR_NO_ERROR
;
221 case i386_THREAD_FPSTATE
:
222 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
223 return KDPERR_NO_ERROR
;
226 dprintf(("kdp_writeregs bad flavor %d\n"));
227 return KDPERR_BADFLAVOR
;
234 kdp_machine_hostinfo(
235 kdp_hostinfo_t
*hostinfo
240 hostinfo
->cpus_mask
= 0;
242 for (i
= 0; i
< machine_info
.max_cpus
; i
++) {
243 if (cpu_data_ptr
[i
] == NULL
)
246 hostinfo
->cpus_mask
|= (1 << i
);
250 hostinfo
->cpu_type
= CPU_TYPE_I386
;
251 hostinfo
->cpu_subtype
= CPU_SUBTYPE_486
;
259 kprintf("kdp panic: %s\n", msg
);
260 __asm__
volatile("hlt");
289 kdp_us_spin(int usec
)
294 void print_saved_state(void *state
)
296 struct i386_saved_state
*saved_state
;
300 kprintf("pc = 0x%x\n", saved_state
->eip
);
301 kprintf("cr3= 0x%x\n", saved_state
->cr2
);
302 kprintf("rp = TODO FIXME\n");
303 kprintf("sp = 0x%x\n", saved_state
->esp
);
310 return; /* No op here. */
316 __asm__
volatile ("int $3"); /* Let the processor do the work */
320 typedef struct _cframe_t
{
321 struct _cframe_t
*prev
;
326 #include <i386/pmap.h>
327 extern pt_entry_t
*DMAP2
;
328 extern caddr_t DADDR2
;
331 kdp_print_phys(int src
)
336 *(int *) DMAP2
= 0x63 | (src
& 0xfffff000);
337 invlpg((u_int
) DADDR2
);
338 iptr
= (unsigned int *) DADDR2
;
339 for (i
= 0; i
< 100; i
++) {
340 kprintf("0x%x ", *iptr
++);
350 #define MAX_FRAME_DELTA 65536
353 kdp_i386_backtrace(void *_frame
, int nframes
)
355 cframe_t
*frame
= (cframe_t
*)_frame
;
358 for (i
=0; i
<nframes
; i
++) {
359 if ((vm_offset_t
)frame
< VM_MIN_KERNEL_ADDRESS
||
360 (vm_offset_t
)frame
> VM_MAX_KERNEL_ADDRESS
) {
363 kprintf("frame 0x%x called by 0x%x ",
364 frame
, frame
->caller
);
365 kprintf("args 0x%x 0x%x 0x%x 0x%x\n",
366 frame
->args
[0], frame
->args
[1],
367 frame
->args
[2], frame
->args
[3]);
368 if ((frame
->prev
< frame
) || /* wrong direction */
369 ((frame
->prev
- frame
) > MAX_FRAME_DELTA
)) {
376 kprintf("invalid frame pointer 0x%x\n",frame
);
382 struct i386_saved_state
*saved_state
,
383 kern_return_t result
,
387 unsigned int exception
, subcode
= 0, code
;
391 if (trapno
!= T_INT3
&& trapno
!= T_DEBUG
)
392 kprintf("unexpected kernel trap 0x%x eip 0x%x cr2 0x%x \n",
393 trapno
, saved_state
->eip
, saved_state
->esp
);
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_i386_backtrace((void *) saved_state
->ebp
, 10);
460 kdp_raise_exception(exception
, code
, subcode
, saved_state
);
473 kdp_ml_get_breakinsn(void)