Received: by 10.223.176.5 with SMTP id f5csp1008360wra; Sat, 3 Feb 2018 15:52:25 -0800 (PST) X-Google-Smtp-Source: AH8x224Lykxvt6356Z+YyIYlunTJOb0JWoWJIFjWyp+ASQebz59qnvDKTH8NDeGIn5puv35+5QCd X-Received: by 10.101.80.69 with SMTP id k5mr24303824pgo.425.1517701945501; Sat, 03 Feb 2018 15:52:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517701945; cv=none; d=google.com; s=arc-20160816; b=M7sHstPT3zMpoHtibL2QcAeWPm2hakPXTYg3AWaKFssIsCZXNB89EhGWcNxS1oG4nA U2kJ3XuR1X2bL0QSucZa7Q4G2k1ArCAlHLFYQeBRZfTxarARKqPfYXVd8Deu9y6BI0My a6nhWf4VlZErzDW+1yzdSG8khzsZkGIxLbiJXsvZY17X0Kxtdd9VF7Rziu13QtbfGZ4y bfR2RtOzo242c/tKk0ebK8NmwEyeIneiRzzxbK030EsZWj6hDst00J3px1SKj8/oeH9C k0Ho4HTay4YV39czszDCZD1BcR7t9JbhRjsTt/W4zj6ThOzU4ZqNN9ML47s5gspcLKL1 CwAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=36s8cP3wLIx8epx28EuwxOKzSSDPl8UvF2WTSCmkb0k=; b=hmb49oVIFB9IlbVA2cOTVCnSdd2HNsFg/XcTuhpKLwRQqZwATaI2B7PDSJCa1ARzTi EdjcWj1MT7ZvPnHtgLJ6EQVawJfsCIKUZoYKQNigYeRQQ6Ah/Pxy/hGV4VYhhnnIvJJA jHkXPHg6OErSo15trE+Zl1x9ffUAhk25iuGqEMvC9uhj0x+iGHQI/dYd3RVGCsQk115w xoR9b5QJ76h3PrKLdasZyTnVtwIJovWSXWG+9sK7nE8WzugBGl942NtlULFqUiyFtLzm /5MrtpkCuspOkWYIxjRHFKXqjQfbTQLw0L91UfzD2tr3tl7jJ2cx7BQSjJ2YlLY1CLk/ ro5Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n7-v6si2571637plk.437.2018.02.03.15.52.11; Sat, 03 Feb 2018 15:52:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754001AbeBCWyp (ORCPT + 99 others); Sat, 3 Feb 2018 17:54:45 -0500 Received: from terminus.zytor.com ([65.50.211.136]:52237 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752505AbeBCWyl (ORCPT ); Sat, 3 Feb 2018 17:54:41 -0500 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTP id w13MqEbM019701; Sat, 3 Feb 2018 14:52:14 -0800 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w13MqDtu019693; Sat, 3 Feb 2018 14:52:13 -0800 Date: Sat, 3 Feb 2018 14:52:13 -0800 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for KarimAllah Ahmed Message-ID: Cc: dan.j.williams@intel.com, hpa@zytor.com, gregkh@linuxfoundation.org, mingo@kernel.org, torvalds@linux-foundation.org, konrad.wilk@oracle.com, tim.c.chen@linux.intel.com, aarcange@redhat.com, dwmw@amazon.co.uk, tglx@linutronix.de, linux-kernel@vger.kernel.org, pbonzini@redhat.com, arjan.van.de.ven@intel.com, asit.k.mallick@intel.com, jun.nakajima@intel.com, ashok.raj@intel.com, karahmed@amazon.de, dave.hansen@intel.com, ak@linux.intel.com, luto@kernel.org, darren.kenny@oracle.com Reply-To: dave.hansen@intel.com, karahmed@amazon.de, ashok.raj@intel.com, darren.kenny@oracle.com, ak@linux.intel.com, luto@kernel.org, tglx@linutronix.de, aarcange@redhat.com, dwmw@amazon.co.uk, tim.c.chen@linux.intel.com, jun.nakajima@intel.com, arjan.van.de.ven@intel.com, asit.k.mallick@intel.com, pbonzini@redhat.com, linux-kernel@vger.kernel.org, konrad.wilk@oracle.com, mingo@kernel.org, torvalds@linux-foundation.org, dan.j.williams@intel.com, gregkh@linuxfoundation.org, hpa@zytor.com In-Reply-To: <1517669783-20732-1-git-send-email-karahmed@amazon.de> References: <1517669783-20732-1-git-send-email-karahmed@amazon.de> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/pti] KVM/SVM: Allow direct access to MSR_IA32_SPEC_CTRL Git-Commit-ID: b2ac58f90540e39324e7a29a7ad471407ae0bf48 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: b2ac58f90540e39324e7a29a7ad471407ae0bf48 Gitweb: https://git.kernel.org/tip/b2ac58f90540e39324e7a29a7ad471407ae0bf48 Author: KarimAllah Ahmed AuthorDate: Sat, 3 Feb 2018 15:56:23 +0100 Committer: Thomas Gleixner CommitDate: Sat, 3 Feb 2018 23:06:52 +0100 KVM/SVM: Allow direct access to MSR_IA32_SPEC_CTRL [ Based on a patch from Paolo Bonzini ] ... basically doing exactly what we do for VMX: - Passthrough SPEC_CTRL to guests (if enabled in guest CPUID) - Save and restore SPEC_CTRL around VMExit and VMEntry only if the guest actually used it. Signed-off-by: KarimAllah Ahmed Signed-off-by: David Woodhouse Signed-off-by: Thomas Gleixner Reviewed-by: Darren Kenny Reviewed-by: Konrad Rzeszutek Wilk Cc: Andrea Arcangeli Cc: Andi Kleen Cc: Jun Nakajima Cc: kvm@vger.kernel.org Cc: Dave Hansen Cc: Tim Chen Cc: Andy Lutomirski Cc: Asit Mallick Cc: Arjan Van De Ven Cc: Greg KH Cc: Paolo Bonzini Cc: Dan Williams Cc: Linus Torvalds Cc: Ashok Raj Link: https://lkml.kernel.org/r/1517669783-20732-1-git-send-email-karahmed@amazon.de --- arch/x86/kvm/svm.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 254eefb..4e3c795 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -184,6 +184,8 @@ struct vcpu_svm { u64 gs_base; } host; + u64 spec_ctrl; + u32 *msrpm; ulong nmi_iret_rip; @@ -249,6 +251,7 @@ static const struct svm_direct_access_msrs { { .index = MSR_CSTAR, .always = true }, { .index = MSR_SYSCALL_MASK, .always = true }, #endif + { .index = MSR_IA32_SPEC_CTRL, .always = false }, { .index = MSR_IA32_PRED_CMD, .always = false }, { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, @@ -882,6 +885,25 @@ static bool valid_msr_intercept(u32 index) return false; } +static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr) +{ + u8 bit_write; + unsigned long tmp; + u32 offset; + u32 *msrpm; + + msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm: + to_svm(vcpu)->msrpm; + + offset = svm_msrpm_offset(msr); + bit_write = 2 * (msr & 0x0f) + 1; + tmp = msrpm[offset]; + + BUG_ON(offset == MSR_INVALID); + + return !!test_bit(bit_write, &tmp); +} + static void set_msr_interception(u32 *msrpm, unsigned msr, int read, int write) { @@ -1584,6 +1606,8 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) u32 dummy; u32 eax = 1; + svm->spec_ctrl = 0; + if (!init_event) { svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE; @@ -3605,6 +3629,13 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_VM_CR: msr_info->data = svm->nested.vm_cr_msr; break; + case MSR_IA32_SPEC_CTRL: + if (!msr_info->host_initiated && + !guest_cpuid_has(vcpu, X86_FEATURE_IBRS)) + return 1; + + msr_info->data = svm->spec_ctrl; + break; case MSR_IA32_UCODE_REV: msr_info->data = 0x01000065; break; @@ -3696,6 +3727,33 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) case MSR_IA32_TSC: kvm_write_tsc(vcpu, msr); break; + case MSR_IA32_SPEC_CTRL: + if (!msr->host_initiated && + !guest_cpuid_has(vcpu, X86_FEATURE_IBRS)) + return 1; + + /* The STIBP bit doesn't fault even if it's not advertised */ + if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP)) + return 1; + + svm->spec_ctrl = data; + + if (!data) + break; + + /* + * For non-nested: + * When it's written (to non-zero) for the first time, pass + * it through. + * + * For nested: + * The handling of the MSR bitmap for L2 guests is done in + * nested_svm_vmrun_msrpm. + * We update the L1 MSR bit as well since it will end up + * touching the MSR anyway now. + */ + set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); + break; case MSR_IA32_PRED_CMD: if (!msr->host_initiated && !guest_cpuid_has(vcpu, X86_FEATURE_IBPB)) @@ -4964,6 +5022,15 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) local_irq_enable(); + /* + * If this vCPU has touched SPEC_CTRL, restore the guest's value if + * it's non-zero. Since vmentry is serialising on affected CPUs, there + * is no need to worry about the conditional branch over the wrmsr + * being speculatively taken. + */ + if (svm->spec_ctrl) + wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); + asm volatile ( "push %%" _ASM_BP "; \n\t" "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t" @@ -5056,6 +5123,27 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) #endif ); + /* + * We do not use IBRS in the kernel. If this vCPU has used the + * SPEC_CTRL MSR it may have left it on; save the value and + * turn it off. This is much more efficient than blindly adding + * it to the atomic save/restore list. Especially as the former + * (Saving guest MSRs on vmexit) doesn't even exist in KVM. + * + * For non-nested case: + * If the L01 MSR bitmap does not intercept the MSR, then we need to + * save it. + * + * For nested case: + * If the L02 MSR bitmap does not intercept the MSR, then we need to + * save it. + */ + if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)) + rdmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); + + if (svm->spec_ctrl) + wrmsrl(MSR_IA32_SPEC_CTRL, 0); + /* Eliminate branch target predictions from guest mode */ vmexit_fill_RSB();