Skip to content

Commit 6082f35

Browse files
committed
Update debug_defines. Clarify debug output.
Update debug_defines from the spec, commit 920ec9a690. Decode dmstatus scans in the debug output.
1 parent fd81f7f commit 6082f35

File tree

2 files changed

+118
-52
lines changed

2 files changed

+118
-52
lines changed

src/target/riscv/debug_defines.h

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
/*
8585
* 0: Version described in spec version 0.11.
8686
*
87-
* 1: Version described in spec version 0.12 (and later?), which
87+
* 1: Version described in spec version 0.13 (and later?), which
8888
* reduces the DMI data width to 32 bits.
8989
*
9090
* Other values are reserved for future use.
@@ -110,7 +110,12 @@
110110
/*
111111
* When the debugger writes this field, it has the following meaning:
112112
*
113-
* 0: Ignore \Fdata. (nop)
113+
* 0: Ignore \Fdata and \Faddress. (nop)
114+
*
115+
* Don't send anything over the DMI during Update-DR.
116+
* This operation should never result in a busy or error response.
117+
* The address and data reported in the following Capture-DR
118+
* are undefined.
114119
*
115120
* 1: Read from \Faddress. (read)
116121
*
@@ -151,13 +156,11 @@
151156
/*
152157
* 0: There is no external debug support.
153158
*
154-
* 1: External debug support exists as it is described in this document.
155-
*
156-
* Other values are reserved for future standards.
159+
* 4: External debug support exists as it is described in this document.
157160
*/
158-
#define CSR_DCSR_XDEBUGVER_OFFSET 30
159-
#define CSR_DCSR_XDEBUGVER_LENGTH 2
160-
#define CSR_DCSR_XDEBUGVER (0x3 << CSR_DCSR_XDEBUGVER_OFFSET)
161+
#define CSR_DCSR_XDEBUGVER_OFFSET 28
162+
#define CSR_DCSR_XDEBUGVER_LENGTH 4
163+
#define CSR_DCSR_XDEBUGVER (0xf << CSR_DCSR_XDEBUGVER_OFFSET)
161164
/*
162165
* When 1, {\tt ebreak} instructions in Machine Mode enter Debug Mode.
163166
*/
@@ -230,8 +233,11 @@
230233
#define CSR_DCSR_CAUSE (0x7 << CSR_DCSR_CAUSE_OFFSET)
231234
/*
232235
* When set and not in Debug Mode, the hart will only execute a single
233-
* instruction, and then enter Debug Mode. Interrupts are disabled
234-
* when this bit is set.
236+
* instruction and then enter Debug Mode.
237+
* Interrupts are disabled when this bit is set.
238+
* If the instruction does not complete due to an exception,
239+
* the hart will immediately enter Debug Mode before executing
240+
* the trap handler, with appropriate exception registers set.
235241
*/
236242
#define CSR_DCSR_STEP_OFFSET 2
237243
#define CSR_DCSR_STEP_LENGTH 1
@@ -255,16 +261,6 @@
255261
#define CSR_DPC_DPC (((1L<<XLEN)-1) << CSR_DPC_DPC_OFFSET)
256262
#define CSR_DSCRATCH0 0x7b2
257263
#define CSR_DSCRATCH1 0x7b3
258-
#define CSR_PRIV virtual
259-
/*
260-
* Contains the privilege level the hart was operating in when Debug
261-
* Mode was entered. The encoding is described in Table
262-
* \ref{tab:privlevel}. A user can write this value to change the
263-
* hart's privilege level when exiting Debug Mode.
264-
*/
265-
#define CSR_PRIV_PRV_OFFSET 0
266-
#define CSR_PRIV_PRV_LENGTH 2
267-
#define CSR_PRIV_PRV (0x3 << CSR_PRIV_PRV_OFFSET)
268264
#define CSR_TSELECT 0x7a0
269265
#define CSR_TSELECT_INDEX_OFFSET 0
270266
#define CSR_TSELECT_INDEX_LENGTH XLEN
@@ -485,28 +481,28 @@
485481
#define CSR_ICOUNT_COUNT_LENGTH 14
486482
#define CSR_ICOUNT_COUNT (0x3fffL << CSR_ICOUNT_COUNT_OFFSET)
487483
/*
488-
* When set, every instruction completed in M mode decrements \Fcount
484+
* When set, every instruction completed or exception taken in M mode decrements \Fcount
489485
* by 1.
490486
*/
491487
#define CSR_ICOUNT_M_OFFSET 9
492488
#define CSR_ICOUNT_M_LENGTH 1
493489
#define CSR_ICOUNT_M (0x1L << CSR_ICOUNT_M_OFFSET)
494490
/*
495-
* When set, every instruction completed in H mode decrements \Fcount
491+
* When set, every instruction completed or exception taken in in H mode decrements \Fcount
496492
* by 1.
497493
*/
498494
#define CSR_ICOUNT_H_OFFSET 8
499495
#define CSR_ICOUNT_H_LENGTH 1
500496
#define CSR_ICOUNT_H (0x1L << CSR_ICOUNT_H_OFFSET)
501497
/*
502-
* When set, every instruction completed in S mode decrements \Fcount
498+
* When set, every instruction completed or exception taken in S mode decrements \Fcount
503499
* by 1.
504500
*/
505501
#define CSR_ICOUNT_S_OFFSET 7
506502
#define CSR_ICOUNT_S_LENGTH 1
507503
#define CSR_ICOUNT_S (0x1L << CSR_ICOUNT_S_OFFSET)
508504
/*
509-
* When set, every instruction completed in U mode decrements \Fcount
505+
* When set, every instruction completed or exception taken in U mode decrements \Fcount
510506
* by 1.
511507
*/
512508
#define CSR_ICOUNT_U_OFFSET 6
@@ -619,29 +615,24 @@
619615
#define DMI_DMSTATUS_CFGSTRVALID_LENGTH 1
620616
#define DMI_DMSTATUS_CFGSTRVALID (0x1 << DMI_DMSTATUS_CFGSTRVALID_OFFSET)
621617
/*
622-
* Reserved for future use. Reads as 0.
623-
*/
624-
#define DMI_DMSTATUS_VERSIONHI_OFFSET 2
625-
#define DMI_DMSTATUS_VERSIONHI_LENGTH 2
626-
#define DMI_DMSTATUS_VERSIONHI (0x3 << DMI_DMSTATUS_VERSIONHI_OFFSET)
627-
/*
628-
* 00: There is no Debug Module present.
618+
* 0: There is no Debug Module present.
629619
*
630-
* 01: There is a Debug Module and it conforms to version 0.11 of this
620+
* 1: There is a Debug Module and it conforms to version 0.11 of this
631621
* specification.
632622
*
633-
* 10: There is a Debug Module and it conforms to version 0.13 of this
623+
* 2: There is a Debug Module and it conforms to version 0.13 of this
634624
* specification.
635-
*
636-
* 11: Reserved for future use.
637625
*/
638-
#define DMI_DMSTATUS_VERSIONLO_OFFSET 0
639-
#define DMI_DMSTATUS_VERSIONLO_LENGTH 2
640-
#define DMI_DMSTATUS_VERSIONLO (0x3 << DMI_DMSTATUS_VERSIONLO_OFFSET)
626+
#define DMI_DMSTATUS_VERSION_OFFSET 0
627+
#define DMI_DMSTATUS_VERSION_LENGTH 4
628+
#define DMI_DMSTATUS_VERSION (0xf << DMI_DMSTATUS_VERSION_OFFSET)
641629
#define DMI_DMCONTROL 0x10
642630
/*
643-
* Halt request signal for all currently selected harts. When set to 1, the
644-
* hart will halt if it is not currently halted.
631+
* Halt request signal for all currently selected harts. When set to
632+
* 1, each selected hart will halt if it is not currently halted.
633+
*
634+
* Writing 1 or 0 has no effect on a hart which is already halted, but
635+
* the bit should be cleared to 0 before the hart is resumed.
645636
* Setting both \Fhaltreq and \Fresumereq leads to undefined behavior.
646637
*
647638
* Writes apply to the new value of \Fhartsel and \Fhasel.
@@ -651,7 +642,7 @@
651642
#define DMI_DMCONTROL_HALTREQ (0x1 << DMI_DMCONTROL_HALTREQ_OFFSET)
652643
/*
653644
* Resume request signal for all currently selected harts. When set to 1,
654-
* the hart will resume if it is currently halted.
645+
* each selected hart will resume if it is currently halted.
655646
* Setting both \Fhaltreq and \Fresumereq leads to undefined behavior.
656647
*
657648
* Writes apply to the new value of \Fhartsel and \Fhasel.
@@ -698,8 +689,11 @@
698689
#define DMI_DMCONTROL_HARTSEL (0x3ff << DMI_DMCONTROL_HARTSEL_OFFSET)
699690
/*
700691
* This bit controls the reset signal from the DM to the rest of the
701-
* system. To perform a reset the debugger writes 1, and then writes 0
702-
* to deassert the reset.
692+
* system. To perform a system reset the debugger writes 1,
693+
* and then writes 0
694+
* to deassert the reset. This bit must not reset the Debug Module
695+
* registers. What it does reset is platform-specific (it may
696+
* reset nothing).
703697
*/
704698
#define DMI_DMCONTROL_NDMRESET_OFFSET 1
705699
#define DMI_DMCONTROL_NDMRESET_LENGTH 1
@@ -717,8 +711,8 @@
717711
* Debug Module after power up, including the platform's system reset
718712
* or Debug Transport reset signals.
719713
*
720-
* A debugger should pulse this bit low to ensure that the Debug
721-
* Module is fully reset and ready to use.
714+
* A debugger may pulse this bit low to get the debug module into a
715+
* known state.
722716
*
723717
* Implementations may use this bit to aid debugging, for example by
724718
* preventing the Debug Module from being power gated while debugging
@@ -899,8 +893,9 @@
899893
*
900894
* 0 (none): No error.
901895
*
902-
* 1 (busy): An abstract command was executing while \Rcommand or one
903-
* of the {\tt data} registers was accessed.
896+
* 1 (busy): An abstract command was executing while \Rcommand,
897+
* \Rabstractcs, \Rabstractauto was written, or when one
898+
* of the {\tt data} or {\tt progbuf} registers was read or written.
904899
*
905900
* 2 (not supported): The requested command is not supported. A
906901
* command that is not supported while the hart is running may be
@@ -919,7 +914,7 @@
919914
#define DMI_ABSTRACTCS_CMDERR (0x7 << DMI_ABSTRACTCS_CMDERR_OFFSET)
920915
/*
921916
* Number of {\tt data} registers that are implemented as part of the
922-
* abstract command interface. Valid sizes are 0 - 8.
917+
* abstract command interface. Valid sizes are 0 - 12.
923918
*/
924919
#define DMI_ABSTRACTCS_DATACOUNT_OFFSET 0
925920
#define DMI_ABSTRACTCS_DATACOUNT_LENGTH 5
@@ -1138,7 +1133,7 @@
11381133
*
11391134
* 3: There was some other error (eg. alignment).
11401135
*
1141-
* 4: The system bus master was busy when a one of the
1136+
* 4: The system bus master was busy when one of the
11421137
* {\tt sbaddress} or {\tt sbdata} registers was written,
11431138
* or the {\tt sbdata} register was read when it had
11441139
* stale data.
@@ -1396,7 +1391,10 @@
13961391
#define AC_ACCESS_REGISTER_WRITE_LENGTH 1
13971392
#define AC_ACCESS_REGISTER_WRITE (0x1 << AC_ACCESS_REGISTER_WRITE_OFFSET)
13981393
/*
1399-
* Number of the register to access, as described in Table~\ref{tab:regno}.
1394+
* Number of the register to access, as described in
1395+
* Table~\ref{tab:regno}.
1396+
* \Rdpc may be used as an alias for PC if this command is
1397+
* supported on a non-halted hart.
14001398
*/
14011399
#define AC_ACCESS_REGISTER_REGNO_OFFSET 0
14021400
#define AC_ACCESS_REGISTER_REGNO_LENGTH 16
@@ -1408,3 +1406,13 @@
14081406
#define AC_QUICK_ACCESS_CMDTYPE_OFFSET 24
14091407
#define AC_QUICK_ACCESS_CMDTYPE_LENGTH 8
14101408
#define AC_QUICK_ACCESS_CMDTYPE (0xff << AC_QUICK_ACCESS_CMDTYPE_OFFSET)
1409+
#define VIRT_PRIV virtual
1410+
/*
1411+
* Contains the privilege level the hart was operating in when Debug
1412+
* Mode was entered. The encoding is described in Table
1413+
* \ref{tab:privlevel}. A user can write this value to change the
1414+
* hart's privilege level when exiting Debug Mode.
1415+
*/
1416+
#define VIRT_PRIV_PRV_OFFSET 0
1417+
#define VIRT_PRIV_PRV_LENGTH 2
1418+
#define VIRT_PRIV_PRV (0x3 << VIRT_PRIV_PRV_OFFSET)

src/target/riscv/riscv-013.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,56 @@ typedef struct {
188188
int progbuf_size, progbuf_addr, data_addr, data_size;
189189
} riscv013_info_t;
190190

191+
static void decode_dmi(char *text, unsigned address, unsigned data)
192+
{
193+
text[0] = 0;
194+
switch (address) {
195+
case DMI_DMSTATUS:
196+
if (get_field(data, DMI_DMSTATUS_ALLRESUMEACK)) {
197+
strcat(text, " allresumeack");
198+
}
199+
if (get_field(data, DMI_DMSTATUS_ANYRESUMEACK)) {
200+
strcat(text, " anyresumeack");
201+
}
202+
if (get_field(data, DMI_DMSTATUS_ALLNONEXISTENT)) {
203+
strcat(text, " allnonexistent");
204+
}
205+
if (get_field(data, DMI_DMSTATUS_ANYNONEXISTENT)) {
206+
strcat(text, " anynonexistent");
207+
}
208+
if (get_field(data, DMI_DMSTATUS_ALLUNAVAIL)) {
209+
strcat(text, " allunavail");
210+
}
211+
if (get_field(data, DMI_DMSTATUS_ANYUNAVAIL)) {
212+
strcat(text, " anyunavail");
213+
}
214+
if (get_field(data, DMI_DMSTATUS_ALLRUNNING)) {
215+
strcat(text, " allrunning");
216+
}
217+
if (get_field(data, DMI_DMSTATUS_ANYRUNNING)) {
218+
strcat(text, " anyrunning");
219+
}
220+
if (get_field(data, DMI_DMSTATUS_ALLHALTED)) {
221+
strcat(text, " allhalted");
222+
}
223+
if (get_field(data, DMI_DMSTATUS_ANYHALTED)) {
224+
strcat(text, " anyhalted");
225+
}
226+
if (get_field(data, DMI_DMSTATUS_AUTHENTICATED)) {
227+
strcat(text, " authenticated");
228+
}
229+
if (get_field(data, DMI_DMSTATUS_AUTHBUSY)) {
230+
strcat(text, " authbusy");
231+
}
232+
if (get_field(data, DMI_DMSTATUS_CFGSTRVALID)) {
233+
strcat(text, " cfgstrvalid");
234+
}
235+
sprintf(text + strlen(text), " version=%d", get_field(data,
236+
DMI_DMSTATUS_VERSION));
237+
break;
238+
}
239+
}
240+
191241
static void dump_field(const struct scan_field *field)
192242
{
193243
static const char *op_string[] = {"-", "r", "w", "?"};
@@ -213,6 +263,14 @@ static void dump_field(const struct scan_field *field)
213263
op_string[out_op], out_data, out_address,
214264
status_string[in_op], in_data, in_address);
215265

266+
char out_text[500];
267+
char in_text[500];
268+
decode_dmi(out_text, out_address, out_data);
269+
decode_dmi(in_text, in_address, in_data);
270+
if (in_text[0] || out_text[0]) {
271+
log_printf_lf(LOG_LVL_DEBUG, __FILE__, __LINE__, "scan", "%s -> %s",
272+
out_text, in_text);
273+
}
216274
}
217275

218276
static riscv013_info_t *get_info(const struct target *target)
@@ -985,9 +1043,9 @@ static int examine(struct target *target)
9851043

9861044
uint32_t dmcontrol = dmi_read(target, DMI_DMCONTROL);
9871045
uint32_t dmstatus = dmi_read(target, DMI_DMSTATUS);
988-
if (get_field(dmstatus, DMI_DMSTATUS_VERSIONLO) != 2) {
1046+
if (get_field(dmstatus, DMI_DMSTATUS_VERSION) != 2) {
9891047
LOG_ERROR("OpenOCD only supports Debug Module version 2, not %d "
990-
"(dmstatus=0x%x)", get_field(dmstatus, DMI_DMSTATUS_VERSIONLO), dmstatus);
1048+
"(dmstatus=0x%x)", get_field(dmstatus, DMI_DMSTATUS_VERSION), dmstatus);
9911049
return ERROR_FAIL;
9921050
}
9931051

0 commit comments

Comments
 (0)