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