qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts
@ 2021-08-26 14:14 matheus.ferst
  2021-08-26 14:14 ` [PATCH v2 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness matheus.ferst
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: matheus.ferst @ 2021-08-26 14:14 UTC (permalink / raw)
  To: qemu-devel, qemu-ppc
  Cc: peter.maydell, richard.henderson, f4bug, groug, Matheus Ferst, david

From: Matheus Ferst <matheus.ferst@eldorado.org.br>

The definition of struct Int128 is currently independent of the host
endianness, causing different results when using the member s128 of
union ppc_vsr_t in big-endian builds with CONFIG_INT128 or
!CONFIG_INT128.

The only PPC instructions that seem to be affected by this issue are the
"Vector Extract Unsigned Byte/Halfword/Word to GPR using GPR-specified
Left/Right-Index." Even on builds with Int128 support, however, their
helpers give the wrong result on big-endian hosts.

The first patch in this series changes the definition of struct Int128
to allow its use in the ppc_vsr_t union. The second patch fixes the
helper definition.

Matheus Ferst (2):
  include/qemu/int128.h: define struct Int128 according to the host
    endianness
  target/ppc: fix vextu[bhw][lr]x helpers

 include/qemu/int128.h   | 27 ++++++++++++++++++++-------
 target/ppc/int_helper.c | 38 ++++++++++----------------------------
 2 files changed, 30 insertions(+), 35 deletions(-)

-- 
2.25.1



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness
  2021-08-26 14:14 [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts matheus.ferst
@ 2021-08-26 14:14 ` matheus.ferst
  2021-08-26 14:14 ` [PATCH v2 2/2] target/ppc: fix vextu[bhw][lr]x helpers matheus.ferst
  2021-08-27  2:42 ` [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: matheus.ferst @ 2021-08-26 14:14 UTC (permalink / raw)
  To: qemu-devel, qemu-ppc
  Cc: peter.maydell, richard.henderson, f4bug, groug, Matheus Ferst, david

From: Matheus Ferst <matheus.ferst@eldorado.org.br>

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Matheus Ferst <matheus.ferst@eldorado.org.br>
---
 include/qemu/int128.h | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/include/qemu/int128.h b/include/qemu/int128.h
index 64500385e3..17436d851d 100644
--- a/include/qemu/int128.h
+++ b/include/qemu/int128.h
@@ -162,24 +162,37 @@ static inline Int128 bswap128(Int128 a)
 
 typedef struct Int128 Int128;
 
+/*
+ * We guarantee that the in-memory byte representation of an
+ * Int128 is that of a host-endian-order 128-bit integer
+ * (whether using this struct or the __int128_t version of the type).
+ * Some code using this type relies on this (eg when copying it into
+ * guest memory or a gdb protocol buffer, or by using Int128 in
+ * a union with other integer types).
+ */
 struct Int128 {
+#ifdef HOST_WORDS_BIGENDIAN
+    int64_t hi;
+    uint64_t lo;
+#else
     uint64_t lo;
     int64_t hi;
+#endif
 };
 
 static inline Int128 int128_make64(uint64_t a)
 {
-    return (Int128) { a, 0 };
+    return (Int128) { .lo = a, .hi = 0 };
 }
 
 static inline Int128 int128_makes64(int64_t a)
 {
-    return (Int128) { a, a >> 63 };
+    return (Int128) { .lo = a, .hi = a >> 63 };
 }
 
 static inline Int128 int128_make128(uint64_t lo, uint64_t hi)
 {
-    return (Int128) { lo, hi };
+    return (Int128) { .lo = lo, .hi = hi };
 }
 
 static inline uint64_t int128_get64(Int128 a)
@@ -210,22 +223,22 @@ static inline Int128 int128_one(void)
 
 static inline Int128 int128_2_64(void)
 {
-    return (Int128) { 0, 1 };
+    return int128_make128(0, 1);
 }
 
 static inline Int128 int128_exts64(int64_t a)
 {
-    return (Int128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
+    return int128_make128(a, (a < 0) ? -1 : 0);
 }
 
 static inline Int128 int128_and(Int128 a, Int128 b)
 {
-    return (Int128) { a.lo & b.lo, a.hi & b.hi };
+    return int128_make128(a.lo & b.lo, a.hi & b.hi);
 }
 
 static inline Int128 int128_or(Int128 a, Int128 b)
 {
-    return (Int128) { a.lo | b.lo, a.hi | b.hi };
+    return int128_make128(a.lo | b.lo, a.hi | b.hi);
 }
 
 static inline Int128 int128_rshift(Int128 a, int n)
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] target/ppc: fix vextu[bhw][lr]x helpers
  2021-08-26 14:14 [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts matheus.ferst
  2021-08-26 14:14 ` [PATCH v2 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness matheus.ferst
@ 2021-08-26 14:14 ` matheus.ferst
  2021-08-27  2:42 ` [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: matheus.ferst @ 2021-08-26 14:14 UTC (permalink / raw)
  To: qemu-devel, qemu-ppc
  Cc: peter.maydell, richard.henderson, f4bug, groug, Matheus Ferst, david

From: Matheus Ferst <matheus.ferst@eldorado.org.br>

These helpers shouldn't depend on the host endianness, as they only use
shifts, ands, and int128_* methods.

Fixes: 60caf2216bf0 ("target-ppc: add vextu[bhw][lr]x instructions")
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Matheus Ferst <matheus.ferst@eldorado.org.br>
---
 target/ppc/int_helper.c | 38 ++++++++++----------------------------
 1 file changed, 10 insertions(+), 28 deletions(-)

diff --git a/target/ppc/int_helper.c b/target/ppc/int_helper.c
index efa833ef64..c2d3248d1e 100644
--- a/target/ppc/int_helper.c
+++ b/target/ppc/int_helper.c
@@ -1492,34 +1492,16 @@ void helper_vlogefp(CPUPPCState *env, ppc_avr_t *r, ppc_avr_t *b)
     }
 }
 
-#if defined(HOST_WORDS_BIGENDIAN)
-#define VEXTU_X_DO(name, size, left)                                \
-    target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
-    {                                                               \
-        int index;                                                  \
-        if (left) {                                                 \
-            index = (a & 0xf) * 8;                                  \
-        } else {                                                    \
-            index = ((15 - (a & 0xf) + 1) * 8) - size;              \
-        }                                                           \
-        return int128_getlo(int128_rshift(b->s128, index)) &        \
-            MAKE_64BIT_MASK(0, size);                               \
-    }
-#else
-#define VEXTU_X_DO(name, size, left)                                \
-    target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
-    {                                                               \
-        int index;                                                  \
-        if (left) {                                                 \
-            index = ((15 - (a & 0xf) + 1) * 8) - size;              \
-        } else {                                                    \
-            index = (a & 0xf) * 8;                                  \
-        }                                                           \
-        return int128_getlo(int128_rshift(b->s128, index)) &        \
-            MAKE_64BIT_MASK(0, size);                               \
-    }
-#endif
-
+#define VEXTU_X_DO(name, size, left)                            \
+target_ulong glue(helper_, name)(target_ulong a, ppc_avr_t *b)  \
+{                                                               \
+    int index = (a & 0xf) * 8;                                  \
+    if (left) {                                                 \
+        index = 128 - index - size;                             \
+    }                                                           \
+    return int128_getlo(int128_rshift(b->s128, index)) &        \
+        MAKE_64BIT_MASK(0, size);                               \
+}
 VEXTU_X_DO(vextublx,  8, 1)
 VEXTU_X_DO(vextuhlx, 16, 1)
 VEXTU_X_DO(vextuwlx, 32, 1)
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts
  2021-08-26 14:14 [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts matheus.ferst
  2021-08-26 14:14 ` [PATCH v2 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness matheus.ferst
  2021-08-26 14:14 ` [PATCH v2 2/2] target/ppc: fix vextu[bhw][lr]x helpers matheus.ferst
@ 2021-08-27  2:42 ` David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: David Gibson @ 2021-08-27  2:42 UTC (permalink / raw)
  To: matheus.ferst
  Cc: peter.maydell, richard.henderson, f4bug, qemu-devel, groug, qemu-ppc

[-- Attachment #1: Type: text/plain, Size: 1404 bytes --]

On Thu, Aug 26, 2021 at 11:14:44AM -0300, matheus.ferst@eldorado.org.br wrote:
> From: Matheus Ferst <matheus.ferst@eldorado.org.br>
> 
> The definition of struct Int128 is currently independent of the host
> endianness, causing different results when using the member s128 of
> union ppc_vsr_t in big-endian builds with CONFIG_INT128 or
> !CONFIG_INT128.
> 
> The only PPC instructions that seem to be affected by this issue are the
> "Vector Extract Unsigned Byte/Halfword/Word to GPR using GPR-specified
> Left/Right-Index." Even on builds with Int128 support, however, their
> helpers give the wrong result on big-endian hosts.
> 
> The first patch in this series changes the definition of struct Int128
> to allow its use in the ppc_vsr_t union. The second patch fixes the
> helper definition.

Applied to ppc-for-6.2, thanks.

> 
> Matheus Ferst (2):
>   include/qemu/int128.h: define struct Int128 according to the host
>     endianness
>   target/ppc: fix vextu[bhw][lr]x helpers
> 
>  include/qemu/int128.h   | 27 ++++++++++++++++++++-------
>  target/ppc/int_helper.c | 38 ++++++++++----------------------------
>  2 files changed, 30 insertions(+), 35 deletions(-)
> 

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-27  2:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-26 14:14 [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts matheus.ferst
2021-08-26 14:14 ` [PATCH v2 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness matheus.ferst
2021-08-26 14:14 ` [PATCH v2 2/2] target/ppc: fix vextu[bhw][lr]x helpers matheus.ferst
2021-08-27  2:42 ` [PATCH v2 0/2] target/ppc: Fix vextu[bhw][lr]x on big endian hosts David Gibson

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).