Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3128790pxj; Mon, 10 May 2021 19:49:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzX9XsHCuWzR6HjxQqa9uIlzPuzLt/RwRq4dJlv3qpokGQobhsAV+UBBCzrfzGbew0Fckay X-Received: by 2002:a17:906:6981:: with SMTP id i1mr169003ejr.289.1620701347968; Mon, 10 May 2021 19:49:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620701347; cv=none; d=google.com; s=arc-20160816; b=Jj5DhjLuFOsGt209QNQQ/SQdiSgPJ93QwVwZiBgkSBMXFhYXw0o+T8/FwaFvh4Xi74 e5iJavJ1rG9DllrRh6FbTPUrSUj0jMgFSnNGzjmd8paWyuoRwwQTAlpbGSI85/0E4pBI QDwYTrnqEkV/ojxoM9qZt/FDd2/jBpaMq202krSRxs6b3ioSf3LujqB5RHc0U5KFvs7Y PephxetyBCVivpXPrAHMN+K10AIiEb27s8e0zMuqxFfi5gGWUgV8wz3KhIKSJWwn9Kng aifKwqhgozuxpi7C0hm5Pi5M2wc/N0tGNYAHZFXsTUV8jqNmTHpfrGVjEo5t0bcGA/N3 asGQ== 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=8zoOcY9OrEiW+Pd9vSYLFlQRSjBDHbg8mtQb9op5mcc=; b=eneXK+YF9ddtVP4XWXuGM+NhiZzKBbYncw5DzNb3SI/cmI4zaYJUuViD8loK+wKa26 t5Sq/tf01+nZwBBNBkoA63B3i6wpVVJjdrdlwHc9MEm99tWQLcTIFeVMzRLRIOPT0h33 cYzUAfDXVZgpSakat+QSX+Y4d1q+K78r95RfCFEZkQ5y0v3WxmPGDadgMRrCf05BI+3U uU7+UN6o8lDLcoDaehAObEJxfc6ierm/3e6c/fBFq4iEp9E/FK7Gf2N1CLxG/PaJ1+pS V4HuwNsbo6oAkZqE1T5P6fziTmA7xbCyvBxHYNKAY6K5iBS9lb/jDXj8qRrT9WkzbgTI up/w== 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 s5si1367537edw.318.2021.05.10.19.48.44; Mon, 10 May 2021 19:49:07 -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 S231140AbhEKCp3 (ORCPT + 99 others); Mon, 10 May 2021 22:45:29 -0400 Received: from mga14.intel.com ([192.55.52.115]:39591 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230482AbhEKCpM (ORCPT ); Mon, 10 May 2021 22:45:12 -0400 IronPort-SDR: Eb1oAvnvvzK3UdiASXrsQ3KLFicZ3f8wgXbYxqRZqCKUCLwmqOESTot5uJCcSIt1+JUirAUdU3 nt9rS3E5SUEg== X-IronPort-AV: E=McAfee;i="6200,9189,9980"; a="199015663" X-IronPort-AV: E=Sophos;i="5.82,290,1613462400"; d="scan'208";a="199015663" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 May 2021 19:44:06 -0700 IronPort-SDR: 6pmPrqHBQ/9L12PJe0+cUqtKQDKaBh+hTGXVNGv7kiGjrSG5ZIdrfRct/nZOEg9H8LBHvmv3iD QQVWLKwHNQ/g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,290,1613462400"; d="scan'208";a="468592114" Received: from clx-ap-likexu.sh.intel.com ([10.239.48.108]) by fmsmga002.fm.intel.com with ESMTP; 10 May 2021 19:44:03 -0700 From: Like Xu To: Peter Zijlstra , Paolo Bonzini Cc: Borislav Petkov , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , weijiang.yang@intel.com, Kan Liang , 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 , Luwei Kang Subject: [PATCH v6 16/16] KVM: x86/pmu: Expose CPUIDs feature bits PDCM, DS, DTES64 Date: Tue, 11 May 2021 10:42:14 +0800 Message-Id: <20210511024214.280733-17-like.xu@linux.intel.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210511024214.280733-1-like.xu@linux.intel.com> References: <20210511024214.280733-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 8dee8a5fbc17..fd8c9822db9e 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 e11efe9d2ff4..8d7dcf0ce4a3 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2309,6 +2309,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; @@ -7338,6 +7349,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.31.1