]> git.ipfire.org Git - thirdparty/linux.git/blob - tools/testing/selftests/kvm/include/kvm_util.h
Merge branch 'regulator-4.21' into regulator-5.0
[thirdparty/linux.git] / tools / testing / selftests / kvm / include / kvm_util.h
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_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
106 struct kvm_mp_state *mp_state);
107 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs);
108 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs);
109 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...);
110 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid,
111 struct kvm_sregs *sregs);
112 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid,
113 struct kvm_sregs *sregs);
114 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid,
115 struct kvm_sregs *sregs);
116 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
117 struct kvm_vcpu_events *events);
118 void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
119 struct kvm_vcpu_events *events);
120
121 const char *exit_reason_str(unsigned int exit_reason);
122
123 void virt_pgd_alloc(struct kvm_vm *vm, uint32_t pgd_memslot);
124 void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
125 uint32_t pgd_memslot);
126 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
127 uint32_t memslot);
128 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
129 vm_paddr_t paddr_min, uint32_t memslot);
130
131 struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_size,
132 void *guest_code);
133 void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code);
134
135 struct kvm_userspace_memory_region *
136 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
137 uint64_t end);
138
139 struct kvm_dirty_log *
140 allocate_kvm_dirty_log(struct kvm_userspace_memory_region *region);
141
142 int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd);
143
144 #define sync_global_to_guest(vm, g) ({ \
145 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
146 memcpy(_p, &(g), sizeof(g)); \
147 })
148
149 #define sync_global_from_guest(vm, g) ({ \
150 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
151 memcpy(&(g), _p, sizeof(g)); \
152 })
153
154 /* ucall implementation types */
155 typedef enum {
156 UCALL_PIO,
157 UCALL_MMIO,
158 } ucall_type_t;
159
160 /* Common ucalls */
161 enum {
162 UCALL_NONE,
163 UCALL_SYNC,
164 UCALL_ABORT,
165 UCALL_DONE,
166 };
167
168 #define UCALL_MAX_ARGS 6
169
170 struct ucall {
171 uint64_t cmd;
172 uint64_t args[UCALL_MAX_ARGS];
173 };
174
175 void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg);
176 void ucall_uninit(struct kvm_vm *vm);
177 void ucall(uint64_t cmd, int nargs, ...);
178 uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);
179
180 #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage)
181 #define GUEST_DONE() ucall(UCALL_DONE, 0)
182 #define GUEST_ASSERT(_condition) do { \
183 if (!(_condition)) \
184 ucall(UCALL_ABORT, 2, \
185 "Failed guest assert: " \
186 #_condition, __LINE__); \
187 } while (0)
188
189 #endif /* SELFTEST_KVM_UTIL_H */