mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Dennis Reußwig" <dennis@reusswig.codes>
To: Sean Christopherson <seanjc@google.com>,
	 Paolo Bonzini <pbonzini@redhat.com>,
	Thomas Gleixner <tglx@kernel.org>,
	 Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org,  "H. Peter Anvin" <hpa@zytor.com>,
	Jon Kohler <jon@nutanix.com>,  Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	 Randy Dunlap <rdunlap@infradead.org>,
	Shuah Khan <shuah@kernel.org>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org,
	"Chao Gao" <chao.gao@intel.com>,
	"Maxim Levitsky" <mlevitsk@redhat.com>,
	"Vitaly Kuznetsov" <vkuznets@redhat.com>,
	"Panayotis Katsaloulis" <panayotis@panayotis.com>,
	"Dennis Reußwig" <dennis@reusswig.codes>
Subject: [PATCH 09/10] KVM: selftests: Add nested EPT mode-based execute control test
Date: Fri, 04 Dec 2026 00:49:22 +0100	[thread overview]
Message-ID: <20261204-nested-sss-v1-9-57de06f58b2b@reusswig.codes> (raw)
In-Reply-To: <20261204-nested-sss-v1-0-57de06f58b2b@reusswig.codes>

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


  parent reply	other threads:[~2026-12-03 23:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
2026-12-03 23:49 ` [PATCH 01/10] KVM: nVMX: Allow CET entry/exit controls with enlightened VMCS Dennis Reußwig
2026-12-03 23:49 ` [PATCH 02/10] KVM: x86/mmu: Move TDP A/D tracking type to SPTE bits 56-57 Dennis Reußwig
2026-12-03 23:49 ` [PATCH 03/10] KVM: VMX: Report shadow-stack accesses in EPT violations as PFERR_SS Dennis Reußwig
2026-12-03 23:49 ` [PATCH 04/10] KVM: x86/mmu: Handle the EPT supervisor shadow-stack bit like a write bit Dennis Reußwig
2026-12-03 23:49 ` [PATCH 05/10] KVM: nVMX: Shadow supervisor shadow-stack control of nested EPT Dennis Reußwig
2026-12-03 23:49 ` [PATCH 06/10] KVM: nVMX: Advertise supervisor shadow-stack control for " Dennis Reußwig
2026-12-03 23:49 ` [PATCH 07/10] KVM: selftests: Add helpers for nested EPT shadow-stack and MBEC tests Dennis Reußwig
2026-12-03 23:49 ` [PATCH 08/10] KVM: selftests: Add nested EPT supervisor shadow-stack test Dennis Reußwig
2026-12-03 23:49 ` Dennis Reußwig [this message]
2026-12-03 23:49 ` [PATCH 10/10] KVM: selftests: Check VMX capabilities for CET in L2 with MBEC Dennis Reußwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204-nested-sss-v1-9-57de06f58b2b@reusswig.codes \
    --to=dennis@reusswig.codes \
    --cc=bp@alien8.de \
    --cc=chao.gao@intel.com \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jon@nutanix.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=mlevitsk@redhat.com \
    --cc=panayotis@panayotis.com \
    --cc=pbonzini@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tglx@kernel.org \
    --cc=vkuznets@redhat.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®