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