Lines Matching refs:vm

20 static uint64_t page_align(struct kvm_vm *vm, uint64_t v)  in page_align()  argument
22 return (v + vm->page_size) & ~(vm->page_size - 1); in page_align()
25 static uint64_t pgd_index(struct kvm_vm *vm, vm_vaddr_t gva) in pgd_index() argument
27 unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift; in pgd_index()
28 uint64_t mask = (1UL << (vm->va_bits - shift)) - 1; in pgd_index()
33 static uint64_t pud_index(struct kvm_vm *vm, vm_vaddr_t gva) in pud_index() argument
35 unsigned int shift = 2 * (vm->page_shift - 3) + vm->page_shift; in pud_index()
36 uint64_t mask = (1UL << (vm->page_shift - 3)) - 1; in pud_index()
38 TEST_ASSERT(vm->pgtable_levels == 4, in pud_index()
39 "Mode %d does not have 4 page table levels", vm->mode); in pud_index()
44 static uint64_t pmd_index(struct kvm_vm *vm, vm_vaddr_t gva) in pmd_index() argument
46 unsigned int shift = (vm->page_shift - 3) + vm->page_shift; in pmd_index()
47 uint64_t mask = (1UL << (vm->page_shift - 3)) - 1; in pmd_index()
49 TEST_ASSERT(vm->pgtable_levels >= 3, in pmd_index()
50 "Mode %d does not have >= 3 page table levels", vm->mode); in pmd_index()
55 static uint64_t pte_index(struct kvm_vm *vm, vm_vaddr_t gva) in pte_index() argument
57 uint64_t mask = (1UL << (vm->page_shift - 3)) - 1; in pte_index()
58 return (gva >> vm->page_shift) & mask; in pte_index()
61 static uint64_t addr_pte(struct kvm_vm *vm, uint64_t pa, uint64_t attrs) in addr_pte() argument
65 pte = pa & GENMASK(47, vm->page_shift); in addr_pte()
66 if (vm->page_shift == 16) in addr_pte()
73 static uint64_t pte_addr(struct kvm_vm *vm, uint64_t pte) in pte_addr() argument
77 pa = pte & GENMASK(47, vm->page_shift); in pte_addr()
78 if (vm->page_shift == 16) in pte_addr()
84 static uint64_t ptrs_per_pgd(struct kvm_vm *vm) in ptrs_per_pgd() argument
86 unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift; in ptrs_per_pgd()
87 return 1 << (vm->va_bits - shift); in ptrs_per_pgd()
90 static uint64_t __maybe_unused ptrs_per_pte(struct kvm_vm *vm) in ptrs_per_pte() argument
92 return 1 << (vm->page_shift - 3); in ptrs_per_pte()
95 void virt_arch_pgd_alloc(struct kvm_vm *vm) in virt_arch_pgd_alloc() argument
97 size_t nr_pages = page_align(vm, ptrs_per_pgd(vm) * 8) / vm->page_size; in virt_arch_pgd_alloc()
99 if (vm->pgd_created) in virt_arch_pgd_alloc()
102 vm->pgd = vm_phy_pages_alloc(vm, nr_pages, in virt_arch_pgd_alloc()
104 vm->memslots[MEM_REGION_PT]); in virt_arch_pgd_alloc()
105 vm->pgd_created = true; in virt_arch_pgd_alloc()
108 static void _virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, in _virt_pg_map() argument
114 TEST_ASSERT((vaddr % vm->page_size) == 0, in _virt_pg_map()
116 " vaddr: 0x%lx vm->page_size: 0x%x", vaddr, vm->page_size); in _virt_pg_map()
117 TEST_ASSERT(sparsebit_is_set(vm->vpages_valid, in _virt_pg_map()
118 (vaddr >> vm->page_shift)), in _virt_pg_map()
120 TEST_ASSERT((paddr % vm->page_size) == 0, in _virt_pg_map()
122 " paddr: 0x%lx vm->page_size: 0x%x", paddr, vm->page_size); in _virt_pg_map()
123 TEST_ASSERT((paddr >> vm->page_shift) <= vm->max_gfn, in _virt_pg_map()
126 paddr, vm->max_gfn, vm->page_size); in _virt_pg_map()
128 ptep = addr_gpa2hva(vm, vm->pgd) + pgd_index(vm, vaddr) * 8; in _virt_pg_map()
130 *ptep = addr_pte(vm, vm_alloc_page_table(vm), 3); in _virt_pg_map()
132 switch (vm->pgtable_levels) { in _virt_pg_map()
134 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pud_index(vm, vaddr) * 8; in _virt_pg_map()
136 *ptep = addr_pte(vm, vm_alloc_page_table(vm), 3); in _virt_pg_map()
139 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pmd_index(vm, vaddr) * 8; in _virt_pg_map()
141 *ptep = addr_pte(vm, vm_alloc_page_table(vm), 3); in _virt_pg_map()
144 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pte_index(vm, vaddr) * 8; in _virt_pg_map()
150 *ptep = addr_pte(vm, paddr, (attr_idx << 2) | (1 << 10) | 3); /* AF */ in _virt_pg_map()
153 void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr) in virt_arch_pg_map() argument
157 _virt_pg_map(vm, vaddr, paddr, attr_idx); in virt_arch_pg_map()
160 uint64_t *virt_get_pte_hva(struct kvm_vm *vm, vm_vaddr_t gva) in virt_get_pte_hva() argument
164 if (!vm->pgd_created) in virt_get_pte_hva()
167 ptep = addr_gpa2hva(vm, vm->pgd) + pgd_index(vm, gva) * 8; in virt_get_pte_hva()
171 switch (vm->pgtable_levels) { in virt_get_pte_hva()
173 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pud_index(vm, gva) * 8; in virt_get_pte_hva()
178 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pmd_index(vm, gva) * 8; in virt_get_pte_hva()
183 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pte_index(vm, gva) * 8; in virt_get_pte_hva()
198 vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva) in addr_arch_gva2gpa() argument
200 uint64_t *ptep = virt_get_pte_hva(vm, gva); in addr_arch_gva2gpa()
202 return pte_addr(vm, *ptep) + (gva & (vm->page_size - 1)); in addr_arch_gva2gpa()
205 static void pte_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent, uint64_t page, int level) in pte_dump() argument
214 for (pte = page; pte < page + ptrs_per_pte(vm) * 8; pte += 8) { in pte_dump()
215 ptep = addr_gpa2hva(vm, pte); in pte_dump()
219 pte_dump(stream, vm, indent + 1, pte_addr(vm, *ptep), level + 1); in pte_dump()
224 void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) in virt_arch_dump() argument
226 int level = 4 - (vm->pgtable_levels - 1); in virt_arch_dump()
229 if (!vm->pgd_created) in virt_arch_dump()
232 for (pgd = vm->pgd; pgd < vm->pgd + ptrs_per_pgd(vm) * 8; pgd += 8) { in virt_arch_dump()
233 ptep = addr_gpa2hva(vm, pgd); in virt_arch_dump()
237 pte_dump(stream, vm, indent + 1, pte_addr(vm, *ptep), level); in virt_arch_dump()
244 struct kvm_vm *vm = vcpu->vm; in aarch64_vcpu_setup() local
252 vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &preferred); in aarch64_vcpu_setup()
268 switch (vm->mode) { in aarch64_vcpu_setup()
293 TEST_FAIL("Unknown guest mode, mode: 0x%x", vm->mode); in aarch64_vcpu_setup()
296 ttbr0_el1 = vm->pgd & GENMASK(47, vm->page_shift); in aarch64_vcpu_setup()
299 switch (vm->mode) { in aarch64_vcpu_setup()
302 ttbr0_el1 |= FIELD_GET(GENMASK(51, 48), vm->pgd) << 2; in aarch64_vcpu_setup()
321 TEST_FAIL("Unknown guest mode, mode: 0x%x", vm->mode); in aarch64_vcpu_setup()
327 tcr_el1 |= (64 - vm->va_bits) /* T0SZ */; in aarch64_vcpu_setup()
347 struct kvm_vcpu *aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, in aarch64_vcpu_add() argument
352 struct kvm_vcpu *vcpu = __vm_vcpu_add(vm, vcpu_id); in aarch64_vcpu_add()
354 stack_size = vm->page_size == 4096 ? DEFAULT_STACK_PGS * vm->page_size : in aarch64_vcpu_add()
355 vm->page_size; in aarch64_vcpu_add()
356 stack_vaddr = __vm_vaddr_alloc(vm, stack_size, in aarch64_vcpu_add()
368 struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, in vm_arch_vcpu_add() argument
371 return aarch64_vcpu_add(vm, vcpu_id, NULL, guest_code); in vm_arch_vcpu_add()
461 void vm_init_descriptor_tables(struct kvm_vm *vm) in vm_init_descriptor_tables() argument
463 vm->handlers = __vm_vaddr_alloc(vm, sizeof(struct handlers), in vm_init_descriptor_tables()
464 vm->page_size, MEM_REGION_DATA); in vm_init_descriptor_tables()
466 *(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers; in vm_init_descriptor_tables()
469 void vm_install_sync_handler(struct kvm_vm *vm, int vector, int ec, in vm_install_sync_handler() argument
472 struct handlers *handlers = addr_gva2hva(vm, vm->handlers); in vm_install_sync_handler()
480 void vm_install_exception_handler(struct kvm_vm *vm, int vector, in vm_install_exception_handler() argument
483 struct handlers *handlers = addr_gva2hva(vm, vm->handlers); in vm_install_exception_handler()
578 void vm_vaddr_populate_bitmap(struct kvm_vm *vm) in vm_vaddr_populate_bitmap() argument
584 sparsebit_set_num(vm->vpages_valid, 0, in vm_vaddr_populate_bitmap()
585 (1ULL << vm->va_bits) >> vm->page_shift); in vm_vaddr_populate_bitmap()