coding style: open function's brace at beginning of new line
[openocd.git] / src / target / riscv / riscv.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #include "target/target.h"
10 #include "target/algorithm.h"
11 #include "target/target_type.h"
12 #include "log.h"
13 #include "jtag/jtag.h"
14 #include "target/register.h"
15 #include "target/breakpoints.h"
16 #include "helper/time_support.h"
17 #include "riscv.h"
18 #include "gdb_regs.h"
19 #include "rtos/rtos.h"
20
21 /**
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.
26 */
27
28 /**
29 * Code structure
30 *
31 * At the bottom of the stack are the OpenOCD JTAG functions:
32 * jtag_add_[id]r_scan
33 * jtag_execute_query
34 * jtag_add_runtest
35 *
36 * There are a few functions to just instantly shift a register and get its
37 * value:
38 * dtmcontrol_scan
39 * idcode_scan
40 * dbus_scan
41 *
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:
45 * scans_new
46 * scans_delete
47 * scans_execute
48 * scans_add_...
49 * Usually you new(), call a bunch of add functions, then execute() and look
50 * at the results by calling scans_get...()
51 *
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
54 * functions:
55 * cache_get...
56 * cache_set...
57 * cache_write
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.
61 */
62
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)))
65
66 #define DIM(x) (sizeof(x)/sizeof(*x))
67
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)
78
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
83
84 #define SETHALTNOT 0x10c
85
86 /*** JTAG registers. ***/
87
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)
93
94 #define DBUS 0x11
95 #define DBUS_OP_START 0
96 #define DBUS_OP_SIZE 2
97 typedef enum {
98 DBUS_OP_NOP = 0,
99 DBUS_OP_READ = 1,
100 DBUS_OP_WRITE = 2
101 } dbus_op_t;
102 typedef enum {
103 DBUS_STATUS_SUCCESS = 0,
104 DBUS_STATUS_FAILED = 2,
105 DBUS_STATUS_BUSY = 3
106 } dbus_status_t;
107 #define DBUS_DATA_START 2
108 #define DBUS_DATA_SIZE 34
109 #define DBUS_ADDRESS_START 36
110
111 typedef enum {
112 RE_OK,
113 RE_FAIL,
114 RE_AGAIN
115 } riscv_error_t;
116
117 typedef enum slot {
118 SLOT0,
119 SLOT1,
120 SLOT_LAST,
121 } slot_t;
122
123 /*** Debug Bus registers. ***/
124
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
135
136 #define DMINFO 0x11
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
149
150 /*** Info about the core being debugged. ***/
151
152 #define DBUS_ADDRESS_UNKNOWN 0xffff
153
154 #define MAX_HWBPS 16
155 #define DRAM_CACHE_SIZE 16
156
157 uint8_t ir_dtmcontrol[4] = {DTMCONTROL};
158 struct scan_field select_dtmcontrol = {
159 .in_value = NULL,
160 .out_value = ir_dtmcontrol
161 };
162 uint8_t ir_dbus[4] = {DBUS};
163 struct scan_field select_dbus = {
164 .in_value = NULL,
165 .out_value = ir_dbus
166 };
167 uint8_t ir_idcode[4] = {0x1};
168 struct scan_field select_idcode = {
169 .in_value = NULL,
170 .out_value = ir_idcode
171 };
172
173 struct trigger {
174 uint64_t address;
175 uint32_t length;
176 uint64_t mask;
177 uint64_t value;
178 bool read, write, execute;
179 int unique_id;
180 };
181
182 /* Wall-clock timeout for a command/access. Settable via RISC-V Target commands.*/
183 int riscv_command_timeout_sec = DEFAULT_COMMAND_TIMEOUT_SEC;
184
185 /* Wall-clock timeout after reset. Settable via RISC-V Target commands.*/
186 int riscv_reset_timeout_sec = DEFAULT_RESET_TIMEOUT_SEC;
187
188 bool riscv_prefer_sba;
189
190 typedef struct {
191 uint16_t low, high;
192 } range_t;
193
194 /* In addition to the ones in the standard spec, we'll also expose additional
195 * CSRs in this list.
196 * The list is either NULL, or a series of ranges (inclusive), terminated with
197 * 1,0. */
198 range_t *expose_csr;
199 /* Same, but for custom registers. */
200 range_t *expose_custom;
201
202 static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
203 {
204 struct scan_field field;
205 uint8_t in_value[4];
206 uint8_t out_value[4] = { 0 };
207
208 buf_set_u32(out_value, 0, 32, out);
209
210 jtag_add_ir_scan(target->tap, &select_dtmcontrol, TAP_IDLE);
211
212 field.num_bits = 32;
213 field.out_value = out_value;
214 field.in_value = in_value;
215 jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
216
217 /* Always return to dbus. */
218 jtag_add_ir_scan(target->tap, &select_dbus, TAP_IDLE);
219
220 int retval = jtag_execute_queue();
221 if (retval != ERROR_OK) {
222 LOG_ERROR("failed jtag scan: %d", retval);
223 return retval;
224 }
225
226 uint32_t in = buf_get_u32(field.in_value, 0, 32);
227 LOG_DEBUG("DTMCONTROL: 0x%x -> 0x%x", out, in);
228
229 return in;
230 }
231
232 static struct target_type *get_target_type(struct target *target)
233 {
234 riscv_info_t *info = (riscv_info_t *) target->arch_info;
235
236 if (!info) {
237 LOG_ERROR("Target has not been initialized");
238 return NULL;
239 }
240
241 switch (info->dtm_version) {
242 case 0:
243 return &riscv011_target;
244 case 1:
245 return &riscv013_target;
246 default:
247 LOG_ERROR("Unsupported DTM version: %d", info->dtm_version);
248 return NULL;
249 }
250 }
251
252 static int riscv_init_target(struct command_context *cmd_ctx,
253 struct target *target)
254 {
255 LOG_DEBUG("riscv_init_target()");
256 target->arch_info = calloc(1, sizeof(riscv_info_t));
257 if (!target->arch_info)
258 return ERROR_FAIL;
259 riscv_info_t *info = (riscv_info_t *) target->arch_info;
260 riscv_info_init(target, info);
261 info->cmd_ctx = cmd_ctx;
262
263 select_dtmcontrol.num_bits = target->tap->ir_length;
264 select_dbus.num_bits = target->tap->ir_length;
265 select_idcode.num_bits = target->tap->ir_length;
266
267 riscv_semihosting_init(target);
268
269 target->debug_reason = DBG_REASON_DBGRQ;
270
271 return ERROR_OK;
272 }
273
274 static void riscv_free_registers(struct target *target)
275 {
276 /* Free the shared structure use for most registers. */
277 if (target->reg_cache) {
278 if (target->reg_cache->reg_list) {
279 if (target->reg_cache->reg_list[0].arch_info)
280 free(target->reg_cache->reg_list[0].arch_info);
281 /* Free the ones we allocated separately. */
282 for (unsigned i = GDB_REGNO_COUNT; i < target->reg_cache->num_regs; i++)
283 free(target->reg_cache->reg_list[i].arch_info);
284 free(target->reg_cache->reg_list);
285 }
286 free(target->reg_cache);
287 }
288 }
289
290 static void riscv_deinit_target(struct target *target)
291 {
292 LOG_DEBUG("riscv_deinit_target()");
293 struct target_type *tt = get_target_type(target);
294 if (tt) {
295 tt->deinit_target(target);
296 riscv_info_t *info = (riscv_info_t *) target->arch_info;
297 free(info->reg_names);
298 free(info);
299 }
300
301 riscv_free_registers(target);
302
303 target->arch_info = NULL;
304 }
305
306 static int oldriscv_halt(struct target *target)
307 {
308 struct target_type *tt = get_target_type(target);
309 return tt->halt(target);
310 }
311
312 static void trigger_from_breakpoint(struct trigger *trigger,
313 const struct breakpoint *breakpoint)
314 {
315 trigger->address = breakpoint->address;
316 trigger->length = breakpoint->length;
317 trigger->mask = ~0LL;
318 trigger->read = false;
319 trigger->write = false;
320 trigger->execute = true;
321 /* unique_id is unique across both breakpoints and watchpoints. */
322 trigger->unique_id = breakpoint->unique_id;
323 }
324
325 static int maybe_add_trigger_t1(struct target *target, unsigned hartid,
326 struct trigger *trigger, uint64_t tdata1)
327 {
328 RISCV_INFO(r);
329
330 const uint32_t bpcontrol_x = 1<<0;
331 const uint32_t bpcontrol_w = 1<<1;
332 const uint32_t bpcontrol_r = 1<<2;
333 const uint32_t bpcontrol_u = 1<<3;
334 const uint32_t bpcontrol_s = 1<<4;
335 const uint32_t bpcontrol_h = 1<<5;
336 const uint32_t bpcontrol_m = 1<<6;
337 const uint32_t bpcontrol_bpmatch = 0xf << 7;
338 const uint32_t bpcontrol_bpaction = 0xff << 11;
339
340 if (tdata1 & (bpcontrol_r | bpcontrol_w | bpcontrol_x)) {
341 /* Trigger is already in use, presumably by user code. */
342 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
343 }
344
345 tdata1 = set_field(tdata1, bpcontrol_r, trigger->read);
346 tdata1 = set_field(tdata1, bpcontrol_w, trigger->write);
347 tdata1 = set_field(tdata1, bpcontrol_x, trigger->execute);
348 tdata1 = set_field(tdata1, bpcontrol_u,
349 !!(r->misa[hartid] & (1 << ('U' - 'A'))));
350 tdata1 = set_field(tdata1, bpcontrol_s,
351 !!(r->misa[hartid] & (1 << ('S' - 'A'))));
352 tdata1 = set_field(tdata1, bpcontrol_h,
353 !!(r->misa[hartid] & (1 << ('H' - 'A'))));
354 tdata1 |= bpcontrol_m;
355 tdata1 = set_field(tdata1, bpcontrol_bpmatch, 0); /* exact match */
356 tdata1 = set_field(tdata1, bpcontrol_bpaction, 0); /* cause bp exception */
357
358 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, tdata1);
359
360 riscv_reg_t tdata1_rb;
361 if (riscv_get_register_on_hart(target, &tdata1_rb, hartid,
362 GDB_REGNO_TDATA1) != ERROR_OK)
363 return ERROR_FAIL;
364 LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
365
366 if (tdata1 != tdata1_rb) {
367 LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
368 PRIx64 " to tdata1 it contains 0x%" PRIx64,
369 tdata1, tdata1_rb);
370 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
371 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
372 }
373
374 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA2, trigger->address);
375
376 return ERROR_OK;
377 }
378
379 static int maybe_add_trigger_t2(struct target *target, unsigned hartid,
380 struct trigger *trigger, uint64_t tdata1)
381 {
382 RISCV_INFO(r);
383
384 /* tselect is already set */
385 if (tdata1 & (MCONTROL_EXECUTE | MCONTROL_STORE | MCONTROL_LOAD)) {
386 /* Trigger is already in use, presumably by user code. */
387 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
388 }
389
390 /* address/data match trigger */
391 tdata1 |= MCONTROL_DMODE(riscv_xlen(target));
392 tdata1 = set_field(tdata1, MCONTROL_ACTION,
393 MCONTROL_ACTION_DEBUG_MODE);
394 tdata1 = set_field(tdata1, MCONTROL_MATCH, MCONTROL_MATCH_EQUAL);
395 tdata1 |= MCONTROL_M;
396 if (r->misa[hartid] & (1 << ('H' - 'A')))
397 tdata1 |= MCONTROL_H;
398 if (r->misa[hartid] & (1 << ('S' - 'A')))
399 tdata1 |= MCONTROL_S;
400 if (r->misa[hartid] & (1 << ('U' - 'A')))
401 tdata1 |= MCONTROL_U;
402
403 if (trigger->execute)
404 tdata1 |= MCONTROL_EXECUTE;
405 if (trigger->read)
406 tdata1 |= MCONTROL_LOAD;
407 if (trigger->write)
408 tdata1 |= MCONTROL_STORE;
409
410 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, tdata1);
411
412 uint64_t tdata1_rb;
413 int result = riscv_get_register_on_hart(target, &tdata1_rb, hartid, GDB_REGNO_TDATA1);
414 if (result != ERROR_OK)
415 return result;
416 LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
417
418 if (tdata1 != tdata1_rb) {
419 LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
420 PRIx64 " to tdata1 it contains 0x%" PRIx64,
421 tdata1, tdata1_rb);
422 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
423 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
424 }
425
426 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA2, trigger->address);
427
428 return ERROR_OK;
429 }
430
431 static int add_trigger(struct target *target, struct trigger *trigger)
432 {
433 RISCV_INFO(r);
434
435 if (riscv_enumerate_triggers(target) != ERROR_OK)
436 return ERROR_FAIL;
437
438 /* In RTOS mode, we need to set the same trigger in the same slot on every
439 * hart, to keep up the illusion that each hart is a thread running on the
440 * same core. */
441
442 /* Otherwise, we just set the trigger on the one hart this target deals
443 * with. */
444
445 riscv_reg_t tselect[RISCV_MAX_HARTS];
446
447 int first_hart = -1;
448 for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
449 if (!riscv_hart_enabled(target, hartid))
450 continue;
451 if (first_hart < 0)
452 first_hart = hartid;
453 int result = riscv_get_register_on_hart(target, &tselect[hartid],
454 hartid, GDB_REGNO_TSELECT);
455 if (result != ERROR_OK)
456 return result;
457 }
458 assert(first_hart >= 0);
459
460 unsigned int i;
461 for (i = 0; i < r->trigger_count[first_hart]; i++) {
462 if (r->trigger_unique_id[i] != -1)
463 continue;
464
465 riscv_set_register_on_hart(target, first_hart, GDB_REGNO_TSELECT, i);
466
467 uint64_t tdata1;
468 int result = riscv_get_register_on_hart(target, &tdata1, first_hart,
469 GDB_REGNO_TDATA1);
470 if (result != ERROR_OK)
471 return result;
472 int type = get_field(tdata1, MCONTROL_TYPE(riscv_xlen(target)));
473
474 result = ERROR_OK;
475 for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
476 if (!riscv_hart_enabled(target, hartid))
477 continue;
478 if (hartid > first_hart)
479 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, i);
480 switch (type) {
481 case 1:
482 result = maybe_add_trigger_t1(target, hartid, trigger, tdata1);
483 break;
484 case 2:
485 result = maybe_add_trigger_t2(target, hartid, trigger, tdata1);
486 break;
487 default:
488 LOG_DEBUG("trigger %d has unknown type %d", i, type);
489 continue;
490 }
491
492 if (result != ERROR_OK)
493 continue;
494 }
495
496 if (result != ERROR_OK)
497 continue;
498
499 LOG_DEBUG("[%d] Using trigger %d (type %d) for bp %d", target->coreid,
500 i, type, trigger->unique_id);
501 r->trigger_unique_id[i] = trigger->unique_id;
502 break;
503 }
504
505 for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
506 if (!riscv_hart_enabled(target, hartid))
507 continue;
508 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT,
509 tselect[hartid]);
510 }
511
512 if (i >= r->trigger_count[first_hart]) {
513 LOG_ERROR("Couldn't find an available hardware trigger.");
514 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
515 }
516
517 return ERROR_OK;
518 }
519
520 int riscv_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
521 {
522 LOG_DEBUG("[%d] @0x%" TARGET_PRIxADDR, target->coreid, breakpoint->address);
523 assert(breakpoint);
524 if (breakpoint->type == BKPT_SOFT) {
525 /** @todo check RVC for size/alignment */
526 if (!(breakpoint->length == 4 || breakpoint->length == 2)) {
527 LOG_ERROR("Invalid breakpoint length %d", breakpoint->length);
528 return ERROR_FAIL;
529 }
530
531 if (0 != (breakpoint->address % 2)) {
532 LOG_ERROR("Invalid breakpoint alignment for address 0x%" TARGET_PRIxADDR, breakpoint->address);
533 return ERROR_FAIL;
534 }
535
536 if (target_read_memory(target, breakpoint->address, 2, breakpoint->length / 2,
537 breakpoint->orig_instr) != ERROR_OK) {
538 LOG_ERROR("Failed to read original instruction at 0x%" TARGET_PRIxADDR,
539 breakpoint->address);
540 return ERROR_FAIL;
541 }
542
543 uint8_t buff[4] = { 0 };
544 buf_set_u32(buff, 0, breakpoint->length * CHAR_BIT, breakpoint->length == 4 ? ebreak() : ebreak_c());
545 int const retval = target_write_memory(target, breakpoint->address, 2, breakpoint->length / 2, buff);
546
547 if (retval != ERROR_OK) {
548 LOG_ERROR("Failed to write %d-byte breakpoint instruction at 0x%"
549 TARGET_PRIxADDR, breakpoint->length, breakpoint->address);
550 return ERROR_FAIL;
551 }
552
553 } else if (breakpoint->type == BKPT_HARD) {
554 struct trigger trigger;
555 trigger_from_breakpoint(&trigger, breakpoint);
556 int const result = add_trigger(target, &trigger);
557 if (result != ERROR_OK)
558 return result;
559 } else {
560 LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
561 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
562 }
563
564 breakpoint->set = true;
565 return ERROR_OK;
566 }
567
568 static int remove_trigger(struct target *target, struct trigger *trigger)
569 {
570 RISCV_INFO(r);
571
572 if (riscv_enumerate_triggers(target) != ERROR_OK)
573 return ERROR_FAIL;
574
575 int first_hart = -1;
576 for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
577 if (!riscv_hart_enabled(target, hartid))
578 continue;
579 if (first_hart < 0) {
580 first_hart = hartid;
581 break;
582 }
583 }
584 assert(first_hart >= 0);
585
586 unsigned int i;
587 for (i = 0; i < r->trigger_count[first_hart]; i++) {
588 if (r->trigger_unique_id[i] == trigger->unique_id)
589 break;
590 }
591 if (i >= r->trigger_count[first_hart]) {
592 LOG_ERROR("Couldn't find the hardware resources used by hardware "
593 "trigger.");
594 return ERROR_FAIL;
595 }
596 LOG_DEBUG("[%d] Stop using resource %d for bp %d", target->coreid, i,
597 trigger->unique_id);
598 for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
599 if (!riscv_hart_enabled(target, hartid))
600 continue;
601 riscv_reg_t tselect;
602 int result = riscv_get_register_on_hart(target, &tselect, hartid, GDB_REGNO_TSELECT);
603 if (result != ERROR_OK)
604 return result;
605 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, i);
606 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
607 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, tselect);
608 }
609 r->trigger_unique_id[i] = -1;
610
611 return ERROR_OK;
612 }
613
614 int riscv_remove_breakpoint(struct target *target,
615 struct breakpoint *breakpoint)
616 {
617 if (breakpoint->type == BKPT_SOFT) {
618 if (target_write_memory(target, breakpoint->address, 2, breakpoint->length / 2,
619 breakpoint->orig_instr) != ERROR_OK) {
620 LOG_ERROR("Failed to restore instruction for %d-byte breakpoint at "
621 "0x%" TARGET_PRIxADDR, breakpoint->length, breakpoint->address);
622 return ERROR_FAIL;
623 }
624
625 } else if (breakpoint->type == BKPT_HARD) {
626 struct trigger trigger;
627 trigger_from_breakpoint(&trigger, breakpoint);
628 int result = remove_trigger(target, &trigger);
629 if (result != ERROR_OK)
630 return result;
631
632 } else {
633 LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
634 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
635 }
636
637 breakpoint->set = false;
638
639 return ERROR_OK;
640 }
641
642 static void trigger_from_watchpoint(struct trigger *trigger,
643 const struct watchpoint *watchpoint)
644 {
645 trigger->address = watchpoint->address;
646 trigger->length = watchpoint->length;
647 trigger->mask = watchpoint->mask;
648 trigger->value = watchpoint->value;
649 trigger->read = (watchpoint->rw == WPT_READ || watchpoint->rw == WPT_ACCESS);
650 trigger->write = (watchpoint->rw == WPT_WRITE || watchpoint->rw == WPT_ACCESS);
651 trigger->execute = false;
652 /* unique_id is unique across both breakpoints and watchpoints. */
653 trigger->unique_id = watchpoint->unique_id;
654 }
655
656 int riscv_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
657 {
658 struct trigger trigger;
659 trigger_from_watchpoint(&trigger, watchpoint);
660
661 int result = add_trigger(target, &trigger);
662 if (result != ERROR_OK)
663 return result;
664 watchpoint->set = true;
665
666 return ERROR_OK;
667 }
668
669 int riscv_remove_watchpoint(struct target *target,
670 struct watchpoint *watchpoint)
671 {
672 LOG_DEBUG("[%d] @0x%" TARGET_PRIxADDR, target->coreid, watchpoint->address);
673
674 struct trigger trigger;
675 trigger_from_watchpoint(&trigger, watchpoint);
676
677 int result = remove_trigger(target, &trigger);
678 if (result != ERROR_OK)
679 return result;
680 watchpoint->set = false;
681
682 return ERROR_OK;
683 }
684
685 /* Sets *hit_watchpoint to the first watchpoint identified as causing the
686 * current halt.
687 *
688 * The GDB server uses this information to tell GDB what data address has
689 * been hit, which enables GDB to print the hit variable along with its old
690 * and new value. */
691 int riscv_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
692 {
693 struct watchpoint *wp = target->watchpoints;
694
695 LOG_DEBUG("Current hartid = %d", riscv_current_hartid(target));
696
697 /*TODO instead of disassembling the instruction that we think caused the
698 * trigger, check the hit bit of each watchpoint first. The hit bit is
699 * simpler and more reliable to check but as it is optional and relatively
700 * new, not all hardware will implement it */
701 riscv_reg_t dpc;
702 riscv_get_register(target, &dpc, GDB_REGNO_DPC);
703 const uint8_t length = 4;
704 LOG_DEBUG("dpc is 0x%" PRIx64, dpc);
705
706 /* fetch the instruction at dpc */
707 uint8_t buffer[length];
708 if (target_read_buffer(target, dpc, length, buffer) != ERROR_OK) {
709 LOG_ERROR("Failed to read instruction at dpc 0x%" PRIx64, dpc);
710 return ERROR_FAIL;
711 }
712
713 uint32_t instruction = 0;
714
715 for (int i = 0; i < length; i++) {
716 LOG_DEBUG("Next byte is %x", buffer[i]);
717 instruction += (buffer[i] << 8 * i);
718 }
719 LOG_DEBUG("Full instruction is %x", instruction);
720
721 /* find out which memory address is accessed by the instruction at dpc */
722 /* opcode is first 7 bits of the instruction */
723 uint8_t opcode = instruction & 0x7F;
724 uint32_t rs1;
725 int16_t imm;
726 riscv_reg_t mem_addr;
727
728 if (opcode == MATCH_LB || opcode == MATCH_SB) {
729 rs1 = (instruction & 0xf8000) >> 15;
730 riscv_get_register(target, &mem_addr, rs1);
731
732 if (opcode == MATCH_SB) {
733 LOG_DEBUG("%x is store instruction", instruction);
734 imm = ((instruction & 0xf80) >> 7) | ((instruction & 0xfe000000) >> 20);
735 } else {
736 LOG_DEBUG("%x is load instruction", instruction);
737 imm = (instruction & 0xfff00000) >> 20;
738 }
739 /* sign extend 12-bit imm to 16-bits */
740 if (imm & (1 << 11))
741 imm |= 0xf000;
742 mem_addr += imm;
743 LOG_DEBUG("memory address=0x%" PRIx64, mem_addr);
744 } else {
745 LOG_DEBUG("%x is not a RV32I load or store", instruction);
746 return ERROR_FAIL;
747 }
748
749 while (wp) {
750 /*TODO support length/mask */
751 if (wp->address == mem_addr) {
752 *hit_watchpoint = wp;
753 LOG_DEBUG("Hit address=%" TARGET_PRIxADDR, wp->address);
754 return ERROR_OK;
755 }
756 wp = wp->next;
757 }
758
759 /* No match found - either we hit a watchpoint caused by an instruction that
760 * this function does not yet disassemble, or we hit a breakpoint.
761 *
762 * OpenOCD will behave as if this function had never been implemented i.e.
763 * report the halt to GDB with no address information. */
764 return ERROR_FAIL;
765 }
766
767
768 static int oldriscv_step(struct target *target, int current, uint32_t address,
769 int handle_breakpoints)
770 {
771 struct target_type *tt = get_target_type(target);
772 return tt->step(target, current, address, handle_breakpoints);
773 }
774
775 static int old_or_new_riscv_step(struct target *target, int current,
776 target_addr_t address, int handle_breakpoints)
777 {
778 RISCV_INFO(r);
779 LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints);
780 if (r->is_halted == NULL)
781 return oldriscv_step(target, current, address, handle_breakpoints);
782 else
783 return riscv_openocd_step(target, current, address, handle_breakpoints);
784 }
785
786
787 static int riscv_examine(struct target *target)
788 {
789 LOG_DEBUG("riscv_examine()");
790 if (target_was_examined(target)) {
791 LOG_DEBUG("Target was already examined.");
792 return ERROR_OK;
793 }
794
795 /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
796
797 riscv_info_t *info = (riscv_info_t *) target->arch_info;
798 uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
799 LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol);
800 info->dtm_version = get_field(dtmcontrol, DTMCONTROL_VERSION);
801 LOG_DEBUG(" version=0x%x", info->dtm_version);
802
803 struct target_type *tt = get_target_type(target);
804 if (tt == NULL)
805 return ERROR_FAIL;
806
807 int result = tt->init_target(info->cmd_ctx, target);
808 if (result != ERROR_OK)
809 return result;
810
811 return tt->examine(target);
812 }
813
814 static int oldriscv_poll(struct target *target)
815 {
816 struct target_type *tt = get_target_type(target);
817 return tt->poll(target);
818 }
819
820 static int old_or_new_riscv_poll(struct target *target)
821 {
822 RISCV_INFO(r);
823 if (r->is_halted == NULL)
824 return oldriscv_poll(target);
825 else
826 return riscv_openocd_poll(target);
827 }
828
829 static int old_or_new_riscv_halt(struct target *target)
830 {
831 RISCV_INFO(r);
832 if (r->is_halted == NULL)
833 return oldriscv_halt(target);
834 else
835 return riscv_openocd_halt(target);
836 }
837
838 static int riscv_assert_reset(struct target *target)
839 {
840 LOG_DEBUG("[%d]", target->coreid);
841 struct target_type *tt = get_target_type(target);
842 riscv_invalidate_register_cache(target);
843 return tt->assert_reset(target);
844 }
845
846 static int riscv_deassert_reset(struct target *target)
847 {
848 LOG_DEBUG("[%d]", target->coreid);
849 struct target_type *tt = get_target_type(target);
850 return tt->deassert_reset(target);
851 }
852
853
854 static int oldriscv_resume(struct target *target, int current, uint32_t address,
855 int handle_breakpoints, int debug_execution)
856 {
857 struct target_type *tt = get_target_type(target);
858 return tt->resume(target, current, address, handle_breakpoints,
859 debug_execution);
860 }
861
862 static int old_or_new_riscv_resume(struct target *target, int current,
863 target_addr_t address, int handle_breakpoints, int debug_execution)
864 {
865 LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints);
866 if (target->smp) {
867 struct target_list *targets = target->head;
868 int result = ERROR_OK;
869 while (targets) {
870 struct target *t = targets->target;
871 riscv_info_t *r = riscv_info(t);
872 if (r->is_halted == NULL) {
873 if (oldriscv_resume(t, current, address, handle_breakpoints,
874 debug_execution) != ERROR_OK)
875 result = ERROR_FAIL;
876 } else {
877 if (riscv_openocd_resume(t, current, address,
878 handle_breakpoints, debug_execution) != ERROR_OK)
879 result = ERROR_FAIL;
880 }
881 targets = targets->next;
882 }
883 return result;
884 }
885
886 RISCV_INFO(r);
887 if (r->is_halted == NULL)
888 return oldriscv_resume(target, current, address, handle_breakpoints, debug_execution);
889 else
890 return riscv_openocd_resume(target, current, address, handle_breakpoints, debug_execution);
891 }
892
893 static int riscv_select_current_hart(struct target *target)
894 {
895 RISCV_INFO(r);
896 if (riscv_rtos_enabled(target)) {
897 if (r->rtos_hartid == -1)
898 r->rtos_hartid = target->rtos->current_threadid - 1;
899 return riscv_set_current_hartid(target, r->rtos_hartid);
900 } else
901 return riscv_set_current_hartid(target, target->coreid);
902 }
903
904 static int riscv_read_memory(struct target *target, target_addr_t address,
905 uint32_t size, uint32_t count, uint8_t *buffer)
906 {
907 if (riscv_select_current_hart(target) != ERROR_OK)
908 return ERROR_FAIL;
909 struct target_type *tt = get_target_type(target);
910 return tt->read_memory(target, address, size, count, buffer);
911 }
912
913 static int riscv_write_memory(struct target *target, target_addr_t address,
914 uint32_t size, uint32_t count, const uint8_t *buffer)
915 {
916 if (riscv_select_current_hart(target) != ERROR_OK)
917 return ERROR_FAIL;
918 struct target_type *tt = get_target_type(target);
919 return tt->write_memory(target, address, size, count, buffer);
920 }
921
922 static int riscv_get_gdb_reg_list_internal(struct target *target,
923 struct reg **reg_list[], int *reg_list_size,
924 enum target_register_class reg_class, bool read)
925 {
926 RISCV_INFO(r);
927 LOG_DEBUG("rtos_hartid=%d, current_hartid=%d, reg_class=%d, read=%d",
928 r->rtos_hartid, r->current_hartid, reg_class, read);
929
930 if (!target->reg_cache) {
931 LOG_ERROR("Target not initialized. Return ERROR_FAIL.");
932 return ERROR_FAIL;
933 }
934
935 if (riscv_select_current_hart(target) != ERROR_OK)
936 return ERROR_FAIL;
937
938 switch (reg_class) {
939 case REG_CLASS_GENERAL:
940 *reg_list_size = 33;
941 break;
942 case REG_CLASS_ALL:
943 *reg_list_size = target->reg_cache->num_regs;
944 break;
945 default:
946 LOG_ERROR("Unsupported reg_class: %d", reg_class);
947 return ERROR_FAIL;
948 }
949
950 *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
951 if (!*reg_list)
952 return ERROR_FAIL;
953
954 for (int i = 0; i < *reg_list_size; i++) {
955 assert(!target->reg_cache->reg_list[i].valid ||
956 target->reg_cache->reg_list[i].size > 0);
957 (*reg_list)[i] = &target->reg_cache->reg_list[i];
958 if (read && !target->reg_cache->reg_list[i].valid) {
959 if (target->reg_cache->reg_list[i].type->get(
960 &target->reg_cache->reg_list[i]) != ERROR_OK)
961 /* This function is called when first connecting to gdb,
962 * resulting in an attempt to read all kinds of registers which
963 * probably will fail. Ignore these failures, and when
964 * encountered stop reading to save time. */
965 read = false;
966 }
967 }
968
969 return ERROR_OK;
970 }
971
972 static int riscv_get_gdb_reg_list(struct target *target,
973 struct reg **reg_list[], int *reg_list_size,
974 enum target_register_class reg_class)
975 {
976 return riscv_get_gdb_reg_list_internal(target, reg_list, reg_list_size,
977 reg_class, true);
978 }
979
980 static int riscv_arch_state(struct target *target)
981 {
982 struct target_type *tt = get_target_type(target);
983 return tt->arch_state(target);
984 }
985
986 /* Algorithm must end with a software breakpoint instruction. */
987 static int riscv_run_algorithm(struct target *target, int num_mem_params,
988 struct mem_param *mem_params, int num_reg_params,
989 struct reg_param *reg_params, target_addr_t entry_point,
990 target_addr_t exit_point, int timeout_ms, void *arch_info)
991 {
992 riscv_info_t *info = (riscv_info_t *) target->arch_info;
993
994 if (num_mem_params > 0) {
995 LOG_ERROR("Memory parameters are not supported for RISC-V algorithms.");
996 return ERROR_FAIL;
997 }
998
999 if (target->state != TARGET_HALTED) {
1000 LOG_WARNING("target not halted");
1001 return ERROR_TARGET_NOT_HALTED;
1002 }
1003
1004 /* Save registers */
1005 struct reg *reg_pc = register_get_by_name(target->reg_cache, "pc", 1);
1006 if (!reg_pc || reg_pc->type->get(reg_pc) != ERROR_OK)
1007 return ERROR_FAIL;
1008 uint64_t saved_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
1009
1010 uint64_t saved_regs[32];
1011 for (int i = 0; i < num_reg_params; i++) {
1012 if (reg_params[i].direction == PARAM_IN)
1013 continue;
1014
1015 LOG_DEBUG("save %s", reg_params[i].reg_name);
1016 struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, 0);
1017 if (!r) {
1018 LOG_ERROR("Couldn't find register named '%s'", reg_params[i].reg_name);
1019 return ERROR_FAIL;
1020 }
1021
1022 if (r->size != reg_params[i].size) {
1023 LOG_ERROR("Register %s is %d bits instead of %d bits.",
1024 reg_params[i].reg_name, r->size, reg_params[i].size);
1025 return ERROR_FAIL;
1026 }
1027
1028 if (r->number > GDB_REGNO_XPR31) {
1029 LOG_ERROR("Only GPRs can be use as argument registers.");
1030 return ERROR_FAIL;
1031 }
1032
1033 if (r->type->get(r) != ERROR_OK)
1034 return ERROR_FAIL;
1035 saved_regs[r->number] = buf_get_u64(r->value, 0, r->size);
1036 if (r->type->set(r, reg_params[i].value) != ERROR_OK)
1037 return ERROR_FAIL;
1038 }
1039
1040
1041 /* Disable Interrupts before attempting to run the algorithm. */
1042 uint64_t current_mstatus;
1043 uint8_t mstatus_bytes[8] = { 0 };
1044
1045 LOG_DEBUG("Disabling Interrupts");
1046 struct reg *reg_mstatus = register_get_by_name(target->reg_cache,
1047 "mstatus", 1);
1048 if (!reg_mstatus) {
1049 LOG_ERROR("Couldn't find mstatus!");
1050 return ERROR_FAIL;
1051 }
1052
1053 reg_mstatus->type->get(reg_mstatus);
1054 current_mstatus = buf_get_u64(reg_mstatus->value, 0, reg_mstatus->size);
1055 uint64_t ie_mask = MSTATUS_MIE | MSTATUS_HIE | MSTATUS_SIE | MSTATUS_UIE;
1056 buf_set_u64(mstatus_bytes, 0, info->xlen[0], set_field(current_mstatus,
1057 ie_mask, 0));
1058
1059 reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
1060
1061 /* Run algorithm */
1062 LOG_DEBUG("resume at 0x%" TARGET_PRIxADDR, entry_point);
1063 if (oldriscv_resume(target, 0, entry_point, 0, 0) != ERROR_OK)
1064 return ERROR_FAIL;
1065
1066 int64_t start = timeval_ms();
1067 while (target->state != TARGET_HALTED) {
1068 LOG_DEBUG("poll()");
1069 int64_t now = timeval_ms();
1070 if (now - start > timeout_ms) {
1071 LOG_ERROR("Algorithm timed out after %d ms.", timeout_ms);
1072 LOG_ERROR(" now = 0x%08x", (uint32_t) now);
1073 LOG_ERROR(" start = 0x%08x", (uint32_t) start);
1074 oldriscv_halt(target);
1075 old_or_new_riscv_poll(target);
1076 return ERROR_TARGET_TIMEOUT;
1077 }
1078
1079 int result = old_or_new_riscv_poll(target);
1080 if (result != ERROR_OK)
1081 return result;
1082 }
1083
1084 if (reg_pc->type->get(reg_pc) != ERROR_OK)
1085 return ERROR_FAIL;
1086 uint64_t final_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
1087 if (final_pc != exit_point) {
1088 LOG_ERROR("PC ended up at 0x%" PRIx64 " instead of 0x%"
1089 TARGET_PRIxADDR, final_pc, exit_point);
1090 return ERROR_FAIL;
1091 }
1092
1093 /* Restore Interrupts */
1094 LOG_DEBUG("Restoring Interrupts");
1095 buf_set_u64(mstatus_bytes, 0, info->xlen[0], current_mstatus);
1096 reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
1097
1098 /* Restore registers */
1099 uint8_t buf[8] = { 0 };
1100 buf_set_u64(buf, 0, info->xlen[0], saved_pc);
1101 if (reg_pc->type->set(reg_pc, buf) != ERROR_OK)
1102 return ERROR_FAIL;
1103
1104 for (int i = 0; i < num_reg_params; i++) {
1105 LOG_DEBUG("restore %s", reg_params[i].reg_name);
1106 struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, 0);
1107 buf_set_u64(buf, 0, info->xlen[0], saved_regs[r->number]);
1108 if (r->type->set(r, buf) != ERROR_OK)
1109 return ERROR_FAIL;
1110 }
1111
1112 return ERROR_OK;
1113 }
1114
1115 /* Should run code on the target to perform CRC of
1116 memory. Not yet implemented.
1117 */
1118
1119 static int riscv_checksum_memory(struct target *target,
1120 target_addr_t address, uint32_t count,
1121 uint32_t *checksum)
1122 {
1123 *checksum = 0xFFFFFFFF;
1124 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1125 }
1126
1127 /*** OpenOCD Helper Functions ***/
1128
1129 enum riscv_poll_hart {
1130 RPH_NO_CHANGE,
1131 RPH_DISCOVERED_HALTED,
1132 RPH_DISCOVERED_RUNNING,
1133 RPH_ERROR
1134 };
1135 static enum riscv_poll_hart riscv_poll_hart(struct target *target, int hartid)
1136 {
1137 RISCV_INFO(r);
1138 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
1139 return RPH_ERROR;
1140
1141 LOG_DEBUG("polling hart %d, target->state=%d", hartid, target->state);
1142
1143 /* If OpenOCD thinks we're running but this hart is halted then it's time
1144 * to raise an event. */
1145 bool halted = riscv_is_halted(target);
1146 if (target->state != TARGET_HALTED && halted) {
1147 LOG_DEBUG(" triggered a halt");
1148 r->on_halt(target);
1149 return RPH_DISCOVERED_HALTED;
1150 } else if (target->state != TARGET_RUNNING && !halted) {
1151 LOG_DEBUG(" triggered running");
1152 target->state = TARGET_RUNNING;
1153 return RPH_DISCOVERED_RUNNING;
1154 }
1155
1156 return RPH_NO_CHANGE;
1157 }
1158
1159 int set_debug_reason(struct target *target, int hartid)
1160 {
1161 switch (riscv_halt_reason(target, hartid)) {
1162 case RISCV_HALT_BREAKPOINT:
1163 target->debug_reason = DBG_REASON_BREAKPOINT;
1164 break;
1165 case RISCV_HALT_TRIGGER:
1166 target->debug_reason = DBG_REASON_WATCHPOINT;
1167 break;
1168 case RISCV_HALT_INTERRUPT:
1169 target->debug_reason = DBG_REASON_DBGRQ;
1170 break;
1171 case RISCV_HALT_SINGLESTEP:
1172 target->debug_reason = DBG_REASON_SINGLESTEP;
1173 break;
1174 case RISCV_HALT_UNKNOWN:
1175 target->debug_reason = DBG_REASON_UNDEFINED;
1176 break;
1177 case RISCV_HALT_ERROR:
1178 return ERROR_FAIL;
1179 }
1180 return ERROR_OK;
1181 }
1182
1183 /*** OpenOCD Interface ***/
1184 int riscv_openocd_poll(struct target *target)
1185 {
1186 LOG_DEBUG("polling all harts");
1187 int halted_hart = -1;
1188 if (riscv_rtos_enabled(target)) {
1189 /* Check every hart for an event. */
1190 for (int i = 0; i < riscv_count_harts(target); ++i) {
1191 enum riscv_poll_hart out = riscv_poll_hart(target, i);
1192 switch (out) {
1193 case RPH_NO_CHANGE:
1194 case RPH_DISCOVERED_RUNNING:
1195 continue;
1196 case RPH_DISCOVERED_HALTED:
1197 halted_hart = i;
1198 break;
1199 case RPH_ERROR:
1200 return ERROR_FAIL;
1201 }
1202 }
1203 if (halted_hart == -1) {
1204 LOG_DEBUG(" no harts just halted, target->state=%d", target->state);
1205 return ERROR_OK;
1206 }
1207 LOG_DEBUG(" hart %d halted", halted_hart);
1208
1209 /* If we're here then at least one hart triggered. That means
1210 * we want to go and halt _every_ hart in the system, as that's
1211 * the invariant we hold here. Some harts might have already
1212 * halted (as we're either in single-step mode or they also
1213 * triggered a breakpoint), so don't attempt to halt those
1214 * harts. */
1215 for (int i = 0; i < riscv_count_harts(target); ++i)
1216 riscv_halt_one_hart(target, i);
1217
1218 } else if (target->smp) {
1219 bool halt_discovered = false;
1220 bool newly_halted[128] = {0};
1221 unsigned i = 0;
1222 for (struct target_list *list = target->head; list != NULL;
1223 list = list->next, i++) {
1224 struct target *t = list->target;
1225 riscv_info_t *r = riscv_info(t);
1226 assert(i < DIM(newly_halted));
1227 enum riscv_poll_hart out = riscv_poll_hart(t, r->current_hartid);
1228 switch (out) {
1229 case RPH_NO_CHANGE:
1230 break;
1231 case RPH_DISCOVERED_RUNNING:
1232 t->state = TARGET_RUNNING;
1233 break;
1234 case RPH_DISCOVERED_HALTED:
1235 halt_discovered = true;
1236 newly_halted[i] = true;
1237 t->state = TARGET_HALTED;
1238 if (set_debug_reason(t, r->current_hartid) != ERROR_OK)
1239 return ERROR_FAIL;
1240 break;
1241 case RPH_ERROR:
1242 return ERROR_FAIL;
1243 }
1244 }
1245
1246 if (halt_discovered) {
1247 LOG_DEBUG("Halt other targets in this SMP group.");
1248 i = 0;
1249 for (struct target_list *list = target->head; list != NULL;
1250 list = list->next, i++) {
1251 struct target *t = list->target;
1252 riscv_info_t *r = riscv_info(t);
1253 if (t->state != TARGET_HALTED) {
1254 if (riscv_halt_one_hart(t, r->current_hartid) != ERROR_OK)
1255 return ERROR_FAIL;
1256 t->state = TARGET_HALTED;
1257 if (set_debug_reason(t, r->current_hartid) != ERROR_OK)
1258 return ERROR_FAIL;
1259 newly_halted[i] = true;
1260 }
1261 }
1262
1263 /* Now that we have all our ducks in a row, tell the higher layers
1264 * what just happened. */
1265 i = 0;
1266 for (struct target_list *list = target->head; list != NULL;
1267 list = list->next, i++) {
1268 struct target *t = list->target;
1269 if (newly_halted[i])
1270 target_call_event_callbacks(t, TARGET_EVENT_HALTED);
1271 }
1272 }
1273 return ERROR_OK;
1274
1275 } else {
1276 enum riscv_poll_hart out = riscv_poll_hart(target,
1277 riscv_current_hartid(target));
1278 if (out == RPH_NO_CHANGE || out == RPH_DISCOVERED_RUNNING)
1279 return ERROR_OK;
1280 else if (out == RPH_ERROR)
1281 return ERROR_FAIL;
1282
1283 halted_hart = riscv_current_hartid(target);
1284 LOG_DEBUG(" hart %d halted", halted_hart);
1285 }
1286
1287 target->state = TARGET_HALTED;
1288 if (set_debug_reason(target, halted_hart) != ERROR_OK)
1289 return ERROR_FAIL;
1290
1291 if (riscv_rtos_enabled(target)) {
1292 target->rtos->current_threadid = halted_hart + 1;
1293 target->rtos->current_thread = halted_hart + 1;
1294 riscv_set_rtos_hartid(target, halted_hart);
1295 }
1296
1297 target->state = TARGET_HALTED;
1298
1299 if (target->debug_reason == DBG_REASON_BREAKPOINT) {
1300 int retval;
1301 if (riscv_semihosting(target, &retval) != 0)
1302 return retval;
1303 }
1304
1305 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1306 return ERROR_OK;
1307 }
1308
1309 int riscv_openocd_halt(struct target *target)
1310 {
1311 RISCV_INFO(r);
1312 int result;
1313
1314 LOG_DEBUG("[%d] halting all harts", target->coreid);
1315
1316 if (target->smp) {
1317 LOG_DEBUG("Halt other targets in this SMP group.");
1318 struct target_list *targets = target->head;
1319 result = ERROR_OK;
1320 while (targets) {
1321 struct target *t = targets->target;
1322 targets = targets->next;
1323 if (t->state != TARGET_HALTED) {
1324 if (riscv_halt_all_harts(t) != ERROR_OK)
1325 result = ERROR_FAIL;
1326 }
1327 }
1328 } else {
1329 result = riscv_halt_all_harts(target);
1330 }
1331
1332 if (riscv_rtos_enabled(target)) {
1333 if (r->rtos_hartid != -1) {
1334 LOG_DEBUG("halt requested on RTOS hartid %d", r->rtos_hartid);
1335 target->rtos->current_threadid = r->rtos_hartid + 1;
1336 target->rtos->current_thread = r->rtos_hartid + 1;
1337 } else
1338 LOG_DEBUG("halt requested, but no known RTOS hartid");
1339 }
1340
1341 target->state = TARGET_HALTED;
1342 target->debug_reason = DBG_REASON_DBGRQ;
1343 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1344 return result;
1345 }
1346
1347 int riscv_openocd_resume(
1348 struct target *target,
1349 int current,
1350 target_addr_t address,
1351 int handle_breakpoints,
1352 int debug_execution)
1353 {
1354 LOG_DEBUG("debug_reason=%d", target->debug_reason);
1355
1356 if (!current)
1357 riscv_set_register(target, GDB_REGNO_PC, address);
1358
1359 if (target->debug_reason == DBG_REASON_WATCHPOINT) {
1360 /* To be able to run off a trigger, disable all the triggers, step, and
1361 * then resume as usual. */
1362 struct watchpoint *watchpoint = target->watchpoints;
1363 bool trigger_temporarily_cleared[RISCV_MAX_HWBPS] = {0};
1364
1365 int i = 0;
1366 int result = ERROR_OK;
1367 while (watchpoint && result == ERROR_OK) {
1368 LOG_DEBUG("watchpoint %d: set=%d", i, watchpoint->set);
1369 trigger_temporarily_cleared[i] = watchpoint->set;
1370 if (watchpoint->set)
1371 result = riscv_remove_watchpoint(target, watchpoint);
1372 watchpoint = watchpoint->next;
1373 i++;
1374 }
1375
1376 if (result == ERROR_OK)
1377 result = riscv_step_rtos_hart(target);
1378
1379 watchpoint = target->watchpoints;
1380 i = 0;
1381 while (watchpoint) {
1382 LOG_DEBUG("watchpoint %d: cleared=%d", i, trigger_temporarily_cleared[i]);
1383 if (trigger_temporarily_cleared[i]) {
1384 if (result == ERROR_OK)
1385 result = riscv_add_watchpoint(target, watchpoint);
1386 else
1387 riscv_add_watchpoint(target, watchpoint);
1388 }
1389 watchpoint = watchpoint->next;
1390 i++;
1391 }
1392
1393 if (result != ERROR_OK)
1394 return result;
1395 }
1396
1397 int out = riscv_resume_all_harts(target);
1398 if (out != ERROR_OK) {
1399 LOG_ERROR("unable to resume all harts");
1400 return out;
1401 }
1402
1403 register_cache_invalidate(target->reg_cache);
1404 target->state = TARGET_RUNNING;
1405 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
1406 return out;
1407 }
1408
1409 int riscv_openocd_step(struct target *target, int current,
1410 target_addr_t address, int handle_breakpoints)
1411 {
1412 LOG_DEBUG("stepping rtos hart");
1413
1414 if (!current)
1415 riscv_set_register(target, GDB_REGNO_PC, address);
1416
1417 int out = riscv_step_rtos_hart(target);
1418 if (out != ERROR_OK) {
1419 LOG_ERROR("unable to step rtos hart");
1420 return out;
1421 }
1422
1423 register_cache_invalidate(target->reg_cache);
1424 target->state = TARGET_RUNNING;
1425 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
1426 target->state = TARGET_HALTED;
1427 target->debug_reason = DBG_REASON_SINGLESTEP;
1428 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1429 return out;
1430 }
1431
1432 /* Command Handlers */
1433 COMMAND_HANDLER(riscv_set_command_timeout_sec)
1434 {
1435 if (CMD_ARGC != 1) {
1436 LOG_ERROR("Command takes exactly 1 parameter");
1437 return ERROR_COMMAND_SYNTAX_ERROR;
1438 }
1439 int timeout = atoi(CMD_ARGV[0]);
1440 if (timeout <= 0) {
1441 LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
1442 return ERROR_FAIL;
1443 }
1444
1445 riscv_command_timeout_sec = timeout;
1446
1447 return ERROR_OK;
1448 }
1449
1450 COMMAND_HANDLER(riscv_set_reset_timeout_sec)
1451 {
1452 if (CMD_ARGC != 1) {
1453 LOG_ERROR("Command takes exactly 1 parameter");
1454 return ERROR_COMMAND_SYNTAX_ERROR;
1455 }
1456 int timeout = atoi(CMD_ARGV[0]);
1457 if (timeout <= 0) {
1458 LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
1459 return ERROR_FAIL;
1460 }
1461
1462 riscv_reset_timeout_sec = timeout;
1463 return ERROR_OK;
1464 }
1465
1466 COMMAND_HANDLER(riscv_test_compliance) {
1467
1468 struct target *target = get_current_target(CMD_CTX);
1469
1470 RISCV_INFO(r);
1471
1472 if (CMD_ARGC > 0) {
1473 LOG_ERROR("Command does not take any parameters.");
1474 return ERROR_COMMAND_SYNTAX_ERROR;
1475 }
1476
1477 if (r->test_compliance) {
1478 return r->test_compliance(target);
1479 } else {
1480 LOG_ERROR("This target does not support this command (may implement an older version of the spec).");
1481 return ERROR_FAIL;
1482 }
1483 }
1484
1485 COMMAND_HANDLER(riscv_set_prefer_sba)
1486 {
1487 if (CMD_ARGC != 1) {
1488 LOG_ERROR("Command takes exactly 1 parameter");
1489 return ERROR_COMMAND_SYNTAX_ERROR;
1490 }
1491 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], riscv_prefer_sba);
1492 return ERROR_OK;
1493 }
1494
1495 void parse_error(const char *string, char c, unsigned position)
1496 {
1497 char buf[position+2];
1498 for (unsigned i = 0; i < position; i++)
1499 buf[i] = ' ';
1500 buf[position] = '^';
1501 buf[position + 1] = 0;
1502
1503 LOG_ERROR("Parse error at character %c in:", c);
1504 LOG_ERROR("%s", string);
1505 LOG_ERROR("%s", buf);
1506 }
1507
1508 int parse_ranges(range_t **ranges, const char **argv)
1509 {
1510 for (unsigned pass = 0; pass < 2; pass++) {
1511 unsigned range = 0;
1512 unsigned low = 0;
1513 bool parse_low = true;
1514 unsigned high = 0;
1515 for (unsigned i = 0; i == 0 || argv[0][i-1]; i++) {
1516 char c = argv[0][i];
1517 if (isspace(c)) {
1518 /* Ignore whitespace. */
1519 continue;
1520 }
1521
1522 if (parse_low) {
1523 if (isdigit(c)) {
1524 low *= 10;
1525 low += c - '0';
1526 } else if (c == '-') {
1527 parse_low = false;
1528 } else if (c == ',' || c == 0) {
1529 if (pass == 1) {
1530 (*ranges)[range].low = low;
1531 (*ranges)[range].high = low;
1532 }
1533 low = 0;
1534 range++;
1535 } else {
1536 parse_error(argv[0], c, i);
1537 return ERROR_COMMAND_SYNTAX_ERROR;
1538 }
1539
1540 } else {
1541 if (isdigit(c)) {
1542 high *= 10;
1543 high += c - '0';
1544 } else if (c == ',' || c == 0) {
1545 parse_low = true;
1546 if (pass == 1) {
1547 (*ranges)[range].low = low;
1548 (*ranges)[range].high = high;
1549 }
1550 low = 0;
1551 high = 0;
1552 range++;
1553 } else {
1554 parse_error(argv[0], c, i);
1555 return ERROR_COMMAND_SYNTAX_ERROR;
1556 }
1557 }
1558 }
1559
1560 if (pass == 0) {
1561 if (*ranges)
1562 free(*ranges);
1563 *ranges = calloc(range + 2, sizeof(range_t));
1564 } else {
1565 (*ranges)[range].low = 1;
1566 (*ranges)[range].high = 0;
1567 }
1568 }
1569
1570 return ERROR_OK;
1571 }
1572
1573 COMMAND_HANDLER(riscv_set_expose_csrs)
1574 {
1575 if (CMD_ARGC != 1) {
1576 LOG_ERROR("Command takes exactly 1 parameter");
1577 return ERROR_COMMAND_SYNTAX_ERROR;
1578 }
1579
1580 return parse_ranges(&expose_csr, CMD_ARGV);
1581 }
1582
1583 COMMAND_HANDLER(riscv_set_expose_custom)
1584 {
1585 if (CMD_ARGC != 1) {
1586 LOG_ERROR("Command takes exactly 1 parameter");
1587 return ERROR_COMMAND_SYNTAX_ERROR;
1588 }
1589
1590 return parse_ranges(&expose_custom, CMD_ARGV);
1591 }
1592
1593 COMMAND_HANDLER(riscv_authdata_read)
1594 {
1595 if (CMD_ARGC != 0) {
1596 LOG_ERROR("Command takes no parameters");
1597 return ERROR_COMMAND_SYNTAX_ERROR;
1598 }
1599
1600 struct target *target = get_current_target(CMD_CTX);
1601 if (!target) {
1602 LOG_ERROR("target is NULL!");
1603 return ERROR_FAIL;
1604 }
1605
1606 RISCV_INFO(r);
1607 if (!r) {
1608 LOG_ERROR("riscv_info is NULL!");
1609 return ERROR_FAIL;
1610 }
1611
1612 if (r->authdata_read) {
1613 uint32_t value;
1614 if (r->authdata_read(target, &value) != ERROR_OK)
1615 return ERROR_FAIL;
1616 command_print(CMD, "0x%" PRIx32, value);
1617 return ERROR_OK;
1618 } else {
1619 LOG_ERROR("authdata_read is not implemented for this target.");
1620 return ERROR_FAIL;
1621 }
1622 }
1623
1624 COMMAND_HANDLER(riscv_authdata_write)
1625 {
1626 if (CMD_ARGC != 1) {
1627 LOG_ERROR("Command takes exactly 1 argument");
1628 return ERROR_COMMAND_SYNTAX_ERROR;
1629 }
1630
1631 struct target *target = get_current_target(CMD_CTX);
1632 RISCV_INFO(r);
1633
1634 uint32_t value;
1635 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], value);
1636
1637 if (r->authdata_write) {
1638 return r->authdata_write(target, value);
1639 } else {
1640 LOG_ERROR("authdata_write is not implemented for this target.");
1641 return ERROR_FAIL;
1642 }
1643 }
1644
1645 COMMAND_HANDLER(riscv_dmi_read)
1646 {
1647 if (CMD_ARGC != 1) {
1648 LOG_ERROR("Command takes 1 parameter");
1649 return ERROR_COMMAND_SYNTAX_ERROR;
1650 }
1651
1652 struct target *target = get_current_target(CMD_CTX);
1653 if (!target) {
1654 LOG_ERROR("target is NULL!");
1655 return ERROR_FAIL;
1656 }
1657
1658 RISCV_INFO(r);
1659 if (!r) {
1660 LOG_ERROR("riscv_info is NULL!");
1661 return ERROR_FAIL;
1662 }
1663
1664 if (r->dmi_read) {
1665 uint32_t address, value;
1666 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
1667 if (r->dmi_read(target, &value, address) != ERROR_OK)
1668 return ERROR_FAIL;
1669 command_print(CMD, "0x%" PRIx32, value);
1670 return ERROR_OK;
1671 } else {
1672 LOG_ERROR("dmi_read is not implemented for this target.");
1673 return ERROR_FAIL;
1674 }
1675 }
1676
1677
1678 COMMAND_HANDLER(riscv_dmi_write)
1679 {
1680 if (CMD_ARGC != 2) {
1681 LOG_ERROR("Command takes exactly 2 arguments");
1682 return ERROR_COMMAND_SYNTAX_ERROR;
1683 }
1684
1685 struct target *target = get_current_target(CMD_CTX);
1686 RISCV_INFO(r);
1687
1688 uint32_t address, value;
1689 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
1690 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
1691
1692 if (r->dmi_write) {
1693 return r->dmi_write(target, address, value);
1694 } else {
1695 LOG_ERROR("dmi_write is not implemented for this target.");
1696 return ERROR_FAIL;
1697 }
1698 }
1699
1700 COMMAND_HANDLER(riscv_test_sba_config_reg)
1701 {
1702 if (CMD_ARGC != 4) {
1703 LOG_ERROR("Command takes exactly 4 arguments");
1704 return ERROR_COMMAND_SYNTAX_ERROR;
1705 }
1706
1707 struct target *target = get_current_target(CMD_CTX);
1708 RISCV_INFO(r);
1709
1710 target_addr_t legal_address;
1711 uint32_t num_words;
1712 target_addr_t illegal_address;
1713 bool run_sbbusyerror_test;
1714
1715 COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[0], legal_address);
1716 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], num_words);
1717 COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[2], illegal_address);
1718 COMMAND_PARSE_ON_OFF(CMD_ARGV[3], run_sbbusyerror_test);
1719
1720 if (r->test_sba_config_reg) {
1721 return r->test_sba_config_reg(target, legal_address, num_words,
1722 illegal_address, run_sbbusyerror_test);
1723 } else {
1724 LOG_ERROR("test_sba_config_reg is not implemented for this target.");
1725 return ERROR_FAIL;
1726 }
1727 }
1728
1729 COMMAND_HANDLER(riscv_reset_delays)
1730 {
1731 int wait = 0;
1732
1733 if (CMD_ARGC > 1) {
1734 LOG_ERROR("Command takes at most one argument");
1735 return ERROR_COMMAND_SYNTAX_ERROR;
1736 }
1737
1738 if (CMD_ARGC == 1)
1739 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], wait);
1740
1741 struct target *target = get_current_target(CMD_CTX);
1742 RISCV_INFO(r);
1743 r->reset_delays_wait = wait;
1744 return ERROR_OK;
1745 }
1746
1747 COMMAND_HANDLER(riscv_set_ir)
1748 {
1749 if (CMD_ARGC != 2) {
1750 LOG_ERROR("Command takes exactly 2 arguments");
1751 return ERROR_COMMAND_SYNTAX_ERROR;
1752 }
1753
1754 uint32_t value;
1755 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
1756
1757 if (!strcmp(CMD_ARGV[0], "idcode")) {
1758 buf_set_u32(ir_idcode, 0, 32, value);
1759 return ERROR_OK;
1760 } else if (!strcmp(CMD_ARGV[0], "dtmcs")) {
1761 buf_set_u32(ir_dtmcontrol, 0, 32, value);
1762 return ERROR_OK;
1763 } else if (!strcmp(CMD_ARGV[0], "dmi")) {
1764 buf_set_u32(ir_dbus, 0, 32, value);
1765 return ERROR_OK;
1766 } else {
1767 return ERROR_FAIL;
1768 }
1769 }
1770
1771 static const struct command_registration riscv_exec_command_handlers[] = {
1772 {
1773 .name = "test_compliance",
1774 .handler = riscv_test_compliance,
1775 .mode = COMMAND_EXEC,
1776 .usage = "riscv test_compliance",
1777 .help = "Runs a basic compliance test suite against the RISC-V Debug Spec."
1778 },
1779 {
1780 .name = "set_command_timeout_sec",
1781 .handler = riscv_set_command_timeout_sec,
1782 .mode = COMMAND_ANY,
1783 .usage = "riscv set_command_timeout_sec [sec]",
1784 .help = "Set the wall-clock timeout (in seconds) for individual commands"
1785 },
1786 {
1787 .name = "set_reset_timeout_sec",
1788 .handler = riscv_set_reset_timeout_sec,
1789 .mode = COMMAND_ANY,
1790 .usage = "riscv set_reset_timeout_sec [sec]",
1791 .help = "Set the wall-clock timeout (in seconds) after reset is deasserted"
1792 },
1793 {
1794 .name = "set_prefer_sba",
1795 .handler = riscv_set_prefer_sba,
1796 .mode = COMMAND_ANY,
1797 .usage = "riscv set_prefer_sba on|off",
1798 .help = "When on, prefer to use System Bus Access to access memory. "
1799 "When off, prefer to use the Program Buffer to access memory."
1800 },
1801 {
1802 .name = "expose_csrs",
1803 .handler = riscv_set_expose_csrs,
1804 .mode = COMMAND_ANY,
1805 .usage = "riscv expose_csrs n0[-m0][,n1[-m1]]...",
1806 .help = "Configure a list of inclusive ranges for CSRs to expose in "
1807 "addition to the standard ones. This must be executed before "
1808 "`init`."
1809 },
1810 {
1811 .name = "expose_custom",
1812 .handler = riscv_set_expose_custom,
1813 .mode = COMMAND_ANY,
1814 .usage = "riscv expose_custom n0[-m0][,n1[-m1]]...",
1815 .help = "Configure a list of inclusive ranges for custom registers to "
1816 "expose. custom0 is accessed as abstract register number 0xc000, "
1817 "etc. This must be executed before `init`."
1818 },
1819 {
1820 .name = "authdata_read",
1821 .handler = riscv_authdata_read,
1822 .mode = COMMAND_ANY,
1823 .usage = "riscv authdata_read",
1824 .help = "Return the 32-bit value read from authdata."
1825 },
1826 {
1827 .name = "authdata_write",
1828 .handler = riscv_authdata_write,
1829 .mode = COMMAND_ANY,
1830 .usage = "riscv authdata_write value",
1831 .help = "Write the 32-bit value to authdata."
1832 },
1833 {
1834 .name = "dmi_read",
1835 .handler = riscv_dmi_read,
1836 .mode = COMMAND_ANY,
1837 .usage = "riscv dmi_read address",
1838 .help = "Perform a 32-bit DMI read at address, returning the value."
1839 },
1840 {
1841 .name = "dmi_write",
1842 .handler = riscv_dmi_write,
1843 .mode = COMMAND_ANY,
1844 .usage = "riscv dmi_write address value",
1845 .help = "Perform a 32-bit DMI write of value at address."
1846 },
1847 {
1848 .name = "test_sba_config_reg",
1849 .handler = riscv_test_sba_config_reg,
1850 .mode = COMMAND_ANY,
1851 .usage = "riscv test_sba_config_reg legal_address num_words "
1852 "illegal_address run_sbbusyerror_test[on/off]",
1853 .help = "Perform a series of tests on the SBCS register. "
1854 "Inputs are a legal, 128-byte aligned address and a number of words to "
1855 "read/write starting at that address (i.e., address range [legal address, "
1856 "legal_address+word_size*num_words) must be legally readable/writable), "
1857 "an illegal, 128-byte aligned address for error flag/handling cases, "
1858 "and whether sbbusyerror test should be run."
1859 },
1860 {
1861 .name = "reset_delays",
1862 .handler = riscv_reset_delays,
1863 .mode = COMMAND_ANY,
1864 .usage = "reset_delays [wait]",
1865 .help = "OpenOCD learns how many Run-Test/Idle cycles are required "
1866 "between scans to avoid encountering the target being busy. This "
1867 "command resets those learned values after `wait` scans. It's only "
1868 "useful for testing OpenOCD itself."
1869 },
1870 {
1871 .name = "set_ir",
1872 .handler = riscv_set_ir,
1873 .mode = COMMAND_ANY,
1874 .usage = "riscv set_ir_idcode [idcode|dtmcs|dmi] value",
1875 .help = "Set IR value for specified JTAG register."
1876 },
1877 COMMAND_REGISTRATION_DONE
1878 };
1879
1880 /*
1881 * To be noted that RISC-V targets use the same semihosting commands as
1882 * ARM targets.
1883 *
1884 * The main reason is compatibility with existing tools. For example the
1885 * Eclipse OpenOCD/SEGGER J-Link/QEMU plug-ins have several widgets to
1886 * configure semihosting, which generate commands like `arm semihosting
1887 * enable`.
1888 * A secondary reason is the fact that the protocol used is exactly the
1889 * one specified by ARM. If RISC-V will ever define its own semihosting
1890 * protocol, then a command like `riscv semihosting enable` will make
1891 * sense, but for now all semihosting commands are prefixed with `arm`.
1892 */
1893 extern const struct command_registration semihosting_common_handlers[];
1894
1895 const struct command_registration riscv_command_handlers[] = {
1896 {
1897 .name = "riscv",
1898 .mode = COMMAND_ANY,
1899 .help = "RISC-V Command Group",
1900 .usage = "",
1901 .chain = riscv_exec_command_handlers
1902 },
1903 {
1904 .name = "arm",
1905 .mode = COMMAND_ANY,
1906 .help = "ARM Command Group",
1907 .usage = "",
1908 .chain = semihosting_common_handlers
1909 },
1910 COMMAND_REGISTRATION_DONE
1911 };
1912
1913 unsigned riscv_address_bits(struct target *target)
1914 {
1915 return riscv_xlen(target);
1916 }
1917
1918 struct target_type riscv_target = {
1919 .name = "riscv",
1920
1921 .init_target = riscv_init_target,
1922 .deinit_target = riscv_deinit_target,
1923 .examine = riscv_examine,
1924
1925 /* poll current target status */
1926 .poll = old_or_new_riscv_poll,
1927
1928 .halt = old_or_new_riscv_halt,
1929 .resume = old_or_new_riscv_resume,
1930 .step = old_or_new_riscv_step,
1931
1932 .assert_reset = riscv_assert_reset,
1933 .deassert_reset = riscv_deassert_reset,
1934
1935 .read_memory = riscv_read_memory,
1936 .write_memory = riscv_write_memory,
1937
1938 .checksum_memory = riscv_checksum_memory,
1939
1940 .get_gdb_reg_list = riscv_get_gdb_reg_list,
1941
1942 .add_breakpoint = riscv_add_breakpoint,
1943 .remove_breakpoint = riscv_remove_breakpoint,
1944
1945 .add_watchpoint = riscv_add_watchpoint,
1946 .remove_watchpoint = riscv_remove_watchpoint,
1947 .hit_watchpoint = riscv_hit_watchpoint,
1948
1949 .arch_state = riscv_arch_state,
1950
1951 .run_algorithm = riscv_run_algorithm,
1952
1953 .commands = riscv_command_handlers,
1954
1955 .address_bits = riscv_address_bits
1956 };
1957
1958 /*** RISC-V Interface ***/
1959
1960 void riscv_info_init(struct target *target, riscv_info_t *r)
1961 {
1962 memset(r, 0, sizeof(*r));
1963 r->dtm_version = 1;
1964 r->registers_initialized = false;
1965 r->current_hartid = target->coreid;
1966
1967 memset(r->trigger_unique_id, 0xff, sizeof(r->trigger_unique_id));
1968
1969 for (size_t h = 0; h < RISCV_MAX_HARTS; ++h) {
1970 r->xlen[h] = -1;
1971
1972 for (size_t e = 0; e < RISCV_MAX_REGISTERS; ++e)
1973 r->valid_saved_registers[h][e] = false;
1974 }
1975 }
1976
1977 int riscv_halt_all_harts(struct target *target)
1978 {
1979 for (int i = 0; i < riscv_count_harts(target); ++i) {
1980 if (!riscv_hart_enabled(target, i))
1981 continue;
1982
1983 riscv_halt_one_hart(target, i);
1984 }
1985
1986 riscv_invalidate_register_cache(target);
1987
1988 return ERROR_OK;
1989 }
1990
1991 int riscv_halt_one_hart(struct target *target, int hartid)
1992 {
1993 RISCV_INFO(r);
1994 LOG_DEBUG("halting hart %d", hartid);
1995 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
1996 return ERROR_FAIL;
1997 if (riscv_is_halted(target)) {
1998 LOG_DEBUG(" hart %d requested halt, but was already halted", hartid);
1999 return ERROR_OK;
2000 }
2001
2002 int result = r->halt_current_hart(target);
2003 register_cache_invalidate(target->reg_cache);
2004 return result;
2005 }
2006
2007 int riscv_resume_all_harts(struct target *target)
2008 {
2009 for (int i = 0; i < riscv_count_harts(target); ++i) {
2010 if (!riscv_hart_enabled(target, i))
2011 continue;
2012
2013 riscv_resume_one_hart(target, i);
2014 }
2015
2016 riscv_invalidate_register_cache(target);
2017 return ERROR_OK;
2018 }
2019
2020 int riscv_resume_one_hart(struct target *target, int hartid)
2021 {
2022 RISCV_INFO(r);
2023 LOG_DEBUG("resuming hart %d", hartid);
2024 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
2025 return ERROR_FAIL;
2026 if (!riscv_is_halted(target)) {
2027 LOG_DEBUG(" hart %d requested resume, but was already resumed", hartid);
2028 return ERROR_OK;
2029 }
2030
2031 r->on_resume(target);
2032 return r->resume_current_hart(target);
2033 }
2034
2035 int riscv_step_rtos_hart(struct target *target)
2036 {
2037 RISCV_INFO(r);
2038 int hartid = r->current_hartid;
2039 if (riscv_rtos_enabled(target)) {
2040 hartid = r->rtos_hartid;
2041 if (hartid == -1) {
2042 LOG_DEBUG("GDB has asked me to step \"any\" thread, so I'm stepping hart 0.");
2043 hartid = 0;
2044 }
2045 }
2046 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
2047 return ERROR_FAIL;
2048 LOG_DEBUG("stepping hart %d", hartid);
2049
2050 if (!riscv_is_halted(target)) {
2051 LOG_ERROR("Hart isn't halted before single step!");
2052 return ERROR_FAIL;
2053 }
2054 riscv_invalidate_register_cache(target);
2055 r->on_step(target);
2056 if (r->step_current_hart(target) != ERROR_OK)
2057 return ERROR_FAIL;
2058 riscv_invalidate_register_cache(target);
2059 r->on_halt(target);
2060 if (!riscv_is_halted(target)) {
2061 LOG_ERROR("Hart was not halted after single step!");
2062 return ERROR_FAIL;
2063 }
2064 return ERROR_OK;
2065 }
2066
2067 bool riscv_supports_extension(struct target *target, int hartid, char letter)
2068 {
2069 RISCV_INFO(r);
2070 unsigned num;
2071 if (letter >= 'a' && letter <= 'z')
2072 num = letter - 'a';
2073 else if (letter >= 'A' && letter <= 'Z')
2074 num = letter - 'A';
2075 else
2076 return false;
2077 return r->misa[hartid] & (1 << num);
2078 }
2079
2080 int riscv_xlen(const struct target *target)
2081 {
2082 return riscv_xlen_of_hart(target, riscv_current_hartid(target));
2083 }
2084
2085 int riscv_xlen_of_hart(const struct target *target, int hartid)
2086 {
2087 RISCV_INFO(r);
2088 assert(r->xlen[hartid] != -1);
2089 return r->xlen[hartid];
2090 }
2091
2092 extern struct rtos_type riscv_rtos;
2093 bool riscv_rtos_enabled(const struct target *target)
2094 {
2095 return false;
2096 }
2097
2098 int riscv_set_current_hartid(struct target *target, int hartid)
2099 {
2100 RISCV_INFO(r);
2101 if (!r->select_current_hart)
2102 return ERROR_OK;
2103
2104 int previous_hartid = riscv_current_hartid(target);
2105 r->current_hartid = hartid;
2106 assert(riscv_hart_enabled(target, hartid));
2107 LOG_DEBUG("setting hartid to %d, was %d", hartid, previous_hartid);
2108 if (r->select_current_hart(target) != ERROR_OK)
2109 return ERROR_FAIL;
2110
2111 /* This might get called during init, in which case we shouldn't be
2112 * setting up the register cache. */
2113 if (target_was_examined(target) && riscv_rtos_enabled(target))
2114 riscv_invalidate_register_cache(target);
2115
2116 return ERROR_OK;
2117 }
2118
2119 void riscv_invalidate_register_cache(struct target *target)
2120 {
2121 RISCV_INFO(r);
2122
2123 LOG_DEBUG("[%d]", target->coreid);
2124 register_cache_invalidate(target->reg_cache);
2125 for (size_t i = 0; i < target->reg_cache->num_regs; ++i) {
2126 struct reg *reg = &target->reg_cache->reg_list[i];
2127 reg->valid = false;
2128 }
2129
2130 r->registers_initialized = true;
2131 }
2132
2133 int riscv_current_hartid(const struct target *target)
2134 {
2135 RISCV_INFO(r);
2136 return r->current_hartid;
2137 }
2138
2139 void riscv_set_all_rtos_harts(struct target *target)
2140 {
2141 RISCV_INFO(r);
2142 r->rtos_hartid = -1;
2143 }
2144
2145 void riscv_set_rtos_hartid(struct target *target, int hartid)
2146 {
2147 LOG_DEBUG("setting RTOS hartid %d", hartid);
2148 RISCV_INFO(r);
2149 r->rtos_hartid = hartid;
2150 }
2151
2152 int riscv_count_harts(struct target *target)
2153 {
2154 if (target == NULL)
2155 return 1;
2156 RISCV_INFO(r);
2157 if (r == NULL)
2158 return 1;
2159 return r->hart_count;
2160 }
2161
2162 bool riscv_has_register(struct target *target, int hartid, int regid)
2163 {
2164 return 1;
2165 }
2166
2167 /**
2168 * This function is called when the debug user wants to change the value of a
2169 * register. The new value may be cached, and may not be written until the hart
2170 * is resumed. */
2171 int riscv_set_register(struct target *target, enum gdb_regno r, riscv_reg_t v)
2172 {
2173 return riscv_set_register_on_hart(target, riscv_current_hartid(target), r, v);
2174 }
2175
2176 int riscv_set_register_on_hart(struct target *target, int hartid,
2177 enum gdb_regno regid, uint64_t value)
2178 {
2179 RISCV_INFO(r);
2180 LOG_DEBUG("{%d} %s <- %" PRIx64, hartid, gdb_regno_name(regid), value);
2181 assert(r->set_register);
2182 return r->set_register(target, hartid, regid, value);
2183 }
2184
2185 int riscv_get_register(struct target *target, riscv_reg_t *value,
2186 enum gdb_regno r)
2187 {
2188 return riscv_get_register_on_hart(target, value,
2189 riscv_current_hartid(target), r);
2190 }
2191
2192 int riscv_get_register_on_hart(struct target *target, riscv_reg_t *value,
2193 int hartid, enum gdb_regno regid)
2194 {
2195 RISCV_INFO(r);
2196
2197 struct reg *reg = &target->reg_cache->reg_list[regid];
2198
2199 if (reg && reg->valid && hartid == riscv_current_hartid(target)) {
2200 *value = buf_get_u64(reg->value, 0, reg->size);
2201 return ERROR_OK;
2202 }
2203
2204 int result = r->get_register(target, value, hartid, regid);
2205
2206 LOG_DEBUG("{%d} %s: %" PRIx64, hartid, gdb_regno_name(regid), *value);
2207 return result;
2208 }
2209
2210 bool riscv_is_halted(struct target *target)
2211 {
2212 RISCV_INFO(r);
2213 assert(r->is_halted);
2214 return r->is_halted(target);
2215 }
2216
2217 enum riscv_halt_reason riscv_halt_reason(struct target *target, int hartid)
2218 {
2219 RISCV_INFO(r);
2220 if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
2221 return RISCV_HALT_ERROR;
2222 if (!riscv_is_halted(target)) {
2223 LOG_ERROR("Hart is not halted!");
2224 return RISCV_HALT_UNKNOWN;
2225 }
2226 return r->halt_reason(target);
2227 }
2228
2229 size_t riscv_debug_buffer_size(struct target *target)
2230 {
2231 RISCV_INFO(r);
2232 return r->debug_buffer_size[riscv_current_hartid(target)];
2233 }
2234
2235 int riscv_write_debug_buffer(struct target *target, int index, riscv_insn_t insn)
2236 {
2237 RISCV_INFO(r);
2238 r->write_debug_buffer(target, index, insn);
2239 return ERROR_OK;
2240 }
2241
2242 riscv_insn_t riscv_read_debug_buffer(struct target *target, int index)
2243 {
2244 RISCV_INFO(r);
2245 return r->read_debug_buffer(target, index);
2246 }
2247
2248 int riscv_execute_debug_buffer(struct target *target)
2249 {
2250 RISCV_INFO(r);
2251 return r->execute_debug_buffer(target);
2252 }
2253
2254 void riscv_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d)
2255 {
2256 RISCV_INFO(r);
2257 r->fill_dmi_write_u64(target, buf, a, d);
2258 }
2259
2260 void riscv_fill_dmi_read_u64(struct target *target, char *buf, int a)
2261 {
2262 RISCV_INFO(r);
2263 r->fill_dmi_read_u64(target, buf, a);
2264 }
2265
2266 void riscv_fill_dmi_nop_u64(struct target *target, char *buf)
2267 {
2268 RISCV_INFO(r);
2269 r->fill_dmi_nop_u64(target, buf);
2270 }
2271
2272 int riscv_dmi_write_u64_bits(struct target *target)
2273 {
2274 RISCV_INFO(r);
2275 return r->dmi_write_u64_bits(target);
2276 }
2277
2278 bool riscv_hart_enabled(struct target *target, int hartid)
2279 {
2280 /* FIXME: Add a hart mask to the RTOS. */
2281 if (riscv_rtos_enabled(target))
2282 return hartid < riscv_count_harts(target);
2283
2284 return hartid == target->coreid;
2285 }
2286
2287 /**
2288 * Count triggers, and initialize trigger_count for each hart.
2289 * trigger_count is initialized even if this function fails to discover
2290 * something.
2291 * Disable any hardware triggers that have dmode set. We can't have set them
2292 * ourselves. Maybe they're left over from some killed debug session.
2293 * */
2294 int riscv_enumerate_triggers(struct target *target)
2295 {
2296 RISCV_INFO(r);
2297
2298 if (r->triggers_enumerated)
2299 return ERROR_OK;
2300
2301 r->triggers_enumerated = true; /* At the very least we tried. */
2302
2303 for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
2304 if (!riscv_hart_enabled(target, hartid))
2305 continue;
2306
2307 riscv_reg_t tselect;
2308 int result = riscv_get_register_on_hart(target, &tselect, hartid,
2309 GDB_REGNO_TSELECT);
2310 if (result != ERROR_OK)
2311 return result;
2312
2313 for (unsigned t = 0; t < RISCV_MAX_TRIGGERS; ++t) {
2314 r->trigger_count[hartid] = t;
2315
2316 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, t);
2317 uint64_t tselect_rb;
2318 result = riscv_get_register_on_hart(target, &tselect_rb, hartid,
2319 GDB_REGNO_TSELECT);
2320 if (result != ERROR_OK)
2321 return result;
2322 /* Mask off the top bit, which is used as tdrmode in old
2323 * implementations. */
2324 tselect_rb &= ~(1ULL << (riscv_xlen(target)-1));
2325 if (tselect_rb != t)
2326 break;
2327 uint64_t tdata1;
2328 result = riscv_get_register_on_hart(target, &tdata1, hartid,
2329 GDB_REGNO_TDATA1);
2330 if (result != ERROR_OK)
2331 return result;
2332
2333 int type = get_field(tdata1, MCONTROL_TYPE(riscv_xlen(target)));
2334 switch (type) {
2335 case 1:
2336 /* On these older cores we don't support software using
2337 * triggers. */
2338 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
2339 break;
2340 case 2:
2341 if (tdata1 & MCONTROL_DMODE(riscv_xlen(target)))
2342 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
2343 break;
2344 }
2345 }
2346
2347 riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, tselect);
2348
2349 LOG_INFO("[%d] Found %d triggers", hartid, r->trigger_count[hartid]);
2350 }
2351
2352 return ERROR_OK;
2353 }
2354
2355 const char *gdb_regno_name(enum gdb_regno regno)
2356 {
2357 static char buf[32];
2358
2359 switch (regno) {
2360 case GDB_REGNO_ZERO:
2361 return "zero";
2362 case GDB_REGNO_S0:
2363 return "s0";
2364 case GDB_REGNO_S1:
2365 return "s1";
2366 case GDB_REGNO_PC:
2367 return "pc";
2368 case GDB_REGNO_FPR0:
2369 return "fpr0";
2370 case GDB_REGNO_FPR31:
2371 return "fpr31";
2372 case GDB_REGNO_CSR0:
2373 return "csr0";
2374 case GDB_REGNO_TSELECT:
2375 return "tselect";
2376 case GDB_REGNO_TDATA1:
2377 return "tdata1";
2378 case GDB_REGNO_TDATA2:
2379 return "tdata2";
2380 case GDB_REGNO_MISA:
2381 return "misa";
2382 case GDB_REGNO_DPC:
2383 return "dpc";
2384 case GDB_REGNO_DCSR:
2385 return "dcsr";
2386 case GDB_REGNO_DSCRATCH:
2387 return "dscratch";
2388 case GDB_REGNO_MSTATUS:
2389 return "mstatus";
2390 case GDB_REGNO_PRIV:
2391 return "priv";
2392 default:
2393 if (regno <= GDB_REGNO_XPR31)
2394 sprintf(buf, "x%d", regno - GDB_REGNO_ZERO);
2395 else if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095)
2396 sprintf(buf, "csr%d", regno - GDB_REGNO_CSR0);
2397 else if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31)
2398 sprintf(buf, "f%d", regno - GDB_REGNO_FPR0);
2399 else
2400 sprintf(buf, "gdb_regno_%d", regno);
2401 return buf;
2402 }
2403 }
2404
2405 static int register_get(struct reg *reg)
2406 {
2407 riscv_reg_info_t *reg_info = reg->arch_info;
2408 struct target *target = reg_info->target;
2409 uint64_t value;
2410 int result = riscv_get_register(target, &value, reg->number);
2411 if (result != ERROR_OK)
2412 return result;
2413 buf_set_u64(reg->value, 0, reg->size, value);
2414 /* CSRs (and possibly other extension) registers may change value at any
2415 * time. */
2416 if (reg->number <= GDB_REGNO_XPR31 ||
2417 (reg->number >= GDB_REGNO_FPR0 && reg->number <= GDB_REGNO_FPR31) ||
2418 reg->number == GDB_REGNO_PC)
2419 reg->valid = true;
2420 LOG_DEBUG("[%d]{%d} read 0x%" PRIx64 " from %s (valid=%d)",
2421 target->coreid, riscv_current_hartid(target), value, reg->name,
2422 reg->valid);
2423 return ERROR_OK;
2424 }
2425
2426 static int register_set(struct reg *reg, uint8_t *buf)
2427 {
2428 riscv_reg_info_t *reg_info = reg->arch_info;
2429 struct target *target = reg_info->target;
2430
2431 uint64_t value = buf_get_u64(buf, 0, reg->size);
2432
2433 LOG_DEBUG("[%d]{%d} write 0x%" PRIx64 " to %s (valid=%d)",
2434 target->coreid, riscv_current_hartid(target), value, reg->name,
2435 reg->valid);
2436 struct reg *r = &target->reg_cache->reg_list[reg->number];
2437 /* CSRs (and possibly other extension) registers may change value at any
2438 * time. */
2439 if (reg->number <= GDB_REGNO_XPR31 ||
2440 (reg->number >= GDB_REGNO_FPR0 && reg->number <= GDB_REGNO_FPR31) ||
2441 reg->number == GDB_REGNO_PC)
2442 r->valid = true;
2443 memcpy(r->value, buf, (r->size + 7) / 8);
2444
2445 riscv_set_register(target, reg->number, value);
2446 return ERROR_OK;
2447 }
2448
2449 static struct reg_arch_type riscv_reg_arch_type = {
2450 .get = register_get,
2451 .set = register_set
2452 };
2453
2454 struct csr_info {
2455 unsigned number;
2456 const char *name;
2457 };
2458
2459 static int cmp_csr_info(const void *p1, const void *p2)
2460 {
2461 return (int) (((struct csr_info *)p1)->number) - (int) (((struct csr_info *)p2)->number);
2462 }
2463
2464 int riscv_init_registers(struct target *target)
2465 {
2466 RISCV_INFO(info);
2467
2468 riscv_free_registers(target);
2469
2470 target->reg_cache = calloc(1, sizeof(*target->reg_cache));
2471 target->reg_cache->name = "RISC-V Registers";
2472 target->reg_cache->num_regs = GDB_REGNO_COUNT;
2473
2474 if (expose_custom) {
2475 for (unsigned i = 0; expose_custom[i].low <= expose_custom[i].high; i++) {
2476 for (unsigned number = expose_custom[i].low;
2477 number <= expose_custom[i].high;
2478 number++)
2479 target->reg_cache->num_regs++;
2480 }
2481 }
2482
2483 LOG_DEBUG("create register cache for %d registers",
2484 target->reg_cache->num_regs);
2485
2486 target->reg_cache->reg_list =
2487 calloc(target->reg_cache->num_regs, sizeof(struct reg));
2488
2489 const unsigned int max_reg_name_len = 12;
2490 if (info->reg_names)
2491 free(info->reg_names);
2492 info->reg_names =
2493 calloc(target->reg_cache->num_regs, max_reg_name_len);
2494 char *reg_name = info->reg_names;
2495
2496 static struct reg_feature feature_cpu = {
2497 .name = "org.gnu.gdb.riscv.cpu"
2498 };
2499 static struct reg_feature feature_fpu = {
2500 .name = "org.gnu.gdb.riscv.fpu"
2501 };
2502 static struct reg_feature feature_csr = {
2503 .name = "org.gnu.gdb.riscv.csr"
2504 };
2505 static struct reg_feature feature_virtual = {
2506 .name = "org.gnu.gdb.riscv.virtual"
2507 };
2508 static struct reg_feature feature_custom = {
2509 .name = "org.gnu.gdb.riscv.custom"
2510 };
2511
2512 static struct reg_data_type type_ieee_single = {
2513 .type = REG_TYPE_IEEE_SINGLE,
2514 .id = "ieee_single"
2515 };
2516 static struct reg_data_type type_ieee_double = {
2517 .type = REG_TYPE_IEEE_DOUBLE,
2518 .id = "ieee_double"
2519 };
2520 struct csr_info csr_info[] = {
2521 #define DECLARE_CSR(name, number) { number, #name },
2522 #include "encoding.h"
2523 #undef DECLARE_CSR
2524 };
2525 /* encoding.h does not contain the registers in sorted order. */
2526 qsort(csr_info, DIM(csr_info), sizeof(*csr_info), cmp_csr_info);
2527 unsigned csr_info_index = 0;
2528
2529 unsigned custom_range_index = 0;
2530 int custom_within_range = 0;
2531
2532 riscv_reg_info_t *shared_reg_info = calloc(1, sizeof(riscv_reg_info_t));
2533 shared_reg_info->target = target;
2534
2535 /* When gdb requests register N, gdb_get_register_packet() assumes that this
2536 * is register at index N in reg_list. So if there are certain registers
2537 * that don't exist, we need to leave holes in the list (or renumber, but
2538 * it would be nice not to have yet another set of numbers to translate
2539 * between). */
2540 for (uint32_t number = 0; number < target->reg_cache->num_regs; number++) {
2541 struct reg *r = &target->reg_cache->reg_list[number];
2542 r->dirty = false;
2543 r->valid = false;
2544 r->exist = true;
2545 r->type = &riscv_reg_arch_type;
2546 r->arch_info = shared_reg_info;
2547 r->number = number;
2548 r->size = riscv_xlen(target);
2549 /* r->size is set in riscv_invalidate_register_cache, maybe because the
2550 * target is in theory allowed to change XLEN on us. But I expect a lot
2551 * of other things to break in that case as well. */
2552 if (number <= GDB_REGNO_XPR31) {
2553 r->caller_save = true;
2554 switch (number) {
2555 case GDB_REGNO_ZERO:
2556 r->name = "zero";
2557 break;
2558 case GDB_REGNO_RA:
2559 r->name = "ra";
2560 break;
2561 case GDB_REGNO_SP:
2562 r->name = "sp";
2563 break;
2564 case GDB_REGNO_GP:
2565 r->name = "gp";
2566 break;
2567 case GDB_REGNO_TP:
2568 r->name = "tp";
2569 break;
2570 case GDB_REGNO_T0:
2571 r->name = "t0";
2572 break;
2573 case GDB_REGNO_T1:
2574 r->name = "t1";
2575 break;
2576 case GDB_REGNO_T2:
2577 r->name = "t2";
2578 break;
2579 case GDB_REGNO_FP:
2580 r->name = "fp";
2581 break;
2582 case GDB_REGNO_S1:
2583 r->name = "s1";
2584 break;
2585 case GDB_REGNO_A0:
2586 r->name = "a0";
2587 break;
2588 case GDB_REGNO_A1:
2589 r->name = "a1";
2590 break;
2591 case GDB_REGNO_A2:
2592 r->name = "a2";
2593 break;
2594 case GDB_REGNO_A3:
2595 r->name = "a3";
2596 break;
2597 case GDB_REGNO_A4:
2598 r->name = "a4";
2599 break;
2600 case GDB_REGNO_A5:
2601 r->name = "a5";
2602 break;
2603 case GDB_REGNO_A6:
2604 r->name = "a6";
2605 break;
2606 case GDB_REGNO_A7:
2607 r->name = "a7";
2608 break;
2609 case GDB_REGNO_S2:
2610 r->name = "s2";
2611 break;
2612 case GDB_REGNO_S3:
2613 r->name = "s3";
2614 break;
2615 case GDB_REGNO_S4:
2616 r->name = "s4";
2617 break;
2618 case GDB_REGNO_S5:
2619 r->name = "s5";
2620 break;
2621 case GDB_REGNO_S6:
2622 r->name = "s6";
2623 break;
2624 case GDB_REGNO_S7:
2625 r->name = "s7";
2626 break;
2627 case GDB_REGNO_S8:
2628 r->name = "s8";
2629 break;
2630 case GDB_REGNO_S9:
2631 r->name = "s9";
2632 break;
2633 case GDB_REGNO_S10:
2634 r->name = "s10";
2635 break;
2636 case GDB_REGNO_S11:
2637 r->name = "s11";
2638 break;
2639 case GDB_REGNO_T3:
2640 r->name = "t3";
2641 break;
2642 case GDB_REGNO_T4:
2643 r->name = "t4";
2644 break;
2645 case GDB_REGNO_T5:
2646 r->name = "t5";
2647 break;
2648 case GDB_REGNO_T6:
2649 r->name = "t6";
2650 break;
2651 }
2652 r->group = "general";
2653 r->feature = &feature_cpu;
2654 } else if (number == GDB_REGNO_PC) {
2655 r->caller_save = true;
2656 sprintf(reg_name, "pc");
2657 r->group = "general";
2658 r->feature = &feature_cpu;
2659 } else if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
2660 r->caller_save = true;
2661 if (riscv_supports_extension(target, riscv_current_hartid(target),
2662 'D')) {
2663 r->reg_data_type = &type_ieee_double;
2664 r->size = 64;
2665 } else if (riscv_supports_extension(target,
2666 riscv_current_hartid(target), 'F')) {
2667 r->reg_data_type = &type_ieee_single;
2668 r->size = 32;
2669 } else {
2670 r->exist = false;
2671 }
2672 switch (number) {
2673 case GDB_REGNO_FT0:
2674 r->name = "ft0";
2675 break;
2676 case GDB_REGNO_FT1:
2677 r->name = "ft1";
2678 break;
2679 case GDB_REGNO_FT2:
2680 r->name = "ft2";
2681 break;
2682 case GDB_REGNO_FT3:
2683 r->name = "ft3";
2684 break;
2685 case GDB_REGNO_FT4:
2686 r->name = "ft4";
2687 break;
2688 case GDB_REGNO_FT5:
2689 r->name = "ft5";
2690 break;
2691 case GDB_REGNO_FT6:
2692 r->name = "ft6";
2693 break;
2694 case GDB_REGNO_FT7:
2695 r->name = "ft7";
2696 break;
2697 case GDB_REGNO_FS0:
2698 r->name = "fs0";
2699 break;
2700 case GDB_REGNO_FS1:
2701 r->name = "fs1";
2702 break;
2703 case GDB_REGNO_FA0:
2704 r->name = "fa0";
2705 break;
2706 case GDB_REGNO_FA1:
2707 r->name = "fa1";
2708 break;
2709 case GDB_REGNO_FA2:
2710 r->name = "fa2";
2711 break;
2712 case GDB_REGNO_FA3:
2713 r->name = "fa3";
2714 break;
2715 case GDB_REGNO_FA4:
2716 r->name = "fa4";
2717 break;
2718 case GDB_REGNO_FA5:
2719 r->name = "fa5";
2720 break;
2721 case GDB_REGNO_FA6:
2722 r->name = "fa6";
2723 break;
2724 case GDB_REGNO_FA7:
2725 r->name = "fa7";
2726 break;
2727 case GDB_REGNO_FS2:
2728 r->name = "fs2";
2729 break;
2730 case GDB_REGNO_FS3:
2731 r->name = "fs3";
2732 break;
2733 case GDB_REGNO_FS4:
2734 r->name = "fs4";
2735 break;
2736 case GDB_REGNO_FS5:
2737 r->name = "fs5";
2738 break;
2739 case GDB_REGNO_FS6:
2740 r->name = "fs6";
2741 break;
2742 case GDB_REGNO_FS7:
2743 r->name = "fs7";
2744 break;
2745 case GDB_REGNO_FS8:
2746 r->name = "fs8";
2747 break;
2748 case GDB_REGNO_FS9:
2749 r->name = "fs9";
2750 break;
2751 case GDB_REGNO_FS10:
2752 r->name = "fs10";
2753 break;
2754 case GDB_REGNO_FS11:
2755 r->name = "fs11";
2756 break;
2757 case GDB_REGNO_FT8:
2758 r->name = "ft8";
2759 break;
2760 case GDB_REGNO_FT9:
2761 r->name = "ft9";
2762 break;
2763 case GDB_REGNO_FT10:
2764 r->name = "ft10";
2765 break;
2766 case GDB_REGNO_FT11:
2767 r->name = "ft11";
2768 break;
2769 }
2770 r->group = "float";
2771 r->feature = &feature_fpu;
2772 } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
2773 r->group = "csr";
2774 r->feature = &feature_csr;
2775 unsigned csr_number = number - GDB_REGNO_CSR0;
2776
2777 while (csr_info[csr_info_index].number < csr_number &&
2778 csr_info_index < DIM(csr_info) - 1) {
2779 csr_info_index++;
2780 }
2781 if (csr_info[csr_info_index].number == csr_number) {
2782 r->name = csr_info[csr_info_index].name;
2783 } else {
2784 sprintf(reg_name, "csr%d", csr_number);
2785 /* Assume unnamed registers don't exist, unless we have some
2786 * configuration that tells us otherwise. That's important
2787 * because eg. Eclipse crashes if a target has too many
2788 * registers, and apparently has no way of only showing a
2789 * subset of registers in any case. */
2790 r->exist = false;
2791 }
2792
2793 switch (csr_number) {
2794 case CSR_FFLAGS:
2795 case CSR_FRM:
2796 case CSR_FCSR:
2797 r->exist = riscv_supports_extension(target,
2798 riscv_current_hartid(target), 'F');
2799 r->group = "float";
2800 r->feature = &feature_fpu;
2801 break;
2802 case CSR_SSTATUS:
2803 case CSR_STVEC:
2804 case CSR_SIP:
2805 case CSR_SIE:
2806 case CSR_SCOUNTEREN:
2807 case CSR_SSCRATCH:
2808 case CSR_SEPC:
2809 case CSR_SCAUSE:
2810 case CSR_STVAL:
2811 case CSR_SATP:
2812 r->exist = riscv_supports_extension(target,
2813 riscv_current_hartid(target), 'S');
2814 break;
2815 case CSR_MEDELEG:
2816 case CSR_MIDELEG:
2817 /* "In systems with only M-mode, or with both M-mode and
2818 * U-mode but without U-mode trap support, the medeleg and
2819 * mideleg registers should not exist." */
2820 r->exist = riscv_supports_extension(target, riscv_current_hartid(target), 'S') ||
2821 riscv_supports_extension(target, riscv_current_hartid(target), 'N');
2822 break;
2823
2824 case CSR_CYCLEH:
2825 case CSR_TIMEH:
2826 case CSR_INSTRETH:
2827 case CSR_HPMCOUNTER3H:
2828 case CSR_HPMCOUNTER4H:
2829 case CSR_HPMCOUNTER5H:
2830 case CSR_HPMCOUNTER6H:
2831 case CSR_HPMCOUNTER7H:
2832 case CSR_HPMCOUNTER8H:
2833 case CSR_HPMCOUNTER9H:
2834 case CSR_HPMCOUNTER10H:
2835 case CSR_HPMCOUNTER11H:
2836 case CSR_HPMCOUNTER12H:
2837 case CSR_HPMCOUNTER13H:
2838 case CSR_HPMCOUNTER14H:
2839 case CSR_HPMCOUNTER15H:
2840 case CSR_HPMCOUNTER16H:
2841 case CSR_HPMCOUNTER17H:
2842 case CSR_HPMCOUNTER18H:
2843 case CSR_HPMCOUNTER19H:
2844 case CSR_HPMCOUNTER20H:
2845 case CSR_HPMCOUNTER21H:
2846 case CSR_HPMCOUNTER22H:
2847 case CSR_HPMCOUNTER23H:
2848 case CSR_HPMCOUNTER24H:
2849 case CSR_HPMCOUNTER25H:
2850 case CSR_HPMCOUNTER26H:
2851 case CSR_HPMCOUNTER27H:
2852 case CSR_HPMCOUNTER28H:
2853 case CSR_HPMCOUNTER29H:
2854 case CSR_HPMCOUNTER30H:
2855 case CSR_HPMCOUNTER31H:
2856 case CSR_MCYCLEH:
2857 case CSR_MINSTRETH:
2858 case CSR_MHPMCOUNTER3H:
2859 case CSR_MHPMCOUNTER4H:
2860 case CSR_MHPMCOUNTER5H:
2861 case CSR_MHPMCOUNTER6H:
2862 case CSR_MHPMCOUNTER7H:
2863 case CSR_MHPMCOUNTER8H:
2864 case CSR_MHPMCOUNTER9H:
2865 case CSR_MHPMCOUNTER10H:
2866 case CSR_MHPMCOUNTER11H:
2867 case CSR_MHPMCOUNTER12H:
2868 case CSR_MHPMCOUNTER13H:
2869 case CSR_MHPMCOUNTER14H:
2870 case CSR_MHPMCOUNTER15H:
2871 case CSR_MHPMCOUNTER16H:
2872 case CSR_MHPMCOUNTER17H:
2873 case CSR_MHPMCOUNTER18H:
2874 case CSR_MHPMCOUNTER19H:
2875 case CSR_MHPMCOUNTER20H:
2876 case CSR_MHPMCOUNTER21H:
2877 case CSR_MHPMCOUNTER22H:
2878 case CSR_MHPMCOUNTER23H:
2879 case CSR_MHPMCOUNTER24H:
2880 case CSR_MHPMCOUNTER25H:
2881 case CSR_MHPMCOUNTER26H:
2882 case CSR_MHPMCOUNTER27H:
2883 case CSR_MHPMCOUNTER28H:
2884 case CSR_MHPMCOUNTER29H:
2885 case CSR_MHPMCOUNTER30H:
2886 case CSR_MHPMCOUNTER31H:
2887 r->exist = riscv_xlen(target) == 32;
2888 break;
2889 }
2890
2891 if (!r->exist && expose_csr) {
2892 for (unsigned i = 0; expose_csr[i].low <= expose_csr[i].high; i++) {
2893 if (csr_number >= expose_csr[i].low && csr_number <= expose_csr[i].high) {
2894 LOG_INFO("Exposing additional CSR %d", csr_number);
2895 r->exist = true;
2896 break;
2897 }
2898 }
2899 }
2900
2901 } else if (number == GDB_REGNO_PRIV) {
2902 sprintf(reg_name, "priv");
2903 r->group = "general";
2904 r->feature = &feature_virtual;
2905 r->size = 8;
2906
2907 } else {
2908 /* Custom registers. */
2909 assert(expose_custom);
2910
2911 range_t *range = &expose_custom[custom_range_index];
2912 assert(range->low <= range->high);
2913 unsigned custom_number = range->low + custom_within_range;
2914
2915 r->group = "custom";
2916 r->feature = &feature_custom;
2917 r->arch_info = calloc(1, sizeof(riscv_reg_info_t));
2918 assert(r->arch_info);
2919 ((riscv_reg_info_t *) r->arch_info)->target = target;
2920 ((riscv_reg_info_t *) r->arch_info)->custom_number = custom_number;
2921 sprintf(reg_name, "custom%d", custom_number);
2922
2923 custom_within_range++;
2924 if (custom_within_range > range->high - range->low) {
2925 custom_within_range = 0;
2926 custom_range_index++;
2927 }
2928 }
2929
2930 if (reg_name[0])
2931 r->name = reg_name;
2932 reg_name += strlen(reg_name) + 1;
2933 assert(reg_name < info->reg_names + target->reg_cache->num_regs *
2934 max_reg_name_len);
2935 r->value = &info->reg_cache_values[number];
2936 }
2937
2938 return ERROR_OK;
2939 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)