OpenOCD
armv4_5.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2008 by Spencer Oliver *
8  * spen@spen-soft.co.uk *
9  * *
10  * Copyright (C) 2008 by Oyvind Harboe *
11  * oyvind.harboe@zylin.com *
12  * *
13  * Copyright (C) 2018 by Liviu Ionescu *
14  * <ilg@livius.net> *
15  ***************************************************************************/
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "arm.h"
22 #include "armv4_5.h"
23 #include "arm_jtag.h"
24 #include "breakpoints.h"
25 #include "arm_disassembler.h"
26 #include <helper/binarybuffer.h>
27 #include "algorithm.h"
28 #include "register.h"
29 #include "semihosting_common.h"
30 
31 /* offsets into armv4_5 core register cache */
32 enum {
33 /* ARMV4_5_CPSR = 31, */
41 };
42 
43 static const uint8_t arm_usr_indices[17] = {
44  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ARMV4_5_CPSR,
45 };
46 
47 static const uint8_t arm_fiq_indices[8] = {
48  16, 17, 18, 19, 20, 21, 22, ARMV4_5_SPSR_FIQ,
49 };
50 
51 static const uint8_t arm_irq_indices[3] = {
52  23, 24, ARMV4_5_SPSR_IRQ,
53 };
54 
55 static const uint8_t arm_svc_indices[3] = {
56  25, 26, ARMV4_5_SPSR_SVC,
57 };
58 
59 static const uint8_t arm_abt_indices[3] = {
60  27, 28, ARMV4_5_SPSR_ABT,
61 };
62 
63 static const uint8_t arm_und_indices[3] = {
64  29, 30, ARMV4_5_SPSR_UND,
65 };
66 
67 static const uint8_t arm_mon_indices[3] = {
68  39, 40, ARM_SPSR_MON,
69 };
70 
71 static const uint8_t arm_hyp_indices[2] = {
72  42, ARM_SPSR_HYP,
73 };
74 
75 static const struct {
76  const char *name;
77  unsigned short psr;
78  /* For user and system modes, these list indices for all registers.
79  * otherwise they're just indices for the shadow registers and SPSR.
80  */
81  unsigned short n_indices;
82  const uint8_t *indices;
83 } arm_mode_data[] = {
84  /* Seven modes are standard from ARM7 on. "System" and "User" share
85  * the same registers; other modes shadow from 3 to 8 registers.
86  */
87  {
88  .name = "User",
89  .psr = ARM_MODE_USR,
90  .n_indices = ARRAY_SIZE(arm_usr_indices),
91  .indices = arm_usr_indices,
92  },
93  {
94  .name = "FIQ",
95  .psr = ARM_MODE_FIQ,
96  .n_indices = ARRAY_SIZE(arm_fiq_indices),
97  .indices = arm_fiq_indices,
98  },
99  {
100  .name = "Supervisor",
101  .psr = ARM_MODE_SVC,
102  .n_indices = ARRAY_SIZE(arm_svc_indices),
103  .indices = arm_svc_indices,
104  },
105  {
106  .name = "Abort",
107  .psr = ARM_MODE_ABT,
108  .n_indices = ARRAY_SIZE(arm_abt_indices),
109  .indices = arm_abt_indices,
110  },
111  {
112  .name = "IRQ",
113  .psr = ARM_MODE_IRQ,
114  .n_indices = ARRAY_SIZE(arm_irq_indices),
115  .indices = arm_irq_indices,
116  },
117  {
118  .name = "Undefined instruction",
119  .psr = ARM_MODE_UND,
120  .n_indices = ARRAY_SIZE(arm_und_indices),
121  .indices = arm_und_indices,
122  },
123  {
124  .name = "System",
125  .psr = ARM_MODE_SYS,
126  .n_indices = ARRAY_SIZE(arm_usr_indices),
127  .indices = arm_usr_indices,
128  },
129  /* TrustZone "Security Extensions" add a secure monitor mode.
130  * This is distinct from a "debug monitor" which can support
131  * non-halting debug, in conjunction with some debuggers.
132  */
133  {
134  .name = "Secure Monitor",
135  .psr = ARM_MODE_MON,
136  .n_indices = ARRAY_SIZE(arm_mon_indices),
137  .indices = arm_mon_indices,
138  },
139  {
140  .name = "Secure Monitor ARM1176JZF-S",
141  .psr = ARM_MODE_1176_MON,
142  .n_indices = ARRAY_SIZE(arm_mon_indices),
143  .indices = arm_mon_indices,
144  },
145 
146  /* These special modes are currently only supported
147  * by ARMv6M and ARMv7M profiles */
148  {
149  .name = "Thread",
150  .psr = ARM_MODE_THREAD,
151  },
152  {
153  .name = "Thread (User)",
154  .psr = ARM_MODE_USER_THREAD,
155  },
156  {
157  .name = "Handler",
158  .psr = ARM_MODE_HANDLER,
159  },
160 
161  /* armv7-a with virtualization extension */
162  {
163  .name = "Hypervisor",
164  .psr = ARM_MODE_HYP,
165  .n_indices = ARRAY_SIZE(arm_hyp_indices),
166  .indices = arm_hyp_indices,
167  },
168 };
169 
171 const char *arm_mode_name(unsigned int psr_mode)
172 {
173  for (unsigned int i = 0; i < ARRAY_SIZE(arm_mode_data); i++) {
174  if (arm_mode_data[i].psr == psr_mode)
175  return arm_mode_data[i].name;
176  }
177  LOG_ERROR("unrecognized psr mode: %#02x", psr_mode);
178  return "UNRECOGNIZED";
179 }
180 
182 bool is_arm_mode(unsigned int psr_mode)
183 {
184  for (unsigned int i = 0; i < ARRAY_SIZE(arm_mode_data); i++) {
185  if (arm_mode_data[i].psr == psr_mode)
186  return true;
187  }
188  return false;
189 }
190 
193 {
194  switch (mode) {
195  case ARM_MODE_ANY:
196  /* map MODE_ANY to user mode */
197  case ARM_MODE_USR:
198  return 0;
199  case ARM_MODE_FIQ:
200  return 1;
201  case ARM_MODE_IRQ:
202  return 2;
203  case ARM_MODE_SVC:
204  return 3;
205  case ARM_MODE_ABT:
206  return 4;
207  case ARM_MODE_UND:
208  return 5;
209  case ARM_MODE_SYS:
210  return 6;
211  case ARM_MODE_MON:
212  case ARM_MODE_1176_MON:
213  return 7;
214  case ARM_MODE_HYP:
215  return 8;
216  default:
217  LOG_ERROR("invalid mode value encountered %d", mode);
218  return -1;
219  }
220 }
221 
224 {
225  switch (number) {
226  case 0:
227  return ARM_MODE_USR;
228  case 1:
229  return ARM_MODE_FIQ;
230  case 2:
231  return ARM_MODE_IRQ;
232  case 3:
233  return ARM_MODE_SVC;
234  case 4:
235  return ARM_MODE_ABT;
236  case 5:
237  return ARM_MODE_UND;
238  case 6:
239  return ARM_MODE_SYS;
240  case 7:
241  return ARM_MODE_MON;
242  case 8:
243  return ARM_MODE_HYP;
244  default:
245  LOG_ERROR("mode index out of bounds %d", number);
246  return ARM_MODE_ANY;
247  }
248 }
249 
250 static const char *arm_state_strings[] = {
251  [ARM_STATE_ARM] = "ARM",
252  [ARM_STATE_THUMB] = "Thumb",
253  [ARM_STATE_JAZELLE] = "Jazelle",
254  [ARM_STATE_THUMB_EE] = "ThumbEE",
255  [ARM_STATE_AARCH64] = "AArch64",
256 };
257 
258 /* Templates for ARM core registers.
259  *
260  * NOTE: offsets in this table are coupled to the arm_mode_data
261  * table above, the armv4_5_core_reg_map array below, and also to
262  * the ARMV4_5_CPSR symbol (which should vanish after ARM11 updates).
263  */
264 static const struct {
265  /* The name is used for e.g. the "regs" command. */
266  const char *name;
267 
268  /* The {cookie, mode} tuple uniquely identifies one register.
269  * In a given mode, cookies 0..15 map to registers R0..R15,
270  * with R13..R15 usually called SP, LR, PC.
271  *
272  * MODE_ANY is used as *input* to the mapping, and indicates
273  * various special cases (sigh) and errors.
274  *
275  * Cookie 16 is (currently) confusing, since it indicates
276  * CPSR -or- SPSR depending on whether 'mode' is MODE_ANY.
277  * (Exception modes have both CPSR and SPSR registers ...)
278  */
279  unsigned int cookie;
280  unsigned int gdb_index;
281  enum arm_mode mode;
282 } arm_core_regs[] = {
283  /* IMPORTANT: we guarantee that the first eight cached registers
284  * correspond to r0..r7, and the fifteenth to PC, so that callers
285  * don't need to map them.
286  */
287  [0] = { .name = "r0", .cookie = 0, .mode = ARM_MODE_ANY, .gdb_index = 0, },
288  [1] = { .name = "r1", .cookie = 1, .mode = ARM_MODE_ANY, .gdb_index = 1, },
289  [2] = { .name = "r2", .cookie = 2, .mode = ARM_MODE_ANY, .gdb_index = 2, },
290  [3] = { .name = "r3", .cookie = 3, .mode = ARM_MODE_ANY, .gdb_index = 3, },
291  [4] = { .name = "r4", .cookie = 4, .mode = ARM_MODE_ANY, .gdb_index = 4, },
292  [5] = { .name = "r5", .cookie = 5, .mode = ARM_MODE_ANY, .gdb_index = 5, },
293  [6] = { .name = "r6", .cookie = 6, .mode = ARM_MODE_ANY, .gdb_index = 6, },
294  [7] = { .name = "r7", .cookie = 7, .mode = ARM_MODE_ANY, .gdb_index = 7, },
295 
296  /* NOTE: regs 8..12 might be shadowed by FIQ ... flagging
297  * them as MODE_ANY creates special cases. (ANY means
298  * "not mapped" elsewhere; here it's "everything but FIQ".)
299  */
300  [8] = { .name = "r8", .cookie = 8, .mode = ARM_MODE_ANY, .gdb_index = 8, },
301  [9] = { .name = "r9", .cookie = 9, .mode = ARM_MODE_ANY, .gdb_index = 9, },
302  [10] = { .name = "r10", .cookie = 10, .mode = ARM_MODE_ANY, .gdb_index = 10, },
303  [11] = { .name = "r11", .cookie = 11, .mode = ARM_MODE_ANY, .gdb_index = 11, },
304  [12] = { .name = "r12", .cookie = 12, .mode = ARM_MODE_ANY, .gdb_index = 12, },
305 
306  /* Historical GDB mapping of indices:
307  * - 13-14 are sp and lr, but banked counterparts are used
308  * - 16-24 are left for deprecated 8 FPA + 1 FPS
309  * - 25 is the cpsr
310  */
311 
312  /* NOTE all MODE_USR registers are equivalent to MODE_SYS ones */
313  [13] = { .name = "sp_usr", .cookie = 13, .mode = ARM_MODE_USR, .gdb_index = 26, },
314  [14] = { .name = "lr_usr", .cookie = 14, .mode = ARM_MODE_USR, .gdb_index = 27, },
315 
316  /* guaranteed to be at index 15 */
317  [15] = { .name = "pc", .cookie = 15, .mode = ARM_MODE_ANY, .gdb_index = 15, },
318  [16] = { .name = "r8_fiq", .cookie = 8, .mode = ARM_MODE_FIQ, .gdb_index = 28, },
319  [17] = { .name = "r9_fiq", .cookie = 9, .mode = ARM_MODE_FIQ, .gdb_index = 29, },
320  [18] = { .name = "r10_fiq", .cookie = 10, .mode = ARM_MODE_FIQ, .gdb_index = 30, },
321  [19] = { .name = "r11_fiq", .cookie = 11, .mode = ARM_MODE_FIQ, .gdb_index = 31, },
322  [20] = { .name = "r12_fiq", .cookie = 12, .mode = ARM_MODE_FIQ, .gdb_index = 32, },
323 
324  [21] = { .name = "sp_fiq", .cookie = 13, .mode = ARM_MODE_FIQ, .gdb_index = 33, },
325  [22] = { .name = "lr_fiq", .cookie = 14, .mode = ARM_MODE_FIQ, .gdb_index = 34, },
326 
327  [23] = { .name = "sp_irq", .cookie = 13, .mode = ARM_MODE_IRQ, .gdb_index = 35, },
328  [24] = { .name = "lr_irq", .cookie = 14, .mode = ARM_MODE_IRQ, .gdb_index = 36, },
329 
330  [25] = { .name = "sp_svc", .cookie = 13, .mode = ARM_MODE_SVC, .gdb_index = 37, },
331  [26] = { .name = "lr_svc", .cookie = 14, .mode = ARM_MODE_SVC, .gdb_index = 38, },
332 
333  [27] = { .name = "sp_abt", .cookie = 13, .mode = ARM_MODE_ABT, .gdb_index = 39, },
334  [28] = { .name = "lr_abt", .cookie = 14, .mode = ARM_MODE_ABT, .gdb_index = 40, },
335 
336  [29] = { .name = "sp_und", .cookie = 13, .mode = ARM_MODE_UND, .gdb_index = 41, },
337  [30] = { .name = "lr_und", .cookie = 14, .mode = ARM_MODE_UND, .gdb_index = 42, },
338 
339  [31] = { .name = "cpsr", .cookie = 16, .mode = ARM_MODE_ANY, .gdb_index = 25, },
340  [32] = { .name = "spsr_fiq", .cookie = 16, .mode = ARM_MODE_FIQ, .gdb_index = 43, },
341  [33] = { .name = "spsr_irq", .cookie = 16, .mode = ARM_MODE_IRQ, .gdb_index = 44, },
342  [34] = { .name = "spsr_svc", .cookie = 16, .mode = ARM_MODE_SVC, .gdb_index = 45, },
343  [35] = { .name = "spsr_abt", .cookie = 16, .mode = ARM_MODE_ABT, .gdb_index = 46, },
344  [36] = { .name = "spsr_und", .cookie = 16, .mode = ARM_MODE_UND, .gdb_index = 47, },
345 
346  /* These are only used for GDB target description, banked registers are accessed instead */
347  [37] = { .name = "sp", .cookie = 13, .mode = ARM_MODE_ANY, .gdb_index = 13, },
348  [38] = { .name = "lr", .cookie = 14, .mode = ARM_MODE_ANY, .gdb_index = 14, },
349 
350  /* These exist only when the Security Extension (TrustZone) is present */
351  [39] = { .name = "sp_mon", .cookie = 13, .mode = ARM_MODE_MON, .gdb_index = 48, },
352  [40] = { .name = "lr_mon", .cookie = 14, .mode = ARM_MODE_MON, .gdb_index = 49, },
353  [41] = { .name = "spsr_mon", .cookie = 16, .mode = ARM_MODE_MON, .gdb_index = 50, },
354 
355  /* These exist only when the Virtualization Extensions is present */
356  [42] = { .name = "sp_hyp", .cookie = 13, .mode = ARM_MODE_HYP, .gdb_index = 51, },
357  [43] = { .name = "spsr_hyp", .cookie = 16, .mode = ARM_MODE_HYP, .gdb_index = 52, },
358  // .gdb_index numbering continues by ARM_VFP_V3_D0
359 };
360 
361 static const struct {
362  unsigned int id;
363  const char *name;
364  uint32_t bits;
365  enum arm_mode mode;
366  enum reg_type type;
367  const char *group;
368  const char *feature;
369 } arm_vfp_v3_regs[] = {
370  { ARM_VFP_V3_D0, "d0", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
371  { ARM_VFP_V3_D1, "d1", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
372  { ARM_VFP_V3_D2, "d2", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
373  { ARM_VFP_V3_D3, "d3", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
374  { ARM_VFP_V3_D4, "d4", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
375  { ARM_VFP_V3_D5, "d5", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
376  { ARM_VFP_V3_D6, "d6", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
377  { ARM_VFP_V3_D7, "d7", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
378  { ARM_VFP_V3_D8, "d8", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
379  { ARM_VFP_V3_D9, "d9", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
380  { ARM_VFP_V3_D10, "d10", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
381  { ARM_VFP_V3_D11, "d11", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
382  { ARM_VFP_V3_D12, "d12", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
383  { ARM_VFP_V3_D13, "d13", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
384  { ARM_VFP_V3_D14, "d14", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
385  { ARM_VFP_V3_D15, "d15", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
386  { ARM_VFP_V3_D16, "d16", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
387  { ARM_VFP_V3_D17, "d17", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
388  { ARM_VFP_V3_D18, "d18", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
389  { ARM_VFP_V3_D19, "d19", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
390  { ARM_VFP_V3_D20, "d20", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
391  { ARM_VFP_V3_D21, "d21", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
392  { ARM_VFP_V3_D22, "d22", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
393  { ARM_VFP_V3_D23, "d23", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
394  { ARM_VFP_V3_D24, "d24", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
395  { ARM_VFP_V3_D25, "d25", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
396  { ARM_VFP_V3_D26, "d26", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
397  { ARM_VFP_V3_D27, "d27", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
398  { ARM_VFP_V3_D28, "d28", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
399  { ARM_VFP_V3_D29, "d29", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
400  { ARM_VFP_V3_D30, "d30", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
401  { ARM_VFP_V3_D31, "d31", 64, ARM_MODE_ANY, REG_TYPE_IEEE_DOUBLE, NULL, "org.gnu.gdb.arm.vfp"},
402  { ARM_VFP_V3_FPSCR, "fpscr", 32, ARM_MODE_ANY, REG_TYPE_INT, "float", "org.gnu.gdb.arm.vfp"},
403 };
404 
405 /* map core mode (USR, FIQ, ...) and register number to
406  * indices into the register cache
407  */
408 const int armv4_5_core_reg_map[9][17] = {
409  { /* USR */
410  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31
411  },
412  { /* FIQ (8 shadows of USR, vs normal 3) */
413  0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 15, 32
414  },
415  { /* IRQ */
416  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 23, 24, 15, 33
417  },
418  { /* SVC */
419  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 25, 26, 15, 34
420  },
421  { /* ABT */
422  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 27, 28, 15, 35
423  },
424  { /* UND */
425  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 29, 30, 15, 36
426  },
427  { /* SYS (same registers as USR) */
428  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31
429  },
430  { /* MON */
431  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 39, 40, 15, 41,
432  },
433  { /* HYP */
434  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 42, 14, 15, 43,
435  }
436 };
437 
438 static const char *arm_core_state_string(struct arm *arm)
439 {
441  LOG_TARGET_ERROR(arm->target, "core_state exceeds table size");
442  return "Unknown";
443  }
444 
446 }
447 
453 void arm_set_cpsr(struct arm *arm, uint32_t cpsr)
454 {
455  enum arm_mode mode = cpsr & 0x1f;
456  int num;
457 
458  /* NOTE: this may be called very early, before the register
459  * cache is set up. We can't defend against many errors, in
460  * particular against CPSRs that aren't valid *here* ...
461  */
462  if (arm->cpsr) {
463  buf_set_u32(arm->cpsr->value, 0, 32, cpsr);
464  arm->cpsr->valid = true;
465  arm->cpsr->dirty = false;
466  }
467 
468  arm->core_mode = mode;
469 
470  /* mode_to_number() warned; set up a somewhat-sane mapping */
471  num = arm_mode_to_number(mode);
472  if (num < 0) {
473  mode = ARM_MODE_USR;
474  num = 0;
475  }
476 
477  arm->map = &armv4_5_core_reg_map[num][0];
478  arm->spsr = (mode == ARM_MODE_USR || mode == ARM_MODE_SYS)
479  ? NULL
480  : arm->core_cache->reg_list + arm->map[16];
481 
482  /* Older ARMs won't have the J bit */
483  enum arm_state state;
484 
485  if (cpsr & (1 << 5)) { /* T */
486  if (cpsr & (1 << 24)) { /* J */
487  LOG_TARGET_WARNING(arm->target, "ThumbEE -- incomplete support");
489  } else
491  } else {
492  if (cpsr & (1 << 24)) { /* J */
493  LOG_TARGET_ERROR(arm->target, "Jazelle state handling is broken");
495  } else
497  }
498  arm->core_state = state;
499 
500  LOG_TARGET_DEBUG(arm->target, "set CPSR %#8.8" PRIx32 ": %s mode, %s state", cpsr,
503 }
504 
517 struct reg *arm_reg_current(struct arm *arm, unsigned int regnum)
518 {
519  struct reg *r;
520 
521  if (regnum > 16)
522  return NULL;
523 
524  if (!arm->map) {
525  LOG_TARGET_ERROR(arm->target, "Register map is not available yet, the target is not fully initialised");
526  r = arm->core_cache->reg_list + regnum;
527  } else
528  r = arm->core_cache->reg_list + arm->map[regnum];
529 
530  /* e.g. invalid CPSR said "secure monitor" mode on a core
531  * that doesn't support it...
532  */
533  if (!r) {
534  LOG_TARGET_ERROR(arm->target, "Invalid CPSR mode");
535  r = arm->core_cache->reg_list + regnum;
536  }
537 
538  return r;
539 }
540 
541 static const uint8_t arm_gdb_dummy_fp_value[12];
542 
543 static struct reg_feature arm_gdb_dummy_fp_features = {
544  .name = "net.sourceforge.openocd.fake_fpa"
545 };
546 
553 static struct reg arm_gdb_dummy_fp_reg = {
554  .name = "GDB dummy FPA register",
555  .value = (uint8_t *) arm_gdb_dummy_fp_value,
556  .valid = true,
557  .size = 96,
558  .exist = false,
559  .number = 16,
561  .group = "fake_fpa",
562 };
563 
564 static const uint8_t arm_gdb_dummy_fps_value[4];
565 
570 static struct reg arm_gdb_dummy_fps_reg = {
571  .name = "GDB dummy FPA status register",
572  .value = (uint8_t *) arm_gdb_dummy_fps_value,
573  .valid = true,
574  .size = 32,
575  .exist = false,
576  .number = 24,
578  .group = "fake_fpa",
579 };
580 
581 static void arm_gdb_dummy_init(void) __attribute__ ((constructor));
582 
583 static void arm_gdb_dummy_init(void)
584 {
587 }
588 
589 static int armv4_5_get_core_reg(struct reg *reg)
590 {
591  int retval;
592  struct arm_reg *reg_arch_info = reg->arch_info;
593  struct target *target = reg_arch_info->target;
594 
595  if (target->state != TARGET_HALTED) {
596  LOG_TARGET_ERROR(target, "not halted");
598  }
599 
600  retval = reg_arch_info->arm->read_core_reg(target, reg,
601  reg_arch_info->num, reg_arch_info->mode);
602  if (retval == ERROR_OK) {
603  reg->valid = true;
604  reg->dirty = false;
605  }
606 
607  return retval;
608 }
609 
610 static int armv4_5_set_core_reg(struct reg *reg, uint8_t *buf)
611 {
612  struct arm_reg *reg_arch_info = reg->arch_info;
613  struct target *target = reg_arch_info->target;
614  struct arm *armv4_5_target = target_to_arm(target);
615  uint32_t value = buf_get_u32(buf, 0, 32);
616 
617  if (target->state != TARGET_HALTED) {
618  LOG_TARGET_ERROR(target, "not halted");
620  }
621 
622  /* Except for CPSR, the "reg" command exposes a writeback model
623  * for the register cache.
624  */
625  if (reg == armv4_5_target->cpsr) {
626  arm_set_cpsr(armv4_5_target, value);
627 
628  /* Older cores need help to be in ARM mode during halt
629  * mode debug, so we clear the J and T bits if we flush.
630  * For newer cores (v6/v7a/v7r) we don't need that, but
631  * it won't hurt since CPSR is always flushed anyway.
632  */
633  if (armv4_5_target->core_mode !=
634  (enum arm_mode)(value & 0x1f)) {
635  LOG_TARGET_DEBUG(target, "changing ARM core mode to '%s'",
636  arm_mode_name(value & 0x1f));
637  value &= ~((1 << 24) | (1 << 5));
638  uint8_t t[4];
639  buf_set_u32(t, 0, 32, value);
640  armv4_5_target->write_core_reg(target, reg,
641  16, ARM_MODE_ANY, t);
642  }
643  } else {
644  buf_set_u32(reg->value, 0, 32, value);
645  if (reg->size == 64) {
646  value = buf_get_u32(buf + 4, 0, 32);
647  buf_set_u32(reg->value + 4, 0, 32, value);
648  }
649  reg->valid = true;
650  }
651  reg->dirty = true;
652 
653  return ERROR_OK;
654 }
655 
656 static const struct reg_arch_type arm_reg_type = {
658  .set = armv4_5_set_core_reg,
659 };
660 
662 {
663  unsigned int num_regs = ARRAY_SIZE(arm_core_regs);
664  unsigned int num_core_regs = num_regs;
667 
668  struct reg_cache *cache = malloc(sizeof(struct reg_cache));
669  struct reg *reg_list = calloc(num_regs, sizeof(struct reg));
670  struct arm_reg *reg_arch_info = calloc(num_regs, sizeof(struct arm_reg));
671 
672  if (!cache || !reg_list || !reg_arch_info) {
673  free(cache);
674  free(reg_list);
675  free(reg_arch_info);
676  return NULL;
677  }
678 
679  cache->name = "ARM registers";
680  cache->next = NULL;
681  cache->reg_list = reg_list;
682  cache->num_regs = 0;
683 
684  for (unsigned int i = 0; i < num_core_regs; i++) {
685  /* Skip registers this core doesn't expose */
689  continue;
692  continue;
693 
694  reg_arch_info[i].num = arm_core_regs[i].cookie;
695  reg_arch_info[i].mode = arm_core_regs[i].mode;
696  reg_arch_info[i].target = target;
697  reg_arch_info[i].arm = arm;
698 
699  reg_list[i].name = arm_core_regs[i].name;
700  reg_list[i].number = arm_core_regs[i].gdb_index;
701  reg_list[i].size = 32;
702  reg_list[i].value = reg_arch_info[i].value;
703  reg_list[i].type = &arm_reg_type;
704  reg_list[i].arch_info = &reg_arch_info[i];
705  reg_list[i].exist = true;
706 
707  /* Registers data type, as used by GDB target description */
708  reg_list[i].reg_data_type = malloc(sizeof(struct reg_data_type));
709  switch (arm_core_regs[i].cookie) {
710  case 13:
711  reg_list[i].reg_data_type->type = REG_TYPE_DATA_PTR;
712  break;
713  case 14:
714  case 15:
715  reg_list[i].reg_data_type->type = REG_TYPE_CODE_PTR;
716  break;
717  default:
718  reg_list[i].reg_data_type->type = REG_TYPE_UINT32;
719  break;
720  }
721 
722  /* let GDB shows banked registers only in "info all-reg" */
723  reg_list[i].feature = malloc(sizeof(struct reg_feature));
724  if (reg_list[i].number <= 15 || reg_list[i].number == 25) {
725  reg_list[i].feature->name = "org.gnu.gdb.arm.core";
726  reg_list[i].group = "general";
727  /* Registers which should be preserved across GDB inferior function calls.
728  * Avoid saving banked registers as GDB (version 16.2 in time of writing)
729  * does not take the current mode into account and messes the value
730  * by restoring both the not banked register and the banked alias of it
731  * in the current mode. */
732  reg_list[i].caller_save = true;
733  } else {
734  reg_list[i].feature->name = "net.sourceforge.openocd.banked";
735  reg_list[i].group = "banked";
736  reg_list[i].caller_save = false;
737  }
738 
739  cache->num_regs++;
740  }
741 
742  for (unsigned int i = num_core_regs, j = 0; i < num_regs; i++, j++) {
743  reg_arch_info[i].num = arm_vfp_v3_regs[j].id;
744  reg_arch_info[i].mode = arm_vfp_v3_regs[j].mode;
745  reg_arch_info[i].target = target;
746  reg_arch_info[i].arm = arm;
747 
748  reg_list[i].name = arm_vfp_v3_regs[j].name;
749  reg_list[i].number = arm_vfp_v3_regs[j].id;
750  reg_list[i].size = arm_vfp_v3_regs[j].bits;
751  reg_list[i].value = reg_arch_info[i].value;
752  reg_list[i].type = &arm_reg_type;
753  reg_list[i].arch_info = &reg_arch_info[i];
754  reg_list[i].exist = true;
755 
756  /* Mark d0 - d31 and fpscr as save-restore for GDB */
757  reg_list[i].caller_save = true;
758 
759  reg_list[i].reg_data_type = malloc(sizeof(struct reg_data_type));
760  reg_list[i].reg_data_type->type = arm_vfp_v3_regs[j].type;
761 
762  reg_list[i].feature = malloc(sizeof(struct reg_feature));
763  reg_list[i].feature->name = arm_vfp_v3_regs[j].feature;
764 
765  reg_list[i].group = arm_vfp_v3_regs[j].group;
766 
767  cache->num_regs++;
768  }
769 
770  arm->pc = reg_list + 15;
771  arm->cpsr = reg_list + ARMV4_5_CPSR;
772  arm->core_cache = cache;
773 
774  return cache;
775 }
776 
778 {
779  if (!arm || !arm->core_cache)
780  return;
781 
782  struct reg_cache *cache = arm->core_cache;
783 
784  for (unsigned int i = 0; i < cache->num_regs; i++) {
785  struct reg *reg = &cache->reg_list[i];
786 
787  free(reg->feature);
788  free(reg->reg_data_type);
789  }
790 
791  free(cache->reg_list[0].arch_info);
792  free(cache->reg_list);
793  free(cache);
794 
795  arm->core_cache = NULL;
796 }
797 
799 {
800  struct arm *arm = target_to_arm(target);
801 
803  LOG_TARGET_ERROR(target, "BUG: called for a non-ARM target");
804  return ERROR_FAIL;
805  }
806 
807  /* avoid filling log waiting for fileio reply */
809  return ERROR_OK;
810 
811  LOG_TARGET_USER(target, "target halted in %s state due to %s, current mode: %s\n"
812  "cpsr: 0x%8.8" PRIx32 " pc: 0x%8.8" PRIx32 "%s%s",
816  buf_get_u32(arm->cpsr->value, 0, 32),
817  buf_get_u32(arm->pc->value, 0, 32),
818  (target->semihosting && target->semihosting->is_active) ? ", semihosting" : "",
819  (target->semihosting && target->semihosting->is_fileio) ? " fileio" : "");
820 
821  return ERROR_OK;
822 }
823 
824 COMMAND_HANDLER(handle_armv4_5_reg_command)
825 {
827  struct arm *arm = target_to_arm(target);
828  struct reg *regs;
829 
830  if (!is_arm(arm)) {
831  command_print(CMD, "current target isn't an ARM");
832  return ERROR_FAIL;
833  }
834 
835  if (target->state != TARGET_HALTED) {
836  command_print(CMD, "Error: target must be halted for register accesses");
838  }
839 
840  if (arm->core_type != ARM_CORE_TYPE_STD) {
842  "Microcontroller Profile not supported - use standard reg cmd");
843  return ERROR_OK;
844  }
845 
846  if (!is_arm_mode(arm->core_mode)) {
847  command_print(CMD, "not a valid arm core mode - communication failure?");
848  return ERROR_FAIL;
849  }
850 
851  if (!arm->full_context) {
852  command_print(CMD, "Error: target doesn't support %s",
853  CMD_NAME);
854  return ERROR_FAIL;
855  }
856 
858 
859  for (unsigned int mode = 0; mode < ARRAY_SIZE(arm_mode_data); mode++) {
860  const char *name;
861  char *sep = "\n";
862  char *shadow = "";
863 
865  continue;
866 
867  /* label this bank of registers (or shadows) */
868  switch (arm_mode_data[mode].psr) {
869  case ARM_MODE_SYS:
870  continue;
871  case ARM_MODE_USR:
872  name = "System and User";
873  sep = "";
874  break;
875  case ARM_MODE_HYP:
877  continue;
878  /* FALLTHROUGH */
879  case ARM_MODE_MON:
880  case ARM_MODE_1176_MON:
883  continue;
884  /* FALLTHROUGH */
885  default:
886  name = arm_mode_data[mode].name;
887  shadow = "shadow ";
888  break;
889  }
890  command_print(CMD, "%s%s mode %sregisters",
891  sep, name, shadow);
892 
893  /* display N rows of up to 4 registers each */
894  for (unsigned int i = 0; i < arm_mode_data[mode].n_indices; ) {
895  char output[80];
896  int output_len = 0;
897 
898  for (unsigned int j = 0; j < 4; j++, i++) {
899  uint32_t value;
900  struct reg *reg = regs;
901 
902  if (i >= arm_mode_data[mode].n_indices)
903  break;
904 
905  reg += arm_mode_data[mode].indices[i];
906 
907  /* REVISIT be smarter about faults... */
908  if (!reg->valid)
910 
911  value = buf_get_u32(reg->value, 0, 32);
912  output_len += snprintf(output + output_len,
913  sizeof(output) - output_len,
914  "%8s: %8.8" PRIx32 " ",
915  reg->name, value);
916  }
917  command_print(CMD, "%s", output);
918  }
919  }
920 
921  return ERROR_OK;
922 }
923 
924 COMMAND_HANDLER(handle_arm_core_state_command)
925 {
927  struct arm *arm = target_to_arm(target);
928  int ret = ERROR_OK;
929 
930  if (!is_arm(arm)) {
931  command_print(CMD, "current target isn't an ARM");
932  return ERROR_FAIL;
933  }
934 
935  if (CMD_ARGC > 0) {
936  if (strcmp(CMD_ARGV[0], "arm") == 0) {
938  command_print(CMD, "arm mode not supported on Cortex-M");
939  ret = ERROR_FAIL;
940  } else {
942  }
943  }
944  if (strcmp(CMD_ARGV[0], "thumb") == 0)
946  }
947 
948  command_print(CMD, "core state: %s", arm_core_state_string(arm));
949 
950  return ret;
951 }
952 
953 COMMAND_HANDLER(handle_arm_disassemble_command)
954 {
955 #if HAVE_CAPSTONE
957 
958  if (!target) {
959  command_print(CMD, "No target selected");
960  return ERROR_FAIL;
961  }
962 
963  struct arm *arm = target_to_arm(target);
965  unsigned int count = 1;
966  bool thumb = false;
967 
968  if (!is_arm(arm)) {
969  command_print(CMD, "current target isn't an ARM");
970  return ERROR_FAIL;
971  }
972 
974  /* armv7m is always thumb mode */
975  thumb = true;
976  }
977 
978  switch (CMD_ARGC) {
979  case 3:
980  if (strcmp(CMD_ARGV[2], "thumb") != 0)
982  thumb = true;
983  /* FALL THROUGH */
984  case 2:
986  /* FALL THROUGH */
987  case 1:
989  if (address & 0x01) {
990  if (!thumb) {
991  command_print(CMD, "Disassemble as Thumb");
992  thumb = true;
993  }
994  address &= ~1;
995  }
996  break;
997  default:
999  }
1000 
1001  return arm_disassemble(CMD, target, address, count, thumb);
1002 #else
1003  command_print(CMD, "capstone disassembly framework required");
1004  return ERROR_FAIL;
1005 #endif
1006 }
1007 
1008 COMMAND_HANDLER(handle_armv4_5_mcrmrc)
1009 {
1010  bool is_mcr = false;
1011  unsigned int arg_cnt = 5;
1012 
1013  if (!strcmp(CMD_NAME, "mcr")) {
1014  is_mcr = true;
1015  arg_cnt = 6;
1016  }
1017 
1018  if (arg_cnt != CMD_ARGC)
1020 
1022  if (!target) {
1023  command_print(CMD, "no current target");
1024  return ERROR_FAIL;
1025  }
1026  if (!target_was_examined(target)) {
1027  command_print(CMD, "%s: not yet examined", target_name(target));
1029  }
1030 
1031  struct arm *arm = target_to_arm(target);
1032  if (!is_arm(arm)) {
1033  command_print(CMD, "%s: not an ARM", target_name(target));
1034  return ERROR_FAIL;
1035  }
1036 
1037  if (target->state != TARGET_HALTED) {
1038  command_print(CMD, "Error: [%s] not halted", target_name(target));
1039  return ERROR_TARGET_NOT_HALTED;
1040  }
1041 
1042  int cpnum;
1043  uint32_t op1;
1044  uint32_t op2;
1045  uint32_t crn;
1046  uint32_t crm;
1047  uint32_t value;
1048 
1049  /* NOTE: parameter sequence matches ARM instruction set usage:
1050  * MCR pNUM, op1, rX, CRn, CRm, op2 ; write CP from rX
1051  * MRC pNUM, op1, rX, CRn, CRm, op2 ; read CP into rX
1052  * The "rX" is necessarily omitted; it uses Tcl mechanisms.
1053  */
1054  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], cpnum);
1055  if (cpnum & ~0xf) {
1056  command_print(CMD, "coprocessor %d out of range", cpnum);
1058  }
1059 
1060  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], op1);
1061  if (op1 & ~0x7) {
1062  command_print(CMD, "op1 %d out of range", op1);
1064  }
1065 
1066  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], crn);
1067  if (crn & ~0xf) {
1068  command_print(CMD, "CRn %d out of range", crn);
1070  }
1071 
1072  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], crm);
1073  if (crm & ~0xf) {
1074  command_print(CMD, "CRm %d out of range", crm);
1076  }
1077 
1078  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], op2);
1079  if (op2 & ~0x7) {
1080  command_print(CMD, "op2 %d out of range", op2);
1082  }
1083 
1084  /*
1085  * FIXME change the call syntax here ... simplest to just pass
1086  * the MRC() or MCR() instruction to be executed. That will also
1087  * let us support the "mrc2" and "mcr2" opcodes (toggling one bit)
1088  * if that's ever needed.
1089  */
1090  if (is_mcr) {
1091  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[5], value);
1092 
1093  /* NOTE: parameters reordered! */
1094  /* ARMV4_5_MCR(cpnum, op1, 0, crn, crm, op2) */
1095  int retval = arm->mcr(target, cpnum, op1, op2, crn, crm, value);
1096  if (retval != ERROR_OK)
1097  return retval;
1098  } else {
1099  value = 0;
1100  /* NOTE: parameters reordered! */
1101  /* ARMV4_5_MRC(cpnum, op1, 0, crn, crm, op2) */
1102  int retval = arm->mrc(target, cpnum, op1, op2, crn, crm, &value);
1103  if (retval != ERROR_OK)
1104  return retval;
1105 
1106  command_print(CMD, "0x%" PRIx32, value);
1107  }
1108 
1109  return ERROR_OK;
1110 }
1111 
1112 COMMAND_HANDLER(handle_armv4_5_mcrrmrrc)
1113 {
1114  bool is_mcrr = false;
1115  unsigned int arg_cnt = 3;
1116 
1117  if (!strcmp(CMD_NAME, "mcrr")) {
1118  is_mcrr = true;
1119  arg_cnt = 4;
1120  }
1121 
1122  if (arg_cnt != CMD_ARGC)
1124 
1126  if (!target) {
1127  command_print(CMD, "no current target");
1128  return ERROR_FAIL;
1129  }
1130  if (!target_was_examined(target)) {
1131  command_print(CMD, "%s: not yet examined", target_name(target));
1133  }
1134 
1135  struct arm *arm = target_to_arm(target);
1136  if (!is_arm(arm)) {
1137  command_print(CMD, "%s: not an ARM", target_name(target));
1138  return ERROR_FAIL;
1139  }
1140 
1141  if (target->state != TARGET_HALTED)
1142  return ERROR_TARGET_NOT_HALTED;
1143 
1144  int cpnum;
1145  uint32_t op1;
1146  uint32_t crm;
1147  uint64_t value;
1148 
1149  /* NOTE: parameter sequence matches ARM instruction set usage:
1150  * MCRR pNUM, op1, rX1, rX2, CRm ; write CP from rX1 and rX2
1151  * MREC pNUM, op1, rX1, rX2, CRm ; read CP into rX1 and rX2
1152  * The "rXn" are necessarily omitted; they use Tcl mechanisms.
1153  */
1154  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], cpnum);
1155  if (cpnum & ~0xf) {
1156  command_print(CMD, "coprocessor %d out of range", cpnum);
1158  }
1159 
1160  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], op1);
1161  if (op1 & ~0xf) {
1162  command_print(CMD, "op1 %d out of range", op1);
1164  }
1165 
1166  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], crm);
1167  if (crm & ~0xf) {
1168  command_print(CMD, "CRm %d out of range", crm);
1170  }
1171 
1172  /*
1173  * FIXME change the call syntax here ... simplest to just pass
1174  * the MRC() or MCR() instruction to be executed. That will also
1175  * let us support the "mrrc2" and "mcrr2" opcodes (toggling one bit)
1176  * if that's ever needed.
1177  */
1178  if (is_mcrr) {
1179  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[3], value);
1180 
1181  /* NOTE: parameters reordered! */
1182  /* ARMV5_T_MCRR(cpnum, op1, crm) */
1183  int retval = arm->mcrr(target, cpnum, op1, crm, value);
1184  if (retval != ERROR_OK)
1185  return retval;
1186  } else {
1187  value = 0;
1188  /* NOTE: parameters reordered! */
1189  /* ARMV5_T_MRRC(cpnum, op1, crm) */
1190  int retval = arm->mrrc(target, cpnum, op1, crm, &value);
1191  if (retval != ERROR_OK)
1192  return retval;
1193 
1194  command_print(CMD, "0x%" PRIx64, value);
1195  }
1196 
1197  return ERROR_OK;
1198 }
1199 
1200 static const struct command_registration arm_exec_command_handlers[] = {
1201  {
1202  .name = "reg",
1203  .handler = handle_armv4_5_reg_command,
1204  .mode = COMMAND_EXEC,
1205  .help = "display ARM core registers",
1206  .usage = "",
1207  },
1208  {
1209  .name = "mcr",
1210  .mode = COMMAND_EXEC,
1211  .handler = handle_armv4_5_mcrmrc,
1212  .help = "write coprocessor register",
1213  .usage = "cpnum op1 CRn CRm op2 value",
1214  },
1215  {
1216  .name = "mrc",
1217  .mode = COMMAND_EXEC,
1218  .handler = handle_armv4_5_mcrmrc,
1219  .help = "read coprocessor register",
1220  .usage = "cpnum op1 CRn CRm op2",
1221  },
1222  {
1223  .name = "mcrr",
1224  .mode = COMMAND_EXEC,
1225  .handler = handle_armv4_5_mcrrmrrc,
1226  .help = "write coprocessor 64-bit register",
1227  .usage = "cpnum op1 CRm value",
1228  },
1229  {
1230  .name = "mrrc",
1231  .mode = COMMAND_EXEC,
1232  .handler = handle_armv4_5_mcrrmrrc,
1233  .help = "read coprocessor 64-bit register",
1234  .usage = "cpnum op1 CRm",
1235  },
1236  {
1238  },
1240 };
1241 
1243  {
1244  .name = "core_state",
1245  .handler = handle_arm_core_state_command,
1246  .mode = COMMAND_EXEC,
1247  .usage = "['arm'|'thumb']",
1248  .help = "display/change ARM core state",
1249  },
1250  {
1251  .name = "disassemble",
1252  .handler = handle_arm_disassemble_command,
1253  .mode = COMMAND_EXEC,
1254  .usage = "address [count ['thumb']]",
1255  .help = "disassemble instructions",
1256  },
1257  {
1258  .chain = semihosting_common_handlers,
1259  },
1261 };
1262 
1263 const struct command_registration arm_command_handlers[] = {
1264  {
1265  .name = "arm",
1266  .mode = COMMAND_ANY,
1267  .help = "ARM command group",
1268  .usage = "",
1269  .chain = arm_exec_command_handlers,
1270  },
1272 };
1273 
1274 /*
1275  * gdb for arm targets (e.g. arm-none-eabi-gdb) supports several variants
1276  * of arm architecture. You can list them using the autocompletion of gdb
1277  * command prompt by typing "set architecture " and then press TAB key.
1278  * The default, selected automatically, is "arm".
1279  * Let's use the default value, here, to make gdb-multiarch behave in the
1280  * same way as a gdb for arm. This can be changed later on. User can still
1281  * set the specific architecture variant with the gdb command.
1282  */
1283 const char *arm_get_gdb_arch(const struct target *target)
1284 {
1285  return "arm";
1286 }
1287 
1289  struct reg **reg_list[], int *reg_list_size,
1290  enum target_register_class reg_class)
1291 {
1292  struct arm *arm = target_to_arm(target);
1293  unsigned int i;
1294 
1295  if (!is_arm_mode(arm->core_mode)) {
1296  LOG_TARGET_ERROR(target, "not a valid arm core mode - communication failure?");
1297  return ERROR_FAIL;
1298  }
1299 
1300  switch (reg_class) {
1301  case REG_CLASS_GENERAL:
1302  *reg_list_size = 26;
1303  *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
1304 
1305  for (i = 0; i < 16; i++)
1306  (*reg_list)[i] = arm_reg_current(arm, i);
1307 
1308  /* For GDB compatibility, take FPA registers size into account and zero-fill it*/
1309  for (i = 16; i < 24; i++)
1310  (*reg_list)[i] = &arm_gdb_dummy_fp_reg;
1311  (*reg_list)[24] = &arm_gdb_dummy_fps_reg;
1312 
1313  (*reg_list)[25] = arm->cpsr;
1314 
1315  return ERROR_OK;
1316 
1317  case REG_CLASS_ALL:
1318  switch (arm->core_type) {
1319  case ARM_CORE_TYPE_SEC_EXT:
1320  *reg_list_size = 51;
1321  break;
1323  *reg_list_size = 53;
1324  break;
1325  default:
1326  *reg_list_size = 48;
1327  }
1328  unsigned int list_size_core = *reg_list_size;
1329  if (arm->arm_vfp_version == ARM_VFP_V3)
1330  *reg_list_size += 33;
1331 
1332  *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
1333 
1334  for (i = 0; i < 16; i++)
1335  (*reg_list)[i] = arm_reg_current(arm, i);
1336 
1337  for (i = 13; i < ARRAY_SIZE(arm_core_regs); i++) {
1338  int reg_index = arm->core_cache->reg_list[i].number;
1339 
1340  if (arm_core_regs[i].mode == ARM_MODE_MON
1343  continue;
1344  if (arm_core_regs[i].mode == ARM_MODE_HYP
1346  continue;
1347  (*reg_list)[reg_index] = &arm->core_cache->reg_list[i];
1348  }
1349 
1350  /* When we supply the target description, there is no need for fake FPA */
1351  for (i = 16; i < 24; i++) {
1352  (*reg_list)[i] = &arm_gdb_dummy_fp_reg;
1353  (*reg_list)[i]->size = 0;
1354  }
1355  (*reg_list)[24] = &arm_gdb_dummy_fps_reg;
1356  (*reg_list)[24]->size = 0;
1357 
1358  if (arm->arm_vfp_version == ARM_VFP_V3) {
1359  unsigned int num_core_regs = ARRAY_SIZE(arm_core_regs);
1360  for (i = 0; i < 33; i++)
1361  (*reg_list)[list_size_core + i] = &(arm->core_cache->reg_list[num_core_regs + i]);
1362  }
1363 
1364  return ERROR_OK;
1365 
1366  default:
1367  LOG_TARGET_ERROR(target, "not a valid register class type in query");
1368  return ERROR_FAIL;
1369  }
1370 }
1371 
1372 /* wait for execution to complete and check exit point */
1374  uint32_t exit_point,
1375  unsigned int timeout_ms,
1376  void *arch_info)
1377 {
1378  int retval;
1379  struct arm *arm = target_to_arm(target);
1380 
1381  retval = target_wait_state(target, TARGET_HALTED, timeout_ms);
1382  if (retval != ERROR_OK)
1383  return retval;
1384  if (target->state != TARGET_HALTED) {
1385  retval = target_halt(target);
1386  if (retval != ERROR_OK)
1387  return retval;
1388  retval = target_wait_state(target, TARGET_HALTED, 500);
1389  if (retval != ERROR_OK)
1390  return retval;
1391  return ERROR_TARGET_TIMEOUT;
1392  }
1393 
1394  /* fast exit: ARMv5+ code can use BKPT */
1395  if (exit_point && buf_get_u32(arm->pc->value, 0, 32) != exit_point) {
1396  LOG_TARGET_ERROR(target, "reentered debug state, but not at the desired exit point: 0x%4.4" PRIx32,
1397  buf_get_u32(arm->pc->value, 0, 32));
1398  return ERROR_TARGET_TIMEOUT;
1399  }
1400 
1401  return ERROR_OK;
1402 }
1403 
1405  int num_mem_params, struct mem_param *mem_params,
1406  int num_reg_params, struct reg_param *reg_params,
1407  uint32_t entry_point, uint32_t exit_point,
1408  unsigned int timeout_ms, void *arch_info,
1409  int (*run_it)(struct target *target, uint32_t exit_point,
1410  unsigned int timeout_ms, void *arch_info))
1411 {
1412  struct arm *arm = target_to_arm(target);
1413  struct arm_algorithm *arm_algorithm_info = arch_info;
1415  uint32_t context[17];
1416  uint32_t cpsr;
1417  int exit_breakpoint_size = 0;
1418  int retval = ERROR_OK;
1419 
1420  LOG_TARGET_DEBUG(target, "Running algorithm");
1421 
1422  if (arm_algorithm_info->common_magic != ARM_COMMON_MAGIC) {
1423  LOG_TARGET_ERROR(target, "current target isn't an ARMV4/5 target");
1424  return ERROR_TARGET_INVALID;
1425  }
1426 
1427  if (target->state != TARGET_HALTED) {
1428  LOG_TARGET_ERROR(target, "not halted (run target algo)");
1429  return ERROR_TARGET_NOT_HALTED;
1430  }
1431 
1432  if (!is_arm_mode(arm->core_mode)) {
1433  LOG_TARGET_ERROR(target, "not a valid arm core mode - communication failure?");
1434  return ERROR_FAIL;
1435  }
1436 
1437  /* armv5 and later can terminate with BKPT instruction; less overhead */
1438  if (!exit_point && arm->arch == ARM_ARCH_V4) {
1439  LOG_TARGET_ERROR(target, "ARMv4 target needs HW breakpoint location");
1440  return ERROR_FAIL;
1441  }
1442 
1443  /* save r0..pc, cpsr-or-spsr, and then cpsr-for-sure;
1444  * they'll be restored later.
1445  */
1446  for (unsigned int i = 0; i < ARRAY_SIZE(context); i++) {
1447  struct reg *r;
1448 
1450  arm_algorithm_info->core_mode, i);
1451  if (!r->valid)
1452  arm->read_core_reg(target, r, i,
1453  arm_algorithm_info->core_mode);
1454  context[i] = buf_get_u32(r->value, 0, 32);
1455  }
1456  cpsr = buf_get_u32(arm->cpsr->value, 0, 32);
1457 
1458  for (int i = 0; i < num_mem_params; i++) {
1459  if (mem_params[i].direction == PARAM_IN)
1460  continue;
1461  retval = target_write_buffer(target, mem_params[i].address, mem_params[i].size,
1462  mem_params[i].value);
1463  if (retval != ERROR_OK)
1464  return retval;
1465  }
1466 
1467  for (int i = 0; i < num_reg_params; i++) {
1468  if (reg_params[i].direction == PARAM_IN)
1469  continue;
1470 
1471  struct reg *reg = register_get_by_name(arm->core_cache, reg_params[i].reg_name, false);
1472  if (!reg) {
1473  LOG_TARGET_ERROR(target, "BUG: register '%s' not found", reg_params[i].reg_name);
1475  }
1476 
1477  if (reg->size != reg_params[i].size) {
1478  LOG_TARGET_ERROR(target, "BUG: register '%s' size doesn't match reg_params[i].size",
1479  reg_params[i].reg_name);
1481  }
1482 
1483  retval = armv4_5_set_core_reg(reg, reg_params[i].value);
1484  if (retval != ERROR_OK)
1485  return retval;
1486  }
1487 
1488  arm->core_state = arm_algorithm_info->core_state;
1489  if (arm->core_state == ARM_STATE_ARM)
1490  exit_breakpoint_size = 4;
1491  else if (arm->core_state == ARM_STATE_THUMB)
1492  exit_breakpoint_size = 2;
1493  else {
1494  LOG_TARGET_ERROR(target, "BUG: can't execute algorithms when not in ARM or Thumb state");
1496  }
1497 
1498  if (arm_algorithm_info->core_mode != ARM_MODE_ANY) {
1499  LOG_TARGET_DEBUG(target, "setting core_mode: 0x%2.2x",
1500  arm_algorithm_info->core_mode);
1501  buf_set_u32(arm->cpsr->value, 0, 5,
1502  arm_algorithm_info->core_mode);
1503  arm->cpsr->dirty = true;
1504  arm->cpsr->valid = true;
1505  }
1506 
1507  /* terminate using a hardware or (ARMv5+) software breakpoint */
1508  if (exit_point) {
1509  retval = breakpoint_add(target, exit_point,
1510  exit_breakpoint_size, BKPT_HARD);
1511  if (retval != ERROR_OK) {
1512  LOG_TARGET_ERROR(target, "can't add HW breakpoint to terminate algorithm");
1513  return ERROR_TARGET_FAILURE;
1514  }
1515  }
1516 
1517  retval = target_resume(target, false, entry_point, true, true);
1518  if (retval != ERROR_OK)
1519  return retval;
1520  retval = run_it(target, exit_point, timeout_ms, arch_info);
1521 
1522  if (exit_point)
1523  breakpoint_remove(target, exit_point);
1524 
1525  if (retval != ERROR_OK)
1526  return retval;
1527 
1528  for (int i = 0; i < num_mem_params; i++) {
1529  if (mem_params[i].direction != PARAM_OUT) {
1530  int retvaltemp = target_read_buffer(target, mem_params[i].address,
1531  mem_params[i].size,
1532  mem_params[i].value);
1533  if (retvaltemp != ERROR_OK)
1534  retval = retvaltemp;
1535  }
1536  }
1537 
1538  for (int i = 0; i < num_reg_params; i++) {
1539  if (reg_params[i].direction != PARAM_OUT) {
1540 
1542  reg_params[i].reg_name,
1543  false);
1544  if (!reg) {
1545  LOG_TARGET_ERROR(target, "BUG: register '%s' not found", reg_params[i].reg_name);
1546  retval = ERROR_COMMAND_SYNTAX_ERROR;
1547  continue;
1548  }
1549 
1550  if (reg->size != reg_params[i].size) {
1552  "BUG: register '%s' size doesn't match reg_params[i].size",
1553  reg_params[i].reg_name);
1554  retval = ERROR_COMMAND_SYNTAX_ERROR;
1555  continue;
1556  }
1557 
1558  buf_set_u32(reg_params[i].value, 0, 32, buf_get_u32(reg->value, 0, 32));
1559  }
1560  }
1561 
1562  /* restore everything we saved before (17 or 18 registers) */
1563  for (unsigned int i = 0; i < ARRAY_SIZE(context); i++) {
1564  uint32_t regvalue;
1566  arm_algorithm_info->core_mode, i).value, 0, 32);
1567  if (regvalue != context[i]) {
1568  LOG_DEBUG("restoring register %s with value 0x%8.8" PRIx32,
1570  arm_algorithm_info->core_mode, i).name, context[i]);
1572  arm_algorithm_info->core_mode, i).value, 0, 32, context[i]);
1573  ARMV4_5_CORE_REG_MODE(arm->core_cache, arm_algorithm_info->core_mode,
1574  i).valid = true;
1575  ARMV4_5_CORE_REG_MODE(arm->core_cache, arm_algorithm_info->core_mode,
1576  i).dirty = true;
1577  }
1578  }
1579 
1580  arm_set_cpsr(arm, cpsr);
1581  arm->cpsr->dirty = true;
1582 
1583  arm->core_state = core_state;
1584 
1585  return retval;
1586 }
1587 
1589  int num_mem_params,
1590  struct mem_param *mem_params,
1591  int num_reg_params,
1592  struct reg_param *reg_params,
1593  target_addr_t entry_point,
1594  target_addr_t exit_point,
1595  unsigned int timeout_ms,
1596  void *arch_info)
1597 {
1599  num_mem_params,
1600  mem_params,
1601  num_reg_params,
1602  reg_params,
1603  (uint32_t)entry_point,
1604  (uint32_t)exit_point,
1605  timeout_ms,
1606  arch_info,
1608 }
1609 
1615  target_addr_t address, uint32_t count, uint32_t *checksum)
1616 {
1617  struct working_area *crc_algorithm;
1618  struct arm_algorithm arm_algo;
1619  struct arm *arm = target_to_arm(target);
1620  struct reg_param reg_params[2];
1621  int retval;
1622  uint32_t i;
1623  uint32_t exit_var = 0;
1624 
1625  static const uint8_t arm_crc_code_le[] = {
1626 #include "../../contrib/loaders/checksum/armv4_5_crc.inc"
1627  };
1628 
1629  assert(sizeof(arm_crc_code_le) % 4 == 0);
1630 
1632  sizeof(arm_crc_code_le), &crc_algorithm);
1633  if (retval != ERROR_OK)
1634  return retval;
1635 
1636  /* convert code into a buffer in target endianness */
1637  for (i = 0; i < ARRAY_SIZE(arm_crc_code_le) / 4; i++) {
1638  retval = target_write_u32(target,
1639  crc_algorithm->address + i * sizeof(uint32_t),
1640  le_to_h_u32(&arm_crc_code_le[i * 4]));
1641  if (retval != ERROR_OK)
1642  goto cleanup;
1643  }
1644 
1645  arm_algo.common_magic = ARM_COMMON_MAGIC;
1646  arm_algo.core_mode = ARM_MODE_SVC;
1647  arm_algo.core_state = ARM_STATE_ARM;
1648 
1649  init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT);
1650  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
1651 
1652  buf_set_u32(reg_params[0].value, 0, 32, address);
1653  buf_set_u32(reg_params[1].value, 0, 32, count);
1654 
1655  /* 20 second timeout/megabyte */
1656  unsigned int timeout = 20000 * (1 + (count / (1024 * 1024)));
1657 
1658  /* armv4 must exit using a hardware breakpoint */
1659  if (arm->arch == ARM_ARCH_V4)
1660  exit_var = crc_algorithm->address + sizeof(arm_crc_code_le) - 8;
1661 
1662  retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
1663  crc_algorithm->address,
1664  exit_var,
1665  timeout, &arm_algo);
1666 
1667  if (retval == ERROR_OK)
1668  *checksum = buf_get_u32(reg_params[0].value, 0, 32);
1669  else
1670  LOG_TARGET_ERROR(target, "error executing ARM CRC algorithm");
1671 
1672  destroy_reg_param(&reg_params[0]);
1673  destroy_reg_param(&reg_params[1]);
1674 
1675 cleanup:
1676  target_free_working_area(target, crc_algorithm);
1677 
1678  return retval;
1679 }
1680 
1688  struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
1689 {
1690  struct working_area *check_algorithm;
1691  struct reg_param reg_params[3];
1692  struct arm_algorithm arm_algo;
1693  struct arm *arm = target_to_arm(target);
1694  int retval;
1695  uint32_t i;
1696  uint32_t exit_var = 0;
1697 
1698  static const uint8_t check_code_le[] = {
1699 #include "../../contrib/loaders/erase_check/armv4_5_erase_check.inc"
1700  };
1701 
1702  assert(sizeof(check_code_le) % 4 == 0);
1703 
1704  if (erased_value != 0xff) {
1705  LOG_TARGET_ERROR(target, "Erase value 0x%02" PRIx8 " not yet supported for ARMv4/v5 targets",
1706  erased_value);
1707  return ERROR_FAIL;
1708  }
1709 
1710  /* make sure we have a working area */
1712  sizeof(check_code_le), &check_algorithm);
1713  if (retval != ERROR_OK)
1714  return retval;
1715 
1716  /* convert code into a buffer in target endianness */
1717  for (i = 0; i < ARRAY_SIZE(check_code_le) / 4; i++) {
1718  retval = target_write_u32(target,
1719  check_algorithm->address
1720  + i * sizeof(uint32_t),
1721  le_to_h_u32(&check_code_le[i * 4]));
1722  if (retval != ERROR_OK)
1723  goto cleanup;
1724  }
1725 
1726  arm_algo.common_magic = ARM_COMMON_MAGIC;
1727  arm_algo.core_mode = ARM_MODE_SVC;
1728  arm_algo.core_state = ARM_STATE_ARM;
1729 
1730  init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
1731  buf_set_u32(reg_params[0].value, 0, 32, blocks[0].address);
1732 
1733  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
1734  buf_set_u32(reg_params[1].value, 0, 32, blocks[0].size);
1735 
1736  init_reg_param(&reg_params[2], "r2", 32, PARAM_IN_OUT);
1737  buf_set_u32(reg_params[2].value, 0, 32, erased_value);
1738 
1739  /* armv4 must exit using a hardware breakpoint */
1740  if (arm->arch == ARM_ARCH_V4)
1741  exit_var = check_algorithm->address + sizeof(check_code_le) - 4;
1742 
1743  retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
1744  check_algorithm->address,
1745  exit_var,
1746  10000, &arm_algo);
1747 
1748  if (retval == ERROR_OK)
1749  blocks[0].result = buf_get_u32(reg_params[2].value, 0, 32);
1750 
1751  destroy_reg_param(&reg_params[0]);
1752  destroy_reg_param(&reg_params[1]);
1753  destroy_reg_param(&reg_params[2]);
1754 
1755 cleanup:
1756  target_free_working_area(target, check_algorithm);
1757 
1758  if (retval != ERROR_OK)
1759  return retval;
1760 
1761  return 1; /* only one block has been checked */
1762 }
1763 
1764 static int arm_full_context(struct target *target)
1765 {
1766  struct arm *arm = target_to_arm(target);
1767  unsigned int num_regs = arm->core_cache->num_regs;
1768  struct reg *reg = arm->core_cache->reg_list;
1769  int retval = ERROR_OK;
1770 
1771  for (; num_regs && retval == ERROR_OK; num_regs--, reg++) {
1772  if (!reg->exist || reg->valid)
1773  continue;
1774  retval = armv4_5_get_core_reg(reg);
1775  }
1776  return retval;
1777 }
1778 
1779 static int arm_default_mrc(struct target *target, int cpnum,
1780  uint32_t op1, uint32_t op2,
1781  uint32_t crn, uint32_t crm,
1782  uint32_t *value)
1783 {
1784  LOG_TARGET_ERROR(target, "%s doesn't implement MRC", target_type_name(target));
1785  return ERROR_FAIL;
1786 }
1787 
1788 static int arm_default_mrrc(struct target *target, int cpnum,
1789  uint32_t op, uint32_t crm,
1790  uint64_t *value)
1791 {
1792  LOG_TARGET_ERROR(target, "%s doesn't implement MRRC", target_type_name(target));
1793  return ERROR_FAIL;
1794 }
1795 
1796 static int arm_default_mcr(struct target *target, int cpnum,
1797  uint32_t op1, uint32_t op2,
1798  uint32_t crn, uint32_t crm,
1799  uint32_t value)
1800 {
1801  LOG_TARGET_ERROR(target, "%s doesn't implement MCR", target_type_name(target));
1802  return ERROR_FAIL;
1803 }
1804 
1805 static int arm_default_mcrr(struct target *target, int cpnum,
1806  uint32_t op, uint32_t crm,
1807  uint64_t value)
1808 {
1809  LOG_TARGET_ERROR(target, "%s doesn't implement MCRR", target_type_name(target));
1810  return ERROR_FAIL;
1811 }
1812 
1813 int arm_init_arch_info(struct target *target, struct arm *arm)
1814 {
1815  target->arch_info = arm;
1816  arm->target = target;
1817 
1819 
1820  /* core_type may be overridden by subtype logic */
1824  }
1825 
1826  /* default full_context() has no core-specific optimizations */
1827  if (!arm->full_context && arm->read_core_reg)
1829 
1830  if (!arm->mrc)
1831  arm->mrc = arm_default_mrc;
1832  if (!arm->mrrc)
1834  if (!arm->mcr)
1835  arm->mcr = arm_default_mcr;
1836  if (!arm->mcrr)
1838 
1839  return ERROR_OK;
1840 }
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
Definition: algorithm.c:29
void destroy_reg_param(struct reg_param *param)
Definition: algorithm.c:38
@ PARAM_OUT
Definition: algorithm.h:16
@ PARAM_IN
Definition: algorithm.h:15
@ PARAM_IN_OUT
Definition: algorithm.h:17
Holds the interface to ARM cores.
@ ARM_VFP_V3
Definition: arm.h:164
#define ARM_COMMON_MAGIC
Definition: arm.h:167
@ ARM_ARCH_V4
Definition: arm.h:55
static bool is_arm(struct arm *arm)
Definition: arm.h:268
arm_mode
Represent state of an ARM core.
Definition: arm.h:82
@ ARM_MODE_IRQ
Definition: arm.h:85
@ ARM_MODE_HANDLER
Definition: arm.h:96
@ ARM_MODE_SYS
Definition: arm.h:92
@ ARM_MODE_HYP
Definition: arm.h:89
@ ARM_MODE_MON
Definition: arm.h:87
@ ARM_MODE_FIQ
Definition: arm.h:84
@ ARM_MODE_UND
Definition: arm.h:90
@ ARM_MODE_1176_MON
Definition: arm.h:91
@ ARM_MODE_ANY
Definition: arm.h:106
@ ARM_MODE_USR
Definition: arm.h:83
@ ARM_MODE_SVC
Definition: arm.h:86
@ ARM_MODE_USER_THREAD
Definition: arm.h:95
@ ARM_MODE_ABT
Definition: arm.h:88
@ ARM_MODE_THREAD
Definition: arm.h:94
@ ARM_VFP_V3_D14
Definition: arm.h:126
@ ARM_VFP_V3_D24
Definition: arm.h:136
@ ARM_VFP_V3_D9
Definition: arm.h:121
@ ARM_VFP_V3_D1
Definition: arm.h:113
@ ARM_VFP_V3_D17
Definition: arm.h:129
@ ARM_VFP_V3_D19
Definition: arm.h:131
@ ARM_VFP_V3_D4
Definition: arm.h:116
@ ARM_VFP_V3_D15
Definition: arm.h:127
@ ARM_VFP_V3_D10
Definition: arm.h:122
@ ARM_VFP_V3_D3
Definition: arm.h:115
@ ARM_VFP_V3_D31
Definition: arm.h:143
@ ARM_VFP_V3_D16
Definition: arm.h:128
@ ARM_VFP_V3_D22
Definition: arm.h:134
@ ARM_VFP_V3_D5
Definition: arm.h:117
@ ARM_VFP_V3_D18
Definition: arm.h:130
@ ARM_VFP_V3_D26
Definition: arm.h:138
@ ARM_VFP_V3_D7
Definition: arm.h:119
@ ARM_VFP_V3_D23
Definition: arm.h:135
@ ARM_VFP_V3_D21
Definition: arm.h:133
@ ARM_VFP_V3_D28
Definition: arm.h:140
@ ARM_VFP_V3_D2
Definition: arm.h:114
@ ARM_VFP_V3_D27
Definition: arm.h:139
@ ARM_VFP_V3_D29
Definition: arm.h:141
@ ARM_VFP_V3_D11
Definition: arm.h:123
@ ARM_VFP_V3_FPSCR
Definition: arm.h:144
@ ARM_VFP_V3_D20
Definition: arm.h:132
@ ARM_VFP_V3_D13
Definition: arm.h:125
@ ARM_VFP_V3_D12
Definition: arm.h:124
@ ARM_VFP_V3_D6
Definition: arm.h:118
@ ARM_VFP_V3_D8
Definition: arm.h:120
@ ARM_VFP_V3_D0
Definition: arm.h:111
@ ARM_VFP_V3_D30
Definition: arm.h:142
@ ARM_VFP_V3_D25
Definition: arm.h:137
static struct arm * target_to_arm(const struct target *target)
Convert target handle to generic ARM target state handle.
Definition: arm.h:262
arm_state
The PSR "T" and "J" bits define the mode of "classic ARM" cores.
Definition: arm.h:151
@ ARM_STATE_JAZELLE
Definition: arm.h:154
@ ARM_STATE_THUMB
Definition: arm.h:153
@ ARM_STATE_ARM
Definition: arm.h:152
@ ARM_STATE_AARCH64
Definition: arm.h:156
@ ARM_STATE_THUMB_EE
Definition: arm.h:155
@ ARM_CORE_TYPE_SEC_EXT
Definition: arm.h:47
@ ARM_CORE_TYPE_VIRT_EXT
Definition: arm.h:48
@ ARM_CORE_TYPE_M_PROFILE
Definition: arm.h:49
@ ARM_CORE_TYPE_STD
Definition: arm.h:46
int arm_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
Runs ARM code in the target to check whether a memory block holds all ones.
Definition: armv4_5.c:1687
static const struct reg_arch_type arm_reg_type
Definition: armv4_5.c:656
static int armv4_5_set_core_reg(struct reg *reg, uint8_t *buf)
Definition: armv4_5.c:610
static const uint8_t arm_svc_indices[3]
Definition: armv4_5.c:55
static const struct @77 arm_vfp_v3_regs[]
static const uint8_t arm_gdb_dummy_fp_value[12]
Definition: armv4_5.c:541
static const uint8_t arm_irq_indices[3]
Definition: armv4_5.c:51
const struct command_registration arm_all_profiles_command_handlers[]
Definition: armv4_5.c:1242
struct reg * arm_reg_current(struct arm *arm, unsigned int regnum)
Returns handle to the register currently mapped to a given number.
Definition: armv4_5.c:517
const uint8_t * indices
Definition: armv4_5.c:82
int armv4_5_run_algorithm_inner(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, unsigned int timeout_ms, void *arch_info, int(*run_it)(struct target *target, uint32_t exit_point, unsigned int timeout_ms, void *arch_info))
Definition: armv4_5.c:1404
int arm_arch_state(struct target *target)
Definition: armv4_5.c:798
static const char * arm_core_state_string(struct arm *arm)
Definition: armv4_5.c:438
enum arm_mode mode
Definition: armv4_5.c:281
unsigned int gdb_index
Definition: armv4_5.c:280
static const struct @76 arm_core_regs[]
bool is_arm_mode(unsigned int psr_mode)
Return true iff the parameter denotes a valid ARM processor mode.
Definition: armv4_5.c:182
int arm_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Runs ARM code in the target to calculate a CRC32 checksum.
Definition: armv4_5.c:1614
int arm_mode_to_number(enum arm_mode mode)
Map PSR mode bits to linear number indexing armv4_5_core_reg_map.
Definition: armv4_5.c:192
unsigned short n_indices
Definition: armv4_5.c:81
static int arm_default_mcrr(struct target *target, int cpnum, uint32_t op, uint32_t crm, uint64_t value)
Definition: armv4_5.c:1805
static int arm_default_mrrc(struct target *target, int cpnum, uint32_t op, uint32_t crm, uint64_t *value)
Definition: armv4_5.c:1788
static struct reg arm_gdb_dummy_fp_reg
Dummy FPA registers are required to support GDB on ARM.
Definition: armv4_5.c:553
static const char * arm_state_strings[]
Definition: armv4_5.c:250
COMMAND_HANDLER(handle_armv4_5_reg_command)
Definition: armv4_5.c:824
static int arm_full_context(struct target *target)
Definition: armv4_5.c:1764
static const uint8_t arm_abt_indices[3]
Definition: armv4_5.c:59
struct reg_cache * arm_build_reg_cache(struct target *target, struct arm *arm)
Definition: armv4_5.c:661
const char * arm_get_gdb_arch(const struct target *target)
Definition: armv4_5.c:1283
static const uint8_t arm_hyp_indices[2]
Definition: armv4_5.c:71
const char * group
Definition: armv4_5.c:367
int arm_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Definition: armv4_5.c:1288
const int armv4_5_core_reg_map[9][17]
Definition: armv4_5.c:408
static struct reg arm_gdb_dummy_fps_reg
Dummy FPA status registers are required to support GDB on ARM.
Definition: armv4_5.c:570
static const uint8_t arm_fiq_indices[8]
Definition: armv4_5.c:47
static int arm_default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t crn, uint32_t crm, uint32_t *value)
Definition: armv4_5.c:1779
static int armv4_5_get_core_reg(struct reg *reg)
Definition: armv4_5.c:589
const char * name
Definition: armv4_5.c:76
void arm_free_reg_cache(struct arm *arm)
Definition: armv4_5.c:777
static const uint8_t arm_usr_indices[17]
Definition: armv4_5.c:43
static struct reg_feature arm_gdb_dummy_fp_features
Definition: armv4_5.c:543
enum reg_type type
Definition: armv4_5.c:366
static const uint8_t arm_mon_indices[3]
Definition: armv4_5.c:67
unsigned int id
Definition: armv4_5.c:362
static const uint8_t arm_und_indices[3]
Definition: armv4_5.c:63
static const struct command_registration arm_exec_command_handlers[]
Definition: armv4_5.c:1200
const struct command_registration arm_command_handlers[]
Definition: armv4_5.c:1263
static void arm_gdb_dummy_init(void)
Definition: armv4_5.c:581
unsigned int cookie
Definition: armv4_5.c:279
@ ARMV4_5_SPSR_UND
Definition: armv4_5.c:38
@ ARMV4_5_SPSR_ABT
Definition: armv4_5.c:37
@ ARMV4_5_SPSR_IRQ
Definition: armv4_5.c:35
@ ARM_SPSR_MON
Definition: armv4_5.c:39
@ ARMV4_5_SPSR_SVC
Definition: armv4_5.c:36
@ ARM_SPSR_HYP
Definition: armv4_5.c:40
@ ARMV4_5_SPSR_FIQ
Definition: armv4_5.c:34
unsigned short psr
Definition: armv4_5.c:77
enum arm_mode armv4_5_number_to_mode(int number)
Map linear number indexing armv4_5_core_reg_map to PSR mode bits.
Definition: armv4_5.c:223
int arm_init_arch_info(struct target *target, struct arm *arm)
Definition: armv4_5.c:1813
const char * arm_mode_name(unsigned int psr_mode)
Map PSR mode bits to the name of an ARM processor operating mode.
Definition: armv4_5.c:171
static const struct @75 arm_mode_data[]
void arm_set_cpsr(struct arm *arm, uint32_t cpsr)
Configures host-side ARM records to reflect the specified CPSR.
Definition: armv4_5.c:453
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, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Definition: armv4_5.c:1588
const char * feature
Definition: armv4_5.c:368
static int armv4_5_run_algorithm_completion(struct target *target, uint32_t exit_point, unsigned int timeout_ms, void *arch_info)
Definition: armv4_5.c:1373
static int arm_default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t crn, uint32_t crm, uint32_t value)
Definition: armv4_5.c:1796
static const uint8_t arm_gdb_dummy_fps_value[4]
Definition: armv4_5.c:564
uint32_t bits
Definition: armv4_5.c:364
#define ARMV4_5_CORE_REG_MODE(cache, mode, num)
Definition: armv4_5.h:32
@ ARMV4_5_CPSR
Definition: armv4_5.h:36
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
int breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:344
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:208
@ BKPT_HARD
Definition: breakpoints.h:18
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:371
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:166
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define COMMAND_PARSE_ADDRESS(in, out)
Definition: command.h:450
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:440
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:402
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
enum esirisc_reg_num number
Definition: esirisc.c:87
static uint16_t output
Definition: ftdi.c:119
static uint16_t direction
Definition: ftdi.c:120
uint64_t op
Definition: lakemont.c:68
static const struct @113 regs[]
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:160
#define LOG_TARGET_USER(target, fmt_str,...)
Definition: log.h:157
#define ERROR_FAIL
Definition: log.h:175
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:163
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:151
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define LOG_DEBUG(expr ...)
Definition: log.h:111
#define ERROR_OK
Definition: log.h:169
struct qn908x_flash_bank __attribute__
Definition: armv8.c:1054
void register_init_dummy(struct reg *reg)
Definition: register.c:123
struct reg * register_get_by_name(struct reg_cache *first, const char *name, bool search_all)
Definition: register.c:50
reg_type
Definition: register.h:19
@ REG_TYPE_INT
Definition: register.h:21
@ REG_TYPE_IEEE_DOUBLE
Definition: register.h:37
@ REG_TYPE_UINT32
Definition: register.h:30
@ REG_TYPE_CODE_PTR
Definition: register.h:33
@ REG_TYPE_DATA_PTR
Definition: register.h:34
struct target * target
Definition: rtt/rtt.c:26
const struct command_registration semihosting_common_handlers[]
unsigned int common_magic
Definition: arm.h:275
enum arm_mode core_mode
Definition: arm.h:277
enum arm_state core_state
Definition: arm.h:278
Definition: arm.h:281
int num
Definition: arm.h:282
struct arm * arm
Definition: arm.h:285
uint8_t value[16]
Definition: arm.h:286
enum arm_mode mode
Definition: arm.h:283
struct target * target
Definition: arm.h:284
Represents a generic ARM core, with standard application registers.
Definition: arm.h:176
int(* full_context)(struct target *target)
Retrieve all core registers, for display.
Definition: arm.h:222
enum arm_arch arch
ARM architecture version.
Definition: arm.h:203
int(* mrrc)(struct target *target, int cpnum, uint32_t op, uint32_t crm, uint64_t *value)
Read coprocessor to two registers.
Definition: arm.h:237
void * arch_info
Definition: arm.h:252
enum arm_core_type core_type
Indicates what registers are in the ARM state core register set.
Definition: arm.h:194
int(* mrc)(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t crn, uint32_t crm, uint32_t *value)
Read coprocessor register.
Definition: arm.h:231
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:197
struct reg * cpsr
Handle to the CPSR/xPSR; valid in all core modes.
Definition: arm.h:185
struct reg * pc
Handle to the PC; valid in all core modes.
Definition: arm.h:182
int(* write_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode, uint8_t *value)
Definition: arm.h:227
const int * map
Support for arm_reg_current()
Definition: arm.h:191
int(* mcrr)(struct target *target, int cpnum, uint32_t op, uint32_t crm, uint64_t value)
Write coprocessor from two registers.
Definition: arm.h:248
int(* read_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode)
Retrieve a single core register.
Definition: arm.h:225
struct reg_cache * core_cache
Definition: arm.h:179
int(* mcr)(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t crn, uint32_t crm, uint32_t value)
Write coprocessor register.
Definition: arm.h:242
struct reg * spsr
Handle to the SPSR; valid only in core modes with an SPSR.
Definition: arm.h:188
unsigned int common_magic
Definition: arm.h:177
int arm_vfp_version
Floating point or VFP version, 0 if disabled.
Definition: arm.h:206
struct target * target
Backpointer to the target.
Definition: arm.h:211
enum arm_state core_state
Record the current core state: ARM, Thumb, or otherwise.
Definition: arm.h:200
const char * name
Definition: command.h:234
int(* get)(struct reg *reg)
Definition: register.h:152
const char * name
Definition: register.h:145
unsigned int num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
struct reg_cache * next
Definition: register.h:146
enum reg_type type
Definition: register.h:100
const char * name
Definition: register.h:42
uint32_t size
Definition: algorithm.h:29
uint8_t * value
Definition: algorithm.h:30
const char * reg_name
Definition: algorithm.h:28
Definition: register.h:111
bool caller_save
Definition: register.h:119
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
const char * group
Definition: register.h:138
uint8_t * value
Definition: register.h:122
struct reg_feature * feature
Definition: register.h:117
struct reg_data_type * reg_data_type
Definition: register.h:135
uint32_t number
Definition: register.h:115
void * arch_info
Definition: register.h:140
bool dirty
Definition: register.h:124
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
bool hit_fileio
A flag reporting whether semihosting fileio operation is active.
bool is_fileio
A flag reporting whether semihosting fileio is active.
bool is_active
A flag reporting whether semihosting is active.
Definition: target.h:119
struct semihosting * semihosting
Definition: target.h:212
enum target_state state
Definition: target.h:160
void * arch_info
Definition: target.h:167
Definition: psoc6.c:83
target_addr_t address
Definition: target.h:89
int target_halt(struct target *target)
Definition: target.c:516
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2351
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2416
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:783
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2070
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2650
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2128
int target_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
Definition: target.c:565
const char * debug_reason_name(const struct target *t)
Definition: target.c:256
int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
Definition: target.c:3220
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:467
const char * target_type_name(const struct target *target)
Get the target type name.
Definition: target.c:746
target_register_class
Definition: target.h:113
@ REG_CLASS_GENERAL
Definition: target.h:115
@ REG_CLASS_ALL
Definition: target.h:114
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:786
static bool target_was_examined(const struct target *target)
Definition: target.h:432
#define ERROR_TARGET_INVALID
Definition: target.h:783
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:236
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:793
#define ERROR_TARGET_TIMEOUT
Definition: target.h:785
#define ERROR_TARGET_FAILURE
Definition: target.h:787
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
uint64_t target_addr_t
Definition: types.h:279
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
#define NULL
Definition: usb.h:16
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22