From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=109.224.244.108 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796341801; b=q15txE+0Es0uuhrU9kva7tyRBZyGpt4UsWwX9FfxWz73K1wEMulUxYq04Ktxr4EEKu7g /XKFZmQXbFuU8msxUB68jGMCHiD7h9lymKw/y8shwu6e0KklHk81ZTNRTAD7HQJwEUJQ0 qop0Xwnmjtv2/Lv15gCbjTPGC6GAvjeJmK8SYmHrfzMqKimD7JhG21iqMX7nSoz1ks2RW RX0/iRXAIPBIDs9APKlBC+adFX1Hb/UiKRZBZmU+xFmYo++wKt0VR5B0WCZbOfLa8n2/6 egFTuV4mAnsYCeUDUgyiaFhXzqQTpH3Zyc2/tphh0tYG4zQrt8zH5/TIdZhnM++A6gQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796341801; h=DMARC-Filter:DKIM-Signature:X-Pm-Submission-Id:From:Date:Subject: MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id: References:In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature: X-Developer-Key; bh=8JA4pwhyL/NuyvVNE/OCUH0p/zqSiEC64coXVkU1YlM=; b=zGPbddaJSUYOSE1YoFU9MuYBEuLavP701BEt4mCakmZXzXvrAKno/p3uqKSQOhcTquUh ZGSuV82Swxkc8zdJp3YFLk8saGErNciiEjxhhOBWHS01cUASuEwUKlwxNag+hXtuPHaUd 9KuSIfzp4e/AttvUzSfCcUVI/7/2po+OOLvLyLEzV6rF2ONM4jLlRUAlAMVmDolHjnhEu z5BaG1bek7JsAcOLObWCwQYmfAt6+MWBii4h5D5QKGt2BESh8bGCftKvZnu/CYW5J4rbY qb339hQ1qK5KCo4E0T3NkHLyKxsGrT2cM4dLvmJsH0kDWkyxAa8+PbZ1Zjr45ZgSaVw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=reusswig.codes; spf=pass smtp.mailfrom=reusswig.codes; dkim=pass header.d=reusswig.codes header.i=@reusswig.codes header.a=rsa-sha256 header.s=protonmail3 header.b=R9SJObgk; arc=none smtp.remote-ip=109.224.244.108 Received: from mail-244108.protonmail.ch (mail-244108.protonmail.ch [109.224.244.108]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1AC460A381F for ; Thu, 3 Dec 2026 23:50:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C1AC460A381F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=reusswig.codes Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=reusswig.codes Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=reusswig.codes header.i=@reusswig.codes header.a=rsa-sha256 header.s=protonmail3 header.b=R9SJObgk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=reusswig.codes; s=protonmail3; t=1796341798; x=1796600998; bh=8JA4pwhyL/NuyvVNE/OCUH0p/zqSiEC64coXVkU1YlM=; h=From:Date:Subject:Message-Id:References:In-Reply-To:To:Cc:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=R9SJObgk3+rX/bYl1qKVAEGDF22DPP5AS296OWLXt8fDyrvM8eTVFzWdqO4VUOHfm O0RqwpYpvKUFMG/0EAVO3X0Bz/FUwxGkBlsewwPtEhNGr+s5OtveiYWMuGCvRUpSUE DFNk/sjtfXLvA5Uo/bgYQanXSltWOH0igmKEAdbyBalJF8XIrZWGgsDq54sJn3N3GS D3UVyD6TV1AaPGr1z5G11PiVi+Am+nT8W8yPcaOz5xnhl7T3aITjfxcZW0FZHL/BYX 8VFNo0mn84r6KAlZlnu2rpUtsCSBjdaKM832DxvcQblfXC/dFXh0K/v8NIvQ2ItNRA atPUiJP5YqmGw== X-Pm-Submission-Id: 4jgWZY0BS4z2ScNv From: =?utf-8?q?Dennis_Reu=C3=9Fwig?= Date: Fri, 04 Dec 2026 00:49:22 +0100 Subject: [PATCH 09/10] KVM: selftests: Add nested EPT mode-based execute control test X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20261204-nested-sss-v1-9-57de06f58b2b@reusswig.codes> References: <20261204-nested-sss-v1-0-57de06f58b2b@reusswig.codes> In-Reply-To: <20261204-nested-sss-v1-0-57de06f58b2b@reusswig.codes> To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Jon Kohler , Jonathan Corbet , Shuah Khan , Randy Dunlap , Shuah Khan Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Chao Gao , Maxim Levitsky , Vitaly Kuznetsov , Panayotis Katsaloulis , =?utf-8?q?Dennis_Reu=C3=9Fwig?= X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=7208; i=dennis@reusswig.codes; h=from:subject:message-id; bh=Z/OAczCLI6PPzjcRA22T8JtGt2ifb6SCkpK6i0g3apg=; b=owEBbQKS/ZANAwAIAWBucoRlTGuCAcsmYgBrEgAR+vrk8cN44OVOdsdzRi3Jh7KLlt9GGTOof i8UXmL0VeeJAjMEAAEIAB0WIQS59eJ1vy/tsv/LkIBgbnKEZUxrggUCaxIAEQAKCRBgbnKEZUxr ghFrD/9R7wn7Vn29vjTc5w7unYfKRIPw1yUS9AX34NRPGSHS/fvTJRDck1M0zNpnpcPF0ghzv2M FSI3G+asc6lABi54ln7NzNYvDeVbdmHJbZJz88DsSuAF2yhaGzAH//gxktUMkwbXFg20TWd/fLR CLp8lZUXu697DCaZ10GVDhJFx92by6Uw1YQqjtKUxoELxGhe0+YD2cn4B7sJxRIvxokJrYMOpWl xQGKDuLkPUvxo7FBSbT87Z4rTEtbS2xEkwIaYW9HALKwTa0popc6Fgz6cL6ITBSInKRqbBahryc vXCnyCMwdqQvvRI0R7hAcHtq9KCtKwYypQrUNzuW5q6AzLOR5+LYkHaHyWphdYl3PrpiZm+UgGE sDKFW1ORgcGgBerM2H8W40WqnJeo7IeRXzGK4HFr4qjghORwT7rgp0BzPTOhR5RrdU7p8GmqyVM MJ2tXDrH2xyrvX2Xv5BIpXgeX8WDE+zTzW92wy9uwST5F2wiuxIw5FdlRqEfPGZ4AXqWoQrIKDX 2E+3mDau85rpaQBsvt1b/gHzk3l7Y4mvS8wxsvuVaB8FKHvCrTtwj4WlsSLwzhL2he1xVvnPkdC 4R4lu/MJfqqr7b1XxSbAhaI2otxwW5aDt4DUy8L598NUcjQgfF9wbn0J0PhEC2yoKhvjxUc2KtK dAr32IitGYJT5pA== X-Developer-Key: i=dennis@reusswig.codes; a=openpgp; fpr=D8E955E3BCD7A0A0DEDE55D1B92D668A3A02E8CA 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 --- 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