ARM11: minor cleanup, mostly ITR comments
[openocd.git] / src / target / target.c
index 31734b8f1bb30db61142ebfb1009b43f87118068..70130d9127d68fe59c7f3267066d410ea5501a22 100644 (file)
 #include "target_type.h"
 #include "target_request.h"
 #include "breakpoints.h"
-#include "time_support.h"
+#include <helper/time_support.h>
 #include "register.h"
 #include "trace.h"
 #include "image.h"
-#include "jtag.h"
+#include <jtag/jtag.h>
 
 
-static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
-
 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
 
@@ -92,7 +90,7 @@ struct target *all_targets = NULL;
 struct target_event_callback *target_event_callbacks = NULL;
 struct target_timer_callback *target_timer_callbacks = NULL;
 
-const Jim_Nvp nvp_assert[] = {
+static const Jim_Nvp nvp_assert[] = {
        { .name = "assert", NVP_ASSERT },
        { .name = "deassert", NVP_DEASSERT },
        { .name = "T", NVP_ASSERT },
@@ -102,7 +100,7 @@ const Jim_Nvp nvp_assert[] = {
        { .name = NULL, .value = -1 }
 };
 
-const Jim_Nvp nvp_error_target[] = {
+static const Jim_Nvp nvp_error_target[] = {
        { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
        { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
        { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
@@ -180,7 +178,7 @@ static const Jim_Nvp nvp_target_event[] = {
        { .name = NULL, .value = -1 }
 };
 
-const Jim_Nvp nvp_target_state[] = {
+static const Jim_Nvp nvp_target_state[] = {
        { .name = "unknown", .value = TARGET_UNKNOWN },
        { .name = "running", .value = TARGET_RUNNING },
        { .name = "halted",  .value = TARGET_HALTED },
@@ -189,7 +187,7 @@ const Jim_Nvp nvp_target_state[] = {
        { .name = NULL, .value = -1 },
 };
 
-const Jim_Nvp nvp_target_debug_reason [] = {
+static const Jim_Nvp nvp_target_debug_reason [] = {
        { .name = "debug-request"            , .value = DBG_REASON_DBGRQ },
        { .name = "breakpoint"               , .value = DBG_REASON_BREAKPOINT },
        { .name = "watchpoint"               , .value = DBG_REASON_WATCHPOINT },
@@ -200,7 +198,7 @@ const Jim_Nvp nvp_target_debug_reason [] = {
        { .name = NULL, .value = -1 },
 };
 
-const Jim_Nvp nvp_target_endian[] = {
+static const Jim_Nvp nvp_target_endian[] = {
        { .name = "big",    .value = TARGET_BIG_ENDIAN },
        { .name = "little", .value = TARGET_LITTLE_ENDIAN },
        { .name = "be",     .value = TARGET_BIG_ENDIAN },
@@ -208,7 +206,7 @@ const Jim_Nvp nvp_target_endian[] = {
        { .name = NULL,     .value = -1 },
 };
 
-const Jim_Nvp nvp_reset_modes[] = {
+static const Jim_Nvp nvp_reset_modes[] = {
        { .name = "unknown", .value = RESET_UNKNOWN },
        { .name = "run"    , .value = RESET_RUN },
        { .name = "halt"   , .value = RESET_HALT },
@@ -216,6 +214,19 @@ const Jim_Nvp nvp_reset_modes[] = {
        { .name = NULL     , .value = -1 },
 };
 
+const char *debug_reason_name(struct target *t)
+{
+       const char *cp;
+
+       cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
+                       t->debug_reason)->name;
+       if (!cp) {
+               LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
+               cp = "(*BUG*unknown*BUG*)";
+       }
+       return cp;
+}
+
 const char *
 target_state_name( struct target *t )
 {
@@ -665,84 +676,6 @@ static void target_reset_examined(struct target *target)
        target->examined = false;
 }
 
-
-
-static int default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
-{
-       LOG_ERROR("Not implemented: %s", __func__);
-       return ERROR_FAIL;
-}
-
-static int default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
-{
-       LOG_ERROR("Not implemented: %s", __func__);
-       return ERROR_FAIL;
-}
-
-static int arm_cp_check(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
-{
-       /* basic check */
-       if (!target_was_examined(target))
-       {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
-
-       if ((cpnum <0) || (cpnum > 15))
-       {
-               LOG_ERROR("Illegal co-processor %d", cpnum);
-               return ERROR_FAIL;
-       }
-
-       if (op1 > 7)
-       {
-               LOG_ERROR("Illegal op1");
-               return ERROR_FAIL;
-       }
-
-       if (op2 > 7)
-       {
-               LOG_ERROR("Illegal op2");
-               return ERROR_FAIL;
-       }
-
-       if (CRn > 15)
-       {
-               LOG_ERROR("Illegal CRn");
-               return ERROR_FAIL;
-       }
-
-       if (CRm > 15)
-       {
-               LOG_ERROR("Illegal CRm");
-               return ERROR_FAIL;
-       }
-
-       return ERROR_OK;
-}
-
-int target_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
-{
-       int retval;
-
-       retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
-       if (retval != ERROR_OK)
-               return retval;
-
-       return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
-}
-
-int target_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
-{
-       int retval;
-
-       retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
-       if (retval != ERROR_OK)
-               return retval;
-
-       return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
-}
-
 static int
 err_read_phys_memory(struct target *target, uint32_t address,
                uint32_t size, uint32_t count, uint8_t *buffer)
@@ -761,130 +694,129 @@ err_write_phys_memory(struct target *target, uint32_t address,
 
 static int handle_target(void *priv);
 
-int target_init(struct command_context *cmd_ctx)
+static int target_init_one(struct command_context *cmd_ctx,
+               struct target *target)
 {
-       struct target *target;
-       int retval;
+       target_reset_examined(target);
 
-       for (target = all_targets; target; target = target->next) {
-               struct target_type *type = target->type;
+       struct target_type *type = target->type;
+       if (type->examine == NULL)
+               type->examine = default_examine;
 
-               target_reset_examined(target);
-               if (target->type->examine == NULL)
-               {
-                       target->type->examine = default_examine;
-               }
+       int retval = type->init_target(cmd_ctx, target);
+       if (ERROR_OK != retval)
+       {
+               LOG_ERROR("target '%s' init failed", target_name(target));
+               return retval;
+       }
 
-               if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
-               {
-                       LOG_ERROR("target '%s' init failed", target_name(target));
-                       return retval;
-               }
+       /**
+        * @todo get rid of those *memory_imp() methods, now that all
+        * callers are using target_*_memory() accessors ... and make
+        * sure the "physical" paths handle the same issues.
+        */
+       /* a non-invasive way(in terms of patches) to add some code that
+        * runs before the type->write/read_memory implementation
+        */
+       type->write_memory_imp = target->type->write_memory;
+       type->write_memory = target_write_memory_imp;
 
-               /**
-                * @todo MCR/MRC are ARM-specific; don't require them in
-                * all targets, or for ARMs without coprocessors.
-                */
-               if (target->type->mcr == NULL)
-               {
-                       target->type->mcr = default_mcr;
-               } else
+       type->read_memory_imp = target->type->read_memory;
+       type->read_memory = target_read_memory_imp;
+
+       type->soft_reset_halt_imp = target->type->soft_reset_halt;
+       type->soft_reset_halt = target_soft_reset_halt_imp;
+
+       type->run_algorithm_imp = target->type->run_algorithm;
+       type->run_algorithm = target_run_algorithm_imp;
+
+       /* Sanity-check MMU support ... stub in what we must, to help
+        * implement it in stages, but warn if we need to do so.
+        */
+       if (type->mmu)
+       {
+               if (type->write_phys_memory == NULL)
                {
-                       const struct command_registration mcr_cmd = {
-                               .name = "mcr",
-                               .mode = COMMAND_EXEC,
-                               .jim_handler = &jim_mcrmrc,
-                               .help = "write coprocessor",
-                               .usage = "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
-                       };
-                       register_command(cmd_ctx, NULL, &mcr_cmd);
+                       LOG_ERROR("type '%s' is missing write_phys_memory",
+                                       type->name);
+                       type->write_phys_memory = err_write_phys_memory;
                }
-
-               if (target->type->mrc == NULL)
+               if (type->read_phys_memory == NULL)
                {
-                       target->type->mrc = default_mrc;
-               } else
+                       LOG_ERROR("type '%s' is missing read_phys_memory",
+                                       type->name);
+                       type->read_phys_memory = err_read_phys_memory;
+               }
+               if (type->virt2phys == NULL)
                {
-                       const struct command_registration mrc_cmd = {
-                               .name = "mrc",
-                               .jim_handler = &jim_mcrmrc,
-                               .help = "read coprocessor",
-                               .usage = "<cpnum> <op1> <op2> <CRn> <CRm>",
-                       };
-                       register_command(cmd_ctx, NULL, &mrc_cmd);
+                       LOG_ERROR("type '%s' is missing virt2phys", type->name);
+                       type->virt2phys = identity_virt2phys;
                }
-
-
-               /**
-                * @todo get rid of those *memory_imp() methods, now that all
-                * callers are using target_*_memory() accessors ... and make
-                * sure the "physical" paths handle the same issues.
-                */
-
-               /* a non-invasive way(in terms of patches) to add some code that
-                * runs before the type->write/read_memory implementation
-                */
-               target->type->write_memory_imp = target->type->write_memory;
-               target->type->write_memory = target_write_memory_imp;
-               target->type->read_memory_imp = target->type->read_memory;
-               target->type->read_memory = target_read_memory_imp;
-               target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
-               target->type->soft_reset_halt = target_soft_reset_halt_imp;
-               target->type->run_algorithm_imp = target->type->run_algorithm;
-               target->type->run_algorithm = target_run_algorithm_imp;
-
-               /* Sanity-check MMU support ... stub in what we must, to help
-                * implement it in stages, but warn if we need to do so.
-                */
-               if (type->mmu) {
-                       if (type->write_phys_memory == NULL) {
-                               LOG_ERROR("type '%s' is missing %s",
-                                               type->name,
-                                               "write_phys_memory");
-                               type->write_phys_memory = err_write_phys_memory;
-                       }
-                       if (type->read_phys_memory == NULL) {
-                               LOG_ERROR("type '%s' is missing %s",
-                                               type->name,
-                                               "read_phys_memory");
-                               type->read_phys_memory = err_read_phys_memory;
-                       }
-                       if (type->virt2phys == NULL) {
-                               LOG_ERROR("type '%s' is missing %s",
-                                               type->name,
-                                               "virt2phys");
-                               type->virt2phys = identity_virt2phys;
-                       }
-
+       }
+       else
+       {
                /* Make sure no-MMU targets all behave the same:  make no
                 * distinction between physical and virtual addresses, and
                 * ensure that virt2phys() is always an identity mapping.
                 */
-               } else {
-                       if (type->write_phys_memory
-                                       || type->read_phys_memory
-                                       || type->virt2phys)
-                               LOG_WARNING("type '%s' has broken MMU hooks",
-                                               type->name);
-
-                       type->mmu = no_mmu;
-                       type->write_phys_memory = type->write_memory;
-                       type->read_phys_memory = type->read_memory;
-                       type->virt2phys = identity_virt2phys;
+               if (type->write_phys_memory || type->read_phys_memory
+                               || type->virt2phys)
+               {
+                       LOG_WARNING("type '%s' has bad MMU hooks", type->name);
                }
+
+               type->mmu = no_mmu;
+               type->write_phys_memory = type->write_memory;
+               type->read_phys_memory = type->read_memory;
+               type->virt2phys = identity_virt2phys;
        }
+       return ERROR_OK;
+}
+
+int target_init(struct command_context *cmd_ctx)
+{
+       struct target *target;
+       int retval;
 
-       if (all_targets)
+       for (target = all_targets; target; target = target->next)
        {
-               if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
-                       return retval;
-               if ((retval = target_register_timer_callback(&handle_target, 100, 1, cmd_ctx->interp)) != ERROR_OK)
+               retval = target_init_one(cmd_ctx, target);
+               if (ERROR_OK != retval)
                        return retval;
        }
 
+       if (!all_targets)
+               return ERROR_OK;
+
+       retval = target_register_user_commands(cmd_ctx);
+       if (ERROR_OK != retval)
+               return retval;
+
+       retval = target_register_timer_callback(&handle_target,
+                       100, 1, cmd_ctx->interp);
+       if (ERROR_OK != retval)
+               return retval;
+
        return ERROR_OK;
 }
 
+COMMAND_HANDLER(handle_target_init_command)
+{
+       if (CMD_ARGC != 0)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
+       static bool target_initialized = false;
+       if (target_initialized)
+       {
+               LOG_INFO("'target init' has already been called");
+               return ERROR_OK;
+       }
+       target_initialized = true;
+
+       LOG_DEBUG("Initializing targets...");
+       return target_init(CMD_CTX);
+}
+
 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
 {
        struct target_event_callback **callbacks_p = &target_event_callbacks;
@@ -2222,8 +2154,9 @@ static void handle_md_output(struct command_context *cmd_ctx,
        case 2: value_fmt = "%4.2x "; break;
        case 1: value_fmt = "%2.2x "; break;
        default:
+               /* "can't happen", caller checked */
                LOG_ERROR("invalid memory read size: %u", size);
-               exit(-1);
+               return;
        }
 
        for (unsigned i = 0; i < count; i++)
@@ -4671,6 +4604,12 @@ static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 }
 
 static const struct command_registration target_subcommand_handlers[] = {
+       {
+               .name = "init",
+               .mode = COMMAND_CONFIG,
+               .handler = &handle_target_init_command,
+               .help = "initialize targets",
+       },
        {
                .name = "create",
                .mode = COMMAND_ANY,
@@ -4883,92 +4822,6 @@ COMMAND_HANDLER(handle_fast_load_command)
        return retval;
 }
 
-static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
-{
-       struct command_context *context;
-       struct target *target;
-       int retval;
-
-       context = Jim_GetAssocData(interp, "context");
-       if (context == NULL) {
-               LOG_ERROR("array2mem: no command context");
-               return JIM_ERR;
-       }
-       target = get_current_target(context);
-       if (target == NULL) {
-               LOG_ERROR("array2mem: no current target");
-               return JIM_ERR;
-       }
-
-       if ((argc < 6) || (argc > 7))
-       {
-               return JIM_ERR;
-       }
-
-       int cpnum;
-       uint32_t op1;
-       uint32_t op2;
-       uint32_t CRn;
-       uint32_t CRm;
-       uint32_t value;
-
-       int e;
-       long l;
-       e = Jim_GetLong(interp, argv[1], &l);
-       if (e != JIM_OK) {
-               return e;
-       }
-       cpnum = l;
-
-       e = Jim_GetLong(interp, argv[2], &l);
-       if (e != JIM_OK) {
-               return e;
-       }
-       op1 = l;
-
-       e = Jim_GetLong(interp, argv[3], &l);
-       if (e != JIM_OK) {
-               return e;
-       }
-       CRn = l;
-
-       e = Jim_GetLong(interp, argv[4], &l);
-       if (e != JIM_OK) {
-               return e;
-       }
-       CRm = l;
-
-       e = Jim_GetLong(interp, argv[5], &l);
-       if (e != JIM_OK) {
-               return e;
-       }
-       op2 = l;
-
-       value = 0;
-
-       if (argc == 7)
-       {
-               e = Jim_GetLong(interp, argv[6], &l);
-               if (e != JIM_OK) {
-                       return e;
-               }
-               value = l;
-
-               retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
-               if (retval != ERROR_OK)
-                       return JIM_ERR;
-       } else
-       {
-               retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
-               if (retval != ERROR_OK)
-                       return JIM_ERR;
-
-               Jim_SetResult(interp, Jim_NewIntObj(interp, value));
-       }
-
-       return JIM_OK;
-}
-
 static const struct command_registration target_command_handlers[] = {
        {
                .name = "targets",

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)