Skip to content

Commit a130066

Browse files
Binbin Wusean-jc
authored andcommitted
KVM: x86/mmu: Drop non-PA bits when getting GFN for guest's PGD
Drop non-PA bits when getting GFN for guest's PGD with the maximum theoretical mask for guest MAXPHYADDR. Do it unconditionally because it's harmless for 32-bit guests, querying 64-bit mode would be more expensive, and for EPT the mask isn't tied to guest mode. Using PT_BASE_ADDR_MASK would be technically wrong (PAE paging has 64-bit elements _except_ for CR3, which has only 32 valid bits), it wouldn't matter in practice though. Opportunistically use GENMASK_ULL() to define __PT_BASE_ADDR_MASK. Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com> Tested-by: Xuelian Guo <xuelian.guo@intel.com> Link: https://lore.kernel.org/r/20230913124227.12574-6-binbin.wu@linux.intel.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 538ac9a commit a130066

3 files changed

Lines changed: 3 additions & 2 deletions

File tree

arch/x86/kvm/mmu/mmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3806,7 +3806,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
38063806
hpa_t root;
38073807

38083808
root_pgd = kvm_mmu_get_guest_pgd(vcpu, mmu);
3809-
root_gfn = root_pgd >> PAGE_SHIFT;
3809+
root_gfn = (root_pgd & __PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
38103810

38113811
if (!kvm_vcpu_is_visible_gfn(vcpu, root_gfn)) {
38123812
mmu->root.hpa = kvm_mmu_get_dummy_root();

arch/x86/kvm/mmu/mmu_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#endif
1414

1515
/* Page table builder macros common to shadow (host) PTEs and guest PTEs. */
16+
#define __PT_BASE_ADDR_MASK GENMASK_ULL(51, 12)
1617
#define __PT_LEVEL_SHIFT(level, bits_per_level) \
1718
(PAGE_SHIFT + ((level) - 1) * (bits_per_level))
1819
#define __PT_INDEX(address, level, bits_per_level) \

arch/x86/kvm/mmu/paging_tmpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#endif
6363

6464
/* Common logic, but per-type values. These also need to be undefined. */
65-
#define PT_BASE_ADDR_MASK ((pt_element_t)(((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)))
65+
#define PT_BASE_ADDR_MASK ((pt_element_t)__PT_BASE_ADDR_MASK)
6666
#define PT_LVL_ADDR_MASK(lvl) __PT_LVL_ADDR_MASK(PT_BASE_ADDR_MASK, lvl, PT_LEVEL_BITS)
6767
#define PT_LVL_OFFSET_MASK(lvl) __PT_LVL_OFFSET_MASK(PT_BASE_ADDR_MASK, lvl, PT_LEVEL_BITS)
6868
#define PT_INDEX(addr, lvl) __PT_INDEX(addr, lvl, PT_LEVEL_BITS)

0 commit comments

Comments
 (0)