VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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, siginfo_t *info, struct pt_regs *regs)
138 {
139         if (!user_mode(regs)) {
140                 if (die("Exception in kernel mode", regs, signr))
141                         return;
142         }
143
144         force_sig_info(signr, info, current);
145 }
146
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.
152  */
153 static struct rtas_error_log *FWNMI_get_errinfo(struct pt_regs *regs)
154 {
155         unsigned long errdata = regs->gpr[3];
156         struct rtas_error_log *errhdr = NULL;
157         unsigned long *savep;
158
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);
164         } else {
165                 printk("FWNMI: corrupt r3\n");
166         }
167         return errhdr;
168 }
169
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.
173  */
174 static void FWNMI_release_errinfo(void)
175 {
176         int ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
177         if (ret != 0)
178                 printk("FWNMI: nmi-interlock failed: %d\n", ret);
179 }
180 #endif
181
182 void
183 SystemResetException(struct pt_regs *regs)
184 {
185 #ifdef CONFIG_PPC_PSERIES
186         if (fwnmi_active) {
187                 struct rtas_error_log *errhdr = FWNMI_get_errinfo(regs);
188                 if (errhdr) {
189                         /* XXX Should look at FWNMI information */
190                 }
191                 FWNMI_release_errinfo();
192         }
193 #endif
194
195         die("System Reset", regs, 0);
196
197         /* Must die if the interrupt is not recoverable */
198         if (!(regs->msr & MSR_RI))
199                 panic("Unrecoverable System Reset");
200
201         /* What should we do here? We could issue a shutdown or hard reset. */
202 }
203
204 #ifdef CONFIG_PPC_PSERIES
205 /* 
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.
210  *
211  * Return 1 if corrected (or delivered a signal).
212  * Return 0 if there is nothing we can do.
213  */
214 static int recover_mce(struct pt_regs *regs, struct rtas_error_log err)
215 {
216         siginfo_t info;
217
218         if (err.disposition == DISP_FULLY_RECOVERED) {
219                 /* Platform corrected itself */
220                 return 1;
221         } else if ((regs->msr & MSR_RI) &&
222                    user_mode(regs) &&
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;
230                 info.si_errno = 0;
231                 /* XXX something better for ECC error? */
232                 info.si_code = BUS_ADRERR;
233                 info.si_addr = (void __user *)regs->nip;
234                 printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
235                        current->pid);
236                 _exception(SIGBUS, &info, regs);
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         siginfo_t info;
282
283         printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
284                regs->nip, regs->msr, regs->trap);
285
286         info.si_signo = SIGTRAP;
287         info.si_errno = 0;
288         info.si_code = 0;
289         info.si_addr = NULL;
290         _exception(SIGTRAP, &info, regs);       
291 }
292
293 void
294 InstructionBreakpointException(struct pt_regs *regs)
295 {
296         siginfo_t info;
297
298         if (debugger_iabr_match(regs))
299                 return;
300         info.si_signo = SIGTRAP;
301         info.si_errno = 0;
302         info.si_code = TRAP_BRKPT;
303         info.si_addr = (void __user *)regs->nip;
304         _exception(SIGTRAP, &info, regs);
305 }
306
307 static void parse_fpe(struct pt_regs *regs)
308 {
309         siginfo_t info;
310         unsigned long fpscr;
311
312         flush_fp_to_thread(current);
313
314         fpscr = current->thread.fpscr;
315
316         /* Invalid operation */
317         if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
318                 info.si_code = FPE_FLTINV;
319
320         /* Overflow */
321         else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
322                 info.si_code = FPE_FLTOVF;
323
324         /* Underflow */
325         else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
326                 info.si_code = FPE_FLTUND;
327
328         /* Divide by zero */
329         else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
330                 info.si_code = FPE_FLTDIV;
331
332         /* Inexact result */
333         else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
334                 info.si_code = FPE_FLTRES;
335
336         else
337                 info.si_code = 0;
338
339         info.si_signo = SIGFPE;
340         info.si_errno = 0;
341         info.si_addr = (void __user *)regs->nip;
342         _exception(SIGFPE, &info, regs);
343 }
344
345 /*
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
350  * otherwise.
351  */
352 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
353
354 #ifndef CONFIG_MODULES
355 #define module_find_bug(x)      NULL
356 #endif
357
358 static struct bug_entry *find_bug(unsigned long bugaddr)
359 {
360         struct bug_entry *bug;
361
362         for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
363                 if (bugaddr == bug->bug_addr)
364                         return bug;
365         return module_find_bug(bugaddr);
366 }
367
368 int
369 check_bug_trap(struct pt_regs *regs)
370 {
371         struct bug_entry *bug;
372         unsigned long addr;
373
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)
378                 return 0;
379         bug = find_bug(regs->nip);
380         if (bug == NULL)
381                 return 0;
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]);
388                 return 1;
389         }
390         printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
391                bug->function, bug->file, (unsigned int)bug->line);
392         return 0;
393 }
394
395 void
396 ProgramCheckException(struct pt_regs *regs)
397 {
398         siginfo_t info;
399
400         if (regs->msr & 0x100000) {
401                 /* IEEE FP exception */
402
403                 parse_fpe(regs);
404         } else if (regs->msr & 0x40000) {
405                 /* Privileged instruction */
406
407                 info.si_signo = SIGILL;
408                 info.si_errno = 0;
409                 info.si_code = ILL_PRVOPC;
410                 info.si_addr = (void __user *)regs->nip;
411                 _exception(SIGILL, &info, regs);
412         } else if (regs->msr & 0x20000) {
413                 /* trap exception */
414
415                 if (debugger_bpt(regs))
416                         return;
417
418                 if (check_bug_trap(regs)) {
419                         regs->nip += 4;
420                         return;
421                 }
422                 info.si_signo = SIGTRAP;
423                 info.si_errno = 0;
424                 info.si_code = TRAP_BRKPT;
425                 info.si_addr = (void __user *)regs->nip;
426                 _exception(SIGTRAP, &info, regs);
427         } else {
428                 /* Illegal instruction */
429
430                 info.si_signo = SIGILL;
431                 info.si_errno = 0;
432                 info.si_code = ILL_ILLTRP;
433                 info.si_addr = (void __user *)regs->nip;
434                 _exception(SIGILL, &info, regs);
435         }
436 }
437
438 void KernelFPUnavailableException(struct pt_regs *regs)
439 {
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);
443 }
444
445 void AltivecUnavailableException(struct pt_regs *regs)
446 {
447 #ifndef CONFIG_ALTIVEC
448         if (user_mode(regs)) {
449                 /* A user program has executed an altivec instruction,
450                    but this kernel doesn't support altivec. */
451                 siginfo_t info;
452
453                 memset(&info, 0, sizeof(info));
454                 info.si_signo = SIGILL;
455                 info.si_code = ILL_ILLOPC;
456                 info.si_addr = (void *) regs->nip;
457                 _exception(SIGILL, &info, regs);
458                 return;
459         }
460 #endif
461         printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
462                           "%lx at %lx\n", regs->trap, regs->nip);
463         die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
464 }
465
466 void
467 SingleStepException(struct pt_regs *regs)
468 {
469         siginfo_t info;
470
471         regs->msr &= ~MSR_SE;  /* Turn off 'trace' bit */
472
473         if (debugger_sstep(regs))
474                 return;
475
476         info.si_signo = SIGTRAP;
477         info.si_errno = 0;
478         info.si_code = TRAP_TRACE;
479         info.si_addr = (void __user *)regs->nip;
480         _exception(SIGTRAP, &info, regs);       
481 }
482
483 /*
484  * After we have successfully emulated an instruction, we have to
485  * check if the instruction was being single-stepped, and if so,
486  * pretend we got a single-step exception.  This was pointed out
487  * by Kumar Gala.  -- paulus
488  */
489 static inline void emulate_single_step(struct pt_regs *regs)
490 {
491         if (regs->msr & MSR_SE)
492                 SingleStepException(regs);
493 }
494
495 static void dummy_perf(struct pt_regs *regs)
496 {
497 }
498
499 void (*perf_irq)(struct pt_regs *) = dummy_perf;
500
501 void
502 PerformanceMonitorException(struct pt_regs *regs)
503 {
504         perf_irq(regs);
505 }
506
507 void
508 AlignmentException(struct pt_regs *regs)
509 {
510         int fixed;
511         siginfo_t info;
512
513         fixed = fix_alignment(regs);
514
515         if (fixed == 1) {
516                 regs->nip += 4; /* skip over emulated instruction */
517                 emulate_single_step(regs);
518                 return;
519         }
520
521         /* Operand address was bad */   
522         if (fixed == -EFAULT) {
523                 if (user_mode(regs)) {
524                         info.si_signo = SIGSEGV;
525                         info.si_errno = 0;
526                         info.si_code = SEGV_MAPERR;
527                         info.si_addr = (void __user *)regs->dar;
528                         force_sig_info(SIGSEGV, &info, current);
529                 } else {
530                         /* Search exception table */
531                         bad_page_fault(regs, regs->dar, SIGSEGV);
532                 }
533
534                 return;
535         }
536
537         info.si_signo = SIGBUS;
538         info.si_errno = 0;
539         info.si_code = BUS_ADRALN;
540         info.si_addr = (void __user *)regs->nip;
541         _exception(SIGBUS, &info, regs);        
542 }
543
544 #ifdef CONFIG_ALTIVEC
545 void
546 AltivecAssistException(struct pt_regs *regs)
547 {
548         int err;
549         siginfo_t info;
550
551         if (!user_mode(regs)) {
552                 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
553                        " at %lx\n", regs->nip);
554                 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
555         }
556
557         flush_altivec_to_thread(current);
558
559         err = emulate_altivec(regs);
560         if (err == 0) {
561                 regs->nip += 4;         /* skip emulated instruction */
562                 emulate_single_step(regs);
563                 return;
564         }
565
566         if (err == -EFAULT) {
567                 /* got an error reading the instruction */
568                 info.si_signo = SIGSEGV;
569                 info.si_errno = 0;
570                 info.si_code = SEGV_MAPERR;
571                 info.si_addr = (void __user *) regs->nip;
572                 force_sig_info(SIGSEGV, &info, current);
573         } else {
574                 /* didn't recognize the instruction */
575                 /* XXX quick hack for now: set the non-Java bit in the VSCR */
576                 if (printk_ratelimit())
577                         printk(KERN_ERR "Unrecognized altivec instruction "
578                                "in %s at %lx\n", current->comm, regs->nip);
579                 current->thread.vscr.u[3] |= 0x10000;
580         }
581 }
582 #endif /* CONFIG_ALTIVEC */
583
584 /*
585  * We enter here if we get an unrecoverable exception, that is, one
586  * that happened at a point where the RI (recoverable interrupt) bit
587  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
588  * we therefore lost state by taking this exception.
589  */
590 void unrecoverable_exception(struct pt_regs *regs)
591 {
592         printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
593                regs->trap, regs->nip);
594         die("Unrecoverable exception", regs, SIGABRT);
595 }
596
597 /*
598  * We enter here if we discover during exception entry that we are
599  * running in supervisor mode with a userspace value in the stack pointer.
600  */
601 void kernel_bad_stack(struct pt_regs *regs)
602 {
603         printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
604                regs->gpr[1], regs->nip);
605         die("Bad kernel stack pointer", regs, SIGABRT);
606 }
607
608 void __init trap_init(void)
609 {
610 }