OpenOCD
riscv.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <time.h>
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include <helper/log.h>
12 #include <helper/time_support.h>
13 #include "target/target.h"
14 #include "target/algorithm.h"
15 #include "target/target_type.h"
16 #include <target/smp.h>
17 #include "jtag/jtag.h"
18 #include "target/register.h"
19 #include "target/breakpoints.h"
20 #include "riscv.h"
21 #include "gdb_regs.h"
22 #include "rtos/rtos.h"
23 #include "debug_defines.h"
24 #include <helper/bits.h>
25 
26 #define get_field(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
27 #define set_field(reg, mask, val) (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
28 
29 /* Constants for legacy SiFive hardware breakpoints. */
30 #define CSR_BPCONTROL_X (1<<0)
31 #define CSR_BPCONTROL_W (1<<1)
32 #define CSR_BPCONTROL_R (1<<2)
33 #define CSR_BPCONTROL_U (1<<3)
34 #define CSR_BPCONTROL_S (1<<4)
35 #define CSR_BPCONTROL_H (1<<5)
36 #define CSR_BPCONTROL_M (1<<6)
37 #define CSR_BPCONTROL_BPMATCH (0xf<<7)
38 #define CSR_BPCONTROL_BPACTION (0xff<<11)
39 
40 #define DEBUG_ROM_START 0x800
41 #define DEBUG_ROM_RESUME (DEBUG_ROM_START + 4)
42 #define DEBUG_ROM_EXCEPTION (DEBUG_ROM_START + 8)
43 #define DEBUG_RAM_START 0x400
44 
45 #define SETHALTNOT 0x10c
46 
47 /*** JTAG registers. ***/
48 
49 #define DTMCONTROL 0x10
50 #define DTMCONTROL_DBUS_RESET (1<<16)
51 #define DTMCONTROL_IDLE (7<<10)
52 #define DTMCONTROL_ADDRBITS (0xf<<4)
53 #define DTMCONTROL_VERSION (0xf)
54 
55 #define DBUS 0x11
56 #define DBUS_OP_START 0
57 #define DBUS_OP_SIZE 2
58 typedef enum {
61  DBUS_OP_WRITE = 2
63 typedef enum {
68 #define DBUS_DATA_START 2
69 #define DBUS_DATA_SIZE 34
70 #define DBUS_ADDRESS_START 36
71 
72 typedef enum slot {
77 
78 /*** Debug Bus registers. ***/
79 
80 #define DMCONTROL 0x10
81 #define DMCONTROL_INTERRUPT (((uint64_t)1)<<33)
82 #define DMCONTROL_HALTNOT (((uint64_t)1)<<32)
83 #define DMCONTROL_BUSERROR (7<<19)
84 #define DMCONTROL_SERIAL (3<<16)
85 #define DMCONTROL_AUTOINCREMENT (1<<15)
86 #define DMCONTROL_ACCESS (7<<12)
87 #define DMCONTROL_HARTID (0x3ff<<2)
88 #define DMCONTROL_NDRESET (1<<1)
89 #define DMCONTROL_FULLRESET 1
90 
91 #define DMINFO 0x11
92 #define DMINFO_ABUSSIZE (0x7fU<<25)
93 #define DMINFO_SERIALCOUNT (0xf<<21)
94 #define DMINFO_ACCESS128 (1<<20)
95 #define DMINFO_ACCESS64 (1<<19)
96 #define DMINFO_ACCESS32 (1<<18)
97 #define DMINFO_ACCESS16 (1<<17)
98 #define DMINFO_ACCESS8 (1<<16)
99 #define DMINFO_DRAMSIZE (0x3f<<10)
100 #define DMINFO_AUTHENTICATED (1<<5)
101 #define DMINFO_AUTHBUSY (1<<4)
102 #define DMINFO_AUTHTYPE (3<<2)
103 #define DMINFO_VERSION 3
104 
105 /*** Info about the core being debugged. ***/
106 
107 #define DBUS_ADDRESS_UNKNOWN 0xffff
108 
109 #define MAX_HWBPS 16
110 #define DRAM_CACHE_SIZE 16
111 
112 static uint8_t ir_dtmcontrol[4] = {DTMCONTROL};
113 struct scan_field select_dtmcontrol = {
114  .in_value = NULL,
115  .out_value = ir_dtmcontrol
116 };
117 static uint8_t ir_dbus[4] = {DBUS};
118 struct scan_field select_dbus = {
119  .in_value = NULL,
120  .out_value = ir_dbus
121 };
122 static uint8_t ir_idcode[4] = {0x1};
123 struct scan_field select_idcode = {
124  .in_value = NULL,
125  .out_value = ir_idcode
126 };
127 
129 int bscan_tunnel_ir_width; /* if zero, then tunneling is not present/active */
130 
131 static const uint8_t bscan_zero[4] = {0};
132 static const uint8_t bscan_one[4] = {1};
133 
134 static uint8_t ir_user4[4];
135 static struct scan_field select_user4 = {
136  .in_value = NULL,
137  .out_value = ir_user4
138 };
139 
140 
141 static uint8_t bscan_tunneled_ir_width[4] = {5}; /* overridden by assignment in riscv_init_target */
143  {
144  .num_bits = 3,
145  .out_value = bscan_zero,
146  .in_value = NULL,
147  },
148  {
149  .num_bits = 5, /* initialized in riscv_init_target to ir width of DM */
150  .out_value = ir_dbus,
151  .in_value = NULL,
152  },
153  {
154  .num_bits = 7,
155  .out_value = bscan_tunneled_ir_width,
156  .in_value = NULL,
157  },
158  {
159  .num_bits = 1,
160  .out_value = bscan_zero,
161  .in_value = NULL,
162  }
163 };
164 
166  {
167  .num_bits = 1,
168  .out_value = bscan_zero,
169  .in_value = NULL,
170  },
171  {
172  .num_bits = 7,
173  .out_value = bscan_tunneled_ir_width,
174  .in_value = NULL,
175  },
176  {
177  .num_bits = 0, /* initialized in riscv_init_target to ir width of DM */
178  .out_value = ir_dbus,
179  .in_value = NULL,
180  },
181  {
182  .num_bits = 3,
183  .out_value = bscan_zero,
184  .in_value = NULL,
185  }
186 };
189 
192 
193 struct trigger {
194  uint64_t address;
195  uint32_t length;
196  uint64_t mask;
197  uint64_t value;
198  bool read, write, execute;
199  int unique_id;
200 };
201 
202 /* Wall-clock timeout for a command/access. Settable via RISC-V Target commands.*/
204 
205 /* Wall-clock timeout after reset. Settable via RISC-V Target commands.*/
207 
208 static bool riscv_enable_virt2phys = true;
209 bool riscv_ebreakm = true;
210 bool riscv_ebreaks = true;
211 bool riscv_ebreaku = true;
212 
214 
215 static enum {
219 
220 static const virt2phys_info_t sv32 = {
221  .name = "Sv32",
222  .va_bits = 32,
223  .level = 2,
224  .pte_shift = 2,
225  .vpn_shift = {12, 22},
226  .vpn_mask = {0x3ff, 0x3ff},
227  .pte_ppn_shift = {10, 20},
228  .pte_ppn_mask = {0x3ff, 0xfff},
229  .pa_ppn_shift = {12, 22},
230  .pa_ppn_mask = {0x3ff, 0xfff},
231 };
232 
233 static const virt2phys_info_t sv39 = {
234  .name = "Sv39",
235  .va_bits = 39,
236  .level = 3,
237  .pte_shift = 3,
238  .vpn_shift = {12, 21, 30},
239  .vpn_mask = {0x1ff, 0x1ff, 0x1ff},
240  .pte_ppn_shift = {10, 19, 28},
241  .pte_ppn_mask = {0x1ff, 0x1ff, 0x3ffffff},
242  .pa_ppn_shift = {12, 21, 30},
243  .pa_ppn_mask = {0x1ff, 0x1ff, 0x3ffffff},
244 };
245 
246 static const virt2phys_info_t sv48 = {
247  .name = "Sv48",
248  .va_bits = 48,
249  .level = 4,
250  .pte_shift = 3,
251  .vpn_shift = {12, 21, 30, 39},
252  .vpn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff},
253  .pte_ppn_shift = {10, 19, 28, 37},
254  .pte_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
255  .pa_ppn_shift = {12, 21, 30, 39},
256  .pa_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
257 };
258 
259 static enum riscv_halt_reason riscv_halt_reason(struct target *target, int hartid);
260 static void riscv_info_init(struct target *target, struct riscv_info *r);
261 static void riscv_invalidate_register_cache(struct target *target);
262 static int riscv_step_rtos_hart(struct target *target);
263 
264 static void riscv_sample_buf_maybe_add_timestamp(struct target *target, bool before)
265 {
266  RISCV_INFO(r);
267  uint32_t now = timeval_ms() & 0xffffffff;
268  if (r->sample_buf.used + 5 < r->sample_buf.size) {
269  if (before)
270  r->sample_buf.buf[r->sample_buf.used++] = RISCV_SAMPLE_BUF_TIMESTAMP_BEFORE;
271  else
272  r->sample_buf.buf[r->sample_buf.used++] = RISCV_SAMPLE_BUF_TIMESTAMP_AFTER;
273  r->sample_buf.buf[r->sample_buf.used++] = now & 0xff;
274  r->sample_buf.buf[r->sample_buf.used++] = (now >> 8) & 0xff;
275  r->sample_buf.buf[r->sample_buf.used++] = (now >> 16) & 0xff;
276  r->sample_buf.buf[r->sample_buf.used++] = (now >> 24) & 0xff;
277  }
278 }
279 
280 static int riscv_resume_go_all_harts(struct target *target);
281 
283 {
288  else /* BSCAN_TUNNEL_NESTED_TAP */
291 }
292 
293 uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out)
294 {
295  /* On BSCAN TAP: Select IR=USER4, issue tunneled IR scan via BSCAN TAP's DR */
296  uint8_t tunneled_ir_width[4] = {bscan_tunnel_ir_width};
297  uint8_t tunneled_dr_width[4] = {32};
298  uint8_t out_value[5] = {0};
299  uint8_t in_value[5] = {0};
300 
301  buf_set_u32(out_value, 0, 32, out);
302  struct scan_field tunneled_ir[4] = {};
303  struct scan_field tunneled_dr[4] = {};
304 
306  tunneled_ir[0].num_bits = 3;
307  tunneled_ir[0].out_value = bscan_zero;
308  tunneled_ir[0].in_value = NULL;
309  tunneled_ir[1].num_bits = bscan_tunnel_ir_width;
310  tunneled_ir[1].out_value = ir_dtmcontrol;
311  tunneled_ir[1].in_value = NULL;
312  tunneled_ir[2].num_bits = 7;
313  tunneled_ir[2].out_value = tunneled_ir_width;
314  tunneled_ir[2].in_value = NULL;
315  tunneled_ir[3].num_bits = 1;
316  tunneled_ir[3].out_value = bscan_zero;
317  tunneled_ir[3].in_value = NULL;
318 
319  tunneled_dr[0].num_bits = 3;
320  tunneled_dr[0].out_value = bscan_zero;
321  tunneled_dr[0].in_value = NULL;
322  tunneled_dr[1].num_bits = 32 + 1;
323  tunneled_dr[1].out_value = out_value;
324  tunneled_dr[1].in_value = in_value;
325  tunneled_dr[2].num_bits = 7;
326  tunneled_dr[2].out_value = tunneled_dr_width;
327  tunneled_dr[2].in_value = NULL;
328  tunneled_dr[3].num_bits = 1;
329  tunneled_dr[3].out_value = bscan_one;
330  tunneled_dr[3].in_value = NULL;
331  } else {
332  /* BSCAN_TUNNEL_NESTED_TAP */
333  tunneled_ir[3].num_bits = 3;
334  tunneled_ir[3].out_value = bscan_zero;
335  tunneled_ir[3].in_value = NULL;
336  tunneled_ir[2].num_bits = bscan_tunnel_ir_width;
337  tunneled_ir[2].out_value = ir_dtmcontrol;
338  tunneled_ir[1].in_value = NULL;
339  tunneled_ir[1].num_bits = 7;
340  tunneled_ir[1].out_value = tunneled_ir_width;
341  tunneled_ir[2].in_value = NULL;
342  tunneled_ir[0].num_bits = 1;
343  tunneled_ir[0].out_value = bscan_zero;
344  tunneled_ir[0].in_value = NULL;
345 
346  tunneled_dr[3].num_bits = 3;
347  tunneled_dr[3].out_value = bscan_zero;
348  tunneled_dr[3].in_value = NULL;
349  tunneled_dr[2].num_bits = 32 + 1;
350  tunneled_dr[2].out_value = out_value;
351  tunneled_dr[2].in_value = in_value;
352  tunneled_dr[1].num_bits = 7;
353  tunneled_dr[1].out_value = tunneled_dr_width;
354  tunneled_dr[1].in_value = NULL;
355  tunneled_dr[0].num_bits = 1;
356  tunneled_dr[0].out_value = bscan_one;
357  tunneled_dr[0].in_value = NULL;
358  }
360  jtag_add_dr_scan(target->tap, ARRAY_SIZE(tunneled_ir), tunneled_ir, TAP_IDLE);
361  jtag_add_dr_scan(target->tap, ARRAY_SIZE(tunneled_dr), tunneled_dr, TAP_IDLE);
363 
364  int retval = jtag_execute_queue();
365  if (retval != ERROR_OK) {
366  LOG_ERROR("failed jtag scan: %d", retval);
367  return retval;
368  }
369  /* Note the starting offset is bit 1, not bit 0. In BSCAN tunnel, there is a one-bit TCK skew between
370  output and input */
371  uint32_t in = buf_get_u32(in_value, 1, 32);
372  LOG_DEBUG("DTMCS: 0x%x -> 0x%x", out, in);
373 
374  return in;
375 }
376 
377 static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
378 {
379  struct scan_field field;
380  uint8_t in_value[4];
381  uint8_t out_value[4] = { 0 };
382 
383  if (bscan_tunnel_ir_width != 0)
384  return dtmcontrol_scan_via_bscan(target, out);
385 
386 
387  buf_set_u32(out_value, 0, 32, out);
388 
390 
391  field.num_bits = 32;
392  field.out_value = out_value;
393  field.in_value = in_value;
394  jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
395 
396  /* Always return to dbus. */
398 
399  int retval = jtag_execute_queue();
400  if (retval != ERROR_OK) {
401  LOG_ERROR("failed jtag scan: %d", retval);
402  return retval;
403  }
404 
405  uint32_t in = buf_get_u32(field.in_value, 0, 32);
406  LOG_DEBUG("DTMCONTROL: 0x%x -> 0x%x", out, in);
407 
408  return in;
409 }
410 
411 static struct target_type *get_target_type(struct target *target)
412 {
413  if (!target->arch_info) {
414  LOG_ERROR("Target has not been initialized");
415  return NULL;
416  }
417 
418  RISCV_INFO(info);
419  switch (info->dtm_version) {
420  case 0:
421  return &riscv011_target;
422  case 1:
423  return &riscv013_target;
424  default:
425  LOG_ERROR("Unsupported DTM version: %d", info->dtm_version);
426  return NULL;
427  }
428 }
429 
430 static int riscv_create_target(struct target *target, Jim_Interp *interp)
431 {
432  LOG_DEBUG("riscv_create_target()");
433  target->arch_info = calloc(1, sizeof(struct riscv_info));
434  if (!target->arch_info) {
435  LOG_ERROR("Failed to allocate RISC-V target structure.");
436  return ERROR_FAIL;
437  }
439  return ERROR_OK;
440 }
441 
442 static int riscv_init_target(struct command_context *cmd_ctx,
443  struct target *target)
444 {
445  LOG_DEBUG("riscv_init_target()");
446  RISCV_INFO(info);
447  info->cmd_ctx = cmd_ctx;
448 
452 
453  if (bscan_tunnel_ir_width != 0) {
454  assert(target->tap->ir_length >= 6);
455  uint32_t ir_user4_raw = 0x23 << (target->tap->ir_length - 6);
456  h_u32_to_le(ir_user4, ir_user4_raw);
461  else /* BSCAN_TUNNEL_NESTED_TAP */
463  }
464 
466 
468 
469  return ERROR_OK;
470 }
471 
472 static void riscv_free_registers(struct target *target)
473 {
474  /* Free the shared structure use for most registers. */
475  if (target->reg_cache) {
476  if (target->reg_cache->reg_list) {
477  free(target->reg_cache->reg_list[0].arch_info);
478  /* Free the ones we allocated separately. */
479  for (unsigned int i = GDB_REGNO_COUNT; i < target->reg_cache->num_regs; i++)
480  free(target->reg_cache->reg_list[i].arch_info);
481  for (unsigned int i = 0; i < target->reg_cache->num_regs; i++)
482  free(target->reg_cache->reg_list[i].value);
483  free(target->reg_cache->reg_list);
484  }
485  free(target->reg_cache);
486  }
487 }
488 
489 static void riscv_deinit_target(struct target *target)
490 {
491  LOG_DEBUG("riscv_deinit_target()");
492 
493  struct riscv_info *info = target->arch_info;
494  struct target_type *tt = get_target_type(target);
495 
496  if (tt && info && info->version_specific)
497  tt->deinit_target(target);
498 
500 
501  if (!info)
502  return;
503 
504  range_list_t *entry, *tmp;
505  list_for_each_entry_safe(entry, tmp, &info->expose_csr, list) {
506  free(entry->name);
507  free(entry);
508  }
509 
510  list_for_each_entry_safe(entry, tmp, &info->expose_custom, list) {
511  free(entry->name);
512  free(entry);
513  }
514 
515  free(info->reg_names);
516  free(target->arch_info);
517 
518  target->arch_info = NULL;
519 }
520 
522  const struct breakpoint *breakpoint)
523 {
526  trigger->mask = ~0LL;
527  trigger->read = false;
528  trigger->write = false;
529  trigger->execute = true;
530  /* unique_id is unique across both breakpoints and watchpoints. */
532 }
533 
534 static int maybe_add_trigger_t1(struct target *target,
535  struct trigger *trigger, uint64_t tdata1)
536 {
537  RISCV_INFO(r);
538 
539  const uint32_t bpcontrol_x = 1<<0;
540  const uint32_t bpcontrol_w = 1<<1;
541  const uint32_t bpcontrol_r = 1<<2;
542  const uint32_t bpcontrol_u = 1<<3;
543  const uint32_t bpcontrol_s = 1<<4;
544  const uint32_t bpcontrol_h = 1<<5;
545  const uint32_t bpcontrol_m = 1<<6;
546  const uint32_t bpcontrol_bpmatch = 0xf << 7;
547  const uint32_t bpcontrol_bpaction = 0xff << 11;
548 
549  if (tdata1 & (bpcontrol_r | bpcontrol_w | bpcontrol_x)) {
550  /* Trigger is already in use, presumably by user code. */
552  }
553 
554  tdata1 = set_field(tdata1, bpcontrol_r, trigger->read);
555  tdata1 = set_field(tdata1, bpcontrol_w, trigger->write);
556  tdata1 = set_field(tdata1, bpcontrol_x, trigger->execute);
557  tdata1 = set_field(tdata1, bpcontrol_u,
558  !!(r->misa & BIT('U' - 'A')));
559  tdata1 = set_field(tdata1, bpcontrol_s,
560  !!(r->misa & BIT('S' - 'A')));
561  tdata1 = set_field(tdata1, bpcontrol_h,
562  !!(r->misa & BIT('H' - 'A')));
563  tdata1 |= bpcontrol_m;
564  tdata1 = set_field(tdata1, bpcontrol_bpmatch, 0); /* exact match */
565  tdata1 = set_field(tdata1, bpcontrol_bpaction, 0); /* cause bp exception */
566 
568 
569  riscv_reg_t tdata1_rb;
570  if (riscv_get_register(target, &tdata1_rb, GDB_REGNO_TDATA1) != ERROR_OK)
571  return ERROR_FAIL;
572  LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
573 
574  if (tdata1 != tdata1_rb) {
575  LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
576  PRIx64 " to tdata1 it contains 0x%" PRIx64,
577  tdata1, tdata1_rb);
580  }
581 
583 
584  return ERROR_OK;
585 }
586 
587 static int maybe_add_trigger_t2(struct target *target,
588  struct trigger *trigger, uint64_t tdata1)
589 {
590  RISCV_INFO(r);
591 
592  /* tselect is already set */
593  if (tdata1 & (MCONTROL_EXECUTE | MCONTROL_STORE | MCONTROL_LOAD)) {
594  /* Trigger is already in use, presumably by user code. */
596  }
597 
598  /* address/data match trigger */
599  tdata1 |= MCONTROL_DMODE(riscv_xlen(target));
600  tdata1 = set_field(tdata1, MCONTROL_ACTION,
602  tdata1 = set_field(tdata1, MCONTROL_MATCH, MCONTROL_MATCH_EQUAL);
603  tdata1 |= MCONTROL_M;
604  if (r->misa & (1 << ('S' - 'A')))
605  tdata1 |= MCONTROL_S;
606  if (r->misa & (1 << ('U' - 'A')))
607  tdata1 |= MCONTROL_U;
608 
609  if (trigger->execute)
610  tdata1 |= MCONTROL_EXECUTE;
611  if (trigger->read)
612  tdata1 |= MCONTROL_LOAD;
613  if (trigger->write)
614  tdata1 |= MCONTROL_STORE;
615 
617 
618  uint64_t tdata1_rb;
619  int result = riscv_get_register(target, &tdata1_rb, GDB_REGNO_TDATA1);
620  if (result != ERROR_OK)
621  return result;
622  LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
623 
624  if (tdata1 != tdata1_rb) {
625  LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
626  PRIx64 " to tdata1 it contains 0x%" PRIx64,
627  tdata1, tdata1_rb);
630  }
631 
633 
634  return ERROR_OK;
635 }
636 
637 static int maybe_add_trigger_t6(struct target *target,
638  struct trigger *trigger, uint64_t tdata1)
639 {
640  RISCV_INFO(r);
641 
642  /* tselect is already set */
644  /* Trigger is already in use, presumably by user code. */
646  }
647 
648  /* address/data match trigger */
649  tdata1 |= MCONTROL_DMODE(riscv_xlen(target));
650  tdata1 = set_field(tdata1, CSR_MCONTROL6_ACTION,
653  tdata1 |= CSR_MCONTROL6_M;
654  if (r->misa & (1 << ('H' - 'A')))
656  if (r->misa & (1 << ('S' - 'A')))
657  tdata1 |= CSR_MCONTROL6_S;
658  if (r->misa & (1 << ('U' - 'A')))
659  tdata1 |= CSR_MCONTROL6_U;
660 
661  if (trigger->execute)
662  tdata1 |= CSR_MCONTROL6_EXECUTE;
663  if (trigger->read)
664  tdata1 |= CSR_MCONTROL6_LOAD;
665  if (trigger->write)
666  tdata1 |= CSR_MCONTROL6_STORE;
667 
669 
670  uint64_t tdata1_rb;
671  int result = riscv_get_register(target, &tdata1_rb, GDB_REGNO_TDATA1);
672  if (result != ERROR_OK)
673  return result;
674  LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
675 
676  if (tdata1 != tdata1_rb) {
677  LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
678  PRIx64 " to tdata1 it contains 0x%" PRIx64,
679  tdata1, tdata1_rb);
682  }
683 
685 
686  return ERROR_OK;
687 }
688 
689 static int add_trigger(struct target *target, struct trigger *trigger)
690 {
691  RISCV_INFO(r);
692 
694  return ERROR_FAIL;
695 
696  riscv_reg_t tselect;
698  return ERROR_FAIL;
699 
700  unsigned int i;
701  for (i = 0; i < r->trigger_count; i++) {
702  if (r->trigger_unique_id[i] != -1)
703  continue;
704 
706 
707  uint64_t tdata1;
708  int result = riscv_get_register(target, &tdata1, GDB_REGNO_TDATA1);
709  if (result != ERROR_OK)
710  return result;
711  int type = get_field(tdata1, MCONTROL_TYPE(riscv_xlen(target)));
712 
713  switch (type) {
714  case 1:
715  result = maybe_add_trigger_t1(target, trigger, tdata1);
716  break;
717  case 2:
718  result = maybe_add_trigger_t2(target, trigger, tdata1);
719  break;
720  case 6:
721  result = maybe_add_trigger_t6(target, trigger, tdata1);
722  break;
723  default:
724  LOG_DEBUG("trigger %d has unknown type %d", i, type);
725  continue;
726  }
727 
728  if (result != ERROR_OK)
729  continue;
730 
731  LOG_DEBUG("[%d] Using trigger %d (type %d) for bp %d", target->coreid,
732  i, type, trigger->unique_id);
733  r->trigger_unique_id[i] = trigger->unique_id;
734  break;
735  }
736 
738 
739  if (i >= r->trigger_count) {
740  LOG_ERROR("Couldn't find an available hardware trigger.");
742  }
743 
744  return ERROR_OK;
745 }
746 
752  uint32_t size, uint8_t *buffer, uint32_t access_size)
753 {
754  assert(size == 1 || size == 2 || size == 4 || size == 8);
755  assert(access_size == 1 || access_size == 2 || access_size == 4 || access_size == 8);
756 
757  if (access_size <= size && address % access_size == 0)
758  /* Can do the memory access directly without a helper buffer. */
759  return target_write_memory(target, address, access_size, size / access_size, buffer);
760 
761  unsigned int offset_head = address % access_size;
762  unsigned int n_blocks = ((size + offset_head) <= access_size) ? 1 : 2;
763  uint8_t helper_buf[n_blocks * access_size];
764 
765  /* Read from memory */
766  if (target_read_memory(target, address - offset_head, access_size, n_blocks, helper_buf) != ERROR_OK)
767  return ERROR_FAIL;
768 
769  /* Modify and write back */
770  memcpy(helper_buf + offset_head, buffer, size);
771  return target_write_memory(target, address - offset_head, access_size, n_blocks, helper_buf);
772 }
773 
779  uint32_t size, uint8_t *buffer, uint32_t access_size)
780 {
781  assert(size == 1 || size == 2 || size == 4 || size == 8);
782  assert(access_size == 1 || access_size == 2 || access_size == 4 || access_size == 8);
783 
784  if (access_size <= size && address % access_size == 0)
785  /* Can do the memory access directly without a helper buffer. */
786  return target_read_memory(target, address, access_size, size / access_size, buffer);
787 
788  unsigned int offset_head = address % access_size;
789  unsigned int n_blocks = ((size + offset_head) <= access_size) ? 1 : 2;
790  uint8_t helper_buf[n_blocks * access_size];
791 
792  /* Read from memory */
793  if (target_read_memory(target, address - offset_head, access_size, n_blocks, helper_buf) != ERROR_OK)
794  return ERROR_FAIL;
795 
796  /* Pick the requested portion from the buffer */
797  memcpy(buffer, helper_buf + offset_head, size);
798  return ERROR_OK;
799 }
800 
806 {
807  assert(size == 1 || size == 2 || size == 4 || size == 8);
808 
809  /* Find access size that correspond to data size and the alignment. */
810  unsigned int preferred_size = size;
811  while (address % preferred_size != 0)
812  preferred_size /= 2;
813 
814  /* First try the preferred (most natural) access size. */
815  if (write_by_given_size(target, address, size, buffer, preferred_size) == ERROR_OK)
816  return ERROR_OK;
817 
818  /* On failure, try other access sizes.
819  Minimize the number of accesses by trying first the largest size. */
820  for (unsigned int access_size = 8; access_size > 0; access_size /= 2) {
821  if (access_size == preferred_size)
822  /* Already tried this size. */
823  continue;
824 
825  if (write_by_given_size(target, address, size, buffer, access_size) == ERROR_OK)
826  return ERROR_OK;
827  }
828 
829  /* No access attempt succeeded. */
830  return ERROR_FAIL;
831 }
832 
838 {
839  assert(size == 1 || size == 2 || size == 4 || size == 8);
840 
841  /* Find access size that correspond to data size and the alignment. */
842  unsigned int preferred_size = size;
843  while (address % preferred_size != 0)
844  preferred_size /= 2;
845 
846  /* First try the preferred (most natural) access size. */
847  if (read_by_given_size(target, address, size, buffer, preferred_size) == ERROR_OK)
848  return ERROR_OK;
849 
850  /* On failure, try other access sizes.
851  Minimize the number of accesses by trying first the largest size. */
852  for (unsigned int access_size = 8; access_size > 0; access_size /= 2) {
853  if (access_size == preferred_size)
854  /* Already tried this size. */
855  continue;
856 
857  if (read_by_given_size(target, address, size, buffer, access_size) == ERROR_OK)
858  return ERROR_OK;
859  }
860 
861  /* No access attempt succeeded. */
862  return ERROR_FAIL;
863 }
864 
866 {
868  assert(breakpoint);
869  if (breakpoint->type == BKPT_SOFT) {
871  if (!(breakpoint->length == 4 || breakpoint->length == 2)) {
872  LOG_ERROR("Invalid breakpoint length %d", breakpoint->length);
873  return ERROR_FAIL;
874  }
875 
876  if (0 != (breakpoint->address % 2)) {
877  LOG_ERROR("Invalid breakpoint alignment for address 0x%" TARGET_PRIxADDR, breakpoint->address);
878  return ERROR_FAIL;
879  }
880 
881  /* Read the original instruction. */
884  LOG_ERROR("Failed to read original instruction at 0x%" TARGET_PRIxADDR,
886  return ERROR_FAIL;
887  }
888 
889  uint8_t buff[4] = { 0 };
890  buf_set_u32(buff, 0, breakpoint->length * CHAR_BIT, breakpoint->length == 4 ? ebreak() : ebreak_c());
891  /* Write the ebreak instruction. */
893  LOG_ERROR("Failed to write %d-byte breakpoint instruction at 0x%"
895  return ERROR_FAIL;
896  }
897 
898  } else if (breakpoint->type == BKPT_HARD) {
899  struct trigger trigger;
901  int const result = add_trigger(target, &trigger);
902  if (result != ERROR_OK)
903  return result;
904  } else {
905  LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
907  }
908 
909  breakpoint->is_set = true;
910  return ERROR_OK;
911 }
912 
913 static int remove_trigger(struct target *target, struct trigger *trigger)
914 {
915  RISCV_INFO(r);
916 
918  return ERROR_FAIL;
919 
920  unsigned int i;
921  for (i = 0; i < r->trigger_count; i++) {
922  if (r->trigger_unique_id[i] == trigger->unique_id)
923  break;
924  }
925  if (i >= r->trigger_count) {
926  LOG_ERROR("Couldn't find the hardware resources used by hardware "
927  "trigger.");
928  return ERROR_FAIL;
929  }
930  LOG_DEBUG("[%d] Stop using resource %d for bp %d", target->coreid, i,
931  trigger->unique_id);
932 
933  riscv_reg_t tselect;
934  int result = riscv_get_register(target, &tselect, GDB_REGNO_TSELECT);
935  if (result != ERROR_OK)
936  return result;
940  r->trigger_unique_id[i] = -1;
941 
942  return ERROR_OK;
943 }
944 
946  struct breakpoint *breakpoint)
947 {
948  if (breakpoint->type == BKPT_SOFT) {
949  /* Write the original instruction. */
952  LOG_ERROR("Failed to restore instruction for %d-byte breakpoint at "
954  return ERROR_FAIL;
955  }
956 
957  } else if (breakpoint->type == BKPT_HARD) {
958  struct trigger trigger;
960  int result = remove_trigger(target, &trigger);
961  if (result != ERROR_OK)
962  return result;
963 
964  } else {
965  LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
967  }
968 
969  breakpoint->is_set = false;
970 
971  return ERROR_OK;
972 }
973 
975  const struct watchpoint *watchpoint)
976 {
983  trigger->execute = false;
984  /* unique_id is unique across both breakpoints and watchpoints. */
986 }
987 
989 {
990  struct trigger trigger;
992 
993  int result = add_trigger(target, &trigger);
994  if (result != ERROR_OK)
995  return result;
996  watchpoint->is_set = true;
997 
998  return ERROR_OK;
999 }
1000 
1002  struct watchpoint *watchpoint)
1003 {
1005 
1006  struct trigger trigger;
1008 
1009  int result = remove_trigger(target, &trigger);
1010  if (result != ERROR_OK)
1011  return result;
1012  watchpoint->is_set = false;
1013 
1014  return ERROR_OK;
1015 }
1016 
1017 /* Sets *hit_watchpoint to the first watchpoint identified as causing the
1018  * current halt.
1019  *
1020  * The GDB server uses this information to tell GDB what data address has
1021  * been hit, which enables GDB to print the hit variable along with its old
1022  * and new value. */
1023 static int riscv_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
1024 {
1025  struct watchpoint *wp = target->watchpoints;
1026 
1027  LOG_DEBUG("Current hartid = %d", riscv_current_hartid(target));
1028 
1029  /*TODO instead of disassembling the instruction that we think caused the
1030  * trigger, check the hit bit of each watchpoint first. The hit bit is
1031  * simpler and more reliable to check but as it is optional and relatively
1032  * new, not all hardware will implement it */
1033  riscv_reg_t dpc;
1035  const uint8_t length = 4;
1036  LOG_DEBUG("dpc is 0x%" PRIx64, dpc);
1037 
1038  /* fetch the instruction at dpc */
1039  uint8_t buffer[length];
1040  if (target_read_buffer(target, dpc, length, buffer) != ERROR_OK) {
1041  LOG_ERROR("Failed to read instruction at dpc 0x%" PRIx64, dpc);
1042  return ERROR_FAIL;
1043  }
1044 
1045  uint32_t instruction = 0;
1046 
1047  for (int i = 0; i < length; i++) {
1048  LOG_DEBUG("Next byte is %x", buffer[i]);
1049  instruction += (buffer[i] << 8 * i);
1050  }
1051  LOG_DEBUG("Full instruction is %x", instruction);
1052 
1053  /* find out which memory address is accessed by the instruction at dpc */
1054  /* opcode is first 7 bits of the instruction */
1055  uint8_t opcode = instruction & 0x7F;
1056  uint32_t rs1;
1057  int16_t imm;
1058  riscv_reg_t mem_addr;
1059 
1060  if (opcode == MATCH_LB || opcode == MATCH_SB) {
1061  rs1 = (instruction & 0xf8000) >> 15;
1062  riscv_get_register(target, &mem_addr, rs1);
1063 
1064  if (opcode == MATCH_SB) {
1065  LOG_DEBUG("%x is store instruction", instruction);
1066  imm = ((instruction & 0xf80) >> 7) | ((instruction & 0xfe000000) >> 20);
1067  } else {
1068  LOG_DEBUG("%x is load instruction", instruction);
1069  imm = (instruction & 0xfff00000) >> 20;
1070  }
1071  /* sign extend 12-bit imm to 16-bits */
1072  if (imm & (1 << 11))
1073  imm |= 0xf000;
1074  mem_addr += imm;
1075  LOG_DEBUG("memory address=0x%" PRIx64, mem_addr);
1076  } else {
1077  LOG_DEBUG("%x is not a RV32I load or store", instruction);
1078  return ERROR_FAIL;
1079  }
1080 
1081  while (wp) {
1082  /*TODO support length/mask */
1083  if (wp->address == mem_addr) {
1084  *hit_watchpoint = wp;
1085  LOG_DEBUG("Hit address=%" TARGET_PRIxADDR, wp->address);
1086  return ERROR_OK;
1087  }
1088  wp = wp->next;
1089  }
1090 
1091  /* No match found - either we hit a watchpoint caused by an instruction that
1092  * this function does not yet disassemble, or we hit a breakpoint.
1093  *
1094  * OpenOCD will behave as if this function had never been implemented i.e.
1095  * report the halt to GDB with no address information. */
1096  return ERROR_FAIL;
1097 }
1098 
1099 static int oldriscv_step(struct target *target, bool current, uint32_t address,
1100  bool handle_breakpoints)
1101 {
1102  struct target_type *tt = get_target_type(target);
1103  return tt->step(target, current, address, handle_breakpoints);
1104 }
1105 
1106 static int old_or_new_riscv_step(struct target *target, bool current,
1107  target_addr_t address, bool handle_breakpoints)
1108 {
1109  RISCV_INFO(r);
1110  LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints);
1111  if (!r->is_halted)
1112  return oldriscv_step(target, current, address, handle_breakpoints);
1113  else
1114  return riscv_openocd_step(target, current, address, handle_breakpoints);
1115 }
1116 
1117 static int riscv_examine(struct target *target)
1118 {
1119  LOG_DEBUG("riscv_examine()");
1120  if (target_was_examined(target)) {
1121  LOG_DEBUG("Target was already examined.");
1122  return ERROR_OK;
1123  }
1124 
1125  /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
1126 
1127  RISCV_INFO(info);
1128  uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
1129  LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol);
1130  info->dtm_version = get_field(dtmcontrol, DTMCONTROL_VERSION);
1131  LOG_DEBUG(" version=0x%x", info->dtm_version);
1132 
1133  struct target_type *tt = get_target_type(target);
1134  if (!tt)
1135  return ERROR_FAIL;
1136 
1137  int result = tt->init_target(info->cmd_ctx, target);
1138  if (result != ERROR_OK)
1139  return result;
1140 
1141  return tt->examine(target);
1142 }
1143 
1144 static int oldriscv_poll(struct target *target)
1145 {
1146  struct target_type *tt = get_target_type(target);
1147  return tt->poll(target);
1148 }
1149 
1151 {
1152  RISCV_INFO(r);
1153  if (!r->is_halted)
1154  return oldriscv_poll(target);
1155  else
1156  return riscv_openocd_poll(target);
1157 }
1158 
1160 {
1162 }
1163 
1164 static int halt_prep(struct target *target)
1165 {
1166  RISCV_INFO(r);
1167 
1168  LOG_DEBUG("[%s] prep hart, debug_reason=%d", target_name(target),
1169  target->debug_reason);
1171  return ERROR_FAIL;
1172  if (riscv_is_halted(target)) {
1173  LOG_DEBUG("[%s] Hart is already halted (reason=%d).",
1175  } else {
1176  if (r->halt_prep(target) != ERROR_OK)
1177  return ERROR_FAIL;
1178  r->prepped = true;
1179  }
1180 
1181  return ERROR_OK;
1182 }
1183 
1185 {
1186  RISCV_INFO(r);
1187 
1189  return ERROR_FAIL;
1190  if (riscv_is_halted(target)) {
1191  LOG_DEBUG("[%s] Hart is already halted.", target_name(target));
1192  } else {
1193  if (r->halt_go(target) != ERROR_OK)
1194  return ERROR_FAIL;
1195  }
1196 
1198 
1199  return ERROR_OK;
1200 }
1201 
1202 static int halt_go(struct target *target)
1203 {
1204  RISCV_INFO(r);
1205  int result;
1206  if (!r->is_halted) {
1207  struct target_type *tt = get_target_type(target);
1208  result = tt->halt(target);
1209  } else {
1210  result = riscv_halt_go_all_harts(target);
1211  }
1215 
1216  return result;
1217 }
1218 
1219 static int halt_finish(struct target *target)
1220 {
1222 }
1223 
1225 {
1226  RISCV_INFO(r);
1227 
1228  if (!r->is_halted) {
1229  struct target_type *tt = get_target_type(target);
1230  return tt->halt(target);
1231  }
1232 
1233  LOG_DEBUG("[%d] halting all harts", target->coreid);
1234 
1235  int result = ERROR_OK;
1236  if (target->smp) {
1237  struct target_list *tlist;
1239  struct target *t = tlist->target;
1240  if (halt_prep(t) != ERROR_OK)
1241  result = ERROR_FAIL;
1242  }
1243 
1245  struct target *t = tlist->target;
1246  struct riscv_info *i = riscv_info(t);
1247  if (i->prepped) {
1248  if (halt_go(t) != ERROR_OK)
1249  result = ERROR_FAIL;
1250  }
1251  }
1252 
1254  struct target *t = tlist->target;
1255  if (halt_finish(t) != ERROR_OK)
1256  return ERROR_FAIL;
1257  }
1258 
1259  } else {
1260  if (halt_prep(target) != ERROR_OK)
1261  result = ERROR_FAIL;
1262  if (halt_go(target) != ERROR_OK)
1263  result = ERROR_FAIL;
1264  if (halt_finish(target) != ERROR_OK)
1265  return ERROR_FAIL;
1266  }
1267 
1268  return result;
1269 }
1270 
1271 static int riscv_assert_reset(struct target *target)
1272 {
1273  LOG_DEBUG("[%d]", target->coreid);
1274  struct target_type *tt = get_target_type(target);
1276  return tt->assert_reset(target);
1277 }
1278 
1280 {
1281  LOG_DEBUG("[%d]", target->coreid);
1282  struct target_type *tt = get_target_type(target);
1283  return tt->deassert_reset(target);
1284 }
1285 
1287 {
1288  RISCV_INFO(r);
1289 
1290  LOG_DEBUG("[%s] prep hart", target_name(target));
1292  return ERROR_FAIL;
1293  if (riscv_is_halted(target)) {
1294  if (r->resume_prep(target) != ERROR_OK)
1295  return ERROR_FAIL;
1296  } else {
1297  LOG_DEBUG("[%s] hart requested resume, but was already resumed",
1298  target_name(target));
1299  }
1300 
1301  LOG_DEBUG("[%s] mark as prepped", target_name(target));
1302  r->prepped = true;
1303 
1304  return ERROR_OK;
1305 }
1306 
1307 /* state must be riscv_reg_t state[RISCV_MAX_HWBPS] = {0}; */
1309 {
1310  RISCV_INFO(r);
1311 
1312  LOG_DEBUG("deal with triggers");
1313 
1315  return ERROR_FAIL;
1316 
1317  if (r->manual_hwbp_set) {
1318  /* Look at every trigger that may have been set. */
1319  riscv_reg_t tselect;
1321  return ERROR_FAIL;
1322  for (unsigned int t = 0; t < r->trigger_count; t++) {
1324  return ERROR_FAIL;
1325  riscv_reg_t tdata1;
1327  return ERROR_FAIL;
1328  if (tdata1 & MCONTROL_DMODE(riscv_xlen(target))) {
1329  state[t] = tdata1;
1331  return ERROR_FAIL;
1332  }
1333  }
1335  return ERROR_FAIL;
1336 
1337  } else {
1338  /* Just go through the triggers we manage. */
1340  int i = 0;
1341  while (watchpoint) {
1342  LOG_DEBUG("watchpoint %d: set=%d", i, watchpoint->is_set);
1343  state[i] = watchpoint->is_set;
1344  if (watchpoint->is_set) {
1346  return ERROR_FAIL;
1347  }
1349  i++;
1350  }
1351  }
1352 
1353  return ERROR_OK;
1354 }
1355 
1357 {
1358  RISCV_INFO(r);
1359 
1360  if (r->manual_hwbp_set) {
1361  /* Look at every trigger that may have been set. */
1362  riscv_reg_t tselect;
1364  return ERROR_FAIL;
1365  for (unsigned int t = 0; t < r->trigger_count; t++) {
1366  if (state[t] != 0) {
1368  return ERROR_FAIL;
1370  return ERROR_FAIL;
1371  }
1372  }
1374  return ERROR_FAIL;
1375 
1376  } else {
1378  int i = 0;
1379  while (watchpoint) {
1380  LOG_DEBUG("watchpoint %d: cleared=%" PRId64, i, state[i]);
1381  if (state[i]) {
1383  return ERROR_FAIL;
1384  }
1386  i++;
1387  }
1388  }
1389 
1390  return ERROR_OK;
1391 }
1392 
1396 static int resume_prep(struct target *target, bool current,
1397  target_addr_t address, bool handle_breakpoints, bool debug_execution)
1398 {
1399  RISCV_INFO(r);
1400  LOG_DEBUG("[%d]", target->coreid);
1401 
1402  if (!current)
1404 
1406  /* To be able to run off a trigger, disable all the triggers, step, and
1407  * then resume as usual. */
1408  riscv_reg_t trigger_state[RISCV_MAX_HWBPS] = {0};
1409 
1410  if (disable_triggers(target, trigger_state) != ERROR_OK)
1411  return ERROR_FAIL;
1412 
1413  if (old_or_new_riscv_step(target, true, 0, false) != ERROR_OK)
1414  return ERROR_FAIL;
1415 
1416  if (enable_triggers(target, trigger_state) != ERROR_OK)
1417  return ERROR_FAIL;
1418  }
1419 
1420  if (r->is_halted) {
1422  return ERROR_FAIL;
1423  }
1424 
1425  LOG_DEBUG("[%d] mark as prepped", target->coreid);
1426  r->prepped = true;
1427 
1428  return ERROR_OK;
1429 }
1430 
1435 static int resume_go(struct target *target, bool current,
1436  target_addr_t address, bool handle_breakpoints, bool debug_execution)
1437 {
1438  RISCV_INFO(r);
1439  int result;
1440  if (!r->is_halted) {
1441  struct target_type *tt = get_target_type(target);
1442  result = tt->resume(target, current, address, handle_breakpoints,
1443  debug_execution);
1444  } else {
1446  }
1447 
1448  return result;
1449 }
1450 
1451 static int resume_finish(struct target *target)
1452 {
1454 
1458 }
1459 
1464 static int riscv_resume(
1465  struct target *target,
1466  bool current,
1468  bool handle_breakpoints,
1469  bool debug_execution,
1470  bool single_hart)
1471 {
1472  LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints);
1473  int result = ERROR_OK;
1474  if (target->smp && !single_hart) {
1475  struct target_list *tlist;
1477  tlist, target->smp_targets) {
1478  struct target *t = tlist->target;
1479  if (resume_prep(t, current, address, handle_breakpoints,
1480  debug_execution) != ERROR_OK)
1481  result = ERROR_FAIL;
1482  }
1483 
1485  tlist, target->smp_targets) {
1486  struct target *t = tlist->target;
1487  struct riscv_info *i = riscv_info(t);
1488  if (i->prepped) {
1489  if (resume_go(t, current, address, handle_breakpoints,
1490  debug_execution) != ERROR_OK)
1491  result = ERROR_FAIL;
1492  }
1493  }
1494 
1496  tlist, target->smp_targets) {
1497  struct target *t = tlist->target;
1498  if (resume_finish(t) != ERROR_OK)
1499  return ERROR_FAIL;
1500  }
1501 
1502  } else {
1503  if (resume_prep(target, current, address, handle_breakpoints,
1504  debug_execution) != ERROR_OK)
1505  result = ERROR_FAIL;
1506  if (resume_go(target, current, address, handle_breakpoints,
1507  debug_execution) != ERROR_OK)
1508  result = ERROR_FAIL;
1509  if (resume_finish(target) != ERROR_OK)
1510  return ERROR_FAIL;
1511  }
1512 
1513  return result;
1514 }
1515 
1516 static int riscv_target_resume(struct target *target, bool current,
1517  target_addr_t address, bool handle_breakpoints, bool debug_execution)
1518 {
1519  return riscv_resume(target, current, address, handle_breakpoints,
1520  debug_execution, false);
1521 }
1522 
1523 static int riscv_mmu(struct target *target, int *enabled)
1524 {
1525  if (!riscv_enable_virt2phys) {
1526  *enabled = 0;
1527  return ERROR_OK;
1528  }
1529 
1530  /* Don't use MMU in explicit or effective M (machine) mode */
1531  riscv_reg_t priv;
1533  LOG_ERROR("Failed to read priv register.");
1534  return ERROR_FAIL;
1535  }
1536 
1537  riscv_reg_t mstatus;
1538  if (riscv_get_register(target, &mstatus, GDB_REGNO_MSTATUS) != ERROR_OK) {
1539  LOG_ERROR("Failed to read mstatus register.");
1540  return ERROR_FAIL;
1541  }
1542 
1543  if ((get_field(mstatus, MSTATUS_MPRV) ? get_field(mstatus, MSTATUS_MPP) : priv) == PRV_M) {
1544  LOG_DEBUG("SATP/MMU ignored in Machine mode (mstatus=0x%" PRIx64 ").", mstatus);
1545  *enabled = 0;
1546  return ERROR_OK;
1547  }
1548 
1549  riscv_reg_t satp;
1551  LOG_DEBUG("Couldn't read SATP.");
1552  /* If we can't read SATP, then there must not be an MMU. */
1553  *enabled = 0;
1554  return ERROR_OK;
1555  }
1556 
1558  LOG_DEBUG("MMU is disabled.");
1559  *enabled = 0;
1560  } else {
1561  LOG_DEBUG("MMU is enabled.");
1562  *enabled = 1;
1563  }
1564 
1565  return ERROR_OK;
1566 }
1567 
1569  target_addr_t virtual, target_addr_t *physical)
1570 {
1571  RISCV_INFO(r);
1572  riscv_reg_t satp_value;
1573  int mode;
1574  uint64_t ppn_value;
1575  target_addr_t table_address;
1576  const virt2phys_info_t *info;
1577  uint64_t pte = 0;
1578  int i;
1579 
1580  int result = riscv_get_register(target, &satp_value, GDB_REGNO_SATP);
1581  if (result != ERROR_OK)
1582  return result;
1583 
1584  unsigned int xlen = riscv_xlen(target);
1585  mode = get_field(satp_value, RISCV_SATP_MODE(xlen));
1586  switch (mode) {
1587  case SATP_MODE_SV32:
1588  info = &sv32;
1589  break;
1590  case SATP_MODE_SV39:
1591  info = &sv39;
1592  break;
1593  case SATP_MODE_SV48:
1594  info = &sv48;
1595  break;
1596  case SATP_MODE_OFF:
1597  LOG_ERROR("No translation or protection." \
1598  " (satp: 0x%" PRIx64 ")", satp_value);
1599  return ERROR_FAIL;
1600  default:
1601  LOG_ERROR("The translation mode is not supported." \
1602  " (satp: 0x%" PRIx64 ")", satp_value);
1603  return ERROR_FAIL;
1604  }
1605  LOG_DEBUG("virtual=0x%" TARGET_PRIxADDR "; mode=%s", virtual, info->name);
1606 
1607  /* verify bits xlen-1:va_bits-1 are all equal */
1608  assert(xlen >= info->va_bits);
1609  target_addr_t mask = ((target_addr_t)1 << (xlen - (info->va_bits - 1))) - 1;
1610  target_addr_t masked_msbs = (virtual >> (info->va_bits - 1)) & mask;
1611  if (masked_msbs != 0 && masked_msbs != mask) {
1612  LOG_ERROR("Virtual address 0x%" TARGET_PRIxADDR " is not sign-extended "
1613  "for %s mode.", virtual, info->name);
1614  return ERROR_FAIL;
1615  }
1616 
1617  ppn_value = get_field(satp_value, RISCV_SATP_PPN(xlen));
1618  table_address = ppn_value << RISCV_PGSHIFT;
1619  i = info->level - 1;
1620  while (i >= 0) {
1621  uint64_t vpn = virtual >> info->vpn_shift[i];
1622  vpn &= info->vpn_mask[i];
1623  target_addr_t pte_address = table_address +
1624  (vpn << info->pte_shift);
1625  uint8_t buffer[8];
1626  assert(info->pte_shift <= 3);
1627  int retval = r->read_memory(target, pte_address,
1628  4, (1 << info->pte_shift) / 4, buffer, 4);
1629  if (retval != ERROR_OK)
1630  return ERROR_FAIL;
1631 
1632  if (info->pte_shift == 2)
1633  pte = buf_get_u32(buffer, 0, 32);
1634  else
1635  pte = buf_get_u64(buffer, 0, 64);
1636 
1637  LOG_DEBUG("i=%d; PTE @0x%" TARGET_PRIxADDR " = 0x%" PRIx64, i,
1638  pte_address, pte);
1639 
1640  if (!(pte & PTE_V) || (!(pte & PTE_R) && (pte & PTE_W)))
1641  return ERROR_FAIL;
1642 
1643  if ((pte & PTE_R) || (pte & PTE_X)) /* Found leaf PTE. */
1644  break;
1645 
1646  i--;
1647  if (i < 0)
1648  break;
1649  ppn_value = pte >> PTE_PPN_SHIFT;
1650  table_address = ppn_value << RISCV_PGSHIFT;
1651  }
1652 
1653  if (i < 0) {
1654  LOG_ERROR("Couldn't find the PTE.");
1655  return ERROR_FAIL;
1656  }
1657 
1658  /* Make sure to clear out the high bits that may be set. */
1659  *physical = virtual & (((target_addr_t)1 << info->va_bits) - 1);
1660 
1661  while (i < info->level) {
1662  ppn_value = pte >> info->pte_ppn_shift[i];
1663  ppn_value &= info->pte_ppn_mask[i];
1664  *physical &= ~(((target_addr_t)info->pa_ppn_mask[i]) <<
1665  info->pa_ppn_shift[i]);
1666  *physical |= (ppn_value << info->pa_ppn_shift[i]);
1667  i++;
1668  }
1669  LOG_DEBUG("0x%" TARGET_PRIxADDR " -> 0x%" TARGET_PRIxADDR, virtual,
1670  *physical);
1671 
1672  return ERROR_OK;
1673 }
1674 
1675 static int riscv_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
1676 {
1677  int enabled;
1678  if (riscv_mmu(target, &enabled) == ERROR_OK) {
1679  if (!enabled)
1680  return ERROR_FAIL;
1681 
1682  if (riscv_address_translate(target, virtual, physical) == ERROR_OK)
1683  return ERROR_OK;
1684  }
1685 
1686  return ERROR_FAIL;
1687 }
1688 
1689 static int riscv_read_phys_memory(struct target *target, target_addr_t phys_address,
1690  uint32_t size, uint32_t count, uint8_t *buffer)
1691 {
1692  RISCV_INFO(r);
1694  return ERROR_FAIL;
1695  return r->read_memory(target, phys_address, size, count, buffer, size);
1696 }
1697 
1699  uint32_t size, uint32_t count, uint8_t *buffer)
1700 {
1701  if (count == 0) {
1702  LOG_WARNING("0-length read from 0x%" TARGET_PRIxADDR, address);
1703  return ERROR_OK;
1704  }
1705 
1707  return ERROR_FAIL;
1708 
1709  target_addr_t physical_addr;
1710  if (target->type->virt2phys(target, address, &physical_addr) == ERROR_OK)
1711  address = physical_addr;
1712 
1713  RISCV_INFO(r);
1714  return r->read_memory(target, address, size, count, buffer, size);
1715 }
1716 
1717 static int riscv_write_phys_memory(struct target *target, target_addr_t phys_address,
1718  uint32_t size, uint32_t count, const uint8_t *buffer)
1719 {
1721  return ERROR_FAIL;
1722  struct target_type *tt = get_target_type(target);
1723  return tt->write_memory(target, phys_address, size, count, buffer);
1724 }
1725 
1727  uint32_t size, uint32_t count, const uint8_t *buffer)
1728 {
1729  if (count == 0) {
1730  LOG_WARNING("0-length write to 0x%" TARGET_PRIxADDR, address);
1731  return ERROR_OK;
1732  }
1733 
1735  return ERROR_FAIL;
1736 
1737  target_addr_t physical_addr;
1738  if (target->type->virt2phys(target, address, &physical_addr) == ERROR_OK)
1739  address = physical_addr;
1740 
1741  struct target_type *tt = get_target_type(target);
1742  return tt->write_memory(target, address, size, count, buffer);
1743 }
1744 
1745 static const char *riscv_get_gdb_arch(const struct target *target)
1746 {
1747  switch (riscv_xlen(target)) {
1748  case 32:
1749  return "riscv:rv32";
1750  case 64:
1751  return "riscv:rv64";
1752  }
1753  LOG_ERROR("Unsupported xlen: %d", riscv_xlen(target));
1754  return NULL;
1755 }
1756 
1758  struct reg **reg_list[], int *reg_list_size,
1759  enum target_register_class reg_class, bool read)
1760 {
1761  RISCV_INFO(r);
1762  LOG_DEBUG("[%s] {%d} reg_class=%d, read=%d",
1763  target_name(target), r->current_hartid, reg_class, read);
1764 
1765  if (!target->reg_cache) {
1766  LOG_ERROR("Target not initialized. Return ERROR_FAIL.");
1767  return ERROR_FAIL;
1768  }
1769 
1771  return ERROR_FAIL;
1772 
1773  switch (reg_class) {
1774  case REG_CLASS_GENERAL:
1775  *reg_list_size = 33;
1776  break;
1777  case REG_CLASS_ALL:
1778  *reg_list_size = target->reg_cache->num_regs;
1779  break;
1780  default:
1781  LOG_ERROR("Unsupported reg_class: %d", reg_class);
1782  return ERROR_FAIL;
1783  }
1784 
1785  *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
1786  if (!*reg_list)
1787  return ERROR_FAIL;
1788 
1789  for (int i = 0; i < *reg_list_size; i++) {
1790  assert(!target->reg_cache->reg_list[i].valid ||
1791  target->reg_cache->reg_list[i].size > 0);
1792  (*reg_list)[i] = &target->reg_cache->reg_list[i];
1793  if (read &&
1794  target->reg_cache->reg_list[i].exist &&
1795  !target->reg_cache->reg_list[i].valid) {
1796  if (target->reg_cache->reg_list[i].type->get(
1797  &target->reg_cache->reg_list[i]) != ERROR_OK)
1798  return ERROR_FAIL;
1799  }
1800  }
1801 
1802  return ERROR_OK;
1803 }
1804 
1806  struct reg **reg_list[], int *reg_list_size,
1807  enum target_register_class reg_class)
1808 {
1809  return riscv_get_gdb_reg_list_internal(target, reg_list, reg_list_size,
1810  reg_class, false);
1811 }
1812 
1814  struct reg **reg_list[], int *reg_list_size,
1815  enum target_register_class reg_class)
1816 {
1817  return riscv_get_gdb_reg_list_internal(target, reg_list, reg_list_size,
1818  reg_class, true);
1819 }
1820 
1821 static int riscv_arch_state(struct target *target)
1822 {
1823  struct target_type *tt = get_target_type(target);
1824  return tt->arch_state(target);
1825 }
1826 
1827 /* Algorithm must end with a software breakpoint instruction. */
1828 static int riscv_run_algorithm(struct target *target, int num_mem_params,
1829  struct mem_param *mem_params, int num_reg_params,
1830  struct reg_param *reg_params, target_addr_t entry_point,
1831  target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
1832 {
1833  RISCV_INFO(info);
1834 
1835  if (num_mem_params > 0) {
1836  LOG_ERROR("Memory parameters are not supported for RISC-V algorithms.");
1837  return ERROR_FAIL;
1838  }
1839 
1840  if (target->state != TARGET_HALTED) {
1841  LOG_TARGET_ERROR(target, "not halted (run target algo)");
1842  return ERROR_TARGET_NOT_HALTED;
1843  }
1844 
1845  /* Save registers */
1846  struct reg *reg_pc = register_get_by_name(target->reg_cache, "pc", true);
1847  if (!reg_pc || reg_pc->type->get(reg_pc) != ERROR_OK)
1848  return ERROR_FAIL;
1849  uint64_t saved_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
1850  LOG_DEBUG("saved_pc=0x%" PRIx64, saved_pc);
1851 
1852  uint64_t saved_regs[32];
1853  for (int i = 0; i < num_reg_params; i++) {
1854  LOG_DEBUG("save %s", reg_params[i].reg_name);
1855  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
1856  if (!r) {
1857  LOG_ERROR("Couldn't find register named '%s'", reg_params[i].reg_name);
1858  return ERROR_FAIL;
1859  }
1860 
1861  if (r->size != reg_params[i].size) {
1862  LOG_ERROR("Register %s is %d bits instead of %d bits.",
1863  reg_params[i].reg_name, r->size, reg_params[i].size);
1864  return ERROR_FAIL;
1865  }
1866 
1867  if (r->number > GDB_REGNO_XPR31) {
1868  LOG_ERROR("Only GPRs can be use as argument registers.");
1869  return ERROR_FAIL;
1870  }
1871 
1872  if (r->type->get(r) != ERROR_OK)
1873  return ERROR_FAIL;
1874  saved_regs[r->number] = buf_get_u64(r->value, 0, r->size);
1875 
1876  if (reg_params[i].direction == PARAM_OUT || reg_params[i].direction == PARAM_IN_OUT) {
1877  if (r->type->set(r, reg_params[i].value) != ERROR_OK)
1878  return ERROR_FAIL;
1879  }
1880  }
1881 
1882 
1883  /* Disable Interrupts before attempting to run the algorithm. */
1884  uint64_t current_mstatus;
1885  uint8_t mstatus_bytes[8] = { 0 };
1886 
1887  LOG_DEBUG("Disabling Interrupts");
1888  struct reg *reg_mstatus = register_get_by_name(target->reg_cache,
1889  "mstatus", true);
1890  if (!reg_mstatus) {
1891  LOG_ERROR("Couldn't find mstatus!");
1892  return ERROR_FAIL;
1893  }
1894 
1895  reg_mstatus->type->get(reg_mstatus);
1896  current_mstatus = buf_get_u64(reg_mstatus->value, 0, reg_mstatus->size);
1897  uint64_t ie_mask = MSTATUS_MIE | MSTATUS_HIE | MSTATUS_SIE | MSTATUS_UIE;
1898  buf_set_u64(mstatus_bytes, 0, info->xlen, set_field(current_mstatus,
1899  ie_mask, 0));
1900 
1901  reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
1902 
1903  /* Run algorithm */
1904  LOG_DEBUG("resume at 0x%" TARGET_PRIxADDR, entry_point);
1905  if (riscv_resume(target, false, entry_point, false, false, true) != ERROR_OK)
1906  return ERROR_FAIL;
1907 
1908  int64_t start = timeval_ms();
1909  while (target->state != TARGET_HALTED) {
1910  LOG_DEBUG("poll()");
1911  int64_t now = timeval_ms();
1912  if (now - start > timeout_ms) {
1913  LOG_ERROR("Algorithm timed out after %" PRId64 " ms.", now - start);
1914  riscv_halt(target);
1916  enum gdb_regno regnums[] = {
1925  GDB_REGNO_PC,
1927  };
1928  for (unsigned int i = 0; i < ARRAY_SIZE(regnums); i++) {
1929  enum gdb_regno regno = regnums[i];
1930  riscv_reg_t reg_value;
1931  if (riscv_get_register(target, &reg_value, regno) != ERROR_OK)
1932  break;
1933  LOG_ERROR("%s = 0x%" PRIx64, gdb_regno_name(regno), reg_value);
1934  }
1935  return ERROR_TARGET_TIMEOUT;
1936  }
1937 
1938  int result = old_or_new_riscv_poll(target);
1939  if (result != ERROR_OK)
1940  return result;
1941  }
1942 
1943  /* The current hart id might have been changed in poll(). */
1945  return ERROR_FAIL;
1946 
1947  if (reg_pc->type->get(reg_pc) != ERROR_OK)
1948  return ERROR_FAIL;
1949  uint64_t final_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
1950  if (exit_point && final_pc != exit_point) {
1951  LOG_ERROR("PC ended up at 0x%" PRIx64 " instead of 0x%"
1952  TARGET_PRIxADDR, final_pc, exit_point);
1953  return ERROR_FAIL;
1954  }
1955 
1956  /* Restore Interrupts */
1957  LOG_DEBUG("Restoring Interrupts");
1958  buf_set_u64(mstatus_bytes, 0, info->xlen, current_mstatus);
1959  reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
1960 
1961  /* Restore registers */
1962  uint8_t buf[8] = { 0 };
1963  buf_set_u64(buf, 0, info->xlen, saved_pc);
1964  if (reg_pc->type->set(reg_pc, buf) != ERROR_OK)
1965  return ERROR_FAIL;
1966 
1967  for (int i = 0; i < num_reg_params; i++) {
1968  if (reg_params[i].direction == PARAM_IN ||
1969  reg_params[i].direction == PARAM_IN_OUT) {
1970  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
1971  if (r->type->get(r) != ERROR_OK) {
1972  LOG_ERROR("get(%s) failed", r->name);
1973  return ERROR_FAIL;
1974  }
1975  buf_cpy(r->value, reg_params[i].value, reg_params[i].size);
1976  }
1977  LOG_DEBUG("restore %s", reg_params[i].reg_name);
1978  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
1979  buf_set_u64(buf, 0, info->xlen, saved_regs[r->number]);
1980  if (r->type->set(r, buf) != ERROR_OK) {
1981  LOG_ERROR("set(%s) failed", r->name);
1982  return ERROR_FAIL;
1983  }
1984  }
1985 
1986  return ERROR_OK;
1987 }
1988 
1990  target_addr_t address, uint32_t count,
1991  uint32_t *checksum)
1992 {
1993  struct working_area *crc_algorithm;
1994  struct reg_param reg_params[2];
1995  int retval;
1996 
1997  LOG_DEBUG("address=0x%" TARGET_PRIxADDR "; count=0x%" PRIx32, address, count);
1998 
1999  static const uint8_t riscv32_crc_code[] = {
2000 #include "../../../contrib/loaders/checksum/riscv32_crc.inc"
2001  };
2002  static const uint8_t riscv64_crc_code[] = {
2003 #include "../../../contrib/loaders/checksum/riscv64_crc.inc"
2004  };
2005 
2006  static const uint8_t *crc_code;
2007 
2008  unsigned int xlen = riscv_xlen(target);
2009  unsigned int crc_code_size;
2010  if (xlen == 32) {
2011  crc_code = riscv32_crc_code;
2012  crc_code_size = sizeof(riscv32_crc_code);
2013  } else {
2014  crc_code = riscv64_crc_code;
2015  crc_code_size = sizeof(riscv64_crc_code);
2016  }
2017 
2018  if (count < crc_code_size * 4) {
2019  /* Don't use the algorithm for relatively small buffers. It's faster
2020  * just to read the memory. target_checksum_memory() will take care of
2021  * that if we fail. */
2022  return ERROR_FAIL;
2023  }
2024 
2025  retval = target_alloc_working_area(target, crc_code_size, &crc_algorithm);
2026  if (retval != ERROR_OK)
2027  return retval;
2028 
2029  if (crc_algorithm->address + crc_algorithm->size > address &&
2030  crc_algorithm->address < address + count) {
2031  /* Region to checksum overlaps with the work area we've been assigned.
2032  * Bail. (Would be better to manually checksum what we read there, and
2033  * use the algorithm for the rest.) */
2034  target_free_working_area(target, crc_algorithm);
2035  return ERROR_FAIL;
2036  }
2037 
2038  retval = target_write_buffer(target, crc_algorithm->address, crc_code_size,
2039  crc_code);
2040  if (retval != ERROR_OK) {
2041  LOG_ERROR("Failed to write code to " TARGET_ADDR_FMT ": %d",
2042  crc_algorithm->address, retval);
2043  target_free_working_area(target, crc_algorithm);
2044  return retval;
2045  }
2046 
2047  init_reg_param(&reg_params[0], "a0", xlen, PARAM_IN_OUT);
2048  init_reg_param(&reg_params[1], "a1", xlen, PARAM_OUT);
2049  buf_set_u64(reg_params[0].value, 0, xlen, address);
2050  buf_set_u64(reg_params[1].value, 0, xlen, count);
2051 
2052  /* 20 second timeout/megabyte */
2053  unsigned int timeout = 20000 * (1 + (count / (1024 * 1024)));
2054 
2055  retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
2056  crc_algorithm->address,
2057  0, /* Leave exit point unspecified because we don't know. */
2058  timeout, NULL);
2059 
2060  if (retval == ERROR_OK)
2061  *checksum = buf_get_u32(reg_params[0].value, 0, 32);
2062  else
2063  LOG_ERROR("error executing RISC-V CRC algorithm");
2064 
2065  destroy_reg_param(&reg_params[0]);
2066  destroy_reg_param(&reg_params[1]);
2067 
2068  target_free_working_area(target, crc_algorithm);
2069 
2070  LOG_DEBUG("checksum=0x%" PRIx32 ", result=%d", *checksum, retval);
2071 
2072  return retval;
2073 }
2074 
2075 /*** OpenOCD Helper Functions ***/
2076 
2081  RPH_ERROR
2082 };
2083 static enum riscv_poll_hart riscv_poll_hart(struct target *target, int hartid)
2084 {
2085  RISCV_INFO(r);
2086  if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
2087  return RPH_ERROR;
2088 
2089  LOG_DEBUG("polling hart %d, target->state=%d", hartid, target->state);
2090 
2091  /* If OpenOCD thinks we're running but this hart is halted then it's time
2092  * to raise an event. */
2093  bool halted = riscv_is_halted(target);
2094  if (target->state != TARGET_HALTED && halted) {
2095  LOG_DEBUG(" triggered a halt");
2096  r->on_halt(target);
2097  return RPH_DISCOVERED_HALTED;
2098  } else if (target->state != TARGET_RUNNING && !halted) {
2099  LOG_DEBUG(" triggered running");
2102  return RPH_DISCOVERED_RUNNING;
2103  }
2104 
2105  return RPH_NO_CHANGE;
2106 }
2107 
2108 static int set_debug_reason(struct target *target, enum riscv_halt_reason halt_reason)
2109 {
2110  switch (halt_reason) {
2111  case RISCV_HALT_BREAKPOINT:
2113  break;
2114  case RISCV_HALT_TRIGGER:
2116  break;
2117  case RISCV_HALT_INTERRUPT:
2118  case RISCV_HALT_GROUP:
2120  break;
2121  case RISCV_HALT_SINGLESTEP:
2123  break;
2124  case RISCV_HALT_UNKNOWN:
2126  break;
2127  case RISCV_HALT_ERROR:
2128  return ERROR_FAIL;
2129  }
2130  LOG_DEBUG("[%s] debug_reason=%d", target_name(target), target->debug_reason);
2131  return ERROR_OK;
2132 }
2133 
2134 static int sample_memory(struct target *target)
2135 {
2136  RISCV_INFO(r);
2137 
2138  if (!r->sample_buf.buf || !r->sample_config.enabled)
2139  return ERROR_OK;
2140 
2141  LOG_DEBUG("buf used/size: %d/%d", r->sample_buf.used, r->sample_buf.size);
2142 
2143  uint64_t start = timeval_ms();
2145  int result = ERROR_OK;
2146  if (r->sample_memory) {
2147  result = r->sample_memory(target, &r->sample_buf, &r->sample_config,
2149  if (result != ERROR_NOT_IMPLEMENTED)
2150  goto exit;
2151  }
2152 
2153  /* Default slow path. */
2155  for (unsigned int i = 0; i < ARRAY_SIZE(r->sample_config.bucket); i++) {
2156  if (r->sample_config.bucket[i].enabled &&
2157  r->sample_buf.used + 1 + r->sample_config.bucket[i].size_bytes < r->sample_buf.size) {
2159  r->sample_buf.buf[r->sample_buf.used] = i;
2160  result = riscv_read_phys_memory(
2161  target, r->sample_config.bucket[i].address,
2162  r->sample_config.bucket[i].size_bytes, 1,
2163  r->sample_buf.buf + r->sample_buf.used + 1);
2164  if (result == ERROR_OK)
2165  r->sample_buf.used += 1 + r->sample_config.bucket[i].size_bytes;
2166  else
2167  goto exit;
2168  }
2169  }
2170  }
2171 
2172 exit:
2174  if (result != ERROR_OK) {
2175  LOG_INFO("Turning off memory sampling because it failed.");
2176  r->sample_config.enabled = false;
2177  }
2178  return result;
2179 }
2180 
2181 /*** OpenOCD Interface ***/
2183 {
2184  LOG_DEBUG("polling all harts");
2185  int halted_hart = -1;
2186 
2187  if (target->smp) {
2188  unsigned int should_remain_halted = 0;
2189  unsigned int should_resume = 0;
2190  struct target_list *list;
2192  struct target *t = list->target;
2193  struct riscv_info *r = riscv_info(t);
2195  switch (out) {
2196  case RPH_NO_CHANGE:
2197  break;
2199  t->state = TARGET_RUNNING;
2201  break;
2202  case RPH_DISCOVERED_HALTED:
2203  t->state = TARGET_HALTED;
2207  return ERROR_FAIL;
2208 
2210  int retval;
2211  switch (riscv_semihosting(t, &retval)) {
2212  case SEMIHOSTING_NONE:
2213  case SEMIHOSTING_WAITING:
2214  /* This hart should remain halted. */
2215  should_remain_halted++;
2216  break;
2217  case SEMIHOSTING_HANDLED:
2218  /* This hart should be resumed, along with any other
2219  * harts that halted due to haltgroups. */
2220  should_resume++;
2221  break;
2222  case SEMIHOSTING_ERROR:
2223  return retval;
2224  }
2225  } else if (halt_reason != RISCV_HALT_GROUP) {
2226  should_remain_halted++;
2227  }
2228  break;
2229 
2230  case RPH_ERROR:
2231  return ERROR_FAIL;
2232  }
2233  }
2234 
2235  LOG_DEBUG("should_remain_halted=%d, should_resume=%d",
2236  should_remain_halted, should_resume);
2237  if (should_remain_halted && should_resume) {
2238  LOG_WARNING("%d harts should remain halted, and %d should resume.",
2239  should_remain_halted, should_resume);
2240  }
2241  if (should_remain_halted) {
2242  LOG_DEBUG("halt all");
2243  riscv_halt(target);
2244  } else if (should_resume) {
2245  LOG_DEBUG("resume all");
2246  riscv_resume(target, true, 0, false, false, false);
2247  }
2248 
2249  /* Sample memory if any target is running. */
2251  struct target *t = list->target;
2252  if (t->state == TARGET_RUNNING) {
2254  break;
2255  }
2256  }
2257 
2258  return ERROR_OK;
2259 
2260  } else {
2263  if (out == RPH_NO_CHANGE || out == RPH_DISCOVERED_RUNNING) {
2264  if (target->state == TARGET_RUNNING)
2266  return ERROR_OK;
2267  } else if (out == RPH_ERROR) {
2268  return ERROR_FAIL;
2269  }
2270 
2271  halted_hart = riscv_current_hartid(target);
2272  LOG_DEBUG(" hart %d halted", halted_hart);
2273 
2274  enum riscv_halt_reason halt_reason = riscv_halt_reason(target, halted_hart);
2275  if (set_debug_reason(target, halt_reason) != ERROR_OK)
2276  return ERROR_FAIL;
2278  }
2279 
2281  int retval;
2282  switch (riscv_semihosting(target, &retval)) {
2283  case SEMIHOSTING_NONE:
2284  case SEMIHOSTING_WAITING:
2286  break;
2287  case SEMIHOSTING_HANDLED:
2288  if (riscv_resume(target, true, 0, false, false, false) != ERROR_OK)
2289  return ERROR_FAIL;
2290  break;
2291  case SEMIHOSTING_ERROR:
2292  return retval;
2293  }
2294  } else {
2296  }
2297 
2298  return ERROR_OK;
2299 }
2300 
2301 int riscv_openocd_step(struct target *target, bool current,
2302  target_addr_t address, bool handle_breakpoints)
2303 {
2304  LOG_DEBUG("stepping rtos hart");
2305 
2306  if (!current)
2308 
2309  riscv_reg_t trigger_state[RISCV_MAX_HWBPS] = {0};
2310  if (disable_triggers(target, trigger_state) != ERROR_OK)
2311  return ERROR_FAIL;
2312 
2313  int out = riscv_step_rtos_hart(target);
2314  if (out != ERROR_OK) {
2315  LOG_ERROR("unable to step rtos hart");
2316  return out;
2317  }
2318 
2320 
2321  if (enable_triggers(target, trigger_state) != ERROR_OK)
2322  return ERROR_FAIL;
2323 
2329  return out;
2330 }
2331 
2332 /* Command Handlers */
2333 COMMAND_HANDLER(riscv_set_command_timeout_sec)
2334 {
2335  if (CMD_ARGC != 1) {
2336  LOG_ERROR("Command takes exactly 1 parameter");
2338  }
2339  int timeout = atoi(CMD_ARGV[0]);
2340  if (timeout <= 0) {
2341  LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
2342  return ERROR_FAIL;
2343  }
2344 
2346 
2347  return ERROR_OK;
2348 }
2349 
2350 COMMAND_HANDLER(riscv_set_reset_timeout_sec)
2351 {
2352  if (CMD_ARGC != 1) {
2353  LOG_ERROR("Command takes exactly 1 parameter");
2355  }
2356  int timeout = atoi(CMD_ARGV[0]);
2357  if (timeout <= 0) {
2358  LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
2359  return ERROR_FAIL;
2360  }
2361 
2363  return ERROR_OK;
2364 }
2365 
2366 COMMAND_HANDLER(riscv_set_mem_access)
2367 {
2369  RISCV_INFO(r);
2370  int progbuf_cnt = 0;
2371  int sysbus_cnt = 0;
2372  int abstract_cnt = 0;
2373 
2374  if (CMD_ARGC < 1 || CMD_ARGC > RISCV_NUM_MEM_ACCESS_METHODS) {
2375  LOG_ERROR("Command takes 1 to %d parameters", RISCV_NUM_MEM_ACCESS_METHODS);
2377  }
2378 
2379  /* Check argument validity */
2380  for (unsigned int i = 0; i < CMD_ARGC; i++) {
2381  if (strcmp("progbuf", CMD_ARGV[i]) == 0) {
2382  progbuf_cnt++;
2383  } else if (strcmp("sysbus", CMD_ARGV[i]) == 0) {
2384  sysbus_cnt++;
2385  } else if (strcmp("abstract", CMD_ARGV[i]) == 0) {
2386  abstract_cnt++;
2387  } else {
2388  LOG_ERROR("Unknown argument '%s'. "
2389  "Must be one of: 'progbuf', 'sysbus' or 'abstract'.", CMD_ARGV[i]);
2391  }
2392  }
2393  if (progbuf_cnt > 1 || sysbus_cnt > 1 || abstract_cnt > 1) {
2394  LOG_ERROR("Syntax error - duplicate arguments to `riscv set_mem_access`.");
2396  }
2397 
2398  /* Args are valid, store them */
2399  for (unsigned int i = 0; i < RISCV_NUM_MEM_ACCESS_METHODS; i++)
2400  r->mem_access_methods[i] = RISCV_MEM_ACCESS_UNSPECIFIED;
2401  for (unsigned int i = 0; i < CMD_ARGC; i++) {
2402  if (strcmp("progbuf", CMD_ARGV[i]) == 0)
2403  r->mem_access_methods[i] = RISCV_MEM_ACCESS_PROGBUF;
2404  else if (strcmp("sysbus", CMD_ARGV[i]) == 0)
2405  r->mem_access_methods[i] = RISCV_MEM_ACCESS_SYSBUS;
2406  else if (strcmp("abstract", CMD_ARGV[i]) == 0)
2407  r->mem_access_methods[i] = RISCV_MEM_ACCESS_ABSTRACT;
2408  }
2409 
2410  /* Reset warning flags */
2411  r->mem_access_progbuf_warn = true;
2412  r->mem_access_sysbus_warn = true;
2413  r->mem_access_abstract_warn = true;
2414 
2415  return ERROR_OK;
2416 }
2417 
2418 COMMAND_HANDLER(riscv_set_enable_virtual)
2419 {
2420  if (CMD_ARGC != 1) {
2421  LOG_ERROR("Command takes exactly 1 parameter");
2423  }
2425  return ERROR_OK;
2426 }
2427 
2428 static int parse_ranges(struct list_head *ranges, const char *tcl_arg, const char *reg_type, unsigned int max_val)
2429 {
2430  char *args = strdup(tcl_arg);
2431  if (!args)
2432  return ERROR_FAIL;
2433 
2434  /* For backward compatibility, allow multiple parameters within one TCL argument, separated by ',' */
2435  char *arg = strtok(args, ",");
2436  while (arg) {
2437  unsigned int low = 0;
2438  unsigned int high = 0;
2439  char *name = NULL;
2440 
2441  char *dash = strchr(arg, '-');
2442  char *equals = strchr(arg, '=');
2443  unsigned int pos;
2444 
2445  if (!dash && !equals) {
2446  /* Expecting single register number. */
2447  if (sscanf(arg, "%u%n", &low, &pos) != 1 || pos != strlen(arg)) {
2448  LOG_ERROR("Failed to parse single register number from '%s'.", arg);
2449  free(args);
2451  }
2452  } else if (dash && !equals) {
2453  /* Expecting register range - two numbers separated by a dash: ##-## */
2454  *dash = 0;
2455  dash++;
2456  if (sscanf(arg, "%u%n", &low, &pos) != 1 || pos != strlen(arg)) {
2457  LOG_ERROR("Failed to parse single register number from '%s'.", arg);
2458  free(args);
2460  }
2461  if (sscanf(dash, "%u%n", &high, &pos) != 1 || pos != strlen(dash)) {
2462  LOG_ERROR("Failed to parse single register number from '%s'.", dash);
2463  free(args);
2465  }
2466  if (high < low) {
2467  LOG_ERROR("Incorrect range encountered [%u, %u].", low, high);
2468  free(args);
2469  return ERROR_FAIL;
2470  }
2471  } else if (!dash && equals) {
2472  /* Expecting single register number with textual name specified: ##=name */
2473  *equals = 0;
2474  equals++;
2475  if (sscanf(arg, "%u%n", &low, &pos) != 1 || pos != strlen(arg)) {
2476  LOG_ERROR("Failed to parse single register number from '%s'.", arg);
2477  free(args);
2479  }
2480 
2481  name = calloc(1, strlen(equals) + strlen(reg_type) + 2);
2482  if (!name) {
2483  LOG_ERROR("Failed to allocate register name.");
2484  free(args);
2485  return ERROR_FAIL;
2486  }
2487 
2488  /* Register prefix: "csr_" or "custom_" */
2489  strcpy(name, reg_type);
2490  name[strlen(reg_type)] = '_';
2491 
2492  if (sscanf(equals, "%[_a-zA-Z0-9]%n", name + strlen(reg_type) + 1, &pos) != 1 || pos != strlen(equals)) {
2493  LOG_ERROR("Failed to parse register name from '%s'.", equals);
2494  free(args);
2495  free(name);
2497  }
2498  } else {
2499  LOG_ERROR("Invalid argument '%s'.", arg);
2500  free(args);
2502  }
2503 
2504  high = high > low ? high : low;
2505 
2506  if (high > max_val) {
2507  LOG_ERROR("Cannot expose %s register number %u, maximum allowed value is %u.", reg_type, high, max_val);
2508  free(name);
2509  free(args);
2510  return ERROR_FAIL;
2511  }
2512 
2513  /* Check for overlap, name uniqueness. */
2514  range_list_t *entry;
2515  list_for_each_entry(entry, ranges, list) {
2516  if ((entry->low <= high) && (low <= entry->high)) {
2517  if (low == high)
2518  LOG_WARNING("Duplicate %s register number - "
2519  "Register %u has already been exposed previously", reg_type, low);
2520  else
2521  LOG_WARNING("Overlapping register ranges - Register range starting from %u overlaps "
2522  "with already exposed register/range at %u.", low, entry->low);
2523  }
2524 
2525  if (entry->name && name && (strcasecmp(entry->name, name) == 0)) {
2526  LOG_ERROR("Duplicate register name \"%s\" found.", name);
2527  free(name);
2528  free(args);
2529  return ERROR_FAIL;
2530  }
2531  }
2532 
2533  range_list_t *range = calloc(1, sizeof(range_list_t));
2534  if (!range) {
2535  LOG_ERROR("Failed to allocate range list.");
2536  free(name);
2537  free(args);
2538  return ERROR_FAIL;
2539  }
2540 
2541  range->low = low;
2542  range->high = high;
2543  range->name = name;
2544  list_add(&range->list, ranges);
2545 
2546  arg = strtok(NULL, ",");
2547  }
2548 
2549  free(args);
2550  return ERROR_OK;
2551 }
2552 
2553 COMMAND_HANDLER(riscv_set_expose_csrs)
2554 {
2555  if (CMD_ARGC == 0) {
2556  LOG_ERROR("Command expects parameters");
2558  }
2559 
2561  RISCV_INFO(info);
2562  int ret = ERROR_OK;
2563 
2564  for (unsigned int i = 0; i < CMD_ARGC; i++) {
2565  ret = parse_ranges(&info->expose_csr, CMD_ARGV[i], "csr", 0xfff);
2566  if (ret != ERROR_OK)
2567  break;
2568  }
2569 
2570  return ret;
2571 }
2572 
2573 COMMAND_HANDLER(riscv_set_expose_custom)
2574 {
2575  if (CMD_ARGC == 0) {
2576  LOG_ERROR("Command expects parameters");
2578  }
2579 
2581  RISCV_INFO(info);
2582  int ret = ERROR_OK;
2583 
2584  for (unsigned int i = 0; i < CMD_ARGC; i++) {
2585  ret = parse_ranges(&info->expose_custom, CMD_ARGV[i], "custom", 0x3fff);
2586  if (ret != ERROR_OK)
2587  break;
2588  }
2589 
2590  return ret;
2591 }
2592 
2593 COMMAND_HANDLER(riscv_authdata_read)
2594 {
2595  unsigned int index = 0;
2596  if (CMD_ARGC == 0) {
2597  /* nop */
2598  } else if (CMD_ARGC == 1) {
2599  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], index);
2600  } else {
2601  LOG_ERROR("Command takes at most one parameter");
2603  }
2604 
2606  if (!target) {
2607  LOG_ERROR("target is NULL!");
2608  return ERROR_FAIL;
2609  }
2610 
2611  RISCV_INFO(r);
2612  if (!r) {
2613  LOG_ERROR("riscv_info is NULL!");
2614  return ERROR_FAIL;
2615  }
2616 
2617  if (r->authdata_read) {
2618  uint32_t value;
2619  if (r->authdata_read(target, &value, index) != ERROR_OK)
2620  return ERROR_FAIL;
2621  command_print_sameline(CMD, "0x%08" PRIx32, value);
2622  return ERROR_OK;
2623  } else {
2624  LOG_ERROR("authdata_read is not implemented for this target.");
2625  return ERROR_FAIL;
2626  }
2627 }
2628 
2629 COMMAND_HANDLER(riscv_authdata_write)
2630 {
2631  uint32_t value;
2632  unsigned int index = 0;
2633 
2634  if (CMD_ARGC == 0 || CMD_ARGC > 2)
2636 
2637  if (CMD_ARGC == 1) {
2638  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], value);
2639  } else {
2640  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], index);
2641  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2642  }
2643 
2645  RISCV_INFO(r);
2646 
2647  if (!r->authdata_write) {
2648  LOG_ERROR("authdata_write is not implemented for this target.");
2649  return ERROR_FAIL;
2650  }
2651 
2652  return r->authdata_write(target, value, index);
2653 }
2654 
2655 COMMAND_HANDLER(riscv_dmi_read)
2656 {
2657  if (CMD_ARGC != 1) {
2658  LOG_ERROR("Command takes 1 parameter");
2660  }
2661 
2663  if (!target) {
2664  LOG_ERROR("target is NULL!");
2665  return ERROR_FAIL;
2666  }
2667 
2668  RISCV_INFO(r);
2669  if (!r) {
2670  LOG_ERROR("riscv_info is NULL!");
2671  return ERROR_FAIL;
2672  }
2673 
2674  if (r->dmi_read) {
2675  uint32_t address, value;
2677  if (r->dmi_read(target, &value, address) != ERROR_OK)
2678  return ERROR_FAIL;
2679  command_print(CMD, "0x%" PRIx32, value);
2680  return ERROR_OK;
2681  } else {
2682  LOG_ERROR("dmi_read is not implemented for this target.");
2683  return ERROR_FAIL;
2684  }
2685 }
2686 
2687 
2688 COMMAND_HANDLER(riscv_dmi_write)
2689 {
2690  if (CMD_ARGC != 2) {
2691  LOG_ERROR("Command takes exactly 2 arguments");
2693  }
2694 
2696  RISCV_INFO(r);
2697 
2698  uint32_t address, value;
2700  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2701 
2702  if (r->dmi_write) {
2703  return r->dmi_write(target, address, value);
2704  } else {
2705  LOG_ERROR("dmi_write is not implemented for this target.");
2706  return ERROR_FAIL;
2707  }
2708 }
2709 
2710 COMMAND_HANDLER(riscv_reset_delays)
2711 {
2712  int wait = 0;
2713 
2714  if (CMD_ARGC > 1) {
2715  LOG_ERROR("Command takes at most one argument");
2717  }
2718 
2719  if (CMD_ARGC == 1)
2720  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], wait);
2721 
2723  RISCV_INFO(r);
2724  r->reset_delays_wait = wait;
2725  return ERROR_OK;
2726 }
2727 
2728 COMMAND_HANDLER(riscv_set_ir)
2729 {
2730  if (CMD_ARGC != 2) {
2731  LOG_ERROR("Command takes exactly 2 arguments");
2733  }
2734 
2735  uint32_t value;
2736  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2737 
2738  if (!strcmp(CMD_ARGV[0], "idcode"))
2739  buf_set_u32(ir_idcode, 0, 32, value);
2740  else if (!strcmp(CMD_ARGV[0], "dtmcs"))
2741  buf_set_u32(ir_dtmcontrol, 0, 32, value);
2742  else if (!strcmp(CMD_ARGV[0], "dmi"))
2743  buf_set_u32(ir_dbus, 0, 32, value);
2744  else
2745  return ERROR_FAIL;
2746 
2747  return ERROR_OK;
2748 }
2749 
2750 COMMAND_HANDLER(riscv_resume_order)
2751 {
2752  if (CMD_ARGC > 1) {
2753  LOG_ERROR("Command takes at most one argument");
2755  }
2756 
2757  if (!strcmp(CMD_ARGV[0], "normal")) {
2759  } else if (!strcmp(CMD_ARGV[0], "reversed")) {
2761  } else {
2762  LOG_ERROR("Unsupported resume order: %s", CMD_ARGV[0]);
2763  return ERROR_FAIL;
2764  }
2765 
2766  return ERROR_OK;
2767 }
2768 
2769 COMMAND_HANDLER(riscv_use_bscan_tunnel)
2770 {
2771  int irwidth = 0;
2772  int tunnel_type = BSCAN_TUNNEL_NESTED_TAP;
2773 
2774  if (CMD_ARGC > 2) {
2775  LOG_ERROR("Command takes at most two arguments");
2777  } else if (CMD_ARGC == 1) {
2778  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], irwidth);
2779  } else if (CMD_ARGC == 2) {
2780  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], irwidth);
2781  COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tunnel_type);
2782  }
2783  if (tunnel_type == BSCAN_TUNNEL_NESTED_TAP)
2784  LOG_INFO("Nested Tap based Bscan Tunnel Selected");
2785  else if (tunnel_type == BSCAN_TUNNEL_DATA_REGISTER)
2786  LOG_INFO("Simple Register based Bscan Tunnel Selected");
2787  else
2788  LOG_INFO("Invalid Tunnel type selected ! : selecting default Nested Tap Type");
2789 
2790  bscan_tunnel_type = tunnel_type;
2791  bscan_tunnel_ir_width = irwidth;
2792  return ERROR_OK;
2793 }
2794 
2795 COMMAND_HANDLER(riscv_set_enable_virt2phys)
2796 {
2797  if (CMD_ARGC != 1) {
2798  LOG_ERROR("Command takes exactly 1 parameter");
2800  }
2802  return ERROR_OK;
2803 }
2804 
2805 COMMAND_HANDLER(riscv_set_ebreakm)
2806 {
2807  if (CMD_ARGC != 1) {
2808  LOG_ERROR("Command takes exactly 1 parameter");
2810  }
2812  return ERROR_OK;
2813 }
2814 
2815 COMMAND_HANDLER(riscv_set_ebreaks)
2816 {
2817  if (CMD_ARGC != 1) {
2818  LOG_ERROR("Command takes exactly 1 parameter");
2820  }
2822  return ERROR_OK;
2823 }
2824 
2825 COMMAND_HANDLER(riscv_set_ebreaku)
2826 {
2827  if (CMD_ARGC != 1) {
2828  LOG_ERROR("Command takes exactly 1 parameter");
2830  }
2832  return ERROR_OK;
2833 }
2834 
2835 COMMAND_HELPER(riscv_print_info_line, const char *section, const char *key,
2836  unsigned int value)
2837 {
2838  char full_key[80];
2839  snprintf(full_key, sizeof(full_key), "%s.%s", section, key);
2840  command_print(CMD, "%-21s %3d", full_key, value);
2841  return 0;
2842 }
2843 
2844 COMMAND_HANDLER(handle_info)
2845 {
2847  RISCV_INFO(r);
2848 
2849  /* This output format can be fed directly into TCL's "array set". */
2850 
2851  riscv_print_info_line(CMD, "hart", "xlen", riscv_xlen(target));
2853  riscv_print_info_line(CMD, "hart", "trigger_count",
2854  r->trigger_count);
2855 
2856  if (r->print_info)
2857  return CALL_COMMAND_HANDLER(r->print_info, target);
2858 
2859  return 0;
2860 }
2861 
2862 static const struct command_registration riscv_exec_command_handlers[] = {
2863  {
2864  .name = "info",
2865  .handler = handle_info,
2866  .mode = COMMAND_EXEC,
2867  .usage = "",
2868  .help = "Displays some information OpenOCD detected about the target."
2869  },
2870  {
2871  .name = "set_command_timeout_sec",
2872  .handler = riscv_set_command_timeout_sec,
2873  .mode = COMMAND_ANY,
2874  .usage = "[sec]",
2875  .help = "Set the wall-clock timeout (in seconds) for individual commands"
2876  },
2877  {
2878  .name = "set_reset_timeout_sec",
2879  .handler = riscv_set_reset_timeout_sec,
2880  .mode = COMMAND_ANY,
2881  .usage = "[sec]",
2882  .help = "Set the wall-clock timeout (in seconds) after reset is deasserted"
2883  },
2884  {
2885  .name = "set_mem_access",
2886  .handler = riscv_set_mem_access,
2887  .mode = COMMAND_ANY,
2888  .usage = "method1 [method2] [method3]",
2889  .help = "Set which memory access methods shall be used and in which order "
2890  "of priority. Method can be one of: 'progbuf', 'sysbus' or 'abstract'."
2891  },
2892  {
2893  .name = "set_enable_virtual",
2894  .handler = riscv_set_enable_virtual,
2895  .mode = COMMAND_ANY,
2896  .usage = "on|off",
2897  .help = "When on, memory accesses are performed on physical or virtual "
2898  "memory depending on the current system configuration. "
2899  "When off (default), all memory accessses are performed on physical memory."
2900  },
2901  {
2902  .name = "expose_csrs",
2903  .handler = riscv_set_expose_csrs,
2904  .mode = COMMAND_CONFIG,
2905  .usage = "n0[-m0|=name0][,n1[-m1|=name1]]...",
2906  .help = "Configure a list of inclusive ranges for CSRs to expose in "
2907  "addition to the standard ones. This must be executed before "
2908  "`init`."
2909  },
2910  {
2911  .name = "expose_custom",
2912  .handler = riscv_set_expose_custom,
2913  .mode = COMMAND_CONFIG,
2914  .usage = "n0[-m0|=name0][,n1[-m1|=name1]]...",
2915  .help = "Configure a list of inclusive ranges for custom registers to "
2916  "expose. custom0 is accessed as abstract register number 0xc000, "
2917  "etc. This must be executed before `init`."
2918  },
2919  {
2920  .name = "authdata_read",
2921  .handler = riscv_authdata_read,
2922  .usage = "[index]",
2923  .mode = COMMAND_ANY,
2924  .help = "Return the 32-bit value read from authdata or authdata0 "
2925  "(index=0), or authdata1 (index=1)."
2926  },
2927  {
2928  .name = "authdata_write",
2929  .handler = riscv_authdata_write,
2930  .mode = COMMAND_ANY,
2931  .usage = "[index] value",
2932  .help = "Write the 32-bit value to authdata or authdata0 (index=0), "
2933  "or authdata1 (index=1)."
2934  },
2935  {
2936  .name = "dmi_read",
2937  .handler = riscv_dmi_read,
2938  .mode = COMMAND_ANY,
2939  .usage = "address",
2940  .help = "Perform a 32-bit DMI read at address, returning the value."
2941  },
2942  {
2943  .name = "dmi_write",
2944  .handler = riscv_dmi_write,
2945  .mode = COMMAND_ANY,
2946  .usage = "address value",
2947  .help = "Perform a 32-bit DMI write of value at address."
2948  },
2949  {
2950  .name = "reset_delays",
2951  .handler = riscv_reset_delays,
2952  .mode = COMMAND_ANY,
2953  .usage = "[wait]",
2954  .help = "OpenOCD learns how many Run-Test/Idle cycles are required "
2955  "between scans to avoid encountering the target being busy. This "
2956  "command resets those learned values after `wait` scans. It's only "
2957  "useful for testing OpenOCD itself."
2958  },
2959  {
2960  .name = "resume_order",
2961  .handler = riscv_resume_order,
2962  .mode = COMMAND_ANY,
2963  .usage = "normal|reversed",
2964  .help = "Choose the order that harts are resumed in when `hasel` is not "
2965  "supported. Normal order is from lowest hart index to highest. "
2966  "Reversed order is from highest hart index to lowest."
2967  },
2968  {
2969  .name = "set_ir",
2970  .handler = riscv_set_ir,
2971  .mode = COMMAND_ANY,
2972  .usage = "[idcode|dtmcs|dmi] value",
2973  .help = "Set IR value for specified JTAG register."
2974  },
2975  {
2976  .name = "use_bscan_tunnel",
2977  .handler = riscv_use_bscan_tunnel,
2978  .mode = COMMAND_ANY,
2979  .usage = "value [type]",
2980  .help = "Enable or disable use of a BSCAN tunnel to reach DM. Supply "
2981  "the width of the DM transport TAP's instruction register to "
2982  "enable. Supply a value of 0 to disable. Pass A second argument "
2983  "(optional) to indicate Bscan Tunnel Type {0:(default) NESTED_TAP , "
2984  "1: DATA_REGISTER}"
2985  },
2986  {
2987  .name = "set_enable_virt2phys",
2988  .handler = riscv_set_enable_virt2phys,
2989  .mode = COMMAND_ANY,
2990  .usage = "on|off",
2991  .help = "When on (default), enable translation from virtual address to "
2992  "physical address."
2993  },
2994  {
2995  .name = "set_ebreakm",
2996  .handler = riscv_set_ebreakm,
2997  .mode = COMMAND_ANY,
2998  .usage = "on|off",
2999  .help = "Control dcsr.ebreakm. When off, M-mode ebreak instructions "
3000  "don't trap to OpenOCD. Defaults to on."
3001  },
3002  {
3003  .name = "set_ebreaks",
3004  .handler = riscv_set_ebreaks,
3005  .mode = COMMAND_ANY,
3006  .usage = "on|off",
3007  .help = "Control dcsr.ebreaks. When off, S-mode ebreak instructions "
3008  "don't trap to OpenOCD. Defaults to on."
3009  },
3010  {
3011  .name = "set_ebreaku",
3012  .handler = riscv_set_ebreaku,
3013  .mode = COMMAND_ANY,
3014  .usage = "on|off",
3015  .help = "Control dcsr.ebreaku. When off, U-mode ebreak instructions "
3016  "don't trap to OpenOCD. Defaults to on."
3017  },
3019 };
3020 
3021 /*
3022  * To be noted that RISC-V targets use the same semihosting commands as
3023  * ARM targets.
3024  *
3025  * The main reason is compatibility with existing tools. For example the
3026  * Eclipse OpenOCD/SEGGER J-Link/QEMU plug-ins have several widgets to
3027  * configure semihosting, which generate commands like `arm semihosting
3028  * enable`.
3029  * A secondary reason is the fact that the protocol used is exactly the
3030  * one specified by ARM. If RISC-V will ever define its own semihosting
3031  * protocol, then a command like `riscv semihosting enable` will make
3032  * sense, but for now all semihosting commands are prefixed with `arm`.
3033  */
3034 
3035 static const struct command_registration riscv_command_handlers[] = {
3036  {
3037  .name = "riscv",
3038  .mode = COMMAND_ANY,
3039  .help = "RISC-V Command Group",
3040  .usage = "",
3042  },
3043  {
3044  .name = "arm",
3045  .mode = COMMAND_ANY,
3046  .help = "ARM Command Group",
3047  .usage = "",
3049  },
3050  {
3052  },
3054 };
3055 
3056 static unsigned int riscv_xlen_nonconst(struct target *target)
3057 {
3058  return riscv_xlen(target);
3059 }
3060 
3061 static unsigned int riscv_data_bits(struct target *target)
3062 {
3063  RISCV_INFO(r);
3064  if (r->data_bits)
3065  return r->data_bits(target);
3066  return riscv_xlen(target);
3067 }
3068 
3069 struct target_type riscv_target = {
3070  .name = "riscv",
3071 
3072  .target_create = riscv_create_target,
3073  .init_target = riscv_init_target,
3074  .deinit_target = riscv_deinit_target,
3075  .examine = riscv_examine,
3076 
3077  /* poll current target status */
3078  .poll = old_or_new_riscv_poll,
3079 
3080  .halt = riscv_halt,
3081  .resume = riscv_target_resume,
3082  .step = old_or_new_riscv_step,
3083 
3084  .assert_reset = riscv_assert_reset,
3085  .deassert_reset = riscv_deassert_reset,
3086 
3087  .read_memory = riscv_read_memory,
3088  .write_memory = riscv_write_memory,
3089  .read_phys_memory = riscv_read_phys_memory,
3090  .write_phys_memory = riscv_write_phys_memory,
3091 
3092  .checksum_memory = riscv_checksum_memory,
3093 
3094  .mmu = riscv_mmu,
3095  .virt2phys = riscv_virt2phys,
3096 
3097  .get_gdb_arch = riscv_get_gdb_arch,
3098  .get_gdb_reg_list = riscv_get_gdb_reg_list,
3099  .get_gdb_reg_list_noread = riscv_get_gdb_reg_list_noread,
3100 
3101  .add_breakpoint = riscv_add_breakpoint,
3102  .remove_breakpoint = riscv_remove_breakpoint,
3103 
3104  .add_watchpoint = riscv_add_watchpoint,
3105  .remove_watchpoint = riscv_remove_watchpoint,
3106  .hit_watchpoint = riscv_hit_watchpoint,
3107 
3108  .arch_state = riscv_arch_state,
3109 
3110  .run_algorithm = riscv_run_algorithm,
3111 
3112  .commands = riscv_command_handlers,
3113 
3114  .address_bits = riscv_xlen_nonconst,
3115  .data_bits = riscv_data_bits
3116 };
3117 
3118 /*** RISC-V Interface ***/
3119 
3120 /* Initializes the shared RISC-V structure. */
3121 static void riscv_info_init(struct target *target, struct riscv_info *r)
3122 {
3123  memset(r, 0, sizeof(*r));
3124 
3126 
3127  r->dtm_version = 1;
3129  r->version_specific = NULL;
3130 
3131  memset(r->trigger_unique_id, 0xff, sizeof(r->trigger_unique_id));
3132 
3133  r->xlen = -1;
3134 
3138 
3139  r->mem_access_progbuf_warn = true;
3140  r->mem_access_sysbus_warn = true;
3141  r->mem_access_abstract_warn = true;
3142 
3145 }
3146 
3148 {
3149  RISCV_INFO(r);
3150 
3151  LOG_DEBUG("[%s] resuming hart", target_name(target));
3153  return ERROR_FAIL;
3154  if (riscv_is_halted(target)) {
3155  if (r->resume_go(target) != ERROR_OK)
3156  return ERROR_FAIL;
3157  } else {
3158  LOG_DEBUG("[%s] hart requested resume, but was already resumed",
3159  target_name(target));
3160  }
3161 
3163  return ERROR_OK;
3164 }
3165 
3166 /* Steps the hart that's currently selected in the RTOS, or if there is no RTOS
3167  * then the only hart. */
3169 {
3170  RISCV_INFO(r);
3172  return ERROR_FAIL;
3173  LOG_DEBUG("[%s] stepping", target_name(target));
3174 
3175  if (!riscv_is_halted(target)) {
3176  LOG_ERROR("Hart isn't halted before single step!");
3177  return ERROR_FAIL;
3178  }
3180  r->on_step(target);
3181  if (r->step_current_hart(target) != ERROR_OK)
3182  return ERROR_FAIL;
3184  r->on_halt(target);
3185  if (!riscv_is_halted(target)) {
3186  LOG_ERROR("Hart was not halted after single step!");
3187  return ERROR_FAIL;
3188  }
3189  return ERROR_OK;
3190 }
3191 
3192 bool riscv_supports_extension(struct target *target, char letter)
3193 {
3194  RISCV_INFO(r);
3195  unsigned int num;
3196  if (letter >= 'a' && letter <= 'z')
3197  num = letter - 'a';
3198  else if (letter >= 'A' && letter <= 'Z')
3199  num = letter - 'A';
3200  else
3201  return false;
3202  return r->misa & BIT(num);
3203 }
3204 
3205 unsigned int riscv_xlen(const struct target *target)
3206 {
3207  RISCV_INFO(r);
3208  return r->xlen;
3209 }
3210 
3211 int riscv_set_current_hartid(struct target *target, int hartid)
3212 {
3213  RISCV_INFO(r);
3214  if (!r->select_current_hart)
3215  return ERROR_OK;
3216 
3217  int previous_hartid = riscv_current_hartid(target);
3218  r->current_hartid = hartid;
3219  LOG_DEBUG("setting hartid to %d, was %d", hartid, previous_hartid);
3220  if (r->select_current_hart(target) != ERROR_OK)
3221  return ERROR_FAIL;
3222 
3223  return ERROR_OK;
3224 }
3225 
3226 /* Invalidates the register cache. */
3228 {
3229  LOG_DEBUG("[%d]", target->coreid);
3231  for (size_t i = 0; i < target->reg_cache->num_regs; ++i) {
3232  struct reg *reg = &target->reg_cache->reg_list[i];
3233  reg->valid = false;
3234  }
3235 }
3236 
3238 {
3239  RISCV_INFO(r);
3240  return r->current_hartid;
3241 }
3242 
3244 {
3245  if (!target)
3246  return 1;
3247  RISCV_INFO(r);
3248  if (!r || !r->hart_count)
3249  return 1;
3250  return r->hart_count(target);
3251 }
3252 
3261 static bool gdb_regno_cacheable(enum gdb_regno regno, bool write)
3262 {
3263  /* GPRs, FPRs, vector registers are just normal data stores. */
3264  if (regno <= GDB_REGNO_XPR31 ||
3265  (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31) ||
3266  (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31))
3267  return true;
3268 
3269  /* Most CSRs won't change value on us, but we can't assume it about arbitrary
3270  * CSRs. */
3271  switch (regno) {
3272  case GDB_REGNO_DPC:
3273  return true;
3274 
3275  case GDB_REGNO_VSTART:
3276  case GDB_REGNO_VXSAT:
3277  case GDB_REGNO_VXRM:
3278  case GDB_REGNO_VLENB:
3279  case GDB_REGNO_VL:
3280  case GDB_REGNO_VTYPE:
3281  case GDB_REGNO_MISA:
3282  case GDB_REGNO_DCSR:
3283  case GDB_REGNO_DSCRATCH0:
3284  case GDB_REGNO_MSTATUS:
3285  case GDB_REGNO_MEPC:
3286  case GDB_REGNO_MCAUSE:
3287  case GDB_REGNO_SATP:
3288  /*
3289  * WARL registers might not contain the value we just wrote, but
3290  * these ones won't spontaneously change their value either. *
3291  */
3292  return !write;
3293 
3294  case GDB_REGNO_TSELECT: /* I think this should be above, but then it doesn't work. */
3295  case GDB_REGNO_TDATA1: /* Changes value when tselect is changed. */
3296  case GDB_REGNO_TDATA2: /* Changse value when tselect is changed. */
3297  default:
3298  return false;
3299  }
3300 }
3301 
3307 {
3308  RISCV_INFO(r);
3309  LOG_DEBUG("[%s] %s <- %" PRIx64, target_name(target), gdb_regno_name(regid), value);
3310  assert(r->set_register);
3311 
3312  keep_alive();
3313 
3314  /* TODO: Hack to deal with gdb that thinks these registers still exist. */
3315  if (regid > GDB_REGNO_XPR15 && regid <= GDB_REGNO_XPR31 && value == 0 &&
3317  return ERROR_OK;
3318 
3319  struct reg *reg = &target->reg_cache->reg_list[regid];
3320  buf_set_u64(reg->value, 0, reg->size, value);
3321 
3322  int result = r->set_register(target, regid, value);
3323  if (result == ERROR_OK)
3324  reg->valid = gdb_regno_cacheable(regid, true);
3325  else
3326  reg->valid = false;
3327  LOG_DEBUG("[%s] wrote 0x%" PRIx64 " to %s valid=%d",
3329  return result;
3330 }
3331 
3333  enum gdb_regno regid)
3334 {
3335  RISCV_INFO(r);
3336 
3337  keep_alive();
3338 
3339  struct reg *reg = &target->reg_cache->reg_list[regid];
3340  if (!reg->exist) {
3341  LOG_DEBUG("[%s] %s does not exist.",
3342  target_name(target), gdb_regno_name(regid));
3343  return ERROR_FAIL;
3344  }
3345 
3346  if (reg && reg->valid) {
3347  *value = buf_get_u64(reg->value, 0, reg->size);
3348  LOG_DEBUG("[%s] %s: %" PRIx64 " (cached)", target_name(target),
3349  gdb_regno_name(regid), *value);
3350  return ERROR_OK;
3351  }
3352 
3353  /* TODO: Hack to deal with gdb that thinks these registers still exist. */
3354  if (regid > GDB_REGNO_XPR15 && regid <= GDB_REGNO_XPR31 &&
3356  *value = 0;
3357  return ERROR_OK;
3358  }
3359 
3360  int result = r->get_register(target, value, regid);
3361 
3362  if (result == ERROR_OK)
3363  reg->valid = gdb_regno_cacheable(regid, false);
3364 
3365  LOG_DEBUG("[%s] %s: %" PRIx64, target_name(target),
3366  gdb_regno_name(regid), *value);
3367  return result;
3368 }
3369 
3371 {
3372  RISCV_INFO(r);
3373  assert(r->is_halted);
3374  return r->is_halted(target);
3375 }
3376 
3377 static enum riscv_halt_reason riscv_halt_reason(struct target *target, int hartid)
3378 {
3379  RISCV_INFO(r);
3380  if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
3381  return RISCV_HALT_ERROR;
3382  if (!riscv_is_halted(target)) {
3383  LOG_ERROR("Hart is not halted!");
3384  return RISCV_HALT_UNKNOWN;
3385  }
3386  return r->halt_reason(target);
3387 }
3388 
3390 {
3391  RISCV_INFO(r);
3392  return r->debug_buffer_size;
3393 }
3394 
3396 {
3397  RISCV_INFO(r);
3398  r->write_debug_buffer(target, index, insn);
3399  return ERROR_OK;
3400 }
3401 
3403 {
3404  RISCV_INFO(r);
3405  return r->read_debug_buffer(target, index);
3406 }
3407 
3409 {
3410  RISCV_INFO(r);
3411  return r->execute_debug_buffer(target);
3412 }
3413 
3414 void riscv_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d)
3415 {
3416  RISCV_INFO(r);
3417  r->fill_dmi_write_u64(target, buf, a, d);
3418 }
3419 
3420 void riscv_fill_dmi_read_u64(struct target *target, char *buf, int a)
3421 {
3422  RISCV_INFO(r);
3423  r->fill_dmi_read_u64(target, buf, a);
3424 }
3425 
3426 void riscv_fill_dmi_nop_u64(struct target *target, char *buf)
3427 {
3428  RISCV_INFO(r);
3429  r->fill_dmi_nop_u64(target, buf);
3430 }
3431 
3433 {
3434  RISCV_INFO(r);
3435  return r->dmi_write_u64_bits(target);
3436 }
3437 
3446 {
3447  RISCV_INFO(r);
3448 
3449  if (r->triggers_enumerated)
3450  return ERROR_OK;
3451 
3452  r->triggers_enumerated = true; /* At the very least we tried. */
3453 
3454  riscv_reg_t tselect;
3455  int result = riscv_get_register(target, &tselect, GDB_REGNO_TSELECT);
3456  /* If tselect is not readable, the trigger module is likely not
3457  * implemented. There are no triggers to enumerate then and no error
3458  * should be thrown. */
3459  if (result != ERROR_OK) {
3460  LOG_DEBUG("[%s] Cannot access tselect register. "
3461  "Assuming that triggers are not implemented.", target_name(target));
3462  r->trigger_count = 0;
3463  return ERROR_OK;
3464  }
3465 
3466  for (unsigned int t = 0; t < RISCV_MAX_TRIGGERS; ++t) {
3467  r->trigger_count = t;
3468 
3469  /* If we can't write tselect, then this hart does not support triggers. */
3471  break;
3472  uint64_t tselect_rb;
3473  result = riscv_get_register(target, &tselect_rb, GDB_REGNO_TSELECT);
3474  if (result != ERROR_OK)
3475  return result;
3476  /* Mask off the top bit, which is used as tdrmode in old
3477  * implementations. */
3478  tselect_rb &= ~(1ULL << (riscv_xlen(target) - 1));
3479  if (tselect_rb != t)
3480  break;
3481  uint64_t tdata1;
3482  result = riscv_get_register(target, &tdata1, GDB_REGNO_TDATA1);
3483  if (result != ERROR_OK)
3484  return result;
3485 
3486  int type = get_field(tdata1, MCONTROL_TYPE(riscv_xlen(target)));
3487  if (type == 0)
3488  break;
3489  switch (type) {
3490  case 1:
3491  /* On these older cores we don't support software using
3492  * triggers. */
3494  break;
3495  case 2:
3496  if (tdata1 & MCONTROL_DMODE(riscv_xlen(target)))
3498  break;
3499  case 6:
3500  if (tdata1 & MCONTROL_DMODE(riscv_xlen(target)))
3502  break;
3503  }
3504  }
3505 
3507 
3508  LOG_INFO("[%s] Found %d triggers", target_name(target), r->trigger_count);
3509 
3510  return ERROR_OK;
3511 }
3512 
3513 const char *gdb_regno_name(enum gdb_regno regno)
3514 {
3515  static char buf[32];
3516 
3517  switch (regno) {
3518  case GDB_REGNO_ZERO:
3519  return "zero";
3520  case GDB_REGNO_RA:
3521  return "ra";
3522  case GDB_REGNO_SP:
3523  return "sp";
3524  case GDB_REGNO_GP:
3525  return "gp";
3526  case GDB_REGNO_TP:
3527  return "tp";
3528  case GDB_REGNO_T0:
3529  return "t0";
3530  case GDB_REGNO_T1:
3531  return "t1";
3532  case GDB_REGNO_T2:
3533  return "t2";
3534  case GDB_REGNO_S0:
3535  return "s0";
3536  case GDB_REGNO_S1:
3537  return "s1";
3538  case GDB_REGNO_A0:
3539  return "a0";
3540  case GDB_REGNO_A1:
3541  return "a1";
3542  case GDB_REGNO_A2:
3543  return "a2";
3544  case GDB_REGNO_A3:
3545  return "a3";
3546  case GDB_REGNO_A4:
3547  return "a4";
3548  case GDB_REGNO_A5:
3549  return "a5";
3550  case GDB_REGNO_A6:
3551  return "a6";
3552  case GDB_REGNO_A7:
3553  return "a7";
3554  case GDB_REGNO_S2:
3555  return "s2";
3556  case GDB_REGNO_S3:
3557  return "s3";
3558  case GDB_REGNO_S4:
3559  return "s4";
3560  case GDB_REGNO_S5:
3561  return "s5";
3562  case GDB_REGNO_S6:
3563  return "s6";
3564  case GDB_REGNO_S7:
3565  return "s7";
3566  case GDB_REGNO_S8:
3567  return "s8";
3568  case GDB_REGNO_S9:
3569  return "s9";
3570  case GDB_REGNO_S10:
3571  return "s10";
3572  case GDB_REGNO_S11:
3573  return "s11";
3574  case GDB_REGNO_T3:
3575  return "t3";
3576  case GDB_REGNO_T4:
3577  return "t4";
3578  case GDB_REGNO_T5:
3579  return "t5";
3580  case GDB_REGNO_T6:
3581  return "t6";
3582  case GDB_REGNO_PC:
3583  return "pc";
3584  case GDB_REGNO_FPR0:
3585  return "fpr0";
3586  case GDB_REGNO_FPR31:
3587  return "fpr31";
3588  case GDB_REGNO_CSR0:
3589  return "csr0";
3590  case GDB_REGNO_TSELECT:
3591  return "tselect";
3592  case GDB_REGNO_TDATA1:
3593  return "tdata1";
3594  case GDB_REGNO_TDATA2:
3595  return "tdata2";
3596  case GDB_REGNO_MISA:
3597  return "misa";
3598  case GDB_REGNO_DPC:
3599  return "dpc";
3600  case GDB_REGNO_DCSR:
3601  return "dcsr";
3602  case GDB_REGNO_DSCRATCH0:
3603  return "dscratch0";
3604  case GDB_REGNO_MSTATUS:
3605  return "mstatus";
3606  case GDB_REGNO_MEPC:
3607  return "mepc";
3608  case GDB_REGNO_MCAUSE:
3609  return "mcause";
3610  case GDB_REGNO_PRIV:
3611  return "priv";
3612  case GDB_REGNO_SATP:
3613  return "satp";
3614  case GDB_REGNO_VTYPE:
3615  return "vtype";
3616  case GDB_REGNO_VL:
3617  return "vl";
3618  case GDB_REGNO_V0:
3619  return "v0";
3620  case GDB_REGNO_V1:
3621  return "v1";
3622  case GDB_REGNO_V2:
3623  return "v2";
3624  case GDB_REGNO_V3:
3625  return "v3";
3626  case GDB_REGNO_V4:
3627  return "v4";
3628  case GDB_REGNO_V5:
3629  return "v5";
3630  case GDB_REGNO_V6:
3631  return "v6";
3632  case GDB_REGNO_V7:
3633  return "v7";
3634  case GDB_REGNO_V8:
3635  return "v8";
3636  case GDB_REGNO_V9:
3637  return "v9";
3638  case GDB_REGNO_V10:
3639  return "v10";
3640  case GDB_REGNO_V11:
3641  return "v11";
3642  case GDB_REGNO_V12:
3643  return "v12";
3644  case GDB_REGNO_V13:
3645  return "v13";
3646  case GDB_REGNO_V14:
3647  return "v14";
3648  case GDB_REGNO_V15:
3649  return "v15";
3650  case GDB_REGNO_V16:
3651  return "v16";
3652  case GDB_REGNO_V17:
3653  return "v17";
3654  case GDB_REGNO_V18:
3655  return "v18";
3656  case GDB_REGNO_V19:
3657  return "v19";
3658  case GDB_REGNO_V20:
3659  return "v20";
3660  case GDB_REGNO_V21:
3661  return "v21";
3662  case GDB_REGNO_V22:
3663  return "v22";
3664  case GDB_REGNO_V23:
3665  return "v23";
3666  case GDB_REGNO_V24:
3667  return "v24";
3668  case GDB_REGNO_V25:
3669  return "v25";
3670  case GDB_REGNO_V26:
3671  return "v26";
3672  case GDB_REGNO_V27:
3673  return "v27";
3674  case GDB_REGNO_V28:
3675  return "v28";
3676  case GDB_REGNO_V29:
3677  return "v29";
3678  case GDB_REGNO_V30:
3679  return "v30";
3680  case GDB_REGNO_V31:
3681  return "v31";
3682  default:
3683  if (regno <= GDB_REGNO_XPR31)
3684  sprintf(buf, "x%d", regno - GDB_REGNO_ZERO);
3685  else if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095)
3686  sprintf(buf, "csr%d", regno - GDB_REGNO_CSR0);
3687  else if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31)
3688  sprintf(buf, "f%d", regno - GDB_REGNO_FPR0);
3689  else
3690  sprintf(buf, "gdb_regno_%d", regno);
3691  return buf;
3692  }
3693 }
3694 
3695 static int register_get(struct reg *reg)
3696 {
3697  riscv_reg_info_t *reg_info = reg->arch_info;
3698  struct target *target = reg_info->target;
3699  RISCV_INFO(r);
3700 
3701  if (reg->number >= GDB_REGNO_V0 && reg->number <= GDB_REGNO_V31) {
3702  if (!r->get_register_buf) {
3703  LOG_ERROR("Reading register %s not supported on this RISC-V target.",
3705  return ERROR_FAIL;
3706  }
3707 
3708  if (r->get_register_buf(target, reg->value, reg->number) != ERROR_OK)
3709  return ERROR_FAIL;
3710  } else {
3711  uint64_t value;
3712  int result = riscv_get_register(target, &value, reg->number);
3713  if (result != ERROR_OK)
3714  return result;
3715  buf_set_u64(reg->value, 0, reg->size, value);
3716  }
3717  reg->valid = gdb_regno_cacheable(reg->number, false);
3718  char *str = buf_to_hex_str(reg->value, reg->size);
3719  LOG_DEBUG("[%s] read 0x%s from %s (valid=%d)", target_name(target),
3720  str, reg->name, reg->valid);
3721  free(str);
3722  return ERROR_OK;
3723 }
3724 
3725 static int register_set(struct reg *reg, uint8_t *buf)
3726 {
3727  riscv_reg_info_t *reg_info = reg->arch_info;
3728  struct target *target = reg_info->target;
3729  RISCV_INFO(r);
3730 
3731  char *str = buf_to_hex_str(buf, reg->size);
3732  LOG_DEBUG("[%s] write 0x%s to %s (valid=%d)", target_name(target),
3733  str, reg->name, reg->valid);
3734  free(str);
3735 
3736  /* Exit early for writing x0, which on the hardware would be ignored, and we
3737  * don't want to update our cache. */
3738  if (reg->number == GDB_REGNO_ZERO)
3739  return ERROR_OK;
3740 
3741  memcpy(reg->value, buf, DIV_ROUND_UP(reg->size, 8));
3742  reg->valid = gdb_regno_cacheable(reg->number, true);
3743 
3744  if (reg->number == GDB_REGNO_TDATA1 ||
3745  reg->number == GDB_REGNO_TDATA2) {
3746  r->manual_hwbp_set = true;
3747  /* When enumerating triggers, we clear any triggers with DMODE set,
3748  * assuming they were left over from a previous debug session. So make
3749  * sure that is done before a user might be setting their own triggers.
3750  */
3752  return ERROR_FAIL;
3753  }
3754 
3755  if (reg->number >= GDB_REGNO_V0 && reg->number <= GDB_REGNO_V31) {
3756  if (!r->set_register_buf) {
3757  LOG_ERROR("Writing register %s not supported on this RISC-V target.",
3759  return ERROR_FAIL;
3760  }
3761 
3762  if (r->set_register_buf(target, reg->number, reg->value) != ERROR_OK)
3763  return ERROR_FAIL;
3764  } else {
3765  uint64_t value = buf_get_u64(buf, 0, reg->size);
3766  if (riscv_set_register(target, reg->number, value) != ERROR_OK)
3767  return ERROR_FAIL;
3768  }
3769 
3770  return ERROR_OK;
3771 }
3772 
3773 static struct reg_arch_type riscv_reg_arch_type = {
3774  .get = register_get,
3775  .set = register_set
3776 };
3777 
3778 struct csr_info {
3779  unsigned int number;
3780  const char *name;
3781 };
3782 
3783 static int cmp_csr_info(const void *p1, const void *p2)
3784 {
3785  return (int) (((struct csr_info *)p1)->number) - (int) (((struct csr_info *)p2)->number);
3786 }
3787 
3789 {
3790  RISCV_INFO(info);
3791 
3793 
3794  target->reg_cache = calloc(1, sizeof(*target->reg_cache));
3795  if (!target->reg_cache)
3796  return ERROR_FAIL;
3797  target->reg_cache->name = "RISC-V Registers";
3799 
3800  if (!list_empty(&info->expose_custom)) {
3801  range_list_t *entry;
3802  list_for_each_entry(entry, &info->expose_custom, list)
3803  target->reg_cache->num_regs += entry->high - entry->low + 1;
3804  }
3805 
3806  LOG_DEBUG("create register cache for %d registers",
3808 
3810  calloc(target->reg_cache->num_regs, sizeof(struct reg));
3811  if (!target->reg_cache->reg_list)
3812  return ERROR_FAIL;
3813 
3814  const unsigned int max_reg_name_len = 12;
3815  free(info->reg_names);
3816  info->reg_names =
3817  calloc(target->reg_cache->num_regs, max_reg_name_len);
3818  if (!info->reg_names)
3819  return ERROR_FAIL;
3820  char *reg_name = info->reg_names;
3821 
3822  static struct reg_feature feature_cpu = {
3823  .name = "org.gnu.gdb.riscv.cpu"
3824  };
3825  static struct reg_feature feature_fpu = {
3826  .name = "org.gnu.gdb.riscv.fpu"
3827  };
3828  static struct reg_feature feature_csr = {
3829  .name = "org.gnu.gdb.riscv.csr"
3830  };
3831  static struct reg_feature feature_vector = {
3832  .name = "org.gnu.gdb.riscv.vector"
3833  };
3834  static struct reg_feature feature_virtual = {
3835  .name = "org.gnu.gdb.riscv.virtual"
3836  };
3837  static struct reg_feature feature_custom = {
3838  .name = "org.gnu.gdb.riscv.custom"
3839  };
3840 
3841  /* These types are built into gdb. */
3842  static struct reg_data_type type_ieee_single = { .type = REG_TYPE_IEEE_SINGLE, .id = "ieee_single" };
3843  static struct reg_data_type type_ieee_double = { .type = REG_TYPE_IEEE_DOUBLE, .id = "ieee_double" };
3844  static struct reg_data_type_union_field single_double_fields[] = {
3845  {"float", &type_ieee_single, single_double_fields + 1},
3846  {"double", &type_ieee_double, NULL},
3847  };
3848  static struct reg_data_type_union single_double_union = {
3849  .fields = single_double_fields
3850  };
3851  static struct reg_data_type type_ieee_single_double = {
3853  .id = "FPU_FD",
3854  .type_class = REG_TYPE_CLASS_UNION,
3855  { .reg_type_union = &single_double_union }
3856  };
3857  static struct reg_data_type type_uint8 = { .type = REG_TYPE_UINT8, .id = "uint8" };
3858  static struct reg_data_type type_uint16 = { .type = REG_TYPE_UINT16, .id = "uint16" };
3859  static struct reg_data_type type_uint32 = { .type = REG_TYPE_UINT32, .id = "uint32" };
3860  static struct reg_data_type type_uint64 = { .type = REG_TYPE_UINT64, .id = "uint64" };
3861  static struct reg_data_type type_uint128 = { .type = REG_TYPE_UINT128, .id = "uint128" };
3862 
3863  /* This is roughly the XML we want:
3864  * <vector id="bytes" type="uint8" count="16"/>
3865  * <vector id="shorts" type="uint16" count="8"/>
3866  * <vector id="words" type="uint32" count="4"/>
3867  * <vector id="longs" type="uint64" count="2"/>
3868  * <vector id="quads" type="uint128" count="1"/>
3869  * <union id="riscv_vector_type">
3870  * <field name="b" type="bytes"/>
3871  * <field name="s" type="shorts"/>
3872  * <field name="w" type="words"/>
3873  * <field name="l" type="longs"/>
3874  * <field name="q" type="quads"/>
3875  * </union>
3876  */
3877 
3878  info->vector_uint8.type = &type_uint8;
3879  info->vector_uint8.count = info->vlenb;
3880  info->type_uint8_vector.type = REG_TYPE_ARCH_DEFINED;
3881  info->type_uint8_vector.id = "bytes";
3882  info->type_uint8_vector.type_class = REG_TYPE_CLASS_VECTOR;
3883  info->type_uint8_vector.reg_type_vector = &info->vector_uint8;
3884 
3885  info->vector_uint16.type = &type_uint16;
3886  info->vector_uint16.count = info->vlenb / 2;
3887  info->type_uint16_vector.type = REG_TYPE_ARCH_DEFINED;
3888  info->type_uint16_vector.id = "shorts";
3889  info->type_uint16_vector.type_class = REG_TYPE_CLASS_VECTOR;
3890  info->type_uint16_vector.reg_type_vector = &info->vector_uint16;
3891 
3892  info->vector_uint32.type = &type_uint32;
3893  info->vector_uint32.count = info->vlenb / 4;
3894  info->type_uint32_vector.type = REG_TYPE_ARCH_DEFINED;
3895  info->type_uint32_vector.id = "words";
3896  info->type_uint32_vector.type_class = REG_TYPE_CLASS_VECTOR;
3897  info->type_uint32_vector.reg_type_vector = &info->vector_uint32;
3898 
3899  info->vector_uint64.type = &type_uint64;
3900  info->vector_uint64.count = info->vlenb / 8;
3901  info->type_uint64_vector.type = REG_TYPE_ARCH_DEFINED;
3902  info->type_uint64_vector.id = "longs";
3903  info->type_uint64_vector.type_class = REG_TYPE_CLASS_VECTOR;
3904  info->type_uint64_vector.reg_type_vector = &info->vector_uint64;
3905 
3906  info->vector_uint128.type = &type_uint128;
3907  info->vector_uint128.count = info->vlenb / 16;
3908  info->type_uint128_vector.type = REG_TYPE_ARCH_DEFINED;
3909  info->type_uint128_vector.id = "quads";
3910  info->type_uint128_vector.type_class = REG_TYPE_CLASS_VECTOR;
3911  info->type_uint128_vector.reg_type_vector = &info->vector_uint128;
3912 
3913  info->vector_fields[0].name = "b";
3914  info->vector_fields[0].type = &info->type_uint8_vector;
3915  if (info->vlenb >= 2) {
3916  info->vector_fields[0].next = info->vector_fields + 1;
3917  info->vector_fields[1].name = "s";
3918  info->vector_fields[1].type = &info->type_uint16_vector;
3919  } else {
3920  info->vector_fields[0].next = NULL;
3921  }
3922  if (info->vlenb >= 4) {
3923  info->vector_fields[1].next = info->vector_fields + 2;
3924  info->vector_fields[2].name = "w";
3925  info->vector_fields[2].type = &info->type_uint32_vector;
3926  } else {
3927  info->vector_fields[1].next = NULL;
3928  }
3929  if (info->vlenb >= 8) {
3930  info->vector_fields[2].next = info->vector_fields + 3;
3931  info->vector_fields[3].name = "l";
3932  info->vector_fields[3].type = &info->type_uint64_vector;
3933  } else {
3934  info->vector_fields[2].next = NULL;
3935  }
3936  if (info->vlenb >= 16) {
3937  info->vector_fields[3].next = info->vector_fields + 4;
3938  info->vector_fields[4].name = "q";
3939  info->vector_fields[4].type = &info->type_uint128_vector;
3940  } else {
3941  info->vector_fields[3].next = NULL;
3942  }
3943  info->vector_fields[4].next = NULL;
3944 
3945  info->vector_union.fields = info->vector_fields;
3946 
3947  info->type_vector.type = REG_TYPE_ARCH_DEFINED;
3948  info->type_vector.id = "riscv_vector";
3949  info->type_vector.type_class = REG_TYPE_CLASS_UNION;
3950  info->type_vector.reg_type_union = &info->vector_union;
3951 
3952  struct csr_info csr_info[] = {
3953 #define DECLARE_CSR(name, number) { number, #name },
3954 #include "encoding.h"
3955 #undef DECLARE_CSR
3956  };
3957  /* encoding.h does not contain the registers in sorted order. */
3958  qsort(csr_info, ARRAY_SIZE(csr_info), sizeof(*csr_info), cmp_csr_info);
3959  unsigned int csr_info_index = 0;
3960 
3961  int custom_within_range = 0;
3962 
3963  riscv_reg_info_t *shared_reg_info = calloc(1, sizeof(riscv_reg_info_t));
3964  if (!shared_reg_info)
3965  return ERROR_FAIL;
3966  shared_reg_info->target = target;
3967 
3968  /* When gdb requests register N, gdb_get_register_packet() assumes that this
3969  * is register at index N in reg_list. So if there are certain registers
3970  * that don't exist, we need to leave holes in the list (or renumber, but
3971  * it would be nice not to have yet another set of numbers to translate
3972  * between). */
3973  for (uint32_t number = 0; number < target->reg_cache->num_regs; number++) {
3974  struct reg *r = &target->reg_cache->reg_list[number];
3975  r->dirty = false;
3976  r->valid = false;
3977  r->exist = true;
3978  r->type = &riscv_reg_arch_type;
3979  r->arch_info = shared_reg_info;
3980  r->number = number;
3981  r->size = riscv_xlen(target);
3982  /* r->size is set in riscv_invalidate_register_cache, maybe because the
3983  * target is in theory allowed to change XLEN on us. But I expect a lot
3984  * of other things to break in that case as well. */
3985  if (number <= GDB_REGNO_XPR31) {
3986  r->exist = number <= GDB_REGNO_XPR15 ||
3988  /* TODO: For now we fake that all GPRs exist because otherwise gdb
3989  * doesn't work. */
3990  r->exist = true;
3991  r->caller_save = true;
3992  switch (number) {
3993  case GDB_REGNO_ZERO:
3994  r->name = "zero";
3995  break;
3996  case GDB_REGNO_RA:
3997  r->name = "ra";
3998  break;
3999  case GDB_REGNO_SP:
4000  r->name = "sp";
4001  break;
4002  case GDB_REGNO_GP:
4003  r->name = "gp";
4004  break;
4005  case GDB_REGNO_TP:
4006  r->name = "tp";
4007  break;
4008  case GDB_REGNO_T0:
4009  r->name = "t0";
4010  break;
4011  case GDB_REGNO_T1:
4012  r->name = "t1";
4013  break;
4014  case GDB_REGNO_T2:
4015  r->name = "t2";
4016  break;
4017  case GDB_REGNO_FP:
4018  r->name = "fp";
4019  break;
4020  case GDB_REGNO_S1:
4021  r->name = "s1";
4022  break;
4023  case GDB_REGNO_A0:
4024  r->name = "a0";
4025  break;
4026  case GDB_REGNO_A1:
4027  r->name = "a1";
4028  break;
4029  case GDB_REGNO_A2:
4030  r->name = "a2";
4031  break;
4032  case GDB_REGNO_A3:
4033  r->name = "a3";
4034  break;
4035  case GDB_REGNO_A4:
4036  r->name = "a4";
4037  break;
4038  case GDB_REGNO_A5:
4039  r->name = "a5";
4040  break;
4041  case GDB_REGNO_A6:
4042  r->name = "a6";
4043  break;
4044  case GDB_REGNO_A7:
4045  r->name = "a7";
4046  break;
4047  case GDB_REGNO_S2:
4048  r->name = "s2";
4049  break;
4050  case GDB_REGNO_S3:
4051  r->name = "s3";
4052  break;
4053  case GDB_REGNO_S4:
4054  r->name = "s4";
4055  break;
4056  case GDB_REGNO_S5:
4057  r->name = "s5";
4058  break;
4059  case GDB_REGNO_S6:
4060  r->name = "s6";
4061  break;
4062  case GDB_REGNO_S7:
4063  r->name = "s7";
4064  break;
4065  case GDB_REGNO_S8:
4066  r->name = "s8";
4067  break;
4068  case GDB_REGNO_S9:
4069  r->name = "s9";
4070  break;
4071  case GDB_REGNO_S10:
4072  r->name = "s10";
4073  break;
4074  case GDB_REGNO_S11:
4075  r->name = "s11";
4076  break;
4077  case GDB_REGNO_T3:
4078  r->name = "t3";
4079  break;
4080  case GDB_REGNO_T4:
4081  r->name = "t4";
4082  break;
4083  case GDB_REGNO_T5:
4084  r->name = "t5";
4085  break;
4086  case GDB_REGNO_T6:
4087  r->name = "t6";
4088  break;
4089  }
4090  r->group = "general";
4091  r->feature = &feature_cpu;
4092  } else if (number == GDB_REGNO_PC) {
4093  r->caller_save = true;
4094  sprintf(reg_name, "pc");
4095  r->group = "general";
4096  r->feature = &feature_cpu;
4097  } else if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
4098  r->caller_save = true;
4099  if (riscv_supports_extension(target, 'D')) {
4100  r->size = 64;
4101  if (riscv_supports_extension(target, 'F'))
4102  r->reg_data_type = &type_ieee_single_double;
4103  else
4104  r->reg_data_type = &type_ieee_double;
4105  } else if (riscv_supports_extension(target, 'F')) {
4106  r->reg_data_type = &type_ieee_single;
4107  r->size = 32;
4108  } else {
4109  r->exist = false;
4110  }
4111  switch (number) {
4112  case GDB_REGNO_FT0:
4113  r->name = "ft0";
4114  break;
4115  case GDB_REGNO_FT1:
4116  r->name = "ft1";
4117  break;
4118  case GDB_REGNO_FT2:
4119  r->name = "ft2";
4120  break;
4121  case GDB_REGNO_FT3:
4122  r->name = "ft3";
4123  break;
4124  case GDB_REGNO_FT4:
4125  r->name = "ft4";
4126  break;
4127  case GDB_REGNO_FT5:
4128  r->name = "ft5";
4129  break;
4130  case GDB_REGNO_FT6:
4131  r->name = "ft6";
4132  break;
4133  case GDB_REGNO_FT7:
4134  r->name = "ft7";
4135  break;
4136  case GDB_REGNO_FS0:
4137  r->name = "fs0";
4138  break;
4139  case GDB_REGNO_FS1:
4140  r->name = "fs1";
4141  break;
4142  case GDB_REGNO_FA0:
4143  r->name = "fa0";
4144  break;
4145  case GDB_REGNO_FA1:
4146  r->name = "fa1";
4147  break;
4148  case GDB_REGNO_FA2:
4149  r->name = "fa2";
4150  break;
4151  case GDB_REGNO_FA3:
4152  r->name = "fa3";
4153  break;
4154  case GDB_REGNO_FA4:
4155  r->name = "fa4";
4156  break;
4157  case GDB_REGNO_FA5:
4158  r->name = "fa5";
4159  break;
4160  case GDB_REGNO_FA6:
4161  r->name = "fa6";
4162  break;
4163  case GDB_REGNO_FA7:
4164  r->name = "fa7";
4165  break;
4166  case GDB_REGNO_FS2:
4167  r->name = "fs2";
4168  break;
4169  case GDB_REGNO_FS3:
4170  r->name = "fs3";
4171  break;
4172  case GDB_REGNO_FS4:
4173  r->name = "fs4";
4174  break;
4175  case GDB_REGNO_FS5:
4176  r->name = "fs5";
4177  break;
4178  case GDB_REGNO_FS6:
4179  r->name = "fs6";
4180  break;
4181  case GDB_REGNO_FS7:
4182  r->name = "fs7";
4183  break;
4184  case GDB_REGNO_FS8:
4185  r->name = "fs8";
4186  break;
4187  case GDB_REGNO_FS9:
4188  r->name = "fs9";
4189  break;
4190  case GDB_REGNO_FS10:
4191  r->name = "fs10";
4192  break;
4193  case GDB_REGNO_FS11:
4194  r->name = "fs11";
4195  break;
4196  case GDB_REGNO_FT8:
4197  r->name = "ft8";
4198  break;
4199  case GDB_REGNO_FT9:
4200  r->name = "ft9";
4201  break;
4202  case GDB_REGNO_FT10:
4203  r->name = "ft10";
4204  break;
4205  case GDB_REGNO_FT11:
4206  r->name = "ft11";
4207  break;
4208  }
4209  r->group = "float";
4210  r->feature = &feature_fpu;
4211  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
4212  r->group = "csr";
4213  r->feature = &feature_csr;
4214  unsigned int csr_number = number - GDB_REGNO_CSR0;
4215 
4216  while (csr_info[csr_info_index].number < csr_number &&
4217  csr_info_index < ARRAY_SIZE(csr_info) - 1) {
4218  csr_info_index++;
4219  }
4220  if (csr_info[csr_info_index].number == csr_number) {
4221  r->name = csr_info[csr_info_index].name;
4222  } else {
4223  sprintf(reg_name, "csr%d", csr_number);
4224  /* Assume unnamed registers don't exist, unless we have some
4225  * configuration that tells us otherwise. That's important
4226  * because eg. Eclipse crashes if a target has too many
4227  * registers, and apparently has no way of only showing a
4228  * subset of registers in any case. */
4229  r->exist = false;
4230  }
4231 
4232  switch (csr_number) {
4233  case CSR_FFLAGS:
4234  case CSR_FRM:
4235  case CSR_FCSR:
4237  r->group = "float";
4238  r->feature = &feature_fpu;
4239  break;
4240  case CSR_SSTATUS:
4241  case CSR_STVEC:
4242  case CSR_SIP:
4243  case CSR_SIE:
4244  case CSR_SCOUNTEREN:
4245  case CSR_SSCRATCH:
4246  case CSR_SEPC:
4247  case CSR_SCAUSE:
4248  case CSR_STVAL:
4249  case CSR_SATP:
4251  break;
4252  case CSR_MEDELEG:
4253  case CSR_MIDELEG:
4254  /* "In systems with only M-mode, or with both M-mode and
4255  * U-mode but without U-mode trap support, the medeleg and
4256  * mideleg registers should not exist." */
4257  r->exist = riscv_supports_extension(target, 'S') ||
4259  break;
4260 
4261  case CSR_PMPCFG1:
4262  case CSR_PMPCFG3:
4263  case CSR_CYCLEH:
4264  case CSR_TIMEH:
4265  case CSR_INSTRETH:
4266  case CSR_HPMCOUNTER3H:
4267  case CSR_HPMCOUNTER4H:
4268  case CSR_HPMCOUNTER5H:
4269  case CSR_HPMCOUNTER6H:
4270  case CSR_HPMCOUNTER7H:
4271  case CSR_HPMCOUNTER8H:
4272  case CSR_HPMCOUNTER9H:
4273  case CSR_HPMCOUNTER10H:
4274  case CSR_HPMCOUNTER11H:
4275  case CSR_HPMCOUNTER12H:
4276  case CSR_HPMCOUNTER13H:
4277  case CSR_HPMCOUNTER14H:
4278  case CSR_HPMCOUNTER15H:
4279  case CSR_HPMCOUNTER16H:
4280  case CSR_HPMCOUNTER17H:
4281  case CSR_HPMCOUNTER18H:
4282  case CSR_HPMCOUNTER19H:
4283  case CSR_HPMCOUNTER20H:
4284  case CSR_HPMCOUNTER21H:
4285  case CSR_HPMCOUNTER22H:
4286  case CSR_HPMCOUNTER23H:
4287  case CSR_HPMCOUNTER24H:
4288  case CSR_HPMCOUNTER25H:
4289  case CSR_HPMCOUNTER26H:
4290  case CSR_HPMCOUNTER27H:
4291  case CSR_HPMCOUNTER28H:
4292  case CSR_HPMCOUNTER29H:
4293  case CSR_HPMCOUNTER30H:
4294  case CSR_HPMCOUNTER31H:
4295  case CSR_MCYCLEH:
4296  case CSR_MINSTRETH:
4297  case CSR_MHPMCOUNTER3H:
4298  case CSR_MHPMCOUNTER4H:
4299  case CSR_MHPMCOUNTER5H:
4300  case CSR_MHPMCOUNTER6H:
4301  case CSR_MHPMCOUNTER7H:
4302  case CSR_MHPMCOUNTER8H:
4303  case CSR_MHPMCOUNTER9H:
4304  case CSR_MHPMCOUNTER10H:
4305  case CSR_MHPMCOUNTER11H:
4306  case CSR_MHPMCOUNTER12H:
4307  case CSR_MHPMCOUNTER13H:
4308  case CSR_MHPMCOUNTER14H:
4309  case CSR_MHPMCOUNTER15H:
4310  case CSR_MHPMCOUNTER16H:
4311  case CSR_MHPMCOUNTER17H:
4312  case CSR_MHPMCOUNTER18H:
4313  case CSR_MHPMCOUNTER19H:
4314  case CSR_MHPMCOUNTER20H:
4315  case CSR_MHPMCOUNTER21H:
4316  case CSR_MHPMCOUNTER22H:
4317  case CSR_MHPMCOUNTER23H:
4318  case CSR_MHPMCOUNTER24H:
4319  case CSR_MHPMCOUNTER25H:
4320  case CSR_MHPMCOUNTER26H:
4321  case CSR_MHPMCOUNTER27H:
4322  case CSR_MHPMCOUNTER28H:
4323  case CSR_MHPMCOUNTER29H:
4324  case CSR_MHPMCOUNTER30H:
4325  case CSR_MHPMCOUNTER31H:
4326  r->exist = riscv_xlen(target) == 32;
4327  break;
4328 
4329  case CSR_VSTART:
4330  case CSR_VXSAT:
4331  case CSR_VXRM:
4332  case CSR_VL:
4333  case CSR_VTYPE:
4334  case CSR_VLENB:
4336  break;
4337  }
4338 
4339  if (!r->exist && !list_empty(&info->expose_csr)) {
4340  range_list_t *entry;
4341  list_for_each_entry(entry, &info->expose_csr, list)
4342  if ((entry->low <= csr_number) && (csr_number <= entry->high)) {
4343  if (entry->name) {
4344  *reg_name = 0;
4345  r->name = entry->name;
4346  }
4347 
4348  LOG_DEBUG("Exposing additional CSR %d (name=%s)",
4349  csr_number, entry->name ? entry->name : reg_name);
4350 
4351  r->exist = true;
4352  break;
4353  }
4354  }
4355 
4356  } else if (number == GDB_REGNO_PRIV) {
4357  sprintf(reg_name, "priv");
4358  r->group = "general";
4359  r->feature = &feature_virtual;
4360  r->size = 8;
4361 
4362  } else if (number >= GDB_REGNO_V0 && number <= GDB_REGNO_V31) {
4363  r->caller_save = false;
4364  r->exist = riscv_supports_extension(target, 'V') && info->vlenb;
4365  r->size = info->vlenb * 8;
4366  sprintf(reg_name, "v%d", number - GDB_REGNO_V0);
4367  r->group = "vector";
4368  r->feature = &feature_vector;
4369  r->reg_data_type = &info->type_vector;
4370 
4371  } else if (number >= GDB_REGNO_COUNT) {
4372  /* Custom registers. */
4373  assert(!list_empty(&info->expose_custom));
4374 
4375  range_list_t *range = list_first_entry(&info->expose_custom, range_list_t, list);
4376 
4377  unsigned int custom_number = range->low + custom_within_range;
4378 
4379  r->group = "custom";
4380  r->feature = &feature_custom;
4381  r->arch_info = calloc(1, sizeof(riscv_reg_info_t));
4382  if (!r->arch_info)
4383  return ERROR_FAIL;
4384  ((riscv_reg_info_t *) r->arch_info)->target = target;
4385  ((riscv_reg_info_t *) r->arch_info)->custom_number = custom_number;
4386  sprintf(reg_name, "custom%d", custom_number);
4387 
4388  if (range->name) {
4389  *reg_name = 0;
4390  r->name = range->name;
4391  }
4392 
4393  LOG_DEBUG("Exposing additional custom register %d (name=%s)",
4394  number, range->name ? range->name : reg_name);
4395 
4396  custom_within_range++;
4397  if (custom_within_range > range->high - range->low) {
4398  custom_within_range = 0;
4399  list_rotate_left(&info->expose_custom);
4400  }
4401  }
4402 
4403  if (reg_name[0]) {
4404  r->name = reg_name;
4405  reg_name += strlen(reg_name) + 1;
4406  assert(reg_name < info->reg_names + target->reg_cache->num_regs *
4407  max_reg_name_len);
4408  }
4409  r->value = calloc(1, DIV_ROUND_UP(r->size, 8));
4410  }
4411 
4412  return ERROR_OK;
4413 }
4414 
4415 
4418 {
4420 
4421  memset(ctxt->tunneled_dr, 0, sizeof(ctxt->tunneled_dr));
4423  ctxt->tunneled_dr[3].num_bits = 1;
4424  ctxt->tunneled_dr[3].out_value = bscan_one;
4425  ctxt->tunneled_dr[2].num_bits = 7;
4426  ctxt->tunneled_dr_width = field->num_bits;
4427  ctxt->tunneled_dr[2].out_value = &ctxt->tunneled_dr_width;
4428  /* for BSCAN tunnel, there is a one-TCK skew between shift in and shift out, so
4429  scanning num_bits + 1, and then will right shift the input field after executing the queues */
4430 
4431  ctxt->tunneled_dr[1].num_bits = field->num_bits + 1;
4432  ctxt->tunneled_dr[1].out_value = field->out_value;
4433  ctxt->tunneled_dr[1].in_value = field->in_value;
4434 
4435  ctxt->tunneled_dr[0].num_bits = 3;
4436  ctxt->tunneled_dr[0].out_value = bscan_zero;
4437  } else {
4438  /* BSCAN_TUNNEL_NESTED_TAP */
4439  ctxt->tunneled_dr[0].num_bits = 1;
4440  ctxt->tunneled_dr[0].out_value = bscan_one;
4441  ctxt->tunneled_dr[1].num_bits = 7;
4442  ctxt->tunneled_dr_width = field->num_bits;
4443  ctxt->tunneled_dr[1].out_value = &ctxt->tunneled_dr_width;
4444  /* for BSCAN tunnel, there is a one-TCK skew between shift in and shift out, so
4445  scanning num_bits + 1, and then will right shift the input field after executing the queues */
4446  ctxt->tunneled_dr[2].num_bits = field->num_bits + 1;
4447  ctxt->tunneled_dr[2].out_value = field->out_value;
4448  ctxt->tunneled_dr[2].in_value = field->in_value;
4449  ctxt->tunneled_dr[3].num_bits = 3;
4450  ctxt->tunneled_dr[3].out_value = bscan_zero;
4451  }
4453 }
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
Definition: algorithm.c:29
void destroy_reg_param(struct reg_param *param)
Definition: algorithm.c:38
@ PARAM_OUT
Definition: algorithm.h:16
@ PARAM_IN
Definition: algorithm.h:15
@ PARAM_IN_OUT
Definition: algorithm.h:17
enum arm_mode mode
Definition: armv4_5.c:281
const char * name
Definition: armv4_5.c:76
char * buf_to_hex_str(const void *_buf, unsigned int buf_len)
Definition: binarybuffer.c:178
void * buf_cpy(const void *from, void *_to, unsigned int size)
Copies size bits out of from and into to.
Definition: binarybuffer.c:43
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
static uint64_t buf_get_u64(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 64-bit word.
Definition: binarybuffer.h:134
static void buf_set_u64(uint8_t *_buffer, unsigned int first, unsigned int num, uint64_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:65
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:420
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:118
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:530
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:442
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static int halted(struct target *target, const char *label)
Definition: davinci.c:58
#define CSR_MCONTROL6_VS
#define CSR_MCONTROL6_LOAD
#define CSR_MCONTROL6_ACTION
#define CSR_MCONTROL6_EXECUTE
#define CSR_MCONTROL6_STORE
#define CSR_MCONTROL6_M
#define CSR_MCONTROL6_S
#define CSR_MCONTROL6_U
#define CSR_MCONTROL6_VU
#define CSR_MCONTROL6_MATCH
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
#define PTE_W
Definition: encoding.h:287
#define CSR_MHPMCOUNTER17H
Definition: encoding.h:3165
#define MCONTROL_M
Definition: encoding.h:107
#define CSR_HPMCOUNTER25H
Definition: encoding.h:3106
#define CSR_MHPMCOUNTER7H
Definition: encoding.h:3155
#define CSR_MHPMCOUNTER27H
Definition: encoding.h:3175
#define MCONTROL_U
Definition: encoding.h:110
#define MCONTROL_EXECUTE
Definition: encoding.h:111
#define CSR_PMPCFG1
Definition: encoding.h:2916
#define CSR_HPMCOUNTER22H
Definition: encoding.h:3103
#define CSR_MHPMCOUNTER18H
Definition: encoding.h:3166
#define CSR_MHPMCOUNTER21H
Definition: encoding.h:3169
#define MSTATUS_MIE
Definition: encoding.h:16
#define CSR_HPMCOUNTER15H
Definition: encoding.h:3096
#define MCONTROL_ACTION_DEBUG_MODE
Definition: encoding.h:119
#define MSTATUS_SIE
Definition: encoding.h:14
#define MCONTROL_MATCH
Definition: encoding.h:106
#define MSTATUS_MPP
Definition: encoding.h:23
#define CSR_HPMCOUNTER12H
Definition: encoding.h:3093
#define CSR_HPMCOUNTER19H
Definition: encoding.h:3100
#define CSR_TIMEH
Definition: encoding.h:3082
#define CSR_MHPMCOUNTER28H
Definition: encoding.h:3176
#define CSR_HPMCOUNTER3H
Definition: encoding.h:3084
#define CSR_MHPMCOUNTER9H
Definition: encoding.h:3157
#define CSR_SEPC
Definition: encoding.h:2843
#define PTE_R
Definition: encoding.h:286
#define CSR_MHPMCOUNTER29H
Definition: encoding.h:3177
#define MSTATUS_UIE
Definition: encoding.h:13
#define CSR_MHPMCOUNTER10H
Definition: encoding.h:3158
#define CSR_HPMCOUNTER4H
Definition: encoding.h:3085
#define CSR_PMPCFG3
Definition: encoding.h:2918
#define CSR_MHPMCOUNTER11H
Definition: encoding.h:3159
#define CSR_SCAUSE
Definition: encoding.h:2844
#define CSR_HPMCOUNTER9H
Definition: encoding.h:3090
#define MCONTROL_TYPE(xlen)
Definition: encoding.h:98
#define CSR_MHPMCOUNTER12H
Definition: encoding.h:3160
#define CSR_HPMCOUNTER31H
Definition: encoding.h:3112
#define CSR_SSCRATCH
Definition: encoding.h:2842
#define CSR_MHPMCOUNTER25H
Definition: encoding.h:3173
#define PTE_PPN_SHIFT
Definition: encoding.h:299
#define CSR_HPMCOUNTER6H
Definition: encoding.h:3087
#define CSR_MHPMCOUNTER26H
Definition: encoding.h:3174
#define CSR_HPMCOUNTER27H
Definition: encoding.h:3108
#define CSR_HPMCOUNTER17H
Definition: encoding.h:3098
#define CSR_MHPMCOUNTER5H
Definition: encoding.h:3153
#define CSR_HPMCOUNTER21H
Definition: encoding.h:3102
#define CSR_SSTATUS
Definition: encoding.h:2831
#define CSR_MHPMCOUNTER24H
Definition: encoding.h:3172
#define CSR_HPMCOUNTER18H
Definition: encoding.h:3099
#define MATCH_LB
Definition: encoding.h:1235
#define CSR_STVAL
Definition: encoding.h:2845
#define CSR_MHPMCOUNTER8H
Definition: encoding.h:3156
#define CSR_MHPMCOUNTER3H
Definition: encoding.h:3151
#define CSR_HPMCOUNTER14H
Definition: encoding.h:3095
#define CSR_MHPMCOUNTER20H
Definition: encoding.h:3168
#define MCONTROL_LOAD
Definition: encoding.h:113
#define CSR_VLENB
Definition: encoding.h:2830
#define SATP_MODE_SV32
Definition: encoding.h:237
#define SATP_MODE_SV39
Definition: encoding.h:238
#define CSR_HPMCOUNTER28H
Definition: encoding.h:3109
#define CSR_SATP
Definition: encoding.h:2848
#define CSR_MHPMCOUNTER31H
Definition: encoding.h:3179
#define CSR_VTYPE
Definition: encoding.h:2829
#define PTE_V
Definition: encoding.h:285
#define MCONTROL_MATCH_EQUAL
Definition: encoding.h:124
#define MATCH_SB
Definition: encoding.h:1391
#define CSR_MHPMCOUNTER6H
Definition: encoding.h:3154
#define CSR_HPMCOUNTER16H
Definition: encoding.h:3097
#define MCONTROL_ACTION
Definition: encoding.h:104
#define CSR_MHPMCOUNTER23H
Definition: encoding.h:3171
#define CSR_MINSTRETH
Definition: encoding.h:3150
#define CSR_HPMCOUNTER20H
Definition: encoding.h:3101
#define CSR_FRM
Definition: encoding.h:2789
#define SATP_MODE_SV48
Definition: encoding.h:239
#define CSR_MHPMCOUNTER15H
Definition: encoding.h:3163
#define MCONTROL_DMODE(xlen)
Definition: encoding.h:99
#define CSR_STVEC
Definition: encoding.h:2835
#define CSR_MEDELEG
Definition: encoding.h:2897
#define CSR_HPMCOUNTER23H
Definition: encoding.h:3104
#define CSR_VL
Definition: encoding.h:2828
#define MSTATUS_HIE
Definition: encoding.h:15
#define CSR_HPMCOUNTER30H
Definition: encoding.h:3111
#define CSR_MHPMCOUNTER14H
Definition: encoding.h:3162
#define CSR_FCSR
Definition: encoding.h:2790
#define CSR_HPMCOUNTER5H
Definition: encoding.h:3086
#define CSR_HPMCOUNTER13H
Definition: encoding.h:3094
#define CSR_SIP
Definition: encoding.h:2846
#define MCONTROL_S
Definition: encoding.h:109
#define CSR_HPMCOUNTER7H
Definition: encoding.h:3088
#define CSR_MHPMCOUNTER13H
Definition: encoding.h:3161
#define SATP_MODE_OFF
Definition: encoding.h:236
#define CSR_HPMCOUNTER26H
Definition: encoding.h:3107
#define CSR_SCOUNTEREN
Definition: encoding.h:2836
#define CSR_VXRM
Definition: encoding.h:2793
#define CSR_MHPMCOUNTER4H
Definition: encoding.h:3152
#define PTE_X
Definition: encoding.h:288
#define CSR_HPMCOUNTER8H
Definition: encoding.h:3089
#define CSR_MCYCLEH
Definition: encoding.h:3149
#define CSR_MIDELEG
Definition: encoding.h:2898
#define CSR_VXSAT
Definition: encoding.h:2792
#define CSR_HPMCOUNTER11H
Definition: encoding.h:3092
#define MCONTROL_STORE
Definition: encoding.h:112
#define CSR_HPMCOUNTER24H
Definition: encoding.h:3105
#define CSR_INSTRETH
Definition: encoding.h:3083
#define CSR_FFLAGS
Definition: encoding.h:2788
#define CSR_MHPMCOUNTER22H
Definition: encoding.h:3170
#define CSR_HPMCOUNTER29H
Definition: encoding.h:3110
#define CSR_HPMCOUNTER10H
Definition: encoding.h:3091
#define CSR_MHPMCOUNTER19H
Definition: encoding.h:3167
#define CSR_CYCLEH
Definition: encoding.h:3081
#define CSR_MHPMCOUNTER16H
Definition: encoding.h:3164
#define CSR_VSTART
Definition: encoding.h:2791
#define CSR_MHPMCOUNTER30H
Definition: encoding.h:3178
#define MSTATUS_MPRV
Definition: encoding.h:26
#define CSR_SIE
Definition: encoding.h:2834
#define PRV_M
Definition: encoding.h:225
enum esirisc_reg_num number
Definition: esirisc.c:87
int mask
Definition: esirisc.c:1740
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
uint8_t length
Definition: esp_usb_jtag.c:1
static uint16_t direction
Definition: ftdi.c:120
gdb_regno
Definition: gdb_regs.h:8
@ GDB_REGNO_DPC
Definition: gdb_regs.h:91
@ GDB_REGNO_V18
Definition: gdb_regs.h:108
@ GDB_REGNO_S8
Definition: gdb_regs.h:35
@ GDB_REGNO_V19
Definition: gdb_regs.h:108
@ GDB_REGNO_FS7
Definition: gdb_regs.h:70
@ GDB_REGNO_FS10
Definition: gdb_regs.h:73
@ GDB_REGNO_SATP
Definition: gdb_regs.h:97
@ GDB_REGNO_CSR0
Definition: gdb_regs.h:80
@ GDB_REGNO_FS8
Definition: gdb_regs.h:71
@ GDB_REGNO_MSTATUS
Definition: gdb_regs.h:94
@ GDB_REGNO_FS6
Definition: gdb_regs.h:69
@ GDB_REGNO_S4
Definition: gdb_regs.h:31
@ GDB_REGNO_V15
Definition: gdb_regs.h:107
@ GDB_REGNO_S11
Definition: gdb_regs.h:38
@ GDB_REGNO_V22
Definition: gdb_regs.h:109
@ GDB_REGNO_V2
Definition: gdb_regs.h:104
@ GDB_REGNO_VXRM
Definition: gdb_regs.h:83
@ GDB_REGNO_ZERO
Definition: gdb_regs.h:9
@ GDB_REGNO_V1
Definition: gdb_regs.h:104
@ GDB_REGNO_S5
Definition: gdb_regs.h:32
@ GDB_REGNO_T5
Definition: gdb_regs.h:41
@ GDB_REGNO_VTYPE
Definition: gdb_regs.h:86
@ GDB_REGNO_T6
Definition: gdb_regs.h:42
@ GDB_REGNO_A4
Definition: gdb_regs.h:24
@ GDB_REGNO_GP
Definition: gdb_regs.h:12
@ GDB_REGNO_FA3
Definition: gdb_regs.h:60
@ GDB_REGNO_VXSAT
Definition: gdb_regs.h:82
@ GDB_REGNO_S7
Definition: gdb_regs.h:34
@ GDB_REGNO_A1
Definition: gdb_regs.h:21
@ GDB_REGNO_FT3
Definition: gdb_regs.h:50
@ GDB_REGNO_V7
Definition: gdb_regs.h:105
@ GDB_REGNO_V21
Definition: gdb_regs.h:109
@ GDB_REGNO_V14
Definition: gdb_regs.h:107
@ GDB_REGNO_A5
Definition: gdb_regs.h:25
@ GDB_REGNO_TSELECT
Definition: gdb_regs.h:87
@ GDB_REGNO_T2
Definition: gdb_regs.h:16
@ GDB_REGNO_S1
Definition: gdb_regs.h:19
@ GDB_REGNO_XPR15
Definition: gdb_regs.h:26
@ GDB_REGNO_T3
Definition: gdb_regs.h:39
@ GDB_REGNO_V26
Definition: gdb_regs.h:110
@ GDB_REGNO_SP
Definition: gdb_regs.h:11
@ GDB_REGNO_FA2
Definition: gdb_regs.h:59
@ GDB_REGNO_FA0
Definition: gdb_regs.h:57
@ GDB_REGNO_FPR31
Definition: gdb_regs.h:79
@ GDB_REGNO_V12
Definition: gdb_regs.h:107
@ GDB_REGNO_V24
Definition: gdb_regs.h:110
@ GDB_REGNO_FPR0
Definition: gdb_regs.h:46
@ GDB_REGNO_V11
Definition: gdb_regs.h:106
@ GDB_REGNO_FT5
Definition: gdb_regs.h:52
@ GDB_REGNO_V0
Definition: gdb_regs.h:104
@ GDB_REGNO_V29
Definition: gdb_regs.h:111
@ GDB_REGNO_DSCRATCH0
Definition: gdb_regs.h:93
@ GDB_REGNO_A6
Definition: gdb_regs.h:27
@ GDB_REGNO_FP
Definition: gdb_regs.h:18
@ GDB_REGNO_VL
Definition: gdb_regs.h:85
@ GDB_REGNO_TDATA1
Definition: gdb_regs.h:88
@ GDB_REGNO_V16
Definition: gdb_regs.h:108
@ GDB_REGNO_VSTART
Definition: gdb_regs.h:81
@ GDB_REGNO_FA1
Definition: gdb_regs.h:58
@ GDB_REGNO_XPR31
Definition: gdb_regs.h:43
@ GDB_REGNO_A0
Definition: gdb_regs.h:20
@ GDB_REGNO_MEPC
Definition: gdb_regs.h:95
@ GDB_REGNO_FS4
Definition: gdb_regs.h:67
@ GDB_REGNO_FT9
Definition: gdb_regs.h:76
@ GDB_REGNO_FT2
Definition: gdb_regs.h:49
@ GDB_REGNO_V28
Definition: gdb_regs.h:111
@ GDB_REGNO_FA4
Definition: gdb_regs.h:61
@ GDB_REGNO_V6
Definition: gdb_regs.h:105
@ GDB_REGNO_V25
Definition: gdb_regs.h:110
@ GDB_REGNO_FT7
Definition: gdb_regs.h:54
@ GDB_REGNO_FS0
Definition: gdb_regs.h:55
@ GDB_REGNO_FT6
Definition: gdb_regs.h:53
@ GDB_REGNO_FS9
Definition: gdb_regs.h:72
@ GDB_REGNO_FT1
Definition: gdb_regs.h:48
@ GDB_REGNO_FT8
Definition: gdb_regs.h:75
@ GDB_REGNO_FT11
Definition: gdb_regs.h:78
@ GDB_REGNO_A2
Definition: gdb_regs.h:22
@ GDB_REGNO_V20
Definition: gdb_regs.h:109
@ GDB_REGNO_V13
Definition: gdb_regs.h:107
@ GDB_REGNO_V17
Definition: gdb_regs.h:108
@ GDB_REGNO_FS3
Definition: gdb_regs.h:66
@ GDB_REGNO_A7
Definition: gdb_regs.h:28
@ GDB_REGNO_RA
Definition: gdb_regs.h:10
@ GDB_REGNO_V5
Definition: gdb_regs.h:105
@ GDB_REGNO_S9
Definition: gdb_regs.h:36
@ GDB_REGNO_PC
Definition: gdb_regs.h:45
@ GDB_REGNO_S0
Definition: gdb_regs.h:17
@ GDB_REGNO_T4
Definition: gdb_regs.h:40
@ GDB_REGNO_VLENB
Definition: gdb_regs.h:84
@ GDB_REGNO_FA5
Definition: gdb_regs.h:62
@ GDB_REGNO_V9
Definition: gdb_regs.h:106
@ GDB_REGNO_V27
Definition: gdb_regs.h:110
@ GDB_REGNO_V31
Definition: gdb_regs.h:111
@ GDB_REGNO_FA6
Definition: gdb_regs.h:63
@ GDB_REGNO_V3
Definition: gdb_regs.h:104
@ GDB_REGNO_S10
Definition: gdb_regs.h:37
@ GDB_REGNO_FT10
Definition: gdb_regs.h:77
@ GDB_REGNO_PRIV
Definition: gdb_regs.h:99
@ GDB_REGNO_S2
Definition: gdb_regs.h:29
@ GDB_REGNO_V4
Definition: gdb_regs.h:105
@ GDB_REGNO_CSR4095
Definition: gdb_regs.h:98
@ GDB_REGNO_FS1
Definition: gdb_regs.h:56
@ GDB_REGNO_FA7
Definition: gdb_regs.h:64
@ GDB_REGNO_TP
Definition: gdb_regs.h:13
@ GDB_REGNO_MCAUSE
Definition: gdb_regs.h:96
@ GDB_REGNO_TDATA2
Definition: gdb_regs.h:89
@ GDB_REGNO_FS2
Definition: gdb_regs.h:65
@ GDB_REGNO_FT0
Definition: gdb_regs.h:47
@ GDB_REGNO_COUNT
Definition: gdb_regs.h:112
@ GDB_REGNO_V23
Definition: gdb_regs.h:109
@ GDB_REGNO_T1
Definition: gdb_regs.h:15
@ GDB_REGNO_S6
Definition: gdb_regs.h:33
@ GDB_REGNO_V10
Definition: gdb_regs.h:106
@ GDB_REGNO_FT4
Definition: gdb_regs.h:51
@ GDB_REGNO_S3
Definition: gdb_regs.h:30
@ GDB_REGNO_T0
Definition: gdb_regs.h:14
@ GDB_REGNO_FS11
Definition: gdb_regs.h:74
@ GDB_REGNO_MISA
Definition: gdb_regs.h:90
@ GDB_REGNO_DCSR
Definition: gdb_regs.h:92
@ GDB_REGNO_V30
Definition: gdb_regs.h:111
@ GDB_REGNO_A3
Definition: gdb_regs.h:23
@ GDB_REGNO_FS5
Definition: gdb_regs.h:68
@ GDB_REGNO_V8
Definition: gdb_regs.h:106
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1050
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, enum tap_state state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:457
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, enum tap_state state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:380
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_IDLE
Definition: jtag.h:53
static void list_add(struct list_head *new, struct list_head *head)
Definition: list.h:193
#define list_first_entry(ptr, type, member)
Definition: list.h:127
static int list_empty(const struct list_head *head)
Definition: list.h:60
#define list_for_each_entry_safe(p, n, h, field)
Definition: list.h:155
#define list_for_each_entry(p, h, field)
Definition: list.h:151
static void list_rotate_left(struct list_head *h)
list_rotate_left - rotate the list to the left
Definition: list.h:364
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:53
void keep_alive(void)
Definition: log.c:426
static int64_t start
Definition: log.c:54
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:178
#define LOG_WARNING(expr ...)
Definition: log.h:130
#define ERROR_FAIL
Definition: log.h:174
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:162
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_INFO(expr ...)
Definition: log.h:127
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
static uint32_t ebreak(void) __attribute__((unused))
Definition: opcodes.h:219
static uint32_t ebreak_c(void) __attribute__((unused))
Definition: opcodes.h:224
struct reg * register_get_by_name(struct reg_cache *first, const char *name, bool search_all)
Definition: register.c:50
void register_cache_invalidate(struct reg_cache *cache)
Marks the contents of the register cache as invalid (and clean).
Definition: register.c:94
reg_type
Definition: register.h:19
@ REG_TYPE_UINT16
Definition: register.h:29
@ REG_TYPE_IEEE_DOUBLE
Definition: register.h:37
@ REG_TYPE_UINT32
Definition: register.h:30
@ REG_TYPE_UINT128
Definition: register.h:32
@ REG_TYPE_UINT64
Definition: register.h:31
@ REG_TYPE_ARCH_DEFINED
Definition: register.h:38
@ REG_TYPE_IEEE_SINGLE
Definition: register.h:36
@ REG_TYPE_UINT8
Definition: register.h:28
@ REG_TYPE_CLASS_VECTOR
Definition: register.h:93
@ REG_TYPE_CLASS_UNION
Definition: register.h:94
struct target_type riscv011_target
Definition: riscv-011.c:2395
slot
Definition: riscv-011.c:122
struct target_type riscv013_target
Definition: riscv-013.c:4046
int riscv_reset_timeout_sec
Definition: riscv.c:206
static int halt_finish(struct target *target)
Definition: riscv.c:1219
const char * gdb_regno_name(enum gdb_regno regno)
Definition: riscv.c:3513
static struct scan_field _bscan_tunnel_data_register_select_dmi[]
Definition: riscv.c:142
static int riscv_create_target(struct target *target, Jim_Interp *interp)
Definition: riscv.c:430
static int remove_trigger(struct target *target, struct trigger *trigger)
Definition: riscv.c:913
struct scan_field select_idcode
Definition: riscv.c:123
bool riscv_ebreaks
Definition: riscv.c:210
riscv_poll_hart
Definition: riscv.c:2077
@ RPH_ERROR
Definition: riscv.c:2081
@ RPH_NO_CHANGE
Definition: riscv.c:2078
@ RPH_DISCOVERED_HALTED
Definition: riscv.c:2079
@ RPH_DISCOVERED_RUNNING
Definition: riscv.c:2080
static bool riscv_enable_virt2phys
Definition: riscv.c:208
static int set_debug_reason(struct target *target, enum riscv_halt_reason halt_reason)
Definition: riscv.c:2108
unsigned int riscv_xlen(const struct target *target)
Definition: riscv.c:3205
int riscv_set_register(struct target *target, enum gdb_regno regid, riscv_reg_t value)
This function is called when the debug user wants to change the value of a register.
Definition: riscv.c:3306
static struct scan_field select_user4
Definition: riscv.c:135
static bscan_tunnel_type_t bscan_tunnel_type
Definition: riscv.c:128
static int riscv_halt_go_all_harts(struct target *target)
Definition: riscv.c:1184
struct scan_field select_dbus
Definition: riscv.c:118
int riscv_set_current_hartid(struct target *target, int hartid)
Definition: riscv.c:3211
static int maybe_add_trigger_t6(struct target *target, struct trigger *trigger, uint64_t tdata1)
Definition: riscv.c:637
static int riscv_step_rtos_hart(struct target *target)
Definition: riscv.c:3168
static int riscv_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: riscv.c:1989
static struct reg_arch_type riscv_reg_arch_type
Definition: riscv.c:3773
static enum @120 resume_order
static uint32_t bscan_tunnel_data_register_select_dmi_num_fields
Definition: riscv.c:191
static const struct command_registration riscv_command_handlers[]
Definition: riscv.c:3035
int riscv_write_debug_buffer(struct target *target, int index, riscv_insn_t insn)
Definition: riscv.c:3395
bool riscv_is_halted(struct target *target)
Definition: riscv.c:3370
int riscv_read_by_any_size(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Read one memory item using any memory access size that will work.
Definition: riscv.c:837
static int riscv_read_phys_memory(struct target *target, target_addr_t phys_address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: riscv.c:1689
int riscv_init_registers(struct target *target)
Definition: riscv.c:3788
static int riscv_write_phys_memory(struct target *target, target_addr_t phys_address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: riscv.c:1717
int riscv_halt(struct target *target)
Definition: riscv.c:1224
bool riscv_ebreakm
Definition: riscv.c:209
static int riscv_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: riscv.c:945
static int add_trigger(struct target *target, struct trigger *trigger)
Definition: riscv.c:689
static int riscv_address_translate(struct target *target, target_addr_t virtual, target_addr_t *physical)
Definition: riscv.c:1568
static int register_set(struct reg *reg, uint8_t *buf)
Definition: riscv.c:3725
int riscv_write_by_any_size(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Write one memory item using any memory access size that will work.
Definition: riscv.c:805
static void trigger_from_watchpoint(struct trigger *trigger, const struct watchpoint *watchpoint)
Definition: riscv.c:974
static int riscv_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution, bool single_hart)
Definition: riscv.c:1464
static uint8_t bscan_tunneled_ir_width[4]
Definition: riscv.c:141
static const virt2phys_info_t sv32
Definition: riscv.c:220
static int riscv_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: riscv.c:442
static int old_or_new_riscv_poll(struct target *target)
Definition: riscv.c:1150
static int riscv_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: riscv.c:865
void riscv_fill_dmi_nop_u64(struct target *target, char *buf)
Definition: riscv.c:3426
static int riscv_mmu(struct target *target, int *enabled)
Definition: riscv.c:1523
static int riscv_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Definition: riscv.c:1813
void riscv_add_bscan_tunneled_scan(struct target *target, struct scan_field *field, riscv_bscan_tunneled_scan_context_t *ctxt)
Definition: riscv.c:4416
enum slot slot_t
static void riscv_sample_buf_maybe_add_timestamp(struct target *target, bool before)
Definition: riscv.c:264
COMMAND_HELPER(riscv_print_info_line, const char *section, const char *key, unsigned int value)
Definition: riscv.c:2835
dbus_status_t
Definition: riscv.c:63
@ DBUS_STATUS_BUSY
Definition: riscv.c:66
@ DBUS_STATUS_FAILED
Definition: riscv.c:65
@ DBUS_STATUS_SUCCESS
Definition: riscv.c:64
static int maybe_add_trigger_t2(struct target *target, struct trigger *trigger, uint64_t tdata1)
Definition: riscv.c:587
static int enable_triggers(struct target *target, riscv_reg_t *state)
Definition: riscv.c:1356
static void riscv_free_registers(struct target *target)
Definition: riscv.c:472
static const virt2phys_info_t sv48
Definition: riscv.c:246
int riscv_current_hartid(const struct target *target)
Definition: riscv.c:3237
int riscv_select_current_hart(struct target *target)
Definition: riscv.c:1159
@ SLOT0
Definition: riscv.c:73
@ SLOT1
Definition: riscv.c:74
@ SLOT_LAST
Definition: riscv.c:75
#define get_field(reg, mask)
Definition: riscv.c:26
static struct scan_field _bscan_tunnel_nested_tap_select_dmi[]
Definition: riscv.c:165
int riscv_execute_debug_buffer(struct target *target)
Definition: riscv.c:3408
size_t riscv_debug_buffer_size(struct target *target)
Definition: riscv.c:3389
static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
Definition: riscv.c:377
struct target_type riscv_target
Definition: riscv.c:3069
static int resume_prep(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Get everything ready to resume.
Definition: riscv.c:1396
struct scan_field select_dtmcontrol
Definition: riscv.c:113
#define DTMCONTROL_VERSION
Definition: riscv.c:53
static const struct command_registration riscv_exec_command_handlers[]
Definition: riscv.c:2862
static void riscv_info_init(struct target *target, struct riscv_info *r)
Definition: riscv.c:3121
static uint8_t ir_dtmcontrol[4]
Definition: riscv.c:112
static int riscv_get_gdb_reg_list_internal(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class, bool read)
Definition: riscv.c:1757
uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out)
Definition: riscv.c:293
static int riscv_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: riscv.c:1698
static int riscv_resume_prep_all_harts(struct target *target)
Definition: riscv.c:1286
static bool gdb_regno_cacheable(enum gdb_regno regno, bool write)
If write is true: return true iff we are guaranteed that the register will contain exactly the value ...
Definition: riscv.c:3261
static int maybe_add_trigger_t1(struct target *target, struct trigger *trigger, uint64_t tdata1)
Definition: riscv.c:534
static enum riscv_halt_reason riscv_halt_reason(struct target *target, int hartid)
Definition: riscv.c:3377
static int cmp_csr_info(const void *p1, const void *p2)
Definition: riscv.c:3783
static uint8_t ir_user4[4]
Definition: riscv.c:134
static int riscv_target_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: riscv.c:1516
int riscv_openocd_poll(struct target *target)
Definition: riscv.c:2182
static unsigned int riscv_xlen_nonconst(struct target *target)
Definition: riscv.c:3056
#define DTMCONTROL
Definition: riscv.c:49
int riscv_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: riscv.c:988
static int oldriscv_poll(struct target *target)
Definition: riscv.c:1144
static int riscv_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
Definition: riscv.c:1023
void select_dmi_via_bscan(struct target *target)
Definition: riscv.c:282
static int halt_go(struct target *target)
Definition: riscv.c:1202
static int riscv_assert_reset(struct target *target)
Definition: riscv.c:1271
static int read_by_given_size(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer, uint32_t access_size)
Read one memory item of given "size".
Definition: riscv.c:778
static uint32_t bscan_tunnel_nested_tap_select_dmi_num_fields
Definition: riscv.c:188
static int halt_prep(struct target *target)
Definition: riscv.c:1164
static uint8_t ir_dbus[4]
Definition: riscv.c:117
static unsigned int riscv_data_bits(struct target *target)
Definition: riscv.c:3061
static int register_get(struct reg *reg)
Definition: riscv.c:3695
COMMAND_HANDLER(riscv_set_command_timeout_sec)
Definition: riscv.c:2333
int riscv_dmi_write_u64_bits(struct target *target)
Definition: riscv.c:3432
bool riscv_enable_virtual
Definition: riscv.c:213
static void riscv_deinit_target(struct target *target)
Definition: riscv.c:489
static const char * riscv_get_gdb_arch(const struct target *target)
Definition: riscv.c:1745
bool riscv_supports_extension(struct target *target, char letter)
Definition: riscv.c:3192
static int riscv_examine(struct target *target)
Definition: riscv.c:1117
void riscv_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d)
Definition: riscv.c:3414
static int riscv_resume_go_all_harts(struct target *target)
Definition: riscv.c:3147
void riscv_fill_dmi_read_u64(struct target *target, char *buf, int a)
Definition: riscv.c:3420
static int sample_memory(struct target *target)
Definition: riscv.c:2134
bool riscv_ebreaku
Definition: riscv.c:211
int riscv_get_register(struct target *target, riscv_reg_t *value, enum gdb_regno regid)
Get register, from the cache if it's in there.
Definition: riscv.c:3332
static int riscv_arch_state(struct target *target)
Definition: riscv.c:1821
static int riscv_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Definition: riscv.c:1828
static struct scan_field * bscan_tunnel_nested_tap_select_dmi
Definition: riscv.c:187
int riscv_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: riscv.c:1001
static void riscv_invalidate_register_cache(struct target *target)
Definition: riscv.c:3227
static int riscv_get_gdb_reg_list_noread(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Definition: riscv.c:1805
static int oldriscv_step(struct target *target, bool current, uint32_t address, bool handle_breakpoints)
Definition: riscv.c:1099
static struct target_type * get_target_type(struct target *target)
Definition: riscv.c:411
static struct scan_field * bscan_tunnel_data_register_select_dmi
Definition: riscv.c:190
static void trigger_from_breakpoint(struct trigger *trigger, const struct breakpoint *breakpoint)
Definition: riscv.c:521
dbus_op_t
Definition: riscv.c:58
@ DBUS_OP_NOP
Definition: riscv.c:59
@ DBUS_OP_WRITE
Definition: riscv.c:61
@ DBUS_OP_READ
Definition: riscv.c:60
static const uint8_t bscan_zero[4]
Definition: riscv.c:131
static int riscv_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: riscv.c:1726
riscv_insn_t riscv_read_debug_buffer(struct target *target, int index)
Definition: riscv.c:3402
static int write_by_given_size(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer, uint32_t access_size)
Write one memory item of given "size".
Definition: riscv.c:751
int riscv_enumerate_triggers(struct target *target)
Count triggers, and initialize trigger_count for each hart.
Definition: riscv.c:3445
int riscv_command_timeout_sec
Definition: riscv.c:203
@ RO_REVERSED
Definition: riscv.c:217
@ RO_NORMAL
Definition: riscv.c:216
int riscv_openocd_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv.c:2301
int riscv_count_harts(struct target *target)
Definition: riscv.c:3243
int bscan_tunnel_ir_width
Definition: riscv.c:129
static int resume_go(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Resume all the harts that have been prepped, as close to instantaneous as possible.
Definition: riscv.c:1435
static uint8_t ir_idcode[4]
Definition: riscv.c:122
static int riscv_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
Definition: riscv.c:1675
static int resume_finish(struct target *target)
Definition: riscv.c:1451
static int parse_ranges(struct list_head *ranges, const char *tcl_arg, const char *reg_type, unsigned int max_val)
Definition: riscv.c:2428
#define set_field(reg, mask, val)
Definition: riscv.c:27
static int old_or_new_riscv_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv.c:1106
static int disable_triggers(struct target *target, riscv_reg_t *state)
Definition: riscv.c:1308
static const virt2phys_info_t sv39
Definition: riscv.c:233
static int riscv_deassert_reset(struct target *target)
Definition: riscv.c:1279
#define DBUS
Definition: riscv.c:55
static const uint8_t bscan_one[4]
Definition: riscv.c:132
#define RISCV_SAMPLE_BUF_TIMESTAMP_BEFORE
Definition: riscv.h:67
#define RISCV_PGSHIFT
Definition: riscv.h:29
#define RISCV_INFO(R)
Definition: riscv.h:271
static struct riscv_info * riscv_info(const struct target *target) __attribute__((unused))
Definition: riscv.h:266
void riscv_semihosting_init(struct target *target)
Initialize RISC-V semihosting.
@ RISCV_MEM_ACCESS_UNSPECIFIED
Definition: riscv.h:46
@ RISCV_MEM_ACCESS_SYSBUS
Definition: riscv.h:48
@ RISCV_MEM_ACCESS_PROGBUF
Definition: riscv.h:47
@ RISCV_MEM_ACCESS_ABSTRACT
Definition: riscv.h:49
#define RISCV_NUM_MEM_ACCESS_METHODS
Definition: riscv.h:33
#define RISCV_MAX_HWBPS
Definition: riscv.h:22
enum semihosting_result riscv_semihosting(struct target *target, int *retval)
Check for and process a semihosting request using the ARM protocol).
#define RISCV_SATP_PPN(xlen)
Definition: riscv.h:28
#define RISCV_MAX_TRIGGERS
Definition: riscv.h:21
#define RISCV_SAMPLE_BUF_TIMESTAMP_AFTER
Definition: riscv.h:68
uint64_t riscv_reg_t
Definition: riscv.h:41
#define RISCV_COMMON_MAGIC
Definition: riscv.h:16
#define DEFAULT_COMMAND_TIMEOUT_SEC
Definition: riscv.h:24
bscan_tunnel_type_t
Definition: riscv.h:284
@ BSCAN_TUNNEL_NESTED_TAP
Definition: riscv.h:284
@ BSCAN_TUNNEL_DATA_REGISTER
Definition: riscv.h:284
#define DEFAULT_RESET_TIMEOUT_SEC
Definition: riscv.h:25
#define RISCV_SATP_MODE(xlen)
Definition: riscv.h:27
uint32_t riscv_insn_t
Definition: riscv.h:42
riscv_halt_reason
Definition: riscv.h:52
@ RISCV_HALT_INTERRUPT
Definition: riscv.h:53
@ RISCV_HALT_BREAKPOINT
Definition: riscv.h:54
@ RISCV_HALT_SINGLESTEP
Definition: riscv.h:55
@ RISCV_HALT_UNKNOWN
Definition: riscv.h:57
@ RISCV_HALT_ERROR
Definition: riscv.h:59
@ RISCV_HALT_GROUP
Definition: riscv.h:58
@ RISCV_HALT_TRIGGER
Definition: riscv.h:56
struct target * target
Definition: rtt/rtt.c:26
const struct command_registration semihosting_common_handlers[]
@ SEMIHOSTING_ERROR
@ SEMIHOSTING_HANDLED
@ SEMIHOSTING_WAITING
@ SEMIHOSTING_NONE
const struct command_registration smp_command_handlers[]
Definition: smp.c:153
#define foreach_smp_target(pos, head)
Definition: smp.h:15
#define foreach_smp_target_direction(forward, pos, head)
Definition: smp.h:18
#define BIT(nr)
Definition: stm32l4x.h:18
unsigned int length
Definition: breakpoints.h:29
uint8_t * orig_instr
Definition: breakpoints.h:33
enum breakpoint_type type
Definition: breakpoints.h:30
uint32_t unique_id
Definition: breakpoints.h:35
bool is_set
Definition: breakpoints.h:31
target_addr_t address
Definition: breakpoints.h:27
const char * name
Definition: command.h:235
const struct command_registration * chain
If non-NULL, the commands in chain will be registered in the same context and scope of this registrat...
Definition: command.h:249
unsigned int number
Definition: riscv.c:3779
const char * name
Definition: riscv.c:3780
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
Definition: list.h:40
char * name
Definition: riscv.h:87
uint16_t high
Definition: riscv.h:86
uint16_t low
Definition: riscv.h:86
int(* get)(struct reg *reg)
Definition: register.h:152
int(* set)(struct reg *reg, uint8_t *buf)
Definition: register.h:153
const char * name
Definition: register.h:145
unsigned int num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
struct reg_data_type_union_field * fields
Definition: register.h:57
enum reg_type type
Definition: register.h:100
const char * name
Definition: register.h:42
uint32_t size
Definition: algorithm.h:29
uint8_t * value
Definition: algorithm.h:30
const char * reg_name
Definition: algorithm.h:28
Definition: register.h:111
bool caller_save
Definition: register.h:119
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
const char * group
Definition: register.h:138
uint8_t * value
Definition: register.h:122
struct reg_feature * feature
Definition: register.h:117
struct reg_data_type * reg_data_type
Definition: register.h:135
uint32_t number
Definition: register.h:115
void * arch_info
Definition: register.h:140
bool dirty
Definition: register.h:124
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
struct scan_field tunneled_dr[4]
Definition: riscv.h:236
void * version_specific
Definition: riscv.h:96
struct list_head expose_custom
Definition: riscv.h:225
int mem_access_methods[RISCV_NUM_MEM_ACCESS_METHODS]
Definition: riscv.h:211
int xlen
Definition: riscv.h:109
bool prepped
Definition: riscv.h:136
int current_hartid
Definition: riscv.h:102
unsigned int dtm_version
Definition: riscv.h:93
bool mem_access_abstract_warn
Definition: riscv.h:217
bool mem_access_sysbus_warn
Definition: riscv.h:216
struct list_head expose_csr
Definition: riscv.h:221
bool mem_access_progbuf_warn
Definition: riscv.h:215
unsigned int common_magic
Definition: riscv.h:91
enum riscv_halt_reason(* halt_reason)(struct target *target)
Definition: riscv.h:161
int trigger_unique_id[RISCV_MAX_HWBPS]
Definition: riscv.h:121
struct target * target
Definition: riscv.h:63
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
struct target * target
Definition: target.h:214
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
int(* write_memory)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Target memory write callback.
Definition: target_type.h:124
const char * name
Name of this type of target.
Definition: target_type.h:31
int(* deassert_reset)(struct target *target)
The implementation is responsible for polling the target such that target->state reflects the state c...
Definition: target_type.h:76
int(* init_target)(struct command_context *cmd_ctx, struct target *target)
Definition: target_type.h:229
int(* resume)(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: target_type.h:45
void(* deinit_target)(struct target *target)
Free all the resources allocated by the target.
Definition: target_type.h:247
int(* halt)(struct target *target)
Definition: target_type.h:43
int(* assert_reset)(struct target *target)
Definition: target_type.h:64
int(* arch_state)(struct target *target)
Definition: target_type.h:37
int(* step)(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: target_type.h:47
int(* poll)(struct target *target)
Definition: target_type.h:34
int(* examine)(struct target *target)
This method is used to perform target setup that requires JTAG access.
Definition: target_type.h:222
int(* virt2phys)(struct target *target, target_addr_t address, target_addr_t *physical)
Definition: target_type.h:252
Definition: target.h:116
int32_t coreid
Definition: target.h:120
struct jtag_tap * tap
Definition: target.h:119
enum target_debug_reason debug_reason
Definition: target.h:154
enum target_state state
Definition: target.h:157
struct reg_cache * reg_cache
Definition: target.h:158
struct list_head * smp_targets
Definition: target.h:188
unsigned int smp
Definition: target.h:187
struct target_type * type
Definition: target.h:117
struct watchpoint * watchpoints
Definition: target.h:160
void * arch_info
Definition: target.h:164
Definition: psoc6.c:83
uint64_t value
Definition: riscv-011.c:168
bool read
Definition: riscv-011.c:169
uint64_t address
Definition: riscv-011.c:165
uint32_t length
Definition: riscv-011.c:166
uint64_t mask
Definition: riscv-011.c:167
int unique_id
Definition: riscv-011.c:170
bool execute
Definition: riscv-011.c:169
bool write
Definition: riscv-011.c:169
const char * name
Definition: riscv.h:240
uint64_t mask
Definition: breakpoints.h:44
enum watchpoint_rw rw
Definition: breakpoints.h:46
bool is_set
Definition: breakpoints.h:47
struct watchpoint * next
Definition: breakpoints.h:49
unsigned int length
Definition: breakpoints.h:43
uint64_t value
Definition: breakpoints.h:45
int unique_id
Definition: breakpoints.h:50
target_addr_t address
Definition: breakpoints.h:42
uint32_t size
Definition: target.h:87
target_addr_t address
Definition: target.h:86
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1765
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2343
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2408
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:774
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1266
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2061
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2119
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1238
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:458
@ DBG_REASON_UNDEFINED
Definition: target.h:77
@ DBG_REASON_NOTHALTED
Definition: target.h:74
@ DBG_REASON_DBGRQ
Definition: target.h:69
@ DBG_REASON_SINGLESTEP
Definition: target.h:73
@ DBG_REASON_WATCHPOINT
Definition: target.h:71
@ DBG_REASON_BREAKPOINT
Definition: target.h:70
target_register_class
Definition: target.h:110
@ REG_CLASS_GENERAL
Definition: target.h:112
@ REG_CLASS_ALL
Definition: target.h:111
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:790
static bool target_was_examined(const struct target *target)
Definition: target.h:436
@ TARGET_EVENT_HALTED
Definition: target.h:252
@ TARGET_EVENT_RESUMED
Definition: target.h:253
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:233
@ TARGET_HALTED
Definition: target.h:56
@ TARGET_RUNNING
Definition: target.h:55
#define TARGET_DEFAULT_POLLING_INTERVAL
Definition: target.h:806
#define ERROR_TARGET_TIMEOUT
Definition: target.h:789
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:794
int64_t timeval_ms(void)
#define TARGET_ADDR_FMT
Definition: types.h:342
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
uint64_t target_addr_t
Definition: types.h:335
#define TARGET_PRIxADDR
Definition: types.h:340
static struct ublast_lowlevel low
static struct ublast_lowlevel_priv info
#define NULL
Definition: usb.h:16
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22