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. Dirty logging clears the writable bit without flushing TLBs before it drops mmu_lock; write-protection for shadow paging then still flushes, because the SPTE keeps MMU-writable. Bit 60 needs the same: add the software bit MMU-SSS (bit 59, ignored by the CPU in EPT leaves), set it together with bit 60, clear it only when write-protecting for shadow paging, and flush when it goes away. As for the writable bit, only losing MMU-SSS requires a flush; when dirty logging clears bit 60 alone, the caller flushes the memslot. MMU-writable itself cannot be used for this, because fast_page_fault() makes SPTEs with MMU-writable writable, while bit 60 does not allow ordinary writes. This uses one of the last free SPTE bits: after the A/D tracking type moved to bits 56-57, only bits 58 and 61 stay unused. Like MMU-writable (bit 55), bit 59 overlaps the MMIO generation bits; that is fine because MMIO SPTEs are never shadow-present. 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 --- 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 | 22 +++++++++++------- arch/x86/kvm/mmu/spte.c | 34 +++++++++++++++++++-------- arch/x86/kvm/mmu/spte.h | 58 +++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 100 insertions(+), 21 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 dda237a1ebd9..a07c7a585879 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..e89d930e8444 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, @@ -1377,13 +1377,13 @@ static bool spte_write_protect(u64 *sptep, bool pt_protect) { u64 spte = *sptep; - if (!is_writable_pte(spte) && - !(pt_protect && is_mmu_writable_spte(spte))) + if (!is_writable_pte(spte) && !is_sss_spte(spte) && + !(pt_protect && (is_mmu_writable_spte(spte) || is_mmu_sss_spte(spte)))) return false; if (pt_protect) - spte &= ~shadow_mmu_writable_mask; - spte = spte & ~PT_WRITABLE_MASK; + spte &= ~(shadow_mmu_writable_mask | EPT_SPTE_MMU_SSS); + spte &= ~(PT_WRITABLE_MASK | VMX_EPT_SSS_MASK); return mmu_spte_update(sptep, spte); } @@ -1424,11 +1424,16 @@ 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 + /* Bit 60 is set only under mmu_lock, a plain read is enough. */ + if (is_sss_spte(*sptep)) + flush |= test_and_clear_bit(SPTE_EPT_SSS_SHIFT, + (unsigned long *)sptep); + } else { flush |= spte_clear_dirty(sptep); + } } return flush; @@ -3245,7 +3250,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..5cb6953a040f 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 | EPT_SPTE_MMU_SSS; + + 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..7a6f2379331c 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -23,8 +23,10 @@ * - bits 53 (EPT only): host writable * - bits 55 (EPT only): MMU-writable * - 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 58, 61: unused + * - bit 59 (EPT only): MMU-SSS, see is_mmu_sss_spte() + * - bit 60: 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 */ @@ -97,6 +99,16 @@ 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))); + +/* Bit 59 is the software MMU-SSS bit, see is_mmu_sss_spte(). */ +#define EPT_SPTE_MMU_SSS BIT_ULL(59) +static_assert(!(EPT_SPTE_MMU_SSS & (SPTE_TDP_AD_MASK | SHADOW_ACC_TRACK_SAVED_MASK | + EPT_SPTE_HOST_WRITABLE | EPT_SPTE_MMU_WRITABLE | + VMX_EPT_SSS_MASK))); + /* Defined only to keep the above static asserts readable. */ #undef SHADOW_ACC_TRACK_SAVED_MASK @@ -480,6 +492,17 @@ static inline void check_spte_writable_invariants(u64 spte) else WARN_ONCE(is_writable_pte(spte), KBUILD_MODNAME ": Writable SPTE is not MMU-writable: %llx", spte); + + /* Bits 59 and 60 hold the MMIO generation in non-present SPTEs. */ + if (!is_shadow_present_pte(spte)) + return; + + if (spte & EPT_SPTE_MMU_SSS) + WARN_ONCE(!(spte & shadow_host_writable_mask), + KBUILD_MODNAME ": MMU-SSS SPTE is not Host-writable: %llx", spte); + else + WARN_ONCE(spte & VMX_EPT_SSS_MASK, + KBUILD_MODNAME ": SSS SPTE is not MMU-SSS: %llx", spte); } static inline bool is_mmu_writable_spte(u64 spte) @@ -487,6 +510,33 @@ 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; +} + +/* + * MMU-SSS is to bit 60 what MMU-writable is to the writable bit: it is set + * together with bit 60 and cleared only when KVM write-protects the SPTE for + * shadow paging, with a TLB flush. Dirty logging clears bit 60 but keeps + * MMU-SSS and defers its flush, so write-protecting for shadow paging must + * flush if MMU-SSS is set, even if bit 60 is already clear. Unlike + * MMU-writable, it never lets fast_page_fault() restore any permission. + */ +static inline bool is_mmu_sss_spte(u64 spte) +{ + return spte & EPT_SPTE_MMU_SSS; +} + /* * Returns true if the access indicated by @fault is forbidden by the existing * SPTE protections. @@ -531,6 +581,7 @@ static inline bool spte_permission_fault(struct kvm_mmu *mmu, u64 spte, * flushes TLBs based on whether or not dirty bitmap/ring entries were reaped, * not whether or not SPTEs were modified, i.e. only the write-tracking case * needs to flush at the time the SPTEs is modified, before dropping mmu_lock. + * MMU-SSS and the EPT supervisor shadow-stack bit work the same way. * * Don't flush if the Accessed bit is cleared, as access tracking tolerates * false negatives, e.g. KVM x86 omits TLB flushes even when aging SPTEs for a @@ -548,7 +599,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_mmu_sss_spte(old_spte) && !is_mmu_sss_spte(new_spte)); } static inline u64 get_mmio_spte_generation(u64 spte) -- 2.53.0