[PATCH v2 27/50] target/riscv: Replace ldtul_p() -> ldn_p()

Philippe Mathieu-Daudé posted 50 patches 1 month ago
Maintainers: Laurent Vivier <laurent@vivier.eu>, Pierrick Bouvier <pierrick.bouvier@linaro.org>, Michael Rolnik <mrolnik@gmail.com>, "Alex Bennée" <alex.bennee@linaro.org>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>, "Dr. David Alan Gilbert" <dave@treblig.org>, Richard Henderson <richard.henderson@linaro.org>, Brian Cain <brian.cain@oss.qualcomm.com>, Paolo Bonzini <pbonzini@redhat.com>, Song Gao <gaosong@loongson.cn>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Aurelien Jarno <aurelien@aurel32.net>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Aleksandar Rikalo <arikalo@gmail.com>, Stafford Horne <shorne@gmail.com>, Nicholas Piggin <npiggin@gmail.com>, Chinmay Rath <rathc@linux.ibm.com>, 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>, Yoshinori Sato <yoshinori.sato@nifty.com>, Ilya Leoshkevich <iii@linux.ibm.com>, David Hildenbrand <david@kernel.org>, Thomas Huth <thuth@redhat.com>, Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>, Artyom Tarasenko <atar4qemu@gmail.com>, Bastian Koppelmann <kbastian@rumtueddeln.de>
[PATCH v2 27/50] target/riscv: Replace ldtul_p() -> ldn_p()
Posted by Philippe Mathieu-Daudé 1 month ago
Prefer ldn_p(riscv_cpu_is_32bit) over target-specific ldtul_p().

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

diff --git a/target/riscv/gdbstub.c b/target/riscv/gdbstub.c
index 1934f919c01..f89b20ea84a 100644
--- a/target/riscv/gdbstub.c
+++ b/target/riscv/gdbstub.c
@@ -191,14 +191,15 @@ static int riscv_gdb_set_csr(CPUState *cs, uint8_t *mem_buf, int n)
 {
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
+    const unsigned regsz = riscv_cpu_is_32bit(cpu) ? 4 : 8;
 
     if (n < CSR_TABLE_SIZE) {
-        target_ulong val = ldtul_p(mem_buf);
+        target_ulong val = ldn_p(mem_buf, regsz);
         int result;
 
         result = riscv_csrrw_debug(env, n, NULL, val, -1);
         if (result == RISCV_EXCP_NONE) {
-            return sizeof(target_ulong);
+            return regsz;
         }
     }
     return 0;
@@ -225,11 +226,12 @@ static int riscv_gdb_get_virtual(CPUState *cs, GByteArray *buf, int n)
 static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
 {
     if (n == 0) {
-#ifndef CONFIG_USER_ONLY
         RISCVCPU *cpu = RISCV_CPU(cs);
+        const unsigned regsz = riscv_cpu_is_32bit(cpu) ? 4 : 8;
+#ifndef CONFIG_USER_ONLY
         CPURISCVState *env = &cpu->env;
 
-        target_ulong new_priv = ldtul_p(mem_buf) & 0x3;
+        target_ulong new_priv = ldn_p(mem_buf, regsz) & 0x3;
         bool new_virt = 0;
 
         if (new_priv == PRV_RESERVED) {
@@ -237,7 +239,7 @@ static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
         }
 
         if (new_priv != PRV_M) {
-            new_virt = (ldtul_p(mem_buf) & BIT(2)) >> 2;
+            new_virt = (ldn_p(mem_buf, regsz) & BIT(2)) >> 2;
         }
 
         if (riscv_has_ext(env, RVH) && new_virt != env->virt_enabled) {
@@ -246,7 +248,7 @@ static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
 
         riscv_cpu_set_mode(env, new_priv, new_virt);
 #endif
-        return sizeof(target_ulong);
+        return regsz;
     }
     return 0;
 }
-- 
2.52.0


Re: [PATCH v2 27/50] target/riscv: Replace ldtul_p() -> ldn_p()
Posted by Chao Liu 4 weeks, 1 day ago
On Thu, Feb 19, 2026 at 08:19:29PM +0100, Philippe Mathieu-Daudé wrote:
> Prefer ldn_p(riscv_cpu_is_32bit) over target-specific ldtul_p().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  target/riscv/gdbstub.c | 14 ++++++++------
>  1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/target/riscv/gdbstub.c b/target/riscv/gdbstub.c
> index 1934f919c01..f89b20ea84a 100644
> --- a/target/riscv/gdbstub.c
> +++ b/target/riscv/gdbstub.c
> @@ -191,14 +191,15 @@ static int riscv_gdb_set_csr(CPUState *cs, uint8_t *mem_buf, int n)
>  {
>      RISCVCPU *cpu = RISCV_CPU(cs);
>      CPURISCVState *env = &cpu->env;
> +    const unsigned regsz = riscv_cpu_is_32bit(cpu) ? 4 : 8;
>  
>      if (n < CSR_TABLE_SIZE) {
> -        target_ulong val = ldtul_p(mem_buf);
> +        target_ulong val = ldn_p(mem_buf, regsz);
>          int result;
>  
>          result = riscv_csrrw_debug(env, n, NULL, val, -1);
>          if (result == RISCV_EXCP_NONE) {
> -            return sizeof(target_ulong);
> +            return regsz;
>          }
>      }
>      return 0;
> @@ -225,11 +226,12 @@ static int riscv_gdb_get_virtual(CPUState *cs, GByteArray *buf, int n)
>  static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
>  {
>      if (n == 0) {
> -#ifndef CONFIG_USER_ONLY
>          RISCVCPU *cpu = RISCV_CPU(cs);
> +        const unsigned regsz = riscv_cpu_is_32bit(cpu) ? 4 : 8;
> +#ifndef CONFIG_USER_ONLY
>          CPURISCVState *env = &cpu->env;
>  
> -        target_ulong new_priv = ldtul_p(mem_buf) & 0x3;
> +        target_ulong new_priv = ldn_p(mem_buf, regsz) & 0x3;
>          bool new_virt = 0;
>  
>          if (new_priv == PRV_RESERVED) {
> @@ -237,7 +239,7 @@ static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
>          }
>  
>          if (new_priv != PRV_M) {
> -            new_virt = (ldtul_p(mem_buf) & BIT(2)) >> 2;
> +            new_virt = (ldn_p(mem_buf, regsz) & BIT(2)) >> 2;
>          }
>  
>          if (riscv_has_ext(env, RVH) && new_virt != env->virt_enabled) {
> @@ -246,7 +248,7 @@ static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
>  
>          riscv_cpu_set_mode(env, new_priv, new_virt);
>  #endif
> -        return sizeof(target_ulong);
> +        return regsz;
>      }
>      return 0;
>  }
> -- 
> 2.52.0
> 
> 

Reviewed-by: Chao Liu <chao.liu.zevorn@gmail.com>

Thanks,
Chao
Re: [PATCH v2 27/50] target/riscv: Replace ldtul_p() -> ldn_p()
Posted by Daniel Henrique Barboza 1 month ago

On 2/19/2026 4:19 PM, Philippe Mathieu-Daudé wrote:
> Prefer ldn_p(riscv_cpu_is_32bit) over target-specific ldtul_p().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---

Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>

>   target/riscv/gdbstub.c | 14 ++++++++------
>   1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/target/riscv/gdbstub.c b/target/riscv/gdbstub.c
> index 1934f919c01..f89b20ea84a 100644
> --- a/target/riscv/gdbstub.c
> +++ b/target/riscv/gdbstub.c
> @@ -191,14 +191,15 @@ static int riscv_gdb_set_csr(CPUState *cs, uint8_t *mem_buf, int n)
>   {
>       RISCVCPU *cpu = RISCV_CPU(cs);
>       CPURISCVState *env = &cpu->env;
> +    const unsigned regsz = riscv_cpu_is_32bit(cpu) ? 4 : 8;
>   
>       if (n < CSR_TABLE_SIZE) {
> -        target_ulong val = ldtul_p(mem_buf);
> +        target_ulong val = ldn_p(mem_buf, regsz);
>           int result;
>   
>           result = riscv_csrrw_debug(env, n, NULL, val, -1);
>           if (result == RISCV_EXCP_NONE) {
> -            return sizeof(target_ulong);
> +            return regsz;
>           }
>       }
>       return 0;
> @@ -225,11 +226,12 @@ static int riscv_gdb_get_virtual(CPUState *cs, GByteArray *buf, int n)
>   static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
>   {
>       if (n == 0) {
> -#ifndef CONFIG_USER_ONLY
>           RISCVCPU *cpu = RISCV_CPU(cs);
> +        const unsigned regsz = riscv_cpu_is_32bit(cpu) ? 4 : 8;
> +#ifndef CONFIG_USER_ONLY
>           CPURISCVState *env = &cpu->env;
>   
> -        target_ulong new_priv = ldtul_p(mem_buf) & 0x3;
> +        target_ulong new_priv = ldn_p(mem_buf, regsz) & 0x3;
>           bool new_virt = 0;
>   
>           if (new_priv == PRV_RESERVED) {
> @@ -237,7 +239,7 @@ static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
>           }
>   
>           if (new_priv != PRV_M) {
> -            new_virt = (ldtul_p(mem_buf) & BIT(2)) >> 2;
> +            new_virt = (ldn_p(mem_buf, regsz) & BIT(2)) >> 2;
>           }
>   
>           if (riscv_has_ext(env, RVH) && new_virt != env->virt_enabled) {
> @@ -246,7 +248,7 @@ static int riscv_gdb_set_virtual(CPUState *cs, uint8_t *mem_buf, int n)
>   
>           riscv_cpu_set_mode(env, new_priv, new_virt);
>   #endif
> -        return sizeof(target_ulong);
> +        return regsz;
>       }
>       return 0;
>   }


Re: [PATCH v2 27/50] target/riscv: Replace ldtul_p() -> ldn_p()
Posted by Pierrick Bouvier 1 month ago
On 2/19/26 11:19 AM, Philippe Mathieu-Daudé wrote:
> Prefer ldn_p(riscv_cpu_is_32bit) over target-specific ldtul_p().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   target/riscv/gdbstub.c | 14 ++++++++------
>   1 file changed, 8 insertions(+), 6 deletions(-)
> 

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>