Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  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>
 
 14#include <linux/kernel.h>
 15#include <linux/signal.h>
 16#include <linux/export.h>
 17#include <linux/mm.h>
 18#include <linux/ptrace.h>
 19
 20#include <asm/traps.h>
 21#include <asm/sections.h>
 22#include <asm/uaccess.h>
 23
 24static DEFINE_SPINLOCK(die_lock);
 25
 26static void _send_sig(int signo, int code, unsigned long addr)
 27{
 28	siginfo_t info;
 29
 30	info.si_signo = signo;
 31	info.si_errno = 0;
 32	info.si_code = code;
 33	info.si_addr = (void __user *) addr;
 34	force_sig_info(signo, &info, current);
 35}
 36
 37void die(const char *str, struct pt_regs *regs, long err)
 38{
 39	console_verbose();
 40	spin_lock_irq(&die_lock);
 41	pr_warn("Oops: %s, sig: %ld\n", str, err);
 42	show_regs(regs);
 43	spin_unlock_irq(&die_lock);
 44	/*
 45	 * do_exit() should take care of panic'ing from an interrupt
 46	 * context so we don't handle it here
 47	 */
 48	do_exit(err);
 49}
 50
 51void _exception(int signo, struct pt_regs *regs, int code, unsigned long addr)
 52{
 53	if (!user_mode(regs))
 54		die("Exception in kernel mode", regs, signo);
 55
 56	_send_sig(signo, code, addr);
 57}
 58
 59/*
 60 * The show_stack is an external API which we do not use ourselves.
 61 */
 62
 63int kstack_depth_to_print = 48;
 64
 65void show_stack(struct task_struct *task, unsigned long *stack)
 66{
 67	unsigned long *endstack, addr;
 68	int i;
 69
 70	if (!stack) {
 71		if (task)
 72			stack = (unsigned long *)task->thread.ksp;
 73		else
 74			stack = (unsigned long *)&stack;
 75	}
 76
 77	addr = (unsigned long) stack;
 78	endstack = (unsigned long *) PAGE_ALIGN(addr);
 79
 80	pr_emerg("Stack from %08lx:", (unsigned long)stack);
 81	for (i = 0; i < kstack_depth_to_print; i++) {
 82		if (stack + 1 > endstack)
 83			break;
 84		if (i % 8 == 0)
 85			pr_emerg("\n       ");
 86		pr_emerg(" %08lx", *stack++);
 87	}
 88
 89	pr_emerg("\nCall Trace:");
 90	i = 0;
 91	while (stack + 1 <= endstack) {
 92		addr = *stack++;
 93		/*
 94		 * If the address is either in the text segment of the
 95		 * kernel, or in the region which contains vmalloc'ed
 96		 * memory, it *may* be the address of a calling
 97		 * routine; if so, print it so that someone tracing
 98		 * down the cause of the crash will be able to figure
 99		 * out the call path that was taken.
100		 */
101		if (((addr >= (unsigned long) _stext) &&
102		     (addr <= (unsigned long) _etext))) {
103			if (i % 4 == 0)
104				pr_emerg("\n       ");
105			pr_emerg(" [<%08lx>]", addr);
106			i++;
107		}
108	}
109	pr_emerg("\n");
110}
111
112void __init trap_init(void)
113{
114	/* Nothing to do here */
115}
116
117/* Breakpoint handler */
118asmlinkage void breakpoint_c(struct pt_regs *fp)
119{
120	/*
121	 * The breakpoint entry code has moved the PC on by 4 bytes, so we must
122	 * move it back. This could be done on the host but we do it here
123	 * because monitor.S of JTAG gdbserver does it too.
124	 */
125	fp->ea -= 4;
126	_exception(SIGTRAP, fp, TRAP_BRKPT, fp->ea);
127}
128
129#ifndef CONFIG_NIOS2_ALIGNMENT_TRAP
130/* Alignment exception handler */
131asmlinkage void handle_unaligned_c(struct pt_regs *fp, int cause)
132{
133	unsigned long addr = RDCTL(CTL_BADADDR);
134
135	cause >>= 2;
136	fp->ea -= 4;
137
138	if (fixup_exception(fp))
139		return;
140
141	if (!user_mode(fp)) {
142		pr_alert("Unaligned access from kernel mode, this might be a hardware\n");
143		pr_alert("problem, dump registers and restart the instruction\n");
144		pr_alert("  BADADDR 0x%08lx\n", addr);
145		pr_alert("  cause   %d\n", cause);
146		pr_alert("  op-code 0x%08lx\n", *(unsigned long *)(fp->ea));
147		show_regs(fp);
148		return;
149	}
150
151	_exception(SIGBUS, fp, BUS_ADRALN, addr);
152}
153#endif /* CONFIG_NIOS2_ALIGNMENT_TRAP */
154
155/* Illegal instruction handler */
156asmlinkage void handle_illegal_c(struct pt_regs *fp)
157{
158	fp->ea -= 4;
159	_exception(SIGILL, fp, ILL_ILLOPC, fp->ea);
160}
161
162/* Supervisor instruction handler */
163asmlinkage void handle_supervisor_instr(struct pt_regs *fp)
164{
165	fp->ea -= 4;
166	_exception(SIGILL, fp, ILL_PRVOPC, fp->ea);
167}
168
169/* Division error handler */
170asmlinkage void handle_diverror_c(struct pt_regs *fp)
171{
172	fp->ea -= 4;
173	_exception(SIGFPE, fp, FPE_INTDIV, fp->ea);
174}
175
176/* Unhandled exception handler */
177asmlinkage void unhandled_exception(struct pt_regs *regs, int cause)
178{
179	unsigned long addr = RDCTL(CTL_BADADDR);
180
181	cause /= 4;
182
183	pr_emerg("Unhandled exception #%d in %s mode (badaddr=0x%08lx)\n",
184			cause, user_mode(regs) ? "user" : "kernel", addr);
185
186	regs->ea -= 4;
187	show_regs(regs);
188
189	pr_emerg("opcode: 0x%08lx\n", *(unsigned long *)(regs->ea));
190}
191
192asmlinkage void handle_trap_1_c(struct pt_regs *fp)
193{
194	_send_sig(SIGUSR1, 0, fp->ea);
195}
196
197asmlinkage void handle_trap_2_c(struct pt_regs *fp)
198{
199	_send_sig(SIGUSR2, 0, fp->ea);
200}
201
202asmlinkage void handle_trap_3_c(struct pt_regs *fp)
203{
204	_send_sig(SIGILL, ILL_ILLTRP, fp->ea);
205}
v4.17
  1/*
  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>
 14#include <linux/sched/debug.h>
 15#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>
 23#include <linux/uaccess.h>
 24
 25static DEFINE_SPINLOCK(die_lock);
 26
 27static void _send_sig(int signo, int code, unsigned long addr)
 28{
 29	siginfo_t info;
 30
 31	info.si_signo = signo;
 32	info.si_errno = 0;
 33	info.si_code = code;
 34	info.si_addr = (void __user *) addr;
 35	force_sig_info(signo, &info, current);
 36}
 37
 38void die(const char *str, struct pt_regs *regs, long err)
 39{
 40	console_verbose();
 41	spin_lock_irq(&die_lock);
 42	pr_warn("Oops: %s, sig: %ld\n", str, err);
 43	show_regs(regs);
 44	spin_unlock_irq(&die_lock);
 45	/*
 46	 * do_exit() should take care of panic'ing from an interrupt
 47	 * context so we don't handle it here
 48	 */
 49	do_exit(err);
 50}
 51
 52void _exception(int signo, struct pt_regs *regs, int code, unsigned long addr)
 53{
 54	if (!user_mode(regs))
 55		die("Exception in kernel mode", regs, signo);
 56
 57	_send_sig(signo, code, addr);
 58}
 59
 60/*
 61 * The show_stack is an external API which we do not use ourselves.
 62 */
 63
 64int kstack_depth_to_print = 48;
 65
 66void show_stack(struct task_struct *task, unsigned long *stack)
 67{
 68	unsigned long *endstack, addr;
 69	int i;
 70
 71	if (!stack) {
 72		if (task)
 73			stack = (unsigned long *)task->thread.ksp;
 74		else
 75			stack = (unsigned long *)&stack;
 76	}
 77
 78	addr = (unsigned long) stack;
 79	endstack = (unsigned long *) PAGE_ALIGN(addr);
 80
 81	pr_emerg("Stack from %08lx:", (unsigned long)stack);
 82	for (i = 0; i < kstack_depth_to_print; i++) {
 83		if (stack + 1 > endstack)
 84			break;
 85		if (i % 8 == 0)
 86			pr_emerg("\n       ");
 87		pr_emerg(" %08lx", *stack++);
 88	}
 89
 90	pr_emerg("\nCall Trace:");
 91	i = 0;
 92	while (stack + 1 <= endstack) {
 93		addr = *stack++;
 94		/*
 95		 * If the address is either in the text segment of the
 96		 * kernel, or in the region which contains vmalloc'ed
 97		 * memory, it *may* be the address of a calling
 98		 * routine; if so, print it so that someone tracing
 99		 * down the cause of the crash will be able to figure
100		 * out the call path that was taken.
101		 */
102		if (((addr >= (unsigned long) _stext) &&
103		     (addr <= (unsigned long) _etext))) {
104			if (i % 4 == 0)
105				pr_emerg("\n       ");
106			pr_emerg(" [<%08lx>]", addr);
107			i++;
108		}
109	}
110	pr_emerg("\n");
111}
112
113void __init trap_init(void)
114{
115	/* Nothing to do here */
116}
117
118/* Breakpoint handler */
119asmlinkage void breakpoint_c(struct pt_regs *fp)
120{
121	/*
122	 * The breakpoint entry code has moved the PC on by 4 bytes, so we must
123	 * move it back. This could be done on the host but we do it here
124	 * because monitor.S of JTAG gdbserver does it too.
125	 */
126	fp->ea -= 4;
127	_exception(SIGTRAP, fp, TRAP_BRKPT, fp->ea);
128}
129
130#ifndef CONFIG_NIOS2_ALIGNMENT_TRAP
131/* Alignment exception handler */
132asmlinkage void handle_unaligned_c(struct pt_regs *fp, int cause)
133{
134	unsigned long addr = RDCTL(CTL_BADADDR);
135
136	cause >>= 2;
137	fp->ea -= 4;
138
139	if (fixup_exception(fp))
140		return;
141
142	if (!user_mode(fp)) {
143		pr_alert("Unaligned access from kernel mode, this might be a hardware\n");
144		pr_alert("problem, dump registers and restart the instruction\n");
145		pr_alert("  BADADDR 0x%08lx\n", addr);
146		pr_alert("  cause   %d\n", cause);
147		pr_alert("  op-code 0x%08lx\n", *(unsigned long *)(fp->ea));
148		show_regs(fp);
149		return;
150	}
151
152	_exception(SIGBUS, fp, BUS_ADRALN, addr);
153}
154#endif /* CONFIG_NIOS2_ALIGNMENT_TRAP */
155
156/* Illegal instruction handler */
157asmlinkage void handle_illegal_c(struct pt_regs *fp)
158{
159	fp->ea -= 4;
160	_exception(SIGILL, fp, ILL_ILLOPC, fp->ea);
161}
162
163/* Supervisor instruction handler */
164asmlinkage void handle_supervisor_instr(struct pt_regs *fp)
165{
166	fp->ea -= 4;
167	_exception(SIGILL, fp, ILL_PRVOPC, fp->ea);
168}
169
170/* Division error handler */
171asmlinkage void handle_diverror_c(struct pt_regs *fp)
172{
173	fp->ea -= 4;
174	_exception(SIGFPE, fp, FPE_INTDIV, fp->ea);
175}
176
177/* Unhandled exception handler */
178asmlinkage void unhandled_exception(struct pt_regs *regs, int cause)
179{
180	unsigned long addr = RDCTL(CTL_BADADDR);
181
182	cause /= 4;
183
184	pr_emerg("Unhandled exception #%d in %s mode (badaddr=0x%08lx)\n",
185			cause, user_mode(regs) ? "user" : "kernel", addr);
186
187	regs->ea -= 4;
188	show_regs(regs);
189
190	pr_emerg("opcode: 0x%08lx\n", *(unsigned long *)(regs->ea));
191}
192
193asmlinkage void handle_trap_1_c(struct pt_regs *fp)
194{
195	_send_sig(SIGUSR1, 0, fp->ea);
196}
197
198asmlinkage void handle_trap_2_c(struct pt_regs *fp)
199{
200	_send_sig(SIGUSR2, 0, fp->ea);
201}
202
203asmlinkage void handle_trap_3_c(struct pt_regs *fp)
204{
205	_send_sig(SIGILL, ILL_ILLTRP, fp->ea);
206}