blob: 596986a74a26d3b24ab2262a3c3cfd9a671bc08a [file] [log] [blame]
Ley Foon Tan771a0162014-11-06 15:19:40 +08001/*
2 * Hardware exception handling
3 *
4 * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
5 * Copyright (C) 2004 Microtronix Datacom Ltd.
6 * Copyright (C) 2001 Vic Phillips
7 *
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.
11 */
12
13#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010014#include <linux/sched/debug.h>
Ley Foon Tan771a0162014-11-06 15:19:40 +080015#include <linux/kernel.h>
16#include <linux/signal.h>
17#include <linux/export.h>
18#include <linux/mm.h>
19#include <linux/ptrace.h>
20
21#include <asm/traps.h>
22#include <asm/sections.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Ley Foon Tan771a0162014-11-06 15:19:40 +080024
25static DEFINE_SPINLOCK(die_lock);
26
Ley Foon Tanbaa54ab2015-04-16 15:19:01 +080027static void _send_sig(int signo, int code, unsigned long addr)
28{
Eric W. Biederman2e1661d22019-05-23 11:04:24 -050029 force_sig_fault(signo, code, (void __user *) addr);
Ley Foon Tanbaa54ab2015-04-16 15:19:01 +080030}
31
Ley Foon Tan771a0162014-11-06 15:19:40 +080032void die(const char *str, struct pt_regs *regs, long err)
33{
34 console_verbose();
35 spin_lock_irq(&die_lock);
36 pr_warn("Oops: %s, sig: %ld\n", str, err);
37 show_regs(regs);
38 spin_unlock_irq(&die_lock);
39 /*
40 * do_exit() should take care of panic'ing from an interrupt
41 * context so we don't handle it here
42 */
43 do_exit(err);
44}
45
46void _exception(int signo, struct pt_regs *regs, int code, unsigned long addr)
47{
Ley Foon Tan771a0162014-11-06 15:19:40 +080048 if (!user_mode(regs))
49 die("Exception in kernel mode", regs, signo);
50
Ley Foon Tanbaa54ab2015-04-16 15:19:01 +080051 _send_sig(signo, code, addr);
Ley Foon Tan771a0162014-11-06 15:19:40 +080052}
53
54/*
Dmitry Safonov9cb8f062020-06-08 21:32:29 -070055 * The show_stack() is external API which we do not use ourselves.
Ley Foon Tan771a0162014-11-06 15:19:40 +080056 */
57
58int kstack_depth_to_print = 48;
59
Dmitry Safonov9cb8f062020-06-08 21:32:29 -070060void show_stack(struct task_struct *task, unsigned long *stack,
61 const char *loglvl)
Ley Foon Tan771a0162014-11-06 15:19:40 +080062{
63 unsigned long *endstack, addr;
64 int i;
65
66 if (!stack) {
67 if (task)
68 stack = (unsigned long *)task->thread.ksp;
69 else
70 stack = (unsigned long *)&stack;
71 }
72
73 addr = (unsigned long) stack;
74 endstack = (unsigned long *) PAGE_ALIGN(addr);
75
Dmitry Safonov351dd612020-06-08 21:31:05 -070076 printk("%sStack from %08lx:", loglvl, (unsigned long)stack);
Ley Foon Tan771a0162014-11-06 15:19:40 +080077 for (i = 0; i < kstack_depth_to_print; i++) {
78 if (stack + 1 > endstack)
79 break;
80 if (i % 8 == 0)
Dmitry Safonov351dd612020-06-08 21:31:05 -070081 printk("%s\n ", loglvl);
82 printk("%s %08lx", loglvl, *stack++);
Ley Foon Tan771a0162014-11-06 15:19:40 +080083 }
84
Dmitry Safonov351dd612020-06-08 21:31:05 -070085 printk("%s\nCall Trace:", loglvl);
Ley Foon Tan771a0162014-11-06 15:19:40 +080086 i = 0;
87 while (stack + 1 <= endstack) {
88 addr = *stack++;
89 /*
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.
96 */
97 if (((addr >= (unsigned long) _stext) &&
98 (addr <= (unsigned long) _etext))) {
99 if (i % 4 == 0)
100 pr_emerg("\n ");
Dmitry Safonov351dd612020-06-08 21:31:05 -0700101 printk("%s [<%08lx>]", loglvl, addr);
Ley Foon Tan771a0162014-11-06 15:19:40 +0800102 i++;
103 }
104 }
Dmitry Safonov351dd612020-06-08 21:31:05 -0700105 printk("%s\n", loglvl);
106}
107
Ley Foon Tan771a0162014-11-06 15:19:40 +0800108/* Breakpoint handler */
109asmlinkage void breakpoint_c(struct pt_regs *fp)
110{
111 /*
112 * The breakpoint entry code has moved the PC on by 4 bytes, so we must
113 * move it back. This could be done on the host but we do it here
114 * because monitor.S of JTAG gdbserver does it too.
115 */
116 fp->ea -= 4;
117 _exception(SIGTRAP, fp, TRAP_BRKPT, fp->ea);
118}
119
120#ifndef CONFIG_NIOS2_ALIGNMENT_TRAP
121/* Alignment exception handler */
122asmlinkage void handle_unaligned_c(struct pt_regs *fp, int cause)
123{
124 unsigned long addr = RDCTL(CTL_BADADDR);
125
126 cause >>= 2;
127 fp->ea -= 4;
128
129 if (fixup_exception(fp))
130 return;
131
132 if (!user_mode(fp)) {
133 pr_alert("Unaligned access from kernel mode, this might be a hardware\n");
134 pr_alert("problem, dump registers and restart the instruction\n");
135 pr_alert(" BADADDR 0x%08lx\n", addr);
136 pr_alert(" cause %d\n", cause);
137 pr_alert(" op-code 0x%08lx\n", *(unsigned long *)(fp->ea));
138 show_regs(fp);
139 return;
140 }
141
142 _exception(SIGBUS, fp, BUS_ADRALN, addr);
143}
144#endif /* CONFIG_NIOS2_ALIGNMENT_TRAP */
145
146/* Illegal instruction handler */
147asmlinkage void handle_illegal_c(struct pt_regs *fp)
148{
149 fp->ea -= 4;
150 _exception(SIGILL, fp, ILL_ILLOPC, fp->ea);
151}
152
153/* Supervisor instruction handler */
154asmlinkage void handle_supervisor_instr(struct pt_regs *fp)
155{
156 fp->ea -= 4;
157 _exception(SIGILL, fp, ILL_PRVOPC, fp->ea);
158}
159
160/* Division error handler */
161asmlinkage void handle_diverror_c(struct pt_regs *fp)
162{
163 fp->ea -= 4;
164 _exception(SIGFPE, fp, FPE_INTDIV, fp->ea);
165}
166
167/* Unhandled exception handler */
168asmlinkage void unhandled_exception(struct pt_regs *regs, int cause)
169{
170 unsigned long addr = RDCTL(CTL_BADADDR);
171
172 cause /= 4;
173
174 pr_emerg("Unhandled exception #%d in %s mode (badaddr=0x%08lx)\n",
175 cause, user_mode(regs) ? "user" : "kernel", addr);
176
177 regs->ea -= 4;
178 show_regs(regs);
179
180 pr_emerg("opcode: 0x%08lx\n", *(unsigned long *)(regs->ea));
181}
Ley Foon Tanbaa54ab2015-04-16 15:19:01 +0800182
183asmlinkage void handle_trap_1_c(struct pt_regs *fp)
184{
185 _send_sig(SIGUSR1, 0, fp->ea);
186}
187
188asmlinkage void handle_trap_2_c(struct pt_regs *fp)
189{
190 _send_sig(SIGUSR2, 0, fp->ea);
191}
192
193asmlinkage void handle_trap_3_c(struct pt_regs *fp)
194{
195 _send_sig(SIGILL, ILL_ILLTRP, fp->ea);
196}