1/*
2 * Based on arch/arm/kernel/traps.c
3 *
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/signal.h>
21#include <linux/personality.h>
22#include <linux/kallsyms.h>
23#include <linux/spinlock.h>
24#include <linux/uaccess.h>
25#include <linux/hardirq.h>
26#include <linux/kdebug.h>
27#include <linux/module.h>
28#include <linux/kexec.h>
29#include <linux/delay.h>
30#include <linux/init.h>
31#include <linux/sched.h>
32#include <linux/syscalls.h>
33
34#include <asm/atomic.h>
35#include <asm/debug-monitors.h>
36#include <asm/esr.h>
37#include <asm/traps.h>
38#include <asm/stacktrace.h>
39#include <asm/exception.h>
40#include <asm/system_misc.h>
41
42static const char *handler[]= {
43	"Synchronous Abort",
44	"IRQ",
45	"FIQ",
46	"Error"
47};
48
49int show_unhandled_signals = 1;
50
51/*
52 * Dump out the contents of some memory nicely...
53 */
54static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
55		     unsigned long top)
56{
57	unsigned long first;
58	mm_segment_t fs;
59	int i;
60
61	/*
62	 * We need to switch to kernel mode so that we can use __get_user
63	 * to safely read from kernel space.  Note that we now dump the
64	 * code first, just in case the backtrace kills us.
65	 */
66	fs = get_fs();
67	set_fs(KERNEL_DS);
68
69	printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
70
71	for (first = bottom & ~31; first < top; first += 32) {
72		unsigned long p;
73		char str[sizeof(" 12345678") * 8 + 1];
74
75		memset(str, ' ', sizeof(str));
76		str[sizeof(str) - 1] = '\0';
77
78		for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
79			if (p >= bottom && p < top) {
80				unsigned int val;
81				if (__get_user(val, (unsigned int *)p) == 0)
82					sprintf(str + i * 9, " %08x", val);
83				else
84					sprintf(str + i * 9, " ????????");
85			}
86		}
87		printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
88	}
89
90	set_fs(fs);
91}
92
93static void dump_backtrace_entry(unsigned long where, unsigned long stack)
94{
95	print_ip_sym(where);
96	if (in_exception_text(where))
97		dump_mem("", "Exception stack", stack,
98			 stack + sizeof(struct pt_regs));
99}
100
101static void dump_instr(const char *lvl, struct pt_regs *regs)
102{
103	unsigned long addr = instruction_pointer(regs);
104	mm_segment_t fs;
105	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
106	int i;
107
108	/*
109	 * We need to switch to kernel mode so that we can use __get_user
110	 * to safely read from kernel space.  Note that we now dump the
111	 * code first, just in case the backtrace kills us.
112	 */
113	fs = get_fs();
114	set_fs(KERNEL_DS);
115
116	for (i = -4; i < 1; i++) {
117		unsigned int val, bad;
118
119		bad = __get_user(val, &((u32 *)addr)[i]);
120
121		if (!bad)
122			p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
123		else {
124			p += sprintf(p, "bad PC value");
125			break;
126		}
127	}
128	printk("%sCode: %s\n", lvl, str);
129
130	set_fs(fs);
131}
132
133static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
134{
135	struct stackframe frame;
136
137	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
138
139	if (!tsk)
140		tsk = current;
141
142	if (regs) {
143		frame.fp = regs->regs[29];
144		frame.sp = regs->sp;
145		frame.pc = regs->pc;
146	} else if (tsk == current) {
147		frame.fp = (unsigned long)__builtin_frame_address(0);
148		frame.sp = current_stack_pointer;
149		frame.pc = (unsigned long)dump_backtrace;
150	} else {
151		/*
152		 * task blocked in __switch_to
153		 */
154		frame.fp = thread_saved_fp(tsk);
155		frame.sp = thread_saved_sp(tsk);
156		frame.pc = thread_saved_pc(tsk);
157	}
158
159	pr_emerg("Call trace:\n");
160	while (1) {
161		unsigned long where = frame.pc;
162		int ret;
163
164		ret = unwind_frame(&frame);
165		if (ret < 0)
166			break;
167		dump_backtrace_entry(where, frame.sp);
168	}
169}
170
171void show_stack(struct task_struct *tsk, unsigned long *sp)
172{
173	dump_backtrace(NULL, tsk);
174	barrier();
175}
176
177#ifdef CONFIG_PREEMPT
178#define S_PREEMPT " PREEMPT"
179#else
180#define S_PREEMPT ""
181#endif
182#ifdef CONFIG_SMP
183#define S_SMP " SMP"
184#else
185#define S_SMP ""
186#endif
187
188static int __die(const char *str, int err, struct thread_info *thread,
189		 struct pt_regs *regs)
190{
191	struct task_struct *tsk = thread->task;
192	static int die_counter;
193	int ret;
194
195	pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
196		 str, err, ++die_counter);
197
198	/* trap and error numbers are mostly meaningless on ARM */
199	ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
200	if (ret == NOTIFY_STOP)
201		return ret;
202
203	print_modules();
204	__show_regs(regs);
205	pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
206		 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
207
208	if (!user_mode(regs) || in_interrupt()) {
209		dump_mem(KERN_EMERG, "Stack: ", regs->sp,
210			 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
211		dump_backtrace(regs, tsk);
212		dump_instr(KERN_EMERG, regs);
213	}
214
215	return ret;
216}
217
218static DEFINE_RAW_SPINLOCK(die_lock);
219
220/*
221 * This function is protected against re-entrancy.
222 */
223void die(const char *str, struct pt_regs *regs, int err)
224{
225	struct thread_info *thread = current_thread_info();
226	int ret;
227
228	oops_enter();
229
230	raw_spin_lock_irq(&die_lock);
231	console_verbose();
232	bust_spinlocks(1);
233	ret = __die(str, err, thread, regs);
234
235	if (regs && kexec_should_crash(thread->task))
236		crash_kexec(regs);
237
238	bust_spinlocks(0);
239	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
240	raw_spin_unlock_irq(&die_lock);
241	oops_exit();
242
243	if (in_interrupt())
244		panic("Fatal exception in interrupt");
245	if (panic_on_oops)
246		panic("Fatal exception");
247	if (ret != NOTIFY_STOP)
248		do_exit(SIGSEGV);
249}
250
251void arm64_notify_die(const char *str, struct pt_regs *regs,
252		      struct siginfo *info, int err)
253{
254	if (user_mode(regs)) {
255		current->thread.fault_address = 0;
256		current->thread.fault_code = err;
257		force_sig_info(info->si_signo, info, current);
258	} else {
259		die(str, regs, err);
260	}
261}
262
263static LIST_HEAD(undef_hook);
264static DEFINE_RAW_SPINLOCK(undef_lock);
265
266void register_undef_hook(struct undef_hook *hook)
267{
268	unsigned long flags;
269
270	raw_spin_lock_irqsave(&undef_lock, flags);
271	list_add(&hook->node, &undef_hook);
272	raw_spin_unlock_irqrestore(&undef_lock, flags);
273}
274
275void unregister_undef_hook(struct undef_hook *hook)
276{
277	unsigned long flags;
278
279	raw_spin_lock_irqsave(&undef_lock, flags);
280	list_del(&hook->node);
281	raw_spin_unlock_irqrestore(&undef_lock, flags);
282}
283
284static int call_undef_hook(struct pt_regs *regs)
285{
286	struct undef_hook *hook;
287	unsigned long flags;
288	u32 instr;
289	int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
290	void __user *pc = (void __user *)instruction_pointer(regs);
291
292	if (!user_mode(regs))
293		return 1;
294
295	if (compat_thumb_mode(regs)) {
296		/* 16-bit Thumb instruction */
297		if (get_user(instr, (u16 __user *)pc))
298			goto exit;
299		instr = le16_to_cpu(instr);
300		if (aarch32_insn_is_wide(instr)) {
301			u32 instr2;
302
303			if (get_user(instr2, (u16 __user *)(pc + 2)))
304				goto exit;
305			instr2 = le16_to_cpu(instr2);
306			instr = (instr << 16) | instr2;
307		}
308	} else {
309		/* 32-bit ARM instruction */
310		if (get_user(instr, (u32 __user *)pc))
311			goto exit;
312		instr = le32_to_cpu(instr);
313	}
314
315	raw_spin_lock_irqsave(&undef_lock, flags);
316	list_for_each_entry(hook, &undef_hook, node)
317		if ((instr & hook->instr_mask) == hook->instr_val &&
318			(regs->pstate & hook->pstate_mask) == hook->pstate_val)
319			fn = hook->fn;
320
321	raw_spin_unlock_irqrestore(&undef_lock, flags);
322exit:
323	return fn ? fn(regs, instr) : 1;
324}
325
326asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
327{
328	siginfo_t info;
329	void __user *pc = (void __user *)instruction_pointer(regs);
330
331	/* check for AArch32 breakpoint instructions */
332	if (!aarch32_break_handler(regs))
333		return;
334
335	if (call_undef_hook(regs) == 0)
336		return;
337
338	if (show_unhandled_signals && unhandled_signal(current, SIGILL) &&
339	    printk_ratelimit()) {
340		pr_info("%s[%d]: undefined instruction: pc=%p\n",
341			current->comm, task_pid_nr(current), pc);
342		dump_instr(KERN_INFO, regs);
343	}
344
345	info.si_signo = SIGILL;
346	info.si_errno = 0;
347	info.si_code  = ILL_ILLOPC;
348	info.si_addr  = pc;
349
350	arm64_notify_die("Oops - undefined instruction", regs, &info, 0);
351}
352
353long compat_arm_syscall(struct pt_regs *regs);
354
355asmlinkage long do_ni_syscall(struct pt_regs *regs)
356{
357#ifdef CONFIG_COMPAT
358	long ret;
359	if (is_compat_task()) {
360		ret = compat_arm_syscall(regs);
361		if (ret != -ENOSYS)
362			return ret;
363	}
364#endif
365
366	if (show_unhandled_signals && printk_ratelimit()) {
367		pr_info("%s[%d]: syscall %d\n", current->comm,
368			task_pid_nr(current), (int)regs->syscallno);
369		dump_instr("", regs);
370		if (user_mode(regs))
371			__show_regs(regs);
372	}
373
374	return sys_ni_syscall();
375}
376
377static const char *esr_class_str[] = {
378	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
379	[ESR_ELx_EC_UNKNOWN]		= "Unknown/Uncategorized",
380	[ESR_ELx_EC_WFx]		= "WFI/WFE",
381	[ESR_ELx_EC_CP15_32]		= "CP15 MCR/MRC",
382	[ESR_ELx_EC_CP15_64]		= "CP15 MCRR/MRRC",
383	[ESR_ELx_EC_CP14_MR]		= "CP14 MCR/MRC",
384	[ESR_ELx_EC_CP14_LS]		= "CP14 LDC/STC",
385	[ESR_ELx_EC_FP_ASIMD]		= "ASIMD",
386	[ESR_ELx_EC_CP10_ID]		= "CP10 MRC/VMRS",
387	[ESR_ELx_EC_CP14_64]		= "CP14 MCRR/MRRC",
388	[ESR_ELx_EC_ILL]		= "PSTATE.IL",
389	[ESR_ELx_EC_SVC32]		= "SVC (AArch32)",
390	[ESR_ELx_EC_HVC32]		= "HVC (AArch32)",
391	[ESR_ELx_EC_SMC32]		= "SMC (AArch32)",
392	[ESR_ELx_EC_SVC64]		= "SVC (AArch64)",
393	[ESR_ELx_EC_HVC64]		= "HVC (AArch64)",
394	[ESR_ELx_EC_SMC64]		= "SMC (AArch64)",
395	[ESR_ELx_EC_SYS64]		= "MSR/MRS (AArch64)",
396	[ESR_ELx_EC_IMP_DEF]		= "EL3 IMP DEF",
397	[ESR_ELx_EC_IABT_LOW]		= "IABT (lower EL)",
398	[ESR_ELx_EC_IABT_CUR]		= "IABT (current EL)",
399	[ESR_ELx_EC_PC_ALIGN]		= "PC Alignment",
400	[ESR_ELx_EC_DABT_LOW]		= "DABT (lower EL)",
401	[ESR_ELx_EC_DABT_CUR]		= "DABT (current EL)",
402	[ESR_ELx_EC_SP_ALIGN]		= "SP Alignment",
403	[ESR_ELx_EC_FP_EXC32]		= "FP (AArch32)",
404	[ESR_ELx_EC_FP_EXC64]		= "FP (AArch64)",
405	[ESR_ELx_EC_SERROR]		= "SError",
406	[ESR_ELx_EC_BREAKPT_LOW]	= "Breakpoint (lower EL)",
407	[ESR_ELx_EC_BREAKPT_CUR]	= "Breakpoint (current EL)",
408	[ESR_ELx_EC_SOFTSTP_LOW]	= "Software Step (lower EL)",
409	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
410	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
411	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
412	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
413	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
414	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
415};
416
417const char *esr_get_class_string(u32 esr)
418{
419	return esr_class_str[esr >> ESR_ELx_EC_SHIFT];
420}
421
422/*
423 * bad_mode handles the impossible case in the exception vector.
424 */
425asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
426{
427	siginfo_t info;
428	void __user *pc = (void __user *)instruction_pointer(regs);
429	console_verbose();
430
431	pr_crit("Bad mode in %s handler detected, code 0x%08x -- %s\n",
432		handler[reason], esr, esr_get_class_string(esr));
433	__show_regs(regs);
434
435	info.si_signo = SIGILL;
436	info.si_errno = 0;
437	info.si_code  = ILL_ILLOPC;
438	info.si_addr  = pc;
439
440	arm64_notify_die("Oops - bad mode", regs, &info, 0);
441}
442
443void __pte_error(const char *file, int line, unsigned long val)
444{
445	pr_crit("%s:%d: bad pte %016lx.\n", file, line, val);
446}
447
448void __pmd_error(const char *file, int line, unsigned long val)
449{
450	pr_crit("%s:%d: bad pmd %016lx.\n", file, line, val);
451}
452
453void __pud_error(const char *file, int line, unsigned long val)
454{
455	pr_crit("%s:%d: bad pud %016lx.\n", file, line, val);
456}
457
458void __pgd_error(const char *file, int line, unsigned long val)
459{
460	pr_crit("%s:%d: bad pgd %016lx.\n", file, line, val);
461}
462
463void __init trap_init(void)
464{
465	return;
466}
467