Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3350527pxf; Sun, 28 Mar 2021 22:54:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzeo4j3Vx/olJf70Tes1YPkxz7D8QUCc7TK+zQJSW+JRsYhSSNcHwNUl0HnpVe5NHDv/brc X-Received: by 2002:a17:906:828e:: with SMTP id h14mr27090486ejx.529.1616997245849; Sun, 28 Mar 2021 22:54:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616997245; cv=none; d=google.com; s=arc-20160816; b=fFNtx9a1FvVnrzQpRbCFeTenptoNa7w0dPnzF5MiZQN8JPO7yxYHs190LsJkOox2gh 9Pqhpqk2RwJEA30VQb5MyzXp2UyXxKPxmqWGzq+PCXINpF3SzhRGBKGX5O5OTjbkSLUh X0yk5qPYlWOiPTLwMMvOo1hxNum6WDSEGwBi0Ja4jZYbUeMOBRzsCy11fF11sjMtlN+B ogDroIEdnItJh5kF63muKO7fI2zpPnCRI9jnIvRjxwrS1UUYZU1gtmp674eUDAJQdGDd axs1kG23Yj4oXwrtw1N5sSeQmoS7a1E8wYvbYZj653aO8UX+9UpqgA4ddelF2j7N7S0z PwRQ== 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=h838PiWt/lJ2vm/ye+LSTvg9MKbIoDHT6IS2VCNHqRM=; b=k5xUu60lHrJlMtbc6toE3RD+W7t4vvAjQb1QMjytK4Dzpn8vugDsNPJR6a5Xo3nuhZ GZnhPbrCKx2ayn25euEmoqRZw0UiOOGE92o2evJkR4+KCDFWABlTCi4PbrnDp3IQnr1Y gIJogvP+486x9aD2VJSIelov3l4umqbZAHbnB2qRcCDdQooaEXVsqawC3oZsNNpMheWy mm1SW61DyAZj41Dv4LfYYdwm+5O72HqFj/6VeNXsleS2Yqlgy2r3+soqxjnHfR6LJztj U0FdvoVoujAVJmgqBCiNfAdiH7N4gCwmjsv25wGvttlDpNgB/l1Wwfh7EXG67NPCzD0p cUAw== 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 k18si12411050eja.602.2021.03.28.22.53.43; Sun, 28 Mar 2021 22:54:05 -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 S229873AbhC2FvT (ORCPT + 99 others); Mon, 29 Mar 2021 01:51:19 -0400 Received: from mga07.intel.com ([134.134.136.100]:15632 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231466AbhC2Fuj (ORCPT ); Mon, 29 Mar 2021 01:50:39 -0400 IronPort-SDR: Tn1UkLFCzhxQtV72ITryQweGPspJ5QhNPCCZdtn05ZbiA/Hwt7szn9xX+zclLElRzEccgr1ce/ KwWw9uKdIDTg== X-IronPort-AV: E=McAfee;i="6000,8403,9937"; a="255478777" X-IronPort-AV: E=Sophos;i="5.81,285,1610438400"; d="scan'208";a="255478777" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2021 22:50:25 -0700 IronPort-SDR: AV7WsDBH8TmQnMrOqrdFcgHRYJGgDqdPzssgSfe8fSxWrtiKMNfpvvbK/2kjrIM9DjgoaarJKQ 2OUfLR6ePWiA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.81,285,1610438400"; d="scan'208";a="417507204" Received: from clx-ap-likexu.sh.intel.com ([10.239.48.108]) by orsmga008.jf.intel.com with ESMTP; 28 Mar 2021 22:50:21 -0700 From: Like Xu To: peterz@infradead.org, Sean Christopherson , Paolo Bonzini Cc: eranian@google.com, andi@firstfloor.org, kan.liang@linux.intel.com, wei.w.wang@intel.com, Wanpeng Li , Vitaly Kuznetsov , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, Like Xu , Andi Kleen , Luwei Kang Subject: [PATCH v4 16/16] KVM: x86/pmu: Expose CPUIDs feature bits PDCM, DS, DTES64 Date: Mon, 29 Mar 2021 13:41:37 +0800 Message-Id: <20210329054137.120994-17-like.xu@linux.intel.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210329054137.120994-1-like.xu@linux.intel.com> References: <20210329054137.120994-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, 35 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilities.h index d1d77985e889..df06da09f84c 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,33 @@ 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; + struct x86_pmu_capability x86_pmu; - if (boot_cpu_has(X86_FEATURE_PDCM)) - rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_cap); + perf_get_x86_pmu_capability(&x86_pmu); - perf_cap &= PMU_CAP_LBR_FMT; + return boot_cpu_has(X86_FEATURE_PEBS) && x86_pmu.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 b0f2cb790359..7cd9370357f9 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2262,6 +2262,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; @@ -7264,6 +7275,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.29.2