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;
195 unsigned char __user *srcu, *dstu;
197 index = (instruction>>8)&15; /* 0x0F00 */
198 rn = ®s->regs[index];
200 index = (instruction>>4)&15; /* 0x00F0 */
201 rm = ®s->regs[index];
203 count = 1<<(instruction&3);
206 switch (instruction>>12) {
207 case 0: /* mov.[bwl] to/from memory via r0+rn */
208 if (instruction & 8) {
210 srcu = (unsigned char __user *)*rm;
211 srcu += regs->regs[0];
212 dst = (unsigned char *)rn;
213 *(unsigned long *)dst = 0;
215 #if !defined(__LITTLE_ENDIAN__)
218 if (ma->from(dst, srcu, count))
221 sign_extend(count, dst);
224 src = (unsigned char *)rm;
225 #if !defined(__LITTLE_ENDIAN__)
228 dstu = (unsigned char __user *)*rn;
229 dstu += regs->regs[0];
231 if (ma->to(dstu, src, count))
237 case 1: /* mov.l Rm,@(disp,Rn) */
238 src = (unsigned char*) rm;
239 dstu = (unsigned char __user *)*rn;
240 dstu += (instruction&0x000F)<<2;
242 if (ma->to(dstu, src, 4))
247 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
250 src = (unsigned char*) rm;
251 dstu = (unsigned char __user *)*rn;
252 #if !defined(__LITTLE_ENDIAN__)
255 if (ma->to(dstu, src, count))
260 case 5: /* mov.l @(disp,Rm),Rn */
261 srcu = (unsigned char __user *)*rm;
262 srcu += (instruction & 0x000F) << 2;
263 dst = (unsigned char *)rn;
264 *(unsigned long *)dst = 0;
266 if (ma->from(dst, srcu, 4))
271 case 6: /* mov.[bwl] from memory, possibly with post-increment */
272 srcu = (unsigned char __user *)*rm;
275 dst = (unsigned char*) rn;
276 *(unsigned long*)dst = 0;
278 #if !defined(__LITTLE_ENDIAN__)
281 if (ma->from(dst, srcu, count))
283 sign_extend(count, dst);
288 switch ((instruction&0xFF00)>>8) {
289 case 0x81: /* mov.w R0,@(disp,Rn) */
290 src = (unsigned char *) ®s->regs[0];
291 #if !defined(__LITTLE_ENDIAN__)
294 dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
295 dstu += (instruction & 0x000F) << 1;
297 if (ma->to(dstu, src, 2))
302 case 0x85: /* mov.w @(disp,Rm),R0 */
303 srcu = (unsigned char __user *)*rm;
304 srcu += (instruction & 0x000F) << 1;
305 dst = (unsigned char *) ®s->regs[0];
306 *(unsigned long *)dst = 0;
308 #if !defined(__LITTLE_ENDIAN__)
311 if (ma->from(dst, srcu, 2))
322 /* Argh. Address not only misaligned but also non-existent.
323 * Raise an EFAULT and see if it's trapped
325 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
329 * emulate the instruction in the delay slot
330 * - fetches the instruction from PC+2
332 static inline int handle_delayslot(struct pt_regs *regs,
333 opcode_t old_instruction,
334 struct mem_access *ma)
336 opcode_t instruction;
337 void __user *addr = (void __user *)(regs->pc +
338 instruction_size(old_instruction));
340 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
341 /* the instruction-fetch faulted */
346 die("delay-slot-insn faulting in handle_unaligned_delayslot",
350 return handle_unaligned_ins(instruction, regs, ma);
354 * handle an instruction that does an unaligned memory access
355 * - have to be careful of branch delay-slot instructions that fault
357 * - if the branch would be taken PC points to the branch
358 * - if the branch would not be taken, PC points to delay-slot
360 * - PC always points to delayed branch
361 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
364 /* Macros to determine offset from current PC for branch instructions */
365 /* Explicit type coercion is used to force sign extension where needed */
366 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
367 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
370 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
374 static int handle_unaligned_notify_count = 10;
376 int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs,
377 struct mem_access *ma)
382 index = (instruction>>8)&15; /* 0x0F00 */
383 rm = regs->regs[index];
385 /* shout about the first ten userspace fixups */
386 if (user_mode(regs) && handle_unaligned_notify_count>0) {
387 handle_unaligned_notify_count--;
389 printk(KERN_NOTICE "Fixing up unaligned userspace access "
390 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
391 current->comm, task_pid_nr(current),
392 (void *)regs->pc, instruction);
396 switch (instruction&0xF000) {
398 if (instruction==0x000B) {
400 ret = handle_delayslot(regs, instruction, ma);
404 else if ((instruction&0x00FF)==0x0023) {
406 ret = handle_delayslot(regs, instruction, ma);
410 else if ((instruction&0x00FF)==0x0003) {
412 ret = handle_delayslot(regs, instruction, ma);
414 regs->pr = regs->pc + 4;
419 /* mov.[bwl] to/from memory via r0+rn */
424 case 0x1000: /* mov.l Rm,@(disp,Rn) */
427 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
431 if ((instruction&0x00FF)==0x002B) {
433 ret = handle_delayslot(regs, instruction, ma);
437 else if ((instruction&0x00FF)==0x000B) {
439 ret = handle_delayslot(regs, instruction, ma);
441 regs->pr = regs->pc + 4;
446 /* mov.[bwl] to/from memory via r0+rn */
451 case 0x5000: /* mov.l @(disp,Rm),Rn */
454 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
457 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
458 switch (instruction&0x0F00) {
459 case 0x0100: /* mov.w R0,@(disp,Rm) */
461 case 0x0500: /* mov.w @(disp,Rm),R0 */
463 case 0x0B00: /* bf lab - no delayslot*/
465 case 0x0F00: /* bf/s lab */
466 ret = handle_delayslot(regs, instruction, ma);
468 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
469 if ((regs->sr & 0x00000001) != 0)
470 regs->pc += 4; /* next after slot */
473 regs->pc += SH_PC_8BIT_OFFSET(instruction);
476 case 0x0900: /* bt lab - no delayslot */
478 case 0x0D00: /* bt/s lab */
479 ret = handle_delayslot(regs, instruction, ma);
481 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
482 if ((regs->sr & 0x00000001) == 0)
483 regs->pc += 4; /* next after slot */
486 regs->pc += SH_PC_8BIT_OFFSET(instruction);
492 case 0xA000: /* bra label */
493 ret = handle_delayslot(regs, instruction, ma);
495 regs->pc += SH_PC_12BIT_OFFSET(instruction);
498 case 0xB000: /* bsr label */
499 ret = handle_delayslot(regs, instruction, ma);
501 regs->pr = regs->pc + 4;
502 regs->pc += SH_PC_12BIT_OFFSET(instruction);
508 /* handle non-delay-slot instruction */
510 ret = handle_unaligned_ins(instruction, regs, ma);
512 regs->pc += instruction_size(instruction);
516 #ifdef CONFIG_CPU_HAS_SR_RB
517 #define lookup_exception_vector(x) \
518 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
520 #define lookup_exception_vector(x) \
521 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
525 * Handle various address error exceptions:
526 * - instruction address error:
528 * PC >= 0x80000000 in user mode
529 * - data address error (read and write)
530 * misaligned data access
531 * access to >= 0x80000000 is user mode
532 * Unfortuntaly we can't distinguish between instruction address error
533 * and data address errors caused by read accesses.
535 asmlinkage void do_address_error(struct pt_regs *regs,
536 unsigned long writeaccess,
537 unsigned long address)
539 unsigned long error_code = 0;
542 opcode_t instruction;
545 /* Intentional ifdef */
546 #ifdef CONFIG_CPU_HAS_SR_RB
547 lookup_exception_vector(error_code);
552 if (user_mode(regs)) {
553 int si_code = BUS_ADRERR;
557 /* bad PC is not something we can fix */
559 si_code = BUS_ADRALN;
564 if (copy_from_user(&instruction, (void __user *)(regs->pc),
565 sizeof(instruction))) {
566 /* Argh. Fault on the instruction itself.
567 This should never happen non-SMP
573 tmp = handle_unaligned_access(instruction, regs,
580 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
581 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
584 info.si_signo = SIGBUS;
586 info.si_code = si_code;
587 info.si_addr = (void __user *)address;
588 force_sig_info(SIGBUS, &info, current);
591 die("unaligned program counter", regs, error_code);
594 if (copy_from_user(&instruction, (void __user *)(regs->pc),
595 sizeof(instruction))) {
596 /* Argh. Fault on the instruction itself.
597 This should never happen non-SMP
600 die("insn faulting in do_address_error", regs, 0);
603 handle_unaligned_access(instruction, regs, &user_mem_access);
610 * SH-DSP support gerg@snapgear.com.
612 int is_dsp_inst(struct pt_regs *regs)
614 unsigned short inst = 0;
617 * Safe guard if DSP mode is already enabled or we're lacking
618 * the DSP altogether.
620 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
623 get_user(inst, ((unsigned short *) regs->pc));
627 /* Check for any type of DSP or support instruction */
628 if ((inst == 0xf000) || (inst == 0x4000))
634 #define is_dsp_inst(regs) (0)
635 #endif /* CONFIG_SH_DSP */
637 #ifdef CONFIG_CPU_SH2A
638 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
639 unsigned long r6, unsigned long r7,
640 struct pt_regs __regs)
645 case TRAP_DIVZERO_ERROR:
646 info.si_code = FPE_INTDIV;
648 case TRAP_DIVOVF_ERROR:
649 info.si_code = FPE_INTOVF;
653 force_sig_info(SIGFPE, &info, current);
657 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
658 unsigned long r6, unsigned long r7,
659 struct pt_regs __regs)
661 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
662 unsigned long error_code;
663 struct task_struct *tsk = current;
665 #ifdef CONFIG_SH_FPU_EMU
666 unsigned short inst = 0;
669 get_user(inst, (unsigned short*)regs->pc);
671 err = do_fpu_inst(inst, regs);
673 regs->pc += instruction_size(inst);
676 /* not a FPU inst. */
680 /* Check if it's a DSP instruction */
681 if (is_dsp_inst(regs)) {
682 /* Enable DSP mode, and restart instruction. */
688 lookup_exception_vector(error_code);
691 CHK_REMOTE_DEBUG(regs);
692 force_sig(SIGILL, tsk);
693 die_if_no_fixup("reserved instruction", regs, error_code);
696 #ifdef CONFIG_SH_FPU_EMU
697 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
700 * bfs: 8fxx: PC+=d*2+4;
701 * bts: 8dxx: PC+=d*2+4;
702 * bra: axxx: PC+=D*2+4;
703 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
704 * braf:0x23: PC+=Rn*2+4;
705 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
707 * jsr: 4x0b: PC=Rn after PR=PC+4;
710 if ((inst & 0xfd00) == 0x8d00) {
711 regs->pc += SH_PC_8BIT_OFFSET(inst);
715 if ((inst & 0xe000) == 0xa000) {
716 regs->pc += SH_PC_12BIT_OFFSET(inst);
720 if ((inst & 0xf0df) == 0x0003) {
721 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
725 if ((inst & 0xf0df) == 0x400b) {
726 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
730 if ((inst & 0xffff) == 0x000b) {
739 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
740 unsigned long r6, unsigned long r7,
741 struct pt_regs __regs)
743 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
744 unsigned long error_code;
745 struct task_struct *tsk = current;
746 #ifdef CONFIG_SH_FPU_EMU
747 unsigned short inst = 0;
749 get_user(inst, (unsigned short *)regs->pc + 1);
750 if (!do_fpu_inst(inst, regs)) {
751 get_user(inst, (unsigned short *)regs->pc);
752 if (!emulate_branch(inst, regs))
754 /* fault in branch.*/
756 /* not a FPU inst. */
759 lookup_exception_vector(error_code);
762 CHK_REMOTE_DEBUG(regs);
763 force_sig(SIGILL, tsk);
764 die_if_no_fixup("illegal slot instruction", regs, error_code);
767 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
768 unsigned long r6, unsigned long r7,
769 struct pt_regs __regs)
771 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
774 lookup_exception_vector(ex);
775 die_if_kernel("exception", regs, ex);
778 #if defined(CONFIG_SH_STANDARD_BIOS)
779 void *gdb_vbr_vector;
781 static inline void __init gdb_vbr_init(void)
783 register unsigned long vbr;
786 * Read the old value of the VBR register to initialise
787 * the vector through which debug and BIOS traps are
788 * delegated by the Linux trap handler.
790 asm volatile("stc vbr, %0" : "=r" (vbr));
792 gdb_vbr_vector = (void *)(vbr + 0x100);
793 printk("Setting GDB trap vector to 0x%08lx\n",
794 (unsigned long)gdb_vbr_vector);
798 void __cpuinit per_cpu_trap_init(void)
800 extern void *vbr_base;
802 #ifdef CONFIG_SH_STANDARD_BIOS
803 if (raw_smp_processor_id() == 0)
807 /* NOTE: The VBR value should be at P1
808 (or P2, virtural "fixed" address space).
809 It's definitely should not in physical address. */
811 asm volatile("ldc %0, vbr"
817 void *set_exception_table_vec(unsigned int vec, void *handler)
819 extern void *exception_handling_table[];
822 old_handler = exception_handling_table[vec];
823 exception_handling_table[vec] = handler;
827 void __init trap_init(void)
829 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
830 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
832 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
833 defined(CONFIG_SH_FPU_EMU)
835 * For SH-4 lacking an FPU, treat floating point instructions as
836 * reserved. They'll be handled in the math-emu case, or faulted on
839 set_exception_table_evt(0x800, do_reserved_inst);
840 set_exception_table_evt(0x820, do_illegal_slot_inst);
841 #elif defined(CONFIG_SH_FPU)
842 #ifdef CONFIG_CPU_SUBTYPE_SHX3
843 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
844 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
846 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
847 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
851 #ifdef CONFIG_CPU_SH2
852 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
854 #ifdef CONFIG_CPU_SH2A
855 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
856 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
858 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
862 /* Setup VBR for boot cpu */
866 void show_trace(struct task_struct *tsk, unsigned long *sp,
867 struct pt_regs *regs)
871 if (regs && user_mode(regs))
874 printk("\nCall trace: ");
875 #ifdef CONFIG_KALLSYMS
879 while (!kstack_end(sp)) {
881 if (kernel_text_address(addr))
890 debug_show_held_locks(tsk);
893 void show_stack(struct task_struct *tsk, unsigned long *sp)
900 sp = (unsigned long *)current_stack_pointer;
902 sp = (unsigned long *)tsk->thread.sp;
904 stack = (unsigned long)sp;
905 dump_mem("Stack: ", stack, THREAD_SIZE +
906 (unsigned long)task_stack_page(tsk));
907 show_trace(tsk, sp, NULL);
910 void dump_stack(void)
912 show_stack(NULL, NULL);
914 EXPORT_SYMBOL(dump_stack);