Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp433575pxb; Wed, 14 Apr 2021 20:23:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwobQYjHqDxu4ctU4wwolz6WFnpnZbq2ckezQDPnimE1YNW1/YTqWLrBoG5QL/6YA1oNI4J X-Received: by 2002:a17:906:b85a:: with SMTP id ga26mr1207690ejb.366.1618457037739; Wed, 14 Apr 2021 20:23:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618457037; cv=none; d=google.com; s=arc-20160816; b=pizuzQ9B2fIQJA/wJUa8Ztt0LN4j6Dx/Mh/c/+Qf1U3KwrToaAYlHkcFLUbYYT147z ps4PCOIgBm0Xc6sis4FzZyeYYd4MJAZRkBY9ShaSwlN8TDMnokHkf9qg1HRbPNNTduqo hAlt1h9r00noBjppRyuMy+G1yHEPhBd2aabhOg/gPnTjFsWRtvGHHigjdX1Kkv4cxjXc GtJsi1yDZiL66f/becG/E4tWCeaSM10nQjDCqnjrzSkxx1KF0HskirKrB5PuZNwGgWHf Dvm/5h98sLzzyxBiT1/dKuONqKfs31lVpTMeTiwy2Cm7Tx0XyfP8Twaw0USvBXtelCAs 70vQ== 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 :ironport-sdr:ironport-sdr; bh=y5NwUgZxYeJVkBU5Xr9RA7VXMdhDkWFTXq69ybfoB9Y=; b=A1TRiPIlsoG/WwUTD3Jdiy7tqnHxWSVguEU6z6+VNIEofrRu9/obsJ6JqGf8uEafGn 3HlGCo7sJeAkbRGVRln6z/60egdLD8muNeY+dzxHXv+nfPu9n6x8sibY60Q35rbBfWB/ rjU3nb+FTgwKc6J3WBe9gUcC2QgGkj/7aLYzdOFa/ugbQaPF9OYr6hw0DKSU5gLUUi6K vI8bi4qDUiKCIm/8iS4Vsa2f20Y3WGbOxprff0dIi1muKhIGFC1ylJppOFG1F01fUHpM Q/+ulyDUnj+7Kenvnv/ObbXWHOPamrcnWYunO9EhA8aVT6gtfEuGSvSdQyeMHDo//Zba nmnw== 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 lu5si997364ejb.687.2021.04.14.20.23.35; Wed, 14 Apr 2021 20:23:57 -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 S230291AbhDODWH (ORCPT + 99 others); Wed, 14 Apr 2021 23:22:07 -0400 Received: from mga01.intel.com ([192.55.52.88]:10604 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230143AbhDODVu (ORCPT ); Wed, 14 Apr 2021 23:21:50 -0400 IronPort-SDR: 7gdELuwzEJqkgB1NMj5sWjgUshs3JBZ8tQe7E1Ypr4DZn2oPfypinFg0LSTvfvd+Qd/D/OpAK3 HDg6lr6lX5Lw== X-IronPort-AV: E=McAfee;i="6200,9189,9954"; a="215281605" X-IronPort-AV: E=Sophos;i="5.82,223,1613462400"; d="scan'208";a="215281605" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2021 20:21:28 -0700 IronPort-SDR: BX1HvQAkmyJ9v9ksZJzGGo/FUtXS/go0jM2DZ8urjLl7KoegO51cB5mkGWxLWoGMLvX/WMtaIx ev+aM8rB4FTw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,223,1613462400"; d="scan'208";a="425014140" Received: from clx-ap-likexu.sh.intel.com ([10.239.48.108]) by orsmga008.jf.intel.com with ESMTP; 14 Apr 2021 20:21:24 -0700 From: Like Xu To: peterz@infradead.org, Paolo Bonzini , Sean Christopherson Cc: andi@firstfloor.org, kan.liang@linux.intel.com, wei.w.wang@intel.com, eranian@google.com, liuxiangdong5@huawei.com, Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, Like Xu , Andi Kleen , Luwei Kang Subject: [PATCH v5 16/16] KVM: x86/pmu: Expose CPUIDs feature bits PDCM, DS, DTES64 Date: Thu, 15 Apr 2021 11:20:16 +0800 Message-Id: <20210415032016.166201-17-like.xu@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210415032016.166201-1-like.xu@linux.intel.com> References: <20210415032016.166201-1-like.xu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 d1d77985e889..241e41221701 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; @@ -378,20 +379,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 5ad12bb76296..e44eb57706e2 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2261,6 +2261,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; @@ -7287,6 +7298,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 (vmx_umip_emulated()) kvm_cpu_cap_set(X86_FEATURE_UMIP); -- 2.30.2