Skip to content

Commit 10fb7b6

Browse files
marcanjannau
authored andcommitted
arm64: Introduce scaffolding to add ACTLR_EL1 to thread state
Some CPUs expose IMPDEF features in ACTLR_EL1 that can be meaningfully controlled per-thread (like TSO control on Apple cores). Add the basic scaffolding to save/restore this register as part of context switching. This mechanism is disabled by default both by config symbol and via a runtime check, which ensures it is never triggered unless the system is known to need it for some feature (which also implies that the layout of ACTLR_EL1 is uniform between all CPU core types). Signed-off-by: Hector Martin <marcan@marcan.st> Reviewed-by: Neal Gompa <neal@gompa.dev>
1 parent c8ea85b commit 10fb7b6

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

arch/arm64/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ config KASAN_SHADOW_OFFSET
431431
config UNWIND_TABLES
432432
bool
433433

434+
config ARM64_ACTLR_STATE
435+
bool
436+
434437
source "arch/arm64/Kconfig.platforms"
435438

436439
menu "Kernel Features"

arch/arm64/include/asm/cpufeature.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,11 @@ static inline unsigned int get_vmid_bits(u64 mmfr1)
943943
return 8;
944944
}
945945

946+
static __always_inline bool system_has_actlr_state(void)
947+
{
948+
return false;
949+
}
950+
946951
s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
947952
struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
948953

arch/arm64/include/asm/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ struct thread_struct {
192192
u64 gcs_base;
193193
u64 gcs_size;
194194
#endif
195+
#ifdef CONFIG_ARM64_ACTLR_STATE
196+
u64 actlr;
197+
#endif
195198
};
196199

197200
static inline unsigned int thread_get_vl(struct thread_struct *thread,

arch/arm64/kernel/process.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
434434
if (system_supports_poe())
435435
p->thread.por_el0 = read_sysreg_s(SYS_POR_EL0);
436436

437+
#ifdef CONFIG_ARM64_ACTLR_STATE
438+
if (system_has_actlr_state())
439+
p->thread.actlr = read_sysreg(actlr_el1);
440+
#endif
441+
437442
if (stack_start) {
438443
if (is_compat_thread(task_thread_info(p)))
439444
childregs->compat_sp = stack_start;
@@ -679,6 +684,25 @@ int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
679684
}
680685
#endif
681686

687+
#ifdef CONFIG_ARM64_ACTLR_STATE
688+
/*
689+
* IMPDEF control register ACTLR_EL1 handling. Some CPUs use this to
690+
* expose features that can be controlled by userspace.
691+
*/
692+
static void actlr_thread_switch(struct task_struct *next)
693+
{
694+
if (!system_has_actlr_state())
695+
return;
696+
697+
current->thread.actlr = read_sysreg(actlr_el1);
698+
write_sysreg(next->thread.actlr, actlr_el1);
699+
}
700+
#else
701+
static inline void actlr_thread_switch(struct task_struct *next)
702+
{
703+
}
704+
#endif
705+
682706
/*
683707
* Thread switching.
684708
*/
@@ -698,6 +722,7 @@ struct task_struct *__switch_to(struct task_struct *prev,
698722
ptrauth_thread_switch_user(next);
699723
permission_overlay_switch(next);
700724
gcs_thread_switch(next);
725+
actlr_thread_switch(next);
701726

702727
/*
703728
* Complete any pending TLB or cache maintenance on this CPU in case

arch/arm64/kernel/setup.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)
370370
*/
371371
init_task.thread_info.ttbr0 = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
372372
#endif
373+
#ifdef CONFIG_ARM64_ACTLR_STATE
374+
/* Store the boot CPU ACTLR_EL1 value as the default. This will only
375+
* be actually restored during context switching iff the platform is
376+
* known to use ACTLR_EL1 for exposable features and its layout is
377+
* known to be the same on all CPUs.
378+
*/
379+
init_task.thread.actlr = read_sysreg(actlr_el1);
380+
#endif
373381

374382
if (boot_args[1] || boot_args[2] || boot_args[3]) {
375383
pr_err("WARNING: x1-x3 nonzero in violation of boot protocol:\n"

0 commit comments

Comments
 (0)