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
39 #define TRAP_RESERVED_INST 12
40 #define TRAP_ILLEGAL_SLOT_INST 13
43 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
48 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
50 for (p = bottom & ~31; p < top; ) {
51 printk("%04lx: ", p & 0xffff);
53 for (i = 0; i < 8; i++, p += 4) {
56 if (p < bottom || p >= top)
59 if (__get_user(val, (unsigned int __user *)p)) {
70 DEFINE_SPINLOCK(die_lock);
72 void die(const char * str, struct pt_regs * regs, long err)
74 static int die_counter;
77 spin_lock_irq(&die_lock);
80 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
82 CHK_REMOTE_DEBUG(regs);
86 printk("Process: %s (pid: %d, stack limit = %p)\n",
87 current->comm, current->pid, task_stack_page(current) + 1);
89 if (!user_mode(regs) || in_interrupt())
90 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
91 (unsigned long)task_stack_page(current));
94 spin_unlock_irq(&die_lock);
98 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
101 if (!user_mode(regs))
105 static int handle_unaligned_notify_count = 10;
108 * try and fix up kernelspace address errors
109 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
110 * - kernel/userspace interfaces cause a jump to an appropriate handler
111 * - other kernel errors are bad
112 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
114 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
116 if (!user_mode(regs)) {
117 const struct exception_table_entry *fixup;
118 fixup = search_exception_tables(regs->pc);
120 regs->pc = fixup->fixup;
129 * handle an instruction that does an unaligned memory access by emulating the
131 * - note that PC _may not_ point to the faulting instruction
132 * (if that instruction is in a branch delay slot)
133 * - return 0 if emulation okay, -EFAULT on existential error
135 static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
137 int ret, index, count;
138 unsigned long *rm, *rn;
139 unsigned char *src, *dst;
141 index = (instruction>>8)&15; /* 0x0F00 */
142 rn = ®s->regs[index];
144 index = (instruction>>4)&15; /* 0x00F0 */
145 rm = ®s->regs[index];
147 count = 1<<(instruction&3);
150 switch (instruction>>12) {
151 case 0: /* mov.[bwl] to/from memory via r0+rn */
152 if (instruction & 8) {
154 src = (unsigned char*) *rm;
155 src += regs->regs[0];
156 dst = (unsigned char*) rn;
157 *(unsigned long*)dst = 0;
159 #ifdef __LITTLE_ENDIAN__
160 if (copy_from_user(dst, src, count))
163 if ((count == 2) && dst[1] & 0x80) {
170 if (__copy_user(dst, src, count))
173 if ((count == 2) && dst[2] & 0x80) {
180 src = (unsigned char*) rm;
181 #if !defined(__LITTLE_ENDIAN__)
184 dst = (unsigned char*) *rn;
185 dst += regs->regs[0];
187 if (copy_to_user(dst, src, count))
193 case 1: /* mov.l Rm,@(disp,Rn) */
194 src = (unsigned char*) rm;
195 dst = (unsigned char*) *rn;
196 dst += (instruction&0x000F)<<2;
198 if (copy_to_user(dst,src,4))
203 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
206 src = (unsigned char*) rm;
207 dst = (unsigned char*) *rn;
208 #if !defined(__LITTLE_ENDIAN__)
211 if (copy_to_user(dst, src, count))
216 case 5: /* mov.l @(disp,Rm),Rn */
217 src = (unsigned char*) *rm;
218 src += (instruction&0x000F)<<2;
219 dst = (unsigned char*) rn;
220 *(unsigned long*)dst = 0;
222 if (copy_from_user(dst,src,4))
227 case 6: /* mov.[bwl] from memory, possibly with post-increment */
228 src = (unsigned char*) *rm;
231 dst = (unsigned char*) rn;
232 *(unsigned long*)dst = 0;
234 #ifdef __LITTLE_ENDIAN__
235 if (copy_from_user(dst, src, count))
238 if ((count == 2) && dst[1] & 0x80) {
245 if (copy_from_user(dst, src, count))
248 if ((count == 2) && dst[2] & 0x80) {
257 switch ((instruction&0xFF00)>>8) {
258 case 0x81: /* mov.w R0,@(disp,Rn) */
259 src = (unsigned char*) ®s->regs[0];
260 #if !defined(__LITTLE_ENDIAN__)
263 dst = (unsigned char*) *rm; /* called Rn in the spec */
264 dst += (instruction&0x000F)<<1;
266 if (copy_to_user(dst, src, 2))
271 case 0x85: /* mov.w @(disp,Rm),R0 */
272 src = (unsigned char*) *rm;
273 src += (instruction&0x000F)<<1;
274 dst = (unsigned char*) ®s->regs[0];
275 *(unsigned long*)dst = 0;
277 #if !defined(__LITTLE_ENDIAN__)
281 if (copy_from_user(dst, src, 2))
284 #ifdef __LITTLE_ENDIAN__
303 /* Argh. Address not only misaligned but also non-existent.
304 * Raise an EFAULT and see if it's trapped
306 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
310 * emulate the instruction in the delay slot
311 * - fetches the instruction from PC+2
313 static inline int handle_unaligned_delayslot(struct pt_regs *regs)
317 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
318 /* the instruction-fetch faulted */
323 die("delay-slot-insn faulting in handle_unaligned_delayslot", regs, 0);
326 return handle_unaligned_ins(instruction,regs);
330 * handle an instruction that does an unaligned memory access
331 * - have to be careful of branch delay-slot instructions that fault
333 * - if the branch would be taken PC points to the branch
334 * - if the branch would not be taken, PC points to delay-slot
336 * - PC always points to delayed branch
337 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
340 /* Macros to determine offset from current PC for branch instructions */
341 /* Explicit type coercion is used to force sign extension where needed */
342 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
343 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
345 static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
350 index = (instruction>>8)&15; /* 0x0F00 */
351 rm = regs->regs[index];
353 /* shout about the first ten userspace fixups */
354 if (user_mode(regs) && handle_unaligned_notify_count>0) {
355 handle_unaligned_notify_count--;
357 printk("Fixing up unaligned userspace access in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
358 current->comm,current->pid,(u16*)regs->pc,instruction);
362 switch (instruction&0xF000) {
364 if (instruction==0x000B) {
366 ret = handle_unaligned_delayslot(regs);
370 else if ((instruction&0x00FF)==0x0023) {
372 ret = handle_unaligned_delayslot(regs);
376 else if ((instruction&0x00FF)==0x0003) {
378 ret = handle_unaligned_delayslot(regs);
380 regs->pr = regs->pc + 4;
385 /* mov.[bwl] to/from memory via r0+rn */
390 case 0x1000: /* mov.l Rm,@(disp,Rn) */
393 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
397 if ((instruction&0x00FF)==0x002B) {
399 ret = handle_unaligned_delayslot(regs);
403 else if ((instruction&0x00FF)==0x000B) {
405 ret = handle_unaligned_delayslot(regs);
407 regs->pr = regs->pc + 4;
412 /* mov.[bwl] to/from memory via r0+rn */
417 case 0x5000: /* mov.l @(disp,Rm),Rn */
420 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
423 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
424 switch (instruction&0x0F00) {
425 case 0x0100: /* mov.w R0,@(disp,Rm) */
427 case 0x0500: /* mov.w @(disp,Rm),R0 */
429 case 0x0B00: /* bf lab - no delayslot*/
431 case 0x0F00: /* bf/s lab */
432 ret = handle_unaligned_delayslot(regs);
434 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
435 if ((regs->sr & 0x00000001) != 0)
436 regs->pc += 4; /* next after slot */
439 regs->pc += SH_PC_8BIT_OFFSET(instruction);
442 case 0x0900: /* bt lab - no delayslot */
444 case 0x0D00: /* bt/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);
458 case 0xA000: /* bra label */
459 ret = handle_unaligned_delayslot(regs);
461 regs->pc += SH_PC_12BIT_OFFSET(instruction);
464 case 0xB000: /* bsr label */
465 ret = handle_unaligned_delayslot(regs);
467 regs->pr = regs->pc + 4;
468 regs->pc += SH_PC_12BIT_OFFSET(instruction);
474 /* handle non-delay-slot instruction */
476 ret = handle_unaligned_ins(instruction,regs);
483 * Handle various address error exceptions
485 asmlinkage void do_address_error(struct pt_regs *regs,
486 unsigned long writeaccess,
487 unsigned long address)
489 unsigned long error_code;
494 asm volatile("stc r2_bank,%0": "=r" (error_code));
498 if (user_mode(regs)) {
500 current->thread.error_code = error_code;
501 current->thread.trap_no = (writeaccess) ? 8 : 7;
503 /* bad PC is not something we can fix */
508 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
509 /* Argh. Fault on the instruction itself.
510 This should never happen non-SMP
516 tmp = handle_unaligned_access(instruction, regs);
523 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned access\n", current->comm);
524 force_sig(SIGSEGV, current);
527 die("unaligned program counter", regs, error_code);
530 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
531 /* Argh. Fault on the instruction itself.
532 This should never happen non-SMP
535 die("insn faulting in do_address_error", regs, 0);
538 handle_unaligned_access(instruction, regs);
545 * SH-DSP support gerg@snapgear.com.
547 int is_dsp_inst(struct pt_regs *regs)
552 * Safe guard if DSP mode is already enabled or we're lacking
553 * the DSP altogether.
555 if (!(cpu_data->flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
558 get_user(inst, ((unsigned short *) regs->pc));
562 /* Check for any type of DSP or support instruction */
563 if ((inst == 0xf000) || (inst == 0x4000))
569 #define is_dsp_inst(regs) (0)
570 #endif /* CONFIG_SH_DSP */
572 /* arch/sh/kernel/cpu/sh4/fpu.c */
573 extern int do_fpu_inst(unsigned short, struct pt_regs *);
574 extern asmlinkage void do_fpu_state_restore(unsigned long r4, unsigned long r5,
575 unsigned long r6, unsigned long r7, struct pt_regs regs);
577 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
578 unsigned long r6, unsigned long r7,
581 unsigned long error_code;
582 struct task_struct *tsk = current;
584 #ifdef CONFIG_SH_FPU_EMU
588 get_user(inst, (unsigned short*)regs.pc);
590 err = do_fpu_inst(inst, ®s);
595 /* not a FPU inst. */
599 /* Check if it's a DSP instruction */
600 if (is_dsp_inst(®s)) {
601 /* Enable DSP mode, and restart instruction. */
607 asm volatile("stc r2_bank, %0": "=r" (error_code));
609 tsk->thread.error_code = error_code;
610 tsk->thread.trap_no = TRAP_RESERVED_INST;
611 CHK_REMOTE_DEBUG(®s);
612 force_sig(SIGILL, tsk);
613 die_if_no_fixup("reserved instruction", ®s, error_code);
616 #ifdef CONFIG_SH_FPU_EMU
617 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
620 * bfs: 8fxx: PC+=d*2+4;
621 * bts: 8dxx: PC+=d*2+4;
622 * bra: axxx: PC+=D*2+4;
623 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
624 * braf:0x23: PC+=Rn*2+4;
625 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
627 * jsr: 4x0b: PC=Rn after PR=PC+4;
630 if ((inst & 0xfd00) == 0x8d00) {
631 regs->pc += SH_PC_8BIT_OFFSET(inst);
635 if ((inst & 0xe000) == 0xa000) {
636 regs->pc += SH_PC_12BIT_OFFSET(inst);
640 if ((inst & 0xf0df) == 0x0003) {
641 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
645 if ((inst & 0xf0df) == 0x400b) {
646 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
650 if ((inst & 0xffff) == 0x000b) {
659 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
660 unsigned long r6, unsigned long r7,
663 unsigned long error_code;
664 struct task_struct *tsk = current;
665 #ifdef CONFIG_SH_FPU_EMU
668 get_user(inst, (unsigned short *)regs.pc + 1);
669 if (!do_fpu_inst(inst, ®s)) {
670 get_user(inst, (unsigned short *)regs.pc);
671 if (!emulate_branch(inst, ®s))
673 /* fault in branch.*/
675 /* not a FPU inst. */
678 asm volatile("stc r2_bank, %0": "=r" (error_code));
680 tsk->thread.error_code = error_code;
681 tsk->thread.trap_no = TRAP_RESERVED_INST;
682 CHK_REMOTE_DEBUG(®s);
683 force_sig(SIGILL, tsk);
684 die_if_no_fixup("illegal slot instruction", ®s, error_code);
687 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
688 unsigned long r6, unsigned long r7,
692 asm volatile("stc r2_bank, %0" : "=r" (ex));
693 die_if_kernel("exception", ®s, ex);
696 #if defined(CONFIG_SH_STANDARD_BIOS)
697 void *gdb_vbr_vector;
699 static inline void __init gdb_vbr_init(void)
701 register unsigned long vbr;
704 * Read the old value of the VBR register to initialise
705 * the vector through which debug and BIOS traps are
706 * delegated by the Linux trap handler.
708 asm volatile("stc vbr, %0" : "=r" (vbr));
710 gdb_vbr_vector = (void *)(vbr + 0x100);
711 printk("Setting GDB trap vector to 0x%08lx\n",
712 (unsigned long)gdb_vbr_vector);
716 void __init per_cpu_trap_init(void)
718 extern void *vbr_base;
720 #ifdef CONFIG_SH_STANDARD_BIOS
724 /* NOTE: The VBR value should be at P1
725 (or P2, virtural "fixed" address space).
726 It's definitely should not in physical address. */
728 asm volatile("ldc %0, vbr"
734 void *set_exception_table_vec(unsigned int vec, void *handler)
736 extern void *exception_handling_table[];
739 old_handler = exception_handling_table[vec];
740 exception_handling_table[vec] = handler;
744 void __init trap_init(void)
746 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
747 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
749 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
750 defined(CONFIG_SH_FPU_EMU)
752 * For SH-4 lacking an FPU, treat floating point instructions as
753 * reserved. They'll be handled in the math-emu case, or faulted on
756 set_exception_table_evt(0x800, do_reserved_inst);
757 set_exception_table_evt(0x820, do_illegal_slot_inst);
758 #elif defined(CONFIG_SH_FPU)
759 set_exception_table_evt(0x800, do_fpu_state_restore);
760 set_exception_table_evt(0x820, do_fpu_state_restore);
763 /* Setup VBR for boot cpu */
767 void show_trace(struct task_struct *tsk, unsigned long *sp,
768 struct pt_regs *regs)
772 if (regs && user_mode(regs))
775 printk("\nCall trace: ");
776 #ifdef CONFIG_KALLSYMS
780 while (!kstack_end(sp)) {
782 if (kernel_text_address(addr))
789 void show_stack(struct task_struct *tsk, unsigned long *sp)
796 sp = (unsigned long *)current_stack_pointer;
798 sp = (unsigned long *)tsk->thread.sp;
800 stack = (unsigned long)sp;
801 dump_mem("Stack: ", stack, THREAD_SIZE +
802 (unsigned long)task_stack_page(tsk));
803 show_trace(tsk, sp, NULL);
806 void dump_stack(void)
808 show_stack(NULL, NULL);
810 EXPORT_SYMBOL(dump_stack);