vserver 1.9.5.x5
[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 #include <linux/delay.h>
33 #include <asm/kdebug.h>
34
35 #include <asm/pgtable.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
38 #include <asm/io.h>
39 #include <asm/processor.h>
40 #include <asm/ppcdebug.h>
41 #include <asm/rtas.h>
42 #include <asm/systemcfg.h>
43 #include <asm/machdep.h>
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 struct notifier_block *ppc64_die_chain;
64 static DEFINE_SPINLOCK(die_notifier_lock);
65
66 int register_die_notifier(struct notifier_block *nb)
67 {
68         int err = 0;
69         unsigned long flags;
70
71         spin_lock_irqsave(&die_notifier_lock, flags);
72         err = notifier_chain_register(&ppc64_die_chain, nb);
73         spin_unlock_irqrestore(&die_notifier_lock, flags);
74         return err;
75 }
76
77 /*
78  * Trap & Exception support
79  */
80
81 static DEFINE_SPINLOCK(die_lock);
82
83 int die(const char *str, struct pt_regs *regs, long err)
84 {
85         static int die_counter;
86         int nl = 0;
87
88         if (debugger(regs))
89                 return 1;
90
91         console_verbose();
92         spin_lock_irq(&die_lock);
93         bust_spinlocks(1);
94         printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
95 #ifdef CONFIG_PREEMPT
96         printk("PREEMPT ");
97         nl = 1;
98 #endif
99 #ifdef CONFIG_SMP
100         printk("SMP NR_CPUS=%d ", NR_CPUS);
101         nl = 1;
102 #endif
103 #ifdef CONFIG_DEBUG_PAGEALLOC
104         printk("DEBUG_PAGEALLOC ");
105         nl = 1;
106 #endif
107 #ifdef CONFIG_NUMA
108         printk("NUMA ");
109         nl = 1;
110 #endif
111         switch(systemcfg->platform) {
112                 case PLATFORM_PSERIES:
113                         printk("PSERIES ");
114                         nl = 1;
115                         break;
116                 case PLATFORM_PSERIES_LPAR:
117                         printk("PSERIES LPAR ");
118                         nl = 1;
119                         break;
120                 case PLATFORM_ISERIES_LPAR:
121                         printk("ISERIES LPAR ");
122                         nl = 1;
123                         break;
124                 case PLATFORM_POWERMAC:
125                         printk("POWERMAC ");
126                         nl = 1;
127                         break;
128         }
129         if (nl)
130                 printk("\n");
131         print_modules();
132         show_regs(regs);
133         bust_spinlocks(0);
134         spin_unlock_irq(&die_lock);
135
136         if (in_interrupt())
137                 panic("Fatal exception in interrupt");
138
139         if (panic_on_oops) {
140                 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
141                 ssleep(5);
142                 panic("Fatal exception");
143         }
144         do_exit(SIGSEGV);
145
146         return 0;
147 }
148
149 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
150 {
151         siginfo_t info;
152
153         if (!user_mode(regs)) {
154                 if (die("Exception in kernel mode", regs, signr))
155                         return;
156         }
157
158         memset(&info, 0, sizeof(info));
159         info.si_signo = signr;
160         info.si_code = code;
161         info.si_addr = (void __user *) addr;
162         force_sig_info(signr, &info, current);
163 }
164
165 void system_reset_exception(struct pt_regs *regs)
166 {
167         /* See if any machine dependent calls */
168         if (ppc_md.system_reset_exception)
169                 ppc_md.system_reset_exception(regs);
170
171         die("System Reset", regs, 0);
172
173         /* Must die if the interrupt is not recoverable */
174         if (!(regs->msr & MSR_RI))
175                 panic("Unrecoverable System Reset");
176
177         /* What should we do here? We could issue a shutdown or hard reset. */
178 }
179
180 void machine_check_exception(struct pt_regs *regs)
181 {
182         int recover = 0;
183
184         /* See if any machine dependent calls */
185         if (ppc_md.machine_check_exception)
186                 recover = ppc_md.machine_check_exception(regs);
187
188         if (recover)
189                 return;
190
191         if (debugger_fault_handler(regs))
192                 return;
193         die("Machine check", regs, 0);
194
195         /* Must die if the interrupt is not recoverable */
196         if (!(regs->msr & MSR_RI))
197                 panic("Unrecoverable Machine check");
198 }
199
200 void unknown_exception(struct pt_regs *regs)
201 {
202         printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
203                regs->nip, regs->msr, regs->trap);
204
205         _exception(SIGTRAP, regs, 0, 0);
206 }
207
208 void instruction_breakpoint_exception(struct pt_regs *regs)
209 {
210         if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
211                                         5, SIGTRAP) == NOTIFY_STOP)
212                 return;
213         if (debugger_iabr_match(regs))
214                 return;
215         _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
216 }
217
218 void single_step_exception(struct pt_regs *regs)
219 {
220         regs->msr &= ~MSR_SE;  /* Turn off 'trace' bit */
221
222         if (notify_die(DIE_SSTEP, "single_step", regs, 5,
223                                         5, SIGTRAP) == NOTIFY_STOP)
224                 return;
225         if (debugger_sstep(regs))
226                 return;
227
228         _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
229 }
230
231 /*
232  * After we have successfully emulated an instruction, we have to
233  * check if the instruction was being single-stepped, and if so,
234  * pretend we got a single-step exception.  This was pointed out
235  * by Kumar Gala.  -- paulus
236  */
237 static inline void emulate_single_step(struct pt_regs *regs)
238 {
239         if (regs->msr & MSR_SE)
240                 single_step_exception(regs);
241 }
242
243 static void parse_fpe(struct pt_regs *regs)
244 {
245         int code = 0;
246         unsigned long fpscr;
247
248         flush_fp_to_thread(current);
249
250         fpscr = current->thread.fpscr;
251
252         /* Invalid operation */
253         if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
254                 code = FPE_FLTINV;
255
256         /* Overflow */
257         else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
258                 code = FPE_FLTOVF;
259
260         /* Underflow */
261         else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
262                 code = FPE_FLTUND;
263
264         /* Divide by zero */
265         else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
266                 code = FPE_FLTDIV;
267
268         /* Inexact result */
269         else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
270                 code = FPE_FLTRES;
271
272         _exception(SIGFPE, regs, code, regs->nip);
273 }
274
275 /*
276  * Illegal instruction emulation support.  Return non-zero if we can't
277  * emulate, or -EFAULT if the associated memory access caused an access
278  * fault.  Return zero on success.
279  */
280
281 #define INST_DCBA               0x7c0005ec
282 #define INST_DCBA_MASK          0x7c0007fe
283
284 #define INST_MCRXR              0x7c000400
285 #define INST_MCRXR_MASK         0x7c0007fe
286
287 static int emulate_instruction(struct pt_regs *regs)
288 {
289         unsigned int instword;
290
291         if (!user_mode(regs))
292                 return -EINVAL;
293
294         CHECK_FULL_REGS(regs);
295
296         if (get_user(instword, (unsigned int __user *)(regs->nip)))
297                 return -EFAULT;
298
299         /* Emulating the dcba insn is just a no-op.  */
300         if ((instword & INST_DCBA_MASK) == INST_DCBA) {
301                 static int warned;
302
303                 if (!warned) {
304                         printk(KERN_WARNING
305                                "process %d (%s) uses obsolete 'dcba' insn\n",
306                                current->pid, current->comm);
307                         warned = 1;
308                 }
309                 return 0;
310         }
311
312         /* Emulate the mcrxr insn.  */
313         if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
314                 static int warned;
315                 unsigned int shift;
316
317                 if (!warned) {
318                         printk(KERN_WARNING
319                                "process %d (%s) uses obsolete 'mcrxr' insn\n",
320                                current->pid, current->comm);
321                         warned = 1;
322                 }
323
324                 shift = (instword >> 21) & 0x1c;
325                 regs->ccr &= ~(0xf0000000 >> shift);
326                 regs->ccr |= (regs->xer & 0xf0000000) >> shift;
327                 regs->xer &= ~0xf0000000;
328                 return 0;
329         }
330
331         return -EINVAL;
332 }
333
334 /*
335  * Look through the list of trap instructions that are used for BUG(),
336  * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know
337  * that the exception was caused by a trap instruction of some kind.
338  * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
339  * otherwise.
340  */
341 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
342
343 #ifndef CONFIG_MODULES
344 #define module_find_bug(x)      NULL
345 #endif
346
347 struct bug_entry *find_bug(unsigned long bugaddr)
348 {
349         struct bug_entry *bug;
350
351         for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
352                 if (bugaddr == bug->bug_addr)
353                         return bug;
354         return module_find_bug(bugaddr);
355 }
356
357 static int
358 check_bug_trap(struct pt_regs *regs)
359 {
360         struct bug_entry *bug;
361         unsigned long addr;
362
363         if (regs->msr & MSR_PR)
364                 return 0;       /* not in kernel */
365         addr = regs->nip;       /* address of trap instruction */
366         if (addr < PAGE_OFFSET)
367                 return 0;
368         bug = find_bug(regs->nip);
369         if (bug == NULL)
370                 return 0;
371         if (bug->line & BUG_WARNING_TRAP) {
372                 /* this is a WARN_ON rather than BUG/BUG_ON */
373                 printk(KERN_ERR "Badness in %s at %s:%d\n",
374                        bug->function, bug->file,
375                       (unsigned int)bug->line & ~BUG_WARNING_TRAP);
376                 show_stack(current, (void *)regs->gpr[1]);
377                 return 1;
378         }
379         printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
380                bug->function, bug->file, (unsigned int)bug->line);
381         return 0;
382 }
383
384 void program_check_exception(struct pt_regs *regs)
385 {
386         if (debugger_fault_handler(regs))
387                 return;
388
389         if (regs->msr & 0x100000) {
390                 /* IEEE FP exception */
391                 parse_fpe(regs);
392
393         } else if (regs->msr & 0x40000) {
394                 /* Privileged instruction */
395                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
396
397         } else if (regs->msr & 0x20000) {
398                 /* trap exception */
399
400                 if (notify_die(DIE_BPT, "breakpoint", regs, 5,
401                                         5, SIGTRAP) == NOTIFY_STOP)
402                         return;
403                 if (debugger_bpt(regs))
404                         return;
405
406                 if (check_bug_trap(regs)) {
407                         regs->nip += 4;
408                         return;
409                 }
410                 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
411
412         } else {
413                 /* Illegal instruction; try to emulate it.  */
414                 switch (emulate_instruction(regs)) {
415                 case 0:
416                         regs->nip += 4;
417                         emulate_single_step(regs);
418                         break;
419
420                 case -EFAULT:
421                         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
422                         break;
423
424                 default:
425                         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
426                         break;
427                 }
428         }
429 }
430
431 void kernel_fp_unavailable_exception(struct pt_regs *regs)
432 {
433         printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
434                           "%lx at %lx\n", regs->trap, regs->nip);
435         die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
436 }
437
438 void altivec_unavailable_exception(struct pt_regs *regs)
439 {
440 #ifndef CONFIG_ALTIVEC
441         if (user_mode(regs)) {
442                 /* A user program has executed an altivec instruction,
443                    but this kernel doesn't support altivec. */
444                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
445                 return;
446         }
447 #endif
448         printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
449                           "%lx at %lx\n", regs->trap, regs->nip);
450         die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
451 }
452
453 /* Ensure exceptions are disabled */
454 static void dummy_perf(struct pt_regs *regs)
455 {
456         unsigned int mmcr0 = mfspr(SPRN_MMCR0);
457
458         mmcr0 &= ~(MMCR0_PMXE|MMCR0_PMAO);
459         mtspr(SPRN_MMCR0, mmcr0);
460 }
461
462 void (*perf_irq)(struct pt_regs *) = dummy_perf;
463
464 EXPORT_SYMBOL(perf_irq);
465
466 void performance_monitor_exception(struct pt_regs *regs)
467 {
468         perf_irq(regs);
469 }
470
471 void alignment_exception(struct pt_regs *regs)
472 {
473         int fixed;
474
475         fixed = fix_alignment(regs);
476
477         if (fixed == 1) {
478                 regs->nip += 4; /* skip over emulated instruction */
479                 emulate_single_step(regs);
480                 return;
481         }
482
483         /* Operand address was bad */   
484         if (fixed == -EFAULT) {
485                 if (user_mode(regs)) {
486                         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->dar);
487                 } else {
488                         /* Search exception table */
489                         bad_page_fault(regs, regs->dar, SIGSEGV);
490                 }
491
492                 return;
493         }
494
495         _exception(SIGBUS, regs, BUS_ADRALN, regs->nip);
496 }
497
498 #ifdef CONFIG_ALTIVEC
499 void altivec_assist_exception(struct pt_regs *regs)
500 {
501         int err;
502         siginfo_t info;
503
504         if (!user_mode(regs)) {
505                 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
506                        " at %lx\n", regs->nip);
507                 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
508         }
509
510         flush_altivec_to_thread(current);
511
512         err = emulate_altivec(regs);
513         if (err == 0) {
514                 regs->nip += 4;         /* skip emulated instruction */
515                 emulate_single_step(regs);
516                 return;
517         }
518
519         if (err == -EFAULT) {
520                 /* got an error reading the instruction */
521                 info.si_signo = SIGSEGV;
522                 info.si_errno = 0;
523                 info.si_code = SEGV_MAPERR;
524                 info.si_addr = (void __user *) regs->nip;
525                 force_sig_info(SIGSEGV, &info, current);
526         } else {
527                 /* didn't recognize the instruction */
528                 /* XXX quick hack for now: set the non-Java bit in the VSCR */
529                 if (printk_ratelimit())
530                         printk(KERN_ERR "Unrecognized altivec instruction "
531                                "in %s at %lx\n", current->comm, regs->nip);
532                 current->thread.vscr.u[3] |= 0x10000;
533         }
534 }
535 #endif /* CONFIG_ALTIVEC */
536
537 /*
538  * We enter here if we get an unrecoverable exception, that is, one
539  * that happened at a point where the RI (recoverable interrupt) bit
540  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
541  * we therefore lost state by taking this exception.
542  */
543 void unrecoverable_exception(struct pt_regs *regs)
544 {
545         printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
546                regs->trap, regs->nip);
547         die("Unrecoverable exception", regs, SIGABRT);
548 }
549
550 /*
551  * We enter here if we discover during exception entry that we are
552  * running in supervisor mode with a userspace value in the stack pointer.
553  */
554 void kernel_bad_stack(struct pt_regs *regs)
555 {
556         printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
557                regs->gpr[1], regs->nip);
558         die("Bad kernel stack pointer", regs, SIGABRT);
559 }
560
561 void __init trap_init(void)
562 {
563 }