KVM: selftests: Use a loop to walk guest page tables

Walk the guest page tables via a loop when searching for a PTE,
instead of using unique variables for each level of the page tables.

This simplifies the code and makes it easier to support 5-level paging
in the future.

Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Yosry Ahmed <yosry.ahmed@linux.dev>
Link: https://patch.msgid.link/20251028225827.2269128-3-jmattson@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Jim Mattson
2025-10-28 15:30:40 -07:00
committed by Sean Christopherson
parent ae5b498b8d
commit 2103a8baf5

View File

@@ -307,7 +307,8 @@ static bool vm_is_target_pte(uint64_t *pte, int *level, int current_level)
uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
int *level)
{
uint64_t *pml4e, *pdpe, *pde;
uint64_t *pte = &vm->pgd;
int current_level;
TEST_ASSERT(!vm->arch.is_pt_protected,
"Walking page tables of protected guests is impossible");
@@ -328,19 +329,15 @@ uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
TEST_ASSERT(vaddr == (((int64_t)vaddr << 16) >> 16),
"Canonical check failed. The virtual address is invalid.");
pml4e = virt_get_pte(vm, &vm->pgd, vaddr, PG_LEVEL_512G);
if (vm_is_target_pte(pml4e, level, PG_LEVEL_512G))
return pml4e;
for (current_level = vm->pgtable_levels;
current_level > PG_LEVEL_4K;
current_level--) {
pte = virt_get_pte(vm, pte, vaddr, current_level);
if (vm_is_target_pte(pte, level, current_level))
return pte;
}
pdpe = virt_get_pte(vm, pml4e, vaddr, PG_LEVEL_1G);
if (vm_is_target_pte(pdpe, level, PG_LEVEL_1G))
return pdpe;
pde = virt_get_pte(vm, pdpe, vaddr, PG_LEVEL_2M);
if (vm_is_target_pte(pde, level, PG_LEVEL_2M))
return pde;
return virt_get_pte(vm, pde, vaddr, PG_LEVEL_4K);
return virt_get_pte(vm, pte, vaddr, PG_LEVEL_4K);
}
uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr)