Skip to content

Commit 0ae3b13

Browse files
ardbiesheuvelctmarinas
authored andcommitted
arm64/entry: deduplicate SW PAN entry/exit routines
Factor the 12 copies of the SW PAN entry and exit code into callable subroutines, and use alternatives patching to either emit a 'bl' instruction to call them, or a NOP if h/w PAN is found to be available at runtime. Signed-off-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 493cf9b commit 0ae3b13

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

arch/arm64/kernel/entry.S

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -209,28 +209,9 @@ alternative_cb_end
209209
add x29, sp, #S_STACKFRAME
210210

211211
#ifdef CONFIG_ARM64_SW_TTBR0_PAN
212-
/*
213-
* Set the TTBR0 PAN bit in SPSR. When the exception is taken from
214-
* EL0, there is no need to check the state of TTBR0_EL1 since
215-
* accesses are always enabled.
216-
* Note that the meaning of this bit differs from the ARMv8.1 PAN
217-
* feature as all TTBR0_EL1 accesses are disabled, not just those to
218-
* user mappings.
219-
*/
220-
alternative_if ARM64_HAS_PAN
221-
b 1f // skip TTBR0 PAN
212+
alternative_if_not ARM64_HAS_PAN
213+
bl __swpan_entry_el\el
222214
alternative_else_nop_endif
223-
224-
.if \el != 0
225-
mrs x21, ttbr0_el1
226-
tst x21, #TTBR_ASID_MASK // Check for the reserved ASID
227-
orr x23, x23, #PSR_PAN_BIT // Set the emulated PAN in the saved SPSR
228-
b.eq 1f // TTBR0 access already disabled
229-
and x23, x23, #~PSR_PAN_BIT // Clear the emulated PAN in the saved SPSR
230-
.endif
231-
232-
__uaccess_ttbr0_disable x21
233-
1:
234215
#endif
235216

236217
stp x22, x23, [sp, #S_PC]
@@ -284,34 +265,9 @@ alternative_else_nop_endif
284265
.endif
285266

286267
#ifdef CONFIG_ARM64_SW_TTBR0_PAN
287-
/*
288-
* Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR
289-
* PAN bit checking.
290-
*/
291-
alternative_if ARM64_HAS_PAN
292-
b 2f // skip TTBR0 PAN
268+
alternative_if_not ARM64_HAS_PAN
269+
bl __swpan_exit_el\el
293270
alternative_else_nop_endif
294-
295-
.if \el != 0
296-
tbnz x22, #22, 1f // Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set
297-
.endif
298-
299-
__uaccess_ttbr0_enable x0, x1
300-
301-
.if \el == 0
302-
/*
303-
* Enable errata workarounds only if returning to user. The only
304-
* workaround currently required for TTBR0_EL1 changes are for the
305-
* Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache
306-
* corruption).
307-
*/
308-
bl post_ttbr_update_workaround
309-
.endif
310-
1:
311-
.if \el != 0
312-
and x22, x22, #~PSR_PAN_BIT // ARMv8.0 CPUs do not understand this bit
313-
.endif
314-
2:
315271
#endif
316272

317273
.if \el == 0
@@ -391,6 +347,49 @@ alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
391347
sb
392348
.endm
393349

350+
#ifdef CONFIG_ARM64_SW_TTBR0_PAN
351+
/*
352+
* Set the TTBR0 PAN bit in SPSR. When the exception is taken from
353+
* EL0, there is no need to check the state of TTBR0_EL1 since
354+
* accesses are always enabled.
355+
* Note that the meaning of this bit differs from the ARMv8.1 PAN
356+
* feature as all TTBR0_EL1 accesses are disabled, not just those to
357+
* user mappings.
358+
*/
359+
SYM_CODE_START_LOCAL(__swpan_entry_el1)
360+
mrs x21, ttbr0_el1
361+
tst x21, #TTBR_ASID_MASK // Check for the reserved ASID
362+
orr x23, x23, #PSR_PAN_BIT // Set the emulated PAN in the saved SPSR
363+
b.eq 1f // TTBR0 access already disabled
364+
and x23, x23, #~PSR_PAN_BIT // Clear the emulated PAN in the saved SPSR
365+
SYM_INNER_LABEL(__swpan_entry_el0, SYM_L_LOCAL)
366+
__uaccess_ttbr0_disable x21
367+
1: ret
368+
SYM_CODE_END(__swpan_entry_el1)
369+
370+
/*
371+
* Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR
372+
* PAN bit checking.
373+
*/
374+
SYM_CODE_START_LOCAL(__swpan_exit_el1)
375+
tbnz x22, #22, 1f // Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set
376+
__uaccess_ttbr0_enable x0, x1
377+
1: and x22, x22, #~PSR_PAN_BIT // ARMv8.0 CPUs do not understand this bit
378+
ret
379+
SYM_CODE_END(__swpan_exit_el1)
380+
381+
SYM_CODE_START_LOCAL(__swpan_exit_el0)
382+
__uaccess_ttbr0_enable x0, x1
383+
/*
384+
* Enable errata workarounds only if returning to user. The only
385+
* workaround currently required for TTBR0_EL1 changes are for the
386+
* Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache
387+
* corruption).
388+
*/
389+
b post_ttbr_update_workaround
390+
SYM_CODE_END(__swpan_exit_el0)
391+
#endif
392+
394393
.macro irq_stack_entry
395394
mov x19, sp // preserve the original sp
396395
#ifdef CONFIG_SHADOW_CALL_STACK

0 commit comments

Comments
 (0)