kvm_util.c 40 KB

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