kvm_util.c 41 KB

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