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 <linux/debug_locks.h>
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
27 #define CHK_REMOTE_DEBUG(regs) \
29 if (kgdb_debug_hook && !user_mode(regs))\
30 (*kgdb_debug_hook)(regs); \
33 #define CHK_REMOTE_DEBUG(regs)
37 # define TRAP_RESERVED_INST 4
38 # define TRAP_ILLEGAL_SLOT_INST 6
39 # define TRAP_ADDRESS_ERROR 9
40 # ifdef CONFIG_CPU_SH2A
41 # define TRAP_DIVZERO_ERROR 17
42 # define TRAP_DIVOVF_ERROR 18
45 #define TRAP_RESERVED_INST 12
46 #define TRAP_ILLEGAL_SLOT_INST 13
49 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
54 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
56 for (p = bottom & ~31; p < top; ) {
57 printk("%04lx: ", p & 0xffff);
59 for (i = 0; i < 8; i++, p += 4) {
62 if (p < bottom || p >= top)
65 if (__get_user(val, (unsigned int __user *)p)) {
76 DEFINE_SPINLOCK(die_lock);
78 void die(const char * str, struct pt_regs * regs, long err)
80 static int die_counter;
83 spin_lock_irq(&die_lock);
86 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
88 CHK_REMOTE_DEBUG(regs);
92 printk("Process: %s (pid: %d, stack limit = %p)\n",
93 current->comm, current->pid, task_stack_page(current) + 1);
95 if (!user_mode(regs) || in_interrupt())
96 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
97 (unsigned long)task_stack_page(current));
100 spin_unlock_irq(&die_lock);
104 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
107 if (!user_mode(regs))
112 * try and fix up kernelspace address errors
113 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
114 * - kernel/userspace interfaces cause a jump to an appropriate handler
115 * - other kernel errors are bad
116 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
118 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
120 if (!user_mode(regs)) {
121 const struct exception_table_entry *fixup;
122 fixup = search_exception_tables(regs->pc);
124 regs->pc = fixup->fixup;
133 * handle an instruction that does an unaligned memory access by emulating the
135 * - note that PC _may not_ point to the faulting instruction
136 * (if that instruction is in a branch delay slot)
137 * - return 0 if emulation okay, -EFAULT on existential error
139 static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
141 int ret, index, count;
142 unsigned long *rm, *rn;
143 unsigned char *src, *dst;
145 index = (instruction>>8)&15; /* 0x0F00 */
146 rn = ®s->regs[index];
148 index = (instruction>>4)&15; /* 0x00F0 */
149 rm = ®s->regs[index];
151 count = 1<<(instruction&3);
154 switch (instruction>>12) {
155 case 0: /* mov.[bwl] to/from memory via r0+rn */
156 if (instruction & 8) {
158 src = (unsigned char*) *rm;
159 src += regs->regs[0];
160 dst = (unsigned char*) rn;
161 *(unsigned long*)dst = 0;
163 #ifdef __LITTLE_ENDIAN__
164 if (copy_from_user(dst, src, count))
167 if ((count == 2) && dst[1] & 0x80) {
174 if (__copy_user(dst, src, count))
177 if ((count == 2) && dst[2] & 0x80) {
184 src = (unsigned char*) rm;
185 #if !defined(__LITTLE_ENDIAN__)
188 dst = (unsigned char*) *rn;
189 dst += regs->regs[0];
191 if (copy_to_user(dst, src, count))
197 case 1: /* mov.l Rm,@(disp,Rn) */
198 src = (unsigned char*) rm;
199 dst = (unsigned char*) *rn;
200 dst += (instruction&0x000F)<<2;
202 if (copy_to_user(dst,src,4))
207 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
210 src = (unsigned char*) rm;
211 dst = (unsigned char*) *rn;
212 #if !defined(__LITTLE_ENDIAN__)
215 if (copy_to_user(dst, src, count))
220 case 5: /* mov.l @(disp,Rm),Rn */
221 src = (unsigned char*) *rm;
222 src += (instruction&0x000F)<<2;
223 dst = (unsigned char*) rn;
224 *(unsigned long*)dst = 0;
226 if (copy_from_user(dst,src,4))
231 case 6: /* mov.[bwl] from memory, possibly with post-increment */
232 src = (unsigned char*) *rm;
235 dst = (unsigned char*) rn;
236 *(unsigned long*)dst = 0;
238 #ifdef __LITTLE_ENDIAN__
239 if (copy_from_user(dst, src, count))
242 if ((count == 2) && dst[1] & 0x80) {
249 if (copy_from_user(dst, src, count))
252 if ((count == 2) && dst[2] & 0x80) {
261 switch ((instruction&0xFF00)>>8) {
262 case 0x81: /* mov.w R0,@(disp,Rn) */
263 src = (unsigned char*) ®s->regs[0];
264 #if !defined(__LITTLE_ENDIAN__)
267 dst = (unsigned char*) *rm; /* called Rn in the spec */
268 dst += (instruction&0x000F)<<1;
270 if (copy_to_user(dst, src, 2))
275 case 0x85: /* mov.w @(disp,Rm),R0 */
276 src = (unsigned char*) *rm;
277 src += (instruction&0x000F)<<1;
278 dst = (unsigned char*) ®s->regs[0];
279 *(unsigned long*)dst = 0;
281 #if !defined(__LITTLE_ENDIAN__)
285 if (copy_from_user(dst, src, 2))
288 #ifdef __LITTLE_ENDIAN__
307 /* Argh. Address not only misaligned but also non-existent.
308 * Raise an EFAULT and see if it's trapped
310 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
314 * emulate the instruction in the delay slot
315 * - fetches the instruction from PC+2
317 static inline int handle_unaligned_delayslot(struct pt_regs *regs)
321 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
322 /* the instruction-fetch faulted */
327 die("delay-slot-insn faulting in handle_unaligned_delayslot",
331 return handle_unaligned_ins(instruction,regs);
335 * handle an instruction that does an unaligned memory access
336 * - have to be careful of branch delay-slot instructions that fault
338 * - if the branch would be taken PC points to the branch
339 * - if the branch would not be taken, PC points to delay-slot
341 * - PC always points to delayed branch
342 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
345 /* Macros to determine offset from current PC for branch instructions */
346 /* Explicit type coercion is used to force sign extension where needed */
347 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
348 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
351 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
354 #ifndef CONFIG_CPU_SH2A
355 static int handle_unaligned_notify_count = 10;
357 static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
362 index = (instruction>>8)&15; /* 0x0F00 */
363 rm = regs->regs[index];
365 /* shout about the first ten userspace fixups */
366 if (user_mode(regs) && handle_unaligned_notify_count>0) {
367 handle_unaligned_notify_count--;
369 printk(KERN_NOTICE "Fixing up unaligned userspace access "
370 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
371 current->comm,current->pid,(u16*)regs->pc,instruction);
375 switch (instruction&0xF000) {
377 if (instruction==0x000B) {
379 ret = handle_unaligned_delayslot(regs);
383 else if ((instruction&0x00FF)==0x0023) {
385 ret = handle_unaligned_delayslot(regs);
389 else if ((instruction&0x00FF)==0x0003) {
391 ret = handle_unaligned_delayslot(regs);
393 regs->pr = regs->pc + 4;
398 /* mov.[bwl] to/from memory via r0+rn */
403 case 0x1000: /* mov.l Rm,@(disp,Rn) */
406 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
410 if ((instruction&0x00FF)==0x002B) {
412 ret = handle_unaligned_delayslot(regs);
416 else if ((instruction&0x00FF)==0x000B) {
418 ret = handle_unaligned_delayslot(regs);
420 regs->pr = regs->pc + 4;
425 /* mov.[bwl] to/from memory via r0+rn */
430 case 0x5000: /* mov.l @(disp,Rm),Rn */
433 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
436 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
437 switch (instruction&0x0F00) {
438 case 0x0100: /* mov.w R0,@(disp,Rm) */
440 case 0x0500: /* mov.w @(disp,Rm),R0 */
442 case 0x0B00: /* bf lab - no delayslot*/
444 case 0x0F00: /* bf/s lab */
445 ret = handle_unaligned_delayslot(regs);
447 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
448 if ((regs->sr & 0x00000001) != 0)
449 regs->pc += 4; /* next after slot */
452 regs->pc += SH_PC_8BIT_OFFSET(instruction);
455 case 0x0900: /* bt lab - no delayslot */
457 case 0x0D00: /* bt/s lab */
458 ret = handle_unaligned_delayslot(regs);
460 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
461 if ((regs->sr & 0x00000001) == 0)
462 regs->pc += 4; /* next after slot */
465 regs->pc += SH_PC_8BIT_OFFSET(instruction);
471 case 0xA000: /* bra label */
472 ret = handle_unaligned_delayslot(regs);
474 regs->pc += SH_PC_12BIT_OFFSET(instruction);
477 case 0xB000: /* bsr label */
478 ret = handle_unaligned_delayslot(regs);
480 regs->pr = regs->pc + 4;
481 regs->pc += SH_PC_12BIT_OFFSET(instruction);
487 /* handle non-delay-slot instruction */
489 ret = handle_unaligned_ins(instruction,regs);
494 #endif /* CONFIG_CPU_SH2A */
496 #ifdef CONFIG_CPU_HAS_SR_RB
497 #define lookup_exception_vector(x) \
498 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
500 #define lookup_exception_vector(x) \
501 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
505 * Handle various address error exceptions:
506 * - instruction address error:
508 * PC >= 0x80000000 in user mode
509 * - data address error (read and write)
510 * misaligned data access
511 * access to >= 0x80000000 is user mode
512 * Unfortuntaly we can't distinguish between instruction address error
513 * and data address errors caused by read acceses.
515 asmlinkage void do_address_error(struct pt_regs *regs,
516 unsigned long writeaccess,
517 unsigned long address)
519 unsigned long error_code = 0;
522 #ifndef CONFIG_CPU_SH2A
527 /* Intentional ifdef */
528 #ifdef CONFIG_CPU_HAS_SR_RB
529 lookup_exception_vector(error_code);
534 if (user_mode(regs)) {
535 int si_code = BUS_ADRERR;
539 /* bad PC is not something we can fix */
541 si_code = BUS_ADRALN;
545 #ifndef CONFIG_CPU_SH2A
547 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
548 /* Argh. Fault on the instruction itself.
549 This should never happen non-SMP
555 tmp = handle_unaligned_access(instruction, regs);
563 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
564 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
567 info.si_signo = SIGBUS;
569 info.si_code = si_code;
570 info.si_addr = (void *) address;
571 force_sig_info(SIGBUS, &info, current);
574 die("unaligned program counter", regs, error_code);
576 #ifndef CONFIG_CPU_SH2A
578 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
579 /* Argh. Fault on the instruction itself.
580 This should never happen non-SMP
583 die("insn faulting in do_address_error", regs, 0);
586 handle_unaligned_access(instruction, regs);
589 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
590 "access\n", current->comm);
592 force_sig(SIGSEGV, current);
599 * SH-DSP support gerg@snapgear.com.
601 int is_dsp_inst(struct pt_regs *regs)
606 * Safe guard if DSP mode is already enabled or we're lacking
607 * the DSP altogether.
609 if (!(cpu_data->flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
612 get_user(inst, ((unsigned short *) regs->pc));
616 /* Check for any type of DSP or support instruction */
617 if ((inst == 0xf000) || (inst == 0x4000))
623 #define is_dsp_inst(regs) (0)
624 #endif /* CONFIG_SH_DSP */
626 #ifdef CONFIG_CPU_SH2A
627 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
628 unsigned long r6, unsigned long r7,
629 struct pt_regs __regs)
631 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
635 case TRAP_DIVZERO_ERROR:
636 info.si_code = FPE_INTDIV;
638 case TRAP_DIVOVF_ERROR:
639 info.si_code = FPE_INTOVF;
643 force_sig_info(SIGFPE, &info, current);
647 /* arch/sh/kernel/cpu/sh4/fpu.c */
648 extern int do_fpu_inst(unsigned short, struct pt_regs *);
649 extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
650 unsigned long r6, unsigned long r7, struct pt_regs __regs);
652 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
653 unsigned long r6, unsigned long r7,
654 struct pt_regs __regs)
656 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
657 unsigned long error_code;
658 struct task_struct *tsk = current;
660 #ifdef CONFIG_SH_FPU_EMU
661 unsigned short inst = 0;
664 get_user(inst, (unsigned short*)regs->pc);
666 err = do_fpu_inst(inst, regs);
671 /* not a FPU inst. */
675 /* Check if it's a DSP instruction */
676 if (is_dsp_inst(regs)) {
677 /* Enable DSP mode, and restart instruction. */
683 lookup_exception_vector(error_code);
686 CHK_REMOTE_DEBUG(regs);
687 force_sig(SIGILL, tsk);
688 die_if_no_fixup("reserved instruction", regs, error_code);
691 #ifdef CONFIG_SH_FPU_EMU
692 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
695 * bfs: 8fxx: PC+=d*2+4;
696 * bts: 8dxx: PC+=d*2+4;
697 * bra: axxx: PC+=D*2+4;
698 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
699 * braf:0x23: PC+=Rn*2+4;
700 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
702 * jsr: 4x0b: PC=Rn after PR=PC+4;
705 if ((inst & 0xfd00) == 0x8d00) {
706 regs->pc += SH_PC_8BIT_OFFSET(inst);
710 if ((inst & 0xe000) == 0xa000) {
711 regs->pc += SH_PC_12BIT_OFFSET(inst);
715 if ((inst & 0xf0df) == 0x0003) {
716 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
720 if ((inst & 0xf0df) == 0x400b) {
721 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
725 if ((inst & 0xffff) == 0x000b) {
734 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
735 unsigned long r6, unsigned long r7,
736 struct pt_regs __regs)
738 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
739 unsigned long error_code;
740 struct task_struct *tsk = current;
741 #ifdef CONFIG_SH_FPU_EMU
742 unsigned short inst = 0;
744 get_user(inst, (unsigned short *)regs->pc + 1);
745 if (!do_fpu_inst(inst, regs)) {
746 get_user(inst, (unsigned short *)regs->pc);
747 if (!emulate_branch(inst, regs))
749 /* fault in branch.*/
751 /* not a FPU inst. */
754 lookup_exception_vector(error_code);
757 CHK_REMOTE_DEBUG(regs);
758 force_sig(SIGILL, tsk);
759 die_if_no_fixup("illegal slot instruction", regs, error_code);
762 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
763 unsigned long r6, unsigned long r7,
764 struct pt_regs __regs)
766 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
769 lookup_exception_vector(ex);
770 die_if_kernel("exception", regs, ex);
773 #if defined(CONFIG_SH_STANDARD_BIOS)
774 void *gdb_vbr_vector;
776 static inline void __init gdb_vbr_init(void)
778 register unsigned long vbr;
781 * Read the old value of the VBR register to initialise
782 * the vector through which debug and BIOS traps are
783 * delegated by the Linux trap handler.
785 asm volatile("stc vbr, %0" : "=r" (vbr));
787 gdb_vbr_vector = (void *)(vbr + 0x100);
788 printk("Setting GDB trap vector to 0x%08lx\n",
789 (unsigned long)gdb_vbr_vector);
793 void __init per_cpu_trap_init(void)
795 extern void *vbr_base;
797 #ifdef CONFIG_SH_STANDARD_BIOS
801 /* NOTE: The VBR value should be at P1
802 (or P2, virtural "fixed" address space).
803 It's definitely should not in physical address. */
805 asm volatile("ldc %0, vbr"
811 void *set_exception_table_vec(unsigned int vec, void *handler)
813 extern void *exception_handling_table[];
816 old_handler = exception_handling_table[vec];
817 exception_handling_table[vec] = handler;
821 extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
822 unsigned long r6, unsigned long r7,
823 struct pt_regs __regs);
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 set_exception_table_evt(0x800, do_fpu_state_restore);
841 set_exception_table_evt(0x820, do_fpu_state_restore);
844 #ifdef CONFIG_CPU_SH2
845 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_handler);
847 #ifdef CONFIG_CPU_SH2A
848 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
849 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
852 /* Setup VBR for boot cpu */
856 void show_trace(struct task_struct *tsk, unsigned long *sp,
857 struct pt_regs *regs)
861 if (regs && user_mode(regs))
864 printk("\nCall trace: ");
865 #ifdef CONFIG_KALLSYMS
869 while (!kstack_end(sp)) {
871 if (kernel_text_address(addr))
880 debug_show_held_locks(tsk);
883 void show_stack(struct task_struct *tsk, unsigned long *sp)
890 sp = (unsigned long *)current_stack_pointer;
892 sp = (unsigned long *)tsk->thread.sp;
894 stack = (unsigned long)sp;
895 dump_mem("Stack: ", stack, THREAD_SIZE +
896 (unsigned long)task_stack_page(tsk));
897 show_trace(tsk, sp, NULL);
900 void dump_stack(void)
902 show_stack(NULL, NULL);
904 EXPORT_SYMBOL(dump_stack);