Skip to contentSkip to conversation

amd/msr: implement MSR_VIRT_SPEC_CTRL for HVM guests

PATCH Superseded Roger Pau Monne 3 participants 16 messages Feb 1, 2022 – Mar 14, 2022

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover amd/msr: implement MSR_VIRT_SPEC_CTRL for HVM guests

Hello,

The following series implements support for MSR_VIRT_SPEC_CTRL on different AMD CPU families.

Note that the support is added backwards, starting with the newer CPUs that support MSR_SPEC_CTRL and moving to the older ones either using MSR_VIRT_SPEC_CTRL or the SSBD bit in LS_CFG.

First patch is quite clean, as it uses the shadow SPEC_CTRL in order to set the SSBD bit and have it context switched by Xen using the existing logic recently added.

The next two patches introduce a different way to context switch SSBD either depending on the underlying SSBD support, so it's either using VIRT_SPEC_CTRL or the LS_CFG MSR. They also kind of overload the usage of several spec_ctrl variables in the hypervisor in order to store the status of SSBD even when not using MSR_SPEC_CTRL itself. I've tried to document those in the commit messages, but it could be controversial.

Thanks, Roger.

Roger Pau Monne (3):
amd/msr: implement VIRT_SPEC_CTRL for HVM guests on top of SPEC_CTRL amd/msr: allow passthrough of VIRT_SPEC_CTRL for HVM guests amd/msr: implement VIRT_SPEC_CTRL for HVM guests using legacy SSBD

 docs/misc/xen-command-line.pandoc           |   5 +-
 xen/arch/x86/cpu/amd.c                      | 116 +++++++++++++++++---
 xen/arch/x86/cpuid.c                        |  18 +++
 xen/arch/x86/hvm/hvm.c                      |   1 +
 xen/arch/x86/hvm/svm/entry.S                |   8 +-
 xen/arch/x86/hvm/svm/svm.c                  |  67 +++++++++++
 xen/arch/x86/include/asm/amd.h              |   3 +
 xen/arch/x86/include/asm/cpufeatures.h      |   2 +
 xen/arch/x86/include/asm/msr.h              |   6 +-
 xen/arch/x86/msr.c                          |  15 +++
 xen/arch/x86/spec_ctrl.c                    |  11 +-
 xen/include/public/arch-x86/cpufeatureset.h |   2 +-
 12 files changed, 230 insertions(+), 24 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 amd/msr: implement VIRT_SPEC_CTRL for HVM guests on top of SPEC_CTRL

Use the logic to set shadow SPEC_CTRL values in order to implement support for VIRT_SPEC_CTRL (signaled by VIRT_SSBD CPUID flag) for HVM guests. This includes using the spec_ctrl vCPU MSR variable to store the guest set value of VIRT_SPEC_CTRL.SSBD.

Note that VIRT_SSBD is only set in the HVM max CPUID policy, as the default should be to expose SPEC_CTRL only and support VIRT_SPEC_CTRL for migration compatibility.

---
 docs/misc/xen-command-line.pandoc           |  5 +++--
 xen/arch/x86/cpuid.c                        |  7 +++++++
 xen/arch/x86/hvm/hvm.c                      |  1 +
 xen/arch/x86/include/asm/msr.h              |  6 +++++-
 xen/arch/x86/msr.c                          | 15 +++++++++++++++
 xen/arch/x86/spec_ctrl.c                    |  3 ++-
 xen/include/public/arch-x86/cpufeatureset.h |  2 +-
 7 files changed, 34 insertions(+), 5 deletions(-)
docs/misc/xen-command-line.pandoc +3 -2
index 6b3da6ddc1..081e10f80b 100644
@@ -2273,8 +2273,9 @@ to use.
 * `pv=` and `hvm=` offer control over all suboptions for PV and HVM guests
   respectively.
 * `msr-sc=` offers control over Xen's support for manipulating `MSR_SPEC_CTRL`
-  on entry and exit.  These blocks are necessary to virtualise support for
-  guests and if disabled, guests will be unable to use IBRS/STIBP/SSBD/etc.
+  and/or `MSR_VIRT_SPEC_CTRL` on entry and exit.  These blocks are necessary to
+  virtualise support for guests and if disabled, guests will be unable to use
+  IBRS/STIBP/SSBD/etc.
 * `rsb=` offers control over whether to overwrite the Return Stack Buffer /
   Return Address Stack on entry to Xen.
 * `md-clear=` offers control over whether to use VERW to flush
xen/arch/x86/cpuid.c +7 -0
index e24dd283e7..29b4cfc9e6 100644
@@ -543,6 +543,13 @@ static void __init calculate_hvm_max_policy(void)
         __clear_bit(X86_FEATURE_IBRSB, hvm_featureset);
         __clear_bit(X86_FEATURE_IBRS, hvm_featureset);
     }
+    else
+        /*
+         * If SPEC_CTRL is available VIRT_SPEC_CTRL can also be implemented as
+         * it's a subset of the controls exposed in SPEC_CTRL (SSBD only).
+         * Expose in the max policy for compatibility migration.
+         */
+        __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);
 
     /*
      * With VT-x, some features are only supported by Xen if dedicated
xen/arch/x86/hvm/hvm.c +1 -0
index c4ddb8607d..3400c9299c 100644
@@ -1332,6 +1332,7 @@ static const uint32_t msrs_to_send[] = {
     MSR_INTEL_MISC_FEATURES_ENABLES,
     MSR_IA32_BNDCFGS,
     MSR_IA32_XSS,
+    MSR_VIRT_SPEC_CTRL,
     MSR_AMD64_DR0_ADDRESS_MASK,
     MSR_AMD64_DR1_ADDRESS_MASK,
     MSR_AMD64_DR2_ADDRESS_MASK,
xen/arch/x86/include/asm/msr.h +5 -1
index ce4fe51afe..98f6b79e09 100644
@@ -291,6 +291,7 @@ struct vcpu_msrs
 {
     /*
      * 0x00000048 - MSR_SPEC_CTRL
+     * 0xc001011f - MSR_VIRT_SPEC_CTRL
      *
      * For PV guests, this holds the guest kernel value.  It is accessed on
      * every entry/exit path.
@@ -301,7 +302,10 @@ struct vcpu_msrs
      * For SVM, the guest value lives in the VMCB, and hardware saves/restores
      * the host value automatically.  However, guests run with the OR of the
      * host and guest value, which allows Xen to set protections behind the
-     * guest's back.
+     * guest's back.  Use such functionality in order to implement support for
+     * VIRT_SPEC_CTRL as a shadow value of SPEC_CTRL and thus store the value
+     * of VIRT_SPEC_CTRL in this field, taking advantage of both MSRs having
+     * compatible layouts.
      *
      * We must clear/restore Xen's value before/after VMRUN to avoid unduly
      * influencing the guest.  In order to support "behind the guest's back"
xen/arch/x86/msr.c +15 -0
index 4ac5b5a048..aa74cfde6c 100644
@@ -381,6 +381,13 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
                ? K8_HWCR_TSC_FREQ_SEL : 0;
         break;
 
+    case MSR_VIRT_SPEC_CTRL:
+        if ( !cp->extd.virt_ssbd )
+            goto gp_fault;
+
+        *val = msrs->spec_ctrl.raw & SPEC_CTRL_SSBD;
+        break;
+
     case MSR_AMD64_DE_CFG:
         if ( !(cp->x86_vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
             goto gp_fault;
@@ -666,6 +673,14 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
             wrmsr_tsc_aux(val);
         break;
 
+    case MSR_VIRT_SPEC_CTRL:
+        if ( !cp->extd.virt_ssbd )
+            goto gp_fault;
+
+        /* Only supports SSBD bit, the rest are ignored. */
+        msrs->spec_ctrl.raw = val & SPEC_CTRL_SSBD;
+        break;
+
     case MSR_AMD64_DE_CFG:
         /*
          * OpenBSD 6.7 will panic if writing to DE_CFG triggers a #GP:
xen/arch/x86/spec_ctrl.c +2 -1
index ee862089b7..64b154b2d3 100644
@@ -395,12 +395,13 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps)
      * mitigation support for guests.
      */
 #ifdef CONFIG_HVM
-    printk("  Support for HVM VMs:%s%s%s%s%s\n",
+    printk("  Support for HVM VMs:%s%s%s%s%s%s\n",
            (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ||
             boot_cpu_has(X86_FEATURE_SC_RSB_HVM) ||
             boot_cpu_has(X86_FEATURE_MD_CLEAR)   ||
             opt_eager_fpu)                           ? ""               : " None",
            boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_SPEC_CTRL" : "",
+           boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_VIRT_SPEC_CTRL" : "",
            boot_cpu_has(X86_FEATURE_SC_RSB_HVM)      ? " RSB"           : "",
            opt_eager_fpu                             ? " EAGER_FPU"     : "",
            boot_cpu_has(X86_FEATURE_MD_CLEAR)        ? " MD_CLEAR"      : "");
xen/include/public/arch-x86/cpufeatureset.h +1 -1
index 957df23b65..b9ab878ec1 100644
@@ -265,7 +265,7 @@ XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S  IBRS provides same-mode protection
 XEN_CPUFEATURE(NO_LMSL,       8*32+20) /*S  EFER.LMSLE no longer supported. */
 XEN_CPUFEATURE(AMD_PPIN,      8*32+23) /*   Protected Processor Inventory Number */
 XEN_CPUFEATURE(AMD_SSBD,      8*32+24) /*S  MSR_SPEC_CTRL.SSBD available */
-XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*   MSR_VIRT_SPEC_CTRL.SSBD */
+XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*!s MSR_VIRT_SPEC_CTRL.SSBD */
 XEN_CPUFEATURE(SSB_NO,        8*32+26) /*A  Hardware not vulnerable to SSB */
 XEN_CPUFEATURE(PSFD,          8*32+28) /*S  MSR_SPEC_CTRL.PSFD */
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests on top of SPEC_CTRL

Quoted 4 lines · Roger Pau Monne

Use the logic to set shadow SPEC_CTRL values in order to implement support for VIRT_SPEC_CTRL (signaled by VIRT_SSBD CPUID flag) for HVM guests. This includes using the spec_ctrl vCPU MSR variable to store the guest set value of VIRT_SPEC_CTRL.SSBD.

This leverages the guest running on the OR of host and guest values, aiui. If so, this could do with spelling out.

Quoted 3 lines

Note that VIRT_SSBD is only set in the HVM max CPUID policy, as the default should be to expose SPEC_CTRL only and support VIRT_SPEC_CTRL for migration compatibility.

I'm afraid I don't understand this last statement: How would this be about migration compatibility? No guest so far can use VIRT_SPEC_CTRL, and a future guest using it is unlikely to be able to cope with the MSR "disappearing" during migration.

docs/misc/xen-command-line.pandoc to use. · line 2276

Quoted 9 lines
docs/misc/xen-command-line.pandoc +1 -2
@@ -2273,8 +2273,9 @@ to use.
 * `pv=` and `hvm=` offer control over all suboptions for PV and HVM guests
   respectively.
 * `msr-sc=` offers control over Xen's support for manipulating `MSR_SPEC_CTRL`
-  on entry and exit.  These blocks are necessary to virtualise support for
-  guests and if disabled, guests will be unable to use IBRS/STIBP/SSBD/etc.
+  and/or `MSR_VIRT_SPEC_CTRL` on entry and exit.  These blocks are necessary to

Why would Xen be manipulating an MSR it only brings into existence for its guests?

xen/arch/x86/cpuid.c static void __init calculate_hvm_max_policy(void) · line 552

Quoted 13 lines
xen/arch/x86/cpuid.c +7 -0
@@ -543,6 +543,13 @@ static void __init calculate_hvm_max_policy(void)
         __clear_bit(X86_FEATURE_IBRSB, hvm_featureset);
         __clear_bit(X86_FEATURE_IBRS, hvm_featureset);
     }
+    else
+        /*
+         * If SPEC_CTRL is available VIRT_SPEC_CTRL can also be implemented as
+         * it's a subset of the controls exposed in SPEC_CTRL (SSBD only).
+         * Expose in the max policy for compatibility migration.
+         */
+        __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);

This means even Intel guests can use the feature then? I thought it was meanwhile deemed bad to offer such cross-vendor features?

Additionally, is SPEC_CTRL (i.e. IBRS) availability enough? Don't you need AMD_SSBD as a prereq (which may want expressing in gen-cpuid.py)?

Quoted 11 lines
xen/arch/x86/include/asm/msr.h +1 -0
@@ -291,6 +291,7 @@ struct vcpu_msrs
 {
     /*
      * 0x00000048 - MSR_SPEC_CTRL
+     * 0xc001011f - MSR_VIRT_SPEC_CTRL
      *
      * For PV guests, this holds the guest kernel value.  It is accessed on
      * every entry/exit path.

xen/arch/x86/include/asm/msr.h struct vcpu_msrs · line 308

Quoted 8 lines
xen/arch/x86/include/asm/msr.h +4 -1
@@ -301,7 +302,10 @@ struct vcpu_msrs
      * For SVM, the guest value lives in the VMCB, and hardware saves/restores
      * the host value automatically.  However, guests run with the OR of the
      * host and guest value, which allows Xen to set protections behind the
-     * guest's back.
+     * guest's back.  Use such functionality in order to implement support for
+     * VIRT_SPEC_CTRL as a shadow value of SPEC_CTRL and thus store the value
+     * of VIRT_SPEC_CTRL in this field, taking advantage of both MSRs having
+     * compatible layouts.

I guess "shadow value" means more like an alternative value, but (see above) this is about setting for now just one bit behind the guest's back.

Quoted 7 lines
xen/arch/x86/spec_ctrl.c +0 -1
@@ -395,12 +395,13 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps)
      * mitigation support for guests.
      */
 #ifdef CONFIG_HVM
-    printk("  Support for HVM VMs:%s%s%s%s%s\n",

xen/arch/x86/spec_ctrl.c static void __init print_details(enum ind_thunk thunk, uint64_t caps) · line 407

Quoted 10 lines
xen/arch/x86/spec_ctrl.c +2 -0
+    printk("  Support for HVM VMs:%s%s%s%s%s%s\n",
            (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ||
             boot_cpu_has(X86_FEATURE_SC_RSB_HVM) ||
             boot_cpu_has(X86_FEATURE_MD_CLEAR)   ||
             opt_eager_fpu)                           ? ""               : " None",
            boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_SPEC_CTRL" : "",
+           boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_VIRT_SPEC_CTRL" : "",
            boot_cpu_has(X86_FEATURE_SC_RSB_HVM)      ? " RSB"           : "",
            opt_eager_fpu                             ? " EAGER_FPU"     : "",
            boot_cpu_has(X86_FEATURE_MD_CLEAR)        ? " MD_CLEAR"      : "");

The output getting longish, can the two SC_MSR_HVM dependent items perhaps be folded, e.g. by making it "MSR_{,VIRT_}SPEC_CTRL"?

xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S IBRS provides same-mode protection · line 268

Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -265,7 +265,7 @@ XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S  IBRS provides same-mode protection
 XEN_CPUFEATURE(NO_LMSL,       8*32+20) /*S  EFER.LMSLE no longer supported. */
 XEN_CPUFEATURE(AMD_PPIN,      8*32+23) /*   Protected Processor Inventory Number */
 XEN_CPUFEATURE(AMD_SSBD,      8*32+24) /*S  MSR_SPEC_CTRL.SSBD available */
-XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*   MSR_VIRT_SPEC_CTRL.SSBD */
+XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*!s MSR_VIRT_SPEC_CTRL.SSBD */

What is the ! intended to cover here? From guest perspective the MSR acts entirely normally afaict.

Jan

#3 lore raw

Re: [PATCH 1/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests on top of SPEC_CTRL

Quoted 9 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

Use the logic to set shadow SPEC_CTRL values in order to implement support for VIRT_SPEC_CTRL (signaled by VIRT_SSBD CPUID flag) for HVM guests. This includes using the spec_ctrl vCPU MSR variable to store the guest set value of VIRT_SPEC_CTRL.SSBD.

This leverages the guest running on the OR of host and guest values, aiui. If so, this could do with spelling out.

Quoted 8 lines
Quoted 3 lines

Note that VIRT_SSBD is only set in the HVM max CPUID policy, as the default should be to expose SPEC_CTRL only and support VIRT_SPEC_CTRL for migration compatibility.

I'm afraid I don't understand this last statement: How would this be about migration compatibility? No guest so far can use VIRT_SPEC_CTRL, and a future guest using it is unlikely to be able to cope with the MSR "disappearing" during migration.

Maybe I didn't express this correctly. What I meant to explain is that on hardware having SPEC_CTRL VIRT_SPEC_CTRL will not be offered by default to guests. VIRT_SPEC_CTRL will only be part of the max CPUID policy so it can be enabled for compatibility purposes. Does this make sense?

Quoted 4 lines
Quoted 4 lines
docs/misc/xen-command-line.pandoc +0 -0
@@ -2273,8 +2273,9 @@ to use.
 * `pv=` and `hvm=` offer control over all suboptions for PV and HVM guests
Quoted 8 lines

docs/misc/xen-command-line.pandoc to use. · line 2276

Quoted 5 lines
docs/misc/xen-command-line.pandoc +1 -2
   respectively.
 * `msr-sc=` offers control over Xen's support for manipulating `MSR_SPEC_CTRL`
-  on entry and exit.  These blocks are necessary to virtualise support for
-  guests and if disabled, guests will be unable to use IBRS/STIBP/SSBD/etc.
+  and/or `MSR_VIRT_SPEC_CTRL` on entry and exit.  These blocks are necessary to

Why would Xen be manipulating an MSR it only brings into existence for its guests?

Well, that's not exactly true. Xen does use VIRT_SPEC_CTRL (see amd_init_ssbd).

I'm unsure how to express support for VIRT_SPEC_CTRL, as it does rely on SPEC_CTRL when available.

Quoted 8 lines
Quoted 8 lines
xen/arch/x86/cpuid.c +2 -0
@@ -543,6 +543,13 @@ static void __init calculate_hvm_max_policy(void)
         __clear_bit(X86_FEATURE_IBRSB, hvm_featureset);
         __clear_bit(X86_FEATURE_IBRS, hvm_featureset);
     }
+    else
+        /*
Quoted 8 lines

xen/arch/x86/cpuid.c static void __init calculate_hvm_max_policy(void) · line 552

Quoted 5 lines
xen/arch/x86/cpuid.c +5 -0
+         * If SPEC_CTRL is available VIRT_SPEC_CTRL can also be implemented as
+         * it's a subset of the controls exposed in SPEC_CTRL (SSBD only).
+         * Expose in the max policy for compatibility migration.
+         */
+        __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);

This means even Intel guests can use the feature then? I thought it was meanwhile deemed bad to offer such cross-vendor features?

No, we shouldn't expose to Intel.

Quoted 2 lines

Additionally, is SPEC_CTRL (i.e. IBRS) availability enough? Don't you need AMD_SSBD as a prereq (which may want expressing in gen-cpuid.py)?

We need AMD_SSBD if we implement VIRT_SPEC_CTRL on top of SPEC_CTRL, but we could also implement it on top of VIRT_SPEC_CTRL (if Xen runs virtualized) or even using the legacy SSBD setting mechanisms found in amd_init_ssbd, so I don't think VIRT_SSBD should explicitly depend on AMD_SSBD in gen-cpuid.py.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/include/asm/msr.h +1 -1
@@ -291,6 +291,7 @@ struct vcpu_msrs
 {
     /*
      * 0x00000048 - MSR_SPEC_CTRL
+     * 0xc001011f - MSR_VIRT_SPEC_CTRL
      *
      * For PV guests, this holds the guest kernel value.  It is accessed on
      * every entry/exit path.
@@ -301,7 +302,10 @@ struct vcpu_msrs
      * For SVM, the guest value lives in the VMCB, and hardware saves/restores
      * the host value automatically.  However, guests run with the OR of the
      * host and guest value, which allows Xen to set protections behind the
-     * guest's back.
Quoted 8 lines

xen/arch/x86/include/asm/msr.h struct vcpu_msrs · line 308

Quoted 4 lines
xen/arch/x86/include/asm/msr.h +4 -0
+     * guest's back.  Use such functionality in order to implement support for
+     * VIRT_SPEC_CTRL as a shadow value of SPEC_CTRL and thus store the value
+     * of VIRT_SPEC_CTRL in this field, taking advantage of both MSRs having
+     * compatible layouts.

I guess "shadow value" means more like an alternative value, but (see above) this is about setting for now just one bit behind the guest's back.

Well, the guest sets the bit in VIRT_SPEC_CTRL and Xen sets it on SPEC_CTRL in order for it to have effect. I can use 'alternative value' if that's clearer.

Quoted 12 lines
Quoted 12 lines
xen/arch/x86/spec_ctrl.c +1 -1
@@ -395,12 +395,13 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps)
      * mitigation support for guests.
      */
 #ifdef CONFIG_HVM
-    printk("  Support for HVM VMs:%s%s%s%s%s\n",
+    printk("  Support for HVM VMs:%s%s%s%s%s%s\n",
            (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ||
             boot_cpu_has(X86_FEATURE_SC_RSB_HVM) ||
             boot_cpu_has(X86_FEATURE_MD_CLEAR)   ||
             opt_eager_fpu)                           ? ""               : " None",
Quoted 8 lines

xen/arch/x86/spec_ctrl.c static void __init print_details(enum ind_thunk thunk, uint64_t caps) · line 407

Quoted 5 lines
xen/arch/x86/spec_ctrl.c +1 -0
            boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_SPEC_CTRL" : "",
+           boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_VIRT_SPEC_CTRL" : "",
            boot_cpu_has(X86_FEATURE_SC_RSB_HVM)      ? " RSB"           : "",
            opt_eager_fpu                             ? " EAGER_FPU"     : "",
            boot_cpu_has(X86_FEATURE_MD_CLEAR)        ? " MD_CLEAR"      : "");

The output getting longish, can the two SC_MSR_HVM dependent items perhaps be folded, e.g. by making it "MSR_{,VIRT_}SPEC_CTRL"?

OK, but further patches will add MSR_VIRT_SPEC_CTRL to hardware that doesn't expose MSR_SPEC_CTRL to guests, at which point it could be confusing?

Quoted 11 lines

xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S IBRS provides same-mode protection · line 268

Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -265,7 +265,7 @@ XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S  IBRS provides same-mode protection
 XEN_CPUFEATURE(NO_LMSL,       8*32+20) /*S  EFER.LMSLE no longer supported. */
 XEN_CPUFEATURE(AMD_PPIN,      8*32+23) /*   Protected Processor Inventory Number */
 XEN_CPUFEATURE(AMD_SSBD,      8*32+24) /*S  MSR_SPEC_CTRL.SSBD available */
-XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*   MSR_VIRT_SPEC_CTRL.SSBD */
+XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*!s MSR_VIRT_SPEC_CTRL.SSBD */

What is the ! intended to cover here? From guest perspective the MSR acts entirely normally afaict.

I've used the ! to note that VIRT_SSBD might be exposed on hardware whether it's not available as part of the host featureset. It did seem to me that using just 's' didn't reflect this properly.

According to my reading of the comment at the top '!' is not used to signal that the feature might act differently, but just that it's presence cannot be properly expressed with just the A, S, H flags, which would be the case for VIRT_SSBD I think.

Thanks, Roger.

#4 lore raw

Re: [PATCH 1/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests on top of SPEC_CTRL

Quoted 19 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

Use the logic to set shadow SPEC_CTRL values in order to implement support for VIRT_SPEC_CTRL (signaled by VIRT_SSBD CPUID flag) for HVM guests. This includes using the spec_ctrl vCPU MSR variable to store the guest set value of VIRT_SPEC_CTRL.SSBD.

This leverages the guest running on the OR of host and guest values, aiui. If so, this could do with spelling out.

Quoted 8 lines
Quoted 3 lines

Note that VIRT_SSBD is only set in the HVM max CPUID policy, as the default should be to expose SPEC_CTRL only and support VIRT_SPEC_CTRL for migration compatibility.

I'm afraid I don't understand this last statement: How would this be about migration compatibility? No guest so far can use VIRT_SPEC_CTRL, and a future guest using it is unlikely to be able to cope with the MSR "disappearing" during migration.

Quoted 5 lines

Maybe I didn't express this correctly. What I meant to explain is that on hardware having SPEC_CTRL VIRT_SPEC_CTRL will not be offered by default to guests. VIRT_SPEC_CTRL will only be part of the max CPUID policy so it can be enabled for compatibility purposes. Does this make sense?

Yes. Can you re-word along these lines?

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
docs/misc/xen-command-line.pandoc +0 -0
@@ -2273,8 +2273,9 @@ to use.
 * `pv=` and `hvm=` offer control over all suboptions for PV and HVM guests
Quoted 8 lines

docs/misc/xen-command-line.pandoc to use. · line 2276

Quoted 5 lines
docs/misc/xen-command-line.pandoc +1 -2
   respectively.
 * `msr-sc=` offers control over Xen's support for manipulating `MSR_SPEC_CTRL`
-  on entry and exit.  These blocks are necessary to virtualise support for
-  guests and if disabled, guests will be unable to use IBRS/STIBP/SSBD/etc.
+  and/or `MSR_VIRT_SPEC_CTRL` on entry and exit.  These blocks are necessary to

Why would Xen be manipulating an MSR it only brings into existence for its guests?

Quoted 5 lines

Well, that's not exactly true. Xen does use VIRT_SPEC_CTRL (see amd_init_ssbd).

I'm unsure how to express support for VIRT_SPEC_CTRL, as it does rely on SPEC_CTRL when available.

I wonder whether the command line doc needs to go into this level of detail.

Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/cpuid.c +2 -0
@@ -543,6 +543,13 @@ static void __init calculate_hvm_max_policy(void)
         __clear_bit(X86_FEATURE_IBRSB, hvm_featureset);
         __clear_bit(X86_FEATURE_IBRS, hvm_featureset);
     }
+    else
+        /*
Quoted 8 lines

xen/arch/x86/cpuid.c static void __init calculate_hvm_max_policy(void) · line 552

Quoted 5 lines
xen/arch/x86/cpuid.c +5 -0
+         * If SPEC_CTRL is available VIRT_SPEC_CTRL can also be implemented as
+         * it's a subset of the controls exposed in SPEC_CTRL (SSBD only).
+         * Expose in the max policy for compatibility migration.
+         */
+        __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);

This means even Intel guests can use the feature then? I thought it was meanwhile deemed bad to offer such cross-vendor features?

No, we shouldn't expose to Intel.

Quoted 8 lines
Quoted 2 lines

Additionally, is SPEC_CTRL (i.e. IBRS) availability enough? Don't you need AMD_SSBD as a prereq (which may want expressing in gen-cpuid.py)?

We need AMD_SSBD if we implement VIRT_SPEC_CTRL on top of SPEC_CTRL, but we could also implement it on top of VIRT_SPEC_CTRL (if Xen runs virtualized) or even using the legacy SSBD setting mechanisms found in amd_init_ssbd, so I don't think VIRT_SSBD should explicitly depend on AMD_SSBD in gen-cpuid.py.

Hmm, yes, good point. But when the prereqs cannot be expressed in gen-cpuid.py, I guess they need to be encoded here.

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/include/asm/msr.h +1 -1
@@ -291,6 +291,7 @@ struct vcpu_msrs
 {
     /*
      * 0x00000048 - MSR_SPEC_CTRL
+     * 0xc001011f - MSR_VIRT_SPEC_CTRL
      *
      * For PV guests, this holds the guest kernel value.  It is accessed on
      * every entry/exit path.
@@ -301,7 +302,10 @@ struct vcpu_msrs
      * For SVM, the guest value lives in the VMCB, and hardware saves/restores
      * the host value automatically.  However, guests run with the OR of the
      * host and guest value, which allows Xen to set protections behind the
-     * guest's back.
Quoted 8 lines

xen/arch/x86/include/asm/msr.h struct vcpu_msrs · line 308

Quoted 4 lines
xen/arch/x86/include/asm/msr.h +4 -0
+     * guest's back.  Use such functionality in order to implement support for
+     * VIRT_SPEC_CTRL as a shadow value of SPEC_CTRL and thus store the value
+     * of VIRT_SPEC_CTRL in this field, taking advantage of both MSRs having
+     * compatible layouts.

I guess "shadow value" means more like an alternative value, but (see above) this is about setting for now just one bit behind the guest's back.

Quoted 3 lines

Well, the guest sets the bit in VIRT_SPEC_CTRL and Xen sets it on SPEC_CTRL in order for it to have effect. I can use 'alternative value' if that's clearer.

Well, as I tried to express in my earlier reply, I view "shadow value" to mean "alternative value", so replacing wouldn't help. The question whether it acts like the shadow values we know elsewhere (VMX'es CR0 and CR4, for example). If it does, using the same term is of course fine. But it didn't look to me as if it would, hence I'd prefer to avoid ambiguity. But please realize that I may have misunderstood things ...

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/x86/spec_ctrl.c +1 -1
@@ -395,12 +395,13 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps)
      * mitigation support for guests.
      */
 #ifdef CONFIG_HVM
-    printk("  Support for HVM VMs:%s%s%s%s%s\n",
+    printk("  Support for HVM VMs:%s%s%s%s%s%s\n",
            (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ||
             boot_cpu_has(X86_FEATURE_SC_RSB_HVM) ||
             boot_cpu_has(X86_FEATURE_MD_CLEAR)   ||
             opt_eager_fpu)                           ? ""               : " None",
Quoted 8 lines

xen/arch/x86/spec_ctrl.c static void __init print_details(enum ind_thunk thunk, uint64_t caps) · line 407

Quoted 5 lines
xen/arch/x86/spec_ctrl.c +1 -0
            boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_SPEC_CTRL" : "",
+           boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_VIRT_SPEC_CTRL" : "",
            boot_cpu_has(X86_FEATURE_SC_RSB_HVM)      ? " RSB"           : "",
            opt_eager_fpu                             ? " EAGER_FPU"     : "",
            boot_cpu_has(X86_FEATURE_MD_CLEAR)        ? " MD_CLEAR"      : "");

The output getting longish, can the two SC_MSR_HVM dependent items perhaps be folded, e.g. by making it "MSR_{,VIRT_}SPEC_CTRL"?

Quoted 3 lines

OK, but further patches will add MSR_VIRT_SPEC_CTRL to hardware that doesn't expose MSR_SPEC_CTRL to guests, at which point it could be confusing?

Yeah, I obviously hadn't seen adjustments done here by later patches. When I saw those, I think I understood why you things do this way.

Quoted 12 lines
Quoted 11 lines

xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S IBRS provides same-mode protection · line 268

Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -265,7 +265,7 @@ XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S  IBRS provides same-mode protection
 XEN_CPUFEATURE(NO_LMSL,       8*32+20) /*S  EFER.LMSLE no longer supported. */
 XEN_CPUFEATURE(AMD_PPIN,      8*32+23) /*   Protected Processor Inventory Number */
 XEN_CPUFEATURE(AMD_SSBD,      8*32+24) /*S  MSR_SPEC_CTRL.SSBD available */
-XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*   MSR_VIRT_SPEC_CTRL.SSBD */
+XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*!s MSR_VIRT_SPEC_CTRL.SSBD */

What is the ! intended to cover here? From guest perspective the MSR acts entirely normally afaict.

Quoted 3 lines

I've used the ! to note that VIRT_SSBD might be exposed on hardware whether it's not available as part of the host featureset. It did seem to me that using just 's' didn't reflect this properly.

I wouldn't have assigned such meaning to !. In fact if we emulated a feature completely, I think it could legitimately show up here without !. But then again I may also not fully be aware of all of Andrew's intentions ...

Jan

Quoted 7 lines

According to my reading of the comment at the top '!' is not used to signal that the feature might act differently, but just that it's presence cannot be properly expressed with just the A, S, H flags, which would be the case for VIRT_SSBD I think.

Thanks, Roger.

#5 lore raw

Re: [PATCH 1/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests on top of SPEC_CTRL

Quoted 20 lines · Jan Beulich
Quoted 19 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

Use the logic to set shadow SPEC_CTRL values in order to implement support for VIRT_SPEC_CTRL (signaled by VIRT_SSBD CPUID flag) for HVM guests. This includes using the spec_ctrl vCPU MSR variable to store the guest set value of VIRT_SPEC_CTRL.SSBD.

This leverages the guest running on the OR of host and guest values, aiui. If so, this could do with spelling out.

Quoted 3 lines

Note that VIRT_SSBD is only set in the HVM max CPUID policy, as the default should be to expose SPEC_CTRL only and support VIRT_SPEC_CTRL for migration compatibility.

I'm afraid I don't understand this last statement: How would this be about migration compatibility? No guest so far can use VIRT_SPEC_CTRL, and a future guest using it is unlikely to be able to cope with the MSR "disappearing" during migration.

Quoted 7 lines
Quoted 5 lines

Maybe I didn't express this correctly. What I meant to explain is that on hardware having SPEC_CTRL VIRT_SPEC_CTRL will not be offered by default to guests. VIRT_SPEC_CTRL will only be part of the max CPUID policy so it can be enabled for compatibility purposes. Does this make sense?

Yes. Can you re-word along these lines?

Sure.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

docs/misc/xen-command-line.pandoc to use. · line 2276

Quoted 9 lines
docs/misc/xen-command-line.pandoc +1 -2
@@ -2273,8 +2273,9 @@ to use.
 * `pv=` and `hvm=` offer control over all suboptions for PV and HVM guests
   respectively.
 * `msr-sc=` offers control over Xen's support for manipulating `MSR_SPEC_CTRL`
-  on entry and exit.  These blocks are necessary to virtualise support for
-  guests and if disabled, guests will be unable to use IBRS/STIBP/SSBD/etc.
+  and/or `MSR_VIRT_SPEC_CTRL` on entry and exit.  These blocks are necessary to

Why would Xen be manipulating an MSR it only brings into existence for its guests?

Quoted 8 lines
Quoted 5 lines

Well, that's not exactly true. Xen does use VIRT_SPEC_CTRL (see amd_init_ssbd).

I'm unsure how to express support for VIRT_SPEC_CTRL, as it does rely on SPEC_CTRL when available.

I wonder whether the command line doc needs to go into this level of detail.

Right, so you would be fine with leaving the command line option description alone.

Quoted 22 lines
Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/cpuid.c +2 -0
@@ -543,6 +543,13 @@ static void __init calculate_hvm_max_policy(void)
         __clear_bit(X86_FEATURE_IBRSB, hvm_featureset);
         __clear_bit(X86_FEATURE_IBRS, hvm_featureset);
     }
+    else
+        /*
Quoted 8 lines

xen/arch/x86/cpuid.c static void __init calculate_hvm_max_policy(void) · line 552

Quoted 5 lines
xen/arch/x86/cpuid.c +5 -0
+         * If SPEC_CTRL is available VIRT_SPEC_CTRL can also be implemented as
+         * it's a subset of the controls exposed in SPEC_CTRL (SSBD only).
+         * Expose in the max policy for compatibility migration.
+         */
+        __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);

This means even Intel guests can use the feature then? I thought it was meanwhile deemed bad to offer such cross-vendor features?

No, we shouldn't expose to Intel.

Quoted 2 lines

Additionally, is SPEC_CTRL (i.e. IBRS) availability enough? Don't you need AMD_SSBD as a prereq (which may want expressing in gen-cpuid.py)?

Quoted 8 lines
Quoted 5 lines

We need AMD_SSBD if we implement VIRT_SPEC_CTRL on top of SPEC_CTRL, but we could also implement it on top of VIRT_SPEC_CTRL (if Xen runs virtualized) or even using the legacy SSBD setting mechanisms found in amd_init_ssbd, so I don't think VIRT_SSBD should explicitly depend on AMD_SSBD in gen-cpuid.py.

Hmm, yes, good point. But when the prereqs cannot be expressed in gen-cpuid.py, I guess they need to be encoded here.

Yes, I've added a dependency on AMD_SSBD here, which was missing.

Quoted 28 lines
Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/include/asm/msr.h +1 -1
@@ -291,6 +291,7 @@ struct vcpu_msrs
 {
     /*
      * 0x00000048 - MSR_SPEC_CTRL
+     * 0xc001011f - MSR_VIRT_SPEC_CTRL
      *
      * For PV guests, this holds the guest kernel value.  It is accessed on
      * every entry/exit path.
@@ -301,7 +302,10 @@ struct vcpu_msrs
      * For SVM, the guest value lives in the VMCB, and hardware saves/restores
      * the host value automatically.  However, guests run with the OR of the
      * host and guest value, which allows Xen to set protections behind the
-     * guest's back.
Quoted 8 lines

xen/arch/x86/include/asm/msr.h struct vcpu_msrs · line 308

Quoted 4 lines
xen/arch/x86/include/asm/msr.h +4 -0
+     * guest's back.  Use such functionality in order to implement support for
+     * VIRT_SPEC_CTRL as a shadow value of SPEC_CTRL and thus store the value
+     * of VIRT_SPEC_CTRL in this field, taking advantage of both MSRs having
+     * compatible layouts.

I guess "shadow value" means more like an alternative value, but (see above) this is about setting for now just one bit behind the guest's back.

Quoted 3 lines

Well, the guest sets the bit in VIRT_SPEC_CTRL and Xen sets it on SPEC_CTRL in order for it to have effect. I can use 'alternative value' if that's clearer.

Quoted 7 lines

Well, as I tried to express in my earlier reply, I view "shadow value" to mean "alternative value", so replacing wouldn't help. The question whether it acts like the shadow values we know elsewhere (VMX'es CR0 and CR4, for example). If it does, using the same term is of course fine. But it didn't look to me as if it would, hence I'd prefer to avoid ambiguity. But please realize that I may have misunderstood things ...

No, you are OK to ask. When developing the series I went back and forth myself deciding whether 'hijacking' the spec_ctrl field to implement VIRT_SPEC_CTRL was OK.

If host has AMD_SSBD: VIRT_SPEC_CTRL.SSBD will use the SPEC_CTRL.SSBD bit in the spec_ctrl field, but it will be set behind the guests back. If guests sets VIRT_SPEC_CTRL.SSBD but not SPEC_CTRL.SSBD, reads of SPEC_CTRL.SSBD from guest context will return 0, but the bit will be set.

I called it 'shadow' because the underlying SPEC_CTRL.SSBD bit will get set, but reading SPEC_CTRL.SSBD could return 0 if the bit has been set from VIRT_SPEC_CTRL.

Do you think that's a suitable use of 'shadow'?

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S IBRS provides same-mode protection · line 268

Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -265,7 +265,7 @@ XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S  IBRS provides same-mode protection
 XEN_CPUFEATURE(NO_LMSL,       8*32+20) /*S  EFER.LMSLE no longer supported. */
 XEN_CPUFEATURE(AMD_PPIN,      8*32+23) /*   Protected Processor Inventory Number */
 XEN_CPUFEATURE(AMD_SSBD,      8*32+24) /*S  MSR_SPEC_CTRL.SSBD available */
-XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*   MSR_VIRT_SPEC_CTRL.SSBD */
+XEN_CPUFEATURE(VIRT_SSBD,     8*32+25) /*!s MSR_VIRT_SPEC_CTRL.SSBD */

What is the ! intended to cover here? From guest perspective the MSR acts entirely normally afaict.

Quoted 8 lines
Quoted 3 lines

I've used the ! to note that VIRT_SSBD might be exposed on hardware whether it's not available as part of the host featureset. It did seem to me that using just 's' didn't reflect this properly.

I wouldn't have assigned such meaning to !. In fact if we emulated a feature completely, I think it could legitimately show up here without !. But then again I may also not fully be aware of all of Andrew's intentions ...

Not sure either. I've assumed '!' to mean that such feature could appear on guest policies even when not present on the host one, but I might be wrong. I'm happy to use a different annotation here.

Thanks, Roger.

#6 lore raw

Re: [PATCH 1/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests on top of SPEC_CTRL

Quoted 20 lines · Roger Pau Monné
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich

docs/misc/xen-command-line.pandoc to use. · line 2276

Quoted 9 lines · Roger Pau Monne
docs/misc/xen-command-line.pandoc +1 -2
@@ -2273,8 +2273,9 @@ to use.
 * `pv=` and `hvm=` offer control over all suboptions for PV and HVM guests
   respectively.
 * `msr-sc=` offers control over Xen's support for manipulating `MSR_SPEC_CTRL`
-  on entry and exit.  These blocks are necessary to virtualise support for
-  guests and if disabled, guests will be unable to use IBRS/STIBP/SSBD/etc.
+  and/or `MSR_VIRT_SPEC_CTRL` on entry and exit.  These blocks are necessary to

Why would Xen be manipulating an MSR it only brings into existence for its guests?

Well, that's not exactly true. Xen does use VIRT_SPEC_CTRL (see amd_init_ssbd).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I'm unsure how to express support for VIRT_SPEC_CTRL, as it does rely on SPEC_CTRL when available.

I wonder whether the command line doc needs to go into this level of detail.

Right, so you would be fine with leaving the command line option description alone.

Yes.

Quoted 46 lines
Quoted 28 lines
Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/include/asm/msr.h +1 -1
@@ -291,6 +291,7 @@ struct vcpu_msrs
 {
     /*
      * 0x00000048 - MSR_SPEC_CTRL
+     * 0xc001011f - MSR_VIRT_SPEC_CTRL
      *
      * For PV guests, this holds the guest kernel value.  It is accessed on
      * every entry/exit path.
@@ -301,7 +302,10 @@ struct vcpu_msrs
      * For SVM, the guest value lives in the VMCB, and hardware saves/restores
      * the host value automatically.  However, guests run with the OR of the
      * host and guest value, which allows Xen to set protections behind the
-     * guest's back.
Quoted 8 lines

xen/arch/x86/include/asm/msr.h struct vcpu_msrs · line 308

Quoted 4 lines
xen/arch/x86/include/asm/msr.h +4 -0
+     * guest's back.  Use such functionality in order to implement support for
+     * VIRT_SPEC_CTRL as a shadow value of SPEC_CTRL and thus store the value
+     * of VIRT_SPEC_CTRL in this field, taking advantage of both MSRs having
+     * compatible layouts.

I guess "shadow value" means more like an alternative value, but (see above) this is about setting for now just one bit behind the guest's back.

Quoted 3 lines

Well, the guest sets the bit in VIRT_SPEC_CTRL and Xen sets it on SPEC_CTRL in order for it to have effect. I can use 'alternative value' if that's clearer.

Quoted 7 lines

Well, as I tried to express in my earlier reply, I view "shadow value" to mean "alternative value", so replacing wouldn't help. The question whether it acts like the shadow values we know elsewhere (VMX'es CR0 and CR4, for example). If it does, using the same term is of course fine. But it didn't look to me as if it would, hence I'd prefer to avoid ambiguity. But please realize that I may have misunderstood things ...

No, you are OK to ask. When developing the series I went back and forth myself deciding whether 'hijacking' the spec_ctrl field to implement VIRT_SPEC_CTRL was OK.

If host has AMD_SSBD: VIRT_SPEC_CTRL.SSBD will use the SPEC_CTRL.SSBD bit in the spec_ctrl field, but it will be set behind the guests back. If guests sets VIRT_SPEC_CTRL.SSBD but not SPEC_CTRL.SSBD, reads of SPEC_CTRL.SSBD from guest context will return 0, but the bit will be set.

Quoted 5 lines

I called it 'shadow' because the underlying SPEC_CTRL.SSBD bit will get set, but reading SPEC_CTRL.SSBD could return 0 if the bit has been set from VIRT_SPEC_CTRL.

Do you think that's a suitable use of 'shadow'?

Not sure, but since I don't have a good alternative suggestion, please keep using "shadow".

Jan

#7 lore raw

PATCH 2/3 amd/msr: allow passthrough of VIRT_SPEC_CTRL for HVM guests

In reply to Roger Pau Monne

Allow HVM guests untrapped access to MSR_VIRT_SPEC_CTRL if the hardware has support for it. This requires adding logic in the vm{entry,exit} paths for SVM in order to context switch between the hypervisor value and the guest one. The added handlers for context switch will also be used for the legacy SSBD support.

Note that the implementation relies on storing the guest value in the spec_ctrl MSR per-vCPU variable, as the usage of VIRT_SPEC_CTRL precludes the usage of SPEC_CTRL. Also store the current and hypervisor states of VIRT_SPEC_CTRL in the per-pCPU spec_ctrl fields at cpu_info in order to properly context switch the values between Xen and HVM guests.

---
 xen/arch/x86/cpu/amd.c                 |  7 +++-
 xen/arch/x86/cpuid.c                   | 11 ++++++
 xen/arch/x86/hvm/svm/entry.S           |  8 +++-
 xen/arch/x86/hvm/svm/svm.c             | 55 ++++++++++++++++++++++++++
 xen/arch/x86/include/asm/cpufeatures.h |  1 +
 xen/arch/x86/spec_ctrl.c               |  8 +++-
 6 files changed, 86 insertions(+), 4 deletions(-)
xen/arch/x86/cpu/amd.c +6 -1
index a8e37dbb1f..c3fcc0e558 100644
@@ -687,6 +687,7 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
  */
 void amd_init_ssbd(const struct cpuinfo_x86 *c)
 {
+	struct cpu_info *info = get_cpu_info();
 	int bit = -1;
 
 	if (cpu_has_ssb_no)
@@ -699,7 +700,7 @@ void amd_init_ssbd(const struct cpuinfo_x86 *c)
 
 	if (cpu_has_virt_ssbd) {
 		wrmsrl(MSR_VIRT_SPEC_CTRL, opt_ssbd ? SPEC_CTRL_SSBD : 0);
-		return;
+		goto out;
 	}
 
 	switch (c->x86) {
@@ -729,6 +730,10 @@ void amd_init_ssbd(const struct cpuinfo_x86 *c)
 
 	if (bit < 0)
 		printk_once(XENLOG_ERR "No SSBD controls available\n");
+
+ out:
+	info->last_spec_ctrl = info->xen_spec_ctrl = opt_ssbd ? SPEC_CTRL_SSBD
+							      : 0;
 }
 
 void __init detect_zen2_null_seg_behaviour(void)
xen/arch/x86/cpuid.c +11 -0
index 29b4cfc9e6..7b10fbf12f 100644
@@ -551,6 +551,9 @@ static void __init calculate_hvm_max_policy(void)
          */
         __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);
 
+    if ( boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) )
+        __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);
+
     /*
      * With VT-x, some features are only supported by Xen if dedicated
      * hardware support is also available.
@@ -590,6 +593,14 @@ static void __init calculate_hvm_def_policy(void)
     guest_common_feature_adjustments(hvm_featureset);
     guest_common_default_feature_adjustments(hvm_featureset);
 
+    /*
+     * Only expose VIRT_SPEC_CTRL support by default if SPEC_CTRL is not
+     * supported.
+     */
+    if ( boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) &&
+         !boot_cpu_has(X86_FEATURE_SC_MSR_HVM) )
+        __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset);
+
     sanitise_featureset(hvm_featureset);
     cpuid_featureset_to_policy(hvm_featureset, p);
     recalculate_xstate(p);
xen/arch/x86/hvm/svm/entry.S +6 -2
index 4ae55a2ef6..2a0c41625b 100644
@@ -71,7 +71,9 @@ __UNLIKELY_END(nsvm_hap)
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
 1:          /* No Spectre v1 concerns.  Execution will hit VMRUN imminently. */
         .endm
-        ALTERNATIVE "", svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE_2 "", STR(call vmentry_virt_spec_ctrl), \
+                          X86_FEATURE_VIRT_SC_MSR_HVM, \
+                      svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
 
         pop  %r15
         pop  %r14
@@ -111,7 +113,9 @@ __UNLIKELY_END(nsvm_hap)
             wrmsr
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
         .endm
-        ALTERNATIVE "", svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE_2 "", STR(call vmexit_virt_spec_ctrl), \
+                          X86_FEATURE_VIRT_SC_MSR_HVM, \
+                      svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
 
         stgi
xen/arch/x86/hvm/svm/svm.c +55 -0

67 lines not on this page · raw message

xen/arch/x86/include/asm/cpufeatures.h +1 -0
index b10154fc44..a2c37bfdd4 100644
@@ -39,6 +39,7 @@ XEN_CPUFEATURE(SC_VERW_PV,        X86_SYNTH(23)) /* VERW used by Xen for PV */
 XEN_CPUFEATURE(SC_VERW_HVM,       X86_SYNTH(24)) /* VERW used by Xen for HVM */
 XEN_CPUFEATURE(SC_VERW_IDLE,      X86_SYNTH(25)) /* VERW used by Xen for idle */
 XEN_CPUFEATURE(XEN_SHSTK,         X86_SYNTH(26)) /* Xen uses CET Shadow Stacks */
+XEN_CPUFEATURE(VIRT_SC_MSR_HVM,   X86_SYNTH(27)) /* MSR_VIRT_SPEC_CTRL exposed to HVM */
 
 /* Bug words follow the synthetic words. */
 #define X86_NR_BUG 1
xen/arch/x86/spec_ctrl.c +7 -1
index 64b154b2d3..2c46e1485f 100644
@@ -399,9 +399,12 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps)
            (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ||
             boot_cpu_has(X86_FEATURE_SC_RSB_HVM) ||
             boot_cpu_has(X86_FEATURE_MD_CLEAR)   ||
+            boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) ||
             opt_eager_fpu)                           ? ""               : " None",
            boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_SPEC_CTRL" : "",
-           boot_cpu_has(X86_FEATURE_SC_MSR_HVM)      ? " MSR_VIRT_SPEC_CTRL" : "",
+           (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ||
+            boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM)) ? " MSR_VIRT_SPEC_CTRL"
+                                                       : "",
            boot_cpu_has(X86_FEATURE_SC_RSB_HVM)      ? " RSB"           : "",
            opt_eager_fpu                             ? " EAGER_FPU"     : "",
            boot_cpu_has(X86_FEATURE_MD_CLEAR)        ? " MD_CLEAR"      : "");
@@ -1053,6 +1056,9 @@ void __init init_speculation_mitigations(void)
             setup_force_cpu_cap(X86_FEATURE_SC_MSR_HVM);
     }
 
+    if ( opt_msr_sc_hvm && cpu_has_virt_ssbd )
+        setup_force_cpu_cap(X86_FEATURE_VIRT_SC_MSR_HVM);
+
     /* If we have IBRS available, see whether we should use it. */
     if ( has_spec_ctrl && ibrs )
         default_xen_spec_ctrl |= SPEC_CTRL_IBRS;
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH 2/3] amd/msr: allow passthrough of VIRT_SPEC_CTRL for HVM guests

Quoted 5 lines · Roger Pau Monne

Allow HVM guests untrapped access to MSR_VIRT_SPEC_CTRL if the hardware has support for it. This requires adding logic in the vm{entry,exit} paths for SVM in order to context switch between the hypervisor value and the guest one. The added handlers for context switch will also be used for the legacy SSBD support.

So by "hardware" you mean virtual hardware here, when we run virtualized ourselves? While the wording in AMD's whitepaper suggests hardware could exist with both MSRs implemented, so far it was my understanding that VIRT_SPEC_CTRL was rather left for hypervisors to implement. Maybe I'm wrong with this, in which case some of the further comments may also be wrong.

Quoted 20 lines
xen/arch/x86/cpu/amd.c +2 -1
@@ -687,6 +687,7 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
  */
 void amd_init_ssbd(const struct cpuinfo_x86 *c)
 {
+	struct cpu_info *info = get_cpu_info();
 	int bit = -1;
 
 	if (cpu_has_ssb_no)
@@ -699,7 +700,7 @@ void amd_init_ssbd(const struct cpuinfo_x86 *c)
 
 	if (cpu_has_virt_ssbd) {
 		wrmsrl(MSR_VIRT_SPEC_CTRL, opt_ssbd ? SPEC_CTRL_SSBD : 0);
-		return;
+		goto out;
 	}
 
 	switch (c->x86) {

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 737

Quoted 8 lines
xen/arch/x86/cpu/amd.c +4 -0
@@ -729,6 +730,10 @@ void amd_init_ssbd(const struct cpuinfo_x86 *c)
 
 	if (bit < 0)
 		printk_once(XENLOG_ERR "No SSBD controls available\n");
+
+ out:
+	info->last_spec_ctrl = info->xen_spec_ctrl = opt_ssbd ? SPEC_CTRL_SSBD
+							      : 0;
 }

Besides me being uncertain about the placement of these (preferably the writes would be where the other similar writes are), this re-use of the values suggests that you mean to prefer VIRT_SPEC_CTRL use over that of SPEC_CTRL (see below).

Additionally - the value you store isn't necessarily the value you wrote to the MSR. It only is if you cam here via the "goto out".

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 74

Quoted 8 lines
xen/arch/x86/hvm/svm/entry.S +1 -1
@@ -71,7 +71,9 @@ __UNLIKELY_END(nsvm_hap)
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
 1:          /* No Spectre v1 concerns.  Execution will hit VMRUN imminently. */
         .endm
-        ALTERNATIVE "", svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE_2 "", STR(call vmentry_virt_spec_ctrl), \

I'm afraid this violates the "ret" part of the warning a few lines up, while ...

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 116

Quoted 11 lines
xen/arch/x86/hvm/svm/entry.S +3 -1
+                          X86_FEATURE_VIRT_SC_MSR_HVM, \
+                      svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
 
         pop  %r15
         pop  %r14
@@ -111,7 +113,9 @@ __UNLIKELY_END(nsvm_hap)
             wrmsr
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
         .endm
-        ALTERNATIVE "", svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE_2 "", STR(call vmexit_virt_spec_ctrl), \

... this violates ...

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 119

Quoted 3 lines
xen/arch/x86/hvm/svm/entry.S +2 -0
+                          X86_FEATURE_VIRT_SC_MSR_HVM, \
+                      svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */

... the "ret" part of this warning.

Furthermore, opposite to what the change to amd_init_ssbd() suggests, the ordering of the alternatives here means you prefer SPEC_CTRL over VIRT_SPEC_CTRL; see the comment near the top of _apply_alternatives(). Unless I've missed logic guaranteeing that both of the keyed to features can't be active at the same time.

Jan

#9 lore raw

Re: [PATCH 2/3] amd/msr: allow passthrough of VIRT_SPEC_CTRL for HVM guests

Quoted 27 lines · Jan Beulich

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 74

Quoted 8 lines · Roger Pau Monne
xen/arch/x86/hvm/svm/entry.S +1 -1
@@ -71,7 +71,9 @@ __UNLIKELY_END(nsvm_hap)
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
 1:          /* No Spectre v1 concerns.  Execution will hit VMRUN imminently. */
         .endm
-        ALTERNATIVE "", svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE_2 "", STR(call vmentry_virt_spec_ctrl), \

I'm afraid this violates the "ret" part of the warning a few lines up, while ...

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 116

Quoted 11 lines
xen/arch/x86/hvm/svm/entry.S +3 -1
+                          X86_FEATURE_VIRT_SC_MSR_HVM, \
+                      svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
 
         pop  %r15
         pop  %r14
@@ -111,7 +113,9 @@ __UNLIKELY_END(nsvm_hap)
             wrmsr
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
         .endm
-        ALTERNATIVE "", svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE_2 "", STR(call vmexit_virt_spec_ctrl), \

... this violates ...

Quoted 5 lines

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 119

Quoted 3 lines
xen/arch/x86/hvm/svm/entry.S +2 -0
+                          X86_FEATURE_VIRT_SC_MSR_HVM, \
+                      svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */

... the "ret" part of this warning.

Hm, so while I could load VIRT_SPEC_CTRL easily from assembly, loading of the legacy non-architectural setting of SSBD for Fam18h and earlier it's likely not doable from assembly.

Since those helpers would only set SSBD, isn't it fine to execute a ret after either having set or clear SSBD?

AFAICT the requirement would be to either have loaded SPEC_CTRL first (if present) in the VM exit path, or to set SSBD before setting SPEC_CTRL in the VM entry path.

Quoted 5 lines

Furthermore, opposite to what the change to amd_init_ssbd() suggests, the ordering of the alternatives here means you prefer SPEC_CTRL over VIRT_SPEC_CTRL; see the comment near the top of _apply_alternatives(). Unless I've missed logic guaranteeing that both of the keyed to features can't be active at the same time.

Xen itself will only use a single one (either SPEC_CTRL.SSBD or VIRT_SPEC_CTRL.SSBD) in order to implement support on behalf of the guest. amd_init_ssbd already prefer to use SPEC_CTRL.SSBD over VIRT_SPEC_CTRL.SSBD when both are available, so we aim to do the same here.

I think part of the confusion steams from using info->{last_spec_ctrl, xen_spec_ctrl} even when SPEC_CTRL MSR is not used by Xen, I need to clarify this somehow, maybe by not using those fields in the first place.

Thanks, Roger.

#10 lore raw

Re: [PATCH 2/3] amd/msr: allow passthrough of VIRT_SPEC_CTRL for HVM guests

Quoted 38 lines · Roger Pau Monné
Quoted 27 lines · Jan Beulich

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 74

Quoted 8 lines · Roger Pau Monne
xen/arch/x86/hvm/svm/entry.S +1 -1
@@ -71,7 +71,9 @@ __UNLIKELY_END(nsvm_hap)
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
 1:          /* No Spectre v1 concerns.  Execution will hit VMRUN imminently. */
         .endm
-        ALTERNATIVE "", svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE_2 "", STR(call vmentry_virt_spec_ctrl), \

I'm afraid this violates the "ret" part of the warning a few lines up, while ...

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 116

Quoted 11 lines
xen/arch/x86/hvm/svm/entry.S +3 -1
+                          X86_FEATURE_VIRT_SC_MSR_HVM, \
+                      svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM
 
         pop  %r15
         pop  %r14
@@ -111,7 +113,9 @@ __UNLIKELY_END(nsvm_hap)
             wrmsr
             mov    %al, CPUINFO_last_spec_ctrl(%rsp)
         .endm
-        ALTERNATIVE "", svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
+        ALTERNATIVE_2 "", STR(call vmexit_virt_spec_ctrl), \

... this violates ...

Quoted 5 lines

xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 119

Quoted 3 lines
xen/arch/x86/hvm/svm/entry.S +2 -0
+                          X86_FEATURE_VIRT_SC_MSR_HVM, \
+                      svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */

... the "ret" part of this warning.

Hm, so while I could load VIRT_SPEC_CTRL easily from assembly, loading of the legacy non-architectural setting of SSBD for Fam18h and earlier it's likely not doable from assembly.

Quoted 6 lines

Since those helpers would only set SSBD, isn't it fine to execute a ret after either having set or clear SSBD?

AFAICT the requirement would be to either have loaded SPEC_CTRL first (if present) in the VM exit path, or to set SSBD before setting SPEC_CTRL in the VM entry path.

Yes, setting SSBD with SPEC_CTRL already / still set ought to be fine.

Quoted 11 lines
Quoted 5 lines

Furthermore, opposite to what the change to amd_init_ssbd() suggests, the ordering of the alternatives here means you prefer SPEC_CTRL over VIRT_SPEC_CTRL; see the comment near the top of _apply_alternatives(). Unless I've missed logic guaranteeing that both of the keyed to features can't be active at the same time.

Xen itself will only use a single one (either SPEC_CTRL.SSBD or VIRT_SPEC_CTRL.SSBD) in order to implement support on behalf of the guest. amd_init_ssbd already prefer to use SPEC_CTRL.SSBD over VIRT_SPEC_CTRL.SSBD when both are available, so we aim to do the same here.

Hmm, I can't see the change to init_speculation_mitigations() guaranteeing that at most one of the two would be enabled.

Quoted 4 lines

I think part of the confusion steams from using info->{last_spec_ctrl, xen_spec_ctrl} even when SPEC_CTRL MSR is not used by Xen, I need to clarify this somehow, maybe by not using those fields in the first place.

I don't think this matters for this particular aspect of my reply. It was possibly causing some confusion to me, but elsewhere.

Jan

#11 lore raw

PATCH 3/3 amd/msr: implement VIRT_SPEC_CTRL for HVM guests using legacy SSBD

In reply to Roger Pau Monne

Expose VIRT_SPEC_CTRL to guests if the hardware supports setting SSBD in the LS_CFG MSR. Different AMD CPU families use different bits in LS_CFG, so exposing VIRT_SPEC_CTRL.SSBD allows for an unified way of setting SSBD on AMD hardware that's compatible migration wise, regardless of what underlying mechanism is used to set SSBD.

Note that on AMD Family 17h (Zen 1) the value of SSBD in LS_CFG is shared between threads on the same core, so there's extra logic in order to synchronize the value and have SSBD set as long as one of the threads in the core requires it to be set. Such logic also requires extra storage for each thread state, which is allocated at initialization time.

Do the context switching of the SSBD selection in LS_CFG between hypervisor and guest in the same handler that's already used to switch the value of VIRT_SPEC_CTRL in the hardware when supported.

---
 xen/arch/x86/cpu/amd.c                 | 113 +++++++++++++++++++++----
 xen/arch/x86/hvm/svm/svm.c             |  14 ++-
 xen/arch/x86/include/asm/amd.h         |   3 +
 xen/arch/x86/include/asm/cpufeatures.h |   1 +
 xen/arch/x86/spec_ctrl.c               |   4 +-
 5 files changed, 115 insertions(+), 20 deletions(-)
xen/arch/x86/cpu/amd.c +95 -18
index c3fcc0e558..7318623874 100644
@@ -685,24 +685,10 @@ void amd_init_lfence(struct cpuinfo_x86 *c)
  * Refer to the AMD Speculative Store Bypass whitepaper:
  * https://developer.amd.com/wp-content/resources/124441_AMD64_SpeculativeStoreBypassDisable_Whitepaper_final.pdf
  */
-void amd_init_ssbd(const struct cpuinfo_x86 *c)
+static bool set_legacy_ssbd(const struct cpuinfo_x86 *c, bool enable)
 {
-	struct cpu_info *info = get_cpu_info();
 	int bit = -1;
 
-	if (cpu_has_ssb_no)
-		return;
-
-	if (cpu_has_amd_ssbd) {
-		/* Handled by common MSR_SPEC_CTRL logic */
-		return;
-	}
-
-	if (cpu_has_virt_ssbd) {
-		wrmsrl(MSR_VIRT_SPEC_CTRL, opt_ssbd ? SPEC_CTRL_SSBD : 0);
-		goto out;
-	}
-
 	switch (c->x86) {
 	case 0x15: bit = 54; break;
 	case 0x16: bit = 33; break;
@@ -716,26 +702,117 @@ void amd_init_ssbd(const struct cpuinfo_x86 *c)
 		if (rdmsr_safe(MSR_AMD64_LS_CFG, val) ||
 		    ({
 			    val &= ~mask;
-			    if (opt_ssbd)
+			    if (enable)
 				    val |= mask;
 			    false;
 		    }) ||
 		    wrmsr_safe(MSR_AMD64_LS_CFG, val) ||
 		    ({
 			    rdmsrl(MSR_AMD64_LS_CFG, val);
-			    (val & mask) != (opt_ssbd * mask);
+			    (val & mask) != (enable * mask);
 		    }))
 			bit = -1;
 	}
 
-	if (bit < 0)
+	return bit >= 0;
+}
+
+void amd_init_ssbd(const struct cpuinfo_x86 *c)
+{
+	struct cpu_info *info = get_cpu_info();
+
+	if (cpu_has_ssb_no)
+		return;
+
+	if (cpu_has_amd_ssbd) {
+		/* Handled by common MSR_SPEC_CTRL logic */
+		return;
+	}
+
+	if (cpu_has_virt_ssbd) {
+		wrmsrl(MSR_VIRT_SPEC_CTRL, opt_ssbd ? SPEC_CTRL_SSBD : 0);
+		goto out;
+	}
+
+	if (!set_legacy_ssbd(c, opt_ssbd)) {
 		printk_once(XENLOG_ERR "No SSBD controls available\n");
+		return;
+	}
+
+	if (!smp_processor_id())
+		setup_force_cpu_cap(X86_FEATURE_LEGACY_SSBD);
 
  out:
 	info->last_spec_ctrl = info->xen_spec_ctrl = opt_ssbd ? SPEC_CTRL_SSBD
 							      : 0;
 }
 
+static struct ssbd_core {
+    spinlock_t lock;
+    unsigned int count;
+} *ssbd_core;
+static unsigned int __read_mostly ssbd_max_cores;
+
+bool __init amd_setup_legacy_ssbd(void)
+{
+	unsigned int i;
+
+	if (boot_cpu_data.x86 != 0x17 || boot_cpu_data.x86_num_siblings == 1)
+		return true;
+
+	/*
+	 * One could be forgiven for thinking that c->x86_max_cores is the
+	 * correct value to use here.
+	 *
+	 * However, that value is derived from the current configuration, and
+	 * c->cpu_core_id is sparse on all but the top end CPUs.  Derive
+	 * max_cpus from ApicIdCoreIdSize which will cover any sparseness.
+	 */
+	if (boot_cpu_data.extended_cpuid_level >= 0x80000008) {
+		ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008), 0xf000);
+		ssbd_max_cores /= boot_cpu_data.x86_num_siblings;
+	}
+	if (!ssbd_max_cores)
+		return false;
+
+	/* Max is two sockets for Fam17h hardware. */
+	ssbd_core = xzalloc_array(struct ssbd_core, ssbd_max_cores * 2);
+	if (!ssbd_core)
+		return false;
+
+	for (i = 0; i < ssbd_max_cores * 2; i++) {
+		spin_lock_init(&ssbd_core[i].lock);
+		/* Record the current state. */
+		ssbd_core[i].count = opt_ssbd ?
+				     boot_cpu_data.x86_num_siblings : 0;
+	}
+
+	return true;
+}
+
+void amd_set_legacy_ssbd(bool enable)
+{
+	const struct cpuinfo_x86 *c = &current_cpu_data;
+	struct ssbd_core *core;
+	unsigned long flags;
+
+	if (c->x86 != 0x17 || c->x86_num_siblings == 1) {
+		set_legacy_ssbd(c, enable);
+		return;
+	}
+
+	ASSERT(c->phys_proc_id < 2);
+	ASSERT(c->cpu_core_id < ssbd_max_cores);
+	core = &ssbd_core[c->phys_proc_id * ssbd_max_cores + c->cpu_core_id];
+	spin_lock_irqsave(&core->lock, flags);
+	core->count += enable ? 1 : -1;
+	ASSERT(core->count <= c->x86_num_siblings);
+	if ((enable  && core->count == 1) ||
+	    (!enable && core->count == 0))
+		BUG_ON(!set_legacy_ssbd(c, enable));
+	spin_unlock_irqrestore(&core->lock, flags);
+}
+
 void __init detect_zen2_null_seg_behaviour(void)
 {
 	uint64_t base;
xen/arch/x86/hvm/svm/svm.c +13 -1
index 56c7b30b32..10a5a77ad7 100644
@@ -3134,6 +3134,15 @@ void vmexit_virt_spec_ctrl(void)
             wrmsr(MSR_VIRT_SPEC_CTRL, val, 0);
         curr->arch.msrs->spec_ctrl.raw = lo;
         info->last_spec_ctrl = val;
+
+        return;
+    }
+
+    ASSERT(boot_cpu_has(X86_FEATURE_LEGACY_SSBD));
+    if ( val != info->last_spec_ctrl )
+    {
+        amd_set_legacy_ssbd(val & SPEC_CTRL_SSBD);
+        info->last_spec_ctrl = val;
     }
 }
 
@@ -3147,7 +3156,10 @@ void vmentry_virt_spec_ctrl(void)
     ASSERT(!boot_cpu_has(X86_FEATURE_SC_MSR_HVM));
     if ( val != info->last_spec_ctrl )
     {
-        wrmsr(MSR_VIRT_SPEC_CTRL, val, 0);
+        if ( boot_cpu_has(X86_FEATURE_LEGACY_SSBD) )
+            amd_set_legacy_ssbd(val & SPEC_CTRL_SSBD);
+        else
+            wrmsr(MSR_VIRT_SPEC_CTRL, val, 0);
         info->last_spec_ctrl = val;
     }
 
xen/arch/x86/include/asm/amd.h +3 -0
index a82382e6bf..823e2f3bd2 100644
@@ -151,4 +151,7 @@ void check_enable_amd_mmconf_dmi(void);
 extern bool amd_acpi_c1e_quirk;
 void amd_check_disable_c1e(unsigned int port, u8 value);
 
+bool amd_setup_legacy_ssbd(void);
+void amd_set_legacy_ssbd(bool enable);
+
 #endif /* __AMD_H__ */
xen/arch/x86/include/asm/cpufeatures.h +1 -0
index a2c37bfdd4..f12d423fe9 100644
@@ -40,6 +40,7 @@ XEN_CPUFEATURE(SC_VERW_HVM,       X86_SYNTH(24)) /* VERW used by Xen for HVM */
 XEN_CPUFEATURE(SC_VERW_IDLE,      X86_SYNTH(25)) /* VERW used by Xen for idle */
 XEN_CPUFEATURE(XEN_SHSTK,         X86_SYNTH(26)) /* Xen uses CET Shadow Stacks */
 XEN_CPUFEATURE(VIRT_SC_MSR_HVM,   X86_SYNTH(27)) /* MSR_VIRT_SPEC_CTRL exposed to HVM */
+XEN_CPUFEATURE(LEGACY_SSBD,       X86_SYNTH(28)) /* LS_CFG available for SSBD */
 
 /* Bug words follow the synthetic words. */
 #define X86_NR_BUG 1
xen/arch/x86/spec_ctrl.c +3 -1
index 2c46e1485f..c7f8ec29f4 100644
@@ -22,6 +22,7 @@
 #include <xen/param.h>
 #include <xen/warning.h>
 
+#include <asm/amd.h>
 #include <asm/hvm/svm/svm.h>
 #include <asm/microcode.h>
 #include <asm/msr.h>
@@ -1056,7 +1057,8 @@ void __init init_speculation_mitigations(void)
             setup_force_cpu_cap(X86_FEATURE_SC_MSR_HVM);
     }
 
-    if ( opt_msr_sc_hvm && cpu_has_virt_ssbd )
+    if ( opt_msr_sc_hvm && (cpu_has_virt_ssbd ||
+         (boot_cpu_has(X86_FEATURE_LEGACY_SSBD) && amd_setup_legacy_ssbd())) )
         setup_force_cpu_cap(X86_FEATURE_VIRT_SC_MSR_HVM);
 
     /* If we have IBRS available, see whether we should use it. */
Signature · 1 line
2.34.1
#12 lore raw

Re: [PATCH 3/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests using legacy SSBD

Quoted 36 lines · Roger Pau Monne
xen/arch/x86/cpu/amd.c +19 -3
@@ -716,26 +702,117 @@ void amd_init_ssbd(const struct cpuinfo_x86 *c)
 		if (rdmsr_safe(MSR_AMD64_LS_CFG, val) ||
 		    ({
 			    val &= ~mask;
-			    if (opt_ssbd)
+			    if (enable)
 				    val |= mask;
 			    false;
 		    }) ||
 		    wrmsr_safe(MSR_AMD64_LS_CFG, val) ||
 		    ({
 			    rdmsrl(MSR_AMD64_LS_CFG, val);
-			    (val & mask) != (opt_ssbd * mask);
+			    (val & mask) != (enable * mask);
 		    }))
 			bit = -1;
 	}
 
-	if (bit < 0)
+	return bit >= 0;
+}
+
+void amd_init_ssbd(const struct cpuinfo_x86 *c)
+{
+	struct cpu_info *info = get_cpu_info();
+
+	if (cpu_has_ssb_no)
+		return;
+
+	if (cpu_has_amd_ssbd) {
+		/* Handled by common MSR_SPEC_CTRL logic */
+		return;
+	}
+
+	if (cpu_has_virt_ssbd) {
+		wrmsrl(MSR_VIRT_SPEC_CTRL, opt_ssbd ? SPEC_CTRL_SSBD : 0);

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 743

Quoted 10 lines
xen/arch/x86/cpu/amd.c +9 -0
+		goto out;
+	}
+
+	if (!set_legacy_ssbd(c, opt_ssbd)) {
 		printk_once(XENLOG_ERR "No SSBD controls available\n");
+		return;
+	}
+
+	if (!smp_processor_id())
+		setup_force_cpu_cap(X86_FEATURE_LEGACY_SSBD);

I don't think you need a new feature flag here: You only ever use it with boot_cpu_has() and there's no alternatives patching keyed to it, so a single global flag will likely do.

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 754

Quoted 10 lines
xen/arch/x86/cpu/amd.c +5 -0
  out:
 	info->last_spec_ctrl = info->xen_spec_ctrl = opt_ssbd ? SPEC_CTRL_SSBD
 							      : 0;
 }
 
+static struct ssbd_core {
+    spinlock_t lock;
+    unsigned int count;
+} *ssbd_core;
+static unsigned int __read_mostly ssbd_max_cores;

__ro_after_init?

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 760

Quoted 5 lines
xen/arch/x86/cpu/amd.c +5 -0
+bool __init amd_setup_legacy_ssbd(void)
+{
+	unsigned int i;
+
+	if (boot_cpu_data.x86 != 0x17 || boot_cpu_data.x86_num_siblings == 1)

Maybe better "<= 1", not the least ...

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 773

Quoted 13 lines
xen/arch/x86/cpu/amd.c +13 -0
+		return true;
+
+	/*
+	 * One could be forgiven for thinking that c->x86_max_cores is the
+	 * correct value to use here.
+	 *
+	 * However, that value is derived from the current configuration, and
+	 * c->cpu_core_id is sparse on all but the top end CPUs.  Derive
+	 * max_cpus from ApicIdCoreIdSize which will cover any sparseness.
+	 */
+	if (boot_cpu_data.extended_cpuid_level >= 0x80000008) {
+		ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008), 0xf000);
+		ssbd_max_cores /= boot_cpu_data.x86_num_siblings;

... because of this division. I don't know whether we're also susceptible to this, but I've seen Linux (on top of Xen) being confused enough about the topology related CPUID data we expose that it ended up running with the value set to zero (and then exploding e.g. on a similar use).

Quoted 24 lines
xen/arch/x86/cpu/amd.c +24 -0
+	}
+	if (!ssbd_max_cores)
+		return false;
+
+	/* Max is two sockets for Fam17h hardware. */
+	ssbd_core = xzalloc_array(struct ssbd_core, ssbd_max_cores * 2);
+	if (!ssbd_core)
+		return false;
+
+	for (i = 0; i < ssbd_max_cores * 2; i++) {
+		spin_lock_init(&ssbd_core[i].lock);
+		/* Record the current state. */
+		ssbd_core[i].count = opt_ssbd ?
+				     boot_cpu_data.x86_num_siblings : 0;
+	}
+
+	return true;
+}
+
+void amd_set_legacy_ssbd(bool enable)
+{
+	const struct cpuinfo_x86 *c = &current_cpu_data;
+	struct ssbd_core *core;
+	unsigned long flags;

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 807

Quoted 10 lines
xen/arch/x86/cpu/amd.c +10 -0
+
+	if (c->x86 != 0x17 || c->x86_num_siblings == 1) {
+		set_legacy_ssbd(c, enable);
+		return;
+	}
+
+	ASSERT(c->phys_proc_id < 2);
+	ASSERT(c->cpu_core_id < ssbd_max_cores);
+	core = &ssbd_core[c->phys_proc_id * ssbd_max_cores + c->cpu_core_id];
+	spin_lock_irqsave(&core->lock, flags);

May I suggest a brief comment on the irqsave aspect here? Aiui when called from vmexit_virt_spec_ctrl() while we're still in a GIF=0 section, IF is 1 and hence check_lock() would be unhappy (albeit in a false positive way).

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 811

Quoted 4 lines
xen/arch/x86/cpu/amd.c +4 -0
+	core->count += enable ? 1 : -1;
+	ASSERT(core->count <= c->x86_num_siblings);
+	if ((enable  && core->count == 1) ||
+	    (!enable && core->count == 0))

Maybe simply "if ( core->count == enable )"? Or do compilers not like comparisons with booleans?

Quoted 11 lines
xen/arch/x86/spec_ctrl.c +1 -0
@@ -22,6 +22,7 @@
 #include <xen/param.h>
 #include <xen/warning.h>
 
+#include <asm/amd.h>
 #include <asm/hvm/svm/svm.h>
 #include <asm/microcode.h>
 #include <asm/msr.h>

xen/arch/x86/spec_ctrl.c void __init init_speculation_mitigations(void) · line 1061

Quoted 6 lines
xen/arch/x86/spec_ctrl.c +2 -1
@@ -1056,7 +1057,8 @@ void __init init_speculation_mitigations(void)
             setup_force_cpu_cap(X86_FEATURE_SC_MSR_HVM);
     }
 
-    if ( opt_msr_sc_hvm && cpu_has_virt_ssbd )
+    if ( opt_msr_sc_hvm && (cpu_has_virt_ssbd ||
+         (boot_cpu_has(X86_FEATURE_LEGACY_SSBD) && amd_setup_legacy_ssbd())) )

Nit: I think such expressions are better wrapped such that indentation expresses the number of pending open parentheses.

Jan

#13 lore raw

Re: [PATCH 3/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests using legacy SSBD

Quoted 7 lines · Jan Beulich

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 811

Quoted 3 lines · Roger Pau Monne
xen/arch/x86/cpu/amd.c +3 -0
+	ASSERT(core->count <= c->x86_num_siblings);
+	if ((enable  && core->count == 1) ||
+	    (!enable && core->count == 0))

Maybe simply "if ( core->count == enable )"? Or do compilers not like comparisons with booleans?

I had it like that, but decided to switch to the current code just before sending because I think it's clearer. I didn't get complaints from compilers, but I felt it was kind of abusing to compare a boolean with and integer.

If you wish I can restore to that form.

Thanks, Roger.

#14 lore raw

Re: [PATCH 3/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests using legacy SSBD

Quoted 9 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

xen/arch/x86/cpu/amd.c void amd_init_ssbd(const struct cpuinfo_x86 *c) · line 811

Quoted 3 lines · Roger Pau Monne
xen/arch/x86/cpu/amd.c +3 -0
+	ASSERT(core->count <= c->x86_num_siblings);
+	if ((enable  && core->count == 1) ||
+	    (!enable && core->count == 0))

Maybe simply "if ( core->count == enable )"? Or do compilers not like comparisons with booleans?

Quoted 6 lines

I had it like that, but decided to switch to the current code just before sending because I think it's clearer. I didn't get complaints from compilers, but I felt it was kind of abusing to compare a boolean with and integer.

If you wish I can restore to that form.

Well, if you don't like that alternative form, and since I don't like the redundancy, how about

    if ( enable ? core->count == 1 : !core->count )

? It was actually via this transformation how I landed at what I did suggest.

Jan

#15 lore raw
In reply to Roger Pau Monne
Quoted 13 lines · Roger Pau Monne

Hello,

The following series implements support for MSR_VIRT_SPEC_CTRL on different AMD CPU families.

Note that the support is added backwards, starting with the newer CPUs that support MSR_SPEC_CTRL and moving to the older ones either using MSR_VIRT_SPEC_CTRL or the SSBD bit in LS_CFG.

First patch is quite clean, as it uses the shadow SPEC_CTRL in order to set the SSBD bit and have it context switched by Xen using the existing logic recently added.

Quoted 8 lines

The next two patches introduce a different way to context switch SSBD either depending on the underlying SSBD support, so it's either using VIRT_SPEC_CTRL or the LS_CFG MSR. They also kind of overload the usage of several spec_ctrl variables in the hypervisor in order to store the status of SSBD even when not using MSR_SPEC_CTRL itself. I've tried to document those in the commit messages, but it could be controversial.

Thanks, Roger.

I suspect it would help reviewing things to state what the end result is intended to be.

1) Xen should use the AMD provided algorithm for engaging SSBD itself. This includes using MSR_VIRT_SPEC_CTRL if Xen is nested under another hypervisor.  In the current code, this is implemented by amd_init_ssbd() even if only limited to boot paths for simplicity.

2) On Fam15h thru Zen1, Xen should expose MSR_VIRT_SPEC_CTRL to guests by default to abstract away the model and/or hypervisor specific differences away in MSR_LS_CFG/MSR_VIRT_SPEC_CTRL.

3) On Zen2 and later, MSR_SPEC_CTRL exists and should be used in preference.  However, for migration compatibility, Xen should be capable of offering MSR_VIRT_SPEC_CTRL to guests (max, not default) implemented in terms of MSR_SPEC_CTRL.

This way, a VM levelled to run on Zen1 and Zen2 sees MSR_VIRT_SPEC_CTRL and can use it on both hosts, whereas a VM only intending to run on Zen2 gets MSR_SPEC_CTRL by default.

Obviously this means that a VM on Zen2 can opt in to MSR_VIRT_SPEC_CTRL because of how max vs default works and this is a legal configuration, even if it's not one you'd expect to see outside of testing scenarios.

~Andrew