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