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