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