blob: f4ddbe9ed3f12fd67f8f7e0afc1ae8e55f187eeb [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Catalin Marinas60ffc302012-03-05 11:49:27 +00002/*
3 * Based on arch/arm/kernel/traps.c
4 *
5 * Copyright (C) 1995-2009 Russell King
6 * Copyright (C) 2012 ARM Ltd.
Catalin Marinas60ffc302012-03-05 11:49:27 +00007 */
8
Dave P Martin9fb74102015-07-24 16:37:48 +01009#include <linux/bug.h>
James Morse26718282019-08-20 18:45:57 +010010#include <linux/context_tracking.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000011#include <linux/signal.h>
12#include <linux/personality.h>
13#include <linux/kallsyms.h>
James Morse26718282019-08-20 18:45:57 +010014#include <linux/kprobes.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000015#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 Molnar3f07c012017-02-08 18:51:30 +010023#include <linux/sched/signal.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010024#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010025#include <linux/sched/task_stack.h>
Mark Rutland872d8322017-07-14 20:30:35 +010026#include <linux/sizes.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000027#include <linux/syscalls.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010028#include <linux/mm_types.h>
Andrey Konovalov41eea9c2018-12-28 00:30:54 -080029#include <linux/kasan.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000030
31#include <asm/atomic.h>
Dave P Martin9fb74102015-07-24 16:37:48 +010032#include <asm/bug.h>
Dave Martinc0cda3b2018-03-26 15:12:28 +010033#include <asm/cpufeature.h>
James Morse0fbeb312017-11-02 12:12:34 +000034#include <asm/daifflags.h>
Will Deacon1442b6e2013-03-16 08:48:13 +000035#include <asm/debug-monitors.h>
Mark Rutland60a1f022014-11-18 12:16:30 +000036#include <asm/esr.h>
Will Deacon0fdb64c2020-09-15 15:48:09 +010037#include <asm/extable.h>
Dave P Martin9fb74102015-07-24 16:37:48 +010038#include <asm/insn.h>
James Morseb6e43c02019-10-25 17:42:10 +010039#include <asm/kprobes.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000040#include <asm/traps.h>
Mark Rutland872d8322017-07-14 20:30:35 +010041#include <asm/smp.h>
Mark Rutlanda9ea0012016-11-03 20:23:05 +000042#include <asm/stack_pointer.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000043#include <asm/stacktrace.h>
44#include <asm/exception.h>
45#include <asm/system_misc.h>
Andre Przywara7dd01ae2016-06-28 18:07:32 +010046#include <asm/sysreg.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000047
48static const char *handler[]= {
49 "Synchronous Abort",
50 "IRQ",
51 "FIQ",
52 "Error"
53};
54
Michael Weiser5ee39a72018-02-01 23:13:38 +010055int show_unhandled_signals = 0;
Catalin Marinas60ffc302012-03-05 11:49:27 +000056
jinho lim7b716652019-06-26 20:50:13 +090057static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
Catalin Marinas60ffc302012-03-05 11:49:27 +000058{
59 unsigned long addr = instruction_pointer(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +000060 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
61 int i;
62
jinho lim7b716652019-06-26 20:50:13 +090063 if (user_mode(regs))
64 return;
65
Catalin Marinas60ffc302012-03-05 11:49:27 +000066 for (i = -4; i < 1; i++) {
67 unsigned int val, bad;
68
jinho lim7b716652019-06-26 20:50:13 +090069 bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
Catalin Marinas60ffc302012-03-05 11:49:27 +000070
71 if (!bad)
72 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
73 else {
74 p += sprintf(p, "bad PC value");
75 break;
76 }
77 }
Catalin Marinas60ffc302012-03-05 11:49:27 +000078
jinho lim7b716652019-06-26 20:50:13 +090079 printk("%sCode: %s\n", lvl, str);
Catalin Marinas60ffc302012-03-05 11:49:27 +000080}
81
Catalin Marinas60ffc302012-03-05 11:49:27 +000082#ifdef CONFIG_PREEMPT
83#define S_PREEMPT " PREEMPT"
Thomas Gleixner7ef858d2019-10-15 21:17:49 +020084#elif defined(CONFIG_PREEMPT_RT)
85#define S_PREEMPT " PREEMPT_RT"
Catalin Marinas60ffc302012-03-05 11:49:27 +000086#else
87#define S_PREEMPT ""
88#endif
Thomas Gleixner7ef858d2019-10-15 21:17:49 +020089
Catalin Marinas60ffc302012-03-05 11:49:27 +000090#define S_SMP " SMP"
Catalin Marinas60ffc302012-03-05 11:49:27 +000091
Mark Rutland876e7a32016-11-03 20:23:06 +000092static int __die(const char *str, int err, struct pt_regs *regs)
Catalin Marinas60ffc302012-03-05 11:49:27 +000093{
Catalin Marinas60ffc302012-03-05 11:49:27 +000094 static int die_counter;
95 int ret;
96
97 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
98 str, err, ++die_counter);
99
100 /* trap and error numbers are mostly meaningless on ARM */
101 ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
102 if (ret == NOTIFY_STOP)
103 return ret;
104
105 print_modules();
Will Deacon1e6f54402019-04-08 17:56:34 +0100106 show_regs(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000107
jinho lim7b716652019-06-26 20:50:13 +0900108 dump_kernel_instr(KERN_EMERG, regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000109
110 return ret;
111}
112
113static DEFINE_RAW_SPINLOCK(die_lock);
114
115/*
116 * This function is protected against re-entrancy.
117 */
118void die(const char *str, struct pt_regs *regs, int err)
119{
Catalin Marinas60ffc302012-03-05 11:49:27 +0000120 int ret;
Qiao Zhou6f44a0b2017-07-07 17:29:34 +0800121 unsigned long flags;
122
123 raw_spin_lock_irqsave(&die_lock, flags);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000124
125 oops_enter();
126
Catalin Marinas60ffc302012-03-05 11:49:27 +0000127 console_verbose();
128 bust_spinlocks(1);
Mark Rutland876e7a32016-11-03 20:23:06 +0000129 ret = __die(str, err, regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000130
Mark Rutland876e7a32016-11-03 20:23:06 +0000131 if (regs && kexec_should_crash(current))
Catalin Marinas60ffc302012-03-05 11:49:27 +0000132 crash_kexec(regs);
133
134 bust_spinlocks(0);
Rusty Russell373d4d02013-01-21 17:17:39 +1030135 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000136 oops_exit();
137
138 if (in_interrupt())
Yue Hub4c97122020-08-04 16:53:47 +0800139 panic("%s: Fatal exception in interrupt", str);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000140 if (panic_on_oops)
Yue Hub4c97122020-08-04 16:53:47 +0800141 panic("%s: Fatal exception", str);
Qiao Zhou6f44a0b2017-07-07 17:29:34 +0800142
143 raw_spin_unlock_irqrestore(&die_lock, flags);
144
Catalin Marinas60ffc302012-03-05 11:49:27 +0000145 if (ret != NOTIFY_STOP)
146 do_exit(SIGSEGV);
147}
148
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200149static void arm64_show_signal(int signo, const char *str)
Will Deacona26731d2018-02-20 15:08:51 +0000150{
151 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
152 DEFAULT_RATELIMIT_BURST);
Eric W. Biederman24b8f792018-09-22 00:38:41 +0200153 struct task_struct *tsk = current;
Will Deacona1ece822018-02-20 13:46:05 +0000154 unsigned int esr = tsk->thread.fault_code;
155 struct pt_regs *regs = task_pt_regs(tsk);
156
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200157 /* Leave if the signal won't be shown */
158 if (!show_unhandled_signals ||
159 !unhandled_signal(tsk, signo) ||
160 !__ratelimit(&rs))
161 return;
Will Deacona1ece822018-02-20 13:46:05 +0000162
163 pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
164 if (esr)
165 pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);
166
167 pr_cont("%s", str);
168 print_vma_addr(KERN_CONT " in ", regs->pc);
169 pr_cont("\n");
170 __show_regs(regs);
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200171}
Will Deacona1ece822018-02-20 13:46:05 +0000172
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800173void arm64_force_sig_fault(int signo, int code, unsigned long far,
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200174 const char *str)
175{
176 arm64_show_signal(signo, str);
Eric W. Biedermand76cac62019-05-23 11:11:19 -0500177 if (signo == SIGKILL)
Eric W. Biederman3cf5d072019-05-23 10:17:27 -0500178 force_sig(SIGKILL);
Eric W. Biedermand76cac62019-05-23 11:11:19 -0500179 else
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800180 force_sig_fault(signo, code, (void __user *)far);
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200181}
182
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800183void arm64_force_sig_mceerr(int code, unsigned long far, short lsb,
Eric W. Biedermanb4d55572018-09-22 10:37:15 +0200184 const char *str)
185{
186 arm64_show_signal(SIGBUS, str);
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800187 force_sig_mceerr(code, (void __user *)far, lsb);
Eric W. Biedermanb4d55572018-09-22 10:37:15 +0200188}
189
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800190void arm64_force_sig_ptrace_errno_trap(int errno, unsigned long far,
Eric W. Biedermanf3a900b2018-09-22 10:52:41 +0200191 const char *str)
192{
193 arm64_show_signal(SIGTRAP, str);
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800194 force_sig_ptrace_errno_trap(errno, (void __user *)far);
Will Deacona1ece822018-02-20 13:46:05 +0000195}
196
Catalin Marinas60ffc302012-03-05 11:49:27 +0000197void arm64_notify_die(const char *str, struct pt_regs *regs,
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800198 int signo, int sicode, unsigned long far,
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200199 int err)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000200{
Catalin Marinas91413002014-04-06 23:04:12 +0100201 if (user_mode(regs)) {
Will Deacona1ece822018-02-20 13:46:05 +0000202 WARN_ON(regs != current_pt_regs());
Catalin Marinas91413002014-04-06 23:04:12 +0100203 current->thread.fault_address = 0;
204 current->thread.fault_code = err;
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200205
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800206 arm64_force_sig_fault(signo, sicode, far, str);
Catalin Marinas91413002014-04-06 23:04:12 +0100207 } else {
Catalin Marinas60ffc302012-03-05 11:49:27 +0000208 die(str, regs, err);
Catalin Marinas91413002014-04-06 23:04:12 +0100209 }
Catalin Marinas60ffc302012-03-05 11:49:27 +0000210}
211
Dave Martind2c2ee42020-03-16 16:50:50 +0000212#ifdef CONFIG_COMPAT
213#define PSTATE_IT_1_0_SHIFT 25
214#define PSTATE_IT_1_0_MASK (0x3 << PSTATE_IT_1_0_SHIFT)
215#define PSTATE_IT_7_2_SHIFT 10
216#define PSTATE_IT_7_2_MASK (0x3f << PSTATE_IT_7_2_SHIFT)
217
218static u32 compat_get_it_state(struct pt_regs *regs)
219{
220 u32 it, pstate = regs->pstate;
221
222 it = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
223 it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
224
225 return it;
226}
227
228static void compat_set_it_state(struct pt_regs *regs, u32 it)
229{
230 u32 pstate_it;
231
232 pstate_it = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
233 pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
234
235 regs->pstate &= ~PSR_AA32_IT_MASK;
236 regs->pstate |= pstate_it;
237}
238
239static void advance_itstate(struct pt_regs *regs)
240{
241 u32 it;
242
243 /* ARM mode */
244 if (!(regs->pstate & PSR_AA32_T_BIT) ||
245 !(regs->pstate & PSR_AA32_IT_MASK))
246 return;
247
248 it = compat_get_it_state(regs);
249
250 /*
251 * If this is the last instruction of the block, wipe the IT
252 * state. Otherwise advance it.
253 */
254 if (!(it & 7))
255 it = 0;
256 else
257 it = (it & 0xe0) | ((it << 1) & 0x1f);
258
259 compat_set_it_state(regs, it);
260}
261#else
262static void advance_itstate(struct pt_regs *regs)
263{
264}
265#endif
Dave Martin172a7972020-03-16 16:50:49 +0000266
Julien Thierry6436bee2017-10-25 10:04:33 +0100267void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
268{
269 regs->pc += size;
270
271 /*
272 * If we were single stepping, we want to get the step exception after
273 * we return from the trap.
274 */
Mark Rutland9478f192018-04-03 11:22:51 +0100275 if (user_mode(regs))
276 user_fastforward_single_step(current);
Dave Martin172a7972020-03-16 16:50:49 +0000277
Dave Martind2c2ee42020-03-16 16:50:50 +0000278 if (compat_user_mode(regs))
Dave Martin172a7972020-03-16 16:50:49 +0000279 advance_itstate(regs);
Dave Martin0537c4c2020-03-16 16:50:51 +0000280 else
281 regs->pstate &= ~PSR_BTYPE_MASK;
Julien Thierry6436bee2017-10-25 10:04:33 +0100282}
283
Punit Agrawal9b79f522014-11-18 11:41:22 +0000284static LIST_HEAD(undef_hook);
285static DEFINE_RAW_SPINLOCK(undef_lock);
286
287void register_undef_hook(struct undef_hook *hook)
288{
289 unsigned long flags;
290
291 raw_spin_lock_irqsave(&undef_lock, flags);
292 list_add(&hook->node, &undef_hook);
293 raw_spin_unlock_irqrestore(&undef_lock, flags);
294}
295
296void unregister_undef_hook(struct undef_hook *hook)
297{
298 unsigned long flags;
299
300 raw_spin_lock_irqsave(&undef_lock, flags);
301 list_del(&hook->node);
302 raw_spin_unlock_irqrestore(&undef_lock, flags);
303}
304
305static int call_undef_hook(struct pt_regs *regs)
306{
307 struct undef_hook *hook;
308 unsigned long flags;
309 u32 instr;
310 int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
311 void __user *pc = (void __user *)instruction_pointer(regs);
312
Will Deacon0bf0f442018-08-07 13:43:06 +0100313 if (!user_mode(regs)) {
314 __le32 instr_le;
Christoph Hellwig25f12ae2020-06-17 09:37:55 +0200315 if (get_kernel_nofault(instr_le, (__force __le32 *)pc))
Will Deacon0bf0f442018-08-07 13:43:06 +0100316 goto exit;
317 instr = le32_to_cpu(instr_le);
318 } else if (compat_thumb_mode(regs)) {
Punit Agrawal9b79f522014-11-18 11:41:22 +0000319 /* 16-bit Thumb instruction */
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200320 __le16 instr_le;
321 if (get_user(instr_le, (__le16 __user *)pc))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000322 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200323 instr = le16_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000324 if (aarch32_insn_is_wide(instr)) {
325 u32 instr2;
326
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200327 if (get_user(instr_le, (__le16 __user *)(pc + 2)))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000328 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200329 instr2 = le16_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000330 instr = (instr << 16) | instr2;
331 }
332 } else {
333 /* 32-bit ARM instruction */
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200334 __le32 instr_le;
335 if (get_user(instr_le, (__le32 __user *)pc))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000336 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200337 instr = le32_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000338 }
339
340 raw_spin_lock_irqsave(&undef_lock, flags);
341 list_for_each_entry(hook, &undef_hook, node)
342 if ((instr & hook->instr_mask) == hook->instr_val &&
343 (regs->pstate & hook->pstate_mask) == hook->pstate_val)
344 fn = hook->fn;
345
346 raw_spin_unlock_irqrestore(&undef_lock, flags);
347exit:
348 return fn ? fn(regs, instr) : 1;
349}
350
Amit Daniel Kachhap4ef333b2020-09-14 14:06:52 +0530351void force_signal_inject(int signal, int code, unsigned long address, unsigned int err)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000352{
Andre Przywara390bf172016-06-28 18:07:31 +0100353 const char *desc;
Will Deacon2c9120f32018-02-20 14:16:29 +0000354 struct pt_regs *regs = current_pt_regs();
355
Will Deacon8a604192018-08-14 16:24:54 +0100356 if (WARN_ON(!user_mode(regs)))
357 return;
358
Andre Przywara390bf172016-06-28 18:07:31 +0100359 switch (signal) {
360 case SIGILL:
361 desc = "undefined instruction";
362 break;
363 case SIGSEGV:
364 desc = "illegal memory access";
365 break;
366 default:
Dave Martinbc0ee472017-10-31 15:51:05 +0000367 desc = "unknown or unrecoverable error";
Andre Przywara390bf172016-06-28 18:07:31 +0100368 break;
369 }
370
Will Deacona7e6f1c2018-02-20 18:08:40 +0000371 /* Force signals we don't understand to SIGKILL */
Mark Rutlandb2d71b32018-04-16 16:45:01 +0100372 if (WARN_ON(signal != SIGKILL &&
Will Deacona7e6f1c2018-02-20 18:08:40 +0000373 siginfo_layout(signal, code) != SIL_FAULT)) {
374 signal = SIGKILL;
375 }
376
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800377 arm64_notify_die(desc, regs, signal, code, address, err);
Andre Przywara390bf172016-06-28 18:07:31 +0100378}
379
380/*
381 * Set up process info to signal segmentation fault - called on access error.
382 */
Will Deacon2c9120f32018-02-20 14:16:29 +0000383void arm64_notify_segfault(unsigned long addr)
Andre Przywara390bf172016-06-28 18:07:31 +0100384{
385 int code;
386
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700387 mmap_read_lock(current->mm);
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800388 if (find_vma(current->mm, untagged_addr(addr)) == NULL)
Andre Przywara390bf172016-06-28 18:07:31 +0100389 code = SEGV_MAPERR;
390 else
391 code = SEGV_ACCERR;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700392 mmap_read_unlock(current->mm);
Andre Przywara390bf172016-06-28 18:07:31 +0100393
Amit Daniel Kachhap4ef333b2020-09-14 14:06:52 +0530394 force_signal_inject(SIGSEGV, code, addr, 0);
Andre Przywara390bf172016-06-28 18:07:31 +0100395}
396
James Morseafa7c0e2019-10-25 17:42:15 +0100397void do_undefinstr(struct pt_regs *regs)
Andre Przywara390bf172016-06-28 18:07:31 +0100398{
Catalin Marinas60ffc302012-03-05 11:49:27 +0000399 /* check for AArch32 breakpoint instructions */
Will Deacon1442b6e2013-03-16 08:48:13 +0000400 if (!aarch32_break_handler(regs))
Catalin Marinas60ffc302012-03-05 11:49:27 +0000401 return;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000402
Punit Agrawal9b79f522014-11-18 11:41:22 +0000403 if (call_undef_hook(regs) == 0)
404 return;
405
Will Deacon0bf0f442018-08-07 13:43:06 +0100406 BUG_ON(!user_mode(regs));
Amit Daniel Kachhap4ef333b2020-09-14 14:06:52 +0530407 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000408}
James Morseb6e43c02019-10-25 17:42:10 +0100409NOKPROBE_SYMBOL(do_undefinstr);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000410
Dave Martin8ef8f3602020-03-16 16:50:45 +0000411void do_bti(struct pt_regs *regs)
412{
413 BUG_ON(!user_mode(regs));
Amit Daniel Kachhap4ef333b2020-09-14 14:06:52 +0530414 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
Dave Martin8ef8f3602020-03-16 16:50:45 +0000415}
416NOKPROBE_SYMBOL(do_bti);
417
Amit Daniel Kachhape16aeb02020-09-14 14:06:53 +0530418void do_ptrauth_fault(struct pt_regs *regs, unsigned int esr)
419{
420 /*
421 * Unexpected FPAC exception or pointer authentication failure in
422 * the kernel: kill the task before it does any more harm.
423 */
424 BUG_ON(!user_mode(regs));
425 force_signal_inject(SIGILL, ILL_ILLOPN, regs->pc, esr);
426}
427NOKPROBE_SYMBOL(do_ptrauth_fault);
428
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100429#define __user_cache_maint(insn, address, res) \
Kristina Martsenko81cddd62017-05-03 16:37:45 +0100430 if (address >= user_addr_max()) { \
Andre Przywara87261d12016-10-19 14:40:54 +0100431 res = -EFAULT; \
Catalin Marinas39bc88e2016-09-02 14:54:03 +0100432 } else { \
433 uaccess_ttbr0_enable(); \
Andre Przywara87261d12016-10-19 14:40:54 +0100434 asm volatile ( \
435 "1: " insn ", %1\n" \
436 " mov %w0, #0\n" \
437 "2:\n" \
438 " .pushsection .fixup,\"ax\"\n" \
439 " .align 2\n" \
440 "3: mov %w0, %w2\n" \
441 " b 2b\n" \
442 " .popsection\n" \
443 _ASM_EXTABLE(1b, 3b) \
444 : "=r" (res) \
Catalin Marinas39bc88e2016-09-02 14:54:03 +0100445 : "r" (address), "i" (-EFAULT)); \
446 uaccess_ttbr0_disable(); \
447 }
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100448
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100449static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100450{
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800451 unsigned long tagged_address, address;
Anshuman Khandual1c839142018-09-20 09:36:19 +0530452 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100453 int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
454 int ret = 0;
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100455
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800456 tagged_address = pt_regs_read_reg(regs, rt);
457 address = untagged_addr(tagged_address);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100458
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100459 switch (crm) {
460 case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
461 __user_cache_maint("dc civac", address, ret);
462 break;
463 case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
464 __user_cache_maint("dc civac", address, ret);
465 break;
Andrew Murrayd16ed4102019-04-09 10:52:42 +0100466 case ESR_ELx_SYS64_ISS_CRM_DC_CVADP: /* DC CVADP */
467 __user_cache_maint("sys 3, c7, c13, 1", address, ret);
468 break;
Robin Murphye1bc5d12017-07-25 11:55:41 +0100469 case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
470 __user_cache_maint("sys 3, c7, c12, 1", address, ret);
471 break;
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100472 case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
473 __user_cache_maint("dc civac", address, ret);
474 break;
475 case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
476 __user_cache_maint("ic ivau", address, ret);
477 break;
478 default:
Amit Daniel Kachhap4ef333b2020-09-14 14:06:52 +0530479 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100480 return;
481 }
482
483 if (ret)
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800484 arm64_notify_segfault(tagged_address);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100485 else
Julien Thierry6436bee2017-10-25 10:04:33 +0100486 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100487}
488
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100489static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
490{
Anshuman Khandual1c839142018-09-20 09:36:19 +0530491 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Mark Rutland8b6e70f2017-02-09 15:19:19 +0000492 unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100493
James Morseee9d90b2019-10-17 18:42:59 +0100494 if (cpus_have_const_cap(ARM64_WORKAROUND_1542419)) {
495 /* Hide DIC so that we can trap the unnecessary maintenance...*/
James Morse05460842019-10-17 18:42:58 +0100496 val &= ~BIT(CTR_DIC_SHIFT);
497
James Morseee9d90b2019-10-17 18:42:59 +0100498 /* ... and fake IminLine to reduce the number of traps. */
499 val &= ~CTR_IMINLINE_MASK;
500 val |= (PAGE_SHIFT - 2) & CTR_IMINLINE_MASK;
501 }
502
Mark Rutland8b6e70f2017-02-09 15:19:19 +0000503 pt_regs_write_reg(regs, rt, val);
504
Julien Thierry6436bee2017-10-25 10:04:33 +0100505 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100506}
507
Marc Zyngier6126ce02017-02-01 11:48:58 +0000508static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
509{
Anshuman Khandual1c839142018-09-20 09:36:19 +0530510 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Marc Zyngier6126ce02017-02-01 11:48:58 +0000511
Marc Zyngierdea86a82019-04-08 16:49:03 +0100512 pt_regs_write_reg(regs, rt, arch_timer_read_counter());
Julien Thierry6436bee2017-10-25 10:04:33 +0100513 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Marc Zyngier6126ce02017-02-01 11:48:58 +0000514}
515
Marc Zyngier98421192017-04-24 09:04:03 +0100516static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
517{
Anshuman Khandual1c839142018-09-20 09:36:19 +0530518 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Marc Zyngier98421192017-04-24 09:04:03 +0100519
Marc Zyngierc6f97ad2017-07-21 18:15:27 +0100520 pt_regs_write_reg(regs, rt, arch_timer_get_rate());
Julien Thierry6436bee2017-10-25 10:04:33 +0100521 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Marc Zyngier98421192017-04-24 09:04:03 +0100522}
523
Anshuman Khandual21f84792018-09-20 09:36:21 +0530524static void mrs_handler(unsigned int esr, struct pt_regs *regs)
525{
526 u32 sysreg, rt;
527
528 rt = ESR_ELx_SYS64_ISS_RT(esr);
529 sysreg = esr_sys64_to_sysreg(esr);
530
531 if (do_emulate_mrs(regs, sysreg, rt) != 0)
Amit Daniel Kachhap4ef333b2020-09-14 14:06:52 +0530532 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
Anshuman Khandual21f84792018-09-20 09:36:21 +0530533}
534
Marc Zyngierc219bc42018-10-01 12:19:43 +0100535static void wfi_handler(unsigned int esr, struct pt_regs *regs)
536{
537 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
538}
539
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100540struct sys64_hook {
541 unsigned int esr_mask;
542 unsigned int esr_val;
543 void (*handler)(unsigned int esr, struct pt_regs *regs);
544};
545
Mark Rutland37143dc2019-08-13 15:16:39 +0100546static const struct sys64_hook sys64_hooks[] = {
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100547 {
548 .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
549 .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
550 .handler = user_cache_maint_handler,
551 },
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100552 {
553 /* Trap read access to CTR_EL0 */
554 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
555 .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
556 .handler = ctr_read_handler,
557 },
Marc Zyngier6126ce02017-02-01 11:48:58 +0000558 {
559 /* Trap read access to CNTVCT_EL0 */
560 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
561 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
562 .handler = cntvct_read_handler,
563 },
Marc Zyngier98421192017-04-24 09:04:03 +0100564 {
565 /* Trap read access to CNTFRQ_EL0 */
566 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
567 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
568 .handler = cntfrq_read_handler,
569 },
Anshuman Khandual21f84792018-09-20 09:36:21 +0530570 {
571 /* Trap read access to CPUID registers */
572 .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
573 .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
574 .handler = mrs_handler,
575 },
Marc Zyngierc219bc42018-10-01 12:19:43 +0100576 {
577 /* Trap WFI instructions executed in userspace */
578 .esr_mask = ESR_ELx_WFx_MASK,
579 .esr_val = ESR_ELx_WFx_WFI_VAL,
580 .handler = wfi_handler,
581 },
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100582 {},
583};
584
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100585#ifdef CONFIG_COMPAT
Marc Zyngier1f1c0142018-09-27 17:15:30 +0100586static bool cp15_cond_valid(unsigned int esr, struct pt_regs *regs)
587{
588 int cond;
589
590 /* Only a T32 instruction can trap without CV being set */
591 if (!(esr & ESR_ELx_CV)) {
592 u32 it;
593
594 it = compat_get_it_state(regs);
595 if (!it)
596 return true;
597
598 cond = it >> 4;
599 } else {
600 cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
601 }
602
603 return aarch32_opcode_cond_checks[cond](regs->pstate);
604}
605
Marc Zyngier32a3e632018-09-27 17:15:33 +0100606static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
607{
608 int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
609
610 pt_regs_write_reg(regs, reg, arch_timer_get_rate());
Dave Martin172a7972020-03-16 16:50:49 +0000611 arm64_skip_faulting_instruction(regs, 4);
Marc Zyngier32a3e632018-09-27 17:15:33 +0100612}
613
Mark Rutland37143dc2019-08-13 15:16:39 +0100614static const struct sys64_hook cp15_32_hooks[] = {
Marc Zyngier32a3e632018-09-27 17:15:33 +0100615 {
616 .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
617 .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
618 .handler = compat_cntfrq_read_handler,
619 },
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100620 {},
621};
622
Marc Zyngier50de0132018-09-27 17:15:32 +0100623static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
624{
625 int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
626 int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
Marc Zyngierdea86a82019-04-08 16:49:03 +0100627 u64 val = arch_timer_read_counter();
Marc Zyngier50de0132018-09-27 17:15:32 +0100628
629 pt_regs_write_reg(regs, rt, lower_32_bits(val));
630 pt_regs_write_reg(regs, rt2, upper_32_bits(val));
Dave Martin172a7972020-03-16 16:50:49 +0000631 arm64_skip_faulting_instruction(regs, 4);
Marc Zyngier50de0132018-09-27 17:15:32 +0100632}
633
Mark Rutland37143dc2019-08-13 15:16:39 +0100634static const struct sys64_hook cp15_64_hooks[] = {
Marc Zyngier50de0132018-09-27 17:15:32 +0100635 {
636 .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
637 .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
638 .handler = compat_cntvct_read_handler,
639 },
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100640 {},
641};
642
James Morseafa7c0e2019-10-25 17:42:15 +0100643void do_cp15instr(unsigned int esr, struct pt_regs *regs)
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100644{
Mark Rutland37143dc2019-08-13 15:16:39 +0100645 const struct sys64_hook *hook, *hook_base;
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100646
Marc Zyngier1f1c0142018-09-27 17:15:30 +0100647 if (!cp15_cond_valid(esr, regs)) {
648 /*
649 * There is no T16 variant of a CP access, so we
650 * always advance PC by 4 bytes.
651 */
Dave Martin172a7972020-03-16 16:50:49 +0000652 arm64_skip_faulting_instruction(regs, 4);
Marc Zyngier1f1c0142018-09-27 17:15:30 +0100653 return;
654 }
655
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100656 switch (ESR_ELx_EC(esr)) {
657 case ESR_ELx_EC_CP15_32:
658 hook_base = cp15_32_hooks;
659 break;
660 case ESR_ELx_EC_CP15_64:
661 hook_base = cp15_64_hooks;
662 break;
663 default:
664 do_undefinstr(regs);
665 return;
666 }
667
668 for (hook = hook_base; hook->handler; hook++)
669 if ((hook->esr_mask & esr) == hook->esr_val) {
670 hook->handler(esr, regs);
671 return;
672 }
673
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100674 /*
675 * New cp15 instructions may previously have been undefined at
676 * EL0. Fall back to our usual undefined instruction handler
677 * so that we handle these consistently.
678 */
679 do_undefinstr(regs);
680}
James Morseb6e43c02019-10-25 17:42:10 +0100681NOKPROBE_SYMBOL(do_cp15instr);
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100682#endif
683
James Morseafa7c0e2019-10-25 17:42:15 +0100684void do_sysinstr(unsigned int esr, struct pt_regs *regs)
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100685{
Mark Rutland37143dc2019-08-13 15:16:39 +0100686 const struct sys64_hook *hook;
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100687
688 for (hook = sys64_hooks; hook->handler; hook++)
689 if ((hook->esr_mask & esr) == hook->esr_val) {
690 hook->handler(esr, regs);
691 return;
692 }
693
Mark Rutland49f6cba2017-01-27 16:15:38 +0000694 /*
695 * New SYS instructions may previously have been undefined at EL0. Fall
696 * back to our usual undefined instruction handler so that we handle
697 * these consistently.
698 */
699 do_undefinstr(regs);
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100700}
James Morseb6e43c02019-10-25 17:42:10 +0100701NOKPROBE_SYMBOL(do_sysinstr);
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100702
Mark Rutland60a1f022014-11-18 12:16:30 +0000703static const char *esr_class_str[] = {
704 [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
705 [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
706 [ESR_ELx_EC_WFx] = "WFI/WFE",
707 [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
708 [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
709 [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
710 [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
711 [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
712 [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
Zenghui Yu6701c612019-07-13 04:40:54 +0000713 [ESR_ELx_EC_PAC] = "PAC",
Mark Rutland60a1f022014-11-18 12:16:30 +0000714 [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
Dave Martin8ef8f3602020-03-16 16:50:45 +0000715 [ESR_ELx_EC_BTI] = "BTI",
Mark Rutland60a1f022014-11-18 12:16:30 +0000716 [ESR_ELx_EC_ILL] = "PSTATE.IL",
717 [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
718 [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
719 [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
720 [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
721 [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
722 [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
723 [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
Dave Martin67236562017-10-31 15:51:00 +0000724 [ESR_ELx_EC_SVE] = "SVE",
Will Deacon332e5282019-07-16 08:14:19 +0100725 [ESR_ELx_EC_ERET] = "ERET/ERETAA/ERETAB",
Amit Daniel Kachhape16aeb02020-09-14 14:06:53 +0530726 [ESR_ELx_EC_FPAC] = "FPAC",
Mark Rutland60a1f022014-11-18 12:16:30 +0000727 [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
728 [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
729 [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
730 [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
731 [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
732 [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
733 [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
734 [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
735 [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
736 [ESR_ELx_EC_SERROR] = "SError",
737 [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
738 [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
739 [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
740 [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
741 [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
742 [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
743 [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
744 [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
745 [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
746};
747
748const char *esr_get_class_string(u32 esr)
749{
Mark Rutland275f3442016-05-31 12:33:01 +0100750 return esr_class_str[ESR_ELx_EC(esr)];
Mark Rutland60a1f022014-11-18 12:16:30 +0000751}
752
Catalin Marinas60ffc302012-03-05 11:49:27 +0000753/*
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000754 * bad_mode handles the impossible case in the exception vector. This is always
755 * fatal.
Catalin Marinas60ffc302012-03-05 11:49:27 +0000756 */
757asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
758{
759 console_verbose();
760
Mark Rutland8051f4d2016-05-31 12:07:47 +0100761 pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
762 handler[reason], smp_processor_id(), esr,
763 esr_get_class_string(esr));
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000764
Will Deacon413d3ea2020-06-15 12:23:16 +0100765 __show_regs(regs);
James Morse0fbeb312017-11-02 12:12:34 +0000766 local_daif_mask();
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000767 panic("bad mode");
768}
769
770/*
771 * bad_el0_sync handles unexpected, but potentially recoverable synchronous
772 * exceptions taken from EL0. Unlike bad_mode, this returns.
773 */
James Morseafa7c0e2019-10-25 17:42:15 +0100774void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000775{
Peter Collingbournedceec3f2020-11-20 12:33:46 -0800776 unsigned long pc = instruction_pointer(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000777
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000778 current->thread.fault_address = 0;
Will Deacon4e829b62018-02-20 15:18:13 +0000779 current->thread.fault_code = esr;
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000780
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200781 arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
782 "Bad EL0 synchronous exception");
Catalin Marinas60ffc302012-03-05 11:49:27 +0000783}
784
Mark Rutland872d8322017-07-14 20:30:35 +0100785#ifdef CONFIG_VMAP_STACK
786
787DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
788 __aligned(16);
789
790asmlinkage void handle_bad_stack(struct pt_regs *regs)
791{
792 unsigned long tsk_stk = (unsigned long)current->stack;
793 unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
794 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
795 unsigned int esr = read_sysreg(esr_el1);
796 unsigned long far = read_sysreg(far_el1);
797
798 console_verbose();
799 pr_emerg("Insufficient stack space to handle exception!");
800
801 pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
802 pr_emerg("FAR: 0x%016lx\n", far);
803
804 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
805 tsk_stk, tsk_stk + THREAD_SIZE);
806 pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
Maninder Singh338c11e2020-07-31 17:19:50 +0530807 irq_stk, irq_stk + IRQ_STACK_SIZE);
Mark Rutland872d8322017-07-14 20:30:35 +0100808 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
809 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
810
811 __show_regs(regs);
812
813 /*
814 * We use nmi_panic to limit the potential for recusive overflows, and
815 * to get a better stack trace.
816 */
817 nmi_panic(NULL, "kernel stack overflow");
818 cpu_park_loop();
819}
820#endif
821
James Morse6bf0dcf2018-01-15 19:38:57 +0000822void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
Xie XiuQia92d4d12017-11-02 12:12:42 +0000823{
Xie XiuQia92d4d12017-11-02 12:12:42 +0000824 console_verbose();
825
826 pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
827 smp_processor_id(), esr, esr_get_class_string(esr));
James Morse6bf0dcf2018-01-15 19:38:57 +0000828 if (regs)
829 __show_regs(regs);
Xie XiuQia92d4d12017-11-02 12:12:42 +0000830
James Morse6bf0dcf2018-01-15 19:38:57 +0000831 nmi_panic(regs, "Asynchronous SError Interrupt");
832
833 cpu_park_loop();
834 unreachable();
835}
836
837bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
838{
839 u32 aet = arm64_ras_serror_get_severity(esr);
840
841 switch (aet) {
842 case ESR_ELx_AET_CE: /* corrected error */
843 case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
844 /*
845 * The CPU can make progress. We may take UEO again as
846 * a more severe error.
847 */
848 return false;
849
850 case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
851 case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
852 /*
853 * The CPU can't make progress. The exception may have
854 * been imprecise.
James Morse3276cc22019-06-18 16:17:38 +0100855 *
856 * Neoverse-N1 #1349291 means a non-KVM SError reported as
857 * Unrecoverable should be treated as Uncontainable. We
858 * call arm64_serror_panic() in both cases.
James Morse6bf0dcf2018-01-15 19:38:57 +0000859 */
860 return true;
861
862 case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
863 default:
864 /* Error has been silently propagated */
865 arm64_serror_panic(regs, esr);
866 }
867}
868
869asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
870{
Peter Zijlstra69ea03b2020-02-19 09:46:47 +0100871 nmi_enter();
James Morse6bf0dcf2018-01-15 19:38:57 +0000872
873 /* non-RAS errors are not containable */
874 if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
875 arm64_serror_panic(regs, esr);
876
Peter Zijlstra69ea03b2020-02-19 09:46:47 +0100877 nmi_exit();
Xie XiuQia92d4d12017-11-02 12:12:42 +0000878}
879
James Morse26718282019-08-20 18:45:57 +0100880asmlinkage void enter_from_user_mode(void)
881{
882 CT_WARN_ON(ct_state() != CONTEXT_USER);
883 user_exit_irqoff();
884}
885NOKPROBE_SYMBOL(enter_from_user_mode);
886
Dave P Martin9fb74102015-07-24 16:37:48 +0100887/* GENERIC_BUG traps */
888
889int is_valid_bugaddr(unsigned long addr)
890{
891 /*
892 * bug_handler() only called for BRK #BUG_BRK_IMM.
893 * So the answer is trivial -- any spurious instances with no
894 * bug table entry will be rejected by report_bug() and passed
895 * back to the debug-monitors code and handled as a fatal
896 * unexpected debug exception.
897 */
898 return 1;
899}
900
901static int bug_handler(struct pt_regs *regs, unsigned int esr)
902{
Dave P Martin9fb74102015-07-24 16:37:48 +0100903 switch (report_bug(regs->pc, regs)) {
904 case BUG_TRAP_TYPE_BUG:
905 die("Oops - BUG", regs, 0);
906 break;
907
908 case BUG_TRAP_TYPE_WARN:
909 break;
910
911 default:
912 /* unknown/unrecognised bug trap type */
913 return DBG_HOOK_ERROR;
914 }
915
916 /* If thread survives, skip over the BUG instruction and continue: */
Julien Thierry6436bee2017-10-25 10:04:33 +0100917 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Dave P Martin9fb74102015-07-24 16:37:48 +0100918 return DBG_HOOK_HANDLED;
919}
920
921static struct break_hook bug_break_hook = {
Dave P Martin9fb74102015-07-24 16:37:48 +0100922 .fn = bug_handler,
Will Deacon26a04d82019-02-26 12:52:47 +0000923 .imm = BUG_BRK_IMM,
Dave P Martin9fb74102015-07-24 16:37:48 +0100924};
925
Will Deacon0fdb64c2020-09-15 15:48:09 +0100926static int reserved_fault_handler(struct pt_regs *regs, unsigned int esr)
927{
928 pr_err("%s generated an invalid instruction at %pS!\n",
929 in_bpf_jit(regs) ? "BPF JIT" : "Kernel text patching",
930 (void *)instruction_pointer(regs));
931
932 /* We cannot handle this */
933 return DBG_HOOK_ERROR;
934}
935
936static struct break_hook fault_break_hook = {
937 .fn = reserved_fault_handler,
938 .imm = FAULT_BRK_IMM,
939};
940
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800941#ifdef CONFIG_KASAN_SW_TAGS
942
943#define KASAN_ESR_RECOVER 0x20
944#define KASAN_ESR_WRITE 0x10
945#define KASAN_ESR_SIZE_MASK 0x0f
946#define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK))
947
948static int kasan_handler(struct pt_regs *regs, unsigned int esr)
949{
950 bool recover = esr & KASAN_ESR_RECOVER;
951 bool write = esr & KASAN_ESR_WRITE;
952 size_t size = KASAN_ESR_SIZE(esr);
953 u64 addr = regs->regs[0];
954 u64 pc = regs->pc;
955
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800956 kasan_report(addr, size, write, pc);
957
958 /*
959 * The instrumentation allows to control whether we can proceed after
960 * a crash was detected. This is done by passing the -recover flag to
961 * the compiler. Disabling recovery allows to generate more compact
962 * code.
963 *
964 * Unfortunately disabling recovery doesn't work for the kernel right
965 * now. KASAN reporting is disabled in some contexts (for example when
966 * the allocator accesses slab object metadata; this is controlled by
967 * current->kasan_depth). All these accesses are detected by the tool,
968 * even though the reports for them are not printed.
969 *
970 * This is something that might be fixed at some point in the future.
971 */
972 if (!recover)
973 die("Oops - KASAN", regs, 0);
974
975 /* If thread survives, skip over the brk instruction and continue: */
976 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
977 return DBG_HOOK_HANDLED;
978}
979
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800980static struct break_hook kasan_break_hook = {
Will Deacon26a04d82019-02-26 12:52:47 +0000981 .fn = kasan_handler,
982 .imm = KASAN_BRK_IMM,
983 .mask = KASAN_BRK_MASK,
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800984};
985#endif
986
Dave P Martin9fb74102015-07-24 16:37:48 +0100987/*
988 * Initial handler for AArch64 BRK exceptions
989 * This handler only used until debug_traps_init().
990 */
991int __init early_brk64(unsigned long addr, unsigned int esr,
992 struct pt_regs *regs)
993{
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800994#ifdef CONFIG_KASAN_SW_TAGS
Will Deacon453b7742019-02-26 15:06:42 +0000995 unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
Will Deacon26a04d82019-02-26 12:52:47 +0000996
997 if ((comment & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800998 return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
999#endif
Dave P Martin9fb74102015-07-24 16:37:48 +01001000 return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
1001}
1002
Catalin Marinas60ffc302012-03-05 11:49:27 +00001003void __init trap_init(void)
1004{
Will Deacon26a04d82019-02-26 12:52:47 +00001005 register_kernel_break_hook(&bug_break_hook);
Will Deacon0fdb64c2020-09-15 15:48:09 +01001006 register_kernel_break_hook(&fault_break_hook);
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001007#ifdef CONFIG_KASAN_SW_TAGS
Will Deacon26a04d82019-02-26 12:52:47 +00001008 register_kernel_break_hook(&kasan_break_hook);
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001009#endif
Douglas Andersonb322c652020-05-13 16:06:37 -07001010 debug_traps_init();
Catalin Marinas60ffc302012-03-05 11:49:27 +00001011}