Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1118947pxv; Fri, 16 Jul 2021 01:57:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwY2Cyj6L/fGeA+oinWvnxj41IkT++ABDmNnf+N4iQk6wwvsyKc72KRyuJCCV+baQF7wf0F X-Received: by 2002:a02:8783:: with SMTP id t3mr7965743jai.45.1626425872735; Fri, 16 Jul 2021 01:57:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626425872; cv=none; d=google.com; s=arc-20160816; b=SLXlFSHnsuS6Dgdus4jPNvZ1/QvFeAHGspjVpF8KIKaPy/6sZKAMFzKW5VYq62IibE 32b8RWCw0E9Fj4YuzmfyUs9TL6e7BH42XuSgBk7ZZV10lJ1llh+pkuH8oaEnO91Fvupi MhfpRF8liCcO3Obt2DGTCaL2LRvl8g143/lqryxvhT/tPpXsKUyjHccxp6PZo5ZztR5D NmqZRfpV77hrZCw7T9QDtunCPnLNFhEuI9ArPQ5e+a1Zv8FQtzdeRRAMPWdtmUEbeLu0 FVBjl5BpAmMG45EHSVyGgmGcHeG1u76WglBsRJ3M0DdhWoAkHOFkt7BSg0V1uixcLC2S BMcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=aWW7DNP226sqaSUyq1y95mYoo3nqNcd4IRRhnilwTNo=; b=MoKWJveIHHjk5dxrwx6wYt3K8v0S5qmCJNy8215dECHcfhem/a4HUL8F76djiZtRzc Mm0x0pn1FhD9xtFHBwK/NTdaR4iIUt+BOsvKZvp5/uniK+XK6z2fauS2BnVUwCjcIFA6 Zip4e2qWodDlYEsFHILTuX7v1gUHX/MrhYeEnPi9RUqvzslIKEkE4M7m7tpiMNgfYlax 4vGxBG3H5EpvHf5fKCv4/wM1bJezt+MhXaUAZ+WNzYiWED46YqRP7jY2/AK9hv1VhUt8 cUSYXzJnhCrORrh0oaGIv5p2e4/rEjvbsfvUwZ19Y/Sn6qMBsnk7fkEZwK2O9hO0iNf8 pbyA== 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 a21si9347005jam.67.2021.07.16.01.57.41; Fri, 16 Jul 2021 01:57:52 -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 S239179AbhGPI62 (ORCPT + 99 others); Fri, 16 Jul 2021 04:58:28 -0400 Received: from mga07.intel.com ([134.134.136.100]:48076 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239718AbhGPI6K (ORCPT ); Fri, 16 Jul 2021 04:58:10 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10046"; a="274526448" X-IronPort-AV: E=Sophos;i="5.84,244,1620716400"; d="scan'208";a="274526448" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jul 2021 01:55:15 -0700 X-IronPort-AV: E=Sophos;i="5.84,244,1620716400"; d="scan'208";a="495984199" Received: from vmm_a4_icx.sh.intel.com (HELO localhost.localdomain) ([10.239.53.245]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jul 2021 01:55:11 -0700 From: Zhu Lingshan To: peterz@infradead.org, pbonzini@redhat.com Cc: bp@alien8.de, seanjc@google.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, kan.liang@linux.intel.com, ak@linux.intel.com, wei.w.wang@intel.com, eranian@google.com, liuxiangdong5@huawei.com, linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, like.xu.linux@gmail.com, boris.ostrvsky@oracle.com, Like Xu , Luwei Kang , Zhu Lingshan Subject: [PATCH V8 18/18] KVM: x86/pmu: Expose CPUIDs feature bits PDCM, DS, DTES64 Date: Fri, 16 Jul 2021 16:53:25 +0800 Message-Id: <20210716085325.10300-19-lingshan.zhu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210716085325.10300-1-lingshan.zhu@intel.com> References: <20210716085325.10300-1-lingshan.zhu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Like Xu The CPUID features PDCM, DS and DTES64 are required for PEBS feature. KVM would expose CPUID feature PDCM, DS and DTES64 to guest when PEBS is supported in the KVM on the Ice Lake server platforms. Originally-by: Andi Kleen Co-developed-by: Kan Liang Signed-off-by: Kan Liang Co-developed-by: Luwei Kang Signed-off-by: Luwei Kang Signed-off-by: Like Xu Signed-off-by: Zhu Lingshan --- arch/x86/kvm/vmx/capabilities.h | 26 ++++++++++++++++++-------- arch/x86/kvm/vmx/vmx.c | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilities.h index 4705ad55abb5..41b0933abdb1 100644 --- a/arch/x86/kvm/vmx/capabilities.h +++ b/arch/x86/kvm/vmx/capabilities.h @@ -5,6 +5,7 @@ #include #include "lapic.h" +#include "pmu.h" extern bool __read_mostly enable_vpid; extern bool __read_mostly flexpriority_enabled; @@ -376,20 +377,29 @@ static inline bool vmx_pt_mode_is_host_guest(void) return pt_mode == PT_MODE_HOST_GUEST; } -static inline u64 vmx_get_perf_capabilities(void) +static inline bool vmx_pebs_supported(void) { - u64 perf_cap = 0; - - if (boot_cpu_has(X86_FEATURE_PDCM)) - rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_cap); - - perf_cap &= PMU_CAP_LBR_FMT; + return boot_cpu_has(X86_FEATURE_PEBS) && kvm_pmu_cap.pebs_vmx; +} +static inline u64 vmx_get_perf_capabilities(void) +{ /* * Since counters are virtualized, KVM would support full * width counting unconditionally, even if the host lacks it. */ - return PMU_CAP_FW_WRITES | perf_cap; + u64 perf_cap = PMU_CAP_FW_WRITES; + u64 host_perf_cap = 0; + + if (boot_cpu_has(X86_FEATURE_PDCM)) + rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap); + + perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT; + + if (vmx_pebs_supported()) + perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK; + + return perf_cap; } static inline u64 vmx_supported_debugctl(void) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d0af51c1389d..32dd90707b0d 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2224,6 +2224,17 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) if (!cpuid_model_is_consistent(vcpu)) return 1; } + if (data & PERF_CAP_PEBS_FORMAT) { + if ((data & PERF_CAP_PEBS_MASK) != + (vmx_get_perf_capabilities() & PERF_CAP_PEBS_MASK)) + return 1; + if (!guest_cpuid_has(vcpu, X86_FEATURE_DS)) + return 1; + if (!guest_cpuid_has(vcpu, X86_FEATURE_DTES64)) + return 1; + if (!cpuid_model_is_consistent(vcpu)) + return 1; + } ret = kvm_set_msr_common(vcpu, msr_info); break; @@ -7225,6 +7236,10 @@ static __init void vmx_set_cpu_caps(void) kvm_cpu_cap_clear(X86_FEATURE_INVPCID); if (vmx_pt_mode_is_host_guest()) kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT); + if (vmx_pebs_supported()) { + kvm_cpu_cap_check_and_set(X86_FEATURE_DS); + kvm_cpu_cap_check_and_set(X86_FEATURE_DTES64); + } if (!enable_sgx) { kvm_cpu_cap_clear(X86_FEATURE_SGX); -- 2.27.0