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