patch-2_6_7-vs1_9_1_12
[linux-2.6.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
34 #include <asm/pgtable.h>
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
37 #include <asm/io.h>
38 #include <asm/reg.h>
39 #include <asm/xmon.h>
40 #ifdef CONFIG_PMAC_BACKLIGHT
41 #include <asm/backlight.h>
42 #endif
43
44 #ifdef CONFIG_XMON
45 void (*debugger)(struct pt_regs *regs) = xmon;
46 int (*debugger_bpt)(struct pt_regs *regs) = xmon_bpt;
47 int (*debugger_sstep)(struct pt_regs *regs) = xmon_sstep;
48 int (*debugger_iabr_match)(struct pt_regs *regs) = xmon_iabr_match;
49 int (*debugger_dabr_match)(struct pt_regs *regs) = xmon_dabr_match;
50 void (*debugger_fault_handler)(struct pt_regs *regs);
51 #else
52 #ifdef CONFIG_KGDB
53 void (*debugger)(struct pt_regs *regs);
54 int (*debugger_bpt)(struct pt_regs *regs);
55 int (*debugger_sstep)(struct pt_regs *regs);
56 int (*debugger_iabr_match)(struct pt_regs *regs);
57 int (*debugger_dabr_match)(struct pt_regs *regs);
58 void (*debugger_fault_handler)(struct pt_regs *regs);
59 #else
60 #define debugger(regs)                  do { } while (0)
61 #define debugger_bpt(regs)              0
62 #define debugger_sstep(regs)            0
63 #define debugger_iabr_match(regs)       0
64 #define debugger_dabr_match(regs)       0
65 #define debugger_fault_handler          ((void (*)(struct pt_regs *))0)
66 #endif
67 #endif
68
69 /*
70  * Trap & Exception support
71  */
72
73
74 spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
75
76 void die(const char * str, struct pt_regs * fp, long err)
77 {
78         static int die_counter;
79         int nl = 0;
80         console_verbose();
81         spin_lock_irq(&die_lock);
82 #ifdef CONFIG_PMAC_BACKLIGHT
83         set_backlight_enable(1);
84         set_backlight_level(BACKLIGHT_MAX);
85 #endif
86         printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
87 #ifdef CONFIG_PREEMPT
88         printk("PREEMPT ");
89         nl = 1;
90 #endif
91 #ifdef CONFIG_SMP
92         printk("SMP NR_CPUS=%d ", NR_CPUS);
93         nl = 1;
94 #endif
95         if (nl)
96                 printk("\n");
97         show_regs(fp);
98         spin_unlock_irq(&die_lock);
99         /* do_exit() should take care of panic'ing from an interrupt
100          * context so we don't handle it here
101          */
102         do_exit(err);
103 }
104
105 void
106 _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 *) 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 #define REASON_FP               0
175 #define REASON_ILLEGAL          ESR_PIL
176 #define REASON_PRIVILEGED       ESR_PPR
177 #define REASON_TRAP             ESR_PTR
178
179 /* single-step stuff */
180 #define single_stepping(regs)   (current->thread.dbcr0 & DBCR0_IC)
181 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
182
183 #else
184 /* On non-4xx, the reason for the machine check or program
185    exception is in the MSR. */
186 #define get_reason(regs)        ((regs)->msr)
187 #define REASON_FP               0x100000
188 #define REASON_ILLEGAL          0x80000
189 #define REASON_PRIVILEGED       0x40000
190 #define REASON_TRAP             0x20000
191
192 #define single_stepping(regs)   ((regs)->msr & MSR_SE)
193 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
194 #endif
195
196 void
197 MachineCheckException(struct pt_regs *regs)
198 {
199         unsigned long reason = get_reason(regs);
200
201         if (user_mode(regs)) {
202                 regs->msr |= MSR_RI;
203                 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
204                 return;
205         }
206
207 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
208         /* the qspan pci read routines can cause machine checks -- Cort */
209         bad_page_fault(regs, regs->dar, SIGBUS);
210         return;
211 #endif
212
213         if (debugger_fault_handler) {
214                 debugger_fault_handler(regs);
215                 regs->msr |= MSR_RI;
216                 return;
217         }
218
219         if (check_io_access(regs))
220                 return;
221
222 #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
223         if (reason & ESR_IMCP) {
224                 printk("Instruction");
225                 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
226         } else
227                 printk("Data");
228         printk(" machine check in kernel mode.\n");
229 #elif defined(CONFIG_440A)
230         printk("Machine check in kernel mode.\n");
231         if (reason & ESR_IMCP){
232                 printk("Instruction Synchronous Machine Check exception\n");
233                 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
234         }
235         else {
236                 u32 mcsr = mfspr(SPRN_MCSR);
237                 if (mcsr & MCSR_IB)
238                         printk("Instruction Read PLB Error\n");
239                 if (mcsr & MCSR_DRB)
240                         printk("Data Read PLB Error\n");
241                 if (mcsr & MCSR_DWB)
242                         printk("Data Write PLB Error\n");
243                 if (mcsr & MCSR_TLBP)
244                         printk("TLB Parity Error\n");
245                 if (mcsr & MCSR_ICP){
246                         flush_instruction_cache();
247                         printk("I-Cache Parity Error\n");
248                 }
249                 if (mcsr & MCSR_DCSP)
250                         printk("D-Cache Search Parity Error\n");
251                 if (mcsr & MCSR_DCFP)
252                         printk("D-Cache Flush Parity Error\n");
253                 if (mcsr & MCSR_IMPE)
254                         printk("Machine Check exception is imprecise\n");
255
256                 /* Clear MCSR */
257                 mtspr(SPRN_MCSR, mcsr);
258         }
259 #else /* !CONFIG_4xx */
260         printk("Machine check in kernel mode.\n");
261         printk("Caused by (from SRR1=%lx): ", reason);
262         switch (reason & 0x601F0000) {
263         case 0x80000:
264                 printk("Machine check signal\n");
265                 break;
266         case 0:         /* for 601 */
267         case 0x40000:
268         case 0x140000:  /* 7450 MSS error and TEA */
269                 printk("Transfer error ack signal\n");
270                 break;
271         case 0x20000:
272                 printk("Data parity error signal\n");
273                 break;
274         case 0x10000:
275                 printk("Address parity error signal\n");
276                 break;
277         case 0x20000000:
278                 printk("L1 Data Cache error\n");
279                 break;
280         case 0x40000000:
281                 printk("L1 Instruction Cache error\n");
282                 break;
283         case 0x00100000:
284                 printk("L2 data cache parity error\n");
285                 break;
286         default:
287                 printk("Unknown values in msr\n");
288         }
289 #endif /* CONFIG_4xx */
290
291         debugger(regs);
292         die("machine check", regs, SIGBUS);
293 }
294
295 void
296 SMIException(struct pt_regs *regs)
297 {
298         debugger(regs);
299 #if !(defined(CONFIG_XMON) || defined(CONFIG_KGDB))
300         show_regs(regs);
301         panic("System Management Interrupt");
302 #endif
303 }
304
305 void
306 UnknownException(struct pt_regs *regs)
307 {
308         printk("Bad trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
309                regs->nip, regs->msr, regs->trap, print_tainted());
310         _exception(SIGTRAP, regs, 0, 0);
311 }
312
313 void
314 InstructionBreakpoint(struct pt_regs *regs)
315 {
316         if (debugger_iabr_match(regs))
317                 return;
318         _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
319 }
320
321 void
322 RunModeException(struct pt_regs *regs)
323 {
324         _exception(SIGTRAP, regs, 0, 0);
325 }
326
327 /* Illegal instruction emulation support.  Originally written to
328  * provide the PVR to user applications using the mfspr rd, PVR.
329  * Return non-zero if we can't emulate, or EFAULT if the associated
330  * memory access caused an access fault.  Return zero on success.
331  *
332  * There are a couple of ways to do this, either "decode" the instruction
333  * or directly match lots of bits.  In this case, matching lots of
334  * bits is faster and easier.
335  *
336  */
337 #define INST_MFSPR_PVR          0x7c1f42a6
338 #define INST_MFSPR_PVR_MASK     0xfc1fffff
339
340 static int
341 emulate_instruction(struct pt_regs *regs)
342 {
343         u32 instword;
344         u32 rd;
345         int retval;
346
347         retval = -EINVAL;
348
349         if (!user_mode(regs))
350                 return retval;
351         CHECK_FULL_REGS(regs);
352
353         if (get_user(instword, (u32 __user *)(regs->nip)))
354                 return -EFAULT;
355
356         /* Emulate the mfspr rD, PVR.
357          */
358         if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
359                 rd = (instword >> 21) & 0x1f;
360                 regs->gpr[rd] = mfspr(PVR);
361                 retval = 0;
362                 regs->nip += 4;
363         }
364         return retval;
365 }
366
367 /*
368  * After we have successfully emulated an instruction, we have to
369  * check if the instruction was being single-stepped, and if so,
370  * pretend we got a single-step exception.  This was pointed out
371  * by Kumar Gala.  -- paulus
372  */
373 static void emulate_single_step(struct pt_regs *regs)
374 {
375         if (single_stepping(regs)) {
376                 clear_single_step(regs);
377                 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
378         }
379 }
380
381 /*
382  * Look through the list of trap instructions that are used for BUG(),
383  * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know
384  * that the exception was caused by a trap instruction of some kind.
385  * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
386  * otherwise.
387  */
388 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
389
390 #ifndef CONFIG_MODULES
391 #define module_find_bug(x)      NULL
392 #endif
393
394 static struct bug_entry *find_bug(unsigned long bugaddr)
395 {
396         struct bug_entry *bug;
397
398         for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
399                 if (bugaddr == bug->bug_addr)
400                         return bug;
401         return module_find_bug(bugaddr);
402 }
403
404 int
405 check_bug_trap(struct pt_regs *regs)
406 {
407         struct bug_entry *bug;
408         unsigned long addr;
409
410         if (regs->msr & MSR_PR)
411                 return 0;       /* not in kernel */
412         addr = regs->nip;       /* address of trap instruction */
413         if (addr < PAGE_OFFSET)
414                 return 0;
415         bug = find_bug(regs->nip);
416         if (bug == NULL)
417                 return 0;
418         if (bug->line & BUG_WARNING_TRAP) {
419                 /* this is a WARN_ON rather than BUG/BUG_ON */
420 #ifdef CONFIG_XMON
421                 xmon_printf(KERN_ERR "Badness in %s at %s:%d\n",
422                        bug->function, bug->file,
423                        bug->line & ~BUG_WARNING_TRAP);
424 #endif /* CONFIG_XMON */                
425                 printk(KERN_ERR "Badness in %s at %s:%d\n",
426                        bug->function, bug->file,
427                        bug->line & ~BUG_WARNING_TRAP);
428                 dump_stack();
429                 return 1;
430         }
431 #ifdef CONFIG_XMON
432         xmon_printf(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
433                bug->function, bug->file, bug->line);
434         xmon(regs);
435 #endif /* CONFIG_XMON */
436         printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
437                bug->function, bug->file, bug->line);
438
439         return 0;
440 }
441
442 void
443 ProgramCheckException(struct pt_regs *regs)
444 {
445         unsigned int reason = get_reason(regs);
446         extern int do_mathemu(struct pt_regs *regs);
447
448 #ifdef CONFIG_MATH_EMULATION
449         /* (reason & REASON_ILLEGAL) would be the obvious thing here,
450          * but there seems to be a hardware bug on the 405GP (RevD)
451          * that means ESR is sometimes set incorrectly - either to
452          * ESR_DST (!?) or 0.  In the process of chasing this with the
453          * hardware people - not sure if it can happen on any illegal
454          * instruction or only on FP instructions, whether there is a
455          * pattern to occurences etc. -dgibson 31/Mar/2003 */
456         if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
457                 emulate_single_step(regs);
458                 return;
459         }
460 #endif /* CONFIG_MATH_EMULATION */
461
462         if (reason & REASON_FP) {
463                 /* IEEE FP exception */
464                 int code = 0;
465                 u32 fpscr;
466
467                 /* We must make sure the FP state is consistent with
468                  * our MSR_FP in regs
469                  */
470                 preempt_disable();
471                 if (regs->msr & MSR_FP)
472                         giveup_fpu(current);
473                 preempt_enable();
474
475                 fpscr = current->thread.fpscr;
476                 fpscr &= fpscr << 22;   /* mask summary bits with enables */
477                 if (fpscr & FPSCR_VX)
478                         code = FPE_FLTINV;
479                 else if (fpscr & FPSCR_OX)
480                         code = FPE_FLTOVF;
481                 else if (fpscr & FPSCR_UX)
482                         code = FPE_FLTUND;
483                 else if (fpscr & FPSCR_ZX)
484                         code = FPE_FLTDIV;
485                 else if (fpscr & FPSCR_XX)
486                         code = FPE_FLTRES;
487                 _exception(SIGFPE, regs, code, regs->nip);
488                 return;
489         }
490
491         if (reason & REASON_TRAP) {
492                 /* trap exception */
493                 if (debugger_bpt(regs))
494                         return;
495                 if (check_bug_trap(regs)) {
496                         regs->nip += 4;
497                         return;
498                 }
499                 _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
500                 return;
501         }
502
503         if (reason & REASON_PRIVILEGED) {
504                 /* Try to emulate it if we should. */
505                 if (emulate_instruction(regs) == 0) {
506                         emulate_single_step(regs);
507                         return;
508                 }
509                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
510                 return;
511         }
512
513         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
514 }
515
516 void
517 SingleStepException(struct pt_regs *regs)
518 {
519         regs->msr &= ~MSR_SE;  /* Turn off 'trace' bit */
520         if (debugger_sstep(regs))
521                 return;
522         _exception(SIGTRAP, regs, TRAP_TRACE, 0);
523 }
524
525 void
526 AlignmentException(struct pt_regs *regs)
527 {
528         int fixed;
529
530         fixed = fix_alignment(regs);
531         if (fixed == 1) {
532                 regs->nip += 4; /* skip over emulated instruction */
533                 return;
534         }
535         if (fixed == -EFAULT) {
536                 /* fixed == -EFAULT means the operand address was bad */
537                 if (user_mode(regs))
538                         _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
539                 else
540                         bad_page_fault(regs, regs->dar, SIGSEGV);
541                 return;
542         }
543         _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
544 }
545
546 void
547 StackOverflow(struct pt_regs *regs)
548 {
549         printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
550                current, regs->gpr[1]);
551         debugger(regs);
552         show_regs(regs);
553         panic("kernel stack overflow");
554 }
555
556 void nonrecoverable_exception(struct pt_regs *regs)
557 {
558         printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
559                regs->nip, regs->msr);
560         debugger(regs);
561         die("nonrecoverable exception", regs, SIGKILL);
562 }
563
564 void
565 trace_syscall(struct pt_regs *regs)
566 {
567         printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
568                current, current->pid, regs->nip, regs->link, regs->gpr[0],
569                regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
570 }
571
572 #ifdef CONFIG_8xx
573 void
574 SoftwareEmulation(struct pt_regs *regs)
575 {
576         extern int do_mathemu(struct pt_regs *);
577         extern int Soft_emulate_8xx(struct pt_regs *);
578         int errcode;
579
580         CHECK_FULL_REGS(regs);
581
582         if (!user_mode(regs)) {
583                 debugger(regs);
584                 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
585         }
586
587 #ifdef CONFIG_MATH_EMULATION
588         errcode = do_mathemu(regs);
589 #else
590         errcode = Soft_emulate_8xx(regs);
591 #endif
592         if (errcode) {
593                 if (errcode > 0)
594                         _exception(SIGFPE, regs, 0, 0);
595                 else if (errcode == -EFAULT)
596                         _exception(SIGSEGV, regs, 0, 0);
597                 else
598                         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
599         } else
600                 emulate_single_step(regs);
601 }
602 #endif /* CONFIG_8xx */
603
604 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
605
606 void DebugException(struct pt_regs *regs, unsigned long debug_status)
607 {
608 #if 0
609         if (debug_status & DBSR_TIE) {          /* trap instruction*/
610                 if (!user_mode(regs) && debugger_bpt(regs))
611                         return;
612                 _exception(SIGTRAP, regs, 0, 0);
613
614         }
615 #endif
616         if (debug_status & DBSR_IC) {   /* instruction completion */
617                 if (!user_mode(regs) && debugger_sstep(regs))
618                         return;
619                 current->thread.dbcr0 &= ~DBCR0_IC;
620                 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
621         }
622 }
623 #endif /* CONFIG_4xx || CONFIG_BOOKE */
624
625 #if !defined(CONFIG_TAU_INT)
626 void
627 TAUException(struct pt_regs *regs)
628 {
629         printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
630                regs->nip, regs->msr, regs->trap, print_tainted());
631 }
632 #endif /* CONFIG_INT_TAU */
633
634 void AltivecUnavailException(struct pt_regs *regs)
635 {
636         static int kernel_altivec_count;
637
638 #ifndef CONFIG_ALTIVEC
639         if (user_mode(regs)) {
640                 /* A user program has executed an altivec instruction,
641                    but this kernel doesn't support altivec. */
642                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
643                 return;
644         }
645 #endif
646         /* The kernel has executed an altivec instruction without
647            first enabling altivec.  Whinge but let it do it. */
648         if (++kernel_altivec_count < 10)
649                 printk(KERN_ERR "AltiVec used in kernel (task=%p, pc=%x)\n",
650                        current, regs->nip);
651         regs->msr |= MSR_VEC;
652 }
653
654 #ifdef CONFIG_ALTIVEC
655 void
656 AltivecAssistException(struct pt_regs *regs)
657 {
658         int err;
659
660         preempt_disable();
661         if (regs->msr & MSR_VEC)
662                 giveup_altivec(current);
663         preempt_enable();
664
665         err = emulate_altivec(regs);
666         if (err == 0) {
667                 regs->nip += 4;         /* skip emulated instruction */
668                 emulate_single_step(regs);
669                 return;
670         }
671
672         if (err == -EFAULT) {
673                 /* got an error reading the instruction */
674                 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
675         } else {
676                 /* didn't recognize the instruction */
677                 /* XXX quick hack for now: set the non-Java bit in the VSCR */
678                 printk(KERN_ERR "unrecognized altivec instruction "
679                        "in %s at %lx\n", current->comm, regs->nip);
680                 current->thread.vscr.u[3] |= 0x10000;
681         }
682 }
683 #endif /* CONFIG_ALTIVEC */
684
685
686 void __init trap_init(void)
687 {
688 }