2 * tools/testing/selftests/kvm/include/kvm_util.h
4 * Copyright (C) 2018, Google LLC.
6 * This work is licensed under the terms of the GNU GPL, version 2.
9 #ifndef SELFTEST_KVM_UTIL_H
10 #define SELFTEST_KVM_UTIL_H
12 #include "test_util.h"
15 #include "linux/kvm.h"
16 #include <sys/ioctl.h>
18 #include "sparsebit.h"
21 /* Callers of kvm_util only have an incomplete/opaque description of the
22 * structure kvm_util is using to maintain the state of a VM.
26 typedef uint64_t vm_paddr_t
; /* Virtual Machine (Guest) physical address */
27 typedef uint64_t vm_vaddr_t
; /* Virtual Machine (Guest) virtual address */
29 /* Minimum allocated guest virtual and physical addresses */
30 #define KVM_UTIL_MIN_VADDR 0x2000
32 #define DEFAULT_GUEST_PHY_PAGES 512
33 #define DEFAULT_GUEST_STACK_VADDR_MIN 0xab6000
34 #define DEFAULT_STACK_PGS 5
46 #define vm_guest_mode_string(m) vm_guest_mode_string[m]
47 extern const char * const vm_guest_mode_string
[];
49 enum vm_mem_backing_src_type
{
51 VM_MEM_SRC_ANONYMOUS_THP
,
52 VM_MEM_SRC_ANONYMOUS_HUGETLB
,
55 int kvm_check_cap(long cap
);
56 int vm_enable_cap(struct kvm_vm
*vm
, struct kvm_enable_cap
*cap
);
58 struct kvm_vm
*vm_create(enum vm_guest_mode mode
, uint64_t phy_pages
, int perm
);
59 struct kvm_vm
*_vm_create(enum vm_guest_mode mode
, uint64_t phy_pages
,
60 int perm
, unsigned long type
);
61 void kvm_vm_free(struct kvm_vm
*vmp
);
62 void kvm_vm_restart(struct kvm_vm
*vmp
, int perm
);
63 void kvm_vm_release(struct kvm_vm
*vmp
);
64 void kvm_vm_get_dirty_log(struct kvm_vm
*vm
, int slot
, void *log
);
65 void kvm_vm_clear_dirty_log(struct kvm_vm
*vm
, int slot
, void *log
,
66 uint64_t first_page
, uint32_t num_pages
);
68 int kvm_memcmp_hva_gva(void *hva
, struct kvm_vm
*vm
, const vm_vaddr_t gva
,
71 void kvm_vm_elf_load(struct kvm_vm
*vm
, const char *filename
,
72 uint32_t data_memslot
, uint32_t pgd_memslot
);
74 void vm_dump(FILE *stream
, struct kvm_vm
*vm
, uint8_t indent
);
75 void vcpu_dump(FILE *stream
, struct kvm_vm
*vm
, uint32_t vcpuid
,
78 void vm_create_irqchip(struct kvm_vm
*vm
);
80 void vm_userspace_mem_region_add(struct kvm_vm
*vm
,
81 enum vm_mem_backing_src_type src_type
,
82 uint64_t guest_paddr
, uint32_t slot
, uint64_t npages
,
85 void vcpu_ioctl(struct kvm_vm
*vm
, uint32_t vcpuid
, unsigned long ioctl
,
87 int _vcpu_ioctl(struct kvm_vm
*vm
, uint32_t vcpuid
, unsigned long ioctl
,
89 void vm_ioctl(struct kvm_vm
*vm
, unsigned long ioctl
, void *arg
);
90 void vm_mem_region_set_flags(struct kvm_vm
*vm
, uint32_t slot
, uint32_t flags
);
91 void vm_vcpu_add(struct kvm_vm
*vm
, uint32_t vcpuid
, int pgd_memslot
,
93 vm_vaddr_t
vm_vaddr_alloc(struct kvm_vm
*vm
, size_t sz
, vm_vaddr_t vaddr_min
,
94 uint32_t data_memslot
, uint32_t pgd_memslot
);
95 void virt_map(struct kvm_vm
*vm
, uint64_t vaddr
, uint64_t paddr
,
96 size_t size
, uint32_t pgd_memslot
);
97 void *addr_gpa2hva(struct kvm_vm
*vm
, vm_paddr_t gpa
);
98 void *addr_gva2hva(struct kvm_vm
*vm
, vm_vaddr_t gva
);
99 vm_paddr_t
addr_hva2gpa(struct kvm_vm
*vm
, void *hva
);
100 vm_paddr_t
addr_gva2gpa(struct kvm_vm
*vm
, vm_vaddr_t gva
);
102 struct kvm_run
*vcpu_state(struct kvm_vm
*vm
, uint32_t vcpuid
);
103 void vcpu_run(struct kvm_vm
*vm
, uint32_t vcpuid
);
104 int _vcpu_run(struct kvm_vm
*vm
, uint32_t vcpuid
);
105 void vcpu_set_mp_state(struct kvm_vm
*vm
, uint32_t vcpuid
,
106 struct kvm_mp_state
*mp_state
);
107 void vcpu_regs_get(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_regs
*regs
);
108 void vcpu_regs_set(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_regs
*regs
);
109 void vcpu_args_set(struct kvm_vm
*vm
, uint32_t vcpuid
, unsigned int num
, ...);
110 void vcpu_sregs_get(struct kvm_vm
*vm
, uint32_t vcpuid
,
111 struct kvm_sregs
*sregs
);
112 void vcpu_sregs_set(struct kvm_vm
*vm
, uint32_t vcpuid
,
113 struct kvm_sregs
*sregs
);
114 int _vcpu_sregs_set(struct kvm_vm
*vm
, uint32_t vcpuid
,
115 struct kvm_sregs
*sregs
);
116 void vcpu_events_get(struct kvm_vm
*vm
, uint32_t vcpuid
,
117 struct kvm_vcpu_events
*events
);
118 void vcpu_events_set(struct kvm_vm
*vm
, uint32_t vcpuid
,
119 struct kvm_vcpu_events
*events
);
121 const char *exit_reason_str(unsigned int exit_reason
);
123 void virt_pgd_alloc(struct kvm_vm
*vm
, uint32_t pgd_memslot
);
124 void virt_pg_map(struct kvm_vm
*vm
, uint64_t vaddr
, uint64_t paddr
,
125 uint32_t pgd_memslot
);
126 vm_paddr_t
vm_phy_page_alloc(struct kvm_vm
*vm
, vm_paddr_t paddr_min
,
128 vm_paddr_t
vm_phy_pages_alloc(struct kvm_vm
*vm
, size_t num
,
129 vm_paddr_t paddr_min
, uint32_t memslot
);
131 struct kvm_vm
*vm_create_default(uint32_t vcpuid
, uint64_t extra_mem_size
,
133 void vm_vcpu_add_default(struct kvm_vm
*vm
, uint32_t vcpuid
, void *guest_code
);
135 struct kvm_userspace_memory_region
*
136 kvm_userspace_memory_region_find(struct kvm_vm
*vm
, uint64_t start
,
139 struct kvm_dirty_log
*
140 allocate_kvm_dirty_log(struct kvm_userspace_memory_region
*region
);
142 int vm_create_device(struct kvm_vm
*vm
, struct kvm_create_device
*cd
);
144 #define sync_global_to_guest(vm, g) ({ \
145 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
146 memcpy(_p, &(g), sizeof(g)); \
149 #define sync_global_from_guest(vm, g) ({ \
150 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
151 memcpy(&(g), _p, sizeof(g)); \
154 /* ucall implementation types */
168 #define UCALL_MAX_ARGS 6
172 uint64_t args
[UCALL_MAX_ARGS
];
175 void ucall_init(struct kvm_vm
*vm
, ucall_type_t type
, void *arg
);
176 void ucall_uninit(struct kvm_vm
*vm
);
177 void ucall(uint64_t cmd
, int nargs
, ...);
178 uint64_t get_ucall(struct kvm_vm
*vm
, uint32_t vcpu_id
, struct ucall
*uc
);
180 #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage)
181 #define GUEST_DONE() ucall(UCALL_DONE, 0)
182 #define GUEST_ASSERT(_condition) do { \
184 ucall(UCALL_ABORT, 2, \
185 "Failed guest assert: " \
186 #_condition, __LINE__); \
189 #endif /* SELFTEST_KVM_UTIL_H */