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
38 # define TRAP_ADDRESS_ERROR 9
39 # ifdef CONFIG_CPU_SH2A
40 # define TRAP_DIVZERO_ERROR 17
41 # define TRAP_DIVOVF_ERROR 18
44 #define TRAP_RESERVED_INST 12
45 #define TRAP_ILLEGAL_SLOT_INST 13
48 static void dump_mem(const char *str
, unsigned long bottom
, unsigned long top
)
53 printk("%s(0x%08lx to 0x%08lx)\n", str
, bottom
, top
);
55 for (p
= bottom
& ~31; p
< top
; ) {
56 printk("%04lx: ", p
& 0xffff);
58 for (i
= 0; i
< 8; i
++, p
+= 4) {
61 if (p
< bottom
|| p
>= top
)
64 if (__get_user(val
, (unsigned int __user
*)p
)) {
75 DEFINE_SPINLOCK(die_lock
);
77 void die(const char * str
, struct pt_regs
* regs
, long err
)
79 static int die_counter
;
82 spin_lock_irq(&die_lock
);
85 printk("%s: %04lx [#%d]\n", str
, err
& 0xffff, ++die_counter
);
87 CHK_REMOTE_DEBUG(regs
);
91 printk("Process: %s (pid: %d, stack limit = %p)\n",
92 current
->comm
, current
->pid
, task_stack_page(current
) + 1);
94 if (!user_mode(regs
) || in_interrupt())
95 dump_mem("Stack: ", regs
->regs
[15], THREAD_SIZE
+
96 (unsigned long)task_stack_page(current
));
99 spin_unlock_irq(&die_lock
);
103 static inline void die_if_kernel(const char *str
, struct pt_regs
*regs
,
106 if (!user_mode(regs
))
110 static int handle_unaligned_notify_count
= 10;
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 * handle an instruction that does an unaligned memory access by emulating the
136 * - note that PC _may not_ point to the faulting instruction
137 * (if that instruction is in a branch delay slot)
138 * - return 0 if emulation okay, -EFAULT on existential error
140 static int handle_unaligned_ins(u16 instruction
, struct pt_regs
*regs
)
142 int ret
, index
, count
;
143 unsigned long *rm
, *rn
;
144 unsigned char *src
, *dst
;
146 index
= (instruction
>>8)&15; /* 0x0F00 */
147 rn
= ®s
->regs
[index
];
149 index
= (instruction
>>4)&15; /* 0x00F0 */
150 rm
= ®s
->regs
[index
];
152 count
= 1<<(instruction
&3);
155 switch (instruction
>>12) {
156 case 0: /* mov.[bwl] to/from memory via r0+rn */
157 if (instruction
& 8) {
159 src
= (unsigned char*) *rm
;
160 src
+= regs
->regs
[0];
161 dst
= (unsigned char*) rn
;
162 *(unsigned long*)dst
= 0;
164 #ifdef __LITTLE_ENDIAN__
165 if (copy_from_user(dst
, src
, count
))
168 if ((count
== 2) && dst
[1] & 0x80) {
175 if (__copy_user(dst
, src
, count
))
178 if ((count
== 2) && dst
[2] & 0x80) {
185 src
= (unsigned char*) rm
;
186 #if !defined(__LITTLE_ENDIAN__)
189 dst
= (unsigned char*) *rn
;
190 dst
+= regs
->regs
[0];
192 if (copy_to_user(dst
, src
, count
))
198 case 1: /* mov.l Rm,@(disp,Rn) */
199 src
= (unsigned char*) rm
;
200 dst
= (unsigned char*) *rn
;
201 dst
+= (instruction
&0x000F)<<2;
203 if (copy_to_user(dst
,src
,4))
208 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
211 src
= (unsigned char*) rm
;
212 dst
= (unsigned char*) *rn
;
213 #if !defined(__LITTLE_ENDIAN__)
216 if (copy_to_user(dst
, src
, count
))
221 case 5: /* mov.l @(disp,Rm),Rn */
222 src
= (unsigned char*) *rm
;
223 src
+= (instruction
&0x000F)<<2;
224 dst
= (unsigned char*) rn
;
225 *(unsigned long*)dst
= 0;
227 if (copy_from_user(dst
,src
,4))
232 case 6: /* mov.[bwl] from memory, possibly with post-increment */
233 src
= (unsigned char*) *rm
;
236 dst
= (unsigned char*) rn
;
237 *(unsigned long*)dst
= 0;
239 #ifdef __LITTLE_ENDIAN__
240 if (copy_from_user(dst
, src
, count
))
243 if ((count
== 2) && dst
[1] & 0x80) {
250 if (copy_from_user(dst
, src
, count
))
253 if ((count
== 2) && dst
[2] & 0x80) {
262 switch ((instruction
&0xFF00)>>8) {
263 case 0x81: /* mov.w R0,@(disp,Rn) */
264 src
= (unsigned char*) ®s
->regs
[0];
265 #if !defined(__LITTLE_ENDIAN__)
268 dst
= (unsigned char*) *rm
; /* called Rn in the spec */
269 dst
+= (instruction
&0x000F)<<1;
271 if (copy_to_user(dst
, src
, 2))
276 case 0x85: /* mov.w @(disp,Rm),R0 */
277 src
= (unsigned char*) *rm
;
278 src
+= (instruction
&0x000F)<<1;
279 dst
= (unsigned char*) ®s
->regs
[0];
280 *(unsigned long*)dst
= 0;
282 #if !defined(__LITTLE_ENDIAN__)
286 if (copy_from_user(dst
, src
, 2))
289 #ifdef __LITTLE_ENDIAN__
308 /* Argh. Address not only misaligned but also non-existent.
309 * Raise an EFAULT and see if it's trapped
311 return die_if_no_fixup("Fault in unaligned fixup", regs
, 0);
315 * emulate the instruction in the delay slot
316 * - fetches the instruction from PC+2
318 static inline int handle_unaligned_delayslot(struct pt_regs
*regs
)
322 if (copy_from_user(&instruction
, (u16
*)(regs
->pc
+2), 2)) {
323 /* the instruction-fetch faulted */
328 die("delay-slot-insn faulting in handle_unaligned_delayslot", regs
, 0);
331 return handle_unaligned_ins(instruction
,regs
);
335 * handle an instruction that does an unaligned memory access
336 * - have to be careful of branch delay-slot instructions that fault
338 * - if the branch would be taken PC points to the branch
339 * - if the branch would not be taken, PC points to delay-slot
341 * - PC always points to delayed branch
342 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
345 /* Macros to determine offset from current PC for branch instructions */
346 /* Explicit type coercion is used to force sign extension where needed */
347 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
348 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
350 static int handle_unaligned_access(u16 instruction
, struct pt_regs
*regs
)
355 index
= (instruction
>>8)&15; /* 0x0F00 */
356 rm
= regs
->regs
[index
];
358 /* shout about the first ten userspace fixups */
359 if (user_mode(regs
) && handle_unaligned_notify_count
>0) {
360 handle_unaligned_notify_count
--;
362 printk("Fixing up unaligned userspace access in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
363 current
->comm
,current
->pid
,(u16
*)regs
->pc
,instruction
);
367 switch (instruction
&0xF000) {
369 if (instruction
==0x000B) {
371 ret
= handle_unaligned_delayslot(regs
);
375 else if ((instruction
&0x00FF)==0x0023) {
377 ret
= handle_unaligned_delayslot(regs
);
381 else if ((instruction
&0x00FF)==0x0003) {
383 ret
= handle_unaligned_delayslot(regs
);
385 regs
->pr
= regs
->pc
+ 4;
390 /* mov.[bwl] to/from memory via r0+rn */
395 case 0x1000: /* mov.l Rm,@(disp,Rn) */
398 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
402 if ((instruction
&0x00FF)==0x002B) {
404 ret
= handle_unaligned_delayslot(regs
);
408 else if ((instruction
&0x00FF)==0x000B) {
410 ret
= handle_unaligned_delayslot(regs
);
412 regs
->pr
= regs
->pc
+ 4;
417 /* mov.[bwl] to/from memory via r0+rn */
422 case 0x5000: /* mov.l @(disp,Rm),Rn */
425 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
428 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
429 switch (instruction
&0x0F00) {
430 case 0x0100: /* mov.w R0,@(disp,Rm) */
432 case 0x0500: /* mov.w @(disp,Rm),R0 */
434 case 0x0B00: /* bf lab - no delayslot*/
436 case 0x0F00: /* bf/s lab */
437 ret
= handle_unaligned_delayslot(regs
);
439 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
440 if ((regs
->sr
& 0x00000001) != 0)
441 regs
->pc
+= 4; /* next after slot */
444 regs
->pc
+= SH_PC_8BIT_OFFSET(instruction
);
447 case 0x0900: /* bt lab - no delayslot */
449 case 0x0D00: /* bt/s lab */
450 ret
= handle_unaligned_delayslot(regs
);
452 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
453 if ((regs
->sr
& 0x00000001) == 0)
454 regs
->pc
+= 4; /* next after slot */
457 regs
->pc
+= SH_PC_8BIT_OFFSET(instruction
);
463 case 0xA000: /* bra label */
464 ret
= handle_unaligned_delayslot(regs
);
466 regs
->pc
+= SH_PC_12BIT_OFFSET(instruction
);
469 case 0xB000: /* bsr label */
470 ret
= handle_unaligned_delayslot(regs
);
472 regs
->pr
= regs
->pc
+ 4;
473 regs
->pc
+= SH_PC_12BIT_OFFSET(instruction
);
479 /* handle non-delay-slot instruction */
481 ret
= handle_unaligned_ins(instruction
,regs
);
487 #ifdef CONFIG_CPU_HAS_SR_RB
488 #define lookup_exception_vector(x) \
489 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
491 #define lookup_exception_vector(x) \
492 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
496 * Handle various address error exceptions
498 asmlinkage
void do_address_error(struct pt_regs
*regs
,
499 unsigned long writeaccess
,
500 unsigned long address
)
502 unsigned long error_code
= 0;
507 /* Intentional ifdef */
508 #ifdef CONFIG_CPU_HAS_SR_RB
509 lookup_exception_vector(error_code
);
514 if (user_mode(regs
)) {
516 current
->thread
.error_code
= error_code
;
517 #ifdef CONFIG_CPU_SH2
519 * On the SH-2, we only have a single vector for address
520 * errors, there's no differentiating between a load error
523 current
->thread
.trap_no
= 9;
525 current
->thread
.trap_no
= (writeaccess
) ? 8 : 7;
528 /* bad PC is not something we can fix */
532 #ifndef CONFIG_CPU_SH2A
534 if (copy_from_user(&instruction
, (u16
*)(regs
->pc
), 2)) {
535 /* Argh. Fault on the instruction itself.
536 This should never happen non-SMP
542 tmp
= handle_unaligned_access(instruction
, regs
);
550 printk(KERN_NOTICE
"Killing process \"%s\" due to unaligned access\n", current
->comm
);
551 force_sig(SIGSEGV
, current
);
554 die("unaligned program counter", regs
, error_code
);
556 #ifndef CONFIG_CPU_SH2A
558 if (copy_from_user(&instruction
, (u16
*)(regs
->pc
), 2)) {
559 /* Argh. Fault on the instruction itself.
560 This should never happen non-SMP
563 die("insn faulting in do_address_error", regs
, 0);
566 handle_unaligned_access(instruction
, regs
);
569 printk(KERN_NOTICE
"Killing process \"%s\" due to unaligned access\n", current
->comm
);
570 force_sig(SIGSEGV
, current
);
577 * SH-DSP support gerg@snapgear.com.
579 int is_dsp_inst(struct pt_regs
*regs
)
584 * Safe guard if DSP mode is already enabled or we're lacking
585 * the DSP altogether.
587 if (!(cpu_data
->flags
& CPU_HAS_DSP
) || (regs
->sr
& SR_DSP
))
590 get_user(inst
, ((unsigned short *) regs
->pc
));
594 /* Check for any type of DSP or support instruction */
595 if ((inst
== 0xf000) || (inst
== 0x4000))
601 #define is_dsp_inst(regs) (0)
602 #endif /* CONFIG_SH_DSP */
604 #ifdef CONFIG_CPU_SH2A
605 asmlinkage
void do_divide_error(unsigned long r4
, unsigned long r5
,
606 unsigned long r6
, unsigned long r7
,
611 current
->thread
.trap_no
= r4
;
612 current
->thread
.error_code
= 0;
615 case TRAP_DIVZERO_ERROR
:
616 info
.si_code
= FPE_INTDIV
;
618 case TRAP_DIVOVF_ERROR
:
619 info
.si_code
= FPE_INTOVF
;
623 force_sig_info(SIGFPE
, &info
, current
);
627 /* arch/sh/kernel/cpu/sh4/fpu.c */
628 extern int do_fpu_inst(unsigned short, struct pt_regs
*);
629 extern asmlinkage
void do_fpu_state_restore(unsigned long r4
, unsigned long r5
,
630 unsigned long r6
, unsigned long r7
, struct pt_regs regs
);
632 asmlinkage
void do_reserved_inst(unsigned long r4
, unsigned long r5
,
633 unsigned long r6
, unsigned long r7
,
636 unsigned long error_code
;
637 struct task_struct
*tsk
= current
;
639 #ifdef CONFIG_SH_FPU_EMU
640 unsigned short inst
= 0;
643 get_user(inst
, (unsigned short*)regs
.pc
);
645 err
= do_fpu_inst(inst
, ®s
);
650 /* not a FPU inst. */
654 /* Check if it's a DSP instruction */
655 if (is_dsp_inst(®s
)) {
656 /* Enable DSP mode, and restart instruction. */
662 lookup_exception_vector(error_code
);
665 tsk
->thread
.error_code
= error_code
;
666 tsk
->thread
.trap_no
= TRAP_RESERVED_INST
;
667 CHK_REMOTE_DEBUG(®s
);
668 force_sig(SIGILL
, tsk
);
669 die_if_no_fixup("reserved instruction", ®s
, error_code
);
672 #ifdef CONFIG_SH_FPU_EMU
673 static int emulate_branch(unsigned short inst
, struct pt_regs
* regs
)
676 * bfs: 8fxx: PC+=d*2+4;
677 * bts: 8dxx: PC+=d*2+4;
678 * bra: axxx: PC+=D*2+4;
679 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
680 * braf:0x23: PC+=Rn*2+4;
681 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
683 * jsr: 4x0b: PC=Rn after PR=PC+4;
686 if ((inst
& 0xfd00) == 0x8d00) {
687 regs
->pc
+= SH_PC_8BIT_OFFSET(inst
);
691 if ((inst
& 0xe000) == 0xa000) {
692 regs
->pc
+= SH_PC_12BIT_OFFSET(inst
);
696 if ((inst
& 0xf0df) == 0x0003) {
697 regs
->pc
+= regs
->regs
[(inst
& 0x0f00) >> 8] + 4;
701 if ((inst
& 0xf0df) == 0x400b) {
702 regs
->pc
= regs
->regs
[(inst
& 0x0f00) >> 8];
706 if ((inst
& 0xffff) == 0x000b) {
715 asmlinkage
void do_illegal_slot_inst(unsigned long r4
, unsigned long r5
,
716 unsigned long r6
, unsigned long r7
,
719 unsigned long error_code
;
720 struct task_struct
*tsk
= current
;
721 #ifdef CONFIG_SH_FPU_EMU
722 unsigned short inst
= 0;
724 get_user(inst
, (unsigned short *)regs
.pc
+ 1);
725 if (!do_fpu_inst(inst
, ®s
)) {
726 get_user(inst
, (unsigned short *)regs
.pc
);
727 if (!emulate_branch(inst
, ®s
))
729 /* fault in branch.*/
731 /* not a FPU inst. */
734 lookup_exception_vector(error_code
);
737 tsk
->thread
.error_code
= error_code
;
738 tsk
->thread
.trap_no
= TRAP_RESERVED_INST
;
739 CHK_REMOTE_DEBUG(®s
);
740 force_sig(SIGILL
, tsk
);
741 die_if_no_fixup("illegal slot instruction", ®s
, error_code
);
744 asmlinkage
void do_exception_error(unsigned long r4
, unsigned long r5
,
745 unsigned long r6
, unsigned long r7
,
750 lookup_exception_vector(ex
);
751 die_if_kernel("exception", ®s
, ex
);
754 #if defined(CONFIG_SH_STANDARD_BIOS)
755 void *gdb_vbr_vector
;
757 static inline void __init
gdb_vbr_init(void)
759 register unsigned long vbr
;
762 * Read the old value of the VBR register to initialise
763 * the vector through which debug and BIOS traps are
764 * delegated by the Linux trap handler.
766 asm volatile("stc vbr, %0" : "=r" (vbr
));
768 gdb_vbr_vector
= (void *)(vbr
+ 0x100);
769 printk("Setting GDB trap vector to 0x%08lx\n",
770 (unsigned long)gdb_vbr_vector
);
774 void __init
per_cpu_trap_init(void)
776 extern void *vbr_base
;
778 #ifdef CONFIG_SH_STANDARD_BIOS
782 /* NOTE: The VBR value should be at P1
783 (or P2, virtural "fixed" address space).
784 It's definitely should not in physical address. */
786 asm volatile("ldc %0, vbr"
792 void *set_exception_table_vec(unsigned int vec
, void *handler
)
794 extern void *exception_handling_table
[];
797 old_handler
= exception_handling_table
[vec
];
798 exception_handling_table
[vec
] = handler
;
802 extern asmlinkage
void address_error_handler(unsigned long r4
, unsigned long r5
,
803 unsigned long r6
, unsigned long r7
,
804 struct pt_regs regs
);
806 void __init
trap_init(void)
808 set_exception_table_vec(TRAP_RESERVED_INST
, do_reserved_inst
);
809 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST
, do_illegal_slot_inst
);
811 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
812 defined(CONFIG_SH_FPU_EMU)
814 * For SH-4 lacking an FPU, treat floating point instructions as
815 * reserved. They'll be handled in the math-emu case, or faulted on
818 set_exception_table_evt(0x800, do_reserved_inst
);
819 set_exception_table_evt(0x820, do_illegal_slot_inst
);
820 #elif defined(CONFIG_SH_FPU)
821 set_exception_table_evt(0x800, do_fpu_state_restore
);
822 set_exception_table_evt(0x820, do_fpu_state_restore
);
825 #ifdef CONFIG_CPU_SH2
826 set_exception_table_vec(TRAP_ADDRESS_ERROR
, address_error_handler
);
828 #ifdef CONFIG_CPU_SH2A
829 set_exception_table_vec(TRAP_DIVZERO_ERROR
, do_divide_error
);
830 set_exception_table_vec(TRAP_DIVOVF_ERROR
, do_divide_error
);
833 /* Setup VBR for boot cpu */
837 void show_trace(struct task_struct
*tsk
, unsigned long *sp
,
838 struct pt_regs
*regs
)
842 if (regs
&& user_mode(regs
))
845 printk("\nCall trace: ");
846 #ifdef CONFIG_KALLSYMS
850 while (!kstack_end(sp
)) {
852 if (kernel_text_address(addr
))
859 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
866 sp
= (unsigned long *)current_stack_pointer
;
868 sp
= (unsigned long *)tsk
->thread
.sp
;
870 stack
= (unsigned long)sp
;
871 dump_mem("Stack: ", stack
, THREAD_SIZE
+
872 (unsigned long)task_stack_page(tsk
));
873 show_trace(tsk
, sp
, NULL
);
876 void dump_stack(void)
878 show_stack(NULL
, NULL
);
880 EXPORT_SYMBOL(dump_stack
);