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 - 2006 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 <asm/system.h>
22 #include <asm/uaccess.h>
26 #define CHK_REMOTE_DEBUG(regs) \
28 if (kgdb_debug_hook && !user_mode(regs))\
29 (*kgdb_debug_hook)(regs); \
32 #define CHK_REMOTE_DEBUG(regs)
36 # define TRAP_RESERVED_INST 4
37 # define TRAP_ILLEGAL_SLOT_INST 6
38 # define TRAP_ADDRESS_ERROR 9
39 # ifdef CONFIG_CPU_SH2A
40 # define TRAP_DIVZERO_ERROR 17
41 # define TRAP_DIVOVF_ERROR 18
44 #define TRAP_RESERVED_INST 12
45 #define TRAP_ILLEGAL_SLOT_INST 13
48 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
53 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
55 for (p = bottom & ~31; p < top; ) {
56 printk("%04lx: ", p & 0xffff);
58 for (i = 0; i < 8; i++, p += 4) {
61 if (p < bottom || p >= top)
64 if (__get_user(val, (unsigned int __user *)p)) {
75 DEFINE_SPINLOCK(die_lock);
77 void die(const char * str, struct pt_regs * regs, long err)
79 static int die_counter;
82 spin_lock_irq(&die_lock);
85 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
87 CHK_REMOTE_DEBUG(regs);
91 printk("Process: %s (pid: %d, stack limit = %p)\n",
92 current->comm, current->pid, task_stack_page(current) + 1);
94 if (!user_mode(regs) || in_interrupt())
95 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
96 (unsigned long)task_stack_page(current));
99 spin_unlock_irq(&die_lock);
103 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
106 if (!user_mode(regs))
111 * try and fix up kernelspace address errors
112 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
113 * - kernel/userspace interfaces cause a jump to an appropriate handler
114 * - other kernel errors are bad
115 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
117 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
119 if (!user_mode(regs)) {
120 const struct exception_table_entry *fixup;
121 fixup = search_exception_tables(regs->pc);
123 regs->pc = fixup->fixup;
132 * handle an instruction that does an unaligned memory access by emulating the
134 * - note that PC _may not_ point to the faulting instruction
135 * (if that instruction is in a branch delay slot)
136 * - return 0 if emulation okay, -EFAULT on existential error
138 static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
140 int ret, index, count;
141 unsigned long *rm, *rn;
142 unsigned char *src, *dst;
144 index = (instruction>>8)&15; /* 0x0F00 */
145 rn = ®s->regs[index];
147 index = (instruction>>4)&15; /* 0x00F0 */
148 rm = ®s->regs[index];
150 count = 1<<(instruction&3);
153 switch (instruction>>12) {
154 case 0: /* mov.[bwl] to/from memory via r0+rn */
155 if (instruction & 8) {
157 src = (unsigned char*) *rm;
158 src += regs->regs[0];
159 dst = (unsigned char*) rn;
160 *(unsigned long*)dst = 0;
162 #ifdef __LITTLE_ENDIAN__
163 if (copy_from_user(dst, src, count))
166 if ((count == 2) && dst[1] & 0x80) {
173 if (__copy_user(dst, src, count))
176 if ((count == 2) && dst[2] & 0x80) {
183 src = (unsigned char*) rm;
184 #if !defined(__LITTLE_ENDIAN__)
187 dst = (unsigned char*) *rn;
188 dst += regs->regs[0];
190 if (copy_to_user(dst, src, count))
196 case 1: /* mov.l Rm,@(disp,Rn) */
197 src = (unsigned char*) rm;
198 dst = (unsigned char*) *rn;
199 dst += (instruction&0x000F)<<2;
201 if (copy_to_user(dst,src,4))
206 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
209 src = (unsigned char*) rm;
210 dst = (unsigned char*) *rn;
211 #if !defined(__LITTLE_ENDIAN__)
214 if (copy_to_user(dst, src, count))
219 case 5: /* mov.l @(disp,Rm),Rn */
220 src = (unsigned char*) *rm;
221 src += (instruction&0x000F)<<2;
222 dst = (unsigned char*) rn;
223 *(unsigned long*)dst = 0;
225 if (copy_from_user(dst,src,4))
230 case 6: /* mov.[bwl] from memory, possibly with post-increment */
231 src = (unsigned char*) *rm;
234 dst = (unsigned char*) rn;
235 *(unsigned long*)dst = 0;
237 #ifdef __LITTLE_ENDIAN__
238 if (copy_from_user(dst, src, count))
241 if ((count == 2) && dst[1] & 0x80) {
248 if (copy_from_user(dst, src, count))
251 if ((count == 2) && dst[2] & 0x80) {
260 switch ((instruction&0xFF00)>>8) {
261 case 0x81: /* mov.w R0,@(disp,Rn) */
262 src = (unsigned char*) ®s->regs[0];
263 #if !defined(__LITTLE_ENDIAN__)
266 dst = (unsigned char*) *rm; /* called Rn in the spec */
267 dst += (instruction&0x000F)<<1;
269 if (copy_to_user(dst, src, 2))
274 case 0x85: /* mov.w @(disp,Rm),R0 */
275 src = (unsigned char*) *rm;
276 src += (instruction&0x000F)<<1;
277 dst = (unsigned char*) ®s->regs[0];
278 *(unsigned long*)dst = 0;
280 #if !defined(__LITTLE_ENDIAN__)
284 if (copy_from_user(dst, src, 2))
287 #ifdef __LITTLE_ENDIAN__
306 /* Argh. Address not only misaligned but also non-existent.
307 * Raise an EFAULT and see if it's trapped
309 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
313 * emulate the instruction in the delay slot
314 * - fetches the instruction from PC+2
316 static inline int handle_unaligned_delayslot(struct pt_regs *regs)
320 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
321 /* the instruction-fetch faulted */
326 die("delay-slot-insn faulting in handle_unaligned_delayslot",
330 return handle_unaligned_ins(instruction,regs);
334 * handle an instruction that does an unaligned memory access
335 * - have to be careful of branch delay-slot instructions that fault
337 * - if the branch would be taken PC points to the branch
338 * - if the branch would not be taken, PC points to delay-slot
340 * - PC always points to delayed branch
341 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
344 /* Macros to determine offset from current PC for branch instructions */
345 /* Explicit type coercion is used to force sign extension where needed */
346 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
347 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
350 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
353 #ifndef CONFIG_CPU_SH2A
354 static int handle_unaligned_notify_count = 10;
356 static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
361 index = (instruction>>8)&15; /* 0x0F00 */
362 rm = regs->regs[index];
364 /* shout about the first ten userspace fixups */
365 if (user_mode(regs) && handle_unaligned_notify_count>0) {
366 handle_unaligned_notify_count--;
368 printk(KERN_NOTICE "Fixing up unaligned userspace access "
369 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
370 current->comm,current->pid,(u16*)regs->pc,instruction);
374 switch (instruction&0xF000) {
376 if (instruction==0x000B) {
378 ret = handle_unaligned_delayslot(regs);
382 else if ((instruction&0x00FF)==0x0023) {
384 ret = handle_unaligned_delayslot(regs);
388 else if ((instruction&0x00FF)==0x0003) {
390 ret = handle_unaligned_delayslot(regs);
392 regs->pr = regs->pc + 4;
397 /* mov.[bwl] to/from memory via r0+rn */
402 case 0x1000: /* mov.l Rm,@(disp,Rn) */
405 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
409 if ((instruction&0x00FF)==0x002B) {
411 ret = handle_unaligned_delayslot(regs);
415 else if ((instruction&0x00FF)==0x000B) {
417 ret = handle_unaligned_delayslot(regs);
419 regs->pr = regs->pc + 4;
424 /* mov.[bwl] to/from memory via r0+rn */
429 case 0x5000: /* mov.l @(disp,Rm),Rn */
432 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
435 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
436 switch (instruction&0x0F00) {
437 case 0x0100: /* mov.w R0,@(disp,Rm) */
439 case 0x0500: /* mov.w @(disp,Rm),R0 */
441 case 0x0B00: /* bf lab - no delayslot*/
443 case 0x0F00: /* bf/s lab */
444 ret = handle_unaligned_delayslot(regs);
446 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
447 if ((regs->sr & 0x00000001) != 0)
448 regs->pc += 4; /* next after slot */
451 regs->pc += SH_PC_8BIT_OFFSET(instruction);
454 case 0x0900: /* bt lab - no delayslot */
456 case 0x0D00: /* bt/s lab */
457 ret = handle_unaligned_delayslot(regs);
459 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
460 if ((regs->sr & 0x00000001) == 0)
461 regs->pc += 4; /* next after slot */
464 regs->pc += SH_PC_8BIT_OFFSET(instruction);
470 case 0xA000: /* bra label */
471 ret = handle_unaligned_delayslot(regs);
473 regs->pc += SH_PC_12BIT_OFFSET(instruction);
476 case 0xB000: /* bsr label */
477 ret = handle_unaligned_delayslot(regs);
479 regs->pr = regs->pc + 4;
480 regs->pc += SH_PC_12BIT_OFFSET(instruction);
486 /* handle non-delay-slot instruction */
488 ret = handle_unaligned_ins(instruction,regs);
493 #endif /* CONFIG_CPU_SH2A */
495 #ifdef CONFIG_CPU_HAS_SR_RB
496 #define lookup_exception_vector(x) \
497 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
499 #define lookup_exception_vector(x) \
500 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
504 * Handle various address error exceptions:
505 * - instruction address error:
507 * PC >= 0x80000000 in user mode
508 * - data address error (read and write)
509 * misaligned data access
510 * access to >= 0x80000000 is user mode
511 * Unfortuntaly we can't distinguish between instruction address error
512 * and data address errors caused by read acceses.
514 asmlinkage void do_address_error(struct pt_regs *regs,
515 unsigned long writeaccess,
516 unsigned long address)
518 unsigned long error_code = 0;
521 #ifndef CONFIG_CPU_SH2A
526 /* Intentional ifdef */
527 #ifdef CONFIG_CPU_HAS_SR_RB
528 lookup_exception_vector(error_code);
533 if (user_mode(regs)) {
534 int si_code = BUS_ADRERR;
538 /* bad PC is not something we can fix */
540 si_code = BUS_ADRALN;
544 #ifndef CONFIG_CPU_SH2A
546 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
547 /* Argh. Fault on the instruction itself.
548 This should never happen non-SMP
554 tmp = handle_unaligned_access(instruction, regs);
562 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
563 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
566 info.si_signo = SIGBUS;
568 info.si_code = si_code;
569 info.si_addr = (void *) address;
570 force_sig_info(SIGBUS, &info, current);
573 die("unaligned program counter", regs, error_code);
575 #ifndef CONFIG_CPU_SH2A
577 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
578 /* Argh. Fault on the instruction itself.
579 This should never happen non-SMP
582 die("insn faulting in do_address_error", regs, 0);
585 handle_unaligned_access(instruction, regs);
588 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
589 "access\n", current->comm);
591 force_sig(SIGSEGV, current);
598 * SH-DSP support gerg@snapgear.com.
600 int is_dsp_inst(struct pt_regs *regs)
605 * Safe guard if DSP mode is already enabled or we're lacking
606 * the DSP altogether.
608 if (!(cpu_data->flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
611 get_user(inst, ((unsigned short *) regs->pc));
615 /* Check for any type of DSP or support instruction */
616 if ((inst == 0xf000) || (inst == 0x4000))
622 #define is_dsp_inst(regs) (0)
623 #endif /* CONFIG_SH_DSP */
625 #ifdef CONFIG_CPU_SH2A
626 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
627 unsigned long r6, unsigned long r7,
628 struct pt_regs __regs)
630 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
634 case TRAP_DIVZERO_ERROR:
635 info.si_code = FPE_INTDIV;
637 case TRAP_DIVOVF_ERROR:
638 info.si_code = FPE_INTOVF;
642 force_sig_info(SIGFPE, &info, current);
646 /* arch/sh/kernel/cpu/sh4/fpu.c */
647 extern int do_fpu_inst(unsigned short, struct pt_regs *);
648 extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
649 unsigned long r6, unsigned long r7, struct pt_regs __regs);
651 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
652 unsigned long r6, unsigned long r7,
653 struct pt_regs __regs)
655 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
656 unsigned long error_code;
657 struct task_struct *tsk = current;
659 #ifdef CONFIG_SH_FPU_EMU
660 unsigned short inst = 0;
663 get_user(inst, (unsigned short*)regs->pc);
665 err = do_fpu_inst(inst, regs);
670 /* not a FPU inst. */
674 /* Check if it's a DSP instruction */
675 if (is_dsp_inst(regs)) {
676 /* Enable DSP mode, and restart instruction. */
682 lookup_exception_vector(error_code);
685 CHK_REMOTE_DEBUG(regs);
686 force_sig(SIGILL, tsk);
687 die_if_no_fixup("reserved instruction", regs, error_code);
690 #ifdef CONFIG_SH_FPU_EMU
691 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
694 * bfs: 8fxx: PC+=d*2+4;
695 * bts: 8dxx: PC+=d*2+4;
696 * bra: axxx: PC+=D*2+4;
697 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
698 * braf:0x23: PC+=Rn*2+4;
699 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
701 * jsr: 4x0b: PC=Rn after PR=PC+4;
704 if ((inst & 0xfd00) == 0x8d00) {
705 regs->pc += SH_PC_8BIT_OFFSET(inst);
709 if ((inst & 0xe000) == 0xa000) {
710 regs->pc += SH_PC_12BIT_OFFSET(inst);
714 if ((inst & 0xf0df) == 0x0003) {
715 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
719 if ((inst & 0xf0df) == 0x400b) {
720 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
724 if ((inst & 0xffff) == 0x000b) {
733 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
734 unsigned long r6, unsigned long r7,
735 struct pt_regs __regs)
737 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
738 unsigned long error_code;
739 struct task_struct *tsk = current;
740 #ifdef CONFIG_SH_FPU_EMU
741 unsigned short inst = 0;
743 get_user(inst, (unsigned short *)regs->pc + 1);
744 if (!do_fpu_inst(inst, regs)) {
745 get_user(inst, (unsigned short *)regs->pc);
746 if (!emulate_branch(inst, regs))
748 /* fault in branch.*/
750 /* not a FPU inst. */
753 lookup_exception_vector(error_code);
756 CHK_REMOTE_DEBUG(regs);
757 force_sig(SIGILL, tsk);
758 die_if_no_fixup("illegal slot instruction", regs, error_code);
761 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
762 unsigned long r6, unsigned long r7,
763 struct pt_regs __regs)
765 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
768 lookup_exception_vector(ex);
769 die_if_kernel("exception", regs, ex);
772 #if defined(CONFIG_SH_STANDARD_BIOS)
773 void *gdb_vbr_vector;
775 static inline void __init gdb_vbr_init(void)
777 register unsigned long vbr;
780 * Read the old value of the VBR register to initialise
781 * the vector through which debug and BIOS traps are
782 * delegated by the Linux trap handler.
784 asm volatile("stc vbr, %0" : "=r" (vbr));
786 gdb_vbr_vector = (void *)(vbr + 0x100);
787 printk("Setting GDB trap vector to 0x%08lx\n",
788 (unsigned long)gdb_vbr_vector);
792 void __init per_cpu_trap_init(void)
794 extern void *vbr_base;
796 #ifdef CONFIG_SH_STANDARD_BIOS
800 /* NOTE: The VBR value should be at P1
801 (or P2, virtural "fixed" address space).
802 It's definitely should not in physical address. */
804 asm volatile("ldc %0, vbr"
810 void *set_exception_table_vec(unsigned int vec, void *handler)
812 extern void *exception_handling_table[];
815 old_handler = exception_handling_table[vec];
816 exception_handling_table[vec] = handler;
820 extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
821 unsigned long r6, unsigned long r7,
822 struct pt_regs __regs);
824 void __init trap_init(void)
826 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
827 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
829 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
830 defined(CONFIG_SH_FPU_EMU)
832 * For SH-4 lacking an FPU, treat floating point instructions as
833 * reserved. They'll be handled in the math-emu case, or faulted on
836 set_exception_table_evt(0x800, do_reserved_inst);
837 set_exception_table_evt(0x820, do_illegal_slot_inst);
838 #elif defined(CONFIG_SH_FPU)
839 set_exception_table_evt(0x800, do_fpu_state_restore);
840 set_exception_table_evt(0x820, do_fpu_state_restore);
843 #ifdef CONFIG_CPU_SH2
844 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_handler);
846 #ifdef CONFIG_CPU_SH2A
847 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
848 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
851 /* Setup VBR for boot cpu */
855 void show_trace(struct task_struct *tsk, unsigned long *sp,
856 struct pt_regs *regs)
860 if (regs && user_mode(regs))
863 printk("\nCall trace: ");
864 #ifdef CONFIG_KALLSYMS
868 while (!kstack_end(sp)) {
870 if (kernel_text_address(addr))
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);