* [PATCH] target/i386: Fix 32-bit AD[CO]X insns in 64-bit mode
@ 2023-01-15 1:21 Richard Henderson
2023-02-16 6:51 ` Richard Henderson
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Richard Henderson @ 2023-01-15 1:21 UTC (permalink / raw)
To: qemu-devel; +Cc: pbonzini
Failure to truncate the inputs results in garbage for the carry-out.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1373
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
tests/tcg/x86_64/adox.c | 69 ++++++++++++++++++++++++++++++++
target/i386/tcg/emit.c.inc | 2 +
tests/tcg/x86_64/Makefile.target | 3 ++
3 files changed, 74 insertions(+)
create mode 100644 tests/tcg/x86_64/adox.c
diff --git a/tests/tcg/x86_64/adox.c b/tests/tcg/x86_64/adox.c
new file mode 100644
index 0000000000..36be644c8b
--- /dev/null
+++ b/tests/tcg/x86_64/adox.c
@@ -0,0 +1,69 @@
+/* See if ADOX give expected results */
+
+#include <assert.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+static uint64_t adoxq(bool *c_out, uint64_t a, uint64_t b, bool c)
+{
+ asm ("addl $0x7fffffff, %k1\n\t"
+ "adoxq %2, %0\n\t"
+ "seto %b1"
+ : "+r"(a), "=&r"(c) : "r"(b), "1"((int)c));
+ *c_out = c;
+ return a;
+}
+
+static uint64_t adoxl(bool *c_out, uint64_t a, uint64_t b, bool c)
+{
+ asm ("addl $0x7fffffff, %k1\n\t"
+ "adoxl %k2, %k0\n\t"
+ "seto %b1"
+ : "+r"(a), "=&r"(c) : "r"(b), "1"((int)c));
+ *c_out = c;
+ return a;
+}
+
+int main()
+{
+ uint64_t r;
+ bool c;
+
+ r = adoxq(&c, 0, 0, 0);
+ assert(r == 0);
+ assert(c == 0);
+
+ r = adoxl(&c, 0, 0, 0);
+ assert(r == 0);
+ assert(c == 0);
+
+ r = adoxl(&c, 0x100000000, 0, 0);
+ assert(r == 0);
+ assert(c == 0);
+
+ r = adoxq(&c, 0, 0, 1);
+ assert(r == 1);
+ assert(c == 0);
+
+ r = adoxl(&c, 0, 0, 1);
+ assert(r == 1);
+ assert(c == 0);
+
+ r = adoxq(&c, -1, -1, 0);
+ assert(r == -2);
+ assert(c == 1);
+
+ r = adoxl(&c, -1, -1, 0);
+ assert(r == 0xfffffffe);
+ assert(c == 1);
+
+ r = adoxq(&c, -1, -1, 1);
+ assert(r == -1);
+ assert(c == 1);
+
+ r = adoxl(&c, -1, -1, 1);
+ assert(r == 0xffffffff);
+ assert(c == 1);
+
+ return 0;
+}
diff --git a/target/i386/tcg/emit.c.inc b/target/i386/tcg/emit.c.inc
index 1eace1231a..d44c51209d 100644
--- a/target/i386/tcg/emit.c.inc
+++ b/target/i386/tcg/emit.c.inc
@@ -1042,6 +1042,8 @@ static void gen_ADCOX(DisasContext *s, CPUX86State *env, MemOp ot, int cc_op)
#ifdef TARGET_X86_64
case MO_32:
/* If TL is 64-bit just do everything in 64-bit arithmetic. */
+ tcg_gen_ext32u_tl(s->T0, s->T0);
+ tcg_gen_ext32u_tl(s->T1, s->T1);
tcg_gen_add_i64(s->T0, s->T0, s->T1);
tcg_gen_add_i64(s->T0, s->T0, carry_in);
tcg_gen_shri_i64(carry_out, s->T0, 32);
diff --git a/tests/tcg/x86_64/Makefile.target b/tests/tcg/x86_64/Makefile.target
index 4eac78293f..e64aab1b81 100644
--- a/tests/tcg/x86_64/Makefile.target
+++ b/tests/tcg/x86_64/Makefile.target
@@ -12,11 +12,14 @@ ifeq ($(filter %-linux-user, $(TARGET)),$(TARGET))
X86_64_TESTS += vsyscall
X86_64_TESTS += noexec
X86_64_TESTS += cmpxchg
+X86_64_TESTS += adox
TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64
else
TESTS=$(MULTIARCH_TESTS)
endif
+adox: CFLAGS=-O2
+
run-test-i386-ssse3: QEMU_OPTS += -cpu max
run-plugin-test-i386-ssse3-%: QEMU_OPTS += -cpu max
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] target/i386: Fix 32-bit AD[CO]X insns in 64-bit mode
2023-01-15 1:21 [PATCH] target/i386: Fix 32-bit AD[CO]X insns in 64-bit mode Richard Henderson
@ 2023-02-16 6:51 ` Richard Henderson
2023-02-16 7:26 ` Philippe Mathieu-Daudé
2023-02-16 14:09 ` Paolo Bonzini
2 siblings, 0 replies; 4+ messages in thread
From: Richard Henderson @ 2023-02-16 6:51 UTC (permalink / raw)
To: qemu-devel; +Cc: pbonzini
Ping.
Paolo, I see you've queued a fix for a different ADCOX bug in your latest pull. You could
probably adjust your new test for this case, but this problem is exclusively x86_64.
r~
On 1/14/23 15:21, Richard Henderson wrote:
> Failure to truncate the inputs results in garbage for the carry-out.
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1373
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
> tests/tcg/x86_64/adox.c | 69 ++++++++++++++++++++++++++++++++
> target/i386/tcg/emit.c.inc | 2 +
> tests/tcg/x86_64/Makefile.target | 3 ++
> 3 files changed, 74 insertions(+)
> create mode 100644 tests/tcg/x86_64/adox.c
>
> diff --git a/tests/tcg/x86_64/adox.c b/tests/tcg/x86_64/adox.c
> new file mode 100644
> index 0000000000..36be644c8b
> --- /dev/null
> +++ b/tests/tcg/x86_64/adox.c
> @@ -0,0 +1,69 @@
> +/* See if ADOX give expected results */
> +
> +#include <assert.h>
> +#include <stdint.h>
> +#include <stdbool.h>
> +
> +static uint64_t adoxq(bool *c_out, uint64_t a, uint64_t b, bool c)
> +{
> + asm ("addl $0x7fffffff, %k1\n\t"
> + "adoxq %2, %0\n\t"
> + "seto %b1"
> + : "+r"(a), "=&r"(c) : "r"(b), "1"((int)c));
> + *c_out = c;
> + return a;
> +}
> +
> +static uint64_t adoxl(bool *c_out, uint64_t a, uint64_t b, bool c)
> +{
> + asm ("addl $0x7fffffff, %k1\n\t"
> + "adoxl %k2, %k0\n\t"
> + "seto %b1"
> + : "+r"(a), "=&r"(c) : "r"(b), "1"((int)c));
> + *c_out = c;
> + return a;
> +}
> +
> +int main()
> +{
> + uint64_t r;
> + bool c;
> +
> + r = adoxq(&c, 0, 0, 0);
> + assert(r == 0);
> + assert(c == 0);
> +
> + r = adoxl(&c, 0, 0, 0);
> + assert(r == 0);
> + assert(c == 0);
> +
> + r = adoxl(&c, 0x100000000, 0, 0);
> + assert(r == 0);
> + assert(c == 0);
> +
> + r = adoxq(&c, 0, 0, 1);
> + assert(r == 1);
> + assert(c == 0);
> +
> + r = adoxl(&c, 0, 0, 1);
> + assert(r == 1);
> + assert(c == 0);
> +
> + r = adoxq(&c, -1, -1, 0);
> + assert(r == -2);
> + assert(c == 1);
> +
> + r = adoxl(&c, -1, -1, 0);
> + assert(r == 0xfffffffe);
> + assert(c == 1);
> +
> + r = adoxq(&c, -1, -1, 1);
> + assert(r == -1);
> + assert(c == 1);
> +
> + r = adoxl(&c, -1, -1, 1);
> + assert(r == 0xffffffff);
> + assert(c == 1);
> +
> + return 0;
> +}
> diff --git a/target/i386/tcg/emit.c.inc b/target/i386/tcg/emit.c.inc
> index 1eace1231a..d44c51209d 100644
> --- a/target/i386/tcg/emit.c.inc
> +++ b/target/i386/tcg/emit.c.inc
> @@ -1042,6 +1042,8 @@ static void gen_ADCOX(DisasContext *s, CPUX86State *env, MemOp ot, int cc_op)
> #ifdef TARGET_X86_64
> case MO_32:
> /* If TL is 64-bit just do everything in 64-bit arithmetic. */
> + tcg_gen_ext32u_tl(s->T0, s->T0);
> + tcg_gen_ext32u_tl(s->T1, s->T1);
> tcg_gen_add_i64(s->T0, s->T0, s->T1);
> tcg_gen_add_i64(s->T0, s->T0, carry_in);
> tcg_gen_shri_i64(carry_out, s->T0, 32);
> diff --git a/tests/tcg/x86_64/Makefile.target b/tests/tcg/x86_64/Makefile.target
> index 4eac78293f..e64aab1b81 100644
> --- a/tests/tcg/x86_64/Makefile.target
> +++ b/tests/tcg/x86_64/Makefile.target
> @@ -12,11 +12,14 @@ ifeq ($(filter %-linux-user, $(TARGET)),$(TARGET))
> X86_64_TESTS += vsyscall
> X86_64_TESTS += noexec
> X86_64_TESTS += cmpxchg
> +X86_64_TESTS += adox
> TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64
> else
> TESTS=$(MULTIARCH_TESTS)
> endif
>
> +adox: CFLAGS=-O2
> +
> run-test-i386-ssse3: QEMU_OPTS += -cpu max
> run-plugin-test-i386-ssse3-%: QEMU_OPTS += -cpu max
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] target/i386: Fix 32-bit AD[CO]X insns in 64-bit mode
2023-01-15 1:21 [PATCH] target/i386: Fix 32-bit AD[CO]X insns in 64-bit mode Richard Henderson
2023-02-16 6:51 ` Richard Henderson
@ 2023-02-16 7:26 ` Philippe Mathieu-Daudé
2023-02-16 14:09 ` Paolo Bonzini
2 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-02-16 7:26 UTC (permalink / raw)
To: Richard Henderson, qemu-devel; +Cc: pbonzini
On 15/1/23 02:21, Richard Henderson wrote:
> Failure to truncate the inputs results in garbage for the carry-out.
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1373
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
> tests/tcg/x86_64/adox.c | 69 ++++++++++++++++++++++++++++++++
> target/i386/tcg/emit.c.inc | 2 +
> tests/tcg/x86_64/Makefile.target | 3 ++
> 3 files changed, 74 insertions(+)
> create mode 100644 tests/tcg/x86_64/adox.c
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] target/i386: Fix 32-bit AD[CO]X insns in 64-bit mode
2023-01-15 1:21 [PATCH] target/i386: Fix 32-bit AD[CO]X insns in 64-bit mode Richard Henderson
2023-02-16 6:51 ` Richard Henderson
2023-02-16 7:26 ` Philippe Mathieu-Daudé
@ 2023-02-16 14:09 ` Paolo Bonzini
2 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2023-02-16 14:09 UTC (permalink / raw)
To: Richard Henderson; +Cc: qemu-devel, pbonzini
Queued, thanks.
Paolo
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-02-16 14:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-15 1:21 [PATCH] target/i386: Fix 32-bit AD[CO]X insns in 64-bit mode Richard Henderson
2023-02-16 6:51 ` Richard Henderson
2023-02-16 7:26 ` Philippe Mathieu-Daudé
2023-02-16 14:09 ` Paolo Bonzini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).