1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * tools/testing/selftests/kvm/include/kvm_util_base.h
4 *
5 * Copyright (C) 2018, Google LLC.
6 */
7 #ifndef SELFTEST_KVM_UTIL_BASE_H
8 #define SELFTEST_KVM_UTIL_BASE_H
9
10 #include "test_util.h"
11
12 #include <linux/compiler.h>
13 #include "linux/hashtable.h"
14 #include "linux/list.h"
15 #include <linux/kernel.h>
16 #include <linux/kvm.h>
17 #include "linux/rbtree.h"
18 #include <linux/types.h>
19
20 #include <asm/atomic.h>
21
22 #include <sys/ioctl.h>
23
24 #include "sparsebit.h"
25
26 /*
27 * Provide a version of static_assert() that is guaranteed to have an optional
28 * message param. If _ISOC11_SOURCE is defined, glibc (/usr/include/assert.h)
29 * #undefs and #defines static_assert() as a direct alias to _Static_assert(),
30 * i.e. effectively makes the message mandatory. Many KVM selftests #define
31 * _GNU_SOURCE for various reasons, and _GNU_SOURCE implies _ISOC11_SOURCE. As
32 * a result, static_assert() behavior is non-deterministic and may or may not
33 * require a message depending on #include order.
34 */
35 #define __kvm_static_assert(expr, msg, ...) _Static_assert(expr, msg)
36 #define kvm_static_assert(expr, ...) __kvm_static_assert(expr, ##__VA_ARGS__, #expr)
37
38 #define KVM_DEV_PATH "/dev/kvm"
39 #define KVM_MAX_VCPUS 512
40
41 #define NSEC_PER_SEC 1000000000L
42
43 typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */
44 typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */
45
46 struct userspace_mem_region {
47 struct kvm_userspace_memory_region2 region;
48 struct sparsebit *unused_phy_pages;
49 int fd;
50 off_t offset;
51 enum vm_mem_backing_src_type backing_src_type;
52 void *host_mem;
53 void *host_alias;
54 void *mmap_start;
55 void *mmap_alias;
56 size_t mmap_size;
57 struct rb_node gpa_node;
58 struct rb_node hva_node;
59 struct hlist_node slot_node;
60 };
61
62 struct kvm_vcpu {
63 struct list_head list;
64 uint32_t id;
65 int fd;
66 struct kvm_vm *vm;
67 struct kvm_run *run;
68 #ifdef __x86_64__
69 struct kvm_cpuid2 *cpuid;
70 #endif
71 struct kvm_dirty_gfn *dirty_gfns;
72 uint32_t fetch_index;
73 uint32_t dirty_gfns_count;
74 };
75
76 struct userspace_mem_regions {
77 struct rb_root gpa_tree;
78 struct rb_root hva_tree;
79 DECLARE_HASHTABLE(slot_hash, 9);
80 };
81
82 enum kvm_mem_region_type {
83 MEM_REGION_CODE,
84 MEM_REGION_DATA,
85 MEM_REGION_PT,
86 MEM_REGION_TEST_DATA,
87 NR_MEM_REGIONS,
88 };
89
90 struct kvm_vm {
91 int mode;
92 unsigned long type;
93 int kvm_fd;
94 int fd;
95 unsigned int pgtable_levels;
96 unsigned int page_size;
97 unsigned int page_shift;
98 unsigned int pa_bits;
99 unsigned int va_bits;
100 uint64_t max_gfn;
101 struct list_head vcpus;
102 struct userspace_mem_regions regions;
103 struct sparsebit *vpages_valid;
104 struct sparsebit *vpages_mapped;
105 bool has_irqchip;
106 bool pgd_created;
107 vm_paddr_t ucall_mmio_addr;
108 vm_paddr_t pgd;
109 vm_vaddr_t gdt;
110 vm_vaddr_t tss;
111 vm_vaddr_t idt;
112 vm_vaddr_t handlers;
113 uint32_t dirty_ring_size;
114
115 /* Cache of information for binary stats interface */
116 int stats_fd;
117 struct kvm_stats_header stats_header;
118 struct kvm_stats_desc *stats_desc;
119
120 /*
121 * KVM region slots. These are the default memslots used by page
122 * allocators, e.g., lib/elf uses the memslots[MEM_REGION_CODE]
123 * memslot.
124 */
125 uint32_t memslots[NR_MEM_REGIONS];
126 };
127
128 struct vcpu_reg_sublist {
129 const char *name;
130 long capability;
131 int feature;
132 int feature_type;
133 bool finalize;
134 __u64 *regs;
135 __u64 regs_n;
136 __u64 *rejects_set;
137 __u64 rejects_set_n;
138 __u64 *skips_set;
139 __u64 skips_set_n;
140 };
141
142 struct vcpu_reg_list {
143 char *name;
144 struct vcpu_reg_sublist sublists[];
145 };
146
147 #define for_each_sublist(c, s) \
148 for ((s) = &(c)->sublists[0]; (s)->regs; ++(s))
149
150 #define kvm_for_each_vcpu(vm, i, vcpu) \
151 for ((i) = 0; (i) <= (vm)->last_vcpu_id; (i)++) \
152 if (!((vcpu) = vm->vcpus[i])) \
153 continue; \
154 else
155
156 struct userspace_mem_region *
157 memslot2region(struct kvm_vm *vm, uint32_t memslot);
158
vm_get_mem_region(struct kvm_vm * vm,enum kvm_mem_region_type type)159 static inline struct userspace_mem_region *vm_get_mem_region(struct kvm_vm *vm,
160 enum kvm_mem_region_type type)
161 {
162 assert(type < NR_MEM_REGIONS);
163 return memslot2region(vm, vm->memslots[type]);
164 }
165
166 /* Minimum allocated guest virtual and physical addresses */
167 #define KVM_UTIL_MIN_VADDR 0x2000
168 #define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000
169
170 #define DEFAULT_GUEST_STACK_VADDR_MIN 0xab6000
171 #define DEFAULT_STACK_PGS 5
172
173 enum vm_guest_mode {
174 VM_MODE_P52V48_4K,
175 VM_MODE_P52V48_16K,
176 VM_MODE_P52V48_64K,
177 VM_MODE_P48V48_4K,
178 VM_MODE_P48V48_16K,
179 VM_MODE_P48V48_64K,
180 VM_MODE_P40V48_4K,
181 VM_MODE_P40V48_16K,
182 VM_MODE_P40V48_64K,
183 VM_MODE_PXXV48_4K, /* For 48bits VA but ANY bits PA */
184 VM_MODE_P47V64_4K,
185 VM_MODE_P44V64_4K,
186 VM_MODE_P36V48_4K,
187 VM_MODE_P36V48_16K,
188 VM_MODE_P36V48_64K,
189 VM_MODE_P36V47_16K,
190 NUM_VM_MODES,
191 };
192
193 struct vm_shape {
194 enum vm_guest_mode mode;
195 unsigned int type;
196 };
197
198 #define VM_TYPE_DEFAULT 0
199
200 #define VM_SHAPE(__mode) \
201 ({ \
202 struct vm_shape shape = { \
203 .mode = (__mode), \
204 .type = VM_TYPE_DEFAULT \
205 }; \
206 \
207 shape; \
208 })
209
210 #if defined(__aarch64__)
211
212 extern enum vm_guest_mode vm_mode_default;
213
214 #define VM_MODE_DEFAULT vm_mode_default
215 #define MIN_PAGE_SHIFT 12U
216 #define ptes_per_page(page_size) ((page_size) / 8)
217
218 #elif defined(__x86_64__)
219
220 #define VM_MODE_DEFAULT VM_MODE_PXXV48_4K
221 #define MIN_PAGE_SHIFT 12U
222 #define ptes_per_page(page_size) ((page_size) / 8)
223
224 #elif defined(__s390x__)
225
226 #define VM_MODE_DEFAULT VM_MODE_P44V64_4K
227 #define MIN_PAGE_SHIFT 12U
228 #define ptes_per_page(page_size) ((page_size) / 16)
229
230 #elif defined(__riscv)
231
232 #if __riscv_xlen == 32
233 #error "RISC-V 32-bit kvm selftests not supported"
234 #endif
235
236 #define VM_MODE_DEFAULT VM_MODE_P40V48_4K
237 #define MIN_PAGE_SHIFT 12U
238 #define ptes_per_page(page_size) ((page_size) / 8)
239
240 #endif
241
242 #define VM_SHAPE_DEFAULT VM_SHAPE(VM_MODE_DEFAULT)
243
244 #define MIN_PAGE_SIZE (1U << MIN_PAGE_SHIFT)
245 #define PTES_PER_MIN_PAGE ptes_per_page(MIN_PAGE_SIZE)
246
247 struct vm_guest_mode_params {
248 unsigned int pa_bits;
249 unsigned int va_bits;
250 unsigned int page_size;
251 unsigned int page_shift;
252 };
253 extern const struct vm_guest_mode_params vm_guest_mode_params[];
254
255 int open_path_or_exit(const char *path, int flags);
256 int open_kvm_dev_path_or_exit(void);
257
258 bool get_kvm_param_bool(const char *param);
259 bool get_kvm_intel_param_bool(const char *param);
260 bool get_kvm_amd_param_bool(const char *param);
261
262 unsigned int kvm_check_cap(long cap);
263
kvm_has_cap(long cap)264 static inline bool kvm_has_cap(long cap)
265 {
266 return kvm_check_cap(cap);
267 }
268
269 #define __KVM_SYSCALL_ERROR(_name, _ret) \
270 "%s failed, rc: %i errno: %i (%s)", (_name), (_ret), errno, strerror(errno)
271
272 /*
273 * Use the "inner", double-underscore macro when reporting errors from within
274 * other macros so that the name of ioctl() and not its literal numeric value
275 * is printed on error. The "outer" macro is strongly preferred when reporting
276 * errors "directly", i.e. without an additional layer of macros, as it reduces
277 * the probability of passing in the wrong string.
278 */
279 #define __KVM_IOCTL_ERROR(_name, _ret) __KVM_SYSCALL_ERROR(_name, _ret)
280 #define KVM_IOCTL_ERROR(_ioctl, _ret) __KVM_IOCTL_ERROR(#_ioctl, _ret)
281
282 #define kvm_do_ioctl(fd, cmd, arg) \
283 ({ \
284 kvm_static_assert(!_IOC_SIZE(cmd) || sizeof(*arg) == _IOC_SIZE(cmd)); \
285 ioctl(fd, cmd, arg); \
286 })
287
288 #define __kvm_ioctl(kvm_fd, cmd, arg) \
289 kvm_do_ioctl(kvm_fd, cmd, arg)
290
291 #define kvm_ioctl(kvm_fd, cmd, arg) \
292 ({ \
293 int ret = __kvm_ioctl(kvm_fd, cmd, arg); \
294 \
295 TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \
296 })
297
static_assert_is_vm(struct kvm_vm * vm)298 static __always_inline void static_assert_is_vm(struct kvm_vm *vm) { }
299
300 #define __vm_ioctl(vm, cmd, arg) \
301 ({ \
302 static_assert_is_vm(vm); \
303 kvm_do_ioctl((vm)->fd, cmd, arg); \
304 })
305
306 /*
307 * Assert that a VM or vCPU ioctl() succeeded, with extra magic to detect if
308 * the ioctl() failed because KVM killed/bugged the VM. To detect a dead VM,
309 * probe KVM_CAP_USER_MEMORY, which (a) has been supported by KVM since before
310 * selftests existed and (b) should never outright fail, i.e. is supposed to
311 * return 0 or 1. If KVM kills a VM, KVM returns -EIO for all ioctl()s for the
312 * VM and its vCPUs, including KVM_CHECK_EXTENSION.
313 */
314 #define __TEST_ASSERT_VM_VCPU_IOCTL(cond, name, ret, vm) \
315 do { \
316 int __errno = errno; \
317 \
318 static_assert_is_vm(vm); \
319 \
320 if (cond) \
321 break; \
322 \
323 if (errno == EIO && \
324 __vm_ioctl(vm, KVM_CHECK_EXTENSION, (void *)KVM_CAP_USER_MEMORY) < 0) { \
325 TEST_ASSERT(errno == EIO, "KVM killed the VM, should return -EIO"); \
326 TEST_FAIL("KVM killed/bugged the VM, check the kernel log for clues"); \
327 } \
328 errno = __errno; \
329 TEST_ASSERT(cond, __KVM_IOCTL_ERROR(name, ret)); \
330 } while (0)
331
332 #define TEST_ASSERT_VM_VCPU_IOCTL(cond, cmd, ret, vm) \
333 __TEST_ASSERT_VM_VCPU_IOCTL(cond, #cmd, ret, vm)
334
335 #define vm_ioctl(vm, cmd, arg) \
336 ({ \
337 int ret = __vm_ioctl(vm, cmd, arg); \
338 \
339 __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \
340 })
341
static_assert_is_vcpu(struct kvm_vcpu * vcpu)342 static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { }
343
344 #define __vcpu_ioctl(vcpu, cmd, arg) \
345 ({ \
346 static_assert_is_vcpu(vcpu); \
347 kvm_do_ioctl((vcpu)->fd, cmd, arg); \
348 })
349
350 #define vcpu_ioctl(vcpu, cmd, arg) \
351 ({ \
352 int ret = __vcpu_ioctl(vcpu, cmd, arg); \
353 \
354 __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, (vcpu)->vm); \
355 })
356
357 /*
358 * Looks up and returns the value corresponding to the capability
359 * (KVM_CAP_*) given by cap.
360 */
vm_check_cap(struct kvm_vm * vm,long cap)361 static inline int vm_check_cap(struct kvm_vm *vm, long cap)
362 {
363 int ret = __vm_ioctl(vm, KVM_CHECK_EXTENSION, (void *)cap);
364
365 TEST_ASSERT_VM_VCPU_IOCTL(ret >= 0, KVM_CHECK_EXTENSION, ret, vm);
366 return ret;
367 }
368
__vm_enable_cap(struct kvm_vm * vm,uint32_t cap,uint64_t arg0)369 static inline int __vm_enable_cap(struct kvm_vm *vm, uint32_t cap, uint64_t arg0)
370 {
371 struct kvm_enable_cap enable_cap = { .cap = cap, .args = { arg0 } };
372
373 return __vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap);
374 }
vm_enable_cap(struct kvm_vm * vm,uint32_t cap,uint64_t arg0)375 static inline void vm_enable_cap(struct kvm_vm *vm, uint32_t cap, uint64_t arg0)
376 {
377 struct kvm_enable_cap enable_cap = { .cap = cap, .args = { arg0 } };
378
379 vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap);
380 }
381
vm_set_memory_attributes(struct kvm_vm * vm,uint64_t gpa,uint64_t size,uint64_t attributes)382 static inline void vm_set_memory_attributes(struct kvm_vm *vm, uint64_t gpa,
383 uint64_t size, uint64_t attributes)
384 {
385 struct kvm_memory_attributes attr = {
386 .attributes = attributes,
387 .address = gpa,
388 .size = size,
389 .flags = 0,
390 };
391
392 /*
393 * KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These flows
394 * need significant enhancements to support multiple attributes.
395 */
396 TEST_ASSERT(!attributes || attributes == KVM_MEMORY_ATTRIBUTE_PRIVATE,
397 "Update me to support multiple attributes!");
398
399 vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES, &attr);
400 }
401
402
vm_mem_set_private(struct kvm_vm * vm,uint64_t gpa,uint64_t size)403 static inline void vm_mem_set_private(struct kvm_vm *vm, uint64_t gpa,
404 uint64_t size)
405 {
406 vm_set_memory_attributes(vm, gpa, size, KVM_MEMORY_ATTRIBUTE_PRIVATE);
407 }
408
vm_mem_set_shared(struct kvm_vm * vm,uint64_t gpa,uint64_t size)409 static inline void vm_mem_set_shared(struct kvm_vm *vm, uint64_t gpa,
410 uint64_t size)
411 {
412 vm_set_memory_attributes(vm, gpa, size, 0);
413 }
414
415 void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t gpa, uint64_t size,
416 bool punch_hole);
417
vm_guest_mem_punch_hole(struct kvm_vm * vm,uint64_t gpa,uint64_t size)418 static inline void vm_guest_mem_punch_hole(struct kvm_vm *vm, uint64_t gpa,
419 uint64_t size)
420 {
421 vm_guest_mem_fallocate(vm, gpa, size, true);
422 }
423
vm_guest_mem_allocate(struct kvm_vm * vm,uint64_t gpa,uint64_t size)424 static inline void vm_guest_mem_allocate(struct kvm_vm *vm, uint64_t gpa,
425 uint64_t size)
426 {
427 vm_guest_mem_fallocate(vm, gpa, size, false);
428 }
429
430 void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size);
431 const char *vm_guest_mode_string(uint32_t i);
432
433 void kvm_vm_free(struct kvm_vm *vmp);
434 void kvm_vm_restart(struct kvm_vm *vmp);
435 void kvm_vm_release(struct kvm_vm *vmp);
436 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, const vm_vaddr_t gva,
437 size_t len);
438 void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename);
439 int kvm_memfd_alloc(size_t size, bool hugepages);
440
441 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
442
kvm_vm_get_dirty_log(struct kvm_vm * vm,int slot,void * log)443 static inline void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log)
444 {
445 struct kvm_dirty_log args = { .dirty_bitmap = log, .slot = slot };
446
447 vm_ioctl(vm, KVM_GET_DIRTY_LOG, &args);
448 }
449
kvm_vm_clear_dirty_log(struct kvm_vm * vm,int slot,void * log,uint64_t first_page,uint32_t num_pages)450 static inline void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log,
451 uint64_t first_page, uint32_t num_pages)
452 {
453 struct kvm_clear_dirty_log args = {
454 .dirty_bitmap = log,
455 .slot = slot,
456 .first_page = first_page,
457 .num_pages = num_pages
458 };
459
460 vm_ioctl(vm, KVM_CLEAR_DIRTY_LOG, &args);
461 }
462
kvm_vm_reset_dirty_ring(struct kvm_vm * vm)463 static inline uint32_t kvm_vm_reset_dirty_ring(struct kvm_vm *vm)
464 {
465 return __vm_ioctl(vm, KVM_RESET_DIRTY_RINGS, NULL);
466 }
467
vm_get_stats_fd(struct kvm_vm * vm)468 static inline int vm_get_stats_fd(struct kvm_vm *vm)
469 {
470 int fd = __vm_ioctl(vm, KVM_GET_STATS_FD, NULL);
471
472 TEST_ASSERT_VM_VCPU_IOCTL(fd >= 0, KVM_GET_STATS_FD, fd, vm);
473 return fd;
474 }
475
read_stats_header(int stats_fd,struct kvm_stats_header * header)476 static inline void read_stats_header(int stats_fd, struct kvm_stats_header *header)
477 {
478 ssize_t ret;
479
480 ret = pread(stats_fd, header, sizeof(*header), 0);
481 TEST_ASSERT(ret == sizeof(*header),
482 "Failed to read '%lu' header bytes, ret = '%ld'",
483 sizeof(*header), ret);
484 }
485
486 struct kvm_stats_desc *read_stats_descriptors(int stats_fd,
487 struct kvm_stats_header *header);
488
get_stats_descriptor_size(struct kvm_stats_header * header)489 static inline ssize_t get_stats_descriptor_size(struct kvm_stats_header *header)
490 {
491 /*
492 * The base size of the descriptor is defined by KVM's ABI, but the
493 * size of the name field is variable, as far as KVM's ABI is
494 * concerned. For a given instance of KVM, the name field is the same
495 * size for all stats and is provided in the overall stats header.
496 */
497 return sizeof(struct kvm_stats_desc) + header->name_size;
498 }
499
get_stats_descriptor(struct kvm_stats_desc * stats,int index,struct kvm_stats_header * header)500 static inline struct kvm_stats_desc *get_stats_descriptor(struct kvm_stats_desc *stats,
501 int index,
502 struct kvm_stats_header *header)
503 {
504 /*
505 * Note, size_desc includes the size of the name field, which is
506 * variable. i.e. this is NOT equivalent to &stats_desc[i].
507 */
508 return (void *)stats + index * get_stats_descriptor_size(header);
509 }
510
511 void read_stat_data(int stats_fd, struct kvm_stats_header *header,
512 struct kvm_stats_desc *desc, uint64_t *data,
513 size_t max_elements);
514
515 void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
516 size_t max_elements);
517
vm_get_stat(struct kvm_vm * vm,const char * stat_name)518 static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
519 {
520 uint64_t data;
521
522 __vm_get_stat(vm, stat_name, &data, 1);
523 return data;
524 }
525
526 void vm_create_irqchip(struct kvm_vm *vm);
527
__vm_create_guest_memfd(struct kvm_vm * vm,uint64_t size,uint64_t flags)528 static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
529 uint64_t flags)
530 {
531 struct kvm_create_guest_memfd guest_memfd = {
532 .size = size,
533 .flags = flags,
534 };
535
536 return __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &guest_memfd);
537 }
538
vm_create_guest_memfd(struct kvm_vm * vm,uint64_t size,uint64_t flags)539 static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
540 uint64_t flags)
541 {
542 int fd = __vm_create_guest_memfd(vm, size, flags);
543
544 TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_GUEST_MEMFD, fd));
545 return fd;
546 }
547
548 void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
549 uint64_t gpa, uint64_t size, void *hva);
550 int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
551 uint64_t gpa, uint64_t size, void *hva);
552 void vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
553 uint64_t gpa, uint64_t size, void *hva,
554 uint32_t guest_memfd, uint64_t guest_memfd_offset);
555 int __vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
556 uint64_t gpa, uint64_t size, void *hva,
557 uint32_t guest_memfd, uint64_t guest_memfd_offset);
558
559 void vm_userspace_mem_region_add(struct kvm_vm *vm,
560 enum vm_mem_backing_src_type src_type,
561 uint64_t guest_paddr, uint32_t slot, uint64_t npages,
562 uint32_t flags);
563 void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
564 uint64_t guest_paddr, uint32_t slot, uint64_t npages,
565 uint32_t flags, int guest_memfd_fd, uint64_t guest_memfd_offset);
566
567 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags);
568 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
569 void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
570 struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id);
571 void vm_populate_vaddr_bitmap(struct kvm_vm *vm);
572 vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
573 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
574 vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
575 enum kvm_mem_region_type type);
576 vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages);
577 vm_vaddr_t __vm_vaddr_alloc_page(struct kvm_vm *vm,
578 enum kvm_mem_region_type type);
579 vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm);
580
581 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
582 unsigned int npages);
583 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa);
584 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);
585 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva);
586 void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa);
587
588 void vcpu_run(struct kvm_vcpu *vcpu);
589 int _vcpu_run(struct kvm_vcpu *vcpu);
590
__vcpu_run(struct kvm_vcpu * vcpu)591 static inline int __vcpu_run(struct kvm_vcpu *vcpu)
592 {
593 return __vcpu_ioctl(vcpu, KVM_RUN, NULL);
594 }
595
596 void vcpu_run_complete_io(struct kvm_vcpu *vcpu);
597 struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vcpu *vcpu);
598
vcpu_enable_cap(struct kvm_vcpu * vcpu,uint32_t cap,uint64_t arg0)599 static inline void vcpu_enable_cap(struct kvm_vcpu *vcpu, uint32_t cap,
600 uint64_t arg0)
601 {
602 struct kvm_enable_cap enable_cap = { .cap = cap, .args = { arg0 } };
603
604 vcpu_ioctl(vcpu, KVM_ENABLE_CAP, &enable_cap);
605 }
606
vcpu_guest_debug_set(struct kvm_vcpu * vcpu,struct kvm_guest_debug * debug)607 static inline void vcpu_guest_debug_set(struct kvm_vcpu *vcpu,
608 struct kvm_guest_debug *debug)
609 {
610 vcpu_ioctl(vcpu, KVM_SET_GUEST_DEBUG, debug);
611 }
612
vcpu_mp_state_get(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)613 static inline void vcpu_mp_state_get(struct kvm_vcpu *vcpu,
614 struct kvm_mp_state *mp_state)
615 {
616 vcpu_ioctl(vcpu, KVM_GET_MP_STATE, mp_state);
617 }
vcpu_mp_state_set(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)618 static inline void vcpu_mp_state_set(struct kvm_vcpu *vcpu,
619 struct kvm_mp_state *mp_state)
620 {
621 vcpu_ioctl(vcpu, KVM_SET_MP_STATE, mp_state);
622 }
623
vcpu_regs_get(struct kvm_vcpu * vcpu,struct kvm_regs * regs)624 static inline void vcpu_regs_get(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
625 {
626 vcpu_ioctl(vcpu, KVM_GET_REGS, regs);
627 }
628
vcpu_regs_set(struct kvm_vcpu * vcpu,struct kvm_regs * regs)629 static inline void vcpu_regs_set(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
630 {
631 vcpu_ioctl(vcpu, KVM_SET_REGS, regs);
632 }
vcpu_sregs_get(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)633 static inline void vcpu_sregs_get(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
634 {
635 vcpu_ioctl(vcpu, KVM_GET_SREGS, sregs);
636
637 }
vcpu_sregs_set(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)638 static inline void vcpu_sregs_set(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
639 {
640 vcpu_ioctl(vcpu, KVM_SET_SREGS, sregs);
641 }
_vcpu_sregs_set(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)642 static inline int _vcpu_sregs_set(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
643 {
644 return __vcpu_ioctl(vcpu, KVM_SET_SREGS, sregs);
645 }
vcpu_fpu_get(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)646 static inline void vcpu_fpu_get(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
647 {
648 vcpu_ioctl(vcpu, KVM_GET_FPU, fpu);
649 }
vcpu_fpu_set(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)650 static inline void vcpu_fpu_set(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
651 {
652 vcpu_ioctl(vcpu, KVM_SET_FPU, fpu);
653 }
654
__vcpu_get_reg(struct kvm_vcpu * vcpu,uint64_t id,void * addr)655 static inline int __vcpu_get_reg(struct kvm_vcpu *vcpu, uint64_t id, void *addr)
656 {
657 struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)addr };
658
659 return __vcpu_ioctl(vcpu, KVM_GET_ONE_REG, ®);
660 }
__vcpu_set_reg(struct kvm_vcpu * vcpu,uint64_t id,uint64_t val)661 static inline int __vcpu_set_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val)
662 {
663 struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val };
664
665 return __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®);
666 }
vcpu_get_reg(struct kvm_vcpu * vcpu,uint64_t id,void * addr)667 static inline void vcpu_get_reg(struct kvm_vcpu *vcpu, uint64_t id, void *addr)
668 {
669 struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)addr };
670
671 vcpu_ioctl(vcpu, KVM_GET_ONE_REG, ®);
672 }
vcpu_set_reg(struct kvm_vcpu * vcpu,uint64_t id,uint64_t val)673 static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val)
674 {
675 struct kvm_one_reg reg = { .id = id, .addr = (uint64_t)&val };
676
677 vcpu_ioctl(vcpu, KVM_SET_ONE_REG, ®);
678 }
679
680 #ifdef __KVM_HAVE_VCPU_EVENTS
vcpu_events_get(struct kvm_vcpu * vcpu,struct kvm_vcpu_events * events)681 static inline void vcpu_events_get(struct kvm_vcpu *vcpu,
682 struct kvm_vcpu_events *events)
683 {
684 vcpu_ioctl(vcpu, KVM_GET_VCPU_EVENTS, events);
685 }
vcpu_events_set(struct kvm_vcpu * vcpu,struct kvm_vcpu_events * events)686 static inline void vcpu_events_set(struct kvm_vcpu *vcpu,
687 struct kvm_vcpu_events *events)
688 {
689 vcpu_ioctl(vcpu, KVM_SET_VCPU_EVENTS, events);
690 }
691 #endif
692 #ifdef __x86_64__
vcpu_nested_state_get(struct kvm_vcpu * vcpu,struct kvm_nested_state * state)693 static inline void vcpu_nested_state_get(struct kvm_vcpu *vcpu,
694 struct kvm_nested_state *state)
695 {
696 vcpu_ioctl(vcpu, KVM_GET_NESTED_STATE, state);
697 }
__vcpu_nested_state_set(struct kvm_vcpu * vcpu,struct kvm_nested_state * state)698 static inline int __vcpu_nested_state_set(struct kvm_vcpu *vcpu,
699 struct kvm_nested_state *state)
700 {
701 return __vcpu_ioctl(vcpu, KVM_SET_NESTED_STATE, state);
702 }
703
vcpu_nested_state_set(struct kvm_vcpu * vcpu,struct kvm_nested_state * state)704 static inline void vcpu_nested_state_set(struct kvm_vcpu *vcpu,
705 struct kvm_nested_state *state)
706 {
707 vcpu_ioctl(vcpu, KVM_SET_NESTED_STATE, state);
708 }
709 #endif
vcpu_get_stats_fd(struct kvm_vcpu * vcpu)710 static inline int vcpu_get_stats_fd(struct kvm_vcpu *vcpu)
711 {
712 int fd = __vcpu_ioctl(vcpu, KVM_GET_STATS_FD, NULL);
713
714 TEST_ASSERT_VM_VCPU_IOCTL(fd >= 0, KVM_CHECK_EXTENSION, fd, vcpu->vm);
715 return fd;
716 }
717
718 int __kvm_has_device_attr(int dev_fd, uint32_t group, uint64_t attr);
719
kvm_has_device_attr(int dev_fd,uint32_t group,uint64_t attr)720 static inline void kvm_has_device_attr(int dev_fd, uint32_t group, uint64_t attr)
721 {
722 int ret = __kvm_has_device_attr(dev_fd, group, attr);
723
724 TEST_ASSERT(!ret, "KVM_HAS_DEVICE_ATTR failed, rc: %i errno: %i", ret, errno);
725 }
726
727 int __kvm_device_attr_get(int dev_fd, uint32_t group, uint64_t attr, void *val);
728
kvm_device_attr_get(int dev_fd,uint32_t group,uint64_t attr,void * val)729 static inline void kvm_device_attr_get(int dev_fd, uint32_t group,
730 uint64_t attr, void *val)
731 {
732 int ret = __kvm_device_attr_get(dev_fd, group, attr, val);
733
734 TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_GET_DEVICE_ATTR, ret));
735 }
736
737 int __kvm_device_attr_set(int dev_fd, uint32_t group, uint64_t attr, void *val);
738
kvm_device_attr_set(int dev_fd,uint32_t group,uint64_t attr,void * val)739 static inline void kvm_device_attr_set(int dev_fd, uint32_t group,
740 uint64_t attr, void *val)
741 {
742 int ret = __kvm_device_attr_set(dev_fd, group, attr, val);
743
744 TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret));
745 }
746
__vcpu_has_device_attr(struct kvm_vcpu * vcpu,uint32_t group,uint64_t attr)747 static inline int __vcpu_has_device_attr(struct kvm_vcpu *vcpu, uint32_t group,
748 uint64_t attr)
749 {
750 return __kvm_has_device_attr(vcpu->fd, group, attr);
751 }
752
vcpu_has_device_attr(struct kvm_vcpu * vcpu,uint32_t group,uint64_t attr)753 static inline void vcpu_has_device_attr(struct kvm_vcpu *vcpu, uint32_t group,
754 uint64_t attr)
755 {
756 kvm_has_device_attr(vcpu->fd, group, attr);
757 }
758
__vcpu_device_attr_get(struct kvm_vcpu * vcpu,uint32_t group,uint64_t attr,void * val)759 static inline int __vcpu_device_attr_get(struct kvm_vcpu *vcpu, uint32_t group,
760 uint64_t attr, void *val)
761 {
762 return __kvm_device_attr_get(vcpu->fd, group, attr, val);
763 }
764
vcpu_device_attr_get(struct kvm_vcpu * vcpu,uint32_t group,uint64_t attr,void * val)765 static inline void vcpu_device_attr_get(struct kvm_vcpu *vcpu, uint32_t group,
766 uint64_t attr, void *val)
767 {
768 kvm_device_attr_get(vcpu->fd, group, attr, val);
769 }
770
__vcpu_device_attr_set(struct kvm_vcpu * vcpu,uint32_t group,uint64_t attr,void * val)771 static inline int __vcpu_device_attr_set(struct kvm_vcpu *vcpu, uint32_t group,
772 uint64_t attr, void *val)
773 {
774 return __kvm_device_attr_set(vcpu->fd, group, attr, val);
775 }
776
vcpu_device_attr_set(struct kvm_vcpu * vcpu,uint32_t group,uint64_t attr,void * val)777 static inline void vcpu_device_attr_set(struct kvm_vcpu *vcpu, uint32_t group,
778 uint64_t attr, void *val)
779 {
780 kvm_device_attr_set(vcpu->fd, group, attr, val);
781 }
782
783 int __kvm_test_create_device(struct kvm_vm *vm, uint64_t type);
784 int __kvm_create_device(struct kvm_vm *vm, uint64_t type);
785
kvm_create_device(struct kvm_vm * vm,uint64_t type)786 static inline int kvm_create_device(struct kvm_vm *vm, uint64_t type)
787 {
788 int fd = __kvm_create_device(vm, type);
789
790 TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_DEVICE, fd));
791 return fd;
792 }
793
794 void *vcpu_map_dirty_ring(struct kvm_vcpu *vcpu);
795
796 /*
797 * VM VCPU Args Set
798 *
799 * Input Args:
800 * vm - Virtual Machine
801 * num - number of arguments
802 * ... - arguments, each of type uint64_t
803 *
804 * Output Args: None
805 *
806 * Return: None
807 *
808 * Sets the first @num input parameters for the function at @vcpu's entry point,
809 * per the C calling convention of the architecture, to the values given as
810 * variable args. Each of the variable args is expected to be of type uint64_t.
811 * The maximum @num can be is specific to the architecture.
812 */
813 void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...);
814
815 void kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level);
816 int _kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level);
817
818 #define KVM_MAX_IRQ_ROUTES 4096
819
820 struct kvm_irq_routing *kvm_gsi_routing_create(void);
821 void kvm_gsi_routing_irqchip_add(struct kvm_irq_routing *routing,
822 uint32_t gsi, uint32_t pin);
823 int _kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);
824 void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);
825
826 const char *exit_reason_str(unsigned int exit_reason);
827
828 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
829 uint32_t memslot);
830 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
831 vm_paddr_t paddr_min, uint32_t memslot);
832 vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm);
833
834 /*
835 * ____vm_create() does KVM_CREATE_VM and little else. __vm_create() also
836 * loads the test binary into guest memory and creates an IRQ chip (x86 only).
837 * __vm_create() does NOT create vCPUs, @nr_runnable_vcpus is used purely to
838 * calculate the amount of memory needed for per-vCPU data, e.g. stacks.
839 */
840 struct kvm_vm *____vm_create(struct vm_shape shape);
841 struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
842 uint64_t nr_extra_pages);
843
vm_create_barebones(void)844 static inline struct kvm_vm *vm_create_barebones(void)
845 {
846 return ____vm_create(VM_SHAPE_DEFAULT);
847 }
848
849 #ifdef __x86_64__
vm_create_barebones_protected_vm(void)850 static inline struct kvm_vm *vm_create_barebones_protected_vm(void)
851 {
852 const struct vm_shape shape = {
853 .mode = VM_MODE_DEFAULT,
854 .type = KVM_X86_SW_PROTECTED_VM,
855 };
856
857 return ____vm_create(shape);
858 }
859 #endif
860
vm_create(uint32_t nr_runnable_vcpus)861 static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus)
862 {
863 return __vm_create(VM_SHAPE_DEFAULT, nr_runnable_vcpus, 0);
864 }
865
866 struct kvm_vm *__vm_create_with_vcpus(struct vm_shape shape, uint32_t nr_vcpus,
867 uint64_t extra_mem_pages,
868 void *guest_code, struct kvm_vcpu *vcpus[]);
869
vm_create_with_vcpus(uint32_t nr_vcpus,void * guest_code,struct kvm_vcpu * vcpus[])870 static inline struct kvm_vm *vm_create_with_vcpus(uint32_t nr_vcpus,
871 void *guest_code,
872 struct kvm_vcpu *vcpus[])
873 {
874 return __vm_create_with_vcpus(VM_SHAPE_DEFAULT, nr_vcpus, 0,
875 guest_code, vcpus);
876 }
877
878
879 struct kvm_vm *__vm_create_shape_with_one_vcpu(struct vm_shape shape,
880 struct kvm_vcpu **vcpu,
881 uint64_t extra_mem_pages,
882 void *guest_code);
883
884 /*
885 * Create a VM with a single vCPU with reasonable defaults and @extra_mem_pages
886 * additional pages of guest memory. Returns the VM and vCPU (via out param).
887 */
__vm_create_with_one_vcpu(struct kvm_vcpu ** vcpu,uint64_t extra_mem_pages,void * guest_code)888 static inline struct kvm_vm *__vm_create_with_one_vcpu(struct kvm_vcpu **vcpu,
889 uint64_t extra_mem_pages,
890 void *guest_code)
891 {
892 return __vm_create_shape_with_one_vcpu(VM_SHAPE_DEFAULT, vcpu,
893 extra_mem_pages, guest_code);
894 }
895
vm_create_with_one_vcpu(struct kvm_vcpu ** vcpu,void * guest_code)896 static inline struct kvm_vm *vm_create_with_one_vcpu(struct kvm_vcpu **vcpu,
897 void *guest_code)
898 {
899 return __vm_create_with_one_vcpu(vcpu, 0, guest_code);
900 }
901
vm_create_shape_with_one_vcpu(struct vm_shape shape,struct kvm_vcpu ** vcpu,void * guest_code)902 static inline struct kvm_vm *vm_create_shape_with_one_vcpu(struct vm_shape shape,
903 struct kvm_vcpu **vcpu,
904 void *guest_code)
905 {
906 return __vm_create_shape_with_one_vcpu(shape, vcpu, 0, guest_code);
907 }
908
909 struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm);
910
911 void kvm_pin_this_task_to_pcpu(uint32_t pcpu);
912 void kvm_print_vcpu_pinning_help(void);
913 void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
914 int nr_vcpus);
915
916 unsigned long vm_compute_max_gfn(struct kvm_vm *vm);
917 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size);
918 unsigned int vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages);
919 unsigned int vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages);
920 static inline unsigned int
vm_adjust_num_guest_pages(enum vm_guest_mode mode,unsigned int num_guest_pages)921 vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
922 {
923 unsigned int n;
924 n = vm_num_guest_pages(mode, vm_num_host_pages(mode, num_guest_pages));
925 #ifdef __s390x__
926 /* s390 requires 1M aligned guest sizes */
927 n = (n + 255) & ~255;
928 #endif
929 return n;
930 }
931
932 #define sync_global_to_guest(vm, g) ({ \
933 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
934 memcpy(_p, &(g), sizeof(g)); \
935 })
936
937 #define sync_global_from_guest(vm, g) ({ \
938 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
939 memcpy(&(g), _p, sizeof(g)); \
940 })
941
942 /*
943 * Write a global value, but only in the VM's (guest's) domain. Primarily used
944 * for "globals" that hold per-VM values (VMs always duplicate code and global
945 * data into their own region of physical memory), but can be used anytime it's
946 * undesirable to change the host's copy of the global.
947 */
948 #define write_guest_global(vm, g, val) ({ \
949 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
950 typeof(g) _val = val; \
951 \
952 memcpy(_p, &(_val), sizeof(g)); \
953 })
954
955 void assert_on_unhandled_exception(struct kvm_vcpu *vcpu);
956
957 void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu,
958 uint8_t indent);
959
vcpu_dump(FILE * stream,struct kvm_vcpu * vcpu,uint8_t indent)960 static inline void vcpu_dump(FILE *stream, struct kvm_vcpu *vcpu,
961 uint8_t indent)
962 {
963 vcpu_arch_dump(stream, vcpu, indent);
964 }
965
966 /*
967 * Adds a vCPU with reasonable defaults (e.g. a stack)
968 *
969 * Input Args:
970 * vm - Virtual Machine
971 * vcpu_id - The id of the VCPU to add to the VM.
972 * guest_code - The vCPU's entry point
973 */
974 struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
975 void *guest_code);
976
vm_vcpu_add(struct kvm_vm * vm,uint32_t vcpu_id,void * guest_code)977 static inline struct kvm_vcpu *vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
978 void *guest_code)
979 {
980 return vm_arch_vcpu_add(vm, vcpu_id, guest_code);
981 }
982
983 /* Re-create a vCPU after restarting a VM, e.g. for state save/restore tests. */
984 struct kvm_vcpu *vm_arch_vcpu_recreate(struct kvm_vm *vm, uint32_t vcpu_id);
985
vm_vcpu_recreate(struct kvm_vm * vm,uint32_t vcpu_id)986 static inline struct kvm_vcpu *vm_vcpu_recreate(struct kvm_vm *vm,
987 uint32_t vcpu_id)
988 {
989 return vm_arch_vcpu_recreate(vm, vcpu_id);
990 }
991
992 void vcpu_arch_free(struct kvm_vcpu *vcpu);
993
994 void virt_arch_pgd_alloc(struct kvm_vm *vm);
995
virt_pgd_alloc(struct kvm_vm * vm)996 static inline void virt_pgd_alloc(struct kvm_vm *vm)
997 {
998 virt_arch_pgd_alloc(vm);
999 }
1000
1001 /*
1002 * VM Virtual Page Map
1003 *
1004 * Input Args:
1005 * vm - Virtual Machine
1006 * vaddr - VM Virtual Address
1007 * paddr - VM Physical Address
1008 * memslot - Memory region slot for new virtual translation tables
1009 *
1010 * Output Args: None
1011 *
1012 * Return: None
1013 *
1014 * Within @vm, creates a virtual translation for the page starting
1015 * at @vaddr to the page starting at @paddr.
1016 */
1017 void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr);
1018
virt_pg_map(struct kvm_vm * vm,uint64_t vaddr,uint64_t paddr)1019 static inline void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
1020 {
1021 virt_arch_pg_map(vm, vaddr, paddr);
1022 }
1023
1024
1025 /*
1026 * Address Guest Virtual to Guest Physical
1027 *
1028 * Input Args:
1029 * vm - Virtual Machine
1030 * gva - VM virtual address
1031 *
1032 * Output Args: None
1033 *
1034 * Return:
1035 * Equivalent VM physical address
1036 *
1037 * Returns the VM physical address of the translated VM virtual
1038 * address given by @gva.
1039 */
1040 vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva);
1041
addr_gva2gpa(struct kvm_vm * vm,vm_vaddr_t gva)1042 static inline vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)
1043 {
1044 return addr_arch_gva2gpa(vm, gva);
1045 }
1046
1047 /*
1048 * Virtual Translation Tables Dump
1049 *
1050 * Input Args:
1051 * stream - Output FILE stream
1052 * vm - Virtual Machine
1053 * indent - Left margin indent amount
1054 *
1055 * Output Args: None
1056 *
1057 * Return: None
1058 *
1059 * Dumps to the FILE stream given by @stream, the contents of all the
1060 * virtual translation tables for the VM given by @vm.
1061 */
1062 void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
1063
virt_dump(FILE * stream,struct kvm_vm * vm,uint8_t indent)1064 static inline void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1065 {
1066 virt_arch_dump(stream, vm, indent);
1067 }
1068
1069
__vm_disable_nx_huge_pages(struct kvm_vm * vm)1070 static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)
1071 {
1072 return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0);
1073 }
1074
1075 /*
1076 * Arch hook that is invoked via a constructor, i.e. before exeucting main(),
1077 * to allow for arch-specific setup that is common to all tests, e.g. computing
1078 * the default guest "mode".
1079 */
1080 void kvm_selftest_arch_init(void);
1081
1082 void kvm_arch_vm_post_create(struct kvm_vm *vm);
1083
1084 #endif /* SELFTEST_KVM_UTIL_BASE_H */
1085