Skip to contentSkip to conversation

target/s390x: CC fixes

PATCH In review Ilya Leoshkevich 3 participants 11 messages Nov 6, 2023

lore
Share & tools
lore
#5 lore raw

PATCH v2 3/5 target/s390x: Fix LAALG not updating cc_src

In reply to Ilya Leoshkevich

LAALG uses op_laa() and wout_addu64(). The latter expects cc_src to be set, but the former does not do it. This can lead to assertion failures if something sets cc_src to neither 0 nor 1 before.

Fix by introducing op_laa_addu64(), which sets cc_src, and using it for LAALG.

Fixes 4dba4d6fef61 ("target/s390x: Use atomic operations for LOAD AND OP")
---
 target/s390x/tcg/insn-data.h.inc |  2 +-
 target/s390x/tcg/translate.c     | 19 +++++++++++++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)
target/s390x/tcg/insn-data.h.inc +1 -1
index 0bfd88d3c3a..2f07f39d9cb 100644
@@ -442,7 +442,7 @@
     D(0xebe8, LAAG,    RSY_a, ILA, r3, a2, new, in2_r1, laa, adds64, MO_TEUQ)
 /* LOAD AND ADD LOGICAL */
     D(0xebfa, LAAL,    RSY_a, ILA, r3_32u, a2, new, in2_r1_32, laa, addu32, MO_TEUL)
-    D(0xebea, LAALG,   RSY_a, ILA, r3, a2, new, in2_r1, laa, addu64, MO_TEUQ)
+    D(0xebea, LAALG,   RSY_a, ILA, r3, a2, new, in2_r1, laa_addu64, addu64, MO_TEUQ)
 /* LOAD AND AND */
     D(0xebf4, LAN,     RSY_a, ILA, r3_32s, a2, new, in2_r1_32, lan, nz32, MO_TESL)
     D(0xebe4, LANG,    RSY_a, ILA, r3, a2, new, in2_r1, lan, nz64, MO_TEUQ)
target/s390x/tcg/translate.c +17 -2
index a0d6a2a35dd..62ab2be8b12 100644
@@ -2677,17 +2677,32 @@ static DisasJumpType op_kxb(DisasContext *s, DisasOps *o)
     return DISAS_NEXT;
 }
 
-static DisasJumpType op_laa(DisasContext *s, DisasOps *o)
+static DisasJumpType help_laa(DisasContext *s, DisasOps *o, bool addu64)
 {
     /* The real output is indeed the original value in memory;
        recompute the addition for the computation of CC.  */
     tcg_gen_atomic_fetch_add_i64(o->in2, o->in2, o->in1, get_mem_index(s),
                                  s->insn->data | MO_ALIGN);
     /* However, we need to recompute the addition for setting CC.  */
-    tcg_gen_add_i64(o->out, o->in1, o->in2);
+    if (addu64) {
+        tcg_gen_movi_i64(cc_src, 0);
+        tcg_gen_add2_i64(o->out, cc_src, o->in1, cc_src, o->in2, cc_src);
+    } else {
+        tcg_gen_add_i64(o->out, o->in1, o->in2);
+    }
     return DISAS_NEXT;
 }
 
+static DisasJumpType op_laa(DisasContext *s, DisasOps *o)
+{
+    return help_laa(s, o, false);
+}
+
+static DisasJumpType op_laa_addu64(DisasContext *s, DisasOps *o)
+{
+    return help_laa(s, o, true);
+}
+
 static DisasJumpType op_lan(DisasContext *s, DisasOps *o)
 {
     /* The real output is indeed the original value in memory;
Signature · 1 line
2.41.0
#8 lore raw

PATCH v2 4/5 tests/tcg/s390x: Test LAALG with negative cc_src

In reply to Ilya Leoshkevich

Add a small test to prevent regressions.

---
 tests/tcg/s390x/Makefile.target |  1 +
 tests/tcg/s390x/laalg.c         | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 tests/tcg/s390x/laalg.c
tests/tcg/s390x/Makefile.target +1 -0
index ccd4f4e68de..a476547b659 100644
@@ -42,6 +42,7 @@ TESTS+=mdeb
 TESTS+=cgebra
 TESTS+=clgebr
 TESTS+=clc
+TESTS+=laalg
 
 cdsg: CFLAGS+=-pthread
 cdsg: LDFLAGS+=-pthread
tests/tcg/s390x/laalg.c new file +27 -0
new file mode 100644
index 00000000000..797d168bb16
@@ -0,0 +1,27 @@
+/*
+ * Test the LAALG instruction.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <assert.h>
+#include <stdlib.h>
+
+int main(void)
+{
+    unsigned long cc = 0, op1, op2 = 40, op3 = 2;
+
+    asm("slgfi %[cc],1\n"  /* Set cc_src = -1. */
+        "laalg %[op1],%[op3],%[op2]\n"
+        "ipm %[cc]"
+        : [cc] "+r" (cc)
+        , [op1] "=r" (op1)
+        , [op2] "+T" (op2)
+        : [op3] "r" (op3)
+        : "cc");
+
+    assert(cc == 0xffffffff10ffffff);
+    assert(op1 == 40);
+    assert(op2 == 42);
+
+    return EXIT_SUCCESS;
+}
Signature · 1 line
2.41.0
#0 lore raw

v2 cover target/s390x: CC fixes

v1: https://lists.gnu.org/archive/html/qemu-devel/2023-10/msg10251.html
v1 -> v2: Introduce op_laa_addu64() (Richard).
Add the ADD LOGICAL WITH CARRY test. This was intended to catch the issue mentioned by David, but did not succeed. I thought it would still be worthwile to include it as a regression test.

Hi,

This series fixes two issues with updating CC. David was suggesting a bigger rewrite [1], but I did not dare do this (yet). Instead, these are targeted fixes: patch 1 helps with installing Fedora, and patch 3 addresses something I noticed when reviewing the code. Patches 2, 4 and 5 are tests.

Best regards,
Ilya

[1] https://gitlab.com/qemu-project/qemu/-/issues/1865#note_1545060658

Ilya Leoshkevich (5):
target/s390x: Fix CLC corrupting cc_src tests/tcg/s390x: Test CLC with inaccessible second operand target/s390x: Fix LAALG not updating cc_src tests/tcg/s390x: Test LAALG with negative cc_src tests/tcg/s390x: Test ADD LOGICAL WITH CARRY

 target/s390x/tcg/insn-data.h.inc         |   2 +-
 target/s390x/tcg/translate.c             |  26 +++-
 tests/tcg/s390x/Makefile.target          |   3 +
 tests/tcg/s390x/add-logical-with-carry.c | 156 +++++++++++++++++++++++
 tests/tcg/s390x/clc.c                    |  48 +++++++
 tests/tcg/s390x/laalg.c                  |  27 ++++
 6 files changed, 257 insertions(+), 5 deletions(-)
 create mode 100644 tests/tcg/s390x/add-logical-with-carry.c
 create mode 100644 tests/tcg/s390x/clc.c
 create mode 100644 tests/tcg/s390x/laalg.c
Signature · 1 line
2.41.0
#3 lore raw

PATCH v2 2/5 tests/tcg/s390x: Test CLC with inaccessible second operand

Add a small test to prevent regressions.

---
 tests/tcg/s390x/Makefile.target |  1 +
 tests/tcg/s390x/clc.c           | 48 +++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 tests/tcg/s390x/clc.c
tests/tcg/s390x/Makefile.target +1 -0
index 826f0a18e43..ccd4f4e68de 100644
@@ -41,6 +41,7 @@ TESTS+=larl
 TESTS+=mdeb
 TESTS+=cgebra
 TESTS+=clgebr
+TESTS+=clc
 
 cdsg: CFLAGS+=-pthread
 cdsg: LDFLAGS+=-pthread
tests/tcg/s390x/clc.c new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/5 target/s390x: Fix CLC corrupting cc_src

In reply to Ilya Leoshkevich

CLC updates cc_src before accessing the second operand; if the latter is inaccessible, the former ends up containing a bogus value.

Fix by reading cc_src into a temporary first.

---
 target/s390x/tcg/translate.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
target/s390x/tcg/translate.c +5 -2
index 4bae1509f50..a0d6a2a35dd 100644
@@ -2007,6 +2007,7 @@ static DisasJumpType op_cksm(DisasContext *s, DisasOps *o)
 static DisasJumpType op_clc(DisasContext *s, DisasOps *o)
 {
     int l = get_field(s, l1);
+    TCGv_i64 src;
     TCGv_i32 vl;
     MemOp mop;
 
@@ -2016,9 +2017,11 @@ static DisasJumpType op_clc(DisasContext *s, DisasOps *o)
     case 4:
     case 8:
         mop = ctz32(l + 1) | MO_TE;
-        tcg_gen_qemu_ld_tl(cc_src, o->addr1, get_mem_index(s), mop);
+        /* Do not update cc_src yet: loading cc_dst may cause an exception. */
+        src = tcg_temp_new_i64();
+        tcg_gen_qemu_ld_tl(src, o->addr1, get_mem_index(s), mop);
         tcg_gen_qemu_ld_tl(cc_dst, o->in2, get_mem_index(s), mop);
-        gen_op_update2_cc_i64(s, CC_OP_LTUGTU_64, cc_src, cc_dst);
+        gen_op_update2_cc_i64(s, CC_OP_LTUGTU_64, src, cc_dst);
         return DISAS_NEXT;
     default:
         vl = tcg_constant_i32(l);
Signature · 1 line
2.41.0
#10 lore raw

PATCH v2 5/5 tests/tcg/s390x: Test ADD LOGICAL WITH CARRY

In reply to Ilya Leoshkevich

Add a test that tries different combinations of ADD LOGICAL WITH CARRY instructions.

---
 tests/tcg/s390x/Makefile.target          |   1 +
 tests/tcg/s390x/add-logical-with-carry.c | 156 +++++++++++++++++++++++
 2 files changed, 157 insertions(+)
 create mode 100644 tests/tcg/s390x/add-logical-with-carry.c
tests/tcg/s390x/Makefile.target +1 -0
index a476547b659..0e670f3f8b9 100644
@@ -43,6 +43,7 @@ TESTS+=cgebra
 TESTS+=clgebr
 TESTS+=clc
 TESTS+=laalg
+TESTS+=add-logical-with-carry
 
 cdsg: CFLAGS+=-pthread
 cdsg: LDFLAGS+=-pthread
tests/tcg/s390x/add-logical-with-carry.c new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
2.41.0
Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH v2 2/5] tests/tcg/s390x: Test CLC with inaccessible second operand

In reply to Ilya Leoshkevich
Quoted 8 lines · Ilya Leoshkevich

Add a small test to prevent regressions.

---
  tests/tcg/s390x/Makefile.target |  1 +
  tests/tcg/s390x/clc.c           | 48 +++++++++++++++++++++++++++++++++
  2 files changed, 49 insertions(+)

create mode 100644 tests/tcg/s390x/clc.c

r~

Reviewed-by on v2 3/5
#6 lore raw

Re: [PATCH v2 3/5] target/s390x: Fix LAALG not updating cc_src

In reply to Ilya Leoshkevich
Quoted 7 lines · Ilya Leoshkevich

LAALG uses op_laa() and wout_addu64(). The latter expects cc_src to be set, but the former does not do it. This can lead to assertion failures if something sets cc_src to neither 0 nor 1 before.

Fix by introducing op_laa_addu64(), which sets cc_src, and using it for LAALG.

Quoted 7 lines
Fixes 4dba4d6fef61 ("target/s390x: Use atomic operations for LOAD AND OP")
---
  target/s390x/tcg/insn-data.h.inc |  2 +-
  target/s390x/tcg/translate.c     | 19 +++++++++++++++++--
  2 files changed, 18 insertions(+), 3 deletions(-)

r~

Reviewed-by on v2 4/5
#9 lore raw

Re: [PATCH v2 4/5] tests/tcg/s390x: Test LAALG with negative cc_src

In reply to Ilya Leoshkevich
Quoted 8 lines · Ilya Leoshkevich

Add a small test to prevent regressions.

---
  tests/tcg/s390x/Makefile.target |  1 +
  tests/tcg/s390x/laalg.c         | 27 +++++++++++++++++++++++++++
  2 files changed, 28 insertions(+)

create mode 100644 tests/tcg/s390x/laalg.c

r~

Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] target/s390x: Fix CLC corrupting cc_src

In reply to Ilya Leoshkevich
Quoted 30 lines · Ilya Leoshkevich

CLC updates cc_src before accessing the second operand; if the latter is inaccessible, the former ends up containing a bogus value.

Fix by reading cc_src into a temporary first.

---
  target/s390x/tcg/translate.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
target/s390x/tcg/translate.c +1 -0
index 4bae1509f50..a0d6a2a35dd 100644
@@ -2007,6 +2007,7 @@ static DisasJumpType op_cksm(DisasContext *s, DisasOps *o)
  static DisasJumpType op_clc(DisasContext *s, DisasOps *o)
  {
      int l = get_field(s, l1);
+    TCGv_i64 src;
      TCGv_i32 vl;
      MemOp mop;
 
@@ -2016,9 +2017,11 @@ static DisasJumpType op_clc(DisasContext *s, DisasOps *o)
      case 4:
      case 8:
          mop = ctz32(l + 1) | MO_TE;

target/s390x/tcg/translate.c static DisasJumpType op_clc(DisasContext *s, DisasOps *o) · line 2027

Quoted 10 lines
target/s390x/tcg/translate.c +4 -2
-        tcg_gen_qemu_ld_tl(cc_src, o->addr1, get_mem_index(s), mop);
+        /* Do not update cc_src yet: loading cc_dst may cause an exception. */
+        src = tcg_temp_new_i64();
+        tcg_gen_qemu_ld_tl(src, o->addr1, get_mem_index(s), mop);
          tcg_gen_qemu_ld_tl(cc_dst, o->in2, get_mem_index(s), mop);
-        gen_op_update2_cc_i64(s, CC_OP_LTUGTU_64, cc_src, cc_dst);
+        gen_op_update2_cc_i64(s, CC_OP_LTUGTU_64, src, cc_dst);
          return DISAS_NEXT;
      default:
          vl = tcg_constant_i32(l);
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v2 3/5
#7 lore raw

Re: [PATCH v2 3/5] target/s390x: Fix LAALG not updating cc_src

In reply to Ilya Leoshkevich
Quoted 57 lines · Ilya Leoshkevich

LAALG uses op_laa() and wout_addu64(). The latter expects cc_src to be set, but the former does not do it. This can lead to assertion failures if something sets cc_src to neither 0 nor 1 before.

Fix by introducing op_laa_addu64(), which sets cc_src, and using it for LAALG.

Fixes 4dba4d6fef61 ("target/s390x: Use atomic operations for LOAD AND OP")
---
  target/s390x/tcg/insn-data.h.inc |  2 +-
  target/s390x/tcg/translate.c     | 19 +++++++++++++++++--
  2 files changed, 18 insertions(+), 3 deletions(-)
target/s390x/tcg/insn-data.h.inc +1 -1
index 0bfd88d3c3a..2f07f39d9cb 100644
@@ -442,7 +442,7 @@
      D(0xebe8, LAAG,    RSY_a, ILA, r3, a2, new, in2_r1, laa, adds64, MO_TEUQ)
  /* LOAD AND ADD LOGICAL */
      D(0xebfa, LAAL,    RSY_a, ILA, r3_32u, a2, new, in2_r1_32, laa, addu32, MO_TEUL)
-    D(0xebea, LAALG,   RSY_a, ILA, r3, a2, new, in2_r1, laa, addu64, MO_TEUQ)
+    D(0xebea, LAALG,   RSY_a, ILA, r3, a2, new, in2_r1, laa_addu64, addu64, MO_TEUQ)
  /* LOAD AND AND */
      D(0xebf4, LAN,     RSY_a, ILA, r3_32s, a2, new, in2_r1_32, lan, nz32, MO_TESL)
      D(0xebe4, LANG,    RSY_a, ILA, r3, a2, new, in2_r1, lan, nz64, MO_TEUQ)
target/s390x/tcg/translate.c +10 -2
index a0d6a2a35dd..62ab2be8b12 100644
@@ -2677,17 +2677,32 @@ static DisasJumpType op_kxb(DisasContext *s, DisasOps *o)
      return DISAS_NEXT;
  }
 
-static DisasJumpType op_laa(DisasContext *s, DisasOps *o)
+static DisasJumpType help_laa(DisasContext *s, DisasOps *o, bool addu64)
  {
      /* The real output is indeed the original value in memory;
         recompute the addition for the computation of CC.  */
      tcg_gen_atomic_fetch_add_i64(o->in2, o->in2, o->in1, get_mem_index(s),
                                   s->insn->data | MO_ALIGN);
      /* However, we need to recompute the addition for setting CC.  */
-    tcg_gen_add_i64(o->out, o->in1, o->in2);
+    if (addu64) {
+        tcg_gen_movi_i64(cc_src, 0);
+        tcg_gen_add2_i64(o->out, cc_src, o->in1, cc_src, o->in2, cc_src);
+    } else {
+        tcg_gen_add_i64(o->out, o->in1, o->in2);
+    }
      return DISAS_NEXT;
  }
 
+static DisasJumpType op_laa(DisasContext *s, DisasOps *o)
+{
+    return help_laa(s, o, false);

target/s390x/tcg/translate.c static DisasJumpType op_kxb(DisasContext *s, DisasOps *o) · line 2708

Quoted 10 lines
target/s390x/tcg/translate.c +7 -0
+}
+
+static DisasJumpType op_laa_addu64(DisasContext *s, DisasOps *o)
+{
+    return help_laa(s, o, true);
+}
+
  static DisasJumpType op_lan(DisasContext *s, DisasOps *o)
  {
      /* The real output is indeed the original value in memory;
Signature · 3 lines
Cheers,

David / dhildenb