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