[PATCH 13/13] target/riscv: Introduce mo_endian_env() helper

Philippe Mathieu-Daudé posted 13 patches 1 month ago
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <dbarboza@ventanamicro.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Christoph Muellner <christoph.muellner@vrull.eu>
[PATCH 13/13] target/riscv: Introduce mo_endian_env() helper
Posted by Philippe Mathieu-Daudé 1 month ago
mo_endian_env() returns the target endianness from CPUArchState.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/riscv/op_helper.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c
index c486f771d35..9d048089e2a 100644
--- a/target/riscv/op_helper.c
+++ b/target/riscv/op_helper.c
@@ -28,6 +28,18 @@
 #include "exec/tlb-flags.h"
 #include "trace.h"
 
+static inline MemOp mo_endian_env(CPURISCVState *env)
+{
+    /*
+     * A couple of bits in MSTATUS set the endianness:
+     *  - MSTATUS_UBE (User-mode),
+     *  - MSTATUS_SBE (Supervisor-mode),
+     *  - MSTATUS_MBE (Machine-mode)
+     * but we don't implement that yet.
+     */
+    return MO_TE;
+}
+
 /* Exceptions processing helpers */
 G_NORETURN void riscv_raise_exception(CPURISCVState *env,
                                       RISCVException exception,
@@ -633,7 +645,7 @@ target_ulong helper_hyp_hlv_hu(CPURISCVState *env, target_ulong addr)
 {
     uintptr_t ra = GETPC();
     int mmu_idx = check_access_hlsv(env, false, ra);
-    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
+    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
 
     return cpu_ldw_mmu(env, adjust_addr_virt(env, addr), oi, ra);
 }
@@ -642,7 +654,7 @@ target_ulong helper_hyp_hlv_wu(CPURISCVState *env, target_ulong addr)
 {
     uintptr_t ra = GETPC();
     int mmu_idx = check_access_hlsv(env, false, ra);
-    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
+    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
 
     return cpu_ldl_mmu(env, adjust_addr_virt(env, addr), oi, ra);
 }
@@ -651,7 +663,7 @@ target_ulong helper_hyp_hlv_d(CPURISCVState *env, target_ulong addr)
 {
     uintptr_t ra = GETPC();
     int mmu_idx = check_access_hlsv(env, false, ra);
-    MemOpIdx oi = make_memop_idx(MO_TE | MO_UQ, mmu_idx);
+    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UQ, mmu_idx);
 
     return cpu_ldq_mmu(env, adjust_addr_virt(env, addr), oi, ra);
 }
@@ -669,7 +681,7 @@ void helper_hyp_hsv_h(CPURISCVState *env, target_ulong addr, target_ulong val)
 {
     uintptr_t ra = GETPC();
     int mmu_idx = check_access_hlsv(env, false, ra);
-    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
+    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
 
     cpu_stw_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
 }
@@ -678,7 +690,7 @@ void helper_hyp_hsv_w(CPURISCVState *env, target_ulong addr, target_ulong val)
 {
     uintptr_t ra = GETPC();
     int mmu_idx = check_access_hlsv(env, false, ra);
-    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
+    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
 
     cpu_stl_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
 }
@@ -687,7 +699,7 @@ void helper_hyp_hsv_d(CPURISCVState *env, target_ulong addr, target_ulong val)
 {
     uintptr_t ra = GETPC();
     int mmu_idx = check_access_hlsv(env, false, ra);
-    MemOpIdx oi = make_memop_idx(MO_TE | MO_UQ, mmu_idx);
+    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UQ, mmu_idx);
 
     cpu_stq_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
 }
@@ -703,7 +715,7 @@ target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong addr)
 {
     uintptr_t ra = GETPC();
     int mmu_idx = check_access_hlsv(env, true, ra);
-    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
+    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
 
     return cpu_ldw_code_mmu(env, addr, oi, GETPC());
 }
@@ -712,7 +724,7 @@ target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr)
 {
     uintptr_t ra = GETPC();
     int mmu_idx = check_access_hlsv(env, true, ra);
-    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
+    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
 
     return cpu_ldl_code_mmu(env, addr, oi, ra);
 }
-- 
2.51.0


Re: [PATCH 13/13] target/riscv: Introduce mo_endian_env() helper
Posted by Alistair Francis 1 month ago
On Sat, Oct 11, 2025 at 1:56 AM Philippe Mathieu-Daudé
<philmd@linaro.org> wrote:
>
> mo_endian_env() returns the target endianness from CPUArchState.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
>  target/riscv/op_helper.c | 28 ++++++++++++++++++++--------
>  1 file changed, 20 insertions(+), 8 deletions(-)
>
> diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c
> index c486f771d35..9d048089e2a 100644
> --- a/target/riscv/op_helper.c
> +++ b/target/riscv/op_helper.c
> @@ -28,6 +28,18 @@
>  #include "exec/tlb-flags.h"
>  #include "trace.h"
>
> +static inline MemOp mo_endian_env(CPURISCVState *env)
> +{
> +    /*
> +     * A couple of bits in MSTATUS set the endianness:
> +     *  - MSTATUS_UBE (User-mode),
> +     *  - MSTATUS_SBE (Supervisor-mode),
> +     *  - MSTATUS_MBE (Machine-mode)
> +     * but we don't implement that yet.
> +     */
> +    return MO_TE;
> +}
> +
>  /* Exceptions processing helpers */
>  G_NORETURN void riscv_raise_exception(CPURISCVState *env,
>                                        RISCVException exception,
> @@ -633,7 +645,7 @@ target_ulong helper_hyp_hlv_hu(CPURISCVState *env, target_ulong addr)
>  {
>      uintptr_t ra = GETPC();
>      int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
>
>      return cpu_ldw_mmu(env, adjust_addr_virt(env, addr), oi, ra);
>  }
> @@ -642,7 +654,7 @@ target_ulong helper_hyp_hlv_wu(CPURISCVState *env, target_ulong addr)
>  {
>      uintptr_t ra = GETPC();
>      int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
>
>      return cpu_ldl_mmu(env, adjust_addr_virt(env, addr), oi, ra);
>  }
> @@ -651,7 +663,7 @@ target_ulong helper_hyp_hlv_d(CPURISCVState *env, target_ulong addr)
>  {
>      uintptr_t ra = GETPC();
>      int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UQ, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UQ, mmu_idx);
>
>      return cpu_ldq_mmu(env, adjust_addr_virt(env, addr), oi, ra);
>  }
> @@ -669,7 +681,7 @@ void helper_hyp_hsv_h(CPURISCVState *env, target_ulong addr, target_ulong val)
>  {
>      uintptr_t ra = GETPC();
>      int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
>
>      cpu_stw_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
>  }
> @@ -678,7 +690,7 @@ void helper_hyp_hsv_w(CPURISCVState *env, target_ulong addr, target_ulong val)
>  {
>      uintptr_t ra = GETPC();
>      int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
>
>      cpu_stl_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
>  }
> @@ -687,7 +699,7 @@ void helper_hyp_hsv_d(CPURISCVState *env, target_ulong addr, target_ulong val)
>  {
>      uintptr_t ra = GETPC();
>      int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UQ, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UQ, mmu_idx);
>
>      cpu_stq_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
>  }
> @@ -703,7 +715,7 @@ target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong addr)
>  {
>      uintptr_t ra = GETPC();
>      int mmu_idx = check_access_hlsv(env, true, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
>
>      return cpu_ldw_code_mmu(env, addr, oi, GETPC());
>  }
> @@ -712,7 +724,7 @@ target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr)
>  {
>      uintptr_t ra = GETPC();
>      int mmu_idx = check_access_hlsv(env, true, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
>
>      return cpu_ldl_code_mmu(env, addr, oi, ra);
>  }
> --
> 2.51.0
>
>
Re: [PATCH 13/13] target/riscv: Introduce mo_endian_env() helper
Posted by Heinrich Schuchardt 1 month ago
On 10/10/25 17:50, Philippe Mathieu-Daudé wrote:
> mo_endian_env() returns the target endianness from CPUArchState.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>

LGTM

Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

> ---
>   target/riscv/op_helper.c | 28 ++++++++++++++++++++--------
>   1 file changed, 20 insertions(+), 8 deletions(-)
> 
> diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c
> index c486f771d35..9d048089e2a 100644
> --- a/target/riscv/op_helper.c
> +++ b/target/riscv/op_helper.c
> @@ -28,6 +28,18 @@
>   #include "exec/tlb-flags.h"
>   #include "trace.h"
>   
> +static inline MemOp mo_endian_env(CPURISCVState *env)
> +{
> +    /*
> +     * A couple of bits in MSTATUS set the endianness:
> +     *  - MSTATUS_UBE (User-mode),
> +     *  - MSTATUS_SBE (Supervisor-mode),
> +     *  - MSTATUS_MBE (Machine-mode)
> +     * but we don't implement that yet.
> +     */
> +    return MO_TE;
> +}
> +
>   /* Exceptions processing helpers */
>   G_NORETURN void riscv_raise_exception(CPURISCVState *env,
>                                         RISCVException exception,
> @@ -633,7 +645,7 @@ target_ulong helper_hyp_hlv_hu(CPURISCVState *env, target_ulong addr)
>   {
>       uintptr_t ra = GETPC();
>       int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
>   
>       return cpu_ldw_mmu(env, adjust_addr_virt(env, addr), oi, ra);
>   }
> @@ -642,7 +654,7 @@ target_ulong helper_hyp_hlv_wu(CPURISCVState *env, target_ulong addr)
>   {
>       uintptr_t ra = GETPC();
>       int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
>   
>       return cpu_ldl_mmu(env, adjust_addr_virt(env, addr), oi, ra);
>   }
> @@ -651,7 +663,7 @@ target_ulong helper_hyp_hlv_d(CPURISCVState *env, target_ulong addr)
>   {
>       uintptr_t ra = GETPC();
>       int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UQ, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UQ, mmu_idx);
>   
>       return cpu_ldq_mmu(env, adjust_addr_virt(env, addr), oi, ra);
>   }
> @@ -669,7 +681,7 @@ void helper_hyp_hsv_h(CPURISCVState *env, target_ulong addr, target_ulong val)
>   {
>       uintptr_t ra = GETPC();
>       int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
>   
>       cpu_stw_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
>   }
> @@ -678,7 +690,7 @@ void helper_hyp_hsv_w(CPURISCVState *env, target_ulong addr, target_ulong val)
>   {
>       uintptr_t ra = GETPC();
>       int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
>   
>       cpu_stl_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
>   }
> @@ -687,7 +699,7 @@ void helper_hyp_hsv_d(CPURISCVState *env, target_ulong addr, target_ulong val)
>   {
>       uintptr_t ra = GETPC();
>       int mmu_idx = check_access_hlsv(env, false, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UQ, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UQ, mmu_idx);
>   
>       cpu_stq_mmu(env, adjust_addr_virt(env, addr), val, oi, ra);
>   }
> @@ -703,7 +715,7 @@ target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong addr)
>   {
>       uintptr_t ra = GETPC();
>       int mmu_idx = check_access_hlsv(env, true, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UW, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UW, mmu_idx);
>   
>       return cpu_ldw_code_mmu(env, addr, oi, GETPC());
>   }
> @@ -712,7 +724,7 @@ target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr)
>   {
>       uintptr_t ra = GETPC();
>       int mmu_idx = check_access_hlsv(env, true, ra);
> -    MemOpIdx oi = make_memop_idx(MO_TE | MO_UL, mmu_idx);
> +    MemOpIdx oi = make_memop_idx(mo_endian_env(env) | MO_UL, mmu_idx);
>   
>       return cpu_ldl_code_mmu(env, addr, oi, ra);
>   }