kvm_util.c 40 KB

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