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