Skip to contentSkip to conversation

qemu-system-arm: cortex-m gdb registers

RFC In review Christopher Friedt 3 participants 12 messages Dec 14, 2015 – Dec 16, 2015

lore
Share & tools
lore
#0 lore raw

At least for Cortex-M3 devices (but also M0, M0+, M4, ...), while JTAG debugging using OpenOCD's built-in GDB server, the general purpose register layout (i.e. `info reg' in GDB) should contain slightly more than the usual ARM core registers.

The non-addressable core registers that appear in OpenOCD's listing are:

r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, sp (r13), lr (r14), pc (r15), xpsr, primask, basepri, faultmask, and control.

These registers are well documented in the ARMv7M Architecture Reference Manual.

This change addes preliminary support for those registers via a custom qXfer:features:read+ and XML response that follows the GNU convention documented here [1] for org.gnu.gdb.arm.m-profile and org.gnu.gdb.arm.m-system.

[1] https://goo.gl/NMxlC5

The default behaviour for non-cortex-m will be to mimic the original ARM behaviour of referring the GDB client to an <xi:include href="arm-core.xml">.

A simple test environment for this patch is to compile and link the following assembly fragment:

		.syntax unified
		.cpu cortex-m3

		.section .interp
		.word 0x20020000
		.word 0x9

		.text

		.global _start
		.thumb
		.thumb_func
		.type _start, %function
	_start:
		b _start
		.size _start, .-_start

Using the command below:

arm-none-eabi-gcc -g -O0 -mthumb -march=armv7-m -Wl,-Ttext-segment,0 \
-static -nostartfiles -o foo foo.S

Launch the resulting binary with qemu:

qemu-system-arm -S -s -M netduino2 -nographic -kernel foo

Run gdb, attaching to the qemu's GDB server:

    arm-none-eabi-gdb foo
    (gdb) target remote localhost:1234
    Remote debugging using localhost:1234
    0x00000000 in _start ()
    (gdb) info all-registers
    r0             0x0	0
    r1             0x0	0
    r2             0x0	0
    r3             0x0	0
    r4             0x0	0
    r5             0x0	0
    r6             0x0	0
    r7             0x0	0
    r8             0x0	0
    r9             0x0	0
    r10            0x0	0
    r11            0x0	0
    r12            0x0	0
    sp             0xbffef7fc	0xbffef7fc
    lr             0x0	0
    pc             0x0	0x0 <_start>
    xpsr           0x40000000	1073741824
    msp            0xbffef7fc	0xbffef7fc
    psp            0x0	0x0 <_start>
    primask        0x0	0
    basepri        0x0	0
    faultmask      0x1	1
    control        0x0	0

The changes have been tested with other firmware images and also via Eclipse Mars.

Christopher Friedt (1):
qemu-system-arm: cortex-m gdb registers

 gdbstub.c            |  29 ++++---
 include/qom/cpu.h    |   1 +
 target-arm/cpu-qom.h |   4 +
 target-arm/cpu.c     |   5 +-
 target-arm/gdbstub.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 241 insertions(+), 13 deletions(-)
Signature · 1 line
2.5.4 (Apple Git-61)
#1 lore raw

RFC 1/1 qemu-system-arm: cortex-m gdb registers

* allow overriding the default xml descriptor with gdb_xml_descriptor()
* read cortex-m registers using arm_cortexm_gdb_read_register()
* write cortex-m registers using arm_cortexm_gdb_write_register()
* correct the number of cortex-m core regs to 23

---
 gdbstub.c            |  29 ++++---
 include/qom/cpu.h    |   1 +
 target-arm/cpu-qom.h |   4 +
 target-arm/cpu.c     |   5 +-
 target-arm/gdbstub.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 241 insertions(+), 13 deletions(-)
gdbstub.c +17 -12
index 9c29aa0..4684a4b 100644
@@ -540,19 +540,24 @@ static const char *get_feature_xml(const char *p, const char **newp,
             GDBRegisterState *r;
             CPUState *cpu = first_cpu;
 
-            snprintf(target_xml, sizeof(target_xml),
-                     "<?xml version=\"1.0\"?>"
-                     "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
-                     "<target>"
-                     "<xi:include href=\"%s\"/>",
-                     cc->gdb_core_xml_file);
-
-            for (r = cpu->gdb_regs; r; r = r->next) {
-                pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
-                pstrcat(target_xml, sizeof(target_xml), r->xml);
-                pstrcat(target_xml, sizeof(target_xml), "\"/>");
+            if (cc->gdb_xml_descriptor) {
+                cc->gdb_xml_descriptor(cpu, target_xml, sizeof(target_xml));
+            } else {
+                snprintf(target_xml, sizeof(target_xml),
+                         "<?xml version=\"1.0\"?>"
+                         "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
+                         "<target>"
+                         "<xi:include href=\"%s\"/>",
+                         cc->gdb_core_xml_file);
+
+                for (r = cpu->gdb_regs; r; r = r->next) {
+                    pstrcat(target_xml, sizeof(target_xml),
+                        "<xi:include href=\"");
+                    pstrcat(target_xml, sizeof(target_xml), r->xml);
+                    pstrcat(target_xml, sizeof(target_xml), "\"/>");
+                }
+                pstrcat(target_xml, sizeof(target_xml), "</target>");
             }
-            pstrcat(target_xml, sizeof(target_xml), "</target>");
         }
         return target_xml;
     }
include/qom/cpu.h +1 -0
index 51a1323..7b8d875 100644
@@ -152,6 +152,7 @@ typedef struct CPUClass {
     int (*handle_mmu_fault)(CPUState *cpu, vaddr address, int rw,
                             int mmu_index);
     hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
+    int (*gdb_xml_descriptor)(CPUState *cpu, char *buf, size_t len);
     int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg);
     int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
     void (*debug_excp_handler)(CPUState *cpu);
target-arm/cpu-qom.h +4 -0
index 25fb1ce..eafae6b 100644
@@ -221,6 +221,10 @@ hwaddr arm_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
 int arm_cpu_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg);
 int arm_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
 
+int arm_cortexm_gdb_xml_descriptor(CPUState *cpu, char *buf, size_t len);
+int arm_cortexm_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg);
+int arm_cortexm_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
+
 /* Callback functions for the generic timer's timers. */
 void arm_gt_ptimer_cb(void *opaque);
 void arm_gt_vtimer_cb(void *opaque);
target-arm/cpu.c +4 -1
index 30739fc..e56b77a 100644
@@ -553,7 +553,6 @@ static void arm_cpu_post_init(Object *obj)
                                      &error_abort);
         }
     }
-
 }
 
 static void arm_cpu_finalizefn(Object *obj)
@@ -910,6 +909,10 @@ static void arm_v7m_class_init(ObjectClass *oc, void *data)
 #endif
 
     cc->cpu_exec_interrupt = arm_v7m_cpu_exec_interrupt;
+    cc->gdb_num_core_regs = 23;
+    cc->gdb_xml_descriptor = arm_cortexm_gdb_xml_descriptor;
+    cc->gdb_read_register = arm_cortexm_gdb_read_register;
+    cc->gdb_write_register = arm_cortexm_gdb_write_register;
 }
 
 static const ARMCPRegInfo cortexr5_cp_reginfo[] = {
target-arm/gdbstub.c +215 -0

224 lines not on this page · raw message

Signature · 1 line
2.5.4 (Apple Git-61)
#2 lore raw
Quoted 40 lines · Christopher Friedt

* allow overriding the default xml descriptor with gdb_xml_descriptor()
* read cortex-m registers using arm_cortexm_gdb_read_register()
* write cortex-m registers using arm_cortexm_gdb_write_register()
* correct the number of cortex-m core regs to 23

---
 gdbstub.c            |  29 ++++---
 include/qom/cpu.h    |   1 +
 target-arm/cpu-qom.h |   4 +
 target-arm/cpu.c     |   5 +-
 target-arm/gdbstub.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 241 insertions(+), 13 deletions(-)
gdbstub.c +7 -11
index 9c29aa0..4684a4b 100644
@@ -540,19 +540,24 @@ static const char *get_feature_xml(const char *p, const char **newp,
             GDBRegisterState *r;
             CPUState *cpu = first_cpu;
 
-            snprintf(target_xml, sizeof(target_xml),
-                     "<?xml version=\"1.0\"?>"
-                     "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
-                     "<target>"
-                     "<xi:include href=\"%s\"/>",
-                     cc->gdb_core_xml_file);
-
-            for (r = cpu->gdb_regs; r; r = r->next) {
-                pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
-                pstrcat(target_xml, sizeof(target_xml), r->xml);
-                pstrcat(target_xml, sizeof(target_xml), "\"/>");
+            if (cc->gdb_xml_descriptor) {
+                cc->gdb_xml_descriptor(cpu, target_xml, sizeof(target_xml));
+            } else {
+                snprintf(target_xml, sizeof(target_xml),
+                         "<?xml version=\"1.0\"?>"
+                         "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
+                         "<target>"

gdbstub.c static const char *get_feature_xml(const char *p, const char **newp, · line 559

Quoted 10 lines
gdbstub.c +10 -0
+                         "<xi:include href=\"%s\"/>",
+                         cc->gdb_core_xml_file);
+
+                for (r = cpu->gdb_regs; r; r = r->next) {
+                    pstrcat(target_xml, sizeof(target_xml),
+                        "<xi:include href=\"");
+                    pstrcat(target_xml, sizeof(target_xml), r->xml);
+                    pstrcat(target_xml, sizeof(target_xml), "\"/>");
+                }
+                pstrcat(target_xml, sizeof(target_xml), "</target>");

This patch seems to be creating a completely new method of constructing the XML to send to gdb. Is it really not possible to do this using the existing mechanisms we have for selecting XML to send to gdb and handling the registers it implies?

thanks
-- PMM

#3 lore raw
Quoted 4 lines · Peter Maydell

This patch seems to be creating a completely new method of constructing the XML to send to gdb. Is it really not possible to do this using the existing mechanisms we have for selecting XML to send to gdb and handling the registers it implies?

Hehehe... I only just saw the gdb-xml/ directory. I'll create a gdb-xml/arm-cortex-m-core.xml then, and refactor some of the remaining lines of code.

Thanks for the feedback ;-)

#4 lore raw
Quoted 9 lines · Christopher Friedt
Quoted 4 lines · Peter Maydell

This patch seems to be creating a completely new method of constructing the XML to send to gdb. Is it really not possible to do this using the existing mechanisms we have for selecting XML to send to gdb and handling the registers it implies?

Hehehe... I only just saw the gdb-xml/ directory. I'll create a gdb-xml/arm-cortex-m-core.xml then, and refactor some of the remaining lines of code.

Note that our XML files are from gdb itself, so you should start by checking whether gdb has a suitable Cortex-M xml file.

thanks
-- PMM

#5 lore raw
Quoted 2 lines · Peter Maydell

Note that our XML files are from gdb itself, so you should start by checking whether gdb has a suitable Cortex-M xml file.

They do indeed. Thanks for the tip.

#6 lore raw
Quoted 5 lines · Christopher Friedt
Quoted 2 lines · Peter Maydell

Note that our XML files are from gdb itself, so you should start by checking whether gdb has a suitable Cortex-M xml file.

They do indeed. Thanks for the tip.

binutils-gdb arm-m-profile.xml: https://goo.gl/hpTye8
openocd armv7m.c: http://goo.gl/FFn56X

There are 2 (major) differences from what I've seen:

1) xpsr is regnum 25 instead of 16 (what OpenOCD uses), and I'm fine with that.
2) binutils-gdb does not specify anything for the org.gnu.gdb.arm.m-system group of core registers in any xml file.

It also seems very clear that the binutils people and the openocd people have diverged at some point in their assignment of regnum values; in openocd, the registers are mostly all consecutive with moderate reuse between cores, whereas in binutils-gdb, their are occasional gaps and extensive reuse between cores. The differences seem primarily technical, but it's unclear as to why binutils-gdb does *not* include the m-system group of core registers.

The m-system group of core registers are *incredibly* useful, but I'm also inclined not to clobber binutils-gdb's register numbering convention.

I think it would be most ideal to append the crucial m-system information directly [1] in arm-m-profile.xml from binutils-gdb (or possibly declare it as an include [2]):

<feature name="org.gnu.gdb.arm.m-system">
<reg name="msp" bitsize="32" type="data_ptr"/>
<reg name="psp" bitsize="32" type="data_ptr"/>
<reg name="primask" bitsize="1" type="int8"/>
<reg name="basepri" bitsize="8" type="int8"/>
<reg name="faultmask" bitsize="1" type="int8"/>
<reg name="control" bitsize="3" type="int8"/>
</feature>

However, if the worry there is that it diverges from binutils-gdb, then the next best solution would be to create a separate arm-m-system.xml, and to append that to the cpu->gdb_reg linked list in cortex_m3_initfn(), cortex_m4_initfn(), and any other m's [3].

Which solution would work best for qemu?

C

#7 lore raw
Quoted 17 lines · Christopher Friedt
Quoted 5 lines · Christopher Friedt
Quoted 2 lines · Peter Maydell

Note that our XML files are from gdb itself, so you should start by checking whether gdb has a suitable Cortex-M xml file.

They do indeed. Thanks for the tip.

binutils-gdb arm-m-profile.xml: https://goo.gl/hpTye8
openocd armv7m.c: http://goo.gl/FFn56X

There are 2 (major) differences from what I've seen:

1) xpsr is regnum 25 instead of 16 (what OpenOCD uses), and I'm fine with that.
2) binutils-gdb does not specify anything for the org.gnu.gdb.arm.m-system group of core registers in any xml file.

Quoted 7 lines

It also seems very clear that the binutils people and the openocd people have diverged at some point in their assignment of regnum values; in openocd, the registers are mostly all consecutive with moderate reuse between cores, whereas in binutils-gdb, their are occasional gaps and extensive reuse between cores. The differences seem primarily technical, but it's unclear as to why binutils-gdb does *not* include the m-system group of core registers.

My guess would be because gdb is primarily thinking of itself as a user-mode debugger, and system registers aren't accessible from there. And/or "nobody asked for it".

Quoted 17 lines

The m-system group of core registers are *incredibly* useful, but I'm also inclined not to clobber binutils-gdb's register numbering convention.

I think it would be most ideal to append the crucial m-system information directly [1] in arm-m-profile.xml from binutils-gdb (or possibly declare it as an include [2]):

<feature name="org.gnu.gdb.arm.m-system">
<reg name="msp" bitsize="32" type="data_ptr"/>
<reg name="psp" bitsize="32" type="data_ptr"/>
<reg name="primask" bitsize="1" type="int8"/>
<reg name="basepri" bitsize="8" type="int8"/>
<reg name="faultmask" bitsize="1" type="int8"/>
<reg name="control" bitsize="3" type="int8"/>
</feature>

Quoted 6 lines

However, if the worry there is that it diverges from binutils-gdb, then the next best solution would be to create a separate arm-m-system.xml, and to append that to the cpu->gdb_reg linked list in cortex_m3_initfn(), cortex_m4_initfn(), and any other m's [3].

Which solution would work best for qemu?

I'd rather we didn't diverge from upstream gdb too. On the other hand I'm not sure how much it matters if we all end up using different XML to describe the same target hardware. It would be nice to ask the gdb folks first though, maybe.

rth: do you know how this stuff works?

thanks
-- PMM

#8 lore raw
Quoted 52 lines · Peter Maydell
Quoted 17 lines · Christopher Friedt
Quoted 5 lines · Christopher Friedt
Quoted 2 lines · Peter Maydell

Note that our XML files are from gdb itself, so you should start by checking whether gdb has a suitable Cortex-M xml file.

They do indeed. Thanks for the tip.

binutils-gdb arm-m-profile.xml: https://goo.gl/hpTye8
openocd armv7m.c: http://goo.gl/FFn56X

There are 2 (major) differences from what I've seen:

1) xpsr is regnum 25 instead of 16 (what OpenOCD uses), and I'm fine with that.
2) binutils-gdb does not specify anything for the org.gnu.gdb.arm.m-system group of core registers in any xml file.

Quoted 7 lines

It also seems very clear that the binutils people and the openocd people have diverged at some point in their assignment of regnum values; in openocd, the registers are mostly all consecutive with moderate reuse between cores, whereas in binutils-gdb, their are occasional gaps and extensive reuse between cores. The differences seem primarily technical, but it's unclear as to why binutils-gdb does *not* include the m-system group of core registers.

My guess would be because gdb is primarily thinking of itself as a user-mode debugger, and system registers aren't accessible from there. And/or "nobody asked for it".

Quoted 22 lines

The m-system group of core registers are *incredibly* useful, but I'm also inclined not to clobber binutils-gdb's register numbering convention.

I think it would be most ideal to append the crucial m-system information directly [1] in arm-m-profile.xml from binutils-gdb (or possibly declare it as an include [2]):

<feature name="org.gnu.gdb.arm.m-system">
<reg name="msp" bitsize="32" type="data_ptr"/>
<reg name="psp" bitsize="32" type="data_ptr"/>
<reg name="primask" bitsize="1" type="int8"/>
<reg name="basepri" bitsize="8" type="int8"/>
<reg name="faultmask" bitsize="1" type="int8"/>
<reg name="control" bitsize="3" type="int8"/>
</feature>

However, if the worry there is that it diverges from binutils-gdb, then the next best solution would be to create a separate arm-m-system.xml, and to append that to the cpu->gdb_reg linked list in cortex_m3_initfn(), cortex_m4_initfn(), and any other m's [3].

Quoted 8 lines
Quoted 1 line

Which solution would work best for qemu?

I'd rather we didn't diverge from upstream gdb too. On the other hand I'm not sure how much it matters if we all end up using different XML to describe the same target hardware. It would be nice to ask the gdb folks first though, maybe.

rth: do you know how this stuff works?

IIRC last time I played with this when adding aarch64 system registers for debugging is the number is irrelevant to gdb, its all dependant on what the stub sends. As long as the coprocessor get/set functions agree on the order with the xml everything should be fine.

Quoted 3 lines

thanks
-- PMM

--
Alex Bennée

#9 lore raw
Quoted 4 lines · Alex Bennée

IIRC last time I played with this when adding aarch64 system registers for debugging is the number is irrelevant to gdb, its all dependant on what the stub sends. As long as the coprocessor get/set functions agree on the order with the xml everything should be fine.

That's right - the question isn't really how get the two to communicate, but really which registers numbers to choose and how to choose them in such a way that it doesn't make anyone angry :-)

And Peter is likely right that we should ask the gdb folks input - Peter, do you have someone particular in mind?

#11 lore raw
Quoted 5 lines · Christopher Friedt

Just to update everyone, there is a thread on gdb-patches here [1] that is awaiting consensus before a patch is submitted that we may pull into qemu.

[1] https://goo.gl/FyUvQu

Thanks for following up with the GDB developers.

-- PMM