• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * tools/testing/selftests/kvm/include/kvm_util.h
3  *
4  * Copyright (C) 2018, Google LLC.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.
7  *
8  */
9 #ifndef SELFTEST_KVM_UTIL_H
10 #define SELFTEST_KVM_UTIL_H
11 
12 #include "test_util.h"
13 
14 #include "asm/kvm.h"
15 #include "linux/kvm.h"
16 #include <sys/ioctl.h>
17 
18 #include "sparsebit.h"
19 
20 
21 /* Callers of kvm_util only have an incomplete/opaque description of the
22  * structure kvm_util is using to maintain the state of a VM.
23  */
24 struct kvm_vm;
25 
26 typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */
27 typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */
28 
29 /* Minimum allocated guest virtual and physical addresses */
30 #define KVM_UTIL_MIN_VADDR		0x2000
31 
32 #define DEFAULT_GUEST_PHY_PAGES		512
33 #define DEFAULT_GUEST_STACK_VADDR_MIN	0xab6000
34 #define DEFAULT_STACK_PGS		5
35 
36 enum vm_guest_mode {
37 	VM_MODE_P52V48_4K,
38 	VM_MODE_P52V48_64K,
39 	VM_MODE_P40V48_4K,
40 	VM_MODE_P40V48_64K,
41 	NUM_VM_MODES,
42 };
43 
44 #define vm_guest_mode_string(m) vm_guest_mode_string[m]
45 extern const char * const vm_guest_mode_string[];
46 
47 enum vm_mem_backing_src_type {
48 	VM_MEM_SRC_ANONYMOUS,
49 	VM_MEM_SRC_ANONYMOUS_THP,
50 	VM_MEM_SRC_ANONYMOUS_HUGETLB,
51 };
52 
53 int kvm_check_cap(long cap);
54 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap);
55 
56 struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm);
57 void kvm_vm_free(struct kvm_vm *vmp);
58 void kvm_vm_restart(struct kvm_vm *vmp, int perm);
59 void kvm_vm_release(struct kvm_vm *vmp);
60 void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log);
61 
62 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, const vm_vaddr_t gva,
63 		       size_t len);
64 
65 void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename,
66 		     uint32_t data_memslot, uint32_t pgd_memslot);
67 
68 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
69 void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid,
70 	       uint8_t indent);
71 
72 void vm_create_irqchip(struct kvm_vm *vm);
73 
74 void vm_userspace_mem_region_add(struct kvm_vm *vm,
75 	enum vm_mem_backing_src_type src_type,
76 	uint64_t guest_paddr, uint32_t slot, uint64_t npages,
77 	uint32_t flags);
78 
79 void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl,
80 		void *arg);
81 void vm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
82 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags);
83 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, int pgd_memslot,
84 		 int gdt_memslot);
85 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
86 			  uint32_t data_memslot, uint32_t pgd_memslot);
87 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
88 	      size_t size, uint32_t pgd_memslot);
89 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa);
90 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);
91 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva);
92 vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva);
93 
94 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid);
95 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid);
96 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid);
97 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
98 		       struct kvm_mp_state *mp_state);
99 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs);
100 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs);
101 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...);
102 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid,
103 		    struct kvm_sregs *sregs);
104 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid,
105 		    struct kvm_sregs *sregs);
106 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid,
107 		    struct kvm_sregs *sregs);
108 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
109 		     struct kvm_vcpu_events *events);
110 void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
111 		     struct kvm_vcpu_events *events);
112 
113 const char *exit_reason_str(unsigned int exit_reason);
114 
115 void virt_pgd_alloc(struct kvm_vm *vm, uint32_t pgd_memslot);
116 void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
117 		 uint32_t pgd_memslot);
118 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
119 			     uint32_t memslot);
120 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
121 			      vm_paddr_t paddr_min, uint32_t memslot);
122 
123 struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_size,
124 				 void *guest_code);
125 void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code);
126 
127 struct kvm_userspace_memory_region *
128 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
129 				 uint64_t end);
130 
131 struct kvm_dirty_log *
132 allocate_kvm_dirty_log(struct kvm_userspace_memory_region *region);
133 
134 int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd);
135 
136 #define sync_global_to_guest(vm, g) ({				\
137 	typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g));	\
138 	memcpy(_p, &(g), sizeof(g));				\
139 })
140 
141 #define sync_global_from_guest(vm, g) ({			\
142 	typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g));	\
143 	memcpy(&(g), _p, sizeof(g));				\
144 })
145 
146 /* ucall implementation types */
147 typedef enum {
148 	UCALL_PIO,
149 	UCALL_MMIO,
150 } ucall_type_t;
151 
152 /* Common ucalls */
153 enum {
154 	UCALL_NONE,
155 	UCALL_SYNC,
156 	UCALL_ABORT,
157 	UCALL_DONE,
158 };
159 
160 #define UCALL_MAX_ARGS 6
161 
162 struct ucall {
163 	uint64_t cmd;
164 	uint64_t args[UCALL_MAX_ARGS];
165 };
166 
167 void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg);
168 void ucall_uninit(struct kvm_vm *vm);
169 void ucall(uint64_t cmd, int nargs, ...);
170 uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);
171 
172 #define GUEST_SYNC(stage)	ucall(UCALL_SYNC, 2, "hello", stage)
173 #define GUEST_DONE()		ucall(UCALL_DONE, 0)
174 #define GUEST_ASSERT(_condition) do {			\
175 	if (!(_condition))				\
176 		ucall(UCALL_ABORT, 2,			\
177 			"Failed guest assert: "		\
178 			#_condition, __LINE__);		\
179 } while (0)
180 
181 #endif /* SELFTEST_KVM_UTIL_H */
182