1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * tools/testing/selftests/kvm/include/kvm_util.h 4 * 5 * Copyright (C) 2018, Google LLC. 6 */ 7 #ifndef SELFTEST_KVM_UTIL_H 8 #define SELFTEST_KVM_UTIL_H 9 10 #include "test_util.h" 11 12 #include "asm/kvm.h" 13 #include "linux/kvm.h" 14 #include <sys/ioctl.h> 15 16 #include "sparsebit.h" 17 18 19 /* Callers of kvm_util only have an incomplete/opaque description of the 20 * structure kvm_util is using to maintain the state of a VM. 21 */ 22 struct kvm_vm; 23 24 typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */ 25 typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */ 26 27 /* Minimum allocated guest virtual and physical addresses */ 28 #define KVM_UTIL_MIN_VADDR 0x2000 29 30 #define DEFAULT_GUEST_PHY_PAGES 512 31 #define DEFAULT_GUEST_STACK_VADDR_MIN 0xab6000 32 #define DEFAULT_STACK_PGS 5 33 34 enum vm_guest_mode { 35 VM_MODE_P52V48_4K, 36 VM_MODE_P52V48_64K, 37 VM_MODE_P48V48_4K, 38 VM_MODE_P48V48_64K, 39 VM_MODE_P40V48_4K, 40 VM_MODE_P40V48_64K, 41 NUM_VM_MODES, 42 }; 43 44 #ifdef __aarch64__ 45 #define VM_MODE_DEFAULT VM_MODE_P40V48_4K 46 #else 47 #define VM_MODE_DEFAULT VM_MODE_P52V48_4K 48 #endif 49 50 #define vm_guest_mode_string(m) vm_guest_mode_string[m] 51 extern const char * const vm_guest_mode_string[]; 52 53 enum vm_mem_backing_src_type { 54 VM_MEM_SRC_ANONYMOUS, 55 VM_MEM_SRC_ANONYMOUS_THP, 56 VM_MEM_SRC_ANONYMOUS_HUGETLB, 57 }; 58 59 int kvm_check_cap(long cap); 60 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap); 61 62 struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm); 63 struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, 64 int perm, unsigned long type); 65 void kvm_vm_free(struct kvm_vm *vmp); 66 void kvm_vm_restart(struct kvm_vm *vmp, int perm); 67 void kvm_vm_release(struct kvm_vm *vmp); 68 void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log); 69 void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log, 70 uint64_t first_page, uint32_t num_pages); 71 72 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, const vm_vaddr_t gva, 73 size_t len); 74 75 void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename, 76 uint32_t data_memslot, uint32_t pgd_memslot); 77 78 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent); 79 void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, 80 uint8_t indent); 81 82 void vm_create_irqchip(struct kvm_vm *vm); 83 84 void vm_userspace_mem_region_add(struct kvm_vm *vm, 85 enum vm_mem_backing_src_type src_type, 86 uint64_t guest_paddr, uint32_t slot, uint64_t npages, 87 uint32_t flags); 88 89 void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl, 90 void *arg); 91 int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl, 92 void *arg); 93 void vm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg); 94 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); 95 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid); 96 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, 97 uint32_t data_memslot, uint32_t pgd_memslot); 98 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, 99 size_t size, uint32_t pgd_memslot); 100 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa); 101 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); 102 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); 103 vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva); 104 105 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid); 106 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid); 107 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid); 108 void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid); 109 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid, 110 struct kvm_mp_state *mp_state); 111 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs); 112 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs); 113 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...); 114 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, 115 struct kvm_sregs *sregs); 116 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, 117 struct kvm_sregs *sregs); 118 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, 119 struct kvm_sregs *sregs); 120 #ifdef __KVM_HAVE_VCPU_EVENTS 121 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid, 122 struct kvm_vcpu_events *events); 123 void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid, 124 struct kvm_vcpu_events *events); 125 #endif 126 #ifdef __x86_64__ 127 void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid, 128 struct kvm_nested_state *state); 129 int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid, 130 struct kvm_nested_state *state, bool ignore_error); 131 #endif 132 133 const char *exit_reason_str(unsigned int exit_reason); 134 135 void virt_pgd_alloc(struct kvm_vm *vm, uint32_t pgd_memslot); 136 void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, 137 uint32_t pgd_memslot); 138 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min, 139 uint32_t memslot); 140 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, 141 vm_paddr_t paddr_min, uint32_t memslot); 142 143 struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_size, 144 void *guest_code); 145 void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code); 146 147 bool vm_is_unrestricted_guest(struct kvm_vm *vm); 148 149 struct kvm_userspace_memory_region * 150 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start, 151 uint64_t end); 152 153 struct kvm_dirty_log * 154 allocate_kvm_dirty_log(struct kvm_userspace_memory_region *region); 155 156 int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd); 157 158 #define sync_global_to_guest(vm, g) ({ \ 159 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ 160 memcpy(_p, &(g), sizeof(g)); \ 161 }) 162 163 #define sync_global_from_guest(vm, g) ({ \ 164 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ 165 memcpy(&(g), _p, sizeof(g)); \ 166 }) 167 168 /* ucall implementation types */ 169 typedef enum { 170 UCALL_PIO, 171 UCALL_MMIO, 172 } ucall_type_t; 173 174 /* Common ucalls */ 175 enum { 176 UCALL_NONE, 177 UCALL_SYNC, 178 UCALL_ABORT, 179 UCALL_DONE, 180 }; 181 182 #define UCALL_MAX_ARGS 6 183 184 struct ucall { 185 uint64_t cmd; 186 uint64_t args[UCALL_MAX_ARGS]; 187 }; 188 189 void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg); 190 void ucall_uninit(struct kvm_vm *vm); 191 void ucall(uint64_t cmd, int nargs, ...); 192 uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); 193 194 #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) 195 #define GUEST_DONE() ucall(UCALL_DONE, 0) 196 #define GUEST_ASSERT(_condition) do { \ 197 if (!(_condition)) \ 198 ucall(UCALL_ABORT, 2, \ 199 "Failed guest assert: " \ 200 #_condition, __LINE__); \ 201 } while (0) 202 203 #endif /* SELFTEST_KVM_UTIL_H */ 204