Skip to contentSkip to conversation

target/ppc: Fix vector registers access in gdbstub for little-endian

PATCH Applied [email protected] 3 participants 5 messages Aug 26, 2021 – Aug 27, 2021

lore
Share & tools
lore
#0 lore raw

v3 cover target/ppc: Fix vector registers access in gdbstub for little-endian

From: Matheus Ferst <[email protected]>

PPC gdbstub code has two possible swaps of the 64-bit elements of AVR
registers: in gdb_get_avr_reg/gdb_set_avr_reg (based on msr_le) and in gdb_get_reg128/ldq_p (based on TARGET_WORDS_BIGENDIAN).

In softmmu, only the first is done, because TARGET_WORDS_BIGENDIAN is always true. In user mode, both are being done, resulting in swapped high and low doublewords of AVR registers in little-endian binaries.

We fix this by moving the first swap to ppc_maybe_bswap_register, which already handles the endianness swap of each element's value in softmmu and does nothing in user mode.

Based-on: <[email protected]>

Matheus Ferst (2):
include/qemu/int128.h: introduce bswap128s
target/ppc: fix vector registers access in gdbstub for little-endian

 include/qemu/int128.h | 17 ++++++++++++++++-
 target/ppc/gdbstub.c  | 32 +++++++-------------------------
 2 files changed, 23 insertions(+), 26 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/2 include/qemu/int128.h: introduce bswap128s

From: Matheus Ferst <[email protected]>

Changes the current bswap128 implementation to use __builtin_bswap128 when available, adds a bswap128 implementation for !CONFIG_INT128 builds, and introduces bswap128s based on bswap128.

---
 include/qemu/int128.h | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
include/qemu/int128.h +16 -1
index e36c6e6db5..aa7a532727 100644
@@ -1,9 +1,9 @@
 #ifndef INT128_H
 #define INT128_H
 
-#ifdef CONFIG_INT128
 #include "qemu/bswap.h"
 
+#ifdef CONFIG_INT128
 typedef __int128_t Int128;
 
 static inline Int128 int128_make64(uint64_t a)
@@ -155,7 +155,11 @@ static inline void int128_subfrom(Int128 *a, Int128 b)
 
 static inline Int128 bswap128(Int128 a)
 {
+#if __has_builtin(__builtin_bswap128)
+    return __builtin_bswap128(a);
+#else
     return int128_make128(bswap64(int128_gethi(a)), bswap64(int128_getlo(a)));
+#endif
 }
 
 #else /* !CONFIG_INT128 */
@@ -342,5 +346,16 @@ static inline void int128_subfrom(Int128 *a, Int128 b)
     *a = int128_sub(*a, b);
 }
 
+static inline Int128 bswap128(Int128 a)
+{
+    return int128_make128(bswap64(a.hi), bswap64(a.lo));
+}
+
 #endif /* CONFIG_INT128 */
+
+static inline void bswap128s(Int128 *s)
+{
+    *s = bswap128(*s);
+}
+
 #endif /* INT128_H */
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 2/2 target/ppc: fix vector registers access in gdbstub for little-endian

In reply to [email protected]

From: Matheus Ferst <[email protected]>

As vector registers are stored in host endianness, we shouldn't swap its 64-bit elements in user mode. Add a 16-byte case in ppc_maybe_bswap_register to handle the reordering of elements in softmmu and remove avr_need_swap which is now unused.

---
The fix of the order of Int128 fields is in the based-on patchset.

---
 target/ppc/gdbstub.c | 32 +++++++-------------------------
 1 file changed, 7 insertions(+), 25 deletions(-)
target/ppc/gdbstub.c +7 -25
index 09ff1328d4..1808a150e4 100644
@@ -101,6 +101,8 @@ void ppc_maybe_bswap_register(CPUPPCState *env, uint8_t *mem_buf, int len)
         bswap32s((uint32_t *)mem_buf);
     } else if (len == 8) {
         bswap64s((uint64_t *)mem_buf);
+    } else if (len == 16) {
+        bswap128s((Int128 *)mem_buf);
     } else {
         g_assert_not_reached();
     }
@@ -389,15 +391,6 @@ const char *ppc_gdb_get_dynamic_xml(CPUState *cs, const char *xml_name)
 }
 #endif
 
-static bool avr_need_swap(CPUPPCState *env)
-{
-#ifdef HOST_WORDS_BIGENDIAN
-    return msr_le;
-#else
-    return !msr_le;
-#endif
-}
-
 #if !defined(CONFIG_USER_ONLY)
 static int gdb_find_spr_idx(CPUPPCState *env, int n)
 {
@@ -486,14 +479,9 @@ static int gdb_get_avr_reg(CPUPPCState *env, GByteArray *buf, int n)
 
     if (n < 32) {
         ppc_avr_t *avr = cpu_avr_ptr(env, n);
-        if (!avr_need_swap(env)) {
-            gdb_get_reg128(buf, avr->u64[0] , avr->u64[1]);
-        } else {
-            gdb_get_reg128(buf, avr->u64[1] , avr->u64[0]);
-        }
+        gdb_get_reg128(buf, avr->VsrD(0), avr->VsrD(1));
         mem_buf = gdb_get_reg_ptr(buf, 16);
-        ppc_maybe_bswap_register(env, mem_buf, 8);
-        ppc_maybe_bswap_register(env, mem_buf + 8, 8);
+        ppc_maybe_bswap_register(env, mem_buf, 16);
         return 16;
     }
     if (n == 32) {
@@ -515,15 +503,9 @@ static int gdb_set_avr_reg(CPUPPCState *env, uint8_t *mem_buf, int n)
 {
     if (n < 32) {
         ppc_avr_t *avr = cpu_avr_ptr(env, n);
-        ppc_maybe_bswap_register(env, mem_buf, 8);
-        ppc_maybe_bswap_register(env, mem_buf + 8, 8);
-        if (!avr_need_swap(env)) {
-            avr->u64[0] = ldq_p(mem_buf);
-            avr->u64[1] = ldq_p(mem_buf + 8);
-        } else {
-            avr->u64[1] = ldq_p(mem_buf);
-            avr->u64[0] = ldq_p(mem_buf + 8);
-        }
+        ppc_maybe_bswap_register(env, mem_buf, 16);
+        avr->VsrD(0) = ldq_p(mem_buf);
+        avr->VsrD(1) = ldq_p(mem_buf + 8);
         return 16;
     }
     if (n == 32) {
Signature · 1 line
2.25.1
Reviewed-by on v3 2/2
#3 lore raw

Re: [PATCH v3 2/2] target/ppc: fix vector registers access in gdbstub for little-endian

Quoted 7 lines · On 8/26/21 4:56 PM, [email protected] wrote:

From: Matheus Ferst <[email protected]>

As vector registers are stored in host endianness, we shouldn't swap its 64-bit elements in user mode. Add a 16-byte case in ppc_maybe_bswap_register to handle the reordering of elements in softmmu and remove avr_need_swap which is now unused.

Quoted 7 lines

---
The fix of the order of Int128 fields is in the based-on patchset.

---
 target/ppc/gdbstub.c | 32 +++++++-------------------------
 1 file changed, 7 insertions(+), 25 deletions(-)
Applied
#4 lore raw
In reply to [email protected]
Quoted 6 lines · On Thu, Aug 26, 2021 at 11:56:54AM -0300, [email protected] wrote:

From: Matheus Ferst <[email protected]>

PPC gdbstub code has two possible swaps of the 64-bit elements of AVR
registers: in gdb_get_avr_reg/gdb_set_avr_reg (based on msr_le) and in gdb_get_reg128/ldq_p (based on TARGET_WORDS_BIGENDIAN).

Quoted 7 lines

In softmmu, only the first is done, because TARGET_WORDS_BIGENDIAN is always true. In user mode, both are being done, resulting in swapped high and low doublewords of AVR registers in little-endian binaries.

We fix this by moving the first swap to ppc_maybe_bswap_register, which already handles the endianness swap of each element's value in softmmu and does nothing in user mode.

Applied to ppc-for-6.2, thanks.

Quoted 11 lines

Based-on: <[email protected]>

Matheus Ferst (2):
include/qemu/int128.h: introduce bswap128s
target/ppc: fix vector registers access in gdbstub for little-endian

 include/qemu/int128.h | 17 ++++++++++++++++-
 target/ppc/gdbstub.c  | 32 +++++++-------------------------
 2 files changed, 23 insertions(+), 26 deletions(-)
Signature · 4 lines
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