• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Exception handling code
4  *
5  * Copyright (C) 2019 ARM Ltd.
6  */
7 
8 #include <linux/context_tracking.h>
9 #include <linux/kasan.h>
10 #include <linux/linkage.h>
11 #include <linux/lockdep.h>
12 #include <linux/ptrace.h>
13 #include <linux/resume_user_mode.h>
14 #include <linux/sched.h>
15 #include <linux/sched/debug.h>
16 #include <linux/thread_info.h>
17 
18 #include <asm/cpufeature.h>
19 #include <asm/daifflags.h>
20 #include <asm/esr.h>
21 #include <asm/exception.h>
22 #include <asm/irq_regs.h>
23 #include <asm/kprobes.h>
24 #include <asm/mmu.h>
25 #include <asm/processor.h>
26 #include <asm/sdei.h>
27 #include <asm/stacktrace.h>
28 #include <asm/sysreg.h>
29 #include <asm/system_misc.h>
30 
31 #include <trace/hooks/traps.h>
32 #include <trace/hooks/gic.h>
33 
34 /*
35  * Handle IRQ/context state management when entering from kernel mode.
36  * Before this function is called it is not safe to call regular kernel code,
37  * instrumentable code, or any code which may trigger an exception.
38  *
39  * This is intended to match the logic in irqentry_enter(), handling the kernel
40  * mode transitions only.
41  */
__enter_from_kernel_mode(struct pt_regs * regs)42 static __always_inline void __enter_from_kernel_mode(struct pt_regs *regs)
43 {
44 	regs->exit_rcu = false;
45 
46 	if (!IS_ENABLED(CONFIG_TINY_RCU) && is_idle_task(current)) {
47 		lockdep_hardirqs_off(CALLER_ADDR0);
48 		ct_irq_enter();
49 		trace_hardirqs_off_finish();
50 
51 		regs->exit_rcu = true;
52 		return;
53 	}
54 
55 	lockdep_hardirqs_off(CALLER_ADDR0);
56 	rcu_irq_enter_check_tick();
57 	trace_hardirqs_off_finish();
58 }
59 
enter_from_kernel_mode(struct pt_regs * regs)60 static void noinstr enter_from_kernel_mode(struct pt_regs *regs)
61 {
62 	__enter_from_kernel_mode(regs);
63 	mte_check_tfsr_entry();
64 	mte_disable_tco_entry(current);
65 }
66 
67 /*
68  * Handle IRQ/context state management when exiting to kernel mode.
69  * After this function returns it is not safe to call regular kernel code,
70  * instrumentable code, or any code which may trigger an exception.
71  *
72  * This is intended to match the logic in irqentry_exit(), handling the kernel
73  * mode transitions only, and with preemption handled elsewhere.
74  */
__exit_to_kernel_mode(struct pt_regs * regs)75 static __always_inline void __exit_to_kernel_mode(struct pt_regs *regs)
76 {
77 	lockdep_assert_irqs_disabled();
78 
79 	if (interrupts_enabled(regs)) {
80 		if (regs->exit_rcu) {
81 			trace_hardirqs_on_prepare();
82 			lockdep_hardirqs_on_prepare();
83 			ct_irq_exit();
84 			lockdep_hardirqs_on(CALLER_ADDR0);
85 			return;
86 		}
87 
88 		trace_hardirqs_on();
89 	} else {
90 		if (regs->exit_rcu)
91 			ct_irq_exit();
92 	}
93 }
94 
exit_to_kernel_mode(struct pt_regs * regs)95 static void noinstr exit_to_kernel_mode(struct pt_regs *regs)
96 {
97 	mte_check_tfsr_exit();
98 	__exit_to_kernel_mode(regs);
99 }
100 
101 /*
102  * Handle IRQ/context state management when entering from user mode.
103  * Before this function is called it is not safe to call regular kernel code,
104  * instrumentable code, or any code which may trigger an exception.
105  */
__enter_from_user_mode(void)106 static __always_inline void __enter_from_user_mode(void)
107 {
108 	lockdep_hardirqs_off(CALLER_ADDR0);
109 	CT_WARN_ON(ct_state() != CT_STATE_USER);
110 	user_exit_irqoff();
111 	trace_hardirqs_off_finish();
112 	mte_disable_tco_entry(current);
113 }
114 
enter_from_user_mode(struct pt_regs * regs)115 static __always_inline void enter_from_user_mode(struct pt_regs *regs)
116 {
117 	__enter_from_user_mode();
118 }
119 
120 /*
121  * Handle IRQ/context state management when exiting to user mode.
122  * After this function returns it is not safe to call regular kernel code,
123  * instrumentable code, or any code which may trigger an exception.
124  */
__exit_to_user_mode(void)125 static __always_inline void __exit_to_user_mode(void)
126 {
127 	trace_hardirqs_on_prepare();
128 	lockdep_hardirqs_on_prepare();
129 	user_enter_irqoff();
130 	lockdep_hardirqs_on(CALLER_ADDR0);
131 }
132 
do_notify_resume(struct pt_regs * regs,unsigned long thread_flags)133 static void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags)
134 {
135 	do {
136 		local_irq_enable();
137 
138 		if (thread_flags & _TIF_NEED_RESCHED)
139 			schedule();
140 
141 		if (thread_flags & _TIF_UPROBE)
142 			uprobe_notify_resume(regs);
143 
144 		if (thread_flags & _TIF_MTE_ASYNC_FAULT) {
145 			clear_thread_flag(TIF_MTE_ASYNC_FAULT);
146 			send_sig_fault(SIGSEGV, SEGV_MTEAERR,
147 				       (void __user *)NULL, current);
148 		}
149 
150 		if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
151 			do_signal(regs);
152 
153 		if (thread_flags & _TIF_NOTIFY_RESUME)
154 			resume_user_mode_work(regs);
155 
156 		if (thread_flags & _TIF_FOREIGN_FPSTATE)
157 			fpsimd_restore_current_state();
158 
159 		local_irq_disable();
160 		thread_flags = read_thread_flags();
161 	} while (thread_flags & _TIF_WORK_MASK);
162 }
163 
exit_to_user_mode_prepare(struct pt_regs * regs)164 static __always_inline void exit_to_user_mode_prepare(struct pt_regs *regs)
165 {
166 	unsigned long flags;
167 
168 	local_irq_disable();
169 
170 	flags = read_thread_flags();
171 	if (unlikely(flags & _TIF_WORK_MASK))
172 		do_notify_resume(regs, flags);
173 
174 	local_daif_mask();
175 
176 	lockdep_sys_exit();
177 }
178 
exit_to_user_mode(struct pt_regs * regs)179 static __always_inline void exit_to_user_mode(struct pt_regs *regs)
180 {
181 	exit_to_user_mode_prepare(regs);
182 	mte_check_tfsr_exit();
183 	__exit_to_user_mode();
184 }
185 
asm_exit_to_user_mode(struct pt_regs * regs)186 asmlinkage void noinstr asm_exit_to_user_mode(struct pt_regs *regs)
187 {
188 	exit_to_user_mode(regs);
189 }
190 
191 /*
192  * Handle IRQ/context state management when entering an NMI from user/kernel
193  * mode. Before this function is called it is not safe to call regular kernel
194  * code, instrumentable code, or any code which may trigger an exception.
195  */
arm64_enter_nmi(struct pt_regs * regs)196 static void noinstr arm64_enter_nmi(struct pt_regs *regs)
197 {
198 	regs->lockdep_hardirqs = lockdep_hardirqs_enabled();
199 
200 	__nmi_enter();
201 	lockdep_hardirqs_off(CALLER_ADDR0);
202 	lockdep_hardirq_enter();
203 	ct_nmi_enter();
204 
205 	trace_hardirqs_off_finish();
206 	ftrace_nmi_enter();
207 }
208 
209 /*
210  * Handle IRQ/context state management when exiting an NMI from user/kernel
211  * mode. After this function returns it is not safe to call regular kernel
212  * code, instrumentable code, or any code which may trigger an exception.
213  */
arm64_exit_nmi(struct pt_regs * regs)214 static void noinstr arm64_exit_nmi(struct pt_regs *regs)
215 {
216 	bool restore = regs->lockdep_hardirqs;
217 
218 	ftrace_nmi_exit();
219 	if (restore) {
220 		trace_hardirqs_on_prepare();
221 		lockdep_hardirqs_on_prepare();
222 	}
223 
224 	ct_nmi_exit();
225 	lockdep_hardirq_exit();
226 	if (restore)
227 		lockdep_hardirqs_on(CALLER_ADDR0);
228 	__nmi_exit();
229 }
230 
231 /*
232  * Handle IRQ/context state management when entering a debug exception from
233  * kernel mode. Before this function is called it is not safe to call regular
234  * kernel code, instrumentable code, or any code which may trigger an exception.
235  */
arm64_enter_el1_dbg(struct pt_regs * regs)236 static void noinstr arm64_enter_el1_dbg(struct pt_regs *regs)
237 {
238 	regs->lockdep_hardirqs = lockdep_hardirqs_enabled();
239 
240 	lockdep_hardirqs_off(CALLER_ADDR0);
241 	ct_nmi_enter();
242 
243 	trace_hardirqs_off_finish();
244 }
245 
246 /*
247  * Handle IRQ/context state management when exiting a debug exception from
248  * kernel mode. After this function returns it is not safe to call regular
249  * kernel code, instrumentable code, or any code which may trigger an exception.
250  */
arm64_exit_el1_dbg(struct pt_regs * regs)251 static void noinstr arm64_exit_el1_dbg(struct pt_regs *regs)
252 {
253 	bool restore = regs->lockdep_hardirqs;
254 
255 	if (restore) {
256 		trace_hardirqs_on_prepare();
257 		lockdep_hardirqs_on_prepare();
258 	}
259 
260 	ct_nmi_exit();
261 	if (restore)
262 		lockdep_hardirqs_on(CALLER_ADDR0);
263 }
264 
265 #ifdef CONFIG_PREEMPT_DYNAMIC
266 DEFINE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
267 #define need_irq_preemption() \
268 	(static_branch_unlikely(&sk_dynamic_irqentry_exit_cond_resched))
269 #else
270 #define need_irq_preemption()	(IS_ENABLED(CONFIG_PREEMPTION))
271 #endif
272 
arm64_preempt_schedule_irq(void)273 static void __sched arm64_preempt_schedule_irq(void)
274 {
275 	if (!need_irq_preemption())
276 		return;
277 
278 	/*
279 	 * Note: thread_info::preempt_count includes both thread_info::count
280 	 * and thread_info::need_resched, and is not equivalent to
281 	 * preempt_count().
282 	 */
283 	if (READ_ONCE(current_thread_info()->preempt_count) != 0)
284 		return;
285 
286 	/*
287 	 * DAIF.DA are cleared at the start of IRQ/FIQ handling, and when GIC
288 	 * priority masking is used the GIC irqchip driver will clear DAIF.IF
289 	 * using gic_arch_enable_irqs() for normal IRQs. If anything is set in
290 	 * DAIF we must have handled an NMI, so skip preemption.
291 	 */
292 	if (system_uses_irq_prio_masking() && read_sysreg(daif))
293 		return;
294 
295 	/*
296 	 * Preempting a task from an IRQ means we leave copies of PSTATE
297 	 * on the stack. cpufeature's enable calls may modify PSTATE, but
298 	 * resuming one of these preempted tasks would undo those changes.
299 	 *
300 	 * Only allow a task to be preempted once cpufeatures have been
301 	 * enabled.
302 	 */
303 	if (system_capabilities_finalized())
304 		preempt_schedule_irq();
305 }
306 
do_interrupt_handler(struct pt_regs * regs,void (* handler)(struct pt_regs *))307 static void do_interrupt_handler(struct pt_regs *regs,
308 				 void (*handler)(struct pt_regs *))
309 {
310 	struct pt_regs *old_regs = set_irq_regs(regs);
311 
312 	if (on_thread_stack())
313 		call_on_irq_stack(regs, handler);
314 	else
315 		handler(regs);
316 
317 	set_irq_regs(old_regs);
318 }
319 
320 extern void (*handle_arch_irq)(struct pt_regs *);
321 extern void (*handle_arch_fiq)(struct pt_regs *);
322 
__panic_unhandled(struct pt_regs * regs,const char * vector,unsigned long esr)323 static void noinstr __panic_unhandled(struct pt_regs *regs, const char *vector,
324 				      unsigned long esr)
325 {
326 	arm64_enter_nmi(regs);
327 
328 	console_verbose();
329 
330 	pr_crit("Unhandled %s exception on CPU%d, ESR 0x%016lx -- %s\n",
331 		vector, smp_processor_id(), esr,
332 		esr_get_class_string(esr));
333 
334 	trace_android_rvh_panic_unhandled(regs, vector, esr);
335 	__show_regs(regs);
336 	panic("Unhandled exception");
337 }
338 
339 #define UNHANDLED(el, regsize, vector)							\
340 asmlinkage void noinstr el##_##regsize##_##vector##_handler(struct pt_regs *regs)	\
341 {											\
342 	const char *desc = #regsize "-bit " #el " " #vector;				\
343 	__panic_unhandled(regs, desc, read_sysreg(esr_el1));				\
344 }
345 
346 #ifdef CONFIG_ARM64_ERRATUM_1463225
347 static DEFINE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
348 
cortex_a76_erratum_1463225_svc_handler(void)349 static void cortex_a76_erratum_1463225_svc_handler(void)
350 {
351 	u32 reg, val;
352 
353 	if (!unlikely(test_thread_flag(TIF_SINGLESTEP)))
354 		return;
355 
356 	if (!unlikely(this_cpu_has_cap(ARM64_WORKAROUND_1463225)))
357 		return;
358 
359 	__this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 1);
360 	reg = read_sysreg(mdscr_el1);
361 	val = reg | DBG_MDSCR_SS | DBG_MDSCR_KDE;
362 	write_sysreg(val, mdscr_el1);
363 	asm volatile("msr daifclr, #8");
364 	isb();
365 
366 	/* We will have taken a single-step exception by this point */
367 
368 	write_sysreg(reg, mdscr_el1);
369 	__this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 0);
370 }
371 
372 static __always_inline bool
cortex_a76_erratum_1463225_debug_handler(struct pt_regs * regs)373 cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
374 {
375 	if (!__this_cpu_read(__in_cortex_a76_erratum_1463225_wa))
376 		return false;
377 
378 	/*
379 	 * We've taken a dummy step exception from the kernel to ensure
380 	 * that interrupts are re-enabled on the syscall path. Return back
381 	 * to cortex_a76_erratum_1463225_svc_handler() with debug exceptions
382 	 * masked so that we can safely restore the mdscr and get on with
383 	 * handling the syscall.
384 	 */
385 	regs->pstate |= PSR_D_BIT;
386 	return true;
387 }
388 #else /* CONFIG_ARM64_ERRATUM_1463225 */
cortex_a76_erratum_1463225_svc_handler(void)389 static void cortex_a76_erratum_1463225_svc_handler(void) { }
cortex_a76_erratum_1463225_debug_handler(struct pt_regs * regs)390 static bool cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
391 {
392 	return false;
393 }
394 #endif /* CONFIG_ARM64_ERRATUM_1463225 */
395 
396 /*
397  * As per the ABI exit SME streaming mode and clear the SVE state not
398  * shared with FPSIMD on syscall entry.
399  */
fpsimd_syscall_enter(void)400 static inline void fpsimd_syscall_enter(void)
401 {
402 	/* Ensure PSTATE.SM is clear, but leave PSTATE.ZA as-is. */
403 	if (system_supports_sme())
404 		sme_smstop_sm();
405 
406 	/*
407 	 * The CPU is not in streaming mode. If non-streaming SVE is not
408 	 * supported, there is no SVE state that needs to be discarded.
409 	 */
410 	if (!system_supports_sve())
411 		return;
412 
413 	if (test_thread_flag(TIF_SVE)) {
414 		unsigned int sve_vq_minus_one;
415 
416 		sve_vq_minus_one = sve_vq_from_vl(task_get_sve_vl(current)) - 1;
417 		sve_flush_live(true, sve_vq_minus_one);
418 	}
419 
420 	/*
421 	 * Any live non-FPSIMD SVE state has been zeroed. Allow
422 	 * fpsimd_save_user_state() to lazily discard SVE state until either
423 	 * the live state is unbound or fpsimd_syscall_exit() is called.
424 	 */
425 	__this_cpu_write(fpsimd_last_state.to_save, FP_STATE_FPSIMD);
426 }
427 
fpsimd_syscall_exit(void)428 static __always_inline void fpsimd_syscall_exit(void)
429 {
430 	if (!system_supports_sve())
431 		return;
432 
433 	/*
434 	 * The current task's user FPSIMD/SVE/SME state is now bound to this
435 	 * CPU. The fpsimd_last_state.to_save value is either:
436 	 *
437 	 * - FP_STATE_FPSIMD, if the state has not been reloaded on this CPU
438 	 *   since fpsimd_syscall_enter().
439 	 *
440 	 * - FP_STATE_CURRENT, if the state has been reloaded on this CPU at
441 	 *   any point.
442 	 *
443 	 * Reset this to FP_STATE_CURRENT to stop lazy discarding.
444 	 */
445 	__this_cpu_write(fpsimd_last_state.to_save, FP_STATE_CURRENT);
446 }
447 
448 UNHANDLED(el1t, 64, sync)
449 UNHANDLED(el1t, 64, irq)
450 UNHANDLED(el1t, 64, fiq)
451 UNHANDLED(el1t, 64, error)
452 
el1_abort(struct pt_regs * regs,unsigned long esr)453 static void noinstr el1_abort(struct pt_regs *regs, unsigned long esr)
454 {
455 	unsigned long far = read_sysreg(far_el1);
456 
457 	enter_from_kernel_mode(regs);
458 	local_daif_inherit(regs);
459 	do_mem_abort(far, esr, regs);
460 	local_daif_mask();
461 	exit_to_kernel_mode(regs);
462 }
463 
el1_pc(struct pt_regs * regs,unsigned long esr)464 static void noinstr el1_pc(struct pt_regs *regs, unsigned long esr)
465 {
466 	unsigned long far = read_sysreg(far_el1);
467 
468 	enter_from_kernel_mode(regs);
469 	local_daif_inherit(regs);
470 	do_sp_pc_abort(far, esr, regs);
471 	local_daif_mask();
472 	exit_to_kernel_mode(regs);
473 }
474 
el1_undef(struct pt_regs * regs,unsigned long esr)475 static void noinstr el1_undef(struct pt_regs *regs, unsigned long esr)
476 {
477 	enter_from_kernel_mode(regs);
478 	local_daif_inherit(regs);
479 	do_el1_undef(regs, esr);
480 	local_daif_mask();
481 	exit_to_kernel_mode(regs);
482 }
483 
el1_bti(struct pt_regs * regs,unsigned long esr)484 static void noinstr el1_bti(struct pt_regs *regs, unsigned long esr)
485 {
486 	enter_from_kernel_mode(regs);
487 	local_daif_inherit(regs);
488 	do_el1_bti(regs, esr);
489 	local_daif_mask();
490 	exit_to_kernel_mode(regs);
491 }
492 
el1_dbg(struct pt_regs * regs,unsigned long esr)493 static void noinstr el1_dbg(struct pt_regs *regs, unsigned long esr)
494 {
495 	unsigned long far = read_sysreg(far_el1);
496 
497 	arm64_enter_el1_dbg(regs);
498 	if (!cortex_a76_erratum_1463225_debug_handler(regs))
499 		do_debug_exception(far, esr, regs);
500 	arm64_exit_el1_dbg(regs);
501 }
502 
el1_fpac(struct pt_regs * regs,unsigned long esr)503 static void noinstr el1_fpac(struct pt_regs *regs, unsigned long esr)
504 {
505 	enter_from_kernel_mode(regs);
506 	local_daif_inherit(regs);
507 	do_el1_fpac(regs, esr);
508 	local_daif_mask();
509 	exit_to_kernel_mode(regs);
510 }
511 
el1h_64_sync_handler(struct pt_regs * regs)512 asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)
513 {
514 	unsigned long esr = read_sysreg(esr_el1);
515 
516 	switch (ESR_ELx_EC(esr)) {
517 	case ESR_ELx_EC_DABT_CUR:
518 	case ESR_ELx_EC_IABT_CUR:
519 		el1_abort(regs, esr);
520 		break;
521 	/*
522 	 * We don't handle ESR_ELx_EC_SP_ALIGN, since we will have hit a
523 	 * recursive exception when trying to push the initial pt_regs.
524 	 */
525 	case ESR_ELx_EC_PC_ALIGN:
526 		el1_pc(regs, esr);
527 		break;
528 	case ESR_ELx_EC_SYS64:
529 	case ESR_ELx_EC_UNKNOWN:
530 		el1_undef(regs, esr);
531 		break;
532 	case ESR_ELx_EC_BTI:
533 		el1_bti(regs, esr);
534 		break;
535 	case ESR_ELx_EC_BREAKPT_CUR:
536 	case ESR_ELx_EC_SOFTSTP_CUR:
537 	case ESR_ELx_EC_WATCHPT_CUR:
538 	case ESR_ELx_EC_BRK64:
539 		el1_dbg(regs, esr);
540 		break;
541 	case ESR_ELx_EC_FPAC:
542 		el1_fpac(regs, esr);
543 		break;
544 	default:
545 		__panic_unhandled(regs, "64-bit el1h sync", esr);
546 	}
547 }
548 
__el1_pnmi(struct pt_regs * regs,void (* handler)(struct pt_regs *))549 static __always_inline void __el1_pnmi(struct pt_regs *regs,
550 				       void (*handler)(struct pt_regs *))
551 {
552 	arm64_enter_nmi(regs);
553 	do_interrupt_handler(regs, handler);
554 	arm64_exit_nmi(regs);
555 }
556 
__el1_irq(struct pt_regs * regs,void (* handler)(struct pt_regs *))557 static __always_inline void __el1_irq(struct pt_regs *regs,
558 				      void (*handler)(struct pt_regs *))
559 {
560 	enter_from_kernel_mode(regs);
561 
562 	irq_enter_rcu();
563 	do_interrupt_handler(regs, handler);
564 	irq_exit_rcu();
565 
566 	arm64_preempt_schedule_irq();
567 
568 	exit_to_kernel_mode(regs);
569 }
el1_interrupt(struct pt_regs * regs,void (* handler)(struct pt_regs *))570 static void noinstr el1_interrupt(struct pt_regs *regs,
571 				  void (*handler)(struct pt_regs *))
572 {
573 	write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
574 
575 	if (IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) && !interrupts_enabled(regs))
576 		__el1_pnmi(regs, handler);
577 	else
578 		__el1_irq(regs, handler);
579 }
580 
el1h_64_irq_handler(struct pt_regs * regs)581 asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs)
582 {
583 	el1_interrupt(regs, handle_arch_irq);
584 }
585 
el1h_64_fiq_handler(struct pt_regs * regs)586 asmlinkage void noinstr el1h_64_fiq_handler(struct pt_regs *regs)
587 {
588 	trace_android_rvh_fiq_dump(regs);
589 	el1_interrupt(regs, handle_arch_fiq);
590 }
591 
el1h_64_error_handler(struct pt_regs * regs)592 asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs)
593 {
594 	unsigned long esr = read_sysreg(esr_el1);
595 
596 	local_daif_restore(DAIF_ERRCTX);
597 	arm64_enter_nmi(regs);
598 	do_serror(regs, esr);
599 	arm64_exit_nmi(regs);
600 }
601 
el0_da(struct pt_regs * regs,unsigned long esr)602 static void noinstr el0_da(struct pt_regs *regs, unsigned long esr)
603 {
604 	unsigned long far = read_sysreg(far_el1);
605 
606 	enter_from_user_mode(regs);
607 	local_daif_restore(DAIF_PROCCTX);
608 	do_mem_abort(far, esr, regs);
609 	exit_to_user_mode(regs);
610 }
611 
el0_ia(struct pt_regs * regs,unsigned long esr)612 static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr)
613 {
614 	unsigned long far = read_sysreg(far_el1);
615 
616 	/*
617 	 * We've taken an instruction abort from userspace and not yet
618 	 * re-enabled IRQs. If the address is a kernel address, apply
619 	 * BP hardening prior to enabling IRQs and pre-emption.
620 	 */
621 	if (!is_ttbr0_addr(far))
622 		arm64_apply_bp_hardening();
623 
624 	enter_from_user_mode(regs);
625 	local_daif_restore(DAIF_PROCCTX);
626 	do_mem_abort(far, esr, regs);
627 	exit_to_user_mode(regs);
628 }
629 
el0_fpsimd_acc(struct pt_regs * regs,unsigned long esr)630 static void noinstr el0_fpsimd_acc(struct pt_regs *regs, unsigned long esr)
631 {
632 	enter_from_user_mode(regs);
633 	local_daif_restore(DAIF_PROCCTX);
634 	do_fpsimd_acc(esr, regs);
635 	exit_to_user_mode(regs);
636 }
637 
el0_sve_acc(struct pt_regs * regs,unsigned long esr)638 static void noinstr el0_sve_acc(struct pt_regs *regs, unsigned long esr)
639 {
640 	enter_from_user_mode(regs);
641 	local_daif_restore(DAIF_PROCCTX);
642 	do_sve_acc(esr, regs);
643 	exit_to_user_mode(regs);
644 }
645 
el0_sme_acc(struct pt_regs * regs,unsigned long esr)646 static void noinstr el0_sme_acc(struct pt_regs *regs, unsigned long esr)
647 {
648 	enter_from_user_mode(regs);
649 	local_daif_restore(DAIF_PROCCTX);
650 	do_sme_acc(esr, regs);
651 	exit_to_user_mode(regs);
652 }
653 
el0_fpsimd_exc(struct pt_regs * regs,unsigned long esr)654 static void noinstr el0_fpsimd_exc(struct pt_regs *regs, unsigned long esr)
655 {
656 	enter_from_user_mode(regs);
657 	local_daif_restore(DAIF_PROCCTX);
658 	do_fpsimd_exc(esr, regs);
659 	exit_to_user_mode(regs);
660 }
661 
el0_sys(struct pt_regs * regs,unsigned long esr)662 static void noinstr el0_sys(struct pt_regs *regs, unsigned long esr)
663 {
664 	enter_from_user_mode(regs);
665 	local_daif_restore(DAIF_PROCCTX);
666 	do_el0_sys(esr, regs);
667 	exit_to_user_mode(regs);
668 }
669 
el0_pc(struct pt_regs * regs,unsigned long esr)670 static void noinstr el0_pc(struct pt_regs *regs, unsigned long esr)
671 {
672 	unsigned long far = read_sysreg(far_el1);
673 
674 	if (!is_ttbr0_addr(instruction_pointer(regs)))
675 		arm64_apply_bp_hardening();
676 
677 	enter_from_user_mode(regs);
678 	local_daif_restore(DAIF_PROCCTX);
679 	do_sp_pc_abort(far, esr, regs);
680 	exit_to_user_mode(regs);
681 }
682 
el0_sp(struct pt_regs * regs,unsigned long esr)683 static void noinstr el0_sp(struct pt_regs *regs, unsigned long esr)
684 {
685 	enter_from_user_mode(regs);
686 	local_daif_restore(DAIF_PROCCTX);
687 	do_sp_pc_abort(regs->sp, esr, regs);
688 	exit_to_user_mode(regs);
689 }
690 
el0_undef(struct pt_regs * regs,unsigned long esr)691 static void noinstr el0_undef(struct pt_regs *regs, unsigned long esr)
692 {
693 	enter_from_user_mode(regs);
694 	local_daif_restore(DAIF_PROCCTX);
695 	do_el0_undef(regs, esr);
696 	exit_to_user_mode(regs);
697 }
698 
el0_bti(struct pt_regs * regs)699 static void noinstr el0_bti(struct pt_regs *regs)
700 {
701 	enter_from_user_mode(regs);
702 	local_daif_restore(DAIF_PROCCTX);
703 	do_el0_bti(regs);
704 	exit_to_user_mode(regs);
705 }
706 
el0_mops(struct pt_regs * regs,unsigned long esr)707 static void noinstr el0_mops(struct pt_regs *regs, unsigned long esr)
708 {
709 	enter_from_user_mode(regs);
710 	local_daif_restore(DAIF_PROCCTX);
711 	do_el0_mops(regs, esr);
712 	exit_to_user_mode(regs);
713 }
714 
el0_inv(struct pt_regs * regs,unsigned long esr)715 static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr)
716 {
717 	enter_from_user_mode(regs);
718 	local_daif_restore(DAIF_PROCCTX);
719 	bad_el0_sync(regs, 0, esr);
720 	exit_to_user_mode(regs);
721 }
722 
el0_dbg(struct pt_regs * regs,unsigned long esr)723 static void noinstr el0_dbg(struct pt_regs *regs, unsigned long esr)
724 {
725 	/* Only watchpoints write FAR_EL1, otherwise its UNKNOWN */
726 	unsigned long far = read_sysreg(far_el1);
727 
728 	enter_from_user_mode(regs);
729 	do_debug_exception(far, esr, regs);
730 	local_daif_restore(DAIF_PROCCTX);
731 	exit_to_user_mode(regs);
732 }
733 
el0_svc(struct pt_regs * regs)734 static void noinstr el0_svc(struct pt_regs *regs)
735 {
736 	enter_from_user_mode(regs);
737 	cortex_a76_erratum_1463225_svc_handler();
738 	fpsimd_syscall_enter();
739 	local_daif_restore(DAIF_PROCCTX);
740 	do_el0_svc(regs);
741 	exit_to_user_mode(regs);
742 	fpsimd_syscall_exit();
743 }
744 
el0_fpac(struct pt_regs * regs,unsigned long esr)745 static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr)
746 {
747 	enter_from_user_mode(regs);
748 	local_daif_restore(DAIF_PROCCTX);
749 	do_el0_fpac(regs, esr);
750 	exit_to_user_mode(regs);
751 }
752 
el0t_64_sync_handler(struct pt_regs * regs)753 asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)
754 {
755 	unsigned long esr = read_sysreg(esr_el1);
756 
757 	switch (ESR_ELx_EC(esr)) {
758 	case ESR_ELx_EC_SVC64:
759 		el0_svc(regs);
760 		break;
761 	case ESR_ELx_EC_DABT_LOW:
762 		el0_da(regs, esr);
763 		break;
764 	case ESR_ELx_EC_IABT_LOW:
765 		el0_ia(regs, esr);
766 		break;
767 	case ESR_ELx_EC_FP_ASIMD:
768 		el0_fpsimd_acc(regs, esr);
769 		break;
770 	case ESR_ELx_EC_SVE:
771 		el0_sve_acc(regs, esr);
772 		break;
773 	case ESR_ELx_EC_SME:
774 		el0_sme_acc(regs, esr);
775 		break;
776 	case ESR_ELx_EC_FP_EXC64:
777 		el0_fpsimd_exc(regs, esr);
778 		break;
779 	case ESR_ELx_EC_SYS64:
780 	case ESR_ELx_EC_WFx:
781 		el0_sys(regs, esr);
782 		break;
783 	case ESR_ELx_EC_SP_ALIGN:
784 		el0_sp(regs, esr);
785 		break;
786 	case ESR_ELx_EC_PC_ALIGN:
787 		el0_pc(regs, esr);
788 		break;
789 	case ESR_ELx_EC_UNKNOWN:
790 		el0_undef(regs, esr);
791 		break;
792 	case ESR_ELx_EC_BTI:
793 		el0_bti(regs);
794 		break;
795 	case ESR_ELx_EC_MOPS:
796 		el0_mops(regs, esr);
797 		break;
798 	case ESR_ELx_EC_BREAKPT_LOW:
799 	case ESR_ELx_EC_SOFTSTP_LOW:
800 	case ESR_ELx_EC_WATCHPT_LOW:
801 	case ESR_ELx_EC_BRK64:
802 		el0_dbg(regs, esr);
803 		break;
804 	case ESR_ELx_EC_FPAC:
805 		el0_fpac(regs, esr);
806 		break;
807 	default:
808 		el0_inv(regs, esr);
809 	}
810 }
811 
el0_interrupt(struct pt_regs * regs,void (* handler)(struct pt_regs *))812 static void noinstr el0_interrupt(struct pt_regs *regs,
813 				  void (*handler)(struct pt_regs *))
814 {
815 	enter_from_user_mode(regs);
816 
817 	write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
818 
819 	if (regs->pc & BIT(55))
820 		arm64_apply_bp_hardening();
821 
822 	irq_enter_rcu();
823 	do_interrupt_handler(regs, handler);
824 	irq_exit_rcu();
825 
826 	exit_to_user_mode(regs);
827 }
828 
__el0_irq_handler_common(struct pt_regs * regs)829 static void noinstr __el0_irq_handler_common(struct pt_regs *regs)
830 {
831 	el0_interrupt(regs, handle_arch_irq);
832 }
833 
el0t_64_irq_handler(struct pt_regs * regs)834 asmlinkage void noinstr el0t_64_irq_handler(struct pt_regs *regs)
835 {
836 	__el0_irq_handler_common(regs);
837 }
838 
__el0_fiq_handler_common(struct pt_regs * regs)839 static void noinstr __el0_fiq_handler_common(struct pt_regs *regs)
840 {
841 	el0_interrupt(regs, handle_arch_fiq);
842 }
843 
el0t_64_fiq_handler(struct pt_regs * regs)844 asmlinkage void noinstr el0t_64_fiq_handler(struct pt_regs *regs)
845 {
846 	trace_android_rvh_fiq_dump(regs);
847 	__el0_fiq_handler_common(regs);
848 }
849 
__el0_error_handler_common(struct pt_regs * regs)850 static void noinstr __el0_error_handler_common(struct pt_regs *regs)
851 {
852 	unsigned long esr = read_sysreg(esr_el1);
853 
854 	enter_from_user_mode(regs);
855 	local_daif_restore(DAIF_ERRCTX);
856 	arm64_enter_nmi(regs);
857 	do_serror(regs, esr);
858 	arm64_exit_nmi(regs);
859 	local_daif_restore(DAIF_PROCCTX);
860 	exit_to_user_mode(regs);
861 }
862 
el0t_64_error_handler(struct pt_regs * regs)863 asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)
864 {
865 	__el0_error_handler_common(regs);
866 }
867 
868 #ifdef CONFIG_COMPAT
el0_cp15(struct pt_regs * regs,unsigned long esr)869 static void noinstr el0_cp15(struct pt_regs *regs, unsigned long esr)
870 {
871 	enter_from_user_mode(regs);
872 	local_daif_restore(DAIF_PROCCTX);
873 	do_el0_cp15(esr, regs);
874 	exit_to_user_mode(regs);
875 }
876 
el0_svc_compat(struct pt_regs * regs)877 static void noinstr el0_svc_compat(struct pt_regs *regs)
878 {
879 	enter_from_user_mode(regs);
880 	cortex_a76_erratum_1463225_svc_handler();
881 	local_daif_restore(DAIF_PROCCTX);
882 	do_el0_svc_compat(regs);
883 	exit_to_user_mode(regs);
884 }
885 
el0t_32_sync_handler(struct pt_regs * regs)886 asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)
887 {
888 	unsigned long esr = read_sysreg(esr_el1);
889 
890 	switch (ESR_ELx_EC(esr)) {
891 	case ESR_ELx_EC_SVC32:
892 		el0_svc_compat(regs);
893 		break;
894 	case ESR_ELx_EC_DABT_LOW:
895 		el0_da(regs, esr);
896 		break;
897 	case ESR_ELx_EC_IABT_LOW:
898 		el0_ia(regs, esr);
899 		break;
900 	case ESR_ELx_EC_FP_ASIMD:
901 		el0_fpsimd_acc(regs, esr);
902 		break;
903 	case ESR_ELx_EC_FP_EXC32:
904 		el0_fpsimd_exc(regs, esr);
905 		break;
906 	case ESR_ELx_EC_PC_ALIGN:
907 		el0_pc(regs, esr);
908 		break;
909 	case ESR_ELx_EC_UNKNOWN:
910 	case ESR_ELx_EC_CP14_MR:
911 	case ESR_ELx_EC_CP14_LS:
912 	case ESR_ELx_EC_CP14_64:
913 		el0_undef(regs, esr);
914 		break;
915 	case ESR_ELx_EC_CP15_32:
916 	case ESR_ELx_EC_CP15_64:
917 		el0_cp15(regs, esr);
918 		break;
919 	case ESR_ELx_EC_BREAKPT_LOW:
920 	case ESR_ELx_EC_SOFTSTP_LOW:
921 	case ESR_ELx_EC_WATCHPT_LOW:
922 	case ESR_ELx_EC_BKPT32:
923 		el0_dbg(regs, esr);
924 		break;
925 	default:
926 		el0_inv(regs, esr);
927 	}
928 }
929 
el0t_32_irq_handler(struct pt_regs * regs)930 asmlinkage void noinstr el0t_32_irq_handler(struct pt_regs *regs)
931 {
932 	__el0_irq_handler_common(regs);
933 }
934 
el0t_32_fiq_handler(struct pt_regs * regs)935 asmlinkage void noinstr el0t_32_fiq_handler(struct pt_regs *regs)
936 {
937 	__el0_fiq_handler_common(regs);
938 }
939 
el0t_32_error_handler(struct pt_regs * regs)940 asmlinkage void noinstr el0t_32_error_handler(struct pt_regs *regs)
941 {
942 	__el0_error_handler_common(regs);
943 }
944 #else /* CONFIG_COMPAT */
945 UNHANDLED(el0t, 32, sync)
946 UNHANDLED(el0t, 32, irq)
947 UNHANDLED(el0t, 32, fiq)
948 UNHANDLED(el0t, 32, error)
949 #endif /* CONFIG_COMPAT */
950 
951 #ifdef CONFIG_VMAP_STACK
handle_bad_stack(struct pt_regs * regs)952 asmlinkage void noinstr __noreturn handle_bad_stack(struct pt_regs *regs)
953 {
954 	unsigned long esr = read_sysreg(esr_el1);
955 	unsigned long far = read_sysreg(far_el1);
956 
957 	arm64_enter_nmi(regs);
958 	trace_android_rvh_handle_bad_stack(regs, esr, far);
959 	panic_bad_stack(regs, esr, far);
960 }
961 #endif /* CONFIG_VMAP_STACK */
962 
963 #ifdef CONFIG_ARM_SDE_INTERFACE
964 asmlinkage noinstr unsigned long
__sdei_handler(struct pt_regs * regs,struct sdei_registered_event * arg)965 __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
966 {
967 	unsigned long ret;
968 
969 	/*
970 	 * We didn't take an exception to get here, so the HW hasn't
971 	 * set/cleared bits in PSTATE that we may rely on.
972 	 *
973 	 * The original SDEI spec (ARM DEN 0054A) can be read ambiguously as to
974 	 * whether PSTATE bits are inherited unchanged or generated from
975 	 * scratch, and the TF-A implementation always clears PAN and always
976 	 * clears UAO. There are no other known implementations.
977 	 *
978 	 * Subsequent revisions (ARM DEN 0054B) follow the usual rules for how
979 	 * PSTATE is modified upon architectural exceptions, and so PAN is
980 	 * either inherited or set per SCTLR_ELx.SPAN, and UAO is always
981 	 * cleared.
982 	 *
983 	 * We must explicitly reset PAN to the expected state, including
984 	 * clearing it when the host isn't using it, in case a VM had it set.
985 	 */
986 	if (system_uses_hw_pan())
987 		set_pstate_pan(1);
988 	else if (cpu_has_pan())
989 		set_pstate_pan(0);
990 
991 	arm64_enter_nmi(regs);
992 	ret = do_sdei_event(regs, arg);
993 	arm64_exit_nmi(regs);
994 
995 	return ret;
996 }
997 #endif /* CONFIG_ARM_SDE_INTERFACE */
998