From: Thomas Huth <thuth@redhat.com>
To: qemu-devel@nongnu.org
Cc: Stefan Hajnoczi <stefanha@redhat.com>,
Ilya Leoshkevich <iii@linux.ibm.com>
Subject: [PULL 2/7] tests/tcg/s390x: Test precise self-modifying code handling
Date: Thu, 31 Aug 2023 21:17:14 +0200 [thread overview]
Message-ID: <20230831191719.140001-3-thuth@redhat.com> (raw)
In-Reply-To: <20230831191719.140001-1-thuth@redhat.com>
From: Ilya Leoshkevich <iii@linux.ibm.com>
Add small softmmu and user tests to prevent regressions.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Message-Id: <20230807114921.438881-2-iii@linux.ibm.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
tests/tcg/s390x/precise-smc-user.c | 39 +++++++++++++++
tests/tcg/s390x/Makefile.softmmu-target | 1 +
tests/tcg/s390x/Makefile.target | 1 +
tests/tcg/s390x/precise-smc-softmmu.S | 63 +++++++++++++++++++++++++
4 files changed, 104 insertions(+)
create mode 100644 tests/tcg/s390x/precise-smc-user.c
create mode 100644 tests/tcg/s390x/precise-smc-softmmu.S
diff --git a/tests/tcg/s390x/precise-smc-user.c b/tests/tcg/s390x/precise-smc-user.c
new file mode 100644
index 0000000000..33a5270865
--- /dev/null
+++ b/tests/tcg/s390x/precise-smc-user.c
@@ -0,0 +1,39 @@
+/*
+ * Test s390x-linux-user precise self-modifying code handling.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <assert.h>
+#include <sys/mman.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+extern __uint128_t __attribute__((__aligned__(1))) smc;
+extern __uint128_t __attribute__((__aligned__(1))) patch;
+
+int main(void)
+{
+ char *aligned_smc = (char *)((uintptr_t)&smc & ~0xFFFULL);
+ char *smc_end = (char *)&smc + sizeof(smc);
+ uint64_t value = 21;
+ int err;
+
+ err = mprotect(aligned_smc, smc_end - aligned_smc,
+ PROT_READ | PROT_WRITE | PROT_EXEC);
+ assert(err == 0);
+
+ asm("jg 0f\n" /* start a new TB */
+ "patch: .byte 0,0,0,0,0,0\n" /* replaces padding */
+ ".byte 0,0,0,0,0,0\n" /* replaces vstl */
+ "agr %[value],%[value]\n" /* replaces sgr */
+ "smc: .org . + 6\n" /* pad patched code to 16 bytes */
+ "0: vstl %[patch],%[idx],%[smc]\n" /* start writing before TB */
+ "sgr %[value],%[value]" /* this becomes `agr %r0,%r0` */
+ : [smc] "=R" (smc)
+ , [value] "+r" (value)
+ : [patch] "v" (patch)
+ , [idx] "r" (sizeof(patch) - 1)
+ : "cc");
+
+ return value == 42 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
diff --git a/tests/tcg/s390x/Makefile.softmmu-target b/tests/tcg/s390x/Makefile.softmmu-target
index 76345b6e64..1a1f088b28 100644
--- a/tests/tcg/s390x/Makefile.softmmu-target
+++ b/tests/tcg/s390x/Makefile.softmmu-target
@@ -25,6 +25,7 @@ ASM_TESTS = \
lpswe-early \
lra \
mc \
+ precise-smc-softmmu \
ssm-early \
stosm-early \
stpq \
diff --git a/tests/tcg/s390x/Makefile.target b/tests/tcg/s390x/Makefile.target
index 9c0e70c6ca..c650aefe5c 100644
--- a/tests/tcg/s390x/Makefile.target
+++ b/tests/tcg/s390x/Makefile.target
@@ -61,6 +61,7 @@ Z13_TESTS+=locfhr
Z13_TESTS+=vcksm
Z13_TESTS+=vstl
Z13_TESTS+=vrep
+Z13_TESTS+=precise-smc-user
$(Z13_TESTS): CFLAGS+=-march=z13 -O2
TESTS+=$(Z13_TESTS)
diff --git a/tests/tcg/s390x/precise-smc-softmmu.S b/tests/tcg/s390x/precise-smc-softmmu.S
new file mode 100644
index 0000000000..f7fa57d899
--- /dev/null
+++ b/tests/tcg/s390x/precise-smc-softmmu.S
@@ -0,0 +1,63 @@
+/*
+ * Test s390x-softmmu precise self-modifying code handling.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+ .org 0x8e
+program_interruption_code:
+ .org 0x150
+program_old_psw:
+ .org 0x1D0 /* program new PSW */
+ .quad 0x180000000,pgm /* 64-bit mode */
+ .org 0x200 /* lowcore padding */
+ .globl _start
+_start:
+ lctlg %c0,%c0,c0
+ lghi %r0,15
+
+ /* Test 1: replace sgr with agr. */
+ lghi %r1,21
+ vl %v0,patch1
+ jg 1f /* start a new TB */
+0:
+ .org . + 6 /* pad patched code to 16 bytes */
+1:
+ vstl %v0,%r0,0b /* start writing before TB */
+ sgr %r1,%r1 /* this becomes `agr %r1,%r1` */
+ cgijne %r1,42,failure
+
+ /* Test 2: replace agr with division by zero. */
+ vl %v0,patch2
+ jg 1f /* start a new TB */
+0:
+ .org . + 6 /* pad patched code to 16 bytes */
+1:
+ vstl %v0,%r0,0b /* start writing before TB */
+ sgr %r1,%r1 /* this becomes `d %r0,zero` */
+failure:
+ lpswe failure_psw
+
+pgm:
+ chhsi program_interruption_code,0x9 /* divide exception? */
+ jne failure
+ clc program_old_psw(16),expected_old_psw2 /* correct old PSW? */
+ jne failure
+ lpswe success_psw
+
+patch1:
+ .fill 12 /* replaces padding and stpq */
+ agr %r1,%r1 /* replaces sgr */
+patch2:
+ .fill 12 /* replaces padding and stpq */
+ d %r0,zero /* replaces sgr */
+zero:
+ .long 0
+expected_old_psw2:
+ .quad 0x200180000000,failure /* cc is from addition */
+ .align 8
+c0:
+ .quad 0x60000 /* AFP, VX */
+success_psw:
+ .quad 0x2000000000000,0xfff /* see is_special_wait_psw() */
+failure_psw:
+ .quad 0x2000000000000,0 /* disabled wait */
--
2.39.3
next prev parent reply other threads:[~2023-08-31 19:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-31 19:17 [PULL 0/7] s390x and qtest patches Thomas Huth
2023-08-31 19:17 ` [PULL 1/7] target/s390x: Define TARGET_HAS_PRECISE_SMC Thomas Huth
2023-08-31 19:17 ` Thomas Huth [this message]
2023-08-31 19:17 ` [PULL 3/7] tests/qtest/usb-hcd-xhci-test: Check availability of devices before using them Thomas Huth
2023-08-31 19:17 ` [PULL 4/7] tests/qtest/netdev-socket: Avoid variable-length array in inet_get_free_port_multiple() Thomas Huth
2023-08-31 19:17 ` [PULL 5/7] tests/qtest/bios-tables-test: Check for virtio-iommu device before using it Thomas Huth
2023-08-31 19:17 ` [PULL 6/7] subprojects/berkeley-testfloat-3: Update to fix a problem with compiler warnings Thomas Huth
2023-08-31 19:17 ` [PULL 7/7] meson: test for CONFIG_TCG in config_all Thomas Huth
2023-09-05 15:08 ` [PULL 0/7] s390x and qtest patches Stefan Hajnoczi
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=20230831191719.140001-3-thuth@redhat.com \
--to=thuth@redhat.com \
--cc=iii@linux.ibm.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.