sev_smoke_test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/ioctl.h>
  7. #include <math.h>
  8. #include "test_util.h"
  9. #include "kvm_util.h"
  10. #include "processor.h"
  11. #include "svm_util.h"
  12. #include "linux/psp-sev.h"
  13. #include "sev.h"
  14. static void guest_sev_test_msr(uint32_t msr)
  15. {
  16. uint64_t val = rdmsr(msr);
  17. wrmsr(msr, val);
  18. GUEST_ASSERT(val == rdmsr(msr));
  19. }
  20. #define guest_sev_test_reg(reg) \
  21. do { \
  22. uint64_t val = get_##reg(); \
  23. \
  24. set_##reg(val); \
  25. GUEST_ASSERT(val == get_##reg()); \
  26. } while (0)
  27. static void guest_sev_test_regs(void)
  28. {
  29. guest_sev_test_msr(MSR_EFER);
  30. guest_sev_test_reg(cr0);
  31. guest_sev_test_reg(cr3);
  32. guest_sev_test_reg(cr4);
  33. guest_sev_test_reg(cr8);
  34. }
  35. #define XFEATURE_MASK_X87_AVX (XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM)
  36. static void guest_snp_code(void)
  37. {
  38. uint64_t sev_msr = rdmsr(MSR_AMD64_SEV);
  39. GUEST_ASSERT(sev_msr & MSR_AMD64_SEV_ENABLED);
  40. GUEST_ASSERT(sev_msr & MSR_AMD64_SEV_ES_ENABLED);
  41. GUEST_ASSERT(sev_msr & MSR_AMD64_SEV_SNP_ENABLED);
  42. guest_sev_test_regs();
  43. wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ);
  44. vmgexit();
  45. }
  46. static void guest_sev_es_code(void)
  47. {
  48. /* TODO: Check CPUID after GHCB-based hypercall support is added. */
  49. GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED);
  50. GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ES_ENABLED);
  51. guest_sev_test_regs();
  52. /*
  53. * TODO: Add GHCB and ucall support for SEV-ES guests. For now, simply
  54. * force "termination" to signal "done" via the GHCB MSR protocol.
  55. */
  56. wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ);
  57. vmgexit();
  58. }
  59. static void guest_sev_code(void)
  60. {
  61. GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV));
  62. GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED);
  63. guest_sev_test_regs();
  64. GUEST_DONE();
  65. }
  66. /* Stash state passed via VMSA before any compiled code runs. */
  67. extern void guest_code_xsave(void);
  68. asm("guest_code_xsave:\n"
  69. "mov $" __stringify(XFEATURE_MASK_X87_AVX) ", %eax\n"
  70. "xor %edx, %edx\n"
  71. "xsave (%rdi)\n"
  72. "jmp guest_sev_es_code");
  73. static void compare_xsave(u8 *from_host, u8 *from_guest)
  74. {
  75. int i;
  76. bool bad = false;
  77. for (i = 0; i < 4095; i++) {
  78. if (from_host[i] != from_guest[i]) {
  79. printf("mismatch at %u | %02hhx %02hhx\n",
  80. i, from_host[i], from_guest[i]);
  81. bad = true;
  82. }
  83. }
  84. if (bad)
  85. abort();
  86. }
  87. static void test_sync_vmsa(uint32_t type, uint64_t policy)
  88. {
  89. struct kvm_vcpu *vcpu;
  90. struct kvm_vm *vm;
  91. vm_vaddr_t gva;
  92. void *hva;
  93. double x87val = M_PI;
  94. struct kvm_xsave __attribute__((aligned(64))) xsave = { 0 };
  95. vm = vm_sev_create_with_one_vcpu(type, guest_code_xsave, &vcpu);
  96. gva = vm_vaddr_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR,
  97. MEM_REGION_TEST_DATA);
  98. hva = addr_gva2hva(vm, gva);
  99. vcpu_args_set(vcpu, 1, gva);
  100. asm("fninit\n"
  101. "vpcmpeqb %%ymm4, %%ymm4, %%ymm4\n"
  102. "fldl %3\n"
  103. "xsave (%2)\n"
  104. "fstp %%st\n"
  105. : "=m"(xsave)
  106. : "A"(XFEATURE_MASK_X87_AVX), "r"(&xsave), "m" (x87val)
  107. : "ymm4", "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)");
  108. vcpu_xsave_set(vcpu, &xsave);
  109. vm_sev_launch(vm, policy, NULL);
  110. /* This page is shared, so make it decrypted. */
  111. memset(hva, 0, PAGE_SIZE);
  112. vcpu_run(vcpu);
  113. TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SYSTEM_EVENT,
  114. "Wanted SYSTEM_EVENT, got %s",
  115. exit_reason_str(vcpu->run->exit_reason));
  116. TEST_ASSERT_EQ(vcpu->run->system_event.type, KVM_SYSTEM_EVENT_SEV_TERM);
  117. TEST_ASSERT_EQ(vcpu->run->system_event.ndata, 1);
  118. TEST_ASSERT_EQ(vcpu->run->system_event.data[0], GHCB_MSR_TERM_REQ);
  119. compare_xsave((u8 *)&xsave, (u8 *)hva);
  120. kvm_vm_free(vm);
  121. }
  122. static void test_sev(void *guest_code, uint32_t type, uint64_t policy)
  123. {
  124. struct kvm_vcpu *vcpu;
  125. struct kvm_vm *vm;
  126. struct ucall uc;
  127. vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu);
  128. /* TODO: Validate the measurement is as expected. */
  129. vm_sev_launch(vm, policy, NULL);
  130. for (;;) {
  131. vcpu_run(vcpu);
  132. if (is_sev_es_vm(vm)) {
  133. TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SYSTEM_EVENT,
  134. "Wanted SYSTEM_EVENT, got %s",
  135. exit_reason_str(vcpu->run->exit_reason));
  136. TEST_ASSERT_EQ(vcpu->run->system_event.type, KVM_SYSTEM_EVENT_SEV_TERM);
  137. TEST_ASSERT_EQ(vcpu->run->system_event.ndata, 1);
  138. TEST_ASSERT_EQ(vcpu->run->system_event.data[0], GHCB_MSR_TERM_REQ);
  139. break;
  140. }
  141. switch (get_ucall(vcpu, &uc)) {
  142. case UCALL_SYNC:
  143. continue;
  144. case UCALL_DONE:
  145. return;
  146. case UCALL_ABORT:
  147. REPORT_GUEST_ASSERT(uc);
  148. default:
  149. TEST_FAIL("Unexpected exit: %s",
  150. exit_reason_str(vcpu->run->exit_reason));
  151. }
  152. }
  153. kvm_vm_free(vm);
  154. }
  155. static void guest_shutdown_code(void)
  156. {
  157. struct desc_ptr idt;
  158. /* Clobber the IDT so that #UD is guaranteed to trigger SHUTDOWN. */
  159. memset(&idt, 0, sizeof(idt));
  160. set_idt(&idt);
  161. __asm__ __volatile__("ud2");
  162. }
  163. static void test_sev_shutdown(uint32_t type, uint64_t policy)
  164. {
  165. struct kvm_vcpu *vcpu;
  166. struct kvm_vm *vm;
  167. vm = vm_sev_create_with_one_vcpu(type, guest_shutdown_code, &vcpu);
  168. vm_sev_launch(vm, policy, NULL);
  169. vcpu_run(vcpu);
  170. TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SHUTDOWN,
  171. "Wanted SHUTDOWN, got %s",
  172. exit_reason_str(vcpu->run->exit_reason));
  173. kvm_vm_free(vm);
  174. }
  175. static void test_sev_smoke(void *guest, uint32_t type, uint64_t policy)
  176. {
  177. const u64 xf_mask = XFEATURE_MASK_X87_AVX;
  178. if (type == KVM_X86_SNP_VM)
  179. test_sev(guest, type, policy | SNP_POLICY_DBG);
  180. else
  181. test_sev(guest, type, policy | SEV_POLICY_NO_DBG);
  182. test_sev(guest, type, policy);
  183. if (type == KVM_X86_SEV_VM)
  184. return;
  185. test_sev_shutdown(type, policy);
  186. if (kvm_has_cap(KVM_CAP_XCRS) &&
  187. (xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) == xf_mask) {
  188. test_sync_vmsa(type, policy);
  189. if (type == KVM_X86_SNP_VM)
  190. test_sync_vmsa(type, policy | SNP_POLICY_DBG);
  191. else
  192. test_sync_vmsa(type, policy | SEV_POLICY_NO_DBG);
  193. }
  194. }
  195. int main(int argc, char *argv[])
  196. {
  197. TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
  198. test_sev_smoke(guest_sev_code, KVM_X86_SEV_VM, 0);
  199. if (kvm_cpu_has(X86_FEATURE_SEV_ES))
  200. test_sev_smoke(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES);
  201. if (kvm_cpu_has(X86_FEATURE_SEV_SNP))
  202. test_sev_smoke(guest_snp_code, KVM_X86_SNP_VM, snp_default_policy());
  203. return 0;
  204. }