target/cortex_m: allow setting the type of a breakpoint
[openocd.git] / src / target / cortex_m.c
index 11dbf24f28eb871fffd7ed05ebfb6875a37acd53..30439f458f907de2d9884ba9f935c8b78334bc56 100644 (file)
  * any longer.
  */
 
-/**
- * Returns the type of a break point required by address location
- */
-#define BKPT_TYPE_BY_ADDR(addr) ((addr) < 0x20000000 ? BKPT_HARD : BKPT_SOFT)
-
 /* forward declarations */
 static int cortex_m_store_core_reg_u32(struct target *target,
                uint32_t num, uint32_t value);
@@ -168,12 +163,8 @@ static int cortex_m_single_step_core(struct target *target)
 {
        struct cortex_m_common *cortex_m = target_to_cm(target);
        struct armv7m_common *armv7m = &cortex_m->armv7m;
-       uint32_t dhcsr_save;
        int retval;
 
-       /* backup dhcsr reg */
-       dhcsr_save = cortex_m->dcb_dhcsr;
-
        /* Mask interrupts before clearing halt, if done already.  This avoids
         * Erratum 377497 (fixed in r1p0) where setting MASKINTS while clearing
         * HALT can put the core into an unknown state.
@@ -191,7 +182,6 @@ static int cortex_m_single_step_core(struct target *target)
        LOG_DEBUG(" ");
 
        /* restore dhcsr reg */
-       cortex_m->dcb_dhcsr = dhcsr_save;
        cortex_m_clear_halt(target);
 
        return ERROR_OK;
@@ -873,7 +863,7 @@ static int cortex_m_step(struct target *target, int current,
                                if (breakpoint)
                                        retval = cortex_m_set_breakpoint(target, breakpoint);
                                else
-                                       retval = breakpoint_add(target, pc_value, 2, BKPT_TYPE_BY_ADDR(pc_value));
+                                       retval = breakpoint_add(target, pc_value, 2, BKPT_HARD);
                                bool tmp_bp_set = (retval == ERROR_OK);
 
                                /* No more breakpoints left, just do a step */
@@ -1136,9 +1126,6 @@ int cortex_m_set_breakpoint(struct target *target, struct breakpoint *breakpoint
                return ERROR_OK;
        }
 
-       if (cortex_m->auto_bp_type)
-               breakpoint->type = BKPT_TYPE_BY_ADDR(breakpoint->address);
-
        if (breakpoint->type == BKPT_HARD) {
                uint32_t fpcr_value;
                while (comparator_list[fp_num].used && (fp_num < cortex_m->fp_num_code))
@@ -1258,21 +1245,6 @@ int cortex_m_add_breakpoint(struct target *target, struct breakpoint *breakpoint
 {
        struct cortex_m_common *cortex_m = target_to_cm(target);
 
-       if (cortex_m->auto_bp_type)
-               breakpoint->type = BKPT_TYPE_BY_ADDR(breakpoint->address);
-
-       if (breakpoint->type != BKPT_TYPE_BY_ADDR(breakpoint->address)) {
-               if (breakpoint->type == BKPT_HARD) {
-                       LOG_INFO("flash patch comparator requested outside code memory region");
-                       return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
-               }
-
-               if (breakpoint->type == BKPT_SOFT) {
-                       LOG_INFO("soft breakpoint requested in code (flash) memory region");
-                       return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
-               }
-       }
-
        if ((breakpoint->type == BKPT_HARD) && (cortex_m->fp_code_available < 1)) {
                LOG_INFO("no flash patch comparator unit available for hardware breakpoint");
                return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
@@ -1304,9 +1276,6 @@ int cortex_m_remove_breakpoint(struct target *target, struct breakpoint *breakpo
                return ERROR_TARGET_NOT_HALTED;
        }
 
-       if (cortex_m->auto_bp_type)
-               breakpoint->type = BKPT_TYPE_BY_ADDR(breakpoint->address);
-
        if (breakpoint->set)
                cortex_m_unset_breakpoint(target, breakpoint);
 
@@ -1712,6 +1681,97 @@ void cortex_m_deinit_target(struct target *target)
        free(cortex_m);
 }
 
+int cortex_m_profiling(struct target *target, uint32_t *samples,
+                             uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
+{
+       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);
+
+       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);
+       }
+
+       /* Make sure the target is running */
+       target_poll(target);
+       if (target->state == TARGET_HALTED)
+               retval = target_resume(target, 1, 0, 0, 0);
+
+       if (retval != ERROR_OK) {
+               LOG_ERROR("Error while resuming target");
+               return retval;
+       }
+
+       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++]);
+                       }
+               } 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;
+                       }
+               }
+
+               if (retval != ERROR_OK) {
+                       LOG_ERROR("Error while reading %s", use_pcsr ? "PCSR" : "target pc");
+                       return retval;
+               }
+
+
+               gettimeofday(&now, NULL);
+               if (sample_count >= max_num_samples || timeval_compare(&now, &timeout) > 0) {
+                       LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
+                       break;
+               }
+       }
+
+       *num_samples = sample_count;
+       return retval;
+}
+
+
 /* REVISIT cache valid/dirty bits are unmaintained.  We could set "valid"
  * on r/w if the core is not running, and clear on resume or reset ... or
  * at least, in a post_restore_context() method.
@@ -1768,6 +1828,18 @@ static struct dwt_reg dwt_comp[] = {
        DWT_COMPARATOR(1),
        DWT_COMPARATOR(2),
        DWT_COMPARATOR(3),
+       DWT_COMPARATOR(4),
+       DWT_COMPARATOR(5),
+       DWT_COMPARATOR(6),
+       DWT_COMPARATOR(7),
+       DWT_COMPARATOR(8),
+       DWT_COMPARATOR(9),
+       DWT_COMPARATOR(10),
+       DWT_COMPARATOR(11),
+       DWT_COMPARATOR(12),
+       DWT_COMPARATOR(13),
+       DWT_COMPARATOR(14),
+       DWT_COMPARATOR(15),
 #undef DWT_COMPARATOR
 };
 
@@ -1801,6 +1873,7 @@ void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
        int reg, i;
 
        target_read_u32(target, DWT_CTRL, &dwtcr);
+       LOG_DEBUG("DWT_CTRL: 0x%" PRIx32, dwtcr);
        if (!dwtcr) {
                LOG_DEBUG("no DWT");
                return;
@@ -1906,12 +1979,6 @@ int cortex_m_examine(struct target *target)
        /* stlink shares the examine handler but does not support
         * all its calls */
        if (!armv7m->stlink) {
-               retval = dap_dp_init(swjdp);
-               if (retval != ERROR_OK) {
-                       LOG_ERROR("Could not initialize the debug port");
-                       return retval;
-               }
-
                if (cortex_m->apsel < 0) {
                        /* Search for the MEM-AP */
                        retval = dap_find_ap(swjdp, AP_TYPE_AHB_AP, &armv7m->debug_ap);
@@ -2018,7 +2085,6 @@ int cortex_m_examine(struct target *target)
 
                /* Setup FPB */
                target_read_u32(target, FP_CTRL, &fpcr);
-               cortex_m->auto_bp_type = 1;
                /* bits [14:12] and [7:4] */
                cortex_m->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF);
                cortex_m->fp_num_lit = (fpcr >> 8) & 0xF;
@@ -2142,25 +2208,17 @@ static int cortex_m_handle_target_request(void *priv)
 }
 
 static int cortex_m_init_arch_info(struct target *target,
-       struct cortex_m_common *cortex_m, struct jtag_tap *tap)
+       struct cortex_m_common *cortex_m, struct adiv5_dap *dap)
 {
        struct armv7m_common *armv7m = &cortex_m->armv7m;
 
        armv7m_init_arch_info(target, armv7m);
 
-       /*  tap has no dap initialized */
-       if (!tap->dap) {
-               tap->dap = dap_init();
-
-               /* Leave (only) generic DAP stuff for debugport_init() */
-               tap->dap->tap = tap;
-       }
-
        /* default reset mode is to use srst if fitted
         * if not it will use CORTEX_M3_RESET_VECTRESET */
        cortex_m->soft_reset_config = CORTEX_M_RESET_VECTRESET;
 
-       armv7m->arm.dap = tap->dap;
+       armv7m->arm.dap = dap;
 
        /* register arch-specific functions */
        armv7m->examine_debug_reason = cortex_m_examine_debug_reason;
@@ -2180,16 +2238,16 @@ static int cortex_m_init_arch_info(struct target *target,
 static int cortex_m_target_create(struct target *target, Jim_Interp *interp)
 {
        struct cortex_m_common *cortex_m = calloc(1, sizeof(struct cortex_m_common));
-
        cortex_m->common_magic = CORTEX_M_COMMON_MAGIC;
-       cortex_m_init_arch_info(target, cortex_m, target->tap);
+       struct adiv5_private_config *pc;
 
-       if (target->private_config != NULL) {
-               struct adiv5_private_config *pc =
-                               (struct adiv5_private_config *)target->private_config;
-               cortex_m->apsel = pc->ap_num;
-       } else
-               cortex_m->apsel = -1;
+       pc = (struct adiv5_private_config *)target->private_config;
+       if (adiv5_verify_config(pc) != ERROR_OK)
+               return ERROR_FAIL;
+
+       cortex_m->apsel = pc->ap_num;
+
+       cortex_m_init_arch_info(target, cortex_m, pc->dap);
 
        return ERROR_OK;
 }
@@ -2456,4 +2514,6 @@ struct target_type cortexm_target = {
        .init_target = cortex_m_init_target,
        .examine = cortex_m_examine,
        .deinit_target = cortex_m_deinit_target,
+
+       .profiling = cortex_m_profiling,
 };

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)