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