kvm_util.c 38 KB

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