Skip to content

Commit 1d75ef8

Browse files
marcanherrnst
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 <[email protected]>
1 parent 64cc7cb commit 1d75ef8

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

arch/arm64/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ config KASAN_SHADOW_OFFSET
411411
config UNWIND_TABLES
412412
bool
413413

414+
config ARM64_ACTLR_STATE
415+
bool
416+
414417
source "arch/arm64/Kconfig.platforms"
415418

416419
menu "Kernel Features"

arch/arm64/include/asm/cpufeature.h

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

911+
static __always_inline bool system_has_actlr_state(void)
912+
{
913+
return false;
914+
}
915+
911916
s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
912917
struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
913918

arch/arm64/include/asm/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ struct thread_struct {
179179
u64 sctlr_user;
180180
u64 svcr;
181181
u64 tpidr2_el0;
182+
#ifdef CONFIG_ARM64_ACTLR_STATE
183+
u64 actlr;
184+
#endif
182185
};
183186

184187
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
@@ -374,6 +374,11 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
374374
if (system_supports_tpidr2())
375375
p->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0);
376376

377+
#ifdef CONFIG_ARM64_ACTLR_STATE
378+
if (system_has_actlr_state())
379+
p->thread.actlr = read_sysreg(actlr_el1);
380+
#endif
381+
377382
if (stack_start) {
378383
if (is_compat_thread(task_thread_info(p)))
379384
childregs->compat_sp = stack_start;
@@ -516,6 +521,25 @@ void update_sctlr_el1(u64 sctlr)
516521
isb();
517522
}
518523

524+
#ifdef CONFIG_ARM64_ACTLR_STATE
525+
/*
526+
* IMPDEF control register ACTLR_EL1 handling. Some CPUs use this to
527+
* expose features that can be controlled by userspace.
528+
*/
529+
static void actlr_thread_switch(struct task_struct *next)
530+
{
531+
if (!system_has_actlr_state())
532+
return;
533+
534+
current->thread.actlr = read_sysreg(actlr_el1);
535+
write_sysreg(next->thread.actlr, actlr_el1);
536+
}
537+
#else
538+
static inline void actlr_thread_switch(struct task_struct *next)
539+
{
540+
}
541+
#endif
542+
519543
/*
520544
* Thread switching.
521545
*/
@@ -533,6 +557,7 @@ struct task_struct *__switch_to(struct task_struct *prev,
533557
ssbs_thread_switch(next);
534558
erratum_1418040_thread_switch(next);
535559
ptrauth_thread_switch_user(next);
560+
actlr_thread_switch(next);
536561

537562
/*
538563
* 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
@@ -382,6 +382,14 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)
382382
*/
383383
init_task.thread_info.ttbr0 = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
384384
#endif
385+
#ifdef CONFIG_ARM64_ACTLR_STATE
386+
/* Store the boot CPU ACTLR_EL1 value as the default. This will only
387+
* be actually restored during context switching iff the platform is
388+
* known to use ACTLR_EL1 for exposable features and its layout is
389+
* known to be the same on all CPUs.
390+
*/
391+
init_task.thread.actlr = read_sysreg(actlr_el1);
392+
#endif
385393

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

0 commit comments

Comments
 (0)