kvm_util.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /*
  2. * tools/testing/selftests/kvm/lib/kvm_util.c
  3. *
  4. * Copyright (C) 2018, Google LLC.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2.
  7. */
  8. #include "test_util.h"
  9. #include "kvm_util.h"
  10. #include "kvm_util_internal.h"
  11. #include <assert.h>
  12. #include <sys/mman.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #define KVM_DEV_PATH "/dev/kvm"
  16. #define KVM_UTIL_PGS_PER_HUGEPG 512
  17. #define KVM_UTIL_MIN_PADDR 0x2000
  18. /* Aligns x up to the next multiple of size. Size must be a power of 2. */
  19. static void *align(void *x, size_t size)
  20. {
  21. size_t mask = size - 1;
  22. TEST_ASSERT(size != 0 && !(size & (size - 1)),
  23. "size not a power of 2: %lu", size);
  24. return (void *) (((size_t) x + mask) & ~mask);
  25. }
  26. /* Capability
  27. *
  28. * Input Args:
  29. * cap - Capability
  30. *
  31. * Output Args: None
  32. *
  33. * Return:
  34. * On success, the Value corresponding to the capability (KVM_CAP_*)
  35. * specified by the value of cap. On failure a TEST_ASSERT failure
  36. * is produced.
  37. *
  38. * Looks up and returns the value corresponding to the capability
  39. * (KVM_CAP_*) given by cap.
  40. */
  41. int kvm_check_cap(long cap)
  42. {
  43. int ret;
  44. int kvm_fd;
  45. kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
  46. if (kvm_fd < 0)
  47. exit(KSFT_SKIP);
  48. ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap);
  49. TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n"
  50. " rc: %i errno: %i", ret, errno);
  51. close(kvm_fd);
  52. return ret;
  53. }
  54. /* VM Create
  55. *
  56. * Input Args:
  57. * mode - VM Mode (e.g. VM_MODE_FLAT48PG)
  58. * phy_pages - Physical memory pages
  59. * perm - permission
  60. *
  61. * Output Args: None
  62. *
  63. * Return:
  64. * Pointer to opaque structure that describes the created VM.
  65. *
  66. * Creates a VM with the mode specified by mode (e.g. VM_MODE_FLAT48PG).
  67. * When phy_pages is non-zero, a memory region of phy_pages physical pages
  68. * is created and mapped starting at guest physical address 0. The file
  69. * descriptor to control the created VM is created with the permissions
  70. * given by perm (e.g. O_RDWR).
  71. */
  72. struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
  73. {
  74. struct kvm_vm *vm;
  75. int kvm_fd;
  76. /* Allocate memory. */
  77. vm = calloc(1, sizeof(*vm));
  78. TEST_ASSERT(vm != NULL, "Insufficent Memory");
  79. vm->mode = mode;
  80. kvm_fd = open(KVM_DEV_PATH, perm);
  81. if (kvm_fd < 0)
  82. exit(KSFT_SKIP);
  83. /* Create VM. */
  84. vm->fd = ioctl(kvm_fd, KVM_CREATE_VM, NULL);
  85. TEST_ASSERT(vm->fd >= 0, "KVM_CREATE_VM ioctl failed, "
  86. "rc: %i errno: %i", vm->fd, errno);
  87. close(kvm_fd);
  88. /* Setup mode specific traits. */
  89. switch (vm->mode) {
  90. case VM_MODE_FLAT48PG:
  91. vm->page_size = 0x1000;
  92. vm->page_shift = 12;
  93. /* Limit to 48-bit canonical virtual addresses. */
  94. vm->vpages_valid = sparsebit_alloc();
  95. sparsebit_set_num(vm->vpages_valid,
  96. 0, (1ULL << (48 - 1)) >> vm->page_shift);
  97. sparsebit_set_num(vm->vpages_valid,
  98. (~((1ULL << (48 - 1)) - 1)) >> vm->page_shift,
  99. (1ULL << (48 - 1)) >> vm->page_shift);
  100. /* Limit physical addresses to 52-bits. */
  101. vm->max_gfn = ((1ULL << 52) >> vm->page_shift) - 1;
  102. break;
  103. default:
  104. TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode);
  105. }
  106. /* Allocate and setup memory for guest. */
  107. vm->vpages_mapped = sparsebit_alloc();
  108. if (phy_pages != 0)
  109. vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
  110. 0, 0, phy_pages, 0);
  111. return vm;
  112. }
  113. /* Userspace Memory Region Find
  114. *
  115. * Input Args:
  116. * vm - Virtual Machine
  117. * start - Starting VM physical address
  118. * end - Ending VM physical address, inclusive.
  119. *
  120. * Output Args: None
  121. *
  122. * Return:
  123. * Pointer to overlapping region, NULL if no such region.
  124. *
  125. * Searches for a region with any physical memory that overlaps with
  126. * any portion of the guest physical addresses from start to end
  127. * inclusive. If multiple overlapping regions exist, a pointer to any
  128. * of the regions is returned. Null is returned only when no overlapping
  129. * region exists.
  130. */
  131. static struct userspace_mem_region *userspace_mem_region_find(
  132. struct kvm_vm *vm, uint64_t start, uint64_t end)
  133. {
  134. struct userspace_mem_region *region;
  135. for (region = vm->userspace_mem_region_head; region;
  136. region = region->next) {
  137. uint64_t existing_start = region->region.guest_phys_addr;
  138. uint64_t existing_end = region->region.guest_phys_addr
  139. + region->region.memory_size - 1;
  140. if (start <= existing_end && end >= existing_start)
  141. return region;
  142. }
  143. return NULL;
  144. }
  145. /* KVM Userspace Memory Region Find
  146. *
  147. * Input Args:
  148. * vm - Virtual Machine
  149. * start - Starting VM physical address
  150. * end - Ending VM physical address, inclusive.
  151. *
  152. * Output Args: None
  153. *
  154. * Return:
  155. * Pointer to overlapping region, NULL if no such region.
  156. *
  157. * Public interface to userspace_mem_region_find. Allows tests to look up
  158. * the memslot datastructure for a given range of guest physical memory.
  159. */
  160. struct kvm_userspace_memory_region *
  161. kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
  162. uint64_t end)
  163. {
  164. struct userspace_mem_region *region;
  165. region = userspace_mem_region_find(vm, start, end);
  166. if (!region)
  167. return NULL;
  168. return &region->region;
  169. }
  170. /* VCPU Find
  171. *
  172. * Input Args:
  173. * vm - Virtual Machine
  174. * vcpuid - VCPU ID
  175. *
  176. * Output Args: None
  177. *
  178. * Return:
  179. * Pointer to VCPU structure
  180. *
  181. * Locates a vcpu structure that describes the VCPU specified by vcpuid and
  182. * returns a pointer to it. Returns NULL if the VM doesn't contain a VCPU
  183. * for the specified vcpuid.
  184. */
  185. struct vcpu *vcpu_find(struct kvm_vm *vm,
  186. uint32_t vcpuid)
  187. {
  188. struct vcpu *vcpup;
  189. for (vcpup = vm->vcpu_head; vcpup; vcpup = vcpup->next) {
  190. if (vcpup->id == vcpuid)
  191. return vcpup;
  192. }
  193. return NULL;
  194. }
  195. /* VM VCPU Remove
  196. *
  197. * Input Args:
  198. * vm - Virtual Machine
  199. * vcpuid - VCPU ID
  200. *
  201. * Output Args: None
  202. *
  203. * Return: None, TEST_ASSERT failures for all error conditions
  204. *
  205. * Within the VM specified by vm, removes the VCPU given by vcpuid.
  206. */
  207. static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
  208. {
  209. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  210. int ret;
  211. ret = munmap(vcpu->state, sizeof(*vcpu->state));
  212. TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i "
  213. "errno: %i", ret, errno);
  214. close(vcpu->fd);
  215. TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i "
  216. "errno: %i", ret, errno);
  217. if (vcpu->next)
  218. vcpu->next->prev = vcpu->prev;
  219. if (vcpu->prev)
  220. vcpu->prev->next = vcpu->next;
  221. else
  222. vm->vcpu_head = vcpu->next;
  223. free(vcpu);
  224. }
  225. /* Destroys and frees the VM pointed to by vmp.
  226. */
  227. void kvm_vm_free(struct kvm_vm *vmp)
  228. {
  229. int ret;
  230. if (vmp == NULL)
  231. return;
  232. /* Free userspace_mem_regions. */
  233. while (vmp->userspace_mem_region_head) {
  234. struct userspace_mem_region *region
  235. = vmp->userspace_mem_region_head;
  236. region->region.memory_size = 0;
  237. ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION,
  238. &region->region);
  239. TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
  240. "rc: %i errno: %i", ret, errno);
  241. vmp->userspace_mem_region_head = region->next;
  242. sparsebit_free(&region->unused_phy_pages);
  243. ret = munmap(region->mmap_start, region->mmap_size);
  244. TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i",
  245. ret, errno);
  246. free(region);
  247. }
  248. /* Free VCPUs. */
  249. while (vmp->vcpu_head)
  250. vm_vcpu_rm(vmp, vmp->vcpu_head->id);
  251. /* Free sparsebit arrays. */
  252. sparsebit_free(&vmp->vpages_valid);
  253. sparsebit_free(&vmp->vpages_mapped);
  254. /* Close file descriptor for the VM. */
  255. ret = close(vmp->fd);
  256. TEST_ASSERT(ret == 0, "Close of vm fd failed,\n"
  257. " vmp->fd: %i rc: %i errno: %i", vmp->fd, ret, errno);
  258. close(vmp->kvm_fd);
  259. TEST_ASSERT(ret == 0, "Close of /dev/kvm fd failed,\n"
  260. " vmp->kvm_fd: %i rc: %i errno: %i", vmp->kvm_fd, ret, errno);
  261. /* Free the structure describing the VM. */
  262. free(vmp);
  263. }
  264. /* Memory Compare, host virtual to guest virtual
  265. *
  266. * Input Args:
  267. * hva - Starting host virtual address
  268. * vm - Virtual Machine
  269. * gva - Starting guest virtual address
  270. * len - number of bytes to compare
  271. *
  272. * Output Args: None
  273. *
  274. * Input/Output Args: None
  275. *
  276. * Return:
  277. * Returns 0 if the bytes starting at hva for a length of len
  278. * are equal the guest virtual bytes starting at gva. Returns
  279. * a value < 0, if bytes at hva are less than those at gva.
  280. * Otherwise a value > 0 is returned.
  281. *
  282. * Compares the bytes starting at the host virtual address hva, for
  283. * a length of len, to the guest bytes starting at the guest virtual
  284. * address given by gva.
  285. */
  286. int kvm_memcmp_hva_gva(void *hva,
  287. struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
  288. {
  289. size_t amt;
  290. /* Compare a batch of bytes until either a match is found
  291. * or all the bytes have been compared.
  292. */
  293. for (uintptr_t offset = 0; offset < len; offset += amt) {
  294. uintptr_t ptr1 = (uintptr_t)hva + offset;
  295. /* Determine host address for guest virtual address
  296. * at offset.
  297. */
  298. uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
  299. /* Determine amount to compare on this pass.
  300. * Don't allow the comparsion to cross a page boundary.
  301. */
  302. amt = len - offset;
  303. if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift))
  304. amt = vm->page_size - (ptr1 % vm->page_size);
  305. if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift))
  306. amt = vm->page_size - (ptr2 % vm->page_size);
  307. assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
  308. assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
  309. /* Perform the comparison. If there is a difference
  310. * return that result to the caller, otherwise need
  311. * to continue on looking for a mismatch.
  312. */
  313. int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
  314. if (ret != 0)
  315. return ret;
  316. }
  317. /* No mismatch found. Let the caller know the two memory
  318. * areas are equal.
  319. */
  320. return 0;
  321. }
  322. /* Allocate an instance of struct kvm_cpuid2
  323. *
  324. * Input Args: None
  325. *
  326. * Output Args: None
  327. *
  328. * Return: A pointer to the allocated struct. The caller is responsible
  329. * for freeing this struct.
  330. *
  331. * Since kvm_cpuid2 uses a 0-length array to allow a the size of the
  332. * array to be decided at allocation time, allocation is slightly
  333. * complicated. This function uses a reasonable default length for
  334. * the array and performs the appropriate allocation.
  335. */
  336. static struct kvm_cpuid2 *allocate_kvm_cpuid2(void)
  337. {
  338. struct kvm_cpuid2 *cpuid;
  339. int nent = 100;
  340. size_t size;
  341. size = sizeof(*cpuid);
  342. size += nent * sizeof(struct kvm_cpuid_entry2);
  343. cpuid = malloc(size);
  344. if (!cpuid) {
  345. perror("malloc");
  346. abort();
  347. }
  348. cpuid->nent = nent;
  349. return cpuid;
  350. }
  351. /* KVM Supported CPUID Get
  352. *
  353. * Input Args: None
  354. *
  355. * Output Args:
  356. *
  357. * Return: The supported KVM CPUID
  358. *
  359. * Get the guest CPUID supported by KVM.
  360. */
  361. struct kvm_cpuid2 *kvm_get_supported_cpuid(void)
  362. {
  363. static struct kvm_cpuid2 *cpuid;
  364. int ret;
  365. int kvm_fd;
  366. if (cpuid)
  367. return cpuid;
  368. cpuid = allocate_kvm_cpuid2();
  369. kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
  370. if (kvm_fd < 0)
  371. exit(KSFT_SKIP);
  372. ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid);
  373. TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n",
  374. ret, errno);
  375. close(kvm_fd);
  376. return cpuid;
  377. }
  378. /* Locate a cpuid entry.
  379. *
  380. * Input Args:
  381. * cpuid: The cpuid.
  382. * function: The function of the cpuid entry to find.
  383. *
  384. * Output Args: None
  385. *
  386. * Return: A pointer to the cpuid entry. Never returns NULL.
  387. */
  388. struct kvm_cpuid_entry2 *
  389. kvm_get_supported_cpuid_index(uint32_t function, uint32_t index)
  390. {
  391. struct kvm_cpuid2 *cpuid;
  392. struct kvm_cpuid_entry2 *entry = NULL;
  393. int i;
  394. cpuid = kvm_get_supported_cpuid();
  395. for (i = 0; i < cpuid->nent; i++) {
  396. if (cpuid->entries[i].function == function &&
  397. cpuid->entries[i].index == index) {
  398. entry = &cpuid->entries[i];
  399. break;
  400. }
  401. }
  402. TEST_ASSERT(entry, "Guest CPUID entry not found: (EAX=%x, ECX=%x).",
  403. function, index);
  404. return entry;
  405. }
  406. /* VM Userspace Memory Region Add
  407. *
  408. * Input Args:
  409. * vm - Virtual Machine
  410. * backing_src - Storage source for this region.
  411. * NULL to use anonymous memory.
  412. * guest_paddr - Starting guest physical address
  413. * slot - KVM region slot
  414. * npages - Number of physical pages
  415. * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES)
  416. *
  417. * Output Args: None
  418. *
  419. * Return: None
  420. *
  421. * Allocates a memory area of the number of pages specified by npages
  422. * and maps it to the VM specified by vm, at a starting physical address
  423. * given by guest_paddr. The region is created with a KVM region slot
  424. * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM. The
  425. * region is created with the flags given by flags.
  426. */
  427. void vm_userspace_mem_region_add(struct kvm_vm *vm,
  428. enum vm_mem_backing_src_type src_type,
  429. uint64_t guest_paddr, uint32_t slot, uint64_t npages,
  430. uint32_t flags)
  431. {
  432. int ret;
  433. unsigned long pmem_size = 0;
  434. struct userspace_mem_region *region;
  435. size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
  436. TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
  437. "address not on a page boundary.\n"
  438. " guest_paddr: 0x%lx vm->page_size: 0x%x",
  439. guest_paddr, vm->page_size);
  440. TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1)
  441. <= vm->max_gfn, "Physical range beyond maximum "
  442. "supported physical address,\n"
  443. " guest_paddr: 0x%lx npages: 0x%lx\n"
  444. " vm->max_gfn: 0x%lx vm->page_size: 0x%x",
  445. guest_paddr, npages, vm->max_gfn, vm->page_size);
  446. /* Confirm a mem region with an overlapping address doesn't
  447. * already exist.
  448. */
  449. region = (struct userspace_mem_region *) userspace_mem_region_find(
  450. vm, guest_paddr, guest_paddr + npages * vm->page_size);
  451. if (region != NULL)
  452. TEST_ASSERT(false, "overlapping userspace_mem_region already "
  453. "exists\n"
  454. " requested guest_paddr: 0x%lx npages: 0x%lx "
  455. "page_size: 0x%x\n"
  456. " existing guest_paddr: 0x%lx size: 0x%lx",
  457. guest_paddr, npages, vm->page_size,
  458. (uint64_t) region->region.guest_phys_addr,
  459. (uint64_t) region->region.memory_size);
  460. /* Confirm no region with the requested slot already exists. */
  461. for (region = vm->userspace_mem_region_head; region;
  462. region = region->next) {
  463. if (region->region.slot == slot)
  464. break;
  465. if ((guest_paddr <= (region->region.guest_phys_addr
  466. + region->region.memory_size))
  467. && ((guest_paddr + npages * vm->page_size)
  468. >= region->region.guest_phys_addr))
  469. break;
  470. }
  471. if (region != NULL)
  472. TEST_ASSERT(false, "A mem region with the requested slot "
  473. "or overlapping physical memory range already exists.\n"
  474. " requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
  475. " existing slot: %u paddr: 0x%lx size: 0x%lx",
  476. slot, guest_paddr, npages,
  477. region->region.slot,
  478. (uint64_t) region->region.guest_phys_addr,
  479. (uint64_t) region->region.memory_size);
  480. /* Allocate and initialize new mem region structure. */
  481. region = calloc(1, sizeof(*region));
  482. TEST_ASSERT(region != NULL, "Insufficient Memory");
  483. region->mmap_size = npages * vm->page_size;
  484. /* Enough memory to align up to a huge page. */
  485. if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
  486. region->mmap_size += huge_page_size;
  487. region->mmap_start = mmap(NULL, region->mmap_size,
  488. PROT_READ | PROT_WRITE,
  489. MAP_PRIVATE | MAP_ANONYMOUS
  490. | (src_type == VM_MEM_SRC_ANONYMOUS_HUGETLB ? MAP_HUGETLB : 0),
  491. -1, 0);
  492. TEST_ASSERT(region->mmap_start != MAP_FAILED,
  493. "test_malloc failed, mmap_start: %p errno: %i",
  494. region->mmap_start, errno);
  495. /* Align THP allocation up to start of a huge page. */
  496. region->host_mem = align(region->mmap_start,
  497. src_type == VM_MEM_SRC_ANONYMOUS_THP ? huge_page_size : 1);
  498. /* As needed perform madvise */
  499. if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) {
  500. ret = madvise(region->host_mem, npages * vm->page_size,
  501. src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE);
  502. TEST_ASSERT(ret == 0, "madvise failed,\n"
  503. " addr: %p\n"
  504. " length: 0x%lx\n"
  505. " src_type: %x",
  506. region->host_mem, npages * vm->page_size, src_type);
  507. }
  508. region->unused_phy_pages = sparsebit_alloc();
  509. sparsebit_set_num(region->unused_phy_pages,
  510. guest_paddr >> vm->page_shift, npages);
  511. region->region.slot = slot;
  512. region->region.flags = flags;
  513. region->region.guest_phys_addr = guest_paddr;
  514. region->region.memory_size = npages * vm->page_size;
  515. region->region.userspace_addr = (uintptr_t) region->host_mem;
  516. ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
  517. TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
  518. " rc: %i errno: %i\n"
  519. " slot: %u flags: 0x%x\n"
  520. " guest_phys_addr: 0x%lx size: 0x%lx",
  521. ret, errno, slot, flags,
  522. guest_paddr, (uint64_t) region->region.memory_size);
  523. /* Add to linked-list of memory regions. */
  524. if (vm->userspace_mem_region_head)
  525. vm->userspace_mem_region_head->prev = region;
  526. region->next = vm->userspace_mem_region_head;
  527. vm->userspace_mem_region_head = region;
  528. }
  529. /* Memslot to region
  530. *
  531. * Input Args:
  532. * vm - Virtual Machine
  533. * memslot - KVM memory slot ID
  534. *
  535. * Output Args: None
  536. *
  537. * Return:
  538. * Pointer to memory region structure that describe memory region
  539. * using kvm memory slot ID given by memslot. TEST_ASSERT failure
  540. * on error (e.g. currently no memory region using memslot as a KVM
  541. * memory slot ID).
  542. */
  543. static struct userspace_mem_region *memslot2region(struct kvm_vm *vm,
  544. uint32_t memslot)
  545. {
  546. struct userspace_mem_region *region;
  547. for (region = vm->userspace_mem_region_head; region;
  548. region = region->next) {
  549. if (region->region.slot == memslot)
  550. break;
  551. }
  552. if (region == NULL) {
  553. fprintf(stderr, "No mem region with the requested slot found,\n"
  554. " requested slot: %u\n", memslot);
  555. fputs("---- vm dump ----\n", stderr);
  556. vm_dump(stderr, vm, 2);
  557. TEST_ASSERT(false, "Mem region not found");
  558. }
  559. return region;
  560. }
  561. /* VM Memory Region Flags Set
  562. *
  563. * Input Args:
  564. * vm - Virtual Machine
  565. * flags - Starting guest physical address
  566. *
  567. * Output Args: None
  568. *
  569. * Return: None
  570. *
  571. * Sets the flags of the memory region specified by the value of slot,
  572. * to the values given by flags.
  573. */
  574. void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
  575. {
  576. int ret;
  577. struct userspace_mem_region *region;
  578. /* Locate memory region. */
  579. region = memslot2region(vm, slot);
  580. region->region.flags = flags;
  581. ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
  582. TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
  583. " rc: %i errno: %i slot: %u flags: 0x%x",
  584. ret, errno, slot, flags);
  585. }
  586. /* VCPU mmap Size
  587. *
  588. * Input Args: None
  589. *
  590. * Output Args: None
  591. *
  592. * Return:
  593. * Size of VCPU state
  594. *
  595. * Returns the size of the structure pointed to by the return value
  596. * of vcpu_state().
  597. */
  598. static int vcpu_mmap_sz(void)
  599. {
  600. int dev_fd, ret;
  601. dev_fd = open(KVM_DEV_PATH, O_RDONLY);
  602. if (dev_fd < 0)
  603. exit(KSFT_SKIP);
  604. ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
  605. TEST_ASSERT(ret >= sizeof(struct kvm_run),
  606. "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i",
  607. __func__, ret, errno);
  608. close(dev_fd);
  609. return ret;
  610. }
  611. /* VM VCPU Add
  612. *
  613. * Input Args:
  614. * vm - Virtual Machine
  615. * vcpuid - VCPU ID
  616. *
  617. * Output Args: None
  618. *
  619. * Return: None
  620. *
  621. * Creates and adds to the VM specified by vm and virtual CPU with
  622. * the ID given by vcpuid.
  623. */
  624. void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, int pgd_memslot, int gdt_memslot)
  625. {
  626. struct vcpu *vcpu;
  627. /* Confirm a vcpu with the specified id doesn't already exist. */
  628. vcpu = vcpu_find(vm, vcpuid);
  629. if (vcpu != NULL)
  630. TEST_ASSERT(false, "vcpu with the specified id "
  631. "already exists,\n"
  632. " requested vcpuid: %u\n"
  633. " existing vcpuid: %u state: %p",
  634. vcpuid, vcpu->id, vcpu->state);
  635. /* Allocate and initialize new vcpu structure. */
  636. vcpu = calloc(1, sizeof(*vcpu));
  637. TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
  638. vcpu->id = vcpuid;
  639. vcpu->fd = ioctl(vm->fd, KVM_CREATE_VCPU, vcpuid);
  640. TEST_ASSERT(vcpu->fd >= 0, "KVM_CREATE_VCPU failed, rc: %i errno: %i",
  641. vcpu->fd, errno);
  642. TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->state), "vcpu mmap size "
  643. "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
  644. vcpu_mmap_sz(), sizeof(*vcpu->state));
  645. vcpu->state = (struct kvm_run *) mmap(NULL, sizeof(*vcpu->state),
  646. PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
  647. TEST_ASSERT(vcpu->state != MAP_FAILED, "mmap vcpu_state failed, "
  648. "vcpu id: %u errno: %i", vcpuid, errno);
  649. /* Add to linked-list of VCPUs. */
  650. if (vm->vcpu_head)
  651. vm->vcpu_head->prev = vcpu;
  652. vcpu->next = vm->vcpu_head;
  653. vm->vcpu_head = vcpu;
  654. vcpu_setup(vm, vcpuid, pgd_memslot, gdt_memslot);
  655. }
  656. /* VM Virtual Address Unused Gap
  657. *
  658. * Input Args:
  659. * vm - Virtual Machine
  660. * sz - Size (bytes)
  661. * vaddr_min - Minimum Virtual Address
  662. *
  663. * Output Args: None
  664. *
  665. * Return:
  666. * Lowest virtual address at or below vaddr_min, with at least
  667. * sz unused bytes. TEST_ASSERT failure if no area of at least
  668. * size sz is available.
  669. *
  670. * Within the VM specified by vm, locates the lowest starting virtual
  671. * address >= vaddr_min, that has at least sz unallocated bytes. A
  672. * TEST_ASSERT failure occurs for invalid input or no area of at least
  673. * sz unallocated bytes >= vaddr_min is available.
  674. */
  675. static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
  676. vm_vaddr_t vaddr_min)
  677. {
  678. uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
  679. /* Determine lowest permitted virtual page index. */
  680. uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift;
  681. if ((pgidx_start * vm->page_size) < vaddr_min)
  682. goto no_va_found;
  683. /* Loop over section with enough valid virtual page indexes. */
  684. if (!sparsebit_is_set_num(vm->vpages_valid,
  685. pgidx_start, pages))
  686. pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
  687. pgidx_start, pages);
  688. do {
  689. /*
  690. * Are there enough unused virtual pages available at
  691. * the currently proposed starting virtual page index.
  692. * If not, adjust proposed starting index to next
  693. * possible.
  694. */
  695. if (sparsebit_is_clear_num(vm->vpages_mapped,
  696. pgidx_start, pages))
  697. goto va_found;
  698. pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
  699. pgidx_start, pages);
  700. if (pgidx_start == 0)
  701. goto no_va_found;
  702. /*
  703. * If needed, adjust proposed starting virtual address,
  704. * to next range of valid virtual addresses.
  705. */
  706. if (!sparsebit_is_set_num(vm->vpages_valid,
  707. pgidx_start, pages)) {
  708. pgidx_start = sparsebit_next_set_num(
  709. vm->vpages_valid, pgidx_start, pages);
  710. if (pgidx_start == 0)
  711. goto no_va_found;
  712. }
  713. } while (pgidx_start != 0);
  714. no_va_found:
  715. TEST_ASSERT(false, "No vaddr of specified pages available, "
  716. "pages: 0x%lx", pages);
  717. /* NOT REACHED */
  718. return -1;
  719. va_found:
  720. TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
  721. pgidx_start, pages),
  722. "Unexpected, invalid virtual page index range,\n"
  723. " pgidx_start: 0x%lx\n"
  724. " pages: 0x%lx",
  725. pgidx_start, pages);
  726. TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
  727. pgidx_start, pages),
  728. "Unexpected, pages already mapped,\n"
  729. " pgidx_start: 0x%lx\n"
  730. " pages: 0x%lx",
  731. pgidx_start, pages);
  732. return pgidx_start * vm->page_size;
  733. }
  734. /* VM Virtual Address Allocate
  735. *
  736. * Input Args:
  737. * vm - Virtual Machine
  738. * sz - Size in bytes
  739. * vaddr_min - Minimum starting virtual address
  740. * data_memslot - Memory region slot for data pages
  741. * pgd_memslot - Memory region slot for new virtual translation tables
  742. *
  743. * Output Args: None
  744. *
  745. * Return:
  746. * Starting guest virtual address
  747. *
  748. * Allocates at least sz bytes within the virtual address space of the vm
  749. * given by vm. The allocated bytes are mapped to a virtual address >=
  750. * the address given by vaddr_min. Note that each allocation uses a
  751. * a unique set of pages, with the minimum real allocation being at least
  752. * a page.
  753. */
  754. vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
  755. uint32_t data_memslot, uint32_t pgd_memslot)
  756. {
  757. uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
  758. virt_pgd_alloc(vm, pgd_memslot);
  759. /* Find an unused range of virtual page addresses of at least
  760. * pages in length.
  761. */
  762. vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
  763. /* Map the virtual pages. */
  764. for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
  765. pages--, vaddr += vm->page_size) {
  766. vm_paddr_t paddr;
  767. paddr = vm_phy_page_alloc(vm, KVM_UTIL_MIN_PADDR, data_memslot);
  768. virt_pg_map(vm, vaddr, paddr, pgd_memslot);
  769. sparsebit_set(vm->vpages_mapped,
  770. vaddr >> vm->page_shift);
  771. }
  772. return vaddr_start;
  773. }
  774. /* Address VM Physical to Host Virtual
  775. *
  776. * Input Args:
  777. * vm - Virtual Machine
  778. * gpa - VM physical address
  779. *
  780. * Output Args: None
  781. *
  782. * Return:
  783. * Equivalent host virtual address
  784. *
  785. * Locates the memory region containing the VM physical address given
  786. * by gpa, within the VM given by vm. When found, the host virtual
  787. * address providing the memory to the vm physical address is returned.
  788. * A TEST_ASSERT failure occurs if no region containing gpa exists.
  789. */
  790. void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
  791. {
  792. struct userspace_mem_region *region;
  793. for (region = vm->userspace_mem_region_head; region;
  794. region = region->next) {
  795. if ((gpa >= region->region.guest_phys_addr)
  796. && (gpa <= (region->region.guest_phys_addr
  797. + region->region.memory_size - 1)))
  798. return (void *) ((uintptr_t) region->host_mem
  799. + (gpa - region->region.guest_phys_addr));
  800. }
  801. TEST_ASSERT(false, "No vm physical memory at 0x%lx", gpa);
  802. return NULL;
  803. }
  804. /* Address Host Virtual to VM Physical
  805. *
  806. * Input Args:
  807. * vm - Virtual Machine
  808. * hva - Host virtual address
  809. *
  810. * Output Args: None
  811. *
  812. * Return:
  813. * Equivalent VM physical address
  814. *
  815. * Locates the memory region containing the host virtual address given
  816. * by hva, within the VM given by vm. When found, the equivalent
  817. * VM physical address is returned. A TEST_ASSERT failure occurs if no
  818. * region containing hva exists.
  819. */
  820. vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
  821. {
  822. struct userspace_mem_region *region;
  823. for (region = vm->userspace_mem_region_head; region;
  824. region = region->next) {
  825. if ((hva >= region->host_mem)
  826. && (hva <= (region->host_mem
  827. + region->region.memory_size - 1)))
  828. return (vm_paddr_t) ((uintptr_t)
  829. region->region.guest_phys_addr
  830. + (hva - (uintptr_t) region->host_mem));
  831. }
  832. TEST_ASSERT(false, "No mapping to a guest physical address, "
  833. "hva: %p", hva);
  834. return -1;
  835. }
  836. /* VM Create IRQ Chip
  837. *
  838. * Input Args:
  839. * vm - Virtual Machine
  840. *
  841. * Output Args: None
  842. *
  843. * Return: None
  844. *
  845. * Creates an interrupt controller chip for the VM specified by vm.
  846. */
  847. void vm_create_irqchip(struct kvm_vm *vm)
  848. {
  849. int ret;
  850. ret = ioctl(vm->fd, KVM_CREATE_IRQCHIP, 0);
  851. TEST_ASSERT(ret == 0, "KVM_CREATE_IRQCHIP IOCTL failed, "
  852. "rc: %i errno: %i", ret, errno);
  853. }
  854. /* VM VCPU State
  855. *
  856. * Input Args:
  857. * vm - Virtual Machine
  858. * vcpuid - VCPU ID
  859. *
  860. * Output Args: None
  861. *
  862. * Return:
  863. * Pointer to structure that describes the state of the VCPU.
  864. *
  865. * Locates and returns a pointer to a structure that describes the
  866. * state of the VCPU with the given vcpuid.
  867. */
  868. struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid)
  869. {
  870. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  871. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  872. return vcpu->state;
  873. }
  874. /* VM VCPU Run
  875. *
  876. * Input Args:
  877. * vm - Virtual Machine
  878. * vcpuid - VCPU ID
  879. *
  880. * Output Args: None
  881. *
  882. * Return: None
  883. *
  884. * Switch to executing the code for the VCPU given by vcpuid, within the VM
  885. * given by vm.
  886. */
  887. void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
  888. {
  889. int ret = _vcpu_run(vm, vcpuid);
  890. TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
  891. "rc: %i errno: %i", ret, errno);
  892. }
  893. int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
  894. {
  895. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  896. int rc;
  897. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  898. do {
  899. rc = ioctl(vcpu->fd, KVM_RUN, NULL);
  900. } while (rc == -1 && errno == EINTR);
  901. return rc;
  902. }
  903. /* VM VCPU Set MP State
  904. *
  905. * Input Args:
  906. * vm - Virtual Machine
  907. * vcpuid - VCPU ID
  908. * mp_state - mp_state to be set
  909. *
  910. * Output Args: None
  911. *
  912. * Return: None
  913. *
  914. * Sets the MP state of the VCPU given by vcpuid, to the state given
  915. * by mp_state.
  916. */
  917. void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
  918. struct kvm_mp_state *mp_state)
  919. {
  920. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  921. int ret;
  922. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  923. ret = ioctl(vcpu->fd, KVM_SET_MP_STATE, mp_state);
  924. TEST_ASSERT(ret == 0, "KVM_SET_MP_STATE IOCTL failed, "
  925. "rc: %i errno: %i", ret, errno);
  926. }
  927. /* VM VCPU Regs Get
  928. *
  929. * Input Args:
  930. * vm - Virtual Machine
  931. * vcpuid - VCPU ID
  932. *
  933. * Output Args:
  934. * regs - current state of VCPU regs
  935. *
  936. * Return: None
  937. *
  938. * Obtains the current register state for the VCPU specified by vcpuid
  939. * and stores it at the location given by regs.
  940. */
  941. void vcpu_regs_get(struct kvm_vm *vm,
  942. uint32_t vcpuid, struct kvm_regs *regs)
  943. {
  944. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  945. int ret;
  946. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  947. /* Get the regs. */
  948. ret = ioctl(vcpu->fd, KVM_GET_REGS, regs);
  949. TEST_ASSERT(ret == 0, "KVM_GET_REGS failed, rc: %i errno: %i",
  950. ret, errno);
  951. }
  952. /* VM VCPU Regs Set
  953. *
  954. * Input Args:
  955. * vm - Virtual Machine
  956. * vcpuid - VCPU ID
  957. * regs - Values to set VCPU regs to
  958. *
  959. * Output Args: None
  960. *
  961. * Return: None
  962. *
  963. * Sets the regs of the VCPU specified by vcpuid to the values
  964. * given by regs.
  965. */
  966. void vcpu_regs_set(struct kvm_vm *vm,
  967. uint32_t vcpuid, struct kvm_regs *regs)
  968. {
  969. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  970. int ret;
  971. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  972. /* Set the regs. */
  973. ret = ioctl(vcpu->fd, KVM_SET_REGS, regs);
  974. TEST_ASSERT(ret == 0, "KVM_SET_REGS failed, rc: %i errno: %i",
  975. ret, errno);
  976. }
  977. void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
  978. struct kvm_vcpu_events *events)
  979. {
  980. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  981. int ret;
  982. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  983. /* Get the regs. */
  984. ret = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, events);
  985. TEST_ASSERT(ret == 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
  986. ret, errno);
  987. }
  988. void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
  989. struct kvm_vcpu_events *events)
  990. {
  991. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  992. int ret;
  993. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  994. /* Set the regs. */
  995. ret = ioctl(vcpu->fd, KVM_SET_VCPU_EVENTS, events);
  996. TEST_ASSERT(ret == 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
  997. ret, errno);
  998. }
  999. /* VM VCPU Args Set
  1000. *
  1001. * Input Args:
  1002. * vm - Virtual Machine
  1003. * vcpuid - VCPU ID
  1004. * num - number of arguments
  1005. * ... - arguments, each of type uint64_t
  1006. *
  1007. * Output Args: None
  1008. *
  1009. * Return: None
  1010. *
  1011. * Sets the first num function input arguments to the values
  1012. * given as variable args. Each of the variable args is expected to
  1013. * be of type uint64_t.
  1014. */
  1015. void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...)
  1016. {
  1017. va_list ap;
  1018. struct kvm_regs regs;
  1019. TEST_ASSERT(num >= 1 && num <= 6, "Unsupported number of args,\n"
  1020. " num: %u\n",
  1021. num);
  1022. va_start(ap, num);
  1023. vcpu_regs_get(vm, vcpuid, &regs);
  1024. if (num >= 1)
  1025. regs.rdi = va_arg(ap, uint64_t);
  1026. if (num >= 2)
  1027. regs.rsi = va_arg(ap, uint64_t);
  1028. if (num >= 3)
  1029. regs.rdx = va_arg(ap, uint64_t);
  1030. if (num >= 4)
  1031. regs.rcx = va_arg(ap, uint64_t);
  1032. if (num >= 5)
  1033. regs.r8 = va_arg(ap, uint64_t);
  1034. if (num >= 6)
  1035. regs.r9 = va_arg(ap, uint64_t);
  1036. vcpu_regs_set(vm, vcpuid, &regs);
  1037. va_end(ap);
  1038. }
  1039. /* VM VCPU System Regs Get
  1040. *
  1041. * Input Args:
  1042. * vm - Virtual Machine
  1043. * vcpuid - VCPU ID
  1044. *
  1045. * Output Args:
  1046. * sregs - current state of VCPU system regs
  1047. *
  1048. * Return: None
  1049. *
  1050. * Obtains the current system register state for the VCPU specified by
  1051. * vcpuid and stores it at the location given by sregs.
  1052. */
  1053. void vcpu_sregs_get(struct kvm_vm *vm,
  1054. uint32_t vcpuid, struct kvm_sregs *sregs)
  1055. {
  1056. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  1057. int ret;
  1058. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  1059. /* Get the regs. */
  1060. /* Get the regs. */
  1061. ret = ioctl(vcpu->fd, KVM_GET_SREGS, sregs);
  1062. TEST_ASSERT(ret == 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
  1063. ret, errno);
  1064. }
  1065. /* VM VCPU System Regs Set
  1066. *
  1067. * Input Args:
  1068. * vm - Virtual Machine
  1069. * vcpuid - VCPU ID
  1070. * sregs - Values to set VCPU system regs to
  1071. *
  1072. * Output Args: None
  1073. *
  1074. * Return: None
  1075. *
  1076. * Sets the system regs of the VCPU specified by vcpuid to the values
  1077. * given by sregs.
  1078. */
  1079. void vcpu_sregs_set(struct kvm_vm *vm,
  1080. uint32_t vcpuid, struct kvm_sregs *sregs)
  1081. {
  1082. int ret = _vcpu_sregs_set(vm, vcpuid, sregs);
  1083. TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
  1084. "rc: %i errno: %i", ret, errno);
  1085. }
  1086. int _vcpu_sregs_set(struct kvm_vm *vm,
  1087. uint32_t vcpuid, struct kvm_sregs *sregs)
  1088. {
  1089. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  1090. int ret;
  1091. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  1092. /* Get the regs. */
  1093. return ioctl(vcpu->fd, KVM_SET_SREGS, sregs);
  1094. }
  1095. /* VCPU Ioctl
  1096. *
  1097. * Input Args:
  1098. * vm - Virtual Machine
  1099. * vcpuid - VCPU ID
  1100. * cmd - Ioctl number
  1101. * arg - Argument to pass to the ioctl
  1102. *
  1103. * Return: None
  1104. *
  1105. * Issues an arbitrary ioctl on a VCPU fd.
  1106. */
  1107. void vcpu_ioctl(struct kvm_vm *vm,
  1108. uint32_t vcpuid, unsigned long cmd, void *arg)
  1109. {
  1110. struct vcpu *vcpu = vcpu_find(vm, vcpuid);
  1111. int ret;
  1112. TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
  1113. ret = ioctl(vcpu->fd, cmd, arg);
  1114. TEST_ASSERT(ret == 0, "vcpu ioctl %lu failed, rc: %i errno: %i (%s)",
  1115. cmd, ret, errno, strerror(errno));
  1116. }
  1117. /* VM Ioctl
  1118. *
  1119. * Input Args:
  1120. * vm - Virtual Machine
  1121. * cmd - Ioctl number
  1122. * arg - Argument to pass to the ioctl
  1123. *
  1124. * Return: None
  1125. *
  1126. * Issues an arbitrary ioctl on a VM fd.
  1127. */
  1128. void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
  1129. {
  1130. int ret;
  1131. ret = ioctl(vm->fd, cmd, arg);
  1132. TEST_ASSERT(ret == 0, "vm ioctl %lu failed, rc: %i errno: %i (%s)",
  1133. cmd, ret, errno, strerror(errno));
  1134. }
  1135. /* VM Dump
  1136. *
  1137. * Input Args:
  1138. * vm - Virtual Machine
  1139. * indent - Left margin indent amount
  1140. *
  1141. * Output Args:
  1142. * stream - Output FILE stream
  1143. *
  1144. * Return: None
  1145. *
  1146. * Dumps the current state of the VM given by vm, to the FILE stream
  1147. * given by stream.
  1148. */
  1149. void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
  1150. {
  1151. struct userspace_mem_region *region;
  1152. struct vcpu *vcpu;
  1153. fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode);
  1154. fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
  1155. fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
  1156. fprintf(stream, "%*sMem Regions:\n", indent, "");
  1157. for (region = vm->userspace_mem_region_head; region;
  1158. region = region->next) {
  1159. fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
  1160. "host_virt: %p\n", indent + 2, "",
  1161. (uint64_t) region->region.guest_phys_addr,
  1162. (uint64_t) region->region.memory_size,
  1163. region->host_mem);
  1164. fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
  1165. sparsebit_dump(stream, region->unused_phy_pages, 0);
  1166. }
  1167. fprintf(stream, "%*sMapped Virtual Pages:\n", indent, "");
  1168. sparsebit_dump(stream, vm->vpages_mapped, indent + 2);
  1169. fprintf(stream, "%*spgd_created: %u\n", indent, "",
  1170. vm->pgd_created);
  1171. if (vm->pgd_created) {
  1172. fprintf(stream, "%*sVirtual Translation Tables:\n",
  1173. indent + 2, "");
  1174. virt_dump(stream, vm, indent + 4);
  1175. }
  1176. fprintf(stream, "%*sVCPUs:\n", indent, "");
  1177. for (vcpu = vm->vcpu_head; vcpu; vcpu = vcpu->next)
  1178. vcpu_dump(stream, vm, vcpu->id, indent + 2);
  1179. }
  1180. /* VM VCPU Dump
  1181. *
  1182. * Input Args:
  1183. * vm - Virtual Machine
  1184. * vcpuid - VCPU ID
  1185. * indent - Left margin indent amount
  1186. *
  1187. * Output Args:
  1188. * stream - Output FILE stream
  1189. *
  1190. * Return: None
  1191. *
  1192. * Dumps the current state of the VCPU specified by vcpuid, within the VM
  1193. * given by vm, to the FILE stream given by stream.
  1194. */
  1195. void vcpu_dump(FILE *stream, struct kvm_vm *vm,
  1196. uint32_t vcpuid, uint8_t indent)
  1197. {
  1198. struct kvm_regs regs;
  1199. struct kvm_sregs sregs;
  1200. fprintf(stream, "%*scpuid: %u\n", indent, "", vcpuid);
  1201. fprintf(stream, "%*sregs:\n", indent + 2, "");
  1202. vcpu_regs_get(vm, vcpuid, &regs);
  1203. regs_dump(stream, &regs, indent + 4);
  1204. fprintf(stream, "%*ssregs:\n", indent + 2, "");
  1205. vcpu_sregs_get(vm, vcpuid, &sregs);
  1206. sregs_dump(stream, &sregs, indent + 4);
  1207. }
  1208. /* Known KVM exit reasons */
  1209. static struct exit_reason {
  1210. unsigned int reason;
  1211. const char *name;
  1212. } exit_reasons_known[] = {
  1213. {KVM_EXIT_UNKNOWN, "UNKNOWN"},
  1214. {KVM_EXIT_EXCEPTION, "EXCEPTION"},
  1215. {KVM_EXIT_IO, "IO"},
  1216. {KVM_EXIT_HYPERCALL, "HYPERCALL"},
  1217. {KVM_EXIT_DEBUG, "DEBUG"},
  1218. {KVM_EXIT_HLT, "HLT"},
  1219. {KVM_EXIT_MMIO, "MMIO"},
  1220. {KVM_EXIT_IRQ_WINDOW_OPEN, "IRQ_WINDOW_OPEN"},
  1221. {KVM_EXIT_SHUTDOWN, "SHUTDOWN"},
  1222. {KVM_EXIT_FAIL_ENTRY, "FAIL_ENTRY"},
  1223. {KVM_EXIT_INTR, "INTR"},
  1224. {KVM_EXIT_SET_TPR, "SET_TPR"},
  1225. {KVM_EXIT_TPR_ACCESS, "TPR_ACCESS"},
  1226. {KVM_EXIT_S390_SIEIC, "S390_SIEIC"},
  1227. {KVM_EXIT_S390_RESET, "S390_RESET"},
  1228. {KVM_EXIT_DCR, "DCR"},
  1229. {KVM_EXIT_NMI, "NMI"},
  1230. {KVM_EXIT_INTERNAL_ERROR, "INTERNAL_ERROR"},
  1231. {KVM_EXIT_OSI, "OSI"},
  1232. {KVM_EXIT_PAPR_HCALL, "PAPR_HCALL"},
  1233. #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
  1234. {KVM_EXIT_MEMORY_NOT_PRESENT, "MEMORY_NOT_PRESENT"},
  1235. #endif
  1236. };
  1237. /* Exit Reason String
  1238. *
  1239. * Input Args:
  1240. * exit_reason - Exit reason
  1241. *
  1242. * Output Args: None
  1243. *
  1244. * Return:
  1245. * Constant string pointer describing the exit reason.
  1246. *
  1247. * Locates and returns a constant string that describes the KVM exit
  1248. * reason given by exit_reason. If no such string is found, a constant
  1249. * string of "Unknown" is returned.
  1250. */
  1251. const char *exit_reason_str(unsigned int exit_reason)
  1252. {
  1253. unsigned int n1;
  1254. for (n1 = 0; n1 < ARRAY_SIZE(exit_reasons_known); n1++) {
  1255. if (exit_reason == exit_reasons_known[n1].reason)
  1256. return exit_reasons_known[n1].name;
  1257. }
  1258. return "Unknown";
  1259. }
  1260. /* Physical Page Allocate
  1261. *
  1262. * Input Args:
  1263. * vm - Virtual Machine
  1264. * paddr_min - Physical address minimum
  1265. * memslot - Memory region to allocate page from
  1266. *
  1267. * Output Args: None
  1268. *
  1269. * Return:
  1270. * Starting physical address
  1271. *
  1272. * Within the VM specified by vm, locates an available physical page
  1273. * at or above paddr_min. If found, the page is marked as in use
  1274. * and its address is returned. A TEST_ASSERT failure occurs if no
  1275. * page is available at or above paddr_min.
  1276. */
  1277. vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm,
  1278. vm_paddr_t paddr_min, uint32_t memslot)
  1279. {
  1280. struct userspace_mem_region *region;
  1281. sparsebit_idx_t pg;
  1282. TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
  1283. "not divisible by page size.\n"
  1284. " paddr_min: 0x%lx page_size: 0x%x",
  1285. paddr_min, vm->page_size);
  1286. /* Locate memory region. */
  1287. region = memslot2region(vm, memslot);
  1288. /* Locate next available physical page at or above paddr_min. */
  1289. pg = paddr_min >> vm->page_shift;
  1290. if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
  1291. pg = sparsebit_next_set(region->unused_phy_pages, pg);
  1292. if (pg == 0) {
  1293. fprintf(stderr, "No guest physical page available, "
  1294. "paddr_min: 0x%lx page_size: 0x%x memslot: %u",
  1295. paddr_min, vm->page_size, memslot);
  1296. fputs("---- vm dump ----\n", stderr);
  1297. vm_dump(stderr, vm, 2);
  1298. abort();
  1299. }
  1300. }
  1301. /* Specify page as in use and return its address. */
  1302. sparsebit_clear(region->unused_phy_pages, pg);
  1303. return pg * vm->page_size;
  1304. }
  1305. /* Address Guest Virtual to Host Virtual
  1306. *
  1307. * Input Args:
  1308. * vm - Virtual Machine
  1309. * gva - VM virtual address
  1310. *
  1311. * Output Args: None
  1312. *
  1313. * Return:
  1314. * Equivalent host virtual address
  1315. */
  1316. void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
  1317. {
  1318. return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
  1319. }