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>
37 #include <asm/exception.h>
38 #include <asm/extable.h>
40 #include <asm/kprobes.h>
41 #include <asm/traps.h>
43 #include <asm/stack_pointer.h>
44 #include <asm/stacktrace.h>
45 #include <asm/exception.h>
46 #include <asm/system_misc.h>
47 #include <asm/sysreg.h>
49 static const char *handler[]= {
56 int show_unhandled_signals = 0;
58 static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
60 unsigned long addr = instruction_pointer(regs);
61 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
67 for (i = -4; i < 1; i++) {
68 unsigned int val, bad;
70 bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
73 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
75 p += sprintf(p, "bad PC value");
80 printk("%sCode: %s\n", lvl, str);
84 #define S_PREEMPT " PREEMPT"
85 #elif defined(CONFIG_PREEMPT_RT)
86 #define S_PREEMPT " PREEMPT_RT"
93 static int __die(const char *str, long err, struct pt_regs *regs)
95 static int die_counter;
98 pr_emerg("Internal error: %s: %016lx [#%d]" S_PREEMPT S_SMP "\n",
99 str, err, ++die_counter);
101 /* trap and error numbers are mostly meaningless on ARM */
102 ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
103 if (ret == NOTIFY_STOP)
109 dump_kernel_instr(KERN_EMERG, regs);
114 static DEFINE_RAW_SPINLOCK(die_lock);
117 * This function is protected against re-entrancy.
119 void die(const char *str, struct pt_regs *regs, long err)
124 raw_spin_lock_irqsave(&die_lock, flags);
130 ret = __die(str, err, regs);
132 if (regs && kexec_should_crash(current))
136 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
140 panic("%s: Fatal exception in interrupt", str);
142 panic("%s: Fatal exception", str);
144 raw_spin_unlock_irqrestore(&die_lock, flags);
146 if (ret != NOTIFY_STOP)
147 make_task_dead(SIGSEGV);
150 static void arm64_show_signal(int signo, const char *str)
152 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
153 DEFAULT_RATELIMIT_BURST);
154 struct task_struct *tsk = current;
155 unsigned int esr = tsk->thread.fault_code;
156 struct pt_regs *regs = task_pt_regs(tsk);
158 /* Leave if the signal won't be shown */
159 if (!show_unhandled_signals ||
160 !unhandled_signal(tsk, signo) ||
164 pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
166 pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);
169 print_vma_addr(KERN_CONT " in ", regs->pc);
174 void arm64_force_sig_fault(int signo, int code, void __user *addr,
177 arm64_show_signal(signo, str);
178 if (signo == SIGKILL)
181 force_sig_fault(signo, code, addr);
184 void arm64_force_sig_mceerr(int code, void __user *addr, short lsb,
187 arm64_show_signal(SIGBUS, str);
188 force_sig_mceerr(code, addr, lsb);
191 void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr,
194 arm64_show_signal(SIGTRAP, str);
195 force_sig_ptrace_errno_trap(errno, addr);
198 void arm64_notify_die(const char *str, struct pt_regs *regs,
199 int signo, int sicode, void __user *addr,
202 if (user_mode(regs)) {
203 WARN_ON(regs != current_pt_regs());
204 current->thread.fault_address = 0;
205 current->thread.fault_code = err;
207 arm64_force_sig_fault(signo, sicode, addr, str);
214 #define PSTATE_IT_1_0_SHIFT 25
215 #define PSTATE_IT_1_0_MASK (0x3 << PSTATE_IT_1_0_SHIFT)
216 #define PSTATE_IT_7_2_SHIFT 10
217 #define PSTATE_IT_7_2_MASK (0x3f << PSTATE_IT_7_2_SHIFT)
219 static u32 compat_get_it_state(struct pt_regs *regs)
221 u32 it, pstate = regs->pstate;
223 it = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
224 it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
229 static void compat_set_it_state(struct pt_regs *regs, u32 it)
233 pstate_it = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
234 pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
236 regs->pstate &= ~PSR_AA32_IT_MASK;
237 regs->pstate |= pstate_it;
240 static void advance_itstate(struct pt_regs *regs)
245 if (!(regs->pstate & PSR_AA32_T_BIT) ||
246 !(regs->pstate & PSR_AA32_IT_MASK))
249 it = compat_get_it_state(regs);
252 * If this is the last instruction of the block, wipe the IT
253 * state. Otherwise advance it.
258 it = (it & 0xe0) | ((it << 1) & 0x1f);
260 compat_set_it_state(regs, it);
263 static void advance_itstate(struct pt_regs *regs)
268 void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
273 * If we were single stepping, we want to get the step exception after
274 * we return from the trap.
277 user_fastforward_single_step(current);
279 if (compat_user_mode(regs))
280 advance_itstate(regs);
282 regs->pstate &= ~PSR_BTYPE_MASK;
285 static int user_insn_read(struct pt_regs *regs, u32 *insnp)
288 void __user *pc = (void __user *)instruction_pointer(regs);
290 if (compat_thumb_mode(regs)) {
291 /* 16-bit Thumb instruction */
293 if (get_user(instr_le, (__le16 __user *)pc))
295 instr = le16_to_cpu(instr_le);
296 if (aarch32_insn_is_wide(instr)) {
299 if (get_user(instr_le, (__le16 __user *)(pc + 2)))
301 instr2 = le16_to_cpu(instr_le);
302 instr = (instr << 16) | instr2;
305 /* 32-bit ARM instruction */
307 if (get_user(instr_le, (__le32 __user *)pc))
309 instr = le32_to_cpu(instr_le);
316 void force_signal_inject(int signal, int code, unsigned long address, unsigned int err)
319 struct pt_regs *regs = current_pt_regs();
321 if (WARN_ON(!user_mode(regs)))
326 desc = "undefined instruction";
329 desc = "illegal memory access";
332 desc = "unknown or unrecoverable error";
336 /* Force signals we don't understand to SIGKILL */
337 if (WARN_ON(signal != SIGKILL &&
338 siginfo_layout(signal, code) != SIL_FAULT)) {
342 arm64_notify_die(desc, regs, signal, code, (void __user *)address, err);
346 * Set up process info to signal segmentation fault - called on access error.
348 void arm64_notify_segfault(unsigned long addr)
352 mmap_read_lock(current->mm);
353 if (find_vma(current->mm, addr) == NULL)
357 mmap_read_unlock(current->mm);
359 force_signal_inject(SIGSEGV, code, addr, 0);
362 void do_el0_undef(struct pt_regs *regs, unsigned long esr)
366 /* check for AArch32 breakpoint instructions */
367 if (!aarch32_break_handler(regs))
370 if (user_insn_read(regs, &insn))
373 if (try_emulate_mrs(regs, insn))
376 if (try_emulate_armv8_deprecated(regs, insn))
380 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
383 void do_el1_undef(struct pt_regs *regs, unsigned long esr)
387 if (aarch64_insn_read((void *)regs->pc, &insn))
390 if (try_emulate_el1_ssbs(regs, insn))
394 die("Oops - Undefined instruction", regs, esr);
397 void do_el0_bti(struct pt_regs *regs)
399 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
402 void do_el1_bti(struct pt_regs *regs, unsigned long esr)
404 die("Oops - BTI", regs, esr);
407 void do_el0_fpac(struct pt_regs *regs, unsigned long esr)
409 force_signal_inject(SIGILL, ILL_ILLOPN, regs->pc, esr);
412 void do_el1_fpac(struct pt_regs *regs, unsigned long esr)
415 * Unexpected FPAC exception in the kernel: kill the task before it
416 * does any more harm.
418 die("Oops - FPAC", regs, esr);
421 #define __user_cache_maint(insn, address, res) \
422 if (address >= user_addr_max()) { \
425 uaccess_ttbr0_enable(); \
427 "1: " insn ", %1\n" \
430 " .pushsection .fixup,\"ax\"\n" \
432 "3: mov %w0, %w2\n" \
435 _ASM_EXTABLE(1b, 3b) \
437 : "r" (address), "i" (-EFAULT)); \
438 uaccess_ttbr0_disable(); \
441 static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
443 unsigned long address;
444 int rt = ESR_ELx_SYS64_ISS_RT(esr);
445 int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
448 address = untagged_addr(pt_regs_read_reg(regs, rt));
451 case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
452 __user_cache_maint("dc civac", address, ret);
454 case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
455 __user_cache_maint("dc civac", address, ret);
457 case ESR_ELx_SYS64_ISS_CRM_DC_CVADP: /* DC CVADP */
458 __user_cache_maint("sys 3, c7, c13, 1", address, ret);
460 case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
461 __user_cache_maint("sys 3, c7, c12, 1", address, ret);
463 case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
464 __user_cache_maint("dc civac", address, ret);
466 case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
467 __user_cache_maint("ic ivau", address, ret);
470 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
475 arm64_notify_segfault(address);
477 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
480 static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
482 int rt = ESR_ELx_SYS64_ISS_RT(esr);
483 unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
485 if (cpus_have_const_cap(ARM64_WORKAROUND_1542419)) {
486 /* Hide DIC so that we can trap the unnecessary maintenance...*/
487 val &= ~BIT(CTR_DIC_SHIFT);
489 /* ... and fake IminLine to reduce the number of traps. */
490 val &= ~CTR_IMINLINE_MASK;
491 val |= (PAGE_SHIFT - 2) & CTR_IMINLINE_MASK;
494 pt_regs_write_reg(regs, rt, val);
496 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
499 static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
501 int rt = ESR_ELx_SYS64_ISS_RT(esr);
503 pt_regs_write_reg(regs, rt, arch_timer_read_counter());
504 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
507 static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
509 int rt = ESR_ELx_SYS64_ISS_RT(esr);
511 pt_regs_write_reg(regs, rt, arch_timer_get_rate());
512 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
515 static void mrs_handler(unsigned int esr, struct pt_regs *regs)
519 rt = ESR_ELx_SYS64_ISS_RT(esr);
520 sysreg = esr_sys64_to_sysreg(esr);
522 if (do_emulate_mrs(regs, sysreg, rt) != 0)
523 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
526 static void wfi_handler(unsigned int esr, struct pt_regs *regs)
528 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
532 unsigned int esr_mask;
533 unsigned int esr_val;
534 void (*handler)(unsigned int esr, struct pt_regs *regs);
537 static const struct sys64_hook sys64_hooks[] = {
539 .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
540 .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
541 .handler = user_cache_maint_handler,
544 /* Trap read access to CTR_EL0 */
545 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
546 .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
547 .handler = ctr_read_handler,
550 /* Trap read access to CNTVCT_EL0 */
551 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
552 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
553 .handler = cntvct_read_handler,
556 /* Trap read access to CNTFRQ_EL0 */
557 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
558 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
559 .handler = cntfrq_read_handler,
562 /* Trap read access to CPUID registers */
563 .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
564 .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
565 .handler = mrs_handler,
568 /* Trap WFI instructions executed in userspace */
569 .esr_mask = ESR_ELx_WFx_MASK,
570 .esr_val = ESR_ELx_WFx_WFI_VAL,
571 .handler = wfi_handler,
577 static bool cp15_cond_valid(unsigned int esr, struct pt_regs *regs)
581 /* Only a T32 instruction can trap without CV being set */
582 if (!(esr & ESR_ELx_CV)) {
585 it = compat_get_it_state(regs);
591 cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
594 return aarch32_opcode_cond_checks[cond](regs->pstate);
597 static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
599 int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
601 pt_regs_write_reg(regs, reg, arch_timer_get_rate());
602 arm64_skip_faulting_instruction(regs, 4);
605 static const struct sys64_hook cp15_32_hooks[] = {
607 .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
608 .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
609 .handler = compat_cntfrq_read_handler,
614 static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
616 int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
617 int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
618 u64 val = arch_timer_read_counter();
620 pt_regs_write_reg(regs, rt, lower_32_bits(val));
621 pt_regs_write_reg(regs, rt2, upper_32_bits(val));
622 arm64_skip_faulting_instruction(regs, 4);
625 static const struct sys64_hook cp15_64_hooks[] = {
627 .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
628 .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
629 .handler = compat_cntvct_read_handler,
634 void do_el0_cp15(unsigned long esr, struct pt_regs *regs)
636 const struct sys64_hook *hook, *hook_base;
638 if (!cp15_cond_valid(esr, regs)) {
640 * There is no T16 variant of a CP access, so we
641 * always advance PC by 4 bytes.
643 arm64_skip_faulting_instruction(regs, 4);
647 switch (ESR_ELx_EC(esr)) {
648 case ESR_ELx_EC_CP15_32:
649 hook_base = cp15_32_hooks;
651 case ESR_ELx_EC_CP15_64:
652 hook_base = cp15_64_hooks;
655 do_el0_undef(regs, esr);
659 for (hook = hook_base; hook->handler; hook++)
660 if ((hook->esr_mask & esr) == hook->esr_val) {
661 hook->handler(esr, regs);
666 * New cp15 instructions may previously have been undefined at
667 * EL0. Fall back to our usual undefined instruction handler
668 * so that we handle these consistently.
670 do_el0_undef(regs, esr);
674 void do_el0_sys(unsigned long esr, struct pt_regs *regs)
676 const struct sys64_hook *hook;
678 for (hook = sys64_hooks; hook->handler; hook++)
679 if ((hook->esr_mask & esr) == hook->esr_val) {
680 hook->handler(esr, regs);
685 * New SYS instructions may previously have been undefined at EL0. Fall
686 * back to our usual undefined instruction handler so that we handle
687 * these consistently.
689 do_el0_undef(regs, esr);
692 static const char *esr_class_str[] = {
693 [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
694 [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
695 [ESR_ELx_EC_WFx] = "WFI/WFE",
696 [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
697 [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
698 [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
699 [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
700 [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
701 [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
702 [ESR_ELx_EC_PAC] = "PAC",
703 [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
704 [ESR_ELx_EC_BTI] = "BTI",
705 [ESR_ELx_EC_ILL] = "PSTATE.IL",
706 [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
707 [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
708 [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
709 [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
710 [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
711 [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
712 [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
713 [ESR_ELx_EC_SVE] = "SVE",
714 [ESR_ELx_EC_ERET] = "ERET/ERETAA/ERETAB",
715 [ESR_ELx_EC_FPAC] = "FPAC",
716 [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
717 [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
718 [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
719 [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
720 [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
721 [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
722 [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
723 [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
724 [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
725 [ESR_ELx_EC_SERROR] = "SError",
726 [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
727 [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
728 [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
729 [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
730 [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
731 [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
732 [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
733 [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
734 [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
737 const char *esr_get_class_string(u32 esr)
739 return esr_class_str[ESR_ELx_EC(esr)];
743 * bad_mode handles the impossible case in the exception vector. This is always
746 asmlinkage void notrace bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
748 arm64_enter_nmi(regs);
752 pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
753 handler[reason], smp_processor_id(), esr,
754 esr_get_class_string(esr));
762 * bad_el0_sync handles unexpected, but potentially recoverable synchronous
763 * exceptions taken from EL0. Unlike bad_mode, this returns.
765 void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
767 void __user *pc = (void __user *)instruction_pointer(regs);
769 current->thread.fault_address = 0;
770 current->thread.fault_code = esr;
772 arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
773 "Bad EL0 synchronous exception");
776 #ifdef CONFIG_VMAP_STACK
778 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
781 asmlinkage void noinstr handle_bad_stack(struct pt_regs *regs)
783 unsigned long tsk_stk = (unsigned long)current->stack;
784 unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
785 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
786 unsigned int esr = read_sysreg(esr_el1);
787 unsigned long far = read_sysreg(far_el1);
789 arm64_enter_nmi(regs);
792 pr_emerg("Insufficient stack space to handle exception!");
794 pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
795 pr_emerg("FAR: 0x%016lx\n", far);
797 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
798 tsk_stk, tsk_stk + THREAD_SIZE);
799 pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
800 irq_stk, irq_stk + IRQ_STACK_SIZE);
801 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
802 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
807 * We use nmi_panic to limit the potential for recusive overflows, and
808 * to get a better stack trace.
810 nmi_panic(NULL, "kernel stack overflow");
815 void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
819 pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
820 smp_processor_id(), esr, esr_get_class_string(esr));
824 nmi_panic(regs, "Asynchronous SError Interrupt");
830 bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
832 u32 aet = arm64_ras_serror_get_severity(esr);
835 case ESR_ELx_AET_CE: /* corrected error */
836 case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
838 * The CPU can make progress. We may take UEO again as
839 * a more severe error.
843 case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
844 case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
846 * The CPU can't make progress. The exception may have
849 * Neoverse-N1 #1349291 means a non-KVM SError reported as
850 * Unrecoverable should be treated as Uncontainable. We
851 * call arm64_serror_panic() in both cases.
855 case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
857 /* Error has been silently propagated */
858 arm64_serror_panic(regs, esr);
862 asmlinkage void noinstr do_serror(struct pt_regs *regs, unsigned int esr)
864 arm64_enter_nmi(regs);
866 /* non-RAS errors are not containable */
867 if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
868 arm64_serror_panic(regs, esr);
870 arm64_exit_nmi(regs);
873 /* GENERIC_BUG traps */
875 int is_valid_bugaddr(unsigned long addr)
878 * bug_handler() only called for BRK #BUG_BRK_IMM.
879 * So the answer is trivial -- any spurious instances with no
880 * bug table entry will be rejected by report_bug() and passed
881 * back to the debug-monitors code and handled as a fatal
882 * unexpected debug exception.
887 static int bug_handler(struct pt_regs *regs, unsigned int esr)
889 switch (report_bug(regs->pc, regs)) {
890 case BUG_TRAP_TYPE_BUG:
891 die("Oops - BUG", regs, esr);
894 case BUG_TRAP_TYPE_WARN:
898 /* unknown/unrecognised bug trap type */
899 return DBG_HOOK_ERROR;
902 /* If thread survives, skip over the BUG instruction and continue: */
903 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
904 return DBG_HOOK_HANDLED;
907 static struct break_hook bug_break_hook = {
912 static int reserved_fault_handler(struct pt_regs *regs, unsigned int esr)
914 pr_err("%s generated an invalid instruction at %pS!\n",
915 in_bpf_jit(regs) ? "BPF JIT" : "Kernel text patching",
916 (void *)instruction_pointer(regs));
918 /* We cannot handle this */
919 return DBG_HOOK_ERROR;
922 static struct break_hook fault_break_hook = {
923 .fn = reserved_fault_handler,
924 .imm = FAULT_BRK_IMM,
927 #ifdef CONFIG_KASAN_SW_TAGS
929 #define KASAN_ESR_RECOVER 0x20
930 #define KASAN_ESR_WRITE 0x10
931 #define KASAN_ESR_SIZE_MASK 0x0f
932 #define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK))
934 static int kasan_handler(struct pt_regs *regs, unsigned int esr)
936 bool recover = esr & KASAN_ESR_RECOVER;
937 bool write = esr & KASAN_ESR_WRITE;
938 size_t size = KASAN_ESR_SIZE(esr);
939 u64 addr = regs->regs[0];
942 kasan_report(addr, size, write, pc);
945 * The instrumentation allows to control whether we can proceed after
946 * a crash was detected. This is done by passing the -recover flag to
947 * the compiler. Disabling recovery allows to generate more compact
950 * Unfortunately disabling recovery doesn't work for the kernel right
951 * now. KASAN reporting is disabled in some contexts (for example when
952 * the allocator accesses slab object metadata; this is controlled by
953 * current->kasan_depth). All these accesses are detected by the tool,
954 * even though the reports for them are not printed.
956 * This is something that might be fixed at some point in the future.
959 die("Oops - KASAN", regs, esr);
961 /* If thread survives, skip over the brk instruction and continue: */
962 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
963 return DBG_HOOK_HANDLED;
966 static struct break_hook kasan_break_hook = {
968 .imm = KASAN_BRK_IMM,
969 .mask = KASAN_BRK_MASK,
974 * Initial handler for AArch64 BRK exceptions
975 * This handler only used until debug_traps_init().
977 int __init early_brk64(unsigned long addr, unsigned int esr,
978 struct pt_regs *regs)
980 #ifdef CONFIG_KASAN_SW_TAGS
981 unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
983 if ((comment & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
984 return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
986 return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
989 void __init trap_init(void)
991 register_kernel_break_hook(&bug_break_hook);
992 register_kernel_break_hook(&fault_break_hook);
993 #ifdef CONFIG_KASAN_SW_TAGS
994 register_kernel_break_hook(&kasan_break_hook);