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>
29 #include <asm/kprobes.h>
33 #define CHK_REMOTE_DEBUG(regs) \
35 if (kgdb_debug_hook && !user_mode(regs))\
36 (*kgdb_debug_hook)(regs); \
39 #define CHK_REMOTE_DEBUG(regs)
43 # define TRAP_RESERVED_INST 4
44 # define TRAP_ILLEGAL_SLOT_INST 6
45 # define TRAP_ADDRESS_ERROR 9
46 # ifdef CONFIG_CPU_SH2A
47 # define TRAP_FPU_ERROR 13
48 # define TRAP_DIVZERO_ERROR 17
49 # define TRAP_DIVOVF_ERROR 18
52 #define TRAP_RESERVED_INST 12
53 #define TRAP_ILLEGAL_SLOT_INST 13
56 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
61 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
63 for (p = bottom & ~31; p < top; ) {
64 printk("%04lx: ", p & 0xffff);
66 for (i = 0; i < 8; i++, p += 4) {
69 if (p < bottom || p >= top)
72 if (__get_user(val, (unsigned int __user *)p)) {
83 static DEFINE_SPINLOCK(die_lock);
85 void die(const char * str, struct pt_regs * regs, long err)
87 static int die_counter;
92 spin_lock_irq(&die_lock);
95 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
97 CHK_REMOTE_DEBUG(regs);
101 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
102 task_pid_nr(current), task_stack_page(current) + 1);
104 if (!user_mode(regs) || in_interrupt())
105 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
106 (unsigned long)task_stack_page(current));
109 add_taint(TAINT_DIE);
110 spin_unlock_irq(&die_lock);
112 if (kexec_should_crash(current))
116 panic("Fatal exception in interrupt");
119 panic("Fatal exception");
125 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
128 if (!user_mode(regs))
133 * try and fix up kernelspace address errors
134 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
135 * - kernel/userspace interfaces cause a jump to an appropriate handler
136 * - other kernel errors are bad
137 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
139 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
141 if (!user_mode(regs)) {
142 const struct exception_table_entry *fixup;
143 fixup = search_exception_tables(regs->pc);
145 regs->pc = fixup->fixup;
153 static inline void sign_extend(unsigned int count, unsigned char *dst)
155 #ifdef __LITTLE_ENDIAN__
156 if ((count == 1) && dst[0] & 0x80) {
161 if ((count == 2) && dst[1] & 0x80) {
166 if ((count == 1) && dst[3] & 0x80) {
171 if ((count == 2) && dst[2] & 0x80) {
178 static struct mem_access user_mem_access = {
184 * handle an instruction that does an unaligned memory access by emulating the
186 * - note that PC _may not_ point to the faulting instruction
187 * (if that instruction is in a branch delay slot)
188 * - return 0 if emulation okay, -EFAULT on existential error
190 static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
191 struct mem_access *ma)
193 int ret, index, count;
194 unsigned long *rm, *rn;
195 unsigned char *src, *dst;
196 unsigned char __user *srcu, *dstu;
198 index = (instruction>>8)&15; /* 0x0F00 */
199 rn = ®s->regs[index];
201 index = (instruction>>4)&15; /* 0x00F0 */
202 rm = ®s->regs[index];
204 count = 1<<(instruction&3);
207 switch (instruction>>12) {
208 case 0: /* mov.[bwl] to/from memory via r0+rn */
209 if (instruction & 8) {
211 srcu = (unsigned char __user *)*rm;
212 srcu += regs->regs[0];
213 dst = (unsigned char *)rn;
214 *(unsigned long *)dst = 0;
216 #if !defined(__LITTLE_ENDIAN__)
219 if (ma->from(dst, srcu, count))
222 sign_extend(count, dst);
225 src = (unsigned char *)rm;
226 #if !defined(__LITTLE_ENDIAN__)
229 dstu = (unsigned char __user *)*rn;
230 dstu += regs->regs[0];
232 if (ma->to(dstu, src, count))
238 case 1: /* mov.l Rm,@(disp,Rn) */
239 src = (unsigned char*) rm;
240 dstu = (unsigned char __user *)*rn;
241 dstu += (instruction&0x000F)<<2;
243 if (ma->to(dstu, src, 4))
248 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
251 src = (unsigned char*) rm;
252 dstu = (unsigned char __user *)*rn;
253 #if !defined(__LITTLE_ENDIAN__)
256 if (ma->to(dstu, src, count))
261 case 5: /* mov.l @(disp,Rm),Rn */
262 srcu = (unsigned char __user *)*rm;
263 srcu += (instruction & 0x000F) << 2;
264 dst = (unsigned char *)rn;
265 *(unsigned long *)dst = 0;
267 if (ma->from(dst, srcu, 4))
272 case 6: /* mov.[bwl] from memory, possibly with post-increment */
273 srcu = (unsigned char __user *)*rm;
276 dst = (unsigned char*) rn;
277 *(unsigned long*)dst = 0;
279 #if !defined(__LITTLE_ENDIAN__)
282 if (ma->from(dst, srcu, count))
284 sign_extend(count, dst);
289 switch ((instruction&0xFF00)>>8) {
290 case 0x81: /* mov.w R0,@(disp,Rn) */
291 src = (unsigned char *) ®s->regs[0];
292 #if !defined(__LITTLE_ENDIAN__)
295 dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
296 dstu += (instruction & 0x000F) << 1;
298 if (ma->to(dstu, src, 2))
303 case 0x85: /* mov.w @(disp,Rm),R0 */
304 srcu = (unsigned char __user *)*rm;
305 srcu += (instruction & 0x000F) << 1;
306 dst = (unsigned char *) ®s->regs[0];
307 *(unsigned long *)dst = 0;
309 #if !defined(__LITTLE_ENDIAN__)
312 if (ma->from(dst, srcu, 2))
323 /* Argh. Address not only misaligned but also non-existent.
324 * Raise an EFAULT and see if it's trapped
326 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
330 * emulate the instruction in the delay slot
331 * - fetches the instruction from PC+2
333 static inline int handle_delayslot(struct pt_regs *regs,
334 opcode_t old_instruction,
335 struct mem_access *ma)
337 opcode_t instruction;
338 void __user *addr = (void __user *)(regs->pc +
339 instruction_size(old_instruction));
341 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
342 /* the instruction-fetch faulted */
347 die("delay-slot-insn faulting in handle_unaligned_delayslot",
351 return handle_unaligned_ins(instruction, regs, ma);
355 * handle an instruction that does an unaligned memory access
356 * - have to be careful of branch delay-slot instructions that fault
358 * - if the branch would be taken PC points to the branch
359 * - if the branch would not be taken, PC points to delay-slot
361 * - PC always points to delayed branch
362 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
365 /* Macros to determine offset from current PC for branch instructions */
366 /* Explicit type coercion is used to force sign extension where needed */
367 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
368 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
371 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
375 static int handle_unaligned_notify_count = 10;
377 int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs,
378 struct mem_access *ma)
383 index = (instruction>>8)&15; /* 0x0F00 */
384 rm = regs->regs[index];
386 /* shout about the first ten userspace fixups */
387 if (user_mode(regs) && handle_unaligned_notify_count>0) {
388 handle_unaligned_notify_count--;
390 printk(KERN_NOTICE "Fixing up unaligned userspace access "
391 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
392 current->comm, task_pid_nr(current),
393 (void *)regs->pc, instruction);
397 switch (instruction&0xF000) {
399 if (instruction==0x000B) {
401 ret = handle_delayslot(regs, instruction, ma);
405 else if ((instruction&0x00FF)==0x0023) {
407 ret = handle_delayslot(regs, instruction, ma);
411 else if ((instruction&0x00FF)==0x0003) {
413 ret = handle_delayslot(regs, instruction, ma);
415 regs->pr = regs->pc + 4;
420 /* mov.[bwl] to/from memory via r0+rn */
425 case 0x1000: /* mov.l Rm,@(disp,Rn) */
428 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
432 if ((instruction&0x00FF)==0x002B) {
434 ret = handle_delayslot(regs, instruction, ma);
438 else if ((instruction&0x00FF)==0x000B) {
440 ret = handle_delayslot(regs, instruction, ma);
442 regs->pr = regs->pc + 4;
447 /* mov.[bwl] to/from memory via r0+rn */
452 case 0x5000: /* mov.l @(disp,Rm),Rn */
455 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
458 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
459 switch (instruction&0x0F00) {
460 case 0x0100: /* mov.w R0,@(disp,Rm) */
462 case 0x0500: /* mov.w @(disp,Rm),R0 */
464 case 0x0B00: /* bf lab - no delayslot*/
466 case 0x0F00: /* bf/s lab */
467 ret = handle_delayslot(regs, instruction, ma);
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);
477 case 0x0900: /* bt lab - no delayslot */
479 case 0x0D00: /* bt/s lab */
480 ret = handle_delayslot(regs, instruction, ma);
482 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
483 if ((regs->sr & 0x00000001) == 0)
484 regs->pc += 4; /* next after slot */
487 regs->pc += SH_PC_8BIT_OFFSET(instruction);
493 case 0xA000: /* bra label */
494 ret = handle_delayslot(regs, instruction, ma);
496 regs->pc += SH_PC_12BIT_OFFSET(instruction);
499 case 0xB000: /* bsr label */
500 ret = handle_delayslot(regs, instruction, ma);
502 regs->pr = regs->pc + 4;
503 regs->pc += SH_PC_12BIT_OFFSET(instruction);
509 /* handle non-delay-slot instruction */
511 ret = handle_unaligned_ins(instruction, regs, ma);
513 regs->pc += instruction_size(instruction);
517 #ifdef CONFIG_CPU_HAS_SR_RB
518 #define lookup_exception_vector(x) \
519 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
521 #define lookup_exception_vector(x) \
522 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
526 * Handle various address error exceptions:
527 * - instruction address error:
529 * PC >= 0x80000000 in user mode
530 * - data address error (read and write)
531 * misaligned data access
532 * access to >= 0x80000000 is user mode
533 * Unfortuntaly we can't distinguish between instruction address error
534 * and data address errors caused by read accesses.
536 asmlinkage void do_address_error(struct pt_regs *regs,
537 unsigned long writeaccess,
538 unsigned long address)
540 unsigned long error_code = 0;
543 opcode_t instruction;
546 /* Intentional ifdef */
547 #ifdef CONFIG_CPU_HAS_SR_RB
548 lookup_exception_vector(error_code);
553 if (user_mode(regs)) {
554 int si_code = BUS_ADRERR;
558 /* bad PC is not something we can fix */
560 si_code = BUS_ADRALN;
565 if (copy_from_user(&instruction, (void __user *)(regs->pc),
566 sizeof(instruction))) {
567 /* Argh. Fault on the instruction itself.
568 This should never happen non-SMP
574 tmp = handle_unaligned_access(instruction, regs,
581 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
582 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
585 info.si_signo = SIGBUS;
587 info.si_code = si_code;
588 info.si_addr = (void __user *)address;
589 force_sig_info(SIGBUS, &info, current);
592 die("unaligned program counter", regs, error_code);
595 if (copy_from_user(&instruction, (void __user *)(regs->pc),
596 sizeof(instruction))) {
597 /* Argh. Fault on the instruction itself.
598 This should never happen non-SMP
601 die("insn faulting in do_address_error", regs, 0);
604 handle_unaligned_access(instruction, regs, &user_mem_access);
611 * SH-DSP support gerg@snapgear.com.
613 int is_dsp_inst(struct pt_regs *regs)
615 unsigned short inst = 0;
618 * Safe guard if DSP mode is already enabled or we're lacking
619 * the DSP altogether.
621 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
624 get_user(inst, ((unsigned short *) regs->pc));
628 /* Check for any type of DSP or support instruction */
629 if ((inst == 0xf000) || (inst == 0x4000))
635 #define is_dsp_inst(regs) (0)
636 #endif /* CONFIG_SH_DSP */
638 #ifdef CONFIG_CPU_SH2A
639 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
640 unsigned long r6, unsigned long r7,
641 struct pt_regs __regs)
646 case TRAP_DIVZERO_ERROR:
647 info.si_code = FPE_INTDIV;
649 case TRAP_DIVOVF_ERROR:
650 info.si_code = FPE_INTOVF;
654 force_sig_info(SIGFPE, &info, current);
658 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
659 unsigned long r6, unsigned long r7,
660 struct pt_regs __regs)
662 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
663 unsigned long error_code;
664 struct task_struct *tsk = current;
666 #ifdef CONFIG_SH_FPU_EMU
667 unsigned short inst = 0;
670 get_user(inst, (unsigned short*)regs->pc);
672 err = do_fpu_inst(inst, regs);
674 regs->pc += instruction_size(inst);
677 /* not a FPU inst. */
681 /* Check if it's a DSP instruction */
682 if (is_dsp_inst(regs)) {
683 /* Enable DSP mode, and restart instruction. */
689 lookup_exception_vector(error_code);
692 CHK_REMOTE_DEBUG(regs);
693 force_sig(SIGILL, tsk);
694 die_if_no_fixup("reserved instruction", regs, error_code);
697 #ifdef CONFIG_SH_FPU_EMU
698 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
701 * bfs: 8fxx: PC+=d*2+4;
702 * bts: 8dxx: PC+=d*2+4;
703 * bra: axxx: PC+=D*2+4;
704 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
705 * braf:0x23: PC+=Rn*2+4;
706 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
708 * jsr: 4x0b: PC=Rn after PR=PC+4;
711 if ((inst & 0xfd00) == 0x8d00) {
712 regs->pc += SH_PC_8BIT_OFFSET(inst);
716 if ((inst & 0xe000) == 0xa000) {
717 regs->pc += SH_PC_12BIT_OFFSET(inst);
721 if ((inst & 0xf0df) == 0x0003) {
722 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
726 if ((inst & 0xf0df) == 0x400b) {
727 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
731 if ((inst & 0xffff) == 0x000b) {
740 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
741 unsigned long r6, unsigned long r7,
742 struct pt_regs __regs)
744 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
745 unsigned long error_code;
746 struct task_struct *tsk = current;
748 if (kprobe_handle_illslot(regs->pc) == 0)
751 #ifdef CONFIG_SH_FPU_EMU
752 unsigned short inst = 0;
754 get_user(inst, (unsigned short *)regs->pc + 1);
755 if (!do_fpu_inst(inst, regs)) {
756 get_user(inst, (unsigned short *)regs->pc);
757 if (!emulate_branch(inst, regs))
759 /* fault in branch.*/
761 /* not a FPU inst. */
764 lookup_exception_vector(error_code);
767 CHK_REMOTE_DEBUG(regs);
768 force_sig(SIGILL, tsk);
769 die_if_no_fixup("illegal slot instruction", regs, error_code);
772 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
773 unsigned long r6, unsigned long r7,
774 struct pt_regs __regs)
776 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
779 lookup_exception_vector(ex);
780 die_if_kernel("exception", regs, ex);
783 #if defined(CONFIG_SH_STANDARD_BIOS)
784 void *gdb_vbr_vector;
786 static inline void __init gdb_vbr_init(void)
788 register unsigned long vbr;
791 * Read the old value of the VBR register to initialise
792 * the vector through which debug and BIOS traps are
793 * delegated by the Linux trap handler.
795 asm volatile("stc vbr, %0" : "=r" (vbr));
797 gdb_vbr_vector = (void *)(vbr + 0x100);
798 printk("Setting GDB trap vector to 0x%08lx\n",
799 (unsigned long)gdb_vbr_vector);
803 void __cpuinit per_cpu_trap_init(void)
805 extern void *vbr_base;
807 #ifdef CONFIG_SH_STANDARD_BIOS
808 if (raw_smp_processor_id() == 0)
812 /* NOTE: The VBR value should be at P1
813 (or P2, virtural "fixed" address space).
814 It's definitely should not in physical address. */
816 asm volatile("ldc %0, vbr"
822 void *set_exception_table_vec(unsigned int vec, void *handler)
824 extern void *exception_handling_table[];
827 old_handler = exception_handling_table[vec];
828 exception_handling_table[vec] = handler;
832 void __init trap_init(void)
834 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
835 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
837 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
838 defined(CONFIG_SH_FPU_EMU)
840 * For SH-4 lacking an FPU, treat floating point instructions as
841 * reserved. They'll be handled in the math-emu case, or faulted on
844 set_exception_table_evt(0x800, do_reserved_inst);
845 set_exception_table_evt(0x820, do_illegal_slot_inst);
846 #elif defined(CONFIG_SH_FPU)
847 #ifdef CONFIG_CPU_SUBTYPE_SHX3
848 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
849 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
851 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
852 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
856 #ifdef CONFIG_CPU_SH2
857 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
859 #ifdef CONFIG_CPU_SH2A
860 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
861 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
863 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
867 /* Setup VBR for boot cpu */
871 void show_trace(struct task_struct *tsk, unsigned long *sp,
872 struct pt_regs *regs)
876 if (regs && user_mode(regs))
879 printk("\nCall trace: ");
880 #ifdef CONFIG_KALLSYMS
884 while (!kstack_end(sp)) {
886 if (kernel_text_address(addr))
895 debug_show_held_locks(tsk);
898 void show_stack(struct task_struct *tsk, unsigned long *sp)
905 sp = (unsigned long *)current_stack_pointer;
907 sp = (unsigned long *)tsk->thread.sp;
909 stack = (unsigned long)sp;
910 dump_mem("Stack: ", stack, THREAD_SIZE +
911 (unsigned long)task_stack_page(tsk));
912 show_trace(tsk, sp, NULL);
915 void dump_stack(void)
917 show_stack(NULL, NULL);
919 EXPORT_SYMBOL(dump_stack);