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  struct target_type *tt = get_target_type(target);
3577  if (!tt)
3578  return ERROR_FAIL;
3579  assert(tt->arch_state);
3580  return tt->arch_state(target);
3581 }
3582 
3583 /* Algorithm must end with a software breakpoint instruction. */
3584 static int riscv_run_algorithm(struct target *target, int num_mem_params,
3585  struct mem_param *mem_params, int num_reg_params,
3586  struct reg_param *reg_params, target_addr_t entry_point,
3587  target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
3588 {
3589  RISCV_INFO(info);
3590 
3591  if (target->state != TARGET_HALTED) {
3592  LOG_TARGET_ERROR(target, "not halted (run target algo)");
3593  return ERROR_TARGET_NOT_HALTED;
3594  }
3595 
3596  /* Write memory parameters to the target memory */
3597  for (int i = 0; i < num_mem_params; i++) {
3598  if (mem_params[i].direction == PARAM_OUT ||
3599  mem_params[i].direction == PARAM_IN_OUT) {
3600  int retval = target_write_buffer(target, mem_params[i].address, mem_params[i].size, mem_params[i].value);
3601  if (retval != ERROR_OK) {
3602  LOG_TARGET_ERROR(target, "Couldn't write input mem param into the memory, addr=0x%" TARGET_PRIxADDR
3603  " size=0x%" PRIx32, mem_params[i].address, mem_params[i].size);
3604  return retval;
3605  }
3606  }
3607  }
3608 
3609  /* Save registers */
3610  struct reg *reg_pc = register_get_by_name(target->reg_cache, "pc", true);
3611  if (!reg_pc || reg_pc->type->get(reg_pc) != ERROR_OK)
3612  return ERROR_FAIL;
3613  uint64_t saved_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
3614  LOG_TARGET_DEBUG(target, "saved_pc=0x%" PRIx64, saved_pc);
3615 
3616  uint64_t saved_regs[32];
3617  for (int i = 0; i < num_reg_params; i++) {
3618  LOG_TARGET_DEBUG(target, "save %s", reg_params[i].reg_name);
3619  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
3620  if (!r) {
3621  LOG_TARGET_ERROR(target, "Couldn't find register named '%s'", reg_params[i].reg_name);
3622  return ERROR_FAIL;
3623  }
3624 
3625  if (r->size != reg_params[i].size) {
3626  LOG_TARGET_ERROR(target, "Register %s is %d bits instead of %d bits.",
3627  reg_params[i].reg_name, r->size, reg_params[i].size);
3628  return ERROR_FAIL;
3629  }
3630 
3631  if (r->number > GDB_REGNO_XPR31) {
3632  LOG_TARGET_ERROR(target, "Only GPRs can be use as argument registers.");
3633  return ERROR_FAIL;
3634  }
3635 
3636  if (r->type->get(r) != ERROR_OK)
3637  return ERROR_FAIL;
3638  saved_regs[r->number] = buf_get_u64(r->value, 0, r->size);
3639 
3640  if (reg_params[i].direction == PARAM_OUT || reg_params[i].direction == PARAM_IN_OUT) {
3641  if (r->type->set(r, reg_params[i].value) != ERROR_OK)
3642  return ERROR_FAIL;
3643  }
3644  }
3645 
3646  /* Disable Interrupts before attempting to run the algorithm. */
3647  riscv_reg_t current_mstatus;
3648  if (riscv_interrupts_disable(target, &current_mstatus) != ERROR_OK)
3649  return ERROR_FAIL;
3650 
3651  /* Run algorithm */
3652  LOG_TARGET_DEBUG(target, "resume at 0x%" TARGET_PRIxADDR, entry_point);
3653  if (riscv_resume(target, false, entry_point, false, true, true) != ERROR_OK)
3654  return ERROR_FAIL;
3655 
3656  int64_t start = timeval_ms();
3657  while (target->state != TARGET_HALTED) {
3658  LOG_TARGET_DEBUG(target, "poll()");
3659  int64_t now = timeval_ms();
3660  if (now - start > timeout_ms) {
3661  LOG_TARGET_ERROR(target, "Algorithm timed out after %" PRId64 " ms.", now - start);
3662  riscv_halt(target);
3664  enum gdb_regno regnums[] = {
3673  GDB_REGNO_PC,
3675  };
3676  for (unsigned int i = 0; i < ARRAY_SIZE(regnums); i++) {
3677  enum gdb_regno regno = regnums[i];
3678  riscv_reg_t reg_value;
3679  if (riscv_reg_get(target, &reg_value, regno) != ERROR_OK)
3680  break;
3681 
3682  LOG_TARGET_ERROR(target, "%s = 0x%" PRIx64, riscv_reg_gdb_regno_name(target, regno), reg_value);
3683  }
3684  return ERROR_TARGET_TIMEOUT;
3685  }
3686 
3687  int result = old_or_new_riscv_poll(target);
3688  if (result != ERROR_OK)
3689  return result;
3690  }
3691 
3692  /* TODO: The current hart id might have been changed in poll(). */
3693  /* if (riscv_select_current_hart(target) != ERROR_OK)
3694  return ERROR_FAIL; */
3695 
3696  if (reg_pc->type->get(reg_pc) != ERROR_OK)
3697  return ERROR_FAIL;
3698  uint64_t final_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
3699  if (exit_point && final_pc != exit_point) {
3700  LOG_TARGET_ERROR(target, "PC ended up at 0x%" PRIx64 " instead of 0x%"
3701  TARGET_PRIxADDR, final_pc, exit_point);
3702  return ERROR_FAIL;
3703  }
3704 
3705  /* Restore Interrupts */
3706  if (riscv_interrupts_restore(target, current_mstatus) != ERROR_OK)
3707  return ERROR_FAIL;
3708 
3709  /* Restore registers */
3710  uint8_t buf[8] = { 0 };
3711  buf_set_u64(buf, 0, info->xlen, saved_pc);
3712  if (reg_pc->type->set(reg_pc, buf) != ERROR_OK)
3713  return ERROR_FAIL;
3714 
3715  for (int i = 0; i < num_reg_params; i++) {
3716  if (reg_params[i].direction == PARAM_IN ||
3717  reg_params[i].direction == PARAM_IN_OUT) {
3718  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
3719  if (r->type->get(r) != ERROR_OK) {
3720  LOG_TARGET_ERROR(target, "get(%s) failed", r->name);
3721  return ERROR_FAIL;
3722  }
3723  buf_cpy(r->value, reg_params[i].value, reg_params[i].size);
3724  }
3725  LOG_TARGET_DEBUG(target, "restore %s", reg_params[i].reg_name);
3726  struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
3727  buf_set_u64(buf, 0, info->xlen, saved_regs[r->number]);
3728  if (r->type->set(r, buf) != ERROR_OK) {
3729  LOG_TARGET_ERROR(target, "set(%s) failed", r->name);
3730  return ERROR_FAIL;
3731  }
3732  }
3733 
3734  /* Read memory parameters from the target memory */
3735  for (int i = 0; i < num_mem_params; i++) {
3736  if (mem_params[i].direction == PARAM_IN ||
3737  mem_params[i].direction == PARAM_IN_OUT) {
3738  int retval = target_read_buffer(target, mem_params[i].address, mem_params[i].size,
3739  mem_params[i].value);
3740  if (retval != ERROR_OK) {
3741  LOG_TARGET_ERROR(target, "Couldn't read output mem param from the memory, "
3742  "addr=0x%" TARGET_PRIxADDR " size=0x%" PRIx32,
3743  mem_params[i].address, mem_params[i].size);
3744  return retval;
3745  }
3746  }
3747  }
3748 
3749  return ERROR_OK;
3750 }
3751 
3753  target_addr_t address, uint32_t count,
3754  uint32_t *checksum)
3755 {
3756  struct working_area *crc_algorithm;
3757  struct reg_param reg_params[2];
3758  int retval;
3759 
3760  LOG_TARGET_DEBUG(target, "address=0x%" TARGET_PRIxADDR "; count=0x%" PRIx32, address, count);
3761 
3762  static const uint8_t riscv32_crc_code[] = {
3763 #include "../../../contrib/loaders/checksum/riscv32_crc.inc"
3764  };
3765  static const uint8_t riscv64_crc_code[] = {
3766 #include "../../../contrib/loaders/checksum/riscv64_crc.inc"
3767  };
3768 
3769  static const uint8_t *crc_code;
3770 
3771  unsigned int xlen = riscv_xlen(target);
3772  unsigned int crc_code_size;
3773  if (xlen == 32) {
3774  crc_code = riscv32_crc_code;
3775  crc_code_size = sizeof(riscv32_crc_code);
3776  } else {
3777  crc_code = riscv64_crc_code;
3778  crc_code_size = sizeof(riscv64_crc_code);
3779  }
3780 
3781  if (count < crc_code_size * 4) {
3782  /* Don't use the algorithm for relatively small buffers. It's faster
3783  * just to read the memory. target_checksum_memory() will take care of
3784  * that if we fail. */
3785  return ERROR_FAIL;
3786  }
3787 
3788  retval = target_alloc_working_area(target, crc_code_size, &crc_algorithm);
3789  if (retval != ERROR_OK)
3790  return retval;
3791 
3792  if (crc_algorithm->address + crc_algorithm->size > address &&
3793  crc_algorithm->address < address + count) {
3794  /* Region to checksum overlaps with the work area we've been assigned.
3795  * Bail. (Would be better to manually checksum what we read there, and
3796  * use the algorithm for the rest.) */
3797  target_free_working_area(target, crc_algorithm);
3798  return ERROR_FAIL;
3799  }
3800 
3801  retval = target_write_buffer(target, crc_algorithm->address, crc_code_size,
3802  crc_code);
3803  if (retval != ERROR_OK) {
3804  LOG_TARGET_ERROR(target, "Failed to write code to " TARGET_ADDR_FMT ": %d",
3805  crc_algorithm->address, retval);
3806  target_free_working_area(target, crc_algorithm);
3807  return retval;
3808  }
3809 
3810  init_reg_param(&reg_params[0], "a0", xlen, PARAM_IN_OUT);
3811  init_reg_param(&reg_params[1], "a1", xlen, PARAM_OUT);
3812  buf_set_u64(reg_params[0].value, 0, xlen, address);
3813  buf_set_u64(reg_params[1].value, 0, xlen, count);
3814 
3815  /* 20 second timeout/megabyte */
3816  unsigned int timeout = 20000 * (1 + (count / (1024 * 1024)));
3817 
3818  retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
3819  crc_algorithm->address,
3820  0, /* Leave exit point unspecified because we don't know. */
3821  timeout, NULL);
3822 
3823  if (retval == ERROR_OK)
3824  *checksum = buf_get_u32(reg_params[0].value, 0, 32);
3825  else
3826  LOG_TARGET_ERROR(target, "Error executing RISC-V CRC algorithm.");
3827 
3828  destroy_reg_param(&reg_params[0]);
3829  destroy_reg_param(&reg_params[1]);
3830 
3831  target_free_working_area(target, crc_algorithm);
3832 
3833  LOG_TARGET_DEBUG(target, "checksum=0x%" PRIx32 ", result=%d", *checksum, retval);
3834 
3835  return retval;
3836 }
3837 
3838 /*** OpenOCD Helper Functions ***/
3839 
3844 };
3845 static int riscv_poll_hart(struct target *target, enum riscv_next_action *next_action)
3846 {
3847  RISCV_INFO(r);
3848 
3849  LOG_TARGET_DEBUG(target, "polling, target->state=%d", target->state);
3850 
3851  *next_action = RPH_NONE;
3852 
3853  enum riscv_hart_state previous_riscv_state = 0;
3854  enum target_state previous_target_state = target->state;
3855  switch (target->state) {
3856  case TARGET_UNKNOWN:
3857  /* Special case, handled further down. */
3858  previous_riscv_state = RISCV_STATE_UNAVAILABLE; /* Need to assign something. */
3859  break;
3860  case TARGET_RUNNING:
3861  previous_riscv_state = RISCV_STATE_RUNNING;
3862  break;
3863  case TARGET_HALTED:
3864  previous_riscv_state = RISCV_STATE_HALTED;
3865  break;
3866  case TARGET_RESET:
3867  previous_riscv_state = RISCV_STATE_HALTED;
3868  break;
3869  case TARGET_DEBUG_RUNNING:
3870  previous_riscv_state = RISCV_STATE_RUNNING;
3871  break;
3872  case TARGET_UNAVAILABLE:
3873  previous_riscv_state = RISCV_STATE_UNAVAILABLE;
3874  break;
3875  }
3876 
3877  /* If OpenOCD thinks we're running but this hart is halted then it's time
3878  * to raise an event. */
3879  enum riscv_hart_state state;
3881  return ERROR_FAIL;
3882 
3884  LOG_TARGET_ERROR(target, "Hart is non-existent!");
3885  return ERROR_FAIL;
3886  }
3887 
3888  if (state == RISCV_STATE_HALTED && timeval_ms() - r->last_activity > 100) {
3889  /* If we've been idle for a while, flush the register cache. Just in case
3890  * OpenOCD is going to be disconnected without shutting down cleanly. */
3892  return ERROR_FAIL;
3893  }
3894 
3895  if (target->state == TARGET_UNKNOWN || state != previous_riscv_state) {
3896  switch (state) {
3897  case RISCV_STATE_HALTED:
3898  if (previous_riscv_state == RISCV_STATE_UNAVAILABLE)
3899  LOG_TARGET_INFO(target, "became available (halted)");
3900 
3901  LOG_TARGET_DEBUG(target, " triggered a halt; previous_target_state=%d",
3902  previous_target_state);
3904  enum riscv_halt_reason halt_reason = riscv_halt_reason(target);
3905  if (set_debug_reason(target, halt_reason) != ERROR_OK)
3906  return ERROR_FAIL;
3907 
3908  if (halt_reason == RISCV_HALT_EBREAK) {
3909  int retval;
3910  /* Detect if this EBREAK is a semihosting request. If so, handle it. */
3911  switch (riscv_semihosting(target, &retval)) {
3912  case SEMIHOSTING_NONE:
3913  break;
3914  case SEMIHOSTING_WAITING:
3915  /* This hart should remain halted. */
3916  *next_action = RPH_REMAIN_HALTED;
3917  break;
3918  case SEMIHOSTING_HANDLED:
3919  /* This hart should be resumed, along with any other
3920  * harts that halted due to haltgroups. */
3921  *next_action = RPH_RESUME;
3922  return ERROR_OK;
3923  case SEMIHOSTING_ERROR:
3924  return retval;
3925  }
3926  }
3927 
3928  if (r->handle_became_halted &&
3929  r->handle_became_halted(target, previous_riscv_state) != ERROR_OK)
3930  return ERROR_FAIL;
3931 
3932  /* We shouldn't do the callbacks yet. What if
3933  * there are multiple harts that halted at the
3934  * same time? We need to set debug reason on each
3935  * of them before calling a callback, which is
3936  * going to figure out the "current thread". */
3937 
3938  r->halted_needs_event_callback = true;
3939  if (previous_target_state == TARGET_DEBUG_RUNNING)
3940  r->halted_callback_event = TARGET_EVENT_DEBUG_HALTED;
3941  else
3942  r->halted_callback_event = TARGET_EVENT_HALTED;
3943  break;
3944 
3945  case RISCV_STATE_RUNNING:
3946  if (previous_riscv_state == RISCV_STATE_UNAVAILABLE)
3947  LOG_TARGET_INFO(target, "became available (running)");
3948 
3949  LOG_TARGET_DEBUG(target, " triggered running");
3952  if (r->handle_became_running &&
3953  r->handle_became_running(target, previous_riscv_state) != ERROR_OK)
3954  return ERROR_FAIL;
3955  break;
3956 
3958  LOG_TARGET_DEBUG(target, " became unavailable");
3959  LOG_TARGET_INFO(target, "became unavailable.");
3961  if (r->handle_became_unavailable &&
3962  r->handle_became_unavailable(target, previous_riscv_state) != ERROR_OK)
3963  return ERROR_FAIL;
3964  break;
3965 
3967  LOG_TARGET_ERROR(target, "Hart is non-existent!");
3969  break;
3970  }
3971  }
3972 
3973  return ERROR_OK;
3974 }
3975 
3976 static int sample_memory(struct target *target)
3977 {
3978  RISCV_INFO(r);
3979 
3980  if (!r->sample_buf.buf || !r->sample_config.enabled)
3981  return ERROR_OK;
3982 
3983  LOG_TARGET_DEBUG(target, "buf used/size: %d/%d", r->sample_buf.used, r->sample_buf.size);
3984 
3985  uint64_t start = timeval_ms();
3987  int result = ERROR_OK;
3988  if (r->sample_memory) {
3989  result = r->sample_memory(target, &r->sample_buf, &r->sample_config,
3991  if (result != ERROR_NOT_IMPLEMENTED)
3992  goto exit;
3993  }
3994 
3995  /* Default slow path. */
3997  for (unsigned int i = 0; i < ARRAY_SIZE(r->sample_config.bucket); i++) {
3998  if (r->sample_config.bucket[i].enabled &&
3999  r->sample_buf.used + 1 + r->sample_config.bucket[i].size_bytes < r->sample_buf.size) {
4001  r->sample_buf.buf[r->sample_buf.used] = i;
4002  result = riscv_read_phys_memory(target,
4003  r->sample_config.bucket[i].address,
4004  r->sample_config.bucket[i].size_bytes, 1,
4005  r->sample_buf.buf + r->sample_buf.used + 1);
4006  if (result == ERROR_OK)
4007  r->sample_buf.used += 1 + r->sample_config.bucket[i].size_bytes;
4008  else
4009  goto exit;
4010  }
4011  }
4012  }
4013 
4014 exit:
4016  if (result != ERROR_OK) {
4017  LOG_TARGET_INFO(target, "Turning off memory sampling because it failed.");
4018  r->sample_config.enabled = false;
4019  }
4020  return result;
4021 }
4022 
4023 /*** OpenOCD Interface ***/
4025 {
4026  LOG_TARGET_DEBUG(target, "Polling all harts.");
4027 
4028  struct riscv_info *i = riscv_info(target);
4029 
4030  struct list_head *targets;
4031 
4032  OOCD_LIST_HEAD(single_target_list);
4033  struct target_list single_target_entry = {
4034  .lh = {NULL, NULL},
4035  .target = target
4036  };
4037 
4038  if (target->smp) {
4039  targets = target->smp_targets;
4040  } else {
4041  /* Make a list that just contains a single target, so we can
4042  * share code below. */
4043  list_add(&single_target_entry.lh, &single_target_list);
4044  targets = &single_target_list;
4045  }
4046 
4047  unsigned int should_remain_halted = 0;
4048  unsigned int should_resume = 0;
4049  unsigned int halted = 0;
4050  unsigned int running = 0;
4051  unsigned int cause_groups = 0;
4052  struct target_list *entry;
4053  foreach_smp_target(entry, targets) {
4054  struct target *t = entry->target;
4055  struct riscv_info *info = riscv_info(t);
4056 
4057  /* Clear here just in case there were errors and we never got to
4058  * check this flag further down. */
4059  info->halted_needs_event_callback = false;
4060 
4061  if (!target_was_examined(t))
4062  continue;
4063 
4064  enum riscv_next_action next_action;
4065  if (riscv_poll_hart(t, &next_action) != ERROR_OK)
4066  return ERROR_FAIL;
4067 
4068  switch (next_action) {
4069  case RPH_NONE:
4070  if (t->state == TARGET_HALTED)
4071  halted++;
4072  if (t->state == TARGET_RUNNING ||
4074  running++;
4075  break;
4076  case RPH_REMAIN_HALTED:
4077  should_remain_halted++;
4078  break;
4079  case RPH_RESUME:
4080  should_resume++;
4081  break;
4082  }
4083  }
4084 
4085  LOG_TARGET_DEBUG(target, "should_remain_halted=%d, should_resume=%d",
4086  should_remain_halted, should_resume);
4087  if (should_remain_halted && should_resume) {
4088  LOG_TARGET_WARNING(target, "%d harts should remain halted, and %d should resume.",
4089  should_remain_halted, should_resume);
4090  }
4091  if (should_remain_halted) {
4092  LOG_TARGET_DEBUG(target, "halt all; should_remain_halted=%d",
4093  should_remain_halted);
4094  riscv_halt(target);
4095  } else if (should_resume) {
4096  LOG_TARGET_DEBUG(target, "resume all");
4097  riscv_resume(target, true, 0, 0, 0, false);
4098  } else if (halted && running) {
4099  LOG_TARGET_DEBUG(target, "SMP group is in inconsistent state: %u halted, %u running",
4100  halted, running);
4101 
4102  /* The SMP group is in an inconsistent state - some harts in the group have halted
4103  * whereas others are running. The reasons for that (and corresponding
4104  * OpenOCD actions) could be:
4105  * 1) The targets are in the process of halting due to halt groups
4106  * but not all of them halted --> poll again so that the halt reason of every
4107  * hart can be accurately determined (e.g. semihosting).
4108  * 2) The targets do not support halt groups --> OpenOCD must halt
4109  * the remaining harts by a standard halt request.
4110  * 3) The hart states got out of sync for some other unknown reason (problem?). -->
4111  * Same as previous - try to halt the harts by a standard halt request
4112  * to get them back in sync. */
4113 
4114  /* Detect if the harts are just in the process of halting due to a halt group */
4115  foreach_smp_target(entry, targets)
4116  {
4117  struct target *t = entry->target;
4118  if (t->state == TARGET_HALTED) {
4119  riscv_reg_t dcsr;
4120  if (riscv_reg_get(t, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
4121  return ERROR_FAIL;
4123  cause_groups++;
4124  else
4125  /* This hart has halted due to something else than a halt group.
4126  * Don't continue checking the rest - exit early. */
4127  break;
4128  }
4129  }
4130  /* Condition: halted == cause_groups
4131  *
4132  * This condition indicates a paradox where:
4133  * - All currently halted harts show CSR_DCSR_CAUSE_GROUP
4134  * - However, no individual hart can be identified as the actual initiator of the halt condition
4135  *
4136  * Poll again so that the true halt reason can be discovered (e.g. CSR_DCSR_CAUSE_EBREAK) */
4137  if (halted == cause_groups) {
4138  LOG_TARGET_DEBUG(target, "The harts appear to just be in the process of halting due to a halt group.");
4140  /* Wait a little, then re-poll. */
4142  alive_sleep(10);
4143  LOG_TARGET_DEBUG(target, "Re-polling the state of the SMP group.");
4144  return riscv_openocd_poll(target);
4145  }
4146  /* We have already re-polled multiple times but the halt group is still inconsistent. */
4147  LOG_TARGET_DEBUG(target, "Re-polled the SMP group %d times it is still not in a consistent state.",
4149  }
4150 
4151  /* Halting the whole SMP group to bring it in sync. */
4152  LOG_TARGET_DEBUG(target, "halt all; halted=%d",
4153  halted);
4154  riscv_halt(target);
4155  } else {
4156  /* For targets that were discovered to be halted, call the
4157  * appropriate callback. */
4158  foreach_smp_target(entry, targets)
4159  {
4160  struct target *t = entry->target;
4161  struct riscv_info *info = riscv_info(t);
4162  if (info->halted_needs_event_callback) {
4163  target_call_event_callbacks(t, info->halted_callback_event);
4164  info->halted_needs_event_callback = false;
4165  }
4166  }
4167  }
4168 
4169  i->halt_group_repoll_count = 0;
4170 
4171  /* Call tick() for every hart. What happens in tick() is opaque to this
4172  * layer. The reason it's outside the previous loop is that at this point
4173  * the state of every hart has settled, so any side effects happening in
4174  * tick() won't affect the delicate poll() code. */
4175  foreach_smp_target(entry, targets) {
4176  struct target *t = entry->target;
4177  struct riscv_info *info = riscv_info(t);
4178  if (info->tick && info->tick(t) != ERROR_OK)
4179  return ERROR_FAIL;
4180  }
4181 
4182  /* Sample memory if any target is running. */
4183  foreach_smp_target(entry, targets) {
4184  struct target *t = entry->target;
4185  if (t->state == TARGET_RUNNING) {
4187  break;
4188  }
4189  }
4190 
4191  return ERROR_OK;
4192 }
4193 
4194 static int riscv_openocd_step_impl(struct target *target, bool current,
4195  target_addr_t address, bool handle_breakpoints, int handle_callbacks)
4196 {
4197  LOG_TARGET_DEBUG(target, "stepping hart");
4198 
4199  if (!current) {
4201  return ERROR_FAIL;
4202  }
4203 
4204  struct breakpoint *breakpoint = NULL;
4205  /* the front-end may request us not to handle breakpoints */
4206  if (handle_breakpoints) {
4207  if (current) {
4209  return ERROR_FAIL;
4210  }
4213  return ERROR_FAIL;
4214  }
4215 
4217  return ERROR_FAIL;
4218 
4219  RISCV_INFO(r);
4220  bool *wps_to_enable = calloc(r->trigger_count, sizeof(*wps_to_enable));
4221  if (!wps_to_enable) {
4222  LOG_ERROR("Out of memory");
4223  return ERROR_FAIL;
4224  }
4225 
4226  if (disable_watchpoints(target, wps_to_enable) != ERROR_OK) {
4227  LOG_TARGET_ERROR(target, "Failed to temporarily disable "
4228  "watchpoints before single-step.");
4229  free(wps_to_enable);
4230  return ERROR_FAIL;
4231  }
4232 
4233  bool success = true;
4234  riscv_reg_t current_mstatus;
4235  RISCV_INFO(info);
4236 
4237  if (info->isrmask_mode == RISCV_ISRMASK_STEPONLY) {
4238  /* Disable Interrupts before stepping. */
4239  if (riscv_interrupts_disable(target, &current_mstatus) != ERROR_OK) {
4240  success = false;
4241  LOG_TARGET_ERROR(target, "Unable to disable interrupts.");
4242  goto _exit;
4243  }
4244  }
4245 
4247  success = false;
4248  LOG_TARGET_ERROR(target, "Unable to step rtos hart.");
4249  }
4250 
4252  /* If this happens, it means there is a bug in the previous
4253  * register-flushing algorithm: not all registers were flushed
4254  * back to the target prior to single-step. */
4256  "BUG: registers should have been flushed by this point.");
4257  }
4258 
4260 
4261  if (info->isrmask_mode == RISCV_ISRMASK_STEPONLY)
4262  if (riscv_interrupts_restore(target, current_mstatus) != ERROR_OK) {
4263  success = false;
4264  LOG_TARGET_ERROR(target, "Unable to restore interrupts.");
4265  }
4266 
4267 _exit:
4268  if (enable_watchpoints(target, wps_to_enable) != ERROR_OK) {
4269  success = false;
4270  LOG_TARGET_ERROR(target, "Failed to re-enable watchpoints "
4271  "after single-step.");
4272  }
4273 
4274  free(wps_to_enable);
4275 
4277  success = false;
4278  LOG_TARGET_ERROR(target, "Unable to restore the disabled breakpoint.");
4279  }
4280 
4281  if (success) {
4283  if (handle_callbacks)
4285 
4288  if (handle_callbacks)
4290  }
4291 
4292  return success ? ERROR_OK : ERROR_FAIL;
4293 }
4294 
4295 int riscv_openocd_step(struct target *target, bool current,
4296  target_addr_t address, bool handle_breakpoints)
4297 {
4298  return riscv_openocd_step_impl(target, current, address, handle_breakpoints,
4299  true /* handle_callbacks */);
4300 }
4301 
4302 /* Command Handlers */
4303 COMMAND_HANDLER(riscv_set_command_timeout_sec)
4304 {
4305  if (CMD_ARGC != 1)
4307 
4308  int timeout = atoi(CMD_ARGV[0]);
4309  if (timeout <= 0) {
4310  LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
4311  return ERROR_FAIL;
4312  }
4313 
4315 
4316  return ERROR_OK;
4317 }
4318 
4319 COMMAND_HANDLER(riscv_set_reset_timeout_sec)
4320 {
4321  LOG_WARNING("The command 'riscv set_reset_timeout_sec' is deprecated! Please, use 'riscv set_command_timeout_sec'.");
4322  if (CMD_ARGC != 1)
4324 
4325  int timeout = atoi(CMD_ARGV[0]);
4326  if (timeout <= 0) {
4327  LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
4328  return ERROR_FAIL;
4329  }
4330 
4332  return ERROR_OK;
4333 }
4334 
4335 COMMAND_HANDLER(riscv_set_mem_access)
4336 {
4338  RISCV_INFO(r);
4339  int progbuf_cnt = 0;
4340  int sysbus_cnt = 0;
4341  int abstract_cnt = 0;
4342 
4343  if (CMD_ARGC < 1 || CMD_ARGC > RISCV_MEM_ACCESS_MAX_METHODS_NUM) {
4344  command_print(CMD, "Command takes 1 to %d parameters",
4347  }
4348 
4349  /* Check argument validity */
4350  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4351  if (strcmp("progbuf", CMD_ARGV[i]) == 0) {
4352  progbuf_cnt++;
4353  } else if (strcmp("sysbus", CMD_ARGV[i]) == 0) {
4354  sysbus_cnt++;
4355  } else if (strcmp("abstract", CMD_ARGV[i]) == 0) {
4356  abstract_cnt++;
4357  } else {
4358  LOG_ERROR("Unknown argument '%s'. "
4359  "Must be one of: 'progbuf', 'sysbus' or 'abstract'.", CMD_ARGV[i]);
4361  }
4362  }
4363  if (progbuf_cnt > 1 || sysbus_cnt > 1 || abstract_cnt > 1) {
4364  LOG_ERROR("Syntax error - duplicate arguments to `riscv set_mem_access`.");
4366  }
4367 
4368  /* Args are valid, store them */
4369  r->num_enabled_mem_access_methods = CMD_ARGC;
4370  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4371  if (strcmp("progbuf", CMD_ARGV[i]) == 0)
4372  r->mem_access_methods[i] = RISCV_MEM_ACCESS_PROGBUF;
4373  else if (strcmp("sysbus", CMD_ARGV[i]) == 0)
4374  r->mem_access_methods[i] = RISCV_MEM_ACCESS_SYSBUS;
4375  else if (strcmp("abstract", CMD_ARGV[i]) == 0)
4376  r->mem_access_methods[i] = RISCV_MEM_ACCESS_ABSTRACT;
4377  }
4378 
4379  /* Reset warning flags */
4380  for (size_t i = 0; i < RISCV_MEM_ACCESS_MAX_METHODS_NUM; ++i)
4381  r->mem_access_warn[i] = true;
4382 
4383  return ERROR_OK;
4384 }
4385 
4386 
4387 static bool parse_csr_address(const char *reg_address_str, unsigned int *reg_addr)
4388 {
4389  *reg_addr = -1;
4390  /* skip initial spaces */
4391  while (isspace((unsigned char)reg_address_str[0]))
4392  ++reg_address_str;
4393  /* try to detect if string starts with 0x or 0X */
4394  bool is_hex_address = strncmp(reg_address_str, "0x", 2) == 0 ||
4395  strncmp(reg_address_str, "0X", 2) == 0;
4396 
4397  unsigned int scanned_chars;
4398  if (is_hex_address) {
4399  reg_address_str += 2;
4400  if (sscanf(reg_address_str, "%x%n", reg_addr, &scanned_chars) != 1)
4401  return false;
4402  } else {
4403  /* If we are here and register address string starts with zero, this is
4404  * an indication that most likely user has an incorrect input because:
4405  * - decimal numbers typically do not start with "0"
4406  * - octals are not supported by our interface
4407  * - hexadecimal numbers should have "0x" prefix
4408  * Thus such input is rejected. */
4409  if (reg_address_str[0] == '0' && strlen(reg_address_str) > 1)
4410  return false;
4411  if (sscanf(reg_address_str, "%u%n", reg_addr, &scanned_chars) != 1)
4412  return false;
4413  }
4414  return scanned_chars == strlen(reg_address_str);
4415 }
4416 
4417 static int parse_reg_ranges_impl(struct list_head *ranges, char *args,
4418  const char *reg_type, unsigned int max_val, char ** const name_buffer)
4419 {
4420  /* For backward compatibility, allow multiple parameters within one TCL
4421  * argument, separated by ',' */
4422  for (char *arg = strtok(args, ","); arg; arg = strtok(NULL, ",")) {
4423  unsigned int low = 0;
4424  unsigned int high = 0;
4425  char *name = NULL;
4426 
4427  char *dash = strchr(arg, '-');
4428  char *equals = strchr(arg, '=');
4429 
4430  if (!dash && !equals) {
4431  /* Expecting single register number. */
4432  if (!parse_csr_address(arg, &low)) {
4433  LOG_ERROR("Failed to parse single register number from '%s'.", arg);
4435  }
4436  } else if (dash && !equals) {
4437  /* Expecting register range - two numbers separated by a dash: ##-## */
4438  *dash = '\0';
4439  if (!parse_csr_address(arg, &low)) {
4440  LOG_ERROR("Failed to parse '%s' - not a valid decimal or hexadecimal number.",
4441  arg);
4443  }
4444  const char *high_num_in = dash + 1;
4445  if (!parse_csr_address(high_num_in, &high)) {
4446  LOG_ERROR("Failed to parse '%s' - not a valid decimal or hexadecimal number.",
4447  high_num_in);
4449  }
4450  if (high < low) {
4451  LOG_ERROR("Incorrect range encountered [%u, %u].", low, high);
4452  return ERROR_FAIL;
4453  }
4454  } else if (!dash && equals) {
4455  /* Expecting single register number with textual name specified: ##=name */
4456  *equals = '\0';
4457  if (!parse_csr_address(arg, &low)) {
4458  LOG_ERROR("Failed to parse '%s' - not a valid decimal or hexadecimal number.",
4459  arg);
4461  }
4462 
4463  const char * const reg_name_in = equals + 1;
4464  const size_t reg_type_len = strlen(reg_type);
4465  /* format is: <reg_type>_<reg_name_in>\0 */
4466  *name_buffer = calloc(1, strlen(reg_name_in) + reg_type_len + 2);
4467  name = *name_buffer;
4468  if (!name) {
4469  LOG_ERROR("Out of memory");
4470  return ERROR_FAIL;
4471  }
4472  strcpy(name, reg_type);
4473  name[reg_type_len] = '_';
4474 
4475  unsigned int scanned_chars;
4476  char *scan_dst = name + strlen(reg_type) + 1;
4477  if (sscanf(reg_name_in, "%[_a-zA-Z0-9]%n", scan_dst, &scanned_chars) != 1 ||
4478  scanned_chars != strlen(reg_name_in)) {
4479  LOG_ERROR("Invalid characters in register name '%s'.", reg_name_in);
4481  }
4482  } else {
4483  LOG_ERROR("Invalid argument '%s'.", arg);
4485  }
4486 
4487  high = MAX(high, low);
4488 
4489  if (high > max_val) {
4490  LOG_ERROR("Cannot expose %s register number 0x%x, maximum allowed value is 0x%x.",
4491  reg_type, high, max_val);
4492  return ERROR_FAIL;
4493  }
4494 
4495  /* Check for overlap, name uniqueness. */
4496  range_list_t *entry;
4497  list_for_each_entry(entry, ranges, list) {
4498  if (entry->low <= high && low <= entry->high) {
4499  if (low == high)
4500  LOG_WARNING("Duplicate %s register number - "
4501  "Register %u has already been exposed previously", reg_type, low);
4502  else
4503  LOG_WARNING("Overlapping register ranges - Register range starting from %u overlaps "
4504  "with already exposed register/range at %u.", low, entry->low);
4505  }
4506 
4507  if (entry->name && name && (strcasecmp(entry->name, name) == 0)) {
4508  LOG_ERROR("Duplicate register name \"%s\" found.", name);
4509  return ERROR_FAIL;
4510  }
4511  }
4512 
4513  range_list_t *range = calloc(1, sizeof(range_list_t));
4514  if (!range) {
4515  LOG_ERROR("Out of memory");
4516  return ERROR_FAIL;
4517  }
4518 
4519  range->low = low;
4520  range->high = high;
4521  range->name = name;
4522  /* ownership over name_buffer contents is transferred to list item here */
4523  *name_buffer = NULL;
4524  list_add(&range->list, ranges);
4525  }
4526 
4527  return ERROR_OK;
4528 }
4529 
4530 static int parse_reg_ranges(struct list_head *ranges, const char *tcl_arg,
4531  const char *reg_type, unsigned int max_val)
4532 {
4533  char *args = strdup(tcl_arg);
4534  if (!args) {
4535  LOG_ERROR("Out of memory");
4536  return ERROR_FAIL;
4537  }
4538  char *name_buffer = NULL;
4539  int result = parse_reg_ranges_impl(ranges, args, reg_type, max_val, &name_buffer);
4540  free(name_buffer);
4541  free(args);
4542  return result;
4543 }
4544 
4545 COMMAND_HANDLER(riscv_set_expose_csrs)
4546 {
4547  if (CMD_ARGC == 0)
4549 
4551  RISCV_INFO(info);
4552  int ret = ERROR_OK;
4553 
4554  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4555  ret = parse_reg_ranges(&info->expose_csr, CMD_ARGV[i], "csr", 0xfff);
4556  if (ret != ERROR_OK)
4557  break;
4558  }
4559 
4560  return ret;
4561 }
4562 
4563 COMMAND_HANDLER(riscv_set_expose_custom)
4564 {
4565  if (CMD_ARGC == 0)
4567 
4569  RISCV_INFO(info);
4570  int ret = ERROR_OK;
4571 
4572  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4573  ret = parse_reg_ranges(&info->expose_custom, CMD_ARGV[i], "custom", 0x3fff);
4574  if (ret != ERROR_OK)
4575  break;
4576  }
4577 
4578  return ret;
4579 }
4580 
4581 COMMAND_HANDLER(riscv_hide_csrs)
4582 {
4583  if (CMD_ARGC == 0)
4585 
4587  RISCV_INFO(info);
4588  int ret = ERROR_OK;
4589 
4590  for (unsigned int i = 0; i < CMD_ARGC; i++) {
4591  ret = parse_reg_ranges(&info->hide_csr, CMD_ARGV[i], "csr", 0xfff);
4592  if (ret != ERROR_OK)
4593  break;
4594  }
4595 
4596  return ret;
4597 }
4598 
4599 COMMAND_HANDLER(riscv_authdata_read)
4600 {
4601  unsigned int index = 0;
4602  if (CMD_ARGC == 1)
4603  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], index);
4604  else if (CMD_ARGC != 0)
4606 
4608  if (!target) {
4609  LOG_ERROR("target is NULL!");
4610  return ERROR_FAIL;
4611  }
4612 
4613  RISCV_INFO(r);
4614  if (!r) {
4615  LOG_TARGET_ERROR(target, "riscv_info is NULL!");
4616  return ERROR_FAIL;
4617  }
4618 
4619  if (r->authdata_read) {
4620  uint32_t value;
4621  if (r->authdata_read(target, &value, index) != ERROR_OK)
4622  return ERROR_FAIL;
4623  command_print_sameline(CMD, "0x%08" PRIx32, value);
4624  return ERROR_OK;
4625  }
4626 
4627  LOG_TARGET_ERROR(target, "authdata_read is not implemented for this target.");
4628  return ERROR_FAIL;
4629 }
4630 
4631 COMMAND_HANDLER(riscv_authdata_write)
4632 {
4633  uint32_t value;
4634  unsigned int index = 0;
4635 
4636  if (CMD_ARGC == 0 || CMD_ARGC > 2)
4638 
4639  if (CMD_ARGC == 1) {
4640  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], value);
4641  } else {
4642  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], index);
4643  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
4644  }
4645 
4647  RISCV_INFO(r);
4648 
4649  if (!r->authdata_write) {
4650  LOG_TARGET_ERROR(target, "authdata_write is not implemented for this target.");
4651  return ERROR_FAIL;
4652  }
4653 
4654  return r->authdata_write(target, value, index);
4655 }
4656 
4657 uint32_t riscv_get_dmi_address(const struct target *target, uint32_t dm_address)
4658 {
4659  assert(target);
4660  RISCV_INFO(r);
4661  if (!r || !r->get_dmi_address)
4662  return dm_address;
4663  return r->get_dmi_address(target, dm_address);
4664 }
4665 
4666 static int riscv_dmi_read(struct target *target, uint32_t *value, uint32_t address)
4667 {
4668  if (!target) {
4669  LOG_ERROR("target is NULL!");
4670  return ERROR_FAIL;
4671  }
4672  RISCV_INFO(r);
4673  if (!r) {
4674  LOG_TARGET_ERROR(target, "riscv_info is NULL!");
4675  return ERROR_FAIL;
4676  }
4677  if (!r->dmi_read) {
4678  LOG_TARGET_ERROR(target, "dmi_read is not implemented.");
4679  return ERROR_FAIL;
4680  }
4681  return r->dmi_read(target, value, address);
4682 }
4683 
4684 static int riscv_dmi_write(struct target *target, uint32_t dmi_address, uint32_t value)
4685 {
4686  if (!target) {
4687  LOG_ERROR("target is NULL!");
4688  return ERROR_FAIL;
4689  }
4690  RISCV_INFO(r);
4691  if (!r) {
4692  LOG_TARGET_ERROR(target, "riscv_info is NULL!");
4693  return ERROR_FAIL;
4694  }
4695  if (!r->dmi_write) {
4696  LOG_TARGET_ERROR(target, "dmi_write is not implemented.");
4697  return ERROR_FAIL;
4698  }
4699  const int result = r->dmi_write(target, dmi_address, value);
4700  /* Invalidate our cached progbuf copy:
4701  * - if the user tinkered directly with a progbuf register
4702  * - if debug module was reset, in which case progbuf registers
4703  * may not retain their value.
4704  * FIXME: If there are multiple DMs on a single TAP, it is possible to
4705  * clobber progbuf or reset the DM of another target.
4706  */
4707  const bool progbuf_touched =
4708  (dmi_address >= riscv_get_dmi_address(target, DM_PROGBUF0) &&
4709  dmi_address <= riscv_get_dmi_address(target, DM_PROGBUF15));
4710  const bool dm_deactivated =
4711  (dmi_address == riscv_get_dmi_address(target, DM_DMCONTROL) &&
4712  (value & DM_DMCONTROL_DMACTIVE) == 0);
4713  if (progbuf_touched || dm_deactivated) {
4714  if (r->invalidate_cached_progbuf) {
4715  /* Here the return value of invalidate_cached_progbuf()
4716  * is ignored. It is okay to do so for now, since the
4717  * only case an error is returned is a failure to
4718  * assign a DM to the target, which would have already
4719  * caused an error during dmi_write().
4720  * FIXME: invalidate_cached_progbuf() should be void.
4721  */
4722  r->invalidate_cached_progbuf(target);
4723  } else {
4725  "invalidate_cached_progbuf() is not implemented.");
4726  }
4727  }
4728  return result;
4729 }
4730 
4731 COMMAND_HANDLER(handle_riscv_dmi_read)
4732 {
4733  if (CMD_ARGC != 1)
4735 
4736  uint32_t dmi_address;
4737  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], dmi_address);
4738 
4739  struct target * const target = get_current_target(CMD_CTX);
4740  uint32_t value;
4741  const int result = riscv_dmi_read(target, &value, dmi_address);
4742  if (result == ERROR_OK)
4743  command_print(CMD, "0x%" PRIx32, value);
4744  return result;
4745 }
4746 
4747 COMMAND_HANDLER(handle_riscv_dmi_write)
4748 {
4749  if (CMD_ARGC != 2)
4751 
4752  uint32_t dmi_address, value;
4753  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], dmi_address);
4754  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
4755 
4756  struct target * const target = get_current_target(CMD_CTX);
4757  return riscv_dmi_write(target, dmi_address, value);
4758 }
4759 
4760 COMMAND_HANDLER(handle_riscv_dm_read)
4761 {
4762  if (CMD_ARGC != 1)
4764 
4765  uint32_t dm_address;
4766  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], dm_address);
4767 
4768  struct target * const target = get_current_target(CMD_CTX);
4769  uint32_t value;
4770  const int result = riscv_dmi_read(target, &value,
4771  riscv_get_dmi_address(target, dm_address));
4772  if (result == ERROR_OK)
4773  command_print(CMD, "0x%" PRIx32, value);
4774  return result;
4775 }
4776 
4777 COMMAND_HANDLER(handle_riscv_dm_write)
4778 {
4779  if (CMD_ARGC != 2)
4781 
4782  uint32_t dm_address, value;
4783  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], dm_address);
4784  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
4785 
4786  struct target * const target = get_current_target(CMD_CTX);
4787  return riscv_dmi_write(target, riscv_get_dmi_address(target, dm_address),
4788  value);
4789 }
4790 
4791 COMMAND_HANDLER(riscv_reset_delays)
4792 {
4793  int wait = 0;
4794 
4795  if (CMD_ARGC > 1)
4797 
4798  if (CMD_ARGC == 1)
4799  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], wait);
4800 
4802  RISCV_INFO(r);
4803  r->reset_delays_wait = wait;
4804  return ERROR_OK;
4805 }
4806 
4807 COMMAND_HANDLER(riscv_set_ir)
4808 {
4809  if (CMD_ARGC != 2)
4811 
4812  uint32_t value;
4813  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
4814 
4815  if (!strcmp(CMD_ARGV[0], "idcode"))
4816  buf_set_u32(ir_idcode, 0, 32, value);
4817  else if (!strcmp(CMD_ARGV[0], "dtmcs"))
4818  buf_set_u32(ir_dtmcontrol, 0, 32, value);
4819  else if (!strcmp(CMD_ARGV[0], "dmi"))
4820  buf_set_u32(ir_dbus, 0, 32, value);
4821  else
4822  return ERROR_FAIL;
4823 
4824  return ERROR_OK;
4825 }
4826 
4827 COMMAND_HANDLER(riscv_resume_order)
4828 {
4829  if (CMD_ARGC != 1)
4831 
4832  if (!strcmp(CMD_ARGV[0], "normal")) {
4834  } else if (!strcmp(CMD_ARGV[0], "reversed")) {
4836  } else {
4837  LOG_ERROR("Unsupported resume order: %s", CMD_ARGV[0]);
4838  return ERROR_FAIL;
4839  }
4840 
4841  return ERROR_OK;
4842 }
4843 
4844 COMMAND_HANDLER(riscv_use_bscan_tunnel)
4845 {
4846  uint8_t irwidth = 0;
4847  int tunnel_type = BSCAN_TUNNEL_NESTED_TAP;
4848 
4849  if (CMD_ARGC < 1 || CMD_ARGC > 2)
4851 
4852  if (CMD_ARGC >= 1) {
4853  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], irwidth);
4854  assert(BSCAN_TUNNEL_IR_WIDTH_NBITS < 8);
4855  if (irwidth >= (uint8_t)1 << BSCAN_TUNNEL_IR_WIDTH_NBITS) {
4856  command_print(CMD, "'value' does not fit into %d bits.",
4859  }
4860  }
4861  if (CMD_ARGC == 2)
4862  COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tunnel_type);
4863  if (tunnel_type == BSCAN_TUNNEL_NESTED_TAP)
4864  LOG_INFO("Nested Tap based Bscan Tunnel Selected");
4865  else if (tunnel_type == BSCAN_TUNNEL_DATA_REGISTER)
4866  LOG_INFO("Simple Register based Bscan Tunnel Selected");
4867  else
4868  LOG_INFO("Invalid Tunnel type selected ! : selecting default Nested Tap Type");
4869 
4870  bscan_tunnel_type = tunnel_type;
4871  bscan_tunnel_ir_width = irwidth;
4872  return ERROR_OK;
4873 }
4874 
4875 COMMAND_HANDLER(riscv_set_bscan_tunnel_ir)
4876 {
4877  int ir_id = 0;
4878 
4879  if (CMD_ARGC > 1)
4881 
4882  if (CMD_ARGC == 1)
4883  COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], ir_id);
4884 
4885  LOG_INFO("Bscan tunnel IR 0x%x selected", ir_id);
4886 
4887  bscan_tunnel_ir_id = ir_id;
4888  return ERROR_OK;
4889 }
4890 
4891 COMMAND_HANDLER(riscv_set_maskisr)
4892 {
4894  RISCV_INFO(info);
4895 
4896  static const struct jim_nvp nvp_maskisr_modes[] = {
4897  { .name = "off", .value = RISCV_ISRMASK_OFF },
4898  { .name = "steponly", .value = RISCV_ISRMASK_STEPONLY },
4899  { .name = NULL, .value = -1 },
4900  };
4901  const struct jim_nvp *n;
4902 
4903  if (CMD_ARGC > 0) {
4904  n = jim_nvp_name2value_simple(nvp_maskisr_modes, CMD_ARGV[0]);
4905  if (!n->name)
4907  info->isrmask_mode = n->value;
4908  } else {
4909  n = jim_nvp_value2name_simple(nvp_maskisr_modes, info->isrmask_mode);
4910  command_print(CMD, "riscv interrupt mask %s", n->name);
4911  }
4912 
4913  return ERROR_OK;
4914 }
4915 
4916 COMMAND_HANDLER(riscv_set_autofence)
4917 {
4919  RISCV_INFO(r);
4920 
4921  if (CMD_ARGC == 0) {
4922  command_print(CMD, "autofence: %s", r->autofence ? "on" : "off");
4923  return ERROR_OK;
4924  } else if (CMD_ARGC == 1) {
4925  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], r->autofence);
4926  return ERROR_OK;
4927  }
4928 
4930 }
4931 
4932 COMMAND_HELPER(ebreakx_deprecation_helper, enum riscv_priv_mode mode)
4933 {
4934  struct target * const target = get_current_target(CMD_CTX);
4936  const char *mode_str;
4937  switch (mode) {
4938  case RISCV_MODE_M:
4939  mode_str = "m";
4940  break;
4941  case RISCV_MODE_S:
4942  mode_str = "s";
4943  break;
4944  case RISCV_MODE_U:
4945  mode_str = "u";
4946  break;
4947  default:
4948  assert(0 && "Unexpected execution mode");
4949  mode_str = "unexpected";
4950  }
4951  if (CMD_ARGC > 1)
4953  if (CMD_ARGC == 0) {
4954  LOG_WARNING("DEPRECATED! use '%s cget -ebreak' not '%s'",
4956  command_print(CMD, "riscv_ebreak%s enabled: %s", mode_str,
4957  config->dcsr_ebreak_fields[mode] ? "on" : "off");
4958  return ERROR_OK;
4959  }
4960  assert(CMD_ARGC == 1);
4961  command_print(CMD, "DEPRECATED! use '%s configure -ebreak %s' not '%s'",
4962  target_name(target), mode_str, CMD_NAME);
4963  bool ebreak_ctl;
4964  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], ebreak_ctl);
4965  config->dcsr_ebreak_fields[mode] = ebreak_ctl;
4966  switch (mode) {
4967  case RISCV_MODE_S:
4968  config->dcsr_ebreak_fields[RISCV_MODE_VS] = ebreak_ctl;
4969  break;
4970  case RISCV_MODE_U:
4971  config->dcsr_ebreak_fields[RISCV_MODE_VU] = ebreak_ctl;
4972  break;
4973  default:
4974  break;
4975  }
4976  return ERROR_OK;
4977 }
4978 
4979 COMMAND_HANDLER(riscv_set_ebreakm)
4980 {
4981  return CALL_COMMAND_HANDLER(ebreakx_deprecation_helper,
4982  RISCV_MODE_M);
4983 }
4984 
4985 COMMAND_HANDLER(riscv_set_ebreaks)
4986 {
4987  return CALL_COMMAND_HANDLER(ebreakx_deprecation_helper,
4988  RISCV_MODE_S);
4989 }
4990 
4991 COMMAND_HANDLER(riscv_set_ebreaku)
4992 {
4993  return CALL_COMMAND_HANDLER(ebreakx_deprecation_helper,
4994  RISCV_MODE_U);
4995 }
4996 
4997 COMMAND_HELPER(riscv_clear_trigger, int trigger_id, const char *name)
4998 {
5000  if (CMD_ARGC != 1)
5002 
5003  if (find_first_trigger_by_id(target, trigger_id) < 0) {
5004  LOG_TARGET_ERROR(target, "No %s is set. Nothing to clear.", name);
5005  return ERROR_FAIL;
5006  }
5007  return remove_trigger(target, trigger_id);
5008 }
5009 
5010 COMMAND_HANDLER(riscv_itrigger)
5011 {
5012  if (CMD_ARGC < 1)
5014 
5016  const int ITRIGGER_UNIQUE_ID = -CSR_TDATA1_TYPE_ITRIGGER;
5017 
5018  if (!target_was_examined(target)) {
5019  LOG_TARGET_ERROR(target, "not examined");
5021  }
5022 
5024  return ERROR_FAIL;
5025 
5026  if (!strcmp(CMD_ARGV[0], "set")) {
5027  if (find_first_trigger_by_id(target, ITRIGGER_UNIQUE_ID) >= 0) {
5028  LOG_TARGET_ERROR(target, "An itrigger is already set, and OpenOCD "
5029  "doesn't support setting more than one at a time.");
5030  return ERROR_FAIL;
5031  }
5032  bool vs = false;
5033  bool vu = false;
5034  bool nmi = false;
5035  bool m = false;
5036  bool s = false;
5037  bool u = false;
5038  riscv_reg_t interrupts = 0;
5039 
5040  for (unsigned int i = 1; i < CMD_ARGC; i++) {
5041  if (!strcmp(CMD_ARGV[i], "vs"))
5042  vs = true;
5043  else if (!strcmp(CMD_ARGV[i], "vu"))
5044  vu = true;
5045  else if (!strcmp(CMD_ARGV[i], "nmi"))
5046  nmi = true;
5047  else if (!strcmp(CMD_ARGV[i], "m"))
5048  m = true;
5049  else if (!strcmp(CMD_ARGV[i], "s"))
5050  s = true;
5051  else if (!strcmp(CMD_ARGV[i], "u"))
5052  u = true;
5053  else
5054  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[i], interrupts);
5055  }
5056  if (!nmi && interrupts == 0) {
5057  LOG_ERROR("Doesn't make sense to set itrigger with "
5058  "mie_bits=0 and without nmi.");
5059  return ERROR_FAIL;
5060  } else if (!vs && !vu && !m && !s && !u) {
5061  LOG_ERROR("Doesn't make sense to set itrigger without at "
5062  "least one of vs, vu, m, s, or u.");
5063  return ERROR_FAIL;
5064  }
5065  int result = maybe_add_trigger_t4(target, vs, vu, nmi, m, s, u, interrupts, ITRIGGER_UNIQUE_ID);
5066  if (result != ERROR_OK)
5067  LOG_TARGET_ERROR(target, "Failed to set requested itrigger.");
5068  return result;
5069 
5070  } else if (!strcmp(CMD_ARGV[0], "clear")) {
5071  return riscv_clear_trigger(CMD, ITRIGGER_UNIQUE_ID, "itrigger");
5072 
5073  } else {
5074  LOG_ERROR("First argument must be either 'set' or 'clear'.");
5076  }
5077  return ERROR_OK;
5078 }
5079 
5080 COMMAND_HANDLER(riscv_icount)
5081 {
5082  if (CMD_ARGC < 1)
5084 
5086  const int ICOUNT_UNIQUE_ID = -CSR_TDATA1_TYPE_ICOUNT;
5087 
5088  if (!target_was_examined(target)) {
5089  LOG_TARGET_ERROR(target, "not examined");
5091  }
5092 
5094  return ERROR_FAIL;
5095 
5096  if (!strcmp(CMD_ARGV[0], "set")) {
5097  if (find_first_trigger_by_id(target, ICOUNT_UNIQUE_ID) >= 0) {
5098  LOG_TARGET_ERROR(target, "An icount trigger is already set, and OpenOCD "
5099  "doesn't support setting more than one at a time.");
5100  return ERROR_FAIL;
5101  }
5102  bool vs = false;
5103  bool vu = false;
5104  bool m = false;
5105  bool s = false;
5106  bool u = false;
5107  bool pending = false;
5108  unsigned int count = 0;
5109 
5110  for (unsigned int i = 1; i < CMD_ARGC; i++) {
5111  if (!strcmp(CMD_ARGV[i], "vs"))
5112  vs = true;
5113  else if (!strcmp(CMD_ARGV[i], "vu"))
5114  vu = true;
5115  else if (!strcmp(CMD_ARGV[i], "pending"))
5116  pending = true;
5117  else if (!strcmp(CMD_ARGV[i], "m"))
5118  m = true;
5119  else if (!strcmp(CMD_ARGV[i], "s"))
5120  s = true;
5121  else if (!strcmp(CMD_ARGV[i], "u"))
5122  u = true;
5123  else
5124  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[i], count);
5125  }
5126  if (count == 0) {
5127  LOG_ERROR("Doesn't make sense to set icount trigger with "
5128  "count=0.");
5129  return ERROR_FAIL;
5130  } else if (!vs && !vu && !m && !s && !u) {
5131  LOG_ERROR("Doesn't make sense to set itrigger without at "
5132  "least one of vs, vu, m, s, or u.");
5133  return ERROR_FAIL;
5134  }
5135  int result = maybe_add_trigger_t3(target, vs, vu, m, s, u, pending, count, ICOUNT_UNIQUE_ID);
5136  if (result != ERROR_OK)
5137  LOG_TARGET_ERROR(target, "Failed to set requested icount trigger.");
5138  return result;
5139 
5140  } else if (!strcmp(CMD_ARGV[0], "clear")) {
5141  return riscv_clear_trigger(CMD, ICOUNT_UNIQUE_ID, "icount trigger");
5142 
5143  } else {
5144  LOG_ERROR("First argument must be either 'set' or 'clear'.");
5146  }
5147  return ERROR_OK;
5148 }
5149 
5150 COMMAND_HANDLER(riscv_etrigger)
5151 {
5152  if (CMD_ARGC < 1)
5154 
5156  const int ETRIGGER_UNIQUE_ID = -CSR_TDATA1_TYPE_ETRIGGER;
5157 
5158  if (!target_was_examined(target)) {
5159  LOG_TARGET_ERROR(target, "not examined");
5161  }
5162 
5164  return ERROR_FAIL;
5165 
5166  if (!strcmp(CMD_ARGV[0], "set")) {
5167  if (find_first_trigger_by_id(target, ETRIGGER_UNIQUE_ID) >= 0) {
5168  LOG_TARGET_ERROR(target, "An etrigger is already set, and OpenOCD "
5169  "doesn't support setting more than one at a time.");
5170  return ERROR_FAIL;
5171  }
5172  bool vs = false;
5173  bool vu = false;
5174  bool m = false;
5175  bool s = false;
5176  bool u = false;
5177  riscv_reg_t exception_codes = 0;
5178 
5179  for (unsigned int i = 1; i < CMD_ARGC; i++) {
5180  if (!strcmp(CMD_ARGV[i], "vs"))
5181  vs = true;
5182  else if (!strcmp(CMD_ARGV[i], "vu"))
5183  vu = true;
5184  else if (!strcmp(CMD_ARGV[i], "m"))
5185  m = true;
5186  else if (!strcmp(CMD_ARGV[i], "s"))
5187  s = true;
5188  else if (!strcmp(CMD_ARGV[i], "u"))
5189  u = true;
5190  else
5191  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[i], exception_codes);
5192  }
5193  if (exception_codes == 0) {
5194  LOG_ERROR("Doesn't make sense to set etrigger with "
5195  "exception_codes=0.");
5196  return ERROR_FAIL;
5197  } else if (!vs && !vu && !m && !s && !u) {
5198  LOG_ERROR("Doesn't make sense to set etrigger without at "
5199  "least one of vs, vu, m, s, or u.");
5200  return ERROR_FAIL;
5201  }
5202  int result = maybe_add_trigger_t5(target, vs, vu, m, s, u, exception_codes, ETRIGGER_UNIQUE_ID);
5203  if (result != ERROR_OK)
5204  LOG_TARGET_ERROR(target, "Failed to set requested etrigger.");
5205  return result;
5206 
5207  } else if (!strcmp(CMD_ARGV[0], "clear")) {
5208  return riscv_clear_trigger(CMD, ETRIGGER_UNIQUE_ID, "etrigger");
5209 
5210  } else {
5211  LOG_ERROR("First argument must be either 'set' or 'clear'.");
5213  }
5214  return ERROR_OK;
5215 }
5216 
5217 COMMAND_HANDLER(handle_repeat_read)
5218 {
5220  if (!target_was_examined(target)) {
5221  LOG_TARGET_ERROR(target, "not examined");
5223  }
5224 
5225  RISCV_INFO(r);
5226 
5227  if (CMD_ARGC < 2 || CMD_ARGC > 3)
5229 
5230  uint32_t count;
5234  uint32_t size = 4;
5235  if (CMD_ARGC > 2)
5237 
5238  if (count == 0)
5239  return ERROR_OK;
5240 
5241  uint8_t *buffer = malloc(size * count);
5242  if (!buffer) {
5243  LOG_ERROR("malloc failed");
5244  return ERROR_FAIL;
5245  }
5246  const struct riscv_mem_access_args args = {
5247  .address = address,
5248  .read_buffer = buffer,
5249  .size = size,
5250  .count = count,
5251  .increment = 0,
5252  };
5253  int result = r->access_memory(target, args);
5254  if (result == ERROR_OK)
5256  free(buffer);
5257  return result;
5258 }
5259 
5260 COMMAND_HANDLER(handle_memory_sample_command)
5261 {
5263  RISCV_INFO(r);
5264 
5265  if (CMD_ARGC == 0) {
5266  command_print(CMD, "Memory sample configuration for %s:", target_name(target));
5267  for (unsigned int i = 0; i < ARRAY_SIZE(r->sample_config.bucket); i++) {
5268  if (r->sample_config.bucket[i].enabled) {
5269  command_print(CMD, "bucket %d; address=0x%" TARGET_PRIxADDR "; size=%d", i,
5270  r->sample_config.bucket[i].address,
5271  r->sample_config.bucket[i].size_bytes);
5272  } else {
5273  command_print(CMD, "bucket %d; disabled", i);
5274  }
5275  }
5276  return ERROR_OK;
5277  }
5278 
5279  if (CMD_ARGC < 2)
5281 
5282  uint32_t bucket;
5283  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bucket);
5284  if (bucket > ARRAY_SIZE(r->sample_config.bucket)) {
5285  LOG_TARGET_ERROR(target, "Max bucket number is %zd.", ARRAY_SIZE(r->sample_config.bucket));
5287  }
5288 
5289  if (!strcmp(CMD_ARGV[1], "clear")) {
5290  r->sample_config.bucket[bucket].enabled = false;
5291  } else {
5292  COMMAND_PARSE_ADDRESS(CMD_ARGV[1], r->sample_config.bucket[bucket].address);
5293 
5294  if (CMD_ARGC > 2) {
5295  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], r->sample_config.bucket[bucket].size_bytes);
5296  if (r->sample_config.bucket[bucket].size_bytes != 4 &&
5297  r->sample_config.bucket[bucket].size_bytes != 8) {
5298  LOG_TARGET_ERROR(target, "Only 4-byte and 8-byte sizes are supported.");
5300  }
5301  } else {
5302  r->sample_config.bucket[bucket].size_bytes = 4;
5303  }
5304 
5305  r->sample_config.bucket[bucket].enabled = true;
5306  }
5307 
5308  if (!r->sample_buf.buf) {
5309  r->sample_buf.size = 1024 * 1024;
5310  r->sample_buf.buf = malloc(r->sample_buf.size);
5311  }
5312 
5313  /* Clear the buffer when the configuration is changed. */
5314  r->sample_buf.used = 0;
5315 
5316  r->sample_config.enabled = true;
5317 
5318  return ERROR_OK;
5319 }
5320 
5321 COMMAND_HANDLER(handle_dump_sample_buf_command)
5322 {
5324  RISCV_INFO(r);
5325 
5326  if (CMD_ARGC > 1)
5328 
5329  bool base64 = false;
5330  if (CMD_ARGC > 0) {
5331  if (!strcmp(CMD_ARGV[0], "base64")) {
5332  base64 = true;
5333  } else {
5334  LOG_ERROR("Unknown argument: %s", CMD_ARGV[0]);
5336  }
5337  }
5338 
5339  int result = ERROR_OK;
5340  if (base64) {
5341  unsigned char *encoded = base64_encode(r->sample_buf.buf,
5342  r->sample_buf.used, NULL);
5343  if (!encoded) {
5344  LOG_TARGET_ERROR(target, "Failed base64 encode!");
5345  result = ERROR_FAIL;
5346  goto error;
5347  }
5348  command_print(CMD, "%s", encoded);
5349  free(encoded);
5350  } else {
5351  unsigned int i = 0;
5352  while (i < r->sample_buf.used) {
5353  uint8_t command = r->sample_buf.buf[i++];
5355  uint32_t timestamp = buf_get_u32(r->sample_buf.buf + i, 0, 32);
5356  i += 4;
5357  command_print(CMD, "timestamp before: %u", timestamp);
5358  } else if (command == RISCV_SAMPLE_BUF_TIMESTAMP_AFTER) {
5359  uint32_t timestamp = buf_get_u32(r->sample_buf.buf + i, 0, 32);
5360  i += 4;
5361  command_print(CMD, "timestamp after: %u", timestamp);
5362  } else if (command < ARRAY_SIZE(r->sample_config.bucket)) {
5364  r->sample_config.bucket[command].address);
5365  if (r->sample_config.bucket[command].size_bytes == 4) {
5366  uint32_t value = buf_get_u32(r->sample_buf.buf + i, 0, 32);
5367  i += 4;
5368  command_print(CMD, "0x%08" PRIx32, value);
5369  } else if (r->sample_config.bucket[command].size_bytes == 8) {
5370  uint64_t value = buf_get_u64(r->sample_buf.buf + i, 0, 64);
5371  i += 8;
5372  command_print(CMD, "0x%016" PRIx64, value);
5373  } else {
5374  LOG_TARGET_ERROR(target, "Found invalid size in bucket %d: %d", command,
5375  r->sample_config.bucket[command].size_bytes);
5376  result = ERROR_FAIL;
5377  goto error;
5378  }
5379  } else {
5380  LOG_TARGET_ERROR(target, "Found invalid command byte in sample buf: 0x%2x at offset 0x%x",
5381  command, i - 1);
5382  result = ERROR_FAIL;
5383  goto error;
5384  }
5385  }
5386  }
5387 
5388 error:
5389  /* Clear the sample buffer even when there was an error. */
5390  r->sample_buf.used = 0;
5391  return result;
5392 }
5393 
5394 static COMMAND_HELPER(riscv_print_info_line_if_available, const char *section,
5395  const char *key, unsigned int value, bool is_available)
5396 {
5397  char full_key[80];
5398  snprintf(full_key, sizeof(full_key), "%s.%s", section, key);
5399  if (is_available)
5400  command_print(CMD, "%-21s %3d", full_key, value);
5401  else
5402  command_print(CMD, "%-21s unavailable", full_key);
5403  return 0;
5404 }
5405 
5406 COMMAND_HELPER(riscv_print_info_line, const char *section, const char *key,
5407  unsigned int value)
5408 {
5409  return CALL_COMMAND_HANDLER(riscv_print_info_line_if_available, section,
5410  key, value, /*is_available*/ true);
5411 }
5412 
5413 COMMAND_HANDLER(handle_info)
5414 {
5416  RISCV_INFO(r);
5417 
5418  /* This output format can be fed directly into TCL's "array set". */
5419 
5420  riscv_print_info_line(CMD, "hart", "xlen", riscv_xlen(target));
5421 
5422  const bool trigger_count_available =
5424  riscv_print_info_line_if_available(CMD, "hart", "trigger_count",
5425  r->trigger_count, trigger_count_available);
5426  if (r->print_info)
5427  return CALL_COMMAND_HANDLER(r->print_info, target);
5428 
5429  return 0;
5430 }
5431 
5432 COMMAND_HANDLER(riscv_exec_progbuf)
5433 {
5434  if (CMD_ARGC < 1 || CMD_ARGC > 16)
5436 
5438 
5439  if (!target_was_examined(target)) {
5440  LOG_TARGET_ERROR(target, "not examined");
5442  }
5443 
5444  RISCV_INFO(r);
5445  if (r->dtm_version != DTM_DTMCS_VERSION_1_0) {
5446  LOG_TARGET_ERROR(target, "exec_progbuf: Program buffer is "
5447  "only supported on v0.13 or v1.0 targets.");
5448  return ERROR_FAIL;
5449  }
5450 
5451  if (target->state != TARGET_HALTED) {
5452  LOG_TARGET_ERROR(target, "exec_progbuf: Can't execute "
5453  "program buffer, target not halted.");
5454  return ERROR_TARGET_NOT_HALTED;
5455  }
5456 
5457  if (riscv_progbuf_size(target) == 0) {
5458  LOG_TARGET_ERROR(target, "exec_progbuf: Program buffer not implemented "
5459  "in the target.");
5460  return ERROR_FAIL;
5461  }
5462 
5463  struct riscv_program prog;
5464  riscv_program_init(&prog, target);
5465 
5466  for (unsigned int i = 0; i < CMD_ARGC; i++) {
5467  riscv_insn_t instr;
5468  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[i], instr);
5469  if (riscv_program_insert(&prog, instr) != ERROR_OK)
5470  return ERROR_FAIL;
5471  }
5472 
5474  return ERROR_FAIL;
5475  int error = riscv_program_exec(&prog, target);
5477 
5478  if (error != ERROR_OK) {
5479  LOG_TARGET_ERROR(target, "exec_progbuf: Program buffer execution failed.");
5480  return ERROR_FAIL;
5481  }
5482 
5483  LOG_TARGET_DEBUG(target, "exec_progbuf: Program buffer execution successful.");
5484 
5485  return ERROR_OK;
5486 }
5487 
5488 COMMAND_HANDLER(riscv_set_enable_trigger_feature)
5489 {
5491  RISCV_INFO(r);
5492 
5493  if (CMD_ARGC == 2) {
5494  bool enable_for_wp = true;
5495 
5496  if (!strcmp(CMD_ARGV[1], "wp"))
5497  enable_for_wp = true;
5498  else if (!strcmp(CMD_ARGV[1], "none"))
5499  enable_for_wp = false;
5500  else
5502 
5503  if (!strcmp(CMD_ARGV[0], "all")) {
5504  r->wp_allow_equality_match_trigger = enable_for_wp;
5505  r->wp_allow_napot_trigger = enable_for_wp;
5506  r->wp_allow_ge_lt_trigger = enable_for_wp;
5507  } else if (!strcmp(CMD_ARGV[0], "eq")) {
5508  r->wp_allow_equality_match_trigger = enable_for_wp;
5509  } else if (!strcmp(CMD_ARGV[0], "napot")) {
5510  r->wp_allow_napot_trigger = enable_for_wp;
5511  } else if (!strcmp(CMD_ARGV[0], "ge_lt")) {
5512  r->wp_allow_ge_lt_trigger = enable_for_wp;
5513  } else {
5515  }
5516  } else if (CMD_ARGC != 0) {
5518  }
5519 
5520  command_print(CMD, "Triggers feature configuration:\n"
5521  "Equality match trigger: for wp (%s)\n"
5522  "NAPOT trigger: for wp (%s)\n"
5523  "ge-lt chained triggers: for wp (%s)",
5524  r->wp_allow_equality_match_trigger ? "enabled" : "disabled",
5525  r->wp_allow_napot_trigger ? "enabled" : "disabled",
5526  r->wp_allow_ge_lt_trigger ? "enabled" : "disabled");
5527 
5528  return ERROR_OK;
5529 }
5530 
5531 static COMMAND_HELPER(report_reserved_triggers, struct target *target)
5532 {
5533  RISCV_INFO(r);
5535  return ERROR_FAIL;
5536  const char *separator = "";
5537  for (riscv_reg_t t = 0; t < r->trigger_count; ++t) {
5538  if (r->reserved_triggers[t]) {
5539  command_print_sameline(CMD, "%s%" PRIu64, separator, t);
5540  separator = " ";
5541  }
5542  }
5543  command_print_sameline(CMD, "\n");
5544  return ERROR_OK;
5545 }
5546 
5547 COMMAND_HANDLER(handle_reserve_trigger)
5548 {
5550  if (!target_was_examined(target)) {
5551  command_print(CMD, "Error: Target not examined");
5553  }
5554 
5555  if (CMD_ARGC == 0)
5556  return CALL_COMMAND_HANDLER(report_reserved_triggers, target);
5557 
5558  if (CMD_ARGC != 2)
5560 
5561  riscv_reg_t t;
5562  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], t);
5563 
5565  return ERROR_FAIL;
5566  RISCV_INFO(r);
5567  if (r->trigger_count == 0) {
5568  command_print(CMD, "Error: There are no triggers on the target.");
5570  }
5571  if (t >= r->trigger_count) {
5572  command_print(CMD, "Error: trigger with index %" PRIu64
5573  " does not exist. There are only %u triggers"
5574  " on the target (with indexes 0 .. %u).",
5575  t, r->trigger_count, r->trigger_count - 1);
5577  }
5578  if (r->trigger_unique_id[t] != -1) {
5579  command_print(CMD, "Error: trigger with index %" PRIu64
5580  " is already in use and can not be reserved.", t);
5582  }
5583  COMMAND_PARSE_ON_OFF(CMD_ARGV[1], r->reserved_triggers[t]);
5584  return ERROR_OK;
5585 }
5586 
5587 COMMAND_HANDLER(handle_riscv_virt2phys_mode)
5588 {
5590  if (CMD_ARGC == 0) {
5591  enum riscv_virt2phys_mode mode = info->virt2phys_mode;
5593  return ERROR_OK;
5594  }
5595 
5596  if (CMD_ARGC != 1)
5598 
5599  // TODO: add auto mode to allow OpenOCD choose translation mode
5600  if (!strcmp(CMD_ARGV[0],
5602  info->virt2phys_mode = RISCV_VIRT2PHYS_MODE_SW;
5603  } else if (!strcmp(CMD_ARGV[0],
5605  info->virt2phys_mode = RISCV_VIRT2PHYS_MODE_HW;
5606  } else if (!strcmp(CMD_ARGV[0],
5608  info->virt2phys_mode = RISCV_VIRT2PHYS_MODE_OFF;
5609  } else {
5610  command_print(CMD, "Unsupported address translation mode: %s", CMD_ARGV[0]);
5612  }
5613 
5614  return ERROR_OK;
5615 }
5616 
5617 static const struct command_registration riscv_exec_command_handlers[] = {
5618  {
5619  .name = "dump_sample_buf",
5620  .handler = handle_dump_sample_buf_command,
5621  .mode = COMMAND_ANY,
5622  .usage = "[base64]",
5623  .help = "Print the contents of the sample buffer, and clear the buffer."
5624  },
5625  {
5626  .name = "info",
5627  .handler = handle_info,
5628  .mode = COMMAND_ANY,
5629  .usage = "",
5630  .help = "Displays some information OpenOCD detected about the target."
5631  },
5632  {
5633  .name = "memory_sample",
5634  .handler = handle_memory_sample_command,
5635  .mode = COMMAND_ANY,
5636  .usage = "bucket address|clear [size=4]",
5637  .help = "Causes OpenOCD to frequently read size bytes at the given address."
5638  },
5639  {
5640  .name = "repeat_read",
5641  .handler = handle_repeat_read,
5642  .mode = COMMAND_ANY,
5643  .usage = "count address [size=4]",
5644  .help = "Repeatedly read the value at address."
5645  },
5646  {
5647  .name = "set_command_timeout_sec",
5648  .handler = riscv_set_command_timeout_sec,
5649  .mode = COMMAND_ANY,
5650  .usage = "sec",
5651  .help = "Set the wall-clock timeout (in seconds) for individual commands"
5652  },
5653  {
5654  .name = "set_reset_timeout_sec",
5655  .handler = riscv_set_reset_timeout_sec,
5656  .mode = COMMAND_ANY,
5657  .usage = "sec",
5658  .help = "DEPRECATED. Use 'riscv set_command_timeout_sec' instead."
5659  },
5660  {
5661  .name = "set_mem_access",
5662  .handler = riscv_set_mem_access,
5663  .mode = COMMAND_ANY,
5664  .usage = "method1 [method2] [method3]",
5665  .help = "Set which memory access methods shall be used and in which order "
5666  "of priority. Method can be one of: 'progbuf', 'sysbus' or 'abstract'."
5667  },
5668  {
5669  .name = "expose_csrs",
5670  .handler = riscv_set_expose_csrs,
5671  .mode = COMMAND_CONFIG,
5672  .usage = "n0[-m0|=name0][,n1[-m1|=name1]]...[,n15[-m15|=name15]]",
5673  .help = "Configure a list of inclusive ranges for CSRs to expose in "
5674  "addition to the standard ones. This must be executed before "
5675  "`init`."
5676  },
5677  {
5678  .name = "expose_custom",
5679  .handler = riscv_set_expose_custom,
5680  .mode = COMMAND_CONFIG,
5681  .usage = "n0[-m0|=name0][,n1[-m1|=name1]]...[,n15[-m15|=name15]]",
5682  .help = "Configure a list of inclusive ranges for custom registers to "
5683  "expose. custom0 is accessed as abstract register number 0xc000, "
5684  "etc. This must be executed before `init`."
5685  },
5686  {
5687  .name = "hide_csrs",
5688  .handler = riscv_hide_csrs,
5689  .mode = COMMAND_CONFIG,
5690  .usage = "{n0|n-m0}[,n1|n-m1]......",
5691  .help = "Configure a list of inclusive ranges for CSRs to hide from gdb. "
5692  "Hidden registers are still available, but are not listed in "
5693  "gdb target description and `reg` command output. "
5694  "This must be executed before `init`."
5695  },
5696  {
5697  .name = "authdata_read",
5698  .handler = riscv_authdata_read,
5699  .usage = "[index]",
5700  .mode = COMMAND_ANY,
5701  .help = "Return the 32-bit value read from authdata or authdata0 "
5702  "(index=0), or authdata1 (index=1)."
5703  },
5704  {
5705  .name = "authdata_write",
5706  .handler = riscv_authdata_write,
5707  .mode = COMMAND_ANY,
5708  .usage = "[index] value",
5709  .help = "Write the 32-bit value to authdata or authdata0 (index=0), "
5710  "or authdata1 (index=1)."
5711  },
5712  {
5713  .name = "dmi_read",
5714  .handler = handle_riscv_dmi_read,
5715  .mode = COMMAND_ANY,
5716  .usage = "address",
5717  .help = "Read and return 32-bit value from the given address on the "
5718  "RISC-V DMI bus."
5719  },
5720  {
5721  .name = "dmi_write",
5722  .handler = handle_riscv_dmi_write,
5723  .mode = COMMAND_ANY,
5724  .usage = "address value",
5725  .help = "Write a 32-bit value to the given address on the RISC-V DMI bus."
5726  },
5727  {
5728  .name = "dm_read",
5729  .handler = handle_riscv_dm_read,
5730  .mode = COMMAND_ANY,
5731  .usage = "reg_address",
5732  .help = "Read and return 32-bit value from a debug module's register "
5733  "at reg_address."
5734  },
5735  {
5736  .name = "dm_write",
5737  .handler = handle_riscv_dm_write,
5738  .mode = COMMAND_ANY,
5739  .usage = "reg_address value",
5740  .help = "Write a 32-bit value to the debug module's register at "
5741  "reg_address."
5742  },
5743  {
5744  .name = "reset_delays",
5745  .handler = riscv_reset_delays,
5746  .mode = COMMAND_ANY,
5747  .usage = "[wait]",
5748  .help = "OpenOCD learns how many Run-Test/Idle cycles are required "
5749  "between scans to avoid encountering the target being busy. This "
5750  "command resets those learned values after `wait` scans. It's only "
5751  "useful for testing OpenOCD itself."
5752  },
5753  {
5754  .name = "resume_order",
5755  .handler = riscv_resume_order,
5756  .mode = COMMAND_ANY,
5757  .usage = "normal|reversed",
5758  .help = "Choose the order that harts are resumed in when `hasel` is not "
5759  "supported. Normal order is from lowest hart index to highest. "
5760  "Reversed order is from highest hart index to lowest."
5761  },
5762  {
5763  .name = "set_ir",
5764  .handler = riscv_set_ir,
5765  .mode = COMMAND_ANY,
5766  .usage = "idcode|dtmcs|dmi value",
5767  .help = "Set IR value for specified JTAG register."
5768  },
5769  {
5770  .name = "use_bscan_tunnel",
5771  .handler = riscv_use_bscan_tunnel,
5772  .mode = COMMAND_CONFIG,
5773  .usage = "value [type]",
5774  .help = "Enable or disable use of a BSCAN tunnel to reach DM."
5775  },
5776  {
5777  .name = "set_bscan_tunnel_ir",
5778  .handler = riscv_set_bscan_tunnel_ir,
5779  .mode = COMMAND_CONFIG,
5780  .usage = "[value]",
5781  .help = "Specify the JTAG TAP IR used to access the bscan tunnel. "
5782  "By default it is 0x23 << (ir_length - 6), which map some "
5783  "Xilinx FPGA (IR USER4)"
5784  },
5785  {
5786  .name = "set_maskisr",
5787  .handler = riscv_set_maskisr,
5788  .mode = COMMAND_EXEC,
5789  .help = "mask riscv interrupts",
5790  .usage = "['off'|'steponly']",
5791  },
5792  {
5793  .name = "set_ebreakm",
5794  .handler = riscv_set_ebreakm,
5795  .mode = COMMAND_ANY,
5796  .usage = "[on|off]",
5797  .help = "DEPRECATED! use '<target_name> configure -ebreak' or "
5798  "'<target_name> cget -ebreak'"
5799  },
5800  {
5801  .name = "set_ebreaks",
5802  .handler = riscv_set_ebreaks,
5803  .mode = COMMAND_ANY,
5804  .usage = "[on|off]",
5805  .help = "DEPRECATED! use '<target_name> configure -ebreak' or "
5806  "'<target_name> cget -ebreak'"
5807  },
5808  {
5809  .name = "set_ebreaku",
5810  .handler = riscv_set_ebreaku,
5811  .mode = COMMAND_ANY,
5812  .usage = "[on|off]",
5813  .help = "DEPRECATED! use '<target_name> configure -ebreak' or "
5814  "'<target_name> cget -ebreak'"
5815  },
5816  {
5817  .name = "etrigger",
5818  .handler = riscv_etrigger,
5819  .mode = COMMAND_EXEC,
5820  .usage = "set [vs] [vu] [m] [s] [u] <exception_codes>|clear",
5821  .help = "Set or clear a single exception trigger."
5822  },
5823  {
5824  .name = "icount",
5825  .handler = riscv_icount,
5826  .mode = COMMAND_EXEC,
5827  .usage = "set [vs] [vu] [m] [s] [u] [pending] <count>|clear",
5828  .help = "Set or clear a single instruction count trigger."
5829  },
5830  {
5831  .name = "itrigger",
5832  .handler = riscv_itrigger,
5833  .mode = COMMAND_EXEC,
5834  .usage = "set [vs] [vu] [nmi] [m] [s] [u] <mie_bits>|clear",
5835  .help = "Set or clear a single interrupt trigger."
5836  },
5837  {
5838  .name = "exec_progbuf",
5839  .handler = riscv_exec_progbuf,
5840  .mode = COMMAND_EXEC,
5841  .usage = "instr1 [instr2 [... instr16]]",
5842  .help = "Execute a sequence of 32-bit instructions using the program buffer. "
5843  "The final ebreak instruction is added automatically, if needed."
5844  },
5845  {
5846  .name = "set_enable_trigger_feature",
5847  .handler = riscv_set_enable_trigger_feature,
5848  .mode = COMMAND_ANY,
5849  .usage = "[('eq'|'napot'|'ge_lt'|'all') ('wp'|'none')]",
5850  .help = "Control whether OpenOCD is allowed to use certain RISC-V trigger features for watchpoints."
5851  },
5852  {
5853  .name = "reserve_trigger",
5854  .handler = handle_reserve_trigger,
5855  /* TODO: Move this to COMMAND_ANY */
5856  .mode = COMMAND_EXEC,
5857  .usage = "[index ('on'|'off')]",
5858  .help = "Controls which RISC-V triggers shall not be touched by OpenOCD.",
5859  },
5860  {
5861  .name = "virt2phys_mode",
5862  .handler = handle_riscv_virt2phys_mode,
5863  .mode = COMMAND_ANY,
5864  .usage = "['sw'|'hw'|'off']",
5865  .help = "Configure the virtual address translation mode: "
5866  "sw - translate vaddr to paddr by manually traversing page tables, "
5867  "hw - translate vaddr to paddr by hardware, "
5868  "off - no address translation."
5869  },
5870  {
5871  .name = "autofence",
5872  .handler = riscv_set_autofence,
5873  .mode = COMMAND_ANY,
5874  .usage = "[on|off]",
5875  .help = "When on (default), OpenOCD will automatically execute fence instructions in some situations. "
5876  "When off, users need to take care of memory coherency themselves, for example by using "
5877  "`riscv exec_progbuf` to execute fence or CMO instructions."
5878  },
5879  {
5880  .chain = smp_command_handlers
5881  },
5883 };
5884 
5885 /*
5886  * To be noted that RISC-V targets use the same semihosting commands as
5887  * ARM targets.
5888  *
5889  * The main reason is compatibility with existing tools. For example the
5890  * Eclipse OpenOCD/SEGGER J-Link/QEMU plug-ins have several widgets to
5891  * configure semihosting, which generate commands like `arm semihosting
5892  * enable`.
5893  * A secondary reason is the fact that the protocol used is exactly the
5894  * one specified by ARM. If RISC-V will ever define its own semihosting
5895  * protocol, then a command like `riscv semihosting enable` will make
5896  * sense, but for now all semihosting commands are prefixed with `arm`.
5897  */
5898 
5899 static const struct command_registration riscv_command_handlers[] = {
5900  {
5901  .name = "riscv",
5902  .mode = COMMAND_ANY,
5903  .help = "RISC-V Command Group",
5904  .usage = "",
5906  },
5907  {
5908  .name = "arm",
5909  .mode = COMMAND_ANY,
5910  .help = "ARM Command Group",
5911  .usage = "",
5913  },
5915 };
5916 
5917 static unsigned int riscv_xlen_nonconst(struct target *target)
5918 {
5919  return riscv_xlen(target);
5920 }
5921 
5922 static unsigned int riscv_data_bits(struct target *target)
5923 {
5924  RISCV_INFO(r);
5925  if (r->data_bits)
5926  return r->data_bits(target);
5927  return riscv_xlen(target);
5928 }
5929 
5930 struct target_type riscv_target = {
5931  .name = "riscv",
5932 
5933  .target_create = riscv_create_target,
5934  .target_jim_configure = riscv_jim_configure,
5935  .init_target = riscv_init_target,
5936  .deinit_target = riscv_deinit_target,
5937  .examine = riscv_examine,
5938 
5939  /* poll current target status */
5940  .poll = old_or_new_riscv_poll,
5941 
5942  .halt = riscv_halt,
5943  .resume = riscv_target_resume,
5944  .step = old_or_new_riscv_step,
5945 
5946  .assert_reset = riscv_assert_reset,
5947  .deassert_reset = riscv_deassert_reset,
5948 
5949  .read_memory = riscv_read_memory,
5950  .write_memory = riscv_write_memory,
5951  .read_phys_memory = riscv_read_phys_memory,
5952  .write_phys_memory = riscv_write_phys_memory,
5953 
5954  .checksum_memory = riscv_checksum_memory,
5955 
5956  .mmu = riscv_mmu,
5957  .virt2phys = riscv_virt2phys,
5958 
5959  .get_gdb_arch = riscv_get_gdb_arch,
5960  .get_gdb_reg_list = riscv_get_gdb_reg_list,
5961  .get_gdb_reg_list_noread = riscv_get_gdb_reg_list_noread,
5962 
5963  .add_breakpoint = riscv_add_breakpoint,
5964  .remove_breakpoint = riscv_remove_breakpoint,
5965 
5966  .add_watchpoint = riscv_add_watchpoint,
5967  .remove_watchpoint = riscv_remove_watchpoint,
5968  .hit_watchpoint = riscv_hit_watchpoint,
5969 
5970  .arch_state = riscv_arch_state,
5971 
5972  .run_algorithm = riscv_run_algorithm,
5973 
5974  .commands = riscv_command_handlers,
5975 
5976  .address_bits = riscv_xlen_nonconst,
5977  .data_bits = riscv_data_bits
5978 };
5979 
5980 /*** RISC-V Interface ***/
5981 
5982 /* Initializes the shared RISC-V structure. */
5983 static void riscv_info_init(struct target *target, struct riscv_info *r)
5984 {
5985  memset(r, 0, sizeof(*r));
5986 
5988 
5990  r->version_specific = NULL;
5991 
5992  memset(r->trigger_unique_id, 0xff, sizeof(r->trigger_unique_id));
5993 
5994  r->xlen = -1;
5995 
5997 
5999 
6003 
6005  for (size_t i = 0; i < RISCV_MEM_ACCESS_MAX_METHODS_NUM; ++i)
6006  r->mem_access_warn[i] = true;
6007 
6010  INIT_LIST_HEAD(&r->hide_csr);
6011 
6013 
6015  r->wp_allow_ge_lt_trigger = true;
6016  r->wp_allow_napot_trigger = true;
6017 
6018  r->autofence = true;
6019 }
6020 
6022 {
6023  RISCV_INFO(r);
6024 
6025  LOG_TARGET_DEBUG(target, "Resuming hart, state=%d.", target->state);
6026  if (target->state == TARGET_HALTED) {
6027  if (r->resume_go(target) != ERROR_OK)
6028  return ERROR_FAIL;
6029  } else {
6030  LOG_TARGET_DEBUG(target, "Hart requested resume, but was already resumed.");
6031  }
6032  return ERROR_OK;
6033 }
6034 
6035 static int riscv_interrupts_disable(struct target *target, riscv_reg_t *old_mstatus)
6036 {
6037  LOG_TARGET_DEBUG(target, "Disabling interrupts.");
6038  riscv_reg_t current_mstatus;
6039  int ret = riscv_reg_get(target, &current_mstatus, GDB_REGNO_MSTATUS);
6040  if (ret != ERROR_OK) {
6041  LOG_TARGET_ERROR(target, "Failed to read mstatus!");
6042  return ret;
6043  }
6044  if (old_mstatus)
6045  *old_mstatus = current_mstatus;
6046  return riscv_reg_set(target, GDB_REGNO_MSTATUS, current_mstatus & ~mstatus_ie_mask);
6047 }
6048 
6049 static int riscv_interrupts_restore(struct target *target, riscv_reg_t old_mstatus)
6050 {
6051  LOG_TARGET_DEBUG(target, "Restoring interrupts.");
6052  riscv_reg_t current_mstatus;
6053  int ret = riscv_reg_get(target, &current_mstatus, GDB_REGNO_MSTATUS);
6054  if (ret != ERROR_OK) {
6055  LOG_TARGET_ERROR(target, "Failed to read mstatus!");
6056  return ret;
6057  }
6058  if ((current_mstatus & mstatus_ie_mask) != 0) {
6059  LOG_TARGET_WARNING(target, "Interrupt enable bits in mstatus changed during single-step.");
6060  LOG_TARGET_WARNING(target, "OpenOCD might have affected the program when it restored the interrupt bits after single-step.");
6061  LOG_TARGET_WARNING(target, "Hint: Use 'riscv set_maskisr off' to prevent OpenOCD from touching mstatus during single-step.");
6062  }
6063  return riscv_reg_set(target, GDB_REGNO_MSTATUS, current_mstatus | (old_mstatus & mstatus_ie_mask));
6064 }
6065 
6067 {
6068  RISCV_INFO(r);
6069  LOG_TARGET_DEBUG(target, "Stepping.");
6070 
6071  if (target->state != TARGET_HALTED) {
6072  LOG_TARGET_ERROR(target, "Hart isn't halted before single step!");
6073  return ERROR_TARGET_NOT_HALTED;
6074  }
6075  r->on_step(target);
6076  if (r->step_current_hart(target) != ERROR_OK)
6077  return ERROR_FAIL;
6078  if (target->state != TARGET_HALTED) {
6079  LOG_TARGET_ERROR(target, "Hart was not halted after single step!");
6080  return ERROR_FAIL;
6081  }
6082  return ERROR_OK;
6083 }
6084 
6085 bool riscv_supports_extension(const struct target *target, char letter)
6086 {
6087  RISCV_INFO(r);
6088  unsigned int num;
6089  if (letter >= 'a' && letter <= 'z')
6090  num = letter - 'a';
6091  else if (letter >= 'A' && letter <= 'Z')
6092  num = letter - 'A';
6093  else
6094  return false;
6095  return r->misa & BIT(num);
6096 }
6097 
6098 unsigned int riscv_xlen(const struct target *target)
6099 {
6100  RISCV_INFO(r);
6101  return r->xlen;
6102 }
6103 
6104 unsigned int riscv_vlenb(const struct target *target)
6105 {
6106  RISCV_INFO(r);
6107  return r->vlenb;
6108 }
6109 
6111 {
6112  RISCV_INFO(r);
6113  assert(r->get_hart_state);
6114  return r->get_hart_state(target, state);
6115 }
6116 
6117 static enum riscv_halt_reason riscv_halt_reason(struct target *target)
6118 {
6119  RISCV_INFO(r);
6120  if (target->state != TARGET_HALTED) {
6121  LOG_TARGET_ERROR(target, "Hart is not halted!");
6122  return RISCV_HALT_UNKNOWN;
6123  }
6124  return r->halt_reason(target);
6125 }
6126 
6127 unsigned int riscv_progbuf_size(struct target *target)
6128 {
6129  RISCV_INFO(r);
6130  return r->get_progbufsize(target);
6131 }
6132 
6133 int riscv_write_progbuf(struct target *target, unsigned int index, riscv_insn_t insn)
6134 {
6135  RISCV_INFO(r);
6136  return r->write_progbuf(target, index, insn);
6137 }
6138 
6140 {
6141  RISCV_INFO(r);
6142  return r->read_progbuf(target, index);
6143 }
6144 
6145 int riscv_execute_progbuf(struct target *target, uint32_t *cmderr)
6146 {
6147  RISCV_INFO(r);
6148  return r->execute_progbuf(target, cmderr);
6149 }
6150 
6151 void riscv_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
6152 {
6153  RISCV_INFO(r);
6154  r->fill_dmi_write(target, buf, a, d);
6155 }
6156 
6157 void riscv_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
6158 {
6159  RISCV_INFO(r);
6160  r->fill_dmi_read(target, buf, a);
6161 }
6162 
6163 void riscv_fill_dm_nop(const struct target *target, uint8_t *buf)
6164 {
6165  RISCV_INFO(r);
6166  r->fill_dm_nop(target, buf);
6167 }
6168 
6169 unsigned int riscv_get_dmi_address_bits(const struct target *target)
6170 {
6171  RISCV_INFO(r);
6172  return r->get_dmi_address_bits(target);
6173 }
6174 
6175 static int check_if_trigger_exists(struct target *target, unsigned int index)
6176 {
6177  /* If we can't write tselect, then this hart does not support triggers. */
6180  riscv_reg_t tselect_rb;
6181  if (riscv_reg_get(target, &tselect_rb, GDB_REGNO_TSELECT) != ERROR_OK)
6182  return ERROR_FAIL;
6183  /* Mask off the top bit, which is used as tdrmode in legacy RISC-V Debug Spec
6184  * (old revisions of v0.11 spec). */
6185  tselect_rb &= ~(1ULL << (riscv_xlen(target) - 1));
6186  if (tselect_rb != index)
6188  return ERROR_OK;
6189 }
6190 
6196 static int get_trigger_types(struct target *target, unsigned int *trigger_tinfo,
6197  riscv_reg_t tdata1)
6198 {
6199  assert(trigger_tinfo);
6200  riscv_reg_t tinfo;
6201  if (riscv_reg_get(target, &tinfo, GDB_REGNO_TINFO) == ERROR_OK) {
6202  /* tinfo.INFO == 1: trigger doesn’t exist
6203  * tinfo == 0 or tinfo.INFO != 1 and tinfo LSB is set: invalid tinfo */
6204  if (tinfo == 0 || tinfo & 0x1)
6206  *trigger_tinfo = tinfo;
6207  return ERROR_OK;
6208  }
6209  const unsigned int type = get_field(tdata1, CSR_TDATA1_TYPE(riscv_xlen(target)));
6210  if (type == 0)
6212  *trigger_tinfo = 1 << type;
6213  return ERROR_OK;
6214 }
6215 
6217 {
6218  bool dmode_is_set = false;
6219  switch (get_field(tdata1, CSR_TDATA1_TYPE(riscv_xlen(target)))) {
6221  /* On these older cores we don't support software using
6222  * triggers. */
6223  dmode_is_set = true;
6224  break;
6226  dmode_is_set = tdata1 & CSR_MCONTROL_DMODE(riscv_xlen(target));
6227  break;
6229  dmode_is_set = tdata1 & CSR_MCONTROL6_DMODE(riscv_xlen(target));
6230  break;
6232  dmode_is_set = tdata1 & CSR_ICOUNT_DMODE(riscv_xlen(target));
6233  break;
6235  dmode_is_set = tdata1 & CSR_ITRIGGER_DMODE(riscv_xlen(target));
6236  break;
6238  dmode_is_set = tdata1 & CSR_ETRIGGER_DMODE(riscv_xlen(target));
6239  break;
6240  }
6241  if (!dmode_is_set)
6242  /* Nothing to do */
6243  return ERROR_OK;
6245 }
6246 
6255 {
6256  RISCV_INFO(r);
6257 
6258  if (r->triggers_enumerated)
6259  return ERROR_OK;
6260 
6261  if (target->state != TARGET_HALTED) {
6262  LOG_TARGET_ERROR(target, "Unable to enumerate triggers: target not halted.");
6263  return ERROR_TARGET_NOT_HALTED;
6264  }
6265 
6266  riscv_reg_t orig_tselect;
6267  int result = riscv_reg_get(target, &orig_tselect, GDB_REGNO_TSELECT);
6268  /* If tselect is not readable, the trigger module is likely not
6269  * implemented. */
6270  if (result != ERROR_OK) {
6271  LOG_TARGET_INFO(target, "Cannot access tselect register. "
6272  "Assuming that triggers are not implemented.");
6273  r->triggers_enumerated = true;
6274  r->trigger_count = 0;
6275  free(r->reserved_triggers);
6276  r->reserved_triggers = NULL;
6277  return ERROR_OK;
6278  }
6279 
6280  /* Obtaining tinfo.version value once.
6281  * No need to enumerate per-trigger.
6282  * See https://github.com/riscv/riscv-debug-spec/pull/1081.
6283  */
6284  riscv_reg_t tinfo;
6285  if (riscv_reg_get(target, &tinfo, GDB_REGNO_TINFO) == ERROR_OK) {
6286  r->tinfo_version = get_field(tinfo, CSR_TINFO_VERSION);
6287  LOG_TARGET_DEBUG(target, "Trigger tinfo.version = %d.", r->tinfo_version);
6288  } else {
6289  r->tinfo_version = RISCV_TINFO_VERSION_UNKNOWN;
6290  LOG_TARGET_DEBUG(target, "Trigger tinfo.version is unknown.");
6291  }
6292 
6293  unsigned int t = 0;
6294  for (; t < ARRAY_SIZE(r->trigger_tinfo); ++t) {
6295  result = check_if_trigger_exists(target, t);
6296  if (result == ERROR_FAIL)
6297  return ERROR_FAIL;
6299  break;
6300 
6301  riscv_reg_t tdata1;
6302  if (riscv_reg_get(target, &tdata1, GDB_REGNO_TDATA1) != ERROR_OK)
6303  return ERROR_FAIL;
6304 
6305  result = get_trigger_types(target, &r->trigger_tinfo[t], tdata1);
6306  if (result == ERROR_FAIL)
6307  return ERROR_FAIL;
6309  break;
6310 
6311  LOG_TARGET_DEBUG(target, "Trigger %u: supported types (mask) = 0x%08x",
6312  t, r->trigger_tinfo[t]);
6313 
6314  if (disable_trigger_if_dmode(target, tdata1) != ERROR_OK)
6315  return ERROR_FAIL;
6316  }
6317 
6318  if (riscv_reg_set(target, GDB_REGNO_TSELECT, orig_tselect) != ERROR_OK)
6319  return ERROR_FAIL;
6320 
6321  r->triggers_enumerated = true;
6322  r->trigger_count = t;
6323  LOG_TARGET_INFO(target, "Found %d triggers", r->trigger_count);
6324  free(r->reserved_triggers);
6325  r->reserved_triggers = calloc(t, sizeof(*r->reserved_triggers));
6327  return ERROR_OK;
6328 }
6329 
6330 void riscv_add_bscan_tunneled_scan(struct jtag_tap *tap, const struct scan_field *field,
6332 {
6334 
6335  memset(ctxt->tunneled_dr, 0, sizeof(ctxt->tunneled_dr));
6337  ctxt->tunneled_dr[3].num_bits = 1;
6338  ctxt->tunneled_dr[3].out_value = bscan_one;
6339  ctxt->tunneled_dr[2].num_bits = 7;
6340  ctxt->tunneled_dr_width = field->num_bits;
6341  ctxt->tunneled_dr[2].out_value = &ctxt->tunneled_dr_width;
6342  /* for BSCAN tunnel, there is a one-TCK skew between shift in and shift out, so
6343  scanning num_bits + 1, and then will right shift the input field after executing the queues */
6344 
6345  ctxt->tunneled_dr[1].num_bits = field->num_bits + 1;
6346  ctxt->tunneled_dr[1].out_value = field->out_value;
6347  ctxt->tunneled_dr[1].in_value = field->in_value;
6348 
6349  ctxt->tunneled_dr[0].num_bits = 3;
6350  ctxt->tunneled_dr[0].out_value = bscan_zero;
6351  } else {
6352  /* BSCAN_TUNNEL_NESTED_TAP */
6353  ctxt->tunneled_dr[0].num_bits = 1;
6354  ctxt->tunneled_dr[0].out_value = bscan_one;
6355  ctxt->tunneled_dr[1].num_bits = 7;
6356  ctxt->tunneled_dr_width = field->num_bits;
6357  ctxt->tunneled_dr[1].out_value = &ctxt->tunneled_dr_width;
6358  /* for BSCAN tunnel, there is a one-TCK skew between shift in and shift out, so
6359  scanning num_bits + 1, and then will right shift the input field after executing the queues */
6360  ctxt->tunneled_dr[2].num_bits = field->num_bits + 1;
6361  ctxt->tunneled_dr[2].out_value = field->out_value;
6362  ctxt->tunneled_dr[2].in_value = field->in_value;
6363  ctxt->tunneled_dr[3].num_bits = 3;
6364  ctxt->tunneled_dr[3].out_value = bscan_zero;
6365  }
6367 }
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:163
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:1048
const char * jtag_tap_name(const struct jtag_tap *tap)
Definition: jtag/core.c:280
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:455
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:378
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:2459
struct target_type riscv013_target
Definition: riscv-013.c:5084
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:6175
static int halt_finish(struct target *target)
Definition: riscv.c:2711
COMMAND_HELPER(ebreakx_deprecation_helper, enum riscv_priv_mode mode)
Definition: riscv.c:4932
static bool parse_csr_address(const char *reg_address_str, unsigned int *reg_addr)
Definition: riscv.c:4387
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:6098
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:6066
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:6330
static int riscv_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: riscv.c:3752
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:6196
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:6169
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:6085
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:5899
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:3845
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:6104
int riscv_get_hart_state(struct target *target, enum riscv_hart_state *state)
Definition: riscv.c:6110
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:4194
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:6127
static int parse_reg_ranges(struct list_head *ranges, const char *tcl_arg, const char *reg_type, unsigned int max_val)
Definition: riscv.c:4530
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:5930
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:5617
static void riscv_info_init(struct target *target, struct riscv_info *r)
Definition: riscv.c:5983
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:6151
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:4024
static int riscv_dmi_write(struct target *target, uint32_t dmi_address, uint32_t value)
Definition: riscv.c:4684
static unsigned int riscv_xlen_nonconst(struct target *target)
Definition: riscv.c:5917
#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:4657
int riscv_write_progbuf(struct target *target, unsigned int index, riscv_insn_t insn)
Definition: riscv.c:6133
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:6139
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:6163
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:5922
COMMAND_HANDLER(riscv_set_command_timeout_sec)
Definition: riscv.c:4303
#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:4666
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:6157
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:6145
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:6021
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:4417
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:3976
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:3584
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:6216
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:6049
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:6035
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:3840
@ RPH_RESUME
Definition: riscv.c:3842
@ RPH_NONE
Definition: riscv.c:3841
@ RPH_REMAIN_HALTED
Definition: riscv.c:3843
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:6254
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:4295
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:6117
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:223
struct target * target
Definition: target.h:224
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(* arch_state)(struct target *target)
Definition: target_type.h:37
int(* step)(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: target_type.h:47
int(* poll)(struct target *target)
Definition: target_type.h:34
int(* examine)(struct target *target)
This method is used to perform target setup that requires JTAG access.
Definition: target_type.h: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:219
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:198
struct breakpoint * breakpoints
Definition: target.h:169
unsigned int smp
Definition: target.h:197
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:1791
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2368
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2433
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:1286
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2087
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:269
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2145
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:1258
const char * debug_reason_name(const struct target *t)
Definition: target.c:256
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:467
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:3303
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:814
static bool target_was_examined(const struct target *target)
Definition: target.h:440
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:282
@ TARGET_EVENT_HALTED
Definition: target.h:262
@ TARGET_EVENT_RESUMED
Definition: target.h:263
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:281
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:243
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:821
#define TARGET_DEFAULT_POLLING_INTERVAL
Definition: target.h:829
#define ERROR_TARGET_TIMEOUT
Definition: target.h:813
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:818
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