2 * Copyright (C) 2009 by David Brownell
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "armv4_5.h" /* REVISIT to become arm.h */
32 * Implements various ARM DPM operations using architectural debug registers.
33 * These routines layer over core-specific communication methods to cope with
34 * implementation differences between cores like ARM1136 and Cortex-A8.
37 /* Toggles between recorded core mode (USR, SVC, etc) and a temporary one.
38 * Routines *must* restore the original mode before returning!!
40 static int dpm_modeswitch(struct arm_dpm *dpm, enum armv4_5_mode mode)
45 /* restore previous mode */
46 if (mode == ARMV4_5_MODE_ANY)
47 cpsr = buf_get_u32(dpm->arm->cpsr->value, 0, 32);
49 /* else force to the specified mode */
53 retval = dpm->instr_write_data_r0(dpm, ARMV4_5_MSR_GP(0, 0xf, 0), cpsr);
55 /* REVISIT on Cortex-A8, we need a Prefetch Flush operation too ...
56 cortex_a8_exec_opcode(target,
57 ARMV4_5_MCR(15, 0, 0, 7, 5, 4));
63 /* just read the register -- rely on the core mode being right */
64 static int dpm_read_reg(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
71 /* return via DCC: "MCR p14, 0, Rnum, c0, c5, 0" */
72 retval = dpm->instr_read_data_dcc(dpm,
73 ARMV4_5_MCR(14, 0, regnum, 0, 5, 0),
77 /* "MOV r0, pc"; then return via DCC */
78 retval = dpm->instr_read_data_r0(dpm, 0xe1a0000f, &value);
80 /* NOTE: this seems like a slightly awkward place to update
81 * this value ... but if the PC gets written (the only way
82 * to change what we compute), the arch spec says subsequent
83 * reads return values which are "unpredictable". So this
84 * is always right except in those broken-by-intent cases.
86 switch (dpm->arm->core_state) {
87 case ARMV4_5_STATE_ARM:
90 case ARMV4_5_STATE_THUMB:
91 case ARM_STATE_THUMB_EE:
94 case ARMV4_5_STATE_JAZELLE:
95 /* core-specific ... ? */
96 LOG_WARNING("Jazelle PC adjustment unknown");
101 /* 16: "MRS r0, CPSR"; then return via DCC
102 * 17: "MRS r0, SPSR"; then return via DCC
104 retval = dpm->instr_read_data_r0(dpm,
105 ARMV4_5_MRS(0, regnum & 1),
110 if (retval == ERROR_OK) {
111 buf_set_u32(r->value, 0, 32, value);
114 LOG_DEBUG("READ: %s, %8.8x", r->name, (unsigned) value);
120 /* just write the register -- rely on the core mode being right */
121 static int dpm_write_reg(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
124 uint32_t value = buf_get_u32(r->value, 0, 32);
128 /* load register from DCC: "MCR p14, 0, Rnum, c0, c5, 0" */
129 retval = dpm->instr_write_data_dcc(dpm,
130 ARMV4_5_MRC(14, 0, regnum, 0, 5, 0),
134 /* read r0 from DCC; then "MOV pc, r0" */
135 retval = dpm->instr_write_data_r0(dpm, 0xe1a0f000, value);
138 /* 16: read r0 from DCC, then "MSR r0, CPSR_cxsf"
139 * 17: read r0 from DCC, then "MSR r0, SPSR_cxsf"
141 retval = dpm->instr_write_data_r0(dpm,
142 ARMV4_5_MSR_GP(0, 0xf, regnum & 1),
145 /* REVISIT on Cortex-A8, we need a Prefetch Flush operation
146 * after writing CPSR ...
147 cortex_a8_exec_opcode(target,
148 ARMV4_5_MCR(15, 0, 0, 7, 5, 4));
154 if (retval == ERROR_OK) {
156 LOG_DEBUG("WRITE: %s, %8.8x", r->name, (unsigned) value);
163 * Read basic registers of the the current context: R0 to R15, and CPSR;
164 * sets the core mode (such as USR or IRQ) and state (such as ARM or Thumb).
165 * In normal operation this is called on entry to halting debug state,
166 * possibly after some other operations supporting restore of debug state
167 * or making sure the CPU is fully idle (drain write buffer, etc).
169 int arm_dpm_read_current_registers(struct arm_dpm *dpm)
171 struct arm *arm = dpm->arm;
176 retval = dpm->prepare(dpm);
177 if (retval != ERROR_OK)
180 /* read R0 first (it's used for scratch), then CPSR */
181 r = arm->core_cache->reg_list + 0;
183 retval = dpm_read_reg(dpm, r, 0);
184 if (retval != ERROR_OK)
189 retval = dpm->instr_read_data_r0(dpm, ARMV4_5_MRS(0, 0), &cpsr);
190 if (retval != ERROR_OK)
193 /* update core mode and state, plus shadow mapping for R8..R14 */
194 arm_set_cpsr(arm, cpsr);
196 /* REVISIT we can probably avoid reading R1..R14, saving time... */
197 for (unsigned i = 1; i < 16; i++) {
198 r = arm_reg_current(arm, i);
202 retval = dpm_read_reg(dpm, r, i);
203 if (retval != ERROR_OK)
207 /* NOTE: SPSR ignored (if it's even relevant). */
210 /* (void) */ dpm->finish(dpm);
215 * Writes all modified core registers for all processor modes. In normal
216 * operation this is called on exit from halting debug state.
218 int arm_dpm_write_dirty_registers(struct arm_dpm *dpm)
220 struct arm *arm = dpm->arm;
221 struct reg_cache *cache = arm->core_cache;
225 retval = dpm->prepare(dpm);
226 if (retval != ERROR_OK)
229 /* Scan the registers until we find one that's both dirty and
230 * eligible for flushing. Flush that and everything else that
231 * shares the same core mode setting. Typically this won't
232 * actually find anything to do...
235 enum armv4_5_mode mode = ARMV4_5_MODE_ANY;
239 /* check everything except our scratch register R0 */
240 for (unsigned i = 1; i < cache->num_regs; i++) {
244 /* also skip PC, CPSR, and non-dirty */
247 if (arm->cpsr == cache->reg_list + i)
249 if (!cache->reg_list[i].dirty)
252 r = cache->reg_list[i].arch_info;
255 /* may need to pick and set a mode */
257 enum armv4_5_mode tmode;
260 mode = tmode = r->mode;
262 /* cope with special cases */
265 /* r8..r12 "anything but FIQ" case;
266 * we "know" core mode is accurate
267 * since we haven't changed it yet
269 if (arm->core_mode == ARMV4_5_MODE_FIQ
272 tmode = ARMV4_5_MODE_USR;
280 /* REVISIT error checks */
281 if (tmode != ARMV4_5_MODE_ANY)
282 retval = dpm_modeswitch(dpm, tmode);
287 retval = dpm_write_reg(dpm,
295 /* Restore original CPSR ... assuming either that we changed it,
296 * or it's dirty. Must write PC to ensure the return address is
297 * defined, and must not write it before CPSR.
299 retval = dpm_modeswitch(dpm, ARMV4_5_MODE_ANY);
300 arm->cpsr->dirty = false;
302 retval = dpm_write_reg(dpm, &cache->reg_list[15], 15);
303 cache->reg_list[15].dirty = false;
305 /* flush R0 -- it's *very* dirty by now */
306 retval = dpm_write_reg(dpm, &cache->reg_list[0], 0);
307 cache->reg_list[0].dirty = false;
309 /* (void) */ dpm->finish(dpm);
314 /* Returns ARMV4_5_MODE_ANY or temporary mode to use while reading the
315 * specified register ... works around flakiness from ARM core calls.
316 * Caller already filtered out SPSR access; mode is never MODE_SYS
319 static enum armv4_5_mode dpm_mapmode(struct arm *arm,
320 unsigned num, enum armv4_5_mode mode)
322 enum armv4_5_mode amode = arm->core_mode;
324 /* don't switch if the mode is already correct */
325 if (amode == ARMV4_5_MODE_SYS)
326 amode = ARMV4_5_MODE_USR;
328 return ARMV4_5_MODE_ANY;
331 /* don't switch for non-shadowed registers (r0..r7, r15/pc, cpsr) */
336 /* r8..r12 aren't shadowed for anything except FIQ */
338 if (mode == ARMV4_5_MODE_FIQ)
341 /* r13/sp, and r14/lr are always shadowed */
346 LOG_WARNING("invalid register #%u", num);
349 return ARMV4_5_MODE_ANY;
352 static int arm_dpm_read_core_reg(struct target *target, struct reg *r,
353 int regnum, enum armv4_5_mode mode)
355 struct arm_dpm *dpm = target_to_arm(target)->dpm;
358 if (regnum < 0 || regnum > 16)
359 return ERROR_INVALID_ARGUMENTS;
362 if (mode != ARMV4_5_MODE_ANY)
365 mode = dpm_mapmode(dpm->arm, regnum, mode);
367 /* REVISIT what happens if we try to read SPSR in a core mode
368 * which has no such register?
371 retval = dpm->prepare(dpm);
372 if (retval != ERROR_OK)
375 if (mode != ARMV4_5_MODE_ANY) {
376 retval = dpm_modeswitch(dpm, mode);
377 if (retval != ERROR_OK)
381 retval = dpm_read_reg(dpm, r, regnum);
382 /* always clean up, regardless of error */
384 if (mode != ARMV4_5_MODE_ANY)
385 /* (void) */ dpm_modeswitch(dpm, ARMV4_5_MODE_ANY);
388 /* (void) */ dpm->finish(dpm);
392 static int arm_dpm_write_core_reg(struct target *target, struct reg *r,
393 int regnum, enum armv4_5_mode mode, uint32_t value)
395 struct arm_dpm *dpm = target_to_arm(target)->dpm;
399 if (regnum < 0 || regnum > 16)
400 return ERROR_INVALID_ARGUMENTS;
403 if (mode != ARMV4_5_MODE_ANY)
406 mode = dpm_mapmode(dpm->arm, regnum, mode);
408 /* REVISIT what happens if we try to write SPSR in a core mode
409 * which has no such register?
412 retval = dpm->prepare(dpm);
413 if (retval != ERROR_OK)
416 if (mode != ARMV4_5_MODE_ANY) {
417 retval = dpm_modeswitch(dpm, mode);
418 if (retval != ERROR_OK)
422 retval = dpm_write_reg(dpm, r, regnum);
423 /* always clean up, regardless of error */
425 if (mode != ARMV4_5_MODE_ANY)
426 /* (void) */ dpm_modeswitch(dpm, ARMV4_5_MODE_ANY);
429 /* (void) */ dpm->finish(dpm);
433 static int arm_dpm_full_context(struct target *target)
435 struct arm *arm = target_to_arm(target);
436 struct arm_dpm *dpm = arm->dpm;
437 struct reg_cache *cache = arm->core_cache;
441 retval = dpm->prepare(dpm);
442 if (retval != ERROR_OK)
446 enum armv4_5_mode mode = ARMV4_5_MODE_ANY;
450 /* We "know" arm_dpm_read_current_registers() was called so
451 * the unmapped registers (R0..R7, PC, AND CPSR) and some
452 * view of R8..R14 are current. We also "know" oddities of
453 * register mapping: special cases for R8..R12 and SPSR.
455 * Pick some mode with unread registers and read them all.
458 for (unsigned i = 0; i < cache->num_regs; i++) {
461 if (cache->reg_list[i].valid)
463 r = cache->reg_list[i].arch_info;
465 /* may need to pick a mode and set CPSR */
470 /* For R8..R12 when we've entered debug
471 * state in FIQ mode... patch mode.
473 if (mode == ARMV4_5_MODE_ANY)
474 mode = ARMV4_5_MODE_USR;
476 /* REVISIT error checks */
477 retval = dpm_modeswitch(dpm, mode);
482 /* CPSR was read, so "R16" must mean SPSR */
483 retval = dpm_read_reg(dpm,
485 (r->num == 16) ? 17 : r->num);
491 retval = dpm_modeswitch(dpm, ARMV4_5_MODE_ANY);
492 /* (void) */ dpm->finish(dpm);
498 * Hooks up this DPM to its associated target; call only once.
499 * Initially this only covers the register cache.
501 int arm_dpm_setup(struct arm_dpm *dpm)
503 struct arm *arm = dpm->arm;
504 struct target *target = arm->target;
505 struct reg_cache *cache;
509 arm->full_context = arm_dpm_full_context;
510 arm->read_core_reg = arm_dpm_read_core_reg;
511 arm->write_core_reg = arm_dpm_write_core_reg;
513 cache = armv4_5_build_reg_cache(target, arm);
517 *register_get_last_cache_p(&target->reg_cache) = cache;
522 * Reinitializes DPM state at the beginning of a new debug session
523 * or after a reset which may have affected the debug module.
525 int arm_dpm_initialize(struct arm_dpm *dpm)
527 /* FIXME -- nothing yet */