hyperv_svm_test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022, Red Hat, Inc.
  4. *
  5. * Tests for Hyper-V extensions to SVM.
  6. */
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/bitmap.h>
  13. #include "test_util.h"
  14. #include "kvm_util.h"
  15. #include "processor.h"
  16. #include "svm_util.h"
  17. #include "hyperv.h"
  18. #define L2_GUEST_STACK_SIZE 256
  19. /* Exit to L1 from L2 with RDMSR instruction */
  20. static inline void rdmsr_from_l2(uint32_t msr)
  21. {
  22. /* Currently, L1 doesn't preserve GPRs during vmexits. */
  23. __asm__ __volatile__ ("rdmsr" : : "c"(msr) :
  24. "rax", "rbx", "rdx", "rsi", "rdi", "r8", "r9",
  25. "r10", "r11", "r12", "r13", "r14", "r15");
  26. }
  27. void l2_guest_code(void)
  28. {
  29. u64 unused;
  30. GUEST_SYNC(3);
  31. /* Exit to L1 */
  32. vmmcall();
  33. /* MSR-Bitmap tests */
  34. rdmsr_from_l2(MSR_FS_BASE); /* intercepted */
  35. rdmsr_from_l2(MSR_FS_BASE); /* intercepted */
  36. rdmsr_from_l2(MSR_GS_BASE); /* not intercepted */
  37. vmmcall();
  38. rdmsr_from_l2(MSR_GS_BASE); /* intercepted */
  39. GUEST_SYNC(5);
  40. /* L2 TLB flush tests */
  41. hyperv_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE |
  42. HV_HYPERCALL_FAST_BIT, 0x0,
  43. HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES |
  44. HV_FLUSH_ALL_PROCESSORS);
  45. rdmsr_from_l2(MSR_FS_BASE);
  46. /*
  47. * Note: hypercall status (RAX) is not preserved correctly by L1 after
  48. * synthetic vmexit, use unchecked version.
  49. */
  50. __hyperv_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE |
  51. HV_HYPERCALL_FAST_BIT, 0x0,
  52. HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES |
  53. HV_FLUSH_ALL_PROCESSORS, &unused);
  54. /* Done, exit to L1 and never come back. */
  55. vmmcall();
  56. }
  57. static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm,
  58. struct hyperv_test_pages *hv_pages,
  59. vm_vaddr_t pgs_gpa)
  60. {
  61. unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
  62. struct vmcb *vmcb = svm->vmcb;
  63. struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
  64. GUEST_SYNC(1);
  65. wrmsr(HV_X64_MSR_GUEST_OS_ID, HYPERV_LINUX_OS_ID);
  66. wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
  67. enable_vp_assist(hv_pages->vp_assist_gpa, hv_pages->vp_assist);
  68. GUEST_ASSERT(svm->vmcb_gpa);
  69. /* Prepare for L2 execution. */
  70. generic_svm_setup(svm, l2_guest_code,
  71. &l2_guest_stack[L2_GUEST_STACK_SIZE]);
  72. /* L2 TLB flush setup */
  73. hve->partition_assist_page = hv_pages->partition_assist_gpa;
  74. hve->hv_enlightenments_control.nested_flush_hypercall = 1;
  75. hve->hv_vm_id = 1;
  76. hve->hv_vp_id = 1;
  77. current_vp_assist->nested_control.features.directhypercall = 1;
  78. *(u32 *)(hv_pages->partition_assist) = 0;
  79. GUEST_SYNC(2);
  80. run_guest(vmcb, svm->vmcb_gpa);
  81. GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
  82. GUEST_SYNC(4);
  83. vmcb->save.rip += 3;
  84. /* Intercept RDMSR 0xc0000100 */
  85. vmcb->control.intercept |= 1ULL << INTERCEPT_MSR_PROT;
  86. __set_bit(2 * (MSR_FS_BASE & 0x1fff), svm->msr + 0x800);
  87. run_guest(vmcb, svm->vmcb_gpa);
  88. GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_MSR);
  89. vmcb->save.rip += 2; /* rdmsr */
  90. /* Enable enlightened MSR bitmap */
  91. hve->hv_enlightenments_control.msr_bitmap = 1;
  92. run_guest(vmcb, svm->vmcb_gpa);
  93. GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_MSR);
  94. vmcb->save.rip += 2; /* rdmsr */
  95. /* Intercept RDMSR 0xc0000101 without telling KVM about it */
  96. __set_bit(2 * (MSR_GS_BASE & 0x1fff), svm->msr + 0x800);
  97. /* Make sure HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP is set */
  98. vmcb->control.clean |= HV_VMCB_NESTED_ENLIGHTENMENTS;
  99. run_guest(vmcb, svm->vmcb_gpa);
  100. /* Make sure we don't see SVM_EXIT_MSR here so eMSR bitmap works */
  101. GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
  102. vmcb->save.rip += 3; /* vmcall */
  103. /* Now tell KVM we've changed MSR-Bitmap */
  104. vmcb->control.clean &= ~HV_VMCB_NESTED_ENLIGHTENMENTS;
  105. run_guest(vmcb, svm->vmcb_gpa);
  106. GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_MSR);
  107. vmcb->save.rip += 2; /* rdmsr */
  108. /*
  109. * L2 TLB flush test. First VMCALL should be handled directly by L0,
  110. * no VMCALL exit expected.
  111. */
  112. run_guest(vmcb, svm->vmcb_gpa);
  113. GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_MSR);
  114. vmcb->save.rip += 2; /* rdmsr */
  115. /* Enable synthetic vmexit */
  116. *(u32 *)(hv_pages->partition_assist) = 1;
  117. run_guest(vmcb, svm->vmcb_gpa);
  118. GUEST_ASSERT(vmcb->control.exit_code == HV_SVM_EXITCODE_ENL);
  119. GUEST_ASSERT(vmcb->control.exit_info_1 == HV_SVM_ENL_EXITCODE_TRAP_AFTER_FLUSH);
  120. run_guest(vmcb, svm->vmcb_gpa);
  121. GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
  122. GUEST_SYNC(6);
  123. GUEST_DONE();
  124. }
  125. int main(int argc, char *argv[])
  126. {
  127. vm_vaddr_t nested_gva = 0, hv_pages_gva = 0;
  128. vm_vaddr_t hcall_page;
  129. struct kvm_vcpu *vcpu;
  130. struct kvm_vm *vm;
  131. struct ucall uc;
  132. int stage;
  133. TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
  134. TEST_REQUIRE(kvm_hv_cpu_has(HV_X64_NESTED_DIRECT_FLUSH));
  135. /* Create VM */
  136. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  137. vcpu_set_hv_cpuid(vcpu);
  138. vcpu_alloc_svm(vm, &nested_gva);
  139. vcpu_alloc_hyperv_test_pages(vm, &hv_pages_gva);
  140. hcall_page = vm_vaddr_alloc_pages(vm, 1);
  141. memset(addr_gva2hva(vm, hcall_page), 0x0, getpagesize());
  142. vcpu_args_set(vcpu, 3, nested_gva, hv_pages_gva, addr_gva2gpa(vm, hcall_page));
  143. vcpu_set_msr(vcpu, HV_X64_MSR_VP_INDEX, vcpu->id);
  144. for (stage = 1;; stage++) {
  145. vcpu_run(vcpu);
  146. TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
  147. switch (get_ucall(vcpu, &uc)) {
  148. case UCALL_ABORT:
  149. REPORT_GUEST_ASSERT(uc);
  150. /* NOT REACHED */
  151. case UCALL_SYNC:
  152. break;
  153. case UCALL_DONE:
  154. goto done;
  155. default:
  156. TEST_FAIL("Unknown ucall %lu", uc.cmd);
  157. }
  158. /* UCALL_SYNC is handled here. */
  159. TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
  160. uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
  161. stage, (ulong)uc.args[1]);
  162. }
  163. done:
  164. kvm_vm_free(vm);
  165. }