Skip to contentSkip to conversation

target/sparc: Fix gdbstub incorrectly handling registers f32-f62

PATCH Applied Mikael Szreder 2 participants 4 messages Feb 14, 2025 – Feb 17, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 target/sparc: Fix gdbstub incorrectly handling registers f32-f62

The gdbstub implementation for the Sparc architecture would incorrectly calculate the the floating point register offset. This resulted in, for example, registers f32 and f34 to point to the same value.

The issue was caused by the confusion between even register numbers and even register indexes. For example, the register index of f32 is 64 and f34 is 65.

Fixes 30038fd81808 ("target-sparc: Change fpr representation to doubles.")
---
 target/sparc/gdbstub.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
target/sparc/gdbstub.c +14 -4
index ec0036e9ef..134617fb23 100644
@@ -79,8 +79,13 @@ int sparc_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
         }
     }
     if (n < 80) {
-        /* f32-f62 (double width, even numbers only) */
-        return gdb_get_reg64(mem_buf, env->fpr[(n - 32) / 2].ll);
+        /* f32-f62 (16 double width registers, even register numbers only)
+         * n == 64: f32 : env->fpr[16]
+         * n == 65: f34 : env->fpr[17]
+         * etc...
+         * n == 79: f62 : env->fpr[31]
+         */
+        return gdb_get_reg64(mem_buf, env->fpr[(n - 64) + 16].ll);
     }
     switch (n) {
     case 80:
@@ -173,8 +178,13 @@ int sparc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
         }
         return 4;
     } else if (n < 80) {
-        /* f32-f62 (double width, even numbers only) */
-        env->fpr[(n - 32) / 2].ll = tmp;
+        /* f32-f62 (16 double width registers, even register numbers only)
+         * n == 64: f32 : env->fpr[16]
+         * n == 65: f34 : env->fpr[17]
+         * etc...
+         * n == 79: f62 : env->fpr[31]
+         */
+        env->fpr[(n - 64) + 16].ll = tmp;
     } else {
         switch (n) {
         case 80:
Signature · 1 line
2.48.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Mikael Szreder

The gdbstub implementation for the Sparc architecture would incorrectly calculate the the floating point register offset. This resulted in, for example, registers f32 and f34 to point to the same value.

The issue was caused by the confusion between even register numbers and even register indexes. For example, the register index of f32 is 64 and f34 is 65.

Quoted 5 lines
Fixes 30038fd81808 ("target-sparc: Change fpr representation to doubles.")
---
  target/sparc/gdbstub.c | 18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)

r~

#2 lore raw
In reply to Mikael Szreder
Quoted 41 lines · Mikael Szreder

The gdbstub implementation for the Sparc architecture would incorrectly calculate the the floating point register offset. This resulted in, for example, registers f32 and f34 to point to the same value.

The issue was caused by the confusion between even register numbers and even register indexes. For example, the register index of f32 is 64 and f34 is 65.

Fixes 30038fd81808 ("target-sparc: Change fpr representation to doubles.")
---
  target/sparc/gdbstub.c | 18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)
target/sparc/gdbstub.c +7 -4
index ec0036e9ef..134617fb23 100644
@@ -79,8 +79,13 @@ int sparc_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
          }
      }
      if (n < 80) {
-        /* f32-f62 (double width, even numbers only) */
-        return gdb_get_reg64(mem_buf, env->fpr[(n - 32) / 2].ll);
+        /* f32-f62 (16 double width registers, even register numbers only)
+         * n == 64: f32 : env->fpr[16]
+         * n == 65: f34 : env->fpr[17]
+         * etc...
+         * n == 79: f62 : env->fpr[31]
+         */
+        return gdb_get_reg64(mem_buf, env->fpr[(n - 64) + 16].ll);
      }
      switch (n) {
      case 80:
@@ -173,8 +178,13 @@ int sparc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
          }
          return 4;
      } else if (n < 80) {
-        /* f32-f62 (double width, even numbers only) */
-        env->fpr[(n - 32) / 2].ll = tmp;

target/sparc/gdbstub.c int sparc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 190

Quoted 10 lines
target/sparc/gdbstub.c +7 -0
+        /* f32-f62 (16 double width registers, even register numbers only)
+         * n == 64: f32 : env->fpr[16]
+         * n == 65: f34 : env->fpr[17]
+         * etc...
+         * n == 79: f62 : env->fpr[31]
+         */
+        env->fpr[(n - 64) + 16].ll = tmp;
      } else {
          switch (n) {
          case 80:

Queued, thanks.

r~

#3 lore raw

This patch should be applicable to the stable releases as well, as the issue has existed since a while back.

Best regards
Mikael Szreder

Quoted 57 lines · Richard Henderson
Quoted 41 lines · Mikael Szreder

The gdbstub implementation for the Sparc architecture would incorrectly calculate the the floating point register offset. This resulted in, for example, registers f32 and f34 to point to the same value.

The issue was caused by the confusion between even register numbers and even register indexes. For example, the register index of f32 is 64 and f34 is 65.

Fixes 30038fd81808 ("target-sparc: Change fpr representation to doubles.")
---
  target/sparc/gdbstub.c | 18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)
target/sparc/gdbstub.c +7 -4
index ec0036e9ef..134617fb23 100644
@@ -79,8 +79,13 @@ int sparc_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
          }
      }
      if (n < 80) {
-        /* f32-f62 (double width, even numbers only) */
-        return gdb_get_reg64(mem_buf, env->fpr[(n - 32) / 2].ll);
+        /* f32-f62 (16 double width registers, even register numbers only)
+         * n == 64: f32 : env->fpr[16]
+         * n == 65: f34 : env->fpr[17]
+         * etc...
+         * n == 79: f62 : env->fpr[31]
+         */
+        return gdb_get_reg64(mem_buf, env->fpr[(n - 64) + 16].ll);
      }
      switch (n) {
      case 80:
@@ -173,8 +178,13 @@ int sparc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
          }
          return 4;
      } else if (n < 80) {
-        /* f32-f62 (double width, even numbers only) */
-        env->fpr[(n - 32) / 2].ll = tmp;

target/sparc/gdbstub.c int sparc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 190

Quoted 10 lines
target/sparc/gdbstub.c +7 -0
+        /* f32-f62 (16 double width registers, even register numbers only)
+         * n == 64: f32 : env->fpr[16]
+         * n == 65: f34 : env->fpr[17]
+         * etc...
+         * n == 79: f62 : env->fpr[31]
+         */
+        env->fpr[(n - 64) + 16].ll = tmp;
      } else {
          switch (n) {
          case 80:

Queued, thanks.

r~