[PATCH 04/10] KVM: x86/mmu: Handle the EPT supervisor shadow-stack bit like a write bit
From: Dennis Reußwig
Date: Thu Dec 03 2026 - 18:50:12 EST
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