2 * linux/arch/ppc64/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/config.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
24 #include <linux/stddef.h>
25 #include <linux/unistd.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/init.h>
31 #include <linux/module.h>
33 #include <asm/pgtable.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
37 #include <asm/processor.h>
38 #include <asm/ppcdebug.h>
40 #ifdef CONFIG_PPC_PSERIES
41 /* This is true if we are using the firmware NMI handler (typically LPAR) */
42 extern int fwnmi_active;
45 #ifdef CONFIG_DEBUGGER
46 int (*__debugger)(struct pt_regs *regs);
47 int (*__debugger_bpt)(struct pt_regs *regs);
48 int (*__debugger_sstep)(struct pt_regs *regs);
49 int (*__debugger_iabr_match)(struct pt_regs *regs);
50 int (*__debugger_dabr_match)(struct pt_regs *regs);
51 int (*__debugger_fault_handler)(struct pt_regs *regs);
53 EXPORT_SYMBOL(__debugger);
54 EXPORT_SYMBOL(__debugger_bpt);
55 EXPORT_SYMBOL(__debugger_sstep);
56 EXPORT_SYMBOL(__debugger_iabr_match);
57 EXPORT_SYMBOL(__debugger_dabr_match);
58 EXPORT_SYMBOL(__debugger_fault_handler);
62 * Trap & Exception support
65 static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
67 int die(const char *str, struct pt_regs *regs, long err)
69 static int die_counter;
72 if (debugger_fault_handler(regs))
79 spin_lock_irq(&die_lock);
81 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
87 printk("SMP NR_CPUS=%d ", NR_CPUS);
90 #ifdef CONFIG_DEBUG_PAGEALLOC
91 printk("DEBUG_PAGEALLOC ");
98 switch(systemcfg->platform) {
99 case PLATFORM_PSERIES:
103 case PLATFORM_PSERIES_LPAR:
104 printk("PSERIES LPAR ");
107 case PLATFORM_ISERIES_LPAR:
108 printk("ISERIES LPAR ");
111 case PLATFORM_POWERMAC:
120 spin_unlock_irq(&die_lock);
123 panic("Fatal exception in interrupt");
126 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
127 set_current_state(TASK_UNINTERRUPTIBLE);
128 schedule_timeout(5 * HZ);
129 panic("Fatal exception");
137 _exception(int signr, siginfo_t *info, struct pt_regs *regs)
139 if (!user_mode(regs)) {
140 if (die("Exception in kernel mode", regs, signr))
144 force_sig_info(signr, info, current);
147 #ifdef CONFIG_PPC_PSERIES
148 /* Get the error information for errors coming through the
149 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
150 * the actual r3 if possible, and a ptr to the error log entry
151 * will be returned if found.
153 static struct rtas_error_log *FWNMI_get_errinfo(struct pt_regs *regs)
155 unsigned long errdata = regs->gpr[3];
156 struct rtas_error_log *errhdr = NULL;
157 unsigned long *savep;
159 if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
160 (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
161 savep = __va(errdata);
162 regs->gpr[3] = savep[0]; /* restore original r3 */
163 errhdr = (struct rtas_error_log *)(savep + 1);
165 printk("FWNMI: corrupt r3\n");
170 /* Call this when done with the data returned by FWNMI_get_errinfo.
171 * It will release the saved data area for other CPUs in the
172 * partition to receive FWNMI errors.
174 static void FWNMI_release_errinfo(void)
176 unsigned long ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
178 printk("FWNMI: nmi-interlock failed: %ld\n", ret);
183 SystemResetException(struct pt_regs *regs)
185 #ifdef CONFIG_PPC_PSERIES
187 struct rtas_error_log *errhdr = FWNMI_get_errinfo(regs);
189 /* XXX Should look at FWNMI information */
191 FWNMI_release_errinfo();
195 die("System Reset", regs, 0);
197 /* Must die if the interrupt is not recoverable */
198 if (!(regs->msr & MSR_RI))
199 panic("Unrecoverable System Reset");
201 /* What should we do here? We could issue a shutdown or hard reset. */
204 #ifdef CONFIG_PPC_PSERIES
206 * See if we can recover from a machine check exception.
207 * This is only called on power4 (or above) and only via
208 * the Firmware Non-Maskable Interrupts (fwnmi) handler
209 * which provides the error analysis for us.
211 * Return 1 if corrected (or delivered a signal).
212 * Return 0 if there is nothing we can do.
214 static int recover_mce(struct pt_regs *regs, struct rtas_error_log err)
218 if (err.disposition == DISP_FULLY_RECOVERED) {
219 /* Platform corrected itself */
221 } else if ((regs->msr & MSR_RI) &&
223 err.severity == SEVERITY_ERROR_SYNC &&
224 err.disposition == DISP_NOT_RECOVERED &&
225 err.target == TARGET_MEMORY &&
226 err.type == TYPE_ECC_UNCORR &&
227 !(current->pid == 0 || current->pid == 1)) {
228 /* Kill off a user process with an ECC error */
229 info.si_signo = SIGBUS;
231 /* XXX something better for ECC error? */
232 info.si_code = BUS_ADRERR;
233 info.si_addr = (void *)regs->nip;
234 printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
236 _exception(SIGBUS, &info, regs);
244 * Handle a machine check.
246 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
247 * should be present. If so the handler which called us tells us if the
248 * error was recovered (never true if RI=0).
250 * On hardware prior to Power 4 these exceptions were asynchronous which
251 * means we can't tell exactly where it occurred and so we can't recover.
254 MachineCheckException(struct pt_regs *regs)
256 #ifdef CONFIG_PPC_PSERIES
257 struct rtas_error_log err, *errp;
260 errp = FWNMI_get_errinfo(regs);
263 FWNMI_release_errinfo(); /* frees errp */
264 if (errp && recover_mce(regs, err))
269 die("Machine check", regs, 0);
271 /* Must die if the interrupt is not recoverable */
272 if (!(regs->msr & MSR_RI))
273 panic("Unrecoverable Machine check");
277 UnknownException(struct pt_regs *regs)
281 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
282 regs->nip, regs->msr, regs->trap);
284 info.si_signo = SIGTRAP;
288 _exception(SIGTRAP, &info, regs);
292 InstructionBreakpointException(struct pt_regs *regs)
296 if (debugger_iabr_match(regs))
298 info.si_signo = SIGTRAP;
300 info.si_code = TRAP_BRKPT;
301 info.si_addr = (void *)regs->nip;
302 _exception(SIGTRAP, &info, regs);
305 static void parse_fpe(struct pt_regs *regs)
310 if (regs->msr & MSR_FP)
313 fpscr = current->thread.fpscr;
315 /* Invalid operation */
316 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
317 info.si_code = FPE_FLTINV;
320 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
321 info.si_code = FPE_FLTOVF;
324 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
325 info.si_code = FPE_FLTUND;
328 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
329 info.si_code = FPE_FLTDIV;
332 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
333 info.si_code = FPE_FLTRES;
338 info.si_signo = SIGFPE;
340 info.si_addr = (void *)regs->nip;
341 _exception(SIGFPE, &info, regs);
345 * Look through the list of trap instructions that are used for BUG(),
346 * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
347 * that the exception was caused by a trap instruction of some kind.
348 * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
351 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
353 #ifndef CONFIG_MODULES
354 #define module_find_bug(x) NULL
357 static struct bug_entry *find_bug(unsigned long bugaddr)
359 struct bug_entry *bug;
361 for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
362 if (bugaddr == bug->bug_addr)
364 return module_find_bug(bugaddr);
368 check_bug_trap(struct pt_regs *regs)
370 struct bug_entry *bug;
373 if (regs->msr & MSR_PR)
374 return 0; /* not in kernel */
375 addr = regs->nip; /* address of trap instruction */
376 if (addr < PAGE_OFFSET)
378 bug = find_bug(regs->nip);
381 if (bug->line & BUG_WARNING_TRAP) {
382 /* this is a WARN_ON rather than BUG/BUG_ON */
383 printk(KERN_ERR "Badness in %s at %s:%d\n",
384 bug->function, bug->file,
385 (unsigned int)bug->line & ~BUG_WARNING_TRAP);
386 show_stack(current, (void *)regs->gpr[1]);
389 printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
390 bug->function, bug->file, (unsigned int)bug->line);
395 ProgramCheckException(struct pt_regs *regs)
399 if (regs->msr & 0x100000) {
400 /* IEEE FP exception */
403 } else if (regs->msr & 0x40000) {
404 /* Privileged instruction */
406 info.si_signo = SIGILL;
408 info.si_code = ILL_PRVOPC;
409 info.si_addr = (void *)regs->nip;
410 _exception(SIGILL, &info, regs);
411 } else if (regs->msr & 0x20000) {
414 if (debugger_bpt(regs))
417 if (check_bug_trap(regs)) {
421 info.si_signo = SIGTRAP;
423 info.si_code = TRAP_BRKPT;
424 info.si_addr = (void *)regs->nip;
425 _exception(SIGTRAP, &info, regs);
427 /* Illegal instruction */
429 info.si_signo = SIGILL;
431 info.si_code = ILL_ILLTRP;
432 info.si_addr = (void *)regs->nip;
433 _exception(SIGILL, &info, regs);
437 void KernelFPUnavailableException(struct pt_regs *regs)
439 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
440 "%lx at %lx\n", regs->trap, regs->nip);
441 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
444 void KernelAltivecUnavailableException(struct pt_regs *regs)
446 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
447 "%lx at %lx\n", regs->trap, regs->nip);
448 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
452 SingleStepException(struct pt_regs *regs)
456 regs->msr &= ~MSR_SE; /* Turn off 'trace' bit */
458 if (debugger_sstep(regs))
461 info.si_signo = SIGTRAP;
463 info.si_code = TRAP_TRACE;
464 info.si_addr = (void *)regs->nip;
465 _exception(SIGTRAP, &info, regs);
468 static void dummy_perf(struct pt_regs *regs)
472 void (*perf_irq)(struct pt_regs *) = dummy_perf;
475 PerformanceMonitorException(struct pt_regs *regs)
481 AlignmentException(struct pt_regs *regs)
486 fixed = fix_alignment(regs);
489 if (!user_mode(regs))
490 PPCDBG(PPCDBG_ALIGNFIXUP, "fix alignment at %lx\n",
492 regs->nip += 4; /* skip over emulated instruction */
496 /* Operand address was bad */
497 if (fixed == -EFAULT) {
498 if (user_mode(regs)) {
499 info.si_signo = SIGSEGV;
501 info.si_code = SEGV_MAPERR;
502 info.si_addr = (void *)regs->dar;
503 force_sig_info(SIGSEGV, &info, current);
505 /* Search exception table */
506 bad_page_fault(regs, regs->dar, SIGSEGV);
512 info.si_signo = SIGBUS;
514 info.si_code = BUS_ADRALN;
515 info.si_addr = (void *)regs->nip;
516 _exception(SIGBUS, &info, regs);
519 #ifdef CONFIG_ALTIVEC
521 AltivecAssistException(struct pt_regs *regs)
523 if (regs->msr & MSR_VEC)
524 giveup_altivec(current);
525 /* XXX quick hack for now: set the non-Java bit in the VSCR */
526 current->thread.vscr.u[3] |= 0x10000;
528 #endif /* CONFIG_ALTIVEC */
531 * We enter here if we get an unrecoverable exception, that is, one
532 * that happened at a point where the RI (recoverable interrupt) bit
533 * in the MSR is 0. This indicates that SRR0/1 are live, and that
534 * we therefore lost state by taking this exception.
536 void unrecoverable_exception(struct pt_regs *regs)
538 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
539 regs->trap, regs->nip);
540 die("Unrecoverable exception", regs, SIGABRT);
544 * We enter here if we discover during exception entry that we are
545 * running in supervisor mode with a userspace value in the stack pointer.
547 void kernel_bad_stack(struct pt_regs *regs)
549 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
550 regs->gpr[1], regs->nip);
551 die("Bad kernel stack pointer", regs, SIGABRT);
554 void __init trap_init(void)