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/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/ptrace.h>
19 #include <linux/timer.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/spinlock.h>
26 #include <linux/module.h>
27 #include <linux/kallsyms.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
32 #include <asm/atomic.h>
33 #include <asm/processor.h>
34 #include <asm/sections.h>
38 #define CHK_REMOTE_DEBUG(regs) \
40 if (kgdb_debug_hook && !user_mode(regs))\
41 (*kgdb_debug_hook)(regs); \
44 #define CHK_REMOTE_DEBUG(regs)
48 #define TRAP_RESERVED_INST 4
49 #define TRAP_ILLEGAL_SLOT_INST 6
51 #define TRAP_RESERVED_INST 12
52 #define TRAP_ILLEGAL_SLOT_INST 13
55 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
60 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
62 for (p = bottom & ~31; p < top; ) {
63 printk("%04lx: ", p & 0xffff);
65 for (i = 0; i < 8; i++, p += 4) {
68 if (p < bottom || p >= top)
71 if (__get_user(val, (unsigned int __user *)p)) {
82 DEFINE_SPINLOCK(die_lock);
84 void die(const char * str, struct pt_regs * regs, long err)
86 static int die_counter;
89 spin_lock_irq(&die_lock);
92 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
94 CHK_REMOTE_DEBUG(regs);
98 printk("Process: %s (pid: %d, stack limit = %p)\n",
99 current->comm, current->pid, task_stack_page(current) + 1);
101 if (!user_mode(regs) || in_interrupt())
102 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
103 (unsigned long)task_stack_page(current));
106 spin_unlock_irq(&die_lock);
110 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
113 if (!user_mode(regs))
117 static int handle_unaligned_notify_count = 10;
120 * try and fix up kernelspace address errors
121 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
122 * - kernel/userspace interfaces cause a jump to an appropriate handler
123 * - other kernel errors are bad
124 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
126 static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
128 if (!user_mode(regs)) {
129 const struct exception_table_entry *fixup;
130 fixup = search_exception_tables(regs->pc);
132 regs->pc = fixup->fixup;
141 * handle an instruction that does an unaligned memory access by emulating the
143 * - note that PC _may not_ point to the faulting instruction
144 * (if that instruction is in a branch delay slot)
145 * - return 0 if emulation okay, -EFAULT on existential error
147 static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
149 int ret, index, count;
150 unsigned long *rm, *rn;
151 unsigned char *src, *dst;
153 index = (instruction>>8)&15; /* 0x0F00 */
154 rn = ®s->regs[index];
156 index = (instruction>>4)&15; /* 0x00F0 */
157 rm = ®s->regs[index];
159 count = 1<<(instruction&3);
162 switch (instruction>>12) {
163 case 0: /* mov.[bwl] to/from memory via r0+rn */
164 if (instruction & 8) {
166 src = (unsigned char*) *rm;
167 src += regs->regs[0];
168 dst = (unsigned char*) rn;
169 *(unsigned long*)dst = 0;
171 #ifdef __LITTLE_ENDIAN__
172 if (copy_from_user(dst, src, count))
175 if ((count == 2) && dst[1] & 0x80) {
182 if (__copy_user(dst, src, count))
185 if ((count == 2) && dst[2] & 0x80) {
192 src = (unsigned char*) rm;
193 #if !defined(__LITTLE_ENDIAN__)
196 dst = (unsigned char*) *rn;
197 dst += regs->regs[0];
199 if (copy_to_user(dst, src, count))
205 case 1: /* mov.l Rm,@(disp,Rn) */
206 src = (unsigned char*) rm;
207 dst = (unsigned char*) *rn;
208 dst += (instruction&0x000F)<<2;
210 if (copy_to_user(dst,src,4))
215 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
218 src = (unsigned char*) rm;
219 dst = (unsigned char*) *rn;
220 #if !defined(__LITTLE_ENDIAN__)
223 if (copy_to_user(dst, src, count))
228 case 5: /* mov.l @(disp,Rm),Rn */
229 src = (unsigned char*) *rm;
230 src += (instruction&0x000F)<<2;
231 dst = (unsigned char*) rn;
232 *(unsigned long*)dst = 0;
234 if (copy_from_user(dst,src,4))
239 case 6: /* mov.[bwl] from memory, possibly with post-increment */
240 src = (unsigned char*) *rm;
243 dst = (unsigned char*) rn;
244 *(unsigned long*)dst = 0;
246 #ifdef __LITTLE_ENDIAN__
247 if (copy_from_user(dst, src, count))
250 if ((count == 2) && dst[1] & 0x80) {
257 if (copy_from_user(dst, src, count))
260 if ((count == 2) && dst[2] & 0x80) {
269 switch ((instruction&0xFF00)>>8) {
270 case 0x81: /* mov.w R0,@(disp,Rn) */
271 src = (unsigned char*) ®s->regs[0];
272 #if !defined(__LITTLE_ENDIAN__)
275 dst = (unsigned char*) *rm; /* called Rn in the spec */
276 dst += (instruction&0x000F)<<1;
278 if (copy_to_user(dst, src, 2))
283 case 0x85: /* mov.w @(disp,Rm),R0 */
284 src = (unsigned char*) *rm;
285 src += (instruction&0x000F)<<1;
286 dst = (unsigned char*) ®s->regs[0];
287 *(unsigned long*)dst = 0;
289 #if !defined(__LITTLE_ENDIAN__)
293 if (copy_from_user(dst, src, 2))
296 #ifdef __LITTLE_ENDIAN__
315 /* Argh. Address not only misaligned but also non-existent.
316 * Raise an EFAULT and see if it's trapped
318 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
322 * emulate the instruction in the delay slot
323 * - fetches the instruction from PC+2
325 static inline int handle_unaligned_delayslot(struct pt_regs *regs)
329 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
330 /* the instruction-fetch faulted */
335 die("delay-slot-insn faulting in handle_unaligned_delayslot", regs, 0);
338 return handle_unaligned_ins(instruction,regs);
342 * handle an instruction that does an unaligned memory access
343 * - have to be careful of branch delay-slot instructions that fault
345 * - if the branch would be taken PC points to the branch
346 * - if the branch would not be taken, PC points to delay-slot
348 * - PC always points to delayed branch
349 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
352 /* Macros to determine offset from current PC for branch instructions */
353 /* Explicit type coercion is used to force sign extension where needed */
354 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
355 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
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("Fixing up unaligned userspace access 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);
495 * Handle various address error exceptions
497 asmlinkage void do_address_error(struct pt_regs *regs,
498 unsigned long writeaccess,
499 unsigned long address)
501 unsigned long error_code;
506 asm volatile("stc r2_bank,%0": "=r" (error_code));
510 if (user_mode(regs)) {
512 current->thread.error_code = error_code;
513 current->thread.trap_no = (writeaccess) ? 8 : 7;
515 /* bad PC is not something we can fix */
520 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
521 /* Argh. Fault on the instruction itself.
522 This should never happen non-SMP
528 tmp = handle_unaligned_access(instruction, regs);
535 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned access\n", current->comm);
536 force_sig(SIGSEGV, current);
539 die("unaligned program counter", regs, error_code);
542 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
543 /* Argh. Fault on the instruction itself.
544 This should never happen non-SMP
547 die("insn faulting in do_address_error", regs, 0);
550 handle_unaligned_access(instruction, regs);
557 * SH-DSP support gerg@snapgear.com.
559 int is_dsp_inst(struct pt_regs *regs)
564 * Safe guard if DSP mode is already enabled or we're lacking
565 * the DSP altogether.
567 if (!(cpu_data->flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
570 get_user(inst, ((unsigned short *) regs->pc));
574 /* Check for any type of DSP or support instruction */
575 if ((inst == 0xf000) || (inst == 0x4000))
581 #define is_dsp_inst(regs) (0)
582 #endif /* CONFIG_SH_DSP */
584 extern int do_fpu_inst(unsigned short, struct pt_regs*);
586 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
587 unsigned long r6, unsigned long r7,
590 unsigned long error_code;
591 struct task_struct *tsk = current;
593 #ifdef CONFIG_SH_FPU_EMU
597 get_user(inst, (unsigned short*)regs.pc);
599 err = do_fpu_inst(inst, ®s);
604 /* not a FPU inst. */
608 /* Check if it's a DSP instruction */
609 if (is_dsp_inst(®s)) {
610 /* Enable DSP mode, and restart instruction. */
616 asm volatile("stc r2_bank, %0": "=r" (error_code));
618 tsk->thread.error_code = error_code;
619 tsk->thread.trap_no = TRAP_RESERVED_INST;
620 CHK_REMOTE_DEBUG(®s);
621 force_sig(SIGILL, tsk);
622 die_if_no_fixup("reserved instruction", ®s, error_code);
625 #ifdef CONFIG_SH_FPU_EMU
626 static int emulate_branch(unsigned short inst, struct pt_regs* regs)
629 * bfs: 8fxx: PC+=d*2+4;
630 * bts: 8dxx: PC+=d*2+4;
631 * bra: axxx: PC+=D*2+4;
632 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
633 * braf:0x23: PC+=Rn*2+4;
634 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
636 * jsr: 4x0b: PC=Rn after PR=PC+4;
639 if ((inst & 0xfd00) == 0x8d00) {
640 regs->pc += SH_PC_8BIT_OFFSET(inst);
644 if ((inst & 0xe000) == 0xa000) {
645 regs->pc += SH_PC_12BIT_OFFSET(inst);
649 if ((inst & 0xf0df) == 0x0003) {
650 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
654 if ((inst & 0xf0df) == 0x400b) {
655 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
659 if ((inst & 0xffff) == 0x000b) {
668 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
669 unsigned long r6, unsigned long r7,
672 unsigned long error_code;
673 struct task_struct *tsk = current;
674 #ifdef CONFIG_SH_FPU_EMU
677 get_user(inst, (unsigned short *)regs.pc + 1);
678 if (!do_fpu_inst(inst, ®s)) {
679 get_user(inst, (unsigned short *)regs.pc);
680 if (!emulate_branch(inst, ®s))
682 /* fault in branch.*/
684 /* not a FPU inst. */
687 asm volatile("stc r2_bank, %0": "=r" (error_code));
689 tsk->thread.error_code = error_code;
690 tsk->thread.trap_no = TRAP_RESERVED_INST;
691 CHK_REMOTE_DEBUG(®s);
692 force_sig(SIGILL, tsk);
693 die_if_no_fixup("illegal slot instruction", ®s, error_code);
696 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
697 unsigned long r6, unsigned long r7,
701 asm volatile("stc r2_bank, %0" : "=r" (ex));
702 die_if_kernel("exception", ®s, ex);
705 #if defined(CONFIG_SH_STANDARD_BIOS)
706 void *gdb_vbr_vector;
708 static inline void __init gdb_vbr_init(void)
710 register unsigned long vbr;
713 * Read the old value of the VBR register to initialise
714 * the vector through which debug and BIOS traps are
715 * delegated by the Linux trap handler.
717 asm volatile("stc vbr, %0" : "=r" (vbr));
719 gdb_vbr_vector = (void *)(vbr + 0x100);
720 printk("Setting GDB trap vector to 0x%08lx\n",
721 (unsigned long)gdb_vbr_vector);
725 void __init per_cpu_trap_init(void)
727 extern void *vbr_base;
729 #ifdef CONFIG_SH_STANDARD_BIOS
733 /* NOTE: The VBR value should be at P1
734 (or P2, virtural "fixed" address space).
735 It's definitely should not in physical address. */
737 asm volatile("ldc %0, vbr"
743 void __init trap_init(void)
745 extern void *exception_handling_table[];
747 exception_handling_table[TRAP_RESERVED_INST]
748 = (void *)do_reserved_inst;
749 exception_handling_table[TRAP_ILLEGAL_SLOT_INST]
750 = (void *)do_illegal_slot_inst;
752 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
753 defined(CONFIG_SH_FPU_EMU)
755 * For SH-4 lacking an FPU, treat floating point instructions as
756 * reserved. They'll be handled in the math-emu case, or faulted on
759 /* entry 64 corresponds to EXPEVT=0x800 */
760 exception_handling_table[64] = (void *)do_reserved_inst;
761 exception_handling_table[65] = (void *)do_illegal_slot_inst;
764 /* Setup VBR for boot cpu */
768 void show_trace(struct task_struct *tsk, unsigned long *sp,
769 struct pt_regs *regs)
773 if (regs && user_mode(regs))
776 printk("\nCall trace: ");
777 #ifdef CONFIG_KALLSYMS
781 while (!kstack_end(sp)) {
783 if (kernel_text_address(addr))
790 void show_stack(struct task_struct *tsk, unsigned long *sp)
797 sp = (unsigned long *)current_stack_pointer;
799 sp = (unsigned long *)tsk->thread.sp;
801 stack = (unsigned long)sp;
802 dump_mem("Stack: ", stack, THREAD_SIZE +
803 (unsigned long)task_stack_page(tsk));
804 show_trace(tsk, sp, NULL);
807 void dump_stack(void)
809 show_stack(NULL, NULL);
811 EXPORT_SYMBOL(dump_stack);