Merge to Fedora kernel-2.6.7-1.441
[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
40 #ifdef CONFIG_PPC_PSERIES
41 /* This is true if we are using the firmware NMI handler (typically LPAR) */
42 extern int fwnmi_active;
43 #endif
44
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);
53
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);
61 #endif
62
63 /*
64  * Trap & Exception support
65  */
66
67 static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
68
69 int die(const char *str, struct pt_regs *regs, long err)
70 {
71         static int die_counter;
72         int nl = 0;
73
74         if (debugger(regs))
75                 return 1;
76
77         console_verbose();
78         spin_lock_irq(&die_lock);
79         bust_spinlocks(1);
80         printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
81 #ifdef CONFIG_PREEMPT
82         printk("PREEMPT ");
83         nl = 1;
84 #endif
85 #ifdef CONFIG_SMP
86         printk("SMP NR_CPUS=%d ", NR_CPUS);
87         nl = 1;
88 #endif
89 #ifdef CONFIG_DEBUG_PAGEALLOC
90         printk("DEBUG_PAGEALLOC ");
91         nl = 1;
92 #endif
93 #ifdef CONFIG_NUMA
94         printk("NUMA ");
95         nl = 1;
96 #endif
97         switch(systemcfg->platform) {
98                 case PLATFORM_PSERIES:
99                         printk("PSERIES ");
100                         nl = 1;
101                         break;
102                 case PLATFORM_PSERIES_LPAR:
103                         printk("PSERIES LPAR ");
104                         nl = 1;
105                         break;
106                 case PLATFORM_ISERIES_LPAR:
107                         printk("ISERIES LPAR ");
108                         nl = 1;
109                         break;
110                 case PLATFORM_POWERMAC:
111                         printk("POWERMAC ");
112                         nl = 1;
113                         break;
114         }
115         if (nl)
116                 printk("\n");
117         show_regs(regs);
118         bust_spinlocks(0);
119         spin_unlock_irq(&die_lock);
120
121         if (in_interrupt())
122                 panic("Fatal exception in interrupt");
123
124         if (panic_on_oops) {
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");
129         }
130         do_exit(SIGSEGV);
131
132         return 0;
133 }
134
135 static void
136 _exception(int signr, siginfo_t *info, struct pt_regs *regs)
137 {
138         if (!user_mode(regs)) {
139                 if (die("Exception in kernel mode", regs, signr))
140                         return;
141         }
142
143         force_sig_info(signr, info, current);
144 }
145
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.
151  */
152 static struct rtas_error_log *FWNMI_get_errinfo(struct pt_regs *regs)
153 {
154         unsigned long errdata = regs->gpr[3];
155         struct rtas_error_log *errhdr = NULL;
156         unsigned long *savep;
157
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);
163         } else {
164                 printk("FWNMI: corrupt r3\n");
165         }
166         return errhdr;
167 }
168
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.
172  */
173 static void FWNMI_release_errinfo(void)
174 {
175         unsigned long ret = rtas_call(rtas_token("ibm,nmi-interlock"), 0, 1, NULL);
176         if (ret != 0)
177                 printk("FWNMI: nmi-interlock failed: %ld\n", ret);
178 }
179 #endif
180
181 void
182 SystemResetException(struct pt_regs *regs)
183 {
184 #ifdef CONFIG_PPC_PSERIES
185         if (fwnmi_active) {
186                 struct rtas_error_log *errhdr = FWNMI_get_errinfo(regs);
187                 if (errhdr) {
188                         /* XXX Should look at FWNMI information */
189                 }
190                 FWNMI_release_errinfo();
191         }
192 #endif
193
194         die("System Reset", regs, 0);
195
196         /* Must die if the interrupt is not recoverable */
197         if (!(regs->msr & MSR_RI))
198                 panic("Unrecoverable System Reset");
199
200         /* What should we do here? We could issue a shutdown or hard reset. */
201 }
202
203 #ifdef CONFIG_PPC_PSERIES
204 /* 
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.
209  *
210  * Return 1 if corrected (or delivered a signal).
211  * Return 0 if there is nothing we can do.
212  */
213 static int recover_mce(struct pt_regs *regs, struct rtas_error_log err)
214 {
215         siginfo_t info;
216
217         if (err.disposition == DISP_FULLY_RECOVERED) {
218                 /* Platform corrected itself */
219                 return 1;
220         } else if ((regs->msr & MSR_RI) &&
221                    user_mode(regs) &&
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;
229                 info.si_errno = 0;
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",
234                        current->pid);
235                 _exception(SIGBUS, &info, regs);
236                 return 1;
237         }
238         return 0;
239 }
240 #endif
241
242 /*
243  * Handle a machine check.
244  *
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).
248  *
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.
251  */
252 void
253 MachineCheckException(struct pt_regs *regs)
254 {
255 #ifdef CONFIG_PPC_PSERIES
256         struct rtas_error_log err, *errp;
257
258         if (fwnmi_active) {
259                 errp = FWNMI_get_errinfo(regs);
260                 if (errp)
261                         err = *errp;
262                 FWNMI_release_errinfo();        /* frees errp */
263                 if (errp && recover_mce(regs, err))
264                         return;
265         }
266 #endif
267
268         if (debugger_fault_handler(regs))
269                 return;
270         die("Machine check", regs, 0);
271
272         /* Must die if the interrupt is not recoverable */
273         if (!(regs->msr & MSR_RI))
274                 panic("Unrecoverable Machine check");
275 }
276
277 void
278 UnknownException(struct pt_regs *regs)
279 {
280         siginfo_t info;
281
282         printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
283                regs->nip, regs->msr, regs->trap);
284
285         info.si_signo = SIGTRAP;
286         info.si_errno = 0;
287         info.si_code = 0;
288         info.si_addr = 0;
289         _exception(SIGTRAP, &info, regs);       
290 }
291
292 void
293 InstructionBreakpointException(struct pt_regs *regs)
294 {
295         siginfo_t info;
296
297         if (debugger_iabr_match(regs))
298                 return;
299         info.si_signo = SIGTRAP;
300         info.si_errno = 0;
301         info.si_code = TRAP_BRKPT;
302         info.si_addr = (void *)regs->nip;
303         _exception(SIGTRAP, &info, regs);
304 }
305
306 static void parse_fpe(struct pt_regs *regs)
307 {
308         siginfo_t info;
309         unsigned long fpscr;
310
311         flush_fp_to_thread(current);
312
313         fpscr = current->thread.fpscr;
314
315         /* Invalid operation */
316         if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
317                 info.si_code = FPE_FLTINV;
318
319         /* Overflow */
320         else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
321                 info.si_code = FPE_FLTOVF;
322
323         /* Underflow */
324         else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
325                 info.si_code = FPE_FLTUND;
326
327         /* Divide by zero */
328         else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
329                 info.si_code = FPE_FLTDIV;
330
331         /* Inexact result */
332         else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
333                 info.si_code = FPE_FLTRES;
334
335         else
336                 info.si_code = 0;
337
338         info.si_signo = SIGFPE;
339         info.si_errno = 0;
340         info.si_addr = (void *)regs->nip;
341         _exception(SIGFPE, &info, regs);
342 }
343
344 /*
345  * Look through the list of trap instructions that are used for BUG(),
346  * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know
347  * that the exception was caused by a trap instruction of some kind.
348  * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
349  * otherwise.
350  */
351 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
352
353 #ifndef CONFIG_MODULES
354 #define module_find_bug(x)      NULL
355 #endif
356
357 static struct bug_entry *find_bug(unsigned long bugaddr)
358 {
359         struct bug_entry *bug;
360
361         for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
362                 if (bugaddr == bug->bug_addr)
363                         return bug;
364         return module_find_bug(bugaddr);
365 }
366
367 int
368 check_bug_trap(struct pt_regs *regs)
369 {
370         struct bug_entry *bug;
371         unsigned long addr;
372
373         if (regs->msr & MSR_PR)
374                 return 0;       /* not in kernel */
375         addr = regs->nip;       /* address of trap instruction */
376         if (addr < PAGE_OFFSET)
377                 return 0;
378         bug = find_bug(regs->nip);
379         if (bug == NULL)
380                 return 0;
381         if (bug->line & BUG_WARNING_TRAP) {
382                 /* this is a WARN_ON rather than BUG/BUG_ON */
383                 printk(KERN_ERR "Badness in %s at %s:%d\n",
384                        bug->function, bug->file,
385                       (unsigned int)bug->line & ~BUG_WARNING_TRAP);
386                 show_stack(current, (void *)regs->gpr[1]);
387                 return 1;
388         }
389         printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
390                bug->function, bug->file, (unsigned int)bug->line);
391         return 0;
392 }
393
394 void
395 ProgramCheckException(struct pt_regs *regs)
396 {
397         siginfo_t info;
398
399         if (regs->msr & 0x100000) {
400                 /* IEEE FP exception */
401
402                 parse_fpe(regs);
403         } else if (regs->msr & 0x40000) {
404                 /* Privileged instruction */
405
406                 info.si_signo = SIGILL;
407                 info.si_errno = 0;
408                 info.si_code = ILL_PRVOPC;
409                 info.si_addr = (void *)regs->nip;
410                 _exception(SIGILL, &info, regs);
411         } else if (regs->msr & 0x20000) {
412                 /* trap exception */
413
414                 if (debugger_bpt(regs))
415                         return;
416
417                 if (check_bug_trap(regs)) {
418                         regs->nip += 4;
419                         return;
420                 }
421                 info.si_signo = SIGTRAP;
422                 info.si_errno = 0;
423                 info.si_code = TRAP_BRKPT;
424                 info.si_addr = (void *)regs->nip;
425                 _exception(SIGTRAP, &info, regs);
426         } else {
427                 /* Illegal instruction */
428
429                 info.si_signo = SIGILL;
430                 info.si_errno = 0;
431                 info.si_code = ILL_ILLTRP;
432                 info.si_addr = (void *)regs->nip;
433                 _exception(SIGILL, &info, regs);
434         }
435 }
436
437 void KernelFPUnavailableException(struct pt_regs *regs)
438 {
439         printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
440                           "%lx at %lx\n", regs->trap, regs->nip);
441         die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
442 }
443
444 void AltivecUnavailableException(struct pt_regs *regs)
445 {
446 #ifndef CONFIG_ALTIVEC
447         if (user_mode(regs)) {
448                 /* A user program has executed an altivec instruction,
449                    but this kernel doesn't support altivec. */
450                 siginfo_t info;
451
452                 memset(&info, 0, sizeof(info));
453                 info.si_signo = SIGILL;
454                 info.si_code = ILL_ILLOPC;
455                 info.si_addr = (void *) regs->nip;
456                 _exception(SIGILL, &info, regs);
457                 return;
458         }
459 #endif
460         printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
461                           "%lx at %lx\n", regs->trap, regs->nip);
462         die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
463 }
464
465 void
466 SingleStepException(struct pt_regs *regs)
467 {
468         siginfo_t info;
469
470         regs->msr &= ~MSR_SE;  /* Turn off 'trace' bit */
471
472         if (debugger_sstep(regs))
473                 return;
474
475         info.si_signo = SIGTRAP;
476         info.si_errno = 0;
477         info.si_code = TRAP_TRACE;
478         info.si_addr = (void *)regs->nip;
479         _exception(SIGTRAP, &info, regs);       
480 }
481
482 /*
483  * After we have successfully emulated an instruction, we have to
484  * check if the instruction was being single-stepped, and if so,
485  * pretend we got a single-step exception.  This was pointed out
486  * by Kumar Gala.  -- paulus
487  */
488 static inline void emulate_single_step(struct pt_regs *regs)
489 {
490         if (regs->msr & MSR_SE)
491                 SingleStepException(regs);
492 }
493
494 static void dummy_perf(struct pt_regs *regs)
495 {
496 }
497
498 void (*perf_irq)(struct pt_regs *) = dummy_perf;
499
500 void
501 PerformanceMonitorException(struct pt_regs *regs)
502 {
503         perf_irq(regs);
504 }
505
506 void
507 AlignmentException(struct pt_regs *regs)
508 {
509         int fixed;
510         siginfo_t info;
511
512         fixed = fix_alignment(regs);
513
514         if (fixed == 1) {
515                 regs->nip += 4; /* skip over emulated instruction */
516                 emulate_single_step(regs);
517                 return;
518         }
519
520         /* Operand address was bad */   
521         if (fixed == -EFAULT) {
522                 if (user_mode(regs)) {
523                         info.si_signo = SIGSEGV;
524                         info.si_errno = 0;
525                         info.si_code = SEGV_MAPERR;
526                         info.si_addr = (void *)regs->dar;
527                         force_sig_info(SIGSEGV, &info, current);
528                 } else {
529                         /* Search exception table */
530                         bad_page_fault(regs, regs->dar, SIGSEGV);
531                 }
532
533                 return;
534         }
535
536         info.si_signo = SIGBUS;
537         info.si_errno = 0;
538         info.si_code = BUS_ADRALN;
539         info.si_addr = (void *)regs->nip;
540         _exception(SIGBUS, &info, regs);        
541 }
542
543 #ifdef CONFIG_ALTIVEC
544 void
545 AltivecAssistException(struct pt_regs *regs)
546 {
547         flush_altivec_to_thread(current);
548         /* XXX quick hack for now: set the non-Java bit in the VSCR */
549         current->thread.vscr.u[3] |= 0x10000;
550 }
551 #endif /* CONFIG_ALTIVEC */
552
553 /*
554  * We enter here if we get an unrecoverable exception, that is, one
555  * that happened at a point where the RI (recoverable interrupt) bit
556  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
557  * we therefore lost state by taking this exception.
558  */
559 void unrecoverable_exception(struct pt_regs *regs)
560 {
561         printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
562                regs->trap, regs->nip);
563         die("Unrecoverable exception", regs, SIGABRT);
564 }
565
566 /*
567  * We enter here if we discover during exception entry that we are
568  * running in supervisor mode with a userspace value in the stack pointer.
569  */
570 void kernel_bad_stack(struct pt_regs *regs)
571 {
572         printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
573                regs->gpr[1], regs->nip);
574         die("Bad kernel stack pointer", regs, SIGABRT);
575 }
576
577 void __init trap_init(void)
578 {
579 }