Skip to contentSkip to conversation

target/sparc: Stop using the legacy native-endian APIs

PATCH Superseded Philippe Mathieu-Daudé 1 participant 12 messages Dec 19, 2025 – Dec 22, 2025

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover target/sparc: Stop using the legacy native-endian APIs

SPARC is always big-endian, so use the '_be' suffix to select the big-endian variant of the load/store helper.

Simplify gdbstub using ldn(TARGET_LONG_BITS...).

Based-on: <[email protected]> and more.

Philippe Mathieu-Daudé (9):
hw/sparc: Use explicit big-endian LD/ST API target/sparc: Replace MO_TE -> MO_BE
target/sparc: Use address_space_ld/st[n]_be() in ld/st_asi helpers target/sparc: Use explicit big-endian LD/ST API target/sparc: Use big-endian variant of cpu_ld/st_data*() target/sparc: Inline translator_ldl()
target/sparc: Inline cpu_ldl_code() call in cpu_do_interrupt() target/sparc: Simplify gdbstub sparc_cpu_gdb_write_register() configs/targets: Forbid SPARC to use legacy native endianness APIs

 configs/targets/sparc-linux-user.mak       |  1 -
 configs/targets/sparc-softmmu.mak          |  1 -
 configs/targets/sparc32plus-linux-user.mak |  1 -
 configs/targets/sparc64-linux-user.mak     |  1 -
 configs/targets/sparc64-softmmu.mak        |  1 -
 hw/sparc/leon3.c                           | 42 +++++++-------
 hw/sparc/sun4m.c                           |  6 +-
 hw/sparc64/sun4u.c                         |  6 +-
 target/sparc/gdbstub.c                     | 12 +---
 target/sparc/int32_helper.c                |  4 +-
 target/sparc/ldst_helper.c                 | 66 ++++++----------------
 target/sparc/mmu_helper.c                  | 32 ++++++-----
 target/sparc/translate.c                   | 60 ++++++++++----------
 13 files changed, 95 insertions(+), 138 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 1/9 hw/sparc: Use explicit big-endian LD/ST API

The SPARC architecture uses big endianness. Directly use the big-endian LD/ST API.

Mechanical change running:

  $ for a in uw w l q; do \
      sed -i -e "s/ld${a}_p(/ld${a}_be_p(/" \
        $(git grep -wlE '(ld|st)u?[wlq]_p' hw/sparc*/);
    done
---
 hw/sparc/leon3.c   | 42 +++++++++++++++++++++---------------------
 hw/sparc/sun4m.c   |  6 +++---
 hw/sparc64/sun4u.c |  6 +++---
 3 files changed, 27 insertions(+), 27 deletions(-)
hw/sparc/leon3.c +21 -21
index 631c6113b5c..0414e9df573 100644
@@ -78,21 +78,21 @@ typedef struct ResetData {
 
 static uint32_t *gen_store_u32(uint32_t *code, hwaddr addr, uint32_t val)
 {
-    stl_p(code++, 0x82100000); /* mov %g0, %g1                */
-    stl_p(code++, 0x84100000); /* mov %g0, %g2                */
-    stl_p(code++, 0x03000000 +
+    stl_be_p(code++, 0x82100000); /* mov %g0, %g1             */
+    stl_be_p(code++, 0x84100000); /* mov %g0, %g2             */
+    stl_be_p(code++, 0x03000000 +
       extract32(addr, 10, 22));
                                /* sethi %hi(addr), %g1        */
-    stl_p(code++, 0x82106000 +
+    stl_be_p(code++, 0x82106000 +
       extract32(addr, 0, 10));
                                /* or %g1, addr, %g1           */
-    stl_p(code++, 0x05000000 +
+    stl_be_p(code++, 0x05000000 +
       extract32(val, 10, 22));
                                /* sethi %hi(val), %g2         */
-    stl_p(code++, 0x8410a000 +
+    stl_be_p(code++, 0x8410a000 +
       extract32(val, 0, 10));
                                /* or %g2, val, %g2            */
-    stl_p(code++, 0xc4204000); /* st %g2, [ %g1 ]             */
+    stl_be_p(code++, 0xc4204000); /* st %g2, [ %g1 ]          */
 
     return code;
 }
@@ -112,13 +112,13 @@ static void write_bootloader(void *ptr, hwaddr kernel_addr)
 
     /* If we are running on a secondary CPU, jump directly to the kernel.  */
 
-    stl_p(p++, 0x85444000); /* rd %asr17, %g2      */
-    stl_p(p++, 0x8530a01c); /* srl  %g2, 0x1c, %g2 */
-    stl_p(p++, 0x80908000); /* tst  %g2            */
+    stl_be_p(p++, 0x85444000); /* rd %asr17, %g2      */
+    stl_be_p(p++, 0x8530a01c); /* srl  %g2, 0x1c, %g2 */
+    stl_be_p(p++, 0x80908000); /* tst  %g2            */
     /* Filled below.  */
     sec_cpu_branch_p = p;
-    stl_p(p++, 0x0BADC0DE); /* bne xxx             */
-    stl_p(p++, 0x01000000); /* nop */
+    stl_be_p(p++, 0x0BADC0DE); /* bne xxx             */
+    stl_be_p(p++, 0x01000000); /* nop */
 
     /* Initialize the UARTs                                        */
     /* *UART_CONTROL = UART_RECEIVE_ENABLE | UART_TRANSMIT_ENABLE; */
@@ -133,17 +133,17 @@ static void write_bootloader(void *ptr, hwaddr kernel_addr)
     p = gen_store_u32(p, 0x80000318, 3);
 
     /* Now, the relative branch above can be computed.  */
-    stl_p(sec_cpu_branch_p, 0x12800000
-          + (p - sec_cpu_branch_p));
+    stl_be_p(sec_cpu_branch_p, 0x12800000
+             + (p - sec_cpu_branch_p));
 
     /* JUMP to the entry point                                     */
-    stl_p(p++, 0x82100000); /* mov %g0, %g1 */
-    stl_p(p++, 0x03000000 + extract32(kernel_addr, 10, 22));
-                            /* sethi %hi(kernel_addr), %g1 */
-    stl_p(p++, 0x82106000 + extract32(kernel_addr, 0, 10));
-                            /* or kernel_addr, %g1 */
-    stl_p(p++, 0x81c04000); /* jmp  %g1 */
-    stl_p(p++, 0x01000000); /* nop */
+    stl_be_p(p++, 0x82100000); /* mov %g0, %g1 */
+    stl_be_p(p++, 0x03000000 + extract32(kernel_addr, 10, 22));
+                               /* sethi %hi(kernel_addr), %g1 */
+    stl_be_p(p++, 0x82106000 + extract32(kernel_addr, 0, 10));
+                               /* or kernel_addr, %g1 */
+    stl_be_p(p++, 0x81c04000); /* jmp  %g1 */
+    stl_be_p(p++, 0x01000000); /* nop */
 }
 
 static void leon3_cpu_reset(void *opaque)
hw/sparc/sun4m.c +3 -3
index 53d7ae08ae9..52ca9586001 100644
@@ -268,9 +268,9 @@ static unsigned long sun4m_load_kernel(const char *kernel_filename,
         if (*initrd_size > 0) {
             for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
                 ptr = rom_ptr(KERNEL_LOAD_ADDR + i, 24);
-                if (ptr && ldl_p(ptr) == 0x48647253) { /* HdrS */
-                    stl_p(ptr + 16, INITRD_LOAD_ADDR);
-                    stl_p(ptr + 20, *initrd_size);
+                if (ptr && ldl_be_p(ptr) == 0x48647253) { /* HdrS */
+                    stl_be_p(ptr + 16, INITRD_LOAD_ADDR);
+                    stl_be_p(ptr + 20, *initrd_size);
                     break;
                 }
             }
hw/sparc64/sun4u.c +3 -3
index 82c3e7c855b..e83c82fc9ae 100644
@@ -206,9 +206,9 @@ static uint64_t sun4u_load_kernel(const char *kernel_filename,
         if (*initrd_size > 0) {
             for (i = 0; i < 64 * TARGET_PAGE_SIZE; i += TARGET_PAGE_SIZE) {
                 ptr = rom_ptr(*kernel_addr + i, 32);
-                if (ptr && ldl_p(ptr + 8) == 0x48647253) { /* HdrS */
-                    stl_p(ptr + 24, *initrd_addr + *kernel_addr);
-                    stl_p(ptr + 28, *initrd_size);
+                if (ptr && ldl_be_p(ptr + 8) == 0x48647253) { /* HdrS */
+                    stl_be_p(ptr + 24, *initrd_addr + *kernel_addr);
+                    stl_be_p(ptr + 28, *initrd_size);
                     break;
                 }
             }
Signature · 1 line
2.52.0
#2 lore raw

PATCH v2 2/9 target/sparc: Replace MO_TE -> MO_BE

In reply to Philippe Mathieu-Daudé

We only build the SPARC targets using big endianness order, therefore the MO_TE definitions expand to the big endian one. Use the latter which is more explicit.

Mechanical change running:

  $ sed -i -e s/MO_TE/MO_BE/ \
        $(git grep -wl MO_TE target/sparc/)
---
 target/sparc/ldst_helper.c |  6 ++--
 target/sparc/translate.c   | 58 +++++++++++++++++++-------------------
 2 files changed, 32 insertions(+), 32 deletions(-)
target/sparc/ldst_helper.c +3 -3
index 2c63eb9e036..a87a0b3eee0 100644
@@ -1169,7 +1169,7 @@ uint64_t helper_ld_code(CPUSPARCState *env, target_ulong addr, uint32_t oi)
         break;
     case MO_16:
         ret = cpu_ldw_code_mmu(env, addr, oi, ra);
-        if ((mop & MO_BSWAP) != MO_TE) {
+        if ((mop & MO_BSWAP) != MO_BE) {
             ret = bswap16(ret);
         }
         if (mop & MO_SIGN) {
@@ -1178,7 +1178,7 @@ uint64_t helper_ld_code(CPUSPARCState *env, target_ulong addr, uint32_t oi)
         break;
     case MO_32:
         ret = cpu_ldl_code_mmu(env, addr, oi, ra);
-        if ((mop & MO_BSWAP) != MO_TE) {
+        if ((mop & MO_BSWAP) != MO_BE) {
             ret = bswap32(ret);
         }
         if (mop & MO_SIGN) {
@@ -1187,7 +1187,7 @@ uint64_t helper_ld_code(CPUSPARCState *env, target_ulong addr, uint32_t oi)
         break;
     case MO_64:
         ret = cpu_ldq_code_mmu(env, addr, oi, ra);
-        if ((mop & MO_BSWAP) != MO_TE) {
+        if ((mop & MO_BSWAP) != MO_BE) {
             ret = bswap64(ret);
         }
         break;
target/sparc/translate.c +29 -29

187 lines not on this page · raw message

Signature · 1 line
2.52.0
#3 lore raw

PATCH v2 3/9 target/sparc: Use address_space_ld/st[n]_be() in ld/st_asi helpers

In reply to Philippe Mathieu-Daudé

Do not open-code address_space_{ld,st}n(), use it passing the access size as argument. Directly expand to the big-endian variant (with the '_be' suffix) since we only build the SPARC targets as big-endian.

---
 target/sparc/ldst_helper.c | 42 ++++----------------------------------
 1 file changed, 4 insertions(+), 38 deletions(-)
target/sparc/ldst_helper.c +4 -38
index a87a0b3eee0..e9139814c26 100644
@@ -701,25 +701,8 @@ uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
         MemTxResult result;
         hwaddr access_addr = (hwaddr)addr | ((hwaddr)(asi & 0xf) << 32);
 
-        switch (size) {
-        case 1:
-            ret = address_space_ldub(cs->as, access_addr,
-                                     MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        case 2:
-            ret = address_space_lduw(cs->as, access_addr,
-                                     MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        default:
-        case 4:
-            ret = address_space_ldl(cs->as, access_addr,
-                                    MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        case 8:
-            ret = address_space_ldq(cs->as, access_addr,
-                                    MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        }
+        ret = address_space_ldn_be(cs->as, access_addr, size,
+                                   MEMTXATTRS_UNSPECIFIED, &result);
 
         if (result != MEMTX_OK) {
             sparc_raise_mmu_fault(cs, access_addr, false, false, false,
@@ -1066,25 +1049,8 @@ void helper_st_asi(CPUSPARCState *env, target_ulong addr, uint64_t val,
             MemTxResult result;
             hwaddr access_addr = (hwaddr)addr | ((hwaddr)(asi & 0xf) << 32);
 
-            switch (size) {
-            case 1:
-                address_space_stb(cs->as, access_addr, val,
-                                  MEMTXATTRS_UNSPECIFIED, &result);
-                break;
-            case 2:
-                address_space_stw(cs->as, access_addr, val,
-                                  MEMTXATTRS_UNSPECIFIED, &result);
-                break;
-            case 4:
-            default:
-                address_space_stl(cs->as, access_addr, val,
-                                  MEMTXATTRS_UNSPECIFIED, &result);
-                break;
-            case 8:
-                address_space_stq(cs->as, access_addr, val,
-                                  MEMTXATTRS_UNSPECIFIED, &result);
-                break;
-            }
+            address_space_stn_be(cs->as, access_addr, val, size,
+                                 MEMTXATTRS_UNSPECIFIED, &result);
             if (result != MEMTX_OK) {
                 sparc_raise_mmu_fault(cs, access_addr, true, false, false,
                                       size, GETPC());
Signature · 1 line
2.52.0
#4 lore raw

Re: [PATCH v2 3/9] target/sparc: Use address_space_ld/st[n]_be() in ld/st_asi helpers

Quoted 18 lines · Philippe Mathieu-Daudé

Do not open-code address_space_{ld,st}n(), use it passing the access size as argument. Directly expand to the big-endian variant (with the '_be' suffix) since we only build the SPARC targets as big-endian.

---
  target/sparc/ldst_helper.c | 42 ++++----------------------------------
  1 file changed, 4 insertions(+), 38 deletions(-)
target/sparc/ldst_helper.c +0 -0
index a87a0b3eee0..e9139814c26 100644
@@ -701,25 +701,8 @@ uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
          MemTxResult result;
          hwaddr access_addr = (hwaddr)addr | ((hwaddr)(asi & 0xf) << 32);
 

target/sparc/ldst_helper.c uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr, · line 713

Quoted 10 lines
target/sparc/ldst_helper.c +0 -10
-        switch (size) {
-        case 1:
-            ret = address_space_ldub(cs->as, access_addr,
-                                     MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        case 2:
-            ret = address_space_lduw(cs->as, access_addr,
-                                     MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        default:

Unfortunately I missed this 'default' case (which I don't understand), so this patch is not a faithful API conversion, thus incorrect.

Quoted 19 lines
target/sparc/ldst_helper.c +2 -10
-        case 4:
-            ret = address_space_ldl(cs->as, access_addr,
-                                    MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        case 8:
-            ret = address_space_ldq(cs->as, access_addr,
-                                    MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        }
+        ret = address_space_ldn_be(cs->as, access_addr, size,
+                                   MEMTXATTRS_UNSPECIFIED, &result);
 
          if (result != MEMTX_OK) {
              sparc_raise_mmu_fault(cs, access_addr, false, false, false,
@@ -1066,25 +1049,8 @@ void helper_st_asi(CPUSPARCState *env, target_ulong addr, uint64_t val,
              MemTxResult result;
              hwaddr access_addr = (hwaddr)addr | ((hwaddr)(asi & 0xf) << 32);
 
-            switch (size) {

target/sparc/ldst_helper.c void helper_st_asi(CPUSPARCState *env, target_ulong addr, uint64_t val, · line 1079

Quoted 10 lines
target/sparc/ldst_helper.c +0 -10
-            case 1:
-                address_space_stb(cs->as, access_addr, val,
-                                  MEMTXATTRS_UNSPECIFIED, &result);
-                break;
-            case 2:
-                address_space_stw(cs->as, access_addr, val,
-                                  MEMTXATTRS_UNSPECIFIED, &result);
-                break;
-            case 4:
-            default:

Ditto.

Quoted 13 lines
target/sparc/ldst_helper.c +2 -8
-                address_space_stl(cs->as, access_addr, val,
-                                  MEMTXATTRS_UNSPECIFIED, &result);
-                break;
-            case 8:
-                address_space_stq(cs->as, access_addr, val,
-                                  MEMTXATTRS_UNSPECIFIED, &result);
-                break;
-            }
+            address_space_stn_be(cs->as, access_addr, val, size,
+                                 MEMTXATTRS_UNSPECIFIED, &result);
              if (result != MEMTX_OK) {
                  sparc_raise_mmu_fault(cs, access_addr, true, false, false,
                                        size, GETPC());
#5 lore raw

Re: [PATCH v2 3/9] target/sparc: Use address_space_ld/st[n]_be() in ld/st_asi helpers

Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Philippe Mathieu-Daudé

Do not open-code address_space_{ld,st}n(), use it passing the access size as argument. Directly expand to the big-endian variant (with the '_be' suffix) since we only build the SPARC targets as big-endian.

---
  target/sparc/ldst_helper.c | 42 ++++----------------------------------

  1 file changed, 4 insertions(+), 38 deletions(-)
target/sparc/ldst_helper.c +0 -5
index a87a0b3eee0..e9139814c26 100644
@@ -701,25 +701,8 @@ uint64_t helper_ld_asi(CPUSPARCState *env, 
 target_ulong addr,
           MemTxResult result;
           hwaddr access_addr = (hwaddr)addr | ((hwaddr)(asi & 0xf) << 32);
-        switch (size) {
-        case 1:
-            ret = address_space_ldub(cs->as, access_addr,
-                                     MEMTXATTRS_UNSPECIFIED, &result);
-            break;
Quoted 20 lines

target/sparc/ldst_helper.c uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr, · line 713

Quoted 5 lines
target/sparc/ldst_helper.c +0 -5
-        case 2:
-            ret = address_space_lduw(cs->as, access_addr,
-                                     MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        default:

Unfortunately I missed this 'default' case (which I don't understand), so this patch is not a faithful API conversion, thus incorrect.

Quoted 11 lines
target/sparc/ldst_helper.c +2 -9
-        case 4:
-            ret = address_space_ldl(cs->as, access_addr,
-                                    MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        case 8:
-            ret = address_space_ldq(cs->as, access_addr,
-                                    MEMTXATTRS_UNSPECIFIED, &result);
-            break;
-        }
+        ret = address_space_ldn_be(cs->as, access_addr, size,
+                                   MEMTXATTRS_UNSPECIFIED, &result);

I just inverted size <-> access_addr :) The default case shouldn't be reachable IMHO.

#6 lore raw

PATCH v2 4/9 target/sparc: Use explicit big-endian LD/ST API

In reply to Philippe Mathieu-Daudé

The SPARC architecture uses big endianness. Directly use the big-endian LD/ST API.

Mechanical change running:

  $ for a in uw w l q; do \
      sed -i -e "s/ld${a}_p(/ld${a}_be_p(/" \
        $(git grep -wlE '(ld|st)u?[wlq]_p' target/sparc/);
    done
---
 target/sparc/ldst_helper.c | 12 ++++++------
 target/sparc/mmu_helper.c  | 32 +++++++++++++++++---------------
 2 files changed, 23 insertions(+), 21 deletions(-)
target/sparc/ldst_helper.c +6 -6
index e9139814c26..043711f11dc 100644
@@ -861,10 +861,10 @@ void helper_st_asi(CPUSPARCState *env, target_ulong addr, uint64_t val,
                 MemTxResult result;
                 hwaddr access_addr = (env->mxccregs[0] & 0xffffffffULL) + 8 * i;
 
-                env->mxccdata[i] = address_space_ldq(cs->as,
-                                                     access_addr,
-                                                     MEMTXATTRS_UNSPECIFIED,
-                                                     &result);
+                env->mxccdata[i] = address_space_ldq_be(cs->as,
+                                                        access_addr,
+                                                        MEMTXATTRS_UNSPECIFIED,
+                                                        &result);
                 if (result != MEMTX_OK) {
                     /* TODO: investigate whether this is the right behaviour */
                     sparc_raise_mmu_fault(cs, access_addr, false, false,
@@ -889,8 +889,8 @@ void helper_st_asi(CPUSPARCState *env, target_ulong addr, uint64_t val,
                 MemTxResult result;
                 hwaddr access_addr = (env->mxccregs[1] & 0xffffffffULL) + 8 * i;
 
-                address_space_stq(cs->as, access_addr, env->mxccdata[i],
-                                  MEMTXATTRS_UNSPECIFIED, &result);
+                address_space_stq_be(cs->as, access_addr, env->mxccdata[i],
+                                     MEMTXATTRS_UNSPECIFIED, &result);
 
                 if (result != MEMTX_OK) {
                     /* TODO: investigate whether this is the right behaviour */
target/sparc/mmu_helper.c +17 -15
index 46bf500ea83..5a58239d65e 100644
@@ -102,7 +102,8 @@ static int get_physical_address(CPUSPARCState *env, CPUTLBEntryFull *full,
     /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
     /* Context base + context number */
     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
-    pde = address_space_ldl(cs->as, pde_ptr, MEMTXATTRS_UNSPECIFIED, &result);
+    pde = address_space_ldl_be(cs->as, pde_ptr,
+                               MEMTXATTRS_UNSPECIFIED, &result);
     if (result != MEMTX_OK) {
         return 4 << 2; /* Translation fault, L = 0 */
     }
@@ -117,8 +118,8 @@ static int get_physical_address(CPUSPARCState *env, CPUTLBEntryFull *full,
         return 4 << 2;
     case 1: /* L0 PDE */
         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
-        pde = address_space_ldl(cs->as, pde_ptr,
-                                MEMTXATTRS_UNSPECIFIED, &result);
+        pde = address_space_ldl_be(cs->as, pde_ptr,
+                                   MEMTXATTRS_UNSPECIFIED, &result);
         if (result != MEMTX_OK) {
             return (1 << 8) | (4 << 2); /* Translation fault, L = 1 */
         }
@@ -131,8 +132,8 @@ static int get_physical_address(CPUSPARCState *env, CPUTLBEntryFull *full,
             return (1 << 8) | (4 << 2);
         case 1: /* L1 PDE */
             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
-            pde = address_space_ldl(cs->as, pde_ptr,
-                                    MEMTXATTRS_UNSPECIFIED, &result);
+            pde = address_space_ldl_be(cs->as, pde_ptr,
+                                       MEMTXATTRS_UNSPECIFIED, &result);
             if (result != MEMTX_OK) {
                 return (2 << 8) | (4 << 2); /* Translation fault, L = 2 */
             }
@@ -145,8 +146,8 @@ static int get_physical_address(CPUSPARCState *env, CPUTLBEntryFull *full,
                 return (2 << 8) | (4 << 2);
             case 1: /* L2 PDE */
                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
-                pde = address_space_ldl(cs->as, pde_ptr,
-                                        MEMTXATTRS_UNSPECIFIED, &result);
+                pde = address_space_ldl_be(cs->as, pde_ptr,
+                                           MEMTXATTRS_UNSPECIFIED, &result);
                 if (result != MEMTX_OK) {
                     return (3 << 8) | (4 << 2); /* Translation fault, L = 3 */
                 }
@@ -189,7 +190,7 @@ static int get_physical_address(CPUSPARCState *env, CPUTLBEntryFull *full,
         if (is_dirty) {
             pde |= PG_MODIFIED_MASK;
         }
-        stl_phys(cs->as, pde_ptr, pde);
+        stl_be_phys(cs->as, pde_ptr, pde);
     }
 
     /* the page can be put in the TLB */
@@ -276,7 +277,8 @@ target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
     /* Context base + context number */
     pde_ptr = (hwaddr)(env->mmuregs[1] << 4) +
         (env->mmuregs[2] << 2);
-    pde = address_space_ldl(cs->as, pde_ptr, MEMTXATTRS_UNSPECIFIED, &result);
+    pde = address_space_ldl_be(cs->as, pde_ptr,
+                               MEMTXATTRS_UNSPECIFIED, &result);
     if (result != MEMTX_OK) {
         return 0;
     }
@@ -292,8 +294,8 @@ target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
             return pde;
         }
         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
-        pde = address_space_ldl(cs->as, pde_ptr,
-                                MEMTXATTRS_UNSPECIFIED, &result);
+        pde = address_space_ldl_be(cs->as, pde_ptr,
+                                   MEMTXATTRS_UNSPECIFIED, &result);
         if (result != MEMTX_OK) {
             return 0;
         }
@@ -310,8 +312,8 @@ target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
                 return pde;
             }
             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
-            pde = address_space_ldl(cs->as, pde_ptr,
-                                    MEMTXATTRS_UNSPECIFIED, &result);
+            pde = address_space_ldl_be(cs->as, pde_ptr,
+                                       MEMTXATTRS_UNSPECIFIED, &result);
             if (result != MEMTX_OK) {
                 return 0;
             }
@@ -328,8 +330,8 @@ target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
                     return pde;
                 }
                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
-                pde = address_space_ldl(cs->as, pde_ptr,
-                                        MEMTXATTRS_UNSPECIFIED, &result);
+                pde = address_space_ldl_be(cs->as, pde_ptr,
+                                           MEMTXATTRS_UNSPECIFIED, &result);
                 if (result != MEMTX_OK) {
                     return 0;
                 }
Signature · 1 line
2.52.0
#7 lore raw

PATCH v2 5/9 target/sparc: Use big-endian variant of cpu_ld/st_data*()

In reply to Philippe Mathieu-Daudé

We only build the SPARC targets using big endianness order, therefore the cpu_ld/st_data*() definitions expand to the big endian declarations. Use the explicit big-endian variants.

Mechanical change running:

  $ tgt=sparc; \
    end=be; \
    for op in data mmuidx_ra; do \
      for ac in uw sw l q; do \
        sed -i -e "s/cpu_ld${ac}_${op}/cpu_ld${ac}_${end}_${op}/" \
                  $(git grep -l cpu_ target/${tgt}/); \
      done;
      for ac in w l q; do \
        sed -i -e "s/cpu_st${ac}_${op}/cpu_st${ac}_${end}_${op}/" \
                  $(git grep -l cpu_ target/${tgt}/); \
      done;
    done
---
 target/sparc/ldst_helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
target/sparc/ldst_helper.c +3 -3
index 043711f11dc..d879ce4e262 100644
@@ -1194,13 +1194,13 @@ uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
             ret = cpu_ldub_data(env, addr);
             break;
         case 2:
-            ret = cpu_lduw_data(env, addr);
+            ret = cpu_lduw_be_data(env, addr);
             break;
         case 4:
-            ret = cpu_ldl_data(env, addr);
+            ret = cpu_ldl_be_data(env, addr);
             break;
         case 8:
-            ret = cpu_ldq_data(env, addr);
+            ret = cpu_ldq_be_data(env, addr);
             break;
         default:
             g_assert_not_reached();
Signature · 1 line
2.52.0
#8 lore raw

PATCH v2 6/9 target/sparc: Inline translator_ldl()

In reply to Philippe Mathieu-Daudé

translator_ldl() is defined in "exec/translator.h" as:

  198 static inline uint32_t
  199 translator_ldl(CPUArchState *env, DisasContextBase *db, vaddr pc)
  200 {
  201     return translator_ldl_end(env, db, pc, MO_TE);
  202 }

Directly use the inlined form, expanding MO_TE -> MO_BE since we only build the SPARC targets as big-endian.

---
 target/sparc/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/sparc/translate.c +1 -1
index 23079697f52..57b50ff8b9a 100644
@@ -5743,7 +5743,7 @@ static void sparc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
     DisasContext *dc = container_of(dcbase, DisasContext, base);
     unsigned int insn;
 
-    insn = translator_ldl(cpu_env(cs), &dc->base, dc->pc);
+    insn = translator_ldl_end(cpu_env(cs), &dc->base, dc->pc, MO_BE);
     dc->base.pc_next += 4;
 
     if (!decode(dc, insn)) {
Signature · 1 line
2.52.0
#9 lore raw

PATCH v2 7/9 target/sparc: Inline cpu_ldl_code() call in cpu_do_interrupt()

In reply to Philippe Mathieu-Daudé

In preparation of removing the cpu_ldl_code wrapper, inline it.

Since SPARC instructions are always stored in big-endian order, replace MO_TE -> MO_BE.

---
 target/sparc/int32_helper.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
target/sparc/int32_helper.c +3 -1
index b29f693a6bf..d6bb3fa1e54 100644
@@ -149,9 +149,11 @@ void sparc_cpu_do_interrupt(CPUState *cs)
          * mimic delayed trap delivery as if by the subsequent insn.
          */
         if (!env->fsr_qne) {
+            MemOpIdx oi = make_memop_idx(MO_BEUL, cpu_mmu_index(cs, true));
+
             env->fsr_qne = FSR_QNE;
             env->fq.s.addr = env->pc;
-            env->fq.s.insn = cpu_ldl_code(env, env->pc);
+            env->fq.s.insn = cpu_ldl_code_mmu(env, env->pc, oi, 0);
         }
         env->pc = env->npc;
         env->npc = env->npc + 4;
Signature · 1 line
2.52.0
#10 lore raw

PATCH v2 8/9 target/sparc: Simplify gdbstub sparc_cpu_gdb_write_register()

In reply to Philippe Mathieu-Daudé

Rather than ldtul_p() which uses the underlying 'unsigned long' size, use the ldn() variant, passing the access size as argument (evaluating TARGET_LONG_BITS / 8).

No need to use #ifdef'ry to check for TARGET_ABI32, since it is 64-bit:

  $ git grep -E '(ABI32|LONG_BITS)' configs/targets/sparc*
  configs/targets/sparc-linux-user.mak:5:TARGET_LONG_BITS=32
  configs/targets/sparc-softmmu.mak:4:TARGET_LONG_BITS=32
  configs/targets/sparc32plus-linux-user.mak:2:TARGET_ABI32=y
  configs/targets/sparc32plus-linux-user.mak:8:TARGET_LONG_BITS=64
  configs/targets/sparc64-linux-user.mak:8:TARGET_LONG_BITS=64
  configs/targets/sparc64-softmmu.mak:6:TARGET_LONG_BITS=64

Directly expand to the big-endian variant (with the '_be' suffix) since we only build the SPARC targets as big-endian.

---
 target/sparc/gdbstub.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
target/sparc/gdbstub.c +2 -10
index 134617fb232..d265681f6d2 100644
@@ -112,15 +112,7 @@ int sparc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 {
     SPARCCPU *cpu = SPARC_CPU(cs);
     CPUSPARCState *env = &cpu->env;
-#if defined(TARGET_ABI32)
-    uint32_t tmp;
-
-    tmp = ldl_p(mem_buf);
-#else
-    target_ulong tmp;
-
-    tmp = ldtul_p(mem_buf);
-#endif
+    uint64_t tmp = ldn_be_p(mem_buf, TARGET_LONG_BITS / 8);
 
     if (n < 8) {
         /* g0..g7 */
@@ -170,7 +162,7 @@ int sparc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 #else
     else if (n < 64) {
         /* f0-f31 */
-        tmp = ldl_p(mem_buf);
+        tmp = ldl_be_p(mem_buf);
         if (n & 1) {
             env->fpr[(n - 32) / 2].l.lower = tmp;
         } else {
Signature · 1 line
2.52.0
#11 lore raw

PATCH v2 9/9 configs/targets: Forbid SPARC to use legacy native endianness APIs

In reply to Philippe Mathieu-Daudé

All SPARC-related binaries are buildable without a single use of the legacy "native endian" API. Unset the transitional TARGET_USE_LEGACY_NATIVE_ENDIAN_API definition to forbid further uses of the legacy API.

---
 configs/targets/sparc-linux-user.mak       | 1 -
 configs/targets/sparc-softmmu.mak          | 1 -
 configs/targets/sparc32plus-linux-user.mak | 1 -
 configs/targets/sparc64-linux-user.mak     | 1 -
 configs/targets/sparc64-softmmu.mak        | 1 -
 5 files changed, 5 deletions(-)
configs/targets/sparc-linux-user.mak +0 -1
index 1b4ee80fe17..4ff4b7287d2 100644
@@ -3,4 +3,3 @@ TARGET_SYSTBL_ABI=common,32
 TARGET_SYSTBL=syscall.tbl
 TARGET_BIG_ENDIAN=y
 TARGET_LONG_BITS=32
-TARGET_USE_LEGACY_NATIVE_ENDIAN_API=y
configs/targets/sparc-softmmu.mak +0 -1
index f02bacb8bbf..57801faf1fc 100644
@@ -1,4 +1,3 @@
 TARGET_ARCH=sparc
 TARGET_BIG_ENDIAN=y
 TARGET_LONG_BITS=32
-TARGET_USE_LEGACY_NATIVE_ENDIAN_API=y
configs/targets/sparc32plus-linux-user.mak +0 -1
index 75c3a600bf0..7a16934fd17 100644
@@ -6,4 +6,3 @@ TARGET_SYSTBL_ABI=common,32
 TARGET_SYSTBL=syscall.tbl
 TARGET_BIG_ENDIAN=y
 TARGET_LONG_BITS=64
-TARGET_USE_LEGACY_NATIVE_ENDIAN_API=y
configs/targets/sparc64-linux-user.mak +0 -1
index 1b75d276426..7c2ecb7be06 100644
@@ -6,4 +6,3 @@ TARGET_SYSTBL=syscall.tbl
 TARGET_BIG_ENDIAN=y
 TARGET_XML_FILES=gdb-xml/sparc64-core.xml
 TARGET_LONG_BITS=64
-TARGET_USE_LEGACY_NATIVE_ENDIAN_API=y
configs/targets/sparc64-softmmu.mak +0 -1
index bfa1bcfb427..d9d51d21e59 100644
@@ -3,4 +3,3 @@ TARGET_BASE_ARCH=sparc
 TARGET_BIG_ENDIAN=y
 TARGET_XML_FILES=gdb-xml/sparc64-core.xml
 TARGET_LONG_BITS=64
-TARGET_USE_LEGACY_NATIVE_ENDIAN_API=y
Signature · 1 line
2.52.0