target/target: read_memory 64-bit bugfix
[fw/openocd] / src / target / target.c
index 8a451883ba4ccdedca12408cd655ca2c51e61b6a..783159feccd85d8de0397832f406b53cc9da6fb6 100644 (file)
@@ -1,3 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
  *   Copyright (C) 2011 Andreas Fritiofson                                 *
  *   andreas.fritiofson@gmail.com                                          *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -57,6 +46,7 @@
 #include "transport/transport.h"
 #include "arm_cti.h"
 #include "smp.h"
+#include "semihosting_common.h"
 
 /* default halt wait timeout (ms) */
 #define DEFAULT_HALT_TIMEOUT 5000
@@ -87,6 +77,7 @@ extern struct target_type fa526_target;
 extern struct target_type feroceon_target;
 extern struct target_type dragonite_target;
 extern struct target_type xscale_target;
+extern struct target_type xtensa_chip_target;
 extern struct target_type cortexm_target;
 extern struct target_type cortexa_target;
 extern struct target_type aarch64_target;
@@ -104,6 +95,9 @@ extern struct target_type hla_target;
 extern struct target_type nds32_v2_target;
 extern struct target_type nds32_v3_target;
 extern struct target_type nds32_v3m_target;
+extern struct target_type esp32_target;
+extern struct target_type esp32s2_target;
+extern struct target_type esp32s3_target;
 extern struct target_type or1k_target;
 extern struct target_type quark_x10xx_target;
 extern struct target_type quark_d20xx_target;
@@ -125,6 +119,7 @@ static struct target_type *target_types[] = {
        &feroceon_target,
        &dragonite_target,
        &xscale_target,
+       &xtensa_chip_target,
        &cortexm_target,
        &cortexa_target,
        &cortexr4_target,
@@ -140,6 +135,9 @@ static struct target_type *target_types[] = {
        &nds32_v2_target,
        &nds32_v3_target,
        &nds32_v3m_target,
+       &esp32_target,
+       &esp32s2_target,
+       &esp32s3_target,
        &or1k_target,
        &quark_x10xx_target,
        &quark_d20xx_target,
@@ -238,14 +236,14 @@ static const struct jim_nvp nvp_target_event[] = {
 
        { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
 
-       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x100, .name = "semihosting-user-cmd-0x100" },
-       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x101, .name = "semihosting-user-cmd-0x101" },
-       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x102, .name = "semihosting-user-cmd-0x102" },
-       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x103, .name = "semihosting-user-cmd-0x103" },
-       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x104, .name = "semihosting-user-cmd-0x104" },
-       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x105, .name = "semihosting-user-cmd-0x105" },
-       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x106, .name = "semihosting-user-cmd-0x106" },
-       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0x107, .name = "semihosting-user-cmd-0x107" },
+       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100, .name = "semihosting-user-cmd-0x100" },
+       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101, .name = "semihosting-user-cmd-0x101" },
+       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102, .name = "semihosting-user-cmd-0x102" },
+       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103, .name = "semihosting-user-cmd-0x103" },
+       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104, .name = "semihosting-user-cmd-0x104" },
+       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105, .name = "semihosting-user-cmd-0x105" },
+       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106, .name = "semihosting-user-cmd-0x106" },
+       { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107, .name = "semihosting-user-cmd-0x107" },
 
        { .name = NULL, .value = -1 }
 };
@@ -658,10 +656,10 @@ int target_resume(struct target *target, int current, target_addr_t address,
         * Disable polling during resume() to guarantee the execution of handlers
         * in the correct order.
         */
-       bool save_poll = jtag_poll_get_enabled();
-       jtag_poll_set_enabled(false);
+       bool save_poll_mask = jtag_poll_mask();
        retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
-       jtag_poll_set_enabled(save_poll);
+       jtag_poll_unmask(save_poll_mask);
+
        if (retval != ERROR_OK)
                return retval;
 
@@ -689,14 +687,12 @@ static int target_process_reset(struct command_invocation *cmd, enum target_rese
         * more predictable, i.e. dr/irscan & pathmove in events will
         * not have JTAG operations injected into the middle of a sequence.
         */
-       bool save_poll = jtag_poll_get_enabled();
-
-       jtag_poll_set_enabled(false);
+       bool save_poll_mask = jtag_poll_mask();
 
        sprintf(buf, "ocd_process_reset %s", n->name);
        retval = Jim_Eval(cmd->ctx->interp, buf);
 
-       jtag_poll_set_enabled(save_poll);
+       jtag_poll_unmask(save_poll_mask);
 
        if (retval != JIM_OK) {
                Jim_MakeErrorMessage(cmd->ctx->interp);
@@ -2075,7 +2071,7 @@ int target_alloc_working_area_try(struct target *target, uint32_t size, struct w
                struct working_area *new_wa = malloc(sizeof(*new_wa));
                if (new_wa) {
                        new_wa->next = NULL;
-                       new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
+                       new_wa->size = ALIGN_DOWN(target->working_area_size, 4); /* 4-byte align */
                        new_wa->address = target->working_area;
                        new_wa->backup = NULL;
                        new_wa->user = NULL;
@@ -2086,8 +2082,7 @@ int target_alloc_working_area_try(struct target *target, uint32_t size, struct w
        }
 
        /* only allocate multiples of 4 byte */
-       if (size % 4)
-               size = (size + 3) & (~3UL);
+       size = ALIGN_UP(size, 4);
 
        struct working_area *c = target->working_areas;
 
@@ -2241,7 +2236,7 @@ uint32_t target_get_working_area_avail(struct target *target)
        uint32_t max_size = 0;
 
        if (!c)
-               return target->working_area_size;
+               return ALIGN_DOWN(target->working_area_size, 4);
 
        while (c) {
                if (c->free && max_size < c->size)
@@ -2258,6 +2253,8 @@ static void target_destroy(struct target *target)
        if (target->type->deinit_target)
                target->type->deinit_target(target);
 
+       if (target->semihosting)
+               free(target->semihosting->basedir);
        free(target->semihosting);
 
        jtag_unregister_event_callback(jtag_enable_callback, target);
@@ -2587,7 +2584,7 @@ int target_blank_check_memory(struct target *target,
        }
 
        if (!target->type->blank_check_memory)
-               return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
+               return ERROR_NOT_IMPLEMENTED;
 
        return target->type->blank_check_memory(target, blocks, num_blocks, erased_value);
 }
@@ -3331,7 +3328,7 @@ COMMAND_HANDLER(handle_soft_reset_halt_command)
 {
        struct target *target = get_current_target(CMD_CTX);
 
-       LOG_USER("requesting target halt and executing a soft reset");
+       LOG_TARGET_INFO(target, "requesting target halt and executing a soft reset");
 
        target_soft_reset_halt(target);
 
@@ -4720,7 +4717,7 @@ static int target_jim_read_memory(Jim_Interp *interp, int argc,
                                break;
                        }
 
-                       char value_buf[11];
+                       char value_buf[19];
                        snprintf(value_buf, sizeof(value_buf), "0x%" PRIx64, v);
 
                        Jim_ListAppendElement(interp, result_list,
@@ -6441,6 +6438,7 @@ static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
        int i;
        const char *targetname;
        int retval, len;
+       static int smp_group = 1;
        struct target *target = NULL;
        struct target_list *head, *new;
 
@@ -6472,9 +6470,10 @@ static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
        /*  now parse the list of cpu and put the target in smp mode*/
        foreach_smp_target(head, lh) {
                target = head->target;
-               target->smp = 1;
+               target->smp = smp_group;
                target->smp_targets = lh;
        }
+       smp_group++;
 
        if (target && target->rtos)
                retval = rtos_smp_init(target);