kvm_util.c 38 KB

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