[kvm-unit-tests PATCH v4 3/8] s390x: Add sck tests

Nico Boehr
2022-02-24T15:44:03Z

SCK updates the guest's TOD clock. It needs to be intercepted, hence we
should have some tests for it:

- its operand must be 8-byte aligned. We test for 1, 2 and 4 byte
misalignment.
- it should actually update the clock value. As guests are unable to
stop their clock, we can only check the clock value is at least higher
than the value we set.
- it is a privileged call, so expect it to fail when in problem state.
We also test the clock value is not changed in this case. We do so by first
adjusting it (privileged) to a high value. We then attempt, in
nonprivileged mode, to set the clock to a comparatively small value.
We can then store the clock value and test its value is strictly higher
than the high value we set previously.

SCK is currently broken in QEMU TCG. A fix was posted upstream ("s390x: sck:
load into a temporary not into in1"):
https://lore.kernel.org/qemu-devel/20220126084201.774457-1-nrb@linux.ibm.com/

Signed-off-by: Nico Boehr <nrb@linux.ibm.com>
Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Reviewed-by: Pierre Morel <pmorel@linux.ibm.com>
---
s390x/Makefile | 1 +
s390x/sck.c | 136 ++++++++++++++++++++++++++++++++++++++++++++
s390x/unittests.cfg | 3 +
3 files changed, 140 insertions(+)
create mode 100644 s390x/sck.c

diff --git a/s390x/Makefile b/s390x/Makefile
index 53b0fe044fe7..a76b78e5a011 100644
--- a/s390x/Makefile
+++ b/s390x/Makefile
@@ -17,6 +17,7 @@ tests += $(TEST_DIR)/stsi.elf
tests += $(TEST_DIR)/skrf.elf
tests += $(TEST_DIR)/smp.elf
tests += $(TEST_DIR)/sclp.elf
+tests += $(TEST_DIR)/sck.elf
tests += $(TEST_DIR)/css.elf
tests += $(TEST_DIR)/uv-guest.elf
tests += $(TEST_DIR)/sie.elf
diff --git a/s390x/sck.c b/s390x/sck.c
new file mode 100644
index 000000000000..88d52b74a586
--- /dev/null
+++ b/s390x/sck.c
@@ -0,0 +1,136 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Perform Set Clock tests
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ * Nico Boehr <nrb@linux.ibm.com>
+ */
+#include <libcflat.h>
+#include <uv.h>
+#include <asm/interrupt.h>
+#include <asm/time.h>
+
+static inline int sck(uint64_t *time)
+{
+ int cc;
+
+ asm volatile(
+ " sck %[time]\n"
+ " ipm %[cc]\n"
+ " srl %[cc],28\n"
+ : [cc] "=d"(cc)
+ : [time] "Q"(*time)
+ : "cc"
+ );
+
+ return cc;
+}
+
+static inline int stck(uint64_t *time)
+{
+ int cc;
+
+ asm volatile(
+ " stck %[time]\n"
+ " ipm %[cc]\n"
+ " srl %[cc],28\n"
+ : [cc] "=d" (cc), [time] "=Q" (*time)
+ :
+ : "cc", "memory"
+ );
+
+ return cc;
+}
+
+static void test_priv(void)
+{
+ uint64_t time_to_set_privileged = 0xfacef00dcafe0000,
+ time_to_set_nonprivileged = 0xcafe0000,
+ time_verify;
+ int cc;
+
+ report_prefix_push("privileged");
+ cc = sck(&time_to_set_privileged);
+ report(!cc, "set clock cc=%d", cc);
+
+ cc = stck(&time_verify);
+ report(!cc, "store clock cc=%d", cc);
+ report(time_verify > time_to_set_privileged,
+ "privileged set affected the clock");
+ report_prefix_pop();
+
+ report_prefix_push("unprivileged");
+ expect_pgm_int();
+ enter_pstate();
+ sck(&time_to_set_nonprivileged);
+ leave_pstate();
+ check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION);
+
+ cc = stck(&time_verify);
+ report(!cc, "store clock cc=%d", cc);
+ report(time_verify > time_to_set_privileged,
+ "unprivileged set did not affect the clock");
+ report_prefix_pop();
+}
+
+static void test_align(void)
+{
+ const int align_to = 8;
+ char unalign[sizeof(uint64_t) + align_to] __attribute__((aligned(8)));
+
+ report_prefix_push("Unaligned operand");
+ for (int i = 1; i < align_to; i *= 2) {
+ report_prefix_pushf("%d", i);
+ expect_pgm_int();
+ sck((uint64_t *)(unalign + i));
+ check_pgm_int_code(PGM_INT_CODE_SPECIFICATION);
+ report_prefix_pop();
+ }
+ report_prefix_pop();
+}
+
+static void test_set(void)
+{
+ uint64_t start = 0, end = 0, time = 0xcafef00dbeef;
+ const uint64_t ticks_per_ms = 1000 << 12, ms_to_wait = 5;
+ int cc;
+
+ report_prefix_push("set");
+
+ cc = sck(&time);
+ report(!cc, "set clock cc=%d", cc);
+
+ cc = stck(&start);
+ report(!cc, "store start clock cc=%d", cc);
+ report(start >= time, "start >= set value");
+
+ mdelay(ms_to_wait);
+
+ cc = stck(&end);
+ report(!cc, "store end clock cc=%d", cc);
+ report(end > time, "end > set value");
+
+ report(end - start > (ticks_per_ms * ms_to_wait), "Advances");
+
+ report_prefix_pop();
+}
+
+int main(void)
+{
+ report_prefix_push("sck");
+
+ if (uv_os_is_guest()) {
+ report_skip("Test unsupported under PV");
+ goto out;
+ }
+
+ test_align();
+ test_set();
+ test_priv();
+
+out:
+ report_prefix_pop();
+ return report_summary();
+}
diff --git a/s390x/unittests.cfg b/s390x/unittests.cfg
index 1600e714c8b9..8b148fe31ac0 100644
--- a/s390x/unittests.cfg
+++ b/s390x/unittests.cfg
@@ -136,3 +136,6 @@ file = firq.elf
timeout = 20
extra_params = -smp 1,maxcpus=3 -cpu qemu -device qemu-s390x-cpu,core-id=2 -device qemu-s390x-cpu,core-id=1
accel = tcg
+
+[sck]
+file = sck.elf
--
2.31.1