Skip to contentSkip to conversation

target/openrisc: Use explicit big-endian LD/ST API

PATCH In review Philippe Mathieu-Daudé 3 participants 14 messages Dec 24, 2025 – Jan 5

lore
Share & tools
lore
#0 lore raw

cover target/openrisc: Use explicit big-endian LD/ST API

OpenRISC is big-endian. Use the explicit 'big'
endianness instead of the 'native' one.
Forbid further uses of legacy APIs.

tag: https://gitlab.com/philmd/qemu/-/tags/endian_openrisc-v1
CI: https://gitlab.com/philmd/qemu/-/pipelines/2231223066

Philippe Mathieu-Daudé (4):
hw/intc: Mark OpenRISC-specific peripheral as big-endian target/openrisc: Use explicit big-endian LD/ST API target/openrisc: Inline translator_ldl()
configs/targets: Forbid OpenRISC to use legacy native endianness APIs

 configs/targets/or1k-linux-user.mak | 1 +
 configs/targets/or1k-softmmu.mak    | 1 +
 hw/intc/ompic.c                     | 2 +-
 target/openrisc/gdbstub.c           | 2 +-
 target/openrisc/translate.c         | 3 ++-
 5 files changed, 6 insertions(+), 3 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/4 hw/intc: Mark OpenRISC-specific peripheral as big-endian

The Open Multi-Processor Interrupt Controller (ompic) is only used by the OpenRISC target, which is only built as big-endian. Therefore the DEVICE_NATIVE_ENDIAN definition expand to DEVICE_BIG_ENDIAN (besides, the DEVICE_LITTLE_ENDIAN case isn't tested). Simplify directly using DEVICE_BIG_ENDIAN.

---
 hw/intc/ompic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/ompic.c +1 -1
index 047c367478d..ee34501f675 100644
@@ -96,7 +96,7 @@ static void ompic_write(void *opaque, hwaddr addr, uint64_t data, unsigned size)
 static const MemoryRegionOps ompic_ops = {
     .read = ompic_read,
     .write = ompic_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_BIG_ENDIAN,
     .impl = {
         .max_access_size = 8,
     },
Signature · 1 line
2.52.0
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] hw/intc: Mark OpenRISC-specific peripheral as big-endian

Quoted 9 lines · Philippe Mathieu-Daudé

The Open Multi-Processor Interrupt Controller (ompic) is only used by the OpenRISC target, which is only built as big-endian. Therefore the DEVICE_NATIVE_ENDIAN definition expand to DEVICE_BIG_ENDIAN (besides, the DEVICE_LITTLE_ENDIAN case isn't tested). Simplify directly using DEVICE_BIG_ENDIAN.

---

Quoted 19 lines
 hw/intc/ompic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/ompic.c +1 -2
index 047c367478d..ee34501f675 100644
@@ -96,7 +96,7 @@ static void ompic_write(void *opaque, hwaddr addr, uint64_t data, unsigned size)
 static const MemoryRegionOps ompic_ops = {
     .read = ompic_read,
     .write = ompic_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_BIG_ENDIAN,
     .impl = {
         .max_access_size = 8,
     },
--

2.52.0

Reviewed-by on v1 1/4
#3 lore raw

Re: [PATCH 1/4] hw/intc: Mark OpenRISC-specific peripheral as big-endian

In reply to Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé

The Open Multi-Processor Interrupt Controller (ompic) is only used by the OpenRISC target, which is only built as big-endian. Therefore the DEVICE_NATIVE_ENDIAN definition expand to DEVICE_BIG_ENDIAN (besides, the DEVICE_LITTLE_ENDIAN case isn't tested). Simplify directly using DEVICE_BIG_ENDIAN.

---
  hw/intc/ompic.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

#4 lore raw

PATCH 2/4 target/openrisc: Use explicit big-endian LD/ST API

In reply to Philippe Mathieu-Daudé

The OpenRISC 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/openrisc/);
    done
---
 target/openrisc/gdbstub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/openrisc/gdbstub.c +1 -1
index 45bba80d878..ca1592c6200 100644
@@ -50,7 +50,7 @@ int openrisc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
     CPUOpenRISCState *env = cpu_env(cs);
     uint32_t tmp;
 
-    tmp = ldl_p(mem_buf);
+    tmp = ldl_be_p(mem_buf);
 
     if (n < 32) {
         cpu_set_gpr(env, n, tmp);
Signature · 1 line
2.52.0
Reviewed-by on v1 2/4
#5 lore raw
Quoted 6 lines · Philippe Mathieu-Daudé

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

Mechanical change running:

Quoted 7 lines
  $ 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/openrisc/);
    done

---

Quoted 19 lines
 target/openrisc/gdbstub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/openrisc/gdbstub.c +1 -2
index 45bba80d878..ca1592c6200 100644
@@ -50,7 +50,7 @@ int openrisc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
     CPUOpenRISCState *env = cpu_env(cs);
     uint32_t tmp;
 
-    tmp = ldl_p(mem_buf);
+    tmp = ldl_be_p(mem_buf);
 
     if (n < 32) {
         cpu_set_gpr(env, n, tmp);
--

2.52.0

Reviewed-by on v1 2/4
#6 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé

The OpenRISC 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/openrisc/);
     done
Quoted 4 lines
---
  target/openrisc/gdbstub.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

#7 lore raw

PATCH 3/4 target/openrisc: 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 OpenRISC targets as big-endian.

---
 target/openrisc/translate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/openrisc/translate.c +2 -1
index 6fa4d6cfa70..a6d550bbc2e 100644
@@ -1558,7 +1558,8 @@ static void openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
 static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
 {
     DisasContext *dc = container_of(dcbase, DisasContext, base);
-    uint32_t insn = translator_ldl(cpu_env(cs), &dc->base, dc->base.pc_next);
+    uint32_t insn = translator_ldl_end(cpu_env(cs), &dc->base,
+                                       dc->base.pc_next, MO_BE);
 
     if (!decode(dc, insn)) {
         gen_illegal_exception(dc);
Signature · 1 line
2.52.0
Reviewed-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] target/openrisc: Inline translator_ldl()

Quoted 9 lines · 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 }
Quoted 5 lines

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

---

Quoted 20 lines
 target/openrisc/translate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/openrisc/translate.c +2 -2
index 6fa4d6cfa70..a6d550bbc2e 100644
@@ -1558,7 +1558,8 @@ static void openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
 static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
 {
     DisasContext *dc = container_of(dcbase, DisasContext, base);
-    uint32_t insn = translator_ldl(cpu_env(cs), &dc->base, dc->base.pc_next);
+    uint32_t insn = translator_ldl_end(cpu_env(cs), &dc->base,
+                                       dc->base.pc_next, MO_BE);
 
     if (!decode(dc, insn)) {
         gen_illegal_exception(dc);
--

2.52.0

Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH 3/4] target/openrisc: Inline translator_ldl()

In reply to Philippe Mathieu-Daudé
Quoted 21 lines · 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 OpenRISC targets as big-endian.

---
  target/openrisc/translate.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
target/openrisc/translate.c +0 -0
index 6fa4d6cfa70..a6d550bbc2e 100644

target/openrisc/translate.c static void openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs) · line 1565

Quoted 9 lines
target/openrisc/translate.c +2 -1
@@ -1558,7 +1558,8 @@ static void openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
  static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
  {
      DisasContext *dc = container_of(dcbase, DisasContext, base);
-    uint32_t insn = translator_ldl(cpu_env(cs), &dc->base, dc->base.pc_next);
+    uint32_t insn = translator_ldl_end(cpu_env(cs), &dc->base,
+                                       dc->base.pc_next, MO_BE);
 
      if (!decode(dc, insn)) {
          gen_illegal_exception(dc);

Use mo_endian(). With that,

r~

#10 lore raw

Re: [PATCH 3/4] target/openrisc: Inline translator_ldl()

Quoted 27 lines · Richard Henderson
Quoted 26 lines · 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 OpenRISC targets as big-endian.

---
  target/openrisc/translate.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
target/openrisc/translate.c +0 -1
index 6fa4d6cfa70..a6d550bbc2e 100644
@@ -1558,7 +1558,8 @@ static void 
 openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
   static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
   {
       DisasContext *dc = container_of(dcbase, DisasContext, base);
-    uint32_t insn = translator_ldl(cpu_env(cs), &dc->base, dc- 
Quoted 7 lines
Quoted 5 lines
target/openrisc/translate.c +2 -0
 >base.pc_next);
+    uint32_t insn = translator_ldl_end(cpu_env(cs), &dc->base,
+                                       dc->base.pc_next, MO_BE);
       if (!decode(dc, insn)) {

          gen_illegal_exception(dc);

Use mo_endian().  With that,

Oops!

Quoted 2 lines

Thanks!

#11 lore raw

PATCH 4/4 configs/targets: Forbid OpenRISC to use legacy native endianness APIs

In reply to Philippe Mathieu-Daudé

All OpenRISC-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.

Preliminary commits allowing this final change:

. 5b67dbf1dc3 target/openrisc: Replace target_ulong -> uint32_t
. 500708331e3 target/openrisc: Inline tcg_gen_trunc_i64_tl()
. 4f6c60683f2 target/openrisc: Replace MO_TE -> MO_BE
. 73cd283e58d target/openrisc: Introduce mo_endian() helper
. 2f737e19a4d target/openrisc: Conceal MO_TE within do_store()
. 22c36c0a20c target/openrisc: Conceal MO_TE within do_load()
. ef797ac0025 target/openrisc: Explode MO_TExx -> MO_TE | MO_xx
. 81e2fb236b7 target/openrisc: Remove 'TARGET_LONG_BITS != 32' dead code . 9dc4862dc4a target/openrisc: Use vaddr type for $pc jumps
. 2367c94cbb1 target/openrisc: Remove target_ulong use in raise_mmu_exception() . 1843e89bec5 target/openrisc: Remove unused cpu_openrisc_map_address_*() handlers . a3c4facd395 target/openrisc: Do not use target_ulong for @mr in MTSPR helper . 2795bc52af4 target/openrisc: Replace VMSTATE_UINTTL() -> VMSTATE_UINT32()

---
 configs/targets/or1k-linux-user.mak | 1 +
 configs/targets/or1k-softmmu.mak    | 1 +
 2 files changed, 2 insertions(+)
configs/targets/or1k-linux-user.mak +1 -0
index 810567a98f9..97d7cb10465 100644
@@ -3,3 +3,4 @@ TARGET_BIG_ENDIAN=y
 TARGET_SYSTBL_ABI=common,32,or1k,time32,stat64,rlimit,renameat
 TARGET_SYSTBL=syscall.tbl
 TARGET_LONG_BITS=32
+TARGET_NOT_USING_LEGACY_NATIVE_ENDIAN_API=y
configs/targets/or1k-softmmu.mak +1 -0
index 89f81b75bf7..c53408a2bf5 100644
@@ -4,3 +4,4 @@ TARGET_BIG_ENDIAN=y
 TARGET_NEED_FDT=y
 TARGET_LONG_BITS=32
 TARGET_NOT_USING_LEGACY_LDST_PHYS_API=y
+TARGET_NOT_USING_LEGACY_NATIVE_ENDIAN_API=y
Signature · 1 line
2.52.0
Reviewed-by on v1 4/4
#12 lore raw

Re: [PATCH 4/4] configs/targets: Forbid OpenRISC to use legacy native endianness APIs

Quoted 22 lines · Philippe Mathieu-Daudé

All OpenRISC-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.

Preliminary commits allowing this final change:

. 5b67dbf1dc3 target/openrisc: Replace target_ulong -> uint32_t
. 500708331e3 target/openrisc: Inline tcg_gen_trunc_i64_tl()
. 4f6c60683f2 target/openrisc: Replace MO_TE -> MO_BE
. 73cd283e58d target/openrisc: Introduce mo_endian() helper
. 2f737e19a4d target/openrisc: Conceal MO_TE within do_store()
. 22c36c0a20c target/openrisc: Conceal MO_TE within do_load()
. ef797ac0025 target/openrisc: Explode MO_TExx -> MO_TE | MO_xx
. 81e2fb236b7 target/openrisc: Remove 'TARGET_LONG_BITS != 32' dead code . 9dc4862dc4a target/openrisc: Use vaddr type for $pc jumps
. 2367c94cbb1 target/openrisc: Remove target_ulong use in raise_mmu_exception() . 1843e89bec5 target/openrisc: Remove unused cpu_openrisc_map_address_*() handlers . a3c4facd395 target/openrisc: Do not use target_ulong for @mr in MTSPR helper . 2795bc52af4 target/openrisc: Replace VMSTATE_UINTTL() -> VMSTATE_UINT32()

Quoted 2 lines

---

Quoted 25 lines
 configs/targets/or1k-linux-user.mak | 1 +
 configs/targets/or1k-softmmu.mak    | 1 +
 2 files changed, 2 insertions(+)
configs/targets/or1k-linux-user.mak +1 -0
index 810567a98f9..97d7cb10465 100644
@@ -3,3 +3,4 @@ TARGET_BIG_ENDIAN=y
 TARGET_SYSTBL_ABI=common,32,or1k,time32,stat64,rlimit,renameat
 TARGET_SYSTBL=syscall.tbl
 TARGET_LONG_BITS=32
+TARGET_NOT_USING_LEGACY_NATIVE_ENDIAN_API=y
configs/targets/or1k-softmmu.mak +1 -1
index 89f81b75bf7..c53408a2bf5 100644
@@ -4,3 +4,4 @@ TARGET_BIG_ENDIAN=y
 TARGET_NEED_FDT=y
 TARGET_LONG_BITS=32
 TARGET_NOT_USING_LEGACY_LDST_PHYS_API=y
+TARGET_NOT_USING_LEGACY_NATIVE_ENDIAN_API=y
--

2.52.0

Reviewed-by on v1 4/4
#13 lore raw

Re: [PATCH 4/4] configs/targets: Forbid OpenRISC to use legacy native endianness APIs

In reply to Philippe Mathieu-Daudé
Quoted 21 lines · Philippe Mathieu-Daudé

All OpenRISC-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.

Preliminary commits allowing this final change:

. 5b67dbf1dc3 target/openrisc: Replace target_ulong -> uint32_t
. 500708331e3 target/openrisc: Inline tcg_gen_trunc_i64_tl()
. 4f6c60683f2 target/openrisc: Replace MO_TE -> MO_BE
. 73cd283e58d target/openrisc: Introduce mo_endian() helper
. 2f737e19a4d target/openrisc: Conceal MO_TE within do_store()
. 22c36c0a20c target/openrisc: Conceal MO_TE within do_load()
. ef797ac0025 target/openrisc: Explode MO_TExx -> MO_TE | MO_xx
. 81e2fb236b7 target/openrisc: Remove 'TARGET_LONG_BITS != 32' dead code . 9dc4862dc4a target/openrisc: Use vaddr type for $pc jumps
. 2367c94cbb1 target/openrisc: Remove target_ulong use in raise_mmu_exception() . 1843e89bec5 target/openrisc: Remove unused cpu_openrisc_map_address_*() handlers . a3c4facd395 target/openrisc: Do not use target_ulong for @mr in MTSPR helper . 2795bc52af4 target/openrisc: Replace VMSTATE_UINTTL() -> VMSTATE_UINT32()

Quoted 5 lines
---
  configs/targets/or1k-linux-user.mak | 1 +
  configs/targets/or1k-softmmu.mak    | 1 +
  2 files changed, 2 insertions(+)

r~