2 * Copyright (c) 2000-2004 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/exception_types.h>
33 #include <ppc/exception.h>
34 #include <ppc/proc_reg.h>
35 #include <kdp/kdp_internal.h>
36 #include <ppc/savearea.h>
37 #include <kern/debug.h>
38 #include <IOKit/IOPlatformExpert.h>
40 #define KDP_TEST_HARNESS 0
42 #define dprintf(x) kprintf x
47 void print_saved_state(void *);
49 void kdp_trap( unsigned int, struct savearea
*saved_state
);
51 boolean_t
kdp_call_kdb(void);
57 unsigned short *remote_port
,
58 unsigned int exception
,
67 kdp_exception_t
*rq
= (kdp_exception_t
*)&aligned_pkt
;
69 bcopy((char *)pkt
, (char *)rq
, sizeof(*rq
));
70 rq
->hdr
.request
= KDP_EXCEPTION
;
72 rq
->hdr
.seq
= kdp
.exception_seq
;
74 rq
->hdr
.len
= sizeof (*rq
) + sizeof(kdp_exc_info_t
);
77 rq
->exc_info
[0].cpu
= 0;
78 rq
->exc_info
[0].exception
= exception
;
79 rq
->exc_info
[0].code
= code
;
80 rq
->exc_info
[0].subcode
= subcode
;
82 rq
->hdr
.len
+= rq
->n_exc_info
* sizeof (kdp_exc_info_t
);
84 bcopy((char *)rq
, (char *)pkt
, rq
->hdr
.len
);
86 kdp
.exception_ack_needed
= TRUE
;
88 *remote_port
= kdp
.exception_port
;
98 kdp_exception_ack_t aligned_pkt
;
99 kdp_exception_ack_t
*rq
= (kdp_exception_ack_t
*)&aligned_pkt
;
101 if (len
< sizeof (*rq
))
104 bcopy((char *)pkt
, (char *)rq
, sizeof(*rq
));
106 if (!rq
->hdr
.is_reply
|| rq
->hdr
.request
!= KDP_EXCEPTION
)
109 dprintf(("kdp_exception_ack seq %x %x\n", rq
->hdr
.seq
, kdp
.exception_seq
));
111 if (rq
->hdr
.seq
== kdp
.exception_seq
) {
112 kdp
.exception_ack_needed
= FALSE
;
120 struct ppc_thread_state
*state
123 struct savearea
*saved_state
;
125 saved_state
= kdp
.saved_state
;
127 bzero((char *)state
,sizeof (struct ppc_thread_state
)) ;
129 state
->srr0
= (unsigned int)saved_state
->save_srr0
;
130 state
->srr1
= (unsigned int)saved_state
->save_srr1
;
131 state
->r0
= (unsigned int)saved_state
->save_r0
;
132 state
->r1
= (unsigned int)saved_state
->save_r1
;
133 state
->r2
= (unsigned int)saved_state
->save_r2
;
134 state
->r3
= (unsigned int)saved_state
->save_r3
;
135 state
->r4
= (unsigned int)saved_state
->save_r4
;
136 state
->r5
= (unsigned int)saved_state
->save_r5
;
137 state
->r6
= (unsigned int)saved_state
->save_r6
;
138 state
->r7
= (unsigned int)saved_state
->save_r7
;
139 state
->r8
= (unsigned int)saved_state
->save_r8
;
140 state
->r9
= (unsigned int)saved_state
->save_r9
;
141 state
->r10
= (unsigned int)saved_state
->save_r10
;
142 state
->r11
= (unsigned int)saved_state
->save_r11
;
143 state
->r12
= (unsigned int)saved_state
->save_r12
;
144 state
->r13
= (unsigned int)saved_state
->save_r13
;
145 state
->r14
= (unsigned int)saved_state
->save_r14
;
146 state
->r15
= (unsigned int)saved_state
->save_r15
;
147 state
->r16
= (unsigned int)saved_state
->save_r16
;
148 state
->r17
= (unsigned int)saved_state
->save_r17
;
149 state
->r18
= (unsigned int)saved_state
->save_r18
;
150 state
->r19
= (unsigned int)saved_state
->save_r19
;
151 state
->r20
= (unsigned int)saved_state
->save_r20
;
152 state
->r21
= (unsigned int)saved_state
->save_r21
;
153 state
->r22
= (unsigned int)saved_state
->save_r22
;
154 state
->r23
= (unsigned int)saved_state
->save_r23
;
155 state
->r24
= (unsigned int)saved_state
->save_r24
;
156 state
->r25
= (unsigned int)saved_state
->save_r25
;
157 state
->r26
= (unsigned int)saved_state
->save_r26
;
158 state
->r27
= (unsigned int)saved_state
->save_r27
;
159 state
->r28
= (unsigned int)saved_state
->save_r28
;
160 state
->r29
= (unsigned int)saved_state
->save_r29
;
161 state
->r30
= (unsigned int)saved_state
->save_r30
;
162 state
->r31
= (unsigned int)saved_state
->save_r31
;
163 state
->cr
= (unsigned int)saved_state
->save_cr
;
164 state
->xer
= (unsigned int)saved_state
->save_xer
;
165 state
->lr
= (unsigned int)saved_state
->save_lr
;
166 state
->ctr
= (unsigned int)saved_state
->save_ctr
;
170 kdp_getintegerstate64(
171 struct ppc_thread_state64
*state
174 struct savearea
*saved_state
;
176 saved_state
= kdp
.saved_state
;
178 bzero((char *)state
,sizeof (struct ppc_thread_state64
)) ;
180 state
->srr0
= saved_state
->save_srr0
;
181 state
->srr1
= saved_state
->save_srr1
;
182 state
->r0
= saved_state
->save_r0
;
183 state
->r1
= saved_state
->save_r1
;
184 state
->r2
= saved_state
->save_r2
;
185 state
->r3
= saved_state
->save_r3
;
186 state
->r4
= saved_state
->save_r4
;
187 state
->r5
= saved_state
->save_r5
;
188 state
->r6
= saved_state
->save_r6
;
189 state
->r7
= saved_state
->save_r7
;
190 state
->r8
= saved_state
->save_r8
;
191 state
->r9
= saved_state
->save_r9
;
192 state
->r10
= saved_state
->save_r10
;
193 state
->r11
= saved_state
->save_r11
;
194 state
->r12
= saved_state
->save_r12
;
195 state
->r13
= saved_state
->save_r13
;
196 state
->r14
= saved_state
->save_r14
;
197 state
->r15
= saved_state
->save_r15
;
198 state
->r16
= saved_state
->save_r16
;
199 state
->r17
= saved_state
->save_r17
;
200 state
->r18
= saved_state
->save_r18
;
201 state
->r19
= saved_state
->save_r19
;
202 state
->r20
= saved_state
->save_r20
;
203 state
->r21
= saved_state
->save_r21
;
204 state
->r22
= saved_state
->save_r22
;
205 state
->r23
= saved_state
->save_r23
;
206 state
->r24
= saved_state
->save_r24
;
207 state
->r25
= saved_state
->save_r25
;
208 state
->r26
= saved_state
->save_r26
;
209 state
->r27
= saved_state
->save_r27
;
210 state
->r28
= saved_state
->save_r28
;
211 state
->r29
= saved_state
->save_r29
;
212 state
->r30
= saved_state
->save_r30
;
213 state
->r31
= saved_state
->save_r31
;
214 state
->cr
= saved_state
->save_cr
;
215 state
->xer
= saved_state
->save_xer
;
216 state
->lr
= saved_state
->save_lr
;
217 state
->ctr
= saved_state
->save_ctr
;
221 kdp_machine_read_regs(
230 case PPC_THREAD_STATE
:
231 dprintf(("kdp_readregs THREAD_STATE\n"));
232 kdp_getintegerstate((struct ppc_thread_state
*)data
);
233 *size
= PPC_THREAD_STATE_COUNT
* sizeof(int);
234 return KDPERR_NO_ERROR
;
236 case PPC_THREAD_STATE64
:
237 dprintf(("kdp_readregs THREAD_STATE\n"));
238 kdp_getintegerstate64((struct ppc_thread_state64
*)data
);
239 *size
= PPC_THREAD_STATE64_COUNT
* sizeof(int);
240 return KDPERR_NO_ERROR
;
242 case PPC_FLOAT_STATE
:
243 dprintf(("kdp_readregs THREAD_FPSTATE\n"));
244 bzero((char *)data
,sizeof(struct ppc_float_state
));
245 *size
= PPC_FLOAT_STATE_COUNT
* sizeof(int);
246 return KDPERR_NO_ERROR
;
249 dprintf(("kdp_readregs bad flavor %d\n"));
250 return KDPERR_BADFLAVOR
;
256 struct ppc_thread_state
*state
259 struct savearea
*saved_state
;
261 saved_state
= kdp
.saved_state
;
263 saved_state
->save_srr0
= state
->srr0
;
264 saved_state
->save_srr1
= state
->srr1
;
265 saved_state
->save_r0
= state
->r0
;
266 saved_state
->save_r1
= state
->r1
;
267 saved_state
->save_r2
= state
->r2
;
268 saved_state
->save_r3
= state
->r3
;
269 saved_state
->save_r4
= state
->r4
;
270 saved_state
->save_r5
= state
->r5
;
271 saved_state
->save_r6
= state
->r6
;
272 saved_state
->save_r7
= state
->r7
;
273 saved_state
->save_r8
= state
->r8
;
274 saved_state
->save_r9
= state
->r9
;
275 saved_state
->save_r10
= state
->r10
;
276 saved_state
->save_r11
= state
->r11
;
277 saved_state
->save_r12
= state
->r12
;
278 saved_state
->save_r13
= state
->r13
;
279 saved_state
->save_r14
= state
->r14
;
280 saved_state
->save_r15
= state
->r15
;
281 saved_state
->save_r16
= state
->r16
;
282 saved_state
->save_r17
= state
->r17
;
283 saved_state
->save_r18
= state
->r18
;
284 saved_state
->save_r19
= state
->r19
;
285 saved_state
->save_r20
= state
->r20
;
286 saved_state
->save_r21
= state
->r21
;
287 saved_state
->save_r22
= state
->r22
;
288 saved_state
->save_r23
= state
->r23
;
289 saved_state
->save_r24
= state
->r24
;
290 saved_state
->save_r25
= state
->r25
;
291 saved_state
->save_r26
= state
->r26
;
292 saved_state
->save_r27
= state
->r27
;
293 saved_state
->save_r28
= state
->r28
;
294 saved_state
->save_r29
= state
->r29
;
295 saved_state
->save_r30
= state
->r30
;
296 saved_state
->save_r31
= state
->r31
;
297 saved_state
->save_cr
= state
->cr
;
298 saved_state
->save_xer
= state
->xer
;
299 saved_state
->save_lr
= state
->lr
;
300 saved_state
->save_ctr
= state
->ctr
;
304 kdp_setintegerstate64(
305 struct ppc_thread_state64
*state
308 struct savearea
*saved_state
;
310 saved_state
= kdp
.saved_state
;
312 saved_state
->save_srr0
= state
->srr0
;
313 saved_state
->save_srr1
= state
->srr1
;
314 saved_state
->save_r0
= state
->r0
;
315 saved_state
->save_r1
= state
->r1
;
316 saved_state
->save_r2
= state
->r2
;
317 saved_state
->save_r3
= state
->r3
;
318 saved_state
->save_r4
= state
->r4
;
319 saved_state
->save_r5
= state
->r5
;
320 saved_state
->save_r6
= state
->r6
;
321 saved_state
->save_r7
= state
->r7
;
322 saved_state
->save_r8
= state
->r8
;
323 saved_state
->save_r9
= state
->r9
;
324 saved_state
->save_r10
= state
->r10
;
325 saved_state
->save_r11
= state
->r11
;
326 saved_state
->save_r12
= state
->r12
;
327 saved_state
->save_r13
= state
->r13
;
328 saved_state
->save_r14
= state
->r14
;
329 saved_state
->save_r15
= state
->r15
;
330 saved_state
->save_r16
= state
->r16
;
331 saved_state
->save_r17
= state
->r17
;
332 saved_state
->save_r18
= state
->r18
;
333 saved_state
->save_r19
= state
->r19
;
334 saved_state
->save_r20
= state
->r20
;
335 saved_state
->save_r21
= state
->r21
;
336 saved_state
->save_r22
= state
->r22
;
337 saved_state
->save_r23
= state
->r23
;
338 saved_state
->save_r24
= state
->r24
;
339 saved_state
->save_r25
= state
->r25
;
340 saved_state
->save_r26
= state
->r26
;
341 saved_state
->save_r27
= state
->r27
;
342 saved_state
->save_r28
= state
->r28
;
343 saved_state
->save_r29
= state
->r29
;
344 saved_state
->save_r30
= state
->r30
;
345 saved_state
->save_r31
= state
->r31
;
346 saved_state
->save_cr
= state
->cr
;
347 saved_state
->save_xer
= state
->xer
;
348 saved_state
->save_lr
= state
->lr
;
349 saved_state
->save_ctr
= state
->ctr
;
353 kdp_machine_write_regs(
362 case PPC_THREAD_STATE
:
363 dprintf(("kdp_writeregs THREAD_STATE\n"));
364 kdp_setintegerstate((struct ppc_thread_state
*)data
);
367 DumpTheSave((struct savearea
*)data
); /* (TEST/DEBUG) */
369 return KDPERR_NO_ERROR
;
371 case PPC_THREAD_STATE64
:
372 dprintf(("kdp_writeregs THREAD_STATE64\n"));
373 kdp_setintegerstate64((struct ppc_thread_state64
*)data
);
376 DumpTheSave((struct savearea
*)data
); /* (TEST/DEBUG) */
378 return KDPERR_NO_ERROR
;
379 case PPC_FLOAT_STATE
:
380 dprintf(("kdp_writeregs THREAD_FPSTATE\n"));
381 return KDPERR_NO_ERROR
;
384 dprintf(("kdp_writeregs bad flavor %d\n"));
385 return KDPERR_BADFLAVOR
;
390 kdp_machine_hostinfo(
391 kdp_hostinfo_t
*hostinfo
396 hostinfo
->cpus_mask
= 0;
397 hostinfo
->cpu_type
= 0;
399 for (i
= 0; i
< machine_info
.max_cpus
; i
++) {
400 if ((PerProcTable
[i
].ppe_vaddr
== (struct per_proc_info
*)NULL
) ||
401 !(PerProcTable
[i
].ppe_vaddr
->running
))
404 hostinfo
->cpus_mask
|= (1 << i
);
405 if (hostinfo
->cpu_type
== 0) {
406 hostinfo
->cpu_type
= slot_type(i
);
407 hostinfo
->cpu_subtype
= slot_subtype(i
);
417 printf("kdp panic: %s\n", msg
);
425 printf("Attempting system restart...");
426 /* Call the platform specific restart*/
428 (*PE_halt_restart
)(kPERestartCPU
);
429 /* If we do reach this, give up */
446 kdp_us_spin(int usec
)
448 extern void delay(int);
453 void print_saved_state(void *state
)
455 struct ppc_thread_state
*saved_state
;
459 printf("pc = 0x%x\n", saved_state
->srr0
);
460 printf("msr = 0x%x\n", saved_state
->srr1
);
461 printf("rp = 0x%x\n", saved_state
->lr
);
462 printf("sp = 0x%x\n", saved_state
->r1
);
469 Debugger("inline call to debugger(machine_startup)");
473 * table to convert system specific code to generic codes for kdb
475 int kdp_trap_codes
[] = {
476 EXC_BAD_ACCESS
, /* 0x0000 INVALID EXCEPTION */
477 EXC_BAD_ACCESS
, /* 0x0100 System reset */
478 EXC_BAD_ACCESS
, /* 0x0200 Machine check */
479 EXC_BAD_ACCESS
, /* 0x0300 Data access */
480 EXC_BAD_ACCESS
, /* 0x0400 Instruction access */
481 EXC_BAD_ACCESS
, /* 0x0500 External interrupt */
482 EXC_BAD_ACCESS
, /* 0x0600 Alignment */
483 EXC_BREAKPOINT
, /* 0x0700 Program - fp exc, ill/priv instr, trap */
484 EXC_ARITHMETIC
, /* 0x0800 Floating point disabled */
485 EXC_SOFTWARE
, /* 0x0900 Decrementer */
486 EXC_BAD_ACCESS
, /* 0x0A00 I/O controller interface */
487 EXC_BAD_ACCESS
, /* 0x0B00 INVALID EXCEPTION */
488 EXC_SOFTWARE
, /* 0x0C00 System call exception */
489 EXC_BREAKPOINT
, /* 0x0D00 Trace */
490 EXC_SOFTWARE
, /* 0x0E00 FP assist */
491 EXC_SOFTWARE
, /* 0x0F00 Performance monitoring */
492 EXC_ARITHMETIC
, /* 0x0F20 Altivec disabled */
493 EXC_BAD_ACCESS
, /* 0x1000 Instruction PTE miss */
494 EXC_BAD_ACCESS
, /* 0x1100 Data load PTE miss */
495 EXC_BAD_ACCESS
, /* 0x1200 Data store PTE miss */
496 EXC_BREAKPOINT
, /* 0x1300 Instruction bkpt */
497 EXC_SOFTWARE
, /* 0x1400 System management */
498 EXC_BAD_ACCESS
, /* 0x1500 INVALID EXCEPTION */
499 EXC_ARITHMETIC
, /* 0x1600 Altivec Assist */
500 EXC_BAD_ACCESS
, /* 0x1700 INVALID EXCEPTION */
501 EXC_BAD_ACCESS
, /* 0x1800 INVALID EXCEPTION */
502 EXC_BAD_ACCESS
, /* 0x1900 INVALID EXCEPTION */
503 EXC_BAD_ACCESS
, /* 0x1A00 INVALID EXCEPTION */
504 EXC_BAD_ACCESS
, /* 0x1B00 INVALID EXCEPTION */
505 EXC_BAD_ACCESS
, /* 0x1C00 INVALID EXCEPTION */
506 EXC_BAD_ACCESS
, /* 0x1D00 INVALID EXCEPTION */
507 EXC_BAD_ACCESS
, /* 0x1E00 INVALID EXCEPTION */
508 EXC_BAD_ACCESS
, /* 0x1F00 INVALID EXCEPTION */
509 EXC_BREAKPOINT
, /* 0x2000 Run Mode/Trace */
510 EXC_BAD_ACCESS
, /* 0x2100 INVALID EXCEPTION */
511 EXC_BAD_ACCESS
, /* 0x2200 INVALID EXCEPTION */
512 EXC_BAD_ACCESS
, /* 0x2300 INVALID EXCEPTION */
513 EXC_BAD_ACCESS
, /* 0x2400 INVALID EXCEPTION */
514 EXC_BAD_ACCESS
, /* 0x2500 INVALID EXCEPTION */
515 EXC_BAD_ACCESS
, /* 0x2600 INVALID EXCEPTION */
516 EXC_BAD_ACCESS
, /* 0x2700 INVALID EXCEPTION */
517 EXC_BAD_ACCESS
, /* 0x2800 INVALID EXCEPTION */
518 EXC_BAD_ACCESS
, /* 0x2900 INVALID EXCEPTION */
519 EXC_BAD_ACCESS
, /* 0x2A00 INVALID EXCEPTION */
520 EXC_BAD_ACCESS
, /* 0x2B00 INVALID EXCEPTION */
521 EXC_BAD_ACCESS
, /* 0x2C00 INVALID EXCEPTION */
522 EXC_BAD_ACCESS
, /* 0x2D00 INVALID EXCEPTION */
523 EXC_BAD_ACCESS
, /* 0x2E00 INVALID EXCEPTION */
524 EXC_BAD_ACCESS
, /* 0x2F00 INVALID EXCEPTION */
525 EXC_SOFTWARE
/* 0x3000 AST trap (software) */
539 #define kdp_code(x) kdp_trap_codes[((x)==T_AST?0x31:(x)/T_VECTOR_SIZE)]
543 unsigned int exception
,
544 struct savearea
*saved_state
549 struct savearea
*state
;
553 printf("\nvector=%x, \n", exception
/4);
554 sp
= saved_state
->save_r1
;
555 printf("stack backtrace - sp(%x) ", sp
);
556 fp
= (unsigned int *) *((unsigned int *)sp
);
558 printf("0x%08x ", fp
[2]);
559 fp
= (unsigned int *)*fp
;
565 dump_segment_registers();
569 printf("vector=%d ", exception
/4);
571 kdp_raise_exception(kdp_code(exception
), 0, 0, saved_state
);
574 printf("kdp_trap: kdp_raise_exception() ret\n");
576 if ((unsigned int)(saved_state
->save_srr0
) == 0x7c800008)
577 saved_state
->save_srr0
+= 4; /* BKPT_SIZE */
579 if(saved_state
->save_srr1
& (MASK(MSR_SE
) | MASK(MSR_BE
))) { /* Are we just stepping or continuing */
580 db_run_mode
= STEP_ONCE
; /* We are stepping */
582 else db_run_mode
= STEP_CONTINUE
; /* Otherwise we are continuing */
585 mtspr(dabr
, kdp_dabr
);
597 void kdp_print_registers(struct savearea
*state
)
600 for (i
=0; i
<32; i
++) {
603 printf(" %08x",*(&state
->save_r0
+i
));
606 printf("cr = 0x%08x\t\t",state
->save_cr
);
607 printf("xer = 0x%08x\n",state
->save_xer
);
608 printf("lr = 0x%08x\t\t",state
->save_lr
);
609 printf("ctr = 0x%08x\n",state
->save_ctr
);
610 printf("srr0(iar) = 0x%08x\t\t",state
->save_srr0
);
611 printf("srr1(msr) = 0x%08B\n",state
->save_srr1
,
612 "\x10\x11""EE\x12PR\x13""FP\x14ME\x15""FE0\x16SE\x18"
613 "FE1\x19""AL\x1a""EP\x1bIT\x1c""DT");
619 unsigned int exception
,
620 struct savearea
*saved_state
)
622 extern void kdp_print_registers(struct savearea
*);
623 extern void print_backtrace(struct savearea
*);
625 disableDebugOuput
= FALSE
;
627 printf("re-entering kdp:\n");
628 printf("vector=%x, \n", exception
/4);
629 kdp_print_registers(saved_state
);
630 print_backtrace(saved_state
);
631 printf("panic: We are hanging here...\n");
635 unsigned int kdp_ml_get_breakinsn(void)