1 /* $Id: traps.c,v 1.17 2004/05/02 01:46:30 sugioka Exp $
3 * linux/arch/sh/traps.c
5 * SuperH version: Copyright (C) 1999 Niibe Yutaka
6 * Copyright (C) 2000 Philipp Rumpf
7 * Copyright (C) 2000 David Howells
8 * Copyright (C) 2002, 2003 Paul Mundt
12 * 'Traps.c' handles hardware traps and faults after we have saved some
15 #include <linux/config.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/timer.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/spinlock.h>
28 #include <linux/module.h>
29 #include <linux/kallsyms.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
34 #include <asm/atomic.h>
35 #include <asm/processor.h>
36 #include <asm/sections.h>
40 #define CHK_REMOTE_DEBUG(regs) \
42 if ((kgdb_debug_hook != (kgdb_debug_hook_t *) NULL) && (!user_mode(regs))) \
44 (*kgdb_debug_hook)(regs); \
48 #define CHK_REMOTE_DEBUG(regs)
51 #define DO_ERROR(trapnr, signr, str, name, tsk) \
52 asmlinkage void do_##name(unsigned long r4, unsigned long r5, \
53 unsigned long r6, unsigned long r7, \
54 struct pt_regs regs) \
56 unsigned long error_code; \
58 /* Check if it's a DSP instruction */ \
59 if (is_dsp_inst(®s)) { \
60 /* Enable DSP mode, and restart instruction. */ \
65 asm volatile("stc r2_bank, %0": "=r" (error_code)); \
67 tsk->thread.error_code = error_code; \
68 tsk->thread.trap_no = trapnr; \
69 CHK_REMOTE_DEBUG(®s); \
70 force_sig(signr, tsk); \
71 die_if_no_fixup(str,®s,error_code); \
75 #define TRAP_RESERVED_INST 4
76 #define TRAP_ILLEGAL_SLOT_INST 6
78 #define TRAP_RESERVED_INST 12
79 #define TRAP_ILLEGAL_SLOT_INST 13
83 * These constants are for searching for possible module text
84 * segments. VMALLOC_OFFSET comes from mm/vmalloc.c; MODULE_RANGE is
85 * a guess of how much space is likely to be vmalloced.
87 #define VMALLOC_OFFSET (8*1024*1024)
88 #define MODULE_RANGE (8*1024*1024)
92 void die(const char * str
, struct pt_regs
* regs
, long err
)
94 static int die_counter
;
97 spin_lock_irq(&die_lock
);
98 printk("%s: %04lx [#%d]\n", str
, err
& 0xffff, ++die_counter
);
99 CHK_REMOTE_DEBUG(regs
);
101 spin_unlock_irq(&die_lock
);
105 static inline void die_if_kernel(const char * str
, struct pt_regs
* regs
, long err
)
107 if (!user_mode(regs
))
111 static int handle_unaligned_notify_count
= 10;
114 * try and fix up kernelspace address errors
115 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
116 * - kernel/userspace interfaces cause a jump to an appropriate handler
117 * - other kernel errors are bad
118 * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
120 static int die_if_no_fixup(const char * str
, struct pt_regs
* regs
, long err
)
122 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", regs
, 0);
333 return handle_unaligned_ins(instruction
,regs
);
337 * handle an instruction that does an unaligned memory access
338 * - have to be careful of branch delay-slot instructions that fault
340 * - if the branch would be taken PC points to the branch
341 * - if the branch would not be taken, PC points to delay-slot
343 * - PC always points to delayed branch
344 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
347 /* Macros to determine offset from current PC for branch instructions */
348 /* Explicit type coercion is used to force sign extension where needed */
349 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
350 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
352 static int handle_unaligned_access(u16 instruction
, struct pt_regs
*regs
)
357 index
= (instruction
>>8)&15; /* 0x0F00 */
358 rm
= regs
->regs
[index
];
360 /* shout about the first ten userspace fixups */
361 if (user_mode(regs
) && handle_unaligned_notify_count
>0) {
362 handle_unaligned_notify_count
--;
364 printk("Fixing up unaligned userspace access in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
365 current
->comm
,current
->pid
,(u16
*)regs
->pc
,instruction
);
369 switch (instruction
&0xF000) {
371 if (instruction
==0x000B) {
373 ret
= handle_unaligned_delayslot(regs
);
377 else if ((instruction
&0x00FF)==0x0023) {
379 ret
= handle_unaligned_delayslot(regs
);
383 else if ((instruction
&0x00FF)==0x0003) {
385 ret
= handle_unaligned_delayslot(regs
);
387 regs
->pr
= regs
->pc
+ 4;
392 /* mov.[bwl] to/from memory via r0+rn */
397 case 0x1000: /* mov.l Rm,@(disp,Rn) */
400 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
404 if ((instruction
&0x00FF)==0x002B) {
406 ret
= handle_unaligned_delayslot(regs
);
410 else if ((instruction
&0x00FF)==0x000B) {
412 ret
= handle_unaligned_delayslot(regs
);
414 regs
->pr
= regs
->pc
+ 4;
419 /* mov.[bwl] to/from memory via r0+rn */
424 case 0x5000: /* mov.l @(disp,Rm),Rn */
427 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
430 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
431 switch (instruction
&0x0F00) {
432 case 0x0100: /* mov.w R0,@(disp,Rm) */
434 case 0x0500: /* mov.w @(disp,Rm),R0 */
436 case 0x0B00: /* bf lab - no delayslot*/
438 case 0x0F00: /* bf/s lab */
439 ret
= handle_unaligned_delayslot(regs
);
441 #if defined(CONFIG_CPU_SH4)
442 if ((regs
->sr
& 0x00000001) != 0)
443 regs
->pc
+= 4; /* next after slot */
446 regs
->pc
+= SH_PC_8BIT_OFFSET(instruction
);
449 case 0x0900: /* bt lab - no delayslot */
451 case 0x0D00: /* bt/s lab */
452 ret
= handle_unaligned_delayslot(regs
);
454 #if defined(CONFIG_CPU_SH4)
455 if ((regs
->sr
& 0x00000001) == 0)
456 regs
->pc
+= 4; /* next after slot */
459 regs
->pc
+= SH_PC_8BIT_OFFSET(instruction
);
465 case 0xA000: /* bra label */
466 ret
= handle_unaligned_delayslot(regs
);
468 regs
->pc
+= SH_PC_12BIT_OFFSET(instruction
);
471 case 0xB000: /* bsr label */
472 ret
= handle_unaligned_delayslot(regs
);
474 regs
->pr
= regs
->pc
+ 4;
475 regs
->pc
+= SH_PC_12BIT_OFFSET(instruction
);
481 /* handle non-delay-slot instruction */
483 ret
= handle_unaligned_ins(instruction
,regs
);
490 * Handle various address error exceptions
492 asmlinkage
void do_address_error(struct pt_regs
*regs
,
493 unsigned long writeaccess
,
494 unsigned long address
)
496 unsigned long error_code
;
501 asm volatile("stc r2_bank,%0": "=r" (error_code
));
505 if (user_mode(regs
)) {
507 current
->thread
.error_code
= error_code
;
508 current
->thread
.trap_no
= (writeaccess
) ? 8 : 7;
510 /* bad PC is not something we can fix */
515 if (copy_from_user(&instruction
, (u16
*)(regs
->pc
), 2)) {
516 /* Argh. Fault on the instruction itself.
517 This should never happen non-SMP
523 tmp
= handle_unaligned_access(instruction
, regs
);
530 printk(KERN_NOTICE
"Killing process \"%s\" due to unaligned access\n", current
->comm
);
531 force_sig(SIGSEGV
, current
);
534 die("unaligned program counter", regs
, error_code
);
537 if (copy_from_user(&instruction
, (u16
*)(regs
->pc
), 2)) {
538 /* Argh. Fault on the instruction itself.
539 This should never happen non-SMP
542 die("insn faulting in do_address_error", regs
, 0);
545 handle_unaligned_access(instruction
, regs
);
552 * SH-DSP support gerg@snapgear.com.
554 int is_dsp_inst(struct pt_regs
*regs
)
559 * Safe guard if DSP mode is already enabled or we're lacking
560 * the DSP altogether.
562 if (!(cpu_data
->flags
& CPU_HAS_DSP
) || (regs
->sr
& SR_DSP
))
565 get_user(inst
, ((unsigned short *) regs
->pc
));
569 /* Check for any type of DSP or support instruction */
570 if ((inst
== 0xf000) || (inst
== 0x4000))
576 #define is_dsp_inst(regs) (0)
577 #endif /* CONFIG_SH_DSP */
579 DO_ERROR(TRAP_RESERVED_INST
, SIGILL
, "reserved instruction", reserved_inst
, current
)
580 DO_ERROR(TRAP_ILLEGAL_SLOT_INST
, SIGILL
, "illegal slot instruction", illegal_slot_inst
, current
)
582 asmlinkage
void do_exception_error(unsigned long r4
, unsigned long r5
,
583 unsigned long r6
, unsigned long r7
,
587 asm volatile("stc r2_bank, %0" : "=r" (ex
));
588 die_if_kernel("exception", ®s
, ex
);
591 #if defined(CONFIG_SH_STANDARD_BIOS)
592 void *gdb_vbr_vector
;
594 static inline void __init
gdb_vbr_init(void)
596 register unsigned long vbr
;
599 * Read the old value of the VBR register to initialise
600 * the vector through which debug and BIOS traps are
601 * delegated by the Linux trap handler.
603 asm volatile("stc vbr, %0" : "=r" (vbr
));
605 gdb_vbr_vector
= (void *)(vbr
+ 0x100);
606 printk("Setting GDB trap vector to 0x%08lx\n",
607 (unsigned long)gdb_vbr_vector
);
611 void __init
per_cpu_trap_init(void)
613 extern void *vbr_base
;
615 #ifdef CONFIG_SH_STANDARD_BIOS
619 /* NOTE: The VBR value should be at P1
620 (or P2, virtural "fixed" address space).
621 It's definitely should not in physical address. */
623 asm volatile("ldc %0, vbr"
629 void __init
trap_init(void)
631 extern void *exception_handling_table
[];
633 exception_handling_table
[TRAP_RESERVED_INST
]
634 = (void *)do_reserved_inst
;
635 exception_handling_table
[TRAP_ILLEGAL_SLOT_INST
]
636 = (void *)do_illegal_slot_inst
;
638 #ifdef CONFIG_CPU_SH4
639 if (!(cpu_data
->flags
& CPU_HAS_FPU
)) {
640 /* For SH-4 lacking an FPU, treat floating point instructions
642 /* entry 64 corresponds to EXPEVT=0x800 */
643 exception_handling_table
[64] = (void *)do_reserved_inst
;
644 exception_handling_table
[65] = (void *)do_illegal_slot_inst
;
648 /* Setup VBR for boot cpu */
652 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
654 unsigned long *stack
, addr
;
655 unsigned long module_start
= VMALLOC_START
;
656 unsigned long module_end
= VMALLOC_END
;
660 sp
= (unsigned long *)tsk
->thread
.sp
;
664 __asm__
__volatile__ (
666 "stc r7_bank, %1\n\t"
667 : "=r" (module_start
),
671 sp
= (unsigned long *)module_start
;
676 printk("\nCall trace: ");
677 #ifdef CONFIG_KALLSYMS
681 while (!kstack_end(stack
)) {
683 if (((addr
>= (unsigned long)_text
) &&
684 (addr
<= (unsigned long)_etext
)) ||
685 ((addr
>= module_start
) && (addr
<= module_end
))) {
687 * For 80-columns display, 6 entry is maximum.
688 * NOTE: '[<8c00abcd>] ' consumes 13 columns .
690 #ifndef CONFIG_KALLSYMS
691 if (i
&& ((i
% 6) == 0))
694 printk("[<%08lx>] ", addr
);
695 print_symbol("%s\n", addr
);
703 void show_task(unsigned long *sp
)
705 show_stack(NULL
, sp
);
708 void dump_stack(void)
710 show_stack(NULL
, NULL
);
712 EXPORT_SYMBOL(dump_stack
);