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