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_ipi)(struct pt_regs *regs);
48 int (*__debugger_bpt)(struct pt_regs *regs);
49 int (*__debugger_sstep)(struct pt_regs *regs);
50 int (*__debugger_iabr_match)(struct pt_regs *regs);
51 int (*__debugger_dabr_match)(struct pt_regs *regs);
52 int (*__debugger_fault_handler)(struct pt_regs *regs);
54 EXPORT_SYMBOL(__debugger);
55 EXPORT_SYMBOL(__debugger_ipi);
56 EXPORT_SYMBOL(__debugger_bpt);
57 EXPORT_SYMBOL(__debugger_sstep);
58 EXPORT_SYMBOL(__debugger_iabr_match);
59 EXPORT_SYMBOL(__debugger_dabr_match);
60 EXPORT_SYMBOL(__debugger_fault_handler);
64 * Trap & Exception support
67 static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
69 int die(const char *str, struct pt_regs *regs, long err)
71 static int die_counter;
78 spin_lock_irq(&die_lock);
80 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
86 printk("SMP NR_CPUS=%d ", NR_CPUS);
89 #ifdef CONFIG_DEBUG_PAGEALLOC
90 printk("DEBUG_PAGEALLOC ");
97 switch(systemcfg->platform) {
98 case PLATFORM_PSERIES:
102 case PLATFORM_PSERIES_LPAR:
103 printk("PSERIES LPAR ");
106 case PLATFORM_ISERIES_LPAR:
107 printk("ISERIES LPAR ");
110 case PLATFORM_POWERMAC:
119 spin_unlock_irq(&die_lock);
122 panic("Fatal exception in interrupt");
125 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
126 set_current_state(TASK_UNINTERRUPTIBLE);
127 schedule_timeout(5 * HZ);
128 panic("Fatal exception");
136 _exception(int signr, siginfo_t *info, struct pt_regs *regs)
138 if (!user_mode(regs)) {
139 if (die("Exception in kernel mode", regs, signr))
143 force_sig_info(signr, info, current);
146 #ifdef CONFIG_PPC_PSERIES
147 /* Get the error information for errors coming through the
148 * FWNMI vectors. The pt_regs' r3 will be updated to reflect
149 * the actual r3 if possible, and a ptr to the error log entry
150 * will be returned if found.
152 static struct rtas_error_log *FWNMI_get_errinfo(struct pt_regs *regs)
154 unsigned long errdata = regs->gpr[3];
155 struct rtas_error_log *errhdr = NULL;
156 unsigned long *savep;
158 if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
159 (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
160 savep = __va(errdata);
161 regs->gpr[3] = savep[0]; /* restore original r3 */
162 errhdr = (struct rtas_error_log *)(savep + 1);
164 printk("FWNMI: corrupt r3\n");
169 /* Call this when done with the data returned by FWNMI_get_errinfo.
170 * It will release the saved data area for other CPUs in the
171 * partition to receive FWNMI errors.
173 static void FWNMI_release_errinfo(void)
175 unsigned long ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
177 printk("FWNMI: nmi-interlock failed: %ld\n", ret);
182 SystemResetException(struct pt_regs *regs)
184 #ifdef CONFIG_PPC_PSERIES
186 struct rtas_error_log *errhdr = FWNMI_get_errinfo(regs);
188 /* XXX Should look at FWNMI information */
190 FWNMI_release_errinfo();
194 die("System Reset", regs, 0);
196 /* Must die if the interrupt is not recoverable */
197 if (!(regs->msr & MSR_RI))
198 panic("Unrecoverable System Reset");
200 /* What should we do here? We could issue a shutdown or hard reset. */
203 #ifdef CONFIG_PPC_PSERIES
205 * See if we can recover from a machine check exception.
206 * This is only called on power4 (or above) and only via
207 * the Firmware Non-Maskable Interrupts (fwnmi) handler
208 * which provides the error analysis for us.
210 * Return 1 if corrected (or delivered a signal).
211 * Return 0 if there is nothing we can do.
213 static int recover_mce(struct pt_regs *regs, struct rtas_error_log err)
217 if (err.disposition == DISP_FULLY_RECOVERED) {
218 /* Platform corrected itself */
220 } else if ((regs->msr & MSR_RI) &&
222 err.severity == SEVERITY_ERROR_SYNC &&
223 err.disposition == DISP_NOT_RECOVERED &&
224 err.target == TARGET_MEMORY &&
225 err.type == TYPE_ECC_UNCORR &&
226 !(current->pid == 0 || current->pid == 1)) {
227 /* Kill off a user process with an ECC error */
228 info.si_signo = SIGBUS;
230 /* XXX something better for ECC error? */
231 info.si_code = BUS_ADRERR;
232 info.si_addr = (void *)regs->nip;
233 printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
235 _exception(SIGBUS, &info, regs);
243 * Handle a machine check.
245 * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
246 * should be present. If so the handler which called us tells us if the
247 * error was recovered (never true if RI=0).
249 * On hardware prior to Power 4 these exceptions were asynchronous which
250 * means we can't tell exactly where it occurred and so we can't recover.
253 MachineCheckException(struct pt_regs *regs)
255 #ifdef CONFIG_PPC_PSERIES
256 struct rtas_error_log err, *errp;
259 errp = FWNMI_get_errinfo(regs);
262 FWNMI_release_errinfo(); /* frees errp */
263 if (errp && recover_mce(regs, err))
268 if (debugger_fault_handler(regs))
270 die("Machine check", regs, 0);
272 /* Must die if the interrupt is not recoverable */
273 if (!(regs->msr & MSR_RI))
274 panic("Unrecoverable Machine check");
278 UnknownException(struct pt_regs *regs)
282 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
283 regs->nip, regs->msr, regs->trap);
285 info.si_signo = SIGTRAP;
289 _exception(SIGTRAP, &info, regs);
293 InstructionBreakpointException(struct pt_regs *regs)
297 if (debugger_iabr_match(regs))
299 info.si_signo = SIGTRAP;
301 info.si_code = TRAP_BRKPT;
302 info.si_addr = (void *)regs->nip;
303 _exception(SIGTRAP, &info, regs);
306 static void parse_fpe(struct pt_regs *regs)
311 if (regs->msr & MSR_FP)
314 fpscr = current->thread.fpscr;
316 /* Invalid operation */
317 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
318 info.si_code = FPE_FLTINV;
321 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
322 info.si_code = FPE_FLTOVF;
325 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
326 info.si_code = FPE_FLTUND;
329 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
330 info.si_code = FPE_FLTDIV;
333 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
334 info.si_code = FPE_FLTRES;
339 info.si_signo = SIGFPE;
341 info.si_addr = (void *)regs->nip;
342 _exception(SIGFPE, &info, regs);
346 * Look through the list of trap instructions that are used for BUG(),
347 * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
348 * that the exception was caused by a trap instruction of some kind.
349 * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
352 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
354 #ifndef CONFIG_MODULES
355 #define module_find_bug(x) NULL
358 static struct bug_entry *find_bug(unsigned long bugaddr)
360 struct bug_entry *bug;
362 for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
363 if (bugaddr == bug->bug_addr)
365 return module_find_bug(bugaddr);
369 check_bug_trap(struct pt_regs *regs)
371 struct bug_entry *bug;
374 if (regs->msr & MSR_PR)
375 return 0; /* not in kernel */
376 addr = regs->nip; /* address of trap instruction */
377 if (addr < PAGE_OFFSET)
379 bug = find_bug(regs->nip);
382 if (bug->line & BUG_WARNING_TRAP) {
383 /* this is a WARN_ON rather than BUG/BUG_ON */
384 printk(KERN_ERR "Badness in %s at %s:%d\n",
385 bug->function, bug->file,
386 (unsigned int)bug->line & ~BUG_WARNING_TRAP);
387 show_stack(current, (void *)regs->gpr[1]);
390 printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
391 bug->function, bug->file, (unsigned int)bug->line);
396 ProgramCheckException(struct pt_regs *regs)
400 if (regs->msr & 0x100000) {
401 /* IEEE FP exception */
404 } else if (regs->msr & 0x40000) {
405 /* Privileged instruction */
407 info.si_signo = SIGILL;
409 info.si_code = ILL_PRVOPC;
410 info.si_addr = (void *)regs->nip;
411 _exception(SIGILL, &info, regs);
412 } else if (regs->msr & 0x20000) {
415 if (debugger_bpt(regs))
418 if (check_bug_trap(regs)) {
422 info.si_signo = SIGTRAP;
424 info.si_code = TRAP_BRKPT;
425 info.si_addr = (void *)regs->nip;
426 _exception(SIGTRAP, &info, regs);
428 /* Illegal instruction */
430 info.si_signo = SIGILL;
432 info.si_code = ILL_ILLTRP;
433 info.si_addr = (void *)regs->nip;
434 _exception(SIGILL, &info, regs);
438 void KernelFPUnavailableException(struct pt_regs *regs)
440 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
441 "%lx at %lx\n", regs->trap, regs->nip);
442 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
445 void KernelAltivecUnavailableException(struct pt_regs *regs)
447 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
448 "%lx at %lx\n", regs->trap, regs->nip);
449 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
453 SingleStepException(struct pt_regs *regs)
457 regs->msr &= ~MSR_SE; /* Turn off 'trace' bit */
459 if (debugger_sstep(regs))
462 info.si_signo = SIGTRAP;
464 info.si_code = TRAP_TRACE;
465 info.si_addr = (void *)regs->nip;
466 _exception(SIGTRAP, &info, regs);
470 * After we have successfully emulated an instruction, we have to
471 * check if the instruction was being single-stepped, and if so,
472 * pretend we got a single-step exception. This was pointed out
473 * by Kumar Gala. -- paulus
475 static inline void emulate_single_step(struct pt_regs *regs)
477 if (regs->msr & MSR_SE)
478 SingleStepException(regs);
481 static void dummy_perf(struct pt_regs *regs)
485 void (*perf_irq)(struct pt_regs *) = dummy_perf;
488 PerformanceMonitorException(struct pt_regs *regs)
494 AlignmentException(struct pt_regs *regs)
499 fixed = fix_alignment(regs);
502 regs->nip += 4; /* skip over emulated instruction */
503 emulate_single_step(regs);
507 /* Operand address was bad */
508 if (fixed == -EFAULT) {
509 if (user_mode(regs)) {
510 info.si_signo = SIGSEGV;
512 info.si_code = SEGV_MAPERR;
513 info.si_addr = (void *)regs->dar;
514 force_sig_info(SIGSEGV, &info, current);
516 /* Search exception table */
517 bad_page_fault(regs, regs->dar, SIGSEGV);
523 info.si_signo = SIGBUS;
525 info.si_code = BUS_ADRALN;
526 info.si_addr = (void *)regs->nip;
527 _exception(SIGBUS, &info, regs);
530 #ifdef CONFIG_ALTIVEC
532 AltivecAssistException(struct pt_regs *regs)
534 if (regs->msr & MSR_VEC)
535 giveup_altivec(current);
536 /* XXX quick hack for now: set the non-Java bit in the VSCR */
537 current->thread.vscr.u[3] |= 0x10000;
539 #endif /* CONFIG_ALTIVEC */
542 * We enter here if we get an unrecoverable exception, that is, one
543 * that happened at a point where the RI (recoverable interrupt) bit
544 * in the MSR is 0. This indicates that SRR0/1 are live, and that
545 * we therefore lost state by taking this exception.
547 void unrecoverable_exception(struct pt_regs *regs)
549 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
550 regs->trap, regs->nip);
551 die("Unrecoverable exception", regs, SIGABRT);
555 * We enter here if we discover during exception entry that we are
556 * running in supervisor mode with a userspace value in the stack pointer.
558 void kernel_bad_stack(struct pt_regs *regs)
560 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
561 regs->gpr[1], regs->nip);
562 die("Bad kernel stack pointer", regs, SIGABRT);
565 void __init trap_init(void)