blob: b8c714dda85107d297824b9ba977f9f23b08a98b [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>
Dave P Martin9fb74102015-07-24 16:37:48 +010037#include <asm/insn.h>
James Morseb6e43c02019-10-25 17:42:10 +010038#include <asm/kprobes.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000039#include <asm/traps.h>
Mark Rutland872d8322017-07-14 20:30:35 +010040#include <asm/smp.h>
Mark Rutlanda9ea0012016-11-03 20:23:05 +000041#include <asm/stack_pointer.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000042#include <asm/stacktrace.h>
43#include <asm/exception.h>
44#include <asm/system_misc.h>
Andre Przywara7dd01ae2016-06-28 18:07:32 +010045#include <asm/sysreg.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000046
47static const char *handler[]= {
48 "Synchronous Abort",
49 "IRQ",
50 "FIQ",
51 "Error"
52};
53
Michael Weiser5ee39a72018-02-01 23:13:38 +010054int show_unhandled_signals = 0;
Catalin Marinas60ffc302012-03-05 11:49:27 +000055
Jungseok Lee9f93f3e2015-10-17 14:28:11 +000056static void dump_backtrace_entry(unsigned long where)
Catalin Marinas60ffc302012-03-05 11:49:27 +000057{
Will Deacona25ffd32017-10-19 13:19:20 +010058 printk(" %pS\n", (void *)where);
Catalin Marinas60ffc302012-03-05 11:49:27 +000059}
60
jinho lim7b716652019-06-26 20:50:13 +090061static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
Catalin Marinas60ffc302012-03-05 11:49:27 +000062{
63 unsigned long addr = instruction_pointer(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +000064 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
65 int i;
66
jinho lim7b716652019-06-26 20:50:13 +090067 if (user_mode(regs))
68 return;
69
Catalin Marinas60ffc302012-03-05 11:49:27 +000070 for (i = -4; i < 1; i++) {
71 unsigned int val, bad;
72
jinho lim7b716652019-06-26 20:50:13 +090073 bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
Catalin Marinas60ffc302012-03-05 11:49:27 +000074
75 if (!bad)
76 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
77 else {
78 p += sprintf(p, "bad PC value");
79 break;
80 }
81 }
Catalin Marinas60ffc302012-03-05 11:49:27 +000082
jinho lim7b716652019-06-26 20:50:13 +090083 printk("%sCode: %s\n", lvl, str);
Catalin Marinas60ffc302012-03-05 11:49:27 +000084}
85
Kefeng Wang1149aad2017-05-09 09:53:37 +080086void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
Catalin Marinas60ffc302012-03-05 11:49:27 +000087{
88 struct stackframe frame;
Will Deacon1e6f54402019-04-08 17:56:34 +010089 int skip = 0;
Catalin Marinas60ffc302012-03-05 11:49:27 +000090
Mark Rutlandb5e73072016-09-23 17:55:05 +010091 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
92
Will Deacon1e6f54402019-04-08 17:56:34 +010093 if (regs) {
94 if (user_mode(regs))
95 return;
96 skip = 1;
97 }
98
Mark Rutlandb5e73072016-09-23 17:55:05 +010099 if (!tsk)
100 tsk = current;
101
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000102 if (!try_get_task_stack(tsk))
103 return;
104
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900105 if (tsk == current) {
Dave Martinf3dcbe62019-07-02 14:07:28 +0100106 start_backtrace(&frame,
107 (unsigned long)__builtin_frame_address(0),
108 (unsigned long)dump_backtrace);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000109 } else {
110 /*
111 * task blocked in __switch_to
112 */
Dave Martinf3dcbe62019-07-02 14:07:28 +0100113 start_backtrace(&frame,
114 thread_saved_fp(tsk),
115 thread_saved_pc(tsk));
Catalin Marinas60ffc302012-03-05 11:49:27 +0000116 }
117
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000118 printk("Call trace:\n");
Will Deacona25ffd32017-10-19 13:19:20 +0100119 do {
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900120 /* skip until specified stack frame */
121 if (!skip) {
Ard Biesheuvel73267492017-07-22 18:45:33 +0100122 dump_backtrace_entry(frame.pc);
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900123 } else if (frame.fp == regs->regs[29]) {
124 skip = 0;
125 /*
126 * Mostly, this is the case where this function is
127 * called in panic/abort. As exception handler's
128 * stack frame does not contain the corresponding pc
129 * at which an exception has taken place, use regs->pc
130 * instead.
131 */
132 dump_backtrace_entry(regs->pc);
133 }
Will Deacona25ffd32017-10-19 13:19:20 +0100134 } while (!unwind_frame(tsk, &frame));
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000135
136 put_task_stack(tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000137}
138
Catalin Marinas60ffc302012-03-05 11:49:27 +0000139void show_stack(struct task_struct *tsk, unsigned long *sp)
140{
141 dump_backtrace(NULL, tsk);
142 barrier();
143}
144
145#ifdef CONFIG_PREEMPT
146#define S_PREEMPT " PREEMPT"
Thomas Gleixner7ef858d2019-10-15 21:17:49 +0200147#elif defined(CONFIG_PREEMPT_RT)
148#define S_PREEMPT " PREEMPT_RT"
Catalin Marinas60ffc302012-03-05 11:49:27 +0000149#else
150#define S_PREEMPT ""
151#endif
Thomas Gleixner7ef858d2019-10-15 21:17:49 +0200152
Catalin Marinas60ffc302012-03-05 11:49:27 +0000153#define S_SMP " SMP"
Catalin Marinas60ffc302012-03-05 11:49:27 +0000154
Mark Rutland876e7a32016-11-03 20:23:06 +0000155static int __die(const char *str, int err, struct pt_regs *regs)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000156{
Catalin Marinas60ffc302012-03-05 11:49:27 +0000157 static int die_counter;
158 int ret;
159
160 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
161 str, err, ++die_counter);
162
163 /* trap and error numbers are mostly meaningless on ARM */
164 ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
165 if (ret == NOTIFY_STOP)
166 return ret;
167
168 print_modules();
Will Deacon1e6f54402019-04-08 17:56:34 +0100169 show_regs(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000170
jinho lim7b716652019-06-26 20:50:13 +0900171 dump_kernel_instr(KERN_EMERG, regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000172
173 return ret;
174}
175
176static DEFINE_RAW_SPINLOCK(die_lock);
177
178/*
179 * This function is protected against re-entrancy.
180 */
181void die(const char *str, struct pt_regs *regs, int err)
182{
Catalin Marinas60ffc302012-03-05 11:49:27 +0000183 int ret;
Qiao Zhou6f44a0b2017-07-07 17:29:34 +0800184 unsigned long flags;
185
186 raw_spin_lock_irqsave(&die_lock, flags);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000187
188 oops_enter();
189
Catalin Marinas60ffc302012-03-05 11:49:27 +0000190 console_verbose();
191 bust_spinlocks(1);
Mark Rutland876e7a32016-11-03 20:23:06 +0000192 ret = __die(str, err, regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000193
Mark Rutland876e7a32016-11-03 20:23:06 +0000194 if (regs && kexec_should_crash(current))
Catalin Marinas60ffc302012-03-05 11:49:27 +0000195 crash_kexec(regs);
196
197 bust_spinlocks(0);
Rusty Russell373d4d02013-01-21 17:17:39 +1030198 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000199 oops_exit();
200
201 if (in_interrupt())
202 panic("Fatal exception in interrupt");
203 if (panic_on_oops)
204 panic("Fatal exception");
Qiao Zhou6f44a0b2017-07-07 17:29:34 +0800205
206 raw_spin_unlock_irqrestore(&die_lock, flags);
207
Catalin Marinas60ffc302012-03-05 11:49:27 +0000208 if (ret != NOTIFY_STOP)
209 do_exit(SIGSEGV);
210}
211
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200212static void arm64_show_signal(int signo, const char *str)
Will Deacona26731d2018-02-20 15:08:51 +0000213{
214 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
215 DEFAULT_RATELIMIT_BURST);
Eric W. Biederman24b8f792018-09-22 00:38:41 +0200216 struct task_struct *tsk = current;
Will Deacona1ece822018-02-20 13:46:05 +0000217 unsigned int esr = tsk->thread.fault_code;
218 struct pt_regs *regs = task_pt_regs(tsk);
219
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200220 /* Leave if the signal won't be shown */
221 if (!show_unhandled_signals ||
222 !unhandled_signal(tsk, signo) ||
223 !__ratelimit(&rs))
224 return;
Will Deacona1ece822018-02-20 13:46:05 +0000225
226 pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
227 if (esr)
228 pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);
229
230 pr_cont("%s", str);
231 print_vma_addr(KERN_CONT " in ", regs->pc);
232 pr_cont("\n");
233 __show_regs(regs);
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200234}
Will Deacona1ece822018-02-20 13:46:05 +0000235
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200236void arm64_force_sig_fault(int signo, int code, void __user *addr,
237 const char *str)
238{
239 arm64_show_signal(signo, str);
Eric W. Biedermand76cac62019-05-23 11:11:19 -0500240 if (signo == SIGKILL)
Eric W. Biederman3cf5d072019-05-23 10:17:27 -0500241 force_sig(SIGKILL);
Eric W. Biedermand76cac62019-05-23 11:11:19 -0500242 else
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500243 force_sig_fault(signo, code, addr);
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200244}
245
Eric W. Biedermanb4d55572018-09-22 10:37:15 +0200246void arm64_force_sig_mceerr(int code, void __user *addr, short lsb,
247 const char *str)
248{
249 arm64_show_signal(SIGBUS, str);
Eric W. Biedermanf8eac902019-02-05 18:14:19 -0600250 force_sig_mceerr(code, addr, lsb);
Eric W. Biedermanb4d55572018-09-22 10:37:15 +0200251}
252
Eric W. Biedermanf3a900b2018-09-22 10:52:41 +0200253void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr,
254 const char *str)
255{
256 arm64_show_signal(SIGTRAP, str);
257 force_sig_ptrace_errno_trap(errno, addr);
Will Deacona1ece822018-02-20 13:46:05 +0000258}
259
Catalin Marinas60ffc302012-03-05 11:49:27 +0000260void arm64_notify_die(const char *str, struct pt_regs *regs,
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200261 int signo, int sicode, void __user *addr,
262 int err)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000263{
Catalin Marinas91413002014-04-06 23:04:12 +0100264 if (user_mode(regs)) {
Will Deacona1ece822018-02-20 13:46:05 +0000265 WARN_ON(regs != current_pt_regs());
Catalin Marinas91413002014-04-06 23:04:12 +0100266 current->thread.fault_address = 0;
267 current->thread.fault_code = err;
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200268
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200269 arm64_force_sig_fault(signo, sicode, addr, str);
Catalin Marinas91413002014-04-06 23:04:12 +0100270 } else {
Catalin Marinas60ffc302012-03-05 11:49:27 +0000271 die(str, regs, err);
Catalin Marinas91413002014-04-06 23:04:12 +0100272 }
Catalin Marinas60ffc302012-03-05 11:49:27 +0000273}
274
Julien Thierry6436bee2017-10-25 10:04:33 +0100275void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
276{
277 regs->pc += size;
278
279 /*
280 * If we were single stepping, we want to get the step exception after
281 * we return from the trap.
282 */
Mark Rutland9478f192018-04-03 11:22:51 +0100283 if (user_mode(regs))
284 user_fastforward_single_step(current);
Julien Thierry6436bee2017-10-25 10:04:33 +0100285}
286
Punit Agrawal9b79f522014-11-18 11:41:22 +0000287static LIST_HEAD(undef_hook);
288static DEFINE_RAW_SPINLOCK(undef_lock);
289
290void register_undef_hook(struct undef_hook *hook)
291{
292 unsigned long flags;
293
294 raw_spin_lock_irqsave(&undef_lock, flags);
295 list_add(&hook->node, &undef_hook);
296 raw_spin_unlock_irqrestore(&undef_lock, flags);
297}
298
299void unregister_undef_hook(struct undef_hook *hook)
300{
301 unsigned long flags;
302
303 raw_spin_lock_irqsave(&undef_lock, flags);
304 list_del(&hook->node);
305 raw_spin_unlock_irqrestore(&undef_lock, flags);
306}
307
308static int call_undef_hook(struct pt_regs *regs)
309{
310 struct undef_hook *hook;
311 unsigned long flags;
312 u32 instr;
313 int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
314 void __user *pc = (void __user *)instruction_pointer(regs);
315
Will Deacon0bf0f442018-08-07 13:43:06 +0100316 if (!user_mode(regs)) {
317 __le32 instr_le;
318 if (probe_kernel_address((__force __le32 *)pc, instr_le))
319 goto exit;
320 instr = le32_to_cpu(instr_le);
321 } else if (compat_thumb_mode(regs)) {
Punit Agrawal9b79f522014-11-18 11:41:22 +0000322 /* 16-bit Thumb instruction */
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200323 __le16 instr_le;
324 if (get_user(instr_le, (__le16 __user *)pc))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000325 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200326 instr = le16_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000327 if (aarch32_insn_is_wide(instr)) {
328 u32 instr2;
329
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200330 if (get_user(instr_le, (__le16 __user *)(pc + 2)))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000331 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200332 instr2 = le16_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000333 instr = (instr << 16) | instr2;
334 }
335 } else {
336 /* 32-bit ARM instruction */
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200337 __le32 instr_le;
338 if (get_user(instr_le, (__le32 __user *)pc))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000339 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200340 instr = le32_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000341 }
342
343 raw_spin_lock_irqsave(&undef_lock, flags);
344 list_for_each_entry(hook, &undef_hook, node)
345 if ((instr & hook->instr_mask) == hook->instr_val &&
346 (regs->pstate & hook->pstate_mask) == hook->pstate_val)
347 fn = hook->fn;
348
349 raw_spin_unlock_irqrestore(&undef_lock, flags);
350exit:
351 return fn ? fn(regs, instr) : 1;
352}
353
Will Deacon2c9120f32018-02-20 14:16:29 +0000354void force_signal_inject(int signal, int code, unsigned long address)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000355{
Andre Przywara390bf172016-06-28 18:07:31 +0100356 const char *desc;
Will Deacon2c9120f32018-02-20 14:16:29 +0000357 struct pt_regs *regs = current_pt_regs();
358
Will Deacon8a604192018-08-14 16:24:54 +0100359 if (WARN_ON(!user_mode(regs)))
360 return;
361
Andre Przywara390bf172016-06-28 18:07:31 +0100362 switch (signal) {
363 case SIGILL:
364 desc = "undefined instruction";
365 break;
366 case SIGSEGV:
367 desc = "illegal memory access";
368 break;
369 default:
Dave Martinbc0ee472017-10-31 15:51:05 +0000370 desc = "unknown or unrecoverable error";
Andre Przywara390bf172016-06-28 18:07:31 +0100371 break;
372 }
373
Will Deacona7e6f1c2018-02-20 18:08:40 +0000374 /* Force signals we don't understand to SIGKILL */
Mark Rutlandb2d71b32018-04-16 16:45:01 +0100375 if (WARN_ON(signal != SIGKILL &&
Will Deacona7e6f1c2018-02-20 18:08:40 +0000376 siginfo_layout(signal, code) != SIL_FAULT)) {
377 signal = SIGKILL;
378 }
379
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200380 arm64_notify_die(desc, regs, signal, code, (void __user *)address, 0);
Andre Przywara390bf172016-06-28 18:07:31 +0100381}
382
383/*
384 * Set up process info to signal segmentation fault - called on access error.
385 */
Will Deacon2c9120f32018-02-20 14:16:29 +0000386void arm64_notify_segfault(unsigned long addr)
Andre Przywara390bf172016-06-28 18:07:31 +0100387{
388 int code;
389
390 down_read(&current->mm->mmap_sem);
391 if (find_vma(current->mm, addr) == NULL)
392 code = SEGV_MAPERR;
393 else
394 code = SEGV_ACCERR;
395 up_read(&current->mm->mmap_sem);
396
Will Deacon2c9120f32018-02-20 14:16:29 +0000397 force_signal_inject(SIGSEGV, code, addr);
Andre Przywara390bf172016-06-28 18:07:31 +0100398}
399
James Morseafa7c0e2019-10-25 17:42:15 +0100400void do_undefinstr(struct pt_regs *regs)
Andre Przywara390bf172016-06-28 18:07:31 +0100401{
Catalin Marinas60ffc302012-03-05 11:49:27 +0000402 /* check for AArch32 breakpoint instructions */
Will Deacon1442b6e2013-03-16 08:48:13 +0000403 if (!aarch32_break_handler(regs))
Catalin Marinas60ffc302012-03-05 11:49:27 +0000404 return;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000405
Punit Agrawal9b79f522014-11-18 11:41:22 +0000406 if (call_undef_hook(regs) == 0)
407 return;
408
Will Deacon0bf0f442018-08-07 13:43:06 +0100409 BUG_ON(!user_mode(regs));
Will Deacon8a604192018-08-14 16:24:54 +0100410 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000411}
James Morseb6e43c02019-10-25 17:42:10 +0100412NOKPROBE_SYMBOL(do_undefinstr);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000413
Dave Martin8ef8f3602020-03-16 16:50:45 +0000414void do_bti(struct pt_regs *regs)
415{
416 BUG_ON(!user_mode(regs));
417 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
418}
419NOKPROBE_SYMBOL(do_bti);
420
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100421#define __user_cache_maint(insn, address, res) \
Kristina Martsenko81cddd62017-05-03 16:37:45 +0100422 if (address >= user_addr_max()) { \
Andre Przywara87261d12016-10-19 14:40:54 +0100423 res = -EFAULT; \
Catalin Marinas39bc88e2016-09-02 14:54:03 +0100424 } else { \
425 uaccess_ttbr0_enable(); \
Andre Przywara87261d12016-10-19 14:40:54 +0100426 asm volatile ( \
427 "1: " insn ", %1\n" \
428 " mov %w0, #0\n" \
429 "2:\n" \
430 " .pushsection .fixup,\"ax\"\n" \
431 " .align 2\n" \
432 "3: mov %w0, %w2\n" \
433 " b 2b\n" \
434 " .popsection\n" \
435 _ASM_EXTABLE(1b, 3b) \
436 : "=r" (res) \
Catalin Marinas39bc88e2016-09-02 14:54:03 +0100437 : "r" (address), "i" (-EFAULT)); \
438 uaccess_ttbr0_disable(); \
439 }
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100440
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100441static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100442{
443 unsigned long address;
Anshuman Khandual1c839142018-09-20 09:36:19 +0530444 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100445 int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
446 int ret = 0;
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100447
Kristina Martsenko81cddd62017-05-03 16:37:45 +0100448 address = untagged_addr(pt_regs_read_reg(regs, rt));
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100449
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100450 switch (crm) {
451 case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
452 __user_cache_maint("dc civac", address, ret);
453 break;
454 case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
455 __user_cache_maint("dc civac", address, ret);
456 break;
Andrew Murrayd16ed4102019-04-09 10:52:42 +0100457 case ESR_ELx_SYS64_ISS_CRM_DC_CVADP: /* DC CVADP */
458 __user_cache_maint("sys 3, c7, c13, 1", address, ret);
459 break;
Robin Murphye1bc5d12017-07-25 11:55:41 +0100460 case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
461 __user_cache_maint("sys 3, c7, c12, 1", address, ret);
462 break;
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100463 case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
464 __user_cache_maint("dc civac", address, ret);
465 break;
466 case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
467 __user_cache_maint("ic ivau", address, ret);
468 break;
469 default:
Will Deacon2c9120f32018-02-20 14:16:29 +0000470 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100471 return;
472 }
473
474 if (ret)
Will Deacon2c9120f32018-02-20 14:16:29 +0000475 arm64_notify_segfault(address);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100476 else
Julien Thierry6436bee2017-10-25 10:04:33 +0100477 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100478}
479
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100480static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
481{
Anshuman Khandual1c839142018-09-20 09:36:19 +0530482 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Mark Rutland8b6e70f2017-02-09 15:19:19 +0000483 unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100484
James Morseee9d90b2019-10-17 18:42:59 +0100485 if (cpus_have_const_cap(ARM64_WORKAROUND_1542419)) {
486 /* Hide DIC so that we can trap the unnecessary maintenance...*/
James Morse05460842019-10-17 18:42:58 +0100487 val &= ~BIT(CTR_DIC_SHIFT);
488
James Morseee9d90b2019-10-17 18:42:59 +0100489 /* ... and fake IminLine to reduce the number of traps. */
490 val &= ~CTR_IMINLINE_MASK;
491 val |= (PAGE_SHIFT - 2) & CTR_IMINLINE_MASK;
492 }
493
Mark Rutland8b6e70f2017-02-09 15:19:19 +0000494 pt_regs_write_reg(regs, rt, val);
495
Julien Thierry6436bee2017-10-25 10:04:33 +0100496 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100497}
498
Marc Zyngier6126ce02017-02-01 11:48:58 +0000499static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
500{
Anshuman Khandual1c839142018-09-20 09:36:19 +0530501 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Marc Zyngier6126ce02017-02-01 11:48:58 +0000502
Marc Zyngierdea86a82019-04-08 16:49:03 +0100503 pt_regs_write_reg(regs, rt, arch_timer_read_counter());
Julien Thierry6436bee2017-10-25 10:04:33 +0100504 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Marc Zyngier6126ce02017-02-01 11:48:58 +0000505}
506
Marc Zyngier98421192017-04-24 09:04:03 +0100507static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
508{
Anshuman Khandual1c839142018-09-20 09:36:19 +0530509 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Marc Zyngier98421192017-04-24 09:04:03 +0100510
Marc Zyngierc6f97ad2017-07-21 18:15:27 +0100511 pt_regs_write_reg(regs, rt, arch_timer_get_rate());
Julien Thierry6436bee2017-10-25 10:04:33 +0100512 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Marc Zyngier98421192017-04-24 09:04:03 +0100513}
514
Anshuman Khandual21f84792018-09-20 09:36:21 +0530515static void mrs_handler(unsigned int esr, struct pt_regs *regs)
516{
517 u32 sysreg, rt;
518
519 rt = ESR_ELx_SYS64_ISS_RT(esr);
520 sysreg = esr_sys64_to_sysreg(esr);
521
522 if (do_emulate_mrs(regs, sysreg, rt) != 0)
523 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
524}
525
Marc Zyngierc219bc42018-10-01 12:19:43 +0100526static void wfi_handler(unsigned int esr, struct pt_regs *regs)
527{
528 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
529}
530
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100531struct sys64_hook {
532 unsigned int esr_mask;
533 unsigned int esr_val;
534 void (*handler)(unsigned int esr, struct pt_regs *regs);
535};
536
Mark Rutland37143dc2019-08-13 15:16:39 +0100537static const struct sys64_hook sys64_hooks[] = {
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100538 {
539 .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
540 .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
541 .handler = user_cache_maint_handler,
542 },
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100543 {
544 /* Trap read access to CTR_EL0 */
545 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
546 .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
547 .handler = ctr_read_handler,
548 },
Marc Zyngier6126ce02017-02-01 11:48:58 +0000549 {
550 /* Trap read access to CNTVCT_EL0 */
551 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
552 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
553 .handler = cntvct_read_handler,
554 },
Marc Zyngier98421192017-04-24 09:04:03 +0100555 {
556 /* Trap read access to CNTFRQ_EL0 */
557 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
558 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
559 .handler = cntfrq_read_handler,
560 },
Anshuman Khandual21f84792018-09-20 09:36:21 +0530561 {
562 /* Trap read access to CPUID registers */
563 .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
564 .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
565 .handler = mrs_handler,
566 },
Marc Zyngierc219bc42018-10-01 12:19:43 +0100567 {
568 /* Trap WFI instructions executed in userspace */
569 .esr_mask = ESR_ELx_WFx_MASK,
570 .esr_val = ESR_ELx_WFx_WFI_VAL,
571 .handler = wfi_handler,
572 },
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100573 {},
574};
575
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100576
577#ifdef CONFIG_COMPAT
Marc Zyngier1f1c0142018-09-27 17:15:30 +0100578#define PSTATE_IT_1_0_SHIFT 25
579#define PSTATE_IT_1_0_MASK (0x3 << PSTATE_IT_1_0_SHIFT)
580#define PSTATE_IT_7_2_SHIFT 10
581#define PSTATE_IT_7_2_MASK (0x3f << PSTATE_IT_7_2_SHIFT)
582
583static u32 compat_get_it_state(struct pt_regs *regs)
584{
585 u32 it, pstate = regs->pstate;
586
587 it = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
588 it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
589
590 return it;
591}
592
593static void compat_set_it_state(struct pt_regs *regs, u32 it)
594{
595 u32 pstate_it;
596
597 pstate_it = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
598 pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
599
600 regs->pstate &= ~PSR_AA32_IT_MASK;
601 regs->pstate |= pstate_it;
602}
603
604static bool cp15_cond_valid(unsigned int esr, struct pt_regs *regs)
605{
606 int cond;
607
608 /* Only a T32 instruction can trap without CV being set */
609 if (!(esr & ESR_ELx_CV)) {
610 u32 it;
611
612 it = compat_get_it_state(regs);
613 if (!it)
614 return true;
615
616 cond = it >> 4;
617 } else {
618 cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
619 }
620
621 return aarch32_opcode_cond_checks[cond](regs->pstate);
622}
623
624static void advance_itstate(struct pt_regs *regs)
625{
626 u32 it;
627
628 /* ARM mode */
629 if (!(regs->pstate & PSR_AA32_T_BIT) ||
630 !(regs->pstate & PSR_AA32_IT_MASK))
631 return;
632
633 it = compat_get_it_state(regs);
634
635 /*
636 * If this is the last instruction of the block, wipe the IT
637 * state. Otherwise advance it.
638 */
639 if (!(it & 7))
640 it = 0;
641 else
642 it = (it & 0xe0) | ((it << 1) & 0x1f);
643
644 compat_set_it_state(regs, it);
645}
646
647static void arm64_compat_skip_faulting_instruction(struct pt_regs *regs,
648 unsigned int sz)
649{
650 advance_itstate(regs);
651 arm64_skip_faulting_instruction(regs, sz);
652}
653
Marc Zyngier32a3e632018-09-27 17:15:33 +0100654static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
655{
656 int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
657
658 pt_regs_write_reg(regs, reg, arch_timer_get_rate());
659 arm64_compat_skip_faulting_instruction(regs, 4);
660}
661
Mark Rutland37143dc2019-08-13 15:16:39 +0100662static const struct sys64_hook cp15_32_hooks[] = {
Marc Zyngier32a3e632018-09-27 17:15:33 +0100663 {
664 .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
665 .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
666 .handler = compat_cntfrq_read_handler,
667 },
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100668 {},
669};
670
Marc Zyngier50de0132018-09-27 17:15:32 +0100671static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
672{
673 int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
674 int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
Marc Zyngierdea86a82019-04-08 16:49:03 +0100675 u64 val = arch_timer_read_counter();
Marc Zyngier50de0132018-09-27 17:15:32 +0100676
677 pt_regs_write_reg(regs, rt, lower_32_bits(val));
678 pt_regs_write_reg(regs, rt2, upper_32_bits(val));
679 arm64_compat_skip_faulting_instruction(regs, 4);
680}
681
Mark Rutland37143dc2019-08-13 15:16:39 +0100682static const struct sys64_hook cp15_64_hooks[] = {
Marc Zyngier50de0132018-09-27 17:15:32 +0100683 {
684 .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
685 .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
686 .handler = compat_cntvct_read_handler,
687 },
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100688 {},
689};
690
James Morseafa7c0e2019-10-25 17:42:15 +0100691void do_cp15instr(unsigned int esr, struct pt_regs *regs)
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100692{
Mark Rutland37143dc2019-08-13 15:16:39 +0100693 const struct sys64_hook *hook, *hook_base;
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100694
Marc Zyngier1f1c0142018-09-27 17:15:30 +0100695 if (!cp15_cond_valid(esr, regs)) {
696 /*
697 * There is no T16 variant of a CP access, so we
698 * always advance PC by 4 bytes.
699 */
700 arm64_compat_skip_faulting_instruction(regs, 4);
701 return;
702 }
703
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100704 switch (ESR_ELx_EC(esr)) {
705 case ESR_ELx_EC_CP15_32:
706 hook_base = cp15_32_hooks;
707 break;
708 case ESR_ELx_EC_CP15_64:
709 hook_base = cp15_64_hooks;
710 break;
711 default:
712 do_undefinstr(regs);
713 return;
714 }
715
716 for (hook = hook_base; hook->handler; hook++)
717 if ((hook->esr_mask & esr) == hook->esr_val) {
718 hook->handler(esr, regs);
719 return;
720 }
721
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100722 /*
723 * New cp15 instructions may previously have been undefined at
724 * EL0. Fall back to our usual undefined instruction handler
725 * so that we handle these consistently.
726 */
727 do_undefinstr(regs);
728}
James Morseb6e43c02019-10-25 17:42:10 +0100729NOKPROBE_SYMBOL(do_cp15instr);
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100730#endif
731
James Morseafa7c0e2019-10-25 17:42:15 +0100732void do_sysinstr(unsigned int esr, struct pt_regs *regs)
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100733{
Mark Rutland37143dc2019-08-13 15:16:39 +0100734 const struct sys64_hook *hook;
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100735
736 for (hook = sys64_hooks; hook->handler; hook++)
737 if ((hook->esr_mask & esr) == hook->esr_val) {
738 hook->handler(esr, regs);
739 return;
740 }
741
Mark Rutland49f6cba2017-01-27 16:15:38 +0000742 /*
743 * New SYS instructions may previously have been undefined at EL0. Fall
744 * back to our usual undefined instruction handler so that we handle
745 * these consistently.
746 */
747 do_undefinstr(regs);
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100748}
James Morseb6e43c02019-10-25 17:42:10 +0100749NOKPROBE_SYMBOL(do_sysinstr);
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100750
Mark Rutland60a1f022014-11-18 12:16:30 +0000751static const char *esr_class_str[] = {
752 [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
753 [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
754 [ESR_ELx_EC_WFx] = "WFI/WFE",
755 [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
756 [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
757 [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
758 [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
759 [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
760 [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
Zenghui Yu6701c612019-07-13 04:40:54 +0000761 [ESR_ELx_EC_PAC] = "PAC",
Mark Rutland60a1f022014-11-18 12:16:30 +0000762 [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
Dave Martin8ef8f3602020-03-16 16:50:45 +0000763 [ESR_ELx_EC_BTI] = "BTI",
Mark Rutland60a1f022014-11-18 12:16:30 +0000764 [ESR_ELx_EC_ILL] = "PSTATE.IL",
765 [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
766 [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
767 [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
768 [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
769 [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
770 [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
771 [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
Dave Martin67236562017-10-31 15:51:00 +0000772 [ESR_ELx_EC_SVE] = "SVE",
Will Deacon332e5282019-07-16 08:14:19 +0100773 [ESR_ELx_EC_ERET] = "ERET/ERETAA/ERETAB",
Mark Rutland60a1f022014-11-18 12:16:30 +0000774 [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
775 [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
776 [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
777 [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
778 [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
779 [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
780 [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
781 [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
782 [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
783 [ESR_ELx_EC_SERROR] = "SError",
784 [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
785 [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
786 [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
787 [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
788 [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
789 [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
790 [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
791 [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
792 [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
793};
794
795const char *esr_get_class_string(u32 esr)
796{
Mark Rutland275f3442016-05-31 12:33:01 +0100797 return esr_class_str[ESR_ELx_EC(esr)];
Mark Rutland60a1f022014-11-18 12:16:30 +0000798}
799
Catalin Marinas60ffc302012-03-05 11:49:27 +0000800/*
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000801 * bad_mode handles the impossible case in the exception vector. This is always
802 * fatal.
Catalin Marinas60ffc302012-03-05 11:49:27 +0000803 */
804asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
805{
806 console_verbose();
807
Mark Rutland8051f4d2016-05-31 12:07:47 +0100808 pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
809 handler[reason], smp_processor_id(), esr,
810 esr_get_class_string(esr));
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000811
James Morse0fbeb312017-11-02 12:12:34 +0000812 local_daif_mask();
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000813 panic("bad mode");
814}
815
816/*
817 * bad_el0_sync handles unexpected, but potentially recoverable synchronous
818 * exceptions taken from EL0. Unlike bad_mode, this returns.
819 */
James Morseafa7c0e2019-10-25 17:42:15 +0100820void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000821{
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000822 void __user *pc = (void __user *)instruction_pointer(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000823
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000824 current->thread.fault_address = 0;
Will Deacon4e829b62018-02-20 15:18:13 +0000825 current->thread.fault_code = esr;
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000826
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200827 arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
828 "Bad EL0 synchronous exception");
Catalin Marinas60ffc302012-03-05 11:49:27 +0000829}
830
Mark Rutland872d8322017-07-14 20:30:35 +0100831#ifdef CONFIG_VMAP_STACK
832
833DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
834 __aligned(16);
835
836asmlinkage void handle_bad_stack(struct pt_regs *regs)
837{
838 unsigned long tsk_stk = (unsigned long)current->stack;
839 unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
840 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
841 unsigned int esr = read_sysreg(esr_el1);
842 unsigned long far = read_sysreg(far_el1);
843
844 console_verbose();
845 pr_emerg("Insufficient stack space to handle exception!");
846
847 pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
848 pr_emerg("FAR: 0x%016lx\n", far);
849
850 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
851 tsk_stk, tsk_stk + THREAD_SIZE);
852 pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
853 irq_stk, irq_stk + THREAD_SIZE);
854 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
855 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
856
857 __show_regs(regs);
858
859 /*
860 * We use nmi_panic to limit the potential for recusive overflows, and
861 * to get a better stack trace.
862 */
863 nmi_panic(NULL, "kernel stack overflow");
864 cpu_park_loop();
865}
866#endif
867
James Morse6bf0dcf2018-01-15 19:38:57 +0000868void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
Xie XiuQia92d4d12017-11-02 12:12:42 +0000869{
Xie XiuQia92d4d12017-11-02 12:12:42 +0000870 console_verbose();
871
872 pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
873 smp_processor_id(), esr, esr_get_class_string(esr));
James Morse6bf0dcf2018-01-15 19:38:57 +0000874 if (regs)
875 __show_regs(regs);
Xie XiuQia92d4d12017-11-02 12:12:42 +0000876
James Morse6bf0dcf2018-01-15 19:38:57 +0000877 nmi_panic(regs, "Asynchronous SError Interrupt");
878
879 cpu_park_loop();
880 unreachable();
881}
882
883bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
884{
885 u32 aet = arm64_ras_serror_get_severity(esr);
886
887 switch (aet) {
888 case ESR_ELx_AET_CE: /* corrected error */
889 case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
890 /*
891 * The CPU can make progress. We may take UEO again as
892 * a more severe error.
893 */
894 return false;
895
896 case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
897 case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
898 /*
899 * The CPU can't make progress. The exception may have
900 * been imprecise.
James Morse3276cc22019-06-18 16:17:38 +0100901 *
902 * Neoverse-N1 #1349291 means a non-KVM SError reported as
903 * Unrecoverable should be treated as Uncontainable. We
904 * call arm64_serror_panic() in both cases.
James Morse6bf0dcf2018-01-15 19:38:57 +0000905 */
906 return true;
907
908 case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
909 default:
910 /* Error has been silently propagated */
911 arm64_serror_panic(regs, esr);
912 }
913}
914
915asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
916{
Julien Thierry7d314642019-01-31 14:59:00 +0000917 const bool was_in_nmi = in_nmi();
918
919 if (!was_in_nmi)
920 nmi_enter();
James Morse6bf0dcf2018-01-15 19:38:57 +0000921
922 /* non-RAS errors are not containable */
923 if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
924 arm64_serror_panic(regs, esr);
925
Julien Thierry7d314642019-01-31 14:59:00 +0000926 if (!was_in_nmi)
927 nmi_exit();
Xie XiuQia92d4d12017-11-02 12:12:42 +0000928}
929
James Morse26718282019-08-20 18:45:57 +0100930asmlinkage void enter_from_user_mode(void)
931{
932 CT_WARN_ON(ct_state() != CONTEXT_USER);
933 user_exit_irqoff();
934}
935NOKPROBE_SYMBOL(enter_from_user_mode);
936
Catalin Marinas60ffc302012-03-05 11:49:27 +0000937void __pte_error(const char *file, int line, unsigned long val)
938{
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000939 pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000940}
941
942void __pmd_error(const char *file, int line, unsigned long val)
943{
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000944 pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000945}
946
Jungseok Leec79b954b2014-05-12 18:40:51 +0900947void __pud_error(const char *file, int line, unsigned long val)
948{
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000949 pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
Jungseok Leec79b954b2014-05-12 18:40:51 +0900950}
951
Catalin Marinas60ffc302012-03-05 11:49:27 +0000952void __pgd_error(const char *file, int line, unsigned long val)
953{
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000954 pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000955}
956
Dave P Martin9fb74102015-07-24 16:37:48 +0100957/* GENERIC_BUG traps */
958
959int is_valid_bugaddr(unsigned long addr)
960{
961 /*
962 * bug_handler() only called for BRK #BUG_BRK_IMM.
963 * So the answer is trivial -- any spurious instances with no
964 * bug table entry will be rejected by report_bug() and passed
965 * back to the debug-monitors code and handled as a fatal
966 * unexpected debug exception.
967 */
968 return 1;
969}
970
971static int bug_handler(struct pt_regs *regs, unsigned int esr)
972{
Dave P Martin9fb74102015-07-24 16:37:48 +0100973 switch (report_bug(regs->pc, regs)) {
974 case BUG_TRAP_TYPE_BUG:
975 die("Oops - BUG", regs, 0);
976 break;
977
978 case BUG_TRAP_TYPE_WARN:
979 break;
980
981 default:
982 /* unknown/unrecognised bug trap type */
983 return DBG_HOOK_ERROR;
984 }
985
986 /* If thread survives, skip over the BUG instruction and continue: */
Julien Thierry6436bee2017-10-25 10:04:33 +0100987 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Dave P Martin9fb74102015-07-24 16:37:48 +0100988 return DBG_HOOK_HANDLED;
989}
990
991static struct break_hook bug_break_hook = {
Dave P Martin9fb74102015-07-24 16:37:48 +0100992 .fn = bug_handler,
Will Deacon26a04d82019-02-26 12:52:47 +0000993 .imm = BUG_BRK_IMM,
Dave P Martin9fb74102015-07-24 16:37:48 +0100994};
995
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800996#ifdef CONFIG_KASAN_SW_TAGS
997
998#define KASAN_ESR_RECOVER 0x20
999#define KASAN_ESR_WRITE 0x10
1000#define KASAN_ESR_SIZE_MASK 0x0f
1001#define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK))
1002
1003static int kasan_handler(struct pt_regs *regs, unsigned int esr)
1004{
1005 bool recover = esr & KASAN_ESR_RECOVER;
1006 bool write = esr & KASAN_ESR_WRITE;
1007 size_t size = KASAN_ESR_SIZE(esr);
1008 u64 addr = regs->regs[0];
1009 u64 pc = regs->pc;
1010
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001011 kasan_report(addr, size, write, pc);
1012
1013 /*
1014 * The instrumentation allows to control whether we can proceed after
1015 * a crash was detected. This is done by passing the -recover flag to
1016 * the compiler. Disabling recovery allows to generate more compact
1017 * code.
1018 *
1019 * Unfortunately disabling recovery doesn't work for the kernel right
1020 * now. KASAN reporting is disabled in some contexts (for example when
1021 * the allocator accesses slab object metadata; this is controlled by
1022 * current->kasan_depth). All these accesses are detected by the tool,
1023 * even though the reports for them are not printed.
1024 *
1025 * This is something that might be fixed at some point in the future.
1026 */
1027 if (!recover)
1028 die("Oops - KASAN", regs, 0);
1029
1030 /* If thread survives, skip over the brk instruction and continue: */
1031 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
1032 return DBG_HOOK_HANDLED;
1033}
1034
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001035static struct break_hook kasan_break_hook = {
Will Deacon26a04d82019-02-26 12:52:47 +00001036 .fn = kasan_handler,
1037 .imm = KASAN_BRK_IMM,
1038 .mask = KASAN_BRK_MASK,
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001039};
1040#endif
1041
Dave P Martin9fb74102015-07-24 16:37:48 +01001042/*
1043 * Initial handler for AArch64 BRK exceptions
1044 * This handler only used until debug_traps_init().
1045 */
1046int __init early_brk64(unsigned long addr, unsigned int esr,
1047 struct pt_regs *regs)
1048{
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001049#ifdef CONFIG_KASAN_SW_TAGS
Will Deacon453b7742019-02-26 15:06:42 +00001050 unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
Will Deacon26a04d82019-02-26 12:52:47 +00001051
1052 if ((comment & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001053 return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
1054#endif
Dave P Martin9fb74102015-07-24 16:37:48 +01001055 return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
1056}
1057
1058/* This registration must happen early, before debug_traps_init(). */
Catalin Marinas60ffc302012-03-05 11:49:27 +00001059void __init trap_init(void)
1060{
Will Deacon26a04d82019-02-26 12:52:47 +00001061 register_kernel_break_hook(&bug_break_hook);
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001062#ifdef CONFIG_KASAN_SW_TAGS
Will Deacon26a04d82019-02-26 12:52:47 +00001063 register_kernel_break_hook(&kasan_break_hook);
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001064#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +00001065}