mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS
@ 2026-12-03 23:49 Dennis Reußwig
  2026-12-03 23:49 ` [PATCH 01/10] KVM: nVMX: Allow CET entry/exit controls with enlightened VMCS Dennis Reußwig
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

Windows 11 guests with Hyper-V enabled (virtualization-based security,
HVCI, Credential Guard, WSL2) stopped booting on Intel hosts with 7.2;
they end in Automatic Repair [1][2].  Proxmox VE hides MBEC from L1
for older machine versions as a mitigation [4].

The trigger is commit 606ddbfb4c02 ("KVM: nVMX: advertise MBEC to
nested guests").  Hyper-V appears to enable CET for its guests only
when it is offered MBEC (or GMET): without the series, Windows boots
with vmx-mbec=off, and also with MBEC but without CET
(cet-ss=off,cet-ibt=off).  With CET, Hyper-V needs two things that KVM
does not provide to L1:

1. Supervisor shadow-stack control for EPT (IA32_VMX_EPT_VPID_CAP bit
   23, EPTP bit 7, EPT bit 60).  KVM never advertised it.

2. The VM-entry and VM-exit controls that load CET state.  KVM offers
   them, but with the enlightened VMCS nested_evmcs_filter_control_msr()
   removes them, and the CET fields of the eVMCS are not wired up.

This matches the report: vmx-mbec=off boots, hv-evmcs=off alone does
not.  AMD hosts are not affected: Windows with VBS and GMET boots on
7.2 and kvm-x86/next on a Zen 3 host.

Which patches a configuration needs:

- without enlightened VMCS: patches 2 to 6;
- with enlightened VMCS (hv-evmcs, or implicitly via hv-passthrough):
  patches 1 to 6.

Patch 1 is first because it is small and independent of the MMU work,
but on its own it does not make Windows boot; it carries a Fixes tag
because it is half of the fix for eVMCS users.

The series:

- Patch 1 lets L1 use the CET entry/exit controls and fields with
  eVMCS.
- Patch 2 moves the A/D tracking type of TDP SPTEs from bits 60-61 to
  bits 56-57, because the CPU interprets bit 60 as the supervisor
  shadow-stack permission when EPTP bit 7 is set.  This is the only
  layout change and is meant to have no functional effect.
- Patch 3 passes the shadow-stack bit of EPT violations to the MMU as
  PFERR_SS, as Paolo suggested in the report thread.
- Patch 4 makes the MMU treat bit 60 like the writable bit (write
  protection, unsync, dirty logging, TLB flushes), since it lets the
  guest write the page with shadow-stack accesses.
- Patch 5 implements supervisor shadow-stack control in the nested EPT
  walker, with a new role bit, and reports exit qualification bits 13
  and 14 to L1.
- Patch 6 advertises the capability and accepts EPTP bit 7.
- Patches 7 to 10 add selftests: the SSS rules of the walker, MBEC
  supervisor fetches, and the VMX capabilities that L1 needs for CET
  with MBEC, with and without eVMCS.  The last one fails without the
  series.

With CET, Hyper-V does not boot unless the capability is offered, but
it did not set EPTP bit 7 in any of my runs.  KVM cannot advertise the
capability without implementing it, hence patches 2 to 5; the Windows
boots exercise the capability and the eVMCS part, and the enforcement
in the walker is covered by the selftest of patch 8.

The VMM has to pass bit 23 of IA32_VMX_EPT_VPID_CAP to L1.  QEMU drops
EPT capability bits it does not know, so with QEMU 11.1.4 the series
alone is not enough; I will post a QEMU patch that adds the
"vmx-ept-sss" property (depending on cet-ss) to qemu-devel.

Testing, base kvm-x86/next cbe54fc3462b vs the series:

Intel Core i7-13700K, QEMU 11.1.4 with the property added, OVMF
2025.11 with Secure Boot, swtpm 0.10.1, -cpu host:
- Windows 11 Pro 25H2 (26200) with VBS, HVCI and WSL2: base fails,
  the series boots with eVMCS on and off; 5 of 5 in-guest reboots;
  Alpine runs in WSL2.  The series with an unmodified QEMU still
  fails, as expected.
- Windows 11 Enterprise evaluation (26300): Credential Guard and HVCI
  running.
- Linux L1 running an L2 and KVM selftests inside L1.
- KVM selftests and kvm-unit-tests vmx: same results with and without
  the series apart from the new tests, for all 91 x86 selftests with
  default parameters, and for the nested, MMU and Hyper-V subset also
  with kvm_intel ept=0, mbec=0 and enable_apicv=0.
- KASAN, PROVE_LOCKING and DEBUG_ATOMIC_SLEEP kernels: no reports.
- Negative tests: supervisor fetches from user-execute-only pages under
  MBEC fault the same way with and without the series.  Restricted
  supervisor shadow-stack accesses cause EPT violations as L1
  configured them; without the series L1 cannot enable supervisor
  shadow-stack control at all.  The bug over-restricted L2 and did not
  let it bypass L1's EPT permissions.

AMD Ryzen 5 5600G (regression check for the shared MMU code): Windows
with VBS and GMET boots with and without the series; KVM selftests the
same, also with kvm_amd npt=0, avic=0 and gmet=0.

Not tested: Windows Sandbox; the host lockup reported on a Xeon 6426Y
while testing the MBEC series [3] (not reproduced on my hardware); MBEC
user-mode instruction fetches (the selftest covers supervisor fetches
only).

There is no Cc: stable.  The series changes the SPTE layout and is
large for a backport; if a minimal fix for 7.2.y is preferred, I am
happy to prepare one, for example not offering MBEC to L1 when
supervisor shadow-stack control cannot be offered.

Thanks to Panayotis Katsaloulis for the report and to Paolo for the
PFERR_SS pointer.

[1] https://lore.kernel.org/kvm/58dcd595e3187d335e103a042d196cb7@panayotis.com/
[2] https://bugzilla.suse.com/show_bug.cgi?id=1277716
[3] Earlier report while testing the MBEC series (Xeon, host lockups,
    -cet-ss,-cet-ibt workaround): https://lkml.iu.edu/2605.2/05048.html
[4] Proxmox mitigation hiding MBEC from L1 for older machine versions:
    https://lore.proxmox.com/all/20260713124025.121154-4-f.ebner@proxmox.com/

---
Dennis Reußwig (10):
      KVM: nVMX: Allow CET entry/exit controls with enlightened VMCS
      KVM: x86/mmu: Move TDP A/D tracking type to SPTE bits 56-57
      KVM: VMX: Report shadow-stack accesses in EPT violations as PFERR_SS
      KVM: x86/mmu: Handle the EPT supervisor shadow-stack bit like a write bit
      KVM: nVMX: Shadow supervisor shadow-stack control of nested EPT
      KVM: nVMX: Advertise supervisor shadow-stack control for nested EPT
      KVM: selftests: Add helpers for nested EPT shadow-stack and MBEC tests
      KVM: selftests: Add nested EPT supervisor shadow-stack test
      KVM: selftests: Add nested EPT mode-based execute control test
      KVM: selftests: Check VMX capabilities for CET in L2 with MBEC

 Documentation/virt/kvm/x86/mmu.rst                 |   4 +
 arch/x86/include/asm/kvm_host.h                    |  18 +-
 arch/x86/include/asm/vmx.h                         |   5 +
 arch/x86/kvm/mmu.h                                 |   8 +-
 arch/x86/kvm/mmu/mmu.c                             |  35 +++-
 arch/x86/kvm/mmu/paging_tmpl.h                     |  90 ++++++++-
 arch/x86/kvm/mmu/spte.c                            |  34 +++-
 arch/x86/kvm/mmu/spte.h                            |  34 +++-
 arch/x86/kvm/vmx/common.h                          |   3 +
 arch/x86/kvm/vmx/hyperv_evmcs.c                    |  24 +--
 arch/x86/kvm/vmx/hyperv_evmcs.h                    |   6 +-
 arch/x86/kvm/vmx/nested.c                          |  52 +++--
 arch/x86/kvm/vmx/vmx.c                             |   4 +
 tools/testing/selftests/kvm/Makefile.kvm           |   3 +
 .../testing/selftests/kvm/include/x86/processor.h  |   5 +
 tools/testing/selftests/kvm/include/x86/vmx.h      |  11 ++
 tools/testing/selftests/kvm/lib/x86/processor.c    |   9 +-
 .../selftests/kvm/x86/vmx_nested_cet_caps_test.c   |  90 +++++++++
 .../selftests/kvm/x86/vmx_nested_mbec_test.c       | 189 ++++++++++++++++++
 .../selftests/kvm/x86/vmx_nested_sss_test.c        | 213 +++++++++++++++++++++
 20 files changed, 763 insertions(+), 74 deletions(-)
---
base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7
change-id: 20261204-nested-sss-0798eddc4d58

Best regards,
-- 
Dennis Reußwig <dennis@reusswig.codes>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 01/10] KVM: nVMX: Allow CET entry/exit controls with enlightened VMCS
  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 ` 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
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

Since commit 55a71224bd90 ("KVM: nVMX: allow MBEC with EVMCS"), L1 is
offered mode-based execute control also when it uses the enlightened
VMCS.  Windows Hyper-V then enables CET for its guests and needs the
VM-entry and VM-exit controls that load CET state.  With eVMCS,
nested_evmcs_filter_control_msr() hides them from L1, because they are
missing from EVMCS1_SUPPORTED_VMENTRY_CTRL and
EVMCS1_SUPPORTED_VMEXIT_CTRL, and the CET fields of the eVMCS are
neither in vmcs_field_to_evmcs_1[] nor copied to and from vmcs12.

Add the CET fields (GUEST_S_CET, GUEST_SSP, GUEST_INTR_SSP_TABLE,
HOST_S_CET, HOST_SSP, HOST_INTR_SSP_TABLE) to the field table, using
the clean-field groups of the entries that were commented out, copy
them in copy_enlightened_to_vmcs12() and the guest fields back in
copy_vmcs12_to_enlightened(), and allow the two controls.

This alone does not make Windows boot: Hyper-V also needs supervisor
shadow-stack control for nested EPT, which is added later in this
series.

Fixes: 55a71224bd90 ("KVM: nVMX: allow MBEC with EVMCS")
Reported-by: Panayotis Katsaloulis <panayotis@panayotis.com>
Closes: https://lore.kernel.org/kvm/58dcd595e3187d335e103a042d196cb7@panayotis.com/
Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
 arch/x86/kvm/vmx/hyperv_evmcs.c | 24 ++++++++++++------------
 arch/x86/kvm/vmx/hyperv_evmcs.h |  6 ++++--
 arch/x86/kvm/vmx/nested.c       | 31 +++++++++++++------------------
 3 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/arch/x86/kvm/vmx/hyperv_evmcs.c b/arch/x86/kvm/vmx/hyperv_evmcs.c
index cc728c9a3de5..f00e4edf1e6c 100644
--- a/arch/x86/kvm/vmx/hyperv_evmcs.c
+++ b/arch/x86/kvm/vmx/hyperv_evmcs.c
@@ -128,23 +128,23 @@ const struct evmcs_field vmcs_field_to_evmcs_1[] = {
 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),
 	EVMCS1_FIELD(TSC_MULTIPLIER, tsc_multiplier,
 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),
+	EVMCS1_FIELD(GUEST_S_CET, guest_ia32_s_cet,
+		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
+	EVMCS1_FIELD(GUEST_SSP, guest_ssp,
+		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC),
+	EVMCS1_FIELD(GUEST_INTR_SSP_TABLE, guest_ia32_int_ssp_table_addr,
+		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
+	EVMCS1_FIELD(HOST_S_CET, host_ia32_s_cet,
+		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
+	EVMCS1_FIELD(HOST_SSP, host_ssp,
+		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
+	EVMCS1_FIELD(HOST_INTR_SSP_TABLE, host_ia32_int_ssp_table_addr,
+		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
 	/*
 	 * Not used by KVM:
 	 *
-	 * EVMCS1_FIELD(0x00006828, guest_ia32_s_cet,
-	 *	     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
-	 * EVMCS1_FIELD(0x0000682A, guest_ssp,
-	 *	     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC),
-	 * EVMCS1_FIELD(0x0000682C, guest_ia32_int_ssp_table_addr,
-	 *	     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
 	 * EVMCS1_FIELD(0x00002816, guest_ia32_lbr_ctl,
 	 *	     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
-	 * EVMCS1_FIELD(0x00006C18, host_ia32_s_cet,
-	 *	     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
-	 * EVMCS1_FIELD(0x00006C1A, host_ssp,
-	 *	     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
-	 * EVMCS1_FIELD(0x00006C1C, host_ia32_int_ssp_table_addr,
-	 *	     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
 	 */
 
 	/* 64 bit read only */
diff --git a/arch/x86/kvm/vmx/hyperv_evmcs.h b/arch/x86/kvm/vmx/hyperv_evmcs.h
index 052b62b8ed8e..a6e9210e4fa5 100644
--- a/arch/x86/kvm/vmx/hyperv_evmcs.h
+++ b/arch/x86/kvm/vmx/hyperv_evmcs.h
@@ -104,7 +104,8 @@
 	 VM_EXIT_LOAD_IA32_EFER |					\
 	 VM_EXIT_CLEAR_BNDCFGS |					\
 	 VM_EXIT_PT_CONCEAL_PIP |					\
-	 VM_EXIT_CLEAR_IA32_RTIT_CTL)
+	 VM_EXIT_CLEAR_IA32_RTIT_CTL |					\
+	 VM_EXIT_LOAD_CET_STATE)
 
 #define EVMCS1_SUPPORTED_VMENTRY_CTRL					\
 	(VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |				\
@@ -115,7 +116,8 @@
 	 VM_ENTRY_LOAD_IA32_EFER |					\
 	 VM_ENTRY_LOAD_BNDCFGS |					\
 	 VM_ENTRY_PT_CONCEAL_PIP |					\
-	 VM_ENTRY_LOAD_IA32_RTIT_CTL)
+	 VM_ENTRY_LOAD_IA32_RTIT_CTL |					\
+	 VM_ENTRY_LOAD_CET_STATE)
 
 #define EVMCS1_SUPPORTED_VMFUNC (0)
 
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 1a98f28651f7..a7c8bbdfe4ca 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -1758,10 +1758,7 @@ static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields
 		vmcs12->guest_rflags = evmcs->guest_rflags;
 		vmcs12->guest_interruptibility_info =
 			evmcs->guest_interruptibility_info;
-		/*
-		 * Not present in struct vmcs12:
-		 * vmcs12->guest_ssp = evmcs->guest_ssp;
-		 */
+		vmcs12->guest_ssp = evmcs->guest_ssp;
 	}
 
 	if (unlikely(!(hv_clean_fields &
@@ -1809,12 +1806,9 @@ static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields
 		vmcs12->host_gs_selector = evmcs->host_gs_selector;
 		vmcs12->host_tr_selector = evmcs->host_tr_selector;
 		vmcs12->host_ia32_perf_global_ctrl = evmcs->host_ia32_perf_global_ctrl;
-		/*
-		 * Not present in struct vmcs12:
-		 * vmcs12->host_ia32_s_cet = evmcs->host_ia32_s_cet;
-		 * vmcs12->host_ssp = evmcs->host_ssp;
-		 * vmcs12->host_ia32_int_ssp_table_addr = evmcs->host_ia32_int_ssp_table_addr;
-		 */
+		vmcs12->host_s_cet = evmcs->host_ia32_s_cet;
+		vmcs12->host_ssp = evmcs->host_ssp;
+		vmcs12->host_ssp_tbl = evmcs->host_ia32_int_ssp_table_addr;
 	}
 
 	if (unlikely(!(hv_clean_fields &
@@ -1932,11 +1926,11 @@ static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields
 		vmcs12->guest_activity_state = evmcs->guest_activity_state;
 		vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
 		vmcs12->guest_ia32_perf_global_ctrl = evmcs->guest_ia32_perf_global_ctrl;
+		vmcs12->guest_s_cet = evmcs->guest_ia32_s_cet;
+		vmcs12->guest_ssp_tbl = evmcs->guest_ia32_int_ssp_table_addr;
 		/*
 		 * Not present in struct vmcs12:
-		 * vmcs12->guest_ia32_s_cet = evmcs->guest_ia32_s_cet;
 		 * vmcs12->guest_ia32_lbr_ctl = evmcs->guest_ia32_lbr_ctl;
-		 * vmcs12->guest_ia32_int_ssp_table_addr = evmcs->guest_ia32_int_ssp_table_addr;
 		 */
 	}
 
@@ -2046,6 +2040,9 @@ static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
 	 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
 	 * evmcs->guest_ia32_perf_global_ctrl = vmcs12->guest_ia32_perf_global_ctrl;
 	 * evmcs->host_ia32_perf_global_ctrl = vmcs12->host_ia32_perf_global_ctrl;
+	 * evmcs->host_ia32_s_cet = vmcs12->host_s_cet;
+	 * evmcs->host_ssp = vmcs12->host_ssp;
+	 * evmcs->host_ia32_int_ssp_table_addr = vmcs12->host_ssp_tbl;
 	 * evmcs->encls_exiting_bitmap = vmcs12->encls_exiting_bitmap;
 	 * evmcs->tsc_multiplier = vmcs12->tsc_multiplier;
 	 *
@@ -2054,13 +2051,7 @@ static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
 	 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
 	 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
 	 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
-	 * evmcs->host_ia32_s_cet = vmcs12->host_ia32_s_cet;
-	 * evmcs->host_ssp = vmcs12->host_ssp;
-	 * evmcs->host_ia32_int_ssp_table_addr = vmcs12->host_ia32_int_ssp_table_addr;
-	 * evmcs->guest_ia32_s_cet = vmcs12->guest_ia32_s_cet;
 	 * evmcs->guest_ia32_lbr_ctl = vmcs12->guest_ia32_lbr_ctl;
-	 * evmcs->guest_ia32_int_ssp_table_addr = vmcs12->guest_ia32_int_ssp_table_addr;
-	 * evmcs->guest_ssp = vmcs12->guest_ssp;
 	 */
 
 	evmcs->guest_es_selector = vmcs12->guest_es_selector;
@@ -2154,6 +2145,10 @@ static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
 
 	evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
 
+	evmcs->guest_ia32_s_cet = vmcs12->guest_s_cet;
+	evmcs->guest_ssp = vmcs12->guest_ssp;
+	evmcs->guest_ia32_int_ssp_table_addr = vmcs12->guest_ssp_tbl;
+
 	return;
 #else /* CONFIG_KVM_HYPERV */
 	KVM_BUG_ON(1, vmx->vcpu.kvm);

-- 
2.53.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 02/10] KVM: x86/mmu: Move TDP A/D tracking type to SPTE bits 56-57
  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 ` 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
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

Bits 60 and 61 of TDP SPTEs hold the type of A/D tracking.  If bit 7 of
the EPTP is set, the CPU uses bit 60 of an EPT leaf as the supervisor
shadow-stack permission (SDM Vol. 3C, 31.3.3.2).  KVM needs to set that
bit in shadow EPT SPTEs to support supervisor shadow-stack control for
nested EPT, so it cannot hold software state.

Move the A/D tracking type to bits 56 and 57, which are unused since
commit be4e45f360a2 ("KVM: x86/mmu: shuffle high bits of SPTEs in
preparation for MBEC").  The CPU ignores bit 56.  Bit 57 is "verify
guest paging" only if the guest-paging verification VM-execution
control is set, which KVM never does; bit 61 was in the same situation
with the sub-page write permission control.  The existing static
asserts against the host-writable, MMU-writable and access-tracking
bits still hold.

No functional change intended.

Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
 arch/x86/kvm/mmu/spte.h | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h
index e730717824b3..237a0484367a 100644
--- a/arch/x86/kvm/mmu/spte.h
+++ b/arch/x86/kvm/mmu/spte.h
@@ -22,16 +22,19 @@
  * - bits 52, 54: saved X-R bits for access tracking when EPT does not have A/D
  * - bits 53 (EPT only): host writable
  * - bits 55 (EPT only): MMU-writable
- * - bits 56-59: unused
- * - bits 60-61: type of A/D tracking
+ * - bits 56-57: type of A/D tracking
+ * - bits 58-61: unused; bit 60 is the EPT supervisor shadow-stack page bit
+ *   when EPTP[7] is set, so it must not be used for software state
  * - bits 62 (EPT only): saved XU bit for disabled AD
  */
 
 /*
  * TDP SPTES (more specifically, EPT SPTEs) may not have A/D bits, and may also
  * be restricted to using write-protection (for L2 when CPU dirty logging, i.e.
- * PML, is enabled).  Use bits 60 and 61 to hold the type of A/D tracking that
- * is must be employed for a given TDP SPTE.
+ * PML, is enabled).  Use bits 56 and 57 to hold the type of A/D tracking that
+ * is must be employed for a given TDP SPTE.  Bit 57 is "verify guest paging"
+ * only if the guest-paging verification VM-execution control is set, which
+ * KVM never does.
  *
  * Note, the "enabled" mask must be '0', as bits 62:52 are _reserved_ for PAE
  * paging, including NPT PAE.  This scheme works because legacy shadow paging
@@ -39,7 +42,7 @@
  * TDP with CPU dirty logging (PML).  If NPT ever gains PML-like support, it
  * must be restricted to 64-bit KVM.
  */
-#define SPTE_TDP_AD_SHIFT		60
+#define SPTE_TDP_AD_SHIFT		56
 #define SPTE_TDP_AD_MASK		(3ULL << SPTE_TDP_AD_SHIFT)
 #define SPTE_TDP_AD_ENABLED		(0ULL << SPTE_TDP_AD_SHIFT)
 #define SPTE_TDP_AD_DISABLED		(1ULL << SPTE_TDP_AD_SHIFT)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 03/10] KVM: VMX: Report shadow-stack accesses in EPT violations as PFERR_SS
  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 ` 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
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

Bit 13 of the exit qualification of an EPT violation is set if the
access was a shadow-stack access.  __vt_handle_ept_violation() does not
pass it on, so the MMU cannot tell shadow-stack accesses from ordinary
ones.  Translate it to PFERR_SS_MASK.  This is the VMX counterpart of
commit e90ab3669181 ("KVM: x86/mmu: Cope with the SS bit being set
in #NPF exit codes").

No functional change on its own: permission_fault() ignores
PFERR_SS_MASK.  Supervisor shadow-stack control for nested EPT uses the
bit in a later patch.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
 arch/x86/include/asm/vmx.h | 1 +
 arch/x86/kvm/vmx/common.h  | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h
index e49b836c7313..20e2ad19d65b 100644
--- a/arch/x86/include/asm/vmx.h
+++ b/arch/x86/include/asm/vmx.h
@@ -630,6 +630,7 @@ enum vm_entry_failure_code {
 #define EPT_VIOLATION_GVA_USER		BIT(9)
 #define EPT_VIOLATION_GVA_WRITABLE	BIT(10)
 #define EPT_VIOLATION_GVA_NX		BIT(11)
+#define EPT_VIOLATION_SHADOW_STACK	BIT(13)
 
 #define EPT_VIOLATION_RWX_TO_PROT(__epte) (((__epte) & VMX_EPT_RWX_MASK) << 3)
 #define EPT_VIOLATION_USER_EXEC_TO_PROT(__epte) (((__epte) & VMX_EPT_USER_EXECUTABLE_MASK) >> 4)
diff --git a/arch/x86/kvm/vmx/common.h b/arch/x86/kvm/vmx/common.h
index b2905335a2d2..eb60ba97074c 100644
--- a/arch/x86/kvm/vmx/common.h
+++ b/arch/x86/kvm/vmx/common.h
@@ -144,6 +144,9 @@ static inline int __vt_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa,
 	/* Is it a fetch fault? */
 	error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
 		      ? PFERR_FETCH_MASK : 0;
+	/* Is it a shadow-stack access? */
+	error_code |= (exit_qualification & EPT_VIOLATION_SHADOW_STACK)
+		      ? PFERR_SS_MASK : 0;
 	/* ept page table entry is present?  */
 	error_code |= (exit_qualification &
 		       (EPT_VIOLATION_PROT_MASK & ~EPT_VIOLATION_PROT_USER_EXEC))

-- 
2.53.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 04/10] KVM: x86/mmu: Handle the EPT supervisor shadow-stack bit like a write bit
  2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
                   ` (2 preceding siblings ...)
  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 ` Dennis Reußwig
  2026-12-03 23:49 ` [PATCH 05/10] KVM: nVMX: Shadow supervisor shadow-stack control of nested EPT Dennis Reußwig
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

With supervisor shadow-stack control (EPTP bit 7), bit 60 of an EPT
leaf allows supervisor shadow-stack accesses, including shadow-stack
writes, even if the write bit of the leaf is clear (SDM Vol. 3C,
31.3.3.2).  An SPTE with bit 60 set therefore lets the guest write the
page, and the MMU must handle the bit like the writable bit:

- make_spte() grants it only where writes are allowed, unsyncs the
  shadowed page tables, sets the Dirty bit and marks the page dirty in
  the dirty log;
- spte_write_protect() and write-protection based dirty logging clear
  it, and losing it requires a TLB flush;
- spte_needs_atomic_update() covers such SPTEs without the Dirty bit,
  and prefetch looks up the memslot for dirty logging.

The new ACC_EPT_SSS_MASK carries the permission from the guest walker
to make_spte() and in shadowed_translation[].  It is never part of
role.access or of the permission bitmasks.

Nothing sets ACC_EPT_SSS_MASK yet; nested EPT supervisor shadow-stack
control does in the next patch.

Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
checkpatch --strict suggests BIT_ULL() for the (1ull << n) definitions that
this patch and the next two add to arch/x86/include/asm/vmx.h; they follow
the style of the neighbouring EPT definitions.
---
 arch/x86/include/asm/vmx.h |  1 +
 arch/x86/kvm/mmu.h         |  6 ++++++
 arch/x86/kvm/mmu/mmu.c     | 23 ++++++++++++++++-------
 arch/x86/kvm/mmu/spte.c    | 34 ++++++++++++++++++++++++----------
 arch/x86/kvm/mmu/spte.h    | 21 ++++++++++++++++++++-
 5 files changed, 67 insertions(+), 18 deletions(-)

diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h
index 20e2ad19d65b..79646365369f 100644
--- a/arch/x86/include/asm/vmx.h
+++ b/arch/x86/include/asm/vmx.h
@@ -570,6 +570,7 @@ enum vmcs_field {
 #define VMX_EPT_ACCESS_BIT			(1ull << 8)
 #define VMX_EPT_DIRTY_BIT			(1ull << 9)
 #define VMX_EPT_SUPPRESS_VE_BIT			(1ull << 63)
+#define VMX_EPT_SSS_MASK			(1ull << 60)
 
 #define VMX_EPT_RWX_MASK                        (VMX_EPT_READABLE_MASK |       \
 						 VMX_EPT_WRITABLE_MASK |       \
diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
index 2f0a6f2f5021..3d4de461e948 100644
--- a/arch/x86/kvm/mmu.h
+++ b/arch/x86/kvm/mmu.h
@@ -56,6 +56,12 @@ extern bool __read_mostly eager_page_split;
 #define ACC_USER_EXEC_MASK ACC_USER_MASK /* EPT only */
 #define ACC_EXEC_MASK    8
 #define ACC_ALL          (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK | ACC_READ_MASK)
+/*
+ * Nested EPT only: the leaf grants supervisor shadow-stack accesses (EPT bit
+ * 60 with EPTP bit 7 set).  Passed with the access of a leaf SPTE and stored
+ * with it, but never part of role.access or of the permission bitmasks.
+ */
+#define ACC_EPT_SSS_MASK 16
 
 #define KVM_MMU_CR4_ROLE_BITS (X86_CR4_PSE | X86_CR4_PAE | X86_CR4_LA57 | \
 			       X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index f89e9f8a6cc2..d2aa576f97cd 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -672,7 +672,7 @@ static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
 static u32 kvm_mmu_page_get_access(struct kvm_mmu_page *sp, int index)
 {
 	if (sp->shadowed_translation)
-		return sp->shadowed_translation[index] & ACC_ALL;
+		return sp->shadowed_translation[index] & (ACC_ALL | ACC_EPT_SSS_MASK);
 
 	/*
 	 * For direct MMUs (e.g. TDP or non-paging guests) or passthrough SPs,
@@ -697,7 +697,12 @@ static void kvm_mmu_page_set_translation(struct kvm_mmu_page *sp, int index,
 		return;
 	}
 
-	WARN_ONCE(access != kvm_mmu_page_get_access(sp, index),
+	/*
+	 * role.access cannot hold ACC_EPT_SSS_MASK.  Direct SPs that shadow a
+	 * guest huge page carry the supervisor shadow-stack grant only in
+	 * their SPTEs, which inherit it on splits.
+	 */
+	WARN_ONCE((access & ~ACC_EPT_SSS_MASK) != kvm_mmu_page_get_access(sp, index),
 	          "access mismatch under %s page %llx (expected %u, got %u)\n",
 	          sp->role.passthrough ? "passthrough" : "direct",
 	          sp->gfn, kvm_mmu_page_get_access(sp, index), access);
@@ -1377,13 +1382,13 @@ static bool spte_write_protect(u64 *sptep, bool pt_protect)
 {
 	u64 spte = *sptep;
 
-	if (!is_writable_pte(spte) &&
+	if (!is_writable_pte(spte) && !is_sss_spte(spte) &&
 	    !(pt_protect && is_mmu_writable_spte(spte)))
 		return false;
 
 	if (pt_protect)
 		spte &= ~shadow_mmu_writable_mask;
-	spte = spte & ~PT_WRITABLE_MASK;
+	spte &= ~(PT_WRITABLE_MASK | VMX_EPT_SSS_MASK);
 
 	return mmu_spte_update(sptep, spte);
 }
@@ -1424,11 +1429,14 @@ static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
 	bool flush = false;
 
 	for_each_rmap_spte(rmap_head, &iter, sptep) {
-		if (spte_ad_need_write_protect(*sptep))
+		if (spte_ad_need_write_protect(*sptep)) {
 			flush |= test_and_clear_bit(PT_WRITABLE_SHIFT,
 						    (unsigned long *)sptep);
-		else
+			flush |= test_and_clear_bit(SPTE_EPT_SSS_SHIFT,
+						    (unsigned long *)sptep);
+		} else {
 			flush |= spte_clear_dirty(sptep);
+		}
 	}
 
 	return flush;
@@ -3245,7 +3253,8 @@ static bool kvm_mmu_prefetch_sptes(struct kvm_vcpu *vcpu, gfn_t gfn, u64 *sptep,
 	if (WARN_ON_ONCE(nr_pages > PTE_PREFETCH_NUM))
 		return false;
 
-	slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
+	slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn,
+					   access & (ACC_WRITE_MASK | ACC_EPT_SSS_MASK));
 	if (!slot)
 		return false;
 
diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c
index 5fc27e9733b3..5251d0384181 100644
--- a/arch/x86/kvm/mmu/spte.c
+++ b/arch/x86/kvm/mmu/spte.c
@@ -180,9 +180,11 @@ bool spte_needs_atomic_update(u64 spte)
 	 * Dirty and Accessed bits can be set by the CPU.  Ignore the Accessed
 	 * bit, as KVM tolerates false negatives/positives, e.g. KVM doesn't
 	 * invalidate TLBs when aging SPTEs, and so it's safe to clobber the
-	 * Accessed bit (and rare in practice).
+	 * Accessed bit (and rare in practice).  Shadow-stack writes through a
+	 * supervisor shadow-stack SPTE also set the Dirty bit.
 	 */
-	return is_writable_pte(spte) && !(spte & shadow_dirty_mask);
+	return (is_writable_pte(spte) || is_sss_spte(spte)) &&
+	       !(spte & shadow_dirty_mask);
 }
 
 bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
@@ -249,14 +251,18 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
 	if (host_writable)
 		spte |= shadow_host_writable_mask;
 	else
-		pte_access &= ~ACC_WRITE_MASK;
+		pte_access &= ~(ACC_WRITE_MASK | ACC_EPT_SSS_MASK);
 
 	if (shadow_me_value && !kvm_is_mmio_pfn(pfn, &is_host_mmio))
 		spte |= shadow_me_value;
 
 	spte |= (u64)pfn << PAGE_SHIFT;
 
-	if (pte_access & ACC_WRITE_MASK) {
+	/*
+	 * A supervisor shadow-stack SPTE lets L2 write the page with shadow-stack
+	 * accesses, so it is subject to the same rules as a writable SPTE.
+	 */
+	if (pte_access & (ACC_WRITE_MASK | ACC_EPT_SSS_MASK)) {
 		/*
 		 * Unsync shadow pages that are reachable by the new, writable
 		 * SPTE.  Write-protect the SPTE if the page can't be unsync'd,
@@ -270,12 +276,19 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
 		 * changing PFNs without first zapping the old SPTE, which is
 		 * guaranteed by both the shadow MMU and the TDP MMU.
 		 */
-		if ((!is_last_spte(old_spte, level) || !is_writable_pte(old_spte)) &&
-		    mmu_try_to_unsync_pages(vcpu->kvm, slot, gfn, synchronizing, prefetch))
+		if ((!is_last_spte(old_spte, level) ||
+		     !(is_writable_pte(old_spte) ||
+		       (is_shadow_present_pte(old_spte) && is_sss_spte(old_spte)))) &&
+		    mmu_try_to_unsync_pages(vcpu->kvm, slot, gfn, synchronizing, prefetch)) {
 			wrprot = true;
-		else
-			spte |= PT_WRITABLE_MASK | shadow_mmu_writable_mask |
-				shadow_dirty_mask;
+		} else {
+			if (pte_access & ACC_WRITE_MASK)
+				spte |= PT_WRITABLE_MASK | shadow_mmu_writable_mask;
+			if (pte_access & ACC_EPT_SSS_MASK)
+				spte |= VMX_EPT_SSS_MASK;
+
+			spte |= shadow_dirty_mask;
+		}
 	}
 
 	if (prefetch && !synchronizing)
@@ -290,7 +303,8 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
 	 * Unlike folios, memslots can be safely marked dirty out of mmu_lock,
 	 * i.e. in the fast page fault handler.
 	 */
-	if ((spte & PT_WRITABLE_MASK) && kvm_slot_dirty_track_enabled(slot)) {
+	if ((spte & (PT_WRITABLE_MASK | VMX_EPT_SSS_MASK)) &&
+	    kvm_slot_dirty_track_enabled(slot)) {
 		/* Enforced by kvm_mmu_hugepage_adjust. */
 		WARN_ON_ONCE(level > PG_LEVEL_4K);
 		mark_page_dirty_in_slot(vcpu->kvm, slot, gfn);
diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h
index 237a0484367a..fa586c84204a 100644
--- a/arch/x86/kvm/mmu/spte.h
+++ b/arch/x86/kvm/mmu/spte.h
@@ -97,6 +97,10 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SPTE_TDP_AD_MASK));
 static_assert(!(EPT_SPTE_HOST_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));
 static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));
 
+/* Bit 60 is the EPT supervisor shadow-stack bit, see is_sss_spte(). */
+static_assert(!(VMX_EPT_SSS_MASK & (SPTE_TDP_AD_MASK | SHADOW_ACC_TRACK_SAVED_MASK |
+				    EPT_SPTE_HOST_WRITABLE | EPT_SPTE_MMU_WRITABLE)));
+
 /* Defined only to keep the above static asserts readable. */
 #undef SHADOW_ACC_TRACK_SAVED_MASK
 
@@ -487,6 +491,20 @@ static inline bool is_mmu_writable_spte(u64 spte)
 	return spte & shadow_mmu_writable_mask;
 }
 
+/*
+ * Bit 60 of a shadow-present SPTE is set only by nested EPT with supervisor
+ * shadow-stack control, where it lets L2 write supervisor shadow stacks even
+ * if PT_WRITABLE_MASK is clear.  KVM must treat it like a write permission:
+ * grant it only where writes are allowed and drop it when write-protecting.
+ */
+#define SPTE_EPT_SSS_SHIFT 60
+static_assert(BIT_ULL(SPTE_EPT_SSS_SHIFT) == VMX_EPT_SSS_MASK);
+
+static inline bool is_sss_spte(u64 spte)
+{
+	return spte & VMX_EPT_SSS_MASK;
+}
+
 /*
  * Returns true if the access indicated by @fault is forbidden by the existing
  * SPTE protections.
@@ -548,7 +566,8 @@ static inline bool spte_permission_fault(struct kvm_mmu *mmu, u64 spte,
  */
 static inline bool leaf_spte_change_needs_tlb_flush(u64 old_spte, u64 new_spte)
 {
-	return is_mmu_writable_spte(old_spte) && !is_mmu_writable_spte(new_spte);
+	return (is_mmu_writable_spte(old_spte) && !is_mmu_writable_spte(new_spte)) ||
+	       (is_sss_spte(old_spte) && !is_sss_spte(new_spte));
 }
 
 static inline u64 get_mmio_spte_generation(u64 spte)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 05/10] KVM: nVMX: Shadow supervisor shadow-stack control of nested EPT
  2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
                   ` (3 preceding siblings ...)
  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 ` Dennis Reußwig
  2026-12-03 23:49 ` [PATCH 06/10] KVM: nVMX: Advertise supervisor shadow-stack control for " Dennis Reußwig
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

If bit 7 of L1's EPTP is set, supervisor shadow-stack accesses of L2
are allowed if every entry of the walk is readable, every non-leaf
entry is writable and the leaf sets bit 60; the write bit of the leaf
does not matter (SDM Vol. 3C, 31.3.3.2).  User shadow-stack accesses
and the accesses to the guest paging structures follow the normal
rules.

Implement this in the nested EPT MMU:

- add role.ept_sss, so that shadow pages for EPTP bit 7 set and clear
  are not shared, and set bit 7 in the EPTP of such roots so that the
  CPU enforces bit 60 of the shadow SPTEs;
- in FNAME(walk_addr_generic), check supervisor shadow-stack accesses
  against bit 60 and set the Dirty flag for them like for writes, and
  pass the permission to the SPTEs in FNAME(fetch),
  FNAME(prefetch_gpte) and FNAME(sync_spte);
- report bit 13 (shadow-stack access) and bit 14 (bit 60 of the leaf)
  in the exit qualification of EPT violations reflected to L1.

The role grows to 22 bits.  Update the comment that bounds the number
of shadow pages per gfn: it was already one bit low (21 - 2 - 2 - 2 =
15, not 14) and named execonly, which is in the extended role.

L1 cannot set EPTP bit 7 yet, because nested_vmx_check_eptp() rejects
it; the next patch allows it.

Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
 Documentation/virt/kvm/x86/mmu.rst |  4 ++
 arch/x86/include/asm/kvm_host.h    | 18 +++++---
 arch/x86/include/asm/vmx.h         |  2 +
 arch/x86/kvm/mmu.h                 |  2 +-
 arch/x86/kvm/mmu/mmu.c             | 12 +++--
 arch/x86/kvm/mmu/paging_tmpl.h     | 90 +++++++++++++++++++++++++++++++++++---
 arch/x86/kvm/vmx/nested.c          |  1 +
 arch/x86/kvm/vmx/vmx.c             |  4 ++
 8 files changed, 119 insertions(+), 14 deletions(-)

diff --git a/Documentation/virt/kvm/x86/mmu.rst b/Documentation/virt/kvm/x86/mmu.rst
index 666aa179601a..4067b483876e 100644
--- a/Documentation/virt/kvm/x86/mmu.rst
+++ b/Documentation/virt/kvm/x86/mmu.rst
@@ -206,6 +206,10 @@ Shadow pages contain the following information:
     The page is not backed by a guest page table, but its first entry
     points to one.  This is set if NPT uses 5-level page tables (host
     CR4.LA57=1) and is shadowing L1's 4-level NPT (L1 CR4.LA57=0).
+  role.ept_sss:
+    Is 1 if the page shadows L1 EPT with supervisor shadow-stack control
+    (EPTP bit 7).  Bit 60 of a leaf SPTE then grants L2 supervisor
+    shadow-stack accesses, and KVM treats it like a write permission.
   mmu_valid_gen:
     The MMU generation of this page, used to fast zap of all MMU pages within a
     VM without blocking vCPUs too long. Specifically, KVM updates the per-VM
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 3efef3415f8e..43e582507e39 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -319,14 +319,14 @@ extern struct kvm_host_values kvm_host;
  * the number of unique SPs that can theoretically be created is 2^n, where n
  * is the number of bits that are used to compute the role.
  *
- * But, even though there are 21 bits in the mask below, not all combinations
+ * But, even though there are 22 bits in the mask below, not all combinations
  * of modes and flags are possible:
  *
  *   - invalid shadow pages are not accounted, mirror pages are not shadowed,
- *     so the bits are effectively 19.
+ *     so the bits are effectively 20.
  *
  *   - quadrant will only be used if has_4_byte_gpte=1 (non-PAE paging);
- *     execonly and ad_disabled are only used for nested EPT which has
+ *     ad_disabled and ept_sss are only used for nested EPT which has
  *     has_4_byte_gpte=0.  Therefore, 2 bits are always unused.
  *
  *   - the 4 bits of level are effectively limited to the values 2/3/4/5,
@@ -338,7 +338,7 @@ extern struct kvm_host_values kvm_host;
  *     therefore these two bits only give rise to 3 possibilities.
  *
  * Therefore, the maximum number of possible upper-level shadow pages for a
- * single gfn is a bit less than 2^14.
+ * single gfn is a bit less than 2^16.
  */
 union kvm_mmu_page_role {
 	u32 word;
@@ -366,7 +366,15 @@ union kvm_mmu_page_role {
 		 */
 		unsigned cr4_smep:1;
 
-		unsigned:3;
+		/*
+		 * Nested EPT with supervisor shadow-stack control (EPTP bit 7).
+		 * EPT bit 60 of a leaf SPTE grants supervisor shadow-stack
+		 * accesses only if the EPTP sets bit 7, so SPTEs built for one
+		 * setting are not valid for the other.
+		 */
+		unsigned ept_sss:1;
+
+		unsigned:2;
 
 		/*
 		 * This is left at the top of the word so that
diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h
index 79646365369f..df6feba112f7 100644
--- a/arch/x86/include/asm/vmx.h
+++ b/arch/x86/include/asm/vmx.h
@@ -559,6 +559,7 @@ enum vmcs_field {
 #define VMX_EPTP_PWL_4				0x18ull
 #define VMX_EPTP_PWL_5				0x20ull
 #define VMX_EPTP_AD_ENABLE_BIT			(1ull << 6)
+#define VMX_EPTP_SSS_BIT			(1ull << 7)
 /* The EPTP memtype is encoded in bits 2:0, i.e. doesn't need to be shifted. */
 #define VMX_EPTP_MT_MASK			0x7ull
 #define VMX_EPTP_MT_WB				X86_MEMTYPE_WB
@@ -632,6 +633,7 @@ enum vm_entry_failure_code {
 #define EPT_VIOLATION_GVA_WRITABLE	BIT(10)
 #define EPT_VIOLATION_GVA_NX		BIT(11)
 #define EPT_VIOLATION_SHADOW_STACK	BIT(13)
+#define EPT_VIOLATION_SSS_PAGE		BIT(14)
 
 #define EPT_VIOLATION_RWX_TO_PROT(__epte) (((__epte) & VMX_EPT_RWX_MASK) << 3)
 #define EPT_VIOLATION_USER_EXEC_TO_PROT(__epte) (((__epte) & VMX_EPT_USER_EXECUTABLE_MASK) >> 4)
diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
index 3d4de461e948..09f6f48ebc5f 100644
--- a/arch/x86/kvm/mmu.h
+++ b/arch/x86/kvm/mmu.h
@@ -151,7 +151,7 @@ void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr4,
 			     u64 efer, gpa_t nested_cr3, u64 misc_ctl);
 void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
 			     int huge_page_level, bool accessed_dirty,
-			     bool mbec, gpa_t new_eptp);
+			     bool mbec, bool sss, gpa_t new_eptp);
 
 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
 		       void *insn, int insn_len);
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index d2aa576f97cd..39637dca870f 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -246,6 +246,11 @@ static inline bool has_pferr_fetch(struct kvm_pagewalk *w)
 	return w->cpu_role.ext.has_pferr_fetch;
 }
 
+static inline bool is_ept_sss(struct kvm_pagewalk *w)
+{
+	return w->cpu_role.base.ept_sss;
+}
+
 static inline bool is_cr0_pg(struct kvm_pagewalk *w)
 {
         return w->cpu_role.base.level > 0;
@@ -6217,7 +6222,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_init_shadow_npt_mmu);
 
 static union kvm_cpu_role
 kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty,
-				   bool execonly, u8 level, bool mbec)
+				   bool execonly, u8 level, bool mbec, bool sss)
 {
 	union kvm_cpu_role role = {0};
 
@@ -6228,6 +6233,7 @@ kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty,
 	WARN_ON_ONCE(is_smm(vcpu));
 	role.base.level = level;
 	role.base.cr4_smep = mbec;
+	role.base.ept_sss = sss;
 	role.base.has_4_byte_gpte = false;
 	role.base.direct = false;
 	role.base.ad_disabled = !accessed_dirty;
@@ -6243,13 +6249,13 @@ kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty,
 
 void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
 			     int huge_page_level, bool accessed_dirty,
-			     bool mbec, gpa_t new_eptp)
+			     bool mbec, bool sss, gpa_t new_eptp)
 {
 	struct kvm_mmu *context = &vcpu->arch.guest_mmu;
 	u8 level = vmx_eptp_page_walk_level(new_eptp);
 	union kvm_cpu_role new_mode =
 		kvm_calc_shadow_ept_root_page_role(vcpu, accessed_dirty,
-						   execonly, level, mbec);
+						   execonly, level, mbec, sss);
 
 	struct kvm_pagewalk *ngpa_walk = &vcpu->arch.ngpa_walk;
 
diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h
index 939a10fc1110..02421e74ec36 100644
--- a/arch/x86/kvm/mmu/paging_tmpl.h
+++ b/arch/x86/kvm/mmu/paging_tmpl.h
@@ -88,6 +88,8 @@ struct guest_walker {
 	bool pte_writable[PT_MAX_FULL_LEVELS];
 	unsigned int pt_access[PT_MAX_FULL_LEVELS];
 	unsigned int pte_access;
+	/* ACC_EPT_SSS_MASK if the leaf grants supervisor shadow-stack accesses. */
+	unsigned int sss_access;
 	gfn_t gfn;
 	struct x86_exception fault;
 };
@@ -124,6 +126,55 @@ static inline void FNAME(protect_clean_gpte)(struct kvm_pagewalk *w, unsigned *a
 	*access &= mask;
 }
 
+/*
+ * Supervisor shadow-stack accesses through nested EPT with EPTP bit 7 set are
+ * governed by bit 60 of the leaf instead of its write bit: the access is
+ * allowed if the leaf sets bit 60, every entry is readable and every non-leaf
+ * entry is writable (SDM Vol. 3C 31.3.3.2).  User shadow-stack accesses keep
+ * the normal rules.  For EPT, PFERR_USER_MASK reports a user-mode linear
+ * address (advanced VM-exit information).  The CPU also reports the guest
+ * paging-structure accesses of a shadow-stack access as shadow-stack
+ * accesses; those are ordinary reads and writes of the paging structure.
+ */
+static inline bool FNAME(is_sss_access)(struct kvm_pagewalk *w, u64 access)
+{
+#if PTTYPE == PTTYPE_EPT
+	return is_ept_sss(w) && (access & PFERR_SS_MASK) &&
+	       !(access & (PFERR_USER_MASK | PFERR_GUEST_PAGE_MASK));
+#else
+	return false;
+#endif
+}
+
+/*
+ * Return ACC_EPT_SSS_MASK if @gpte grants supervisor shadow-stack accesses.
+ * @pt_access holds the ACC_* permissions of the non-leaf entries above it.
+ * Bit 60 also allows shadow-stack writes, so it is withheld from leaves whose
+ * Dirty flag is clear unless @dirty: the first shadow-stack access then
+ * faults and the walker sets the Dirty flag, as for writes.
+ */
+static inline unsigned int FNAME(gpte_sss_access)(struct kvm_pagewalk *w,
+						  unsigned int pt_access,
+						  pt_element_t gpte, bool dirty)
+{
+#if PTTYPE == PTTYPE_EPT
+	if (!is_ept_sss(w) || !(gpte & VMX_EPT_SSS_MASK) ||
+	    !(gpte & VMX_EPT_READABLE_MASK))
+		return 0;
+
+	if ((pt_access & (ACC_READ_MASK | ACC_WRITE_MASK)) !=
+	    (ACC_READ_MASK | ACC_WRITE_MASK))
+		return 0;
+
+	if (!dirty && PT_HAVE_ACCESSED_DIRTY(w) && !(gpte & PT_GUEST_DIRTY_MASK))
+		return 0;
+
+	return ACC_EPT_SSS_MASK;
+#else
+	return 0;
+#endif
+}
+
 static inline int FNAME(is_present_gpte)(struct kvm_pagewalk *w,
 					 unsigned long pte)
 {
@@ -329,6 +380,12 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
 	u64 walk_nx_mask = 0;
 	const int write_fault = access & PFERR_WRITE_MASK;
 	const int fetch_fault = access & PFERR_FETCH_MASK;
+	/*
+	 * Supervisor shadow-stack accesses under EPTP bit 7 can write the page,
+	 * so they set the Dirty flag even when they only read the shadow stack.
+	 */
+	const bool sss_fault = FNAME(is_sss_access)(w, access);
+	const bool dirty_fault = write_fault || sss_fault;
 	/*
 	 * Note! Track the error_code that's common to legacy shadow paging
 	 * and NPT shadow paging as a u16 to guard against unintentionally
@@ -452,7 +509,13 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
 
 	/* Convert to ACC_*_MASK flags for struct guest_walker.  */
 	walker->pte_access = FNAME(gpte_access)(pte_access ^ walk_nx_mask);
-	errcode = permission_fault(vcpu, w, walker->pte_access, pte_pkey, access);
+	walker->sss_access = FNAME(gpte_sss_access)(w,
+						    walker->pt_access[walker->level - 1],
+						    pte, true);
+	if (sss_fault)
+		errcode = walker->sss_access ? 0 : PFERR_PRESENT_MASK;
+	else
+		errcode = permission_fault(vcpu, w, walker->pte_access, pte_pkey, access);
 	if (unlikely(errcode))
 		goto error;
 
@@ -472,9 +535,12 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
 
 	walker->gfn = real_gpa >> PAGE_SHIFT;
 
-	if (!write_fault)
+	if (!dirty_fault) {
 		FNAME(protect_clean_gpte)(w, &walker->pte_access, pte);
-	else
+		walker->sss_access = FNAME(gpte_sss_access)(w,
+							    walker->pt_access[walker->level - 1],
+							    pte, false);
+	} else {
 		/*
 		 * On a write fault, fold the dirty bit into accessed_dirty.
 		 * For modes without A/D bits support accessed_dirty will be
@@ -482,10 +548,11 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
 		 */
 		accessed_dirty &= pte >>
 			(PT_GUEST_DIRTY_SHIFT - PT_GUEST_ACCESSED_SHIFT);
+	}
 
 	if (unlikely(!accessed_dirty)) {
 		ret = FNAME(update_accessed_dirty_bits)(vcpu, w, walker,
-							addr, write_fault);
+							addr, dirty_fault);
 		if (unlikely(ret < 0))
 			goto error;
 		else if (ret) {
@@ -556,6 +623,14 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
 		if (is_cr4_smep(w))
 			walker->fault.exit_qualification |=
 				EPT_VIOLATION_USER_EXEC_TO_PROT(pte_access);
+		if (access & PFERR_SS_MASK)
+			walker->fault.exit_qualification |= EPT_VIOLATION_SHADOW_STACK;
+		/*
+		 * Bit 14 mirrors bit 60 of the leaf; it is undefined if the walk
+		 * stopped earlier, so report the last entry read either way.
+		 */
+		if (is_ept_sss(w) && (pte & VMX_EPT_SSS_MASK))
+			walker->fault.exit_qualification |= EPT_VIOLATION_SSS_PAGE;
 	}
 #endif
 	walker->fault.address = addr;
@@ -591,6 +666,8 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
 	gfn = gpte_to_gfn(gpte);
 	pte_access = sp->role.access & FNAME(gpte_access)(gpte);
 	FNAME(protect_clean_gpte)(vcpu->arch.mmu->w, &pte_access, gpte);
+	pte_access |= FNAME(gpte_sss_access)(vcpu->arch.mmu->w, sp->role.access,
+					     gpte, false);
 
 	return kvm_mmu_prefetch_sptes(vcpu, gfn, spte, 1, pte_access);
 }
@@ -789,7 +866,8 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
 	if (KVM_BUG_ON(it.level != fault->goal_level, vcpu->kvm))
 		return -EIO;
 
-	ret = mmu_set_spte(vcpu, fault->slot, it.sptep, gw->pte_access,
+	ret = mmu_set_spte(vcpu, fault->slot, it.sptep,
+			   gw->pte_access | gw->sss_access,
 			   base_gfn, fault->pfn, fault);
 	if (ret == RET_PF_SPURIOUS)
 		return ret;
@@ -973,6 +1051,8 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int
 	pte_access = sp->role.access;
 	pte_access &= FNAME(gpte_access)(gpte);
 	FNAME(protect_clean_gpte)(vcpu->arch.mmu->w, &pte_access, gpte);
+	pte_access |= FNAME(gpte_sss_access)(vcpu->arch.mmu->w, sp->role.access,
+					     gpte, false);
 
 	if (sync_mmio_spte(vcpu, &sp->spt[i], gfn, pte_access))
 		return 0;
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index a7c8bbdfe4ca..dc7199855497 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -519,6 +519,7 @@ static void nested_ept_new_eptp(struct kvm_vcpu *vcpu)
 	kvm_init_shadow_ept_mmu(vcpu, execonly, ept_lpage_level,
 				nested_ept_ad_enabled(vcpu),
 				nested_ept_mbec_enabled(vcpu),
+				nested_ept_get_eptp(vcpu) & VMX_EPTP_SSS_BIT,
 				nested_ept_get_eptp(vcpu));
 }
 
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 68fa251dda60..57aa8b3be509 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -3361,6 +3361,10 @@ static u64 construct_eptp(hpa_t root_hpa)
 	if (enable_ept_ad_bits && !root->role.ad_disabled)
 		eptp |= VMX_EPTP_AD_ENABLE_BIT;
 
+	/* Only nested EPT roots for an L1 EPTP with bit 7 set. */
+	if (root->role.ept_sss)
+		eptp |= VMX_EPTP_SSS_BIT;
+
 	return eptp;
 }
 

-- 
2.53.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 06/10] KVM: nVMX: Advertise supervisor shadow-stack control for nested EPT
  2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
                   ` (4 preceding siblings ...)
  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 ` 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
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

Since commit 606ddbfb4c02 ("KVM: nVMX: advertise MBEC to nested
guests"), Windows Hyper-V is offered MBEC and then enables CET for its
guests.  It requires supervisor shadow-stack control for EPT
(IA32_VMX_EPT_VPID_CAP bit 23), which KVM never advertised, and Windows
with virtualization-based security stops booting (Automatic Repair).

Advertise the capability if the CPU supports it, KVM supports shadow
stacks and advanced VM-exit information is offered, which is needed to
tell supervisor from user shadow-stack accesses.  Accept EPTP bit 7 in
nested_vmx_check_eptp() only if the capability is offered; bits 11:8
stay reserved.

The VMM must also expose bit 23 to L1; QEMU names it separately.

Fixes: 606ddbfb4c02 ("KVM: nVMX: advertise MBEC to nested guests")
Reported-by: Panayotis Katsaloulis <panayotis@panayotis.com>
Closes: https://lore.kernel.org/kvm/58dcd595e3187d335e103a042d196cb7@panayotis.com/
Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
 arch/x86/include/asm/vmx.h |  1 +
 arch/x86/kvm/vmx/nested.c  | 20 ++++++++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h
index df6feba112f7..22e890f35435 100644
--- a/arch/x86/include/asm/vmx.h
+++ b/arch/x86/include/asm/vmx.h
@@ -545,6 +545,7 @@ enum vmcs_field {
 #define VMX_EPT_INVEPT_BIT			(1ull << 20)
 #define VMX_EPT_AD_BIT				    (1ull << 21)
 #define VMX_EPT_ADVANCED_VMEXIT_INFO_BIT	(1ull << 22)
+#define VMX_EPT_SSS_BIT				(1ull << 23)
 #define VMX_EPT_EXTENT_CONTEXT_BIT		(1ull << 25)
 #define VMX_EPT_EXTENT_GLOBAL_BIT		(1ull << 26)
 
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index dc7199855497..f7bcf1891716 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -2930,7 +2930,12 @@ static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
 	}
 
 	/* Reserved bits should not be set */
-	if (CC(!kvm_vcpu_is_legal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
+	if (CC(!kvm_vcpu_is_legal_gpa(vcpu, new_eptp) || ((new_eptp >> 8) & 0xf)))
+		return false;
+
+	/* Supervisor shadow-stack control, if set, should be supported */
+	if (CC((new_eptp & VMX_EPTP_SSS_BIT) &&
+	       !(vmx->nested.msrs.ept_caps & VMX_EPT_SSS_BIT)))
 		return false;
 
 	/* AD, if set, should be supported */
@@ -7251,9 +7256,20 @@ static void nested_vmx_setup_secondary_ctls(u32 ept_caps,
 			VMX_EPTP_WB_BIT |
 			VMX_EPT_INVEPT_BIT |
 			VMX_EPT_EXECUTE_ONLY_BIT |
-			VMX_EPT_ADVANCED_VMEXIT_INFO_BIT;
+			VMX_EPT_ADVANCED_VMEXIT_INFO_BIT |
+			VMX_EPT_SSS_BIT;
 
 		msrs->ept_caps &= ept_caps;
+
+		/*
+		 * Supervisor shadow-stack control needs shadow stacks for the
+		 * guest, and advanced VM-exit information to tell supervisor
+		 * from user shadow-stack accesses in EPT violations.
+		 */
+		if (!kvm_cpu_cap_has(X86_FEATURE_SHSTK) ||
+		    !(msrs->ept_caps & VMX_EPT_ADVANCED_VMEXIT_INFO_BIT))
+			msrs->ept_caps &= ~VMX_EPT_SSS_BIT;
+
 		msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
 			VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
 			VMX_EPT_1GB_PAGE_BIT;

-- 
2.53.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 07/10] KVM: selftests: Add helpers for nested EPT shadow-stack and MBEC tests
  2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
                   ` (5 preceding siblings ...)
  2026-12-03 23:49 ` [PATCH 06/10] KVM: nVMX: Advertise supervisor shadow-stack control for " Dennis Reußwig
@ 2026-12-03 23:49 ` Dennis Reußwig
  2026-12-03 23:49 ` [PATCH 08/10] KVM: selftests: Add nested EPT supervisor shadow-stack test Dennis Reußwig
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

Add __tdp_get_pte() to look up the EPT entry of an L2 GPA, and the VMX
definitions the following tests use: the CET VMCS fields and VM-entry
and VM-exit controls, mode-based execute control, the EPT capabilities
for advanced VM-exit information and supervisor shadow-stack control,
X86_CR4_CET and the EPT violation exit qualification bits for user
execute, shadow-stack access and supervisor shadow-stack page.

Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
 tools/testing/selftests/kvm/include/x86/processor.h |  5 +++++
 tools/testing/selftests/kvm/include/x86/vmx.h       | 11 +++++++++++
 tools/testing/selftests/kvm/lib/x86/processor.c     |  9 ++++++---
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index 3e4351ccb359..79f399a40333 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -93,6 +93,7 @@ const char *ex_str(int vector);
 #define X86_CR4_SMEP		(1ul << 20)
 #define X86_CR4_SMAP		(1ul << 21)
 #define X86_CR4_PKE		(1ul << 22)
+#define X86_CR4_CET		(1ul << 23)
 
 struct xstate_header {
 	u64				xstate_bv;
@@ -1641,6 +1642,7 @@ void tdp_map(struct kvm_vm *vm, gpa_t l2_gpa, gpa_t gpa, u64 size);
 void tdp_identity_map_default_memslots(struct kvm_vm *vm);
 void tdp_identity_map_1g(struct kvm_vm *vm,  u64 addr, u64 size);
 u64 *tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa);
+u64 *__tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa, int level);
 
 /*
  * Basic CPU control in CR0
@@ -1685,8 +1687,11 @@ u64 *tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa);
 #define EPT_VIOLATION_PROT_READ		BIT(3)
 #define EPT_VIOLATION_PROT_WRITE	BIT(4)
 #define EPT_VIOLATION_PROT_EXEC		BIT(5)
+#define EPT_VIOLATION_PROT_USER_EXEC	BIT(6)
 #define EPT_VIOLATION_GVA_IS_VALID	BIT(7)
 #define EPT_VIOLATION_GVA_TRANSLATED	BIT(8)
+#define EPT_VIOLATION_SHADOW_STACK	BIT(13)
+#define EPT_VIOLATION_SSS_PAGE		BIT(14)
 
 bool sys_clocksource_is_based_on_tsc(void);
 
diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
index 8fd97434eee5..3cf3766f335c 100644
--- a/tools/testing/selftests/kvm/include/x86/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86/vmx.h
@@ -61,6 +61,7 @@
 #define SECONDARY_EXEC_ENABLE_PML		0x00020000
 #define SECONDARY_EPT_VE			0x00040000
 #define SECONDARY_ENABLE_XSAV_RESTORE		0x00100000
+#define SECONDARY_EXEC_MODE_BASED_EPT_EXEC	0x00400000
 #define SECONDARY_EXEC_TSC_SCALING		0x02000000
 
 #define PIN_BASED_EXT_INTR_MASK			0x00000001
@@ -80,6 +81,7 @@
 #define VM_EXIT_SAVE_IA32_EFER			0x00100000
 #define VM_EXIT_LOAD_IA32_EFER			0x00200000
 #define VM_EXIT_SAVE_VMX_PREEMPTION_TIMER	0x00400000
+#define VM_EXIT_LOAD_CET_STATE			0x10000000
 
 #define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR	0x00036dff
 
@@ -90,6 +92,7 @@
 #define VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL	0x00002000
 #define VM_ENTRY_LOAD_IA32_PAT			0x00004000
 #define VM_ENTRY_LOAD_IA32_EFER			0x00008000
+#define VM_ENTRY_LOAD_CET_STATE			0x00100000
 
 #define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR	0x000011ff
 
@@ -98,6 +101,8 @@
 
 #define VMX_EPT_VPID_CAP_1G_PAGES		0x00020000
 #define VMX_EPT_VPID_CAP_AD_BITS		0x00200000
+#define VMX_EPT_VPID_CAP_ADVANCED_VMEXIT_INFO	0x00400000
+#define VMX_EPT_VPID_CAP_SSS			0x00800000
 
 #define EXIT_REASON_FAILED_VMENTRY	0x80000000
 
@@ -270,6 +275,9 @@ enum vmcs_field {
 	GUEST_PENDING_DBG_EXCEPTIONS	= 0x00006822,
 	GUEST_SYSENTER_ESP		= 0x00006824,
 	GUEST_SYSENTER_EIP		= 0x00006826,
+	GUEST_S_CET = 0x00006828,
+	GUEST_SSP = 0x0000682a,
+	GUEST_INTR_SSP_TABLE = 0x0000682c,
 	HOST_CR0			= 0x00006c00,
 	HOST_CR3			= 0x00006c02,
 	HOST_CR4			= 0x00006c04,
@@ -282,6 +290,9 @@ enum vmcs_field {
 	HOST_IA32_SYSENTER_EIP		= 0x00006c12,
 	HOST_RSP			= 0x00006c14,
 	HOST_RIP			= 0x00006c16,
+	HOST_S_CET = 0x00006c18,
+	HOST_SSP = 0x00006c1a,
+	HOST_INTR_SSP_TABLE = 0x00006c1c,
 };
 
 struct vmx_msr_entry {
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index ee8a1715d844..fdf39dcea3c3 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -389,13 +389,16 @@ static u64 *__vm_get_page_table_entry(struct kvm_vm *vm,
 	return virt_get_pte(vm, mmu, pte, gva, PG_LEVEL_4K);
 }
 
-u64 *tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa)
+u64 *__tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa, int level)
 {
-	int level = PG_LEVEL_4K;
-
 	return __vm_get_page_table_entry(vm, &vm->stage2_mmu, l2_gpa, &level);
 }
 
+u64 *tdp_get_pte(struct kvm_vm *vm, u64 l2_gpa)
+{
+	return __tdp_get_pte(vm, l2_gpa, PG_LEVEL_4K);
+}
+
 u64 *vm_get_pte(struct kvm_vm *vm, gva_t gva)
 {
 	int level = PG_LEVEL_4K;

-- 
2.53.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 08/10] KVM: selftests: Add nested EPT supervisor shadow-stack test
  2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
                   ` (6 preceding siblings ...)
  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 ` 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 ` [PATCH 10/10] KVM: selftests: Check VMX capabilities for CET in L2 with MBEC Dennis Reußwig
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

L1 runs L2 with supervisor shadow stacks.  L2 does a CALL and RET whose
shadow-stack push and pop hit a page whose L1 EPT permissions vary per
case, with and without EPTP bit 7.  Check that the accesses succeed or
cause EPT violations as the SDM defines, and that the violations reach
L1 with exit qualification bits 13 and 14 set as expected.

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

diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 95867977beb3..dc890dbe48d1 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_sss_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_sss_test.c b/tools/testing/selftests/kvm/x86/vmx_nested_sss_test.c
new file mode 100644
index 000000000000..a685fba0af7f
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/vmx_nested_sss_test.c
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Nested EPT supervisor shadow-stack control (EPTP bit 7, EPT bit 60).
+ *
+ * L1 runs L2 with supervisor shadow stacks enabled; L2 does a CALL/RET whose
+ * shadow-stack push and pop hit a page whose L1 EPT permissions vary per test.
+ * With EPTP bit 7 set, supervisor shadow-stack accesses are allowed iff every
+ * EPT entry is readable, every non-leaf entry is writable and the leaf sets
+ * bit 60; the write bit of the leaf does not matter.  Without EPTP bit 7 they
+ * follow the normal write rules.  Denied accesses must reach L1 as EPT
+ * violations with exit qualification bit 13 (shadow-stack access) and, with
+ * EPTP bit 7, bit 14 (bit 60 of the leaf).
+ */
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "vmx.h"
+
+#define SS_GVA 0x30000000ULL
+/* Alone in its 2M region, so the non-leaf EPT entries above it are its own. */
+#define SS_L2_GPA 0x80000000ULL
+
+#define EPT_SSS_PAGE BIT_ULL(60)
+#define VMX_EPTP_SSS BIT_ULL(7)
+
+/* Paging attributes of L2's linear address, not under test. */
+#define QUAL_IGNORE (BIT_ULL(9) | BIT_ULL(10) | BIT_ULL(11))
+
+#define QUAL_SS_WRITE (EPT_VIOLATION_ACC_WRITE | EPT_VIOLATION_GVA_IS_VALID | \
+		       EPT_VIOLATION_GVA_TRANSLATED | EPT_VIOLATION_SHADOW_STACK)
+
+struct sss_test {
+	const char *name;
+	bool eptp_sss;
+	bool leaf_writable;
+	bool leaf_sss;
+	bool pde_writable;
+	/* 0 if L2 must reach its VMCALL. */
+	u64 qual;
+	u64 qual_ignore;
+};
+
+static const struct sss_test tests[] = {
+	{
+		.name = "SSS page, leaf read-only",
+		.eptp_sss = true, .leaf_writable = false, .leaf_sss = true,
+		.pde_writable = true,
+	},
+	{
+		.name = "not an SSS page, leaf read-only",
+		.eptp_sss = true, .leaf_writable = false, .leaf_sss = false,
+		.pde_writable = true,
+		.qual = QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ | EPT_VIOLATION_PROT_EXEC,
+	},
+	{
+		.name = "not an SSS page, leaf writable",
+		.eptp_sss = true, .leaf_writable = true, .leaf_sss = false,
+		.pde_writable = true,
+		.qual = QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ |
+			EPT_VIOLATION_PROT_WRITE | EPT_VIOLATION_PROT_EXEC,
+	},
+	{
+		.name = "SSS page, non-leaf read-only",
+		.eptp_sss = true, .leaf_writable = false, .leaf_sss = true,
+		.pde_writable = false,
+		.qual = QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ | EPT_VIOLATION_PROT_EXEC |
+			EPT_VIOLATION_SSS_PAGE,
+	},
+	{
+		.name = "no EPTP bit 7, leaf read-only",
+		.eptp_sss = false, .leaf_writable = false, .leaf_sss = true,
+		.pde_writable = true,
+		.qual = QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ | EPT_VIOLATION_PROT_EXEC,
+		/* Bit 14 is undefined without EPTP bit 7. */
+		.qual_ignore = EPT_VIOLATION_SSS_PAGE,
+	},
+	{
+		.name = "no EPTP bit 7, leaf writable",
+		.eptp_sss = false, .leaf_writable = true, .leaf_sss = true,
+		.pde_writable = true,
+	},
+};
+
+static void l2_guest_code(void)
+{
+	/* The CALL pushes to and the RET pops from the shadow stack. */
+	asm volatile("call 1f\n\t"
+		     "jmp 2f\n\t"
+		     "1: ret\n\t"
+		     "2:" ::: "memory");
+	vmcall();
+}
+
+static void l1_guest_code(struct vmx_pages *vmx, u64 eptp_sss, u64 qual,
+			  u64 qual_ignore)
+{
+	u64 actual;
+
+	prepare_for_vmx_operation(vmx);
+	load_vmcs(vmx);
+	prepare_vmcs(vmx, l2_guest_code);
+
+	if (eptp_sss)
+		vmwrite(EPT_POINTER, vmread(EPT_POINTER) | VMX_EPTP_SSS);
+
+	/* CR4.CET requires CR0.WP. */
+	vmwrite(GUEST_CR0, vmread(GUEST_CR0) | X86_CR0_WP);
+	vmwrite(GUEST_CR4, vmread(GUEST_CR4) | X86_CR4_CET);
+	vmwrite(GUEST_S_CET, CET_SHSTK_EN);
+	vmwrite(GUEST_SSP, SS_GVA + PAGE_SIZE);
+	vmwrite(GUEST_INTR_SSP_TABLE, 0);
+	vmwrite(VM_ENTRY_CONTROLS, vmread(VM_ENTRY_CONTROLS) | VM_ENTRY_LOAD_CET_STATE);
+	vmwrite(HOST_S_CET, 0);
+	vmwrite(HOST_SSP, 0);
+	vmwrite(HOST_INTR_SSP_TABLE, 0);
+	vmwrite(VM_EXIT_CONTROLS, vmread(VM_EXIT_CONTROLS) | VM_EXIT_LOAD_CET_STATE);
+
+	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));
+
+	/* The CALL pushes the return address just below the top of the page. */
+	__GUEST_ASSERT(vmread(GUEST_PHYSICAL_ADDRESS) == SS_L2_GPA + PAGE_SIZE - 8,
+		       "Expected GPA 0x%llx, got 0x%lx", SS_L2_GPA + PAGE_SIZE - 8,
+		       vmread(GUEST_PHYSICAL_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 sss_test *t)
+{
+	struct kvm_mmu *ept;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	gva_t vmx_gva;
+	struct ucall uc;
+	gpa_t ss_gpa;
+	u64 *pte;
+
+	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;
+
+	/*
+	 * Map the shadow-stack page into L2's (and L1's) page tables as a
+	 * supervisor shadow-stack page: read-only and dirty.
+	 */
+	ss_gpa = vm_phy_page_alloc(vm, MEM_REGION_DATA);
+	virt_pg_map(vm, SS_GVA, SS_L2_GPA);
+	pte = vm_get_pte(vm, SS_GVA);
+	*pte &= ~(PTE_WRITABLE_MASK(&vm->mmu) | PTE_USER_MASK(&vm->mmu));
+	*pte |= PTE_DIRTY_MASK(&vm->mmu);
+
+	tdp_identity_map_default_memslots(vm);
+	tdp_map(vm, SS_L2_GPA, ss_gpa, PAGE_SIZE);
+
+	pte = tdp_get_pte(vm, SS_L2_GPA);
+	if (!t->leaf_writable)
+		*pte &= ~PTE_WRITABLE_MASK(ept);
+	if (t->leaf_sss)
+		*pte |= EPT_SSS_PAGE;
+
+	if (!t->pde_writable) {
+		pte = __tdp_get_pte(vm, SS_L2_GPA, PG_LEVEL_2M);
+		*pte &= ~PTE_WRITABLE_MASK(ept);
+	}
+
+	vcpu_args_set(vcpu, 4, vmx_gva, (u64)t->eptp_sss, 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[])
+{
+	int i;
+
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SHSTK));
+	TEST_REQUIRE(kvm_cpu_has_ept());
+	TEST_REQUIRE(kvm_get_feature_msr(MSR_IA32_VMX_EPT_VPID_CAP) &
+		     VMX_EPT_VPID_CAP_SSS);
+
+	for (i = 0; i < ARRAY_SIZE(tests); i++)
+		run_test(&tests[i]);
+
+	return 0;
+}

-- 
2.53.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 09/10] KVM: selftests: Add nested EPT mode-based execute control test
  2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
                   ` (7 preceding siblings ...)
  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
  2026-12-03 23:49 ` [PATCH 10/10] KVM: selftests: Check VMX capabilities for CET in L2 with MBEC Dennis Reußwig
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 10/10] KVM: selftests: Check VMX capabilities for CET in L2 with MBEC
  2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
                   ` (8 preceding siblings ...)
  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
  9 siblings, 0 replies; 11+ messages in thread
From: Dennis Reußwig @ 2026-12-03 23:49 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Jon Kohler,
	Jonathan Corbet, Shuah Khan, Randy Dunlap, Shuah Khan
  Cc: kvm, linux-kernel, linux-doc, linux-kselftest, Chao Gao,
	Maxim Levitsky, Vitaly Kuznetsov, Panayotis Katsaloulis,
	Dennis Reußwig

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


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-12-03 23:50 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 10/10] KVM: selftests: Check VMX capabilities for CET in L2 with MBEC Dennis Reußwig

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®