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