From mboxrd@z Thu Jan 1 00:00:00 1970 From: Gleb Natapov Subject: Re: [PATCH 1/2] KVM: nVMX: Validate EFER values for VM_ENTRY/EXIT_LOAD_IA32_EFER Date: Mon, 22 Apr 2013 12:54:41 +0300 Message-ID: <20130422095441.GA6027@redhat.com> References: <516A8879.6050202@web.de> Mime-Version: 1.0 Content-Type: text/plain; charset=cp1255 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Marcelo Tosatti , kvm , Paolo Bonzini , "Nadav Har'El" To: Jan Kiszka Return-path: Received: from mx1.redhat.com ([209.132.183.28]:7281 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755626Ab3DVJyt convert rfc822-to-8bit (ORCPT ); Mon, 22 Apr 2013 05:54:49 -0400 Content-Disposition: inline In-Reply-To: <516A8879.6050202@web.de> Sender: kvm-owner@vger.kernel.org List-ID: On Sun, Apr 14, 2013 at 12:44:09PM +0200, Jan Kiszka wrote: > From: Jan Kiszka >=20 > As we may emulate the loading of EFER on VM-entry and VM-exit, implem= ent > the checks that VMX performs on the guest and host values on vmlaunch= / > vmresume. Factor out kvm_valid_efer for this purpose which checks for > set reserved bits. >=20 > Signed-off-by: Jan Kiszka Applied v2 of 1/2 and 2/2. > --- > arch/x86/include/asm/kvm_host.h | 1 + > arch/x86/kvm/vmx.c | 38 +++++++++++++++++++++++++++++= +++++++++ > arch/x86/kvm/x86.c | 29 +++++++++++++++++++---------- > 3 files changed, 58 insertions(+), 10 deletions(-) >=20 > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/k= vm_host.h > index b2c7263..28a458f 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -805,6 +805,7 @@ static inline int emulate_instruction(struct kvm_= vcpu *vcpu, > } > =20 > void kvm_enable_efer_bits(u64); > +bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer); > int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data); > int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr); > =20 > diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c > index cc2ba55..0d13b29 100644 > --- a/arch/x86/kvm/vmx.c > +++ b/arch/x86/kvm/vmx.c > @@ -7257,6 +7257,44 @@ static int nested_vmx_run(struct kvm_vcpu *vcp= u, bool launch) > } > =20 > /* > + * If the =93load IA32_EFER=94 VM-entry control is 1, the following= checks > + * are performed on the field for the IA32_EFER MSR: > + * - Bits reserved in the IA32_EFER MSR must be 0. > + * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value o= f > + * the IA-32e mode guest VM-exit control. It must also be identic= al > + * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to > + * CR0.PG) is 1.1 > + */ > + if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER && > + (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || > + !!(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) !=3D > + !!(vmcs12->guest_ia32_efer & EFER_LMA) || > + (vmcs12->guest_cr0 & X86_CR0_PG && > + !!(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) !=3D > + !!(vmcs12->guest_ia32_efer & EFER_LME)))) { > + nested_vmx_entry_failure(vcpu, vmcs12, > + EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); > + return 1; > + } > + > + /* > + * If the load IA32_EFER VM-exit control is 1, bits reserved in the > + * IA32_EFER MSR must be 0 in the field for that register. In addit= ion, > + * the values of the LMA and LME bits in the field must each be tha= t of > + * the host address-space size VM-exit control. > + */ > + if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER && > + (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || > + !!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) !=3D > + !!(vmcs12->host_ia32_efer & EFER_LMA) || > + !!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) !=3D > + !!(vmcs12->host_ia32_efer & EFER_LME))) { > + nested_vmx_entry_failure(vcpu, vmcs12, > + EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); > + return 1; > + } > + > + /* > * We're finally done with prerequisite checking, and can start wit= h > * the nested entry. > */ > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index eb9927e..f248a3a 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -845,23 +845,17 @@ static const u32 emulated_msrs[] =3D { > MSR_IA32_MCG_CTL, > }; > =20 > -static int set_efer(struct kvm_vcpu *vcpu, u64 efer) > +bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) > { > - u64 old_efer =3D vcpu->arch.efer; > - > if (efer & efer_reserved_bits) > - return 1; > - > - if (is_paging(vcpu) > - && (vcpu->arch.efer & EFER_LME) !=3D (efer & EFER_LME)) > - return 1; > + return false; > =20 > if (efer & EFER_FFXSR) { > struct kvm_cpuid_entry2 *feat; > =20 > feat =3D kvm_find_cpuid_entry(vcpu, 0x80000001, 0); > if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) > - return 1; > + return false; > } > =20 > if (efer & EFER_SVME) { > @@ -869,9 +863,24 @@ static int set_efer(struct kvm_vcpu *vcpu, u64 e= fer) > =20 > feat =3D kvm_find_cpuid_entry(vcpu, 0x80000001, 0); > if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) > - return 1; > + return false; > } > =20 > + return true; > +} > +EXPORT_SYMBOL_GPL(kvm_valid_efer); > + > +static int set_efer(struct kvm_vcpu *vcpu, u64 efer) > +{ > + u64 old_efer =3D vcpu->arch.efer; > + > + if (!kvm_valid_efer(vcpu, efer)) > + return 1; > + > + if (is_paging(vcpu) > + && (vcpu->arch.efer & EFER_LME) !=3D (efer & EFER_LME)) > + return 1; > + > efer &=3D ~EFER_LMA; > efer |=3D vcpu->arch.efer & EFER_LMA; > =20 > --=20 > 1.7.3.4 -- Gleb.