2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <mach/mach_types.h>
25 #include <mach/machine.h>
26 #include <mach/exception_types.h>
27 #include <kern/cpu_data.h>
28 #include <i386/trap.h>
30 #include <kdp/kdp_internal.h>
32 #define KDP_TEST_HARNESS 0
34 #define dprintf(x) printf x
39 extern void kdreboot(void);
41 void print_saved_state(void *);
44 boolean_t
kdp_call_kdb(void);
45 void kdp_getstate(i386_thread_state_t
*);
46 void kdp_setstate(i386_thread_state_t
*);
47 void kdp_print_phys(int);
48 void kdp_i386_backtrace(void *, int);
51 struct i386_saved_state
*,
59 unsigned short *remote_port
,
60 unsigned int exception
,
65 kdp_exception_t
*rq
= (kdp_exception_t
*)pkt
;
67 rq
->hdr
.request
= KDP_EXCEPTION
;
69 rq
->hdr
.seq
= kdp
.exception_seq
;
71 rq
->hdr
.len
= sizeof (*rq
);
74 rq
->exc_info
[0].cpu
= 0;
75 rq
->exc_info
[0].exception
= exception
;
76 rq
->exc_info
[0].code
= code
;
77 rq
->exc_info
[0].subcode
= subcode
;
79 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof (kdp_exc_info_t
);
81 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
83 kdp
.exception_ack_needed
= TRUE
;
85 *remote_port
= kdp
.exception_port
;
95 kdp_exception_ack_t
*rq
= (kdp_exception_ack_t
*)pkt
;
97 if (((unsigned int) len
) < sizeof (*rq
))
100 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
)
103 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
105 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
106 kdp
.exception_ack_needed
= FALSE
;
114 i386_thread_state_t
*state
117 static i386_thread_state_t null_state
;
118 struct i386_saved_state
*saved_state
;
120 saved_state
= (struct i386_saved_state
*)kdp
.saved_state
;
123 state
->eax
= saved_state
->eax
;
124 state
->ebx
= saved_state
->ebx
;
125 state
->ecx
= saved_state
->ecx
;
126 state
->edx
= saved_state
->edx
;
127 state
->edi
= saved_state
->edi
;
128 state
->esi
= saved_state
->esi
;
129 state
->ebp
= saved_state
->ebp
;
131 if ((saved_state
->cs
& 0x3) == 0){ /* Kernel State */
132 state
->esp
= (unsigned int) &saved_state
->uesp
;
133 state
->ss
= KERNEL_DS
;
135 state
->esp
= saved_state
->uesp
;
136 state
->ss
= saved_state
->ss
;
139 state
->eflags
= saved_state
->efl
;
140 state
->eip
= saved_state
->eip
;
141 state
->cs
= saved_state
->cs
;
142 state
->ds
= saved_state
->ds
;
143 state
->es
= saved_state
->es
;
144 state
->fs
= saved_state
->fs
;
145 state
->gs
= saved_state
->gs
;
151 i386_thread_state_t
*state
154 struct i386_saved_state
*saved_state
;
156 saved_state
= (struct i386_saved_state
*)kdp
.saved_state
;
158 saved_state
->eax
= state
->eax
;
159 saved_state
->ebx
= state
->ebx
;
160 saved_state
->ecx
= state
->ecx
;
161 saved_state
->edx
= state
->edx
;
162 saved_state
->edi
= state
->edi
;
163 saved_state
->esi
= state
->esi
;
164 saved_state
->ebp
= state
->ebp
;
165 saved_state
->efl
= state
->eflags
;
167 saved_state
->frame
.eflags
&= ~( EFL_VM
| EFL_NT
| EFL_IOPL
| EFL_CLR
);
168 saved_state
->frame
.eflags
|= ( EFL_IF
| EFL_SET
);
170 saved_state
->eip
= state
->eip
;
171 saved_state
->fs
= state
->fs
;
172 saved_state
->gs
= state
->gs
;
177 kdp_machine_read_regs(
178 __unused
unsigned int cpu
,
179 __unused
unsigned int flavor
,
184 static i386_thread_fpstate_t null_fpstate
;
188 case i386_THREAD_STATE
:
189 dprintf(("kdp_readregs THREAD_STATE\n"));
190 kdp_getstate((i386_thread_state_t
*)data
);
191 *size
= sizeof (i386_thread_state_t
);
192 return KDPERR_NO_ERROR
;
194 case i386_THREAD_FPSTATE
:
195 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
196 *(i386_thread_fpstate_t
*)data
= null_fpstate
;
197 *size
= sizeof (i386_thread_fpstate_t
);
198 return KDPERR_NO_ERROR
;
201 dprintf(("kdp_readregs bad flavor %d\n", flavor
));
203 return KDPERR_BADFLAVOR
;
208 kdp_machine_write_regs(
209 __unused
unsigned int cpu
,
217 case i386_THREAD_STATE
:
218 dprintf(("kdp_writeregs THREAD_STATE\n"));
219 kdp_setstate((i386_thread_state_t
*)data
);
220 return KDPERR_NO_ERROR
;
222 case i386_THREAD_FPSTATE
:
223 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
224 return KDPERR_NO_ERROR
;
227 dprintf(("kdp_writeregs bad flavor %d\n"));
228 return KDPERR_BADFLAVOR
;
235 kdp_machine_hostinfo(
236 kdp_hostinfo_t
*hostinfo
241 hostinfo
->cpus_mask
= 0;
243 for (i
= 0; i
< machine_info
.max_cpus
; i
++) {
244 if (cpu_data_ptr
[i
] == NULL
)
247 hostinfo
->cpus_mask
|= (1 << i
);
251 hostinfo
->cpu_type
= CPU_TYPE_I386
;
252 hostinfo
->cpu_subtype
= CPU_SUBTYPE_486
;
260 kprintf("kdp panic: %s\n", msg
);
261 __asm__
volatile("hlt");
290 kdp_us_spin(int usec
)
295 void print_saved_state(void *state
)
297 struct i386_saved_state
*saved_state
;
301 kprintf("pc = 0x%x\n", saved_state
->eip
);
302 kprintf("cr3= 0x%x\n", saved_state
->cr2
);
303 kprintf("rp = TODO FIXME\n");
304 kprintf("sp = 0x%x\n", saved_state
->esp
);
311 return; /* No op here. */
317 __asm__
volatile ("int $3"); /* Let the processor do the work */
321 typedef struct _cframe_t
{
322 struct _cframe_t
*prev
;
327 #include <i386/pmap.h>
328 extern pt_entry_t
*DMAP2
;
329 extern caddr_t DADDR2
;
332 kdp_print_phys(int src
)
337 *(int *) DMAP2
= 0x63 | (src
& 0xfffff000);
338 invlpg((u_int
) DADDR2
);
339 iptr
= (unsigned int *) DADDR2
;
340 for (i
= 0; i
< 100; i
++) {
341 kprintf("0x%x ", *iptr
++);
351 #define MAX_FRAME_DELTA 65536
354 kdp_i386_backtrace(void *_frame
, int nframes
)
356 cframe_t
*frame
= (cframe_t
*)_frame
;
359 for (i
=0; i
<nframes
; i
++) {
360 if ((vm_offset_t
)frame
< VM_MIN_KERNEL_ADDRESS
||
361 (vm_offset_t
)frame
> VM_MAX_KERNEL_ADDRESS
) {
364 kprintf("frame 0x%x called by 0x%x ",
365 frame
, frame
->caller
);
366 kprintf("args 0x%x 0x%x 0x%x 0x%x\n",
367 frame
->args
[0], frame
->args
[1],
368 frame
->args
[2], frame
->args
[3]);
369 if ((frame
->prev
< frame
) || /* wrong direction */
370 ((frame
->prev
- frame
) > MAX_FRAME_DELTA
)) {
377 kprintf("invalid frame pointer 0x%x\n",frame
);
383 struct i386_saved_state
*saved_state
,
384 kern_return_t result
,
388 unsigned int exception
, subcode
= 0, code
;
392 if (trapno
!= T_INT3
&& trapno
!= T_DEBUG
)
393 kprintf("unexpected kernel trap 0x%x eip 0x%x cr2 0x%x \n",
394 trapno
, saved_state
->eip
, saved_state
->esp
);
399 exception
= EXC_ARITHMETIC
;
400 code
= EXC_I386_DIVERR
;
404 exception
= EXC_SOFTWARE
;
405 code
= EXC_I386_INTOFLT
;
408 case T_OUT_OF_BOUNDS
:
409 exception
= EXC_ARITHMETIC
;
410 code
= EXC_I386_BOUNDFLT
;
413 case T_INVALID_OPCODE
:
414 exception
= EXC_BAD_INSTRUCTION
;
415 code
= EXC_I386_INVOPFLT
;
418 case T_SEGMENT_NOT_PRESENT
:
419 exception
= EXC_BAD_INSTRUCTION
;
420 code
= EXC_I386_SEGNPFLT
;
421 subcode
= saved_state
->err
;
425 exception
= EXC_BAD_INSTRUCTION
;
426 code
= EXC_I386_STKFLT
;
427 subcode
= saved_state
->err
;
430 case T_GENERAL_PROTECTION
:
431 exception
= EXC_BAD_INSTRUCTION
;
432 code
= EXC_I386_GPFLT
;
433 subcode
= saved_state
->err
;
437 exception
= EXC_BAD_ACCESS
;
443 exception
= EXC_SOFTWARE
;
444 code
= EXC_I386_ALIGNFLT
;
449 exception
= EXC_BREAKPOINT
;
450 code
= EXC_I386_BPTFLT
;
454 exception
= EXC_BAD_INSTRUCTION
;
459 kdp_i386_backtrace((void *) saved_state
->ebp
, 10);
461 kdp_raise_exception(exception
, code
, subcode
, saved_state
);
474 kdp_ml_get_breakinsn(void)