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