Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp13819895pxu; Mon, 4 Jan 2021 05:29:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJyqmC6vq0/uMOFn9v/1FJcY1da/fAWNsrvCuUyD1wbOqJp1aFj2XFIfm8VYozGFgOB1WAbs X-Received: by 2002:a17:906:e84:: with SMTP id p4mr54101532ejf.141.1609766960898; Mon, 04 Jan 2021 05:29:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609766960; cv=none; d=google.com; s=arc-20160816; b=stjANnojGH0jTy3osyRu/olIwy9zJ4bzM2G2I0JRahA+r9BGW4dCdK4+p/RqoGLrSH F7kPN+jAzSe/IrJ5qyG7zX34niyK2IYlAAXRJ7obEHUic5cRYYkBct0WWgA1y2xJ6t1m W3wFQuNjW7ED7OSVXdiTnVtl4UrNtxne8tcRpiJTGgfL02f79GMb784GK1lNIuJhD04I HXgOMJycgicSUm0xY91B+6HR/O+D8DwM5Tsbmqmtt+11TJpYgYbJdlsGWrg5ddnE5bfi rWwcidrD/ZeVPi+k8ZhAaOMkpfpEnWxsnJ0zC70zbzUnczqNQyD3WpbBJu0OFO9cL0hG aQlw== 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=NJAKt+I/sxaMWupPObCL8rq/oWuDW1OBQHjww0bQa2c=; b=LXPtYfUYQI8/AUgho9empsB9vJTuHCPyJYTgEO6c/mDOWrk8zYJnKDSMdr97Ewn2vg x2mIS/zjbTW7giH1sFaAXrg5Ry0DZK9iIHG/XQoqetkLgxsR/Eh0w6JU9zml7OE5XxHX LLb7HgH0p5IiJ9N1bDA8fD2jUwO/m5wR9tODwcF3KheLXdgD3N9hpCe8AzWgHDS1sqls MtvWWqJ1/xczMFIyTgC//UtW2Jgo3gT3V0kV8eEoQEy451qWJHq5JLxNHdTNbtYqovTO odvOsSe57hl3LM72zw530akxIUwmhEjxjJmpPuKaSMc/KqwCseiFPg0dbEycNrEvXX2q +mWQ== 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 j10si29990236edt.142.2021.01.04.05.28.57; Mon, 04 Jan 2021 05:29:20 -0800 (PST) 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 S1726908AbhADN1w (ORCPT + 99 others); Mon, 4 Jan 2021 08:27:52 -0500 Received: from mga07.intel.com ([134.134.136.100]:23246 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726662AbhADN1v (ORCPT ); Mon, 4 Jan 2021 08:27:51 -0500 IronPort-SDR: gvZlxIob5E1rHyJbhmn7g0TIJwGZhIilrnScVGjbFpo5DfENYue04XP6Qhbc03rZQPPJYRlvMM RMKn7ZtS46fA== X-IronPort-AV: E=McAfee;i="6000,8403,9853"; a="241034368" X-IronPort-AV: E=Sophos;i="5.78,474,1599548400"; d="scan'208";a="241034368" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jan 2021 05:22:32 -0800 IronPort-SDR: 2ClTuwdre+ashFI/IodTy4xYrURt7mpTaTansZZm+gyGJdoyZTAWxYdbvoDogR+FRo1gXZWzzB QvU1u/v4b50w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,474,1599548400"; d="scan'208";a="461944620" Received: from clx-ap-likexu.sh.intel.com ([10.239.48.108]) by fmsmga001.fm.intel.com with ESMTP; 04 Jan 2021 05:22:29 -0800 From: Like Xu To: Peter Zijlstra , Paolo Bonzini , eranian@google.com, kvm@vger.kernel.org Cc: Ingo Molnar , Sean Christopherson , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Andi Kleen , Kan Liang , wei.w.wang@intel.com, luwei.kang@intel.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 10/17] KVM: x86/pmu: Expose CPUIDs feature bits PDCM, DS, DTES64 Date: Mon, 4 Jan 2021 21:15:35 +0800 Message-Id: <20210104131542.495413-11-like.xu@linux.intel.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210104131542.495413-1-like.xu@linux.intel.com> References: <20210104131542.495413-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/pmu.h | 6 ++++++ arch/x86/kvm/vmx/capabilities.h | 17 ++++++++++++++++- arch/x86/kvm/vmx/vmx.c | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 067fef51760c..ee8f15cc4b5e 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -3,6 +3,7 @@ #define __KVM_X86_PMU_H #include +#include #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) #define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) @@ -16,6 +17,11 @@ #define VMWARE_BACKDOOR_PMC_APPARENT_TIME 0x10002 #define MAX_FIXED_COUNTERS 3 +static const struct x86_cpu_id vmx_icl_pebs_cpu[] = { + X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, NULL), + X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, NULL), + {} +}; struct kvm_event_hw_type_mapping { u8 eventsel; diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilities.h index 3a1861403d73..2f22ce34b165 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; @@ -369,13 +370,27 @@ static inline bool vmx_pt_mode_is_host_guest(void) return pt_mode == PT_MODE_HOST_GUEST; } +static inline bool vmx_pebs_supported(void) +{ + return boot_cpu_has(X86_FEATURE_PEBS) && x86_match_cpu(vmx_icl_pebs_cpu); +} + 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; + u64 value = PMU_CAP_FW_WRITES; + u64 perf_cap = 0; + + if (boot_cpu_has(X86_FEATURE_PDCM)) + rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_cap); + + if (vmx_pebs_supported()) + value |= perf_cap & PERF_CAP_PEBS_MASK; + + return value; } #endif /* __KVM_X86_VMX_CAPS_H */ diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index dbb0e49aae64..341794b67f9a 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2200,6 +2200,17 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_IA32_PERF_CAPABILITIES: if (data && !vcpu_to_pmu(vcpu)->version) 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 (boot_cpu_data.x86_model != guest_cpuid_model(vcpu)) + return 1; + } ret = kvm_set_msr_common(vcpu, msr_info); break; @@ -7277,6 +7288,10 @@ static __init void vmx_set_cpu_caps(void) kvm_cpu_cap_check_and_set(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