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/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/timer.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/spinlock.h>
27 #include <linux/module.h>
28 #include <linux/kallsyms.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
33 #include <asm/atomic.h>
34 #include <asm/processor.h>
35 #include <asm/sections.h>
39 #define CHK_REMOTE_DEBUG(regs) \
41 if ((kgdb_debug_hook != (kgdb_debug_hook_t *) NULL) && (!user_mode(regs))) \
43 (*kgdb_debug_hook)(regs); \
47 #define CHK_REMOTE_DEBUG(regs)
50 #define DO_ERROR(trapnr, signr, str, name, tsk) \
51 asmlinkage void do_##name(unsigned long r4, unsigned long r5, \
52 unsigned long r6, unsigned long r7, \
53 struct pt_regs regs) \
55 unsigned long error_code; \
57 /* Check if it's a DSP instruction */ \
58 if (is_dsp_inst(®s)) { \
59 /* Enable DSP mode, and restart instruction. */ \
64 asm volatile("stc r2_bank, %0": "=r" (error_code)); \
66 tsk->thread.error_code = error_code; \
67 tsk->thread.trap_no = trapnr; \
68 CHK_REMOTE_DEBUG(®s); \
69 force_sig(signr, tsk); \
70 die_if_no_fixup(str,®s,error_code); \
74 #define TRAP_RESERVED_INST 4
75 #define TRAP_ILLEGAL_SLOT_INST 6
77 #define TRAP_RESERVED_INST 12
78 #define TRAP_ILLEGAL_SLOT_INST 13
82 * These constants are for searching for possible module text
83 * segments. VMALLOC_OFFSET comes from mm/vmalloc.c; MODULE_RANGE is
84 * a guess of how much space is likely to be vmalloced.
86 #define VMALLOC_OFFSET (8*1024*1024)
87 #define MODULE_RANGE (8*1024*1024)
91 void die(const char * str, struct pt_regs * regs, long err)
93 static int die_counter;
96 spin_lock_irq(&die_lock);
97 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
98 CHK_REMOTE_DEBUG(regs);
100 spin_unlock_irq(&die_lock);
104 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
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))
123 const struct exception_table_entry *fixup;
124 fixup = search_exception_tables(regs->pc);
126 regs->pc = fixup->fixup;
135 * handle an instruction that does an unaligned memory access by emulating the
137 * - note that PC _may not_ point to the faulting instruction
138 * (if that instruction is in a branch delay slot)
139 * - return 0 if emulation okay, -EFAULT on existential error
141 static int handle_unaligned_ins(u16 instruction, struct pt_regs *regs)
143 int ret, index, count;
144 unsigned long *rm, *rn;
145 unsigned char *src, *dst;
147 index = (instruction>>8)&15; /* 0x0F00 */
148 rn = ®s->regs[index];
150 index = (instruction>>4)&15; /* 0x00F0 */
151 rm = ®s->regs[index];
153 count = 1<<(instruction&3);
156 switch (instruction>>12) {
157 case 0: /* mov.[bwl] to/from memory via r0+rn */
158 if (instruction & 8) {
160 src = (unsigned char*) *rm;
161 src += regs->regs[0];
162 dst = (unsigned char*) rn;
163 *(unsigned long*)dst = 0;
165 #ifdef __LITTLE_ENDIAN__
166 if (copy_from_user(dst, src, count))
169 if ((count == 2) && dst[1] & 0x80) {
176 if (__copy_user(dst, src, count))
179 if ((count == 2) && dst[2] & 0x80) {
186 src = (unsigned char*) rm;
187 #if !defined(__LITTLE_ENDIAN__)
190 dst = (unsigned char*) *rn;
191 dst += regs->regs[0];
193 if (copy_to_user(dst, src, count))
199 case 1: /* mov.l Rm,@(disp,Rn) */
200 src = (unsigned char*) rm;
201 dst = (unsigned char*) *rn;
202 dst += (instruction&0x000F)<<2;
204 if (copy_to_user(dst,src,4))
209 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
212 src = (unsigned char*) rm;
213 dst = (unsigned char*) *rn;
214 #if !defined(__LITTLE_ENDIAN__)
217 if (copy_to_user(dst, src, count))
222 case 5: /* mov.l @(disp,Rm),Rn */
223 src = (unsigned char*) *rm;
224 src += (instruction&0x000F)<<2;
225 dst = (unsigned char*) rn;
226 *(unsigned long*)dst = 0;
228 if (copy_from_user(dst,src,4))
233 case 6: /* mov.[bwl] from memory, possibly with post-increment */
234 src = (unsigned char*) *rm;
237 dst = (unsigned char*) rn;
238 *(unsigned long*)dst = 0;
240 #ifdef __LITTLE_ENDIAN__
241 if (copy_from_user(dst, src, count))
244 if ((count == 2) && dst[1] & 0x80) {
251 if (copy_from_user(dst, src, count))
254 if ((count == 2) && dst[2] & 0x80) {
263 switch ((instruction&0xFF00)>>8) {
264 case 0x81: /* mov.w R0,@(disp,Rn) */
265 src = (unsigned char*) ®s->regs[0];
266 #if !defined(__LITTLE_ENDIAN__)
269 dst = (unsigned char*) *rm; /* called Rn in the spec */
270 dst += (instruction&0x000F)<<1;
272 if (copy_to_user(dst, src, 2))
277 case 0x85: /* mov.w @(disp,Rm),R0 */
278 src = (unsigned char*) *rm;
279 src += (instruction&0x000F)<<1;
280 dst = (unsigned char*) ®s->regs[0];
281 *(unsigned long*)dst = 0;
283 #if !defined(__LITTLE_ENDIAN__)
287 if (copy_from_user(dst, src, 2))
290 #ifdef __LITTLE_ENDIAN__
309 /* Argh. Address not only misaligned but also non-existent.
310 * Raise an EFAULT and see if it's trapped
312 return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
316 * emulate the instruction in the delay slot
317 * - fetches the instruction from PC+2
319 static inline int handle_unaligned_delayslot(struct pt_regs *regs)
323 if (copy_from_user(&instruction, (u16 *)(regs->pc+2), 2)) {
324 /* the instruction-fetch faulted */
329 die("delay-slot-insn faulting in handle_unaligned_delayslot", regs, 0);
332 return handle_unaligned_ins(instruction,regs);
336 * handle an instruction that does an unaligned memory access
337 * - have to be careful of branch delay-slot instructions that fault
339 * - if the branch would be taken PC points to the branch
340 * - if the branch would not be taken, PC points to delay-slot
342 * - PC always points to delayed branch
343 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
346 /* Macros to determine offset from current PC for branch instructions */
347 /* Explicit type coercion is used to force sign extension where needed */
348 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
349 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
351 static int handle_unaligned_access(u16 instruction, struct pt_regs *regs)
356 index = (instruction>>8)&15; /* 0x0F00 */
357 rm = regs->regs[index];
359 /* shout about the first ten userspace fixups */
360 if (user_mode(regs) && handle_unaligned_notify_count>0) {
361 handle_unaligned_notify_count--;
363 printk("Fixing up unaligned userspace access in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
364 current->comm,current->pid,(u16*)regs->pc,instruction);
368 switch (instruction&0xF000) {
370 if (instruction==0x000B) {
372 ret = handle_unaligned_delayslot(regs);
376 else if ((instruction&0x00FF)==0x0023) {
378 ret = handle_unaligned_delayslot(regs);
382 else if ((instruction&0x00FF)==0x0003) {
384 ret = handle_unaligned_delayslot(regs);
386 regs->pr = regs->pc + 4;
391 /* mov.[bwl] to/from memory via r0+rn */
396 case 0x1000: /* mov.l Rm,@(disp,Rn) */
399 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
403 if ((instruction&0x00FF)==0x002B) {
405 ret = handle_unaligned_delayslot(regs);
409 else if ((instruction&0x00FF)==0x000B) {
411 ret = handle_unaligned_delayslot(regs);
413 regs->pr = regs->pc + 4;
418 /* mov.[bwl] to/from memory via r0+rn */
423 case 0x5000: /* mov.l @(disp,Rm),Rn */
426 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
429 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
430 switch (instruction&0x0F00) {
431 case 0x0100: /* mov.w R0,@(disp,Rm) */
433 case 0x0500: /* mov.w @(disp,Rm),R0 */
435 case 0x0B00: /* bf lab - no delayslot*/
437 case 0x0F00: /* bf/s lab */
438 ret = handle_unaligned_delayslot(regs);
440 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
441 if ((regs->sr & 0x00000001) != 0)
442 regs->pc += 4; /* next after slot */
445 regs->pc += SH_PC_8BIT_OFFSET(instruction);
448 case 0x0900: /* bt lab - no delayslot */
450 case 0x0D00: /* bt/s lab */
451 ret = handle_unaligned_delayslot(regs);
453 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
454 if ((regs->sr & 0x00000001) == 0)
455 regs->pc += 4; /* next after slot */
458 regs->pc += SH_PC_8BIT_OFFSET(instruction);
464 case 0xA000: /* bra label */
465 ret = handle_unaligned_delayslot(regs);
467 regs->pc += SH_PC_12BIT_OFFSET(instruction);
470 case 0xB000: /* bsr label */
471 ret = handle_unaligned_delayslot(regs);
473 regs->pr = regs->pc + 4;
474 regs->pc += SH_PC_12BIT_OFFSET(instruction);
480 /* handle non-delay-slot instruction */
482 ret = handle_unaligned_ins(instruction,regs);
489 * Handle various address error exceptions
491 asmlinkage void do_address_error(struct pt_regs *regs,
492 unsigned long writeaccess,
493 unsigned long address)
495 unsigned long error_code;
500 asm volatile("stc r2_bank,%0": "=r" (error_code));
504 if (user_mode(regs)) {
506 current->thread.error_code = error_code;
507 current->thread.trap_no = (writeaccess) ? 8 : 7;
509 /* bad PC is not something we can fix */
514 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
515 /* Argh. Fault on the instruction itself.
516 This should never happen non-SMP
522 tmp = handle_unaligned_access(instruction, regs);
529 printk(KERN_NOTICE "Killing process \"%s\" due to unaligned access\n", current->comm);
530 force_sig(SIGSEGV, current);
533 die("unaligned program counter", regs, error_code);
536 if (copy_from_user(&instruction, (u16 *)(regs->pc), 2)) {
537 /* Argh. Fault on the instruction itself.
538 This should never happen non-SMP
541 die("insn faulting in do_address_error", regs, 0);
544 handle_unaligned_access(instruction, regs);
551 * SH-DSP support gerg@snapgear.com.
553 int is_dsp_inst(struct pt_regs *regs)
558 * Safe guard if DSP mode is already enabled or we're lacking
559 * the DSP altogether.
561 if (!(cpu_data->flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
564 get_user(inst, ((unsigned short *) regs->pc));
568 /* Check for any type of DSP or support instruction */
569 if ((inst == 0xf000) || (inst == 0x4000))
575 #define is_dsp_inst(regs) (0)
576 #endif /* CONFIG_SH_DSP */
578 DO_ERROR(TRAP_RESERVED_INST, SIGILL, "reserved instruction", reserved_inst, current)
579 DO_ERROR(TRAP_ILLEGAL_SLOT_INST, SIGILL, "illegal slot instruction", illegal_slot_inst, current)
581 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
582 unsigned long r6, unsigned long r7,
586 asm volatile("stc r2_bank, %0" : "=r" (ex));
587 die_if_kernel("exception", ®s, ex);
590 #if defined(CONFIG_SH_STANDARD_BIOS)
591 void *gdb_vbr_vector;
593 static inline void __init gdb_vbr_init(void)
595 register unsigned long vbr;
598 * Read the old value of the VBR register to initialise
599 * the vector through which debug and BIOS traps are
600 * delegated by the Linux trap handler.
602 asm volatile("stc vbr, %0" : "=r" (vbr));
604 gdb_vbr_vector = (void *)(vbr + 0x100);
605 printk("Setting GDB trap vector to 0x%08lx\n",
606 (unsigned long)gdb_vbr_vector);
610 void __init per_cpu_trap_init(void)
612 extern void *vbr_base;
614 #ifdef CONFIG_SH_STANDARD_BIOS
618 /* NOTE: The VBR value should be at P1
619 (or P2, virtural "fixed" address space).
620 It's definitely should not in physical address. */
622 asm volatile("ldc %0, vbr"
628 void __init trap_init(void)
630 extern void *exception_handling_table[];
632 exception_handling_table[TRAP_RESERVED_INST]
633 = (void *)do_reserved_inst;
634 exception_handling_table[TRAP_ILLEGAL_SLOT_INST]
635 = (void *)do_illegal_slot_inst;
637 #ifdef CONFIG_CPU_SH4
638 if (!(cpu_data->flags & CPU_HAS_FPU)) {
639 /* For SH-4 lacking an FPU, treat floating point instructions
641 /* entry 64 corresponds to EXPEVT=0x800 */
642 exception_handling_table[64] = (void *)do_reserved_inst;
643 exception_handling_table[65] = (void *)do_illegal_slot_inst;
647 /* Setup VBR for boot cpu */
651 void show_stack(struct task_struct *tsk, unsigned long *sp)
653 unsigned long *stack, addr;
654 unsigned long module_start = VMALLOC_START;
655 unsigned long module_end = VMALLOC_END;
659 sp = (unsigned long *)tsk->thread.sp;
663 __asm__ __volatile__ (
665 "stc r7_bank, %1\n\t"
666 : "=r" (module_start),
670 sp = (unsigned long *)module_start;
675 printk("\nCall trace: ");
676 #ifdef CONFIG_KALLSYMS
680 while (!kstack_end(stack)) {
682 if (((addr >= (unsigned long)_text) &&
683 (addr <= (unsigned long)_etext)) ||
684 ((addr >= module_start) && (addr <= module_end))) {
686 * For 80-columns display, 6 entry is maximum.
687 * NOTE: '[<8c00abcd>] ' consumes 13 columns .
689 #ifndef CONFIG_KALLSYMS
690 if (i && ((i % 6) == 0))
693 printk("[<%08lx>] ", addr);
694 print_symbol("%s\n", addr);
702 void show_task(unsigned long *sp)
704 show_stack(NULL, sp);
707 void dump_stack(void)
709 show_stack(NULL, NULL);
711 EXPORT_SYMBOL(dump_stack);