Skip to contentSkip to conversation

x86/cpuid: Use AMD's NullSelectorClearsBase CPUID bit

PATCH In review Jane Malalane 3 participants 13 messages Sep 6, 2021 – Sep 8, 2021

lore
Share & tools
lore
#0 lore raw

cover x86/cpuid: Use AMD's NullSelectorClearsBase CPUID bit

Jane Malalane (2):
x86/cpuid: Expose NullSelectorClearsBase CPUID bit to guests x86/cpuid: Detect null segment behaviour on Zen2 CPUs

 tools/libs/light/libxl_cpuid.c              |  1 +
 tools/misc/xen-cpuid.c                      |  1 +
 xen/arch/x86/cpu/amd.c                      | 18 ++++++++++++++++++
 xen/arch/x86/cpu/cpu.h                      |  1 +
 xen/arch/x86/cpu/hygon.c                    |  5 +++++
 xen/include/asm-x86/cpufeature.h            |  1 +
 xen/include/public/arch-x86/cpufeatureset.h |  1 +
 7 files changed, 28 insertions(+)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/2 x86/cpuid: Expose NullSelectorClearsBase CPUID bit to guests

AMD Zen3 adds the NullSelectorClearsBase bit to indicate that loading a NULL segment selector zeroes the base and limit fields, as well as just attributes.

Expose bit to all guests.

---
CC: Wei Liu <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Pu Wen <[email protected]>
CC: Andy Lutomirski <[email protected]>

---
 tools/libs/light/libxl_cpuid.c              | 1 +
 tools/misc/xen-cpuid.c                      | 1 +
 xen/include/public/arch-x86/cpufeatureset.h | 1 +
 3 files changed, 3 insertions(+)
tools/libs/light/libxl_cpuid.c +1 -0
index b2c673841a..d667c36f31 100644
@@ -289,6 +289,7 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str)
         {"svm_pausefilt",0x8000000a, NA, CPUID_REG_EDX, 10,  1},
 
         {"lfence+",      0x80000021, NA, CPUID_REG_EAX,  2,  1},
+        {"nscb",         0x80000021, NA, CPUID_REG_EAX,  6,  1},
 
         {"maxhvleaf",    0x40000000, NA, CPUID_REG_EAX,  0,  8},
 
tools/misc/xen-cpuid.c +1 -0
index 735bcf8f0e..d79e67ecfb 100644
@@ -185,6 +185,7 @@ static const char *const str_7a1[32] =
 static const char *const str_e21a[32] =
 {
     [ 2] = "lfence+",
+    [ 6] = "nscb",
 };
 
 static const struct {
xen/include/public/arch-x86/cpufeatureset.h +1 -0
index 380b51b1b3..e5a7c94c78 100644
@@ -285,6 +285,7 @@ XEN_CPUFEATURE(FSRCS,        10*32+12) /*A  Fast Short REP CMPSB/SCASB */
 
 /* AMD-defined CPU features, CPUID level 0x80000021.eax, word 11 */
 XEN_CPUFEATURE(LFENCE_DISPATCH,    11*32+ 2) /*A  LFENCE always serializing */
+XEN_CPUFEATURE(NSCB,               11*32+ 6) /*A  Null Selector Clears Base */
 
 #endif /* XEN_CPUFEATURE */
 
Signature · 1 line
2.11.0
#4 lore raw

PATCH 2/2 x86/cpuid: Detect null segment behaviour on Zen2 CPUs

In reply to Jane Malalane

Zen2 CPUs actually have this behaviour, but the CPUID bit couldn't be introduced into Zen2 due to a lack of leaves. So, it was added in a new leaf in Zen3. Nonetheless, hypervisors can synthesize the CPUID bit in software.

So, on Zen2 hardware, Xen probes for NSCB (NullSelectorClearsBit) and synthesizes the bit.

---
CC: Wei Liu <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Pu Wen <[email protected]>
CC: Andy Lutomirski <[email protected]>

---
 xen/arch/x86/cpu/amd.c           | 18 ++++++++++++++++++
 xen/arch/x86/cpu/cpu.h           |  1 +
 xen/arch/x86/cpu/hygon.c         |  5 +++++
 xen/include/asm-x86/cpufeature.h |  1 +
 4 files changed, 25 insertions(+)
xen/arch/x86/cpu/amd.c +18 -0
index 2260eef3aa..654f82e2cb 100644
@@ -681,6 +681,19 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
 			  c->x86_capability);
 }
 
+void detect_zen2_null_seg_behaviour(void)
+{
+	uint64_t base;
+
+	wrmsrl(MSR_FS_BASE, 1);
+	asm volatile ( "mov %0, %%fs" :: "rm" (0) );
+	rdmsrl(MSR_FS_BASE, base);
+
+	if (base == 0)
+		setup_force_cpu_cap(X86_FEATURE_NSCB);
+
+}
+
 static void init_amd(struct cpuinfo_x86 *c)
 {
 	u32 l, h;
@@ -731,6 +744,11 @@ static void init_amd(struct cpuinfo_x86 *c)
 	else /* Implicily "== 0x10 || >= 0x12" by being 64bit. */
 		amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x17 && c->x86_model >= 30 && c->x86_model <= 0x5f)
+		detect_zen2_null_seg_behaviour();
+
 	/*
 	 * If the user has explicitly chosen to disable Memory Disambiguation
 	 * to mitigiate Speculative Store Bypass, poke the appropriate MSR.
xen/arch/x86/cpu/cpu.h +1 -0
index 1ac3b2867a..0dd1b762ff 100644
@@ -21,3 +21,4 @@ extern bool detect_extended_topology(struct cpuinfo_x86 *c);
 void early_init_amd(struct cpuinfo_x86 *c);
 void amd_log_freq(const struct cpuinfo_x86 *c);
 void amd_init_lfence(struct cpuinfo_x86 *c);
+void detect_zen2_null_seg_behaviour(void);
xen/arch/x86/cpu/hygon.c +5 -0
index 67e23c5df9..232edb0c4d 100644
@@ -34,6 +34,11 @@ static void init_hygon(struct cpuinfo_x86 *c)
 
 	amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x18 && c->x86_model >= 4)
+		detect_zen2_null_seg_behaviour();
+
 	/*
 	 * If the user has explicitly chosen to disable Memory Disambiguation
 	 * to mitigiate Speculative Store Bypass, poke the appropriate MSR.
xen/include/asm-x86/cpufeature.h +1 -0
index 5f6b83f71c..4faf9bff29 100644
@@ -146,6 +146,7 @@
 #define cpu_has_cpuid_faulting  boot_cpu_has(X86_FEATURE_CPUID_FAULTING)
 #define cpu_has_aperfmperf      boot_cpu_has(X86_FEATURE_APERFMPERF)
 #define cpu_has_lfence_dispatch boot_cpu_has(X86_FEATURE_LFENCE_DISPATCH)
+#define cpu_has_nscb            boot_cpu_has(X86_FEATURE_NSCB)
 #define cpu_has_xen_lbr         boot_cpu_has(X86_FEATURE_XEN_LBR)
 #define cpu_has_xen_shstk       boot_cpu_has(X86_FEATURE_XEN_SHSTK)
 
Signature · 1 line
2.11.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] x86/cpuid: Expose NullSelectorClearsBase CPUID bit to guests

In reply to Jane Malalane
Quoted 8 lines · Jane Malalane

AMD Zen3 adds the NullSelectorClearsBase bit to indicate that loading a NULL segment selector zeroes the base and limit fields, as well as just attributes.

Expose bit to all guests.

#5 lore raw

Re: [PATCH v1 2/2] x86/cpuid: Detect null segment behaviour on Zen2 CPUs

In reply to Jane Malalane

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 684

Quoted 7 lines · Jane Malalane
xen/arch/x86/cpu/amd.c +1 -0
@@ -681,6 +681,19 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
 			  c->x86_capability);
 }
 
+void detect_zen2_null_seg_behaviour(void)

This can in principle be marked __init.

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 689

Quoted 5 lines
xen/arch/x86/cpu/amd.c +5 -0
+{
+	uint64_t base;
+
+	wrmsrl(MSR_FS_BASE, 1);
+	asm volatile ( "mov %0, %%fs" :: "rm" (0) );

While I don't strictly mind the "m" part of the constraint to remain there (in the hope for compilers actually to support this), iirc it's not useful to have when the value is a constant: Last time I checked, the compiler would not instantiate an anonymous (stack) variable to fulfill this constraint (as can be seen when dropping the "r" part of the constraint).

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 *c) · line 749

Quoted 7 lines
xen/arch/x86/cpu/amd.c +3 -0
@@ -731,6 +744,11 @@ static void init_amd(struct cpuinfo_x86 *c)
 	else /* Implicily "== 0x10 || >= 0x12" by being 64bit. */
 		amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x17 && c->x86_model >= 30 && c->x86_model <= 0x5f)

DYM 0x30 here? Or 0x1e? In any event 0x5f should be accompanied by another hex constant. And it would also help if in the description you said where these bounds as well as ...

xen/arch/x86/cpu/hygon.c static void init_hygon(struct cpuinfo_x86 *c) · line 39

Quoted 9 lines
xen/arch/x86/cpu/hygon.c +3 -0
@@ -34,6 +34,11 @@ static void init_hygon(struct cpuinfo_x86 *c)
 
 	amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x18 && c->x86_model >= 4)

... this one come from.

Jan

#6 lore raw

Re: [PATCH v1 2/2] x86/cpuid: Detect null segment behaviour on Zen2 CPUs

Quoted 10 lines · Jan Beulich

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 684

Quoted 7 lines · Jane Malalane
xen/arch/x86/cpu/amd.c +1 -0
@@ -681,6 +681,19 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
 			  c->x86_capability);
 }
 
+void detect_zen2_null_seg_behaviour(void)

This can in principle be marked __init.

Quoted 11 lines

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 689

Quoted 5 lines
xen/arch/x86/cpu/amd.c +5 -0
+{
+	uint64_t base;
+
+	wrmsrl(MSR_FS_BASE, 1);
+	asm volatile ( "mov %0, %%fs" :: "rm" (0) );

While I don't strictly mind the "m" part of the constraint to remain there (in the hope for compilers actually to support this), iirc it's not useful to have when the value is a constant: Last time I checked, the compiler would not instantiate an anonymous (stack) variable to fulfill this constraint (as can be seen when dropping the "r" part of the constraint).

This is "rm" because it is what we use elsewhere in Xen for selectors, and because it is the correct constraints based on the legal instruction encodings.

If you want to work around what you perceive to be bugs in compilers then submit a independent change yourself.

Quoted 8 lines

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 *c) · line 749

Quoted 7 lines
xen/arch/x86/cpu/amd.c +3 -0
@@ -731,6 +744,11 @@ static void init_amd(struct cpuinfo_x86 *c)
 	else /* Implicily "== 0x10 || >= 0x12" by being 64bit. */
 		amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x17 && c->x86_model >= 30 && c->x86_model <= 0x5f)

DYM 0x30 here?

0x30, although it turns out that some of the mobile Zen2 CPUs exceed 0x60 in terms of model number.

As Zen3 changes the family number to 0x19, I'd just drop the upper bound.

Quoted 3 lines

Or 0x1e? In any event 0x5f should be accompanied by
another hex constant. And it would also help if in the description you said where these bounds

From talking to people at AMD.

Quoted 6 lines

as well as ...

Quoted 4 lines
xen/arch/x86/cpu/hygon.c +0 -0
@@ -34,6 +34,11 @@ static void init_hygon(struct cpuinfo_x86 *c)
 
Quoted 6 lines

xen/arch/x86/cpu/hygon.c static void init_hygon(struct cpuinfo_x86 *c) · line 39

Quoted 5 lines
xen/arch/x86/cpu/hygon.c +3 -0
 	amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x18 && c->x86_model >= 4)

... this one come from.

From talking to people at Hygon.

~Andrew

#3 lore raw

Re: [PATCH v1 1/2] x86/cpuid: Expose NullSelectorClearsBase CPUID bit to guests

In reply to Jane Malalane

xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(FSRCS, 10*32+12) /*A Fast Short REP CMPSB/SCASB */ · line 288

Quoted 9 lines · Jane Malalane
xen/include/public/arch-x86/cpufeatureset.h +1 -0
index 380b51b1b3..e5a7c94c78 100644
@@ -285,6 +285,7 @@ XEN_CPUFEATURE(FSRCS,        10*32+12) /*A  Fast Short REP CMPSB/SCASB */
 
 /* AMD-defined CPU features, CPUID level 0x80000021.eax, word 11 */
 XEN_CPUFEATURE(LFENCE_DISPATCH,    11*32+ 2) /*A  LFENCE always serializing */
+XEN_CPUFEATURE(NSCB,               11*32+ 6) /*A  Null Selector Clears Base */

On second thoughts, I'm tempted to add " (and limit too)" to the comment.

Can be fixed on commit.

~Andrew

#7 lore raw

Re: [PATCH v1 2/2] x86/cpuid: Detect null segment behaviour on Zen2 CPUs

In reply to Andrew Cooper
Quoted 23 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 684

Quoted 7 lines · Jane Malalane
xen/arch/x86/cpu/amd.c +1 -0
@@ -681,6 +681,19 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
 			  c->x86_capability);
 }
 
+void detect_zen2_null_seg_behaviour(void)

This can in principle be marked __init.

Quoted 11 lines

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 689

Quoted 5 lines
xen/arch/x86/cpu/amd.c +5 -0
+{
+	uint64_t base;
+
+	wrmsrl(MSR_FS_BASE, 1);
+	asm volatile ( "mov %0, %%fs" :: "rm" (0) );

While I don't strictly mind the "m" part of the constraint to remain there (in the hope for compilers actually to support this), iirc it's not useful to have when the value is a constant: Last time I checked, the compiler would not instantiate an anonymous (stack) variable to fulfill this constraint (as can be seen when dropping the "r" part of the constraint).

Quoted 3 lines

This is "rm" because it is what we use elsewhere in Xen for selectors, and because it is the correct constraints based on the legal instruction encodings.

grep-ing for "%%[defgs]s" reveals:

efi_arch_post_exit_boot(), svm_ctxt_switch_to(), and do_set_segment_base() all use just "r". This grep has not produced any use of "rm". What are you talking about?

Quoted 2 lines

If you want to work around what you perceive to be bugs in compilers then submit a independent change yourself.

I don't perceive this as a bug; perhaps a desirable feature. I also did start my response with "While I don't strictly mind the "m" part ..." - was this not careful enough to indicate I'm not going to insist on the change, but I'd prefer it to be made?

Quoted 9 lines
Quoted 8 lines

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 *c) · line 749

Quoted 7 lines
xen/arch/x86/cpu/amd.c +3 -0
@@ -731,6 +744,11 @@ static void init_amd(struct cpuinfo_x86 *c)
 	else /* Implicily "== 0x10 || >= 0x12" by being 64bit. */
 		amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x17 && c->x86_model >= 30 && c->x86_model <= 0x5f)

DYM 0x30 here?

Quoted 4 lines

0x30, although it turns out that some of the mobile Zen2 CPUs exceed 0x60 in terms of model number.

As Zen3 changes the family number to 0x19, I'd just drop the upper bound.

Minor note: Even if it didn't, the !cpu_has_nscb would also be enough to avoid the probing there.

Quoted 12 lines
Quoted 3 lines

Or 0x1e? In any event 0x5f should be accompanied by
another hex constant. And it would also help if in the description you said where these bounds

From talking to people at AMD.

Quoted 6 lines

as well as ...

Quoted 4 lines
xen/arch/x86/cpu/hygon.c +0 -0
@@ -34,6 +34,11 @@ static void init_hygon(struct cpuinfo_x86 *c)
 
Quoted 8 lines
Quoted 6 lines

xen/arch/x86/cpu/hygon.c static void init_hygon(struct cpuinfo_x86 *c) · line 39

Quoted 5 lines
xen/arch/x86/cpu/hygon.c +3 -0
 	amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x18 && c->x86_model >= 4)

... this one come from.

From talking to people at Hygon.

Fair enough, but imo this wants mentioning in the description.

Jan

#8 lore raw

Re: [PATCH v1 2/2] x86/cpuid: Detect null segment behaviour on Zen2 CPUs

Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 684

Quoted 7 lines · Jane Malalane
xen/arch/x86/cpu/amd.c +1 -0
@@ -681,6 +681,19 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
 			  c->x86_capability);
 }
 
+void detect_zen2_null_seg_behaviour(void)

This can in principle be marked __init.

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 689

Quoted 5 lines
xen/arch/x86/cpu/amd.c +5 -0
+{
+	uint64_t base;
+
+	wrmsrl(MSR_FS_BASE, 1);
+	asm volatile ( "mov %0, %%fs" :: "rm" (0) );

While I don't strictly mind the "m" part of the constraint to remain there (in the hope for compilers actually to support this), iirc it's not useful to have when the value is a constant: Last time I checked, the compiler would not instantiate an anonymous (stack) variable to fulfill this constraint (as can be seen when dropping the "r" part of the constraint).

Quoted 6 lines
Quoted 3 lines

This is "rm" because it is what we use elsewhere in Xen for selectors, and because it is the correct constraints based on the legal instruction encodings.

grep-ing for "%%[defgs]s" reveals:

efi_arch_post_exit_boot(), svm_ctxt_switch_to(),

These are writing multiple selectors in one go, and a register constraint is the only sane option.

Quoted 1 line

and do_set_segment_base() all use just "r".

I had missed this one.

Quoted 2 lines

This grep has not produced
any use of "rm". What are you talking about?

TRY_LOAD_SEG(), pv_emul_read_descriptor() for both lar and lsl, do_double_fault() for another lsl, lldt(), ltr().

So ok - not everything, but most.

Quoted 7 lines
Quoted 3 lines

If you want to work around what you perceive to be bugs in compilers then submit a independent change yourself.

I don't perceive this as a bug; perhaps a desirable feature. I also did start my response with "While I don't strictly mind the "m" part ..." - was this not careful enough to indicate I'm not going to insist on the change, but I'd prefer it to be made?

No, because a maintainer saying "I'd prefer this to be changed" is still an instruction to the submitter to make the change.

But the request is inappropriate.  "Last time I checked, the compiler would" presumably means you've checked GCC and not Clang, and therefore any conclusions about the behaviour are incomplete.

Unless there is a real concrete compiler bug to work around, "rm" is the appropriate constraint to use, all other things being equal.  If the complier is merely doing something dumb with the flexibility it has been permitted, then fix the compiler and the problem will resolve itself the proper way.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

xen/arch/x86/cpu/amd.c static void init_amd(struct cpuinfo_x86 *c) · line 749

Quoted 8 lines
xen/arch/x86/cpu/amd.c +3 -0
@@ -731,6 +744,11 @@ static void init_amd(struct cpuinfo_x86 *c)
 	else /* Implicily "== 0x10 || >= 0x12" by being 64bit. */
 		amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x17 && c->x86_model >= 30 && c->x86_model <= 0x5f)

DYM 0x30 here?

Quoted 6 lines
Quoted 4 lines

0x30, although it turns out that some of the mobile Zen2 CPUs exceed 0x60 in terms of model number.

As Zen3 changes the family number to 0x19, I'd just drop the upper bound.

Minor note: Even if it didn't, the !cpu_has_nscb would also be enough to avoid the probing there.

There is actually a problem.  From a non-AMD source, I've found the Sucor Z+ CPU which is a Fam17h Model 0x50 Zen1.

So instead I'm going to recommend dropping all model checks and just keeping the family checks.  This will extend the probe function to Zen1 too, but it is once on boot, trivial in terms of complexity, and really not worth the time/effort it has taken to discover that the model list wasn't correct to start with.

~Andrew

#9 lore raw

Re: [PATCH v1 2/2] x86/cpuid: Detect null segment behaviour on Zen2 CPUs

Quoted 36 lines · Andrew Cooper
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 684

Quoted 7 lines · Jane Malalane
xen/arch/x86/cpu/amd.c +1 -0
@@ -681,6 +681,19 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
 			  c->x86_capability);
 }
 
+void detect_zen2_null_seg_behaviour(void)

This can in principle be marked __init.

xen/arch/x86/cpu/amd.c void amd_init_lfence(struct cpuinfo_x86 *c) · line 689

Quoted 5 lines
xen/arch/x86/cpu/amd.c +5 -0
+{
+	uint64_t base;
+
+	wrmsrl(MSR_FS_BASE, 1);
+	asm volatile ( "mov %0, %%fs" :: "rm" (0) );

While I don't strictly mind the "m" part of the constraint to remain there (in the hope for compilers actually to support this), iirc it's not useful to have when the value is a constant: Last time I checked, the compiler would not instantiate an anonymous (stack) variable to fulfill this constraint (as can be seen when dropping the "r" part of the constraint).

Quoted 6 lines
Quoted 3 lines

This is "rm" because it is what we use elsewhere in Xen for selectors, and because it is the correct constraints based on the legal instruction encodings.

grep-ing for "%%[defgs]s" reveals:

efi_arch_post_exit_boot(), svm_ctxt_switch_to(),

These are writing multiple selectors in one go, and a register constraint is the only sane option.

Quoted 1 line

and do_set_segment_base() all use just "r".

Quoted 7 lines

I had missed this one.

Quoted 2 lines

This grep has not produced
any use of "rm". What are you talking about?

TRY_LOAD_SEG(), pv_emul_read_descriptor() for both lar and lsl, do_double_fault() for another lsl, lldt(), ltr().

TRY_LOAD_SEG() and pv_emul_read_descriptor() don't pass constants as asm() argument values. do_double_fault()'s use of lsl is indeed an example matching the pattern here. lldt() and ltr() are generic inline helpers, so validly allow for both because they should not make assumptions on what the caller passes. Plus "m" there is okay, because if the caller passes a constant there will be a named variable (the function parameter), i.e. the compiler does not need to instantiate any anonymous one.

Quoted 5 lines

So ok - not everything, but most.

Quoted 3 lines
Quoted 3 lines

If you want to work around what you perceive to be bugs in compilers then submit a independent change yourself.

Quoted 7 lines
Quoted 4 lines

I don't perceive this as a bug; perhaps a desirable feature. I also did start my response with "While I don't strictly mind the "m" part ..." - was this not careful enough to indicate I'm not going to insist on the change, but I'd prefer it to be made?

No, because a maintainer saying "I'd prefer this to be changed" is still an instruction to the submitter to make the change.

It was a request to _consider_ dropping the m part, yes. But (see below) now that you've forced me to re-check (I presume you didn't check yourself, or else I would expect you would have drawn the same conclusion as I did), I actually feel stronger about this needing adjustment.

Quoted 3 lines

But the request is inappropriate.  "Last time I checked, the compiler would" presumably means you've checked GCC and not Clang, and therefore any conclusions about the behaviour are incomplete.

Not really, no. IIRC I did check the version of clang that I have easy access to. (For gcc I've just now re-checked with 10.x and 11.x.)

Quoted 5 lines

Unless there is a real concrete compiler bug to work around, "rm" is the appropriate constraint to use, all other things being equal.  If the complier is merely doing something dumb with the flexibility it has been permitted, then fix the compiler and the problem will resolve itself the proper way.

I disagree. When an asm() constraint permits multiple kinds of values, dropping one or more of the alternatives should IMO still yield valid (perhaps sub-optimal) code (IOW every one of the supplied kinds should be valid). The issue here is that it is not spelled out clearly whether something like "m" (0) is actually legal. The error messages, however, suggest this is intended to be illegal: gcc says "memory input ... is not directly addressable", whereas clang says "invalid lvalue in asm input for constraint 'm'".

IOW I do think the one case of LSL in do_double_fault() does need adjusting.

Jan

#10 lore raw

PATCH v2 2/2 x86/cpuid: Detect null segment behaviour on Zen2 CPUs

In reply to Jane Malalane

Zen2 CPUs actually have this behaviour, but the CPUID bit couldn't be introduced into Zen2 due to a lack of leaves. So, it was added in a new leaf in Zen3. Nonetheless, hypervisors can synthesize the CPUID bit in software.

So, Xen probes for NSCB (NullSelectorClearsBit) and synthesizes the bit, if the behaviour is present.

---
CC: Wei Liu <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Pu Wen <[email protected]>
CC: Andy Lutomirski <[email protected]>

v2:
* Mark detect_zen2_null_seg_behaviour() with __init
* Remove model checks

---
 xen/arch/x86/cpu/amd.c           | 18 ++++++++++++++++++
 xen/arch/x86/cpu/cpu.h           |  1 +
 xen/arch/x86/cpu/hygon.c         |  5 +++++
 xen/include/asm-x86/cpufeature.h |  1 +
 4 files changed, 25 insertions(+)
xen/arch/x86/cpu/amd.c +18 -0
index 2260eef3aa..cb12861481 100644
@@ -681,6 +681,19 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
 			  c->x86_capability);
 }
 
+void __init detect_zen2_null_seg_behaviour(void)
+{
+	uint64_t base;
+
+	wrmsrl(MSR_FS_BASE, 1);
+	asm volatile ( "mov %0, %%fs" :: "rm" (0) );
+	rdmsrl(MSR_FS_BASE, base);
+
+	if (base == 0)
+		setup_force_cpu_cap(X86_FEATURE_NSCB);
+
+}
+
 static void init_amd(struct cpuinfo_x86 *c)
 {
 	u32 l, h;
@@ -731,6 +744,11 @@ static void init_amd(struct cpuinfo_x86 *c)
 	else /* Implicily "== 0x10 || >= 0x12" by being 64bit. */
 		amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x17)
+		detect_zen2_null_seg_behaviour();
+
 	/*
 	 * If the user has explicitly chosen to disable Memory Disambiguation
 	 * to mitigiate Speculative Store Bypass, poke the appropriate MSR.
xen/arch/x86/cpu/cpu.h +1 -0
index 1ac3b2867a..0dd1b762ff 100644
@@ -21,3 +21,4 @@ extern bool detect_extended_topology(struct cpuinfo_x86 *c);
 void early_init_amd(struct cpuinfo_x86 *c);
 void amd_log_freq(const struct cpuinfo_x86 *c);
 void amd_init_lfence(struct cpuinfo_x86 *c);
+void detect_zen2_null_seg_behaviour(void);
xen/arch/x86/cpu/hygon.c +5 -0
index 67e23c5df9..d7a04af2bb 100644
@@ -34,6 +34,11 @@ static void init_hygon(struct cpuinfo_x86 *c)
 
 	amd_init_lfence(c);
 
+	/* Probe for NSCB on Zen2 CPUs when not virtualised */
+	if (!cpu_has_hypervisor && !cpu_has_nscb && c == &boot_cpu_data &&
+	    c->x86 == 0x18)
+		detect_zen2_null_seg_behaviour();
+
 	/*
 	 * If the user has explicitly chosen to disable Memory Disambiguation
 	 * to mitigiate Speculative Store Bypass, poke the appropriate MSR.
xen/include/asm-x86/cpufeature.h +1 -0
index 5f6b83f71c..4faf9bff29 100644
@@ -146,6 +146,7 @@
 #define cpu_has_cpuid_faulting  boot_cpu_has(X86_FEATURE_CPUID_FAULTING)
 #define cpu_has_aperfmperf      boot_cpu_has(X86_FEATURE_APERFMPERF)
 #define cpu_has_lfence_dispatch boot_cpu_has(X86_FEATURE_LFENCE_DISPATCH)
+#define cpu_has_nscb            boot_cpu_has(X86_FEATURE_NSCB)
 #define cpu_has_xen_lbr         boot_cpu_has(X86_FEATURE_XEN_LBR)
 #define cpu_has_xen_shstk       boot_cpu_has(X86_FEATURE_XEN_SHSTK)
 
Signature · 1 line
2.11.0
Reviewed-by on v2 2/2
#11 lore raw

Re: [PATCH v2 2/2] x86/cpuid: Detect null segment behaviour on Zen2 CPUs

Quoted 4 lines · Jane Malalane

Zen2 CPUs actually have this behaviour, but the CPUID bit couldn't be introduced into Zen2 due to a lack of leaves. So, it was added in a new leaf in Zen3. Nonetheless, hypervisors can synthesize the CPUID bit in software.

Considering the prior model checks, I understand this isn't all Zen2s? No matter what the answer, I'd like to ask that the first sentence start with either "All" or "Some" (or something along these lines). Which is of course fine to insert while committing, so not need to send a v3.

Quoted 5 lines

So, Xen probes for NSCB (NullSelectorClearsBit) and synthesizes the bit, if the behaviour is present.

#12 lore raw

Re: [PATCH v2 2/2] x86/cpuid: Detect null segment behaviour on Zen2 CPUs

Quoted 10 lines · Jan Beulich
Quoted 4 lines · Jane Malalane

Zen2 CPUs actually have this behaviour, but the CPUID bit couldn't be introduced into Zen2 due to a lack of leaves. So, it was added in a new leaf in Zen3. Nonetheless, hypervisors can synthesize the CPUID bit in software.

Considering the prior model checks, I understand this isn't all Zen2s? No matter what the answer, I'd like to ask that the first sentence start with either "All" or "Some" (or something along these lines). Which is of course fine to insert while committing, so not need to send a v3.

All Zen2's have this behaviour.

The model checks were trying to avoid running the probe on Zen1, but "model >= 0x30 && mode != 0x50" is error prone.

There are no faults in the probe, and its fast, so running on all Zen1/2 is better than hoping that we got the model list correct.

~Andrew