mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shivansh Dhiman <shivansh.dhiman@amd.com>
To: <seanjc@google.com>, <pbonzini@redhat.com>,
	<linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org>
Cc: <tglx@linutronix.de>, <mingo@redhat.com>, <bp@alien8.de>,
	<dave.hansen@linux.intel.com>, <x86@kernel.org>, <hpa@zytor.com>,
	<xin@zytor.com>, <andrew.cooper3@citrix.com>,
	<sohil.mehta@intel.com>, <dwmw@amazon.co.uk>, <yosry@kernel.org>,
	<nikunj.dadhania@amd.com>, <santosh.shukla@amd.com>,
	<shivansh.dhiman@amd.com>
Subject: [PATCH v3 3/9] KVM: SVM: Save/restore FRED_RSP0 for FRED supported guests
Date: Fri, 25 Sep 2026 11:40:11 +0000	[thread overview]
Message-ID: <20260925114017.2208531-4-shivansh.dhiman@amd.com> (raw)
In-Reply-To: <20260925114017.2208531-1-shivansh.dhiman@amd.com>

Hardware does not save/restore FRED_RSP0 for Non-SEV-ES guests. Save it
in svm_prepare_host_switch() and restore it in svm_prepare_switch_to_guest()
so that the correct physical CPU state is updated.

Also, synchronize the current value of MSR_IA32_FRED_RSP0 in hardware to the
kernel's local cache. Note that the desired host's RSP0 will be set when the
CPU exits to userspace for servicing vCPU tasks.

Co-developed-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
Signed-off-by: Shivansh Dhiman <shivansh.dhiman@amd.com>
---

Changes in v3:
* Keep the guest's FRED_RSP0 in a new vcpu_svm field, msr_guest_fred_rsp0.
  The VMCB has no FRED_RSP0 field for non-SEV-ES guests.
* Zero msr_guest_fred_rsp0 on RESET.
* Added the svm_manages_fred_rsp0() helper.

Changes in v2:
* Moved FRED MSRs save/restore logic from svm_vcpu_enter_exit() to
  svm_prepare_[host_switch/switch_to_guest]() to reduce some MSR accesses.
* While switching to host, added a safety check on guest_state_loaded.

---
 arch/x86/kvm/svm/svm.c | 28 +++++++++++++++++++++++++++-
 arch/x86/kvm/svm/svm.h |  2 ++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index e212f53d262a..c1263a3b3e73 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -1234,6 +1234,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
 	save->idtr.limit = 0xffff;
 
 	if (!init_event) {
+		svm->msr_guest_fred_rsp0 = 0;
 		save->fred_rsp1 = 0;
 		save->fred_rsp2 = 0;
 		save->fred_rsp3 = 0;
@@ -1458,6 +1459,11 @@ static void svm_srso_vm_init(void) { }
 static void svm_srso_vm_destroy(void) { }
 #endif
 
+static bool svm_manages_fred_rsp0(struct kvm_vcpu *vcpu)
+{
+	return !is_sev_es_guest(vcpu) && guest_cpu_cap_has(vcpu, X86_FEATURE_FRED);
+}
+
 static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -1495,12 +1501,32 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
 		sd->bp_spec_reduce_set = true;
 		msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
 	}
+
+	/* Hardware does not save/restore FRED_RSP0 for Non-SEV-ES guests. */
+	if (svm_manages_fred_rsp0(vcpu))
+		wrmsrq(MSR_IA32_FRED_RSP0, svm->msr_guest_fred_rsp0);
+
 	svm->guest_state_loaded = true;
 }
 
 static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
 {
-	to_svm(vcpu)->guest_state_loaded = false;
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	if (!svm->guest_state_loaded)
+		return;
+
+	/*
+	 * Hardware does not save/restore FRED_RSP0 for Non-SEV-ES guests.
+	 * Also, sync hardware MSR value to per-CPU cache. This helps in
+	 * restoring Host RSP0 when exiting to userspace in fred_update_rsp0().
+	 */
+	if (svm_manages_fred_rsp0(vcpu)) {
+		rdmsrq(MSR_IA32_FRED_RSP0, svm->msr_guest_fred_rsp0);
+		fred_sync_rsp0(svm->msr_guest_fred_rsp0);
+	}
+
+	svm->guest_state_loaded = false;
 }
 
 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index e958943b8162..581289cc6e0b 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -362,6 +362,8 @@ struct vcpu_svm {
 
 	bool guest_state_loaded;
 
+	u64 msr_guest_fred_rsp0;
+
 	bool avic_irq_window;
 	bool x2avic_msrs_intercepted;
 	bool lbr_msrs_intercepted;
-- 
2.43.0


  parent reply	other threads:[~2026-09-25 11:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-25 11:40 [PATCH v3 0/9] KVM: SVM: Enable FRED support Shivansh Dhiman
2026-09-25 11:40 ` [PATCH v3 1/9] KVM: SVM: Initialize FRED VMCB fields Shivansh Dhiman
2026-09-25 11:40 ` [PATCH v3 2/9] KVM: SVM: Disable interception of FRED MSRs for FRED supported guests Shivansh Dhiman
2026-09-25 11:40 ` Shivansh Dhiman [this message]
2026-09-25 11:40 ` [PATCH v3 4/9] KVM: SVM: Add support for saving and restoring FRED MSRs Shivansh Dhiman
2026-09-25 11:40 ` [PATCH v3 5/9] KVM: SVM: Populate FRED event data on event injection Shivansh Dhiman
2026-09-25 11:40 ` [PATCH v3 6/9] KVM: SVM: Support FRED nested exception injection Shivansh Dhiman
2026-09-25 11:40 ` [PATCH v3 7/9] KVM: SVM: Dump FRED context in dump_vmcb() Shivansh Dhiman
2026-09-25 11:40 ` [PATCH v3 8/9] KVM: SVM: Enable FRED virtualization Shivansh Dhiman
2026-09-25 11:40 ` [PATCH v3 9/9] KVM: SVM: Don't intercept ICEBP while the guest has FRED enabled Shivansh Dhiman

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=20260925114017.2208531-4-shivansh.dhiman@amd.com \
    --to=shivansh.dhiman@amd.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=dwmw@amazon.co.uk \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=nikunj.dadhania@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=santosh.shukla@amd.com \
    --cc=seanjc@google.com \
    --cc=sohil.mehta@intel.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=xin@zytor.com \
    --cc=yosry@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

all inboxes | Powered by JetHome®