root/tools/testing/selftests/kvm/include/kvm_util.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


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

/* [<][>][^][v][top][bottom][index][help] */