2 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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.
9 * Modified by Cort Dougan (cort@cs.nmt.edu)
10 * and Paul Mackerras (paulus@cs.anu.edu.au)
14 * This file handles the architecture-dependent parts of hardware exceptions
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.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>
33 #include <asm/pgtable.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
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
);
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
);
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
);
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)
72 * Trap & Exception support
75 DEFINE_SPINLOCK(die_lock
);
77 int die(const char * str
, struct pt_regs
* fp
, long err
)
79 static int die_counter
;
82 spin_lock_irq(&die_lock
);
83 printk("Oops: %s, sig: %ld [#%d]\n", str
, err
, ++die_counter
);
89 printk("SMP NR_CPUS=%d ", NR_CPUS
);
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
103 void _exception(int signr
, struct pt_regs
*regs
, int code
, unsigned long addr
)
107 if (!user_mode(regs
)) {
109 die("Exception in kernel mode", regs
, signr
);
111 info
.si_signo
= signr
;
114 info
.si_addr
= (void __user
*) addr
;
115 force_sig_info(signr
, &info
, current
);
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.
124 if (is_global_init(current
)) {
125 __sighandler_t handler
;
127 spin_lock_irq(¤t
->sighand
->siglock
);
128 handler
= current
->sighand
->action
[signr
-1].sa
.sa_handler
;
129 spin_unlock_irq(¤t
->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
);
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
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.
150 static inline int check_io_access(struct pt_regs
*regs
)
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
;
157 if (((msr
& 0xffff0000) == 0 || (msr
& (0x80000 | 0x40000)))
158 && (entry
= search_exception_tables(regs
->nip
)) != NULL
) {
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
167 if (*nip
== 0x60000000) /* nop */
169 else if (*nip
== 0x4c00012c) /* isync */
171 /* eieio from I/O string functions */
172 else if ((*nip
) == 0x7c0006ac || *(nip
+1) == 0x7c0006ac)
174 if (*nip
== 0x7c0004ac || (*nip
>> 26) == 3 ||
175 (*(nip
+1) >> 26) == 3) {
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
);
185 regs
->nip
= entry
->fixup
;
189 #endif /* CONFIG_8xx */
193 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
194 /* On 4xx, the reason for the machine check or program exception
196 #define get_reason(regs) ((regs)->dsisr)
197 #define get_mc_reason(regs) ((regs)->dsisr)
198 #define REASON_FP ESR_FP
199 #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
200 #define REASON_PRIVILEGED ESR_PPR
201 #define REASON_TRAP ESR_PTR
203 /* single-step stuff */
204 #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
205 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
208 /* On non-4xx, the reason for the machine check or program
209 exception is in the MSR. */
210 #define get_reason(regs) ((regs)->msr)
211 #define get_mc_reason(regs) ((regs)->msr)
212 #define REASON_FP 0x100000
213 #define REASON_ILLEGAL 0x80000
214 #define REASON_PRIVILEGED 0x40000
215 #define REASON_TRAP 0x20000
217 #define single_stepping(regs) ((regs)->msr & MSR_SE)
218 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
222 * This is "fall-back" implementation for configurations
223 * which don't provide platform-specific machine check info
225 void __attribute__ ((weak
))
226 platform_machine_check(struct pt_regs
*regs
)
230 #if defined(CONFIG_4xx)
231 int machine_check_4xx(struct pt_regs
*regs
)
233 unsigned long reason
= get_mc_reason(regs
);
235 if (reason
& ESR_IMCP
) {
236 printk("Instruction");
237 mtspr(SPRN_ESR
, reason
& ~ESR_IMCP
);
240 printk(" machine check in kernel mode.\n");
245 int machine_check_440A(struct pt_regs
*regs
)
247 unsigned long reason
= get_mc_reason(regs
);
249 printk("Machine check in kernel mode.\n");
250 if (reason
& ESR_IMCP
){
251 printk("Instruction Synchronous Machine Check exception\n");
252 mtspr(SPRN_ESR
, reason
& ~ESR_IMCP
);
255 u32 mcsr
= mfspr(SPRN_MCSR
);
257 printk("Instruction Read PLB Error\n");
259 printk("Data Read PLB Error\n");
261 printk("Data Write PLB Error\n");
262 if (mcsr
& MCSR_TLBP
)
263 printk("TLB Parity Error\n");
264 if (mcsr
& MCSR_ICP
){
265 flush_instruction_cache();
266 printk("I-Cache Parity Error\n");
268 if (mcsr
& MCSR_DCSP
)
269 printk("D-Cache Search Parity Error\n");
270 if (mcsr
& MCSR_DCFP
)
271 printk("D-Cache Flush Parity Error\n");
272 if (mcsr
& MCSR_IMPE
)
273 printk("Machine Check exception is imprecise\n");
276 mtspr(SPRN_MCSR
, mcsr
);
281 int machine_check_generic(struct pt_regs
*regs
)
283 unsigned long reason
= get_mc_reason(regs
);
285 printk("Machine check in kernel mode.\n");
286 printk("Caused by (from SRR1=%lx): ", reason
);
287 switch (reason
& 0x601F0000) {
289 printk("Machine check signal\n");
291 case 0: /* for 601 */
293 case 0x140000: /* 7450 MSS error and TEA */
294 printk("Transfer error ack signal\n");
297 printk("Data parity error signal\n");
300 printk("Address parity error signal\n");
303 printk("L1 Data Cache error\n");
306 printk("L1 Instruction Cache error\n");
309 printk("L2 data cache parity error\n");
312 printk("Unknown values in msr\n");
316 #endif /* everything else */
318 void machine_check_exception(struct pt_regs
*regs
)
322 if (cur_cpu_spec
->machine_check
)
323 recover
= cur_cpu_spec
->machine_check(regs
);
327 if (user_mode(regs
)) {
329 _exception(SIGBUS
, regs
, BUS_ADRERR
, regs
->nip
);
333 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
334 /* the qspan pci read routines can cause machine checks -- Cort */
335 bad_page_fault(regs
, regs
->dar
, SIGBUS
);
339 if (debugger_fault_handler
) {
340 debugger_fault_handler(regs
);
345 if (check_io_access(regs
))
349 * Optional platform-provided routine to print out
350 * additional info, e.g. bus error registers.
352 platform_machine_check(regs
);
355 die("machine check", regs
, SIGBUS
);
358 void SMIException(struct pt_regs
*regs
)
361 #if !(defined(CONFIG_XMON) || defined(CONFIG_KGDB))
363 panic("System Management Interrupt");
367 void unknown_exception(struct pt_regs
*regs
)
369 printk("Bad trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
370 regs
->nip
, regs
->msr
, regs
->trap
, print_tainted());
371 _exception(SIGTRAP
, regs
, 0, 0);
374 void instruction_breakpoint_exception(struct pt_regs
*regs
)
376 if (debugger_iabr_match(regs
))
378 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, 0);
381 void RunModeException(struct pt_regs
*regs
)
383 _exception(SIGTRAP
, regs
, 0, 0);
386 /* Illegal instruction emulation support. Originally written to
387 * provide the PVR to user applications using the mfspr rd, PVR.
388 * Return non-zero if we can't emulate, or -EFAULT if the associated
389 * memory access caused an access fault. Return zero on success.
391 * There are a couple of ways to do this, either "decode" the instruction
392 * or directly match lots of bits. In this case, matching lots of
393 * bits is faster and easier.
396 #define INST_MFSPR_PVR 0x7c1f42a6
397 #define INST_MFSPR_PVR_MASK 0xfc1fffff
399 #define INST_DCBA 0x7c0005ec
400 #define INST_DCBA_MASK 0x7c0007fe
402 #define INST_MCRXR 0x7c000400
403 #define INST_MCRXR_MASK 0x7c0007fe
405 #define INST_STRING 0x7c00042a
406 #define INST_STRING_MASK 0x7c0007fe
407 #define INST_STRING_GEN_MASK 0x7c00067e
408 #define INST_LSWI 0x7c0004aa
409 #define INST_LSWX 0x7c00042a
410 #define INST_STSWI 0x7c0005aa
411 #define INST_STSWX 0x7c00052a
413 static int emulate_string_inst(struct pt_regs
*regs
, u32 instword
)
415 u8 rT
= (instword
>> 21) & 0x1f;
416 u8 rA
= (instword
>> 16) & 0x1f;
417 u8 NB_RB
= (instword
>> 11) & 0x1f;
422 /* Early out if we are an invalid form of lswx */
423 if ((instword
& INST_STRING_MASK
) == INST_LSWX
)
424 if ((rT
== rA
) || (rT
== NB_RB
))
427 EA
= (rA
== 0) ? 0 : regs
->gpr
[rA
];
429 switch (instword
& INST_STRING_MASK
) {
433 num_bytes
= regs
->xer
& 0x7f;
437 num_bytes
= (NB_RB
== 0) ? 32 : NB_RB
;
443 while (num_bytes
!= 0)
446 u32 shift
= 8 * (3 - (pos
& 0x3));
448 switch ((instword
& INST_STRING_MASK
)) {
451 if (get_user(val
, (u8 __user
*)EA
))
453 /* first time updating this reg,
457 regs
->gpr
[rT
] |= val
<< shift
;
461 val
= regs
->gpr
[rT
] >> shift
;
462 if (put_user(val
, (u8 __user
*)EA
))
466 /* move EA to next address */
470 /* manage our position within the register */
481 static int emulate_instruction(struct pt_regs
*regs
)
486 if (!user_mode(regs
))
488 CHECK_FULL_REGS(regs
);
490 if (get_user(instword
, (u32 __user
*)(regs
->nip
)))
493 /* Emulate the mfspr rD, PVR.
495 if ((instword
& INST_MFSPR_PVR_MASK
) == INST_MFSPR_PVR
) {
496 rd
= (instword
>> 21) & 0x1f;
497 regs
->gpr
[rd
] = mfspr(SPRN_PVR
);
501 /* Emulating the dcba insn is just a no-op. */
502 if ((instword
& INST_DCBA_MASK
) == INST_DCBA
)
505 /* Emulate the mcrxr insn. */
506 if ((instword
& INST_MCRXR_MASK
) == INST_MCRXR
) {
507 int shift
= (instword
>> 21) & 0x1c;
508 unsigned long msk
= 0xf0000000UL
>> shift
;
510 regs
->ccr
= (regs
->ccr
& ~msk
) | ((regs
->xer
>> shift
) & msk
);
511 regs
->xer
&= ~0xf0000000UL
;
515 /* Emulate load/store string insn. */
516 if ((instword
& INST_STRING_GEN_MASK
) == INST_STRING
)
517 return emulate_string_inst(regs
, instword
);
523 * After we have successfully emulated an instruction, we have to
524 * check if the instruction was being single-stepped, and if so,
525 * pretend we got a single-step exception. This was pointed out
526 * by Kumar Gala. -- paulus
528 static void emulate_single_step(struct pt_regs
*regs
)
530 if (single_stepping(regs
)) {
531 clear_single_step(regs
);
532 _exception(SIGTRAP
, regs
, TRAP_TRACE
, 0);
536 int is_valid_bugaddr(unsigned long addr
)
538 return addr
>= PAGE_OFFSET
;
541 void program_check_exception(struct pt_regs
*regs
)
543 unsigned int reason
= get_reason(regs
);
544 extern int do_mathemu(struct pt_regs
*regs
);
546 #ifdef CONFIG_MATH_EMULATION
547 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
548 * but there seems to be a hardware bug on the 405GP (RevD)
549 * that means ESR is sometimes set incorrectly - either to
550 * ESR_DST (!?) or 0. In the process of chasing this with the
551 * hardware people - not sure if it can happen on any illegal
552 * instruction or only on FP instructions, whether there is a
553 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
554 if (!(reason
& REASON_TRAP
) && do_mathemu(regs
) == 0) {
555 emulate_single_step(regs
);
558 #endif /* CONFIG_MATH_EMULATION */
560 if (reason
& REASON_FP
) {
561 /* IEEE FP exception */
565 /* We must make sure the FP state is consistent with
569 if (regs
->msr
& MSR_FP
)
573 fpscr
= current
->thread
.fpscr
.val
;
574 fpscr
&= fpscr
<< 22; /* mask summary bits with enables */
575 if (fpscr
& FPSCR_VX
)
577 else if (fpscr
& FPSCR_OX
)
579 else if (fpscr
& FPSCR_UX
)
581 else if (fpscr
& FPSCR_ZX
)
583 else if (fpscr
& FPSCR_XX
)
585 _exception(SIGFPE
, regs
, code
, regs
->nip
);
589 if (reason
& REASON_TRAP
) {
591 if (debugger_bpt(regs
))
594 if (!(regs
->msr
& MSR_PR
) && /* not user-mode */
595 report_bug(regs
->nip
, regs
) == BUG_TRAP_TYPE_WARN
) {
599 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, 0);
603 /* Try to emulate it if we should. */
604 if (reason
& (REASON_ILLEGAL
| REASON_PRIVILEGED
)) {
605 switch (emulate_instruction(regs
)) {
608 emulate_single_step(regs
);
611 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
616 if (reason
& REASON_PRIVILEGED
)
617 _exception(SIGILL
, regs
, ILL_PRVOPC
, regs
->nip
);
619 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
622 void single_step_exception(struct pt_regs
*regs
)
624 regs
->msr
&= ~(MSR_SE
| MSR_BE
); /* Turn off 'trace' bits */
625 if (debugger_sstep(regs
))
627 _exception(SIGTRAP
, regs
, TRAP_TRACE
, 0);
630 void alignment_exception(struct pt_regs
*regs
)
632 int sig
, code
, fixed
= 0;
634 fixed
= fix_alignment(regs
);
636 regs
->nip
+= 4; /* skip over emulated instruction */
637 emulate_single_step(regs
);
640 if (fixed
== -EFAULT
) {
648 _exception(sig
, regs
, code
, regs
->dar
);
650 bad_page_fault(regs
, regs
->dar
, sig
);
653 void StackOverflow(struct pt_regs
*regs
)
655 printk(KERN_CRIT
"Kernel stack overflow in process %p, r1=%lx\n",
656 current
, regs
->gpr
[1]);
659 panic("kernel stack overflow");
662 void nonrecoverable_exception(struct pt_regs
*regs
)
664 printk(KERN_ERR
"Non-recoverable exception at PC=%lx MSR=%lx\n",
665 regs
->nip
, regs
->msr
);
667 die("nonrecoverable exception", regs
, SIGKILL
);
670 void trace_syscall(struct pt_regs
*regs
)
672 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
673 current
, current
->pid
, regs
->nip
, regs
->link
, regs
->gpr
[0],
674 regs
->ccr
&0x10000000?"Error=":"", regs
->gpr
[3], print_tainted());
678 void SoftwareEmulation(struct pt_regs
*regs
)
680 extern int do_mathemu(struct pt_regs
*);
681 extern int Soft_emulate_8xx(struct pt_regs
*);
684 CHECK_FULL_REGS(regs
);
686 if (!user_mode(regs
)) {
688 die("Kernel Mode Software FPU Emulation", regs
, SIGFPE
);
691 #ifdef CONFIG_MATH_EMULATION
692 errcode
= do_mathemu(regs
);
694 errcode
= Soft_emulate_8xx(regs
);
698 _exception(SIGFPE
, regs
, 0, 0);
699 else if (errcode
== -EFAULT
)
700 _exception(SIGSEGV
, regs
, 0, 0);
702 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
704 emulate_single_step(regs
);
706 #endif /* CONFIG_8xx */
708 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
710 void DebugException(struct pt_regs
*regs
, unsigned long debug_status
)
712 if (debug_status
& DBSR_IC
) { /* instruction completion */
713 regs
->msr
&= ~MSR_DE
;
714 if (user_mode(regs
)) {
715 current
->thread
.dbcr0
&= ~DBCR0_IC
;
717 /* Disable instruction completion */
718 mtspr(SPRN_DBCR0
, mfspr(SPRN_DBCR0
) & ~DBCR0_IC
);
719 /* Clear the instruction completion event */
720 mtspr(SPRN_DBSR
, DBSR_IC
);
721 if (debugger_sstep(regs
))
724 _exception(SIGTRAP
, regs
, TRAP_TRACE
, 0);
727 #endif /* CONFIG_4xx || CONFIG_BOOKE */
729 #if !defined(CONFIG_TAU_INT)
730 void TAUException(struct pt_regs
*regs
)
732 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
733 regs
->nip
, regs
->msr
, regs
->trap
, print_tainted());
735 #endif /* CONFIG_INT_TAU */
738 * FP unavailable trap from kernel - print a message, but let
739 * the task use FP in the kernel until it returns to user mode.
741 void kernel_fp_unavailable_exception(struct pt_regs
*regs
)
744 printk(KERN_ERR
"floating point used in kernel (task=%p, pc=%lx)\n",
748 void altivec_unavailable_exception(struct pt_regs
*regs
)
750 static int kernel_altivec_count
;
752 #ifndef CONFIG_ALTIVEC
753 if (user_mode(regs
)) {
754 /* A user program has executed an altivec instruction,
755 but this kernel doesn't support altivec. */
756 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
760 /* The kernel has executed an altivec instruction without
761 first enabling altivec. Whinge but let it do it. */
762 if (++kernel_altivec_count
< 10)
763 printk(KERN_ERR
"AltiVec used in kernel (task=%p, pc=%lx)\n",
765 regs
->msr
|= MSR_VEC
;
768 #ifdef CONFIG_ALTIVEC
769 void altivec_assist_exception(struct pt_regs
*regs
)
774 if (regs
->msr
& MSR_VEC
)
775 giveup_altivec(current
);
777 if (!user_mode(regs
)) {
778 printk(KERN_ERR
"altivec assist exception in kernel mode"
779 " at %lx\n", regs
->nip
);
781 die("altivec assist exception", regs
, SIGFPE
);
785 err
= emulate_altivec(regs
);
787 regs
->nip
+= 4; /* skip emulated instruction */
788 emulate_single_step(regs
);
792 if (err
== -EFAULT
) {
793 /* got an error reading the instruction */
794 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, regs
->nip
);
796 /* didn't recognize the instruction */
797 /* XXX quick hack for now: set the non-Java bit in the VSCR */
798 printk(KERN_ERR
"unrecognized altivec instruction "
799 "in %s at %lx\n", current
->comm
, regs
->nip
);
800 current
->thread
.vscr
.u
[3] |= 0x10000;
803 #endif /* CONFIG_ALTIVEC */
805 #ifdef CONFIG_BOOKE_WDT
807 * Default handler for a Watchdog exception,
808 * spins until a reboot occurs
810 void __attribute__ ((weak
)) WatchdogHandler(struct pt_regs
*regs
)
812 /* Generic WatchdogHandler, implement your own */
813 mtspr(SPRN_TCR
, mfspr(SPRN_TCR
)&(~TCR_WIE
));
817 void WatchdogException(struct pt_regs
*regs
)
819 printk (KERN_EMERG
"PowerPC Book-E Watchdog Exception\n");
820 WatchdogHandler(regs
);
824 void __init
trap_init(void)