2 * arch/ppc/kernel/traps.c
4 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Modified by Cort Dougan (cort@cs.nmt.edu)
12 * and Paul Mackerras (paulus@cs.anu.edu.au)
16 * This file handles the architecture-dependent parts of hardware exceptions
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/user.h>
28 #include <linux/a.out.h>
29 #include <linux/interrupt.h>
30 #include <linux/config.h>
31 #include <linux/init.h>
32 #include <linux/module.h>
34 #include <asm/pgtable.h>
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
40 #ifdef CONFIG_PMAC_BACKLIGHT
41 #include <asm/backlight.h>
44 extern int fix_alignment(struct pt_regs *);
45 extern void bad_page_fault(struct pt_regs *, unsigned long, int sig);
48 void (*debugger)(struct pt_regs *regs) = xmon;
49 int (*debugger_bpt)(struct pt_regs *regs) = xmon_bpt;
50 int (*debugger_sstep)(struct pt_regs *regs) = xmon_sstep;
51 int (*debugger_iabr_match)(struct pt_regs *regs) = xmon_iabr_match;
52 int (*debugger_dabr_match)(struct pt_regs *regs) = xmon_dabr_match;
53 void (*debugger_fault_handler)(struct pt_regs *regs);
56 void (*debugger)(struct pt_regs *regs);
57 int (*debugger_bpt)(struct pt_regs *regs);
58 int (*debugger_sstep)(struct pt_regs *regs);
59 int (*debugger_iabr_match)(struct pt_regs *regs);
60 int (*debugger_dabr_match)(struct pt_regs *regs);
61 void (*debugger_fault_handler)(struct pt_regs *regs);
63 #define debugger(regs) do { } while (0)
64 #define debugger_bpt(regs) 0
65 #define debugger_sstep(regs) 0
66 #define debugger_iabr_match(regs) 0
67 #define debugger_dabr_match(regs) 0
68 #define debugger_fault_handler ((void (*)(struct pt_regs *))0)
73 * Trap & Exception support
77 spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
79 void die(const char * str, struct pt_regs * fp, long err)
81 static int die_counter;
84 spin_lock_irq(&die_lock);
85 #ifdef CONFIG_PMAC_BACKLIGHT
86 set_backlight_enable(1);
87 set_backlight_level(BACKLIGHT_MAX);
89 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
95 printk("SMP NR_CPUS=%d ", NR_CPUS);
101 spin_unlock_irq(&die_lock);
102 /* do_exit() should take care of panic'ing from an interrupt
103 * context so we don't handle it here
109 _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
113 if (!user_mode(regs)) {
115 die("Exception in kernel mode", regs, signr);
117 info.si_signo = signr;
120 info.si_addr = (void *) addr;
121 force_sig_info(signr, &info, current);
125 * I/O accesses can cause machine checks on powermacs.
126 * Check if the NIP corresponds to the address of a sync
127 * instruction for which there is an entry in the exception
129 * Note that the 601 only takes a machine check on TEA
130 * (transfer error ack) signal assertion, and does not
131 * set any of the top 16 bits of SRR1.
134 static inline int check_io_access(struct pt_regs *regs)
136 #ifdef CONFIG_PPC_PMAC
137 unsigned long msr = regs->msr;
138 const struct exception_table_entry *entry;
139 unsigned int *nip = (unsigned int *)regs->nip;
141 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
142 && (entry = search_exception_tables(regs->nip)) != NULL) {
144 * Check that it's a sync instruction, or somewhere
145 * in the twi; isync; nop sequence that inb/inw/inl uses.
146 * As the address is in the exception table
147 * we should be able to read the instr there.
148 * For the debug message, we look at the preceding
151 if (*nip == 0x60000000) /* nop */
153 else if (*nip == 0x4c00012c) /* isync */
155 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
160 rb = (*nip >> 11) & 0x1f;
161 printk(KERN_DEBUG "%s bad port %lx at %p\n",
162 (*nip & 0x100)? "OUT to": "IN from",
163 regs->gpr[rb] - _IO_BASE, nip);
165 regs->nip = entry->fixup;
169 #endif /* CONFIG_PPC_PMAC */
173 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
174 /* On 4xx, the reason for the machine check or program exception
176 #define get_reason(regs) ((regs)->dsisr)
178 #define REASON_ILLEGAL ESR_PIL
179 #define REASON_PRIVILEGED ESR_PPR
180 #define REASON_TRAP ESR_PTR
182 /* single-step stuff */
183 #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
184 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
187 /* On non-4xx, the reason for the machine check or program
188 exception is in the MSR. */
189 #define get_reason(regs) ((regs)->msr)
190 #define REASON_FP 0x100000
191 #define REASON_ILLEGAL 0x80000
192 #define REASON_PRIVILEGED 0x40000
193 #define REASON_TRAP 0x20000
195 #define single_stepping(regs) ((regs)->msr & MSR_SE)
196 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
200 MachineCheckException(struct pt_regs *regs)
202 unsigned long reason = get_reason(regs);
204 if (user_mode(regs)) {
206 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
210 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
211 /* the qspan pci read routines can cause machine checks -- Cort */
212 bad_page_fault(regs, regs->dar, SIGBUS);
216 if (debugger_fault_handler) {
217 debugger_fault_handler(regs);
222 if (check_io_access(regs))
226 if (reason & ESR_IMCP) {
227 printk("Instruction");
228 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
231 printk(" machine check in kernel mode.\n");
233 #else /* !CONFIG_4xx */
234 printk("Machine check in kernel mode.\n");
235 printk("Caused by (from SRR1=%lx): ", reason);
236 switch (reason & 0x601F0000) {
238 printk("Machine check signal\n");
240 case 0: /* for 601 */
242 case 0x140000: /* 7450 MSS error and TEA */
243 printk("Transfer error ack signal\n");
246 printk("Data parity error signal\n");
249 printk("Address parity error signal\n");
252 printk("L1 Data Cache error\n");
255 printk("L1 Instruction Cache error\n");
258 printk("L2 data cache parity error\n");
261 printk("Unknown values in msr\n");
263 #endif /* CONFIG_4xx */
266 die("machine check", regs, SIGBUS);
270 SMIException(struct pt_regs *regs)
273 #if !(defined(CONFIG_XMON) || defined(CONFIG_KGDB))
275 panic("System Management Interrupt");
280 UnknownException(struct pt_regs *regs)
282 printk("Bad trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
283 regs->nip, regs->msr, regs->trap, print_tainted());
284 _exception(SIGTRAP, regs, 0, 0);
288 InstructionBreakpoint(struct pt_regs *regs)
290 if (debugger_iabr_match(regs))
292 _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
296 RunModeException(struct pt_regs *regs)
298 _exception(SIGTRAP, regs, 0, 0);
301 /* Illegal instruction emulation support. Originally written to
302 * provide the PVR to user applications using the mfspr rd, PVR.
303 * Return non-zero if we can't emulate, or EFAULT if the associated
304 * memory access caused an access fault. Return zero on success.
306 * There are a couple of ways to do this, either "decode" the instruction
307 * or directly match lots of bits. In this case, matching lots of
308 * bits is faster and easier.
311 #define INST_MFSPR_PVR 0x7c1f42a6
312 #define INST_MFSPR_PVR_MASK 0xfc1fffff
315 emulate_instruction(struct pt_regs *regs)
323 if (!user_mode(regs))
325 CHECK_FULL_REGS(regs);
327 if (get_user(instword, (u32 __user *)(regs->nip)))
330 /* Emulate the mfspr rD, PVR.
332 if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
333 rd = (instword >> 21) & 0x1f;
334 regs->gpr[rd] = mfspr(PVR);
342 * After we have successfully emulated an instruction, we have to
343 * check if the instruction was being single-stepped, and if so,
344 * pretend we got a single-step exception. This was pointed out
345 * by Kumar Gala. -- paulus
347 static void emulate_single_step(struct pt_regs *regs)
349 if (single_stepping(regs)) {
350 clear_single_step(regs);
351 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
356 * Look through the list of trap instructions that are used for BUG(),
357 * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
358 * that the exception was caused by a trap instruction of some kind.
359 * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
362 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
364 #ifndef CONFIG_MODULES
365 #define module_find_bug(x) NULL
368 static struct bug_entry *find_bug(unsigned long bugaddr)
370 struct bug_entry *bug;
372 for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
373 if (bugaddr == bug->bug_addr)
375 return module_find_bug(bugaddr);
379 check_bug_trap(struct pt_regs *regs)
381 struct bug_entry *bug;
384 if (regs->msr & MSR_PR)
385 return 0; /* not in kernel */
386 addr = regs->nip; /* address of trap instruction */
387 if (addr < PAGE_OFFSET)
389 bug = find_bug(regs->nip);
392 if (bug->line & BUG_WARNING_TRAP) {
393 /* this is a WARN_ON rather than BUG/BUG_ON */
395 xmon_printf(KERN_ERR "Badness in %s at %s:%d\n",
396 bug->function, bug->file,
397 bug->line & ~BUG_WARNING_TRAP);
398 #endif /* CONFIG_XMON */
399 printk(KERN_ERR "Badness in %s at %s:%d\n",
400 bug->function, bug->file,
401 bug->line & ~BUG_WARNING_TRAP);
406 xmon_printf(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
407 bug->function, bug->file, bug->line);
409 #endif /* CONFIG_XMON */
410 printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
411 bug->function, bug->file, bug->line);
417 ProgramCheckException(struct pt_regs *regs)
419 unsigned int reason = get_reason(regs);
420 extern int do_mathemu(struct pt_regs *regs);
422 #ifdef CONFIG_MATH_EMULATION
423 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
424 * but there seems to be a hardware bug on the 405GP (RevD)
425 * that means ESR is sometimes set incorrectly - either to
426 * ESR_DST (!?) or 0. In the process of chasing this with the
427 * hardware people - not sure if it can happen on any illegal
428 * instruction or only on FP instructions, whether there is a
429 * pattern to occurences etc. -dgibson 31/Mar/2003 */
430 if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
431 emulate_single_step(regs);
434 #endif /* CONFIG_MATH_EMULATION */
436 if (reason & REASON_FP) {
437 /* IEEE FP exception */
441 /* We must make sure the FP state is consistent with
445 if (regs->msr & MSR_FP)
449 fpscr = current->thread.fpscr;
450 fpscr &= fpscr << 22; /* mask summary bits with enables */
451 if (fpscr & FPSCR_VX)
453 else if (fpscr & FPSCR_OX)
455 else if (fpscr & FPSCR_UX)
457 else if (fpscr & FPSCR_ZX)
459 else if (fpscr & FPSCR_XX)
461 _exception(SIGFPE, regs, code, regs->nip);
465 if (reason & REASON_TRAP) {
467 if (debugger_bpt(regs))
469 if (check_bug_trap(regs)) {
473 _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
477 if (reason & REASON_PRIVILEGED) {
478 /* Try to emulate it if we should. */
479 if (emulate_instruction(regs) == 0) {
480 emulate_single_step(regs);
483 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
487 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
491 SingleStepException(struct pt_regs *regs)
493 regs->msr &= ~MSR_SE; /* Turn off 'trace' bit */
494 if (debugger_sstep(regs))
496 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
500 AlignmentException(struct pt_regs *regs)
504 fixed = fix_alignment(regs);
506 regs->nip += 4; /* skip over emulated instruction */
509 if (fixed == -EFAULT) {
510 /* fixed == -EFAULT means the operand address was bad */
512 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
514 bad_page_fault(regs, regs->dar, SIGSEGV);
517 _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
521 StackOverflow(struct pt_regs *regs)
523 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
524 current, regs->gpr[1]);
527 panic("kernel stack overflow");
530 void nonrecoverable_exception(struct pt_regs *regs)
532 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
533 regs->nip, regs->msr);
535 die("nonrecoverable exception", regs, SIGKILL);
539 trace_syscall(struct pt_regs *regs)
541 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
542 current, current->pid, regs->nip, regs->link, regs->gpr[0],
543 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
548 SoftwareEmulation(struct pt_regs *regs)
550 extern int do_mathemu(struct pt_regs *);
551 extern int Soft_emulate_8xx(struct pt_regs *);
554 CHECK_FULL_REGS(regs);
556 if (!user_mode(regs)) {
558 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
561 #ifdef CONFIG_MATH_EMULATION
562 errcode = do_mathemu(regs);
564 errcode = Soft_emulate_8xx(regs);
568 _exception(SIGFPE, regs, 0, 0);
569 else if (errcode == -EFAULT)
570 _exception(SIGSEGV, regs, 0, 0);
572 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
574 emulate_single_step(regs);
576 #endif /* CONFIG_8xx */
578 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
580 void DebugException(struct pt_regs *regs, unsigned long debug_status)
583 if (debug_status & DBSR_TIE) { /* trap instruction*/
584 if (!user_mode(regs) && debugger_bpt(regs))
586 _exception(SIGTRAP, regs, 0, 0);
590 if (debug_status & DBSR_IC) { /* instruction completion */
591 if (!user_mode(regs) && debugger_sstep(regs))
593 current->thread.dbcr0 &= ~DBCR0_IC;
594 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
597 #endif /* CONFIG_4xx || CONFIG_BOOKE */
599 #if !defined(CONFIG_TAU_INT)
601 TAUException(struct pt_regs *regs)
603 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
604 regs->nip, regs->msr, regs->trap, print_tainted());
606 #endif /* CONFIG_INT_TAU */
608 #ifdef CONFIG_ALTIVEC
610 AltivecAssistException(struct pt_regs *regs)
613 if (regs->msr & MSR_VEC)
614 giveup_altivec(current);
617 /* XXX quick hack for now: set the non-Java bit in the VSCR */
618 current->thread.vscr.u[3] |= 0x10000;
620 #endif /* CONFIG_ALTIVEC */
623 void __init trap_init(void)