From: "Xin Li (Intel)" <xin@zytor.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
linux-doc@vger.kernel.org
Cc: pbonzini@redhat.com, seanjc@google.com, corbet@lwn.net,
tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com,
xin@zytor.com, luto@kernel.org, peterz@infradead.org,
andrew.cooper3@citrix.com, chao.gao@intel.com, hch@infradead.org
Subject: [PATCH v5 07/23] KVM: VMX: Set FRED MSR intercepts
Date: Wed, 23 Jul 2025 10:53:25 -0700 [thread overview]
Message-ID: <20250723175341.1284463-8-xin@zytor.com> (raw)
In-Reply-To: <20250723175341.1284463-1-xin@zytor.com>
From: Xin Li <xin3.li@intel.com>
On a userspace MSR filter change, set FRED MSR intercepts.
8 FRED MSRs, i.e., MSR_IA32_FRED_RSP[123], MSR_IA32_FRED_STKLVLS,
MSR_IA32_FRED_SSP[123] and MSR_IA32_FRED_CONFIG, are all safe to
be passthrough, because they all have a pair of corresponding host
and guest VMCS fields.
Both MSR_IA32_FRED_RSP0 and MSR_IA32_FRED_SSP0 are dedicated for
userspace event delivery only, IOW they are NOT used in any kernel
event delivery and the execution of ERETS. Thus KVM can run safely
with guest values in the two MSRs. As a result, save and restore of
their guest values are deferred until vCPU context switch and their
host values are restored upon host returning to userspace.
Signed-off-by: Xin Li <xin3.li@intel.com>
Signed-off-by: Xin Li (Intel) <xin@zytor.com>
Tested-by: Shan Kang <shan.kang@intel.com>
Tested-by: Xuelian Guo <xuelian.guo@intel.com>
---
Changes in v5:
* Skip execution of vmx_set_intercept_for_fred_msr() if FRED is
not available or enabled (Sean).
* Use 'set' instead of 'flag' as the variable name to indicate
whether MSR interception should be enabled (Sean).
* Add TB from Xuelian Guo.
---
arch/x86/kvm/vmx/vmx.c | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 58017bdf2ff0..4cdc2a0c1465 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4096,6 +4096,37 @@ void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)
}
}
+static void vmx_set_intercept_for_fred_msr(struct kvm_vcpu *vcpu)
+{
+ bool set = !guest_cpu_cap_has(vcpu, X86_FEATURE_FRED);
+
+ if (!kvm_cpu_cap_has(X86_FEATURE_FRED))
+ return;
+
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_RSP1, MSR_TYPE_RW, set);
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_RSP2, MSR_TYPE_RW, set);
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_RSP3, MSR_TYPE_RW, set);
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_STKLVLS, MSR_TYPE_RW, set);
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_SSP1, MSR_TYPE_RW, set);
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_SSP2, MSR_TYPE_RW, set);
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_SSP3, MSR_TYPE_RW, set);
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_CONFIG, MSR_TYPE_RW, set);
+
+ /*
+ * IA32_FRED_RSP0 and IA32_PL0_SSP (a.k.a. IA32_FRED_SSP0) are only used
+ * for delivering events when running userspace, while KVM always runs in
+ * kernel mode (the CPL is always 0 after any VM exit), thus KVM can run
+ * safely with guest IA32_FRED_RSP0 and IA32_PL0_SSP.
+ *
+ * As a result, no need to intercept IA32_FRED_RSP0 and IA32_PL0_SSP.
+ *
+ * Note, save and restore of IA32_PL0_SSP belong to CET supervisor context
+ * management no matter whether FRED is enabled or not. So leave its
+ * state management to CET code.
+ */
+ vmx_set_intercept_for_msr(vcpu, MSR_IA32_FRED_RSP0, MSR_TYPE_RW, set);
+}
+
void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
{
if (!cpu_has_vmx_msr_bitmap())
@@ -4143,6 +4174,8 @@ void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
vmx_set_intercept_for_msr(vcpu, MSR_IA32_FLUSH_CMD, MSR_TYPE_W,
!guest_cpu_cap_has(vcpu, X86_FEATURE_FLUSH_L1D));
+ vmx_set_intercept_for_fred_msr(vcpu);
+
/*
* x2APIC and LBR MSR intercepts are modified on-demand and cannot be
* filtered by userspace.
--
2.50.1
next prev parent reply other threads:[~2025-07-23 17:54 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-23 17:53 [PATCH v5 00/23] Enable FRED with KVM VMX Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 01/23] KVM: VMX: Add support for the secondary VM exit controls Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 02/23] KVM: VMX: Initialize VM entry/exit FRED controls in vmcs_config Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 03/23] KVM: VMX: Disable FRED if FRED consistency checks fail Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 04/23] x86/cea: Export an API to get per CPU exception stacks for KVM to use Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 05/23] KVM: VMX: Fix an indentation Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 06/23] KVM: VMX: Initialize VMCS FRED fields Xin Li (Intel)
2025-07-23 17:53 ` Xin Li (Intel) [this message]
2025-07-23 17:53 ` [PATCH v5 08/23] KVM: VMX: Save/restore guest FRED RSP0 Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 09/23] KVM: VMX: Add host MSR read/write helpers to streamline preemption logic Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 10/23] KVM: VMX: Add support for FRED context save/restore Xin Li (Intel)
2025-07-24 5:35 ` Chao Gao
2025-07-24 18:01 ` Xin Li
2025-08-02 17:15 ` [PATCH v5A " Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 11/23] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 12/23] KVM: VMX: Virtualize FRED event_data Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 13/23] KVM: VMX: Virtualize FRED nested exception tracking Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 14/23] Documentation: kvm: Fix a section number typo Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 15/23] KVM: x86: Save/restore the nested flag of an exception Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 16/23] KVM: x86: Mark CR4.FRED as not reserved Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 17/23] KVM: VMX: Dump FRED context in dump_vmcs() Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 18/23] KVM: x86: Advertise support for FRED Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 19/23] KVM: nVMX: Add support for the secondary VM exit controls Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 20/23] KVM: nVMX: Add FRED VMCS fields to nested VMX context handling Xin Li (Intel)
2025-07-24 6:50 ` Chao Gao
2025-07-31 7:24 ` Xin Li
2025-08-02 17:17 ` [PATCH v5A " Xin Li (Intel)
2025-08-02 17:33 ` Xin Li
2025-08-04 6:03 ` Xin Li
2025-07-23 17:53 ` [PATCH v5 21/23] KVM: nVMX: Add FRED-related VMCS field checks Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 22/23] KVM: nVMX: Allow VMX FRED controls Xin Li (Intel)
2025-07-23 17:53 ` [PATCH v5 23/23] KVM: nVMX: Add prerequisites to SHADOW_FIELD_R[OW] macros Xin Li (Intel)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250723175341.1284463-8-xin@zytor.com \
--to=xin@zytor.com \
--cc=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=chao.gao@intel.com \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.com \
--cc=hch@infradead.org \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).