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/sched/debug.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/export.h>
19 #include <linux/ptrace.h>
21 #include <asm/traps.h>
22 #include <asm/sections.h>
23 #include <linux/uaccess.h>
25 static DEFINE_SPINLOCK(die_lock
);
27 static void _send_sig(int signo
, int code
, unsigned long addr
)
29 force_sig_fault(signo
, code
, (void __user
*) addr
);
32 void die(const char *str
, struct pt_regs
*regs
, long err
)
35 spin_lock_irq(&die_lock
);
36 pr_warn("Oops: %s, sig: %ld\n", str
, err
);
38 spin_unlock_irq(&die_lock
);
40 * do_exit() should take care of panic'ing from an interrupt
41 * context so we don't handle it here
46 void _exception(int signo
, struct pt_regs
*regs
, int code
, unsigned long addr
)
49 die("Exception in kernel mode", regs
, signo
);
51 _send_sig(signo
, code
, addr
);
55 * The show_stack() is 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
,
63 unsigned long *endstack
, addr
;
68 stack
= (unsigned long *)task
->thread
.ksp
;
70 stack
= (unsigned long *)&stack
;
73 addr
= (unsigned long) stack
;
74 endstack
= (unsigned long *) PAGE_ALIGN(addr
);
76 printk("%sStack from %08lx:", loglvl
, (unsigned long)stack
);
77 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
78 if (stack
+ 1 > endstack
)
81 printk("%s\n ", loglvl
);
82 printk("%s %08lx", loglvl
, *stack
++);
85 printk("%s\nCall Trace:", loglvl
);
87 while (stack
+ 1 <= endstack
) {
90 * If the address is either in the text segment of the
91 * kernel, or in the region which contains vmalloc'ed
92 * memory, it *may* be the address of a calling
93 * routine; if so, print it so that someone tracing
94 * down the cause of the crash will be able to figure
95 * out the call path that was taken.
97 if (((addr
>= (unsigned long) _stext
) &&
98 (addr
<= (unsigned long) _etext
))) {
101 printk("%s [<%08lx>]", loglvl
, addr
);
105 printk("%s\n", loglvl
);
108 void __init
trap_init(void)
110 /* Nothing to do here */
113 /* Breakpoint handler */
114 asmlinkage
void breakpoint_c(struct pt_regs
*fp
)
117 * The breakpoint entry code has moved the PC on by 4 bytes, so we must
118 * move it back. This could be done on the host but we do it here
119 * because monitor.S of JTAG gdbserver does it too.
122 _exception(SIGTRAP
, fp
, TRAP_BRKPT
, fp
->ea
);
125 #ifndef CONFIG_NIOS2_ALIGNMENT_TRAP
126 /* Alignment exception handler */
127 asmlinkage
void handle_unaligned_c(struct pt_regs
*fp
, int cause
)
129 unsigned long addr
= RDCTL(CTL_BADADDR
);
134 if (fixup_exception(fp
))
137 if (!user_mode(fp
)) {
138 pr_alert("Unaligned access from kernel mode, this might be a hardware\n");
139 pr_alert("problem, dump registers and restart the instruction\n");
140 pr_alert(" BADADDR 0x%08lx\n", addr
);
141 pr_alert(" cause %d\n", cause
);
142 pr_alert(" op-code 0x%08lx\n", *(unsigned long *)(fp
->ea
));
147 _exception(SIGBUS
, fp
, BUS_ADRALN
, addr
);
149 #endif /* CONFIG_NIOS2_ALIGNMENT_TRAP */
151 /* Illegal instruction handler */
152 asmlinkage
void handle_illegal_c(struct pt_regs
*fp
)
155 _exception(SIGILL
, fp
, ILL_ILLOPC
, fp
->ea
);
158 /* Supervisor instruction handler */
159 asmlinkage
void handle_supervisor_instr(struct pt_regs
*fp
)
162 _exception(SIGILL
, fp
, ILL_PRVOPC
, fp
->ea
);
165 /* Division error handler */
166 asmlinkage
void handle_diverror_c(struct pt_regs
*fp
)
169 _exception(SIGFPE
, fp
, FPE_INTDIV
, fp
->ea
);
172 /* Unhandled exception handler */
173 asmlinkage
void unhandled_exception(struct pt_regs
*regs
, int cause
)
175 unsigned long addr
= RDCTL(CTL_BADADDR
);
179 pr_emerg("Unhandled exception #%d in %s mode (badaddr=0x%08lx)\n",
180 cause
, user_mode(regs
) ? "user" : "kernel", addr
);
185 pr_emerg("opcode: 0x%08lx\n", *(unsigned long *)(regs
->ea
));
188 asmlinkage
void handle_trap_1_c(struct pt_regs
*fp
)
190 _send_sig(SIGUSR1
, 0, fp
->ea
);
193 asmlinkage
void handle_trap_2_c(struct pt_regs
*fp
)
195 _send_sig(SIGUSR2
, 0, fp
->ea
);
198 asmlinkage
void handle_trap_3_c(struct pt_regs
*fp
)
200 _send_sig(SIGILL
, ILL_ILLTRP
, fp
->ea
);