9 #include "target/target.h"
10 #include "target/algorithm.h"
11 #include "target/target_type.h"
13 #include "jtag/jtag.h"
14 #include "target/register.h"
15 #include "target/breakpoints.h"
16 #include "helper/time_support.h"
19 #include "rtos/rtos.h"
22 * Since almost everything can be accomplish by scanning the dbus register, all
23 * functions here assume dbus is already selected. The exception are functions
24 * called directly by OpenOCD, which can't assume anything about what's
25 * currently in IR. They should set IR to dbus explicitly.
31 * At the bottom of the stack are the OpenOCD JTAG functions:
36 * There are a few functions to just instantly shift a register and get its
42 * Because doing one scan and waiting for the result is slow, most functions
43 * batch up a bunch of dbus writes and then execute them all at once. They use
44 * the scans "class" for this:
49 * Usually you new(), call a bunch of add functions, then execute() and look
50 * at the results by calling scans_get...()
52 * Optimized functions will directly use the scans class above, but slightly
53 * lazier code will use the cache functions that in turn use the scans
58 * cache_set... update a local structure, which is then synced to the target
59 * with cache_write(). Only Debug RAM words that are actually changed are sent
60 * to the target. Afterwards use cache_get... to read results.
63 #define get_field(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
64 #define set_field(reg, mask, val) (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
66 #define DIM(x) (sizeof(x)/sizeof(*x))
68 /* Constants for legacy SiFive hardware breakpoints. */
69 #define CSR_BPCONTROL_X (1<<0)
70 #define CSR_BPCONTROL_W (1<<1)
71 #define CSR_BPCONTROL_R (1<<2)
72 #define CSR_BPCONTROL_U (1<<3)
73 #define CSR_BPCONTROL_S (1<<4)
74 #define CSR_BPCONTROL_H (1<<5)
75 #define CSR_BPCONTROL_M (1<<6)
76 #define CSR_BPCONTROL_BPMATCH (0xf<<7)
77 #define CSR_BPCONTROL_BPACTION (0xff<<11)
79 #define DEBUG_ROM_START 0x800
80 #define DEBUG_ROM_RESUME (DEBUG_ROM_START + 4)
81 #define DEBUG_ROM_EXCEPTION (DEBUG_ROM_START + 8)
82 #define DEBUG_RAM_START 0x400
84 #define SETHALTNOT 0x10c
86 /*** JTAG registers. ***/
88 #define DTMCONTROL 0x10
89 #define DTMCONTROL_DBUS_RESET (1<<16)
90 #define DTMCONTROL_IDLE (7<<10)
91 #define DTMCONTROL_ADDRBITS (0xf<<4)
92 #define DTMCONTROL_VERSION (0xf)
95 #define DBUS_OP_START 0
96 #define DBUS_OP_SIZE 2
103 DBUS_STATUS_SUCCESS = 0,
104 DBUS_STATUS_FAILED = 2,
107 #define DBUS_DATA_START 2
108 #define DBUS_DATA_SIZE 34
109 #define DBUS_ADDRESS_START 36
123 /*** Debug Bus registers. ***/
125 #define DMCONTROL 0x10
126 #define DMCONTROL_INTERRUPT (((uint64_t)1)<<33)
127 #define DMCONTROL_HALTNOT (((uint64_t)1)<<32)
128 #define DMCONTROL_BUSERROR (7<<19)
129 #define DMCONTROL_SERIAL (3<<16)
130 #define DMCONTROL_AUTOINCREMENT (1<<15)
131 #define DMCONTROL_ACCESS (7<<12)
132 #define DMCONTROL_HARTID (0x3ff<<2)
133 #define DMCONTROL_NDRESET (1<<1)
134 #define DMCONTROL_FULLRESET 1
137 #define DMINFO_ABUSSIZE (0x7fU<<25)
138 #define DMINFO_SERIALCOUNT (0xf<<21)
139 #define DMINFO_ACCESS128 (1<<20)
140 #define DMINFO_ACCESS64 (1<<19)
141 #define DMINFO_ACCESS32 (1<<18)
142 #define DMINFO_ACCESS16 (1<<17)
143 #define DMINFO_ACCESS8 (1<<16)
144 #define DMINFO_DRAMSIZE (0x3f<<10)
145 #define DMINFO_AUTHENTICATED (1<<5)
146 #define DMINFO_AUTHBUSY (1<<4)
147 #define DMINFO_AUTHTYPE (3<<2)
148 #define DMINFO_VERSION 3
150 /*** Info about the core being debugged. ***/
152 #define DBUS_ADDRESS_UNKNOWN 0xffff
155 #define DRAM_CACHE_SIZE 16
157 uint8_t ir_dtmcontrol[1] = {DTMCONTROL};
158 struct scan_field select_dtmcontrol = {
160 .out_value = ir_dtmcontrol
162 uint8_t ir_dbus[1] = {DBUS};
163 struct scan_field select_dbus = {
167 uint8_t ir_idcode[1] = {0x1};
168 struct scan_field select_idcode = {
170 .out_value = ir_idcode
178 bool read, write, execute;
182 /* Wall-clock timeout for a command/access. Settable via RISC-V Target commands.*/
183 int riscv_command_timeout_sec = DEFAULT_COMMAND_TIMEOUT_SEC;
185 /* Wall-clock timeout after reset. Settable via RISC-V Target commands.*/
186 int riscv_reset_timeout_sec = DEFAULT_RESET_TIMEOUT_SEC;
188 bool riscv_prefer_sba;
190 /* In addition to the ones in the standard spec, we'll also expose additional
192 * The list is either NULL, or a series of ranges (inclusive), terminated with
198 static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
200 struct scan_field field;
202 uint8_t out_value[4];
204 buf_set_u32(out_value, 0, 32, out);
206 jtag_add_ir_scan(target->tap, &select_dtmcontrol, TAP_IDLE);
209 field.out_value = out_value;
210 field.in_value = in_value;
211 jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
213 /* Always return to dbus. */
214 jtag_add_ir_scan(target->tap, &select_dbus, TAP_IDLE);
216 int retval = jtag_execute_queue();
217 if (retval != ERROR_OK) {
218 LOG_ERROR("failed jtag scan: %d", retval);
222 uint32_t in = buf_get_u32(field.in_value, 0, 32);
223 LOG_DEBUG("DTMCONTROL: 0x%x -> 0x%x", out, in);
228 static struct target_type *get_target_type(struct target *target)
230 riscv_info_t *info = (riscv_info_t *) target->arch_info;
233 LOG_ERROR("Target has not been initialized");
237 switch (info->dtm_version) {
239 return &riscv011_target;
241 return &riscv013_target;
243 LOG_ERROR("Unsupported DTM version: %d", info->dtm_version);
248 static int riscv_init_target(struct command_context *cmd_ctx,
249 struct target *target)
251 LOG_DEBUG("riscv_init_target()");
252 target->arch_info = calloc(1, sizeof(riscv_info_t));
253 if (!target->arch_info)
255 riscv_info_t *info = (riscv_info_t *) target->arch_info;
256 riscv_info_init(target, info);
257 info->cmd_ctx = cmd_ctx;
259 select_dtmcontrol.num_bits = target->tap->ir_length;
260 select_dbus.num_bits = target->tap->ir_length;
261 select_idcode.num_bits = target->tap->ir_length;
263 riscv_semihosting_init(target);
268 static void riscv_deinit_target(struct target *target)
270 LOG_DEBUG("riscv_deinit_target()");
271 struct target_type *tt = get_target_type(target);
273 tt->deinit_target(target);
274 riscv_info_t *info = (riscv_info_t *) target->arch_info;
277 target->arch_info = NULL;
280 static int oldriscv_halt(struct target *target)
282 struct target_type *tt = get_target_type(target);
283 return tt->halt(target);
286 static void trigger_from_breakpoint(struct trigger *trigger,
287 const struct breakpoint *breakpoint)
289 trigger->address = breakpoint->address;
290 trigger->length = breakpoint->length;
291 trigger->mask = ~0LL;
292 trigger->read = false;
293 trigger->write = false;
294 trigger->execute = true;
295 /* unique_id is unique across both breakpoints and watchpoints. */
296 trigger->unique_id = breakpoint->unique_id;
299 static int maybe_add_trigger_t1(struct target *target, unsigned hartid,
300 struct trigger *trigger, uint64_t tdata1)
304 const uint32_t bpcontrol_x = 1<<0;
305 const uint32_t bpcontrol_w = 1<<1;
306 const uint32_t bpcontrol_r = 1<<2;
307 const uint32_t bpcontrol_u = 1<<3;
308 const uint32_t bpcontrol_s = 1<<4;
309 const uint32_t bpcontrol_h = 1<<5;
310 const uint32_t bpcontrol_m = 1<<6;
311 const uint32_t bpcontrol_bpmatch = 0xf << 7;
312 const uint32_t bpcontrol_bpaction = 0xff << 11;
314 if (tdata1 & (bpcontrol_r | bpcontrol_w | bpcontrol_x)) {
315 /* Trigger is already in use, presumably by user code. */
316 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
319 tdata1 = set_field(tdata1, bpcontrol_r, trigger->read);
320 tdata1 = set_field(tdata1, bpcontrol_w, trigger->write);
321 tdata1 = set_field(tdata1, bpcontrol_x, trigger->execute);
322 tdata1 = set_field(tdata1, bpcontrol_u,
323 !!(r->misa[hartid] & (1 << ('U' - 'A'))));
324 tdata1 = set_field(tdata1, bpcontrol_s,
325 !!(r->misa[hartid] & (1 << ('S' - 'A'))));
326 tdata1 = set_field(tdata1, bpcontrol_h,
327 !!(r->misa[hartid] & (1 << ('H' - 'A'))));
328 tdata1 |= bpcontrol_m;
329 tdata1 = set_field(tdata1, bpcontrol_bpmatch, 0); /* exact match */
330 tdata1 = set_field(tdata1, bpcontrol_bpaction, 0); /* cause bp exception */
332 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, tdata1);
334 riscv_reg_t tdata1_rb;
335 if (riscv_get_register_on_hart(target, &tdata1_rb, hartid,
336 GDB_REGNO_TDATA1) != ERROR_OK)
338 LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
340 if (tdata1 != tdata1_rb) {
341 LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
342 PRIx64 " to tdata1 it contains 0x%" PRIx64,
344 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
345 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
348 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA2, trigger->address);
353 static int maybe_add_trigger_t2(struct target *target, unsigned hartid,
354 struct trigger *trigger, uint64_t tdata1)
358 /* tselect is already set */
359 if (tdata1 & (MCONTROL_EXECUTE | MCONTROL_STORE | MCONTROL_LOAD)) {
360 /* Trigger is already in use, presumably by user code. */
361 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
364 /* address/data match trigger */
365 tdata1 |= MCONTROL_DMODE(riscv_xlen(target));
366 tdata1 = set_field(tdata1, MCONTROL_ACTION,
367 MCONTROL_ACTION_DEBUG_MODE);
368 tdata1 = set_field(tdata1, MCONTROL_MATCH, MCONTROL_MATCH_EQUAL);
369 tdata1 |= MCONTROL_M;
370 if (r->misa[hartid] & (1 << ('H' - 'A')))
371 tdata1 |= MCONTROL_H;
372 if (r->misa[hartid] & (1 << ('S' - 'A')))
373 tdata1 |= MCONTROL_S;
374 if (r->misa[hartid] & (1 << ('U' - 'A')))
375 tdata1 |= MCONTROL_U;
377 if (trigger->execute)
378 tdata1 |= MCONTROL_EXECUTE;
380 tdata1 |= MCONTROL_LOAD;
382 tdata1 |= MCONTROL_STORE;
384 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, tdata1);
387 int result = riscv_get_register_on_hart(target, &tdata1_rb, hartid, GDB_REGNO_TDATA1);
388 if (result != ERROR_OK)
390 LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
392 if (tdata1 != tdata1_rb) {
393 LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
394 PRIx64 " to tdata1 it contains 0x%" PRIx64,
396 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
397 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
400 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA2, trigger->address);
405 static int add_trigger(struct target *target, struct trigger *trigger)
409 if (riscv_enumerate_triggers(target) != ERROR_OK)
412 /* In RTOS mode, we need to set the same trigger in the same slot on every
413 * hart, to keep up the illusion that each hart is a thread running on the
416 /* Otherwise, we just set the trigger on the one hart this target deals
419 riscv_reg_t tselect[RISCV_MAX_HARTS];
422 for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
423 if (!riscv_hart_enabled(target, hartid))
427 int result = riscv_get_register_on_hart(target, &tselect[hartid],
428 hartid, GDB_REGNO_TSELECT);
429 if (result != ERROR_OK)
432 assert(first_hart >= 0);
435 for (i = 0; i < r->trigger_count[first_hart]; i++) {
436 if (r->trigger_unique_id[i] != -1)
439 riscv_set_register_on_hart(target, first_hart, GDB_REGNO_TSELECT, i);
442 int result = riscv_get_register_on_hart(target, &tdata1, first_hart,
444 if (result != ERROR_OK)
446 int type = get_field(tdata1, MCONTROL_TYPE(riscv_xlen(target)));
449 for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
450 if (!riscv_hart_enabled(target, hartid))
452 if (hartid > first_hart)
453 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, i);
456 result = maybe_add_trigger_t1(target, hartid, trigger, tdata1);
459 result = maybe_add_trigger_t2(target, hartid, trigger, tdata1);
462 LOG_DEBUG("trigger %d has unknown type %d", i, type);
466 if (result != ERROR_OK)
470 if (result != ERROR_OK)
473 LOG_DEBUG("Using trigger %d (type %d) for bp %d", i, type,
475 r->trigger_unique_id[i] = trigger->unique_id;
479 for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
480 if (!riscv_hart_enabled(target, hartid))
482 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT,
486 if (i >= r->trigger_count[first_hart]) {
487 LOG_ERROR("Couldn't find an available hardware trigger.");
488 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
494 int riscv_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
496 if (breakpoint->type == BKPT_SOFT) {
497 if (target_read_memory(target, breakpoint->address, breakpoint->length, 1,
498 breakpoint->orig_instr) != ERROR_OK) {
499 LOG_ERROR("Failed to read original instruction at 0x%" TARGET_PRIxADDR,
500 breakpoint->address);
505 if (breakpoint->length == 4)
506 retval = target_write_u32(target, breakpoint->address, ebreak());
508 retval = target_write_u16(target, breakpoint->address, ebreak_c());
509 if (retval != ERROR_OK) {
510 LOG_ERROR("Failed to write %d-byte breakpoint instruction at 0x%"
511 TARGET_PRIxADDR, breakpoint->length, breakpoint->address);
515 } else if (breakpoint->type == BKPT_HARD) {
516 struct trigger trigger;
517 trigger_from_breakpoint(&trigger, breakpoint);
518 int result = add_trigger(target, &trigger);
519 if (result != ERROR_OK)
523 LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
524 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
527 breakpoint->set = true;
532 static int remove_trigger(struct target *target, struct trigger *trigger)
536 if (riscv_enumerate_triggers(target) != ERROR_OK)
540 for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
541 if (!riscv_hart_enabled(target, hartid))
543 if (first_hart < 0) {
548 assert(first_hart >= 0);
551 for (i = 0; i < r->trigger_count[first_hart]; i++) {
552 if (r->trigger_unique_id[i] == trigger->unique_id)
555 if (i >= r->trigger_count[first_hart]) {
556 LOG_ERROR("Couldn't find the hardware resources used by hardware "
560 LOG_DEBUG("Stop using resource %d for bp %d", i, trigger->unique_id);
561 for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
562 if (!riscv_hart_enabled(target, hartid))
565 int result = riscv_get_register_on_hart(target, &tselect, hartid, GDB_REGNO_TSELECT);
566 if (result != ERROR_OK)
568 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, i);
569 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
570 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, tselect);
572 r->trigger_unique_id[i] = -1;
577 int riscv_remove_breakpoint(struct target *target,
578 struct breakpoint *breakpoint)
580 if (breakpoint->type == BKPT_SOFT) {
581 if (target_write_memory(target, breakpoint->address, breakpoint->length, 1,
582 breakpoint->orig_instr) != ERROR_OK) {
583 LOG_ERROR("Failed to restore instruction for %d-byte breakpoint at "
584 "0x%" TARGET_PRIxADDR, breakpoint->length, breakpoint->address);
588 } else if (breakpoint->type == BKPT_HARD) {
589 struct trigger trigger;
590 trigger_from_breakpoint(&trigger, breakpoint);
591 int result = remove_trigger(target, &trigger);
592 if (result != ERROR_OK)
596 LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
597 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
600 breakpoint->set = false;
605 static void trigger_from_watchpoint(struct trigger *trigger,
606 const struct watchpoint *watchpoint)
608 trigger->address = watchpoint->address;
609 trigger->length = watchpoint->length;
610 trigger->mask = watchpoint->mask;
611 trigger->value = watchpoint->value;
612 trigger->read = (watchpoint->rw == WPT_READ || watchpoint->rw == WPT_ACCESS);
613 trigger->write = (watchpoint->rw == WPT_WRITE || watchpoint->rw == WPT_ACCESS);
614 trigger->execute = false;
615 /* unique_id is unique across both breakpoints and watchpoints. */
616 trigger->unique_id = watchpoint->unique_id;
619 int riscv_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
621 struct trigger trigger;
622 trigger_from_watchpoint(&trigger, watchpoint);
624 int result = add_trigger(target, &trigger);
625 if (result != ERROR_OK)
627 watchpoint->set = true;
632 int riscv_remove_watchpoint(struct target *target,
633 struct watchpoint *watchpoint)
635 struct trigger trigger;
636 trigger_from_watchpoint(&trigger, watchpoint);
638 int result = remove_trigger(target, &trigger);
639 if (result != ERROR_OK)
641 watchpoint->set = false;
646 static int oldriscv_step(struct target *target, int current, uint32_t address,
647 int handle_breakpoints)
649 struct target_type *tt = get_target_type(target);
650 return tt->step(target, current, address, handle_breakpoints);
653 static int old_or_new_riscv_step(
654 struct target *target,
656 target_addr_t address,
657 int handle_breakpoints
660 LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints);
661 if (r->is_halted == NULL)
662 return oldriscv_step(target, current, address, handle_breakpoints);
664 return riscv_openocd_step(target, current, address, handle_breakpoints);
668 static int riscv_examine(struct target *target)
670 LOG_DEBUG("riscv_examine()");
671 if (target_was_examined(target)) {
672 LOG_DEBUG("Target was already examined.");
676 /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
678 riscv_info_t *info = (riscv_info_t *) target->arch_info;
679 uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
680 LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol);
681 info->dtm_version = get_field(dtmcontrol, DTMCONTROL_VERSION);
682 LOG_DEBUG(" version=0x%x", info->dtm_version);
684 struct target_type *tt = get_target_type(target);
688 int result = tt->init_target(info->cmd_ctx, target);
689 if (result != ERROR_OK)
692 return tt->examine(target);
695 static int oldriscv_poll(struct target *target)
697 struct target_type *tt = get_target_type(target);
698 return tt->poll(target);
701 static int old_or_new_riscv_poll(struct target *target)
704 if (r->is_halted == NULL)
705 return oldriscv_poll(target);
707 return riscv_openocd_poll(target);
710 static int old_or_new_riscv_halt(struct target *target)
713 if (r->is_halted == NULL)
714 return oldriscv_halt(target);
716 return riscv_openocd_halt(target);
719 static int riscv_assert_reset(struct target *target)
721 struct target_type *tt = get_target_type(target);
722 return tt->assert_reset(target);
725 static int riscv_deassert_reset(struct target *target)
727 LOG_DEBUG("RISCV DEASSERT RESET");
728 struct target_type *tt = get_target_type(target);
729 return tt->deassert_reset(target);
733 static int oldriscv_resume(struct target *target, int current, uint32_t address,
734 int handle_breakpoints, int debug_execution)
736 struct target_type *tt = get_target_type(target);
737 return tt->resume(target, current, address, handle_breakpoints,
741 static int old_or_new_riscv_resume(
742 struct target *target,
744 target_addr_t address,
745 int handle_breakpoints,
749 LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints);
750 if (r->is_halted == NULL)
751 return oldriscv_resume(target, current, address, handle_breakpoints, debug_execution);
753 return riscv_openocd_resume(target, current, address, handle_breakpoints, debug_execution);
756 static int riscv_select_current_hart(struct target *target)
759 if (r->rtos_hartid != -1 && riscv_rtos_enabled(target))
760 return riscv_set_current_hartid(target, r->rtos_hartid);
762 return riscv_set_current_hartid(target, target->coreid);
765 static int riscv_read_memory(struct target *target, target_addr_t address,
766 uint32_t size, uint32_t count, uint8_t *buffer)
768 if (riscv_select_current_hart(target) != ERROR_OK)
770 struct target_type *tt = get_target_type(target);
771 return tt->read_memory(target, address, size, count, buffer);
774 static int riscv_write_memory(struct target *target, target_addr_t address,
775 uint32_t size, uint32_t count, const uint8_t *buffer)
777 if (riscv_select_current_hart(target) != ERROR_OK)
779 struct target_type *tt = get_target_type(target);
780 return tt->write_memory(target, address, size, count, buffer);
783 static int riscv_get_gdb_reg_list(struct target *target,
784 struct reg **reg_list[], int *reg_list_size,
785 enum target_register_class reg_class)
788 LOG_DEBUG("reg_class=%d", reg_class);
789 LOG_DEBUG("rtos_hartid=%d current_hartid=%d", r->rtos_hartid, r->current_hartid);
791 if (!target->reg_cache) {
792 LOG_ERROR("Target not initialized. Return ERROR_FAIL.");
796 if (riscv_select_current_hart(target) != ERROR_OK)
800 case REG_CLASS_GENERAL:
804 *reg_list_size = GDB_REGNO_COUNT;
807 LOG_ERROR("Unsupported reg_class: %d", reg_class);
811 *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
815 for (int i = 0; i < *reg_list_size; i++) {
816 assert(!target->reg_cache->reg_list[i].valid ||
817 target->reg_cache->reg_list[i].size > 0);
818 (*reg_list)[i] = &target->reg_cache->reg_list[i];
824 static int riscv_arch_state(struct target *target)
826 struct target_type *tt = get_target_type(target);
827 return tt->arch_state(target);
830 /* Algorithm must end with a software breakpoint instruction. */
831 static int riscv_run_algorithm(struct target *target, int num_mem_params,
832 struct mem_param *mem_params, int num_reg_params,
833 struct reg_param *reg_params, target_addr_t entry_point,
834 target_addr_t exit_point, int timeout_ms, void *arch_info)
836 riscv_info_t *info = (riscv_info_t *) target->arch_info;
838 if (num_mem_params > 0) {
839 LOG_ERROR("Memory parameters are not supported for RISC-V algorithms.");
843 if (target->state != TARGET_HALTED) {
844 LOG_WARNING("target not halted");
845 return ERROR_TARGET_NOT_HALTED;
849 struct reg *reg_pc = register_get_by_name(target->reg_cache, "pc", 1);
850 if (!reg_pc || reg_pc->type->get(reg_pc) != ERROR_OK)
852 uint64_t saved_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
854 uint64_t saved_regs[32];
855 for (int i = 0; i < num_reg_params; i++) {
856 if (mem_params[i].direction == PARAM_IN)
859 LOG_DEBUG("save %s", reg_params[i].reg_name);
860 struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, 0);
862 LOG_ERROR("Couldn't find register named '%s'", reg_params[i].reg_name);
866 if (r->size != reg_params[i].size) {
867 LOG_ERROR("Register %s is %d bits instead of %d bits.",
868 reg_params[i].reg_name, r->size, reg_params[i].size);
872 if (r->number > GDB_REGNO_XPR31) {
873 LOG_ERROR("Only GPRs can be use as argument registers.");
877 if (r->type->get(r) != ERROR_OK)
879 saved_regs[r->number] = buf_get_u64(r->value, 0, r->size);
880 if (r->type->set(r, reg_params[i].value) != ERROR_OK)
885 /* Disable Interrupts before attempting to run the algorithm. */
886 uint64_t current_mstatus;
887 uint8_t mstatus_bytes[8];
889 LOG_DEBUG("Disabling Interrupts");
890 struct reg *reg_mstatus = register_get_by_name(target->reg_cache,
893 LOG_ERROR("Couldn't find mstatus!");
897 reg_mstatus->type->get(reg_mstatus);
898 current_mstatus = buf_get_u64(reg_mstatus->value, 0, reg_mstatus->size);
899 uint64_t ie_mask = MSTATUS_MIE | MSTATUS_HIE | MSTATUS_SIE | MSTATUS_UIE;
900 buf_set_u64(mstatus_bytes, 0, info->xlen[0], set_field(current_mstatus,
903 reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
906 LOG_DEBUG("resume at 0x%" TARGET_PRIxADDR, entry_point);
907 if (oldriscv_resume(target, 0, entry_point, 0, 0) != ERROR_OK)
910 int64_t start = timeval_ms();
911 while (target->state != TARGET_HALTED) {
913 int64_t now = timeval_ms();
914 if (now - start > timeout_ms) {
915 LOG_ERROR("Algorithm timed out after %d ms.", timeout_ms);
916 LOG_ERROR(" now = 0x%08x", (uint32_t) now);
917 LOG_ERROR(" start = 0x%08x", (uint32_t) start);
918 oldriscv_halt(target);
919 old_or_new_riscv_poll(target);
920 return ERROR_TARGET_TIMEOUT;
923 int result = old_or_new_riscv_poll(target);
924 if (result != ERROR_OK)
928 if (reg_pc->type->get(reg_pc) != ERROR_OK)
930 uint64_t final_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
931 if (final_pc != exit_point) {
932 LOG_ERROR("PC ended up at 0x%" PRIx64 " instead of 0x%"
933 TARGET_PRIxADDR, final_pc, exit_point);
937 /* Restore Interrupts */
938 LOG_DEBUG("Restoring Interrupts");
939 buf_set_u64(mstatus_bytes, 0, info->xlen[0], current_mstatus);
940 reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
942 /* Restore registers */
944 buf_set_u64(buf, 0, info->xlen[0], saved_pc);
945 if (reg_pc->type->set(reg_pc, buf) != ERROR_OK)
948 for (int i = 0; i < num_reg_params; i++) {
949 LOG_DEBUG("restore %s", reg_params[i].reg_name);
950 struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, 0);
951 buf_set_u64(buf, 0, info->xlen[0], saved_regs[r->number]);
952 if (r->type->set(r, buf) != ERROR_OK)
959 /* Should run code on the target to perform CRC of
960 memory. Not yet implemented.
963 static int riscv_checksum_memory(struct target *target,
964 target_addr_t address, uint32_t count,
967 *checksum = 0xFFFFFFFF;
968 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
971 /*** OpenOCD Helper Functions ***/
973 enum riscv_poll_hart {
975 RPH_DISCOVERED_HALTED,
976 RPH_DISCOVERED_RUNNING,
979 static enum riscv_poll_hart riscv_poll_hart(struct target *target, int hartid)
982 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
985 LOG_DEBUG("polling hart %d, target->state=%d", hartid, target->state);
987 /* If OpenOCD thinks we're running but this hart is halted then it's time
988 * to raise an event. */
989 bool halted = riscv_is_halted(target);
990 if (target->state != TARGET_HALTED && halted) {
991 LOG_DEBUG(" triggered a halt");
993 return RPH_DISCOVERED_HALTED;
994 } else if (target->state != TARGET_RUNNING && !halted) {
995 LOG_DEBUG(" triggered running");
996 target->state = TARGET_RUNNING;
997 return RPH_DISCOVERED_RUNNING;
1000 return RPH_NO_CHANGE;
1003 /*** OpenOCD Interface ***/
1004 int riscv_openocd_poll(struct target *target)
1006 LOG_DEBUG("polling all harts");
1007 int halted_hart = -1;
1008 if (riscv_rtos_enabled(target)) {
1009 /* Check every hart for an event. */
1010 for (int i = 0; i < riscv_count_harts(target); ++i) {
1011 enum riscv_poll_hart out = riscv_poll_hart(target, i);
1014 case RPH_DISCOVERED_RUNNING:
1016 case RPH_DISCOVERED_HALTED:
1023 if (halted_hart == -1) {
1024 LOG_DEBUG(" no harts just halted, target->state=%d", target->state);
1027 LOG_DEBUG(" hart %d halted", halted_hart);
1029 /* If we're here then at least one hart triggered. That means
1030 * we want to go and halt _every_ hart in the system, as that's
1031 * the invariant we hold here. Some harts might have already
1032 * halted (as we're either in single-step mode or they also
1033 * triggered a breakpoint), so don't attempt to halt those
1035 for (int i = 0; i < riscv_count_harts(target); ++i)
1036 riscv_halt_one_hart(target, i);
1038 enum riscv_poll_hart out = riscv_poll_hart(target,
1039 riscv_current_hartid(target));
1040 if (out == RPH_NO_CHANGE || out == RPH_DISCOVERED_RUNNING)
1042 else if (out == RPH_ERROR)
1045 halted_hart = riscv_current_hartid(target);
1046 LOG_DEBUG(" hart %d halted", halted_hart);
1049 target->state = TARGET_HALTED;
1050 switch (riscv_halt_reason(target, halted_hart)) {
1051 case RISCV_HALT_BREAKPOINT:
1052 target->debug_reason = DBG_REASON_BREAKPOINT;
1054 case RISCV_HALT_TRIGGER:
1055 target->debug_reason = DBG_REASON_WATCHPOINT;
1057 case RISCV_HALT_INTERRUPT:
1058 target->debug_reason = DBG_REASON_DBGRQ;
1060 case RISCV_HALT_SINGLESTEP:
1061 target->debug_reason = DBG_REASON_SINGLESTEP;
1063 case RISCV_HALT_UNKNOWN:
1064 target->debug_reason = DBG_REASON_UNDEFINED;
1066 case RISCV_HALT_ERROR:
1070 if (riscv_rtos_enabled(target)) {
1071 target->rtos->current_threadid = halted_hart + 1;
1072 target->rtos->current_thread = halted_hart + 1;
1075 target->state = TARGET_HALTED;
1077 if (target->debug_reason == DBG_REASON_BREAKPOINT) {
1079 if (riscv_semihosting(target, &retval) != 0)
1083 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1087 int riscv_openocd_halt(struct target *target)
1091 LOG_DEBUG("halting all harts");
1093 int out = riscv_halt_all_harts(target);
1094 if (out != ERROR_OK) {
1095 LOG_ERROR("Unable to halt all harts");
1099 register_cache_invalidate(target->reg_cache);
1100 if (riscv_rtos_enabled(target)) {
1101 target->rtos->current_threadid = r->rtos_hartid + 1;
1102 target->rtos->current_thread = r->rtos_hartid + 1;
1105 target->state = TARGET_HALTED;
1106 target->debug_reason = DBG_REASON_DBGRQ;
1107 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1111 int riscv_openocd_resume(
1112 struct target *target,
1114 target_addr_t address,
1115 int handle_breakpoints,
1116 int debug_execution)
1118 LOG_DEBUG("debug_reason=%d", target->debug_reason);
1121 riscv_set_register(target, GDB_REGNO_PC, address);
1123 if (target->debug_reason == DBG_REASON_WATCHPOINT) {
1124 /* To be able to run off a trigger, disable all the triggers, step, and
1125 * then resume as usual. */
1126 struct watchpoint *watchpoint = target->watchpoints;
1127 bool trigger_temporarily_cleared[RISCV_MAX_HWBPS] = {0};
1130 int result = ERROR_OK;
1131 while (watchpoint && result == ERROR_OK) {
1132 LOG_DEBUG("watchpoint %d: set=%d", i, watchpoint->set);
1133 trigger_temporarily_cleared[i] = watchpoint->set;
1134 if (watchpoint->set)
1135 result = riscv_remove_watchpoint(target, watchpoint);
1136 watchpoint = watchpoint->next;
1140 if (result == ERROR_OK)
1141 result = riscv_step_rtos_hart(target);
1143 watchpoint = target->watchpoints;
1145 while (watchpoint) {
1146 LOG_DEBUG("watchpoint %d: cleared=%d", i, trigger_temporarily_cleared[i]);
1147 if (trigger_temporarily_cleared[i]) {
1148 if (result == ERROR_OK)
1149 result = riscv_add_watchpoint(target, watchpoint);
1151 riscv_add_watchpoint(target, watchpoint);
1153 watchpoint = watchpoint->next;
1157 if (result != ERROR_OK)
1161 int out = riscv_resume_all_harts(target);
1162 if (out != ERROR_OK) {
1163 LOG_ERROR("unable to resume all harts");
1167 register_cache_invalidate(target->reg_cache);
1168 target->state = TARGET_RUNNING;
1169 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
1173 int riscv_openocd_step(
1174 struct target *target,
1176 target_addr_t address,
1177 int handle_breakpoints
1179 LOG_DEBUG("stepping rtos hart");
1182 riscv_set_register(target, GDB_REGNO_PC, address);
1184 int out = riscv_step_rtos_hart(target);
1185 if (out != ERROR_OK) {
1186 LOG_ERROR("unable to step rtos hart");
1190 register_cache_invalidate(target->reg_cache);
1191 target->state = TARGET_RUNNING;
1192 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
1193 target->state = TARGET_HALTED;
1194 target->debug_reason = DBG_REASON_SINGLESTEP;
1195 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1199 /* Command Handlers */
1200 COMMAND_HANDLER(riscv_set_command_timeout_sec)
1202 if (CMD_ARGC != 1) {
1203 LOG_ERROR("Command takes exactly 1 parameter");
1204 return ERROR_COMMAND_SYNTAX_ERROR;
1206 int timeout = atoi(CMD_ARGV[0]);
1208 LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
1212 riscv_command_timeout_sec = timeout;
1217 COMMAND_HANDLER(riscv_set_reset_timeout_sec)
1219 if (CMD_ARGC != 1) {
1220 LOG_ERROR("Command takes exactly 1 parameter");
1221 return ERROR_COMMAND_SYNTAX_ERROR;
1223 int timeout = atoi(CMD_ARGV[0]);
1225 LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
1229 riscv_reset_timeout_sec = timeout;
1233 COMMAND_HANDLER(riscv_set_prefer_sba)
1235 if (CMD_ARGC != 1) {
1236 LOG_ERROR("Command takes exactly 1 parameter");
1237 return ERROR_COMMAND_SYNTAX_ERROR;
1239 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], riscv_prefer_sba);
1243 void parse_error(const char *string, char c, unsigned position)
1245 char buf[position+2];
1246 for (unsigned i = 0; i < position; i++)
1248 buf[position] = '^';
1249 buf[position + 1] = 0;
1251 LOG_ERROR("Parse error at character %c in:", c);
1252 LOG_ERROR("%s", string);
1253 LOG_ERROR("%s", buf);
1256 COMMAND_HANDLER(riscv_set_expose_csrs)
1258 if (CMD_ARGC != 1) {
1259 LOG_ERROR("Command takes exactly 1 parameter");
1260 return ERROR_COMMAND_SYNTAX_ERROR;
1263 for (unsigned pass = 0; pass < 2; pass++) {
1266 bool parse_low = true;
1268 for (unsigned i = 0; i == 0 || CMD_ARGV[0][i-1]; i++) {
1269 char c = CMD_ARGV[0][i];
1271 /* Ignore whitespace. */
1279 } else if (c == '-') {
1281 } else if (c == ',' || c == 0) {
1283 expose_csr[range].low = low;
1284 expose_csr[range].high = low;
1289 parse_error(CMD_ARGV[0], c, i);
1290 return ERROR_COMMAND_SYNTAX_ERROR;
1297 } else if (c == ',' || c == 0) {
1300 expose_csr[range].low = low;
1301 expose_csr[range].high = high;
1307 parse_error(CMD_ARGV[0], c, i);
1308 return ERROR_COMMAND_SYNTAX_ERROR;
1316 expose_csr = calloc(range + 2, sizeof(*expose_csr));
1318 expose_csr[range].low = 1;
1319 expose_csr[range].high = 0;
1325 COMMAND_HANDLER(riscv_authdata_read)
1327 if (CMD_ARGC != 0) {
1328 LOG_ERROR("Command takes no parameters");
1329 return ERROR_COMMAND_SYNTAX_ERROR;
1332 struct target *target = get_current_target(CMD_CTX);
1334 LOG_ERROR("target is NULL!");
1340 LOG_ERROR("riscv_info is NULL!");
1344 if (r->authdata_read) {
1346 if (r->authdata_read(target, &value) != ERROR_OK)
1348 command_print(CMD_CTX, "0x%" PRIx32, value);
1351 LOG_ERROR("authdata_read is not implemented for this target.");
1356 COMMAND_HANDLER(riscv_authdata_write)
1358 if (CMD_ARGC != 1) {
1359 LOG_ERROR("Command takes exactly 1 argument");
1360 return ERROR_COMMAND_SYNTAX_ERROR;
1363 struct target *target = get_current_target(CMD_CTX);
1367 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], value);
1369 if (r->authdata_write) {
1370 return r->authdata_write(target, value);
1372 LOG_ERROR("authdata_write is not implemented for this target.");
1377 COMMAND_HANDLER(riscv_dmi_read)
1379 if (CMD_ARGC != 1) {
1380 LOG_ERROR("Command takes 1 parameter");
1381 return ERROR_COMMAND_SYNTAX_ERROR;
1384 struct target *target = get_current_target(CMD_CTX);
1386 LOG_ERROR("target is NULL!");
1392 LOG_ERROR("riscv_info is NULL!");
1397 uint32_t address, value;
1398 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
1399 if (r->dmi_read(target, &value, address) != ERROR_OK)
1401 command_print(CMD_CTX, "0x%" PRIx32, value);
1404 LOG_ERROR("dmi_read is not implemented for this target.");
1410 COMMAND_HANDLER(riscv_dmi_write)
1412 if (CMD_ARGC != 2) {
1413 LOG_ERROR("Command takes exactly 2 arguments");
1414 return ERROR_COMMAND_SYNTAX_ERROR;
1417 struct target *target = get_current_target(CMD_CTX);
1420 uint32_t address, value;
1421 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
1422 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
1425 return r->dmi_write(target, address, value);
1427 LOG_ERROR("dmi_write is not implemented for this target.");
1432 static const struct command_registration riscv_exec_command_handlers[] = {
1434 .name = "set_command_timeout_sec",
1435 .handler = riscv_set_command_timeout_sec,
1436 .mode = COMMAND_ANY,
1437 .usage = "riscv set_command_timeout_sec [sec]",
1438 .help = "Set the wall-clock timeout (in seconds) for individual commands"
1441 .name = "set_reset_timeout_sec",
1442 .handler = riscv_set_reset_timeout_sec,
1443 .mode = COMMAND_ANY,
1444 .usage = "riscv set_reset_timeout_sec [sec]",
1445 .help = "Set the wall-clock timeout (in seconds) after reset is deasserted"
1448 .name = "set_prefer_sba",
1449 .handler = riscv_set_prefer_sba,
1450 .mode = COMMAND_ANY,
1451 .usage = "riscv set_prefer_sba on|off",
1452 .help = "When on, prefer to use System Bus Access to access memory. "
1453 "When off, prefer to use the Program Buffer to access memory."
1456 .name = "expose_csrs",
1457 .handler = riscv_set_expose_csrs,
1458 .mode = COMMAND_ANY,
1459 .usage = "riscv expose_csrs n0[-m0][,n1[-m1]]...",
1460 .help = "Configure a list of inclusive ranges for CSRs to expose in "
1461 "addition to the standard ones. This must be executed before "
1465 .name = "authdata_read",
1466 .handler = riscv_authdata_read,
1467 .mode = COMMAND_ANY,
1468 .usage = "riscv authdata_read",
1469 .help = "Return the 32-bit value read from authdata."
1472 .name = "authdata_write",
1473 .handler = riscv_authdata_write,
1474 .mode = COMMAND_ANY,
1475 .usage = "riscv authdata_write value",
1476 .help = "Write the 32-bit value to authdata."
1480 .handler = riscv_dmi_read,
1481 .mode = COMMAND_ANY,
1482 .usage = "riscv dmi_read address",
1483 .help = "Perform a 32-bit DMI read at address, returning the value."
1486 .name = "dmi_write",
1487 .handler = riscv_dmi_write,
1488 .mode = COMMAND_ANY,
1489 .usage = "riscv dmi_write address value",
1490 .help = "Perform a 32-bit DMI write of value at address."
1492 COMMAND_REGISTRATION_DONE
1495 extern __COMMAND_HANDLER(handle_common_semihosting_command);
1496 extern __COMMAND_HANDLER(handle_common_semihosting_fileio_command);
1497 extern __COMMAND_HANDLER(handle_common_semihosting_resumable_exit_command);
1498 extern __COMMAND_HANDLER(handle_common_semihosting_cmdline);
1501 * To be noted that RISC-V targets use the same semihosting commands as
1504 * The main reason is compatibility with existing tools. For example the
1505 * Eclipse OpenOCD/SEGGER J-Link/QEMU plug-ins have several widgets to
1506 * configure semihosting, which generate commands like `arm semihosting
1508 * A secondary reason is the fact that the protocol used is exactly the
1509 * one specified by ARM. If RISC-V will ever define its own semihosting
1510 * protocol, then a command like `riscv semihosting enable` will make
1511 * sense, but for now all semihosting commands are prefixed with `arm`.
1513 static const struct command_registration arm_exec_command_handlers[] = {
1516 .handler = handle_common_semihosting_command,
1517 .mode = COMMAND_EXEC,
1518 .usage = "['enable'|'disable']",
1519 .help = "activate support for semihosting operations",
1522 "semihosting_cmdline",
1523 .handler = handle_common_semihosting_cmdline,
1524 .mode = COMMAND_EXEC,
1525 .usage = "arguments",
1526 .help = "command line arguments to be passed to program",
1529 "semihosting_fileio",
1530 .handler = handle_common_semihosting_fileio_command,
1531 .mode = COMMAND_EXEC,
1532 .usage = "['enable'|'disable']",
1533 .help = "activate support for semihosting fileio operations",
1536 "semihosting_resexit",
1537 .handler = handle_common_semihosting_resumable_exit_command,
1538 .mode = COMMAND_EXEC,
1539 .usage = "['enable'|'disable']",
1540 .help = "activate support for semihosting resumable exit",
1542 COMMAND_REGISTRATION_DONE
1545 const struct command_registration riscv_command_handlers[] = {
1548 .mode = COMMAND_ANY,
1549 .help = "RISC-V Command Group",
1551 .chain = riscv_exec_command_handlers
1555 .mode = COMMAND_ANY,
1556 .help = "ARM Command Group",
1558 .chain = arm_exec_command_handlers
1560 COMMAND_REGISTRATION_DONE
1563 struct target_type riscv_target = {
1566 .init_target = riscv_init_target,
1567 .deinit_target = riscv_deinit_target,
1568 .examine = riscv_examine,
1570 /* poll current target status */
1571 .poll = old_or_new_riscv_poll,
1573 .halt = old_or_new_riscv_halt,
1574 .resume = old_or_new_riscv_resume,
1575 .step = old_or_new_riscv_step,
1577 .assert_reset = riscv_assert_reset,
1578 .deassert_reset = riscv_deassert_reset,
1580 .read_memory = riscv_read_memory,
1581 .write_memory = riscv_write_memory,
1583 .checksum_memory = riscv_checksum_memory,
1585 .get_gdb_reg_list = riscv_get_gdb_reg_list,
1587 .add_breakpoint = riscv_add_breakpoint,
1588 .remove_breakpoint = riscv_remove_breakpoint,
1590 .add_watchpoint = riscv_add_watchpoint,
1591 .remove_watchpoint = riscv_remove_watchpoint,
1593 .arch_state = riscv_arch_state,
1595 .run_algorithm = riscv_run_algorithm,
1597 .commands = riscv_command_handlers
1600 /*** RISC-V Interface ***/
1602 void riscv_info_init(struct target *target, riscv_info_t *r)
1604 memset(r, 0, sizeof(*r));
1606 r->registers_initialized = false;
1607 r->current_hartid = target->coreid;
1609 memset(r->trigger_unique_id, 0xff, sizeof(r->trigger_unique_id));
1611 for (size_t h = 0; h < RISCV_MAX_HARTS; ++h) {
1614 for (size_t e = 0; e < RISCV_MAX_REGISTERS; ++e)
1615 r->valid_saved_registers[h][e] = false;
1619 int riscv_halt_all_harts(struct target *target)
1621 for (int i = 0; i < riscv_count_harts(target); ++i) {
1622 if (!riscv_hart_enabled(target, i))
1625 riscv_halt_one_hart(target, i);
1631 int riscv_halt_one_hart(struct target *target, int hartid)
1634 LOG_DEBUG("halting hart %d", hartid);
1635 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
1637 if (riscv_is_halted(target)) {
1638 LOG_DEBUG(" hart %d requested halt, but was already halted", hartid);
1642 return r->halt_current_hart(target);
1645 int riscv_resume_all_harts(struct target *target)
1647 for (int i = 0; i < riscv_count_harts(target); ++i) {
1648 if (!riscv_hart_enabled(target, i))
1651 riscv_resume_one_hart(target, i);
1654 riscv_invalidate_register_cache(target);
1658 int riscv_resume_one_hart(struct target *target, int hartid)
1661 LOG_DEBUG("resuming hart %d", hartid);
1662 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
1664 if (!riscv_is_halted(target)) {
1665 LOG_DEBUG(" hart %d requested resume, but was already resumed", hartid);
1669 r->on_resume(target);
1670 return r->resume_current_hart(target);
1673 int riscv_step_rtos_hart(struct target *target)
1676 int hartid = r->current_hartid;
1677 if (riscv_rtos_enabled(target)) {
1678 hartid = r->rtos_hartid;
1680 LOG_USER("GDB has asked me to step \"any\" thread, so I'm stepping hart 0.");
1684 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
1686 LOG_DEBUG("stepping hart %d", hartid);
1688 if (!riscv_is_halted(target)) {
1689 LOG_ERROR("Hart isn't halted before single step!");
1692 riscv_invalidate_register_cache(target);
1694 if (r->step_current_hart(target) != ERROR_OK)
1696 riscv_invalidate_register_cache(target);
1698 if (!riscv_is_halted(target)) {
1699 LOG_ERROR("Hart was not halted after single step!");
1705 bool riscv_supports_extension(struct target *target, int hartid, char letter)
1709 if (letter >= 'a' && letter <= 'z')
1711 else if (letter >= 'A' && letter <= 'Z')
1715 return r->misa[hartid] & (1 << num);
1718 int riscv_xlen(const struct target *target)
1720 return riscv_xlen_of_hart(target, riscv_current_hartid(target));
1723 int riscv_xlen_of_hart(const struct target *target, int hartid)
1726 assert(r->xlen[hartid] != -1);
1727 return r->xlen[hartid];
1730 bool riscv_rtos_enabled(const struct target *target)
1732 return target->rtos != NULL;
1735 int riscv_set_current_hartid(struct target *target, int hartid)
1738 if (!r->select_current_hart)
1741 int previous_hartid = riscv_current_hartid(target);
1742 r->current_hartid = hartid;
1743 assert(riscv_hart_enabled(target, hartid));
1744 LOG_DEBUG("setting hartid to %d, was %d", hartid, previous_hartid);
1745 if (r->select_current_hart(target) != ERROR_OK)
1748 /* This might get called during init, in which case we shouldn't be
1749 * setting up the register cache. */
1750 if (!target_was_examined(target))
1753 /* Avoid invalidating the register cache all the time. */
1754 if (r->registers_initialized
1755 && (!riscv_rtos_enabled(target) || (previous_hartid == hartid))
1756 && target->reg_cache->reg_list[GDB_REGNO_ZERO].size == (unsigned)riscv_xlen(target)
1757 && (!riscv_rtos_enabled(target) || (r->rtos_hartid != -1))) {
1760 LOG_DEBUG("Initializing registers: xlen=%d", riscv_xlen(target));
1762 riscv_invalidate_register_cache(target);
1766 void riscv_invalidate_register_cache(struct target *target)
1770 register_cache_invalidate(target->reg_cache);
1771 for (size_t i = 0; i < GDB_REGNO_COUNT; ++i) {
1772 struct reg *reg = &target->reg_cache->reg_list[i];
1776 r->registers_initialized = true;
1779 int riscv_current_hartid(const struct target *target)
1782 return r->current_hartid;
1785 void riscv_set_all_rtos_harts(struct target *target)
1788 r->rtos_hartid = -1;
1791 void riscv_set_rtos_hartid(struct target *target, int hartid)
1793 LOG_DEBUG("setting RTOS hartid %d", hartid);
1795 r->rtos_hartid = hartid;
1798 int riscv_count_harts(struct target *target)
1805 return r->hart_count;
1808 bool riscv_has_register(struct target *target, int hartid, int regid)
1814 * This function is called when the debug user wants to change the value of a
1815 * register. The new value may be cached, and may not be written until the hart
1817 int riscv_set_register(struct target *target, enum gdb_regno r, riscv_reg_t v)
1819 return riscv_set_register_on_hart(target, riscv_current_hartid(target), r, v);
1822 int riscv_set_register_on_hart(struct target *target, int hartid,
1823 enum gdb_regno regid, uint64_t value)
1826 LOG_DEBUG("[%d] %s <- %" PRIx64, hartid, gdb_regno_name(regid), value);
1827 assert(r->set_register);
1828 return r->set_register(target, hartid, regid, value);
1831 int riscv_get_register(struct target *target, riscv_reg_t *value,
1834 return riscv_get_register_on_hart(target, value,
1835 riscv_current_hartid(target), r);
1838 int riscv_get_register_on_hart(struct target *target, riscv_reg_t *value,
1839 int hartid, enum gdb_regno regid)
1842 int result = r->get_register(target, value, hartid, regid);
1843 LOG_DEBUG("[%d] %s: %" PRIx64, hartid, gdb_regno_name(regid), *value);
1847 bool riscv_is_halted(struct target *target)
1850 assert(r->is_halted);
1851 return r->is_halted(target);
1854 enum riscv_halt_reason riscv_halt_reason(struct target *target, int hartid)
1857 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
1858 return RISCV_HALT_ERROR;
1859 if (!riscv_is_halted(target)) {
1860 LOG_ERROR("Hart is not halted!");
1861 return RISCV_HALT_UNKNOWN;
1863 return r->halt_reason(target);
1866 size_t riscv_debug_buffer_size(struct target *target)
1869 return r->debug_buffer_size[riscv_current_hartid(target)];
1872 int riscv_write_debug_buffer(struct target *target, int index, riscv_insn_t insn)
1875 r->write_debug_buffer(target, index, insn);
1879 riscv_insn_t riscv_read_debug_buffer(struct target *target, int index)
1882 return r->read_debug_buffer(target, index);
1885 int riscv_execute_debug_buffer(struct target *target)
1888 return r->execute_debug_buffer(target);
1891 void riscv_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d)
1894 r->fill_dmi_write_u64(target, buf, a, d);
1897 void riscv_fill_dmi_read_u64(struct target *target, char *buf, int a)
1900 r->fill_dmi_read_u64(target, buf, a);
1903 void riscv_fill_dmi_nop_u64(struct target *target, char *buf)
1906 r->fill_dmi_nop_u64(target, buf);
1909 int riscv_dmi_write_u64_bits(struct target *target)
1912 return r->dmi_write_u64_bits(target);
1915 bool riscv_hart_enabled(struct target *target, int hartid)
1917 /* FIXME: Add a hart mask to the RTOS. */
1918 if (riscv_rtos_enabled(target))
1919 return hartid < riscv_count_harts(target);
1921 return hartid == target->coreid;
1925 * Count triggers, and initialize trigger_count for each hart.
1926 * trigger_count is initialized even if this function fails to discover
1928 * Disable any hardware triggers that have dmode set. We can't have set them
1929 * ourselves. Maybe they're left over from some killed debug session.
1931 int riscv_enumerate_triggers(struct target *target)
1935 if (r->triggers_enumerated)
1938 r->triggers_enumerated = true; /* At the very least we tried. */
1940 for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
1941 if (!riscv_hart_enabled(target, hartid))
1944 riscv_reg_t tselect;
1945 int result = riscv_get_register_on_hart(target, &tselect, hartid,
1947 if (result != ERROR_OK)
1950 for (unsigned t = 0; t < RISCV_MAX_TRIGGERS; ++t) {
1951 r->trigger_count[hartid] = t;
1953 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, t);
1954 uint64_t tselect_rb;
1955 result = riscv_get_register_on_hart(target, &tselect_rb, hartid,
1957 if (result != ERROR_OK)
1959 /* Mask off the top bit, which is used as tdrmode in old
1960 * implementations. */
1961 tselect_rb &= ~(1ULL << (riscv_xlen(target)-1));
1962 if (tselect_rb != t)
1965 result = riscv_get_register_on_hart(target, &tdata1, hartid,
1967 if (result != ERROR_OK)
1970 int type = get_field(tdata1, MCONTROL_TYPE(riscv_xlen(target)));
1973 /* On these older cores we don't support software using
1975 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
1978 if (tdata1 & MCONTROL_DMODE(riscv_xlen(target)))
1979 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
1984 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, tselect);
1986 LOG_INFO("[%d] Found %d triggers", hartid, r->trigger_count[hartid]);
1992 const char *gdb_regno_name(enum gdb_regno regno)
1994 static char buf[32];
1997 case GDB_REGNO_ZERO:
2005 case GDB_REGNO_FPR0:
2007 case GDB_REGNO_FPR31:
2009 case GDB_REGNO_CSR0:
2011 case GDB_REGNO_TSELECT:
2013 case GDB_REGNO_TDATA1:
2015 case GDB_REGNO_TDATA2:
2017 case GDB_REGNO_MISA:
2021 case GDB_REGNO_DCSR:
2023 case GDB_REGNO_DSCRATCH:
2025 case GDB_REGNO_MSTATUS:
2027 case GDB_REGNO_PRIV:
2030 if (regno <= GDB_REGNO_XPR31)
2031 sprintf(buf, "x%d", regno - GDB_REGNO_ZERO);
2032 else if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095)
2033 sprintf(buf, "csr%d", regno - GDB_REGNO_CSR0);
2034 else if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31)
2035 sprintf(buf, "f%d", regno - GDB_REGNO_FPR0);
2037 sprintf(buf, "gdb_regno_%d", regno);
2042 static int register_get(struct reg *reg)
2044 struct target *target = (struct target *) reg->arch_info;
2046 int result = riscv_get_register(target, &value, reg->number);
2047 if (result != ERROR_OK)
2049 buf_set_u64(reg->value, 0, reg->size, value);
2053 static int register_set(struct reg *reg, uint8_t *buf)
2055 struct target *target = (struct target *) reg->arch_info;
2057 uint64_t value = buf_get_u64(buf, 0, reg->size);
2059 LOG_DEBUG("write 0x%" PRIx64 " to %s", value, reg->name);
2060 struct reg *r = &target->reg_cache->reg_list[reg->number];
2062 memcpy(r->value, buf, (r->size + 7) / 8);
2064 riscv_set_register(target, reg->number, value);
2068 static struct reg_arch_type riscv_reg_arch_type = {
2069 .get = register_get,
2078 static int cmp_csr_info(const void *p1, const void *p2)
2080 return (int) (((struct csr_info *)p1)->number) - (int) (((struct csr_info *)p2)->number);
2083 int riscv_init_registers(struct target *target)
2087 if (target->reg_cache) {
2088 if (target->reg_cache->reg_list)
2089 free(target->reg_cache->reg_list);
2090 free(target->reg_cache);
2093 target->reg_cache = calloc(1, sizeof(*target->reg_cache));
2094 target->reg_cache->name = "RISC-V Registers";
2095 target->reg_cache->num_regs = GDB_REGNO_COUNT;
2097 target->reg_cache->reg_list = calloc(GDB_REGNO_COUNT, sizeof(struct reg));
2099 const unsigned int max_reg_name_len = 12;
2100 if (info->reg_names)
2101 free(info->reg_names);
2102 info->reg_names = calloc(1, GDB_REGNO_COUNT * max_reg_name_len);
2103 char *reg_name = info->reg_names;
2105 static struct reg_feature feature_cpu = {
2106 .name = "org.gnu.gdb.riscv.cpu"
2108 static struct reg_feature feature_fpu = {
2109 .name = "org.gnu.gdb.riscv.fpu"
2111 static struct reg_feature feature_csr = {
2112 .name = "org.gnu.gdb.riscv.csr"
2114 static struct reg_feature feature_virtual = {
2115 .name = "org.gnu.gdb.riscv.virtual"
2118 static struct reg_data_type type_ieee_single = {
2119 .type = REG_TYPE_IEEE_SINGLE,
2122 static struct reg_data_type type_ieee_double = {
2123 .type = REG_TYPE_IEEE_DOUBLE,
2126 struct csr_info csr_info[] = {
2127 #define DECLARE_CSR(name, number) { number, #name },
2128 #include "encoding.h"
2131 /* encoding.h does not contain the registers in sorted order. */
2132 qsort(csr_info, DIM(csr_info), sizeof(*csr_info), cmp_csr_info);
2133 unsigned csr_info_index = 0;
2135 /* When gdb request register N, gdb_get_register_packet() assumes that this
2136 * is register at index N in reg_list. So if there are certain registers
2137 * that don't exist, we need to leave holes in the list (or renumber, but
2138 * it would be nice not to have yet another set of numbers to translate
2140 for (uint32_t number = 0; number < GDB_REGNO_COUNT; number++) {
2141 struct reg *r = &target->reg_cache->reg_list[number];
2145 r->type = &riscv_reg_arch_type;
2146 r->arch_info = target;
2148 r->size = riscv_xlen(target);
2149 /* r->size is set in riscv_invalidate_register_cache, maybe because the
2150 * target is in theory allowed to change XLEN on us. But I expect a lot
2151 * of other things to break in that case as well. */
2152 if (number <= GDB_REGNO_XPR31) {
2153 r->caller_save = true;
2155 case GDB_REGNO_ZERO:
2252 r->group = "general";
2253 r->feature = &feature_cpu;
2254 } else if (number == GDB_REGNO_PC) {
2255 r->caller_save = true;
2256 sprintf(reg_name, "pc");
2257 r->group = "general";
2258 r->feature = &feature_cpu;
2259 } else if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
2260 r->caller_save = true;
2261 if (riscv_supports_extension(target, riscv_current_hartid(target),
2263 r->reg_data_type = &type_ieee_double;
2265 } else if (riscv_supports_extension(target,
2266 riscv_current_hartid(target), 'F')) {
2267 r->reg_data_type = &type_ieee_single;
2351 case GDB_REGNO_FS10:
2354 case GDB_REGNO_FS11:
2363 case GDB_REGNO_FT10:
2366 case GDB_REGNO_FT11:
2371 r->feature = &feature_fpu;
2372 } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
2374 r->feature = &feature_csr;
2375 unsigned csr_number = number - GDB_REGNO_CSR0;
2377 while (csr_info[csr_info_index].number < csr_number &&
2378 csr_info_index < DIM(csr_info) - 1) {
2381 if (csr_info[csr_info_index].number == csr_number) {
2382 r->name = csr_info[csr_info_index].name;
2384 sprintf(reg_name, "csr%d", csr_number);
2385 /* Assume unnamed registers don't exist, unless we have some
2386 * configuration that tells us otherwise. That's important
2387 * because eg. Eclipse crashes if a target has too many
2388 * registers, and apparently has no way of only showing a
2389 * subset of registers in any case. */
2393 switch (csr_number) {
2397 r->exist = riscv_supports_extension(target,
2398 riscv_current_hartid(target), 'F');
2400 r->feature = &feature_fpu;
2406 case CSR_SCOUNTEREN:
2412 r->exist = riscv_supports_extension(target,
2413 riscv_current_hartid(target), 'S');
2417 /* "In systems with only M-mode, or with both M-mode and
2418 * U-mode but without U-mode trap support, the medeleg and
2419 * mideleg registers should not exist." */
2420 r->exist = riscv_supports_extension(target, riscv_current_hartid(target), 'S') ||
2421 riscv_supports_extension(target, riscv_current_hartid(target), 'N');
2427 case CSR_HPMCOUNTER3H:
2428 case CSR_HPMCOUNTER4H:
2429 case CSR_HPMCOUNTER5H:
2430 case CSR_HPMCOUNTER6H:
2431 case CSR_HPMCOUNTER7H:
2432 case CSR_HPMCOUNTER8H:
2433 case CSR_HPMCOUNTER9H:
2434 case CSR_HPMCOUNTER10H:
2435 case CSR_HPMCOUNTER11H:
2436 case CSR_HPMCOUNTER12H:
2437 case CSR_HPMCOUNTER13H:
2438 case CSR_HPMCOUNTER14H:
2439 case CSR_HPMCOUNTER15H:
2440 case CSR_HPMCOUNTER16H:
2441 case CSR_HPMCOUNTER17H:
2442 case CSR_HPMCOUNTER18H:
2443 case CSR_HPMCOUNTER19H:
2444 case CSR_HPMCOUNTER20H:
2445 case CSR_HPMCOUNTER21H:
2446 case CSR_HPMCOUNTER22H:
2447 case CSR_HPMCOUNTER23H:
2448 case CSR_HPMCOUNTER24H:
2449 case CSR_HPMCOUNTER25H:
2450 case CSR_HPMCOUNTER26H:
2451 case CSR_HPMCOUNTER27H:
2452 case CSR_HPMCOUNTER28H:
2453 case CSR_HPMCOUNTER29H:
2454 case CSR_HPMCOUNTER30H:
2455 case CSR_HPMCOUNTER31H:
2458 case CSR_MHPMCOUNTER3H:
2459 case CSR_MHPMCOUNTER4H:
2460 case CSR_MHPMCOUNTER5H:
2461 case CSR_MHPMCOUNTER6H:
2462 case CSR_MHPMCOUNTER7H:
2463 case CSR_MHPMCOUNTER8H:
2464 case CSR_MHPMCOUNTER9H:
2465 case CSR_MHPMCOUNTER10H:
2466 case CSR_MHPMCOUNTER11H:
2467 case CSR_MHPMCOUNTER12H:
2468 case CSR_MHPMCOUNTER13H:
2469 case CSR_MHPMCOUNTER14H:
2470 case CSR_MHPMCOUNTER15H:
2471 case CSR_MHPMCOUNTER16H:
2472 case CSR_MHPMCOUNTER17H:
2473 case CSR_MHPMCOUNTER18H:
2474 case CSR_MHPMCOUNTER19H:
2475 case CSR_MHPMCOUNTER20H:
2476 case CSR_MHPMCOUNTER21H:
2477 case CSR_MHPMCOUNTER22H:
2478 case CSR_MHPMCOUNTER23H:
2479 case CSR_MHPMCOUNTER24H:
2480 case CSR_MHPMCOUNTER25H:
2481 case CSR_MHPMCOUNTER26H:
2482 case CSR_MHPMCOUNTER27H:
2483 case CSR_MHPMCOUNTER28H:
2484 case CSR_MHPMCOUNTER29H:
2485 case CSR_MHPMCOUNTER30H:
2486 case CSR_MHPMCOUNTER31H:
2487 r->exist = riscv_xlen(target) == 32;
2491 if (!r->exist && expose_csr) {
2492 for (unsigned i = 0; expose_csr[i].low <= expose_csr[i].high; i++) {
2493 if (csr_number >= expose_csr[i].low && csr_number <= expose_csr[i].high) {
2494 LOG_INFO("Exposing additional CSR %d", csr_number);
2501 } else if (number == GDB_REGNO_PRIV) {
2502 sprintf(reg_name, "priv");
2503 r->group = "general";
2504 r->feature = &feature_virtual;
2509 reg_name += strlen(reg_name) + 1;
2510 assert(reg_name < info->reg_names + GDB_REGNO_COUNT * max_reg_name_len);
2511 r->value = &info->reg_cache_values[number];