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