]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/ml/i386/kdp_machdep.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / kdp / ml / i386 / kdp_machdep.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <mach/mach_types.h>
30 #include <mach/machine.h>
31 #include <mach/exception_types.h>
32 #include <kern/cpu_data.h>
33 #include <i386/trap.h>
34 #include <i386/mp.h>
35 #include <kdp/kdp_internal.h>
36
37 #define KDP_TEST_HARNESS 0
38 #if KDP_TEST_HARNESS
39 #define dprintf(x) printf x
40 #else
41 #define dprintf(x)
42 #endif
43
44 extern void kdreboot(void);
45
46 void print_saved_state(void *);
47 void kdp_call(void);
48 int kdp_getc(void);
49 boolean_t kdp_call_kdb(void);
50 void kdp_getstate(i386_thread_state_t *);
51 void kdp_setstate(i386_thread_state_t *);
52 void kdp_print_phys(int);
53 void kdp_i386_backtrace(void *, int);
54 void kdp_i386_trap(
55 unsigned int,
56 struct i386_saved_state *,
57 kern_return_t,
58 vm_offset_t);
59
60 void
61 kdp_exception(
62 unsigned char *pkt,
63 int *len,
64 unsigned short *remote_port,
65 unsigned int exception,
66 unsigned int code,
67 unsigned int subcode
68 )
69 {
70 kdp_exception_t *rq = (kdp_exception_t *)pkt;
71
72 rq->hdr.request = KDP_EXCEPTION;
73 rq->hdr.is_reply = 0;
74 rq->hdr.seq = kdp.exception_seq;
75 rq->hdr.key = 0;
76 rq->hdr.len = sizeof (*rq);
77
78 rq->n_exc_info = 1;
79 rq->exc_info[0].cpu = 0;
80 rq->exc_info[0].exception = exception;
81 rq->exc_info[0].code = code;
82 rq->exc_info[0].subcode = subcode;
83
84 rq->hdr.len += rq->n_exc_info * sizeof (kdp_exc_info_t);
85
86 bcopy((char *)rq, (char *)pkt, rq->hdr.len);
87
88 kdp.exception_ack_needed = TRUE;
89
90 *remote_port = kdp.exception_port;
91 *len = rq->hdr.len;
92 }
93
94 boolean_t
95 kdp_exception_ack(
96 unsigned char *pkt,
97 int len
98 )
99 {
100 kdp_exception_ack_t *rq = (kdp_exception_ack_t *)pkt;
101
102 if (((unsigned int) len) < sizeof (*rq))
103 return(FALSE);
104
105 if (!rq->hdr.is_reply || rq->hdr.request != KDP_EXCEPTION)
106 return(FALSE);
107
108 dprintf(("kdp_exception_ack seq %x %x\n", rq->hdr.seq, kdp.exception_seq));
109
110 if (rq->hdr.seq == kdp.exception_seq) {
111 kdp.exception_ack_needed = FALSE;
112 kdp.exception_seq++;
113 }
114 return(TRUE);
115 }
116
117 void
118 kdp_getstate(
119 i386_thread_state_t *state
120 )
121 {
122 static i386_thread_state_t null_state;
123 struct i386_saved_state *saved_state;
124
125 saved_state = (struct i386_saved_state *)kdp.saved_state;
126
127 *state = null_state;
128 state->eax = saved_state->eax;
129 state->ebx = saved_state->ebx;
130 state->ecx = saved_state->ecx;
131 state->edx = saved_state->edx;
132 state->edi = saved_state->edi;
133 state->esi = saved_state->esi;
134 state->ebp = saved_state->ebp;
135
136 if ((saved_state->cs & 0x3) == 0){ /* Kernel State */
137 state->esp = (unsigned int) &saved_state->uesp;
138 state->ss = KERNEL_DS;
139 } else {
140 state->esp = saved_state->uesp;
141 state->ss = saved_state->ss;
142 }
143
144 state->eflags = saved_state->efl;
145 state->eip = saved_state->eip;
146 state->cs = saved_state->cs;
147 state->ds = saved_state->ds;
148 state->es = saved_state->es;
149 state->fs = saved_state->fs;
150 state->gs = saved_state->gs;
151 }
152
153
154 void
155 kdp_setstate(
156 i386_thread_state_t *state
157 )
158 {
159 struct i386_saved_state *saved_state;
160
161 saved_state = (struct i386_saved_state *)kdp.saved_state;
162
163 saved_state->eax = state->eax;
164 saved_state->ebx = state->ebx;
165 saved_state->ecx = state->ecx;
166 saved_state->edx = state->edx;
167 saved_state->edi = state->edi;
168 saved_state->esi = state->esi;
169 saved_state->ebp = state->ebp;
170 saved_state->efl = state->eflags;
171 #if 0
172 saved_state->frame.eflags &= ~( EFL_VM | EFL_NT | EFL_IOPL | EFL_CLR );
173 saved_state->frame.eflags |= ( EFL_IF | EFL_SET );
174 #endif
175 saved_state->eip = state->eip;
176 saved_state->fs = state->fs;
177 saved_state->gs = state->gs;
178 }
179
180
181 kdp_error_t
182 kdp_machine_read_regs(
183 __unused unsigned int cpu,
184 __unused unsigned int flavor,
185 char *data,
186 __unused int *size
187 )
188 {
189 static i386_thread_fpstate_t null_fpstate;
190
191 switch (flavor) {
192
193 case i386_THREAD_STATE:
194 dprintf(("kdp_readregs THREAD_STATE\n"));
195 kdp_getstate((i386_thread_state_t *)data);
196 *size = sizeof (i386_thread_state_t);
197 return KDPERR_NO_ERROR;
198
199 case i386_THREAD_FPSTATE:
200 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
201 *(i386_thread_fpstate_t *)data = null_fpstate;
202 *size = sizeof (i386_thread_fpstate_t);
203 return KDPERR_NO_ERROR;
204
205 default:
206 dprintf(("kdp_readregs bad flavor %d\n", flavor));
207 *size = 0;
208 return KDPERR_BADFLAVOR;
209 }
210 }
211
212 kdp_error_t
213 kdp_machine_write_regs(
214 __unused unsigned int cpu,
215 unsigned int flavor,
216 char *data,
217 __unused int *size
218 )
219 {
220 switch (flavor) {
221
222 case i386_THREAD_STATE:
223 dprintf(("kdp_writeregs THREAD_STATE\n"));
224 kdp_setstate((i386_thread_state_t *)data);
225 return KDPERR_NO_ERROR;
226
227 case i386_THREAD_FPSTATE:
228 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
229 return KDPERR_NO_ERROR;
230
231 default:
232 dprintf(("kdp_writeregs bad flavor %d\n"));
233 return KDPERR_BADFLAVOR;
234 }
235 }
236
237
238
239 void
240 kdp_machine_hostinfo(
241 kdp_hostinfo_t *hostinfo
242 )
243 {
244 int i;
245
246 hostinfo->cpus_mask = 0;
247
248 for (i = 0; i < machine_info.max_cpus; i++) {
249 if (cpu_data_ptr[i] == NULL)
250 continue;
251
252 hostinfo->cpus_mask |= (1 << i);
253 }
254
255 /* FIXME?? */
256 hostinfo->cpu_type = CPU_TYPE_I386;
257 hostinfo->cpu_subtype = CPU_SUBTYPE_486;
258 }
259
260 void
261 kdp_panic(
262 const char *msg
263 )
264 {
265 kprintf("kdp panic: %s\n", msg);
266 __asm__ volatile("hlt");
267 }
268
269
270 void
271 kdp_reboot(void)
272 {
273 kdreboot();
274 }
275
276 int
277 kdp_intr_disbl(void)
278 {
279 return splhigh();
280 }
281
282 void
283 kdp_intr_enbl(int s)
284 {
285 splx(s);
286 }
287
288 int
289 kdp_getc()
290 {
291 return cnmaygetc();
292 }
293
294 void
295 kdp_us_spin(int usec)
296 {
297 delay(usec/100);
298 }
299
300 void print_saved_state(void *state)
301 {
302 struct i386_saved_state *saved_state;
303
304 saved_state = state;
305
306 kprintf("pc = 0x%x\n", saved_state->eip);
307 kprintf("cr3= 0x%x\n", saved_state->cr2);
308 kprintf("rp = TODO FIXME\n");
309 kprintf("sp = 0x%x\n", saved_state->esp);
310
311 }
312
313 void
314 kdp_sync_cache()
315 {
316 return; /* No op here. */
317 }
318
319 void
320 kdp_call()
321 {
322 __asm__ volatile ("int $3"); /* Let the processor do the work */
323 }
324
325
326 typedef struct _cframe_t {
327 struct _cframe_t *prev;
328 unsigned caller;
329 unsigned args[0];
330 } cframe_t;
331
332 #include <i386/pmap.h>
333 extern pt_entry_t *DMAP2;
334 extern caddr_t DADDR2;
335
336 void
337 kdp_print_phys(int src)
338 {
339 unsigned int *iptr;
340 int i;
341
342 *(int *) DMAP2 = 0x63 | (src & 0xfffff000);
343 invlpg((u_int) DADDR2);
344 iptr = (unsigned int *) DADDR2;
345 for (i = 0; i < 100; i++) {
346 kprintf("0x%x ", *iptr++);
347 if ((i % 8) == 0)
348 kprintf("\n");
349 }
350 kprintf("\n");
351 *(int *) DMAP2 = 0;
352
353 }
354
355
356 #define MAX_FRAME_DELTA 65536
357
358 void
359 kdp_i386_backtrace(void *_frame, int nframes)
360 {
361 cframe_t *frame = (cframe_t *)_frame;
362 int i;
363
364 for (i=0; i<nframes; i++) {
365 if ((vm_offset_t)frame < VM_MIN_KERNEL_ADDRESS ||
366 (vm_offset_t)frame > VM_MAX_KERNEL_ADDRESS) {
367 goto invalid;
368 }
369 kprintf("frame 0x%x called by 0x%x ",
370 frame, frame->caller);
371 kprintf("args 0x%x 0x%x 0x%x 0x%x\n",
372 frame->args[0], frame->args[1],
373 frame->args[2], frame->args[3]);
374 if ((frame->prev < frame) || /* wrong direction */
375 ((frame->prev - frame) > MAX_FRAME_DELTA)) {
376 goto invalid;
377 }
378 frame = frame->prev;
379 }
380 return;
381 invalid:
382 kprintf("invalid frame pointer 0x%x\n",frame);
383 }
384
385 void
386 kdp_i386_trap(
387 unsigned int trapno,
388 struct i386_saved_state *saved_state,
389 kern_return_t result,
390 vm_offset_t va
391 )
392 {
393 unsigned int exception, subcode = 0, code;
394
395 mp_kdp_enter();
396
397 if (trapno != T_INT3 && trapno != T_DEBUG)
398 kprintf("unexpected kernel trap 0x%x eip 0x%x cr2 0x%x \n",
399 trapno, saved_state->eip, saved_state->esp);
400
401 switch (trapno) {
402
403 case T_DIVIDE_ERROR:
404 exception = EXC_ARITHMETIC;
405 code = EXC_I386_DIVERR;
406 break;
407
408 case T_OVERFLOW:
409 exception = EXC_SOFTWARE;
410 code = EXC_I386_INTOFLT;
411 break;
412
413 case T_OUT_OF_BOUNDS:
414 exception = EXC_ARITHMETIC;
415 code = EXC_I386_BOUNDFLT;
416 break;
417
418 case T_INVALID_OPCODE:
419 exception = EXC_BAD_INSTRUCTION;
420 code = EXC_I386_INVOPFLT;
421 break;
422
423 case T_SEGMENT_NOT_PRESENT:
424 exception = EXC_BAD_INSTRUCTION;
425 code = EXC_I386_SEGNPFLT;
426 subcode = saved_state->err;
427 break;
428
429 case T_STACK_FAULT:
430 exception = EXC_BAD_INSTRUCTION;
431 code = EXC_I386_STKFLT;
432 subcode = saved_state->err;
433 break;
434
435 case T_GENERAL_PROTECTION:
436 exception = EXC_BAD_INSTRUCTION;
437 code = EXC_I386_GPFLT;
438 subcode = saved_state->err;
439 break;
440
441 case T_PAGE_FAULT:
442 exception = EXC_BAD_ACCESS;
443 code = result;
444 subcode = va;
445 break;
446
447 case T_WATCHPOINT:
448 exception = EXC_SOFTWARE;
449 code = EXC_I386_ALIGNFLT;
450 break;
451
452 case T_DEBUG:
453 case T_INT3:
454 exception = EXC_BREAKPOINT;
455 code = EXC_I386_BPTFLT;
456 break;
457
458 default:
459 exception = EXC_BAD_INSTRUCTION;
460 code = trapno;
461 break;
462 }
463
464 kdp_i386_backtrace((void *) saved_state->ebp, 10);
465
466 kdp_raise_exception(exception, code, subcode, saved_state);
467
468 mp_kdp_exit();
469 }
470
471 boolean_t
472 kdp_call_kdb(
473 void)
474 {
475 return(FALSE);
476 }
477
478 unsigned int
479 kdp_ml_get_breakinsn(void)
480 {
481 return 0xcc;
482 }