kvm_util.h 31 KB

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