]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/ml/i386/kdp_machdep.c
7c3a2e59dfbc635a7faa3f2f33054c19cf8d2875
[apple/xnu.git] / osfmk / kdp / ml / i386 / kdp_machdep.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
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>
29 #include <i386/mp.h>
30 #include <kdp/kdp_internal.h>
31
32 #define KDP_TEST_HARNESS 0
33 #if KDP_TEST_HARNESS
34 #define dprintf(x) printf x
35 #else
36 #define dprintf(x)
37 #endif
38
39 extern void kdreboot(void);
40
41 void print_saved_state(void *);
42 void kdp_call(void);
43 int kdp_getc(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);
49 void kdp_i386_trap(
50 unsigned int,
51 struct i386_saved_state *,
52 kern_return_t,
53 vm_offset_t);
54
55 void
56 kdp_exception(
57 unsigned char *pkt,
58 int *len,
59 unsigned short *remote_port,
60 unsigned int exception,
61 unsigned int code,
62 unsigned int subcode
63 )
64 {
65 kdp_exception_t *rq = (kdp_exception_t *)pkt;
66
67 rq->hdr.request = KDP_EXCEPTION;
68 rq->hdr.is_reply = 0;
69 rq->hdr.seq = kdp.exception_seq;
70 rq->hdr.key = 0;
71 rq->hdr.len = sizeof (*rq);
72
73 rq->n_exc_info = 1;
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;
78
79 rq->hdr.len += rq->n_exc_info * sizeof (kdp_exc_info_t);
80
81 bcopy((char *)rq, (char *)pkt, rq->hdr.len);
82
83 kdp.exception_ack_needed = TRUE;
84
85 *remote_port = kdp.exception_port;
86 *len = rq->hdr.len;
87 }
88
89 boolean_t
90 kdp_exception_ack(
91 unsigned char *pkt,
92 int len
93 )
94 {
95 kdp_exception_ack_t *rq = (kdp_exception_ack_t *)pkt;
96
97 if (((unsigned int) len) < sizeof (*rq))
98 return(FALSE);
99
100 if (!rq->hdr.is_reply || rq->hdr.request != KDP_EXCEPTION)
101 return(FALSE);
102
103 dprintf(("kdp_exception_ack seq %x %x\n", rq->hdr.seq, kdp.exception_seq));
104
105 if (rq->hdr.seq == kdp.exception_seq) {
106 kdp.exception_ack_needed = FALSE;
107 kdp.exception_seq++;
108 }
109 return(TRUE);
110 }
111
112 void
113 kdp_getstate(
114 i386_thread_state_t *state
115 )
116 {
117 static i386_thread_state_t null_state;
118 struct i386_saved_state *saved_state;
119
120 saved_state = (struct i386_saved_state *)kdp.saved_state;
121
122 *state = null_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;
130
131 if ((saved_state->cs & 0x3) == 0){ /* Kernel State */
132 state->esp = (unsigned int) &saved_state->uesp;
133 state->ss = KERNEL_DS;
134 } else {
135 state->esp = saved_state->uesp;
136 state->ss = saved_state->ss;
137 }
138
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;
146 }
147
148
149 void
150 kdp_setstate(
151 i386_thread_state_t *state
152 )
153 {
154 struct i386_saved_state *saved_state;
155
156 saved_state = (struct i386_saved_state *)kdp.saved_state;
157
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;
166 #if 0
167 saved_state->frame.eflags &= ~( EFL_VM | EFL_NT | EFL_IOPL | EFL_CLR );
168 saved_state->frame.eflags |= ( EFL_IF | EFL_SET );
169 #endif
170 saved_state->eip = state->eip;
171 saved_state->fs = state->fs;
172 saved_state->gs = state->gs;
173 }
174
175
176 kdp_error_t
177 kdp_machine_read_regs(
178 __unused unsigned int cpu,
179 __unused unsigned int flavor,
180 char *data,
181 __unused int *size
182 )
183 {
184 static i386_thread_fpstate_t null_fpstate;
185
186 switch (flavor) {
187
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;
193
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;
199
200 default:
201 dprintf(("kdp_readregs bad flavor %d\n", flavor));
202 *size = 0;
203 return KDPERR_BADFLAVOR;
204 }
205 }
206
207 kdp_error_t
208 kdp_machine_write_regs(
209 __unused unsigned int cpu,
210 unsigned int flavor,
211 char *data,
212 __unused int *size
213 )
214 {
215 switch (flavor) {
216
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;
221
222 case i386_THREAD_FPSTATE:
223 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
224 return KDPERR_NO_ERROR;
225
226 default:
227 dprintf(("kdp_writeregs bad flavor %d\n"));
228 return KDPERR_BADFLAVOR;
229 }
230 }
231
232
233
234 void
235 kdp_machine_hostinfo(
236 kdp_hostinfo_t *hostinfo
237 )
238 {
239 int i;
240
241 hostinfo->cpus_mask = 0;
242
243 for (i = 0; i < machine_info.max_cpus; i++) {
244 if (cpu_data_ptr[i] == NULL)
245 continue;
246
247 hostinfo->cpus_mask |= (1 << i);
248 }
249
250 /* FIXME?? */
251 hostinfo->cpu_type = CPU_TYPE_I386;
252 hostinfo->cpu_subtype = CPU_SUBTYPE_486;
253 }
254
255 void
256 kdp_panic(
257 const char *msg
258 )
259 {
260 kprintf("kdp panic: %s\n", msg);
261 __asm__ volatile("hlt");
262 }
263
264
265 void
266 kdp_reboot(void)
267 {
268 kdreboot();
269 }
270
271 int
272 kdp_intr_disbl(void)
273 {
274 return splhigh();
275 }
276
277 void
278 kdp_intr_enbl(int s)
279 {
280 splx(s);
281 }
282
283 int
284 kdp_getc()
285 {
286 return cnmaygetc();
287 }
288
289 void
290 kdp_us_spin(int usec)
291 {
292 delay(usec/100);
293 }
294
295 void print_saved_state(void *state)
296 {
297 struct i386_saved_state *saved_state;
298
299 saved_state = state;
300
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);
305
306 }
307
308 void
309 kdp_sync_cache()
310 {
311 return; /* No op here. */
312 }
313
314 void
315 kdp_call()
316 {
317 __asm__ volatile ("int $3"); /* Let the processor do the work */
318 }
319
320
321 typedef struct _cframe_t {
322 struct _cframe_t *prev;
323 unsigned caller;
324 unsigned args[0];
325 } cframe_t;
326
327 #include <i386/pmap.h>
328 extern pt_entry_t *DMAP2;
329 extern caddr_t DADDR2;
330
331 void
332 kdp_print_phys(int src)
333 {
334 unsigned int *iptr;
335 int i;
336
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++);
342 if ((i % 8) == 0)
343 kprintf("\n");
344 }
345 kprintf("\n");
346 *(int *) DMAP2 = 0;
347
348 }
349
350
351 #define MAX_FRAME_DELTA 65536
352
353 void
354 kdp_i386_backtrace(void *_frame, int nframes)
355 {
356 cframe_t *frame = (cframe_t *)_frame;
357 int i;
358
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) {
362 goto invalid;
363 }
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)) {
371 goto invalid;
372 }
373 frame = frame->prev;
374 }
375 return;
376 invalid:
377 kprintf("invalid frame pointer 0x%x\n",frame);
378 }
379
380 void
381 kdp_i386_trap(
382 unsigned int trapno,
383 struct i386_saved_state *saved_state,
384 kern_return_t result,
385 vm_offset_t va
386 )
387 {
388 unsigned int exception, subcode = 0, code;
389
390 mp_kdp_enter();
391
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);
395
396 switch (trapno) {
397
398 case T_DIVIDE_ERROR:
399 exception = EXC_ARITHMETIC;
400 code = EXC_I386_DIVERR;
401 break;
402
403 case T_OVERFLOW:
404 exception = EXC_SOFTWARE;
405 code = EXC_I386_INTOFLT;
406 break;
407
408 case T_OUT_OF_BOUNDS:
409 exception = EXC_ARITHMETIC;
410 code = EXC_I386_BOUNDFLT;
411 break;
412
413 case T_INVALID_OPCODE:
414 exception = EXC_BAD_INSTRUCTION;
415 code = EXC_I386_INVOPFLT;
416 break;
417
418 case T_SEGMENT_NOT_PRESENT:
419 exception = EXC_BAD_INSTRUCTION;
420 code = EXC_I386_SEGNPFLT;
421 subcode = saved_state->err;
422 break;
423
424 case T_STACK_FAULT:
425 exception = EXC_BAD_INSTRUCTION;
426 code = EXC_I386_STKFLT;
427 subcode = saved_state->err;
428 break;
429
430 case T_GENERAL_PROTECTION:
431 exception = EXC_BAD_INSTRUCTION;
432 code = EXC_I386_GPFLT;
433 subcode = saved_state->err;
434 break;
435
436 case T_PAGE_FAULT:
437 exception = EXC_BAD_ACCESS;
438 code = result;
439 subcode = va;
440 break;
441
442 case T_WATCHPOINT:
443 exception = EXC_SOFTWARE;
444 code = EXC_I386_ALIGNFLT;
445 break;
446
447 case T_DEBUG:
448 case T_INT3:
449 exception = EXC_BREAKPOINT;
450 code = EXC_I386_BPTFLT;
451 break;
452
453 default:
454 exception = EXC_BAD_INSTRUCTION;
455 code = trapno;
456 break;
457 }
458
459 kdp_i386_backtrace((void *) saved_state->ebp, 10);
460
461 kdp_raise_exception(exception, code, subcode, saved_state);
462
463 mp_kdp_exit();
464 }
465
466 boolean_t
467 kdp_call_kdb(
468 void)
469 {
470 return(FALSE);
471 }
472
473 unsigned int
474 kdp_ml_get_breakinsn(void)
475 {
476 return 0xcc;
477 }