• 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 int 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%08x, ", 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,int err)291 void arm64_notify_die(const char *str, struct pt_regs *regs,
292 		      int signo, int sicode, unsigned long far,
293 		      int 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 int err)409 void force_signal_inject(int signal, int code, unsigned long address, unsigned int 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 	trace_android_rvh_do_ptrauth_fault(regs, esr);
505 	force_signal_inject(SIGILL, ILL_ILLOPN, regs->pc, esr);
506 }
507 
do_el1_fpac(struct pt_regs * regs,unsigned long esr)508 void do_el1_fpac(struct pt_regs *regs, unsigned long esr)
509 {
510 	/*
511 	 * Unexpected FPAC exception in the kernel: kill the task before it
512 	 * does any more harm.
513 	 */
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 int esr,struct pt_regs * regs)537 static void user_cache_maint_handler(unsigned int 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 int esr,struct pt_regs * regs)577 static void ctr_read_handler(unsigned int 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 	pt_regs_write_reg(regs, rt, val);
583 
584 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
585 }
586 
cntvct_read_handler(unsigned int esr,struct pt_regs * regs)587 static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
588 {
589 	int rt = ESR_ELx_SYS64_ISS_RT(esr);
590 
591 	pt_regs_write_reg(regs, rt, arch_timer_read_counter());
592 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
593 }
594 
cntfrq_read_handler(unsigned int esr,struct pt_regs * regs)595 static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
596 {
597 	int rt = ESR_ELx_SYS64_ISS_RT(esr);
598 
599 	pt_regs_write_reg(regs, rt, arch_timer_get_rate());
600 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
601 }
602 
mrs_handler(unsigned int esr,struct pt_regs * regs)603 static void mrs_handler(unsigned int esr, struct pt_regs *regs)
604 {
605 	u32 sysreg, rt;
606 
607 	rt = ESR_ELx_SYS64_ISS_RT(esr);
608 	sysreg = esr_sys64_to_sysreg(esr);
609 
610 	if (do_emulate_mrs(regs, sysreg, rt) != 0)
611 		force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
612 }
613 
wfi_handler(unsigned int esr,struct pt_regs * regs)614 static void wfi_handler(unsigned int esr, struct pt_regs *regs)
615 {
616 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
617 }
618 
619 struct sys64_hook {
620 	unsigned int esr_mask;
621 	unsigned int esr_val;
622 	void (*handler)(unsigned int esr, struct pt_regs *regs);
623 };
624 
625 static const struct sys64_hook sys64_hooks[] = {
626 	{
627 		.esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
628 		.esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
629 		.handler = user_cache_maint_handler,
630 	},
631 	{
632 		/* Trap read access to CTR_EL0 */
633 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
634 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
635 		.handler = ctr_read_handler,
636 	},
637 	{
638 		/* Trap read access to CNTVCT_EL0 */
639 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
640 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
641 		.handler = cntvct_read_handler,
642 	},
643 	{
644 		/* Trap read access to CNTFRQ_EL0 */
645 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
646 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
647 		.handler = cntfrq_read_handler,
648 	},
649 	{
650 		/* Trap read access to CPUID registers */
651 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
652 		.esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
653 		.handler = mrs_handler,
654 	},
655 	{
656 		/* Trap WFI instructions executed in userspace */
657 		.esr_mask = ESR_ELx_WFx_MASK,
658 		.esr_val = ESR_ELx_WFx_WFI_VAL,
659 		.handler = wfi_handler,
660 	},
661 	{},
662 };
663 
664 #ifdef CONFIG_COMPAT
cp15_cond_valid(unsigned int esr,struct pt_regs * regs)665 static bool cp15_cond_valid(unsigned int esr, struct pt_regs *regs)
666 {
667 	int cond;
668 
669 	/* Only a T32 instruction can trap without CV being set */
670 	if (!(esr & ESR_ELx_CV)) {
671 		u32 it;
672 
673 		it = compat_get_it_state(regs);
674 		if (!it)
675 			return true;
676 
677 		cond = it >> 4;
678 	} else {
679 		cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
680 	}
681 
682 	return aarch32_opcode_cond_checks[cond](regs->pstate);
683 }
684 
compat_cntfrq_read_handler(unsigned int esr,struct pt_regs * regs)685 static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
686 {
687 	int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
688 
689 	pt_regs_write_reg(regs, reg, arch_timer_get_rate());
690 	arm64_skip_faulting_instruction(regs, 4);
691 }
692 
693 static const struct sys64_hook cp15_32_hooks[] = {
694 	{
695 		.esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
696 		.esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
697 		.handler = compat_cntfrq_read_handler,
698 	},
699 	{},
700 };
701 
compat_cntvct_read_handler(unsigned int esr,struct pt_regs * regs)702 static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
703 {
704 	int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
705 	int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
706 	u64 val = arch_timer_read_counter();
707 
708 	pt_regs_write_reg(regs, rt, lower_32_bits(val));
709 	pt_regs_write_reg(regs, rt2, upper_32_bits(val));
710 	arm64_skip_faulting_instruction(regs, 4);
711 }
712 
713 static const struct sys64_hook cp15_64_hooks[] = {
714 	{
715 		.esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
716 		.esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
717 		.handler = compat_cntvct_read_handler,
718 	},
719 	{},
720 };
721 
do_el0_cp15(unsigned long esr,struct pt_regs * regs)722 void do_el0_cp15(unsigned long esr, struct pt_regs *regs)
723 {
724 	const struct sys64_hook *hook, *hook_base;
725 
726 	if (!cp15_cond_valid(esr, regs)) {
727 		/*
728 		 * There is no T16 variant of a CP access, so we
729 		 * always advance PC by 4 bytes.
730 		 */
731 		arm64_skip_faulting_instruction(regs, 4);
732 		return;
733 	}
734 
735 	switch (ESR_ELx_EC(esr)) {
736 	case ESR_ELx_EC_CP15_32:
737 		hook_base = cp15_32_hooks;
738 		break;
739 	case ESR_ELx_EC_CP15_64:
740 		hook_base = cp15_64_hooks;
741 		break;
742 	default:
743 		do_el0_undef(regs, esr);
744 		return;
745 	}
746 
747 	for (hook = hook_base; hook->handler; hook++)
748 		if ((hook->esr_mask & esr) == hook->esr_val) {
749 			hook->handler(esr, regs);
750 			return;
751 		}
752 
753 	/*
754 	 * New cp15 instructions may previously have been undefined at
755 	 * EL0. Fall back to our usual undefined instruction handler
756 	 * so that we handle these consistently.
757 	 */
758 	do_el0_undef(regs, esr);
759 }
760 #endif
761 
do_el0_sys(unsigned long esr,struct pt_regs * regs)762 void do_el0_sys(unsigned long esr, struct pt_regs *regs)
763 {
764 	const struct sys64_hook *hook;
765 
766 	for (hook = sys64_hooks; hook->handler; hook++)
767 		if ((hook->esr_mask & esr) == hook->esr_val) {
768 			hook->handler(esr, regs);
769 			return;
770 		}
771 
772 	/*
773 	 * New SYS instructions may previously have been undefined at EL0. Fall
774 	 * back to our usual undefined instruction handler so that we handle
775 	 * these consistently.
776 	 */
777 	do_el0_undef(regs, esr);
778 }
779 
780 static const char *esr_class_str[] = {
781 	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
782 	[ESR_ELx_EC_UNKNOWN]		= "Unknown/Uncategorized",
783 	[ESR_ELx_EC_WFx]		= "WFI/WFE",
784 	[ESR_ELx_EC_CP15_32]		= "CP15 MCR/MRC",
785 	[ESR_ELx_EC_CP15_64]		= "CP15 MCRR/MRRC",
786 	[ESR_ELx_EC_CP14_MR]		= "CP14 MCR/MRC",
787 	[ESR_ELx_EC_CP14_LS]		= "CP14 LDC/STC",
788 	[ESR_ELx_EC_FP_ASIMD]		= "ASIMD",
789 	[ESR_ELx_EC_CP10_ID]		= "CP10 MRC/VMRS",
790 	[ESR_ELx_EC_PAC]		= "PAC",
791 	[ESR_ELx_EC_CP14_64]		= "CP14 MCRR/MRRC",
792 	[ESR_ELx_EC_BTI]		= "BTI",
793 	[ESR_ELx_EC_ILL]		= "PSTATE.IL",
794 	[ESR_ELx_EC_SVC32]		= "SVC (AArch32)",
795 	[ESR_ELx_EC_HVC32]		= "HVC (AArch32)",
796 	[ESR_ELx_EC_SMC32]		= "SMC (AArch32)",
797 	[ESR_ELx_EC_SVC64]		= "SVC (AArch64)",
798 	[ESR_ELx_EC_HVC64]		= "HVC (AArch64)",
799 	[ESR_ELx_EC_SMC64]		= "SMC (AArch64)",
800 	[ESR_ELx_EC_SYS64]		= "MSR/MRS (AArch64)",
801 	[ESR_ELx_EC_SVE]		= "SVE",
802 	[ESR_ELx_EC_ERET]		= "ERET/ERETAA/ERETAB",
803 	[ESR_ELx_EC_FPAC]		= "FPAC",
804 	[ESR_ELx_EC_IMP_DEF]		= "EL3 IMP DEF",
805 	[ESR_ELx_EC_IABT_LOW]		= "IABT (lower EL)",
806 	[ESR_ELx_EC_IABT_CUR]		= "IABT (current EL)",
807 	[ESR_ELx_EC_PC_ALIGN]		= "PC Alignment",
808 	[ESR_ELx_EC_DABT_LOW]		= "DABT (lower EL)",
809 	[ESR_ELx_EC_DABT_CUR]		= "DABT (current EL)",
810 	[ESR_ELx_EC_SP_ALIGN]		= "SP Alignment",
811 	[ESR_ELx_EC_FP_EXC32]		= "FP (AArch32)",
812 	[ESR_ELx_EC_FP_EXC64]		= "FP (AArch64)",
813 	[ESR_ELx_EC_SERROR]		= "SError",
814 	[ESR_ELx_EC_BREAKPT_LOW]	= "Breakpoint (lower EL)",
815 	[ESR_ELx_EC_BREAKPT_CUR]	= "Breakpoint (current EL)",
816 	[ESR_ELx_EC_SOFTSTP_LOW]	= "Software Step (lower EL)",
817 	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
818 	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
819 	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
820 	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
821 	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
822 	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
823 };
824 
esr_get_class_string(u32 esr)825 const char *esr_get_class_string(u32 esr)
826 {
827 	return esr_class_str[ESR_ELx_EC(esr)];
828 }
829 
830 /*
831  * bad_el0_sync handles unexpected, but potentially recoverable synchronous
832  * exceptions taken from EL0.
833  */
bad_el0_sync(struct pt_regs * regs,int reason,unsigned int esr)834 void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
835 {
836 	unsigned long pc = instruction_pointer(regs);
837 
838 	current->thread.fault_address = 0;
839 	current->thread.fault_code = esr;
840 
841 	arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
842 			      "Bad EL0 synchronous exception");
843 }
844 
845 #ifdef CONFIG_VMAP_STACK
846 
847 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
848 	__aligned(16);
849 
panic_bad_stack(struct pt_regs * regs,unsigned int esr,unsigned long far)850 void panic_bad_stack(struct pt_regs *regs, unsigned int esr, unsigned long far)
851 {
852 	unsigned long tsk_stk = (unsigned long)current->stack;
853 	unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
854 	unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
855 
856 	console_verbose();
857 	pr_emerg("Insufficient stack space to handle exception!");
858 
859 	pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
860 	pr_emerg("FAR: 0x%016lx\n", far);
861 
862 	pr_emerg("Task stack:     [0x%016lx..0x%016lx]\n",
863 		 tsk_stk, tsk_stk + THREAD_SIZE);
864 	pr_emerg("IRQ stack:      [0x%016lx..0x%016lx]\n",
865 		 irq_stk, irq_stk + IRQ_STACK_SIZE);
866 	pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
867 		 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
868 
869 	__show_regs(regs);
870 
871 	/*
872 	 * We use nmi_panic to limit the potential for recusive overflows, and
873 	 * to get a better stack trace.
874 	 */
875 	nmi_panic(NULL, "kernel stack overflow");
876 	cpu_park_loop();
877 }
878 #endif
879 
arm64_serror_panic(struct pt_regs * regs,u32 esr)880 void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
881 {
882 	console_verbose();
883 
884 	pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
885 		smp_processor_id(), esr, esr_get_class_string(esr));
886 
887 	trace_android_rvh_arm64_serror_panic(regs, esr);
888 	if (regs)
889 		__show_regs(regs);
890 
891 	nmi_panic(regs, "Asynchronous SError Interrupt");
892 
893 	cpu_park_loop();
894 	unreachable();
895 }
896 
arm64_is_fatal_ras_serror(struct pt_regs * regs,unsigned int esr)897 bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
898 {
899 	u32 aet = arm64_ras_serror_get_severity(esr);
900 
901 	switch (aet) {
902 	case ESR_ELx_AET_CE:	/* corrected error */
903 	case ESR_ELx_AET_UEO:	/* restartable, not yet consumed */
904 		/*
905 		 * The CPU can make progress. We may take UEO again as
906 		 * a more severe error.
907 		 */
908 		return false;
909 
910 	case ESR_ELx_AET_UEU:	/* Uncorrected Unrecoverable */
911 	case ESR_ELx_AET_UER:	/* Uncorrected Recoverable */
912 		/*
913 		 * The CPU can't make progress. The exception may have
914 		 * been imprecise.
915 		 *
916 		 * Neoverse-N1 #1349291 means a non-KVM SError reported as
917 		 * Unrecoverable should be treated as Uncontainable. We
918 		 * call arm64_serror_panic() in both cases.
919 		 */
920 		return true;
921 
922 	case ESR_ELx_AET_UC:	/* Uncontainable or Uncategorized error */
923 	default:
924 		/* Error has been silently propagated */
925 		arm64_serror_panic(regs, esr);
926 	}
927 }
928 
do_serror(struct pt_regs * regs,unsigned int esr)929 void do_serror(struct pt_regs *regs, unsigned int esr)
930 {
931 	int ret = 0;
932 
933 	/* Add vendor hooks for unusual abort cases */
934 	trace_android_rvh_do_serror(regs, esr, &ret);
935 	if (ret != 0)
936 		return;
937 
938 	/* non-RAS errors are not containable */
939 	if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
940 		arm64_serror_panic(regs, esr);
941 }
942 
943 /* GENERIC_BUG traps */
944 
is_valid_bugaddr(unsigned long addr)945 int is_valid_bugaddr(unsigned long addr)
946 {
947 	/*
948 	 * bug_handler() only called for BRK #BUG_BRK_IMM.
949 	 * So the answer is trivial -- any spurious instances with no
950 	 * bug table entry will be rejected by report_bug() and passed
951 	 * back to the debug-monitors code and handled as a fatal
952 	 * unexpected debug exception.
953 	 */
954 	return 1;
955 }
956 
bug_handler(struct pt_regs * regs,unsigned int esr)957 static int bug_handler(struct pt_regs *regs, unsigned int esr)
958 {
959 	switch (report_bug(regs->pc, regs)) {
960 	case BUG_TRAP_TYPE_BUG:
961 		die("Oops - BUG", regs, esr);
962 		break;
963 
964 	case BUG_TRAP_TYPE_WARN:
965 		break;
966 
967 	default:
968 		/* unknown/unrecognised bug trap type */
969 		return DBG_HOOK_ERROR;
970 	}
971 
972 	/* If thread survives, skip over the BUG instruction and continue: */
973 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
974 	return DBG_HOOK_HANDLED;
975 }
976 
977 static struct break_hook bug_break_hook = {
978 	.fn = bug_handler,
979 	.imm = BUG_BRK_IMM,
980 };
981 
reserved_fault_handler(struct pt_regs * regs,unsigned int esr)982 static int reserved_fault_handler(struct pt_regs *regs, unsigned int esr)
983 {
984 	pr_err("%s generated an invalid instruction at %pS!\n",
985 		"Kernel text patching",
986 		(void *)instruction_pointer(regs));
987 
988 	/* We cannot handle this */
989 	return DBG_HOOK_ERROR;
990 }
991 
992 static struct break_hook fault_break_hook = {
993 	.fn = reserved_fault_handler,
994 	.imm = FAULT_BRK_IMM,
995 };
996 
997 #ifdef CONFIG_KASAN_SW_TAGS
998 
999 #define KASAN_ESR_RECOVER	0x20
1000 #define KASAN_ESR_WRITE	0x10
1001 #define KASAN_ESR_SIZE_MASK	0x0f
1002 #define KASAN_ESR_SIZE(esr)	(1 << ((esr) & KASAN_ESR_SIZE_MASK))
1003 
kasan_handler(struct pt_regs * regs,unsigned int esr)1004 static int kasan_handler(struct pt_regs *regs, unsigned int esr)
1005 {
1006 	bool recover = esr & KASAN_ESR_RECOVER;
1007 	bool write = esr & KASAN_ESR_WRITE;
1008 	size_t size = KASAN_ESR_SIZE(esr);
1009 	u64 addr = regs->regs[0];
1010 	u64 pc = regs->pc;
1011 
1012 	kasan_report(addr, size, write, pc);
1013 
1014 	/*
1015 	 * The instrumentation allows to control whether we can proceed after
1016 	 * a crash was detected. This is done by passing the -recover flag to
1017 	 * the compiler. Disabling recovery allows to generate more compact
1018 	 * code.
1019 	 *
1020 	 * Unfortunately disabling recovery doesn't work for the kernel right
1021 	 * now. KASAN reporting is disabled in some contexts (for example when
1022 	 * the allocator accesses slab object metadata; this is controlled by
1023 	 * current->kasan_depth). All these accesses are detected by the tool,
1024 	 * even though the reports for them are not printed.
1025 	 *
1026 	 * This is something that might be fixed at some point in the future.
1027 	 */
1028 	if (!recover)
1029 		die("Oops - KASAN", regs, esr);
1030 
1031 	/* If thread survives, skip over the brk instruction and continue: */
1032 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
1033 	return DBG_HOOK_HANDLED;
1034 }
1035 
1036 static struct break_hook kasan_break_hook = {
1037 	.fn	= kasan_handler,
1038 	.imm	= KASAN_BRK_IMM,
1039 	.mask	= KASAN_BRK_MASK,
1040 };
1041 #endif
1042 
1043 /*
1044  * Initial handler for AArch64 BRK exceptions
1045  * This handler only used until debug_traps_init().
1046  */
early_brk64(unsigned long addr,unsigned int esr,struct pt_regs * regs)1047 int __init early_brk64(unsigned long addr, unsigned int esr,
1048 		struct pt_regs *regs)
1049 {
1050 #ifdef CONFIG_KASAN_SW_TAGS
1051 	unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
1052 
1053 	if ((comment & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
1054 		return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
1055 #endif
1056 	return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
1057 }
1058 
trap_init(void)1059 void __init trap_init(void)
1060 {
1061 	register_kernel_break_hook(&bug_break_hook);
1062 	register_kernel_break_hook(&fault_break_hook);
1063 #ifdef CONFIG_KASAN_SW_TAGS
1064 	register_kernel_break_hook(&kasan_break_hook);
1065 #endif
1066 	debug_traps_init();
1067 }
1068