arm64: entry: convert IRQ+FIQ handlers to C
[linux-2.6-microblaze.git] / arch / arm64 / kernel / entry-common.c
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/linkage.h>
10 #include <linux/lockdep.h>
11 #include <linux/ptrace.h>
12 #include <linux/sched.h>
13 #include <linux/sched/debug.h>
14 #include <linux/thread_info.h>
15
16 #include <asm/cpufeature.h>
17 #include <asm/daifflags.h>
18 #include <asm/esr.h>
19 #include <asm/exception.h>
20 #include <asm/kprobes.h>
21 #include <asm/mmu.h>
22 #include <asm/processor.h>
23 #include <asm/stacktrace.h>
24 #include <asm/sysreg.h>
25
26 /*
27  * This is intended to match the logic in irqentry_enter(), handling the kernel
28  * mode transitions only.
29  */
30 static void noinstr enter_from_kernel_mode(struct pt_regs *regs)
31 {
32         regs->exit_rcu = false;
33
34         if (!IS_ENABLED(CONFIG_TINY_RCU) && is_idle_task(current)) {
35                 lockdep_hardirqs_off(CALLER_ADDR0);
36                 rcu_irq_enter();
37                 trace_hardirqs_off_finish();
38
39                 regs->exit_rcu = true;
40                 return;
41         }
42
43         lockdep_hardirqs_off(CALLER_ADDR0);
44         rcu_irq_enter_check_tick();
45         trace_hardirqs_off_finish();
46
47         mte_check_tfsr_entry();
48 }
49
50 /*
51  * This is intended to match the logic in irqentry_exit(), handling the kernel
52  * mode transitions only, and with preemption handled elsewhere.
53  */
54 static void noinstr exit_to_kernel_mode(struct pt_regs *regs)
55 {
56         lockdep_assert_irqs_disabled();
57
58         mte_check_tfsr_exit();
59
60         if (interrupts_enabled(regs)) {
61                 if (regs->exit_rcu) {
62                         trace_hardirqs_on_prepare();
63                         lockdep_hardirqs_on_prepare(CALLER_ADDR0);
64                         rcu_irq_exit();
65                         lockdep_hardirqs_on(CALLER_ADDR0);
66                         return;
67                 }
68
69                 trace_hardirqs_on();
70         } else {
71                 if (regs->exit_rcu)
72                         rcu_irq_exit();
73         }
74 }
75
76 void noinstr arm64_enter_nmi(struct pt_regs *regs)
77 {
78         regs->lockdep_hardirqs = lockdep_hardirqs_enabled();
79
80         __nmi_enter();
81         lockdep_hardirqs_off(CALLER_ADDR0);
82         lockdep_hardirq_enter();
83         rcu_nmi_enter();
84
85         trace_hardirqs_off_finish();
86         ftrace_nmi_enter();
87 }
88
89 void noinstr arm64_exit_nmi(struct pt_regs *regs)
90 {
91         bool restore = regs->lockdep_hardirqs;
92
93         ftrace_nmi_exit();
94         if (restore) {
95                 trace_hardirqs_on_prepare();
96                 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
97         }
98
99         rcu_nmi_exit();
100         lockdep_hardirq_exit();
101         if (restore)
102                 lockdep_hardirqs_on(CALLER_ADDR0);
103         __nmi_exit();
104 }
105
106 static void noinstr enter_el1_irq_or_nmi(struct pt_regs *regs)
107 {
108         if (IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) && !interrupts_enabled(regs))
109                 arm64_enter_nmi(regs);
110         else
111                 enter_from_kernel_mode(regs);
112 }
113
114 static void noinstr exit_el1_irq_or_nmi(struct pt_regs *regs)
115 {
116         if (IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) && !interrupts_enabled(regs))
117                 arm64_exit_nmi(regs);
118         else
119                 exit_to_kernel_mode(regs);
120 }
121
122 static void __sched arm64_preempt_schedule_irq(void)
123 {
124         lockdep_assert_irqs_disabled();
125
126         /*
127          * DAIF.DA are cleared at the start of IRQ/FIQ handling, and when GIC
128          * priority masking is used the GIC irqchip driver will clear DAIF.IF
129          * using gic_arch_enable_irqs() for normal IRQs. If anything is set in
130          * DAIF we must have handled an NMI, so skip preemption.
131          */
132         if (system_uses_irq_prio_masking() && read_sysreg(daif))
133                 return;
134
135         /*
136          * Preempting a task from an IRQ means we leave copies of PSTATE
137          * on the stack. cpufeature's enable calls may modify PSTATE, but
138          * resuming one of these preempted tasks would undo those changes.
139          *
140          * Only allow a task to be preempted once cpufeatures have been
141          * enabled.
142          */
143         if (system_capabilities_finalized())
144                 preempt_schedule_irq();
145 }
146
147 static void do_interrupt_handler(struct pt_regs *regs,
148                                  void (*handler)(struct pt_regs *))
149 {
150         if (on_thread_stack())
151                 call_on_irq_stack(regs, handler);
152         else
153                 handler(regs);
154 }
155
156 extern void (*handle_arch_irq)(struct pt_regs *);
157 extern void (*handle_arch_fiq)(struct pt_regs *);
158
159 #ifdef CONFIG_ARM64_ERRATUM_1463225
160 static DEFINE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
161
162 static void cortex_a76_erratum_1463225_svc_handler(void)
163 {
164         u32 reg, val;
165
166         if (!unlikely(test_thread_flag(TIF_SINGLESTEP)))
167                 return;
168
169         if (!unlikely(this_cpu_has_cap(ARM64_WORKAROUND_1463225)))
170                 return;
171
172         __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 1);
173         reg = read_sysreg(mdscr_el1);
174         val = reg | DBG_MDSCR_SS | DBG_MDSCR_KDE;
175         write_sysreg(val, mdscr_el1);
176         asm volatile("msr daifclr, #8");
177         isb();
178
179         /* We will have taken a single-step exception by this point */
180
181         write_sysreg(reg, mdscr_el1);
182         __this_cpu_write(__in_cortex_a76_erratum_1463225_wa, 0);
183 }
184
185 static bool cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
186 {
187         if (!__this_cpu_read(__in_cortex_a76_erratum_1463225_wa))
188                 return false;
189
190         /*
191          * We've taken a dummy step exception from the kernel to ensure
192          * that interrupts are re-enabled on the syscall path. Return back
193          * to cortex_a76_erratum_1463225_svc_handler() with debug exceptions
194          * masked so that we can safely restore the mdscr and get on with
195          * handling the syscall.
196          */
197         regs->pstate |= PSR_D_BIT;
198         return true;
199 }
200 #else /* CONFIG_ARM64_ERRATUM_1463225 */
201 static void cortex_a76_erratum_1463225_svc_handler(void) { }
202 static bool cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
203 {
204         return false;
205 }
206 #endif /* CONFIG_ARM64_ERRATUM_1463225 */
207
208 static void noinstr el1_abort(struct pt_regs *regs, unsigned long esr)
209 {
210         unsigned long far = read_sysreg(far_el1);
211
212         enter_from_kernel_mode(regs);
213         local_daif_inherit(regs);
214         do_mem_abort(far, esr, regs);
215         local_daif_mask();
216         exit_to_kernel_mode(regs);
217 }
218
219 static void noinstr el1_pc(struct pt_regs *regs, unsigned long esr)
220 {
221         unsigned long far = read_sysreg(far_el1);
222
223         enter_from_kernel_mode(regs);
224         local_daif_inherit(regs);
225         do_sp_pc_abort(far, esr, regs);
226         local_daif_mask();
227         exit_to_kernel_mode(regs);
228 }
229
230 static void noinstr el1_undef(struct pt_regs *regs)
231 {
232         enter_from_kernel_mode(regs);
233         local_daif_inherit(regs);
234         do_undefinstr(regs);
235         local_daif_mask();
236         exit_to_kernel_mode(regs);
237 }
238
239 static void noinstr el1_inv(struct pt_regs *regs, unsigned long esr)
240 {
241         enter_from_kernel_mode(regs);
242         local_daif_inherit(regs);
243         bad_mode(regs, 0, esr);
244         local_daif_mask();
245         exit_to_kernel_mode(regs);
246 }
247
248 static void noinstr arm64_enter_el1_dbg(struct pt_regs *regs)
249 {
250         regs->lockdep_hardirqs = lockdep_hardirqs_enabled();
251
252         lockdep_hardirqs_off(CALLER_ADDR0);
253         rcu_nmi_enter();
254
255         trace_hardirqs_off_finish();
256 }
257
258 static void noinstr arm64_exit_el1_dbg(struct pt_regs *regs)
259 {
260         bool restore = regs->lockdep_hardirqs;
261
262         if (restore) {
263                 trace_hardirqs_on_prepare();
264                 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
265         }
266
267         rcu_nmi_exit();
268         if (restore)
269                 lockdep_hardirqs_on(CALLER_ADDR0);
270 }
271
272 static void noinstr el1_dbg(struct pt_regs *regs, unsigned long esr)
273 {
274         unsigned long far = read_sysreg(far_el1);
275
276         arm64_enter_el1_dbg(regs);
277         if (!cortex_a76_erratum_1463225_debug_handler(regs))
278                 do_debug_exception(far, esr, regs);
279         arm64_exit_el1_dbg(regs);
280 }
281
282 static void noinstr el1_fpac(struct pt_regs *regs, unsigned long esr)
283 {
284         enter_from_kernel_mode(regs);
285         local_daif_inherit(regs);
286         do_ptrauth_fault(regs, esr);
287         local_daif_mask();
288         exit_to_kernel_mode(regs);
289 }
290
291 asmlinkage void noinstr el1_sync_handler(struct pt_regs *regs)
292 {
293         unsigned long esr = read_sysreg(esr_el1);
294
295         switch (ESR_ELx_EC(esr)) {
296         case ESR_ELx_EC_DABT_CUR:
297         case ESR_ELx_EC_IABT_CUR:
298                 el1_abort(regs, esr);
299                 break;
300         /*
301          * We don't handle ESR_ELx_EC_SP_ALIGN, since we will have hit a
302          * recursive exception when trying to push the initial pt_regs.
303          */
304         case ESR_ELx_EC_PC_ALIGN:
305                 el1_pc(regs, esr);
306                 break;
307         case ESR_ELx_EC_SYS64:
308         case ESR_ELx_EC_UNKNOWN:
309                 el1_undef(regs);
310                 break;
311         case ESR_ELx_EC_BREAKPT_CUR:
312         case ESR_ELx_EC_SOFTSTP_CUR:
313         case ESR_ELx_EC_WATCHPT_CUR:
314         case ESR_ELx_EC_BRK64:
315                 el1_dbg(regs, esr);
316                 break;
317         case ESR_ELx_EC_FPAC:
318                 el1_fpac(regs, esr);
319                 break;
320         default:
321                 el1_inv(regs, esr);
322         }
323 }
324
325 static void noinstr el1_interrupt(struct pt_regs *regs,
326                                   void (*handler)(struct pt_regs *))
327 {
328         write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
329
330         enter_el1_irq_or_nmi(regs);
331         do_interrupt_handler(regs, handler);
332
333         /*
334          * Note: thread_info::preempt_count includes both thread_info::count
335          * and thread_info::need_resched, and is not equivalent to
336          * preempt_count().
337          */
338         if (IS_ENABLED(CONFIG_PREEMPTION) &&
339             READ_ONCE(current_thread_info()->preempt_count) == 0)
340                 arm64_preempt_schedule_irq();
341
342         exit_el1_irq_or_nmi(regs);
343 }
344
345 asmlinkage void noinstr el1_irq_handler(struct pt_regs *regs)
346 {
347         el1_interrupt(regs, handle_arch_irq);
348 }
349
350 asmlinkage void noinstr el1_fiq_handler(struct pt_regs *regs)
351 {
352         el1_interrupt(regs, handle_arch_fiq);
353 }
354
355 asmlinkage void noinstr el1_error_handler(struct pt_regs *regs)
356 {
357         unsigned long esr = read_sysreg(esr_el1);
358
359         local_daif_restore(DAIF_ERRCTX);
360         arm64_enter_nmi(regs);
361         do_serror(regs, esr);
362         arm64_exit_nmi(regs);
363 }
364
365 asmlinkage void noinstr enter_from_user_mode(void)
366 {
367         lockdep_hardirqs_off(CALLER_ADDR0);
368         CT_WARN_ON(ct_state() != CONTEXT_USER);
369         user_exit_irqoff();
370         trace_hardirqs_off_finish();
371 }
372
373 asmlinkage void noinstr exit_to_user_mode(void)
374 {
375         mte_check_tfsr_exit();
376
377         trace_hardirqs_on_prepare();
378         lockdep_hardirqs_on_prepare(CALLER_ADDR0);
379         user_enter_irqoff();
380         lockdep_hardirqs_on(CALLER_ADDR0);
381 }
382
383 static void noinstr el0_da(struct pt_regs *regs, unsigned long esr)
384 {
385         unsigned long far = read_sysreg(far_el1);
386
387         enter_from_user_mode();
388         local_daif_restore(DAIF_PROCCTX);
389         do_mem_abort(far, esr, regs);
390 }
391
392 static void noinstr el0_ia(struct pt_regs *regs, unsigned long esr)
393 {
394         unsigned long far = read_sysreg(far_el1);
395
396         /*
397          * We've taken an instruction abort from userspace and not yet
398          * re-enabled IRQs. If the address is a kernel address, apply
399          * BP hardening prior to enabling IRQs and pre-emption.
400          */
401         if (!is_ttbr0_addr(far))
402                 arm64_apply_bp_hardening();
403
404         enter_from_user_mode();
405         local_daif_restore(DAIF_PROCCTX);
406         do_mem_abort(far, esr, regs);
407 }
408
409 static void noinstr el0_fpsimd_acc(struct pt_regs *regs, unsigned long esr)
410 {
411         enter_from_user_mode();
412         local_daif_restore(DAIF_PROCCTX);
413         do_fpsimd_acc(esr, regs);
414 }
415
416 static void noinstr el0_sve_acc(struct pt_regs *regs, unsigned long esr)
417 {
418         enter_from_user_mode();
419         local_daif_restore(DAIF_PROCCTX);
420         do_sve_acc(esr, regs);
421 }
422
423 static void noinstr el0_fpsimd_exc(struct pt_regs *regs, unsigned long esr)
424 {
425         enter_from_user_mode();
426         local_daif_restore(DAIF_PROCCTX);
427         do_fpsimd_exc(esr, regs);
428 }
429
430 static void noinstr el0_sys(struct pt_regs *regs, unsigned long esr)
431 {
432         enter_from_user_mode();
433         local_daif_restore(DAIF_PROCCTX);
434         do_sysinstr(esr, regs);
435 }
436
437 static void noinstr el0_pc(struct pt_regs *regs, unsigned long esr)
438 {
439         unsigned long far = read_sysreg(far_el1);
440
441         if (!is_ttbr0_addr(instruction_pointer(regs)))
442                 arm64_apply_bp_hardening();
443
444         enter_from_user_mode();
445         local_daif_restore(DAIF_PROCCTX);
446         do_sp_pc_abort(far, esr, regs);
447 }
448
449 static void noinstr el0_sp(struct pt_regs *regs, unsigned long esr)
450 {
451         enter_from_user_mode();
452         local_daif_restore(DAIF_PROCCTX);
453         do_sp_pc_abort(regs->sp, esr, regs);
454 }
455
456 static void noinstr el0_undef(struct pt_regs *regs)
457 {
458         enter_from_user_mode();
459         local_daif_restore(DAIF_PROCCTX);
460         do_undefinstr(regs);
461 }
462
463 static void noinstr el0_bti(struct pt_regs *regs)
464 {
465         enter_from_user_mode();
466         local_daif_restore(DAIF_PROCCTX);
467         do_bti(regs);
468 }
469
470 static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr)
471 {
472         enter_from_user_mode();
473         local_daif_restore(DAIF_PROCCTX);
474         bad_el0_sync(regs, 0, esr);
475 }
476
477 static void noinstr el0_dbg(struct pt_regs *regs, unsigned long esr)
478 {
479         /* Only watchpoints write FAR_EL1, otherwise its UNKNOWN */
480         unsigned long far = read_sysreg(far_el1);
481
482         enter_from_user_mode();
483         do_debug_exception(far, esr, regs);
484         local_daif_restore(DAIF_PROCCTX);
485 }
486
487 static void noinstr el0_svc(struct pt_regs *regs)
488 {
489         enter_from_user_mode();
490         cortex_a76_erratum_1463225_svc_handler();
491         do_el0_svc(regs);
492 }
493
494 static void noinstr el0_fpac(struct pt_regs *regs, unsigned long esr)
495 {
496         enter_from_user_mode();
497         local_daif_restore(DAIF_PROCCTX);
498         do_ptrauth_fault(regs, esr);
499 }
500
501 asmlinkage void noinstr el0_sync_handler(struct pt_regs *regs)
502 {
503         unsigned long esr = read_sysreg(esr_el1);
504
505         switch (ESR_ELx_EC(esr)) {
506         case ESR_ELx_EC_SVC64:
507                 el0_svc(regs);
508                 break;
509         case ESR_ELx_EC_DABT_LOW:
510                 el0_da(regs, esr);
511                 break;
512         case ESR_ELx_EC_IABT_LOW:
513                 el0_ia(regs, esr);
514                 break;
515         case ESR_ELx_EC_FP_ASIMD:
516                 el0_fpsimd_acc(regs, esr);
517                 break;
518         case ESR_ELx_EC_SVE:
519                 el0_sve_acc(regs, esr);
520                 break;
521         case ESR_ELx_EC_FP_EXC64:
522                 el0_fpsimd_exc(regs, esr);
523                 break;
524         case ESR_ELx_EC_SYS64:
525         case ESR_ELx_EC_WFx:
526                 el0_sys(regs, esr);
527                 break;
528         case ESR_ELx_EC_SP_ALIGN:
529                 el0_sp(regs, esr);
530                 break;
531         case ESR_ELx_EC_PC_ALIGN:
532                 el0_pc(regs, esr);
533                 break;
534         case ESR_ELx_EC_UNKNOWN:
535                 el0_undef(regs);
536                 break;
537         case ESR_ELx_EC_BTI:
538                 el0_bti(regs);
539                 break;
540         case ESR_ELx_EC_BREAKPT_LOW:
541         case ESR_ELx_EC_SOFTSTP_LOW:
542         case ESR_ELx_EC_WATCHPT_LOW:
543         case ESR_ELx_EC_BRK64:
544                 el0_dbg(regs, esr);
545                 break;
546         case ESR_ELx_EC_FPAC:
547                 el0_fpac(regs, esr);
548                 break;
549         default:
550                 el0_inv(regs, esr);
551         }
552 }
553
554 static void noinstr el0_interrupt(struct pt_regs *regs,
555                                   void (*handler)(struct pt_regs *))
556 {
557         enter_from_user_mode();
558
559         write_sysreg(DAIF_PROCCTX_NOIRQ, daif);
560
561         if (regs->pc & BIT(55))
562                 arm64_apply_bp_hardening();
563
564         do_interrupt_handler(regs, handler);
565 }
566
567 static void noinstr __el0_irq_handler_common(struct pt_regs *regs)
568 {
569         el0_interrupt(regs, handle_arch_irq);
570 }
571
572 asmlinkage void noinstr el0_irq_handler(struct pt_regs *regs)
573 {
574         __el0_irq_handler_common(regs);
575 }
576
577 static void noinstr __el0_fiq_handler_common(struct pt_regs *regs)
578 {
579         el0_interrupt(regs, handle_arch_fiq);
580 }
581
582 asmlinkage void noinstr el0_fiq_handler(struct pt_regs *regs)
583 {
584         __el0_fiq_handler_common(regs);
585 }
586
587 static void __el0_error_handler_common(struct pt_regs *regs)
588 {
589         unsigned long esr = read_sysreg(esr_el1);
590
591         enter_from_user_mode();
592         local_daif_restore(DAIF_ERRCTX);
593         arm64_enter_nmi(regs);
594         do_serror(regs, esr);
595         arm64_exit_nmi(regs);
596         local_daif_restore(DAIF_PROCCTX);
597 }
598
599 asmlinkage void noinstr el0_error_handler(struct pt_regs *regs)
600 {
601         __el0_error_handler_common(regs);
602 }
603
604 #ifdef CONFIG_COMPAT
605 static void noinstr el0_cp15(struct pt_regs *regs, unsigned long esr)
606 {
607         enter_from_user_mode();
608         local_daif_restore(DAIF_PROCCTX);
609         do_cp15instr(esr, regs);
610 }
611
612 static void noinstr el0_svc_compat(struct pt_regs *regs)
613 {
614         enter_from_user_mode();
615         cortex_a76_erratum_1463225_svc_handler();
616         do_el0_svc_compat(regs);
617 }
618
619 asmlinkage void noinstr el0_sync_compat_handler(struct pt_regs *regs)
620 {
621         unsigned long esr = read_sysreg(esr_el1);
622
623         switch (ESR_ELx_EC(esr)) {
624         case ESR_ELx_EC_SVC32:
625                 el0_svc_compat(regs);
626                 break;
627         case ESR_ELx_EC_DABT_LOW:
628                 el0_da(regs, esr);
629                 break;
630         case ESR_ELx_EC_IABT_LOW:
631                 el0_ia(regs, esr);
632                 break;
633         case ESR_ELx_EC_FP_ASIMD:
634                 el0_fpsimd_acc(regs, esr);
635                 break;
636         case ESR_ELx_EC_FP_EXC32:
637                 el0_fpsimd_exc(regs, esr);
638                 break;
639         case ESR_ELx_EC_PC_ALIGN:
640                 el0_pc(regs, esr);
641                 break;
642         case ESR_ELx_EC_UNKNOWN:
643         case ESR_ELx_EC_CP14_MR:
644         case ESR_ELx_EC_CP14_LS:
645         case ESR_ELx_EC_CP14_64:
646                 el0_undef(regs);
647                 break;
648         case ESR_ELx_EC_CP15_32:
649         case ESR_ELx_EC_CP15_64:
650                 el0_cp15(regs, esr);
651                 break;
652         case ESR_ELx_EC_BREAKPT_LOW:
653         case ESR_ELx_EC_SOFTSTP_LOW:
654         case ESR_ELx_EC_WATCHPT_LOW:
655         case ESR_ELx_EC_BKPT32:
656                 el0_dbg(regs, esr);
657                 break;
658         default:
659                 el0_inv(regs, esr);
660         }
661 }
662
663 asmlinkage void noinstr el0_irq_compat_handler(struct pt_regs *regs)
664 {
665         __el0_irq_handler_common(regs);
666 }
667
668 asmlinkage void noinstr el0_fiq_compat_handler(struct pt_regs *regs)
669 {
670         __el0_fiq_handler_common(regs);
671 }
672
673 asmlinkage void noinstr el0_error_compat_handler(struct pt_regs *regs)
674 {
675         __el0_error_handler_common(regs);
676 }
677 #endif /* CONFIG_COMPAT */