Skip to content

Commit be250ff

Browse files
pgondasean-jc
authored andcommitted
KVM: selftests: Add a basic SEV smoke test
Add a basic smoke test for SEV guests to verify that KVM can launch an SEV guest and run a few instructions without exploding. To verify that SEV is indeed enabled, assert that SEV is reported as enabled in MSR_AMD64_SEV, a.k.a. SEV_STATUS, which cannot be intercepted by KVM (architecturally enforced). Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Vishal Annapurve <vannapurve@google.com> Cc: Ackerly Tng <ackerleytng@google.com> cc: Andrew Jones <andrew.jones@linux.dev> Cc: Tom Lendacky <thomas.lendacky@amd.com> Cc: Michael Roth <michael.roth@amd.com> Suggested-by: Michael Roth <michael.roth@amd.com> Tested-by: Carlos Bilbao <carlos.bilbao@amd.com> Signed-off-by: Peter Gonda <pgonda@google.com> [sean: rename to "sev_smoke_test"] Link: https://lore.kernel.org/r/20240223004258.3104051-11-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 69f8e15 commit be250ff

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

tools/testing/selftests/kvm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_caps_test
118118
TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test
119119
TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test
120120
TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests
121+
TEST_GEN_PROGS_x86_64 += x86_64/sev_smoke_test
121122
TEST_GEN_PROGS_x86_64 += x86_64/amx_test
122123
TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test
123124
TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
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+
15+
static void guest_sev_code(void)
16+
{
17+
GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV));
18+
GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED);
19+
20+
GUEST_DONE();
21+
}
22+
23+
static void test_sev(void *guest_code, uint64_t policy)
24+
{
25+
struct kvm_vcpu *vcpu;
26+
struct kvm_vm *vm;
27+
struct ucall uc;
28+
29+
vm = vm_sev_create_with_one_vcpu(policy, guest_code, &vcpu);
30+
31+
for (;;) {
32+
vcpu_run(vcpu);
33+
34+
switch (get_ucall(vcpu, &uc)) {
35+
case UCALL_SYNC:
36+
continue;
37+
case UCALL_DONE:
38+
return;
39+
case UCALL_ABORT:
40+
REPORT_GUEST_ASSERT(uc);
41+
default:
42+
TEST_FAIL("Unexpected exit: %s",
43+
exit_reason_str(vcpu->run->exit_reason));
44+
}
45+
}
46+
47+
kvm_vm_free(vm);
48+
}
49+
50+
int main(int argc, char *argv[])
51+
{
52+
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
53+
54+
test_sev(guest_sev_code, SEV_POLICY_NO_DBG);
55+
test_sev(guest_sev_code, 0);
56+
57+
return 0;
58+
}

0 commit comments

Comments
 (0)