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