Skip to content

Commit a53a809

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 2faf731 commit a53a809

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
@@ -444,6 +444,9 @@ config KASAN_SHADOW_OFFSET
444444
config UNWIND_TABLES
445445
bool
446446

447+
config ARM64_ACTLR_STATE
448+
bool
449+
447450
source "arch/arm64/Kconfig.platforms"
448451

449452
menu "Kernel Features"

arch/arm64/include/asm/cpufeature.h

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

958+
static __always_inline bool system_has_actlr_state(void)
959+
{
960+
return false;
961+
}
962+
958963
s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
959964
struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
960965

arch/arm64/include/asm/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ struct thread_struct {
199199
u64 gcs_base;
200200
u64 gcs_size;
201201
#endif
202+
#ifdef CONFIG_ARM64_ACTLR_STATE
203+
u64 actlr;
204+
#endif
202205
};
203206

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

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

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

742767
/*
743768
* 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)