1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * Support for RISC-V, debug version 0.13, which is currently (2/4/17) the
16 #include "target/target.h"
17 #include "target/algorithm.h"
18 #include "target/target_type.h"
20 #include "jtag/jtag.h"
21 #include "target/register.h"
22 #include "target/breakpoints.h"
23 #include "helper/time_support.h"
24 #include "helper/list.h"
26 #include "debug_defines.h"
27 #include "rtos/rtos.h"
32 #define DM_DATA1 (DM_DATA0 + 1)
33 #define DM_PROGBUF1 (DM_PROGBUF0 + 1)
35 static int riscv013_on_step_or_resume(struct target *target, bool step);
36 static int riscv013_step_or_resume_current_hart(struct target *target,
37 bool step, bool use_hasel);
38 static void riscv013_clear_abstract_error(struct target *target);
40 /* Implementations of the functions in riscv_info_t. */
41 static int riscv013_get_register(struct target *target,
42 riscv_reg_t *value, int hid, int rid);
43 static int riscv013_set_register(struct target *target, int hartid, int regid, uint64_t value);
44 static int riscv013_select_current_hart(struct target *target);
45 static int riscv013_halt_prep(struct target *target);
46 static int riscv013_halt_go(struct target *target);
47 static int riscv013_resume_go(struct target *target);
48 static int riscv013_step_current_hart(struct target *target);
49 static int riscv013_on_halt(struct target *target);
50 static int riscv013_on_step(struct target *target);
51 static int riscv013_resume_prep(struct target *target);
52 static bool riscv013_is_halted(struct target *target);
53 static enum riscv_halt_reason riscv013_halt_reason(struct target *target);
54 static int riscv013_write_debug_buffer(struct target *target, unsigned index,
56 static riscv_insn_t riscv013_read_debug_buffer(struct target *target, unsigned
58 static int riscv013_execute_debug_buffer(struct target *target);
59 static void riscv013_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d);
60 static void riscv013_fill_dmi_read_u64(struct target *target, char *buf, int a);
61 static int riscv013_dmi_write_u64_bits(struct target *target);
62 static void riscv013_fill_dmi_nop_u64(struct target *target, char *buf);
63 static int register_read(struct target *target, uint64_t *value, uint32_t number);
64 static int register_read_direct(struct target *target, uint64_t *value, uint32_t number);
65 static int register_write_direct(struct target *target, unsigned number,
67 static int read_memory(struct target *target, target_addr_t address,
68 uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment);
69 static int write_memory(struct target *target, target_addr_t address,
70 uint32_t size, uint32_t count, const uint8_t *buffer);
71 static int riscv013_test_sba_config_reg(struct target *target, target_addr_t legal_address,
72 uint32_t num_words, target_addr_t illegal_address, bool run_sbbusyerror_test);
73 void write_memory_sba_simple(struct target *target, target_addr_t addr, uint32_t *write_data,
74 uint32_t write_size, uint32_t sbcs);
75 void read_memory_sba_simple(struct target *target, target_addr_t addr,
76 uint32_t *rd_buf, uint32_t read_size, uint32_t sbcs);
77 static int riscv013_test_compliance(struct target *target);
80 * Since almost everything can be accomplish by scanning the dbus register, all
81 * functions here assume dbus is already selected. The exception are functions
82 * called directly by OpenOCD, which can't assume anything about what's
83 * currently in IR. They should set IR to dbus explicitly.
86 #define get_field(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
87 #define set_field(reg, mask, val) (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
89 #define DIM(x) (sizeof(x)/sizeof(*x))
91 #define CSR_DCSR_CAUSE_SWBP 1
92 #define CSR_DCSR_CAUSE_TRIGGER 2
93 #define CSR_DCSR_CAUSE_DEBUGINT 3
94 #define CSR_DCSR_CAUSE_STEP 4
95 #define CSR_DCSR_CAUSE_HALT 5
96 #define CSR_DCSR_CAUSE_GROUP 6
98 #define RISCV013_INFO(r) riscv013_info_t *r = get_info(target)
100 /*** JTAG registers. ***/
108 DMI_STATUS_SUCCESS = 0,
109 DMI_STATUS_FAILED = 2,
119 /*** Debug Bus registers. ***/
121 #define CMDERR_NONE 0
122 #define CMDERR_BUSY 1
123 #define CMDERR_NOT_SUPPORTED 2
124 #define CMDERR_EXCEPTION 3
125 #define CMDERR_HALT_RESUME 4
126 #define CMDERR_OTHER 7
128 /*** Info about the core being debugged. ***/
135 bool read, write, execute;
146 struct list_head list;
147 int abs_chain_position;
149 /* The number of harts connected to this DM. */
151 /* Indicates we already reset this DM, so don't need to do it again. */
153 /* Targets that are connected to this DM. */
154 struct list_head target_list;
155 /* The currently selected hartid on this DM. */
157 bool hasel_supported;
159 /* The program buffer stores executable code. 0 is an illegal instruction,
160 * so we use 0 to mean the cached value is invalid. */
161 uint32_t progbuf_cache[16];
165 struct list_head list;
166 struct target *target;
170 /* The indexed used to address this hart in its DM. */
172 /* Number of address bits in the dbus register. */
174 /* Number of abstract command data registers. */
176 /* Number of words in the Program Buffer. */
177 unsigned progbufsize;
179 /* We cache the read-only bits of sbcs here. */
182 yes_no_maybe_t progbuf_writable;
183 /* We only need the address so that we know the alignment of the buffer. */
184 riscv_addr_t progbuf_address;
186 /* Number of run-test/idle cycles the target requests we do after each dbus
188 unsigned int dtmcs_idle;
190 /* This value is incremented every time a dbus access comes back as "busy".
191 * It's used to determine how many run-test/idle cycles to feed the target
192 * in between accesses. */
193 unsigned int dmi_busy_delay;
195 /* Number of run-test/idle cycles to add between consecutive bus master
196 * reads/writes respectively. */
197 unsigned int bus_master_write_delay, bus_master_read_delay;
199 /* This value is increased every time we tried to execute two commands
200 * consecutively, and the second one failed because the previous hadn't
201 * completed yet. It's used to add extra run-test/idle cycles after
202 * starting a command, so we don't have to waste time checking for busy to
204 unsigned int ac_busy_delay;
206 bool abstract_read_csr_supported;
207 bool abstract_write_csr_supported;
208 bool abstract_read_fpr_supported;
209 bool abstract_write_fpr_supported;
211 /* When a function returns some error due to a failure indicated by the
212 * target in cmderr, the caller can look here to see what that error was.
213 * (Compare with errno.) */
216 /* Some fields from hartinfo. */
221 /* The width of the hartsel field. */
224 /* DM that provides access to this target. */
230 static riscv013_info_t *get_info(const struct target *target)
232 riscv_info_t *info = (riscv_info_t *) target->arch_info;
233 return (riscv013_info_t *) info->version_specific;
237 * Return the DM structure for this target. If there isn't one, find it in the
238 * global list of DMs. If it's not in there, then create one and initialize it
241 dm013_info_t *get_dm(struct target *target)
247 int abs_chain_position = target->tap->abs_chain_position;
250 dm013_info_t *dm = NULL;
251 list_for_each_entry(entry, &dm_list, list) {
252 if (entry->abs_chain_position == abs_chain_position) {
259 LOG_DEBUG("[%d] Allocating new DM", target->coreid);
260 dm = calloc(1, sizeof(dm013_info_t));
263 dm->abs_chain_position = abs_chain_position;
264 dm->current_hartid = -1;
266 INIT_LIST_HEAD(&dm->target_list);
267 list_add(&dm->list, &dm_list);
271 target_list_t *target_entry;
272 list_for_each_entry(target_entry, &dm->target_list, list) {
273 if (target_entry->target == target)
276 target_entry = calloc(1, sizeof(*target_entry));
281 target_entry->target = target;
282 list_add(&target_entry->list, &dm->target_list);
287 static uint32_t set_hartsel(uint32_t initial, uint32_t index)
289 initial &= ~DM_DMCONTROL_HARTSELLO;
290 initial &= ~DM_DMCONTROL_HARTSELHI;
292 uint32_t index_lo = index & ((1 << DM_DMCONTROL_HARTSELLO_LENGTH) - 1);
293 initial |= index_lo << DM_DMCONTROL_HARTSELLO_OFFSET;
294 uint32_t index_hi = index >> DM_DMCONTROL_HARTSELLO_LENGTH;
295 assert(index_hi < 1 << DM_DMCONTROL_HARTSELHI_LENGTH);
296 initial |= index_hi << DM_DMCONTROL_HARTSELHI_OFFSET;
301 static void decode_dmi(char *text, unsigned address, unsigned data)
303 static const struct {
308 { DM_DMCONTROL, DM_DMCONTROL_HALTREQ, "haltreq" },
309 { DM_DMCONTROL, DM_DMCONTROL_RESUMEREQ, "resumereq" },
310 { DM_DMCONTROL, DM_DMCONTROL_HARTRESET, "hartreset" },
311 { DM_DMCONTROL, DM_DMCONTROL_HASEL, "hasel" },
312 { DM_DMCONTROL, DM_DMCONTROL_HARTSELHI, "hartselhi" },
313 { DM_DMCONTROL, DM_DMCONTROL_HARTSELLO, "hartsello" },
314 { DM_DMCONTROL, DM_DMCONTROL_NDMRESET, "ndmreset" },
315 { DM_DMCONTROL, DM_DMCONTROL_DMACTIVE, "dmactive" },
316 { DM_DMCONTROL, DM_DMCONTROL_ACKHAVERESET, "ackhavereset" },
318 { DM_DMSTATUS, DM_DMSTATUS_IMPEBREAK, "impebreak" },
319 { DM_DMSTATUS, DM_DMSTATUS_ALLHAVERESET, "allhavereset" },
320 { DM_DMSTATUS, DM_DMSTATUS_ANYHAVERESET, "anyhavereset" },
321 { DM_DMSTATUS, DM_DMSTATUS_ALLRESUMEACK, "allresumeack" },
322 { DM_DMSTATUS, DM_DMSTATUS_ANYRESUMEACK, "anyresumeack" },
323 { DM_DMSTATUS, DM_DMSTATUS_ALLNONEXISTENT, "allnonexistent" },
324 { DM_DMSTATUS, DM_DMSTATUS_ANYNONEXISTENT, "anynonexistent" },
325 { DM_DMSTATUS, DM_DMSTATUS_ALLUNAVAIL, "allunavail" },
326 { DM_DMSTATUS, DM_DMSTATUS_ANYUNAVAIL, "anyunavail" },
327 { DM_DMSTATUS, DM_DMSTATUS_ALLRUNNING, "allrunning" },
328 { DM_DMSTATUS, DM_DMSTATUS_ANYRUNNING, "anyrunning" },
329 { DM_DMSTATUS, DM_DMSTATUS_ALLHALTED, "allhalted" },
330 { DM_DMSTATUS, DM_DMSTATUS_ANYHALTED, "anyhalted" },
331 { DM_DMSTATUS, DM_DMSTATUS_AUTHENTICATED, "authenticated" },
332 { DM_DMSTATUS, DM_DMSTATUS_AUTHBUSY, "authbusy" },
333 { DM_DMSTATUS, DM_DMSTATUS_HASRESETHALTREQ, "hasresethaltreq" },
334 { DM_DMSTATUS, DM_DMSTATUS_CONFSTRPTRVALID, "confstrptrvalid" },
335 { DM_DMSTATUS, DM_DMSTATUS_VERSION, "version" },
337 { DM_ABSTRACTCS, DM_ABSTRACTCS_PROGBUFSIZE, "progbufsize" },
338 { DM_ABSTRACTCS, DM_ABSTRACTCS_BUSY, "busy" },
339 { DM_ABSTRACTCS, DM_ABSTRACTCS_CMDERR, "cmderr" },
340 { DM_ABSTRACTCS, DM_ABSTRACTCS_DATACOUNT, "datacount" },
342 { DM_COMMAND, DM_COMMAND_CMDTYPE, "cmdtype" },
344 { DM_SBCS, DM_SBCS_SBVERSION, "sbversion" },
345 { DM_SBCS, DM_SBCS_SBBUSYERROR, "sbbusyerror" },
346 { DM_SBCS, DM_SBCS_SBBUSY, "sbbusy" },
347 { DM_SBCS, DM_SBCS_SBREADONADDR, "sbreadonaddr" },
348 { DM_SBCS, DM_SBCS_SBACCESS, "sbaccess" },
349 { DM_SBCS, DM_SBCS_SBAUTOINCREMENT, "sbautoincrement" },
350 { DM_SBCS, DM_SBCS_SBREADONDATA, "sbreadondata" },
351 { DM_SBCS, DM_SBCS_SBERROR, "sberror" },
352 { DM_SBCS, DM_SBCS_SBASIZE, "sbasize" },
353 { DM_SBCS, DM_SBCS_SBACCESS128, "sbaccess128" },
354 { DM_SBCS, DM_SBCS_SBACCESS64, "sbaccess64" },
355 { DM_SBCS, DM_SBCS_SBACCESS32, "sbaccess32" },
356 { DM_SBCS, DM_SBCS_SBACCESS16, "sbaccess16" },
357 { DM_SBCS, DM_SBCS_SBACCESS8, "sbaccess8" },
361 for (unsigned i = 0; i < DIM(description); i++) {
362 if (description[i].address == address) {
363 uint64_t mask = description[i].mask;
364 unsigned value = get_field(data, mask);
368 if (mask & (mask >> 1)) {
369 /* If the field is more than 1 bit wide. */
370 sprintf(text, "%s=%d", description[i].name, value);
372 strcpy(text, description[i].name);
374 text += strlen(text);
380 static void dump_field(int idle, const struct scan_field *field)
382 static const char * const op_string[] = {"-", "r", "w", "?"};
383 static const char * const status_string[] = {"+", "?", "F", "b"};
385 if (debug_level < LOG_LVL_DEBUG)
388 uint64_t out = buf_get_u64(field->out_value, 0, field->num_bits);
389 unsigned int out_op = get_field(out, DTM_DMI_OP);
390 unsigned int out_data = get_field(out, DTM_DMI_DATA);
391 unsigned int out_address = out >> DTM_DMI_ADDRESS_OFFSET;
393 uint64_t in = buf_get_u64(field->in_value, 0, field->num_bits);
394 unsigned int in_op = get_field(in, DTM_DMI_OP);
395 unsigned int in_data = get_field(in, DTM_DMI_DATA);
396 unsigned int in_address = in >> DTM_DMI_ADDRESS_OFFSET;
398 log_printf_lf(LOG_LVL_DEBUG,
399 __FILE__, __LINE__, "scan",
400 "%db %s %08x @%02x -> %s %08x @%02x; %di",
401 field->num_bits, op_string[out_op], out_data, out_address,
402 status_string[in_op], in_data, in_address, idle);
406 decode_dmi(out_text, out_address, out_data);
407 decode_dmi(in_text, in_address, in_data);
408 if (in_text[0] || out_text[0]) {
409 log_printf_lf(LOG_LVL_DEBUG, __FILE__, __LINE__, "scan", "%s -> %s",
414 /*** Utility functions. ***/
416 static void select_dmi(struct target *target)
418 if (bscan_tunnel_ir_width != 0) {
419 select_dmi_via_bscan(target);
422 jtag_add_ir_scan(target->tap, &select_dbus, TAP_IDLE);
425 static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
427 struct scan_field field;
429 uint8_t out_value[4] = { 0 };
431 if (bscan_tunnel_ir_width != 0)
432 return dtmcontrol_scan_via_bscan(target, out);
434 buf_set_u32(out_value, 0, 32, out);
436 jtag_add_ir_scan(target->tap, &select_dtmcontrol, TAP_IDLE);
439 field.out_value = out_value;
440 field.in_value = in_value;
441 jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
443 /* Always return to dmi. */
446 int retval = jtag_execute_queue();
447 if (retval != ERROR_OK) {
448 LOG_ERROR("failed jtag scan: %d", retval);
452 uint32_t in = buf_get_u32(field.in_value, 0, 32);
453 LOG_DEBUG("DTMCS: 0x%x -> 0x%x", out, in);
458 static void increase_dmi_busy_delay(struct target *target)
460 riscv013_info_t *info = get_info(target);
461 info->dmi_busy_delay += info->dmi_busy_delay / 10 + 1;
462 LOG_DEBUG("dtmcs_idle=%d, dmi_busy_delay=%d, ac_busy_delay=%d",
463 info->dtmcs_idle, info->dmi_busy_delay,
464 info->ac_busy_delay);
466 dtmcontrol_scan(target, DTM_DTMCS_DMIRESET);
470 * exec: If this is set, assume the scan results in an execution, so more
471 * run-test/idle cycles may be required.
473 static dmi_status_t dmi_scan(struct target *target, uint32_t *address_in,
474 uint32_t *data_in, dmi_op_t op, uint32_t address_out, uint32_t data_out,
477 riscv013_info_t *info = get_info(target);
479 unsigned num_bits = info->abits + DTM_DMI_OP_LENGTH + DTM_DMI_DATA_LENGTH;
480 size_t num_bytes = (num_bits + 7) / 8;
481 uint8_t in[num_bytes];
482 uint8_t out[num_bytes];
483 struct scan_field field = {
484 .num_bits = num_bits,
488 riscv_bscan_tunneled_scan_context_t bscan_ctxt;
490 if (r->reset_delays_wait >= 0) {
491 r->reset_delays_wait--;
492 if (r->reset_delays_wait < 0) {
493 info->dmi_busy_delay = 0;
494 info->ac_busy_delay = 0;
498 memset(in, 0, num_bytes);
499 memset(out, 0, num_bytes);
501 assert(info->abits != 0);
503 buf_set_u32(out, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, op);
504 buf_set_u32(out, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, data_out);
505 buf_set_u32(out, DTM_DMI_ADDRESS_OFFSET, info->abits, address_out);
507 /* I wanted to place this code in a different function, but the way JTAG command
508 queueing works in the jtag handling functions, the scan fields either have to be
509 heap allocated, global/static, or else they need to stay on the stack until
510 the jtag_execute_queue() call. Heap or static fields in this case doesn't seem
511 the best fit. Declaring stack based field values in a subsidiary function call wouldn't
513 if (bscan_tunnel_ir_width != 0) {
514 riscv_add_bscan_tunneled_scan(target, &field, &bscan_ctxt);
516 /* Assume dbus is already selected. */
517 jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
520 int idle_count = info->dmi_busy_delay;
522 idle_count += info->ac_busy_delay;
525 jtag_add_runtest(idle_count, TAP_IDLE);
527 int retval = jtag_execute_queue();
528 if (retval != ERROR_OK) {
529 LOG_ERROR("dmi_scan failed jtag scan");
532 return DMI_STATUS_FAILED;
535 if (bscan_tunnel_ir_width != 0) {
536 /* need to right-shift "in" by one bit, because of clock skew between BSCAN TAP and DM TAP */
537 buffer_shr(in, num_bytes, 1);
541 *data_in = buf_get_u32(in, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH);
544 *address_in = buf_get_u32(in, DTM_DMI_ADDRESS_OFFSET, info->abits);
545 dump_field(idle_count, &field);
546 return buf_get_u32(in, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH);
551 * @param data_in The data we received from the target.
552 * @param dmi_busy_encountered
553 * If non-NULL, will be updated to reflect whether DMI busy was
554 * encountered while executing this operation or not.
555 * @param dmi_op The operation to perform (read/write/nop).
556 * @param address The address argument to that operation.
557 * @param data_out The data to send to the target.
559 * @param exec When true, this scan will execute something, so extra RTI
560 * cycles may be added.
561 * @param ensure_success
562 * Scan a nop after the requested operation, ensuring the
563 * DMI operation succeeded.
565 static int dmi_op_timeout(struct target *target, uint32_t *data_in,
566 bool *dmi_busy_encountered, int dmi_op, uint32_t address,
567 uint32_t data_out, int timeout_sec, bool exec, bool ensure_success)
574 if (dmi_busy_encountered)
575 *dmi_busy_encountered = false;
589 LOG_ERROR("Invalid DMI operation: %d", dmi_op);
593 time_t start = time(NULL);
594 /* This first loop performs the request. Note that if for some reason this
595 * stays busy, it is actually due to the previous access. */
597 status = dmi_scan(target, NULL, NULL, dmi_op, address, data_out,
599 if (status == DMI_STATUS_BUSY) {
600 increase_dmi_busy_delay(target);
601 if (dmi_busy_encountered)
602 *dmi_busy_encountered = true;
603 } else if (status == DMI_STATUS_SUCCESS) {
606 LOG_ERROR("failed %s at 0x%x, status=%d", op_name, address, status);
609 if (time(NULL) - start > timeout_sec)
610 return ERROR_TIMEOUT_REACHED;
613 if (status != DMI_STATUS_SUCCESS) {
614 LOG_ERROR("Failed %s at 0x%x; status=%d", op_name, address, status);
618 if (ensure_success) {
619 /* This second loop ensures the request succeeded, and gets back data.
620 * Note that NOP can result in a 'busy' result as well, but that would be
621 * noticed on the next DMI access we do. */
623 status = dmi_scan(target, &address_in, data_in, DMI_OP_NOP, address, 0,
625 if (status == DMI_STATUS_BUSY) {
626 increase_dmi_busy_delay(target);
627 if (dmi_busy_encountered)
628 *dmi_busy_encountered = true;
629 } else if (status == DMI_STATUS_SUCCESS) {
633 LOG_ERROR("Failed %s (NOP) at 0x%x; value=0x%x, status=%d",
634 op_name, address, *data_in, status);
636 LOG_ERROR("Failed %s (NOP) at 0x%x; status=%d", op_name, address,
641 if (time(NULL) - start > timeout_sec)
642 return ERROR_TIMEOUT_REACHED;
649 static int dmi_op(struct target *target, uint32_t *data_in,
650 bool *dmi_busy_encountered, int dmi_op, uint32_t address,
651 uint32_t data_out, bool exec, bool ensure_success)
653 int result = dmi_op_timeout(target, data_in, dmi_busy_encountered, dmi_op,
654 address, data_out, riscv_command_timeout_sec, exec, ensure_success);
655 if (result == ERROR_TIMEOUT_REACHED) {
656 LOG_ERROR("DMI operation didn't complete in %d seconds. The target is "
657 "either really slow or broken. You could increase the "
658 "timeout with riscv set_command_timeout_sec.",
659 riscv_command_timeout_sec);
665 static int dmi_read(struct target *target, uint32_t *value, uint32_t address)
667 return dmi_op(target, value, NULL, DMI_OP_READ, address, 0, false, true);
670 static int dmi_read_exec(struct target *target, uint32_t *value, uint32_t address)
672 return dmi_op(target, value, NULL, DMI_OP_READ, address, 0, true, true);
675 static int dmi_write(struct target *target, uint32_t address, uint32_t value)
677 return dmi_op(target, NULL, NULL, DMI_OP_WRITE, address, value, false, true);
680 static int dmi_write_exec(struct target *target, uint32_t address,
681 uint32_t value, bool ensure_success)
683 return dmi_op(target, NULL, NULL, DMI_OP_WRITE, address, value, true, ensure_success);
686 int dmstatus_read_timeout(struct target *target, uint32_t *dmstatus,
687 bool authenticated, unsigned timeout_sec)
689 int result = dmi_op_timeout(target, dmstatus, NULL, DMI_OP_READ,
690 DM_DMSTATUS, 0, timeout_sec, false, true);
691 if (result != ERROR_OK)
693 int dmstatus_version = get_field(*dmstatus, DM_DMSTATUS_VERSION);
694 if (dmstatus_version != 2 && dmstatus_version != 3) {
695 LOG_ERROR("OpenOCD only supports Debug Module version 2 (0.13) and 3 (0.14), not "
696 "%d (dmstatus=0x%x). This error might be caused by a JTAG "
697 "signal issue. Try reducing the JTAG clock speed.",
698 get_field(*dmstatus, DM_DMSTATUS_VERSION), *dmstatus);
699 } else if (authenticated && !get_field(*dmstatus, DM_DMSTATUS_AUTHENTICATED)) {
700 LOG_ERROR("Debugger is not authenticated to target Debug Module. "
701 "(dmstatus=0x%x). Use `riscv authdata_read` and "
702 "`riscv authdata_write` commands to authenticate.", *dmstatus);
708 int dmstatus_read(struct target *target, uint32_t *dmstatus,
711 return dmstatus_read_timeout(target, dmstatus, authenticated,
712 riscv_command_timeout_sec);
715 static void increase_ac_busy_delay(struct target *target)
717 riscv013_info_t *info = get_info(target);
718 info->ac_busy_delay += info->ac_busy_delay / 10 + 1;
719 LOG_DEBUG("dtmcs_idle=%d, dmi_busy_delay=%d, ac_busy_delay=%d",
720 info->dtmcs_idle, info->dmi_busy_delay,
721 info->ac_busy_delay);
724 uint32_t abstract_register_size(unsigned width)
728 return set_field(0, AC_ACCESS_REGISTER_AARSIZE, 2);
730 return set_field(0, AC_ACCESS_REGISTER_AARSIZE, 3);
732 return set_field(0, AC_ACCESS_REGISTER_AARSIZE, 4);
734 LOG_ERROR("Unsupported register width: %d", width);
739 static int wait_for_idle(struct target *target, uint32_t *abstractcs)
742 time_t start = time(NULL);
744 if (dmi_read(target, abstractcs, DM_ABSTRACTCS) != ERROR_OK)
747 if (get_field(*abstractcs, DM_ABSTRACTCS_BUSY) == 0)
750 if (time(NULL) - start > riscv_command_timeout_sec) {
751 info->cmderr = get_field(*abstractcs, DM_ABSTRACTCS_CMDERR);
752 if (info->cmderr != CMDERR_NONE) {
753 const char *errors[8] = {
763 LOG_ERROR("Abstract command ended in error '%s' (abstractcs=0x%x)",
764 errors[info->cmderr], *abstractcs);
767 LOG_ERROR("Timed out after %ds waiting for busy to go low (abstractcs=0x%x). "
768 "Increase the timeout with riscv set_command_timeout_sec.",
769 riscv_command_timeout_sec,
776 static int execute_abstract_command(struct target *target, uint32_t command)
779 if (debug_level >= LOG_LVL_DEBUG) {
780 switch (get_field(command, DM_COMMAND_CMDTYPE)) {
782 LOG_DEBUG("command=0x%x; access register, size=%d, postexec=%d, "
783 "transfer=%d, write=%d, regno=0x%x",
785 8 << get_field(command, AC_ACCESS_REGISTER_AARSIZE),
786 get_field(command, AC_ACCESS_REGISTER_POSTEXEC),
787 get_field(command, AC_ACCESS_REGISTER_TRANSFER),
788 get_field(command, AC_ACCESS_REGISTER_WRITE),
789 get_field(command, AC_ACCESS_REGISTER_REGNO));
792 LOG_DEBUG("command=0x%x", command);
797 if (dmi_write_exec(target, DM_COMMAND, command, false) != ERROR_OK)
800 uint32_t abstractcs = 0;
801 int result = wait_for_idle(target, &abstractcs);
803 info->cmderr = get_field(abstractcs, DM_ABSTRACTCS_CMDERR);
804 if (info->cmderr != 0 || result != ERROR_OK) {
805 LOG_DEBUG("command 0x%x failed; abstractcs=0x%x", command, abstractcs);
806 /* Clear the error. */
807 dmi_write(target, DM_ABSTRACTCS, DM_ABSTRACTCS_CMDERR);
814 static riscv_reg_t read_abstract_arg(struct target *target, unsigned index,
817 riscv_reg_t value = 0;
819 unsigned offset = index * size_bits / 32;
822 LOG_ERROR("Unsupported size: %d bits", size_bits);
825 dmi_read(target, &v, DM_DATA0 + offset + 1);
826 value |= ((uint64_t) v) << 32;
829 dmi_read(target, &v, DM_DATA0 + offset);
835 static int write_abstract_arg(struct target *target, unsigned index,
836 riscv_reg_t value, unsigned size_bits)
838 unsigned offset = index * size_bits / 32;
841 LOG_ERROR("Unsupported size: %d bits", size_bits);
844 dmi_write(target, DM_DATA0 + offset + 1, value >> 32);
847 dmi_write(target, DM_DATA0 + offset, value);
855 static uint32_t access_register_command(struct target *target, uint32_t number,
856 unsigned size, uint32_t flags)
858 uint32_t command = set_field(0, DM_COMMAND_CMDTYPE, 0);
861 command = set_field(command, AC_ACCESS_REGISTER_AARSIZE, 2);
864 command = set_field(command, AC_ACCESS_REGISTER_AARSIZE, 3);
867 LOG_ERROR("%d-bit register %s not supported.", size,
868 gdb_regno_name(number));
872 if (number <= GDB_REGNO_XPR31) {
873 command = set_field(command, AC_ACCESS_REGISTER_REGNO,
874 0x1000 + number - GDB_REGNO_ZERO);
875 } else if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
876 command = set_field(command, AC_ACCESS_REGISTER_REGNO,
877 0x1020 + number - GDB_REGNO_FPR0);
878 } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
879 command = set_field(command, AC_ACCESS_REGISTER_REGNO,
880 number - GDB_REGNO_CSR0);
881 } else if (number >= GDB_REGNO_COUNT) {
882 /* Custom register. */
883 assert(target->reg_cache->reg_list[number].arch_info);
884 riscv_reg_info_t *reg_info = target->reg_cache->reg_list[number].arch_info;
886 command = set_field(command, AC_ACCESS_REGISTER_REGNO,
887 0xc000 + reg_info->custom_number);
897 static int register_read_abstract(struct target *target, uint64_t *value,
898 uint32_t number, unsigned size)
902 if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31 &&
903 !info->abstract_read_fpr_supported)
905 if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095 &&
906 !info->abstract_read_csr_supported)
908 /* The spec doesn't define abstract register numbers for vector registers. */
909 if (number >= GDB_REGNO_V0 && number <= GDB_REGNO_V31)
912 uint32_t command = access_register_command(target, number, size,
913 AC_ACCESS_REGISTER_TRANSFER);
915 int result = execute_abstract_command(target, command);
916 if (result != ERROR_OK) {
917 if (info->cmderr == CMDERR_NOT_SUPPORTED) {
918 if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
919 info->abstract_read_fpr_supported = false;
920 LOG_INFO("Disabling abstract command reads from FPRs.");
921 } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
922 info->abstract_read_csr_supported = false;
923 LOG_INFO("Disabling abstract command reads from CSRs.");
930 *value = read_abstract_arg(target, 0, size);
935 static int register_write_abstract(struct target *target, uint32_t number,
936 uint64_t value, unsigned size)
940 if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31 &&
941 !info->abstract_write_fpr_supported)
943 if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095 &&
944 !info->abstract_write_csr_supported)
947 uint32_t command = access_register_command(target, number, size,
948 AC_ACCESS_REGISTER_TRANSFER |
949 AC_ACCESS_REGISTER_WRITE);
951 if (write_abstract_arg(target, 0, value, size) != ERROR_OK)
954 int result = execute_abstract_command(target, command);
955 if (result != ERROR_OK) {
956 if (info->cmderr == CMDERR_NOT_SUPPORTED) {
957 if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
958 info->abstract_write_fpr_supported = false;
959 LOG_INFO("Disabling abstract command writes to FPRs.");
960 } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
961 info->abstract_write_csr_supported = false;
962 LOG_INFO("Disabling abstract command writes to CSRs.");
972 * Sets the AAMSIZE field of a memory access abstract command based on
975 static uint32_t abstract_memory_size(unsigned width)
979 return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 0);
981 return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 1);
983 return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 2);
985 return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 3);
987 return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 4);
989 LOG_ERROR("Unsupported memory width: %d", width);
995 * Creates a memory access abstract command.
997 static uint32_t access_memory_command(struct target *target, bool virtual,
998 unsigned width, bool postincrement, bool write)
1000 uint32_t command = set_field(0, AC_ACCESS_MEMORY_CMDTYPE, 2);
1001 command = set_field(command, AC_ACCESS_MEMORY_AAMVIRTUAL, virtual);
1002 command |= abstract_memory_size(width);
1003 command = set_field(command, AC_ACCESS_MEMORY_AAMPOSTINCREMENT,
1005 command = set_field(command, AC_ACCESS_MEMORY_WRITE, write);
1010 static int examine_progbuf(struct target *target)
1012 riscv013_info_t *info = get_info(target);
1014 if (info->progbuf_writable != YNM_MAYBE)
1017 /* Figure out if progbuf is writable. */
1019 if (info->progbufsize < 1) {
1020 info->progbuf_writable = YNM_NO;
1021 LOG_INFO("No program buffer present.");
1026 if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
1029 struct riscv_program program;
1030 riscv_program_init(&program, target);
1031 riscv_program_insert(&program, auipc(S0));
1032 if (riscv_program_exec(&program, target) != ERROR_OK)
1035 if (register_read_direct(target, &info->progbuf_address, GDB_REGNO_S0) != ERROR_OK)
1038 riscv_program_init(&program, target);
1039 riscv_program_insert(&program, sw(S0, S0, 0));
1040 int result = riscv_program_exec(&program, target);
1042 if (register_write_direct(target, GDB_REGNO_S0, s0) != ERROR_OK)
1045 if (result != ERROR_OK) {
1046 /* This program might have failed if the program buffer is not
1048 info->progbuf_writable = YNM_NO;
1053 if (dmi_read(target, &written, DM_PROGBUF0) != ERROR_OK)
1055 if (written == (uint32_t) info->progbuf_address) {
1056 LOG_INFO("progbuf is writable at 0x%" PRIx64,
1057 info->progbuf_address);
1058 info->progbuf_writable = YNM_YES;
1061 LOG_INFO("progbuf is not writeable at 0x%" PRIx64,
1062 info->progbuf_address);
1063 info->progbuf_writable = YNM_NO;
1069 static int is_fpu_reg(uint32_t gdb_regno)
1071 return (gdb_regno >= GDB_REGNO_FPR0 && gdb_regno <= GDB_REGNO_FPR31) ||
1072 (gdb_regno == GDB_REGNO_CSR0 + CSR_FFLAGS) ||
1073 (gdb_regno == GDB_REGNO_CSR0 + CSR_FRM) ||
1074 (gdb_regno == GDB_REGNO_CSR0 + CSR_FCSR);
1077 static int is_vector_reg(uint32_t gdb_regno)
1079 return (gdb_regno >= GDB_REGNO_V0 && gdb_regno <= GDB_REGNO_V31) ||
1080 gdb_regno == GDB_REGNO_VSTART ||
1081 gdb_regno == GDB_REGNO_VXSAT ||
1082 gdb_regno == GDB_REGNO_VXRM ||
1083 gdb_regno == GDB_REGNO_VL ||
1084 gdb_regno == GDB_REGNO_VTYPE ||
1085 gdb_regno == GDB_REGNO_VLENB;
1088 static int prep_for_register_access(struct target *target, uint64_t *mstatus,
1091 if (is_fpu_reg(regno) || is_vector_reg(regno)) {
1092 if (register_read(target, mstatus, GDB_REGNO_MSTATUS) != ERROR_OK)
1094 if (is_fpu_reg(regno) && (*mstatus & MSTATUS_FS) == 0) {
1095 if (register_write_direct(target, GDB_REGNO_MSTATUS,
1096 set_field(*mstatus, MSTATUS_FS, 1)) != ERROR_OK)
1098 } else if (is_vector_reg(regno) && (*mstatus & MSTATUS_VS) == 0) {
1099 if (register_write_direct(target, GDB_REGNO_MSTATUS,
1100 set_field(*mstatus, MSTATUS_VS, 1)) != ERROR_OK)
1109 static int cleanup_after_register_access(struct target *target,
1110 uint64_t mstatus, int regno)
1112 if ((is_fpu_reg(regno) && (mstatus & MSTATUS_FS) == 0) ||
1113 (is_vector_reg(regno) && (mstatus & MSTATUS_VS) == 0))
1114 if (register_write_direct(target, GDB_REGNO_MSTATUS, mstatus) != ERROR_OK)
1126 /* How can the debugger access this memory? */
1127 memory_space_t memory_space;
1128 /* Memory address to access the scratch memory from the hart. */
1129 riscv_addr_t hart_address;
1130 /* Memory address to access the scratch memory from the debugger. */
1131 riscv_addr_t debug_address;
1132 struct working_area *area;
1136 * Find some scratch memory to be used with the given program.
1138 static int scratch_reserve(struct target *target,
1139 scratch_mem_t *scratch,
1140 struct riscv_program *program,
1141 unsigned size_bytes)
1143 riscv_addr_t alignment = 1;
1144 while (alignment < size_bytes)
1147 scratch->area = NULL;
1149 riscv013_info_t *info = get_info(target);
1151 /* Option 1: See if data# registers can be used as the scratch memory */
1152 if (info->dataaccess == 1) {
1153 /* Sign extend dataaddr. */
1154 scratch->hart_address = info->dataaddr;
1155 if (info->dataaddr & (1<<11))
1156 scratch->hart_address |= 0xfffffffffffff000ULL;
1158 scratch->hart_address = (scratch->hart_address + alignment - 1) & ~(alignment - 1);
1160 if ((size_bytes + scratch->hart_address - info->dataaddr + 3) / 4 >=
1162 scratch->memory_space = SPACE_DM_DATA;
1163 scratch->debug_address = (scratch->hart_address - info->dataaddr) / 4;
1168 /* Option 2: See if progbuf can be used as the scratch memory */
1169 if (examine_progbuf(target) != ERROR_OK)
1172 /* Allow for ebreak at the end of the program. */
1173 unsigned program_size = (program->instruction_count + 1) * 4;
1174 scratch->hart_address = (info->progbuf_address + program_size + alignment - 1) &
1176 if ((info->progbuf_writable == YNM_YES) &&
1177 ((size_bytes + scratch->hart_address - info->progbuf_address + 3) / 4 >=
1178 info->progbufsize)) {
1179 scratch->memory_space = SPACE_DMI_PROGBUF;
1180 scratch->debug_address = (scratch->hart_address - info->progbuf_address) / 4;
1184 /* Option 3: User-configured memory area as scratch RAM */
1185 if (target_alloc_working_area(target, size_bytes + alignment - 1,
1186 &scratch->area) == ERROR_OK) {
1187 scratch->hart_address = (scratch->area->address + alignment - 1) &
1189 scratch->memory_space = SPACE_DMI_RAM;
1190 scratch->debug_address = scratch->hart_address;
1194 LOG_ERROR("Couldn't find %d bytes of scratch RAM to use. Please configure "
1195 "a work area with 'configure -work-area-phys'.", size_bytes);
1199 static int scratch_release(struct target *target,
1200 scratch_mem_t *scratch)
1203 return target_free_working_area(target, scratch->area);
1208 static int scratch_read64(struct target *target, scratch_mem_t *scratch,
1212 switch (scratch->memory_space) {
1214 if (dmi_read(target, &v, DM_DATA0 + scratch->debug_address) != ERROR_OK)
1217 if (dmi_read(target, &v, DM_DATA1 + scratch->debug_address) != ERROR_OK)
1219 *value |= ((uint64_t) v) << 32;
1221 case SPACE_DMI_PROGBUF:
1222 if (dmi_read(target, &v, DM_PROGBUF0 + scratch->debug_address) != ERROR_OK)
1225 if (dmi_read(target, &v, DM_PROGBUF1 + scratch->debug_address) != ERROR_OK)
1227 *value |= ((uint64_t) v) << 32;
1231 uint8_t buffer[8] = {0};
1232 if (read_memory(target, scratch->debug_address, 4, 2, buffer, 4) != ERROR_OK)
1234 *value = buffer[0] |
1235 (((uint64_t) buffer[1]) << 8) |
1236 (((uint64_t) buffer[2]) << 16) |
1237 (((uint64_t) buffer[3]) << 24) |
1238 (((uint64_t) buffer[4]) << 32) |
1239 (((uint64_t) buffer[5]) << 40) |
1240 (((uint64_t) buffer[6]) << 48) |
1241 (((uint64_t) buffer[7]) << 56);
1248 static int scratch_write64(struct target *target, scratch_mem_t *scratch,
1251 switch (scratch->memory_space) {
1253 dmi_write(target, DM_DATA0 + scratch->debug_address, value);
1254 dmi_write(target, DM_DATA1 + scratch->debug_address, value >> 32);
1256 case SPACE_DMI_PROGBUF:
1257 dmi_write(target, DM_PROGBUF0 + scratch->debug_address, value);
1258 dmi_write(target, DM_PROGBUF1 + scratch->debug_address, value >> 32);
1262 uint8_t buffer[8] = {
1272 if (write_memory(target, scratch->debug_address, 4, 2, buffer) != ERROR_OK)
1280 /** Return register size in bits. */
1281 static unsigned register_size(struct target *target, unsigned number)
1283 /* If reg_cache hasn't been initialized yet, make a guess. We need this for
1284 * when this function is called during examine(). */
1285 if (target->reg_cache)
1286 return target->reg_cache->reg_list[number].size;
1288 return riscv_xlen(target);
1291 static bool has_sufficient_progbuf(struct target *target, unsigned size)
1293 RISCV013_INFO(info);
1296 return info->progbufsize + r->impebreak >= size;
1300 * Immediately write the new value to the requested register. This mechanism
1301 * bypasses any caches.
1303 static int register_write_direct(struct target *target, unsigned number,
1306 LOG_DEBUG("{%d} %s <- 0x%" PRIx64, riscv_current_hartid(target),
1307 gdb_regno_name(number), value);
1309 int result = register_write_abstract(target, number, value,
1310 register_size(target, number));
1311 if (result == ERROR_OK || !has_sufficient_progbuf(target, 2) ||
1312 !riscv_is_halted(target))
1315 struct riscv_program program;
1316 riscv_program_init(&program, target);
1319 if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
1323 if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1326 scratch_mem_t scratch;
1327 bool use_scratch = false;
1328 if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31 &&
1329 riscv_supports_extension(target, riscv_current_hartid(target), 'D') &&
1330 riscv_xlen(target) < 64) {
1331 /* There are no instructions to move all the bits from a register, so
1332 * we need to use some scratch RAM. */
1334 riscv_program_insert(&program, fld(number - GDB_REGNO_FPR0, S0, 0));
1336 if (scratch_reserve(target, &scratch, &program, 8) != ERROR_OK)
1339 if (register_write_direct(target, GDB_REGNO_S0, scratch.hart_address)
1341 scratch_release(target, &scratch);
1345 if (scratch_write64(target, &scratch, value) != ERROR_OK) {
1346 scratch_release(target, &scratch);
1350 } else if (number == GDB_REGNO_VTYPE) {
1351 riscv_program_insert(&program, csrr(S0, CSR_VL));
1352 riscv_program_insert(&program, vsetvli(ZERO, S0, value));
1355 if (register_write_direct(target, GDB_REGNO_S0, value) != ERROR_OK)
1358 if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
1359 if (riscv_supports_extension(target, riscv_current_hartid(target), 'D'))
1360 riscv_program_insert(&program, fmv_d_x(number - GDB_REGNO_FPR0, S0));
1362 riscv_program_insert(&program, fmv_w_x(number - GDB_REGNO_FPR0, S0));
1363 } else if (number == GDB_REGNO_VL) {
1364 /* "The XLEN-bit-wide read-only vl CSR can only be updated by the
1365 * vsetvli and vsetvl instructions, and the fault-only-rst vector
1366 * load instruction variants." */
1368 if (register_read(target, &vtype, GDB_REGNO_VTYPE) != ERROR_OK)
1370 if (riscv_program_insert(&program, vsetvli(ZERO, S0, vtype)) != ERROR_OK)
1372 } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
1373 riscv_program_csrw(&program, S0, number);
1375 LOG_ERROR("Unsupported register (enum gdb_regno)(%d)", number);
1380 int exec_out = riscv_program_exec(&program, target);
1381 /* Don't message on error. Probably the register doesn't exist. */
1382 if (exec_out == ERROR_OK && target->reg_cache) {
1383 struct reg *reg = &target->reg_cache->reg_list[number];
1384 buf_set_u64(reg->value, 0, reg->size, value);
1388 scratch_release(target, &scratch);
1390 if (cleanup_after_register_access(target, mstatus, number) != ERROR_OK)
1394 if (register_write_direct(target, GDB_REGNO_S0, s0) != ERROR_OK)
1400 /** Return the cached value, or read from the target if necessary. */
1401 static int register_read(struct target *target, uint64_t *value, uint32_t number)
1403 if (number == GDB_REGNO_ZERO) {
1407 int result = register_read_direct(target, value, number);
1408 if (result != ERROR_OK)
1410 if (target->reg_cache) {
1411 struct reg *reg = &target->reg_cache->reg_list[number];
1412 buf_set_u64(reg->value, 0, reg->size, *value);
1417 /** Actually read registers from the target right now. */
1418 static int register_read_direct(struct target *target, uint64_t *value, uint32_t number)
1420 int result = register_read_abstract(target, value, number,
1421 register_size(target, number));
1423 if (result != ERROR_OK &&
1424 has_sufficient_progbuf(target, 2) &&
1425 number > GDB_REGNO_XPR31) {
1426 struct riscv_program program;
1427 riscv_program_init(&program, target);
1429 scratch_mem_t scratch;
1430 bool use_scratch = false;
1433 if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
1436 /* Write program to move data into s0. */
1439 if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1442 if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
1443 if (riscv_supports_extension(target, riscv_current_hartid(target), 'D')
1444 && riscv_xlen(target) < 64) {
1445 /* There are no instructions to move all the bits from a
1446 * register, so we need to use some scratch RAM. */
1447 riscv_program_insert(&program, fsd(number - GDB_REGNO_FPR0, S0,
1450 if (scratch_reserve(target, &scratch, &program, 8) != ERROR_OK)
1454 if (register_write_direct(target, GDB_REGNO_S0,
1455 scratch.hart_address) != ERROR_OK) {
1456 scratch_release(target, &scratch);
1459 } else if (riscv_supports_extension(target,
1460 riscv_current_hartid(target), 'D')) {
1461 riscv_program_insert(&program, fmv_x_d(S0, number - GDB_REGNO_FPR0));
1463 riscv_program_insert(&program, fmv_x_w(S0, number - GDB_REGNO_FPR0));
1465 } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
1466 riscv_program_csrr(&program, S0, number);
1468 LOG_ERROR("Unsupported register: %s", gdb_regno_name(number));
1472 /* Execute program. */
1473 result = riscv_program_exec(&program, target);
1474 /* Don't message on error. Probably the register doesn't exist. */
1477 result = scratch_read64(target, &scratch, value);
1478 scratch_release(target, &scratch);
1479 if (result != ERROR_OK)
1483 if (register_read_direct(target, value, GDB_REGNO_S0) != ERROR_OK)
1487 if (cleanup_after_register_access(target, mstatus, number) != ERROR_OK)
1491 if (register_write_direct(target, GDB_REGNO_S0, s0) != ERROR_OK)
1495 if (result == ERROR_OK) {
1496 LOG_DEBUG("{%d} %s = 0x%" PRIx64, riscv_current_hartid(target),
1497 gdb_regno_name(number), *value);
1503 int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
1505 time_t start = time(NULL);
1508 if (dmstatus_read(target, &value, false) != ERROR_OK)
1512 if (!get_field(value, DM_DMSTATUS_AUTHBUSY))
1514 if (time(NULL) - start > riscv_command_timeout_sec) {
1515 LOG_ERROR("Timed out after %ds waiting for authbusy to go low (dmstatus=0x%x). "
1516 "Increase the timeout with riscv set_command_timeout_sec.",
1517 riscv_command_timeout_sec,
1526 /*** OpenOCD target functions. ***/
1528 static void deinit_target(struct target *target)
1530 LOG_DEBUG("riscv_deinit_target()");
1531 riscv_info_t *info = (riscv_info_t *) target->arch_info;
1532 free(info->version_specific);
1533 /* TODO: free register arch_info */
1534 info->version_specific = NULL;
1537 static int set_haltgroup(struct target *target, bool *supported)
1539 uint32_t write = set_field(DM_DMCS2_HGWRITE, DM_DMCS2_GROUP, target->smp);
1540 if (dmi_write(target, DM_DMCS2, write) != ERROR_OK)
1543 if (dmi_read(target, &read, DM_DMCS2) != ERROR_OK)
1545 *supported = get_field(read, DM_DMCS2_GROUP) == (unsigned)target->smp;
1549 static int discover_vlenb(struct target *target, int hartid)
1554 if (register_read(target, &vlenb, GDB_REGNO_VLENB) != ERROR_OK) {
1555 LOG_WARNING("Couldn't read vlenb for %s; vector register access won't work.",
1556 target_name(target));
1557 r->vlenb[hartid] = 0;
1560 r->vlenb[hartid] = vlenb;
1562 LOG_INFO("hart %d: Vector support with vlenb=%d", hartid, r->vlenb[hartid]);
1567 static int examine(struct target *target)
1569 /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
1571 uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
1572 LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol);
1573 LOG_DEBUG(" dmireset=%d", get_field(dtmcontrol, DTM_DTMCS_DMIRESET));
1574 LOG_DEBUG(" idle=%d", get_field(dtmcontrol, DTM_DTMCS_IDLE));
1575 LOG_DEBUG(" dmistat=%d", get_field(dtmcontrol, DTM_DTMCS_DMISTAT));
1576 LOG_DEBUG(" abits=%d", get_field(dtmcontrol, DTM_DTMCS_ABITS));
1577 LOG_DEBUG(" version=%d", get_field(dtmcontrol, DTM_DTMCS_VERSION));
1578 if (dtmcontrol == 0) {
1579 LOG_ERROR("dtmcontrol is 0. Check JTAG connectivity/board power.");
1582 if (get_field(dtmcontrol, DTM_DTMCS_VERSION) != 1) {
1583 LOG_ERROR("Unsupported DTM version %d. (dtmcontrol=0x%x)",
1584 get_field(dtmcontrol, DTM_DTMCS_VERSION), dtmcontrol);
1588 riscv013_info_t *info = get_info(target);
1589 /* TODO: This won't be true if there are multiple DMs. */
1590 info->index = target->coreid;
1591 info->abits = get_field(dtmcontrol, DTM_DTMCS_ABITS);
1592 info->dtmcs_idle = get_field(dtmcontrol, DTM_DTMCS_IDLE);
1594 /* Reset the Debug Module. */
1595 dm013_info_t *dm = get_dm(target);
1598 if (!dm->was_reset) {
1599 dmi_write(target, DM_DMCONTROL, 0);
1600 dmi_write(target, DM_DMCONTROL, DM_DMCONTROL_DMACTIVE);
1601 dm->was_reset = true;
1604 dmi_write(target, DM_DMCONTROL, DM_DMCONTROL_HARTSELLO |
1605 DM_DMCONTROL_HARTSELHI | DM_DMCONTROL_DMACTIVE |
1606 DM_DMCONTROL_HASEL);
1608 if (dmi_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
1611 if (!get_field(dmcontrol, DM_DMCONTROL_DMACTIVE)) {
1612 LOG_ERROR("Debug Module did not become active. dmcontrol=0x%x",
1617 dm->hasel_supported = get_field(dmcontrol, DM_DMCONTROL_HASEL);
1620 if (dmstatus_read(target, &dmstatus, false) != ERROR_OK)
1622 LOG_DEBUG("dmstatus: 0x%08x", dmstatus);
1623 int dmstatus_version = get_field(dmstatus, DM_DMSTATUS_VERSION);
1624 if (dmstatus_version != 2 && dmstatus_version != 3) {
1625 /* Error was already printed out in dmstatus_read(). */
1630 (get_field(dmcontrol, DM_DMCONTROL_HARTSELHI) <<
1631 DM_DMCONTROL_HARTSELLO_LENGTH) |
1632 get_field(dmcontrol, DM_DMCONTROL_HARTSELLO);
1633 info->hartsellen = 0;
1634 while (hartsel & 1) {
1638 LOG_DEBUG("hartsellen=%d", info->hartsellen);
1641 if (dmi_read(target, &hartinfo, DM_HARTINFO) != ERROR_OK)
1644 info->datasize = get_field(hartinfo, DM_HARTINFO_DATASIZE);
1645 info->dataaccess = get_field(hartinfo, DM_HARTINFO_DATAACCESS);
1646 info->dataaddr = get_field(hartinfo, DM_HARTINFO_DATAADDR);
1648 if (!get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED)) {
1649 LOG_ERROR("Debugger is not authenticated to target Debug Module. "
1650 "(dmstatus=0x%x). Use `riscv authdata_read` and "
1651 "`riscv authdata_write` commands to authenticate.", dmstatus);
1652 /* If we return ERROR_FAIL here, then in a multicore setup the next
1653 * core won't be examined, which means we won't set up the
1654 * authentication commands for them, which means the config script
1655 * needs to be a lot more complex. */
1659 if (dmi_read(target, &info->sbcs, DM_SBCS) != ERROR_OK)
1662 /* Check that abstract data registers are accessible. */
1663 uint32_t abstractcs;
1664 if (dmi_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
1666 info->datacount = get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT);
1667 info->progbufsize = get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE);
1669 LOG_INFO("datacount=%d progbufsize=%d", info->datacount, info->progbufsize);
1672 r->impebreak = get_field(dmstatus, DM_DMSTATUS_IMPEBREAK);
1674 if (!has_sufficient_progbuf(target, 2)) {
1675 LOG_WARNING("We won't be able to execute fence instructions on this "
1676 "target. Memory may not always appear consistent. "
1677 "(progbufsize=%d, impebreak=%d)", info->progbufsize,
1681 if (info->progbufsize < 4 && riscv_enable_virtual) {
1682 LOG_ERROR("set_enable_virtual is not available on this target. It "
1683 "requires a program buffer size of at least 4. (progbufsize=%d) "
1684 "Use `riscv set_enable_virtual off` to continue."
1685 , info->progbufsize);
1688 /* Before doing anything else we must first enumerate the harts. */
1689 if (dm->hart_count < 0) {
1690 for (int i = 0; i < MIN(RISCV_MAX_HARTS, 1 << info->hartsellen); ++i) {
1691 r->current_hartid = i;
1692 if (riscv013_select_current_hart(target) != ERROR_OK)
1696 if (dmstatus_read(target, &s, true) != ERROR_OK)
1698 if (get_field(s, DM_DMSTATUS_ANYNONEXISTENT))
1700 dm->hart_count = i + 1;
1702 if (get_field(s, DM_DMSTATUS_ANYHAVERESET))
1703 dmi_write(target, DM_DMCONTROL,
1704 set_hartsel(DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_ACKHAVERESET, i));
1707 LOG_DEBUG("Detected %d harts.", dm->hart_count);
1710 if (dm->hart_count == 0) {
1711 LOG_ERROR("No harts found!");
1715 /* Don't call any riscv_* functions until after we've counted the number of
1716 * cores and initialized registers. */
1717 for (int i = 0; i < dm->hart_count; ++i) {
1718 if (!riscv_rtos_enabled(target) && i != target->coreid)
1721 r->current_hartid = i;
1722 if (riscv013_select_current_hart(target) != ERROR_OK)
1725 bool halted = riscv_is_halted(target);
1727 if (riscv013_halt_go(target) != ERROR_OK) {
1728 LOG_ERROR("Fatal: Hart %d failed to halt during examine()", i);
1733 /* Without knowing anything else we can at least mess with the
1734 * program buffer. */
1735 r->debug_buffer_size[i] = info->progbufsize;
1737 int result = register_read_abstract(target, NULL, GDB_REGNO_S0, 64);
1738 if (result == ERROR_OK)
1743 if (register_read(target, &r->misa[i], GDB_REGNO_MISA)) {
1744 LOG_ERROR("Fatal: Failed to read MISA from hart %d.", i);
1748 if (riscv_supports_extension(target, i, 'V')) {
1749 if (discover_vlenb(target, i) != ERROR_OK)
1753 /* Now init registers based on what we discovered. */
1754 if (riscv_init_registers(target) != ERROR_OK)
1757 /* Display this as early as possible to help people who are using
1758 * really slow simulators. */
1759 LOG_DEBUG(" hart %d: XLEN=%d, misa=0x%" PRIx64, i, r->xlen[i],
1763 riscv013_step_or_resume_current_hart(target, false, false);
1766 target_set_examined(target);
1769 bool haltgroup_supported;
1770 if (set_haltgroup(target, &haltgroup_supported) != ERROR_OK)
1772 if (haltgroup_supported)
1773 LOG_INFO("Core %d made part of halt group %d.", target->coreid,
1776 LOG_INFO("Core %d could not be made part of halt group %d.",
1777 target->coreid, target->smp);
1780 /* Some regression suites rely on seeing 'Examined RISC-V core' to know
1781 * when they can connect with gdb/telnet.
1782 * We will need to update those suites if we want to change that text. */
1783 LOG_INFO("Examined RISC-V core; found %d harts",
1784 riscv_count_harts(target));
1785 for (int i = 0; i < riscv_count_harts(target); ++i) {
1786 if (riscv_hart_enabled(target, i)) {
1787 LOG_INFO(" hart %d: XLEN=%d, misa=0x%" PRIx64, i, r->xlen[i],
1790 LOG_INFO(" hart %d: currently disabled", i);
1796 int riscv013_authdata_read(struct target *target, uint32_t *value)
1798 if (wait_for_authbusy(target, NULL) != ERROR_OK)
1801 return dmi_read(target, value, DM_AUTHDATA);
1804 int riscv013_authdata_write(struct target *target, uint32_t value)
1806 uint32_t before, after;
1807 if (wait_for_authbusy(target, &before) != ERROR_OK)
1810 dmi_write(target, DM_AUTHDATA, value);
1812 if (wait_for_authbusy(target, &after) != ERROR_OK)
1815 if (!get_field(before, DM_DMSTATUS_AUTHENTICATED) &&
1816 get_field(after, DM_DMSTATUS_AUTHENTICATED)) {
1817 LOG_INFO("authdata_write resulted in successful authentication");
1818 int result = ERROR_OK;
1819 dm013_info_t *dm = get_dm(target);
1822 target_list_t *entry;
1823 list_for_each_entry(entry, &dm->target_list, list) {
1824 if (examine(entry->target) != ERROR_OK)
1825 result = ERROR_FAIL;
1833 static int riscv013_hart_count(struct target *target)
1835 dm013_info_t *dm = get_dm(target);
1837 return dm->hart_count;
1840 static unsigned riscv013_data_bits(struct target *target)
1842 RISCV013_INFO(info);
1843 /* TODO: Once there is a spec for discovering abstract commands, we can
1844 * take those into account as well. For now we assume abstract commands
1845 * support XLEN-wide accesses. */
1846 if (has_sufficient_progbuf(target, 3) && !riscv_prefer_sba)
1847 return riscv_xlen(target);
1849 if (get_field(info->sbcs, DM_SBCS_SBACCESS128))
1851 if (get_field(info->sbcs, DM_SBCS_SBACCESS64))
1853 if (get_field(info->sbcs, DM_SBCS_SBACCESS32))
1855 if (get_field(info->sbcs, DM_SBCS_SBACCESS16))
1857 if (get_field(info->sbcs, DM_SBCS_SBACCESS8))
1860 return riscv_xlen(target);
1863 static int prep_for_vector_access(struct target *target, uint64_t *vtype,
1864 uint64_t *vl, unsigned *debug_vl)
1867 /* TODO: this continuous save/restore is terrible for performance. */
1868 /* Write vtype and vl. */
1869 unsigned encoded_vsew;
1870 switch (riscv_xlen(target)) {
1878 LOG_ERROR("Unsupported xlen: %d", riscv_xlen(target));
1882 /* Save vtype and vl. */
1883 if (register_read(target, vtype, GDB_REGNO_VTYPE) != ERROR_OK)
1885 if (register_read(target, vl, GDB_REGNO_VL) != ERROR_OK)
1888 if (register_write_direct(target, GDB_REGNO_VTYPE, encoded_vsew << 3) != ERROR_OK)
1890 *debug_vl = DIV_ROUND_UP(r->vlenb[r->current_hartid] * 8,
1891 riscv_xlen(target));
1892 if (register_write_direct(target, GDB_REGNO_VL, *debug_vl) != ERROR_OK)
1898 static int cleanup_after_vector_access(struct target *target, uint64_t vtype,
1901 /* Restore vtype and vl. */
1902 if (register_write_direct(target, GDB_REGNO_VTYPE, vtype) != ERROR_OK)
1904 if (register_write_direct(target, GDB_REGNO_VL, vl) != ERROR_OK)
1909 static int riscv013_get_register_buf(struct target *target,
1910 uint8_t *value, int regno)
1912 assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
1915 if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
1919 if (prep_for_register_access(target, &mstatus, regno) != ERROR_OK)
1924 if (prep_for_vector_access(target, &vtype, &vl, &debug_vl) != ERROR_OK)
1927 unsigned vnum = regno - GDB_REGNO_V0;
1928 unsigned xlen = riscv_xlen(target);
1930 struct riscv_program program;
1931 riscv_program_init(&program, target);
1932 riscv_program_insert(&program, vmv_x_s(S0, vnum));
1933 riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
1935 int result = ERROR_OK;
1936 for (unsigned i = 0; i < debug_vl; i++) {
1937 /* Executing the program might result in an exception if there is some
1938 * issue with the vector implementation/instructions we're using. If that
1939 * happens, attempt to restore as usual. We may have clobbered the
1940 * vector register we tried to read already.
1941 * For other failures, we just return error because things are probably
1942 * so messed up that attempting to restore isn't going to help. */
1943 result = riscv_program_exec(&program, target);
1944 if (result == ERROR_OK) {
1946 if (register_read_direct(target, &v, GDB_REGNO_S0) != ERROR_OK)
1948 buf_set_u64(value, xlen * i, xlen, v);
1954 if (cleanup_after_vector_access(target, vtype, vl) != ERROR_OK)
1957 if (cleanup_after_register_access(target, mstatus, regno) != ERROR_OK)
1959 if (register_write_direct(target, GDB_REGNO_S0, s0) != ERROR_OK)
1965 static int riscv013_set_register_buf(struct target *target,
1966 int regno, const uint8_t *value)
1968 assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
1971 if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
1975 if (prep_for_register_access(target, &mstatus, regno) != ERROR_OK)
1980 if (prep_for_vector_access(target, &vtype, &vl, &debug_vl) != ERROR_OK)
1983 unsigned vnum = regno - GDB_REGNO_V0;
1984 unsigned xlen = riscv_xlen(target);
1986 struct riscv_program program;
1987 riscv_program_init(&program, target);
1988 riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
1989 int result = ERROR_OK;
1990 for (unsigned i = 0; i < debug_vl; i++) {
1991 if (register_write_direct(target, GDB_REGNO_S0,
1992 buf_get_u64(value, xlen * i, xlen)) != ERROR_OK)
1994 result = riscv_program_exec(&program, target);
1995 if (result != ERROR_OK)
1999 if (cleanup_after_vector_access(target, vtype, vl) != ERROR_OK)
2002 if (cleanup_after_register_access(target, mstatus, regno) != ERROR_OK)
2004 if (register_write_direct(target, GDB_REGNO_S0, s0) != ERROR_OK)
2010 static int init_target(struct command_context *cmd_ctx,
2011 struct target *target)
2014 riscv_info_t *generic_info = (riscv_info_t *) target->arch_info;
2016 generic_info->get_register = &riscv013_get_register;
2017 generic_info->set_register = &riscv013_set_register;
2018 generic_info->get_register_buf = &riscv013_get_register_buf;
2019 generic_info->set_register_buf = &riscv013_set_register_buf;
2020 generic_info->select_current_hart = &riscv013_select_current_hart;
2021 generic_info->is_halted = &riscv013_is_halted;
2022 generic_info->resume_go = &riscv013_resume_go;
2023 generic_info->step_current_hart = &riscv013_step_current_hart;
2024 generic_info->on_halt = &riscv013_on_halt;
2025 generic_info->resume_prep = &riscv013_resume_prep;
2026 generic_info->halt_prep = &riscv013_halt_prep;
2027 generic_info->halt_go = &riscv013_halt_go;
2028 generic_info->on_step = &riscv013_on_step;
2029 generic_info->halt_reason = &riscv013_halt_reason;
2030 generic_info->read_debug_buffer = &riscv013_read_debug_buffer;
2031 generic_info->write_debug_buffer = &riscv013_write_debug_buffer;
2032 generic_info->execute_debug_buffer = &riscv013_execute_debug_buffer;
2033 generic_info->fill_dmi_write_u64 = &riscv013_fill_dmi_write_u64;
2034 generic_info->fill_dmi_read_u64 = &riscv013_fill_dmi_read_u64;
2035 generic_info->fill_dmi_nop_u64 = &riscv013_fill_dmi_nop_u64;
2036 generic_info->dmi_write_u64_bits = &riscv013_dmi_write_u64_bits;
2037 generic_info->authdata_read = &riscv013_authdata_read;
2038 generic_info->authdata_write = &riscv013_authdata_write;
2039 generic_info->dmi_read = &dmi_read;
2040 generic_info->dmi_write = &dmi_write;
2041 generic_info->read_memory = read_memory;
2042 generic_info->test_sba_config_reg = &riscv013_test_sba_config_reg;
2043 generic_info->test_compliance = &riscv013_test_compliance;
2044 generic_info->hart_count = &riscv013_hart_count;
2045 generic_info->data_bits = &riscv013_data_bits;
2046 generic_info->version_specific = calloc(1, sizeof(riscv013_info_t));
2047 if (!generic_info->version_specific)
2049 riscv013_info_t *info = get_info(target);
2051 info->progbufsize = -1;
2053 info->dmi_busy_delay = 0;
2054 info->bus_master_read_delay = 0;
2055 info->bus_master_write_delay = 0;
2056 info->ac_busy_delay = 0;
2058 /* Assume all these abstract commands are supported until we learn
2060 * TODO: The spec allows eg. one CSR to be able to be accessed abstractly
2061 * while another one isn't. We don't track that this closely here, but in
2062 * the future we probably should. */
2063 info->abstract_read_csr_supported = true;
2064 info->abstract_write_csr_supported = true;
2065 info->abstract_read_fpr_supported = true;
2066 info->abstract_write_fpr_supported = true;
2071 static int assert_reset(struct target *target)
2077 uint32_t control_base = set_field(0, DM_DMCONTROL_DMACTIVE, 1);
2080 /* There's only one target, and OpenOCD thinks each hart is a thread.
2081 * We must reset them all. */
2083 /* TODO: Try to use hasel in dmcontrol */
2085 /* Set haltreq for each hart. */
2086 uint32_t control = control_base;
2087 for (int i = 0; i < riscv_count_harts(target); ++i) {
2088 if (!riscv_hart_enabled(target, i))
2091 control = set_hartsel(control_base, i);
2092 control = set_field(control, DM_DMCONTROL_HALTREQ,
2093 target->reset_halt ? 1 : 0);
2094 dmi_write(target, DM_DMCONTROL, control);
2096 /* Assert ndmreset */
2097 control = set_field(control, DM_DMCONTROL_NDMRESET, 1);
2098 dmi_write(target, DM_DMCONTROL, control);
2101 /* Reset just this hart. */
2102 uint32_t control = set_hartsel(control_base, r->current_hartid);
2103 control = set_field(control, DM_DMCONTROL_HALTREQ,
2104 target->reset_halt ? 1 : 0);
2105 control = set_field(control, DM_DMCONTROL_NDMRESET, 1);
2106 dmi_write(target, DM_DMCONTROL, control);
2109 target->state = TARGET_RESET;
2111 dm013_info_t *dm = get_dm(target);
2115 /* The DM might have gotten reset if OpenOCD called us in some reset that
2116 * involves SRST being toggled. So clear our cache which may be out of
2118 memset(dm->progbuf_cache, 0, sizeof(dm->progbuf_cache));
2123 static int deassert_reset(struct target *target)
2126 RISCV013_INFO(info);
2129 /* Clear the reset, but make sure haltreq is still set */
2130 uint32_t control = 0;
2131 control = set_field(control, DM_DMCONTROL_HALTREQ, target->reset_halt ? 1 : 0);
2132 control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
2133 dmi_write(target, DM_DMCONTROL,
2134 set_hartsel(control, r->current_hartid));
2137 int dmi_busy_delay = info->dmi_busy_delay;
2138 time_t start = time(NULL);
2140 for (int i = 0; i < riscv_count_harts(target); ++i) {
2143 if (!riscv_hart_enabled(target, index))
2145 dmi_write(target, DM_DMCONTROL,
2146 set_hartsel(control, index));
2148 index = r->current_hartid;
2152 uint32_t expected_field;
2153 if (target->reset_halt) {
2155 expected_field = DM_DMSTATUS_ALLHALTED;
2158 expected_field = DM_DMSTATUS_ALLRUNNING;
2160 LOG_DEBUG("Waiting for hart %d to %s out of reset.", index, operation);
2162 int result = dmstatus_read_timeout(target, &dmstatus, true,
2163 riscv_reset_timeout_sec);
2164 if (result == ERROR_TIMEOUT_REACHED)
2165 LOG_ERROR("Hart %d didn't complete a DMI read coming out of "
2166 "reset in %ds; Increase the timeout with riscv "
2167 "set_reset_timeout_sec.",
2168 index, riscv_reset_timeout_sec);
2169 if (result != ERROR_OK)
2171 if (get_field(dmstatus, expected_field))
2173 if (time(NULL) - start > riscv_reset_timeout_sec) {
2174 LOG_ERROR("Hart %d didn't %s coming out of reset in %ds; "
2176 "Increase the timeout with riscv set_reset_timeout_sec.",
2177 index, operation, riscv_reset_timeout_sec, dmstatus);
2181 target->state = TARGET_HALTED;
2183 if (get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET)) {
2185 dmi_write(target, DM_DMCONTROL,
2186 set_hartsel(control, index) |
2187 DM_DMCONTROL_ACKHAVERESET);
2193 info->dmi_busy_delay = dmi_busy_delay;
2197 static int execute_fence(struct target *target)
2199 int old_hartid = riscv_current_hartid(target);
2201 /* FIXME: For non-coherent systems we need to flush the caches right
2202 * here, but there's no ISA-defined way of doing that. */
2204 struct riscv_program program;
2205 riscv_program_init(&program, target);
2206 riscv_program_fence_i(&program);
2207 riscv_program_fence(&program);
2208 int result = riscv_program_exec(&program, target);
2209 if (result != ERROR_OK)
2210 LOG_DEBUG("Unable to execute pre-fence");
2213 for (int i = 0; i < riscv_count_harts(target); ++i) {
2214 if (!riscv_hart_enabled(target, i))
2217 if (i == old_hartid)
2218 /* Fence already executed for this hart */
2221 riscv_set_current_hartid(target, i);
2223 struct riscv_program program;
2224 riscv_program_init(&program, target);
2225 riscv_program_fence_i(&program);
2226 riscv_program_fence(&program);
2227 int result = riscv_program_exec(&program, target);
2228 if (result != ERROR_OK)
2229 LOG_DEBUG("Unable to execute fence on hart %d", i);
2232 riscv_set_current_hartid(target, old_hartid);
2237 static void log_memory_access(target_addr_t address, uint64_t value,
2238 unsigned size_bytes, bool read)
2240 if (debug_level < LOG_LVL_DEBUG)
2244 sprintf(fmt, "M[0x%" TARGET_PRIxADDR "] %ss 0x%%0%d" PRIx64,
2245 address, read ? "read" : "write", size_bytes * 2);
2246 switch (size_bytes) {
2254 value &= 0xffffffffUL;
2261 LOG_DEBUG(fmt, value);
2264 /* Read the relevant sbdata regs depending on size, and put the results into
2266 static int read_memory_bus_word(struct target *target, target_addr_t address,
2267 uint32_t size, uint8_t *buffer)
2271 static int sbdata[4] = { DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3 };
2273 for (int i = (size - 1) / 4; i >= 0; i--) {
2274 result = dmi_op(target, &value, NULL, DMI_OP_READ, sbdata[i], 0, false, true);
2275 if (result != ERROR_OK)
2277 buf_set_u32(buffer + i * 4, 0, 8 * MIN(size, 4), value);
2278 log_memory_access(address + i * 4, value, MIN(size, 4), true);
2283 static uint32_t sb_sbaccess(unsigned size_bytes)
2285 switch (size_bytes) {
2287 return set_field(0, DM_SBCS_SBACCESS, 0);
2289 return set_field(0, DM_SBCS_SBACCESS, 1);
2291 return set_field(0, DM_SBCS_SBACCESS, 2);
2293 return set_field(0, DM_SBCS_SBACCESS, 3);
2295 return set_field(0, DM_SBCS_SBACCESS, 4);
2298 return 0; /* Make mingw happy. */
2301 static target_addr_t sb_read_address(struct target *target)
2303 RISCV013_INFO(info);
2304 unsigned sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2305 target_addr_t address = 0;
2308 dmi_read(target, &v, DM_SBADDRESS1);
2312 dmi_read(target, &v, DM_SBADDRESS0);
2317 static int sb_write_address(struct target *target, target_addr_t address)
2319 RISCV013_INFO(info);
2320 unsigned sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2321 /* There currently is no support for >64-bit addresses in OpenOCD. */
2323 dmi_write(target, DM_SBADDRESS3, 0);
2325 dmi_write(target, DM_SBADDRESS2, 0);
2327 dmi_write(target, DM_SBADDRESS1, address >> 32);
2328 return dmi_write(target, DM_SBADDRESS0, address);
2331 static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
2333 time_t start = time(NULL);
2335 if (dmi_read(target, sbcs, DM_SBCS) != ERROR_OK)
2337 if (!get_field(*sbcs, DM_SBCS_SBBUSY))
2339 if (time(NULL) - start > riscv_command_timeout_sec) {
2340 LOG_ERROR("Timed out after %ds waiting for sbbusy to go low (sbcs=0x%x). "
2341 "Increase the timeout with riscv set_command_timeout_sec.",
2342 riscv_command_timeout_sec, *sbcs);
2348 static int modify_privilege(struct target *target, uint64_t *mstatus, uint64_t *mstatus_old)
2350 if (riscv_enable_virtual && has_sufficient_progbuf(target, 5)) {
2353 if (register_read(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
2356 /* Read and save MSTATUS */
2357 if (register_read(target, mstatus, GDB_REGNO_MSTATUS) != ERROR_OK)
2359 *mstatus_old = *mstatus;
2361 /* If we come from m-mode with mprv set, we want to keep mpp */
2362 if (get_field(dcsr, DCSR_PRV) < 3) {
2364 *mstatus = set_field(*mstatus, MSTATUS_MPP, get_field(dcsr, DCSR_PRV));
2367 *mstatus = set_field(*mstatus, MSTATUS_MPRV, 1);
2370 if (*mstatus != *mstatus_old)
2371 if (register_write_direct(target, GDB_REGNO_MSTATUS, *mstatus) != ERROR_OK)
2379 static int read_memory_bus_v0(struct target *target, target_addr_t address,
2380 uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
2382 if (size != increment) {
2383 LOG_ERROR("sba v0 reads only support size==increment");
2384 return ERROR_NOT_IMPLEMENTED;
2387 LOG_DEBUG("System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
2388 TARGET_PRIxADDR, size, count, address);
2389 uint8_t *t_buffer = buffer;
2390 riscv_addr_t cur_addr = address;
2391 riscv_addr_t fin_addr = address + (count * size);
2392 uint32_t access = 0;
2394 const int DM_SBCS_SBSINGLEREAD_OFFSET = 20;
2395 const uint32_t DM_SBCS_SBSINGLEREAD = (0x1U << DM_SBCS_SBSINGLEREAD_OFFSET);
2397 const int DM_SBCS_SBAUTOREAD_OFFSET = 15;
2398 const uint32_t DM_SBCS_SBAUTOREAD = (0x1U << DM_SBCS_SBAUTOREAD_OFFSET);
2400 /* ww favorise one off reading if there is an issue */
2402 for (uint32_t i = 0; i < count; i++) {
2403 if (dmi_read(target, &access, DM_SBCS) != ERROR_OK)
2405 dmi_write(target, DM_SBADDRESS0, cur_addr);
2406 /* size/2 matching the bit access of the spec 0.13 */
2407 access = set_field(access, DM_SBCS_SBACCESS, size/2);
2408 access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
2409 LOG_DEBUG("\r\nread_memory: sab: access: 0x%08x", access);
2410 dmi_write(target, DM_SBCS, access);
2413 if (dmi_read(target, &value, DM_SBDATA0) != ERROR_OK)
2415 LOG_DEBUG("\r\nread_memory: sab: value: 0x%08x", value);
2416 buf_set_u32(t_buffer, 0, 8 * size, value);
2423 /* has to be the same size if we want to read a block */
2424 LOG_DEBUG("reading block until final address 0x%" PRIx64, fin_addr);
2425 if (dmi_read(target, &access, DM_SBCS) != ERROR_OK)
2427 /* set current address */
2428 dmi_write(target, DM_SBADDRESS0, cur_addr);
2429 /* 2) write sbaccess=2, sbsingleread,sbautoread,sbautoincrement
2430 * size/2 matching the bit access of the spec 0.13 */
2431 access = set_field(access, DM_SBCS_SBACCESS, size/2);
2432 access = set_field(access, DM_SBCS_SBAUTOREAD, 1);
2433 access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
2434 access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
2435 LOG_DEBUG("\r\naccess: 0x%08x", access);
2436 dmi_write(target, DM_SBCS, access);
2438 while (cur_addr < fin_addr) {
2439 LOG_DEBUG("\r\nsab:autoincrement: \r\n size: %d\tcount:%d\taddress: 0x%08"
2440 PRIx64, size, count, cur_addr);
2443 if (dmi_read(target, &value, DM_SBDATA0) != ERROR_OK)
2445 buf_set_u32(t_buffer, 0, 8 * size, value);
2449 /* if we are reaching last address, we must clear autoread */
2450 if (cur_addr == fin_addr && count != 1) {
2451 dmi_write(target, DM_SBCS, 0);
2452 if (dmi_read(target, &value, DM_SBDATA0) != ERROR_OK)
2454 buf_set_u32(t_buffer, 0, 8 * size, value);
2462 * Read the requested memory using the system bus interface.
2464 static int read_memory_bus_v1(struct target *target, target_addr_t address,
2465 uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
2467 if (increment != size && increment != 0) {
2468 LOG_ERROR("sba v1 reads only support increment of size or 0");
2469 return ERROR_NOT_IMPLEMENTED;
2472 RISCV013_INFO(info);
2473 target_addr_t next_address = address;
2474 target_addr_t end_address = address + count * size;
2476 while (next_address < end_address) {
2477 uint32_t sbcs_write = set_field(0, DM_SBCS_SBREADONADDR, 1);
2478 sbcs_write |= sb_sbaccess(size);
2479 if (increment == size)
2480 sbcs_write = set_field(sbcs_write, DM_SBCS_SBAUTOINCREMENT, 1);
2482 sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, count > 1);
2483 if (dmi_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
2486 /* This address write will trigger the first read. */
2487 if (sb_write_address(target, next_address) != ERROR_OK)
2490 if (info->bus_master_read_delay) {
2491 jtag_add_runtest(info->bus_master_read_delay, TAP_IDLE);
2492 if (jtag_execute_queue() != ERROR_OK) {
2493 LOG_ERROR("Failed to scan idle sequence");
2498 /* First value has been read, and is waiting for us to issue a DMI read
2501 static int sbdata[4] = {DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3};
2503 target_addr_t next_read = address - 1;
2504 for (uint32_t i = (next_address - address) / size; i < count - 1; i++) {
2505 for (int j = (size - 1) / 4; j >= 0; j--) {
2507 unsigned attempt = 0;
2509 if (attempt++ > 100) {
2510 LOG_ERROR("DMI keeps being busy in while reading memory just past " TARGET_ADDR_FMT,
2514 dmi_status_t status = dmi_scan(target, NULL, &value,
2515 DMI_OP_READ, sbdata[j], 0, false);
2516 if (status == DMI_STATUS_BUSY)
2517 increase_dmi_busy_delay(target);
2518 else if (status == DMI_STATUS_SUCCESS)
2523 if (next_read != address - 1) {
2524 buf_set_u32(buffer + next_read - address, 0, 8 * MIN(size, 4), value);
2525 log_memory_access(next_read, value, MIN(size, 4), true);
2527 next_read = address + i * size + j * 4;
2531 uint32_t sbcs_read = 0;
2534 unsigned attempt = 0;
2536 if (attempt++ > 100) {
2537 LOG_ERROR("DMI keeps being busy in while reading memory just past " TARGET_ADDR_FMT,
2541 dmi_status_t status = dmi_scan(target, NULL, &value, DMI_OP_NOP, 0, 0, false);
2542 if (status == DMI_STATUS_BUSY)
2543 increase_dmi_busy_delay(target);
2544 else if (status == DMI_STATUS_SUCCESS)
2549 buf_set_u32(buffer + next_read - address, 0, 8 * MIN(size, 4), value);
2550 log_memory_access(next_read, value, MIN(size, 4), true);
2552 /* "Writes to sbcs while sbbusy is high result in undefined behavior.
2553 * A debugger must not write to sbcs until it reads sbbusy as 0." */
2554 if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
2557 sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, 0);
2558 if (dmi_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
2562 /* Read the last word, after we disabled sbreadondata if necessary. */
2563 if (!get_field(sbcs_read, DM_SBCS_SBERROR) &&
2564 !get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
2565 if (read_memory_bus_word(target, address + (count - 1) * size, size,
2566 buffer + (count - 1) * size) != ERROR_OK)
2569 if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
2573 if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
2574 /* We read while the target was busy. Slow down and try again. */
2575 if (dmi_write(target, DM_SBCS, DM_SBCS_SBBUSYERROR) != ERROR_OK)
2577 next_address = sb_read_address(target);
2578 info->bus_master_read_delay += info->bus_master_read_delay / 10 + 1;
2582 unsigned error = get_field(sbcs_read, DM_SBCS_SBERROR);
2584 next_address = end_address;
2586 /* Some error indicating the bus access failed, but not because of
2587 * something we did wrong. */
2588 if (dmi_write(target, DM_SBCS, DM_SBCS_SBERROR) != ERROR_OK)
2597 static int batch_run(const struct target *target, struct riscv_batch *batch)
2599 RISCV013_INFO(info);
2601 if (r->reset_delays_wait >= 0) {
2602 r->reset_delays_wait -= batch->used_scans;
2603 if (r->reset_delays_wait <= 0) {
2604 batch->idle_count = 0;
2605 info->dmi_busy_delay = 0;
2606 info->ac_busy_delay = 0;
2609 return riscv_batch_run(batch);
2613 * Performs a memory read using memory access abstract commands. The read sizes
2614 * supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16 byte
2615 * aamsize fields in the memory access abstract command.
2617 static int read_memory_abstract(struct target *target, target_addr_t address,
2618 uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
2620 if (size != increment) {
2621 LOG_ERROR("abstract command reads only support size==increment");
2622 return ERROR_NOT_IMPLEMENTED;
2625 int result = ERROR_OK;
2627 LOG_DEBUG("reading %d words of %d bytes from 0x%" TARGET_PRIxADDR, count,
2630 memset(buffer, 0, count * size);
2632 /* Convert the size (bytes) to width (bits) */
2633 unsigned width = size << 3;
2635 /* TODO: Add 128b support if it's ever used. Involves modifying
2636 read/write_abstract_arg() to work on two 64b values. */
2637 LOG_ERROR("Unsupported size: %d bits", size);
2641 /* Create the command (physical address, postincrement, read) */
2642 uint32_t command = access_memory_command(target, false, width, true, false);
2644 /* Execute the reads */
2645 uint8_t *p = buffer;
2646 bool updateaddr = true;
2647 unsigned width32 = (width + 31) / 32 * 32;
2648 for (uint32_t c = 0; c < count; c++) {
2649 /* Only update the address initially and let postincrement update it */
2651 /* Set arg1 to the address: address + c * size */
2652 result = write_abstract_arg(target, 1, address, riscv_xlen(target));
2653 if (result != ERROR_OK) {
2654 LOG_ERROR("Failed to write arg1 during read_memory_abstract().");
2659 /* Execute the command */
2660 result = execute_abstract_command(target, command);
2661 if (result != ERROR_OK) {
2662 LOG_ERROR("Failed to execute command read_memory_abstract().");
2666 /* Copy arg0 to buffer (rounded width up to nearest 32) */
2667 riscv_reg_t value = read_abstract_arg(target, 0, width32);
2668 buf_set_u64(p, 0, 8 * size, value);
2678 * Performs a memory write using memory access abstract commands. The write
2679 * sizes supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16
2680 * byte aamsize fields in the memory access abstract command.
2682 static int write_memory_abstract(struct target *target, target_addr_t address,
2683 uint32_t size, uint32_t count, const uint8_t *buffer)
2685 int result = ERROR_OK;
2687 LOG_DEBUG("writing %d words of %d bytes from 0x%" TARGET_PRIxADDR, count,
2690 /* Convert the size (bytes) to width (bits) */
2691 unsigned width = size << 3;
2693 /* TODO: Add 128b support if it's ever used. Involves modifying
2694 read/write_abstract_arg() to work on two 64b values. */
2695 LOG_ERROR("Unsupported size: %d bits", width);
2699 /* Create the command (physical address, postincrement, write) */
2700 uint32_t command = access_memory_command(target, false, width, true, true);
2702 /* Execute the writes */
2703 const uint8_t *p = buffer;
2704 bool updateaddr = true;
2705 for (uint32_t c = 0; c < count; c++) {
2706 /* Move data to arg0 */
2707 riscv_reg_t value = buf_get_u64(p, 0, 8 * size);
2708 result = write_abstract_arg(target, 0, value, riscv_xlen(target));
2709 if (result != ERROR_OK) {
2710 LOG_ERROR("Failed to write arg0 during write_memory_abstract().");
2714 /* Only update the address initially and let postincrement update it */
2716 /* Set arg1 to the address: address + c * size */
2717 result = write_abstract_arg(target, 1, address, riscv_xlen(target));
2718 if (result != ERROR_OK) {
2719 LOG_ERROR("Failed to write arg1 during write_memory_abstract().");
2724 /* Execute the command */
2725 result = execute_abstract_command(target, command);
2726 if (result != ERROR_OK) {
2727 LOG_ERROR("Failed to execute command write_memory_abstract().");
2739 * Read the requested memory, taking care to execute every read exactly once,
2740 * even if cmderr=busy is encountered.
2742 static int read_memory_progbuf_inner(struct target *target, target_addr_t address,
2743 uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
2745 RISCV013_INFO(info);
2747 int result = ERROR_OK;
2749 /* Write address to S0. */
2750 result = register_write_direct(target, GDB_REGNO_S0, address);
2751 if (result != ERROR_OK)
2754 if (increment == 0 &&
2755 register_write_direct(target, GDB_REGNO_S2, 0) != ERROR_OK)
2758 uint32_t command = access_register_command(target, GDB_REGNO_S1,
2760 AC_ACCESS_REGISTER_TRANSFER | AC_ACCESS_REGISTER_POSTEXEC);
2761 if (execute_abstract_command(target, command) != ERROR_OK)
2764 /* First read has just triggered. Result is in s1. */
2767 if (register_read_direct(target, &value, GDB_REGNO_S1) != ERROR_OK)
2769 buf_set_u64(buffer, 0, 8 * size, value);
2770 log_memory_access(address, value, size, true);
2774 if (dmi_write(target, DM_ABSTRACTAUTO,
2775 1 << DM_ABSTRACTAUTO_AUTOEXECDATA_OFFSET) != ERROR_OK)
2777 /* Read garbage from dmi_data0, which triggers another execution of the
2778 * program. Now dmi_data0 contains the first good result, and s1 the next
2780 if (dmi_read_exec(target, NULL, DM_DATA0) != ERROR_OK)
2783 /* read_addr is the next address that the hart will read from, which is the
2786 while (index < count) {
2787 riscv_addr_t read_addr = address + index * increment;
2788 LOG_DEBUG("i=%d, count=%d, read_addr=0x%" PRIx64, index, count, read_addr);
2789 /* The pipeline looks like this:
2790 * memory -> s1 -> dm_data0 -> debugger
2792 * s0 contains read_addr
2793 * s1 contains mem[read_addr-size]
2794 * dm_data0 contains[read_addr-size*2]
2797 struct riscv_batch *batch = riscv_batch_alloc(target, 32,
2798 info->dmi_busy_delay + info->ac_busy_delay);
2803 for (unsigned j = index; j < count; j++) {
2805 riscv_batch_add_dmi_read(batch, DM_DATA1);
2806 riscv_batch_add_dmi_read(batch, DM_DATA0);
2809 if (riscv_batch_full(batch))
2813 batch_run(target, batch);
2815 /* Wait for the target to finish performing the last abstract command,
2816 * and update our copy of cmderr. If we see that DMI is busy here,
2817 * dmi_busy_delay will be incremented. */
2818 uint32_t abstractcs;
2819 if (dmi_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
2821 while (get_field(abstractcs, DM_ABSTRACTCS_BUSY))
2822 if (dmi_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
2824 info->cmderr = get_field(abstractcs, DM_ABSTRACTCS_CMDERR);
2826 unsigned next_index;
2827 unsigned ignore_last = 0;
2828 switch (info->cmderr) {
2830 LOG_DEBUG("successful (partial?) memory read");
2831 next_index = index + reads;
2834 LOG_DEBUG("memory read resulted in busy response");
2836 increase_ac_busy_delay(target);
2837 riscv013_clear_abstract_error(target);
2839 dmi_write(target, DM_ABSTRACTAUTO, 0);
2841 uint32_t dmi_data0, dmi_data1 = 0;
2842 /* This is definitely a good version of the value that we
2843 * attempted to read when we discovered that the target was
2845 if (dmi_read(target, &dmi_data0, DM_DATA0) != ERROR_OK) {
2846 riscv_batch_free(batch);
2849 if (size > 4 && dmi_read(target, &dmi_data1, DM_DATA1) != ERROR_OK) {
2850 riscv_batch_free(batch);
2854 /* See how far we got, clobbering dmi_data0. */
2855 if (increment == 0) {
2857 result = register_read_direct(target, &counter, GDB_REGNO_S2);
2858 next_index = counter;
2860 uint64_t next_read_addr;
2861 result = register_read_direct(target, &next_read_addr,
2863 next_index = (next_read_addr - address) / increment;
2865 if (result != ERROR_OK) {
2866 riscv_batch_free(batch);
2870 uint64_t value64 = (((uint64_t)dmi_data1) << 32) | dmi_data0;
2871 buf_set_u64(buffer + (next_index - 2) * size, 0, 8 * size, value64);
2872 log_memory_access(address + (next_index - 2) * size, value64, size, true);
2874 /* Restore the command, and execute it.
2875 * Now DM_DATA0 contains the next value just as it would if no
2876 * error had occurred. */
2877 dmi_write_exec(target, DM_COMMAND, command, true);
2880 dmi_write(target, DM_ABSTRACTAUTO,
2881 1 << DM_ABSTRACTAUTO_AUTOEXECDATA_OFFSET);
2887 LOG_DEBUG("error when reading memory, abstractcs=0x%08lx", (long)abstractcs);
2888 riscv013_clear_abstract_error(target);
2889 riscv_batch_free(batch);
2890 result = ERROR_FAIL;
2894 /* Now read whatever we got out of the batch. */
2895 dmi_status_t status = DMI_STATUS_SUCCESS;
2898 for (unsigned j = index - 2; j < index + reads; j++) {
2900 LOG_DEBUG("index=%d, reads=%d, next_index=%d, ignore_last=%d, j=%d",
2901 index, reads, next_index, ignore_last, j);
2902 if (j + 3 + ignore_last > next_index)
2905 status = riscv_batch_get_dmi_read_op(batch, read);
2906 uint64_t value = riscv_batch_get_dmi_read_data(batch, read);
2908 if (status != DMI_STATUS_SUCCESS) {
2909 /* If we're here because of busy count, dmi_busy_delay will
2910 * already have been increased and busy state will have been
2911 * cleared in dmi_read(). */
2912 /* In at least some implementations, we issue a read, and then
2913 * can get busy back when we try to scan out the read result,
2914 * and the actual read value is lost forever. Since this is
2915 * rare in any case, we return error here and rely on our
2916 * caller to reread the entire block. */
2917 LOG_WARNING("Batch memory read encountered DMI error %d. "
2918 "Falling back on slower reads.", status);
2919 riscv_batch_free(batch);
2920 result = ERROR_FAIL;
2924 status = riscv_batch_get_dmi_read_op(batch, read);
2925 if (status != DMI_STATUS_SUCCESS) {
2926 LOG_WARNING("Batch memory read encountered DMI error %d. "
2927 "Falling back on slower reads.", status);
2928 riscv_batch_free(batch);
2929 result = ERROR_FAIL;
2933 value |= riscv_batch_get_dmi_read_data(batch, read);
2936 riscv_addr_t offset = j * size;
2937 buf_set_u64(buffer + offset, 0, 8 * size, value);
2938 log_memory_access(address + j * increment, value, size, true);
2943 riscv_batch_free(batch);
2946 dmi_write(target, DM_ABSTRACTAUTO, 0);
2949 /* Read the penultimate word. */
2950 uint32_t dmi_data0, dmi_data1 = 0;
2951 if (dmi_read(target, &dmi_data0, DM_DATA0) != ERROR_OK)
2953 if (size > 4 && dmi_read(target, &dmi_data1, DM_DATA1) != ERROR_OK)
2955 uint64_t value64 = (((uint64_t)dmi_data1) << 32) | dmi_data0;
2956 buf_set_u64(buffer + size * (count - 2), 0, 8 * size, value64);
2957 log_memory_access(address + size * (count - 2), value64, size, true);
2960 /* Read the last word. */
2962 result = register_read_direct(target, &value, GDB_REGNO_S1);
2963 if (result != ERROR_OK)
2965 buf_set_u64(buffer + size * (count-1), 0, 8 * size, value);
2966 log_memory_access(address + size * (count-1), value, size, true);
2971 dmi_write(target, DM_ABSTRACTAUTO, 0);
2976 /* Only need to save/restore one GPR to read a single word, and the progbuf
2977 * program doesn't need to increment. */
2978 static int read_memory_progbuf_one(struct target *target, target_addr_t address,
2979 uint32_t size, uint8_t *buffer)
2981 uint64_t mstatus = 0;
2982 uint64_t mstatus_old = 0;
2983 if (modify_privilege(target, &mstatus, &mstatus_old) != ERROR_OK)
2988 if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
2991 /* Write the program (load, increment) */
2992 struct riscv_program program;
2993 riscv_program_init(&program, target);
2994 if (riscv_enable_virtual && has_sufficient_progbuf(target, 5) && get_field(mstatus, MSTATUS_MPRV))
2995 riscv_program_csrrsi(&program, GDB_REGNO_ZERO, CSR_DCSR_MPRVEN, GDB_REGNO_DCSR);
2998 riscv_program_lbr(&program, GDB_REGNO_S0, GDB_REGNO_S0, 0);
3001 riscv_program_lhr(&program, GDB_REGNO_S0, GDB_REGNO_S0, 0);
3004 riscv_program_lwr(&program, GDB_REGNO_S0, GDB_REGNO_S0, 0);
3007 riscv_program_ldr(&program, GDB_REGNO_S0, GDB_REGNO_S0, 0);
3010 LOG_ERROR("Unsupported size: %d", size);
3013 if (riscv_enable_virtual && has_sufficient_progbuf(target, 5) && get_field(mstatus, MSTATUS_MPRV))
3014 riscv_program_csrrci(&program, GDB_REGNO_ZERO, CSR_DCSR_MPRVEN, GDB_REGNO_DCSR);
3016 if (riscv_program_ebreak(&program) != ERROR_OK)
3018 if (riscv_program_write(&program) != ERROR_OK)
3021 /* Write address to S0, and execute buffer. */
3022 if (write_abstract_arg(target, 0, address, riscv_xlen(target)) != ERROR_OK)
3024 uint32_t command = access_register_command(target, GDB_REGNO_S0,
3025 riscv_xlen(target), AC_ACCESS_REGISTER_WRITE |
3026 AC_ACCESS_REGISTER_TRANSFER | AC_ACCESS_REGISTER_POSTEXEC);
3027 if (execute_abstract_command(target, command) != ERROR_OK)
3031 if (register_read(target, &value, GDB_REGNO_S0) != ERROR_OK)
3033 buf_set_u64(buffer, 0, 8 * size, value);
3034 log_memory_access(address, value, size, true);
3036 if (riscv_set_register(target, GDB_REGNO_S0, s0) != ERROR_OK)
3039 /* Restore MSTATUS */
3040 if (mstatus != mstatus_old)
3041 if (register_write_direct(target, GDB_REGNO_MSTATUS, mstatus_old))
3048 * Read the requested memory, silently handling memory access errors.
3050 static int read_memory_progbuf(struct target *target, target_addr_t address,
3051 uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
3053 if (riscv_xlen(target) < size * 8) {
3054 LOG_ERROR("XLEN (%d) is too short for %d-bit memory read.",
3055 riscv_xlen(target), size * 8);
3059 int result = ERROR_OK;
3061 LOG_DEBUG("reading %d words of %d bytes from 0x%" TARGET_PRIxADDR, count,
3066 memset(buffer, 0, count*size);
3068 if (execute_fence(target) != ERROR_OK)
3072 return read_memory_progbuf_one(target, address, size, buffer);
3074 uint64_t mstatus = 0;
3075 uint64_t mstatus_old = 0;
3076 if (modify_privilege(target, &mstatus, &mstatus_old) != ERROR_OK)
3079 /* s0 holds the next address to write to
3080 * s1 holds the next data value to write
3081 * s2 is a counter in case increment is 0
3083 uint64_t s0, s1, s2;
3084 if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
3086 if (register_read(target, &s1, GDB_REGNO_S1) != ERROR_OK)
3088 if (increment == 0 && register_read(target, &s2, GDB_REGNO_S1) != ERROR_OK)
3091 /* Write the program (load, increment) */
3092 struct riscv_program program;
3093 riscv_program_init(&program, target);
3094 if (riscv_enable_virtual && has_sufficient_progbuf(target, 5) && get_field(mstatus, MSTATUS_MPRV))
3095 riscv_program_csrrsi(&program, GDB_REGNO_ZERO, CSR_DCSR_MPRVEN, GDB_REGNO_DCSR);
3099 riscv_program_lbr(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0);
3102 riscv_program_lhr(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0);
3105 riscv_program_lwr(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0);
3108 riscv_program_ldr(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0);
3111 LOG_ERROR("Unsupported size: %d", size);
3115 if (riscv_enable_virtual && has_sufficient_progbuf(target, 5) && get_field(mstatus, MSTATUS_MPRV))
3116 riscv_program_csrrci(&program, GDB_REGNO_ZERO, CSR_DCSR_MPRVEN, GDB_REGNO_DCSR);
3118 riscv_program_addi(&program, GDB_REGNO_S2, GDB_REGNO_S2, 1);
3120 riscv_program_addi(&program, GDB_REGNO_S0, GDB_REGNO_S0, increment);
3122 if (riscv_program_ebreak(&program) != ERROR_OK)
3124 if (riscv_program_write(&program) != ERROR_OK)
3127 result = read_memory_progbuf_inner(target, address, size, count, buffer, increment);
3129 if (result != ERROR_OK) {
3130 /* The full read did not succeed, so we will try to read each word individually. */
3131 /* This will not be fast, but reading outside actual memory is a special case anyway. */
3132 /* It will make the toolchain happier, especially Eclipse Memory View as it reads ahead. */
3133 target_addr_t address_i = address;
3134 uint32_t count_i = 1;
3135 uint8_t *buffer_i = buffer;
3137 for (uint32_t i = 0; i < count; i++, address_i += increment, buffer_i += size) {
3139 /* TODO: This is much slower than it needs to be because we end up
3140 * writing the address to read for every word we read. */
3141 result = read_memory_progbuf_inner(target, address_i, size, count_i, buffer_i, increment);
3143 /* The read of a single word failed, so we will just return 0 for that instead */
3144 if (result != ERROR_OK) {
3145 LOG_DEBUG("error reading single word of %d bytes from 0x%" TARGET_PRIxADDR,
3148 buf_set_u64(buffer_i, 0, 8 * size, 0);
3154 riscv_set_register(target, GDB_REGNO_S0, s0);
3155 riscv_set_register(target, GDB_REGNO_S1, s1);
3157 riscv_set_register(target, GDB_REGNO_S2, s2);
3159 /* Restore MSTATUS */
3160 if (mstatus != mstatus_old)
3161 if (register_write_direct(target, GDB_REGNO_MSTATUS, mstatus_old))
3167 static int read_memory(struct target *target, target_addr_t address,
3168 uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
3173 RISCV013_INFO(info);
3174 if (has_sufficient_progbuf(target, 3) && !riscv_prefer_sba)
3175 return read_memory_progbuf(target, address, size, count, buffer,
3178 if ((get_field(info->sbcs, DM_SBCS_SBACCESS8) && size == 1) ||
3179 (get_field(info->sbcs, DM_SBCS_SBACCESS16) && size == 2) ||
3180 (get_field(info->sbcs, DM_SBCS_SBACCESS32) && size == 4) ||
3181 (get_field(info->sbcs, DM_SBCS_SBACCESS64) && size == 8) ||
3182 (get_field(info->sbcs, DM_SBCS_SBACCESS128) && size == 16)) {
3183 if (get_field(info->sbcs, DM_SBCS_SBVERSION) == 0)
3184 return read_memory_bus_v0(target, address, size, count, buffer,
3186 else if (get_field(info->sbcs, DM_SBCS_SBVERSION) == 1)
3187 return read_memory_bus_v1(target, address, size, count, buffer,
3191 if (has_sufficient_progbuf(target, 3))
3192 return read_memory_progbuf(target, address, size, count, buffer,
3195 return read_memory_abstract(target, address, size, count, buffer,
3199 static int write_memory_bus_v0(struct target *target, target_addr_t address,
3200 uint32_t size, uint32_t count, const uint8_t *buffer)
3202 /*1) write sbaddress: for singlewrite and autoincrement, we need to write the address once*/
3203 LOG_DEBUG("System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
3204 TARGET_PRIxADDR, size, count, address);
3205 dmi_write(target, DM_SBADDRESS0, address);
3208 riscv_addr_t offset = 0;
3209 riscv_addr_t t_addr = 0;
3210 const uint8_t *t_buffer = buffer + offset;
3212 /* B.8 Writing Memory, single write check if we write in one go */
3213 if (count == 1) { /* count is in bytes here */
3214 value = buf_get_u64(t_buffer, 0, 8 * size);
3217 access = set_field(access, DM_SBCS_SBACCESS, size/2);
3218 dmi_write(target, DM_SBCS, access);
3219 LOG_DEBUG("\r\naccess: 0x%08" PRIx64, access);
3220 LOG_DEBUG("\r\nwrite_memory:SAB: ONE OFF: value 0x%08" PRIx64, value);
3221 dmi_write(target, DM_SBDATA0, value);
3225 /*B.8 Writing Memory, using autoincrement*/
3228 access = set_field(access, DM_SBCS_SBACCESS, size/2);
3229 access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
3230 LOG_DEBUG("\r\naccess: 0x%08" PRIx64, access);
3231 dmi_write(target, DM_SBCS, access);
3233 /*2)set the value according to the size required and write*/
3234 for (riscv_addr_t i = 0; i < count; ++i) {
3236 /* for monitoring only */
3237 t_addr = address + offset;
3238 t_buffer = buffer + offset;
3240 value = buf_get_u64(t_buffer, 0, 8 * size);
3241 LOG_DEBUG("SAB:autoincrement: expected address: 0x%08x value: 0x%08x"
3242 PRIx64, (uint32_t)t_addr, (uint32_t)value);
3243 dmi_write(target, DM_SBDATA0, value);
3245 /*reset the autoincrement when finished (something weird is happening if this is not done at the end*/
3246 access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 0);
3247 dmi_write(target, DM_SBCS, access);
3252 static int write_memory_bus_v1(struct target *target, target_addr_t address,
3253 uint32_t size, uint32_t count, const uint8_t *buffer)
3255 RISCV013_INFO(info);
3256 uint32_t sbcs = sb_sbaccess(size);
3257 sbcs = set_field(sbcs, DM_SBCS_SBAUTOINCREMENT, 1);
3258 dmi_write(target, DM_SBCS, sbcs);
3260 target_addr_t next_address = address;
3261 target_addr_t end_address = address + count * size;
3265 sb_write_address(target, next_address);
3266 while (next_address < end_address) {
3267 LOG_DEBUG("transferring burst starting at address 0x%" TARGET_PRIxADDR,
3270 struct riscv_batch *batch = riscv_batch_alloc(
3273 info->dmi_busy_delay + info->bus_master_write_delay);
3277 for (uint32_t i = (next_address - address) / size; i < count; i++) {
3278 const uint8_t *p = buffer + i * size;
3280 if (riscv_batch_available_scans(batch) < (size + 3) / 4)
3284 riscv_batch_add_dmi_write(batch, DM_SBDATA3,
3285 ((uint32_t) p[12]) |
3286 (((uint32_t) p[13]) << 8) |
3287 (((uint32_t) p[14]) << 16) |
3288 (((uint32_t) p[15]) << 24));
3291 riscv_batch_add_dmi_write(batch, DM_SBDATA2,
3293 (((uint32_t) p[9]) << 8) |
3294 (((uint32_t) p[10]) << 16) |
3295 (((uint32_t) p[11]) << 24));
3297 riscv_batch_add_dmi_write(batch, DM_SBDATA1,
3299 (((uint32_t) p[5]) << 8) |
3300 (((uint32_t) p[6]) << 16) |
3301 (((uint32_t) p[7]) << 24));
3302 uint32_t value = p[0];
3304 value |= ((uint32_t) p[2]) << 16;
3305 value |= ((uint32_t) p[3]) << 24;
3308 value |= ((uint32_t) p[1]) << 8;
3309 riscv_batch_add_dmi_write(batch, DM_SBDATA0, value);
3311 log_memory_access(address + i * size, value, size, false);
3312 next_address += size;
3315 result = batch_run(target, batch);
3316 riscv_batch_free(batch);
3317 if (result != ERROR_OK)
3320 bool dmi_busy_encountered;
3321 if (dmi_op(target, &sbcs, &dmi_busy_encountered, DMI_OP_READ,
3322 DM_SBCS, 0, false, false) != ERROR_OK)
3325 time_t start = time(NULL);
3326 bool dmi_busy = dmi_busy_encountered;
3327 while (get_field(sbcs, DM_SBCS_SBBUSY) || dmi_busy) {
3328 if (time(NULL) - start > riscv_command_timeout_sec) {
3329 LOG_ERROR("Timed out after %ds waiting for sbbusy to go low (sbcs=0x%x). "
3330 "Increase the timeout with riscv set_command_timeout_sec.",
3331 riscv_command_timeout_sec, sbcs);
3335 if (dmi_op(target, &sbcs, &dmi_busy, DMI_OP_READ,
3336 DM_SBCS, 0, false, true) != ERROR_OK)
3340 if (get_field(sbcs, DM_SBCS_SBBUSYERROR)) {
3341 /* We wrote while the target was busy. Slow down and try again. */
3342 dmi_write(target, DM_SBCS, DM_SBCS_SBBUSYERROR);
3343 info->bus_master_write_delay += info->bus_master_write_delay / 10 + 1;
3346 if (get_field(sbcs, DM_SBCS_SBBUSYERROR) || dmi_busy_encountered) {
3347 next_address = sb_read_address(target);
3348 if (next_address < address) {
3349 /* This should never happen, probably buggy hardware. */
3350 LOG_DEBUG("unexpected system bus address 0x%" TARGET_PRIxADDR,
3358 unsigned error = get_field(sbcs, DM_SBCS_SBERROR);
3360 /* Some error indicating the bus access failed, but not because of
3361 * something we did wrong. */
3362 dmi_write(target, DM_SBCS, DM_SBCS_SBERROR);
3370 static int write_memory_progbuf(struct target *target, target_addr_t address,
3371 uint32_t size, uint32_t count, const uint8_t *buffer)
3373 RISCV013_INFO(info);
3375 if (riscv_xlen(target) < size * 8) {
3376 LOG_ERROR("XLEN (%d) is too short for %d-bit memory write.",
3377 riscv_xlen(target), size * 8);
3381 LOG_DEBUG("writing %d words of %d bytes to 0x%08lx", count, size, (long)address);
3385 uint64_t mstatus = 0;
3386 uint64_t mstatus_old = 0;
3387 if (modify_privilege(target, &mstatus, &mstatus_old) != ERROR_OK)
3390 /* s0 holds the next address to write to
3391 * s1 holds the next data value to write
3394 int result = ERROR_OK;
3396 if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
3398 if (register_read(target, &s1, GDB_REGNO_S1) != ERROR_OK)
3401 /* Write the program (store, increment) */
3402 struct riscv_program program;
3403 riscv_program_init(&program, target);
3404 if (riscv_enable_virtual && has_sufficient_progbuf(target, 5) && get_field(mstatus, MSTATUS_MPRV))
3405 riscv_program_csrrsi(&program, GDB_REGNO_ZERO, CSR_DCSR_MPRVEN, GDB_REGNO_DCSR);
3409 riscv_program_sbr(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0);
3412 riscv_program_shr(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0);
3415 riscv_program_swr(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0);
3418 riscv_program_sdr(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0);
3421 LOG_ERROR("write_memory_progbuf(): Unsupported size: %d", size);
3422 result = ERROR_FAIL;
3426 if (riscv_enable_virtual && has_sufficient_progbuf(target, 5) && get_field(mstatus, MSTATUS_MPRV))
3427 riscv_program_csrrci(&program, GDB_REGNO_ZERO, CSR_DCSR_MPRVEN, GDB_REGNO_DCSR);
3428 riscv_program_addi(&program, GDB_REGNO_S0, GDB_REGNO_S0, size);
3430 result = riscv_program_ebreak(&program);
3431 if (result != ERROR_OK)
3433 riscv_program_write(&program);
3435 riscv_addr_t cur_addr = address;
3436 riscv_addr_t fin_addr = address + (count * size);
3437 bool setup_needed = true;
3438 LOG_DEBUG("writing until final address 0x%016" PRIx64, fin_addr);
3439 while (cur_addr < fin_addr) {
3440 LOG_DEBUG("transferring burst starting at address 0x%016" PRIx64,
3443 struct riscv_batch *batch = riscv_batch_alloc(
3446 info->dmi_busy_delay + info->ac_busy_delay);
3450 /* To write another word, we put it in S1 and execute the program. */
3451 unsigned start = (cur_addr - address) / size;
3452 for (unsigned i = start; i < count; ++i) {
3453 unsigned offset = size*i;
3454 const uint8_t *t_buffer = buffer + offset;
3456 uint64_t value = buf_get_u64(t_buffer, 0, 8 * size);
3458 log_memory_access(address + offset, value, size, false);
3462 result = register_write_direct(target, GDB_REGNO_S0,
3464 if (result != ERROR_OK) {
3465 riscv_batch_free(batch);
3471 dmi_write(target, DM_DATA1, value >> 32);
3472 dmi_write(target, DM_DATA0, value);
3474 /* Write and execute command that moves value into S1 and
3475 * executes program buffer. */
3476 uint32_t command = access_register_command(target,
3477 GDB_REGNO_S1, riscv_xlen(target),
3478 AC_ACCESS_REGISTER_POSTEXEC |
3479 AC_ACCESS_REGISTER_TRANSFER |
3480 AC_ACCESS_REGISTER_WRITE);
3481 result = execute_abstract_command(target, command);
3482 if (result != ERROR_OK) {
3483 riscv_batch_free(batch);
3487 /* Turn on autoexec */
3488 dmi_write(target, DM_ABSTRACTAUTO,
3489 1 << DM_ABSTRACTAUTO_AUTOEXECDATA_OFFSET);
3491 setup_needed = false;
3494 riscv_batch_add_dmi_write(batch, DM_DATA1, value >> 32);
3495 riscv_batch_add_dmi_write(batch, DM_DATA0, value);
3496 if (riscv_batch_full(batch))
3501 result = batch_run(target, batch);
3502 riscv_batch_free(batch);
3503 if (result != ERROR_OK)
3506 /* Note that if the scan resulted in a Busy DMI response, it
3507 * is this read to abstractcs that will cause the dmi_busy_delay
3508 * to be incremented if necessary. */
3510 uint32_t abstractcs;
3511 bool dmi_busy_encountered;
3512 result = dmi_op(target, &abstractcs, &dmi_busy_encountered,
3513 DMI_OP_READ, DM_ABSTRACTCS, 0, false, true);
3514 if (result != ERROR_OK)
3516 while (get_field(abstractcs, DM_ABSTRACTCS_BUSY))
3517 if (dmi_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
3519 info->cmderr = get_field(abstractcs, DM_ABSTRACTCS_CMDERR);
3520 if (info->cmderr == CMDERR_NONE && !dmi_busy_encountered) {
3521 LOG_DEBUG("successful (partial?) memory write");
3522 } else if (info->cmderr == CMDERR_BUSY || dmi_busy_encountered) {
3523 if (info->cmderr == CMDERR_BUSY)
3524 LOG_DEBUG("Memory write resulted in abstract command busy response.");
3525 else if (dmi_busy_encountered)
3526 LOG_DEBUG("Memory write resulted in DMI busy response.");
3527 riscv013_clear_abstract_error(target);
3528 increase_ac_busy_delay(target);
3530 dmi_write(target, DM_ABSTRACTAUTO, 0);
3531 result = register_read_direct(target, &cur_addr, GDB_REGNO_S0);
3532 if (result != ERROR_OK)
3534 setup_needed = true;
3536 LOG_ERROR("error when writing memory, abstractcs=0x%08lx", (long)abstractcs);
3537 riscv013_clear_abstract_error(target);
3538 result = ERROR_FAIL;
3544 dmi_write(target, DM_ABSTRACTAUTO, 0);
3546 if (register_write_direct(target, GDB_REGNO_S1, s1) != ERROR_OK)
3548 if (register_write_direct(target, GDB_REGNO_S0, s0) != ERROR_OK)
3551 /* Restore MSTATUS */
3552 if (mstatus != mstatus_old)
3553 if (register_write_direct(target, GDB_REGNO_MSTATUS, mstatus_old))
3556 if (execute_fence(target) != ERROR_OK)
3562 static int write_memory(struct target *target, target_addr_t address,
3563 uint32_t size, uint32_t count, const uint8_t *buffer)
3565 RISCV013_INFO(info);
3567 if (has_sufficient_progbuf(target, 3) && !riscv_prefer_sba)
3568 return write_memory_progbuf(target, address, size, count, buffer);
3570 if ((get_field(info->sbcs, DM_SBCS_SBACCESS8) && size == 1) ||
3571 (get_field(info->sbcs, DM_SBCS_SBACCESS16) && size == 2) ||
3572 (get_field(info->sbcs, DM_SBCS_SBACCESS32) && size == 4) ||
3573 (get_field(info->sbcs, DM_SBCS_SBACCESS64) && size == 8) ||
3574 (get_field(info->sbcs, DM_SBCS_SBACCESS128) && size == 16)) {
3575 if (get_field(info->sbcs, DM_SBCS_SBVERSION) == 0)
3576 return write_memory_bus_v0(target, address, size, count, buffer);
3577 else if (get_field(info->sbcs, DM_SBCS_SBVERSION) == 1)
3578 return write_memory_bus_v1(target, address, size, count, buffer);
3581 if (has_sufficient_progbuf(target, 3))
3582 return write_memory_progbuf(target, address, size, count, buffer);
3584 return write_memory_abstract(target, address, size, count, buffer);
3587 static int arch_state(struct target *target)
3592 struct target_type riscv013_target = {
3595 .init_target = init_target,
3596 .deinit_target = deinit_target,
3599 .poll = &riscv_openocd_poll,
3600 .halt = &riscv_halt,
3601 .step = &riscv_openocd_step,
3603 .assert_reset = assert_reset,
3604 .deassert_reset = deassert_reset,
3606 .write_memory = write_memory,
3608 .arch_state = arch_state,
3611 /*** 0.13-specific implementations of various RISC-V helper functions. ***/
3612 static int riscv013_get_register(struct target *target,
3613 riscv_reg_t *value, int hid, int rid)
3615 LOG_DEBUG("[%d] reading register %s on hart %d", target->coreid,
3616 gdb_regno_name(rid), hid);
3618 riscv_set_current_hartid(target, hid);
3620 int result = ERROR_OK;
3621 if (rid == GDB_REGNO_PC) {
3622 /* TODO: move this into riscv.c. */
3623 result = register_read(target, value, GDB_REGNO_DPC);
3624 LOG_DEBUG("[%d] read PC from DPC: 0x%" PRIx64, target->coreid, *value);
3625 } else if (rid == GDB_REGNO_PRIV) {
3627 /* TODO: move this into riscv.c. */
3628 result = register_read(target, &dcsr, GDB_REGNO_DCSR);
3629 *value = get_field(dcsr, CSR_DCSR_PRV);
3631 result = register_read(target, value, rid);
3632 if (result != ERROR_OK)
3639 static int riscv013_set_register(struct target *target, int hid, int rid, uint64_t value)
3641 LOG_DEBUG("[%d] writing 0x%" PRIx64 " to register %s on hart %d",
3642 target->coreid, value, gdb_regno_name(rid), hid);
3644 riscv_set_current_hartid(target, hid);
3646 if (rid <= GDB_REGNO_XPR31) {
3647 return register_write_direct(target, rid, value);
3648 } else if (rid == GDB_REGNO_PC) {
3649 LOG_DEBUG("[%d] writing PC to DPC: 0x%" PRIx64, target->coreid, value);
3650 register_write_direct(target, GDB_REGNO_DPC, value);
3651 uint64_t actual_value;
3652 register_read_direct(target, &actual_value, GDB_REGNO_DPC);
3653 LOG_DEBUG("[%d] actual DPC written: 0x%016" PRIx64, target->coreid, actual_value);
3654 if (value != actual_value) {
3655 LOG_ERROR("Written PC (0x%" PRIx64 ") does not match read back "
3656 "value (0x%" PRIx64 ")", value, actual_value);
3659 } else if (rid == GDB_REGNO_PRIV) {
3661 register_read(target, &dcsr, GDB_REGNO_DCSR);
3662 dcsr = set_field(dcsr, CSR_DCSR_PRV, value);
3663 return register_write_direct(target, GDB_REGNO_DCSR, dcsr);
3665 return register_write_direct(target, rid, value);
3671 static int riscv013_select_current_hart(struct target *target)
3675 dm013_info_t *dm = get_dm(target);
3678 if (r->current_hartid == dm->current_hartid)
3682 /* TODO: can't we just "dmcontrol = DMI_DMACTIVE"? */
3683 if (dmi_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
3685 dmcontrol = set_hartsel(dmcontrol, r->current_hartid);
3686 int result = dmi_write(target, DM_DMCONTROL, dmcontrol);
3687 dm->current_hartid = r->current_hartid;
3691 /* Select all harts that were prepped and that are selectable, clearing the
3692 * prepped flag on the harts that actually were selected. */
3693 static int select_prepped_harts(struct target *target, bool *use_hasel)
3695 dm013_info_t *dm = get_dm(target);
3698 if (!dm->hasel_supported) {
3705 assert(dm->hart_count);
3706 unsigned hawindow_count = (dm->hart_count + 31) / 32;
3707 uint32_t hawindow[hawindow_count];
3709 memset(hawindow, 0, sizeof(uint32_t) * hawindow_count);
3711 target_list_t *entry;
3712 unsigned total_selected = 0;
3713 list_for_each_entry(entry, &dm->target_list, list) {
3714 struct target *t = entry->target;
3715 riscv_info_t *r = riscv_info(t);
3716 riscv013_info_t *info = get_info(t);
3717 unsigned index = info->index;
3718 LOG_DEBUG("index=%d, coreid=%d, prepped=%d", index, t->coreid, r->prepped);
3719 r->selected = r->prepped;
3721 hawindow[index / 32] |= 1 << (index % 32);
3728 /* Don't use hasel if we only need to talk to one hart. */
3729 if (total_selected <= 1) {
3734 for (unsigned i = 0; i < hawindow_count; i++) {
3735 if (dmi_write(target, DM_HAWINDOWSEL, i) != ERROR_OK)
3737 if (dmi_write(target, DM_HAWINDOW, hawindow[i]) != ERROR_OK)
3745 static int riscv013_halt_prep(struct target *target)
3750 static int riscv013_halt_go(struct target *target)
3752 bool use_hasel = false;
3753 if (!riscv_rtos_enabled(target)) {
3754 if (select_prepped_harts(target, &use_hasel) != ERROR_OK)
3759 LOG_DEBUG("halting hart %d", r->current_hartid);
3761 /* Issue the halt command, and then wait for the current hart to halt. */
3762 uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_HALTREQ;
3764 dmcontrol |= DM_DMCONTROL_HASEL;
3765 dmcontrol = set_hartsel(dmcontrol, r->current_hartid);
3766 dmi_write(target, DM_DMCONTROL, dmcontrol);
3767 for (size_t i = 0; i < 256; ++i)
3768 if (riscv_is_halted(target))
3771 if (!riscv_is_halted(target)) {
3773 if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
3775 if (dmi_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
3778 LOG_ERROR("unable to halt hart %d", r->current_hartid);
3779 LOG_ERROR(" dmcontrol=0x%08x", dmcontrol);
3780 LOG_ERROR(" dmstatus =0x%08x", dmstatus);
3784 dmcontrol = set_field(dmcontrol, DM_DMCONTROL_HALTREQ, 0);
3785 dmi_write(target, DM_DMCONTROL, dmcontrol);
3788 target_list_t *entry;
3789 dm013_info_t *dm = get_dm(target);
3792 list_for_each_entry(entry, &dm->target_list, list) {
3793 struct target *t = entry->target;
3794 t->state = TARGET_HALTED;
3795 if (t->debug_reason == DBG_REASON_NOTHALTED)
3796 t->debug_reason = DBG_REASON_DBGRQ;
3799 /* The "else" case is handled in halt_go(). */
3804 static int riscv013_resume_go(struct target *target)
3806 bool use_hasel = false;
3807 if (!riscv_rtos_enabled(target)) {
3808 if (select_prepped_harts(target, &use_hasel) != ERROR_OK)
3812 return riscv013_step_or_resume_current_hart(target, false, use_hasel);
3815 static int riscv013_step_current_hart(struct target *target)
3817 return riscv013_step_or_resume_current_hart(target, true, false);
3820 static int riscv013_resume_prep(struct target *target)
3822 return riscv013_on_step_or_resume(target, false);
3825 static int riscv013_on_step(struct target *target)
3827 return riscv013_on_step_or_resume(target, true);
3830 static int riscv013_on_halt(struct target *target)
3835 static bool riscv013_is_halted(struct target *target)
3838 if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
3840 if (get_field(dmstatus, DM_DMSTATUS_ANYUNAVAIL))
3841 LOG_ERROR("Hart %d is unavailable.", riscv_current_hartid(target));
3842 if (get_field(dmstatus, DM_DMSTATUS_ANYNONEXISTENT))
3843 LOG_ERROR("Hart %d doesn't exist.", riscv_current_hartid(target));
3844 if (get_field(dmstatus, DM_DMSTATUS_ANYHAVERESET)) {
3845 int hartid = riscv_current_hartid(target);
3846 LOG_INFO("Hart %d unexpectedly reset!", hartid);
3847 /* TODO: Can we make this more obvious to eg. a gdb user? */
3848 uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE |
3849 DM_DMCONTROL_ACKHAVERESET;
3850 dmcontrol = set_hartsel(dmcontrol, hartid);
3851 /* If we had been halted when we reset, request another halt. If we
3852 * ended up running out of reset, then the user will (hopefully) get a
3853 * message that a reset happened, that the target is running, and then
3854 * that it is halted again once the request goes through.
3856 if (target->state == TARGET_HALTED)
3857 dmcontrol |= DM_DMCONTROL_HALTREQ;
3858 dmi_write(target, DM_DMCONTROL, dmcontrol);
3860 return get_field(dmstatus, DM_DMSTATUS_ALLHALTED);
3863 static enum riscv_halt_reason riscv013_halt_reason(struct target *target)
3866 int result = register_read(target, &dcsr, GDB_REGNO_DCSR);
3867 if (result != ERROR_OK)
3868 return RISCV_HALT_UNKNOWN;
3870 switch (get_field(dcsr, CSR_DCSR_CAUSE)) {
3871 case CSR_DCSR_CAUSE_SWBP:
3872 return RISCV_HALT_BREAKPOINT;
3873 case CSR_DCSR_CAUSE_TRIGGER:
3874 /* We could get here before triggers are enumerated if a trigger was
3875 * already set when we connected. Force enumeration now, which has the
3876 * side effect of clearing any triggers we did not set. */
3877 riscv_enumerate_triggers(target);
3878 LOG_DEBUG("{%d} halted because of trigger", target->coreid);
3879 return RISCV_HALT_TRIGGER;
3880 case CSR_DCSR_CAUSE_STEP:
3881 return RISCV_HALT_SINGLESTEP;
3882 case CSR_DCSR_CAUSE_DEBUGINT:
3883 case CSR_DCSR_CAUSE_HALT:
3884 return RISCV_HALT_INTERRUPT;
3885 case CSR_DCSR_CAUSE_GROUP:
3886 return RISCV_HALT_GROUP;
3889 LOG_ERROR("Unknown DCSR cause field: %x", (int)get_field(dcsr, CSR_DCSR_CAUSE));
3890 LOG_ERROR(" dcsr=0x%016lx", (long)dcsr);
3891 return RISCV_HALT_UNKNOWN;
3894 int riscv013_write_debug_buffer(struct target *target, unsigned index, riscv_insn_t data)
3896 dm013_info_t *dm = get_dm(target);
3899 if (dm->progbuf_cache[index] != data) {
3900 if (dmi_write(target, DM_PROGBUF0 + index, data) != ERROR_OK)
3902 dm->progbuf_cache[index] = data;
3904 LOG_DEBUG("cache hit for 0x%" PRIx32 " @%d", data, index);
3909 riscv_insn_t riscv013_read_debug_buffer(struct target *target, unsigned index)
3912 dmi_read(target, &value, DM_PROGBUF0 + index);
3916 int riscv013_execute_debug_buffer(struct target *target)
3918 uint32_t run_program = 0;
3919 run_program = set_field(run_program, AC_ACCESS_REGISTER_AARSIZE, 2);
3920 run_program = set_field(run_program, AC_ACCESS_REGISTER_POSTEXEC, 1);
3921 run_program = set_field(run_program, AC_ACCESS_REGISTER_TRANSFER, 0);
3922 run_program = set_field(run_program, AC_ACCESS_REGISTER_REGNO, 0x1000);
3924 return execute_abstract_command(target, run_program);
3927 void riscv013_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d)
3929 RISCV013_INFO(info);
3930 buf_set_u64((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_WRITE);
3931 buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, d);
3932 buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
3935 void riscv013_fill_dmi_read_u64(struct target *target, char *buf, int a)
3937 RISCV013_INFO(info);
3938 buf_set_u64((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_READ);
3939 buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, 0);
3940 buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
3943 void riscv013_fill_dmi_nop_u64(struct target *target, char *buf)
3945 RISCV013_INFO(info);
3946 buf_set_u64((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_NOP);
3947 buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, 0);
3948 buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, 0);
3951 /* Helper function for riscv013_test_sba_config_reg */
3952 static int get_max_sbaccess(struct target *target)
3954 RISCV013_INFO(info);
3956 uint32_t sbaccess128 = get_field(info->sbcs, DM_SBCS_SBACCESS128);
3957 uint32_t sbaccess64 = get_field(info->sbcs, DM_SBCS_SBACCESS64);
3958 uint32_t sbaccess32 = get_field(info->sbcs, DM_SBCS_SBACCESS32);
3959 uint32_t sbaccess16 = get_field(info->sbcs, DM_SBCS_SBACCESS16);
3960 uint32_t sbaccess8 = get_field(info->sbcs, DM_SBCS_SBACCESS8);
3964 else if (sbaccess64)
3966 else if (sbaccess32)
3968 else if (sbaccess16)
3976 static uint32_t get_num_sbdata_regs(struct target *target)
3978 RISCV013_INFO(info);
3980 uint32_t sbaccess128 = get_field(info->sbcs, DM_SBCS_SBACCESS128);
3981 uint32_t sbaccess64 = get_field(info->sbcs, DM_SBCS_SBACCESS64);
3982 uint32_t sbaccess32 = get_field(info->sbcs, DM_SBCS_SBACCESS32);
3986 else if (sbaccess64)
3988 else if (sbaccess32)
3994 static int riscv013_test_sba_config_reg(struct target *target,
3995 target_addr_t legal_address, uint32_t num_words,
3996 target_addr_t illegal_address, bool run_sbbusyerror_test)
3998 LOG_INFO("Testing System Bus Access as defined by RISC-V Debug Spec v0.13");
4000 uint32_t tests_failed = 0;
4004 dmi_read(target, &sbcs_orig, DM_SBCS);
4006 uint32_t sbcs = sbcs_orig;
4009 int max_sbaccess = get_max_sbaccess(target);
4011 if (max_sbaccess == -1) {
4012 LOG_ERROR("System Bus Access not supported in this config.");
4016 if (get_field(sbcs, DM_SBCS_SBVERSION) != 1) {
4017 LOG_ERROR("System Bus Access unsupported SBVERSION (%d). Only version 1 is supported.",
4018 get_field(sbcs, DM_SBCS_SBVERSION));
4022 uint32_t num_sbdata_regs = get_num_sbdata_regs(target);
4023 assert(num_sbdata_regs);
4025 uint32_t rd_buf[num_sbdata_regs];
4027 /* Test 1: Simple write/read test */
4029 sbcs = set_field(sbcs_orig, DM_SBCS_SBAUTOINCREMENT, 0);
4030 dmi_write(target, DM_SBCS, sbcs);
4032 uint32_t test_patterns[4] = {0xdeadbeef, 0xfeedbabe, 0x12345678, 0x08675309};
4033 for (uint32_t sbaccess = 0; sbaccess <= (uint32_t)max_sbaccess; sbaccess++) {
4034 sbcs = set_field(sbcs, DM_SBCS_SBACCESS, sbaccess);
4035 dmi_write(target, DM_SBCS, sbcs);
4037 uint32_t compare_mask = (sbaccess == 0) ? 0xff : (sbaccess == 1) ? 0xffff : 0xffffffff;
4039 for (uint32_t i = 0; i < num_words; i++) {
4040 uint32_t addr = legal_address + (i << sbaccess);
4041 uint32_t wr_data[num_sbdata_regs];
4042 for (uint32_t j = 0; j < num_sbdata_regs; j++)
4043 wr_data[j] = test_patterns[j] + i;
4044 write_memory_sba_simple(target, addr, wr_data, num_sbdata_regs, sbcs);
4047 for (uint32_t i = 0; i < num_words; i++) {
4048 uint32_t addr = legal_address + (i << sbaccess);
4049 read_memory_sba_simple(target, addr, rd_buf, num_sbdata_regs, sbcs);
4050 for (uint32_t j = 0; j < num_sbdata_regs; j++) {
4051 if (((test_patterns[j]+i)&compare_mask) != (rd_buf[j]&compare_mask)) {
4052 LOG_ERROR("System Bus Access Test 1: Error reading non-autoincremented address %x,"
4053 "expected val = %x, read val = %x", addr, test_patterns[j]+i, rd_buf[j]);
4054 test_passed = false;
4061 LOG_INFO("System Bus Access Test 1: Simple write/read test PASSED.");
4063 /* Test 2: Address autoincrement test */
4064 target_addr_t curr_addr;
4065 target_addr_t prev_addr;
4067 sbcs = set_field(sbcs_orig, DM_SBCS_SBAUTOINCREMENT, 1);
4068 dmi_write(target, DM_SBCS, sbcs);
4070 for (uint32_t sbaccess = 0; sbaccess <= (uint32_t)max_sbaccess; sbaccess++) {
4071 sbcs = set_field(sbcs, DM_SBCS_SBACCESS, sbaccess);
4072 dmi_write(target, DM_SBCS, sbcs);
4074 dmi_write(target, DM_SBADDRESS0, legal_address);
4075 read_sbcs_nonbusy(target, &sbcs);
4076 curr_addr = legal_address;
4077 for (uint32_t i = 0; i < num_words; i++) {
4078 prev_addr = curr_addr;
4079 read_sbcs_nonbusy(target, &sbcs);
4080 curr_addr = sb_read_address(target);
4081 if ((curr_addr - prev_addr != (uint32_t)(1 << sbaccess)) && (i != 0)) {
4082 LOG_ERROR("System Bus Access Test 2: Error with address auto-increment, sbaccess = %x.", sbaccess);
4083 test_passed = false;
4086 dmi_write(target, DM_SBDATA0, i);
4089 read_sbcs_nonbusy(target, &sbcs);
4091 dmi_write(target, DM_SBADDRESS0, legal_address);
4094 sbcs = set_field(sbcs, DM_SBCS_SBREADONDATA, 1);
4095 dmi_write(target, DM_SBCS, sbcs);
4096 dmi_read(target, &val, DM_SBDATA0); /* Dummy read to trigger first system bus read */
4097 curr_addr = legal_address;
4098 for (uint32_t i = 0; i < num_words; i++) {
4099 prev_addr = curr_addr;
4100 read_sbcs_nonbusy(target, &sbcs);
4101 curr_addr = sb_read_address(target);
4102 if ((curr_addr - prev_addr != (uint32_t)(1 << sbaccess)) && (i != 0)) {
4103 LOG_ERROR("System Bus Access Test 2: Error with address auto-increment, sbaccess = %x", sbaccess);
4104 test_passed = false;
4107 dmi_read(target, &val, DM_SBDATA0);
4108 read_sbcs_nonbusy(target, &sbcs);
4110 LOG_ERROR("System Bus Access Test 2: Error reading auto-incremented address,"
4111 "expected val = %x, read val = %x.", i, val);
4112 test_passed = false;
4118 LOG_INFO("System Bus Access Test 2: Address auto-increment test PASSED.");
4120 /* Test 3: Read from illegal address */
4121 read_memory_sba_simple(target, illegal_address, rd_buf, 1, sbcs_orig);
4123 dmi_read(target, &rd_val, DM_SBCS);
4124 if (get_field(rd_val, DM_SBCS_SBERROR) == 2) {
4125 sbcs = set_field(sbcs_orig, DM_SBCS_SBERROR, 2);
4126 dmi_write(target, DM_SBCS, sbcs);
4127 dmi_read(target, &rd_val, DM_SBCS);
4128 if (get_field(rd_val, DM_SBCS_SBERROR) == 0)
4129 LOG_INFO("System Bus Access Test 3: Illegal address read test PASSED.");
4131 LOG_ERROR("System Bus Access Test 3: Illegal address read test FAILED, unable to clear to 0.");
4133 LOG_ERROR("System Bus Access Test 3: Illegal address read test FAILED, unable to set error code.");
4136 /* Test 4: Write to illegal address */
4137 write_memory_sba_simple(target, illegal_address, test_patterns, 1, sbcs_orig);
4139 dmi_read(target, &rd_val, DM_SBCS);
4140 if (get_field(rd_val, DM_SBCS_SBERROR) == 2) {
4141 sbcs = set_field(sbcs_orig, DM_SBCS_SBERROR, 2);
4142 dmi_write(target, DM_SBCS, sbcs);
4143 dmi_read(target, &rd_val, DM_SBCS);
4144 if (get_field(rd_val, DM_SBCS_SBERROR) == 0)
4145 LOG_INFO("System Bus Access Test 4: Illegal address write test PASSED.");
4147 LOG_ERROR("System Bus Access Test 4: Illegal address write test FAILED, unable to clear to 0.");
4151 LOG_ERROR("System Bus Access Test 4: Illegal address write test FAILED, unable to set error code.");
4155 /* Test 5: Write with unsupported sbaccess size */
4156 uint32_t sbaccess128 = get_field(sbcs_orig, DM_SBCS_SBACCESS128);
4159 LOG_INFO("System Bus Access Test 5: SBCS sbaccess error test PASSED, all sbaccess sizes supported.");
4161 sbcs = set_field(sbcs_orig, DM_SBCS_SBACCESS, 4);
4163 write_memory_sba_simple(target, legal_address, test_patterns, 1, sbcs);
4165 dmi_read(target, &rd_val, DM_SBCS);
4166 if (get_field(rd_val, DM_SBCS_SBERROR) == 4) {
4167 sbcs = set_field(sbcs_orig, DM_SBCS_SBERROR, 4);
4168 dmi_write(target, DM_SBCS, sbcs);
4169 dmi_read(target, &rd_val, DM_SBCS);
4170 if (get_field(rd_val, DM_SBCS_SBERROR) == 0)
4171 LOG_INFO("System Bus Access Test 5: SBCS sbaccess error test PASSED.");
4173 LOG_ERROR("System Bus Access Test 5: SBCS sbaccess error test FAILED, unable to clear to 0.");
4177 LOG_ERROR("System Bus Access Test 5: SBCS sbaccess error test FAILED, unable to set error code.");
4182 /* Test 6: Write to misaligned address */
4183 sbcs = set_field(sbcs_orig, DM_SBCS_SBACCESS, 1);
4185 write_memory_sba_simple(target, legal_address+1, test_patterns, 1, sbcs);
4187 dmi_read(target, &rd_val, DM_SBCS);
4188 if (get_field(rd_val, DM_SBCS_SBERROR) == 3) {
4189 sbcs = set_field(sbcs_orig, DM_SBCS_SBERROR, 3);
4190 dmi_write(target, DM_SBCS, sbcs);
4191 dmi_read(target, &rd_val, DM_SBCS);
4192 if (get_field(rd_val, DM_SBCS_SBERROR) == 0)
4193 LOG_INFO("System Bus Access Test 6: SBCS address alignment error test PASSED");
4195 LOG_ERROR("System Bus Access Test 6: SBCS address alignment error test FAILED, unable to clear to 0.");
4199 LOG_ERROR("System Bus Access Test 6: SBCS address alignment error test FAILED, unable to set error code.");
4203 /* Test 7: Set sbbusyerror, only run this case in simulation as it is likely
4204 * impossible to hit otherwise */
4205 if (run_sbbusyerror_test) {
4206 sbcs = set_field(sbcs_orig, DM_SBCS_SBREADONADDR, 1);
4207 dmi_write(target, DM_SBCS, sbcs);
4209 for (int i = 0; i < 16; i++)
4210 dmi_write(target, DM_SBDATA0, 0xdeadbeef);
4212 for (int i = 0; i < 16; i++)
4213 dmi_write(target, DM_SBADDRESS0, legal_address);
4215 dmi_read(target, &rd_val, DM_SBCS);
4216 if (get_field(rd_val, DM_SBCS_SBBUSYERROR)) {
4217 sbcs = set_field(sbcs_orig, DM_SBCS_SBBUSYERROR, 1);
4218 dmi_write(target, DM_SBCS, sbcs);
4219 dmi_read(target, &rd_val, DM_SBCS);
4220 if (get_field(rd_val, DM_SBCS_SBBUSYERROR) == 0)
4221 LOG_INFO("System Bus Access Test 7: SBCS sbbusyerror test PASSED.");
4223 LOG_ERROR("System Bus Access Test 7: SBCS sbbusyerror test FAILED, unable to clear to 0.");
4227 LOG_ERROR("System Bus Access Test 7: SBCS sbbusyerror test FAILED, unable to set error code.");
4232 if (tests_failed == 0) {
4233 LOG_INFO("ALL TESTS PASSED");
4236 LOG_ERROR("%d TESTS FAILED", tests_failed);
4242 void write_memory_sba_simple(struct target *target, target_addr_t addr,
4243 uint32_t *write_data, uint32_t write_size, uint32_t sbcs)
4245 RISCV013_INFO(info);
4248 uint32_t masked_addr;
4250 uint32_t sba_size = get_field(info->sbcs, DM_SBCS_SBASIZE);
4252 read_sbcs_nonbusy(target, &rd_sbcs);
4254 uint32_t sbcs_no_readonaddr = set_field(sbcs, DM_SBCS_SBREADONADDR, 0);
4255 dmi_write(target, DM_SBCS, sbcs_no_readonaddr);
4257 for (uint32_t i = 0; i < sba_size/32; i++) {
4258 masked_addr = (addr >> 32*i) & 0xffffffff;
4261 dmi_write(target, DM_SBADDRESS0+i, masked_addr);
4263 dmi_write(target, DM_SBADDRESS3, masked_addr);
4266 /* Write SBDATA registers starting with highest address, since write to
4267 * SBDATA0 triggers write */
4268 for (int i = write_size-1; i >= 0; i--)
4269 dmi_write(target, DM_SBDATA0+i, write_data[i]);
4272 void read_memory_sba_simple(struct target *target, target_addr_t addr,
4273 uint32_t *rd_buf, uint32_t read_size, uint32_t sbcs)
4275 RISCV013_INFO(info);
4278 uint32_t masked_addr;
4280 uint32_t sba_size = get_field(info->sbcs, DM_SBCS_SBASIZE);
4282 read_sbcs_nonbusy(target, &rd_sbcs);
4284 uint32_t sbcs_readonaddr = set_field(sbcs, DM_SBCS_SBREADONADDR, 1);
4285 dmi_write(target, DM_SBCS, sbcs_readonaddr);
4287 /* Write addresses starting with highest address register */
4288 for (int i = sba_size/32-1; i >= 0; i--) {
4289 masked_addr = (addr >> 32*i) & 0xffffffff;
4292 dmi_write(target, DM_SBADDRESS0+i, masked_addr);
4294 dmi_write(target, DM_SBADDRESS3, masked_addr);
4297 read_sbcs_nonbusy(target, &rd_sbcs);
4299 for (uint32_t i = 0; i < read_size; i++)
4300 dmi_read(target, &(rd_buf[i]), DM_SBDATA0+i);
4303 int riscv013_dmi_write_u64_bits(struct target *target)
4305 RISCV013_INFO(info);
4306 return info->abits + DTM_DMI_DATA_LENGTH + DTM_DMI_OP_LENGTH;
4309 static int maybe_execute_fence_i(struct target *target)
4311 if (has_sufficient_progbuf(target, 3))
4312 return execute_fence(target);
4316 /* Helper Functions. */
4317 static int riscv013_on_step_or_resume(struct target *target, bool step)
4319 if (maybe_execute_fence_i(target) != ERROR_OK)
4322 /* We want to twiddle some bits in the debug CSR so debugging works. */
4324 int result = register_read(target, &dcsr, GDB_REGNO_DCSR);
4325 if (result != ERROR_OK)
4327 dcsr = set_field(dcsr, CSR_DCSR_STEP, step);
4328 dcsr = set_field(dcsr, CSR_DCSR_EBREAKM, riscv_ebreakm);
4329 dcsr = set_field(dcsr, CSR_DCSR_EBREAKS, riscv_ebreaks);
4330 dcsr = set_field(dcsr, CSR_DCSR_EBREAKU, riscv_ebreaku);
4331 return riscv_set_register(target, GDB_REGNO_DCSR, dcsr);
4334 static int riscv013_step_or_resume_current_hart(struct target *target,
4335 bool step, bool use_hasel)
4338 LOG_DEBUG("resuming hart %d (for step?=%d)", r->current_hartid, step);
4339 if (!riscv_is_halted(target)) {
4340 LOG_ERROR("Hart %d is not halted!", r->current_hartid);
4344 /* Issue the resume command, and then wait for the current hart to resume. */
4345 uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_RESUMEREQ;
4347 dmcontrol |= DM_DMCONTROL_HASEL;
4348 dmcontrol = set_hartsel(dmcontrol, r->current_hartid);
4349 dmi_write(target, DM_DMCONTROL, dmcontrol);
4351 dmcontrol = set_field(dmcontrol, DM_DMCONTROL_HASEL, 0);
4352 dmcontrol = set_field(dmcontrol, DM_DMCONTROL_RESUMEREQ, 0);
4355 for (size_t i = 0; i < 256; ++i) {
4357 if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
4359 if (get_field(dmstatus, DM_DMSTATUS_ALLRESUMEACK) == 0)
4361 if (step && get_field(dmstatus, DM_DMSTATUS_ALLHALTED) == 0)
4364 dmi_write(target, DM_DMCONTROL, dmcontrol);
4368 dmi_write(target, DM_DMCONTROL, dmcontrol);
4370 LOG_ERROR("unable to resume hart %d", r->current_hartid);
4371 if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
4373 LOG_ERROR(" dmstatus =0x%08x", dmstatus);
4376 LOG_ERROR(" was stepping, halting");
4384 void riscv013_clear_abstract_error(struct target *target)
4386 /* Wait for busy to go away. */
4387 time_t start = time(NULL);
4388 uint32_t abstractcs;
4389 dmi_read(target, &abstractcs, DM_ABSTRACTCS);
4390 while (get_field(abstractcs, DM_ABSTRACTCS_BUSY)) {
4391 dmi_read(target, &abstractcs, DM_ABSTRACTCS);
4393 if (time(NULL) - start > riscv_command_timeout_sec) {
4394 LOG_ERROR("abstractcs.busy is not going low after %d seconds "
4395 "(abstractcs=0x%x). The target is either really slow or "
4396 "broken. You could increase the timeout with riscv "
4397 "set_command_timeout_sec.",
4398 riscv_command_timeout_sec, abstractcs);
4402 /* Clear the error status. */
4403 dmi_write(target, DM_ABSTRACTCS, DM_ABSTRACTCS_CMDERR);
4407 #define FILE_SEP '\\'
4409 #define FILE_SEP '/'
4411 #define COMPLIANCE_TEST(b, message) \
4413 const char *last_sep = strrchr(__FILE__, FILE_SEP); \
4414 const char *fname = (last_sep == NULL ? __FILE__ : last_sep + 1); \
4415 LOG_INFO("Executing test %d (%s:%d): %s", total_tests, fname, __LINE__, message); \
4421 LOG_INFO(" %s", (pass) ? "PASSED" : "FAILED"); \
4426 #define COMPLIANCE_MUST_PASS(b) COMPLIANCE_TEST(ERROR_OK == (b), "Regular calls must return ERROR_OK")
4428 #define COMPLIANCE_READ(target, addr, value) COMPLIANCE_MUST_PASS(dmi_read(target, addr, value))
4429 #define COMPLIANCE_WRITE(target, addr, value) COMPLIANCE_MUST_PASS(dmi_write(target, addr, value))
4431 #define COMPLIANCE_CHECK_RO(target, addr) \
4435 COMPLIANCE_READ(target, &orig, addr); \
4436 COMPLIANCE_WRITE(target, addr, ~orig); \
4437 COMPLIANCE_READ(target, &inverse, addr); \
4438 COMPLIANCE_TEST(orig == inverse, "Register must be read-only"); \
4441 int riscv013_test_compliance(struct target *target)
4443 LOG_INFO("Basic compliance test against RISC-V Debug Spec v0.13");
4444 LOG_INFO("This test is not complete, and not well supported.");
4445 LOG_INFO("Your core might pass this test without being compliant.");
4446 LOG_INFO("Your core might fail this test while being compliant.");
4447 LOG_INFO("Use your judgment, and please contribute improvements.");
4449 if (!riscv_rtos_enabled(target)) {
4450 LOG_ERROR("Please run with -rtos riscv to run compliance test.");
4454 if (!target_was_examined(target)) {
4455 LOG_ERROR("Cannot run compliance test, because target has not yet "
4456 "been examined, or the examination failed.\n");
4460 int total_tests = 0;
4461 int passed_tests = 0;
4463 uint32_t dmcontrol_orig = DM_DMCONTROL_DMACTIVE;
4466 uint32_t testvar_read;
4468 RISCV013_INFO(info);
4470 /* All the bits of HARTSEL are covered by the examine sequence. */
4473 /* This field is optional. Either we can read and write it to 1/0,
4474 or it is tied to 0. This check doesn't really do anything, but
4475 it does attempt to set the bit to 1 and then back to 0, which needs to
4476 work if its implemented. */
4477 COMPLIANCE_WRITE(target, DM_DMCONTROL, set_field(dmcontrol_orig, DM_DMCONTROL_HARTRESET, 1));
4478 COMPLIANCE_WRITE(target, DM_DMCONTROL, set_field(dmcontrol_orig, DM_DMCONTROL_HARTRESET, 0));
4479 COMPLIANCE_READ(target, &dmcontrol, DM_DMCONTROL);
4480 COMPLIANCE_TEST((get_field(dmcontrol, DM_DMCONTROL_HARTRESET) == 0),
4481 "DMCONTROL.hartreset can be 0 or RW.");
4484 COMPLIANCE_WRITE(target, DM_DMCONTROL, set_field(dmcontrol_orig, DM_DMCONTROL_HASEL, 1));
4485 COMPLIANCE_WRITE(target, DM_DMCONTROL, set_field(dmcontrol_orig, DM_DMCONTROL_HASEL, 0));
4486 COMPLIANCE_READ(target, &dmcontrol, DM_DMCONTROL);
4487 COMPLIANCE_TEST((get_field(dmcontrol, DM_DMCONTROL_HASEL) == 0),
4488 "DMCONTROL.hasel can be 0 or RW.");
4489 /* TODO: test that hamask registers exist if hasel does. */
4492 COMPLIANCE_MUST_PASS(riscv_halt(target));
4493 /* This bit is not actually readable according to the spec, so nothing to check.*/
4496 COMPLIANCE_CHECK_RO(target, DM_DMSTATUS);
4499 /* This bit is not actually readable according to the spec, so nothing to check.*/
4500 COMPLIANCE_MUST_PASS(riscv_resume(target, true, 0, false, false, false));
4502 /* Halt all harts again so the test can continue.*/
4503 COMPLIANCE_MUST_PASS(riscv_halt(target));
4505 /* HARTINFO: Read-Only. This is per-hart, so need to adjust hartsel. */
4507 COMPLIANCE_READ(target, &hartinfo, DM_HARTINFO);
4508 for (int hartsel = 0; hartsel < riscv_count_harts(target); hartsel++) {
4509 COMPLIANCE_MUST_PASS(riscv_set_current_hartid(target, hartsel));
4511 COMPLIANCE_CHECK_RO(target, DM_HARTINFO);
4513 /* $dscratch CSRs */
4514 uint32_t nscratch = get_field(hartinfo, DM_HARTINFO_NSCRATCH);
4515 for (unsigned int d = 0; d < nscratch; d++) {
4516 riscv_reg_t testval, testval_read;
4517 /* Because DSCRATCH0 is not guaranteed to last across PB executions, need to put
4518 this all into one PB execution. Which may not be possible on all implementations.*/
4519 if (info->progbufsize >= 5) {
4520 for (testval = 0x0011223300112233;
4522 testval = testval == 0x0011223300112233 ? ~testval : 0xDEAD) {
4523 COMPLIANCE_TEST(register_write_direct(target, GDB_REGNO_S0, testval) == ERROR_OK,
4524 "Need to be able to write S0 in order to test DSCRATCH0.");
4525 struct riscv_program program32;
4526 riscv_program_init(&program32, target);
4527 riscv_program_csrw(&program32, GDB_REGNO_S0, GDB_REGNO_DSCRATCH0 + d);
4528 riscv_program_csrr(&program32, GDB_REGNO_S1, GDB_REGNO_DSCRATCH0 + d);
4529 riscv_program_fence(&program32);
4530 riscv_program_ebreak(&program32);
4531 COMPLIANCE_TEST(riscv_program_exec(&program32, target) == ERROR_OK,
4532 "Accessing DSCRATCH0 with program buffer should succeed.");
4533 COMPLIANCE_TEST(register_read_direct(target, &testval_read, GDB_REGNO_S1) == ERROR_OK,
4534 "Need to be able to read S1 in order to test DSCRATCH0.");
4535 if (riscv_xlen(target) > 32) {
4536 COMPLIANCE_TEST(testval == testval_read,
4537 "All DSCRATCH0 registers in HARTINFO must be R/W.");
4539 COMPLIANCE_TEST(testval_read == (testval & 0xFFFFFFFF),
4540 "All DSCRATCH0 registers in HARTINFO must be R/W.");
4545 /* TODO: dataaccess */
4546 if (get_field(hartinfo, DM_HARTINFO_DATAACCESS)) {
4547 /* TODO: Shadowed in memory map. */
4548 /* TODO: datasize */
4549 /* TODO: dataaddr */
4551 /* TODO: Shadowed in CSRs. */
4552 /* TODO: datasize */
4553 /* TODO: dataaddr */
4558 /* HALTSUM -- TODO: More than 32 harts. Would need to loop over this to set hartsel */
4559 /* TODO: HALTSUM2, HALTSUM3 */
4561 uint32_t expected_haltsum0 = 0;
4562 for (int i = 0; i < MIN(riscv_count_harts(target), 32); i++)
4563 expected_haltsum0 |= (1 << i);
4565 COMPLIANCE_READ(target, &testvar_read, DM_HALTSUM0);
4566 COMPLIANCE_TEST(testvar_read == expected_haltsum0,
4567 "HALTSUM0 should report summary of up to 32 halted harts");
4569 COMPLIANCE_WRITE(target, DM_HALTSUM0, 0xffffffff);
4570 COMPLIANCE_READ(target, &testvar_read, DM_HALTSUM0);
4571 COMPLIANCE_TEST(testvar_read == expected_haltsum0, "HALTSUM0 should be R/O");
4573 COMPLIANCE_WRITE(target, DM_HALTSUM0, 0x0);
4574 COMPLIANCE_READ(target, &testvar_read, DM_HALTSUM0);
4575 COMPLIANCE_TEST(testvar_read == expected_haltsum0, "HALTSUM0 should be R/O");
4578 uint32_t expected_haltsum1 = 0;
4579 for (int i = 0; i < MIN(riscv_count_harts(target), 1024); i += 32)
4580 expected_haltsum1 |= (1 << (i/32));
4582 COMPLIANCE_READ(target, &testvar_read, DM_HALTSUM1);
4583 COMPLIANCE_TEST(testvar_read == expected_haltsum1,
4584 "HALTSUM1 should report summary of up to 1024 halted harts");
4586 COMPLIANCE_WRITE(target, DM_HALTSUM1, 0xffffffff);
4587 COMPLIANCE_READ(target, &testvar_read, DM_HALTSUM1);
4588 COMPLIANCE_TEST(testvar_read == expected_haltsum1, "HALTSUM1 should be R/O");
4590 COMPLIANCE_WRITE(target, DM_HALTSUM1, 0x0);
4591 COMPLIANCE_READ(target, &testvar_read, DM_HALTSUM1);
4592 COMPLIANCE_TEST(testvar_read == expected_haltsum1, "HALTSUM1 should be R/O");
4594 /* TODO: HAWINDOWSEL */
4596 /* TODO: HAWINDOW */
4600 uint32_t abstractcs;
4601 COMPLIANCE_READ(target, &abstractcs, DM_ABSTRACTCS);
4603 /* Check that all reported Data Words are really R/W */
4604 for (int invert = 0; invert < 2; invert++) {
4605 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT); i++) {
4606 testvar = (i + 1) * 0x11111111;
4609 COMPLIANCE_WRITE(target, DM_DATA0 + i, testvar);
4611 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT); i++) {
4612 testvar = (i + 1) * 0x11111111;
4615 COMPLIANCE_READ(target, &testvar_read, DM_DATA0 + i);
4616 COMPLIANCE_TEST(testvar_read == testvar, "All reported DATA words must be R/W");
4620 /* Check that all reported ProgBuf words are really R/W */
4621 for (int invert = 0; invert < 2; invert++) {
4622 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE); i++) {
4623 testvar = (i + 1) * 0x11111111;
4626 COMPLIANCE_WRITE(target, DM_PROGBUF0 + i, testvar);
4628 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE); i++) {
4629 testvar = (i + 1) * 0x11111111;
4632 COMPLIANCE_READ(target, &testvar_read, DM_PROGBUF0 + i);
4633 COMPLIANCE_TEST(testvar_read == testvar, "All reported PROGBUF words must be R/W");
4637 /* TODO: Cause and clear all error types */
4640 According to the spec, this register is only W, so can't really check the read result.
4641 But at any rate, this is not legal and should cause an error. */
4642 COMPLIANCE_WRITE(target, DM_COMMAND, 0xAAAAAAAA);
4643 COMPLIANCE_READ(target, &testvar_read, DM_ABSTRACTCS);
4644 COMPLIANCE_TEST(get_field(testvar_read, DM_ABSTRACTCS_CMDERR) == CMDERR_NOT_SUPPORTED,
4645 "Illegal COMMAND should result in UNSUPPORTED");
4646 COMPLIANCE_WRITE(target, DM_ABSTRACTCS, DM_ABSTRACTCS_CMDERR);
4648 COMPLIANCE_WRITE(target, DM_COMMAND, 0x55555555);
4649 COMPLIANCE_READ(target, &testvar_read, DM_ABSTRACTCS);
4650 COMPLIANCE_TEST(get_field(testvar_read, DM_ABSTRACTCS_CMDERR) == CMDERR_NOT_SUPPORTED,
4651 "Illegal COMMAND should result in UNSUPPORTED");
4652 COMPLIANCE_WRITE(target, DM_ABSTRACTCS, DM_ABSTRACTCS_CMDERR);
4654 /* Basic Abstract Commands */
4655 for (unsigned int i = 1; i < 32; i = i << 1) {
4656 riscv_reg_t testval = i | ((i + 1ULL) << 32);
4657 riscv_reg_t testval_read;
4658 COMPLIANCE_TEST(ERROR_OK == register_write_direct(target, GDB_REGNO_ZERO + i, testval),
4659 "GPR Writes should be supported.");
4660 COMPLIANCE_MUST_PASS(write_abstract_arg(target, 0, 0xDEADBEEFDEADBEEF, 64));
4661 COMPLIANCE_TEST(ERROR_OK == register_read_direct(target, &testval_read, GDB_REGNO_ZERO + i),
4662 "GPR Reads should be supported.");
4663 if (riscv_xlen(target) > 32) {
4664 /* Dummy comment to satisfy linter, since removing the branches here doesn't actually compile. */
4665 COMPLIANCE_TEST(testval == testval_read, "GPR Reads and writes should be supported.");
4667 /* Dummy comment to satisfy linter, since removing the branches here doesn't actually compile. */
4668 COMPLIANCE_TEST((testval & 0xFFFFFFFF) == testval_read, "GPR Reads and writes should be supported.");
4673 See which bits are actually writable */
4674 COMPLIANCE_WRITE(target, DM_ABSTRACTAUTO, 0xFFFFFFFF);
4675 uint32_t abstractauto;
4677 COMPLIANCE_READ(target, &abstractauto, DM_ABSTRACTAUTO);
4678 COMPLIANCE_WRITE(target, DM_ABSTRACTAUTO, 0x0);
4679 if (abstractauto > 0) {
4680 /* This mechanism only works when you have a reasonable sized progbuf, which is not
4681 a true compliance requirement. */
4682 if (info->progbufsize >= 3) {
4685 COMPLIANCE_TEST(ERROR_OK == register_write_direct(target, GDB_REGNO_S0, 0),
4686 "Need to be able to write S0 to test ABSTRACTAUTO");
4687 struct riscv_program program;
4688 COMPLIANCE_MUST_PASS(riscv_program_init(&program, target));
4689 /* This is also testing that WFI() is a NOP during debug mode. */
4690 COMPLIANCE_MUST_PASS(riscv_program_insert(&program, wfi()));
4691 COMPLIANCE_MUST_PASS(riscv_program_addi(&program, GDB_REGNO_S0, GDB_REGNO_S0, 1));
4692 COMPLIANCE_MUST_PASS(riscv_program_ebreak(&program));
4693 COMPLIANCE_WRITE(target, DM_ABSTRACTAUTO, 0x0);
4694 COMPLIANCE_MUST_PASS(riscv_program_exec(&program, target));
4696 COMPLIANCE_WRITE(target, DM_ABSTRACTAUTO, 0xFFFFFFFF);
4697 COMPLIANCE_READ(target, &abstractauto, DM_ABSTRACTAUTO);
4698 uint32_t autoexec_data = get_field(abstractauto, DM_ABSTRACTAUTO_AUTOEXECDATA);
4699 uint32_t autoexec_progbuf = get_field(abstractauto, DM_ABSTRACTAUTO_AUTOEXECPROGBUF);
4700 for (unsigned int i = 0; i < 12; i++) {
4701 COMPLIANCE_READ(target, &testvar_read, DM_DATA0 + i);
4703 COMPLIANCE_READ(target, &testvar_read, DM_ABSTRACTCS);
4704 busy = get_field(testvar_read, DM_ABSTRACTCS_BUSY);
4706 if (autoexec_data & (1 << i)) {
4707 COMPLIANCE_TEST(i < get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT),
4708 "AUTOEXEC may be writable up to DATACOUNT bits.");
4712 for (unsigned int i = 0; i < 16; i++) {
4713 COMPLIANCE_READ(target, &testvar_read, DM_PROGBUF0 + i);
4715 COMPLIANCE_READ(target, &testvar_read, DM_ABSTRACTCS);
4716 busy = get_field(testvar_read, DM_ABSTRACTCS_BUSY);
4718 if (autoexec_progbuf & (1 << i)) {
4719 COMPLIANCE_TEST(i < get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE),
4720 "AUTOEXEC may be writable up to PROGBUFSIZE bits.");
4725 COMPLIANCE_WRITE(target, DM_ABSTRACTAUTO, 0);
4726 COMPLIANCE_TEST(ERROR_OK == register_read_direct(target, &value, GDB_REGNO_S0),
4727 "Need to be able to read S0 to test ABSTRACTAUTO");
4729 COMPLIANCE_TEST(testvar == value,
4730 "ABSTRACTAUTO should cause COMMAND to run the expected number of times.");
4734 /* Single-Step each hart. */
4735 for (int hartsel = 0; hartsel < riscv_count_harts(target); hartsel++) {
4736 COMPLIANCE_MUST_PASS(riscv_set_current_hartid(target, hartsel));
4737 COMPLIANCE_MUST_PASS(riscv013_on_step(target));
4738 COMPLIANCE_MUST_PASS(riscv013_step_current_hart(target));
4739 COMPLIANCE_TEST(riscv_halt_reason(target, hartsel) == RISCV_HALT_SINGLESTEP,
4740 "Single Step should result in SINGLESTEP");
4743 /* Core Register Tests */
4744 uint64_t bogus_dpc = 0xdeadbeef;
4745 for (int hartsel = 0; hartsel < riscv_count_harts(target); hartsel++) {
4746 COMPLIANCE_MUST_PASS(riscv_set_current_hartid(target, hartsel));
4749 COMPLIANCE_MUST_PASS(register_write_direct(target, GDB_REGNO_DCSR, 0x0));
4750 COMPLIANCE_MUST_PASS(register_read_direct(target, &value, GDB_REGNO_DCSR));
4751 COMPLIANCE_TEST(value != 0, "Not all bits in DCSR are writable by Debugger");
4752 COMPLIANCE_MUST_PASS(register_write_direct(target, GDB_REGNO_DCSR, 0xFFFFFFFF));
4753 COMPLIANCE_MUST_PASS(register_read_direct(target, &value, GDB_REGNO_DCSR));
4754 COMPLIANCE_TEST(value != 0, "At least some bits in DCSR must be 1");
4756 /* DPC. Note that DPC is sign-extended. */
4757 riscv_reg_t dpcmask = 0xFFFFFFFCUL;
4760 if (riscv_xlen(target) > 32)
4761 dpcmask |= (0xFFFFFFFFULL << 32);
4763 if (riscv_supports_extension(target, riscv_current_hartid(target), 'C'))
4766 COMPLIANCE_MUST_PASS(register_write_direct(target, GDB_REGNO_DPC, dpcmask));
4767 COMPLIANCE_MUST_PASS(register_read_direct(target, &dpc, GDB_REGNO_DPC));
4768 COMPLIANCE_TEST(dpcmask == dpc,
4769 "DPC must be sign-extended to XLEN and writable to all-1s (except the least significant bits)");
4770 COMPLIANCE_MUST_PASS(register_write_direct(target, GDB_REGNO_DPC, 0));
4771 COMPLIANCE_MUST_PASS(register_read_direct(target, &dpc, GDB_REGNO_DPC));
4772 COMPLIANCE_TEST(dpc == 0, "DPC must be writable to 0.");
4774 bogus_dpc = dpc; /* For a later test step */
4778 Asserting non-debug module reset should not reset Debug Module state.
4779 But it should reset Hart State, e.g. DPC should get a different value.
4780 Also make sure that DCSR reports cause of 'HALT' even though previously we single-stepped.
4783 /* Write some registers. They should not be impacted by ndmreset. */
4784 COMPLIANCE_WRITE(target, DM_COMMAND, 0xFFFFFFFF);
4786 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE); i++) {
4787 testvar = (i + 1) * 0x11111111;
4788 COMPLIANCE_WRITE(target, DM_PROGBUF0 + i, testvar);
4791 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT); i++) {
4792 testvar = (i + 1) * 0x11111111;
4793 COMPLIANCE_WRITE(target, DM_DATA0 + i, testvar);
4796 COMPLIANCE_WRITE(target, DM_ABSTRACTAUTO, 0xFFFFFFFF);
4797 COMPLIANCE_READ(target, &abstractauto, DM_ABSTRACTAUTO);
4800 target->reset_halt = true;
4801 COMPLIANCE_MUST_PASS(riscv_set_current_hartid(target, 0));
4802 COMPLIANCE_TEST(ERROR_OK == assert_reset(target), "Must be able to assert NDMRESET");
4803 COMPLIANCE_TEST(ERROR_OK == deassert_reset(target), "Must be able to deassert NDMRESET");
4805 /* Verify that most stuff is not affected by ndmreset. */
4806 COMPLIANCE_READ(target, &testvar_read, DM_ABSTRACTCS);
4807 COMPLIANCE_TEST(get_field(testvar_read, DM_ABSTRACTCS_CMDERR) == CMDERR_NOT_SUPPORTED,
4808 "NDMRESET should not affect DM_ABSTRACTCS");
4809 COMPLIANCE_READ(target, &testvar_read, DM_ABSTRACTAUTO);
4810 COMPLIANCE_TEST(testvar_read == abstractauto, "NDMRESET should not affect DM_ABSTRACTAUTO");
4812 /* Clean up to avoid future test failures */
4813 COMPLIANCE_WRITE(target, DM_ABSTRACTCS, DM_ABSTRACTCS_CMDERR);
4814 COMPLIANCE_WRITE(target, DM_ABSTRACTAUTO, 0);
4816 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE); i++) {
4817 testvar = (i + 1) * 0x11111111;
4818 COMPLIANCE_READ(target, &testvar_read, DM_PROGBUF0 + i);
4819 COMPLIANCE_TEST(testvar_read == testvar, "PROGBUF words must not be affected by NDMRESET");
4822 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT); i++) {
4823 testvar = (i + 1) * 0x11111111;
4824 COMPLIANCE_READ(target, &testvar_read, DM_DATA0 + i);
4825 COMPLIANCE_TEST(testvar_read == testvar, "DATA words must not be affected by NDMRESET");
4828 /* Verify that DPC *is* affected by ndmreset. Since we don't know what it *should* be,
4829 just verify that at least it's not the bogus value anymore. */
4831 COMPLIANCE_TEST(bogus_dpc != 0xdeadbeef, "BOGUS DPC should have been set somehow (bug in compliance test)");
4832 COMPLIANCE_MUST_PASS(register_read_direct(target, &value, GDB_REGNO_DPC));
4833 COMPLIANCE_TEST(bogus_dpc != value, "NDMRESET should move DPC to reset value.");
4835 COMPLIANCE_TEST(riscv_halt_reason(target, 0) == RISCV_HALT_INTERRUPT,
4836 "After NDMRESET halt, DCSR should report cause of halt");
4838 /* DMACTIVE -- deasserting DMACTIVE should reset all the above values. */
4840 /* Toggle dmactive */
4841 COMPLIANCE_WRITE(target, DM_DMCONTROL, 0);
4842 COMPLIANCE_WRITE(target, DM_DMCONTROL, DM_DMCONTROL_DMACTIVE);
4843 COMPLIANCE_READ(target, &testvar_read, DM_ABSTRACTCS);
4844 COMPLIANCE_TEST(get_field(testvar_read, DM_ABSTRACTCS_CMDERR) == 0, "ABSTRACTCS.cmderr should reset to 0");
4845 COMPLIANCE_READ(target, &testvar_read, DM_ABSTRACTAUTO);
4846 COMPLIANCE_TEST(testvar_read == 0, "ABSTRACTAUTO should reset to 0");
4848 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE); i++) {
4849 COMPLIANCE_READ(target, &testvar_read, DM_PROGBUF0 + i);
4850 COMPLIANCE_TEST(testvar_read == 0, "PROGBUF words should reset to 0");
4853 for (unsigned int i = 0; i < get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT); i++) {
4854 COMPLIANCE_READ(target, &testvar_read, DM_DATA0 + i);
4855 COMPLIANCE_TEST(testvar_read == 0, "DATA words should reset to 0");
4860 * DCSR.cause priorities
4861 * DCSR.stoptime/stopcycle
4867 /* Halt every hart for any follow-up tests*/
4868 COMPLIANCE_MUST_PASS(riscv_halt(target));
4870 uint32_t failed_tests = total_tests - passed_tests;
4871 if (total_tests == passed_tests) {
4872 LOG_INFO("ALL TESTS PASSED\n");
4875 LOG_INFO("%d TESTS FAILED\n", failed_tests);