ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 extern int fix_alignment(struct pt_regs *);
45 extern void bad_page_fault(struct pt_regs *, unsigned long, int sig);
46
47 #ifdef CONFIG_XMON
48 void (*debugger)(struct pt_regs *regs) = xmon;
49 int (*debugger_bpt)(struct pt_regs *regs) = xmon_bpt;
50 int (*debugger_sstep)(struct pt_regs *regs) = xmon_sstep;
51 int (*debugger_iabr_match)(struct pt_regs *regs) = xmon_iabr_match;
52 int (*debugger_dabr_match)(struct pt_regs *regs) = xmon_dabr_match;
53 void (*debugger_fault_handler)(struct pt_regs *regs);
54 #else
55 #ifdef CONFIG_KGDB
56 void (*debugger)(struct pt_regs *regs);
57 int (*debugger_bpt)(struct pt_regs *regs);
58 int (*debugger_sstep)(struct pt_regs *regs);
59 int (*debugger_iabr_match)(struct pt_regs *regs);
60 int (*debugger_dabr_match)(struct pt_regs *regs);
61 void (*debugger_fault_handler)(struct pt_regs *regs);
62 #else
63 #define debugger(regs)                  do { } while (0)
64 #define debugger_bpt(regs)              0
65 #define debugger_sstep(regs)            0
66 #define debugger_iabr_match(regs)       0
67 #define debugger_dabr_match(regs)       0
68 #define debugger_fault_handler          ((void (*)(struct pt_regs *))0)
69 #endif
70 #endif
71
72 /*
73  * Trap & Exception support
74  */
75
76
77 spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
78
79 void die(const char * str, struct pt_regs * fp, long err)
80 {
81         static int die_counter;
82         int nl = 0;
83         console_verbose();
84         spin_lock_irq(&die_lock);
85 #ifdef CONFIG_PMAC_BACKLIGHT
86         set_backlight_enable(1);
87         set_backlight_level(BACKLIGHT_MAX);
88 #endif
89         printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
90 #ifdef CONFIG_PREEMPT
91         printk("PREEMPT ");
92         nl = 1;
93 #endif
94 #ifdef CONFIG_SMP
95         printk("SMP NR_CPUS=%d ", NR_CPUS);
96         nl = 1;
97 #endif
98         if (nl)
99                 printk("\n");
100         show_regs(fp);
101         spin_unlock_irq(&die_lock);
102         /* do_exit() should take care of panic'ing from an interrupt
103          * context so we don't handle it here
104          */
105         do_exit(err);
106 }
107
108 void
109 _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
110 {
111         siginfo_t info;
112
113         if (!user_mode(regs)) {
114                 debugger(regs);
115                 die("Exception in kernel mode", regs, signr);
116         }
117         info.si_signo = signr;
118         info.si_errno = 0;
119         info.si_code = code;
120         info.si_addr = (void *) addr;
121         force_sig_info(signr, &info, current);
122 }
123
124 /*
125  * I/O accesses can cause machine checks on powermacs.
126  * Check if the NIP corresponds to the address of a sync
127  * instruction for which there is an entry in the exception
128  * table.
129  * Note that the 601 only takes a machine check on TEA
130  * (transfer error ack) signal assertion, and does not
131  * set any of the top 16 bits of SRR1.
132  *  -- paulus.
133  */
134 static inline int check_io_access(struct pt_regs *regs)
135 {
136 #ifdef CONFIG_PPC_PMAC
137         unsigned long msr = regs->msr;
138         const struct exception_table_entry *entry;
139         unsigned int *nip = (unsigned int *)regs->nip;
140
141         if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
142             && (entry = search_exception_tables(regs->nip)) != NULL) {
143                 /*
144                  * Check that it's a sync instruction, or somewhere
145                  * in the twi; isync; nop sequence that inb/inw/inl uses.
146                  * As the address is in the exception table
147                  * we should be able to read the instr there.
148                  * For the debug message, we look at the preceding
149                  * load or store.
150                  */
151                 if (*nip == 0x60000000)         /* nop */
152                         nip -= 2;
153                 else if (*nip == 0x4c00012c)    /* isync */
154                         --nip;
155                 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
156                         /* sync or twi */
157                         unsigned int rb;
158
159                         --nip;
160                         rb = (*nip >> 11) & 0x1f;
161                         printk(KERN_DEBUG "%s bad port %lx at %p\n",
162                                (*nip & 0x100)? "OUT to": "IN from",
163                                regs->gpr[rb] - _IO_BASE, nip);
164                         regs->msr |= MSR_RI;
165                         regs->nip = entry->fixup;
166                         return 1;
167                 }
168         }
169 #endif /* CONFIG_PPC_PMAC */
170         return 0;
171 }
172
173 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
174 /* On 4xx, the reason for the machine check or program exception
175    is in the ESR. */
176 #define get_reason(regs)        ((regs)->dsisr)
177 #define REASON_FP               0
178 #define REASON_ILLEGAL          ESR_PIL
179 #define REASON_PRIVILEGED       ESR_PPR
180 #define REASON_TRAP             ESR_PTR
181
182 /* single-step stuff */
183 #define single_stepping(regs)   (current->thread.dbcr0 & DBCR0_IC)
184 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
185
186 #else
187 /* On non-4xx, the reason for the machine check or program
188    exception is in the MSR. */
189 #define get_reason(regs)        ((regs)->msr)
190 #define REASON_FP               0x100000
191 #define REASON_ILLEGAL          0x80000
192 #define REASON_PRIVILEGED       0x40000
193 #define REASON_TRAP             0x20000
194
195 #define single_stepping(regs)   ((regs)->msr & MSR_SE)
196 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
197 #endif
198
199 void
200 MachineCheckException(struct pt_regs *regs)
201 {
202         unsigned long reason = get_reason(regs);
203
204         if (user_mode(regs)) {
205                 regs->msr |= MSR_RI;
206                 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
207                 return;
208         }
209
210 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
211         /* the qspan pci read routines can cause machine checks -- Cort */
212         bad_page_fault(regs, regs->dar, SIGBUS);
213         return;
214 #endif
215
216         if (debugger_fault_handler) {
217                 debugger_fault_handler(regs);
218                 regs->msr |= MSR_RI;
219                 return;
220         }
221
222         if (check_io_access(regs))
223                 return;
224
225 #ifdef CONFIG_4xx
226         if (reason & ESR_IMCP) {
227                 printk("Instruction");
228                 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
229         } else
230                 printk("Data");
231         printk(" machine check in kernel mode.\n");
232
233 #else /* !CONFIG_4xx */
234         printk("Machine check in kernel mode.\n");
235         printk("Caused by (from SRR1=%lx): ", reason);
236         switch (reason & 0x601F0000) {
237         case 0x80000:
238                 printk("Machine check signal\n");
239                 break;
240         case 0:         /* for 601 */
241         case 0x40000:
242         case 0x140000:  /* 7450 MSS error and TEA */
243                 printk("Transfer error ack signal\n");
244                 break;
245         case 0x20000:
246                 printk("Data parity error signal\n");
247                 break;
248         case 0x10000:
249                 printk("Address parity error signal\n");
250                 break;
251         case 0x20000000:
252                 printk("L1 Data Cache error\n");
253                 break;
254         case 0x40000000:
255                 printk("L1 Instruction Cache error\n");
256                 break;
257         case 0x00100000:
258                 printk("L2 data cache parity error\n");
259                 break;
260         default:
261                 printk("Unknown values in msr\n");
262         }
263 #endif /* CONFIG_4xx */
264
265         debugger(regs);
266         die("machine check", regs, SIGBUS);
267 }
268
269 void
270 SMIException(struct pt_regs *regs)
271 {
272         debugger(regs);
273 #if !(defined(CONFIG_XMON) || defined(CONFIG_KGDB))
274         show_regs(regs);
275         panic("System Management Interrupt");
276 #endif
277 }
278
279 void
280 UnknownException(struct pt_regs *regs)
281 {
282         printk("Bad trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
283                regs->nip, regs->msr, regs->trap, print_tainted());
284         _exception(SIGTRAP, regs, 0, 0);
285 }
286
287 void
288 InstructionBreakpoint(struct pt_regs *regs)
289 {
290         if (debugger_iabr_match(regs))
291                 return;
292         _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
293 }
294
295 void
296 RunModeException(struct pt_regs *regs)
297 {
298         _exception(SIGTRAP, regs, 0, 0);
299 }
300
301 /* Illegal instruction emulation support.  Originally written to
302  * provide the PVR to user applications using the mfspr rd, PVR.
303  * Return non-zero if we can't emulate, or EFAULT if the associated
304  * memory access caused an access fault.  Return zero on success.
305  *
306  * There are a couple of ways to do this, either "decode" the instruction
307  * or directly match lots of bits.  In this case, matching lots of
308  * bits is faster and easier.
309  *
310  */
311 #define INST_MFSPR_PVR          0x7c1f42a6
312 #define INST_MFSPR_PVR_MASK     0xfc1fffff
313
314 static int
315 emulate_instruction(struct pt_regs *regs)
316 {
317         u32 instword;
318         u32 rd;
319         int retval;
320
321         retval = -EINVAL;
322
323         if (!user_mode(regs))
324                 return retval;
325         CHECK_FULL_REGS(regs);
326
327         if (get_user(instword, (u32 __user *)(regs->nip)))
328                 return -EFAULT;
329
330         /* Emulate the mfspr rD, PVR.
331          */
332         if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
333                 rd = (instword >> 21) & 0x1f;
334                 regs->gpr[rd] = mfspr(PVR);
335                 retval = 0;
336                 regs->nip += 4;
337         }
338         return retval;
339 }
340
341 /*
342  * After we have successfully emulated an instruction, we have to
343  * check if the instruction was being single-stepped, and if so,
344  * pretend we got a single-step exception.  This was pointed out
345  * by Kumar Gala.  -- paulus
346  */
347 static void emulate_single_step(struct pt_regs *regs)
348 {
349         if (single_stepping(regs)) {
350                 clear_single_step(regs);
351                 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
352         }
353 }
354
355 /*
356  * Look through the list of trap instructions that are used for BUG(),
357  * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know
358  * that the exception was caused by a trap instruction of some kind.
359  * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
360  * otherwise.
361  */
362 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
363
364 #ifndef CONFIG_MODULES
365 #define module_find_bug(x)      NULL
366 #endif
367
368 static struct bug_entry *find_bug(unsigned long bugaddr)
369 {
370         struct bug_entry *bug;
371
372         for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
373                 if (bugaddr == bug->bug_addr)
374                         return bug;
375         return module_find_bug(bugaddr);
376 }
377
378 int
379 check_bug_trap(struct pt_regs *regs)
380 {
381         struct bug_entry *bug;
382         unsigned long addr;
383
384         if (regs->msr & MSR_PR)
385                 return 0;       /* not in kernel */
386         addr = regs->nip;       /* address of trap instruction */
387         if (addr < PAGE_OFFSET)
388                 return 0;
389         bug = find_bug(regs->nip);
390         if (bug == NULL)
391                 return 0;
392         if (bug->line & BUG_WARNING_TRAP) {
393                 /* this is a WARN_ON rather than BUG/BUG_ON */
394 #ifdef CONFIG_XMON
395                 xmon_printf(KERN_ERR "Badness in %s at %s:%d\n",
396                        bug->function, bug->file,
397                        bug->line & ~BUG_WARNING_TRAP);
398 #endif /* CONFIG_XMON */                
399                 printk(KERN_ERR "Badness in %s at %s:%d\n",
400                        bug->function, bug->file,
401                        bug->line & ~BUG_WARNING_TRAP);
402                 dump_stack();
403                 return 1;
404         }
405 #ifdef CONFIG_XMON
406         xmon_printf(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
407                bug->function, bug->file, bug->line);
408         xmon(regs);
409 #endif /* CONFIG_XMON */
410         printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
411                bug->function, bug->file, bug->line);
412
413         return 0;
414 }
415
416 void
417 ProgramCheckException(struct pt_regs *regs)
418 {
419         unsigned int reason = get_reason(regs);
420         extern int do_mathemu(struct pt_regs *regs);
421
422 #ifdef CONFIG_MATH_EMULATION
423         /* (reason & REASON_ILLEGAL) would be the obvious thing here,
424          * but there seems to be a hardware bug on the 405GP (RevD)
425          * that means ESR is sometimes set incorrectly - either to
426          * ESR_DST (!?) or 0.  In the process of chasing this with the
427          * hardware people - not sure if it can happen on any illegal
428          * instruction or only on FP instructions, whether there is a
429          * pattern to occurences etc. -dgibson 31/Mar/2003 */
430         if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
431                 emulate_single_step(regs);
432                 return;
433         }
434 #endif /* CONFIG_MATH_EMULATION */
435
436         if (reason & REASON_FP) {
437                 /* IEEE FP exception */
438                 int code = 0;
439                 u32 fpscr;
440
441                 /* We must make sure the FP state is consistent with
442                  * our MSR_FP in regs
443                  */
444                 preempt_disable();
445                 if (regs->msr & MSR_FP)
446                         giveup_fpu(current);
447                 preempt_enable();
448
449                 fpscr = current->thread.fpscr;
450                 fpscr &= fpscr << 22;   /* mask summary bits with enables */
451                 if (fpscr & FPSCR_VX)
452                         code = FPE_FLTINV;
453                 else if (fpscr & FPSCR_OX)
454                         code = FPE_FLTOVF;
455                 else if (fpscr & FPSCR_UX)
456                         code = FPE_FLTUND;
457                 else if (fpscr & FPSCR_ZX)
458                         code = FPE_FLTDIV;
459                 else if (fpscr & FPSCR_XX)
460                         code = FPE_FLTRES;
461                 _exception(SIGFPE, regs, code, regs->nip);
462                 return;
463         }
464
465         if (reason & REASON_TRAP) {
466                 /* trap exception */
467                 if (debugger_bpt(regs))
468                         return;
469                 if (check_bug_trap(regs)) {
470                         regs->nip += 4;
471                         return;
472                 }
473                 _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
474                 return;
475         }
476
477         if (reason & REASON_PRIVILEGED) {
478                 /* Try to emulate it if we should. */
479                 if (emulate_instruction(regs) == 0) {
480                         emulate_single_step(regs);
481                         return;
482                 }
483                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
484                 return;
485         }
486
487         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
488 }
489
490 void
491 SingleStepException(struct pt_regs *regs)
492 {
493         regs->msr &= ~MSR_SE;  /* Turn off 'trace' bit */
494         if (debugger_sstep(regs))
495                 return;
496         _exception(SIGTRAP, regs, TRAP_TRACE, 0);
497 }
498
499 void
500 AlignmentException(struct pt_regs *regs)
501 {
502         int fixed;
503
504         fixed = fix_alignment(regs);
505         if (fixed == 1) {
506                 regs->nip += 4; /* skip over emulated instruction */
507                 return;
508         }
509         if (fixed == -EFAULT) {
510                 /* fixed == -EFAULT means the operand address was bad */
511                 if (user_mode(regs))
512                         _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
513                 else
514                         bad_page_fault(regs, regs->dar, SIGSEGV);
515                 return;
516         }
517         _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
518 }
519
520 void
521 StackOverflow(struct pt_regs *regs)
522 {
523         printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
524                current, regs->gpr[1]);
525         debugger(regs);
526         show_regs(regs);
527         panic("kernel stack overflow");
528 }
529
530 void nonrecoverable_exception(struct pt_regs *regs)
531 {
532         printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
533                regs->nip, regs->msr);
534         debugger(regs);
535         die("nonrecoverable exception", regs, SIGKILL);
536 }
537
538 void
539 trace_syscall(struct pt_regs *regs)
540 {
541         printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
542                current, current->pid, regs->nip, regs->link, regs->gpr[0],
543                regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
544 }
545
546 #ifdef CONFIG_8xx
547 void
548 SoftwareEmulation(struct pt_regs *regs)
549 {
550         extern int do_mathemu(struct pt_regs *);
551         extern int Soft_emulate_8xx(struct pt_regs *);
552         int errcode;
553
554         CHECK_FULL_REGS(regs);
555
556         if (!user_mode(regs)) {
557                 debugger(regs);
558                 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
559         }
560
561 #ifdef CONFIG_MATH_EMULATION
562         errcode = do_mathemu(regs);
563 #else
564         errcode = Soft_emulate_8xx(regs);
565 #endif
566         if (errcode) {
567                 if (errcode > 0)
568                         _exception(SIGFPE, regs, 0, 0);
569                 else if (errcode == -EFAULT)
570                         _exception(SIGSEGV, regs, 0, 0);
571                 else
572                         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
573         } else
574                 emulate_single_step(regs);
575 }
576 #endif /* CONFIG_8xx */
577
578 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
579
580 void DebugException(struct pt_regs *regs, unsigned long debug_status)
581 {
582 #if 0
583         if (debug_status & DBSR_TIE) {          /* trap instruction*/
584                 if (!user_mode(regs) && debugger_bpt(regs))
585                         return;
586                 _exception(SIGTRAP, regs, 0, 0);
587
588         }
589 #endif
590         if (debug_status & DBSR_IC) {   /* instruction completion */
591                 if (!user_mode(regs) && debugger_sstep(regs))
592                         return;
593                 current->thread.dbcr0 &= ~DBCR0_IC;
594                 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
595         }
596 }
597 #endif /* CONFIG_4xx || CONFIG_BOOKE */
598
599 #if !defined(CONFIG_TAU_INT)
600 void
601 TAUException(struct pt_regs *regs)
602 {
603         printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
604                regs->nip, regs->msr, regs->trap, print_tainted());
605 }
606 #endif /* CONFIG_INT_TAU */
607
608 #ifdef CONFIG_ALTIVEC
609 void
610 AltivecAssistException(struct pt_regs *regs)
611 {
612         preempt_disable();
613         if (regs->msr & MSR_VEC)
614                 giveup_altivec(current);
615         preempt_enable();
616
617         /* XXX quick hack for now: set the non-Java bit in the VSCR */
618         current->thread.vscr.u[3] |= 0x10000;
619 }
620 #endif /* CONFIG_ALTIVEC */
621
622
623 void __init trap_init(void)
624 {
625 }