KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup
cover KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup
Two bug fixes to handle KVM_SET_SREGS without a preceding KVM_SET_CPUID2.
The overarching issue is that kvm_x86_ops.set_cr4() can fail, but its invocation from __set_sregs(), a.k.a. KVM_SET_SREGS, ignores the result. Fix the issue by moving all validity checks out of .set_cr4() in one way or another.
I intentionally omitted a Cc to stable. The first bug fix in particular may break stable trees as it simply removes a check, and I don't know that stable trees have the generic CR4 reserved bit check that is needed to prevent the guest from setting VMXE when nVMX is not allowed.
Sean Christopherson (6):
KVM: VMX: Drop guest CPUID check for VMXE in vmx_set_cr4()
KVM: VMX: Drop explicit 'nested' check from vmx_set_cr4()
KVM: SVM: Drop VMXE check from svm_set_cr4()
KVM: x86: Move vendor CR4 validity check to dedicated kvm_x86_ops hook
KVM: x86: Return bool instead of int for CR4 and SREGS validity checks
KVM: selftests: Verify supported CR4 bits can be set before
KVM_SET_CPUID2
arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/svm/svm.c | 12 ++- arch/x86/kvm/svm/svm.h | 2 +- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 35 +++---- arch/x86/kvm/vmx/vmx.h | 2 +- arch/x86/kvm/x86.c | 28 +++--- arch/x86/kvm/x86.h | 2 +- .../selftests/kvm/include/x86_64/processor.h | 17 ++++ .../selftests/kvm/include/x86_64/vmx.h | 4 - .../selftests/kvm/x86_64/set_sregs_test.c | 92 ++++++++++++++++++- 12 files changed, 153 insertions(+), 48 deletions(-)
Signature · 1 line
2.28.0
PATCH 1/6 KVM: VMX: Drop guest CPUID check for VMXE in vmx_set_cr4()
Drop vmx_set_cr4()'s somewhat hidden guest_cpuid_has() check on VMXE now that common x86 handles the check by incorporating VMXE into the CR4 reserved bits, i.e. in cr4_guest_rsvd_bits. This fixes a bug where KVM incorrectly rejects KVM_SET_SREGS with CR4.VMXE=1 if it's executed before KVM_SET_CPUID{,2}.
--- arch/x86/kvm/vmx/vmx.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +3 -2
@@ -3110,9 +3110,10 @@ int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)* must first be able to turn on cr4.VMXE (see handle_vmon()). * So basically the check on whether to allow nested VMX * is here. We operate under the default treatment of SMM, -* so VMX cannot be enabled under SMM. +* so VMX cannot be enabled under SMM. Note, guest CPUID is +* intentionally ignored, it's handled by cr4_guest_rsvd_bits. */ -if (!nested_vmx_allowed (vcpu) ||is_smm (vcpu)) +if (!nested ||is_smm (vcpu))return 1 ; }
Signature · 1 line
2.28.0
PATCH 2/6 KVM: VMX: Drop explicit 'nested' check from vmx_set_cr4()
In reply to Sean ChristophersonDrop vmx_set_cr4()'s explicit check on the 'nested' module param now that common x86 handles the check by incorporating VMXE into the CR4 reserved bits, via kvm_cpu_caps. X86_FEATURE_VMX is set in kvm_cpu_caps (by vmx_set_cpu_caps()), if and only if 'nested' is true.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-)
arch/x86/kvm/vmx/vmx.c +7 -12
@@ -3104,18 +3104,13 @@ int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) } } -if (cr4 &X86_CR4_VMXE ) { -/* -* To use VMXON (and later other VMX instructions), a guest -* must first be able to turn on cr4.VMXE (see handle_vmon()). -* So basically the check on whether to allow nested VMX -* is here. We operate under the default treatment of SMM, -* so VMX cannot be enabled under SMM. Note, guest CPUID is -* intentionally ignored, it's handled by cr4_guest_rsvd_bits. -*/ -if (!nested ||is_smm (vcpu)) -return 1 ; - } +/* +* We operate under the default treatment of SMM, so VMX cannot be +* enabled under SMM. Note, whether or not VMXE is allowed at all is +* handled by kvm_valid_cr4(). +*/ +if ((cr4 &X86_CR4_VMXE ) &&is_smm (vcpu)) +return 1 ;if (vmx->nested.vmxon && !nested_cr4_valid (vcpu, cr4))return 1 ;
Signature · 1 line
2.28.0
PATCH 3/6 KVM: SVM: Drop VMXE check from svm_set_cr4()
In reply to Sean ChristophersonDrop svm_set_cr4()'s explicit check CR4.VMXE now that common x86 handles the check by incorporating VMXE into the CR4 reserved bits, via kvm_cpu_caps. SVM obviously does not set X86_FEATURE_VMX.
No functional change intended.
--- arch/x86/kvm/svm/svm.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/kvm/svm/svm.c +0 -3
@@ -1684,9 +1684,6 @@ int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)unsigned long host_cr4_mce =cr4_read_shadow () &X86_CR4_MCE ;unsigned long old_cr4 =to_svm (vcpu)->vmcb->save.cr4; -if (cr4 &X86_CR4_VMXE ) -return 1 ; -if (npt_enabled && ((old_cr4 ^ cr4) &X86_CR4_PGE ))svm_flush_tlb (vcpu);
Signature · 1 line
2.28.0
PATCH 4/6 KVM: x86: Move vendor CR4 validity check to dedicated kvm_x86_ops hook
In reply to Sean ChristophersonSplit out VMX's checks on CR4.VMXE to a dedicated hook, .is_valid_cr4(), and invoke the new hook from kvm_valid_cr4(). This fixes an issue where KVM_SET_SREGS would return success while failing to actually set CR4.
Fixing the issue by explicitly checking kvm_x86_ops.set_cr4()'s return in __set_sregs() is not a viable option as KVM has already stuffed a variety of vCPU state.
Note, kvm_valid_cr4() and is_valid_cr4() have different return types and inverted semantics. This will be remedied in a future patch.
--- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/svm/svm.c | 9 +++++++-- arch/x86/kvm/svm/svm.h | 2 +- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 31 ++++++++++++++++++------------- arch/x86/kvm/vmx/vmx.h | 2 +- arch/x86/kvm/x86.c | 6 ++++-- 7 files changed, 34 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -1085,7 +1085,8 @@ struct kvm_x86_ops { struct kvm_segment *var, int seg);void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu,int *db,int *l);void (*set_cr0)(struct kvm_vcpu *vcpu,unsigned long cr0); -int (*set_cr4)(struct kvm_vcpu *vcpu,unsigned long cr4); +bool (*is_valid_cr4)(struct kvm_vcpu *vcpu,unsigned long cr0); +void (*set_cr4)(struct kvm_vcpu *vcpu,unsigned long cr4);void (*set_efer)(struct kvm_vcpu *vcpu,u64 efer);void (*get_idt)(struct kvm_vcpu *vcpu,struct desc_ptr *dt);void (*set_idt)(struct kvm_vcpu *vcpu,struct desc_ptr *dt);
arch/x86/kvm/svm/svm.c +7 -2
@@ -1679,7 +1679,12 @@ void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)update_cr0_intercept (svm); } -int svm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) +static bool svm_is_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) +{ +return true; +} + +void svm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {unsigned long host_cr4_mce =cr4_read_shadow () &X86_CR4_MCE ;unsigned long old_cr4 =to_svm (vcpu)->vmcb->save.cr4;
@@ -1693,7 +1698,6 @@ int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) cr4 |= host_cr4_mce;to_svm (vcpu)->vmcb->save.cr4 = cr4;vmcb_mark_dirty (to_svm (vcpu)->vmcb,VMCB_CR ); -return 0 ; } static void svm_set_segment(struct kvm_vcpu *vcpu,
@@ -4192,6 +4196,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .get_cpl = svm_get_cpl, .get_cs_db_l_bits = kvm_get_cs_db_l_bits, .set_cr0 = svm_set_cr0, + .is_valid_cr4 = svm_is_valid_cr4, .set_cr4 = svm_set_cr4, .set_efer = svm_set_efer, .get_idt = svm_get_idt,
arch/x86/kvm/svm/svm.h +1 -1
@@ -352,7 +352,7 @@ static inline bool gif_set(struct vcpu_svm *svm)u32 svm_msrpm_offset (u32 msr);void svm_set_efer (struct kvm_vcpu *vcpu,u64 efer);void svm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0); -int svm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4); +void svm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4);void svm_flush_tlb (struct kvm_vcpu *vcpu);void disable_nmi_singlestep (struct vcpu_svm *svm);bool svm_smi_blocked (struct kvm_vcpu *vcpu);
arch/x86/kvm/vmx/nested.c +1 -1
@@ -4814,7 +4814,7 @@ static int handle_vmon(struct kvm_vcpu *vcpu)/* * The Intel VMX Instruction Reference lists a bunch of bits that are * prerequisite to running VMXON, most notably cr4.VMXE must be set to -* 1 (see vmx_set_cr4() for when we allow the guest to set this). +* 1 (see vmx_is_valid_cr4() for when we allow the guest to set this). * Otherwise, we should fail with #UD. But most faulting conditions * have already been checked by hardware, prior to the VM-exit for * VMXON. We do test guest cr4.VMXE because processor CR4 always has
arch/x86/kvm/vmx/vmx.c +18 -13
@@ -3076,7 +3076,23 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,vmcs_writel (GUEST_CR3 , guest_cr3); } -int vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) +static bool vmx_is_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) +{ +/* +* We operate under the default treatment of SMM, so VMX cannot be +* enabled under SMM. Note, whether or not VMXE is allowed at all is +* handled by kvm_valid_cr4(). +*/ +if ((cr4 &X86_CR4_VMXE ) &&is_smm (vcpu)) +return false; + +if (to_vmx (vcpu)->nested.vmxon && !nested_cr4_valid (vcpu, cr4)) +return false; + +return true; +} + +void vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {struct vcpu_vmx *vmx =to_vmx (vcpu);/*
@@ -3104,17 +3120,6 @@ int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) } } -/* -* We operate under the default treatment of SMM, so VMX cannot be -* enabled under SMM. Note, whether or not VMXE is allowed at all is -* handled by kvm_valid_cr4(). -*/ -if ((cr4 &X86_CR4_VMXE ) &&is_smm (vcpu)) -return 1 ; - -if (vmx->nested.vmxon && !nested_cr4_valid (vcpu, cr4)) -return 1 ; - vcpu->arch.cr4 = cr4;kvm_register_mark_available (vcpu,VCPU_EXREG_CR4 );
@@ -3145,7 +3150,6 @@ int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)vmcs_writel (CR4_READ_SHADOW , cr4);vmcs_writel (GUEST_CR4 , hw_cr4); -return 0 ; }void vmx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg)
@@ -7597,6 +7601,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .get_cpl = vmx_get_cpl, .get_cs_db_l_bits = vmx_get_cs_db_l_bits, .set_cr0 = vmx_set_cr0, + .is_valid_cr4 = vmx_is_valid_cr4, .set_cr4 = vmx_set_cr4, .set_efer = vmx_set_efer, .get_idt = vmx_get_idt,
arch/x86/kvm/vmx/vmx.h +1 -1
@@ -321,7 +321,7 @@ u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu);void vmx_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask);void vmx_set_efer (struct kvm_vcpu *vcpu,u64 efer);void vmx_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0); -int vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4); +void vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4);void set_cr4_guest_host_mask (struct vcpu_vmx *vmx);void ept_save_pdptrs (struct kvm_vcpu *vcpu);void vmx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);
arch/x86/kvm/x86.c +4 -2
@@ -973,6 +973,9 @@ int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)if (cr4 & vcpu->arch.cr4_guest_rsvd_bits)return -EINVAL ; +if (!kvm_x86_ops.is_valid_cr4 (vcpu, cr4)) +return -EINVAL ; +return 0 ; }EXPORT_SYMBOL_GPL (kvm_valid_cr4);
@@ -1006,8 +1009,7 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ; } -if (kvm_x86_ops.set_cr4 (vcpu, cr4)) -return 1 ; + kvm_x86_ops.set_cr4 (vcpu, cr4);if (((cr4 ^ old_cr4) & pdptr_bits) || (!(cr4 &X86_CR4_PCIDE ) && (old_cr4 &X86_CR4_PCIDE )))
Signature · 1 line
2.28.0
PATCH 5/6 KVM: x86: Return bool instead of int for CR4 and SREGS validity checks
In reply to Sean ChristophersonRework the common CR4 and SREGS checks to return a bool instead of an int, i.e. true/false instead of 0/-EINVAL, and add "is" to the name to clarify the polarity of the return value (which is effectively inverted by this change).
No functional changed intended.
--- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/x86.c | 28 ++++++++++++---------------- arch/x86/kvm/x86.h | 2 +- 4 files changed, 15 insertions(+), 19 deletions(-)
arch/x86/kvm/svm/nested.c +1 -1
@@ -254,7 +254,7 @@ static bool nested_vmcb_checks(struct vcpu_svm *svm, struct vmcb *vmcb12) (vmcb12->save.cr3 & MSR_CR3_LONG_MBZ_MASK))return false; } -if (kvm_valid_cr4 (&svm->vcpu, vmcb12->save.cr4)) +if (!kvm_is_valid_cr4 (&svm->vcpu, vmcb12->save.cr4))return false;return nested_vmcb_check_controls (&vmcb12->control);
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -3081,7 +3081,7 @@ static bool vmx_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)/* * We operate under the default treatment of SMM, so VMX cannot be * enabled under SMM. Note, whether or not VMXE is allowed at all is -* handled by kvm_valid_cr4(). +* handled by kvm_is_valid_cr4(). */ if ((cr4 &X86_CR4_VMXE ) &&is_smm (vcpu))return false;
arch/x86/kvm/x86.c +12 -16
@@ -965,20 +965,17 @@ int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) }EXPORT_SYMBOL_GPL (kvm_set_xcr); -int kvm_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) +bool kvm_is_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {if (cr4 & cr4_reserved_bits) -return -EINVAL ; +return false;if (cr4 & vcpu->arch.cr4_guest_rsvd_bits) -return -EINVAL ; +return false; -if (!kvm_x86_ops.is_valid_cr4 (vcpu, cr4)) -return -EINVAL ; - -return 0 ; +return kvm_x86_ops.is_valid_cr4 (vcpu, cr4); } -EXPORT_SYMBOL_GPL (kvm_valid_cr4); +EXPORT_SYMBOL_GPL (kvm_is_valid_cr4);int kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {
@@ -986,7 +983,7 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)unsigned long pdptr_bits =X86_CR4_PGE |X86_CR4_PSE |X86_CR4_PAE |X86_CR4_SMEP ; -if (kvm_valid_cr4 (vcpu, cr4)) +if (!kvm_is_valid_cr4 (vcpu, cr4))return 1 ;if (is_long_mode (vcpu)) {
@@ -9422,7 +9419,7 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index, }EXPORT_SYMBOL_GPL (kvm_task_switch); -static int kvm_valid_sregs (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs) +static bool kvm_is_valid_sregs (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs) {if ((sregs->efer &EFER_LME ) && (sregs->cr0 &X86_CR0_PG )) {/*
@@ -9430,19 +9427,18 @@ static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)* 64-bit mode (though maybe in a 32-bit code segment). * CR4.PAE and EFER.LMA must be set. */ -if (!(sregs->cr4 &X86_CR4_PAE ) - || !(sregs->efer &EFER_LMA )) -return -EINVAL ; +if (!(sregs->cr4 &X86_CR4_PAE ) || !(sregs->efer &EFER_LMA )) +return false; }else {/* * Not in 64-bit mode: EFER.LMA is clear and the code * segment cannot be 64-bit. */ if (sregs->efer &EFER_LMA || sregs->cs.l) -return -EINVAL ; +return false; } -return kvm_valid_cr4 (vcpu, sregs->cr4); +return kvm_is_valid_cr4 (vcpu, sregs->cr4); }static int __set_sregs (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs)
@@ -9454,7 +9450,7 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)struct desc_ptr dt;int ret = -EINVAL ; -if (kvm_valid_sregs (vcpu, sregs)) +if (!kvm_is_valid_sregs (vcpu, sregs)) goto out; apic_base_msr.data = sregs->apic_base;
arch/x86/kvm/x86.h +1 -1
@@ -369,7 +369,7 @@ static inline bool kvm_dr6_valid(u64 data)void kvm_load_guest_xsave_state (struct kvm_vcpu *vcpu);void kvm_load_host_xsave_state (struct kvm_vcpu *vcpu);int kvm_spec_ctrl_test_value (u64 value); -int kvm_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4); +bool kvm_is_valid_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4);bool kvm_vcpu_exit_request (struct kvm_vcpu *vcpu);int kvm_handle_memory_failure (struct kvm_vcpu *vcpu,int r,struct x86_exception *e);
Signature · 1 line
2.28.0
PATCH 6/6 KVM: selftests: Verify supported CR4 bits can be set before KVM_SET_CPUID2
In reply to Sean ChristophersonExtend the KVM_SET_SREGS test to verify that all supported CR4 bits, as enumerated by KVM, can be set before KVM_SET_CPUID2, i.e. without first defining the vCPU model. KVM is supposed to skip guest CPUID checks when host userspace is stuffing guest state.
Check the inverse as well, i.e. that KVM rejects KVM_SET_REGS if CR4 has one or more unsupported bits set.
--- .../selftests/kvm/include/x86_64/processor.h | 17 ++++ .../selftests/kvm/include/x86_64/vmx.h | 4 - .../selftests/kvm/x86_64/set_sregs_test.c | 92 ++++++++++++++++++- 3 files changed, 108 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +17 -0
@@ -27,6 +27,7 @@#define X86_CR4_OSFXSR (1ul << 9)#define X86_CR4_OSXMMEXCPT (1ul << 10)#define X86_CR4_UMIP (1ul << 11) +#define X86_CR4_LA57 (1ul << 12)#define X86_CR4_VMXE (1ul << 13)#define X86_CR4_SMXE (1ul << 14)#define X86_CR4_FSGSBASE (1ul << 16)
@@ -36,6 +37,22 @@#define X86_CR4_SMAP (1ul << 21)#define X86_CR4_PKE (1ul << 22) +/* CPUID.1.ECX */ +#define CPUID_VMX (1ul << 5) +#define CPUID_SMX (1ul << 6) +#define CPUID_PCID (1ul << 17) +#define CPUID_XSAVE (1ul << 26) + +/* CPUID.7.EBX */ +#define CPUID_FSGSBASE (1ul << 0) +#define CPUID_SMEP (1ul << 7) +#define CPUID_SMAP (1ul << 20) + +/* CPUID.7.ECX */ +#define CPUID_UMIP (1ul << 2) +#define CPUID_PKU (1ul << 3) +#define CPUID_LA57 (1ul << 16) +/* General Registers in 64-Bit Mode */ struct gpr64_regs {u64 rax;
tools/testing/selftests/kvm/include/x86_64/vmx.h +0 -4
@@ -11,10 +11,6 @@#include <stdint.h> #include "processor.h" -#define CPUID_VMX_BIT 5 - -#define CPUID_VMX (1 << 5) -/* * Definitions of Primary Processor-Based VM-Execution Controls. */
tools/testing/selftests/kvm/x86_64/set_sregs_test.c +91 -1
107 lines not on this page · raw message
Signature · 1 line
2.28.0
07.10.2020 04:44, Sean Christopherson пишет:
Quoted 1 line
Two bug fixes to handle
KVM_SET_SREGSwithout a precedingKVM_SET_CPUID2.
Hi Sean & KVM devs.
I tested the patches, and wherever I set VMXE in CR4, I now get
KVM: KVM_SET_SREGS: Invalid argument Before the patch I was able (with many problems, but still) to set VMXE sometimes.
So its a NAK so far, waiting for an update. :)
Quoted 11 lines · stsp
07.10.2020 04:44, Sean Christopherson пишет:
Quoted 1 line
Two bug fixes to handle
KVM_SET_SREGSwithout a precedingKVM_SET_CPUID2.Hi Sean & KVM devs.
I tested the patches, and wherever I set VMXE in CR4, I now get
KVM:KVM_SET_SREGS: Invalid argument Before the patch I was able (with many problems, but still) to set VMXE sometimes.So its a NAK so far, waiting for an update. :)
IIRC, you said you were going to test on AMD? Assuming that's correct, -EINVAL is the expected behavior. KVM was essentially lying before; it never actually set CR4.VMXE in hardware, it just didn't properply detect the error and so VMXE was set in KVM's shadow of the guest's CR4.
08.10.2020 20:59, Sean Christopherson пишет:
Quoted 5 lines · stsp
Quoted 4 lines
07.10.2020 04:44, Sean Christopherson пишет:
Quoted 1 line
Two bug fixes to handle
KVM_SET_SREGSwithout a precedingKVM_SET_CPUID2.Hi Sean & KVM devs.
Quoted 8 lines
Quoted 7 lines
I tested the patches, and wherever I set VMXE in CR4, I now get
KVM:KVM_SET_SREGS: Invalid argument Before the patch I was able (with many problems, but still) to set VMXE sometimes.So its a NAK so far, waiting for an update. :)
IIRC, you said you were going to test on AMD? Assuming that's correct,
Yes, that is true.
Quoted 4 lines
-EINVAL
is the expected behavior. KVM was essentially lying before; it never actually set CR4.VMXE in hardware, it just didn't properply detect the error and so VMXE was set in KVM's shadow of the guest's CR4.
Hmm. But at least it was lying similarly on AMD and Intel CPUs. :) So I was able to reproduce the problems myself.
Do you mean, any AMD tests are now useless, and we need to proceed with Intel tests only?
Then additional question.
On old Intel CPUs we needed to set VMXE in guest to make it to work in nested-guest mode.
Is it still needed even with your patches? Or the nested-guest mode will work now even on older Intel CPUs and KVM will set VMXE for us itself, when needed?
Quoted 23 lines · stsp
08.10.2020 20:59, Sean Christopherson пишет:
Quoted 5 lines · stsp
Quoted 4 lines
07.10.2020 04:44, Sean Christopherson пишет:
Quoted 1 line
Two bug fixes to handle
KVM_SET_SREGSwithout a precedingKVM_SET_CPUID2.Hi Sean & KVM devs.
Quoted 8 lines
Quoted 7 lines
I tested the patches, and wherever I set VMXE in CR4, I now get
KVM:KVM_SET_SREGS: Invalid argument Before the patch I was able (with many problems, but still) to set VMXE sometimes.So its a NAK so far, waiting for an update. :)
IIRC, you said you were going to test on AMD? Assuming that's correct,
Yes, that is true.
Quoted 4 lines
-EINVAL
is the expected behavior. KVM was essentially lying before; it never actually set CR4.VMXE in hardware, it just didn't properply detect the error and so VMXE was set in KVM's shadow of the guest's CR4.
Quoted 6 lines
Hmm. But at least it was lying
similarly on AMD and Intel CPUs. :)
So I was able to reproduce the problems
myself.
Do you mean, any AMD tests are now useless, and we need to proceed with Intel tests only?
For anything VMXE related, yes.
Quoted 6 lines
Then additional question.
On old Intel CPUs we needed to set VMXE in guest to make it to work in nested-guest mode.
Is it still needed even with your patches?
Or the nested-guest mode will work now even on older Intel CPUs and KVM will set VMXE for us itself, when needed?
I'm struggling to even come up with a theory as to how setting VMXE from userspace would have impacted KVM with unrestricted_guest=n, let alone fixed anything.
CR4.VMXE must always be 1 in _hardware_ when VMX is on, including when running the guest. But KVM forces vmcs.GUEST_CR4.VMXE=1 at all times, regardless of the guest's actual value (the guest sees a shadow value when it reads CR4).
And unless I grossly misunderstand dosemu2, it's not doing anything related to nested virtualization, i.e. the stuffing VMXE=1 for the guest's shadow value should have absolutely zero impact.
More than likely, VMXE was a red herring. Given that the reporter is also seeing the same bug on bare metal after moving to kernel 5.4, odds are good the issue is related to unrestricted_guest=n and has nothing to do with nVMX.
09.10.2020 07:04, Sean Christopherson пишет:
Quoted 7 lines
Quoted 6 lines
Hmm. But at least it was lying
similarly on AMD and Intel CPUs. :)
So I was able to reproduce the problems
myself.
Do you mean, any AMD tests are now useless, and we need to proceed with Intel tests only?For anything VMXE related, yes.
What would be the expected behaviour
on Intel, if it is set? Any difference with AMD?
Quoted 14 lines
Quoted 6 lines
Then additional question.
On old Intel CPUs we needed to set VMXE in guest to make it to work in nested-guest mode.
Is it still needed even with your patches?
Or the nested-guest mode will work now even on older Intel CPUs and KVM will set VMXE for us itself, when needed?I'm struggling to even come up with a theory as to how setting VMXE from userspace would have impacted KVM with unrestricted_guest=n, let alone fixed anything.
CR4.VMXE must always be 1 in _hardware_ when VMX is on, including when running the guest. But KVM forces vmcs.GUEST_CR4.VMXE=1 at all times, regardless of the guest's actual value (the guest sees a shadow value when it reads CR4).
Quoted 5 lines
And unless I grossly misunderstand dosemu2, it's not doing anything related to nested virtualization, i.e. the stuffing VMXE=1 for the guest's shadow value should have absolutely zero impact.
More than likely, VMXE was a red herring.
Yes, it was. :(
(as you can see from the end of the github thread)
Quoted 3 lines
Given that the reporter is also
seeing the same bug on bare metal after moving to kernel 5.4, odds are good the issue is related to unrestricted_guest=n and has nothing to do with nVMX.
But we do not use unrestricted guest. We use v86 under KVM.
The only other effect of setting VMXE was clearing VME. Which shouldn't affect anything either, right?
Quoted 11 lines · stsp
09.10.2020 07:04, Sean Christopherson пишет:
Quoted 7 lines
Quoted 6 lines
Hmm. But at least it was lying
similarly on AMD and Intel CPUs. :)
So I was able to reproduce the problems
myself.
Do you mean, any AMD tests are now useless, and we need to proceed with Intel tests only?For anything VMXE related, yes.
What would be the expected behaviour on Intel, if it is set? Any difference with AMD?
On Intel, userspace should be able to stuff CR4.VMXE=1 via KVM_SET_SREGS if the 'nested' module param is 1, e.g. if 'modprobe kvm_intel nested=1'. Note, 'nested' is enabled by default on kernel 5.0 and later.
With AMD, setting CR4.VMXE=1 is never allowed as AMD doesn't support VMX, AMD's virtualization solution is called SVM (Secure Virtual Machine). KVM doesn't support nesting VMX within SVM and vice versa.
Quoted 23 lines
Quoted 14 lines
Quoted 6 lines
Then additional question.
On old Intel CPUs we needed to set VMXE in guest to make it to work in nested-guest mode.
Is it still needed even with your patches?
Or the nested-guest mode will work now even on older Intel CPUs and KVM will set VMXE for us itself, when needed?I'm struggling to even come up with a theory as to how setting VMXE from userspace would have impacted KVM with unrestricted_guest=n, let alone fixed anything.
CR4.VMXE must always be 1 in _hardware_ when VMX is on, including when running the guest. But KVM forces vmcs.GUEST_CR4.VMXE=1 at all times, regardless of the guest's actual value (the guest sees a shadow value when it reads CR4).
Quoted 5 lines
And unless I grossly misunderstand dosemu2, it's not doing anything related to nested virtualization, i.e. the stuffing VMXE=1 for the guest's shadow value should have absolutely zero impact.
More than likely, VMXE was a red herring.
Yes, it was. :( (as you can see from the end of the github thread)
Quoted 6 lines
Quoted 3 lines
Given that the reporter is also
seeing the same bug on bare metal after moving to kernel 5.4, odds are good the issue is related to unrestricted_guest=n and has nothing to do with nVMX.But we do not use unrestricted guest.
We use v86 under KVM.
Unrestricted guest can kick in even if CR0.PG=1 && CR0.PE=1, e.g. there are segmentation checks that apply if and only if unrestricted_guest=0. Long story short, without a deep audit, it's basically impossible to rule out a dependency on unrestricted guest since you're playing around with v86.
Quoted 2 lines
The only other effect of setting VMXE was clearing VME. Which shouldn't affect anything either, right?
Hmm, clearing VME would mean that exceptions/interrupts within the guest would trigger a switch out of v86 and into vanilla protected mode. v86 and PM have different consistency checks, particularly for segmentation, so it's plausible that clearing CR4.VME inadvertantly worked around the bug by avoiding invalid guest state for v86.
stsp
Tested-by Stas Sergeev09.10.2020 18:30, Sean Christopherson пишет:
Quoted 8 lines · stsp
Quoted 7 lines
09.10.2020 07:04, Sean Christopherson пишет:
Quoted 6 lines
Quoted 6 lines
Hmm. But at least it was lying
similarly on AMD and Intel CPUs. :)
So I was able to reproduce the problems
myself.
Do you mean, any AMD tests are now useless, and we need to proceed with Intel tests only?
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
For anything VMXE related, yes.
What would be the expected behaviour on Intel, if it is set? Any difference with AMD?
On Intel, userspace should be able to stuff CR4.VMXE=1 via
KVM_SET_SREGSif the 'nested' module param is 1, e.g. if 'modprobekvm_intelnested=1'. Note, 'nested' is enabled by default on kernel 5.0 and later.
So if I understand you correctly, we need to test that:
- with nested=0 VMXE gives EINVAL
- with nested=1 VMXE changes nothing visible, except probably to allow guest to read that value (we won't test guest reading though).
Is this correct?
Quoted 1 line
With AMD, setting CR4.VMXE=1 is never allowed as AMD doesn't support VMX,
OK, for that I can give you a
because I confirm that on AMD it now consistently returns EINVAL, whereas without your patches it did random crap, depending on whether it is a first call to KVM_SET_SREGS, or not first.
Quoted 6 lines
Quoted 2 lines
But we do not use unrestricted guest.
We use v86 under KVM.Unrestricted guest can kick in even if CR0.PG=1 && CR0.PE=1, e.g. there are segmentation checks that apply if and only if unrestricted_guest=0. Long story short, without a deep audit, it's basically impossible to rule out a dependency on unrestricted guest since you're playing around with v86.
You mean "unrestricted_guest" as a module parameter, rather than the similar named CPU feature, right? So we may depend on unrestricted_guest parameter, but not on a hardware feature, correct?
Quoted 7 lines
Quoted 2 lines
The only other effect of setting VMXE was clearing VME. Which shouldn't affect anything either, right?
Hmm, clearing VME would mean that exceptions/interrupts within the guest would trigger a switch out of v86 and into vanilla protected mode. v86 and PM have different consistency checks, particularly for segmentation, so it's plausible that clearing CR4.VME inadvertantly worked around the bug by avoiding invalid guest state for v86.
Lets assume that was the case.
With those github guys its not possible to do any consistent checks. :(
Quoted 24 lines · stsp
09.10.2020 18:30, Sean Christopherson пишет:
Quoted 8 lines · stsp
Quoted 7 lines
09.10.2020 07:04, Sean Christopherson пишет:
Quoted 6 lines
Quoted 6 lines
Hmm. But at least it was lying
similarly on AMD and Intel CPUs. :)
So I was able to reproduce the problems
myself.
Do you mean, any AMD tests are now useless, and we need to proceed with Intel tests only?Quoted 6 lines
Quoted 3 lines
Quoted 1 line
For anything VMXE related, yes.
What would be the expected behaviour on Intel, if it is set? Any difference with AMD?
On Intel, userspace should be able to stuff CR4.VMXE=1 via
KVM_SET_SREGSif the 'nested' module param is 1, e.g. if 'modprobekvm_intelnested=1'. Note, 'nested' is enabled by default on kernel 5.0 and later.So if I understand you correctly, we
need to test that:
- with nested=0 VMXE gives EINVAL
- with nested=1 VMXE changes nothing
visible, except probably to allow guest
to read that value (we won't test guest
reading though).
Quoted 1 line
Is this correct?
Yep, exactly!
Quoted 12 lines
Quoted 1 line
With AMD, setting CR4.VMXE=1 is never allowed as AMD doesn't support VMX,
OK, for that I can give you a
Tested-by Stas Sergeev <[email protected]>because I confirm that on AMD it now consistently returns EINVAL, whereas without your patches it did random crap, depending on whether it is a first call to
KVM_SET_SREGS, or not first.Quoted 2 lines
Quoted 2 lines
But we do not use unrestricted guest.
We use v86 under KVM.
Quoted 8 lines
Quoted 4 lines
Unrestricted guest can kick in even if CR0.PG=1 && CR0.PE=1, e.g. there are segmentation checks that apply if and only if unrestricted_guest=0. Long story short, without a deep audit, it's basically impossible to rule out a dependency on unrestricted guest since you're playing around with v86.
You mean "
unrestricted_guest" as a module parameter, rather than the similar named CPU feature, right? So we may depend onunrestricted_guestparameter, but not on a hardware feature, correct?
The unrestricted_guest module param is tied directly to the hardware feature, i.e. if kvm_intel.unrestricted_guest=0 then KVM will run guests with unrestricted guest disabled. That doesn't necessarily mean any of the behavior that is allowed by unrestricted guest will be encountered, but if it is encountered, then it will be handled by the CPU instead of causing a VM-Exit and requiring KVM emulation.
The reported is using an old CPU that doesn't support unrestricted guest, so both the hardware feature and the module param will be off/0.
Quoted 10 lines
Quoted 7 lines
Quoted 2 lines
The only other effect of setting VMXE was clearing VME. Which shouldn't affect anything either, right?
Hmm, clearing VME would mean that exceptions/interrupts within the guest would trigger a switch out of v86 and into vanilla protected mode. v86 and PM have different consistency checks, particularly for segmentation, so it's plausible that clearing CR4.VME inadvertantly worked around the bug by avoiding invalid guest state for v86.
Lets assume that was the case. With those github guys its not possible to do any consistent checks. :(
K. If this is ever a problem in the future, having a way relatively simple reproducer, e.g. something we can run without having to build/install a variety of tools, would make it easier to debug. In theory, the bug should be reproducible even on modern hardware by loading KVM with unrestricted_guest=0.
KVM_SET_CPUID doesn't check supported bits (was Re: [PATCH 0/6] KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup)
In reply to Sean Christopherson09.10.2020 18:30, Sean Christopherson пишет:
Quoted 7 lines
Quoted 2 lines
The only other effect of setting VMXE was clearing VME. Which shouldn't affect anything either, right?
Hmm, clearing VME would mean that exceptions/interrupts within the guest would trigger a switch out of v86 and into vanilla protected mode. v86 and PM have different consistency checks, particularly for segmentation, so it's plausible that clearing CR4.VME inadvertantly worked around the bug by avoiding invalid guest state for v86.
Almost.
So with your patch set (thanks!) and a bit of further investigations, it now became clear where the problem is.
We have this code:
---
|cpuid->nent = 2; // Use the same values as in emu-i386/simx86/interp.c // (Pentium 133-200MHz, "GenuineIntel") cpuid->entries[0] = (struct kvm_cpuid_entry) { .function = 0, .eax = 1, .ebx = 0x756e6547, .ecx = 0x6c65746e, .edx = 0x49656e69 }; // family 5, model 2, stepping 12, fpu vme de pse tsc msr mce cx8 cpuid->entries[1] = (struct kvm_cpuid_entry) { .function = 1, .eax = 0x052c, .ebx = 0, .ecx = 0, .edx = 0x1bf }; ret = ioctl(vcpufd, KVM_SET_CPUID, cpuid); free(cpuid); if (ret == -1) { perror("KVM: KVM_SET_CPUID"); return 0; } --- It tries to enable VME among other things. qemu appears to disable VME by default, unless you do "-cpu host". So we have a situation where the host (which is qemu) doesn't have VME, and guest (dosemu) is trying to enable it. Now obviously ||KVM_SET_CPUID| doesn't check anyting at all and returns success. That later turns
into an invalid guest state.
Question: should|KVM_SET_CPUID| check for supported bits, end return error if not everything is supported?
||
KVM_SET_CPUID doesn't check supported bits (was Re: [PATCH 0/6] KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup)
In reply to Sean Christopherson[re-send because of bad formatting]
09.10.2020 18:30, Sean Christopherson пишет:
Quoted 7 lines
Quoted 2 lines
The only other effect of setting VMXE was clearing VME. Which shouldn't affect anything either, right?
Hmm, clearing VME would mean that exceptions/interrupts within the guest would trigger a switch out of v86 and into vanilla protected mode. v86 and PM have different consistency checks, particularly for segmentation, so it's plausible that clearing CR4.VME inadvertantly worked around the bug by avoiding invalid guest state for v86.
Almost.
So with your patch set (thanks!) and a bit of further investigations, it now became clear where the problem is.
We have this code:
---
|cpuid->nent = 2; // Use the same values as in emu-i386/simx86/interp.c // (Pentium 133-200MHz, "GenuineIntel") cpuid->entries[0] = (struct kvm_cpuid_entry) { .function = 0, .eax = 1, .ebx = 0x756e6547, .ecx = 0x6c65746e, .edx = 0x49656e69 }; // family 5, model 2, stepping 12, fpu vme de pse tsc msr mce cx8 cpuid->entries[1] = (struct kvm_cpuid_entry) { .function = 1, .eax = 0x052c, .ebx = 0, .ecx = 0, .edx = 0x1bf }; ret = ioctl(vcpufd, KVM_SET_CPUID, cpuid); free(cpuid); if (ret == -1) { perror("KVM: KVM_SET_CPUID"); return 0; }|
---
It tries to enable VME among other things. qemu appears to disable VME by default,
unless you do "-cpu host". So we have a situation where the host (which is qemu) doesn't have VME,
and guest (dosemu) is trying to enable it.
Now obviously KVM_SET_CPUID doesn't check anyting at all and returns success. That later turns into an invalid guest state.
Question: should KVM_SET_CPUID check for supported bits, end return error if not everything is supported?
Paolo Bonzini
maintainerRe: KVM_SET_CPUID doesn't check supported bits (was Re: [PATCH 0/6] KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup)
Quoted 10 lines · stsp
It tries to enable VME among other things. qemu appears to disable VME by default,
unless you do "-cpu host". So we have a situation where the host (which is qemu) doesn't have VME,
and guest (dosemu) is trying to enable it.
Now obviouslyKVM_SET_CPUIDdoesn't check anyting at all and returns success. That later turns into an invalid guest state.
Quoted 3 lines
Question: should
KVM_SET_CPUIDcheck for supported bits, end return error if not everything is supported?
No, it is intentional. Most bits of CPUID are not ever checked by KVM, so userspace is supposed to set values that makes sense or just copy the value of KVM_GET_SUPPORTED_CPUID more or less blindly.
Paolo
Re: KVM_SET_CPUID doesn't check supported bits (was Re: [PATCH 0/6] KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup)
07.12.2020 14:29, Paolo Bonzini пишет:
Quoted 11 lines · stsp
Quoted 10 lines
It tries to enable VME among other things. qemu appears to disable VME by default,
unless you do "-cpu host". So we have a situation where the host (which is qemu) doesn't have VME,
and guest (dosemu) is trying to enable it.
Now obviouslyKVM_SET_CPUIDdoesn't check anyting at all and returns success. That later turns into an invalid guest state.
Quoted 5 lines
Quoted 3 lines
Question: should
KVM_SET_CPUIDcheck for supported bits, end return error if not everything is supported?No, it is intentional. Most bits of CPUID are not ever checked by KVM, so userspace is supposed to set values that makes sense
By "that makes sense" you probably
meant to say "bits_that_makes_sense masked with the ones returned by KVM_GET_SUPPORTED_CPUID"?
So am I right that KVM_SET_CPUID only "lowers"
the supported bits? In which case I don't need to call it at all, but instead just call KVM_GET_SUPPORTED_CPUID and see if the needed bits are supported, and
exit otherwise, right?
A message that is not in the archive (sent off-list or not yet received): CABgObfYS57_ez-t=eu9+3S2bhSXC_9DTj=64Sna2jnYEMYo2Ag@mail.gmail.com
Re: KVM_SET_CPUID doesn't check supported bits (was Re: [PATCH 0/6] KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup)
07.12.2020 16:35, Paolo Bonzini пишет:
Quoted 4 lines
Il lun 7 dic 2020, 12:47 stsp <[email protected] <mailto:[email protected]>> ha scritto:
Quoted 8 lines
So am I right that
KVM_SET_CPUIDonly "lowers"
the supported bits? In which case I don't need to call it at all, but instead just callKVM_GET_SUPPORTED_CPUIDand see if the needed bits are supported, and
exit otherwise, right?You always have to call
KVM_SET_CPUID2, but you can just pass in whatever you got fromKVM_GET_SUPPORTED_CPUID.
OK, done that, thanks.
(after checking that KVM_GET_SUPPORTED_CPUID actually has the needed features itself, otherwise exit).
Perhaps it would be good if guest cpuid to have a default values of KVM_GET_SUPPORTED_CPUID, so that the user doesn't have to do the needless calls to just copy host features to guest cpuid.
A message that is not in the archive (sent off-list or not yet received): CABgObfb_4r=k_qakd+48hPar8rzc-P50+dgdoYvQaL2H-po6+g@mail.gmail.com
Re: KVM_SET_CPUID doesn't check supported bits (was Re: [PATCH 0/6] KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup)
07.12.2020 17:09, Paolo Bonzini пишет:
Quoted 11 lines
Il lun 7 dic 2020, 15:04 stsp <[email protected] <mailto:[email protected]>> ha scritto:
Perhaps it would be good if guest cpuid to have a default values of
KVM_GET_SUPPORTED_CPUID, so that the user doesn't have to do the needless calls to just copy host features to guest cpuid.It is too late to change that aspect of the API, unfortunately. We don't know how various userspaces would behave.
Which means some sensible behaviour already exists if I don't call KVM_SET_CPUID2. So what is it, #UD on CPUID?
Would be good to have that documented.
A message that is not in the archive (sent off-list or not yet received): CABgObfYN7Okdt+YfHtsd3M_00iuWf=UyKPmbQhhYBhoiMtdXuw@mail.gmail.com
Re: KVM_SET_CPUID doesn't check supported bits (was Re: [PATCH 0/6] KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup)
07.12.2020 17:34, Paolo Bonzini пишет:
Quoted 9 lines
Quoted 3 lines
It is too late to change that aspect of the API, unfortunately. We don't know how various userspaces would behave.
Which means some sensible behaviour already exists if I don't call
KVM_SET_CPUID2. So what is it, #UD on CPUID?I would have to check but I think you always get zeroes; not entirely sensible.
In that case I would argue that you can't break anything by changing that to something sensible. :)
But anyway, since my problem is solved, this is just a potential improvement for the future, or the case for documenting.
Re: KVM_SET_CPUID doesn't check supported bits (was Re: [PATCH 0/6] KVM: x86: KVM_SET_SREGS.CR4 bug fixes and cleanup)
In reply to stspQuoted 23 lines · stsp
07.12.2020 14:29, Paolo Bonzini пишет:
Quoted 11 lines · stsp
Quoted 10 lines
It tries to enable VME among other things. qemu appears to disable VME by default,
unless you do "-cpu host". So we have a situation where the host (which is qemu) doesn't have VME,
and guest (dosemu) is trying to enable it.
Now obviouslyKVM_SET_CPUIDdoesn't check anyting at all and returns success. That later turns into an invalid guest state.Quoted 6 lines
Quoted 3 lines
Question: should
KVM_SET_CPUIDcheck for supported bits, end return error if not everything is supported?No, it is intentional. Most bits of CPUID are not ever checked by KVM, so userspace is supposed to set values that makes sense
By "that makes sense" you probably
meant to say "bits_that_makes_sensemasked with the ones returned byKVM_GET_SUPPORTED_CPUID"?
Quoted 5 lines
So am I right that
KVM_SET_CPUIDonly "lowers"
the supported bits? In which case I don't need to call it at all, but instead just callKVM_GET_SUPPORTED_CPUIDand see if the needed bits are supported, and
exit otherwise, right?
"Lowers" is a tricky concept for CPUID information. Some feature bits report 0 for "present" and 1 for "not-present." Some multi-bit fields are interpreted as numbers, which may be signed or unsigned. Some multi-bit fields are strings. Some fields have dependencies on other fields. Etc.
Quoted 21 lines · Sean Christopherson
Two bug fixes to handle
KVM_SET_SREGSwithout a precedingKVM_SET_CPUID2.The overarching issue is that
kvm_x86_ops.set_cr4()can fail, but its invocation from__set_sregs(), a.k.a.KVM_SET_SREGS, ignores the result. Fix the issue by moving all validity checks out of .set_cr4() in one way or another.I intentionally omitted a Cc to stable. The first bug fix in particular may break stable trees as it simply removes a check, and I don't know that stable trees have the generic CR4 reserved bit check that is needed to prevent the guest from setting VMXE when nVMX is not allowed.
Sean Christopherson (6):
KVM: VMX: Drop guest CPUID check for VMXE invmx_set_cr4()
KVM: VMX: Drop explicit 'nested' check fromvmx_set_cr4()
KVM: SVM: Drop VMXE check fromsvm_set_cr4()
KVM: x86: Move vendor CR4 validity check to dedicatedkvm_x86_opshook
KVM: x86: Return bool instead of int for CR4 and SREGS validity checks
KVM: selftests: Verify supported CR4 bits can be set before
KVM_SET_CPUID2
Quoted 14 lines
arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/svm/svm.c | 12 ++- arch/x86/kvm/svm/svm.h | 2 +- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 35 +++---- arch/x86/kvm/vmx/vmx.h | 2 +- arch/x86/kvm/x86.c | 28 +++--- arch/x86/kvm/x86.h | 2 +- .../selftests/kvm/include/x86_64/processor.h | 17 ++++ .../selftests/kvm/include/x86_64/vmx.h | 4 - .../selftests/kvm/x86_64/set_sregs_test.c | 92 ++++++++++++++++++- 12 files changed, 153 insertions(+), 48 deletions(-)
Queued, thanks.
Paolo