kvm: x86: Sink cpuid update into vendor-specific set_cr4 functions
PATCH 1/1 kvm: x86: Sink cpuid update into vendor-specific set_cr4 functions
On emulated VM-entry and VM-exit, update the CPUID bits that reflect CR4.OSXSAVE and CR4.PKE.
This fixes a bug where the CPUID bits could continue to reflect L2 CR4 values after emulated VM-exit to L1. It also fixes a related bug where the CPUID bits could continue to reflect L1 CR4 values after emulated VM-entry to L2. The latter bug is mainly relevant to SVM, wherein CPUID is not a required intercept. However, it could also be relevant to VMX, because the code to conditionally update these CPUID bits assumes that the guest CPUID and the guest CR4 are always in sync.
--- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/svm/svm.c | 4 ++++ arch/x86/kvm/vmx/vmx.c | 5 +++++ arch/x86/kvm/x86.c | 8 -------- 4 files changed, 10 insertions(+), 8 deletions(-)
arch/x86/kvm/cpuid.c +1 -0
@@ -139,6 +139,7 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) MSR_IA32_MISC_ENABLE_MWAIT); } } +EXPORT_SYMBOL_GPL (kvm_update_cpuid_runtime);static void kvm_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu) {
arch/x86/kvm/svm/svm.c +4 -0
@@ -1699,6 +1699,10 @@ 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 ); + +if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE |X86_CR4_PKE )) +kvm_update_cpuid_runtime (vcpu); +return 0 ; }
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -3095,6 +3095,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,int vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) { +unsigned long old_cr4 = vcpu->arch.cr4;struct vcpu_vmx *vmx =to_vmx (vcpu);/* * Pass through host's Machine Check Enable value to hw_cr4, which
@@ -3166,6 +3167,10 @@ int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)vmcs_writel (CR4_READ_SHADOW , cr4);vmcs_writel (GUEST_CR4 , hw_cr4); + +if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE |X86_CR4_PKE )) +kvm_update_cpuid_runtime (vcpu); +return 0 ; }
arch/x86/kvm/x86.c +0 -8
@@ -1014,9 +1014,6 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))kvm_mmu_reset_context (vcpu); -if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE |X86_CR4_PKE )) -kvm_update_cpuid_runtime (vcpu); -return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_cr4);
@@ -9522,7 +9519,6 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) {struct msr_data apic_base_msr;int mmu_reset_needed =0 ; -int cpuid_update_needed =0 ;int pending_vec, max_bits, idx;struct desc_ptr dt;int ret = -EINVAL ;
@@ -9557,11 +9553,7 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) vcpu->arch.cr0 = sregs->cr0; mmu_reset_needed |=kvm_read_cr4 (vcpu) != sregs->cr4; - cpuid_update_needed |= ((kvm_read_cr4 (vcpu) ^ sregs->cr4) & - (X86_CR4_OSXSAVE |X86_CR4_PKE )); kvm_x86_ops.set_cr4 (vcpu, sregs->cr4); -if (cpuid_update_needed) -kvm_update_cpuid_runtime (vcpu); idx =srcu_read_lock (&vcpu->kvm->srcu);if (is_pae_paging (vcpu)) {
Signature · 1 line
2.29.1.341.ge80a0c044ae-goog
Quoted 103 lines · Jim Mattson
On emulated VM-entry and VM-exit, update the CPUID bits that reflect CR4.OSXSAVE and CR4.PKE.
This fixes a bug where the CPUID bits could continue to reflect L2 CR4 values after emulated VM-exit to L1. It also fixes a related bug where the CPUID bits could continue to reflect L1 CR4 values after emulated VM-entry to L2. The latter bug is mainly relevant to SVM, wherein CPUID is not a required intercept. However, it could also be relevant to VMX, because the code to conditionally update these CPUID bits assumes that the guest CPUID and the guest CR4 are always in sync.
Fixes 8eb3f87d903168 ("KVM: nVMX: fix guest CR4 loading when emulating L2 to L1 exit")Fixes 2acf923e38fb6a ("KVM: VMX: Enable XSAVE/XRSTOR for guest")Fixes b9baba86148904 ("KVM, pkeys: expose CPUID/CR4 to guest")Reported-by Abhiroop Dabral <[email protected]>Signed-off-by Jim Mattson <[email protected]>Reviewed-by Ricardo Koller <[email protected]>Reviewed-by Peter Shier <[email protected]>Cc Haozhong Zhang <[email protected]>Cc Dexuan Cui <[email protected]>Cc Huaitong Han <[email protected]>--- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/svm/svm.c | 4 ++++ arch/x86/kvm/vmx/vmx.c | 5 +++++ arch/x86/kvm/x86.c | 8 -------- 4 files changed, 10 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +1
-0@@ -139,6 +139,7 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) MSR_IA32_MISC_ENABLE_MWAIT); } } +EXPORT_SYMBOL_GPL (kvm_update_cpuid_runtime);static void kvm_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu) {arch/x86/kvm/svm/svm.c +4
-0@@ -1699,6 +1699,10 @@ 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 ); + +if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE |X86_CR4_PKE )) +kvm_update_cpuid_runtime (vcpu); +return 0 ; }arch/x86/kvm/vmx/vmx.c +5
-0@@ -3095,6 +3095,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,int vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) { +unsigned long old_cr4 = vcpu->arch.cr4;struct vcpu_vmx *vmx =to_vmx (vcpu);/* * Pass through host's Machine Check Enable value to hw_cr4, which @@ -3166,6 +3167,10 @@ int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)vmcs_writel (CR4_READ_SHADOW , cr4);vmcs_writel (GUEST_CR4 , hw_cr4); + +if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE |X86_CR4_PKE )) +kvm_update_cpuid_runtime (vcpu); +return 0 ; }arch/x86/kvm/x86.c +0
-4@@ -1014,9 +1014,6 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))kvm_mmu_reset_context (vcpu); -if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE |X86_CR4_PKE )) -kvm_update_cpuid_runtime (vcpu); -return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_cr4);@@ -9522,7 +9519,6 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) {struct msr_data apic_base_msr;int mmu_reset_needed =0 ; -int cpuid_update_needed =0 ;int pending_vec, max_bits, idx;struct desc_ptr dt;int ret = -EINVAL ;@@ -9557,11 +9553,7 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) vcpu->arch.cr0 = sregs->cr0;
arch/x86/kvm/x86.c static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) · line 9559
Quoted 10 lines
arch/x86/kvm/x86.c +0
-4mmu_reset_needed |=kvm_read_cr4 (vcpu) != sregs->cr4; - cpuid_update_needed |= ((kvm_read_cr4 (vcpu) ^ sregs->cr4) & - (X86_CR4_OSXSAVE |X86_CR4_PKE )); kvm_x86_ops.set_cr4 (vcpu, sregs->cr4); -if (cpuid_update_needed) -kvm_update_cpuid_runtime (vcpu); idx =srcu_read_lock (&vcpu->kvm->srcu);if (is_pae_paging (vcpu)) {
Queued, thanks.
Paolo