powerpc: merge ppc signal.c and ppc64 signal32.c
[safe/jmp/linux-2.6] / arch / powerpc / kernel / traps.c
1 /*
2  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
3  *
4  *  This program is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU General Public License
6  *  as published by the Free Software Foundation; either version
7  *  2 of the License, or (at your option) any later version.
8  *
9  *  Modified by Cort Dougan (cort@cs.nmt.edu)
10  *  and Paul Mackerras (paulus@samba.org)
11  */
12
13 /*
14  * This file handles the architecture-dependent parts of hardware exceptions
15  */
16
17 #include <linux/config.h>
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/stddef.h>
23 #include <linux/unistd.h>
24 #include <linux/ptrace.h>
25 #include <linux/slab.h>
26 #include <linux/user.h>
27 #include <linux/a.out.h>
28 #include <linux/interrupt.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/prctl.h>
32 #include <linux/delay.h>
33 #include <linux/kprobes.h>
34
35 #include <asm/kdebug.h>
36 #include <asm/pgtable.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <asm/io.h>
40 #include <asm/machdep.h>
41 #include <asm/rtas.h>
42 #include <asm/xmon.h>
43 #ifdef CONFIG_PPC32
44 #include <asm/reg.h>
45 #include <asm/perfmon.h>
46 #endif
47 #ifdef CONFIG_PMAC_BACKLIGHT
48 #include <asm/backlight.h>
49 #endif
50 #ifdef CONFIG_PPC64
51 #include <asm/firmware.h>
52 #include <asm/processor.h>
53 #include <asm/systemcfg.h>
54 #include <asm/pmc.h>
55 #endif
56
57 #ifdef CONFIG_PPC64     /* XXX */
58 #define _IO_BASE        pci_io_base
59 #endif
60
61 #ifdef CONFIG_DEBUGGER
62 int (*__debugger)(struct pt_regs *regs);
63 int (*__debugger_ipi)(struct pt_regs *regs);
64 int (*__debugger_bpt)(struct pt_regs *regs);
65 int (*__debugger_sstep)(struct pt_regs *regs);
66 int (*__debugger_iabr_match)(struct pt_regs *regs);
67 int (*__debugger_dabr_match)(struct pt_regs *regs);
68 int (*__debugger_fault_handler)(struct pt_regs *regs);
69
70 EXPORT_SYMBOL(__debugger);
71 EXPORT_SYMBOL(__debugger_ipi);
72 EXPORT_SYMBOL(__debugger_bpt);
73 EXPORT_SYMBOL(__debugger_sstep);
74 EXPORT_SYMBOL(__debugger_iabr_match);
75 EXPORT_SYMBOL(__debugger_dabr_match);
76 EXPORT_SYMBOL(__debugger_fault_handler);
77 #endif
78
79 struct notifier_block *powerpc_die_chain;
80 static DEFINE_SPINLOCK(die_notifier_lock);
81
82 int register_die_notifier(struct notifier_block *nb)
83 {
84         int err = 0;
85         unsigned long flags;
86
87         spin_lock_irqsave(&die_notifier_lock, flags);
88         err = notifier_chain_register(&powerpc_die_chain, nb);
89         spin_unlock_irqrestore(&die_notifier_lock, flags);
90         return err;
91 }
92
93 /*
94  * Trap & Exception support
95  */
96
97 static DEFINE_SPINLOCK(die_lock);
98
99 int die(const char *str, struct pt_regs *regs, long err)
100 {
101         static int die_counter;
102         int nl = 0;
103
104         if (debugger(regs))
105                 return 1;
106
107         console_verbose();
108         spin_lock_irq(&die_lock);
109         bust_spinlocks(1);
110 #ifdef CONFIG_PMAC_BACKLIGHT
111         if (_machine == _MACH_Pmac) {
112                 set_backlight_enable(1);
113                 set_backlight_level(BACKLIGHT_MAX);
114         }
115 #endif
116         printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
117 #ifdef CONFIG_PREEMPT
118         printk("PREEMPT ");
119         nl = 1;
120 #endif
121 #ifdef CONFIG_SMP
122         printk("SMP NR_CPUS=%d ", NR_CPUS);
123         nl = 1;
124 #endif
125 #ifdef CONFIG_DEBUG_PAGEALLOC
126         printk("DEBUG_PAGEALLOC ");
127         nl = 1;
128 #endif
129 #ifdef CONFIG_NUMA
130         printk("NUMA ");
131         nl = 1;
132 #endif
133 #ifdef CONFIG_PPC64
134         switch (systemcfg->platform) {
135         case PLATFORM_PSERIES:
136                 printk("PSERIES ");
137                 nl = 1;
138                 break;
139         case PLATFORM_PSERIES_LPAR:
140                 printk("PSERIES LPAR ");
141                 nl = 1;
142                 break;
143         case PLATFORM_ISERIES_LPAR:
144                 printk("ISERIES LPAR ");
145                 nl = 1;
146                 break;
147         case PLATFORM_POWERMAC:
148                 printk("POWERMAC ");
149                 nl = 1;
150                 break;
151         case PLATFORM_BPA:
152                 printk("BPA ");
153                 nl = 1;
154                 break;
155         }
156 #endif
157         if (nl)
158                 printk("\n");
159         print_modules();
160         show_regs(regs);
161         bust_spinlocks(0);
162         spin_unlock_irq(&die_lock);
163
164         if (in_interrupt())
165                 panic("Fatal exception in interrupt");
166
167         if (panic_on_oops) {
168 #ifdef CONFIG_PPC64
169                 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
170                 ssleep(5);
171 #endif
172                 panic("Fatal exception");
173         }
174         do_exit(err);
175
176         return 0;
177 }
178
179 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
180 {
181         siginfo_t info;
182
183         if (!user_mode(regs)) {
184                 if (die("Exception in kernel mode", regs, signr))
185                         return;
186         }
187
188         memset(&info, 0, sizeof(info));
189         info.si_signo = signr;
190         info.si_code = code;
191         info.si_addr = (void __user *) addr;
192         force_sig_info(signr, &info, current);
193
194         /*
195          * Init gets no signals that it doesn't have a handler for.
196          * That's all very well, but if it has caused a synchronous
197          * exception and we ignore the resulting signal, it will just
198          * generate the same exception over and over again and we get
199          * nowhere.  Better to kill it and let the kernel panic.
200          */
201         if (current->pid == 1) {
202                 __sighandler_t handler;
203
204                 spin_lock_irq(&current->sighand->siglock);
205                 handler = current->sighand->action[signr-1].sa.sa_handler;
206                 spin_unlock_irq(&current->sighand->siglock);
207                 if (handler == SIG_DFL) {
208                         /* init has generated a synchronous exception
209                            and it doesn't have a handler for the signal */
210                         printk(KERN_CRIT "init has generated signal %d "
211                                "but has no handler for it\n", signr);
212                         do_exit(signr);
213                 }
214         }
215 }
216
217 #ifdef CONFIG_PPC64
218 void system_reset_exception(struct pt_regs *regs)
219 {
220         /* See if any machine dependent calls */
221         if (ppc_md.system_reset_exception)
222                 ppc_md.system_reset_exception(regs);
223
224         die("System Reset", regs, SIGABRT);
225
226         /* Must die if the interrupt is not recoverable */
227         if (!(regs->msr & MSR_RI))
228                 panic("Unrecoverable System Reset");
229
230         /* What should we do here? We could issue a shutdown or hard reset. */
231 }
232 #endif
233
234 /*
235  * I/O accesses can cause machine checks on powermacs.
236  * Check if the NIP corresponds to the address of a sync
237  * instruction for which there is an entry in the exception
238  * table.
239  * Note that the 601 only takes a machine check on TEA
240  * (transfer error ack) signal assertion, and does not
241  * set any of the top 16 bits of SRR1.
242  *  -- paulus.
243  */
244 static inline int check_io_access(struct pt_regs *regs)
245 {
246 #ifdef CONFIG_PPC_PMAC
247         unsigned long msr = regs->msr;
248         const struct exception_table_entry *entry;
249         unsigned int *nip = (unsigned int *)regs->nip;
250
251         if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
252             && (entry = search_exception_tables(regs->nip)) != NULL) {
253                 /*
254                  * Check that it's a sync instruction, or somewhere
255                  * in the twi; isync; nop sequence that inb/inw/inl uses.
256                  * As the address is in the exception table
257                  * we should be able to read the instr there.
258                  * For the debug message, we look at the preceding
259                  * load or store.
260                  */
261                 if (*nip == 0x60000000)         /* nop */
262                         nip -= 2;
263                 else if (*nip == 0x4c00012c)    /* isync */
264                         --nip;
265                 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
266                         /* sync or twi */
267                         unsigned int rb;
268
269                         --nip;
270                         rb = (*nip >> 11) & 0x1f;
271                         printk(KERN_DEBUG "%s bad port %lx at %p\n",
272                                (*nip & 0x100)? "OUT to": "IN from",
273                                regs->gpr[rb] - _IO_BASE, nip);
274                         regs->msr |= MSR_RI;
275                         regs->nip = entry->fixup;
276                         return 1;
277                 }
278         }
279 #endif /* CONFIG_PPC_PMAC */
280         return 0;
281 }
282
283 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
284 /* On 4xx, the reason for the machine check or program exception
285    is in the ESR. */
286 #define get_reason(regs)        ((regs)->dsisr)
287 #ifndef CONFIG_FSL_BOOKE
288 #define get_mc_reason(regs)     ((regs)->dsisr)
289 #else
290 #define get_mc_reason(regs)     (mfspr(SPRN_MCSR))
291 #endif
292 #define REASON_FP               ESR_FP
293 #define REASON_ILLEGAL          (ESR_PIL | ESR_PUO)
294 #define REASON_PRIVILEGED       ESR_PPR
295 #define REASON_TRAP             ESR_PTR
296
297 /* single-step stuff */
298 #define single_stepping(regs)   (current->thread.dbcr0 & DBCR0_IC)
299 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
300
301 #else
302 /* On non-4xx, the reason for the machine check or program
303    exception is in the MSR. */
304 #define get_reason(regs)        ((regs)->msr)
305 #define get_mc_reason(regs)     ((regs)->msr)
306 #define REASON_FP               0x100000
307 #define REASON_ILLEGAL          0x80000
308 #define REASON_PRIVILEGED       0x40000
309 #define REASON_TRAP             0x20000
310
311 #define single_stepping(regs)   ((regs)->msr & MSR_SE)
312 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
313 #endif
314
315 /*
316  * This is "fall-back" implementation for configurations
317  * which don't provide platform-specific machine check info
318  */
319 void __attribute__ ((weak))
320 platform_machine_check(struct pt_regs *regs)
321 {
322 }
323
324 void machine_check_exception(struct pt_regs *regs)
325 {
326 #ifdef CONFIG_PPC64
327         int recover = 0;
328
329         /* See if any machine dependent calls */
330         if (ppc_md.machine_check_exception)
331                 recover = ppc_md.machine_check_exception(regs);
332
333         if (recover)
334                 return;
335 #else
336         unsigned long reason = get_mc_reason(regs);
337
338         if (user_mode(regs)) {
339                 regs->msr |= MSR_RI;
340                 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
341                 return;
342         }
343
344 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
345         /* the qspan pci read routines can cause machine checks -- Cort */
346         bad_page_fault(regs, regs->dar, SIGBUS);
347         return;
348 #endif
349
350         if (debugger_fault_handler(regs)) {
351                 regs->msr |= MSR_RI;
352                 return;
353         }
354
355         if (check_io_access(regs))
356                 return;
357
358 #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
359         if (reason & ESR_IMCP) {
360                 printk("Instruction");
361                 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
362         } else
363                 printk("Data");
364         printk(" machine check in kernel mode.\n");
365 #elif defined(CONFIG_440A)
366         printk("Machine check in kernel mode.\n");
367         if (reason & ESR_IMCP){
368                 printk("Instruction Synchronous Machine Check exception\n");
369                 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
370         }
371         else {
372                 u32 mcsr = mfspr(SPRN_MCSR);
373                 if (mcsr & MCSR_IB)
374                         printk("Instruction Read PLB Error\n");
375                 if (mcsr & MCSR_DRB)
376                         printk("Data Read PLB Error\n");
377                 if (mcsr & MCSR_DWB)
378                         printk("Data Write PLB Error\n");
379                 if (mcsr & MCSR_TLBP)
380                         printk("TLB Parity Error\n");
381                 if (mcsr & MCSR_ICP){
382                         flush_instruction_cache();
383                         printk("I-Cache Parity Error\n");
384                 }
385                 if (mcsr & MCSR_DCSP)
386                         printk("D-Cache Search Parity Error\n");
387                 if (mcsr & MCSR_DCFP)
388                         printk("D-Cache Flush Parity Error\n");
389                 if (mcsr & MCSR_IMPE)
390                         printk("Machine Check exception is imprecise\n");
391
392                 /* Clear MCSR */
393                 mtspr(SPRN_MCSR, mcsr);
394         }
395 #elif defined (CONFIG_E500)
396         printk("Machine check in kernel mode.\n");
397         printk("Caused by (from MCSR=%lx): ", reason);
398
399         if (reason & MCSR_MCP)
400                 printk("Machine Check Signal\n");
401         if (reason & MCSR_ICPERR)
402                 printk("Instruction Cache Parity Error\n");
403         if (reason & MCSR_DCP_PERR)
404                 printk("Data Cache Push Parity Error\n");
405         if (reason & MCSR_DCPERR)
406                 printk("Data Cache Parity Error\n");
407         if (reason & MCSR_GL_CI)
408                 printk("Guarded Load or Cache-Inhibited stwcx.\n");
409         if (reason & MCSR_BUS_IAERR)
410                 printk("Bus - Instruction Address Error\n");
411         if (reason & MCSR_BUS_RAERR)
412                 printk("Bus - Read Address Error\n");
413         if (reason & MCSR_BUS_WAERR)
414                 printk("Bus - Write Address Error\n");
415         if (reason & MCSR_BUS_IBERR)
416                 printk("Bus - Instruction Data Error\n");
417         if (reason & MCSR_BUS_RBERR)
418                 printk("Bus - Read Data Bus Error\n");
419         if (reason & MCSR_BUS_WBERR)
420                 printk("Bus - Read Data Bus Error\n");
421         if (reason & MCSR_BUS_IPERR)
422                 printk("Bus - Instruction Parity Error\n");
423         if (reason & MCSR_BUS_RPERR)
424                 printk("Bus - Read Parity Error\n");
425 #elif defined (CONFIG_E200)
426         printk("Machine check in kernel mode.\n");
427         printk("Caused by (from MCSR=%lx): ", reason);
428
429         if (reason & MCSR_MCP)
430                 printk("Machine Check Signal\n");
431         if (reason & MCSR_CP_PERR)
432                 printk("Cache Push Parity Error\n");
433         if (reason & MCSR_CPERR)
434                 printk("Cache Parity Error\n");
435         if (reason & MCSR_EXCP_ERR)
436                 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
437         if (reason & MCSR_BUS_IRERR)
438                 printk("Bus - Read Bus Error on instruction fetch\n");
439         if (reason & MCSR_BUS_DRERR)
440                 printk("Bus - Read Bus Error on data load\n");
441         if (reason & MCSR_BUS_WRERR)
442                 printk("Bus - Write Bus Error on buffered store or cache line push\n");
443 #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
444         printk("Machine check in kernel mode.\n");
445         printk("Caused by (from SRR1=%lx): ", reason);
446         switch (reason & 0x601F0000) {
447         case 0x80000:
448                 printk("Machine check signal\n");
449                 break;
450         case 0:         /* for 601 */
451         case 0x40000:
452         case 0x140000:  /* 7450 MSS error and TEA */
453                 printk("Transfer error ack signal\n");
454                 break;
455         case 0x20000:
456                 printk("Data parity error signal\n");
457                 break;
458         case 0x10000:
459                 printk("Address parity error signal\n");
460                 break;
461         case 0x20000000:
462                 printk("L1 Data Cache error\n");
463                 break;
464         case 0x40000000:
465                 printk("L1 Instruction Cache error\n");
466                 break;
467         case 0x00100000:
468                 printk("L2 data cache parity error\n");
469                 break;
470         default:
471                 printk("Unknown values in msr\n");
472         }
473 #endif /* CONFIG_4xx */
474
475         /*
476          * Optional platform-provided routine to print out
477          * additional info, e.g. bus error registers.
478          */
479         platform_machine_check(regs);
480 #endif /* CONFIG_PPC64 */
481
482         if (debugger_fault_handler(regs))
483                 return;
484         die("Machine check", regs, SIGBUS);
485
486         /* Must die if the interrupt is not recoverable */
487         if (!(regs->msr & MSR_RI))
488                 panic("Unrecoverable Machine check");
489 }
490
491 void SMIException(struct pt_regs *regs)
492 {
493         die("System Management Interrupt", regs, SIGABRT);
494 }
495
496 void unknown_exception(struct pt_regs *regs)
497 {
498         printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
499                regs->nip, regs->msr, regs->trap);
500
501         _exception(SIGTRAP, regs, 0, 0);
502 }
503
504 void instruction_breakpoint_exception(struct pt_regs *regs)
505 {
506         if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
507                                         5, SIGTRAP) == NOTIFY_STOP)
508                 return;
509         if (debugger_iabr_match(regs))
510                 return;
511         _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
512 }
513
514 void RunModeException(struct pt_regs *regs)
515 {
516         _exception(SIGTRAP, regs, 0, 0);
517 }
518
519 void __kprobes single_step_exception(struct pt_regs *regs)
520 {
521         regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
522
523         if (notify_die(DIE_SSTEP, "single_step", regs, 5,
524                                         5, SIGTRAP) == NOTIFY_STOP)
525                 return;
526         if (debugger_sstep(regs))
527                 return;
528
529         _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
530 }
531
532 /*
533  * After we have successfully emulated an instruction, we have to
534  * check if the instruction was being single-stepped, and if so,
535  * pretend we got a single-step exception.  This was pointed out
536  * by Kumar Gala.  -- paulus
537  */
538 static void emulate_single_step(struct pt_regs *regs)
539 {
540         if (single_stepping(regs)) {
541                 clear_single_step(regs);
542                 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
543         }
544 }
545
546 static void parse_fpe(struct pt_regs *regs)
547 {
548         int code = 0;
549         unsigned long fpscr;
550
551         flush_fp_to_thread(current);
552
553         fpscr = current->thread.fpscr;
554
555         /* Invalid operation */
556         if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
557                 code = FPE_FLTINV;
558
559         /* Overflow */
560         else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
561                 code = FPE_FLTOVF;
562
563         /* Underflow */
564         else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
565                 code = FPE_FLTUND;
566
567         /* Divide by zero */
568         else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
569                 code = FPE_FLTDIV;
570
571         /* Inexact result */
572         else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
573                 code = FPE_FLTRES;
574
575         _exception(SIGFPE, regs, code, regs->nip);
576 }
577
578 /*
579  * Illegal instruction emulation support.  Originally written to
580  * provide the PVR to user applications using the mfspr rd, PVR.
581  * Return non-zero if we can't emulate, or -EFAULT if the associated
582  * memory access caused an access fault.  Return zero on success.
583  *
584  * There are a couple of ways to do this, either "decode" the instruction
585  * or directly match lots of bits.  In this case, matching lots of
586  * bits is faster and easier.
587  *
588  */
589 #define INST_MFSPR_PVR          0x7c1f42a6
590 #define INST_MFSPR_PVR_MASK     0xfc1fffff
591
592 #define INST_DCBA               0x7c0005ec
593 #define INST_DCBA_MASK          0x7c0007fe
594
595 #define INST_MCRXR              0x7c000400
596 #define INST_MCRXR_MASK         0x7c0007fe
597
598 #define INST_STRING             0x7c00042a
599 #define INST_STRING_MASK        0x7c0007fe
600 #define INST_STRING_GEN_MASK    0x7c00067e
601 #define INST_LSWI               0x7c0004aa
602 #define INST_LSWX               0x7c00042a
603 #define INST_STSWI              0x7c0005aa
604 #define INST_STSWX              0x7c00052a
605
606 static int emulate_string_inst(struct pt_regs *regs, u32 instword)
607 {
608         u8 rT = (instword >> 21) & 0x1f;
609         u8 rA = (instword >> 16) & 0x1f;
610         u8 NB_RB = (instword >> 11) & 0x1f;
611         u32 num_bytes;
612         unsigned long EA;
613         int pos = 0;
614
615         /* Early out if we are an invalid form of lswx */
616         if ((instword & INST_STRING_MASK) == INST_LSWX)
617                 if ((rT == rA) || (rT == NB_RB))
618                         return -EINVAL;
619
620         EA = (rA == 0) ? 0 : regs->gpr[rA];
621
622         switch (instword & INST_STRING_MASK) {
623                 case INST_LSWX:
624                 case INST_STSWX:
625                         EA += NB_RB;
626                         num_bytes = regs->xer & 0x7f;
627                         break;
628                 case INST_LSWI:
629                 case INST_STSWI:
630                         num_bytes = (NB_RB == 0) ? 32 : NB_RB;
631                         break;
632                 default:
633                         return -EINVAL;
634         }
635
636         while (num_bytes != 0)
637         {
638                 u8 val;
639                 u32 shift = 8 * (3 - (pos & 0x3));
640
641                 switch ((instword & INST_STRING_MASK)) {
642                         case INST_LSWX:
643                         case INST_LSWI:
644                                 if (get_user(val, (u8 __user *)EA))
645                                         return -EFAULT;
646                                 /* first time updating this reg,
647                                  * zero it out */
648                                 if (pos == 0)
649                                         regs->gpr[rT] = 0;
650                                 regs->gpr[rT] |= val << shift;
651                                 break;
652                         case INST_STSWI:
653                         case INST_STSWX:
654                                 val = regs->gpr[rT] >> shift;
655                                 if (put_user(val, (u8 __user *)EA))
656                                         return -EFAULT;
657                                 break;
658                 }
659                 /* move EA to next address */
660                 EA += 1;
661                 num_bytes--;
662
663                 /* manage our position within the register */
664                 if (++pos == 4) {
665                         pos = 0;
666                         if (++rT == 32)
667                                 rT = 0;
668                 }
669         }
670
671         return 0;
672 }
673
674 static int emulate_instruction(struct pt_regs *regs)
675 {
676         u32 instword;
677         u32 rd;
678
679         if (!user_mode(regs))
680                 return -EINVAL;
681         CHECK_FULL_REGS(regs);
682
683         if (get_user(instword, (u32 __user *)(regs->nip)))
684                 return -EFAULT;
685
686         /* Emulate the mfspr rD, PVR. */
687         if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
688                 rd = (instword >> 21) & 0x1f;
689                 regs->gpr[rd] = mfspr(SPRN_PVR);
690                 return 0;
691         }
692
693         /* Emulating the dcba insn is just a no-op.  */
694         if ((instword & INST_DCBA_MASK) == INST_DCBA)
695                 return 0;
696
697         /* Emulate the mcrxr insn.  */
698         if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
699                 int shift = (instword >> 21) & 0x1c;
700                 unsigned long msk = 0xf0000000UL >> shift;
701
702                 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
703                 regs->xer &= ~0xf0000000UL;
704                 return 0;
705         }
706
707         /* Emulate load/store string insn. */
708         if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
709                 return emulate_string_inst(regs, instword);
710
711         return -EINVAL;
712 }
713
714 /*
715  * Look through the list of trap instructions that are used for BUG(),
716  * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know
717  * that the exception was caused by a trap instruction of some kind.
718  * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
719  * otherwise.
720  */
721 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
722
723 #ifndef CONFIG_MODULES
724 #define module_find_bug(x)      NULL
725 #endif
726
727 struct bug_entry *find_bug(unsigned long bugaddr)
728 {
729         struct bug_entry *bug;
730
731         for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
732                 if (bugaddr == bug->bug_addr)
733                         return bug;
734         return module_find_bug(bugaddr);
735 }
736
737 static int check_bug_trap(struct pt_regs *regs)
738 {
739         struct bug_entry *bug;
740         unsigned long addr;
741
742         if (regs->msr & MSR_PR)
743                 return 0;       /* not in kernel */
744         addr = regs->nip;       /* address of trap instruction */
745         if (addr < PAGE_OFFSET)
746                 return 0;
747         bug = find_bug(regs->nip);
748         if (bug == NULL)
749                 return 0;
750         if (bug->line & BUG_WARNING_TRAP) {
751                 /* this is a WARN_ON rather than BUG/BUG_ON */
752 #ifdef CONFIG_XMON
753                 xmon_printf(KERN_ERR "Badness in %s at %s:%d\n",
754                        bug->function, bug->file,
755                        bug->line & ~BUG_WARNING_TRAP);
756 #endif /* CONFIG_XMON */                
757                 printk(KERN_ERR "Badness in %s at %s:%d\n",
758                        bug->function, bug->file,
759                        bug->line & ~BUG_WARNING_TRAP);
760                 dump_stack();
761                 return 1;
762         }
763 #ifdef CONFIG_XMON
764         xmon_printf(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
765                bug->function, bug->file, bug->line);
766         xmon(regs);
767 #endif /* CONFIG_XMON */
768         printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
769                bug->function, bug->file, bug->line);
770
771         return 0;
772 }
773
774 void __kprobes program_check_exception(struct pt_regs *regs)
775 {
776         unsigned int reason = get_reason(regs);
777         extern int do_mathemu(struct pt_regs *regs);
778
779 #ifdef CONFIG_MATH_EMULATION
780         /* (reason & REASON_ILLEGAL) would be the obvious thing here,
781          * but there seems to be a hardware bug on the 405GP (RevD)
782          * that means ESR is sometimes set incorrectly - either to
783          * ESR_DST (!?) or 0.  In the process of chasing this with the
784          * hardware people - not sure if it can happen on any illegal
785          * instruction or only on FP instructions, whether there is a
786          * pattern to occurences etc. -dgibson 31/Mar/2003 */
787         if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
788                 emulate_single_step(regs);
789                 return;
790         }
791 #endif /* CONFIG_MATH_EMULATION */
792
793         if (reason & REASON_FP) {
794                 /* IEEE FP exception */
795                 parse_fpe(regs);
796                 return;
797         }
798         if (reason & REASON_TRAP) {
799                 /* trap exception */
800                 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
801                                 == NOTIFY_STOP)
802                         return;
803                 if (debugger_bpt(regs))
804                         return;
805                 if (check_bug_trap(regs)) {
806                         regs->nip += 4;
807                         return;
808                 }
809                 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
810                 return;
811         }
812
813         /* Try to emulate it if we should. */
814         if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
815                 switch (emulate_instruction(regs)) {
816                 case 0:
817                         regs->nip += 4;
818                         emulate_single_step(regs);
819                         return;
820                 case -EFAULT:
821                         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
822                         return;
823                 }
824         }
825
826         if (reason & REASON_PRIVILEGED)
827                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
828         else
829                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
830 }
831
832 void alignment_exception(struct pt_regs *regs)
833 {
834         int fixed;
835
836         fixed = fix_alignment(regs);
837
838         if (fixed == 1) {
839                 regs->nip += 4; /* skip over emulated instruction */
840                 emulate_single_step(regs);
841                 return;
842         }
843
844         /* Operand address was bad */
845         if (fixed == -EFAULT) {
846                 if (user_mode(regs))
847                         _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
848                 else
849                         /* Search exception table */
850                         bad_page_fault(regs, regs->dar, SIGSEGV);
851                 return;
852         }
853         _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
854 }
855
856 void StackOverflow(struct pt_regs *regs)
857 {
858         printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
859                current, regs->gpr[1]);
860         debugger(regs);
861         show_regs(regs);
862         panic("kernel stack overflow");
863 }
864
865 void nonrecoverable_exception(struct pt_regs *regs)
866 {
867         printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
868                regs->nip, regs->msr);
869         debugger(regs);
870         die("nonrecoverable exception", regs, SIGKILL);
871 }
872
873 void trace_syscall(struct pt_regs *regs)
874 {
875         printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
876                current, current->pid, regs->nip, regs->link, regs->gpr[0],
877                regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
878 }
879
880 void kernel_fp_unavailable_exception(struct pt_regs *regs)
881 {
882         printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
883                           "%lx at %lx\n", regs->trap, regs->nip);
884         die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
885 }
886
887 void altivec_unavailable_exception(struct pt_regs *regs)
888 {
889 #if !defined(CONFIG_ALTIVEC)
890         if (user_mode(regs)) {
891                 /* A user program has executed an altivec instruction,
892                    but this kernel doesn't support altivec. */
893                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
894                 return;
895         }
896 #endif
897         printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
898                         "%lx at %lx\n", regs->trap, regs->nip);
899         die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
900 }
901
902 #ifdef CONFIG_PPC64
903 extern perf_irq_t perf_irq;
904 #endif
905
906 #if defined(CONFIG_PPC64) || defined(CONFIG_E500)
907 void performance_monitor_exception(struct pt_regs *regs)
908 {
909         perf_irq(regs);
910 }
911 #endif
912
913 #ifdef CONFIG_8xx
914 void SoftwareEmulation(struct pt_regs *regs)
915 {
916         extern int do_mathemu(struct pt_regs *);
917         extern int Soft_emulate_8xx(struct pt_regs *);
918         int errcode;
919
920         CHECK_FULL_REGS(regs);
921
922         if (!user_mode(regs)) {
923                 debugger(regs);
924                 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
925         }
926
927 #ifdef CONFIG_MATH_EMULATION
928         errcode = do_mathemu(regs);
929 #else
930         errcode = Soft_emulate_8xx(regs);
931 #endif
932         if (errcode) {
933                 if (errcode > 0)
934                         _exception(SIGFPE, regs, 0, 0);
935                 else if (errcode == -EFAULT)
936                         _exception(SIGSEGV, regs, 0, 0);
937                 else
938                         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
939         } else
940                 emulate_single_step(regs);
941 }
942 #endif /* CONFIG_8xx */
943
944 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
945
946 void DebugException(struct pt_regs *regs, unsigned long debug_status)
947 {
948         if (debug_status & DBSR_IC) {   /* instruction completion */
949                 regs->msr &= ~MSR_DE;
950                 if (user_mode(regs)) {
951                         current->thread.dbcr0 &= ~DBCR0_IC;
952                 } else {
953                         /* Disable instruction completion */
954                         mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
955                         /* Clear the instruction completion event */
956                         mtspr(SPRN_DBSR, DBSR_IC);
957                         if (debugger_sstep(regs))
958                                 return;
959                 }
960                 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
961         }
962 }
963 #endif /* CONFIG_4xx || CONFIG_BOOKE */
964
965 #if !defined(CONFIG_TAU_INT)
966 void TAUException(struct pt_regs *regs)
967 {
968         printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
969                regs->nip, regs->msr, regs->trap, print_tainted());
970 }
971 #endif /* CONFIG_INT_TAU */
972
973 #ifdef CONFIG_ALTIVEC
974 void altivec_assist_exception(struct pt_regs *regs)
975 {
976         int err;
977
978         if (!user_mode(regs)) {
979                 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
980                        " at %lx\n", regs->nip);
981                 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
982         }
983
984         flush_altivec_to_thread(current);
985
986         err = emulate_altivec(regs);
987         if (err == 0) {
988                 regs->nip += 4;         /* skip emulated instruction */
989                 emulate_single_step(regs);
990                 return;
991         }
992
993         if (err == -EFAULT) {
994                 /* got an error reading the instruction */
995                 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
996         } else {
997                 /* didn't recognize the instruction */
998                 /* XXX quick hack for now: set the non-Java bit in the VSCR */
999                 if (printk_ratelimit())
1000                         printk(KERN_ERR "Unrecognized altivec instruction "
1001                                "in %s at %lx\n", current->comm, regs->nip);
1002                 current->thread.vscr.u[3] |= 0x10000;
1003         }
1004 }
1005 #endif /* CONFIG_ALTIVEC */
1006
1007 #ifdef CONFIG_FSL_BOOKE
1008 void CacheLockingException(struct pt_regs *regs, unsigned long address,
1009                            unsigned long error_code)
1010 {
1011         /* We treat cache locking instructions from the user
1012          * as priv ops, in the future we could try to do
1013          * something smarter
1014          */
1015         if (error_code & (ESR_DLK|ESR_ILK))
1016                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1017         return;
1018 }
1019 #endif /* CONFIG_FSL_BOOKE */
1020
1021 #ifdef CONFIG_SPE
1022 void SPEFloatingPointException(struct pt_regs *regs)
1023 {
1024         unsigned long spefscr;
1025         int fpexc_mode;
1026         int code = 0;
1027
1028         spefscr = current->thread.spefscr;
1029         fpexc_mode = current->thread.fpexc_mode;
1030
1031         /* Hardware does not neccessarily set sticky
1032          * underflow/overflow/invalid flags */
1033         if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1034                 code = FPE_FLTOVF;
1035                 spefscr |= SPEFSCR_FOVFS;
1036         }
1037         else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1038                 code = FPE_FLTUND;
1039                 spefscr |= SPEFSCR_FUNFS;
1040         }
1041         else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1042                 code = FPE_FLTDIV;
1043         else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1044                 code = FPE_FLTINV;
1045                 spefscr |= SPEFSCR_FINVS;
1046         }
1047         else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1048                 code = FPE_FLTRES;
1049
1050         current->thread.spefscr = spefscr;
1051
1052         _exception(SIGFPE, regs, code, regs->nip);
1053         return;
1054 }
1055 #endif
1056
1057 /*
1058  * We enter here if we get an unrecoverable exception, that is, one
1059  * that happened at a point where the RI (recoverable interrupt) bit
1060  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1061  * we therefore lost state by taking this exception.
1062  */
1063 void unrecoverable_exception(struct pt_regs *regs)
1064 {
1065         printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1066                regs->trap, regs->nip);
1067         die("Unrecoverable exception", regs, SIGABRT);
1068 }
1069
1070 #ifdef CONFIG_BOOKE_WDT
1071 /*
1072  * Default handler for a Watchdog exception,
1073  * spins until a reboot occurs
1074  */
1075 void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1076 {
1077         /* Generic WatchdogHandler, implement your own */
1078         mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1079         return;
1080 }
1081
1082 void WatchdogException(struct pt_regs *regs)
1083 {
1084         printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1085         WatchdogHandler(regs);
1086 }
1087 #endif
1088
1089 /*
1090  * We enter here if we discover during exception entry that we are
1091  * running in supervisor mode with a userspace value in the stack pointer.
1092  */
1093 void kernel_bad_stack(struct pt_regs *regs)
1094 {
1095         printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1096                regs->gpr[1], regs->nip);
1097         die("Bad kernel stack pointer", regs, SIGABRT);
1098 }
1099
1100 void __init trap_init(void)
1101 {
1102 }