KVM: VMX: Let userspace set IA32_FEAT_CTL at will
Sean Christopherson
maintainercover KVM: VMX: Let userspace set IA32_FEAT_CTL at will
Fix a bug where KVM requires userspace to set the guest CPUID model in order to enable features via IA32_FEAT_CTL.
Note, the selftest builds on the vmx_msrs_test from the VMX MSRs series[*], i.e. it won't apply cleanly on kvm/queue.
[*] https://lkml.kernel.org/r/[email protected]
Sean Christopherson (3):
KVM: VMX: Allow userspace to set all supported FEATURE_CONTROL bits
KVM: VMX: Move MSR_IA32_FEAT_CTL.LOCKED check into "is valid" helper
KVM: selftests: Verify userspace can stuff IA32_FEATURE_CONTROL at
will
arch/x86/kvm/vmx/vmx.c | 43 ++++++++++++++--- .../selftests/kvm/include/x86_64/processor.h | 2 + .../selftests/kvm/x86_64/vmx_msrs_test.c | 47 +++++++++++++++++++ 3 files changed, 85 insertions(+), 7 deletions(-)
base-commit: 838fd3e5a7208ee3b95bf73898c135a44a0c589d
Signature · 1 line
2.36.1.255.ge46751e96f-goog
Sean Christopherson
maintainerPATCH 1/3 KVM: VMX: Allow userspace to set all supported FEATURE_CONTROL bits
Allow userspace to set all supported bits in MSR IA32_FEATURE_CONTROL irrespective of the guest CPUID model, e.g. via KVM_SET_MSRS. KVM's ABI is that userspace is allowed to set MSRs before CPUID, i.e. can set MSRs to values that would fault according to the guest CPUID model.
--- arch/x86/kvm/vmx/vmx.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +31 -5
@@ -1741,12 +1741,38 @@ bool nested_vmx_allowed(struct kvm_vcpu *vcpu)return nested &&guest_cpuid_has (vcpu,X86_FEATURE_VMX ); } -static inline bool vmx_feature_control_msr_valid (struct kvm_vcpu *vcpu, -uint64_t val) +/* +* Userspace is allowed to set any supported IA32_FEATURE_CONTROL regardless of +* guest CPUID. Note, KVM allows userspace to set "VMX in SMX" to maintain +* backwards compatibility even though KVM doesn't support emulating SMX. And +* because userspace set "VMX in SMX", the guest must also be allowed to set it, +* e.g. if the MSR is left unlocked and the guest does a RMW operation. +*/ +#define KVM_SUPPORTED_FEATURE_CONTROL (FEAT_CTL_LOCKED | \ + FEAT_CTL_VMX_ENABLED_INSIDE_SMX | \ + FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX | \ + FEAT_CTL_SGX_LC_ENABLED | \ + FEAT_CTL_SGX_ENABLED | \ + FEAT_CTL_LMCE_ENABLED) + +static inline bool vmx_feature_control_msr_valid (struct vcpu_vmx *vmx, +struct msr_data *msr) { -uint64_t valid_bits =to_vmx (vcpu)->msr_ia32_feature_control_valid_bits; +uint64_t valid_bits; -return !(val & ~valid_bits); +/* +* Ensure KVM_SUPPORTED_FEATURE_CONTROL is updated when new bits are +* exposed to the guest. +*/ +WARN_ON_ONCE (vmx->msr_ia32_feature_control_valid_bits & + ~KVM_SUPPORTED_FEATURE_CONTROL ); + +if (msr->host_initiated) + valid_bits =KVM_SUPPORTED_FEATURE_CONTROL ; +else + valid_bits = vmx->msr_ia32_feature_control_valid_bits; + +return !(msr->data & ~valid_bits); }static int vmx_get_msr_feature (struct kvm_msr_entry *msr)
@@ -2139,7 +2165,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.mcg_ext_ctl = data;break ;case MSR_IA32_FEAT_CTL : -if (!vmx_feature_control_msr_valid (vcpu, data) || +if (!vmx_feature_control_msr_valid (vmx, msr_info) || (to_vmx (vcpu)->msr_ia32_feature_control &FEAT_CTL_LOCKED && !msr_info->host_initiated))return 1 ;
Signature · 1 line
2.36.1.255.ge46751e96f-goog
Sean Christopherson
maintainerPATCH 2/3 KVM: VMX: Move MSR_IA32_FEAT_CTL.LOCKED check into "is valid" helper
In reply to Sean ChristophersonMove the check on IA32_FEATURE_CONTROL being locked, i.e. read-only from the guest, into the helper to check the overall validity of the incoming value. Opportunistically rename the helper to make it clear that it returns a bool.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +8 -5
@@ -1755,8 +1755,8 @@ bool nested_vmx_allowed(struct kvm_vcpu *vcpu) FEAT_CTL_SGX_ENABLED | \ FEAT_CTL_LMCE_ENABLED) -static inline bool vmx_feature_control_msr_valid (struct vcpu_vmx *vmx, -struct msr_data *msr) +static inline bool is_vmx_feature_control_msr_valid (struct vcpu_vmx *vmx, +struct msr_data *msr) {uint64_t valid_bits;
@@ -1767,6 +1767,10 @@ static inline bool vmx_feature_control_msr_valid(struct vcpu_vmx *vmx,WARN_ON_ONCE (vmx->msr_ia32_feature_control_valid_bits & ~KVM_SUPPORTED_FEATURE_CONTROL ); +if (!msr->host_initiated && + (vmx->msr_ia32_feature_control &FEAT_CTL_LOCKED )) +return false; +if (msr->host_initiated) valid_bits =KVM_SUPPORTED_FEATURE_CONTROL ;else
@@ -2165,10 +2169,9 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.mcg_ext_ctl = data;break ;case MSR_IA32_FEAT_CTL : -if (!vmx_feature_control_msr_valid (vmx, msr_info) || - (to_vmx (vcpu)->msr_ia32_feature_control & -FEAT_CTL_LOCKED && !msr_info->host_initiated)) +if (!is_vmx_feature_control_msr_valid (vmx, msr_info))return 1 ; + vmx->msr_ia32_feature_control = data;if (msr_info->host_initiated && data ==0 )vmx_leave_nested (vcpu);
Signature · 1 line
2.36.1.255.ge46751e96f-goog
Sean Christopherson
maintainerPATCH 3/3 KVM: selftests: Verify userspace can stuff IA32_FEATURE_CONTROL at will
In reply to Sean ChristophersonVerify the KVM allows userspace to set all supported bits in the IA32_FEATURE_CONTROL MSR irrespective of the current guest CPUID, and that all unsupported bits are rejected.
Throw the testcase into vmx_msrs_test even though it's not technically a VMX MSR; it's close enough, and the most frequently feature controlled by the MSR is VMX.
--- .../selftests/kvm/include/x86_64/processor.h | 2 + .../selftests/kvm/x86_64/vmx_msrs_test.c | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+)
tools/testing/selftests/kvm/include/x86_64/processor.h +2 -0
@@ -101,6 +101,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_XMM2 KVM_X86_CPU_FEATURE(0x1, 0, EDX, 26)#define X86_FEATURE_FSGSBASE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 0)#define X86_FEATURE_TSC_ADJUST KVM_X86_CPU_FEATURE(0x7, 0, EBX, 1) +#define X86_FEATURE_SGX KVM_X86_CPU_FEATURE(0x7, 0, EBX, 2)#define X86_FEATURE_HLE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 4)#define X86_FEATURE_SMEP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 7)#define X86_FEATURE_INVPCID KVM_X86_CPU_FEATURE(0x7, 0, EBX, 10)
@@ -114,6 +115,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_PKU KVM_X86_CPU_FEATURE(0x7, 0, ECX, 3)#define X86_FEATURE_LA57 KVM_X86_CPU_FEATURE(0x7, 0, ECX, 16)#define X86_FEATURE_RDPID KVM_X86_CPU_FEATURE(0x7, 0, ECX, 22) +#define X86_FEATURE_SGX_LC KVM_X86_CPU_FEATURE(0x7, 0, ECX, 30)#define X86_FEATURE_SHSTK KVM_X86_CPU_FEATURE(0x7, 0, ECX, 7)#define X86_FEATURE_IBT KVM_X86_CPU_FEATURE(0x7, 0, EDX, 20)#define X86_FEATURE_AMX_TILE KVM_X86_CPU_FEATURE(0x7, 0, EDX, 24)
tools/testing/selftests/kvm/x86_64/vmx_msrs_test.c +47 -0
59 lines not on this page · raw message
Signature · 1 line
2.36.1.255.ge46751e96f-goog