target/s390x: CC fixes
PATCH 2/4 tests/tcg/s390x: Test CLC with inaccessible second operand
In reply to Ilya LeoshkevichAdd 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
@@ -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
PATCH 1/4 target/s390x: Fix CLC corrupting cc_src
In reply to Ilya LeoshkevichCLC 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
@@ -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
cover target/s390x: CC fixes
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 and 4 are tests.
Best regards,
Ilya
[1] https://gitlab.com/qemu-project/qemu/-/issues/1865#note_1545060658
Ilya Leoshkevich (4):
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
target/s390x/tcg/translate.c | 10 ++++--- tests/tcg/s390x/Makefile.target | 2 ++ tests/tcg/s390x/clc.c | 48 +++++++++++++++++++++++++++++++++ tests/tcg/s390x/laalg.c | 27 +++++++++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 tests/tcg/s390x/clc.c create mode 100644 tests/tcg/s390x/laalg.c
Signature · 1 line
2.41.0
PATCH 3/4 target/s390x: Fix LAALG not updating cc_src
LAALG uses op_laa() and cout_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 setting cc_src in op_laa().
--- target/s390x/tcg/translate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/s390x/tcg/translate.c +2 -1
@@ -2684,7 +2684,8 @@ static DisasJumpType op_laa(DisasContext *s, DisasOps *o)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); +tcg_gen_movi_i64 (cc_src,0 ); +tcg_gen_add2_i64 (o->out, cc_src, o->in1, cc_src, o->in2, cc_src);return DISAS_NEXT ; }
Signature · 1 line
2.41.0
PATCH 4/4 tests/tcg/s390x: Test LAALG with negative cc_src
In reply to Ilya LeoshkevichAdd 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
@@ -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
@@ -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
Quoted 4 lines · Ilya Leoshkevich
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
I started coding that up but was distracted by other things; last time I looked at that, I concluded that the way we are calculating the carry in not suitable when we're doing two additions (like ADD LOGICAL WITH CARRY).
In addition, the way cout_addu32/cout_addu64 relies on cc_src/cc_dst is wrong in some occurrences.
Quoted 3 lines
are targeted fixes: patch 1 helps with installing Fedora, and patch 3 addresses something I noticed when reviewing the code. Patches 2 and 4 are tests.
I assume you are only scratching the surface with your fixes and we might be better off just doing CC_OP_ADDU / CC_OP_SUBU like CC_OP_ADD_32 / CC_OP_ADD_64 / ... to fix all issues involved.
Quoted 20 lines
Best regards,
Ilya[1] https://gitlab.com/qemu-project/qemu/-/issues/1865#note_1545060658
Ilya Leoshkevich (4):
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_srctarget/s390x/tcg/translate.c | 10 ++++--- tests/tcg/s390x/Makefile.target | 2 ++ tests/tcg/s390x/clc.c | 48 +++++++++++++++++++++++++++++++++ tests/tcg/s390x/laalg.c | 27 +++++++++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-)create mode 100644
tests/tcg/s390x/clc.c
create mode 100644tests/tcg/s390x/laalg.c
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH 1/4] target/s390x: Fix CLC corrupting cc_src
In reply to Ilya LeoshkevichQuoted 5 lines · Ilya Leoshkevich
CLC updates
cc_srcbefore accessing the second operand; if the latter is inaccessible, the former ends up containing a bogus value.Fix by reading
cc_srcinto a temporary first.
Quoted 7 lines
Fixes 4f7403d52b1c ("target-s390: Convert CLC")Signed-off-by Ilya Leoshkevich <[email protected]>--- target/s390x/tcg/translate.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
r~
Re: [PATCH 2/4] tests/tcg/s390x: Test CLC with inaccessible second operand
In reply to Ilya LeoshkevichQuoted 11 lines · Ilya Leoshkevich
tests/tcg/s390x/clc.c new file +11
-0+int main (void ) +{ + registerunsigned long r0 asm("r0" ); +unsigned long mem =42 , rhs =500 ; +struct sigaction act; +int err; + +memset (&act,0 ,sizeof (act)); + act.sa_sigaction = handle_sigsegv; + act.sa_flags =SA_SIGINFO ; + err =sigaction (SIGSEGV , &act,NULL );
tests/tcg/s390x/clc.c line 46
Quoted 10 lines
tests/tcg/s390x/clc.c new file +10
-0+assert (err ==0 ); + + r0 =100 ; + asm("algr %[r0],%[rhs]\n" +"clc 0(8,%[mem]),0(0)\n" /* The 2nd operand will cause a SEGV. */ + : [r0]"+r" (r0) + : [mem]"r" (&mem) + , [rhs]"r" (rhs) + :"cc" ,"memory" ); +
You could just as easily set cc based on CHI or something to avoid hard-coding r0, or even clobbering an output register at all.
But I guess there's little point bike shedding this too much...
r~
Re: [PATCH 3/4] target/s390x: Fix LAALG not updating cc_src
In reply to Ilya LeoshkevichQuoted 18 lines · Ilya Leoshkevich
LAALG uses
op_laa()andcout_addu64(). The latter expectscc_srcto be set, but the former does not do it. This can lead to assertion failures if something setscc_srcto neither 0 nor 1 before.Fix by setting
cc_srcinop_laa().Fixes 4dba4d6fef61 ("target/s390x: Use atomic operations for LOAD AND OP")Signed-off-by Ilya Leoshkevich <[email protected]>--- target/s390x/tcg/translate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)target/s390x/tcg/translate.c +0
-0
target/s390x/tcg/translate.c static DisasJumpType op_laa(DisasContext *s, DisasOps *o) · line 2691
Quoted 9 lines
target/s390x/tcg/translate.c +2
-1@@ -2684,7 +2684,8 @@ static DisasJumpType op_laa(DisasContext *s, DisasOps *o)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); +tcg_gen_movi_i64 (cc_src,0 ); +tcg_gen_add2_i64 (o->out, cc_src, o->in1, cc_src, o->in2, cc_src);return DISAS_NEXT ; }
This is wrong, or at least not ideal -- op_laa is not *only* used with cout_addu64. I think this would be better with a second version of op_laa.
r~
Quoted 8 lines · David Hildenbrand
Quoted 6 lines · Ilya Leoshkevich
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
Quoted 6 lines
I started coding that up but was distracted by other things; last time I
looked at that, I concluded that the way we are calculating the carry in
not suitable when we're doing two additions (like ADD LOGICAL WITH CARRY).
Do you per chance remember any details? IIUC the code in question is:
static DisasJumpType op_addc64(DisasContext *s, DisasOps *o)
{
compute_carry(s);
TCGv_i64 zero = tcg_constant_i64(0);
tcg_gen_add2_i64(o->out, cc_src, o->in1, zero, cc_src, zero);
tcg_gen_add2_i64(o->out, cc_src, o->out, cc_src, o->in2, zero);
return DISAS_NEXT;
}This looks correct to me, because the 128-bit result of the first addition is passed fully to the second addition, and the upper half is always either 0 or 1.
I played with chaining ADD-LOGICAL-WITH-CARRYs with various inputs, and could not produce any wrong calculations in the emulation.
Best regards,
Ilya
[...]
Quoted 9 lines · Ilya Leoshkevich
Quoted 8 lines · David Hildenbrand
Quoted 6 lines · Ilya Leoshkevich
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
Quoted 8 lines
Quoted 6 lines
I started coding that up but was distracted by other things; last time I
looked at that, I concluded that the way we are calculating the carry in
not suitable when we're doing two additions (like ADD LOGICAL WITH CARRY).Do you per chance remember any details? IIUC the code in question is:
Unfortunately, I don't. I thought there would be a case where we could overflow twice, and result in a carry value of 2. Or some other weird corner case where the result would not be expressive.
Maybe I was daydreaming, let me see if I can re-discover what I found (should have taken notes but was just briefly looking at this).
If not, your fixes might be just good enough.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH 2/4] tests/tcg/s390x: Test CLC with inaccessible second operand
In reply to Richard HendersonQuoted 19 lines · Richard Henderson
Quoted 18 lines · Ilya Leoshkevich
tests/tcg/s390x/clc.c new file +16
-0+int main(void) +{ + register unsigned long r0 asm("r0"); + unsigned long mem = 42, rhs = 500; + struct sigaction act; + int err; + + memset(&act, 0, sizeof(act)); + act.sa_sigaction = handle_sigsegv; + act.sa_flags = SA_SIGINFO; + err = sigaction(SIGSEGV, &act, NULL); + assert(err == 0); + + r0 = 100; + asm("algr %[r0],%[rhs]\n" + "clc 0(8,%[mem]),0(0)\n" /* The 2nd operand will cause aSEGV. */
+ : [r0] "+r" (r0)
Quoted 8 lines
Quoted 4 lines
+ : [mem] "r" (&mem)
+ , [rhs] "r" (rhs)
+ : "cc", "memory");
+You could just as easily set cc based on CHI or something to avoid hard-coding r0, or even
clobbering an output register at all.
The point of hardcoding r0 is rather to be able to check its value in handle_sigsegv(). While this was not buggy, I still wanted to make sure that the updated value is "committed" despite SEGV.
Quoted 4 lines
But I guess there's little point bike shedding this too much...
r~