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@
31 #include <mach/mach_types.h>
32 #include <mach/machine.h>
33 #include <mach/exception_types.h>
34 #include <kern/cpu_data.h>
35 #include <i386/trap.h>
37 #include <kdp/kdp_internal.h>
39 #define KDP_TEST_HARNESS 0
41 #define dprintf(x) printf x
46 extern void kdreboot(void);
48 void print_saved_state(void *);
51 boolean_t
kdp_call_kdb(void);
52 void kdp_getstate(i386_thread_state_t
*);
53 void kdp_setstate(i386_thread_state_t
*);
54 void kdp_print_phys(int);
55 void kdp_i386_backtrace(void *, int);
58 struct i386_saved_state
*,
66 unsigned short *remote_port
,
67 unsigned int exception
,
72 kdp_exception_t
*rq
= (kdp_exception_t
*)pkt
;
74 rq
->hdr
.request
= KDP_EXCEPTION
;
76 rq
->hdr
.seq
= kdp
.exception_seq
;
78 rq
->hdr
.len
= sizeof (*rq
);
81 rq
->exc_info
[0].cpu
= 0;
82 rq
->exc_info
[0].exception
= exception
;
83 rq
->exc_info
[0].code
= code
;
84 rq
->exc_info
[0].subcode
= subcode
;
86 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof (kdp_exc_info_t
);
88 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
90 kdp
.exception_ack_needed
= TRUE
;
92 *remote_port
= kdp
.exception_port
;
102 kdp_exception_ack_t
*rq
= (kdp_exception_ack_t
*)pkt
;
104 if (((unsigned int) len
) < sizeof (*rq
))
107 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
)
110 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
112 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
113 kdp
.exception_ack_needed
= FALSE
;
121 i386_thread_state_t
*state
124 static i386_thread_state_t null_state
;
125 struct i386_saved_state
*saved_state
;
127 saved_state
= (struct i386_saved_state
*)kdp
.saved_state
;
130 state
->eax
= saved_state
->eax
;
131 state
->ebx
= saved_state
->ebx
;
132 state
->ecx
= saved_state
->ecx
;
133 state
->edx
= saved_state
->edx
;
134 state
->edi
= saved_state
->edi
;
135 state
->esi
= saved_state
->esi
;
136 state
->ebp
= saved_state
->ebp
;
138 if ((saved_state
->cs
& 0x3) == 0){ /* Kernel State */
139 state
->esp
= (unsigned int) &saved_state
->uesp
;
140 state
->ss
= KERNEL_DS
;
142 state
->esp
= saved_state
->uesp
;
143 state
->ss
= saved_state
->ss
;
146 state
->eflags
= saved_state
->efl
;
147 state
->eip
= saved_state
->eip
;
148 state
->cs
= saved_state
->cs
;
149 state
->ds
= saved_state
->ds
;
150 state
->es
= saved_state
->es
;
151 state
->fs
= saved_state
->fs
;
152 state
->gs
= saved_state
->gs
;
158 i386_thread_state_t
*state
161 struct i386_saved_state
*saved_state
;
163 saved_state
= (struct i386_saved_state
*)kdp
.saved_state
;
165 saved_state
->eax
= state
->eax
;
166 saved_state
->ebx
= state
->ebx
;
167 saved_state
->ecx
= state
->ecx
;
168 saved_state
->edx
= state
->edx
;
169 saved_state
->edi
= state
->edi
;
170 saved_state
->esi
= state
->esi
;
171 saved_state
->ebp
= state
->ebp
;
172 saved_state
->efl
= state
->eflags
;
174 saved_state
->frame
.eflags
&= ~( EFL_VM
| EFL_NT
| EFL_IOPL
| EFL_CLR
);
175 saved_state
->frame
.eflags
|= ( EFL_IF
| EFL_SET
);
177 saved_state
->eip
= state
->eip
;
178 saved_state
->fs
= state
->fs
;
179 saved_state
->gs
= state
->gs
;
184 kdp_machine_read_regs(
185 __unused
unsigned int cpu
,
186 __unused
unsigned int flavor
,
191 static i386_thread_fpstate_t null_fpstate
;
195 case i386_THREAD_STATE
:
196 dprintf(("kdp_readregs THREAD_STATE\n"));
197 kdp_getstate((i386_thread_state_t
*)data
);
198 *size
= sizeof (i386_thread_state_t
);
199 return KDPERR_NO_ERROR
;
201 case i386_THREAD_FPSTATE
:
202 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
203 *(i386_thread_fpstate_t
*)data
= null_fpstate
;
204 *size
= sizeof (i386_thread_fpstate_t
);
205 return KDPERR_NO_ERROR
;
208 dprintf(("kdp_readregs bad flavor %d\n", flavor
));
210 return KDPERR_BADFLAVOR
;
215 kdp_machine_write_regs(
216 __unused
unsigned int cpu
,
224 case i386_THREAD_STATE
:
225 dprintf(("kdp_writeregs THREAD_STATE\n"));
226 kdp_setstate((i386_thread_state_t
*)data
);
227 return KDPERR_NO_ERROR
;
229 case i386_THREAD_FPSTATE
:
230 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
231 return KDPERR_NO_ERROR
;
234 dprintf(("kdp_writeregs bad flavor %d\n"));
235 return KDPERR_BADFLAVOR
;
242 kdp_machine_hostinfo(
243 kdp_hostinfo_t
*hostinfo
248 hostinfo
->cpus_mask
= 0;
250 for (i
= 0; i
< machine_info
.max_cpus
; i
++) {
251 if (cpu_data_ptr
[i
] == NULL
)
254 hostinfo
->cpus_mask
|= (1 << i
);
258 hostinfo
->cpu_type
= CPU_TYPE_I386
;
259 hostinfo
->cpu_subtype
= CPU_SUBTYPE_486
;
267 kprintf("kdp panic: %s\n", msg
);
268 __asm__
volatile("hlt");
297 kdp_us_spin(int usec
)
302 void print_saved_state(void *state
)
304 struct i386_saved_state
*saved_state
;
308 kprintf("pc = 0x%x\n", saved_state
->eip
);
309 kprintf("cr3= 0x%x\n", saved_state
->cr2
);
310 kprintf("rp = TODO FIXME\n");
311 kprintf("sp = 0x%x\n", saved_state
->esp
);
318 return; /* No op here. */
324 __asm__
volatile ("int $3"); /* Let the processor do the work */
328 typedef struct _cframe_t
{
329 struct _cframe_t
*prev
;
334 #include <i386/pmap.h>
335 extern pt_entry_t
*DMAP2
;
336 extern caddr_t DADDR2
;
339 kdp_print_phys(int src
)
344 *(int *) DMAP2
= 0x63 | (src
& 0xfffff000);
345 invlpg((u_int
) DADDR2
);
346 iptr
= (unsigned int *) DADDR2
;
347 for (i
= 0; i
< 100; i
++) {
348 kprintf("0x%x ", *iptr
++);
358 #define MAX_FRAME_DELTA 65536
361 kdp_i386_backtrace(void *_frame
, int nframes
)
363 cframe_t
*frame
= (cframe_t
*)_frame
;
366 for (i
=0; i
<nframes
; i
++) {
367 if ((vm_offset_t
)frame
< VM_MIN_KERNEL_ADDRESS
||
368 (vm_offset_t
)frame
> VM_MAX_KERNEL_ADDRESS
) {
371 kprintf("frame 0x%x called by 0x%x ",
372 frame
, frame
->caller
);
373 kprintf("args 0x%x 0x%x 0x%x 0x%x\n",
374 frame
->args
[0], frame
->args
[1],
375 frame
->args
[2], frame
->args
[3]);
376 if ((frame
->prev
< frame
) || /* wrong direction */
377 ((frame
->prev
- frame
) > MAX_FRAME_DELTA
)) {
384 kprintf("invalid frame pointer 0x%x\n",frame
);
390 struct i386_saved_state
*saved_state
,
391 kern_return_t result
,
395 unsigned int exception
, subcode
= 0, code
;
399 if (trapno
!= T_INT3
&& trapno
!= T_DEBUG
)
400 kprintf("unexpected kernel trap 0x%x eip 0x%x cr2 0x%x \n",
401 trapno
, saved_state
->eip
, saved_state
->esp
);
406 exception
= EXC_ARITHMETIC
;
407 code
= EXC_I386_DIVERR
;
411 exception
= EXC_SOFTWARE
;
412 code
= EXC_I386_INTOFLT
;
415 case T_OUT_OF_BOUNDS
:
416 exception
= EXC_ARITHMETIC
;
417 code
= EXC_I386_BOUNDFLT
;
420 case T_INVALID_OPCODE
:
421 exception
= EXC_BAD_INSTRUCTION
;
422 code
= EXC_I386_INVOPFLT
;
425 case T_SEGMENT_NOT_PRESENT
:
426 exception
= EXC_BAD_INSTRUCTION
;
427 code
= EXC_I386_SEGNPFLT
;
428 subcode
= saved_state
->err
;
432 exception
= EXC_BAD_INSTRUCTION
;
433 code
= EXC_I386_STKFLT
;
434 subcode
= saved_state
->err
;
437 case T_GENERAL_PROTECTION
:
438 exception
= EXC_BAD_INSTRUCTION
;
439 code
= EXC_I386_GPFLT
;
440 subcode
= saved_state
->err
;
444 exception
= EXC_BAD_ACCESS
;
450 exception
= EXC_SOFTWARE
;
451 code
= EXC_I386_ALIGNFLT
;
456 exception
= EXC_BREAKPOINT
;
457 code
= EXC_I386_BPTFLT
;
461 exception
= EXC_BAD_INSTRUCTION
;
466 kdp_i386_backtrace((void *) saved_state
->ebp
, 10);
468 kdp_raise_exception(exception
, code
, subcode
, saved_state
);
481 kdp_ml_get_breakinsn(void)