Re: [PATCH v6 31/31] KVM: selftest: Add a selftest for VMRUN/#VMEXIT with unmappable vmcb12

From: Yosry Ahmed

Date: Mon Mar 02 2026 - 19:14:12 EST


> +++ b/tools/testing/selftests/kvm/x86/svm_nested_invalid_vmcb12_gpa.c
> @@ -0,0 +1,95 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2026, Google LLC.
> + */
> +#include "kvm_util.h"
> +#include "vmx.h"
> +#include "svm_util.h"
> +#include "kselftest.h"
> +
> +
> +#define L2_GUEST_STACK_SIZE 64
> +
> +#define SYNC_GP 101
> +#define SYNC_L2_STARTED 102
> +
> +extern char invalid_vmrun;
> +
> +static void guest_gp_handler(struct ex_regs *regs)
> +{
> + GUEST_SYNC(SYNC_GP);
> + regs->rip = (uintptr_t)&invalid_vmrun;

Instead of jumping after run_guest() and skipping the host restore
sequence, it's probably better to fixup RAX here and have a single
run_guest() call in l1_guest_code().

> +}
> +
> +static void l2_guest_code(void)
> +{
> + GUEST_SYNC(SYNC_L2_STARTED);
> + vmcall();
> +}
> +
> +static void l1_guest_code(struct svm_test_data *svm, u64 invalid_vmcb12_gpa)
> +{
> + unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
> +
> + generic_svm_setup(svm, l2_guest_code,
> + &l2_guest_stack[L2_GUEST_STACK_SIZE]);
> +
> + run_guest(svm->vmcb, invalid_vmcb12_gpa); /* #GP */
> +
> + /* GP handler should jump here */
> + asm volatile ("invalid_vmrun:");
> + run_guest(svm->vmcb, svm->vmcb_gpa);
> + GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_VMMCALL);
> + GUEST_DONE();
> +}