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 - 2010 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/hardirq.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/module.h>
20 #include <linux/kallsyms.h>
22 #include <linux/bug.h>
23 #include <linux/debug_locks.h>
24 #include <linux/kdebug.h>
25 #include <linux/kexec.h>
26 #include <linux/limits.h>
27 #include <linux/sysfs.h>
28 #include <linux/uaccess.h>
29 #include <linux/perf_event.h>
30 #include <asm/alignment.h>
32 #include <asm/kprobes.h>
33 #include <asm/traps.h>
34 #include <asm/bl_bit.h>
37 # define TRAP_RESERVED_INST 4
38 # define TRAP_ILLEGAL_SLOT_INST 6
39 # define TRAP_ADDRESS_ERROR 9
40 # ifdef CONFIG_CPU_SH2A
42 # define TRAP_FPU_ERROR 13
43 # define TRAP_DIVZERO_ERROR 17
44 # define TRAP_DIVOVF_ERROR 18
47 #define TRAP_RESERVED_INST 12
48 #define TRAP_ILLEGAL_SLOT_INST 13
51 static DEFINE_SPINLOCK(die_lock);
53 void die(const char * str, struct pt_regs * regs, long err)
55 static int die_counter;
59 spin_lock_irq(&die_lock);
63 printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
67 printk("Process: %s (pid: %d, stack limit = %p)\n", current->comm,
68 task_pid_nr(current), task_stack_page(current) + 1);
70 if (!user_mode(regs) || in_interrupt())
71 dump_mem("Stack: ", regs->regs[15], THREAD_SIZE +
72 (unsigned long)task_stack_page(current));
74 notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV);
78 spin_unlock_irq(&die_lock);
81 if (kexec_should_crash(current))
85 panic("Fatal exception in interrupt");
88 panic("Fatal exception");
93 static inline void die_if_kernel(const char *str, struct pt_regs *regs,
101 * try and fix up kernelspace address errors
102 * - userspace errors just cause EFAULT to be returned, resulting in SEGV
103 * - kernel/userspace interfaces cause a jump to an appropriate handler
104 * - other kernel errors are bad
106 static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
108 if (!user_mode(regs)) {
109 const struct exception_table_entry *fixup;
110 fixup = search_exception_tables(regs->pc);
112 regs->pc = fixup->fixup;
120 static inline void sign_extend(unsigned int count, unsigned char *dst)
122 #ifdef __LITTLE_ENDIAN__
123 if ((count == 1) && dst[0] & 0x80) {
128 if ((count == 2) && dst[1] & 0x80) {
133 if ((count == 1) && dst[3] & 0x80) {
138 if ((count == 2) && dst[2] & 0x80) {
145 static struct mem_access user_mem_access = {
151 * handle an instruction that does an unaligned memory access by emulating the
153 * - note that PC _may not_ point to the faulting instruction
154 * (if that instruction is in a branch delay slot)
155 * - return 0 if emulation okay, -EFAULT on existential error
157 static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
158 struct mem_access *ma)
160 int ret, index, count;
161 unsigned long *rm, *rn;
162 unsigned char *src, *dst;
163 unsigned char __user *srcu, *dstu;
165 index = (instruction>>8)&15; /* 0x0F00 */
166 rn = ®s->regs[index];
168 index = (instruction>>4)&15; /* 0x00F0 */
169 rm = ®s->regs[index];
171 count = 1<<(instruction&3);
174 case 1: inc_unaligned_byte_access(); break;
175 case 2: inc_unaligned_word_access(); break;
176 case 4: inc_unaligned_dword_access(); break;
177 case 8: inc_unaligned_multi_access(); break;
181 switch (instruction>>12) {
182 case 0: /* mov.[bwl] to/from memory via r0+rn */
183 if (instruction & 8) {
185 srcu = (unsigned char __user *)*rm;
186 srcu += regs->regs[0];
187 dst = (unsigned char *)rn;
188 *(unsigned long *)dst = 0;
190 #if !defined(__LITTLE_ENDIAN__)
193 if (ma->from(dst, srcu, count))
196 sign_extend(count, dst);
199 src = (unsigned char *)rm;
200 #if !defined(__LITTLE_ENDIAN__)
203 dstu = (unsigned char __user *)*rn;
204 dstu += regs->regs[0];
206 if (ma->to(dstu, src, count))
212 case 1: /* mov.l Rm,@(disp,Rn) */
213 src = (unsigned char*) rm;
214 dstu = (unsigned char __user *)*rn;
215 dstu += (instruction&0x000F)<<2;
217 if (ma->to(dstu, src, 4))
222 case 2: /* mov.[bwl] to memory, possibly with pre-decrement */
225 src = (unsigned char*) rm;
226 dstu = (unsigned char __user *)*rn;
227 #if !defined(__LITTLE_ENDIAN__)
230 if (ma->to(dstu, src, count))
235 case 5: /* mov.l @(disp,Rm),Rn */
236 srcu = (unsigned char __user *)*rm;
237 srcu += (instruction & 0x000F) << 2;
238 dst = (unsigned char *)rn;
239 *(unsigned long *)dst = 0;
241 if (ma->from(dst, srcu, 4))
246 case 6: /* mov.[bwl] from memory, possibly with post-increment */
247 srcu = (unsigned char __user *)*rm;
250 dst = (unsigned char*) rn;
251 *(unsigned long*)dst = 0;
253 #if !defined(__LITTLE_ENDIAN__)
256 if (ma->from(dst, srcu, count))
258 sign_extend(count, dst);
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 dstu = (unsigned char __user *)*rm; /* called Rn in the spec */
270 dstu += (instruction & 0x000F) << 1;
272 if (ma->to(dstu, src, 2))
277 case 0x85: /* mov.w @(disp,Rm),R0 */
278 srcu = (unsigned char __user *)*rm;
279 srcu += (instruction & 0x000F) << 1;
280 dst = (unsigned char *) ®s->regs[0];
281 *(unsigned long *)dst = 0;
283 #if !defined(__LITTLE_ENDIAN__)
286 if (ma->from(dst, srcu, 2))
294 case 9: /* mov.w @(disp,PC),Rn */
295 srcu = (unsigned char __user *)regs->pc;
297 srcu += (instruction & 0x00FF) << 1;
298 dst = (unsigned char *)rn;
299 *(unsigned long *)dst = 0;
301 #if !defined(__LITTLE_ENDIAN__)
305 if (ma->from(dst, srcu, 2))
311 case 0xd: /* mov.l @(disp,PC),Rn */
312 srcu = (unsigned char __user *)(regs->pc & ~0x3);
314 srcu += (instruction & 0x00FF) << 2;
315 dst = (unsigned char *)rn;
316 *(unsigned long *)dst = 0;
318 if (ma->from(dst, srcu, 4))
326 /* Argh. Address not only misaligned but also non-existent.
327 * Raise an EFAULT and see if it's trapped
329 die_if_no_fixup("Fault in unaligned fixup", regs, 0);
334 * emulate the instruction in the delay slot
335 * - fetches the instruction from PC+2
337 static inline int handle_delayslot(struct pt_regs *regs,
338 insn_size_t old_instruction,
339 struct mem_access *ma)
341 insn_size_t instruction;
342 void __user *addr = (void __user *)(regs->pc +
343 instruction_size(old_instruction));
345 if (copy_from_user(&instruction, addr, sizeof(instruction))) {
346 /* the instruction-fetch faulted */
351 die("delay-slot-insn faulting in handle_unaligned_delayslot",
355 return handle_unaligned_ins(instruction, regs, ma);
359 * handle an instruction that does an unaligned memory access
360 * - have to be careful of branch delay-slot instructions that fault
362 * - if the branch would be taken PC points to the branch
363 * - if the branch would not be taken, PC points to delay-slot
365 * - PC always points to delayed branch
366 * - return 0 if handled, -EFAULT if failed (may not return if in kernel)
369 /* Macros to determine offset from current PC for branch instructions */
370 /* Explicit type coercion is used to force sign extension where needed */
371 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
372 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
374 int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
375 struct mem_access *ma, int expected,
376 unsigned long address)
382 * XXX: We can't handle mixed 16/32-bit instructions yet
384 if (instruction_size(instruction) != 2)
387 index = (instruction>>8)&15; /* 0x0F00 */
388 rm = regs->regs[index];
391 * Log the unexpected fixups, and then pass them on to perf.
393 * We intentionally don't report the expected cases to perf as
394 * otherwise the trapped I/O case will skew the results too much
398 unaligned_fixups_notify(current, instruction, regs);
399 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1,
404 switch (instruction&0xF000) {
406 if (instruction==0x000B) {
408 ret = handle_delayslot(regs, instruction, ma);
412 else if ((instruction&0x00FF)==0x0023) {
414 ret = handle_delayslot(regs, instruction, ma);
418 else if ((instruction&0x00FF)==0x0003) {
420 ret = handle_delayslot(regs, instruction, ma);
422 regs->pr = regs->pc + 4;
427 /* mov.[bwl] to/from memory via r0+rn */
432 case 0x1000: /* mov.l Rm,@(disp,Rn) */
435 case 0x2000: /* mov.[bwl] to memory, possibly with pre-decrement */
439 if ((instruction&0x00FF)==0x002B) {
441 ret = handle_delayslot(regs, instruction, ma);
445 else if ((instruction&0x00FF)==0x000B) {
447 ret = handle_delayslot(regs, instruction, ma);
449 regs->pr = regs->pc + 4;
454 /* mov.[bwl] to/from memory via r0+rn */
459 case 0x5000: /* mov.l @(disp,Rm),Rn */
462 case 0x6000: /* mov.[bwl] from memory, possibly with post-increment */
465 case 0x8000: /* bf lab, bf/s lab, bt lab, bt/s lab */
466 switch (instruction&0x0F00) {
467 case 0x0100: /* mov.w R0,@(disp,Rm) */
469 case 0x0500: /* mov.w @(disp,Rm),R0 */
471 case 0x0B00: /* bf lab - no delayslot*/
474 case 0x0F00: /* bf/s lab */
475 ret = handle_delayslot(regs, instruction, ma);
477 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
478 if ((regs->sr & 0x00000001) != 0)
479 regs->pc += 4; /* next after slot */
482 regs->pc += SH_PC_8BIT_OFFSET(instruction);
485 case 0x0900: /* bt lab - no delayslot */
488 case 0x0D00: /* bt/s lab */
489 ret = handle_delayslot(regs, instruction, ma);
491 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_SH7705_CACHE_32KB)
492 if ((regs->sr & 0x00000001) == 0)
493 regs->pc += 4; /* next after slot */
496 regs->pc += SH_PC_8BIT_OFFSET(instruction);
502 case 0x9000: /* mov.w @(disp,Rm),Rn */
505 case 0xA000: /* bra label */
506 ret = handle_delayslot(regs, instruction, ma);
508 regs->pc += SH_PC_12BIT_OFFSET(instruction);
511 case 0xB000: /* bsr label */
512 ret = handle_delayslot(regs, instruction, ma);
514 regs->pr = regs->pc + 4;
515 regs->pc += SH_PC_12BIT_OFFSET(instruction);
519 case 0xD000: /* mov.l @(disp,Rm),Rn */
524 /* handle non-delay-slot instruction */
526 ret = handle_unaligned_ins(instruction, regs, ma);
528 regs->pc += instruction_size(instruction);
533 * Handle various address error exceptions:
534 * - instruction address error:
536 * PC >= 0x80000000 in user mode
537 * - data address error (read and write)
538 * misaligned data access
539 * access to >= 0x80000000 is user mode
540 * Unfortuntaly we can't distinguish between instruction address error
541 * and data address errors caused by read accesses.
543 asmlinkage void do_address_error(struct pt_regs *regs,
544 unsigned long writeaccess,
545 unsigned long address)
547 unsigned long error_code = 0;
550 insn_size_t instruction;
553 /* Intentional ifdef */
554 #ifdef CONFIG_CPU_HAS_SR_RB
555 error_code = lookup_exception_vector();
560 if (user_mode(regs)) {
561 int si_code = BUS_ADRERR;
562 unsigned int user_action;
565 inc_unaligned_user_access();
568 if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
569 sizeof(instruction))) {
575 /* shout about userspace fixups */
576 unaligned_fixups_notify(current, instruction, regs);
578 user_action = unaligned_user_action();
579 if (user_action & UM_FIXUP)
581 if (user_action & UM_SIGNAL)
585 regs->pc += instruction_size(instruction);
590 /* bad PC is not something we can fix */
592 si_code = BUS_ADRALN;
597 tmp = handle_unaligned_access(instruction, regs,
605 printk(KERN_NOTICE "Sending SIGBUS to \"%s\" due to unaligned "
606 "access (PC %lx PR %lx)\n", current->comm, regs->pc,
609 info.si_signo = SIGBUS;
611 info.si_code = si_code;
612 info.si_addr = (void __user *)address;
613 force_sig_info(SIGBUS, &info, current);
615 inc_unaligned_kernel_access();
618 die("unaligned program counter", regs, error_code);
621 if (copy_from_user(&instruction, (void __user *)(regs->pc),
622 sizeof(instruction))) {
623 /* Argh. Fault on the instruction itself.
624 This should never happen non-SMP
627 die("insn faulting in do_address_error", regs, 0);
630 unaligned_fixups_notify(current, instruction, regs);
632 handle_unaligned_access(instruction, regs, &user_mem_access,
640 * SH-DSP support gerg@snapgear.com.
642 int is_dsp_inst(struct pt_regs *regs)
644 unsigned short inst = 0;
647 * Safe guard if DSP mode is already enabled or we're lacking
648 * the DSP altogether.
650 if (!(current_cpu_data.flags & CPU_HAS_DSP) || (regs->sr & SR_DSP))
653 get_user(inst, ((unsigned short *) regs->pc));
657 /* Check for any type of DSP or support instruction */
658 if ((inst == 0xf000) || (inst == 0x4000))
664 #define is_dsp_inst(regs) (0)
665 #endif /* CONFIG_SH_DSP */
667 #ifdef CONFIG_CPU_SH2A
668 asmlinkage void do_divide_error(unsigned long r4, unsigned long r5,
669 unsigned long r6, unsigned long r7,
670 struct pt_regs __regs)
675 case TRAP_DIVZERO_ERROR:
676 info.si_code = FPE_INTDIV;
678 case TRAP_DIVOVF_ERROR:
679 info.si_code = FPE_INTOVF;
683 force_sig_info(SIGFPE, &info, current);
687 asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
688 unsigned long r6, unsigned long r7,
689 struct pt_regs __regs)
691 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
692 unsigned long error_code;
693 struct task_struct *tsk = current;
695 #ifdef CONFIG_SH_FPU_EMU
696 unsigned short inst = 0;
699 get_user(inst, (unsigned short*)regs->pc);
701 err = do_fpu_inst(inst, regs);
703 regs->pc += instruction_size(inst);
706 /* not a FPU inst. */
710 /* Check if it's a DSP instruction */
711 if (is_dsp_inst(regs)) {
712 /* Enable DSP mode, and restart instruction. */
715 tsk->thread.dsp_status.status |= SR_DSP;
720 error_code = lookup_exception_vector();
723 force_sig(SIGILL, tsk);
724 die_if_no_fixup("reserved instruction", regs, error_code);
727 #ifdef CONFIG_SH_FPU_EMU
728 static int emulate_branch(unsigned short inst, struct pt_regs *regs)
731 * bfs: 8fxx: PC+=d*2+4;
732 * bts: 8dxx: PC+=d*2+4;
733 * bra: axxx: PC+=D*2+4;
734 * bsr: bxxx: PC+=D*2+4 after PR=PC+4;
735 * braf:0x23: PC+=Rn*2+4;
736 * bsrf:0x03: PC+=Rn*2+4 after PR=PC+4;
738 * jsr: 4x0b: PC=Rn after PR=PC+4;
741 if (((inst & 0xf000) == 0xb000) || /* bsr */
742 ((inst & 0xf0ff) == 0x0003) || /* bsrf */
743 ((inst & 0xf0ff) == 0x400b)) /* jsr */
744 regs->pr = regs->pc + 4;
746 if ((inst & 0xfd00) == 0x8d00) { /* bfs, bts */
747 regs->pc += SH_PC_8BIT_OFFSET(inst);
751 if ((inst & 0xe000) == 0xa000) { /* bra, bsr */
752 regs->pc += SH_PC_12BIT_OFFSET(inst);
756 if ((inst & 0xf0df) == 0x0003) { /* braf, bsrf */
757 regs->pc += regs->regs[(inst & 0x0f00) >> 8] + 4;
761 if ((inst & 0xf0df) == 0x400b) { /* jmp, jsr */
762 regs->pc = regs->regs[(inst & 0x0f00) >> 8];
766 if ((inst & 0xffff) == 0x000b) { /* rts */
775 asmlinkage void do_illegal_slot_inst(unsigned long r4, unsigned long r5,
776 unsigned long r6, unsigned long r7,
777 struct pt_regs __regs)
779 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
781 struct task_struct *tsk = current;
783 if (kprobe_handle_illslot(regs->pc) == 0)
786 #ifdef CONFIG_SH_FPU_EMU
787 get_user(inst, (unsigned short *)regs->pc + 1);
788 if (!do_fpu_inst(inst, regs)) {
789 get_user(inst, (unsigned short *)regs->pc);
790 if (!emulate_branch(inst, regs))
792 /* fault in branch.*/
794 /* not a FPU inst. */
797 inst = lookup_exception_vector();
800 force_sig(SIGILL, tsk);
801 die_if_no_fixup("illegal slot instruction", regs, inst);
804 asmlinkage void do_exception_error(unsigned long r4, unsigned long r5,
805 unsigned long r6, unsigned long r7,
806 struct pt_regs __regs)
808 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
811 ex = lookup_exception_vector();
812 die_if_kernel("exception", regs, ex);
815 void __cpuinit per_cpu_trap_init(void)
817 extern void *vbr_base;
819 /* NOTE: The VBR value should be at P1
820 (or P2, virtural "fixed" address space).
821 It's definitely should not in physical address. */
823 asm volatile("ldc %0, vbr"
828 /* disable exception blocking now when the vbr has been setup */
832 void *set_exception_table_vec(unsigned int vec, void *handler)
834 extern void *exception_handling_table[];
837 old_handler = exception_handling_table[vec];
838 exception_handling_table[vec] = handler;
842 void __init trap_init(void)
844 set_exception_table_vec(TRAP_RESERVED_INST, do_reserved_inst);
845 set_exception_table_vec(TRAP_ILLEGAL_SLOT_INST, do_illegal_slot_inst);
847 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SH_FPU) || \
848 defined(CONFIG_SH_FPU_EMU)
850 * For SH-4 lacking an FPU, treat floating point instructions as
851 * reserved. They'll be handled in the math-emu case, or faulted on
854 set_exception_table_evt(0x800, do_reserved_inst);
855 set_exception_table_evt(0x820, do_illegal_slot_inst);
856 #elif defined(CONFIG_SH_FPU)
857 set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
858 set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
861 #ifdef CONFIG_CPU_SH2
862 set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
864 #ifdef CONFIG_CPU_SH2A
865 set_exception_table_vec(TRAP_DIVZERO_ERROR, do_divide_error);
866 set_exception_table_vec(TRAP_DIVOVF_ERROR, do_divide_error);
868 set_exception_table_vec(TRAP_FPU_ERROR, fpu_error_trap_handler);
873 set_exception_table_vec(TRAP_UBC, breakpoint_trap_handler);