x86/nestedsvm: Require SpecCtrl
PATCH 1/1 x86/nestedsvm: Require SpecCtrl
From: Chunjie Zhu <[email protected]>
Previously L0 did not expose this bit, forcing L1 to intercept and emulate MSR_SPEC_CTRL accesses from L2 in software, which is functionally correct but suboptimal. Per APM vol.2 rev 3.44 "SPEC_CTRL Virtualization", hardware supports combining host and guest SPEC_CTRL values via VMCB.spec_ctrl with a logical OR while a guest runs, and saves the (possibly guest-modified) value back into VMCB.spec_ctrl on #VMEXIT.
Require the SVM SpecCtrl feature before running any nested virt guests.
Update docs/designs/nested-svm-cpu-features.md with the rationale for requiring and exposing bit 20 (SpecCtrl).
--- docs/designs/nested-svm-cpu-features.md | 13 +++++++++++++ xen/arch/x86/cpu-policy.c | 3 ++- xen/arch/x86/hvm/svm/nestedsvm.c | 26 ++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-)
docs/designs/nested-svm-cpu-features.md +13 -0
@@ -109,3 +109,16 @@ leaf 8000000A:edx Using it in L0 reduces the chance that we'll make some sort of error in the decode path. And if hardware supports it, it's easy enough to provide to the L1. + +- 20 `SpecCtrl` *MSR_SPEC_CTRL virtualisation*: Require for L0, provide to L1 + + L0 already relies on this feature to virtualize MSR_SPEC_CTRL for the + L1 hypervisor itself (see `X86_FEATURE_SC_MSR_HVM`). Requiring it + as a baseline for nested virt (first available on Zen2) means L1 can + rely on the same hardware virtualisation for its own L2 guests, + instead of having L0 unconditionally intercept and trap/emulate + MSR_SPEC_CTRL accesses coming from L2. Providing it to L1 is + basically free once it's required of L0, and it removes a + significant amount of "CPU without the feature" fallback code that + would otherwise need to be implemented and maintained for nested + virt.
xen/arch/x86/cpu-policy.c +2 -1
@@ -832,7 +832,8 @@ static void __init calculate_hvm_max_policy(void) (1u <<SVM_FEATURE_LBRV ) | (1u <<SVM_FEATURE_NRIPS ) | (1u <<SVM_FEATURE_PAUSEFILTER ) | - (1u << SVM_FEATURE_DECODEASSISTS)); + (1u <<SVM_FEATURE_DECODEASSISTS ) | + (1u << SVM_FEATURE_SPEC_CTRL));/* Enable features which are always emulated. */ p->extd.raw[0xa ].d |= (1u <<SVM_FEATURE_VMCBCLEAN ); }
xen/arch/x86/hvm/svm/nestedsvm.c +25 -1
@@ -481,6 +481,16 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)/* TSC offset */ n2vmcb->_tsc_offset = n1vmcb->_tsc_offset + ns_vmcb->_tsc_offset; +/* +* SPEC_CTRL: When in host mode, the host SPEC_CTRL value is in effect +* and writes update the host SPEC_CTRL register. On a VMRUN, the +* processor loads the guest SPEC_CTRL register value from the VMCB or +* VMSA. For most guests, processor behavior is controlled by the logical +* OR of the two registers. (APM vol.2 rev 3.44 +* "SPEC_CTRL Virtualization"). +*/ + n2vmcb->spec_ctrl = n1vmcb->spec_ctrl | ns_vmcb->spec_ctrl; +/* Nested IO permission bitmaps */ rc =nsvm_vmrun_permissionmap (v, clean.iopm);if ( rc )
@@ -1022,6 +1032,7 @@ nsvm_vmcb_prepare4vmexit(struct vcpu *v, struct cpu_user_regs *regs)struct nestedvcpu *nv = &vcpu_nestedhvm (v);struct nestedsvm *svm = &vcpu_nestedsvm (v);struct vmcb_struct *ns_vmcb = nv->nv_vvmcx; +struct vmcb_struct *n1vmcb = nv->nv_n1vmcx;struct vmcb_struct *n2vmcb = nv->nv_n2vmcx;svm_vmsave_pa (nv->nv_n1vmcx_pa);
@@ -1035,6 +1046,18 @@ nsvm_vmcb_prepare4vmexit(struct vcpu *v, struct cpu_user_regs *regs)/* Pausefilter - keep it as is */ +/* +* SPEC_CTRL: on real hardware, only the guest's own SPEC_CTRL bits are +* ever saved back to the VMCB on #VMEXIT (APM vol.2 rev 3.44 "SPEC_CTRL +* Virtualization"); the OR with the host's bits is a transient +* execution-time effect, never written back into the guest register. +* Here, n2vmcb->spec_ctrl was seeded with n1vmcb's bits OR'd in at VMRUN +* time (see nsvm_vmcb_prepare4vmrun()), and will still contain them if L2 +* never wrote the MSR itself. Mask those back out so L1's own SPEC_CTRL +* doesn't leak into, and persist in, L2's saved state. +*/ + ns_vmcb->spec_ctrl = n2vmcb->spec_ctrl & ~n1vmcb->spec_ctrl; +/* Nested IO permission bitmap */ /* Just keep the iopm_base_pa and msrpm_base_pa values. * The guest must not see the virtualized values.
@@ -1678,5 +1701,6 @@ void __init start_nested_svm(struct hvm_function_table *hvm_function_table) cpu_has_svm_lbrv && cpu_has_svm_nrips && cpu_has_svm_flushbyasid && - cpu_has_svm_decode; + cpu_has_svm_decode && + cpu_has_svm_spec_ctrl; }
Signature · 1 line
2.34.1
Le 20/10/2026 à 07:01, [email protected] a écrit :
Quoted 34 lines
From: Chunjie Zhu <[email protected]>
Previously L0 did not expose this bit, forcing L1 to intercept and emulate
MSR_SPEC_CTRLaccesses from L2 in software, which is functionally correct but suboptimal. Per APM vol.2 rev 3.44 "SPEC_CTRLVirtualization", hardware supports combining host and guestSPEC_CTRLvalues via VMCB.spec_ctrl with a logical OR while a guest runs, and saves the (possibly guest-modified) value back into VMCB.spec_ctrl on #VMEXIT.Require the SVM SpecCtrl feature before running any nested virt guests.
Update
docs/designs/nested-svm-cpu-features.mdwith the rationale for requiring and exposing bit 20 (SpecCtrl).Signed-off-by Chunjie Zhu <[email protected]>--- docs/designs/nested-svm-cpu-features.md | 13 +++++++++++++ xen/arch/x86/cpu-policy.c | 3 ++- xen/arch/x86/hvm/svm/nestedsvm.c | 26 ++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-)docs/designs/nested-svm-cpu-features.md +3
-0@@ -109,3 +109,16 @@ leaf 8000000A:edx Using it in L0 reduces the chance that we'll make some sort of error in the decode path. And if hardware supports it, it's easy enough to provide to the L1. + +- 20 `SpecCtrl` *MSR_SPEC_CTRL virtualisation*: Require for L0, provide to L1 +
docs/designs/nested-svm-cpu-features.md leaf 8000000A:edx · line 124
Quoted 10 lines
docs/designs/nested-svm-cpu-features.md +10
-0+ L0 already relies on this feature to virtualize MSR_SPEC_CTRL for the + L1 hypervisor itself (see `X86_FEATURE_SC_MSR_HVM`). Requiring it + as a baseline for nested virt (first available on Zen2) means L1 can + rely on the same hardware virtualisation for its own L2 guests, + instead of having L0 unconditionally intercept and trap/emulate + MSR_SPEC_CTRL accesses coming from L2. Providing it to L1 is + basically free once it's required of L0, and it removes a + significant amount of "CPU without the feature" fallback code that + would otherwise need to be implemented and maintained for nested + virt.
I'm not sure we actually need this requirement.
A comment regarding MSR_SPEC_CTRL virtualization suggest that MSR_SPEC_CTRL virtualization is as old as MSR_SPEC_CTRL itself
Quoted 3 lines
Virtualising
MSR_SPEC_CTRLfor guests depends on SVM support, which on real hardware matches the availability ofMSR_SPEC_CTRLin the first place.
So if there's no MSR_SPEC_CTRL, there is nothing special to do for nested virt.
What we can eventually say is that we can require MSR_SPEC_CTRL virtualization support iff we want host MSR_SPEC_CTRL.
Teddy
PATCH v2 1/1 x86/nestedsvm: Expose SpecCtrl to L1 when L0 virtualises MSR_SPEC_CTRL
From: Chunjie Zhu <[email protected]>
Previously L0 did not expose the SVM SpecCtrl bit, forcing L1 to intercept and emulate MSR_SPEC_CTRL accesses from L2 in software, which is functionally correct but suboptimal. Per APM vol.2 rev 3.44 "SPEC_CTRL Virtualization", hardware supports combining host and guest SPEC_CTRL values via VMCB.spec_ctrl with a logical OR while a guest runs, and saves the (possibly guest-modified) value back into VMCB.spec_ctrl on #VMEXIT.
Handle VMCB.spec_ctrl across nested VMRUN/#VMEXIT, and expose the SpecCtrl bit to L1 if L0 virtualises MSR_SPEC_CTRL for HVM guests (X86_FEATURE_SC_MSR_HVM, which on AMD implies hardware support for the bit). Otherwise IBRS is hidden from L1 and there is nothing to do, so SpecCtrl is not required for nested virt.
Update docs/designs/nested-svm-cpu-features.md with the rationale.
---
v2:
* Don't require SVM SpecCtrl for nested virt (Teddy): on AMD, X86_FEATURE_SC_MSR_HVM already implies hardware support, and without it IBRS is hidden from L1.
* Only expose SpecCtrl to L1 when X86_FEATURE_SC_MSR_HVM is set.
* Update the design doc and commit message accordingly.
docs/designs/nested-svm-cpu-features.md | 12 ++++++++++++ xen/arch/x86/cpu-policy.c | 10 +++++++++- xen/arch/x86/hvm/svm/nestedsvm.c | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-)
docs/designs/nested-svm-cpu-features.md +12 -0
@@ -114,3 +114,15 @@ leaf 8000000A:edx L0 uses the hardware virtual GIF to virtualize the GIF of both L1 and its L2, which removes the need to shadow it in software. + +- 20 `SpecCtrl` *MSR_SPEC_CTRL virtualisation*: Not required for L0, provide + to L1 if L0 virtualises MSR_SPEC_CTRL + + Without MSR_SPEC_CTRL virtualisation in L0 (hardware without + MSR_SPEC_CTRL, or `X86_FEATURE_SC_MSR_HVM` disabled), IBRS is hidden + from the L1 and there is nothing for nested virt to do, so the feature + is not required. When L0 does virtualise MSR_SPEC_CTRL (see + `X86_FEATURE_SC_MSR_HVM`, which implies hardware support for this + bit), provide it to L1 so that L1 can use the same hardware + virtualisation for its own L2 guests, instead of intercepting and + emulating MSR_SPEC_CTRL accesses from L2.
xen/arch/x86/cpu-policy.c +9 -1
@@ -845,9 +845,17 @@ static void __init calculate_hvm_max_policy(void) (1u <<SVM_FEATURE_NRIPS ) | (1u <<SVM_FEATURE_FLUSHBYASID ) | (1u <<SVM_FEATURE_PAUSEFILTER ) | - (1u << SVM_FEATURE_DECODEASSISTS)); + (1u <<SVM_FEATURE_DECODEASSISTS ) | + (1u << SVM_FEATURE_SPEC_CTRL));/* Enable features which are always emulated. */ p->extd.raw[0xa ].d |= (1u <<SVM_FEATURE_VMCBCLEAN ); + +/* +* SpecCtrl is only meaningful if Xen virtualises MSR_SPEC_CTRL for +* HVM guests, which implies hardware support. +*/ +if ( !boot_cpu_has (X86_FEATURE_SC_MSR_HVM ) ) + p->extd.raw[0xa ].d &= ~(1u <<SVM_FEATURE_SPEC_CTRL ); }guest_common_max_feature_adjustments (fs);
xen/arch/x86/hvm/svm/nestedsvm.c +23 -0
@@ -417,6 +417,16 @@ static int nsvm_vmcb_prepare4vmrun(struct vcpu *v, struct cpu_user_regs *regs)/* TSC offset */ n2vmcb->_tsc_offset = n1vmcb->_tsc_offset + ns_vmcb->_tsc_offset; +/* +* SPEC_CTRL: When in host mode, the host SPEC_CTRL value is in effect +* and writes update the host SPEC_CTRL register. On a VMRUN, the +* processor loads the guest SPEC_CTRL register value from the VMCB or +* VMSA. For most guests, processor behavior is controlled by the logical +* OR of the two registers. (APM vol.2 rev 3.44 +* "SPEC_CTRL Virtualization"). +*/ + n2vmcb->spec_ctrl = n1vmcb->spec_ctrl | ns_vmcb->spec_ctrl; +/* Nested IO permission bitmaps */ rc =nsvm_vmrun_permissionmap (v);if ( rc )
@@ -988,6 +998,7 @@ nsvm_vmcb_prepare4vmexit(struct vcpu *v, struct cpu_user_regs *regs)struct nestedvcpu *nv = &vcpu_nestedhvm (v);struct nestedsvm *svm = &vcpu_nestedsvm (v);struct vmcb_struct *ns_vmcb = nv->nv_vvmcx; +struct vmcb_struct *n1vmcb = nv->nv_n1vmcx;struct vmcb_struct *n2vmcb = nv->nv_n2vmcx;/* Cache guest physical address of virtual vmcb
@@ -999,6 +1010,18 @@ nsvm_vmcb_prepare4vmexit(struct vcpu *v, struct cpu_user_regs *regs)/* Pausefilter - keep it as is */ +/* +* SPEC_CTRL: on real hardware, only the guest's own SPEC_CTRL bits are +* ever saved back to the VMCB on #VMEXIT (APM vol.2 rev 3.44 "SPEC_CTRL +* Virtualization"); the OR with the host's bits is a transient +* execution-time effect, never written back into the guest register. +* Here, n2vmcb->spec_ctrl was seeded with n1vmcb's bits OR'd in at VMRUN +* time (see nsvm_vmcb_prepare4vmrun()), and will still contain them if L2 +* never wrote the MSR itself. Mask those back out so L1's own SPEC_CTRL +* doesn't leak into, and persist in, L2's saved state. +*/ + ns_vmcb->spec_ctrl = n2vmcb->spec_ctrl & ~n1vmcb->spec_ctrl; +/* Nested IO permission bitmap */ /* Just keep the iopm_base_pa and msrpm_base_pa values. * The guest must not see the virtualized values.
Signature · 1 line
2.34.1