2 * Based on arch/arm/kernel/traps.c
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
6 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/signal.h>
22 #include <linux/personality.h>
23 #include <linux/kallsyms.h>
24 #include <linux/spinlock.h>
25 #include <linux/uaccess.h>
26 #include <linux/hardirq.h>
27 #include <linux/kdebug.h>
28 #include <linux/module.h>
29 #include <linux/kexec.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/sched.h>
33 #include <linux/syscalls.h>
35 #include <asm/atomic.h>
36 #include <asm/debug-monitors.h>
37 #include <asm/traps.h>
38 #include <asm/stacktrace.h>
39 #include <asm/exception.h>
40 #include <asm/system_misc.h>
41 #include <asm/cacheflush.h>
43 static const char *handler
[]= {
50 int show_unhandled_signals
= 1;
53 * Dump out the contents of some memory nicely...
55 static void dump_mem(const char *lvl
, const char *str
, unsigned long bottom
,
63 * We need to switch to kernel mode so that we can use __get_user
64 * to safely read from kernel space. Note that we now dump the
65 * code first, just in case the backtrace kills us.
70 printk("%s%s(0x%016lx to 0x%016lx)\n", lvl
, str
, bottom
, top
);
72 for (first
= bottom
& ~31; first
< top
; first
+= 32) {
74 char str
[sizeof(" 12345678") * 8 + 1];
76 memset(str
, ' ', sizeof(str
));
77 str
[sizeof(str
) - 1] = '\0';
79 for (p
= first
, i
= 0; i
< 8 && p
< top
; i
++, p
+= 4) {
80 if (p
>= bottom
&& p
< top
) {
82 if (__get_user(val
, (unsigned int *)p
) == 0)
83 sprintf(str
+ i
* 9, " %08x", val
);
85 sprintf(str
+ i
* 9, " ????????");
88 printk("%s%04lx:%s\n", lvl
, first
& 0xffff, str
);
94 static void dump_backtrace_entry(unsigned long where
, unsigned long stack
)
97 if (in_exception_text(where
))
98 dump_mem("", "Exception stack", stack
,
99 stack
+ sizeof(struct pt_regs
));
102 static void dump_instr(const char *lvl
, struct pt_regs
*regs
)
104 unsigned long addr
= instruction_pointer(regs
);
106 char str
[sizeof("00000000 ") * 5 + 2 + 1], *p
= str
;
110 * We need to switch to kernel mode so that we can use __get_user
111 * to safely read from kernel space. Note that we now dump the
112 * code first, just in case the backtrace kills us.
117 for (i
= -4; i
< 1; i
++) {
118 unsigned int val
, bad
;
120 bad
= __get_user(val
, &((u32
*)addr
)[i
]);
123 p
+= sprintf(p
, i
== 0 ? "(%08x) " : "%08x ", val
);
125 p
+= sprintf(p
, "bad PC value");
129 printk("%sCode: %s\n", lvl
, str
);
134 static void dump_backtrace(struct pt_regs
*regs
, struct task_struct
*tsk
)
136 struct stackframe frame
;
137 const register unsigned long current_sp
asm ("sp");
139 pr_debug("%s(regs = %p tsk = %p)\n", __func__
, regs
, tsk
);
145 frame
.fp
= regs
->regs
[29];
148 } else if (tsk
== current
) {
149 frame
.fp
= (unsigned long)__builtin_frame_address(0);
150 frame
.sp
= current_sp
;
151 frame
.pc
= (unsigned long)dump_backtrace
;
154 * task blocked in __switch_to
156 frame
.fp
= thread_saved_fp(tsk
);
157 frame
.sp
= thread_saved_sp(tsk
);
158 frame
.pc
= thread_saved_pc(tsk
);
161 printk("Call trace:\n");
163 unsigned long where
= frame
.pc
;
166 ret
= unwind_frame(&frame
);
169 dump_backtrace_entry(where
, frame
.sp
);
173 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
175 dump_backtrace(NULL
, tsk
);
179 #ifdef CONFIG_PREEMPT
180 #define S_PREEMPT " PREEMPT"
190 static int __die(const char *str
, int err
, struct thread_info
*thread
,
191 struct pt_regs
*regs
)
193 unsigned long sp
, stack
;
194 struct task_struct
*tsk
= thread
->task
;
195 static int die_counter
;
198 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP
"\n",
199 str
, err
, ++die_counter
);
201 /* trap and error numbers are mostly meaningless on ARM */
202 ret
= notify_die(DIE_OOPS
, str
, regs
, err
, 0, SIGSEGV
);
203 if (ret
== NOTIFY_STOP
)
208 pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
209 TASK_COMM_LEN
, tsk
->comm
, task_pid_nr(tsk
), thread
+ 1);
211 if (!user_mode(regs
) || in_interrupt()) {
213 stack
= (unsigned long)task_stack_page(tsk
);
214 dump_mem(KERN_EMERG
, "Stack: ", sp
, ALIGN(sp
, THREAD_SIZE
));
215 if (sp
< stack
|| (sp
- stack
) > THREAD_SIZE
) {
216 printk(KERN_EMERG
"Invalid sp[%lx] or stack address[%lx]\n", sp
, stack
);
217 dump_mem(KERN_EMERG
, "Stack(backup) ", stack
, THREAD_SIZE
+ stack
);
219 dump_backtrace(regs
, tsk
);
220 dump_instr(KERN_EMERG
, regs
);
226 static DEFINE_RAW_SPINLOCK(die_lock
);
229 * This function is protected against re-entrancy.
231 void die(const char *str
, struct pt_regs
*regs
, int err
)
233 struct thread_info
*thread
= current_thread_info();
238 raw_spin_lock_irq(&die_lock
);
241 ret
= __die(str
, err
, thread
, regs
);
243 if (regs
&& kexec_should_crash(thread
->task
))
247 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
248 /* keep preemption/irq disabled in KE flow to prevent context switch*/
249 //raw_spin_unlock_irq(&die_lock);
253 panic("Fatal exception in interrupt");
255 panic("Fatal exception");
256 if (ret
!= NOTIFY_STOP
)
260 void arm64_notify_die(const char *str
, struct pt_regs
*regs
,
261 struct siginfo
*info
, int err
)
264 force_sig_info(info
->si_signo
, info
, current
);
269 static LIST_HEAD(undef_hook
);
271 void register_undef_hook(struct undef_hook
*hook
)
273 list_add(&hook
->node
, &undef_hook
);
276 static int call_undef_hook(struct pt_regs
*regs
, unsigned int instr
)
278 struct undef_hook
*hook
;
279 int (*fn
)(struct pt_regs
*regs
, unsigned int instr
) = NULL
;
281 list_for_each_entry(hook
, &undef_hook
, node
)
282 if ((instr
& hook
->instr_mask
) == hook
->instr_val
&&
283 (regs
->pstate
& hook
->pstate_mask
) == hook
->pstate_val
)
286 return fn
? fn(regs
, instr
) : 1;
289 volatile static void __user
*prev_undefinstr_pc
=0;
290 volatile static int prev_undefinstr_counter
=0;
291 volatile static unsigned long prev_undefinstr_curr
=0;
293 asmlinkage
void __exception
do_undefinstr(struct pt_regs
*regs
)
297 void __user
*pc
= (void __user
*)instruction_pointer(regs
);
298 struct thread_info
*thread
= current_thread_info();
300 /* check for AArch32 breakpoint instructions */
301 if (!aarch32_break_handler(regs
))
303 if (user_mode(regs
)) {
304 if (compat_thumb_mode(regs
)) {
305 if (get_user(instr
, (u16 __user
*)pc
))
307 if (is_wide_instruction(instr
)) {
309 if (get_user(instr2
, (u16 __user
*)pc
+1))
314 } else if (get_user(instr
, (u32 __user
*)pc
)) {
319 instr
= *((u32
*)pc
);
322 if (call_undef_hook(regs
, instr
) == 0)
326 if (show_unhandled_signals
&& unhandled_signal(current
, SIGILL
) &&
327 printk_ratelimit()) {
328 pr_info("%s[%d]: undefined instruction: pc=%p\n",
329 current
->comm
, task_pid_nr(current
), pc
);
330 dump_instr(KERN_INFO
, regs
);
333 /* Place the SIGILL ICache Invalidate after the Debugger Undefined-Instruction Solution. */
334 if ((user_mode(regs
)) || processor_mode(regs
) == PSR_MODE_EL1h
) {
335 /* Only do it for User-Space Application. */
336 pr_alert("USR_MODE / SVC_MODE Undefined Instruction Address curr:%p pc=%p:%p, compat: %s\n",
337 (void *)current
, (void *)pc
, (void *)prev_undefinstr_pc
,
338 is_compat_task() ? "yes" : "no");
339 if ((prev_undefinstr_pc
!= pc
) || (prev_undefinstr_curr
!= (unsigned long)current
)) {
340 /* If the current process or program counter is changed......renew the counter. */
341 pr_alert("First Time Recovery curr:%p pc=%p:%p\n",
342 (void *)current
, (void *)pc
, (void *)prev_undefinstr_pc
);
343 prev_undefinstr_pc
= pc
;
344 prev_undefinstr_curr
= (unsigned long)current
;
345 prev_undefinstr_counter
= 0;
346 __flush_icache_all();
349 * undo cpu_excp to cancel nest_panic code, see entry.S
351 if (!user_mode(regs
)) {
356 else if(prev_undefinstr_counter
< 1) {
357 pr_alert("2nd Time Recovery curr:%p pc=%p:%p\n",
358 (void *)current
, (void *)pc
, (void *)prev_undefinstr_pc
);
359 prev_undefinstr_counter
++;
360 __flush_icache_all();
363 * undo cpu_excp to cancel nest_panic code, see entry.S
365 if (!user_mode(regs
)) {
370 prev_undefinstr_counter
++;
371 if(prev_undefinstr_counter
>= 4) {
372 /* 2=first time SigILL,3=2nd time NE-SigILL,4=3rd time CoreDump-SigILL */
373 prev_undefinstr_pc
= 0;
374 prev_undefinstr_curr
= 0;
375 prev_undefinstr_counter
= 0;
377 pr_alert("Go to ARM Notify Die curr:%p pc=%p:%p\n",
378 (void *)current
, (void *)pc
, (void *)prev_undefinstr_pc
);
381 info
.si_signo
= SIGILL
;
383 info
.si_code
= ILL_ILLOPC
;
386 arm64_notify_die("Oops - undefined instruction", regs
, &info
, 0);
389 long compat_arm_syscall(struct pt_regs
*regs
);
391 asmlinkage
long do_ni_syscall(struct pt_regs
*regs
)
395 if (is_compat_task()) {
396 ret
= compat_arm_syscall(regs
);
402 if (show_unhandled_signals
&& printk_ratelimit()) {
403 pr_info("%s[%d]: syscall %d\n", current
->comm
,
404 task_pid_nr(current
), (int)regs
->syscallno
);
405 dump_instr("", regs
);
410 return sys_ni_syscall();
413 #ifdef CONFIG_MEDIATEK_SOLUTION
414 static void (*async_abort_handler
)(struct pt_regs
*regs
, void *);
415 static void *async_abort_priv
;
417 int register_async_abort_handler(void (*fn
)(struct pt_regs
*regs
, void *), void *priv
)
419 async_abort_handler
= fn
;
420 async_abort_priv
= priv
;
427 * bad_mode handles the impossible case in the exception vector.
429 asmlinkage
void bad_mode(struct pt_regs
*regs
, int reason
, unsigned int esr
)
432 void __user
*pc
= (void __user
*)instruction_pointer(regs
);
435 #ifdef CONFIG_MEDIATEK_SOLUTION
437 * reason is defined in entry.S, 3 means BAD_ERROR,
438 * which would be triggered by async abort
440 if ((reason
== 3) && async_abort_handler
) {
441 async_abort_handler(regs
, async_abort_priv
);
444 pr_crit("Bad mode in %s handler detected, code 0x%08x\n",
445 handler
[reason
], esr
);
448 info
.si_signo
= SIGILL
;
450 info
.si_code
= ILL_ILLOPC
;
453 arm64_notify_die("Oops - bad mode", regs
, &info
, 0);
456 void __pte_error(const char *file
, int line
, unsigned long val
)
458 printk("%s:%d: bad pte %016lx.\n", file
, line
, val
);
461 void __pmd_error(const char *file
, int line
, unsigned long val
)
463 printk("%s:%d: bad pmd %016lx.\n", file
, line
, val
);
466 void __pgd_error(const char *file
, int line
, unsigned long val
)
468 printk("%s:%d: bad pgd %016lx.\n", file
, line
, val
);
471 void __init
trap_init(void)