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 - 2007 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/kdebug.h>
24 #include <linux/kexec.h>
25 #include <linux/limits.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
32 #define CHK_REMOTE_DEBUG(regs) \
34 if (kgdb_debug_hook && !user_mode(regs))\
35 (*kgdb_debug_hook)(regs); \
38 #define CHK_REMOTE_DEBUG(regs)
42 # define TRAP_RESERVED_INST 4
43 # define TRAP_ILLEGAL_SLOT_INST 6
44 # define TRAP_ADDRESS_ERROR 9
45 # ifdef CONFIG_CPU_SH2A
46 # define TRAP_DIVZERO_ERROR 17
47 # define TRAP_DIVOVF_ERROR 18
50 #define TRAP_RESERVED_INST 12
51 #define TRAP_ILLEGAL_SLOT_INST 13
54 static void dump_mem(const char *str
, unsigned long bottom
, unsigned long top
)
59 printk("%s(0x%08lx to 0x%08lx)\n", str
, bottom
, top
);
61 for (p
= bottom
& ~31; p
< top
; ) {
62 printk("%04lx: ", p
& 0xffff);
64 for (i
= 0; i
< 8; i
++, p
+= 4) {
67 if (p
< bottom
|| p
>= top
)
70 if (__get_user(val
, (unsigned int __user
*)p
)) {
81 static DEFINE_SPINLOCK(die_lock
);
83 void die(const char * str
, struct pt_regs
* regs
, long err
)
85 static int die_counter
;
90 spin_lock_irq(&die_lock
);
93 printk("%s: %04lx [#%d]\n", str
, err
& 0xffff, ++die_counter
);
95 CHK_REMOTE_DEBUG(regs
);
99 printk("Process: %s (pid: %d, stack limit = %p)\n", current
->comm
,
100 task_pid_nr(current
), task_stack_page(current
) + 1);
102 if (!user_mode(regs
) || in_interrupt())
103 dump_mem("Stack: ", regs
->regs
[15], THREAD_SIZE
+
104 (unsigned long)task_stack_page(current
));
107 add_taint(TAINT_DIE
);
108 spin_unlock_irq(&die_lock
);
110 if (kexec_should_crash(current
))
114 panic("Fatal exception in interrupt");
117 panic("Fatal exception");
123 static inline void die_if_kernel(const char *str
, struct pt_regs
*regs
,
126 if (!user_mode(regs
))
131 * try and fix up kernelspace address errors
132 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
133 * - kernel/userspace interfaces cause a jump to an appropriate handler
134 * - other kernel errors are bad
135 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
137 static int die_if_no_fixup(const char * str
, struct pt_regs
* regs
, long err
)
139 if (!user_mode(regs
)) {
140 const struct exception_table_entry
*fixup
;
141 fixup
= search_exception_tables(regs
->pc
);
143 regs
->pc
= fixup
->fixup
;
151 static inline void sign_extend(unsigned int count
, unsigned char *dst
)
153 #ifdef __LITTLE_ENDIAN__
154 if ((count
== 1) && dst
[0] & 0x80) {
159 if ((count
== 2) && dst
[1] & 0x80) {
164 if ((count
== 1) && dst
[3] & 0x80) {
169 if ((count
== 2) && dst
[2] & 0x80) {
176 static struct mem_access user_mem_access
= {
182 * handle an instruction that does an unaligned memory access by emulating the
184 * - note that PC _may not_ point to the faulting instruction
185 * (if that instruction is in a branch delay slot)
186 * - return 0 if emulation okay, -EFAULT on existential error
188 static int handle_unaligned_ins(opcode_t instruction
, struct pt_regs
*regs
,
189 struct mem_access
*ma
)
191 int ret
, index
, count
;
192 unsigned long *rm
, *rn
;
193 unsigned char *src
, *dst
;
195 index
= (instruction
>>8)&15; /* 0x0F00 */
196 rn
= ®s
->regs
[index
];
198 index
= (instruction
>>4)&15; /* 0x00F0 */
199 rm
= ®s
->regs
[index
];
201 count
= 1<<(instruction
&3);
204 switch (instruction
>>12) {
205 case 0: /* mov.[bwl] to/from memory via r0+rn */
206 if (instruction
& 8) {
208 src
= (unsigned char*) *rm
;
209 src
+= regs
->regs
[0];
210 dst
= (unsigned char*) rn
;
211 *(unsigned long*)dst
= 0;
213 #if !defined(__LITTLE_ENDIAN__)
216 if (ma
->from(dst
, src
, count
))
219 sign_extend(count
, dst
);
222 src
= (unsigned char*) rm
;
223 #if !defined(__LITTLE_ENDIAN__)
226 dst
= (unsigned char*) *rn
;
227 dst
+= regs
->regs
[0];
229 if (ma
->to(dst
, src
, count
))
235 case 1: /* mov.l Rm,@(disp,Rn) */
236 src
= (unsigned char*) rm
;
237 dst
= (unsigned char*) *rn
;
238 dst
+= (instruction
&0x000F)<<2;
240 if (ma
->to(dst
, src
, 4))
245 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
248 src
= (unsigned char*) rm
;
249 dst
= (unsigned char*) *rn
;
250 #if !defined(__LITTLE_ENDIAN__)
253 if (ma
->to(dst
, src
, count
))
258 case 5: /* mov.l @(disp,Rm),Rn */
259 src
= (unsigned char*) *rm
;
260 src
+= (instruction
&0x000F)<<2;
261 dst
= (unsigned char*) rn
;
262 *(unsigned long*)dst
= 0;
264 if (ma
->from(dst
, src
, 4))
269 case 6: /* mov.[bwl] from memory, possibly with post-increment */
270 src
= (unsigned char*) *rm
;
273 dst
= (unsigned char*) rn
;
274 *(unsigned long*)dst
= 0;
276 #if !defined(__LITTLE_ENDIAN__)
279 if (ma
->from(dst
, src
, count
))
281 sign_extend(count
, dst
);
286 switch ((instruction
&0xFF00)>>8) {
287 case 0x81: /* mov.w R0,@(disp,Rn) */
288 src
= (unsigned char*) ®s
->regs
[0];
289 #if !defined(__LITTLE_ENDIAN__)
292 dst
= (unsigned char*) *rm
; /* called Rn in the spec */
293 dst
+= (instruction
&0x000F)<<1;
295 if (ma
->to(dst
, src
, 2))
300 case 0x85: /* mov.w @(disp,Rm),R0 */
301 src
= (unsigned char*) *rm
;
302 src
+= (instruction
&0x000F)<<1;
303 dst
= (unsigned char*) ®s
->regs
[0];
304 *(unsigned long*)dst
= 0;
306 #if !defined(__LITTLE_ENDIAN__)
309 if (ma
->from(dst
, src
, 2))
320 /* Argh. Address not only misaligned but also non-existent.
321 * Raise an EFAULT and see if it's trapped
323 return die_if_no_fixup("Fault in unaligned fixup", regs
, 0);
327 * emulate the instruction in the delay slot
328 * - fetches the instruction from PC+2
330 static inline int handle_delayslot(struct pt_regs
*regs
,
331 opcode_t old_instruction
,
332 struct mem_access
*ma
)
334 opcode_t instruction
;
335 void *addr
= (void *)(regs
->pc
+ instruction_size(old_instruction
));
337 if (copy_from_user(&instruction
, addr
, sizeof(instruction
))) {
338 /* the instruction-fetch faulted */
343 die("delay-slot-insn faulting in handle_unaligned_delayslot",
347 return handle_unaligned_ins(instruction
, regs
, ma
);
351 * handle an instruction that does an unaligned memory access
352 * - have to be careful of branch delay-slot instructions that fault
354 * - if the branch would be taken PC points to the branch
355 * - if the branch would not be taken, PC points to delay-slot
357 * - PC always points to delayed branch
358 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
361 /* Macros to determine offset from current PC for branch instructions */
362 /* Explicit type coercion is used to force sign extension where needed */
363 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
364 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
367 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
371 static int handle_unaligned_notify_count
= 10;
373 int handle_unaligned_access(opcode_t instruction
, struct pt_regs
*regs
,
374 struct mem_access
*ma
)
379 index
= (instruction
>>8)&15; /* 0x0F00 */
380 rm
= regs
->regs
[index
];
382 /* shout about the first ten userspace fixups */
383 if (user_mode(regs
) && handle_unaligned_notify_count
>0) {
384 handle_unaligned_notify_count
--;
386 printk(KERN_NOTICE
"Fixing up unaligned userspace access "
387 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
388 current
->comm
, task_pid_nr(current
),
389 (void *)regs
->pc
, instruction
);
393 switch (instruction
&0xF000) {
395 if (instruction
==0x000B) {
397 ret
= handle_delayslot(regs
, instruction
, ma
);
401 else if ((instruction
&0x00FF)==0x0023) {
403 ret
= handle_delayslot(regs
, instruction
, ma
);
407 else if ((instruction
&0x00FF)==0x0003) {
409 ret
= handle_delayslot(regs
, instruction
, ma
);
411 regs
->pr
= regs
->pc
+ 4;
416 /* mov.[bwl] to/from memory via r0+rn */
421 case 0x1000: /* mov.l Rm,@(disp,Rn) */
424 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
428 if ((instruction
&0x00FF)==0x002B) {
430 ret
= handle_delayslot(regs
, instruction
, ma
);
434 else if ((instruction
&0x00FF)==0x000B) {
436 ret
= handle_delayslot(regs
, instruction
, ma
);
438 regs
->pr
= regs
->pc
+ 4;
443 /* mov.[bwl] to/from memory via r0+rn */
448 case 0x5000: /* mov.l @(disp,Rm),Rn */
451 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
454 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
455 switch (instruction
&0x0F00) {
456 case 0x0100: /* mov.w R0,@(disp,Rm) */
458 case 0x0500: /* mov.w @(disp,Rm),R0 */
460 case 0x0B00: /* bf lab - no delayslot*/
462 case 0x0F00: /* bf/s lab */
463 ret
= handle_delayslot(regs
, instruction
, ma
);
465 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
466 if ((regs
->sr
& 0x00000001) != 0)
467 regs
->pc
+= 4; /* next after slot */
470 regs
->pc
+= SH_PC_8BIT_OFFSET(instruction
);
473 case 0x0900: /* bt lab - no delayslot */
475 case 0x0D00: /* bt/s lab */
476 ret
= handle_delayslot(regs
, instruction
, ma
);
478 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
479 if ((regs
->sr
& 0x00000001) == 0)
480 regs
->pc
+= 4; /* next after slot */
483 regs
->pc
+= SH_PC_8BIT_OFFSET(instruction
);
489 case 0xA000: /* bra label */
490 ret
= handle_delayslot(regs
, instruction
, ma
);
492 regs
->pc
+= SH_PC_12BIT_OFFSET(instruction
);
495 case 0xB000: /* bsr label */
496 ret
= handle_delayslot(regs
, instruction
, ma
);
498 regs
->pr
= regs
->pc
+ 4;
499 regs
->pc
+= SH_PC_12BIT_OFFSET(instruction
);
505 /* handle non-delay-slot instruction */
507 ret
= handle_unaligned_ins(instruction
, regs
, ma
);
509 regs
->pc
+= instruction_size(instruction
);
513 #ifdef CONFIG_CPU_HAS_SR_RB
514 #define lookup_exception_vector(x) \
515 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
517 #define lookup_exception_vector(x) \
518 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
522 * Handle various address error exceptions:
523 * - instruction address error:
525 * PC >= 0x80000000 in user mode
526 * - data address error (read and write)
527 * misaligned data access
528 * access to >= 0x80000000 is user mode
529 * Unfortuntaly we can't distinguish between instruction address error
530 * and data address errors caused by read accesses.
532 asmlinkage
void do_address_error(struct pt_regs
*regs
,
533 unsigned long writeaccess
,
534 unsigned long address
)
536 unsigned long error_code
= 0;
539 opcode_t instruction
;
542 /* Intentional ifdef */
543 #ifdef CONFIG_CPU_HAS_SR_RB
544 lookup_exception_vector(error_code
);
549 if (user_mode(regs
)) {
550 int si_code
= BUS_ADRERR
;
554 /* bad PC is not something we can fix */
556 si_code
= BUS_ADRALN
;
561 if (copy_from_user(&instruction
, (void *)(regs
->pc
),
562 sizeof(instruction
))) {
563 /* Argh. Fault on the instruction itself.
564 This should never happen non-SMP
570 tmp
= handle_unaligned_access(instruction
, regs
,
577 printk(KERN_NOTICE
"Sending SIGBUS to \"%s\" due to unaligned "
578 "access (PC %lx PR %lx)\n", current
->comm
, regs
->pc
,
581 info
.si_signo
= SIGBUS
;
583 info
.si_code
= si_code
;
584 info
.si_addr
= (void __user
*)address
;
585 force_sig_info(SIGBUS
, &info
, current
);
588 die("unaligned program counter", regs
, error_code
);
591 if (copy_from_user(&instruction
, (void *)(regs
->pc
),
592 sizeof(instruction
))) {
593 /* Argh. Fault on the instruction itself.
594 This should never happen non-SMP
597 die("insn faulting in do_address_error", regs
, 0);
600 handle_unaligned_access(instruction
, regs
, &user_mem_access
);
607 * SH-DSP support gerg@snapgear.com.
609 int is_dsp_inst(struct pt_regs
*regs
)
611 unsigned short inst
= 0;
614 * Safe guard if DSP mode is already enabled or we're lacking
615 * the DSP altogether.
617 if (!(current_cpu_data
.flags
& CPU_HAS_DSP
) || (regs
->sr
& SR_DSP
))
620 get_user(inst
, ((unsigned short *) regs
->pc
));
624 /* Check for any type of DSP or support instruction */
625 if ((inst
== 0xf000) || (inst
== 0x4000))
631 #define is_dsp_inst(regs) (0)
632 #endif /* CONFIG_SH_DSP */
634 #ifdef CONFIG_CPU_SH2A
635 asmlinkage
void do_divide_error(unsigned long r4
, unsigned long r5
,
636 unsigned long r6
, unsigned long r7
,
637 struct pt_regs __regs
)
642 case TRAP_DIVZERO_ERROR
:
643 info
.si_code
= FPE_INTDIV
;
645 case TRAP_DIVOVF_ERROR
:
646 info
.si_code
= FPE_INTOVF
;
650 force_sig_info(SIGFPE
, &info
, current
);
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
);
670 regs
->pc
+= instruction_size(inst
);
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 __cpuinit
per_cpu_trap_init(void)
797 extern void *vbr_base
;
799 #ifdef CONFIG_SH_STANDARD_BIOS
800 if (raw_smp_processor_id() == 0)
804 /* NOTE: The VBR value should be at P1
805 (or P2, virtural "fixed" address space).
806 It's definitely should not in physical address. */
808 asm volatile("ldc %0, vbr"
814 void *set_exception_table_vec(unsigned int vec
, void *handler
)
816 extern void *exception_handling_table
[];
819 old_handler
= exception_handling_table
[vec
];
820 exception_handling_table
[vec
] = handler
;
824 void __init
trap_init(void)
826 set_exception_table_vec(TRAP_RESERVED_INST
, do_reserved_inst
);
827 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST
, do_illegal_slot_inst
);
829 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
830 defined(CONFIG_SH_FPU_EMU)
832 * For SH-4 lacking an FPU, treat floating point instructions as
833 * reserved. They'll be handled in the math-emu case, or faulted on
836 set_exception_table_evt(0x800, do_reserved_inst
);
837 set_exception_table_evt(0x820, do_illegal_slot_inst
);
838 #elif defined(CONFIG_SH_FPU)
839 #ifdef CONFIG_CPU_SUBTYPE_SHX3
840 set_exception_table_evt(0xd80, fpu_state_restore_trap_handler
);
841 set_exception_table_evt(0xda0, fpu_state_restore_trap_handler
);
843 set_exception_table_evt(0x800, fpu_state_restore_trap_handler
);
844 set_exception_table_evt(0x820, fpu_state_restore_trap_handler
);
848 #ifdef CONFIG_CPU_SH2
849 set_exception_table_vec(TRAP_ADDRESS_ERROR
, address_error_trap_handler
);
851 #ifdef CONFIG_CPU_SH2A
852 set_exception_table_vec(TRAP_DIVZERO_ERROR
, do_divide_error
);
853 set_exception_table_vec(TRAP_DIVOVF_ERROR
, do_divide_error
);
856 /* Setup VBR for boot cpu */
860 void show_trace(struct task_struct
*tsk
, unsigned long *sp
,
861 struct pt_regs
*regs
)
865 if (regs
&& user_mode(regs
))
868 printk("\nCall trace: ");
869 #ifdef CONFIG_KALLSYMS
873 while (!kstack_end(sp
)) {
875 if (kernel_text_address(addr
))
884 debug_show_held_locks(tsk
);
887 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
894 sp
= (unsigned long *)current_stack_pointer
;
896 sp
= (unsigned long *)tsk
->thread
.sp
;
898 stack
= (unsigned long)sp
;
899 dump_mem("Stack: ", stack
, THREAD_SIZE
+
900 (unsigned long)task_stack_page(tsk
));
901 show_trace(tsk
, sp
, NULL
);
904 void dump_stack(void)
906 show_stack(NULL
, NULL
);
908 EXPORT_SYMBOL(dump_stack
);