kvm_util.c 38 KB

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