[PATCH] target/loongarch/gdbstub: Fix gdbstub incorrectly handling register R32

Bibo Mao posted 1 patch 10 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250218032027.4137453-1-maobibo@loongson.cn
Maintainers: Song Gao <gaosong@loongson.cn>
target/loongarch/gdbstub.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
[PATCH] target/loongarch/gdbstub: Fix gdbstub incorrectly handling register R32
Posted by Bibo Mao 10 months ago
Write operation with R32 (orig_a0) and R32 (CSR_BADV) is discarded on
gdbstub implementation for LoongArch system. And return value should
be register size rather than 0, since it is used to calculate offset of
next register such as R33 (PC) in function handle_write_all_regs().

Cc: qemu-stable@nongnu.org
Fixes: ca61e75071c6 ("target/loongarch: Add gdb support.")
Signed-off-by: Bibo Mao <maobibo@loongson.cn>
---
 target/loongarch/gdbstub.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/target/loongarch/gdbstub.c b/target/loongarch/gdbstub.c
index dafa4feb75..471eda28c7 100644
--- a/target/loongarch/gdbstub.c
+++ b/target/loongarch/gdbstub.c
@@ -63,23 +63,24 @@ int loongarch_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 {
     CPULoongArchState *env = cpu_env(cs);
     target_ulong tmp;
-    int read_length;
     int length = 0;
 
+    if (n < 0 || n > 34) {
+        return 0;
+    }
+
     if (is_la64(env)) {
         tmp = ldq_le_p(mem_buf);
-        read_length = 8;
+        length = 8;
     } else {
         tmp = ldl_le_p(mem_buf);
-        read_length = 4;
+        length = 4;
     }
 
     if (0 <= n && n < 32) {
         env->gpr[n] = tmp;
-        length = read_length;
     } else if (n == 33) {
         set_pc(env, tmp);
-        length = read_length;
     }
     return length;
 }

base-commit: db7aa99ef894e88fc5eedf02ca2579b8c344b2ec
-- 
2.39.3
Re: [PATCH] target/loongarch/gdbstub: Fix gdbstub incorrectly handling register R32
Posted by bibo mao 10 months ago
Queued in loongarch-next with title and changelog changed as follows:

target/loongarch/gdbstub: Fix gdbstub incorrectly handling some registers
   Write operation with R32 (orig_a0) and R34 (CSR_BADV) is discarded on

Regards
Bibo Mao

On 2025/2/18 上午11:20, Bibo Mao wrote:
> Write operation with R32 (orig_a0) and R32 (CSR_BADV) is discarded on
> gdbstub implementation for LoongArch system. And return value should
> be register size rather than 0, since it is used to calculate offset of
> next register such as R33 (PC) in function handle_write_all_regs().
> 
> Cc: qemu-stable@nongnu.org
> Fixes: ca61e75071c6 ("target/loongarch: Add gdb support.")
> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
> ---
>   target/loongarch/gdbstub.c | 11 ++++++-----
>   1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/target/loongarch/gdbstub.c b/target/loongarch/gdbstub.c
> index dafa4feb75..471eda28c7 100644
> --- a/target/loongarch/gdbstub.c
> +++ b/target/loongarch/gdbstub.c
> @@ -63,23 +63,24 @@ int loongarch_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
>   {
>       CPULoongArchState *env = cpu_env(cs);
>       target_ulong tmp;
> -    int read_length;
>       int length = 0;
>   
> +    if (n < 0 || n > 34) {
> +        return 0;
> +    }
> +
>       if (is_la64(env)) {
>           tmp = ldq_le_p(mem_buf);
> -        read_length = 8;
> +        length = 8;
>       } else {
>           tmp = ldl_le_p(mem_buf);
> -        read_length = 4;
> +        length = 4;
>       }
>   
>       if (0 <= n && n < 32) {
>           env->gpr[n] = tmp;
> -        length = read_length;
>       } else if (n == 33) {
>           set_pc(env, tmp);
> -        length = read_length;
>       }
>       return length;
>   }
> 
> base-commit: db7aa99ef894e88fc5eedf02ca2579b8c344b2ec
>