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