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 <linux/limits.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
28 #define CHK_REMOTE_DEBUG(regs) \
30 if (kgdb_debug_hook && !user_mode(regs))\
31 (*kgdb_debug_hook)(regs); \
34 #define CHK_REMOTE_DEBUG(regs)
38 # define TRAP_RESERVED_INST 4
39 # define TRAP_ILLEGAL_SLOT_INST 6
40 # define TRAP_ADDRESS_ERROR 9
41 # ifdef CONFIG_CPU_SH2A
42 # define TRAP_DIVZERO_ERROR 17
43 # define TRAP_DIVOVF_ERROR 18
46 #define TRAP_RESERVED_INST 12
47 #define TRAP_ILLEGAL_SLOT_INST 13
50 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
55 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
57 for (p = bottom & ~31; p < top; ) {
58 printk("%04lx: ", p & 0xffff);
60 for (i = 0; i < 8; i++, p += 4) {
63 if (p < bottom || p >= top)
66 if (__get_user(val, (unsigned int __user *)p)) {
77 DEFINE_SPINLOCK(die_lock);
79 void die(const char * str, struct pt_regs * regs, long err)
81 static int die_counter;
84 spin_lock_irq(&die_lock);
87 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
89 CHK_REMOTE_DEBUG(regs);
93 printk("Process: %s (pid: %d, stack limit = %p)\n",
94 current->comm, current->pid, task_stack_page(current) + 1);
96 if (!user_mode(regs) || in_interrupt())
97 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
98 (unsigned long)task_stack_page(current));
101 spin_unlock_irq(&die_lock);
105 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
108 if (!user_mode(regs))
113 * try and fix up kernelspace address errors
114 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
115 * - kernel/userspace interfaces cause a jump to an appropriate handler
116 * - other kernel errors are bad
117 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
119 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
121 if (!user_mode(regs)) {
122 const struct exception_table_entry *fixup;
123 fixup = search_exception_tables(regs->pc);
125 regs->pc = fixup->fixup;
134 #ifdef CONFIG_DEBUG_BUGVERBOSE
135 static inline void do_bug_verbose(struct pt_regs *regs)
140 if (__copy_from_user(&f, (const void __user *)regs->pc,
141 sizeof(struct bug_frame)))
144 len = __strnlen_user(f.file, PATH_MAX) - 1;
145 if (unlikely(len < 0 || len >= PATH_MAX))
146 f.file = "<bad filename>";
147 len = __strnlen_user(f.func, PATH_MAX) - 1;
148 if (unlikely(len < 0 || len >= PATH_MAX))
149 f.func = "<bad function>";
151 printk(KERN_ALERT "kernel BUG in %s() at %s:%d!\n",
152 f.func, f.file, f.line);
155 static inline void do_bug_verbose(struct pt_regs *regs)
158 #endif /* CONFIG_DEBUG_BUGVERBOSE */
159 #endif /* CONFIG_BUG */
161 void handle_BUG(struct pt_regs *regs)
163 do_bug_verbose(regs);
164 die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
168 * handle an instruction that does an unaligned memory access by emulating the
170 * - note that PC _may not_ point to the faulting instruction
171 * (if that instruction is in a branch delay slot)
172 * - return 0 if emulation okay, -EFAULT on existential error
174 static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
176 int ret, index, count;
177 unsigned long *rm, *rn;
178 unsigned char *src, *dst;
180 index = (instruction>>8)&15; /* 0x0F00 */
181 rn = ®s->regs[index];
183 index = (instruction>>4)&15; /* 0x00F0 */
184 rm = ®s->regs[index];
186 count = 1<<(instruction&3);
189 switch (instruction>>12) {
190 case 0: /* mov.[bwl] to/from memory via r0+rn */
191 if (instruction & 8) {
193 src = (unsigned char*) *rm;
194 src += regs->regs[0];
195 dst = (unsigned char*) rn;
196 *(unsigned long*)dst = 0;
198 #ifdef __LITTLE_ENDIAN__
199 if (copy_from_user(dst, src, count))
202 if ((count == 2) && dst[1] & 0x80) {
209 if (__copy_user(dst, src, count))
212 if ((count == 2) && dst[2] & 0x80) {
219 src = (unsigned char*) rm;
220 #if !defined(__LITTLE_ENDIAN__)
223 dst = (unsigned char*) *rn;
224 dst += regs->regs[0];
226 if (copy_to_user(dst, src, count))
232 case 1: /* mov.l Rm,@(disp,Rn) */
233 src = (unsigned char*) rm;
234 dst = (unsigned char*) *rn;
235 dst += (instruction&0x000F)<<2;
237 if (copy_to_user(dst,src,4))
242 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
245 src = (unsigned char*) rm;
246 dst = (unsigned char*) *rn;
247 #if !defined(__LITTLE_ENDIAN__)
250 if (copy_to_user(dst, src, count))
255 case 5: /* mov.l @(disp,Rm),Rn */
256 src = (unsigned char*) *rm;
257 src += (instruction&0x000F)<<2;
258 dst = (unsigned char*) rn;
259 *(unsigned long*)dst = 0;
261 if (copy_from_user(dst,src,4))
266 case 6: /* mov.[bwl] from memory, possibly with post-increment */
267 src = (unsigned char*) *rm;
270 dst = (unsigned char*) rn;
271 *(unsigned long*)dst = 0;
273 #ifdef __LITTLE_ENDIAN__
274 if (copy_from_user(dst, src, count))
277 if ((count == 2) && dst[1] & 0x80) {
284 if (copy_from_user(dst, src, count))
287 if ((count == 2) && dst[2] & 0x80) {
296 switch ((instruction&0xFF00)>>8) {
297 case 0x81: /* mov.w R0,@(disp,Rn) */
298 src = (unsigned char*) ®s->regs[0];
299 #if !defined(__LITTLE_ENDIAN__)
302 dst = (unsigned char*) *rm; /* called Rn in the spec */
303 dst += (instruction&0x000F)<<1;
305 if (copy_to_user(dst, src, 2))
310 case 0x85: /* mov.w @(disp,Rm),R0 */
311 src = (unsigned char*) *rm;
312 src += (instruction&0x000F)<<1;
313 dst = (unsigned char*) ®s->regs[0];
314 *(unsigned long*)dst = 0;
316 #if !defined(__LITTLE_ENDIAN__)
320 if (copy_from_user(dst, src, 2))
323 #ifdef __LITTLE_ENDIAN__
342 /* Argh. Address not only misaligned but also non-existent.
343 * Raise an EFAULT and see if it's trapped
345 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
349 * emulate the instruction in the delay slot
350 * - fetches the instruction from PC+2
352 static inline int handle_unaligned_delayslot(struct pt_regs *regs)
356 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
357 /* the instruction-fetch faulted */
362 die("delay-slot-insn faulting in handle_unaligned_delayslot",
366 return handle_unaligned_ins(instruction,regs);
370 * handle an instruction that does an unaligned memory access
371 * - have to be careful of branch delay-slot instructions that fault
373 * - if the branch would be taken PC points to the branch
374 * - if the branch would not be taken, PC points to delay-slot
376 * - PC always points to delayed branch
377 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
380 /* Macros to determine offset from current PC for branch instructions */
381 /* Explicit type coercion is used to force sign extension where needed */
382 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
383 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
386 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
389 #ifndef CONFIG_CPU_SH2A
390 static int handle_unaligned_notify_count = 10;
392 static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
397 index = (instruction>>8)&15; /* 0x0F00 */
398 rm = regs->regs[index];
400 /* shout about the first ten userspace fixups */
401 if (user_mode(regs) && handle_unaligned_notify_count>0) {
402 handle_unaligned_notify_count--;
404 printk(KERN_NOTICE "Fixing up unaligned userspace access "
405 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
406 current->comm,current->pid,(u16*)regs->pc,instruction);
410 switch (instruction&0xF000) {
412 if (instruction==0x000B) {
414 ret = handle_unaligned_delayslot(regs);
418 else if ((instruction&0x00FF)==0x0023) {
420 ret = handle_unaligned_delayslot(regs);
424 else if ((instruction&0x00FF)==0x0003) {
426 ret = handle_unaligned_delayslot(regs);
428 regs->pr = regs->pc + 4;
433 /* mov.[bwl] to/from memory via r0+rn */
438 case 0x1000: /* mov.l Rm,@(disp,Rn) */
441 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
445 if ((instruction&0x00FF)==0x002B) {
447 ret = handle_unaligned_delayslot(regs);
451 else if ((instruction&0x00FF)==0x000B) {
453 ret = handle_unaligned_delayslot(regs);
455 regs->pr = regs->pc + 4;
460 /* mov.[bwl] to/from memory via r0+rn */
465 case 0x5000: /* mov.l @(disp,Rm),Rn */
468 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
471 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
472 switch (instruction&0x0F00) {
473 case 0x0100: /* mov.w R0,@(disp,Rm) */
475 case 0x0500: /* mov.w @(disp,Rm),R0 */
477 case 0x0B00: /* bf lab - no delayslot*/
479 case 0x0F00: /* bf/s lab */
480 ret = handle_unaligned_delayslot(regs);
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);
490 case 0x0900: /* bt lab - no delayslot */
492 case 0x0D00: /* bt/s lab */
493 ret = handle_unaligned_delayslot(regs);
495 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
496 if ((regs->sr & 0x00000001) == 0)
497 regs->pc += 4; /* next after slot */
500 regs->pc += SH_PC_8BIT_OFFSET(instruction);
506 case 0xA000: /* bra label */
507 ret = handle_unaligned_delayslot(regs);
509 regs->pc += SH_PC_12BIT_OFFSET(instruction);
512 case 0xB000: /* bsr label */
513 ret = handle_unaligned_delayslot(regs);
515 regs->pr = regs->pc + 4;
516 regs->pc += SH_PC_12BIT_OFFSET(instruction);
522 /* handle non-delay-slot instruction */
524 ret = handle_unaligned_ins(instruction,regs);
529 #endif /* CONFIG_CPU_SH2A */
531 #ifdef CONFIG_CPU_HAS_SR_RB
532 #define lookup_exception_vector(x) \
533 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
535 #define lookup_exception_vector(x) \
536 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
540 * Handle various address error exceptions:
541 * - instruction address error:
543 * PC >= 0x80000000 in user mode
544 * - data address error (read and write)
545 * misaligned data access
546 * access to >= 0x80000000 is user mode
547 * Unfortuntaly we can't distinguish between instruction address error
548 * and data address errors caused by read acceses.
550 asmlinkage void do_address_error(struct pt_regs *regs,
551 unsigned long writeaccess,
552 unsigned long address)
554 unsigned long error_code = 0;
557 #ifndef CONFIG_CPU_SH2A
562 /* Intentional ifdef */
563 #ifdef CONFIG_CPU_HAS_SR_RB
564 lookup_exception_vector(error_code);
569 if (user_mode(regs)) {
570 int si_code = BUS_ADRERR;
574 /* bad PC is not something we can fix */
576 si_code = BUS_ADRALN;
580 #ifndef CONFIG_CPU_SH2A
582 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
583 /* Argh. Fault on the instruction itself.
584 This should never happen non-SMP
590 tmp = handle_unaligned_access(instruction, regs);
598 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
599 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
602 info.si_signo = SIGBUS;
604 info.si_code = si_code;
605 info.si_addr = (void *) address;
606 force_sig_info(SIGBUS, &info, current);
609 die("unaligned program counter", regs, error_code);
611 #ifndef CONFIG_CPU_SH2A
613 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
614 /* Argh. Fault on the instruction itself.
615 This should never happen non-SMP
618 die("insn faulting in do_address_error", regs, 0);
621 handle_unaligned_access(instruction, regs);
624 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned "
625 "access\n", current->comm);
627 force_sig(SIGSEGV, current);
634 * SH-DSP support gerg@snapgear.com.
636 int is_dsp_inst(struct pt_regs *regs)
641 * Safe guard if DSP mode is already enabled or we're lacking
642 * the DSP altogether.
644 if (!(cpu_data->flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
647 get_user(inst, ((unsigned short *) regs->pc));
651 /* Check for any type of DSP or support instruction */
652 if ((inst == 0xf000) || (inst == 0x4000))
658 #define is_dsp_inst(regs) (0)
659 #endif /* CONFIG_SH_DSP */
661 #ifdef CONFIG_CPU_SH2A
662 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
663 unsigned long r6, unsigned long r7,
664 struct pt_regs __regs)
666 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
670 case TRAP_DIVZERO_ERROR:
671 info.si_code = FPE_INTDIV;
673 case TRAP_DIVOVF_ERROR:
674 info.si_code = FPE_INTOVF;
678 force_sig_info(SIGFPE, &info, current);
682 /* arch/sh/kernel/cpu/sh4/fpu.c */
683 extern int do_fpu_inst(unsigned short, struct pt_regs *);
684 extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
685 unsigned long r6, unsigned long r7, struct pt_regs __regs);
687 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
688 unsigned long r6, unsigned long r7,
689 struct pt_regs __regs)
691 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
692 unsigned long error_code;
693 struct task_struct *tsk = current;
695 #ifdef CONFIG_SH_FPU_EMU
696 unsigned short inst = 0;
699 get_user(inst, (unsigned short*)regs->pc);
701 err = do_fpu_inst(inst, regs);
706 /* not a FPU inst. */
710 /* Check if it's a DSP instruction */
711 if (is_dsp_inst(regs)) {
712 /* Enable DSP mode, and restart instruction. */
718 lookup_exception_vector(error_code);
721 CHK_REMOTE_DEBUG(regs);
722 force_sig(SIGILL, tsk);
723 die_if_no_fixup("reserved instruction", regs, error_code);
726 #ifdef CONFIG_SH_FPU_EMU
727 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
730 * bfs: 8fxx: PC+=d*2+4;
731 * bts: 8dxx: PC+=d*2+4;
732 * bra: axxx: PC+=D*2+4;
733 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
734 * braf:0x23: PC+=Rn*2+4;
735 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
737 * jsr: 4x0b: PC=Rn after PR=PC+4;
740 if ((inst & 0xfd00) == 0x8d00) {
741 regs->pc += SH_PC_8BIT_OFFSET(inst);
745 if ((inst & 0xe000) == 0xa000) {
746 regs->pc += SH_PC_12BIT_OFFSET(inst);
750 if ((inst & 0xf0df) == 0x0003) {
751 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
755 if ((inst & 0xf0df) == 0x400b) {
756 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
760 if ((inst & 0xffff) == 0x000b) {
769 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
770 unsigned long r6, unsigned long r7,
771 struct pt_regs __regs)
773 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
774 unsigned long error_code;
775 struct task_struct *tsk = current;
776 #ifdef CONFIG_SH_FPU_EMU
777 unsigned short inst = 0;
779 get_user(inst, (unsigned short *)regs->pc + 1);
780 if (!do_fpu_inst(inst, regs)) {
781 get_user(inst, (unsigned short *)regs->pc);
782 if (!emulate_branch(inst, regs))
784 /* fault in branch.*/
786 /* not a FPU inst. */
789 lookup_exception_vector(error_code);
792 CHK_REMOTE_DEBUG(regs);
793 force_sig(SIGILL, tsk);
794 die_if_no_fixup("illegal slot instruction", regs, error_code);
797 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
798 unsigned long r6, unsigned long r7,
799 struct pt_regs __regs)
801 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
804 lookup_exception_vector(ex);
805 die_if_kernel("exception", regs, ex);
808 #if defined(CONFIG_SH_STANDARD_BIOS)
809 void *gdb_vbr_vector;
811 static inline void __init gdb_vbr_init(void)
813 register unsigned long vbr;
816 * Read the old value of the VBR register to initialise
817 * the vector through which debug and BIOS traps are
818 * delegated by the Linux trap handler.
820 asm volatile("stc vbr, %0" : "=r" (vbr));
822 gdb_vbr_vector = (void *)(vbr + 0x100);
823 printk("Setting GDB trap vector to 0x%08lx\n",
824 (unsigned long)gdb_vbr_vector);
828 void __init per_cpu_trap_init(void)
830 extern void *vbr_base;
832 #ifdef CONFIG_SH_STANDARD_BIOS
836 /* NOTE: The VBR value should be at P1
837 (or P2, virtural "fixed" address space).
838 It's definitely should not in physical address. */
840 asm volatile("ldc %0, vbr"
846 void *set_exception_table_vec(unsigned int vec, void *handler)
848 extern void *exception_handling_table[];
851 old_handler = exception_handling_table[vec];
852 exception_handling_table[vec] = handler;
856 extern asmlinkage void address_error_handler(unsigned long r4, unsigned long r5,
857 unsigned long r6, unsigned long r7,
858 struct pt_regs __regs);
860 void __init trap_init(void)
862 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
863 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
865 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
866 defined(CONFIG_SH_FPU_EMU)
868 * For SH-4 lacking an FPU, treat floating point instructions as
869 * reserved. They'll be handled in the math-emu case, or faulted on
872 set_exception_table_evt(0x800, do_reserved_inst);
873 set_exception_table_evt(0x820, do_illegal_slot_inst);
874 #elif defined(CONFIG_SH_FPU)
875 set_exception_table_evt(0x800, do_fpu_state_restore);
876 set_exception_table_evt(0x820, do_fpu_state_restore);
879 #ifdef CONFIG_CPU_SH2
880 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_handler);
882 #ifdef CONFIG_CPU_SH2A
883 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
884 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
887 /* Setup VBR for boot cpu */
891 void show_trace(struct task_struct *tsk, unsigned long *sp,
892 struct pt_regs *regs)
896 if (regs && user_mode(regs))
899 printk("\nCall trace: ");
900 #ifdef CONFIG_KALLSYMS
904 while (!kstack_end(sp)) {
906 if (kernel_text_address(addr))
915 debug_show_held_locks(tsk);
918 void show_stack(struct task_struct *tsk, unsigned long *sp)
925 sp = (unsigned long *)current_stack_pointer;
927 sp = (unsigned long *)tsk->thread.sp;
929 stack = (unsigned long)sp;
930 dump_mem("Stack: ", stack, THREAD_SIZE +
931 (unsigned long)task_stack_page(tsk));
932 show_trace(tsk, sp, NULL);
935 void dump_stack(void)
937 show_stack(NULL, NULL);
939 EXPORT_SYMBOL(dump_stack);