2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
30 #include <kern/thread.h>
31 #include <kern/exception.h>
32 #include <kern/syscall_sw.h>
33 #include <kern/cpu_data.h>
34 #include <kern/debug.h>
35 #include <mach/thread_status.h>
36 #include <vm/vm_fault.h>
37 #include <vm/vm_kern.h> /* For kernel_map */
38 #include <ppc/misc_protos.h>
40 #include <ppc/exception.h>
41 #include <ppc/proc_reg.h> /* for SR_xxx definitions */
44 #include <ppc/Firmware.h>
45 #include <ppc/low_trace.h>
47 #include <sys/kdebug.h>
49 perfTrap perfTrapHook
= 0; /* Pointer to performance trap hook routine */
52 #include <ddb/db_watch.h>
53 #include <ddb/db_run.h>
54 #include <ddb/db_break.h>
55 #include <ddb/db_trap.h>
57 boolean_t let_ddb_vm_fault
= FALSE
;
58 boolean_t debug_all_traps_with_kdb
= FALSE
;
59 extern struct db_watchpoint
*db_watchpoint_list
;
60 extern boolean_t db_watchpoints_inserted
;
61 extern boolean_t db_breakpoints_inserted
;
67 extern int debugger_active
[NCPUS
];
68 extern task_t bsd_init_task
;
69 extern char init_task_failure_data
[];
72 #define PROT_EXEC (VM_PROT_EXECUTE)
73 #define PROT_RO (VM_PROT_READ)
74 #define PROT_RW (VM_PROT_READ|VM_PROT_WRITE)
76 /* A useful macro to update the ppc_exception_state in the PCB
77 * before calling doexception
79 #define UPDATE_PPC_EXCEPTION_STATE { \
80 thread_act_t thr_act = current_act(); \
81 thr_act->mact.pcb->save_dar = dar; \
82 thr_act->mact.pcb->save_dsisr = dsisr; \
83 thr_act->mact.pcb->save_exception = trapno / T_VECTOR_SIZE; /* back to powerpc */ \
86 static void unresolved_kernel_trap(int trapno
,
92 struct savearea
*trap(int trapno
,
102 unsigned int space
, space2
;
104 thread_act_t thr_act
;
108 #endif /* MACH_BSD */
110 if(perfTrapHook
) { /* Is there a hook? */
111 if(perfTrapHook(trapno
, ssp
, dsisr
, dar
) == KERN_SUCCESS
) return ssp
; /* If it succeeds, we are done... */
116 extern void fctx_text(void);
121 thr_act
= current_act(); /* Get current activation */
122 exception
= 0; /* Clear exception for now */
125 * Remember that we are disabled for interruptions when we come in here. Because
126 * of latency concerns, we need to enable interruptions in the interrupted process
127 * was enabled itself as soon as we can.
130 intr
= (ssp
->save_srr1
& MASK(MSR_EE
)) != 0; /* Remember if we were enabled */
132 /* Handle kernel traps first */
134 if (!USER_MODE(ssp
->save_srr1
)) {
136 * Trap came from kernel
140 case T_PREEMPT
: /* Handle a preempt trap */
141 ast_taken(AST_PREEMPT
, FALSE
);
144 case T_RESET
: /* Reset interruption */
146 kprintf("*** Reset exception ignored; srr0 = %08X, srr1 = %08X\n",
147 ssp
->save_srr0
, ssp
->save_srr1
);
149 panic("Unexpected Reset exception; srr0 = %08X, srr1 = %08X\n",
150 ssp
->save_srr0
, ssp
->save_srr1
);
152 break; /* We just ignore these */
155 * These trap types should never be seen by trap()
156 * in kernel mode, anyway.
157 * Some are interrupts that should be seen by
158 * interrupt() others just don't happen because they
159 * are handled elsewhere. Some could happen but are
160 * considered to be fatal in kernel mode.
163 case T_IN_VAIN
: /* Shouldn't ever see this, lowmem_vectors eats it */
164 case T_MACHINE_CHECK
:
165 case T_SYSTEM_MANAGEMENT
:
166 case T_ALTIVEC_ASSIST
:
168 case T_FP_UNAVAILABLE
:
173 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, NULL
);
177 case T_RUNMODE_TRACE
:
178 case T_INSTRUCTION_BKPT
:
179 if (!Call_Debugger(trapno
, ssp
))
180 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, NULL
);
184 if (ssp
->save_srr1
& MASK(SRR1_PRG_TRAP
)) {
185 if (!Call_Debugger(trapno
, ssp
))
186 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, NULL
);
188 unresolved_kernel_trap(trapno
, ssp
,
196 mp_disable_preemption();
198 && debugger_active
[cpu_number()]
199 && !let_ddb_vm_fault
) {
201 * Force kdb to handle this one.
203 kdb_trap(trapno
, ssp
);
205 mp_enable_preemption();
206 #endif /* MACH_KDB */
208 if(intr
) ml_set_interrupts_enabled(TRUE
); /* Enable if we were */
210 /* simple case : not SR_COPYIN segment, from kernel */
211 if ((dar
>> 28) != SR_COPYIN_NUM
) {
218 * Note: Some ROM device drivers will access page 0 when they start. The IOKit will
219 * set a flag to tell us to ignore any access fault on page 0. After the driver is
220 * opened, it will clear the flag.
222 if((0 == (dar
& -PAGE_SIZE
)) && /* Check for access of page 0 and */
223 ((thr_act
->mact
.specFlags
) & ignoreZeroFault
)) {
224 /* special case of ignoring page zero faults */
225 ssp
->save_srr0
+= 4; /* Point to next instruction */
229 code
= vm_fault(map
, trunc_page(offset
),
230 dsisr
& MASK(DSISR_WRITE
) ? PROT_RW
: PROT_RO
,
231 FALSE
, THREAD_UNINT
, NULL
, 0);
233 if (code
!= KERN_SUCCESS
) {
234 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, NULL
);
236 ssp
->save_hdr
.save_flags
|= SAVredrive
; /* Tell low-level to re-try fault */
237 ssp
->save_dsisr
|= MASK(DSISR_HASH
); /* Make sure this is marked as a miss */
242 /* If we get here, the fault was due to a copyin/out */
246 /* Mask out SR_COPYIN and mask in original segment */
248 offset
= (dar
& 0x0fffffff) |
249 ((mfsrin(dar
)<<8) & 0xF0000000);
251 code
= vm_fault(map
, trunc_page(offset
),
252 dsisr
& MASK(DSISR_WRITE
) ? PROT_RW
: PROT_RO
,
253 FALSE
, THREAD_UNINT
, NULL
, 0);
255 /* If we failed, there should be a recovery
258 if (code
!= KERN_SUCCESS
) {
260 if (thr_act
->thread
->recover
) {
262 act_lock_thread(thr_act
);
263 ssp
->save_srr0
= thr_act
->thread
->recover
;
264 thr_act
->thread
->recover
=
266 act_unlock_thread(thr_act
);
268 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, "copyin/out has no recovery point");
272 ssp
->save_hdr
.save_flags
|= SAVredrive
; /* Tell low-level to re-try fault */
273 ssp
->save_dsisr
|= MASK(DSISR_HASH
); /* Make sure this is marked as a miss */
278 case T_INSTRUCTION_ACCESS
:
282 && debugger_active
[cpu_number()]
283 && !let_ddb_vm_fault
) {
285 * Force kdb to handle this one.
287 kdb_trap(trapno
, ssp
);
289 #endif /* MACH_KDB */
291 /* Same as for data access, except fault type
292 * is PROT_EXEC and addr comes from srr0
295 if(intr
) ml_set_interrupts_enabled(TRUE
); /* Enable if we were */
299 code
= vm_fault(map
, trunc_page(ssp
->save_srr0
),
300 PROT_EXEC
, FALSE
, THREAD_UNINT
, NULL
, 0);
302 if (code
!= KERN_SUCCESS
) {
303 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, NULL
);
305 ssp
->save_hdr
.save_flags
|= SAVredrive
; /* Tell low-level to re-try fault */
306 ssp
->save_srr1
|= MASK(DSISR_HASH
); /* Make sure this is marked as a miss */
310 /* Usually shandler handles all the system calls, but the
311 * atomic thread switcher may throwup (via thandler) and
312 * have to pass it up to the exception handler.
316 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, NULL
);
320 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, NULL
);
325 ml_set_interrupts_enabled(TRUE
); /* Processing for user state traps is always enabled */
329 void get_procrustime(time_value_t
*);
331 get_procrustime(&tv
);
333 #endif /* MACH_BSD */
337 * Trap came from user task
343 unresolved_kernel_trap(trapno
, ssp
, dsisr
, dar
, NULL
);
347 * These trap types should never be seen by trap()
348 * Some are interrupts that should be seen by
349 * interrupt() others just don't happen because they
350 * are handled elsewhere.
353 case T_IN_VAIN
: /* Shouldn't ever see this, lowmem_vectors eats it */
354 case T_MACHINE_CHECK
:
356 case T_FP_UNAVAILABLE
:
357 case T_SYSTEM_MANAGEMENT
:
363 ml_set_interrupts_enabled(FALSE
); /* Turn off interruptions */
365 panic("Unexpected user state trap(cpu %d): 0x%08x DSISR=0x%08x DAR=0x%08x PC=0x%08x, MSR=0x%08x\n",
366 cpu_number(), trapno
, dsisr
, dar
, ssp
->save_srr0
, ssp
->save_srr1
);
371 kprintf("*** Reset exception ignored; srr0 = %08X, srr1 = %08X\n",
372 ssp
->save_srr0
, ssp
->save_srr1
);
374 panic("Unexpected Reset exception: srr0 = %0x08x, srr1 = %0x08x\n",
375 ssp
->save_srr0
, ssp
->save_srr1
);
377 break; /* We just ignore these */
381 * If notifyUnaligned is set, we have actually already emulated the unaligned access.
382 * All that we want to do here is to ignore the interrupt. This is to allow logging or
383 * tracing of unaligned accesses. Note that if trapUnaligned is also set, it takes
384 * precedence and we will take a bad access fault.
387 if(thr_act
->mact
.specFlags
& notifyUnalign
) {
389 KERNEL_DEBUG_CONSTANT(
390 MACHDBG_CODE(DBG_MACH_EXCP_ALNG
, 0) | DBG_FUNC_NONE
,
391 (int)ssp
->save_srr0
, (int)dar
, (int)dsisr
, (int)ssp
->save_lr
, 0);
394 if((!(thr_act
->mact
.specFlags
& notifyUnalign
)) || (thr_act
->mact
.specFlags
& trapUnalign
)) {
395 code
= EXC_PPC_UNALIGNED
;
396 exception
= EXC_BAD_ACCESS
;
401 case T_TRACE
: /* Real PPC chips */
408 case T_INSTRUCTION_BKPT
: /* 603 PPC chips */
409 case T_RUNMODE_TRACE
: /* 601 PPC chips */
410 exception
= EXC_BREAKPOINT
;
411 code
= EXC_PPC_TRACE
;
412 subcode
= ssp
->save_srr0
;
416 if (ssp
->save_srr1
& MASK(SRR1_PRG_FE
)) {
417 fpu_save(thr_act
->mact
.curctx
);
418 UPDATE_PPC_EXCEPTION_STATE
;
419 exception
= EXC_ARITHMETIC
;
420 code
= EXC_ARITHMETIC
;
422 mp_disable_preemption();
423 subcode
= ssp
->save_fpscr
;
424 mp_enable_preemption();
426 else if (ssp
->save_srr1
& MASK(SRR1_PRG_ILL_INS
)) {
428 UPDATE_PPC_EXCEPTION_STATE
429 exception
= EXC_BAD_INSTRUCTION
;
430 code
= EXC_PPC_UNIPL_INST
;
431 subcode
= ssp
->save_srr0
;
432 } else if (ssp
->save_srr1
& MASK(SRR1_PRG_PRV_INS
)) {
434 UPDATE_PPC_EXCEPTION_STATE
;
435 exception
= EXC_BAD_INSTRUCTION
;
436 code
= EXC_PPC_PRIVINST
;
437 subcode
= ssp
->save_srr0
;
438 } else if (ssp
->save_srr1
& MASK(SRR1_PRG_TRAP
)) {
441 if (copyin((char *) ssp
->save_srr0
, (char *) &inst
, 4 ))
442 panic("copyin failed\n");
443 UPDATE_PPC_EXCEPTION_STATE
;
444 if (inst
== 0x7FE00008) {
445 exception
= EXC_BREAKPOINT
;
446 code
= EXC_PPC_BREAKPOINT
;
448 exception
= EXC_SOFTWARE
;
451 subcode
= ssp
->save_srr0
;
455 case T_ALTIVEC_ASSIST
:
456 UPDATE_PPC_EXCEPTION_STATE
;
457 exception
= EXC_ARITHMETIC
;
458 code
= EXC_PPC_ALTIVECASSIST
;
459 subcode
= ssp
->save_srr0
;
465 code
= vm_fault(map
, trunc_page(dar
),
466 dsisr
& MASK(DSISR_WRITE
) ? PROT_RW
: PROT_RO
,
467 FALSE
, THREAD_ABORTSAFE
, NULL
, 0);
469 if ((code
!= KERN_SUCCESS
) && (code
!= KERN_ABORTED
)) {
470 UPDATE_PPC_EXCEPTION_STATE
;
471 exception
= EXC_BAD_ACCESS
;
474 ssp
->save_hdr
.save_flags
|= SAVredrive
; /* Tell low-level to re-try fault */
475 ssp
->save_dsisr
|= MASK(DSISR_HASH
); /* Make sure this is marked as a miss */
479 case T_INSTRUCTION_ACCESS
:
480 /* Same as for data access, except fault type
481 * is PROT_EXEC and addr comes from srr0
485 code
= vm_fault(map
, trunc_page(ssp
->save_srr0
),
486 PROT_EXEC
, FALSE
, THREAD_ABORTSAFE
, NULL
, 0);
488 if ((code
!= KERN_SUCCESS
) && (code
!= KERN_ABORTED
)) {
489 UPDATE_PPC_EXCEPTION_STATE
;
490 exception
= EXC_BAD_ACCESS
;
491 subcode
= ssp
->save_srr0
;
493 ssp
->save_hdr
.save_flags
|= SAVredrive
; /* Tell low-level to re-try fault */
494 ssp
->save_srr1
|= MASK(DSISR_HASH
); /* Make sure this is marked as a miss */
499 ml_set_interrupts_enabled(FALSE
);
500 ast_taken(AST_ALL
, intr
);
506 void bsd_uprofil(time_value_t
*, unsigned int);
508 bsd_uprofil(&tv
, ssp
->save_srr0
);
510 #endif /* MACH_BSD */
514 /* if this is the init task, save the exception information */
515 /* this probably is a fatal exception */
516 if(bsd_init_task
== current_task()) {
520 buf
= init_task_failure_data
;
523 buf
+= sprintf(buf
, "Exception Code = 0x%x, Subcode = 0x%x\n", code
, subcode
);
524 buf
+= sprintf(buf
, "DSISR = 0x%08x, DAR = 0x%08x\n"
527 for (i
=0; i
<32; i
++) {
529 buf
+= sprintf(buf
, "\n%4d :",i
);
531 buf
+= sprintf(buf
, " %08x",*(&ssp
->save_r0
+i
));
534 buf
+= sprintf(buf
, "\n\n");
535 buf
+= sprintf(buf
, "cr = 0x%08x\t\t",ssp
->save_cr
);
536 buf
+= sprintf(buf
, "xer = 0x%08x\n",ssp
->save_xer
);
537 buf
+= sprintf(buf
, "lr = 0x%08x\t\t",ssp
->save_lr
);
538 buf
+= sprintf(buf
, "ctr = 0x%08x\n",ssp
->save_ctr
);
539 buf
+= sprintf(buf
, "srr0(iar) = 0x%08x\t\t",ssp
->save_srr0
);
540 buf
+= sprintf(buf
, "srr1(msr) = 0x%08B\n",ssp
->save_srr1
,
541 "\x10\x11""EE\x12PR\x13""FP\x14ME\x15""FE0\x16SE\x18"
542 "FE1\x19""AL\x1a""EP\x1bIT\x1c""DT");
543 buf
+= sprintf(buf
, "\n\n");
545 /* generate some stack trace */
546 buf
+= sprintf(buf
, "Application level back trace:\n");
547 if (ssp
->save_srr1
& MASK(MSR_PR
)) {
548 char *addr
= (char*)ssp
->save_r1
;
549 unsigned int stack_buf
[3];
550 for (i
= 0; i
< 8; i
++) {
551 if (addr
== (char*)NULL
)
553 if (!copyin(addr
,(char*)stack_buf
,
555 buf
+= sprintf(buf
, "0x%08x : 0x%08x\n"
557 addr
= (char*)stack_buf
[0];
565 doexception(exception
, code
, subcode
);
568 * Check to see if we need an AST, if so take care of it here
570 ml_set_interrupts_enabled(FALSE
);
571 if (USER_MODE(ssp
->save_srr1
))
572 while (ast_needed(cpu_number())) {
573 ast_taken(AST_ALL
, intr
);
574 ml_set_interrupts_enabled(FALSE
);
580 /* This routine is called from assembly before each and every system call.
581 * It must preserve r3.
584 extern int syscall_trace(int, struct savearea
*);
589 int syscall_trace(int retval
, struct savearea
*ssp
)
594 /* Always prepare to trace mach system calls */
595 if (kdebug_enable
&& (ssp
->save_r0
& 0x80000000)) {
600 argc
= mach_trap_table
[-(ssp
->save_r0
)].mach_trap_arg_count
;
603 for (i
=0; i
< argc
; i
++)
604 kdarg
[i
] = (int)*(&ssp
->save_r3
+ i
);
605 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC
, (-(ssp
->save_r0
))) | DBG_FUNC_START
,
606 kdarg
[0], kdarg
[1], kdarg
[2], 0, 0);
612 /* This routine is called from assembly after each mach system call
613 * It must preserve r3.
616 extern int syscall_trace_end(int, struct savearea
*);
618 int syscall_trace_end(int retval
, struct savearea
*ssp
)
620 if (kdebug_enable
&& (ssp
->save_r0
& 0x80000000)) {
622 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC
,(-(ssp
->save_r0
))) | DBG_FUNC_END
,
629 * called from syscall if there is an error
636 struct savearea
*ssp
)
638 register thread_t thread
;
640 thread
= current_thread();
643 panic("syscall error in boot phase");
645 if (!USER_MODE(ssp
->save_srr1
))
646 panic("system call called from kernel");
648 doexception(exception
, code
, subcode
);
653 /* Pass up a server syscall/exception */
660 exception_data_type_t codes
[EXCEPTION_CODE_MAX
];
664 exception(exc
, codes
, 2);
667 char *trap_type
[] = {
669 "0x100 - System reset",
670 "0x200 - Machine check",
671 "0x300 - Data access",
672 "0x400 - Inst access",
676 "0x800 - Floating point",
677 "0x900 - Decrementer",
680 "0xC00 - System call",
688 "0x1300 - Inst bkpnt",
690 "0x1600 - Altivec Assist",
701 "0x2000 - Run Mode/Trace",
708 int TRAP_TYPES
= sizeof (trap_type
) / sizeof (trap_type
[0]);
710 void unresolved_kernel_trap(int trapno
,
711 struct savearea
*ssp
,
717 extern void print_backtrace(struct savearea
*);
718 extern unsigned int debug_mode
, disableDebugOuput
;
720 ml_set_interrupts_enabled(FALSE
); /* Turn off interruptions */
721 lastTrace
= LLTraceSet(0); /* Disable low-level tracing */
723 if( logPanicDataToScreen
)
724 disableDebugOuput
= FALSE
;
727 if ((unsigned)trapno
<= T_MAX
)
728 trap_name
= trap_type
[trapno
/ T_VECTOR_SIZE
];
730 trap_name
= "???? unrecognized exception";
734 kdb_printf("\n\nUnresolved kernel trap(cpu %d): %s DAR=0x%08x PC=0x%08x\n",
735 cpu_number(), trap_name
, dar
, ssp
->save_srr0
);
737 print_backtrace(ssp
);
742 (void *)Call_Debugger(trapno
, ssp
);
747 thread_syscall_return(
750 register thread_act_t thr_act
= current_act();
751 register struct savearea
*regs
= USER_REGS(thr_act
);
753 if (kdebug_enable
&& (regs
->save_r0
& 0x80000000)) {
755 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_EXCP_SC
,(-(regs
->save_r0
))) | DBG_FUNC_END
,
760 thread_exception_return();
767 thread_kdb_return(void)
769 register thread_act_t thr_act
= current_act();
770 register thread_t cur_thr
= current_thread();
771 register struct savearea
*regs
= USER_REGS(thr_act
);
773 Call_Debugger(thr_act
->mact
.pcb
->save_exception
, regs
);
775 assert(cur_thr
->mutex_count
== 0);
776 #endif /* MACH_LDEBUG */
777 check_simple_locks();
778 thread_exception_return();
781 #endif /* MACH_KDB */