blob: 465f0a0f8f0ab7d28bd422a2b123d9470640c0a4 [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>
Catalin Marinas60ffc302012-03-05 11:49:27 +000038#include <asm/traps.h>
Mark Rutland872d8322017-07-14 20:30:35 +010039#include <asm/smp.h>
Mark Rutlanda9ea0012016-11-03 20:23:05 +000040#include <asm/stack_pointer.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000041#include <asm/stacktrace.h>
42#include <asm/exception.h>
43#include <asm/system_misc.h>
Andre Przywara7dd01ae2016-06-28 18:07:32 +010044#include <asm/sysreg.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000045
46static const char *handler[]= {
47 "Synchronous Abort",
48 "IRQ",
49 "FIQ",
50 "Error"
51};
52
Michael Weiser5ee39a72018-02-01 23:13:38 +010053int show_unhandled_signals = 0;
Catalin Marinas60ffc302012-03-05 11:49:27 +000054
Jungseok Lee9f93f3e2015-10-17 14:28:11 +000055static void dump_backtrace_entry(unsigned long where)
Catalin Marinas60ffc302012-03-05 11:49:27 +000056{
Will Deacona25ffd32017-10-19 13:19:20 +010057 printk(" %pS\n", (void *)where);
Catalin Marinas60ffc302012-03-05 11:49:27 +000058}
59
jinho lim7b716652019-06-26 20:50:13 +090060static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
Catalin Marinas60ffc302012-03-05 11:49:27 +000061{
62 unsigned long addr = instruction_pointer(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +000063 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
64 int i;
65
jinho lim7b716652019-06-26 20:50:13 +090066 if (user_mode(regs))
67 return;
68
Catalin Marinas60ffc302012-03-05 11:49:27 +000069 for (i = -4; i < 1; i++) {
70 unsigned int val, bad;
71
jinho lim7b716652019-06-26 20:50:13 +090072 bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
Catalin Marinas60ffc302012-03-05 11:49:27 +000073
74 if (!bad)
75 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
76 else {
77 p += sprintf(p, "bad PC value");
78 break;
79 }
80 }
Catalin Marinas60ffc302012-03-05 11:49:27 +000081
jinho lim7b716652019-06-26 20:50:13 +090082 printk("%sCode: %s\n", lvl, str);
Catalin Marinas60ffc302012-03-05 11:49:27 +000083}
84
Kefeng Wang1149aad2017-05-09 09:53:37 +080085void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
Catalin Marinas60ffc302012-03-05 11:49:27 +000086{
87 struct stackframe frame;
Will Deacon1e6f54402019-04-08 17:56:34 +010088 int skip = 0;
Catalin Marinas60ffc302012-03-05 11:49:27 +000089
Mark Rutlandb5e73072016-09-23 17:55:05 +010090 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
91
Will Deacon1e6f54402019-04-08 17:56:34 +010092 if (regs) {
93 if (user_mode(regs))
94 return;
95 skip = 1;
96 }
97
Mark Rutlandb5e73072016-09-23 17:55:05 +010098 if (!tsk)
99 tsk = current;
100
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000101 if (!try_get_task_stack(tsk))
102 return;
103
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900104 if (tsk == current) {
Dave Martinf3dcbe62019-07-02 14:07:28 +0100105 start_backtrace(&frame,
106 (unsigned long)__builtin_frame_address(0),
107 (unsigned long)dump_backtrace);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000108 } else {
109 /*
110 * task blocked in __switch_to
111 */
Dave Martinf3dcbe62019-07-02 14:07:28 +0100112 start_backtrace(&frame,
113 thread_saved_fp(tsk),
114 thread_saved_pc(tsk));
Catalin Marinas60ffc302012-03-05 11:49:27 +0000115 }
116
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000117 printk("Call trace:\n");
Will Deacona25ffd32017-10-19 13:19:20 +0100118 do {
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900119 /* skip until specified stack frame */
120 if (!skip) {
Ard Biesheuvel73267492017-07-22 18:45:33 +0100121 dump_backtrace_entry(frame.pc);
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900122 } else if (frame.fp == regs->regs[29]) {
123 skip = 0;
124 /*
125 * Mostly, this is the case where this function is
126 * called in panic/abort. As exception handler's
127 * stack frame does not contain the corresponding pc
128 * at which an exception has taken place, use regs->pc
129 * instead.
130 */
131 dump_backtrace_entry(regs->pc);
132 }
Will Deacona25ffd32017-10-19 13:19:20 +0100133 } while (!unwind_frame(tsk, &frame));
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000134
135 put_task_stack(tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000136}
137
Catalin Marinas60ffc302012-03-05 11:49:27 +0000138void show_stack(struct task_struct *tsk, unsigned long *sp)
139{
140 dump_backtrace(NULL, tsk);
141 barrier();
142}
143
144#ifdef CONFIG_PREEMPT
145#define S_PREEMPT " PREEMPT"
146#else
147#define S_PREEMPT ""
148#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +0000149#define S_SMP " SMP"
Catalin Marinas60ffc302012-03-05 11:49:27 +0000150
Mark Rutland876e7a32016-11-03 20:23:06 +0000151static int __die(const char *str, int err, struct pt_regs *regs)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000152{
Catalin Marinas60ffc302012-03-05 11:49:27 +0000153 static int die_counter;
154 int ret;
155
156 pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
157 str, err, ++die_counter);
158
159 /* trap and error numbers are mostly meaningless on ARM */
160 ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
161 if (ret == NOTIFY_STOP)
162 return ret;
163
164 print_modules();
Will Deacon1e6f54402019-04-08 17:56:34 +0100165 show_regs(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000166
jinho lim7b716652019-06-26 20:50:13 +0900167 dump_kernel_instr(KERN_EMERG, regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000168
169 return ret;
170}
171
172static DEFINE_RAW_SPINLOCK(die_lock);
173
174/*
175 * This function is protected against re-entrancy.
176 */
177void die(const char *str, struct pt_regs *regs, int err)
178{
Catalin Marinas60ffc302012-03-05 11:49:27 +0000179 int ret;
Qiao Zhou6f44a0b2017-07-07 17:29:34 +0800180 unsigned long flags;
181
182 raw_spin_lock_irqsave(&die_lock, flags);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000183
184 oops_enter();
185
Catalin Marinas60ffc302012-03-05 11:49:27 +0000186 console_verbose();
187 bust_spinlocks(1);
Mark Rutland876e7a32016-11-03 20:23:06 +0000188 ret = __die(str, err, regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000189
Mark Rutland876e7a32016-11-03 20:23:06 +0000190 if (regs && kexec_should_crash(current))
Catalin Marinas60ffc302012-03-05 11:49:27 +0000191 crash_kexec(regs);
192
193 bust_spinlocks(0);
Rusty Russell373d4d02013-01-21 17:17:39 +1030194 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000195 oops_exit();
196
197 if (in_interrupt())
198 panic("Fatal exception in interrupt");
199 if (panic_on_oops)
200 panic("Fatal exception");
Qiao Zhou6f44a0b2017-07-07 17:29:34 +0800201
202 raw_spin_unlock_irqrestore(&die_lock, flags);
203
Catalin Marinas60ffc302012-03-05 11:49:27 +0000204 if (ret != NOTIFY_STOP)
205 do_exit(SIGSEGV);
206}
207
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200208static void arm64_show_signal(int signo, const char *str)
Will Deacona26731d2018-02-20 15:08:51 +0000209{
210 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
211 DEFAULT_RATELIMIT_BURST);
Eric W. Biederman24b8f792018-09-22 00:38:41 +0200212 struct task_struct *tsk = current;
Will Deacona1ece822018-02-20 13:46:05 +0000213 unsigned int esr = tsk->thread.fault_code;
214 struct pt_regs *regs = task_pt_regs(tsk);
215
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200216 /* Leave if the signal won't be shown */
217 if (!show_unhandled_signals ||
218 !unhandled_signal(tsk, signo) ||
219 !__ratelimit(&rs))
220 return;
Will Deacona1ece822018-02-20 13:46:05 +0000221
222 pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
223 if (esr)
224 pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);
225
226 pr_cont("%s", str);
227 print_vma_addr(KERN_CONT " in ", regs->pc);
228 pr_cont("\n");
229 __show_regs(regs);
Eric W. Biederman1628a7c2018-09-22 00:52:21 +0200230}
Will Deacona1ece822018-02-20 13:46:05 +0000231
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200232void arm64_force_sig_fault(int signo, int code, void __user *addr,
233 const char *str)
234{
235 arm64_show_signal(signo, str);
Eric W. Biedermand76cac62019-05-23 11:11:19 -0500236 if (signo == SIGKILL)
Eric W. Biederman3cf5d072019-05-23 10:17:27 -0500237 force_sig(SIGKILL);
Eric W. Biedermand76cac62019-05-23 11:11:19 -0500238 else
Eric W. Biederman2e1661d22019-05-23 11:04:24 -0500239 force_sig_fault(signo, code, addr);
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200240}
241
Eric W. Biedermanb4d55572018-09-22 10:37:15 +0200242void arm64_force_sig_mceerr(int code, void __user *addr, short lsb,
243 const char *str)
244{
245 arm64_show_signal(SIGBUS, str);
Eric W. Biedermanf8eac902019-02-05 18:14:19 -0600246 force_sig_mceerr(code, addr, lsb);
Eric W. Biedermanb4d55572018-09-22 10:37:15 +0200247}
248
Eric W. Biedermanf3a900b2018-09-22 10:52:41 +0200249void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr,
250 const char *str)
251{
252 arm64_show_signal(SIGTRAP, str);
253 force_sig_ptrace_errno_trap(errno, addr);
Will Deacona1ece822018-02-20 13:46:05 +0000254}
255
Catalin Marinas60ffc302012-03-05 11:49:27 +0000256void arm64_notify_die(const char *str, struct pt_regs *regs,
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200257 int signo, int sicode, void __user *addr,
258 int err)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000259{
Catalin Marinas91413002014-04-06 23:04:12 +0100260 if (user_mode(regs)) {
Will Deacona1ece822018-02-20 13:46:05 +0000261 WARN_ON(regs != current_pt_regs());
Catalin Marinas91413002014-04-06 23:04:12 +0100262 current->thread.fault_address = 0;
263 current->thread.fault_code = err;
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200264
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200265 arm64_force_sig_fault(signo, sicode, addr, str);
Catalin Marinas91413002014-04-06 23:04:12 +0100266 } else {
Catalin Marinas60ffc302012-03-05 11:49:27 +0000267 die(str, regs, err);
Catalin Marinas91413002014-04-06 23:04:12 +0100268 }
Catalin Marinas60ffc302012-03-05 11:49:27 +0000269}
270
Julien Thierry6436bee2017-10-25 10:04:33 +0100271void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
272{
273 regs->pc += size;
274
275 /*
276 * If we were single stepping, we want to get the step exception after
277 * we return from the trap.
278 */
Mark Rutland9478f192018-04-03 11:22:51 +0100279 if (user_mode(regs))
280 user_fastforward_single_step(current);
Julien Thierry6436bee2017-10-25 10:04:33 +0100281}
282
Punit Agrawal9b79f522014-11-18 11:41:22 +0000283static LIST_HEAD(undef_hook);
284static DEFINE_RAW_SPINLOCK(undef_lock);
285
286void register_undef_hook(struct undef_hook *hook)
287{
288 unsigned long flags;
289
290 raw_spin_lock_irqsave(&undef_lock, flags);
291 list_add(&hook->node, &undef_hook);
292 raw_spin_unlock_irqrestore(&undef_lock, flags);
293}
294
295void unregister_undef_hook(struct undef_hook *hook)
296{
297 unsigned long flags;
298
299 raw_spin_lock_irqsave(&undef_lock, flags);
300 list_del(&hook->node);
301 raw_spin_unlock_irqrestore(&undef_lock, flags);
302}
303
304static int call_undef_hook(struct pt_regs *regs)
305{
306 struct undef_hook *hook;
307 unsigned long flags;
308 u32 instr;
309 int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
310 void __user *pc = (void __user *)instruction_pointer(regs);
311
Will Deacon0bf0f442018-08-07 13:43:06 +0100312 if (!user_mode(regs)) {
313 __le32 instr_le;
314 if (probe_kernel_address((__force __le32 *)pc, instr_le))
315 goto exit;
316 instr = le32_to_cpu(instr_le);
317 } else if (compat_thumb_mode(regs)) {
Punit Agrawal9b79f522014-11-18 11:41:22 +0000318 /* 16-bit Thumb instruction */
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200319 __le16 instr_le;
320 if (get_user(instr_le, (__le16 __user *)pc))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000321 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200322 instr = le16_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000323 if (aarch32_insn_is_wide(instr)) {
324 u32 instr2;
325
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200326 if (get_user(instr_le, (__le16 __user *)(pc + 2)))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000327 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200328 instr2 = le16_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000329 instr = (instr << 16) | instr2;
330 }
331 } else {
332 /* 32-bit ARM instruction */
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200333 __le32 instr_le;
334 if (get_user(instr_le, (__le32 __user *)pc))
Punit Agrawal9b79f522014-11-18 11:41:22 +0000335 goto exit;
Luc Van Oostenryck6cf5d4a2017-06-28 16:55:55 +0200336 instr = le32_to_cpu(instr_le);
Punit Agrawal9b79f522014-11-18 11:41:22 +0000337 }
338
339 raw_spin_lock_irqsave(&undef_lock, flags);
340 list_for_each_entry(hook, &undef_hook, node)
341 if ((instr & hook->instr_mask) == hook->instr_val &&
342 (regs->pstate & hook->pstate_mask) == hook->pstate_val)
343 fn = hook->fn;
344
345 raw_spin_unlock_irqrestore(&undef_lock, flags);
346exit:
347 return fn ? fn(regs, instr) : 1;
348}
349
Will Deacon2c9120f32018-02-20 14:16:29 +0000350void force_signal_inject(int signal, int code, unsigned long address)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000351{
Andre Przywara390bf172016-06-28 18:07:31 +0100352 const char *desc;
Will Deacon2c9120f32018-02-20 14:16:29 +0000353 struct pt_regs *regs = current_pt_regs();
354
Will Deacon8a604192018-08-14 16:24:54 +0100355 if (WARN_ON(!user_mode(regs)))
356 return;
357
Andre Przywara390bf172016-06-28 18:07:31 +0100358 switch (signal) {
359 case SIGILL:
360 desc = "undefined instruction";
361 break;
362 case SIGSEGV:
363 desc = "illegal memory access";
364 break;
365 default:
Dave Martinbc0ee472017-10-31 15:51:05 +0000366 desc = "unknown or unrecoverable error";
Andre Przywara390bf172016-06-28 18:07:31 +0100367 break;
368 }
369
Will Deacona7e6f1c2018-02-20 18:08:40 +0000370 /* Force signals we don't understand to SIGKILL */
Mark Rutlandb2d71b32018-04-16 16:45:01 +0100371 if (WARN_ON(signal != SIGKILL &&
Will Deacona7e6f1c2018-02-20 18:08:40 +0000372 siginfo_layout(signal, code) != SIL_FAULT)) {
373 signal = SIGKILL;
374 }
375
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200376 arm64_notify_die(desc, regs, signal, code, (void __user *)address, 0);
Andre Przywara390bf172016-06-28 18:07:31 +0100377}
378
379/*
380 * Set up process info to signal segmentation fault - called on access error.
381 */
Will Deacon2c9120f32018-02-20 14:16:29 +0000382void arm64_notify_segfault(unsigned long addr)
Andre Przywara390bf172016-06-28 18:07:31 +0100383{
384 int code;
385
386 down_read(&current->mm->mmap_sem);
387 if (find_vma(current->mm, addr) == NULL)
388 code = SEGV_MAPERR;
389 else
390 code = SEGV_ACCERR;
391 up_read(&current->mm->mmap_sem);
392
Will Deacon2c9120f32018-02-20 14:16:29 +0000393 force_signal_inject(SIGSEGV, code, addr);
Andre Przywara390bf172016-06-28 18:07:31 +0100394}
395
396asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
397{
Catalin Marinas60ffc302012-03-05 11:49:27 +0000398 /* check for AArch32 breakpoint instructions */
Will Deacon1442b6e2013-03-16 08:48:13 +0000399 if (!aarch32_break_handler(regs))
Catalin Marinas60ffc302012-03-05 11:49:27 +0000400 return;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000401
Punit Agrawal9b79f522014-11-18 11:41:22 +0000402 if (call_undef_hook(regs) == 0)
403 return;
404
Will Deacon0bf0f442018-08-07 13:43:06 +0100405 BUG_ON(!user_mode(regs));
Will Deacon8a604192018-08-14 16:24:54 +0100406 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000407}
408
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100409#define __user_cache_maint(insn, address, res) \
Kristina Martsenko81cddd62017-05-03 16:37:45 +0100410 if (address >= user_addr_max()) { \
Andre Przywara87261d12016-10-19 14:40:54 +0100411 res = -EFAULT; \
Catalin Marinas39bc88e2016-09-02 14:54:03 +0100412 } else { \
413 uaccess_ttbr0_enable(); \
Andre Przywara87261d12016-10-19 14:40:54 +0100414 asm volatile ( \
415 "1: " insn ", %1\n" \
416 " mov %w0, #0\n" \
417 "2:\n" \
418 " .pushsection .fixup,\"ax\"\n" \
419 " .align 2\n" \
420 "3: mov %w0, %w2\n" \
421 " b 2b\n" \
422 " .popsection\n" \
423 _ASM_EXTABLE(1b, 3b) \
424 : "=r" (res) \
Catalin Marinas39bc88e2016-09-02 14:54:03 +0100425 : "r" (address), "i" (-EFAULT)); \
426 uaccess_ttbr0_disable(); \
427 }
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100428
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100429static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100430{
431 unsigned long address;
Anshuman Khandual1c839142018-09-20 09:36:19 +0530432 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100433 int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
434 int ret = 0;
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100435
Kristina Martsenko81cddd62017-05-03 16:37:45 +0100436 address = untagged_addr(pt_regs_read_reg(regs, rt));
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100437
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100438 switch (crm) {
439 case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
440 __user_cache_maint("dc civac", address, ret);
441 break;
442 case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
443 __user_cache_maint("dc civac", address, ret);
444 break;
Andrew Murrayd16ed4102019-04-09 10:52:42 +0100445 case ESR_ELx_SYS64_ISS_CRM_DC_CVADP: /* DC CVADP */
446 __user_cache_maint("sys 3, c7, c13, 1", address, ret);
447 break;
Robin Murphye1bc5d12017-07-25 11:55:41 +0100448 case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
449 __user_cache_maint("sys 3, c7, c12, 1", address, ret);
450 break;
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100451 case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
452 __user_cache_maint("dc civac", address, ret);
453 break;
454 case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
455 __user_cache_maint("ic ivau", address, ret);
456 break;
457 default:
Will Deacon2c9120f32018-02-20 14:16:29 +0000458 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100459 return;
460 }
461
462 if (ret)
Will Deacon2c9120f32018-02-20 14:16:29 +0000463 arm64_notify_segfault(address);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100464 else
Julien Thierry6436bee2017-10-25 10:04:33 +0100465 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Andre Przywara7dd01ae2016-06-28 18:07:32 +0100466}
467
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100468static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
469{
Anshuman Khandual1c839142018-09-20 09:36:19 +0530470 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Mark Rutland8b6e70f2017-02-09 15:19:19 +0000471 unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100472
James Morse05460842019-10-17 18:42:58 +0100473 if (cpus_have_const_cap(ARM64_WORKAROUND_1542419))
474 val &= ~BIT(CTR_DIC_SHIFT);
475
Mark Rutland8b6e70f2017-02-09 15:19:19 +0000476 pt_regs_write_reg(regs, rt, val);
477
Julien Thierry6436bee2017-10-25 10:04:33 +0100478 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100479}
480
Marc Zyngier6126ce02017-02-01 11:48:58 +0000481static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
482{
Anshuman Khandual1c839142018-09-20 09:36:19 +0530483 int rt = ESR_ELx_SYS64_ISS_RT(esr);
Marc Zyngier6126ce02017-02-01 11:48:58 +0000484
Marc Zyngierdea86a82019-04-08 16:49:03 +0100485 pt_regs_write_reg(regs, rt, arch_timer_read_counter());
Julien Thierry6436bee2017-10-25 10:04:33 +0100486 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Marc Zyngier6126ce02017-02-01 11:48:58 +0000487}
488
Marc Zyngier98421192017-04-24 09:04:03 +0100489static void cntfrq_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);
Marc Zyngier98421192017-04-24 09:04:03 +0100492
Marc Zyngierc6f97ad2017-07-21 18:15:27 +0100493 pt_regs_write_reg(regs, rt, arch_timer_get_rate());
Julien Thierry6436bee2017-10-25 10:04:33 +0100494 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Marc Zyngier98421192017-04-24 09:04:03 +0100495}
496
Anshuman Khandual21f84792018-09-20 09:36:21 +0530497static void mrs_handler(unsigned int esr, struct pt_regs *regs)
498{
499 u32 sysreg, rt;
500
501 rt = ESR_ELx_SYS64_ISS_RT(esr);
502 sysreg = esr_sys64_to_sysreg(esr);
503
504 if (do_emulate_mrs(regs, sysreg, rt) != 0)
505 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
506}
507
Marc Zyngierc219bc42018-10-01 12:19:43 +0100508static void wfi_handler(unsigned int esr, struct pt_regs *regs)
509{
510 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
511}
512
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100513struct sys64_hook {
514 unsigned int esr_mask;
515 unsigned int esr_val;
516 void (*handler)(unsigned int esr, struct pt_regs *regs);
517};
518
Mark Rutland37143dc2019-08-13 15:16:39 +0100519static const struct sys64_hook sys64_hooks[] = {
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100520 {
521 .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
522 .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
523 .handler = user_cache_maint_handler,
524 },
Suzuki K Poulose116c81f2016-09-09 14:07:16 +0100525 {
526 /* Trap read access to CTR_EL0 */
527 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
528 .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
529 .handler = ctr_read_handler,
530 },
Marc Zyngier6126ce02017-02-01 11:48:58 +0000531 {
532 /* Trap read access to CNTVCT_EL0 */
533 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
534 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
535 .handler = cntvct_read_handler,
536 },
Marc Zyngier98421192017-04-24 09:04:03 +0100537 {
538 /* Trap read access to CNTFRQ_EL0 */
539 .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
540 .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
541 .handler = cntfrq_read_handler,
542 },
Anshuman Khandual21f84792018-09-20 09:36:21 +0530543 {
544 /* Trap read access to CPUID registers */
545 .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
546 .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
547 .handler = mrs_handler,
548 },
Marc Zyngierc219bc42018-10-01 12:19:43 +0100549 {
550 /* Trap WFI instructions executed in userspace */
551 .esr_mask = ESR_ELx_WFx_MASK,
552 .esr_val = ESR_ELx_WFx_WFI_VAL,
553 .handler = wfi_handler,
554 },
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100555 {},
556};
557
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100558
559#ifdef CONFIG_COMPAT
Marc Zyngier1f1c0142018-09-27 17:15:30 +0100560#define PSTATE_IT_1_0_SHIFT 25
561#define PSTATE_IT_1_0_MASK (0x3 << PSTATE_IT_1_0_SHIFT)
562#define PSTATE_IT_7_2_SHIFT 10
563#define PSTATE_IT_7_2_MASK (0x3f << PSTATE_IT_7_2_SHIFT)
564
565static u32 compat_get_it_state(struct pt_regs *regs)
566{
567 u32 it, pstate = regs->pstate;
568
569 it = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
570 it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
571
572 return it;
573}
574
575static void compat_set_it_state(struct pt_regs *regs, u32 it)
576{
577 u32 pstate_it;
578
579 pstate_it = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
580 pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
581
582 regs->pstate &= ~PSR_AA32_IT_MASK;
583 regs->pstate |= pstate_it;
584}
585
586static 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
606static void advance_itstate(struct pt_regs *regs)
607{
608 u32 it;
609
610 /* ARM mode */
611 if (!(regs->pstate & PSR_AA32_T_BIT) ||
612 !(regs->pstate & PSR_AA32_IT_MASK))
613 return;
614
615 it = compat_get_it_state(regs);
616
617 /*
618 * If this is the last instruction of the block, wipe the IT
619 * state. Otherwise advance it.
620 */
621 if (!(it & 7))
622 it = 0;
623 else
624 it = (it & 0xe0) | ((it << 1) & 0x1f);
625
626 compat_set_it_state(regs, it);
627}
628
629static void arm64_compat_skip_faulting_instruction(struct pt_regs *regs,
630 unsigned int sz)
631{
632 advance_itstate(regs);
633 arm64_skip_faulting_instruction(regs, sz);
634}
635
Marc Zyngier32a3e632018-09-27 17:15:33 +0100636static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
637{
638 int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
639
640 pt_regs_write_reg(regs, reg, arch_timer_get_rate());
641 arm64_compat_skip_faulting_instruction(regs, 4);
642}
643
Mark Rutland37143dc2019-08-13 15:16:39 +0100644static const struct sys64_hook cp15_32_hooks[] = {
Marc Zyngier32a3e632018-09-27 17:15:33 +0100645 {
646 .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
647 .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
648 .handler = compat_cntfrq_read_handler,
649 },
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100650 {},
651};
652
Marc Zyngier50de0132018-09-27 17:15:32 +0100653static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
654{
655 int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
656 int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
Marc Zyngierdea86a82019-04-08 16:49:03 +0100657 u64 val = arch_timer_read_counter();
Marc Zyngier50de0132018-09-27 17:15:32 +0100658
659 pt_regs_write_reg(regs, rt, lower_32_bits(val));
660 pt_regs_write_reg(regs, rt2, upper_32_bits(val));
661 arm64_compat_skip_faulting_instruction(regs, 4);
662}
663
Mark Rutland37143dc2019-08-13 15:16:39 +0100664static const struct sys64_hook cp15_64_hooks[] = {
Marc Zyngier50de0132018-09-27 17:15:32 +0100665 {
666 .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
667 .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
668 .handler = compat_cntvct_read_handler,
669 },
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100670 {},
671};
672
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100673asmlinkage void __exception do_cp15instr(unsigned int esr, struct pt_regs *regs)
674{
Mark Rutland37143dc2019-08-13 15:16:39 +0100675 const struct sys64_hook *hook, *hook_base;
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100676
Marc Zyngier1f1c0142018-09-27 17:15:30 +0100677 if (!cp15_cond_valid(esr, regs)) {
678 /*
679 * There is no T16 variant of a CP access, so we
680 * always advance PC by 4 bytes.
681 */
682 arm64_compat_skip_faulting_instruction(regs, 4);
683 return;
684 }
685
Marc Zyngier2a8905e2018-09-27 17:15:31 +0100686 switch (ESR_ELx_EC(esr)) {
687 case ESR_ELx_EC_CP15_32:
688 hook_base = cp15_32_hooks;
689 break;
690 case ESR_ELx_EC_CP15_64:
691 hook_base = cp15_64_hooks;
692 break;
693 default:
694 do_undefinstr(regs);
695 return;
696 }
697
698 for (hook = hook_base; hook->handler; hook++)
699 if ((hook->esr_mask & esr) == hook->esr_val) {
700 hook->handler(esr, regs);
701 return;
702 }
703
Marc Zyngier70c63cd2018-09-27 17:15:29 +0100704 /*
705 * New cp15 instructions may previously have been undefined at
706 * EL0. Fall back to our usual undefined instruction handler
707 * so that we handle these consistently.
708 */
709 do_undefinstr(regs);
710}
711#endif
712
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100713asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
714{
Mark Rutland37143dc2019-08-13 15:16:39 +0100715 const struct sys64_hook *hook;
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100716
717 for (hook = sys64_hooks; hook->handler; hook++)
718 if ((hook->esr_mask & esr) == hook->esr_val) {
719 hook->handler(esr, regs);
720 return;
721 }
722
Mark Rutland49f6cba2017-01-27 16:15:38 +0000723 /*
724 * New SYS instructions may previously have been undefined at EL0. Fall
725 * back to our usual undefined instruction handler so that we handle
726 * these consistently.
727 */
728 do_undefinstr(regs);
Suzuki K Poulose9dbd5bb2016-09-09 14:07:15 +0100729}
730
Mark Rutland60a1f022014-11-18 12:16:30 +0000731static const char *esr_class_str[] = {
732 [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
733 [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
734 [ESR_ELx_EC_WFx] = "WFI/WFE",
735 [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
736 [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
737 [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
738 [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
739 [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
740 [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
Zenghui Yu6701c612019-07-13 04:40:54 +0000741 [ESR_ELx_EC_PAC] = "PAC",
Mark Rutland60a1f022014-11-18 12:16:30 +0000742 [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
743 [ESR_ELx_EC_ILL] = "PSTATE.IL",
744 [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
745 [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
746 [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
747 [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
748 [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
749 [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
750 [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
Dave Martin67236562017-10-31 15:51:00 +0000751 [ESR_ELx_EC_SVE] = "SVE",
Will Deacon332e5282019-07-16 08:14:19 +0100752 [ESR_ELx_EC_ERET] = "ERET/ERETAA/ERETAB",
Mark Rutland60a1f022014-11-18 12:16:30 +0000753 [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
754 [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
755 [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
756 [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
757 [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
758 [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
759 [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
760 [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
761 [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
762 [ESR_ELx_EC_SERROR] = "SError",
763 [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
764 [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
765 [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
766 [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
767 [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
768 [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
769 [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
770 [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
771 [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
772};
773
774const char *esr_get_class_string(u32 esr)
775{
Mark Rutland275f3442016-05-31 12:33:01 +0100776 return esr_class_str[ESR_ELx_EC(esr)];
Mark Rutland60a1f022014-11-18 12:16:30 +0000777}
778
Catalin Marinas60ffc302012-03-05 11:49:27 +0000779/*
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000780 * bad_mode handles the impossible case in the exception vector. This is always
781 * fatal.
Catalin Marinas60ffc302012-03-05 11:49:27 +0000782 */
783asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
784{
785 console_verbose();
786
Mark Rutland8051f4d2016-05-31 12:07:47 +0100787 pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
788 handler[reason], smp_processor_id(), esr,
789 esr_get_class_string(esr));
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000790
James Morse0fbeb312017-11-02 12:12:34 +0000791 local_daif_mask();
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000792 panic("bad mode");
793}
794
795/*
796 * bad_el0_sync handles unexpected, but potentially recoverable synchronous
797 * exceptions taken from EL0. Unlike bad_mode, this returns.
798 */
799asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
800{
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000801 void __user *pc = (void __user *)instruction_pointer(regs);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000802
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000803 current->thread.fault_address = 0;
Will Deacon4e829b62018-02-20 15:18:13 +0000804 current->thread.fault_code = esr;
Mark Rutland7d9e8f72017-01-18 17:23:41 +0000805
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200806 arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
807 "Bad EL0 synchronous exception");
Catalin Marinas60ffc302012-03-05 11:49:27 +0000808}
809
Mark Rutland872d8322017-07-14 20:30:35 +0100810#ifdef CONFIG_VMAP_STACK
811
812DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
813 __aligned(16);
814
815asmlinkage void handle_bad_stack(struct pt_regs *regs)
816{
817 unsigned long tsk_stk = (unsigned long)current->stack;
818 unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
819 unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
820 unsigned int esr = read_sysreg(esr_el1);
821 unsigned long far = read_sysreg(far_el1);
822
823 console_verbose();
824 pr_emerg("Insufficient stack space to handle exception!");
825
826 pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
827 pr_emerg("FAR: 0x%016lx\n", far);
828
829 pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
830 tsk_stk, tsk_stk + THREAD_SIZE);
831 pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
832 irq_stk, irq_stk + THREAD_SIZE);
833 pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
834 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
835
836 __show_regs(regs);
837
838 /*
839 * We use nmi_panic to limit the potential for recusive overflows, and
840 * to get a better stack trace.
841 */
842 nmi_panic(NULL, "kernel stack overflow");
843 cpu_park_loop();
844}
845#endif
846
James Morse6bf0dcf2018-01-15 19:38:57 +0000847void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
Xie XiuQia92d4d12017-11-02 12:12:42 +0000848{
Xie XiuQia92d4d12017-11-02 12:12:42 +0000849 console_verbose();
850
851 pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
852 smp_processor_id(), esr, esr_get_class_string(esr));
James Morse6bf0dcf2018-01-15 19:38:57 +0000853 if (regs)
854 __show_regs(regs);
Xie XiuQia92d4d12017-11-02 12:12:42 +0000855
James Morse6bf0dcf2018-01-15 19:38:57 +0000856 nmi_panic(regs, "Asynchronous SError Interrupt");
857
858 cpu_park_loop();
859 unreachable();
860}
861
862bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
863{
864 u32 aet = arm64_ras_serror_get_severity(esr);
865
866 switch (aet) {
867 case ESR_ELx_AET_CE: /* corrected error */
868 case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
869 /*
870 * The CPU can make progress. We may take UEO again as
871 * a more severe error.
872 */
873 return false;
874
875 case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
876 case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
877 /*
878 * The CPU can't make progress. The exception may have
879 * been imprecise.
James Morse3276cc22019-06-18 16:17:38 +0100880 *
881 * Neoverse-N1 #1349291 means a non-KVM SError reported as
882 * Unrecoverable should be treated as Uncontainable. We
883 * call arm64_serror_panic() in both cases.
James Morse6bf0dcf2018-01-15 19:38:57 +0000884 */
885 return true;
886
887 case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
888 default:
889 /* Error has been silently propagated */
890 arm64_serror_panic(regs, esr);
891 }
892}
893
894asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
895{
Julien Thierry7d314642019-01-31 14:59:00 +0000896 const bool was_in_nmi = in_nmi();
897
898 if (!was_in_nmi)
899 nmi_enter();
James Morse6bf0dcf2018-01-15 19:38:57 +0000900
901 /* non-RAS errors are not containable */
902 if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
903 arm64_serror_panic(regs, esr);
904
Julien Thierry7d314642019-01-31 14:59:00 +0000905 if (!was_in_nmi)
906 nmi_exit();
Xie XiuQia92d4d12017-11-02 12:12:42 +0000907}
908
James Morse26718282019-08-20 18:45:57 +0100909asmlinkage void enter_from_user_mode(void)
910{
911 CT_WARN_ON(ct_state() != CONTEXT_USER);
912 user_exit_irqoff();
913}
914NOKPROBE_SYMBOL(enter_from_user_mode);
915
Catalin Marinas60ffc302012-03-05 11:49:27 +0000916void __pte_error(const char *file, int line, unsigned long val)
917{
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000918 pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000919}
920
921void __pmd_error(const char *file, int line, unsigned long val)
922{
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000923 pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000924}
925
Jungseok Leec79b954b2014-05-12 18:40:51 +0900926void __pud_error(const char *file, int line, unsigned long val)
927{
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000928 pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
Jungseok Leec79b954b2014-05-12 18:40:51 +0900929}
930
Catalin Marinas60ffc302012-03-05 11:49:27 +0000931void __pgd_error(const char *file, int line, unsigned long val)
932{
Will Deaconc9cd0ed2015-12-21 16:44:27 +0000933 pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000934}
935
Dave P Martin9fb74102015-07-24 16:37:48 +0100936/* GENERIC_BUG traps */
937
938int is_valid_bugaddr(unsigned long addr)
939{
940 /*
941 * bug_handler() only called for BRK #BUG_BRK_IMM.
942 * So the answer is trivial -- any spurious instances with no
943 * bug table entry will be rejected by report_bug() and passed
944 * back to the debug-monitors code and handled as a fatal
945 * unexpected debug exception.
946 */
947 return 1;
948}
949
950static int bug_handler(struct pt_regs *regs, unsigned int esr)
951{
Dave P Martin9fb74102015-07-24 16:37:48 +0100952 switch (report_bug(regs->pc, regs)) {
953 case BUG_TRAP_TYPE_BUG:
954 die("Oops - BUG", regs, 0);
955 break;
956
957 case BUG_TRAP_TYPE_WARN:
958 break;
959
960 default:
961 /* unknown/unrecognised bug trap type */
962 return DBG_HOOK_ERROR;
963 }
964
965 /* If thread survives, skip over the BUG instruction and continue: */
Julien Thierry6436bee2017-10-25 10:04:33 +0100966 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
Dave P Martin9fb74102015-07-24 16:37:48 +0100967 return DBG_HOOK_HANDLED;
968}
969
970static struct break_hook bug_break_hook = {
Dave P Martin9fb74102015-07-24 16:37:48 +0100971 .fn = bug_handler,
Will Deacon26a04d82019-02-26 12:52:47 +0000972 .imm = BUG_BRK_IMM,
Dave P Martin9fb74102015-07-24 16:37:48 +0100973};
974
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800975#ifdef CONFIG_KASAN_SW_TAGS
976
977#define KASAN_ESR_RECOVER 0x20
978#define KASAN_ESR_WRITE 0x10
979#define KASAN_ESR_SIZE_MASK 0x0f
980#define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK))
981
982static int kasan_handler(struct pt_regs *regs, unsigned int esr)
983{
984 bool recover = esr & KASAN_ESR_RECOVER;
985 bool write = esr & KASAN_ESR_WRITE;
986 size_t size = KASAN_ESR_SIZE(esr);
987 u64 addr = regs->regs[0];
988 u64 pc = regs->pc;
989
Andrey Konovalov41eea9c2018-12-28 00:30:54 -0800990 kasan_report(addr, size, write, pc);
991
992 /*
993 * The instrumentation allows to control whether we can proceed after
994 * a crash was detected. This is done by passing the -recover flag to
995 * the compiler. Disabling recovery allows to generate more compact
996 * code.
997 *
998 * Unfortunately disabling recovery doesn't work for the kernel right
999 * now. KASAN reporting is disabled in some contexts (for example when
1000 * the allocator accesses slab object metadata; this is controlled by
1001 * current->kasan_depth). All these accesses are detected by the tool,
1002 * even though the reports for them are not printed.
1003 *
1004 * This is something that might be fixed at some point in the future.
1005 */
1006 if (!recover)
1007 die("Oops - KASAN", regs, 0);
1008
1009 /* If thread survives, skip over the brk instruction and continue: */
1010 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
1011 return DBG_HOOK_HANDLED;
1012}
1013
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001014static struct break_hook kasan_break_hook = {
Will Deacon26a04d82019-02-26 12:52:47 +00001015 .fn = kasan_handler,
1016 .imm = KASAN_BRK_IMM,
1017 .mask = KASAN_BRK_MASK,
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001018};
1019#endif
1020
Dave P Martin9fb74102015-07-24 16:37:48 +01001021/*
1022 * Initial handler for AArch64 BRK exceptions
1023 * This handler only used until debug_traps_init().
1024 */
1025int __init early_brk64(unsigned long addr, unsigned int esr,
1026 struct pt_regs *regs)
1027{
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001028#ifdef CONFIG_KASAN_SW_TAGS
Will Deacon453b7742019-02-26 15:06:42 +00001029 unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
Will Deacon26a04d82019-02-26 12:52:47 +00001030
1031 if ((comment & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001032 return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
1033#endif
Dave P Martin9fb74102015-07-24 16:37:48 +01001034 return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
1035}
1036
1037/* This registration must happen early, before debug_traps_init(). */
Catalin Marinas60ffc302012-03-05 11:49:27 +00001038void __init trap_init(void)
1039{
Will Deacon26a04d82019-02-26 12:52:47 +00001040 register_kernel_break_hook(&bug_break_hook);
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001041#ifdef CONFIG_KASAN_SW_TAGS
Will Deacon26a04d82019-02-26 12:52:47 +00001042 register_kernel_break_hook(&kasan_break_hook);
Andrey Konovalov41eea9c2018-12-28 00:30:54 -08001043#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +00001044}