kbuild: fix silentoldconfig with make O=
[linux-2.6/verdex.git] / arch / ppc64 / kernel / traps.c
blob7467ae508e6e27e734c1fc9110c2f03e60790900
1 /*
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>
23 #include <linux/mm.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>
32 #include <linux/delay.h>
33 #include <linux/kprobes.h>
34 #include <asm/kdebug.h>
36 #include <asm/pgtable.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <asm/io.h>
40 #include <asm/processor.h>
41 #include <asm/ppcdebug.h>
42 #include <asm/rtas.h>
43 #include <asm/systemcfg.h>
44 #include <asm/machdep.h>
45 #include <asm/pmc.h>
47 #ifdef CONFIG_DEBUGGER
48 int (*__debugger)(struct pt_regs *regs);
49 int (*__debugger_ipi)(struct pt_regs *regs);
50 int (*__debugger_bpt)(struct pt_regs *regs);
51 int (*__debugger_sstep)(struct pt_regs *regs);
52 int (*__debugger_iabr_match)(struct pt_regs *regs);
53 int (*__debugger_dabr_match)(struct pt_regs *regs);
54 int (*__debugger_fault_handler)(struct pt_regs *regs);
56 EXPORT_SYMBOL(__debugger);
57 EXPORT_SYMBOL(__debugger_ipi);
58 EXPORT_SYMBOL(__debugger_bpt);
59 EXPORT_SYMBOL(__debugger_sstep);
60 EXPORT_SYMBOL(__debugger_iabr_match);
61 EXPORT_SYMBOL(__debugger_dabr_match);
62 EXPORT_SYMBOL(__debugger_fault_handler);
63 #endif
65 struct notifier_block *ppc64_die_chain;
66 static DEFINE_SPINLOCK(die_notifier_lock);
68 int register_die_notifier(struct notifier_block *nb)
70 int err = 0;
71 unsigned long flags;
73 spin_lock_irqsave(&die_notifier_lock, flags);
74 err = notifier_chain_register(&ppc64_die_chain, nb);
75 spin_unlock_irqrestore(&die_notifier_lock, flags);
76 return err;
80 * Trap & Exception support
83 static DEFINE_SPINLOCK(die_lock);
85 int die(const char *str, struct pt_regs *regs, long err)
87 static int die_counter;
88 int nl = 0;
90 if (debugger(regs))
91 return 1;
93 console_verbose();
94 spin_lock_irq(&die_lock);
95 bust_spinlocks(1);
96 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
97 #ifdef CONFIG_PREEMPT
98 printk("PREEMPT ");
99 nl = 1;
100 #endif
101 #ifdef CONFIG_SMP
102 printk("SMP NR_CPUS=%d ", NR_CPUS);
103 nl = 1;
104 #endif
105 #ifdef CONFIG_DEBUG_PAGEALLOC
106 printk("DEBUG_PAGEALLOC ");
107 nl = 1;
108 #endif
109 #ifdef CONFIG_NUMA
110 printk("NUMA ");
111 nl = 1;
112 #endif
113 switch(systemcfg->platform) {
114 case PLATFORM_PSERIES:
115 printk("PSERIES ");
116 nl = 1;
117 break;
118 case PLATFORM_PSERIES_LPAR:
119 printk("PSERIES LPAR ");
120 nl = 1;
121 break;
122 case PLATFORM_ISERIES_LPAR:
123 printk("ISERIES LPAR ");
124 nl = 1;
125 break;
126 case PLATFORM_POWERMAC:
127 printk("POWERMAC ");
128 nl = 1;
129 break;
130 case PLATFORM_BPA:
131 printk("BPA ");
132 nl = 1;
133 break;
135 if (nl)
136 printk("\n");
137 print_modules();
138 show_regs(regs);
139 bust_spinlocks(0);
140 spin_unlock_irq(&die_lock);
142 if (in_interrupt())
143 panic("Fatal exception in interrupt");
145 if (panic_on_oops) {
146 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
147 ssleep(5);
148 panic("Fatal exception");
150 do_exit(SIGSEGV);
152 return 0;
155 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
157 siginfo_t info;
159 if (!user_mode(regs)) {
160 if (die("Exception in kernel mode", regs, signr))
161 return;
164 memset(&info, 0, sizeof(info));
165 info.si_signo = signr;
166 info.si_code = code;
167 info.si_addr = (void __user *) addr;
168 force_sig_info(signr, &info, current);
171 void system_reset_exception(struct pt_regs *regs)
173 /* See if any machine dependent calls */
174 if (ppc_md.system_reset_exception)
175 ppc_md.system_reset_exception(regs);
177 die("System Reset", regs, 0);
179 /* Must die if the interrupt is not recoverable */
180 if (!(regs->msr & MSR_RI))
181 panic("Unrecoverable System Reset");
183 /* What should we do here? We could issue a shutdown or hard reset. */
186 void machine_check_exception(struct pt_regs *regs)
188 int recover = 0;
190 /* See if any machine dependent calls */
191 if (ppc_md.machine_check_exception)
192 recover = ppc_md.machine_check_exception(regs);
194 if (recover)
195 return;
197 if (debugger_fault_handler(regs))
198 return;
199 die("Machine check", regs, 0);
201 /* Must die if the interrupt is not recoverable */
202 if (!(regs->msr & MSR_RI))
203 panic("Unrecoverable Machine check");
206 void unknown_exception(struct pt_regs *regs)
208 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
209 regs->nip, regs->msr, regs->trap);
211 _exception(SIGTRAP, regs, 0, 0);
214 void instruction_breakpoint_exception(struct pt_regs *regs)
216 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
217 5, SIGTRAP) == NOTIFY_STOP)
218 return;
219 if (debugger_iabr_match(regs))
220 return;
221 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
224 void __kprobes single_step_exception(struct pt_regs *regs)
226 regs->msr &= ~MSR_SE; /* Turn off 'trace' bit */
228 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
229 5, SIGTRAP) == NOTIFY_STOP)
230 return;
231 if (debugger_sstep(regs))
232 return;
234 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
238 * After we have successfully emulated an instruction, we have to
239 * check if the instruction was being single-stepped, and if so,
240 * pretend we got a single-step exception. This was pointed out
241 * by Kumar Gala. -- paulus
243 static inline void emulate_single_step(struct pt_regs *regs)
245 if (regs->msr & MSR_SE)
246 single_step_exception(regs);
249 static void parse_fpe(struct pt_regs *regs)
251 int code = 0;
252 unsigned long fpscr;
254 flush_fp_to_thread(current);
256 fpscr = current->thread.fpscr;
258 /* Invalid operation */
259 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
260 code = FPE_FLTINV;
262 /* Overflow */
263 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
264 code = FPE_FLTOVF;
266 /* Underflow */
267 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
268 code = FPE_FLTUND;
270 /* Divide by zero */
271 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
272 code = FPE_FLTDIV;
274 /* Inexact result */
275 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
276 code = FPE_FLTRES;
278 _exception(SIGFPE, regs, code, regs->nip);
282 * Illegal instruction emulation support. Return non-zero if we can't
283 * emulate, or -EFAULT if the associated memory access caused an access
284 * fault. Return zero on success.
287 #define INST_MFSPR_PVR 0x7c1f42a6
288 #define INST_MFSPR_PVR_MASK 0xfc1fffff
290 #define INST_DCBA 0x7c0005ec
291 #define INST_DCBA_MASK 0x7c0007fe
293 #define INST_MCRXR 0x7c000400
294 #define INST_MCRXR_MASK 0x7c0007fe
296 static int emulate_instruction(struct pt_regs *regs)
298 unsigned int instword;
300 if (!user_mode(regs))
301 return -EINVAL;
303 CHECK_FULL_REGS(regs);
305 if (get_user(instword, (unsigned int __user *)(regs->nip)))
306 return -EFAULT;
308 /* Emulate the mfspr rD, PVR. */
309 if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
310 unsigned int rd;
312 rd = (instword >> 21) & 0x1f;
313 regs->gpr[rd] = mfspr(SPRN_PVR);
314 return 0;
317 /* Emulating the dcba insn is just a no-op. */
318 if ((instword & INST_DCBA_MASK) == INST_DCBA) {
319 static int warned;
321 if (!warned) {
322 printk(KERN_WARNING
323 "process %d (%s) uses obsolete 'dcba' insn\n",
324 current->pid, current->comm);
325 warned = 1;
327 return 0;
330 /* Emulate the mcrxr insn. */
331 if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
332 static int warned;
333 unsigned int shift;
335 if (!warned) {
336 printk(KERN_WARNING
337 "process %d (%s) uses obsolete 'mcrxr' insn\n",
338 current->pid, current->comm);
339 warned = 1;
342 shift = (instword >> 21) & 0x1c;
343 regs->ccr &= ~(0xf0000000 >> shift);
344 regs->ccr |= (regs->xer & 0xf0000000) >> shift;
345 regs->xer &= ~0xf0000000;
346 return 0;
349 return -EINVAL;
353 * Look through the list of trap instructions that are used for BUG(),
354 * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
355 * that the exception was caused by a trap instruction of some kind.
356 * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
357 * otherwise.
359 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
361 #ifndef CONFIG_MODULES
362 #define module_find_bug(x) NULL
363 #endif
365 struct bug_entry *find_bug(unsigned long bugaddr)
367 struct bug_entry *bug;
369 for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
370 if (bugaddr == bug->bug_addr)
371 return bug;
372 return module_find_bug(bugaddr);
375 static int
376 check_bug_trap(struct pt_regs *regs)
378 struct bug_entry *bug;
379 unsigned long addr;
381 if (regs->msr & MSR_PR)
382 return 0; /* not in kernel */
383 addr = regs->nip; /* address of trap instruction */
384 if (addr < PAGE_OFFSET)
385 return 0;
386 bug = find_bug(regs->nip);
387 if (bug == NULL)
388 return 0;
389 if (bug->line & BUG_WARNING_TRAP) {
390 /* this is a WARN_ON rather than BUG/BUG_ON */
391 printk(KERN_ERR "Badness in %s at %s:%d\n",
392 bug->function, bug->file,
393 (unsigned int)bug->line & ~BUG_WARNING_TRAP);
394 show_stack(current, (void *)regs->gpr[1]);
395 return 1;
397 printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
398 bug->function, bug->file, (unsigned int)bug->line);
399 return 0;
402 void __kprobes program_check_exception(struct pt_regs *regs)
404 if (debugger_fault_handler(regs))
405 return;
407 if (regs->msr & 0x100000) {
408 /* IEEE FP exception */
409 parse_fpe(regs);
410 } else if (regs->msr & 0x20000) {
411 /* trap exception */
413 if (notify_die(DIE_BPT, "breakpoint", regs, 5,
414 5, SIGTRAP) == NOTIFY_STOP)
415 return;
416 if (debugger_bpt(regs))
417 return;
419 if (check_bug_trap(regs)) {
420 regs->nip += 4;
421 return;
423 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
425 } else {
426 /* Privileged or illegal instruction; try to emulate it. */
427 switch (emulate_instruction(regs)) {
428 case 0:
429 regs->nip += 4;
430 emulate_single_step(regs);
431 break;
433 case -EFAULT:
434 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
435 break;
437 default:
438 if (regs->msr & 0x40000)
439 /* priveleged */
440 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
441 else
442 /* illegal */
443 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
444 break;
449 void kernel_fp_unavailable_exception(struct pt_regs *regs)
451 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
452 "%lx at %lx\n", regs->trap, regs->nip);
453 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
456 void altivec_unavailable_exception(struct pt_regs *regs)
458 if (user_mode(regs)) {
459 /* A user program has executed an altivec instruction,
460 but this kernel doesn't support altivec. */
461 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
462 return;
464 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
465 "%lx at %lx\n", regs->trap, regs->nip);
466 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
469 extern perf_irq_t perf_irq;
471 void performance_monitor_exception(struct pt_regs *regs)
473 perf_irq(regs);
476 void alignment_exception(struct pt_regs *regs)
478 int fixed;
480 fixed = fix_alignment(regs);
482 if (fixed == 1) {
483 regs->nip += 4; /* skip over emulated instruction */
484 emulate_single_step(regs);
485 return;
488 /* Operand address was bad */
489 if (fixed == -EFAULT) {
490 if (user_mode(regs)) {
491 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->dar);
492 } else {
493 /* Search exception table */
494 bad_page_fault(regs, regs->dar, SIGSEGV);
497 return;
500 _exception(SIGBUS, regs, BUS_ADRALN, regs->nip);
503 #ifdef CONFIG_ALTIVEC
504 void altivec_assist_exception(struct pt_regs *regs)
506 int err;
507 siginfo_t info;
509 if (!user_mode(regs)) {
510 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
511 " at %lx\n", regs->nip);
512 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
515 flush_altivec_to_thread(current);
517 err = emulate_altivec(regs);
518 if (err == 0) {
519 regs->nip += 4; /* skip emulated instruction */
520 emulate_single_step(regs);
521 return;
524 if (err == -EFAULT) {
525 /* got an error reading the instruction */
526 info.si_signo = SIGSEGV;
527 info.si_errno = 0;
528 info.si_code = SEGV_MAPERR;
529 info.si_addr = (void __user *) regs->nip;
530 force_sig_info(SIGSEGV, &info, current);
531 } else {
532 /* didn't recognize the instruction */
533 /* XXX quick hack for now: set the non-Java bit in the VSCR */
534 if (printk_ratelimit())
535 printk(KERN_ERR "Unrecognized altivec instruction "
536 "in %s at %lx\n", current->comm, regs->nip);
537 current->thread.vscr.u[3] |= 0x10000;
540 #endif /* CONFIG_ALTIVEC */
543 * We enter here if we get an unrecoverable exception, that is, one
544 * that happened at a point where the RI (recoverable interrupt) bit
545 * in the MSR is 0. This indicates that SRR0/1 are live, and that
546 * we therefore lost state by taking this exception.
548 void unrecoverable_exception(struct pt_regs *regs)
550 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
551 regs->trap, regs->nip);
552 die("Unrecoverable exception", regs, SIGABRT);
556 * We enter here if we discover during exception entry that we are
557 * running in supervisor mode with a userspace value in the stack pointer.
559 void kernel_bad_stack(struct pt_regs *regs)
561 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
562 regs->gpr[1], regs->nip);
563 die("Bad kernel stack pointer", regs, SIGABRT);
566 void __init trap_init(void)