2 * Hardware exception handling
4 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
5 * Copyright (C) 2004 Microtronix Datacom Ltd.
6 * Copyright (C) 2001 Vic Phillips
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file COPYING in the main directory of this
10 * archive for more details.
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/export.h>
18 #include <linux/ptrace.h>
20 #include <asm/traps.h>
21 #include <asm/sections.h>
22 #include <asm/uaccess.h>
24 static DEFINE_SPINLOCK(die_lock
);
26 void die(const char *str
, struct pt_regs
*regs
, long err
)
29 spin_lock_irq(&die_lock
);
30 pr_warn("Oops: %s, sig: %ld\n", str
, err
);
32 spin_unlock_irq(&die_lock
);
34 * do_exit() should take care of panic'ing from an interrupt
35 * context so we don't handle it here
40 void _exception(int signo
, struct pt_regs
*regs
, int code
, unsigned long addr
)
45 die("Exception in kernel mode", regs
, signo
);
47 info
.si_signo
= signo
;
50 info
.si_addr
= (void __user
*) addr
;
51 force_sig_info(signo
, &info
, current
);
55 * The show_stack is an external API which we do not use ourselves.
58 int kstack_depth_to_print
= 48;
60 void show_stack(struct task_struct
*task
, unsigned long *stack
)
62 unsigned long *endstack
, addr
;
67 stack
= (unsigned long *)task
->thread
.ksp
;
69 stack
= (unsigned long *)&stack
;
72 addr
= (unsigned long) stack
;
73 endstack
= (unsigned long *) PAGE_ALIGN(addr
);
75 pr_emerg("Stack from %08lx:", (unsigned long)stack
);
76 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
77 if (stack
+ 1 > endstack
)
81 pr_emerg(" %08lx", *stack
++);
84 pr_emerg("\nCall Trace:");
86 while (stack
+ 1 <= endstack
) {
89 * If the address is either in the text segment of the
90 * kernel, or in the region which contains vmalloc'ed
91 * memory, it *may* be the address of a calling
92 * routine; if so, print it so that someone tracing
93 * down the cause of the crash will be able to figure
94 * out the call path that was taken.
96 if (((addr
>= (unsigned long) _stext
) &&
97 (addr
<= (unsigned long) _etext
))) {
100 pr_emerg(" [<%08lx>]", addr
);
107 void __init
trap_init(void)
109 /* Nothing to do here */
112 /* Breakpoint handler */
113 asmlinkage
void breakpoint_c(struct pt_regs
*fp
)
116 * The breakpoint entry code has moved the PC on by 4 bytes, so we must
117 * move it back. This could be done on the host but we do it here
118 * because monitor.S of JTAG gdbserver does it too.
121 _exception(SIGTRAP
, fp
, TRAP_BRKPT
, fp
->ea
);
124 #ifndef CONFIG_NIOS2_ALIGNMENT_TRAP
125 /* Alignment exception handler */
126 asmlinkage
void handle_unaligned_c(struct pt_regs
*fp
, int cause
)
128 unsigned long addr
= RDCTL(CTL_BADADDR
);
133 if (fixup_exception(fp
))
136 if (!user_mode(fp
)) {
137 pr_alert("Unaligned access from kernel mode, this might be a hardware\n");
138 pr_alert("problem, dump registers and restart the instruction\n");
139 pr_alert(" BADADDR 0x%08lx\n", addr
);
140 pr_alert(" cause %d\n", cause
);
141 pr_alert(" op-code 0x%08lx\n", *(unsigned long *)(fp
->ea
));
146 _exception(SIGBUS
, fp
, BUS_ADRALN
, addr
);
148 #endif /* CONFIG_NIOS2_ALIGNMENT_TRAP */
150 /* Illegal instruction handler */
151 asmlinkage
void handle_illegal_c(struct pt_regs
*fp
)
154 _exception(SIGILL
, fp
, ILL_ILLOPC
, fp
->ea
);
157 /* Supervisor instruction handler */
158 asmlinkage
void handle_supervisor_instr(struct pt_regs
*fp
)
161 _exception(SIGILL
, fp
, ILL_PRVOPC
, fp
->ea
);
164 /* Division error handler */
165 asmlinkage
void handle_diverror_c(struct pt_regs
*fp
)
168 _exception(SIGFPE
, fp
, FPE_INTDIV
, fp
->ea
);
171 /* Unhandled exception handler */
172 asmlinkage
void unhandled_exception(struct pt_regs
*regs
, int cause
)
174 unsigned long addr
= RDCTL(CTL_BADADDR
);
178 pr_emerg("Unhandled exception #%d in %s mode (badaddr=0x%08lx)\n",
179 cause
, user_mode(regs
) ? "user" : "kernel", addr
);
184 pr_emerg("opcode: 0x%08lx\n", *(unsigned long *)(regs
->ea
));