Skip to content

Commit 6ace16e

Browse files
marcanjannau
authored andcommitted
arm64: Implement PR_{GET,SET}_MEM_MODEL for always-TSO CPUs
Some ARM64 implementations are known to always use the TSO memory model. Add trivial support for the PR_{GET,SET}_MEM_MODEL prctl, which allows userspace to learn this fact. Known TSO implementations: - Nvidia Denver - Nvidia Carmel - Fujitsu A64FX Signed-off-by: Hector Martin <marcan@marcan.st> Reviewed-by: Neal Gompa <neal@gompa.dev>
1 parent 244735d commit 6ace16e

7 files changed

Lines changed: 83 additions & 5 deletions

File tree

arch/arm64/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,15 @@ config ARM64_DEBUG_PRIORITY_MASKING
22682268
If unsure, say N
22692269
endif # ARM64_PSEUDO_NMI
22702270

2271+
config ARM64_MEMORY_MODEL_CONTROL
2272+
bool "Runtime memory model control"
2273+
help
2274+
Some ARM64 CPUs support runtime switching of the CPU memory
2275+
model, which can be useful to emulate other CPU architectures
2276+
which have different memory models. Say Y to enable support
2277+
for the PR_SET_MEM_MODEL/PR_GET_MEM_MODEL prctl() calls on
2278+
CPUs with this feature.
2279+
22712280
config RELOCATABLE
22722281
bool "Build a relocatable kernel image" if EXPERT
22732282
select ARCH_HAS_RELR

arch/arm64/include/asm/cpufeature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,10 @@ static inline bool cpu_has_lpa2(void)
10781078
#endif
10791079
}
10801080

1081+
void __init init_cpucap_indirect_list_impdef(void);
1082+
void __init init_cpucap_indirect_list_from_array(const struct arm64_cpu_capabilities *caps);
1083+
bool cpufeature_matches(u64 reg, const struct arm64_cpu_capabilities *entry);
1084+
10811085
#endif /* __ASSEMBLY__ */
10821086

10831087
#endif

arch/arm64/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +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 idle.o patching.o pi/ \
37+
cpufeature_impdef.o \
3738
rsi.o jump_label.o
3839

3940
obj-$(CONFIG_COMPAT) += sys32.o signal32.o \

arch/arm64/kernel/cpufeature.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ static void init_cpu_ftr_reg(u32 sys_reg, u64 new)
10801080
extern const struct arm64_cpu_capabilities arm64_errata[];
10811081
static const struct arm64_cpu_capabilities arm64_features[];
10821082

1083-
static void __init
1083+
void __init
10841084
init_cpucap_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
10851085
{
10861086
for (; caps->matches; caps++) {
@@ -1592,8 +1592,8 @@ has_always(const struct arm64_cpu_capabilities *entry, int scope)
15921592
return true;
15931593
}
15941594

1595-
static bool
1596-
feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
1595+
bool
1596+
cpufeature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
15971597
{
15981598
int val, min, max;
15991599
u64 tmp;
@@ -1646,14 +1646,14 @@ has_user_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
16461646
if (!mask)
16471647
return false;
16481648

1649-
return feature_matches(val, entry);
1649+
return cpufeature_matches(val, entry);
16501650
}
16511651

16521652
static bool
16531653
has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
16541654
{
16551655
u64 val = read_scoped_sysreg(entry, scope);
1656-
return feature_matches(val, entry);
1656+
return cpufeature_matches(val, entry);
16571657
}
16581658

16591659
const struct cpumask *system_32bit_el0_cpumask(void)
@@ -3829,6 +3829,7 @@ void __init setup_boot_cpu_features(void)
38293829
* handle the boot CPU.
38303830
*/
38313831
init_cpucap_indirect_list();
3832+
init_cpucap_indirect_list_impdef();
38323833

38333834
/*
38343835
* Detect broken pseudo-NMI. Must be called _before_ the call to
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Contains implementation-defined CPU feature definitions.
4+
*/
5+
6+
#include <asm/cpufeature.h>
7+
8+
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
9+
static bool has_tso_fixed(const struct arm64_cpu_capabilities *entry, int scope)
10+
{
11+
/* List of CPUs that always use the TSO memory model */
12+
static const struct midr_range fixed_tso_list[] = {
13+
MIDR_ALL_VERSIONS(MIDR_NVIDIA_DENVER),
14+
MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL),
15+
MIDR_ALL_VERSIONS(MIDR_FUJITSU_A64FX),
16+
{ /* sentinel */ }
17+
};
18+
19+
return is_midr_in_range_list(fixed_tso_list);
20+
}
21+
#endif
22+
23+
static const struct arm64_cpu_capabilities arm64_impdef_features[] = {
24+
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
25+
{
26+
.desc = "TSO memory model (Fixed)",
27+
.capability = ARM64_HAS_TSO_FIXED,
28+
.type = ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE,
29+
.matches = has_tso_fixed,
30+
},
31+
#endif
32+
{},
33+
};
34+
35+
void __init init_cpucap_indirect_list_impdef(void)
36+
{
37+
init_cpucap_indirect_list_from_array(arm64_impdef_features);
38+
}

arch/arm64/kernel/process.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <linux/thread_info.h>
4242
#include <linux/prctl.h>
4343
#include <linux/stacktrace.h>
44+
#include <linux/memory_ordering_model.h>
4445

4546
#include <asm/alternative.h>
4647
#include <asm/arch_timer.h>
@@ -698,6 +699,25 @@ void update_sctlr_el1(u64 sctlr)
698699
isb();
699700
}
700701

702+
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
703+
int arch_prctl_mem_model_get(struct task_struct *t)
704+
{
705+
return PR_SET_MEM_MODEL_DEFAULT;
706+
}
707+
708+
int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
709+
{
710+
if (alternative_has_cap_unlikely(ARM64_HAS_TSO_FIXED) &&
711+
val == PR_SET_MEM_MODEL_TSO)
712+
return 0;
713+
714+
if (val == PR_SET_MEM_MODEL_DEFAULT)
715+
return 0;
716+
717+
return -EINVAL;
718+
}
719+
#endif
720+
701721
/*
702722
* Thread switching.
703723
*/
@@ -839,6 +859,10 @@ void arch_setup_new_exec(void)
839859
arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,
840860
PR_SPEC_ENABLE);
841861
}
862+
863+
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
864+
arch_prctl_mem_model_set(current, PR_SET_MEM_MODEL_DEFAULT);
865+
#endif
842866
}
843867

844868
#ifdef CONFIG_ARM64_TAGGED_ADDR_ABI

arch/arm64/tools/cpucaps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ HAS_STAGE2_FWB
6161
HAS_TCR2
6262
HAS_TIDCP1
6363
HAS_TLB_RANGE
64+
HAS_TSO_FIXED
6465
HAS_VA52
6566
HAS_VIRT_HOST_EXTN
6667
HAS_WFXT

0 commit comments

Comments
 (0)