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 */
__enter_from_kernel_mode(struct pt_regs * regs)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
enter_from_kernel_mode(struct pt_regs * regs)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 */
__exit_to_kernel_mode(struct pt_regs * regs)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
exit_to_kernel_mode(struct pt_regs * regs)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 */
__enter_from_user_mode(void)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
enter_from_user_mode(struct pt_regs * regs)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 */
__exit_to_user_mode(void)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
exit_to_user_mode_prepare(struct pt_regs * regs)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
exit_to_user_mode(struct pt_regs * regs)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
asm_exit_to_user_mode(struct pt_regs * regs)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 */
arm64_enter_nmi(struct pt_regs * regs)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 */
arm64_exit_nmi(struct pt_regs * regs)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 */
arm64_enter_el1_dbg(struct pt_regs * regs)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 */
arm64_exit_el1_dbg(struct pt_regs * regs)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
arm64_preempt_schedule_irq(void)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
do_interrupt_handler(struct pt_regs * regs,void (* handler)(struct pt_regs *))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
__panic_unhandled(struct pt_regs * regs,const char * vector,unsigned long esr)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
cortex_a76_erratum_1463225_svc_handler(void)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
cortex_a76_erratum_1463225_debug_handler(struct pt_regs * regs)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 */
cortex_a76_erratum_1463225_svc_handler(void)351 static void cortex_a76_erratum_1463225_svc_handler(void) { }
cortex_a76_erratum_1463225_debug_handler(struct pt_regs * regs)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 /*
359 * As per the ABI exit SME streaming mode and clear the SVE state not
360 * shared with FPSIMD on syscall entry.
361 */
fpsimd_syscall_enter(void)362 static inline void fpsimd_syscall_enter(void)
363 {
364 /* Ensure PSTATE.SM is clear, but leave PSTATE.ZA as-is. */
365 if (system_supports_sme())
366 sme_smstop_sm();
367
368 /*
369 * The CPU is not in streaming mode. If non-streaming SVE is not
370 * supported, there is no SVE state that needs to be discarded.
371 */
372 if (!system_supports_sve())
373 return;
374
375 if (test_thread_flag(TIF_SVE)) {
376 unsigned int sve_vq_minus_one;
377
378 sve_vq_minus_one = sve_vq_from_vl(task_get_sve_vl(current)) - 1;
379 sve_flush_live(true, sve_vq_minus_one);
380 }
381
382 /*
383 * Any live non-FPSIMD SVE state has been zeroed. Allow
384 * fpsimd_save_user_state() to lazily discard SVE state until either
385 * the live state is unbound or fpsimd_syscall_exit() is called.
386 */
387 __this_cpu_write(fpsimd_last_state.to_save, FP_STATE_FPSIMD);
388 }
389
fpsimd_syscall_exit(void)390 static __always_inline void fpsimd_syscall_exit(void)
391 {
392 if (!system_supports_sve())
393 return;
394
395 /*
396 * The current task's user FPSIMD/SVE/SME state is now bound to this
397 * CPU. The fpsimd_last_state.to_save value is either:
398 *
399 * - FP_STATE_FPSIMD, if the state has not been reloaded on this CPU
400 * since fpsimd_syscall_enter().
401 *
402 * - FP_STATE_CURRENT, if the state has been reloaded on this CPU at
403 * any point.
404 *
405 * Reset this to FP_STATE_CURRENT to stop lazy discarding.
406 */
407 __this_cpu_write(fpsimd_last_state.to_save, FP_STATE_CURRENT);
408 }
409
410 UNHANDLED(el1t, 64, sync)
411 UNHANDLED(el1t, 64, irq)
412 UNHANDLED(el1t, 64, fiq)
413 UNHANDLED(el1t, 64, error)
414
el1_abort(struct pt_regs * regs,unsigned long esr)415 static void noinstr el1_abort(struct pt_regs *regs, unsigned long esr)
416 {
417 unsigned long far = read_sysreg(far_el1);
418
419 enter_from_kernel_mode(regs);
420 local_daif_inherit(regs);
421 do_mem_abort(far, esr, regs);
422 local_daif_mask();
423 exit_to_kernel_mode(regs);
424 }
425
el1_pc(struct pt_regs * regs,unsigned long esr)426 static void noinstr el1_pc(struct pt_regs *regs, unsigned long esr)
427 {
428 unsigned long far = read_sysreg(far_el1);
429
430 enter_from_kernel_mode(regs);
431 local_daif_inherit(regs);
432 do_sp_pc_abort(far, esr, regs);
433 local_daif_mask();
434 exit_to_kernel_mode(regs);
435 }
436
el1_undef(struct pt_regs * regs,unsigned long esr)437 static void noinstr el1_undef(struct pt_regs *regs, unsigned long esr)
438 {
439 enter_from_kernel_mode(regs);
440 local_daif_inherit(regs);
441 do_el1_undef(regs, esr);
442 local_daif_mask();
443 exit_to_kernel_mode(regs);
444 }
445
el1_bti(struct pt_regs * regs,unsigned long esr)446 static void noinstr el1_bti(struct pt_regs *regs, unsigned long esr)
447 {
448 enter_from_kernel_mode(regs);
449 local_daif_inherit(regs);
450 do_el1_bti(regs, esr);
451 local_daif_mask();
452 exit_to_kernel_mode(regs);
453 }
454
el1_dbg(struct pt_regs * regs,unsigned long esr)455 static void noinstr el1_dbg(struct pt_regs *regs, unsigned long esr)
456 {
457 unsigned long far = read_sysreg(far_el1);
458
459 arm64_enter_el1_dbg(regs);
460 if (!cortex_a76_erratum_1463225_debug_handler(regs))
461 do_debug_exception(far, esr, regs);
462 arm64_exit_el1_dbg(regs);
463 }
464
el1_fpac(struct pt_regs * regs,unsigned long esr)465 static void noinstr el1_fpac(struct pt_regs *regs, unsigned long esr)
466 {
467 enter_from_kernel_mode(regs);
468 local_daif_inherit(regs);
469 do_el1_fpac(regs, esr);
470 local_daif_mask();
471 exit_to_kernel_mode(regs);
472 }
473
el1h_64_sync_handler(struct pt_regs * regs)474 asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)
475 {
476 unsigned long esr = read_sysreg(esr_el1);
477
478 switch (ESR_ELx_EC(esr)) {
479 case ESR_ELx_EC_DABT_CUR:
480 case ESR_ELx_EC_IABT_CUR:
481 el1_abort(regs, esr);
482 break;
483 /*
484 * We don't handle ESR_ELx_EC_SP_ALIGN, since we will have hit a
485 * recursive exception when trying to push the initial pt_regs.
486 */
487 case ESR_ELx_EC_PC_ALIGN:
488 el1_pc(regs, esr);
489 break;
490 case ESR_ELx_EC_SYS64:
491 case ESR_ELx_EC_UNKNOWN:
492 el1_undef(regs, esr);
493 break;
494 case ESR_ELx_EC_BTI:
495 el1_bti(regs, esr);
496 break;
497 case ESR_ELx_EC_BREAKPT_CUR:
498 case ESR_ELx_EC_SOFTSTP_CUR:
499 case ESR_ELx_EC_WATCHPT_CUR:
500 case ESR_ELx_EC_BRK64:
501 el1_dbg(regs, esr);
502 break;
503 case ESR_ELx_EC_FPAC:
504 el1_fpac(regs, esr);
505 break;
506 default:
507 __panic_unhandled(regs, "64-bit el1h sync", esr);
508 }
509 }
510
__el1_pnmi(struct pt_regs * regs,void (* handler)(struct pt_regs *))511 static __always_inline void __el1_pnmi(struct pt_regs *regs,
512 void (*handler)(struct pt_regs *))
513 {
514 arm64_enter_nmi(regs);
515 do_interrupt_handler(regs, handler);
516 arm64_exit_nmi(regs);
517 }
518
__el1_irq(struct pt_regs * regs,void (* handler)(struct pt_regs *))519 static __always_inline void __el1_irq(struct pt_regs *regs,
520 void (*handler)(struct pt_regs *))
521 {
522 enter_from_kernel_mode(regs);
523
524 irq_enter_rcu();
525 do_interrupt_handler(regs, handler);
526 irq_exit_rcu();
527
528 arm64_preempt_schedule_irq();
529
530 exit_to_kernel_mode(regs);
531 }
el1_interrupt(struct pt_regs * regs,void (* handler)(struct pt_regs *))532 static void noinstr el1_interrupt(struct pt_regs *regs,
533 void (*handler)(struct pt_regs *))
534 {
535 write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
536
537 if (IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) && !interrupts_enabled(regs))
538 __el1_pnmi(regs, handler);
539 else
540 __el1_irq(regs, handler);
541 }
542
el1h_64_irq_handler(struct pt_regs * regs)543 asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs)
544 {
545 el1_interrupt(regs, handle_arch_irq);
546 }
547
el1h_64_fiq_handler(struct pt_regs * regs)548 asmlinkage void noinstr el1h_64_fiq_handler(struct pt_regs *regs)
549 {
550 el1_interrupt(regs, handle_arch_fiq);
551 }
552
el1h_64_error_handler(struct pt_regs * regs)553 asmlinkage void noinstr el1h_64_error_handler(struct pt_regs *regs)
554 {
555 unsigned long esr = read_sysreg(esr_el1);
556
557 local_daif_restore(DAIF_ERRCTX);
558 arm64_enter_nmi(regs);
559 do_serror(regs, esr);
560 arm64_exit_nmi(regs);
561 }
562
el0_da(struct pt_regs * regs,unsigned long esr)563 static void noinstr el0_da(struct pt_regs *regs, unsigned long esr)
564 {
565 unsigned long far = read_sysreg(far_el1);
566
567 enter_from_user_mode(regs);
568 local_daif_restore(DAIF_PROCCTX);
569 do_mem_abort(far, esr, regs);
570 exit_to_user_mode(regs);
571 }
572
el0_ia(struct pt_regs * regs,unsigned long esr)573 static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr)
574 {
575 unsigned long far = read_sysreg(far_el1);
576
577 /*
578 * We've taken an instruction abort from userspace and not yet
579 * re-enabled IRQs. If the address is a kernel address, apply
580 * BP hardening prior to enabling IRQs and pre-emption.
581 */
582 if (!is_ttbr0_addr(far))
583 arm64_apply_bp_hardening();
584
585 enter_from_user_mode(regs);
586 local_daif_restore(DAIF_PROCCTX);
587 do_mem_abort(far, esr, regs);
588 exit_to_user_mode(regs);
589 }
590
el0_fpsimd_acc(struct pt_regs * regs,unsigned long esr)591 static void noinstr el0_fpsimd_acc(struct pt_regs *regs, unsigned long esr)
592 {
593 enter_from_user_mode(regs);
594 local_daif_restore(DAIF_PROCCTX);
595 do_fpsimd_acc(esr, regs);
596 exit_to_user_mode(regs);
597 }
598
el0_sve_acc(struct pt_regs * regs,unsigned long esr)599 static void noinstr el0_sve_acc(struct pt_regs *regs, unsigned long esr)
600 {
601 enter_from_user_mode(regs);
602 local_daif_restore(DAIF_PROCCTX);
603 do_sve_acc(esr, regs);
604 exit_to_user_mode(regs);
605 }
606
el0_sme_acc(struct pt_regs * regs,unsigned long esr)607 static void noinstr el0_sme_acc(struct pt_regs *regs, unsigned long esr)
608 {
609 enter_from_user_mode(regs);
610 local_daif_restore(DAIF_PROCCTX);
611 do_sme_acc(esr, regs);
612 exit_to_user_mode(regs);
613 }
614
el0_fpsimd_exc(struct pt_regs * regs,unsigned long esr)615 static void noinstr el0_fpsimd_exc(struct pt_regs *regs, unsigned long esr)
616 {
617 enter_from_user_mode(regs);
618 local_daif_restore(DAIF_PROCCTX);
619 do_fpsimd_exc(esr, regs);
620 exit_to_user_mode(regs);
621 }
622
el0_sys(struct pt_regs * regs,unsigned long esr)623 static void noinstr el0_sys(struct pt_regs *regs, unsigned long esr)
624 {
625 enter_from_user_mode(regs);
626 local_daif_restore(DAIF_PROCCTX);
627 do_el0_sys(esr, regs);
628 exit_to_user_mode(regs);
629 }
630
el0_pc(struct pt_regs * regs,unsigned long esr)631 static void noinstr el0_pc(struct pt_regs *regs, unsigned long esr)
632 {
633 unsigned long far = read_sysreg(far_el1);
634
635 if (!is_ttbr0_addr(instruction_pointer(regs)))
636 arm64_apply_bp_hardening();
637
638 enter_from_user_mode(regs);
639 local_daif_restore(DAIF_PROCCTX);
640 do_sp_pc_abort(far, esr, regs);
641 exit_to_user_mode(regs);
642 }
643
el0_sp(struct pt_regs * regs,unsigned long esr)644 static void noinstr el0_sp(struct pt_regs *regs, unsigned long esr)
645 {
646 enter_from_user_mode(regs);
647 local_daif_restore(DAIF_PROCCTX);
648 do_sp_pc_abort(regs->sp, esr, regs);
649 exit_to_user_mode(regs);
650 }
651
el0_undef(struct pt_regs * regs,unsigned long esr)652 static void noinstr el0_undef(struct pt_regs *regs, unsigned long esr)
653 {
654 enter_from_user_mode(regs);
655 local_daif_restore(DAIF_PROCCTX);
656 do_el0_undef(regs, esr);
657 exit_to_user_mode(regs);
658 }
659
el0_bti(struct pt_regs * regs)660 static void noinstr el0_bti(struct pt_regs *regs)
661 {
662 enter_from_user_mode(regs);
663 local_daif_restore(DAIF_PROCCTX);
664 do_el0_bti(regs);
665 exit_to_user_mode(regs);
666 }
667
el0_mops(struct pt_regs * regs,unsigned long esr)668 static void noinstr el0_mops(struct pt_regs *regs, unsigned long esr)
669 {
670 enter_from_user_mode(regs);
671 local_daif_restore(DAIF_PROCCTX);
672 do_el0_mops(regs, esr);
673 exit_to_user_mode(regs);
674 }
675
el0_inv(struct pt_regs * regs,unsigned long esr)676 static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr)
677 {
678 enter_from_user_mode(regs);
679 local_daif_restore(DAIF_PROCCTX);
680 bad_el0_sync(regs, 0, esr);
681 exit_to_user_mode(regs);
682 }
683
el0_dbg(struct pt_regs * regs,unsigned long esr)684 static void noinstr el0_dbg(struct pt_regs *regs, unsigned long esr)
685 {
686 /* Only watchpoints write FAR_EL1, otherwise its UNKNOWN */
687 unsigned long far = read_sysreg(far_el1);
688
689 enter_from_user_mode(regs);
690 do_debug_exception(far, esr, regs);
691 local_daif_restore(DAIF_PROCCTX);
692 exit_to_user_mode(regs);
693 }
694
el0_svc(struct pt_regs * regs)695 static void noinstr el0_svc(struct pt_regs *regs)
696 {
697 enter_from_user_mode(regs);
698 cortex_a76_erratum_1463225_svc_handler();
699 fpsimd_syscall_enter();
700 local_daif_restore(DAIF_PROCCTX);
701 do_el0_svc(regs);
702 exit_to_user_mode(regs);
703 fpsimd_syscall_exit();
704 }
705
el0_fpac(struct pt_regs * regs,unsigned long esr)706 static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr)
707 {
708 enter_from_user_mode(regs);
709 local_daif_restore(DAIF_PROCCTX);
710 do_el0_fpac(regs, esr);
711 exit_to_user_mode(regs);
712 }
713
el0t_64_sync_handler(struct pt_regs * regs)714 asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)
715 {
716 unsigned long esr = read_sysreg(esr_el1);
717
718 switch (ESR_ELx_EC(esr)) {
719 case ESR_ELx_EC_SVC64:
720 el0_svc(regs);
721 break;
722 case ESR_ELx_EC_DABT_LOW:
723 el0_da(regs, esr);
724 break;
725 case ESR_ELx_EC_IABT_LOW:
726 el0_ia(regs, esr);
727 break;
728 case ESR_ELx_EC_FP_ASIMD:
729 el0_fpsimd_acc(regs, esr);
730 break;
731 case ESR_ELx_EC_SVE:
732 el0_sve_acc(regs, esr);
733 break;
734 case ESR_ELx_EC_SME:
735 el0_sme_acc(regs, esr);
736 break;
737 case ESR_ELx_EC_FP_EXC64:
738 el0_fpsimd_exc(regs, esr);
739 break;
740 case ESR_ELx_EC_SYS64:
741 case ESR_ELx_EC_WFx:
742 el0_sys(regs, esr);
743 break;
744 case ESR_ELx_EC_SP_ALIGN:
745 el0_sp(regs, esr);
746 break;
747 case ESR_ELx_EC_PC_ALIGN:
748 el0_pc(regs, esr);
749 break;
750 case ESR_ELx_EC_UNKNOWN:
751 el0_undef(regs, esr);
752 break;
753 case ESR_ELx_EC_BTI:
754 el0_bti(regs);
755 break;
756 case ESR_ELx_EC_MOPS:
757 el0_mops(regs, esr);
758 break;
759 case ESR_ELx_EC_BREAKPT_LOW:
760 case ESR_ELx_EC_SOFTSTP_LOW:
761 case ESR_ELx_EC_WATCHPT_LOW:
762 case ESR_ELx_EC_BRK64:
763 el0_dbg(regs, esr);
764 break;
765 case ESR_ELx_EC_FPAC:
766 el0_fpac(regs, esr);
767 break;
768 default:
769 el0_inv(regs, esr);
770 }
771 }
772
el0_interrupt(struct pt_regs * regs,void (* handler)(struct pt_regs *))773 static void noinstr el0_interrupt(struct pt_regs *regs,
774 void (*handler)(struct pt_regs *))
775 {
776 enter_from_user_mode(regs);
777
778 write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
779
780 if (regs->pc & BIT(55))
781 arm64_apply_bp_hardening();
782
783 irq_enter_rcu();
784 do_interrupt_handler(regs, handler);
785 irq_exit_rcu();
786
787 exit_to_user_mode(regs);
788 }
789
__el0_irq_handler_common(struct pt_regs * regs)790 static void noinstr __el0_irq_handler_common(struct pt_regs *regs)
791 {
792 el0_interrupt(regs, handle_arch_irq);
793 }
794
el0t_64_irq_handler(struct pt_regs * regs)795 asmlinkage void noinstr el0t_64_irq_handler(struct pt_regs *regs)
796 {
797 __el0_irq_handler_common(regs);
798 }
799
__el0_fiq_handler_common(struct pt_regs * regs)800 static void noinstr __el0_fiq_handler_common(struct pt_regs *regs)
801 {
802 el0_interrupt(regs, handle_arch_fiq);
803 }
804
el0t_64_fiq_handler(struct pt_regs * regs)805 asmlinkage void noinstr el0t_64_fiq_handler(struct pt_regs *regs)
806 {
807 __el0_fiq_handler_common(regs);
808 }
809
__el0_error_handler_common(struct pt_regs * regs)810 static void noinstr __el0_error_handler_common(struct pt_regs *regs)
811 {
812 unsigned long esr = read_sysreg(esr_el1);
813
814 enter_from_user_mode(regs);
815 local_daif_restore(DAIF_ERRCTX);
816 arm64_enter_nmi(regs);
817 do_serror(regs, esr);
818 arm64_exit_nmi(regs);
819 local_daif_restore(DAIF_PROCCTX);
820 exit_to_user_mode(regs);
821 }
822
el0t_64_error_handler(struct pt_regs * regs)823 asmlinkage void noinstr el0t_64_error_handler(struct pt_regs *regs)
824 {
825 __el0_error_handler_common(regs);
826 }
827
828 #ifdef CONFIG_COMPAT
el0_cp15(struct pt_regs * regs,unsigned long esr)829 static void noinstr el0_cp15(struct pt_regs *regs, unsigned long esr)
830 {
831 enter_from_user_mode(regs);
832 local_daif_restore(DAIF_PROCCTX);
833 do_el0_cp15(esr, regs);
834 exit_to_user_mode(regs);
835 }
836
el0_svc_compat(struct pt_regs * regs)837 static void noinstr el0_svc_compat(struct pt_regs *regs)
838 {
839 enter_from_user_mode(regs);
840 cortex_a76_erratum_1463225_svc_handler();
841 local_daif_restore(DAIF_PROCCTX);
842 do_el0_svc_compat(regs);
843 exit_to_user_mode(regs);
844 }
845
el0t_32_sync_handler(struct pt_regs * regs)846 asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)
847 {
848 unsigned long esr = read_sysreg(esr_el1);
849
850 switch (ESR_ELx_EC(esr)) {
851 case ESR_ELx_EC_SVC32:
852 el0_svc_compat(regs);
853 break;
854 case ESR_ELx_EC_DABT_LOW:
855 el0_da(regs, esr);
856 break;
857 case ESR_ELx_EC_IABT_LOW:
858 el0_ia(regs, esr);
859 break;
860 case ESR_ELx_EC_FP_ASIMD:
861 el0_fpsimd_acc(regs, esr);
862 break;
863 case ESR_ELx_EC_FP_EXC32:
864 el0_fpsimd_exc(regs, esr);
865 break;
866 case ESR_ELx_EC_PC_ALIGN:
867 el0_pc(regs, esr);
868 break;
869 case ESR_ELx_EC_UNKNOWN:
870 case ESR_ELx_EC_CP14_MR:
871 case ESR_ELx_EC_CP14_LS:
872 case ESR_ELx_EC_CP14_64:
873 el0_undef(regs, esr);
874 break;
875 case ESR_ELx_EC_CP15_32:
876 case ESR_ELx_EC_CP15_64:
877 el0_cp15(regs, esr);
878 break;
879 case ESR_ELx_EC_BREAKPT_LOW:
880 case ESR_ELx_EC_SOFTSTP_LOW:
881 case ESR_ELx_EC_WATCHPT_LOW:
882 case ESR_ELx_EC_BKPT32:
883 el0_dbg(regs, esr);
884 break;
885 default:
886 el0_inv(regs, esr);
887 }
888 }
889
el0t_32_irq_handler(struct pt_regs * regs)890 asmlinkage void noinstr el0t_32_irq_handler(struct pt_regs *regs)
891 {
892 __el0_irq_handler_common(regs);
893 }
894
el0t_32_fiq_handler(struct pt_regs * regs)895 asmlinkage void noinstr el0t_32_fiq_handler(struct pt_regs *regs)
896 {
897 __el0_fiq_handler_common(regs);
898 }
899
el0t_32_error_handler(struct pt_regs * regs)900 asmlinkage void noinstr el0t_32_error_handler(struct pt_regs *regs)
901 {
902 __el0_error_handler_common(regs);
903 }
904 #else /* CONFIG_COMPAT */
905 UNHANDLED(el0t, 32, sync)
906 UNHANDLED(el0t, 32, irq)
907 UNHANDLED(el0t, 32, fiq)
908 UNHANDLED(el0t, 32, error)
909 #endif /* CONFIG_COMPAT */
910
911 #ifdef CONFIG_VMAP_STACK
handle_bad_stack(struct pt_regs * regs)912 asmlinkage void noinstr __noreturn handle_bad_stack(struct pt_regs *regs)
913 {
914 unsigned long esr = read_sysreg(esr_el1);
915 unsigned long far = read_sysreg(far_el1);
916
917 arm64_enter_nmi(regs);
918 panic_bad_stack(regs, esr, far);
919 }
920 #endif /* CONFIG_VMAP_STACK */
921
922 #ifdef CONFIG_ARM_SDE_INTERFACE
923 asmlinkage noinstr unsigned long
__sdei_handler(struct pt_regs * regs,struct sdei_registered_event * arg)924 __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
925 {
926 unsigned long ret;
927
928 /*
929 * We didn't take an exception to get here, so the HW hasn't
930 * set/cleared bits in PSTATE that we may rely on.
931 *
932 * The original SDEI spec (ARM DEN 0054A) can be read ambiguously as to
933 * whether PSTATE bits are inherited unchanged or generated from
934 * scratch, and the TF-A implementation always clears PAN and always
935 * clears UAO. There are no other known implementations.
936 *
937 * Subsequent revisions (ARM DEN 0054B) follow the usual rules for how
938 * PSTATE is modified upon architectural exceptions, and so PAN is
939 * either inherited or set per SCTLR_ELx.SPAN, and UAO is always
940 * cleared.
941 *
942 * We must explicitly reset PAN to the expected state, including
943 * clearing it when the host isn't using it, in case a VM had it set.
944 */
945 if (system_uses_hw_pan())
946 set_pstate_pan(1);
947 else if (cpu_has_pan())
948 set_pstate_pan(0);
949
950 arm64_enter_nmi(regs);
951 ret = do_sdei_event(regs, arg);
952 arm64_exit_nmi(regs);
953
954 return ret;
955 }
956 #endif /* CONFIG_ARM_SDE_INTERFACE */
957