state_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "x86.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(5);
  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(9);
  30. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
  31. GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0fffee));
  32. GUEST_SYNC(10);
  33. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0fffee);
  34. GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0ffffee));
  35. GUEST_SYNC(11);
  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_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
  46. GUEST_SYNC(3);
  47. GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
  48. prepare_vmcs(vmx_pages, l2_guest_code,
  49. &l2_guest_stack[L2_GUEST_STACK_SIZE]);
  50. GUEST_SYNC(4);
  51. GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
  52. GUEST_ASSERT(!vmlaunch());
  53. GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
  54. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  55. /* Check that the launched state is preserved. */
  56. GUEST_ASSERT(vmlaunch());
  57. GUEST_ASSERT(!vmresume());
  58. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  59. GUEST_SYNC(6);
  60. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  61. GUEST_ASSERT(!vmresume());
  62. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  63. vmwrite(GUEST_RIP, vmreadz(GUEST_RIP) + 3);
  64. vmwrite(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  65. vmwrite(VMCS_LINK_POINTER, vmx_pages->shadow_vmcs_gpa);
  66. GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
  67. GUEST_ASSERT(vmlaunch());
  68. GUEST_SYNC(7);
  69. GUEST_ASSERT(vmlaunch());
  70. GUEST_ASSERT(vmresume());
  71. vmwrite(GUEST_RIP, 0xc0ffee);
  72. GUEST_SYNC(8);
  73. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
  74. GUEST_ASSERT(!vmptrld(vmx_pages->vmcs_gpa));
  75. GUEST_ASSERT(!vmresume());
  76. GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
  77. GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
  78. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
  79. GUEST_ASSERT(vmlaunch());
  80. GUEST_ASSERT(vmresume());
  81. GUEST_SYNC(12);
  82. GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
  83. GUEST_ASSERT(vmlaunch());
  84. GUEST_ASSERT(vmresume());
  85. }
  86. void guest_code(struct vmx_pages *vmx_pages)
  87. {
  88. GUEST_SYNC(1);
  89. GUEST_SYNC(2);
  90. if (vmx_pages)
  91. l1_guest_code(vmx_pages);
  92. GUEST_DONE();
  93. }
  94. int main(int argc, char *argv[])
  95. {
  96. struct vmx_pages *vmx_pages = NULL;
  97. vm_vaddr_t vmx_pages_gva = 0;
  98. struct kvm_regs regs1, regs2;
  99. struct kvm_vm *vm;
  100. struct kvm_run *run;
  101. struct kvm_x86_state *state;
  102. int stage;
  103. struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
  104. /* Create VM */
  105. vm = vm_create_default(VCPU_ID, 0, guest_code);
  106. vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
  107. run = vcpu_state(vm, VCPU_ID);
  108. vcpu_regs_get(vm, VCPU_ID, &regs1);
  109. if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
  110. vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva);
  111. vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
  112. } else {
  113. printf("will skip nested state checks\n");
  114. vcpu_args_set(vm, VCPU_ID, 1, 0);
  115. }
  116. for (stage = 1;; stage++) {
  117. _vcpu_run(vm, VCPU_ID);
  118. TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
  119. "Unexpected exit reason: %u (%s),\n",
  120. run->exit_reason,
  121. exit_reason_str(run->exit_reason));
  122. memset(&regs1, 0, sizeof(regs1));
  123. vcpu_regs_get(vm, VCPU_ID, &regs1);
  124. switch (run->io.port) {
  125. case GUEST_PORT_ABORT:
  126. TEST_ASSERT(false, "%s at %s:%d", (const char *) regs1.rdi,
  127. __FILE__, regs1.rsi);
  128. /* NOT REACHED */
  129. case GUEST_PORT_SYNC:
  130. break;
  131. case GUEST_PORT_DONE:
  132. goto done;
  133. default:
  134. TEST_ASSERT(false, "Unknown port 0x%x.", run->io.port);
  135. }
  136. /* PORT_SYNC is handled here. */
  137. TEST_ASSERT(!strcmp((const char *)regs1.rdi, "hello") &&
  138. regs1.rsi == stage, "Unexpected register values vmexit #%lx, got %lx",
  139. stage, (ulong) regs1.rsi);
  140. state = vcpu_save_state(vm, VCPU_ID);
  141. kvm_vm_release(vm);
  142. /* Restore state in a new VM. */
  143. kvm_vm_restart(vm, O_RDWR);
  144. vm_vcpu_add(vm, VCPU_ID, 0, 0);
  145. vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
  146. vcpu_load_state(vm, VCPU_ID, state);
  147. run = vcpu_state(vm, VCPU_ID);
  148. free(state);
  149. memset(&regs2, 0, sizeof(regs2));
  150. vcpu_regs_get(vm, VCPU_ID, &regs2);
  151. TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
  152. "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
  153. (ulong) regs2.rdi, (ulong) regs2.rsi);
  154. }
  155. done:
  156. kvm_vm_free(vm);
  157. }