Skip to content

Commit 375b72e

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

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
@@ -2318,6 +2318,15 @@ config ARM64_DEBUG_PRIORITY_MASKING
23182318
If unsure, say N
23192319
endif # ARM64_PSEUDO_NMI
23202320

2321+
config ARM64_MEMORY_MODEL_CONTROL
2322+
bool "Runtime memory model control"
2323+
help
2324+
Some ARM64 CPUs support runtime switching of the CPU memory
2325+
model, which can be useful to emulate other CPU architectures
2326+
which have different memory models. Say Y to enable support
2327+
for the PR_SET_MEM_MODEL/PR_GET_MEM_MODEL prctl() calls on
2328+
CPUs with this feature.
2329+
23212330
config RELOCATABLE
23222331
bool "Build a relocatable kernel image" if EXPERT
23232332
select ARCH_HAS_RELR

arch/arm64/include/asm/cpufeature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,10 @@ static inline bool cpu_has_lpa2(void)
10481048
#endif
10491049
}
10501050

1051+
void __init init_cpucap_indirect_list_impdef(void);
1052+
void __init init_cpucap_indirect_list_from_array(const struct arm64_cpu_capabilities *caps);
1053+
bool cpufeature_matches(u64 reg, const struct arm64_cpu_capabilities *entry);
1054+
10511055
#endif /* __ASSEMBLY__ */
10521056

10531057
#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
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
@@ -1072,7 +1072,7 @@ static void init_cpu_ftr_reg(u32 sys_reg, u64 new)
10721072
extern const struct arm64_cpu_capabilities arm64_errata[];
10731073
static const struct arm64_cpu_capabilities arm64_features[];
10741074

1075-
static void __init
1075+
void __init
10761076
init_cpucap_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
10771077
{
10781078
for (; caps->matches; caps++) {
@@ -1579,8 +1579,8 @@ has_always(const struct arm64_cpu_capabilities *entry, int scope)
15791579
return true;
15801580
}
15811581

1582-
static bool
1583-
feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
1582+
bool
1583+
cpufeature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
15841584
{
15851585
int val, min, max;
15861586
u64 tmp;
@@ -1633,14 +1633,14 @@ has_user_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
16331633
if (!mask)
16341634
return false;
16351635

1636-
return feature_matches(val, entry);
1636+
return cpufeature_matches(val, entry);
16371637
}
16381638

16391639
static bool
16401640
has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
16411641
{
16421642
u64 val = read_scoped_sysreg(entry, scope);
1643-
return feature_matches(val, entry);
1643+
return cpufeature_matches(val, entry);
16441644
}
16451645

16461646
const struct cpumask *system_32bit_el0_cpumask(void)
@@ -3747,6 +3747,7 @@ void __init setup_boot_cpu_features(void)
37473747
* handle the boot CPU.
37483748
*/
37493749
init_cpucap_indirect_list();
3750+
init_cpucap_indirect_list_impdef();
37503751

37513752
/*
37523753
* 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 = SCOPE_LOCAL_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU,
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>
@@ -659,6 +660,25 @@ void update_sctlr_el1(u64 sctlr)
659660
isb();
660661
}
661662

663+
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
664+
int arch_prctl_mem_model_get(struct task_struct *t)
665+
{
666+
return PR_SET_MEM_MODEL_DEFAULT;
667+
}
668+
669+
int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
670+
{
671+
if (alternative_has_cap_unlikely(ARM64_HAS_TSO_FIXED) &&
672+
val == PR_SET_MEM_MODEL_TSO)
673+
return 0;
674+
675+
if (val == PR_SET_MEM_MODEL_DEFAULT)
676+
return 0;
677+
678+
return -EINVAL;
679+
}
680+
#endif
681+
662682
/*
663683
* Thread switching.
664684
*/
@@ -799,6 +819,10 @@ void arch_setup_new_exec(void)
799819
arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,
800820
PR_SPEC_ENABLE);
801821
}
822+
823+
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
824+
arch_prctl_mem_model_set(current, PR_SET_MEM_MODEL_DEFAULT);
825+
#endif
802826
}
803827

804828
#ifdef CONFIG_ARM64_TAGGED_ADDR_ABI

arch/arm64/tools/cpucaps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ HAS_STAGE2_FWB
5555
HAS_TCR2
5656
HAS_TIDCP1
5757
HAS_TLB_RANGE
58+
HAS_TSO_FIXED
5859
HAS_VA52
5960
HAS_VIRT_HOST_EXTN
6061
HAS_WFXT

0 commit comments

Comments
 (0)