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