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