Skip to contentSkip to conversation

Add a dynamic XML-description of the cp-registers to GDB

PATCH Superseded Abdallah Bouassida 2 participants 8 messages Mar 12, 2018 – Mar 13, 2018

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add a dynamic XML-description of the cp-registers to GDB

The previous version:
http://patchwork.ozlabs.org/project/qemu-devel/list/?series=30973

Abdallah Bouassida (4):
target/arm: Add "ARM_CP_NO_GDB" as a new bit field for ARMCPRegInfo
type
target/arm: Add "_S" suffix to the secure version of a sysreg target/arm: Add the XML dynamic generation
target/arm: Add arm_gdb_set_sysreg() callback

 gdbstub.c            | 10 ++++++++
 include/qom/cpu.h    |  5 +++-
 target/arm/cpu.c     |  1 +
 target/arm/cpu.h     | 20 ++++++++++++++-
 target/arm/gdbstub.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++
 target/arm/helper.c  | 72 +++++++++++++++++++++++++++++++++++++++++++---------
 6 files changed, 165 insertions(+), 14 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v4 1/4 target/arm: Add "ARM_CP_NO_GDB" as a new bit field for ARMCPRegInfo type

This is a preparation for the coming feature of creating dynamically an XML description for the ARM sysregs.
A register has ARM_CP_NO_GDB enabled will not be shown in the dynamic XML. This bit is enabled automatically when creating CP_ANY wildcard aliases. This bit could be enabled manually for any register we want to remove from the dynamic XML description.

---
 target/arm/cpu.h    | 3 ++-
 target/arm/helper.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
target/arm/cpu.h +2 -1
index 2b97408..818a98a 100644
@@ -1796,10 +1796,11 @@ static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)
 #define ARM_LAST_SPECIAL         ARM_CP_DC_ZVA
 #define ARM_CP_FPU               0x1000
 #define ARM_CP_SVE               0x2000
+#define ARM_CP_NO_GDB            0x4000
 /* Used only as a terminator for ARMCPRegInfo lists */
 #define ARM_CP_SENTINEL          0xffff
 /* Mask of only the flag bits in a type field */
-#define ARM_CP_FLAG_MASK         0x30ff
+#define ARM_CP_FLAG_MASK         0x70ff
 
 /* Valid values for ARMCPRegInfo state field, indicating which of
  * the AArch32 and AArch64 execution states this register is visible in.
target/arm/helper.c +1 -1
index c82f63d..c5d62c8 100644
@@ -5663,7 +5663,7 @@ static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
     if (((r->crm == CP_ANY) && crm != 0) ||
         ((r->opc1 == CP_ANY) && opc1 != 0) ||
         ((r->opc2 == CP_ANY) && opc2 != 0)) {
-        r2->type |= ARM_CP_ALIAS;
+        r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
     }
 
     /* Check that raw accesses are either forbidden or handled. Note that
Signature · 1 line
2.7.4
#2 lore raw

PATCH v4 2/4 target/arm: Add "_S" suffix to the secure version of a sysreg

In reply to Abdallah Bouassida

This is a preparation for the coming feature of creating dynamically an XML description for the ARM sysregs.
Add "_S" suffix to the secure version of sysregs that have both S and NS views Replace (S) by _S and remove (NS) for the register that are manually defined, so all the registers follow the same convention.

---
 target/arm/helper.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
target/arm/helper.c +18 -11
index c5d62c8..3b31f71 100644
@@ -694,12 +694,12 @@ static const ARMCPRegInfo cp_reginfo[] = {
      * the secure register to be properly reset and migrated. There is also no
      * v8 EL1 version of the register so the non-secure instance stands alone.
      */
-    { .name = "FCSEIDR(NS)",
+    { .name = "FCSEIDR",
       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
-    { .name = "FCSEIDR(S)",
+    { .name = "FCSEIDR_S",
       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
@@ -715,7 +715,7 @@ static const ARMCPRegInfo cp_reginfo[] = {
       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
-    { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
+    { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
@@ -1966,7 +1966,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
                                    cp15.c14_timer[GTIMER_PHYS].ctl),
       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
     },
-    { .name = "CNTP_CTL(S)",
+    { .name = "CNTP_CTL_S",
       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
       .secure = ARM_CP_SECSTATE_S,
       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
@@ -2005,7 +2005,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
       .accessfn = gt_ptimer_access,
       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
     },
-    { .name = "CNTP_TVAL(S)",
+    { .name = "CNTP_TVAL_S",
       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
       .secure = ARM_CP_SECSTATE_S,
       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
@@ -2059,7 +2059,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
       .accessfn = gt_ptimer_access,
       .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
     },
-    { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
+    { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
       .secure = ARM_CP_SECSTATE_S,
       .access = PL1_RW | PL0_R,
       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
@@ -5562,7 +5562,8 @@ CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
 
 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
                                    void *opaque, int state, int secstate,
-                                   int crm, int opc1, int opc2)
+                                   int crm, int opc1, int opc2,
+                                   const char *name)
 {
     /* Private utility function for define_one_arm_cp_reg_with_opaque():
      * add a single reginfo struct to the hash table.
@@ -5572,6 +5573,7 @@ static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
     int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
     int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
 
+    r2->name = g_strdup(name);
     /* Reset the secure state to the specific incoming state.  This is
      * necessary as the register may have been defined with both states.
      */
@@ -5803,19 +5805,24 @@ void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
                         /* Under AArch32 CP registers can be common
                          * (same for secure and non-secure world) or banked.
                          */
+                        char *name;
+
                         switch (r->secure) {
                         case ARM_CP_SECSTATE_S:
                         case ARM_CP_SECSTATE_NS:
                             add_cpreg_to_hashtable(cpu, r, opaque, state,
-                                                   r->secure, crm, opc1, opc2);
+                                                   r->secure, crm, opc1, opc2,
+                                                   r->name);
                             break;
                         default:
+                            name = g_strdup_printf("%s_S", r->name);
                             add_cpreg_to_hashtable(cpu, r, opaque, state,
                                                    ARM_CP_SECSTATE_S,
-                                                   crm, opc1, opc2);
+                                                   crm, opc1, opc2, name);
+                            g_free(name);
                             add_cpreg_to_hashtable(cpu, r, opaque, state,
                                                    ARM_CP_SECSTATE_NS,
-                                                   crm, opc1, opc2);
+                                                   crm, opc1, opc2, r->name);
                             break;
                         }
                     } else {
@@ -5823,7 +5830,7 @@ void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
                          * of AArch32 */
                         add_cpreg_to_hashtable(cpu, r, opaque, state,
                                                ARM_CP_SECSTATE_NS,
-                                               crm, opc1, opc2);
+                                               crm, opc1, opc2, r->name);
                     }
                 }
             }
Signature · 1 line
2.7.4
Reviewed-by on v4 2/4
#3 lore raw

Re: [Qemu-arm] [PATCH v4 2/4] target/arm: Add "_S" suffix to the secure version of a sysreg

Quoted 7 lines · Abdallah Bouassida

This is a preparation for the coming feature of creating dynamically an XML description for the ARM sysregs.
Add "_S" suffix to the secure version of sysregs that have both S and NS views Replace (S) by _S and remove (NS) for the register that are manually defined, so all the registers follow the same convention.

thanks
-- PMM

#4 lore raw

PATCH v4 3/4 target/arm: Add the XML dynamic generation

In reply to Abdallah Bouassida

Generate an XML description for the cp-regs.
Register these regs with the gdb_register_coprocessor(). Add arm_gdb_get_sysreg() to use it as a callback to read those regs. Add a dummy arm_gdb_set_sysreg().

---
 gdbstub.c            | 10 ++++++++
 include/qom/cpu.h    |  5 +++-
 target/arm/cpu.c     |  1 +
 target/arm/cpu.h     | 17 +++++++++++++
 target/arm/gdbstub.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 target/arm/helper.c  | 27 ++++++++++++++++++++
 6 files changed, 130 insertions(+), 1 deletion(-)
gdbstub.c +10 -0
index f1d5148..160bdbd 100644
@@ -674,6 +674,16 @@ static const char *get_feature_xml(const char *p, const char **newp,
         }
         return target_xml;
     }
+    if (cc->gdb_get_dynamic_xml) {
+        CPUState *cpu = first_cpu;
+        char *xmlname = g_strndup(p, len);
+        const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname, len);
+
+        free(xmlname);
+        if (xml) {
+            return xml;
+        }
+    }
     for (i = 0; ; i++) {
         name = xml_builtin[i][0];
         if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
include/qom/cpu.h +4 -1
index aff88fa..3f53da7 100644
@@ -131,6 +131,9 @@ struct TranslationBlock;
  *           before the insn which triggers a watchpoint rather than after it.
  * @gdb_arch_name: Optional callback that returns the architecture name known
  * to GDB. The caller must free the returned string with g_free.
+ * @gdb_get_dynamic_xml: Callback to return dynamically generated XML for the
+ *   gdb stub. Returns a pointer to the XML contents for the specified XML file
+ *   or NULL if the CPU doesn't have a dynamically generated content for it.
  * @cpu_exec_enter: Callback for cpu_exec preparation.
  * @cpu_exec_exit: Callback for cpu_exec cleanup.
  * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.
@@ -197,7 +200,7 @@ typedef struct CPUClass {
     const struct VMStateDescription *vmsd;
     const char *gdb_core_xml_file;
     gchar * (*gdb_arch_name)(CPUState *cpu);
-
+    char *(*gdb_get_dynamic_xml)(CPUState *cpu, char *xmlname, size_t len);
     void (*cpu_exec_enter)(CPUState *cpu);
     void (*cpu_exec_exit)(CPUState *cpu);
     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
target/arm/cpu.c +1 -0
index 1b3ae62..4fdda2f 100644
@@ -1781,6 +1781,7 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)
     cc->gdb_num_core_regs = 26;
     cc->gdb_core_xml_file = "arm-core.xml";
     cc->gdb_arch_name = arm_gdb_arch_name;
+    cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml;
     cc->gdb_stop_before_watchpoint = true;
     cc->debug_excp_handler = arm_debug_excp_handler;
     cc->debug_check_watchpoint = arm_debug_check_watchpoint;
target/arm/cpu.h +17 -0
index 818a98a..39b4f3a 100644
@@ -133,6 +133,19 @@ enum {
    s<2n+1> maps to the most significant half of d<n>
  */
 
+/**
+ * DynamicGDBXMLInfo:
+ * @desc: Contains the XML descriptions.
+ * @num_cpregs: Number of the Coprocessor registers seen by GDB.
+ * @cpregs_keys: Array that contains the corresponding Key of
+ * a given cpreg with the same order of the cpreg in the XML description.
+ */
+typedef struct DynamicGDBXMLInfo {
+    char *desc;
+    int num_cpregs;
+    uint32_t *cpregs_keys;
+} DynamicGDBXMLInfo;
+
 /* CPU state for each instance of a generic timer (in cp15 c14) */
 typedef struct ARMGenericTimer {
     uint64_t cval; /* Timer CompareValue register */
@@ -682,6 +695,8 @@ struct ARMCPU {
     uint64_t *cpreg_vmstate_values;
     int32_t cpreg_vmstate_array_len;
 
+    DynamicGDBXMLInfo dyn_xml;
+
     /* Timers used by the generic (architected) timer */
     QEMUTimer *gt_timer[NUM_GTIMERS];
     /* GPIO outputs for generic timer */
@@ -846,6 +861,8 @@ hwaddr arm_cpu_get_phys_page_attrs_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_gen_dynamic_xml(CPUState *cpu);
+char *arm_gdb_get_dynamic_xml(CPUState *cpu, char *xmlname, size_t len);
 
 int arm_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
                              int cpuid, void *opaque);
target/arm/gdbstub.c +71 -0

74 lines not on this page · raw message

target/arm/helper.c +27 -0
index 3b31f71..5929e0b 100644
@@ -219,6 +219,29 @@ static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
     }
 }
 
+static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
+{
+    ARMCPU *cpu = arm_env_get_cpu(env);
+    const ARMCPRegInfo *ri;
+    uint32_t key;
+
+    key = cpu->dyn_xml.cpregs_keys[reg];
+    ri = get_arm_cp_reginfo(cpu->cp_regs, key);
+    if (ri) {
+        if (cpreg_field_is_64bit(ri)) {
+            return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
+        } else {
+            return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
+        }
+    }
+    return 0;
+}
+
+static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
+{
+    return 0;
+}
+
 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
 {
    /* Return true if the regdef would cause an assertion if you called
@@ -5458,6 +5481,7 @@ void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
 {
     CPUState *cs = CPU(cpu);
     CPUARMState *env = &cpu->env;
+    int n;
 
     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
         gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
@@ -5473,6 +5497,9 @@ void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
                                  19, "arm-vfp.xml", 0);
     }
+    n = arm_gen_dynamic_xml(cs);
+    gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
+                             n, "system-registers.xml", 0);
 }
 
 /* Sort alphabetically by type name, except for "any". */
Signature · 1 line
2.7.4
#5 lore raw

Re: [Qemu-arm] [PATCH v4 3/4] target/arm: Add the XML dynamic generation

Quoted 20 lines · Abdallah Bouassida

Generate an XML description for the cp-regs.
Register these regs with the gdb_register_coprocessor(). Add arm_gdb_get_sysreg() to use it as a callback to read those regs. Add a dummy arm_gdb_set_sysreg().

---
 gdbstub.c            | 10 ++++++++
 include/qom/cpu.h    |  5 +++-
 target/arm/cpu.c     |  1 +
 target/arm/cpu.h     | 17 +++++++++++++
 target/arm/gdbstub.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 target/arm/helper.c  | 27 ++++++++++++++++++++
 6 files changed, 130 insertions(+), 1 deletion(-)
gdbstub.c +0 -0
index f1d5148..160bdbd 100644

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

Quoted 7 lines
gdbstub.c +4 -0
@@ -674,6 +674,16 @@ static const char *get_feature_xml(const char *p, const char **newp,
         }
         return target_xml;
     }
+    if (cc->gdb_get_dynamic_xml) {
+        CPUState *cpu = first_cpu;
+        char *xmlname = g_strndup(p, len);
+        const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname, len);

You've just turned the string name into a properly NUL terminated string, so you don't need to pass len to the hook.

Quoted 24 lines
gdbstub.c +6 -0
+
+        free(xmlname);
+        if (xml) {
+            return xml;
+        }
+    }
     for (i = 0; ; i++) {
         name = xml_builtin[i][0];
         if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
include/qom/cpu.h +3 -0
index aff88fa..3f53da7 100644
@@ -131,6 +131,9 @@ struct TranslationBlock;
  *           before the insn which triggers a watchpoint rather than after it.
  * @gdb_arch_name: Optional callback that returns the architecture name known
  * to GDB. The caller must free the returned string with g_free.
+ * @gdb_get_dynamic_xml: Callback to return dynamically generated XML for the
+ *   gdb stub. Returns a pointer to the XML contents for the specified XML file
+ *   or NULL if the CPU doesn't have a dynamically generated content for it.
  * @cpu_exec_enter: Callback for cpu_exec preparation.
  * @cpu_exec_exit: Callback for cpu_exec cleanup.
  * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.

include/qom/cpu.h typedef struct CPUClass { · line 203

Quoted 5 lines
include/qom/cpu.h +1 -1
@@ -197,7 +200,7 @@ typedef struct CPUClass {
     const struct VMStateDescription *vmsd;
     const char *gdb_core_xml_file;
     gchar * (*gdb_arch_name)(CPUState *cpu);
-
+    char *(*gdb_get_dynamic_xml)(CPUState *cpu, char *xmlname, size_t len);

This would be better to return a 'const char *' and for xmlname to be 'const char *'. Drop the len parameter.

Quoted 49 lines
include/qom/cpu.h +0 -0
     void (*cpu_exec_enter)(CPUState *cpu);
     void (*cpu_exec_exit)(CPUState *cpu);
     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
target/arm/cpu.c +1 -0
index 1b3ae62..4fdda2f 100644
@@ -1781,6 +1781,7 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)
     cc->gdb_num_core_regs = 26;
     cc->gdb_core_xml_file = "arm-core.xml";
     cc->gdb_arch_name = arm_gdb_arch_name;
+    cc->gdb_get_dynamic_xml = arm_gdb_get_dynamic_xml;
     cc->gdb_stop_before_watchpoint = true;
     cc->debug_excp_handler = arm_debug_excp_handler;
     cc->debug_check_watchpoint = arm_debug_check_watchpoint;
target/arm/cpu.h +15 -0
index 818a98a..39b4f3a 100644
@@ -133,6 +133,19 @@ enum {
    s<2n+1> maps to the most significant half of d<n>
  */
 
+/**
+ * DynamicGDBXMLInfo:
+ * @desc: Contains the XML descriptions.
+ * @num_cpregs: Number of the Coprocessor registers seen by GDB.
+ * @cpregs_keys: Array that contains the corresponding Key of
+ * a given cpreg with the same order of the cpreg in the XML description.
+ */
+typedef struct DynamicGDBXMLInfo {
+    char *desc;
+    int num_cpregs;
+    uint32_t *cpregs_keys;
+} DynamicGDBXMLInfo;
+
 /* CPU state for each instance of a generic timer (in cp15 c14) */
 typedef struct ARMGenericTimer {
     uint64_t cval; /* Timer CompareValue register */
@@ -682,6 +695,8 @@ struct ARMCPU {
     uint64_t *cpreg_vmstate_values;
     int32_t cpreg_vmstate_array_len;
 
+    DynamicGDBXMLInfo dyn_xml;
+
     /* Timers used by the generic (architected) timer */
     QEMUTimer *gt_timer[NUM_GTIMERS];
     /* GPIO outputs for generic timer */

target/arm/cpu.h hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr, · line 865

Quoted 5 lines
target/arm/cpu.h +2 -0
@@ -846,6 +861,8 @@ hwaddr arm_cpu_get_phys_page_attrs_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_gen_dynamic_xml(CPUState *cpu);
+char *arm_gdb_get_dynamic_xml(CPUState *cpu, char *xmlname, size_t len);

New function prototypes in header files should have at least a brief documentation comment, please.

Quoted 7 lines
target/arm/cpu.h +0 -0
 int arm_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
                              int cpuid, void *opaque);
target/arm/gdbstub.c +0 -0
index 04c1208..debd873 100644

target/arm/gdbstub.c int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 110

Quoted 10 lines
target/arm/gdbstub.c +7 -0
@@ -101,3 +101,74 @@ int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
     /* Unknown register.  */
     return 0;
 }
+
+static void arm_gen_one_xml_reg_tag(GString *s, DynamicGDBXMLInfo *dyn_xml,
+                                    ARMCPRegInfo *ri, uint32_t ri_key,
+                                    bool is64)
+{
+    g_string_append_printf(s, "<reg name=\"%s\"", ri->name);
+    g_string_append_printf(s, " bitsize=\"%s\"", is64 ? "64" : "32");

If you make the 'bool is64' argument instead be "int bitsize", then you can have this be (..., "bitsize=\"%d\"", bitsize), and your callsites are easier to read because it's more obvious they're correct when they're passing in 32 or 64 rather than 0 or 1 or true or false.

target/arm/gdbstub.c int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 114

Quoted 4 lines
target/arm/gdbstub.c +4 -0
+    g_string_append_printf(s, " group=\"cp_regs\"/>");
+    dyn_xml->num_cpregs++;
+    dyn_xml->cpregs_keys = g_renew(uint32_t, dyn_xml->cpregs_keys,
+                                   dyn_xml->num_cpregs);

This does a memory reallocation for every register we add to the array, which is pretty expensive. You have a reasonable upper bound on the size you need by calling g_hash_table_size() before you start iterating through it, so just allocate the array that big to start with.

Quoted 6 lines
target/arm/gdbstub.c +6 -0
+    dyn_xml->cpregs_keys[dyn_xml->num_cpregs - 1] = ri_key;
+}
+
+static void arm_register_sysreg_for_xml(gpointer key, gpointer value,
+                                        gpointer p)
+{

target/arm/gdbstub.c int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 130

Quoted 10 lines
target/arm/gdbstub.c +10 -0
+    uint32_t ri_key = *(uint32_t *)key;
+    ARMCPRegInfo *ri = value;
+    void **p_array = p;
+    ARMCPU *cpu = ARM_CPU((CPUState *)(p_array[0]));
+    CPUARMState *env = &cpu->env;
+    DynamicGDBXMLInfo *dyn_xml = &cpu->dyn_xml;
+    GString *s = (GString *)(p_array[1]);
+
+    if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_NO_GDB))) {
+        if (env->aarch64) {

This is wrong, because env->aarch64 is the current state of the CPU. You want to check arm_feature(env, ARM_FEATURE_AARCH64), which tells you whether the CPU is 64-bit or not.

Quoted 19 lines
target/arm/gdbstub.c +19 -0
+            if (ri->state == ARM_CP_STATE_AA64) {
+                arm_gen_one_xml_reg_tag(s , dyn_xml, ri, ri_key, 1);
+            }
+        } else {
+            if (ri->state == ARM_CP_STATE_AA32) {
+                if (!arm_feature(env, ARM_FEATURE_EL3) &&
+                    (ri->secure & ARM_CP_SECSTATE_S)) {
+                    return;
+                }
+                if (ri->type & ARM_CP_64BIT) {
+                    arm_gen_one_xml_reg_tag(s , dyn_xml, ri, ri_key, 1);
+                } else {
+                    arm_gen_one_xml_reg_tag(s , dyn_xml, ri, ri_key, 0);
+                }
+            }
+        }
+    }
+}
+

target/arm/gdbstub.c int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 159

Quoted 10 lines
target/arm/gdbstub.c +10 -0
+int arm_gen_dynamic_xml(CPUState *cs)
+{
+    ARMCPU *cpu = ARM_CPU(cs);
+    GString *s = g_string_new(NULL);
+    void *p_array[] = {cs, s};
+
+    cpu->dyn_xml.num_cpregs = 0;
+    g_string_printf(s, "<?xml version=\"1.0\"?>");
+    g_string_append_printf(s, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
+    g_string_append_printf(s, "<feature name=\"org.gnu.gdb.sys.regs\">");

What's the process for deciding what the feature name is in a gdb protocol XML declaration? Are we allowed to claim to be gdb.gnu.org, or should this be "org.qemu.something"? Should we have something in here that says "arm" ?

target/arm/gdbstub.c int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 163

Quoted 4 lines
target/arm/gdbstub.c +4 -0
+    g_hash_table_foreach(cpu->cp_regs, arm_register_sysreg_for_xml, p_array);
+    g_string_append_printf(s, "</feature>");
+    cpu->dyn_xml.desc = g_string_free(s, false);
+    return  cpu->dyn_xml.num_cpregs;

Stray double space after "return".

target/arm/gdbstub.c int arm_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 170

Quoted 7 lines
target/arm/gdbstub.c +7 -0
+}
+
+char *arm_gdb_get_dynamic_xml(CPUState *cs, char *xmlname, size_t len)
+{
+    ARMCPU *cpu = ARM_CPU(cs);
+
+    if (strncmp(xmlname, "system-registers.xml", len) == 0) {

Just strcmp() will do.

Quoted 57 lines
target/arm/gdbstub.c +4 -0
+        return cpu->dyn_xml.desc;
+    }
+    return NULL;
+}
target/arm/helper.c +27 -1
index 3b31f71..5929e0b 100644
@@ -219,6 +219,29 @@ static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
     }
 }
 
+static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
+{
+    ARMCPU *cpu = arm_env_get_cpu(env);
+    const ARMCPRegInfo *ri;
+    uint32_t key;
+
+    key = cpu->dyn_xml.cpregs_keys[reg];
+    ri = get_arm_cp_reginfo(cpu->cp_regs, key);
+    if (ri) {
+        if (cpreg_field_is_64bit(ri)) {
+            return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
+        } else {
+            return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
+        }
+    }
+    return 0;
+}
+
+static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
+{
+    return 0;
+}
+
 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
 {
    /* Return true if the regdef would cause an assertion if you called
@@ -5458,6 +5481,7 @@ void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
 {
     CPUState *cs = CPU(cpu);
     CPUARMState *env = &cpu->env;
+    int n;
 
     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
         gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
@@ -5473,6 +5497,9 @@ void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
         gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
                                  19, "arm-vfp.xml", 0);
     }
+    n = arm_gen_dynamic_xml(cs);
+    gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
+                             n, "system-registers.xml", 0);
 }
 
 /* Sort alphabetically by type name, except for "any". */
--
Quoted 1 line

2.7.4

thanks
-- PMM

#6 lore raw

PATCH v4 4/4 target/arm: Add arm_gdb_set_sysreg() callback

In reply to Abdallah Bouassida

This is a callback to set the cp-regs registered by the dynamic XML.

---

Quoted 11 lines
Quoted 3 lines

Adding to that our customers may need this write access, our tool TRACE32® needs this also in some particular cases. For example: temporary disabling MMU to do a physical memory access.

By clearing the SCTLR bit? That's a good example of a case that won't work reliably. If you clear the SCTLR.M bit via raw_write this will not perform the tlb_flush() that it needs to, which means that if anything does a memory access via the QEMU TLB it may get the wrong cached results. If you always clear the bit, do one gdb memory access then set the bit then it will probably not run into problems but you're walking on thin ice.

Does adding tlb_flush() before or after write_raw_cp_reg() could solve the reliability issue for other particular cases? Or is there any improvement that could be done for this write callback in order to get more reliable results for other particular cases?

 target/arm/helper.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
target/arm/helper.c +14 -0
index 5929e0b..bb5a97c 100644
@@ -239,6 +239,20 @@ static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
 
 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
 {
+    ARMCPU *cpu = arm_env_get_cpu(env);
+    const ARMCPRegInfo *ri;
+    uint32_t key;
+    uint32_t tmp;
+
+    tmp = ldl_p(buf);
+    key = cpu->dyn_xml.cpregs_keys[reg];
+    ri = get_arm_cp_reginfo(arm_env_get_cpu(env)->cp_regs, key);
+    if (ri) {
+        if (!(ri->type & ARM_CP_CONST)) {
+            write_raw_cp_reg(env, ri, tmp);
+            return cpreg_field_is_64bit(ri) ? 8 : 4;
+        }
+    }
     return 0;
 }
 
Signature · 1 line
2.7.4
#7 lore raw

Re: [Qemu-devel] [PATCH v4 4/4] target/arm: Add arm_gdb_set_sysreg() callback

Quoted 16 lines · Abdallah Bouassida

This is a callback to set the cp-regs registered by the dynamic XML.

---

Quoted 11 lines
Quoted 3 lines

Adding to that our customers may need this write access, our tool TRACE32® needs this also in some particular cases. For example: temporary disabling MMU to do a physical memory access.

By clearing the SCTLR bit? That's a good example of a case that won't work reliably. If you clear the SCTLR.M bit via raw_write this will not perform the tlb_flush() that it needs to, which means that if anything does a memory access via the QEMU TLB it may get the wrong cached results. If you always clear the bit, do one gdb memory access then set the bit then it will probably not run into problems but you're walking on thin ice.

Quoted 5 lines

Does adding tlb_flush() before or after write_raw_cp_reg() could solve the reliability issue for other particular cases? Or is there any improvement that could be done for this write callback in order to get more reliable results for other particular cases?

(We kind of discussed some of this on IRC, so this is just writing that conversation down as I understand the situation. I think we can get the first 3 patches in to shape to apply without having to resolve the writing-registers part first, anyway.)

I think there's an underlying unaddressed design question here, which is: what should the semantics of gdbstub writes to system registers be, and how can we best implement that? At the moment system registers have basically two access methods:
* real guest reads and writes, which we open-code in the code-generation parts of translate.c and translate-a64.c. This includes access-permission checks that don't allow a guest running at a lower exception level to access higher EL registers, and is also the code path that uses the CPRegInfo accessfn, readfn and writefn functions if a register spec defines them.
* "raw" reads and writes. These were designed for VM migration and for synchronization of state with the kernel when running under KVM. That means they're not expected to actually be changing the state of the CPU, and they just update the underlying CPU state data structure fields for the most part.

Neither of those is necessarily the right thing for a write from GDB.

We get away with using 'raw read' for reads from GDB because generally that gives us the right value and if we do happen to return a wrong value that's pretty harmless: it won't have upset the state of QEMU. But raw writes are potentially dangerous if we let the user make them. SCTLR.M is one example discussed above. Another is if you allow the user to write SCR_EL3 directly when the CPU isn't at EL3 then you might be allowing them to set SCR_EL3.NS == 0 with a PSTATE indicating EL2; QEMU will likely assert() shortly after because that's not architecturally valid.

Using an equivalent of the "real guest write" path would at least be a bit safer than a raw write, but I'm not sure that "sorry, can't do a physical memory access because the CPU's at EL0 and we can't write SCTLR" is the behaviour you want...

So my feeling is that to safely support guest sysreg writes from the gdbstub we need to:
* define what the semantics of doing that are (preferably by reference to something architectural, eg what you get if you try it via halting debug mode)
* provide some way of getting those semantics, which is probably another function pointer in the regdef struct for a gdb write
* go through the various regdefs and update them to be able to handle gdb writes (either by providing a function to do the job, or by being flagged as "raw write is ok for gdb write" or something. Will depend on the individual register.)

The really important part here is 1, I think. Part 3 will be a bit tedious, but I don't think there is any way to avoid it. There's no "works for all registers" shortcut.

thanks
-- PMM