Skip to contentSkip to conversation

KVM: SVM: Enable FRED support

PATCH Superseded Shivansh Dhiman 1 participant 9 messages Apr 2 – Apr 24

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: SVM: Enable FRED support

This series adds SVM support for FRED (Flexible Return and Event Delivery) virtualization in KVM.

FRED introduces simplified privilege level transitions to replace IDT-based event delivery and IRET returns, providing lower latency event handling while ensuring complete supervisor context on delivery and full user context on return. FRED defines event delivery for both ring 3->0 and ring 0->0 transitions, and introduces ERETU for returning to ring 3 and ERETS for remaining in ring 0.

AMD hardware extends the VMCB to support FRED virtualization with dedicated save area fields for FRED MSRs (RSP0-3, SSP1-3, STKLVLS, CONFIG) and control fields for event injection data (EXITINTDATA, EVENTINJDATA).

The implementation spans seven patches. The important changes are:

1) Extend VMCB structures with FRED fields mentioned above and disable MSR interception for FRED-enabled guests to avoid unnecessary VM exits.

2) Support for nested exceptions where we populate event injection data when delivering exceptions like page faults and debug traps.

Changes in v2:
- Modified the zeroing of FRED MSRs from INIT to RESET and updated the commit message (Sean Christopherson).
- Used guest_cpu_cap_has() instead of checking VMCB bit in recalc intercepts (Sean Christopherson).
- Variable rename from 'fred_enable' to 'intercept' (Sean Christopherson).
- 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.
- Removed the need for a new function parameter 'reinject_on_vmexit' in svm_complete_interrupts() (Paolo Bonzini).
- Replace variable 'nested' with 'is_nested' (Sean Christopherson).
- Formatted variables in reverse fir-tree order (Sean Christopherson).
- Gate all the FRED MSRs through guest_cpu_cap_has() while dumping VMCB (Sean Christopherson).
- Confined the enabling of vFRED to svm_vcpu_after_set_cpuid() only (Sean Christopherson).

This series is based on top of FRED support for VMX patchset [1], patches 1-17. The VMX patchset was rebased on top of v6.18.0 kernel. Previous version v1 of this series can be found here:
https://lore.kernel.org/kvm/[email protected]/

[1] https://lore.kernel.org/kvm/[email protected]

Regards,
Shivansh
---
Neeraj Upadhyay (3):
KVM: SVM: Initialize FRED VMCB fields
KVM: SVM: Populate FRED event data on event injection
KVM: SVM: Support FRED nested exception injection

Shivansh Dhiman (4):
KVM: SVM: Disable interception of FRED MSRs for FRED supported guests
KVM: SVM: Save restore FRED_RSP0 for FRED supported guests
KVM: SVM: Dump FRED context in dump_vmcb()
KVM: SVM: Enable FRED virtualization

 arch/x86/include/asm/svm.h |  35 ++++++++++-
 arch/x86/kvm/svm/svm.c     | 122 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 152 insertions(+), 5 deletions(-)

base-commit: f76e83ecf6bce6d3793f828d92170b69e636f3c9

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/7 KVM: SVM: Initialize FRED VMCB fields

From: Neeraj Upadhyay <[email protected]>

The upcoming AMD FRED (Flexible Return and Event Delivery) feature introduces several new fields to the VMCB save area. These fields include FRED-specific stack pointers (fred_rsp[0-3], fred_ssp[1-3]), stack level tracking (fred_stklvls), and configuration (fred_config).

All FRED MSRs are zeroed on RESET. Reproduce the HW behavior here to ensure that vCPU starts with a valid FRED state. Also update the size of save areas of VMCB.

---

Changes in v2:
- Modified the zeroing of FRED MSRs from INIT to RESET and updated the commit message (Sean Christopherson).

---
 arch/x86/include/asm/svm.h | 33 ++++++++++++++++++++++++++++++---
 arch/x86/kvm/svm/svm.c     | 12 ++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)
arch/x86/include/asm/svm.h +30 -3
index 17f6c3fedeee..a42ed39aa8fb 100644
@@ -165,7 +165,10 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 	u8 reserved_9[22];
 	u64 allowed_sev_features;	/* Offset 0x138 */
 	u64 guest_sev_features;		/* Offset 0x140 */
-	u8 reserved_10[664];
+	u8 reserved_10[40];
+	u64 exit_int_data;		/* Offset 0x170 */
+	u64 event_inj_data;
+	u8 reserved_11[608];
 	/*
 	 * Offset 0x3e0, 32 bytes reserved
 	 * for use by hypervisor/software.
@@ -360,6 +363,18 @@ struct vmcb_save_area {
 	u64 last_excp_to;
 	u8 reserved_0x298[72];
 	u64 spec_ctrl;		/* Guest version of SPEC_CTRL at 0x2E0 */
+	u8 reserved_0x2e8[448];
+	u64 guest_exit_int_data;        /* GUEST_EXITINTDATA 0x4A8 */
+	u64 guest_event_inj_data;
+	u64 fred_rsp0;
+	u64 fred_rsp1;
+	u64 fred_rsp2;
+	u64 fred_rsp3;
+	u64 fred_stklvls;
+	u64 fred_ssp1;
+	u64 fred_ssp2;
+	u64 fred_ssp3;
+	u64 fred_config;
 } __packed;
 
 /* Save area definition for SEV-ES and SEV-SNP guests */
@@ -472,6 +487,18 @@ struct sev_es_save_area {
 	u8 fpreg_x87[80];
 	u8 fpreg_xmm[256];
 	u8 fpreg_ymm[256];
+	u8 reserved_0x670[568];
+	u64 guest_exit_int_data;        /* GUEST_EXITINTDATA 0x8A8 */
+	u64 guest_event_inj_data;
+	u64 fred_rsp0;
+	u64 fred_rsp1;
+	u64 fred_rsp2;
+	u64 fred_rsp3;
+	u64 fred_stklvls;
+	u64 fred_ssp1;
+	u64 fred_ssp2;
+	u64 fred_ssp3;
+	u64 fred_config;
 } __packed;
 
 struct ghcb_save_area {
@@ -542,9 +569,9 @@ struct vmcb {
 	};
 } __packed;
 
-#define EXPECTED_VMCB_SAVE_AREA_SIZE		744
+#define EXPECTED_VMCB_SAVE_AREA_SIZE		1280
 #define EXPECTED_GHCB_SAVE_AREA_SIZE		1032
-#define EXPECTED_SEV_ES_SAVE_AREA_SIZE		1648
+#define EXPECTED_SEV_ES_SAVE_AREA_SIZE		2304
 #define EXPECTED_VMCB_CONTROL_AREA_SIZE		1024
 #define EXPECTED_GHCB_SIZE			PAGE_SIZE
 
arch/x86/kvm/svm/svm.c +12 -0
index f4ccb3e66635..cf6ba59d6d1e 100644
@@ -1110,6 +1110,18 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
 	save->idtr.base = 0;
 	save->idtr.limit = 0xffff;
 
+	if (!init_event) {
+		save->fred_rsp0 = 0;
+		save->fred_rsp1 = 0;
+		save->fred_rsp2 = 0;
+		save->fred_rsp3 = 0;
+		save->fred_stklvls = 0;
+		save->fred_ssp1 = 0;
+		save->fred_ssp2 = 0;
+		save->fred_ssp3 = 0;
+		save->fred_config = 0;
+	}
+
 	init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
 	init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/7 KVM: SVM: Disable interception of FRED MSRs for FRED supported guests

In reply to Shivansh Dhiman

The FRED (Flexible Return and Event Delivery) feature introduces a new set of MSRs for managing its state, such as MSR_IA32_FRED_CONFIG and the various stack pointer MSRs.

For a guest that has FRED enabled via its CPUID bits, the guest OS expects to be able to directly read and write these MSRs. Intercepting these accesses would cause unnecessary VM-Exits and performance overhead. In addition, the state of the MSRs at any point should always correspond to the context (host or guest) which is running. Otherwise, the event delivery could refer to wrong MSR values.

---

Changes in v2:
- Used guest_cpu_cap_has() instead of checking VMCB bit (Sean Christopherson).
- Variable rename from 'fred_enable' to 'intercept' (Sean Christopherson).

---
 arch/x86/kvm/svm/svm.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/x86/kvm/svm/svm.c +17 -0
index cf6ba59d6d1e..5e85b2853ad6 100644
@@ -727,6 +727,21 @@ void svm_vcpu_free_msrpm(void *msrpm)
 	__free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE));
 }
 
+static void svm_recalc_fred_msr_intercepts(struct kvm_vcpu *vcpu)
+{
+	bool intercept = guest_cpu_cap_has(vcpu, X86_FEATURE_FRED);
+
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_RSP0, MSR_TYPE_RW, !intercept);
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_RSP1, MSR_TYPE_RW, !intercept);
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_RSP2, MSR_TYPE_RW, !intercept);
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_RSP3, MSR_TYPE_RW, !intercept);
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_STKLVLS, MSR_TYPE_RW, !intercept);
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_SSP1, MSR_TYPE_RW, !intercept);
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_SSP2, MSR_TYPE_RW, !intercept);
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_SSP3, MSR_TYPE_RW, !intercept);
+	svm_set_intercept_for_msr(vcpu, MSR_IA32_FRED_CONFIG, MSR_TYPE_RW, !intercept);
+}
+
 static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -795,6 +810,8 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
 	if (sev_es_guest(vcpu->kvm))
 		sev_es_recalc_msr_intercepts(vcpu);
 
+	svm_recalc_fred_msr_intercepts(vcpu);
+
 	/*
 	 * x2APIC intercepts are modified on-demand and cannot be filtered by
 	 * userspace.
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/7 KVM: SVM: Save restore FRED_RSP0 for FRED supported guests

In reply to Shivansh Dhiman

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.

---

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 | 24 +++++++++++++++++++++++-

 1 file changed, 23 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +23 -1
index 5e85b2853ad6..3f7f8fb0dfac 100644
@@ -1383,12 +1383,34 @@ 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 (!sev_es_guest(vcpu->kvm) && guest_cpu_cap_has(vcpu, X86_FEATURE_FRED))
+		wrmsrq(MSR_IA32_FRED_RSP0, svm->vmcb->save.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 (!sev_es_guest(vcpu->kvm) && guest_cpu_cap_has(vcpu, X86_FEATURE_FRED)) {
+		rdmsrq(MSR_IA32_FRED_RSP0, svm->vmcb->save.fred_rsp0);
+		fred_sync_rsp0(svm->vmcb->save.fred_rsp0);
+	}
+
+	svm->guest_state_loaded = false;
 }
 
 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/7 KVM: SVM: Populate FRED event data on event injection

In reply to Shivansh Dhiman

From: Neeraj Upadhyay <[email protected]>

Set injected-event data (in EVENTINJDATA) when injecting an event, use EXITINTDATA for populating the injected-event data during reinjection.

Unlike IDT using some extra CPU register as part of an event context, e.g., %cr2 for #PF, FRED saves a complete event context in its stack frame, e.g., FRED saves the faulting linear address of a #PF into the event data field defined in its stack frame.

Populate the EVENTINJDATA during event injection. The event data will be pushed into a FRED stack frame for VM entries that inject an event using FRED event delivery.

---

Changes in v2:
- Removed the need for a new function parameter 'reinject_on_vmexit' in svm_complete_interrupts() (Paolo Bonzini).

---
 arch/x86/kvm/svm/svm.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +14 -1
index 3f7f8fb0dfac..2da493f9dd8b 100644
@@ -374,6 +374,10 @@ static void svm_inject_exception(struct kvm_vcpu *vcpu)
 		| SVM_EVTINJ_VALID
 		| (ex->has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
 		| SVM_EVTINJ_TYPE_EXEPT;
+
+	if (is_fred_enabled(vcpu))
+		svm->vmcb->control.event_inj_data = ex->event_data;
+
 	svm->vmcb->control.event_inj_err = ex->error_code;
 }
 
@@ -4134,6 +4138,7 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
 		break;
 	case SVM_EXITINTINFO_TYPE_EXEPT: {
 		u32 error_code = 0;
+		u64 event_data = 0;
 
 		/*
 		 * Never re-inject a #VC exception.
@@ -4144,9 +4149,16 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
 		if (exitintinfo & SVM_EXITINTINFO_VALID_ERR)
 			error_code = svm->vmcb->control.exit_int_info_err;
 
+		/*
+		 * FRED requires an additional field to pass injected-event
+		 * data to the guest.
+		 */
+		if (is_fred_enabled(vcpu) && (vector == PF_VECTOR || vector == DB_VECTOR))
+			event_data = svm->vmcb->control.exit_int_data;
+
 		kvm_requeue_exception(vcpu, vector,
 				      exitintinfo & SVM_EXITINTINFO_VALID_ERR,
-				      error_code, false, 0);
+				      error_code, false, event_data);
 		break;
 	}
 	case SVM_EXITINTINFO_TYPE_INTR:
@@ -4168,6 +4180,7 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu)
 
 	control->exit_int_info = control->event_inj;
 	control->exit_int_info_err = control->event_inj_err;
+	control->exit_int_data = control->event_inj_data;
 	control->event_inj = 0;
 	svm_complete_interrupts(vcpu);
 }
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 5/7 KVM: SVM: Support FRED nested exception injection

In reply to Shivansh Dhiman

From: Neeraj Upadhyay <[email protected]>

Set the SVM nested exception bit in EVENT_INJECTION_CTL when injecting a nested exception using FRED event delivery to ensure:
1) A nested exception is injected on a correct stack level.
2) The nested bit defined in FRED stack frame is set.

The event stack level used by FRED event delivery depends on whether the event was a nested exception encountered during delivery of an earlier event, because a nested exception is "regarded" as happening on ring 0. E.g., when #PF is configured to use stack level 1 in IA32_FRED_STKLVLS MSR:
- nested #PF will be delivered on the stack pointed by FRED_RSP1 MSR when encountered in ring 3 and ring 0.
- normal #PF will be delivered on the stack pointed by FRED_RSP0 MSR when encountered in ring 3.

The SVM nested-exception support ensures a correct event stack level is chosen when a VM entry injects a nested exception.

---

Changes in v2:
- Replace variable 'nested' with 'is_nested' (Sean Christopherson).
- Formatted variables in reverse fir-tree order (Sean Christopherson).

---
 arch/x86/include/asm/svm.h | 1 +
 arch/x86/kvm/svm/svm.c     | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/include/asm/svm.h +1 -0
index a42ed39aa8fb..c0f6523e2b2d 100644
@@ -656,6 +656,7 @@ static inline void __unused_size_checks(void)
 
 #define SVM_EVTINJ_VALID (1 << 31)
 #define SVM_EVTINJ_VALID_ERR (1 << 11)
+#define SVM_EVTINJ_NESTED_EXCEPTION    (1 << 13)
 
 #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK
 #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK
arch/x86/kvm/svm/svm.c +4 -1
index 2da493f9dd8b..39d1177a24f0 100644
@@ -362,6 +362,7 @@ static int svm_update_soft_interrupt_rip(struct kvm_vcpu *vcpu)
 static void svm_inject_exception(struct kvm_vcpu *vcpu)
 {
 	struct kvm_queued_exception *ex = &vcpu->arch.exception;
+	bool is_nested = is_fred_enabled(vcpu) && ex->nested;
 	struct vcpu_svm *svm = to_svm(vcpu);
 
 	kvm_deliver_exception_payload(vcpu, ex);
@@ -373,6 +374,7 @@ static void svm_inject_exception(struct kvm_vcpu *vcpu)
 	svm->vmcb->control.event_inj = ex->vector
 		| SVM_EVTINJ_VALID
 		| (ex->has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
+		| (is_nested ? SVM_EVTINJ_NESTED_EXCEPTION : 0)
 		| SVM_EVTINJ_TYPE_EXEPT;
 
 	if (is_fred_enabled(vcpu))
@@ -4158,7 +4160,8 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
 
 		kvm_requeue_exception(vcpu, vector,
 				      exitintinfo & SVM_EXITINTINFO_VALID_ERR,
-				      error_code, false, event_data);
+				      error_code, exitintinfo & SVM_EVTINJ_NESTED_EXCEPTION,
+				      event_data);
 		break;
 	}
 	case SVM_EXITINTINFO_TYPE_INTR:
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 6/7 KVM: SVM: Dump FRED context in dump_vmcb()

In reply to Shivansh Dhiman

Add fields related to FRED to dump_vmcb() to dump FRED context.

---

Changes in v2:
- Gate all the FRED MSRs through guest_cpu_cap_has() (Sean Christopherson).

---
 arch/x86/kvm/svm/svm.c | 46 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
arch/x86/kvm/svm/svm.c +46 -0
index 39d1177a24f0..7d411df3dd78 100644
@@ -3343,6 +3343,11 @@ static void dump_vmcb(struct kvm_vcpu *vcpu)
 	pr_err("%-20s%016llx\n", "allowed_sev_features:", control->allowed_sev_features);
 	pr_err("%-20s%016llx\n", "guest_sev_features:", control->guest_sev_features);
 
+	if (guest_cpu_cap_has(vcpu, X86_FEATURE_FRED)) {
+		pr_err("%-20s%016llx\n", "exit_int_data:", control->exit_int_data);
+		pr_err("%-20s%016llx\n", "event_inj_data:", control->event_inj_data);
+	}
+
 	if (sev_es_guest(vcpu->kvm)) {
 		save = sev_decrypt_vmsa(vcpu);
 		if (!save)
@@ -3457,6 +3462,27 @@ static void dump_vmcb(struct kvm_vcpu *vcpu)
 		       "r14:", vmsa->r14, "r15:", vmsa->r15);
 		pr_err("%-15s %016llx %-13s %016llx\n",
 		       "xcr0:", vmsa->xcr0, "xss:", vmsa->xss);
+
+		if (guest_cpu_cap_has(vcpu, X86_FEATURE_FRED)) {
+			pr_err("%-27s %d %-18s%016llx\n",
+			       "is_fred_enabled:", is_fred_enabled(vcpu),
+			       "guest_evntinjdata:", vmsa->guest_event_inj_data);
+			pr_err("%-12s %016llx %-18s%016llx\n",
+			       "fred_config:", vmsa->fred_config,
+			       "guest_exitintdata:", vmsa->guest_exit_int_data);
+			pr_err("%-15s %016llx %-13s %016llx\n",
+			       "fred_rsp0:", vmsa->fred_rsp0,
+			       "fred_rsp1:", vmsa->fred_rsp1);
+			pr_err("%-15s %016llx %-13s %016llx\n",
+			       "fred_rsp2:", vmsa->fred_rsp2,
+			       "fred_rsp3:", vmsa->fred_rsp3);
+			pr_err("%-15s %016llx %-13s %016llx\n",
+			       "fred_stklvls:", vmsa->fred_stklvls,
+			       "fred_ssp1:", vmsa->fred_ssp1);
+			pr_err("%-15s %016llx %-13s %016llx\n",
+			       "fred_ssp2:", vmsa->fred_ssp2,
+			       "fred_ssp3:", vmsa->fred_ssp3);
+		}
 	} else {
 		pr_err("%-15s %016llx %-13s %016lx\n",
 		       "rax:", save->rax, "rbx:",
@@ -3484,6 +3510,26 @@ static void dump_vmcb(struct kvm_vcpu *vcpu)
 		       "r14:", vcpu->arch.regs[VCPU_REGS_R14],
 		       "r15:", vcpu->arch.regs[VCPU_REGS_R15]);
 #endif
+		if (guest_cpu_cap_has(vcpu, X86_FEATURE_FRED)) {
+			pr_err("%-26s %d %-18s%016llx\n",
+			       "is_fred_enabled:", is_fred_enabled(vcpu),
+			       "guest_evntinjdata:", save->guest_event_inj_data);
+			pr_err("%-12s%016llx %-18s%016llx\n",
+			       "fred_config:", save->fred_config,
+			       "guest_exitintdata:", save->guest_exit_int_data);
+			pr_err("%-15s %016llx %-13s %016llx\n",
+			       "fred_rsp0:", save->fred_rsp0,
+			       "fred_rsp1:", save->fred_rsp1);
+			pr_err("%-15s %016llx %-13s %016llx\n",
+			       "fred_rsp2:", save->fred_rsp2,
+			       "fred_rsp3:", save->fred_rsp3);
+			pr_err("%-15s %016llx %-13s %016llx\n",
+			       "fred_stklvls:", save->fred_stklvls,
+			       "fred_ssp1:", save->fred_ssp1);
+			pr_err("%-15s %016llx %-13s %016llx\n",
+			       "fred_ssp2:", save->fred_ssp2,
+			       "fred_ssp3:", save->fred_ssp3);
+		}
 	}
 
 no_vmsa:
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 7/7 KVM: SVM: Enable FRED virtualization

In reply to Shivansh Dhiman

Set the FRED_VIRT_ENABLE bit (bit 4) in the VIRT_EXT field of VMCB to enable FRED Virtualization for the guest. This enables automatic save/restore of FRED MSRs.

---

Changes in v2:
- Confined the enabling of vFRED to svm_vcpu_after_set_cpuid() only (Sean Christopherson).

---
 arch/x86/include/asm/svm.h | 1 +
 arch/x86/kvm/svm/svm.c     | 5 +++++
 2 files changed, 6 insertions(+)
arch/x86/include/asm/svm.h +1 -0
index c0f6523e2b2d..f4a9781c1d6c 100644
@@ -224,6 +224,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 
 #define LBR_CTL_ENABLE_MASK BIT_ULL(0)
 #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
+#define FRED_VIRT_ENABLE_MASK BIT_ULL(4)
 
 #define SVM_INTERRUPT_SHADOW_MASK	BIT_ULL(0)
 #define SVM_GUEST_INTERRUPT_MASK	BIT_ULL(1)
arch/x86/kvm/svm/svm.c +5 -0
index 7d411df3dd78..4b270490b2fd 100644
@@ -4540,6 +4540,11 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
 	if (guest_cpuid_is_intel_compatible(vcpu))
 		guest_cpu_cap_clear(vcpu, X86_FEATURE_V_VMSAVE_VMLOAD);
 
+	if (guest_cpu_cap_has(vcpu, X86_FEATURE_FRED))
+		svm->vmcb->control.virt_ext |= FRED_VIRT_ENABLE_MASK;
+	else
+		svm->vmcb->control.virt_ext &= ~FRED_VIRT_ENABLE_MASK;
+
 	if (sev_guest(vcpu->kvm))
 		sev_vcpu_after_set_cpuid(svm);
 }
Signature · 1 line
2.43.0
#8 lore raw
In reply to Shivansh Dhiman

Hi,

I'm planning to add another patch to this series in the next version. This is related to dependency of FRED Virtualization on NMI Virtualization as mentioned in the vFRED specification [1]. Any comments are welcome.

[1] https://docs.amd.com/v/u/en-US/69191-PUB

Thanks,
Shivansh

---

arch/x86/kvm/svm/svm.c +3 -0
index 4b270490b2fd..f1fe407ea5da 100644
@@ -5379,6 +5379,9 @@ static __init void svm_set_cpu_caps(void)
        /* CPUID 0x8000001F (SME/SEV features) */
        sev_set_cpu_caps();
 
+       if (!vnmi)
+               kvm_cpu_cap_clear(X86_FEATURE_FRED);
+
        /*
         * Clear capabilities that are automatically configured by common code,
         * but that require explicit SVM support (that isn't yet implemented).
Quoted 66 lines · Shivansh Dhiman

This series adds SVM support for FRED (Flexible Return and Event Delivery) virtualization in KVM.

FRED introduces simplified privilege level transitions to replace IDT-based event delivery and IRET returns, providing lower latency event handling while ensuring complete supervisor context on delivery and full user context on return. FRED defines event delivery for both ring 3->0 and ring 0->0 transitions, and introduces ERETU for returning to ring 3 and ERETS for remaining in ring 0.

AMD hardware extends the VMCB to support FRED virtualization with dedicated save area fields for FRED MSRs (RSP0-3, SSP1-3, STKLVLS, CONFIG) and control fields for event injection data (EXITINTDATA, EVENTINJDATA).

The implementation spans seven patches. The important changes are:

1) Extend VMCB structures with FRED fields mentioned above and disable MSR interception for FRED-enabled guests to avoid unnecessary VM exits.

2) Support for nested exceptions where we populate event injection data when delivering exceptions like page faults and debug traps.

Changes in v2:
- Modified the zeroing of FRED MSRs from INIT to RESET and updated the commit message (Sean Christopherson).
- Used guest_cpu_cap_has() instead of checking VMCB bit in recalc intercepts (Sean Christopherson).
- Variable rename from 'fred_enable' to 'intercept' (Sean Christopherson).
- 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.
- Removed the need for a new function parameter 'reinject_on_vmexit' in svm_complete_interrupts() (Paolo Bonzini).
- Replace variable 'nested' with 'is_nested' (Sean Christopherson).
- Formatted variables in reverse fir-tree order (Sean Christopherson).
- Gate all the FRED MSRs through guest_cpu_cap_has() while dumping VMCB (Sean Christopherson).
- Confined the enabling of vFRED to svm_vcpu_after_set_cpuid() only (Sean Christopherson).

This series is based on top of FRED support for VMX patchset [1], patches 1-17. The VMX patchset was rebased on top of v6.18.0 kernel. Previous version v1 of this series can be found here:
https://lore.kernel.org/kvm/[email protected]/

[1] https://lore.kernel.org/kvm/[email protected]

Regards,
Shivansh
---
Neeraj Upadhyay (3):
KVM: SVM: Initialize FRED VMCB fields
KVM: SVM: Populate FRED event data on event injection
KVM: SVM: Support FRED nested exception injection

Shivansh Dhiman (4):
KVM: SVM: Disable interception of FRED MSRs for FRED supported guests
KVM: SVM: Save restore FRED_RSP0 for FRED supported guests
KVM: SVM: Dump FRED context in dump_vmcb()
KVM: SVM: Enable FRED virtualization

 arch/x86/include/asm/svm.h |  35 ++++++++++-
 arch/x86/kvm/svm/svm.c     | 122 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 152 insertions(+), 5 deletions(-)

base-commit: f76e83ecf6bce6d3793f828d92170b69e636f3c9