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