[PATCH 09/10] KVM: selftests: Add nested EPT mode-based execute control test
From: Dennis Reußwig
Date: Thu Dec 03 2026 - 18:50:20 EST
L1 runs L2 with L1 EPT entries that vary the supervisor and user
execute bits, with and without mode-based execute control, and checks
that supervisor instruction fetches of L2 succeed or cause EPT
violations with the expected exit qualification.
Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/x86/vmx_nested_mbec_test.c | 189 +++++++++++++++++++++
2 files changed, 190 insertions(+)
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index dc890dbe48d1..2b502c9ec7cb 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -146,6 +146,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
TEST_GEN_PROGS_x86 += x86/vmx_nested_la57_state_test
+TEST_GEN_PROGS_x86 += x86/vmx_nested_mbec_test
TEST_GEN_PROGS_x86 += x86/vmx_nested_sss_test
TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test
TEST_GEN_PROGS_x86 += x86/apic_id_routing_test
diff --git a/tools/testing/selftests/kvm/x86/vmx_nested_mbec_test.c b/tools/testing/selftests/kvm/x86/vmx_nested_mbec_test.c
new file mode 100644
index 000000000000..652b685466d5
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/vmx_nested_mbec_test.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Nested EPT mode-based execute control (MBEC).
+ *
+ * L1 runs L2, which calls a one-byte RET at a fixed linear address in
+ * supervisor mode. L1's EPT entries for that page vary per test: XS (bit 2,
+ * supervisor-executable) and XU (bit 10, user-executable), with and without
+ * MBEC enabled in vmcs12. With MBEC, a supervisor fetch is allowed iff XS is
+ * set; XU must not grant it. Without MBEC, bit 2 alone decides and bit 10 is
+ * ignored. A denied fetch must reach L1 as an EPT violation for the code page,
+ * with the exit qualification describing the fetch and L1's permissions.
+ */
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "vmx.h"
+
+#define CODE_GVA 0x38000000ULL
+/* Alone in its 1G region, so the non-leaf EPT entries below the PML4E are its own. */
+#define CODE_L2_GPA 0x80000000ULL
+
+#define EPT_XS BIT_ULL(2)
+#define EPT_XU BIT_ULL(10)
+
+/* Paging attributes of L2's linear address, not under test. */
+#define QUAL_IGNORE (BIT_ULL(9) | BIT_ULL(10) | BIT_ULL(11))
+
+#define QUAL_FETCH (EPT_VIOLATION_ACC_INSTR | EPT_VIOLATION_PROT_READ | \
+ EPT_VIOLATION_GVA_IS_VALID | EPT_VIOLATION_GVA_TRANSLATED)
+
+struct mbec_test {
+ const char *name;
+ bool mbec;
+ bool xs;
+ bool xu;
+ /* 0 if L2 must reach its VMCALL. */
+ u64 qual;
+ u64 qual_ignore;
+};
+
+static const struct mbec_test tests[] = {
+ {
+ .name = "MBEC, user-executable only: supervisor fetch denied",
+ .mbec = true, .xs = false, .xu = true,
+ .qual = QUAL_FETCH | EPT_VIOLATION_PROT_USER_EXEC,
+ },
+ {
+ .name = "MBEC, supervisor-executable only: supervisor fetch allowed",
+ .mbec = true, .xs = true, .xu = false,
+ },
+ {
+ .name = "MBEC, executable for both: supervisor fetch allowed",
+ .mbec = true, .xs = true, .xu = true,
+ },
+ {
+ .name = "MBEC, not executable: supervisor fetch denied",
+ .mbec = true, .xs = false, .xu = false,
+ .qual = QUAL_FETCH,
+ },
+ {
+ .name = "no MBEC, bit 10 only: fetch denied",
+ .mbec = false, .xs = false, .xu = true,
+ .qual = QUAL_FETCH,
+ /* Bit 6 is undefined without MBEC. */
+ .qual_ignore = EPT_VIOLATION_PROT_USER_EXEC,
+ },
+ {
+ .name = "no MBEC, bit 2 only: fetch allowed",
+ .mbec = false, .xs = true, .xu = false,
+ },
+};
+
+static void l2_guest_code(void)
+{
+ ((void (*)(void))CODE_GVA)();
+ vmcall();
+}
+
+static void l1_guest_code(struct vmx_pages *vmx, u64 mbec, u64 qual, u64 qual_ignore)
+{
+ u64 actual;
+
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+ prepare_vmcs(vmx, l2_guest_code);
+
+ if (mbec)
+ vmwrite(SECONDARY_VM_EXEC_CONTROL,
+ vmread(SECONDARY_VM_EXEC_CONTROL) | SECONDARY_EXEC_MODE_BASED_EPT_EXEC);
+
+ vmlaunch();
+
+ if (!qual) {
+ __GUEST_ASSERT(vmread(VM_EXIT_REASON) == EXIT_REASON_VMCALL,
+ "Expected VMCALL, got exit reason 0x%lx, qualification 0x%lx",
+ vmread(VM_EXIT_REASON), vmread(EXIT_QUALIFICATION));
+ GUEST_DONE();
+ }
+
+ __GUEST_ASSERT(vmread(VM_EXIT_REASON) == EXIT_REASON_EPT_VIOLATION,
+ "Expected EPT violation, got exit reason 0x%lx",
+ vmread(VM_EXIT_REASON));
+ __GUEST_ASSERT(vmread(GUEST_PHYSICAL_ADDRESS) == CODE_L2_GPA,
+ "Expected GPA 0x%llx, got 0x%lx", CODE_L2_GPA,
+ vmread(GUEST_PHYSICAL_ADDRESS));
+ __GUEST_ASSERT(vmread(GUEST_LINEAR_ADDRESS) == CODE_GVA,
+ "Expected linear address 0x%llx, got 0x%lx", CODE_GVA,
+ vmread(GUEST_LINEAR_ADDRESS));
+
+ actual = vmread(EXIT_QUALIFICATION) & ~(QUAL_IGNORE | qual_ignore);
+ __GUEST_ASSERT(actual == qual, "Expected qualification 0x%lx, got 0x%lx",
+ qual, actual);
+ GUEST_DONE();
+}
+
+static void run_test(const struct mbec_test *t)
+{
+ struct kvm_mmu *ept;
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ gva_t vmx_gva;
+ struct ucall uc;
+ gpa_t code_gpa;
+ u64 *pte;
+ int level;
+
+ pr_info("%s\n", t->name);
+
+ vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
+ vm_enable_tdp(vm);
+ vcpu_alloc_vmx(vm, &vmx_gva);
+ ept = &vm->stage2_mmu;
+
+ /* A supervisor, executable mapping of the code page in L2's page tables. */
+ code_gpa = vm_phy_page_alloc(vm, MEM_REGION_DATA);
+ *(u8 *)addr_gpa2hva(vm, code_gpa) = 0xc3; /* RET */
+ virt_pg_map(vm, CODE_GVA, CODE_L2_GPA);
+
+ tdp_identity_map_default_memslots(vm);
+ tdp_map(vm, CODE_L2_GPA, code_gpa, PAGE_SIZE);
+
+ /* Leaf: readable, never writable, XS and XU per test. */
+ pte = tdp_get_pte(vm, CODE_L2_GPA);
+ *pte &= ~(PTE_WRITABLE_MASK(ept) | EPT_XS);
+ if (t->xs)
+ *pte |= EPT_XS;
+ if (t->xu)
+ *pte |= EPT_XU;
+
+ /* XU counts only if set at every level; the library leaves it clear in non-leaf entries. */
+ for (level = PG_LEVEL_2M; level <= PG_LEVEL_512G; level++)
+ *__tdp_get_pte(vm, CODE_L2_GPA, level) |= EPT_XU;
+
+ vcpu_args_set(vcpu, 4, vmx_gva, (u64)t->mbec, t->qual, t->qual_ignore);
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_DONE:
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ default:
+ TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+ }
+
+ kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+ bool has_mbec;
+ int i;
+
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
+ TEST_REQUIRE(kvm_cpu_has_ept());
+
+ has_mbec = kvm_cpu_has_secondary_exec_control(SECONDARY_EXEC_MODE_BASED_EPT_EXEC);
+ if (!has_mbec)
+ pr_info("MBEC not offered to L1, running only the cases without MBEC\n");
+
+ for (i = 0; i < ARRAY_SIZE(tests); i++) {
+ if (tests[i].mbec && !has_mbec)
+ continue;
+ run_test(&tests[i]);
+ }
+
+ return 0;
+}
--
2.53.0