Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp2490854ybg; Fri, 31 Jul 2020 00:48:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw0STLdUsah4Vx1yKJN61Cwlu4Xu9UnJ0n/9juXHocevGMh3k2nxEDIA9c3lvDIu82gxYi1 X-Received: by 2002:a50:9316:: with SMTP id m22mr194971eda.178.1596181693411; Fri, 31 Jul 2020 00:48:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596181693; cv=none; d=google.com; s=arc-20160816; b=X9djyL0lzNkEfTo2j45Gy0bnKIxgyPUSKLgDnsLfsR3YJ5WVfPbp+I17f4/fu/TqMt DdTuoGBgTta9rQp2ULsuWGdvCdHpqwcMO3URtqpqaegmfvln+Qweb69ItnXL1UTN/2FK k/toEASyEnnIr/z5sS6KDQOpo84ajM4NzVIdE912CQRt8KgnK5PqizUAtEnuepU/aEg7 QS2/pT1RAIdU6S5KhSp/ocCplN0r4/eB7i0/jO45u/2i2KfxxNmSYJN2p9ZrHAXATMVQ y2f+5u8w0jxISxjM0rz9/tKtIV31S8m3GGV0AuZng0FF/Xlxfm56CKwF9RqAEqLGgUY8 TggQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=TFTV2fvkbERzd7c7SDJLEICFRPKXCgdtrxgJBV2el8k=; b=JbBCDjwQhh4hbw/NAN+XL0E/s7z/FlP2add5FhO92MS0UcfaqbHS3nc9R+AHtdAOR8 VwUnjzAozf9nDX38LxG37ChZTRbYq+clLA5edWLspLF7ffOkL9q9gqTSRQGLsRvnF6vi JKC/XnLCCJ2PjQDjJNPMoxrnaHHd6Oh9gob9BKMjIJlRLTQwkMxfu/ED7SQjXlbw8zZf oKQBa4fbbH5i8honpB7L2HnTmxw3Xq96R+66Wf21VphOh56QSlyyFDDDofCLp+oNOVJi hGKd08byBH7U6qnMJ/cltAXNvZOrpuAyBezsWVkkNaoA1VRLwA+vCXWYPLdqxMPao4sh UC5Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l13si4867063edq.209.2020.07.31.00.47.51; Fri, 31 Jul 2020 00:48:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731831AbgGaHqW (ORCPT + 99 others); Fri, 31 Jul 2020 03:46:22 -0400 Received: from mga11.intel.com ([192.55.52.93]:55474 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731821AbgGaHqS (ORCPT ); Fri, 31 Jul 2020 03:46:18 -0400 IronPort-SDR: nvoGIYaPkWX8pvYsTqbpYanNaVvTLqw3IIEcJGCHXxOJkeukAHG7FeeCjO0Rhye5xGFUuvRatH FnlJEkow6DKw== X-IronPort-AV: E=McAfee;i="6000,8403,9698"; a="149570538" X-IronPort-AV: E=Sophos;i="5.75,417,1589266800"; d="scan'208";a="149570538" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jul 2020 00:46:18 -0700 IronPort-SDR: zfbK1Cz6gFXSxE0PUhamrX+tM7QB8XadwU5JH1kcfZ4iubYKL4SXyHDF87TWUSRiEt/tvw3sFs PeGpUIVHubqQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,417,1589266800"; d="scan'208";a="323160606" Received: from sqa-gate.sh.intel.com (HELO clx-ap-likexu.tsp.org) ([10.239.48.212]) by fmsmga002.fm.intel.com with ESMTP; 31 Jul 2020 00:46:16 -0700 From: Like Xu To: kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Wanpeng Li , Jim Mattson Cc: Vitaly Kuznetsov , Joerg Roedel , wei.w.wang@intel.com, linux-kernel@vger.kernel.org, Like Xu Subject: [PATCH 4/6] KVM: vmx/pmu: Add MSR_ARCH_LBR_CTL emulation for Arch LBR Date: Fri, 31 Jul 2020 15:44:00 +0800 Message-Id: <20200731074402.8879-5-like.xu@linux.intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20200731074402.8879-1-like.xu@linux.intel.com> References: <20200731074402.8879-1-like.xu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Arch LBRs are enabled by setting MSR_ARCH_LBR_CTL.LBREn to 1. On processors that support Arch LBR, MSR_IA32_DEBUGCTLMSR[bit 0] has no meaning. It can be written to 0 or 1, but reads will always return 0. A new guest state field named "Guest IA32_LBR_CTL" has been added to enhance guest LBR usage and the guest value of MSR_ARCH_LBR_CTL is written to this field on all VM exits. Signed-off-by: Like Xu --- arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/vmx/pmu_intel.c | 13 +++++++++++++ arch/x86/kvm/vmx/vmx.c | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index cd7de4b401fe..27f53c81a17f 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -243,6 +243,8 @@ enum vmcs_field { GUEST_BNDCFGS_HIGH = 0x00002813, GUEST_IA32_RTIT_CTL = 0x00002814, GUEST_IA32_RTIT_CTL_HIGH = 0x00002815, + GUEST_IA32_LBR_CTL = 0x00002816, + GUEST_IA32_LBR_CTL_HIGH = 0x00002817, HOST_IA32_PAT = 0x00002c00, HOST_IA32_PAT_HIGH = 0x00002c01, HOST_IA32_EFER = 0x00002c02, diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 8021fbdbd618..289c267732bd 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -19,6 +19,7 @@ #include "pmu.h" #define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0) +#define MSR_ARCH_LBR_CTL_MASK 0x7f000f static struct kvm_event_hw_type_mapping intel_arch_events[] = { /* Index must match CPUID 0x0A.EBX bit vector */ @@ -232,6 +233,7 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) ret = pmu->version > 1; break; case MSR_ARCH_LBR_DEPTH: + case MSR_ARCH_LBR_CTL: ret = guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR); break; default: @@ -398,6 +400,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_ARCH_LBR_DEPTH: msr_info->data = lbr_desc->records.nr; return 0; + case MSR_ARCH_LBR_CTL: + msr_info->data = vmcs_read64(GUEST_IA32_LBR_CTL); + return 0; default: if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) { @@ -466,6 +471,14 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) lbr_desc->arch_lbr_reset = true; __set_bit(INTEL_GUEST_LBR_INUSE, pmu->pmc_in_use); return 0; + case MSR_ARCH_LBR_CTL: + if (data & ~MSR_ARCH_LBR_CTL_MASK) + return 1; + vmcs_write64(GUEST_IA32_LBR_CTL, data); + if (intel_pmu_lbr_is_enabled(vcpu) && !lbr_desc->event && + (data & DEBUGCTLMSR_LBR)) + intel_pmu_create_guest_lbr_event(vcpu); + return 0; default: if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) { diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 6d8b1d98ae1d..9f42553b1e11 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2054,6 +2054,14 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) __func__, data); data &= ~DEBUGCTLMSR_BTF; } + + /* + * For Arch LBR, IA32_DEBUGCTL[bit 0] has no meaning. + * It can be written to 0 or 1, but reads will always return 0. + */ + if (guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR)) + data &= ~DEBUGCTLMSR_LBR; + vmcs_write64(GUEST_IA32_DEBUGCTL, data); if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event && (data & DEBUGCTLMSR_LBR)) -- 2.21.3