import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / arm64 / kernel / traps.c
1 /*
2 * Based on arch/arm/kernel/traps.c
3 *
4 * Copyright (C) 1995-2009 Russell King
5 * Copyright (C) 2012 ARM Ltd.
6 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
7 *
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.
11 *
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.
16 *
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/>.
19 */
20
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>
34
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>
42
43 static const char *handler[]= {
44 "Synchronous Abort",
45 "IRQ",
46 "FIQ",
47 "Error"
48 };
49
50 int show_unhandled_signals = 1;
51
52 /*
53 * Dump out the contents of some memory nicely...
54 */
55 static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
56 unsigned long top)
57 {
58 unsigned long first;
59 mm_segment_t fs;
60 int i;
61
62 /*
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.
66 */
67 fs = get_fs();
68 set_fs(KERNEL_DS);
69
70 printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
71
72 for (first = bottom & ~31; first < top; first += 32) {
73 unsigned long p;
74 char str[sizeof(" 12345678") * 8 + 1];
75
76 memset(str, ' ', sizeof(str));
77 str[sizeof(str) - 1] = '\0';
78
79 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
80 if (p >= bottom && p < top) {
81 unsigned int val;
82 if (__get_user(val, (unsigned int *)p) == 0)
83 sprintf(str + i * 9, " %08x", val);
84 else
85 sprintf(str + i * 9, " ????????");
86 }
87 }
88 printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
89 }
90
91 set_fs(fs);
92 }
93
94 static void dump_backtrace_entry(unsigned long where, unsigned long stack)
95 {
96 print_ip_sym(where);
97 if (in_exception_text(where))
98 dump_mem("", "Exception stack", stack,
99 stack + sizeof(struct pt_regs));
100 }
101
102 static void dump_instr(const char *lvl, struct pt_regs *regs)
103 {
104 unsigned long addr = instruction_pointer(regs);
105 mm_segment_t fs;
106 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
107 int i;
108
109 /*
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.
113 */
114 fs = get_fs();
115 set_fs(KERNEL_DS);
116
117 for (i = -4; i < 1; i++) {
118 unsigned int val, bad;
119
120 bad = __get_user(val, &((u32 *)addr)[i]);
121
122 if (!bad)
123 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
124 else {
125 p += sprintf(p, "bad PC value");
126 break;
127 }
128 }
129 printk("%sCode: %s\n", lvl, str);
130
131 set_fs(fs);
132 }
133
134 static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
135 {
136 struct stackframe frame;
137 const register unsigned long current_sp asm ("sp");
138
139 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
140
141 if (!tsk)
142 tsk = current;
143
144 if (regs) {
145 frame.fp = regs->regs[29];
146 frame.sp = regs->sp;
147 frame.pc = regs->pc;
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;
152 } else {
153 /*
154 * task blocked in __switch_to
155 */
156 frame.fp = thread_saved_fp(tsk);
157 frame.sp = thread_saved_sp(tsk);
158 frame.pc = thread_saved_pc(tsk);
159 }
160
161 printk("Call trace:\n");
162 while (1) {
163 unsigned long where = frame.pc;
164 int ret;
165
166 ret = unwind_frame(&frame);
167 if (ret < 0)
168 break;
169 dump_backtrace_entry(where, frame.sp);
170 }
171 }
172
173 void show_stack(struct task_struct *tsk, unsigned long *sp)
174 {
175 dump_backtrace(NULL, tsk);
176 barrier();
177 }
178
179 #ifdef CONFIG_PREEMPT
180 #define S_PREEMPT " PREEMPT"
181 #else
182 #define S_PREEMPT ""
183 #endif
184 #ifdef CONFIG_SMP
185 #define S_SMP " SMP"
186 #else
187 #define S_SMP ""
188 #endif
189
190 static int __die(const char *str, int err, struct thread_info *thread,
191 struct pt_regs *regs)
192 {
193 unsigned long sp, stack;
194 struct task_struct *tsk = thread->task;
195 static int die_counter;
196 int ret;
197
198 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
199 str, err, ++die_counter);
200
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)
204 return ret;
205
206 print_modules();
207 __show_regs(regs);
208 pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
209 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
210
211 if (!user_mode(regs) || in_interrupt()) {
212 sp = regs->sp;
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);
218 }
219 dump_backtrace(regs, tsk);
220 dump_instr(KERN_EMERG, regs);
221 }
222
223 return ret;
224 }
225
226 static DEFINE_RAW_SPINLOCK(die_lock);
227
228 /*
229 * This function is protected against re-entrancy.
230 */
231 void die(const char *str, struct pt_regs *regs, int err)
232 {
233 struct thread_info *thread = current_thread_info();
234 int ret;
235
236 oops_enter();
237
238 raw_spin_lock_irq(&die_lock);
239 console_verbose();
240 bust_spinlocks(1);
241 ret = __die(str, err, thread, regs);
242
243 if (regs && kexec_should_crash(thread->task))
244 crash_kexec(regs);
245
246 bust_spinlocks(0);
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);
250 oops_exit();
251
252 if (in_interrupt())
253 panic("Fatal exception in interrupt");
254 if (panic_on_oops)
255 panic("Fatal exception");
256 if (ret != NOTIFY_STOP)
257 do_exit(SIGSEGV);
258 }
259
260 void arm64_notify_die(const char *str, struct pt_regs *regs,
261 struct siginfo *info, int err)
262 {
263 if (user_mode(regs))
264 force_sig_info(info->si_signo, info, current);
265 else
266 die(str, regs, err);
267 }
268
269 static LIST_HEAD(undef_hook);
270
271 void register_undef_hook(struct undef_hook *hook)
272 {
273 list_add(&hook->node, &undef_hook);
274 }
275
276 static int call_undef_hook(struct pt_regs *regs, unsigned int instr)
277 {
278 struct undef_hook *hook;
279 int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;
280
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)
284 fn = hook->fn;
285
286 return fn ? fn(regs, instr) : 1;
287 }
288
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;
292
293 asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
294 {
295 u32 instr;
296 siginfo_t info;
297 void __user *pc = (void __user *)instruction_pointer(regs);
298 struct thread_info *thread = current_thread_info();
299
300 /* check for AArch32 breakpoint instructions */
301 if (!aarch32_break_handler(regs))
302 return;
303 if (user_mode(regs)) {
304 if (compat_thumb_mode(regs)) {
305 if (get_user(instr, (u16 __user *)pc))
306 goto die_sig;
307 if (is_wide_instruction(instr)) {
308 u32 instr2;
309 if (get_user(instr2, (u16 __user *)pc+1))
310 goto die_sig;
311 instr <<= 16;
312 instr |= instr2;
313 }
314 } else if (get_user(instr, (u32 __user *)pc)) {
315 goto die_sig;
316 }
317 } else {
318 /* kernel mode */
319 instr = *((u32 *)pc);
320 }
321
322 if (call_undef_hook(regs, instr) == 0)
323 return;
324
325 die_sig:
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);
331 }
332
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();
347 flush_cache_all();
348 /*
349 * undo cpu_excp to cancel nest_panic code, see entry.S
350 */
351 if (!user_mode(regs)) {
352 thread->cpu_excp--;
353 }
354 return;
355 }
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();
361 flush_cache_all();
362 /*
363 * undo cpu_excp to cancel nest_panic code, see entry.S
364 */
365 if (!user_mode(regs)) {
366 thread->cpu_excp--;
367 }
368 return;
369 }
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;
376 }
377 pr_alert("Go to ARM Notify Die curr:%p pc=%p:%p\n",
378 (void *)current, (void *)pc, (void *)prev_undefinstr_pc);
379 }
380
381 info.si_signo = SIGILL;
382 info.si_errno = 0;
383 info.si_code = ILL_ILLOPC;
384 info.si_addr = pc;
385
386 arm64_notify_die("Oops - undefined instruction", regs, &info, 0);
387 }
388
389 long compat_arm_syscall(struct pt_regs *regs);
390
391 asmlinkage long do_ni_syscall(struct pt_regs *regs)
392 {
393 #ifdef CONFIG_COMPAT
394 long ret;
395 if (is_compat_task()) {
396 ret = compat_arm_syscall(regs);
397 if (ret != -ENOSYS)
398 return ret;
399 }
400 #endif
401
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);
406 if (user_mode(regs))
407 __show_regs(regs);
408 }
409
410 return sys_ni_syscall();
411 }
412
413 #ifdef CONFIG_MEDIATEK_SOLUTION
414 static void (*async_abort_handler)(struct pt_regs *regs, void *);
415 static void *async_abort_priv;
416
417 int register_async_abort_handler(void (*fn)(struct pt_regs *regs, void *), void *priv)
418 {
419 async_abort_handler = fn;
420 async_abort_priv = priv;
421
422 return 0;
423 }
424 #endif
425
426 /*
427 * bad_mode handles the impossible case in the exception vector.
428 */
429 asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
430 {
431 siginfo_t info;
432 void __user *pc = (void __user *)instruction_pointer(regs);
433 console_verbose();
434
435 #ifdef CONFIG_MEDIATEK_SOLUTION
436 /*
437 * reason is defined in entry.S, 3 means BAD_ERROR,
438 * which would be triggered by async abort
439 */
440 if ((reason == 3) && async_abort_handler) {
441 async_abort_handler(regs, async_abort_priv);
442 }
443 #endif
444 pr_crit("Bad mode in %s handler detected, code 0x%08x\n",
445 handler[reason], esr);
446 __show_regs(regs);
447
448 info.si_signo = SIGILL;
449 info.si_errno = 0;
450 info.si_code = ILL_ILLOPC;
451 info.si_addr = pc;
452
453 arm64_notify_die("Oops - bad mode", regs, &info, 0);
454 }
455
456 void __pte_error(const char *file, int line, unsigned long val)
457 {
458 printk("%s:%d: bad pte %016lx.\n", file, line, val);
459 }
460
461 void __pmd_error(const char *file, int line, unsigned long val)
462 {
463 printk("%s:%d: bad pmd %016lx.\n", file, line, val);
464 }
465
466 void __pgd_error(const char *file, int line, unsigned long val)
467 {
468 printk("%s:%d: bad pgd %016lx.\n", file, line, val);
469 }
470
471 void __init trap_init(void)
472 {
473 return;
474 }