Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Add wrapper to allocate page table page

Add a helper to allocate a page for use in constructing the guest's page
tables. All architectures have identical address and memslot
requirements (which appear to be arbitrary anyways).

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20210622200529.3650424-15-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Sean Christopherson and committed by
Paolo Bonzini
cce0c23d 444d084b

+23 -39
+2
tools/testing/selftests/kvm/include/kvm_util.h
··· 30 30 31 31 /* Minimum allocated guest virtual and physical addresses */ 32 32 #define KVM_UTIL_MIN_VADDR 0x2000 33 + #define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 33 34 34 35 #define DEFAULT_GUEST_PHY_PAGES 512 35 36 #define DEFAULT_GUEST_STACK_VADDR_MIN 0xab6000 ··· 263 262 uint32_t memslot); 264 263 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, 265 264 vm_paddr_t paddr_min, uint32_t memslot); 265 + vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm); 266 266 267 267 /* 268 268 * Create a VM with reasonable defaults
+6 -13
tools/testing/selftests/kvm/lib/aarch64/processor.c
··· 11 11 #include "../kvm_util_internal.h" 12 12 #include "processor.h" 13 13 14 - #define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 15 14 #define DEFAULT_ARM64_GUEST_STACK_VADDR_MIN 0xac0000 16 15 17 16 static uint64_t page_align(struct kvm_vm *vm, uint64_t v) ··· 103 104 paddr, vm->max_gfn, vm->page_size); 104 105 105 106 ptep = addr_gpa2hva(vm, vm->pgd) + pgd_index(vm, vaddr) * 8; 106 - if (!*ptep) { 107 - *ptep = vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0); 108 - *ptep |= 3; 109 - } 107 + if (!*ptep) 108 + *ptep = vm_alloc_page_table(vm) | 3; 110 109 111 110 switch (vm->pgtable_levels) { 112 111 case 4: 113 112 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pud_index(vm, vaddr) * 8; 114 - if (!*ptep) { 115 - *ptep = vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0); 116 - *ptep |= 3; 117 - } 113 + if (!*ptep) 114 + *ptep = vm_alloc_page_table(vm) | 3; 118 115 /* fall through */ 119 116 case 3: 120 117 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pmd_index(vm, vaddr) * 8; 121 - if (!*ptep) { 122 - *ptep = vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0); 123 - *ptep |= 3; 124 - } 118 + if (!*ptep) 119 + *ptep = vm_alloc_page_table(vm) | 3; 125 120 /* fall through */ 126 121 case 2: 127 122 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pte_index(vm, vaddr) * 8;
+8
tools/testing/selftests/kvm/lib/kvm_util.c
··· 2209 2209 return vm_phy_pages_alloc(vm, 1, paddr_min, memslot); 2210 2210 } 2211 2211 2212 + /* Arbitrary minimum physical address used for virtual translation tables. */ 2213 + #define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 2214 + 2215 + vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm) 2216 + { 2217 + return vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0); 2218 + } 2219 + 2212 2220 /* 2213 2221 * Address Guest Virtual to Host Virtual 2214 2222 *
-2
tools/testing/selftests/kvm/lib/s390x/processor.c
··· 9 9 #include "kvm_util.h" 10 10 #include "../kvm_util_internal.h" 11 11 12 - #define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 13 - 14 12 #define PAGES_PER_REGION 4 15 13 16 14 void virt_pgd_alloc(struct kvm_vm *vm)
+4 -15
tools/testing/selftests/kvm/lib/x86_64/processor.c
··· 17 17 #define DEFAULT_CODE_SELECTOR 0x8 18 18 #define DEFAULT_DATA_SELECTOR 0x10 19 19 20 - /* Minimum physical address used for virtual translation tables. */ 21 - #define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 22 - 23 20 vm_vaddr_t exception_handlers; 24 21 25 22 /* Virtual translation table structure declarations */ ··· 211 214 212 215 /* If needed, create page map l4 table. */ 213 216 if (!vm->pgd_created) { 214 - vm_paddr_t paddr = vm_phy_page_alloc(vm, 215 - KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0); 216 - vm->pgd = paddr; 217 + vm->pgd = vm_alloc_page_table(vm); 217 218 vm->pgd_created = true; 218 219 } 219 220 } ··· 249 254 /* Allocate page directory pointer table if not present. */ 250 255 pml4e = addr_gpa2hva(vm, vm->pgd); 251 256 if (!pml4e[index[3]].present) { 252 - pml4e[index[3]].address = vm_phy_page_alloc(vm, 253 - KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0) 254 - >> vm->page_shift; 257 + pml4e[index[3]].address = vm_alloc_page_table(vm) >> vm->page_shift; 255 258 pml4e[index[3]].writable = true; 256 259 pml4e[index[3]].present = true; 257 260 } ··· 258 265 struct pageDirectoryPointerEntry *pdpe; 259 266 pdpe = addr_gpa2hva(vm, pml4e[index[3]].address * vm->page_size); 260 267 if (!pdpe[index[2]].present) { 261 - pdpe[index[2]].address = vm_phy_page_alloc(vm, 262 - KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0) 263 - >> vm->page_shift; 268 + pdpe[index[2]].address = vm_alloc_page_table(vm) >> vm->page_shift; 264 269 pdpe[index[2]].writable = true; 265 270 pdpe[index[2]].present = true; 266 271 } ··· 267 276 struct pageDirectoryEntry *pde; 268 277 pde = addr_gpa2hva(vm, pdpe[index[2]].address * vm->page_size); 269 278 if (!pde[index[1]].present) { 270 - pde[index[1]].address = vm_phy_page_alloc(vm, 271 - KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0) 272 - >> vm->page_shift; 279 + pde[index[1]].address = vm_alloc_page_table(vm) >> vm->page_shift; 273 280 pde[index[1]].writable = true; 274 281 pde[index[1]].present = true; 275 282 }
+3 -9
tools/testing/selftests/kvm/lib/x86_64/vmx.c
··· 426 426 /* Allocate page directory pointer table if not present. */ 427 427 pml4e = vmx->eptp_hva; 428 428 if (!pml4e[index[3]].readable) { 429 - pml4e[index[3]].address = vm_phy_page_alloc(vm, 430 - KVM_EPT_PAGE_TABLE_MIN_PADDR, 0) 431 - >> vm->page_shift; 429 + pml4e[index[3]].address = vm_alloc_page_table(vm) >> vm->page_shift; 432 430 pml4e[index[3]].writable = true; 433 431 pml4e[index[3]].readable = true; 434 432 pml4e[index[3]].executable = true; ··· 436 438 struct eptPageTableEntry *pdpe; 437 439 pdpe = addr_gpa2hva(vm, pml4e[index[3]].address * vm->page_size); 438 440 if (!pdpe[index[2]].readable) { 439 - pdpe[index[2]].address = vm_phy_page_alloc(vm, 440 - KVM_EPT_PAGE_TABLE_MIN_PADDR, 0) 441 - >> vm->page_shift; 441 + pdpe[index[2]].address = vm_alloc_page_table(vm) >> vm->page_shift; 442 442 pdpe[index[2]].writable = true; 443 443 pdpe[index[2]].readable = true; 444 444 pdpe[index[2]].executable = true; ··· 446 450 struct eptPageTableEntry *pde; 447 451 pde = addr_gpa2hva(vm, pdpe[index[2]].address * vm->page_size); 448 452 if (!pde[index[1]].readable) { 449 - pde[index[1]].address = vm_phy_page_alloc(vm, 450 - KVM_EPT_PAGE_TABLE_MIN_PADDR, 0) 451 - >> vm->page_shift; 453 + pde[index[1]].address = vm_alloc_page_table(vm) >> vm->page_shift; 452 454 pde[index[1]].writable = true; 453 455 pde[index[1]].readable = true; 454 456 pde[index[1]].executable = true;