Skip to contentSkip to conversation

Fix EXECUTE of relative long instructions

PATCH Superseded Ilya Leoshkevich 3 participants 6 messages Mar 13, 2023 – Mar 14, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Fix EXECUTE of relative long instructions

Hi,

This series fixes EXECUTE of instructions like LARL, LGLR, etc. Currently the address calculation uses EXECUTE's address as a base, while it should be using that of the target instruction. Patch 1 fixes the issue, patch 2 adds a test.

Best regards,
Ilya

Ilya Leoshkevich (2):
target/s390x: Fix EXECUTE of relative long instructions tests/tcg/s390x: Add ex-relative-long.c

 target/s390x/cpu.h                 |   1 +
 target/s390x/tcg/mem_helper.c      |   1 +
 target/s390x/tcg/translate.c       |  10 +-
 tests/tcg/s390x/Makefile.target    |   1 +
 tests/tcg/s390x/ex-relative-long.c | 149 +++++++++++++++++++++++++++++
 5 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/s390x/ex-relative-long.c
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/2 target/s390x: Fix EXECUTE of relative long instructions

The code uses the wrong base for relative addressing: it should use the target instruction address and not the EXECUTE's address.

Fix by storing the target instruction address in the new CPUS390XState member and loading it from the code generated by in2_ri2().

---
 target/s390x/cpu.h            |  1 +
 target/s390x/tcg/mem_helper.c |  1 +
 target/s390x/tcg/translate.c  | 10 +++++++++-
 3 files changed, 11 insertions(+), 1 deletion(-)
target/s390x/cpu.h +1 -0
index 7d6d01325b2..8aaf8dd5a3b 100644
@@ -87,6 +87,7 @@ struct CPUArchState {
     uint64_t cc_vr;
 
     uint64_t ex_value;
+    uint64_t ex_target;
 
     uint64_t __excp_addr;
     uint64_t psa;
target/s390x/tcg/mem_helper.c +1 -0
index 6835c26dda4..00afae2b640 100644
@@ -2530,6 +2530,7 @@ void HELPER(ex)(CPUS390XState *env, uint32_t ilen, uint64_t r1, uint64_t addr)
        that ex_value is non-zero, which flags that we are in a state
        that requires such execution.  */
     env->ex_value = insn | ilen;
+    env->ex_target = addr;
 }
 
 uint32_t HELPER(mvcos)(CPUS390XState *env, uint64_t dest, uint64_t src,
target/s390x/tcg/translate.c +9 -1
index 811049ea281..fefff95b91c 100644
@@ -5888,7 +5888,15 @@ static void in2_a2(DisasContext *s, DisasOps *o)
 
 static void in2_ri2(DisasContext *s, DisasOps *o)
 {
-    o->in2 = tcg_const_i64(s->base.pc_next + (int64_t)get_field(s, i2) * 2);
+    int64_t delta = (int64_t)get_field(s, i2) * 2;
+
+    if (unlikely(s->ex_value)) {
+        o->in2 = tcg_temp_new_i64();
+        tcg_gen_ld_i64(o->in2, cpu_env, offsetof(CPUS390XState, ex_target));
+        tcg_gen_addi_i64(o->in2, o->in2, delta);
+    } else {
+        o->in2 = tcg_const_i64(s->base.pc_next + delta);
+    }
 }
 #define SPEC_in2_ri2 0
 
Signature · 1 line
2.39.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] target/s390x: Fix EXECUTE of relative long instructions

Quoted 49 lines · Ilya Leoshkevich

The code uses the wrong base for relative addressing: it should use the target instruction address and not the EXECUTE's address.

Fix by storing the target instruction address in the new CPUS390XState member and loading it from the code generated by in2_ri2().

---
  target/s390x/cpu.h            |  1 +
  target/s390x/tcg/mem_helper.c |  1 +
  target/s390x/tcg/translate.c  | 10 +++++++++-
  3 files changed, 11 insertions(+), 1 deletion(-)
target/s390x/cpu.h +1 -0
index 7d6d01325b2..8aaf8dd5a3b 100644
@@ -87,6 +87,7 @@ struct CPUArchState {
      uint64_t cc_vr;
 
      uint64_t ex_value;
+    uint64_t ex_target;
 
      uint64_t __excp_addr;
      uint64_t psa;
target/s390x/tcg/mem_helper.c +1 -0
index 6835c26dda4..00afae2b640 100644
@@ -2530,6 +2530,7 @@ void HELPER(ex)(CPUS390XState *env, uint32_t ilen, uint64_t r1, uint64_t addr)
         that ex_value is non-zero, which flags that we are in a state
         that requires such execution.  */
      env->ex_value = insn | ilen;
+    env->ex_target = addr;
  }
 
  uint32_t HELPER(mvcos)(CPUS390XState *env, uint64_t dest, uint64_t src,
target/s390x/tcg/translate.c +2 -1
index 811049ea281..fefff95b91c 100644
@@ -5888,7 +5888,15 @@ static void in2_a2(DisasContext *s, DisasOps *o)
 
  static void in2_ri2(DisasContext *s, DisasOps *o)
  {
-    o->in2 = tcg_const_i64(s->base.pc_next + (int64_t)get_field(s, i2) * 2);
+    int64_t delta = (int64_t)get_field(s, i2) * 2;
+

target/s390x/tcg/translate.c static void in2_a2(DisasContext *s, DisasOps *o) · line 5902

Quoted 10 lines
target/s390x/tcg/translate.c +7 -0
+    if (unlikely(s->ex_value)) {
+        o->in2 = tcg_temp_new_i64();
+        tcg_gen_ld_i64(o->in2, cpu_env, offsetof(CPUS390XState, ex_target));
+        tcg_gen_addi_i64(o->in2, o->in2, delta);
+    } else {
+        o->in2 = tcg_const_i64(s->base.pc_next + delta);
+    }
  }
  #define SPEC_in2_ri2 0
 

LGTM, hopefully Richard can have another look. Thanks!

Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] target/s390x: Fix EXECUTE of relative long instructions

In reply to Ilya Leoshkevich
Quoted 6 lines · Ilya Leoshkevich

The code uses the wrong base for relative addressing: it should use the target instruction address and not the EXECUTE's address.

Fix by storing the target instruction address in the new CPUS390XState member and loading it from the code generated by in2_ri2().

Quoted 7 lines
---
  target/s390x/cpu.h            |  1 +
  target/s390x/tcg/mem_helper.c |  1 +
  target/s390x/tcg/translate.c  | 10 +++++++++-
  3 files changed, 11 insertions(+), 1 deletion(-)

Good solution, reading the value from env.

r~

#4 lore raw

PATCH 2/2 tests/tcg/s390x: Add ex-relative-long.c

In reply to Ilya Leoshkevich

Test EXECUTE and EXECUTE RELATIVE LONG with relative long instructions as targets.

---
 tests/tcg/s390x/Makefile.target    |   1 +
 tests/tcg/s390x/ex-relative-long.c | 149 +++++++++++++++++++++++++++++
 2 files changed, 150 insertions(+)
 create mode 100644 tests/tcg/s390x/ex-relative-long.c
tests/tcg/s390x/Makefile.target +1 -0
index 72ad309b273..ed2709ee2c3 100644
@@ -28,6 +28,7 @@ TESTS+=div
 TESTS+=clst
 TESTS+=long-double
 TESTS+=cdsg
+TESTS+=ex-relative-long
 
 cdsg: CFLAGS+=-pthread
 cdsg: LDFLAGS+=-pthread
tests/tcg/s390x/ex-relative-long.c new file +149 -0

149 lines not on this page · raw message

Signature · 1 line
2.39.2
#5 lore raw

Re: [PATCH 2/2] tests/tcg/s390x: Add ex-relative-long.c

Quoted 32 lines · Ilya Leoshkevich

Test EXECUTE and EXECUTE RELATIVE LONG with relative long instructions as targets.

---
  tests/tcg/s390x/Makefile.target    |   1 +
  tests/tcg/s390x/ex-relative-long.c | 149 +++++++++++++++++++++++++++++
  2 files changed, 150 insertions(+)

create mode 100644 tests/tcg/s390x/ex-relative-long.c

tests/tcg/s390x/Makefile.target +1 -0
index 72ad309b273..ed2709ee2c3 100644
@@ -28,6 +28,7 @@ TESTS+=div
  TESTS+=clst
  TESTS+=long-double
  TESTS+=cdsg
+TESTS+=ex-relative-long
 
  cdsg: CFLAGS+=-pthread
  cdsg: LDFLAGS+=-pthread
tests/tcg/s390x/ex-relative-long.c new file +4 -0
new file mode 100644
index 00000000000..e47dac7e2c3
@@ -0,0 +1,149 @@
+/* Check EXECUTE with relative long instructions as targets. */
+#include <stdlib.h>
+#include <stdio.h>
+

tests/tcg/s390x/ex-relative-long.c line 14

Quoted 10 lines
tests/tcg/s390x/ex-relative-long.c new file +10 -0
+struct test {
+    const char *name;
+    long (*func)(long reg, long *cc);
+    long exp_reg;
+    long exp_mem;
+    long exp_cc;
+};
+
+/* Variable targeted by relative long instructions. */
+long mem;

I guess you're assuming that the adjacent memory, which the buggy qemu would address, contains something other than

tests/tcg/s390x/ex-relative-long.c line 20

Quoted 2 lines
tests/tcg/s390x/ex-relative-long.c new file +2 -0
+/* Initial "mem" value. */
+#define MEM 0xfedcba9889abcdef

this? Perhaps better to use an array, and address the middle of it?

r~