Skip to contentSkip to conversation

x86/svm: Track and handle exit code as an unsigned 64-bit value

PATCH Applied Sean Christopherson 2 participants 3 messages Dec 30, 2025 – May 19 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

PATCH 1/1 x86/svm: Track and handle exit code as an unsigned 64-bit value

Track and handle SVM's exit_code as an unsigned 64-bit value. Per the APM, offset 0x70 is a single 64-bit value:

070h 63:0 EXITCODE

And a sane reading of the error values defined in "Table C-1. SVM Intercept Codes" is that negative values use the full 64 bits:

–1 VMEXIT_INVALID Invalid guest state in VMCB.
–2 VMEXIT_BUSYBUSY bit was set in the VMSA
–3 VMEXIT_IDLE_REQUIREDThe sibling thread is not in an idle state -4 VMEXIT_INVALID_PMC Invalid PMC state

And that interpretation is confirmed by testing on Milan and Turin (by setting bits in CR0[63:32] to generate VMEXIT_INVALID on VMRUN).

Furthermore, Xen has treated exitcode as a 64-bit value since HVM support was adding in 2006 (see Xen commit d1bd157fbc ("Big merge the HVM full-virtualisation abstractions.")).

Note, the SVM tests will fail when on KVM builds without commit f402ecd7a8b6 ("KVM: nSVM: Set exit_code_hi to -1 when synthesizing SVM_EXIT_ERR (failed VMRUN)").

---
 x86/svm.c       |  4 +--
 x86/svm.h       |  9 +++----
 x86/svm_npt.c   |  4 +--
 x86/svm_tests.c | 66 ++++++++++++++++++++++++-------------------------
 4 files changed, 41 insertions(+), 42 deletions(-)
x86/svm.c +2 -2
index de9eb194..3f6d70dc 100644
@@ -227,7 +227,7 @@ void svm_setup_vmrun(u64 rip)
 	vmcb->save.rsp = (ulong)(guest_stack + ARRAY_SIZE(guest_stack));
 }
 
-int __svm_vmrun(u64 rip)
+u64 __svm_vmrun(u64 rip)
 {
 	svm_setup_vmrun(rip);
 	regs.rdi = (ulong)v2_test;
@@ -243,7 +243,7 @@ int __svm_vmrun(u64 rip)
 	return (vmcb->control.exit_code);
 }
 
-int svm_vmrun(void)
+u64 svm_vmrun(void)
 {
 	return __svm_vmrun((u64)test_thunk);
 }
x86/svm.h +4 -5
index 264583a6..02d9bac3 100644
@@ -88,8 +88,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 	u32 int_vector;
 	u32 int_state;
 	u8 reserved_3[4];
-	u32 exit_code;
-	u32 exit_code_hi;
+	u64 exit_code;
 	u64 exit_info_1;
 	u64 exit_info_2;
 	u32 exit_int_info;
@@ -354,7 +353,7 @@ struct __attribute__ ((__packed__)) vmcb {
 #define SVM_EXIT_MWAIT_COND	0x08c
 #define SVM_EXIT_NPF  		0x400
 
-#define SVM_EXIT_ERR		-1
+#define SVM_EXIT_ERR		-1ull
 
 #define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
 
@@ -435,8 +434,8 @@ void vmcb_ident(struct vmcb *vmcb);
 struct regs get_regs(void);
 void vmmcall(void);
 void svm_setup_vmrun(u64 rip);
-int __svm_vmrun(u64 rip);
-int svm_vmrun(void);
+u64 __svm_vmrun(u64 rip);
+u64 svm_vmrun(void);
 void test_set_guest(test_guest_func func);
 
 extern struct vmcb *vmcb;
x86/svm_npt.c +2 -2
index bd5e8f35..24d7707b 100644
@@ -219,7 +219,7 @@ static void __svm_npt_rsvd_bits_test(u64 * pxe, u64 rsvd_bits, u64 efer,
 				     ulong cr4, u64 guest_efer, ulong guest_cr4)
 {
 	u64 pxe_orig = *pxe;
-	int exit_reason;
+	u64 exit_reason;
 	u64 pfec;
 
 	wrmsr(MSR_EFER, efer);
@@ -233,7 +233,7 @@ static void __svm_npt_rsvd_bits_test(u64 * pxe, u64 rsvd_bits, u64 efer,
 	exit_reason = svm_vmrun();
 
 	report(exit_reason == SVM_EXIT_NPF,
-	       "Wanted #NPF on rsvd bits = 0x%lx, got exit = 0x%x", rsvd_bits,
+	       "Wanted #NPF on rsvd bits = 0x%lx, got exit = 0x%lx", rsvd_bits,
 	       exit_reason);
 
 	if (pxe == npt_get_pdpe((u64) basic_guest_main) || pxe == npt_get_pml4e()) {
x86/svm_tests.c +33 -33

258 lines not on this page · raw message

base-commit: 31d91f5c9b7546471b729491664b05c933d64a7a

Signature · 1 line
2.52.0.351.gbe84eed79e-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Sean Christopherson

Track and handle SVM's exit_code as an unsigned 64-bit value. Per the APM, offset 0x70 is a single 64-bit value:

070h 63:0 EXITCODE

And a sane reading of the error values defined in "Table C-1. SVM Intercept Codes" is that negative values use the full 64 bits:

–1 VMEXIT_INVALID Invalid guest state in VMCB.
–2 VMEXIT_BUSYBUSY bit was set in the VMSA
–3 VMEXIT_IDLE_REQUIREDThe sibling thread is not in an idle state -4 VMEXIT_INVALID_PMC Invalid PMC state

And that interpretation is confirmed by testing on Milan and Turin (by setting bits in CR0[63:32] to generate VMEXIT_INVALID on VMRUN).

Furthermore, Xen has treated exitcode as a 64-bit value since HVM support was adding in 2006 (see Xen commit d1bd157fbc ("Big merge the HVM full-virtualisation abstractions.")).

Quoted 6 lines

Note, the SVM tests will fail when on KVM builds without commit f402ecd7a8b6 ("KVM: nSVM: Set exit_code_hi to -1 when synthesizing SVM_EXIT_ERR (failed VMRUN)").

author reviewer Applied
#2 lore raw
In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson

Track and handle SVM's exit_code as an unsigned 64-bit value. Per the APM, offset 0x70 is a single 64-bit value:

070h 63:0 EXITCODE

And a sane reading of the error values defined in "Table C-1. SVM Intercept Codes" is that negative values use the full 64 bits:

[...]

Applied to kvm-x86 next, thanks!

[1/1] x86/svm: Track and handle exit code as an unsigned 64-bit value

      https://github.com/kvm-x86/kvm-unit-tests/commit/4d60e2429d63

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next