aarch64: consolidate sticky error handling
[fw/openocd] / src / target / armv8_dpm.c
1 /*
2  * Copyright (C) 2009 by David Brownell
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19
20 #include "arm.h"
21 #include "armv8.h"
22 #include "armv8_dpm.h"
23 #include <jtag/jtag.h>
24 #include "register.h"
25 #include "breakpoints.h"
26 #include "target_type.h"
27 #include "armv8_opcodes.h"
28
29 #include "helper/time_support.h"
30
31 /* T32 ITR format */
32 #define T32_FMTITR(instr) (((instr & 0x0000FFFF) << 16) | ((instr & 0xFFFF0000) >> 16))
33
34 /**
35  * @file
36  * Implements various ARM DPM operations using architectural debug registers.
37  * These routines layer over core-specific communication methods to cope with
38  * implementation differences between cores like ARM1136 and Cortex-A8.
39  *
40  * The "Debug Programmers' Model" (DPM) for ARMv6 and ARMv7 is defined by
41  * Part C (Debug Architecture) of the ARM Architecture Reference Manual,
42  * ARMv7-A and ARMv7-R edition (ARM DDI 0406B).  In OpenOCD, DPM operations
43  * are abstracted through internal programming interfaces to share code and
44  * to minimize needless differences in debug behavior between cores.
45  */
46
47 /**
48  * Get core state from EDSCR, without necessity to retrieve CPSR
49  */
50 enum arm_state armv8_dpm_get_core_state(struct arm_dpm *dpm)
51 {
52         int el = (dpm->dscr >> 8) & 0x3;
53         int rw = (dpm->dscr >> 10) & 0xF;
54         int pos;
55
56         dpm->last_el = el;
57
58         /* find the first '0' in DSCR.RW */
59         for (pos = 3; pos >= 0; pos--) {
60                 if ((rw & (1 << pos)) == 0)
61                         break;
62         }
63
64         if (el > pos)
65                 return ARM_STATE_AARCH64;
66
67         return ARM_STATE_ARM;
68 }
69
70 /*----------------------------------------------------------------------*/
71
72 static int dpmv8_write_dcc(struct armv8_common *armv8, uint32_t data)
73 {
74         LOG_DEBUG("write DCC 0x%08" PRIx32, data);
75         return mem_ap_write_u32(armv8->debug_ap,
76                                 armv8->debug_base + CPUV8_DBG_DTRRX, data);
77 }
78
79 static int dpmv8_write_dcc_64(struct armv8_common *armv8, uint64_t data)
80 {
81         int ret;
82         LOG_DEBUG("write DCC Low word 0x%08" PRIx32, (unsigned)data);
83         LOG_DEBUG("write DCC High word 0x%08" PRIx32, (unsigned)(data >> 32));
84         ret = mem_ap_write_u32(armv8->debug_ap,
85                                armv8->debug_base + CPUV8_DBG_DTRRX, data);
86         ret += mem_ap_write_u32(armv8->debug_ap,
87                                 armv8->debug_base + CPUV8_DBG_DTRTX, data >> 32);
88         return ret;
89 }
90
91 static int dpmv8_read_dcc(struct armv8_common *armv8, uint32_t *data,
92         uint32_t *dscr_p)
93 {
94         uint32_t dscr = DSCR_ITE;
95         int retval;
96
97         if (dscr_p)
98                 dscr = *dscr_p;
99
100         /* Wait for DTRRXfull */
101         long long then = timeval_ms();
102         while ((dscr & DSCR_DTR_TX_FULL) == 0) {
103                 retval = mem_ap_read_atomic_u32(armv8->debug_ap,
104                                 armv8->debug_base + CPUV8_DBG_DSCR,
105                                 &dscr);
106                 if (retval != ERROR_OK)
107                         return retval;
108                 if (timeval_ms() > then + 1000) {
109                         LOG_ERROR("Timeout waiting for read dcc");
110                         return ERROR_FAIL;
111                 }
112         }
113
114         retval = mem_ap_read_atomic_u32(armv8->debug_ap,
115                                             armv8->debug_base + CPUV8_DBG_DTRTX,
116                                             data);
117         if (retval != ERROR_OK)
118                 return retval;
119         LOG_DEBUG("read DCC 0x%08" PRIx32, *data);
120
121         if (dscr_p)
122                 *dscr_p = dscr;
123
124         return retval;
125 }
126
127 static int dpmv8_read_dcc_64(struct armv8_common *armv8, uint64_t *data,
128         uint32_t *dscr_p)
129 {
130         uint32_t dscr = DSCR_ITE;
131         uint32_t higher;
132         int retval;
133
134         if (dscr_p)
135                 dscr = *dscr_p;
136
137         /* Wait for DTRRXfull */
138         long long then = timeval_ms();
139         while ((dscr & DSCR_DTR_TX_FULL) == 0) {
140                 retval = mem_ap_read_atomic_u32(armv8->debug_ap,
141                                 armv8->debug_base + CPUV8_DBG_DSCR,
142                                 &dscr);
143                 if (retval != ERROR_OK)
144                         return retval;
145                 if (timeval_ms() > then + 1000) {
146                         LOG_ERROR("Timeout waiting for read dcc");
147                         return ERROR_FAIL;
148                 }
149         }
150
151         retval = mem_ap_read_atomic_u32(armv8->debug_ap,
152                                             armv8->debug_base + CPUV8_DBG_DTRTX,
153                                             (uint32_t *)data);
154         if (retval != ERROR_OK)
155                 return retval;
156
157         retval = mem_ap_read_atomic_u32(armv8->debug_ap,
158                                             armv8->debug_base + CPUV8_DBG_DTRRX,
159                                             &higher);
160         if (retval != ERROR_OK)
161                 return retval;
162
163         *data = *(uint32_t *)data | (uint64_t)higher << 32;
164         LOG_DEBUG("read DCC 0x%16.16" PRIx64, *data);
165
166         if (dscr_p)
167                 *dscr_p = dscr;
168
169         return retval;
170 }
171
172 static int dpmv8_dpm_prepare(struct arm_dpm *dpm)
173 {
174         struct armv8_common *armv8 = dpm->arm->arch_info;
175         uint32_t dscr;
176         int retval;
177
178         /* set up invariant:  INSTR_COMP is set after ever DPM operation */
179         long long then = timeval_ms();
180         for (;; ) {
181                 retval = mem_ap_read_atomic_u32(armv8->debug_ap,
182                                 armv8->debug_base + CPUV8_DBG_DSCR,
183                                 &dscr);
184                 if (retval != ERROR_OK)
185                         return retval;
186                 if ((dscr & DSCR_ITE) != 0)
187                         break;
188                 if (timeval_ms() > then + 1000) {
189                         LOG_ERROR("Timeout waiting for dpm prepare");
190                         return ERROR_FAIL;
191                 }
192         }
193
194         /* update the stored copy of dscr */
195         dpm->dscr = dscr;
196
197         /* this "should never happen" ... */
198         if (dscr & DSCR_DTR_RX_FULL) {
199                 LOG_ERROR("DSCR_DTR_RX_FULL, dscr 0x%08" PRIx32, dscr);
200                 /* Clear DCCRX */
201                 retval = mem_ap_read_u32(armv8->debug_ap,
202                         armv8->debug_base + CPUV8_DBG_DTRRX, &dscr);
203                 if (retval != ERROR_OK)
204                         return retval;
205         }
206
207         return retval;
208 }
209
210 static int dpmv8_dpm_finish(struct arm_dpm *dpm)
211 {
212         /* REVISIT what could be done here? */
213         return ERROR_OK;
214 }
215
216 static int dpmv8_exec_opcode(struct arm_dpm *dpm,
217         uint32_t opcode, uint32_t *p_dscr)
218 {
219         struct armv8_common *armv8 = dpm->arm->arch_info;
220         uint32_t dscr = DSCR_ITE;
221         int retval;
222
223         LOG_DEBUG("exec opcode 0x%08" PRIx32, opcode);
224
225         if (p_dscr)
226                 dscr = *p_dscr;
227
228         /* Wait for InstrCompl bit to be set */
229         long long then = timeval_ms();
230         while ((dscr & DSCR_ITE) == 0) {
231                 retval = mem_ap_read_atomic_u32(armv8->debug_ap,
232                                 armv8->debug_base + CPUV8_DBG_DSCR, &dscr);
233                 if (retval != ERROR_OK) {
234                         LOG_ERROR("Could not read DSCR register, opcode = 0x%08" PRIx32, opcode);
235                         return retval;
236                 }
237                 if (timeval_ms() > then + 1000) {
238                         LOG_ERROR("Timeout waiting for aarch64_exec_opcode");
239                         return ERROR_FAIL;
240                 }
241         }
242
243         if (armv8_dpm_get_core_state(dpm) != ARM_STATE_AARCH64)
244                 opcode = T32_FMTITR(opcode);
245
246         retval = mem_ap_write_u32(armv8->debug_ap,
247                         armv8->debug_base + CPUV8_DBG_ITR, opcode);
248         if (retval != ERROR_OK)
249                 return retval;
250
251         then = timeval_ms();
252         do {
253                 retval = mem_ap_read_atomic_u32(armv8->debug_ap,
254                                 armv8->debug_base + CPUV8_DBG_DSCR, &dscr);
255                 if (retval != ERROR_OK) {
256                         LOG_ERROR("Could not read DSCR register");
257                         return retval;
258                 }
259                 if (timeval_ms() > then + 1000) {
260                         LOG_ERROR("Timeout waiting for aarch64_exec_opcode");
261                         return ERROR_FAIL;
262                 }
263         } while ((dscr & DSCR_ITE) == 0);       /* Wait for InstrCompl bit to be set */
264
265         /* update dscr and el after each command execution */
266         dpm->dscr = dscr;
267         if (dpm->last_el != ((dscr >> 8) & 3))
268                 LOG_DEBUG("EL %i -> %i", dpm->last_el, (dscr >> 8) & 3);
269         dpm->last_el = (dscr >> 8) & 3;
270
271         if (dscr & DSCR_ERR) {
272                 LOG_ERROR("Opcode 0x%08"PRIx32", DSCR.ERR=1, DSCR.EL=%i", opcode, dpm->last_el);
273                 armv8_dpm_handle_exception(dpm);
274                 retval = ERROR_FAIL;
275         }
276
277         if (p_dscr)
278                 *p_dscr = dscr;
279
280         return retval;
281 }
282
283 static int dpmv8_instr_execute(struct arm_dpm *dpm, uint32_t opcode)
284 {
285         return dpmv8_exec_opcode(dpm, opcode, NULL);
286 }
287
288 static int dpmv8_instr_write_data_dcc(struct arm_dpm *dpm,
289         uint32_t opcode, uint32_t data)
290 {
291         struct armv8_common *armv8 = dpm->arm->arch_info;
292         int retval;
293
294         retval = dpmv8_write_dcc(armv8, data);
295         if (retval != ERROR_OK)
296                 return retval;
297
298         return dpmv8_exec_opcode(dpm, opcode, 0);
299 }
300
301 static int dpmv8_instr_write_data_dcc_64(struct arm_dpm *dpm,
302         uint32_t opcode, uint64_t data)
303 {
304         struct armv8_common *armv8 = dpm->arm->arch_info;
305         int retval;
306
307         retval = dpmv8_write_dcc_64(armv8, data);
308         if (retval != ERROR_OK)
309                 return retval;
310
311         return dpmv8_exec_opcode(dpm, opcode, 0);
312 }
313
314 static int dpmv8_instr_write_data_r0(struct arm_dpm *dpm,
315         uint32_t opcode, uint32_t data)
316 {
317         struct armv8_common *armv8 = dpm->arm->arch_info;
318         uint32_t dscr = DSCR_ITE;
319         int retval;
320
321         retval = dpmv8_write_dcc(armv8, data);
322         if (retval != ERROR_OK)
323                 return retval;
324
325         retval = dpmv8_exec_opcode(dpm, armv8_opcode(armv8, READ_REG_DTRRX), &dscr);
326         if (retval != ERROR_OK)
327                 return retval;
328
329         /* then the opcode, taking data from R0 */
330         return dpmv8_exec_opcode(dpm, opcode, &dscr);
331 }
332
333 static int dpmv8_instr_write_data_r0_64(struct arm_dpm *dpm,
334         uint32_t opcode, uint64_t data)
335 {
336         struct armv8_common *armv8 = dpm->arm->arch_info;
337         uint32_t dscr = DSCR_ITE;
338         int retval;
339
340         retval = dpmv8_write_dcc_64(armv8, data);
341         if (retval != ERROR_OK)
342                 return retval;
343
344         retval = dpmv8_exec_opcode(dpm, ARMV8_MRS(SYSTEM_DBG_DBGDTR_EL0, 0), &dscr);
345         if (retval != ERROR_OK)
346                 return retval;
347
348         /* then the opcode, taking data from R0 */
349         return dpmv8_exec_opcode(dpm, opcode, &dscr);
350 }
351
352 static int dpmv8_instr_cpsr_sync(struct arm_dpm *dpm)
353 {
354         int retval;
355         struct armv8_common *armv8 = dpm->arm->arch_info;
356
357         /* "Prefetch flush" after modifying execution status in CPSR */
358         retval = dpmv8_exec_opcode(dpm, armv8_opcode(armv8, ARMV8_OPC_DSB_SY), &dpm->dscr);
359         if (retval == ERROR_OK)
360                 dpmv8_exec_opcode(dpm, armv8_opcode(armv8, ARMV8_OPC_ISB_SY), &dpm->dscr);
361         return retval;
362 }
363
364 static int dpmv8_instr_read_data_dcc(struct arm_dpm *dpm,
365         uint32_t opcode, uint32_t *data)
366 {
367         struct armv8_common *armv8 = dpm->arm->arch_info;
368         uint32_t dscr = DSCR_ITE;
369         int retval;
370
371         /* the opcode, writing data to DCC */
372         retval = dpmv8_exec_opcode(dpm, opcode, &dscr);
373         if (retval != ERROR_OK)
374                 return retval;
375
376         return dpmv8_read_dcc(armv8, data, &dscr);
377 }
378
379 static int dpmv8_instr_read_data_dcc_64(struct arm_dpm *dpm,
380         uint32_t opcode, uint64_t *data)
381 {
382         struct armv8_common *armv8 = dpm->arm->arch_info;
383         uint32_t dscr = DSCR_ITE;
384         int retval;
385
386         /* the opcode, writing data to DCC */
387         retval = dpmv8_exec_opcode(dpm, opcode, &dscr);
388         if (retval != ERROR_OK)
389                 return retval;
390
391         return dpmv8_read_dcc_64(armv8, data, &dscr);
392 }
393
394 static int dpmv8_instr_read_data_r0(struct arm_dpm *dpm,
395         uint32_t opcode, uint32_t *data)
396 {
397         struct armv8_common *armv8 = dpm->arm->arch_info;
398         uint32_t dscr = DSCR_ITE;
399         int retval;
400
401         /* the opcode, writing data to R0 */
402         retval = dpmv8_exec_opcode(dpm, opcode, &dscr);
403         if (retval != ERROR_OK)
404                 return retval;
405
406         /* write R0 to DCC */
407         retval = dpmv8_exec_opcode(dpm, armv8_opcode(armv8, WRITE_REG_DTRTX), &dscr);
408         if (retval != ERROR_OK)
409                 return retval;
410
411         return dpmv8_read_dcc(armv8, data, &dscr);
412 }
413
414 static int dpmv8_instr_read_data_r0_64(struct arm_dpm *dpm,
415         uint32_t opcode, uint64_t *data)
416 {
417         struct armv8_common *armv8 = dpm->arm->arch_info;
418         uint32_t dscr = DSCR_ITE;
419         int retval;
420
421         /* the opcode, writing data to R0 */
422         retval = dpmv8_exec_opcode(dpm, opcode, &dscr);
423         if (retval != ERROR_OK)
424                 return retval;
425
426         /* write R0 to DCC */
427         retval = dpmv8_exec_opcode(dpm, ARMV8_MSR_GP(SYSTEM_DBG_DBGDTR_EL0, 0), &dscr);
428         if (retval != ERROR_OK)
429                 return retval;
430
431         return dpmv8_read_dcc_64(armv8, data, &dscr);
432 }
433
434 #if 0
435 static int dpmv8_bpwp_enable(struct arm_dpm *dpm, unsigned index_t,
436         target_addr_t addr, uint32_t control)
437 {
438         struct armv8_common *armv8 = dpm->arm->arch_info;
439         uint32_t vr = armv8->debug_base;
440         uint32_t cr = armv8->debug_base;
441         int retval;
442
443         switch (index_t) {
444                 case 0 ... 15:  /* breakpoints */
445                         vr += CPUV8_DBG_BVR_BASE;
446                         cr += CPUV8_DBG_BCR_BASE;
447                         break;
448                 case 16 ... 31: /* watchpoints */
449                         vr += CPUV8_DBG_WVR_BASE;
450                         cr += CPUV8_DBG_WCR_BASE;
451                         index_t -= 16;
452                         break;
453                 default:
454                         return ERROR_FAIL;
455         }
456         vr += 16 * index_t;
457         cr += 16 * index_t;
458
459         LOG_DEBUG("A8: bpwp enable, vr %08x cr %08x",
460                 (unsigned) vr, (unsigned) cr);
461
462         retval = mem_ap_write_atomic_u32(armv8->debug_ap, vr, addr);
463         if (retval != ERROR_OK)
464                 return retval;
465         return mem_ap_write_atomic_u32(armv8->debug_ap, cr, control);
466 }
467 #endif
468
469 static int dpmv8_bpwp_disable(struct arm_dpm *dpm, unsigned index_t)
470 {
471         struct armv8_common *armv8 = dpm->arm->arch_info;
472         uint32_t cr;
473
474         switch (index_t) {
475                 case 0 ... 15:
476                         cr = armv8->debug_base + CPUV8_DBG_BCR_BASE;
477                         break;
478                 case 16 ... 31:
479                         cr = armv8->debug_base + CPUV8_DBG_WCR_BASE;
480                         index_t -= 16;
481                         break;
482                 default:
483                         return ERROR_FAIL;
484         }
485         cr += 16 * index_t;
486
487         LOG_DEBUG("A: bpwp disable, cr %08x", (unsigned) cr);
488
489         /* clear control register */
490         return mem_ap_write_atomic_u32(armv8->debug_ap, cr, 0);
491 }
492
493 /*
494  * Coprocessor support
495  */
496
497 /* Read coprocessor */
498 static int dpmv8_mrc(struct target *target, int cpnum,
499         uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm,
500         uint32_t *value)
501 {
502         struct arm *arm = target_to_arm(target);
503         struct arm_dpm *dpm = arm->dpm;
504         int retval;
505
506         retval = dpm->prepare(dpm);
507         if (retval != ERROR_OK)
508                 return retval;
509
510         LOG_DEBUG("MRC p%d, %d, r0, c%d, c%d, %d", cpnum,
511                 (int) op1, (int) CRn,
512                 (int) CRm, (int) op2);
513
514         /* read coprocessor register into R0; return via DCC */
515         retval = dpm->instr_read_data_r0(dpm,
516                         ARMV4_5_MRC(cpnum, op1, 0, CRn, CRm, op2),
517                         value);
518
519         /* (void) */ dpm->finish(dpm);
520         return retval;
521 }
522
523 static int dpmv8_mcr(struct target *target, int cpnum,
524         uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm,
525         uint32_t value)
526 {
527         struct arm *arm = target_to_arm(target);
528         struct arm_dpm *dpm = arm->dpm;
529         int retval;
530
531         retval = dpm->prepare(dpm);
532         if (retval != ERROR_OK)
533                 return retval;
534
535         LOG_DEBUG("MCR p%d, %d, r0, c%d, c%d, %d", cpnum,
536                 (int) op1, (int) CRn,
537                 (int) CRm, (int) op2);
538
539         /* read DCC into r0; then write coprocessor register from R0 */
540         retval = dpm->instr_write_data_r0(dpm,
541                         ARMV4_5_MCR(cpnum, op1, 0, CRn, CRm, op2),
542                         value);
543
544         /* (void) */ dpm->finish(dpm);
545         return retval;
546 }
547
548 static int dpmv8_mrs(struct target *target, uint32_t op0,
549         uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm,
550         uint32_t *value)
551 {
552         struct arm *arm = target_to_arm(target);
553         struct arm_dpm *dpm = arm->dpm;
554         int retval;
555         uint32_t op_code;
556
557         retval = dpm->prepare(dpm);
558         if (retval != ERROR_OK)
559                 return retval;
560         op_code = ((op0 & 0x3) << 19 | (op1 & 0x7) << 16 | (CRn & 0xF) << 12 |\
561                                 (CRm & 0xF) << 8 | (op2 & 0x7) << 5);
562         op_code >>= 5;
563         LOG_DEBUG("MRS p%d, %d, r0, c%d, c%d, %d", (int)op0,
564                 (int) op1, (int) CRn,
565                 (int) CRm, (int) op2);
566         /* read coprocessor register into R0; return via DCC */
567         retval = dpm->instr_read_data_r0(dpm,
568                         ARMV8_MRS(op_code, 0),
569                         value);
570
571         /* (void) */ dpm->finish(dpm);
572         return retval;
573 }
574
575 static int dpmv8_msr(struct target *target, uint32_t op0,
576         uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm,
577         uint32_t value)
578 {
579         struct arm *arm = target_to_arm(target);
580         struct arm_dpm *dpm = arm->dpm;
581         int retval;
582         uint32_t op_code;
583
584         retval = dpm->prepare(dpm);
585         if (retval != ERROR_OK)
586                 return retval;
587
588         op_code = ((op0 & 0x3) << 19 | (op1 & 0x7) << 16 | (CRn & 0xF) << 12 |\
589                                 (CRm & 0xF) << 8 | (op2 & 0x7) << 5);
590         op_code >>= 5;
591         LOG_DEBUG("MSR p%d, %d, r0, c%d, c%d, %d", (int)op0,
592                 (int) op1, (int) CRn,
593                 (int) CRm, (int) op2);
594
595         /* read DCC into r0; then write coprocessor register from R0 */
596         retval = dpm->instr_write_data_r0(dpm,
597                         ARMV8_MSR_GP(op_code, 0),
598                         value);
599
600         /* (void) */ dpm->finish(dpm);
601         return retval;
602 }
603
604 /*----------------------------------------------------------------------*/
605
606 /*
607  * Register access utilities
608  */
609
610 int armv8_dpm_modeswitch(struct arm_dpm *dpm, enum arm_mode mode)
611 {
612         struct armv8_common *armv8 = (struct armv8_common *)dpm->arm->arch_info;
613         int retval = ERROR_OK;
614         unsigned int target_el;
615         enum arm_state core_state;
616         uint32_t cpsr;
617
618         /* restore previous mode */
619         if (mode == ARM_MODE_ANY) {
620                 cpsr = buf_get_u32(dpm->arm->cpsr->value, 0, 32);
621
622                 LOG_DEBUG("restoring mode, cpsr = 0x%08"PRIx32, cpsr);
623
624         } else {
625                 LOG_DEBUG("setting mode 0x%"PRIx32, mode);
626
627                 /* else force to the specified mode */
628                 if (is_arm_mode(mode))
629                         cpsr = mode;
630                 else
631                         cpsr = mode >> 4;
632         }
633
634         switch (cpsr & 0x1f) {
635         /* aarch32 modes */
636         case ARM_MODE_USR:
637                 target_el = 0;
638                 break;
639         case ARM_MODE_SVC:
640         case ARM_MODE_ABT:
641         case ARM_MODE_IRQ:
642         case ARM_MODE_FIQ:
643                 target_el = 1;
644                 break;
645         /*
646          * TODO: handle ARM_MODE_HYP
647          * case ARM_MODE_HYP:
648          *      target_el = 2;
649          *      break;
650          */
651         case ARM_MODE_MON:
652                 target_el = 3;
653                 break;
654         /* aarch64 modes */
655         default:
656                 target_el = (cpsr >> 2) & 3;
657         }
658
659         if (target_el > SYSTEM_CUREL_EL3) {
660                 LOG_ERROR("%s: Invalid target exception level %i", __func__, target_el);
661                 return ERROR_FAIL;
662         }
663
664         LOG_DEBUG("target_el = %i, last_el = %i", target_el, dpm->last_el);
665         if (target_el > dpm->last_el) {
666                 retval = dpm->instr_execute(dpm,
667                                 armv8_opcode(armv8, ARMV8_OPC_DCPS) | target_el);
668
669                 /* DCPS clobbers registers just like an exception taken */
670                 armv8_dpm_handle_exception(dpm);
671         } else {
672                 core_state = armv8_dpm_get_core_state(dpm);
673                 if (core_state != ARM_STATE_AARCH64) {
674                         /* cannot do DRPS/ERET when already in EL0 */
675                         if (dpm->last_el != 0) {
676                                 /* load SPSR with the desired mode and execute DRPS */
677                                 LOG_DEBUG("SPSR = 0x%08"PRIx32, cpsr);
678                                 retval = dpm->instr_write_data_r0(dpm,
679                                                 ARMV8_MSR_GP_xPSR_T1(1, 0, 15), cpsr);
680                                 if (retval == ERROR_OK)
681                                         retval = dpm->instr_execute(dpm, armv8_opcode(armv8, ARMV8_OPC_DRPS));
682                         }
683                 } else {
684                         /*
685                          * need to execute multiple DRPS instructions until target_el
686                          * is reached
687                          */
688                         while (retval == ERROR_OK && dpm->last_el != target_el) {
689                                 unsigned int cur_el = dpm->last_el;
690                                 retval = dpm->instr_execute(dpm, armv8_opcode(armv8, ARMV8_OPC_DRPS));
691                                 if (cur_el == dpm->last_el) {
692                                         LOG_INFO("Cannot reach EL %i, SPSR corrupted?", target_el);
693                                         break;
694                                 }
695                         }
696                 }
697
698                 /* On executing DRPS, DSPSR and DLR become UNKNOWN, mark them as dirty */
699                 dpm->arm->cpsr->dirty = true;
700                 dpm->arm->pc->dirty = true;
701
702                 /*
703                  * re-evaluate the core state, we might be in Aarch32 state now
704                  * we rely on dpm->dscr being up-to-date
705                  */
706                 core_state = armv8_dpm_get_core_state(dpm);
707                 armv8_select_opcodes(armv8, core_state == ARM_STATE_AARCH64);
708                 armv8_select_reg_access(armv8, core_state == ARM_STATE_AARCH64);
709         }
710
711         return retval;
712 }
713
714 /*
715  * Common register read, relies on armv8_select_reg_access() having been called.
716  */
717 static int dpmv8_read_reg(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
718 {
719         struct armv8_common *armv8 = dpm->arm->arch_info;
720         uint64_t value_64;
721         int retval;
722
723         retval = armv8->read_reg_u64(armv8, regnum, &value_64);
724
725         if (retval == ERROR_OK) {
726                 r->valid = true;
727                 r->dirty = false;
728                 buf_set_u64(r->value, 0, r->size, value_64);
729                 if (r->size == 64)
730                         LOG_DEBUG("READ: %s, %16.8llx", r->name, (unsigned long long) value_64);
731                 else
732                         LOG_DEBUG("READ: %s, %8.8x", r->name, (unsigned int) value_64);
733         }
734         return ERROR_OK;
735 }
736
737 /*
738  * Common register write, relies on armv8_select_reg_access() having been called.
739  */
740 static int dpmv8_write_reg(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
741 {
742         struct armv8_common *armv8 = dpm->arm->arch_info;
743         int retval = ERROR_FAIL;
744         uint64_t value_64;
745
746         value_64 = buf_get_u64(r->value, 0, r->size);
747
748         retval = armv8->write_reg_u64(armv8, regnum, value_64);
749         if (retval == ERROR_OK) {
750                 r->dirty = false;
751                 if (r->size == 64)
752                         LOG_DEBUG("WRITE: %s, %16.8llx", r->name, (unsigned long long)value_64);
753                 else
754                         LOG_DEBUG("WRITE: %s, %8.8x", r->name, (unsigned int)value_64);
755         }
756
757         return ERROR_OK;
758 }
759
760 /**
761  * Read basic registers of the the current context:  R0 to R15, and CPSR;
762  * sets the core mode (such as USR or IRQ) and state (such as ARM or Thumb).
763  * In normal operation this is called on entry to halting debug state,
764  * possibly after some other operations supporting restore of debug state
765  * or making sure the CPU is fully idle (drain write buffer, etc).
766  */
767 int armv8_dpm_read_current_registers(struct arm_dpm *dpm)
768 {
769         struct arm *arm = dpm->arm;
770         struct armv8_common *armv8 = (struct armv8_common *)arm->arch_info;
771         struct reg_cache *cache;
772         struct reg *r;
773         uint32_t cpsr;
774         int retval;
775
776         retval = dpm->prepare(dpm);
777         if (retval != ERROR_OK)
778                 return retval;
779
780         cache = arm->core_cache;
781
782         /* read R0 first (it's used for scratch), then CPSR */
783         r = cache->reg_list + 0;
784         if (!r->valid) {
785                 retval = dpmv8_read_reg(dpm, r, 0);
786                 if (retval != ERROR_OK)
787                         goto fail;
788         }
789         r->dirty = true;
790
791         /* read cpsr to r0 and get it back */
792         retval = dpm->instr_read_data_r0(dpm,
793                         armv8_opcode(armv8, READ_REG_DSPSR), &cpsr);
794         if (retval != ERROR_OK)
795                 goto fail;
796
797         /* update core mode and state */
798         armv8_set_cpsr(arm, cpsr);
799
800         for (unsigned int i = 1; i < cache->num_regs ; i++) {
801                 struct arm_reg *arm_reg;
802
803                 r = armv8_reg_current(arm, i);
804                 if (r->valid)
805                         continue;
806
807                 /*
808                  * Only read registers that are available from the
809                  * current EL (or core mode).
810                  */
811                 arm_reg = r->arch_info;
812                 if (arm_reg->mode != ARM_MODE_ANY &&
813                                 dpm->last_el != armv8_curel_from_core_mode(arm_reg->mode))
814                         continue;
815
816                 retval = dpmv8_read_reg(dpm, r, i);
817                 if (retval != ERROR_OK)
818                         goto fail;
819
820         }
821
822 fail:
823         dpm->finish(dpm);
824         return retval;
825 }
826
827 /* Avoid needless I/O ... leave breakpoints and watchpoints alone
828  * unless they're removed, or need updating because of single-stepping
829  * or running debugger code.
830  */
831 static int dpmv8_maybe_update_bpwp(struct arm_dpm *dpm, bool bpwp,
832         struct dpm_bpwp *xp, int *set_p)
833 {
834         int retval = ERROR_OK;
835         bool disable;
836
837         if (!set_p) {
838                 if (!xp->dirty)
839                         goto done;
840                 xp->dirty = false;
841                 /* removed or startup; we must disable it */
842                 disable = true;
843         } else if (bpwp) {
844                 if (!xp->dirty)
845                         goto done;
846                 /* disabled, but we must set it */
847                 xp->dirty = disable = false;
848                 *set_p = true;
849         } else {
850                 if (!*set_p)
851                         goto done;
852                 /* set, but we must temporarily disable it */
853                 xp->dirty = disable = true;
854                 *set_p = false;
855         }
856
857         if (disable)
858                 retval = dpm->bpwp_disable(dpm, xp->number);
859         else
860                 retval = dpm->bpwp_enable(dpm, xp->number,
861                                 xp->address, xp->control);
862
863         if (retval != ERROR_OK)
864                 LOG_ERROR("%s: can't %s HW %spoint %d",
865                         disable ? "disable" : "enable",
866                         target_name(dpm->arm->target),
867                         (xp->number < 16) ? "break" : "watch",
868                         xp->number & 0xf);
869 done:
870         return retval;
871 }
872
873 static int dpmv8_add_breakpoint(struct target *target, struct breakpoint *bp);
874
875 /**
876  * Writes all modified core registers for all processor modes.  In normal
877  * operation this is called on exit from halting debug state.
878  *
879  * @param dpm: represents the processor
880  * @param bpwp: true ensures breakpoints and watchpoints are set,
881  *      false ensures they are cleared
882  */
883 int armv8_dpm_write_dirty_registers(struct arm_dpm *dpm, bool bpwp)
884 {
885         struct arm *arm = dpm->arm;
886         struct reg_cache *cache = arm->core_cache;
887         int retval;
888
889         retval = dpm->prepare(dpm);
890         if (retval != ERROR_OK)
891                 goto done;
892
893         /* If we're managing hardware breakpoints for this core, enable
894          * or disable them as requested.
895          *
896          * REVISIT We don't yet manage them for ANY cores.  Eventually
897          * we should be able to assume we handle them; but until then,
898          * cope with the hand-crafted breakpoint code.
899          */
900         if (arm->target->type->add_breakpoint == dpmv8_add_breakpoint) {
901                 for (unsigned i = 0; i < dpm->nbp; i++) {
902                         struct dpm_bp *dbp = dpm->dbp + i;
903                         struct breakpoint *bp = dbp->bp;
904
905                         retval = dpmv8_maybe_update_bpwp(dpm, bpwp, &dbp->bpwp,
906                                         bp ? &bp->set : NULL);
907                         if (retval != ERROR_OK)
908                                 goto done;
909                 }
910         }
911
912         /* enable/disable watchpoints */
913         for (unsigned i = 0; i < dpm->nwp; i++) {
914                 struct dpm_wp *dwp = dpm->dwp + i;
915                 struct watchpoint *wp = dwp->wp;
916
917                 retval = dpmv8_maybe_update_bpwp(dpm, bpwp, &dwp->bpwp,
918                                 wp ? &wp->set : NULL);
919                 if (retval != ERROR_OK)
920                         goto done;
921         }
922
923         /* NOTE:  writes to breakpoint and watchpoint registers might
924          * be queued, and need (efficient/batched) flushing later.
925          */
926
927         /* Restore original core mode and state */
928         retval = armv8_dpm_modeswitch(dpm, ARM_MODE_ANY);
929         if (retval != ERROR_OK)
930                 goto done;
931
932         /* check everything except our scratch register R0 */
933         for (unsigned i = 1; i < cache->num_regs; i++) {
934                 struct arm_reg *r;
935
936                 /* skip PC and CPSR */
937                 if (i == ARMV8_PC || i == ARMV8_xPSR)
938                         continue;
939                 /* skip invalid */
940                 if (!cache->reg_list[i].valid)
941                         continue;
942                 /* skip non-dirty */
943                 if (!cache->reg_list[i].dirty)
944                         continue;
945
946                 /* skip all registers not on the current EL */
947                 r = cache->reg_list[i].arch_info;
948                 if (r->mode != ARM_MODE_ANY &&
949                                 dpm->last_el != armv8_curel_from_core_mode(r->mode))
950                         continue;
951
952                 retval = dpmv8_write_reg(dpm, &cache->reg_list[i], i);
953                 if (retval != ERROR_OK)
954                         break;
955         }
956
957         /* flush CPSR and PC */
958         if (retval == ERROR_OK)
959                 retval = dpmv8_write_reg(dpm, &cache->reg_list[ARMV8_xPSR], ARMV8_xPSR);
960         if (retval == ERROR_OK)
961                 retval = dpmv8_write_reg(dpm, &cache->reg_list[ARMV8_PC], ARMV8_PC);
962         /* flush R0 -- it's *very* dirty by now */
963         if (retval == ERROR_OK)
964                 retval = dpmv8_write_reg(dpm, &cache->reg_list[0], 0);
965         if (retval == ERROR_OK)
966                 dpm->instr_cpsr_sync(dpm);
967 done:
968         dpm->finish(dpm);
969         return retval;
970 }
971
972 /*
973  * Standard ARM register accessors ... there are three methods
974  * in "struct arm", to support individual read/write and bulk read
975  * of registers.
976  */
977
978 static int armv8_dpm_read_core_reg(struct target *target, struct reg *r,
979         int regnum, enum arm_mode mode)
980 {
981         struct arm *arm = target_to_arm(target);
982         struct arm_dpm *dpm = target_to_arm(target)->dpm;
983         int retval;
984         int max = arm->core_cache->num_regs;
985
986         if (regnum < 0 || regnum >= max)
987                 return ERROR_COMMAND_SYNTAX_ERROR;
988
989         /*
990          * REVISIT what happens if we try to read SPSR in a core mode
991          * which has no such register?
992          */
993         retval = dpm->prepare(dpm);
994         if (retval != ERROR_OK)
995                 return retval;
996
997         retval = dpmv8_read_reg(dpm, r, regnum);
998         if (retval != ERROR_OK)
999                 goto fail;
1000
1001 fail:
1002         /* (void) */ dpm->finish(dpm);
1003         return retval;
1004 }
1005
1006 static int armv8_dpm_write_core_reg(struct target *target, struct reg *r,
1007         int regnum, enum arm_mode mode, uint8_t *value)
1008 {
1009         struct arm *arm = target_to_arm(target);
1010         struct arm_dpm *dpm = target_to_arm(target)->dpm;
1011         int retval;
1012         int max = arm->core_cache->num_regs;
1013
1014         if (regnum < 0 || regnum > max)
1015                 return ERROR_COMMAND_SYNTAX_ERROR;
1016
1017         /* REVISIT what happens if we try to write SPSR in a core mode
1018          * which has no such register?
1019          */
1020
1021         retval = dpm->prepare(dpm);
1022         if (retval != ERROR_OK)
1023                 return retval;
1024
1025         retval = dpmv8_write_reg(dpm, r, regnum);
1026
1027         /* always clean up, regardless of error */
1028         dpm->finish(dpm);
1029
1030         return retval;
1031 }
1032
1033 static int armv8_dpm_full_context(struct target *target)
1034 {
1035         struct arm *arm = target_to_arm(target);
1036         struct arm_dpm *dpm = arm->dpm;
1037         struct reg_cache *cache = arm->core_cache;
1038         int retval;
1039         bool did_read;
1040
1041         retval = dpm->prepare(dpm);
1042         if (retval != ERROR_OK)
1043                 goto done;
1044
1045         do {
1046                 enum arm_mode mode = ARM_MODE_ANY;
1047
1048                 did_read = false;
1049
1050                 /* We "know" arm_dpm_read_current_registers() was called so
1051                  * the unmapped registers (R0..R7, PC, AND CPSR) and some
1052                  * view of R8..R14 are current.  We also "know" oddities of
1053                  * register mapping: special cases for R8..R12 and SPSR.
1054                  *
1055                  * Pick some mode with unread registers and read them all.
1056                  * Repeat until done.
1057                  */
1058                 for (unsigned i = 0; i < cache->num_regs; i++) {
1059                         struct arm_reg *r;
1060
1061                         if (cache->reg_list[i].valid)
1062                                 continue;
1063                         r = cache->reg_list[i].arch_info;
1064
1065                         /* may need to pick a mode and set CPSR */
1066                         if (!did_read) {
1067                                 did_read = true;
1068                                 mode = r->mode;
1069
1070                                 /* For regular (ARM_MODE_ANY) R8..R12
1071                                  * in case we've entered debug state
1072                                  * in FIQ mode we need to patch mode.
1073                                  */
1074                                 if (mode != ARM_MODE_ANY)
1075                                         retval = armv8_dpm_modeswitch(dpm, mode);
1076                                 else
1077                                         retval = armv8_dpm_modeswitch(dpm, ARM_MODE_USR);
1078
1079                                 if (retval != ERROR_OK)
1080                                         goto done;
1081                         }
1082                         if (r->mode != mode)
1083                                 continue;
1084
1085                         /* CPSR was read, so "R16" must mean SPSR */
1086                         retval = dpmv8_read_reg(dpm,
1087                                         &cache->reg_list[i],
1088                                         (r->num == 16) ? 17 : r->num);
1089                         if (retval != ERROR_OK)
1090                                 goto done;
1091                 }
1092
1093         } while (did_read);
1094
1095         retval = armv8_dpm_modeswitch(dpm, ARM_MODE_ANY);
1096         /* (void) */ dpm->finish(dpm);
1097 done:
1098         return retval;
1099 }
1100
1101
1102 /*----------------------------------------------------------------------*/
1103
1104 /*
1105  * Breakpoint and Watchpoint support.
1106  *
1107  * Hardware {break,watch}points are usually left active, to minimize
1108  * debug entry/exit costs.  When they are set or cleared, it's done in
1109  * batches.  Also, DPM-conformant hardware can update debug registers
1110  * regardless of whether the CPU is running or halted ... though that
1111  * fact isn't currently leveraged.
1112  */
1113
1114 static int dpmv8_bpwp_setup(struct arm_dpm *dpm, struct dpm_bpwp *xp,
1115         uint32_t addr, uint32_t length)
1116 {
1117         uint32_t control;
1118
1119         control = (1 << 0)      /* enable */
1120                 | (3 << 1);     /* both user and privileged access */
1121
1122         /* Match 1, 2, or all 4 byte addresses in this word.
1123          *
1124          * FIXME:  v7 hardware allows lengths up to 2 GB for BP and WP.
1125          * Support larger length, when addr is suitably aligned.  In
1126          * particular, allow watchpoints on 8 byte "double" values.
1127          *
1128          * REVISIT allow watchpoints on unaligned 2-bit values; and on
1129          * v7 hardware, unaligned 4-byte ones too.
1130          */
1131         switch (length) {
1132                 case 1:
1133                         control |= (1 << (addr & 3)) << 5;
1134                         break;
1135                 case 2:
1136                         /* require 2-byte alignment */
1137                         if (!(addr & 1)) {
1138                                 control |= (3 << (addr & 2)) << 5;
1139                                 break;
1140                         }
1141                 /* FALL THROUGH */
1142                 case 4:
1143                         /* require 4-byte alignment */
1144                         if (!(addr & 3)) {
1145                                 control |= 0xf << 5;
1146                                 break;
1147                         }
1148                 /* FALL THROUGH */
1149                 default:
1150                         LOG_ERROR("unsupported {break,watch}point length/alignment");
1151                         return ERROR_COMMAND_SYNTAX_ERROR;
1152         }
1153
1154         /* other shared control bits:
1155          * bits 15:14 == 0 ... both secure and nonsecure states (v6.1+ only)
1156          * bit 20 == 0 ... not linked to a context ID
1157          * bit 28:24 == 0 ... not ignoring N LSBs (v7 only)
1158          */
1159
1160         xp->address = addr & ~3;
1161         xp->control = control;
1162         xp->dirty = true;
1163
1164         LOG_DEBUG("BPWP: addr %8.8" PRIx32 ", control %" PRIx32 ", number %d",
1165                 xp->address, control, xp->number);
1166
1167         /* hardware is updated in write_dirty_registers() */
1168         return ERROR_OK;
1169 }
1170
1171 static int dpmv8_add_breakpoint(struct target *target, struct breakpoint *bp)
1172 {
1173         struct arm *arm = target_to_arm(target);
1174         struct arm_dpm *dpm = arm->dpm;
1175         int retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1176
1177         if (bp->length < 2)
1178                 return ERROR_COMMAND_SYNTAX_ERROR;
1179         if (!dpm->bpwp_enable)
1180                 return retval;
1181
1182         /* FIXME we need a generic solution for software breakpoints. */
1183         if (bp->type == BKPT_SOFT)
1184                 LOG_DEBUG("using HW bkpt, not SW...");
1185
1186         for (unsigned i = 0; i < dpm->nbp; i++) {
1187                 if (!dpm->dbp[i].bp) {
1188                         retval = dpmv8_bpwp_setup(dpm, &dpm->dbp[i].bpwp,
1189                                         bp->address, bp->length);
1190                         if (retval == ERROR_OK)
1191                                 dpm->dbp[i].bp = bp;
1192                         break;
1193                 }
1194         }
1195
1196         return retval;
1197 }
1198
1199 static int dpmv8_remove_breakpoint(struct target *target, struct breakpoint *bp)
1200 {
1201         struct arm *arm = target_to_arm(target);
1202         struct arm_dpm *dpm = arm->dpm;
1203         int retval = ERROR_COMMAND_SYNTAX_ERROR;
1204
1205         for (unsigned i = 0; i < dpm->nbp; i++) {
1206                 if (dpm->dbp[i].bp == bp) {
1207                         dpm->dbp[i].bp = NULL;
1208                         dpm->dbp[i].bpwp.dirty = true;
1209
1210                         /* hardware is updated in write_dirty_registers() */
1211                         retval = ERROR_OK;
1212                         break;
1213                 }
1214         }
1215
1216         return retval;
1217 }
1218
1219 static int dpmv8_watchpoint_setup(struct arm_dpm *dpm, unsigned index_t,
1220         struct watchpoint *wp)
1221 {
1222         int retval;
1223         struct dpm_wp *dwp = dpm->dwp + index_t;
1224         uint32_t control;
1225
1226         /* this hardware doesn't support data value matching or masking */
1227         if (wp->value || wp->mask != ~(uint32_t)0) {
1228                 LOG_DEBUG("watchpoint values and masking not supported");
1229                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1230         }
1231
1232         retval = dpmv8_bpwp_setup(dpm, &dwp->bpwp, wp->address, wp->length);
1233         if (retval != ERROR_OK)
1234                 return retval;
1235
1236         control = dwp->bpwp.control;
1237         switch (wp->rw) {
1238                 case WPT_READ:
1239                         control |= 1 << 3;
1240                         break;
1241                 case WPT_WRITE:
1242                         control |= 2 << 3;
1243                         break;
1244                 case WPT_ACCESS:
1245                         control |= 3 << 3;
1246                         break;
1247         }
1248         dwp->bpwp.control = control;
1249
1250         dpm->dwp[index_t].wp = wp;
1251
1252         return retval;
1253 }
1254
1255 static int dpmv8_add_watchpoint(struct target *target, struct watchpoint *wp)
1256 {
1257         struct arm *arm = target_to_arm(target);
1258         struct arm_dpm *dpm = arm->dpm;
1259         int retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1260
1261         if (dpm->bpwp_enable) {
1262                 for (unsigned i = 0; i < dpm->nwp; i++) {
1263                         if (!dpm->dwp[i].wp) {
1264                                 retval = dpmv8_watchpoint_setup(dpm, i, wp);
1265                                 break;
1266                         }
1267                 }
1268         }
1269
1270         return retval;
1271 }
1272
1273 static int dpmv8_remove_watchpoint(struct target *target, struct watchpoint *wp)
1274 {
1275         struct arm *arm = target_to_arm(target);
1276         struct arm_dpm *dpm = arm->dpm;
1277         int retval = ERROR_COMMAND_SYNTAX_ERROR;
1278
1279         for (unsigned i = 0; i < dpm->nwp; i++) {
1280                 if (dpm->dwp[i].wp == wp) {
1281                         dpm->dwp[i].wp = NULL;
1282                         dpm->dwp[i].bpwp.dirty = true;
1283
1284                         /* hardware is updated in write_dirty_registers() */
1285                         retval = ERROR_OK;
1286                         break;
1287                 }
1288         }
1289
1290         return retval;
1291 }
1292
1293 void armv8_dpm_report_wfar(struct arm_dpm *dpm, uint64_t addr)
1294 {
1295         switch (dpm->arm->core_state) {
1296                 case ARM_STATE_ARM:
1297                 case ARM_STATE_AARCH64:
1298                         addr -= 8;
1299                         break;
1300                 case ARM_STATE_THUMB:
1301                 case ARM_STATE_THUMB_EE:
1302                         addr -= 4;
1303                         break;
1304                 case ARM_STATE_JAZELLE:
1305                         /* ?? */
1306                         break;
1307                 default:
1308                         LOG_DEBUG("Unknow core_state");
1309                         break;
1310         }
1311         dpm->wp_pc = addr;
1312 }
1313
1314 /*
1315  * Handle exceptions taken in debug state. This happens mostly for memory
1316  * accesses that violated a MMU policy. Taking an exception while in debug
1317  * state clobbers certain state registers on the target exception level.
1318  * Just mark those registers dirty so that they get restored on resume.
1319  * This works both for Aarch32 and Aarch64 states.
1320  *
1321  * This function must not perform any actions that trigger another exception
1322  * or a recursion will happen.
1323  */
1324 void armv8_dpm_handle_exception(struct arm_dpm *dpm)
1325 {
1326         struct armv8_common *armv8 = dpm->arm->arch_info;
1327         struct reg_cache *cache = dpm->arm->core_cache;
1328         enum arm_state core_state;
1329         uint64_t dlr;
1330         uint32_t dspsr;
1331         unsigned int el;
1332
1333         static const int clobbered_regs_by_el[3][5] = {
1334                 { ARMV8_PC, ARMV8_xPSR, ARMV8_ELR_EL1, ARMV8_ESR_EL1, ARMV8_SPSR_EL1 },
1335                 { ARMV8_PC, ARMV8_xPSR, ARMV8_ELR_EL2, ARMV8_ESR_EL2, ARMV8_SPSR_EL2 },
1336                 { ARMV8_PC, ARMV8_xPSR, ARMV8_ELR_EL3, ARMV8_ESR_EL3, ARMV8_SPSR_EL3 },
1337         };
1338
1339         el = (dpm->dscr >> 8) & 3;
1340
1341         /* safety check, must not happen since EL0 cannot be a target for an exception */
1342         if (el < SYSTEM_CUREL_EL1 || el > SYSTEM_CUREL_EL3) {
1343                 LOG_ERROR("%s: EL %i is invalid, DSCR corrupted?", __func__, el);
1344                 return;
1345         }
1346
1347         /* Clear sticky error */
1348         mem_ap_write_u32(armv8->debug_ap,
1349                 armv8->debug_base + CPUV8_DBG_DRCR, DRCR_CSE);
1350
1351         armv8->read_reg_u64(armv8, ARMV8_xPSR, &dlr);
1352         dspsr = dlr;
1353         armv8->read_reg_u64(armv8, ARMV8_PC, &dlr);
1354
1355         LOG_DEBUG("Exception taken to EL %i, DLR=0x%016"PRIx64" DSPSR=0x%08"PRIx32,
1356                         el, dlr, dspsr);
1357
1358         /* mark all clobbered registers as dirty */
1359         for (int i = 0; i < 5; i++)
1360                 cache->reg_list[clobbered_regs_by_el[el-1][i]].dirty = true;
1361
1362         /*
1363          * re-evaluate the core state, we might be in Aarch64 state now
1364          * we rely on dpm->dscr being up-to-date
1365          */
1366         core_state = armv8_dpm_get_core_state(dpm);
1367         armv8_select_opcodes(armv8, core_state == ARM_STATE_AARCH64);
1368         armv8_select_reg_access(armv8, core_state == ARM_STATE_AARCH64);
1369 }
1370
1371 /*----------------------------------------------------------------------*/
1372
1373 /*
1374  * Other debug and support utilities
1375  */
1376
1377 void armv8_dpm_report_dscr(struct arm_dpm *dpm, uint32_t dscr)
1378 {
1379         struct target *target = dpm->arm->target;
1380
1381         dpm->dscr = dscr;
1382         dpm->last_el = (dscr >> 8) & 3;
1383
1384         /* Examine debug reason */
1385         switch (DSCR_ENTRY(dscr)) {
1386                 /* FALL THROUGH -- assume a v6 core in abort mode */
1387                 case DSCRV8_ENTRY_EXT_DEBUG:    /* EDBGRQ */
1388                         target->debug_reason = DBG_REASON_DBGRQ;
1389                         break;
1390                 case DSCRV8_ENTRY_HALT_STEP_EXECLU:     /* HALT step */
1391                 case DSCRV8_ENTRY_HALT_STEP_NORMAL: /* Halt step*/
1392                 case DSCRV8_ENTRY_HALT_STEP:
1393                         target->debug_reason = DBG_REASON_SINGLESTEP;
1394                         break;
1395                 case DSCRV8_ENTRY_HLT:  /* HLT instruction (software breakpoint) */
1396                 case DSCRV8_ENTRY_BKPT: /* SW BKPT (?) */
1397                 case DSCRV8_ENTRY_RESET_CATCH:  /* Reset catch */
1398                 case DSCRV8_ENTRY_OS_UNLOCK:  /*OS unlock catch*/
1399                 case DSCRV8_ENTRY_EXCEPTION_CATCH:  /*exception catch*/
1400                 case DSCRV8_ENTRY_SW_ACCESS_DBG: /*SW access dbg register*/
1401                         target->debug_reason = DBG_REASON_BREAKPOINT;
1402                         break;
1403                 case DSCRV8_ENTRY_WATCHPOINT:   /* asynch watchpoint */
1404                         target->debug_reason = DBG_REASON_WATCHPOINT;
1405                         break;
1406                 default:
1407                         target->debug_reason = DBG_REASON_UNDEFINED;
1408                         break;
1409         }
1410
1411 }
1412
1413 /*----------------------------------------------------------------------*/
1414
1415 /*
1416  * Setup and management support.
1417  */
1418
1419 /**
1420  * Hooks up this DPM to its associated target; call only once.
1421  * Initially this only covers the register cache.
1422  *
1423  * Oh, and watchpoints.  Yeah.
1424  */
1425 int armv8_dpm_setup(struct arm_dpm *dpm)
1426 {
1427         struct arm *arm = dpm->arm;
1428         struct target *target = arm->target;
1429         struct reg_cache *cache;
1430         arm->dpm = dpm;
1431
1432         /* register access setup */
1433         arm->full_context = armv8_dpm_full_context;
1434         arm->read_core_reg = armv8_dpm_read_core_reg;
1435         arm->write_core_reg = armv8_dpm_write_core_reg;
1436
1437         if (arm->core_cache == NULL) {
1438                 cache = armv8_build_reg_cache(target);
1439                 if (!cache)
1440                         return ERROR_FAIL;
1441         }
1442
1443         /* coprocessor access setup */
1444         arm->mrc = dpmv8_mrc;
1445         arm->mcr = dpmv8_mcr;
1446         arm->mrs = dpmv8_mrs;
1447         arm->msr = dpmv8_msr;
1448
1449         dpm->prepare = dpmv8_dpm_prepare;
1450         dpm->finish = dpmv8_dpm_finish;
1451
1452         dpm->instr_execute = dpmv8_instr_execute;
1453         dpm->instr_write_data_dcc = dpmv8_instr_write_data_dcc;
1454         dpm->instr_write_data_dcc_64 = dpmv8_instr_write_data_dcc_64;
1455         dpm->instr_write_data_r0 = dpmv8_instr_write_data_r0;
1456         dpm->instr_write_data_r0_64 = dpmv8_instr_write_data_r0_64;
1457         dpm->instr_cpsr_sync = dpmv8_instr_cpsr_sync;
1458
1459         dpm->instr_read_data_dcc = dpmv8_instr_read_data_dcc;
1460         dpm->instr_read_data_dcc_64 = dpmv8_instr_read_data_dcc_64;
1461         dpm->instr_read_data_r0 = dpmv8_instr_read_data_r0;
1462         dpm->instr_read_data_r0_64 = dpmv8_instr_read_data_r0_64;
1463
1464         dpm->arm_reg_current = armv8_reg_current;
1465
1466 /*      dpm->bpwp_enable = dpmv8_bpwp_enable; */
1467         dpm->bpwp_disable = dpmv8_bpwp_disable;
1468
1469         /* breakpoint setup -- optional until it works everywhere */
1470         if (!target->type->add_breakpoint) {
1471                 target->type->add_breakpoint = dpmv8_add_breakpoint;
1472                 target->type->remove_breakpoint = dpmv8_remove_breakpoint;
1473         }
1474
1475         /* watchpoint setup */
1476         target->type->add_watchpoint = dpmv8_add_watchpoint;
1477         target->type->remove_watchpoint = dpmv8_remove_watchpoint;
1478
1479         /* FIXME add vector catch support */
1480
1481         dpm->nbp = 1 + ((dpm->didr >> 12) & 0xf);
1482         dpm->dbp = calloc(dpm->nbp, sizeof *dpm->dbp);
1483
1484         dpm->nwp = 1 + ((dpm->didr >> 20) & 0xf);
1485         dpm->dwp = calloc(dpm->nwp, sizeof *dpm->dwp);
1486
1487         if (!dpm->dbp || !dpm->dwp) {
1488                 free(dpm->dbp);
1489                 free(dpm->dwp);
1490                 return ERROR_FAIL;
1491         }
1492
1493         LOG_INFO("%s: hardware has %d breakpoints, %d watchpoints",
1494                 target_name(target), dpm->nbp, dpm->nwp);
1495
1496         /* REVISIT ... and some of those breakpoints could match
1497          * execution context IDs...
1498          */
1499
1500         return ERROR_OK;
1501 }
1502
1503 /**
1504  * Reinitializes DPM state at the beginning of a new debug session
1505  * or after a reset which may have affected the debug module.
1506  */
1507 int armv8_dpm_initialize(struct arm_dpm *dpm)
1508 {
1509         /* Disable all breakpoints and watchpoints at startup. */
1510         if (dpm->bpwp_disable) {
1511                 unsigned i;
1512
1513                 for (i = 0; i < dpm->nbp; i++) {
1514                         dpm->dbp[i].bpwp.number = i;
1515                         (void) dpm->bpwp_disable(dpm, i);
1516                 }
1517                 for (i = 0; i < dpm->nwp; i++) {
1518                         dpm->dwp[i].bpwp.number = 16 + i;
1519                         (void) dpm->bpwp_disable(dpm, 16 + i);
1520                 }
1521         } else
1522                 LOG_WARNING("%s: can't disable breakpoints and watchpoints",
1523                         target_name(dpm->arm->target));
1524
1525         return ERROR_OK;
1526 }