Skip to contentSkip to conversation

target-ppc: gdbstub allow byte swapping for reading/writing registers

PATCH Superseded Thomas Falcon 2 participants 4 messages Feb 26, 2014 – Feb 28, 2014

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 target-ppc: gdbstub allow byte swapping for reading/writing registers

This patch allows registers to be properly read from and written to when using the gdbstub to debug a ppc guest running in little endian mode. It accomplishes this goal by byte swapping the values of any registers if the MSR:LE value is set.

---
Differences from v3:

Included ppc_cpu_gdb_register_len function suggested by Alexander Graf Moved byteswapping loop into single function: ppc_cpu_gdb_swap_register

---
 target-ppc/cpu-qom.h        |   3 +
 target-ppc/gdbstub.c        | 141 +++++++++++++++++++++++++++++++++++---------
 target-ppc/translate_init.c |   4 +-
 3 files changed, 117 insertions(+), 31 deletions(-)
target-ppc/cpu-qom.h +3 -0
index 72b2232..cf4617e 100644
@@ -107,9 +107,12 @@ void ppc_cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
                         int flags);
 void ppc_cpu_dump_statistics(CPUState *cpu, FILE *f,
                              fprintf_function cpu_fprintf, int flags);
+cpu_gdb_swap_register(uint8_t *buf, int reg);
 hwaddr ppc_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
 int ppc_cpu_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg);
+int ppc_cpu_gdb_read_register_wrap(CPUState *cpu, uint8_t *buf, int reg);
 int ppc_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
+int ppc_cpu_gdb_write_register_wrap(CPUState *cpu, uint8_t *buf, int reg);
 int ppc64_cpu_write_elf64_qemunote(WriteCoreDumpFunction f,
                                    CPUState *cpu, void *opaque);
 int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
target-ppc/gdbstub.c +112 -29

216 lines not on this page · raw message

target-ppc/translate_init.c +2 -2
index 445c360..cf12ba7 100644
@@ -8655,8 +8655,8 @@ static void ppc_cpu_class_init(ObjectClass *oc, void *data)
     cc->dump_state = ppc_cpu_dump_state;
     cc->dump_statistics = ppc_cpu_dump_statistics;
     cc->set_pc = ppc_cpu_set_pc;
-    cc->gdb_read_register = ppc_cpu_gdb_read_register;
-    cc->gdb_write_register = ppc_cpu_gdb_write_register;
+    cc->gdb_read_register = ppc_cpu_gdb_read_register_wrap;
+    cc->gdb_write_register = ppc_cpu_gdb_write_register_wrap;
 #ifndef CONFIG_USER_ONLY
     cc->get_phys_page_debug = ppc_cpu_get_phys_page_debug;
     cc->vmsd = &vmstate_ppc_cpu;
Signature · 1 line
1.8.3.1
#1 lore raw

target-ppc/gdbstub.c line 77

Quoted 11 lines · Thomas Falcon
target-ppc/gdbstub.c +11 -0
+void ppc_cpu_gdb_swap_register(uint8_t *mem_buf, int n)
+{
+    int len = ppc_cpu_gdb_register_len(n);
+    int i = 0;
+    uint8_t tmp;
+    for (i = 0; i < len/2; i++) {
+        tmp = *(mem_buf + i);
+        *(mem_buf+i) = *(mem_buf + len - 1 - i);
+        *(mem_buf + len - 1 - i) = tmp;
+    }
+}

Ew.

The number of register sizes you need to handle is extremely limited. One can easily use bswap32 and bswap64 for this.

r~

#2 lore raw
Quoted 14 lines · Richard Henderson

target-ppc/gdbstub.c line 77

Quoted 11 lines · Thomas Falcon
target-ppc/gdbstub.c +11 -0
+void ppc_cpu_gdb_swap_register(uint8_t *mem_buf, int n)
+{
+    int len = ppc_cpu_gdb_register_len(n);
+    int i = 0;
+    uint8_t tmp;
+    for (i = 0; i < len/2; i++) {
+        tmp = *(mem_buf + i);
+        *(mem_buf+i) = *(mem_buf + len - 1 - i);
+        *(mem_buf + len - 1 - i) = tmp;
+    }
+}

Ew.

Quoted 6 lines

The number of register sizes you need to handle is extremely limited. One can easily use bswap32 and bswap64 for this.

r~

Thanks for your feedback. Did not realize there were built-in bswap functions for pointers. How about this instead?

int len = ppc_cpu_gdb_register_len(n);

if (len==4) {
    bswap32s((uint32_t *)mem_buf);
} else {
    bswap64s((uint64_t *)mem_buf);
}
#3 lore raw
Quoted 8 lines · Thomas Falcon

How about this instead?

int len = ppc_cpu_gdb_register_len(n);

if (len==4) {
    bswap32s((uint32_t *)mem_buf);
} else {
    bswap64s((uint64_t *)mem_buf);
}

Looks much better, thanks.

r~