KVM: nVMX: Fix another case where KVM overrides VMX MSRs
Sean Christopherson
maintainercover KVM: nVMX: Fix another case where KVM overrides VMX MSRs
Fix another case where KVM overrides the VMX MSRs (well, just the one MSR) during KVM_SET_CPUID. Similar to somewhat recent reverts
8805875aa473 ("Revert "KVM: nVMX: Do not expose MPX VMX controls when guest MPX disabled"") 9389d5774aca ("Revert "KVM: nVMX: Expose load IA32_PERF_GLOBAL_CTRL VM-{Entry,Exit} control"")
undo misguided KVM behavior where KVM overrides allowed-1 settings in the secondary execution controls in response to changes to the guest's CPUID model. To avoid breaking userspace that doesn't take ownership of the VMX MSRs, go hands off if and only if userpace sets the MSR in question
Sean Christopherson (2):
KVM: nVMX: Don't muck with allowed sec exec controls on CPUID changes
KVM: selftests: Test KVM's handling of VMX's sec exec MSR on
KVM_SET_CPUID
arch/x86/kvm/vmx/capabilities.h | 1 + arch/x86/kvm/vmx/nested.c | 3 + arch/x86/kvm/vmx/vmx.c | 2 +- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/include/x86_64/vmx.h | 4 +- .../selftests/kvm/x86_64/vmx_msrs_test.c | 92 +++++++++++++++++++ 6 files changed, 100 insertions(+), 3 deletions(-)
base-commit: d663b8a285986072428a6a145e5994bc275df994
Signature · 1 line
2.38.1.431.g37b22c650d-goog
Sean Christopherson
maintainerPATCH 1/2 KVM: nVMX: Don't muck with allowed sec exec controls on CPUID changes
Don't modify the set of allowed secondary execution controls, i.e. the virtual MSR_IA32_VMX_PROCBASED_CTLS2, in response to guest CPUID changes. To avoid breaking old userspace that never sets the VMX MSRs, i.e. relies on KVM to provide a consistent vCPU model, keep the existing behavior if userspace has never written MSR_IA32_VMX_PROCBASED_CTLS2.
KVM should not modify the VMX capabilities presented to L1 based on CPUID as doing so may discard explicit settings provided by userspace. E.g. if userspace does KVM_SET_MSRS => KVM_SET_CPUID and disables a feature in the VMX MSRs but not CPUID (to prevent exposing the feature to L2), then stuffing the VMX MSRs during KVM_SET_CPUID will expose the feature to L2 against userspace's wishes.
Alternatively, KVM could add a quirk, but that's less than ideal as a VMM that is affected by the bug would need to be updated in order to opt out of the buggy behavior. The "has the MSR ever been written" logic handles both the case where an enlightened userspace sets the MSR during setup, and the case where userspace blindly migrates the MSR, as the migrated value will already have been sanitized by the source KVM or explicitly set by the source VMM.
--- arch/x86/kvm/vmx/capabilities.h | 1 + arch/x86/kvm/vmx/nested.c | 3 +++ arch/x86/kvm/vmx/vmx.c | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/capabilities.h +1 -0
@@ -51,6 +51,7 @@ struct nested_vmx_msrs {u64 cr4_fixed1;u64 vmcs_enum;u64 vmfunc_controls; +bool secondary_set_by_userspace; };struct vmcs_config {
arch/x86/kvm/vmx/nested.c +3 -0
@@ -1256,6 +1256,9 @@ vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)if (!is_bitwise_subset (supported, data,GENMASK_ULL (63 ,32 )))return -EINVAL ; +if (msr_index ==MSR_IA32_VMX_PROCBASED_CTLS2 ) + vmx->nested.msrs.secondary_set_by_userspace = true; +vmx_get_control_msr (&vmx->nested.msrs, msr_index, &lowp, &highp); *lowp = data; *highp = data >>32 ;
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -4428,7 +4428,7 @@ vmx_adjust_secondary_exec_control(struct vcpu_vmx *vmx, u32 *exec_control,* Update the nested MSR settings so that a nested VMM can/can't set * controls for features that are/aren't exposed to the guest. */ -if (nested) { +if (nested && !vmx->nested.msrs.secondary_set_by_userspace) {if (enabled) vmx->nested.msrs.secondary_ctls_high |= control;else
Signature · 1 line
2.38.1.431.g37b22c650d-goog
Sean Christopherson
maintainerPATCH 2/2 KVM: selftests: Test KVM's handling of VMX's sec exec MSR on KVM_SET_CPUID
In reply to Sean ChristophersonVerify that KVM does, and does not, modify the allowed set of VMX's secondary execution controls during KVM_SET_CPUID. Historically, KVM has modified select bits in response to guest CPUID changes to try and force a consistent CPU model. KVM's meddling causes problems if userspace invokes KVM_SET_CPUID after explicitly setting the MSR, as KVM may end up overriding a legal userspace config.
Newer, fixed KVM versions maintain the historical meddling for backwards compatibility, but only if userspace has never set the MSR for the vCPU. I.e. KVM transfers ownership to userspace on the first write.
Opportunistically fix some funky names in tools' definitions for a few secondary execution controls.
--- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/include/x86_64/vmx.h | 4 +- .../selftests/kvm/x86_64/vmx_msrs_test.c | 92 +++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -101,6 +101,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_INVPCID KVM_X86_CPU_FEATURE(0x7, 0, EBX, 10)#define X86_FEATURE_RTM KVM_X86_CPU_FEATURE(0x7, 0, EBX, 11)#define X86_FEATURE_MPX KVM_X86_CPU_FEATURE(0x7, 0, EBX, 14) +#define X86_FEATURE_RDSEED KVM_X86_CPU_FEATURE(0x7, 0, EBX, 18)#define X86_FEATURE_SMAP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 20)#define X86_FEATURE_PCOMMIT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 22)#define X86_FEATURE_CLFLUSHOPT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 23)
tools/testing/selftests/kvm/include/x86_64/vmx.h +2 -2
@@ -61,8 +61,8 @@#define SECONDARY_EXEC_SHADOW_VMCS 0x00004000#define SECONDARY_EXEC_RDSEED_EXITING 0x00010000#define SECONDARY_EXEC_ENABLE_PML 0x00020000 -#define SECONDARY_EPT_VE 0x00040000 -#define SECONDARY_ENABLE_XSAV_RESTORE 0x00100000 +#define SECONDARY_EXEC_EPT_VE 0x00040000 +#define SECONDARY_EXEC_ENABLE_XSAVES 0x00100000#define SECONDARY_EXEC_TSC_SCALING 0x02000000#define PIN_BASED_EXT_INTR_MASK 0x00000001
tools/testing/selftests/kvm/x86_64/vmx_msrs_test.c +92 -0
104 lines not on this page · raw message
Signature · 1 line
2.38.1.431.g37b22c650d-goog