Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1090801pxb; Thu, 4 Mar 2021 03:04:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJya3E/MtRvBAUw92dJG6QzhGkIeDfw3aqqLRM+P+k07Libe+GmZNmc8mg8VxDXGgc/Z4b1j X-Received: by 2002:a05:6402:438f:: with SMTP id o15mr3686464edc.123.1614855848872; Thu, 04 Mar 2021 03:04:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614855848; cv=none; d=google.com; s=arc-20160816; b=A6JKnItusiro0ZtA5+t0CqNSH+oyZVK+5Me2XD7yUQFZ0M8C5orzmwNPfl7vclpA0e cUOEiLwMQmrZWSrHJpWPb4hGQMecgsCELDCtQaQ9WEQWyvQ3GDjamzl0ftTU6xdT1C/F j9kiQohn78t5Nbl/WxXp1Lc54+NUsR9VXxEk6NcfzuNkZ26EZOU1uIG3i/Fq0cS726Cs sjS2NEw9tiO4JZzvgmnKtuJg4tQ5niPfheQOaXndRptTObQ0vz01wJ7Y2k2LxvQUYYTm o6afAXcUP9EPRyeM0vJQnbq4KdqjRPIDTq+3ZD4+rP5M0tIrX7P5flMSNy/0MV7B+eAM KcxA== 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=lfZ/7Flh7lyPsZn4WLOZbSyDOf3Me4UNPthc5i69jH4=; b=O4Yt1WE7OFSa6CTx+Po9pxO+mD0o0mCcVYFF8ZysZLy6OZbs40caolcTfHMGZQPblW wadpkmbLyJRlIKkgWPrHLmNucIVx/nq5ox2gtgnNtKC5yNBwzZkXRjigcRXSLzFFXAAk d4MXTggWHzWNyDXsdDdP+n3h2GMNGTlvOHIvhO4byrzeYMrnwihfh8cYzcM+OAusBY28 WvAcX9E9gFEz+Ak+AJ0VfHvrv8wUBzzoqpNrX8hMmSDTEH+zPMCBBlWx5JHt05pvQmYW DxVDnt+hYc17XxnJv8PY8T6Li5K/cXe2k1Hh70X1Bo+GIxWLMSr4xOWsDg1qkGqjo9j7 W2ww== 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 o16si14906285ejd.213.2021.03.04.03.03.41; Thu, 04 Mar 2021 03:04:08 -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 S1575372AbhCCRex (ORCPT + 99 others); Wed, 3 Mar 2021 12:34:53 -0500 Received: from mga11.intel.com ([192.55.52.93]:43939 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242937AbhCCORw (ORCPT ); Wed, 3 Mar 2021 09:17:52 -0500 IronPort-SDR: j1QjgAT8Iqae2ikARb887Z9Fydk9cHuyIqITX3KrhsZQNSdyfLkn5HtTF9TdeDnRqSmjyTRKwQ Ya54bTTwD4SA== X-IronPort-AV: E=McAfee;i="6000,8403,9911"; a="183819036" X-IronPort-AV: E=Sophos;i="5.81,220,1610438400"; d="scan'208";a="183819036" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Mar 2021 06:05:49 -0800 IronPort-SDR: ecxHEOlHBFfFnjozlZD6z1qWXR9uIlSWO0SQaPqDmno39TC+Nj2JcvHWNMQea+FxTAahHPXyve TPTWlrK6zexw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.81,220,1610438400"; d="scan'208";a="399729490" Received: from clx-ap-likexu.sh.intel.com ([10.239.48.108]) by fmsmga008.fm.intel.com with ESMTP; 03 Mar 2021 06:05:45 -0800 From: Like Xu To: Peter Zijlstra , Paolo Bonzini , Sean Christopherson Cc: Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Kan Liang , Dave Hansen , wei.w.wang@intel.com, Borislav Petkov , kvm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, Like Xu Subject: [PATCH v3 9/9] KVM: x86: Add XSAVE Support for Architectural LBRs Date: Wed, 3 Mar 2021 21:57:55 +0800 Message-Id: <20210303135756.1546253-10-like.xu@linux.intel.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210303135756.1546253-1-like.xu@linux.intel.com> References: <20210303135756.1546253-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 On processors whose XSAVE feature set supports XSAVES and XRSTORS, the availability of support for Architectural LBR configuration state save and restore can be determined from CPUID.(EAX=0DH, ECX=1):EDX:ECX[bit 15]. The detailed leaf for Arch LBRs is enumerated in CPUID.(EAX=0DH, ECX=0FH). XSAVES provides a faster means than RDMSR for guest to read all LBRs. When guest IA32_XSS[bit 15] is set, the Arch LBRs state can be saved using XSAVES and restored by XRSTORS with the appropriate RFBM. If the KVM fails to pass-through the LBR msrs to the guest, the LBR msrs will be reset to prevent the leakage of host records via XSAVES. In this case, the guest results may be inaccurate as the legacy LBR. Signed-off-by: Like Xu --- arch/x86/kvm/vmx/pmu_intel.c | 2 ++ arch/x86/kvm/vmx/vmx.c | 2 ++ arch/x86/kvm/x86.c | 2 ++ 3 files changed, 6 insertions(+) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 48a817be60ab..08114f70c496 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -768,6 +768,8 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) return; warn: + if (kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR)) + wrmsrl(MSR_ARCH_LBR_DEPTH, lbr_desc->records.nr); pr_warn_ratelimited("kvm: vcpu-%d: fail to passthrough LBR.\n", vcpu->vcpu_id); } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 034708a3df20..ec4593e0ee6d 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7268,6 +7268,8 @@ static __init void vmx_set_cpu_caps(void) supported_xss = 0; if (!cpu_has_vmx_xsaves()) kvm_cpu_cap_clear(X86_FEATURE_XSAVES); + else if (kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR)) + supported_xss |= XFEATURE_MASK_LBR; /* CPUID 0x80000001 */ if (!cpu_has_vmx_rdtscp()) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d773836ceb7a..bca2e318ff24 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10433,6 +10433,8 @@ int kvm_arch_hardware_setup(void *opaque) if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) supported_xss = 0; + else + supported_xss &= host_xss; #define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f) cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_); -- 2.29.2