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