public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Christoph Schlameuss <schlameuss@linux.ibm.com>
To: kvm@vger.kernel.org
Cc: linux-s390@vger.kernel.org, linux-kselftest@vger.kernel.org,
	Paolo Bonzini <pbonzini@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Janosch Frank <frankja@linux.ibm.com>,
	Claudio Imbrenda <imbrenda@linux.ibm.com>,
	David Hildenbrand <david@redhat.com>,
	Nina Schoetterl-Glausch <nsg@linux.ibm.com>
Subject: [PATCH v2 06/10] selftests: kvm: s390: Add VM run test case
Date: Tue, 23 Jul 2024 11:31:22 +0200	[thread overview]
Message-ID: <20240723093126.285319-7-schlameuss@linux.ibm.com> (raw)
In-Reply-To: <20240723093126.285319-1-schlameuss@linux.ibm.com>

Add test case running code interacting with registers within a
ucontrol VM.

* Add uc_gprs test case

The test uses the same VM setup using the fixture and debug macros
introduced in earlier patches in this series.

Signed-off-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
---
 .../selftests/kvm/s390x/ucontrol_test.c       | 132 ++++++++++++++++++
 1 file changed, 132 insertions(+)

diff --git a/tools/testing/selftests/kvm/s390x/ucontrol_test.c b/tools/testing/selftests/kvm/s390x/ucontrol_test.c
index 527c431a9758..c98d5a3a315b 100644
--- a/tools/testing/selftests/kvm/s390x/ucontrol_test.c
+++ b/tools/testing/selftests/kvm/s390x/ucontrol_test.c
@@ -43,6 +43,23 @@ void require_ucontrol_admin(void)
 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_S390_UCONTROL));
 }
 
+/* Test program setting some registers and looping */
+extern char test_gprs_pgm[];
+asm("test_gprs_pgm:\n"
+	"xgr	%r0, %r0\n"
+	"lgfi	%r1,1\n"
+	"lgfi	%r2,2\n"
+	"lgfi	%r3,3\n"
+	"lgfi	%r4,4\n"
+	"lgfi	%r5,5\n"
+	"lgfi	%r6,6\n"
+	"lgfi	%r7,7\n"
+	"0:\n"
+	"	diag	0,0,0x44\n"
+	"	ahi	%r0,1\n"
+	"	j	0b\n"
+);
+
 FIXTURE(uc_kvm)
 {
 	struct kvm_s390_sie_block *sie_block;
@@ -205,4 +222,119 @@ TEST(uc_cap_hpage)
 	close(kvm_fd);
 }
 
+/* verify SIEIC exit
+ * * reset stop requests
+ * * fail on codes not expected in the test cases
+ */
+static bool uc_handle_sieic(FIXTURE_DATA(uc_kvm) * self)
+{
+	struct kvm_s390_sie_block *sie_block = self->sie_block;
+	struct kvm_run *run = self->run;
+
+	/* check SIE interception code */
+	pr_info("sieic: 0x%2x 0x%4x 0x%4x\n",
+		run->s390_sieic.icptcode,
+		run->s390_sieic.ipa,
+		run->s390_sieic.ipb);
+	switch (run->s390_sieic.icptcode) {
+	case ICPT_STOP:
+		/* stopped via sie V P --> ignore */
+		/* reset stop request */
+		sie_block->cpuflags = sie_block->cpuflags & ~CPUSTAT_STOP_INT;
+		pr_info("sie V P - cleared %.4x\n", sie_block->cpuflags);
+		break;
+	case ICPT_INST:
+		/* end execution in caller on intercepted instruction */
+		return false;
+	default:
+		TEST_FAIL("UNEXPECTED SIEIC CODE %d", run->s390_sieic.icptcode);
+	}
+	return true;
+}
+
+/* verify VM state on exit */
+static bool uc_handle_exit(FIXTURE_DATA(uc_kvm) * self)
+{
+	struct kvm_run *run = self->run;
+
+	switch (run->exit_reason) {
+	case KVM_EXIT_S390_SIEIC:
+		return uc_handle_sieic(self);
+	default:
+		pr_info("exit_reason %2d not handled\n", run->exit_reason);
+	}
+	return true;
+}
+
+/* run the VM until interrupted */
+static int uc_run_once(FIXTURE_DATA(uc_kvm) * self)
+{
+	int rc;
+
+	rc = ioctl(self->vcpu_fd, KVM_RUN, NULL);
+	print_run(self->run, self->sie_block);
+	print_regs(self->run);
+	pr_debug("run %d / %d %s\n", rc, errno, strerror(errno));
+	return rc;
+}
+
+static void uc_assert_diag44(FIXTURE_DATA(uc_kvm) * self)
+{
+	struct kvm_s390_sie_block *sie_block = self->sie_block;
+
+	/* assert vm was interrupted by diag 0x0044 */
+	TEST_ASSERT_EQ(KVM_EXIT_S390_SIEIC, self->run->exit_reason);
+	TEST_ASSERT_EQ(ICPT_INST, sie_block->icptcode);
+	TEST_ASSERT_EQ(0x8300, sie_block->ipa);
+	TEST_ASSERT_EQ(0x440000, sie_block->ipb);
+}
+
+TEST_F(uc_kvm, uc_gprs)
+{
+	struct kvm_s390_sie_block *sie_block = self->sie_block;
+	struct kvm_sync_regs *sync_regs = &self->run->s.regs;
+	struct kvm_run *run = self->run;
+	struct kvm_regs regs = {};
+
+	/* Set registers to values that are different from the ones that we expect below */
+	for (int i = 0; i < 8; i++)
+		sync_regs->gprs[i] = 8;
+	run->kvm_dirty_regs |= KVM_SYNC_GPRS;
+
+	/* copy test_gprs_pgm to code_hva / code_gpa */
+	TH_LOG("copy code %p to vm mapped memory %p / %p",
+	       &test_gprs_pgm, (void *)self->code_hva, (void *)self->code_gpa);
+	memcpy((void *)self->code_hva, &test_gprs_pgm, PAGE_SIZE);
+
+	run->psw_mask = 0x0000000180000000ULL;  /* DAT disabled + 64 bit mode */
+	run->psw_addr = self->code_gpa;
+
+	/* run VM and expect immediate stop */
+	ASSERT_EQ(0, uc_run_once(self));
+	ASSERT_EQ(true, uc_handle_exit(self));
+	ASSERT_EQ(self->code_gpa, sie_block->psw_addr);
+
+	/* run and expect interception of diag 44 */
+	ASSERT_EQ(0, uc_run_once(self));
+	ASSERT_EQ(false, uc_handle_exit(self));
+	uc_assert_diag44(self);
+
+	/* Retrieve and check guest register values */
+	ASSERT_EQ(0, ioctl(self->vcpu_fd, KVM_GET_REGS, &regs));
+	for (int i = 0; i < 8; i++) {
+		ASSERT_EQ(i, regs.gprs[i]);
+		ASSERT_EQ(i, sync_regs->gprs[i]);
+	}
+
+	/* run and expect interception of diag 44 again */
+	ASSERT_EQ(0, uc_run_once(self));
+	ASSERT_EQ(false, uc_handle_exit(self));
+	uc_assert_diag44(self);
+
+	/* check continued increment of register 0 value */
+	ASSERT_EQ(0, ioctl(self->vcpu_fd, KVM_GET_REGS, &regs));
+	ASSERT_EQ(1, regs.gprs[0]);
+	ASSERT_EQ(1, sync_regs->gprs[0]);
+}
+
 TEST_HARNESS_MAIN
-- 
2.45.2


  parent reply	other threads:[~2024-07-23  9:32 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-23  9:31 [PATCH v2 00/10] selftests: kvm: s390: Add s390x ucontrol selftests Christoph Schlameuss
2024-07-23  9:31 ` [PATCH v2 01/10] selftests: kvm: s390: Define page sizes in shared header Christoph Schlameuss
2024-07-24 14:31   ` Janosch Frank
2024-07-23  9:31 ` [PATCH v2 02/10] selftests: kvm: s390: Add kvm_s390_sie_block definition for userspace tests Christoph Schlameuss
2024-07-24 14:39   ` Janosch Frank
2024-07-24 16:07     ` Christoph Schlameuss
2024-07-23  9:31 ` [PATCH v2 03/10] selftests: kvm: s390: Add s390x ucontrol test suite with hpage test Christoph Schlameuss
2024-07-24 14:52   ` Janosch Frank
2024-07-24 16:18     ` Christoph Schlameuss
2024-07-23  9:31 ` [PATCH v2 04/10] selftests: kvm: s390: Add test fixture and simple VM setup tests Christoph Schlameuss
2024-07-23  9:31 ` [PATCH v2 05/10] selftests: kvm: s390: Add debug print functions Christoph Schlameuss
2024-07-23  9:31 ` Christoph Schlameuss [this message]
2024-07-25 12:03   ` [PATCH v2 06/10] selftests: kvm: s390: Add VM run test case Janosch Frank
2024-07-25 14:07     ` Christoph Schlameuss
2024-07-23  9:31 ` [PATCH v2 07/10] selftests: kvm: s390: Add uc_map_unmap VM " Christoph Schlameuss
2024-07-23  9:31 ` [PATCH v2 08/10] selftests: kvm: s390: Add uc_skey " Christoph Schlameuss
2024-07-23  9:31 ` [PATCH v2 09/10] selftests: kvm: s390: Verify reject memory region operations for ucontrol VMs Christoph Schlameuss
2024-07-23  9:31 ` [PATCH v2 10/10] s390: Enable KVM_S390_UCONTROL config in debug_defconfig Christoph Schlameuss
2024-07-23  9:40   ` Christian Borntraeger
2024-07-23 12:48     ` Christoph Schlameuss

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=20240723093126.285319-7-schlameuss@linux.ibm.com \
    --to=schlameuss@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=david@redhat.com \
    --cc=frankja@linux.ibm.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=nsg@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=shuah@kernel.org \
    /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