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 "helper/base64.h"
21 #include "helper/time_support.h"
22 #include "riscv.h"
23 #include "riscv_reg.h"
24 #include "program.h"
25 #include "gdb_regs.h"
26 #include "rtos/rtos.h"
27 #include "debug_defines.h"
28 #include <helper/bits.h>
29 #include "field_helpers.h"
30 
31 /*** JTAG registers. ***/
32 
33 #define DTMCONTROL 0x10
34 #define DTMCONTROL_VERSION (0xf)
35 
36 #define DBUS 0x11
37 
38 #define RISCV_TRIGGER_HIT_NOT_FOUND ((int64_t)-1)
39 
40 #define RISCV_HALT_GROUP_REPOLL_LIMIT 5
41 
42 static uint8_t ir_dtmcontrol[4] = {DTMCONTROL};
44  .in_value = NULL,
45  .out_value = ir_dtmcontrol
46 };
47 static uint8_t ir_dbus[4] = {DBUS};
48 struct scan_field select_dbus = {
49  .in_value = NULL,
50  .out_value = ir_dbus
51 };
52 static uint8_t ir_idcode[4] = {0x1};
53 struct scan_field select_idcode = {
54  .in_value = NULL,
55  .out_value = ir_idcode
56 };
57 
59 #define BSCAN_TUNNEL_IR_WIDTH_NBITS 7
60 uint8_t bscan_tunnel_ir_width; /* if zero, then tunneling is not present/active */
61 static int bscan_tunnel_ir_id; /* IR ID of the JTAG TAP to access the tunnel. Valid when not 0 */
62 
63 static const uint8_t bscan_zero[4] = {0};
64 static const uint8_t bscan_one[4] = {1};
65 
66 static uint8_t ir_user4[4];
67 static struct scan_field select_user4 = {
68  .in_value = NULL,
69  .out_value = ir_user4
70 };
71 
72 
74  {
75  .num_bits = 3,
76  .out_value = bscan_zero,
77  .in_value = NULL,
78  },
79  {
80  .num_bits = 5, /* initialized in riscv_init_target to ir width of DM */
81  .out_value = ir_dbus,
82  .in_value = NULL,
83  },
84  {
85  .num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS,
86  .out_value = &bscan_tunnel_ir_width,
87  .in_value = NULL,
88  },
89  {
90  .num_bits = 1,
91  .out_value = bscan_zero,
92  .in_value = NULL,
93  }
94 };
95 
97  {
98  .num_bits = 1,
99  .out_value = bscan_zero,
100  .in_value = NULL,
101  },
102  {
103  .num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS,
104  .out_value = &bscan_tunnel_ir_width,
105  .in_value = NULL,
106  },
107  {
108  .num_bits = 0, /* initialized in riscv_init_target to ir width of DM */
109  .out_value = ir_dbus,
110  .in_value = NULL,
111  },
112  {
113  .num_bits = 3,
114  .out_value = bscan_zero,
115  .in_value = NULL,
116  }
117 };
120 
123 
124 struct trigger {
125  uint64_t address;
126  uint32_t length;
127  uint64_t mask;
128  uint64_t value;
131 };
132 
133 struct tdata2_cache {
134  struct list_head elem_tdata2;
136 };
137 
138 struct tdata1_cache {
141  struct list_head elem_tdata1;
142 };
143 
145 {
146  assert(target);
147  RISCV_INFO(r);
148  return r->virt2phys_mode == RISCV_VIRT2PHYS_MODE_HW;
149 }
150 
152 {
153  assert(target);
154  RISCV_INFO(r);
155  return r->virt2phys_mode == RISCV_VIRT2PHYS_MODE_SW;
156 }
157 
159 {
163 
164  static const char *const names[] = {
165  [RISCV_VIRT2PHYS_MODE_HW] = "hw",
166  [RISCV_VIRT2PHYS_MODE_SW] = "sw",
167  [RISCV_VIRT2PHYS_MODE_OFF] = "off",
168  };
169 
170  return names[mode];
171 }
172 
173 /* Wall-clock timeout for a command/access. Settable via RISC-V Target commands.*/
175 
176 /* DEPRECATED Wall-clock timeout after reset. Settable via RISC-V Target commands.*/
178 
180 {
182 }
183 
184 static enum {
188 
189 static const virt2phys_info_t sv32 = {
190  .name = "Sv32",
191  .va_bits = 32,
192  .level = 2,
193  .pte_shift = 2,
194  .vpn_shift = {12, 22},
195  .vpn_mask = {0x3ff, 0x3ff},
196  .pte_ppn_shift = {10, 20},
197  .pte_ppn_mask = {0x3ff, 0xfff},
198  .pa_ppn_shift = {12, 22},
199  .pa_ppn_mask = {0x3ff, 0xfff},
200 };
201 
202 static const virt2phys_info_t sv32x4 = {
203  .name = "Sv32x4",
204  .va_bits = 34,
205  .level = 2,
206  .pte_shift = 2,
207  .vpn_shift = {12, 22},
208  .vpn_mask = {0x3ff, 0xfff},
209  .pte_ppn_shift = {10, 20},
210  .pte_ppn_mask = {0x3ff, 0xfff},
211  .pa_ppn_shift = {12, 22},
212  .pa_ppn_mask = {0x3ff, 0xfff},
213 };
214 
215 static const virt2phys_info_t sv39 = {
216  .name = "Sv39",
217  .va_bits = 39,
218  .level = 3,
219  .pte_shift = 3,
220  .vpn_shift = {12, 21, 30},
221  .vpn_mask = {0x1ff, 0x1ff, 0x1ff},
222  .pte_ppn_shift = {10, 19, 28},
223  .pte_ppn_mask = {0x1ff, 0x1ff, 0x3ffffff},
224  .pa_ppn_shift = {12, 21, 30},
225  .pa_ppn_mask = {0x1ff, 0x1ff, 0x3ffffff},
226 };
227 
228 static const virt2phys_info_t sv39x4 = {
229  .name = "Sv39x4",
230  .va_bits = 41,
231  .level = 3,
232  .pte_shift = 3,
233  .vpn_shift = {12, 21, 30},
234  .vpn_mask = {0x1ff, 0x1ff, 0x7ff},
235  .pte_ppn_shift = {10, 19, 28},
236  .pte_ppn_mask = {0x1ff, 0x1ff, 0x3ffffff},
237  .pa_ppn_shift = {12, 21, 30},
238  .pa_ppn_mask = {0x1ff, 0x1ff, 0x3ffffff},
239 };
240 
241 static const virt2phys_info_t sv48 = {
242  .name = "Sv48",
243  .va_bits = 48,
244  .level = 4,
245  .pte_shift = 3,
246  .vpn_shift = {12, 21, 30, 39},
247  .vpn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff},
248  .pte_ppn_shift = {10, 19, 28, 37},
249  .pte_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
250  .pa_ppn_shift = {12, 21, 30, 39},
251  .pa_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
252 };
253 
254 static const virt2phys_info_t sv48x4 = {
255  .name = "Sv48x4",
256  .va_bits = 50,
257  .level = 4,
258  .pte_shift = 3,
259  .vpn_shift = {12, 21, 30, 39},
260  .vpn_mask = {0x1ff, 0x1ff, 0x1ff, 0x7ff},
261  .pte_ppn_shift = {10, 19, 28, 37},
262  .pte_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
263  .pa_ppn_shift = {12, 21, 30, 39},
264  .pa_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
265 };
266 
267 static const virt2phys_info_t sv57 = {
268  .name = "Sv57",
269  .va_bits = 57,
270  .level = 5,
271  .pte_shift = 3,
272  .vpn_shift = {12, 21, 30, 39, 48},
273  .vpn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff, 0x1ff},
274  .pte_ppn_shift = {10, 19, 28, 37, 46},
275  .pte_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff, 0xff},
276  .pa_ppn_shift = {12, 21, 30, 39, 48},
277  .pa_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff, 0x1ff},
278 };
279 
280 static const virt2phys_info_t sv57x4 = {
281  .name = "Sv57x4",
282  .va_bits = 59,
283  .level = 5,
284  .pte_shift = 3,
285  .vpn_shift = {12, 21, 30, 39, 48},
286  .vpn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff, 0x7ff},
287  .pte_ppn_shift = {10, 19, 28, 37, 46},
288  .pte_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff, 0xff},
289  .pa_ppn_shift = {12, 21, 30, 39, 48},
290  .pa_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff, 0xff},
291 };
292 
293 static enum riscv_halt_reason riscv_halt_reason(struct target *target);
294 static void riscv_info_init(struct target *target, struct riscv_info *r);
295 static int riscv_step_rtos_hart(struct target *target);
296 
298 static int riscv_interrupts_disable(struct target *target, riscv_reg_t *old_mstatus);
299 static int riscv_interrupts_restore(struct target *target, riscv_reg_t old_mstatus);
300 
301 static void riscv_sample_buf_maybe_add_timestamp(struct target *target, bool before)
302 {
303  RISCV_INFO(r);
304  uint32_t now = timeval_ms() & 0xffffffff;
305  if (r->sample_buf.used + 5 < r->sample_buf.size) {
306  if (before)
307  r->sample_buf.buf[r->sample_buf.used++] = RISCV_SAMPLE_BUF_TIMESTAMP_BEFORE;
308  else
309  r->sample_buf.buf[r->sample_buf.used++] = RISCV_SAMPLE_BUF_TIMESTAMP_AFTER;
310  r->sample_buf.buf[r->sample_buf.used++] = now & 0xff;
311  r->sample_buf.buf[r->sample_buf.used++] = (now >> 8) & 0xff;
312  r->sample_buf.buf[r->sample_buf.used++] = (now >> 16) & 0xff;
313  r->sample_buf.buf[r->sample_buf.used++] = (now >> 24) & 0xff;
314  }
315 }
316 
317 static int riscv_resume_go_all_harts(struct target *target);
318 
320 {
325  else /* BSCAN_TUNNEL_NESTED_TAP */
328 }
329 
330 static int dtmcs_scan_via_bscan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr)
331 {
332  /* On BSCAN TAP: Select IR=USER4, issue tunneled IR scan via BSCAN TAP's DR */
333  uint8_t tunneled_dr_width[4] = {32};
334  uint8_t out_value[5] = {0};
335  uint8_t in_value[5] = {0};
336 
337  buf_set_u32(out_value, 0, 32, out);
338  struct scan_field tunneled_ir[4] = {};
339  struct scan_field tunneled_dr[4] = {};
340 
342  tunneled_ir[0].num_bits = 3;
343  tunneled_ir[0].out_value = bscan_zero;
344  tunneled_ir[0].in_value = NULL;
345  tunneled_ir[1].num_bits = bscan_tunnel_ir_width;
346  tunneled_ir[1].out_value = ir_dtmcontrol;
347  tunneled_ir[1].in_value = NULL;
348  tunneled_ir[2].num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS;
349  tunneled_ir[2].out_value = &bscan_tunnel_ir_width;
350  tunneled_ir[2].in_value = NULL;
351  tunneled_ir[3].num_bits = 1;
352  tunneled_ir[3].out_value = bscan_zero;
353  tunneled_ir[3].in_value = NULL;
354 
355  tunneled_dr[0].num_bits = 3;
356  tunneled_dr[0].out_value = bscan_zero;
357  tunneled_dr[0].in_value = NULL;
358  tunneled_dr[1].num_bits = 32 + 1;
359  tunneled_dr[1].out_value = out_value;
360  tunneled_dr[1].in_value = in_value;
361  tunneled_dr[2].num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS;
362  tunneled_dr[2].out_value = tunneled_dr_width;
363  tunneled_dr[2].in_value = NULL;
364  tunneled_dr[3].num_bits = 1;
365  tunneled_dr[3].out_value = bscan_one;
366  tunneled_dr[3].in_value = NULL;
367  } else {
368  /* BSCAN_TUNNEL_NESTED_TAP */
369  tunneled_ir[3].num_bits = 3;
370  tunneled_ir[3].out_value = bscan_zero;
371  tunneled_ir[3].in_value = NULL;
372  tunneled_ir[2].num_bits = bscan_tunnel_ir_width;
373  tunneled_ir[2].out_value = ir_dtmcontrol;
374  tunneled_ir[1].in_value = NULL;
375  tunneled_ir[1].num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS;
376  tunneled_ir[1].out_value = &bscan_tunnel_ir_width;
377  tunneled_ir[2].in_value = NULL;
378  tunneled_ir[0].num_bits = 1;
379  tunneled_ir[0].out_value = bscan_zero;
380  tunneled_ir[0].in_value = NULL;
381 
382  tunneled_dr[3].num_bits = 3;
383  tunneled_dr[3].out_value = bscan_zero;
384  tunneled_dr[3].in_value = NULL;
385  tunneled_dr[2].num_bits = 32 + 1;
386  tunneled_dr[2].out_value = out_value;
387  tunneled_dr[2].in_value = in_value;
388  tunneled_dr[1].num_bits = 7;
389  tunneled_dr[1].out_value = tunneled_dr_width;
390  tunneled_dr[1].in_value = NULL;
391  tunneled_dr[0].num_bits = 1;
392  tunneled_dr[0].out_value = bscan_one;
393  tunneled_dr[0].in_value = NULL;
394  }
396  jtag_add_dr_scan(tap, ARRAY_SIZE(tunneled_ir), tunneled_ir, TAP_IDLE);
397  jtag_add_dr_scan(tap, ARRAY_SIZE(tunneled_dr), tunneled_dr, TAP_IDLE);
399 
400  int retval = jtag_execute_queue();
401  if (retval != ERROR_OK) {
402  LOG_ERROR("failed jtag scan: %d", retval);
403  return retval;
404  }
405  /* Note the starting offset is bit 1, not bit 0. In BSCAN tunnel, there is a one-bit TCK skew between
406  output and input */
407  uint32_t in = buf_get_u32(in_value, 1, 32);
408  LOG_DEBUG("DTMCS: 0x%x -> 0x%x", out, in);
409 
410  if (in_ptr)
411  *in_ptr = in;
412  return ERROR_OK;
413 }
414 
415 /* TODO: rename "dtmcontrol"-> "dtmcs" */
416 int dtmcs_scan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr)
417 {
418  uint8_t value[4];
419 
420  if (bscan_tunnel_ir_width != 0)
421  return dtmcs_scan_via_bscan(tap, out, in_ptr);
422 
423  buf_set_u32(value, 0, 32, out);
424 
426 
427  struct scan_field field = {
428  .num_bits = 32,
429  .out_value = value,
430  .in_value = in_ptr ? value : NULL
431  };
432  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
433 
434  /* Always return to dbus. */
436 
437  int retval = jtag_execute_queue();
438  if (retval != ERROR_OK) {
439  LOG_ERROR("'dtmcs' scan failed on TAP %s, error code = %d",
440  jtag_tap_name(tap), retval);
441  return retval;
442  }
443 
444  if (in_ptr) {
445  assert(field.in_value);
446  uint32_t in = buf_get_u32(field.in_value, 0, 32);
447  LOG_DEBUG("TAP %s: DTMCS: 0x%" PRIx32 " -> 0x%" PRIx32,
448  jtag_tap_name(tap), out, in);
449  *in_ptr = in;
450  } else {
451  LOG_DEBUG("TAP %s: DTMCS: 0x%" PRIx32 " -> ?", jtag_tap_name(tap), out);
452  }
453  return ERROR_OK;
454 }
455 
456 static struct target_type *get_target_type(struct target *target)
457 {
458  if (!target->arch_info) {
459  LOG_TARGET_ERROR(target, "Target has not been initialized.");
460  return NULL;
461  }
462 
463  RISCV_INFO(info);
464  switch (info->dtm_version) {
466  return &riscv011_target;
468  return &riscv013_target;
469  default:
470  /* TODO: once we have proper support for non-examined targets
471  * we should have an assert here */
472  LOG_TARGET_ERROR(target, "Unsupported DTM version: %d",
473  info->dtm_version);
474  return NULL;
475  }
476 }
477 
479 {
480  struct riscv_private_config * const config = malloc(sizeof(*config));
481  if (!config) {
482  LOG_ERROR("Out of memory!");
483  return NULL;
484  }
485 
486  for (unsigned int i = 0; i < ARRAY_SIZE(config->dcsr_ebreak_fields); ++i)
487  config->dcsr_ebreak_fields[i] = true;
488 
489  return config;
490 }
491 
492 static int riscv_create_target(struct target *target)
493 {
494  LOG_TARGET_DEBUG(target, "riscv_create_target()");
496  if (!config) {
498  if (!config)
499  return ERROR_FAIL;
501  }
502  target->arch_info = calloc(1, sizeof(struct riscv_info));
503  if (!target->arch_info) {
504  LOG_TARGET_ERROR(target, "Failed to allocate RISC-V target structure.");
505  return ERROR_FAIL;
506  }
508  return ERROR_OK;
509 }
510 
511 static struct jim_nvp nvp_ebreak_config_opts[] = {
512  { .name = "m", .value = RISCV_MODE_M },
513  { .name = "s", .value = RISCV_MODE_S },
514  { .name = "u", .value = RISCV_MODE_U },
515  { .name = "vs", .value = RISCV_MODE_VS },
516  { .name = "vu", .value = RISCV_MODE_VU },
517  { .name = NULL, .value = N_RISCV_MODE }
518 };
519 
520 #define RISCV_EBREAK_MODE_INVALID -1
521 
522 static struct jim_nvp nvp_ebreak_mode_opts[] = {
523  { .name = "exception", .value = false },
524  { .name = "halt", .value = true },
525  { .name = NULL, .value = RISCV_EBREAK_MODE_INVALID }
526 };
527 
529 {
530  if (goi->argc == 0) {
531  Jim_WrongNumArgs(goi->interp, 1, goi->argv - 1,
532  "[?execution_mode?] ?ebreak_action?");
533  return JIM_ERR;
534  }
535  struct jim_nvp *common_mode_nvp;
537  &common_mode_nvp) == JIM_OK) {
538  /* Here a common "ebreak" action is processed, e.g:
539  * "riscv.cpu configure -ebreak halt"
540  */
541  int res = jim_getopt_obj(goi, NULL);
542  if (res != JIM_OK)
543  return res;
544  for (int ebreak_ctl_i = 0; ebreak_ctl_i < N_RISCV_MODE; ++ebreak_ctl_i)
545  config->dcsr_ebreak_fields[ebreak_ctl_i] = common_mode_nvp->value;
546  return JIM_OK;
547  }
548 
549  /* Here a "ebreak" action for a specific execution mode is processed, e.g:
550  * "riscv.cpu configure -ebreak m halt"
551  */
552  if (goi->argc < 2) {
553  Jim_WrongNumArgs(goi->interp, 2, goi->argv - 2,
554  "?ebreak_action?");
555  return JIM_ERR;
556  }
557  struct jim_nvp *ctrl_nvp;
558  if (jim_getopt_nvp(goi, nvp_ebreak_config_opts, &ctrl_nvp) != JIM_OK) {
559  jim_getopt_nvp_unknown(goi, nvp_ebreak_config_opts, /*hadprefix*/ true);
560  return JIM_ERR;
561  }
562  struct jim_nvp *mode_nvp;
563  if (jim_getopt_nvp(goi, nvp_ebreak_mode_opts, &mode_nvp) != JIM_OK) {
564  jim_getopt_nvp_unknown(goi, nvp_ebreak_mode_opts, /*hadprefix*/ true);
565  return JIM_ERR;
566  }
567  config->dcsr_ebreak_fields[ctrl_nvp->value] = mode_nvp->value;
568  return JIM_OK;
569 }
570 
578  char *buffer)
579 {
580  int len = 0;
581  const char *separator = "";
582  for (int ebreak_ctl_i = 0; ebreak_ctl_i < N_RISCV_MODE;
583  ++ebreak_ctl_i) {
584  const char * const format = "%s%s %s";
585  const char * const priv_mode =
588  config->dcsr_ebreak_fields[ebreak_ctl_i])->name;
589  if (!buffer)
590  len += snprintf(NULL, 0, format, separator, priv_mode, mode);
591  else
592  len += sprintf(buffer + len, format, separator, priv_mode, mode);
593 
594  separator = "\n";
595  }
596  return len;
597 }
598 
600  Jim_Interp *interp)
601 {
602  const int len = ebreak_config_to_tcl_dict(config, NULL);
603  char *str = malloc(len + 1);
604  if (!str) {
605  LOG_ERROR("Unable to allocate a string of %d bytes.", len + 1);
606  return JIM_ERR;
607  }
609  Jim_SetResultString(interp, str, len);
610  free(str);
611  return JIM_OK;
612 }
613 
616  RISCV_CFG_INVALID = -1
617 };
618 
619 static struct jim_nvp nvp_config_opts[] = {
620  { .name = "-ebreak", .value = RISCV_CFG_EBREAK },
621  { .name = NULL, .value = RISCV_CFG_INVALID }
622 };
623 
624 static int riscv_jim_configure(struct target *target,
625  struct jim_getopt_info *goi)
626 {
628  if (!config) {
630  if (!config)
631  return JIM_ERR;
633  }
634  if (!goi->argc)
635  return JIM_OK;
636 
637  struct jim_nvp *n;
639  goi->argv[0], &n);
640  if (e != JIM_OK)
641  return JIM_CONTINUE;
642 
643  e = jim_getopt_obj(goi, NULL);
644  if (e != JIM_OK)
645  return e;
646 
647  if (!goi->is_configure && goi->argc > 0) {
648  /* Expecting no arguments */
649  Jim_WrongNumArgs(goi->interp, 2, goi->argv - 2, "");
650  return JIM_ERR;
651  }
652  switch (n->value) {
653  case RISCV_CFG_EBREAK:
654  return goi->is_configure
657  default:
658  assert(false && "'jim_getopt_nvp' should have returned an error.");
659  }
660  return JIM_ERR;
661 }
662 
663 static int riscv_init_target(struct command_context *cmd_ctx,
664  struct target *target)
665 {
666  LOG_TARGET_DEBUG(target, "riscv_init_target()");
667  RISCV_INFO(info);
668  info->cmd_ctx = cmd_ctx;
669  info->reset_delays_wait = -1;
670 
674 
675  if (bscan_tunnel_ir_width != 0) {
676  uint32_t ir_user4_raw = bscan_tunnel_ir_id;
677  /* Provide a default value which target some Xilinx FPGA USER4 IR */
678  if (ir_user4_raw == 0) {
679  assert(target->tap->ir_length >= 6);
680  ir_user4_raw = 0x23 << (target->tap->ir_length - 6);
681  }
682  h_u32_to_le(ir_user4, ir_user4_raw);
686  else /* BSCAN_TUNNEL_NESTED_TAP */
688  }
689 
691 
693 
694  return ERROR_OK;
695 }
696 
698 {
699  RISCV_INFO(r);
700 
701  for (unsigned int i = 0; i < r->trigger_count; ++i) {
702  struct tdata1_cache *elem_1, *tmp_1;
703  list_for_each_entry_safe(elem_1, tmp_1, &r->wp_triggers_negative_cache[i], elem_tdata1) {
704  struct tdata2_cache *elem_2, *tmp_2;
705  list_for_each_entry_safe(elem_2, tmp_2, &elem_1->tdata2_cache_head, elem_tdata2) {
706  list_del(&elem_2->elem_tdata2);
707  free(elem_2);
708  }
709  list_del(&elem_1->elem_tdata1);
710  free(elem_1);
711  }
712  }
713  free(r->wp_triggers_negative_cache);
714 }
715 
716 static void riscv_deinit_target(struct target *target)
717 {
718  LOG_TARGET_DEBUG(target, "riscv_deinit_target()");
719 
720  free(target->private_config);
721 
722  struct riscv_info *info = target->arch_info;
723  struct target_type *tt = get_target_type(target);
724  if (!tt)
725  LOG_TARGET_ERROR(target, "Could not identify target type.");
726 
728  LOG_TARGET_ERROR(target, "Failed to flush registers. Ignoring this error.");
729 
730  if (tt && info && info->version_specific)
731  tt->deinit_target(target);
732 
735 
736  if (!info)
737  return;
738 
739  free(info->reserved_triggers);
740 
741  range_list_t *entry, *tmp;
742  list_for_each_entry_safe(entry, tmp, &info->hide_csr, list) {
743  free(entry->name);
744  free(entry);
745  }
746 
747  list_for_each_entry_safe(entry, tmp, &info->expose_csr, list) {
748  free(entry->name);
749  free(entry);
750  }
751 
752  list_for_each_entry_safe(entry, tmp, &info->expose_custom, list) {
753  free(entry->name);
754  free(entry);
755  }
756 
757  free(target->arch_info);
758 
759  target->arch_info = NULL;
760 }
761 
763  const struct breakpoint *breakpoint)
764 {
767  trigger->mask = ~0LL;
768  trigger->is_read = false;
769  trigger->is_write = false;
770  trigger->is_execute = true;
771  /* unique_id is unique across both breakpoints and watchpoints. */
773 }
774 
775 static bool can_use_napot_match(struct trigger *trigger)
776 {
779  bool size_power_of_2 = (size & (size - 1)) == 0;
780  bool addr_aligned = (addr & (size - 1)) == 0;
781  return size > 1 && size_power_of_2 && addr_aligned;
782 }
783 
784 /* Find the next free trigger of the given type, without talking to the target. */
785 static int find_next_free_trigger(struct target *target, int type, bool chained,
786  unsigned int *idx)
787 {
788  assert(idx);
789  RISCV_INFO(r);
790 
791  unsigned int num_found = 0;
792  unsigned int num_required = chained ? 2 : 1;
793 
794  for (unsigned int i = *idx; i < r->trigger_count; i++) {
795  if (r->trigger_unique_id[i] == -1) {
796  if (r->trigger_tinfo[i] & (1 << type)) {
797  num_found++;
798  if (num_required == num_found) {
799  /* Found num_required consecutive free triggers - success, done. */
800  *idx = i - (num_required - 1);
802  "%d trigger(s) of type %d found on index %u, "
803  "chained == %s",
804  num_required, type, *idx,
805  chained ? "true" : "false");
806  return ERROR_OK;
807  }
808  /* Found a trigger but need more consecutive ones */
809  continue;
810  }
811  }
812  /* Trigger already occupied or incompatible type.
813  * Reset the counter of found consecutive triggers */
814  num_found = 0;
815  }
816 
817  return ERROR_FAIL;
818 }
819 
820 static int find_first_trigger_by_id(struct target *target, int unique_id)
821 {
822  RISCV_INFO(r);
823 
824  for (unsigned int i = 0; i < r->trigger_count; i++) {
825  if (r->trigger_unique_id[i] == unique_id)
826  return i;
827  }
828  return -1;
829 }
830 
831 static unsigned int count_trailing_ones(riscv_reg_t reg)
832 {
833  const unsigned int riscv_reg_bits = sizeof(riscv_reg_t) * CHAR_BIT;
834  for (unsigned int i = 0; i < riscv_reg_bits; i++) {
835  if ((1 & (reg >> i)) == 0)
836  return i;
837  }
838  return riscv_reg_bits;
839 }
840 
841 static int set_trigger(struct target *target, unsigned int idx, riscv_reg_t tdata1, riscv_reg_t tdata2)
842 {
843  RISCV_INFO(r);
844  assert(r->reserved_triggers);
845  assert(idx < r->trigger_count);
846  if (r->reserved_triggers[idx]) {
848  "Trigger %u is reserved by 'reserve_trigger' command.", idx);
850  }
851 
852  riscv_reg_t tdata1_rb, tdata2_rb;
853  // Select which trigger to use
855  return ERROR_FAIL;
856 
857  // Disable the trigger by writing 0 to it
859  return ERROR_FAIL;
860 
861  // Set trigger data for tdata2 (and tdata3 if it was supported)
863  return ERROR_FAIL;
864 
865  // Set trigger data for tdata1
867  return ERROR_FAIL;
868 
869  // Read back tdata1, tdata2, (tdata3), and check if the configuration is supported
870  if (riscv_reg_get(target, &tdata1_rb, GDB_REGNO_TDATA1) != ERROR_OK)
871  return ERROR_FAIL;
872  if (riscv_reg_get(target, &tdata2_rb, GDB_REGNO_TDATA2) != ERROR_OK)
873  return ERROR_FAIL;
874 
875  const uint32_t type = get_field(tdata1, CSR_TDATA1_TYPE(riscv_xlen(target)));
876  const bool is_mcontrol = type == CSR_TDATA1_TYPE_MCONTROL;
877 
878  /* Determine if tdata1 supports what we need.
879  * For mcontrol triggers, we don't care about
880  * the value in the read-only "maskmax" field.
881  */
882  const riscv_reg_t tdata1_ignore_mask = is_mcontrol ? CSR_MCONTROL_MASKMAX(riscv_xlen(target)) : 0;
883  const bool tdata1_config_denied = (tdata1 & ~tdata1_ignore_mask) != (tdata1_rb & ~tdata1_ignore_mask);
884 
885  /* Determine if tdata1.maxmask is sufficient
886  * (only relevant for mcontrol triggers and NAPOT match type)
887  */
888  bool unsupported_napot_range = false;
889  riscv_reg_t maskmax_value = 0;
890  if (!tdata1_config_denied) {
891  const bool is_napot_match = get_field(tdata1_rb, CSR_MCONTROL_MATCH) == CSR_MCONTROL_MATCH_NAPOT;
892  if (is_mcontrol && is_napot_match) {
893  maskmax_value = get_field(tdata1_rb, CSR_MCONTROL_MASKMAX(riscv_xlen(target)));
894  const unsigned int napot_size = count_trailing_ones(tdata2) + 1;
895  if (maskmax_value < napot_size)
896  unsupported_napot_range = true;
897  }
898  }
899 
900  const bool tdata2_config_denied = tdata2 != tdata2_rb;
901  if (tdata1_config_denied || tdata2_config_denied || unsupported_napot_range) {
902  LOG_TARGET_DEBUG(target, "Trigger %u doesn't support what we need.", idx);
903 
904  if (tdata1_config_denied)
906  "After writing 0x%" PRIx64 " to tdata1 it contains 0x%" PRIx64,
907  tdata1, tdata1_rb);
908 
909  if (tdata2_config_denied)
911  "After writing 0x%" PRIx64 " to tdata2 it contains 0x%" PRIx64,
912  tdata2, tdata2_rb);
913 
914  if (unsupported_napot_range)
916  "The requested NAPOT match range (tdata2=0x%" PRIx64 ") exceeds maskmax_value=0x%" PRIx64,
917  tdata2, maskmax_value);
918 
920  return ERROR_FAIL;
922  }
923 
924  return ERROR_OK;
925 }
926 
927 static int maybe_add_trigger_t1(struct target *target, struct trigger *trigger)
928 {
929  int ret;
930  riscv_reg_t tdata1, tdata2;
931 
932  RISCV_INFO(r);
933 
934  const uint32_t bpcontrol_x = 1<<0;
935  const uint32_t bpcontrol_w = 1<<1;
936  const uint32_t bpcontrol_r = 1<<2;
937  const uint32_t bpcontrol_u = 1<<3;
938  const uint32_t bpcontrol_s = 1<<4;
939  const uint32_t bpcontrol_h = 1<<5;
940  const uint32_t bpcontrol_m = 1<<6;
941  const uint32_t bpcontrol_bpmatch = 0xf << 7;
942  const uint32_t bpcontrol_bpaction = 0xff << 11;
943 
944  unsigned int idx = 0;
946  if (ret != ERROR_OK)
947  return ret;
948 
949  if (riscv_reg_get(target, &tdata1, GDB_REGNO_TDATA1) != ERROR_OK)
950  return ERROR_FAIL;
951  if (tdata1 & (bpcontrol_r | bpcontrol_w | bpcontrol_x)) {
952  /* Trigger is already in use, presumably by user code. */
954  }
955 
956  tdata1 = 0;
957  tdata1 = set_field(tdata1, bpcontrol_r, trigger->is_read);
958  tdata1 = set_field(tdata1, bpcontrol_w, trigger->is_write);
959  tdata1 = set_field(tdata1, bpcontrol_x, trigger->is_execute);
960  tdata1 = set_field(tdata1, bpcontrol_u, !!(r->misa & BIT('U' - 'A')));
961  tdata1 = set_field(tdata1, bpcontrol_s, !!(r->misa & BIT('S' - 'A')));
962  tdata1 = set_field(tdata1, bpcontrol_h, !!(r->misa & BIT('H' - 'A')));
963  tdata1 = set_field(tdata1, bpcontrol_m, 1);
964  tdata1 = set_field(tdata1, bpcontrol_bpaction, 0); /* cause bp exception */
965  tdata1 = set_field(tdata1, bpcontrol_bpmatch, 0); /* exact match */
966  tdata2 = trigger->address;
967  ret = set_trigger(target, idx, tdata1, tdata2);
968  if (ret != ERROR_OK)
969  return ret;
970  r->trigger_unique_id[idx] = trigger->unique_id;
971  return ERROR_OK;
972 }
973 
977 };
978 
979 static void log_trigger_request_info(struct trigger_request_info trig_info)
980 {
981  LOG_DEBUG("tdata1=%" PRIx64 ", tdata2=%" PRIx64, trig_info.tdata1, trig_info.tdata2);
982 };
983 
984 static struct tdata1_cache *tdata1_cache_alloc(struct list_head *tdata1_cache_head, riscv_reg_t tdata1)
985 {
986  struct tdata1_cache *elem = (struct tdata1_cache *)calloc(1, sizeof(struct tdata1_cache));
987  elem->tdata1 = tdata1;
989  list_add_tail(&elem->elem_tdata1, tdata1_cache_head);
990  return elem;
991 }
992 
994 {
995  struct tdata2_cache * const elem = calloc(1, sizeof(struct tdata2_cache));
996  elem->tdata2 = tdata2;
997  list_add(&elem->elem_tdata2, tdata2_cache_head);
998 }
999 
1000 struct tdata2_cache *tdata2_cache_search(struct list_head *tdata2_cache_head, riscv_reg_t find_tdata2)
1001 {
1002  struct tdata2_cache *elem_2;
1003  list_for_each_entry(elem_2, tdata2_cache_head, elem_tdata2) {
1004  if (elem_2->tdata2 == find_tdata2)
1005  return elem_2;
1006  }
1007  return NULL;
1008 }
1009 
1010 struct tdata1_cache *tdata1_cache_search(struct list_head *tdata1_cache_head, riscv_reg_t find_tdata1)
1011 {
1012  struct tdata1_cache *elem_1;
1013  list_for_each_entry(elem_1, tdata1_cache_head, elem_tdata1) {
1014  if (elem_1->tdata1 == find_tdata1)
1015  return elem_1;
1016  }
1017  return NULL;
1018 }
1019 
1021 {
1022  RISCV_INFO(r);
1023 
1024  r->wp_triggers_negative_cache = (struct list_head *)calloc(r->trigger_count,
1025  sizeof(struct list_head));
1026  for (unsigned int i = 0; i < r->trigger_count; ++i)
1027  INIT_LIST_HEAD(&r->wp_triggers_negative_cache[i]);
1028 }
1029 
1030 static void wp_triggers_cache_add(struct target *target, unsigned int idx, riscv_reg_t tdata1,
1031  riscv_reg_t tdata2, int error_code)
1032 {
1033  RISCV_INFO(r);
1034 
1035  struct tdata1_cache *tdata1_cache = tdata1_cache_search(&r->wp_triggers_negative_cache[idx], tdata1);
1036  if (!tdata1_cache) {
1037  tdata1_cache = tdata1_cache_alloc(&r->wp_triggers_negative_cache[idx], tdata1);
1038  } else {
1040  if (tdata2_cache) {
1042  return;
1043  }
1044  }
1046 }
1047 
1048 static bool wp_triggers_cache_search(struct target *target, unsigned int idx,
1049  riscv_reg_t tdata1, riscv_reg_t tdata2)
1050 {
1051  RISCV_INFO(r);
1052 
1053  struct tdata1_cache *tdata1_cache = tdata1_cache_search(&r->wp_triggers_negative_cache[idx], tdata1);
1054  if (!tdata1_cache)
1055  return false;
1057  if (!tdata2_cache)
1058  return false;
1059  assert(tdata1_cache->tdata1 == tdata1 && tdata2_cache->tdata2 == tdata2);
1060  return true;
1061 }
1062 
1063 static int try_use_trigger_and_cache_result(struct target *target, unsigned int idx, riscv_reg_t tdata1,
1065 {
1066  if (wp_triggers_cache_search(target, idx, tdata1, tdata2))
1068 
1069  int ret = set_trigger(target, idx, tdata1, tdata2);
1070 
1071  /* Add these values to the cache to remember that they are not supported. */
1073  wp_triggers_cache_add(target, idx, tdata1, tdata2, ret);
1074  return ret;
1075 }
1076 
1078  struct trigger *trigger, struct trigger_request_info trig_info)
1079 {
1080  LOG_TARGET_DEBUG(target, "trying to set up a match trigger");
1081  log_trigger_request_info(trig_info);
1082 
1083  int trigger_type =
1086  RISCV_INFO(r);
1087 
1088  /* Find the first trigger, supporting required tdata1 value */
1089  for (unsigned int idx = 0;
1090  find_next_free_trigger(target, trigger_type, false, &idx) == ERROR_OK;
1091  ++idx) {
1092  ret = try_use_trigger_and_cache_result(target, idx, trig_info.tdata1, trig_info.tdata2);
1093 
1094  if (ret == ERROR_OK) {
1095  r->trigger_unique_id[idx] = trigger->unique_id;
1096  return ERROR_OK;
1097  }
1099  return ret;
1100  }
1101  return ret;
1102 }
1103 
1105  struct trigger *trigger, struct trigger_request_info t1,
1106  struct trigger_request_info t2)
1107 {
1108  LOG_TARGET_DEBUG(target, "trying to set up a chain of match triggers");
1111  int trigger_type =
1114  RISCV_INFO(r);
1115 
1116  /* Find the first 2 consecutive triggers, supporting required tdata1 values */
1117  for (unsigned int idx = 0;
1118  find_next_free_trigger(target, trigger_type, true, &idx) == ERROR_OK;
1119  ++idx) {
1120  ret = try_use_trigger_and_cache_result(target, idx, t1.tdata1, t1.tdata2);
1121 
1123  continue;
1124  else if (ret != ERROR_OK)
1125  return ret;
1126 
1127  ret = try_use_trigger_and_cache_result(target, idx + 1, t2.tdata1, t2.tdata2);
1128 
1129  if (ret == ERROR_OK) {
1130  r->trigger_unique_id[idx] = trigger->unique_id;
1131  r->trigger_unique_id[idx + 1] = trigger->unique_id;
1132  return ERROR_OK;
1133  }
1134  /* Undo the setting of the previous trigger */
1135  int ret_undo = set_trigger(target, idx, 0, 0);
1136  if (ret_undo != ERROR_OK)
1137  return ret_undo;
1138 
1140  return ret;
1141  }
1142  return ret;
1143 }
1144 
1147  struct {
1148  /* Other values are available for this field,
1149  * but currently only `any` is needed.
1150  */
1152  } size;
1153  struct {
1157  struct {
1163 };
1164 
1166  struct trigger *trigger)
1167 {
1168  RISCV_INFO(r);
1169 
1170  struct match_triggers_tdata1_fields result = {
1171  .common =
1176  field_value(CSR_MCONTROL_S, !!(r->misa & BIT('S' - 'A'))) |
1177  field_value(CSR_MCONTROL_U, !!(r->misa & BIT('U' - 'A'))) |
1181  .size = {
1182  .any =
1185  },
1186  .chain = {
1189  },
1190  .match = {
1195  }
1196  };
1197  return result;
1198 }
1199 
1201  struct trigger *trigger)
1202 {
1203  bool misa_s = riscv_supports_extension(target, 'S');
1204  bool misa_u = riscv_supports_extension(target, 'U');
1205  bool misa_h = riscv_supports_extension(target, 'H');
1206 
1207  struct match_triggers_tdata1_fields result = {
1208  .common =
1213  field_value(CSR_MCONTROL6_S, misa_s) |
1214  field_value(CSR_MCONTROL6_U, misa_u) |
1215  field_value(CSR_MCONTROL6_VS, misa_h && misa_s) |
1216  field_value(CSR_MCONTROL6_VU, misa_h && misa_u) |
1220  .size = {
1222  },
1223  .chain = {
1226  },
1227  .match = {
1232  }
1233  };
1234  return result;
1235 }
1236 
1238  struct trigger *trigger, struct match_triggers_tdata1_fields fields)
1239 {
1240  RISCV_INFO(r);
1242 
1243  if (trigger->length > 0) {
1244  /* Setting a load/store trigger ("watchpoint") on a range of addresses */
1246  if (r->wp_allow_napot_trigger) {
1247  LOG_TARGET_DEBUG(target, "trying to setup NAPOT match trigger");
1248  struct trigger_request_info napot = {
1249  .tdata1 = fields.common | fields.size.any |
1250  fields.chain.disable | fields.match.napot,
1251  .tdata2 = trigger->address | ((trigger->length - 1) >> 1)
1252  };
1255  return ret;
1256  } else {
1257  LOG_TARGET_DEBUG(target, "NAPOT match triggers are disabled for watchpoints. "
1258  "Use 'riscv set_enable_trigger_feature napot wp' to enable it.");
1259  }
1260  }
1261 
1262  if (r->wp_allow_ge_lt_trigger) {
1263  LOG_TARGET_DEBUG(target, "trying to setup GE+LT chained match trigger pair");
1264  struct trigger_request_info ge_1 = {
1265  .tdata1 = fields.common | fields.size.any | fields.chain.enable |
1266  fields.match.ge,
1267  .tdata2 = trigger->address
1268  };
1269  struct trigger_request_info lt_2 = {
1270  .tdata1 = fields.common | fields.size.any | fields.chain.disable |
1271  fields.match.lt,
1272  .tdata2 = trigger->address + trigger->length
1273  };
1274  ret = try_setup_chained_match_triggers(target, trigger, ge_1, lt_2);
1276  return ret;
1277 
1278  LOG_TARGET_DEBUG(target, "trying to setup LT+GE chained match trigger pair");
1279  struct trigger_request_info lt_1 = {
1280  .tdata1 = fields.common | fields.size.any | fields.chain.enable |
1281  fields.match.lt,
1282  .tdata2 = trigger->address + trigger->length
1283  };
1284  struct trigger_request_info ge_2 = {
1285  .tdata1 = fields.common | fields.size.any | fields.chain.disable |
1286  fields.match.ge,
1287  .tdata2 = trigger->address
1288  };
1289  ret = try_setup_chained_match_triggers(target, trigger, lt_1, ge_2);
1291  return ret;
1292  } else {
1293  LOG_TARGET_DEBUG(target, "LT+GE chained match triggers are disabled for watchpoints. "
1294  "Use 'riscv set_enable_trigger_feature ge_lt wp' to enable it.");
1295  }
1296  }
1297 
1298  if (r->wp_allow_equality_match_trigger) {
1299  LOG_TARGET_DEBUG(target, "trying to setup equality match trigger");
1300  struct trigger_request_info eq = {
1301  .tdata1 = fields.common | fields.size.any | fields.chain.disable |
1302  fields.match.eq,
1303  .tdata2 = trigger->address
1304  };
1306  if (ret != ERROR_OK)
1307  return ret;
1308  } else {
1309  LOG_TARGET_DEBUG(target, "equality match triggers are disabled for watchpoints. "
1310  "Use 'riscv set_enable_trigger_feature eq wp' to enable it.");
1311  }
1312 
1313  if (ret == ERROR_OK && trigger->length > 1) {
1314  LOG_TARGET_DEBUG(target, "Trigger will match accesses at address 0x%" TARGET_PRIxADDR
1315  ", but may not match accesses at addresses in the inclusive range from 0x%"
1317  trigger->address + 1, trigger->address + trigger->length - 1);
1318  RISCV_INFO(info);
1319  if (!info->range_trigger_fallback_encountered)
1320  /* This message is displayed only once per target to avoid
1321  * overwhelming the user with such messages on resume.
1322  */
1324  "Could not set a trigger that will match a whole address range. "
1325  "As a fallback, this trigger (and maybe others) will only match "
1326  "against the first address of the range.");
1327  info->range_trigger_fallback_encountered = true;
1328  }
1329 
1330  return ret;
1331 }
1332 
1334  struct trigger *trigger, struct match_triggers_tdata1_fields fields)
1335 {
1336  LOG_TARGET_DEBUG(target, "trying to setup equality match trigger");
1337  struct trigger_request_info eq = {
1338  .tdata1 = fields.common | fields.size.any | fields.chain.disable |
1339  fields.match.eq,
1340  .tdata2 = trigger->address
1341  };
1342 
1344 }
1345 
1347  struct trigger *trigger, struct match_triggers_tdata1_fields fields)
1348 {
1349  if (trigger->is_execute) {
1350  assert(!trigger->is_read && !trigger->is_write);
1352  }
1353 
1354  assert(trigger->is_read || trigger->is_write);
1356 }
1357 
1358 static int maybe_add_trigger_t3(struct target *target, bool vs, bool vu,
1359  bool m, bool s, bool u, bool pending, unsigned int count,
1360  int unique_id)
1361 {
1362  int ret;
1364 
1365  RISCV_INFO(r);
1366 
1367  tdata1 = 0;
1378 
1379  unsigned int idx = 0;
1381  if (ret != ERROR_OK)
1382  return ret;
1383  ret = set_trigger(target, idx, tdata1, 0);
1384  if (ret != ERROR_OK)
1385  return ret;
1386  r->trigger_unique_id[idx] = unique_id;
1387  return ERROR_OK;
1388 }
1389 
1390 static int maybe_add_trigger_t4(struct target *target, bool vs, bool vu,
1391  bool nmi, bool m, bool s, bool u, riscv_reg_t interrupts,
1392  int unique_id)
1393 {
1394  int ret;
1396 
1397  RISCV_INFO(r);
1398 
1399  tdata1 = 0;
1409 
1410  tdata2 = interrupts;
1411 
1412  unsigned int idx = 0;
1414  if (ret != ERROR_OK)
1415  return ret;
1416  ret = set_trigger(target, idx, tdata1, tdata2);
1417  if (ret != ERROR_OK)
1418  return ret;
1419  r->trigger_unique_id[idx] = unique_id;
1420  return ERROR_OK;
1421 }
1422 
1423 static int maybe_add_trigger_t5(struct target *target, bool vs, bool vu,
1424  bool m, bool s, bool u, riscv_reg_t exception_codes,
1425  int unique_id)
1426 {
1427  int ret;
1429 
1430  RISCV_INFO(r);
1431 
1432  tdata1 = 0;
1441 
1442  tdata2 = exception_codes;
1443 
1444  unsigned int idx = 0;
1446  if (ret != ERROR_OK)
1447  return ret;
1448  ret = set_trigger(target, idx, tdata1, tdata2);
1449  if (ret != ERROR_OK)
1450  return ret;
1451  r->trigger_unique_id[idx] = unique_id;
1452  return ERROR_OK;
1453 }
1454 
1455 static int add_trigger(struct target *target, struct trigger *trigger)
1456 {
1457  int ret;
1458  riscv_reg_t tselect;
1459 
1461  if (ret != ERROR_OK)
1462  return ret;
1463 
1464  ret = riscv_reg_get(target, &tselect, GDB_REGNO_TSELECT);
1465  if (ret != ERROR_OK)
1466  return ret;
1467 
1468  do {
1470  if (ret == ERROR_OK)
1471  break;
1474  if (ret == ERROR_OK)
1475  break;
1478  if (ret == ERROR_OK)
1479  break;
1480  } while (0);
1481 
1482  if (riscv_reg_set(target, GDB_REGNO_TSELECT, tselect) != ERROR_OK &&
1483  ret == ERROR_OK)
1484  return ERROR_FAIL;
1485 
1486  return ret;
1487 }
1488 
1494  uint32_t size, uint8_t *buffer, uint32_t access_size)
1495 {
1496  assert(size == 1 || size == 2 || size == 4 || size == 8);
1497  assert(access_size == 1 || access_size == 2 || access_size == 4 || access_size == 8);
1498 
1499  if (access_size <= size && address % access_size == 0)
1500  /* Can do the memory access directly without a helper buffer. */
1501  return target_write_memory(target, address, access_size, size / access_size, buffer);
1502 
1503  unsigned int offset_head = address % access_size;
1504  unsigned int n_blocks = ((size + offset_head) <= access_size) ? 1 : 2;
1505  uint8_t helper_buf[n_blocks * access_size];
1506 
1507  /* Read from memory */
1508  if (target_read_memory(target, address - offset_head, access_size, n_blocks, helper_buf) != ERROR_OK)
1509  return ERROR_FAIL;
1510 
1511  /* Modify and write back */
1512  memcpy(helper_buf + offset_head, buffer, size);
1513  return target_write_memory(target, address - offset_head, access_size, n_blocks, helper_buf);
1514 }
1515 
1521  uint32_t size, uint8_t *buffer, uint32_t access_size)
1522 {
1523  assert(size == 1 || size == 2 || size == 4 || size == 8);
1524  assert(access_size == 1 || access_size == 2 || access_size == 4 || access_size == 8);
1525 
1526  if (access_size <= size && address % access_size == 0)
1527  /* Can do the memory access directly without a helper buffer. */
1528  return target_read_memory(target, address, access_size, size / access_size, buffer);
1529 
1530  unsigned int offset_head = address % access_size;
1531  unsigned int n_blocks = ((size + offset_head) <= access_size) ? 1 : 2;
1532  uint8_t helper_buf[n_blocks * access_size];
1533 
1534  /* Read from memory */
1535  if (target_read_memory(target, address - offset_head, access_size, n_blocks, helper_buf) != ERROR_OK)
1536  return ERROR_FAIL;
1537 
1538  /* Pick the requested portion from the buffer */
1539  memcpy(buffer, helper_buf + offset_head, size);
1540  return ERROR_OK;
1541 }
1542 
1548 {
1549  assert(size == 1 || size == 2 || size == 4 || size == 8);
1550 
1551  /* Find access size that correspond to data size and the alignment. */
1552  unsigned int preferred_size = size;
1553  while (address % preferred_size != 0)
1554  preferred_size /= 2;
1555 
1556  /* First try the preferred (most natural) access size. */
1557  if (write_by_given_size(target, address, size, buffer, preferred_size) == ERROR_OK)
1558  return ERROR_OK;
1559 
1560  /* On failure, try other access sizes.
1561  Minimize the number of accesses by trying first the largest size. */
1562  for (unsigned int access_size = 8; access_size > 0; access_size /= 2) {
1563  if (access_size == preferred_size)
1564  /* Already tried this size. */
1565  continue;
1566 
1567  if (write_by_given_size(target, address, size, buffer, access_size) == ERROR_OK)
1568  return ERROR_OK;
1569  }
1570 
1571  /* No access attempt succeeded. */
1572  return ERROR_FAIL;
1573 }
1574 
1580 {
1581  assert(size == 1 || size == 2 || size == 4 || size == 8);
1582 
1583  /* Find access size that correspond to data size and the alignment. */
1584  unsigned int preferred_size = size;
1585  while (address % preferred_size != 0)
1586  preferred_size /= 2;
1587 
1588  /* First try the preferred (most natural) access size. */
1589  if (read_by_given_size(target, address, size, buffer, preferred_size) == ERROR_OK)
1590  return ERROR_OK;
1591 
1592  /* On failure, try other access sizes.
1593  Minimize the number of accesses by trying first the largest size. */
1594  for (unsigned int access_size = 8; access_size > 0; access_size /= 2) {
1595  if (access_size == preferred_size)
1596  /* Already tried this size. */
1597  continue;
1598 
1599  if (read_by_given_size(target, address, size, buffer, access_size) == ERROR_OK)
1600  return ERROR_OK;
1601  }
1602 
1603  /* No access attempt succeeded. */
1604  return ERROR_FAIL;
1605 }
1606 
1608 {
1610  assert(breakpoint);
1611  if (breakpoint->type == BKPT_SOFT) {
1613  if (!(breakpoint->length == 4 || breakpoint->length == 2)) {
1614  LOG_TARGET_ERROR(target, "Invalid breakpoint length %d", breakpoint->length);
1615  return ERROR_FAIL;
1616  }
1617 
1618  if (0 != (breakpoint->address % 2)) {
1619  LOG_TARGET_ERROR(target, "Invalid breakpoint alignment for address 0x%" TARGET_PRIxADDR,
1620  breakpoint->address);
1621  return ERROR_FAIL;
1622  }
1623 
1624  /* Read the original instruction. */
1627  LOG_TARGET_ERROR(target, "Failed to read original instruction at 0x%" TARGET_PRIxADDR,
1628  breakpoint->address);
1629  return ERROR_FAIL;
1630  }
1631 
1632  uint8_t buff[4] = { 0 };
1633  buf_set_u32(buff, 0, breakpoint->length * CHAR_BIT, breakpoint->length == 4 ? ebreak() : ebreak_c());
1634  /* Write the ebreak instruction. */
1636  LOG_TARGET_ERROR(target, "Failed to write %d-byte breakpoint instruction at 0x%"
1638  return ERROR_FAIL;
1639  }
1640  breakpoint->is_set = true;
1641 
1642  } else if (breakpoint->type == BKPT_HARD) {
1643  struct trigger trigger;
1645  int const result = add_trigger(target, &trigger);
1646  if (result != ERROR_OK)
1647  return result;
1648 
1649  int trigger_idx = find_first_trigger_by_id(target, breakpoint->unique_id);
1650  breakpoint_hw_set(breakpoint, trigger_idx);
1651  } else {
1652  LOG_TARGET_INFO(target, "OpenOCD only supports hardware and software breakpoints.");
1654  }
1655  return ERROR_OK;
1656 }
1657 
1658 static int remove_trigger(struct target *target, int unique_id)
1659 {
1660  RISCV_INFO(r);
1661 
1663  return ERROR_FAIL;
1664 
1665  riscv_reg_t tselect;
1666  int result = riscv_reg_get(target, &tselect, GDB_REGNO_TSELECT);
1667  if (result != ERROR_OK)
1668  return result;
1669 
1670  bool done = false;
1671  for (unsigned int i = 0; i < r->trigger_count; i++) {
1672  if (r->trigger_unique_id[i] == unique_id) {
1675  r->trigger_unique_id[i] = -1;
1676  LOG_TARGET_DEBUG(target, "Stop using resource %d for bp %d",
1677  i, unique_id);
1678  done = true;
1679  }
1680  }
1681  if (!done) {
1683  "Couldn't find the hardware resources used by hardware trigger.");
1685  }
1686 
1688 
1689  return ERROR_OK;
1690 }
1691 
1693  struct breakpoint *breakpoint)
1694 {
1695  if (breakpoint->type == BKPT_SOFT) {
1696  /* Write the original instruction. */
1699  LOG_TARGET_ERROR(target, "Failed to restore instruction for %d-byte breakpoint at "
1701  return ERROR_FAIL;
1702  }
1703 
1704  } else if (breakpoint->type == BKPT_HARD) {
1705  struct trigger trigger;
1707  int result = remove_trigger(target, trigger.unique_id);
1708  if (result != ERROR_OK)
1709  return result;
1710 
1711  } else {
1712  LOG_TARGET_INFO(target, "OpenOCD only supports hardware and software breakpoints.");
1714  }
1715 
1716  breakpoint->is_set = false;
1717 
1718  return ERROR_OK;
1719 }
1720 
1722  const struct watchpoint *watchpoint)
1723 {
1730  trigger->is_execute = false;
1731  /* unique_id is unique across both breakpoints and watchpoints. */
1733 }
1734 
1736 {
1738  LOG_TARGET_ERROR(target, "Watchpoints on data values are not implemented");
1740  }
1741 
1742  struct trigger trigger;
1744 
1745  int result = add_trigger(target, &trigger);
1746  if (result != ERROR_OK)
1747  return result;
1748 
1749  int trigger_idx = find_first_trigger_by_id(target, watchpoint->unique_id);
1750  watchpoint_set(watchpoint, trigger_idx);
1751 
1752  return ERROR_OK;
1753 }
1754 
1756  struct watchpoint *watchpoint)
1757 {
1758  LOG_TARGET_DEBUG(target, "Removing watchpoint @0x%" TARGET_PRIxADDR, watchpoint->address);
1759 
1760  struct trigger trigger;
1762 
1763  int result = remove_trigger(target, trigger.unique_id);
1764  if (result != ERROR_OK)
1765  return result;
1766  watchpoint->is_set = false;
1767 
1768  return ERROR_OK;
1769 }
1770 
1778 };
1779 
1781  riscv_reg_t tdata1, uint64_t hit_mask)
1782 {
1783  const uint32_t hit0 = get_field(tdata1, CSR_MCONTROL6_HIT0);
1784  const uint32_t hit1 = get_field(tdata1, CSR_MCONTROL6_HIT1);
1785  const uint32_t hit_info = (hit1 << 1) | hit0;
1786  if (hit_info == CSR_MCONTROL6_HIT0_BEFORE)
1787  return M6_HIT_BEFORE;
1788 
1789  if (hit_info == CSR_MCONTROL6_HIT0_AFTER)
1790  return M6_HIT_AFTER;
1791 
1792  if (hit_info == CSR_MCONTROL6_HIT0_IMMEDIATELY_AFTER)
1793  return M6_HIT_IMM_AFTER;
1794 
1795  if (hit_info == CSR_MCONTROL6_HIT0_FALSE) {
1796  /* hit[1..0] equals 0, which can mean one of the following:
1797  * - "hit" bits are supported and this trigger has not fired
1798  * - "hit" bits are not supported on this trigger
1799  * To distinguish these two cases, try writing all non-zero bit
1800  * patterns to hit[1..0] to determine if the "hit" bits are supported:
1801  */
1802  riscv_reg_t tdata1_tests[] = {
1803  set_field(tdata1, CSR_MCONTROL6_HIT0, 1),
1804  set_field(tdata1, CSR_MCONTROL6_HIT1, 1),
1806  };
1807  riscv_reg_t tdata1_test_rb;
1808  for (uint64_t i = 0; i < ARRAY_SIZE(tdata1_tests); ++i) {
1809  if (riscv_reg_set(target, GDB_REGNO_TDATA1, tdata1_tests[i]) != ERROR_OK)
1810  return M6_HIT_ERROR;
1811  if (riscv_reg_get(target, &tdata1_test_rb, GDB_REGNO_TDATA1) != ERROR_OK)
1812  return M6_HIT_ERROR;
1813  if (tdata1_test_rb == tdata1_tests[i]) {
1814  if (riscv_reg_set(target, GDB_REGNO_TDATA1, tdata1_test_rb & ~hit_mask) != ERROR_OK)
1815  return M6_HIT_ERROR;
1816  return M6_NOT_HIT;
1817  }
1818  }
1819  }
1820  return M6_HIT_NOT_SUPPORTED;
1821 }
1822 
1830  bool *need_single_step)
1831 {
1832  /* FIXME: this function assumes that we have only one trigger that can
1833  * have hit bit set. Debug spec allows hit bit to bit set if a trigger has
1834  * matched but did not fire. Such targets will receive erroneous results.
1835  */
1836 
1837  RISCV_INFO(r);
1838  assert(need_single_step);
1839  *need_single_step = false;
1840 
1841  riscv_reg_t tselect;
1842  if (riscv_reg_get(target, &tselect, GDB_REGNO_TSELECT) != ERROR_OK)
1843  return ERROR_FAIL;
1844 
1846  for (unsigned int i = 0; i < r->trigger_count; i++) {
1847  if (r->trigger_unique_id[i] == -1)
1848  continue;
1849 
1851  return ERROR_FAIL;
1852 
1853  uint64_t tdata1;
1854  if (riscv_reg_get(target, &tdata1, GDB_REGNO_TDATA1) != ERROR_OK)
1855  return ERROR_FAIL;
1856  int type = get_field(tdata1, CSR_TDATA1_TYPE(riscv_xlen(target)));
1857 
1858  uint64_t hit_mask = 0;
1859  switch (type) {
1861  /* Doesn't support hit bit. */
1862  break;
1864  hit_mask = CSR_MCONTROL_HIT;
1865  *need_single_step = true;
1866  break;
1869  if (r->tinfo_version == CSR_TINFO_VERSION_0) {
1870  *need_single_step = true;
1871  } else if (r->tinfo_version == RISCV_TINFO_VERSION_UNKNOWN
1872  || r->tinfo_version == CSR_TINFO_VERSION_1) {
1874  tdata1, hit_mask);
1875  if (hits_status == M6_HIT_ERROR)
1876  return ERROR_FAIL;
1877  if (hits_status == M6_HIT_BEFORE || hits_status == M6_HIT_NOT_SUPPORTED)
1878  *need_single_step = true;
1879  }
1880  break;
1882  hit_mask = CSR_ICOUNT_HIT;
1883  break;
1885  hit_mask = CSR_ITRIGGER_HIT(riscv_xlen(target));
1886  break;
1888  hit_mask = CSR_ETRIGGER_HIT(riscv_xlen(target));
1889  break;
1890  default:
1891  LOG_TARGET_DEBUG(target, "Trigger %u has unknown type %d", i, type);
1892  continue;
1893  }
1894 
1895  /* FIXME: this logic needs to be changed to ignore triggers that are not
1896  * the last one in the chain. */
1897  if (tdata1 & hit_mask) {
1898  LOG_TARGET_DEBUG(target, "Trigger %u (unique_id=%" PRIi64
1899  ") has hit bit set. (need_single_step=%s)",
1900  i, r->trigger_unique_id[i], (*need_single_step) ? "yes" : "no");
1901  if (riscv_reg_set(target, GDB_REGNO_TDATA1, tdata1 & ~hit_mask) != ERROR_OK)
1902  return ERROR_FAIL;
1903 
1904  *unique_id = r->trigger_unique_id[i];
1905  break;
1906  }
1907  }
1908 
1909  if (riscv_reg_set(target, GDB_REGNO_TSELECT, tselect) != ERROR_OK)
1910  return ERROR_FAIL;
1911 
1912  return ERROR_OK;
1913 }
1914 
1919 // c.lwsp rd_n0 c_uimm8sphi c_uimm8splo - offset[5] offset[4:2|7:6]
1920 static uint16_t get_offset_clwsp(riscv_insn_t instruction)
1921 {
1922  uint16_t offset_4to2and7to6_bits =
1923  get_field32(instruction, INSN_FIELD_C_UIMM8SPLO);
1924  uint16_t offset_4to2_bits = offset_4to2and7to6_bits >> 2;
1925  uint16_t offset_7to6_bits = offset_4to2and7to6_bits & 0x3;
1926  uint16_t offset_5_bit = get_field32(instruction, INSN_FIELD_C_UIMM8SPHI);
1927  return (offset_4to2_bits << 2) + (offset_5_bit << 5)
1928  + (offset_7to6_bits << 6);
1929 }
1930 
1931 // c.ldsp rd_n0 c_uimm9sphi c_uimm9splo - offset[5] offset[4:3|8:6]
1932 static uint16_t get_offset_cldsp(riscv_insn_t instruction)
1933 {
1934  uint16_t offset_4to3and8to6_bits =
1935  get_field32(instruction, INSN_FIELD_C_UIMM9SPLO);
1936  uint16_t offset_4to3_bits = offset_4to3and8to6_bits >> 3;
1937  uint16_t offset_8to6_bits = offset_4to3and8to6_bits & 0x7;
1938  uint16_t offset_5_bit = get_field32(instruction, INSN_FIELD_C_UIMM9SPHI);
1939  return (offset_4to3_bits << 3) + (offset_5_bit << 5)
1940  + (offset_8to6_bits << 6);
1941 }
1942 
1943 // c.swsp c_rs2 c_uimm8sp_s - offset[5:2|7:6]
1944 static uint16_t get_offset_cswsp(riscv_insn_t instruction)
1945 {
1946  uint16_t offset_5to2and7to6_bits =
1947  get_field32(instruction, INSN_FIELD_C_UIMM8SP_S);
1948  uint16_t offset_5to2_bits = offset_5to2and7to6_bits >> 2;
1949  uint16_t offset_7to6_bits = offset_5to2and7to6_bits & 0x3;
1950  return (offset_5to2_bits << 2) + (offset_7to6_bits << 6);
1951 }
1952 
1953 // c.sdsp c_rs2 c_uimm9sp_s - offset[5:3|8:6]
1954 static uint16_t get_offset_csdsp(riscv_insn_t instruction)
1955 {
1956  uint16_t offset_5to3and8to6_bits =
1957  get_field32(instruction, INSN_FIELD_C_UIMM9SP_S);
1958  uint16_t offset_5to3_bits = offset_5to3and8to6_bits >> 3;
1959  uint16_t offset_8to6_bits = offset_5to3and8to6_bits & 0x7;
1960  return (offset_5to3_bits << 3) + (offset_8to6_bits << 6);
1961 }
1962 
1963 // c.lw rd_p rs1_p c_uimm7lo c_uimm7hi - offset[2|6] offset[5:3]
1964 static uint16_t get_offset_clw(riscv_insn_t instruction)
1965 {
1966  uint16_t offset_2and6_bits = get_field32(instruction, INSN_FIELD_C_UIMM7LO);
1967  uint16_t offset_2_bit = offset_2and6_bits >> 1;
1968  uint16_t offset_6_bit = offset_2and6_bits & 0x1;
1969  uint16_t offset_5to3_bits = get_field32(instruction, INSN_FIELD_C_UIMM7HI);
1970  return (offset_2_bit << 2) + (offset_5to3_bits << 3) + (offset_6_bit << 6);
1971 }
1972 
1973 // c.ld rd_p rs1_p c_uimm8lo c_uimm8hi - offset[7:6] offset[5:3]
1974 static uint16_t get_offset_cld(riscv_insn_t instruction)
1975 {
1976  uint16_t offset_7to6_bits = get_field32(instruction, INSN_FIELD_C_UIMM8LO);
1977  uint16_t offset_5to3_bits = get_field32(instruction, INSN_FIELD_C_UIMM8HI);
1978  return (offset_5to3_bits << 3) + (offset_7to6_bits << 6);
1979 }
1980 
1981 // c.lq rd_p rs1_p c_uimm9lo c_uimm9hi - offset[7:6] offset[5|4|8]
1982 static uint16_t get_offset_clq(riscv_insn_t instruction)
1983 {
1984  uint16_t offset_7to6_bits = get_field32(instruction, INSN_FIELD_C_UIMM9LO);
1985  uint16_t offset_5to4and8_bits =
1986  get_field32(instruction, INSN_FIELD_C_UIMM9HI);
1987  uint16_t offset_5to4_bits = offset_5to4and8_bits >> 1;
1988  uint16_t offset_8_bit = offset_5to4and8_bits & 0x1;
1989  return (offset_5to4_bits << 4) + (offset_7to6_bits << 6)
1990  + (offset_8_bit << 8);
1991 }
1992 
1993 // c.lqsp rd_n0 c_uimm10sphi c_uimm10splo - offset[5] offset[4|9:6]
1994 static uint16_t get_offset_clqsp(riscv_insn_t instruction)
1995 {
1996  uint16_t offset_4and9to6_bits =
1997  get_field32(instruction, INSN_FIELD_C_UIMM10SPLO);
1998  uint16_t offset_4_bit = offset_4and9to6_bits >> 4;
1999  uint16_t offset_9to6_bits = offset_4and9to6_bits & 0xf;
2000  uint16_t offset_5_bit = get_field32(instruction, INSN_FIELD_C_UIMM10SPHI);
2001  return (offset_4_bit << 4) + (offset_5_bit << 5) + (offset_9to6_bits << 6);
2002 }
2003 
2004 // c.sqsp c_rs2 c_uimm10sp_s - offset[5:4|9:6]
2005 static uint16_t get_offset_csqsp(riscv_insn_t instruction)
2006 {
2007  uint16_t offset_5to4and9to6_bits =
2008  get_field32(instruction, INSN_FIELD_C_UIMM10SP_S);
2009  uint16_t offset_5to4_biits = offset_5to4and9to6_bits >> 4;
2010  uint16_t offset_9to6_bits = offset_5to4and9to6_bits & 0xf;
2011  return (offset_5to4_biits << 4) + (offset_9to6_bits << 6);
2012 }
2013 
2020 static uint32_t get_rs1_c(riscv_insn_t instruction)
2021 {
2022  return GDB_REGNO_S0 + get_field32(instruction, INSN_FIELD_C_SREG1);
2023 }
2024 
2025 static uint32_t get_opcode(const riscv_insn_t instruction)
2026 {
2027  // opcode is first 7 bits of the instruction
2028  uint32_t opcode = instruction & INSN_FIELD_OPCODE;
2029  if ((instruction & 0x03) < 0x03) { // opcode size RVC
2030  // RVC MASK_C = 0xe003 for load/store instructions
2031  opcode = instruction & MASK_C_LD;
2032  }
2033  return opcode;
2034 }
2035 
2037  const riscv_insn_t instruction, int *regid)
2038 {
2039  uint32_t opcode = get_opcode(instruction);
2040  int rs;
2041 
2042  switch (opcode) {
2043  case MATCH_LB:
2044  case MATCH_FLH & ~INSN_FIELD_FUNCT3:
2045  case MATCH_SB:
2046  case MATCH_FSH & ~INSN_FIELD_FUNCT3:
2047  rs = get_field32(instruction, INSN_FIELD_RS1);
2048  break;
2049 
2050  case MATCH_C_LWSP:
2051  case MATCH_C_LDSP: // if xlen >= 64 or MATCH_C_FLWSP:
2052  case MATCH_C_FLDSP: // or MATCH_C_LQSP if xlen == 128
2053  case MATCH_C_SWSP:
2054  case MATCH_C_SDSP: // if xlen >= 64 or MATCH_C_FSWSP:
2055  case MATCH_C_FSDSP: // or MATCH_C_SQSP if xlen == 128
2056  rs = GDB_REGNO_SP;
2057  break;
2058 
2059  case MATCH_C_LW:
2060  case MATCH_C_FLW: // or MATCH_C_LD if xlen >= 64
2061  case MATCH_C_FLD: // or MATCH_C_LQ if xlen == 128
2062  case MATCH_C_SW:
2063  case MATCH_C_FSW: // or MATCH_C_SD if xlen >= 64
2064  case MATCH_C_FSD: // or MATCH_C_SQ if xlen == 128
2065  rs = get_rs1_c(instruction);
2066  break;
2067 
2068  default:
2069  LOG_TARGET_DEBUG(target, "0x%" PRIx32 " is not a RV32I or \"C\" load or"
2070  " store", instruction);
2071  return ERROR_FAIL;
2072  }
2073  *regid = rs;
2074  return ERROR_OK;
2075 }
2076 
2078  const riscv_insn_t instruction, int16_t *memoffset)
2079 {
2080  uint32_t opcode = get_opcode(instruction);
2081  int16_t offset = 0;
2082 
2083  switch (opcode) {
2084  case MATCH_LB:
2085  case MATCH_FLH & ~INSN_FIELD_FUNCT3:
2086  case MATCH_SB:
2087  case MATCH_FSH & ~INSN_FIELD_FUNCT3:
2088  if (opcode == MATCH_SB || opcode == (MATCH_FSH & ~INSN_FIELD_FUNCT3)) {
2089  offset = get_field32(instruction, INSN_FIELD_IMM12LO) |
2090  (get_field32(instruction, INSN_FIELD_IMM12HI) << 5);
2091  } else if (opcode == MATCH_LB ||
2092  opcode == (MATCH_FLH & ~INSN_FIELD_FUNCT3)) {
2093  offset = get_field32(instruction, INSN_FIELD_IMM12);
2094  } else {
2095  assert(false);
2096  }
2097  /* sign extend 12-bit imm to 16-bits */
2098  if (offset & (1 << 11))
2099  offset |= 0xf000;
2100  break;
2101 
2102  case MATCH_C_LWSP:
2103  offset = get_offset_clwsp(instruction);
2104  break;
2105 
2106  case MATCH_C_LDSP: // if xlen >= 64 or MATCH_C_FLWSP:
2107  if (riscv_xlen(target) > 32) { // MATCH_C_LDSP
2108  offset = get_offset_cldsp(instruction);
2109  } else { // MATCH_C_FLWSP
2110  offset = get_offset_clwsp(instruction);
2111  }
2112  break;
2113 
2114  case MATCH_C_FLDSP: // or MATCH_C_LQSP if xlen == 128
2115  if (riscv_xlen(target) == 128) { // MATCH_C_LQSP
2116  offset = get_offset_clqsp(instruction);
2117  } else { // MATCH_C_FLDSP
2118  offset = get_offset_cldsp(instruction);
2119  }
2120  break;
2121 
2122  case MATCH_C_SWSP:
2123  offset = get_offset_cswsp(instruction);
2124  break;
2125 
2126  case MATCH_C_SDSP: // if xlen >= 64 or MATCH_C_FSWSP:
2127  if (riscv_xlen(target) > 32) { // MATCH_C_SDSP
2128  offset = get_offset_csdsp(instruction);
2129  } else { // MATCH_C_FSWSP
2130  offset = get_offset_cswsp(instruction);
2131  }
2132  break;
2133 
2134  case MATCH_C_FSDSP: // or MATCH_C_SQSP if xlen == 128
2135  if (riscv_xlen(target) == 128) { // MATCH_C_SQSP
2136  offset = get_offset_csqsp(instruction);
2137  } else { // MATCH_C_FSDSP
2138  offset = get_offset_csdsp(instruction); // same as C.SDSP
2139  }
2140  break;
2141 
2142  case MATCH_C_LW:
2143  offset = get_offset_clw(instruction);
2144  break;
2145 
2146  case MATCH_C_FLW: // or MATCH_C_LD if xlen >= 64
2147  if (riscv_xlen(target) > 32) { // MATCH_C_LD
2148  offset = get_offset_cld(instruction);
2149  } else { // MATCH_C_FLW
2150  offset = get_offset_clw(instruction); // same as C.FLW
2151  }
2152  break;
2153 
2154  case MATCH_C_FLD: // or MATCH_C_LQ if xlen == 128
2155  if (riscv_xlen(target) == 128) { // MATCH_C_LQ
2156  offset = get_offset_clq(instruction);
2157  } else { // MATCH_C_FLD
2158  offset = get_offset_cld(instruction); // same as C.LD
2159  }
2160  break;
2161 
2162  case MATCH_C_SW:
2163  offset = get_offset_clw(instruction); // same as C.LW
2164  break;
2165 
2166  case MATCH_C_FSW: // or MATCH_C_SD if xlen >= 64
2167  if (riscv_xlen(target) > 32) { // MATCH_C_SD
2168  offset = get_offset_cld(instruction); // same as C.LD
2169  } else { // MATCH_C_FSW
2170  offset = get_offset_clw(instruction); // same as C.LW
2171  }
2172  break;
2173 
2174  case MATCH_C_FSD: // or MATCH_C_SQ if xlen == 128
2175  if (riscv_xlen(target) == 128) { // MATCH_C_SQ
2176  offset = get_offset_clq(instruction); // same as C.LQ
2177  } else { // MATCH_C_FSD
2178  offset = get_offset_cld(instruction); // same as C.LD
2179  }
2180  break;
2181 
2182  default:
2183  LOG_TARGET_DEBUG(target, "0x%" PRIx32 " is not a RV32I or \"C\" load or"
2184  " store", instruction);
2185  return ERROR_FAIL;
2186  }
2187  *memoffset = offset;
2188  return ERROR_OK;
2189 }
2190 
2191 static int verify_loadstore(struct target *target,
2192  const riscv_insn_t instruction, bool *is_read)
2193 {
2194  uint32_t opcode = get_opcode(instruction);
2195  bool misa_f = riscv_supports_extension(target, 'F');
2196  bool misa_d = riscv_supports_extension(target, 'D');
2197  enum watchpoint_rw rw;
2198 
2199  switch (opcode) {
2200  case MATCH_LB:
2201  case MATCH_FLH & ~INSN_FIELD_FUNCT3:
2202  rw = WPT_READ;
2203  break;
2204 
2205  case MATCH_SB:
2206  case MATCH_FSH & ~INSN_FIELD_FUNCT3:
2207  rw = WPT_WRITE;
2208  break;
2209 
2210  case MATCH_C_LWSP:
2211  if (get_field32(instruction, INSN_FIELD_RD) == 0) {
2213  "The code points with rd = x0 are reserved for C.LWSP");
2214  return ERROR_FAIL;
2215  }
2216  rw = WPT_READ;
2217  break;
2218 
2219  case MATCH_C_LDSP: // if xlen >= 64 or MATCH_C_FLWSP:
2220  if (riscv_xlen(target) > 32) { // MATCH_C_LDSP
2221  if (get_field32(instruction, INSN_FIELD_RD) == 0) {
2223  "The code points with rd = x0 are reserved for C.LDSP");
2224  return ERROR_FAIL;
2225  }
2226  } else { // MATCH_C_FLWSP
2227  if (!misa_f) {
2228  LOG_TARGET_DEBUG(target, "Matched C.FLWSP but target doesn\'t "
2229  "have the \"F\" extension");
2230  return ERROR_FAIL;
2231  }
2232  }
2233  rw = WPT_READ;
2234  break;
2235 
2236  case MATCH_C_FLDSP: // or MATCH_C_LQSP if xlen == 128
2237  if (riscv_xlen(target) == 128) { // MATCH_C_LQSP
2238  if (get_field32(instruction, INSN_FIELD_RD) == 0) {
2240  "The code points with rd = x0 are reserved for C.LQSP");
2241  return ERROR_FAIL;
2242  }
2243  } else { // MATCH_C_FLDSP
2244  if (!misa_d) {
2245  LOG_TARGET_DEBUG(target, "Matched C.FLDSP but target doesn\'t "
2246  "have the \"D\" extension");
2247  return ERROR_FAIL;
2248  }
2249  }
2250  rw = WPT_READ;
2251  break;
2252 
2253  case MATCH_C_SWSP:
2254  rw = WPT_WRITE;
2255  break;
2256 
2257  case MATCH_C_SDSP: // if xlen >= 64 or MATCH_C_FSWSP:
2258  if (riscv_xlen(target) == 32) { // MATCH_C_FSWSP
2259  if (!misa_f) {
2260  LOG_TARGET_DEBUG(target, "Matched C.FSWSP but target doesn\'t "
2261  "have the \"F\" extension");
2262  return ERROR_FAIL;
2263  }
2264  }
2265  rw = WPT_WRITE;
2266  break;
2267 
2268  case MATCH_C_FSDSP: // or MATCH_C_SQSP if xlen == 128
2269  if (riscv_xlen(target) != 128) { // MATCH_C_SQSP
2270  if (!misa_d) {
2271  LOG_TARGET_DEBUG(target, "Matched C.FSDSP but target doesn\'t "
2272  "have the \"D\" extension");
2273  return ERROR_FAIL;
2274  }
2275  }
2276  rw = WPT_WRITE;
2277  break;
2278 
2279  case MATCH_C_LW:
2280  rw = WPT_READ;
2281  break;
2282 
2283  case MATCH_C_FLW: // or MATCH_C_LD if xlen >= 64
2284  if (riscv_xlen(target) == 32) { // MATCH_C_FLW
2285  if (!misa_f) {
2286  LOG_TARGET_DEBUG(target, "Matched C.FLW but target doesn\'t "
2287  "have the \"F\" extension");
2288  return ERROR_FAIL;
2289  }
2290  }
2291  rw = WPT_READ;
2292  break;
2293 
2294  case MATCH_C_FLD: // or MATCH_C_LQ if xlen == 128
2295  if (riscv_xlen(target) != 128) { // MATCH_C_FLD
2296  if (!misa_d) {
2297  LOG_TARGET_DEBUG(target, "Matched C.FLD but target doesn\'t "
2298  "have the \"D\" extension");
2299  return ERROR_FAIL;
2300  }
2301  }
2302  rw = WPT_READ;
2303  break;
2304 
2305  case MATCH_C_SW:
2306  rw = WPT_WRITE;
2307  break;
2308 
2309  case MATCH_C_FSW: // or MATCH_C_SD if xlen >= 64
2310  if (riscv_xlen(target) == 32) { // MATCH_C_FSW
2311  if (!misa_f) {
2312  LOG_TARGET_DEBUG(target, "Matched C.FSW but target doesn\'t "
2313  "have the \"F\" extension");
2314  return ERROR_FAIL;
2315  }
2316  }
2317  rw = WPT_WRITE;
2318  break;
2319 
2320  case MATCH_C_FSD: // or MATCH_C_SQ if xlen == 128
2321  if (riscv_xlen(target) != 128) { // MATCH_C_FSD
2322  if (!misa_d) {
2323  LOG_TARGET_DEBUG(target, "Matched C.FSD but target doesn\'t "
2324  "have the \"D\" extension");
2325  return ERROR_FAIL;
2326  }
2327  }
2328  rw = WPT_WRITE;
2329  break;
2330 
2331  default:
2332  LOG_TARGET_DEBUG(target, "0x%" PRIx32 " is not a RV32I or \"C\" load or"
2333  " store", instruction);
2334  return ERROR_FAIL;
2335  }
2336 
2337  if (rw == WPT_WRITE) {
2338  *is_read = false;
2339  LOG_TARGET_DEBUG(target, "0x%" PRIx32 " is store instruction",
2340  instruction);
2341  } else {
2342  *is_read = true;
2343  LOG_TARGET_DEBUG(target, "0x%" PRIx32 " is load instruction",
2344  instruction);
2345  }
2346  return ERROR_OK;
2347 }
2348 
2349 /* Sets *hit_watchpoint to the first watchpoint identified as causing the
2350  * current halt.
2351  *
2352  * The GDB server uses this information to tell GDB what data address has
2353  * been hit, which enables GDB to print the hit variable along with its old
2354  * and new value. */
2355 static int riscv_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
2356 {
2357  RISCV_INFO(r);
2358 
2359  LOG_TARGET_DEBUG(target, "Hit Watchpoint");
2360 
2361  /* If we identified which trigger caused the halt earlier, then just use
2362  * that. */
2363  for (struct watchpoint *wp = target->watchpoints; wp; wp = wp->next) {
2364  if (wp->unique_id == r->trigger_hit) {
2365  *hit_watchpoint = wp;
2366  return ERROR_OK;
2367  }
2368  }
2369 
2370  riscv_reg_t dpc;
2371  if (riscv_reg_get(target, &dpc, GDB_REGNO_DPC) != ERROR_OK)
2372  return ERROR_FAIL;
2373  const uint8_t length = 4;
2374  LOG_TARGET_DEBUG(target, "dpc is 0x%" PRIx64, dpc);
2375 
2376  /* fetch the instruction at dpc */
2377  uint8_t buffer[length];
2378  if (target_read_buffer(target, dpc, length, buffer) != ERROR_OK) {
2379  LOG_TARGET_ERROR(target, "Failed to read instruction at dpc 0x%" PRIx64,
2380  dpc);
2381  return ERROR_FAIL;
2382  }
2383 
2384  riscv_insn_t instruction = 0;
2385 
2386  for (int i = 0; i < length; i++) {
2387  LOG_TARGET_DEBUG(target, "Next byte is %x", buffer[i]);
2388  instruction += (buffer[i] << 8 * i);
2389  }
2390  LOG_TARGET_DEBUG(target, "Full instruction is %x", instruction);
2391 
2392  int rs;
2393  target_addr_t mem_addr;
2394  int16_t memoffset;
2395 
2396  if (get_loadstore_membase_regno(target, instruction, &rs) != ERROR_OK)
2397  return ERROR_FAIL;
2398  if (riscv_reg_get(target, &mem_addr, rs) != ERROR_OK)
2399  return ERROR_FAIL;
2400  if (get_loadstore_memoffset(target, instruction, &memoffset) != ERROR_OK)
2401  return ERROR_FAIL;
2402 
2403  mem_addr += memoffset;
2404  bool is_load;
2405 
2406  if (verify_loadstore(target, instruction, &is_load) != ERROR_OK)
2407  return ERROR_FAIL;
2408 
2409  struct watchpoint *wp = target->watchpoints;
2410  while (wp) {
2411  /* TODO support mask and check read/write/access */
2412  /* TODO check for intersection of the access range and watchpoint range
2413  Recommended matching:
2414  if (intersects(mem_addr, mem_addr + ref_size, wp->address, wp->address + wp->length))
2415  */
2416  if (mem_addr >= wp->address &&
2417  mem_addr < (wp->address + wp->length)) {
2418  *hit_watchpoint = wp;
2419  LOG_TARGET_DEBUG(target, "WP hit found: %s 0x%" TARGET_PRIxADDR
2420  " covered by %s wp at address 0x%" TARGET_PRIxADDR,
2421  is_load ? "Load from" : "Store to", mem_addr,
2422  (wp->rw == WPT_READ ?
2423  "read" : (wp->rw == WPT_WRITE ? "write" : "access")),
2424  wp->address);
2425  return ERROR_OK;
2426  }
2427  wp = wp->next;
2428  }
2429 
2430  /* No match found - either we hit a watchpoint caused by an instruction that
2431  * this function does not yet disassemble, or we hit a breakpoint.
2432  *
2433  * OpenOCD will behave as if this function had never been implemented i.e.
2434  * report the halt to GDB with no address information. */
2435  LOG_TARGET_DEBUG(target, "No watchpoint found that would cover %s 0x%"
2436  TARGET_PRIxADDR, is_load ? "load from" : "store to", mem_addr);
2437  return ERROR_FAIL;
2438 }
2439 
2440 static int oldriscv_step(struct target *target, bool current, uint32_t address,
2441  bool handle_breakpoints)
2442 {
2443  struct target_type *tt = get_target_type(target);
2444  if (!tt)
2445  return ERROR_FAIL;
2446  return tt->step(target, current, address, handle_breakpoints);
2447 }
2448 
2449 static int riscv_openocd_step_impl(struct target *target, bool current,
2450  target_addr_t address, bool handle_breakpoints, int handle_callbacks);
2451 
2452 static int old_or_new_riscv_step_impl(struct target *target, bool current,
2453  target_addr_t address, bool handle_breakpoints, int handle_callbacks)
2454 {
2455  RISCV_INFO(r);
2456  LOG_TARGET_DEBUG(target, "handle_breakpoints=%s",
2457  handle_breakpoints ? "true" : "false");
2458  if (!r->get_hart_state)
2459  return oldriscv_step(target, current, address, handle_breakpoints);
2460  else
2461  return riscv_openocd_step_impl(target, current, address, handle_breakpoints,
2462  handle_callbacks);
2463 }
2464 
2465 static int old_or_new_riscv_step(struct target *target, bool current,
2466  target_addr_t address, bool handle_breakpoints)
2467 {
2468  return old_or_new_riscv_step_impl(target, current, address,
2469  handle_breakpoints, true /* handle callbacks*/);
2470 }
2471 
2472 static int riscv_examine(struct target *target)
2473 {
2474  LOG_TARGET_DEBUG(target, "Starting examination");
2475  if (target_was_examined(target)) {
2476  LOG_TARGET_DEBUG(target, "Target was already examined.");
2477  return ERROR_OK;
2478  }
2479 
2480  /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
2481 
2482  RISCV_INFO(info);
2483  uint32_t dtmcontrol;
2484  if (dtmcs_scan(target->tap, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
2485  LOG_TARGET_ERROR(target, "Could not read dtmcontrol. Check JTAG connectivity/board power.");
2486  return ERROR_FAIL;
2487  }
2488  LOG_TARGET_DEBUG(target, "dtmcontrol=0x%" PRIx32, dtmcontrol);
2489  uint32_t dtm_version = get_field(dtmcontrol, DTMCONTROL_VERSION);
2490  LOG_TARGET_DEBUG(target, "version=0x%" PRIx32, dtm_version);
2491 
2492  struct target_type *tt;
2493  if (info->dtm_version == DTM_DTMCS_VERSION_UNKNOWN) {
2494  info->dtm_version = dtm_version;
2495  tt = get_target_type(target);
2496  if (!tt) {
2497  info->dtm_version = DTM_DTMCS_VERSION_UNKNOWN;
2498  return ERROR_FAIL;
2499  }
2500 
2501  int retval = tt->init_target(info->cmd_ctx, target);
2502  if (retval != ERROR_OK) {
2503  info->dtm_version = DTM_DTMCS_VERSION_UNKNOWN;
2504  return retval;
2505  }
2506  } else {
2507  if (info->dtm_version != dtm_version) {
2508  // REVISIT: could we deinit_target, change version and init_target again?
2509  LOG_TARGET_ERROR(target, "dtmcs.version changed to 0x%" PRIx32, dtm_version);
2510  return ERROR_FAIL;
2511  }
2512  tt = get_target_type(target);
2513  if (!tt)
2514  return ERROR_FAIL;
2515  }
2516 
2517  return tt->examine(target);
2518 }
2519 
2520 static int oldriscv_poll(struct target *target)
2521 {
2522  struct target_type *tt = get_target_type(target);
2523  if (!tt)
2524  return ERROR_FAIL;
2525  return tt->poll(target);
2526 }
2527 
2529 {
2530  RISCV_INFO(r);
2531  if (!r->get_hart_state)
2532  return oldriscv_poll(target);
2533  else
2534  return riscv_openocd_poll(target);
2535 }
2536 
2537 static enum target_debug_reason
2539 {
2540  /* TODO: if we detect that etrigger/itrigger/icount is set, we should
2541  * just report DBG_REASON_UNKNOWN, since we can't disctiguish these
2542  * triggers from BP/WP or from other triggers of such type. However,
2543  * currently this renders existing testsuite as failing. We need to
2544  * fix the testsuite first
2545  */
2546  // TODO: the code below does not handle context-aware trigger types
2547  for (const struct breakpoint *bp = target->breakpoints; bp; bp = bp->next) {
2548  // TODO: investigate if we need to handle bp length
2549  if (bp->type == BKPT_HARD && bp->is_set && bp->address == dpc) {
2550  // FIXME: bp->linked_brp is uninitialized
2551  if (bp->asid) {
2553  "can't derive debug reason for context-aware breakpoint: "
2554  "unique_id = %" PRIu32 ", address = %" TARGET_PRIxADDR
2555  ", asid = %" PRIx32 ", linked = %d",
2556  bp->unique_id, bp->address, bp->asid, bp->linked_brp);
2557  return DBG_REASON_UNDEFINED;
2558  }
2559  return DBG_REASON_BREAKPOINT;
2560  }
2561  }
2562  return DBG_REASON_WATCHPOINT;
2563 }
2567 static int set_debug_reason(struct target *target, enum riscv_halt_reason halt_reason)
2568 {
2569  RISCV_INFO(r);
2570  r->trigger_hit = -1;
2571  r->need_single_step = false;
2572  switch (halt_reason) {
2573  case RISCV_HALT_EBREAK:
2575  break;
2576  case RISCV_HALT_TRIGGER:
2578  if (riscv_trigger_detect_hit_bits(target, &r->trigger_hit,
2579  &r->need_single_step) != ERROR_OK)
2580  return ERROR_FAIL;
2581  // FIXME: handle multiple hit bits
2582  if (r->trigger_hit != RISCV_TRIGGER_HIT_NOT_FOUND) {
2583  /* We scan for breakpoints first. If no breakpoints are found we still
2584  * assume that debug reason is DBG_REASON_BREAKPOINT, unless
2585  * there is a watchpoint match - This is to take
2586  * ETrigger/ITrigger/ICount into account
2587  */
2589  "Active hit bit is detected, trying to find trigger owner.");
2590  for (struct breakpoint *bp = target->breakpoints; bp; bp = bp->next) {
2591  if (bp->unique_id == r->trigger_hit) {
2594  "Breakpoint with unique_id = %" PRIu32 " owns the trigger.",
2595  bp->unique_id);
2596  }
2597  }
2599  // by default we report all triggers as breakpoints
2601  for (struct watchpoint *wp = target->watchpoints; wp; wp = wp->next) {
2602  if (wp->unique_id == r->trigger_hit) {
2605  "Watchpoint with unique_id = %" PRIu32 " owns the trigger.",
2606  wp->unique_id);
2607  }
2608  }
2609  }
2610  } else {
2612  "No trigger hit found, deriving debug reason without it.");
2613  riscv_reg_t dpc;
2614  if (riscv_reg_get(target, &dpc, GDB_REGNO_DPC) != ERROR_OK)
2615  return ERROR_FAIL;
2616  /* Here we don't have the hit bit set (likely, HW does not support it).
2617  * We are trying to guess the state. But here comes the problem:
2618  * if we have etrigger/itrigger/icount raised - we can't really
2619  * distinguish it from the breakpoint or watchpoint. There is not
2620  * much we can do here, except for checking current PC against pending
2621  * breakpoints and hope for the best)
2622  */
2624  }
2625  break;
2626  case RISCV_HALT_INTERRUPT:
2627  case RISCV_HALT_GROUP:
2629  break;
2630  case RISCV_HALT_SINGLESTEP:
2632  break;
2633  case RISCV_HALT_UNKNOWN:
2635  break;
2636  case RISCV_HALT_ERROR:
2637  return ERROR_FAIL;
2638  }
2639  LOG_TARGET_DEBUG(target, "debug_reason=%d", target->debug_reason);
2640 
2641  return ERROR_OK;
2642 }
2643 
2644 static int halt_prep(struct target *target)
2645 {
2646  RISCV_INFO(r);
2647 
2648  LOG_TARGET_DEBUG(target, "prep hart, debug_reason=%d", target->debug_reason);
2649  r->prepped = false;
2650  if (target->state == TARGET_HALTED) {
2651  LOG_TARGET_DEBUG(target, "Hart is already halted.");
2652  } else if (target->state == TARGET_UNAVAILABLE) {
2653  LOG_TARGET_DEBUG(target, "Hart is unavailable.");
2654  } else {
2655  if (r->halt_prep(target) != ERROR_OK)
2656  return ERROR_FAIL;
2657  r->prepped = true;
2658  }
2659 
2660  return ERROR_OK;
2661 }
2662 
2664 {
2665  RISCV_INFO(r);
2666 
2667  enum riscv_hart_state state;
2669  return ERROR_FAIL;
2670  if (state == RISCV_STATE_HALTED) {
2671  LOG_TARGET_DEBUG(target, "Hart is already halted.");
2672  if (target->state != TARGET_HALTED) {
2674  enum riscv_halt_reason halt_reason = riscv_halt_reason(target);
2675  if (set_debug_reason(target, halt_reason) != ERROR_OK)
2676  return ERROR_FAIL;
2677  }
2678  } else {
2679  // Safety check:
2681  LOG_TARGET_INFO(target, "BUG: Registers should not be dirty while "
2682  "the target is not halted!");
2683 
2685 
2686  if (r->halt_go(target) != ERROR_OK)
2687  return ERROR_FAIL;
2688  }
2689 
2690  return ERROR_OK;
2691 }
2692 
2693 static int halt_go(struct target *target)
2694 {
2695  RISCV_INFO(r);
2696  int result;
2697  if (!r->get_hart_state) {
2698  struct target_type *tt = get_target_type(target);
2699  if (!tt)
2700  return ERROR_FAIL;
2701  result = tt->halt(target);
2702  } else {
2703  result = riscv_halt_go_all_harts(target);
2704  }
2707 
2708  return result;
2709 }
2710 
2711 static int halt_finish(struct target *target)
2712 {
2714 }
2715 
2717 {
2718  RISCV_INFO(r);
2719 
2720  if (!r->get_hart_state) {
2721  struct target_type *tt = get_target_type(target);
2722  if (!tt)
2723  return ERROR_FAIL;
2724  return tt->halt(target);
2725  }
2726 
2727  LOG_TARGET_DEBUG(target, "halting all harts");
2728 
2729  int result = ERROR_OK;
2730  if (target->smp) {
2731  struct target_list *tlist;
2733  struct target *t = tlist->target;
2734  if (halt_prep(t) != ERROR_OK)
2735  result = ERROR_FAIL;
2736  }
2737 
2739  struct target *t = tlist->target;
2740  struct riscv_info *i = riscv_info(t);
2741  if (i->prepped) {
2742  if (halt_go(t) != ERROR_OK)
2743  result = ERROR_FAIL;
2744  }
2745  }
2746 
2748  struct target *t = tlist->target;
2749  if (halt_finish(t) != ERROR_OK)
2750  return ERROR_FAIL;
2751  }
2752 
2753  } else {
2754  if (halt_prep(target) != ERROR_OK)
2755  result = ERROR_FAIL;
2756  if (halt_go(target) != ERROR_OK)
2757  result = ERROR_FAIL;
2758  if (halt_finish(target) != ERROR_OK)
2759  return ERROR_FAIL;
2760  }
2761 
2762  return result;
2763 }
2764 
2765 static int riscv_assert_reset(struct target *target)
2766 {
2767  LOG_TARGET_DEBUG(target, "");
2768  struct target_type *tt = get_target_type(target);
2769  if (!tt)
2770  return ERROR_FAIL;
2771 
2773  LOG_TARGET_INFO(target, "Discarding values of dirty registers.");
2774 
2776  return tt->assert_reset(target);
2777 }
2778 
2780 {
2781  LOG_TARGET_DEBUG(target, "");
2782  struct target_type *tt = get_target_type(target);
2783  if (!tt)
2784  return ERROR_FAIL;
2785  return tt->deassert_reset(target);
2786 }
2787 
2788 /* "wp_is_set" array must have at least "r->trigger_count" items. */
2789 static int disable_watchpoints(struct target *target, bool *wp_is_set)
2790 {
2791  RISCV_INFO(r);
2792  LOG_TARGET_DEBUG(target, "Disabling triggers.");
2793 
2794  /* TODO: The algorithm is flawed and may result in a situation described in
2795  * https://github.com/riscv-collab/riscv-openocd/issues/1108
2796  */
2797  memset(wp_is_set, false, r->trigger_count);
2799  int i = 0;
2800  while (watchpoint) {
2801  LOG_TARGET_DEBUG(target, "Watchpoint %" PRIu32 ": set=%s",
2803  wp_is_set[i] ? "true" : "false");
2804  wp_is_set[i] = watchpoint->is_set;
2805  if (watchpoint->is_set) {
2807  return ERROR_FAIL;
2808  }
2810  i++;
2811  }
2812 
2813  return ERROR_OK;
2814 }
2815 
2816 static int enable_watchpoints(struct target *target, bool *wp_is_set)
2817 {
2819  int i = 0;
2820  while (watchpoint) {
2821  LOG_TARGET_DEBUG(target, "Watchpoint %" PRIu32
2822  ": %s to be re-enabled.", watchpoint->unique_id,
2823  wp_is_set[i] ? "needs " : "does not need");
2824  if (wp_is_set[i]) {
2826  return ERROR_FAIL;
2827  }
2829  i++;
2830  }
2831 
2832  return ERROR_OK;
2833 }
2834 
2838 static int resume_prep(struct target *target, bool current,
2839  target_addr_t address, bool handle_breakpoints, bool debug_execution)
2840 {
2841  assert(target->state == TARGET_HALTED);
2842  RISCV_INFO(r);
2843 
2844  if (!current && riscv_reg_set(target, GDB_REGNO_PC, address) != ERROR_OK)
2845  return ERROR_FAIL;
2846 
2847  if (handle_breakpoints) {
2848  /* To be able to run off a trigger, we perform a step operation and then
2849  * resume. If handle_breakpoints is true then step temporarily disables
2850  * pending breakpoints so we can safely perform the step.
2851  *
2852  * Two cases where single step is needed before resuming:
2853  * 1. ebreak used in software breakpoint;
2854  * 2. a trigger that is taken just before the instruction that triggered it is retired.
2855  */
2858  && r->need_single_step)) {
2859  if (old_or_new_riscv_step_impl(target, current, address, handle_breakpoints,
2860  false /* callbacks are not called */) != ERROR_OK)
2861  return ERROR_FAIL;
2862  }
2863  }
2864 
2865  if (r->get_hart_state) {
2866  if (r->resume_prep(target) != ERROR_OK)
2867  return ERROR_FAIL;
2868  }
2869 
2870  LOG_TARGET_DEBUG(target, "Mark as prepped.");
2871  r->prepped = true;
2872 
2873  return ERROR_OK;
2874 }
2875 
2880 static int resume_go(struct target *target, bool current,
2881  target_addr_t address, bool handle_breakpoints, bool debug_execution)
2882 {
2883  assert(target->state == TARGET_HALTED);
2884  RISCV_INFO(r);
2885  int result;
2886  if (!r->get_hart_state) {
2887  struct target_type *tt = get_target_type(target);
2888  if (!tt)
2889  return ERROR_FAIL;
2890  result = tt->resume(target, current, address, handle_breakpoints,
2891  debug_execution);
2892  } else {
2894  }
2895 
2896  return result;
2897 }
2898 
2899 static int resume_finish(struct target *target, bool debug_execution)
2900 {
2901  assert(target->state == TARGET_HALTED);
2903  /* If this happens, it means there is a bug in the previous
2904  * register-flushing algorithm: not all registers were flushed
2905  * back to the target in preparation for the resume.*/
2907  "BUG: registers should have been flushed by this point.");
2908  }
2909 
2911 
2912  target->state = debug_execution ? TARGET_DEBUG_RUNNING : TARGET_RUNNING;
2915  debug_execution ? TARGET_EVENT_DEBUG_RESUMED : TARGET_EVENT_RESUMED);
2916 }
2917 
2922 static int riscv_resume(struct target *target,
2923  bool current,
2925  bool handle_breakpoints,
2926  bool debug_execution,
2927  bool single_hart)
2928 {
2929  int result = ERROR_OK;
2930 
2931  struct list_head *targets;
2932 
2933  OOCD_LIST_HEAD(single_target_list);
2934  struct target_list single_target_entry = {
2935  .lh = {NULL, NULL},
2936  .target = target
2937  };
2938 
2939  if (target->smp && !single_hart) {
2940  targets = target->smp_targets;
2941  } else {
2942  /* Make a list that just contains a single target, so we can
2943  * share code below. */
2944  list_add(&single_target_entry.lh, &single_target_list);
2945  targets = &single_target_list;
2946  }
2947 
2948  LOG_TARGET_DEBUG(target, "current=%s, address=0x%"
2949  TARGET_PRIxADDR ", handle_breakpoints=%s, debug_exec=%s",
2950  current ? "true" : "false",
2951  address,
2952  handle_breakpoints ? "true" : "false",
2953  debug_execution ? "true" : "false");
2954 
2955  struct target_list *tlist;
2957  struct target *t = tlist->target;
2958  LOG_TARGET_DEBUG(t, "target->state=%s", target_state_name(t));
2959  if (t->state != TARGET_HALTED)
2960  LOG_TARGET_DEBUG(t, "skipping this target: target not halted");
2961  else if (resume_prep(t, current, address, handle_breakpoints,
2962  debug_execution) != ERROR_OK)
2963  result = ERROR_FAIL;
2964  }
2965 
2967  struct target *t = tlist->target;
2968  struct riscv_info *i = riscv_info(t);
2969  if (i->prepped) {
2970  if (resume_go(t, current, address, handle_breakpoints,
2971  debug_execution) != ERROR_OK)
2972  result = ERROR_FAIL;
2973  }
2974  }
2975 
2977  struct target *t = tlist->target;
2978  if (t->state == TARGET_HALTED) {
2979  if (resume_finish(t, debug_execution) != ERROR_OK)
2980  result = ERROR_FAIL;
2981  }
2982  }
2983 
2984  return result;
2985 }
2986 
2987 static int riscv_target_resume(struct target *target, bool current,
2988  target_addr_t address, bool handle_breakpoints, bool debug_execution)
2989 {
2990  if (target->state != TARGET_HALTED) {
2991  LOG_TARGET_ERROR(target, "Not halted.");
2992  return ERROR_TARGET_NOT_HALTED;
2993  }
2994  return riscv_resume(target, current, address, handle_breakpoints,
2995  debug_execution, false);
2996 }
2997 
2998 static int riscv_effective_privilege_mode(struct target *target, int *v_mode, int *effective_mode)
2999 {
3000  riscv_reg_t priv;
3002  LOG_TARGET_ERROR(target, "Failed to read priv register.");
3003  return ERROR_FAIL;
3004  }
3005  *v_mode = get_field(priv, VIRT_PRIV_V);
3006 
3007  riscv_reg_t mstatus;
3008  if (riscv_reg_get(target, &mstatus, GDB_REGNO_MSTATUS) != ERROR_OK) {
3009  LOG_TARGET_ERROR(target, "Failed to read mstatus register.");
3010  return ERROR_FAIL;
3011  }
3012 
3013  if (get_field(mstatus, MSTATUS_MPRV))
3014  *effective_mode = get_field(mstatus, MSTATUS_MPP);
3015  else
3016  *effective_mode = get_field(priv, VIRT_PRIV_PRV);
3017 
3018  LOG_TARGET_DEBUG(target, "Effective mode=%d; v=%d", *effective_mode, *v_mode);
3019 
3020  return ERROR_OK;
3021 }
3022 
3023 static int riscv_mmu(struct target *target, bool *enabled)
3024 {
3025  *enabled = false;
3026 
3028  return ERROR_OK;
3029 
3030  /* Don't use MMU in explicit or effective M (machine) mode */
3031  riscv_reg_t priv;
3033  LOG_TARGET_ERROR(target, "Failed to read priv register.");
3034  return ERROR_FAIL;
3035  }
3036 
3037  int effective_mode;
3038  int v_mode;
3039  if (riscv_effective_privilege_mode(target, &v_mode, &effective_mode) != ERROR_OK)
3040  return ERROR_FAIL;
3041 
3042  unsigned int xlen = riscv_xlen(target);
3043 
3044  if (v_mode) {
3045  /* In VU or VS mode, MMU is considered enabled when
3046  * either hgatp or vsatp mode is not OFF */
3047  riscv_reg_t vsatp;
3048  if (riscv_reg_get(target, &vsatp, GDB_REGNO_VSATP) != ERROR_OK) {
3049  LOG_TARGET_ERROR(target, "Failed to read vsatp register; priv=0x%" PRIx64,
3050  priv);
3051  return ERROR_FAIL;
3052  }
3053  /* vsatp is identical to satp, so we can use the satp macros. */
3054  if (get_field(vsatp, RISCV_SATP_MODE(xlen)) != SATP_MODE_OFF) {
3055  LOG_TARGET_DEBUG(target, "VS-stage translation is enabled.");
3056  *enabled = true;
3057  return ERROR_OK;
3058  }
3059 
3060  riscv_reg_t hgatp;
3061  if (riscv_reg_get(target, &hgatp, GDB_REGNO_HGATP) != ERROR_OK) {
3062  LOG_TARGET_ERROR(target, "Failed to read hgatp register; priv=0x%" PRIx64,
3063  priv);
3064  return ERROR_FAIL;
3065  }
3066  if (get_field(hgatp, RISCV_HGATP_MODE(xlen)) != HGATP_MODE_OFF) {
3067  LOG_TARGET_DEBUG(target, "G-stage address translation is enabled.");
3068  *enabled = true;
3069  } else {
3070  LOG_TARGET_DEBUG(target, "No V-mode address translation enabled.");
3071  }
3072 
3073  return ERROR_OK;
3074  }
3075 
3076  /* Don't use MMU in explicit or effective M (machine) mode */
3077  if (effective_mode == PRV_M) {
3078  LOG_TARGET_DEBUG(target, "SATP/MMU ignored in Machine mode.");
3079  return ERROR_OK;
3080  }
3081 
3082  riscv_reg_t satp;
3083  if (riscv_reg_get(target, &satp, GDB_REGNO_SATP) != ERROR_OK) {
3084  LOG_TARGET_DEBUG(target, "Couldn't read SATP.");
3085  /* If we can't read SATP, then there must not be an MMU. */
3086  return ERROR_OK;
3087  }
3088 
3089  if (get_field(satp, RISCV_SATP_MODE(xlen)) == SATP_MODE_OFF) {
3090  LOG_TARGET_DEBUG(target, "MMU is disabled.");
3091  } else {
3092  LOG_TARGET_DEBUG(target, "MMU is enabled.");
3093  *enabled = true;
3094  }
3095 
3096  return ERROR_OK;
3097 }
3098 
3099 /* Translate address from virtual to physical, using info and ppn.
3100  * If extra_info is non-NULL, then translate page table accesses for the primary
3101  * translation using extra_info and extra_ppn. */
3103  const virt2phys_info_t *info, target_addr_t ppn,
3104  const virt2phys_info_t *extra_info, target_addr_t extra_ppn,
3105  target_addr_t virtual, target_addr_t *physical)
3106 {
3107  RISCV_INFO(r);
3108  unsigned int xlen = riscv_xlen(target);
3109 
3110  LOG_TARGET_DEBUG(target, "mode=%s; ppn=0x%" TARGET_PRIxADDR "; virtual=0x%" TARGET_PRIxADDR,
3111  info->name, ppn, virtual);
3112 
3113  /* verify bits xlen-1:va_bits-1 are all equal */
3114  assert(xlen >= info->va_bits);
3115  target_addr_t mask = ((target_addr_t)1 << (xlen - (info->va_bits - 1))) - 1;
3116  target_addr_t masked_msbs = (virtual >> (info->va_bits - 1)) & mask;
3117  if (masked_msbs != 0 && masked_msbs != mask) {
3118  LOG_TARGET_ERROR(target, "Virtual address 0x%" TARGET_PRIxADDR " is not sign-extended "
3119  "for %s mode.", virtual, info->name);
3120  return ERROR_FAIL;
3121  }
3122 
3123  uint64_t pte = 0;
3124  target_addr_t table_address = ppn << RISCV_PGSHIFT;
3125  int i = info->level - 1;
3126  while (i >= 0) {
3127  uint64_t vpn = virtual >> info->vpn_shift[i];
3128  vpn &= info->vpn_mask[i];
3129  target_addr_t pte_address = table_address + (vpn << info->pte_shift);
3130 
3131  if (extra_info) {
3132  /* Perform extra stage translation. */
3133  if (riscv_address_translate(target, extra_info, extra_ppn,
3134  NULL, 0, pte_address, &pte_address) != ERROR_OK)
3135  return ERROR_FAIL;
3136  }
3137 
3138  uint8_t buffer[8];
3139  assert(info->pte_shift <= 3);
3140  const struct riscv_mem_access_args args = {
3141  .address = pte_address,
3142  .read_buffer = buffer,
3143  .size = 4,
3144  .increment = 4,
3145  .count = (1 << info->pte_shift) / 4,
3146  };
3147  int retval = r->access_memory(target, args);
3148  if (retval != ERROR_OK)
3149  return ERROR_FAIL;
3150 
3151  if (info->pte_shift == 2)
3152  pte = buf_get_u32(buffer, 0, 32);
3153  else
3154  pte = buf_get_u64(buffer, 0, 64);
3155 
3156  LOG_TARGET_DEBUG(target, "i=%d; PTE @0x%" TARGET_PRIxADDR " = 0x%" PRIx64, i,
3157  pte_address, pte);
3158 
3159  if (!(pte & PTE_V) || (!(pte & PTE_R) && (pte & PTE_W))) {
3160  LOG_TARGET_ERROR(target, "invalid PTE @0x%" TARGET_PRIxADDR ": 0x%" PRIx64
3161  "; mode=%s; i=%d", pte_address, pte, info->name, i);
3162  return ERROR_FAIL;
3163  }
3164 
3165  if ((pte & PTE_R) || (pte & PTE_W) || (pte & PTE_X)) /* Found leaf PTE. */
3166  break;
3167 
3168  i--;
3169  if (i < 0)
3170  break;
3171  ppn = pte >> PTE_PPN_SHIFT;
3172  table_address = ppn << RISCV_PGSHIFT;
3173  }
3174 
3175  if (i < 0) {
3176  LOG_TARGET_ERROR(target, "Couldn't find the PTE.");
3177  return ERROR_FAIL;
3178  }
3179 
3180  /* Make sure to clear out the high bits that may be set. */
3181  *physical = virtual & (((target_addr_t)1 << info->va_bits) - 1);
3182 
3183  while (i < info->level) {
3184  ppn = pte >> info->pte_ppn_shift[i];
3185  ppn &= info->pte_ppn_mask[i];
3186  *physical &= ~(((target_addr_t)info->pa_ppn_mask[i]) <<
3187  info->pa_ppn_shift[i]);
3188  *physical |= (ppn << info->pa_ppn_shift[i]);
3189  i++;
3190  }
3191  LOG_TARGET_DEBUG(target, "mode=%s; 0x%" TARGET_PRIxADDR " -> 0x%" TARGET_PRIxADDR,
3192  info->name, virtual, *physical);
3193  return ERROR_OK;
3194 }
3195 
3196 /* Virtual to physical translation for hypervisor mode. */
3197 static int riscv_virt2phys_v(struct target *target, target_addr_t virtual, target_addr_t *physical)
3198 {
3199  riscv_reg_t vsatp;
3200  if (riscv_reg_get(target, &vsatp, GDB_REGNO_VSATP) != ERROR_OK) {
3201  LOG_TARGET_ERROR(target, "Failed to read vsatp register.");
3202  return ERROR_FAIL;
3203  }
3204  /* vsatp is identical to satp, so we can use the satp macros. */
3205  unsigned int xlen = riscv_xlen(target);
3206  int vsatp_mode = get_field(vsatp, RISCV_SATP_MODE(xlen));
3207  LOG_TARGET_DEBUG(target, "VS-stage translation mode: %d", vsatp_mode);
3208  riscv_reg_t hgatp;
3209  if (riscv_reg_get(target, &hgatp, GDB_REGNO_HGATP) != ERROR_OK) {
3210  LOG_TARGET_ERROR(target, "Failed to read hgatp register.");
3211  return ERROR_FAIL;
3212  }
3213  int hgatp_mode = get_field(hgatp, RISCV_HGATP_MODE(xlen));
3214  LOG_TARGET_DEBUG(target, "G-stage translation mode: %d", hgatp_mode);
3215 
3216  const virt2phys_info_t *vsatp_info;
3217  /* VS-stage address translation. */
3218  switch (vsatp_mode) {
3219  case SATP_MODE_SV32:
3220  vsatp_info = &sv32;
3221  break;
3222  case SATP_MODE_SV39:
3223  vsatp_info = &sv39;
3224  break;
3225  case SATP_MODE_SV48:
3226  vsatp_info = &sv48;
3227  break;
3228  case SATP_MODE_SV57:
3229  vsatp_info = &sv57;
3230  break;
3231  case SATP_MODE_OFF:
3232  vsatp_info = NULL;
3233  LOG_TARGET_DEBUG(target, "vsatp mode is %d. No VS-stage translation. (vsatp: 0x%" PRIx64 ")",
3234  vsatp_mode, vsatp);
3235  break;
3236  default:
3238  "vsatp mode %d is not supported. (vsatp: 0x%" PRIx64 ")",
3239  vsatp_mode, vsatp);
3240  return ERROR_FAIL;
3241  }
3242 
3243  const virt2phys_info_t *hgatp_info;
3244  /* G-stage address translation. */
3245  switch (hgatp_mode) {
3246  case HGATP_MODE_SV32X4:
3247  hgatp_info = &sv32x4;
3248  break;
3249  case HGATP_MODE_SV39X4:
3250  hgatp_info = &sv39x4;
3251  break;
3252  case HGATP_MODE_SV48X4:
3253  hgatp_info = &sv48x4;
3254  break;
3255  case HGATP_MODE_SV57X4:
3256  hgatp_info = &sv57x4;
3257  break;
3258  case HGATP_MODE_OFF:
3259  hgatp_info = NULL;
3260  LOG_TARGET_DEBUG(target, "hgatp mode is %d. No G-stage translation. (hgatp: 0x%" PRIx64 ")",
3261  hgatp_mode, hgatp);
3262  break;
3263  default:
3265  "hgatp mode %d is not supported. (hgatp: 0x%" PRIx64 ")",
3266  hgatp_mode, hgatp);
3267  return ERROR_FAIL;
3268  }
3269 
3270  /* For any virtual memory access, the original virtual address is
3271  * converted in the first stage by VS-level address translation,
3272  * as controlled by the vsatp register, into a guest physical
3273  * address. */
3274  target_addr_t guest_physical;
3275  if (vsatp_info) {
3276  /* When V=1, memory accesses that would normally bypass
3277  * address translation are subject to G- stage address
3278  * translation alone. This includes memory accesses made
3279  * in support of VS-stage address translation, such as
3280  * reads and writes of VS-level page tables. */
3281 
3283  vsatp_info, get_field(vsatp, RISCV_SATP_PPN(xlen)),
3284  hgatp_info, get_field(hgatp, RISCV_SATP_PPN(xlen)),
3285  virtual, &guest_physical) != ERROR_OK)
3286  return ERROR_FAIL;
3287  } else {
3288  guest_physical = virtual;
3289  }
3290 
3291  /* The guest physical address is then converted in the second
3292  * stage by guest physical address translation, as controlled by
3293  * the hgatp register, into a supervisor physical address. */
3294  if (hgatp_info) {
3296  hgatp_info, get_field(hgatp, RISCV_HGATP_PPN(xlen)),
3297  NULL, 0,
3298  guest_physical, physical) != ERROR_OK)
3299  return ERROR_FAIL;
3300  } else {
3301  *physical = guest_physical;
3302  }
3303 
3304  return ERROR_OK;
3305 }
3306 
3307 static int riscv_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
3308 {
3309  bool enabled;
3310  if (riscv_mmu(target, &enabled) != ERROR_OK)
3311  return ERROR_FAIL;
3312  if (!enabled) {
3313  *physical = virtual;
3314  LOG_TARGET_DEBUG(target, "MMU is disabled. 0x%" TARGET_PRIxADDR " -> 0x%" TARGET_PRIxADDR, virtual, *physical);
3315  return ERROR_OK;
3316  }
3317 
3318  riscv_reg_t priv;
3320  LOG_TARGET_ERROR(target, "Failed to read priv register.");
3321  return ERROR_FAIL;
3322  }
3323 
3324  if (priv & VIRT_PRIV_V)
3325  return riscv_virt2phys_v(target, virtual, physical);
3326 
3327  riscv_reg_t satp_value;
3328  if (riscv_reg_get(target, &satp_value, GDB_REGNO_SATP) != ERROR_OK) {
3329  LOG_TARGET_ERROR(target, "Failed to read SATP register.");
3330  return ERROR_FAIL;
3331  }
3332 
3333  unsigned int xlen = riscv_xlen(target);
3334  int satp_mode = get_field(satp_value, RISCV_SATP_MODE(xlen));
3335  const virt2phys_info_t *satp_info;
3336  switch (satp_mode) {
3337  case SATP_MODE_SV32:
3338  satp_info = &sv32;
3339  break;
3340  case SATP_MODE_SV39:
3341  satp_info = &sv39;
3342  break;
3343  case SATP_MODE_SV48:
3344  satp_info = &sv48;
3345  break;
3346  case SATP_MODE_SV57:
3347  satp_info = &sv57;
3348  break;
3349  case SATP_MODE_OFF:
3350  LOG_TARGET_ERROR(target, "No translation or protection."
3351  " (satp: 0x%" PRIx64 ")", satp_value);
3352  return ERROR_FAIL;
3353  default:
3354  LOG_TARGET_ERROR(target, "The translation mode is not supported."
3355  " (satp: 0x%" PRIx64 ")", satp_value);
3356  return ERROR_FAIL;
3357  }
3358 
3360  satp_info, get_field(satp_value, RISCV_SATP_PPN(xlen)),
3361  NULL, 0,
3362  virtual, physical);
3363 }
3364 
3366  uint32_t size, uint32_t count, bool is_write)
3367 {
3368  const bool is_misaligned = address % size != 0;
3369  // TODO: This assumes that size of each page is 4 KiB, which is not necessarily the case.
3370  const bool crosses_page_boundary = RISCV_PGBASE(address + size * count - 1) != RISCV_PGBASE(address);
3371  if (is_misaligned && crosses_page_boundary) {
3372  LOG_TARGET_ERROR(target, "Mis-aligned memory %s (address=0x%" TARGET_PRIxADDR ", size=%d, count=%d)"
3373  " would access an element across page boundary. This is not supported.",
3374  is_write ? "write" : "read", address, size, count);
3375  return ERROR_FAIL;
3376  }
3377  return ERROR_OK;
3378 }
3379 
3380 static int riscv_read_phys_memory(struct target *target, target_addr_t phys_address,
3381  uint32_t size, uint32_t count, uint8_t *buffer)
3382 {
3383  const struct riscv_mem_access_args args = {
3384  .address = phys_address,
3385  .read_buffer = buffer,
3386  .size = size,
3387  .count = count,
3388  .increment = size,
3389  };
3390  RISCV_INFO(r);
3391  return r->access_memory(target, args);
3392 }
3393 
3394 static int riscv_write_phys_memory(struct target *target, target_addr_t phys_address,
3395  uint32_t size, uint32_t count, const uint8_t *buffer)
3396 {
3397  const struct riscv_mem_access_args args = {
3398  .address = phys_address,
3399  .write_buffer = buffer,
3400  .size = size,
3401  .count = count,
3402  .increment = size,
3403  };
3404 
3405  RISCV_INFO(r);
3406  return r->access_memory(target, args);
3407 }
3408 
3409 static int riscv_rw_memory(struct target *target, const struct riscv_mem_access_args args)
3410 {
3411  assert(riscv_mem_access_is_valid(args));
3412 
3413  const bool is_write = riscv_mem_access_is_write(args);
3414  if (args.count == 0) {
3415  LOG_TARGET_WARNING(target, "0-length %s 0x%" TARGET_PRIxADDR,
3416  is_write ? "write to" : "read from", args.address);
3417  return ERROR_OK;
3418  }
3419 
3420  bool mmu_enabled;
3421  int result = riscv_mmu(target, &mmu_enabled);
3422  if (result != ERROR_OK)
3423  return result;
3424 
3425  RISCV_INFO(r);
3426  if (!mmu_enabled)
3427  return r->access_memory(target, args);
3428 
3429  result = check_virt_memory_access(target, args.address,
3430  args.size, args.count, is_write);
3431  if (result != ERROR_OK)
3432  return result;
3433 
3434  uint32_t current_count = 0;
3435  target_addr_t current_address = args.address;
3436  while (current_count < args.count) {
3437  target_addr_t physical_addr;
3438  result = target->type->virt2phys(target, current_address, &physical_addr);
3439  if (result != ERROR_OK) {
3440  LOG_TARGET_ERROR(target, "Address translation failed.");
3441  return result;
3442  }
3443 
3444  /* TODO: For simplicity, this algorithm assumes the worst case - the smallest possible page size,
3445  * which is 4 KiB. The algorithm can be improved to detect the real page size, and allow to use larger
3446  * memory transfers and avoid extra unnecessary virt2phys address translations. */
3447  uint32_t chunk_count = MIN(args.count - current_count,
3448  (RISCV_PGSIZE - RISCV_PGOFFSET(current_address))
3449  / args.size);
3450 
3451  struct riscv_mem_access_args current_access = args;
3452  current_access.address = physical_addr;
3453  current_access.count = chunk_count;
3454  if (is_write)
3455  current_access.write_buffer += current_count * args.size;
3456  else
3457  current_access.read_buffer += current_count * args.size;
3458 
3459  result = r->access_memory(target, current_access);
3460  if (result != ERROR_OK)
3461  return result;
3462 
3463  current_count += chunk_count;
3464  current_address += chunk_count * args.size;
3465  }
3466  return ERROR_OK;
3467 }
3468 
3470  uint32_t size, uint32_t count, uint8_t *buffer)
3471 {
3472  const struct riscv_mem_access_args args = {
3473  .address = address,
3474  .read_buffer = buffer,
3475  .size = size,
3476  .count = count,
3477  .increment = size,
3478  };
3479 
3480  return riscv_rw_memory(target, args);
3481 }
3482 
3484  uint32_t size, uint32_t count, const uint8_t *buffer)
3485 {
3486  const struct riscv_mem_access_args args = {
3487  .address = address,
3488  .write_buffer = buffer,
3489  .size = size,
3490  .count = count,
3491  .increment = size,
3492  };
3493 
3494  return riscv_rw_memory(target, args);
3495 }
3496 
3497 static const char *riscv_get_gdb_arch(const struct target *target)
3498 {
3499  switch (riscv_xlen(target)) {
3500  case 32:
3501  return "riscv:rv32";
3502  case 64:
3503  return "riscv:rv64";
3504  }
3505  LOG_TARGET_ERROR(target, "Unsupported xlen: %d", riscv_xlen(target));
3506  return NULL;
3507 }
3508 
3510  struct reg **reg_list[], int *reg_list_size,
3511  enum target_register_class reg_class, bool is_read)
3512 {
3513  LOG_TARGET_DEBUG(target, "reg_class=%d, read=%d", reg_class, is_read);
3514 
3515  if (!target->reg_cache) {
3516  LOG_TARGET_ERROR(target, "Target not initialized. Return ERROR_FAIL.");
3517  return ERROR_FAIL;
3518  }
3519 
3520  switch (reg_class) {
3521  case REG_CLASS_GENERAL:
3522  *reg_list_size = 33;
3523  break;
3524  case REG_CLASS_ALL:
3525  *reg_list_size = target->reg_cache->num_regs;
3526  break;
3527  default:
3528  LOG_TARGET_ERROR(target, "Unsupported reg_class: %d", reg_class);
3529  return ERROR_FAIL;
3530  }
3531 
3532  *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
3533  if (!*reg_list)
3534  return ERROR_FAIL;
3535 
3536  for (int i = 0; i < *reg_list_size; i++) {
3537  assert(!target->reg_cache->reg_list[i].valid ||
3538  target->reg_cache->reg_list[i].size > 0);
3539  (*reg_list)[i] = &target->reg_cache->reg_list[i];
3540  if (is_read &&
3541  target->reg_cache->reg_list[i].exist &&
3542  !target->reg_cache->reg_list[i].valid) {
3544  return ERROR_FAIL;
3545  }
3546  }
3547 
3548  return ERROR_OK;
3549 }
3550 
3552  struct reg **reg_list[], int *reg_list_size,
3553  enum target_register_class reg_class)
3554 {
3555  return riscv_get_gdb_reg_list_internal(target, reg_list, reg_list_size,
3556  reg_class, false);
3557 }
3558 
3560  struct reg **reg_list[], int *reg_list_size,
3561  enum target_register_class reg_class)
3562 {
3563  return riscv_get_gdb_reg_list_internal(target, reg_list, reg_list_size,
3564  reg_class, true);
3565 }
3566 
3567 static int riscv_arch_state(struct target *target)
3568 {
3569  assert(target->state == TARGET_HALTED);
3570  const bool semihosting_active = target->semihosting &&
3572  LOG_USER("%s halted due to %s.%s",
3575  semihosting_active ? " Semihosting is active." : "");
3576  return ERROR_OK;
3577 }
3578 
3579 /* Algorithm must end with a software breakpoint instruction. */
3580 static int riscv_run_algorithm(struct target *target, int num_mem_params,
3581  struct mem_param *mem_params, int num_reg_params,
3582  struct reg_param *reg_params, target_addr_t entry_point,
3583  target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
3584 {
3585  RISCV_INFO(info);
3586 
3587  if (target->state != TARGET_HALTED) {
3588  LOG_TARGET_ERROR(target, "not halted (run target algo)");
3589  return ERROR_TARGET_NOT_HALTED;
3590  }
3591 
3592  /* Write memory parameters to the target memory */
3593  for (int i = 0; i < num_mem_params; i++) {
3594  if (mem_params[i].direction == PARAM_OUT ||
3595  mem_params[i].direction == PARAM_IN_OUT) {
3596  int retval = target_write_buffer(target, mem_params[i].address, mem_params[i].size, mem_params[i].value);
3597  if (retval != ERROR_OK) {
3598  LOG_TARGET_ERROR(target, "Couldn't write input mem param into the memory, addr=0x%" TARGET_PRIxADDR
3599  " size=0x%" PRIx32, mem_params[i].address, mem_params[i].size);
3600  return retval;
3601  }
3602  }
3603  }
3604 
3605  /* Save registers */
3606  struct reg *reg_pc = register_get_by_name(target->reg_cache, "pc", true);
3607  if (!reg_pc || reg_pc->type->get(reg_pc) != ERROR_OK)
3608  return ERROR_FAIL;
3609  uint64_t saved_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
3610  LOG_TARGET_DEBUG(target, "saved_pc=0x%" PRIx64, saved_pc);
3611 
3612  uint64_t saved_regs[32];
3613  for (int i = 0; i < num_reg_params; i++) {
3614  LOG_TARGET_DEBUG(target, "save %s", reg_params[i].reg_name);
3615  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
3616  if (!r) {
3617  LOG_TARGET_ERROR(target, "Couldn't find register named '%s'", reg_params[i].reg_name);
3618  return ERROR_FAIL;
3619  }
3620 
3621  if (r->size != reg_params[i].size) {
3622  LOG_TARGET_ERROR(target, "Register %s is %d bits instead of %d bits.",
3623  reg_params[i].reg_name, r->size, reg_params[i].size);
3624  return ERROR_FAIL;
3625  }
3626 
3627  if (r->number > GDB_REGNO_XPR31) {
3628  LOG_TARGET_ERROR(target, "Only GPRs can be use as argument registers.");
3629  return ERROR_FAIL;
3630  }
3631 
3632  if (r->type->get(r) != ERROR_OK)
3633  return ERROR_FAIL;
3634  saved_regs[r->number] = buf_get_u64(r->value, 0, r->size);
3635 
3636  if (reg_params[i].direction == PARAM_OUT || reg_params[i].direction == PARAM_IN_OUT) {
3637  if (r->type->set(r, reg_params[i].value) != ERROR_OK)
3638  return ERROR_FAIL;
3639  }
3640  }
3641 
3642  /* Disable Interrupts before attempting to run the algorithm. */
3643  riscv_reg_t current_mstatus;
3644  if (riscv_interrupts_disable(target, &current_mstatus) != ERROR_OK)
3645  return ERROR_FAIL;
3646 
3647  /* Run algorithm */
3648  LOG_TARGET_DEBUG(target, "resume at 0x%" TARGET_PRIxADDR, entry_point);
3649  if (riscv_resume(target, false, entry_point, false, true, true) != ERROR_OK)
3650  return ERROR_FAIL;
3651 
3652  int64_t start = timeval_ms();
3653  while (target->state != TARGET_HALTED) {
3654  LOG_TARGET_DEBUG(target, "poll()");
3655  int64_t now = timeval_ms();
3656  if (now - start > timeout_ms) {
3657  LOG_TARGET_ERROR(target, "Algorithm timed out after %" PRId64 " ms.", now - start);
3658  riscv_halt(target);
3660  enum gdb_regno regnums[] = {
3669  GDB_REGNO_PC,
3671  };
3672  for (unsigned int i = 0; i < ARRAY_SIZE(regnums); i++) {
3673  enum gdb_regno regno = regnums[i];
3674  riscv_reg_t reg_value;
3675  if (riscv_reg_get(target, &reg_value, regno) != ERROR_OK)
3676  break;
3677 
3678  LOG_TARGET_ERROR(target, "%s = 0x%" PRIx64, riscv_reg_gdb_regno_name(target, regno), reg_value);
3679  }
3680  return ERROR_TARGET_TIMEOUT;
3681  }
3682 
3683  int result = old_or_new_riscv_poll(target);
3684  if (result != ERROR_OK)
3685  return result;
3686  }
3687 
3688  /* TODO: The current hart id might have been changed in poll(). */
3689  /* if (riscv_select_current_hart(target) != ERROR_OK)
3690  return ERROR_FAIL; */
3691 
3692  if (reg_pc->type->get(reg_pc) != ERROR_OK)
3693  return ERROR_FAIL;
3694  uint64_t final_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
3695  if (exit_point && final_pc != exit_point) {
3696  LOG_TARGET_ERROR(target, "PC ended up at 0x%" PRIx64 " instead of 0x%"
3697  TARGET_PRIxADDR, final_pc, exit_point);
3698  return ERROR_FAIL;
3699  }
3700 
3701  /* Restore Interrupts */
3702  if (riscv_interrupts_restore(target, current_mstatus) != ERROR_OK)
3703  return ERROR_FAIL;
3704 
3705  /* Restore registers */
3706  uint8_t buf[8] = { 0 };
3707  buf_set_u64(buf, 0, info->xlen, saved_pc);
3708  if (reg_pc->type->set(reg_pc, buf) != ERROR_OK)
3709  return ERROR_FAIL;
3710 
3711  for (int i = 0; i < num_reg_params; i++) {
3712  if (reg_params[i].direction == PARAM_IN ||
3713  reg_params[i].direction == PARAM_IN_OUT) {
3714  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
3715  if (r->type->get(r) != ERROR_OK) {
3716  LOG_TARGET_ERROR(target, "get(%s) failed", r->name);
3717  return ERROR_FAIL;
3718  }
3719  buf_cpy(r->value, reg_params[i].value, reg_params[i].size);
3720  }
3721  LOG_TARGET_DEBUG(target, "restore %s", reg_params[i].reg_name);
3722  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
3723  buf_set_u64(buf, 0, info->xlen, saved_regs[r->number]);
3724  if (r->type->set(r, buf) != ERROR_OK) {
3725  LOG_TARGET_ERROR(target, "set(%s) failed", r->name);
3726  return ERROR_FAIL;
3727  }
3728  }
3729 
3730  /* Read memory parameters from the target memory */
3731  for (int i = 0; i < num_mem_params; i++) {
3732  if (mem_params[i].direction == PARAM_IN ||
3733  mem_params[i].direction == PARAM_IN_OUT) {
3734  int retval = target_read_buffer(target, mem_params[i].address, mem_params[i].size,
3735  mem_params[i].value);
3736  if (retval != ERROR_OK) {
3737  LOG_TARGET_ERROR(target, "Couldn't read output mem param from the memory, "
3738  "addr=0x%" TARGET_PRIxADDR " size=0x%" PRIx32,
3739  mem_params[i].address, mem_params[i].size);
3740  return retval;
3741  }
3742  }
3743  }
3744 
3745  return ERROR_OK;
3746 }
3747 
3749  target_addr_t address, uint32_t count,
3750  uint32_t *checksum)
3751 {
3752  struct working_area *crc_algorithm;
3753  struct reg_param reg_params[2];
3754  int retval;
3755 
3756  LOG_TARGET_DEBUG(target, "address=0x%" TARGET_PRIxADDR "; count=0x%" PRIx32, address, count);
3757 
3758  static const uint8_t riscv32_crc_code[] = {
3759 #include "../../../contrib/loaders/checksum/riscv32_crc.inc"
3760  };
3761  static const uint8_t riscv64_crc_code[] = {
3762 #include "../../../contrib/loaders/checksum/riscv64_crc.inc"
3763  };
3764 
3765  static const uint8_t *crc_code;
3766 
3767  unsigned int xlen = riscv_xlen(target);
3768  unsigned int crc_code_size;
3769  if (xlen == 32) {
3770  crc_code = riscv32_crc_code;
3771  crc_code_size = sizeof(riscv32_crc_code);
3772  } else {
3773  crc_code = riscv64_crc_code;
3774  crc_code_size = sizeof(riscv64_crc_code);
3775  }
3776 
3777  if (count < crc_code_size * 4) {
3778  /* Don't use the algorithm for relatively small buffers. It's faster
3779  * just to read the memory. target_checksum_memory() will take care of
3780  * that if we fail. */
3781  return ERROR_FAIL;
3782  }
3783 
3784  retval = target_alloc_working_area(target, crc_code_size, &crc_algorithm);
3785  if (retval != ERROR_OK)
3786  return retval;
3787 
3788  if (crc_algorithm->address + crc_algorithm->size > address &&
3789  crc_algorithm->address < address + count) {
3790  /* Region to checksum overlaps with the work area we've been assigned.
3791  * Bail. (Would be better to manually checksum what we read there, and
3792  * use the algorithm for the rest.) */
3793  target_free_working_area(target, crc_algorithm);
3794  return ERROR_FAIL;
3795  }
3796 
3797  retval = target_write_buffer(target, crc_algorithm->address, crc_code_size,
3798  crc_code);
3799  if (retval != ERROR_OK) {
3800  LOG_TARGET_ERROR(target, "Failed to write code to " TARGET_ADDR_FMT ": %d",
3801  crc_algorithm->address, retval);
3802  target_free_working_area(target, crc_algorithm);
3803  return retval;
3804  }
3805 
3806  init_reg_param(&reg_params[0], "a0", xlen, PARAM_IN_OUT);
3807  init_reg_param(&reg_params[1], "a1", xlen, PARAM_OUT);
3808  buf_set_u64(reg_params[0].value, 0, xlen, address);
3809  buf_set_u64(reg_params[1].value, 0, xlen, count);
3810 
3811  /* 20 second timeout/megabyte */
3812  unsigned int timeout = 20000 * (1 + (count / (1024 * 1024)));
3813 
3814  retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
3815  crc_algorithm->address,
3816  0, /* Leave exit point unspecified because we don't know. */
3817  timeout, NULL);
3818 
3819  if (retval == ERROR_OK)
3820  *checksum = buf_get_u32(reg_params[0].value, 0, 32);
3821  else
3822  LOG_TARGET_ERROR(target, "Error executing RISC-V CRC algorithm.");
3823 
3824  destroy_reg_param(&reg_params[0]);
3825  destroy_reg_param(&reg_params[1]);
3826 
3827  target_free_working_area(target, crc_algorithm);
3828 
3829  LOG_TARGET_DEBUG(target, "checksum=0x%" PRIx32 ", result=%d", *checksum, retval);
3830 
3831  return retval;
3832 }
3833 
3834 /*** OpenOCD Helper Functions ***/
3835 
3840 };
3841 static int riscv_poll_hart(struct target *target, enum riscv_next_action *next_action)
3842 {
3843  RISCV_INFO(r);
3844 
3845  LOG_TARGET_DEBUG(target, "polling, target->state=%d", target->state);
3846 
3847  *next_action = RPH_NONE;
3848 
3849  enum riscv_hart_state previous_riscv_state = 0;
3850  enum target_state previous_target_state = target->state;
3851  switch (target->state) {
3852  case TARGET_UNKNOWN:
3853  /* Special case, handled further down. */
3854  previous_riscv_state = RISCV_STATE_UNAVAILABLE; /* Need to assign something. */
3855  break;
3856  case TARGET_RUNNING:
3857  previous_riscv_state = RISCV_STATE_RUNNING;
3858  break;
3859  case TARGET_HALTED:
3860  previous_riscv_state = RISCV_STATE_HALTED;
3861  break;
3862  case TARGET_RESET:
3863  previous_riscv_state = RISCV_STATE_HALTED;
3864  break;
3865  case TARGET_DEBUG_RUNNING:
3866  previous_riscv_state = RISCV_STATE_RUNNING;
3867  break;
3868  case TARGET_UNAVAILABLE:
3869  previous_riscv_state = RISCV_STATE_UNAVAILABLE;
3870  break;
3871  }
3872 
3873  /* If OpenOCD thinks we're running but this hart is halted then it's time
3874  * to raise an event. */
3875  enum riscv_hart_state state;
3877  return ERROR_FAIL;
3878 
3880  LOG_TARGET_ERROR(target, "Hart is non-existent!");
3881  return ERROR_FAIL;
3882  }
3883 
3884  if (state == RISCV_STATE_HALTED && timeval_ms() - r->last_activity > 100) {
3885  /* If we've been idle for a while, flush the register cache. Just in case
3886  * OpenOCD is going to be disconnected without shutting down cleanly. */
3888  return ERROR_FAIL;
3889  }
3890 
3891  if (target->state == TARGET_UNKNOWN || state != previous_riscv_state) {
3892  switch (state) {
3893  case RISCV_STATE_HALTED:
3894  if (previous_riscv_state == RISCV_STATE_UNAVAILABLE)
3895  LOG_TARGET_INFO(target, "became available (halted)");
3896 
3897  LOG_TARGET_DEBUG(target, " triggered a halt; previous_target_state=%d",
3898  previous_target_state);
3900  enum riscv_halt_reason halt_reason = riscv_halt_reason(target);
3901  if (set_debug_reason(target, halt_reason) != ERROR_OK)
3902  return ERROR_FAIL;
3903 
3904  if (halt_reason == RISCV_HALT_EBREAK) {
3905  int retval;
3906  /* Detect if this EBREAK is a semihosting request. If so, handle it. */
3907  switch (riscv_semihosting(target, &retval)) {
3908  case SEMIHOSTING_NONE:
3909  break;
3910  case SEMIHOSTING_WAITING:
3911  /* This hart should remain halted. */
3912  *next_action = RPH_REMAIN_HALTED;
3913  break;
3914  case SEMIHOSTING_HANDLED:
3915  /* This hart should be resumed, along with any other
3916  * harts that halted due to haltgroups. */
3917  *next_action = RPH_RESUME;
3918  return ERROR_OK;
3919  case SEMIHOSTING_ERROR:
3920  return retval;
3921  }
3922  }
3923 
3924  if (r->handle_became_halted &&
3925  r->handle_became_halted(target, previous_riscv_state) != ERROR_OK)
3926  return ERROR_FAIL;
3927 
3928  /* We shouldn't do the callbacks yet. What if
3929  * there are multiple harts that halted at the
3930  * same time? We need to set debug reason on each
3931  * of them before calling a callback, which is
3932  * going to figure out the "current thread". */
3933 
3934  r->halted_needs_event_callback = true;
3935  if (previous_target_state == TARGET_DEBUG_RUNNING)
3936  r->halted_callback_event = TARGET_EVENT_DEBUG_HALTED;
3937  else
3938  r->halted_callback_event = TARGET_EVENT_HALTED;
3939  break;
3940 
3941  case RISCV_STATE_RUNNING:
3942  if (previous_riscv_state == RISCV_STATE_UNAVAILABLE)
3943  LOG_TARGET_INFO(target, "became available (running)");
3944 
3945  LOG_TARGET_DEBUG(target, " triggered running");
3948  if (r->handle_became_running &&
3949  r->handle_became_running(target, previous_riscv_state) != ERROR_OK)
3950  return ERROR_FAIL;
3951  break;
3952 
3954  LOG_TARGET_DEBUG(target, " became unavailable");
3955  LOG_TARGET_INFO(target, "became unavailable.");
3957  if (r->handle_became_unavailable &&
3958  r->handle_became_unavailable(target, previous_riscv_state) != ERROR_OK)
3959  return ERROR_FAIL;
3960  break;
3961 
3963  LOG_TARGET_ERROR(target, "Hart is non-existent!");
3965  break;
3966  }
3967  }
3968 
3969  return ERROR_OK;
3970 }
3971 
3972 static int sample_memory(struct target *target)
3973 {
3974  RISCV_INFO(r);
3975 
3976  if (!r->sample_buf.buf || !r->sample_config.enabled)
3977  return ERROR_OK;
3978 
3979  LOG_TARGET_DEBUG(target, "buf used/size: %d/%d", r->sample_buf.used, r->sample_buf.size);
3980 
3981  uint64_t start = timeval_ms();
3983  int result = ERROR_OK;
3984  if (r->sample_memory) {
3985  result = r->sample_memory(target, &r->sample_buf, &r->sample_config,
3987  if (result != ERROR_NOT_IMPLEMENTED)
3988  goto exit;
3989  }
3990 
3991  /* Default slow path. */
3993  for (unsigned int i = 0; i < ARRAY_SIZE(r->sample_config.bucket); i++) {
3994  if (r->sample_config.bucket[i].enabled &&
3995  r->sample_buf.used + 1 + r->sample_config.bucket[i].size_bytes < r->sample_buf.size) {
3997  r->sample_buf.buf[r->sample_buf.used] = i;
3998  result = riscv_read_phys_memory(target,
3999  r->sample_config.bucket[i].address,
4000  r->sample_config.bucket[i].size_bytes, 1,
4001  r->sample_buf.buf + r->sample_buf.used + 1);
4002  if (result == ERROR_OK)
4003  r->sample_buf.used += 1 + r->sample_config.bucket[i].size_bytes;
4004  else
4005  goto exit;
4006  }
4007  }
4008  }
4009 
4010 exit:
4012  if (result != ERROR_OK) {
4013  LOG_TARGET_INFO(target, "Turning off memory sampling because it failed.");
4014  r->sample_config.enabled = false;
4015  }
4016  return result;
4017 }
4018 
4019 /*** OpenOCD Interface ***/
4021 {
4022  LOG_TARGET_DEBUG(target, "Polling all harts.");
4023 
4024  struct riscv_info *i = riscv_info(target);
4025 
4026  struct list_head *targets;
4027 
4028  OOCD_LIST_HEAD(single_target_list);
4029  struct target_list single_target_entry = {
4030  .lh = {NULL, NULL},
4031  .target = target
4032  };
4033 
4034  if (target->smp) {
4035  targets = target->smp_targets;
4036  } else {
4037  /* Make a list that just contains a single target, so we can
4038  * share code below. */
4039  list_add(&single_target_entry.lh, &single_target_list);
4040  targets = &single_target_list;
4041  }
4042 
4043  unsigned int should_remain_halted = 0;
4044  unsigned int should_resume = 0;
4045  unsigned int halted = 0;
4046  unsigned int running = 0;
4047  unsigned int cause_groups = 0;
4048  struct target_list *entry;
4049  foreach_smp_target(entry, targets) {
4050  struct target *t = entry->target;
4051  struct riscv_info *info = riscv_info(t);
4052 
4053  /* Clear here just in case there were errors and we never got to
4054  * check this flag further down. */
4055  info->halted_needs_event_callback = false;
4056 
4057  if (!target_was_examined(t))
4058  continue;
4059 
4060  enum riscv_next_action next_action;
4061  if (riscv_poll_hart(t, &next_action) != ERROR_OK)
4062  return ERROR_FAIL;
4063 
4064  switch (next_action) {
4065  case RPH_NONE:
4066  if (t->state == TARGET_HALTED)
4067  halted++;
4068  if (t->state == TARGET_RUNNING ||
4070  running++;
4071  break;
4072  case RPH_REMAIN_HALTED:
4073  should_remain_halted++;
4074  break;
4075  case RPH_RESUME:
4076  should_resume++;
4077  break;
4078  }
4079  }
4080 
4081  LOG_TARGET_DEBUG(target, "should_remain_halted=%d, should_resume=%d",
4082  should_remain_halted, should_resume);
4083  if (should_remain_halted && should_resume) {
4084  LOG_TARGET_WARNING(target, "%d harts should remain halted, and %d should resume.",
4085  should_remain_halted, should_resume);
4086  }
4087  if (should_remain_halted) {
4088  LOG_TARGET_DEBUG(target, "halt all; should_remain_halted=%d",
4089  should_remain_halted);
4090  riscv_halt(target);
4091  } else if (should_resume) {
4092  LOG_TARGET_DEBUG(target, "resume all");
4093  riscv_resume(target, true, 0, 0, 0, false);
4094  } else if (halted && running) {
4095  LOG_TARGET_DEBUG(target, "SMP group is in inconsistent state: %u halted, %u running",
4096  halted, running);
4097 
4098  /* The SMP group is in an inconsistent state - some harts in the group have halted
4099  * whereas others are running. The reasons for that (and corresponding
4100  * OpenOCD actions) could be:
4101  * 1) The targets are in the process of halting due to halt groups
4102  * but not all of them halted --> poll again so that the halt reason of every
4103  * hart can be accurately determined (e.g. semihosting).
4104  * 2) The targets do not support halt groups --> OpenOCD must halt
4105  * the remaining harts by a standard halt request.
4106  * 3) The hart states got out of sync for some other unknown reason (problem?). -->
4107  * Same as previous - try to halt the harts by a standard halt request
4108  * to get them back in sync. */
4109 
4110  /* Detect if the harts are just in the process of halting due to a halt group */
4111  foreach_smp_target(entry, targets)
4112  {
4113  struct target *t = entry->target;
4114  if (t->state == TARGET_HALTED) {
4115  riscv_reg_t dcsr;
4116  if (riscv_reg_get(t, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
4117  return ERROR_FAIL;
4119  cause_groups++;
4120  else
4121  /* This hart has halted due to something else than a halt group.
4122  * Don't continue checking the rest - exit early. */
4123  break;
4124  }
4125  }
4126  /* Condition: halted == cause_groups
4127  *
4128  * This condition indicates a paradox where:
4129  * - All currently halted harts show CSR_DCSR_CAUSE_GROUP
4130  * - However, no individual hart can be identified as the actual initiator of the halt condition
4131  *
4132  * Poll again so that the true halt reason can be discovered (e.g. CSR_DCSR_CAUSE_EBREAK) */
4133  if (halted == cause_groups) {
4134  LOG_TARGET_DEBUG(target, "The harts appear to just be in the process of halting due to a halt group.");
4136  /* Wait a little, then re-poll. */
4138  alive_sleep(10);
4139  LOG_TARGET_DEBUG(target, "Re-polling the state of the SMP group.");
4140  return riscv_openocd_poll(target);
4141  }
4142  /* We have already re-polled multiple times but the halt group is still inconsistent. */
4143  LOG_TARGET_DEBUG(target, "Re-polled the SMP group %d times it is still not in a consistent state.",
4145  }
4146 
4147  /* Halting the whole SMP group to bring it in sync. */
4148  LOG_TARGET_DEBUG(target, "halt all; halted=%d",
4149  halted);
4150  riscv_halt(target);
4151  } else {
4152  /* For targets that were discovered to be halted, call the
4153  * appropriate callback. */
4154  foreach_smp_target(entry, targets)
4155  {
4156  struct target *t = entry->target;
4157  struct riscv_info *info = riscv_info(t);
4158  if (info->halted_needs_event_callback) {
4159  target_call_event_callbacks(t, info->halted_callback_event);
4160  info->halted_needs_event_callback = false;
4161  }
4162  }
4163  }
4164 
4165  i->halt_group_repoll_count = 0;
4166 
4167  /* Call tick() for every hart. What happens in tick() is opaque to this
4168  * layer. The reason it's outside the previous loop is that at this point
4169  * the state of every hart has settled, so any side effects happening in
4170  * tick() won't affect the delicate poll() code. */
4171  foreach_smp_target(entry, targets) {
4172  struct target *t = entry->target;
4173  struct riscv_info *info = riscv_info(t);
4174  if (info->tick && info->tick(t) != ERROR_OK)
4175  return ERROR_FAIL;
4176  }
4177 
4178  /* Sample memory if any target is running. */
4179  foreach_smp_target(entry, targets) {
4180  struct target *t = entry->target;
4181  if (t->state == TARGET_RUNNING) {
4183  break;
4184  }
4185  }
4186 
4187  return ERROR_OK;
4188 }
4189 
4190 static int riscv_openocd_step_impl(struct target *target, bool current,
4191  target_addr_t address, bool handle_breakpoints, int handle_callbacks)
4192 {
4193  LOG_TARGET_DEBUG(target, "stepping hart");
4194 
4195  if (!current) {
4197  return ERROR_FAIL;
4198  }
4199 
4200  struct breakpoint *breakpoint = NULL;
4201  /* the front-end may request us not to handle breakpoints */
4202  if (handle_breakpoints) {
4203  if (current) {
4205  return ERROR_FAIL;
4206  }
4209  return ERROR_FAIL;
4210  }
4211 
4213  return ERROR_FAIL;
4214 
4215  RISCV_INFO(r);
4216  bool *wps_to_enable = calloc(r->trigger_count, sizeof(*wps_to_enable));
4217  if (!wps_to_enable) {
4218  LOG_ERROR("Out of memory");
4219  return ERROR_FAIL;
4220  }
4221 
4222  if (disable_watchpoints(target, wps_to_enable) != ERROR_OK) {
4223  LOG_TARGET_ERROR(target, "Failed to temporarily disable "
4224  "watchpoints before single-step.");
4225  free(wps_to_enable);
4226  return ERROR_FAIL;
4227  }
4228 
4229  bool success = true;
4230  riscv_reg_t current_mstatus;
4231  RISCV_INFO(info);
4232 
4233  if (info->isrmask_mode == RISCV_ISRMASK_STEPONLY) {
4234  /* Disable Interrupts before stepping. */
4235  if (riscv_interrupts_disable(target, &current_mstatus) != ERROR_OK) {
4236  success = false;
4237  LOG_TARGET_ERROR(target, "Unable to disable interrupts.");
4238  goto _exit;
4239  }
4240  }
4241 
4243  success = false;
4244  LOG_TARGET_ERROR(target, "Unable to step rtos hart.");
4245  }
4246 
4248  /* If this happens, it means there is a bug in the previous
4249  * register-flushing algorithm: not all registers were flushed
4250  * back to the target prior to single-step. */
4252  "BUG: registers should have been flushed by this point.");
4253  }
4254 
4256 
4257  if (info->isrmask_mode == RISCV_ISRMASK_STEPONLY)
4258  if (riscv_interrupts_restore(target, current_mstatus) != ERROR_OK) {
4259  success = false;
4260  LOG_TARGET_ERROR(target, "Unable to restore interrupts.");
4261  }
4262 
4263 _exit:
4264  if (enable_watchpoints(target, wps_to_enable) != ERROR_OK) {
4265  success = false;
4266  LOG_TARGET_ERROR(target, "Failed to re-enable watchpoints "
4267  "after single-step.");
4268  }
4269 
4270  free(wps_to_enable);
4271 
4273  success = false;
4274  LOG_TARGET_ERROR(target, "Unable to restore the disabled breakpoint.");
4275  }
4276 
4277  if (success) {
4279  if (handle_callbacks)
4281 
4284  if (handle_callbacks)
4286  }
4287 
4288  return success ? ERROR_OK : ERROR_FAIL;
4289 }
4290 
4291 int riscv_openocd_step(struct target *target, bool current,
4292  target_addr_t address, bool handle_breakpoints)
4293 {
4294  return riscv_openocd_step_impl(target, current, address, handle_breakpoints,
4295  true /* handle_callbacks */);
4296 }
4297 
4298 /* Command Handlers */
4299 COMMAND_HANDLER(riscv_set_command_timeout_sec)
4300 {
4301  if (CMD_ARGC != 1)
4303 
4304  int timeout = atoi(CMD_ARGV[0]);
4305  if (timeout <= 0) {
4306  LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
4307  return ERROR_FAIL;
4308  }
4309 
4311 
4312  return ERROR_OK;
4313 }
4314 
4315 COMMAND_HANDLER(riscv_set_reset_timeout_sec)
4316 {
4317  LOG_WARNING("The command 'riscv set_reset_timeout_sec' is deprecated! Please, use 'riscv set_command_timeout_sec'.");
4318  if (CMD_ARGC != 1)
4320 
4321  int timeout = atoi(CMD_ARGV[0]);
4322  if (timeout <= 0) {
4323  LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
4324  return ERROR_FAIL;
4325  }
4326 
4328  return ERROR_OK;
4329 }
4330 
4331 COMMAND_HANDLER(riscv_set_mem_access)
4332 {
4334  RISCV_INFO(r);
4335  int progbuf_cnt = 0;
4336  int sysbus_cnt = 0;
4337  int abstract_cnt = 0;
4338 
4339  if (CMD_ARGC < 1 || CMD_ARGC > RISCV_MEM_ACCESS_MAX_METHODS_NUM) {
4340  command_print(CMD, "Command takes 1 to %d parameters",
4343  }
4344 
4345  /* Check argument validity */
4346  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4347  if (strcmp("progbuf", CMD_ARGV[i]) == 0) {
4348  progbuf_cnt++;
4349  } else if (strcmp("sysbus", CMD_ARGV[i]) == 0) {
4350  sysbus_cnt++;
4351  } else if (strcmp("abstract", CMD_ARGV[i]) == 0) {
4352  abstract_cnt++;
4353  } else {
4354  LOG_ERROR("Unknown argument '%s'. "
4355  "Must be one of: 'progbuf', 'sysbus' or 'abstract'.", CMD_ARGV[i]);
4357  }
4358  }
4359  if (progbuf_cnt > 1 || sysbus_cnt > 1 || abstract_cnt > 1) {
4360  LOG_ERROR("Syntax error - duplicate arguments to `riscv set_mem_access`.");
4362  }
4363 
4364  /* Args are valid, store them */
4365  r->num_enabled_mem_access_methods = CMD_ARGC;
4366  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4367  if (strcmp("progbuf", CMD_ARGV[i]) == 0)
4368  r->mem_access_methods[i] = RISCV_MEM_ACCESS_PROGBUF;
4369  else if (strcmp("sysbus", CMD_ARGV[i]) == 0)
4370  r->mem_access_methods[i] = RISCV_MEM_ACCESS_SYSBUS;
4371  else if (strcmp("abstract", CMD_ARGV[i]) == 0)
4372  r->mem_access_methods[i] = RISCV_MEM_ACCESS_ABSTRACT;
4373  }
4374 
4375  /* Reset warning flags */
4376  for (size_t i = 0; i < RISCV_MEM_ACCESS_MAX_METHODS_NUM; ++i)
4377  r->mem_access_warn[i] = true;
4378 
4379  return ERROR_OK;
4380 }
4381 
4382 
4383 static bool parse_csr_address(const char *reg_address_str, unsigned int *reg_addr)
4384 {
4385  *reg_addr = -1;
4386  /* skip initial spaces */
4387  while (isspace((unsigned char)reg_address_str[0]))
4388  ++reg_address_str;
4389  /* try to detect if string starts with 0x or 0X */
4390  bool is_hex_address = strncmp(reg_address_str, "0x", 2) == 0 ||
4391  strncmp(reg_address_str, "0X", 2) == 0;
4392 
4393  unsigned int scanned_chars;
4394  if (is_hex_address) {
4395  reg_address_str += 2;
4396  if (sscanf(reg_address_str, "%x%n", reg_addr, &scanned_chars) != 1)
4397  return false;
4398  } else {
4399  /* If we are here and register address string starts with zero, this is
4400  * an indication that most likely user has an incorrect input because:
4401  * - decimal numbers typically do not start with "0"
4402  * - octals are not supported by our interface
4403  * - hexadecimal numbers should have "0x" prefix
4404  * Thus such input is rejected. */
4405  if (reg_address_str[0] == '0' && strlen(reg_address_str) > 1)
4406  return false;
4407  if (sscanf(reg_address_str, "%u%n", reg_addr, &scanned_chars) != 1)
4408  return false;
4409  }
4410  return scanned_chars == strlen(reg_address_str);
4411 }
4412 
4413 static int parse_reg_ranges_impl(struct list_head *ranges, char *args,
4414  const char *reg_type, unsigned int max_val, char ** const name_buffer)
4415 {
4416  /* For backward compatibility, allow multiple parameters within one TCL
4417  * argument, separated by ',' */
4418  for (char *arg = strtok(args, ","); arg; arg = strtok(NULL, ",")) {
4419  unsigned int low = 0;
4420  unsigned int high = 0;
4421  char *name = NULL;
4422 
4423  char *dash = strchr(arg, '-');
4424  char *equals = strchr(arg, '=');
4425 
4426  if (!dash && !equals) {
4427  /* Expecting single register number. */
4428  if (!parse_csr_address(arg, &low)) {
4429  LOG_ERROR("Failed to parse single register number from '%s'.", arg);
4431  }
4432  } else if (dash && !equals) {
4433  /* Expecting register range - two numbers separated by a dash: ##-## */
4434  *dash = '\0';
4435  if (!parse_csr_address(arg, &low)) {
4436  LOG_ERROR("Failed to parse '%s' - not a valid decimal or hexadecimal number.",
4437  arg);
4439  }
4440  const char *high_num_in = dash + 1;
4441  if (!parse_csr_address(high_num_in, &high)) {
4442  LOG_ERROR("Failed to parse '%s' - not a valid decimal or hexadecimal number.",
4443  high_num_in);
4445  }
4446  if (high < low) {
4447  LOG_ERROR("Incorrect range encountered [%u, %u].", low, high);
4448  return ERROR_FAIL;
4449  }
4450  } else if (!dash && equals) {
4451  /* Expecting single register number with textual name specified: ##=name */
4452  *equals = '\0';
4453  if (!parse_csr_address(arg, &low)) {
4454  LOG_ERROR("Failed to parse '%s' - not a valid decimal or hexadecimal number.",
4455  arg);
4457  }
4458 
4459  const char * const reg_name_in = equals + 1;
4460  const size_t reg_type_len = strlen(reg_type);
4461  /* format is: <reg_type>_<reg_name_in>\0 */
4462  *name_buffer = calloc(1, strlen(reg_name_in) + reg_type_len + 2);
4463  name = *name_buffer;
4464  if (!name) {
4465  LOG_ERROR("Out of memory");
4466  return ERROR_FAIL;
4467  }
4468  strcpy(name, reg_type);
4469  name[reg_type_len] = '_';
4470 
4471  unsigned int scanned_chars;
4472  char *scan_dst = name + strlen(reg_type) + 1;
4473  if (sscanf(reg_name_in, "%[_a-zA-Z0-9]%n", scan_dst, &scanned_chars) != 1 ||
4474  scanned_chars != strlen(reg_name_in)) {
4475  LOG_ERROR("Invalid characters in register name '%s'.", reg_name_in);
4477  }
4478  } else {
4479  LOG_ERROR("Invalid argument '%s'.", arg);
4481  }
4482 
4483  high = MAX(high, low);
4484 
4485  if (high > max_val) {
4486  LOG_ERROR("Cannot expose %s register number 0x%x, maximum allowed value is 0x%x.",
4487  reg_type, high, max_val);
4488  return ERROR_FAIL;
4489  }
4490 
4491  /* Check for overlap, name uniqueness. */
4492  range_list_t *entry;
4493  list_for_each_entry(entry, ranges, list) {
4494  if (entry->low <= high && low <= entry->high) {
4495  if (low == high)
4496  LOG_WARNING("Duplicate %s register number - "
4497  "Register %u has already been exposed previously", reg_type, low);
4498  else
4499  LOG_WARNING("Overlapping register ranges - Register range starting from %u overlaps "
4500  "with already exposed register/range at %u.", low, entry->low);
4501  }
4502 
4503  if (entry->name && name && (strcasecmp(entry->name, name) == 0)) {
4504  LOG_ERROR("Duplicate register name \"%s\" found.", name);
4505  return ERROR_FAIL;
4506  }
4507  }
4508 
4509  range_list_t *range = calloc(1, sizeof(range_list_t));
4510  if (!range) {
4511  LOG_ERROR("Out of memory");
4512  return ERROR_FAIL;
4513  }
4514 
4515  range->low = low;
4516  range->high = high;
4517  range->name = name;
4518  /* ownership over name_buffer contents is transferred to list item here */
4519  *name_buffer = NULL;
4520  list_add(&range->list, ranges);
4521  }
4522 
4523  return ERROR_OK;
4524 }
4525 
4526 static int parse_reg_ranges(struct list_head *ranges, const char *tcl_arg,
4527  const char *reg_type, unsigned int max_val)
4528 {
4529  char *args = strdup(tcl_arg);
4530  if (!args) {
4531  LOG_ERROR("Out of memory");
4532  return ERROR_FAIL;
4533  }
4534  char *name_buffer = NULL;
4535  int result = parse_reg_ranges_impl(ranges, args, reg_type, max_val, &name_buffer);
4536  free(name_buffer);
4537  free(args);
4538  return result;
4539 }
4540 
4541 COMMAND_HANDLER(riscv_set_expose_csrs)
4542 {
4543  if (CMD_ARGC == 0)
4545 
4547  RISCV_INFO(info);
4548  int ret = ERROR_OK;
4549 
4550  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4551  ret = parse_reg_ranges(&info->expose_csr, CMD_ARGV[i], "csr", 0xfff);
4552  if (ret != ERROR_OK)
4553  break;
4554  }
4555 
4556  return ret;
4557 }
4558 
4559 COMMAND_HANDLER(riscv_set_expose_custom)
4560 {
4561  if (CMD_ARGC == 0)
4563 
4565  RISCV_INFO(info);
4566  int ret = ERROR_OK;
4567 
4568  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4569  ret = parse_reg_ranges(&info->expose_custom, CMD_ARGV[i], "custom", 0x3fff);
4570  if (ret != ERROR_OK)
4571  break;
4572  }
4573 
4574  return ret;
4575 }
4576 
4577 COMMAND_HANDLER(riscv_hide_csrs)
4578 {
4579  if (CMD_ARGC == 0)
4581 
4583  RISCV_INFO(info);
4584  int ret = ERROR_OK;
4585 
4586  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4587  ret = parse_reg_ranges(&info->hide_csr, CMD_ARGV[i], "csr", 0xfff);
4588  if (ret != ERROR_OK)
4589  break;
4590  }
4591 
4592  return ret;
4593 }
4594 
4595 COMMAND_HANDLER(riscv_authdata_read)
4596 {
4597  unsigned int index = 0;
4598  if (CMD_ARGC == 1)
4599  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], index);
4600  else if (CMD_ARGC != 0)
4602 
4604  if (!target) {
4605  LOG_ERROR("target is NULL!");
4606  return ERROR_FAIL;
4607  }
4608 
4609  RISCV_INFO(r);
4610  if (!r) {
4611  LOG_TARGET_ERROR(target, "riscv_info is NULL!");
4612  return ERROR_FAIL;
4613  }
4614 
4615  if (r->authdata_read) {
4616  uint32_t value;
4617  if (r->authdata_read(target, &value, index) != ERROR_OK)
4618  return ERROR_FAIL;
4619  command_print_sameline(CMD, "0x%08" PRIx32, value);
4620  return ERROR_OK;
4621  }
4622 
4623  LOG_TARGET_ERROR(target, "authdata_read is not implemented for this target.");
4624  return ERROR_FAIL;
4625 }
4626 
4627 COMMAND_HANDLER(riscv_authdata_write)
4628 {
4629  uint32_t value;
4630  unsigned int index = 0;
4631 
4632  if (CMD_ARGC == 0 || CMD_ARGC > 2)
4634 
4635  if (CMD_ARGC == 1) {
4636  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], value);
4637  } else {
4638  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], index);
4639  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
4640  }
4641 
4643  RISCV_INFO(r);
4644 
4645  if (!r->authdata_write) {
4646  LOG_TARGET_ERROR(target, "authdata_write is not implemented for this target.");
4647  return ERROR_FAIL;
4648  }
4649 
4650  return r->authdata_write(target, value, index);
4651 }
4652 
4653 uint32_t riscv_get_dmi_address(const struct target *target, uint32_t dm_address)
4654 {
4655  assert(target);
4656  RISCV_INFO(r);
4657  if (!r || !r->get_dmi_address)
4658  return dm_address;
4659  return r->get_dmi_address(target, dm_address);
4660 }
4661 
4662 static int riscv_dmi_read(struct target *target, uint32_t *value, uint32_t address)
4663 {
4664  if (!target) {
4665  LOG_ERROR("target is NULL!");
4666  return ERROR_FAIL;
4667  }
4668  RISCV_INFO(r);
4669  if (!r) {
4670  LOG_TARGET_ERROR(target, "riscv_info is NULL!");
4671  return ERROR_FAIL;
4672  }
4673  if (!r->dmi_read) {
4674  LOG_TARGET_ERROR(target, "dmi_read is not implemented.");
4675  return ERROR_FAIL;
4676  }
4677  return r->dmi_read(target, value, address);
4678 }
4679 
4680 static int riscv_dmi_write(struct target *target, uint32_t dmi_address, uint32_t value)
4681 {
4682  if (!target) {
4683  LOG_ERROR("target is NULL!");
4684  return ERROR_FAIL;
4685  }
4686  RISCV_INFO(r);
4687  if (!r) {
4688  LOG_TARGET_ERROR(target, "riscv_info is NULL!");
4689  return ERROR_FAIL;
4690  }
4691  if (!r->dmi_write) {
4692  LOG_TARGET_ERROR(target, "dmi_write is not implemented.");
4693  return ERROR_FAIL;
4694  }
4695  const int result = r->dmi_write(target, dmi_address, value);
4696  /* Invalidate our cached progbuf copy:
4697  * - if the user tinkered directly with a progbuf register
4698  * - if debug module was reset, in which case progbuf registers
4699  * may not retain their value.
4700  * FIXME: If there are multiple DMs on a single TAP, it is possible to
4701  * clobber progbuf or reset the DM of another target.
4702  */
4703  const bool progbuf_touched =
4704  (dmi_address >= riscv_get_dmi_address(target, DM_PROGBUF0) &&
4705  dmi_address <= riscv_get_dmi_address(target, DM_PROGBUF15));
4706  const bool dm_deactivated =
4707  (dmi_address == riscv_get_dmi_address(target, DM_DMCONTROL) &&
4708  (value & DM_DMCONTROL_DMACTIVE) == 0);
4709  if (progbuf_touched || dm_deactivated) {
4710  if (r->invalidate_cached_progbuf) {
4711  /* Here the return value of invalidate_cached_progbuf()
4712  * is ignored. It is okay to do so for now, since the
4713  * only case an error is returned is a failure to
4714  * assign a DM to the target, which would have already
4715  * caused an error during dmi_write().
4716  * FIXME: invalidate_cached_progbuf() should be void.
4717  */
4718  r->invalidate_cached_progbuf(target);
4719  } else {
4721  "invalidate_cached_progbuf() is not implemented.");
4722  }
4723  }
4724  return result;
4725 }
4726 
4727 COMMAND_HANDLER(handle_riscv_dmi_read)
4728 {
4729  if (CMD_ARGC != 1)
4731 
4732  uint32_t dmi_address;
4733  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], dmi_address);
4734 
4735  struct target * const target = get_current_target(CMD_CTX);
4736  uint32_t value;
4737  const int result = riscv_dmi_read(target, &value, dmi_address);
4738  if (result == ERROR_OK)
4739  command_print(CMD, "0x%" PRIx32, value);
4740  return result;
4741 }
4742 
4743 COMMAND_HANDLER(handle_riscv_dmi_write)
4744 {
4745  if (CMD_ARGC != 2)
4747 
4748  uint32_t dmi_address, value;
4749  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], dmi_address);
4750  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
4751 
4752  struct target * const target = get_current_target(CMD_CTX);
4753  return riscv_dmi_write(target, dmi_address, value);
4754 }
4755 
4756 COMMAND_HANDLER(handle_riscv_dm_read)
4757 {
4758  if (CMD_ARGC != 1)
4760 
4761  uint32_t dm_address;
4762  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], dm_address);
4763 
4764  struct target * const target = get_current_target(CMD_CTX);
4765  uint32_t value;
4766  const int result = riscv_dmi_read(target, &value,
4767  riscv_get_dmi_address(target, dm_address));
4768  if (result == ERROR_OK)
4769  command_print(CMD, "0x%" PRIx32, value);
4770  return result;
4771 }
4772 
4773 COMMAND_HANDLER(handle_riscv_dm_write)
4774 {
4775  if (CMD_ARGC != 2)
4777 
4778  uint32_t dm_address, value;
4779  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], dm_address);
4780  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
4781 
4782  struct target * const target = get_current_target(CMD_CTX);
4783  return riscv_dmi_write(target, riscv_get_dmi_address(target, dm_address),
4784  value);
4785 }
4786 
4787 COMMAND_HANDLER(riscv_reset_delays)
4788 {
4789  int wait = 0;
4790 
4791  if (CMD_ARGC > 1)
4793 
4794  if (CMD_ARGC == 1)
4795  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], wait);
4796 
4798  RISCV_INFO(r);
4799  r->reset_delays_wait = wait;
4800  return ERROR_OK;
4801 }
4802 
4803 COMMAND_HANDLER(riscv_set_ir)
4804 {
4805  if (CMD_ARGC != 2)
4807 
4808  uint32_t value;
4809  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
4810 
4811  if (!strcmp(CMD_ARGV[0], "idcode"))
4812  buf_set_u32(ir_idcode, 0, 32, value);
4813  else if (!strcmp(CMD_ARGV[0], "dtmcs"))
4814  buf_set_u32(ir_dtmcontrol, 0, 32, value);
4815  else if (!strcmp(CMD_ARGV[0], "dmi"))
4816  buf_set_u32(ir_dbus, 0, 32, value);
4817  else
4818  return ERROR_FAIL;
4819 
4820  return ERROR_OK;
4821 }
4822 
4823 COMMAND_HANDLER(riscv_resume_order)
4824 {
4825  if (CMD_ARGC != 1)
4827 
4828  if (!strcmp(CMD_ARGV[0], "normal")) {
4830  } else if (!strcmp(CMD_ARGV[0], "reversed")) {
4832  } else {
4833  LOG_ERROR("Unsupported resume order: %s", CMD_ARGV[0]);
4834  return ERROR_FAIL;
4835  }
4836 
4837  return ERROR_OK;
4838 }
4839 
4840 COMMAND_HANDLER(riscv_use_bscan_tunnel)
4841 {
4842  uint8_t irwidth = 0;
4843  int tunnel_type = BSCAN_TUNNEL_NESTED_TAP;
4844 
4845  if (CMD_ARGC < 1 || CMD_ARGC > 2)
4847 
4848  if (CMD_ARGC >= 1) {
4849  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], irwidth);
4850  assert(BSCAN_TUNNEL_IR_WIDTH_NBITS < 8);
4851  if (irwidth >= (uint8_t)1 << BSCAN_TUNNEL_IR_WIDTH_NBITS) {
4852  command_print(CMD, "'value' does not fit into %d bits.",
4855  }
4856  }
4857  if (CMD_ARGC == 2)
4858  COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tunnel_type);
4859  if (tunnel_type == BSCAN_TUNNEL_NESTED_TAP)
4860  LOG_INFO("Nested Tap based Bscan Tunnel Selected");
4861  else if (tunnel_type == BSCAN_TUNNEL_DATA_REGISTER)
4862  LOG_INFO("Simple Register based Bscan Tunnel Selected");
4863  else
4864  LOG_INFO("Invalid Tunnel type selected ! : selecting default Nested Tap Type");
4865 
4866  bscan_tunnel_type = tunnel_type;
4867  bscan_tunnel_ir_width = irwidth;
4868  return ERROR_OK;
4869 }
4870 
4871 COMMAND_HANDLER(riscv_set_bscan_tunnel_ir)
4872 {
4873  int ir_id = 0;
4874 
4875  if (CMD_ARGC > 1)
4877 
4878  if (CMD_ARGC == 1)
4879  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], ir_id);
4880 
4881  LOG_INFO("Bscan tunnel IR 0x%x selected", ir_id);
4882 
4883  bscan_tunnel_ir_id = ir_id;
4884  return ERROR_OK;
4885 }
4886 
4887 COMMAND_HANDLER(riscv_set_maskisr)
4888 {
4890  RISCV_INFO(info);
4891 
4892  static const struct jim_nvp nvp_maskisr_modes[] = {
4893  { .name = "off", .value = RISCV_ISRMASK_OFF },
4894  { .name = "steponly", .value = RISCV_ISRMASK_STEPONLY },
4895  { .name = NULL, .value = -1 },
4896  };
4897  const struct jim_nvp *n;
4898 
4899  if (CMD_ARGC > 0) {
4900  n = jim_nvp_name2value_simple(nvp_maskisr_modes, CMD_ARGV[0]);
4901  if (!n->name)
4903  info->isrmask_mode = n->value;
4904  } else {
4905  n = jim_nvp_value2name_simple(nvp_maskisr_modes, info->isrmask_mode);
4906  command_print(CMD, "riscv interrupt mask %s", n->name);
4907  }
4908 
4909  return ERROR_OK;
4910 }
4911 
4912 COMMAND_HANDLER(riscv_set_autofence)
4913 {
4915  RISCV_INFO(r);
4916 
4917  if (CMD_ARGC == 0) {
4918  command_print(CMD, "autofence: %s", r->autofence ? "on" : "off");
4919  return ERROR_OK;
4920  } else if (CMD_ARGC == 1) {
4921  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], r->autofence);
4922  return ERROR_OK;
4923  }
4924 
4926 }
4927 
4928 COMMAND_HELPER(ebreakx_deprecation_helper, enum riscv_priv_mode mode)
4929 {
4930  struct target * const target = get_current_target(CMD_CTX);
4932  const char *mode_str;
4933  switch (mode) {
4934  case RISCV_MODE_M:
4935  mode_str = "m";
4936  break;
4937  case RISCV_MODE_S:
4938  mode_str = "s";
4939  break;
4940  case RISCV_MODE_U:
4941  mode_str = "u";
4942  break;
4943  default:
4944  assert(0 && "Unexpected execution mode");
4945  mode_str = "unexpected";
4946  }
4947  if (CMD_ARGC > 1)
4949  if (CMD_ARGC == 0) {
4950  LOG_WARNING("DEPRECATED! use '%s cget -ebreak' not '%s'",
4952  command_print(CMD, "riscv_ebreak%s enabled: %s", mode_str,
4953  config->dcsr_ebreak_fields[mode] ? "on" : "off");
4954  return ERROR_OK;
4955  }
4956  assert(CMD_ARGC == 1);
4957  command_print(CMD, "DEPRECATED! use '%s configure -ebreak %s' not '%s'",
4958  target_name(target), mode_str, CMD_NAME);
4959  bool ebreak_ctl;
4960  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], ebreak_ctl);
4961  config->dcsr_ebreak_fields[mode] = ebreak_ctl;
4962  switch (mode) {
4963  case RISCV_MODE_S:
4964  config->dcsr_ebreak_fields[RISCV_MODE_VS] = ebreak_ctl;
4965  break;
4966  case RISCV_MODE_U:
4967  config->dcsr_ebreak_fields[RISCV_MODE_VU] = ebreak_ctl;
4968  break;
4969  default:
4970  break;
4971  }
4972  return ERROR_OK;
4973 }
4974 
4975 COMMAND_HANDLER(riscv_set_ebreakm)
4976 {
4977  return CALL_COMMAND_HANDLER(ebreakx_deprecation_helper,
4978  RISCV_MODE_M);
4979 }
4980 
4981 COMMAND_HANDLER(riscv_set_ebreaks)
4982 {
4983  return CALL_COMMAND_HANDLER(ebreakx_deprecation_helper,
4984  RISCV_MODE_S);
4985 }
4986 
4987 COMMAND_HANDLER(riscv_set_ebreaku)
4988 {
4989  return CALL_COMMAND_HANDLER(ebreakx_deprecation_helper,
4990  RISCV_MODE_U);
4991 }
4992 
4993 COMMAND_HELPER(riscv_clear_trigger, int trigger_id, const char *name)
4994 {
4996  if (CMD_ARGC != 1)
4998 
4999  if (find_first_trigger_by_id(target, trigger_id) < 0) {
5000  LOG_TARGET_ERROR(target, "No %s is set. Nothing to clear.", name);
5001  return ERROR_FAIL;
5002  }
5003  return remove_trigger(target, trigger_id);
5004 }
5005 
5006 COMMAND_HANDLER(riscv_itrigger)
5007 {
5008  if (CMD_ARGC < 1)
5010 
5012  const int ITRIGGER_UNIQUE_ID = -CSR_TDATA1_TYPE_ITRIGGER;
5013 
5014  if (!target_was_examined(target)) {
5015  LOG_TARGET_ERROR(target, "not examined");
5017  }
5018 
5020  return ERROR_FAIL;
5021 
5022  if (!strcmp(CMD_ARGV[0], "set")) {
5023  if (find_first_trigger_by_id(target, ITRIGGER_UNIQUE_ID) >= 0) {
5024  LOG_TARGET_ERROR(target, "An itrigger is already set, and OpenOCD "
5025  "doesn't support setting more than one at a time.");
5026  return ERROR_FAIL;
5027  }
5028  bool vs = false;
5029  bool vu = false;
5030  bool nmi = false;
5031  bool m = false;
5032  bool s = false;
5033  bool u = false;
5034  riscv_reg_t interrupts = 0;
5035 
5036  for (unsigned int i = 1; i < CMD_ARGC; i++) {
5037  if (!strcmp(CMD_ARGV[i], "vs"))
5038  vs = true;
5039  else if (!strcmp(CMD_ARGV[i], "vu"))
5040  vu = true;
5041  else if (!strcmp(CMD_ARGV[i], "nmi"))
5042  nmi = true;
5043  else if (!strcmp(CMD_ARGV[i], "m"))
5044  m = true;
5045  else if (!strcmp(CMD_ARGV[i], "s"))
5046  s = true;
5047  else if (!strcmp(CMD_ARGV[i], "u"))
5048  u = true;
5049  else
5050  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[i], interrupts);
5051  }
5052  if (!nmi && interrupts == 0) {
5053  LOG_ERROR("Doesn't make sense to set itrigger with "
5054  "mie_bits=0 and without nmi.");
5055  return ERROR_FAIL;
5056  } else if (!vs && !vu && !m && !s && !u) {
5057  LOG_ERROR("Doesn't make sense to set itrigger without at "
5058  "least one of vs, vu, m, s, or u.");
5059  return ERROR_FAIL;
5060  }
5061  int result = maybe_add_trigger_t4(target, vs, vu, nmi, m, s, u, interrupts, ITRIGGER_UNIQUE_ID);
5062  if (result != ERROR_OK)
5063  LOG_TARGET_ERROR(target, "Failed to set requested itrigger.");
5064  return result;
5065 
5066  } else if (!strcmp(CMD_ARGV[0], "clear")) {
5067  return riscv_clear_trigger(CMD, ITRIGGER_UNIQUE_ID, "itrigger");
5068 
5069  } else {
5070  LOG_ERROR("First argument must be either 'set' or 'clear'.");
5072  }
5073  return ERROR_OK;
5074 }
5075 
5076 COMMAND_HANDLER(riscv_icount)
5077 {
5078  if (CMD_ARGC < 1)
5080 
5082  const int ICOUNT_UNIQUE_ID = -CSR_TDATA1_TYPE_ICOUNT;
5083 
5084  if (!target_was_examined(target)) {
5085  LOG_TARGET_ERROR(target, "not examined");
5087  }
5088 
5090  return ERROR_FAIL;
5091 
5092  if (!strcmp(CMD_ARGV[0], "set")) {
5093  if (find_first_trigger_by_id(target, ICOUNT_UNIQUE_ID) >= 0) {
5094  LOG_TARGET_ERROR(target, "An icount trigger is already set, and OpenOCD "
5095  "doesn't support setting more than one at a time.");
5096  return ERROR_FAIL;
5097  }
5098  bool vs = false;
5099  bool vu = false;
5100  bool m = false;
5101  bool s = false;
5102  bool u = false;
5103  bool pending = false;
5104  unsigned int count = 0;
5105 
5106  for (unsigned int i = 1; i < CMD_ARGC; i++) {
5107  if (!strcmp(CMD_ARGV[i], "vs"))
5108  vs = true;
5109  else if (!strcmp(CMD_ARGV[i], "vu"))
5110  vu = true;
5111  else if (!strcmp(CMD_ARGV[i], "pending"))
5112  pending = true;
5113  else if (!strcmp(CMD_ARGV[i], "m"))
5114  m = true;
5115  else if (!strcmp(CMD_ARGV[i], "s"))
5116  s = true;
5117  else if (!strcmp(CMD_ARGV[i], "u"))
5118  u = true;
5119  else
5120  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[i], count);
5121  }
5122  if (count == 0) {
5123  LOG_ERROR("Doesn't make sense to set icount trigger with "
5124  "count=0.");
5125  return ERROR_FAIL;
5126  } else if (!vs && !vu && !m && !s && !u) {
5127  LOG_ERROR("Doesn't make sense to set itrigger without at "
5128  "least one of vs, vu, m, s, or u.");
5129  return ERROR_FAIL;
5130  }
5131  int result = maybe_add_trigger_t3(target, vs, vu, m, s, u, pending, count, ICOUNT_UNIQUE_ID);
5132  if (result != ERROR_OK)
5133  LOG_TARGET_ERROR(target, "Failed to set requested icount trigger.");
5134  return result;
5135 
5136  } else if (!strcmp(CMD_ARGV[0], "clear")) {
5137  return riscv_clear_trigger(CMD, ICOUNT_UNIQUE_ID, "icount trigger");
5138 
5139  } else {
5140  LOG_ERROR("First argument must be either 'set' or 'clear'.");
5142  }
5143  return ERROR_OK;
5144 }
5145 
5146 COMMAND_HANDLER(riscv_etrigger)
5147 {
5148  if (CMD_ARGC < 1)
5150 
5152  const int ETRIGGER_UNIQUE_ID = -CSR_TDATA1_TYPE_ETRIGGER;
5153 
5154  if (!target_was_examined(target)) {
5155  LOG_TARGET_ERROR(target, "not examined");
5157  }
5158 
5160  return ERROR_FAIL;
5161 
5162  if (!strcmp(CMD_ARGV[0], "set")) {
5163  if (find_first_trigger_by_id(target, ETRIGGER_UNIQUE_ID) >= 0) {
5164  LOG_TARGET_ERROR(target, "An etrigger is already set, and OpenOCD "
5165  "doesn't support setting more than one at a time.");
5166  return ERROR_FAIL;
5167  }
5168  bool vs = false;
5169  bool vu = false;
5170  bool m = false;
5171  bool s = false;
5172  bool u = false;
5173  riscv_reg_t exception_codes = 0;
5174 
5175  for (unsigned int i = 1; i < CMD_ARGC; i++) {
5176  if (!strcmp(CMD_ARGV[i], "vs"))
5177  vs = true;
5178  else if (!strcmp(CMD_ARGV[i], "vu"))
5179  vu = true;
5180  else if (!strcmp(CMD_ARGV[i], "m"))
5181  m = true;
5182  else if (!strcmp(CMD_ARGV[i], "s"))
5183  s = true;
5184  else if (!strcmp(CMD_ARGV[i], "u"))
5185  u = true;
5186  else
5187  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[i], exception_codes);
5188  }
5189  if (exception_codes == 0) {
5190  LOG_ERROR("Doesn't make sense to set etrigger with "
5191  "exception_codes=0.");
5192  return ERROR_FAIL;
5193  } else if (!vs && !vu && !m && !s && !u) {
5194  LOG_ERROR("Doesn't make sense to set etrigger without at "
5195  "least one of vs, vu, m, s, or u.");
5196  return ERROR_FAIL;
5197  }
5198  int result = maybe_add_trigger_t5(target, vs, vu, m, s, u, exception_codes, ETRIGGER_UNIQUE_ID);
5199  if (result != ERROR_OK)
5200  LOG_TARGET_ERROR(target, "Failed to set requested etrigger.");
5201  return result;
5202 
5203  } else if (!strcmp(CMD_ARGV[0], "clear")) {
5204  return riscv_clear_trigger(CMD, ETRIGGER_UNIQUE_ID, "etrigger");
5205 
5206  } else {
5207  LOG_ERROR("First argument must be either 'set' or 'clear'.");
5209  }
5210  return ERROR_OK;
5211 }
5212 
5213 COMMAND_HANDLER(handle_repeat_read)
5214 {
5216  if (!target_was_examined(target)) {
5217  LOG_TARGET_ERROR(target, "not examined");
5219  }
5220 
5221  RISCV_INFO(r);
5222 
5223  if (CMD_ARGC < 2 || CMD_ARGC > 3)
5225 
5226  uint32_t count;
5230  uint32_t size = 4;
5231  if (CMD_ARGC > 2)
5233 
5234  if (count == 0)
5235  return ERROR_OK;
5236 
5237  uint8_t *buffer = malloc(size * count);
5238  if (!buffer) {
5239  LOG_ERROR("malloc failed");
5240  return ERROR_FAIL;
5241  }
5242  const struct riscv_mem_access_args args = {
5243  .address = address,
5244  .read_buffer = buffer,
5245  .size = size,
5246  .count = count,
5247  .increment = 0,
5248  };
5249  int result = r->access_memory(target, args);
5250  if (result == ERROR_OK)
5252  free(buffer);
5253  return result;
5254 }
5255 
5256 COMMAND_HANDLER(handle_memory_sample_command)
5257 {
5259  RISCV_INFO(r);
5260 
5261  if (CMD_ARGC == 0) {
5262  command_print(CMD, "Memory sample configuration for %s:", target_name(target));
5263  for (unsigned int i = 0; i < ARRAY_SIZE(r->sample_config.bucket); i++) {
5264  if (r->sample_config.bucket[i].enabled) {
5265  command_print(CMD, "bucket %d; address=0x%" TARGET_PRIxADDR "; size=%d", i,
5266  r->sample_config.bucket[i].address,
5267  r->sample_config.bucket[i].size_bytes);
5268  } else {
5269  command_print(CMD, "bucket %d; disabled", i);
5270  }
5271  }
5272  return ERROR_OK;
5273  }
5274 
5275  if (CMD_ARGC < 2)
5277 
5278  uint32_t bucket;
5279  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bucket);
5280  if (bucket > ARRAY_SIZE(r->sample_config.bucket)) {
5281  LOG_TARGET_ERROR(target, "Max bucket number is %zd.", ARRAY_SIZE(r->sample_config.bucket));
5283  }
5284 
5285  if (!strcmp(CMD_ARGV[1], "clear")) {
5286  r->sample_config.bucket[bucket].enabled = false;
5287  } else {
5288  COMMAND_PARSE_ADDRESS(CMD_ARGV[1], r->sample_config.bucket[bucket].address);
5289 
5290  if (CMD_ARGC > 2) {
5291  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], r->sample_config.bucket[bucket].size_bytes);
5292  if (r->sample_config.bucket[bucket].size_bytes != 4 &&
5293  r->sample_config.bucket[bucket].size_bytes != 8) {
5294  LOG_TARGET_ERROR(target, "Only 4-byte and 8-byte sizes are supported.");
5296  }
5297  } else {
5298  r->sample_config.bucket[bucket].size_bytes = 4;
5299  }
5300 
5301  r->sample_config.bucket[bucket].enabled = true;
5302  }
5303 
5304  if (!r->sample_buf.buf) {
5305  r->sample_buf.size = 1024 * 1024;
5306  r->sample_buf.buf = malloc(r->sample_buf.size);
5307  }
5308 
5309  /* Clear the buffer when the configuration is changed. */
5310  r->sample_buf.used = 0;
5311 
5312  r->sample_config.enabled = true;
5313 
5314  return ERROR_OK;
5315 }
5316 
5317 COMMAND_HANDLER(handle_dump_sample_buf_command)
5318 {
5320  RISCV_INFO(r);
5321 
5322  if (CMD_ARGC > 1)
5324 
5325  bool base64 = false;
5326  if (CMD_ARGC > 0) {
5327  if (!strcmp(CMD_ARGV[0], "base64")) {
5328  base64 = true;
5329  } else {
5330  LOG_ERROR("Unknown argument: %s", CMD_ARGV[0]);
5332  }
5333  }
5334 
5335  int result = ERROR_OK;
5336  if (base64) {
5337  unsigned char *encoded = base64_encode(r->sample_buf.buf,
5338  r->sample_buf.used, NULL);
5339  if (!encoded) {
5340  LOG_TARGET_ERROR(target, "Failed base64 encode!");
5341  result = ERROR_FAIL;
5342  goto error;
5343  }
5344  command_print(CMD, "%s", encoded);
5345  free(encoded);
5346  } else {
5347  unsigned int i = 0;
5348  while (i < r->sample_buf.used) {
5349  uint8_t command = r->sample_buf.buf[i++];
5351  uint32_t timestamp = buf_get_u32(r->sample_buf.buf + i, 0, 32);
5352  i += 4;
5353  command_print(CMD, "timestamp before: %u", timestamp);
5354  } else if (command == RISCV_SAMPLE_BUF_TIMESTAMP_AFTER) {
5355  uint32_t timestamp = buf_get_u32(r->sample_buf.buf + i, 0, 32);
5356  i += 4;
5357  command_print(CMD, "timestamp after: %u", timestamp);
5358  } else if (command < ARRAY_SIZE(r->sample_config.bucket)) {
5360  r->sample_config.bucket[command].address);
5361  if (r->sample_config.bucket[command].size_bytes == 4) {
5362  uint32_t value = buf_get_u32(r->sample_buf.buf + i, 0, 32);
5363  i += 4;
5364  command_print(CMD, "0x%08" PRIx32, value);
5365  } else if (r->sample_config.bucket[command].size_bytes == 8) {
5366  uint64_t value = buf_get_u64(r->sample_buf.buf + i, 0, 64);
5367  i += 8;
5368  command_print(CMD, "0x%016" PRIx64, value);
5369  } else {
5370  LOG_TARGET_ERROR(target, "Found invalid size in bucket %d: %d", command,
5371  r->sample_config.bucket[command].size_bytes);
5372  result = ERROR_FAIL;
5373  goto error;
5374  }
5375  } else {
5376  LOG_TARGET_ERROR(target, "Found invalid command byte in sample buf: 0x%2x at offset 0x%x",
5377  command, i - 1);
5378  result = ERROR_FAIL;
5379  goto error;
5380  }
5381  }
5382  }
5383 
5384 error:
5385  /* Clear the sample buffer even when there was an error. */
5386  r->sample_buf.used = 0;
5387  return result;
5388 }
5389 
5390 static COMMAND_HELPER(riscv_print_info_line_if_available, const char *section,
5391  const char *key, unsigned int value, bool is_available)
5392 {
5393  char full_key[80];
5394  snprintf(full_key, sizeof(full_key), "%s.%s", section, key);
5395  if (is_available)
5396  command_print(CMD, "%-21s %3d", full_key, value);
5397  else
5398  command_print(CMD, "%-21s unavailable", full_key);
5399  return 0;
5400 }
5401 
5402 COMMAND_HELPER(riscv_print_info_line, const char *section, const char *key,
5403  unsigned int value)
5404 {
5405  return CALL_COMMAND_HANDLER(riscv_print_info_line_if_available, section,
5406  key, value, /*is_available*/ true);
5407 }
5408 
5409 COMMAND_HANDLER(handle_info)
5410 {
5412  RISCV_INFO(r);
5413 
5414  /* This output format can be fed directly into TCL's "array set". */
5415 
5416  riscv_print_info_line(CMD, "hart", "xlen", riscv_xlen(target));
5417 
5418  const bool trigger_count_available =
5420  riscv_print_info_line_if_available(CMD, "hart", "trigger_count",
5421  r->trigger_count, trigger_count_available);
5422  if (r->print_info)
5423  return CALL_COMMAND_HANDLER(r->print_info, target);
5424 
5425  return 0;
5426 }
5427 
5428 COMMAND_HANDLER(riscv_exec_progbuf)
5429 {
5430  if (CMD_ARGC < 1 || CMD_ARGC > 16)
5432 
5434 
5435  if (!target_was_examined(target)) {
5436  LOG_TARGET_ERROR(target, "not examined");
5438  }
5439 
5440  RISCV_INFO(r);
5441  if (r->dtm_version != DTM_DTMCS_VERSION_1_0) {
5442  LOG_TARGET_ERROR(target, "exec_progbuf: Program buffer is "
5443  "only supported on v0.13 or v1.0 targets.");
5444  return ERROR_FAIL;
5445  }
5446 
5447  if (target->state != TARGET_HALTED) {
5448  LOG_TARGET_ERROR(target, "exec_progbuf: Can't execute "
5449  "program buffer, target not halted.");
5450  return ERROR_TARGET_NOT_HALTED;
5451  }
5452 
5453  if (riscv_progbuf_size(target) == 0) {
5454  LOG_TARGET_ERROR(target, "exec_progbuf: Program buffer not implemented "
5455  "in the target.");
5456  return ERROR_FAIL;
5457  }
5458 
5459  struct riscv_program prog;
5460  riscv_program_init(&prog, target);
5461 
5462  for (unsigned int i = 0; i < CMD_ARGC; i++) {
5463  riscv_insn_t instr;
5464  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[i], instr);
5465  if (riscv_program_insert(&prog, instr) != ERROR_OK)
5466  return ERROR_FAIL;
5467  }
5468 
5470  return ERROR_FAIL;
5471  int error = riscv_program_exec(&prog, target);
5473 
5474  if (error != ERROR_OK) {
5475  LOG_TARGET_ERROR(target, "exec_progbuf: Program buffer execution failed.");
5476  return ERROR_FAIL;
5477  }
5478 
5479  LOG_TARGET_DEBUG(target, "exec_progbuf: Program buffer execution successful.");
5480 
5481  return ERROR_OK;
5482 }
5483 
5484 COMMAND_HANDLER(riscv_set_enable_trigger_feature)
5485 {
5487  RISCV_INFO(r);
5488 
5489  if (CMD_ARGC == 2) {
5490  bool enable_for_wp = true;
5491 
5492  if (!strcmp(CMD_ARGV[1], "wp"))
5493  enable_for_wp = true;
5494  else if (!strcmp(CMD_ARGV[1], "none"))
5495  enable_for_wp = false;
5496  else
5498 
5499  if (!strcmp(CMD_ARGV[0], "all")) {
5500  r->wp_allow_equality_match_trigger = enable_for_wp;
5501  r->wp_allow_napot_trigger = enable_for_wp;
5502  r->wp_allow_ge_lt_trigger = enable_for_wp;
5503  } else if (!strcmp(CMD_ARGV[0], "eq")) {
5504  r->wp_allow_equality_match_trigger = enable_for_wp;
5505  } else if (!strcmp(CMD_ARGV[0], "napot")) {
5506  r->wp_allow_napot_trigger = enable_for_wp;
5507  } else if (!strcmp(CMD_ARGV[0], "ge_lt")) {
5508  r->wp_allow_ge_lt_trigger = enable_for_wp;
5509  } else {
5511  }
5512  } else if (CMD_ARGC != 0) {
5514  }
5515 
5516  command_print(CMD, "Triggers feature configuration:\n"
5517  "Equality match trigger: for wp (%s)\n"
5518  "NAPOT trigger: for wp (%s)\n"
5519  "ge-lt chained triggers: for wp (%s)",
5520  r->wp_allow_equality_match_trigger ? "enabled" : "disabled",
5521  r->wp_allow_napot_trigger ? "enabled" : "disabled",
5522  r->wp_allow_ge_lt_trigger ? "enabled" : "disabled");
5523 
5524  return ERROR_OK;
5525 }
5526 
5527 static COMMAND_HELPER(report_reserved_triggers, struct target *target)
5528 {
5529  RISCV_INFO(r);
5531  return ERROR_FAIL;
5532  const char *separator = "";
5533  for (riscv_reg_t t = 0; t < r->trigger_count; ++t) {
5534  if (r->reserved_triggers[t]) {
5535  command_print_sameline(CMD, "%s%" PRIu64, separator, t);
5536  separator = " ";
5537  }
5538  }
5539  command_print_sameline(CMD, "\n");
5540  return ERROR_OK;
5541 }
5542 
5543 COMMAND_HANDLER(handle_reserve_trigger)
5544 {
5546  if (!target_was_examined(target)) {
5547  command_print(CMD, "Error: Target not examined");
5549  }
5550 
5551  if (CMD_ARGC == 0)
5552  return CALL_COMMAND_HANDLER(report_reserved_triggers, target);
5553 
5554  if (CMD_ARGC != 2)
5556 
5557  riscv_reg_t t;
5558  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], t);
5559 
5561  return ERROR_FAIL;
5562  RISCV_INFO(r);
5563  if (r->trigger_count == 0) {
5564  command_print(CMD, "Error: There are no triggers on the target.");
5566  }
5567  if (t >= r->trigger_count) {
5568  command_print(CMD, "Error: trigger with index %" PRIu64
5569  " does not exist. There are only %u triggers"
5570  " on the target (with indexes 0 .. %u).",
5571  t, r->trigger_count, r->trigger_count - 1);
5573  }
5574  if (r->trigger_unique_id[t] != -1) {
5575  command_print(CMD, "Error: trigger with index %" PRIu64
5576  " is already in use and can not be reserved.", t);
5578  }
5579  COMMAND_PARSE_ON_OFF(CMD_ARGV[1], r->reserved_triggers[t]);
5580  return ERROR_OK;
5581 }
5582 
5583 COMMAND_HANDLER(handle_riscv_virt2phys_mode)
5584 {
5586  if (CMD_ARGC == 0) {
5587  enum riscv_virt2phys_mode mode = info->virt2phys_mode;
5589  return ERROR_OK;
5590  }
5591 
5592  if (CMD_ARGC != 1)
5594 
5595  // TODO: add auto mode to allow OpenOCD choose translation mode
5596  if (!strcmp(CMD_ARGV[0],
5598  info->virt2phys_mode = RISCV_VIRT2PHYS_MODE_SW;
5599  } else if (!strcmp(CMD_ARGV[0],
5601  info->virt2phys_mode = RISCV_VIRT2PHYS_MODE_HW;
5602  } else if (!strcmp(CMD_ARGV[0],
5604  info->virt2phys_mode = RISCV_VIRT2PHYS_MODE_OFF;
5605  } else {
5606  command_print(CMD, "Unsupported address translation mode: %s", CMD_ARGV[0]);
5608  }
5609 
5610  return ERROR_OK;
5611 }
5612 
5613 static const struct command_registration riscv_exec_command_handlers[] = {
5614  {
5615  .name = "dump_sample_buf",
5616  .handler = handle_dump_sample_buf_command,
5617  .mode = COMMAND_ANY,
5618  .usage = "[base64]",
5619  .help = "Print the contents of the sample buffer, and clear the buffer."
5620  },
5621  {
5622  .name = "info",
5623  .handler = handle_info,
5624  .mode = COMMAND_ANY,
5625  .usage = "",
5626  .help = "Displays some information OpenOCD detected about the target."
5627  },
5628  {
5629  .name = "memory_sample",
5630  .handler = handle_memory_sample_command,
5631  .mode = COMMAND_ANY,
5632  .usage = "bucket address|clear [size=4]",
5633  .help = "Causes OpenOCD to frequently read size bytes at the given address."
5634  },
5635  {
5636  .name = "repeat_read",
5637  .handler = handle_repeat_read,
5638  .mode = COMMAND_ANY,
5639  .usage = "count address [size=4]",
5640  .help = "Repeatedly read the value at address."
5641  },
5642  {
5643  .name = "set_command_timeout_sec",
5644  .handler = riscv_set_command_timeout_sec,
5645  .mode = COMMAND_ANY,
5646  .usage = "sec",
5647  .help = "Set the wall-clock timeout (in seconds) for individual commands"
5648  },
5649  {
5650  .name = "set_reset_timeout_sec",
5651  .handler = riscv_set_reset_timeout_sec,
5652  .mode = COMMAND_ANY,
5653  .usage = "sec",
5654  .help = "DEPRECATED. Use 'riscv set_command_timeout_sec' instead."
5655  },
5656  {
5657  .name = "set_mem_access",
5658  .handler = riscv_set_mem_access,
5659  .mode = COMMAND_ANY,
5660  .usage = "method1 [method2] [method3]",
5661  .help = "Set which memory access methods shall be used and in which order "
5662  "of priority. Method can be one of: 'progbuf', 'sysbus' or 'abstract'."
5663  },
5664  {
5665  .name = "expose_csrs",
5666  .handler = riscv_set_expose_csrs,
5667  .mode = COMMAND_CONFIG,
5668  .usage = "n0[-m0|=name0][,n1[-m1|=name1]]...[,n15[-m15|=name15]]",
5669  .help = "Configure a list of inclusive ranges for CSRs to expose in "
5670  "addition to the standard ones. This must be executed before "
5671  "`init`."
5672  },
5673  {
5674  .name = "expose_custom",
5675  .handler = riscv_set_expose_custom,
5676  .mode = COMMAND_CONFIG,
5677  .usage = "n0[-m0|=name0][,n1[-m1|=name1]]...[,n15[-m15|=name15]]",
5678  .help = "Configure a list of inclusive ranges for custom registers to "
5679  "expose. custom0 is accessed as abstract register number 0xc000, "
5680  "etc. This must be executed before `init`."
5681  },
5682  {
5683  .name = "hide_csrs",
5684  .handler = riscv_hide_csrs,
5685  .mode = COMMAND_CONFIG,
5686  .usage = "{n0|n-m0}[,n1|n-m1]......",
5687  .help = "Configure a list of inclusive ranges for CSRs to hide from gdb. "
5688  "Hidden registers are still available, but are not listed in "
5689  "gdb target description and `reg` command output. "
5690  "This must be executed before `init`."
5691  },
5692  {
5693  .name = "authdata_read",
5694  .handler = riscv_authdata_read,
5695  .usage = "[index]",
5696  .mode = COMMAND_ANY,
5697  .help = "Return the 32-bit value read from authdata or authdata0 "
5698  "(index=0), or authdata1 (index=1)."
5699  },
5700  {
5701  .name = "authdata_write",
5702  .handler = riscv_authdata_write,
5703  .mode = COMMAND_ANY,
5704  .usage = "[index] value",
5705  .help = "Write the 32-bit value to authdata or authdata0 (index=0), "
5706  "or authdata1 (index=1)."
5707  },
5708  {
5709  .name = "dmi_read",
5710  .handler = handle_riscv_dmi_read,
5711  .mode = COMMAND_ANY,
5712  .usage = "address",
5713  .help = "Read and return 32-bit value from the given address on the "
5714  "RISC-V DMI bus."
5715  },
5716  {
5717  .name = "dmi_write",
5718  .handler = handle_riscv_dmi_write,
5719  .mode = COMMAND_ANY,
5720  .usage = "address value",
5721  .help = "Write a 32-bit value to the given address on the RISC-V DMI bus."
5722  },
5723  {
5724  .name = "dm_read",
5725  .handler = handle_riscv_dm_read,
5726  .mode = COMMAND_ANY,
5727  .usage = "reg_address",
5728  .help = "Read and return 32-bit value from a debug module's register "
5729  "at reg_address."
5730  },
5731  {
5732  .name = "dm_write",
5733  .handler = handle_riscv_dm_write,
5734  .mode = COMMAND_ANY,
5735  .usage = "reg_address value",
5736  .help = "Write a 32-bit value to the debug module's register at "
5737  "reg_address."
5738  },
5739  {
5740  .name = "reset_delays",
5741  .handler = riscv_reset_delays,
5742  .mode = COMMAND_ANY,
5743  .usage = "[wait]",
5744  .help = "OpenOCD learns how many Run-Test/Idle cycles are required "
5745  "between scans to avoid encountering the target being busy. This "
5746  "command resets those learned values after `wait` scans. It's only "
5747  "useful for testing OpenOCD itself."
5748  },
5749  {
5750  .name = "resume_order",
5751  .handler = riscv_resume_order,
5752  .mode = COMMAND_ANY,
5753  .usage = "normal|reversed",
5754  .help = "Choose the order that harts are resumed in when `hasel` is not "
5755  "supported. Normal order is from lowest hart index to highest. "
5756  "Reversed order is from highest hart index to lowest."
5757  },
5758  {
5759  .name = "set_ir",
5760  .handler = riscv_set_ir,
5761  .mode = COMMAND_ANY,
5762  .usage = "idcode|dtmcs|dmi value",
5763  .help = "Set IR value for specified JTAG register."
5764  },
5765  {
5766  .name = "use_bscan_tunnel",
5767  .handler = riscv_use_bscan_tunnel,
5768  .mode = COMMAND_CONFIG,
5769  .usage = "value [type]",
5770  .help = "Enable or disable use of a BSCAN tunnel to reach DM."
5771  },
5772  {
5773  .name = "set_bscan_tunnel_ir",
5774  .handler = riscv_set_bscan_tunnel_ir,
5775  .mode = COMMAND_CONFIG,
5776  .usage = "[value]",
5777  .help = "Specify the JTAG TAP IR used to access the bscan tunnel. "
5778  "By default it is 0x23 << (ir_length - 6), which map some "
5779  "Xilinx FPGA (IR USER4)"
5780  },
5781  {
5782  .name = "set_maskisr",
5783  .handler = riscv_set_maskisr,
5784  .mode = COMMAND_EXEC,
5785  .help = "mask riscv interrupts",
5786  .usage = "['off'|'steponly']",
5787  },
5788  {
5789  .name = "set_ebreakm",
5790  .handler = riscv_set_ebreakm,
5791  .mode = COMMAND_ANY,
5792  .usage = "[on|off]",
5793  .help = "DEPRECATED! use '<target_name> configure -ebreak' or "
5794  "'<target_name> cget -ebreak'"
5795  },
5796  {
5797  .name = "set_ebreaks",
5798  .handler = riscv_set_ebreaks,
5799  .mode = COMMAND_ANY,
5800  .usage = "[on|off]",
5801  .help = "DEPRECATED! use '<target_name> configure -ebreak' or "
5802  "'<target_name> cget -ebreak'"
5803  },
5804  {
5805  .name = "set_ebreaku",
5806  .handler = riscv_set_ebreaku,
5807  .mode = COMMAND_ANY,
5808  .usage = "[on|off]",
5809  .help = "DEPRECATED! use '<target_name> configure -ebreak' or "
5810  "'<target_name> cget -ebreak'"
5811  },
5812  {
5813  .name = "etrigger",
5814  .handler = riscv_etrigger,
5815  .mode = COMMAND_EXEC,
5816  .usage = "set [vs] [vu] [m] [s] [u] <exception_codes>|clear",
5817  .help = "Set or clear a single exception trigger."
5818  },
5819  {
5820  .name = "icount",
5821  .handler = riscv_icount,
5822  .mode = COMMAND_EXEC,
5823  .usage = "set [vs] [vu] [m] [s] [u] [pending] <count>|clear",
5824  .help = "Set or clear a single instruction count trigger."
5825  },
5826  {
5827  .name = "itrigger",
5828  .handler = riscv_itrigger,
5829  .mode = COMMAND_EXEC,
5830  .usage = "set [vs] [vu] [nmi] [m] [s] [u] <mie_bits>|clear",
5831  .help = "Set or clear a single interrupt trigger."
5832  },
5833  {
5834  .name = "exec_progbuf",
5835  .handler = riscv_exec_progbuf,
5836  .mode = COMMAND_EXEC,
5837  .usage = "instr1 [instr2 [... instr16]]",
5838  .help = "Execute a sequence of 32-bit instructions using the program buffer. "
5839  "The final ebreak instruction is added automatically, if needed."
5840  },
5841  {
5842  .name = "set_enable_trigger_feature",
5843  .handler = riscv_set_enable_trigger_feature,
5844  .mode = COMMAND_ANY,
5845  .usage = "[('eq'|'napot'|'ge_lt'|'all') ('wp'|'none')]",
5846  .help = "Control whether OpenOCD is allowed to use certain RISC-V trigger features for watchpoints."
5847  },
5848  {
5849  .name = "reserve_trigger",
5850  .handler = handle_reserve_trigger,
5851  /* TODO: Move this to COMMAND_ANY */
5852  .mode = COMMAND_EXEC,
5853  .usage = "[index ('on'|'off')]",
5854  .help = "Controls which RISC-V triggers shall not be touched by OpenOCD.",
5855  },
5856  {
5857  .name = "virt2phys_mode",
5858  .handler = handle_riscv_virt2phys_mode,
5859  .mode = COMMAND_ANY,
5860  .usage = "['sw'|'hw'|'off']",
5861  .help = "Configure the virtual address translation mode: "
5862  "sw - translate vaddr to paddr by manually traversing page tables, "
5863  "hw - translate vaddr to paddr by hardware, "
5864  "off - no address translation."
5865  },
5866  {
5867  .name = "autofence",
5868  .handler = riscv_set_autofence,
5869  .mode = COMMAND_ANY,
5870  .usage = "[on|off]",
5871  .help = "When on (default), OpenOCD will automatically execute fence instructions in some situations. "
5872  "When off, users need to take care of memory coherency themselves, for example by using "
5873  "`riscv exec_progbuf` to execute fence or CMO instructions."
5874  },
5875  {
5876  .chain = smp_command_handlers
5877  },
5879 };
5880 
5881 /*
5882  * To be noted that RISC-V targets use the same semihosting commands as
5883  * ARM targets.
5884  *
5885  * The main reason is compatibility with existing tools. For example the
5886  * Eclipse OpenOCD/SEGGER J-Link/QEMU plug-ins have several widgets to
5887  * configure semihosting, which generate commands like `arm semihosting
5888  * enable`.
5889  * A secondary reason is the fact that the protocol used is exactly the
5890  * one specified by ARM. If RISC-V will ever define its own semihosting
5891  * protocol, then a command like `riscv semihosting enable` will make
5892  * sense, but for now all semihosting commands are prefixed with `arm`.
5893  */
5894 
5895 static const struct command_registration riscv_command_handlers[] = {
5896  {
5897  .name = "riscv",
5898  .mode = COMMAND_ANY,
5899  .help = "RISC-V Command Group",
5900  .usage = "",
5902  },
5903  {
5904  .name = "arm",
5905  .mode = COMMAND_ANY,
5906  .help = "ARM Command Group",
5907  .usage = "",
5909  },
5911 };
5912 
5913 static unsigned int riscv_xlen_nonconst(struct target *target)
5914 {
5915  return riscv_xlen(target);
5916 }
5917 
5918 static unsigned int riscv_data_bits(struct target *target)
5919 {
5920  RISCV_INFO(r);
5921  if (r->data_bits)
5922  return r->data_bits(target);
5923  return riscv_xlen(target);
5924 }
5925 
5926 struct target_type riscv_target = {
5927  .name = "riscv",
5928 
5929  .target_create = riscv_create_target,
5930  .target_jim_configure = riscv_jim_configure,
5931  .init_target = riscv_init_target,
5932  .deinit_target = riscv_deinit_target,
5933  .examine = riscv_examine,
5934 
5935  /* poll current target status */
5936  .poll = old_or_new_riscv_poll,
5937 
5938  .halt = riscv_halt,
5939  .resume = riscv_target_resume,
5940  .step = old_or_new_riscv_step,
5941 
5942  .assert_reset = riscv_assert_reset,
5943  .deassert_reset = riscv_deassert_reset,
5944 
5945  .read_memory = riscv_read_memory,
5946  .write_memory = riscv_write_memory,
5947  .read_phys_memory = riscv_read_phys_memory,
5948  .write_phys_memory = riscv_write_phys_memory,
5949 
5950  .checksum_memory = riscv_checksum_memory,
5951 
5952  .mmu = riscv_mmu,
5953  .virt2phys = riscv_virt2phys,
5954 
5955  .get_gdb_arch = riscv_get_gdb_arch,
5956  .get_gdb_reg_list = riscv_get_gdb_reg_list,
5957  .get_gdb_reg_list_noread = riscv_get_gdb_reg_list_noread,
5958 
5959  .add_breakpoint = riscv_add_breakpoint,
5960  .remove_breakpoint = riscv_remove_breakpoint,
5961 
5962  .add_watchpoint = riscv_add_watchpoint,
5963  .remove_watchpoint = riscv_remove_watchpoint,
5964  .hit_watchpoint = riscv_hit_watchpoint,
5965 
5966  .arch_state = riscv_arch_state,
5967 
5968  .run_algorithm = riscv_run_algorithm,
5969 
5970  .commands = riscv_command_handlers,
5971 
5972  .address_bits = riscv_xlen_nonconst,
5973  .data_bits = riscv_data_bits
5974 };
5975 
5976 /*** RISC-V Interface ***/
5977 
5978 /* Initializes the shared RISC-V structure. */
5979 static void riscv_info_init(struct target *target, struct riscv_info *r)
5980 {
5981  memset(r, 0, sizeof(*r));
5982 
5984 
5986  r->version_specific = NULL;
5987 
5988  memset(r->trigger_unique_id, 0xff, sizeof(r->trigger_unique_id));
5989 
5990  r->xlen = -1;
5991 
5993 
5995 
5999 
6001  for (size_t i = 0; i < RISCV_MEM_ACCESS_MAX_METHODS_NUM; ++i)
6002  r->mem_access_warn[i] = true;
6003 
6006  INIT_LIST_HEAD(&r->hide_csr);
6007 
6009 
6011  r->wp_allow_ge_lt_trigger = true;
6012  r->wp_allow_napot_trigger = true;
6013 
6014  r->autofence = true;
6015 }
6016 
6018 {
6019  RISCV_INFO(r);
6020 
6021  LOG_TARGET_DEBUG(target, "Resuming hart, state=%d.", target->state);
6022  if (target->state == TARGET_HALTED) {
6023  if (r->resume_go(target) != ERROR_OK)
6024  return ERROR_FAIL;
6025  } else {
6026  LOG_TARGET_DEBUG(target, "Hart requested resume, but was already resumed.");
6027  }
6028  return ERROR_OK;
6029 }
6030 
6031 static int riscv_interrupts_disable(struct target *target, riscv_reg_t *old_mstatus)
6032 {
6033  LOG_TARGET_DEBUG(target, "Disabling interrupts.");
6034  riscv_reg_t current_mstatus;
6035  int ret = riscv_reg_get(target, &current_mstatus, GDB_REGNO_MSTATUS);
6036  if (ret != ERROR_OK) {
6037  LOG_TARGET_ERROR(target, "Failed to read mstatus!");
6038  return ret;
6039  }
6040  if (old_mstatus)
6041  *old_mstatus = current_mstatus;
6042  return riscv_reg_set(target, GDB_REGNO_MSTATUS, current_mstatus & ~mstatus_ie_mask);
6043 }
6044 
6045 static int riscv_interrupts_restore(struct target *target, riscv_reg_t old_mstatus)
6046 {
6047  LOG_TARGET_DEBUG(target, "Restoring interrupts.");
6048  riscv_reg_t current_mstatus;
6049  int ret = riscv_reg_get(target, &current_mstatus, GDB_REGNO_MSTATUS);
6050  if (ret != ERROR_OK) {
6051  LOG_TARGET_ERROR(target, "Failed to read mstatus!");
6052  return ret;
6053  }
6054  if ((current_mstatus & mstatus_ie_mask) != 0) {
6055  LOG_TARGET_WARNING(target, "Interrupt enable bits in mstatus changed during single-step.");
6056  LOG_TARGET_WARNING(target, "OpenOCD might have affected the program when it restored the interrupt bits after single-step.");
6057  LOG_TARGET_WARNING(target, "Hint: Use 'riscv set_maskisr off' to prevent OpenOCD from touching mstatus during single-step.");
6058  }
6059  return riscv_reg_set(target, GDB_REGNO_MSTATUS, current_mstatus | (old_mstatus & mstatus_ie_mask));
6060 }
6061 
6063 {
6064  RISCV_INFO(r);
6065  LOG_TARGET_DEBUG(target, "Stepping.");
6066 
6067  if (target->state != TARGET_HALTED) {
6068  LOG_TARGET_ERROR(target, "Hart isn't halted before single step!");
6069  return ERROR_TARGET_NOT_HALTED;
6070  }
6071  r->on_step(target);
6072  if (r->step_current_hart(target) != ERROR_OK)
6073  return ERROR_FAIL;
6074  if (target->state != TARGET_HALTED) {
6075  LOG_TARGET_ERROR(target, "Hart was not halted after single step!");
6076  return ERROR_FAIL;
6077  }
6078  return ERROR_OK;
6079 }
6080 
6081 bool riscv_supports_extension(const struct target *target, char letter)
6082 {
6083  RISCV_INFO(r);
6084  unsigned int num;
6085  if (letter >= 'a' && letter <= 'z')
6086  num = letter - 'a';
6087  else if (letter >= 'A' && letter <= 'Z')
6088  num = letter - 'A';
6089  else
6090  return false;
6091  return r->misa & BIT(num);
6092 }
6093 
6094 unsigned int riscv_xlen(const struct target *target)
6095 {
6096  RISCV_INFO(r);
6097  return r->xlen;
6098 }
6099 
6100 unsigned int riscv_vlenb(const struct target *target)
6101 {
6102  RISCV_INFO(r);
6103  return r->vlenb;
6104 }
6105 
6107 {
6108  RISCV_INFO(r);
6109  assert(r->get_hart_state);
6110  return r->get_hart_state(target, state);
6111 }
6112 
6113 static enum riscv_halt_reason riscv_halt_reason(struct target *target)
6114 {
6115  RISCV_INFO(r);
6116  if (target->state != TARGET_HALTED) {
6117  LOG_TARGET_ERROR(target, "Hart is not halted!");
6118  return RISCV_HALT_UNKNOWN;
6119  }
6120  return r->halt_reason(target);
6121 }
6122 
6123 unsigned int riscv_progbuf_size(struct target *target)
6124 {
6125  RISCV_INFO(r);
6126  return r->get_progbufsize(target);
6127 }
6128 
6129 int riscv_write_progbuf(struct target *target, unsigned int index, riscv_insn_t insn)
6130 {
6131  RISCV_INFO(r);
6132  return r->write_progbuf(target, index, insn);
6133 }
6134 
6136 {
6137  RISCV_INFO(r);
6138  return r->read_progbuf(target, index);
6139 }
6140 
6141 int riscv_execute_progbuf(struct target *target, uint32_t *cmderr)
6142 {
6143  RISCV_INFO(r);
6144  return r->execute_progbuf(target, cmderr);
6145 }
6146 
6147 void riscv_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
6148 {
6149  RISCV_INFO(r);
6150  r->fill_dmi_write(target, buf, a, d);
6151 }
6152 
6153 void riscv_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
6154 {
6155  RISCV_INFO(r);
6156  r->fill_dmi_read(target, buf, a);
6157 }
6158 
6159 void riscv_fill_dm_nop(const struct target *target, uint8_t *buf)
6160 {
6161  RISCV_INFO(r);
6162  r->fill_dm_nop(target, buf);
6163 }
6164 
6165 unsigned int riscv_get_dmi_address_bits(const struct target *target)
6166 {
6167  RISCV_INFO(r);
6168  return r->get_dmi_address_bits(target);
6169 }
6170 
6171 static int check_if_trigger_exists(struct target *target, unsigned int index)
6172 {
6173  /* If we can't write tselect, then this hart does not support triggers. */
6176  riscv_reg_t tselect_rb;
6177  if (riscv_reg_get(target, &tselect_rb, GDB_REGNO_TSELECT) != ERROR_OK)
6178  return ERROR_FAIL;
6179  /* Mask off the top bit, which is used as tdrmode in legacy RISC-V Debug Spec
6180  * (old revisions of v0.11 spec). */
6181  tselect_rb &= ~(1ULL << (riscv_xlen(target) - 1));
6182  if (tselect_rb != index)
6184  return ERROR_OK;
6185 }
6186 
6192 static int get_trigger_types(struct target *target, unsigned int *trigger_tinfo,
6193  riscv_reg_t tdata1)
6194 {
6195  assert(trigger_tinfo);
6196  riscv_reg_t tinfo;
6197  if (riscv_reg_get(target, &tinfo, GDB_REGNO_TINFO) == ERROR_OK) {
6198  /* tinfo.INFO == 1: trigger doesn’t exist
6199  * tinfo == 0 or tinfo.INFO != 1 and tinfo LSB is set: invalid tinfo */
6200  if (tinfo == 0 || tinfo & 0x1)
6202  *trigger_tinfo = tinfo;
6203  return ERROR_OK;
6204  }
6205  const unsigned int type = get_field(tdata1, CSR_TDATA1_TYPE(riscv_xlen(target)));
6206  if (type == 0)
6208  *trigger_tinfo = 1 << type;
6209  return ERROR_OK;
6210 }
6211 
6213 {
6214  bool dmode_is_set = false;
6215  switch (get_field(tdata1, CSR_TDATA1_TYPE(riscv_xlen(target)))) {
6217  /* On these older cores we don't support software using
6218  * triggers. */
6219  dmode_is_set = true;
6220  break;
6222  dmode_is_set = tdata1 & CSR_MCONTROL_DMODE(riscv_xlen(target));
6223  break;
6225  dmode_is_set = tdata1 & CSR_MCONTROL6_DMODE(riscv_xlen(target));
6226  break;
6228  dmode_is_set = tdata1 & CSR_ICOUNT_DMODE(riscv_xlen(target));
6229  break;
6231  dmode_is_set = tdata1 & CSR_ITRIGGER_DMODE(riscv_xlen(target));
6232  break;
6234  dmode_is_set = tdata1 & CSR_ETRIGGER_DMODE(riscv_xlen(target));
6235  break;
6236  }
6237  if (!dmode_is_set)
6238  /* Nothing to do */
6239  return ERROR_OK;
6241 }
6242 
6251 {
6252  RISCV_INFO(r);
6253 
6254  if (r->triggers_enumerated)
6255  return ERROR_OK;
6256 
6257  if (target->state != TARGET_HALTED) {
6258  LOG_TARGET_ERROR(target, "Unable to enumerate triggers: target not halted.");
6259  return ERROR_TARGET_NOT_HALTED;
6260  }
6261 
6262  riscv_reg_t orig_tselect;
6263  int result = riscv_reg_get(target, &orig_tselect, GDB_REGNO_TSELECT);
6264  /* If tselect is not readable, the trigger module is likely not
6265  * implemented. */
6266  if (result != ERROR_OK) {
6267  LOG_TARGET_INFO(target, "Cannot access tselect register. "
6268  "Assuming that triggers are not implemented.");
6269  r->triggers_enumerated = true;
6270  r->trigger_count = 0;
6271  free(r->reserved_triggers);
6272  r->reserved_triggers = NULL;
6273  return ERROR_OK;
6274  }
6275 
6276  /* Obtaining tinfo.version value once.
6277  * No need to enumerate per-trigger.
6278  * See https://github.com/riscv/riscv-debug-spec/pull/1081.
6279  */
6280  riscv_reg_t tinfo;
6281  if (riscv_reg_get(target, &tinfo, GDB_REGNO_TINFO) == ERROR_OK) {
6282  r->tinfo_version = get_field(tinfo, CSR_TINFO_VERSION);
6283  LOG_TARGET_DEBUG(target, "Trigger tinfo.version = %d.", r->tinfo_version);
6284  } else {
6285  r->tinfo_version = RISCV_TINFO_VERSION_UNKNOWN;
6286  LOG_TARGET_DEBUG(target, "Trigger tinfo.version is unknown.");
6287  }
6288 
6289  unsigned int t = 0;
6290  for (; t < ARRAY_SIZE(r->trigger_tinfo); ++t) {
6291  result = check_if_trigger_exists(target, t);
6292  if (result == ERROR_FAIL)
6293  return ERROR_FAIL;
6295  break;
6296 
6297  riscv_reg_t tdata1;
6298  if (riscv_reg_get(target, &tdata1, GDB_REGNO_TDATA1) != ERROR_OK)
6299  return ERROR_FAIL;
6300 
6301  result = get_trigger_types(target, &r->trigger_tinfo[t], tdata1);
6302  if (result == ERROR_FAIL)
6303  return ERROR_FAIL;
6305  break;
6306 
6307  LOG_TARGET_DEBUG(target, "Trigger %u: supported types (mask) = 0x%08x",
6308  t, r->trigger_tinfo[t]);
6309 
6310  if (disable_trigger_if_dmode(target, tdata1) != ERROR_OK)
6311  return ERROR_FAIL;
6312  }
6313 
6314  if (riscv_reg_set(target, GDB_REGNO_TSELECT, orig_tselect) != ERROR_OK)
6315  return ERROR_FAIL;
6316 
6317  r->triggers_enumerated = true;
6318  r->trigger_count = t;
6319  LOG_TARGET_INFO(target, "Found %d triggers", r->trigger_count);
6320  free(r->reserved_triggers);
6321  r->reserved_triggers = calloc(t, sizeof(*r->reserved_triggers));
6323  return ERROR_OK;
6324 }
6325 
6326 void riscv_add_bscan_tunneled_scan(struct jtag_tap *tap, const struct scan_field *field,
6328 {
6330 
6331  memset(ctxt->tunneled_dr, 0, sizeof(ctxt->tunneled_dr));
6333  ctxt->tunneled_dr[3].num_bits = 1;
6334  ctxt->tunneled_dr[3].out_value = bscan_one;
6335  ctxt->tunneled_dr[2].num_bits = 7;
6336  ctxt->tunneled_dr_width = field->num_bits;
6337  ctxt->tunneled_dr[2].out_value = &ctxt->tunneled_dr_width;
6338  /* for BSCAN tunnel, there is a one-TCK skew between shift in and shift out, so
6339  scanning num_bits + 1, and then will right shift the input field after executing the queues */
6340 
6341  ctxt->tunneled_dr[1].num_bits = field->num_bits + 1;
6342  ctxt->tunneled_dr[1].out_value = field->out_value;
6343  ctxt->tunneled_dr[1].in_value = field->in_value;
6344 
6345  ctxt->tunneled_dr[0].num_bits = 3;
6346  ctxt->tunneled_dr[0].out_value = bscan_zero;
6347  } else {
6348  /* BSCAN_TUNNEL_NESTED_TAP */
6349  ctxt->tunneled_dr[0].num_bits = 1;
6350  ctxt->tunneled_dr[0].out_value = bscan_one;
6351  ctxt->tunneled_dr[1].num_bits = 7;
6352  ctxt->tunneled_dr_width = field->num_bits;
6353  ctxt->tunneled_dr[1].out_value = &ctxt->tunneled_dr_width;
6354  /* for BSCAN tunnel, there is a one-TCK skew between shift in and shift out, so
6355  scanning num_bits + 1, and then will right shift the input field after executing the queues */
6356  ctxt->tunneled_dr[2].num_bits = field->num_bits + 1;
6357  ctxt->tunneled_dr[2].out_value = field->out_value;
6358  ctxt->tunneled_dr[2].in_value = field->in_value;
6359  ctxt->tunneled_dr[3].num_bits = 3;
6360  ctxt->tunneled_dr[3].out_value = bscan_zero;
6361  }
6363 }
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
unsigned char * base64_encode(const unsigned char *src, size_t len, size_t *out_len)
base64_encode - Base64 encode @src: Data to be encoded @len: Length of the data to be encoded @out_le...
Definition: base64.c:33
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
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:472
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
static void watchpoint_set(struct watchpoint *watchpoint, unsigned int number)
Definition: breakpoints.h:81
#define WATCHPOINT_IGNORE_DATA_VALUE_MASK
Definition: breakpoints.h:39
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:65
watchpoint_rw
Definition: breakpoints.h:22
@ 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:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:171
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define COMMAND_PARSE_ADDRESS(in, out)
Definition: command.h:455
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:533
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#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:445
#define ERROR_COMMAND_ARGUMENT_OVERFLOW
Definition: command.h:408
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ 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_MCONTROL_CHAIN
#define CSR_MCONTROL6_CHAIN
#define CSR_MCONTROL6_MATCH_NAPOT
#define CSR_ETRIGGER_VS
#define CSR_ICOUNT_ACTION_DEBUG_MODE
#define CSR_MCONTROL6_HIT0_AFTER
#define CSR_MCONTROL6_SIZE_ANY
#define CSR_ITRIGGER_HIT(XLEN)
#define CSR_MCONTROL_LOAD
#define CSR_ITRIGGER_ACTION_DEBUG_MODE
#define CSR_ETRIGGER_ACTION
#define CSR_DCSR_CAUSE_GROUP
#define CSR_MCONTROL6_CHAIN_DISABLED
#define CSR_MCONTROL6_TYPE(XLEN)
#define CSR_MCONTROL6_LOAD
#define CSR_TDATA1_TYPE_ICOUNT
#define CSR_MCONTROL6_HIT0_FALSE
#define CSR_ITRIGGER_ACTION
#define CSR_MCONTROL_ACTION_DEBUG_MODE
#define CSR_MCONTROL6_ACTION
#define CSR_MCONTROL6_MATCH_GE
#define DM_DMCONTROL
#define CSR_ETRIGGER_HIT(XLEN)
#define CSR_ETRIGGER_ACTION_DEBUG_MODE
#define CSR_ICOUNT_ACTION
#define CSR_MCONTROL_STORE
#define CSR_MCONTROL_CHAIN_DISABLED
#define CSR_MCONTROL_CHAIN_ENABLED
#define CSR_MCONTROL6_EXECUTE
#define CSR_MCONTROL_MATCH_LT
#define CSR_MCONTROL_MATCH_GE
#define CSR_ICOUNT_PENDING
#define CSR_ITRIGGER_DMODE(XLEN)
#define CSR_TDATA1_TYPE_MCONTROL6
#define CSR_MCONTROL6_STORE
#define CSR_MCONTROL_TYPE(XLEN)
#define VIRT_PRIV_PRV
#define CSR_MCONTROL_M
#define CSR_MCONTROL_EXECUTE
#define CSR_ETRIGGER_DMODE(XLEN)
#define CSR_MCONTROL_ACTION
#define CSR_MCONTROL6_M
#define CSR_MCONTROL6_S
#define CSR_MCONTROL_SIZELO_ANY
#define CSR_TINFO_VERSION_0
#define CSR_MCONTROL_MASKMAX(XLEN)
#define CSR_MCONTROL_U
#define CSR_MCONTROL6_U
#define CSR_MCONTROL6_MATCH_LT
#define CSR_MCONTROL6_VU
#define CSR_TDATA1_TYPE_ETRIGGER
#define CSR_ICOUNT_COUNT
#define CSR_MCONTROL6_MATCH_EQUAL
#define DM_DMCONTROL_DMACTIVE
#define CSR_ITRIGGER_VS
#define CSR_ETRIGGER_TYPE(XLEN)
#define CSR_ETRIGGER_S
#define CSR_TINFO_VERSION_1
#define CSR_MCONTROL_S
#define CSR_ETRIGGER_VU
#define CSR_MCONTROL6_MATCH
#define CSR_ITRIGGER_M
#define CSR_MCONTROL_DMODE(XLEN)
#define CSR_ICOUNT_M
#define CSR_ICOUNT_VU
#define CSR_ITRIGGER_NMI
#define DTM_DTMCS_VERSION_0_11
#define CSR_MCONTROL_SIZELO
#define CSR_ETRIGGER_M
#define CSR_MCONTROL6_DMODE(XLEN)
#define CSR_MCONTROL_MATCH_NAPOT
#define CSR_TDATA1_TYPE_MCONTROL
#define CSR_ICOUNT_TYPE(XLEN)
#define CSR_ITRIGGER_VU
#define CSR_MCONTROL6_HIT1
#define VIRT_PRIV_V
#define CSR_ITRIGGER_S
#define CSR_TDATA1_TYPE_LEGACY
#define CSR_TINFO_VERSION
#define CSR_ICOUNT_DMODE(XLEN)
#define CSR_ITRIGGER_TYPE(XLEN)
#define DTM_DTMCS_VERSION_1_0
#define CSR_MCONTROL6_HIT0
#define CSR_MCONTROL6_SIZE
#define DM_PROGBUF0
#define CSR_ETRIGGER_U
#define CSR_TDATA1_TYPE_ITRIGGER
#define CSR_ICOUNT_HIT
#define CSR_MCONTROL_SIZEHI
#define CSR_ITRIGGER_U
#define CSR_MCONTROL_MATCH
#define CSR_TDATA1_TYPE(XLEN)
#define DM_PROGBUF15
#define CSR_ICOUNT_VS
#define CSR_MCONTROL6_HIT0_BEFORE
#define CSR_ICOUNT_U
#define CSR_ICOUNT_S
#define CSR_MCONTROL6_CHAIN_ENABLED
#define CSR_MCONTROL_HIT
#define CSR_MCONTROL6_HIT0_IMMEDIATELY_AFTER
#define CSR_MCONTROL_MATCH_EQUAL
#define CSR_DCSR_CAUSE
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
const char * rs
Definition: ecos.c:480
#define PTE_W
Definition: encoding.h:298
#define MATCH_C_SDSP
Definition: encoding.h:578
#define INSN_FIELD_C_UIMM9SP_S
Definition: encoding.h:3312
#define INSN_FIELD_C_UIMM9SPLO
Definition: encoding.h:3309
#define INSN_FIELD_C_SREG1
Definition: encoding.h:3330
#define MSTATUS_MIE
Definition: encoding.h:16
#define INSN_FIELD_C_UIMM8LO
Definition: encoding.h:3286
#define INSN_FIELD_IMM12
Definition: encoding.h:3251
#define MATCH_C_LW
Definition: encoding.h:560
#define MSTATUS_SIE
Definition: encoding.h:14
#define MSTATUS_MPP
Definition: encoding.h:23
#define INSN_FIELD_C_UIMM10SP_S
Definition: encoding.h:3311
#define INSN_FIELD_C_UIMM7HI
Definition: encoding.h:3285
#define INSN_FIELD_C_UIMM7LO
Definition: encoding.h:3284
#define INSN_FIELD_C_UIMM8HI
Definition: encoding.h:3287
#define PTE_R
Definition: encoding.h:297
#define MATCH_C_SW
Definition: encoding.h:596
#define MSTATUS_UIE
Definition: encoding.h:13
#define MATCH_C_FSW
Definition: encoding.h:534
#define INSN_FIELD_C_UIMM8SPLO
Definition: encoding.h:3304
#define HGATP_MODE_SV57X4
Definition: encoding.h:266
#define INSN_FIELD_C_UIMM9SPHI
Definition: encoding.h:3310
#define PTE_PPN_SHIFT
Definition: encoding.h:310
#define MATCH_C_FSD
Definition: encoding.h:530
#define MATCH_LB
Definition: encoding.h:1264
#define INSN_FIELD_IMM12HI
Definition: encoding.h:3253
#define HGATP_MODE_SV32X4
Definition: encoding.h:263
#define INSN_FIELD_C_UIMM9HI
Definition: encoding.h:3289
#define SATP_MODE_SV32
Definition: encoding.h:248
#define SATP_MODE_SV39
Definition: encoding.h:249
#define HGATP_MODE_SV39X4
Definition: encoding.h:264
#define MATCH_C_LWSP
Definition: encoding.h:562
#define INSN_FIELD_C_UIMM8SPHI
Definition: encoding.h:3305
#define MATCH_C_LDSP
Definition: encoding.h:550
#define PTE_V
Definition: encoding.h:296
#define MATCH_SB
Definition: encoding.h:1408
#define MATCH_FLH
Definition: encoding.h:856
#define MATCH_C_FSDSP
Definition: encoding.h:532
#define INSN_FIELD_C_UIMM10SPLO
Definition: encoding.h:3307
#define INSN_FIELD_C_UIMM10SPHI
Definition: encoding.h:3308
#define HGATP_MODE_SV48X4
Definition: encoding.h:265
#define SATP_MODE_SV48
Definition: encoding.h:250
#define MSTATUS_HIE
Definition: encoding.h:15
#define MATCH_FSH
Definition: encoding.h:964
#define INSN_FIELD_C_UIMM8SP_S
Definition: encoding.h:3306
#define MATCH_C_FLW
Definition: encoding.h:526
#define INSN_FIELD_OPCODE
Definition: encoding.h:3270
#define HGATP_MODE_OFF
Definition: encoding.h:262
#define INSN_FIELD_C_UIMM9LO
Definition: encoding.h:3288
#define INSN_FIELD_RD
Definition: encoding.h:3235
#define SATP_MODE_OFF
Definition: encoding.h:247
#define SATP_MODE_SV57
Definition: encoding.h:251
#define MASK_C_LD
Definition: encoding.h:549
#define INSN_FIELD_RS1
Definition: encoding.h:3237
#define PTE_X
Definition: encoding.h:299
#define MATCH_C_SWSP
Definition: encoding.h:598
#define INSN_FIELD_IMM12LO
Definition: encoding.h:3255
#define MSTATUS_MPRV
Definition: encoding.h:26
#define INSN_FIELD_FUNCT3
Definition: encoding.h:3247
#define MATCH_C_FLDSP
Definition: encoding.h:524
#define PRV_M
Definition: encoding.h:236
#define MATCH_C_FLD
Definition: encoding.h:522
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 uint64_t set_field(uint64_t reg, uint64_t mask, uint64_t val)
Definition: field_helpers.h:21
static uint64_t field_value(uint64_t mask, uint64_t val)
Definition: field_helpers.h:37
static uint32_t get_field32(uint64_t reg, uint64_t mask)
Definition: field_helpers.h:14
static uint64_t get_field(uint64_t reg, uint64_t mask)
Definition: field_helpers.h:9
static uint16_t direction
Definition: ftdi.c:157
gdb_regno
Definition: gdb_regs.h:10
@ GDB_REGNO_DPC
Definition: gdb_regs.h:99
@ GDB_REGNO_S8
Definition: gdb_regs.h:37
@ GDB_REGNO_SATP
Definition: gdb_regs.h:105
@ GDB_REGNO_MSTATUS
Definition: gdb_regs.h:102
@ GDB_REGNO_S4
Definition: gdb_regs.h:33
@ GDB_REGNO_S11
Definition: gdb_regs.h:40
@ GDB_REGNO_S5
Definition: gdb_regs.h:34
@ GDB_REGNO_T5
Definition: gdb_regs.h:43
@ GDB_REGNO_T6
Definition: gdb_regs.h:44
@ GDB_REGNO_A4
Definition: gdb_regs.h:26
@ GDB_REGNO_GP
Definition: gdb_regs.h:14
@ GDB_REGNO_S7
Definition: gdb_regs.h:36
@ GDB_REGNO_A1
Definition: gdb_regs.h:23
@ GDB_REGNO_A5
Definition: gdb_regs.h:27
@ GDB_REGNO_TSELECT
Definition: gdb_regs.h:93
@ GDB_REGNO_T2
Definition: gdb_regs.h:18
@ GDB_REGNO_S1
Definition: gdb_regs.h:21
@ GDB_REGNO_T3
Definition: gdb_regs.h:41
@ GDB_REGNO_SP
Definition: gdb_regs.h:13
@ GDB_REGNO_VSATP
Definition: gdb_regs.h:106
@ GDB_REGNO_A6
Definition: gdb_regs.h:29
@ GDB_REGNO_FP
Definition: gdb_regs.h:20
@ GDB_REGNO_TDATA1
Definition: gdb_regs.h:94
@ GDB_REGNO_XPR31
Definition: gdb_regs.h:45
@ GDB_REGNO_A0
Definition: gdb_regs.h:22
@ GDB_REGNO_MEPC
Definition: gdb_regs.h:103
@ GDB_REGNO_A2
Definition: gdb_regs.h:24
@ GDB_REGNO_A7
Definition: gdb_regs.h:30
@ GDB_REGNO_RA
Definition: gdb_regs.h:12
@ GDB_REGNO_S9
Definition: gdb_regs.h:38
@ GDB_REGNO_PC
Definition: gdb_regs.h:47
@ GDB_REGNO_S0
Definition: gdb_regs.h:19
@ GDB_REGNO_T4
Definition: gdb_regs.h:42
@ GDB_REGNO_S10
Definition: gdb_regs.h:39
@ GDB_REGNO_PRIV
Definition: gdb_regs.h:112
@ GDB_REGNO_S2
Definition: gdb_regs.h:31
@ GDB_REGNO_TP
Definition: gdb_regs.h:15
@ GDB_REGNO_MCAUSE
Definition: gdb_regs.h:104
@ GDB_REGNO_TDATA2
Definition: gdb_regs.h:95
@ GDB_REGNO_T1
Definition: gdb_regs.h:17
@ GDB_REGNO_TINFO
Definition: gdb_regs.h:97
@ GDB_REGNO_S6
Definition: gdb_regs.h:35
@ GDB_REGNO_S3
Definition: gdb_regs.h:32
@ GDB_REGNO_T0
Definition: gdb_regs.h:16
@ GDB_REGNO_DCSR
Definition: gdb_regs.h:100
@ GDB_REGNO_A3
Definition: gdb_regs.h:25
@ GDB_REGNO_HGATP
Definition: gdb_regs.h:107
struct jim_nvp * jim_nvp_name2value_simple(const struct jim_nvp *p, const char *name)
Definition: jim-nvp.c:46
int jim_getopt_nvp(struct jim_getopt_info *goi, const struct jim_nvp *nvp, struct jim_nvp **puthere)
Remove argv[0] as NVP.
Definition: jim-nvp.c:237
void jim_getopt_nvp_unknown(struct jim_getopt_info *goi, const struct jim_nvp *nvptable, int hadprefix)
Create an appropriate error message for an NVP.
Definition: jim-nvp.c:253
int jim_nvp_name2value_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
Definition: jim-nvp.c:66
int jim_getopt_obj(struct jim_getopt_info *goi, Jim_Obj **puthere)
Remove argv[0] from the list.
Definition: jim-nvp.c:169
struct jim_nvp * jim_nvp_value2name_simple(const struct jim_nvp *p, int value)
Definition: jim-nvp.c:124
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1045
const char * jtag_tap_name(const struct jtag_tap *tap)
Definition: jtag/core.c:277
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:452
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:375
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:197
#define OOCD_LIST_HEAD(name)
Definition: list.h:50
static void list_add_tail(struct list_head *new, struct list_head *head)
Definition: list.h:203
#define list_for_each_entry_safe(p, n, h, field)
Definition: list.h:159
#define list_for_each_entry(p, h, field)
Definition: list.h:155
static void list_del(struct list_head *entry)
Definition: list.h:88
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:54
static void list_move(struct list_head *list, struct list_head *head)
Definition: list.h:209
void alive_sleep(uint64_t ms)
Definition: log.c:478
static int64_t start
Definition: log.c:38
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:167
#define LOG_USER(expr ...)
Definition: log.h:150
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:173
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:164
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_INFO
Definition: log.h:54
@ LOG_LVL_ERROR
Definition: log.h:52
#define t1
Definition: mips32.c:193
#define t2
Definition: mips32.c:194
static uint32_t ebreak(void) __attribute__((unused))
Definition: opcodes.h:336
static uint32_t ebreak_c(void) __attribute__((unused))
Definition: opcodes.h:341
uint8_t mask
Definition: parport.c:70
int riscv_program_insert(struct riscv_program *p, riscv_insn_t i)
Definition: program.c:197
int riscv_program_init(struct riscv_program *p, struct target *target)
Definition: program.c:17
int riscv_program_exec(struct riscv_program *p, struct target *t)
Add ebreak and execute the program.
Definition: program.c:42
struct reg * register_get_by_name(struct reg_cache *first, const char *name, bool search_all)
Definition: register.c:50
reg_type
Definition: register.h:19
#define MIN(a, b)
Definition: replacements.h:22
#define MAX(a, b)
Definition: replacements.h:25
struct target_type riscv011_target
Definition: riscv-011.c:2454
struct target_type riscv013_target
Definition: riscv-013.c:5078
static int riscv_reset_timeout_sec
Definition: riscv.c:177
static enum target_debug_reason derive_debug_reason_without_hitbit(const struct target *target, riscv_reg_t dpc)
Definition: riscv.c:2538
static int check_if_trigger_exists(struct target *target, unsigned int index)
Definition: riscv.c:6171
static int halt_finish(struct target *target)
Definition: riscv.c:2711
COMMAND_HELPER(ebreakx_deprecation_helper, enum riscv_priv_mode mode)
Definition: riscv.c:4928
static bool parse_csr_address(const char *reg_address_str, unsigned int *reg_addr)
Definition: riscv.c:4383
static struct scan_field _bscan_tunnel_data_register_select_dmi[]
Definition: riscv.c:73
static const virt2phys_info_t sv57
Definition: riscv.c:267
static int riscv_rw_memory(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv.c:3409
static struct tdata1_cache * tdata1_cache_alloc(struct list_head *tdata1_cache_head, riscv_reg_t tdata1)
Definition: riscv.c:984
struct scan_field select_idcode
Definition: riscv.c:53
static int verify_loadstore(struct target *target, const riscv_insn_t instruction, bool *is_read)
Definition: riscv.c:2191
static const virt2phys_info_t sv48x4
Definition: riscv.c:254
static int set_debug_reason(struct target *target, enum riscv_halt_reason halt_reason)
Set OpenOCD's generic debug reason from the RISC-V halt reason.
Definition: riscv.c:2567
static uint32_t get_opcode(const riscv_insn_t instruction)
Definition: riscv.c:2025
static struct jim_nvp nvp_ebreak_config_opts[]
Definition: riscv.c:511
unsigned int riscv_xlen(const struct target *target)
Definition: riscv.c:6094
static struct scan_field select_user4
Definition: riscv.c:67
static bscan_tunnel_type_t bscan_tunnel_type
Definition: riscv.c:58
static int riscv_halt_go_all_harts(struct target *target)
Definition: riscv.c:2663
struct scan_field select_dbus
Definition: riscv.c:48
static const virt2phys_info_t sv57x4
Definition: riscv.c:280
static int riscv_step_rtos_hart(struct target *target)
Definition: riscv.c:6062
mctrl6hitstatus
Definition: riscv.c:1771
@ M6_HIT_NOT_SUPPORTED
Definition: riscv.c:1773
@ M6_HIT_ERROR
Definition: riscv.c:1772
@ M6_HIT_BEFORE
Definition: riscv.c:1775
@ M6_HIT_AFTER
Definition: riscv.c:1776
@ M6_HIT_IMM_AFTER
Definition: riscv.c:1777
@ M6_NOT_HIT
Definition: riscv.c:1774
void riscv_add_bscan_tunneled_scan(struct jtag_tap *tap, const struct scan_field *field, riscv_bscan_tunneled_scan_context_t *ctxt)
Definition: riscv.c:6326
static int riscv_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: riscv.c:3748
static int get_trigger_types(struct target *target, unsigned int *trigger_tinfo, riscv_reg_t tdata1)
This function reads tinfo or tdata1, when reading tinfo fails, to determine trigger types supported b...
Definition: riscv.c:6192
static int maybe_add_trigger_t2_t6_for_bp(struct target *target, struct trigger *trigger, struct match_triggers_tdata1_fields fields)
Definition: riscv.c:1333
static int maybe_add_trigger_t2_t6_for_wp(struct target *target, struct trigger *trigger, struct match_triggers_tdata1_fields fields)
Definition: riscv.c:1237
unsigned int riscv_get_dmi_address_bits(const struct target *target)
Definition: riscv.c:6165
static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_id, bool *need_single_step)
Look at the trigger hit bits to find out which trigger is the reason we're halted.
Definition: riscv.c:1829
static const virt2phys_info_t sv32x4
Definition: riscv.c:202
static uint16_t get_offset_csqsp(riscv_insn_t instruction)
Definition: riscv.c:2005
static int get_loadstore_memoffset(struct target *target, const riscv_insn_t instruction, int16_t *memoffset)
Definition: riscv.c:2077
static enum @126 resume_order
static uint32_t bscan_tunnel_data_register_select_dmi_num_fields
Definition: riscv.c:122
bool riscv_supports_extension(const struct target *target, char letter)
Definition: riscv.c:6081
static uint16_t get_offset_cswsp(riscv_insn_t instruction)
Definition: riscv.c:1944
static int maybe_add_trigger_t2_t6(struct target *target, struct trigger *trigger, struct match_triggers_tdata1_fields fields)
Definition: riscv.c:1346
static const struct command_registration riscv_command_handlers[]
Definition: riscv.c:5895
static int riscv_command_timeout_sec_value
Definition: riscv.c:174
void select_dmi_via_bscan(struct jtag_tap *tap)
Definition: riscv.c:319
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:1579
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:3380
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:3394
int riscv_halt(struct target *target)
Definition: riscv.c:2716
static void create_wp_trigger_cache(struct target *target)
Definition: riscv.c:1020
static int riscv_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: riscv.c:1692
static void tdata2_cache_alloc(struct list_head *tdata2_cache_head, riscv_reg_t tdata2)
Definition: riscv.c:993
static int riscv_poll_hart(struct target *target, enum riscv_next_action *next_action)
Definition: riscv.c:3841
static int add_trigger(struct target *target, struct trigger *trigger)
Definition: riscv.c:1455
static uint16_t get_offset_cldsp(riscv_insn_t instruction)
Definition: riscv.c:1932
static int remove_trigger(struct target *target, int unique_id)
Definition: riscv.c:1658
static bool can_use_napot_match(struct trigger *trigger)
Definition: riscv.c:775
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:1547
static uint16_t get_offset_clqsp(riscv_insn_t instruction)
Definition: riscv.c:1994
static void trigger_from_watchpoint(struct trigger *trigger, const struct watchpoint *watchpoint)
Definition: riscv.c:1721
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:2922
unsigned int riscv_vlenb(const struct target *target)
Definition: riscv.c:6100
int riscv_get_hart_state(struct target *target, enum riscv_hart_state *state)
Definition: riscv.c:6106
static const virt2phys_info_t sv32
Definition: riscv.c:189
static int riscv_create_target(struct target *target)
Definition: riscv.c:492
static int riscv_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: riscv.c:663
static struct jim_nvp nvp_config_opts[]
Definition: riscv.c:619
static int old_or_new_riscv_poll(struct target *target)
Definition: riscv.c:2528
static int riscv_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: riscv.c:1607
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:3559
static void log_trigger_request_info(struct trigger_request_info trig_info)
Definition: riscv.c:979
static const riscv_reg_t mstatus_ie_mask
Definition: riscv.c:297
static int riscv_mmu(struct target *target, bool *enabled)
Definition: riscv.c:3023
static int try_setup_chained_match_triggers(struct target *target, struct trigger *trigger, struct trigger_request_info t1, struct trigger_request_info t2)
Definition: riscv.c:1104
static int jim_report_ebreak_config(const struct riscv_private_config *config, Jim_Interp *interp)
Definition: riscv.c:599
bool riscv_virt2phys_mode_is_hw(const struct target *target)
Definition: riscv.c:144
static int riscv_openocd_step_impl(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, int handle_callbacks)
Definition: riscv.c:4190
static void riscv_sample_buf_maybe_add_timestamp(struct target *target, bool before)
Definition: riscv.c:301
static int old_or_new_riscv_step_impl(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, int handle_callbacks)
Definition: riscv.c:2452
static uint16_t get_offset_clwsp(riscv_insn_t instruction)
These functions are needed to extract individual bits (for offset) from the instruction.
Definition: riscv.c:1920
static int riscv_virt2phys_v(struct target *target, target_addr_t virtual, target_addr_t *physical)
Definition: riscv.c:3197
#define RISCV_EBREAK_MODE_INVALID
Definition: riscv.c:520
unsigned int riscv_progbuf_size(struct target *target)
Definition: riscv.c:6123
static int parse_reg_ranges(struct list_head *ranges, const char *tcl_arg, const char *reg_type, unsigned int max_val)
Definition: riscv.c:4526
static const virt2phys_info_t sv39x4
Definition: riscv.c:228
#define RISCV_TRIGGER_HIT_NOT_FOUND
Definition: riscv.c:38
static int riscv_address_translate(struct target *target, const virt2phys_info_t *info, target_addr_t ppn, const virt2phys_info_t *extra_info, target_addr_t extra_ppn, target_addr_t virtual, target_addr_t *physical)
Definition: riscv.c:3102
struct tdata1_cache * tdata1_cache_search(struct list_head *tdata1_cache_head, riscv_reg_t find_tdata1)
Definition: riscv.c:1010
static const virt2phys_info_t sv48
Definition: riscv.c:241
static int set_trigger(struct target *target, unsigned int idx, riscv_reg_t tdata1, riscv_reg_t tdata2)
Definition: riscv.c:841
static int ebreak_config_to_tcl_dict(const struct riscv_private_config *config, char *buffer)
Obtain dcsr.ebreak* configuration as a Tcl dictionary.
Definition: riscv.c:577
static int jim_configure_ebreak(struct riscv_private_config *config, struct jim_getopt_info *goi)
Definition: riscv.c:528
uint8_t bscan_tunnel_ir_width
Definition: riscv.c:60
static struct scan_field _bscan_tunnel_nested_tap_select_dmi[]
Definition: riscv.c:96
struct tdata2_cache * tdata2_cache_search(struct list_head *tdata2_cache_head, riscv_reg_t find_tdata2)
Definition: riscv.c:1000
static enum mctrl6hitstatus check_mcontrol6_hit_status(struct target *target, riscv_reg_t tdata1, uint64_t hit_mask)
Definition: riscv.c:1780
struct target_type riscv_target
Definition: riscv.c:5926
static int riscv_effective_privilege_mode(struct target *target, int *v_mode, int *effective_mode)
Definition: riscv.c:2998
static struct match_triggers_tdata1_fields fill_match_triggers_tdata1_fields_t6(struct target *target, struct trigger *trigger)
Definition: riscv.c:1200
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:2838
static int resume_finish(struct target *target, bool debug_execution)
Definition: riscv.c:2899
struct scan_field select_dtmcontrol
Definition: riscv.c:43
static int check_virt_memory_access(struct target *target, target_addr_t address, uint32_t size, uint32_t count, bool is_write)
Definition: riscv.c:3365
#define DTMCONTROL_VERSION
Definition: riscv.c:34
static int disable_watchpoints(struct target *target, bool *wp_is_set)
Definition: riscv.c:2789
static const struct command_registration riscv_exec_command_handlers[]
Definition: riscv.c:5613
static void riscv_info_init(struct target *target, struct riscv_info *r)
Definition: riscv.c:5979
static uint8_t ir_dtmcontrol[4]
Definition: riscv.c:42
void riscv_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
Definition: riscv.c:6147
static bool wp_triggers_cache_search(struct target *target, unsigned int idx, riscv_reg_t tdata1, riscv_reg_t tdata2)
Definition: riscv.c:1048
int dtmcs_scan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr)
Definition: riscv.c:416
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 is_read)
Definition: riscv.c:3509
static int get_loadstore_membase_regno(struct target *target, const riscv_insn_t instruction, int *regid)
Definition: riscv.c:2036
static int riscv_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: riscv.c:3469
static void free_wp_triggers_cache(struct target *target)
Definition: riscv.c:697
static int bscan_tunnel_ir_id
Definition: riscv.c:61
static int maybe_add_trigger_t5(struct target *target, bool vs, bool vu, bool m, bool s, bool u, riscv_reg_t exception_codes, int unique_id)
Definition: riscv.c:1423
static uint16_t get_offset_clq(riscv_insn_t instruction)
Definition: riscv.c:1982
const char * riscv_virt2phys_mode_to_str(enum riscv_virt2phys_mode mode)
Definition: riscv.c:158
static uint8_t ir_user4[4]
Definition: riscv.c:66
static int riscv_target_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: riscv.c:2987
int riscv_openocd_poll(struct target *target)
Definition: riscv.c:4020
static int riscv_dmi_write(struct target *target, uint32_t dmi_address, uint32_t value)
Definition: riscv.c:4680
static unsigned int riscv_xlen_nonconst(struct target *target)
Definition: riscv.c:5913
#define DTMCONTROL
Definition: riscv.c:33
int riscv_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: riscv.c:1735
static int maybe_add_trigger_t3(struct target *target, bool vs, bool vu, bool m, bool s, bool u, bool pending, unsigned int count, int unique_id)
Definition: riscv.c:1358
static int oldriscv_poll(struct target *target)
Definition: riscv.c:2520
static int riscv_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
Definition: riscv.c:2355
static int dtmcs_scan_via_bscan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr)
Definition: riscv.c:330
uint32_t riscv_get_dmi_address(const struct target *target, uint32_t dm_address)
Definition: riscv.c:4653
int riscv_write_progbuf(struct target *target, unsigned int index, riscv_insn_t insn)
Definition: riscv.c:6129
static int try_setup_single_match_trigger(struct target *target, struct trigger *trigger, struct trigger_request_info trig_info)
Definition: riscv.c:1077
static uint16_t get_offset_cld(riscv_insn_t instruction)
Definition: riscv.c:1974
static int halt_go(struct target *target)
Definition: riscv.c:2693
static int riscv_assert_reset(struct target *target)
Definition: riscv.c:2765
riscv_insn_t riscv_read_progbuf(struct target *target, int index)
Definition: riscv.c:6135
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:1520
void riscv_fill_dm_nop(const struct target *target, uint8_t *buf)
Definition: riscv.c:6159
static uint32_t bscan_tunnel_nested_tap_select_dmi_num_fields
Definition: riscv.c:119
static int halt_prep(struct target *target)
Definition: riscv.c:2644
static int riscv_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: riscv.c:624
static uint8_t ir_dbus[4]
Definition: riscv.c:47
static unsigned int riscv_data_bits(struct target *target)
Definition: riscv.c:5918
COMMAND_HANDLER(riscv_set_command_timeout_sec)
Definition: riscv.c:4299
#define RISCV_HALT_GROUP_REPOLL_LIMIT
Definition: riscv.c:40
static uint16_t get_offset_csdsp(riscv_insn_t instruction)
Definition: riscv.c:1954
static int riscv_dmi_read(struct target *target, uint32_t *value, uint32_t address)
Definition: riscv.c:4662
static int enable_watchpoints(struct target *target, bool *wp_is_set)
Definition: riscv.c:2816
void riscv_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
Definition: riscv.c:6153
static struct jim_nvp nvp_ebreak_mode_opts[]
Definition: riscv.c:522
int riscv_execute_progbuf(struct target *target, uint32_t *cmderr)
Definition: riscv.c:6141
static void riscv_deinit_target(struct target *target)
Definition: riscv.c:716
static const char * riscv_get_gdb_arch(const struct target *target)
Definition: riscv.c:3497
#define BSCAN_TUNNEL_IR_WIDTH_NBITS
Definition: riscv.c:59
static int riscv_examine(struct target *target)
Definition: riscv.c:2472
bool riscv_virt2phys_mode_is_sw(const struct target *target)
Definition: riscv.c:151
static int riscv_resume_go_all_harts(struct target *target)
Definition: riscv.c:6017
static int parse_reg_ranges_impl(struct list_head *ranges, char *args, const char *reg_type, unsigned int max_val, char **const name_buffer)
Definition: riscv.c:4413
static int maybe_add_trigger_t1(struct target *target, struct trigger *trigger)
Definition: riscv.c:927
static int maybe_add_trigger_t4(struct target *target, bool vs, bool vu, bool nmi, bool m, bool s, bool u, riscv_reg_t interrupts, int unique_id)
Definition: riscv.c:1390
static int sample_memory(struct target *target)
Definition: riscv.c:3972
static int try_use_trigger_and_cache_result(struct target *target, unsigned int idx, riscv_reg_t tdata1, riscv_reg_t tdata2)
Definition: riscv.c:1063
static int find_next_free_trigger(struct target *target, int type, bool chained, unsigned int *idx)
Definition: riscv.c:785
static uint16_t get_offset_clw(riscv_insn_t instruction)
Definition: riscv.c:1964
static unsigned int count_trailing_ones(riscv_reg_t reg)
Definition: riscv.c:831
static int riscv_arch_state(struct target *target)
Definition: riscv.c:3567
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:3580
static struct scan_field * bscan_tunnel_nested_tap_select_dmi
Definition: riscv.c:118
static int disable_trigger_if_dmode(struct target *target, riscv_reg_t tdata1)
Definition: riscv.c:6212
static void wp_triggers_cache_add(struct target *target, unsigned int idx, riscv_reg_t tdata1, riscv_reg_t tdata2, int error_code)
Definition: riscv.c:1030
int riscv_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: riscv.c:1755
static int riscv_interrupts_restore(struct target *target, riscv_reg_t old_mstatus)
Definition: riscv.c:6045
static struct match_triggers_tdata1_fields fill_match_triggers_tdata1_fields_t2(struct target *target, struct trigger *trigger)
Definition: riscv.c:1165
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:3551
static int oldriscv_step(struct target *target, bool current, uint32_t address, bool handle_breakpoints)
Definition: riscv.c:2440
static struct target_type * get_target_type(struct target *target)
Definition: riscv.c:456
static struct scan_field * bscan_tunnel_data_register_select_dmi
Definition: riscv.c:121
int riscv_get_command_timeout_sec(void)
Definition: riscv.c:179
static int find_first_trigger_by_id(struct target *target, int unique_id)
Definition: riscv.c:820
static void trigger_from_breakpoint(struct trigger *trigger, const struct breakpoint *breakpoint)
Definition: riscv.c:762
static const uint8_t bscan_zero[4]
Definition: riscv.c:63
static int riscv_interrupts_disable(struct target *target, riscv_reg_t *old_mstatus)
Definition: riscv.c:6031
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:3483
riscv_next_action
Definition: riscv.c:3836
@ RPH_RESUME
Definition: riscv.c:3838
@ RPH_NONE
Definition: riscv.c:3837
@ RPH_REMAIN_HALTED
Definition: riscv.c:3839
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:1493
int riscv_enumerate_triggers(struct target *target)
Count triggers, and initialize trigger_count for each hart.
Definition: riscv.c:6250
riscv_cfg_opts
Definition: riscv.c:614
@ RISCV_CFG_EBREAK
Definition: riscv.c:615
@ RISCV_CFG_INVALID
Definition: riscv.c:616
int riscv_openocd_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv.c:4291
static struct riscv_private_config * alloc_default_riscv_private_config(void)
Definition: riscv.c:478
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:2880
static uint8_t ir_idcode[4]
Definition: riscv.c:52
static int riscv_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
Definition: riscv.c:3307
static int old_or_new_riscv_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv.c:2465
static enum riscv_halt_reason riscv_halt_reason(struct target *target)
Definition: riscv.c:6113
static const virt2phys_info_t sv39
Definition: riscv.c:215
static int riscv_deassert_reset(struct target *target)
Definition: riscv.c:2779
#define DBUS
Definition: riscv.c:36
@ RO_REVERSED
Definition: riscv.c:186
@ RO_NORMAL
Definition: riscv.c:185
static const uint8_t bscan_one[4]
Definition: riscv.c:64
static uint32_t get_rs1_c(riscv_insn_t instruction)
Decode rs1' register num for RVC.
Definition: riscv.c:2020
static bool riscv_mem_access_is_valid(const struct riscv_mem_access_args args)
Definition: riscv.h:148
#define RISCV_SAMPLE_BUF_TIMESTAMP_BEFORE
Definition: riscv.h:102
#define RISCV_PGSHIFT
Definition: riscv.h:31
#define RISCV_INFO(R)
Definition: riscv.h:426
static struct riscv_info * riscv_info(const struct target *target) __attribute__((unused))
Definition: riscv.h:421
void riscv_semihosting_init(struct target *target)
Initialize RISC-V semihosting.
@ RISCV_MEM_ACCESS_MAX_METHODS_NUM
Definition: riscv.h:60
@ RISCV_MEM_ACCESS_SYSBUS
Definition: riscv.h:58
@ RISCV_MEM_ACCESS_PROGBUF
Definition: riscv.h:57
@ RISCV_MEM_ACCESS_ABSTRACT
Definition: riscv.h:59
riscv_hart_state
Definition: riscv.h:88
@ RISCV_STATE_RUNNING
Definition: riscv.h:90
@ RISCV_STATE_UNAVAILABLE
Definition: riscv.h:92
@ RISCV_STATE_NON_EXISTENT
Definition: riscv.h:89
@ RISCV_STATE_HALTED
Definition: riscv.h:91
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_TINFO_VERSION_UNKNOWN
Definition: riscv.h:126
riscv_priv_mode
Definition: riscv.h:370
@ RISCV_MODE_M
Definition: riscv.h:371
@ RISCV_MODE_U
Definition: riscv.h:373
@ N_RISCV_MODE
Definition: riscv.h:376
@ RISCV_MODE_VU
Definition: riscv.h:375
@ RISCV_MODE_VS
Definition: riscv.h:374
@ RISCV_MODE_S
Definition: riscv.h:372
#define RISCV_PGSIZE
Definition: riscv.h:32
#define RISCV_SAMPLE_BUF_TIMESTAMP_AFTER
Definition: riscv.h:103
uint64_t riscv_reg_t
Definition: riscv.h:46
#define RISCV_PGOFFSET(addr)
Definition: riscv.h:34
#define RISCV_PGBASE(addr)
Definition: riscv.h:33
#define RISCV_COMMON_MAGIC
Definition: riscv.h:18
static bool riscv_mem_access_is_write(const struct riscv_mem_access_args args)
Definition: riscv.h:161
#define DEFAULT_COMMAND_TIMEOUT_SEC
Definition: riscv.h:25
bscan_tunnel_type_t
Definition: riscv.h:441
@ BSCAN_TUNNEL_NESTED_TAP
Definition: riscv.h:441
@ BSCAN_TUNNEL_DATA_REGISTER
Definition: riscv.h:441
static struct riscv_private_config * riscv_private_config(const struct target *target)
Definition: riscv.h:384
@ YNM_MAYBE
Definition: riscv.h:51
#define RISCV_SATP_MODE(xlen)
Definition: riscv.h:27
#define RISCV_HGATP_MODE(xlen)
Definition: riscv.h:29
uint32_t riscv_insn_t
Definition: riscv.h:47
riscv_halt_reason
Definition: riscv.h:71
@ RISCV_HALT_INTERRUPT
Definition: riscv.h:72
@ RISCV_HALT_SINGLESTEP
Definition: riscv.h:74
@ RISCV_HALT_EBREAK
Definition: riscv.h:73
@ RISCV_HALT_UNKNOWN
Definition: riscv.h:76
@ RISCV_HALT_ERROR
Definition: riscv.h:78
@ RISCV_HALT_GROUP
Definition: riscv.h:77
@ RISCV_HALT_TRIGGER
Definition: riscv.h:75
#define DTM_DTMCS_VERSION_UNKNOWN
Definition: riscv.h:125
#define RISCV_HGATP_PPN(xlen)
Definition: riscv.h:30
riscv_virt2phys_mode
Definition: riscv.h:63
@ RISCV_VIRT2PHYS_MODE_HW
Definition: riscv.h:64
@ RISCV_VIRT2PHYS_MODE_OFF
Definition: riscv.h:66
@ RISCV_VIRT2PHYS_MODE_SW
Definition: riscv.h:65
@ RISCV_ISRMASK_OFF
Definition: riscv.h:83
@ RISCV_ISRMASK_STEPONLY
Definition: riscv.h:85
int riscv_reg_set(struct target *target, enum gdb_regno regid, riscv_reg_t value)
This function is used to change the value of a register.
Definition: riscv_reg.c:918
void riscv_reg_cache_invalidate_all(struct target *target)
Invalidate all registers - forget their cached register values.
Definition: riscv_reg.c:899
const char * riscv_reg_gdb_regno_name(const struct target *target, enum gdb_regno regno)
This file describes the register cache interface available to the RISC-V target.
Definition: riscv_reg.c:171
int riscv_reg_flush_all(struct target *target)
Write all dirty registers to the target.
Definition: riscv_reg.c:776
int riscv_reg_get(struct target *target, riscv_reg_t *value, enum gdb_regno regid)
This function is used to get the value of a register.
Definition: riscv_reg.c:952
void riscv_reg_free_all(struct target *target)
Free register cache and associated structures.
Definition: riscv_reg.c:759
bool riscv_reg_cache_any_dirty(const struct target *target, int log_level)
Check whether there are any dirty registers in the OpenOCD's register cache.
Definition: riscv_reg.c:880
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
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:150
#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
struct breakpoint * next
Definition: breakpoints.h:34
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:239
A TCL -ish GetOpt like code.
Definition: jim-nvp.h:136
Jim_Interp * interp
Definition: jim-nvp.h:137
bool is_configure
Definition: jim-nvp.h:140
Jim_Obj *const * argv
Definition: jim-nvp.h:139
Name Value Pairs, aka: NVP.
Definition: jim-nvp.h:60
const char * name
Definition: jim-nvp.h:61
int value
Definition: jim-nvp.h:62
Definition: jtag.h:101
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
Definition: list.h:41
struct match_triggers_tdata1_fields::@127 size
struct match_triggers_tdata1_fields::@128 chain
struct match_triggers_tdata1_fields::@129 match
char * name
Definition: riscv.h:122
uint16_t low
Definition: riscv.h:121
int(* get)(struct reg *reg)
Definition: register.h:152
int(* set)(struct reg *reg, uint8_t *buf)
Definition: register.h:153
unsigned int num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
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 valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
uint8_t * value
Definition: register.h:122
uint32_t number
Definition: register.h:115
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:395
void * version_specific
Definition: riscv.h:174
enum riscv_virt2phys_mode virt2phys_mode
Definition: riscv.h:226
bool wp_allow_equality_match_trigger
Definition: riscv.h:363
struct list_head expose_custom
Definition: riscv.h:347
int64_t trigger_unique_id[RISCV_MAX_HWBPS]
Definition: riscv.h:219
int xlen
Definition: riscv.h:180
bool prepped
Definition: riscv.h:235
unsigned int num_enabled_mem_access_methods
Definition: riscv.h:335
enum riscv_isrmasking_mode isrmask_mode
Definition: riscv.h:244
unsigned int dtm_version
Definition: riscv.h:171
unsigned int halt_group_repoll_count
Definition: riscv.h:242
struct list_head expose_csr
Definition: riscv.h:343
enum yes_no_maybe vsew64_supported
Definition: riscv.h:359
bool autofence
Definition: riscv.h:367
unsigned int common_magic
Definition: riscv.h:169
bool mem_access_warn[RISCV_MEM_ACCESS_MAX_METHODS_NUM]
Definition: riscv.h:339
bool wp_allow_ge_lt_trigger
Definition: riscv.h:365
enum riscv_mem_access_method mem_access_methods[RISCV_MEM_ACCESS_MAX_METHODS_NUM]
Definition: riscv.h:333
bool wp_allow_napot_trigger
Definition: riscv.h:364
struct list_head hide_csr
Definition: riscv.h:351
uint8_t * read_buffer
Definition: riscv.h:140
const uint8_t * write_buffer
Definition: riscv.h:139
target_addr_t address
Definition: riscv.h:137
uint32_t count
Definition: riscv.h:143
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
bool is_active
A flag reporting whether semihosting is active.
struct list_head lh
Definition: target.h:226
struct target * target
Definition: target.h:227
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
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:231
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:249
int(* halt)(struct target *target)
Definition: target_type.h:43
int(* assert_reset)(struct target *target)
Definition: target_type.h:64
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:224
int(* virt2phys)(struct target *target, target_addr_t address, target_addr_t *physical)
Definition: target_type.h:254
Definition: target.h:119
struct semihosting * semihosting
Definition: target.h:222
struct jtag_tap * tap
Definition: target.h:122
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
void * private_config
Definition: target.h:175
struct reg_cache * reg_cache
Definition: target.h:168
struct list_head * smp_targets
Definition: target.h:201
struct breakpoint * breakpoints
Definition: target.h:169
unsigned int smp
Definition: target.h:200
struct target_type * type
Definition: target.h:120
struct watchpoint * watchpoints
Definition: target.h:170
void * arch_info
Definition: target.h:174
struct list_head elem_tdata1
Definition: riscv.c:141
struct list_head tdata2_cache_head
Definition: riscv.c:140
riscv_reg_t tdata1
Definition: riscv.c:139
struct list_head elem_tdata2
Definition: riscv.c:134
riscv_reg_t tdata2
Definition: riscv.c:135
Definition: psoc6.c:83
riscv_reg_t tdata2
Definition: riscv.c:976
riscv_reg_t tdata1
Definition: riscv.c:975
Definition: riscv.c:124
bool is_write
Definition: riscv.c:129
uint64_t value
Definition: riscv.c:128
uint64_t address
Definition: riscv.c:125
uint32_t length
Definition: riscv.c:126
bool is_read
Definition: riscv.c:129
uint64_t mask
Definition: riscv.c:127
int unique_id
Definition: riscv.c:130
bool is_execute
Definition: riscv.c:129
const char * name
Definition: riscv.h:399
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:90
target_addr_t address
Definition: target.h:89
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1794
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2369
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2434
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:786
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:1288
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2090
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:270
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2148
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:1260
const char * debug_reason_name(const struct target *t)
Definition: target.c:257
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:468
void target_handle_md_output(struct command_invocation *cmd, struct target *target, target_addr_t address, unsigned int size, unsigned int count, const uint8_t *buffer, bool include_address)
Definition: target.c:3297
target_debug_reason
Definition: target.h:71
@ DBG_REASON_UNDEFINED
Definition: target.h:80
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_WATCHPOINT
Definition: target.h:74
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
target_register_class
Definition: target.h:113
@ REG_CLASS_GENERAL
Definition: target.h:115
@ REG_CLASS_ALL
Definition: target.h:114
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:817
static bool target_was_examined(const struct target *target)
Definition: target.h:443
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:285
@ TARGET_EVENT_HALTED
Definition: target.h:265
@ TARGET_EVENT_RESUMED
Definition: target.h:266
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:284
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:246
target_state
Definition: target.h:55
@ TARGET_RESET
Definition: target.h:59
@ TARGET_DEBUG_RUNNING
Definition: target.h:60
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_UNAVAILABLE
Definition: target.h:61
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:824
#define TARGET_DEFAULT_POLLING_INTERVAL
Definition: target.h:832
#define ERROR_TARGET_TIMEOUT
Definition: target.h:816
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:821
int64_t timeval_ms(void)
#define TARGET_ADDR_FMT
Definition: types.h:286
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
uint64_t target_addr_t
Definition: types.h:279
#define TARGET_PRIxADDR
Definition: types.h:284
static struct ublast_lowlevel low
static struct ublast_lowlevel_priv info
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22