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