2 * Based on arch/arm/kernel/traps.c
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
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.
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.
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/>.
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>
34 #include <asm/atomic.h>
35 #include <asm/debug-monitors.h>
36 #include <asm/traps.h>
37 #include <asm/stacktrace.h>
38 #include <asm/exception.h>
39 #include <asm/system_misc.h>
41 static const char *handler[]= {
48 int show_unhandled_signals = 1;
51 * Dump out the contents of some memory nicely...
53 static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
61 * We need to switch to kernel mode so that we can use __get_user
62 * to safely read from kernel space. Note that we now dump the
63 * code first, just in case the backtrace kills us.
68 printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
70 for (first = bottom & ~31; first < top; first += 32) {
72 char str[sizeof(" 12345678") * 8 + 1];
74 memset(str, ' ', sizeof(str));
75 str[sizeof(str) - 1] = '\0';
77 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
78 if (p >= bottom && p < top) {
80 if (__get_user(val, (unsigned int *)p) == 0)
81 sprintf(str + i * 9, " %08x", val);
83 sprintf(str + i * 9, " ????????");
86 printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
92 static void dump_backtrace_entry(unsigned long where, unsigned long stack)
95 if (in_exception_text(where))
96 dump_mem("", "Exception stack", stack,
97 stack + sizeof(struct pt_regs));
100 static void dump_instr(const char *lvl, struct pt_regs *regs)
102 unsigned long addr = instruction_pointer(regs);
104 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
108 * We need to switch to kernel mode so that we can use __get_user
109 * to safely read from kernel space. Note that we now dump the
110 * code first, just in case the backtrace kills us.
115 for (i = -4; i < 1; i++) {
116 unsigned int val, bad;
118 bad = __get_user(val, &((u32 *)addr)[i]);
121 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
123 p += sprintf(p, "bad PC value");
127 printk("%sCode: %s\n", lvl, str);
132 static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
134 struct stackframe frame;
136 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
142 frame.fp = regs->regs[29];
145 } else if (tsk == current) {
146 frame.fp = (unsigned long)__builtin_frame_address(0);
147 frame.sp = current_stack_pointer;
148 frame.pc = (unsigned long)dump_backtrace;
151 * task blocked in __switch_to
153 frame.fp = thread_saved_fp(tsk);
154 frame.sp = thread_saved_sp(tsk);
155 frame.pc = thread_saved_pc(tsk);
158 pr_emerg("Call trace:\n");
160 unsigned long where = frame.pc;
163 ret = unwind_frame(&frame);
166 dump_backtrace_entry(where, frame.sp);
170 void show_stack(struct task_struct *tsk, unsigned long *sp)
172 dump_backtrace(NULL, tsk);
176 #ifdef CONFIG_PREEMPT
177 #define S_PREEMPT " PREEMPT"
187 static int __die(const char *str, int err, struct thread_info *thread,
188 struct pt_regs *regs)
190 struct task_struct *tsk = thread->task;
191 static int die_counter;
194 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
195 str, err, ++die_counter);
197 /* trap and error numbers are mostly meaningless on ARM */
198 ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
199 if (ret == NOTIFY_STOP)
204 pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
205 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
207 if (!user_mode(regs) || in_interrupt()) {
208 dump_mem(KERN_EMERG, "Stack: ", regs->sp,
209 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
210 dump_backtrace(regs, tsk);
211 dump_instr(KERN_EMERG, regs);
217 static DEFINE_RAW_SPINLOCK(die_lock);
220 * This function is protected against re-entrancy.
222 void die(const char *str, struct pt_regs *regs, int err)
224 struct thread_info *thread = current_thread_info();
229 raw_spin_lock_irq(&die_lock);
232 ret = __die(str, err, thread, regs);
234 if (regs && kexec_should_crash(thread->task))
238 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
239 raw_spin_unlock_irq(&die_lock);
243 panic("Fatal exception in interrupt");
245 panic("Fatal exception");
246 if (ret != NOTIFY_STOP)
250 void arm64_notify_die(const char *str, struct pt_regs *regs,
251 struct siginfo *info, int err)
253 if (user_mode(regs)) {
254 current->thread.fault_address = 0;
255 current->thread.fault_code = err;
256 force_sig_info(info->si_signo, info, current);
262 static LIST_HEAD(undef_hook);
263 static DEFINE_RAW_SPINLOCK(undef_lock);
265 void register_undef_hook(struct undef_hook *hook)
269 raw_spin_lock_irqsave(&undef_lock, flags);
270 list_add(&hook->node, &undef_hook);
271 raw_spin_unlock_irqrestore(&undef_lock, flags);
274 void unregister_undef_hook(struct undef_hook *hook)
278 raw_spin_lock_irqsave(&undef_lock, flags);
279 list_del(&hook->node);
280 raw_spin_unlock_irqrestore(&undef_lock, flags);
283 static int call_undef_hook(struct pt_regs *regs)
285 struct undef_hook *hook;
288 int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
289 void __user *pc = (void __user *)instruction_pointer(regs);
291 if (!user_mode(regs))
294 if (compat_thumb_mode(regs)) {
295 /* 16-bit Thumb instruction */
296 if (get_user(instr, (u16 __user *)pc))
298 instr = le16_to_cpu(instr);
299 if (aarch32_insn_is_wide(instr)) {
302 if (get_user(instr2, (u16 __user *)(pc + 2)))
304 instr2 = le16_to_cpu(instr2);
305 instr = (instr << 16) | instr2;
308 /* 32-bit ARM instruction */
309 if (get_user(instr, (u32 __user *)pc))
311 instr = le32_to_cpu(instr);
314 raw_spin_lock_irqsave(&undef_lock, flags);
315 list_for_each_entry(hook, &undef_hook, node)
316 if ((instr & hook->instr_mask) == hook->instr_val &&
317 (regs->pstate & hook->pstate_mask) == hook->pstate_val)
320 raw_spin_unlock_irqrestore(&undef_lock, flags);
322 return fn ? fn(regs, instr) : 1;
325 asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
328 void __user *pc = (void __user *)instruction_pointer(regs);
330 /* check for AArch32 breakpoint instructions */
331 if (!aarch32_break_handler(regs))
334 if (call_undef_hook(regs) == 0)
337 if (show_unhandled_signals && unhandled_signal(current, SIGILL) &&
338 printk_ratelimit()) {
339 pr_info("%s[%d]: undefined instruction: pc=%p\n",
340 current->comm, task_pid_nr(current), pc);
341 dump_instr(KERN_INFO, regs);
344 info.si_signo = SIGILL;
346 info.si_code = ILL_ILLOPC;
349 arm64_notify_die("Oops - undefined instruction", regs, &info, 0);
352 long compat_arm_syscall(struct pt_regs *regs);
354 asmlinkage long do_ni_syscall(struct pt_regs *regs)
358 if (is_compat_task()) {
359 ret = compat_arm_syscall(regs);
365 if (show_unhandled_signals && printk_ratelimit()) {
366 pr_info("%s[%d]: syscall %d\n", current->comm,
367 task_pid_nr(current), (int)regs->syscallno);
368 dump_instr("", regs);
373 return sys_ni_syscall();
377 * bad_mode handles the impossible case in the exception vector.
379 asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
382 void __user *pc = (void __user *)instruction_pointer(regs);
385 pr_crit("Bad mode in %s handler detected, code 0x%08x\n",
386 handler[reason], esr);
389 info.si_signo = SIGILL;
391 info.si_code = ILL_ILLOPC;
394 arm64_notify_die("Oops - bad mode", regs, &info, 0);
397 void __pte_error(const char *file, int line, unsigned long val)
399 pr_crit("%s:%d: bad pte %016lx.\n", file, line, val);
402 void __pmd_error(const char *file, int line, unsigned long val)
404 pr_crit("%s:%d: bad pmd %016lx.\n", file, line, val);
407 void __pud_error(const char *file, int line, unsigned long val)
409 pr_crit("%s:%d: bad pud %016lx.\n", file, line, val);
412 void __pgd_error(const char *file, int line, unsigned long val)
414 pr_crit("%s:%d: bad pgd %016lx.\n", file, line, val);
417 void __init trap_init(void)