From: Liran Alon Subject: Re: [PATCH 6/8] kvm: svm: pass MSR_IA32_SPEC_CTRL and MSR_IA32_PRED_CMD down to guest Date: Tue, 9 Jan 2018 08:10:05 -0800 (PST) Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: , , , , , , , , To: Return-path: Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org ----- pbonzini@redhat.com wrote: > Direct access to MSR_IA32_SPEC_CTRL and MSR_IA32_PRED_CMD is > important > for performance. Allow load/store of MSR_IA32_SPEC_CTRL, restore > guest > IBRS on VM entry and set it to 0 on VM exit (because Linux does not > use > it yet). >=20 > Signed-off-by: Paolo Bonzini > --- > arch/x86/kvm/svm.c | 42 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 42 insertions(+) >=20 > diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c > index 31ace8d7774a..934a21e02e03 100644 > --- a/arch/x86/kvm/svm.c > +++ b/arch/x86/kvm/svm.c > @@ -183,6 +183,8 @@ struct vcpu_svm { > =09=09u64 gs_base; > =09} host; > =20 > +=09u64 spec_ctrl; > + > =09u32 *msrpm; > =20 > =09ulong nmi_iret_rip; > @@ -248,6 +250,8 @@ struct amd_svm_iommu_ir { > =09{ .index =3D MSR_CSTAR,=09=09=09=09.always =3D true }, > =09{ .index =3D MSR_SYSCALL_MASK,=09=09=09.always =3D true }, > #endif > +=09{ .index =3D MSR_IA32_SPEC_CTRL,=09=09=09.always =3D true }, > +=09{ .index =3D MSR_IA32_PRED_CMD,=09=09=09.always =3D true }, > =09{ .index =3D MSR_IA32_LASTBRANCHFROMIP,=09=09.always =3D false }, > =09{ .index =3D MSR_IA32_LASTBRANCHTOIP,=09=09.always =3D false }, > =09{ .index =3D MSR_IA32_LASTINTFROMIP,=09=09.always =3D false }, > @@ -283,6 +287,8 @@ struct amd_svm_iommu_ir { > /* enable/disable Virtual GIF */ > static int vgif =3D true; > module_param(vgif, int, 0444); > +=20 > +static bool __read_mostly have_spec_ctrl; > =20 > static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); > static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool > invalidate_gpa); > @@ -1135,6 +1141,17 @@ static __init int svm_hardware_setup(void) > =09=09=09pr_info("Virtual GIF supported\n"); > =09} > =20 > +=09/* > +=09 * FIXME: this is only needed until SPEC_CTRL is supported > +=09 * by upstream Linux in cpufeatures, then it can be replaced > +=09 * with static_cpu_has. > +=09 */ > +=09have_spec_ctrl =3D cpu_has_spec_ctrl(); > +=09if (have_spec_ctrl) > +=09=09pr_info("kvm: SPEC_CTRL available\n"); > +=09else > +=09=09pr_info("kvm: SPEC_CTRL not available\n"); > + > =09return 0; > =20 > err: > @@ -3599,6 +3616,9 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, > struct msr_data *msr_info) > =09case MSR_VM_CR: > =09=09msr_info->data =3D svm->nested.vm_cr_msr; > =09=09break; > +=09case MSR_IA32_SPEC_CTRL: > +=09=09msr_info->data =3D svm->spec_ctrl; > +=09=09break; > =09case MSR_IA32_UCODE_REV: > =09=09msr_info->data =3D 0x01000065; > =09=09break; > @@ -3754,6 +3774,9 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, > struct msr_data *msr) > =09case MSR_VM_IGNNE: > =09=09vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, > data); > =09=09break; > +=09case MSR_IA32_SPEC_CTRL: > +=09=09svm->spec_ctrl =3D data; > +=09=09break; > =09case MSR_IA32_APICBASE: > =09=09if (kvm_vcpu_apicv_active(vcpu)) > =09=09=09avic_update_vapic_bar(to_svm(vcpu), data); > @@ -4942,6 +4965,13 @@ static void svm_vcpu_run(struct kvm_vcpu > *vcpu) > =20 > =09local_irq_enable(); > =20 > +=09/* > +=09 * MSR_IA32_SPEC_CTRL is restored after the last indirect branch > +=09 * before vmentry. > +=09 */ > +=09if (have_spec_ctrl && svm->spec_ctrl !=3D 0) > +=09=09wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); > + > =09asm volatile ( > =09=09"push %%" _ASM_BP "; \n\t" > =09=09"mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t" > @@ -5015,6 +5045,18 @@ static void svm_vcpu_run(struct kvm_vcpu > *vcpu) > #endif > =09=09); > =20 > +=09if (have_spec_ctrl) { > +=09=09rdmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); > +=09=09if (svm->spec_ctrl !=3D 0) > +=09=09=09wrmsrl(MSR_IA32_SPEC_CTRL, 0); > +=09} > +=09/* > +=09 * Speculative execution past the above wrmsrl might encounter > +=09 * an indirect branch and use guest-controlled contents of the > +=09 * indirect branch predictor; block it. > +=09 */ > +=09asm("lfence"); > + > #ifdef CONFIG_X86_64 > =09wrmsrl(MSR_GS_BASE, svm->host.gs_base); > #else > --=20 > 1.8.3.1 Reviewed-by: Liran Alon