Skip to content

Commit 62a679c

Browse files
mrutland-armwilldeacon
authored andcommitted
arm64: simplify ptrauth initialization
Currently __cpu_setup conditionally initializes the address authentication keys and enables them in SCTLR_EL1, doing so differently for the primary CPU and secondary CPUs, and skipping this work for CPUs returning from an idle state. For the latter case, cpu_do_resume restores the keys and SCTLR_EL1 value after the MMU has been enabled. This flow is rather difficult to follow, so instead let's move the primary and secondary CPU initialization into their respective boot paths. By following the example of cpu_do_resume and doing so once the MMU is enabled, we can always initialize the keys from the values in thread_struct, and avoid the machinery necessary to pass the keys in secondary_data or open-coding initialization for the boot CPU. This means we perform an additional RMW of SCTLR_EL1, but we already do this in the cpu_do_resume path, and for other features in cpufeature.c, so this isn't a major concern in a bringup path. Note that even while the enable bits are clear, the key registers are accessible. As this now renders the argument to __cpu_setup redundant, let's also remove that entirely. Future extensions can follow a similar approach to initialize values that differ for primary/secondary CPUs. Signed-off-by: Mark Rutland <[email protected]> Tested-by: Amit Daniel Kachhap <[email protected]> Reviewed-by: Amit Daniel Kachhap <[email protected]> Cc: Amit Daniel Kachhap <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: James Morse <[email protected]> Cc: Suzuki K Poulose <[email protected]> Cc: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent d0055da commit 62a679c

File tree

7 files changed

+32
-69
lines changed

7 files changed

+32
-69
lines changed

arch/arm64/include/asm/asm_pointer_auth.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ alternative_if ARM64_HAS_ADDRESS_AUTH
6060
alternative_else_nop_endif
6161
.endm
6262

63+
.macro __ptrauth_keys_init_cpu tsk, tmp1, tmp2, tmp3
64+
mrs \tmp1, id_aa64isar1_el1
65+
ubfx \tmp1, \tmp1, #ID_AA64ISAR1_APA_SHIFT, #8
66+
cbz \tmp1, .Lno_addr_auth\@
67+
mov_q \tmp1, (SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | \
68+
SCTLR_ELx_ENDA | SCTLR_ELx_ENDB)
69+
mrs \tmp2, sctlr_el1
70+
orr \tmp2, \tmp2, \tmp1
71+
msr sctlr_el1, \tmp2
72+
__ptrauth_keys_install_kernel_nosync \tsk, \tmp1, \tmp2, \tmp3
73+
isb
74+
.Lno_addr_auth\@:
75+
.endm
76+
77+
.macro ptrauth_keys_init_cpu tsk, tmp1, tmp2, tmp3
78+
alternative_if_not ARM64_HAS_ADDRESS_AUTH
79+
b .Lno_addr_auth\@
80+
alternative_else_nop_endif
81+
__ptrauth_keys_init_cpu \tsk, \tmp1, \tmp2, \tmp3
82+
.Lno_addr_auth\@:
83+
.endm
84+
6385
#else /* CONFIG_ARM64_PTR_AUTH */
6486

6587
.macro ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3

arch/arm64/include/asm/smp.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@
2323
#define CPU_STUCK_REASON_52_BIT_VA (UL(1) << CPU_STUCK_REASON_SHIFT)
2424
#define CPU_STUCK_REASON_NO_GRAN (UL(2) << CPU_STUCK_REASON_SHIFT)
2525

26-
/* Possible options for __cpu_setup */
27-
/* Option to setup primary cpu */
28-
#define ARM64_CPU_BOOT_PRIMARY (1)
29-
/* Option to setup secondary cpus */
30-
#define ARM64_CPU_BOOT_SECONDARY (2)
31-
/* Option to setup cpus for different cpu run time services */
32-
#define ARM64_CPU_RUNTIME (3)
33-
3426
#ifndef __ASSEMBLY__
3527

3628
#include <asm/percpu.h>
@@ -96,9 +88,6 @@ asmlinkage void secondary_start_kernel(void);
9688
struct secondary_data {
9789
void *stack;
9890
struct task_struct *task;
99-
#ifdef CONFIG_ARM64_PTR_AUTH
100-
struct ptrauth_keys_kernel ptrauth_key;
101-
#endif
10291
long status;
10392
};
10493

arch/arm64/kernel/asm-offsets.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ int main(void)
9292
BLANK();
9393
DEFINE(CPU_BOOT_STACK, offsetof(struct secondary_data, stack));
9494
DEFINE(CPU_BOOT_TASK, offsetof(struct secondary_data, task));
95-
#ifdef CONFIG_ARM64_PTR_AUTH
96-
DEFINE(CPU_BOOT_PTRAUTH_KEY, offsetof(struct secondary_data, ptrauth_key));
97-
#endif
9895
BLANK();
9996
#ifdef CONFIG_KVM_ARM_HOST
10097
DEFINE(VCPU_CONTEXT, offsetof(struct kvm_vcpu, arch.ctxt));

arch/arm64/kernel/head.S

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/init.h>
1414
#include <linux/irqchip/arm-gic-v3.h>
1515

16+
#include <asm/asm_pointer_auth.h>
1617
#include <asm/assembler.h>
1718
#include <asm/boot.h>
1819
#include <asm/ptrace.h>
@@ -118,7 +119,6 @@ SYM_CODE_START(stext)
118119
* On return, the CPU will be ready for the MMU to be turned on and
119120
* the TCR will have been set.
120121
*/
121-
mov x0, #ARM64_CPU_BOOT_PRIMARY
122122
bl __cpu_setup // initialise processor
123123
b __primary_switch
124124
SYM_CODE_END(stext)
@@ -417,6 +417,10 @@ SYM_FUNC_START_LOCAL(__primary_switched)
417417
adr_l x5, init_task
418418
msr sp_el0, x5 // Save thread_info
419419

420+
#ifdef CONFIG_ARM64_PTR_AUTH
421+
__ptrauth_keys_init_cpu x5, x6, x7, x8
422+
#endif
423+
420424
adr_l x8, vectors // load VBAR_EL1 with virtual
421425
msr vbar_el1, x8 // vector table address
422426
isb
@@ -717,7 +721,6 @@ SYM_FUNC_START_LOCAL(secondary_startup)
717721
* Common entry point for secondary CPUs.
718722
*/
719723
bl __cpu_secondary_check52bitva
720-
mov x0, #ARM64_CPU_BOOT_SECONDARY
721724
bl __cpu_setup // initialise processor
722725
adrp x1, swapper_pg_dir
723726
bl __enable_mmu
@@ -739,6 +742,11 @@ SYM_FUNC_START_LOCAL(__secondary_switched)
739742
msr sp_el0, x2
740743
mov x29, #0
741744
mov x30, #0
745+
746+
#ifdef CONFIG_ARM64_PTR_AUTH
747+
ptrauth_keys_init_cpu x2, x3, x4, x5
748+
#endif
749+
742750
b secondary_start_kernel
743751
SYM_FUNC_END(__secondary_switched)
744752

arch/arm64/kernel/sleep.S

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ ENDPROC(__cpu_suspend_enter)
100100
.pushsection ".idmap.text", "awx"
101101
ENTRY(cpu_resume)
102102
bl el2_setup // if in EL2 drop to EL1 cleanly
103-
mov x0, #ARM64_CPU_RUNTIME
104103
bl __cpu_setup
105104
/* enable the MMU early - so we can access sleep_save_stash by va */
106105
adrp x1, swapper_pg_dir

arch/arm64/kernel/smp.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle)
114114
*/
115115
secondary_data.task = idle;
116116
secondary_data.stack = task_stack_page(idle) + THREAD_SIZE;
117-
#if defined(CONFIG_ARM64_PTR_AUTH)
118-
secondary_data.ptrauth_key.apia.lo = idle->thread.keys_kernel.apia.lo;
119-
secondary_data.ptrauth_key.apia.hi = idle->thread.keys_kernel.apia.hi;
120-
#endif
121117
update_cpu_boot_status(CPU_MMU_OFF);
122118
__flush_dcache_area(&secondary_data, sizeof(secondary_data));
123119

@@ -140,10 +136,6 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle)
140136
pr_crit("CPU%u: failed to come online\n", cpu);
141137
secondary_data.task = NULL;
142138
secondary_data.stack = NULL;
143-
#if defined(CONFIG_ARM64_PTR_AUTH)
144-
secondary_data.ptrauth_key.apia.lo = 0;
145-
secondary_data.ptrauth_key.apia.hi = 0;
146-
#endif
147139
__flush_dcache_area(&secondary_data, sizeof(secondary_data));
148140
status = READ_ONCE(secondary_data.status);
149141
if (status == CPU_MMU_OFF)

arch/arm64/mm/proc.S

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,6 @@ SYM_FUNC_END(idmap_kpti_install_ng_mappings)
386386
*
387387
* Initialise the processor for turning the MMU on.
388388
*
389-
* Input:
390-
* x0 with a flag ARM64_CPU_BOOT_PRIMARY/ARM64_CPU_BOOT_SECONDARY/ARM64_CPU_RUNTIME.
391389
* Output:
392390
* Return in x0 the value of the SCTLR_EL1 register.
393391
*/
@@ -446,51 +444,9 @@ SYM_FUNC_START(__cpu_setup)
446444
1:
447445
#endif /* CONFIG_ARM64_HW_AFDBM */
448446
msr tcr_el1, x10
449-
mov x1, x0
450447
/*
451448
* Prepare SCTLR
452449
*/
453450
mov_q x0, SCTLR_EL1_SET
454-
455-
#ifdef CONFIG_ARM64_PTR_AUTH
456-
/* No ptrauth setup for run time cpus */
457-
cmp x1, #ARM64_CPU_RUNTIME
458-
b.eq 3f
459-
460-
/* Check if the CPU supports ptrauth */
461-
mrs x2, id_aa64isar1_el1
462-
ubfx x2, x2, #ID_AA64ISAR1_APA_SHIFT, #8
463-
cbz x2, 3f
464-
465-
/*
466-
* The primary cpu keys are reset here and can be
467-
* re-initialised with some proper values later.
468-
*/
469-
msr_s SYS_APIAKEYLO_EL1, xzr
470-
msr_s SYS_APIAKEYHI_EL1, xzr
471-
472-
/* Just enable ptrauth for primary cpu */
473-
cmp x1, #ARM64_CPU_BOOT_PRIMARY
474-
b.eq 2f
475-
476-
/* if !system_supports_address_auth() then skip enable */
477-
alternative_if_not ARM64_HAS_ADDRESS_AUTH
478-
b 3f
479-
alternative_else_nop_endif
480-
481-
/* Install ptrauth key for secondary cpus */
482-
adr_l x2, secondary_data
483-
ldr x3, [x2, #CPU_BOOT_TASK] // get secondary_data.task
484-
cbz x3, 2f // check for slow booting cpus
485-
ldp x3, x4, [x2, #CPU_BOOT_PTRAUTH_KEY]
486-
msr_s SYS_APIAKEYLO_EL1, x3
487-
msr_s SYS_APIAKEYHI_EL1, x4
488-
489-
2: /* Enable ptrauth instructions */
490-
ldr x2, =SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | \
491-
SCTLR_ELx_ENDA | SCTLR_ELx_ENDB
492-
orr x0, x0, x2
493-
3:
494-
#endif
495451
ret // return to head.S
496452
SYM_FUNC_END(__cpu_setup)

0 commit comments

Comments
 (0)