xref: /openbmc/linux/arch/arm64/kernel/traps.c (revision 83f865d7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/kernel/traps.c
4  *
5  * Copyright (C) 1995-2009 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  */
8 
9 #include <linux/bug.h>
10 #include <linux/context_tracking.h>
11 #include <linux/signal.h>
12 #include <linux/personality.h>
13 #include <linux/kallsyms.h>
14 #include <linux/kprobes.h>
15 #include <linux/spinlock.h>
16 #include <linux/uaccess.h>
17 #include <linux/hardirq.h>
18 #include <linux/kdebug.h>
19 #include <linux/module.h>
20 #include <linux/kexec.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/sched/signal.h>
24 #include <linux/sched/debug.h>
25 #include <linux/sched/task_stack.h>
26 #include <linux/sizes.h>
27 #include <linux/syscalls.h>
28 #include <linux/mm_types.h>
29 #include <linux/kasan.h>
30 
31 #include <asm/atomic.h>
32 #include <asm/bug.h>
33 #include <asm/cpufeature.h>
34 #include <asm/daifflags.h>
35 #include <asm/debug-monitors.h>
36 #include <asm/esr.h>
37 #include <asm/extable.h>
38 #include <asm/insn.h>
39 #include <asm/kprobes.h>
40 #include <asm/traps.h>
41 #include <asm/smp.h>
42 #include <asm/stack_pointer.h>
43 #include <asm/stacktrace.h>
44 #include <asm/exception.h>
45 #include <asm/system_misc.h>
46 #include <asm/sysreg.h>
47 
48 static const char *handler[]= {
49 	"Synchronous Abort",
50 	"IRQ",
51 	"FIQ",
52 	"Error"
53 };
54 
55 int show_unhandled_signals = 0;
56 
57 static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
58 {
59 	unsigned long addr = instruction_pointer(regs);
60 	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
61 	int i;
62 
63 	if (user_mode(regs))
64 		return;
65 
66 	for (i = -4; i < 1; i++) {
67 		unsigned int val, bad;
68 
69 		bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
70 
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 	}
78 
79 	printk("%sCode: %s\n", lvl, str);
80 }
81 
82 #ifdef CONFIG_PREEMPT
83 #define S_PREEMPT " PREEMPT"
84 #elif defined(CONFIG_PREEMPT_RT)
85 #define S_PREEMPT " PREEMPT_RT"
86 #else
87 #define S_PREEMPT ""
88 #endif
89 
90 #define S_SMP " SMP"
91 
92 static int __die(const char *str, int err, struct pt_regs *regs)
93 {
94 	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();
106 	show_regs(regs);
107 
108 	dump_kernel_instr(KERN_EMERG, regs);
109 
110 	return ret;
111 }
112 
113 static DEFINE_RAW_SPINLOCK(die_lock);
114 
115 /*
116  * This function is protected against re-entrancy.
117  */
118 void die(const char *str, struct pt_regs *regs, int err)
119 {
120 	int ret;
121 	unsigned long flags;
122 
123 	raw_spin_lock_irqsave(&die_lock, flags);
124 
125 	oops_enter();
126 
127 	console_verbose();
128 	bust_spinlocks(1);
129 	ret = __die(str, err, regs);
130 
131 	if (regs && kexec_should_crash(current))
132 		crash_kexec(regs);
133 
134 	bust_spinlocks(0);
135 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
136 	oops_exit();
137 
138 	if (in_interrupt())
139 		panic("%s: Fatal exception in interrupt", str);
140 	if (panic_on_oops)
141 		panic("%s: Fatal exception", str);
142 
143 	raw_spin_unlock_irqrestore(&die_lock, flags);
144 
145 	if (ret != NOTIFY_STOP)
146 		do_exit(SIGSEGV);
147 }
148 
149 static void arm64_show_signal(int signo, const char *str)
150 {
151 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
152 				      DEFAULT_RATELIMIT_BURST);
153 	struct task_struct *tsk = current;
154 	unsigned int esr = tsk->thread.fault_code;
155 	struct pt_regs *regs = task_pt_regs(tsk);
156 
157 	/* Leave if the signal won't be shown */
158 	if (!show_unhandled_signals ||
159 	    !unhandled_signal(tsk, signo) ||
160 	    !__ratelimit(&rs))
161 		return;
162 
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);
171 }
172 
173 void arm64_force_sig_fault(int signo, int code, void __user *addr,
174 			   const char *str)
175 {
176 	arm64_show_signal(signo, str);
177 	if (signo == SIGKILL)
178 		force_sig(SIGKILL);
179 	else
180 		force_sig_fault(signo, code, addr);
181 }
182 
183 void arm64_force_sig_mceerr(int code, void __user *addr, short lsb,
184 			    const char *str)
185 {
186 	arm64_show_signal(SIGBUS, str);
187 	force_sig_mceerr(code, addr, lsb);
188 }
189 
190 void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr,
191 				       const char *str)
192 {
193 	arm64_show_signal(SIGTRAP, str);
194 	force_sig_ptrace_errno_trap(errno, addr);
195 }
196 
197 void arm64_notify_die(const char *str, struct pt_regs *regs,
198 		      int signo, int sicode, void __user *addr,
199 		      int err)
200 {
201 	if (user_mode(regs)) {
202 		WARN_ON(regs != current_pt_regs());
203 		current->thread.fault_address = 0;
204 		current->thread.fault_code = err;
205 
206 		arm64_force_sig_fault(signo, sicode, addr, str);
207 	} else {
208 		die(str, regs, err);
209 	}
210 }
211 
212 #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 
218 static 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 
228 static 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 
239 static 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
262 static void advance_itstate(struct pt_regs *regs)
263 {
264 }
265 #endif
266 
267 void 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 	 */
275 	if (user_mode(regs))
276 		user_fastforward_single_step(current);
277 
278 	if (compat_user_mode(regs))
279 		advance_itstate(regs);
280 	else
281 		regs->pstate &= ~PSR_BTYPE_MASK;
282 }
283 
284 static LIST_HEAD(undef_hook);
285 static DEFINE_RAW_SPINLOCK(undef_lock);
286 
287 void 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 
296 void 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 
305 static 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 
313 	if (!user_mode(regs)) {
314 		__le32 instr_le;
315 		if (get_kernel_nofault(instr_le, (__force __le32 *)pc))
316 			goto exit;
317 		instr = le32_to_cpu(instr_le);
318 	} else if (compat_thumb_mode(regs)) {
319 		/* 16-bit Thumb instruction */
320 		__le16 instr_le;
321 		if (get_user(instr_le, (__le16 __user *)pc))
322 			goto exit;
323 		instr = le16_to_cpu(instr_le);
324 		if (aarch32_insn_is_wide(instr)) {
325 			u32 instr2;
326 
327 			if (get_user(instr_le, (__le16 __user *)(pc + 2)))
328 				goto exit;
329 			instr2 = le16_to_cpu(instr_le);
330 			instr = (instr << 16) | instr2;
331 		}
332 	} else {
333 		/* 32-bit ARM instruction */
334 		__le32 instr_le;
335 		if (get_user(instr_le, (__le32 __user *)pc))
336 			goto exit;
337 		instr = le32_to_cpu(instr_le);
338 	}
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);
347 exit:
348 	return fn ? fn(regs, instr) : 1;
349 }
350 
351 void force_signal_inject(int signal, int code, unsigned long address, unsigned int err)
352 {
353 	const char *desc;
354 	struct pt_regs *regs = current_pt_regs();
355 
356 	if (WARN_ON(!user_mode(regs)))
357 		return;
358 
359 	switch (signal) {
360 	case SIGILL:
361 		desc = "undefined instruction";
362 		break;
363 	case SIGSEGV:
364 		desc = "illegal memory access";
365 		break;
366 	default:
367 		desc = "unknown or unrecoverable error";
368 		break;
369 	}
370 
371 	/* Force signals we don't understand to SIGKILL */
372 	if (WARN_ON(signal != SIGKILL &&
373 		    siginfo_layout(signal, code) != SIL_FAULT)) {
374 		signal = SIGKILL;
375 	}
376 
377 	arm64_notify_die(desc, regs, signal, code, (void __user *)address, err);
378 }
379 
380 /*
381  * Set up process info to signal segmentation fault - called on access error.
382  */
383 void arm64_notify_segfault(unsigned long addr)
384 {
385 	int code;
386 
387 	mmap_read_lock(current->mm);
388 	if (find_vma(current->mm, addr) == NULL)
389 		code = SEGV_MAPERR;
390 	else
391 		code = SEGV_ACCERR;
392 	mmap_read_unlock(current->mm);
393 
394 	force_signal_inject(SIGSEGV, code, addr, 0);
395 }
396 
397 void do_undefinstr(struct pt_regs *regs)
398 {
399 	/* check for AArch32 breakpoint instructions */
400 	if (!aarch32_break_handler(regs))
401 		return;
402 
403 	if (call_undef_hook(regs) == 0)
404 		return;
405 
406 	BUG_ON(!user_mode(regs));
407 	force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
408 }
409 NOKPROBE_SYMBOL(do_undefinstr);
410 
411 void do_bti(struct pt_regs *regs)
412 {
413 	BUG_ON(!user_mode(regs));
414 	force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
415 }
416 NOKPROBE_SYMBOL(do_bti);
417 
418 void 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 }
427 NOKPROBE_SYMBOL(do_ptrauth_fault);
428 
429 #define __user_cache_maint(insn, address, res)			\
430 	if (address >= user_addr_max()) {			\
431 		res = -EFAULT;					\
432 	} else {						\
433 		uaccess_ttbr0_enable();				\
434 		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)				\
445 			: "r" (address), "i" (-EFAULT));	\
446 		uaccess_ttbr0_disable();			\
447 	}
448 
449 static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
450 {
451 	unsigned long address;
452 	int rt = ESR_ELx_SYS64_ISS_RT(esr);
453 	int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
454 	int ret = 0;
455 
456 	address = untagged_addr(pt_regs_read_reg(regs, rt));
457 
458 	switch (crm) {
459 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAU:	/* DC CVAU, gets promoted */
460 		__user_cache_maint("dc civac", address, ret);
461 		break;
462 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAC:	/* DC CVAC, gets promoted */
463 		__user_cache_maint("dc civac", address, ret);
464 		break;
465 	case ESR_ELx_SYS64_ISS_CRM_DC_CVADP:	/* DC CVADP */
466 		__user_cache_maint("sys 3, c7, c13, 1", address, ret);
467 		break;
468 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAP:	/* DC CVAP */
469 		__user_cache_maint("sys 3, c7, c12, 1", address, ret);
470 		break;
471 	case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC:	/* DC CIVAC */
472 		__user_cache_maint("dc civac", address, ret);
473 		break;
474 	case ESR_ELx_SYS64_ISS_CRM_IC_IVAU:	/* IC IVAU */
475 		__user_cache_maint("ic ivau", address, ret);
476 		break;
477 	default:
478 		force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
479 		return;
480 	}
481 
482 	if (ret)
483 		arm64_notify_segfault(address);
484 	else
485 		arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
486 }
487 
488 static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
489 {
490 	int rt = ESR_ELx_SYS64_ISS_RT(esr);
491 	unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
492 
493 	if (cpus_have_const_cap(ARM64_WORKAROUND_1542419)) {
494 		/* Hide DIC so that we can trap the unnecessary maintenance...*/
495 		val &= ~BIT(CTR_DIC_SHIFT);
496 
497 		/* ... and fake IminLine to reduce the number of traps. */
498 		val &= ~CTR_IMINLINE_MASK;
499 		val |= (PAGE_SHIFT - 2) & CTR_IMINLINE_MASK;
500 	}
501 
502 	pt_regs_write_reg(regs, rt, val);
503 
504 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
505 }
506 
507 static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
508 {
509 	int rt = ESR_ELx_SYS64_ISS_RT(esr);
510 
511 	pt_regs_write_reg(regs, rt, arch_timer_read_counter());
512 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
513 }
514 
515 static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
516 {
517 	int rt = ESR_ELx_SYS64_ISS_RT(esr);
518 
519 	pt_regs_write_reg(regs, rt, arch_timer_get_rate());
520 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
521 }
522 
523 static void mrs_handler(unsigned int esr, struct pt_regs *regs)
524 {
525 	u32 sysreg, rt;
526 
527 	rt = ESR_ELx_SYS64_ISS_RT(esr);
528 	sysreg = esr_sys64_to_sysreg(esr);
529 
530 	if (do_emulate_mrs(regs, sysreg, rt) != 0)
531 		force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
532 }
533 
534 static void wfi_handler(unsigned int esr, struct pt_regs *regs)
535 {
536 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
537 }
538 
539 struct sys64_hook {
540 	unsigned int esr_mask;
541 	unsigned int esr_val;
542 	void (*handler)(unsigned int esr, struct pt_regs *regs);
543 };
544 
545 static const struct sys64_hook sys64_hooks[] = {
546 	{
547 		.esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
548 		.esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
549 		.handler = user_cache_maint_handler,
550 	},
551 	{
552 		/* Trap read access to CTR_EL0 */
553 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
554 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
555 		.handler = ctr_read_handler,
556 	},
557 	{
558 		/* Trap read access to CNTVCT_EL0 */
559 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
560 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
561 		.handler = cntvct_read_handler,
562 	},
563 	{
564 		/* Trap read access to CNTFRQ_EL0 */
565 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
566 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
567 		.handler = cntfrq_read_handler,
568 	},
569 	{
570 		/* Trap read access to CPUID registers */
571 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
572 		.esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
573 		.handler = mrs_handler,
574 	},
575 	{
576 		/* Trap WFI instructions executed in userspace */
577 		.esr_mask = ESR_ELx_WFx_MASK,
578 		.esr_val = ESR_ELx_WFx_WFI_VAL,
579 		.handler = wfi_handler,
580 	},
581 	{},
582 };
583 
584 #ifdef CONFIG_COMPAT
585 static bool cp15_cond_valid(unsigned int esr, struct pt_regs *regs)
586 {
587 	int cond;
588 
589 	/* Only a T32 instruction can trap without CV being set */
590 	if (!(esr & ESR_ELx_CV)) {
591 		u32 it;
592 
593 		it = compat_get_it_state(regs);
594 		if (!it)
595 			return true;
596 
597 		cond = it >> 4;
598 	} else {
599 		cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
600 	}
601 
602 	return aarch32_opcode_cond_checks[cond](regs->pstate);
603 }
604 
605 static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
606 {
607 	int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
608 
609 	pt_regs_write_reg(regs, reg, arch_timer_get_rate());
610 	arm64_skip_faulting_instruction(regs, 4);
611 }
612 
613 static const struct sys64_hook cp15_32_hooks[] = {
614 	{
615 		.esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
616 		.esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
617 		.handler = compat_cntfrq_read_handler,
618 	},
619 	{},
620 };
621 
622 static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
623 {
624 	int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
625 	int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
626 	u64 val = arch_timer_read_counter();
627 
628 	pt_regs_write_reg(regs, rt, lower_32_bits(val));
629 	pt_regs_write_reg(regs, rt2, upper_32_bits(val));
630 	arm64_skip_faulting_instruction(regs, 4);
631 }
632 
633 static const struct sys64_hook cp15_64_hooks[] = {
634 	{
635 		.esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
636 		.esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
637 		.handler = compat_cntvct_read_handler,
638 	},
639 	{},
640 };
641 
642 void do_cp15instr(unsigned int esr, struct pt_regs *regs)
643 {
644 	const struct sys64_hook *hook, *hook_base;
645 
646 	if (!cp15_cond_valid(esr, regs)) {
647 		/*
648 		 * There is no T16 variant of a CP access, so we
649 		 * always advance PC by 4 bytes.
650 		 */
651 		arm64_skip_faulting_instruction(regs, 4);
652 		return;
653 	}
654 
655 	switch (ESR_ELx_EC(esr)) {
656 	case ESR_ELx_EC_CP15_32:
657 		hook_base = cp15_32_hooks;
658 		break;
659 	case ESR_ELx_EC_CP15_64:
660 		hook_base = cp15_64_hooks;
661 		break;
662 	default:
663 		do_undefinstr(regs);
664 		return;
665 	}
666 
667 	for (hook = hook_base; hook->handler; hook++)
668 		if ((hook->esr_mask & esr) == hook->esr_val) {
669 			hook->handler(esr, regs);
670 			return;
671 		}
672 
673 	/*
674 	 * New cp15 instructions may previously have been undefined at
675 	 * EL0. Fall back to our usual undefined instruction handler
676 	 * so that we handle these consistently.
677 	 */
678 	do_undefinstr(regs);
679 }
680 NOKPROBE_SYMBOL(do_cp15instr);
681 #endif
682 
683 void do_sysinstr(unsigned int esr, struct pt_regs *regs)
684 {
685 	const struct sys64_hook *hook;
686 
687 	for (hook = sys64_hooks; hook->handler; hook++)
688 		if ((hook->esr_mask & esr) == hook->esr_val) {
689 			hook->handler(esr, regs);
690 			return;
691 		}
692 
693 	/*
694 	 * New SYS instructions may previously have been undefined at EL0. Fall
695 	 * back to our usual undefined instruction handler so that we handle
696 	 * these consistently.
697 	 */
698 	do_undefinstr(regs);
699 }
700 NOKPROBE_SYMBOL(do_sysinstr);
701 
702 static const char *esr_class_str[] = {
703 	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
704 	[ESR_ELx_EC_UNKNOWN]		= "Unknown/Uncategorized",
705 	[ESR_ELx_EC_WFx]		= "WFI/WFE",
706 	[ESR_ELx_EC_CP15_32]		= "CP15 MCR/MRC",
707 	[ESR_ELx_EC_CP15_64]		= "CP15 MCRR/MRRC",
708 	[ESR_ELx_EC_CP14_MR]		= "CP14 MCR/MRC",
709 	[ESR_ELx_EC_CP14_LS]		= "CP14 LDC/STC",
710 	[ESR_ELx_EC_FP_ASIMD]		= "ASIMD",
711 	[ESR_ELx_EC_CP10_ID]		= "CP10 MRC/VMRS",
712 	[ESR_ELx_EC_PAC]		= "PAC",
713 	[ESR_ELx_EC_CP14_64]		= "CP14 MCRR/MRRC",
714 	[ESR_ELx_EC_BTI]		= "BTI",
715 	[ESR_ELx_EC_ILL]		= "PSTATE.IL",
716 	[ESR_ELx_EC_SVC32]		= "SVC (AArch32)",
717 	[ESR_ELx_EC_HVC32]		= "HVC (AArch32)",
718 	[ESR_ELx_EC_SMC32]		= "SMC (AArch32)",
719 	[ESR_ELx_EC_SVC64]		= "SVC (AArch64)",
720 	[ESR_ELx_EC_HVC64]		= "HVC (AArch64)",
721 	[ESR_ELx_EC_SMC64]		= "SMC (AArch64)",
722 	[ESR_ELx_EC_SYS64]		= "MSR/MRS (AArch64)",
723 	[ESR_ELx_EC_SVE]		= "SVE",
724 	[ESR_ELx_EC_ERET]		= "ERET/ERETAA/ERETAB",
725 	[ESR_ELx_EC_FPAC]		= "FPAC",
726 	[ESR_ELx_EC_IMP_DEF]		= "EL3 IMP DEF",
727 	[ESR_ELx_EC_IABT_LOW]		= "IABT (lower EL)",
728 	[ESR_ELx_EC_IABT_CUR]		= "IABT (current EL)",
729 	[ESR_ELx_EC_PC_ALIGN]		= "PC Alignment",
730 	[ESR_ELx_EC_DABT_LOW]		= "DABT (lower EL)",
731 	[ESR_ELx_EC_DABT_CUR]		= "DABT (current EL)",
732 	[ESR_ELx_EC_SP_ALIGN]		= "SP Alignment",
733 	[ESR_ELx_EC_FP_EXC32]		= "FP (AArch32)",
734 	[ESR_ELx_EC_FP_EXC64]		= "FP (AArch64)",
735 	[ESR_ELx_EC_SERROR]		= "SError",
736 	[ESR_ELx_EC_BREAKPT_LOW]	= "Breakpoint (lower EL)",
737 	[ESR_ELx_EC_BREAKPT_CUR]	= "Breakpoint (current EL)",
738 	[ESR_ELx_EC_SOFTSTP_LOW]	= "Software Step (lower EL)",
739 	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
740 	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
741 	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
742 	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
743 	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
744 	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
745 };
746 
747 const char *esr_get_class_string(u32 esr)
748 {
749 	return esr_class_str[ESR_ELx_EC(esr)];
750 }
751 
752 /*
753  * bad_mode handles the impossible case in the exception vector. This is always
754  * fatal.
755  */
756 asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
757 {
758 	console_verbose();
759 
760 	pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
761 		handler[reason], smp_processor_id(), esr,
762 		esr_get_class_string(esr));
763 
764 	__show_regs(regs);
765 	local_daif_mask();
766 	panic("bad mode");
767 }
768 
769 /*
770  * bad_el0_sync handles unexpected, but potentially recoverable synchronous
771  * exceptions taken from EL0. Unlike bad_mode, this returns.
772  */
773 void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
774 {
775 	void __user *pc = (void __user *)instruction_pointer(regs);
776 
777 	current->thread.fault_address = 0;
778 	current->thread.fault_code = esr;
779 
780 	arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
781 			      "Bad EL0 synchronous exception");
782 }
783 
784 #ifdef CONFIG_VMAP_STACK
785 
786 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
787 	__aligned(16);
788 
789 asmlinkage void handle_bad_stack(struct pt_regs *regs)
790 {
791 	unsigned long tsk_stk = (unsigned long)current->stack;
792 	unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
793 	unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
794 	unsigned int esr = read_sysreg(esr_el1);
795 	unsigned long far = read_sysreg(far_el1);
796 
797 	console_verbose();
798 	pr_emerg("Insufficient stack space to handle exception!");
799 
800 	pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
801 	pr_emerg("FAR: 0x%016lx\n", far);
802 
803 	pr_emerg("Task stack:     [0x%016lx..0x%016lx]\n",
804 		 tsk_stk, tsk_stk + THREAD_SIZE);
805 	pr_emerg("IRQ stack:      [0x%016lx..0x%016lx]\n",
806 		 irq_stk, irq_stk + IRQ_STACK_SIZE);
807 	pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
808 		 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
809 
810 	__show_regs(regs);
811 
812 	/*
813 	 * We use nmi_panic to limit the potential for recusive overflows, and
814 	 * to get a better stack trace.
815 	 */
816 	nmi_panic(NULL, "kernel stack overflow");
817 	cpu_park_loop();
818 }
819 #endif
820 
821 void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
822 {
823 	console_verbose();
824 
825 	pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
826 		smp_processor_id(), esr, esr_get_class_string(esr));
827 	if (regs)
828 		__show_regs(regs);
829 
830 	nmi_panic(regs, "Asynchronous SError Interrupt");
831 
832 	cpu_park_loop();
833 	unreachable();
834 }
835 
836 bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
837 {
838 	u32 aet = arm64_ras_serror_get_severity(esr);
839 
840 	switch (aet) {
841 	case ESR_ELx_AET_CE:	/* corrected error */
842 	case ESR_ELx_AET_UEO:	/* restartable, not yet consumed */
843 		/*
844 		 * The CPU can make progress. We may take UEO again as
845 		 * a more severe error.
846 		 */
847 		return false;
848 
849 	case ESR_ELx_AET_UEU:	/* Uncorrected Unrecoverable */
850 	case ESR_ELx_AET_UER:	/* Uncorrected Recoverable */
851 		/*
852 		 * The CPU can't make progress. The exception may have
853 		 * been imprecise.
854 		 *
855 		 * Neoverse-N1 #1349291 means a non-KVM SError reported as
856 		 * Unrecoverable should be treated as Uncontainable. We
857 		 * call arm64_serror_panic() in both cases.
858 		 */
859 		return true;
860 
861 	case ESR_ELx_AET_UC:	/* Uncontainable or Uncategorized error */
862 	default:
863 		/* Error has been silently propagated */
864 		arm64_serror_panic(regs, esr);
865 	}
866 }
867 
868 asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
869 {
870 	nmi_enter();
871 
872 	/* non-RAS errors are not containable */
873 	if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
874 		arm64_serror_panic(regs, esr);
875 
876 	nmi_exit();
877 }
878 
879 asmlinkage void enter_from_user_mode(void)
880 {
881 	CT_WARN_ON(ct_state() != CONTEXT_USER);
882 	user_exit_irqoff();
883 }
884 NOKPROBE_SYMBOL(enter_from_user_mode);
885 
886 /* GENERIC_BUG traps */
887 
888 int is_valid_bugaddr(unsigned long addr)
889 {
890 	/*
891 	 * bug_handler() only called for BRK #BUG_BRK_IMM.
892 	 * So the answer is trivial -- any spurious instances with no
893 	 * bug table entry will be rejected by report_bug() and passed
894 	 * back to the debug-monitors code and handled as a fatal
895 	 * unexpected debug exception.
896 	 */
897 	return 1;
898 }
899 
900 static int bug_handler(struct pt_regs *regs, unsigned int esr)
901 {
902 	switch (report_bug(regs->pc, regs)) {
903 	case BUG_TRAP_TYPE_BUG:
904 		die("Oops - BUG", regs, 0);
905 		break;
906 
907 	case BUG_TRAP_TYPE_WARN:
908 		break;
909 
910 	default:
911 		/* unknown/unrecognised bug trap type */
912 		return DBG_HOOK_ERROR;
913 	}
914 
915 	/* If thread survives, skip over the BUG instruction and continue: */
916 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
917 	return DBG_HOOK_HANDLED;
918 }
919 
920 static struct break_hook bug_break_hook = {
921 	.fn = bug_handler,
922 	.imm = BUG_BRK_IMM,
923 };
924 
925 static int reserved_fault_handler(struct pt_regs *regs, unsigned int esr)
926 {
927 	pr_err("%s generated an invalid instruction at %pS!\n",
928 		in_bpf_jit(regs) ? "BPF JIT" : "Kernel text patching",
929 		(void *)instruction_pointer(regs));
930 
931 	/* We cannot handle this */
932 	return DBG_HOOK_ERROR;
933 }
934 
935 static struct break_hook fault_break_hook = {
936 	.fn = reserved_fault_handler,
937 	.imm = FAULT_BRK_IMM,
938 };
939 
940 #ifdef CONFIG_KASAN_SW_TAGS
941 
942 #define KASAN_ESR_RECOVER	0x20
943 #define KASAN_ESR_WRITE	0x10
944 #define KASAN_ESR_SIZE_MASK	0x0f
945 #define KASAN_ESR_SIZE(esr)	(1 << ((esr) & KASAN_ESR_SIZE_MASK))
946 
947 static int kasan_handler(struct pt_regs *regs, unsigned int esr)
948 {
949 	bool recover = esr & KASAN_ESR_RECOVER;
950 	bool write = esr & KASAN_ESR_WRITE;
951 	size_t size = KASAN_ESR_SIZE(esr);
952 	u64 addr = regs->regs[0];
953 	u64 pc = regs->pc;
954 
955 	kasan_report(addr, size, write, pc);
956 
957 	/*
958 	 * The instrumentation allows to control whether we can proceed after
959 	 * a crash was detected. This is done by passing the -recover flag to
960 	 * the compiler. Disabling recovery allows to generate more compact
961 	 * code.
962 	 *
963 	 * Unfortunately disabling recovery doesn't work for the kernel right
964 	 * now. KASAN reporting is disabled in some contexts (for example when
965 	 * the allocator accesses slab object metadata; this is controlled by
966 	 * current->kasan_depth). All these accesses are detected by the tool,
967 	 * even though the reports for them are not printed.
968 	 *
969 	 * This is something that might be fixed at some point in the future.
970 	 */
971 	if (!recover)
972 		die("Oops - KASAN", regs, 0);
973 
974 	/* If thread survives, skip over the brk instruction and continue: */
975 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
976 	return DBG_HOOK_HANDLED;
977 }
978 
979 static struct break_hook kasan_break_hook = {
980 	.fn	= kasan_handler,
981 	.imm	= KASAN_BRK_IMM,
982 	.mask	= KASAN_BRK_MASK,
983 };
984 #endif
985 
986 /*
987  * Initial handler for AArch64 BRK exceptions
988  * This handler only used until debug_traps_init().
989  */
990 int __init early_brk64(unsigned long addr, unsigned int esr,
991 		struct pt_regs *regs)
992 {
993 #ifdef CONFIG_KASAN_SW_TAGS
994 	unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
995 
996 	if ((comment & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
997 		return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
998 #endif
999 	return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
1000 }
1001 
1002 void __init trap_init(void)
1003 {
1004 	register_kernel_break_hook(&bug_break_hook);
1005 	register_kernel_break_hook(&fault_break_hook);
1006 #ifdef CONFIG_KASAN_SW_TAGS
1007 	register_kernel_break_hook(&kasan_break_hook);
1008 #endif
1009 	debug_traps_init();
1010 }
1011