target/armv7m: use arch_info[i].value instead of allocated memory
[openocd.git] / src / target / cortex_m.c
index e52332dd620dfdfcc32185dc561a4f96dd3805bf..7b3f3c70e31d6fc20e559db9de9c31fc47937be1 100644 (file)
@@ -56,8 +56,8 @@ static int cortex_m_store_core_reg_u32(struct target *target,
                uint32_t num, uint32_t value);
 static void cortex_m_dwt_free(struct target *target);
 
-static int cortexm_dap_read_coreregister_u32(struct target *target,
-       uint32_t *value, int regnum)
+static int cortex_m_load_core_reg_u32(struct target *target,
+               uint32_t regsel, uint32_t *value)
 {
        struct armv7m_common *armv7m = target_to_armv7m(target);
        int retval;
@@ -71,7 +71,7 @@ static int cortexm_dap_read_coreregister_u32(struct target *target,
                        return retval;
        }
 
-       retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regnum);
+       retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
        if (retval != ERROR_OK)
                return retval;
 
@@ -89,8 +89,8 @@ static int cortexm_dap_read_coreregister_u32(struct target *target,
        return retval;
 }
 
-static int cortexm_dap_write_coreregister_u32(struct target *target,
-       uint32_t value, int regnum)
+static int cortex_m_store_core_reg_u32(struct target *target,
+               uint32_t regsel, uint32_t value)
 {
        struct armv7m_common *armv7m = target_to_armv7m(target);
        int retval;
@@ -108,7 +108,7 @@ static int cortexm_dap_write_coreregister_u32(struct target *target,
        if (retval != ERROR_OK)
                return retval;
 
-       retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRSR, regnum | DCRSR_WnR);
+       retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRSR, regsel | DCRSR_WnR);
        if (retval != ERROR_OK)
                return retval;
 
@@ -530,7 +530,7 @@ static int cortex_m_debug_entry(struct target *target)
        /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
        if (xPSR & 0xf00) {
                r->dirty = r->valid;
-               cortex_m_store_core_reg_u32(target, 16, xPSR & ~0xff);
+               cortex_m_store_core_reg_u32(target, ARMV7M_REGSEL_xPSR, xPSR & ~0xff);
        }
 
        /* Are we in an exception handler */
@@ -541,7 +541,7 @@ static int cortex_m_debug_entry(struct target *target)
                arm->map = armv7m_msp_reg_map;
        } else {
                unsigned control = buf_get_u32(arm->core_cache
-                               ->reg_list[ARMV7M_CONTROL].value, 0, 2);
+                               ->reg_list[ARMV7M_CONTROL].value, 0, 3);
 
                /* is this thread privileged? */
                arm->core_mode = control & 1
@@ -656,13 +656,9 @@ static int cortex_m_poll(struct target *target)
                }
        }
 
-       /* REVISIT when S_SLEEP is set, it's in a Sleep or DeepSleep state.
-        * How best to model low power modes?
-        */
-
        if (target->state == TARGET_UNKNOWN) {
-               /* check if processor is retiring instructions */
-               if (cortex_m->dcb_dhcsr & S_RETIRE_ST) {
+               /* check if processor is retiring instructions or sleeping */
+               if (cortex_m->dcb_dhcsr & S_RETIRE_ST || cortex_m->dcb_dhcsr & S_SLEEP) {
                        target->state = TARGET_RUNNING;
                        retval = ERROR_OK;
                }
@@ -1413,7 +1409,7 @@ int cortex_m_remove_breakpoint(struct target *target, struct breakpoint *breakpo
        return cortex_m_unset_breakpoint(target, breakpoint);
 }
 
-int cortex_m_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
+static int cortex_m_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
 {
        int dwt_num = 0;
        struct cortex_m_common *cortex_m = target_to_cm(target);
@@ -1496,7 +1492,7 @@ int cortex_m_set_watchpoint(struct target *target, struct watchpoint *watchpoint
        return ERROR_OK;
 }
 
-int cortex_m_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
+static int cortex_m_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
 {
        struct cortex_m_common *cortex_m = target_to_cm(target);
        struct cortex_m_dwt_comparator *comparator;
@@ -1610,176 +1606,6 @@ void cortex_m_enable_watchpoints(struct target *target)
        }
 }
 
-static int cortex_m_load_core_reg_u32(struct target *target,
-               uint32_t num, uint32_t *value)
-{
-       int retval;
-
-       /* NOTE:  we "know" here that the register identifiers used
-        * in the v7m header match the Cortex-M3 Debug Core Register
-        * Selector values for R0..R15, xPSR, MSP, and PSP.
-        */
-       switch (num) {
-               case 0 ... 18:
-                       /* read a normal core register */
-                       retval = cortexm_dap_read_coreregister_u32(target, value, num);
-
-                       if (retval != ERROR_OK) {
-                               LOG_ERROR("JTAG failure %i", retval);
-                               return ERROR_JTAG_DEVICE_ERROR;
-                       }
-                       LOG_DEBUG("load from core reg %i  value 0x%" PRIx32 "", (int)num, *value);
-                       break;
-
-               case ARMV7M_FPSCR:
-                       /* Floating-point Status and Registers */
-                       retval = target_write_u32(target, DCB_DCRSR, 0x21);
-                       if (retval != ERROR_OK)
-                               return retval;
-                       retval = target_read_u32(target, DCB_DCRDR, value);
-                       if (retval != ERROR_OK)
-                               return retval;
-                       LOG_DEBUG("load from FPSCR  value 0x%" PRIx32, *value);
-                       break;
-
-               case ARMV7M_S0 ... ARMV7M_S31:
-                       /* Floating-point Status and Registers */
-                       retval = target_write_u32(target, DCB_DCRSR, num - ARMV7M_S0 + 0x40);
-                       if (retval != ERROR_OK)
-                               return retval;
-                       retval = target_read_u32(target, DCB_DCRDR, value);
-                       if (retval != ERROR_OK)
-                               return retval;
-                       LOG_DEBUG("load from FPU reg S%d  value 0x%" PRIx32,
-                                 (int)(num - ARMV7M_S0), *value);
-                       break;
-
-               case ARMV7M_PRIMASK:
-               case ARMV7M_BASEPRI:
-               case ARMV7M_FAULTMASK:
-               case ARMV7M_CONTROL:
-                       /* Cortex-M3 packages these four registers as bitfields
-                        * in one Debug Core register.  So say r0 and r2 docs;
-                        * it was removed from r1 docs, but still works.
-                        */
-                       cortexm_dap_read_coreregister_u32(target, value, 20);
-
-                       switch (num) {
-                               case ARMV7M_PRIMASK:
-                                       *value = buf_get_u32((uint8_t *)value, 0, 1);
-                                       break;
-
-                               case ARMV7M_BASEPRI:
-                                       *value = buf_get_u32((uint8_t *)value, 8, 8);
-                                       break;
-
-                               case ARMV7M_FAULTMASK:
-                                       *value = buf_get_u32((uint8_t *)value, 16, 1);
-                                       break;
-
-                               case ARMV7M_CONTROL:
-                                       *value = buf_get_u32((uint8_t *)value, 24, 2);
-                                       break;
-                       }
-
-                       LOG_DEBUG("load from special reg %i value 0x%" PRIx32 "", (int)num, *value);
-                       break;
-
-               default:
-                       return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-
-       return ERROR_OK;
-}
-
-static int cortex_m_store_core_reg_u32(struct target *target,
-               uint32_t num, uint32_t value)
-{
-       int retval;
-       uint32_t reg;
-       struct armv7m_common *armv7m = target_to_armv7m(target);
-
-       /* NOTE:  we "know" here that the register identifiers used
-        * in the v7m header match the Cortex-M3 Debug Core Register
-        * Selector values for R0..R15, xPSR, MSP, and PSP.
-        */
-       switch (num) {
-               case 0 ... 18:
-                       retval = cortexm_dap_write_coreregister_u32(target, value, num);
-                       if (retval != ERROR_OK) {
-                               struct reg *r;
-
-                               LOG_ERROR("JTAG failure");
-                               r = armv7m->arm.core_cache->reg_list + num;
-                               r->dirty = r->valid;
-                               return ERROR_JTAG_DEVICE_ERROR;
-                       }
-                       LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
-                       break;
-
-               case ARMV7M_FPSCR:
-                       /* Floating-point Status and Registers */
-                       retval = target_write_u32(target, DCB_DCRDR, value);
-                       if (retval != ERROR_OK)
-                               return retval;
-                       retval = target_write_u32(target, DCB_DCRSR, 0x21 | (1<<16));
-                       if (retval != ERROR_OK)
-                               return retval;
-                       LOG_DEBUG("write FPSCR value 0x%" PRIx32, value);
-                       break;
-
-               case ARMV7M_S0 ... ARMV7M_S31:
-                       /* Floating-point Status and Registers */
-                       retval = target_write_u32(target, DCB_DCRDR, value);
-                       if (retval != ERROR_OK)
-                               return retval;
-                       retval = target_write_u32(target, DCB_DCRSR, (num - ARMV7M_S0 + 0x40) | (1<<16));
-                       if (retval != ERROR_OK)
-                               return retval;
-                       LOG_DEBUG("write FPU reg S%d  value 0x%" PRIx32,
-                                 (int)(num - ARMV7M_S0), value);
-                       break;
-
-               case ARMV7M_PRIMASK:
-               case ARMV7M_BASEPRI:
-               case ARMV7M_FAULTMASK:
-               case ARMV7M_CONTROL:
-                       /* Cortex-M3 packages these four registers as bitfields
-                        * in one Debug Core register.  So say r0 and r2 docs;
-                        * it was removed from r1 docs, but still works.
-                        */
-                       cortexm_dap_read_coreregister_u32(target, &reg, 20);
-
-                       switch (num) {
-                               case ARMV7M_PRIMASK:
-                                       buf_set_u32((uint8_t *)&reg, 0, 1, value);
-                                       break;
-
-                               case ARMV7M_BASEPRI:
-                                       buf_set_u32((uint8_t *)&reg, 8, 8, value);
-                                       break;
-
-                               case ARMV7M_FAULTMASK:
-                                       buf_set_u32((uint8_t *)&reg, 16, 1, value);
-                                       break;
-
-                               case ARMV7M_CONTROL:
-                                       buf_set_u32((uint8_t *)&reg, 24, 2, value);
-                                       break;
-                       }
-
-                       cortexm_dap_write_coreregister_u32(target, reg, 20);
-
-                       LOG_DEBUG("write special reg %i value 0x%" PRIx32 " ", (int)num, value);
-                       break;
-
-               default:
-                       return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-
-       return ERROR_OK;
-}
-
 static int cortex_m_read_memory(struct target *target, target_addr_t address,
        uint32_t size, uint32_t count, uint8_t *buffer)
 {
@@ -1835,28 +1661,23 @@ int cortex_m_profiling(struct target *target, uint32_t *samples,
        struct timeval timeout, now;
        struct armv7m_common *armv7m = target_to_armv7m(target);
        uint32_t reg_value;
-       bool use_pcsr = false;
-       int retval = ERROR_OK;
-       struct reg *reg;
-
-       gettimeofday(&timeout, NULL);
-       timeval_add_time(&timeout, seconds, 0);
+       int retval;
 
        retval = target_read_u32(target, DWT_PCSR, &reg_value);
        if (retval != ERROR_OK) {
                LOG_ERROR("Error while reading PCSR");
                return retval;
        }
-
-       if (reg_value != 0) {
-               use_pcsr = true;
-               LOG_INFO("Starting Cortex-M profiling. Sampling DWT_PCSR as fast as we can...");
-       } else {
-               LOG_INFO("Starting profiling. Halting and resuming the"
-                        " target as often as we can...");
-               reg = register_get_by_name(target->reg_cache, "pc", 1);
+       if (reg_value == 0) {
+               LOG_INFO("PCSR sampling not supported on this processor.");
+               return target_profiling_default(target, samples, max_num_samples, num_samples, seconds);
        }
 
+       gettimeofday(&timeout, NULL);
+       timeval_add_time(&timeout, seconds, 0);
+
+       LOG_INFO("Starting Cortex-M profiling. Sampling DWT_PCSR as fast as we can...");
+
        /* Make sure the target is running */
        target_poll(target);
        if (target->state == TARGET_HALTED)
@@ -1870,40 +1691,21 @@ int cortex_m_profiling(struct target *target, uint32_t *samples,
        uint32_t sample_count = 0;
 
        for (;;) {
-               if (use_pcsr) {
-                       if (armv7m && armv7m->debug_ap) {
-                               uint32_t read_count = max_num_samples - sample_count;
-                               if (read_count > 1024)
-                                       read_count = 1024;
-
-                               retval = mem_ap_read_buf_noincr(armv7m->debug_ap,
-                                                       (void *)&samples[sample_count],
-                                                       4, read_count, DWT_PCSR);
-                               sample_count += read_count;
-                       } else {
-                               target_read_u32(target, DWT_PCSR, &samples[sample_count++]);
-                       }
+               if (armv7m && armv7m->debug_ap) {
+                       uint32_t read_count = max_num_samples - sample_count;
+                       if (read_count > 1024)
+                               read_count = 1024;
+
+                       retval = mem_ap_read_buf_noincr(armv7m->debug_ap,
+                                               (void *)&samples[sample_count],
+                                               4, read_count, DWT_PCSR);
+                       sample_count += read_count;
                } else {
-                       target_poll(target);
-                       if (target->state == TARGET_HALTED) {
-                               reg_value = buf_get_u32(reg->value, 0, 32);
-                               /* current pc, addr = 0, do not handle breakpoints, not debugging */
-                               retval = target_resume(target, 1, 0, 0, 0);
-                               samples[sample_count++] = reg_value;
-                               target_poll(target);
-                               alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
-                       } else if (target->state == TARGET_RUNNING) {
-                               /* We want to quickly sample the PC. */
-                               retval = target_halt(target);
-                       } else {
-                               LOG_INFO("Target not halted or running");
-                               retval = ERROR_OK;
-                               break;
-                       }
+                       target_read_u32(target, DWT_PCSR, &samples[sample_count++]);
                }
 
                if (retval != ERROR_OK) {
-                       LOG_ERROR("Error while reading %s", use_pcsr ? "PCSR" : "target pc");
+                       LOG_ERROR("Error while reading PCSR");
                        return retval;
                }
 
@@ -2013,7 +1815,7 @@ static void cortex_m_dwt_addreg(struct target *t, struct reg *r, const struct dw
        r->type = &dwt_reg_type;
 }
 
-void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
+static void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
 {
        uint32_t dwtcr;
        struct reg_cache *cache;
@@ -2242,7 +2044,6 @@ int cortex_m_examine(struct target *target)
                        for (idx = ARMV7M_NUM_CORE_REGS_NOFP;
                             idx < armv7m->arm.core_cache->num_regs;
                             idx++) {
-                               free(armv7m->arm.core_cache->reg_list[idx].value);
                                free(armv7m->arm.core_cache->reg_list[idx].feature);
                                free(armv7m->arm.core_cache->reg_list[idx].reg_data_type);
                        }
@@ -2502,6 +2303,11 @@ COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
        if (retval != ERROR_OK)
                return retval;
 
+       if (!target_was_examined(target)) {
+               LOG_ERROR("Target not examined yet");
+               return ERROR_FAIL;
+       }
+
        retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
        if (retval != ERROR_OK)
                return retval;

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)