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