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/limits.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
30 #define CHK_REMOTE_DEBUG(regs) \
32 if (kgdb_debug_hook && !user_mode(regs))\
33 (*kgdb_debug_hook)(regs); \
36 #define CHK_REMOTE_DEBUG(regs)
40 # define TRAP_RESERVED_INST 4
41 # define TRAP_ILLEGAL_SLOT_INST 6
42 # define TRAP_ADDRESS_ERROR 9
43 # ifdef CONFIG_CPU_SH2A
44 # define TRAP_DIVZERO_ERROR 17
45 # define TRAP_DIVOVF_ERROR 18
48 #define TRAP_RESERVED_INST 12
49 #define TRAP_ILLEGAL_SLOT_INST 13
52 static void dump_mem(const char *str
, unsigned long bottom
, unsigned long top
)
57 printk("%s(0x%08lx to 0x%08lx)\n", str
, bottom
, top
);
59 for (p
= bottom
& ~31; p
< top
; ) {
60 printk("%04lx: ", p
& 0xffff);
62 for (i
= 0; i
< 8; i
++, p
+= 4) {
65 if (p
< bottom
|| p
>= top
)
68 if (__get_user(val
, (unsigned int __user
*)p
)) {
79 static DEFINE_SPINLOCK(die_lock
);
81 void die(const char * str
, struct pt_regs
* regs
, long err
)
83 static int die_counter
;
86 spin_lock_irq(&die_lock
);
89 printk("%s: %04lx [#%d]\n", str
, err
& 0xffff, ++die_counter
);
91 CHK_REMOTE_DEBUG(regs
);
95 printk("Process: %s (pid: %d, stack limit = %p)\n",
96 current
->comm
, current
->pid
, task_stack_page(current
) + 1);
98 if (!user_mode(regs
) || in_interrupt())
99 dump_mem("Stack: ", regs
->regs
[15], THREAD_SIZE
+
100 (unsigned long)task_stack_page(current
));
103 spin_unlock_irq(&die_lock
);
107 static inline void die_if_kernel(const char *str
, struct pt_regs
*regs
,
110 if (!user_mode(regs
))
115 * try and fix up kernelspace address errors
116 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
117 * - kernel/userspace interfaces cause a jump to an appropriate handler
118 * - other kernel errors are bad
119 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
121 static int die_if_no_fixup(const char * str
, struct pt_regs
* regs
, long err
)
123 if (!user_mode(regs
)) {
124 const struct exception_table_entry
*fixup
;
125 fixup
= search_exception_tables(regs
->pc
);
127 regs
->pc
= fixup
->fixup
;
136 * handle an instruction that does an unaligned memory access by emulating the
138 * - note that PC _may not_ point to the faulting instruction
139 * (if that instruction is in a branch delay slot)
140 * - return 0 if emulation okay, -EFAULT on existential error
142 static int handle_unaligned_ins(u16 instruction
, struct pt_regs
*regs
)
144 int ret
, index
, count
;
145 unsigned long *rm
, *rn
;
146 unsigned char *src
, *dst
;
148 index
= (instruction
>>8)&15; /* 0x0F00 */
149 rn
= ®s
->regs
[index
];
151 index
= (instruction
>>4)&15; /* 0x00F0 */
152 rm
= ®s
->regs
[index
];
154 count
= 1<<(instruction
&3);
157 switch (instruction
>>12) {
158 case 0: /* mov.[bwl] to/from memory via r0+rn */
159 if (instruction
& 8) {
161 src
= (unsigned char*) *rm
;
162 src
+= regs
->regs
[0];
163 dst
= (unsigned char*) rn
;
164 *(unsigned long*)dst
= 0;
166 #ifdef __LITTLE_ENDIAN__
167 if (copy_from_user(dst
, src
, count
))
170 if ((count
== 2) && dst
[1] & 0x80) {
177 if (__copy_user(dst
, src
, count
))
180 if ((count
== 2) && dst
[2] & 0x80) {
187 src
= (unsigned char*) rm
;
188 #if !defined(__LITTLE_ENDIAN__)
191 dst
= (unsigned char*) *rn
;
192 dst
+= regs
->regs
[0];
194 if (copy_to_user(dst
, src
, count
))
200 case 1: /* mov.l Rm,@(disp,Rn) */
201 src
= (unsigned char*) rm
;
202 dst
= (unsigned char*) *rn
;
203 dst
+= (instruction
&0x000F)<<2;
205 if (copy_to_user(dst
,src
,4))
210 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
213 src
= (unsigned char*) rm
;
214 dst
= (unsigned char*) *rn
;
215 #if !defined(__LITTLE_ENDIAN__)
218 if (copy_to_user(dst
, src
, count
))
223 case 5: /* mov.l @(disp,Rm),Rn */
224 src
= (unsigned char*) *rm
;
225 src
+= (instruction
&0x000F)<<2;
226 dst
= (unsigned char*) rn
;
227 *(unsigned long*)dst
= 0;
229 if (copy_from_user(dst
,src
,4))
234 case 6: /* mov.[bwl] from memory, possibly with post-increment */
235 src
= (unsigned char*) *rm
;
238 dst
= (unsigned char*) rn
;
239 *(unsigned long*)dst
= 0;
241 #ifdef __LITTLE_ENDIAN__
242 if (copy_from_user(dst
, src
, count
))
245 if ((count
== 2) && dst
[1] & 0x80) {
252 if (copy_from_user(dst
, src
, count
))
255 if ((count
== 2) && dst
[2] & 0x80) {
264 switch ((instruction
&0xFF00)>>8) {
265 case 0x81: /* mov.w R0,@(disp,Rn) */
266 src
= (unsigned char*) ®s
->regs
[0];
267 #if !defined(__LITTLE_ENDIAN__)
270 dst
= (unsigned char*) *rm
; /* called Rn in the spec */
271 dst
+= (instruction
&0x000F)<<1;
273 if (copy_to_user(dst
, src
, 2))
278 case 0x85: /* mov.w @(disp,Rm),R0 */
279 src
= (unsigned char*) *rm
;
280 src
+= (instruction
&0x000F)<<1;
281 dst
= (unsigned char*) ®s
->regs
[0];
282 *(unsigned long*)dst
= 0;
284 #if !defined(__LITTLE_ENDIAN__)
288 if (copy_from_user(dst
, src
, 2))
291 #ifdef __LITTLE_ENDIAN__
310 /* Argh. Address not only misaligned but also non-existent.
311 * Raise an EFAULT and see if it's trapped
313 return die_if_no_fixup("Fault in unaligned fixup", regs
, 0);
317 * emulate the instruction in the delay slot
318 * - fetches the instruction from PC+2
320 static inline int handle_unaligned_delayslot(struct pt_regs
*regs
)
324 if (copy_from_user(&instruction
, (u16
*)(regs
->pc
+2), 2)) {
325 /* the instruction-fetch faulted */
330 die("delay-slot-insn faulting in handle_unaligned_delayslot",
334 return handle_unaligned_ins(instruction
,regs
);
338 * handle an instruction that does an unaligned memory access
339 * - have to be careful of branch delay-slot instructions that fault
341 * - if the branch would be taken PC points to the branch
342 * - if the branch would not be taken, PC points to delay-slot
344 * - PC always points to delayed branch
345 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
348 /* Macros to determine offset from current PC for branch instructions */
349 /* Explicit type coercion is used to force sign extension where needed */
350 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
351 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
354 * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
357 #ifndef CONFIG_CPU_SH2A
358 static int handle_unaligned_notify_count
= 10;
360 static int handle_unaligned_access(u16 instruction
, struct pt_regs
*regs
)
365 index
= (instruction
>>8)&15; /* 0x0F00 */
366 rm
= regs
->regs
[index
];
368 /* shout about the first ten userspace fixups */
369 if (user_mode(regs
) && handle_unaligned_notify_count
>0) {
370 handle_unaligned_notify_count
--;
372 printk(KERN_NOTICE
"Fixing up unaligned userspace access "
373 "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
374 current
->comm
,current
->pid
,(u16
*)regs
->pc
,instruction
);
378 switch (instruction
&0xF000) {
380 if (instruction
==0x000B) {
382 ret
= handle_unaligned_delayslot(regs
);
386 else if ((instruction
&0x00FF)==0x0023) {
388 ret
= handle_unaligned_delayslot(regs
);
392 else if ((instruction
&0x00FF)==0x0003) {
394 ret
= handle_unaligned_delayslot(regs
);
396 regs
->pr
= regs
->pc
+ 4;
401 /* mov.[bwl] to/from memory via r0+rn */
406 case 0x1000: /* mov.l Rm,@(disp,Rn) */
409 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
413 if ((instruction
&0x00FF)==0x002B) {
415 ret
= handle_unaligned_delayslot(regs
);
419 else if ((instruction
&0x00FF)==0x000B) {
421 ret
= handle_unaligned_delayslot(regs
);
423 regs
->pr
= regs
->pc
+ 4;
428 /* mov.[bwl] to/from memory via r0+rn */
433 case 0x5000: /* mov.l @(disp,Rm),Rn */
436 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
439 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
440 switch (instruction
&0x0F00) {
441 case 0x0100: /* mov.w R0,@(disp,Rm) */
443 case 0x0500: /* mov.w @(disp,Rm),R0 */
445 case 0x0B00: /* bf lab - no delayslot*/
447 case 0x0F00: /* bf/s lab */
448 ret
= handle_unaligned_delayslot(regs
);
450 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
451 if ((regs
->sr
& 0x00000001) != 0)
452 regs
->pc
+= 4; /* next after slot */
455 regs
->pc
+= SH_PC_8BIT_OFFSET(instruction
);
458 case 0x0900: /* bt lab - no delayslot */
460 case 0x0D00: /* bt/s lab */
461 ret
= handle_unaligned_delayslot(regs
);
463 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
464 if ((regs
->sr
& 0x00000001) == 0)
465 regs
->pc
+= 4; /* next after slot */
468 regs
->pc
+= SH_PC_8BIT_OFFSET(instruction
);
474 case 0xA000: /* bra label */
475 ret
= handle_unaligned_delayslot(regs
);
477 regs
->pc
+= SH_PC_12BIT_OFFSET(instruction
);
480 case 0xB000: /* bsr label */
481 ret
= handle_unaligned_delayslot(regs
);
483 regs
->pr
= regs
->pc
+ 4;
484 regs
->pc
+= SH_PC_12BIT_OFFSET(instruction
);
490 /* handle non-delay-slot instruction */
492 ret
= handle_unaligned_ins(instruction
,regs
);
494 regs
->pc
+= instruction_size(instruction
);
497 #endif /* CONFIG_CPU_SH2A */
499 #ifdef CONFIG_CPU_HAS_SR_RB
500 #define lookup_exception_vector(x) \
501 __asm__ __volatile__ ("stc r2_bank, %0\n\t" : "=r" ((x)))
503 #define lookup_exception_vector(x) \
504 __asm__ __volatile__ ("mov r4, %0\n\t" : "=r" ((x)))
508 * Handle various address error exceptions:
509 * - instruction address error:
511 * PC >= 0x80000000 in user mode
512 * - data address error (read and write)
513 * misaligned data access
514 * access to >= 0x80000000 is user mode
515 * Unfortuntaly we can't distinguish between instruction address error
516 * and data address errors caused by read acceses.
518 asmlinkage
void do_address_error(struct pt_regs
*regs
,
519 unsigned long writeaccess
,
520 unsigned long address
)
522 unsigned long error_code
= 0;
525 #ifndef CONFIG_CPU_SH2A
530 /* Intentional ifdef */
531 #ifdef CONFIG_CPU_HAS_SR_RB
532 lookup_exception_vector(error_code
);
537 if (user_mode(regs
)) {
538 int si_code
= BUS_ADRERR
;
542 /* bad PC is not something we can fix */
544 si_code
= BUS_ADRALN
;
548 #ifndef CONFIG_CPU_SH2A
550 if (copy_from_user(&instruction
, (u16
*)(regs
->pc
), 2)) {
551 /* Argh. Fault on the instruction itself.
552 This should never happen non-SMP
558 tmp
= handle_unaligned_access(instruction
, regs
);
566 printk(KERN_NOTICE
"Sending SIGBUS to \"%s\" due to unaligned "
567 "access (PC %lx PR %lx)\n", current
->comm
, regs
->pc
,
570 info
.si_signo
= SIGBUS
;
572 info
.si_code
= si_code
;
573 info
.si_addr
= (void *) address
;
574 force_sig_info(SIGBUS
, &info
, current
);
577 die("unaligned program counter", regs
, error_code
);
579 #ifndef CONFIG_CPU_SH2A
581 if (copy_from_user(&instruction
, (u16
*)(regs
->pc
), 2)) {
582 /* Argh. Fault on the instruction itself.
583 This should never happen non-SMP
586 die("insn faulting in do_address_error", regs
, 0);
589 handle_unaligned_access(instruction
, regs
);
592 printk(KERN_NOTICE
"Killing process \"%s\" due to unaligned "
593 "access\n", current
->comm
);
595 force_sig(SIGSEGV
, current
);
602 * SH-DSP support gerg@snapgear.com.
604 int is_dsp_inst(struct pt_regs
*regs
)
609 * Safe guard if DSP mode is already enabled or we're lacking
610 * the DSP altogether.
612 if (!(current_cpu_data
.flags
& CPU_HAS_DSP
) || (regs
->sr
& SR_DSP
))
615 get_user(inst
, ((unsigned short *) regs
->pc
));
619 /* Check for any type of DSP or support instruction */
620 if ((inst
== 0xf000) || (inst
== 0x4000))
626 #define is_dsp_inst(regs) (0)
627 #endif /* CONFIG_SH_DSP */
629 #ifdef CONFIG_CPU_SH2A
630 asmlinkage
void do_divide_error(unsigned long r4
, unsigned long r5
,
631 unsigned long r6
, unsigned long r7
,
632 struct pt_regs __regs
)
634 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
638 case TRAP_DIVZERO_ERROR
:
639 info
.si_code
= FPE_INTDIV
;
641 case TRAP_DIVOVF_ERROR
:
642 info
.si_code
= FPE_INTOVF
;
646 force_sig_info(SIGFPE
, &info
, current
);
650 /* arch/sh/kernel/cpu/sh4/fpu.c */
651 extern int do_fpu_inst(unsigned short, struct pt_regs
*);
652 extern asmlinkage
void do_fpu_state_restore(unsigned long r4
, unsigned long r5
,
653 unsigned long r6
, unsigned long r7
, struct pt_regs __regs
);
655 asmlinkage
void do_reserved_inst(unsigned long r4
, unsigned long r5
,
656 unsigned long r6
, unsigned long r7
,
657 struct pt_regs __regs
)
659 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
660 unsigned long error_code
;
661 struct task_struct
*tsk
= current
;
663 #ifdef CONFIG_SH_FPU_EMU
664 unsigned short inst
= 0;
667 get_user(inst
, (unsigned short*)regs
->pc
);
669 err
= do_fpu_inst(inst
, regs
);
671 regs
->pc
+= instruction_size(inst
);
674 /* not a FPU inst. */
678 /* Check if it's a DSP instruction */
679 if (is_dsp_inst(regs
)) {
680 /* Enable DSP mode, and restart instruction. */
686 lookup_exception_vector(error_code
);
689 CHK_REMOTE_DEBUG(regs
);
690 force_sig(SIGILL
, tsk
);
691 die_if_no_fixup("reserved instruction", regs
, error_code
);
694 #ifdef CONFIG_SH_FPU_EMU
695 static int emulate_branch(unsigned short inst
, struct pt_regs
* regs
)
698 * bfs: 8fxx: PC+=d*2+4;
699 * bts: 8dxx: PC+=d*2+4;
700 * bra: axxx: PC+=D*2+4;
701 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
702 * braf:0x23: PC+=Rn*2+4;
703 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
705 * jsr: 4x0b: PC=Rn after PR=PC+4;
708 if ((inst
& 0xfd00) == 0x8d00) {
709 regs
->pc
+= SH_PC_8BIT_OFFSET(inst
);
713 if ((inst
& 0xe000) == 0xa000) {
714 regs
->pc
+= SH_PC_12BIT_OFFSET(inst
);
718 if ((inst
& 0xf0df) == 0x0003) {
719 regs
->pc
+= regs
->regs
[(inst
& 0x0f00) >> 8] + 4;
723 if ((inst
& 0xf0df) == 0x400b) {
724 regs
->pc
= regs
->regs
[(inst
& 0x0f00) >> 8];
728 if ((inst
& 0xffff) == 0x000b) {
737 asmlinkage
void do_illegal_slot_inst(unsigned long r4
, unsigned long r5
,
738 unsigned long r6
, unsigned long r7
,
739 struct pt_regs __regs
)
741 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
742 unsigned long error_code
;
743 struct task_struct
*tsk
= current
;
744 #ifdef CONFIG_SH_FPU_EMU
745 unsigned short inst
= 0;
747 get_user(inst
, (unsigned short *)regs
->pc
+ 1);
748 if (!do_fpu_inst(inst
, regs
)) {
749 get_user(inst
, (unsigned short *)regs
->pc
);
750 if (!emulate_branch(inst
, regs
))
752 /* fault in branch.*/
754 /* not a FPU inst. */
757 lookup_exception_vector(error_code
);
760 CHK_REMOTE_DEBUG(regs
);
761 force_sig(SIGILL
, tsk
);
762 die_if_no_fixup("illegal slot instruction", regs
, error_code
);
765 asmlinkage
void do_exception_error(unsigned long r4
, unsigned long r5
,
766 unsigned long r6
, unsigned long r7
,
767 struct pt_regs __regs
)
769 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
772 lookup_exception_vector(ex
);
773 die_if_kernel("exception", regs
, ex
);
776 #if defined(CONFIG_SH_STANDARD_BIOS)
777 void *gdb_vbr_vector
;
779 static inline void __init
gdb_vbr_init(void)
781 register unsigned long vbr
;
784 * Read the old value of the VBR register to initialise
785 * the vector through which debug and BIOS traps are
786 * delegated by the Linux trap handler.
788 asm volatile("stc vbr, %0" : "=r" (vbr
));
790 gdb_vbr_vector
= (void *)(vbr
+ 0x100);
791 printk("Setting GDB trap vector to 0x%08lx\n",
792 (unsigned long)gdb_vbr_vector
);
796 void __init
per_cpu_trap_init(void)
798 extern void *vbr_base
;
800 #ifdef CONFIG_SH_STANDARD_BIOS
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 extern asmlinkage
void address_error_handler(unsigned long r4
, unsigned long r5
,
825 unsigned long r6
, unsigned long r7
,
826 struct pt_regs __regs
);
828 void __init
trap_init(void)
830 set_exception_table_vec(TRAP_RESERVED_INST
, do_reserved_inst
);
831 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST
, do_illegal_slot_inst
);
833 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
834 defined(CONFIG_SH_FPU_EMU)
836 * For SH-4 lacking an FPU, treat floating point instructions as
837 * reserved. They'll be handled in the math-emu case, or faulted on
840 set_exception_table_evt(0x800, do_reserved_inst
);
841 set_exception_table_evt(0x820, do_illegal_slot_inst
);
842 #elif defined(CONFIG_SH_FPU)
843 set_exception_table_evt(0x800, do_fpu_state_restore
);
844 set_exception_table_evt(0x820, do_fpu_state_restore
);
847 #ifdef CONFIG_CPU_SH2
848 set_exception_table_vec(TRAP_ADDRESS_ERROR
, address_error_handler
);
850 #ifdef CONFIG_CPU_SH2A
851 set_exception_table_vec(TRAP_DIVZERO_ERROR
, do_divide_error
);
852 set_exception_table_vec(TRAP_DIVOVF_ERROR
, do_divide_error
);
855 /* Setup VBR for boot cpu */
860 void handle_BUG(struct pt_regs
*regs
)
862 enum bug_trap_type tt
;
863 tt
= report_bug(regs
->pc
);
864 if (tt
== BUG_TRAP_TYPE_WARN
) {
869 die("Kernel BUG", regs
, TRAPA_BUG_OPCODE
& 0xff);
872 int is_valid_bugaddr(unsigned long addr
)
874 return addr
>= PAGE_OFFSET
;
878 void show_trace(struct task_struct
*tsk
, unsigned long *sp
,
879 struct pt_regs
*regs
)
883 if (regs
&& user_mode(regs
))
886 printk("\nCall trace: ");
887 #ifdef CONFIG_KALLSYMS
891 while (!kstack_end(sp
)) {
893 if (kernel_text_address(addr
))
902 debug_show_held_locks(tsk
);
905 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
912 sp
= (unsigned long *)current_stack_pointer
;
914 sp
= (unsigned long *)tsk
->thread
.sp
;
916 stack
= (unsigned long)sp
;
917 dump_mem("Stack: ", stack
, THREAD_SIZE
+
918 (unsigned long)task_stack_page(tsk
));
919 show_trace(tsk
, sp
, NULL
);
922 void dump_stack(void)
924 show_stack(NULL
, NULL
);
926 EXPORT_SYMBOL(dump_stack
);