1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2008 by Oyvind Harboe *
9 * oyvind.harboe@zylin.com *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
33 #include "breakpoints.h"
34 #include "arm_disassembler.h"
35 #include <helper/binarybuffer.h>
36 #include "algorithm.h"
40 /* offsets into armv4_5 core register cache */
43 ARMV4_5_SPSR_FIQ = 32,
44 ARMV4_5_SPSR_IRQ = 33,
45 ARMV4_5_SPSR_SVC = 34,
46 ARMV4_5_SPSR_ABT = 35,
47 ARMV4_5_SPSR_UND = 36,
51 static const uint8_t arm_usr_indices[17] = {
52 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ARMV4_5_CPSR,
55 static const uint8_t arm_fiq_indices[8] = {
56 16, 17, 18, 19, 20, 21, 22, ARMV4_5_SPSR_FIQ,
59 static const uint8_t arm_irq_indices[3] = {
60 23, 24, ARMV4_5_SPSR_IRQ,
63 static const uint8_t arm_svc_indices[3] = {
64 25, 26, ARMV4_5_SPSR_SVC,
67 static const uint8_t arm_abt_indices[3] = {
68 27, 28, ARMV4_5_SPSR_ABT,
71 static const uint8_t arm_und_indices[3] = {
72 29, 30, ARMV4_5_SPSR_UND,
75 static const uint8_t arm_mon_indices[3] = {
82 /* For user and system modes, these list indices for all registers.
83 * otherwise they're just indices for the shadow registers and SPSR.
85 unsigned short n_indices;
86 const uint8_t *indices;
88 /* Seven modes are standard from ARM7 on. "System" and "User" share
89 * the same registers; other modes shadow from 3 to 8 registers.
94 .n_indices = ARRAY_SIZE(arm_usr_indices),
95 .indices = arm_usr_indices,
100 .n_indices = ARRAY_SIZE(arm_fiq_indices),
101 .indices = arm_fiq_indices,
104 .name = "Supervisor",
106 .n_indices = ARRAY_SIZE(arm_svc_indices),
107 .indices = arm_svc_indices,
112 .n_indices = ARRAY_SIZE(arm_abt_indices),
113 .indices = arm_abt_indices,
118 .n_indices = ARRAY_SIZE(arm_irq_indices),
119 .indices = arm_irq_indices,
122 .name = "Undefined instruction",
124 .n_indices = ARRAY_SIZE(arm_und_indices),
125 .indices = arm_und_indices,
130 .n_indices = ARRAY_SIZE(arm_usr_indices),
131 .indices = arm_usr_indices,
133 /* TrustZone "Security Extensions" add a secure monitor mode.
134 * This is distinct from a "debug monitor" which can support
135 * non-halting debug, in conjunction with some debuggers.
138 .name = "Secure Monitor",
140 .n_indices = ARRAY_SIZE(arm_mon_indices),
141 .indices = arm_mon_indices,
145 /** Map PSR mode bits to the name of an ARM processor operating mode. */
146 const char *arm_mode_name(unsigned psr_mode)
148 for (unsigned i = 0; i < ARRAY_SIZE(arm_mode_data); i++) {
149 if (arm_mode_data[i].psr == psr_mode)
150 return arm_mode_data[i].name;
152 LOG_ERROR("unrecognized psr mode: %#02x", psr_mode);
153 return "UNRECOGNIZED";
156 /** Return true iff the parameter denotes a valid ARM processor mode. */
157 bool is_arm_mode(unsigned psr_mode)
159 for (unsigned i = 0; i < ARRAY_SIZE(arm_mode_data); i++) {
160 if (arm_mode_data[i].psr == psr_mode)
166 /** Map PSR mode bits to linear number indexing armv4_5_core_reg_map */
167 int arm_mode_to_number(enum arm_mode mode)
171 /* map MODE_ANY to user mode */
189 LOG_ERROR("invalid mode value encountered %d", mode);
194 /** Map linear number indexing armv4_5_core_reg_map to PSR mode bits. */
195 enum arm_mode armv4_5_number_to_mode(int number)
215 LOG_ERROR("mode index out of bounds %d", number);
220 static const char *arm_state_strings[] =
222 "ARM", "Thumb", "Jazelle", "ThumbEE",
225 /* Templates for ARM core registers.
227 * NOTE: offsets in this table are coupled to the arm_mode_data
228 * table above, the armv4_5_core_reg_map array below, and also to
229 * the ARMV4_5_CPSR symbol (which should vanish after ARM11 updates).
231 static const struct {
232 /* The name is used for e.g. the "regs" command. */
235 /* The {cookie, mode} tuple uniquely identifies one register.
236 * In a given mode, cookies 0..15 map to registers R0..R15,
237 * with R13..R15 usually called SP, LR, PC.
239 * MODE_ANY is used as *input* to the mapping, and indicates
240 * various special cases (sigh) and errors.
242 * Cookie 16 is (currently) confusing, since it indicates
243 * CPSR -or- SPSR depending on whether 'mode' is MODE_ANY.
244 * (Exception modes have both CPSR and SPSR registers ...)
248 } arm_core_regs[] = {
249 /* IMPORTANT: we guarantee that the first eight cached registers
250 * correspond to r0..r7, and the fifteenth to PC, so that callers
251 * don't need to map them.
253 { .name = "r0", .cookie = 0, .mode = ARM_MODE_ANY, },
254 { .name = "r1", .cookie = 1, .mode = ARM_MODE_ANY, },
255 { .name = "r2", .cookie = 2, .mode = ARM_MODE_ANY, },
256 { .name = "r3", .cookie = 3, .mode = ARM_MODE_ANY, },
257 { .name = "r4", .cookie = 4, .mode = ARM_MODE_ANY, },
258 { .name = "r5", .cookie = 5, .mode = ARM_MODE_ANY, },
259 { .name = "r6", .cookie = 6, .mode = ARM_MODE_ANY, },
260 { .name = "r7", .cookie = 7, .mode = ARM_MODE_ANY, },
262 /* NOTE: regs 8..12 might be shadowed by FIQ ... flagging
263 * them as MODE_ANY creates special cases. (ANY means
264 * "not mapped" elsewhere; here it's "everything but FIQ".)
266 { .name = "r8", .cookie = 8, .mode = ARM_MODE_ANY, },
267 { .name = "r9", .cookie = 9, .mode = ARM_MODE_ANY, },
268 { .name = "r10", .cookie = 10, .mode = ARM_MODE_ANY, },
269 { .name = "r11", .cookie = 11, .mode = ARM_MODE_ANY, },
270 { .name = "r12", .cookie = 12, .mode = ARM_MODE_ANY, },
272 /* NOTE all MODE_USR registers are equivalent to MODE_SYS ones */
273 { .name = "sp_usr", .cookie = 13, .mode = ARM_MODE_USR, },
274 { .name = "lr_usr", .cookie = 14, .mode = ARM_MODE_USR, },
276 /* guaranteed to be at index 15 */
277 { .name = "pc", .cookie = 15, .mode = ARM_MODE_ANY, },
279 { .name = "r8_fiq", .cookie = 8, .mode = ARM_MODE_FIQ, },
280 { .name = "r9_fiq", .cookie = 9, .mode = ARM_MODE_FIQ, },
281 { .name = "r10_fiq", .cookie = 10, .mode = ARM_MODE_FIQ, },
282 { .name = "r11_fiq", .cookie = 11, .mode = ARM_MODE_FIQ, },
283 { .name = "r12_fiq", .cookie = 12, .mode = ARM_MODE_FIQ, },
285 { .name = "sp_fiq", .cookie = 13, .mode = ARM_MODE_FIQ, },
286 { .name = "lr_fiq", .cookie = 14, .mode = ARM_MODE_FIQ, },
288 { .name = "sp_irq", .cookie = 13, .mode = ARM_MODE_IRQ, },
289 { .name = "lr_irq", .cookie = 14, .mode = ARM_MODE_IRQ, },
291 { .name = "sp_svc", .cookie = 13, .mode = ARM_MODE_SVC, },
292 { .name = "lr_svc", .cookie = 14, .mode = ARM_MODE_SVC, },
294 { .name = "sp_abt", .cookie = 13, .mode = ARM_MODE_ABT, },
295 { .name = "lr_abt", .cookie = 14, .mode = ARM_MODE_ABT, },
297 { .name = "sp_und", .cookie = 13, .mode = ARM_MODE_UND, },
298 { .name = "lr_und", .cookie = 14, .mode = ARM_MODE_UND, },
300 { .name = "cpsr", .cookie = 16, .mode = ARM_MODE_ANY, },
301 { .name = "spsr_fiq", .cookie = 16, .mode = ARM_MODE_FIQ, },
302 { .name = "spsr_irq", .cookie = 16, .mode = ARM_MODE_IRQ, },
303 { .name = "spsr_svc", .cookie = 16, .mode = ARM_MODE_SVC, },
304 { .name = "spsr_abt", .cookie = 16, .mode = ARM_MODE_ABT, },
305 { .name = "spsr_und", .cookie = 16, .mode = ARM_MODE_UND, },
307 { .name = "sp_mon", .cookie = 13, .mode = ARM_MODE_MON, },
308 { .name = "lr_mon", .cookie = 14, .mode = ARM_MODE_MON, },
309 { .name = "spsr_mon", .cookie = 16, .mode = ARM_MODE_MON, },
312 /* map core mode (USR, FIQ, ...) and register number to
313 * indices into the register cache
315 const int armv4_5_core_reg_map[8][17] =
318 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31
320 { /* FIQ (8 shadows of USR, vs normal 3) */
321 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 15, 32
324 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 23, 24, 15, 33
327 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 25, 26, 15, 34
330 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 27, 28, 15, 35
333 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 29, 30, 15, 36
335 { /* SYS (same registers as USR) */
336 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31
339 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 37, 38, 15, 39,
344 * Configures host-side ARM records to reflect the specified CPSR.
345 * Later, code can use arm_reg_current() to map register numbers
346 * according to how they are exposed by this mode.
348 void arm_set_cpsr(struct arm *arm, uint32_t cpsr)
350 enum arm_mode mode = cpsr & 0x1f;
353 /* NOTE: this may be called very early, before the register
354 * cache is set up. We can't defend against many errors, in
355 * particular against CPSRs that aren't valid *here* ...
358 buf_set_u32(arm->cpsr->value, 0, 32, cpsr);
359 arm->cpsr->valid = 1;
360 arm->cpsr->dirty = 0;
363 arm->core_mode = mode;
365 /* mode_to_number() warned; set up a somewhat-sane mapping */
366 num = arm_mode_to_number(mode);
372 arm->map = &armv4_5_core_reg_map[num][0];
373 arm->spsr = (mode == ARM_MODE_USR || mode == ARM_MODE_SYS)
375 : arm->core_cache->reg_list + arm->map[16];
377 /* Older ARMs won't have the J bit */
378 enum arm_state state;
380 if (cpsr & (1 << 5)) { /* T */
381 if (cpsr & (1 << 24)) { /* J */
382 LOG_WARNING("ThumbEE -- incomplete support");
383 state = ARM_STATE_THUMB_EE;
385 state = ARM_STATE_THUMB;
387 if (cpsr & (1 << 24)) { /* J */
388 LOG_ERROR("Jazelle state handling is BROKEN!");
389 state = ARM_STATE_JAZELLE;
391 state = ARM_STATE_ARM;
393 arm->core_state = state;
395 LOG_DEBUG("set CPSR %#8.8x: %s mode, %s state", (unsigned) cpsr,
397 arm_state_strings[arm->core_state]);
401 * Returns handle to the register currently mapped to a given number.
402 * Someone must have called arm_set_cpsr() before.
404 * \param arm This core's state and registers are used.
405 * \param regnum From 0..15 corresponding to R0..R14 and PC.
406 * Note that R0..R7 don't require mapping; you may access those
407 * as the first eight entries in the register cache. Likewise
408 * R15 (PC) doesn't need mapping; you may also access it directly.
409 * However, R8..R14, and SPSR (arm->spsr) *must* be mapped.
410 * CPSR (arm->cpsr) is also not mapped.
412 struct reg *arm_reg_current(struct arm *arm, unsigned regnum)
419 r = arm->core_cache->reg_list + arm->map[regnum];
421 /* e.g. invalid CPSR said "secure monitor" mode on a core
422 * that doesn't support it...
425 LOG_ERROR("Invalid CPSR mode");
426 r = arm->core_cache->reg_list + regnum;
432 static const uint8_t arm_gdb_dummy_fp_value[12];
435 * Dummy FPA registers are required to support GDB on ARM.
436 * Register packets require eight obsolete FPA register values.
437 * Modern ARM cores use Vector Floating Point (VFP), if they
438 * have any floating point support. VFP is not FPA-compatible.
440 struct reg arm_gdb_dummy_fp_reg =
442 .name = "GDB dummy FPA register",
443 .value = (uint8_t *) arm_gdb_dummy_fp_value,
448 static const uint8_t arm_gdb_dummy_fps_value[4];
451 * Dummy FPA status registers are required to support GDB on ARM.
452 * Register packets require an obsolete FPA status register.
454 struct reg arm_gdb_dummy_fps_reg =
456 .name = "GDB dummy FPA status register",
457 .value = (uint8_t *) arm_gdb_dummy_fps_value,
462 static void arm_gdb_dummy_init(void) __attribute__ ((constructor));
464 static void arm_gdb_dummy_init(void)
466 register_init_dummy(&arm_gdb_dummy_fp_reg);
467 register_init_dummy(&arm_gdb_dummy_fps_reg);
470 static int armv4_5_get_core_reg(struct reg *reg)
473 struct arm_reg *armv4_5 = reg->arch_info;
474 struct target *target = armv4_5->target;
476 if (target->state != TARGET_HALTED)
478 LOG_ERROR("Target not halted");
479 return ERROR_TARGET_NOT_HALTED;
482 retval = armv4_5->armv4_5_common->read_core_reg(target, reg, armv4_5->num, armv4_5->mode);
483 if (retval == ERROR_OK) {
491 static int armv4_5_set_core_reg(struct reg *reg, uint8_t *buf)
493 struct arm_reg *armv4_5 = reg->arch_info;
494 struct target *target = armv4_5->target;
495 struct arm *armv4_5_target = target_to_arm(target);
496 uint32_t value = buf_get_u32(buf, 0, 32);
498 if (target->state != TARGET_HALTED)
500 LOG_ERROR("Target not halted");
501 return ERROR_TARGET_NOT_HALTED;
504 /* Except for CPSR, the "reg" command exposes a writeback model
505 * for the register cache.
507 if (reg == armv4_5_target->cpsr) {
508 arm_set_cpsr(armv4_5_target, value);
510 /* Older cores need help to be in ARM mode during halt
511 * mode debug, so we clear the J and T bits if we flush.
512 * For newer cores (v6/v7a/v7r) we don't need that, but
513 * it won't hurt since CPSR is always flushed anyway.
515 if (armv4_5_target->core_mode !=
516 (enum arm_mode)(value & 0x1f)) {
517 LOG_DEBUG("changing ARM core mode to '%s'",
518 arm_mode_name(value & 0x1f));
519 value &= ~((1 << 24) | (1 << 5));
520 armv4_5_target->write_core_reg(target, reg,
521 16, ARM_MODE_ANY, value);
524 buf_set_u32(reg->value, 0, 32, value);
532 static const struct reg_arch_type arm_reg_type = {
533 .get = armv4_5_get_core_reg,
534 .set = armv4_5_set_core_reg,
537 struct reg_cache *arm_build_reg_cache(struct target *target, struct arm *arm)
539 int num_regs = ARRAY_SIZE(arm_core_regs);
540 struct reg_cache *cache = malloc(sizeof(struct reg_cache));
541 struct reg *reg_list = calloc(num_regs, sizeof(struct reg));
542 struct arm_reg *arch_info = calloc(num_regs, sizeof(struct arm_reg));
545 if (!cache || !reg_list || !arch_info) {
552 cache->name = "ARM registers";
554 cache->reg_list = reg_list;
557 for (i = 0; i < num_regs; i++)
559 /* Skip registers this core doesn't expose */
560 if (arm_core_regs[i].mode == ARM_MODE_MON
561 && arm->core_type != ARM_MODE_MON)
564 /* REVISIT handle Cortex-M, which only shadows R13/SP */
566 arch_info[i].num = arm_core_regs[i].cookie;
567 arch_info[i].mode = arm_core_regs[i].mode;
568 arch_info[i].target = target;
569 arch_info[i].armv4_5_common = arm;
571 reg_list[i].name = (char *) arm_core_regs[i].name;
572 reg_list[i].size = 32;
573 reg_list[i].value = &arch_info[i].value;
574 reg_list[i].type = &arm_reg_type;
575 reg_list[i].arch_info = &arch_info[i];
580 arm->pc = reg_list + 15;
581 arm->cpsr = reg_list + ARMV4_5_CPSR;
582 arm->core_cache = cache;
586 int arm_arch_state(struct target *target)
588 struct arm *armv4_5 = target_to_arm(target);
590 if (armv4_5->common_magic != ARM_COMMON_MAGIC)
592 LOG_ERROR("BUG: called for a non-ARM target");
596 LOG_USER("target halted in %s state due to %s, current mode: %s\n"
597 "cpsr: 0x%8.8" PRIx32 " pc: 0x%8.8" PRIx32 "%s",
598 arm_state_strings[armv4_5->core_state],
599 debug_reason_name(target),
600 arm_mode_name(armv4_5->core_mode),
601 buf_get_u32(armv4_5->cpsr->value, 0, 32),
602 buf_get_u32(armv4_5->pc->value, 0, 32),
603 armv4_5->is_semihosting ? ", semihosting" : "");
608 #define ARMV4_5_CORE_REG_MODENUM(cache, mode, num) \
609 cache->reg_list[armv4_5_core_reg_map[mode][num]]
611 COMMAND_HANDLER(handle_armv4_5_reg_command)
613 struct target *target = get_current_target(CMD_CTX);
614 struct arm *armv4_5 = target_to_arm(target);
617 if (!is_arm(armv4_5))
619 command_print(CMD_CTX, "current target isn't an ARM");
623 if (target->state != TARGET_HALTED)
625 command_print(CMD_CTX, "error: target must be halted for register accesses");
629 if (armv4_5->core_type != ARM_MODE_ANY)
631 command_print(CMD_CTX, "Microcontroller Profile not supported - use standard reg cmd");
635 if (!is_arm_mode(armv4_5->core_mode))
637 LOG_ERROR("not a valid arm core mode - communication failure?");
641 if (!armv4_5->full_context) {
642 command_print(CMD_CTX, "error: target doesn't support %s",
647 regs = armv4_5->core_cache->reg_list;
649 for (unsigned mode = 0; mode < ARRAY_SIZE(arm_mode_data); mode++) {
654 /* label this bank of registers (or shadows) */
655 switch (arm_mode_data[mode].psr) {
659 name = "System and User";
663 if (armv4_5->core_type != ARM_MODE_MON)
667 name = arm_mode_data[mode].name;
671 command_print(CMD_CTX, "%s%s mode %sregisters",
674 /* display N rows of up to 4 registers each */
675 for (unsigned i = 0; i < arm_mode_data[mode].n_indices;) {
679 for (unsigned j = 0; j < 4; j++, i++) {
681 struct reg *reg = regs;
683 if (i >= arm_mode_data[mode].n_indices)
686 reg += arm_mode_data[mode].indices[i];
688 /* REVISIT be smarter about faults... */
690 armv4_5->full_context(target);
692 value = buf_get_u32(reg->value, 0, 32);
693 output_len += snprintf(output + output_len,
694 sizeof(output) - output_len,
695 "%8s: %8.8" PRIx32 " ",
698 command_print(CMD_CTX, "%s", output);
705 COMMAND_HANDLER(handle_armv4_5_core_state_command)
707 struct target *target = get_current_target(CMD_CTX);
708 struct arm *armv4_5 = target_to_arm(target);
710 if (!is_arm(armv4_5))
712 command_print(CMD_CTX, "current target isn't an ARM");
716 if (armv4_5->core_type == ARM_MODE_THREAD)
718 /* armv7m not supported */
719 command_print(CMD_CTX, "Unsupported Command");
725 if (strcmp(CMD_ARGV[0], "arm") == 0)
727 armv4_5->core_state = ARM_STATE_ARM;
729 if (strcmp(CMD_ARGV[0], "thumb") == 0)
731 armv4_5->core_state = ARM_STATE_THUMB;
735 command_print(CMD_CTX, "core state: %s", arm_state_strings[armv4_5->core_state]);
740 COMMAND_HANDLER(handle_arm_disassemble_command)
742 int retval = ERROR_OK;
743 struct target *target = get_current_target(CMD_CTX);
744 struct arm *arm = target ? target_to_arm(target) : NULL;
750 command_print(CMD_CTX, "current target isn't an ARM");
754 if (arm->core_type == ARM_MODE_THREAD)
756 /* armv7m is always thumb mode */
762 if (strcmp(CMD_ARGV[2], "thumb") != 0)
767 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], count);
770 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
771 if (address & 0x01) {
773 command_print(CMD_CTX, "Disassemble as Thumb");
781 command_print(CMD_CTX,
782 "usage: arm disassemble <address> [<count> ['thumb']]");
787 while (count-- > 0) {
788 struct arm_instruction cur_instruction;
791 /* Always use Thumb2 disassembly for best handling
792 * of 32-bit BL/BLX, and to work with newer cores
793 * (some ARMv6, all ARMv7) that use Thumb2.
795 retval = thumb2_opcode(target, address,
797 if (retval != ERROR_OK)
802 retval = target_read_u32(target, address, &opcode);
803 if (retval != ERROR_OK)
805 retval = arm_evaluate_opcode(opcode, address,
806 &cur_instruction) != ERROR_OK;
807 if (retval != ERROR_OK)
810 command_print(CMD_CTX, "%s", cur_instruction.text);
811 address += cur_instruction.instruction_size;
817 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
819 struct command_context *context;
820 struct target *target;
824 context = current_command_context(interp);
825 assert( context != NULL);
827 target = get_current_target(context);
828 if (target == NULL) {
829 LOG_ERROR("%s: no current target", __func__);
832 if (!target_was_examined(target)) {
833 LOG_ERROR("%s: not yet examined", target_name(target));
836 arm = target_to_arm(target);
838 LOG_ERROR("%s: not an ARM", target_name(target));
842 if ((argc < 6) || (argc > 7)) {
843 /* FIXME use the command name to verify # params... */
844 LOG_ERROR("%s: wrong number of arguments", __func__);
856 /* NOTE: parameter sequence matches ARM instruction set usage:
857 * MCR pNUM, op1, rX, CRn, CRm, op2 ; write CP from rX
858 * MRC pNUM, op1, rX, CRn, CRm, op2 ; read CP into rX
859 * The "rX" is necessarily omitted; it uses Tcl mechanisms.
861 retval = Jim_GetLong(interp, argv[1], &l);
862 if (retval != JIM_OK)
865 LOG_ERROR("%s: %s %d out of range", __func__,
866 "coprocessor", (int) l);
871 retval = Jim_GetLong(interp, argv[2], &l);
872 if (retval != JIM_OK)
875 LOG_ERROR("%s: %s %d out of range", __func__,
881 retval = Jim_GetLong(interp, argv[3], &l);
882 if (retval != JIM_OK)
885 LOG_ERROR("%s: %s %d out of range", __func__,
891 retval = Jim_GetLong(interp, argv[4], &l);
892 if (retval != JIM_OK)
895 LOG_ERROR("%s: %s %d out of range", __func__,
901 retval = Jim_GetLong(interp, argv[5], &l);
902 if (retval != JIM_OK)
905 LOG_ERROR("%s: %s %d out of range", __func__,
913 /* FIXME don't assume "mrc" vs "mcr" from the number of params;
914 * that could easily be a typo! Check both...
916 * FIXME change the call syntax here ... simplest to just pass
917 * the MRC() or MCR() instruction to be executed. That will also
918 * let us support the "mrc2" and "mcr2" opcodes (toggling one bit)
919 * if that's ever needed.
922 retval = Jim_GetLong(interp, argv[6], &l);
923 if (retval != JIM_OK) {
928 /* NOTE: parameters reordered! */
929 // ARMV4_5_MCR(cpnum, op1, 0, CRn, CRm, op2)
930 retval = arm->mcr(target, cpnum, op1, op2, CRn, CRm, value);
931 if (retval != ERROR_OK)
934 /* NOTE: parameters reordered! */
935 // ARMV4_5_MRC(cpnum, op1, 0, CRn, CRm, op2)
936 retval = arm->mrc(target, cpnum, op1, op2, CRn, CRm, &value);
937 if (retval != ERROR_OK)
940 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
946 COMMAND_HANDLER(handle_arm_semihosting_command)
948 struct target *target = get_current_target(CMD_CTX);
949 struct arm *arm = target ? target_to_arm(target) : NULL;
952 command_print(CMD_CTX, "current target isn't an ARM");
956 if (!arm->setup_semihosting)
958 command_print(CMD_CTX, "semihosting not supported for current target");
965 COMMAND_PARSE_ENABLE(CMD_ARGV[0], semihosting);
967 if (!target_was_examined(target))
969 LOG_ERROR("Target not examined yet");
973 if (arm->setup_semihosting(target, semihosting) != ERROR_OK) {
974 LOG_ERROR("Failed to Configure semihosting");
978 /* FIXME never let that "catch" be dropped! */
979 arm->is_semihosting = semihosting;
982 command_print(CMD_CTX, "semihosting is %s",
984 ? "enabled" : "disabled");
989 static const struct command_registration arm_exec_command_handlers[] = {
992 .handler = handle_armv4_5_reg_command,
993 .mode = COMMAND_EXEC,
994 .help = "display ARM core registers",
997 .name = "core_state",
998 .handler = handle_armv4_5_core_state_command,
999 .mode = COMMAND_EXEC,
1000 .usage = "['arm'|'thumb']",
1001 .help = "display/change ARM core state",
1004 .name = "disassemble",
1005 .handler = handle_arm_disassemble_command,
1006 .mode = COMMAND_EXEC,
1007 .usage = "address [count ['thumb']]",
1008 .help = "disassemble instructions ",
1012 .mode = COMMAND_EXEC,
1013 .jim_handler = &jim_mcrmrc,
1014 .help = "write coprocessor register",
1015 .usage = "cpnum op1 CRn op2 CRm value",
1019 .jim_handler = &jim_mcrmrc,
1020 .help = "read coprocessor register",
1021 .usage = "cpnum op1 CRn op2 CRm",
1025 .handler = handle_arm_semihosting_command,
1026 .mode = COMMAND_EXEC,
1027 .usage = "['enable'|'disable']",
1028 .help = "activate support for semihosting operations",
1031 COMMAND_REGISTRATION_DONE
1033 const struct command_registration arm_command_handlers[] = {
1036 .mode = COMMAND_ANY,
1037 .help = "ARM command group",
1038 .chain = arm_exec_command_handlers,
1040 COMMAND_REGISTRATION_DONE
1043 int arm_get_gdb_reg_list(struct target *target,
1044 struct reg **reg_list[], int *reg_list_size)
1046 struct arm *armv4_5 = target_to_arm(target);
1049 if (!is_arm_mode(armv4_5->core_mode))
1051 LOG_ERROR("not a valid arm core mode - communication failure?");
1055 *reg_list_size = 26;
1056 *reg_list = malloc(sizeof(struct reg*) * (*reg_list_size));
1058 for (i = 0; i < 16; i++)
1059 (*reg_list)[i] = arm_reg_current(armv4_5, i);
1061 for (i = 16; i < 24; i++)
1062 (*reg_list)[i] = &arm_gdb_dummy_fp_reg;
1064 (*reg_list)[24] = &arm_gdb_dummy_fps_reg;
1065 (*reg_list)[25] = armv4_5->cpsr;
1070 /* wait for execution to complete and check exit point */
1071 static int armv4_5_run_algorithm_completion(struct target *target, uint32_t exit_point, int timeout_ms, void *arch_info)
1074 struct arm *armv4_5 = target_to_arm(target);
1076 if ((retval = target_wait_state(target, TARGET_HALTED, timeout_ms)) != ERROR_OK)
1080 if (target->state != TARGET_HALTED)
1082 if ((retval = target_halt(target)) != ERROR_OK)
1084 if ((retval = target_wait_state(target, TARGET_HALTED, 500)) != ERROR_OK)
1088 return ERROR_TARGET_TIMEOUT;
1091 /* fast exit: ARMv5+ code can use BKPT */
1092 if (exit_point && buf_get_u32(armv4_5->pc->value, 0, 32) != exit_point)
1094 LOG_WARNING("target reentered debug state, but not at the desired exit point: 0x%4.4" PRIx32 "",
1095 buf_get_u32(armv4_5->pc->value, 0, 32));
1096 return ERROR_TARGET_TIMEOUT;
1102 int armv4_5_run_algorithm_inner(struct target *target,
1103 int num_mem_params, struct mem_param *mem_params,
1104 int num_reg_params, struct reg_param *reg_params,
1105 uint32_t entry_point, uint32_t exit_point,
1106 int timeout_ms, void *arch_info,
1107 int (*run_it)(struct target *target, uint32_t exit_point,
1108 int timeout_ms, void *arch_info))
1110 struct arm *armv4_5 = target_to_arm(target);
1111 struct arm_algorithm *arm_algorithm_info = arch_info;
1112 enum arm_state core_state = armv4_5->core_state;
1113 uint32_t context[17];
1115 int exit_breakpoint_size = 0;
1117 int retval = ERROR_OK;
1119 LOG_DEBUG("Running algorithm");
1121 if (arm_algorithm_info->common_magic != ARM_COMMON_MAGIC)
1123 LOG_ERROR("current target isn't an ARMV4/5 target");
1124 return ERROR_TARGET_INVALID;
1127 if (target->state != TARGET_HALTED)
1129 LOG_WARNING("target not halted");
1130 return ERROR_TARGET_NOT_HALTED;
1133 if (!is_arm_mode(armv4_5->core_mode))
1135 LOG_ERROR("not a valid arm core mode - communication failure?");
1139 /* armv5 and later can terminate with BKPT instruction; less overhead */
1140 if (!exit_point && armv4_5->is_armv4)
1142 LOG_ERROR("ARMv4 target needs HW breakpoint location");
1146 /* save r0..pc, cpsr-or-spsr, and then cpsr-for-sure;
1147 * they'll be restored later.
1149 for (i = 0; i <= 16; i++)
1153 r = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache,
1154 arm_algorithm_info->core_mode, i);
1156 armv4_5->read_core_reg(target, r, i,
1157 arm_algorithm_info->core_mode);
1158 context[i] = buf_get_u32(r->value, 0, 32);
1160 cpsr = buf_get_u32(armv4_5->cpsr->value, 0, 32);
1162 for (i = 0; i < num_mem_params; i++)
1164 if ((retval = target_write_buffer(target, mem_params[i].address, mem_params[i].size, mem_params[i].value)) != ERROR_OK)
1170 for (i = 0; i < num_reg_params; i++)
1172 struct reg *reg = register_get_by_name(armv4_5->core_cache, reg_params[i].reg_name, 0);
1175 LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1176 return ERROR_INVALID_ARGUMENTS;
1179 if (reg->size != reg_params[i].size)
1181 LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size", reg_params[i].reg_name);
1182 return ERROR_INVALID_ARGUMENTS;
1185 if ((retval = armv4_5_set_core_reg(reg, reg_params[i].value)) != ERROR_OK)
1191 armv4_5->core_state = arm_algorithm_info->core_state;
1192 if (armv4_5->core_state == ARM_STATE_ARM)
1193 exit_breakpoint_size = 4;
1194 else if (armv4_5->core_state == ARM_STATE_THUMB)
1195 exit_breakpoint_size = 2;
1198 LOG_ERROR("BUG: can't execute algorithms when not in ARM or Thumb state");
1199 return ERROR_INVALID_ARGUMENTS;
1202 if (arm_algorithm_info->core_mode != ARM_MODE_ANY)
1204 LOG_DEBUG("setting core_mode: 0x%2.2x",
1205 arm_algorithm_info->core_mode);
1206 buf_set_u32(armv4_5->cpsr->value, 0, 5,
1207 arm_algorithm_info->core_mode);
1208 armv4_5->cpsr->dirty = 1;
1209 armv4_5->cpsr->valid = 1;
1212 /* terminate using a hardware or (ARMv5+) software breakpoint */
1213 if (exit_point && (retval = breakpoint_add(target, exit_point,
1214 exit_breakpoint_size, BKPT_HARD)) != ERROR_OK)
1216 LOG_ERROR("can't add HW breakpoint to terminate algorithm");
1217 return ERROR_TARGET_FAILURE;
1220 if ((retval = target_resume(target, 0, entry_point, 1, 1)) != ERROR_OK)
1225 retval = run_it(target, exit_point, timeout_ms, arch_info);
1228 breakpoint_remove(target, exit_point);
1230 if (retval != ERROR_OK)
1233 for (i = 0; i < num_mem_params; i++)
1235 if (mem_params[i].direction != PARAM_OUT)
1236 if ((retvaltemp = target_read_buffer(target, mem_params[i].address, mem_params[i].size, mem_params[i].value)) != ERROR_OK)
1238 retval = retvaltemp;
1242 for (i = 0; i < num_reg_params; i++)
1244 if (reg_params[i].direction != PARAM_OUT)
1247 struct reg *reg = register_get_by_name(armv4_5->core_cache, reg_params[i].reg_name, 0);
1250 LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1251 retval = ERROR_INVALID_ARGUMENTS;
1255 if (reg->size != reg_params[i].size)
1257 LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size", reg_params[i].reg_name);
1258 retval = ERROR_INVALID_ARGUMENTS;
1262 buf_set_u32(reg_params[i].value, 0, 32, buf_get_u32(reg->value, 0, 32));
1266 /* restore everything we saved before (17 or 18 registers) */
1267 for (i = 0; i <= 16; i++)
1270 regvalue = buf_get_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, arm_algorithm_info->core_mode, i).value, 0, 32);
1271 if (regvalue != context[i])
1273 LOG_DEBUG("restoring register %s with value 0x%8.8" PRIx32 "", ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, arm_algorithm_info->core_mode, i).name, context[i]);
1274 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, arm_algorithm_info->core_mode, i).value, 0, 32, context[i]);
1275 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, arm_algorithm_info->core_mode, i).valid = 1;
1276 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, arm_algorithm_info->core_mode, i).dirty = 1;
1280 arm_set_cpsr(armv4_5, cpsr);
1281 armv4_5->cpsr->dirty = 1;
1283 armv4_5->core_state = core_state;
1288 int armv4_5_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
1290 return armv4_5_run_algorithm_inner(target, num_mem_params, mem_params, num_reg_params, reg_params, entry_point, exit_point, timeout_ms, arch_info, armv4_5_run_algorithm_completion);
1294 * Runs ARM code in the target to calculate a CRC32 checksum.
1297 int arm_checksum_memory(struct target *target,
1298 uint32_t address, uint32_t count, uint32_t *checksum)
1300 struct working_area *crc_algorithm;
1301 struct arm_algorithm armv4_5_info;
1302 struct arm *armv4_5 = target_to_arm(target);
1303 struct reg_param reg_params[2];
1306 uint32_t exit_var = 0;
1308 /* see contib/loaders/checksum/armv4_5_crc.s for src */
1310 static const uint32_t arm_crc_code[] = {
1311 0xE1A02000, /* mov r2, r0 */
1312 0xE3E00000, /* mov r0, #0xffffffff */
1313 0xE1A03001, /* mov r3, r1 */
1314 0xE3A04000, /* mov r4, #0 */
1315 0xEA00000B, /* b ncomp */
1317 0xE7D21004, /* ldrb r1, [r2, r4] */
1318 0xE59F7030, /* ldr r7, CRC32XOR */
1319 0xE0200C01, /* eor r0, r0, r1, asl 24 */
1320 0xE3A05000, /* mov r5, #0 */
1322 0xE3500000, /* cmp r0, #0 */
1323 0xE1A06080, /* mov r6, r0, asl #1 */
1324 0xE2855001, /* add r5, r5, #1 */
1325 0xE1A00006, /* mov r0, r6 */
1326 0xB0260007, /* eorlt r0, r6, r7 */
1327 0xE3550008, /* cmp r5, #8 */
1328 0x1AFFFFF8, /* bne loop */
1329 0xE2844001, /* add r4, r4, #1 */
1331 0xE1540003, /* cmp r4, r3 */
1332 0x1AFFFFF1, /* bne nbyte */
1334 0xe1200070, /* bkpt #0 */
1336 0x04C11DB7 /* .word 0x04C11DB7 */
1339 retval = target_alloc_working_area(target,
1340 sizeof(arm_crc_code), &crc_algorithm);
1341 if (retval != ERROR_OK)
1344 /* convert code into a buffer in target endianness */
1345 for (i = 0; i < ARRAY_SIZE(arm_crc_code); i++) {
1346 retval = target_write_u32(target,
1347 crc_algorithm->address + i * sizeof(uint32_t),
1349 if (retval != ERROR_OK)
1353 armv4_5_info.common_magic = ARM_COMMON_MAGIC;
1354 armv4_5_info.core_mode = ARM_MODE_SVC;
1355 armv4_5_info.core_state = ARM_STATE_ARM;
1357 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT);
1358 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
1360 buf_set_u32(reg_params[0].value, 0, 32, address);
1361 buf_set_u32(reg_params[1].value, 0, 32, count);
1363 /* 20 second timeout/megabyte */
1364 int timeout = 20000 * (1 + (count / (1024 * 1024)));
1366 /* armv4 must exit using a hardware breakpoint */
1367 if (armv4_5->is_armv4)
1368 exit_var = crc_algorithm->address + sizeof(arm_crc_code) - 8;
1370 retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
1371 crc_algorithm->address,
1373 timeout, &armv4_5_info);
1374 if (retval != ERROR_OK) {
1375 LOG_ERROR("error executing ARM crc algorithm");
1376 destroy_reg_param(®_params[0]);
1377 destroy_reg_param(®_params[1]);
1378 target_free_working_area(target, crc_algorithm);
1382 *checksum = buf_get_u32(reg_params[0].value, 0, 32);
1384 destroy_reg_param(®_params[0]);
1385 destroy_reg_param(®_params[1]);
1387 target_free_working_area(target, crc_algorithm);
1393 * Runs ARM code in the target to check whether a memory block holds
1394 * all ones. NOR flash which has been erased, and thus may be written,
1398 int arm_blank_check_memory(struct target *target,
1399 uint32_t address, uint32_t count, uint32_t *blank)
1401 struct working_area *check_algorithm;
1402 struct reg_param reg_params[3];
1403 struct arm_algorithm armv4_5_info;
1404 struct arm *armv4_5 = target_to_arm(target);
1407 uint32_t exit_var = 0;
1409 static const uint32_t check_code[] = {
1411 0xe4d03001, /* ldrb r3, [r0], #1 */
1412 0xe0022003, /* and r2, r2, r3 */
1413 0xe2511001, /* subs r1, r1, #1 */
1414 0x1afffffb, /* bne loop */
1416 0xe1200070, /* bkpt #0 */
1419 /* make sure we have a working area */
1420 retval = target_alloc_working_area(target,
1421 sizeof(check_code), &check_algorithm);
1422 if (retval != ERROR_OK)
1425 /* convert code into a buffer in target endianness */
1426 for (i = 0; i < ARRAY_SIZE(check_code); i++) {
1427 retval = target_write_u32(target,
1428 check_algorithm->address
1429 + i * sizeof(uint32_t),
1431 if (retval != ERROR_OK)
1435 armv4_5_info.common_magic = ARM_COMMON_MAGIC;
1436 armv4_5_info.core_mode = ARM_MODE_SVC;
1437 armv4_5_info.core_state = ARM_STATE_ARM;
1439 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
1440 buf_set_u32(reg_params[0].value, 0, 32, address);
1442 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
1443 buf_set_u32(reg_params[1].value, 0, 32, count);
1445 init_reg_param(®_params[2], "r2", 32, PARAM_IN_OUT);
1446 buf_set_u32(reg_params[2].value, 0, 32, 0xff);
1448 /* armv4 must exit using a hardware breakpoint */
1449 if (armv4_5->is_armv4)
1450 exit_var = check_algorithm->address + sizeof(check_code) - 4;
1452 retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
1453 check_algorithm->address,
1455 10000, &armv4_5_info);
1456 if (retval != ERROR_OK) {
1457 destroy_reg_param(®_params[0]);
1458 destroy_reg_param(®_params[1]);
1459 destroy_reg_param(®_params[2]);
1460 target_free_working_area(target, check_algorithm);
1464 *blank = buf_get_u32(reg_params[2].value, 0, 32);
1466 destroy_reg_param(®_params[0]);
1467 destroy_reg_param(®_params[1]);
1468 destroy_reg_param(®_params[2]);
1470 target_free_working_area(target, check_algorithm);
1475 static int arm_full_context(struct target *target)
1477 struct arm *armv4_5 = target_to_arm(target);
1478 unsigned num_regs = armv4_5->core_cache->num_regs;
1479 struct reg *reg = armv4_5->core_cache->reg_list;
1480 int retval = ERROR_OK;
1482 for (; num_regs && retval == ERROR_OK; num_regs--, reg++) {
1485 retval = armv4_5_get_core_reg(reg);
1490 static int arm_default_mrc(struct target *target, int cpnum,
1491 uint32_t op1, uint32_t op2,
1492 uint32_t CRn, uint32_t CRm,
1495 LOG_ERROR("%s doesn't implement MRC", target_type_name(target));
1499 static int arm_default_mcr(struct target *target, int cpnum,
1500 uint32_t op1, uint32_t op2,
1501 uint32_t CRn, uint32_t CRm,
1504 LOG_ERROR("%s doesn't implement MCR", target_type_name(target));
1508 int arm_init_arch_info(struct target *target, struct arm *armv4_5)
1510 target->arch_info = armv4_5;
1511 armv4_5->target = target;
1513 armv4_5->common_magic = ARM_COMMON_MAGIC;
1515 /* core_type may be overridden by subtype logic */
1516 if (armv4_5->core_type != ARM_MODE_THREAD) {
1517 armv4_5->core_type = ARM_MODE_ANY;
1518 arm_set_cpsr(armv4_5, ARM_MODE_USR);
1521 /* default full_context() has no core-specific optimizations */
1522 if (!armv4_5->full_context && armv4_5->read_core_reg)
1523 armv4_5->full_context = arm_full_context;
1526 armv4_5->mrc = arm_default_mrc;
1528 armv4_5->mcr = arm_default_mcr;