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