David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Based on arch/arm/kernel/traps.c |
| 4 | * |
| 5 | * Copyright (C) 1995-2009 Russell King |
| 6 | * Copyright (C) 2012 ARM Ltd. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/bug.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10 | #include <linux/context_tracking.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11 | #include <linux/signal.h> |
| 12 | #include <linux/personality.h> |
| 13 | #include <linux/kallsyms.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 14 | #include <linux/kprobes.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 29 | #include <linux/kasan.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 30 | |
| 31 | #include <asm/atomic.h> |
| 32 | #include <asm/bug.h> |
| 33 | #include <asm/cpufeature.h> |
| 34 | #include <asm/daifflags.h> |
| 35 | #include <asm/debug-monitors.h> |
| 36 | #include <asm/esr.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 37 | #include <asm/exception.h> |
| 38 | #include <asm/extable.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 39 | #include <asm/insn.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 40 | #include <asm/kprobes.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | #include <asm/traps.h> |
| 42 | #include <asm/smp.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> |
| 48 | |
| 49 | static const char *handler[]= { |
| 50 | "Synchronous Abort", |
| 51 | "IRQ", |
| 52 | "FIQ", |
| 53 | "Error" |
| 54 | }; |
| 55 | |
| 56 | int show_unhandled_signals = 0; |
| 57 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 58 | static void dump_kernel_instr(const char *lvl, struct pt_regs *regs) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 59 | { |
| 60 | unsigned long addr = instruction_pointer(regs); |
| 61 | char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str; |
| 62 | int i; |
| 63 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 64 | if (user_mode(regs)) |
| 65 | return; |
| 66 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 67 | for (i = -4; i < 1; i++) { |
| 68 | unsigned int val, bad; |
| 69 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 70 | bad = aarch64_insn_read(&((u32 *)addr)[i], &val); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 71 | |
| 72 | if (!bad) |
| 73 | p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val); |
| 74 | else { |
| 75 | p += sprintf(p, "bad PC value"); |
| 76 | break; |
| 77 | } |
| 78 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 79 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 80 | printk("%sCode: %s\n", lvl, str); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 83 | #ifdef CONFIG_PREEMPT |
| 84 | #define S_PREEMPT " PREEMPT" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 85 | #elif defined(CONFIG_PREEMPT_RT) |
| 86 | #define S_PREEMPT " PREEMPT_RT" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 87 | #else |
| 88 | #define S_PREEMPT "" |
| 89 | #endif |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 90 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 91 | #define S_SMP " SMP" |
| 92 | |
| 93 | static int __die(const char *str, int err, struct pt_regs *regs) |
| 94 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 95 | static int die_counter; |
| 96 | int ret; |
| 97 | |
| 98 | pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n", |
| 99 | str, err, ++die_counter); |
| 100 | |
| 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) |
| 104 | return ret; |
| 105 | |
| 106 | print_modules(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 107 | show_regs(regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 108 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 109 | dump_kernel_instr(KERN_EMERG, regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | static DEFINE_RAW_SPINLOCK(die_lock); |
| 115 | |
| 116 | /* |
| 117 | * This function is protected against re-entrancy. |
| 118 | */ |
| 119 | void die(const char *str, struct pt_regs *regs, int err) |
| 120 | { |
| 121 | int ret; |
| 122 | unsigned long flags; |
| 123 | |
| 124 | raw_spin_lock_irqsave(&die_lock, flags); |
| 125 | |
| 126 | oops_enter(); |
| 127 | |
| 128 | console_verbose(); |
| 129 | bust_spinlocks(1); |
| 130 | ret = __die(str, err, regs); |
| 131 | |
| 132 | if (regs && kexec_should_crash(current)) |
| 133 | crash_kexec(regs); |
| 134 | |
| 135 | bust_spinlocks(0); |
| 136 | add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); |
| 137 | oops_exit(); |
| 138 | |
| 139 | if (in_interrupt()) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 140 | panic("%s: Fatal exception in interrupt", str); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 141 | if (panic_on_oops) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 142 | panic("%s: Fatal exception", str); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 143 | |
| 144 | raw_spin_unlock_irqrestore(&die_lock, flags); |
| 145 | |
| 146 | if (ret != NOTIFY_STOP) |
| 147 | do_exit(SIGSEGV); |
| 148 | } |
| 149 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 150 | static void arm64_show_signal(int signo, const char *str) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 151 | { |
| 152 | static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, |
| 153 | DEFAULT_RATELIMIT_BURST); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 154 | struct task_struct *tsk = current; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 155 | unsigned int esr = tsk->thread.fault_code; |
| 156 | struct pt_regs *regs = task_pt_regs(tsk); |
| 157 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 158 | /* Leave if the signal won't be shown */ |
| 159 | if (!show_unhandled_signals || |
| 160 | !unhandled_signal(tsk, signo) || |
| 161 | !__ratelimit(&rs)) |
| 162 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 163 | |
| 164 | pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk)); |
| 165 | if (esr) |
| 166 | pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr); |
| 167 | |
| 168 | pr_cont("%s", str); |
| 169 | print_vma_addr(KERN_CONT " in ", regs->pc); |
| 170 | pr_cont("\n"); |
| 171 | __show_regs(regs); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 172 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 173 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 174 | void arm64_force_sig_fault(int signo, int code, void __user *addr, |
| 175 | const char *str) |
| 176 | { |
| 177 | arm64_show_signal(signo, str); |
| 178 | if (signo == SIGKILL) |
| 179 | force_sig(SIGKILL); |
| 180 | else |
| 181 | force_sig_fault(signo, code, addr); |
| 182 | } |
| 183 | |
| 184 | void arm64_force_sig_mceerr(int code, void __user *addr, short lsb, |
| 185 | const char *str) |
| 186 | { |
| 187 | arm64_show_signal(SIGBUS, str); |
| 188 | force_sig_mceerr(code, addr, lsb); |
| 189 | } |
| 190 | |
| 191 | void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr, |
| 192 | const char *str) |
| 193 | { |
| 194 | arm64_show_signal(SIGTRAP, str); |
| 195 | force_sig_ptrace_errno_trap(errno, addr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void arm64_notify_die(const char *str, struct pt_regs *regs, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 199 | int signo, int sicode, void __user *addr, |
| 200 | int err) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 201 | { |
| 202 | if (user_mode(regs)) { |
| 203 | WARN_ON(regs != current_pt_regs()); |
| 204 | current->thread.fault_address = 0; |
| 205 | current->thread.fault_code = err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 206 | |
| 207 | arm64_force_sig_fault(signo, sicode, addr, str); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 208 | } else { |
| 209 | die(str, regs, err); |
| 210 | } |
| 211 | } |
| 212 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 213 | #ifdef CONFIG_COMPAT |
| 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) |
| 218 | |
| 219 | static u32 compat_get_it_state(struct pt_regs *regs) |
| 220 | { |
| 221 | u32 it, pstate = regs->pstate; |
| 222 | |
| 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; |
| 225 | |
| 226 | return it; |
| 227 | } |
| 228 | |
| 229 | static void compat_set_it_state(struct pt_regs *regs, u32 it) |
| 230 | { |
| 231 | u32 pstate_it; |
| 232 | |
| 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; |
| 235 | |
| 236 | regs->pstate &= ~PSR_AA32_IT_MASK; |
| 237 | regs->pstate |= pstate_it; |
| 238 | } |
| 239 | |
| 240 | static void advance_itstate(struct pt_regs *regs) |
| 241 | { |
| 242 | u32 it; |
| 243 | |
| 244 | /* ARM mode */ |
| 245 | if (!(regs->pstate & PSR_AA32_T_BIT) || |
| 246 | !(regs->pstate & PSR_AA32_IT_MASK)) |
| 247 | return; |
| 248 | |
| 249 | it = compat_get_it_state(regs); |
| 250 | |
| 251 | /* |
| 252 | * If this is the last instruction of the block, wipe the IT |
| 253 | * state. Otherwise advance it. |
| 254 | */ |
| 255 | if (!(it & 7)) |
| 256 | it = 0; |
| 257 | else |
| 258 | it = (it & 0xe0) | ((it << 1) & 0x1f); |
| 259 | |
| 260 | compat_set_it_state(regs, it); |
| 261 | } |
| 262 | #else |
| 263 | static void advance_itstate(struct pt_regs *regs) |
| 264 | { |
| 265 | } |
| 266 | #endif |
| 267 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 268 | void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size) |
| 269 | { |
| 270 | regs->pc += size; |
| 271 | |
| 272 | /* |
| 273 | * If we were single stepping, we want to get the step exception after |
| 274 | * we return from the trap. |
| 275 | */ |
| 276 | if (user_mode(regs)) |
| 277 | user_fastforward_single_step(current); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 278 | |
| 279 | if (compat_user_mode(regs)) |
| 280 | advance_itstate(regs); |
| 281 | else |
| 282 | regs->pstate &= ~PSR_BTYPE_MASK; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static LIST_HEAD(undef_hook); |
| 286 | static DEFINE_RAW_SPINLOCK(undef_lock); |
| 287 | |
| 288 | void register_undef_hook(struct undef_hook *hook) |
| 289 | { |
| 290 | unsigned long flags; |
| 291 | |
| 292 | raw_spin_lock_irqsave(&undef_lock, flags); |
| 293 | list_add(&hook->node, &undef_hook); |
| 294 | raw_spin_unlock_irqrestore(&undef_lock, flags); |
| 295 | } |
| 296 | |
| 297 | void unregister_undef_hook(struct undef_hook *hook) |
| 298 | { |
| 299 | unsigned long flags; |
| 300 | |
| 301 | raw_spin_lock_irqsave(&undef_lock, flags); |
| 302 | list_del(&hook->node); |
| 303 | raw_spin_unlock_irqrestore(&undef_lock, flags); |
| 304 | } |
| 305 | |
| 306 | static int call_undef_hook(struct pt_regs *regs) |
| 307 | { |
| 308 | struct undef_hook *hook; |
| 309 | unsigned long flags; |
| 310 | u32 instr; |
| 311 | int (*fn)(struct pt_regs *regs, u32 instr) = NULL; |
| 312 | void __user *pc = (void __user *)instruction_pointer(regs); |
| 313 | |
| 314 | if (!user_mode(regs)) { |
| 315 | __le32 instr_le; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 316 | if (get_kernel_nofault(instr_le, (__force __le32 *)pc)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 317 | goto exit; |
| 318 | instr = le32_to_cpu(instr_le); |
| 319 | } else if (compat_thumb_mode(regs)) { |
| 320 | /* 16-bit Thumb instruction */ |
| 321 | __le16 instr_le; |
| 322 | if (get_user(instr_le, (__le16 __user *)pc)) |
| 323 | goto exit; |
| 324 | instr = le16_to_cpu(instr_le); |
| 325 | if (aarch32_insn_is_wide(instr)) { |
| 326 | u32 instr2; |
| 327 | |
| 328 | if (get_user(instr_le, (__le16 __user *)(pc + 2))) |
| 329 | goto exit; |
| 330 | instr2 = le16_to_cpu(instr_le); |
| 331 | instr = (instr << 16) | instr2; |
| 332 | } |
| 333 | } else { |
| 334 | /* 32-bit ARM instruction */ |
| 335 | __le32 instr_le; |
| 336 | if (get_user(instr_le, (__le32 __user *)pc)) |
| 337 | goto exit; |
| 338 | instr = le32_to_cpu(instr_le); |
| 339 | } |
| 340 | |
| 341 | raw_spin_lock_irqsave(&undef_lock, flags); |
| 342 | list_for_each_entry(hook, &undef_hook, node) |
| 343 | if ((instr & hook->instr_mask) == hook->instr_val && |
| 344 | (regs->pstate & hook->pstate_mask) == hook->pstate_val) |
| 345 | fn = hook->fn; |
| 346 | |
| 347 | raw_spin_unlock_irqrestore(&undef_lock, flags); |
| 348 | exit: |
| 349 | return fn ? fn(regs, instr) : 1; |
| 350 | } |
| 351 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 352 | void force_signal_inject(int signal, int code, unsigned long address, unsigned int err) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 353 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 354 | const char *desc; |
| 355 | struct pt_regs *regs = current_pt_regs(); |
| 356 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 357 | if (WARN_ON(!user_mode(regs))) |
| 358 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 359 | |
| 360 | switch (signal) { |
| 361 | case SIGILL: |
| 362 | desc = "undefined instruction"; |
| 363 | break; |
| 364 | case SIGSEGV: |
| 365 | desc = "illegal memory access"; |
| 366 | break; |
| 367 | default: |
| 368 | desc = "unknown or unrecoverable error"; |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | /* Force signals we don't understand to SIGKILL */ |
| 373 | if (WARN_ON(signal != SIGKILL && |
| 374 | siginfo_layout(signal, code) != SIL_FAULT)) { |
| 375 | signal = SIGKILL; |
| 376 | } |
| 377 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 378 | arm64_notify_die(desc, regs, signal, code, (void __user *)address, err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Set up process info to signal segmentation fault - called on access error. |
| 383 | */ |
| 384 | void arm64_notify_segfault(unsigned long addr) |
| 385 | { |
| 386 | int code; |
| 387 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 388 | mmap_read_lock(current->mm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 389 | if (find_vma(current->mm, addr) == NULL) |
| 390 | code = SEGV_MAPERR; |
| 391 | else |
| 392 | code = SEGV_ACCERR; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 393 | mmap_read_unlock(current->mm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 394 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 395 | force_signal_inject(SIGSEGV, code, addr, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 398 | void do_undefinstr(struct pt_regs *regs) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 399 | { |
| 400 | /* check for AArch32 breakpoint instructions */ |
| 401 | if (!aarch32_break_handler(regs)) |
| 402 | return; |
| 403 | |
| 404 | if (call_undef_hook(regs) == 0) |
| 405 | return; |
| 406 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 407 | BUG_ON(!user_mode(regs)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 408 | force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 409 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 410 | NOKPROBE_SYMBOL(do_undefinstr); |
| 411 | |
| 412 | void do_bti(struct pt_regs *regs) |
| 413 | { |
| 414 | BUG_ON(!user_mode(regs)); |
| 415 | force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0); |
| 416 | } |
| 417 | NOKPROBE_SYMBOL(do_bti); |
| 418 | |
| 419 | void do_ptrauth_fault(struct pt_regs *regs, unsigned int esr) |
| 420 | { |
| 421 | /* |
| 422 | * Unexpected FPAC exception or pointer authentication failure in |
| 423 | * the kernel: kill the task before it does any more harm. |
| 424 | */ |
| 425 | BUG_ON(!user_mode(regs)); |
| 426 | force_signal_inject(SIGILL, ILL_ILLOPN, regs->pc, esr); |
| 427 | } |
| 428 | NOKPROBE_SYMBOL(do_ptrauth_fault); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 429 | |
| 430 | #define __user_cache_maint(insn, address, res) \ |
| 431 | if (address >= user_addr_max()) { \ |
| 432 | res = -EFAULT; \ |
| 433 | } else { \ |
| 434 | uaccess_ttbr0_enable(); \ |
| 435 | asm volatile ( \ |
| 436 | "1: " insn ", %1\n" \ |
| 437 | " mov %w0, #0\n" \ |
| 438 | "2:\n" \ |
| 439 | " .pushsection .fixup,\"ax\"\n" \ |
| 440 | " .align 2\n" \ |
| 441 | "3: mov %w0, %w2\n" \ |
| 442 | " b 2b\n" \ |
| 443 | " .popsection\n" \ |
| 444 | _ASM_EXTABLE(1b, 3b) \ |
| 445 | : "=r" (res) \ |
| 446 | : "r" (address), "i" (-EFAULT)); \ |
| 447 | uaccess_ttbr0_disable(); \ |
| 448 | } |
| 449 | |
| 450 | static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs) |
| 451 | { |
| 452 | unsigned long address; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 453 | int rt = ESR_ELx_SYS64_ISS_RT(esr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 454 | int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT; |
| 455 | int ret = 0; |
| 456 | |
| 457 | address = untagged_addr(pt_regs_read_reg(regs, rt)); |
| 458 | |
| 459 | switch (crm) { |
| 460 | case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */ |
| 461 | __user_cache_maint("dc civac", address, ret); |
| 462 | break; |
| 463 | case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */ |
| 464 | __user_cache_maint("dc civac", address, ret); |
| 465 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 466 | case ESR_ELx_SYS64_ISS_CRM_DC_CVADP: /* DC CVADP */ |
| 467 | __user_cache_maint("sys 3, c7, c13, 1", address, ret); |
| 468 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 469 | case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */ |
| 470 | __user_cache_maint("sys 3, c7, c12, 1", address, ret); |
| 471 | break; |
| 472 | case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */ |
| 473 | __user_cache_maint("dc civac", address, ret); |
| 474 | break; |
| 475 | case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */ |
| 476 | __user_cache_maint("ic ivau", address, ret); |
| 477 | break; |
| 478 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 479 | force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
| 483 | if (ret) |
| 484 | arm64_notify_segfault(address); |
| 485 | else |
| 486 | arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); |
| 487 | } |
| 488 | |
| 489 | static void ctr_read_handler(unsigned int esr, struct pt_regs *regs) |
| 490 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 491 | int rt = ESR_ELx_SYS64_ISS_RT(esr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 492 | unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0); |
| 493 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 494 | if (cpus_have_const_cap(ARM64_WORKAROUND_1542419)) { |
| 495 | /* Hide DIC so that we can trap the unnecessary maintenance...*/ |
| 496 | val &= ~BIT(CTR_DIC_SHIFT); |
| 497 | |
| 498 | /* ... and fake IminLine to reduce the number of traps. */ |
| 499 | val &= ~CTR_IMINLINE_MASK; |
| 500 | val |= (PAGE_SHIFT - 2) & CTR_IMINLINE_MASK; |
| 501 | } |
| 502 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 503 | pt_regs_write_reg(regs, rt, val); |
| 504 | |
| 505 | arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); |
| 506 | } |
| 507 | |
| 508 | static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs) |
| 509 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 510 | int rt = ESR_ELx_SYS64_ISS_RT(esr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 511 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 512 | pt_regs_write_reg(regs, rt, arch_timer_read_counter()); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 513 | arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); |
| 514 | } |
| 515 | |
| 516 | static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs) |
| 517 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 518 | int rt = ESR_ELx_SYS64_ISS_RT(esr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 519 | |
| 520 | pt_regs_write_reg(regs, rt, arch_timer_get_rate()); |
| 521 | arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); |
| 522 | } |
| 523 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 524 | static void mrs_handler(unsigned int esr, struct pt_regs *regs) |
| 525 | { |
| 526 | u32 sysreg, rt; |
| 527 | |
| 528 | rt = ESR_ELx_SYS64_ISS_RT(esr); |
| 529 | sysreg = esr_sys64_to_sysreg(esr); |
| 530 | |
| 531 | if (do_emulate_mrs(regs, sysreg, rt) != 0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 532 | force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static void wfi_handler(unsigned int esr, struct pt_regs *regs) |
| 536 | { |
| 537 | arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); |
| 538 | } |
| 539 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 540 | struct sys64_hook { |
| 541 | unsigned int esr_mask; |
| 542 | unsigned int esr_val; |
| 543 | void (*handler)(unsigned int esr, struct pt_regs *regs); |
| 544 | }; |
| 545 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 546 | static const struct sys64_hook sys64_hooks[] = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 547 | { |
| 548 | .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK, |
| 549 | .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL, |
| 550 | .handler = user_cache_maint_handler, |
| 551 | }, |
| 552 | { |
| 553 | /* Trap read access to CTR_EL0 */ |
| 554 | .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK, |
| 555 | .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ, |
| 556 | .handler = ctr_read_handler, |
| 557 | }, |
| 558 | { |
| 559 | /* Trap read access to CNTVCT_EL0 */ |
| 560 | .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK, |
| 561 | .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT, |
| 562 | .handler = cntvct_read_handler, |
| 563 | }, |
| 564 | { |
| 565 | /* Trap read access to CNTFRQ_EL0 */ |
| 566 | .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK, |
| 567 | .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ, |
| 568 | .handler = cntfrq_read_handler, |
| 569 | }, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 570 | { |
| 571 | /* Trap read access to CPUID registers */ |
| 572 | .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK, |
| 573 | .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL, |
| 574 | .handler = mrs_handler, |
| 575 | }, |
| 576 | { |
| 577 | /* Trap WFI instructions executed in userspace */ |
| 578 | .esr_mask = ESR_ELx_WFx_MASK, |
| 579 | .esr_val = ESR_ELx_WFx_WFI_VAL, |
| 580 | .handler = wfi_handler, |
| 581 | }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 582 | {}, |
| 583 | }; |
| 584 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 585 | #ifdef CONFIG_COMPAT |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 586 | static bool cp15_cond_valid(unsigned int esr, struct pt_regs *regs) |
| 587 | { |
| 588 | int cond; |
| 589 | |
| 590 | /* Only a T32 instruction can trap without CV being set */ |
| 591 | if (!(esr & ESR_ELx_CV)) { |
| 592 | u32 it; |
| 593 | |
| 594 | it = compat_get_it_state(regs); |
| 595 | if (!it) |
| 596 | return true; |
| 597 | |
| 598 | cond = it >> 4; |
| 599 | } else { |
| 600 | cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT; |
| 601 | } |
| 602 | |
| 603 | return aarch32_opcode_cond_checks[cond](regs->pstate); |
| 604 | } |
| 605 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 606 | static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs) |
| 607 | { |
| 608 | int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT; |
| 609 | |
| 610 | pt_regs_write_reg(regs, reg, arch_timer_get_rate()); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 611 | arm64_skip_faulting_instruction(regs, 4); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static const struct sys64_hook cp15_32_hooks[] = { |
| 615 | { |
| 616 | .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK, |
| 617 | .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ, |
| 618 | .handler = compat_cntfrq_read_handler, |
| 619 | }, |
| 620 | {}, |
| 621 | }; |
| 622 | |
| 623 | static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs) |
| 624 | { |
| 625 | int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT; |
| 626 | int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT; |
| 627 | u64 val = arch_timer_read_counter(); |
| 628 | |
| 629 | pt_regs_write_reg(regs, rt, lower_32_bits(val)); |
| 630 | pt_regs_write_reg(regs, rt2, upper_32_bits(val)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 631 | arm64_skip_faulting_instruction(regs, 4); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static const struct sys64_hook cp15_64_hooks[] = { |
| 635 | { |
| 636 | .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK, |
| 637 | .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT, |
| 638 | .handler = compat_cntvct_read_handler, |
| 639 | }, |
| 640 | {}, |
| 641 | }; |
| 642 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 643 | void do_cp15instr(unsigned int esr, struct pt_regs *regs) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 644 | { |
| 645 | const struct sys64_hook *hook, *hook_base; |
| 646 | |
| 647 | if (!cp15_cond_valid(esr, regs)) { |
| 648 | /* |
| 649 | * There is no T16 variant of a CP access, so we |
| 650 | * always advance PC by 4 bytes. |
| 651 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 652 | arm64_skip_faulting_instruction(regs, 4); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 653 | return; |
| 654 | } |
| 655 | |
| 656 | switch (ESR_ELx_EC(esr)) { |
| 657 | case ESR_ELx_EC_CP15_32: |
| 658 | hook_base = cp15_32_hooks; |
| 659 | break; |
| 660 | case ESR_ELx_EC_CP15_64: |
| 661 | hook_base = cp15_64_hooks; |
| 662 | break; |
| 663 | default: |
| 664 | do_undefinstr(regs); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | for (hook = hook_base; hook->handler; hook++) |
| 669 | if ((hook->esr_mask & esr) == hook->esr_val) { |
| 670 | hook->handler(esr, regs); |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * New cp15 instructions may previously have been undefined at |
| 676 | * EL0. Fall back to our usual undefined instruction handler |
| 677 | * so that we handle these consistently. |
| 678 | */ |
| 679 | do_undefinstr(regs); |
| 680 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 681 | NOKPROBE_SYMBOL(do_cp15instr); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 682 | #endif |
| 683 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 684 | void do_sysinstr(unsigned int esr, struct pt_regs *regs) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 685 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 686 | const struct sys64_hook *hook; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 687 | |
| 688 | for (hook = sys64_hooks; hook->handler; hook++) |
| 689 | if ((hook->esr_mask & esr) == hook->esr_val) { |
| 690 | hook->handler(esr, regs); |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * New SYS instructions may previously have been undefined at EL0. Fall |
| 696 | * back to our usual undefined instruction handler so that we handle |
| 697 | * these consistently. |
| 698 | */ |
| 699 | do_undefinstr(regs); |
| 700 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 701 | NOKPROBE_SYMBOL(do_sysinstr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 702 | |
| 703 | static const char *esr_class_str[] = { |
| 704 | [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC", |
| 705 | [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized", |
| 706 | [ESR_ELx_EC_WFx] = "WFI/WFE", |
| 707 | [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC", |
| 708 | [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC", |
| 709 | [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC", |
| 710 | [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC", |
| 711 | [ESR_ELx_EC_FP_ASIMD] = "ASIMD", |
| 712 | [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 713 | [ESR_ELx_EC_PAC] = "PAC", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 714 | [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 715 | [ESR_ELx_EC_BTI] = "BTI", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 716 | [ESR_ELx_EC_ILL] = "PSTATE.IL", |
| 717 | [ESR_ELx_EC_SVC32] = "SVC (AArch32)", |
| 718 | [ESR_ELx_EC_HVC32] = "HVC (AArch32)", |
| 719 | [ESR_ELx_EC_SMC32] = "SMC (AArch32)", |
| 720 | [ESR_ELx_EC_SVC64] = "SVC (AArch64)", |
| 721 | [ESR_ELx_EC_HVC64] = "HVC (AArch64)", |
| 722 | [ESR_ELx_EC_SMC64] = "SMC (AArch64)", |
| 723 | [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)", |
| 724 | [ESR_ELx_EC_SVE] = "SVE", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 725 | [ESR_ELx_EC_ERET] = "ERET/ERETAA/ERETAB", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 726 | [ESR_ELx_EC_FPAC] = "FPAC", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 727 | [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF", |
| 728 | [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)", |
| 729 | [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)", |
| 730 | [ESR_ELx_EC_PC_ALIGN] = "PC Alignment", |
| 731 | [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)", |
| 732 | [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)", |
| 733 | [ESR_ELx_EC_SP_ALIGN] = "SP Alignment", |
| 734 | [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)", |
| 735 | [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)", |
| 736 | [ESR_ELx_EC_SERROR] = "SError", |
| 737 | [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)", |
| 738 | [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)", |
| 739 | [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)", |
| 740 | [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)", |
| 741 | [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)", |
| 742 | [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)", |
| 743 | [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)", |
| 744 | [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)", |
| 745 | [ESR_ELx_EC_BRK64] = "BRK (AArch64)", |
| 746 | }; |
| 747 | |
| 748 | const char *esr_get_class_string(u32 esr) |
| 749 | { |
| 750 | return esr_class_str[ESR_ELx_EC(esr)]; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * bad_mode handles the impossible case in the exception vector. This is always |
| 755 | * fatal. |
| 756 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 757 | asmlinkage void notrace bad_mode(struct pt_regs *regs, int reason, unsigned int esr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 758 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 759 | arm64_enter_nmi(regs); |
| 760 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 761 | console_verbose(); |
| 762 | |
| 763 | pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n", |
| 764 | handler[reason], smp_processor_id(), esr, |
| 765 | esr_get_class_string(esr)); |
| 766 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 767 | __show_regs(regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 768 | local_daif_mask(); |
| 769 | panic("bad mode"); |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * bad_el0_sync handles unexpected, but potentially recoverable synchronous |
| 774 | * exceptions taken from EL0. Unlike bad_mode, this returns. |
| 775 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 776 | void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 777 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 778 | void __user *pc = (void __user *)instruction_pointer(regs); |
| 779 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 780 | current->thread.fault_address = 0; |
| 781 | current->thread.fault_code = esr; |
| 782 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 783 | arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc, |
| 784 | "Bad EL0 synchronous exception"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | #ifdef CONFIG_VMAP_STACK |
| 788 | |
| 789 | DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack) |
| 790 | __aligned(16); |
| 791 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 792 | asmlinkage void noinstr handle_bad_stack(struct pt_regs *regs) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 793 | { |
| 794 | unsigned long tsk_stk = (unsigned long)current->stack; |
| 795 | unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr); |
| 796 | unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack); |
| 797 | unsigned int esr = read_sysreg(esr_el1); |
| 798 | unsigned long far = read_sysreg(far_el1); |
| 799 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 800 | arm64_enter_nmi(regs); |
| 801 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 802 | console_verbose(); |
| 803 | pr_emerg("Insufficient stack space to handle exception!"); |
| 804 | |
| 805 | pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr)); |
| 806 | pr_emerg("FAR: 0x%016lx\n", far); |
| 807 | |
| 808 | pr_emerg("Task stack: [0x%016lx..0x%016lx]\n", |
| 809 | tsk_stk, tsk_stk + THREAD_SIZE); |
| 810 | pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 811 | irq_stk, irq_stk + IRQ_STACK_SIZE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 812 | pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n", |
| 813 | ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE); |
| 814 | |
| 815 | __show_regs(regs); |
| 816 | |
| 817 | /* |
| 818 | * We use nmi_panic to limit the potential for recusive overflows, and |
| 819 | * to get a better stack trace. |
| 820 | */ |
| 821 | nmi_panic(NULL, "kernel stack overflow"); |
| 822 | cpu_park_loop(); |
| 823 | } |
| 824 | #endif |
| 825 | |
| 826 | void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr) |
| 827 | { |
| 828 | console_verbose(); |
| 829 | |
| 830 | pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n", |
| 831 | smp_processor_id(), esr, esr_get_class_string(esr)); |
| 832 | if (regs) |
| 833 | __show_regs(regs); |
| 834 | |
| 835 | nmi_panic(regs, "Asynchronous SError Interrupt"); |
| 836 | |
| 837 | cpu_park_loop(); |
| 838 | unreachable(); |
| 839 | } |
| 840 | |
| 841 | bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr) |
| 842 | { |
| 843 | u32 aet = arm64_ras_serror_get_severity(esr); |
| 844 | |
| 845 | switch (aet) { |
| 846 | case ESR_ELx_AET_CE: /* corrected error */ |
| 847 | case ESR_ELx_AET_UEO: /* restartable, not yet consumed */ |
| 848 | /* |
| 849 | * The CPU can make progress. We may take UEO again as |
| 850 | * a more severe error. |
| 851 | */ |
| 852 | return false; |
| 853 | |
| 854 | case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */ |
| 855 | case ESR_ELx_AET_UER: /* Uncorrected Recoverable */ |
| 856 | /* |
| 857 | * The CPU can't make progress. The exception may have |
| 858 | * been imprecise. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 859 | * |
| 860 | * Neoverse-N1 #1349291 means a non-KVM SError reported as |
| 861 | * Unrecoverable should be treated as Uncontainable. We |
| 862 | * call arm64_serror_panic() in both cases. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 863 | */ |
| 864 | return true; |
| 865 | |
| 866 | case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */ |
| 867 | default: |
| 868 | /* Error has been silently propagated */ |
| 869 | arm64_serror_panic(regs, esr); |
| 870 | } |
| 871 | } |
| 872 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 873 | asmlinkage void noinstr do_serror(struct pt_regs *regs, unsigned int esr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 874 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 875 | arm64_enter_nmi(regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 876 | |
| 877 | /* non-RAS errors are not containable */ |
| 878 | if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr)) |
| 879 | arm64_serror_panic(regs, esr); |
| 880 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 881 | arm64_exit_nmi(regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /* GENERIC_BUG traps */ |
| 885 | |
| 886 | int is_valid_bugaddr(unsigned long addr) |
| 887 | { |
| 888 | /* |
| 889 | * bug_handler() only called for BRK #BUG_BRK_IMM. |
| 890 | * So the answer is trivial -- any spurious instances with no |
| 891 | * bug table entry will be rejected by report_bug() and passed |
| 892 | * back to the debug-monitors code and handled as a fatal |
| 893 | * unexpected debug exception. |
| 894 | */ |
| 895 | return 1; |
| 896 | } |
| 897 | |
| 898 | static int bug_handler(struct pt_regs *regs, unsigned int esr) |
| 899 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 900 | switch (report_bug(regs->pc, regs)) { |
| 901 | case BUG_TRAP_TYPE_BUG: |
| 902 | die("Oops - BUG", regs, 0); |
| 903 | break; |
| 904 | |
| 905 | case BUG_TRAP_TYPE_WARN: |
| 906 | break; |
| 907 | |
| 908 | default: |
| 909 | /* unknown/unrecognised bug trap type */ |
| 910 | return DBG_HOOK_ERROR; |
| 911 | } |
| 912 | |
| 913 | /* If thread survives, skip over the BUG instruction and continue: */ |
| 914 | arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); |
| 915 | return DBG_HOOK_HANDLED; |
| 916 | } |
| 917 | |
| 918 | static struct break_hook bug_break_hook = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 919 | .fn = bug_handler, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 920 | .imm = BUG_BRK_IMM, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 921 | }; |
| 922 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 923 | static int reserved_fault_handler(struct pt_regs *regs, unsigned int esr) |
| 924 | { |
| 925 | pr_err("%s generated an invalid instruction at %pS!\n", |
| 926 | in_bpf_jit(regs) ? "BPF JIT" : "Kernel text patching", |
| 927 | (void *)instruction_pointer(regs)); |
| 928 | |
| 929 | /* We cannot handle this */ |
| 930 | return DBG_HOOK_ERROR; |
| 931 | } |
| 932 | |
| 933 | static struct break_hook fault_break_hook = { |
| 934 | .fn = reserved_fault_handler, |
| 935 | .imm = FAULT_BRK_IMM, |
| 936 | }; |
| 937 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 938 | #ifdef CONFIG_KASAN_SW_TAGS |
| 939 | |
| 940 | #define KASAN_ESR_RECOVER 0x20 |
| 941 | #define KASAN_ESR_WRITE 0x10 |
| 942 | #define KASAN_ESR_SIZE_MASK 0x0f |
| 943 | #define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK)) |
| 944 | |
| 945 | static int kasan_handler(struct pt_regs *regs, unsigned int esr) |
| 946 | { |
| 947 | bool recover = esr & KASAN_ESR_RECOVER; |
| 948 | bool write = esr & KASAN_ESR_WRITE; |
| 949 | size_t size = KASAN_ESR_SIZE(esr); |
| 950 | u64 addr = regs->regs[0]; |
| 951 | u64 pc = regs->pc; |
| 952 | |
| 953 | kasan_report(addr, size, write, pc); |
| 954 | |
| 955 | /* |
| 956 | * The instrumentation allows to control whether we can proceed after |
| 957 | * a crash was detected. This is done by passing the -recover flag to |
| 958 | * the compiler. Disabling recovery allows to generate more compact |
| 959 | * code. |
| 960 | * |
| 961 | * Unfortunately disabling recovery doesn't work for the kernel right |
| 962 | * now. KASAN reporting is disabled in some contexts (for example when |
| 963 | * the allocator accesses slab object metadata; this is controlled by |
| 964 | * current->kasan_depth). All these accesses are detected by the tool, |
| 965 | * even though the reports for them are not printed. |
| 966 | * |
| 967 | * This is something that might be fixed at some point in the future. |
| 968 | */ |
| 969 | if (!recover) |
| 970 | die("Oops - KASAN", regs, 0); |
| 971 | |
| 972 | /* If thread survives, skip over the brk instruction and continue: */ |
| 973 | arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); |
| 974 | return DBG_HOOK_HANDLED; |
| 975 | } |
| 976 | |
| 977 | static struct break_hook kasan_break_hook = { |
| 978 | .fn = kasan_handler, |
| 979 | .imm = KASAN_BRK_IMM, |
| 980 | .mask = KASAN_BRK_MASK, |
| 981 | }; |
| 982 | #endif |
| 983 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 984 | /* |
| 985 | * Initial handler for AArch64 BRK exceptions |
| 986 | * This handler only used until debug_traps_init(). |
| 987 | */ |
| 988 | int __init early_brk64(unsigned long addr, unsigned int esr, |
| 989 | struct pt_regs *regs) |
| 990 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 991 | #ifdef CONFIG_KASAN_SW_TAGS |
| 992 | unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK; |
| 993 | |
| 994 | if ((comment & ~KASAN_BRK_MASK) == KASAN_BRK_IMM) |
| 995 | return kasan_handler(regs, esr) != DBG_HOOK_HANDLED; |
| 996 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 997 | return bug_handler(regs, esr) != DBG_HOOK_HANDLED; |
| 998 | } |
| 999 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1000 | void __init trap_init(void) |
| 1001 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1002 | register_kernel_break_hook(&bug_break_hook); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1003 | register_kernel_break_hook(&fault_break_hook); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1004 | #ifdef CONFIG_KASAN_SW_TAGS |
| 1005 | register_kernel_break_hook(&kasan_break_hook); |
| 1006 | #endif |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1007 | debug_traps_init(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1008 | } |