2 * 'traps.c' handles hardware traps and faults after we have saved some
5 * SuperH version: Copyright (C) 1999 Niibe Yutaka
6 * Copyright (C) 2000 Philipp Rumpf
7 * Copyright (C) 2000 David Howells
8 * Copyright (C) 2002 - 2007 Paul Mundt
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file "COPYING" in the main directory of this archive
14 #include <linux/kernel.h>
15 #include <linux/ptrace.h>
16 #include <linux/init.h>
17 #include <linux/spinlock.h>
18 #include <linux/module.h>
19 #include <linux/kallsyms.h>
21 #include <linux/bug.h>
22 #include <linux/debug_locks.h>
23 #include <linux/kdebug.h>
24 #include <linux/kexec.h>
25 #include <linux/limits.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
31 #define CHK_REMOTE_DEBUG(regs) \
33 if (kgdb_debug_hook && !user_mode(regs))\
34 (*kgdb_debug_hook)(regs); \
37 #define CHK_REMOTE_DEBUG(regs)
41 # define TRAP_RESERVED_INST 4
42 # define TRAP_ILLEGAL_SLOT_INST 6
43 # define TRAP_ADDRESS_ERROR 9
44 # ifdef CONFIG_CPU_SH2A
45 # define TRAP_DIVZERO_ERROR 17
46 # define TRAP_DIVOVF_ERROR 18
49 #define TRAP_RESERVED_INST 12
50 #define TRAP_ILLEGAL_SLOT_INST 13
53 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
58 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
60 for (p = bottom & ~31; p < top; ) {
61 printk("%04lx: ", p & 0xffff);
63 for (i = 0; i < 8; i++, p += 4) {
66 if (p < bottom || p >= top)
69 if (__get_user(val, (unsigned int __user *)p)) {
80 static DEFINE_SPINLOCK(die_lock);
82 void die(const char * str, struct pt_regs * regs, long err)
84 static int die_counter;
89 spin_lock_irq(&die_lock);
92 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
94 CHK_REMOTE_DEBUG(regs);
98 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
99 task_pid_nr(current), task_stack_page(current) + 1);
101 if (!user_mode(regs) || in_interrupt())
102 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
103 (unsigned long)task_stack_page(current));
106 add_taint(TAINT_DIE);
107 spin_unlock_irq(&die_lock);
109 if (kexec_should_crash(current))
113 panic("Fatal exception in interrupt");
116 panic("Fatal exception");
122 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
125 if (!user_mode(regs))
130 * try and fix up kernelspace address errors
131 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
132 * - kernel/userspace interfaces cause a jump to an appropriate handler
133 * - other kernel errors are bad
134 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
136 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
138 if (!user_mode(regs)) {
139 const struct exception_table_entry *fixup;
140 fixup = search_exception_tables(regs->pc);
142 regs->pc = fixup->fixup;
150 static inline void sign_extend(unsigned int count, unsigned char *dst)
152 #ifdef __LITTLE_ENDIAN__
153 if ((count == 1) && dst[0] & 0x80) {
158 if ((count == 2) && dst[1] & 0x80) {
163 if ((count == 1) && dst[3] & 0x80) {
168 if ((count == 2) && dst[2] & 0x80) {
176 * handle an instruction that does an unaligned memory access by emulating the
178 * - note that PC _may not_ point to the faulting instruction
179 * (if that instruction is in a branch delay slot)
180 * - return 0 if emulation okay, -EFAULT on existential error
182 static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs)
184 int ret, index, count;
185 unsigned long *rm, *rn;
186 unsigned char *src, *dst;
188 index = (instruction>>8)&15; /* 0x0F00 */
189 rn = ®s->regs[index];
191 index = (instruction>>4)&15; /* 0x00F0 */
192 rm = ®s->regs[index];
194 count = 1<<(instruction&3);
197 switch (instruction>>12) {
198 case 0: /* mov.[bwl] to/from memory via r0+rn */
199 if (instruction & 8) {
201 src = (unsigned char*) *rm;
202 src += regs->regs[0];
203 dst = (unsigned char*) rn;
204 *(unsigned long*)dst = 0;
206 #if !defined(__LITTLE_ENDIAN__)
209 if (copy_from_user(dst, src, count))
212 sign_extend(count, dst);
215 src = (unsigned char*) rm;
216 #if !defined(__LITTLE_ENDIAN__)
219 dst = (unsigned char*) *rn;
220 dst += regs->regs[0];
222 if (copy_to_user(dst, src, count))
228 case 1: /* mov.l Rm,@(disp,Rn) */
229 src = (unsigned char*) rm;
230 dst = (unsigned char*) *rn;
231 dst += (instruction&0x000F)<<2;
233 if (copy_to_user(dst,src,4))
238 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
241 src = (unsigned char*) rm;
242 dst = (unsigned char*) *rn;
243 #if !defined(__LITTLE_ENDIAN__)
246 if (copy_to_user(dst, src, count))
251 case 5: /* mov.l @(disp,Rm),Rn */
252 src = (unsigned char*) *rm;
253 src += (instruction&0x000F)<<2;
254 dst = (unsigned char*) rn;
255 *(unsigned long*)dst = 0;
257 if (copy_from_user(dst,src,4))
262 case 6: /* mov.[bwl] from memory, possibly with post-increment */
263 src = (unsigned char*) *rm;
266 dst = (unsigned char*) rn;
267 *(unsigned long*)dst = 0;
269 #if !defined(__LITTLE_ENDIAN__)
272 if (copy_from_user(dst, src, count))
274 sign_extend(count, dst);
279 switch ((instruction&0xFF00)>>8) {
280 case 0x81: /* mov.w R0,@(disp,Rn) */
281 src = (unsigned char*) ®s->regs[0];
282 #if !defined(__LITTLE_ENDIAN__)
285 dst = (unsigned char*) *rm; /* called Rn in the spec */
286 dst += (instruction&0x000F)<<1;
288 if (copy_to_user(dst, src, 2))
293 case 0x85: /* mov.w @(disp,Rm),R0 */
294 src = (unsigned char*) *rm;
295 src += (instruction&0x000F)<<1;
296 dst = (unsigned char*) ®s->regs[0];
297 *(unsigned long*)dst = 0;
299 #if !defined(__LITTLE_ENDIAN__)
302 if (copy_from_user(dst, src, 2))
313 /* Argh. Address not only misaligned but also non-existent.
314 * Raise an EFAULT and see if it's trapped
316 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
320 * emulate the instruction in the delay slot
321 * - fetches the instruction from PC+2
323 static inline int handle_unaligned_delayslot(struct pt_regs *regs,
324 opcode_t old_instruction)
326 opcode_t instruction;
327 void *addr = (void *)(regs->pc + instruction_size(old_instruction));
329 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
330 /* the instruction-fetch faulted */
335 die("delay-slot-insn faulting in handle_unaligned_delayslot",
339 return handle_unaligned_ins(instruction, regs);
343 * handle an instruction that does an unaligned memory access
344 * - have to be careful of branch delay-slot instructions that fault
346 * - if the branch would be taken PC points to the branch
347 * - if the branch would not be taken, PC points to delay-slot
349 * - PC always points to delayed branch
350 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
353 /* Macros to determine offset from current PC for branch instructions */
354 /* Explicit type coercion is used to force sign extension where needed */
355 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
356 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
359 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
363 static int handle_unaligned_notify_count = 10;
365 static int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs)
370 index = (instruction>>8)&15; /* 0x0F00 */
371 rm = regs->regs[index];
373 /* shout about the first ten userspace fixups */
374 if (user_mode(regs) && handle_unaligned_notify_count>0) {
375 handle_unaligned_notify_count--;
377 printk(KERN_NOTICE "Fixing up unaligned userspace access "
378 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
379 current->comm, task_pid_nr(current),
380 (void *)regs->pc, instruction);
384 switch (instruction&0xF000) {
386 if (instruction==0x000B) {
388 ret = handle_unaligned_delayslot(regs, instruction);
392 else if ((instruction&0x00FF)==0x0023) {
394 ret = handle_unaligned_delayslot(regs, instruction);
398 else if ((instruction&0x00FF)==0x0003) {
400 ret = handle_unaligned_delayslot(regs, instruction);
402 regs->pr = regs->pc + 4;
407 /* mov.[bwl] to/from memory via r0+rn */
412 case 0x1000: /* mov.l Rm,@(disp,Rn) */
415 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
419 if ((instruction&0x00FF)==0x002B) {
421 ret = handle_unaligned_delayslot(regs, instruction);
425 else if ((instruction&0x00FF)==0x000B) {
427 ret = handle_unaligned_delayslot(regs, instruction);
429 regs->pr = regs->pc + 4;
434 /* mov.[bwl] to/from memory via r0+rn */
439 case 0x5000: /* mov.l @(disp,Rm),Rn */
442 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
445 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
446 switch (instruction&0x0F00) {
447 case 0x0100: /* mov.w R0,@(disp,Rm) */
449 case 0x0500: /* mov.w @(disp,Rm),R0 */
451 case 0x0B00: /* bf lab - no delayslot*/
453 case 0x0F00: /* bf/s lab */
454 ret = handle_unaligned_delayslot(regs, instruction);
456 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
457 if ((regs->sr & 0x00000001) != 0)
458 regs->pc += 4; /* next after slot */
461 regs->pc += SH_PC_8BIT_OFFSET(instruction);
464 case 0x0900: /* bt lab - no delayslot */
466 case 0x0D00: /* bt/s lab */
467 ret = handle_unaligned_delayslot(regs, instruction);
469 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
470 if ((regs->sr & 0x00000001) == 0)
471 regs->pc += 4; /* next after slot */
474 regs->pc += SH_PC_8BIT_OFFSET(instruction);
480 case 0xA000: /* bra label */
481 ret = handle_unaligned_delayslot(regs, instruction);
483 regs->pc += SH_PC_12BIT_OFFSET(instruction);
486 case 0xB000: /* bsr label */
487 ret = handle_unaligned_delayslot(regs, instruction);
489 regs->pr = regs->pc + 4;
490 regs->pc += SH_PC_12BIT_OFFSET(instruction);
496 /* handle non-delay-slot instruction */
498 ret = handle_unaligned_ins(instruction, regs);
500 regs->pc += instruction_size(instruction);
504 #ifdef CONFIG_CPU_HAS_SR_RB
505 #define lookup_exception_vector(x) \
506 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
508 #define lookup_exception_vector(x) \
509 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
513 * Handle various address error exceptions:
514 * - instruction address error:
516 * PC >= 0x80000000 in user mode
517 * - data address error (read and write)
518 * misaligned data access
519 * access to >= 0x80000000 is user mode
520 * Unfortuntaly we can't distinguish between instruction address error
521 * and data address errors caused by read accesses.
523 asmlinkage void do_address_error(struct pt_regs *regs,
524 unsigned long writeaccess,
525 unsigned long address)
527 unsigned long error_code = 0;
530 opcode_t instruction;
533 /* Intentional ifdef */
534 #ifdef CONFIG_CPU_HAS_SR_RB
535 lookup_exception_vector(error_code);
540 if (user_mode(regs)) {
541 int si_code = BUS_ADRERR;
545 /* bad PC is not something we can fix */
547 si_code = BUS_ADRALN;
552 if (copy_from_user(&instruction, (void *)(regs->pc),
553 sizeof(instruction))) {
554 /* Argh. Fault on the instruction itself.
555 This should never happen non-SMP
561 tmp = handle_unaligned_access(instruction, regs);
567 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
568 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
571 info.si_signo = SIGBUS;
573 info.si_code = si_code;
574 info.si_addr = (void __user *)address;
575 force_sig_info(SIGBUS, &info, current);
578 die("unaligned program counter", regs, error_code);
581 if (copy_from_user(&instruction, (void *)(regs->pc),
582 sizeof(instruction))) {
583 /* Argh. Fault on the instruction itself.
584 This should never happen non-SMP
587 die("insn faulting in do_address_error", regs, 0);
590 handle_unaligned_access(instruction, regs);
597 * SH-DSP support gerg@snapgear.com.
599 int is_dsp_inst(struct pt_regs *regs)
601 unsigned short inst = 0;
604 * Safe guard if DSP mode is already enabled or we're lacking
605 * the DSP altogether.
607 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
610 get_user(inst, ((unsigned short *) regs->pc));
614 /* Check for any type of DSP or support instruction */
615 if ((inst == 0xf000) || (inst == 0x4000))
621 #define is_dsp_inst(regs) (0)
622 #endif /* CONFIG_SH_DSP */
624 #ifdef CONFIG_CPU_SH2A
625 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
626 unsigned long r6, unsigned long r7,
627 struct pt_regs __regs)
632 case TRAP_DIVZERO_ERROR:
633 info.si_code = FPE_INTDIV;
635 case TRAP_DIVOVF_ERROR:
636 info.si_code = FPE_INTOVF;
640 force_sig_info(SIGFPE, &info, current);
644 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
645 unsigned long r6, unsigned long r7,
646 struct pt_regs __regs)
648 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
649 unsigned long error_code;
650 struct task_struct *tsk = current;
652 #ifdef CONFIG_SH_FPU_EMU
653 unsigned short inst = 0;
656 get_user(inst, (unsigned short*)regs->pc);
658 err = do_fpu_inst(inst, regs);
660 regs->pc += instruction_size(inst);
663 /* not a FPU inst. */
667 /* Check if it's a DSP instruction */
668 if (is_dsp_inst(regs)) {
669 /* Enable DSP mode, and restart instruction. */
675 lookup_exception_vector(error_code);
678 CHK_REMOTE_DEBUG(regs);
679 force_sig(SIGILL, tsk);
680 die_if_no_fixup("reserved instruction", regs, error_code);
683 #ifdef CONFIG_SH_FPU_EMU
684 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
687 * bfs: 8fxx: PC+=d*2+4;
688 * bts: 8dxx: PC+=d*2+4;
689 * bra: axxx: PC+=D*2+4;
690 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
691 * braf:0x23: PC+=Rn*2+4;
692 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
694 * jsr: 4x0b: PC=Rn after PR=PC+4;
697 if ((inst & 0xfd00) == 0x8d00) {
698 regs->pc += SH_PC_8BIT_OFFSET(inst);
702 if ((inst & 0xe000) == 0xa000) {
703 regs->pc += SH_PC_12BIT_OFFSET(inst);
707 if ((inst & 0xf0df) == 0x0003) {
708 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
712 if ((inst & 0xf0df) == 0x400b) {
713 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
717 if ((inst & 0xffff) == 0x000b) {
726 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
727 unsigned long r6, unsigned long r7,
728 struct pt_regs __regs)
730 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
731 unsigned long error_code;
732 struct task_struct *tsk = current;
733 #ifdef CONFIG_SH_FPU_EMU
734 unsigned short inst = 0;
736 get_user(inst, (unsigned short *)regs->pc + 1);
737 if (!do_fpu_inst(inst, regs)) {
738 get_user(inst, (unsigned short *)regs->pc);
739 if (!emulate_branch(inst, regs))
741 /* fault in branch.*/
743 /* not a FPU inst. */
746 lookup_exception_vector(error_code);
749 CHK_REMOTE_DEBUG(regs);
750 force_sig(SIGILL, tsk);
751 die_if_no_fixup("illegal slot instruction", regs, error_code);
754 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
755 unsigned long r6, unsigned long r7,
756 struct pt_regs __regs)
758 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
761 lookup_exception_vector(ex);
762 die_if_kernel("exception", regs, ex);
765 #if defined(CONFIG_SH_STANDARD_BIOS)
766 void *gdb_vbr_vector;
768 static inline void __init gdb_vbr_init(void)
770 register unsigned long vbr;
773 * Read the old value of the VBR register to initialise
774 * the vector through which debug and BIOS traps are
775 * delegated by the Linux trap handler.
777 asm volatile("stc vbr, %0" : "=r" (vbr));
779 gdb_vbr_vector = (void *)(vbr + 0x100);
780 printk("Setting GDB trap vector to 0x%08lx\n",
781 (unsigned long)gdb_vbr_vector);
785 void __cpuinit per_cpu_trap_init(void)
787 extern void *vbr_base;
789 #ifdef CONFIG_SH_STANDARD_BIOS
790 if (raw_smp_processor_id() == 0)
794 /* NOTE: The VBR value should be at P1
795 (or P2, virtural "fixed" address space).
796 It's definitely should not in physical address. */
798 asm volatile("ldc %0, vbr"
804 void *set_exception_table_vec(unsigned int vec, void *handler)
806 extern void *exception_handling_table[];
809 old_handler = exception_handling_table[vec];
810 exception_handling_table[vec] = handler;
814 void __init trap_init(void)
816 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
817 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
819 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
820 defined(CONFIG_SH_FPU_EMU)
822 * For SH-4 lacking an FPU, treat floating point instructions as
823 * reserved. They'll be handled in the math-emu case, or faulted on
826 set_exception_table_evt(0x800, do_reserved_inst);
827 set_exception_table_evt(0x820, do_illegal_slot_inst);
828 #elif defined(CONFIG_SH_FPU)
829 #ifdef CONFIG_CPU_SUBTYPE_SHX3
830 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
831 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
833 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
834 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
838 #ifdef CONFIG_CPU_SH2
839 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
841 #ifdef CONFIG_CPU_SH2A
842 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
843 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
846 /* Setup VBR for boot cpu */
850 void show_trace(struct task_struct *tsk, unsigned long *sp,
851 struct pt_regs *regs)
855 if (regs && user_mode(regs))
858 printk("\nCall trace: ");
859 #ifdef CONFIG_KALLSYMS
863 while (!kstack_end(sp)) {
865 if (kernel_text_address(addr))
874 debug_show_held_locks(tsk);
877 void show_stack(struct task_struct *tsk, unsigned long *sp)
884 sp = (unsigned long *)current_stack_pointer;
886 sp = (unsigned long *)tsk->thread.sp;
888 stack = (unsigned long)sp;
889 dump_mem("Stack: ", stack, THREAD_SIZE +
890 (unsigned long)task_stack_page(tsk));
891 show_trace(tsk, sp, NULL);
894 void dump_stack(void)
896 show_stack(NULL, NULL);
898 EXPORT_SYMBOL(dump_stack);