upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / ppc64 / kernel / traps.c
1 /*
2  *  linux/arch/ppc64/kernel/traps.c
3  *
4  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
5  *
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.
10  *
11  *  Modified by Cort Dougan (cort@cs.nmt.edu)
12  *  and Paul Mackerras (paulus@cs.anu.edu.au)
13  */
14
15 /*
16  * This file handles the architecture-dependent parts of hardware exceptions
17  */
18
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
33 #include <asm/pgtable.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <asm/io.h>
37 #include <asm/processor.h>
38 #include <asm/ppcdebug.h>
39 #include <asm/rtas.h>
40
41 #ifdef CONFIG_PPC_PSERIES
42 /* This is true if we are using the firmware NMI handler (typically LPAR) */
43 extern int fwnmi_active;
44 #endif
45
46 #ifdef CONFIG_DEBUGGER
47 int (*__debugger)(struct pt_regs *regs);
48 int (*__debugger_ipi)(struct pt_regs *regs);
49 int (*__debugger_bpt)(struct pt_regs *regs);
50 int (*__debugger_sstep)(struct pt_regs *regs);
51 int (*__debugger_iabr_match)(struct pt_regs *regs);
52 int (*__debugger_dabr_match)(struct pt_regs *regs);
53 int (*__debugger_fault_handler)(struct pt_regs *regs);
54
55 EXPORT_SYMBOL(__debugger);
56 EXPORT_SYMBOL(__debugger_ipi);
57 EXPORT_SYMBOL(__debugger_bpt);
58 EXPORT_SYMBOL(__debugger_sstep);
59 EXPORT_SYMBOL(__debugger_iabr_match);
60 EXPORT_SYMBOL(__debugger_dabr_match);
61 EXPORT_SYMBOL(__debugger_fault_handler);
62 #endif
63
64 /*
65  * Trap & Exception support
66  */
67
68 static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
69
70 int die(const char *str, struct pt_regs *regs, long err)
71 {
72         static int die_counter;
73         int nl = 0;
74
75         if (debugger(regs))
76                 return 1;
77
78         console_verbose();
79         spin_lock_irq(&die_lock);
80         bust_spinlocks(1);
81         printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
82 #ifdef CONFIG_PREEMPT
83         printk("PREEMPT ");
84         nl = 1;
85 #endif
86 #ifdef CONFIG_SMP
87         printk("SMP NR_CPUS=%d ", NR_CPUS);
88         nl = 1;
89 #endif
90 #ifdef CONFIG_DEBUG_PAGEALLOC
91         printk("DEBUG_PAGEALLOC ");
92         nl = 1;
93 #endif
94 #ifdef CONFIG_NUMA
95         printk("NUMA ");
96         nl = 1;
97 #endif
98         switch(systemcfg->platform) {
99                 case PLATFORM_PSERIES:
100                         printk("PSERIES ");
101                         nl = 1;
102                         break;
103                 case PLATFORM_PSERIES_LPAR:
104                         printk("PSERIES LPAR ");
105                         nl = 1;
106                         break;
107                 case PLATFORM_ISERIES_LPAR:
108                         printk("ISERIES LPAR ");
109                         nl = 1;
110                         break;
111                 case PLATFORM_POWERMAC:
112                         printk("POWERMAC ");
113                         nl = 1;
114                         break;
115         }
116         if (nl)
117                 printk("\n");
118         show_regs(regs);
119         try_crashdump(regs);
120         bust_spinlocks(0);
121         spin_unlock_irq(&die_lock);
122
123         if (in_interrupt())
124                 panic("Fatal exception in interrupt");
125
126         if (panic_on_oops) {
127                 if (netdump_func)
128                         netdump_func = NULL;
129                 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
130                 set_current_state(TASK_UNINTERRUPTIBLE);
131                 schedule_timeout(5 * HZ);
132                 panic("Fatal exception");
133         }
134         do_exit(SIGSEGV);
135
136         return 0;
137 }
138
139 static void
140 _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
141 {
142         siginfo_t info;
143
144         if (!user_mode(regs)) {
145                 if (die("Exception in kernel mode", regs, signr))
146                         return;
147         }
148
149         memset(&info, 0, sizeof(info));
150         info.si_signo = signr;
151         info.si_code = code;
152         info.si_addr = (void __user *) addr;
153         force_sig_info(signr, &info, current);
154 }
155
156 #ifdef CONFIG_PPC_PSERIES
157 /* Get the error information for errors coming through the
158  * FWNMI vectors.  The pt_regs' r3 will be updated to reflect
159  * the actual r3 if possible, and a ptr to the error log entry
160  * will be returned if found.
161  */
162 static struct rtas_error_log *FWNMI_get_errinfo(struct pt_regs *regs)
163 {
164         unsigned long errdata = regs->gpr[3];
165         struct rtas_error_log *errhdr = NULL;
166         unsigned long *savep;
167
168         if ((errdata >= 0x7000 && errdata < 0x7fff0) ||
169             (errdata >= rtas.base && errdata < rtas.base + rtas.size - 16)) {
170                 savep = __va(errdata);
171                 regs->gpr[3] = savep[0];        /* restore original r3 */
172                 errhdr = (struct rtas_error_log *)(savep + 1);
173         } else {
174                 printk("FWNMI: corrupt r3\n");
175         }
176         return errhdr;
177 }
178
179 /* Call this when done with the data returned by FWNMI_get_errinfo.
180  * It will release the saved data area for other CPUs in the
181  * partition to receive FWNMI errors.
182  */
183 static void FWNMI_release_errinfo(void)
184 {
185         int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
186         if (ret != 0)
187                 printk("FWNMI: nmi-interlock failed: %d\n", ret);
188 }
189 #endif
190
191 void
192 SystemResetException(struct pt_regs *regs)
193 {
194 #ifdef CONFIG_PPC_PSERIES
195         if (fwnmi_active) {
196                 struct rtas_error_log *errhdr = FWNMI_get_errinfo(regs);
197                 if (errhdr) {
198                         /* XXX Should look at FWNMI information */
199                 }
200                 FWNMI_release_errinfo();
201         }
202 #endif
203
204         die("System Reset", regs, 0);
205
206         /* Must die if the interrupt is not recoverable */
207         if (!(regs->msr & MSR_RI))
208                 panic("Unrecoverable System Reset");
209
210         /* What should we do here? We could issue a shutdown or hard reset. */
211 }
212
213 #ifdef CONFIG_PPC_PSERIES
214 /* 
215  * See if we can recover from a machine check exception.
216  * This is only called on power4 (or above) and only via
217  * the Firmware Non-Maskable Interrupts (fwnmi) handler
218  * which provides the error analysis for us.
219  *
220  * Return 1 if corrected (or delivered a signal).
221  * Return 0 if there is nothing we can do.
222  */
223 static int recover_mce(struct pt_regs *regs, struct rtas_error_log err)
224 {
225         if (err.disposition == RTAS_DISP_FULLY_RECOVERED) {
226                 /* Platform corrected itself */
227                 return 1;
228         } else if ((regs->msr & MSR_RI) &&
229                    user_mode(regs) &&
230                    err.severity == RTAS_SEVERITY_ERROR_SYNC &&
231                    err.disposition == RTAS_DISP_NOT_RECOVERED &&
232                    err.target == RTAS_TARGET_MEMORY &&
233                    err.type == RTAS_TYPE_ECC_UNCORR &&
234                    !(current->pid == 0 || current->pid == 1)) {
235                 /* Kill off a user process with an ECC error */
236                 printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
237                        current->pid);
238                 /* XXX something better for ECC error? */
239                 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
240                 return 1;
241         }
242         return 0;
243 }
244 #endif
245
246 /*
247  * Handle a machine check.
248  *
249  * Note that on Power 4 and beyond Firmware Non-Maskable Interrupts (fwnmi)
250  * should be present.  If so the handler which called us tells us if the
251  * error was recovered (never true if RI=0).
252  *
253  * On hardware prior to Power 4 these exceptions were asynchronous which
254  * means we can't tell exactly where it occurred and so we can't recover.
255  */
256 void
257 MachineCheckException(struct pt_regs *regs)
258 {
259 #ifdef CONFIG_PPC_PSERIES
260         struct rtas_error_log err, *errp;
261
262         if (fwnmi_active) {
263                 errp = FWNMI_get_errinfo(regs);
264                 if (errp)
265                         err = *errp;
266                 FWNMI_release_errinfo();        /* frees errp */
267                 if (errp && recover_mce(regs, err))
268                         return;
269         }
270 #endif
271
272         if (debugger_fault_handler(regs))
273                 return;
274         die("Machine check", regs, 0);
275
276         /* Must die if the interrupt is not recoverable */
277         if (!(regs->msr & MSR_RI))
278                 panic("Unrecoverable Machine check");
279 }
280
281 void
282 UnknownException(struct pt_regs *regs)
283 {
284         printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
285                regs->nip, regs->msr, regs->trap);
286
287         _exception(SIGTRAP, regs, 0, 0);
288 }
289
290 void
291 InstructionBreakpointException(struct pt_regs *regs)
292 {
293         if (debugger_iabr_match(regs))
294                 return;
295         _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
296 }
297
298 void
299 SingleStepException(struct pt_regs *regs)
300 {
301         regs->msr &= ~MSR_SE;  /* Turn off 'trace' bit */
302
303         if (debugger_sstep(regs))
304                 return;
305
306         _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
307 }
308
309 /*
310  * After we have successfully emulated an instruction, we have to
311  * check if the instruction was being single-stepped, and if so,
312  * pretend we got a single-step exception.  This was pointed out
313  * by Kumar Gala.  -- paulus
314  */
315 static inline void emulate_single_step(struct pt_regs *regs)
316 {
317         if (regs->msr & MSR_SE)
318                 SingleStepException(regs);
319 }
320
321 static void parse_fpe(struct pt_regs *regs)
322 {
323         int code = 0;
324         unsigned long fpscr;
325
326         flush_fp_to_thread(current);
327
328         fpscr = current->thread.fpscr;
329
330         /* Invalid operation */
331         if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
332                 code = FPE_FLTINV;
333
334         /* Overflow */
335         else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
336                 code = FPE_FLTOVF;
337
338         /* Underflow */
339         else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
340                 code = FPE_FLTUND;
341
342         /* Divide by zero */
343         else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
344                 code = FPE_FLTDIV;
345
346         /* Inexact result */
347         else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
348                 code = FPE_FLTRES;
349
350         _exception(SIGFPE, regs, code, regs->nip);
351 }
352
353 /*
354  * Illegal instruction emulation support.  Return non-zero if we can't
355  * emulate, or -EFAULT if the associated memory access caused an access
356  * fault.  Return zero on success.
357  */
358
359 #define INST_DCBA               0x7c0005ec
360 #define INST_DCBA_MASK          0x7c0007fe
361
362 #define INST_MCRXR              0x7c000400
363 #define INST_MCRXR_MASK         0x7c0007fe
364
365 static int emulate_instruction(struct pt_regs *regs)
366 {
367         unsigned int instword;
368
369         if (!user_mode(regs))
370                 return -EINVAL;
371
372         CHECK_FULL_REGS(regs);
373
374         if (get_user(instword, (unsigned int __user *)(regs->nip)))
375                 return -EFAULT;
376
377         /* Emulating the dcba insn is just a no-op.  */
378         if ((instword & INST_DCBA_MASK) == INST_DCBA) {
379                 static int warned;
380
381                 if (!warned) {
382                         printk(KERN_WARNING
383                                "process %d (%s) uses obsolete 'dcba' insn\n",
384                                current->pid, current->comm);
385                         warned = 1;
386                 }
387                 return 0;
388         }
389
390         /* Emulate the mcrxr insn.  */
391         if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
392                 static int warned;
393                 unsigned int shift;
394
395                 if (!warned) {
396                         printk(KERN_WARNING
397                                "process %d (%s) uses obsolete 'mcrxr' insn\n",
398                                current->pid, current->comm);
399                         warned = 1;
400                 }
401
402                 shift = (instword >> 21) & 0x1c;
403                 regs->ccr &= ~(0xf0000000 >> shift);
404                 regs->ccr |= (regs->xer & 0xf0000000) >> shift;
405                 regs->xer &= ~0xf0000000;
406                 return 0;
407         }
408
409         return -EINVAL;
410 }
411
412 /*
413  * Look through the list of trap instructions that are used for BUG(),
414  * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know
415  * that the exception was caused by a trap instruction of some kind.
416  * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
417  * otherwise.
418  */
419 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
420
421 #ifndef CONFIG_MODULES
422 #define module_find_bug(x)      NULL
423 #endif
424
425 static struct bug_entry *find_bug(unsigned long bugaddr)
426 {
427         struct bug_entry *bug;
428
429         for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
430                 if (bugaddr == bug->bug_addr)
431                         return bug;
432         return module_find_bug(bugaddr);
433 }
434
435 int
436 check_bug_trap(struct pt_regs *regs)
437 {
438         struct bug_entry *bug;
439         unsigned long addr;
440
441         if (regs->msr & MSR_PR)
442                 return 0;       /* not in kernel */
443         addr = regs->nip;       /* address of trap instruction */
444         if (addr < PAGE_OFFSET)
445                 return 0;
446         bug = find_bug(regs->nip);
447         if (bug == NULL)
448                 return 0;
449         if (bug->line & BUG_WARNING_TRAP) {
450                 /* this is a WARN_ON rather than BUG/BUG_ON */
451                 printk(KERN_ERR "Badness in %s at %s:%d\n",
452                        bug->function, bug->file,
453                       (unsigned int)bug->line & ~BUG_WARNING_TRAP);
454                 show_stack(current, (void *)regs->gpr[1]);
455                 return 1;
456         }
457         printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
458                bug->function, bug->file, (unsigned int)bug->line);
459         return 0;
460 }
461
462 void
463 ProgramCheckException(struct pt_regs *regs)
464 {
465         if (regs->msr & 0x100000) {
466                 /* IEEE FP exception */
467                 parse_fpe(regs);
468
469         } else if (regs->msr & 0x40000) {
470                 /* Privileged instruction */
471                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
472
473         } else if (regs->msr & 0x20000) {
474                 /* trap exception */
475
476                 if (debugger_bpt(regs))
477                         return;
478
479                 if (check_bug_trap(regs)) {
480                         regs->nip += 4;
481                         return;
482                 }
483                 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
484
485         } else {
486                 /* Illegal instruction; try to emulate it.  */
487                 switch (emulate_instruction(regs)) {
488                 case 0:
489                         regs->nip += 4;
490                         emulate_single_step(regs);
491                         break;
492
493                 case -EFAULT:
494                         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
495                         break;
496
497                 default:
498                         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
499                         break;
500                 }
501         }
502 }
503
504 void KernelFPUnavailableException(struct pt_regs *regs)
505 {
506         printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
507                           "%lx at %lx\n", regs->trap, regs->nip);
508         die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
509 }
510
511 void AltivecUnavailableException(struct pt_regs *regs)
512 {
513 #ifndef CONFIG_ALTIVEC
514         if (user_mode(regs)) {
515                 /* A user program has executed an altivec instruction,
516                    but this kernel doesn't support altivec. */
517                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
518                 return;
519         }
520 #endif
521         printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
522                           "%lx at %lx\n", regs->trap, regs->nip);
523         die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
524 }
525
526 /* Ensure exceptions are disabled */
527 #define MMCR0_PMXE      (1UL << (31 - 5))
528 #define MMCR0_PMAO      (1UL << (31 - 24))
529
530 static void dummy_perf(struct pt_regs *regs)
531 {
532         unsigned int mmcr0 = mfspr(SPRN_MMCR0);
533
534         mmcr0 &= ~(MMCR0_PMXE|MMCR0_PMAO);
535         mtspr(SPRN_MMCR0, mmcr0);
536 }
537
538 void (*perf_irq)(struct pt_regs *) = dummy_perf;
539
540 EXPORT_SYMBOL(perf_irq);
541
542 void
543 PerformanceMonitorException(struct pt_regs *regs)
544 {
545         perf_irq(regs);
546 }
547
548 void
549 AlignmentException(struct pt_regs *regs)
550 {
551         int fixed;
552
553         fixed = fix_alignment(regs);
554
555         if (fixed == 1) {
556                 regs->nip += 4; /* skip over emulated instruction */
557                 emulate_single_step(regs);
558                 return;
559         }
560
561         /* Operand address was bad */   
562         if (fixed == -EFAULT) {
563                 if (user_mode(regs)) {
564                         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->dar);
565                 } else {
566                         /* Search exception table */
567                         bad_page_fault(regs, regs->dar, SIGSEGV);
568                 }
569
570                 return;
571         }
572
573         _exception(SIGBUS, regs, BUS_ADRALN, regs->nip);
574 }
575
576 #ifdef CONFIG_ALTIVEC
577 void
578 AltivecAssistException(struct pt_regs *regs)
579 {
580         int err;
581         siginfo_t info;
582
583         if (!user_mode(regs)) {
584                 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
585                        " at %lx\n", regs->nip);
586                 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
587         }
588
589         flush_altivec_to_thread(current);
590
591         err = emulate_altivec(regs);
592         if (err == 0) {
593                 regs->nip += 4;         /* skip emulated instruction */
594                 emulate_single_step(regs);
595                 return;
596         }
597
598         if (err == -EFAULT) {
599                 /* got an error reading the instruction */
600                 info.si_signo = SIGSEGV;
601                 info.si_errno = 0;
602                 info.si_code = SEGV_MAPERR;
603                 info.si_addr = (void __user *) regs->nip;
604                 force_sig_info(SIGSEGV, &info, current);
605         } else {
606                 /* didn't recognize the instruction */
607                 /* XXX quick hack for now: set the non-Java bit in the VSCR */
608                 if (printk_ratelimit())
609                         printk(KERN_ERR "Unrecognized altivec instruction "
610                                "in %s at %lx\n", current->comm, regs->nip);
611                 current->thread.vscr.u[3] |= 0x10000;
612         }
613 }
614 #endif /* CONFIG_ALTIVEC */
615
616 /*
617  * We enter here if we get an unrecoverable exception, that is, one
618  * that happened at a point where the RI (recoverable interrupt) bit
619  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
620  * we therefore lost state by taking this exception.
621  */
622 void unrecoverable_exception(struct pt_regs *regs)
623 {
624         printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
625                regs->trap, regs->nip);
626         die("Unrecoverable exception", regs, SIGABRT);
627 }
628
629 /*
630  * We enter here if we discover during exception entry that we are
631  * running in supervisor mode with a userspace value in the stack pointer.
632  */
633 void kernel_bad_stack(struct pt_regs *regs)
634 {
635         printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
636                regs->gpr[1], regs->nip);
637         die("Bad kernel stack pointer", regs, SIGABRT);
638 }
639
640 void __init trap_init(void)
641 {
642 }