state_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * KVM_GET/SET_* tests
  3. *
  4. * Copyright (C) 2018, Red Hat, Inc.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2.
  7. *
  8. * Tests for vCPU state save/restore, including nested guest state.
  9. */
  10. #define _GNU_SOURCE /* for program_invocation_short_name */
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/ioctl.h>
  16. #include "test_util.h"
  17. #include "kvm_util.h"
  18. #include "processor.h"
  19. #include "vmx.h"
  20. #define VCPU_ID 5
  21. static bool have_nested_state;
  22. void l2_guest_code(void)
  23. {
  24. GUEST_SYNC(6);
  25. /* Exit to L1 */
  26. vmcall();
  27. /* L1 has now set up a shadow VMCS for us. */
  28. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
  29. GUEST_SYNC(10);
  30. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
  31. GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0fffee));
  32. GUEST_SYNC(11);
  33. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0fffee);
  34. GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0ffffee));
  35. GUEST_SYNC(12);
  36. /* Done, exit to L1 and never come back. */
  37. vmcall();
  38. }
  39. void l1_guest_code(struct vmx_pages *vmx_pages)
  40. {
  41. #define L2_GUEST_STACK_SIZE 64
  42. unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
  43. GUEST_ASSERT(vmx_pages->vmcs_gpa);
  44. GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
  45. GUEST_SYNC(3);
  46. GUEST_ASSERT(load_vmcs(vmx_pages));
  47. GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
  48. GUEST_SYNC(4);
  49. GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
  50. prepare_vmcs(vmx_pages, l2_guest_code,
  51. &l2_guest_stack[L2_GUEST_STACK_SIZE]);
  52. GUEST_SYNC(5);
  53. GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
  54. GUEST_ASSERT(!vmlaunch());
  55. GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
  56. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  57. /* Check that the launched state is preserved. */
  58. GUEST_ASSERT(vmlaunch());
  59. GUEST_ASSERT(!vmresume());
  60. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  61. GUEST_SYNC(7);
  62. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  63. GUEST_ASSERT(!vmresume());
  64. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  65. vmwrite(GUEST_RIP, vmreadz(GUEST_RIP) + 3);
  66. vmwrite(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  67. vmwrite(VMCS_LINK_POINTER, vmx_pages->shadow_vmcs_gpa);
  68. GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
  69. GUEST_ASSERT(vmlaunch());
  70. GUEST_SYNC(8);
  71. GUEST_ASSERT(vmlaunch());
  72. GUEST_ASSERT(vmresume());
  73. vmwrite(GUEST_RIP, 0xc0ffee);
  74. GUEST_SYNC(9);
  75. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
  76. GUEST_ASSERT(!vmptrld(vmx_pages->vmcs_gpa));
  77. GUEST_ASSERT(!vmresume());
  78. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  79. GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
  80. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
  81. GUEST_ASSERT(vmlaunch());
  82. GUEST_ASSERT(vmresume());
  83. GUEST_SYNC(13);
  84. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
  85. GUEST_ASSERT(vmlaunch());
  86. GUEST_ASSERT(vmresume());
  87. }
  88. void guest_code(struct vmx_pages *vmx_pages)
  89. {
  90. GUEST_SYNC(1);
  91. GUEST_SYNC(2);
  92. if (vmx_pages)
  93. l1_guest_code(vmx_pages);
  94. GUEST_DONE();
  95. }
  96. int main(int argc, char *argv[])
  97. {
  98. struct vmx_pages *vmx_pages = NULL;
  99. vm_vaddr_t vmx_pages_gva = 0;
  100. struct kvm_regs regs1, regs2;
  101. struct kvm_vm *vm;
  102. struct kvm_run *run;
  103. struct kvm_x86_state *state;
  104. struct ucall uc;
  105. int stage;
  106. struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
  107. /* Create VM */
  108. vm = vm_create_default(VCPU_ID, 0, guest_code);
  109. vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
  110. run = vcpu_state(vm, VCPU_ID);
  111. vcpu_regs_get(vm, VCPU_ID, &regs1);
  112. if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
  113. vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva);
  114. vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
  115. } else {
  116. printf("will skip nested state checks\n");
  117. vcpu_args_set(vm, VCPU_ID, 1, 0);
  118. }
  119. for (stage = 1;; stage++) {
  120. _vcpu_run(vm, VCPU_ID);
  121. TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
  122. "Unexpected exit reason: %u (%s),\n",
  123. run->exit_reason,
  124. exit_reason_str(run->exit_reason));
  125. memset(&regs1, 0, sizeof(regs1));
  126. vcpu_regs_get(vm, VCPU_ID, &regs1);
  127. switch (get_ucall(vm, VCPU_ID, &uc)) {
  128. case UCALL_ABORT:
  129. TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
  130. __FILE__, uc.args[1]);
  131. /* NOT REACHED */
  132. case UCALL_SYNC:
  133. break;
  134. case UCALL_DONE:
  135. goto done;
  136. default:
  137. TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
  138. }
  139. /* UCALL_SYNC is handled here. */
  140. TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
  141. uc.args[1] == stage, "Unexpected register values vmexit #%lx, got %lx",
  142. stage, (ulong)uc.args[1]);
  143. state = vcpu_save_state(vm, VCPU_ID);
  144. kvm_vm_release(vm);
  145. /* Restore state in a new VM. */
  146. kvm_vm_restart(vm, O_RDWR);
  147. vm_vcpu_add(vm, VCPU_ID, 0, 0);
  148. vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
  149. vcpu_load_state(vm, VCPU_ID, state);
  150. run = vcpu_state(vm, VCPU_ID);
  151. free(state);
  152. memset(&regs2, 0, sizeof(regs2));
  153. vcpu_regs_get(vm, VCPU_ID, &regs2);
  154. TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
  155. "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
  156. (ulong) regs2.rdi, (ulong) regs2.rsi);
  157. }
  158. done:
  159. kvm_vm_free(vm);
  160. }