arm64: entry: convert IRQ+FIQ handlers to C
[linux-2.6-microblaze.git] / arch / arm64 / kernel / entry.S
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Low-level exception handling code
4  *
5  * Copyright (C) 2012 ARM Ltd.
6  * Authors:     Catalin Marinas <catalin.marinas@arm.com>
7  *              Will Deacon <will.deacon@arm.com>
8  */
9
10 #include <linux/arm-smccc.h>
11 #include <linux/init.h>
12 #include <linux/linkage.h>
13
14 #include <asm/alternative.h>
15 #include <asm/assembler.h>
16 #include <asm/asm-offsets.h>
17 #include <asm/asm_pointer_auth.h>
18 #include <asm/bug.h>
19 #include <asm/cpufeature.h>
20 #include <asm/errno.h>
21 #include <asm/esr.h>
22 #include <asm/irq.h>
23 #include <asm/memory.h>
24 #include <asm/mmu.h>
25 #include <asm/processor.h>
26 #include <asm/ptrace.h>
27 #include <asm/scs.h>
28 #include <asm/thread_info.h>
29 #include <asm/asm-uaccess.h>
30 #include <asm/unistd.h>
31
32 /*
33  * Context tracking and irqflag tracing need to instrument transitions between
34  * user and kernel mode.
35  */
36         .macro user_enter_irqoff
37 #if defined(CONFIG_CONTEXT_TRACKING) || defined(CONFIG_TRACE_IRQFLAGS)
38         bl      exit_to_user_mode
39 #endif
40         .endm
41
42         .macro  clear_gp_regs
43         .irp    n,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
44         mov     x\n, xzr
45         .endr
46         .endm
47
48 /*
49  * Bad Abort numbers
50  *-----------------
51  */
52 #define BAD_SYNC        0
53 #define BAD_IRQ         1
54 #define BAD_FIQ         2
55 #define BAD_ERROR       3
56
57         .macro kernel_ventry, el, label, regsize = 64
58         .align 7
59 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
60         .if     \el == 0
61 alternative_if ARM64_UNMAP_KERNEL_AT_EL0
62         .if     \regsize == 64
63         mrs     x30, tpidrro_el0
64         msr     tpidrro_el0, xzr
65         .else
66         mov     x30, xzr
67         .endif
68 alternative_else_nop_endif
69         .endif
70 #endif
71
72         sub     sp, sp, #PT_REGS_SIZE
73 #ifdef CONFIG_VMAP_STACK
74         /*
75          * Test whether the SP has overflowed, without corrupting a GPR.
76          * Task and IRQ stacks are aligned so that SP & (1 << THREAD_SHIFT)
77          * should always be zero.
78          */
79         add     sp, sp, x0                      // sp' = sp + x0
80         sub     x0, sp, x0                      // x0' = sp' - x0 = (sp + x0) - x0 = sp
81         tbnz    x0, #THREAD_SHIFT, 0f
82         sub     x0, sp, x0                      // x0'' = sp' - x0' = (sp + x0) - sp = x0
83         sub     sp, sp, x0                      // sp'' = sp' - x0 = (sp + x0) - x0 = sp
84         b       el\()\el\()_\label
85
86 0:
87         /*
88          * Either we've just detected an overflow, or we've taken an exception
89          * while on the overflow stack. Either way, we won't return to
90          * userspace, and can clobber EL0 registers to free up GPRs.
91          */
92
93         /* Stash the original SP (minus PT_REGS_SIZE) in tpidr_el0. */
94         msr     tpidr_el0, x0
95
96         /* Recover the original x0 value and stash it in tpidrro_el0 */
97         sub     x0, sp, x0
98         msr     tpidrro_el0, x0
99
100         /* Switch to the overflow stack */
101         adr_this_cpu sp, overflow_stack + OVERFLOW_STACK_SIZE, x0
102
103         /*
104          * Check whether we were already on the overflow stack. This may happen
105          * after panic() re-enables interrupts.
106          */
107         mrs     x0, tpidr_el0                   // sp of interrupted context
108         sub     x0, sp, x0                      // delta with top of overflow stack
109         tst     x0, #~(OVERFLOW_STACK_SIZE - 1) // within range?
110         b.ne    __bad_stack                     // no? -> bad stack pointer
111
112         /* We were already on the overflow stack. Restore sp/x0 and carry on. */
113         sub     sp, sp, x0
114         mrs     x0, tpidrro_el0
115 #endif
116         b       el\()\el\()_\label
117         .endm
118
119         .macro tramp_alias, dst, sym
120         mov_q   \dst, TRAMP_VALIAS
121         add     \dst, \dst, #(\sym - .entry.tramp.text)
122         .endm
123
124         /*
125          * This macro corrupts x0-x3. It is the caller's duty  to save/restore
126          * them if required.
127          */
128         .macro  apply_ssbd, state, tmp1, tmp2
129 alternative_cb  spectre_v4_patch_fw_mitigation_enable
130         b       .L__asm_ssbd_skip\@             // Patched to NOP
131 alternative_cb_end
132         ldr_this_cpu    \tmp2, arm64_ssbd_callback_required, \tmp1
133         cbz     \tmp2,  .L__asm_ssbd_skip\@
134         ldr     \tmp2, [tsk, #TSK_TI_FLAGS]
135         tbnz    \tmp2, #TIF_SSBD, .L__asm_ssbd_skip\@
136         mov     w0, #ARM_SMCCC_ARCH_WORKAROUND_2
137         mov     w1, #\state
138 alternative_cb  spectre_v4_patch_fw_mitigation_conduit
139         nop                                     // Patched to SMC/HVC #0
140 alternative_cb_end
141 .L__asm_ssbd_skip\@:
142         .endm
143
144         /* Check for MTE asynchronous tag check faults */
145         .macro check_mte_async_tcf, tmp, ti_flags
146 #ifdef CONFIG_ARM64_MTE
147         .arch_extension lse
148 alternative_if_not ARM64_MTE
149         b       1f
150 alternative_else_nop_endif
151         mrs_s   \tmp, SYS_TFSRE0_EL1
152         tbz     \tmp, #SYS_TFSR_EL1_TF0_SHIFT, 1f
153         /* Asynchronous TCF occurred for TTBR0 access, set the TI flag */
154         mov     \tmp, #_TIF_MTE_ASYNC_FAULT
155         add     \ti_flags, tsk, #TSK_TI_FLAGS
156         stset   \tmp, [\ti_flags]
157         msr_s   SYS_TFSRE0_EL1, xzr
158 1:
159 #endif
160         .endm
161
162         /* Clear the MTE asynchronous tag check faults */
163         .macro clear_mte_async_tcf
164 #ifdef CONFIG_ARM64_MTE
165 alternative_if ARM64_MTE
166         dsb     ish
167         msr_s   SYS_TFSRE0_EL1, xzr
168 alternative_else_nop_endif
169 #endif
170         .endm
171
172         .macro mte_set_gcr, tmp, tmp2
173 #ifdef CONFIG_ARM64_MTE
174         /*
175          * Calculate and set the exclude mask preserving
176          * the RRND (bit[16]) setting.
177          */
178         mrs_s   \tmp2, SYS_GCR_EL1
179         bfi     \tmp2, \tmp, #0, #16
180         msr_s   SYS_GCR_EL1, \tmp2
181 #endif
182         .endm
183
184         .macro mte_set_kernel_gcr, tmp, tmp2
185 #ifdef CONFIG_KASAN_HW_TAGS
186 alternative_if_not ARM64_MTE
187         b       1f
188 alternative_else_nop_endif
189         ldr_l   \tmp, gcr_kernel_excl
190
191         mte_set_gcr \tmp, \tmp2
192         isb
193 1:
194 #endif
195         .endm
196
197         .macro mte_set_user_gcr, tsk, tmp, tmp2
198 #ifdef CONFIG_ARM64_MTE
199 alternative_if_not ARM64_MTE
200         b       1f
201 alternative_else_nop_endif
202         ldr     \tmp, [\tsk, #THREAD_GCR_EL1_USER]
203
204         mte_set_gcr \tmp, \tmp2
205 1:
206 #endif
207         .endm
208
209         .macro  kernel_entry, el, regsize = 64
210         .if     \regsize == 32
211         mov     w0, w0                          // zero upper 32 bits of x0
212         .endif
213         stp     x0, x1, [sp, #16 * 0]
214         stp     x2, x3, [sp, #16 * 1]
215         stp     x4, x5, [sp, #16 * 2]
216         stp     x6, x7, [sp, #16 * 3]
217         stp     x8, x9, [sp, #16 * 4]
218         stp     x10, x11, [sp, #16 * 5]
219         stp     x12, x13, [sp, #16 * 6]
220         stp     x14, x15, [sp, #16 * 7]
221         stp     x16, x17, [sp, #16 * 8]
222         stp     x18, x19, [sp, #16 * 9]
223         stp     x20, x21, [sp, #16 * 10]
224         stp     x22, x23, [sp, #16 * 11]
225         stp     x24, x25, [sp, #16 * 12]
226         stp     x26, x27, [sp, #16 * 13]
227         stp     x28, x29, [sp, #16 * 14]
228
229         .if     \el == 0
230         clear_gp_regs
231         mrs     x21, sp_el0
232         ldr_this_cpu    tsk, __entry_task, x20
233         msr     sp_el0, tsk
234
235         /*
236          * Ensure MDSCR_EL1.SS is clear, since we can unmask debug exceptions
237          * when scheduling.
238          */
239         ldr     x19, [tsk, #TSK_TI_FLAGS]
240         disable_step_tsk x19, x20
241
242         /* Check for asynchronous tag check faults in user space */
243         check_mte_async_tcf x22, x23
244         apply_ssbd 1, x22, x23
245
246 #ifdef CONFIG_ARM64_PTR_AUTH
247 alternative_if ARM64_HAS_ADDRESS_AUTH
248         /*
249          * Enable IA for in-kernel PAC if the task had it disabled. Although
250          * this could be implemented with an unconditional MRS which would avoid
251          * a load, this was measured to be slower on Cortex-A75 and Cortex-A76.
252          *
253          * Install the kernel IA key only if IA was enabled in the task. If IA
254          * was disabled on kernel exit then we would have left the kernel IA
255          * installed so there is no need to install it again.
256          */
257         ldr     x0, [tsk, THREAD_SCTLR_USER]
258         tbz     x0, SCTLR_ELx_ENIA_SHIFT, 1f
259         __ptrauth_keys_install_kernel_nosync tsk, x20, x22, x23
260         b       2f
261 1:
262         mrs     x0, sctlr_el1
263         orr     x0, x0, SCTLR_ELx_ENIA
264         msr     sctlr_el1, x0
265 2:
266         isb
267 alternative_else_nop_endif
268 #endif
269
270         mte_set_kernel_gcr x22, x23
271
272         scs_load tsk, x20
273         .else
274         add     x21, sp, #PT_REGS_SIZE
275         get_current_task tsk
276         .endif /* \el == 0 */
277         mrs     x22, elr_el1
278         mrs     x23, spsr_el1
279         stp     lr, x21, [sp, #S_LR]
280
281         /*
282          * For exceptions from EL0, create a terminal frame record.
283          * For exceptions from EL1, create a synthetic frame record so the
284          * interrupted code shows up in the backtrace.
285          */
286         .if \el == 0
287         stp     xzr, xzr, [sp, #S_STACKFRAME]
288         .else
289         stp     x29, x22, [sp, #S_STACKFRAME]
290         .endif
291         add     x29, sp, #S_STACKFRAME
292
293 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
294 alternative_if_not ARM64_HAS_PAN
295         bl      __swpan_entry_el\el
296 alternative_else_nop_endif
297 #endif
298
299         stp     x22, x23, [sp, #S_PC]
300
301         /* Not in a syscall by default (el0_svc overwrites for real syscall) */
302         .if     \el == 0
303         mov     w21, #NO_SYSCALL
304         str     w21, [sp, #S_SYSCALLNO]
305         .endif
306
307         /* Save pmr */
308 alternative_if ARM64_HAS_IRQ_PRIO_MASKING
309         mrs_s   x20, SYS_ICC_PMR_EL1
310         str     x20, [sp, #S_PMR_SAVE]
311         mov     x20, #GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET
312         msr_s   SYS_ICC_PMR_EL1, x20
313 alternative_else_nop_endif
314
315         /* Re-enable tag checking (TCO set on exception entry) */
316 #ifdef CONFIG_ARM64_MTE
317 alternative_if ARM64_MTE
318         SET_PSTATE_TCO(0)
319 alternative_else_nop_endif
320 #endif
321
322         /*
323          * Registers that may be useful after this macro is invoked:
324          *
325          * x20 - ICC_PMR_EL1
326          * x21 - aborted SP
327          * x22 - aborted PC
328          * x23 - aborted PSTATE
329         */
330         .endm
331
332         .macro  kernel_exit, el
333         .if     \el != 0
334         disable_daif
335         .endif
336
337         /* Restore pmr */
338 alternative_if ARM64_HAS_IRQ_PRIO_MASKING
339         ldr     x20, [sp, #S_PMR_SAVE]
340         msr_s   SYS_ICC_PMR_EL1, x20
341         mrs_s   x21, SYS_ICC_CTLR_EL1
342         tbz     x21, #6, .L__skip_pmr_sync\@    // Check for ICC_CTLR_EL1.PMHE
343         dsb     sy                              // Ensure priority change is seen by redistributor
344 .L__skip_pmr_sync\@:
345 alternative_else_nop_endif
346
347         ldp     x21, x22, [sp, #S_PC]           // load ELR, SPSR
348
349 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
350 alternative_if_not ARM64_HAS_PAN
351         bl      __swpan_exit_el\el
352 alternative_else_nop_endif
353 #endif
354
355         .if     \el == 0
356         ldr     x23, [sp, #S_SP]                // load return stack pointer
357         msr     sp_el0, x23
358         tst     x22, #PSR_MODE32_BIT            // native task?
359         b.eq    3f
360
361 #ifdef CONFIG_ARM64_ERRATUM_845719
362 alternative_if ARM64_WORKAROUND_845719
363 #ifdef CONFIG_PID_IN_CONTEXTIDR
364         mrs     x29, contextidr_el1
365         msr     contextidr_el1, x29
366 #else
367         msr contextidr_el1, xzr
368 #endif
369 alternative_else_nop_endif
370 #endif
371 3:
372         scs_save tsk, x0
373
374 #ifdef CONFIG_ARM64_PTR_AUTH
375 alternative_if ARM64_HAS_ADDRESS_AUTH
376         /*
377          * IA was enabled for in-kernel PAC. Disable it now if needed, or
378          * alternatively install the user's IA. All other per-task keys and
379          * SCTLR bits were updated on task switch.
380          *
381          * No kernel C function calls after this.
382          */
383         ldr     x0, [tsk, THREAD_SCTLR_USER]
384         tbz     x0, SCTLR_ELx_ENIA_SHIFT, 1f
385         __ptrauth_keys_install_user tsk, x0, x1, x2
386         b       2f
387 1:
388         mrs     x0, sctlr_el1
389         bic     x0, x0, SCTLR_ELx_ENIA
390         msr     sctlr_el1, x0
391 2:
392 alternative_else_nop_endif
393 #endif
394
395         mte_set_user_gcr tsk, x0, x1
396
397         apply_ssbd 0, x0, x1
398         .endif
399
400         msr     elr_el1, x21                    // set up the return data
401         msr     spsr_el1, x22
402         ldp     x0, x1, [sp, #16 * 0]
403         ldp     x2, x3, [sp, #16 * 1]
404         ldp     x4, x5, [sp, #16 * 2]
405         ldp     x6, x7, [sp, #16 * 3]
406         ldp     x8, x9, [sp, #16 * 4]
407         ldp     x10, x11, [sp, #16 * 5]
408         ldp     x12, x13, [sp, #16 * 6]
409         ldp     x14, x15, [sp, #16 * 7]
410         ldp     x16, x17, [sp, #16 * 8]
411         ldp     x18, x19, [sp, #16 * 9]
412         ldp     x20, x21, [sp, #16 * 10]
413         ldp     x22, x23, [sp, #16 * 11]
414         ldp     x24, x25, [sp, #16 * 12]
415         ldp     x26, x27, [sp, #16 * 13]
416         ldp     x28, x29, [sp, #16 * 14]
417         ldr     lr, [sp, #S_LR]
418         add     sp, sp, #PT_REGS_SIZE           // restore sp
419
420         .if     \el == 0
421 alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
422 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
423         bne     4f
424         msr     far_el1, x30
425         tramp_alias     x30, tramp_exit_native
426         br      x30
427 4:
428         tramp_alias     x30, tramp_exit_compat
429         br      x30
430 #endif
431         .else
432         /* Ensure any device/NC reads complete */
433         alternative_insn nop, "dmb sy", ARM64_WORKAROUND_1508412
434
435         eret
436         .endif
437         sb
438         .endm
439
440 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
441         /*
442          * Set the TTBR0 PAN bit in SPSR. When the exception is taken from
443          * EL0, there is no need to check the state of TTBR0_EL1 since
444          * accesses are always enabled.
445          * Note that the meaning of this bit differs from the ARMv8.1 PAN
446          * feature as all TTBR0_EL1 accesses are disabled, not just those to
447          * user mappings.
448          */
449 SYM_CODE_START_LOCAL(__swpan_entry_el1)
450         mrs     x21, ttbr0_el1
451         tst     x21, #TTBR_ASID_MASK            // Check for the reserved ASID
452         orr     x23, x23, #PSR_PAN_BIT          // Set the emulated PAN in the saved SPSR
453         b.eq    1f                              // TTBR0 access already disabled
454         and     x23, x23, #~PSR_PAN_BIT         // Clear the emulated PAN in the saved SPSR
455 SYM_INNER_LABEL(__swpan_entry_el0, SYM_L_LOCAL)
456         __uaccess_ttbr0_disable x21
457 1:      ret
458 SYM_CODE_END(__swpan_entry_el1)
459
460         /*
461          * Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR
462          * PAN bit checking.
463          */
464 SYM_CODE_START_LOCAL(__swpan_exit_el1)
465         tbnz    x22, #22, 1f                    // Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set
466         __uaccess_ttbr0_enable x0, x1
467 1:      and     x22, x22, #~PSR_PAN_BIT         // ARMv8.0 CPUs do not understand this bit
468         ret
469 SYM_CODE_END(__swpan_exit_el1)
470
471 SYM_CODE_START_LOCAL(__swpan_exit_el0)
472         __uaccess_ttbr0_enable x0, x1
473         /*
474          * Enable errata workarounds only if returning to user. The only
475          * workaround currently required for TTBR0_EL1 changes are for the
476          * Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache
477          * corruption).
478          */
479         b       post_ttbr_update_workaround
480 SYM_CODE_END(__swpan_exit_el0)
481 #endif
482
483 /* GPRs used by entry code */
484 tsk     .req    x28             // current thread_info
485
486 /*
487  * Interrupt handling.
488  */
489         .macro  gic_prio_kentry_setup, tmp:req
490 #ifdef CONFIG_ARM64_PSEUDO_NMI
491         alternative_if ARM64_HAS_IRQ_PRIO_MASKING
492         mov     \tmp, #(GIC_PRIO_PSR_I_SET | GIC_PRIO_IRQON)
493         msr_s   SYS_ICC_PMR_EL1, \tmp
494         alternative_else_nop_endif
495 #endif
496         .endm
497
498         .text
499
500 /*
501  * Exception vectors.
502  */
503         .pushsection ".entry.text", "ax"
504
505         .align  11
506 SYM_CODE_START(vectors)
507         kernel_ventry   1, sync_invalid                 // Synchronous EL1t
508         kernel_ventry   1, irq_invalid                  // IRQ EL1t
509         kernel_ventry   1, fiq_invalid                  // FIQ EL1t
510         kernel_ventry   1, error_invalid                // Error EL1t
511
512         kernel_ventry   1, sync                         // Synchronous EL1h
513         kernel_ventry   1, irq                          // IRQ EL1h
514         kernel_ventry   1, fiq                          // FIQ EL1h
515         kernel_ventry   1, error                        // Error EL1h
516
517         kernel_ventry   0, sync                         // Synchronous 64-bit EL0
518         kernel_ventry   0, irq                          // IRQ 64-bit EL0
519         kernel_ventry   0, fiq                          // FIQ 64-bit EL0
520         kernel_ventry   0, error                        // Error 64-bit EL0
521
522 #ifdef CONFIG_COMPAT
523         kernel_ventry   0, sync_compat, 32              // Synchronous 32-bit EL0
524         kernel_ventry   0, irq_compat, 32               // IRQ 32-bit EL0
525         kernel_ventry   0, fiq_compat, 32               // FIQ 32-bit EL0
526         kernel_ventry   0, error_compat, 32             // Error 32-bit EL0
527 #else
528         kernel_ventry   0, sync_invalid, 32             // Synchronous 32-bit EL0
529         kernel_ventry   0, irq_invalid, 32              // IRQ 32-bit EL0
530         kernel_ventry   0, fiq_invalid, 32              // FIQ 32-bit EL0
531         kernel_ventry   0, error_invalid, 32            // Error 32-bit EL0
532 #endif
533 SYM_CODE_END(vectors)
534
535 #ifdef CONFIG_VMAP_STACK
536         /*
537          * We detected an overflow in kernel_ventry, which switched to the
538          * overflow stack. Stash the exception regs, and head to our overflow
539          * handler.
540          */
541 __bad_stack:
542         /* Restore the original x0 value */
543         mrs     x0, tpidrro_el0
544
545         /*
546          * Store the original GPRs to the new stack. The orginal SP (minus
547          * PT_REGS_SIZE) was stashed in tpidr_el0 by kernel_ventry.
548          */
549         sub     sp, sp, #PT_REGS_SIZE
550         kernel_entry 1
551         mrs     x0, tpidr_el0
552         add     x0, x0, #PT_REGS_SIZE
553         str     x0, [sp, #S_SP]
554
555         /* Stash the regs for handle_bad_stack */
556         mov     x0, sp
557
558         /* Time to die */
559         bl      handle_bad_stack
560         ASM_BUG()
561 #endif /* CONFIG_VMAP_STACK */
562
563 /*
564  * Invalid mode handlers
565  */
566         .macro  inv_entry, el, reason, regsize = 64
567         kernel_entry \el, \regsize
568         mov     x0, sp
569         mov     x1, #\reason
570         mrs     x2, esr_el1
571         bl      bad_mode
572         ASM_BUG()
573         .endm
574
575 SYM_CODE_START_LOCAL(el0_sync_invalid)
576         inv_entry 0, BAD_SYNC
577 SYM_CODE_END(el0_sync_invalid)
578
579 SYM_CODE_START_LOCAL(el0_irq_invalid)
580         inv_entry 0, BAD_IRQ
581 SYM_CODE_END(el0_irq_invalid)
582
583 SYM_CODE_START_LOCAL(el0_fiq_invalid)
584         inv_entry 0, BAD_FIQ
585 SYM_CODE_END(el0_fiq_invalid)
586
587 SYM_CODE_START_LOCAL(el0_error_invalid)
588         inv_entry 0, BAD_ERROR
589 SYM_CODE_END(el0_error_invalid)
590
591 SYM_CODE_START_LOCAL(el1_sync_invalid)
592         inv_entry 1, BAD_SYNC
593 SYM_CODE_END(el1_sync_invalid)
594
595 SYM_CODE_START_LOCAL(el1_irq_invalid)
596         inv_entry 1, BAD_IRQ
597 SYM_CODE_END(el1_irq_invalid)
598
599 SYM_CODE_START_LOCAL(el1_fiq_invalid)
600         inv_entry 1, BAD_FIQ
601 SYM_CODE_END(el1_fiq_invalid)
602
603 SYM_CODE_START_LOCAL(el1_error_invalid)
604         inv_entry 1, BAD_ERROR
605 SYM_CODE_END(el1_error_invalid)
606
607 /*
608  * EL1 mode handlers.
609  */
610         .align  6
611 SYM_CODE_START_LOCAL_NOALIGN(el1_sync)
612         kernel_entry 1
613         mov     x0, sp
614         bl      el1_sync_handler
615         kernel_exit 1
616 SYM_CODE_END(el1_sync)
617
618         .align  6
619 SYM_CODE_START_LOCAL_NOALIGN(el1_irq)
620         kernel_entry 1
621         mov     x0, sp
622         bl      el1_irq_handler
623         kernel_exit 1
624 SYM_CODE_END(el1_irq)
625
626 SYM_CODE_START_LOCAL_NOALIGN(el1_fiq)
627         kernel_entry 1
628         mov     x0, sp
629         bl      el1_fiq_handler
630         kernel_exit 1
631 SYM_CODE_END(el1_fiq)
632
633 /*
634  * EL0 mode handlers.
635  */
636         .align  6
637 SYM_CODE_START_LOCAL_NOALIGN(el0_sync)
638         kernel_entry 0
639         mov     x0, sp
640         bl      el0_sync_handler
641         b       ret_to_user
642 SYM_CODE_END(el0_sync)
643
644 #ifdef CONFIG_COMPAT
645         .align  6
646 SYM_CODE_START_LOCAL_NOALIGN(el0_sync_compat)
647         kernel_entry 0, 32
648         mov     x0, sp
649         bl      el0_sync_compat_handler
650         b       ret_to_user
651 SYM_CODE_END(el0_sync_compat)
652
653         .align  6
654 SYM_CODE_START_LOCAL_NOALIGN(el0_irq_compat)
655         kernel_entry 0, 32
656         mov     x0, sp
657         bl      el0_irq_compat_handler
658         b       ret_to_user
659 SYM_CODE_END(el0_irq_compat)
660
661 SYM_CODE_START_LOCAL_NOALIGN(el0_fiq_compat)
662         kernel_entry 0, 32
663         mov     x0, sp
664         bl      el0_fiq_compat_handler
665         b       ret_to_user
666 SYM_CODE_END(el0_fiq_compat)
667
668 SYM_CODE_START_LOCAL_NOALIGN(el0_error_compat)
669         kernel_entry 0, 32
670         mov     x0, sp
671         bl      el0_error_compat_handler
672         b       ret_to_user
673 SYM_CODE_END(el0_error_compat)
674 #endif
675
676         .align  6
677 SYM_CODE_START_LOCAL_NOALIGN(el0_irq)
678         kernel_entry 0
679         mov     x0, sp
680         bl      el0_irq_handler
681         b       ret_to_user
682 SYM_CODE_END(el0_irq)
683
684 SYM_CODE_START_LOCAL_NOALIGN(el0_fiq)
685         kernel_entry 0
686         mov     x0, sp
687         bl      el0_fiq_handler
688         b       ret_to_user
689 SYM_CODE_END(el0_fiq)
690
691 SYM_CODE_START_LOCAL(el1_error)
692         kernel_entry 1
693         mov     x0, sp
694         bl      el1_error_handler
695         kernel_exit 1
696 SYM_CODE_END(el1_error)
697
698 SYM_CODE_START_LOCAL(el0_error)
699         kernel_entry 0
700         mov     x0, sp
701         bl      el0_error_handler
702         b       ret_to_user
703 SYM_CODE_END(el0_error)
704
705 /*
706  * "slow" syscall return path.
707  */
708 SYM_CODE_START_LOCAL(ret_to_user)
709         disable_daif
710         gic_prio_kentry_setup tmp=x3
711 #ifdef CONFIG_TRACE_IRQFLAGS
712         bl      trace_hardirqs_off
713 #endif
714         ldr     x19, [tsk, #TSK_TI_FLAGS]
715         and     x2, x19, #_TIF_WORK_MASK
716         cbnz    x2, work_pending
717 finish_ret_to_user:
718         user_enter_irqoff
719         /* Ignore asynchronous tag check faults in the uaccess routines */
720         clear_mte_async_tcf
721         enable_step_tsk x19, x2
722 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
723         bl      stackleak_erase
724 #endif
725         kernel_exit 0
726
727 /*
728  * Ok, we need to do extra processing, enter the slow path.
729  */
730 work_pending:
731         mov     x0, sp                          // 'regs'
732         mov     x1, x19
733         bl      do_notify_resume
734         ldr     x19, [tsk, #TSK_TI_FLAGS]       // re-check for single-step
735         b       finish_ret_to_user
736 SYM_CODE_END(ret_to_user)
737
738         .popsection                             // .entry.text
739
740 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
741 /*
742  * Exception vectors trampoline.
743  */
744         .pushsection ".entry.tramp.text", "ax"
745
746         // Move from tramp_pg_dir to swapper_pg_dir
747         .macro tramp_map_kernel, tmp
748         mrs     \tmp, ttbr1_el1
749         add     \tmp, \tmp, #TRAMP_SWAPPER_OFFSET
750         bic     \tmp, \tmp, #USER_ASID_FLAG
751         msr     ttbr1_el1, \tmp
752 #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
753 alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
754         /* ASID already in \tmp[63:48] */
755         movk    \tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
756         movk    \tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12)
757         /* 2MB boundary containing the vectors, so we nobble the walk cache */
758         movk    \tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12)
759         isb
760         tlbi    vae1, \tmp
761         dsb     nsh
762 alternative_else_nop_endif
763 #endif /* CONFIG_QCOM_FALKOR_ERRATUM_1003 */
764         .endm
765
766         // Move from swapper_pg_dir to tramp_pg_dir
767         .macro tramp_unmap_kernel, tmp
768         mrs     \tmp, ttbr1_el1
769         sub     \tmp, \tmp, #TRAMP_SWAPPER_OFFSET
770         orr     \tmp, \tmp, #USER_ASID_FLAG
771         msr     ttbr1_el1, \tmp
772         /*
773          * We avoid running the post_ttbr_update_workaround here because
774          * it's only needed by Cavium ThunderX, which requires KPTI to be
775          * disabled.
776          */
777         .endm
778
779         .macro tramp_ventry, regsize = 64
780         .align  7
781 1:
782         .if     \regsize == 64
783         msr     tpidrro_el0, x30        // Restored in kernel_ventry
784         .endif
785         /*
786          * Defend against branch aliasing attacks by pushing a dummy
787          * entry onto the return stack and using a RET instruction to
788          * enter the full-fat kernel vectors.
789          */
790         bl      2f
791         b       .
792 2:
793         tramp_map_kernel        x30
794 #ifdef CONFIG_RANDOMIZE_BASE
795         adr     x30, tramp_vectors + PAGE_SIZE
796 alternative_insn isb, nop, ARM64_WORKAROUND_QCOM_FALKOR_E1003
797         ldr     x30, [x30]
798 #else
799         ldr     x30, =vectors
800 #endif
801 alternative_if_not ARM64_WORKAROUND_CAVIUM_TX2_219_PRFM
802         prfm    plil1strm, [x30, #(1b - tramp_vectors)]
803 alternative_else_nop_endif
804         msr     vbar_el1, x30
805         add     x30, x30, #(1b - tramp_vectors)
806         isb
807         ret
808         .endm
809
810         .macro tramp_exit, regsize = 64
811         adr     x30, tramp_vectors
812         msr     vbar_el1, x30
813         tramp_unmap_kernel      x30
814         .if     \regsize == 64
815         mrs     x30, far_el1
816         .endif
817         eret
818         sb
819         .endm
820
821         .align  11
822 SYM_CODE_START_NOALIGN(tramp_vectors)
823         .space  0x400
824
825         tramp_ventry
826         tramp_ventry
827         tramp_ventry
828         tramp_ventry
829
830         tramp_ventry    32
831         tramp_ventry    32
832         tramp_ventry    32
833         tramp_ventry    32
834 SYM_CODE_END(tramp_vectors)
835
836 SYM_CODE_START(tramp_exit_native)
837         tramp_exit
838 SYM_CODE_END(tramp_exit_native)
839
840 SYM_CODE_START(tramp_exit_compat)
841         tramp_exit      32
842 SYM_CODE_END(tramp_exit_compat)
843
844         .ltorg
845         .popsection                             // .entry.tramp.text
846 #ifdef CONFIG_RANDOMIZE_BASE
847         .pushsection ".rodata", "a"
848         .align PAGE_SHIFT
849 SYM_DATA_START(__entry_tramp_data_start)
850         .quad   vectors
851 SYM_DATA_END(__entry_tramp_data_start)
852         .popsection                             // .rodata
853 #endif /* CONFIG_RANDOMIZE_BASE */
854 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
855
856 /*
857  * Register switch for AArch64. The callee-saved registers need to be saved
858  * and restored. On entry:
859  *   x0 = previous task_struct (must be preserved across the switch)
860  *   x1 = next task_struct
861  * Previous and next are guaranteed not to be the same.
862  *
863  */
864 SYM_FUNC_START(cpu_switch_to)
865         mov     x10, #THREAD_CPU_CONTEXT
866         add     x8, x0, x10
867         mov     x9, sp
868         stp     x19, x20, [x8], #16             // store callee-saved registers
869         stp     x21, x22, [x8], #16
870         stp     x23, x24, [x8], #16
871         stp     x25, x26, [x8], #16
872         stp     x27, x28, [x8], #16
873         stp     x29, x9, [x8], #16
874         str     lr, [x8]
875         add     x8, x1, x10
876         ldp     x19, x20, [x8], #16             // restore callee-saved registers
877         ldp     x21, x22, [x8], #16
878         ldp     x23, x24, [x8], #16
879         ldp     x25, x26, [x8], #16
880         ldp     x27, x28, [x8], #16
881         ldp     x29, x9, [x8], #16
882         ldr     lr, [x8]
883         mov     sp, x9
884         msr     sp_el0, x1
885         ptrauth_keys_install_kernel x1, x8, x9, x10
886         scs_save x0, x8
887         scs_load x1, x8
888         ret
889 SYM_FUNC_END(cpu_switch_to)
890 NOKPROBE(cpu_switch_to)
891
892 /*
893  * This is how we return from a fork.
894  */
895 SYM_CODE_START(ret_from_fork)
896         bl      schedule_tail
897         cbz     x19, 1f                         // not a kernel thread
898         mov     x0, x20
899         blr     x19
900 1:      get_current_task tsk
901         b       ret_to_user
902 SYM_CODE_END(ret_from_fork)
903 NOKPROBE(ret_from_fork)
904
905 /*
906  * void call_on_irq_stack(struct pt_regs *regs,
907  *                        void (*func)(struct pt_regs *));
908  *
909  * Calls func(regs) using this CPU's irq stack and shadow irq stack.
910  */
911 SYM_FUNC_START(call_on_irq_stack)
912 #ifdef CONFIG_SHADOW_CALL_STACK
913         stp     scs_sp, xzr, [sp, #-16]!
914         ldr_this_cpu scs_sp, irq_shadow_call_stack_ptr, x17
915 #endif
916         /* Create a frame record to save our LR and SP (implicit in FP) */
917         stp     x29, x30, [sp, #-16]!
918         mov     x29, sp
919
920         ldr_this_cpu x16, irq_stack_ptr, x17
921         mov     x15, #IRQ_STACK_SIZE
922         add     x16, x16, x15
923
924         /* Move to the new stack and call the function there */
925         mov     sp, x16
926         blr     x1
927
928         /*
929          * Restore the SP from the FP, and restore the FP and LR from the frame
930          * record.
931          */
932         mov     sp, x29
933         ldp     x29, x30, [sp], #16
934 #ifdef CONFIG_SHADOW_CALL_STACK
935         ldp     scs_sp, xzr, [sp], #16
936 #endif
937         ret
938 SYM_FUNC_END(call_on_irq_stack)
939 NOKPROBE(call_on_irq_stack)
940
941 #ifdef CONFIG_ARM_SDE_INTERFACE
942
943 #include <asm/sdei.h>
944 #include <uapi/linux/arm_sdei.h>
945
946 .macro sdei_handler_exit exit_mode
947         /* On success, this call never returns... */
948         cmp     \exit_mode, #SDEI_EXIT_SMC
949         b.ne    99f
950         smc     #0
951         b       .
952 99:     hvc     #0
953         b       .
954 .endm
955
956 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
957 /*
958  * The regular SDEI entry point may have been unmapped along with the rest of
959  * the kernel. This trampoline restores the kernel mapping to make the x1 memory
960  * argument accessible.
961  *
962  * This clobbers x4, __sdei_handler() will restore this from firmware's
963  * copy.
964  */
965 .ltorg
966 .pushsection ".entry.tramp.text", "ax"
967 SYM_CODE_START(__sdei_asm_entry_trampoline)
968         mrs     x4, ttbr1_el1
969         tbz     x4, #USER_ASID_BIT, 1f
970
971         tramp_map_kernel tmp=x4
972         isb
973         mov     x4, xzr
974
975         /*
976          * Remember whether to unmap the kernel on exit.
977          */
978 1:      str     x4, [x1, #(SDEI_EVENT_INTREGS + S_SDEI_TTBR1)]
979
980 #ifdef CONFIG_RANDOMIZE_BASE
981         adr     x4, tramp_vectors + PAGE_SIZE
982         add     x4, x4, #:lo12:__sdei_asm_trampoline_next_handler
983         ldr     x4, [x4]
984 #else
985         ldr     x4, =__sdei_asm_handler
986 #endif
987         br      x4
988 SYM_CODE_END(__sdei_asm_entry_trampoline)
989 NOKPROBE(__sdei_asm_entry_trampoline)
990
991 /*
992  * Make the exit call and restore the original ttbr1_el1
993  *
994  * x0 & x1: setup for the exit API call
995  * x2: exit_mode
996  * x4: struct sdei_registered_event argument from registration time.
997  */
998 SYM_CODE_START(__sdei_asm_exit_trampoline)
999         ldr     x4, [x4, #(SDEI_EVENT_INTREGS + S_SDEI_TTBR1)]
1000         cbnz    x4, 1f
1001
1002         tramp_unmap_kernel      tmp=x4
1003
1004 1:      sdei_handler_exit exit_mode=x2
1005 SYM_CODE_END(__sdei_asm_exit_trampoline)
1006 NOKPROBE(__sdei_asm_exit_trampoline)
1007         .ltorg
1008 .popsection             // .entry.tramp.text
1009 #ifdef CONFIG_RANDOMIZE_BASE
1010 .pushsection ".rodata", "a"
1011 SYM_DATA_START(__sdei_asm_trampoline_next_handler)
1012         .quad   __sdei_asm_handler
1013 SYM_DATA_END(__sdei_asm_trampoline_next_handler)
1014 .popsection             // .rodata
1015 #endif /* CONFIG_RANDOMIZE_BASE */
1016 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
1017
1018 /*
1019  * Software Delegated Exception entry point.
1020  *
1021  * x0: Event number
1022  * x1: struct sdei_registered_event argument from registration time.
1023  * x2: interrupted PC
1024  * x3: interrupted PSTATE
1025  * x4: maybe clobbered by the trampoline
1026  *
1027  * Firmware has preserved x0->x17 for us, we must save/restore the rest to
1028  * follow SMC-CC. We save (or retrieve) all the registers as the handler may
1029  * want them.
1030  */
1031 SYM_CODE_START(__sdei_asm_handler)
1032         stp     x2, x3, [x1, #SDEI_EVENT_INTREGS + S_PC]
1033         stp     x4, x5, [x1, #SDEI_EVENT_INTREGS + 16 * 2]
1034         stp     x6, x7, [x1, #SDEI_EVENT_INTREGS + 16 * 3]
1035         stp     x8, x9, [x1, #SDEI_EVENT_INTREGS + 16 * 4]
1036         stp     x10, x11, [x1, #SDEI_EVENT_INTREGS + 16 * 5]
1037         stp     x12, x13, [x1, #SDEI_EVENT_INTREGS + 16 * 6]
1038         stp     x14, x15, [x1, #SDEI_EVENT_INTREGS + 16 * 7]
1039         stp     x16, x17, [x1, #SDEI_EVENT_INTREGS + 16 * 8]
1040         stp     x18, x19, [x1, #SDEI_EVENT_INTREGS + 16 * 9]
1041         stp     x20, x21, [x1, #SDEI_EVENT_INTREGS + 16 * 10]
1042         stp     x22, x23, [x1, #SDEI_EVENT_INTREGS + 16 * 11]
1043         stp     x24, x25, [x1, #SDEI_EVENT_INTREGS + 16 * 12]
1044         stp     x26, x27, [x1, #SDEI_EVENT_INTREGS + 16 * 13]
1045         stp     x28, x29, [x1, #SDEI_EVENT_INTREGS + 16 * 14]
1046         mov     x4, sp
1047         stp     lr, x4, [x1, #SDEI_EVENT_INTREGS + S_LR]
1048
1049         mov     x19, x1
1050
1051 #if defined(CONFIG_VMAP_STACK) || defined(CONFIG_SHADOW_CALL_STACK)
1052         ldrb    w4, [x19, #SDEI_EVENT_PRIORITY]
1053 #endif
1054
1055 #ifdef CONFIG_VMAP_STACK
1056         /*
1057          * entry.S may have been using sp as a scratch register, find whether
1058          * this is a normal or critical event and switch to the appropriate
1059          * stack for this CPU.
1060          */
1061         cbnz    w4, 1f
1062         ldr_this_cpu dst=x5, sym=sdei_stack_normal_ptr, tmp=x6
1063         b       2f
1064 1:      ldr_this_cpu dst=x5, sym=sdei_stack_critical_ptr, tmp=x6
1065 2:      mov     x6, #SDEI_STACK_SIZE
1066         add     x5, x5, x6
1067         mov     sp, x5
1068 #endif
1069
1070 #ifdef CONFIG_SHADOW_CALL_STACK
1071         /* Use a separate shadow call stack for normal and critical events */
1072         cbnz    w4, 3f
1073         ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal_ptr, tmp=x6
1074         b       4f
1075 3:      ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical_ptr, tmp=x6
1076 4:
1077 #endif
1078
1079         /*
1080          * We may have interrupted userspace, or a guest, or exit-from or
1081          * return-to either of these. We can't trust sp_el0, restore it.
1082          */
1083         mrs     x28, sp_el0
1084         ldr_this_cpu    dst=x0, sym=__entry_task, tmp=x1
1085         msr     sp_el0, x0
1086
1087         /* If we interrupted the kernel point to the previous stack/frame. */
1088         and     x0, x3, #0xc
1089         mrs     x1, CurrentEL
1090         cmp     x0, x1
1091         csel    x29, x29, xzr, eq       // fp, or zero
1092         csel    x4, x2, xzr, eq         // elr, or zero
1093
1094         stp     x29, x4, [sp, #-16]!
1095         mov     x29, sp
1096
1097         add     x0, x19, #SDEI_EVENT_INTREGS
1098         mov     x1, x19
1099         bl      __sdei_handler
1100
1101         msr     sp_el0, x28
1102         /* restore regs >x17 that we clobbered */
1103         mov     x4, x19         // keep x4 for __sdei_asm_exit_trampoline
1104         ldp     x28, x29, [x4, #SDEI_EVENT_INTREGS + 16 * 14]
1105         ldp     x18, x19, [x4, #SDEI_EVENT_INTREGS + 16 * 9]
1106         ldp     lr, x1, [x4, #SDEI_EVENT_INTREGS + S_LR]
1107         mov     sp, x1
1108
1109         mov     x1, x0                  // address to complete_and_resume
1110         /* x0 = (x0 <= 1) ? EVENT_COMPLETE:EVENT_COMPLETE_AND_RESUME */
1111         cmp     x0, #1
1112         mov_q   x2, SDEI_1_0_FN_SDEI_EVENT_COMPLETE
1113         mov_q   x3, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME
1114         csel    x0, x2, x3, ls
1115
1116         ldr_l   x2, sdei_exit_mode
1117
1118 alternative_if_not ARM64_UNMAP_KERNEL_AT_EL0
1119         sdei_handler_exit exit_mode=x2
1120 alternative_else_nop_endif
1121
1122 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
1123         tramp_alias     dst=x5, sym=__sdei_asm_exit_trampoline
1124         br      x5
1125 #endif
1126 SYM_CODE_END(__sdei_asm_handler)
1127 NOKPROBE(__sdei_asm_handler)
1128 #endif /* CONFIG_ARM_SDE_INTERFACE */