[PATCH v2 4/4] KVM: selftests: Check VMPTRLD with active eVMCS
From: Vitaly Kuznetsov
Date: Tue Aug 18 2026 - 12:56:23 EST
Check that VMPTRLD when eVMCS is active results in #UD. This matches
genuine Hyper-V's behavior.
Use KVM_ASM_SAFE framework to handle #UD from VMPTRLD. Unfortunately, the
same trick cannot be applied to the existing #UD check on VMLAUNCH as
VMLAUNCH clobbers all registers which KVM_ASM_SAFE depends on. Keep
VMLAUNCH handling separately.
Assisted-by: Claude:claude-sonnet-4-6
Signed-off-by: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
---
tools/testing/selftests/kvm/include/x86/vmx.h | 19 +++++++++++---
.../testing/selftests/kvm/x86/hyperv_evmcs.c | 26 +++++++++++++++----
2 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
index 047d02aa9688..e6adde3970d4 100644
--- a/tools/testing/selftests/kvm/include/x86/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86/vmx.h
@@ -325,9 +325,6 @@ static inline int vmptrld(u64 vmcs_pa)
{
u8 ret;
- if (enable_evmcs)
- return -1;
-
__asm__ __volatile__ ("vmptrld %[pa]; setna %[ret]"
: [ret]"=rm"(ret)
: [pa]"m"(vmcs_pa)
@@ -336,6 +333,22 @@ static inline int vmptrld(u64 vmcs_pa)
return ret;
}
+static inline int vmptrld_safe(u64 vmcs_pa)
+{
+ u64 error_code;
+ u8 vector;
+ u8 failed;
+
+ asm volatile(KVM_ASM_SAFE("vmptrld %[pa]")
+ "\n\tsetna %[failed]"
+ : KVM_ASM_SAFE_OUTPUTS(vector, error_code),
+ [failed]"=qm"(failed)
+ : [pa]"m"(vmcs_pa)
+ : "cc", "memory", KVM_ASM_SAFE_CLOBBERS);
+
+ return vector ? vector : failed ? -EINVAL : 0;
+}
+
static inline int vmptrst(u64 *value)
{
u64 tmp;
diff --git a/tools/testing/selftests/kvm/x86/hyperv_evmcs.c b/tools/testing/selftests/kvm/x86/hyperv_evmcs.c
index 88262ddf7fcb..cff27638834b 100644
--- a/tools/testing/selftests/kvm/x86/hyperv_evmcs.c
+++ b/tools/testing/selftests/kvm/x86/hyperv_evmcs.c
@@ -23,7 +23,11 @@ static int ud_count;
static void guest_ud_handler(struct ex_regs *regs)
{
ud_count++;
- regs->rip += 3; /* VMLAUNCH */
+ /*
+ * VMLAUNCH insn can't be easily covered by KVM_ASM_SAFE framework but
+ * luckily the instruction is always three bytes.
+ */
+ regs->rip += 3;
}
static void guest_nmi_handler(struct ex_regs *regs)
@@ -182,7 +186,15 @@ void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages,
GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
GUEST_SYNC(11);
- /* Try enlightened vmptrld with an incorrect GPA */
+ /* VMPTRLD instruction causes #UD after enlightened VMLAUNCH */
+ GUEST_ASSERT(vmptrld_safe(hv_pages->enlightened_vmcs_gpa) == UD_VECTOR);
+
+ /*
+ * Try enlightened vmptrld with an incorrect GPA. GUEST_SYNC(12) signals
+ * the host to enable guest_ud_handler() which cannot be enabled beforehand
+ * to not override the default fixup handler from KVM_ASM_SAFE().
+ */
+ GUEST_SYNC(12);
evmcs_vmptrld(0xdeadbeef, hv_pages->enlightened_vmcs);
GUEST_ASSERT(vmlaunch());
GUEST_ASSERT(ud_count == 1);
@@ -256,7 +268,6 @@ int main(int argc, char *argv[])
vcpu_args_set(vcpu, 3, vmx_pages_gva, hv_pages_gva, addr_gva2gpa(vm, hcall_page));
vcpu_set_msr(vcpu, HV_X64_MSR_VP_INDEX, vcpu->id);
- vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
vm_install_exception_handler(vm, NMI_VECTOR, guest_nmi_handler);
pr_info("Running L1 which uses EVMCS to run L2\n");
@@ -286,7 +297,7 @@ int main(int argc, char *argv[])
/* Force immediate L2->L1 exit before resuming */
if (stage == 8) {
- pr_info("Injecting NMI into L1 before L2 had a chance to run after restore\n");
+ pr_debug("Injecting NMI into L1 before L2 had a chance to run after restore\n");
inject_nmi(vcpu);
}
@@ -296,9 +307,14 @@ int main(int argc, char *argv[])
* KVM_STATE_NESTED_EVMCS is not lost.
*/
if (stage == 9) {
- pr_info("Trying extra KVM_GET_NESTED_STATE/KVM_SET_NESTED_STATE cycle\n");
+ pr_debug("Trying extra KVM_GET_NESTED_STATE/KVM_SET_NESTED_STATE cycle\n");
vcpu = save_restore_vm(vm, vcpu);
}
+
+ if (stage == 12) {
+ pr_debug("Trying enlightened VMLAUNCH with an invalid PTR\n");
+ vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
+ }
}
done:
--
2.55.0