xref: /linux/arch/arm64/kernel/traps.c (revision 5a4332062e9e71de8e78dc1b389d21e0dd44848b)
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/kallsyms.h>
13 #include <linux/kprobes.h>
14 #include <linux/spinlock.h>
15 #include <linux/uaccess.h>
16 #include <linux/hardirq.h>
17 #include <linux/kdebug.h>
18 #include <linux/module.h>
19 #include <linux/kexec.h>
20 #include <linux/delay.h>
21 #include <linux/efi.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 #include <linux/ubsan.h>
31 #include <linux/cfi.h>
32 
33 #include <asm/atomic.h>
34 #include <asm/bug.h>
35 #include <asm/cpufeature.h>
36 #include <asm/daifflags.h>
37 #include <asm/debug-monitors.h>
38 #include <asm/efi.h>
39 #include <asm/esr.h>
40 #include <asm/exception.h>
41 #include <asm/extable.h>
42 #include <asm/insn.h>
43 #include <asm/kprobes.h>
44 #include <asm/patching.h>
45 #include <asm/traps.h>
46 #include <asm/smp.h>
47 #include <asm/stack_pointer.h>
48 #include <asm/stacktrace.h>
49 #include <asm/system_misc.h>
50 #include <asm/sysreg.h>
51 
52 static bool __kprobes __check_eq(unsigned long pstate)
53 {
54 	return (pstate & PSR_Z_BIT) != 0;
55 }
56 
57 static bool __kprobes __check_ne(unsigned long pstate)
58 {
59 	return (pstate & PSR_Z_BIT) == 0;
60 }
61 
62 static bool __kprobes __check_cs(unsigned long pstate)
63 {
64 	return (pstate & PSR_C_BIT) != 0;
65 }
66 
67 static bool __kprobes __check_cc(unsigned long pstate)
68 {
69 	return (pstate & PSR_C_BIT) == 0;
70 }
71 
72 static bool __kprobes __check_mi(unsigned long pstate)
73 {
74 	return (pstate & PSR_N_BIT) != 0;
75 }
76 
77 static bool __kprobes __check_pl(unsigned long pstate)
78 {
79 	return (pstate & PSR_N_BIT) == 0;
80 }
81 
82 static bool __kprobes __check_vs(unsigned long pstate)
83 {
84 	return (pstate & PSR_V_BIT) != 0;
85 }
86 
87 static bool __kprobes __check_vc(unsigned long pstate)
88 {
89 	return (pstate & PSR_V_BIT) == 0;
90 }
91 
92 static bool __kprobes __check_hi(unsigned long pstate)
93 {
94 	pstate &= ~(pstate >> 1);	/* PSR_C_BIT &= ~PSR_Z_BIT */
95 	return (pstate & PSR_C_BIT) != 0;
96 }
97 
98 static bool __kprobes __check_ls(unsigned long pstate)
99 {
100 	pstate &= ~(pstate >> 1);	/* PSR_C_BIT &= ~PSR_Z_BIT */
101 	return (pstate & PSR_C_BIT) == 0;
102 }
103 
104 static bool __kprobes __check_ge(unsigned long pstate)
105 {
106 	pstate ^= (pstate << 3);	/* PSR_N_BIT ^= PSR_V_BIT */
107 	return (pstate & PSR_N_BIT) == 0;
108 }
109 
110 static bool __kprobes __check_lt(unsigned long pstate)
111 {
112 	pstate ^= (pstate << 3);	/* PSR_N_BIT ^= PSR_V_BIT */
113 	return (pstate & PSR_N_BIT) != 0;
114 }
115 
116 static bool __kprobes __check_gt(unsigned long pstate)
117 {
118 	/*PSR_N_BIT ^= PSR_V_BIT */
119 	unsigned long temp = pstate ^ (pstate << 3);
120 
121 	temp |= (pstate << 1);	/*PSR_N_BIT |= PSR_Z_BIT */
122 	return (temp & PSR_N_BIT) == 0;
123 }
124 
125 static bool __kprobes __check_le(unsigned long pstate)
126 {
127 	/*PSR_N_BIT ^= PSR_V_BIT */
128 	unsigned long temp = pstate ^ (pstate << 3);
129 
130 	temp |= (pstate << 1);	/*PSR_N_BIT |= PSR_Z_BIT */
131 	return (temp & PSR_N_BIT) != 0;
132 }
133 
134 static bool __kprobes __check_al(unsigned long pstate)
135 {
136 	return true;
137 }
138 
139 /*
140  * Note that the ARMv8 ARM calls condition code 0b1111 "nv", but states that
141  * it behaves identically to 0b1110 ("al").
142  */
143 pstate_check_t * const aarch32_opcode_cond_checks[16] = {
144 	__check_eq, __check_ne, __check_cs, __check_cc,
145 	__check_mi, __check_pl, __check_vs, __check_vc,
146 	__check_hi, __check_ls, __check_ge, __check_lt,
147 	__check_gt, __check_le, __check_al, __check_al
148 };
149 
150 int show_unhandled_signals = 0;
151 
152 static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
153 {
154 	unsigned long addr = instruction_pointer(regs);
155 	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
156 	int i;
157 
158 	if (user_mode(regs))
159 		return;
160 
161 	for (i = -4; i < 1; i++) {
162 		unsigned int val, bad;
163 
164 		bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
165 
166 		if (!bad)
167 			p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
168 		else
169 			p += sprintf(p, i == 0 ? "(????????) " : "???????? ");
170 	}
171 
172 	printk("%sCode: %s\n", lvl, str);
173 }
174 
175 #ifdef CONFIG_PREEMPT
176 #define S_PREEMPT " PREEMPT"
177 #elif defined(CONFIG_PREEMPT_RT)
178 #define S_PREEMPT " PREEMPT_RT"
179 #else
180 #define S_PREEMPT ""
181 #endif
182 
183 #define S_SMP " SMP"
184 
185 static int __die(const char *str, long err, struct pt_regs *regs)
186 {
187 	static int die_counter;
188 	int ret;
189 
190 	pr_emerg("Internal error: %s: %016lx [#%d]" S_PREEMPT S_SMP "\n",
191 		 str, err, ++die_counter);
192 
193 	/* trap and error numbers are mostly meaningless on ARM */
194 	ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
195 	if (ret == NOTIFY_STOP)
196 		return ret;
197 
198 	print_modules();
199 	show_regs(regs);
200 
201 	dump_kernel_instr(KERN_EMERG, regs);
202 
203 	return ret;
204 }
205 
206 static DEFINE_RAW_SPINLOCK(die_lock);
207 
208 /*
209  * This function is protected against re-entrancy.
210  */
211 void die(const char *str, struct pt_regs *regs, long err)
212 {
213 	int ret;
214 	unsigned long flags;
215 
216 	raw_spin_lock_irqsave(&die_lock, flags);
217 
218 	oops_enter();
219 
220 	console_verbose();
221 	bust_spinlocks(1);
222 	ret = __die(str, err, regs);
223 
224 	if (regs && kexec_should_crash(current))
225 		crash_kexec(regs);
226 
227 	bust_spinlocks(0);
228 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
229 	oops_exit();
230 
231 	if (in_interrupt())
232 		panic("%s: Fatal exception in interrupt", str);
233 	if (panic_on_oops)
234 		panic("%s: Fatal exception", str);
235 
236 	raw_spin_unlock_irqrestore(&die_lock, flags);
237 
238 	if (ret != NOTIFY_STOP)
239 		make_task_dead(SIGSEGV);
240 }
241 
242 static void arm64_show_signal(int signo, const char *str)
243 {
244 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
245 				      DEFAULT_RATELIMIT_BURST);
246 	struct task_struct *tsk = current;
247 	unsigned long esr = tsk->thread.fault_code;
248 	struct pt_regs *regs = task_pt_regs(tsk);
249 
250 	/* Leave if the signal won't be shown */
251 	if (!show_unhandled_signals ||
252 	    !unhandled_signal(tsk, signo) ||
253 	    !__ratelimit(&rs))
254 		return;
255 
256 	pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
257 	if (esr)
258 		pr_cont("%s, ESR 0x%016lx, ", esr_get_class_string(esr), esr);
259 
260 	pr_cont("%s", str);
261 	print_vma_addr(KERN_CONT " in ", regs->pc);
262 	pr_cont("\n");
263 	__show_regs(regs);
264 }
265 
266 void arm64_force_sig_fault(int signo, int code, unsigned long far,
267 			   const char *str)
268 {
269 	arm64_show_signal(signo, str);
270 	if (signo == SIGKILL)
271 		force_sig(SIGKILL);
272 	else
273 		force_sig_fault(signo, code, (void __user *)far);
274 }
275 
276 void arm64_force_sig_fault_pkey(unsigned long far, const char *str, int pkey)
277 {
278 	arm64_show_signal(SIGSEGV, str);
279 	force_sig_pkuerr((void __user *)far, pkey);
280 }
281 
282 void arm64_force_sig_mceerr(int code, unsigned long far, short lsb,
283 			    const char *str)
284 {
285 	arm64_show_signal(SIGBUS, str);
286 	force_sig_mceerr(code, (void __user *)far, lsb);
287 }
288 
289 void arm64_force_sig_ptrace_errno_trap(int errno, unsigned long far,
290 				       const char *str)
291 {
292 	arm64_show_signal(SIGTRAP, str);
293 	force_sig_ptrace_errno_trap(errno, (void __user *)far);
294 }
295 
296 void arm64_notify_die(const char *str, struct pt_regs *regs,
297 		      int signo, int sicode, unsigned long far,
298 		      unsigned long err)
299 {
300 	if (user_mode(regs)) {
301 		WARN_ON(regs != current_pt_regs());
302 		current->thread.fault_address = 0;
303 		current->thread.fault_code = err;
304 
305 		arm64_force_sig_fault(signo, sicode, far, str);
306 	} else {
307 		die(str, regs, err);
308 	}
309 }
310 
311 #ifdef CONFIG_COMPAT
312 #define PSTATE_IT_1_0_SHIFT	25
313 #define PSTATE_IT_1_0_MASK	(0x3 << PSTATE_IT_1_0_SHIFT)
314 #define PSTATE_IT_7_2_SHIFT	10
315 #define PSTATE_IT_7_2_MASK	(0x3f << PSTATE_IT_7_2_SHIFT)
316 
317 static u32 compat_get_it_state(struct pt_regs *regs)
318 {
319 	u32 it, pstate = regs->pstate;
320 
321 	it  = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
322 	it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
323 
324 	return it;
325 }
326 
327 static void compat_set_it_state(struct pt_regs *regs, u32 it)
328 {
329 	u32 pstate_it;
330 
331 	pstate_it  = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
332 	pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
333 
334 	regs->pstate &= ~PSR_AA32_IT_MASK;
335 	regs->pstate |= pstate_it;
336 }
337 
338 static void advance_itstate(struct pt_regs *regs)
339 {
340 	u32 it;
341 
342 	/* ARM mode */
343 	if (!(regs->pstate & PSR_AA32_T_BIT) ||
344 	    !(regs->pstate & PSR_AA32_IT_MASK))
345 		return;
346 
347 	it  = compat_get_it_state(regs);
348 
349 	/*
350 	 * If this is the last instruction of the block, wipe the IT
351 	 * state. Otherwise advance it.
352 	 */
353 	if (!(it & 7))
354 		it = 0;
355 	else
356 		it = (it & 0xe0) | ((it << 1) & 0x1f);
357 
358 	compat_set_it_state(regs, it);
359 }
360 #else
361 static void advance_itstate(struct pt_regs *regs)
362 {
363 }
364 #endif
365 
366 void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
367 {
368 	regs->pc += size;
369 
370 	/*
371 	 * If we were single stepping, we want to get the step exception after
372 	 * we return from the trap.
373 	 */
374 	if (user_mode(regs))
375 		user_fastforward_single_step(current);
376 
377 	if (compat_user_mode(regs))
378 		advance_itstate(regs);
379 	else
380 		regs->pstate &= ~PSR_BTYPE_MASK;
381 }
382 
383 static int user_insn_read(struct pt_regs *regs, u32 *insnp)
384 {
385 	u32 instr;
386 	unsigned long pc = instruction_pointer(regs);
387 
388 	if (compat_thumb_mode(regs)) {
389 		/* 16-bit Thumb instruction */
390 		__le16 instr_le;
391 		if (get_user(instr_le, (__le16 __user *)pc))
392 			return -EFAULT;
393 		instr = le16_to_cpu(instr_le);
394 		if (aarch32_insn_is_wide(instr)) {
395 			u32 instr2;
396 
397 			if (get_user(instr_le, (__le16 __user *)(pc + 2)))
398 				return -EFAULT;
399 			instr2 = le16_to_cpu(instr_le);
400 			instr = (instr << 16) | instr2;
401 		}
402 	} else {
403 		/* 32-bit ARM instruction */
404 		__le32 instr_le;
405 		if (get_user(instr_le, (__le32 __user *)pc))
406 			return -EFAULT;
407 		instr = le32_to_cpu(instr_le);
408 	}
409 
410 	*insnp = instr;
411 	return 0;
412 }
413 
414 void force_signal_inject(int signal, int code, unsigned long address, unsigned long err)
415 {
416 	const char *desc;
417 	struct pt_regs *regs = current_pt_regs();
418 
419 	if (WARN_ON(!user_mode(regs)))
420 		return;
421 
422 	switch (signal) {
423 	case SIGILL:
424 		desc = "undefined instruction";
425 		break;
426 	case SIGSEGV:
427 		desc = "illegal memory access";
428 		break;
429 	default:
430 		desc = "unknown or unrecoverable error";
431 		break;
432 	}
433 
434 	/* Force signals we don't understand to SIGKILL */
435 	if (WARN_ON(signal != SIGKILL &&
436 		    siginfo_layout(signal, code) != SIL_FAULT)) {
437 		signal = SIGKILL;
438 	}
439 
440 	arm64_notify_die(desc, regs, signal, code, address, err);
441 }
442 
443 /*
444  * Set up process info to signal segmentation fault - called on access error.
445  */
446 void arm64_notify_segfault(unsigned long addr)
447 {
448 	int code;
449 
450 	mmap_read_lock(current->mm);
451 	if (find_vma(current->mm, untagged_addr(addr)) == NULL)
452 		code = SEGV_MAPERR;
453 	else
454 		code = SEGV_ACCERR;
455 	mmap_read_unlock(current->mm);
456 
457 	force_signal_inject(SIGSEGV, code, addr, 0);
458 }
459 
460 void do_el0_undef(struct pt_regs *regs, unsigned long esr)
461 {
462 	u32 insn;
463 
464 	/* check for AArch32 breakpoint instructions */
465 	if (!aarch32_break_handler(regs))
466 		return;
467 
468 	if (user_insn_read(regs, &insn))
469 		goto out_err;
470 
471 	if (try_emulate_mrs(regs, insn))
472 		return;
473 
474 	if (try_emulate_armv8_deprecated(regs, insn))
475 		return;
476 
477 out_err:
478 	force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
479 }
480 
481 void do_el1_undef(struct pt_regs *regs, unsigned long esr)
482 {
483 	u32 insn;
484 
485 	if (aarch64_insn_read((void *)regs->pc, &insn))
486 		goto out_err;
487 
488 	if (try_emulate_el1_ssbs(regs, insn))
489 		return;
490 
491 out_err:
492 	die("Oops - Undefined instruction", regs, esr);
493 }
494 
495 void do_el0_bti(struct pt_regs *regs)
496 {
497 	force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
498 }
499 
500 void do_el1_bti(struct pt_regs *regs, unsigned long esr)
501 {
502 	if (efi_runtime_fixup_exception(regs, "BTI violation")) {
503 		regs->pstate &= ~PSR_BTYPE_MASK;
504 		return;
505 	}
506 	die("Oops - BTI", regs, esr);
507 }
508 
509 void do_el0_gcs(struct pt_regs *regs, unsigned long esr)
510 {
511 	force_signal_inject(SIGSEGV, SEGV_CPERR, regs->pc, 0);
512 }
513 
514 void do_el1_gcs(struct pt_regs *regs, unsigned long esr)
515 {
516 	die("Oops - GCS", regs, esr);
517 }
518 
519 void do_el0_fpac(struct pt_regs *regs, unsigned long esr)
520 {
521 	force_signal_inject(SIGILL, ILL_ILLOPN, regs->pc, esr);
522 }
523 
524 void do_el1_fpac(struct pt_regs *regs, unsigned long esr)
525 {
526 	/*
527 	 * Unexpected FPAC exception in the kernel: kill the task before it
528 	 * does any more harm.
529 	 */
530 	die("Oops - FPAC", regs, esr);
531 }
532 
533 void do_el0_mops(struct pt_regs *regs, unsigned long esr)
534 {
535 	arm64_mops_reset_regs(&regs->user_regs, esr);
536 
537 	/*
538 	 * If single stepping then finish the step before executing the
539 	 * prologue instruction.
540 	 */
541 	user_fastforward_single_step(current);
542 }
543 
544 #define __user_cache_maint(insn, address, res)			\
545 	if (address >= TASK_SIZE_MAX) {				\
546 		res = -EFAULT;					\
547 	} else {						\
548 		uaccess_ttbr0_enable();				\
549 		asm volatile (					\
550 			"1:	" insn ", %1\n"			\
551 			"	mov	%w0, #0\n"		\
552 			"2:\n"					\
553 			_ASM_EXTABLE_UACCESS_ERR(1b, 2b, %w0)	\
554 			: "=r" (res)				\
555 			: "r" (address));			\
556 		uaccess_ttbr0_disable();			\
557 	}
558 
559 static void user_cache_maint_handler(unsigned long esr, struct pt_regs *regs)
560 {
561 	unsigned long tagged_address, address;
562 	int rt = ESR_ELx_SYS64_ISS_RT(esr);
563 	int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
564 	int ret = 0;
565 
566 	tagged_address = pt_regs_read_reg(regs, rt);
567 	address = untagged_addr(tagged_address);
568 
569 	switch (crm) {
570 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAU:	/* DC CVAU, gets promoted */
571 		__user_cache_maint("dc civac", address, ret);
572 		break;
573 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAC:	/* DC CVAC, gets promoted */
574 		__user_cache_maint("dc civac", address, ret);
575 		break;
576 	case ESR_ELx_SYS64_ISS_CRM_DC_CVADP:	/* DC CVADP */
577 		__user_cache_maint("sys 3, c7, c13, 1", address, ret);
578 		break;
579 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAP:	/* DC CVAP */
580 		__user_cache_maint("sys 3, c7, c12, 1", address, ret);
581 		break;
582 	case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC:	/* DC CIVAC */
583 		__user_cache_maint("dc civac", address, ret);
584 		break;
585 	case ESR_ELx_SYS64_ISS_CRM_IC_IVAU:	/* IC IVAU */
586 		__user_cache_maint("ic ivau", address, ret);
587 		break;
588 	default:
589 		force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
590 		return;
591 	}
592 
593 	if (ret)
594 		arm64_notify_segfault(tagged_address);
595 	else
596 		arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
597 }
598 
599 static void ctr_read_handler(unsigned long esr, struct pt_regs *regs)
600 {
601 	int rt = ESR_ELx_SYS64_ISS_RT(esr);
602 	unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
603 
604 	if (cpus_have_final_cap(ARM64_WORKAROUND_1542419)) {
605 		/* Hide DIC so that we can trap the unnecessary maintenance...*/
606 		val &= ~BIT(CTR_EL0_DIC_SHIFT);
607 
608 		/* ... and fake IminLine to reduce the number of traps. */
609 		val &= ~CTR_EL0_IminLine_MASK;
610 		val |= (PAGE_SHIFT - 2) & CTR_EL0_IminLine_MASK;
611 	}
612 
613 	pt_regs_write_reg(regs, rt, val);
614 
615 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
616 }
617 
618 static void cntvct_read_handler(unsigned long esr, struct pt_regs *regs)
619 {
620 	if (test_thread_flag(TIF_TSC_SIGSEGV)) {
621 		force_sig(SIGSEGV);
622 	} else {
623 		int rt = ESR_ELx_SYS64_ISS_RT(esr);
624 
625 		pt_regs_write_reg(regs, rt, arch_timer_read_counter());
626 		arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
627 	}
628 }
629 
630 static void cntfrq_read_handler(unsigned long esr, struct pt_regs *regs)
631 {
632 	if (test_thread_flag(TIF_TSC_SIGSEGV)) {
633 		force_sig(SIGSEGV);
634 	} else {
635 		int rt = ESR_ELx_SYS64_ISS_RT(esr);
636 
637 		pt_regs_write_reg(regs, rt, arch_timer_get_rate());
638 		arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
639 	}
640 }
641 
642 static void mrs_handler(unsigned long esr, struct pt_regs *regs)
643 {
644 	u32 sysreg, rt;
645 
646 	rt = ESR_ELx_SYS64_ISS_RT(esr);
647 	sysreg = esr_sys64_to_sysreg(esr);
648 
649 	if (do_emulate_mrs(regs, sysreg, rt) != 0)
650 		force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
651 }
652 
653 static void wfi_handler(unsigned long esr, struct pt_regs *regs)
654 {
655 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
656 }
657 
658 struct sys64_hook {
659 	unsigned long esr_mask;
660 	unsigned long esr_val;
661 	void (*handler)(unsigned long esr, struct pt_regs *regs);
662 };
663 
664 static const struct sys64_hook sys64_hooks[] = {
665 	{
666 		.esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
667 		.esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
668 		.handler = user_cache_maint_handler,
669 	},
670 	{
671 		/* Trap read access to CTR_EL0 */
672 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
673 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
674 		.handler = ctr_read_handler,
675 	},
676 	{
677 		/* Trap read access to CNTVCT_EL0 */
678 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
679 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
680 		.handler = cntvct_read_handler,
681 	},
682 	{
683 		/* Trap read access to CNTVCTSS_EL0 */
684 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
685 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCTSS,
686 		.handler = cntvct_read_handler,
687 	},
688 	{
689 		/* Trap read access to CNTFRQ_EL0 */
690 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
691 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
692 		.handler = cntfrq_read_handler,
693 	},
694 	{
695 		/* Trap read access to CPUID registers */
696 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
697 		.esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
698 		.handler = mrs_handler,
699 	},
700 	{
701 		/* Trap WFI instructions executed in userspace */
702 		.esr_mask = ESR_ELx_WFx_MASK,
703 		.esr_val = ESR_ELx_WFx_WFI_VAL,
704 		.handler = wfi_handler,
705 	},
706 	{},
707 };
708 
709 #ifdef CONFIG_COMPAT
710 static bool cp15_cond_valid(unsigned long esr, struct pt_regs *regs)
711 {
712 	int cond;
713 
714 	/* Only a T32 instruction can trap without CV being set */
715 	if (!(esr & ESR_ELx_CV)) {
716 		u32 it;
717 
718 		it = compat_get_it_state(regs);
719 		if (!it)
720 			return true;
721 
722 		cond = it >> 4;
723 	} else {
724 		cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
725 	}
726 
727 	return aarch32_opcode_cond_checks[cond](regs->pstate);
728 }
729 
730 static void compat_cntfrq_read_handler(unsigned long esr, struct pt_regs *regs)
731 {
732 	int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
733 
734 	pt_regs_write_reg(regs, reg, arch_timer_get_rate());
735 	arm64_skip_faulting_instruction(regs, 4);
736 }
737 
738 static const struct sys64_hook cp15_32_hooks[] = {
739 	{
740 		.esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
741 		.esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
742 		.handler = compat_cntfrq_read_handler,
743 	},
744 	{},
745 };
746 
747 static void compat_cntvct_read_handler(unsigned long esr, struct pt_regs *regs)
748 {
749 	int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
750 	int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
751 	u64 val = arch_timer_read_counter();
752 
753 	pt_regs_write_reg(regs, rt, lower_32_bits(val));
754 	pt_regs_write_reg(regs, rt2, upper_32_bits(val));
755 	arm64_skip_faulting_instruction(regs, 4);
756 }
757 
758 static const struct sys64_hook cp15_64_hooks[] = {
759 	{
760 		.esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
761 		.esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
762 		.handler = compat_cntvct_read_handler,
763 	},
764 	{
765 		.esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
766 		.esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCTSS,
767 		.handler = compat_cntvct_read_handler,
768 	},
769 	{},
770 };
771 
772 void do_el0_cp15(unsigned long esr, struct pt_regs *regs)
773 {
774 	const struct sys64_hook *hook, *hook_base;
775 
776 	if (!cp15_cond_valid(esr, regs)) {
777 		/*
778 		 * There is no T16 variant of a CP access, so we
779 		 * always advance PC by 4 bytes.
780 		 */
781 		arm64_skip_faulting_instruction(regs, 4);
782 		return;
783 	}
784 
785 	switch (ESR_ELx_EC(esr)) {
786 	case ESR_ELx_EC_CP15_32:
787 		hook_base = cp15_32_hooks;
788 		break;
789 	case ESR_ELx_EC_CP15_64:
790 		hook_base = cp15_64_hooks;
791 		break;
792 	default:
793 		do_el0_undef(regs, esr);
794 		return;
795 	}
796 
797 	for (hook = hook_base; hook->handler; hook++)
798 		if ((hook->esr_mask & esr) == hook->esr_val) {
799 			hook->handler(esr, regs);
800 			return;
801 		}
802 
803 	/*
804 	 * New cp15 instructions may previously have been undefined at
805 	 * EL0. Fall back to our usual undefined instruction handler
806 	 * so that we handle these consistently.
807 	 */
808 	do_el0_undef(regs, esr);
809 }
810 #endif
811 
812 void do_el0_sys(unsigned long esr, struct pt_regs *regs)
813 {
814 	const struct sys64_hook *hook;
815 
816 	for (hook = sys64_hooks; hook->handler; hook++)
817 		if ((hook->esr_mask & esr) == hook->esr_val) {
818 			hook->handler(esr, regs);
819 			return;
820 		}
821 
822 	/*
823 	 * New SYS instructions may previously have been undefined at EL0. Fall
824 	 * back to our usual undefined instruction handler so that we handle
825 	 * these consistently.
826 	 */
827 	do_el0_undef(regs, esr);
828 }
829 
830 static const char *esr_class_str[] = {
831 	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
832 	[ESR_ELx_EC_UNKNOWN]		= "Unknown/Uncategorized",
833 	[ESR_ELx_EC_WFx]		= "WFI/WFE",
834 	[ESR_ELx_EC_CP15_32]		= "CP15 MCR/MRC",
835 	[ESR_ELx_EC_CP15_64]		= "CP15 MCRR/MRRC",
836 	[ESR_ELx_EC_CP14_MR]		= "CP14 MCR/MRC",
837 	[ESR_ELx_EC_CP14_LS]		= "CP14 LDC/STC",
838 	[ESR_ELx_EC_FP_ASIMD]		= "ASIMD",
839 	[ESR_ELx_EC_CP10_ID]		= "CP10 MRC/VMRS",
840 	[ESR_ELx_EC_PAC]		= "PAC",
841 	[ESR_ELx_EC_CP14_64]		= "CP14 MCRR/MRRC",
842 	[ESR_ELx_EC_BTI]		= "BTI",
843 	[ESR_ELx_EC_ILL]		= "PSTATE.IL",
844 	[ESR_ELx_EC_SVC32]		= "SVC (AArch32)",
845 	[ESR_ELx_EC_HVC32]		= "HVC (AArch32)",
846 	[ESR_ELx_EC_SMC32]		= "SMC (AArch32)",
847 	[ESR_ELx_EC_SVC64]		= "SVC (AArch64)",
848 	[ESR_ELx_EC_HVC64]		= "HVC (AArch64)",
849 	[ESR_ELx_EC_SMC64]		= "SMC (AArch64)",
850 	[ESR_ELx_EC_SYS64]		= "MSR/MRS (AArch64)",
851 	[ESR_ELx_EC_SVE]		= "SVE",
852 	[ESR_ELx_EC_ERET]		= "ERET/ERETAA/ERETAB",
853 	[ESR_ELx_EC_FPAC]		= "FPAC",
854 	[ESR_ELx_EC_SME]		= "SME",
855 	[ESR_ELx_EC_IMP_DEF]		= "EL3 IMP DEF",
856 	[ESR_ELx_EC_IABT_LOW]		= "IABT (lower EL)",
857 	[ESR_ELx_EC_IABT_CUR]		= "IABT (current EL)",
858 	[ESR_ELx_EC_PC_ALIGN]		= "PC Alignment",
859 	[ESR_ELx_EC_DABT_LOW]		= "DABT (lower EL)",
860 	[ESR_ELx_EC_DABT_CUR]		= "DABT (current EL)",
861 	[ESR_ELx_EC_SP_ALIGN]		= "SP Alignment",
862 	[ESR_ELx_EC_MOPS]		= "MOPS",
863 	[ESR_ELx_EC_FP_EXC32]		= "FP (AArch32)",
864 	[ESR_ELx_EC_FP_EXC64]		= "FP (AArch64)",
865 	[ESR_ELx_EC_GCS]		= "Guarded Control Stack",
866 	[ESR_ELx_EC_SERROR]		= "SError",
867 	[ESR_ELx_EC_BREAKPT_LOW]	= "Breakpoint (lower EL)",
868 	[ESR_ELx_EC_BREAKPT_CUR]	= "Breakpoint (current EL)",
869 	[ESR_ELx_EC_SOFTSTP_LOW]	= "Software Step (lower EL)",
870 	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
871 	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
872 	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
873 	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
874 	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
875 	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
876 };
877 
878 const char *esr_get_class_string(unsigned long esr)
879 {
880 	return esr_class_str[ESR_ELx_EC(esr)];
881 }
882 
883 /*
884  * bad_el0_sync handles unexpected, but potentially recoverable synchronous
885  * exceptions taken from EL0.
886  */
887 void bad_el0_sync(struct pt_regs *regs, int reason, unsigned long esr)
888 {
889 	unsigned long pc = instruction_pointer(regs);
890 
891 	current->thread.fault_address = 0;
892 	current->thread.fault_code = esr;
893 
894 	arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
895 			      "Bad EL0 synchronous exception");
896 }
897 
898 #ifdef CONFIG_VMAP_STACK
899 
900 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
901 	__aligned(16);
902 
903 void __noreturn panic_bad_stack(struct pt_regs *regs, unsigned long esr, unsigned long far)
904 {
905 	unsigned long tsk_stk = (unsigned long)current->stack;
906 	unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
907 	unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
908 
909 	console_verbose();
910 	pr_emerg("Insufficient stack space to handle exception!");
911 
912 	pr_emerg("ESR: 0x%016lx -- %s\n", esr, esr_get_class_string(esr));
913 	pr_emerg("FAR: 0x%016lx\n", far);
914 
915 	pr_emerg("Task stack:     [0x%016lx..0x%016lx]\n",
916 		 tsk_stk, tsk_stk + THREAD_SIZE);
917 	pr_emerg("IRQ stack:      [0x%016lx..0x%016lx]\n",
918 		 irq_stk, irq_stk + IRQ_STACK_SIZE);
919 	pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
920 		 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
921 
922 	__show_regs(regs);
923 
924 	/*
925 	 * We use nmi_panic to limit the potential for recusive overflows, and
926 	 * to get a better stack trace.
927 	 */
928 	nmi_panic(NULL, "kernel stack overflow");
929 	cpu_park_loop();
930 }
931 #endif
932 
933 void __noreturn arm64_serror_panic(struct pt_regs *regs, unsigned long esr)
934 {
935 	console_verbose();
936 
937 	pr_crit("SError Interrupt on CPU%d, code 0x%016lx -- %s\n",
938 		smp_processor_id(), esr, esr_get_class_string(esr));
939 	if (regs)
940 		__show_regs(regs);
941 
942 	nmi_panic(regs, "Asynchronous SError Interrupt");
943 
944 	cpu_park_loop();
945 }
946 
947 bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned long esr)
948 {
949 	unsigned long aet = arm64_ras_serror_get_severity(esr);
950 
951 	switch (aet) {
952 	case ESR_ELx_AET_CE:	/* corrected error */
953 	case ESR_ELx_AET_UEO:	/* restartable, not yet consumed */
954 		/*
955 		 * The CPU can make progress. We may take UEO again as
956 		 * a more severe error.
957 		 */
958 		return false;
959 
960 	case ESR_ELx_AET_UEU:	/* Uncorrected Unrecoverable */
961 	case ESR_ELx_AET_UER:	/* Uncorrected Recoverable */
962 		/*
963 		 * The CPU can't make progress. The exception may have
964 		 * been imprecise.
965 		 *
966 		 * Neoverse-N1 #1349291 means a non-KVM SError reported as
967 		 * Unrecoverable should be treated as Uncontainable. We
968 		 * call arm64_serror_panic() in both cases.
969 		 */
970 		return true;
971 
972 	case ESR_ELx_AET_UC:	/* Uncontainable or Uncategorized error */
973 	default:
974 		/* Error has been silently propagated */
975 		arm64_serror_panic(regs, esr);
976 	}
977 }
978 
979 void do_serror(struct pt_regs *regs, unsigned long esr)
980 {
981 	/* non-RAS errors are not containable */
982 	if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
983 		arm64_serror_panic(regs, esr);
984 }
985 
986 /* GENERIC_BUG traps */
987 #ifdef CONFIG_GENERIC_BUG
988 int is_valid_bugaddr(unsigned long addr)
989 {
990 	/*
991 	 * bug_handler() only called for BRK #BUG_BRK_IMM.
992 	 * So the answer is trivial -- any spurious instances with no
993 	 * bug table entry will be rejected by report_bug() and passed
994 	 * back to the debug-monitors code and handled as a fatal
995 	 * unexpected debug exception.
996 	 */
997 	return 1;
998 }
999 #endif
1000 
1001 static int bug_handler(struct pt_regs *regs, unsigned long esr)
1002 {
1003 	switch (report_bug(regs->pc, regs)) {
1004 	case BUG_TRAP_TYPE_BUG:
1005 		die("Oops - BUG", regs, esr);
1006 		break;
1007 
1008 	case BUG_TRAP_TYPE_WARN:
1009 		break;
1010 
1011 	default:
1012 		/* unknown/unrecognised bug trap type */
1013 		return DBG_HOOK_ERROR;
1014 	}
1015 
1016 	/* If thread survives, skip over the BUG instruction and continue: */
1017 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
1018 	return DBG_HOOK_HANDLED;
1019 }
1020 
1021 static struct break_hook bug_break_hook = {
1022 	.fn = bug_handler,
1023 	.imm = BUG_BRK_IMM,
1024 };
1025 
1026 #ifdef CONFIG_CFI_CLANG
1027 static int cfi_handler(struct pt_regs *regs, unsigned long esr)
1028 {
1029 	unsigned long target;
1030 	u32 type;
1031 
1032 	target = pt_regs_read_reg(regs, FIELD_GET(CFI_BRK_IMM_TARGET, esr));
1033 	type = (u32)pt_regs_read_reg(regs, FIELD_GET(CFI_BRK_IMM_TYPE, esr));
1034 
1035 	switch (report_cfi_failure(regs, regs->pc, &target, type)) {
1036 	case BUG_TRAP_TYPE_BUG:
1037 		die("Oops - CFI", regs, esr);
1038 		break;
1039 
1040 	case BUG_TRAP_TYPE_WARN:
1041 		break;
1042 
1043 	default:
1044 		return DBG_HOOK_ERROR;
1045 	}
1046 
1047 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
1048 	return DBG_HOOK_HANDLED;
1049 }
1050 
1051 static struct break_hook cfi_break_hook = {
1052 	.fn = cfi_handler,
1053 	.imm = CFI_BRK_IMM_BASE,
1054 	.mask = CFI_BRK_IMM_MASK,
1055 };
1056 #endif /* CONFIG_CFI_CLANG */
1057 
1058 static int reserved_fault_handler(struct pt_regs *regs, unsigned long esr)
1059 {
1060 	pr_err("%s generated an invalid instruction at %pS!\n",
1061 		"Kernel text patching",
1062 		(void *)instruction_pointer(regs));
1063 
1064 	/* We cannot handle this */
1065 	return DBG_HOOK_ERROR;
1066 }
1067 
1068 static struct break_hook fault_break_hook = {
1069 	.fn = reserved_fault_handler,
1070 	.imm = FAULT_BRK_IMM,
1071 };
1072 
1073 #ifdef CONFIG_KASAN_SW_TAGS
1074 
1075 #define KASAN_ESR_RECOVER	0x20
1076 #define KASAN_ESR_WRITE	0x10
1077 #define KASAN_ESR_SIZE_MASK	0x0f
1078 #define KASAN_ESR_SIZE(esr)	(1 << ((esr) & KASAN_ESR_SIZE_MASK))
1079 
1080 static int kasan_handler(struct pt_regs *regs, unsigned long esr)
1081 {
1082 	bool recover = esr & KASAN_ESR_RECOVER;
1083 	bool write = esr & KASAN_ESR_WRITE;
1084 	size_t size = KASAN_ESR_SIZE(esr);
1085 	void *addr = (void *)regs->regs[0];
1086 	u64 pc = regs->pc;
1087 
1088 	kasan_report(addr, size, write, pc);
1089 
1090 	/*
1091 	 * The instrumentation allows to control whether we can proceed after
1092 	 * a crash was detected. This is done by passing the -recover flag to
1093 	 * the compiler. Disabling recovery allows to generate more compact
1094 	 * code.
1095 	 *
1096 	 * Unfortunately disabling recovery doesn't work for the kernel right
1097 	 * now. KASAN reporting is disabled in some contexts (for example when
1098 	 * the allocator accesses slab object metadata; this is controlled by
1099 	 * current->kasan_depth). All these accesses are detected by the tool,
1100 	 * even though the reports for them are not printed.
1101 	 *
1102 	 * This is something that might be fixed at some point in the future.
1103 	 */
1104 	if (!recover)
1105 		die("Oops - KASAN", regs, esr);
1106 
1107 	/* If thread survives, skip over the brk instruction and continue: */
1108 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
1109 	return DBG_HOOK_HANDLED;
1110 }
1111 
1112 static struct break_hook kasan_break_hook = {
1113 	.fn	= kasan_handler,
1114 	.imm	= KASAN_BRK_IMM,
1115 	.mask	= KASAN_BRK_MASK,
1116 };
1117 #endif
1118 
1119 #ifdef CONFIG_UBSAN_TRAP
1120 static int ubsan_handler(struct pt_regs *regs, unsigned long esr)
1121 {
1122 	die(report_ubsan_failure(regs, esr & UBSAN_BRK_MASK), regs, esr);
1123 	return DBG_HOOK_HANDLED;
1124 }
1125 
1126 static struct break_hook ubsan_break_hook = {
1127 	.fn	= ubsan_handler,
1128 	.imm	= UBSAN_BRK_IMM,
1129 	.mask	= UBSAN_BRK_MASK,
1130 };
1131 #endif
1132 
1133 /*
1134  * Initial handler for AArch64 BRK exceptions
1135  * This handler only used until debug_traps_init().
1136  */
1137 int __init early_brk64(unsigned long addr, unsigned long esr,
1138 		struct pt_regs *regs)
1139 {
1140 #ifdef CONFIG_CFI_CLANG
1141 	if (esr_is_cfi_brk(esr))
1142 		return cfi_handler(regs, esr) != DBG_HOOK_HANDLED;
1143 #endif
1144 #ifdef CONFIG_KASAN_SW_TAGS
1145 	if ((esr_brk_comment(esr) & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
1146 		return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
1147 #endif
1148 #ifdef CONFIG_UBSAN_TRAP
1149 	if ((esr_brk_comment(esr) & ~UBSAN_BRK_MASK) == UBSAN_BRK_IMM)
1150 		return ubsan_handler(regs, esr) != DBG_HOOK_HANDLED;
1151 #endif
1152 	return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
1153 }
1154 
1155 void __init trap_init(void)
1156 {
1157 	register_kernel_break_hook(&bug_break_hook);
1158 #ifdef CONFIG_CFI_CLANG
1159 	register_kernel_break_hook(&cfi_break_hook);
1160 #endif
1161 	register_kernel_break_hook(&fault_break_hook);
1162 #ifdef CONFIG_KASAN_SW_TAGS
1163 	register_kernel_break_hook(&kasan_break_hook);
1164 #endif
1165 #ifdef CONFIG_UBSAN_TRAP
1166 	register_kernel_break_hook(&ubsan_break_hook);
1167 #endif
1168 	debug_traps_init();
1169 }
1170