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/bug.h>
22 #include <linux/debug_locks.h>
23 #include <linux/limits.h>
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
29 #define CHK_REMOTE_DEBUG(regs) \
31 if (kgdb_debug_hook && !user_mode(regs))\
32 (*kgdb_debug_hook)(regs); \
35 #define CHK_REMOTE_DEBUG(regs)
39 # define TRAP_RESERVED_INST 4
40 # define TRAP_ILLEGAL_SLOT_INST 6
41 # define TRAP_ADDRESS_ERROR 9
42 # ifdef CONFIG_CPU_SH2A
43 # define TRAP_DIVZERO_ERROR 17
44 # define TRAP_DIVOVF_ERROR 18
47 #define TRAP_RESERVED_INST 12
48 #define TRAP_ILLEGAL_SLOT_INST 13
51 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
56 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
58 for (p = bottom & ~31; p < top; ) {
59 printk("%04lx: ", p & 0xffff);
61 for (i = 0; i < 8; i++, p += 4) {
64 if (p < bottom || p >= top)
67 if (__get_user(val, (unsigned int __user *)p)) {
78 DEFINE_SPINLOCK(die_lock);
80 void die(const char * str, struct pt_regs * regs, long err)
82 static int die_counter;
85 spin_lock_irq(&die_lock);
88 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
90 CHK_REMOTE_DEBUG(regs);
94 printk("Process: %s (pid: %d, stack limit = %p)\n",
95 current->comm, current->pid, task_stack_page(current) + 1);
97 if (!user_mode(regs) || in_interrupt())
98 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
99 (unsigned long)task_stack_page(current));
102 spin_unlock_irq(&die_lock);
106 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
109 if (!user_mode(regs))
114 * try and fix up kernelspace address errors
115 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
116 * - kernel/userspace interfaces cause a jump to an appropriate handler
117 * - other kernel errors are bad
118 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
120 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
122 if (!user_mode(regs)) {
123 const struct exception_table_entry *fixup;
124 fixup = search_exception_tables(regs->pc);
126 regs->pc = fixup->fixup;
135 * handle an instruction that does an unaligned memory access by emulating the
137 * - note that PC _may not_ point to the faulting instruction
138 * (if that instruction is in a branch delay slot)
139 * - return 0 if emulation okay, -EFAULT on existential error
141 static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
143 int ret, index, count;
144 unsigned long *rm, *rn;
145 unsigned char *src, *dst;
147 index = (instruction>>8)&15; /* 0x0F00 */
148 rn = ®s->regs[index];
150 index = (instruction>>4)&15; /* 0x00F0 */
151 rm = ®s->regs[index];
153 count = 1<<(instruction&3);
156 switch (instruction>>12) {
157 case 0: /* mov.[bwl] to/from memory via r0+rn */
158 if (instruction & 8) {
160 src = (unsigned char*) *rm;
161 src += regs->regs[0];
162 dst = (unsigned char*) rn;
163 *(unsigned long*)dst = 0;
165 #ifdef __LITTLE_ENDIAN__
166 if (copy_from_user(dst, src, count))
169 if ((count == 2) && dst[1] & 0x80) {
176 if (__copy_user(dst, src, count))
179 if ((count == 2) && dst[2] & 0x80) {
186 src = (unsigned char*) rm;
187 #if !defined(__LITTLE_ENDIAN__)
190 dst = (unsigned char*) *rn;
191 dst += regs->regs[0];
193 if (copy_to_user(dst, src, count))
199 case 1: /* mov.l Rm,@(disp,Rn) */
200 src = (unsigned char*) rm;
201 dst = (unsigned char*) *rn;
202 dst += (instruction&0x000F)<<2;
204 if (copy_to_user(dst,src,4))
209 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
212 src = (unsigned char*) rm;
213 dst = (unsigned char*) *rn;
214 #if !defined(__LITTLE_ENDIAN__)
217 if (copy_to_user(dst, src, count))
222 case 5: /* mov.l @(disp,Rm),Rn */
223 src = (unsigned char*) *rm;
224 src += (instruction&0x000F)<<2;
225 dst = (unsigned char*) rn;
226 *(unsigned long*)dst = 0;
228 if (copy_from_user(dst,src,4))
233 case 6: /* mov.[bwl] from memory, possibly with post-increment */
234 src = (unsigned char*) *rm;
237 dst = (unsigned char*) rn;
238 *(unsigned long*)dst = 0;
240 #ifdef __LITTLE_ENDIAN__
241 if (copy_from_user(dst, src, count))
244 if ((count == 2) && dst[1] & 0x80) {
251 if (copy_from_user(dst, src, count))
254 if ((count == 2) && dst[2] & 0x80) {
263 switch ((instruction&0xFF00)>>8) {
264 case 0x81: /* mov.w R0,@(disp,Rn) */
265 src = (unsigned char*) ®s->regs[0];
266 #if !defined(__LITTLE_ENDIAN__)
269 dst = (unsigned char*) *rm; /* called Rn in the spec */
270 dst += (instruction&0x000F)<<1;
272 if (copy_to_user(dst, src, 2))
277 case 0x85: /* mov.w @(disp,Rm),R0 */
278 src = (unsigned char*) *rm;
279 src += (instruction&0x000F)<<1;
280 dst = (unsigned char*) ®s->regs[0];
281 *(unsigned long*)dst = 0;
283 #if !defined(__LITTLE_ENDIAN__)
287 if (copy_from_user(dst, src, 2))
290 #ifdef __LITTLE_ENDIAN__
309 /* Argh. Address not only misaligned but also non-existent.
310 * Raise an EFAULT and see if it's trapped
312 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
316 * emulate the instruction in the delay slot
317 * - fetches the instruction from PC+2
319 static inline int handle_unaligned_delayslot(struct pt_regs *regs)
323 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
324 /* the instruction-fetch faulted */
329 die("delay-slot-insn faulting in handle_unaligned_delayslot",
333 return handle_unaligned_ins(instruction,regs);
337 * handle an instruction that does an unaligned memory access
338 * - have to be careful of branch delay-slot instructions that fault
340 * - if the branch would be taken PC points to the branch
341 * - if the branch would not be taken, PC points to delay-slot
343 * - PC always points to delayed branch
344 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
347 /* Macros to determine offset from current PC for branch instructions */
348 /* Explicit type coercion is used to force sign extension where needed */
349 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
350 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
353 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
356 #ifndef CONFIG_CPU_SH2A
357 static int handle_unaligned_notify_count = 10;
359 static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
364 index = (instruction>>8)&15; /* 0x0F00 */
365 rm = regs->regs[index];
367 /* shout about the first ten userspace fixups */
368 if (user_mode(regs) && handle_unaligned_notify_count>0) {
369 handle_unaligned_notify_count--;
371 printk(KERN_NOTICE "Fixing up unaligned userspace access "
372 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
373 current->comm,current->pid,(u16*)regs->pc,instruction);
377 switch (instruction&0xF000) {
379 if (instruction==0x000B) {
381 ret = handle_unaligned_delayslot(regs);
385 else if ((instruction&0x00FF)==0x0023) {
387 ret = handle_unaligned_delayslot(regs);
391 else if ((instruction&0x00FF)==0x0003) {
393 ret = handle_unaligned_delayslot(regs);
395 regs->pr = regs->pc + 4;
400 /* mov.[bwl] to/from memory via r0+rn */
405 case 0x1000: /* mov.l Rm,@(disp,Rn) */
408 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
412 if ((instruction&0x00FF)==0x002B) {
414 ret = handle_unaligned_delayslot(regs);
418 else if ((instruction&0x00FF)==0x000B) {
420 ret = handle_unaligned_delayslot(regs);
422 regs->pr = regs->pc + 4;
427 /* mov.[bwl] to/from memory via r0+rn */
432 case 0x5000: /* mov.l @(disp,Rm),Rn */
435 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
438 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
439 switch (instruction&0x0F00) {
440 case 0x0100: /* mov.w R0,@(disp,Rm) */
442 case 0x0500: /* mov.w @(disp,Rm),R0 */
444 case 0x0B00: /* bf lab - no delayslot*/
446 case 0x0F00: /* bf/s lab */
447 ret = handle_unaligned_delayslot(regs);
449 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
450 if ((regs->sr & 0x00000001) != 0)
451 regs->pc += 4; /* next after slot */
454 regs->pc += SH_PC_8BIT_OFFSET(instruction);
457 case 0x0900: /* bt lab - no delayslot */
459 case 0x0D00: /* bt/s lab */
460 ret = handle_unaligned_delayslot(regs);
462 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
463 if ((regs->sr & 0x00000001) == 0)
464 regs->pc += 4; /* next after slot */
467 regs->pc += SH_PC_8BIT_OFFSET(instruction);
473 case 0xA000: /* bra label */
474 ret = handle_unaligned_delayslot(regs);
476 regs->pc += SH_PC_12BIT_OFFSET(instruction);
479 case 0xB000: /* bsr label */
480 ret = handle_unaligned_delayslot(regs);
482 regs->pr = regs->pc + 4;
483 regs->pc += SH_PC_12BIT_OFFSET(instruction);
489 /* handle non-delay-slot instruction */
491 ret = handle_unaligned_ins(instruction,regs);
496 #endif /* CONFIG_CPU_SH2A */
498 #ifdef CONFIG_CPU_HAS_SR_RB
499 #define lookup_exception_vector(x) \
500 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
502 #define lookup_exception_vector(x) \
503 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
507 * Handle various address error exceptions:
508 * - instruction address error:
510 * PC >= 0x80000000 in user mode
511 * - data address error (read and write)
512 * misaligned data access
513 * access to >= 0x80000000 is user mode
514 * Unfortuntaly we can't distinguish between instruction address error
515 * and data address errors caused by read acceses.
517 asmlinkage void do_address_error(struct pt_regs *regs,
518 unsigned long writeaccess,
519 unsigned long address)
521 unsigned long error_code = 0;
524 #ifndef CONFIG_CPU_SH2A
529 /* Intentional ifdef */
530 #ifdef CONFIG_CPU_HAS_SR_RB
531 lookup_exception_vector(error_code);
536 if (user_mode(regs)) {
537 int si_code = BUS_ADRERR;
541 /* bad PC is not something we can fix */
543 si_code = BUS_ADRALN;
547 #ifndef CONFIG_CPU_SH2A
549 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
550 /* Argh. Fault on the instruction itself.
551 This should never happen non-SMP
557 tmp = handle_unaligned_access(instruction, regs);
565 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
566 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
569 info.si_signo = SIGBUS;
571 info.si_code = si_code;
572 info.si_addr = (void *) address;
573 force_sig_info(SIGBUS, &info, current);
576 die("unaligned program counter", regs, error_code);
578 #ifndef CONFIG_CPU_SH2A
580 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
581 /* Argh. Fault on the instruction itself.
582 This should never happen non-SMP
585 die("insn faulting in do_address_error", regs, 0);
588 handle_unaligned_access(instruction, regs);
591 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
592 "access\n", current->comm);
594 force_sig(SIGSEGV, current);
601 * SH-DSP support gerg@snapgear.com.
603 int is_dsp_inst(struct pt_regs *regs)
608 * Safe guard if DSP mode is already enabled or we're lacking
609 * the DSP altogether.
611 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
614 get_user(inst, ((unsigned short *) regs->pc));
618 /* Check for any type of DSP or support instruction */
619 if ((inst == 0xf000) || (inst == 0x4000))
625 #define is_dsp_inst(regs) (0)
626 #endif /* CONFIG_SH_DSP */
628 #ifdef CONFIG_CPU_SH2A
629 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
630 unsigned long r6, unsigned long r7,
631 struct pt_regs __regs)
633 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
637 case TRAP_DIVZERO_ERROR:
638 info.si_code = FPE_INTDIV;
640 case TRAP_DIVOVF_ERROR:
641 info.si_code = FPE_INTOVF;
645 force_sig_info(SIGFPE, &info, current);
649 /* arch/sh/kernel/cpu/sh4/fpu.c */
650 extern int do_fpu_inst(unsigned short, struct pt_regs *);
651 extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
652 unsigned long r6, unsigned long r7, struct pt_regs __regs);
654 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
655 unsigned long r6, unsigned long r7,
656 struct pt_regs __regs)
658 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
659 unsigned long error_code;
660 struct task_struct *tsk = current;
662 #ifdef CONFIG_SH_FPU_EMU
663 unsigned short inst = 0;
666 get_user(inst, (unsigned short*)regs->pc);
668 err = do_fpu_inst(inst, regs);
673 /* not a FPU inst. */
677 /* Check if it's a DSP instruction */
678 if (is_dsp_inst(regs)) {
679 /* Enable DSP mode, and restart instruction. */
685 lookup_exception_vector(error_code);
688 CHK_REMOTE_DEBUG(regs);
689 force_sig(SIGILL, tsk);
690 die_if_no_fixup("reserved instruction", regs, error_code);
693 #ifdef CONFIG_SH_FPU_EMU
694 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
697 * bfs: 8fxx: PC+=d*2+4;
698 * bts: 8dxx: PC+=d*2+4;
699 * bra: axxx: PC+=D*2+4;
700 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
701 * braf:0x23: PC+=Rn*2+4;
702 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
704 * jsr: 4x0b: PC=Rn after PR=PC+4;
707 if ((inst & 0xfd00) == 0x8d00) {
708 regs->pc += SH_PC_8BIT_OFFSET(inst);
712 if ((inst & 0xe000) == 0xa000) {
713 regs->pc += SH_PC_12BIT_OFFSET(inst);
717 if ((inst & 0xf0df) == 0x0003) {
718 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
722 if ((inst & 0xf0df) == 0x400b) {
723 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
727 if ((inst & 0xffff) == 0x000b) {
736 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
737 unsigned long r6, unsigned long r7,
738 struct pt_regs __regs)
740 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
741 unsigned long error_code;
742 struct task_struct *tsk = current;
743 #ifdef CONFIG_SH_FPU_EMU
744 unsigned short inst = 0;
746 get_user(inst, (unsigned short *)regs->pc + 1);
747 if (!do_fpu_inst(inst, regs)) {
748 get_user(inst, (unsigned short *)regs->pc);
749 if (!emulate_branch(inst, regs))
751 /* fault in branch.*/
753 /* not a FPU inst. */
756 lookup_exception_vector(error_code);
759 CHK_REMOTE_DEBUG(regs);
760 force_sig(SIGILL, tsk);
761 die_if_no_fixup("illegal slot instruction", regs, error_code);
764 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
765 unsigned long r6, unsigned long r7,
766 struct pt_regs __regs)
768 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
771 lookup_exception_vector(ex);
772 die_if_kernel("exception", regs, ex);
775 #if defined(CONFIG_SH_STANDARD_BIOS)
776 void *gdb_vbr_vector;
778 static inline void __init gdb_vbr_init(void)
780 register unsigned long vbr;
783 * Read the old value of the VBR register to initialise
784 * the vector through which debug and BIOS traps are
785 * delegated by the Linux trap handler.
787 asm volatile("stc vbr, %0" : "=r" (vbr));
789 gdb_vbr_vector = (void *)(vbr + 0x100);
790 printk("Setting GDB trap vector to 0x%08lx\n",
791 (unsigned long)gdb_vbr_vector);
795 void __init per_cpu_trap_init(void)
797 extern void *vbr_base;
799 #ifdef CONFIG_SH_STANDARD_BIOS
803 /* NOTE: The VBR value should be at P1
804 (or P2, virtural "fixed" address space).
805 It's definitely should not in physical address. */
807 asm volatile("ldc %0, vbr"
813 void *set_exception_table_vec(unsigned int vec, void *handler)
815 extern void *exception_handling_table[];
818 old_handler = exception_handling_table[vec];
819 exception_handling_table[vec] = handler;
823 extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
824 unsigned long r6, unsigned long r7,
825 struct pt_regs __regs);
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 set_exception_table_evt(0x800, do_fpu_state_restore);
843 set_exception_table_evt(0x820, do_fpu_state_restore);
846 #ifdef CONFIG_CPU_SH2
847 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_handler);
849 #ifdef CONFIG_CPU_SH2A
850 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
851 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
854 /* Setup VBR for boot cpu */
859 void handle_BUG(struct pt_regs *regs)
861 enum bug_trap_type tt;
862 tt = report_bug(regs->pc);
863 if (tt == BUG_TRAP_TYPE_WARN) {
868 die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
871 int is_valid_bugaddr(unsigned long addr)
873 return addr >= PAGE_OFFSET;
877 void show_trace(struct task_struct *tsk, unsigned long *sp,
878 struct pt_regs *regs)
882 if (regs && user_mode(regs))
885 printk("\nCall trace: ");
886 #ifdef CONFIG_KALLSYMS
890 while (!kstack_end(sp)) {
892 if (kernel_text_address(addr))
901 debug_show_held_locks(tsk);
904 void show_stack(struct task_struct *tsk, unsigned long *sp)
911 sp = (unsigned long *)current_stack_pointer;
913 sp = (unsigned long *)tsk->thread.sp;
915 stack = (unsigned long)sp;
916 dump_mem("Stack: ", stack, THREAD_SIZE +
917 (unsigned long)task_stack_page(tsk));
918 show_trace(tsk, sp, NULL);
921 void dump_stack(void)
923 show_stack(NULL, NULL);
925 EXPORT_SYMBOL(dump_stack);