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

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)