Skip to content

Commit a5d55f7

Browse files
pratiksampatsean-jc
authored andcommitted
KVM: selftests: Decouple SEV policy from VM type
In preparation for SNP, cleanup the smoke test to decouple deriving type from policy. This will allow reusing the existing interfaces for SNP. No functional change intended. Signed-off-by: Pratik R. Sampat <[email protected]> Link: https://lore.kernel.org/r/[email protected] [sean: massage shortlog+changelog] Signed-off-by: Sean Christopherson <[email protected]>
1 parent b73a30c commit a5d55f7

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

tools/testing/selftests/kvm/x86/sev_smoke_test.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void compare_xsave(u8 *from_host, u8 *from_guest)
6262
abort();
6363
}
6464

65-
static void test_sync_vmsa(uint32_t policy)
65+
static void test_sync_vmsa(uint32_t type, uint64_t policy)
6666
{
6767
struct kvm_vcpu *vcpu;
6868
struct kvm_vm *vm;
@@ -72,7 +72,7 @@ static void test_sync_vmsa(uint32_t policy)
7272
double x87val = M_PI;
7373
struct kvm_xsave __attribute__((aligned(64))) xsave = { 0 };
7474

75-
vm = vm_sev_create_with_one_vcpu(KVM_X86_SEV_ES_VM, guest_code_xsave, &vcpu);
75+
vm = vm_sev_create_with_one_vcpu(type, guest_code_xsave, &vcpu);
7676
gva = vm_vaddr_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR,
7777
MEM_REGION_TEST_DATA);
7878
hva = addr_gva2hva(vm, gva);
@@ -89,7 +89,7 @@ static void test_sync_vmsa(uint32_t policy)
8989
: "ymm4", "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)");
9090
vcpu_xsave_set(vcpu, &xsave);
9191

92-
vm_sev_launch(vm, SEV_POLICY_ES | policy, NULL);
92+
vm_sev_launch(vm, policy, NULL);
9393

9494
/* This page is shared, so make it decrypted. */
9595
memset(hva, 0, 4096);
@@ -108,14 +108,12 @@ static void test_sync_vmsa(uint32_t policy)
108108
kvm_vm_free(vm);
109109
}
110110

111-
static void test_sev(void *guest_code, uint64_t policy)
111+
static void test_sev(void *guest_code, uint32_t type, uint64_t policy)
112112
{
113113
struct kvm_vcpu *vcpu;
114114
struct kvm_vm *vm;
115115
struct ucall uc;
116116

117-
uint32_t type = policy & SEV_POLICY_ES ? KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM;
118-
119117
vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu);
120118

121119
/* TODO: Validate the measurement is as expected. */
@@ -161,16 +159,14 @@ static void guest_shutdown_code(void)
161159
__asm__ __volatile__("ud2");
162160
}
163161

164-
static void test_sev_es_shutdown(void)
162+
static void test_sev_shutdown(uint32_t type, uint64_t policy)
165163
{
166164
struct kvm_vcpu *vcpu;
167165
struct kvm_vm *vm;
168166

169-
uint32_t type = KVM_X86_SEV_ES_VM;
170-
171167
vm = vm_sev_create_with_one_vcpu(type, guest_shutdown_code, &vcpu);
172168

173-
vm_sev_launch(vm, SEV_POLICY_ES, NULL);
169+
vm_sev_launch(vm, policy, NULL);
174170

175171
vcpu_run(vcpu);
176172
TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SHUTDOWN,
@@ -180,27 +176,33 @@ static void test_sev_es_shutdown(void)
180176
kvm_vm_free(vm);
181177
}
182178

183-
int main(int argc, char *argv[])
179+
static void test_sev_smoke(void *guest, uint32_t type, uint64_t policy)
184180
{
185181
const u64 xf_mask = XFEATURE_MASK_X87_AVX;
186182

187-
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
188-
189-
test_sev(guest_sev_code, SEV_POLICY_NO_DBG);
190-
test_sev(guest_sev_code, 0);
183+
test_sev(guest, type, policy | SEV_POLICY_NO_DBG);
184+
test_sev(guest, type, policy);
191185

192-
if (kvm_cpu_has(X86_FEATURE_SEV_ES)) {
193-
test_sev(guest_sev_es_code, SEV_POLICY_ES | SEV_POLICY_NO_DBG);
194-
test_sev(guest_sev_es_code, SEV_POLICY_ES);
186+
if (type == KVM_X86_SEV_VM)
187+
return;
195188

196-
test_sev_es_shutdown();
189+
test_sev_shutdown(type, policy);
197190

198-
if (kvm_has_cap(KVM_CAP_XCRS) &&
199-
(xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) == xf_mask) {
200-
test_sync_vmsa(0);
201-
test_sync_vmsa(SEV_POLICY_NO_DBG);
202-
}
191+
if (kvm_has_cap(KVM_CAP_XCRS) &&
192+
(xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) == xf_mask) {
193+
test_sync_vmsa(type, policy);
194+
test_sync_vmsa(type, policy | SEV_POLICY_NO_DBG);
203195
}
196+
}
197+
198+
int main(int argc, char *argv[])
199+
{
200+
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
201+
202+
test_sev_smoke(guest_sev_code, KVM_X86_SEV_VM, 0);
203+
204+
if (kvm_cpu_has(X86_FEATURE_SEV_ES))
205+
test_sev_smoke(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES);
204206

205207
return 0;
206208
}

0 commit comments

Comments
 (0)