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 <schlame...@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


Reply via email to