Skip to contentSkip to conversation

KVM: selftests: Add SEV-ES shutdown test

PATCH In review Peter Gonda 3 participants 5 messages Jul 9, 2024 – Aug 31, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: selftests: Add SEV-ES shutdown test

Regression test for ae20eef5 ("KVM: SVM: Update SEV-ES shutdown intercepts with more metadata"). Test confirms userspace is correctly indicated of a guest shutdown not previous behavior of an EINVAL from KVM_RUN.

Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Alper Gun <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
---
 .../selftests/kvm/x86_64/sev_smoke_test.c     | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)
tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +26 -0
index 7c70c0da4fb74..04f24d5f09877 100644
@@ -160,6 +160,30 @@ static void test_sev(void *guest_code, uint64_t policy)
 	kvm_vm_free(vm);
 }
 
+static void guest_shutdown_code(void)
+{
+	__asm__ __volatile__("ud2");
+}
+
+static void test_sev_es_shutdown(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	uint32_t type = KVM_X86_SEV_ES_VM;
+
+	vm = vm_sev_create_with_one_vcpu(type, guest_shutdown_code, &vcpu);
+
+	vm_sev_launch(vm, SEV_POLICY_ES, NULL);
+
+	vcpu_run(vcpu);
+	TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SHUTDOWN,
+		    "Wanted SHUTDOWN, got %s",
+		    exit_reason_str(vcpu->run->exit_reason));
+
+	kvm_vm_free(vm);
+}
+
 int main(int argc, char *argv[])
 {
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
@@ -171,6 +195,8 @@ int main(int argc, char *argv[])
 		test_sev(guest_sev_es_code, SEV_POLICY_ES | SEV_POLICY_NO_DBG);
 		test_sev(guest_sev_es_code, SEV_POLICY_ES);
 
+		test_sev_es_shutdown();
+
 		if (kvm_has_cap(KVM_CAP_XCRS) &&
 		    (xgetbv(0) & XFEATURE_MASK_X87_AVX) == XFEATURE_MASK_X87_AVX) {
 			test_sync_vmsa(0);
Signature · 1 line
2.45.2.803.g4e1b14247a-goog
Tested-by on v1 1/1
#1 lore raw

Hi Peter,

Quoted 4 lines · Peter Gonda

Regression test for ae20eef5 ("KVM: SVM: Update SEV-ES shutdown intercepts with more metadata"). Test confirms userspace is correctly indicated of a guest shutdown not previous behavior of an EINVAL from KVM_RUN.

Quoted 8 lines
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Alper Gun <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
Quoted 27 lines
---
 .../selftests/kvm/x86_64/sev_smoke_test.c     | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)
tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +14 -0
index 7c70c0da4fb74..04f24d5f09877 100644
@@ -160,6 +160,30 @@ static void test_sev(void *guest_code, uint64_t policy)
 	kvm_vm_free(vm);
 }
 
+static void guest_shutdown_code(void)
+{
+	__asm__ __volatile__("ud2");
+}
+
+static void test_sev_es_shutdown(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	uint32_t type = KVM_X86_SEV_ES_VM;
+
+	vm = vm_sev_create_with_one_vcpu(type, guest_shutdown_code, &vcpu);
+

tools/testing/selftests/kvm/x86_64/sev_smoke_test.c static void test_sev(void *guest_code, uint64_t policy) · line 186

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +10 -0
+	vm_sev_launch(vm, SEV_POLICY_ES, NULL);
+
+	vcpu_run(vcpu);
+	TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SHUTDOWN,
+		    "Wanted SHUTDOWN, got %s",
+		    exit_reason_str(vcpu->run->exit_reason));
+
+	kvm_vm_free(vm);
+}
+

I guess this case also applies to SNP. So maybe once this patch is queued up I could spin another patch in my SNP kselftest patch series that parameterizes this function to test SNP as well.

Thanks!
Pratik

Quoted 12 lines
tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +2 -0
 int main(int argc, char *argv[])
 {
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
@@ -171,6 +195,8 @@ int main(int argc, char *argv[])
 		test_sev(guest_sev_es_code, SEV_POLICY_ES | SEV_POLICY_NO_DBG);
 		test_sev(guest_sev_es_code, SEV_POLICY_ES);
 
+		test_sev_es_shutdown();
+
 		if (kvm_has_cap(KVM_CAP_XCRS) &&
 		    (xgetbv(0) & XFEATURE_MASK_X87_AVX) == XFEATURE_MASK_X87_AVX) {
 			test_sync_vmsa(0);
#2 lore raw
Quoted 5 lines

I guess this case also applies to SNP. So maybe once this patch is queued up I could spin another patch in my SNP kselftest patch series that parameterizes this function to test SNP as well.

Thanks! That sounds great. I plan on sending a few tests for the sev-es termination codes. I can base that on top of your SNP series.

#3 lore raw
In reply to Peter Gonda
Quoted 22 lines · Peter Gonda

Regression test for ae20eef5 ("KVM: SVM: Update SEV-ES shutdown intercepts with more metadata"). Test confirms userspace is correctly indicated of a guest shutdown not previous behavior of an EINVAL from KVM_RUN.

Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Alper Gun <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Michael Roth <[email protected]>
---
 .../selftests/kvm/x86_64/sev_smoke_test.c     | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)
tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +0 -0
index 7c70c0da4fb74..04f24d5f09877 100644

tools/testing/selftests/kvm/x86_64/sev_smoke_test.c static void test_sev(void *guest_code, uint64_t policy) · line 165

Quoted 6 lines
tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +3 -0
@@ -160,6 +160,30 @@ static void test_sev(void *guest_code, uint64_t policy)
 	kvm_vm_free(vm);
 }
 
+static void guest_shutdown_code(void)
+{
+	__asm__ __volatile__("ud2");

Heh, this passes by dumb luck, not because the #UD itself causes a SHUTDOWN. It _looks_ like the #UD causes a shutdown, because KVM will always see the original guest RIP, but the shutdown actually occurs somewhere in the ucall_assert() in route_exception().

Now that x86 selftests install an IDT and exception handlers by default, it's actually quite hard to induce shutdown. Ok, not "hard", but it requires more work than simply generating a #UD.

I'll add this as fixup when applying:

tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +6 -0
index 04f24d5f0987..2e9197eb1652 100644
@@ -162,6 +162,12 @@ static void test_sev(void *guest_code, uint64_t policy)
 
 static void guest_shutdown_code(void)
 {
+       struct desc_ptr idt;
+
+       /* Clobber the IDT so that #UD is guaranteed to trigger SHUTDOWN. */
+       memset(&idt, 0, sizeof(idt));
+       __asm__ __volatile__("lidt %0" :: "m"(idt));
+
        __asm__ __volatile__("ud2");
 }
#4 lore raw
In reply to Peter Gonda
Quoted 3 lines · Peter Gonda

Regression test for ae20eef5 ("KVM: SVM: Update SEV-ES shutdown intercepts with more metadata"). Test confirms userspace is correctly indicated of a guest shutdown not previous behavior of an EINVAL from KVM_RUN.

Applied to kvm-x86 selftests, with the IDT clobbering. Thanks!

[1/1] KVM: selftests: Add SEV-ES shutdown test

      https://github.com/kvm-x86/linux/commit/2f6fcfa1f426

--
https://github.com/kvm-x86/linux/tree/next