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 };
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_ERROR("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_WARNING("ThumbEE -- incomplete support");
488  } else
490  } else {
491  if (cpsr & (1 << 24)) { /* J */
492  LOG_ERROR("Jazelle state handling is BROKEN!");
494  } else
496  }
497  arm->core_state = state;
498 
499  LOG_DEBUG("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_ERROR("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_ERROR("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_DEBUG("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 {
663  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  int i;
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 (i = 0; i < num_core_regs; i++) {
685  /* Skip registers this core doesn't expose */
689  continue;
692  continue;
693 
694  /* REVISIT handle Cortex-M, which only shadows R13/SP */
695 
696  reg_arch_info[i].num = arm_core_regs[i].cookie;
697  reg_arch_info[i].mode = arm_core_regs[i].mode;
698  reg_arch_info[i].target = target;
699  reg_arch_info[i].arm = arm;
700 
701  reg_list[i].name = arm_core_regs[i].name;
702  reg_list[i].number = arm_core_regs[i].gdb_index;
703  reg_list[i].size = 32;
704  reg_list[i].value = reg_arch_info[i].value;
705  reg_list[i].type = &arm_reg_type;
706  reg_list[i].arch_info = &reg_arch_info[i];
707  reg_list[i].exist = true;
708 
709  /* This really depends on the calling convention in use */
710  reg_list[i].caller_save = false;
711 
712  /* Registers data type, as used by GDB target description */
713  reg_list[i].reg_data_type = malloc(sizeof(struct reg_data_type));
714  switch (arm_core_regs[i].cookie) {
715  case 13:
716  reg_list[i].reg_data_type->type = REG_TYPE_DATA_PTR;
717  break;
718  case 14:
719  case 15:
720  reg_list[i].reg_data_type->type = REG_TYPE_CODE_PTR;
721  break;
722  default:
723  reg_list[i].reg_data_type->type = REG_TYPE_UINT32;
724  break;
725  }
726 
727  /* let GDB shows banked registers only in "info all-reg" */
728  reg_list[i].feature = malloc(sizeof(struct reg_feature));
729  if (reg_list[i].number <= 15 || reg_list[i].number == 25) {
730  reg_list[i].feature->name = "org.gnu.gdb.arm.core";
731  reg_list[i].group = "general";
732  } else {
733  reg_list[i].feature->name = "net.sourceforge.openocd.banked";
734  reg_list[i].group = "banked";
735  }
736 
737  cache->num_regs++;
738  }
739 
740  int j;
741  for (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  reg_list[i].caller_save = false;
756 
757  reg_list[i].reg_data_type = malloc(sizeof(struct reg_data_type));
758  reg_list[i].reg_data_type->type = arm_vfp_v3_regs[j].type;
759 
760  reg_list[i].feature = malloc(sizeof(struct reg_feature));
761  reg_list[i].feature->name = arm_vfp_v3_regs[j].feature;
762 
763  reg_list[i].group = arm_vfp_v3_regs[j].group;
764 
765  cache->num_regs++;
766  }
767 
768  arm->pc = reg_list + 15;
769  arm->cpsr = reg_list + ARMV4_5_CPSR;
770  arm->core_cache = cache;
771 
772  return cache;
773 }
774 
776 {
777  if (!arm || !arm->core_cache)
778  return;
779 
780  struct reg_cache *cache = arm->core_cache;
781 
782  for (unsigned int i = 0; i < cache->num_regs; i++) {
783  struct reg *reg = &cache->reg_list[i];
784 
785  free(reg->feature);
786  free(reg->reg_data_type);
787  }
788 
789  free(cache->reg_list[0].arch_info);
790  free(cache->reg_list);
791  free(cache);
792 
793  arm->core_cache = NULL;
794 }
795 
797 {
798  struct arm *arm = target_to_arm(target);
799 
801  LOG_ERROR("BUG: called for a non-ARM target");
802  return ERROR_FAIL;
803  }
804 
805  /* avoid filling log waiting for fileio reply */
807  return ERROR_OK;
808 
809  LOG_USER("target halted in %s state due to %s, current mode: %s\n"
810  "cpsr: 0x%8.8" PRIx32 " pc: 0x%8.8" PRIx32 "%s%s",
814  buf_get_u32(arm->cpsr->value, 0, 32),
815  buf_get_u32(arm->pc->value, 0, 32),
816  (target->semihosting && target->semihosting->is_active) ? ", semihosting" : "",
817  (target->semihosting && target->semihosting->is_fileio) ? " fileio" : "");
818 
819  return ERROR_OK;
820 }
821 
822 COMMAND_HANDLER(handle_armv4_5_reg_command)
823 {
825  struct arm *arm = target_to_arm(target);
826  struct reg *regs;
827 
828  if (!is_arm(arm)) {
829  command_print(CMD, "current target isn't an ARM");
830  return ERROR_FAIL;
831  }
832 
833  if (target->state != TARGET_HALTED) {
834  command_print(CMD, "Error: target must be halted for register accesses");
836  }
837 
838  if (arm->core_type != ARM_CORE_TYPE_STD) {
840  "Microcontroller Profile not supported - use standard reg cmd");
841  return ERROR_OK;
842  }
843 
844  if (!is_arm_mode(arm->core_mode)) {
845  LOG_ERROR("not a valid arm core mode - communication failure?");
846  return ERROR_FAIL;
847  }
848 
849  if (!arm->full_context) {
850  command_print(CMD, "Error: target doesn't support %s",
851  CMD_NAME);
852  return ERROR_FAIL;
853  }
854 
856 
857  for (unsigned int mode = 0; mode < ARRAY_SIZE(arm_mode_data); mode++) {
858  const char *name;
859  char *sep = "\n";
860  char *shadow = "";
861 
863  continue;
864 
865  /* label this bank of registers (or shadows) */
866  switch (arm_mode_data[mode].psr) {
867  case ARM_MODE_SYS:
868  continue;
869  case ARM_MODE_USR:
870  name = "System and User";
871  sep = "";
872  break;
873  case ARM_MODE_HYP:
875  continue;
876  /* FALLTHROUGH */
877  case ARM_MODE_MON:
878  case ARM_MODE_1176_MON:
881  continue;
882  /* FALLTHROUGH */
883  default:
884  name = arm_mode_data[mode].name;
885  shadow = "shadow ";
886  break;
887  }
888  command_print(CMD, "%s%s mode %sregisters",
889  sep, name, shadow);
890 
891  /* display N rows of up to 4 registers each */
892  for (unsigned int i = 0; i < arm_mode_data[mode].n_indices; ) {
893  char output[80];
894  int output_len = 0;
895 
896  for (unsigned int j = 0; j < 4; j++, i++) {
897  uint32_t value;
898  struct reg *reg = regs;
899 
900  if (i >= arm_mode_data[mode].n_indices)
901  break;
902 
903  reg += arm_mode_data[mode].indices[i];
904 
905  /* REVISIT be smarter about faults... */
906  if (!reg->valid)
908 
909  value = buf_get_u32(reg->value, 0, 32);
910  output_len += snprintf(output + output_len,
911  sizeof(output) - output_len,
912  "%8s: %8.8" PRIx32 " ",
913  reg->name, value);
914  }
915  command_print(CMD, "%s", output);
916  }
917  }
918 
919  return ERROR_OK;
920 }
921 
922 COMMAND_HANDLER(handle_arm_core_state_command)
923 {
925  struct arm *arm = target_to_arm(target);
926  int ret = ERROR_OK;
927 
928  if (!is_arm(arm)) {
929  command_print(CMD, "current target isn't an ARM");
930  return ERROR_FAIL;
931  }
932 
933  if (CMD_ARGC > 0) {
934  if (strcmp(CMD_ARGV[0], "arm") == 0) {
936  command_print(CMD, "arm mode not supported on Cortex-M");
937  ret = ERROR_FAIL;
938  } else {
940  }
941  }
942  if (strcmp(CMD_ARGV[0], "thumb") == 0)
944  }
945 
946  command_print(CMD, "core state: %s", arm_core_state_string(arm));
947 
948  return ret;
949 }
950 
951 COMMAND_HANDLER(handle_arm_disassemble_command)
952 {
953 #if HAVE_CAPSTONE
955 
956  if (!target) {
957  LOG_ERROR("No target selected");
958  return ERROR_FAIL;
959  }
960 
961  struct arm *arm = target_to_arm(target);
962  target_addr_t address;
963  unsigned int count = 1;
964  bool thumb = false;
965 
966  if (!is_arm(arm)) {
967  command_print(CMD, "current target isn't an ARM");
968  return ERROR_FAIL;
969  }
970 
972  /* armv7m is always thumb mode */
973  thumb = true;
974  }
975 
976  switch (CMD_ARGC) {
977  case 3:
978  if (strcmp(CMD_ARGV[2], "thumb") != 0)
980  thumb = true;
981  /* FALL THROUGH */
982  case 2:
984  /* FALL THROUGH */
985  case 1:
986  COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
987  if (address & 0x01) {
988  if (!thumb) {
989  command_print(CMD, "Disassemble as Thumb");
990  thumb = true;
991  }
992  address &= ~1;
993  }
994  break;
995  default:
997  }
998 
999  return arm_disassemble(CMD, target, address, count, thumb);
1000 #else
1001  command_print(CMD, "capstone disassembly framework required");
1002  return ERROR_FAIL;
1003 #endif
1004 }
1005 
1006 COMMAND_HANDLER(handle_armv4_5_mcrmrc)
1007 {
1008  bool is_mcr = false;
1009  unsigned int arg_cnt = 5;
1010 
1011  if (!strcmp(CMD_NAME, "mcr")) {
1012  is_mcr = true;
1013  arg_cnt = 6;
1014  }
1015 
1016  if (arg_cnt != CMD_ARGC)
1018 
1020  if (!target) {
1021  command_print(CMD, "no current target");
1022  return ERROR_FAIL;
1023  }
1024  if (!target_was_examined(target)) {
1025  command_print(CMD, "%s: not yet examined", target_name(target));
1027  }
1028 
1029  struct arm *arm = target_to_arm(target);
1030  if (!is_arm(arm)) {
1031  command_print(CMD, "%s: not an ARM", target_name(target));
1032  return ERROR_FAIL;
1033  }
1034 
1035  if (target->state != TARGET_HALTED) {
1036  command_print(CMD, "Error: [%s] not halted", target_name(target));
1037  return ERROR_TARGET_NOT_HALTED;
1038  }
1039 
1040  int cpnum;
1041  uint32_t op1;
1042  uint32_t op2;
1043  uint32_t crn;
1044  uint32_t crm;
1045  uint32_t value;
1046 
1047  /* NOTE: parameter sequence matches ARM instruction set usage:
1048  * MCR pNUM, op1, rX, CRn, CRm, op2 ; write CP from rX
1049  * MRC pNUM, op1, rX, CRn, CRm, op2 ; read CP into rX
1050  * The "rX" is necessarily omitted; it uses Tcl mechanisms.
1051  */
1052  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], cpnum);
1053  if (cpnum & ~0xf) {
1054  command_print(CMD, "coprocessor %d out of range", cpnum);
1056  }
1057 
1058  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], op1);
1059  if (op1 & ~0x7) {
1060  command_print(CMD, "op1 %d out of range", op1);
1062  }
1063 
1064  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], crn);
1065  if (crn & ~0xf) {
1066  command_print(CMD, "CRn %d out of range", crn);
1068  }
1069 
1070  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], crm);
1071  if (crm & ~0xf) {
1072  command_print(CMD, "CRm %d out of range", crm);
1074  }
1075 
1076  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], op2);
1077  if (op2 & ~0x7) {
1078  command_print(CMD, "op2 %d out of range", op2);
1080  }
1081 
1082  /*
1083  * FIXME change the call syntax here ... simplest to just pass
1084  * the MRC() or MCR() instruction to be executed. That will also
1085  * let us support the "mrc2" and "mcr2" opcodes (toggling one bit)
1086  * if that's ever needed.
1087  */
1088  if (is_mcr) {
1089  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[5], value);
1090 
1091  /* NOTE: parameters reordered! */
1092  /* ARMV4_5_MCR(cpnum, op1, 0, crn, crm, op2) */
1093  int retval = arm->mcr(target, cpnum, op1, op2, crn, crm, value);
1094  if (retval != ERROR_OK)
1095  return retval;
1096  } else {
1097  value = 0;
1098  /* NOTE: parameters reordered! */
1099  /* ARMV4_5_MRC(cpnum, op1, 0, crn, crm, op2) */
1100  int retval = arm->mrc(target, cpnum, op1, op2, crn, crm, &value);
1101  if (retval != ERROR_OK)
1102  return retval;
1103 
1104  command_print(CMD, "0x%" PRIx32, value);
1105  }
1106 
1107  return ERROR_OK;
1108 }
1109 
1110 COMMAND_HANDLER(handle_armv4_5_mcrrmrrc)
1111 {
1112  bool is_mcrr = false;
1113  unsigned int arg_cnt = 3;
1114 
1115  if (!strcmp(CMD_NAME, "mcrr")) {
1116  is_mcrr = true;
1117  arg_cnt = 4;
1118  }
1119 
1120  if (arg_cnt != CMD_ARGC)
1122 
1124  if (!target) {
1125  command_print(CMD, "no current target");
1126  return ERROR_FAIL;
1127  }
1128  if (!target_was_examined(target)) {
1129  command_print(CMD, "%s: not yet examined", target_name(target));
1131  }
1132 
1133  struct arm *arm = target_to_arm(target);
1134  if (!is_arm(arm)) {
1135  command_print(CMD, "%s: not an ARM", target_name(target));
1136  return ERROR_FAIL;
1137  }
1138 
1139  if (target->state != TARGET_HALTED)
1140  return ERROR_TARGET_NOT_HALTED;
1141 
1142  int cpnum;
1143  uint32_t op1;
1144  uint32_t crm;
1145  uint64_t value;
1146 
1147  /* NOTE: parameter sequence matches ARM instruction set usage:
1148  * MCRR pNUM, op1, rX1, rX2, CRm ; write CP from rX1 and rX2
1149  * MREC pNUM, op1, rX1, rX2, CRm ; read CP into rX1 and rX2
1150  * The "rXn" are necessarily omitted; they use Tcl mechanisms.
1151  */
1152  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], cpnum);
1153  if (cpnum & ~0xf) {
1154  command_print(CMD, "coprocessor %d out of range", cpnum);
1156  }
1157 
1158  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], op1);
1159  if (op1 & ~0xf) {
1160  command_print(CMD, "op1 %d out of range", op1);
1162  }
1163 
1164  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], crm);
1165  if (crm & ~0xf) {
1166  command_print(CMD, "CRm %d out of range", crm);
1168  }
1169 
1170  /*
1171  * FIXME change the call syntax here ... simplest to just pass
1172  * the MRC() or MCR() instruction to be executed. That will also
1173  * let us support the "mrrc2" and "mcrr2" opcodes (toggling one bit)
1174  * if that's ever needed.
1175  */
1176  if (is_mcrr) {
1177  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[3], value);
1178 
1179  /* NOTE: parameters reordered! */
1180  /* ARMV5_T_MCRR(cpnum, op1, crm) */
1181  int retval = arm->mcrr(target, cpnum, op1, crm, value);
1182  if (retval != ERROR_OK)
1183  return retval;
1184  } else {
1185  value = 0;
1186  /* NOTE: parameters reordered! */
1187  /* ARMV5_T_MRRC(cpnum, op1, crm) */
1188  int retval = arm->mrrc(target, cpnum, op1, crm, &value);
1189  if (retval != ERROR_OK)
1190  return retval;
1191 
1192  command_print(CMD, "0x%" PRIx64, value);
1193  }
1194 
1195  return ERROR_OK;
1196 }
1197 
1198 static const struct command_registration arm_exec_command_handlers[] = {
1199  {
1200  .name = "reg",
1201  .handler = handle_armv4_5_reg_command,
1202  .mode = COMMAND_EXEC,
1203  .help = "display ARM core registers",
1204  .usage = "",
1205  },
1206  {
1207  .name = "mcr",
1208  .mode = COMMAND_EXEC,
1209  .handler = handle_armv4_5_mcrmrc,
1210  .help = "write coprocessor register",
1211  .usage = "cpnum op1 CRn CRm op2 value",
1212  },
1213  {
1214  .name = "mrc",
1215  .mode = COMMAND_EXEC,
1216  .handler = handle_armv4_5_mcrmrc,
1217  .help = "read coprocessor register",
1218  .usage = "cpnum op1 CRn CRm op2",
1219  },
1220  {
1221  .name = "mcrr",
1222  .mode = COMMAND_EXEC,
1223  .handler = handle_armv4_5_mcrrmrrc,
1224  .help = "write coprocessor 64-bit register",
1225  .usage = "cpnum op1 CRm value",
1226  },
1227  {
1228  .name = "mrrc",
1229  .mode = COMMAND_EXEC,
1230  .handler = handle_armv4_5_mcrrmrrc,
1231  .help = "read coprocessor 64-bit register",
1232  .usage = "cpnum op1 CRm",
1233  },
1234  {
1236  },
1238 };
1239 
1241  {
1242  .name = "core_state",
1243  .handler = handle_arm_core_state_command,
1244  .mode = COMMAND_EXEC,
1245  .usage = "['arm'|'thumb']",
1246  .help = "display/change ARM core state",
1247  },
1248  {
1249  .name = "disassemble",
1250  .handler = handle_arm_disassemble_command,
1251  .mode = COMMAND_EXEC,
1252  .usage = "address [count ['thumb']]",
1253  .help = "disassemble instructions",
1254  },
1255  {
1256  .chain = semihosting_common_handlers,
1257  },
1259 };
1260 
1261 const struct command_registration arm_command_handlers[] = {
1262  {
1263  .name = "arm",
1264  .mode = COMMAND_ANY,
1265  .help = "ARM command group",
1266  .usage = "",
1267  .chain = arm_exec_command_handlers,
1268  },
1270 };
1271 
1272 /*
1273  * gdb for arm targets (e.g. arm-none-eabi-gdb) supports several variants
1274  * of arm architecture. You can list them using the autocompletion of gdb
1275  * command prompt by typing "set architecture " and then press TAB key.
1276  * The default, selected automatically, is "arm".
1277  * Let's use the default value, here, to make gdb-multiarch behave in the
1278  * same way as a gdb for arm. This can be changed later on. User can still
1279  * set the specific architecture variant with the gdb command.
1280  */
1281 const char *arm_get_gdb_arch(const struct target *target)
1282 {
1283  return "arm";
1284 }
1285 
1287  struct reg **reg_list[], int *reg_list_size,
1288  enum target_register_class reg_class)
1289 {
1290  struct arm *arm = target_to_arm(target);
1291  unsigned int i;
1292 
1293  if (!is_arm_mode(arm->core_mode)) {
1294  LOG_ERROR("not a valid arm core mode - communication failure?");
1295  return ERROR_FAIL;
1296  }
1297 
1298  switch (reg_class) {
1299  case REG_CLASS_GENERAL:
1300  *reg_list_size = 26;
1301  *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
1302 
1303  for (i = 0; i < 16; i++)
1304  (*reg_list)[i] = arm_reg_current(arm, i);
1305 
1306  /* For GDB compatibility, take FPA registers size into account and zero-fill it*/
1307  for (i = 16; i < 24; i++)
1308  (*reg_list)[i] = &arm_gdb_dummy_fp_reg;
1309  (*reg_list)[24] = &arm_gdb_dummy_fps_reg;
1310 
1311  (*reg_list)[25] = arm->cpsr;
1312 
1313  return ERROR_OK;
1314 
1315  case REG_CLASS_ALL:
1316  switch (arm->core_type) {
1317  case ARM_CORE_TYPE_SEC_EXT:
1318  *reg_list_size = 51;
1319  break;
1321  *reg_list_size = 53;
1322  break;
1323  default:
1324  *reg_list_size = 48;
1325  }
1326  unsigned int list_size_core = *reg_list_size;
1327  if (arm->arm_vfp_version == ARM_VFP_V3)
1328  *reg_list_size += 33;
1329 
1330  *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
1331 
1332  for (i = 0; i < 16; i++)
1333  (*reg_list)[i] = arm_reg_current(arm, i);
1334 
1335  for (i = 13; i < ARRAY_SIZE(arm_core_regs); i++) {
1336  int reg_index = arm->core_cache->reg_list[i].number;
1337 
1338  if (arm_core_regs[i].mode == ARM_MODE_MON
1341  continue;
1342  if (arm_core_regs[i].mode == ARM_MODE_HYP
1344  continue;
1345  (*reg_list)[reg_index] = &(arm->core_cache->reg_list[i]);
1346  }
1347 
1348  /* When we supply the target description, there is no need for fake FPA */
1349  for (i = 16; i < 24; i++) {
1350  (*reg_list)[i] = &arm_gdb_dummy_fp_reg;
1351  (*reg_list)[i]->size = 0;
1352  }
1353  (*reg_list)[24] = &arm_gdb_dummy_fps_reg;
1354  (*reg_list)[24]->size = 0;
1355 
1356  if (arm->arm_vfp_version == ARM_VFP_V3) {
1357  unsigned int num_core_regs = ARRAY_SIZE(arm_core_regs);
1358  for (i = 0; i < 33; i++)
1359  (*reg_list)[list_size_core + i] = &(arm->core_cache->reg_list[num_core_regs + i]);
1360  }
1361 
1362  return ERROR_OK;
1363 
1364  default:
1365  LOG_ERROR("not a valid register class type in query.");
1366  return ERROR_FAIL;
1367  }
1368 }
1369 
1370 /* wait for execution to complete and check exit point */
1372  uint32_t exit_point,
1373  unsigned int timeout_ms,
1374  void *arch_info)
1375 {
1376  int retval;
1377  struct arm *arm = target_to_arm(target);
1378 
1379  retval = target_wait_state(target, TARGET_HALTED, timeout_ms);
1380  if (retval != ERROR_OK)
1381  return retval;
1382  if (target->state != TARGET_HALTED) {
1383  retval = target_halt(target);
1384  if (retval != ERROR_OK)
1385  return retval;
1386  retval = target_wait_state(target, TARGET_HALTED, 500);
1387  if (retval != ERROR_OK)
1388  return retval;
1389  return ERROR_TARGET_TIMEOUT;
1390  }
1391 
1392  /* fast exit: ARMv5+ code can use BKPT */
1393  if (exit_point && buf_get_u32(arm->pc->value, 0, 32) != exit_point) {
1394  LOG_WARNING(
1395  "target reentered debug state, but not at the desired exit point: 0x%4.4" PRIx32 "",
1396  buf_get_u32(arm->pc->value, 0, 32));
1397  return ERROR_TARGET_TIMEOUT;
1398  }
1399 
1400  return ERROR_OK;
1401 }
1402 
1404  int num_mem_params, struct mem_param *mem_params,
1405  int num_reg_params, struct reg_param *reg_params,
1406  uint32_t entry_point, uint32_t exit_point,
1407  unsigned int timeout_ms, void *arch_info,
1408  int (*run_it)(struct target *target, uint32_t exit_point,
1409  unsigned int timeout_ms, void *arch_info))
1410 {
1411  struct arm *arm = target_to_arm(target);
1412  struct arm_algorithm *arm_algorithm_info = arch_info;
1414  uint32_t context[17];
1415  uint32_t cpsr;
1416  int exit_breakpoint_size = 0;
1417  int i;
1418  int retval = ERROR_OK;
1419 
1420  LOG_DEBUG("Running algorithm");
1421 
1422  if (arm_algorithm_info->common_magic != ARM_COMMON_MAGIC) {
1423  LOG_ERROR("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_ERROR("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_ERROR("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 (i = 0; i <= 16; 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 (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 (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_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1475  }
1476 
1477  if (reg->size != reg_params[i].size) {
1478  LOG_ERROR("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_ERROR("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_DEBUG("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_ERROR("can't add HW breakpoint to terminate algorithm");
1513  return ERROR_TARGET_FAILURE;
1514  }
1515  }
1516 
1517  retval = target_resume(target, 0, entry_point, 1, 1);
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 (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 (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_ERROR("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) {
1551  LOG_ERROR(
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 (i = 0; i <= 16; 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_ERROR("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_ERROR("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_ERROR("%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_ERROR("%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_ERROR("%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_ERROR("%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, 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:37
@ 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:163
#define ARM_COMMON_MAGIC
Definition: arm.h:166
@ ARM_ARCH_V4
Definition: arm.h:55
static bool is_arm(struct arm *arm)
Definition: arm.h:267
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
static struct arm * target_to_arm(const struct target *target)
Convert target handle to generic ARM target state handle.
Definition: arm.h:261
@ ARM_VFP_V3_D14
Definition: arm.h:125
@ ARM_VFP_V3_D24
Definition: arm.h:135
@ ARM_VFP_V3_D9
Definition: arm.h:120
@ ARM_VFP_V3_D1
Definition: arm.h:112
@ ARM_VFP_V3_D17
Definition: arm.h:128
@ ARM_VFP_V3_D19
Definition: arm.h:130
@ ARM_VFP_V3_D4
Definition: arm.h:115
@ ARM_VFP_V3_D15
Definition: arm.h:126
@ ARM_VFP_V3_D10
Definition: arm.h:121
@ ARM_VFP_V3_D3
Definition: arm.h:114
@ ARM_VFP_V3_D31
Definition: arm.h:142
@ ARM_VFP_V3_D16
Definition: arm.h:127
@ ARM_VFP_V3_D22
Definition: arm.h:133
@ ARM_VFP_V3_D5
Definition: arm.h:116
@ ARM_VFP_V3_D18
Definition: arm.h:129
@ ARM_VFP_V3_D26
Definition: arm.h:137
@ ARM_VFP_V3_D7
Definition: arm.h:118
@ ARM_VFP_V3_D23
Definition: arm.h:134
@ ARM_VFP_V3_D21
Definition: arm.h:132
@ ARM_VFP_V3_D28
Definition: arm.h:139
@ ARM_VFP_V3_D2
Definition: arm.h:113
@ ARM_VFP_V3_D27
Definition: arm.h:138
@ ARM_VFP_V3_D29
Definition: arm.h:140
@ ARM_VFP_V3_D11
Definition: arm.h:122
@ ARM_VFP_V3_FPSCR
Definition: arm.h:143
@ ARM_VFP_V3_D20
Definition: arm.h:131
@ ARM_VFP_V3_D13
Definition: arm.h:124
@ ARM_VFP_V3_D12
Definition: arm.h:123
@ ARM_VFP_V3_D6
Definition: arm.h:117
@ ARM_VFP_V3_D8
Definition: arm.h:119
@ ARM_VFP_V3_D0
Definition: arm.h:111
@ ARM_VFP_V3_D30
Definition: arm.h:141
@ ARM_VFP_V3_D25
Definition: arm.h:136
arm_state
The PSR "T" and "J" bits define the mode of "classic ARM" cores.
Definition: arm.h:150
@ ARM_STATE_JAZELLE
Definition: arm.h:153
@ ARM_STATE_THUMB
Definition: arm.h:152
@ ARM_STATE_ARM
Definition: arm.h:151
@ ARM_STATE_AARCH64
Definition: arm.h:155
@ ARM_STATE_THUMB_EE
Definition: arm.h:154
@ 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: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:55
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:51
const struct command_registration arm_all_profiles_command_handlers[]
Definition: armv4_5.c:1240
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: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:1403
int arm_arch_state(struct target *target)
Definition: armv4_5.c:796
static const char * arm_core_state_string(struct arm *arm)
Definition: armv4_5.c:437
static const struct @71 arm_mode_data[]
enum arm_mode mode
Definition: armv4_5.c:281
unsigned int gdb_index
Definition: armv4_5.c:280
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
@ 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
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:552
static const char * arm_state_strings[]
Definition: armv4_5.c:250
COMMAND_HANDLER(handle_armv4_5_reg_command)
Definition: armv4_5.c:822
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:660
const char * arm_get_gdb_arch(const struct target *target)
Definition: armv4_5.c:1281
static const uint8_t arm_hyp_indices[2]
Definition: armv4_5.c:71
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:1286
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: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:588
const char * name
Definition: armv4_5.c:76
void arm_free_reg_cache(struct arm *arm)
Definition: armv4_5.c:775
static const uint8_t arm_usr_indices[17]
Definition: armv4_5.c:43
static const struct @72 arm_core_regs[]
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:67
unsigned int id
Definition: armv4_5.c:361
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:1198
const struct command_registration arm_command_handlers[]
Definition: armv4_5.c:1261
static void arm_gdb_dummy_init(void)
Definition: armv4_5.c:580
unsigned int cookie
Definition: armv4_5.c:279
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
static const struct @73 arm_vfp_v3_regs[]
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
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:1588
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:1371
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: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: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:443
#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:452
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#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:442
#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:253
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:404
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
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
static const struct @109 regs[]
uint64_t op
Definition: lakemont.c:68
#define LOG_USER(expr ...)
Definition: log.h:135
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:173
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:167
struct qn908x_flash_bank __attribute__
Definition: armv8.c:1016
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
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
const struct command_registration semihosting_common_handlers[]
unsigned int common_magic
Definition: arm.h:274
enum arm_mode core_mode
Definition: arm.h:276
enum arm_state core_state
Definition: arm.h:277
Definition: arm.h:280
int num
Definition: arm.h:281
struct arm * arm
Definition: arm.h:284
uint8_t value[16]
Definition: arm.h:285
enum arm_mode mode
Definition: arm.h:282
struct target * target
Definition: arm.h:283
Represents a generic ARM core, with standard application registers.
Definition: arm.h:175
int(* full_context)(struct target *target)
Retrieve all core registers, for display.
Definition: arm.h:221
enum arm_arch arch
ARM architecture version.
Definition: arm.h:202
int(* mrrc)(struct target *target, int cpnum, uint32_t op, uint32_t crm, uint64_t *value)
Read coprocessor to two registers.
Definition: arm.h:236
void * arch_info
Definition: arm.h:251
enum arm_core_type core_type
Indicates what registers are in the ARM state core register set.
Definition: arm.h:193
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:230
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:196
struct reg * cpsr
Handle to the CPSR/xPSR; valid in all core modes.
Definition: arm.h:184
struct reg * pc
Handle to the PC; valid in all core modes.
Definition: arm.h:181
int(* write_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode, uint8_t *value)
Definition: arm.h:226
const int * map
Support for arm_reg_current()
Definition: arm.h:190
int(* mcrr)(struct target *target, int cpnum, uint32_t op, uint32_t crm, uint64_t value)
Write coprocessor from two registers.
Definition: arm.h:247
int(* read_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode)
Retrieve a single core register.
Definition: arm.h:224
struct reg_cache * core_cache
Definition: arm.h:178
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:241
struct reg * spsr
Handle to the SPSR; valid only in core modes with an SPSR.
Definition: arm.h:187
unsigned int common_magic
Definition: arm.h:176
int arm_vfp_version
Floating point or VFP version, 0 if disabled.
Definition: arm.h:205
struct target * target
Backpointer to the target.
Definition: arm.h:210
enum arm_state core_state
Record the current core state: ARM, Thumb, or otherwise.
Definition: arm.h:199
const char * name
Definition: command.h:235
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:116
struct semihosting * semihosting
Definition: target.h:209
enum target_state state
Definition: target.h:157
void * arch_info
Definition: target.h:164
Definition: psoc6.c:83
target_addr_t address
Definition: target.h:86
int target_halt(struct target *target)
Definition: target.c:507
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2342
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2407
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:773
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2060
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2641
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2118
const char * debug_reason_name(const struct target *t)
Definition: target.c:247
int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
Definition: target.c:3214
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:458
const char * target_type_name(const struct target *target)
Get the target type name.
Definition: target.c:736
int target_resume(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
Definition: target.c:556
target_register_class
Definition: target.h:110
@ REG_CLASS_GENERAL
Definition: target.h:112
@ REG_CLASS_ALL
Definition: target.h:111
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:790
static bool target_was_examined(const struct target *target)
Definition: target.h:436
#define ERROR_TARGET_INVALID
Definition: target.h:787
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:233
@ TARGET_HALTED
Definition: target.h:56
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:797
#define ERROR_TARGET_TIMEOUT
Definition: target.h:789
#define ERROR_TARGET_FAILURE
Definition: target.h:791
#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:335
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