Skip to content

Commit a011e93

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 cccf984 commit a011e93

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
@@ -441,6 +441,9 @@ config KASAN_SHADOW_OFFSET
441441
config UNWIND_TABLES
442442
bool
443443

444+
config ARM64_ACTLR_STATE
445+
bool
446+
444447
source "arch/arm64/Kconfig.platforms"
445448

446449
menu "Kernel Features"

arch/arm64/include/asm/cpufeature.h

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

956+
static __always_inline bool system_has_actlr_state(void)
957+
{
958+
return false;
959+
}
960+
956961
s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
957962
struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
958963

arch/arm64/include/asm/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ struct thread_struct {
194194
u64 gcs_base;
195195
u64 gcs_size;
196196
#endif
197+
#ifdef CONFIG_ARM64_ACTLR_STATE
198+
u64 actlr;
199+
#endif
197200
};
198201

199202
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
@@ -442,6 +442,11 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
442442
if (system_supports_poe())
443443
p->thread.por_el0 = read_sysreg_s(SYS_POR_EL0);
444444

445+
#ifdef CONFIG_ARM64_ACTLR_STATE
446+
if (system_has_actlr_state())
447+
p->thread.actlr = read_sysreg(actlr_el1);
448+
#endif
449+
445450
if (stack_start) {
446451
if (is_compat_thread(task_thread_info(p)))
447452
childregs->compat_sp = stack_start;
@@ -718,6 +723,25 @@ int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
718723
}
719724
#endif
720725

726+
#ifdef CONFIG_ARM64_ACTLR_STATE
727+
/*
728+
* IMPDEF control register ACTLR_EL1 handling. Some CPUs use this to
729+
* expose features that can be controlled by userspace.
730+
*/
731+
static void actlr_thread_switch(struct task_struct *next)
732+
{
733+
if (!system_has_actlr_state())
734+
return;
735+
736+
current->thread.actlr = read_sysreg(actlr_el1);
737+
write_sysreg(next->thread.actlr, actlr_el1);
738+
}
739+
#else
740+
static inline void actlr_thread_switch(struct task_struct *next)
741+
{
742+
}
743+
#endif
744+
721745
/*
722746
* Thread switching.
723747
*/
@@ -737,6 +761,7 @@ struct task_struct *__switch_to(struct task_struct *prev,
737761
ptrauth_thread_switch_user(next);
738762
permission_overlay_switch(next);
739763
gcs_thread_switch(next);
764+
actlr_thread_switch(next);
740765

741766
/*
742767
* Complete any pending TLB or cache maintenance on this CPU in case the

arch/arm64/kernel/setup.c

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

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

0 commit comments

Comments
 (0)