Received: by 10.223.176.5 with SMTP id f5csp1793959wra; Wed, 31 Jan 2018 11:40:34 -0800 (PST) X-Google-Smtp-Source: AH8x226JXWKH1QS2UNxmx7loq4XrtYhNYEF9jyfqK5C2sigynQ8jDLsHdMqz6pwJ8zhNq3nhS4sR X-Received: by 10.98.63.214 with SMTP id z83mr34034247pfj.95.1517427634360; Wed, 31 Jan 2018 11:40:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517427634; cv=none; d=google.com; s=arc-20160816; b=LzEqNrMvbsAz2/Rla2g4i8QIazCDrVYppOC7aaWiU0wrZVRYep8VXGJTcN3/+Y5SyF Ad1gOMVQ1ogqFnuxor3/p3711KipDxDoMXWKrdOfYYVKjzNjpWnAn5eXJg0ilsOuE8tq wNbOQ4Le434oJNk9PMyWQ8c4Ax+aYZyr765W7owpq9lMkT8DS939qz1bdMP6KmzoqUdn bExCawA4+GO601glZvfnMsWf4Ohwe3HqsD0bKyZ6BGVvEU3Z8AHGxqypuRjr7QfJJpuY dXeNiDlcSnXxEn1ajHv7gJZMfo8XufPxFMYRk686JU4BBsW7h5lpdyb1PaJzcCJpVyts tVSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=YzKb2mCblw2yY8pbeZd0HYgN6YkkRDaNdFPySb/PH1w=; b=v5K2/KPOXw1Jh4MUPFQwIQxJg79y++xhgmz/GpQ9zi8BTYNloj6xu4X73B1no8+aKp 7jOnZEp4JVZqj8v2CdeQWxzDFNKQelc0K//syQBr3dr4WIEg+lcdaSl9HFCBJtn2h7o2 7P8rzXRB/pTw0NaKNSL7UbSPHxMbbw6rv4PmsIfqvwqwuHAV4VbQkUETXxuJgHpbJk4s 0cWFu2GcysLmN7jlRulqMzQHDil+hLwkiBcv06jwVmRhLkYXgCirjsV0u9EUqMiHczLa M08tBIcMJGqVt9vJnguvFMyv9hVWfFu1F7auqKIF+72As5ZoOV7CLYhLBvv7EqXbW3/w HPzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=ODvjcYwK; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c2-v6si564559plb.439.2018.01.31.11.40.19; Wed, 31 Jan 2018 11:40:34 -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; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=ODvjcYwK; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751866AbeAaTjT (ORCPT + 99 others); Wed, 31 Jan 2018 14:39:19 -0500 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:28767 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751641AbeAaTiX (ORCPT ); Wed, 31 Jan 2018 14:38:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1517427503; x=1548963503; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=YzKb2mCblw2yY8pbeZd0HYgN6YkkRDaNdFPySb/PH1w=; b=ODvjcYwK6eAxmwzHqQNfUlgTe9dRAxqoTAibfU2+N2D2u6AL9Hrh+t9O oYP5lklH3K7l1SkQgzBKj7BMu12BfqGfyWEH65cqoPtsZQHyR19BvzfUZ uhBCCdl+6kw0zekIm12aP+2OVTwYNuUFsa4JVR0gIWvqSnvhuV7BC8t8d c=; X-IronPort-AV: E=Sophos;i="5.46,441,1511827200"; d="scan'208";a="591808697" Received: from sea3-co-svc-lb6-vlan3.sea.amazon.com (HELO email-inbound-relay-2a-6e2fc477.us-west-2.amazon.com) ([10.47.22.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 31 Jan 2018 19:38:20 +0000 Received: from u54e1ad5160425a4b64ea.ant.amazon.com (pdx2-ws-svc-lb17-vlan3.amazon.com [10.247.140.70]) by email-inbound-relay-2a-6e2fc477.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id w0VJcGEw011080 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 31 Jan 2018 19:38:17 GMT Received: from u54e1ad5160425a4b64ea.ant.amazon.com (localhost [127.0.0.1]) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id w0VJcELv028691; Wed, 31 Jan 2018 20:38:14 +0100 Received: (from karahmed@localhost) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Submit) id w0VJcDq6028690; Wed, 31 Jan 2018 20:38:13 +0100 From: KarimAllah Ahmed To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org Cc: KarimAllah Ahmed , Asit Mallick , Arjan Van De Ven , Dave Hansen , Andi Kleen , Andrea Arcangeli , Linus Torvalds , Tim Chen , Thomas Gleixner , Dan Williams , Jun Nakajima , Paolo Bonzini , David Woodhouse , Greg KH , Andy Lutomirski , Ashok Raj Subject: [PATCH v5 5/5] KVM: SVM: Allow direct access to MSR_IA32_SPEC_CTRL Date: Wed, 31 Jan 2018 20:37:47 +0100 Message-Id: <1517427467-28567-6-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1517427467-28567-1-git-send-email-karahmed@amazon.de> References: <1517427467-28567-1-git-send-email-karahmed@amazon.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ 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. Cc: Asit Mallick Cc: Arjan Van De Ven Cc: Dave Hansen Cc: Andi Kleen Cc: Andrea Arcangeli Cc: Linus Torvalds Cc: Tim Chen Cc: Thomas Gleixner Cc: Dan Williams Cc: Jun Nakajima Cc: Paolo Bonzini Cc: David Woodhouse Cc: Greg KH Cc: Andy Lutomirski Cc: Ashok Raj Signed-off-by: KarimAllah Ahmed Signed-off-by: David Woodhouse --- v5: - Add SPEC_CTRL to direct_access_msrs. --- arch/x86/kvm/svm.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index bfbb7b9..0016a8a 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -184,6 +184,9 @@ struct vcpu_svm { u64 gs_base; } host; + u64 spec_ctrl; + bool save_spec_ctrl_on_exit; + u32 *msrpm; ulong nmi_iret_rip; @@ -250,6 +253,7 @@ static const struct svm_direct_access_msrs { { .index = MSR_SYSCALL_MASK, .always = true }, #endif { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, + { .index = MSR_IA32_SPEC_CTRL, .always = false }, { .index = MSR_IA32_PRED_CMD, .always = false }, { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, { .index = MSR_IA32_LASTINTFROMIP, .always = false }, @@ -1584,6 +1588,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 +3611,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 +3709,30 @@ 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; + + /* + * When it's written (to non-zero) for the first time, pass + * it through. This means we don't have to take the perf + * hit of saving it on vmexit for the common case of guests + * that don't use it. + */ + if (data && !svm->save_spec_ctrl_on_exit) { + svm->save_spec_ctrl_on_exit = true; + if (is_guest_mode(vcpu)) + break; + 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 +5001,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 +5102,19 @@ 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. + */ + if (svm->save_spec_ctrl_on_exit) + 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(); -- 2.7.4