Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: "Pratik R. Sampat" <pratikrajesh.sampat@amd.com>
To: <kvm@vger.kernel.org>
Cc: <seanjc@google.com>, <pbonzini@redhat.com>, <pgonda@google.com>,
	<thomas.lendacky@amd.com>, <michael.roth@amd.com>,
	<shuah@kernel.org>, <linux-kselftest@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCH v3 4/9] KVM: selftests: SEV IOCTL test
Date: Thu, 5 Sep 2024 07:41:02 -0500	[thread overview]
Message-ID: <20240905124107.6954-5-pratikrajesh.sampat@amd.com> (raw)
In-Reply-To: <20240905124107.6954-1-pratikrajesh.sampat@amd.com>

Introduce tests for sev and sev-es ioctl that exercises the boot path
of launch, update and finish on an invalid policy.

Signed-off-by: Pratik R. Sampat <pratikrajesh.sampat@amd.com>
Tested-by: Peter Gonda <pgonda@google.com>
Tested-by: Srikanth Aithal <sraithal@amd.com>
---
 .../selftests/kvm/x86_64/sev_smoke_test.c     | 84 +++++++++++++++++++
 1 file changed, 84 insertions(+)

diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c
index 8e798f5a2a53..5fa4ee27609b 100644
--- a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c
+++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c
@@ -142,12 +142,96 @@ static void test_sync_vmsa(uint32_t type, uint64_t policy)
 	kvm_vm_free(vm);
 }
 
+static void sev_guest_neg_status_assert(struct kvm_vm *vm, uint32_t type)
+{
+	struct kvm_sev_guest_status status;
+	int ret;
+
+	ret = __vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status);
+	TEST_ASSERT(ret, "KVM_SEV_GUEST_STATUS should fail, invalid VM Type.");
+}
+
+static void vm_sev_es_launch_neg(struct kvm_vm *vm, uint32_t type, uint64_t policy)
+{
+	int ret;
+
+	/* Launch start with policy SEV_POLICY_NO_DBG (0x0) */
+	ret = __sev_vm_launch_start(vm, 0);
+	TEST_ASSERT(ret,
+		    "KVM_SEV_LAUNCH_START should fail due to type (%d) - policy(0x0) mismatch",
+		    type);
+
+	ret = __sev_vm_launch_update(vm, policy);
+	TEST_ASSERT(ret,
+		    "KVM_SEV_LAUNCH_UPDATE should fail due to LAUNCH_START. type: %d policy: 0x%lx",
+		    type, policy);
+	sev_guest_neg_status_assert(vm, type);
+
+	ret = __sev_vm_launch_measure(vm, alloca(256));
+	TEST_ASSERT(ret,
+		    "KVM_SEV_LAUNCH_UPDATE should fail due to LAUNCH_START. type: %d policy: 0x%lx",
+		    type, policy);
+	sev_guest_neg_status_assert(vm, type);
+
+	ret = __sev_vm_launch_finish(vm);
+	TEST_ASSERT(ret,
+		    "KVM_SEV_LAUNCH_UPDATE should fail due to LAUNCH_START. type: %d policy: 0x%lx",
+		    type, policy);
+	sev_guest_neg_status_assert(vm, type);
+}
+
+/*
+ * Test for SEV ioctl launch path
+ * VMs of the type SEV and SEV-ES are created, however they are launched with
+ * an empty policy to observe the effect on the control flow of launching a VM.
+ *
+ * SEV - Expected to pass through the path of launch start, update, measure,
+ * and finish. vcpu_run expected to fail with error KVM_EXIT_IO.
+ *
+ * SEV-ES - Expected to fail the launch start as vm created with type
+ * KVM_X86_DEFAULT_VM but policy passed to launch start is KVM_X86_SEV_ES_VM.
+ * Post this, calls that pass the correct policy to update, measure, and finish
+ * are also expected to fail cascading.
+ */
+static void test_sev_launch(void *guest_code, uint32_t type, uint64_t policy)
+{
+	struct kvm_vcpu *vcpu;
+	int exp_exit_reason;
+	struct kvm_vm *vm;
+	struct ucall uc;
+
+	vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu);
+
+	if (type == KVM_X86_SEV_VM) {
+		sev_vm_launch(vm, 0);
+		sev_vm_launch_measure(vm, alloca(256));
+		sev_vm_launch_finish(vm);
+	} else {
+		vm_sev_es_launch_neg(vm, type, policy);
+	}
+
+	vcpu_run(vcpu);
+	get_ucall(vcpu, &uc);
+	if (type == KVM_X86_SEV_VM)
+		exp_exit_reason = KVM_EXIT_IO;
+	else
+		exp_exit_reason = KVM_EXIT_FAIL_ENTRY;
+
+	TEST_ASSERT(vcpu->run->exit_reason == exp_exit_reason,
+		    "vcpu_run failed exit expected: %d, got: %d",
+		    exp_exit_reason, vcpu->run->exit_reason);
+
+	kvm_vm_free(vm);
+}
+
 static void test_sev(void *guest_code, uint32_t type, uint64_t policy)
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 	struct ucall uc;
 
+	test_sev_launch(guest_code, type, policy);
+
 	vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu);
 
 	/* TODO: Validate the measurement is as expected. */
-- 
2.34.1


  parent reply	other threads:[~2024-09-05 12:42 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-05 12:40 [PATCH v3 0/9] SEV Kernel Selftests Pratik R. Sampat
2024-09-05 12:40 ` [PATCH v3 1/9] KVM: selftests: Decouple SEV ioctls from asserts Pratik R. Sampat
2024-10-14 22:18   ` Sean Christopherson
2024-10-21 20:23     ` Pratik R. Sampat
2024-09-05 12:41 ` [PATCH v3 2/9] KVM: selftests: Add a basic SNP smoke test Pratik R. Sampat
2024-10-14 22:46   ` Sean Christopherson
2024-10-21 20:23     ` Pratik R. Sampat
2024-10-28 17:55       ` Sean Christopherson
2024-10-28 20:41         ` Pratik R. Sampat
2024-10-30 13:46           ` Sean Christopherson
2024-10-30 16:35             ` Pratik R. Sampat
2024-10-30 17:57               ` Sean Christopherson
2024-10-31 15:45                 ` Pratik R. Sampat
2024-10-31 16:27                   ` Sean Christopherson
2024-11-04 20:21                     ` Pratik R. Sampat
2024-11-04 23:47                       ` Sean Christopherson
2024-11-05  4:14                         ` Pratik R. Sampat
2024-09-05 12:41 ` [PATCH v3 3/9] KVM: selftests: Add SNP to shutdown testing Pratik R. Sampat
2024-09-05 12:41 ` Pratik R. Sampat [this message]
2024-09-05 12:41 ` [PATCH v3 5/9] KVM: selftests: SNP IOCTL test Pratik R. Sampat
2024-09-05 12:41 ` [PATCH v3 6/9] KVM: selftests: SEV-SNP test for KVM_SEV_INIT2 Pratik R. Sampat
2024-09-05 12:41 ` [PATCH v3 7/9] KVM: selftests: Add interface to manually flag protected/encrypted ranges Pratik R. Sampat
2024-10-14 22:58   ` Sean Christopherson
2024-10-21 20:23     ` Pratik R. Sampat
2024-09-05 12:41 ` [PATCH v3 8/9] KVM: selftests: Add a CoCo-specific test for KVM_PRE_FAULT_MEMORY Pratik R. Sampat
2024-09-05 12:41 ` [PATCH v3 9/9] KVM: selftests: Interleave fallocate " Pratik R. Sampat
2024-10-14 22:23 ` [PATCH v3 0/9] SEV Kernel Selftests Sean Christopherson
2024-10-21 20:23   ` Pratik R. Sampat

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240905124107.6954-5-pratikrajesh.sampat@amd.com \
    --to=pratikrajesh.sampat@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=michael.roth@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=pgonda@google.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=thomas.lendacky@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox