Lines Matching full:vm
19 static uint64_t page_align(struct kvm_vm *vm, uint64_t v) in page_align() argument
21 return (v + vm->page_size) & ~(vm->page_size - 1); in page_align()
24 static uint64_t pgd_index(struct kvm_vm *vm, vm_vaddr_t gva) in pgd_index() argument
26 unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift; in pgd_index()
27 uint64_t mask = (1UL << (vm->va_bits - shift)) - 1; in pgd_index()
32 static uint64_t pud_index(struct kvm_vm *vm, vm_vaddr_t gva) in pud_index() argument
34 unsigned int shift = 2 * (vm->page_shift - 3) + vm->page_shift; in pud_index()
35 uint64_t mask = (1UL << (vm->page_shift - 3)) - 1; in pud_index()
37 TEST_ASSERT(vm->pgtable_levels == 4, in pud_index()
38 "Mode %d does not have 4 page table levels", vm->mode); in pud_index()
43 static uint64_t pmd_index(struct kvm_vm *vm, vm_vaddr_t gva) in pmd_index() argument
45 unsigned int shift = (vm->page_shift - 3) + vm->page_shift; in pmd_index()
46 uint64_t mask = (1UL << (vm->page_shift - 3)) - 1; in pmd_index()
48 TEST_ASSERT(vm->pgtable_levels >= 3, in pmd_index()
49 "Mode %d does not have >= 3 page table levels", vm->mode); in pmd_index()
54 static uint64_t pte_index(struct kvm_vm *vm, vm_vaddr_t gva) in pte_index() argument
56 uint64_t mask = (1UL << (vm->page_shift - 3)) - 1; in pte_index()
57 return (gva >> vm->page_shift) & mask; in pte_index()
60 static uint64_t pte_addr(struct kvm_vm *vm, uint64_t entry) in pte_addr() argument
62 uint64_t mask = ((1UL << (vm->va_bits - vm->page_shift)) - 1) << vm->page_shift; in pte_addr()
66 static uint64_t ptrs_per_pgd(struct kvm_vm *vm) in ptrs_per_pgd() argument
68 unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift; in ptrs_per_pgd()
69 return 1 << (vm->va_bits - shift); in ptrs_per_pgd()
72 static uint64_t __maybe_unused ptrs_per_pte(struct kvm_vm *vm) in ptrs_per_pte() argument
74 return 1 << (vm->page_shift - 3); in ptrs_per_pte()
77 void virt_pgd_alloc(struct kvm_vm *vm, uint32_t pgd_memslot) in virt_pgd_alloc() argument
79 if (!vm->pgd_created) { in virt_pgd_alloc()
80 vm_paddr_t paddr = vm_phy_pages_alloc(vm, in virt_pgd_alloc()
81 page_align(vm, ptrs_per_pgd(vm) * 8) / vm->page_size, in virt_pgd_alloc()
83 vm->pgd = paddr; in virt_pgd_alloc()
84 vm->pgd_created = true; in virt_pgd_alloc()
88 void _virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, in _virt_pg_map() argument
94 TEST_ASSERT((vaddr % vm->page_size) == 0, in _virt_pg_map()
96 " vaddr: 0x%lx vm->page_size: 0x%x", vaddr, vm->page_size); in _virt_pg_map()
97 TEST_ASSERT(sparsebit_is_set(vm->vpages_valid, in _virt_pg_map()
98 (vaddr >> vm->page_shift)), in _virt_pg_map()
100 TEST_ASSERT((paddr % vm->page_size) == 0, in _virt_pg_map()
102 " paddr: 0x%lx vm->page_size: 0x%x", paddr, vm->page_size); in _virt_pg_map()
103 TEST_ASSERT((paddr >> vm->page_shift) <= vm->max_gfn, in _virt_pg_map()
105 " paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x", in _virt_pg_map()
106 paddr, vm->max_gfn, vm->page_size); in _virt_pg_map()
108 ptep = addr_gpa2hva(vm, vm->pgd) + pgd_index(vm, vaddr) * 8; in _virt_pg_map()
110 *ptep = vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, pgd_memslot); in _virt_pg_map()
114 switch (vm->pgtable_levels) { in _virt_pg_map()
116 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pud_index(vm, vaddr) * 8; in _virt_pg_map()
118 *ptep = vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, pgd_memslot); in _virt_pg_map()
123 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pmd_index(vm, vaddr) * 8; in _virt_pg_map()
125 *ptep = vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, pgd_memslot); in _virt_pg_map()
130 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pte_index(vm, vaddr) * 8; in _virt_pg_map()
140 void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, in virt_pg_map() argument
145 _virt_pg_map(vm, vaddr, paddr, pgd_memslot, attr_idx); in virt_pg_map()
148 vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva) in addr_gva2gpa() argument
152 if (!vm->pgd_created) in addr_gva2gpa()
155 ptep = addr_gpa2hva(vm, vm->pgd) + pgd_index(vm, gva) * 8; in addr_gva2gpa()
159 switch (vm->pgtable_levels) { in addr_gva2gpa()
161 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pud_index(vm, gva) * 8; in addr_gva2gpa()
166 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pmd_index(vm, gva) * 8; in addr_gva2gpa()
171 ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pte_index(vm, gva) * 8; in addr_gva2gpa()
179 return pte_addr(vm, *ptep) + (gva & (vm->page_size - 1)); in addr_gva2gpa()
182 TEST_FAIL("No mapping for vm virtual address, gva: 0x%lx", gva); in addr_gva2gpa()
186 static void pte_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent, uint64_t page, int level) in pte_dump() argument
195 for (pte = page; pte < page + ptrs_per_pte(vm) * 8; pte += 8) { in pte_dump()
196 ptep = addr_gpa2hva(vm, pte); in pte_dump()
200 pte_dump(stream, vm, indent + 1, pte_addr(vm, *ptep), level + 1); in pte_dump()
205 void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) in virt_dump() argument
207 int level = 4 - (vm->pgtable_levels - 1); in virt_dump()
210 if (!vm->pgd_created) in virt_dump()
213 for (pgd = vm->pgd; pgd < vm->pgd + ptrs_per_pgd(vm) * 8; pgd += 8) { in virt_dump()
214 ptep = addr_gpa2hva(vm, pgd); in virt_dump()
218 pte_dump(stream, vm, indent + 1, pte_addr(vm, *ptep), level); in virt_dump()
227 struct kvm_vm *vm; in vm_create_default() local
229 vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, O_RDWR); in vm_create_default()
231 kvm_vm_elf_load(vm, program_invocation_name, 0, 0); in vm_create_default()
232 vm_vcpu_add_default(vm, vcpuid, guest_code); in vm_create_default()
234 return vm; in vm_create_default()
237 void aarch64_vcpu_setup(struct kvm_vm *vm, int vcpuid, struct kvm_vcpu_init *init) in aarch64_vcpu_setup() argument
247 vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &preferred); in aarch64_vcpu_setup()
251 vcpu_ioctl(vm, vcpuid, KVM_ARM_VCPU_INIT, init); in aarch64_vcpu_setup()
257 set_reg(vm, vcpuid, ARM64_SYS_REG(CPACR_EL1), 3 << 20); in aarch64_vcpu_setup()
259 get_reg(vm, vcpuid, ARM64_SYS_REG(SCTLR_EL1), &sctlr_el1); in aarch64_vcpu_setup()
260 get_reg(vm, vcpuid, ARM64_SYS_REG(TCR_EL1), &tcr_el1); in aarch64_vcpu_setup()
262 switch (vm->mode) { in aarch64_vcpu_setup()
290 TEST_FAIL("Unknown guest mode, mode: 0x%x", vm->mode); in aarch64_vcpu_setup()
296 tcr_el1 |= (64 - vm->va_bits) /* T0SZ */; in aarch64_vcpu_setup()
298 set_reg(vm, vcpuid, ARM64_SYS_REG(SCTLR_EL1), sctlr_el1); in aarch64_vcpu_setup()
299 set_reg(vm, vcpuid, ARM64_SYS_REG(TCR_EL1), tcr_el1); in aarch64_vcpu_setup()
300 set_reg(vm, vcpuid, ARM64_SYS_REG(MAIR_EL1), DEFAULT_MAIR_EL1); in aarch64_vcpu_setup()
301 set_reg(vm, vcpuid, ARM64_SYS_REG(TTBR0_EL1), vm->pgd); in aarch64_vcpu_setup()
304 void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, uint8_t indent) in vcpu_dump() argument
308 get_reg(vm, vcpuid, ARM64_CORE_REG(regs.pstate), &pstate); in vcpu_dump()
309 get_reg(vm, vcpuid, ARM64_CORE_REG(regs.pc), &pc); in vcpu_dump()
315 void aarch64_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, in aarch64_vcpu_add_default() argument
318 size_t stack_size = vm->page_size == 4096 ? in aarch64_vcpu_add_default()
319 DEFAULT_STACK_PGS * vm->page_size : in aarch64_vcpu_add_default()
320 vm->page_size; in aarch64_vcpu_add_default()
321 uint64_t stack_vaddr = vm_vaddr_alloc(vm, stack_size, in aarch64_vcpu_add_default()
324 vm_vcpu_add(vm, vcpuid); in aarch64_vcpu_add_default()
325 aarch64_vcpu_setup(vm, vcpuid, init); in aarch64_vcpu_add_default()
327 set_reg(vm, vcpuid, ARM64_CORE_REG(sp_el1), stack_vaddr + stack_size); in aarch64_vcpu_add_default()
328 set_reg(vm, vcpuid, ARM64_CORE_REG(regs.pc), (uint64_t)guest_code); in aarch64_vcpu_add_default()
331 void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code) in vm_vcpu_add_default() argument
333 aarch64_vcpu_add_default(vm, vcpuid, NULL, guest_code); in vm_vcpu_add_default()
336 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...) in vcpu_args_set() argument
347 set_reg(vm, vcpuid, ARM64_CORE_REG(regs.regs[i]), in vcpu_args_set()
354 void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid) in assert_on_unhandled_exception() argument