]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
8ad349bb | 4 | * @APPLE_LICENSE_OSREFERENCE_HEADER_START@ |
1c79356b | 5 | * |
8ad349bb A |
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 | |
14 | * agreement. | |
15 | * | |
16 | * Please obtain a copy of the License at | |
17 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
18 | * file. | |
19 | * | |
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. | |
27 | * | |
28 | * @APPLE_LICENSE_OSREFERENCE_HEADER_END@ | |
1c79356b | 29 | */ |
1c79356b A |
30 | |
31 | #include <mach/mach_types.h> | |
32 | #include <mach/machine.h> | |
33 | #include <mach/exception_types.h> | |
91447636 | 34 | #include <kern/cpu_data.h> |
1c79356b | 35 | #include <i386/trap.h> |
55e303ae | 36 | #include <i386/mp.h> |
1c79356b A |
37 | #include <kdp/kdp_internal.h> |
38 | ||
39 | #define KDP_TEST_HARNESS 0 | |
40 | #if KDP_TEST_HARNESS | |
41 | #define dprintf(x) printf x | |
42 | #else | |
43 | #define dprintf(x) | |
44 | #endif | |
45 | ||
8ad349bb | 46 | extern void kdreboot(void); |
91447636 A |
47 | |
48 | void print_saved_state(void *); | |
49 | void kdp_call(void); | |
50 | int kdp_getc(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); | |
8ad349bb A |
55 | void kdp_i386_backtrace(void *, int); |
56 | void kdp_i386_trap( | |
57 | unsigned int, | |
58 | struct i386_saved_state *, | |
59 | kern_return_t, | |
60 | vm_offset_t); | |
1c79356b A |
61 | |
62 | void | |
63 | kdp_exception( | |
64 | unsigned char *pkt, | |
65 | int *len, | |
66 | unsigned short *remote_port, | |
67 | unsigned int exception, | |
68 | unsigned int code, | |
69 | unsigned int subcode | |
70 | ) | |
71 | { | |
72 | kdp_exception_t *rq = (kdp_exception_t *)pkt; | |
73 | ||
74 | rq->hdr.request = KDP_EXCEPTION; | |
75 | rq->hdr.is_reply = 0; | |
76 | rq->hdr.seq = kdp.exception_seq; | |
77 | rq->hdr.key = 0; | |
78 | rq->hdr.len = sizeof (*rq); | |
79 | ||
80 | rq->n_exc_info = 1; | |
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; | |
85 | ||
86 | rq->hdr.len += rq->n_exc_info * sizeof (kdp_exc_info_t); | |
87 | ||
88 | bcopy((char *)rq, (char *)pkt, rq->hdr.len); | |
89 | ||
90 | kdp.exception_ack_needed = TRUE; | |
91 | ||
92 | *remote_port = kdp.exception_port; | |
93 | *len = rq->hdr.len; | |
94 | } | |
95 | ||
96 | boolean_t | |
97 | kdp_exception_ack( | |
98 | unsigned char *pkt, | |
99 | int len | |
100 | ) | |
101 | { | |
102 | kdp_exception_ack_t *rq = (kdp_exception_ack_t *)pkt; | |
103 | ||
91447636 | 104 | if (((unsigned int) len) < sizeof (*rq)) |
1c79356b A |
105 | return(FALSE); |
106 | ||
107 | if (!rq->hdr.is_reply || rq->hdr.request != KDP_EXCEPTION) | |
108 | return(FALSE); | |
109 | ||
110 | dprintf(("kdp_exception_ack seq %x %x\n", rq->hdr.seq, kdp.exception_seq)); | |
111 | ||
112 | if (rq->hdr.seq == kdp.exception_seq) { | |
113 | kdp.exception_ack_needed = FALSE; | |
114 | kdp.exception_seq++; | |
115 | } | |
116 | return(TRUE); | |
117 | } | |
118 | ||
119 | void | |
120 | kdp_getstate( | |
8ad349bb | 121 | i386_thread_state_t *state |
1c79356b A |
122 | ) |
123 | { | |
8ad349bb A |
124 | static i386_thread_state_t null_state; |
125 | struct i386_saved_state *saved_state; | |
1c79356b | 126 | |
8ad349bb | 127 | saved_state = (struct i386_saved_state *)kdp.saved_state; |
1c79356b | 128 | |
91447636 | 129 | *state = null_state; |
1c79356b A |
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; | |
137 | ||
138 | if ((saved_state->cs & 0x3) == 0){ /* Kernel State */ | |
139 | state->esp = (unsigned int) &saved_state->uesp; | |
140 | state->ss = KERNEL_DS; | |
141 | } else { | |
142 | state->esp = saved_state->uesp; | |
143 | state->ss = saved_state->ss; | |
144 | } | |
145 | ||
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; | |
153 | } | |
154 | ||
155 | ||
156 | void | |
157 | kdp_setstate( | |
8ad349bb | 158 | i386_thread_state_t *state |
1c79356b A |
159 | ) |
160 | { | |
8ad349bb | 161 | struct i386_saved_state *saved_state; |
1c79356b | 162 | |
8ad349bb | 163 | saved_state = (struct i386_saved_state *)kdp.saved_state; |
1c79356b A |
164 | |
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; | |
173 | #if 0 | |
174 | saved_state->frame.eflags &= ~( EFL_VM | EFL_NT | EFL_IOPL | EFL_CLR ); | |
175 | saved_state->frame.eflags |= ( EFL_IF | EFL_SET ); | |
176 | #endif | |
177 | saved_state->eip = state->eip; | |
178 | saved_state->fs = state->fs; | |
179 | saved_state->gs = state->gs; | |
180 | } | |
181 | ||
182 | ||
183 | kdp_error_t | |
184 | kdp_machine_read_regs( | |
91447636 A |
185 | __unused unsigned int cpu, |
186 | __unused unsigned int flavor, | |
1c79356b | 187 | char *data, |
91447636 | 188 | __unused int *size |
1c79356b A |
189 | ) |
190 | { | |
8ad349bb | 191 | static i386_thread_fpstate_t null_fpstate; |
91447636 | 192 | |
1c79356b A |
193 | switch (flavor) { |
194 | ||
8ad349bb | 195 | case i386_THREAD_STATE: |
1c79356b | 196 | dprintf(("kdp_readregs THREAD_STATE\n")); |
8ad349bb A |
197 | kdp_getstate((i386_thread_state_t *)data); |
198 | *size = sizeof (i386_thread_state_t); | |
1c79356b A |
199 | return KDPERR_NO_ERROR; |
200 | ||
8ad349bb | 201 | case i386_THREAD_FPSTATE: |
1c79356b | 202 | dprintf(("kdp_readregs THREAD_FPSTATE\n")); |
8ad349bb A |
203 | *(i386_thread_fpstate_t *)data = null_fpstate; |
204 | *size = sizeof (i386_thread_fpstate_t); | |
1c79356b A |
205 | return KDPERR_NO_ERROR; |
206 | ||
207 | default: | |
91447636 A |
208 | dprintf(("kdp_readregs bad flavor %d\n", flavor)); |
209 | *size = 0; | |
1c79356b A |
210 | return KDPERR_BADFLAVOR; |
211 | } | |
212 | } | |
213 | ||
214 | kdp_error_t | |
215 | kdp_machine_write_regs( | |
91447636 | 216 | __unused unsigned int cpu, |
1c79356b A |
217 | unsigned int flavor, |
218 | char *data, | |
91447636 | 219 | __unused int *size |
1c79356b A |
220 | ) |
221 | { | |
222 | switch (flavor) { | |
223 | ||
8ad349bb | 224 | case i386_THREAD_STATE: |
1c79356b | 225 | dprintf(("kdp_writeregs THREAD_STATE\n")); |
8ad349bb | 226 | kdp_setstate((i386_thread_state_t *)data); |
1c79356b A |
227 | return KDPERR_NO_ERROR; |
228 | ||
8ad349bb | 229 | case i386_THREAD_FPSTATE: |
1c79356b A |
230 | dprintf(("kdp_writeregs THREAD_FPSTATE\n")); |
231 | return KDPERR_NO_ERROR; | |
232 | ||
233 | default: | |
234 | dprintf(("kdp_writeregs bad flavor %d\n")); | |
235 | return KDPERR_BADFLAVOR; | |
236 | } | |
237 | } | |
238 | ||
239 | ||
240 | ||
241 | void | |
242 | kdp_machine_hostinfo( | |
243 | kdp_hostinfo_t *hostinfo | |
244 | ) | |
245 | { | |
1c79356b A |
246 | int i; |
247 | ||
248 | hostinfo->cpus_mask = 0; | |
249 | ||
250 | for (i = 0; i < machine_info.max_cpus; i++) { | |
91447636 | 251 | if (cpu_data_ptr[i] == NULL) |
1c79356b A |
252 | continue; |
253 | ||
254 | hostinfo->cpus_mask |= (1 << i); | |
255 | } | |
256 | ||
8ad349bb A |
257 | /* FIXME?? */ |
258 | hostinfo->cpu_type = CPU_TYPE_I386; | |
259 | hostinfo->cpu_subtype = CPU_SUBTYPE_486; | |
1c79356b A |
260 | } |
261 | ||
262 | void | |
263 | kdp_panic( | |
264 | const char *msg | |
265 | ) | |
266 | { | |
91447636 | 267 | kprintf("kdp panic: %s\n", msg); |
1c79356b A |
268 | __asm__ volatile("hlt"); |
269 | } | |
270 | ||
271 | ||
272 | void | |
273 | kdp_reboot(void) | |
274 | { | |
8ad349bb | 275 | kdreboot(); |
1c79356b A |
276 | } |
277 | ||
278 | int | |
279 | kdp_intr_disbl(void) | |
280 | { | |
281 | return splhigh(); | |
282 | } | |
283 | ||
284 | void | |
285 | kdp_intr_enbl(int s) | |
286 | { | |
287 | splx(s); | |
288 | } | |
289 | ||
290 | int | |
291 | kdp_getc() | |
292 | { | |
293 | return cnmaygetc(); | |
294 | } | |
295 | ||
296 | void | |
297 | kdp_us_spin(int usec) | |
298 | { | |
1c79356b A |
299 | delay(usec/100); |
300 | } | |
301 | ||
302 | void print_saved_state(void *state) | |
303 | { | |
8ad349bb | 304 | struct i386_saved_state *saved_state; |
1c79356b A |
305 | |
306 | saved_state = state; | |
307 | ||
91447636 | 308 | kprintf("pc = 0x%x\n", saved_state->eip); |
8ad349bb | 309 | kprintf("cr3= 0x%x\n", saved_state->cr2); |
91447636 | 310 | kprintf("rp = TODO FIXME\n"); |
8ad349bb | 311 | kprintf("sp = 0x%x\n", saved_state->esp); |
1c79356b A |
312 | |
313 | } | |
314 | ||
315 | void | |
316 | kdp_sync_cache() | |
317 | { | |
318 | return; /* No op here. */ | |
319 | } | |
320 | ||
321 | void | |
322 | kdp_call() | |
323 | { | |
324 | __asm__ volatile ("int $3"); /* Let the processor do the work */ | |
325 | } | |
326 | ||
327 | ||
328 | typedef struct _cframe_t { | |
329 | struct _cframe_t *prev; | |
330 | unsigned caller; | |
331 | unsigned args[0]; | |
332 | } cframe_t; | |
333 | ||
91447636 A |
334 | #include <i386/pmap.h> |
335 | extern pt_entry_t *DMAP2; | |
336 | extern caddr_t DADDR2; | |
337 | ||
338 | void | |
339 | kdp_print_phys(int src) | |
340 | { | |
341 | unsigned int *iptr; | |
342 | int i; | |
343 | ||
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++); | |
349 | if ((i % 8) == 0) | |
350 | kprintf("\n"); | |
351 | } | |
352 | kprintf("\n"); | |
353 | *(int *) DMAP2 = 0; | |
354 | ||
355 | } | |
356 | ||
8ad349bb A |
357 | |
358 | #define MAX_FRAME_DELTA 65536 | |
359 | ||
360 | void | |
361 | kdp_i386_backtrace(void *_frame, int nframes) | |
362 | { | |
363 | cframe_t *frame = (cframe_t *)_frame; | |
364 | int i; | |
365 | ||
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) { | |
369 | goto invalid; | |
370 | } | |
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)) { | |
378 | goto invalid; | |
379 | } | |
380 | frame = frame->prev; | |
381 | } | |
382 | return; | |
383 | invalid: | |
384 | kprintf("invalid frame pointer 0x%x\n",frame); | |
385 | } | |
386 | ||
387 | void | |
1c79356b | 388 | kdp_i386_trap( |
8ad349bb A |
389 | unsigned int trapno, |
390 | struct i386_saved_state *saved_state, | |
1c79356b A |
391 | kern_return_t result, |
392 | vm_offset_t va | |
393 | ) | |
394 | { | |
395 | unsigned int exception, subcode = 0, code; | |
396 | ||
c0fea474 | 397 | mp_kdp_enter(); |
1c79356b | 398 | |
8ad349bb A |
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); | |
402 | ||
1c79356b A |
403 | switch (trapno) { |
404 | ||
405 | case T_DIVIDE_ERROR: | |
406 | exception = EXC_ARITHMETIC; | |
407 | code = EXC_I386_DIVERR; | |
408 | break; | |
409 | ||
410 | case T_OVERFLOW: | |
411 | exception = EXC_SOFTWARE; | |
412 | code = EXC_I386_INTOFLT; | |
413 | break; | |
414 | ||
415 | case T_OUT_OF_BOUNDS: | |
416 | exception = EXC_ARITHMETIC; | |
417 | code = EXC_I386_BOUNDFLT; | |
418 | break; | |
419 | ||
420 | case T_INVALID_OPCODE: | |
421 | exception = EXC_BAD_INSTRUCTION; | |
422 | code = EXC_I386_INVOPFLT; | |
423 | break; | |
424 | ||
425 | case T_SEGMENT_NOT_PRESENT: | |
426 | exception = EXC_BAD_INSTRUCTION; | |
427 | code = EXC_I386_SEGNPFLT; | |
428 | subcode = saved_state->err; | |
429 | break; | |
430 | ||
431 | case T_STACK_FAULT: | |
432 | exception = EXC_BAD_INSTRUCTION; | |
433 | code = EXC_I386_STKFLT; | |
434 | subcode = saved_state->err; | |
435 | break; | |
436 | ||
437 | case T_GENERAL_PROTECTION: | |
438 | exception = EXC_BAD_INSTRUCTION; | |
439 | code = EXC_I386_GPFLT; | |
440 | subcode = saved_state->err; | |
441 | break; | |
442 | ||
443 | case T_PAGE_FAULT: | |
444 | exception = EXC_BAD_ACCESS; | |
445 | code = result; | |
446 | subcode = va; | |
447 | break; | |
448 | ||
449 | case T_WATCHPOINT: | |
450 | exception = EXC_SOFTWARE; | |
451 | code = EXC_I386_ALIGNFLT; | |
452 | break; | |
453 | ||
454 | case T_DEBUG: | |
455 | case T_INT3: | |
456 | exception = EXC_BREAKPOINT; | |
457 | code = EXC_I386_BPTFLT; | |
458 | break; | |
459 | ||
460 | default: | |
461 | exception = EXC_BAD_INSTRUCTION; | |
462 | code = trapno; | |
463 | break; | |
464 | } | |
465 | ||
8ad349bb A |
466 | kdp_i386_backtrace((void *) saved_state->ebp, 10); |
467 | ||
1c79356b | 468 | kdp_raise_exception(exception, code, subcode, saved_state); |
55e303ae A |
469 | |
470 | mp_kdp_exit(); | |
1c79356b A |
471 | } |
472 | ||
473 | boolean_t | |
474 | kdp_call_kdb( | |
475 | void) | |
476 | { | |
477 | return(FALSE); | |
478 | } | |
9bccf70c | 479 | |
91447636 A |
480 | unsigned int |
481 | kdp_ml_get_breakinsn(void) | |
9bccf70c A |
482 | { |
483 | return 0xcc; | |
484 | } |