Skip to content

Commit 1a9452f

Browse files
committed
Merge branch 'refs/heads/bits/220-tso' into asahi-wip
2 parents e95f65b + 38015ad commit 1a9452f

12 files changed

Lines changed: 202 additions & 3 deletions

File tree

arch/arm64/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ config KASAN_SHADOW_OFFSET
381381
config UNWIND_TABLES
382382
bool
383383

384+
config ARM64_ACTLR_STATE
385+
bool
386+
384387
source "arch/arm64/Kconfig.platforms"
385388

386389
menu "Kernel Features"
@@ -2128,6 +2131,17 @@ config ARM64_DEBUG_PRIORITY_MASKING
21282131
If unsure, say N
21292132
endif # ARM64_PSEUDO_NMI
21302133

2134+
config ARM64_MEMORY_MODEL_CONTROL
2135+
bool "Runtime memory model control"
2136+
default ARCH_APPLE
2137+
select ARM64_ACTLR_STATE
2138+
help
2139+
Some ARM64 CPUs support runtime switching of the CPU memory
2140+
model, which can be useful to emulate other CPU architectures
2141+
which have different memory models. Say Y to enable support
2142+
for the PR_SET_MEM_MODEL/PR_GET_MEM_MODEL prctl() calls on
2143+
CPUs with this feature.
2144+
21312145
config RELOCATABLE
21322146
bool "Build a relocatable kernel image" if EXPERT
21332147
select ARCH_HAS_RELR
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#ifndef __ASM_APPLE_CPUFEATURES_H
4+
#define __ASM_APPLE_CPUFEATURES_H
5+
6+
#include <linux/bits.h>
7+
#include <asm/sysreg.h>
8+
9+
#define AIDR_APPLE_TSO_SHIFT 9
10+
#define AIDR_APPLE_TSO BIT(9)
11+
12+
#define ACTLR_APPLE_TSO_SHIFT 1
13+
#define ACTLR_APPLE_TSO BIT(1)
14+
15+
#endif

arch/arm64/include/asm/cpufeature.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,12 @@ static inline unsigned int get_vmid_bits(u64 mmfr1)
915915
return 8;
916916
}
917917

918+
static __always_inline bool system_has_actlr_state(void)
919+
{
920+
return IS_ENABLED(CONFIG_ARM64_ACTLR_STATE) &&
921+
cpus_have_const_cap(ARM64_HAS_TSO_APPLE);
922+
}
923+
918924
struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
919925

920926
extern struct arm64_ftr_override id_aa64mmfr1_override;

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/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ obj-y := debug-monitors.o entry.o irq.o fpsimd.o \
3434
cpufeature.o alternative.o cacheinfo.o \
3535
smp.o smp_spin_table.o topology.o smccc-call.o \
3636
syscall.o proton-pack.o idreg-override.o idle.o \
37-
patching.o
37+
patching.o cpufeature_impdef.o
3838

3939
obj-$(CONFIG_COMPAT) += sys32.o signal32.o \
4040
sys_compat.o

arch/arm64/kernel/cpufeature.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ DEFINE_STATIC_KEY_FALSE(arm64_mismatched_32bit_el0);
134134
*/
135135
static cpumask_var_t cpu_32bit_el0_mask __cpumask_var_read_mostly;
136136

137+
void __init init_cpu_hwcaps_indirect_list_impdef(void);
138+
137139
void dump_cpu_features(void)
138140
{
139141
/* file-wide pr_fmt adds "CPU features: " prefix */
@@ -946,7 +948,7 @@ static void init_cpu_ftr_reg(u32 sys_reg, u64 new)
946948
extern const struct arm64_cpu_capabilities arm64_errata[];
947949
static const struct arm64_cpu_capabilities arm64_features[];
948950

949-
static void __init
951+
void __init
950952
init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
951953
{
952954
for (; caps->matches; caps++) {
@@ -1046,6 +1048,7 @@ void __init init_cpu_features(struct cpuinfo_arm64 *info)
10461048
* before we handle the boot CPU below.
10471049
*/
10481050
init_cpu_hwcaps_indirect_list();
1051+
init_cpu_hwcaps_indirect_list_impdef();
10491052

10501053
/*
10511054
* Detect and enable early CPU capabilities based on the boot CPU,
@@ -1414,7 +1417,7 @@ has_always(const struct arm64_cpu_capabilities *entry, int scope)
14141417
return true;
14151418
}
14161419

1417-
static bool
1420+
bool
14181421
feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
14191422
{
14201423
int val = cpuid_feature_extract_field_width(reg, entry->field_pos,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Contains implementation-defined CPU feature definitions.
4+
*/
5+
6+
#include <asm/cpufeature.h>
7+
#include <asm/apple_cpufeature.h>
8+
9+
void __init init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps);
10+
bool feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry);
11+
12+
bool has_apple_feature(const struct arm64_cpu_capabilities *entry, int scope)
13+
{
14+
u64 val;
15+
WARN_ON(scope != SCOPE_SYSTEM);
16+
17+
if (read_cpuid_implementor() != ARM_CPU_IMP_APPLE)
18+
return false;
19+
20+
val = read_sysreg(aidr_el1);
21+
return feature_matches(val, entry);
22+
}
23+
24+
bool has_tso_fixed(const struct arm64_cpu_capabilities *entry, int scope)
25+
{
26+
/* List of CPUs that always use the TSO memory model */
27+
static const struct midr_range fixed_tso_list[] = {
28+
MIDR_ALL_VERSIONS(MIDR_NVIDIA_DENVER),
29+
MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL),
30+
MIDR_ALL_VERSIONS(MIDR_FUJITSU_A64FX),
31+
{ /* sentinel */ }
32+
};
33+
34+
return is_midr_in_range_list(read_cpuid_id(), fixed_tso_list);
35+
}
36+
37+
static const struct arm64_cpu_capabilities arm64_impdef_features[] = {
38+
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
39+
{
40+
.desc = "TSO memory model (Apple)",
41+
.capability = ARM64_HAS_TSO_APPLE,
42+
.type = ARM64_CPUCAP_SYSTEM_FEATURE,
43+
.matches = has_apple_feature,
44+
.field_pos = AIDR_APPLE_TSO_SHIFT,
45+
.field_width = 1,
46+
.sign = FTR_UNSIGNED,
47+
.min_field_value = 1,
48+
},
49+
{
50+
.desc = "TSO memory model (Fixed)",
51+
.capability = ARM64_HAS_TSO_FIXED,
52+
.type = ARM64_CPUCAP_SYSTEM_FEATURE,
53+
.matches = has_tso_fixed,
54+
},
55+
#endif
56+
{},
57+
};
58+
59+
void __init init_cpu_hwcaps_indirect_list_impdef(void)
60+
{
61+
init_cpu_hwcaps_indirect_list_from_array(arm64_impdef_features);
62+
}

arch/arm64/kernel/process.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <linux/stacktrace.h>
4444

4545
#include <asm/alternative.h>
46+
#include <asm/apple_cpufeature.h>
4647
#include <asm/compat.h>
4748
#include <asm/cpufeature.h>
4849
#include <asm/cacheflush.h>
@@ -374,6 +375,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
374375
if (system_supports_tpidr2())
375376
p->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0);
376377

378+
if (system_has_actlr_state())
379+
p->thread.actlr = read_sysreg(actlr_el1);
380+
377381
if (stack_start) {
378382
if (is_compat_thread(task_thread_info(p)))
379383
childregs->compat_sp = stack_start;
@@ -516,6 +520,58 @@ void update_sctlr_el1(u64 sctlr)
516520
isb();
517521
}
518522

523+
/*
524+
* IMPDEF control register ACTLR_EL1 handling. Some CPUs use this to
525+
* expose features that can be controlled by userspace.
526+
*/
527+
static void actlr_thread_switch(struct task_struct *next)
528+
{
529+
if (!system_has_actlr_state())
530+
return;
531+
532+
current->thread.actlr = read_sysreg(actlr_el1);
533+
write_sysreg(next->thread.actlr, actlr_el1);
534+
}
535+
536+
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
537+
int arch_prctl_mem_model_get(struct task_struct *t)
538+
{
539+
if (cpus_have_const_cap(ARM64_HAS_TSO_APPLE) &&
540+
t->thread.actlr & ACTLR_APPLE_TSO)
541+
return PR_SET_MEM_MODEL_TSO;
542+
543+
return PR_SET_MEM_MODEL_DEFAULT;
544+
}
545+
546+
int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
547+
{
548+
if (cpus_have_const_cap(ARM64_HAS_TSO_FIXED) && val == PR_SET_MEM_MODEL_TSO)
549+
return 0;
550+
551+
if (cpus_have_const_cap(ARM64_HAS_TSO_APPLE)) {
552+
WARN_ON(!system_has_actlr_state());
553+
554+
switch (val) {
555+
case PR_SET_MEM_MODEL_TSO:
556+
t->thread.actlr |= ACTLR_APPLE_TSO;
557+
break;
558+
case PR_SET_MEM_MODEL_DEFAULT:
559+
t->thread.actlr &= ~ACTLR_APPLE_TSO;
560+
break;
561+
default:
562+
return -EINVAL;
563+
}
564+
write_sysreg(t->thread.actlr, actlr_el1);
565+
return 0;
566+
}
567+
568+
if (val == PR_SET_MEM_MODEL_DEFAULT)
569+
return 0;
570+
571+
return -EINVAL;
572+
}
573+
#endif
574+
519575
/*
520576
* Thread switching.
521577
*/
@@ -533,6 +589,7 @@ struct task_struct *__switch_to(struct task_struct *prev,
533589
ssbs_thread_switch(next);
534590
erratum_1418040_thread_switch(next);
535591
ptrauth_thread_switch_user(next);
592+
actlr_thread_switch(next);
536593

537594
/*
538595
* Complete any pending TLB or cache maintenance on this CPU in case
@@ -654,6 +711,10 @@ void arch_setup_new_exec(void)
654711
arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,
655712
PR_SPEC_ENABLE);
656713
}
714+
715+
if (IS_ENABLED(CONFIG_ARM64_MEMORY_MODEL_CONTROL)) {
716+
arch_prctl_mem_model_set(current, PR_SET_MEM_MODEL_DEFAULT);
717+
}
657718
}
658719

659720
#ifdef CONFIG_ARM64_TAGGED_ADDR_ABI

arch/arm64/kernel/setup.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)
380380
*/
381381
init_task.thread_info.ttbr0 = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
382382
#endif
383+
#ifdef CONFIG_ARM64_ACTLR_STATE
384+
/* Store the boot CPU ACTLR_EL1 value as the default. This will only
385+
* be actually restored during context switching iff the platform is
386+
* known to use ACTLR_EL1 for exposable features and its layout is
387+
* known to be the same on all CPUs.
388+
*/
389+
init_task.thread.actlr = read_sysreg(actlr_el1);
390+
#endif
383391

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

arch/arm64/tools/cpucaps

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ HAS_SB
4343
HAS_STAGE2_FWB
4444
HAS_TIDCP1
4545
HAS_TLB_RANGE
46+
HAS_TSO_APPLE
47+
HAS_TSO_FIXED
4648
HAS_VIRT_HOST_EXTN
4749
HAS_WFXT
4850
HW_DBM

0 commit comments

Comments
 (0)