Skip to content

Commit dfa5c16

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 375b72e commit dfa5c16

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
@@ -439,6 +439,9 @@ config KASAN_SHADOW_OFFSET
439439
config UNWIND_TABLES
440440
bool
441441

442+
config ARM64_ACTLR_STATE
443+
bool
444+
442445
source "arch/arm64/Kconfig.platforms"
443446

444447
menu "Kernel Features"

arch/arm64/include/asm/cpufeature.h

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

928+
static __always_inline bool system_has_actlr_state(void)
929+
{
930+
return false;
931+
}
932+
928933
s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
929934
struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
930935

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
@@ -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)