1 // SPDX-License-Identifier: GPL-2.0-only
11 #include "processor.h"
13 #include "linux/psp-sev.h"
17 #define XFEATURE_MASK_X87_AVX (XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM)
19 static void guest_sev_es_code(void)
21 /* TODO: Check CPUID after GHCB-based hypercall support is added. */
22 GUEST_ASSERT(rdmsr(MSR_AMD64_SEV
) & MSR_AMD64_SEV_ENABLED
);
23 GUEST_ASSERT(rdmsr(MSR_AMD64_SEV
) & MSR_AMD64_SEV_ES_ENABLED
);
26 * TODO: Add GHCB and ucall support for SEV-ES guests. For now, simply
27 * force "termination" to signal "done" via the GHCB MSR protocol.
29 wrmsr(MSR_AMD64_SEV_ES_GHCB
, GHCB_MSR_TERM_REQ
);
30 __asm__
__volatile__("rep; vmmcall");
33 static void guest_sev_code(void)
35 GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV
));
36 GUEST_ASSERT(rdmsr(MSR_AMD64_SEV
) & MSR_AMD64_SEV_ENABLED
);
41 /* Stash state passed via VMSA before any compiled code runs. */
42 extern void guest_code_xsave(void);
43 asm("guest_code_xsave:\n"
44 "mov $" __stringify(XFEATURE_MASK_X87_AVX
) ", %eax\n"
47 "jmp guest_sev_es_code");
49 static void compare_xsave(u8
*from_host
, u8
*from_guest
)
53 for (i
= 0; i
< 4095; i
++) {
54 if (from_host
[i
] != from_guest
[i
]) {
55 printf("mismatch at %02hhx | %02hhx %02hhx\n", i
, from_host
[i
], from_guest
[i
]);
64 static void test_sync_vmsa(uint32_t policy
)
66 struct kvm_vcpu
*vcpu
;
72 struct kvm_xsave
__attribute__((aligned(64))) xsave
= { 0 };
74 vm
= vm_sev_create_with_one_vcpu(KVM_X86_SEV_ES_VM
, guest_code_xsave
, &vcpu
);
75 gva
= vm_vaddr_alloc_shared(vm
, PAGE_SIZE
, KVM_UTIL_MIN_VADDR
,
76 MEM_REGION_TEST_DATA
);
77 hva
= addr_gva2hva(vm
, gva
);
79 vcpu_args_set(vcpu
, 1, gva
);
82 "vpcmpeqb %%ymm4, %%ymm4, %%ymm4\n"
87 : "A"(XFEATURE_MASK_X87_AVX
), "r"(&xsave
), "m" (x87val
)
88 : "ymm4", "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)");
89 vcpu_xsave_set(vcpu
, &xsave
);
91 vm_sev_launch(vm
, SEV_POLICY_ES
| policy
, NULL
);
93 /* This page is shared, so make it decrypted. */
98 TEST_ASSERT(vcpu
->run
->exit_reason
== KVM_EXIT_SYSTEM_EVENT
,
99 "Wanted SYSTEM_EVENT, got %s",
100 exit_reason_str(vcpu
->run
->exit_reason
));
101 TEST_ASSERT_EQ(vcpu
->run
->system_event
.type
, KVM_SYSTEM_EVENT_SEV_TERM
);
102 TEST_ASSERT_EQ(vcpu
->run
->system_event
.ndata
, 1);
103 TEST_ASSERT_EQ(vcpu
->run
->system_event
.data
[0], GHCB_MSR_TERM_REQ
);
105 compare_xsave((u8
*)&xsave
, (u8
*)hva
);
110 static void test_sev(void *guest_code
, uint64_t policy
)
112 struct kvm_vcpu
*vcpu
;
116 uint32_t type
= policy
& SEV_POLICY_ES
? KVM_X86_SEV_ES_VM
: KVM_X86_SEV_VM
;
118 vm
= vm_sev_create_with_one_vcpu(type
, guest_code
, &vcpu
);
120 /* TODO: Validate the measurement is as expected. */
121 vm_sev_launch(vm
, policy
, NULL
);
126 if (policy
& SEV_POLICY_ES
) {
127 TEST_ASSERT(vcpu
->run
->exit_reason
== KVM_EXIT_SYSTEM_EVENT
,
128 "Wanted SYSTEM_EVENT, got %s",
129 exit_reason_str(vcpu
->run
->exit_reason
));
130 TEST_ASSERT_EQ(vcpu
->run
->system_event
.type
, KVM_SYSTEM_EVENT_SEV_TERM
);
131 TEST_ASSERT_EQ(vcpu
->run
->system_event
.ndata
, 1);
132 TEST_ASSERT_EQ(vcpu
->run
->system_event
.data
[0], GHCB_MSR_TERM_REQ
);
136 switch (get_ucall(vcpu
, &uc
)) {
142 REPORT_GUEST_ASSERT(uc
);
144 TEST_FAIL("Unexpected exit: %s",
145 exit_reason_str(vcpu
->run
->exit_reason
));
152 static void guest_shutdown_code(void)
156 /* Clobber the IDT so that #UD is guaranteed to trigger SHUTDOWN. */
157 memset(&idt
, 0, sizeof(idt
));
158 __asm__
__volatile__("lidt %0" :: "m"(idt
));
160 __asm__
__volatile__("ud2");
163 static void test_sev_es_shutdown(void)
165 struct kvm_vcpu
*vcpu
;
168 uint32_t type
= KVM_X86_SEV_ES_VM
;
170 vm
= vm_sev_create_with_one_vcpu(type
, guest_shutdown_code
, &vcpu
);
172 vm_sev_launch(vm
, SEV_POLICY_ES
, NULL
);
175 TEST_ASSERT(vcpu
->run
->exit_reason
== KVM_EXIT_SHUTDOWN
,
176 "Wanted SHUTDOWN, got %s",
177 exit_reason_str(vcpu
->run
->exit_reason
));
182 int main(int argc
, char *argv
[])
184 const u64 xf_mask
= XFEATURE_MASK_X87_AVX
;
186 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV
));
188 test_sev(guest_sev_code
, SEV_POLICY_NO_DBG
);
189 test_sev(guest_sev_code
, 0);
191 if (kvm_cpu_has(X86_FEATURE_SEV_ES
)) {
192 test_sev(guest_sev_es_code
, SEV_POLICY_ES
| SEV_POLICY_NO_DBG
);
193 test_sev(guest_sev_es_code
, SEV_POLICY_ES
);
195 test_sev_es_shutdown();
197 if (kvm_has_cap(KVM_CAP_XCRS
) &&
198 (xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask
) == xf_mask
) {
200 test_sync_vmsa(SEV_POLICY_NO_DBG
);