Commit 5e3edd7e authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: x86/mmu: Add helper to convert SPTE value to its shadow page

Add a helper to convert a SPTE to its shadow page to deduplicate a
variety of flows and hopefully avoid future bugs, e.g. if KVM attempts to
get the shadow page for a SPTE without dropping high bits.

Opportunistically add a comment in mmu_free_root_page() documenting why
it treats the root HPA as a SPTE.

No functional change intended.
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Message-Id: <20221019165618.927057-7-seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent d25ceb92
...@@ -1819,7 +1819,7 @@ static int __mmu_unsync_walk(struct kvm_mmu_page *sp, ...@@ -1819,7 +1819,7 @@ static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
continue; continue;
} }
child = to_shadow_page(ent & SPTE_BASE_ADDR_MASK); child = spte_to_child_sp(ent);
if (child->unsync_children) { if (child->unsync_children) {
if (mmu_pages_add(pvec, child, i)) if (mmu_pages_add(pvec, child, i))
...@@ -2378,7 +2378,7 @@ static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep, ...@@ -2378,7 +2378,7 @@ static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
* so we should update the spte at this point to get * so we should update the spte at this point to get
* a new sp with the correct access. * a new sp with the correct access.
*/ */
child = to_shadow_page(*sptep & SPTE_BASE_ADDR_MASK); child = spte_to_child_sp(*sptep);
if (child->role.access == direct_access) if (child->role.access == direct_access)
return; return;
...@@ -2399,7 +2399,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp, ...@@ -2399,7 +2399,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
if (is_last_spte(pte, sp->role.level)) { if (is_last_spte(pte, sp->role.level)) {
drop_spte(kvm, spte); drop_spte(kvm, spte);
} else { } else {
child = to_shadow_page(pte & SPTE_BASE_ADDR_MASK); child = spte_to_child_sp(pte);
drop_parent_pte(child, spte); drop_parent_pte(child, spte);
/* /*
...@@ -2838,7 +2838,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot, ...@@ -2838,7 +2838,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot,
struct kvm_mmu_page *child; struct kvm_mmu_page *child;
u64 pte = *sptep; u64 pte = *sptep;
child = to_shadow_page(pte & SPTE_BASE_ADDR_MASK); child = spte_to_child_sp(pte);
drop_parent_pte(child, sptep); drop_parent_pte(child, sptep);
flush = true; flush = true;
} else if (pfn != spte_to_pfn(*sptep)) { } else if (pfn != spte_to_pfn(*sptep)) {
...@@ -3455,7 +3455,11 @@ static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa, ...@@ -3455,7 +3455,11 @@ static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa,
if (!VALID_PAGE(*root_hpa)) if (!VALID_PAGE(*root_hpa))
return; return;
sp = to_shadow_page(*root_hpa & SPTE_BASE_ADDR_MASK); /*
* The "root" may be a special root, e.g. a PAE entry, treat it as a
* SPTE to ensure any non-PA bits are dropped.
*/
sp = spte_to_child_sp(*root_hpa);
if (WARN_ON(!sp)) if (WARN_ON(!sp))
return; return;
...@@ -3940,8 +3944,7 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) ...@@ -3940,8 +3944,7 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
hpa_t root = vcpu->arch.mmu->pae_root[i]; hpa_t root = vcpu->arch.mmu->pae_root[i];
if (IS_VALID_PAE_ROOT(root)) { if (IS_VALID_PAE_ROOT(root)) {
root &= SPTE_BASE_ADDR_MASK; sp = spte_to_child_sp(root);
sp = to_shadow_page(root);
mmu_sync_children(vcpu, sp, true); mmu_sync_children(vcpu, sp, true);
} }
} }
......
...@@ -133,18 +133,6 @@ struct kvm_mmu_page { ...@@ -133,18 +133,6 @@ struct kvm_mmu_page {
extern struct kmem_cache *mmu_page_header_cache; extern struct kmem_cache *mmu_page_header_cache;
static inline struct kvm_mmu_page *to_shadow_page(hpa_t shadow_page)
{
struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
return (struct kvm_mmu_page *)page_private(page);
}
static inline struct kvm_mmu_page *sptep_to_sp(u64 *sptep)
{
return to_shadow_page(__pa(sptep));
}
static inline int kvm_mmu_role_as_id(union kvm_mmu_page_role role) static inline int kvm_mmu_role_as_id(union kvm_mmu_page_role role)
{ {
return role.smm ? 1 : 0; return role.smm ? 1 : 0;
......
...@@ -219,6 +219,23 @@ static inline int spte_index(u64 *sptep) ...@@ -219,6 +219,23 @@ static inline int spte_index(u64 *sptep)
*/ */
extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask; extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;
static inline struct kvm_mmu_page *to_shadow_page(hpa_t shadow_page)
{
struct page *page = pfn_to_page((shadow_page) >> PAGE_SHIFT);
return (struct kvm_mmu_page *)page_private(page);
}
static inline struct kvm_mmu_page *spte_to_child_sp(u64 spte)
{
return to_shadow_page(spte & SPTE_BASE_ADDR_MASK);
}
static inline struct kvm_mmu_page *sptep_to_sp(u64 *sptep)
{
return to_shadow_page(__pa(sptep));
}
static inline bool is_mmio_spte(u64 spte) static inline bool is_mmio_spte(u64 spte)
{ {
return (spte & shadow_mmio_mask) == shadow_mmio_value && return (spte & shadow_mmio_mask) == shadow_mmio_value &&
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <linux/kvm_host.h> #include <linux/kvm_host.h>
#include "spte.h"
hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu); hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu);
__must_check static inline bool kvm_tdp_mmu_get_root(struct kvm_mmu_page *root) __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm_mmu_page *root)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment