Skip to contentSkip to conversation

ARM: Add support for V8M special registers in GDB stub

PATCH In review ~dreiss-meta 2 participants 5 messages Feb 7, 2023 – Feb 14, 2023

lore
Share & tools
lore
#0 lore raw

v4 cover ARM: Add support for V8M special registers in GDB stub

New in v4: Moved arm_v7m_mrs_control out of the #ifdef CONFIG_USER_ONLY block, unbreaking the user-only build. The downside is that this function is now taking up space in the user-only binary, but it can (presumably?) never be used because there are no user modes for v8m cores. Let me know if you'd prefer for me to wrap #ifdef CONFIG_USER_ONLY around the v8m registers in the gdb stub. Also, let me know if you'd prefer a separate commit for renaming and moving v7m_mrs_control.

David Reiss (2):
target/arm/gdbstub: Support reading M system registers from GDB target/arm/gdbstub: Support reading M security extension registers
from GDB

 target/arm/cpu.h      |  25 ++++-
 target/arm/gdbstub.c  | 241 ++++++++++++++++++++++++++++++++++++++++++
 target/arm/m_helper.c |  87 +++++++--------
 3 files changed, 305 insertions(+), 48 deletions(-)
Signature · 1 line
2.34.5
#1 lore raw

PATCH v4 1/2 target/arm/gdbstub: Support reading M system registers from GDB

From: David Reiss <[email protected]>

Follows a fairly similar pattern to the existing special register debug support. Only reading is implemented, but it should be possible to implement writes.

v7m_mrs_control was renamed arm_v7m_mrs_control and made non-static so this logic could be shared between the MRS instruction and the GDB stub.

---
 target/arm/cpu.h      |  12 +++-
 target/arm/gdbstub.c  | 125 ++++++++++++++++++++++++++++++++++++++++++
 target/arm/m_helper.c |   6 +-
 3 files changed, 138 insertions(+), 5 deletions(-)
target/arm/cpu.h +10 -2
index 7bc97fece9..2ba64811a0 100644
@@ -867,6 +867,7 @@ struct ArchCPU {
 
     DynamicGDBXMLInfo dyn_sysreg_xml;
     DynamicGDBXMLInfo dyn_svereg_xml;
+    DynamicGDBXMLInfo dyn_m_systemreg_xml;
 
     /* Timers used by the generic (architected) timer */
     QEMUTimer *gt_timer[NUM_GTIMERS];
@@ -1111,11 +1112,13 @@ int arm_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
 int arm_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
 
 /*
- * Helpers to dynamically generates XML descriptions of the sysregs
- * and SVE registers. Returns the number of registers in each set.
+ * Helpers to dynamically generate XML descriptions of the sysregs,
+ * SVE registers, and M-profile system registers.
+ * Returns the number of registers in each set.
  */
 int arm_gen_dynamic_sysreg_xml(CPUState *cpu, int base_reg);
 int arm_gen_dynamic_svereg_xml(CPUState *cpu, int base_reg);
+int arm_gen_dynamic_m_systemreg_xml(CPUState *cpu, int base_reg);
 
 /* Returns the dynamically generated XML for the gdb stub.
  * Returns a pointer to the XML contents for the specified XML file or NULL
@@ -1507,6 +1510,11 @@ FIELD(SVCR, ZA, 1, 1)
 FIELD(SMCR, LEN, 0, 4)
 FIELD(SMCR, FA64, 31, 1)
 
+/*
+ * Read the CONTROL register as the MRS instruction would.
+ */
+uint32_t arm_v7m_mrs_control(CPUARMState *env, uint32_t secure);
+
 /* Write a new value to v7m.exception, thus transitioning into or out
  * of Handler mode; this may result in a change of active stack pointer.
  */
target/arm/gdbstub.c +125 -0

143 lines not on this page · raw message

target/arm/m_helper.c +3 -3
index e7e746ea18..c20bcac977 100644
@@ -53,7 +53,7 @@ static uint32_t v7m_mrs_xpsr(CPUARMState *env, uint32_t reg, unsigned el)
     return xpsr_read(env) & mask;
 }
 
-static uint32_t v7m_mrs_control(CPUARMState *env, uint32_t secure)
+uint32_t arm_v7m_mrs_control(CPUARMState *env, uint32_t secure)
 {
     uint32_t value = env->v7m.control[secure];
 
@@ -90,7 +90,7 @@ uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
     case 0 ... 7: /* xPSR sub-fields */
         return v7m_mrs_xpsr(env, reg, 0);
     case 20: /* CONTROL */
-        return v7m_mrs_control(env, 0);
+        return arm_v7m_mrs_control(env, 0);
     default:
         /* Unprivileged reads others as zero.  */
         return 0;
@@ -2420,7 +2420,7 @@ uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
     case 0 ... 7: /* xPSR sub-fields */
         return v7m_mrs_xpsr(env, reg, el);
     case 20: /* CONTROL */
-        return v7m_mrs_control(env, env->v7m.secure);
+        return arm_v7m_mrs_control(env, env->v7m.secure);
     case 0x94: /* CONTROL_NS */
         /*
          * We have to handle this here because unprivileged Secure code
Signature · 1 line
2.34.5
#2 lore raw

PATCH v4 2/2 target/arm/gdbstub: Support reading M security extension registers from GDB

In reply to ~dreiss-meta

From: David Reiss <[email protected]>

Follows a fairly similar pattern to the existing special register debug support. Only reading is implemented, but it should be possible to implement writes.

---
 target/arm/cpu.h      |  15 +++++-
 target/arm/gdbstub.c  | 116 ++++++++++++++++++++++++++++++++++++++++++
 target/arm/m_helper.c |  81 ++++++++++++++---------------
 3 files changed, 168 insertions(+), 44 deletions(-)
target/arm/cpu.h +14 -1
index 2ba64811a0..788eb31286 100644
@@ -868,6 +868,7 @@ struct ArchCPU {
     DynamicGDBXMLInfo dyn_sysreg_xml;
     DynamicGDBXMLInfo dyn_svereg_xml;
     DynamicGDBXMLInfo dyn_m_systemreg_xml;
+    DynamicGDBXMLInfo dyn_m_securereg_xml;
 
     /* Timers used by the generic (architected) timer */
     QEMUTimer *gt_timer[NUM_GTIMERS];
@@ -1113,12 +1114,13 @@ int arm_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
 
 /*
  * Helpers to dynamically generate XML descriptions of the sysregs,
- * SVE registers, and M-profile system registers.
+ * SVE registers, M-profile system, and M-profile secure extension registers.
  * Returns the number of registers in each set.
  */
 int arm_gen_dynamic_sysreg_xml(CPUState *cpu, int base_reg);
 int arm_gen_dynamic_svereg_xml(CPUState *cpu, int base_reg);
 int arm_gen_dynamic_m_systemreg_xml(CPUState *cpu, int base_reg);
+int arm_gen_dynamic_m_securereg_xml(CPUState *cpu, int base_reg);
 
 /* Returns the dynamically generated XML for the gdb stub.
  * Returns a pointer to the XML contents for the specified XML file or NULL
@@ -1618,6 +1620,17 @@ static inline void xpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
 #endif
 }
 
+/*
+ * Return a pointer to the location where we currently store the
+ * stack pointer for the requested security state and thread mode.
+ * This pointer will become invalid if the CPU state is updated
+ * such that the stack pointers are switched around (eg changing
+ * the SPSEL control bit).
+ */
+uint32_t *arm_v7m_get_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
+                             bool spsel);
+
+
 #define HCR_VM        (1ULL << 0)
 #define HCR_SWIO      (1ULL << 1)
 #define HCR_PTW       (1ULL << 2)
target/arm/gdbstub.c +116 -0

134 lines not on this page · raw message

target/arm/m_helper.c +38 -43

111 lines not on this page · raw message

Signature · 1 line
2.34.5
#3 lore raw
In reply to ~dreiss-meta
Quoted 9 lines · ~dreiss-meta

New in v4: Moved arm_v7m_mrs_control out of the #ifdef CONFIG_USER_ONLY block, unbreaking the user-only build. The downside is that this function is now taking up space in the user-only binary, but it can (presumably?) never be used because there are no user modes for v8m cores. Let me know if you'd prefer for me to wrap #ifdef CONFIG_USER_ONLY around the v8m registers in the gdb stub. Also, let me know if you'd prefer a separate commit for renaming and moving v7m_mrs_control.

We do support the M-profile cores in the linux-user executables, so this code is "live". The view that the guest program and the debugger should see is that the core is always in user mode (aka unprivileged, Thread mode).

thanks
-- PMM

#4 lore raw
Quoted 12 lines · Peter Maydell
Quoted 9 lines · ~dreiss-meta

New in v4: Moved arm_v7m_mrs_control out of the #ifdef CONFIG_USER_ONLY block, unbreaking the user-only build. The downside is that this function is now taking up space in the user-only binary, but it can (presumably?) never be used because there are no user modes for v8m cores. Let me know if you'd prefer for me to wrap #ifdef CONFIG_USER_ONLY around the v8m registers in the gdb stub. Also, let me know if you'd prefer a separate commit for renaming and moving v7m_mrs_control.

Quoted 4 lines

We do support the M-profile cores in the linux-user executables, so this code is "live". The view that the guest program and the debugger should see is that the core is always in user mode (aka unprivileged, Thread mode).

Having looked more closely at the registers in the org.gnu.gdb.arm.secext set, I think the right answer here is that for CONFIG_USER_ONLY we should never expose the second org.gnu.gdb.arm.secext block. Because the guest is restricted to non-secure mode only, the registers in the org.gnu.gdb.arm.m-system block are sufficient to get all the interesting information, and the debugger shouldn't be confused by only seeing one of the two, because that's what it sees when talking to a CPU which doesn't have the security extension. The register state in the other block is OK.

(I don't think the code as it stands will be broken as such, but it will let the debugger read a lot of confusing 0s, and it leaves the door open for us forgetting to prevent writes if we add system register write suport later, which would let gdb put QEMU into states it doesn't expect to be in.)

That change is simple (couple of ifdefs in gdbstub.c, and then it lets us avoid moving the definition of the arm_v7m_get_sp_ptr() function). But Richard has just told me he has a rework of some of the gdbstub handling which he incorporated this patchset into, so I think the best thing is if he can post that series.

thanks
-- PMM