Commit 5fb07ddb authored by Avi Kivity's avatar Avi Kivity

KVM: MMU: Introduce and use gpte_to_gfn()

Instead of repretitively open-coding this.
Signed-off-by: default avatarAvi Kivity <avi@qumranet.com>
parent b238f7bc
...@@ -52,6 +52,9 @@ ...@@ -52,6 +52,9 @@
#error Invalid PTTYPE value #error Invalid PTTYPE value
#endif #endif
#define gpte_to_gfn FNAME(gpte_to_gfn)
#define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde)
/* /*
* The guest_walker structure emulates the behavior of the hardware page * The guest_walker structure emulates the behavior of the hardware page
* table walker. * table walker.
...@@ -65,6 +68,16 @@ struct guest_walker { ...@@ -65,6 +68,16 @@ struct guest_walker {
u32 error_code; u32 error_code;
}; };
static gfn_t gpte_to_gfn(pt_element_t gpte)
{
return (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
}
static gfn_t gpte_to_gfn_pde(pt_element_t gpte)
{
return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
}
/* /*
* Fetch a guest pte for a guest virtual address * Fetch a guest pte for a guest virtual address
*/ */
...@@ -96,7 +109,7 @@ static int FNAME(walk_addr)(struct guest_walker *walker, ...@@ -96,7 +109,7 @@ static int FNAME(walk_addr)(struct guest_walker *walker,
for (;;) { for (;;) {
index = PT_INDEX(addr, walker->level); index = PT_INDEX(addr, walker->level);
table_gfn = (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; table_gfn = gpte_to_gfn(pte);
pte_gpa = table_gfn << PAGE_SHIFT; pte_gpa = table_gfn << PAGE_SHIFT;
pte_gpa += index * sizeof(pt_element_t); pte_gpa += index * sizeof(pt_element_t);
walker->table_gfn[walker->level - 1] = table_gfn; walker->table_gfn[walker->level - 1] = table_gfn;
...@@ -127,15 +140,14 @@ static int FNAME(walk_addr)(struct guest_walker *walker, ...@@ -127,15 +140,14 @@ static int FNAME(walk_addr)(struct guest_walker *walker,
} }
if (walker->level == PT_PAGE_TABLE_LEVEL) { if (walker->level == PT_PAGE_TABLE_LEVEL) {
walker->gfn = (pte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT; walker->gfn = gpte_to_gfn(pte);
break; break;
} }
if (walker->level == PT_DIRECTORY_LEVEL if (walker->level == PT_DIRECTORY_LEVEL
&& (pte & PT_PAGE_SIZE_MASK) && (pte & PT_PAGE_SIZE_MASK)
&& (PTTYPE == 64 || is_pse(vcpu))) { && (PTTYPE == 64 || is_pse(vcpu))) {
walker->gfn = (pte & PT_DIR_BASE_ADDR_MASK) walker->gfn = gpte_to_gfn_pde(pte);
>> PAGE_SHIFT;
walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL); walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
break; break;
} }
...@@ -296,8 +308,7 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page, ...@@ -296,8 +308,7 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
return; return;
pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte); pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
FNAME(set_pte)(vcpu, gpte, spte, PT_USER_MASK | PT_WRITABLE_MASK, 0, FNAME(set_pte)(vcpu, gpte, spte, PT_USER_MASK | PT_WRITABLE_MASK, 0,
0, NULL, NULL, 0, NULL, NULL, gpte_to_gfn(gpte));
(gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT);
} }
static void FNAME(set_pde)(struct kvm_vcpu *vcpu, pt_element_t gpde, static void FNAME(set_pde)(struct kvm_vcpu *vcpu, pt_element_t gpde,
...@@ -370,8 +381,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, ...@@ -370,8 +381,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
hugepage_access >>= PT_WRITABLE_SHIFT; hugepage_access >>= PT_WRITABLE_SHIFT;
if (walker->pte & PT64_NX_MASK) if (walker->pte & PT64_NX_MASK)
hugepage_access |= (1 << 2); hugepage_access |= (1 << 2);
table_gfn = (walker->pte & PT_BASE_ADDR_MASK) table_gfn = gpte_to_gfn(walker->pte);
>> PAGE_SHIFT;
} else { } else {
metaphysical = 0; metaphysical = 0;
table_gfn = walker->table_gfn[level - 2]; table_gfn = walker->table_gfn[level - 2];
...@@ -519,3 +529,5 @@ static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu, ...@@ -519,3 +529,5 @@ static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
#undef PT_DIR_BASE_ADDR_MASK #undef PT_DIR_BASE_ADDR_MASK
#undef PT_LEVEL_BITS #undef PT_LEVEL_BITS
#undef PT_MAX_FULL_LEVELS #undef PT_MAX_FULL_LEVELS
#undef gpte_to_gfn
#undef gpte_to_gfn_pde
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