Skip to contentSkip to conversation

target/s390x: Define TARGET_HAS_PRECISE_SMC

PATCH In review Ilya Leoshkevich 2 participants 5 messages Aug 7, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 target/s390x: Define TARGET_HAS_PRECISE_SMC

PoP (Sequence of Storage References -> Instruction Fetching) says:

... if a store that is conceptually earlier is made by the same CPU using the same effective address as that by which the instruction is subse- quently fetched, the updated information is obtained ...

QEMU already has support for this in the common code; enable it for s390x.

---
 target/s390x/cpu.h | 2 ++
 1 file changed, 2 insertions(+)
target/s390x/cpu.h +2 -0
index eb5b65b7d3a..304029e57cf 100644
@@ -36,6 +36,8 @@
 /* The z/Architecture has a strong memory model with some store-after-load re-ordering */
 #define TCG_GUEST_DEFAULT_MO      (TCG_MO_ALL & ~TCG_MO_ST_LD)
 
+#define TARGET_HAS_PRECISE_SMC
+
 #define TARGET_INSN_START_EXTRA_WORDS 2
 
 #define MMU_USER_IDX 0
Signature · 1 line
2.41.0
#1 lore raw

PATCH 2/2 tests/tcg/s390x: Test precise self-modifying code handling

Add small softmmu and user tests to prevent regressions.

---
 tests/tcg/s390x/Makefile.softmmu-target |  1 +
 tests/tcg/s390x/Makefile.target         |  1 +
 tests/tcg/s390x/precise-smc-softmmu.S   | 63 +++++++++++++++++++++++++
 tests/tcg/s390x/precise-smc-user.c      | 39 +++++++++++++++
 4 files changed, 104 insertions(+)
 create mode 100644 tests/tcg/s390x/precise-smc-softmmu.S
 create mode 100644 tests/tcg/s390x/precise-smc-user.c
tests/tcg/s390x/Makefile.softmmu-target +1 -0
index 76345b6e643..1a1f088b280 100644
@@ -25,6 +25,7 @@ ASM_TESTS =                                                                    \
     lpswe-early                                                                \
     lra                                                                        \
     mc                                                                         \
+    precise-smc-softmmu                                                        \
     ssm-early                                                                  \
     stosm-early                                                                \
     stpq                                                                       \
tests/tcg/s390x/Makefile.target +1 -0
index 4f3793532bf..b5f88c20fc6 100644
@@ -60,6 +60,7 @@ Z13_TESTS+=lcbb
 Z13_TESTS+=locfhr
 Z13_TESTS+=vcksm
 Z13_TESTS+=vstl
+Z13_TESTS+=precise-smc-user
 $(Z13_TESTS): CFLAGS+=-march=z13 -O2
 TESTS+=$(Z13_TESTS)
 
tests/tcg/s390x/precise-smc-softmmu.S new file +63 -0

63 lines not on this page · raw message

tests/tcg/s390x/precise-smc-user.c new file +39 -0
new file mode 100644
index 00000000000..33a5270865c
@@ -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;
+}
Signature · 1 line
2.41.0
#2 lore raw
In reply to Ilya Leoshkevich
Quoted 9 lines · Ilya Leoshkevich

PoP (Sequence of Storage References -> Instruction Fetching) says:

... if a store that is conceptually earlier is made by the same CPU using the same effective address as that by which the instruction is subse- quently fetched, the updated information is obtained ...

QEMU already has support for this in the common code; enable it for s390x.

Figuring out what TARGET_HAS_PRECISE_SMC is all about, I only learned from git history

commit d720b93d0bcfe1beb729245b9ed1e5f071a24bd5

Author: Fabrice Bellard <[email protected]>
Date:   Sun Apr 25 17:57:43 2004 +0000

precise self modifying code support

git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@745
c046a42c-6fe2-441c-8c8c-71466251a162

AFAIU, precise SMC is stricter compared to what we have right now. So i suspect that this patch is actually fixing SMC behavior: for example, when a basic block ends up modifying itself.

Were there any BUG reports? (does patch #2 test for that and can reproduce the original issue?)

Quoted 19 lines
---
  target/s390x/cpu.h | 2 ++
  1 file changed, 2 insertions(+)
target/s390x/cpu.h +2 -0
index eb5b65b7d3a..304029e57cf 100644
@@ -36,6 +36,8 @@
  /* The z/Architecture has a strong memory model with some store-after-load re-ordering */
  #define TCG_GUEST_DEFAULT_MO      (TCG_MO_ALL & ~TCG_MO_ST_LD)
 
+#define TARGET_HAS_PRECISE_SMC
+
  #define TARGET_INSN_START_EXTRA_WORDS 2
 
  #define MMU_USER_IDX 0
Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw
Quoted 27 lines · David Hildenbrand
Quoted 9 lines · Ilya Leoshkevich

PoP (Sequence of Storage References -> Instruction Fetching) says:

     ... if a store that is conceptually earlier is made by the same CPU using the same effective address as that by which the instruction is subse- quently fetched, the updated information is obtained ...

QEMU already has support for this in the common code; enable it for s390x.

Figuring out what TARGET_HAS_PRECISE_SMC is all about, I only learned from git history

commit d720b93d0bcfe1beb729245b9ed1e5f071a24bd5
Author: Fabrice Bellard <[email protected]>
Date:   Sun Apr 25 17:57:43 2004 +0000

     precise self modifying code support

     git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@745 c046a42c-6fe2-441c-8c8c-71466251a162

Quoted 7 lines

AFAIU, precise SMC is stricter compared to what we have right now. So i
suspect that this patch is actually fixing SMC behavior: for example, when a basic block ends up modifying itself.

Were there any BUG reports? (does patch #2 test for that and can reproduce the original issue?)

There were no bug reports, I found this issue with fuzzing.

Patch #2 tests a TB modifying itself.
Reverting this commit makes the test fail.

[...]

#4 lore raw
Quoted 36 lines · Ilya Leoshkevich
Quoted 27 lines · David Hildenbrand
Quoted 9 lines · Ilya Leoshkevich

PoP (Sequence of Storage References -> Instruction Fetching) says:

     ... if a store that is conceptually earlier is made by the same CPU using the same effective address as that by which the instruction is subse- quently fetched, the updated information is obtained ...

QEMU already has support for this in the common code; enable it for s390x.

Figuring out what TARGET_HAS_PRECISE_SMC is all about, I only learned from git history

commit d720b93d0bcfe1beb729245b9ed1e5f071a24bd5
Author: Fabrice Bellard <[email protected]>
Date:   Sun Apr 25 17:57:43 2004 +0000

     precise self modifying code support

     git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@745
c046a42c-6fe2-441c-8c8c-71466251a162

Quoted 7 lines

AFAIU, precise SMC is stricter compared to what we have right now. So i
suspect that this patch is actually fixing SMC behavior: for example, when a basic block ends up modifying itself.

Were there any BUG reports? (does patch #2 test for that and can reproduce the original issue?)

Quoted 7 lines

There were no bug reports, I found this issue with fuzzing.

Patch #2 tests a TB modifying itself.
Reverting this commit makes the test fail.

[...]

Cool, thanks. Worth adding to the patch description.

Signature · 3 lines
Cheers,

David / dhildenb