target: Use target_addr_t for algorithm addresses.
[openocd.git] / src / target / cortex_m.c
index 6558252270a95959da1cb797cb437cb3d93fcfa8..721cf0a2469b9acdaccdda10471c3154fccdb2bc 100644 (file)
@@ -53,6 +53,9 @@
  * any longer.
  */
 
+/* Timeout for register r/w */
+#define DHCSR_S_REGRDY_TIMEOUT (500)
+
 /* Supported Cortex-M Cores */
 static const struct cortex_m_part_info cortex_m_parts[] = {
        {
@@ -148,9 +151,11 @@ static int cortex_m_read_dhcsr_atomic_sticky(struct target *target)
 static int cortex_m_load_core_reg_u32(struct target *target,
                uint32_t regsel, uint32_t *value)
 {
+       struct cortex_m_common *cortex_m = target_to_cm(target);
        struct armv7m_common *armv7m = target_to_armv7m(target);
        int retval;
-       uint32_t dcrdr;
+       uint32_t dcrdr, tmp_value;
+       int64_t then;
 
        /* because the DCB_DCRDR is used for the emulated dcc channel
         * we have to save/restore the DCB_DCRDR when used */
@@ -164,9 +169,29 @@ static int cortex_m_load_core_reg_u32(struct target *target,
        if (retval != ERROR_OK)
                return retval;
 
-       retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DCRDR, value);
-       if (retval != ERROR_OK)
-               return retval;
+       /* check if value from register is ready and pre-read it */
+       then = timeval_ms();
+       while (1) {
+               retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR,
+                                                                &cortex_m->dcb_dhcsr);
+               if (retval != ERROR_OK)
+                       return retval;
+               retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DCRDR,
+                                                                               &tmp_value);
+               if (retval != ERROR_OK)
+                       return retval;
+               cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
+               if (cortex_m->dcb_dhcsr & S_REGRDY)
+                       break;
+               cortex_m->slow_register_read = true; /* Polling (still) needed. */
+               if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
+                       LOG_ERROR("Timeout waiting for DCRDR transfer ready");
+                       return ERROR_TIMEOUT_REACHED;
+               }
+               keep_alive();
+       }
+
+       *value = tmp_value;
 
        if (target->dbg_msg_enabled) {
                /* restore DCB_DCRDR - this needs to be in a separate
@@ -178,12 +203,180 @@ static int cortex_m_load_core_reg_u32(struct target *target,
        return retval;
 }
 
+static int cortex_m_slow_read_all_regs(struct target *target)
+{
+       struct cortex_m_common *cortex_m = target_to_cm(target);
+       struct armv7m_common *armv7m = target_to_armv7m(target);
+       const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
+
+       /* Opportunistically restore fast read, it'll revert to slow
+        * if any register needed polling in cortex_m_load_core_reg_u32(). */
+       cortex_m->slow_register_read = false;
+
+       for (unsigned int reg_id = 0; reg_id < num_regs; reg_id++) {
+               struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
+               if (r->exist) {
+                       int retval = armv7m->arm.read_core_reg(target, r, reg_id, ARM_MODE_ANY);
+                       if (retval != ERROR_OK)
+                               return retval;
+               }
+       }
+
+       if (!cortex_m->slow_register_read)
+               LOG_DEBUG("Switching back to fast register reads");
+
+       return ERROR_OK;
+}
+
+static int cortex_m_queue_reg_read(struct target *target, uint32_t regsel,
+               uint32_t *reg_value, uint32_t *dhcsr)
+{
+       struct armv7m_common *armv7m = target_to_armv7m(target);
+       int retval;
+
+       retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
+       if (retval != ERROR_OK)
+               return retval;
+
+       retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR, dhcsr);
+       if (retval != ERROR_OK)
+               return retval;
+
+       return mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, reg_value);
+}
+
+static int cortex_m_fast_read_all_regs(struct target *target)
+{
+       struct cortex_m_common *cortex_m = target_to_cm(target);
+       struct armv7m_common *armv7m = target_to_armv7m(target);
+       int retval;
+       uint32_t dcrdr;
+
+       /* because the DCB_DCRDR is used for the emulated dcc channel
+        * we have to save/restore the DCB_DCRDR when used */
+       if (target->dbg_msg_enabled) {
+               retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
+               if (retval != ERROR_OK)
+                       return retval;
+       }
+
+       const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
+       const unsigned int n_r32 = ARMV7M_LAST_REG - ARMV7M_CORE_FIRST_REG + 1
+                                                          + ARMV7M_FPU_LAST_REG - ARMV7M_FPU_FIRST_REG + 1;
+       /* we need one 32-bit word for each register except FP D0..D15, which
+        * need two words */
+       uint32_t r_vals[n_r32];
+       uint32_t dhcsr[n_r32];
+
+       unsigned int wi = 0; /* write index to r_vals and dhcsr arrays */
+       unsigned int reg_id; /* register index in the reg_list, ARMV7M_R0... */
+       for (reg_id = 0; reg_id < num_regs; reg_id++) {
+               struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
+               if (!r->exist)
+                       continue;       /* skip non existent registers */
+
+               if (r->size <= 8) {
+                       /* Any 8-bit or shorter register is unpacked from a 32-bit
+                        * container register. Skip it now. */
+                       continue;
+               }
+
+               uint32_t regsel = armv7m_map_id_to_regsel(reg_id);
+               retval = cortex_m_queue_reg_read(target, regsel, &r_vals[wi],
+                                                                                &dhcsr[wi]);
+               if (retval != ERROR_OK)
+                       return retval;
+               wi++;
+
+               assert(r->size == 32 || r->size == 64);
+               if (r->size == 32)
+                       continue;       /* done with 32-bit register */
+
+               assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
+               /* the odd part of FP register (S1, S3...) */
+               retval = cortex_m_queue_reg_read(target, regsel + 1, &r_vals[wi],
+                                                                                        &dhcsr[wi]);
+               if (retval != ERROR_OK)
+                       return retval;
+               wi++;
+       }
+
+       assert(wi <= n_r32);
+
+       retval = dap_run(armv7m->debug_ap->dap);
+       if (retval != ERROR_OK)
+               return retval;
+
+       if (target->dbg_msg_enabled) {
+               /* restore DCB_DCRDR - this needs to be in a separate
+                * transaction otherwise the emulated DCC channel breaks */
+               retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
+               if (retval != ERROR_OK)
+                       return retval;
+       }
+
+       bool not_ready = false;
+       for (unsigned int i = 0; i < wi; i++) {
+               if ((dhcsr[i] & S_REGRDY) == 0) {
+                       not_ready = true;
+                       LOG_DEBUG("Register %u was not ready during fast read", i);
+               }
+               cortex_m_cumulate_dhcsr_sticky(cortex_m, dhcsr[i]);
+       }
+
+       if (not_ready) {
+               /* Any register was not ready,
+                * fall back to slow read with S_REGRDY polling */
+               return ERROR_TIMEOUT_REACHED;
+       }
+
+       LOG_DEBUG("read %u 32-bit registers", wi);
+
+       unsigned int ri = 0; /* read index from r_vals array */
+       for (reg_id = 0; reg_id < num_regs; reg_id++) {
+               struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
+               if (!r->exist)
+                       continue;       /* skip non existent registers */
+
+               r->dirty = false;
+
+               unsigned int reg32_id;
+               uint32_t offset;
+               if (armv7m_map_reg_packing(reg_id, &reg32_id, &offset)) {
+                       /* Unpack a partial register from 32-bit container register */
+                       struct reg *r32 = &armv7m->arm.core_cache->reg_list[reg32_id];
+
+                       /* The container register ought to precede all regs unpacked
+                        * from it in the reg_list. So the value should be ready
+                        * to unpack */
+                       assert(r32->valid);
+                       buf_cpy(r32->value + offset, r->value, r->size);
+
+               } else {
+                       assert(r->size == 32 || r->size == 64);
+                       buf_set_u32(r->value, 0, 32, r_vals[ri++]);
+
+                       if (r->size == 64) {
+                               assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
+                               /* the odd part of FP register (S1, S3...) */
+                               buf_set_u32(r->value + 4, 0, 32, r_vals[ri++]);
+                       }
+               }
+               r->valid = true;
+       }
+       assert(ri == wi);
+
+       return retval;
+}
+
 static int cortex_m_store_core_reg_u32(struct target *target,
                uint32_t regsel, uint32_t value)
 {
+       struct cortex_m_common *cortex_m = target_to_cm(target);
        struct armv7m_common *armv7m = target_to_armv7m(target);
        int retval;
        uint32_t dcrdr;
+       int64_t then;
 
        /* because the DCB_DCRDR is used for the emulated dcc channel
         * we have to save/restore the DCB_DCRDR when used */
@@ -197,10 +390,27 @@ static int cortex_m_store_core_reg_u32(struct target *target,
        if (retval != ERROR_OK)
                return retval;
 
-       retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRSR, regsel | DCRSR_WNR);
+       retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel | DCRSR_WNR);
        if (retval != ERROR_OK)
                return retval;
 
+       /* check if value is written into register */
+       then = timeval_ms();
+       while (1) {
+               retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR,
+                                                                               &cortex_m->dcb_dhcsr);
+               if (retval != ERROR_OK)
+                       return retval;
+               cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
+               if (cortex_m->dcb_dhcsr & S_REGRDY)
+                       break;
+               if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
+                       LOG_ERROR("Timeout waiting for DCRDR transfer ready");
+                       return ERROR_TIMEOUT_REACHED;
+               }
+               keep_alive();
+       }
+
        if (target->dbg_msg_enabled) {
                /* restore DCB_DCRDR - this needs to be in a separate
                 * transaction otherwise the emulated DCC channel breaks */
@@ -567,7 +777,6 @@ static int cortex_m_examine_exception_reason(struct target *target)
 
 static int cortex_m_debug_entry(struct target *target)
 {
-       int i;
        uint32_t xPSR;
        int retval;
        struct cortex_m_common *cortex_m = target_to_cm(target);
@@ -603,16 +812,21 @@ static int cortex_m_debug_entry(struct target *target)
                secure_state = (dscsr & DSCSR_CDS) == DSCSR_CDS;
        }
 
-       /* Examine target state and mode
-        * First load register accessible through core debug port */
-       int num_regs = arm->core_cache->num_regs;
-
-       for (i = 0; i < num_regs; i++) {
-               r = &armv7m->arm.core_cache->reg_list[i];
-               if (r->exist && !r->valid)
-                       arm->read_core_reg(target, r, i, ARM_MODE_ANY);
+       /* Load all registers to arm.core_cache */
+       if (!cortex_m->slow_register_read) {
+               retval = cortex_m_fast_read_all_regs(target);
+               if (retval == ERROR_TIMEOUT_REACHED) {
+                       cortex_m->slow_register_read = true;
+                       LOG_DEBUG("Switched to slow register read");
+               }
        }
 
+       if (cortex_m->slow_register_read)
+               retval = cortex_m_slow_read_all_regs(target);
+
+       if (retval != ERROR_OK)
+               return retval;
+
        r = arm->cpsr;
        xPSR = buf_get_u32(r->value, 0, 32);
 

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)