Received: by 10.223.164.202 with SMTP id h10csp2845955wrb; Tue, 28 Nov 2017 02:13:49 -0800 (PST) X-Google-Smtp-Source: AGs4zMYqJLth/aPu30t5FrbDcFxqEl5ZdCaZB6sOsujtTw8w+yeFcDEjlmbG3XOWxwOtsgjx4Woi X-Received: by 10.159.194.1 with SMTP id x1mr42493703pln.48.1511864029246; Tue, 28 Nov 2017 02:13:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511864029; cv=none; d=google.com; s=arc-20160816; b=Ymp5yKmr7g/RGwfPO7UfoSDPJV0jt51X3H8/AbPq27Ss4SalZcUk9d7ntr041aUgkW oQmO+Cyq4XrGSbTgeqAVb84k/MqIQqNlDmoCs5XqZSz8xoKJBd53I7p/JoxJ8RAGn0kq ojSJp455U+bbAFlV8F3qq8Dc5/pM3osd/UyevqvmBJmmUXE2ZufTdXP1Manoa8WrwGHK qVL4mZPWx3dQgK6QBTc7lPEXOsVYjbQSxggCCUoDtKTOZQQHhins/trRFOk2Q+dcXqCn uBQ3Puq8hcxPVen4Ndclpi7Ci/v8JbN0RjK3elj1WVjZWs3N4hxVgUBpNWdZ/fsRgQFp 0ZGA== 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:arc-authentication-results; bh=2KizyDI6VWPHedkuOtU6mT8vV8+TukwmPNdNKNvbOB8=; b=tKphBUdDJAOdnyjzXSaEdyNTwFqO8dEnuHamySR1a0MNE+26AU6WqoztDPp+K3j9YO UCEOpbxcZVYxHbcqXSkUA6kY3UX4b8Hwffbwm3HZuAKDlcbeF7GB9/xAvXOO2DVMxYIT 0k5rZOrEqqsur8/h/oYDx84Qr2Tkt8/zLeMwB+1WrrCAsZrMgrJ6H/FE/n+DmtNDX7CK Dl7gMtZDgIgoztAoR1FUMIY9bK57uw7rprz8YY7vRStsnwO6ox2Z7+uH3jFPr9mHSOsg HZnNePqzNxtnWxwiMV9G8MtttsiuUVETHcCmNd1vLlVfPlmIFvbPHjA6X0H7AopQO90L +mGg== 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 t11si24009991pgn.282.2017.11.28.02.13.37; Tue, 28 Nov 2017 02:13:49 -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 S1752118AbdK1KLZ (ORCPT + 78 others); Tue, 28 Nov 2017 05:11:25 -0500 Received: from mga01.intel.com ([192.55.52.88]:59069 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752484AbdK1KJa (ORCPT ); Tue, 28 Nov 2017 05:09:30 -0500 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Nov 2017 02:09:30 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,467,1505804400"; d="scan'208";a="7939953" Received: from vmm.bj.intel.com ([10.238.135.172]) by fmsmga001.fm.intel.com with ESMTP; 28 Nov 2017 02:09:28 -0800 From: Luwei Kang To: kvm@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, x86@kernel.org, pbonzini@redhat.com, rkrcmar@redhat.com, linux-kernel@vger.kernel.org, joro@8bytes.org, Luwei Kang Subject: [PATCH v3 5/9] KVM: x86: Add a function to disable/enable Intel PT MSRs intercept Date: Tue, 28 Nov 2017 04:23:58 +0800 Message-Id: <1511814242-12949-6-git-send-email-luwei.kang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1511814242-12949-1-git-send-email-luwei.kang@intel.com> References: <1511814242-12949-1-git-send-email-luwei.kang@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Intel Processor Trace MSRs(except IA32_RTIT_CTL) would be passthrough to guest when Intel PT is enable in guest. So we need this function to disable/enable intercept these MSRs. Signed-off-by: Luwei Kang --- arch/x86/kvm/vmx.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index d8ad68a..87a9f52 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -4976,6 +4976,41 @@ static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, } } +static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, + u32 msr, int type) +{ + int f = sizeof(unsigned long); + + if (!cpu_has_vmx_msr_bitmap()) + return; + + /* + * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals + * have the write-low and read-high bitmap offsets the wrong way round. + * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. + */ + if (msr <= 0x1fff) { + if (type & MSR_TYPE_R) + /* read-low */ + __set_bit(msr, msr_bitmap + 0x000 / f); + + if (type & MSR_TYPE_W) + /* write-low */ + __set_bit(msr, msr_bitmap + 0x800 / f); + + } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { + msr &= 0x1fff; + if (type & MSR_TYPE_R) + /* read-high */ + __set_bit(msr, msr_bitmap + 0x400 / f); + + if (type & MSR_TYPE_W) + /* write-high */ + __set_bit(msr, msr_bitmap + 0xc00 / f); + + } +} + /* * If a msr is allowed by L0, we should check whether it is allowed by L1. * The corresponding bit will be cleared unless both of L0 and L1 allow it. @@ -5031,6 +5066,40 @@ static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) msr, MSR_TYPE_R | MSR_TYPE_W); } +static void vmx_enable_intercept_for_msr(u32 msr, bool longmode_only) +{ + if (!longmode_only) + __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy, + msr, MSR_TYPE_R | MSR_TYPE_W); + __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode, + msr, MSR_TYPE_R | MSR_TYPE_W); +} + +static inline void pt_disable_intercept_for_msr(bool flag) +{ + u32 i, eax, ebx, ecx, edx; + + cpuid_count(0x14, 1, &eax, &ebx, &ecx, &edx); + + if (flag) { + vmx_disable_intercept_for_msr(MSR_IA32_RTIT_STATUS, false); + vmx_disable_intercept_for_msr(MSR_IA32_RTIT_OUTPUT_BASE, false); + vmx_disable_intercept_for_msr(MSR_IA32_RTIT_OUTPUT_MASK, false); + vmx_disable_intercept_for_msr(MSR_IA32_RTIT_CR3_MATCH, false); + for (i = 0; i < (eax & 0x7); i++) + vmx_disable_intercept_for_msr(MSR_IA32_RTIT_ADDR0_A + i, + false); + } else { + vmx_enable_intercept_for_msr(MSR_IA32_RTIT_STATUS, false); + vmx_enable_intercept_for_msr(MSR_IA32_RTIT_OUTPUT_BASE, false); + vmx_enable_intercept_for_msr(MSR_IA32_RTIT_OUTPUT_MASK, false); + vmx_enable_intercept_for_msr(MSR_IA32_RTIT_CR3_MATCH, false); + for (i = 0; i < (eax & 0x7); i++) + vmx_enable_intercept_for_msr(MSR_IA32_RTIT_ADDR0_A + i, + false); + } +} + static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active) { if (apicv_active) { -- 1.8.3.1 From 1585295498686551798@xxx Tue Nov 28 07:53:21 +0000 2017 X-GM-THRID: 1585295498686551798 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread