1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on arch/arm/kernel/traps.c
5 * Copyright (C) 1995-2009 Russell King
6 * Copyright (C) 2012 ARM Ltd.
10 #include <linux/context_tracking.h>
11 #include <linux/signal.h>
12 #include <linux/personality.h>
13 #include <linux/kallsyms.h>
14 #include <linux/kprobes.h>
15 #include <linux/spinlock.h>
16 #include <linux/uaccess.h>
17 #include <linux/hardirq.h>
18 #include <linux/kdebug.h>
19 #include <linux/module.h>
20 #include <linux/kexec.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/sched/signal.h>
24 #include <linux/sched/debug.h>
25 #include <linux/sched/task_stack.h>
26 #include <linux/sizes.h>
27 #include <linux/syscalls.h>
28 #include <linux/mm_types.h>
29 #include <linux/kasan.h>
31 #include <asm/atomic.h>
33 #include <asm/cpufeature.h>
34 #include <asm/daifflags.h>
35 #include <asm/debug-monitors.h>
38 #include <asm/traps.h>
40 #include <asm/stack_pointer.h>
41 #include <asm/stacktrace.h>
42 #include <asm/exception.h>
43 #include <asm/system_misc.h>
44 #include <asm/sysreg.h>
46 static const char *handler[]= {
53 int show_unhandled_signals = 0;
55 static void dump_backtrace_entry(unsigned long where)
57 printk(" %pS\n", (void *)where);
60 static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
62 unsigned long addr = instruction_pointer(regs);
63 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
69 for (i = -4; i < 1; i++) {
70 unsigned int val, bad;
72 bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
75 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
77 p += sprintf(p, "bad PC value");
82 printk("%sCode: %s\n", lvl, str);
85 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
87 struct stackframe frame;
90 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
101 if (!try_get_task_stack(tsk))
104 if (tsk == current) {
105 start_backtrace(&frame,
106 (unsigned long)__builtin_frame_address(0),
107 (unsigned long)dump_backtrace);
110 * task blocked in __switch_to
112 start_backtrace(&frame,
113 thread_saved_fp(tsk),
114 thread_saved_pc(tsk));
117 printk("Call trace:\n");
119 /* skip until specified stack frame */
121 dump_backtrace_entry(frame.pc);
122 } else if (frame.fp == regs->regs[29]) {
125 * Mostly, this is the case where this function is
126 * called in panic/abort. As exception handler's
127 * stack frame does not contain the corresponding pc
128 * at which an exception has taken place, use regs->pc
131 dump_backtrace_entry(regs->pc);
133 } while (!unwind_frame(tsk, &frame));
138 void show_stack(struct task_struct *tsk, unsigned long *sp)
140 dump_backtrace(NULL, tsk);
144 #ifdef CONFIG_PREEMPT
145 #define S_PREEMPT " PREEMPT"
151 static int __die(const char *str, int err, struct pt_regs *regs)
153 static int die_counter;
156 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
157 str, err, ++die_counter);
159 /* trap and error numbers are mostly meaningless on ARM */
160 ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
161 if (ret == NOTIFY_STOP)
167 dump_kernel_instr(KERN_EMERG, regs);
172 static DEFINE_RAW_SPINLOCK(die_lock);
175 * This function is protected against re-entrancy.
177 void die(const char *str, struct pt_regs *regs, int err)
182 raw_spin_lock_irqsave(&die_lock, flags);
188 ret = __die(str, err, regs);
190 if (regs && kexec_should_crash(current))
194 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
198 panic("Fatal exception in interrupt");
200 panic("Fatal exception");
202 raw_spin_unlock_irqrestore(&die_lock, flags);
204 if (ret != NOTIFY_STOP)
208 static void arm64_show_signal(int signo, const char *str)
210 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
211 DEFAULT_RATELIMIT_BURST);
212 struct task_struct *tsk = current;
213 unsigned int esr = tsk->thread.fault_code;
214 struct pt_regs *regs = task_pt_regs(tsk);
216 /* Leave if the signal won't be shown */
217 if (!show_unhandled_signals ||
218 !unhandled_signal(tsk, signo) ||
222 pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
224 pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);
227 print_vma_addr(KERN_CONT " in ", regs->pc);
232 void arm64_force_sig_fault(int signo, int code, void __user *addr,
235 arm64_show_signal(signo, str);
236 if (signo == SIGKILL)
239 force_sig_fault(signo, code, addr);
242 void arm64_force_sig_mceerr(int code, void __user *addr, short lsb,
245 arm64_show_signal(SIGBUS, str);
246 force_sig_mceerr(code, addr, lsb);
249 void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr,
252 arm64_show_signal(SIGTRAP, str);
253 force_sig_ptrace_errno_trap(errno, addr);
256 void arm64_notify_die(const char *str, struct pt_regs *regs,
257 int signo, int sicode, void __user *addr,
260 if (user_mode(regs)) {
261 WARN_ON(regs != current_pt_regs());
262 current->thread.fault_address = 0;
263 current->thread.fault_code = err;
265 arm64_force_sig_fault(signo, sicode, addr, str);
271 void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
276 * If we were single stepping, we want to get the step exception after
277 * we return from the trap.
280 user_fastforward_single_step(current);
283 static LIST_HEAD(undef_hook);
284 static DEFINE_RAW_SPINLOCK(undef_lock);
286 void register_undef_hook(struct undef_hook *hook)
290 raw_spin_lock_irqsave(&undef_lock, flags);
291 list_add(&hook->node, &undef_hook);
292 raw_spin_unlock_irqrestore(&undef_lock, flags);
295 void unregister_undef_hook(struct undef_hook *hook)
299 raw_spin_lock_irqsave(&undef_lock, flags);
300 list_del(&hook->node);
301 raw_spin_unlock_irqrestore(&undef_lock, flags);
304 static int call_undef_hook(struct pt_regs *regs)
306 struct undef_hook *hook;
309 int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
310 void __user *pc = (void __user *)instruction_pointer(regs);
312 if (!user_mode(regs)) {
314 if (probe_kernel_address((__force __le32 *)pc, instr_le))
316 instr = le32_to_cpu(instr_le);
317 } else if (compat_thumb_mode(regs)) {
318 /* 16-bit Thumb instruction */
320 if (get_user(instr_le, (__le16 __user *)pc))
322 instr = le16_to_cpu(instr_le);
323 if (aarch32_insn_is_wide(instr)) {
326 if (get_user(instr_le, (__le16 __user *)(pc + 2)))
328 instr2 = le16_to_cpu(instr_le);
329 instr = (instr << 16) | instr2;
332 /* 32-bit ARM instruction */
334 if (get_user(instr_le, (__le32 __user *)pc))
336 instr = le32_to_cpu(instr_le);
339 raw_spin_lock_irqsave(&undef_lock, flags);
340 list_for_each_entry(hook, &undef_hook, node)
341 if ((instr & hook->instr_mask) == hook->instr_val &&
342 (regs->pstate & hook->pstate_mask) == hook->pstate_val)
345 raw_spin_unlock_irqrestore(&undef_lock, flags);
347 return fn ? fn(regs, instr) : 1;
350 void force_signal_inject(int signal, int code, unsigned long address)
353 struct pt_regs *regs = current_pt_regs();
355 if (WARN_ON(!user_mode(regs)))
360 desc = "undefined instruction";
363 desc = "illegal memory access";
366 desc = "unknown or unrecoverable error";
370 /* Force signals we don't understand to SIGKILL */
371 if (WARN_ON(signal != SIGKILL &&
372 siginfo_layout(signal, code) != SIL_FAULT)) {
376 arm64_notify_die(desc, regs, signal, code, (void __user *)address, 0);
380 * Set up process info to signal segmentation fault - called on access error.
382 void arm64_notify_segfault(unsigned long addr)
386 down_read(¤t->mm->mmap_sem);
387 if (find_vma(current->mm, addr) == NULL)
391 up_read(¤t->mm->mmap_sem);
393 force_signal_inject(SIGSEGV, code, addr);
396 asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
398 /* check for AArch32 breakpoint instructions */
399 if (!aarch32_break_handler(regs))
402 if (call_undef_hook(regs) == 0)
405 BUG_ON(!user_mode(regs));
406 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
409 #define __user_cache_maint(insn, address, res) \
410 if (address >= user_addr_max()) { \
413 uaccess_ttbr0_enable(); \
415 "1: " insn ", %1\n" \
418 " .pushsection .fixup,\"ax\"\n" \
420 "3: mov %w0, %w2\n" \
423 _ASM_EXTABLE(1b, 3b) \
425 : "r" (address), "i" (-EFAULT)); \
426 uaccess_ttbr0_disable(); \
429 static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
431 unsigned long address;
432 int rt = ESR_ELx_SYS64_ISS_RT(esr);
433 int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
436 address = untagged_addr(pt_regs_read_reg(regs, rt));
439 case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
440 __user_cache_maint("dc civac", address, ret);
442 case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
443 __user_cache_maint("dc civac", address, ret);
445 case ESR_ELx_SYS64_ISS_CRM_DC_CVADP: /* DC CVADP */
446 __user_cache_maint("sys 3, c7, c13, 1", address, ret);
448 case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
449 __user_cache_maint("sys 3, c7, c12, 1", address, ret);
451 case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
452 __user_cache_maint("dc civac", address, ret);
454 case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
455 __user_cache_maint("ic ivau", address, ret);
458 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
463 arm64_notify_segfault(address);
465 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
468 static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
470 int rt = ESR_ELx_SYS64_ISS_RT(esr);
471 unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
473 pt_regs_write_reg(regs, rt, val);
475 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
478 static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
480 int rt = ESR_ELx_SYS64_ISS_RT(esr);
482 pt_regs_write_reg(regs, rt, arch_timer_read_counter());
483 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
486 static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
488 int rt = ESR_ELx_SYS64_ISS_RT(esr);
490 pt_regs_write_reg(regs, rt, arch_timer_get_rate());
491 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
494 static void mrs_handler(unsigned int esr, struct pt_regs *regs)
498 rt = ESR_ELx_SYS64_ISS_RT(esr);
499 sysreg = esr_sys64_to_sysreg(esr);
501 if (do_emulate_mrs(regs, sysreg, rt) != 0)
502 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
505 static void wfi_handler(unsigned int esr, struct pt_regs *regs)
507 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
511 unsigned int esr_mask;
512 unsigned int esr_val;
513 void (*handler)(unsigned int esr, struct pt_regs *regs);
516 static const struct sys64_hook sys64_hooks[] = {
518 .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
519 .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
520 .handler = user_cache_maint_handler,
523 /* Trap read access to CTR_EL0 */
524 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
525 .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
526 .handler = ctr_read_handler,
529 /* Trap read access to CNTVCT_EL0 */
530 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
531 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
532 .handler = cntvct_read_handler,
535 /* Trap read access to CNTFRQ_EL0 */
536 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
537 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
538 .handler = cntfrq_read_handler,
541 /* Trap read access to CPUID registers */
542 .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
543 .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
544 .handler = mrs_handler,
547 /* Trap WFI instructions executed in userspace */
548 .esr_mask = ESR_ELx_WFx_MASK,
549 .esr_val = ESR_ELx_WFx_WFI_VAL,
550 .handler = wfi_handler,
557 #define PSTATE_IT_1_0_SHIFT 25
558 #define PSTATE_IT_1_0_MASK (0x3 << PSTATE_IT_1_0_SHIFT)
559 #define PSTATE_IT_7_2_SHIFT 10
560 #define PSTATE_IT_7_2_MASK (0x3f << PSTATE_IT_7_2_SHIFT)
562 static u32 compat_get_it_state(struct pt_regs *regs)
564 u32 it, pstate = regs->pstate;
566 it = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
567 it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
572 static void compat_set_it_state(struct pt_regs *regs, u32 it)
576 pstate_it = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
577 pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
579 regs->pstate &= ~PSR_AA32_IT_MASK;
580 regs->pstate |= pstate_it;
583 static bool cp15_cond_valid(unsigned int esr, struct pt_regs *regs)
587 /* Only a T32 instruction can trap without CV being set */
588 if (!(esr & ESR_ELx_CV)) {
591 it = compat_get_it_state(regs);
597 cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
600 return aarch32_opcode_cond_checks[cond](regs->pstate);
603 static void advance_itstate(struct pt_regs *regs)
608 if (!(regs->pstate & PSR_AA32_T_BIT) ||
609 !(regs->pstate & PSR_AA32_IT_MASK))
612 it = compat_get_it_state(regs);
615 * If this is the last instruction of the block, wipe the IT
616 * state. Otherwise advance it.
621 it = (it & 0xe0) | ((it << 1) & 0x1f);
623 compat_set_it_state(regs, it);
626 static void arm64_compat_skip_faulting_instruction(struct pt_regs *regs,
629 advance_itstate(regs);
630 arm64_skip_faulting_instruction(regs, sz);
633 static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
635 int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
637 pt_regs_write_reg(regs, reg, arch_timer_get_rate());
638 arm64_compat_skip_faulting_instruction(regs, 4);
641 static const struct sys64_hook cp15_32_hooks[] = {
643 .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
644 .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
645 .handler = compat_cntfrq_read_handler,
650 static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
652 int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
653 int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
654 u64 val = arch_timer_read_counter();
656 pt_regs_write_reg(regs, rt, lower_32_bits(val));
657 pt_regs_write_reg(regs, rt2, upper_32_bits(val));
658 arm64_compat_skip_faulting_instruction(regs, 4);
661 static const struct sys64_hook cp15_64_hooks[] = {
663 .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
664 .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
665 .handler = compat_cntvct_read_handler,
670 asmlinkage void __exception do_cp15instr(unsigned int esr, struct pt_regs *regs)
672 const struct sys64_hook *hook, *hook_base;
674 if (!cp15_cond_valid(esr, regs)) {
676 * There is no T16 variant of a CP access, so we
677 * always advance PC by 4 bytes.
679 arm64_compat_skip_faulting_instruction(regs, 4);
683 switch (ESR_ELx_EC(esr)) {
684 case ESR_ELx_EC_CP15_32:
685 hook_base = cp15_32_hooks;
687 case ESR_ELx_EC_CP15_64:
688 hook_base = cp15_64_hooks;
695 for (hook = hook_base; hook->handler; hook++)
696 if ((hook->esr_mask & esr) == hook->esr_val) {
697 hook->handler(esr, regs);
702 * New cp15 instructions may previously have been undefined at
703 * EL0. Fall back to our usual undefined instruction handler
704 * so that we handle these consistently.
710 asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
712 const struct sys64_hook *hook;
714 for (hook = sys64_hooks; hook->handler; hook++)
715 if ((hook->esr_mask & esr) == hook->esr_val) {
716 hook->handler(esr, regs);
721 * New SYS instructions may previously have been undefined at EL0. Fall
722 * back to our usual undefined instruction handler so that we handle
723 * these consistently.
728 static const char *esr_class_str[] = {
729 [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
730 [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
731 [ESR_ELx_EC_WFx] = "WFI/WFE",
732 [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
733 [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
734 [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
735 [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
736 [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
737 [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
738 [ESR_ELx_EC_PAC] = "PAC",
739 [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
740 [ESR_ELx_EC_ILL] = "PSTATE.IL",
741 [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
742 [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
743 [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
744 [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
745 [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
746 [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
747 [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
748 [ESR_ELx_EC_SVE] = "SVE",
749 [ESR_ELx_EC_ERET] = "ERET/ERETAA/ERETAB",
750 [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
751 [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
752 [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
753 [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
754 [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
755 [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
756 [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
757 [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
758 [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
759 [ESR_ELx_EC_SERROR] = "SError",
760 [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
761 [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
762 [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
763 [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
764 [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
765 [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
766 [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
767 [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
768 [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
771 const char *esr_get_class_string(u32 esr)
773 return esr_class_str[ESR_ELx_EC(esr)];
777 * bad_mode handles the impossible case in the exception vector. This is always
780 asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
784 pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
785 handler[reason], smp_processor_id(), esr,
786 esr_get_class_string(esr));
793 * bad_el0_sync handles unexpected, but potentially recoverable synchronous
794 * exceptions taken from EL0. Unlike bad_mode, this returns.
796 asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
798 void __user *pc = (void __user *)instruction_pointer(regs);
800 current->thread.fault_address = 0;
801 current->thread.fault_code = esr;
803 arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
804 "Bad EL0 synchronous exception");
807 #ifdef CONFIG_VMAP_STACK
809 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
812 asmlinkage void handle_bad_stack(struct pt_regs *regs)
814 unsigned long tsk_stk = (unsigned long)current->stack;
815 unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
816 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
817 unsigned int esr = read_sysreg(esr_el1);
818 unsigned long far = read_sysreg(far_el1);
821 pr_emerg("Insufficient stack space to handle exception!");
823 pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
824 pr_emerg("FAR: 0x%016lx\n", far);
826 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
827 tsk_stk, tsk_stk + THREAD_SIZE);
828 pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
829 irq_stk, irq_stk + THREAD_SIZE);
830 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
831 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
836 * We use nmi_panic to limit the potential for recusive overflows, and
837 * to get a better stack trace.
839 nmi_panic(NULL, "kernel stack overflow");
844 void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
848 pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
849 smp_processor_id(), esr, esr_get_class_string(esr));
853 nmi_panic(regs, "Asynchronous SError Interrupt");
859 bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
861 u32 aet = arm64_ras_serror_get_severity(esr);
864 case ESR_ELx_AET_CE: /* corrected error */
865 case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
867 * The CPU can make progress. We may take UEO again as
868 * a more severe error.
872 case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
873 case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
875 * The CPU can't make progress. The exception may have
878 * Neoverse-N1 #1349291 means a non-KVM SError reported as
879 * Unrecoverable should be treated as Uncontainable. We
880 * call arm64_serror_panic() in both cases.
884 case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
886 /* Error has been silently propagated */
887 arm64_serror_panic(regs, esr);
891 asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
893 const bool was_in_nmi = in_nmi();
898 /* non-RAS errors are not containable */
899 if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
900 arm64_serror_panic(regs, esr);
906 asmlinkage void enter_from_user_mode(void)
908 CT_WARN_ON(ct_state() != CONTEXT_USER);
911 NOKPROBE_SYMBOL(enter_from_user_mode);
913 void __pte_error(const char *file, int line, unsigned long val)
915 pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
918 void __pmd_error(const char *file, int line, unsigned long val)
920 pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
923 void __pud_error(const char *file, int line, unsigned long val)
925 pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
928 void __pgd_error(const char *file, int line, unsigned long val)
930 pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
933 /* GENERIC_BUG traps */
935 int is_valid_bugaddr(unsigned long addr)
938 * bug_handler() only called for BRK #BUG_BRK_IMM.
939 * So the answer is trivial -- any spurious instances with no
940 * bug table entry will be rejected by report_bug() and passed
941 * back to the debug-monitors code and handled as a fatal
942 * unexpected debug exception.
947 static int bug_handler(struct pt_regs *regs, unsigned int esr)
949 switch (report_bug(regs->pc, regs)) {
950 case BUG_TRAP_TYPE_BUG:
951 die("Oops - BUG", regs, 0);
954 case BUG_TRAP_TYPE_WARN:
958 /* unknown/unrecognised bug trap type */
959 return DBG_HOOK_ERROR;
962 /* If thread survives, skip over the BUG instruction and continue: */
963 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
964 return DBG_HOOK_HANDLED;
967 static struct break_hook bug_break_hook = {
972 #ifdef CONFIG_KASAN_SW_TAGS
974 #define KASAN_ESR_RECOVER 0x20
975 #define KASAN_ESR_WRITE 0x10
976 #define KASAN_ESR_SIZE_MASK 0x0f
977 #define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK))
979 static int kasan_handler(struct pt_regs *regs, unsigned int esr)
981 bool recover = esr & KASAN_ESR_RECOVER;
982 bool write = esr & KASAN_ESR_WRITE;
983 size_t size = KASAN_ESR_SIZE(esr);
984 u64 addr = regs->regs[0];
987 kasan_report(addr, size, write, pc);
990 * The instrumentation allows to control whether we can proceed after
991 * a crash was detected. This is done by passing the -recover flag to
992 * the compiler. Disabling recovery allows to generate more compact
995 * Unfortunately disabling recovery doesn't work for the kernel right
996 * now. KASAN reporting is disabled in some contexts (for example when
997 * the allocator accesses slab object metadata; this is controlled by
998 * current->kasan_depth). All these accesses are detected by the tool,
999 * even though the reports for them are not printed.
1001 * This is something that might be fixed at some point in the future.
1004 die("Oops - KASAN", regs, 0);
1006 /* If thread survives, skip over the brk instruction and continue: */
1007 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
1008 return DBG_HOOK_HANDLED;
1011 static struct break_hook kasan_break_hook = {
1012 .fn = kasan_handler,
1013 .imm = KASAN_BRK_IMM,
1014 .mask = KASAN_BRK_MASK,
1019 * Initial handler for AArch64 BRK exceptions
1020 * This handler only used until debug_traps_init().
1022 int __init early_brk64(unsigned long addr, unsigned int esr,
1023 struct pt_regs *regs)
1025 #ifdef CONFIG_KASAN_SW_TAGS
1026 unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
1028 if ((comment & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
1029 return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
1031 return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
1034 /* This registration must happen early, before debug_traps_init(). */
1035 void __init trap_init(void)
1037 register_kernel_break_hook(&bug_break_hook);
1038 #ifdef CONFIG_KASAN_SW_TAGS
1039 register_kernel_break_hook(&kasan_break_hook);