]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/arm64/kernel/traps.c
arm64: syscallno is secretly an int, make it official
[mirror_ubuntu-artful-kernel.git] / arch / arm64 / kernel / traps.c
CommitLineData
60ffc30d
CM
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
9fb7410f 20#include <linux/bug.h>
60ffc30d
CM
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>
3f07c014 32#include <linux/sched/signal.h>
b17b0153 33#include <linux/sched/debug.h>
68db0cf1 34#include <linux/sched/task_stack.h>
901212c5 35#include <linux/sizes.h>
60ffc30d 36#include <linux/syscalls.h>
589ee628 37#include <linux/mm_types.h>
60ffc30d
CM
38
39#include <asm/atomic.h>
9fb7410f 40#include <asm/bug.h>
1442b6ed 41#include <asm/debug-monitors.h>
60a1f02c 42#include <asm/esr.h>
9fb7410f 43#include <asm/insn.h>
60ffc30d 44#include <asm/traps.h>
901212c5 45#include <asm/smp.h>
a9ea0017 46#include <asm/stack_pointer.h>
60ffc30d
CM
47#include <asm/stacktrace.h>
48#include <asm/exception.h>
49#include <asm/system_misc.h>
7dd01aef 50#include <asm/sysreg.h>
60ffc30d
CM
51
52static const char *handler[]= {
53 "Synchronous Abort",
54 "IRQ",
55 "FIQ",
56 "Error"
57};
58
59int show_unhandled_signals = 1;
60
61/*
7ceb3a10 62 * Dump out the contents of some kernel memory nicely...
60ffc30d
CM
63 */
64static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
7ceb3a10 65 unsigned long top)
60ffc30d
CM
66{
67 unsigned long first;
68 mm_segment_t fs;
69 int i;
70
71 /*
72 * We need to switch to kernel mode so that we can use __get_user
c5cea06b 73 * to safely read from kernel space.
60ffc30d
CM
74 */
75 fs = get_fs();
76 set_fs(KERNEL_DS);
77
78 printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
79
80 for (first = bottom & ~31; first < top; first += 32) {
81 unsigned long p;
82 char str[sizeof(" 12345678") * 8 + 1];
83
84 memset(str, ' ', sizeof(str));
85 str[sizeof(str) - 1] = '\0';
86
7ceb3a10
MR
87 for (p = first, i = 0; i < (32 / 8)
88 && p < top; i++, p += 8) {
60ffc30d 89 if (p >= bottom && p < top) {
e147ae6d
RT
90 unsigned long val;
91
7ceb3a10
MR
92 if (__get_user(val, (unsigned long *)p) == 0)
93 sprintf(str + i * 17, " %016lx", val);
94 else
95 sprintf(str + i * 17, " ????????????????");
60ffc30d
CM
96 }
97 }
98 printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
99 }
100
101 set_fs(fs);
102}
103
9f93f3e9 104static void dump_backtrace_entry(unsigned long where)
60ffc30d 105{
9f93f3e9
JL
106 /*
107 * Note that 'where' can have a physical address, but it's not handled.
108 */
60ffc30d 109 print_ip_sym(where);
60ffc30d
CM
110}
111
c5cea06b 112static void __dump_instr(const char *lvl, struct pt_regs *regs)
60ffc30d
CM
113{
114 unsigned long addr = instruction_pointer(regs);
60ffc30d
CM
115 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
116 int i;
117
60ffc30d
CM
118 for (i = -4; i < 1; i++) {
119 unsigned int val, bad;
120
d5b2d1cb 121 bad = get_user(val, &((u32 *)addr)[i]);
60ffc30d
CM
122
123 if (!bad)
124 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
125 else {
126 p += sprintf(p, "bad PC value");
127 break;
128 }
129 }
130 printk("%sCode: %s\n", lvl, str);
c5cea06b 131}
60ffc30d 132
c5cea06b
MR
133static void dump_instr(const char *lvl, struct pt_regs *regs)
134{
135 if (!user_mode(regs)) {
136 mm_segment_t fs = get_fs();
137 set_fs(KERNEL_DS);
138 __dump_instr(lvl, regs);
139 set_fs(fs);
140 } else {
141 __dump_instr(lvl, regs);
142 }
60ffc30d
CM
143}
144
1149aad1 145void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
60ffc30d
CM
146{
147 struct stackframe frame;
20380bb3 148 int skip;
60ffc30d 149
b5e7307d
MR
150 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
151
152 if (!tsk)
153 tsk = current;
154
9bbd4c56
MR
155 if (!try_get_task_stack(tsk))
156 return;
157
20380bb3 158 if (tsk == current) {
60ffc30d 159 frame.fp = (unsigned long)__builtin_frame_address(0);
60ffc30d
CM
160 frame.pc = (unsigned long)dump_backtrace;
161 } else {
162 /*
163 * task blocked in __switch_to
164 */
165 frame.fp = thread_saved_fp(tsk);
60ffc30d
CM
166 frame.pc = thread_saved_pc(tsk);
167 }
20380bb3
AT
168#ifdef CONFIG_FUNCTION_GRAPH_TRACER
169 frame.graph = tsk->curr_ret_stack;
170#endif
60ffc30d 171
20380bb3 172 skip = !!regs;
c9cd0ed9 173 printk("Call trace:\n");
60ffc30d 174 while (1) {
9f93f3e9 175 unsigned long stack;
60ffc30d
CM
176 int ret;
177
20380bb3
AT
178 /* skip until specified stack frame */
179 if (!skip) {
c40c40d2 180 dump_backtrace_entry(frame.pc);
20380bb3
AT
181 } else if (frame.fp == regs->regs[29]) {
182 skip = 0;
183 /*
184 * Mostly, this is the case where this function is
185 * called in panic/abort. As exception handler's
186 * stack frame does not contain the corresponding pc
187 * at which an exception has taken place, use regs->pc
188 * instead.
189 */
190 dump_backtrace_entry(regs->pc);
191 }
fe13f95b 192 ret = unwind_frame(tsk, &frame);
60ffc30d
CM
193 if (ret < 0)
194 break;
c40c40d2
AB
195 if (in_entry_text(frame.pc)) {
196 stack = frame.fp - offsetof(struct pt_regs, stackframe);
132cd887 197
5a44e3d8 198 if (on_accessible_stack(tsk, stack))
c40c40d2
AB
199 dump_mem("", "Exception stack", stack,
200 stack + sizeof(struct pt_regs));
132cd887 201 }
60ffc30d 202 }
9bbd4c56
MR
203
204 put_task_stack(tsk);
60ffc30d
CM
205}
206
60ffc30d
CM
207void show_stack(struct task_struct *tsk, unsigned long *sp)
208{
209 dump_backtrace(NULL, tsk);
210 barrier();
211}
212
213#ifdef CONFIG_PREEMPT
214#define S_PREEMPT " PREEMPT"
215#else
216#define S_PREEMPT ""
217#endif
60ffc30d 218#define S_SMP " SMP"
60ffc30d 219
876e7a38 220static int __die(const char *str, int err, struct pt_regs *regs)
60ffc30d 221{
876e7a38 222 struct task_struct *tsk = current;
60ffc30d
CM
223 static int die_counter;
224 int ret;
225
226 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
227 str, err, ++die_counter);
228
229 /* trap and error numbers are mostly meaningless on ARM */
230 ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
231 if (ret == NOTIFY_STOP)
232 return ret;
233
234 print_modules();
235 __show_regs(regs);
236 pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
876e7a38
MR
237 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk),
238 end_of_stack(tsk));
60ffc30d 239
7ceb3a10 240 if (!user_mode(regs)) {
60ffc30d 241 dump_mem(KERN_EMERG, "Stack: ", regs->sp,
7ceb3a10 242 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
60ffc30d
CM
243 dump_backtrace(regs, tsk);
244 dump_instr(KERN_EMERG, regs);
245 }
246
247 return ret;
248}
249
250static DEFINE_RAW_SPINLOCK(die_lock);
251
252/*
253 * This function is protected against re-entrancy.
254 */
255void die(const char *str, struct pt_regs *regs, int err)
256{
60ffc30d 257 int ret;
6f44a0ba
QZ
258 unsigned long flags;
259
260 raw_spin_lock_irqsave(&die_lock, flags);
60ffc30d
CM
261
262 oops_enter();
263
60ffc30d
CM
264 console_verbose();
265 bust_spinlocks(1);
876e7a38 266 ret = __die(str, err, regs);
60ffc30d 267
876e7a38 268 if (regs && kexec_should_crash(current))
60ffc30d
CM
269 crash_kexec(regs);
270
271 bust_spinlocks(0);
373d4d09 272 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
60ffc30d
CM
273 oops_exit();
274
275 if (in_interrupt())
276 panic("Fatal exception in interrupt");
277 if (panic_on_oops)
278 panic("Fatal exception");
6f44a0ba
QZ
279
280 raw_spin_unlock_irqrestore(&die_lock, flags);
281
60ffc30d
CM
282 if (ret != NOTIFY_STOP)
283 do_exit(SIGSEGV);
284}
285
286void arm64_notify_die(const char *str, struct pt_regs *regs,
287 struct siginfo *info, int err)
288{
9141300a
CM
289 if (user_mode(regs)) {
290 current->thread.fault_address = 0;
291 current->thread.fault_code = err;
60ffc30d 292 force_sig_info(info->si_signo, info, current);
9141300a 293 } else {
60ffc30d 294 die(str, regs, err);
9141300a 295 }
60ffc30d
CM
296}
297
9b79f52d
PA
298static LIST_HEAD(undef_hook);
299static DEFINE_RAW_SPINLOCK(undef_lock);
300
301void register_undef_hook(struct undef_hook *hook)
302{
303 unsigned long flags;
304
305 raw_spin_lock_irqsave(&undef_lock, flags);
306 list_add(&hook->node, &undef_hook);
307 raw_spin_unlock_irqrestore(&undef_lock, flags);
308}
309
310void unregister_undef_hook(struct undef_hook *hook)
311{
312 unsigned long flags;
313
314 raw_spin_lock_irqsave(&undef_lock, flags);
315 list_del(&hook->node);
316 raw_spin_unlock_irqrestore(&undef_lock, flags);
317}
318
319static int call_undef_hook(struct pt_regs *regs)
320{
321 struct undef_hook *hook;
322 unsigned long flags;
323 u32 instr;
324 int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
325 void __user *pc = (void __user *)instruction_pointer(regs);
326
327 if (!user_mode(regs))
328 return 1;
329
330 if (compat_thumb_mode(regs)) {
331 /* 16-bit Thumb instruction */
6cf5d4af
LVO
332 __le16 instr_le;
333 if (get_user(instr_le, (__le16 __user *)pc))
9b79f52d 334 goto exit;
6cf5d4af 335 instr = le16_to_cpu(instr_le);
9b79f52d
PA
336 if (aarch32_insn_is_wide(instr)) {
337 u32 instr2;
338
6cf5d4af 339 if (get_user(instr_le, (__le16 __user *)(pc + 2)))
9b79f52d 340 goto exit;
6cf5d4af 341 instr2 = le16_to_cpu(instr_le);
9b79f52d
PA
342 instr = (instr << 16) | instr2;
343 }
344 } else {
345 /* 32-bit ARM instruction */
6cf5d4af
LVO
346 __le32 instr_le;
347 if (get_user(instr_le, (__le32 __user *)pc))
9b79f52d 348 goto exit;
6cf5d4af 349 instr = le32_to_cpu(instr_le);
9b79f52d
PA
350 }
351
352 raw_spin_lock_irqsave(&undef_lock, flags);
353 list_for_each_entry(hook, &undef_hook, node)
354 if ((instr & hook->instr_mask) == hook->instr_val &&
355 (regs->pstate & hook->pstate_mask) == hook->pstate_val)
356 fn = hook->fn;
357
358 raw_spin_unlock_irqrestore(&undef_lock, flags);
359exit:
360 return fn ? fn(regs, instr) : 1;
361}
362
390bf177
AP
363static void force_signal_inject(int signal, int code, struct pt_regs *regs,
364 unsigned long address)
60ffc30d
CM
365{
366 siginfo_t info;
367 void __user *pc = (void __user *)instruction_pointer(regs);
390bf177
AP
368 const char *desc;
369
370 switch (signal) {
371 case SIGILL:
372 desc = "undefined instruction";
373 break;
374 case SIGSEGV:
375 desc = "illegal memory access";
376 break;
377 default:
378 desc = "bad mode";
379 break;
380 }
381
382 if (unhandled_signal(current, signal) &&
383 show_unhandled_signals_ratelimited()) {
384 pr_info("%s[%d]: %s: pc=%p\n",
385 current->comm, task_pid_nr(current), desc, pc);
386 dump_instr(KERN_INFO, regs);
387 }
388
389 info.si_signo = signal;
390 info.si_errno = 0;
391 info.si_code = code;
392 info.si_addr = pc;
393
394 arm64_notify_die(desc, regs, &info, 0);
395}
396
397/*
398 * Set up process info to signal segmentation fault - called on access error.
399 */
400void arm64_notify_segfault(struct pt_regs *regs, unsigned long addr)
401{
402 int code;
403
404 down_read(&current->mm->mmap_sem);
405 if (find_vma(current->mm, addr) == NULL)
406 code = SEGV_MAPERR;
407 else
408 code = SEGV_ACCERR;
409 up_read(&current->mm->mmap_sem);
60ffc30d 410
390bf177
AP
411 force_signal_inject(SIGSEGV, code, regs, addr);
412}
413
414asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
415{
60ffc30d 416 /* check for AArch32 breakpoint instructions */
1442b6ed 417 if (!aarch32_break_handler(regs))
60ffc30d 418 return;
60ffc30d 419
9b79f52d
PA
420 if (call_undef_hook(regs) == 0)
421 return;
422
390bf177 423 force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
60ffc30d
CM
424}
425
2a6dcb2b 426int cpu_enable_cache_maint_trap(void *__unused)
7dd01aef
AP
427{
428 config_sctlr_el1(SCTLR_EL1_UCI, 0);
2a6dcb2b 429 return 0;
7dd01aef
AP
430}
431
432#define __user_cache_maint(insn, address, res) \
81cddd65 433 if (address >= user_addr_max()) { \
87261d19 434 res = -EFAULT; \
39bc88e5
CM
435 } else { \
436 uaccess_ttbr0_enable(); \
87261d19
AP
437 asm volatile ( \
438 "1: " insn ", %1\n" \
439 " mov %w0, #0\n" \
440 "2:\n" \
441 " .pushsection .fixup,\"ax\"\n" \
442 " .align 2\n" \
443 "3: mov %w0, %w2\n" \
444 " b 2b\n" \
445 " .popsection\n" \
446 _ASM_EXTABLE(1b, 3b) \
447 : "=r" (res) \
39bc88e5
CM
448 : "r" (address), "i" (-EFAULT)); \
449 uaccess_ttbr0_disable(); \
450 }
7dd01aef 451
9dbd5bb2 452static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
7dd01aef
AP
453{
454 unsigned long address;
9dbd5bb2
SP
455 int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
456 int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
457 int ret = 0;
7dd01aef 458
81cddd65 459 address = untagged_addr(pt_regs_read_reg(regs, rt));
7dd01aef 460
9dbd5bb2
SP
461 switch (crm) {
462 case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
463 __user_cache_maint("dc civac", address, ret);
464 break;
465 case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
466 __user_cache_maint("dc civac", address, ret);
467 break;
74aae8ed
RM
468 case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
469 __user_cache_maint("sys 3, c7, c12, 1", address, ret);
470 break;
9dbd5bb2
SP
471 case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
472 __user_cache_maint("dc civac", address, ret);
473 break;
474 case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
475 __user_cache_maint("ic ivau", address, ret);
476 break;
477 default:
7dd01aef
AP
478 force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
479 return;
480 }
481
482 if (ret)
483 arm64_notify_segfault(regs, address);
484 else
485 regs->pc += 4;
486}
487
116c81f4
SP
488static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
489{
490 int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
8b6e70fc
MR
491 unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
492
493 pt_regs_write_reg(regs, rt, val);
116c81f4 494
116c81f4
SP
495 regs->pc += 4;
496}
497
6126ce05
MZ
498static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
499{
500 int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
501
502 pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
503 regs->pc += 4;
504}
505
9842119a
MZ
506static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
507{
508 int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
509
c6f97add 510 pt_regs_write_reg(regs, rt, arch_timer_get_rate());
9842119a
MZ
511 regs->pc += 4;
512}
513
9dbd5bb2
SP
514struct sys64_hook {
515 unsigned int esr_mask;
516 unsigned int esr_val;
517 void (*handler)(unsigned int esr, struct pt_regs *regs);
518};
519
520static struct sys64_hook sys64_hooks[] = {
521 {
522 .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
523 .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
524 .handler = user_cache_maint_handler,
525 },
116c81f4
SP
526 {
527 /* Trap read access to CTR_EL0 */
528 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
529 .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
530 .handler = ctr_read_handler,
531 },
6126ce05
MZ
532 {
533 /* Trap read access to CNTVCT_EL0 */
534 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
535 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
536 .handler = cntvct_read_handler,
537 },
9842119a
MZ
538 {
539 /* Trap read access to CNTFRQ_EL0 */
540 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
541 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
542 .handler = cntfrq_read_handler,
543 },
9dbd5bb2
SP
544 {},
545};
546
547asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
548{
549 struct sys64_hook *hook;
550
551 for (hook = sys64_hooks; hook->handler; hook++)
552 if ((hook->esr_mask & esr) == hook->esr_val) {
553 hook->handler(esr, regs);
554 return;
555 }
556
49f6cba6
MR
557 /*
558 * New SYS instructions may previously have been undefined at EL0. Fall
559 * back to our usual undefined instruction handler so that we handle
560 * these consistently.
561 */
562 do_undefinstr(regs);
9dbd5bb2
SP
563}
564
60ffc30d
CM
565long compat_arm_syscall(struct pt_regs *regs);
566
567asmlinkage long do_ni_syscall(struct pt_regs *regs)
568{
569#ifdef CONFIG_COMPAT
570 long ret;
571 if (is_compat_task()) {
572 ret = compat_arm_syscall(regs);
573 if (ret != -ENOSYS)
574 return ret;
575 }
576#endif
577
86dca36e 578 if (show_unhandled_signals_ratelimited()) {
60ffc30d 579 pr_info("%s[%d]: syscall %d\n", current->comm,
758b01e9 580 task_pid_nr(current), regs->syscallno);
60ffc30d
CM
581 dump_instr("", regs);
582 if (user_mode(regs))
583 __show_regs(regs);
584 }
585
586 return sys_ni_syscall();
587}
588
60a1f02c
MR
589static const char *esr_class_str[] = {
590 [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
591 [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
592 [ESR_ELx_EC_WFx] = "WFI/WFE",
593 [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
594 [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
595 [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
596 [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
597 [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
598 [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
599 [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
600 [ESR_ELx_EC_ILL] = "PSTATE.IL",
601 [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
602 [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
603 [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
604 [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
605 [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
606 [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
607 [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
608 [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
609 [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
610 [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
611 [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
612 [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
613 [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
614 [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
615 [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
616 [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
617 [ESR_ELx_EC_SERROR] = "SError",
618 [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
619 [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
620 [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
621 [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
622 [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
623 [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
624 [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
625 [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
626 [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
627};
628
629const char *esr_get_class_string(u32 esr)
630{
275f344b 631 return esr_class_str[ESR_ELx_EC(esr)];
60a1f02c
MR
632}
633
60ffc30d 634/*
7d9e8f71
MR
635 * bad_mode handles the impossible case in the exception vector. This is always
636 * fatal.
60ffc30d
CM
637 */
638asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
639{
640 console_verbose();
641
8051f4d1
MR
642 pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
643 handler[reason], smp_processor_id(), esr,
644 esr_get_class_string(esr));
7d9e8f71
MR
645
646 die("Oops - bad mode", regs, 0);
647 local_irq_disable();
648 panic("bad mode");
649}
650
651/*
652 * bad_el0_sync handles unexpected, but potentially recoverable synchronous
653 * exceptions taken from EL0. Unlike bad_mode, this returns.
654 */
655asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
656{
657 siginfo_t info;
658 void __user *pc = (void __user *)instruction_pointer(regs);
659 console_verbose();
660
661 pr_crit("Bad EL0 synchronous exception detected on CPU%d, code 0x%08x -- %s\n",
662 smp_processor_id(), esr, esr_get_class_string(esr));
9955ac47
MR
663 __show_regs(regs);
664
665 info.si_signo = SIGILL;
666 info.si_errno = 0;
667 info.si_code = ILL_ILLOPC;
668 info.si_addr = pc;
60ffc30d 669
7d9e8f71
MR
670 current->thread.fault_address = 0;
671 current->thread.fault_code = 0;
672
673 force_sig_info(info.si_signo, &info, current);
60ffc30d
CM
674}
675
901212c5
MR
676#ifdef CONFIG_VMAP_STACK
677
678DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
679 __aligned(16);
680
681asmlinkage void handle_bad_stack(struct pt_regs *regs)
682{
683 unsigned long tsk_stk = (unsigned long)current->stack;
684 unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
685 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
686 unsigned int esr = read_sysreg(esr_el1);
687 unsigned long far = read_sysreg(far_el1);
688
689 console_verbose();
690 pr_emerg("Insufficient stack space to handle exception!");
691
692 pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
693 pr_emerg("FAR: 0x%016lx\n", far);
694
695 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
696 tsk_stk, tsk_stk + THREAD_SIZE);
697 pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
698 irq_stk, irq_stk + THREAD_SIZE);
699 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
700 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
701
702 __show_regs(regs);
703
704 /*
705 * We use nmi_panic to limit the potential for recusive overflows, and
706 * to get a better stack trace.
707 */
708 nmi_panic(NULL, "kernel stack overflow");
709 cpu_park_loop();
710}
711#endif
712
60ffc30d
CM
713void __pte_error(const char *file, int line, unsigned long val)
714{
c9cd0ed9 715 pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
60ffc30d
CM
716}
717
718void __pmd_error(const char *file, int line, unsigned long val)
719{
c9cd0ed9 720 pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
60ffc30d
CM
721}
722
c79b954b
JL
723void __pud_error(const char *file, int line, unsigned long val)
724{
c9cd0ed9 725 pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
c79b954b
JL
726}
727
60ffc30d
CM
728void __pgd_error(const char *file, int line, unsigned long val)
729{
c9cd0ed9 730 pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
60ffc30d
CM
731}
732
9fb7410f
DM
733/* GENERIC_BUG traps */
734
735int is_valid_bugaddr(unsigned long addr)
736{
737 /*
738 * bug_handler() only called for BRK #BUG_BRK_IMM.
739 * So the answer is trivial -- any spurious instances with no
740 * bug table entry will be rejected by report_bug() and passed
741 * back to the debug-monitors code and handled as a fatal
742 * unexpected debug exception.
743 */
744 return 1;
745}
746
747static int bug_handler(struct pt_regs *regs, unsigned int esr)
748{
749 if (user_mode(regs))
750 return DBG_HOOK_ERROR;
751
752 switch (report_bug(regs->pc, regs)) {
753 case BUG_TRAP_TYPE_BUG:
754 die("Oops - BUG", regs, 0);
755 break;
756
757 case BUG_TRAP_TYPE_WARN:
758 break;
759
760 default:
761 /* unknown/unrecognised bug trap type */
762 return DBG_HOOK_ERROR;
763 }
764
765 /* If thread survives, skip over the BUG instruction and continue: */
766 regs->pc += AARCH64_INSN_SIZE; /* skip BRK and resume */
767 return DBG_HOOK_HANDLED;
768}
769
770static struct break_hook bug_break_hook = {
771 .esr_val = 0xf2000000 | BUG_BRK_IMM,
772 .esr_mask = 0xffffffff,
773 .fn = bug_handler,
774};
775
776/*
777 * Initial handler for AArch64 BRK exceptions
778 * This handler only used until debug_traps_init().
779 */
780int __init early_brk64(unsigned long addr, unsigned int esr,
781 struct pt_regs *regs)
782{
783 return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
784}
785
786/* This registration must happen early, before debug_traps_init(). */
60ffc30d
CM
787void __init trap_init(void)
788{
9fb7410f 789 register_break_hook(&bug_break_hook);
60ffc30d 790}