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