xref: /linux/arch/arm64/kernel/entry-common.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
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/irq-entry-common.h>
10 #include <linux/kasan.h>
11 #include <linux/linkage.h>
12 #include <linux/livepatch.h>
13 #include <linux/lockdep.h>
14 #include <linux/ptrace.h>
15 #include <linux/resume_user_mode.h>
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/thread_info.h>
19 
20 #include <asm/cpufeature.h>
21 #include <asm/daifflags.h>
22 #include <asm/esr.h>
23 #include <asm/exception.h>
24 #include <asm/fpsimd.h>
25 #include <asm/irq_regs.h>
26 #include <asm/kprobes.h>
27 #include <asm/mmu.h>
28 #include <asm/percpu.h>
29 #include <asm/processor.h>
30 #include <asm/sdei.h>
31 #include <asm/stacktrace.h>
32 #include <asm/sysreg.h>
33 #include <asm/system_misc.h>
34 #include <asm/vectors.h>
35 
36 /*
37  * Where the context being returned to had an active percpu GPR critical
38  * section, ensure that the offset and address GPRs are updated to match the
39  * current CPU.
40  *
41  * For simplicity we always update the GPRs when a critical section is active
42  * and preemption was *possible*, regardless of whether preemption actually
43  * occurred. Where preemption did not occur, the updates are redundant but not
44  * harmful.
45  */
46 static __always_inline void irqentry_exit_pcpu_adjust(struct pt_regs *regs)
47 {
48 	int reg_pcp, reg_off, reg_addr;
49 	unsigned long pcp, off, addr;
50 	u16 gprs = regs->pcpu_gprs;
51 
52 	/*
53 	 * Zero means no active PCPU GPRs. As the PCPU GPRs must be distinct,
54 	 * a PCPU critical section cannot possibly use {x0,x0,x0}.
55 	 */
56 	if (likely(!gprs))
57 		return;
58 
59 	reg_pcp  = FIELD_GET(PCPU_GPR_PCP,  gprs);
60 	reg_off  = FIELD_GET(PCPU_GPR_OFF,  gprs);
61 	reg_addr = FIELD_GET(PCPU_GPR_ADDR, gprs);
62 
63 	pcp = pt_regs_read_reg(regs, reg_pcp);
64 
65 	off = __kern_my_cpu_offset();
66 	pt_regs_write_reg(regs, reg_off, off);
67 
68 	addr = pcp + off;
69 	pt_regs_write_reg(regs, reg_addr, addr);
70 }
71 
72 /*
73  * Handle IRQ/context state management when entering from kernel mode.
74  * Before this function is called it is not safe to call regular kernel code,
75  * instrumentable code, or any code which may trigger an exception.
76  */
77 static noinstr irqentry_state_t arm64_enter_from_kernel_mode(struct pt_regs *regs)
78 {
79 	irqentry_state_t state;
80 
81 	state = irqentry_enter_from_kernel_mode(regs);
82 	mte_check_tfsr_entry();
83 	mte_disable_tco_entry(current);
84 
85 	return state;
86 }
87 
88 /*
89  * Handle IRQ/context state management when exiting to kernel mode.
90  * After this function returns it is not safe to call regular kernel code,
91  * instrumentable code, or any code which may trigger an exception.
92  */
93 static void noinstr __arm64_exit_to_kernel_mode(struct pt_regs *regs,
94 						irqentry_state_t state)
95 {
96 	local_daif_mask();
97 	irqentry_exit_pcpu_adjust(regs);
98 	mte_check_tfsr_exit();
99 	irqentry_exit_to_kernel_mode_after_preempt(regs, state);
100 }
101 
102 /*
103  * We are returning from the context which allows involuntary kernel preemption
104  */
105 static void noinstr arm64_exit_to_kernel_mode_preempt(struct pt_regs *regs,
106 						      irqentry_state_t state)
107 {
108 	irqentry_exit_to_kernel_mode_preempt(regs, state);
109 	__arm64_exit_to_kernel_mode(regs, state);
110 }
111 
112 static void noinstr arm64_exit_to_kernel_mode(struct pt_regs *regs,
113 					      irqentry_state_t state)
114 {
115 	if (!regs_irqs_disabled(regs)) {
116 		local_irq_disable();
117 		arm64_exit_to_kernel_mode_preempt(regs, state);
118 		return;
119 	}
120 
121 	__arm64_exit_to_kernel_mode(regs, state);
122 }
123 
124 static __always_inline void arm64_syscall_enter_from_user_mode(struct pt_regs *regs)
125 {
126 	enter_from_user_mode(regs);
127 	mte_disable_tco_entry(current);
128 	sme_enter_from_user_mode();
129 }
130 
131 /*
132  * Handle IRQ/context state management when entering from user mode.
133  * Before this function is called it is not safe to call regular kernel code,
134  * instrumentable code, or any code which may trigger an exception.
135  */
136 static __always_inline void arm64_enter_from_user_mode(struct pt_regs *regs)
137 {
138 	enter_from_user_mode(regs);
139 	rseq_note_user_irq_entry();
140 	mte_disable_tco_entry(current);
141 	sme_enter_from_user_mode();
142 }
143 
144 static __always_inline void arm64_syscall_exit_to_user_mode(struct pt_regs *regs)
145 {
146 	local_irq_disable();
147 	syscall_exit_to_user_mode_prepare(regs);
148 	local_daif_mask();
149 	sme_exit_to_user_mode();
150 	mte_check_tfsr_exit();
151 	exit_to_user_mode();
152 }
153 
154 /*
155  * Handle IRQ/context state management when exiting to user mode.
156  * After this function returns it is not safe to call regular kernel code,
157  * instrumentable code, or any code which may trigger an exception.
158  */
159 static __always_inline void arm64_exit_to_user_mode(struct pt_regs *regs)
160 {
161 	local_irq_disable();
162 	irqentry_exit_to_user_mode_prepare(regs);
163 	local_daif_mask();
164 	sme_exit_to_user_mode();
165 	mte_check_tfsr_exit();
166 	exit_to_user_mode();
167 }
168 
169 asmlinkage void noinstr asm_exit_to_user_mode(struct pt_regs *regs)
170 {
171 	arm64_syscall_exit_to_user_mode(regs);
172 }
173 
174 /*
175  * Handle IRQ/context state management when entering a debug exception from
176  * kernel mode. Before this function is called it is not safe to call regular
177  * kernel code, instrumentable code, or any code which may trigger an exception.
178  */
179 static noinstr irqentry_state_t arm64_enter_el1_dbg(struct pt_regs *regs)
180 {
181 	irqentry_state_t state;
182 
183 	state.lockdep = lockdep_hardirqs_enabled();
184 
185 	lockdep_hardirqs_off(CALLER_ADDR0);
186 	ct_nmi_enter();
187 
188 	trace_hardirqs_off_finish();
189 
190 	return state;
191 }
192 
193 /*
194  * Handle IRQ/context state management when exiting a debug exception from
195  * kernel mode. After this function returns it is not safe to call regular
196  * kernel code, instrumentable code, or any code which may trigger an exception.
197  */
198 static void noinstr arm64_exit_el1_dbg(struct pt_regs *regs,
199 				       irqentry_state_t state)
200 {
201 	if (state.lockdep) {
202 		trace_hardirqs_on_prepare();
203 		lockdep_hardirqs_on_prepare();
204 	}
205 
206 	ct_nmi_exit();
207 	if (state.lockdep)
208 		lockdep_hardirqs_on(CALLER_ADDR0);
209 }
210 
211 static void do_interrupt_handler(struct pt_regs *regs,
212 				 void (*handler)(struct pt_regs *))
213 {
214 	struct pt_regs *old_regs = set_irq_regs(regs);
215 
216 	if (on_thread_stack())
217 		call_on_irq_stack(regs, handler);
218 	else
219 		handler(regs);
220 
221 	set_irq_regs(old_regs);
222 }
223 
224 extern void (*handle_arch_irq)(struct pt_regs *);
225 extern void (*handle_arch_fiq)(struct pt_regs *);
226 
227 static void noinstr __panic_unhandled(struct pt_regs *regs, const char *vector,
228 				      unsigned long esr)
229 {
230 	irqentry_nmi_enter(regs);
231 
232 	console_verbose();
233 
234 	pr_crit("Unhandled %s exception on CPU%d, ESR 0x%016lx -- %s\n",
235 		vector, smp_processor_id(), esr,
236 		esr_get_class_string(esr));
237 
238 	__show_regs(regs);
239 	panic("Unhandled exception");
240 }
241 
242 #define UNHANDLED(el, regsize, vector)							\
243 asmlinkage void noinstr el##_##regsize##_##vector##_handler(struct pt_regs *regs)	\
244 {											\
245 	const char *desc = #regsize "-bit " #el " " #vector;				\
246 	__panic_unhandled(regs, desc, read_sysreg(esr_el1));				\
247 }
248 
249 #ifdef CONFIG_ARM64_ERRATUM_1463225
250 static DEFINE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
251 
252 static void cortex_a76_erratum_1463225_svc_handler(void)
253 {
254 	u64 reg, val;
255 
256 	if (!unlikely(test_thread_flag(TIF_SINGLESTEP)))
257 		return;
258 
259 	if (!unlikely(this_cpu_has_cap(ARM64_WORKAROUND_1463225)))
260 		return;
261 
262 	__this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 1);
263 	reg = read_sysreg(mdscr_el1);
264 	val = reg | MDSCR_EL1_SS | MDSCR_EL1_KDE;
265 	write_sysreg(val, mdscr_el1);
266 	asm volatile("msr daifclr, #8");
267 	isb();
268 
269 	/* We will have taken a single-step exception by this point */
270 
271 	write_sysreg(reg, mdscr_el1);
272 	__this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 0);
273 }
274 
275 static __always_inline bool
276 cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
277 {
278 	if (!__this_cpu_read(__in_cortex_a76_erratum_1463225_wa))
279 		return false;
280 
281 	/*
282 	 * We've taken a dummy step exception from the kernel to ensure
283 	 * that interrupts are re-enabled on the syscall path. Return back
284 	 * to cortex_a76_erratum_1463225_svc_handler() with debug exceptions
285 	 * masked so that we can safely restore the mdscr and get on with
286 	 * handling the syscall.
287 	 */
288 	regs->pstate |= PSR_D_BIT;
289 	return true;
290 }
291 #else /* CONFIG_ARM64_ERRATUM_1463225 */
292 static void cortex_a76_erratum_1463225_svc_handler(void) { }
293 static bool cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
294 {
295 	return false;
296 }
297 #endif /* CONFIG_ARM64_ERRATUM_1463225 */
298 
299 /*
300  * As per the ABI exit SME streaming mode and clear the SVE state not
301  * shared with FPSIMD on syscall entry.
302  */
303 static inline void fpsimd_syscall_enter(void)
304 {
305 	/* Ensure PSTATE.SM is clear, but leave PSTATE.ZA as-is. */
306 	if (system_supports_sme())
307 		sme_smstop_sm();
308 
309 	/*
310 	 * The CPU is not in streaming mode. If non-streaming SVE is not
311 	 * supported, there is no SVE state that needs to be discarded.
312 	 */
313 	if (!system_supports_sve())
314 		return;
315 
316 	if (test_thread_flag(TIF_SVE))
317 		sve_flush_live();
318 
319 	/*
320 	 * Any live non-FPSIMD SVE state has been zeroed. Allow
321 	 * fpsimd_save_user_state() to lazily discard SVE state until either
322 	 * the live state is unbound or fpsimd_syscall_exit() is called.
323 	 */
324 	__this_cpu_write(fpsimd_last_state.to_save, FP_STATE_FPSIMD);
325 }
326 
327 static __always_inline void fpsimd_syscall_exit(void)
328 {
329 	if (!system_supports_sve())
330 		return;
331 
332 	/*
333 	 * The current task's user FPSIMD/SVE/SME state is now bound to this
334 	 * CPU. The fpsimd_last_state.to_save value is either:
335 	 *
336 	 * - FP_STATE_FPSIMD, if the state has not been reloaded on this CPU
337 	 *   since fpsimd_syscall_enter().
338 	 *
339 	 * - FP_STATE_CURRENT, if the state has been reloaded on this CPU at
340 	 *   any point.
341 	 *
342 	 * Reset this to FP_STATE_CURRENT to stop lazy discarding.
343 	 */
344 	__this_cpu_write(fpsimd_last_state.to_save, FP_STATE_CURRENT);
345 }
346 
347 /*
348  * In debug exception context, we explicitly disable preemption despite
349  * having interrupts disabled.
350  * This serves two purposes: it makes it much less likely that we would
351  * accidentally schedule in exception context and it will force a warning
352  * if we somehow manage to schedule by accident.
353  */
354 static void debug_exception_enter(struct pt_regs *regs)
355 {
356 	preempt_disable();
357 
358 	/* This code is a bit fragile.  Test it. */
359 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "exception_enter didn't work");
360 }
361 NOKPROBE_SYMBOL(debug_exception_enter);
362 
363 static void debug_exception_exit(struct pt_regs *regs)
364 {
365 	preempt_enable_no_resched();
366 }
367 NOKPROBE_SYMBOL(debug_exception_exit);
368 
369 UNHANDLED(el1t, 64, sync)
370 UNHANDLED(el1t, 64, irq)
371 UNHANDLED(el1t, 64, fiq)
372 UNHANDLED(el1t, 64, error)
373 
374 static void noinstr el1_abort(struct pt_regs *regs, unsigned long esr)
375 {
376 	unsigned long far = read_sysreg(far_el1);
377 	irqentry_state_t state;
378 
379 	state = arm64_enter_from_kernel_mode(regs);
380 	local_daif_inherit(regs);
381 	do_mem_abort(far, esr, regs);
382 	arm64_exit_to_kernel_mode(regs, state);
383 }
384 
385 static void noinstr el1_pc(struct pt_regs *regs, unsigned long esr)
386 {
387 	unsigned long far = read_sysreg(far_el1);
388 	irqentry_state_t state;
389 
390 	state = arm64_enter_from_kernel_mode(regs);
391 	local_daif_inherit(regs);
392 	do_sp_pc_abort(far, esr, regs);
393 	arm64_exit_to_kernel_mode(regs, state);
394 }
395 
396 static void noinstr el1_undef(struct pt_regs *regs, unsigned long esr)
397 {
398 	irqentry_state_t state;
399 
400 	state = arm64_enter_from_kernel_mode(regs);
401 	local_daif_inherit(regs);
402 	do_el1_undef(regs, esr);
403 	arm64_exit_to_kernel_mode(regs, state);
404 }
405 
406 static void noinstr el1_bti(struct pt_regs *regs, unsigned long esr)
407 {
408 	irqentry_state_t state;
409 
410 	state = arm64_enter_from_kernel_mode(regs);
411 	local_daif_inherit(regs);
412 	do_el1_bti(regs, esr);
413 	arm64_exit_to_kernel_mode(regs, state);
414 }
415 
416 static void noinstr el1_gcs(struct pt_regs *regs, unsigned long esr)
417 {
418 	irqentry_state_t state;
419 
420 	state = arm64_enter_from_kernel_mode(regs);
421 	local_daif_inherit(regs);
422 	do_el1_gcs(regs, esr);
423 	arm64_exit_to_kernel_mode(regs, state);
424 }
425 
426 static void noinstr el1_mops(struct pt_regs *regs, unsigned long esr)
427 {
428 	irqentry_state_t state;
429 
430 	state = arm64_enter_from_kernel_mode(regs);
431 	local_daif_inherit(regs);
432 	do_el1_mops(regs, esr);
433 	arm64_exit_to_kernel_mode(regs, state);
434 }
435 
436 static void noinstr el1_breakpt(struct pt_regs *regs, unsigned long esr)
437 {
438 	irqentry_state_t state;
439 
440 	state = arm64_enter_el1_dbg(regs);
441 	debug_exception_enter(regs);
442 	do_breakpoint(esr, regs);
443 	debug_exception_exit(regs);
444 	arm64_exit_el1_dbg(regs, state);
445 }
446 
447 static void noinstr el1_softstp(struct pt_regs *regs, unsigned long esr)
448 {
449 	irqentry_state_t state;
450 
451 	state = arm64_enter_el1_dbg(regs);
452 	if (!cortex_a76_erratum_1463225_debug_handler(regs)) {
453 		debug_exception_enter(regs);
454 		/*
455 		 * After handling a breakpoint, we suspend the breakpoint
456 		 * and use single-step to move to the next instruction.
457 		 * If we are stepping a suspended breakpoint there's nothing more to do:
458 		 * the single-step is complete.
459 		 */
460 		if (!try_step_suspended_breakpoints(regs))
461 			do_el1_softstep(esr, regs);
462 		debug_exception_exit(regs);
463 	}
464 	arm64_exit_el1_dbg(regs, state);
465 }
466 
467 static void noinstr el1_watchpt(struct pt_regs *regs, unsigned long esr)
468 {
469 	/* Watchpoints are the only debug exception to write FAR_EL1 */
470 	unsigned long far = read_sysreg(far_el1);
471 	irqentry_state_t state;
472 
473 	state = arm64_enter_el1_dbg(regs);
474 	debug_exception_enter(regs);
475 	do_watchpoint(far, esr, regs);
476 	debug_exception_exit(regs);
477 	arm64_exit_el1_dbg(regs, state);
478 }
479 
480 static void noinstr el1_brk64(struct pt_regs *regs, unsigned long esr)
481 {
482 	irqentry_state_t state;
483 
484 	state = arm64_enter_el1_dbg(regs);
485 	debug_exception_enter(regs);
486 	do_el1_brk64(esr, regs);
487 	debug_exception_exit(regs);
488 	arm64_exit_el1_dbg(regs, state);
489 }
490 
491 static void noinstr el1_fpac(struct pt_regs *regs, unsigned long esr)
492 {
493 	irqentry_state_t state;
494 
495 	state = arm64_enter_from_kernel_mode(regs);
496 	local_daif_inherit(regs);
497 	do_el1_fpac(regs, esr);
498 	arm64_exit_to_kernel_mode(regs, state);
499 }
500 
501 asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)
502 {
503 	unsigned long esr = read_sysreg(esr_el1);
504 
505 	switch (ESR_ELx_EC(esr)) {
506 	case ESR_ELx_EC_DABT_CUR:
507 	case ESR_ELx_EC_IABT_CUR:
508 		el1_abort(regs, esr);
509 		break;
510 	/*
511 	 * We don't handle ESR_ELx_EC_SP_ALIGN, since we will have hit a
512 	 * recursive exception when trying to push the initial pt_regs.
513 	 */
514 	case ESR_ELx_EC_PC_ALIGN:
515 		el1_pc(regs, esr);
516 		break;
517 	case ESR_ELx_EC_SYS64:
518 	case ESR_ELx_EC_UNKNOWN:
519 		el1_undef(regs, esr);
520 		break;
521 	case ESR_ELx_EC_BTI:
522 		el1_bti(regs, esr);
523 		break;
524 	case ESR_ELx_EC_GCS:
525 		el1_gcs(regs, esr);
526 		break;
527 	case ESR_ELx_EC_MOPS:
528 		el1_mops(regs, esr);
529 		break;
530 	case ESR_ELx_EC_BREAKPT_CUR:
531 		el1_breakpt(regs, esr);
532 		break;
533 	case ESR_ELx_EC_SOFTSTP_CUR:
534 		el1_softstp(regs, esr);
535 		break;
536 	case ESR_ELx_EC_WATCHPT_CUR:
537 		el1_watchpt(regs, esr);
538 		break;
539 	case ESR_ELx_EC_BRK64:
540 		el1_brk64(regs, esr);
541 		break;
542 	case ESR_ELx_EC_FPAC:
543 		el1_fpac(regs, esr);
544 		break;
545 	default:
546 		__panic_unhandled(regs, "64-bit el1h sync", esr);
547 	}
548 }
549 
550 static __always_inline void __el1_pnmi(struct pt_regs *regs,
551 				       void (*handler)(struct pt_regs *))
552 {
553 	irqentry_state_t state;
554 
555 	state = irqentry_nmi_enter(regs);
556 	do_interrupt_handler(regs, handler);
557 	local_daif_mask();
558 	irqentry_nmi_exit(regs, state);
559 }
560 
561 static __always_inline void __el1_irq(struct pt_regs *regs,
562 				      void (*handler)(struct pt_regs *))
563 {
564 	irqentry_state_t state;
565 
566 	state = arm64_enter_from_kernel_mode(regs);
567 
568 	irq_enter_rcu();
569 	do_interrupt_handler(regs, handler);
570 	irq_exit_rcu();
571 
572 	arm64_exit_to_kernel_mode_preempt(regs, state);
573 }
574 static void noinstr el1_interrupt(struct pt_regs *regs,
575 				  void (*handler)(struct pt_regs *))
576 {
577 	write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
578 
579 	if (IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) && regs_irqs_disabled(regs))
580 		__el1_pnmi(regs, handler);
581 	else
582 		__el1_irq(regs, handler);
583 }
584 
585 asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs)
586 {
587 	el1_interrupt(regs, handle_arch_irq);
588 }
589 
590 asmlinkage void noinstr el1h_64_fiq_handler(struct pt_regs *regs)
591 {
592 	el1_interrupt(regs, handle_arch_fiq);
593 }
594 
595 asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs)
596 {
597 	unsigned long esr = read_sysreg(esr_el1);
598 	irqentry_state_t state;
599 
600 	local_daif_restore(DAIF_ERRCTX);
601 	state = irqentry_nmi_enter(regs);
602 	do_serror(regs, esr);
603 	local_daif_mask();
604 	irqentry_nmi_exit(regs, state);
605 }
606 
607 static void noinstr el0_da(struct pt_regs *regs, unsigned long esr)
608 {
609 	unsigned long far = read_sysreg(far_el1);
610 
611 	arm64_enter_from_user_mode(regs);
612 	local_daif_restore(DAIF_PROCCTX);
613 	do_mem_abort(far, esr, regs);
614 	arm64_exit_to_user_mode(regs);
615 }
616 
617 static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr)
618 {
619 	unsigned long far = read_sysreg(far_el1);
620 
621 	/*
622 	 * We've taken an instruction abort from userspace and not yet
623 	 * re-enabled IRQs. If the address is a kernel address, apply
624 	 * BP hardening prior to enabling IRQs and pre-emption.
625 	 */
626 	if (!is_ttbr0_addr(far))
627 		arm64_apply_bp_hardening();
628 
629 	arm64_enter_from_user_mode(regs);
630 	local_daif_restore(DAIF_PROCCTX);
631 	do_mem_abort(far, esr, regs);
632 	arm64_exit_to_user_mode(regs);
633 }
634 
635 static void noinstr el0_fpsimd_acc(struct pt_regs *regs, unsigned long esr)
636 {
637 	arm64_enter_from_user_mode(regs);
638 	local_daif_restore(DAIF_PROCCTX);
639 	do_fpsimd_acc(esr, regs);
640 	arm64_exit_to_user_mode(regs);
641 }
642 
643 static void noinstr el0_sve_acc(struct pt_regs *regs, unsigned long esr)
644 {
645 	arm64_enter_from_user_mode(regs);
646 	local_daif_restore(DAIF_PROCCTX);
647 	do_sve_acc(esr, regs);
648 	arm64_exit_to_user_mode(regs);
649 }
650 
651 static void noinstr el0_sme_acc(struct pt_regs *regs, unsigned long esr)
652 {
653 	arm64_enter_from_user_mode(regs);
654 	local_daif_restore(DAIF_PROCCTX);
655 	do_sme_acc(esr, regs);
656 	arm64_exit_to_user_mode(regs);
657 }
658 
659 static void noinstr el0_fpsimd_exc(struct pt_regs *regs, unsigned long esr)
660 {
661 	arm64_enter_from_user_mode(regs);
662 	local_daif_restore(DAIF_PROCCTX);
663 	do_fpsimd_exc(esr, regs);
664 	arm64_exit_to_user_mode(regs);
665 }
666 
667 static void noinstr el0_sys(struct pt_regs *regs, unsigned long esr)
668 {
669 	arm64_enter_from_user_mode(regs);
670 	local_daif_restore(DAIF_PROCCTX);
671 	do_el0_sys(esr, regs);
672 	arm64_exit_to_user_mode(regs);
673 }
674 
675 static void noinstr el0_pc(struct pt_regs *regs, unsigned long esr)
676 {
677 	unsigned long far = read_sysreg(far_el1);
678 
679 	if (!is_ttbr0_addr(instruction_pointer(regs)))
680 		arm64_apply_bp_hardening();
681 
682 	arm64_enter_from_user_mode(regs);
683 	local_daif_restore(DAIF_PROCCTX);
684 	do_sp_pc_abort(far, esr, regs);
685 	arm64_exit_to_user_mode(regs);
686 }
687 
688 static void noinstr el0_sp(struct pt_regs *regs, unsigned long esr)
689 {
690 	arm64_enter_from_user_mode(regs);
691 	local_daif_restore(DAIF_PROCCTX);
692 	do_sp_pc_abort(regs->sp, esr, regs);
693 	arm64_exit_to_user_mode(regs);
694 }
695 
696 static void noinstr el0_undef(struct pt_regs *regs, unsigned long esr)
697 {
698 	arm64_enter_from_user_mode(regs);
699 	local_daif_restore(DAIF_PROCCTX);
700 	do_el0_undef(regs, esr);
701 	arm64_exit_to_user_mode(regs);
702 }
703 
704 static void noinstr el0_bti(struct pt_regs *regs)
705 {
706 	arm64_enter_from_user_mode(regs);
707 	local_daif_restore(DAIF_PROCCTX);
708 	do_el0_bti(regs);
709 	arm64_exit_to_user_mode(regs);
710 }
711 
712 static void noinstr el0_mops(struct pt_regs *regs, unsigned long esr)
713 {
714 	arm64_enter_from_user_mode(regs);
715 	local_daif_restore(DAIF_PROCCTX);
716 	do_el0_mops(regs, esr);
717 	arm64_exit_to_user_mode(regs);
718 }
719 
720 static void noinstr el0_gcs(struct pt_regs *regs, unsigned long esr)
721 {
722 	arm64_enter_from_user_mode(regs);
723 	local_daif_restore(DAIF_PROCCTX);
724 	do_el0_gcs(regs, esr);
725 	arm64_exit_to_user_mode(regs);
726 }
727 
728 static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr)
729 {
730 	arm64_enter_from_user_mode(regs);
731 	local_daif_restore(DAIF_PROCCTX);
732 	bad_el0_sync(regs, 0, esr);
733 	arm64_exit_to_user_mode(regs);
734 }
735 
736 static void noinstr el0_breakpt(struct pt_regs *regs, unsigned long esr)
737 {
738 	if (!is_ttbr0_addr(regs->pc))
739 		arm64_apply_bp_hardening();
740 
741 	arm64_enter_from_user_mode(regs);
742 	debug_exception_enter(regs);
743 	do_breakpoint(esr, regs);
744 	debug_exception_exit(regs);
745 	local_daif_restore(DAIF_PROCCTX);
746 	arm64_exit_to_user_mode(regs);
747 }
748 
749 static void noinstr el0_softstp(struct pt_regs *regs, unsigned long esr)
750 {
751 	bool step_done;
752 
753 	if (!is_ttbr0_addr(regs->pc))
754 		arm64_apply_bp_hardening();
755 
756 	arm64_enter_from_user_mode(regs);
757 	/*
758 	 * After handling a breakpoint, we suspend the breakpoint
759 	 * and use single-step to move to the next instruction.
760 	 * If we are stepping a suspended breakpoint there's nothing more to do:
761 	 * the single-step is complete.
762 	 */
763 	step_done = try_step_suspended_breakpoints(regs);
764 	local_daif_restore(DAIF_PROCCTX);
765 	if (!step_done)
766 		do_el0_softstep(esr, regs);
767 	arm64_exit_to_user_mode(regs);
768 }
769 
770 static void noinstr el0_watchpt(struct pt_regs *regs, unsigned long esr)
771 {
772 	/* Watchpoints are the only debug exception to write FAR_EL1 */
773 	unsigned long far = read_sysreg(far_el1);
774 
775 	arm64_enter_from_user_mode(regs);
776 	debug_exception_enter(regs);
777 	do_watchpoint(far, esr, regs);
778 	debug_exception_exit(regs);
779 	local_daif_restore(DAIF_PROCCTX);
780 	arm64_exit_to_user_mode(regs);
781 }
782 
783 static void noinstr el0_brk64(struct pt_regs *regs, unsigned long esr)
784 {
785 	arm64_enter_from_user_mode(regs);
786 	local_daif_restore(DAIF_PROCCTX);
787 	do_el0_brk64(esr, regs);
788 	arm64_exit_to_user_mode(regs);
789 }
790 
791 static void noinstr el0_svc(struct pt_regs *regs)
792 {
793 	arm64_syscall_enter_from_user_mode(regs);
794 	cortex_a76_erratum_1463225_svc_handler();
795 	fpsimd_syscall_enter();
796 	local_daif_restore(DAIF_PROCCTX);
797 	do_el0_svc(regs);
798 	arm64_syscall_exit_to_user_mode(regs);
799 	fpsimd_syscall_exit();
800 }
801 
802 static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr)
803 {
804 	arm64_enter_from_user_mode(regs);
805 	local_daif_restore(DAIF_PROCCTX);
806 	do_el0_fpac(regs, esr);
807 	arm64_exit_to_user_mode(regs);
808 }
809 
810 asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)
811 {
812 	unsigned long esr = read_sysreg(esr_el1);
813 
814 	switch (ESR_ELx_EC(esr)) {
815 	case ESR_ELx_EC_SVC64:
816 		el0_svc(regs);
817 		break;
818 	case ESR_ELx_EC_DABT_LOW:
819 		el0_da(regs, esr);
820 		break;
821 	case ESR_ELx_EC_IABT_LOW:
822 		el0_ia(regs, esr);
823 		break;
824 	case ESR_ELx_EC_FP_ASIMD:
825 		el0_fpsimd_acc(regs, esr);
826 		break;
827 	case ESR_ELx_EC_SVE:
828 		el0_sve_acc(regs, esr);
829 		break;
830 	case ESR_ELx_EC_SME:
831 		el0_sme_acc(regs, esr);
832 		break;
833 	case ESR_ELx_EC_FP_EXC64:
834 		el0_fpsimd_exc(regs, esr);
835 		break;
836 	case ESR_ELx_EC_SYS64:
837 	case ESR_ELx_EC_WFx:
838 		el0_sys(regs, esr);
839 		break;
840 	case ESR_ELx_EC_SP_ALIGN:
841 		el0_sp(regs, esr);
842 		break;
843 	case ESR_ELx_EC_PC_ALIGN:
844 		el0_pc(regs, esr);
845 		break;
846 	case ESR_ELx_EC_UNKNOWN:
847 		el0_undef(regs, esr);
848 		break;
849 	case ESR_ELx_EC_BTI:
850 		el0_bti(regs);
851 		break;
852 	case ESR_ELx_EC_MOPS:
853 		el0_mops(regs, esr);
854 		break;
855 	case ESR_ELx_EC_GCS:
856 		el0_gcs(regs, esr);
857 		break;
858 	case ESR_ELx_EC_BREAKPT_LOW:
859 		el0_breakpt(regs, esr);
860 		break;
861 	case ESR_ELx_EC_SOFTSTP_LOW:
862 		el0_softstp(regs, esr);
863 		break;
864 	case ESR_ELx_EC_WATCHPT_LOW:
865 		el0_watchpt(regs, esr);
866 		break;
867 	case ESR_ELx_EC_BRK64:
868 		el0_brk64(regs, esr);
869 		break;
870 	case ESR_ELx_EC_FPAC:
871 		el0_fpac(regs, esr);
872 		break;
873 	default:
874 		el0_inv(regs, esr);
875 	}
876 }
877 
878 static void noinstr el0_interrupt(struct pt_regs *regs,
879 				  void (*handler)(struct pt_regs *))
880 {
881 	arm64_enter_from_user_mode(regs);
882 
883 	write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
884 
885 	if (regs->pc & BIT(55))
886 		arm64_apply_bp_hardening();
887 
888 	irq_enter_rcu();
889 	do_interrupt_handler(regs, handler);
890 	irq_exit_rcu();
891 
892 	arm64_exit_to_user_mode(regs);
893 }
894 
895 static void noinstr __el0_irq_handler_common(struct pt_regs *regs)
896 {
897 	el0_interrupt(regs, handle_arch_irq);
898 }
899 
900 asmlinkage void noinstr el0t_64_irq_handler(struct pt_regs *regs)
901 {
902 	__el0_irq_handler_common(regs);
903 }
904 
905 static void noinstr __el0_fiq_handler_common(struct pt_regs *regs)
906 {
907 	el0_interrupt(regs, handle_arch_fiq);
908 }
909 
910 asmlinkage void noinstr el0t_64_fiq_handler(struct pt_regs *regs)
911 {
912 	__el0_fiq_handler_common(regs);
913 }
914 
915 static void noinstr __el0_error_handler_common(struct pt_regs *regs)
916 {
917 	unsigned long esr = read_sysreg(esr_el1);
918 	irqentry_state_t state;
919 
920 	arm64_enter_from_user_mode(regs);
921 	local_daif_restore(DAIF_ERRCTX);
922 	state = irqentry_nmi_enter(regs);
923 	do_serror(regs, esr);
924 	irqentry_nmi_exit(regs, state);
925 	local_daif_restore(DAIF_PROCCTX);
926 	arm64_exit_to_user_mode(regs);
927 }
928 
929 asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)
930 {
931 	__el0_error_handler_common(regs);
932 }
933 
934 #ifdef CONFIG_COMPAT
935 static void noinstr el0_cp15(struct pt_regs *regs, unsigned long esr)
936 {
937 	arm64_enter_from_user_mode(regs);
938 	local_daif_restore(DAIF_PROCCTX);
939 	do_el0_cp15(esr, regs);
940 	arm64_exit_to_user_mode(regs);
941 }
942 
943 static void noinstr el0_svc_compat(struct pt_regs *regs)
944 {
945 	arm64_syscall_enter_from_user_mode(regs);
946 	cortex_a76_erratum_1463225_svc_handler();
947 	local_daif_restore(DAIF_PROCCTX);
948 	do_el0_svc_compat(regs);
949 	arm64_syscall_exit_to_user_mode(regs);
950 }
951 
952 static void noinstr el0_bkpt32(struct pt_regs *regs, unsigned long esr)
953 {
954 	arm64_enter_from_user_mode(regs);
955 	local_daif_restore(DAIF_PROCCTX);
956 	do_bkpt32(esr, regs);
957 	arm64_exit_to_user_mode(regs);
958 }
959 
960 asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)
961 {
962 	unsigned long esr = read_sysreg(esr_el1);
963 
964 	switch (ESR_ELx_EC(esr)) {
965 	case ESR_ELx_EC_SVC32:
966 		el0_svc_compat(regs);
967 		break;
968 	case ESR_ELx_EC_DABT_LOW:
969 		el0_da(regs, esr);
970 		break;
971 	case ESR_ELx_EC_IABT_LOW:
972 		el0_ia(regs, esr);
973 		break;
974 	case ESR_ELx_EC_FP_ASIMD:
975 		el0_fpsimd_acc(regs, esr);
976 		break;
977 	case ESR_ELx_EC_FP_EXC32:
978 		el0_fpsimd_exc(regs, esr);
979 		break;
980 	case ESR_ELx_EC_PC_ALIGN:
981 		el0_pc(regs, esr);
982 		break;
983 	case ESR_ELx_EC_UNKNOWN:
984 	case ESR_ELx_EC_CP14_MR:
985 	case ESR_ELx_EC_CP14_LS:
986 	case ESR_ELx_EC_CP14_64:
987 		el0_undef(regs, esr);
988 		break;
989 	case ESR_ELx_EC_CP15_32:
990 	case ESR_ELx_EC_CP15_64:
991 		el0_cp15(regs, esr);
992 		break;
993 	case ESR_ELx_EC_BREAKPT_LOW:
994 		el0_breakpt(regs, esr);
995 		break;
996 	case ESR_ELx_EC_SOFTSTP_LOW:
997 		el0_softstp(regs, esr);
998 		break;
999 	case ESR_ELx_EC_WATCHPT_LOW:
1000 		el0_watchpt(regs, esr);
1001 		break;
1002 	case ESR_ELx_EC_BKPT32:
1003 		el0_bkpt32(regs, esr);
1004 		break;
1005 	default:
1006 		el0_inv(regs, esr);
1007 	}
1008 }
1009 
1010 asmlinkage void noinstr el0t_32_irq_handler(struct pt_regs *regs)
1011 {
1012 	__el0_irq_handler_common(regs);
1013 }
1014 
1015 asmlinkage void noinstr el0t_32_fiq_handler(struct pt_regs *regs)
1016 {
1017 	__el0_fiq_handler_common(regs);
1018 }
1019 
1020 asmlinkage void noinstr el0t_32_error_handler(struct pt_regs *regs)
1021 {
1022 	__el0_error_handler_common(regs);
1023 }
1024 #else /* CONFIG_COMPAT */
1025 UNHANDLED(el0t, 32, sync)
1026 UNHANDLED(el0t, 32, irq)
1027 UNHANDLED(el0t, 32, fiq)
1028 UNHANDLED(el0t, 32, error)
1029 #endif /* CONFIG_COMPAT */
1030 
1031 asmlinkage void noinstr __noreturn handle_bad_stack(struct pt_regs *regs)
1032 {
1033 	unsigned long esr = read_sysreg(esr_el1);
1034 	unsigned long far = read_sysreg(far_el1);
1035 
1036 	irqentry_nmi_enter(regs);
1037 	panic_bad_stack(regs, esr, far);
1038 }
1039 
1040 #ifdef CONFIG_ARM_SDE_INTERFACE
1041 asmlinkage noinstr unsigned long
1042 __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
1043 {
1044 	unsigned long elr, spsr, esr, far, vbar;
1045 	irqentry_state_t state;
1046 	unsigned long ret;
1047 
1048 	/*
1049 	 * We didn't take an exception to get here, so the HW hasn't
1050 	 * set/cleared bits in PSTATE that we may rely on.
1051 	 *
1052 	 * The original SDEI spec (ARM DEN 0054A) can be read ambiguously as to
1053 	 * whether PSTATE bits are inherited unchanged or generated from
1054 	 * scratch, and the TF-A implementation always clears PAN and always
1055 	 * clears UAO. There are no other known implementations.
1056 	 *
1057 	 * Subsequent revisions (ARM DEN 0054B) follow the usual rules for how
1058 	 * PSTATE is modified upon architectural exceptions, and so PAN is
1059 	 * either inherited or set per SCTLR_ELx.SPAN, and UAO is always
1060 	 * cleared.
1061 	 *
1062 	 * We must explicitly reset PAN to the expected state, including
1063 	 * clearing it when the host isn't using it, in case a VM had it set.
1064 	 */
1065 	if (system_uses_hw_pan())
1066 		set_pstate_pan(1);
1067 	else if (cpu_has_pan())
1068 		set_pstate_pan(0);
1069 
1070 	/*
1071 	 * Nothing saved the interrupted context's exception registers on the
1072 	 * way in, and it may be in the middle of exception entry or return
1073 	 * (SDEI ignores DAIF). Save them before anything can take an exception
1074 	 * and overwrite them.
1075 	 */
1076 	elr = read_sysreg(elr_el1);
1077 	spsr = read_sysreg(spsr_el1);
1078 	esr = read_sysreg(esr_el1);
1079 	far = read_sysreg(far_el1);
1080 
1081 	/*
1082 	 * With KPTI, VBAR_EL1 holds the trampoline vectors from the return to
1083 	 * EL0 until the next exception from EL0 maps the kernel. They only
1084 	 * handle exceptions from EL0: an exception taken here would run their
1085 	 * empty current-EL half and never come back. Switch to the kernel
1086 	 * vectors. The IRQ-like resume keeps using the interrupted VBAR, so it
1087 	 * still goes through the trampoline.
1088 	 */
1089 	vbar = read_sysreg(vbar_el1);
1090 	if (arm64_kernel_unmapped_at_el0() &&
1091 	    vbar == (unsigned long)raw_cpu_read(this_cpu_vector)) {
1092 		write_sysreg((unsigned long)vectors, vbar_el1);
1093 		isb();
1094 	}
1095 
1096 	state = irqentry_nmi_enter(regs);
1097 	ret = do_sdei_event(regs, arg, vbar);
1098 	irqentry_nmi_exit(regs, state);
1099 
1100 	/*
1101 	 * Put them back unconditionally. An unchanged ELR does not rule out a
1102 	 * nested exception: one may have returned to the same address.
1103 	 */
1104 	write_sysreg(elr, elr_el1);
1105 	write_sysreg(spsr, spsr_el1);
1106 	write_sysreg(esr, esr_el1);
1107 	write_sysreg(far, far_el1);
1108 	write_sysreg(vbar, vbar_el1);
1109 
1110 	return ret;
1111 }
1112 #endif /* CONFIG_ARM_SDE_INTERFACE */
1113