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 instruction fetches of L2 from supervisor-mode and user-mode linear addresses succeed or cause EPT violations with the expected exit qualification. The user-mode cases also fail if MBEC is not enabled in vmcs02. Signed-off-by: Dennis Reußwig --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/vmx_nested_mbec_test.c | 220 +++++++++++++++++++++ 2 files changed, 221 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..04ea22d5ec97 --- /dev/null +++ b/tools/testing/selftests/kvm/x86/vmx_nested_mbec_test.c @@ -0,0 +1,220 @@ +// 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, mapped + * as a supervisor-mode or a user-mode address depending on the test; L2 runs + * at CPL 0 without SMEP, so it may fetch from either. 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 and a user fetch iff XU is set. + * 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; + /* Map the code at a user-mode linear address. */ + bool user; + /* 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, + }, + { + .name = "MBEC, user-executable only: user fetch allowed", + .mbec = true, .xs = false, .xu = true, .user = true, + }, + { + .name = "MBEC, supervisor-executable only: user fetch denied", + .mbec = true, .xs = true, .xu = false, .user = true, + .qual = QUAL_FETCH | EPT_VIOLATION_PROT_EXEC, + }, + { + .name = "no MBEC, bit 10 only: user fetch denied", + .mbec = false, .xs = false, .xu = true, .user = true, + .qual = QUAL_FETCH, + /* Bit 6 is undefined without MBEC. */ + .qual_ignore = EPT_VIOLATION_PROT_USER_EXEC, + }, + { + .name = "no MBEC, bit 2 only: user fetch allowed", + .mbec = false, .xs = true, .xu = false, .user = true, + }, +}; + +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; + + vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code); + vm_enable_tdp(vm); + vcpu_alloc_vmx(vm, &vmx_gva); + ept = &vm->stage2_mmu; + + /* An 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; + + /* A user-mode address needs U/S set at every level of L2's paging. */ + if (t->user) + for (level = PG_LEVEL_4K; level <= vm->mmu.pgtable_levels; level++) + *__vm_get_pte(vm, CODE_GVA, level) |= PTE_USER_MASK(&vm->mmu); + + 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); + + ksft_print_header(); + ksft_set_plan(ARRAY_SIZE(tests)); + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + if (tests[i].mbec && !has_mbec) { + ksft_test_result_skip("%s: MBEC not offered to L1\n", tests[i].name); + continue; + } + run_test(&tests[i]); + ksft_test_result_pass("%s\n", tests[i].name); + } + + ksft_finished(); +} -- 2.53.0