Skip to content

Commit d8fa203

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
The TDX support will need the "suppress #VE" bit (bit 63) set as the initial value for SPTE. To reduce code change size, introduce a new macro SHADOW_NONPRESENT_VALUE for the initial value for the shadow page table entry (SPTE) and replace hard-coded value 0 for it. Initialize shadow page tables with their value. The plan is to unconditionally set the "suppress #VE" bit for both AMD and Intel as: 1) AMD hardware uses the bit 63 as NX for present SPTE and ignored for non-present SPTE; 2) for conventional VMX guests, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware. No functional change intended. Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Isaku Yamahata <[email protected]> Message-Id: <acdf09bf60cad12c495005bf3495c54f6b3069c9.1705965635.git.isaku.yamahata@intel.com> [Remove unnecessary CONFIG_X86_64 check. - Paolo] Reviewed-by: Xiaoyao Li <[email protected]> Reviewed-by: Binbin Wu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent c23e2b7 commit d8fa203

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,9 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)
567567

568568
if (!is_shadow_present_pte(old_spte) ||
569569
!spte_has_volatile_bits(old_spte))
570-
__update_clear_spte_fast(sptep, 0ull);
570+
__update_clear_spte_fast(sptep, SHADOW_NONPRESENT_VALUE);
571571
else
572-
old_spte = __update_clear_spte_slow(sptep, 0ull);
572+
old_spte = __update_clear_spte_slow(sptep, SHADOW_NONPRESENT_VALUE);
573573

574574
if (!is_shadow_present_pte(old_spte))
575575
return old_spte;
@@ -603,7 +603,7 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)
603603
*/
604604
static void mmu_spte_clear_no_track(u64 *sptep)
605605
{
606-
__update_clear_spte_fast(sptep, 0ull);
606+
__update_clear_spte_fast(sptep, SHADOW_NONPRESENT_VALUE);
607607
}
608608

609609
static u64 mmu_spte_get_lockless(u64 *sptep)
@@ -1897,7 +1897,8 @@ static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
18971897

18981898
static int kvm_sync_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int i)
18991899
{
1900-
if (!sp->spt[i])
1900+
/* sp->spt[i] has initial value of shadow page table allocation */
1901+
if (sp->spt[i] == SHADOW_NONPRESENT_VALUE)
19011902
return 0;
19021903

19031904
return vcpu->arch.mmu->sync_spte(vcpu, sp, i);
@@ -6120,7 +6121,10 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu)
61206121
vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache;
61216122
vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO;
61226123

6123-
vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO;
6124+
vcpu->arch.mmu_shadow_page_cache.init_value =
6125+
SHADOW_NONPRESENT_VALUE;
6126+
if (!vcpu->arch.mmu_shadow_page_cache.init_value)
6127+
vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO;
61246128

61256129
vcpu->arch.mmu = &vcpu->arch.root_mmu;
61266130
vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;

arch/x86/kvm/mmu/paging_tmpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int
911911
gpa_t pte_gpa;
912912
gfn_t gfn;
913913

914-
if (WARN_ON_ONCE(!sp->spt[i]))
914+
if (WARN_ON_ONCE(sp->spt[i] == SHADOW_NONPRESENT_VALUE))
915915
return 0;
916916

917917
first_pte_gpa = FNAME(get_level1_sp_gpa)(sp);

arch/x86/kvm/mmu/spte.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11);
149149

150150
#define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0)
151151

152+
#define SHADOW_NONPRESENT_VALUE 0ULL
153+
152154
extern u64 __read_mostly shadow_host_writable_mask;
153155
extern u64 __read_mostly shadow_mmu_writable_mask;
154156
extern u64 __read_mostly shadow_nx_mask;
@@ -194,7 +196,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;
194196
*
195197
* Only used by the TDP MMU.
196198
*/
197-
#define REMOVED_SPTE 0x5a0ULL
199+
#define REMOVED_SPTE (SHADOW_NONPRESENT_VALUE | 0x5a0ULL)
198200

199201
/* Removed SPTEs must not be misconstrued as shadow present PTEs. */
200202
static_assert(!(REMOVED_SPTE & SPTE_MMU_PRESENT_MASK));

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,
603603
* here since the SPTE is going from non-present to non-present. Use
604604
* the raw write helper to avoid an unnecessary check on volatile bits.
605605
*/
606-
__kvm_tdp_mmu_write_spte(iter->sptep, 0);
606+
__kvm_tdp_mmu_write_spte(iter->sptep, SHADOW_NONPRESENT_VALUE);
607607

608608
return 0;
609609
}
@@ -740,8 +740,8 @@ static void __tdp_mmu_zap_root(struct kvm *kvm, struct kvm_mmu_page *root,
740740
continue;
741741

742742
if (!shared)
743-
tdp_mmu_iter_set_spte(kvm, &iter, 0);
744-
else if (tdp_mmu_set_spte_atomic(kvm, &iter, 0))
743+
tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE);
744+
else if (tdp_mmu_set_spte_atomic(kvm, &iter, SHADOW_NONPRESENT_VALUE))
745745
goto retry;
746746
}
747747
}
@@ -808,8 +808,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
808808
if (WARN_ON_ONCE(!is_shadow_present_pte(old_spte)))
809809
return false;
810810

811-
tdp_mmu_set_spte(kvm, kvm_mmu_page_as_id(sp), sp->ptep, old_spte, 0,
812-
sp->gfn, sp->role.level + 1);
811+
tdp_mmu_set_spte(kvm, kvm_mmu_page_as_id(sp), sp->ptep, old_spte,
812+
SHADOW_NONPRESENT_VALUE, sp->gfn, sp->role.level + 1);
813813

814814
return true;
815815
}
@@ -843,7 +843,7 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,
843843
!is_last_spte(iter.old_spte, iter.level))
844844
continue;
845845

846-
tdp_mmu_iter_set_spte(kvm, &iter, 0);
846+
tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE);
847847

848848
/*
849849
* Zappings SPTEs in invalid roots doesn't require a TLB flush,

0 commit comments

Comments
 (0)