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>
32 #define CHK_REMOTE_DEBUG(regs) \
34 if (kgdb_debug_hook && !user_mode(regs))\
35 (*kgdb_debug_hook)(regs); \
38 #define CHK_REMOTE_DEBUG(regs)
42 # define TRAP_RESERVED_INST 4
43 # define TRAP_ILLEGAL_SLOT_INST 6
44 # define TRAP_ADDRESS_ERROR 9
45 # ifdef CONFIG_CPU_SH2A
46 # define TRAP_FPU_ERROR 13
47 # define TRAP_DIVZERO_ERROR 17
48 # define TRAP_DIVOVF_ERROR 18
51 #define TRAP_RESERVED_INST 12
52 #define TRAP_ILLEGAL_SLOT_INST 13
55 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
60 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
62 for (p = bottom & ~31; p < top; ) {
63 printk("%04lx: ", p & 0xffff);
65 for (i = 0; i < 8; i++, p += 4) {
68 if (p < bottom || p >= top)
71 if (__get_user(val, (unsigned int __user *)p)) {
82 static DEFINE_SPINLOCK(die_lock);
84 void die(const char * str, struct pt_regs * regs, long err)
86 static int die_counter;
91 spin_lock_irq(&die_lock);
94 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
96 CHK_REMOTE_DEBUG(regs);
100 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
101 task_pid_nr(current), task_stack_page(current) + 1);
103 if (!user_mode(regs) || in_interrupt())
104 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
105 (unsigned long)task_stack_page(current));
108 add_taint(TAINT_DIE);
109 spin_unlock_irq(&die_lock);
111 if (kexec_should_crash(current))
115 panic("Fatal exception in interrupt");
118 panic("Fatal exception");
124 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
127 if (!user_mode(regs))
132 * try and fix up kernelspace address errors
133 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
134 * - kernel/userspace interfaces cause a jump to an appropriate handler
135 * - other kernel errors are bad
136 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
138 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
140 if (!user_mode(regs)) {
141 const struct exception_table_entry *fixup;
142 fixup = search_exception_tables(regs->pc);
144 regs->pc = fixup->fixup;
152 static inline void sign_extend(unsigned int count, unsigned char *dst)
154 #ifdef __LITTLE_ENDIAN__
155 if ((count == 1) && dst[0] & 0x80) {
160 if ((count == 2) && dst[1] & 0x80) {
165 if ((count == 1) && dst[3] & 0x80) {
170 if ((count == 2) && dst[2] & 0x80) {
177 static struct mem_access user_mem_access = {
183 * handle an instruction that does an unaligned memory access by emulating the
185 * - note that PC _may not_ point to the faulting instruction
186 * (if that instruction is in a branch delay slot)
187 * - return 0 if emulation okay, -EFAULT on existential error
189 static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
190 struct mem_access *ma)
192 int ret, index, count;
193 unsigned long *rm, *rn;
194 unsigned char *src, *dst;
196 index = (instruction>>8)&15; /* 0x0F00 */
197 rn = ®s->regs[index];
199 index = (instruction>>4)&15; /* 0x00F0 */
200 rm = ®s->regs[index];
202 count = 1<<(instruction&3);
205 switch (instruction>>12) {
206 case 0: /* mov.[bwl] to/from memory via r0+rn */
207 if (instruction & 8) {
209 src = (unsigned char*) *rm;
210 src += regs->regs[0];
211 dst = (unsigned char*) rn;
212 *(unsigned long*)dst = 0;
214 #if !defined(__LITTLE_ENDIAN__)
217 if (ma->from(dst, src, count))
220 sign_extend(count, dst);
223 src = (unsigned char*) rm;
224 #if !defined(__LITTLE_ENDIAN__)
227 dst = (unsigned char*) *rn;
228 dst += regs->regs[0];
230 if (ma->to(dst, src, count))
236 case 1: /* mov.l Rm,@(disp,Rn) */
237 src = (unsigned char*) rm;
238 dst = (unsigned char*) *rn;
239 dst += (instruction&0x000F)<<2;
241 if (ma->to(dst, src, 4))
246 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
249 src = (unsigned char*) rm;
250 dst = (unsigned char*) *rn;
251 #if !defined(__LITTLE_ENDIAN__)
254 if (ma->to(dst, src, count))
259 case 5: /* mov.l @(disp,Rm),Rn */
260 src = (unsigned char*) *rm;
261 src += (instruction&0x000F)<<2;
262 dst = (unsigned char*) rn;
263 *(unsigned long*)dst = 0;
265 if (ma->from(dst, src, 4))
270 case 6: /* mov.[bwl] from memory, possibly with post-increment */
271 src = (unsigned char*) *rm;
274 dst = (unsigned char*) rn;
275 *(unsigned long*)dst = 0;
277 #if !defined(__LITTLE_ENDIAN__)
280 if (ma->from(dst, src, count))
282 sign_extend(count, dst);
287 switch ((instruction&0xFF00)>>8) {
288 case 0x81: /* mov.w R0,@(disp,Rn) */
289 src = (unsigned char*) ®s->regs[0];
290 #if !defined(__LITTLE_ENDIAN__)
293 dst = (unsigned char*) *rm; /* called Rn in the spec */
294 dst += (instruction&0x000F)<<1;
296 if (ma->to(dst, src, 2))
301 case 0x85: /* mov.w @(disp,Rm),R0 */
302 src = (unsigned char*) *rm;
303 src += (instruction&0x000F)<<1;
304 dst = (unsigned char*) ®s->regs[0];
305 *(unsigned long*)dst = 0;
307 #if !defined(__LITTLE_ENDIAN__)
310 if (ma->from(dst, src, 2))
321 /* Argh. Address not only misaligned but also non-existent.
322 * Raise an EFAULT and see if it's trapped
324 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
328 * emulate the instruction in the delay slot
329 * - fetches the instruction from PC+2
331 static inline int handle_delayslot(struct pt_regs *regs,
332 opcode_t old_instruction,
333 struct mem_access *ma)
335 opcode_t instruction;
336 void *addr = (void *)(regs->pc + instruction_size(old_instruction));
338 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
339 /* the instruction-fetch faulted */
344 die("delay-slot-insn faulting in handle_unaligned_delayslot",
348 return handle_unaligned_ins(instruction, regs, ma);
352 * handle an instruction that does an unaligned memory access
353 * - have to be careful of branch delay-slot instructions that fault
355 * - if the branch would be taken PC points to the branch
356 * - if the branch would not be taken, PC points to delay-slot
358 * - PC always points to delayed branch
359 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
362 /* Macros to determine offset from current PC for branch instructions */
363 /* Explicit type coercion is used to force sign extension where needed */
364 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
365 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
368 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
372 static int handle_unaligned_notify_count = 10;
374 int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs,
375 struct mem_access *ma)
380 index = (instruction>>8)&15; /* 0x0F00 */
381 rm = regs->regs[index];
383 /* shout about the first ten userspace fixups */
384 if (user_mode(regs) && handle_unaligned_notify_count>0) {
385 handle_unaligned_notify_count--;
387 printk(KERN_NOTICE "Fixing up unaligned userspace access "
388 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
389 current->comm, task_pid_nr(current),
390 (void *)regs->pc, instruction);
394 switch (instruction&0xF000) {
396 if (instruction==0x000B) {
398 ret = handle_delayslot(regs, instruction, ma);
402 else if ((instruction&0x00FF)==0x0023) {
404 ret = handle_delayslot(regs, instruction, ma);
408 else if ((instruction&0x00FF)==0x0003) {
410 ret = handle_delayslot(regs, instruction, ma);
412 regs->pr = regs->pc + 4;
417 /* mov.[bwl] to/from memory via r0+rn */
422 case 0x1000: /* mov.l Rm,@(disp,Rn) */
425 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
429 if ((instruction&0x00FF)==0x002B) {
431 ret = handle_delayslot(regs, instruction, ma);
435 else if ((instruction&0x00FF)==0x000B) {
437 ret = handle_delayslot(regs, instruction, ma);
439 regs->pr = regs->pc + 4;
444 /* mov.[bwl] to/from memory via r0+rn */
449 case 0x5000: /* mov.l @(disp,Rm),Rn */
452 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
455 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
456 switch (instruction&0x0F00) {
457 case 0x0100: /* mov.w R0,@(disp,Rm) */
459 case 0x0500: /* mov.w @(disp,Rm),R0 */
461 case 0x0B00: /* bf lab - no delayslot*/
463 case 0x0F00: /* bf/s lab */
464 ret = handle_delayslot(regs, instruction, ma);
466 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
467 if ((regs->sr & 0x00000001) != 0)
468 regs->pc += 4; /* next after slot */
471 regs->pc += SH_PC_8BIT_OFFSET(instruction);
474 case 0x0900: /* bt lab - no delayslot */
476 case 0x0D00: /* bt/s lab */
477 ret = handle_delayslot(regs, instruction, ma);
479 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
480 if ((regs->sr & 0x00000001) == 0)
481 regs->pc += 4; /* next after slot */
484 regs->pc += SH_PC_8BIT_OFFSET(instruction);
490 case 0xA000: /* bra label */
491 ret = handle_delayslot(regs, instruction, ma);
493 regs->pc += SH_PC_12BIT_OFFSET(instruction);
496 case 0xB000: /* bsr label */
497 ret = handle_delayslot(regs, instruction, ma);
499 regs->pr = regs->pc + 4;
500 regs->pc += SH_PC_12BIT_OFFSET(instruction);
506 /* handle non-delay-slot instruction */
508 ret = handle_unaligned_ins(instruction, regs, ma);
510 regs->pc += instruction_size(instruction);
514 #ifdef CONFIG_CPU_HAS_SR_RB
515 #define lookup_exception_vector(x) \
516 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
518 #define lookup_exception_vector(x) \
519 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
523 * Handle various address error exceptions:
524 * - instruction address error:
526 * PC >= 0x80000000 in user mode
527 * - data address error (read and write)
528 * misaligned data access
529 * access to >= 0x80000000 is user mode
530 * Unfortuntaly we can't distinguish between instruction address error
531 * and data address errors caused by read accesses.
533 asmlinkage void do_address_error(struct pt_regs *regs,
534 unsigned long writeaccess,
535 unsigned long address)
537 unsigned long error_code = 0;
540 opcode_t instruction;
543 /* Intentional ifdef */
544 #ifdef CONFIG_CPU_HAS_SR_RB
545 lookup_exception_vector(error_code);
550 if (user_mode(regs)) {
551 int si_code = BUS_ADRERR;
555 /* bad PC is not something we can fix */
557 si_code = BUS_ADRALN;
562 if (copy_from_user(&instruction, (void *)(regs->pc),
563 sizeof(instruction))) {
564 /* Argh. Fault on the instruction itself.
565 This should never happen non-SMP
571 tmp = handle_unaligned_access(instruction, regs,
578 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
579 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
582 info.si_signo = SIGBUS;
584 info.si_code = si_code;
585 info.si_addr = (void __user *)address;
586 force_sig_info(SIGBUS, &info, current);
589 die("unaligned program counter", regs, error_code);
592 if (copy_from_user(&instruction, (void *)(regs->pc),
593 sizeof(instruction))) {
594 /* Argh. Fault on the instruction itself.
595 This should never happen non-SMP
598 die("insn faulting in do_address_error", regs, 0);
601 handle_unaligned_access(instruction, regs, &user_mem_access);
608 * SH-DSP support gerg@snapgear.com.
610 int is_dsp_inst(struct pt_regs *regs)
612 unsigned short inst = 0;
615 * Safe guard if DSP mode is already enabled or we're lacking
616 * the DSP altogether.
618 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
621 get_user(inst, ((unsigned short *) regs->pc));
625 /* Check for any type of DSP or support instruction */
626 if ((inst == 0xf000) || (inst == 0x4000))
632 #define is_dsp_inst(regs) (0)
633 #endif /* CONFIG_SH_DSP */
635 #ifdef CONFIG_CPU_SH2A
636 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
637 unsigned long r6, unsigned long r7,
638 struct pt_regs __regs)
643 case TRAP_DIVZERO_ERROR:
644 info.si_code = FPE_INTDIV;
646 case TRAP_DIVOVF_ERROR:
647 info.si_code = FPE_INTOVF;
651 force_sig_info(SIGFPE, &info, current);
655 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
656 unsigned long r6, unsigned long r7,
657 struct pt_regs __regs)
659 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
660 unsigned long error_code;
661 struct task_struct *tsk = current;
663 #ifdef CONFIG_SH_FPU_EMU
664 unsigned short inst = 0;
667 get_user(inst, (unsigned short*)regs->pc);
669 err = do_fpu_inst(inst, regs);
671 regs->pc += instruction_size(inst);
674 /* not a FPU inst. */
678 /* Check if it's a DSP instruction */
679 if (is_dsp_inst(regs)) {
680 /* Enable DSP mode, and restart instruction. */
686 lookup_exception_vector(error_code);
689 CHK_REMOTE_DEBUG(regs);
690 force_sig(SIGILL, tsk);
691 die_if_no_fixup("reserved instruction", regs, error_code);
694 #ifdef CONFIG_SH_FPU_EMU
695 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
698 * bfs: 8fxx: PC+=d*2+4;
699 * bts: 8dxx: PC+=d*2+4;
700 * bra: axxx: PC+=D*2+4;
701 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
702 * braf:0x23: PC+=Rn*2+4;
703 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
705 * jsr: 4x0b: PC=Rn after PR=PC+4;
708 if ((inst & 0xfd00) == 0x8d00) {
709 regs->pc += SH_PC_8BIT_OFFSET(inst);
713 if ((inst & 0xe000) == 0xa000) {
714 regs->pc += SH_PC_12BIT_OFFSET(inst);
718 if ((inst & 0xf0df) == 0x0003) {
719 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
723 if ((inst & 0xf0df) == 0x400b) {
724 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
728 if ((inst & 0xffff) == 0x000b) {
737 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
738 unsigned long r6, unsigned long r7,
739 struct pt_regs __regs)
741 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
742 unsigned long error_code;
743 struct task_struct *tsk = current;
744 #ifdef CONFIG_SH_FPU_EMU
745 unsigned short inst = 0;
747 get_user(inst, (unsigned short *)regs->pc + 1);
748 if (!do_fpu_inst(inst, regs)) {
749 get_user(inst, (unsigned short *)regs->pc);
750 if (!emulate_branch(inst, regs))
752 /* fault in branch.*/
754 /* not a FPU inst. */
757 lookup_exception_vector(error_code);
760 CHK_REMOTE_DEBUG(regs);
761 force_sig(SIGILL, tsk);
762 die_if_no_fixup("illegal slot instruction", regs, error_code);
765 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
766 unsigned long r6, unsigned long r7,
767 struct pt_regs __regs)
769 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
772 lookup_exception_vector(ex);
773 die_if_kernel("exception", regs, ex);
776 #if defined(CONFIG_SH_STANDARD_BIOS)
777 void *gdb_vbr_vector;
779 static inline void __init gdb_vbr_init(void)
781 register unsigned long vbr;
784 * Read the old value of the VBR register to initialise
785 * the vector through which debug and BIOS traps are
786 * delegated by the Linux trap handler.
788 asm volatile("stc vbr, %0" : "=r" (vbr));
790 gdb_vbr_vector = (void *)(vbr + 0x100);
791 printk("Setting GDB trap vector to 0x%08lx\n",
792 (unsigned long)gdb_vbr_vector);
796 void __cpuinit per_cpu_trap_init(void)
798 extern void *vbr_base;
800 #ifdef CONFIG_SH_STANDARD_BIOS
801 if (raw_smp_processor_id() == 0)
805 /* NOTE: The VBR value should be at P1
806 (or P2, virtural "fixed" address space).
807 It's definitely should not in physical address. */
809 asm volatile("ldc %0, vbr"
815 void *set_exception_table_vec(unsigned int vec, void *handler)
817 extern void *exception_handling_table[];
820 old_handler = exception_handling_table[vec];
821 exception_handling_table[vec] = handler;
825 void __init trap_init(void)
827 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
828 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
830 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
831 defined(CONFIG_SH_FPU_EMU)
833 * For SH-4 lacking an FPU, treat floating point instructions as
834 * reserved. They'll be handled in the math-emu case, or faulted on
837 set_exception_table_evt(0x800, do_reserved_inst);
838 set_exception_table_evt(0x820, do_illegal_slot_inst);
839 #elif defined(CONFIG_SH_FPU)
840 #ifdef CONFIG_CPU_SUBTYPE_SHX3
841 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
842 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
844 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
845 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
849 #ifdef CONFIG_CPU_SH2
850 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
852 #ifdef CONFIG_CPU_SH2A
853 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
854 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
856 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
860 /* Setup VBR for boot cpu */
864 void show_trace(struct task_struct *tsk, unsigned long *sp,
865 struct pt_regs *regs)
869 if (regs && user_mode(regs))
872 printk("\nCall trace: ");
873 #ifdef CONFIG_KALLSYMS
877 while (!kstack_end(sp)) {
879 if (kernel_text_address(addr))
888 debug_show_held_locks(tsk);
891 void show_stack(struct task_struct *tsk, unsigned long *sp)
898 sp = (unsigned long *)current_stack_pointer;
900 sp = (unsigned long *)tsk->thread.sp;
902 stack = (unsigned long)sp;
903 dump_mem("Stack: ", stack, THREAD_SIZE +
904 (unsigned long)task_stack_page(tsk));
905 show_trace(tsk, sp, NULL);
908 void dump_stack(void)
910 show_stack(NULL, NULL);
912 EXPORT_SYMBOL(dump_stack);