]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/ppc/kernel/traps.c
Merge branch 'for-2.6.25' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerp...
[linux-2.6-omap-h63xx.git] / arch / ppc / 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@cs.anu.edu.au)
11  */
12
13 /*
14  * This file handles the architecture-dependent parts of hardware exceptions
15  */
16
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/stddef.h>
22 #include <linux/unistd.h>
23 #include <linux/ptrace.h>
24 #include <linux/slab.h>
25 #include <linux/user.h>
26 #include <linux/a.out.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/prctl.h>
31 #include <linux/bug.h>
32
33 #include <asm/pgtable.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <asm/io.h>
37 #include <asm/reg.h>
38 #include <asm/xmon.h>
39 #include <asm/pmc.h>
40
41 #ifdef CONFIG_XMON
42 extern int xmon_bpt(struct pt_regs *regs);
43 extern int xmon_sstep(struct pt_regs *regs);
44 extern int xmon_iabr_match(struct pt_regs *regs);
45 extern int xmon_dabr_match(struct pt_regs *regs);
46
47 int (*debugger)(struct pt_regs *regs) = xmon;
48 int (*debugger_bpt)(struct pt_regs *regs) = xmon_bpt;
49 int (*debugger_sstep)(struct pt_regs *regs) = xmon_sstep;
50 int (*debugger_iabr_match)(struct pt_regs *regs) = xmon_iabr_match;
51 int (*debugger_dabr_match)(struct pt_regs *regs) = xmon_dabr_match;
52 void (*debugger_fault_handler)(struct pt_regs *regs);
53 #else
54 #ifdef CONFIG_KGDB
55 int (*debugger)(struct pt_regs *regs);
56 int (*debugger_bpt)(struct pt_regs *regs);
57 int (*debugger_sstep)(struct pt_regs *regs);
58 int (*debugger_iabr_match)(struct pt_regs *regs);
59 int (*debugger_dabr_match)(struct pt_regs *regs);
60 void (*debugger_fault_handler)(struct pt_regs *regs);
61 #else
62 #define debugger(regs)                  do { } while (0)
63 #define debugger_bpt(regs)              0
64 #define debugger_sstep(regs)            0
65 #define debugger_iabr_match(regs)       0
66 #define debugger_dabr_match(regs)       0
67 #define debugger_fault_handler          ((void (*)(struct pt_regs *))0)
68 #endif
69 #endif
70
71 /*
72  * Trap & Exception support
73  */
74
75 DEFINE_SPINLOCK(die_lock);
76
77 int die(const char * str, struct pt_regs * fp, long err)
78 {
79         static int die_counter;
80         int nl = 0;
81         console_verbose();
82         spin_lock_irq(&die_lock);
83         printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
84 #ifdef CONFIG_PREEMPT
85         printk("PREEMPT ");
86         nl = 1;
87 #endif
88 #ifdef CONFIG_SMP
89         printk("SMP NR_CPUS=%d ", NR_CPUS);
90         nl = 1;
91 #endif
92         if (nl)
93                 printk("\n");
94         show_regs(fp);
95         add_taint(TAINT_DIE);
96         spin_unlock_irq(&die_lock);
97         /* do_exit() should take care of panic'ing from an interrupt
98          * context so we don't handle it here
99          */
100         do_exit(err);
101 }
102
103 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
104 {
105         siginfo_t info;
106
107         if (!user_mode(regs)) {
108                 debugger(regs);
109                 die("Exception in kernel mode", regs, signr);
110         }
111         info.si_signo = signr;
112         info.si_errno = 0;
113         info.si_code = code;
114         info.si_addr = (void __user *) addr;
115         force_sig_info(signr, &info, current);
116
117         /*
118          * Init gets no signals that it doesn't have a handler for.
119          * That's all very well, but if it has caused a synchronous
120          * exception and we ignore the resulting signal, it will just
121          * generate the same exception over and over again and we get
122          * nowhere.  Better to kill it and let the kernel panic.
123          */
124         if (is_global_init(current)) {
125                 __sighandler_t handler;
126
127                 spin_lock_irq(&current->sighand->siglock);
128                 handler = current->sighand->action[signr-1].sa.sa_handler;
129                 spin_unlock_irq(&current->sighand->siglock);
130                 if (handler == SIG_DFL) {
131                         /* init has generated a synchronous exception
132                            and it doesn't have a handler for the signal */
133                         printk(KERN_CRIT "init has generated signal %d "
134                                "but has no handler for it\n", signr);
135                         do_exit(signr);
136                 }
137         }
138 }
139
140 /*
141  * I/O accesses can cause machine checks on powermacs.
142  * Check if the NIP corresponds to the address of a sync
143  * instruction for which there is an entry in the exception
144  * table.
145  * Note that the 601 only takes a machine check on TEA
146  * (transfer error ack) signal assertion, and does not
147  * set any of the top 16 bits of SRR1.
148  *  -- paulus.
149  */
150 static inline int check_io_access(struct pt_regs *regs)
151 {
152 #if defined CONFIG_8xx
153         unsigned long msr = regs->msr;
154         const struct exception_table_entry *entry;
155         unsigned int *nip = (unsigned int *)regs->nip;
156
157         if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
158             && (entry = search_exception_tables(regs->nip)) != NULL) {
159                 /*
160                  * Check that it's a sync instruction, or somewhere
161                  * in the twi; isync; nop sequence that inb/inw/inl uses.
162                  * As the address is in the exception table
163                  * we should be able to read the instr there.
164                  * For the debug message, we look at the preceding
165                  * load or store.
166                  */
167                 if (*nip == 0x60000000)         /* nop */
168                         nip -= 2;
169                 else if (*nip == 0x4c00012c)    /* isync */
170                         --nip;
171                 /* eieio from I/O string functions */
172                 else if ((*nip) == 0x7c0006ac || *(nip+1) == 0x7c0006ac)
173                         nip += 2;
174                 if (*nip == 0x7c0004ac || (*nip >> 26) == 3 ||
175                         (*(nip+1) >> 26) == 3) {
176                         /* sync or twi */
177                         unsigned int rb;
178
179                         --nip;
180                         rb = (*nip >> 11) & 0x1f;
181                         printk(KERN_DEBUG "%s bad port %lx at %p\n",
182                                (*nip & 0x100)? "OUT to": "IN from",
183                                regs->gpr[rb] - _IO_BASE, nip);
184                         regs->msr |= MSR_RI;
185                         regs->nip = entry->fixup;
186                         return 1;
187                 }
188         }
189 #endif /* CONFIG_8xx */
190         return 0;
191 }
192
193 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
194 /* On 4xx, the reason for the machine check or program exception
195    is in the ESR. */
196 #define get_reason(regs)        ((regs)->dsisr)
197 #ifndef CONFIG_FSL_BOOKE
198 #define get_mc_reason(regs)     ((regs)->dsisr)
199 #else
200 #define get_mc_reason(regs)     (mfspr(SPRN_MCSR))
201 #endif
202 #define REASON_FP               ESR_FP
203 #define REASON_ILLEGAL          (ESR_PIL | ESR_PUO)
204 #define REASON_PRIVILEGED       ESR_PPR
205 #define REASON_TRAP             ESR_PTR
206
207 /* single-step stuff */
208 #define single_stepping(regs)   (current->thread.dbcr0 & DBCR0_IC)
209 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
210
211 #else
212 /* On non-4xx, the reason for the machine check or program
213    exception is in the MSR. */
214 #define get_reason(regs)        ((regs)->msr)
215 #define get_mc_reason(regs)     ((regs)->msr)
216 #define REASON_FP               0x100000
217 #define REASON_ILLEGAL          0x80000
218 #define REASON_PRIVILEGED       0x40000
219 #define REASON_TRAP             0x20000
220
221 #define single_stepping(regs)   ((regs)->msr & MSR_SE)
222 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
223 #endif
224
225 /*
226  * This is "fall-back" implementation for configurations
227  * which don't provide platform-specific machine check info
228  */
229 void __attribute__ ((weak))
230 platform_machine_check(struct pt_regs *regs)
231 {
232 }
233
234 #if defined(CONFIG_4xx)
235 int machine_check_4xx(struct pt_regs *regs)
236 {
237         unsigned long reason = get_mc_reason(regs);
238
239         if (reason & ESR_IMCP) {
240                 printk("Instruction");
241                 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
242         } else
243                 printk("Data");
244         printk(" machine check in kernel mode.\n");
245
246         return 0;
247 }
248
249 int machine_check_440A(struct pt_regs *regs)
250 {
251         unsigned long reason = get_mc_reason(regs);
252
253         printk("Machine check in kernel mode.\n");
254         if (reason & ESR_IMCP){
255                 printk("Instruction Synchronous Machine Check exception\n");
256                 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
257         }
258         else {
259                 u32 mcsr = mfspr(SPRN_MCSR);
260                 if (mcsr & MCSR_IB)
261                         printk("Instruction Read PLB Error\n");
262                 if (mcsr & MCSR_DRB)
263                         printk("Data Read PLB Error\n");
264                 if (mcsr & MCSR_DWB)
265                         printk("Data Write PLB Error\n");
266                 if (mcsr & MCSR_TLBP)
267                         printk("TLB Parity Error\n");
268                 if (mcsr & MCSR_ICP){
269                         flush_instruction_cache();
270                         printk("I-Cache Parity Error\n");
271                 }
272                 if (mcsr & MCSR_DCSP)
273                         printk("D-Cache Search Parity Error\n");
274                 if (mcsr & MCSR_DCFP)
275                         printk("D-Cache Flush Parity Error\n");
276                 if (mcsr & MCSR_IMPE)
277                         printk("Machine Check exception is imprecise\n");
278
279                 /* Clear MCSR */
280                 mtspr(SPRN_MCSR, mcsr);
281         }
282         return 0;
283 }
284 #elif defined(CONFIG_E500)
285 int machine_check_e500(struct pt_regs *regs)
286 {
287         unsigned long reason = get_mc_reason(regs);
288
289         printk("Machine check in kernel mode.\n");
290         printk("Caused by (from MCSR=%lx): ", reason);
291
292         if (reason & MCSR_MCP)
293                 printk("Machine Check Signal\n");
294         if (reason & MCSR_ICPERR)
295                 printk("Instruction Cache Parity Error\n");
296         if (reason & MCSR_DCP_PERR)
297                 printk("Data Cache Push Parity Error\n");
298         if (reason & MCSR_DCPERR)
299                 printk("Data Cache Parity Error\n");
300         if (reason & MCSR_BUS_IAERR)
301                 printk("Bus - Instruction Address Error\n");
302         if (reason & MCSR_BUS_RAERR)
303                 printk("Bus - Read Address Error\n");
304         if (reason & MCSR_BUS_WAERR)
305                 printk("Bus - Write Address Error\n");
306         if (reason & MCSR_BUS_IBERR)
307                 printk("Bus - Instruction Data Error\n");
308         if (reason & MCSR_BUS_RBERR)
309                 printk("Bus - Read Data Bus Error\n");
310         if (reason & MCSR_BUS_WBERR)
311                 printk("Bus - Read Data Bus Error\n");
312         if (reason & MCSR_BUS_IPERR)
313                 printk("Bus - Instruction Parity Error\n");
314         if (reason & MCSR_BUS_RPERR)
315                 printk("Bus - Read Parity Error\n");
316
317         return 0;
318 }
319 #elif defined(CONFIG_E200)
320 int machine_check_e200(struct pt_regs *regs)
321 {
322         unsigned long reason = get_mc_reason(regs);
323
324         printk("Machine check in kernel mode.\n");
325         printk("Caused by (from MCSR=%lx): ", reason);
326
327         if (reason & MCSR_MCP)
328                 printk("Machine Check Signal\n");
329         if (reason & MCSR_CP_PERR)
330                 printk("Cache Push Parity Error\n");
331         if (reason & MCSR_CPERR)
332                 printk("Cache Parity Error\n");
333         if (reason & MCSR_EXCP_ERR)
334                 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
335         if (reason & MCSR_BUS_IRERR)
336                 printk("Bus - Read Bus Error on instruction fetch\n");
337         if (reason & MCSR_BUS_DRERR)
338                 printk("Bus - Read Bus Error on data load\n");
339         if (reason & MCSR_BUS_WRERR)
340                 printk("Bus - Write Bus Error on buffered store or cache line push\n");
341
342         return 0;
343 }
344 #else
345 int machine_check_generic(struct pt_regs *regs)
346 {
347         unsigned long reason = get_mc_reason(regs);
348
349         printk("Machine check in kernel mode.\n");
350         printk("Caused by (from SRR1=%lx): ", reason);
351         switch (reason & 0x601F0000) {
352         case 0x80000:
353                 printk("Machine check signal\n");
354                 break;
355         case 0:         /* for 601 */
356         case 0x40000:
357         case 0x140000:  /* 7450 MSS error and TEA */
358                 printk("Transfer error ack signal\n");
359                 break;
360         case 0x20000:
361                 printk("Data parity error signal\n");
362                 break;
363         case 0x10000:
364                 printk("Address parity error signal\n");
365                 break;
366         case 0x20000000:
367                 printk("L1 Data Cache error\n");
368                 break;
369         case 0x40000000:
370                 printk("L1 Instruction Cache error\n");
371                 break;
372         case 0x00100000:
373                 printk("L2 data cache parity error\n");
374                 break;
375         default:
376                 printk("Unknown values in msr\n");
377         }
378         return 0;
379 }
380 #endif /* everything else */
381
382 void machine_check_exception(struct pt_regs *regs)
383 {
384         int recover = 0;
385
386         if (cur_cpu_spec->machine_check)
387                 recover = cur_cpu_spec->machine_check(regs);
388         if (recover > 0)
389                 return;
390
391         if (user_mode(regs)) {
392                 regs->msr |= MSR_RI;
393                 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
394                 return;
395         }
396
397 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
398         /* the qspan pci read routines can cause machine checks -- Cort */
399         bad_page_fault(regs, regs->dar, SIGBUS);
400         return;
401 #endif
402
403         if (debugger_fault_handler) {
404                 debugger_fault_handler(regs);
405                 regs->msr |= MSR_RI;
406                 return;
407         }
408
409         if (check_io_access(regs))
410                 return;
411
412         /*
413          * Optional platform-provided routine to print out
414          * additional info, e.g. bus error registers.
415          */
416         platform_machine_check(regs);
417
418         debugger(regs);
419         die("machine check", regs, SIGBUS);
420 }
421
422 void SMIException(struct pt_regs *regs)
423 {
424         debugger(regs);
425 #if !(defined(CONFIG_XMON) || defined(CONFIG_KGDB))
426         show_regs(regs);
427         panic("System Management Interrupt");
428 #endif
429 }
430
431 void unknown_exception(struct pt_regs *regs)
432 {
433         printk("Bad trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
434                regs->nip, regs->msr, regs->trap, print_tainted());
435         _exception(SIGTRAP, regs, 0, 0);
436 }
437
438 void instruction_breakpoint_exception(struct pt_regs *regs)
439 {
440         if (debugger_iabr_match(regs))
441                 return;
442         _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
443 }
444
445 void RunModeException(struct pt_regs *regs)
446 {
447         _exception(SIGTRAP, regs, 0, 0);
448 }
449
450 /* Illegal instruction emulation support.  Originally written to
451  * provide the PVR to user applications using the mfspr rd, PVR.
452  * Return non-zero if we can't emulate, or -EFAULT if the associated
453  * memory access caused an access fault.  Return zero on success.
454  *
455  * There are a couple of ways to do this, either "decode" the instruction
456  * or directly match lots of bits.  In this case, matching lots of
457  * bits is faster and easier.
458  *
459  */
460 #define INST_MFSPR_PVR          0x7c1f42a6
461 #define INST_MFSPR_PVR_MASK     0xfc1fffff
462
463 #define INST_DCBA               0x7c0005ec
464 #define INST_DCBA_MASK          0x7c0007fe
465
466 #define INST_MCRXR              0x7c000400
467 #define INST_MCRXR_MASK         0x7c0007fe
468
469 #define INST_STRING             0x7c00042a
470 #define INST_STRING_MASK        0x7c0007fe
471 #define INST_STRING_GEN_MASK    0x7c00067e
472 #define INST_LSWI               0x7c0004aa
473 #define INST_LSWX               0x7c00042a
474 #define INST_STSWI              0x7c0005aa
475 #define INST_STSWX              0x7c00052a
476
477 static int emulate_string_inst(struct pt_regs *regs, u32 instword)
478 {
479         u8 rT = (instword >> 21) & 0x1f;
480         u8 rA = (instword >> 16) & 0x1f;
481         u8 NB_RB = (instword >> 11) & 0x1f;
482         u32 num_bytes;
483         unsigned long EA;
484         int pos = 0;
485
486         /* Early out if we are an invalid form of lswx */
487         if ((instword & INST_STRING_MASK) == INST_LSWX)
488                 if ((rT == rA) || (rT == NB_RB))
489                         return -EINVAL;
490
491         EA = (rA == 0) ? 0 : regs->gpr[rA];
492
493         switch (instword & INST_STRING_MASK) {
494                 case INST_LSWX:
495                 case INST_STSWX:
496                         EA += NB_RB;
497                         num_bytes = regs->xer & 0x7f;
498                         break;
499                 case INST_LSWI:
500                 case INST_STSWI:
501                         num_bytes = (NB_RB == 0) ? 32 : NB_RB;
502                         break;
503                 default:
504                         return -EINVAL;
505         }
506
507         while (num_bytes != 0)
508         {
509                 u8 val;
510                 u32 shift = 8 * (3 - (pos & 0x3));
511
512                 switch ((instword & INST_STRING_MASK)) {
513                         case INST_LSWX:
514                         case INST_LSWI:
515                                 if (get_user(val, (u8 __user *)EA))
516                                         return -EFAULT;
517                                 /* first time updating this reg,
518                                  * zero it out */
519                                 if (pos == 0)
520                                         regs->gpr[rT] = 0;
521                                 regs->gpr[rT] |= val << shift;
522                                 break;
523                         case INST_STSWI:
524                         case INST_STSWX:
525                                 val = regs->gpr[rT] >> shift;
526                                 if (put_user(val, (u8 __user *)EA))
527                                         return -EFAULT;
528                                 break;
529                 }
530                 /* move EA to next address */
531                 EA += 1;
532                 num_bytes--;
533
534                 /* manage our position within the register */
535                 if (++pos == 4) {
536                         pos = 0;
537                         if (++rT == 32)
538                                 rT = 0;
539                 }
540         }
541
542         return 0;
543 }
544
545 static int emulate_instruction(struct pt_regs *regs)
546 {
547         u32 instword;
548         u32 rd;
549
550         if (!user_mode(regs))
551                 return -EINVAL;
552         CHECK_FULL_REGS(regs);
553
554         if (get_user(instword, (u32 __user *)(regs->nip)))
555                 return -EFAULT;
556
557         /* Emulate the mfspr rD, PVR.
558          */
559         if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
560                 rd = (instword >> 21) & 0x1f;
561                 regs->gpr[rd] = mfspr(SPRN_PVR);
562                 return 0;
563         }
564
565         /* Emulating the dcba insn is just a no-op.  */
566         if ((instword & INST_DCBA_MASK) == INST_DCBA)
567                 return 0;
568
569         /* Emulate the mcrxr insn.  */
570         if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
571                 int shift = (instword >> 21) & 0x1c;
572                 unsigned long msk = 0xf0000000UL >> shift;
573
574                 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
575                 regs->xer &= ~0xf0000000UL;
576                 return 0;
577         }
578
579         /* Emulate load/store string insn. */
580         if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
581                 return emulate_string_inst(regs, instword);
582
583         return -EINVAL;
584 }
585
586 /*
587  * After we have successfully emulated an instruction, we have to
588  * check if the instruction was being single-stepped, and if so,
589  * pretend we got a single-step exception.  This was pointed out
590  * by Kumar Gala.  -- paulus
591  */
592 static void emulate_single_step(struct pt_regs *regs)
593 {
594         if (single_stepping(regs)) {
595                 clear_single_step(regs);
596                 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
597         }
598 }
599
600 int is_valid_bugaddr(unsigned long addr)
601 {
602         return addr >= PAGE_OFFSET;
603 }
604
605 void program_check_exception(struct pt_regs *regs)
606 {
607         unsigned int reason = get_reason(regs);
608         extern int do_mathemu(struct pt_regs *regs);
609
610 #ifdef CONFIG_MATH_EMULATION
611         /* (reason & REASON_ILLEGAL) would be the obvious thing here,
612          * but there seems to be a hardware bug on the 405GP (RevD)
613          * that means ESR is sometimes set incorrectly - either to
614          * ESR_DST (!?) or 0.  In the process of chasing this with the
615          * hardware people - not sure if it can happen on any illegal
616          * instruction or only on FP instructions, whether there is a
617          * pattern to occurrences etc. -dgibson 31/Mar/2003 */
618         if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
619                 emulate_single_step(regs);
620                 return;
621         }
622 #endif /* CONFIG_MATH_EMULATION */
623
624         if (reason & REASON_FP) {
625                 /* IEEE FP exception */
626                 int code = 0;
627                 u32 fpscr;
628
629                 /* We must make sure the FP state is consistent with
630                  * our MSR_FP in regs
631                  */
632                 preempt_disable();
633                 if (regs->msr & MSR_FP)
634                         giveup_fpu(current);
635                 preempt_enable();
636
637                 fpscr = current->thread.fpscr.val;
638                 fpscr &= fpscr << 22;   /* mask summary bits with enables */
639                 if (fpscr & FPSCR_VX)
640                         code = FPE_FLTINV;
641                 else if (fpscr & FPSCR_OX)
642                         code = FPE_FLTOVF;
643                 else if (fpscr & FPSCR_UX)
644                         code = FPE_FLTUND;
645                 else if (fpscr & FPSCR_ZX)
646                         code = FPE_FLTDIV;
647                 else if (fpscr & FPSCR_XX)
648                         code = FPE_FLTRES;
649                 _exception(SIGFPE, regs, code, regs->nip);
650                 return;
651         }
652
653         if (reason & REASON_TRAP) {
654                 /* trap exception */
655                 if (debugger_bpt(regs))
656                         return;
657
658                 if (!(regs->msr & MSR_PR) &&  /* not user-mode */
659                     report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
660                         regs->nip += 4;
661                         return;
662                 }
663                 _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
664                 return;
665         }
666
667         /* Try to emulate it if we should. */
668         if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
669                 switch (emulate_instruction(regs)) {
670                 case 0:
671                         regs->nip += 4;
672                         emulate_single_step(regs);
673                         return;
674                 case -EFAULT:
675                         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
676                         return;
677                 }
678         }
679
680         if (reason & REASON_PRIVILEGED)
681                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
682         else
683                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
684 }
685
686 void single_step_exception(struct pt_regs *regs)
687 {
688         regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
689         if (debugger_sstep(regs))
690                 return;
691         _exception(SIGTRAP, regs, TRAP_TRACE, 0);
692 }
693
694 void alignment_exception(struct pt_regs *regs)
695 {
696         int sig, code, fixed = 0;
697
698         fixed = fix_alignment(regs);
699         if (fixed == 1) {
700                 regs->nip += 4; /* skip over emulated instruction */
701                 emulate_single_step(regs);
702                 return;
703         }
704         if (fixed == -EFAULT) {
705                 sig = SIGSEGV;
706                 code = SEGV_ACCERR;
707         } else {
708                 sig = SIGBUS;
709                 code = BUS_ADRALN;
710         }
711         if (user_mode(regs))
712                 _exception(sig, regs, code, regs->dar);
713         else
714                 bad_page_fault(regs, regs->dar, sig);
715 }
716
717 void StackOverflow(struct pt_regs *regs)
718 {
719         printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
720                current, regs->gpr[1]);
721         debugger(regs);
722         show_regs(regs);
723         panic("kernel stack overflow");
724 }
725
726 void nonrecoverable_exception(struct pt_regs *regs)
727 {
728         printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
729                regs->nip, regs->msr);
730         debugger(regs);
731         die("nonrecoverable exception", regs, SIGKILL);
732 }
733
734 void trace_syscall(struct pt_regs *regs)
735 {
736         printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
737                current, current->pid, regs->nip, regs->link, regs->gpr[0],
738                regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
739 }
740
741 #ifdef CONFIG_8xx
742 void SoftwareEmulation(struct pt_regs *regs)
743 {
744         extern int do_mathemu(struct pt_regs *);
745         extern int Soft_emulate_8xx(struct pt_regs *);
746         int errcode;
747
748         CHECK_FULL_REGS(regs);
749
750         if (!user_mode(regs)) {
751                 debugger(regs);
752                 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
753         }
754
755 #ifdef CONFIG_MATH_EMULATION
756         errcode = do_mathemu(regs);
757 #else
758         errcode = Soft_emulate_8xx(regs);
759 #endif
760         if (errcode) {
761                 if (errcode > 0)
762                         _exception(SIGFPE, regs, 0, 0);
763                 else if (errcode == -EFAULT)
764                         _exception(SIGSEGV, regs, 0, 0);
765                 else
766                         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
767         } else
768                 emulate_single_step(regs);
769 }
770 #endif /* CONFIG_8xx */
771
772 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
773
774 void DebugException(struct pt_regs *regs, unsigned long debug_status)
775 {
776         if (debug_status & DBSR_IC) {   /* instruction completion */
777                 regs->msr &= ~MSR_DE;
778                 if (user_mode(regs)) {
779                         current->thread.dbcr0 &= ~DBCR0_IC;
780                 } else {
781                         /* Disable instruction completion */
782                         mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
783                         /* Clear the instruction completion event */
784                         mtspr(SPRN_DBSR, DBSR_IC);
785                         if (debugger_sstep(regs))
786                                 return;
787                 }
788                 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
789         }
790 }
791 #endif /* CONFIG_4xx || CONFIG_BOOKE */
792
793 #if !defined(CONFIG_TAU_INT)
794 void TAUException(struct pt_regs *regs)
795 {
796         printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
797                regs->nip, regs->msr, regs->trap, print_tainted());
798 }
799 #endif /* CONFIG_INT_TAU */
800
801 /*
802  * FP unavailable trap from kernel - print a message, but let
803  * the task use FP in the kernel until it returns to user mode.
804  */
805 void kernel_fp_unavailable_exception(struct pt_regs *regs)
806 {
807         regs->msr |= MSR_FP;
808         printk(KERN_ERR "floating point used in kernel (task=%p, pc=%lx)\n",
809                current, regs->nip);
810 }
811
812 void altivec_unavailable_exception(struct pt_regs *regs)
813 {
814         static int kernel_altivec_count;
815
816 #ifndef CONFIG_ALTIVEC
817         if (user_mode(regs)) {
818                 /* A user program has executed an altivec instruction,
819                    but this kernel doesn't support altivec. */
820                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
821                 return;
822         }
823 #endif
824         /* The kernel has executed an altivec instruction without
825            first enabling altivec.  Whinge but let it do it. */
826         if (++kernel_altivec_count < 10)
827                 printk(KERN_ERR "AltiVec used in kernel (task=%p, pc=%lx)\n",
828                        current, regs->nip);
829         regs->msr |= MSR_VEC;
830 }
831
832 #ifdef CONFIG_ALTIVEC
833 void altivec_assist_exception(struct pt_regs *regs)
834 {
835         int err;
836
837         preempt_disable();
838         if (regs->msr & MSR_VEC)
839                 giveup_altivec(current);
840         preempt_enable();
841         if (!user_mode(regs)) {
842                 printk(KERN_ERR "altivec assist exception in kernel mode"
843                        " at %lx\n", regs->nip);
844                 debugger(regs);
845                 die("altivec assist exception", regs, SIGFPE);
846                 return;
847         }
848
849         err = emulate_altivec(regs);
850         if (err == 0) {
851                 regs->nip += 4;         /* skip emulated instruction */
852                 emulate_single_step(regs);
853                 return;
854         }
855
856         if (err == -EFAULT) {
857                 /* got an error reading the instruction */
858                 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
859         } else {
860                 /* didn't recognize the instruction */
861                 /* XXX quick hack for now: set the non-Java bit in the VSCR */
862                 printk(KERN_ERR "unrecognized altivec instruction "
863                        "in %s at %lx\n", current->comm, regs->nip);
864                 current->thread.vscr.u[3] |= 0x10000;
865         }
866 }
867 #endif /* CONFIG_ALTIVEC */
868
869 #ifdef CONFIG_E500
870 void performance_monitor_exception(struct pt_regs *regs)
871 {
872         perf_irq(regs);
873 }
874 #endif
875
876 #ifdef CONFIG_FSL_BOOKE
877 void CacheLockingException(struct pt_regs *regs, unsigned long address,
878                            unsigned long error_code)
879 {
880         /* We treat cache locking instructions from the user
881          * as priv ops, in the future we could try to do
882          * something smarter
883          */
884         if (error_code & (ESR_DLK|ESR_ILK))
885                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
886         return;
887 }
888 #endif /* CONFIG_FSL_BOOKE */
889
890 #ifdef CONFIG_SPE
891 void SPEFloatingPointException(struct pt_regs *regs)
892 {
893         unsigned long spefscr;
894         int fpexc_mode;
895         int code = 0;
896
897         spefscr = current->thread.spefscr;
898         fpexc_mode = current->thread.fpexc_mode;
899
900         /* Hardware does not necessarily set sticky
901          * underflow/overflow/invalid flags */
902         if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
903                 code = FPE_FLTOVF;
904                 spefscr |= SPEFSCR_FOVFS;
905         }
906         else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
907                 code = FPE_FLTUND;
908                 spefscr |= SPEFSCR_FUNFS;
909         }
910         else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
911                 code = FPE_FLTDIV;
912         else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
913                 code = FPE_FLTINV;
914                 spefscr |= SPEFSCR_FINVS;
915         }
916         else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
917                 code = FPE_FLTRES;
918
919         current->thread.spefscr = spefscr;
920
921         _exception(SIGFPE, regs, code, regs->nip);
922         return;
923 }
924 #endif
925
926 #ifdef CONFIG_BOOKE_WDT
927 /*
928  * Default handler for a Watchdog exception,
929  * spins until a reboot occurs
930  */
931 void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
932 {
933         /* Generic WatchdogHandler, implement your own */
934         mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
935         return;
936 }
937
938 void WatchdogException(struct pt_regs *regs)
939 {
940         printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
941         WatchdogHandler(regs);
942 }
943 #endif
944
945 void __init trap_init(void)
946 {
947 }