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 10/10] KVM: selftests: Check VMX capabilities for CET in L2 with MBEC
Date: Fri, 04 Dec 2026 00:49:23 +0100	[thread overview]
Message-ID: <20261204-nested-sss-v1-10-57de06f58b2b@reusswig.codes> (raw)
In-Reply-To: <20261204-nested-sss-v1-0-57de06f58b2b@reusswig.codes>

Hyper-V enables CET for its guests when L1 is offered mode-based
execute control.  It then needs supervisor shadow-stack control for
nested EPT and the VM-entry and VM-exit controls that load CET state,
with and without the enlightened VMCS.  Read the capabilities from L1,
once without and once with eVMCS, since KVM filters the control MSRs
for eVMCS only for reads by the guest.

Without the fixes in this series the test fails: supervisor
shadow-stack control is missing, and with eVMCS also the CET controls.

Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
 tools/testing/selftests/kvm/Makefile.kvm           |  1 +
 .../selftests/kvm/x86/vmx_nested_cet_caps_test.c   | 90 ++++++++++++++++++++++
 2 files changed, 91 insertions(+)

diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 2b502c9ec7cb..9e62ed2bec1a 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -148,6 +148,7 @@ 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/vmx_nested_cet_caps_test
 TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test
 TEST_GEN_PROGS_x86 += x86/apic_id_routing_test
 TEST_GEN_PROGS_x86 += x86/xapic_ipi_test
diff --git a/tools/testing/selftests/kvm/x86/vmx_nested_cet_caps_test.c b/tools/testing/selftests/kvm/x86/vmx_nested_cet_caps_test.c
new file mode 100644
index 000000000000..cb9135df5d13
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/vmx_nested_cet_caps_test.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * VMX capabilities that L1 needs to run L2 guests with CET.
+ *
+ * Hyper-V enables CET for its guests when L1 is offered mode-based execute
+ * control (MBEC).  It then needs supervisor shadow-stack control for nested
+ * EPT (IA32_VMX_EPT_VPID_CAP bit 23) and the VM-entry/VM-exit controls that
+ * load CET state, with and without the enlightened VMCS.  KVM filters the
+ * control MSRs for eVMCS only when the guest reads them, so L1 reads them
+ * itself.
+ */
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "hyperv.h"
+#include "vmx.h"
+
+static void guest_code(void)
+{
+	u64 ctls2 = rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2);
+	u64 ept_cap = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
+	u64 exit_ctls = rdmsr(MSR_IA32_VMX_TRUE_EXIT_CTLS);
+	u64 entry_ctls = rdmsr(MSR_IA32_VMX_TRUE_ENTRY_CTLS);
+
+	/* Without MBEC, Hyper-V does not enable CET and nothing is required. */
+	if (!((ctls2 >> 32) & SECONDARY_EXEC_MODE_BASED_EPT_EXEC))
+		GUEST_DONE();
+
+	__GUEST_ASSERT((exit_ctls >> 32) & VM_EXIT_LOAD_CET_STATE,
+		       "MBEC offered but the VM-exit control to load CET state is not, exit controls 0x%lx",
+		       exit_ctls);
+	__GUEST_ASSERT((entry_ctls >> 32) & VM_ENTRY_LOAD_CET_STATE,
+		       "MBEC offered but the VM-entry control to load CET state is not, entry controls 0x%lx",
+		       entry_ctls);
+
+	/* Supervisor shadow-stack control needs advanced VM-exit information. */
+	if (ept_cap & VMX_EPT_VPID_CAP_ADVANCED_VMEXIT_INFO)
+		__GUEST_ASSERT(ept_cap & VMX_EPT_VPID_CAP_SSS,
+			       "MBEC offered but supervisor shadow-stack control is not, EPT capabilities 0x%lx",
+			       ept_cap);
+
+	GUEST_DONE();
+}
+
+static void run_test(bool evmcs)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+
+	pr_info("Checking the VMX capabilities L1 sees %s eVMCS\n", evmcs ? "with" : "without");
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+	if (evmcs) {
+		vcpu_set_hv_cpuid(vcpu);
+		vcpu_enable_evmcs(vcpu);
+	}
+
+	vcpu_run(vcpu);
+	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT(uc);
+		break;
+	case UCALL_DONE:
+		break;
+	default:
+		TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+	}
+
+	kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
+	TEST_REQUIRE(kvm_cpu_has_ept());
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SHSTK));
+	TEST_REQUIRE(kvm_cpu_has_secondary_exec_control(SECONDARY_EXEC_MODE_BASED_EPT_EXEC));
+
+	run_test(false);
+
+	if (kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS))
+		run_test(true);
+	else
+		pr_info("eVMCS not supported, skipping the eVMCS case\n");
+
+	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 ` [PATCH 09/10] KVM: selftests: Add nested EPT mode-based execute control test Dennis Reußwig
2026-12-03 23:49 ` Dennis Reußwig [this message]

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-10-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®