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