OpenOCD
riscv-013.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Support for RISC-V, debug version 0.13, which is currently (2/4/17) the
5  * latest draft.
6  */
7 
8 #include <assert.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 
16 #include "target/target.h"
17 #include "target/algorithm.h"
18 #include "target/target_type.h"
19 #include <helper/align.h>
20 #include <helper/log.h>
21 #include "jtag/jtag.h"
22 #include "target/register.h"
23 #include "target/breakpoints.h"
24 #include "helper/time_support.h"
25 #include "helper/list.h"
26 #include "riscv.h"
27 #include "riscv-013.h"
28 #include "riscv_reg.h"
29 #include "riscv-013_reg.h"
30 #include "debug_defines.h"
31 #include "rtos/rtos.h"
32 #include "program.h"
33 #include "batch.h"
34 #include "debug_reg_printer.h"
35 #include "field_helpers.h"
36 
37 static int riscv013_on_step_or_resume(struct target *target, bool step);
39  bool step);
40 static int riscv013_clear_abstract_error(struct target *target);
41 
42 /* Implementations of the functions in struct riscv_info. */
43 static int dm013_select_hart(struct target *target, int hart_index);
44 static int riscv013_halt_prep(struct target *target);
45 static int riscv013_halt_go(struct target *target);
46 static int riscv013_resume_go(struct target *target);
47 static int riscv013_step_current_hart(struct target *target);
48 static int riscv013_on_step(struct target *target);
49 static int riscv013_resume_prep(struct target *target);
51 static int riscv013_write_progbuf(struct target *target, unsigned int index,
52  riscv_insn_t d);
53 static riscv_insn_t riscv013_read_progbuf(struct target *target, unsigned int
54  index);
56 static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr);
57 static void riscv013_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d);
58 static void riscv013_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a);
59 static unsigned int riscv013_get_dmi_address_bits(const struct target *target);
60 static void riscv013_fill_dm_nop(const struct target *target, uint8_t *buf);
61 static unsigned int register_size(struct target *target, enum gdb_regno number);
62 static int register_read_direct(struct target *target, riscv_reg_t *value,
63  enum gdb_regno number);
64 static int register_write_direct(struct target *target, enum gdb_regno number,
65  riscv_reg_t value);
66 static int riscv013_access_memory(struct target *target, const struct riscv_mem_access_args args);
67 static bool riscv013_get_impebreak(const struct target *target);
68 static unsigned int riscv013_get_progbufsize(const struct target *target);
69 
70 enum grouptype {
73 };
74 static int set_group(struct target *target, bool *supported, unsigned int group,
75  enum grouptype grouptype);
76 
84 #define RISCV013_INFO(r) riscv013_info_t *r = get_info(target)
85 
86 /*** JTAG registers. ***/
87 
88 typedef enum {
93 typedef enum {
98 
99 /*** Debug Bus registers. ***/
100 
101 /* TODO: CMDERR_* defines can removed */
102 #define CMDERR_NONE DM_ABSTRACTCS_CMDERR_NONE
103 #define CMDERR_BUSY DM_ABSTRACTCS_CMDERR_BUSY
104 #define CMDERR_NOT_SUPPORTED DM_ABSTRACTCS_CMDERR_NOT_SUPPORTED
105 #define CMDERR_EXCEPTION DM_ABSTRACTCS_CMDERR_EXCEPTION
106 #define CMDERR_HALT_RESUME DM_ABSTRACTCS_CMDERR_HALT_RESUME
107 #define CMDERR_OTHER DM_ABSTRACTCS_CMDERR_OTHER
108 
109 #define HART_INDEX_MULTIPLE -1
110 #define HART_INDEX_UNKNOWN -2
111 
112 typedef struct {
113  struct list_head list;
114  unsigned int abs_chain_position;
115  /* The base address to access this DM on DMI */
116  uint32_t base;
117  /* The number of harts connected to this DM. */
119  /* Indicates we already examined this DM, so don't need to do it again. */
121  /* Indicates we already reset this DM, so don't need to do it again. */
122  bool was_reset;
123  /* Targets that are connected to this DM. */
124  struct list_head target_list;
125  /* Contains the ID of the hart that is currently selected by this DM.
126  * If multiple harts are selected this is HART_INDEX_MULTIPLE. */
128 
130 
131  /* The program buffer stores executable code. 0 is an illegal instruction,
132  * so we use 0 to mean the cached value is invalid. */
133  uint32_t progbuf_cache[16];
134 
135  /* Some operations are illegal when an abstract command is running.
136  * The field is used to track whether the last command timed out, and
137  * abstractcs.busy may have remained set. In that case we may need to
138  * re-check the busy state before executing these operations. */
140 } dm013_info_t;
141 
142 typedef struct {
143  struct list_head list;
144  struct target *target;
145 } target_list_t;
146 
147 struct ac_cache {
148  uint32_t *commands;
149  size_t size;
150 };
151 
152 static int ac_cache_elem_comparator(const void *p_lhs, const void *p_rhs)
153 {
154  uint32_t lhs = *(const uint32_t *)p_lhs;
155  uint32_t rhs = *(const uint32_t *)p_rhs;
156  if (lhs < rhs)
157  return -1;
158  if (lhs > rhs)
159  return 1;
160  return 0;
161 }
162 
163 static struct ac_cache ac_cache_construct(void)
164 {
165  struct ac_cache cache = {
166  cache.commands = NULL,
167  cache.size = 0,
168  };
169  return cache;
170 }
171 
172 static void ac_cache_free(struct ac_cache *cache)
173 {
174  free(cache->commands);
175  cache->commands = NULL;
176  cache->size = 0;
177 }
178 
179 static void ac_cache_insert(struct ac_cache *cache, uint32_t command)
180 {
181  assert(cache);
182 
183  size_t old_size = cache->size;
184  size_t new_size = old_size + 1;
185  size_t entry_size = sizeof(*cache->commands);
186 
187  uint32_t *commands = realloc(cache->commands, new_size * entry_size);
188  if (!commands) {
189  LOG_ERROR("Reallocation to %zu bytes failed", new_size * entry_size);
190  return;
191  }
192 
193  commands[old_size] = command;
194  cache->commands = commands;
195  cache->size = new_size;
196 
197  qsort(cache->commands, cache->size, entry_size,
199 }
200 
201 static bool ac_cache_contains(const struct ac_cache *cache, uint32_t command)
202 {
203  return bsearch(&command, cache->commands, cache->size,
204  sizeof(*cache->commands), ac_cache_elem_comparator);
205 }
206 
207 typedef struct {
208  /* The indexed used to address this hart in its DM. */
209  unsigned int index;
210  /* Number of address bits in the dbus register. */
211  unsigned int abits;
212  /* Number of abstract command data registers. */
213  unsigned int datacount;
214  /* Number of words in the Program Buffer. */
215  unsigned int progbufsize;
216  /* Hart contains an implicit ebreak at the end of the program buffer. */
217  bool impebreak;
218 
219  /* We cache the read-only bits of sbcs here. */
220  uint32_t sbcs;
221 
222  enum yes_no_maybe progbuf_writable;
223  /* We only need the address so that we know the alignment of the buffer. */
225 
226  /* Number of run-test/idle cycles the target requests we do after each dbus
227  * access. */
228  unsigned int dtmcs_idle;
229 
230  /* This structure is used to determine how many run-test/idle to use after
231  * an access of corresponding "riscv_scan_delay_class".
232  * Values are incremented every time an access results in a busy
233  * response.
234  */
235  struct riscv_scan_delays learned_delays;
236 
237  struct ac_cache ac_not_supported_cache;
238 
239  /* Some fields from hartinfo. */
240  uint8_t datasize;
241  uint8_t dataaccess;
242  int16_t dataaddr;
243 
244  /* The width of the hartsel field. */
245  unsigned int hartsellen;
246 
247  /* DM that provides access to this target. */
249 
250  /* This target was selected using hasel. */
251  bool selected;
252 
253  /* When false, we need to set dcsr.ebreak*, halting the target if that's
254  * necessary. */
256 
257  /* This hart was placed into a halt group in examine(). */
260 
261 static OOCD_LIST_HEAD(dm_list);
262 
263 static riscv013_info_t *get_info(const struct target *target)
264 {
265  struct riscv_info *info = target->arch_info;
266  assert(info);
267  assert(info->version_specific);
268  return info->version_specific;
269 }
270 
277 {
279  if (info->dm)
280  return info->dm;
281 
282  unsigned int abs_chain_position = target->tap->abs_chain_position;
283 
284  dm013_info_t *entry;
285  dm013_info_t *dm = NULL;
286  list_for_each_entry(entry, &dm_list, list) {
287  if (entry->abs_chain_position == abs_chain_position
288  && entry->base == target->dbgbase) {
289  dm = entry;
290  break;
291  }
292  }
293 
294  if (!dm) {
295  LOG_TARGET_DEBUG(target, "Coreid [%d] Allocating new DM", target->coreid);
296  dm = calloc(1, sizeof(dm013_info_t));
297  if (!dm)
298  return NULL;
299  dm->abs_chain_position = abs_chain_position;
300 
301  /* Safety check for dbgbase */
302  assert(target->dbgbase_set || target->dbgbase == 0);
303 
304  dm->base = target->dbgbase;
305  dm->current_hartid = 0;
306  dm->hart_count = -1;
308  list_add(&dm->list, &dm_list);
309  }
310 
311  info->dm = dm;
312  target_list_t *target_entry;
313  list_for_each_entry(target_entry, &dm->target_list, list) {
314  if (target_entry->target == target)
315  return dm;
316  }
317  target_entry = calloc(1, sizeof(*target_entry));
318  if (!target_entry) {
319  info->dm = NULL;
320  return NULL;
321  }
322  target_entry->target = target;
323  list_add(&target_entry->list, &dm->target_list);
324 
325  return dm;
326 }
327 
328 static void riscv013_dm_free(struct target *target)
329 {
331  dm013_info_t *dm = info->dm;
332  if (!dm)
333  return;
334 
335  target_list_t *target_entry;
336  list_for_each_entry(target_entry, &dm->target_list, list) {
337  if (target_entry->target == target) {
338  list_del(&target_entry->list);
339  free(target_entry);
340  break;
341  }
342  }
343 
344  if (list_empty(&dm->target_list)) {
345  list_del(&dm->list);
346  free(dm);
347  }
348  info->dm = NULL;
349 }
350 
351 static struct riscv_debug_reg_ctx get_riscv_debug_reg_ctx(const struct target *target)
352 {
353  if (!target_was_examined(target)) {
354  const struct riscv_debug_reg_ctx default_context = {0};
355  return default_context;
356  }
357 
359  const struct riscv_debug_reg_ctx context = {
360  .XLEN = { .value = riscv_xlen(target), .is_set = true },
361  .DXLEN = { .value = riscv_xlen(target), .is_set = true },
362  .abits = { .value = info->abits, .is_set = true },
363  };
364  return context;
365 }
366 
368  riscv_reg_t value, const char *file, unsigned int line, const char *func)
369 {
371  return;
372  const struct riscv_debug_reg_ctx context = get_riscv_debug_reg_ctx(target);
373  char * const buf = malloc(riscv_debug_reg_to_s(NULL, reg, context, value, RISCV_DEBUG_REG_HIDE_UNNAMED_0) + 1);
374  if (!buf) {
375  LOG_ERROR("Unable to allocate memory.");
376  return;
377  }
379  log_printf_lf(LOG_LVL_DEBUG, file, line, func, "[%s] %s", target_name(target), buf);
380  free(buf);
381 }
382 
383 #define LOG_DEBUG_REG(t, r, v) log_debug_reg(t, r##_ORDINAL, v, __FILE__, __LINE__, __func__)
384 
385 static uint32_t set_dmcontrol_hartsel(uint32_t initial, int hart_index)
386 {
387  assert(hart_index != HART_INDEX_UNKNOWN);
388 
389  if (hart_index >= 0) {
391  uint32_t index_lo = hart_index & ((1 << DM_DMCONTROL_HARTSELLO_LENGTH) - 1);
392  initial = set_field(initial, DM_DMCONTROL_HARTSELLO, index_lo);
393  uint32_t index_hi = hart_index >> DM_DMCONTROL_HARTSELLO_LENGTH;
394  assert(index_hi < (1 << DM_DMCONTROL_HARTSELHI_LENGTH));
395  initial = set_field(initial, DM_DMCONTROL_HARTSELHI, index_hi);
396  } else if (hart_index == HART_INDEX_MULTIPLE) {
398  /* TODO: https://github.com/riscv/riscv-openocd/issues/748 */
399  initial = set_field(initial, DM_DMCONTROL_HARTSELLO, 0);
400  initial = set_field(initial, DM_DMCONTROL_HARTSELHI, 0);
401  }
402 
403  return initial;
404 }
405 
406 /*** Utility functions. ***/
407 
408 static void select_dmi(struct jtag_tap *tap)
409 {
410  if (bscan_tunnel_ir_width != 0) {
412  return;
413  }
414  if (!tap->enabled)
415  LOG_ERROR("BUG: Target's TAP '%s' is disabled!", jtag_tap_name(tap));
416 
417  bool need_ir_scan = false;
418  /* FIXME: make "tap" a const pointer. */
419  for (struct jtag_tap *other_tap = jtag_tap_next_enabled(NULL);
420  other_tap; other_tap = jtag_tap_next_enabled(other_tap)) {
421  if (other_tap != tap) {
422  /* Different TAP than ours - check if it is in bypass */
423  if (!other_tap->bypass) {
424  need_ir_scan = true;
425  break;
426  }
427  } else {
428  /* Our TAP - check if the correct instruction is already loaded */
429  if (!buf_eq(tap->cur_instr, select_dbus.out_value, tap->ir_length)) {
430  need_ir_scan = true;
431  break;
432  }
433  }
434  }
435 
436  if (need_ir_scan)
438 }
439 
441 {
443 
445  NULL /* discard result */);
446  if (res != ERROR_OK)
447  return res;
448 
449  return riscv_scan_increase_delay(&info->learned_delays, RISCV_DELAY_BASE);
450 }
451 
452 static void reset_learned_delays(struct target *target)
453 {
455  assert(info);
456  memset(&info->learned_delays, 0, sizeof(info->learned_delays));
457 }
458 
459 static void decrement_reset_delays_counter(struct target *target, size_t finished_scans)
460 {
461  RISCV_INFO(r);
462  if (r->reset_delays_wait < 0) {
463  assert(r->reset_delays_wait == -1);
464  return;
465  }
466  if ((size_t)r->reset_delays_wait >= finished_scans) {
467  r->reset_delays_wait -= finished_scans;
468  return;
469  }
470  r->reset_delays_wait = -1;
472  "resetting learned delays (reset_delays_wait counter expired)");
474 }
475 
476 static uint32_t riscv013_get_dmi_address(const struct target *target, uint32_t address)
477 {
478  assert(target);
479  uint32_t base = 0;
481  if (info && info->dm)
482  base = info->dm->base;
483  return address + base;
484 }
485 
486 static int batch_run_timeout(struct target *target, struct riscv_batch *batch);
487 
488 static int dmi_read(struct target *target, uint32_t *value, uint32_t address)
489 {
490  struct riscv_batch *batch = riscv_batch_alloc(target, 1);
492  int res = batch_run_timeout(target, batch);
493  if (res == ERROR_OK && value)
494  *value = riscv_batch_get_dmi_read_data(batch, 0);
495  riscv_batch_free(batch);
496  return res;
497 }
498 
499 static int dm_read(struct target *target, uint32_t *value, uint32_t address)
500 {
502 }
503 
504 static int dm_read_exec(struct target *target, uint32_t *value, uint32_t address)
505 {
506  dm013_info_t *dm = get_dm(target);
507  if (!dm)
508  return ERROR_FAIL;
509  struct riscv_batch *batch = riscv_batch_alloc(target, 1);
511  dm->abstract_cmd_maybe_busy = true;
512  int res = batch_run_timeout(target, batch);
513  if (res == ERROR_OK && value)
514  *value = riscv_batch_get_dmi_read_data(batch, 0);
515  riscv_batch_free(batch);
516  return res;
517 }
518 
519 static int dmi_write(struct target *target, uint32_t address, uint32_t value)
520 {
521  struct riscv_batch *batch = riscv_batch_alloc(target, 1);
522  riscv_batch_add_dmi_write(batch, address, value, /*read_back*/ true,
524  int res = batch_run_timeout(target, batch);
525  riscv_batch_free(batch);
526  return res;
527 }
528 
529 static int dm_write(struct target *target, uint32_t address, uint32_t value)
530 {
532 }
533 
534 static bool check_dbgbase_exists(struct target *target)
535 {
536  uint32_t next_dm = 0;
537  unsigned int count = 1;
539 
540  LOG_TARGET_DEBUG(target, "Searching for DM with DMI base address (dbgbase) = 0x%x", target->dbgbase);
541  while (1) {
542  uint32_t current_dm = next_dm;
543  if (current_dm == target->dbgbase)
544  return true;
545  if (dmi_read(target, &next_dm, DM_NEXTDM + current_dm) != ERROR_OK)
546  break;
547  LOG_TARGET_DEBUG(target, "dm @ 0x%x --> nextdm=0x%x", current_dm, next_dm);
548  /* Check if it's last one in the chain. */
549  if (next_dm == 0) {
550  LOG_TARGET_ERROR(target, "Reached the end of DM chain (detected %u DMs in total).", count);
551  break;
552  }
553  if (next_dm >> info->abits) {
554  LOG_TARGET_ERROR(target, "The address of the next Debug Module does not fit into %u bits, "
555  "which is the width of the DMI bus address. This is a HW bug",
556  info->abits);
557  break;
558  }
559  /* Safety: Avoid looping forever in case of buggy nextdm values in the hardware. */
560  if (count++ > RISCV_MAX_DMS) {
561  LOG_TARGET_ERROR(target, "Supporting no more than %d DMs on a DMI bus. Aborting", RISCV_MAX_DMS);
562  break;
563  }
564  }
565  return false;
566 }
567 
568 static int dmstatus_read(struct target *target, uint32_t *dmstatus,
569  bool authenticated)
570 {
571  int result = dm_read(target, dmstatus, DM_DMSTATUS);
572  if (result != ERROR_OK)
573  return result;
574  int dmstatus_version = get_field(*dmstatus, DM_DMSTATUS_VERSION);
575  if (dmstatus_version != 2 && dmstatus_version != 3) {
576  LOG_ERROR("OpenOCD only supports Debug Module version 2 (0.13) and 3 (1.0), not "
577  "%" PRId32 " (dmstatus=0x%" PRIx32 "). This error might be caused by a JTAG "
578  "signal issue. Try reducing the JTAG clock speed.",
579  get_field32(*dmstatus, DM_DMSTATUS_VERSION), *dmstatus);
580  } else if (authenticated && !get_field(*dmstatus, DM_DMSTATUS_AUTHENTICATED)) {
581  LOG_ERROR("Debugger is not authenticated to target Debug Module. "
582  "(dmstatus=0x%x). Use `riscv authdata_read` and "
583  "`riscv authdata_write` commands to authenticate.", *dmstatus);
584  return ERROR_FAIL;
585  }
586  return ERROR_OK;
587 }
588 
590 {
592  return riscv_scan_increase_delay(&info->learned_delays,
594 }
595 
596 static uint32_t __attribute__((unused)) abstract_register_size(unsigned int width)
597 {
598  switch (width) {
599  case 32:
601  case 64:
603  case 128:
605  default:
606  LOG_ERROR("Unsupported register width: %d", width);
607  return 0;
608  }
609 }
610 
611 static int wait_for_idle(struct target *target, uint32_t *abstractcs)
612 {
613  assert(target);
614  assert(abstractcs);
615 
616  dm013_info_t *dm = get_dm(target);
617  if (!dm) {
618  LOG_ERROR("BUG: Target %s is not assigned to any RISC-V debug module",
620  *abstractcs = 0;
621  return ERROR_FAIL;
622  }
623 
624  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
625  do {
626  if (dm_read(target, abstractcs, DM_ABSTRACTCS) != ERROR_OK) {
627  /* We couldn't read abstractcs. For safety, overwrite the output value to
628  * prevent the caller working with a stale value of abstractcs. */
629  *abstractcs = 0;
631  "potentially unrecoverable error detected - could not read abstractcs");
632  return ERROR_FAIL;
633  }
634 
635  if (get_field(*abstractcs, DM_ABSTRACTCS_BUSY) == 0) {
636  dm->abstract_cmd_maybe_busy = false;
637  return ERROR_OK;
638  }
639  } while (timeval_ms() < then);
640 
642  "Timed out after %ds waiting for busy to go low (abstractcs=0x%" PRIx32 "). "
643  "Increase the timeout with riscv set_command_timeout_sec.",
645  *abstractcs);
646 
647  if (!dm->abstract_cmd_maybe_busy)
649  "BUG: dm->abstract_cmd_maybe_busy had not been set when starting an abstract command.");
650  dm->abstract_cmd_maybe_busy = true;
651 
652  return ERROR_TIMEOUT_REACHED;
653 }
654 
655 static int dm013_select_target(struct target *target)
656 {
658  return dm013_select_hart(target, info->index);
659 }
660 
661 #define ABSTRACT_COMMAND_BATCH_SIZE 2
662 
663 static size_t abstract_cmd_fill_batch(struct riscv_batch *batch,
664  uint32_t command)
665 {
666  assert(riscv_batch_available_scans(batch)
668  riscv_batch_add_dm_write(batch, DM_COMMAND, command, /* read_back */ true,
671 }
672 
674  const struct riscv_batch *batch, size_t abstractcs_read_key,
675  uint32_t *cmderr)
676 {
677  uint32_t abstractcs = riscv_batch_get_dmi_read_data(batch,
678  abstractcs_read_key);
679  int res;
680  LOG_DEBUG_REG(target, DM_ABSTRACTCS, abstractcs);
681  if (get_field32(abstractcs, DM_ABSTRACTCS_BUSY) != 0) {
682  res = wait_for_idle(target, &abstractcs);
683  if (res != ERROR_OK)
684  goto clear_cmderr;
686  if (res != ERROR_OK)
687  goto clear_cmderr;
688  }
689 
690  dm013_info_t * const dm = get_dm(target);
691  if (!dm) {
692  LOG_ERROR("BUG: Target %s is not assigned to any RISC-V debug module",
694  return ERROR_FAIL;
695  }
696  dm->abstract_cmd_maybe_busy = false;
697 
698  *cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
699  if (*cmderr == CMDERR_NONE)
700  return ERROR_OK;
701  res = ERROR_FAIL;
703  "Abstract Command execution failed (abstractcs.cmderr = %" PRIx32 ").",
704  *cmderr);
705 clear_cmderr:
706  /* Attempt to clear the error. */
707  /* TODO: can we add a more substantial recovery if the clear operation fails? */
709  LOG_TARGET_ERROR(target, "could not clear abstractcs error");
710  return res;
711 }
712 
714 {
716  case 0:
718  case 1:
720  case 2:
722  default:
723  assert(false && "Unknown command type value");
724  return 0;
725  }
726 }
727 
728 static void mark_command_as_unsupported(struct target *target, uint32_t command)
729 {
730  LOG_TARGET_DEBUG(target, "Caching the abstract "
731  "command 0x%" PRIx32 " as not supported", command);
733  command, __FILE__, __LINE__, __func__);
734  ac_cache_insert(&get_info(target)->ac_not_supported_cache, command);
735 }
736 
738  uint32_t *cmderr)
739 {
740  assert(cmderr);
741  *cmderr = CMDERR_NONE;
744  case 0:
745  LOG_DEBUG_REG(target, AC_ACCESS_REGISTER, command);
746  break;
747  default:
748  LOG_TARGET_DEBUG(target, "command=0x%x", command);
749  break;
750  }
751  }
752 
753  dm013_info_t *dm = get_dm(target);
754  if (!dm)
755  return ERROR_FAIL;
756 
757  struct riscv_batch *batch = riscv_batch_alloc(target,
759  const size_t abstractcs_read_key = abstract_cmd_fill_batch(batch, command);
760 
761  /* Abstract commands are executed while running the batch. */
762  dm->abstract_cmd_maybe_busy = true;
763 
764  int res = batch_run_timeout(target, batch);
765  if (res != ERROR_OK)
766  goto cleanup;
767 
769  abstractcs_read_key, cmderr);
770  if (res != ERROR_OK && *cmderr == CMDERR_NOT_SUPPORTED)
772 
773 cleanup:
774  riscv_batch_free(batch);
775  return res;
776 }
777 
786 static void abstract_data_read_fill_batch(struct riscv_batch *batch, unsigned int index,
787  unsigned int size_bits)
788 {
789  assert(size_bits >= 32);
790  assert(size_bits % 32 == 0);
791  const unsigned int size_in_words = size_bits / 32;
792  const unsigned int offset = index * size_in_words;
793  for (unsigned int i = 0; i < size_in_words; ++i) {
794  const unsigned int reg_address = DM_DATA0 + offset + i;
795  riscv_batch_add_dm_read(batch, reg_address, RISCV_DELAY_BASE);
796  }
797 }
798 
800  unsigned int index, unsigned int size_bits)
801 {
802  assert(size_bits >= 32);
803  assert(size_bits % 32 == 0);
804  const unsigned int size_in_words = size_bits / 32;
805  assert(size_in_words * sizeof(uint32_t) <= sizeof(riscv_reg_t));
806  riscv_reg_t value = 0;
807  for (unsigned int i = 0; i < size_in_words; ++i) {
808  const uint32_t v = riscv_batch_get_dmi_read_data(batch, i);
809  value |= ((riscv_reg_t)v) << (i * 32);
810  }
811  return value;
812 }
813 
814 static int read_abstract_arg(struct target *target, riscv_reg_t *value,
815  unsigned int index, unsigned int size_bits)
816 {
817  assert(value);
818  assert(size_bits >= 32);
819  assert(size_bits % 32 == 0);
820  const unsigned char size_in_words = size_bits / 32;
821  struct riscv_batch * const batch = riscv_batch_alloc(target, size_in_words);
822  abstract_data_read_fill_batch(batch, index, size_bits);
823  int result = batch_run_timeout(target, batch);
824  if (result == ERROR_OK)
825  *value = abstract_data_get_from_batch(batch, index, size_bits);
826  riscv_batch_free(batch);
827  return result;
828 }
829 
838 static void abstract_data_write_fill_batch(struct riscv_batch *batch,
839  riscv_reg_t value, unsigned int index, unsigned int size_bits)
840 {
841  assert(size_bits % 32 == 0);
842  const unsigned int size_in_words = size_bits / 32;
843  assert(value <= UINT32_MAX || size_in_words > 1);
844  const unsigned int offset = index * size_in_words;
845 
846  for (unsigned int i = 0; i < size_in_words; ++i) {
847  const unsigned int reg_address = DM_DATA0 + offset + i;
848 
849  riscv_batch_add_dm_write(batch, reg_address, (uint32_t)value,
850  /* read_back */ true, RISCV_DELAY_BASE);
851  value >>= 32;
852  }
853 }
854 
855 /* TODO: reuse "abstract_data_write_fill_batch()" here*/
856 static int write_abstract_arg(struct target *target, unsigned int index,
857  riscv_reg_t value, unsigned int size_bits)
858 {
859  unsigned int offset = index * size_bits / 32;
860  switch (size_bits) {
861  default:
862  LOG_TARGET_ERROR(target, "Unsupported size: %d bits", size_bits);
863  return ERROR_FAIL;
864  case 64:
865  dm_write(target, DM_DATA0 + offset + 1, (uint32_t)(value >> 32));
866  /* falls through */
867  case 32:
868  dm_write(target, DM_DATA0 + offset, (uint32_t)value);
869  }
870  return ERROR_OK;
871 }
872 
877  unsigned int size, uint32_t flags)
878 {
879  uint32_t command = set_field(0, DM_COMMAND_CMDTYPE, 0);
880  switch (size) {
881  case 32:
883  break;
884  case 64:
886  break;
887  default:
888  LOG_TARGET_ERROR(target, "%d-bit register %s not supported.",
890  assert(0);
891  }
892 
893  if (number <= GDB_REGNO_XPR31) {
895  0x1000 + number - GDB_REGNO_ZERO);
896  } else if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
898  0x1020 + number - GDB_REGNO_FPR0);
899  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
902  } else if (number >= GDB_REGNO_COUNT) {
903  /* Custom register. */
906  assert(reg_info);
908  0xc000 + reg_info->custom_number);
909  } else {
910  assert(0);
911  }
912 
913  command |= flags;
914 
915  return command;
916 }
917 
918 static bool is_command_unsupported(struct target *target, uint32_t command)
919 {
920  bool unsupported = ac_cache_contains(&get_info(target)->ac_not_supported_cache, command);
921  if (!unsupported)
922  return false;
923 
924  LOG_TARGET_DEBUG(target, "Abstract command 0x%"
925  PRIx32 " is cached as not supported", command);
927  command, __FILE__, __LINE__, __func__);
928  return true;
929 }
930 
932  riscv_reg_t *value, enum gdb_regno number, unsigned int size)
933 {
934  /* The spec doesn't define abstract register numbers for vector registers. */
936  return ERROR_FAIL;
937 
941  return ERROR_FAIL;
942 
943  uint32_t cmderr;
944  int result = riscv013_execute_abstract_command(target, command, &cmderr);
945  if (result != ERROR_OK)
946  return result;
947 
948  if (value)
949  return read_abstract_arg(target, value, 0, size);
950 
951  return ERROR_OK;
952 }
953 
954 static int register_read_abstract(struct target *target, riscv_reg_t *value,
955  enum gdb_regno number)
956 {
957  const unsigned int size = register_size(target, number);
958 
960 }
961 
963  riscv_reg_t value)
964 {
965  dm013_info_t *dm = get_dm(target);
966  if (!dm)
967  return ERROR_FAIL;
968 
969  const unsigned int size_bits = register_size(target, number);
970  const uint32_t command = riscv013_access_register_command(target, number, size_bits,
974  return ERROR_FAIL;
975 
976  LOG_DEBUG_REG(target, AC_ACCESS_REGISTER, command);
977  assert(size_bits % 32 == 0);
978  const unsigned int size_in_words = size_bits / 32;
979  const unsigned int batch_size = size_in_words
981  struct riscv_batch * const batch = riscv_batch_alloc(target, batch_size);
982 
983  abstract_data_write_fill_batch(batch, value, /*index*/ 0, size_bits);
984  const size_t abstractcs_read_key = abstract_cmd_fill_batch(batch, command);
985  /* Abstract commands are executed while running the batch. */
986  dm->abstract_cmd_maybe_busy = true;
987 
988  int res = batch_run_timeout(target, batch);
989  if (res != ERROR_OK)
990  goto cleanup;
991 
992  uint32_t cmderr;
994  abstractcs_read_key, &cmderr);
995  if (res != ERROR_OK && cmderr == CMDERR_NOT_SUPPORTED)
997 
998 cleanup:
999  riscv_batch_free(batch);
1000  return res;
1001 }
1002 
1003 /*
1004  * Sets the AAMSIZE field of a memory access abstract command based on
1005  * the width (bits).
1006  */
1007 static uint32_t abstract_memory_size(unsigned int width)
1008 {
1009  switch (width) {
1010  case 8:
1011  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 0);
1012  case 16:
1013  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 1);
1014  case 32:
1015  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 2);
1016  case 64:
1017  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 3);
1018  case 128:
1019  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 4);
1020  default:
1021  LOG_ERROR("Unsupported memory width: %d", width);
1022  return 0;
1023  }
1024 }
1025 
1026 /*
1027  * Creates a memory access abstract command.
1028  */
1029 static uint32_t access_memory_command(struct target *target, bool virtual,
1030  unsigned int width, bool postincrement, bool is_write)
1031 {
1032  uint32_t command = set_field(0, AC_ACCESS_MEMORY_CMDTYPE, 2);
1036  postincrement);
1038 
1039  return command;
1040 }
1041 
1042 static int examine_progbuf(struct target *target)
1043 {
1045 
1046  if (info->progbuf_writable != YNM_MAYBE)
1047  return ERROR_OK;
1048 
1049  /* Figure out if progbuf is writable. */
1050 
1051  if (info->progbufsize < 1) {
1052  info->progbuf_writable = YNM_NO;
1053  LOG_TARGET_INFO(target, "No program buffer present.");
1054  return ERROR_OK;
1055  }
1056 
1058  return ERROR_FAIL;
1059 
1060  struct riscv_program program;
1061  riscv_program_init(&program, target);
1062  riscv_program_insert(&program, auipc(S0));
1063  if (riscv_program_exec(&program, target) != ERROR_OK)
1064  return ERROR_FAIL;
1065 
1066  if (register_read_direct(target, &info->progbuf_address, GDB_REGNO_S0) != ERROR_OK)
1067  return ERROR_FAIL;
1068 
1069  riscv_program_init(&program, target);
1070  riscv_program_insert(&program, sw(S0, S0, 0));
1071  int result = riscv_program_exec(&program, target);
1072 
1073  if (result != ERROR_OK) {
1074  /* This program might have failed if the program buffer is not
1075  * writable. */
1076  info->progbuf_writable = YNM_NO;
1077  return ERROR_OK;
1078  }
1079 
1080  uint32_t written;
1081  if (dm_read(target, &written, DM_PROGBUF0) != ERROR_OK)
1082  return ERROR_FAIL;
1083  if (written == (uint32_t) info->progbuf_address) {
1084  LOG_TARGET_INFO(target, "progbuf is writable at 0x%" PRIx64,
1085  info->progbuf_address);
1086  info->progbuf_writable = YNM_YES;
1087 
1088  } else {
1089  LOG_TARGET_INFO(target, "progbuf is not writeable at 0x%" PRIx64,
1090  info->progbuf_address);
1091  info->progbuf_writable = YNM_NO;
1092  }
1093 
1094  return ERROR_OK;
1095 }
1096 
1098 {
1099  return (gdb_regno >= GDB_REGNO_FPR0 && gdb_regno <= GDB_REGNO_FPR31) ||
1101  (gdb_regno == GDB_REGNO_CSR0 + CSR_FRM) ||
1103 }
1104 
1106 {
1107  return (gdb_regno >= GDB_REGNO_V0 && gdb_regno <= GDB_REGNO_V31) ||
1112  gdb_regno == GDB_REGNO_VL ||
1115 }
1116 
1118  riscv_reg_t *orig_mstatus, enum gdb_regno regno)
1119 {
1120  assert(orig_mstatus);
1121 
1122  if (!is_fpu_reg(regno) && !is_vector_reg(regno)) {
1123  /* If we don't assign orig_mstatus, clang static analysis
1124  * complains when this value is passed to
1125  * cleanup_after_register_access(). */
1126  *orig_mstatus = 0;
1127  /* No special preparation needed */
1128  return ERROR_OK;
1129  }
1130 
1131  LOG_TARGET_DEBUG(target, "Preparing mstatus to access %s",
1133 
1134  assert(target->state == TARGET_HALTED &&
1135  "The target must be halted to modify and then restore mstatus");
1136 
1137  if (riscv_reg_get(target, orig_mstatus, GDB_REGNO_MSTATUS) != ERROR_OK)
1138  return ERROR_FAIL;
1139 
1140  riscv_reg_t new_mstatus = *orig_mstatus;
1141  riscv_reg_t field_mask = is_fpu_reg(regno) ? MSTATUS_FS : MSTATUS_VS;
1142 
1143  if ((new_mstatus & field_mask) != 0)
1144  return ERROR_OK;
1145 
1146  new_mstatus = set_field(new_mstatus, field_mask, 1);
1147 
1148  if (riscv_reg_write(target, GDB_REGNO_MSTATUS, new_mstatus) != ERROR_OK)
1149  return ERROR_FAIL;
1150 
1151  LOG_TARGET_DEBUG(target, "Prepared to access %s (mstatus=0x%" PRIx64 ")",
1152  riscv_reg_gdb_regno_name(target, regno), new_mstatus);
1153  return ERROR_OK;
1154 }
1155 
1157  riscv_reg_t mstatus, enum gdb_regno regno)
1158 {
1159  if (!is_fpu_reg(regno) && !is_vector_reg(regno))
1160  /* Mstatus was not changed for this register access. No need to restore it. */
1161  return ERROR_OK;
1162 
1163  LOG_TARGET_DEBUG(target, "Restoring mstatus to 0x%" PRIx64, mstatus);
1164  return riscv_reg_write(target, GDB_REGNO_MSTATUS, mstatus);
1165 }
1166 
1167 typedef enum {
1172 
1173 typedef struct {
1174  /* How can the debugger access this memory? */
1176  /* Memory address to access the scratch memory from the hart. */
1178  /* Memory address to access the scratch memory from the debugger. */
1181 } scratch_mem_t;
1182 
1186 static int scratch_reserve(struct target *target,
1187  scratch_mem_t *scratch,
1188  struct riscv_program *program,
1189  unsigned int size_bytes)
1190 {
1191  riscv_addr_t alignment = 1;
1192  while (alignment < size_bytes)
1193  alignment *= 2;
1194 
1195  scratch->area = NULL;
1196 
1198 
1199  /* Option 1: See if data# registers can be used as the scratch memory */
1200  if (info->dataaccess == 1) {
1201  /* Sign extend dataaddr. */
1202  scratch->hart_address = info->dataaddr;
1203  if (info->dataaddr & (1<<11))
1204  scratch->hart_address |= 0xfffffffffffff000ULL;
1205  /* Align. */
1206  scratch->hart_address = (scratch->hart_address + alignment - 1) & ~(alignment - 1);
1207 
1208  if ((size_bytes + scratch->hart_address - info->dataaddr + 3) / 4 >=
1209  info->datasize) {
1210  scratch->memory_space = SPACE_DM_DATA;
1211  scratch->debug_address = (scratch->hart_address - info->dataaddr) / 4;
1212  return ERROR_OK;
1213  }
1214  }
1215 
1216  /* Option 2: See if progbuf can be used as the scratch memory */
1218  return ERROR_FAIL;
1219 
1220  /* Allow for ebreak at the end of the program. */
1221  unsigned int program_size = (program->instruction_count + 1) * 4;
1222  scratch->hart_address = (info->progbuf_address + program_size + alignment - 1) &
1223  ~(alignment - 1);
1224  if ((info->progbuf_writable == YNM_YES) &&
1225  ((size_bytes + scratch->hart_address - info->progbuf_address + 3) / 4 >=
1226  info->progbufsize)) {
1227  scratch->memory_space = SPACE_DMI_PROGBUF;
1228  scratch->debug_address = (scratch->hart_address - info->progbuf_address) / 4;
1229  return ERROR_OK;
1230  }
1231 
1232  /* Option 3: User-configured memory area as scratch RAM */
1233  if (target_alloc_working_area(target, size_bytes + alignment - 1,
1234  &scratch->area) == ERROR_OK) {
1235  scratch->hart_address = (scratch->area->address + alignment - 1) &
1236  ~(alignment - 1);
1237  scratch->memory_space = SPACE_DMI_RAM;
1238  scratch->debug_address = scratch->hart_address;
1239  return ERROR_OK;
1240  }
1241 
1242  LOG_TARGET_ERROR(target, "Couldn't find %d bytes of scratch RAM to use. Please configure "
1243  "a work area with 'configure -work-area-phys'.", size_bytes);
1244  return ERROR_FAIL;
1245 }
1246 
1247 static int scratch_release(struct target *target,
1248  scratch_mem_t *scratch)
1249 {
1250  return target_free_working_area(target, scratch->area);
1251 }
1252 
1253 static int scratch_read64(struct target *target, scratch_mem_t *scratch,
1254  uint64_t *value)
1255 {
1256  uint32_t v;
1257  switch (scratch->memory_space) {
1258  case SPACE_DM_DATA:
1259  if (dm_read(target, &v, DM_DATA0 + scratch->debug_address) != ERROR_OK)
1260  return ERROR_FAIL;
1261  *value = v;
1262  if (dm_read(target, &v, DM_DATA1 + scratch->debug_address) != ERROR_OK)
1263  return ERROR_FAIL;
1264  *value |= ((uint64_t)v) << 32;
1265  break;
1266  case SPACE_DMI_PROGBUF:
1267  if (dm_read(target, &v, DM_PROGBUF0 + scratch->debug_address) != ERROR_OK)
1268  return ERROR_FAIL;
1269  *value = v;
1270  if (dm_read(target, &v, DM_PROGBUF1 + scratch->debug_address) != ERROR_OK)
1271  return ERROR_FAIL;
1272  *value |= ((uint64_t)v) << 32;
1273  break;
1274  case SPACE_DMI_RAM:
1275  {
1276  uint8_t buffer[8] = {0};
1277  const struct riscv_mem_access_args args = {
1278  .address = scratch->debug_address,
1279  .read_buffer = buffer,
1280  .size = 4,
1281  .count = 2,
1282  .increment = 4,
1283  };
1284  if (riscv013_access_memory(target, args) != ERROR_OK)
1285  return ERROR_FAIL;
1286  *value = buf_get_u64(buffer,
1287  /* first = */ 0, /* bit_num = */ 64);
1288  }
1289  break;
1290  }
1291  return ERROR_OK;
1292 }
1293 
1294 static int scratch_write64(struct target *target, scratch_mem_t *scratch,
1295  uint64_t value)
1296 {
1297  switch (scratch->memory_space) {
1298  case SPACE_DM_DATA:
1299  dm_write(target, DM_DATA0 + scratch->debug_address, (uint32_t)value);
1300  dm_write(target, DM_DATA1 + scratch->debug_address, (uint32_t)(value >> 32));
1301  break;
1302  case SPACE_DMI_PROGBUF:
1303  dm_write(target, DM_PROGBUF0 + scratch->debug_address, (uint32_t)value);
1304  dm_write(target, DM_PROGBUF1 + scratch->debug_address, (uint32_t)(value >> 32));
1306  break;
1307  case SPACE_DMI_RAM:
1308  {
1309  uint8_t buffer[8] = {
1310  value,
1311  value >> 8,
1312  value >> 16,
1313  value >> 24,
1314  value >> 32,
1315  value >> 40,
1316  value >> 48,
1317  value >> 56
1318  };
1319  const struct riscv_mem_access_args args = {
1320  .address = scratch->debug_address,
1321  .write_buffer = buffer,
1322  .size = 4,
1323  .count = 2,
1324  .increment = 4,
1325  };
1326  if (riscv013_access_memory(target, args) != ERROR_OK)
1327  return ERROR_FAIL;
1328  }
1329  break;
1330  }
1331  return ERROR_OK;
1332 }
1333 
1335 static unsigned int register_size(struct target *target, enum gdb_regno number)
1336 {
1337  /* If reg_cache hasn't been initialized yet, make a guess. We need this for
1338  * when this function is called during examine(). */
1339  if (target->reg_cache)
1340  return target->reg_cache->reg_list[number].size;
1341  else
1342  return riscv_xlen(target);
1343 }
1344 
1345 static bool has_sufficient_progbuf(struct target *target, unsigned int size)
1346 {
1348  return info->progbufsize + info->impebreak >= size;
1349 }
1350 
1358  struct riscv_program *program, riscv_reg_t *value)
1359 {
1360  scratch_mem_t scratch;
1361 
1362  if (scratch_reserve(target, &scratch, program, 8) != ERROR_OK)
1363  return ERROR_FAIL;
1364 
1366  != ERROR_OK) {
1367  scratch_release(target, &scratch);
1368  return ERROR_FAIL;
1369  }
1370  if (riscv_program_exec(program, target) != ERROR_OK) {
1371  scratch_release(target, &scratch);
1372  return ERROR_FAIL;
1373  }
1374 
1375  int result = scratch_read64(target, &scratch, value);
1376 
1377  scratch_release(target, &scratch);
1378  return result;
1379 }
1380 
1381 static int fpr_read_progbuf(struct target *target, uint64_t *value,
1382  enum gdb_regno number)
1383 {
1384  assert(target->state == TARGET_HALTED);
1385  assert(number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31);
1386 
1387  const unsigned int freg = number - GDB_REGNO_FPR0;
1388 
1390  return ERROR_FAIL;
1391 
1392  struct riscv_program program;
1393  riscv_program_init(&program, target);
1394  if (riscv_supports_extension(target, 'D') && riscv_xlen(target) < 64) {
1395  /* There are no instructions to move all the bits from a
1396  * register, so we need to use some scratch RAM.
1397  */
1398  if (riscv_program_insert(&program, fsd(freg, S0, 0)) != ERROR_OK)
1399  return ERROR_FAIL;
1400  return internal_register_read64_progbuf_scratch(target, &program, value);
1401  }
1402  if (riscv_program_insert(&program,
1404  fmv_x_d(S0, freg) : fmv_x_w(S0, freg)) != ERROR_OK)
1405  return ERROR_FAIL;
1406 
1407  if (riscv_program_exec(&program, target) != ERROR_OK)
1408  return ERROR_FAIL;
1409 
1411 }
1412 
1413 static int csr_read_progbuf(struct target *target, uint64_t *value,
1414  enum gdb_regno number)
1415 {
1416  assert(target->state == TARGET_HALTED);
1417  assert(number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095);
1418 
1420  return ERROR_FAIL;
1421 
1422  struct riscv_program program;
1423  riscv_program_init(&program, target);
1424  if (riscv_program_csrr(&program, S0, number) != ERROR_OK)
1425  return ERROR_FAIL;
1426  if (riscv_program_exec(&program, target) != ERROR_OK)
1427  return ERROR_FAIL;
1428 
1430 }
1431 
1436 static int register_read_progbuf(struct target *target, uint64_t *value,
1437  enum gdb_regno number)
1438 {
1439  assert(target->state == TARGET_HALTED);
1440 
1442  return fpr_read_progbuf(target, value, number);
1443  else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095)
1444  return csr_read_progbuf(target, value, number);
1445 
1446  LOG_TARGET_ERROR(target, "Unexpected read of %s via program buffer.",
1448  return ERROR_FAIL;
1449 }
1450 
1458  struct riscv_program *program, riscv_reg_t value)
1459 {
1460  scratch_mem_t scratch;
1461 
1462  if (scratch_reserve(target, &scratch, program, 8) != ERROR_OK)
1463  return ERROR_FAIL;
1464 
1466  != ERROR_OK) {
1467  scratch_release(target, &scratch);
1468  return ERROR_FAIL;
1469  }
1470  if (scratch_write64(target, &scratch, value) != ERROR_OK) {
1471  scratch_release(target, &scratch);
1472  return ERROR_FAIL;
1473  }
1474  int result = riscv_program_exec(program, target);
1475 
1476  scratch_release(target, &scratch);
1477  return result;
1478 }
1479 
1481  riscv_reg_t value)
1482 {
1483  assert(target->state == TARGET_HALTED);
1484  assert(number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31);
1485  const unsigned int freg = number - GDB_REGNO_FPR0;
1486 
1488  return ERROR_FAIL;
1489 
1490  struct riscv_program program;
1491  riscv_program_init(&program, target);
1492 
1493  if (riscv_supports_extension(target, 'D') && riscv_xlen(target) < 64) {
1494  /* There are no instructions to move all the bits from a register,
1495  * so we need to use some scratch RAM.
1496  */
1497  if (riscv_program_insert(&program, fld(freg, S0, 0)) != ERROR_OK)
1498  return ERROR_FAIL;
1499  return internal_register_write64_progbuf_scratch(target, &program, value);
1500  }
1501 
1503  return ERROR_FAIL;
1504 
1505  if (riscv_program_insert(&program,
1507  fmv_d_x(freg, S0) : fmv_w_x(freg, S0)) != ERROR_OK)
1508  return ERROR_FAIL;
1509 
1510  return riscv_program_exec(&program, target);
1511 }
1512 
1513 static int vtype_write_progbuf(struct target *target, riscv_reg_t value)
1514 {
1515  assert(target->state == TARGET_HALTED);
1516 
1518  return ERROR_FAIL;
1520  return ERROR_FAIL;
1522  return ERROR_FAIL;
1523 
1524  struct riscv_program program;
1525  riscv_program_init(&program, target);
1526  if (riscv_program_insert(&program, csrr(S1, CSR_VL)) != ERROR_OK)
1527  return ERROR_FAIL;
1528  if (riscv_program_insert(&program, vsetvl(ZERO, S1, S0)) != ERROR_OK)
1529  return ERROR_FAIL;
1530 
1531  return riscv_program_exec(&program, target);
1532 }
1533 
1534 static int vl_write_progbuf(struct target *target, riscv_reg_t value)
1535 {
1536  assert(target->state == TARGET_HALTED);
1537 
1539  return ERROR_FAIL;
1541  return ERROR_FAIL;
1543  return ERROR_FAIL;
1544 
1545  struct riscv_program program;
1546  riscv_program_init(&program, target);
1547  if (riscv_program_insert(&program, csrr(S1, CSR_VTYPE)) != ERROR_OK)
1548  return ERROR_FAIL;
1549  if (riscv_program_insert(&program, vsetvl(ZERO, S0, S1)) != ERROR_OK)
1550  return ERROR_FAIL;
1551 
1552  return riscv_program_exec(&program, target);
1553 }
1554 
1556  riscv_reg_t value)
1557 {
1558  assert(target->state == TARGET_HALTED);
1559  assert(number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095);
1560 
1562  return ERROR_FAIL;
1564  return ERROR_FAIL;
1565 
1566  struct riscv_program program;
1567  riscv_program_init(&program, target);
1568  if (riscv_program_csrw(&program, S0, number) != ERROR_OK)
1569  return ERROR_FAIL;
1570 
1571  return riscv_program_exec(&program, target);
1572 }
1573 
1579  riscv_reg_t value)
1580 {
1581  assert(target->state == TARGET_HALTED);
1582 
1584  return fpr_write_progbuf(target, number, value);
1585  else if (number == GDB_REGNO_VTYPE)
1586  return vtype_write_progbuf(target, value);
1587  else if (number == GDB_REGNO_VL)
1588  return vl_write_progbuf(target, value);
1589  else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095)
1590  return csr_write_progbuf(target, number, value);
1591 
1592  LOG_TARGET_ERROR(target, "Unexpected write to %s via program buffer.",
1594  return ERROR_FAIL;
1595 }
1596 
1602  riscv_reg_t value)
1603 {
1604  LOG_TARGET_DEBUG(target, "Writing 0x%" PRIx64 " to %s", value,
1606 
1607  if (target->state != TARGET_HALTED)
1608  return register_write_abstract(target, number, value);
1609 
1610  riscv_reg_t mstatus;
1611  if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1612  return ERROR_FAIL;
1613 
1614  int result = register_write_abstract(target, number, value);
1615 
1616  if (result != ERROR_OK && target->state == TARGET_HALTED)
1617  result = register_write_progbuf(target, number, value);
1618 
1620  return ERROR_FAIL;
1621 
1622  if (result == ERROR_OK)
1624  value);
1625 
1626  return result;
1627 }
1628 
1630 static int register_read_direct(struct target *target, riscv_reg_t *value,
1631  enum gdb_regno number)
1632 {
1634 
1635  if (target->state != TARGET_HALTED)
1636  return register_read_abstract(target, value, number);
1637 
1638  riscv_reg_t mstatus;
1639 
1640  if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1641  return ERROR_FAIL;
1642 
1643  int result = register_read_abstract(target, value, number);
1644 
1645  if (result != ERROR_OK && target->state == TARGET_HALTED)
1646  result = register_read_progbuf(target, value, number);
1647 
1649  return ERROR_FAIL;
1650 
1651  if (result == ERROR_OK)
1653  *value);
1654 
1655  return result;
1656 }
1657 
1658 static int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
1659 {
1660  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1661  while (1) {
1662  uint32_t value;
1663  if (dmstatus_read(target, &value, false) != ERROR_OK)
1664  return ERROR_FAIL;
1665  if (dmstatus)
1666  *dmstatus = value;
1667  if (!get_field(value, DM_DMSTATUS_AUTHBUSY))
1668  break;
1669  if (timeval_ms() > then) {
1670  LOG_TARGET_ERROR(target, "Timed out after %ds waiting for authbusy to go low (dmstatus=0x%x). "
1671  "Increase the timeout with riscv set_command_timeout_sec.",
1673  value);
1674  return ERROR_FAIL;
1675  }
1676  }
1677 
1678  return ERROR_OK;
1679 }
1680 
1681 static int set_dcsr_ebreak(struct target *target, bool step)
1682 {
1683  LOG_TARGET_DEBUG(target, "Set dcsr.ebreak*");
1684 
1686  return ERROR_FAIL;
1687 
1689  riscv_reg_t original_dcsr, dcsr;
1690  /* We want to twiddle some bits in the debug CSR so debugging works. */
1691  if (riscv_reg_get(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
1692  return ERROR_FAIL;
1693  original_dcsr = dcsr;
1694  dcsr = set_field(dcsr, CSR_DCSR_STEP, step);
1695  const struct riscv_private_config * const config = riscv_private_config(target);
1696  dcsr = set_field(dcsr, CSR_DCSR_EBREAKM, config->dcsr_ebreak_fields[RISCV_MODE_M]);
1697  dcsr = set_field(dcsr, CSR_DCSR_EBREAKS, config->dcsr_ebreak_fields[RISCV_MODE_S]);
1698  dcsr = set_field(dcsr, CSR_DCSR_EBREAKU, config->dcsr_ebreak_fields[RISCV_MODE_U]);
1699  dcsr = set_field(dcsr, CSR_DCSR_EBREAKVS, config->dcsr_ebreak_fields[RISCV_MODE_VS]);
1700  dcsr = set_field(dcsr, CSR_DCSR_EBREAKVU, config->dcsr_ebreak_fields[RISCV_MODE_VU]);
1701  if (dcsr != original_dcsr &&
1703  return ERROR_FAIL;
1704  info->dcsr_ebreak_is_set = true;
1705  return ERROR_OK;
1706 }
1707 
1709 {
1710  RISCV_INFO(r);
1712  LOG_TARGET_DEBUG(target, "Halt to set DCSR.ebreak*");
1713 
1714  /* Remove this hart from the halt group. This won't work on all targets
1715  * because the debug spec allows halt groups to be hard-coded, but I
1716  * haven't actually encountered those in the wild yet.
1717  *
1718  * There is a possible race condition when another hart halts, and
1719  * this one is expected to also halt because it's supposed to be in the
1720  * same halt group. Or when this hart is halted when that happens.
1721  *
1722  * A better solution might be to leave the halt groups alone, and track
1723  * why we're halting when a halt occurs. When there are halt groups,
1724  * that leads to extra halting if not all harts need to set dcsr.ebreak
1725  * at the same time. It also makes for more complicated code.
1726  *
1727  * The perfect solution would be Quick Access, but I'm not aware of any
1728  * hardware that implements it.
1729  *
1730  * We don't need a perfect solution, because we only get here when a
1731  * hart spontaneously resets, or when it powers down and back up again.
1732  * Those are both relatively rare. (At least I hope so. Maybe some
1733  * design just powers each hart down for 90ms out of every 100ms)
1734  */
1735 
1736 
1737  if (info->haltgroup_supported) {
1738  bool supported;
1739  if (set_group(target, &supported, 0, HALT_GROUP) != ERROR_OK)
1740  return ERROR_FAIL;
1741  if (!supported)
1742  LOG_TARGET_ERROR(target, "Couldn't place hart in halt group 0. "
1743  "Some harts may be unexpectedly halted.");
1744  }
1745 
1746  int result = ERROR_OK;
1747 
1748  r->prepped = true;
1749  if (riscv013_halt_go(target) != ERROR_OK ||
1750  set_dcsr_ebreak(target, false) != ERROR_OK ||
1752  result = ERROR_FAIL;
1753  } else {
1756  }
1757 
1758  /* Add it back to the halt group. */
1759  if (info->haltgroup_supported) {
1760  bool supported;
1761  if (set_group(target, &supported, target->smp, HALT_GROUP) != ERROR_OK)
1762  return ERROR_FAIL;
1763  if (!supported)
1764  LOG_TARGET_ERROR(target, "Couldn't place hart back in halt group %d. "
1765  "Some harts may be unexpectedly halted.", target->smp);
1766  }
1767 
1768  return result;
1769 }
1770 
1771 /*** OpenOCD target functions. ***/
1772 
1773 static void deinit_target(struct target *target)
1774 {
1775  LOG_TARGET_DEBUG(target, "Deinitializing target.");
1776  struct riscv_info *info = target->arch_info;
1777  if (!info)
1778  return;
1779 
1780  riscv013_info_t *vsinfo = info->version_specific;
1781  if (vsinfo)
1783 
1785 
1786  free(info->version_specific);
1787  /* TODO: free register arch_info */
1788  info->version_specific = NULL;
1789 }
1790 
1791 static int set_group(struct target *target, bool *supported, unsigned int group,
1792  enum grouptype grouptype)
1793 {
1794  uint32_t write_val = DM_DMCS2_HGWRITE;
1795  assert(group <= 31);
1796  write_val = set_field(write_val, DM_DMCS2_GROUP, group);
1797  write_val = set_field(write_val, DM_DMCS2_GROUPTYPE, (grouptype == HALT_GROUP) ? 0 : 1);
1798  if (dm_write(target, DM_DMCS2, write_val) != ERROR_OK)
1799  return ERROR_FAIL;
1800  uint32_t read_val;
1801  if (dm_read(target, &read_val, DM_DMCS2) != ERROR_OK)
1802  return ERROR_FAIL;
1803  if (supported)
1804  *supported = (get_field(read_val, DM_DMCS2_GROUP) == group);
1805  return ERROR_OK;
1806 }
1807 
1809 {
1810  dm013_info_t *dm = get_dm(target);
1811  if (!dm)
1812  return ERROR_FAIL;
1813  if (!dm->abstract_cmd_maybe_busy)
1814  /* The previous abstract command ended correctly
1815  * and busy was cleared. No need to do anything. */
1816  return ERROR_OK;
1817 
1818  /* The previous abstract command timed out and abstractcs.busy
1819  * may have remained set. Wait for it to get cleared. */
1820  uint32_t abstractcs;
1821  int result = wait_for_idle(target, &abstractcs);
1822  if (result != ERROR_OK)
1823  return result;
1824  LOG_DEBUG_REG(target, DM_ABSTRACTCS, abstractcs);
1825  return ERROR_OK;
1826 }
1827 
1828 static int reset_dm(struct target *target)
1829 {
1830  /* TODO: This function returns an error when a DMI operation fails.
1831  * However, [3.14.2. Debug Module Control] states:
1832  * > 0 (inactive): ... Any accesses to the module may fail.
1833  *
1834  * Ignoring failures may introduce incompatibility with 0.13.
1835  * See https://github.com/riscv/riscv-debug-spec/issues/1021
1836  */
1837  dm013_info_t *dm = get_dm(target);
1838  assert(dm && "DM is expected to be already allocated.");
1839  assert(!dm->was_reset && "Attempt to reset an already-reset debug module.");
1840  /* `dmcontrol.hartsel` should be read first, in order not to
1841  * change it when requesting the reset, since changing it
1842  * without checking that `abstractcs.busy` is low is
1843  * prohibited.
1844  */
1845  uint32_t dmcontrol;
1846  int result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1847  if (result != ERROR_OK)
1848  return result;
1849 
1850  if (get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE)) {
1851  /* `dmcontrol.hartsel` is not changed. */
1852  dmcontrol = (dmcontrol & DM_DMCONTROL_HARTSELLO) |
1853  (dmcontrol & DM_DMCONTROL_HARTSELHI);
1854  LOG_TARGET_DEBUG(target, "Initiating DM reset.");
1855  result = dm_write(target, DM_DMCONTROL, dmcontrol);
1856  if (result != ERROR_OK)
1857  return result;
1858 
1859  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1860  LOG_TARGET_DEBUG(target, "Waiting for the DM to acknowledge reset.");
1861  do {
1862  result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1863  if (result != ERROR_OK)
1864  return result;
1865 
1866  if (timeval_ms() > then) {
1867  LOG_TARGET_ERROR(target, "DM didn't acknowledge reset in %d s. "
1868  "Increase the timeout with 'riscv set_command_timeout_sec'.",
1870  return ERROR_TIMEOUT_REACHED;
1871  }
1872  } while (get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE));
1873  LOG_TARGET_DEBUG(target, "DM reset initiated.");
1874  }
1875 
1876  LOG_TARGET_DEBUG(target, "Activating the DM.");
1878  if (result != ERROR_OK)
1879  return result;
1880 
1881  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1882  LOG_TARGET_DEBUG(target, "Waiting for the DM to come out of reset.");
1883  do {
1884  result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1885  if (result != ERROR_OK)
1886  return result;
1887 
1888  if (timeval_ms() > then) {
1889  LOG_TARGET_ERROR(target, "Debug Module did not become active in %d s. "
1890  "Increase the timeout with 'riscv set_command_timeout_sec'.",
1892  return ERROR_TIMEOUT_REACHED;
1893  }
1894  } while (!get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE));
1895 
1896  LOG_TARGET_DEBUG(target, "DM successfully reset.");
1897  dm->was_reset = true;
1898  return ERROR_OK;
1899 }
1900 
1901 static int examine_dm(struct target *target)
1902 {
1903  dm013_info_t *dm = get_dm(target);
1904  if (!dm)
1905  return ERROR_FAIL;
1906  if (dm->was_examined)
1907  return ERROR_OK;
1908 
1909  int result = ERROR_FAIL;
1910 
1911  if (dm->was_reset) {
1912  /* The DM was already reset when examining a different hart.
1913  * No need to reset it again. But for safety, assume that an abstract
1914  * command might be in progress at the moment.
1915  */
1916  dm->abstract_cmd_maybe_busy = true;
1917  } else {
1918  result = reset_dm(target);
1919  if (result != ERROR_OK)
1920  return result;
1921  }
1922 
1924 
1928  if (result != ERROR_OK)
1929  return result;
1930 
1931  uint32_t dmcontrol;
1932  result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1933  if (result != ERROR_OK)
1934  return result;
1935 
1936  dm->hasel_supported = get_field(dmcontrol, DM_DMCONTROL_HASEL);
1937 
1938  uint32_t hartsel =
1939  (get_field(dmcontrol, DM_DMCONTROL_HARTSELHI) <<
1941  get_field(dmcontrol, DM_DMCONTROL_HARTSELLO);
1942 
1943  /* Before doing anything else we must first enumerate the harts. */
1944  const int max_hart_count = MIN(RISCV_MAX_HARTS, hartsel + 1);
1945  if (dm->hart_count < 0) {
1946  for (int i = 0; i < max_hart_count; ++i) {
1947  /* TODO: This is extremely similar to
1948  * riscv013_get_hart_state().
1949  * It would be best to reuse the code.
1950  */
1951  result = dm013_select_hart(target, i);
1952  if (result != ERROR_OK)
1953  return result;
1954 
1955  uint32_t s;
1956  result = dmstatus_read(target, &s, /*authenticated*/ true);
1957  if (result != ERROR_OK)
1958  return result;
1959 
1961  break;
1962 
1963  dm->hart_count = i + 1;
1964 
1967  /* If `abstractcs.busy` is set, debugger should not
1968  * change `hartsel`.
1969  */
1970  result = wait_for_idle_if_needed(target);
1971  if (result != ERROR_OK)
1972  return result;
1973  dmcontrol = set_dmcontrol_hartsel(dmcontrol, i);
1974  result = dm_write(target, DM_DMCONTROL, dmcontrol);
1975  if (result != ERROR_OK)
1976  return result;
1977  }
1978  }
1979  LOG_TARGET_DEBUG(target, "Detected %d harts.", dm->hart_count);
1980  }
1981 
1982  if (dm->hart_count <= 0) {
1983  LOG_TARGET_ERROR(target, "No harts found!");
1984  return ERROR_FAIL;
1985  }
1986 
1987  dm->was_examined = true;
1988  return ERROR_OK;
1989 }
1990 
1991 static int examine(struct target *target)
1992 {
1993  /* We reset target state in case if something goes wrong during examine:
1994  * DTM/DM scans could fail or hart may fail to halt. */
1997 
1998  /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
1999  LOG_TARGET_DEBUG(target, "dbgbase=0x%x", target->dbgbase);
2000 
2001  uint32_t dtmcontrol;
2002  if (dtmcs_scan(target->tap, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
2003  LOG_TARGET_ERROR(target, "Could not scan dtmcontrol. Check JTAG connectivity/board power.");
2004  return ERROR_FAIL;
2005  }
2006 
2007  LOG_TARGET_DEBUG(target, "dtmcontrol=0x%x", dtmcontrol);
2008  LOG_DEBUG_REG(target, DTM_DTMCS, dtmcontrol);
2009 
2010  if (get_field(dtmcontrol, DTM_DTMCS_VERSION) != 1) {
2011  LOG_TARGET_ERROR(target, "Unsupported DTM version %" PRIu32 ". (dtmcontrol=0x%" PRIx32 ")",
2012  get_field32(dtmcontrol, DTM_DTMCS_VERSION), dtmcontrol);
2013  return ERROR_FAIL;
2014  }
2015 
2017 
2018  info->index = target->coreid;
2019  info->abits = get_field(dtmcontrol, DTM_DTMCS_ABITS);
2020  info->dtmcs_idle = get_field(dtmcontrol, DTM_DTMCS_IDLE);
2021 
2022  if (info->abits > RISCV013_DTMCS_ABITS_MAX) {
2023  /* Max. address width given by the debug specification is exceeded */
2024  LOG_TARGET_ERROR(target, "The target's debug bus (DMI) address width exceeds "
2025  "the maximum:");
2026  LOG_TARGET_ERROR(target, " found dtmcs.abits = %d; maximum is abits = %d.",
2027  info->abits, RISCV013_DTMCS_ABITS_MAX);
2028  return ERROR_FAIL;
2029  }
2030 
2031  if (info->abits == 0) {
2033  "dtmcs.abits is zero. Check JTAG connectivity/board power");
2034  return ERROR_FAIL;
2035  }
2036  if (info->abits < RISCV013_DTMCS_ABITS_MIN) {
2037  /* The requirement for minimum DMI address width of 7 bits is part of
2038  * the RISC-V Debug spec since Jan-20-2017 (commit 03df6ee7). However,
2039  * implementations exist that implement narrower DMI address. For example
2040  * Spike as of Q1/2025 uses dmi.abits = 6.
2041  *
2042  * For that reason, warn the user but continue.
2043  */
2044  LOG_TARGET_WARNING(target, "The target's debug bus (DMI) address width is "
2045  "lower than the minimum:");
2046  LOG_TARGET_WARNING(target, " found dtmcs.abits = %d; minimum is abits = %d.",
2047  info->abits, RISCV013_DTMCS_ABITS_MIN);
2048  }
2049 
2050  if (!check_dbgbase_exists(target)) {
2051  LOG_TARGET_ERROR(target, "Could not find debug module with DMI base address (dbgbase) = 0x%x", target->dbgbase);
2052  return ERROR_FAIL;
2053  }
2054 
2055  int result = examine_dm(target);
2056  if (result != ERROR_OK)
2057  return result;
2058 
2059  result = dm013_select_target(target);
2060  if (result != ERROR_OK)
2061  return result;
2062 
2063  /* We're here because we're uncertain about the state of the target. That
2064  * includes our progbuf cache. */
2066 
2067  uint32_t dmstatus;
2068  if (dmstatus_read(target, &dmstatus, false) != ERROR_OK)
2069  return ERROR_FAIL;
2070  LOG_TARGET_DEBUG(target, "dmstatus: 0x%08x", dmstatus);
2071  int dmstatus_version = get_field(dmstatus, DM_DMSTATUS_VERSION);
2072  if (dmstatus_version != 2 && dmstatus_version != 3) {
2073  /* Error was already printed out in dmstatus_read(). */
2074  return ERROR_FAIL;
2075  }
2076 
2077  uint32_t hartinfo;
2078  if (dm_read(target, &hartinfo, DM_HARTINFO) != ERROR_OK)
2079  return ERROR_FAIL;
2080 
2081  info->datasize = get_field(hartinfo, DM_HARTINFO_DATASIZE);
2082  info->dataaccess = get_field(hartinfo, DM_HARTINFO_DATAACCESS);
2083  info->dataaddr = get_field(hartinfo, DM_HARTINFO_DATAADDR);
2084 
2085  if (!get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED)) {
2086  LOG_TARGET_ERROR(target, "Debugger is not authenticated to target Debug Module. "
2087  "(dmstatus=0x%x). Use `riscv authdata_read` and "
2088  "`riscv authdata_write` commands to authenticate.", dmstatus);
2089  return ERROR_FAIL;
2090  }
2091 
2092  if (dm_read(target, &info->sbcs, DM_SBCS) != ERROR_OK)
2093  return ERROR_FAIL;
2094 
2095  /* Check that abstract data registers are accessible. */
2096  uint32_t abstractcs;
2097  if (dm_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
2098  return ERROR_FAIL;
2099  info->datacount = get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT);
2100  info->progbufsize = get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE);
2101 
2102  LOG_TARGET_INFO(target, "datacount=%d progbufsize=%d",
2103  info->datacount, info->progbufsize);
2104 
2105  info->impebreak = get_field(dmstatus, DM_DMSTATUS_IMPEBREAK);
2106 
2107  if (!has_sufficient_progbuf(target, 2)) {
2108  LOG_TARGET_WARNING(target, "We won't be able to execute fence instructions on this "
2109  "target. Memory may not always appear consistent. "
2110  "(progbufsize=%d, impebreak=%d)", info->progbufsize,
2111  info->impebreak);
2112  }
2113 
2114  /* Don't call any riscv_* functions until after we've counted the number of
2115  * cores and initialized registers. */
2116 
2117  enum riscv_hart_state state_at_examine_start;
2118  if (riscv_get_hart_state(target, &state_at_examine_start) != ERROR_OK)
2119  return ERROR_FAIL;
2120 
2121  RISCV_INFO(r);
2122  const bool hart_halted_at_examine_start = state_at_examine_start == RISCV_STATE_HALTED;
2123  if (!hart_halted_at_examine_start) {
2124  r->prepped = true;
2125  if (riscv013_halt_go(target) != ERROR_OK) {
2126  LOG_TARGET_ERROR(target, "Fatal: Hart %d failed to halt during %s",
2127  info->index, __func__);
2128  return ERROR_FAIL;
2129  }
2130  }
2131 
2133  target->debug_reason = hart_halted_at_examine_start ? DBG_REASON_UNDEFINED : DBG_REASON_DBGRQ;
2134 
2135  result = riscv013_reg_examine_all(target);
2136  if (result != ERROR_OK)
2137  return result;
2138 
2139  if (set_dcsr_ebreak(target, false) != ERROR_OK)
2140  return ERROR_FAIL;
2141 
2142  if (state_at_examine_start == RISCV_STATE_RUNNING) {
2146  } else if (state_at_examine_start == RISCV_STATE_HALTED) {
2149  }
2150 
2151  if (target->smp) {
2152  if (set_group(target, &info->haltgroup_supported, target->smp, HALT_GROUP) != ERROR_OK)
2153  return ERROR_FAIL;
2154  if (info->haltgroup_supported)
2155  LOG_TARGET_INFO(target, "Core %d made part of halt group %d.", info->index,
2156  target->smp);
2157  else
2158  LOG_TARGET_INFO(target, "Core %d could not be made part of halt group %d.",
2159  info->index, target->smp);
2160  }
2161 
2162  /* Some regression suites rely on seeing 'Examined RISC-V core' to know
2163  * when they can connect with gdb/telnet.
2164  * We will need to update those suites if we want to change that text. */
2165  LOG_TARGET_INFO(target, "Examined RISC-V core");
2166  LOG_TARGET_INFO(target, " XLEN=%d, misa=0x%" PRIx64, r->xlen, r->misa);
2167  return ERROR_OK;
2168 }
2169 
2170 static int riscv013_authdata_read(struct target *target, uint32_t *value, unsigned int index)
2171 {
2172  if (index > 0) {
2173  LOG_TARGET_ERROR(target, "Spec 0.13 only has a single authdata register.");
2174  return ERROR_FAIL;
2175  }
2176 
2178  return ERROR_FAIL;
2179 
2180  return dm_read(target, value, DM_AUTHDATA);
2181 }
2182 
2183 static int riscv013_authdata_write(struct target *target, uint32_t value, unsigned int index)
2184 {
2185  if (index > 0) {
2186  LOG_TARGET_ERROR(target, "Spec 0.13 only has a single authdata register.");
2187  return ERROR_FAIL;
2188  }
2189 
2190  uint32_t before, after;
2191  if (wait_for_authbusy(target, &before) != ERROR_OK)
2192  return ERROR_FAIL;
2193 
2194  dm_write(target, DM_AUTHDATA, value);
2195 
2196  if (wait_for_authbusy(target, &after) != ERROR_OK)
2197  return ERROR_FAIL;
2198 
2199  if (!get_field(before, DM_DMSTATUS_AUTHENTICATED) &&
2201  LOG_TARGET_INFO(target, "authdata_write resulted in successful authentication");
2202  int result = ERROR_OK;
2203  dm013_info_t *dm = get_dm(target);
2204  if (!dm)
2205  return ERROR_FAIL;
2206  target_list_t *entry;
2207  list_for_each_entry(entry, &dm->target_list, list) {
2208  if (target_examine_one(entry->target) != ERROR_OK)
2209  result = ERROR_FAIL;
2210  }
2211  return result;
2212  }
2213 
2214  return ERROR_OK;
2215 }
2216 
2217 /* Try to find out the widest memory access size depending on the selected memory access methods. */
2218 static unsigned int riscv013_data_bits(struct target *target)
2219 {
2221  RISCV_INFO(r);
2222 
2223  for (unsigned int i = 0; i < r->num_enabled_mem_access_methods; i++) {
2224  enum riscv_mem_access_method method = r->mem_access_methods[i];
2225 
2226  if (method == RISCV_MEM_ACCESS_PROGBUF) {
2228  return riscv_xlen(target);
2229  } else if (method == RISCV_MEM_ACCESS_SYSBUS) {
2230  if (get_field(info->sbcs, DM_SBCS_SBACCESS128))
2231  return 128;
2232  if (get_field(info->sbcs, DM_SBCS_SBACCESS64))
2233  return 64;
2234  if (get_field(info->sbcs, DM_SBCS_SBACCESS32))
2235  return 32;
2236  if (get_field(info->sbcs, DM_SBCS_SBACCESS16))
2237  return 16;
2238  if (get_field(info->sbcs, DM_SBCS_SBACCESS8))
2239  return 8;
2240  } else if (method == RISCV_MEM_ACCESS_ABSTRACT) {
2241  /* TODO: Once there is a spec for discovering abstract commands, we can
2242  * take those into account as well. For now we assume abstract commands
2243  * support XLEN-wide accesses. */
2244  return riscv_xlen(target);
2245  } else {
2246  assert(false);
2247  }
2248  }
2249  LOG_TARGET_ERROR(target, "Unable to determine supported data bits on this target. Assuming 32 bits.");
2250  return 32;
2251 }
2252 
2253 static COMMAND_HELPER(riscv013_print_info, struct target *target)
2254 {
2256 
2257  /* Abstract description. */
2258  riscv_print_info_line(CMD, "target", "memory.read_while_running8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2259  riscv_print_info_line(CMD, "target", "memory.write_while_running8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2260  riscv_print_info_line(CMD, "target", "memory.read_while_running16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2261  riscv_print_info_line(CMD, "target", "memory.write_while_running16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2262  riscv_print_info_line(CMD, "target", "memory.read_while_running32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2263  riscv_print_info_line(CMD, "target", "memory.write_while_running32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2264  riscv_print_info_line(CMD, "target", "memory.read_while_running64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2265  riscv_print_info_line(CMD, "target", "memory.write_while_running64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2266  riscv_print_info_line(CMD, "target", "memory.read_while_running128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2267  riscv_print_info_line(CMD, "target", "memory.write_while_running128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2268 
2269  /* Lower level description. */
2270  riscv_print_info_line(CMD, "dm", "abits", info->abits);
2271  riscv_print_info_line(CMD, "dm", "progbufsize", info->progbufsize);
2272  riscv_print_info_line(CMD, "dm", "sbversion", get_field(info->sbcs, DM_SBCS_SBVERSION));
2273  riscv_print_info_line(CMD, "dm", "sbasize", get_field(info->sbcs, DM_SBCS_SBASIZE));
2274  riscv_print_info_line(CMD, "dm", "sbaccess128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2275  riscv_print_info_line(CMD, "dm", "sbaccess64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2276  riscv_print_info_line(CMD, "dm", "sbaccess32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2277  riscv_print_info_line(CMD, "dm", "sbaccess16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2278  riscv_print_info_line(CMD, "dm", "sbaccess8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2279 
2280  uint32_t dmstatus;
2281  if (dmstatus_read(target, &dmstatus, false) == ERROR_OK)
2282  riscv_print_info_line(CMD, "dm", "authenticated", get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED));
2283 
2284  return 0;
2285 }
2286 
2287 static int try_set_vsew(struct target *target, unsigned int *debug_vsew)
2288 {
2289  RISCV_INFO(r);
2290  unsigned int encoded_vsew =
2291  (riscv_xlen(target) == 64 && r->vsew64_supported != YNM_NO) ? 3 : 2;
2292 
2293  /* Set standard element width to match XLEN, for vmv instruction to move
2294  * the least significant bits into a GPR.
2295  */
2296  if (riscv_reg_write(target, GDB_REGNO_VTYPE, encoded_vsew << 3) != ERROR_OK)
2297  return ERROR_FAIL;
2298 
2299  if (encoded_vsew == 3 && r->vsew64_supported == YNM_MAYBE) {
2300  /* Check that it's supported. */
2301  riscv_reg_t vtype;
2302 
2303  if (riscv_reg_get(target, &vtype, GDB_REGNO_VTYPE) != ERROR_OK)
2304  return ERROR_FAIL;
2305  if (vtype >> (riscv_xlen(target) - 1)) {
2306  r->vsew64_supported = YNM_NO;
2307  /* Try again. */
2308  return try_set_vsew(target, debug_vsew);
2309  }
2310  r->vsew64_supported = YNM_YES;
2311  }
2312  *debug_vsew = encoded_vsew == 3 ? 64 : 32;
2313  return ERROR_OK;
2314 }
2315 
2317  riscv_reg_t *orig_mstatus, riscv_reg_t *orig_vtype, riscv_reg_t *orig_vl,
2318  unsigned int *debug_vl, unsigned int *debug_vsew)
2319 {
2320  assert(orig_mstatus);
2321  assert(orig_vtype);
2322  assert(orig_vl);
2323  assert(debug_vl);
2324  assert(debug_vsew);
2325 
2326  RISCV_INFO(r);
2327  if (target->state != TARGET_HALTED) {
2329  "Unable to access vector register: target not halted");
2330  return ERROR_TARGET_NOT_HALTED;
2331  }
2332  if (prep_for_register_access(target, orig_mstatus, GDB_REGNO_VL) != ERROR_OK)
2333  return ERROR_FAIL;
2334 
2335  /* Save vtype and vl. */
2336  if (riscv_reg_get(target, orig_vtype, GDB_REGNO_VTYPE) != ERROR_OK)
2337  return ERROR_FAIL;
2338  if (riscv_reg_get(target, orig_vl, GDB_REGNO_VL) != ERROR_OK)
2339  return ERROR_FAIL;
2340 
2341  if (try_set_vsew(target, debug_vsew) != ERROR_OK)
2342  return ERROR_FAIL;
2343  /* Set the number of elements to be updated with results from a vector
2344  * instruction, for the vslide1down instruction.
2345  * Set it so the entire V register is updated. */
2346  *debug_vl = DIV_ROUND_UP(r->vlenb * 8, *debug_vsew);
2347  return riscv_reg_write(target, GDB_REGNO_VL, *debug_vl);
2348 }
2349 
2351  riscv_reg_t mstatus, riscv_reg_t vtype, riscv_reg_t vl)
2352 {
2353  /* Restore vtype and vl. */
2355  return ERROR_FAIL;
2357  return ERROR_FAIL;
2359 }
2360 
2361 int riscv013_get_register_buf(struct target *target, uint8_t *value,
2362  enum gdb_regno regno)
2363 {
2364  assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
2365 
2367  return ERROR_FAIL;
2368 
2369  riscv_reg_t mstatus, vtype, vl;
2370  unsigned int debug_vl, debug_vsew;
2371 
2372  if (prep_for_vector_access(target, &mstatus, &vtype, &vl,
2373  &debug_vl, &debug_vsew) != ERROR_OK)
2374  return ERROR_FAIL;
2375 
2377  return ERROR_FAIL;
2378 
2379  unsigned int vnum = regno - GDB_REGNO_V0;
2380 
2381  int result = ERROR_OK;
2382  for (unsigned int i = 0; i < debug_vl; i++) {
2383  /* Can't reuse the same program because riscv_program_exec() adds
2384  * ebreak to the end every time. */
2385  struct riscv_program program;
2386  riscv_program_init(&program, target);
2387  riscv_program_insert(&program, vmv_x_s(S0, vnum));
2388  riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
2389 
2390  /* Executing the program might result in an exception if there is some
2391  * issue with the vector implementation/instructions we're using. If that
2392  * happens, attempt to restore as usual. We may have clobbered the
2393  * vector register we tried to read already.
2394  * For other failures, we just return error because things are probably
2395  * so messed up that attempting to restore isn't going to help. */
2396  result = riscv_program_exec(&program, target);
2397  if (result == ERROR_OK) {
2398  riscv_reg_t v;
2400  return ERROR_FAIL;
2401  buf_set_u64(value, debug_vsew * i, debug_vsew, v);
2402  } else {
2404  "Failed to execute vmv/vslide1down while reading %s",
2406  break;
2407  }
2408  }
2409 
2410  if (cleanup_after_vector_access(target, mstatus, vtype, vl) != ERROR_OK)
2411  return ERROR_FAIL;
2412 
2413  return result;
2414 }
2415 
2417  const uint8_t *value)
2418 {
2419  assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
2420 
2422  return ERROR_FAIL;
2423 
2424  riscv_reg_t mstatus, vtype, vl;
2425  unsigned int debug_vl, debug_vsew;
2426 
2427  if (prep_for_vector_access(target, &mstatus, &vtype, &vl,
2428  &debug_vl, &debug_vsew) != ERROR_OK)
2429  return ERROR_FAIL;
2430 
2432  return ERROR_FAIL;
2433 
2434  unsigned int vnum = regno - GDB_REGNO_V0;
2435 
2436  struct riscv_program program;
2437  riscv_program_init(&program, target);
2438  riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
2439  int result = ERROR_OK;
2440  for (unsigned int i = 0; i < debug_vl; i++) {
2442  buf_get_u64(value, debug_vsew * i, debug_vsew)) != ERROR_OK)
2443  return ERROR_FAIL;
2444  result = riscv_program_exec(&program, target);
2445  if (result != ERROR_OK)
2446  break;
2447  }
2448 
2449  if (cleanup_after_vector_access(target, mstatus, vtype, vl) != ERROR_OK)
2450  return ERROR_FAIL;
2451 
2452  return result;
2453 }
2454 
2455 static uint32_t sb_sbaccess(unsigned int size_bytes)
2456 {
2457  switch (size_bytes) {
2458  case 1:
2459  return set_field(0, DM_SBCS_SBACCESS, 0);
2460  case 2:
2461  return set_field(0, DM_SBCS_SBACCESS, 1);
2462  case 4:
2463  return set_field(0, DM_SBCS_SBACCESS, 2);
2464  case 8:
2465  return set_field(0, DM_SBCS_SBACCESS, 3);
2466  case 16:
2467  return set_field(0, DM_SBCS_SBACCESS, 4);
2468  }
2469  assert(0);
2470  return 0;
2471 }
2472 
2473 static unsigned int get_sbaadress_reg_count(const struct target *target)
2474 {
2476  const unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2477  return DIV_ROUND_UP(sbasize, 32);
2478 }
2479 
2480 static void batch_fill_sb_write_address(const struct target *target,
2481  struct riscv_batch *batch, target_addr_t address,
2482  enum riscv_scan_delay_class sbaddr0_delay)
2483 {
2484  /* There currently is no support for >64-bit addresses in OpenOCD. */
2485  assert(sizeof(target_addr_t) == sizeof(uint64_t));
2486  const uint32_t addresses[] = {DM_SBADDRESS0, DM_SBADDRESS1, DM_SBADDRESS2, DM_SBADDRESS3};
2487  const uint32_t values[] = {(uint32_t)address, (uint32_t)(address >> 32), 0, 0};
2488  const unsigned int reg_count = get_sbaadress_reg_count(target);
2489  assert(reg_count > 0);
2490  assert(reg_count <= ARRAY_SIZE(addresses));
2491  assert(ARRAY_SIZE(addresses) == ARRAY_SIZE(values));
2492 
2493  for (unsigned int i = reg_count - 1; i > 0; --i)
2494  riscv_batch_add_dm_write(batch, addresses[i], values[i], /* read back */ true,
2496  riscv_batch_add_dm_write(batch, addresses[0], values[0], /* read back */ true,
2497  sbaddr0_delay);
2498 }
2499 
2501  enum riscv_scan_delay_class sbaddr0_delay)
2502 {
2503  struct riscv_batch *batch = riscv_batch_alloc(target,
2505  batch_fill_sb_write_address(target, batch, address, sbaddr0_delay);
2506  const int res = batch_run_timeout(target, batch);
2507  riscv_batch_free(batch);
2508  return res;
2509 }
2510 
2511 static int batch_run(struct target *target, struct riscv_batch *batch)
2512 {
2513  RISCV_INFO(r);
2515  select_dmi(target->tap);
2516  riscv_batch_add_nop(batch);
2517  const int result = riscv_batch_run_from(batch, 0, &info->learned_delays,
2518  /*resets_delays*/ r->reset_delays_wait >= 0,
2519  r->reset_delays_wait);
2520  if (result != ERROR_OK)
2521  return result;
2522  /* TODO: To use `riscv_batch_finished_scans()` here, it is needed for
2523  * all scans to not discard input, meaning
2524  * "riscv_batch_add_dm_write(..., false)" should not be used. */
2525  const size_t finished_scans = batch->used_scans;
2526  decrement_reset_delays_counter(target, finished_scans);
2527  if (riscv_batch_was_batch_busy(batch))
2529  return ERROR_OK;
2530 }
2531 
2532 /* It is expected that during creation of the batch
2533  * "riscv_batch_add_dm_write(..., false)" was not used.
2534  */
2535 static int batch_run_timeout(struct target *target, struct riscv_batch *batch)
2536 {
2538  select_dmi(target->tap);
2539  riscv_batch_add_nop(batch);
2540 
2541  size_t finished_scans = 0;
2542  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
2543  const unsigned int old_base_delay = riscv_scan_get_delay(&info->learned_delays,
2545  int result;
2546  do {
2547  RISCV_INFO(r);
2548  result = riscv_batch_run_from(batch, finished_scans,
2549  &info->learned_delays,
2550  /*resets_delays*/ r->reset_delays_wait >= 0,
2551  r->reset_delays_wait);
2552  if (result != ERROR_OK)
2553  return result;
2554  const size_t new_finished_scans = riscv_batch_finished_scans(batch);
2555  assert(new_finished_scans >= finished_scans);
2556  decrement_reset_delays_counter(target, new_finished_scans - finished_scans);
2557  finished_scans = new_finished_scans;
2558  if (!riscv_batch_was_batch_busy(batch)) {
2559  assert(finished_scans == batch->used_scans);
2560  return ERROR_OK;
2561  }
2562  result = increase_dmi_busy_delay(target);
2563  if (result != ERROR_OK)
2564  return result;
2565  } while (timeval_ms() < then);
2566 
2567  assert(result == ERROR_OK);
2568  assert(riscv_batch_was_batch_busy(batch));
2569 
2570  /* Reset dmi_busy_delay, so the value doesn't get too big. */
2571  LOG_TARGET_DEBUG(target, "%s delay is restored to %u.",
2573  old_base_delay);
2574  riscv_scan_set_delay(&info->learned_delays, RISCV_DELAY_BASE,
2575  old_base_delay);
2576 
2577  LOG_TARGET_ERROR(target, "DMI operation didn't complete in %d seconds. "
2578  "The target is either really slow or broken. You could increase "
2579  "the timeout with riscv set_command_timeout_sec.",
2581  return ERROR_TIMEOUT_REACHED;
2582 }
2583 
2584 static int sba_supports_access(struct target *target, unsigned int size_bytes)
2585 {
2587  switch (size_bytes) {
2588  case 1:
2589  return get_field(info->sbcs, DM_SBCS_SBACCESS8);
2590  case 2:
2591  return get_field(info->sbcs, DM_SBCS_SBACCESS16);
2592  case 4:
2593  return get_field(info->sbcs, DM_SBCS_SBACCESS32);
2594  case 8:
2595  return get_field(info->sbcs, DM_SBCS_SBACCESS64);
2596  case 16:
2597  return get_field(info->sbcs, DM_SBCS_SBACCESS128);
2598  default:
2599  return 0;
2600  }
2601 }
2602 
2604  struct riscv_sample_buf *buf,
2606  int64_t until_ms)
2607 {
2609  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2610  if (sbasize == 0 || sbasize > 64) {
2611  LOG_TARGET_ERROR(target, "Memory sampling is only implemented for non-zero sbasize <= 64.");
2612  return ERROR_NOT_IMPLEMENTED;
2613  }
2614 
2615  if (get_field(info->sbcs, DM_SBCS_SBVERSION) != 1) {
2616  LOG_TARGET_ERROR(target, "Memory sampling is only implemented for SBA version 1.");
2617  return ERROR_NOT_IMPLEMENTED;
2618  }
2619 
2620  uint32_t sbcs = 0;
2621  uint32_t sbcs_valid = false;
2622 
2623  uint32_t sbaddress0 = 0;
2624  bool sbaddress0_valid = false;
2625  uint32_t sbaddress1 = 0;
2626  bool sbaddress1_valid = false;
2627 
2628  /* How often to read each value in a batch. */
2629  const unsigned int repeat = 5;
2630 
2631  unsigned int enabled_count = 0;
2632  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2633  if (config->bucket[i].enabled)
2634  enabled_count++;
2635  }
2636 
2637  while (timeval_ms() < until_ms) {
2638  /*
2639  * batch_run() adds to the batch, so we can't simply reuse the same
2640  * batch over and over. So we create a new one every time through the
2641  * loop.
2642  */
2643  struct riscv_batch *batch = riscv_batch_alloc(
2644  target, 1 + enabled_count * 5 * repeat);
2645  if (!batch)
2646  return ERROR_FAIL;
2647 
2648  unsigned int result_bytes = 0;
2649  for (unsigned int n = 0; n < repeat; n++) {
2650  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2651  if (config->bucket[i].enabled) {
2652  if (!sba_supports_access(target, config->bucket[i].size_bytes)) {
2653  LOG_TARGET_ERROR(target, "Hardware does not support SBA access for %d-byte memory sampling.",
2654  config->bucket[i].size_bytes);
2655  return ERROR_NOT_IMPLEMENTED;
2656  }
2657 
2658  uint32_t sbcs_write = DM_SBCS_SBREADONADDR;
2659  if (enabled_count == 1)
2660  sbcs_write |= DM_SBCS_SBREADONDATA;
2661  sbcs_write |= sb_sbaccess(config->bucket[i].size_bytes);
2662  if (!sbcs_valid || sbcs_write != sbcs) {
2663  riscv_batch_add_dm_write(batch, DM_SBCS, sbcs_write,
2664  true, RISCV_DELAY_BASE);
2665  sbcs = sbcs_write;
2666  sbcs_valid = true;
2667  }
2668 
2669  if (sbasize > 32 &&
2670  (!sbaddress1_valid ||
2671  sbaddress1 != config->bucket[i].address >> 32)) {
2672  sbaddress1 = config->bucket[i].address >> 32;
2674  sbaddress1, true, RISCV_DELAY_BASE);
2675  sbaddress1_valid = true;
2676  }
2677  if (!sbaddress0_valid ||
2678  sbaddress0 != (config->bucket[i].address & 0xffffffff)) {
2679  sbaddress0 = config->bucket[i].address;
2681  sbaddress0, true,
2683  sbaddress0_valid = true;
2684  }
2685  if (config->bucket[i].size_bytes > 4)
2690  result_bytes += 1 + config->bucket[i].size_bytes;
2691  }
2692  }
2693  }
2694 
2695  if (buf->used + result_bytes >= buf->size) {
2696  riscv_batch_free(batch);
2697  break;
2698  }
2699 
2700  size_t sbcs_read_index = riscv_batch_add_dm_read(batch, DM_SBCS,
2702 
2703  int result = batch_run(target, batch);
2704  if (result != ERROR_OK) {
2705  riscv_batch_free(batch);
2706  return result;
2707  }
2708 
2709  /* Discard the batch when we encounter a busy state on the DMI level.
2710  * It's too much hassle to try to recover partial data. We'll try again
2711  * with a larger DMI delay. */
2712  const uint32_t sbcs_read_op = riscv_batch_get_dmi_read_op(batch, sbcs_read_index);
2713  if (sbcs_read_op == DTM_DMI_OP_BUSY) {
2714  result = increase_dmi_busy_delay(target);
2715  if (result != ERROR_OK) {
2716  riscv_batch_free(batch);
2717  return result;
2718  }
2719  continue;
2720  }
2721 
2722  uint32_t sbcs_read = riscv_batch_get_dmi_read_data(batch, sbcs_read_index);
2723  if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
2724  /* Discard this batch when we encounter "busy error" state on the System Bus level.
2725  * We'll try next time with a larger System Bus read delay. */
2727  int res = riscv_scan_increase_delay(&info->learned_delays,
2729  riscv_batch_free(batch);
2730  if (res != ERROR_OK)
2731  return res;
2732  continue;
2733  }
2734  if (get_field(sbcs_read, DM_SBCS_SBERROR)) {
2735  /* The memory we're sampling was unreadable, somehow. Give up. */
2737  riscv_batch_free(batch);
2738  return ERROR_FAIL;
2739  }
2740 
2741  unsigned int read_count = 0;
2742  for (unsigned int n = 0; n < repeat; n++) {
2743  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2744  if (config->bucket[i].enabled) {
2746  uint64_t value = 0;
2747  if (config->bucket[i].size_bytes > 4)
2748  value = ((uint64_t)riscv_batch_get_dmi_read_data(batch, read_count++)) << 32;
2749  value |= riscv_batch_get_dmi_read_data(batch, read_count++);
2750 
2751  buf->buf[buf->used] = i;
2752  buf_set_u64(buf->buf + buf->used + 1, 0, config->bucket[i].size_bytes * 8, value);
2753  buf->used += 1 + config->bucket[i].size_bytes;
2754  }
2755  }
2756  }
2757 
2758  riscv_batch_free(batch);
2759  }
2760 
2761  return ERROR_OK;
2762 }
2763 
2764 static int sample_memory(struct target *target,
2765  struct riscv_sample_buf *buf,
2767  int64_t until_ms)
2768 {
2769  if (!config->enabled)
2770  return ERROR_OK;
2771 
2772  return sample_memory_bus_v1(target, buf, config, until_ms);
2773 }
2774 
2776 {
2779  return ERROR_FAIL;
2780 
2781  uint32_t dmstatus;
2782  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
2783  return ERROR_FAIL;
2784  if (get_field(dmstatus, DM_DMSTATUS_ANYHAVERESET)) {
2785  LOG_TARGET_INFO(target, "Hart unexpectedly reset!");
2786  info->dcsr_ebreak_is_set = false;
2787  /* TODO: Can we make this more obvious to eg. a gdb user? */
2788  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE |
2790  dmcontrol = set_dmcontrol_hartsel(dmcontrol, info->index);
2791  /* If we had been halted when we reset, request another halt. If we
2792  * ended up running out of reset, then the user will (hopefully) get a
2793  * message that a reset happened, that the target is running, and then
2794  * that it is halted again once the request goes through.
2795  */
2796  if (target->state == TARGET_HALTED) {
2797  dmcontrol |= DM_DMCONTROL_HALTREQ;
2798  /* `haltreq` should not be issued if `abstractcs.busy`
2799  * is set. */
2800  int result = wait_for_idle_if_needed(target);
2801  if (result != ERROR_OK)
2802  return result;
2803  }
2804  dm_write(target, DM_DMCONTROL, dmcontrol);
2805  }
2806  if (get_field(dmstatus, DM_DMSTATUS_ALLNONEXISTENT)) {
2808  return ERROR_OK;
2809  }
2810  if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
2812  return ERROR_OK;
2813  }
2814  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED)) {
2816  return ERROR_OK;
2817  }
2818  if (get_field(dmstatus, DM_DMSTATUS_ALLRUNNING)) {
2820  return ERROR_OK;
2821  }
2822  LOG_TARGET_ERROR(target, "Couldn't determine state. dmstatus=0x%x", dmstatus);
2823  return ERROR_FAIL;
2824 }
2825 
2827  enum riscv_hart_state previous_riscv_state)
2828 {
2830 
2832  LOG_TARGET_WARNING(target, "Discarding values of dirty registers "
2833  "(due to target becoming unavailable).");
2834 
2836 
2837  info->dcsr_ebreak_is_set = false;
2838  return ERROR_OK;
2839 }
2840 
2841 static int tick(struct target *target)
2842 {
2844  if (!info->dcsr_ebreak_is_set &&
2845  target->state == TARGET_RUNNING &&
2847  return halt_set_dcsr_ebreak(target);
2848  return ERROR_OK;
2849 }
2850 
2851 static int init_target(struct command_context *cmd_ctx,
2852  struct target *target)
2853 {
2854  LOG_TARGET_DEBUG(target, "Init.");
2855  RISCV_INFO(generic_info);
2856 
2857  generic_info->select_target = &dm013_select_target;
2858  generic_info->get_hart_state = &riscv013_get_hart_state;
2859  generic_info->resume_go = &riscv013_resume_go;
2860  generic_info->step_current_hart = &riscv013_step_current_hart;
2861  generic_info->resume_prep = &riscv013_resume_prep;
2862  generic_info->halt_prep = &riscv013_halt_prep;
2863  generic_info->halt_go = &riscv013_halt_go;
2864  generic_info->on_step = &riscv013_on_step;
2865  generic_info->halt_reason = &riscv013_halt_reason;
2866  generic_info->read_progbuf = &riscv013_read_progbuf;
2867  generic_info->write_progbuf = &riscv013_write_progbuf;
2868  generic_info->execute_progbuf = &riscv013_execute_progbuf;
2869  generic_info->invalidate_cached_progbuf = &riscv013_invalidate_cached_progbuf;
2870  generic_info->fill_dmi_write = &riscv013_fill_dmi_write;
2871  generic_info->fill_dmi_read = &riscv013_fill_dmi_read;
2872  generic_info->fill_dm_nop = &riscv013_fill_dm_nop;
2873  generic_info->get_dmi_address_bits = &riscv013_get_dmi_address_bits;
2874  generic_info->authdata_read = &riscv013_authdata_read;
2875  generic_info->authdata_write = &riscv013_authdata_write;
2876  generic_info->dmi_read = &dmi_read;
2877  generic_info->dmi_write = &dmi_write;
2878  generic_info->get_dmi_address = &riscv013_get_dmi_address;
2879  generic_info->access_memory = &riscv013_access_memory;
2880  generic_info->data_bits = &riscv013_data_bits;
2881  generic_info->print_info = &riscv013_print_info;
2882  generic_info->get_impebreak = &riscv013_get_impebreak;
2883  generic_info->get_progbufsize = &riscv013_get_progbufsize;
2884 
2885  generic_info->handle_became_unavailable = &handle_became_unavailable;
2886  generic_info->tick = &tick;
2887 
2888  if (!generic_info->version_specific) {
2889  generic_info->version_specific = calloc(1, sizeof(riscv013_info_t));
2890  if (!generic_info->version_specific)
2891  return ERROR_FAIL;
2892  }
2893  generic_info->sample_memory = sample_memory;
2895 
2896  info->progbufsize = -1;
2898 
2899  info->ac_not_supported_cache = ac_cache_construct();
2900 
2901  return ERROR_OK;
2902 }
2903 
2904 static int assert_reset(struct target *target)
2905 {
2907  int result;
2908 
2909  select_dmi(target->tap);
2910 
2912  /* Run the user-supplied script if there is one. */
2914  } else {
2915  dm013_info_t *dm = get_dm(target);
2916  if (!dm)
2917  return ERROR_FAIL;
2918 
2919  uint32_t control = set_field(0, DM_DMCONTROL_DMACTIVE, 1);
2920  control = set_dmcontrol_hartsel(control, info->index);
2921  control = set_field(control, DM_DMCONTROL_HALTREQ,
2922  target->reset_halt ? 1 : 0);
2923  control = set_field(control, DM_DMCONTROL_NDMRESET, 1);
2924  /* If `abstractcs.busy` is set, debugger should not
2925  * change `hartsel` or set `haltreq`
2926  */
2927  const bool hartsel_changed = (int)info->index != dm->current_hartid;
2928  if (hartsel_changed || target->reset_halt) {
2929  result = wait_for_idle_if_needed(target);
2930  if (result != ERROR_OK)
2931  return result;
2932  }
2933  result = dm_write(target, DM_DMCONTROL, control);
2934  if (result != ERROR_OK)
2935  return result;
2936  }
2937 
2939 
2940  /* The DM might have gotten reset if OpenOCD called us in some reset that
2941  * involves SRST being toggled. So clear our cache which may be out of
2942  * date. */
2944 }
2945 
2947 {
2948  const struct riscv_private_config * const config = riscv_private_config(target);
2949  for (int i = 0; i < N_RISCV_MODE; ++i)
2950  if (config->dcsr_ebreak_fields[i])
2951  return false;
2952  return true;
2953 }
2954 
2955 static int deassert_reset(struct target *target)
2956 {
2958  dm013_info_t *dm = get_dm(target);
2959  if (!dm)
2960  return ERROR_FAIL;
2961  int result;
2962 
2963  select_dmi(target->tap);
2964  /* Clear the reset, but make sure haltreq is still set */
2965  uint32_t control = 0;
2966  control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
2967  control = set_field(control, DM_DMCONTROL_HALTREQ, target->reset_halt ? 1 : 0);
2968  control = set_dmcontrol_hartsel(control, info->index);
2969  /* If `abstractcs.busy` is set, debugger should not
2970  * change `hartsel`.
2971  */
2972  const bool hartsel_changed = (int)info->index != dm->current_hartid;
2973  if (hartsel_changed) {
2974  result = wait_for_idle_if_needed(target);
2975  if (result != ERROR_OK)
2976  return result;
2977  }
2978  result = dm_write(target, DM_DMCONTROL, control);
2979  if (result != ERROR_OK)
2980  return result;
2981 
2982  uint32_t dmstatus;
2983  const unsigned int orig_base_delay = riscv_scan_get_delay(&info->learned_delays,
2985  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
2986  LOG_TARGET_DEBUG(target, "Waiting for hart to come out of reset.");
2987  do {
2988  result = dmstatus_read(target, &dmstatus, true);
2989  if (result != ERROR_OK)
2990  return result;
2991 
2992  if (timeval_ms() > then) {
2993  LOG_TARGET_ERROR(target, "Hart didn't leave reset in %ds; "
2994  "dmstatus=0x%x (allunavail=%s, allhavereset=%s); "
2995  "Increase the timeout with riscv set_command_timeout_sec.",
2996  riscv_get_command_timeout_sec(), dmstatus,
2997  get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL) ? "true" : "false",
2998  get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET) ? "true" : "false");
2999  return ERROR_TIMEOUT_REACHED;
3000  }
3001  } while (!get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET));
3002 
3003  riscv_scan_set_delay(&info->learned_delays, RISCV_DELAY_BASE,
3004  orig_base_delay);
3005 
3006  /* Ack reset and clear DM_DMCONTROL_HALTREQ if previously set */
3007  control = 0;
3008  control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
3009  control = set_field(control, DM_DMCONTROL_ACKHAVERESET, 1);
3010  control = set_dmcontrol_hartsel(control, info->index);
3011  result = dm_write(target, DM_DMCONTROL, control);
3012  if (result != ERROR_OK)
3013  return result;
3014 
3015  if (target->reset_halt) {
3018  } else {
3021  }
3022  info->dcsr_ebreak_is_set = dcsr_ebreak_config_equals_reset_value(target);
3023  return ERROR_OK;
3024 }
3025 
3026 static int execute_autofence(struct target *target)
3027 {
3029  return ERROR_FAIL;
3030 
3031  RISCV_INFO(r);
3032  if (!r->autofence)
3033  return ERROR_OK;
3034 
3035  /* FIXME: For non-coherent systems we need to flush the caches right
3036  * here, but there's no ISA-defined way of doing that. */
3037  struct riscv_program program;
3038 
3039  /* program.execution_result may indicate RISCV_PROGBUF_EXEC_RESULT_EXCEPTION -
3040  * currently, we ignore this error since most likely this is an indication
3041  * that target does not support a fence instruction (execution of an
3042  * unsupported instruction results in "Illegal instruction" exception on
3043  * targets that comply with riscv-privilege spec).
3044  * Currently, RISC-V specification does not provide us with a portable and
3045  * less invasive way to detect if a fence is supported by the target. We may
3046  * revise this code once the spec allows us to do this */
3047  if (has_sufficient_progbuf(target, 3)) {
3048  riscv_program_init(&program, target);
3049  riscv_program_fence_i(&program);
3050  riscv_program_fence_rw_rw(&program);
3051  if (riscv_program_exec(&program, target) != ERROR_OK) {
3053  LOG_TARGET_ERROR(target, "Unexpected error during fence execution");
3054  return ERROR_FAIL;
3055  }
3056  LOG_TARGET_DEBUG(target, "Unable to execute fence.i and fence rw, rw");
3057  }
3058  LOG_TARGET_DEBUG(target, "Successfully executed fence.i and fence rw, rw");
3059  return ERROR_OK;
3060  }
3061 
3062  if (has_sufficient_progbuf(target, 2)) {
3063  riscv_program_init(&program, target);
3064  riscv_program_fence_i(&program);
3065  if (riscv_program_exec(&program, target) != ERROR_OK) {
3067  LOG_TARGET_ERROR(target, "Unexpected error during fence.i execution");
3068  return ERROR_FAIL;
3069  }
3070  LOG_TARGET_DEBUG(target, "Unable to execute fence.i");
3071  }
3072  LOG_TARGET_DEBUG(target, "Successfully executed fence.i");
3073 
3074  riscv_program_init(&program, target);
3075  riscv_program_fence_rw_rw(&program);
3076  if (riscv_program_exec(&program, target) != ERROR_OK) {
3078  LOG_TARGET_ERROR(target, "Unexpected error during fence rw, rw execution");
3079  return ERROR_FAIL;
3080  }
3081  LOG_TARGET_DEBUG(target, "Unable to execute fence rw, rw");
3082  }
3083  LOG_TARGET_DEBUG(target, "Successfully executed fence rw, rw");
3084  return ERROR_OK;
3085  }
3086 
3087  return ERROR_FAIL;
3088 }
3089 
3090 static void log_memory_access128(target_addr_t address, uint64_t value_h,
3091  uint64_t value_l, bool is_read)
3092 {
3094  return;
3095 
3096  char fmt[80];
3097  sprintf(fmt, "M[0x%" TARGET_PRIxADDR "] %ss 0x%%016" PRIx64 "%%016" PRIx64,
3098  address, is_read ? "read" : "write");
3099  LOG_DEBUG(fmt, value_h, value_l);
3100 }
3101 
3102 static void log_memory_access64(target_addr_t address, uint64_t value,
3103  unsigned int size_bytes, bool is_read)
3104 {
3106  return;
3107 
3108  char fmt[80];
3109  sprintf(fmt, "M[0x%" TARGET_PRIxADDR "] %ss 0x%%0%d" PRIx64,
3110  address, is_read ? "read" : "write", size_bytes * 2);
3111  switch (size_bytes) {
3112  case 1:
3113  value &= 0xff;
3114  break;
3115  case 2:
3116  value &= 0xffff;
3117  break;
3118  case 4:
3119  value &= 0xffffffffUL;
3120  break;
3121  case 8:
3122  break;
3123  default:
3124  assert(false);
3125  }
3126  LOG_DEBUG(fmt, value);
3127 }
3128 static void log_memory_access(target_addr_t address, uint32_t *sbvalue,
3129  unsigned int size_bytes, bool is_read)
3130 {
3131  if (size_bytes == 16) {
3132  uint64_t value_h = ((uint64_t)sbvalue[3] << 32) | sbvalue[2];
3133  uint64_t value_l = ((uint64_t)sbvalue[1] << 32) | sbvalue[0];
3134  log_memory_access128(address, value_h, value_l, is_read);
3135  } else {
3136  uint64_t value = ((uint64_t)sbvalue[1] << 32) | sbvalue[0];
3137  log_memory_access64(address, value, size_bytes, is_read);
3138  }
3139 }
3140 
3141 /* Read the relevant sbdata regs depending on size, and put the results into
3142  * buffer. */
3144  uint32_t size, uint8_t *buffer)
3145 {
3146  int result;
3147  uint32_t sbvalue[4] = { 0 };
3148  static int sbdata[4] = { DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3 };
3149  assert(size <= 16);
3150  for (int i = (size - 1) / 4; i >= 0; i--) {
3151  result = dm_read(target, &sbvalue[i], sbdata[i]);
3152  if (result != ERROR_OK)
3153  return result;
3154  buf_set_u32(buffer + i * 4, 0, 8 * MIN(size, 4), sbvalue[i]);
3155  }
3156  log_memory_access(address, sbvalue, size, true);
3157  return ERROR_OK;
3158 }
3159 
3161 {
3163  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
3164  target_addr_t address = 0;
3165  uint32_t v;
3166  if (sbasize > 32) {
3167  if (dm_read(target, &v, DM_SBADDRESS1) == ERROR_OK)
3168  address |= v;
3169  address <<= 32;
3170  }
3171  if (dm_read(target, &v, DM_SBADDRESS0) == ERROR_OK)
3172  address |= v;
3173  return address;
3174 }
3175 
3176 static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
3177 {
3178  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
3179  while (1) {
3180  if (dm_read(target, sbcs, DM_SBCS) != ERROR_OK)
3181  return ERROR_FAIL;
3182  if (!get_field(*sbcs, DM_SBCS_SBBUSY))
3183  return ERROR_OK;
3184  if (timeval_ms() > then) {
3185  LOG_TARGET_ERROR(target, "Timed out after %ds waiting for sbbusy to go low (sbcs=0x%x). "
3186  "Increase the timeout with riscv set_command_timeout_sec.",
3188  return ERROR_FAIL;
3189  }
3190  }
3191 }
3192 
3193 /* TODO: return struct mem_access_result */
3194 static int modify_privilege_for_virt2phys_mode(struct target *target, riscv_reg_t *mstatus, riscv_reg_t *mstatus_old,
3195  riscv_reg_t *dcsr, riscv_reg_t *dcsr_old)
3196 {
3197  assert(mstatus);
3198  assert(mstatus_old);
3199  assert(dcsr);
3200  assert(dcsr_old);
3202  return ERROR_OK;
3203 
3204  /* Read and save DCSR */
3206  return ERROR_FAIL;
3207  *dcsr_old = *dcsr;
3208 
3209  /* Read and save MSTATUS */
3210  if (riscv_reg_get(target, mstatus, GDB_REGNO_MSTATUS) != ERROR_OK)
3211  return ERROR_FAIL;
3212  *mstatus_old = *mstatus;
3213 
3214  /* If we come from m-mode with mprv set, we want to keep mpp */
3215  if (get_field(*dcsr, CSR_DCSR_PRV) == PRV_M)
3216  return ERROR_OK;
3217 
3218  /* mstatus.mpp <- dcsr.prv */
3219  *mstatus = set_field(*mstatus, MSTATUS_MPP, get_field(*dcsr, CSR_DCSR_PRV));
3220 
3221  /* mstatus.mprv <- 1 */
3222  *mstatus = set_field(*mstatus, MSTATUS_MPRV, 1);
3223 
3224  /* Write MSTATUS */
3225  if (*mstatus != *mstatus_old &&
3227  return ERROR_FAIL;
3228 
3229  /* dcsr.mprven <- 1 */
3231 
3232  /* Write DCSR */
3233  if (*dcsr != *dcsr_old &&
3235  return ERROR_FAIL;
3236 
3237  return ERROR_OK;
3238 }
3239 
3241  riscv_reg_t dcsr, riscv_reg_t dcsr_old)
3242 {
3244  return ERROR_OK;
3245 
3246  /* Restore MSTATUS */
3247  if (mstatus != mstatus_old &&
3248  riscv_reg_set(target, GDB_REGNO_MSTATUS, mstatus_old) != ERROR_OK)
3249  return ERROR_FAIL;
3250 
3251  /* Restore DCSR */
3252  if (dcsr != dcsr_old &&
3254  return ERROR_FAIL;
3255 
3256  return ERROR_OK;
3257 }
3258 
3259 static int read_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
3260 {
3261  assert(riscv_mem_access_is_read(args));
3262 
3263  if (args.size != args.increment) {
3264  LOG_TARGET_ERROR(target, "sba v0 reads only support size==increment");
3265  return ERROR_NOT_IMPLEMENTED;
3266  }
3267 
3268  LOG_TARGET_DEBUG(target, "System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
3269  TARGET_PRIxADDR, args.size, args.count, args.address);
3270  uint8_t *t_buffer = args.read_buffer;
3271  riscv_addr_t cur_addr = args.address;
3272  riscv_addr_t fin_addr = args.address + (args.count * args.size);
3273  uint32_t access = 0;
3274 
3275  const int DM_SBCS_SBSINGLEREAD_OFFSET = 20;
3276  const uint32_t DM_SBCS_SBSINGLEREAD = (0x1U << DM_SBCS_SBSINGLEREAD_OFFSET);
3277 
3278  const int DM_SBCS_SBAUTOREAD_OFFSET = 15;
3279  const uint32_t DM_SBCS_SBAUTOREAD = (0x1U << DM_SBCS_SBAUTOREAD_OFFSET);
3280 
3281  /* ww favorise one off reading if there is an issue */
3282  if (args.count == 1) {
3283  for (uint32_t i = 0; i < args.count; i++) {
3284  if (dm_read(target, &access, DM_SBCS) != ERROR_OK)
3285  return ERROR_FAIL;
3286  dm_write(target, DM_SBADDRESS0, cur_addr);
3287  /* size/2 matching the bit sbaccess of the spec 0.13 */
3288  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
3289  access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
3290  LOG_TARGET_DEBUG(target, "read_memory: sab: access: 0x%08x", access);
3291  dm_write(target, DM_SBCS, access);
3292  /* 3) read */
3293  uint32_t value;
3294  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3295  return ERROR_FAIL;
3296  LOG_TARGET_DEBUG(target, "read_memory: sab: value: 0x%08x", value);
3297  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3298  t_buffer += args.size;
3299  cur_addr += args.size;
3300  }
3301  return ERROR_OK;
3302  }
3303 
3304  /* has to be the same size if we want to read a block */
3305  LOG_TARGET_DEBUG(target, "Reading block until final address 0x%" PRIx64, fin_addr);
3306  if (dm_read(target, &access, DM_SBCS) != ERROR_OK)
3307  return ERROR_FAIL;
3308  /* set current address */
3309  dm_write(target, DM_SBADDRESS0, cur_addr);
3310  /* 2) write sbaccess=2, sbsingleread,sbautoread,sbautoincrement
3311  * size/2 matching the bit access of the spec 0.13 */
3312  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
3313  access = set_field(access, DM_SBCS_SBAUTOREAD, 1);
3314  access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
3315  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
3316  LOG_TARGET_DEBUG(target, "access: 0x%08x", access);
3317  dm_write(target, DM_SBCS, access);
3318 
3319  while (cur_addr < fin_addr) {
3320  LOG_TARGET_DEBUG(target, "sab:autoincrement:\r\n\tsize: %d\tcount:%d\taddress: 0x%08"
3321  PRIx64, args.size, args.count, cur_addr);
3322  /* read */
3323  uint32_t value;
3324  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3325  return ERROR_FAIL;
3326  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3327  cur_addr += args.size;
3328  t_buffer += args.size;
3329 
3330  /* if we are reaching last address, we must clear autoread */
3331  if (cur_addr == fin_addr && args.count != 1) {
3332  dm_write(target, DM_SBCS, 0);
3333  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3334  return ERROR_FAIL;
3335  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3336  }
3337  }
3338 
3339  uint32_t sbcs;
3340  if (dm_read(target, &sbcs, DM_SBCS) != ERROR_OK)
3341  return ERROR_FAIL;
3342 
3343  return ERROR_OK;
3344 }
3345 
3349 static int read_memory_bus_v1(struct target *target, const struct riscv_mem_access_args args)
3350 {
3351  assert(riscv_mem_access_is_read(args));
3352 
3353  const target_addr_t address = args.address;
3354  const uint32_t increment = args.increment;
3355  const uint32_t count = args.count;
3356  const uint32_t size = args.size;
3357  uint8_t *buffer = args.read_buffer;
3358 
3359  if (increment != size && increment != 0) {
3360  LOG_TARGET_ERROR(target, "sba v1 reads only support increment of size or 0");
3361  return ERROR_NOT_IMPLEMENTED;
3362  }
3363 
3364  assert(size <= 16);
3365  assert(IS_PWR_OF_2(size));
3366 
3367  dm013_info_t *dm = get_dm(target);
3368  if (!dm)
3369  return ERROR_FAIL;
3370 
3372  target_addr_t next_address = address;
3373  target_addr_t end_address = address + (increment ? count : 1) * size;
3374 
3375  /* TODO: Reading all the elements in a single batch will boost the
3376  * performance.
3377  */
3378  while (next_address < end_address) {
3379  uint32_t sbcs_write = set_field(0, DM_SBCS_SBREADONADDR, 1);
3380  sbcs_write |= sb_sbaccess(size);
3381  if (increment == size)
3382  sbcs_write = set_field(sbcs_write, DM_SBCS_SBAUTOINCREMENT, 1);
3383  if (count > 1)
3384  sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, count > 1);
3385  if (dm_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
3386  return ERROR_FAIL;
3387 
3388  /* This address write will trigger the first read. */
3390  return ERROR_FAIL;
3391 
3392  /* First read has been started. Optimistically assume that it has
3393  * completed. */
3394 
3395  static int sbdata[4] = {DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3};
3396  /* TODO: The only purpose of "sbvalue" is to be passed to
3397  * "log_memory_access()". If "log_memory_access()" were to
3398  * accept "uint8_t *" instead of "uint32_t *", "sbvalue" would
3399  * be unnecessary.
3400  */
3401  uint32_t sbvalue[4] = {0};
3402  for (uint32_t i = (next_address - address) / size; i < count - 1; i++) {
3403  const uint32_t size_in_words = DIV_ROUND_UP(size, 4);
3404  struct riscv_batch *batch = riscv_batch_alloc(target, size_in_words);
3405  /* Read of sbdata0 must be performed as last because it
3406  * starts the new bus data transfer
3407  * (in case "sbcs.sbreadondata" was set above).
3408  * We don't want to start the next bus read before we
3409  * fetch all the data from the last bus read. */
3410  for (uint32_t j = size_in_words - 1; j > 0; --j)
3411  riscv_batch_add_dm_read(batch, sbdata[j], RISCV_DELAY_BASE);
3413 
3414  int res = batch_run_timeout(target, batch);
3415  if (res != ERROR_OK) {
3416  riscv_batch_free(batch);
3417  return res;
3418  }
3419 
3420  const size_t last_key = batch->read_keys_used - 1;
3421  for (size_t k = 0; k <= last_key; ++k) {
3422  sbvalue[k] = riscv_batch_get_dmi_read_data(batch, last_key - k);
3423  buf_set_u32(buffer + i * size + k * 4, 0, MIN(32, 8 * size), sbvalue[k]);
3424  }
3425 
3426  riscv_batch_free(batch);
3427  const target_addr_t read_addr = address + i * increment;
3428  log_memory_access(read_addr, sbvalue, size, true);
3429  }
3430 
3431  uint32_t sbcs_read = 0;
3432  if (count > 1) {
3433  /* "Writes to sbcs while sbbusy is high result in undefined behavior.
3434  * A debugger must not write to sbcs until it reads sbbusy as 0." */
3435  if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
3436  return ERROR_FAIL;
3437 
3438  sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, 0);
3439  if (dm_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
3440  return ERROR_FAIL;
3441  }
3442 
3443  /* Read the last word, after we disabled sbreadondata if necessary. */
3444  if (!get_field(sbcs_read, DM_SBCS_SBERROR) &&
3445  !get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
3446  if (read_memory_bus_word(target, address + (count - 1) * increment, size,
3447  buffer + (count - 1) * size) != ERROR_OK)
3448  return ERROR_FAIL;
3449 
3450  if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
3451  return ERROR_FAIL;
3452  }
3453 
3454  if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
3455  /* We read while the target was busy. Slow down and try again.
3456  * Clear sbbusyerror, as well as readondata or readonaddr. */
3458  return ERROR_FAIL;
3459 
3460  if (get_field(sbcs_read, DM_SBCS_SBERROR) == DM_SBCS_SBERROR_NONE) {
3461  /* Read the address whose read was last completed. */
3462  next_address = sb_read_address(target);
3463 
3464  /* Read the value for the last address. It's
3465  * sitting in the register for us, but we read it
3466  * too early (sbbusyerror became set). */
3467  target_addr_t current_address = next_address - (increment ? size : 0);
3468  if (read_memory_bus_word(target, current_address, size,
3469  buffer + current_address - address) != ERROR_OK)
3470  return ERROR_FAIL;
3471  }
3472 
3473  int res = riscv_scan_increase_delay(&info->learned_delays,
3475  if (res != ERROR_OK)
3476  return res;
3477  continue;
3478  }
3479 
3480  unsigned int error = get_field(sbcs_read, DM_SBCS_SBERROR);
3481  if (error == DM_SBCS_SBERROR_NONE) {
3482  next_address = end_address;
3483  } else {
3484  /* Some error indicating the bus access failed, but not because of
3485  * something we did wrong. */
3487  return ERROR_FAIL;
3488  return ERROR_FAIL;
3489  }
3490  }
3491 
3492  return ERROR_OK;
3493 }
3494 
3495 static void log_mem_access_result(struct target *target, bool success,
3496  enum riscv_mem_access_method method, bool is_read)
3497 {
3498  RISCV_INFO(r);
3499  bool warn = false;
3500  char msg[60];
3501 
3502  /* Compose the message */
3503  snprintf(msg, 60, "%s to %s memory via %s.",
3504  success ? "Succeeded" : "Failed",
3505  is_read ? "read" : "write",
3506  (method == RISCV_MEM_ACCESS_PROGBUF) ? "program buffer" :
3507  (method == RISCV_MEM_ACCESS_SYSBUS) ? "system bus" : "abstract access");
3508 
3509  /* Determine the log message severity. Show warnings only once. */
3510  if (!success) {
3511  warn = r->mem_access_warn[method];
3512  r->mem_access_warn[method] = false;
3513  }
3514 
3515  if (warn)
3516  LOG_TARGET_WARNING(target, "%s", msg);
3517  else
3518  LOG_TARGET_DEBUG(target, "%s", msg);
3519 }
3520 
3527 };
3528 
3529 #define LIST_OF_MEM_ACCESS_RESULTS \
3530  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_OK, OK, "ok") \
3531  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_DISABLED, DISABLED, "disabled") \
3532  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED, SKIPPED, "skipped") \
3533  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR, \
3534  SKIPPED, "skipped (abstract access cmderr)") \
3535  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_NOT_PRESENT, \
3536  SKIPPED, "skipped (progbuf not present)") \
3537  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_INSUFFICIENT, \
3538  SKIPPED, "skipped (insufficient progbuf)") \
3539  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE, \
3540  SKIPPED, "skipped (unsupported access size)") \
3541  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_XLEN_TOO_SHORT, \
3542  SKIPPED, "skipped (xlen too short)") \
3543  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TARGET_NOT_HALTED, \
3544  SKIPPED, "skipped (target not halted)") \
3545  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS, \
3546  SKIPPED, "skipped (address too large)") \
3547  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE, \
3548  SKIPPED, "skipped (increment size not supported)") \
3549  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TARGET_SELECT_FAILED, \
3550  SKIPPED, "skipped (dm target select failed)") \
3551  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_FENCE_EXEC_FAILED, \
3552  SKIPPED, "skipped (fence execution failed)") \
3553  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_SYSBUS_ACCESS_FAILED, \
3554  SKIPPED, "skipped (sysbus access failed)") \
3555  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_REG_SAVE_FAILED, \
3556  SKIPPED, "skipped (register save failed)") \
3557  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNKNOWN_SYSBUS_VERSION, \
3558  SKIPPED, "skipped (unknown sysbus version)") \
3559  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGRAM_WRITE_FAILED, \
3560  SKIPPED, "skipped (program write failed)") \
3561  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED, \
3562  SKIPPED, "skipped (progbuf fill failed)") \
3563  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_WRITE_ABSTRACT_ARG_FAILED, \
3564  SKIPPED, "skipped (abstract command argument write failed)") \
3565  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PRIV_MOD_FAILED, \
3566  SKIPPED, "skipped (privilege modification failed)") \
3567  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED, FAILED, "failed") \
3568  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_DM_ACCESS_FAILED, \
3569  FAILED, "failed (DM register access failed)") \
3570  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PRIV_MOD_FAILED, \
3571  FAILED, "failed (privilege modification failed)") \
3572  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_REG_READ_FAILED, \
3573  FAILED, "failed (register read failed)") \
3574  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED, \
3575  FAILED, "failed (progbuf startup failed)") \
3576  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED, \
3577  FAILED, "failed (progbuf inner failed)") \
3578  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_TEARDOWN_FAILED, \
3579  FAILED, "failed (progbuf teardown failed)") \
3580  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_EXECUTE_ABSTRACT_FAILED, \
3581  FAILED, "failed (execute abstract failed)") \
3582  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_NO_FORWARD_PROGRESS, \
3583  FAILED, "failed (no forward progress)") \
3584  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_FENCE_EXEC_FAILED, \
3585  FAILED, "failed (fence execution failed)") \
3586 
3587 
3588 #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) name,
3591 };
3592 #undef MEM_ACCESS_RESULT_HANDLER
3593 
3594 /* Structure is intentionally used to contain the memory access result,
3595  for type safety - to avoid implicit conversions to integers. */
3598 };
3599 
3601 {
3602  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3603  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3604  == MEM_ACCESS_RESULT_TYPE_OK;
3605 
3606  switch (status.value) {
3608  }
3609  #undef MEM_ACCESS_RESULT_HANDLER
3610 
3611  LOG_ERROR("Unknown memory access status: %d", status.value);
3612  assert(false && "Unknown memory access status");
3613  return false;
3614 }
3615 
3617 {
3618  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3619  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3620  == MEM_ACCESS_RESULT_TYPE_FAILED;
3621 
3622  switch (status.value) {
3624  }
3625  #undef MEM_ACCESS_RESULT_HANDLER
3626 
3627  LOG_ERROR("Unknown memory access status: %d", status.value);
3628  assert(false && "Unknown memory access status");
3629  return true;
3630 }
3631 
3633 {
3634  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3635  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3636  == MEM_ACCESS_RESULT_TYPE_SKIPPED;
3637 
3638  switch (status.value) {
3640  }
3641  #undef MEM_ACCESS_RESULT_HANDLER
3642  LOG_ERROR("Unknown memory access status: %d", status.value);
3643  assert(false && "Unknown memory access status");
3644  return true;
3645 }
3646 
3648 {
3649  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3650  [name] = msg,
3651  static const char * const table[] = {
3653  };
3654  #undef MEM_ACCESS_RESULT_HANDLER
3655 
3656  assert(status.value < ARRAY_SIZE(table));
3657  return table[status.value];
3658 }
3659 
3661 {
3662  struct mem_access_result result = {.value = value};
3663  return result;
3664 }
3665 
3667  const struct riscv_mem_access_args args)
3668 {
3669  assert(riscv_mem_access_is_valid(args));
3670  const char *const access_type =
3671  riscv_mem_access_is_read(args) ? "read" : "write";
3672 
3673  if (!has_sufficient_progbuf(target, 1)) {
3674  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf "
3675  "- progbuf not present", access_type);
3676  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_NOT_PRESENT);
3677  }
3678  if (!has_sufficient_progbuf(target, 3)) {
3679  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3680  "insufficient progbuf size.", access_type);
3681  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_INSUFFICIENT);
3682  }
3683  if (target->state != TARGET_HALTED) {
3684  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3685  "target not halted.", access_type);
3686  return mem_access_result(MEM_ACCESS_SKIPPED_TARGET_NOT_HALTED);
3687  }
3688  if (riscv_xlen(target) < args.size * 8) {
3689  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3690  "XLEN (%d) is too short for %d-bit memory args.",
3691  access_type, riscv_xlen(target), args.size * 8);
3692  return mem_access_result(MEM_ACCESS_SKIPPED_XLEN_TOO_SHORT);
3693  }
3694  if (args.size > 8) {
3695  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3696  "unsupported size.", access_type);
3697  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3698  }
3699  if ((sizeof(args.address) * 8 > riscv_xlen(target))
3700  && (args.address >> riscv_xlen(target))) {
3701  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3702  "progbuf only supports %u-bit address.", access_type, riscv_xlen(target));
3703  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3704  }
3705 
3706  return mem_access_result(MEM_ACCESS_OK);
3707 }
3708 
3709 static struct mem_access_result
3710 mem_should_skip_sysbus(struct target *target, const struct riscv_mem_access_args args)
3711 {
3712  assert(riscv_mem_access_is_valid(args));
3713 
3715  const bool is_read = riscv_mem_access_is_read(args);
3716  const char *const access_type = is_read ? "read" : "write";
3717 
3718  if (!sba_supports_access(target, args.size)) {
3719  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3720  "unsupported size.", access_type);
3721  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3722  }
3723  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
3724  if ((sizeof(args.address) * 8 > sbasize)
3725  && (args.address >> sbasize)) {
3726  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3727  "sba only supports %u-bit address.", access_type, sbasize);
3728  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3729  }
3730  if (is_read && args.increment != args.size
3731  && (get_field(info->sbcs, DM_SBCS_SBVERSION) == 0
3732  || args.increment != 0)) {
3733  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3734  "sba %ss only support (size == increment) or also "
3735  "size==0 for sba v1.", access_type, access_type);
3736  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE);
3737  }
3738 
3739  return mem_access_result(MEM_ACCESS_OK);
3740 }
3741 
3742 static struct mem_access_result
3743 mem_should_skip_abstract(struct target *target, const struct riscv_mem_access_args args)
3744 {
3745  assert(riscv_mem_access_is_valid(args));
3746 
3747  const bool is_read = riscv_mem_access_is_read(args);
3748  const char *const access_type = is_read ? "read" : "write";
3749  if (args.size > 8) {
3750  /* TODO: Add 128b support if it's ever used. Involves modifying
3751  read/write_abstract_arg() to work on two 64b values. */
3752  LOG_TARGET_DEBUG(target, "Skipping mem %s via abstract access - "
3753  "unsupported size: %d bits", access_type, args.size * 8);
3754  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3755  }
3756  if ((sizeof(args.address) * 8 > riscv_xlen(target))
3757  && (args.address >> riscv_xlen(target))) {
3758  LOG_TARGET_DEBUG(target, "Skipping mem %s via abstract access - "
3759  "abstract access only supports %u-bit address.",
3760  access_type, riscv_xlen(target));
3761  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3762  }
3763  if (is_read && args.size != args.increment) {
3764  LOG_TARGET_ERROR(target, "Skipping mem %s via abstract access - "
3765  "abstract command %ss only support (size == increment).",
3766  access_type, access_type);
3767  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE);
3768  }
3769  return mem_access_result(MEM_ACCESS_OK);
3770 }
3771 
3772 /*
3773  * Performs a memory read using memory access abstract commands. The read sizes
3774  * supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16 byte
3775  * aamsize fields in the memory access abstract command.
3776  */
3777 static struct mem_access_result
3778 read_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
3779 {
3780  assert(riscv_mem_access_is_read(args));
3781 
3782  memset(args.read_buffer, 0, args.count * args.size);
3783 
3784  /* Convert the size (bytes) to width (bits) */
3785  unsigned int width = args.size << 3;
3786 
3787  uint32_t command = access_memory_command(target, /* virtual = */ false,
3788  width, /* postincrement = */ true, /* is_write = */ false);
3789  bool use_aampostincrement = !is_command_unsupported(target, command);
3790  if (!use_aampostincrement)
3791  /* It is already known that this abstract memory
3792  * access with aampostincrement=1 is not supported.
3793  * So try aampostincrement=0 right away.
3794  *
3795  * TODO: check if new command is supported */
3796  command = access_memory_command(target, /* virtual = */ false,
3797  width, /* postincrement = */ false, /* is_write = */ false);
3798 
3799  /* Execute the reads */
3800  uint8_t *p = args.read_buffer;
3801  int result = ERROR_OK;
3802  bool updateaddr = true;
3803  unsigned int width32 = MAX(width, 32);
3804  for (uint32_t c = 0; c < args.count; c++) {
3805  /* Update the address if it is the first time or aampostincrement is not supported by the target. */
3806  if (updateaddr) {
3807  /* Set arg1 to the address: address + c * size */
3808  result = write_abstract_arg(target, 1, args.address + c * args.size, riscv_xlen(target));
3809  if (result != ERROR_OK) {
3810  LOG_TARGET_ERROR(target, "Failed to write arg1.");
3811  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3812  }
3813  }
3814 
3815  /* Execute the command */
3816  uint32_t cmderr;
3817  result = riscv013_execute_abstract_command(target, command, &cmderr);
3818  if (use_aampostincrement && result != ERROR_OK &&
3819  cmderr == CMDERR_NOT_SUPPORTED) {
3820  LOG_TARGET_DEBUG(target, "Trying the same abstract memory "
3821  "read command, but without aampostincrement");
3822  use_aampostincrement = false;
3823  command = access_memory_command(target, /* virtual = */ false,
3824  width, /* postincrement = */ false, /* is_write = */ false);
3825  result = riscv013_execute_abstract_command(target, command, &cmderr);
3826  }
3827 
3828  /* TODO:
3829  * (1) Only the 1st access can result in a 'skip'
3830  * (2) Analyze cmderr value */
3831  if (result != ERROR_OK)
3832  return mem_access_result(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR);
3833 
3834  /* Copy arg0 to buffer (rounded width up to nearest 32) */
3835  riscv_reg_t value;
3836  result = read_abstract_arg(target, &value, 0, width32);
3837  if (result != ERROR_OK)
3838  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3839  buf_set_u64(p, 0, 8 * args.size, value);
3840 
3841  if (use_aampostincrement)
3842  updateaddr = false;
3843  p += args.size;
3844  }
3845 
3846  return mem_access_result(MEM_ACCESS_OK);
3847 }
3848 
3849 /*
3850  * Performs a memory write using memory access abstract commands. The write
3851  * sizes supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16
3852  * byte aamsize fields in the memory access abstract command.
3853  */
3854 static struct mem_access_result
3855 write_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
3856 {
3857  assert(riscv_mem_access_is_write(args));
3858 
3859  int result = ERROR_OK;
3860 
3861  /* Convert the size (bytes) to width (bits) */
3862  unsigned int width = args.size << 3;
3863 
3864  uint32_t command = access_memory_command(target, /* virtual = */ false,
3865  width, /* postincrement = */ true, /* is_write = */ true);
3866  bool use_aampostincrement = !is_command_unsupported(target, command);
3867  if (!use_aampostincrement)
3868  /* It is already known that this abstract memory
3869  * access with aampostincrement=1 is not supported.
3870  * So try aampostincrement=0 right away.
3871  *
3872  * TODO: check if new command is supported */
3873  command = access_memory_command(target, /* virtual = */ false,
3874  width, /* postincrement = */ false, /* is_write = */ true);
3875 
3876  /* Execute the writes */
3877  const uint8_t *p = args.write_buffer;
3878  bool updateaddr = true;
3879  for (uint32_t c = 0; c < args.count; c++) {
3880  /* Move data to arg0 */
3881  riscv_reg_t value = buf_get_u64(p, 0, 8 * args.size);
3882  result = write_abstract_arg(target, 0, value, riscv_xlen(target));
3883  if (result != ERROR_OK) {
3884  LOG_TARGET_ERROR(target, "Failed to write arg0.");
3885  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3886  }
3887 
3888  /* Update the address if it is the first time or aampostincrement is not supported by the target. */
3889  if (updateaddr) {
3890  /* Set arg1 to the address: address + c * size */
3891  result = write_abstract_arg(target, 1, args.address + c * args.size, riscv_xlen(target));
3892  if (result != ERROR_OK) {
3893  LOG_TARGET_ERROR(target, "Failed to write arg1.");
3894  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3895  }
3896  }
3897 
3898  /* Execute the command */
3899  uint32_t cmderr;
3900  result = riscv013_execute_abstract_command(target, command, &cmderr);
3901  if (use_aampostincrement && result != ERROR_OK &&
3902  cmderr == CMDERR_NOT_SUPPORTED) {
3903  LOG_TARGET_DEBUG(target, "Trying the same abstract memory "
3904  "write command, but without aampostincrement");
3905  use_aampostincrement = false;
3906  command = access_memory_command(target, /* virtual = */ false,
3907  width, /* postincrement = */ false, /* is_write = */ true);
3908  result = riscv013_execute_abstract_command(target, command, &cmderr);
3909  }
3910 
3911  /* TODO:
3912  * (1) Only the 1st access can result in a 'skip'
3913  * (2) Analyze cmderr value */
3914  if (result != ERROR_OK)
3915  return mem_access_result(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR);
3916 
3917  if (use_aampostincrement)
3918  updateaddr = false;
3919  p += args.size;
3920  }
3921 
3922  return mem_access_result(MEM_ACCESS_OK);
3923 }
3924 
3935  target_addr_t address, uint32_t increment, uint32_t index)
3936 {
3937  /* s0 holds the next address to read from.
3938  * s1 holds the next data value read.
3939  * a0 is a counter in case increment is 0.
3940  */
3941  if (register_write_direct(target, GDB_REGNO_S0, address + index * increment)
3942  != ERROR_OK)
3943  return ERROR_FAIL;
3944 
3945  if (/*is_repeated_read*/ increment == 0 &&
3947  return ERROR_FAIL;
3948 
3949  /* AC_ACCESS_REGISTER_POSTEXEC is used to trigger first stage of the
3950  * pipeline (memory -> s1) whenever this command is executed.
3951  */
3952  const uint32_t startup_command = riscv013_access_register_command(target,
3955  uint32_t cmderr;
3956  if (riscv013_execute_abstract_command(target, startup_command, &cmderr) != ERROR_OK)
3957  return ERROR_FAIL;
3958  /* TODO: we need to modify error handling here. */
3959  /* NOTE: in case of timeout cmderr is set to CMDERR_NONE */
3960 
3961  /* First read has just triggered. Result is in s1.
3962  * dm_data registers contain the previous value of s1 (garbage).
3963  */
3966  return ERROR_FAIL;
3967 
3968  /* Read garbage from dm_data0, which triggers another execution of the
3969  * program. Now dm_data contains the first good result (from s1),
3970  * and s1 the next memory value.
3971  */
3973  goto clear_abstractauto_and_fail;
3974 
3975  uint32_t abstractcs;
3976  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
3977  goto clear_abstractauto_and_fail;
3978 
3979  cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
3980  switch (cmderr) {
3981  case CMDERR_NONE:
3982  return ERROR_OK;
3983  case CMDERR_BUSY:
3984  LOG_TARGET_ERROR(target, "Unexpected busy error. This is probably a hardware bug.");
3985  /* fall through */
3986  default:
3987  LOG_TARGET_DEBUG(target, "error when reading memory, cmderr=0x%" PRIx32, cmderr);
3989  goto clear_abstractauto_and_fail;
3990  }
3991 clear_abstractauto_and_fail:
3993  return ERROR_FAIL;
3994 }
3995 
4005  uint32_t start_index, uint32_t *elements_read,
4006  const struct riscv_mem_access_args args)
4007 {
4008  assert(riscv_mem_access_is_read(args));
4009 
4011  if (res != ERROR_OK)
4012  return res;
4014  if (res != ERROR_OK)
4015  return res;
4016 
4018  return ERROR_FAIL;
4019 
4020  /* See how far we got by reading s0/a0 */
4021  uint32_t index_on_target;
4022 
4023  if (/*is_repeated_read*/ args.increment == 0) {
4024  /* s0 is constant, a0 is incremented by one each execution */
4025  riscv_reg_t counter;
4026 
4027  if (register_read_direct(target, &counter, GDB_REGNO_A0) != ERROR_OK)
4028  return ERROR_FAIL;
4029  index_on_target = counter;
4030  } else {
4031  target_addr_t address_on_target;
4032 
4033  if (register_read_direct(target, &address_on_target, GDB_REGNO_S0) != ERROR_OK)
4034  return ERROR_FAIL;
4035  index_on_target = (address_on_target - args.address) /
4036  args.increment;
4037  }
4038 
4039  /* According to the spec, if an abstract command fails, one can't make any
4040  * assumptions about dm_data registers, so all the values in the pipeline
4041  * are clobbered now and need to be reread.
4042  */
4043  const uint32_t min_index_on_target = start_index + 2;
4044  if (index_on_target < min_index_on_target) {
4045  LOG_TARGET_ERROR(target, "Arithmetic does not work correctly on the target");
4046  return ERROR_FAIL;
4047  } else if (index_on_target == min_index_on_target) {
4048  LOG_TARGET_DEBUG(target, "No forward progress");
4049  }
4050  const uint32_t next_index = (index_on_target - 2);
4051  *elements_read = next_index - start_index;
4052  LOG_TARGET_WARNING(target, "Re-reading memory from addresses 0x%"
4053  TARGET_PRIxADDR " and 0x%" TARGET_PRIxADDR ".",
4054  args.address + args.increment * next_index,
4055  args.address + args.increment * (next_index + 1));
4057  args.increment, next_index);
4058 }
4059 
4064  uint32_t start_index, uint32_t next_start_index,
4065  const struct riscv_mem_access_args args)
4066 {
4067  assert(riscv_mem_access_is_read(args));
4068 
4069  LOG_TARGET_DEBUG(target, "DMI_STATUS_BUSY encountered in batch. Memory read [%"
4070  PRIu32 ", %" PRIu32 ")", start_index, next_start_index);
4071  if (start_index == next_start_index)
4072  LOG_TARGET_DEBUG(target, "No forward progress");
4073 
4075  return ERROR_FAIL;
4077  args.increment, next_start_index);
4078 }
4079 
4084  const struct riscv_batch *batch,
4085  uint32_t start_index, uint32_t elements_to_read, uint32_t *elements_read,
4086  const struct riscv_mem_access_args args)
4087 {
4088  assert(riscv_mem_access_is_read(args));
4089 
4090  const bool two_reads_per_element = args.size > 4;
4091  const uint32_t reads_per_element = (two_reads_per_element ? 2 : 1);
4092  assert(!two_reads_per_element || riscv_xlen(target) == 64);
4093  assert(elements_to_read <= UINT32_MAX / reads_per_element);
4094  const uint32_t nreads = elements_to_read * reads_per_element;
4095  for (uint32_t curr_idx = start_index, read = 0; read < nreads; ++read) {
4096  switch (riscv_batch_get_dmi_read_op(batch, read)) {
4097  case DMI_STATUS_BUSY:
4098  *elements_read = curr_idx - start_index;
4099  return read_memory_progbuf_inner_on_dmi_busy(target, start_index, curr_idx
4100  , args);
4101  case DMI_STATUS_FAILED:
4103  "Batch memory read encountered DMI_STATUS_FAILED on read %"
4104  PRIu32, read);
4105  return ERROR_FAIL;
4106  case DMI_STATUS_SUCCESS:
4107  break;
4108  default:
4109  assert(0);
4110  }
4111  const uint32_t value = riscv_batch_get_dmi_read_data(batch, read);
4112  uint8_t * const curr_buff = args.read_buffer +
4113  curr_idx * args.size;
4114  const target_addr_t curr_addr = args.address +
4115  curr_idx * args.increment;
4116  const uint32_t size = args.size;
4117 
4118  assert(size <= 8);
4119  const bool is_odd_read = read % 2;
4120 
4121  if (two_reads_per_element && !is_odd_read) {
4122  buf_set_u32(curr_buff + 4, 0, (size * 8) - 32, value);
4123  continue;
4124  }
4125  const bool is_second_read = two_reads_per_element;
4126 
4127  buf_set_u32(curr_buff, 0, is_second_read ? 32 : (size * 8), value);
4128  log_memory_access64(curr_addr, buf_get_u64(curr_buff, 0, size * 8),
4129  size, /*is_read*/ true);
4130  ++curr_idx;
4131  }
4132  *elements_read = elements_to_read;
4133  return ERROR_OK;
4134 }
4135 
4143  struct riscv_batch *batch, const struct riscv_mem_access_args args,
4144  uint32_t start_index, uint32_t elements_to_read, uint32_t *elements_read)
4145 {
4146  assert(riscv_mem_access_is_read(args));
4147 
4148  dm013_info_t *dm = get_dm(target);
4149  if (!dm)
4150  return ERROR_FAIL;
4151 
4152  /* Abstract commands are executed while running the batch. */
4153  dm->abstract_cmd_maybe_busy = true;
4154  if (batch_run(target, batch) != ERROR_OK)
4155  return ERROR_FAIL;
4156 
4157  uint32_t abstractcs;
4158  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
4159  return ERROR_FAIL;
4160 
4161  uint32_t elements_to_extract_from_batch;
4162 
4163  uint32_t cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
4164  switch (cmderr) {
4165  case CMDERR_NONE:
4166  LOG_TARGET_DEBUG(target, "successful (partial?) memory read [%"
4167  PRIu32 ", %" PRIu32 ")", start_index, start_index + elements_to_read);
4168  elements_to_extract_from_batch = elements_to_read;
4169  break;
4170  case CMDERR_BUSY:
4171  LOG_TARGET_DEBUG(target, "memory read resulted in busy response");
4173  &elements_to_extract_from_batch, args)
4174  != ERROR_OK)
4175  return ERROR_FAIL;
4176  break;
4177  default:
4178  LOG_TARGET_DEBUG(target, "error when reading memory, cmderr=0x%" PRIx32, cmderr);
4180  return ERROR_FAIL;
4181  }
4182 
4183  if (read_memory_progbuf_inner_extract_batch_data(target, batch, start_index,
4184  elements_to_extract_from_batch, elements_read, args) != ERROR_OK)
4185  return ERROR_FAIL;
4186 
4187  return ERROR_OK;
4188 }
4189 
4191  uint32_t count, uint32_t size)
4192 {
4193  assert(size <= 8);
4194  const uint32_t two_regs_used[] = {DM_DATA1, DM_DATA0};
4195  const uint32_t one_reg_used[] = {DM_DATA0};
4196  const uint32_t reads_per_element = size > 4 ? 2 : 1;
4197  const uint32_t * const used_regs = size > 4 ? two_regs_used : one_reg_used;
4198  const uint32_t batch_capacity = riscv_batch_available_scans(batch) / reads_per_element;
4199  const uint32_t end = MIN(batch_capacity, count);
4200 
4201  for (uint32_t j = 0; j < end; ++j) {
4202  /* TODO: reuse "abstract_data_read_fill_batch()" here.
4203  * TODO: Only the read of "DM_DATA0" starts an abstract
4204  * command, so the other read can use "RISCV_DELAY_BASE"
4205  */
4206  for (uint32_t i = 0; i < reads_per_element; ++i)
4207  riscv_batch_add_dm_read(batch, used_regs[i],
4209  }
4210  return end;
4211 }
4212 
4214  const struct riscv_mem_access_args args, uint32_t *elements_read,
4215  uint32_t index, uint32_t loop_count)
4216 {
4217  assert(riscv_mem_access_is_read(args));
4218 
4220  if (!batch)
4221  return ERROR_FAIL;
4222 
4223  const uint32_t elements_to_read = read_memory_progbuf_inner_fill_batch(batch,
4224  loop_count - index, args.size);
4225 
4227  args, index, elements_to_read, elements_read);
4228  riscv_batch_free(batch);
4229  return result;
4230 }
4231 
4237  const struct riscv_mem_access_args args, uint32_t start_index)
4238 {
4239  assert(riscv_mem_access_is_read(args));
4240 
4242  "Executing one loop iteration to ensure forward progress (index=%"
4243  PRIu32 ")", start_index);
4244  const target_addr_t curr_target_address = args.address +
4245  start_index * args.increment;
4246  uint8_t * const curr_buffer_address = args.read_buffer +
4247  start_index * args.size;
4248  const struct riscv_mem_access_args curr_access = {
4249  .read_buffer = curr_buffer_address,
4250  .address = curr_target_address,
4251  .size = args.size,
4252  .increment = args.increment,
4253  };
4254  uint32_t elements_read;
4255  if (read_memory_progbuf_inner_try_to_read(target, curr_access, &elements_read,
4256  /*index*/ 0, /*loop_count*/ 1) != ERROR_OK)
4257  return ERROR_FAIL;
4258 
4259  if (elements_read != 1) {
4260  assert(elements_read == 0);
4261  LOG_TARGET_DEBUG(target, "Can not ensure forward progress");
4262  /* FIXME: Here it would be better to retry the read and fail only if the
4263  * delay is greater then some threshold.
4264  */
4265  return ERROR_FAIL;
4266  }
4267  return ERROR_OK;
4268 }
4269 
4270 static void set_buffer_and_log_read(const struct riscv_mem_access_args args,
4271  uint32_t index, uint64_t value)
4272 {
4273  assert(riscv_mem_access_is_read(args));
4274 
4275  uint8_t * const buffer = args.read_buffer;
4276  const uint32_t size = args.size;
4277  const uint32_t increment = args.increment;
4278  const target_addr_t address = args.address;
4279 
4280  assert(size <= 8);
4281  buf_set_u64(buffer + index * size, 0, 8 * size, value);
4282  log_memory_access64(address + index * increment, value, size,
4283  /*is_read*/ true);
4284 }
4285 
4287  const struct riscv_mem_access_args args, uint32_t index)
4288 {
4289  assert(args.size <= 8);
4290  uint64_t value;
4291  int result = read_abstract_arg(target, &value, /*index*/ 0,
4292  args.size > 4 ? 64 : 32);
4293  if (result == ERROR_OK)
4294  set_buffer_and_log_read(args, index, value);
4295  return result;
4296 }
4297 
4298 static struct mem_access_result read_word_from_s1(struct target *target,
4299  const struct riscv_mem_access_args args, uint32_t index)
4300 {
4301  assert(riscv_mem_access_is_read(args));
4302 
4303  uint64_t value;
4304 
4306  return mem_access_result(MEM_ACCESS_FAILED_REG_READ_FAILED);
4307  set_buffer_and_log_read(args, index, value);
4308  return mem_access_result(MEM_ACCESS_OK);
4309 }
4310 
4312  uint32_t increment, uint32_t size)
4313 {
4314  const bool is_repeated_read = increment == 0;
4315 
4317  return ERROR_FAIL;
4319  return ERROR_FAIL;
4320  if (is_repeated_read && riscv013_reg_save(target, GDB_REGNO_A0) != ERROR_OK)
4321  return ERROR_FAIL;
4322 
4323  struct riscv_program program;
4324 
4325  riscv_program_init(&program, target);
4326  if (riscv_program_load(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0, size) != ERROR_OK)
4327  return ERROR_FAIL;
4328  if (is_repeated_read) {
4329  if (riscv_program_addi(&program, GDB_REGNO_A0, GDB_REGNO_A0, 1)
4330  != ERROR_OK)
4331  return ERROR_FAIL;
4332  } else {
4334  increment)
4335  != ERROR_OK)
4336  return ERROR_FAIL;
4337  }
4338  if (riscv_program_ebreak(&program) != ERROR_OK)
4339  return ERROR_FAIL;
4340  if (riscv_program_write(&program) != ERROR_OK)
4341  return ERROR_FAIL;
4342 
4343  return ERROR_OK;
4344 }
4345 
4351 static struct mem_access_result
4353 {
4354  assert(riscv_mem_access_is_read(args));
4355  assert(args.count > 1 && "If count == 1, read_memory_progbuf_inner_one must be called");
4356 
4358  args.increment, args.size) != ERROR_OK)
4359  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
4360 
4361  if (read_memory_progbuf_inner_startup(target, args.address,
4362  args.increment, /*index*/ 0) != ERROR_OK)
4363  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED);
4364  /* The program in program buffer is executed twice during
4365  * read_memory_progbuf_inner_startup().
4366  * Here:
4367  * dm_data[0:1] == M[address]
4368  * s1 == M[address + increment]
4369  * s0 == address + increment * 2
4370  * `count - 2` program executions are performed in this loop.
4371  * No need to execute the program any more, since S1 will already contain
4372  * M[address + increment * (count - 1)] and we can read it directly.
4373  */
4374  const uint32_t loop_count = args.count - 2;
4375 
4376  for (uint32_t index = 0; index < loop_count;) {
4377  uint32_t elements_read;
4378  if (read_memory_progbuf_inner_try_to_read(target, args, &elements_read,
4379  index, loop_count) != ERROR_OK) {
4381  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED);
4382  }
4383  if (elements_read == 0) {
4385  index) != ERROR_OK) {
4387  return mem_access_result(MEM_ACCESS_FAILED_NO_FORWARD_PROGRESS);
4388  }
4389  elements_read = 1;
4390  }
4391  index += elements_read;
4392  assert(index <= loop_count);
4393  }
4395  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
4396 
4397  /* Read the penultimate word. */
4399  args, args.count - 2) != ERROR_OK)
4400  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
4401  /* Read the last word. */
4402  return read_word_from_s1(target, args, args.count - 1);
4403 }
4404 
4409 static struct mem_access_result
4411 {
4412  assert(riscv_mem_access_is_read(args));
4413 
4415  return mem_access_result(MEM_ACCESS_SKIPPED_REG_SAVE_FAILED);
4416 
4417  struct riscv_program program;
4418 
4419  riscv_program_init(&program, target);
4421  /* offset = */ 0, args.size) != ERROR_OK
4422  || riscv_program_ebreak(&program) != ERROR_OK)
4423  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
4424 
4425  if (riscv_program_write(&program) != ERROR_OK)
4426  return mem_access_result(MEM_ACCESS_SKIPPED_PROGRAM_WRITE_FAILED);
4427 
4428  /* Write address to S1, and execute buffer. */
4429  if (write_abstract_arg(target, /* index = */ 0,
4430  args.address, riscv_xlen(target)) != ERROR_OK)
4431  return mem_access_result(MEM_ACCESS_SKIPPED_WRITE_ABSTRACT_ARG_FAILED);
4435  uint32_t cmderr;
4437  return mem_access_result(MEM_ACCESS_FAILED_EXECUTE_ABSTRACT_FAILED);
4438 
4439  return read_word_from_s1(target, args, 0);
4440 }
4441 
4445 static struct mem_access_result
4446 read_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
4447 {
4448  assert(riscv_mem_access_is_read(args));
4449 
4450  select_dmi(target->tap);
4451  memset(args.read_buffer, 0, args.count * args.size);
4452 
4454  return mem_access_result(MEM_ACCESS_SKIPPED_FENCE_EXEC_FAILED);
4455 
4456  return (args.count == 1) ?
4459 }
4460 
4461 static struct mem_access_result
4462 write_memory_progbuf(struct target *target, const struct riscv_mem_access_args args);
4463 
4464 static struct mem_access_result
4465 access_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
4466 {
4467  struct mem_access_result skip_reason = mem_should_skip_progbuf(target, args);
4468  if (!is_mem_access_ok(skip_reason))
4469  return skip_reason;
4470 
4471  const bool is_read = riscv_mem_access_is_read(args);
4472  const char *const access_type = is_read ? "reading" : "writing";
4473  LOG_TARGET_DEBUG(target, "%s %" PRIu32 " words of %" PRIu32
4474  " bytes at 0x%" TARGET_PRIxADDR, access_type, args.count,
4475  args.size, args.address);
4476 
4478  return mem_access_result(MEM_ACCESS_SKIPPED_TARGET_SELECT_FAILED);
4479 
4480  riscv_reg_t mstatus = 0;
4481  riscv_reg_t mstatus_old = 0;
4482  riscv_reg_t dcsr = 0;
4483  riscv_reg_t dcsr_old = 0;
4485  &mstatus, &mstatus_old, &dcsr, &dcsr_old) != ERROR_OK)
4486  return mem_access_result(MEM_ACCESS_SKIPPED_PRIV_MOD_FAILED);
4487 
4488  struct mem_access_result result = is_read ?
4489  read_memory_progbuf(target, args) :
4491 
4493  mstatus, mstatus_old, dcsr, dcsr_old) != ERROR_OK)
4494  return mem_access_result(MEM_ACCESS_FAILED_PRIV_MOD_FAILED);
4495 
4496  return result;
4497 }
4498 
4499 static int
4500 write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args);
4501 static int
4503 
4504 static struct mem_access_result
4505 access_memory_sysbus(struct target *target, const struct riscv_mem_access_args args)
4506 {
4507  assert(riscv_mem_access_is_valid(args));
4508 
4509  struct mem_access_result skip_reason = mem_should_skip_sysbus(target, args);
4510  if (!is_mem_access_ok(skip_reason))
4511  return skip_reason;
4512 
4514  int ret = ERROR_FAIL;
4515  const bool is_read = riscv_mem_access_is_read(args);
4516  const uint64_t sbver = get_field(info->sbcs, DM_SBCS_SBVERSION);
4517  if (sbver == 0) {
4518  ret = is_read ? read_memory_bus_v0(target, args) :
4519  write_memory_bus_v0(target, args);
4520  } else if (sbver == 1) {
4521  ret = is_read ? read_memory_bus_v1(target, args) :
4522  write_memory_bus_v1(target, args);
4523  } else {
4524  LOG_TARGET_ERROR(target, "Unknown system bus version: %" PRIu64, sbver);
4525  return mem_access_result(MEM_ACCESS_SKIPPED_UNKNOWN_SYSBUS_VERSION);
4526  }
4527 
4528  return mem_access_result(ret == ERROR_OK ?
4529  MEM_ACCESS_OK : MEM_ACCESS_SKIPPED_SYSBUS_ACCESS_FAILED);
4530 }
4531 
4532 static struct mem_access_result
4533 access_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
4534 {
4535  assert(riscv_mem_access_is_valid(args));
4536 
4537  struct mem_access_result skip_reason = mem_should_skip_abstract(target, args);
4538  if (!is_mem_access_ok(skip_reason))
4539  return skip_reason;
4540 
4541  const bool is_read = riscv_mem_access_is_read(args);
4542  const char *const access_type = is_read ? "reading" : "writing";
4543  LOG_TARGET_DEBUG(target, "%s %d words of %d bytes at 0x%"
4544  TARGET_PRIxADDR, access_type, args.count,
4545  args.size, args.address);
4546 
4547  return is_read ? read_memory_abstract(target, args) :
4549 }
4550 
4551 static int
4553 {
4554  assert(riscv_mem_access_is_valid(args));
4555 
4556  const bool is_read = riscv_mem_access_is_read(args);
4557  const char *const access_type = is_read ? "read" : "write";
4558  if (!is_read && args.increment != args.size) {
4559  LOG_TARGET_ERROR(target, "Write increment size has to be equal to element size");
4560  return ERROR_NOT_IMPLEMENTED;
4561  }
4562 
4563  if (!IS_PWR_OF_2(args.size) || args.size < 1 || args.size > 16) {
4564  LOG_TARGET_ERROR(target, "BUG: Unsupported size for "
4565  "memory %s: %d", access_type, args.size);
4566  return ERROR_FAIL;
4567  }
4568 
4569  struct mem_access_result skip_reason[] = {
4570  [RISCV_MEM_ACCESS_PROGBUF] = mem_access_result(MEM_ACCESS_DISABLED),
4571  [RISCV_MEM_ACCESS_SYSBUS] = mem_access_result(MEM_ACCESS_DISABLED),
4572  [RISCV_MEM_ACCESS_ABSTRACT] = mem_access_result(MEM_ACCESS_DISABLED),
4573  };
4574 
4575  RISCV_INFO(r);
4576  for (unsigned int i = 0; i < r->num_enabled_mem_access_methods; ++i) {
4577  enum riscv_mem_access_method method = r->mem_access_methods[i];
4578  switch (method) {
4580  skip_reason[method] = access_memory_progbuf(target, args);
4581  break;
4583  skip_reason[method] = access_memory_sysbus(target, args);
4584  break;
4586  skip_reason[method] = access_memory_abstract(target, args);
4587  break;
4588  default:
4589  LOG_TARGET_ERROR(target, "Unknown memory access method: %d", method);
4590  assert(false && "Unknown memory access method");
4591  goto failure;
4592  }
4593 
4594  if (is_mem_access_failed(skip_reason[method]))
4595  goto failure;
4596 
4597  const bool success = is_mem_access_ok(skip_reason[method]);
4598  log_mem_access_result(target, success, method, is_read);
4599  if (success)
4600  return ERROR_OK;
4601  }
4602 
4603 failure:
4604  LOG_TARGET_ERROR(target, "Failed to %s memory (addr=0x%" PRIx64 ")\n"
4605  " progbuf=%s, sysbus=%s, abstract=%s", access_type, args.address,
4609  return ERROR_FAIL;
4610 }
4611 
4612 static int write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
4613 {
4614  assert(riscv_mem_access_is_write(args));
4615 
4616  /*1) write sbaddress: for singlewrite and autoincrement, we need to write the address once*/
4617  LOG_TARGET_DEBUG(target, "System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
4618  TARGET_PRIxADDR, args.size, args.count, args.address);
4620  int64_t value = 0;
4621  int64_t access = 0;
4622  riscv_addr_t offset = 0;
4623  riscv_addr_t t_addr = 0;
4624  const uint8_t *t_buffer = args.write_buffer + offset;
4625 
4626  /* B.8 Writing Memory, single write check if we write in one go */
4627  if (args.count == 1) { /* count is in bytes here */
4628  value = buf_get_u64(t_buffer, 0, 8 * args.size);
4629 
4630  access = 0;
4631  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
4632  dm_write(target, DM_SBCS, access);
4633  LOG_TARGET_DEBUG(target, " access: 0x%08" PRIx64, access);
4634  LOG_TARGET_DEBUG(target, " write_memory:SAB: ONE OFF: value 0x%08" PRIx64, value);
4636  return ERROR_OK;
4637  }
4638 
4639  /*B.8 Writing Memory, using autoincrement*/
4640 
4641  access = 0;
4642  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
4643  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
4644  LOG_TARGET_DEBUG(target, " access: 0x%08" PRIx64, access);
4645  dm_write(target, DM_SBCS, access);
4646 
4647  /*2)set the value according to the size required and write*/
4648  for (riscv_addr_t i = 0; i < args.count; ++i) {
4649  offset = args.size * i;
4650  /* for monitoring only */
4651  t_addr = args.address + offset;
4652  t_buffer = args.write_buffer + offset;
4653 
4654  value = buf_get_u64(t_buffer, 0, 8 * args.size);
4655  LOG_TARGET_DEBUG(target, "SAB:autoincrement: expected address: 0x%08x value: 0x%08x"
4656  PRIx64, (uint32_t)t_addr, (uint32_t)value);
4658  }
4659  /*reset the autoincrement when finished (something weird is happening if this is not done at the end*/
4660  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 0);
4661  dm_write(target, DM_SBCS, access);
4662 
4663  return ERROR_OK;
4664 }
4665 
4666 static int write_memory_bus_v1(struct target *target, const struct riscv_mem_access_args args)
4667 {
4668  assert(riscv_mem_access_is_write(args));
4669 
4671  uint32_t sbcs = sb_sbaccess(args.size);
4672  sbcs = set_field(sbcs, DM_SBCS_SBAUTOINCREMENT, 1);
4673  dm_write(target, DM_SBCS, sbcs);
4674 
4675  target_addr_t next_address = args.address;
4676  target_addr_t end_address = args.address + args.count * args.size;
4677 
4678  int result = sb_write_address(target, next_address, RISCV_DELAY_BASE);
4679  if (result != ERROR_OK)
4680  return result;
4681 
4682  while (next_address < end_address) {
4683  LOG_TARGET_DEBUG(target, "Transferring burst starting at address 0x%" TARGET_PRIxADDR,
4684  next_address);
4685 
4687  if (!batch)
4688  return ERROR_FAIL;
4689 
4690  for (uint32_t i = (next_address - args.address) / args.size; i < args.count; i++) {
4691  const uint8_t *p = args.write_buffer + i * args.size;
4692 
4693  if (riscv_batch_available_scans(batch) < (args.size + 3) / 4)
4694  break;
4695 
4696  uint32_t sbvalue[4] = { 0 };
4697  if (args.size > 12) {
4698  sbvalue[3] = buf_get_u32(&p[12],
4699  /* first = */ 0, /* bit_num = */ 32);
4700  riscv_batch_add_dm_write(batch, DM_SBDATA3, sbvalue[3], false,
4702  }
4703 
4704  if (args.size > 8) {
4705  sbvalue[2] = buf_get_u32(&p[8],
4706  /* first = */ 0, /* bit_num = */ 32);
4707  riscv_batch_add_dm_write(batch, DM_SBDATA2, sbvalue[2], false,
4709  }
4710  if (args.size > 4) {
4711  sbvalue[1] = buf_get_u32(&p[4],
4712  /* first = */ 0, /* bit_num = */ 32);
4713  riscv_batch_add_dm_write(batch, DM_SBDATA1, sbvalue[1], false,
4715  }
4716 
4717  sbvalue[0] = p[0];
4718  if (args.size > 2) {
4719  sbvalue[0] |= ((uint32_t)p[2]) << 16;
4720  sbvalue[0] |= ((uint32_t)p[3]) << 24;
4721  }
4722  if (args.size > 1)
4723  sbvalue[0] |= ((uint32_t)p[1]) << 8;
4724 
4725  riscv_batch_add_dm_write(batch, DM_SBDATA0, sbvalue[0], false,
4727 
4728  log_memory_access(args.address + i * args.size, sbvalue, args.size, false);
4729 
4730  next_address += args.size;
4731  }
4732 
4733  /* Execute the batch of writes */
4734  result = batch_run(target, batch);
4735  if (result != ERROR_OK) {
4736  riscv_batch_free(batch);
4737  return result;
4738  }
4739 
4740  bool dmi_busy_encountered = riscv_batch_was_batch_busy(batch);
4741  riscv_batch_free(batch);
4742  if (dmi_busy_encountered)
4743  LOG_TARGET_DEBUG(target, "DMI busy encountered during system bus write.");
4744 
4745  result = read_sbcs_nonbusy(target, &sbcs);
4746  if (result != ERROR_OK)
4747  return result;
4748 
4749  if (get_field(sbcs, DM_SBCS_SBBUSYERROR)) {
4750  /* We wrote while the target was busy. */
4751  LOG_TARGET_DEBUG(target, "Sbbusyerror encountered during system bus write.");
4752  /* Clear the sticky error flag. */
4754  /* Slow down before trying again.
4755  * FIXME: Possible overflow is ignored here.
4756  */
4757  riscv_scan_increase_delay(&info->learned_delays,
4759  }
4760 
4761  if (get_field(sbcs, DM_SBCS_SBBUSYERROR) || dmi_busy_encountered) {
4762  /* Recover from the case when the write commands were issued too fast.
4763  * Determine the address from which to resume writing. */
4764  next_address = sb_read_address(target);
4765  if (next_address < args.address) {
4766  /* This should never happen, probably buggy hardware. */
4767  LOG_TARGET_DEBUG(target, "unexpected sbaddress=0x%" TARGET_PRIxADDR
4768  " - buggy sbautoincrement in hw?", next_address);
4769  /* Fail the whole operation. */
4770  return ERROR_FAIL;
4771  }
4772  /* Try again - resume writing. */
4773  continue;
4774  }
4775 
4776  unsigned int sberror = get_field(sbcs, DM_SBCS_SBERROR);
4777  if (sberror != 0) {
4778  /* Sberror indicates the bus access failed, but not because we issued the writes
4779  * too fast. Cannot recover. Sbaddress holds the address where the error occurred
4780  * (unless sbautoincrement in the HW is buggy).
4781  */
4782  target_addr_t sbaddress = sb_read_address(target);
4783  LOG_TARGET_DEBUG(target, "System bus access failed with sberror=%u (sbaddress=0x%" TARGET_PRIxADDR ")",
4784  sberror, sbaddress);
4785  if (sbaddress < args.address) {
4786  /* This should never happen, probably buggy hardware.
4787  * Make a note to the user not to trust the sbaddress value. */
4788  LOG_TARGET_DEBUG(target, "unexpected sbaddress=0x%" TARGET_PRIxADDR
4789  " - buggy sbautoincrement in hw?", next_address);
4790  }
4791  /* Clear the sticky error flag */
4793  /* Fail the whole operation */
4794  return ERROR_FAIL;
4795  }
4796  }
4797 
4798  return ERROR_OK;
4799 }
4800 
4813  const uint8_t *buffer, uint32_t size)
4814 {
4815  /* TODO: There is potential to gain some performance if the operations below are
4816  * executed inside the first DMI batch (not separately). */
4817  if (register_write_direct(target, GDB_REGNO_S0, *address_p) != ERROR_OK)
4818  return ERROR_FAIL;
4819 
4820  /* Write the first item to data0 [, data1] */
4821  assert(size <= 8);
4822  const uint64_t value = buf_get_u64(buffer, 0, 8 * size);
4823  if (write_abstract_arg(target, /*index*/ 0, value, size > 4 ? 64 : 32)
4824  != ERROR_OK)
4825  return ERROR_FAIL;
4826 
4827  /* Write and execute command that moves the value from data0 [, data1]
4828  * into S1 and executes program buffer. */
4834 
4835  uint32_t cmderr;
4837  return ERROR_FAIL;
4838 
4839  log_memory_access64(*address_p, value, size, /*is_read*/ false);
4840 
4841  /* The execution of the command succeeded, which means:
4842  * - write of the first item to memory succeeded
4843  * - address on the target (S0) was incremented
4844  */
4845  *address_p += size;
4846 
4847  /* TODO: Setting abstractauto.autoexecdata is not necessary for a write
4848  * of one element. */
4851 }
4852 
4857 {
4858  return dm_write(target, DM_ABSTRACTAUTO, 0);
4859 }
4860 
4867  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
4868  const uint8_t *buffer)
4869 {
4871  if (res != ERROR_OK)
4872  return res;
4874  if (res != ERROR_OK)
4875  return res;
4876 
4878  return ERROR_FAIL;
4879 
4880  target_addr_t address_on_target;
4881  if (register_read_direct(target, &address_on_target, GDB_REGNO_S0) != ERROR_OK)
4882  return ERROR_FAIL;
4883  const uint8_t * const curr_buff = buffer + (address_on_target - *address_p);
4884  *address_p = address_on_target;
4885  if (*address_p == end_address) {
4886  LOG_TARGET_DEBUG(target, "Got busy while reading after reading the last element");
4887  return ERROR_OK;
4888  }
4889  LOG_TARGET_DEBUG(target, "Restarting from 0x%" TARGET_PRIxADDR, *address_p);
4890  /* This restores the pipeline and ensures one item gets reliably written */
4891  return write_memory_progbuf_startup(target, address_p, curr_buff, size);
4892 }
4893 
4899  target_addr_t start_address, target_addr_t end_address, uint32_t size,
4900  const uint8_t *buffer)
4901 {
4902  assert(size <= 8);
4903  const unsigned int writes_per_element = size > 4 ? 2 : 1;
4904  const size_t batch_capacity = riscv_batch_available_scans(batch) / writes_per_element;
4905  /* This is safe even for the edge case when writing at the very top of
4906  * the 64-bit address space (in which case end_address overflows to 0).
4907  */
4908  const target_addr_t batch_end_address = start_address +
4909  MIN((target_addr_t)batch_capacity * size,
4910  end_address - start_address);
4911  for (target_addr_t address = start_address; address != batch_end_address;
4912  address += size, buffer += size) {
4913  assert(size <= 8);
4914  const uint64_t value = buf_get_u64(buffer, 0, 8 * size);
4915  log_memory_access64(address, value, size, /*is_read*/ false);
4916  if (writes_per_element == 2)
4918  (uint32_t)(value >> 32), false, RISCV_DELAY_BASE);
4919  riscv_batch_add_dm_write(batch, DM_DATA0, (uint32_t)value, false,
4921  }
4922  return batch_end_address;
4923 }
4924 
4929 static int write_memory_progbuf_run_batch(struct target *target, struct riscv_batch *batch,
4930  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
4931  const uint8_t *buffer)
4932 {
4933  dm013_info_t *dm = get_dm(target);
4934  if (!dm)
4935  return ERROR_FAIL;
4936 
4937  /* Abstract commands are executed while running the batch. */
4938  dm->abstract_cmd_maybe_busy = true;
4939  if (batch_run(target, batch) != ERROR_OK)
4940  return ERROR_FAIL;
4941 
4942  /* Note that if the scan resulted in a Busy DMI response, it
4943  * is this call to wait_for_idle() that will cause the dmi_busy_delay
4944  * to be incremented if necessary. */
4945  uint32_t abstractcs;
4946 
4947  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
4948  return ERROR_FAIL;
4949 
4950  uint32_t cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
4951  const bool dmi_busy_encountered = riscv_batch_was_batch_busy(batch);
4952  if (cmderr == CMDERR_NONE && !dmi_busy_encountered) {
4953  LOG_TARGET_DEBUG(target, "Successfully written memory block M[0x%" TARGET_PRIxADDR
4954  ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
4955  *address_p = end_address;
4956  return ERROR_OK;
4957  } else if (cmderr == CMDERR_BUSY || dmi_busy_encountered) {
4958  if (cmderr == CMDERR_BUSY)
4959  LOG_TARGET_DEBUG(target, "Encountered abstract command busy response while writing block M[0x%"
4960  TARGET_PRIxADDR ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
4961  if (dmi_busy_encountered)
4962  LOG_TARGET_DEBUG(target, "Encountered DMI busy response while writing block M[0x%"
4963  TARGET_PRIxADDR ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
4964  /* TODO: If dmi busy is encountered, the address of the last
4965  * successful write can be deduced by analysing the batch.
4966  */
4967  return write_memory_progbuf_handle_busy(target, address_p, end_address,
4968  size, buffer);
4969  }
4970  LOG_TARGET_ERROR(target, "Error when writing memory, abstractcs=0x%" PRIx32,
4971  abstractcs);
4973  return ERROR_FAIL;
4974 }
4975 
4977  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
4978  const uint8_t *buffer)
4979 {
4981  if (!batch)
4982  return ERROR_FAIL;
4983 
4984  const target_addr_t batch_end_addr = write_memory_progbuf_fill_batch(batch,
4985  *address_p, end_address, size, buffer);
4986 
4987  int result = write_memory_progbuf_run_batch(target, batch, address_p,
4988  batch_end_addr, size, buffer);
4989  riscv_batch_free(batch);
4990  return result;
4991 }
4992 
4994 {
4996  return ERROR_FAIL;
4998  return ERROR_FAIL;
4999 
5000  struct riscv_program program;
5001 
5002  riscv_program_init(&program, target);
5004  return ERROR_FAIL;
5005 
5006  if (riscv_program_addi(&program, GDB_REGNO_S0, GDB_REGNO_S0, (int16_t)size) != ERROR_OK)
5007  return ERROR_FAIL;
5008 
5009  if (riscv_program_ebreak(&program) != ERROR_OK)
5010  return ERROR_FAIL;
5011 
5012  return riscv_program_write(&program);
5013 }
5014 
5015 static struct mem_access_result
5017  const struct riscv_mem_access_args args)
5018 {
5019  assert(riscv_mem_access_is_write(args));
5020 
5022  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
5023 
5024  target_addr_t addr_on_target = args.address;
5025  if (write_memory_progbuf_startup(target, &addr_on_target,
5026  args.write_buffer, args.size) != ERROR_OK)
5027  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED);
5028 
5029  const target_addr_t end_addr = args.address + (target_addr_t)args.size * args.count;
5030 
5031  for (target_addr_t next_addr_on_target = addr_on_target; addr_on_target != end_addr;
5032  addr_on_target = next_addr_on_target) {
5033  const uint8_t * const curr_buff = args.write_buffer + (addr_on_target - args.address);
5034  if (write_memory_progbuf_try_to_write(target, &next_addr_on_target,
5035  end_addr, args.size, curr_buff) != ERROR_OK) {
5037  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED);
5038  }
5039  /* write_memory_progbuf_try_to_write() ensures that at least one item
5040  * gets successfully written even when busy condition is encountered.
5041  * These assertions shuld hold when next_address_on_target overflows. */
5042  assert(next_addr_on_target - addr_on_target > 0);
5043  assert(next_addr_on_target - args.address <= (target_addr_t)args.size * args.count);
5044  }
5045 
5047  mem_access_result(MEM_ACCESS_OK) :
5048  mem_access_result(MEM_ACCESS_FAILED_PROGBUF_TEARDOWN_FAILED);
5049 }
5050 
5051 static struct mem_access_result
5052 write_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
5053 {
5054  assert(riscv_mem_access_is_write(args));
5055 
5056  struct mem_access_result result = write_memory_progbuf_inner(target, args);
5057 
5059  return mem_access_result(MEM_ACCESS_FAILED_FENCE_EXEC_FAILED);
5060 
5061  return result;
5062 }
5063 
5064 static bool riscv013_get_impebreak(const struct target *target)
5065 {
5066  RISCV013_INFO(r);
5067  return r->impebreak;
5068 }
5069 
5070 static unsigned int riscv013_get_progbufsize(const struct target *target)
5071 {
5072  RISCV013_INFO(r);
5073  return r->progbufsize;
5074 }
5075 
5076 
5077 struct target_type riscv013_target = {
5078  .name = "riscv",
5079 
5080  .init_target = init_target,
5081  .deinit_target = deinit_target,
5082  .examine = examine,
5083 
5084  .poll = &riscv_openocd_poll,
5085  .halt = &riscv_halt,
5086  .step = &riscv_openocd_step,
5087 
5088  .assert_reset = assert_reset,
5089  .deassert_reset = deassert_reset,
5090 };
5091 
5092 /*** 0.13-specific implementations of various RISC-V helper functions. ***/
5094  riscv_reg_t *value, enum gdb_regno rid)
5095 {
5096  /* It would be beneficial to move this redirection to the
5097  * version-independent section, but there is a conflict:
5098  * `dcsr[5]` is `dcsr.v` in current spec, but it is `dcsr.debugint` in 0.11.
5099  */
5100  if (rid == GDB_REGNO_PRIV) {
5101  uint64_t dcsr;
5102  if (riscv_reg_get(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
5103  return ERROR_FAIL;
5104  *value = set_field(0, VIRT_PRIV_V, get_field(dcsr, CSR_DCSR_V));
5105  *value = set_field(*value, VIRT_PRIV_PRV, get_field(dcsr, CSR_DCSR_PRV));
5106  return ERROR_OK;
5107  }
5108 
5109  LOG_TARGET_DEBUG(target, "reading register %s", riscv_reg_gdb_regno_name(target, rid));
5110 
5112  return ERROR_FAIL;
5113 
5114  if (register_read_direct(target, value, rid) != ERROR_OK) {
5115  *value = -1;
5116  return ERROR_FAIL;
5117  }
5118 
5119  return ERROR_OK;
5120 }
5121 
5123  riscv_reg_t value)
5124 {
5125  LOG_TARGET_DEBUG(target, "writing 0x%" PRIx64 " to register %s",
5127 
5129  return ERROR_FAIL;
5130 
5131  return register_write_direct(target, rid, value);
5132 }
5133 
5134 static int dm013_select_hart(struct target *target, int hart_index)
5135 {
5136  dm013_info_t *dm = get_dm(target);
5137  if (!dm)
5138  return ERROR_FAIL;
5139  if (hart_index == dm->current_hartid)
5140  return ERROR_OK;
5141 
5142  /* `hartsel` should not be changed if `abstractcs.busy` is set. */
5143  int result = wait_for_idle_if_needed(target);
5144  if (result != ERROR_OK)
5145  return result;
5146 
5147  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE;
5148  dmcontrol = set_dmcontrol_hartsel(dmcontrol, hart_index);
5149  if (dm_write(target, DM_DMCONTROL, dmcontrol) != ERROR_OK) {
5150  /* Who knows what the state is? */
5152  return ERROR_FAIL;
5153  }
5154  dm->current_hartid = hart_index;
5155  return ERROR_OK;
5156 }
5157 
5158 /* Select all harts that were prepped and that are selectable, clearing the
5159  * prepped flag on the harts that actually were selected. */
5161 {
5162  RISCV_INFO(r);
5163  dm013_info_t *dm = get_dm(target);
5164  if (!dm)
5165  return ERROR_FAIL;
5166  if (!dm->hasel_supported) {
5167  r->prepped = false;
5168  return dm013_select_target(target);
5169  }
5170 
5171  assert(dm->hart_count);
5172  unsigned int hawindow_count = (dm->hart_count + 31) / 32;
5173  uint32_t *hawindow = calloc(hawindow_count, sizeof(uint32_t));
5174  if (!hawindow)
5175  return ERROR_FAIL;
5176 
5177  target_list_t *entry;
5178  unsigned int total_selected = 0;
5179  unsigned int selected_index = 0;
5180  list_for_each_entry(entry, &dm->target_list, list) {
5181  struct target *t = entry->target;
5182  struct riscv_info *info = riscv_info(t);
5183  riscv013_info_t *info_013 = get_info(t);
5184  unsigned int index = info_013->index;
5185  LOG_TARGET_DEBUG(target, "index=%d, prepped=%d", index, info->prepped);
5186  if (info->prepped) {
5187  info_013->selected = true;
5188  hawindow[index / 32] |= 1 << (index % 32);
5189  info->prepped = false;
5190  total_selected++;
5191  selected_index = index;
5192  }
5193  }
5194 
5195  if (total_selected == 0) {
5196  LOG_TARGET_ERROR(target, "No harts were prepped!");
5197  free(hawindow);
5198  return ERROR_FAIL;
5199  } else if (total_selected == 1) {
5200  /* Don't use hasel if we only need to talk to one hart. */
5201  free(hawindow);
5202  return dm013_select_hart(target, selected_index);
5203  }
5204 
5206  free(hawindow);
5207  return ERROR_FAIL;
5208  }
5209 
5210  for (unsigned int i = 0; i < hawindow_count; i++) {
5211  if (dm_write(target, DM_HAWINDOWSEL, i) != ERROR_OK) {
5212  free(hawindow);
5213  return ERROR_FAIL;
5214  }
5215  if (dm_write(target, DM_HAWINDOW, hawindow[i]) != ERROR_OK) {
5216  free(hawindow);
5217  return ERROR_FAIL;
5218  }
5219  }
5220 
5221  free(hawindow);
5222  return ERROR_OK;
5223 }
5224 
5225 static int riscv013_halt_prep(struct target *target)
5226 {
5227  return ERROR_OK;
5228 }
5229 
5230 static int riscv013_halt_go(struct target *target)
5231 {
5232  dm013_info_t *dm = get_dm(target);
5233  if (!dm)
5234  return ERROR_FAIL;
5235 
5237  return ERROR_FAIL;
5238 
5239  LOG_TARGET_DEBUG(target, "halting hart");
5240 
5241  /* `haltreq` should not be issued if `abstractcs.busy` is set. */
5242  int result = wait_for_idle_if_needed(target);
5243  if (result != ERROR_OK)
5244  return result;
5245 
5246  /* Issue the halt command, and then wait for the current hart to halt. */
5247  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_HALTREQ;
5248  dmcontrol = set_dmcontrol_hartsel(dmcontrol, dm->current_hartid);
5249  dm_write(target, DM_DMCONTROL, dmcontrol);
5250  uint32_t dmstatus;
5251  for (size_t i = 0; i < 256; ++i) {
5252  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5253  return ERROR_FAIL;
5254  /* When no harts are running, there's no point in continuing this loop. */
5255  if (!get_field(dmstatus, DM_DMSTATUS_ANYRUNNING))
5256  break;
5257  }
5258 
5259  /* We declare success if no harts are running. One or more of them may be
5260  * unavailable, though. */
5261 
5262  if ((get_field(dmstatus, DM_DMSTATUS_ANYRUNNING))) {
5263  if (dm_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
5264  return ERROR_FAIL;
5265 
5266  LOG_TARGET_ERROR(target, "Unable to halt. dmcontrol=0x%08x, dmstatus=0x%08x",
5267  dmcontrol, dmstatus);
5268  return ERROR_FAIL;
5269  }
5270 
5271  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_HALTREQ, 0);
5272  dm_write(target, DM_DMCONTROL, dmcontrol);
5273 
5274  if (dm->current_hartid == HART_INDEX_MULTIPLE) {
5275  target_list_t *entry;
5276  list_for_each_entry(entry, &dm->target_list, list) {
5277  struct target *t = entry->target;
5278  uint32_t t_dmstatus;
5279  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED) ||
5280  get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5281  /* All harts are either halted or unavailable. No
5282  * need to read dmstatus for each hart. */
5283  t_dmstatus = dmstatus;
5284  } else {
5285  /* Only some harts were halted/unavailable. Read
5286  * dmstatus for this one to see what its status
5287  * is. */
5289  return ERROR_FAIL;
5290  if (dm_read(target, &t_dmstatus, DM_DMSTATUS) != ERROR_OK)
5291  return ERROR_FAIL;
5292  }
5293  /* Set state for the current target based on its dmstatus. */
5294  if (get_field(t_dmstatus, DM_DMSTATUS_ALLHALTED)) {
5295  t->state = TARGET_HALTED;
5298  } else if (get_field(t_dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5300  }
5301  }
5302 
5303  } else {
5304  /* Set state for the current target based on its dmstatus. */
5305  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED)) {
5309  } else if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5311  }
5312  }
5313 
5314  return ERROR_OK;
5315 }
5316 
5317 static int riscv013_resume_go(struct target *target)
5318 {
5320  return ERROR_FAIL;
5321 
5323 }
5324 
5326 {
5328 }
5329 
5331 {
5332  assert(target->state == TARGET_HALTED);
5333  return riscv013_on_step_or_resume(target, false);
5334 }
5335 
5336 static int riscv013_on_step(struct target *target)
5337 {
5338  return riscv013_on_step_or_resume(target, true);
5339 }
5340 
5342 {
5343  riscv_reg_t dcsr;
5344  int result = register_read_direct(target, &dcsr, GDB_REGNO_DCSR);
5345  if (result != ERROR_OK)
5346  return RISCV_HALT_UNKNOWN;
5347 
5348  LOG_TARGET_DEBUG(target, "dcsr.cause: 0x%" PRIx64, get_field(dcsr, CSR_DCSR_CAUSE));
5349 
5350  switch (get_field(dcsr, CSR_DCSR_CAUSE)) {
5351  case CSR_DCSR_CAUSE_EBREAK:
5352  return RISCV_HALT_EBREAK;
5354  /* We could get here before triggers are enumerated if a trigger was
5355  * already set when we connected. Force enumeration now, which has the
5356  * side effect of clearing any triggers we did not set. */
5358  LOG_TARGET_DEBUG(target, "halted because of trigger");
5359  return RISCV_HALT_TRIGGER;
5360  case CSR_DCSR_CAUSE_STEP:
5361  return RISCV_HALT_SINGLESTEP;
5364  return RISCV_HALT_INTERRUPT;
5365  case CSR_DCSR_CAUSE_GROUP:
5366  return RISCV_HALT_GROUP;
5367  }
5368 
5369  LOG_TARGET_ERROR(target, "Unknown DCSR cause field: 0x%" PRIx64, get_field(dcsr, CSR_DCSR_CAUSE));
5370  LOG_TARGET_ERROR(target, " dcsr=0x%" PRIx32, (uint32_t)dcsr);
5371  return RISCV_HALT_UNKNOWN;
5372 }
5373 
5374 static int riscv013_write_progbuf(struct target *target, unsigned int index, riscv_insn_t data)
5375 {
5376  assert(index < RISCV013_MAX_PROGBUF_SIZE);
5377 
5378  dm013_info_t *dm = get_dm(target);
5379  if (!dm)
5380  return ERROR_FAIL;
5381 
5382  if (dm->progbuf_cache[index] != data) {
5383  if (dm_write(target, DM_PROGBUF0 + index, data) != ERROR_OK)
5384  return ERROR_FAIL;
5385  dm->progbuf_cache[index] = data;
5386  } else {
5387  LOG_TARGET_DEBUG(target, "Cache hit for 0x%" PRIx32 " @%d", data, index);
5388  }
5389  return ERROR_OK;
5390 }
5391 
5392 static riscv_insn_t riscv013_read_progbuf(struct target *target, unsigned int index)
5393 {
5394  uint32_t value;
5395  if (dm_read(target, &value, DM_PROGBUF0 + index) == ERROR_OK)
5396  return value;
5397  else
5398  return 0;
5399 }
5400 
5402 {
5403  dm013_info_t *dm = get_dm(target);
5404  if (!dm) {
5405  LOG_TARGET_DEBUG(target, "No DM is specified for the target");
5406  return ERROR_FAIL;
5407  }
5408 
5409  LOG_TARGET_DEBUG(target, "Invalidating progbuf cache");
5410  memset(dm->progbuf_cache, 0, sizeof(dm->progbuf_cache));
5411  return ERROR_OK;
5412 }
5413 
5414 static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr)
5415 {
5417  return ERROR_FAIL;
5418  uint32_t run_program = 0;
5419  run_program = set_field(run_program, AC_ACCESS_REGISTER_AARSIZE, 2);
5420  run_program = set_field(run_program, AC_ACCESS_REGISTER_POSTEXEC, 1);
5421  run_program = set_field(run_program, AC_ACCESS_REGISTER_TRANSFER, 0);
5422  run_program = set_field(run_program, AC_ACCESS_REGISTER_REGNO, 0x1000);
5423 
5424  return riscv013_execute_abstract_command(target, run_program, cmderr);
5425 }
5426 
5427 static void riscv013_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
5428 {
5432  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
5433 }
5434 
5435 static void riscv013_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
5436 {
5440  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
5441 }
5442 
5443 static void riscv013_fill_dm_nop(const struct target *target, uint8_t *buf)
5444 {
5448  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, 0);
5449 }
5450 
5451 static unsigned int riscv013_get_dmi_address_bits(const struct target *target)
5452 {
5454  return info->abits;
5455 }
5456 
5457 /* Helper Functions. */
5459 {
5462  return ERROR_FAIL;
5463 
5465  return ERROR_FAIL;
5466 
5468  return ERROR_FAIL;
5469  return ERROR_OK;
5470 }
5471 
5473  bool step)
5474 {
5475  if (target->state != TARGET_HALTED) {
5476  LOG_TARGET_ERROR(target, "Hart is not halted!");
5477  return ERROR_TARGET_NOT_HALTED;
5478  }
5479 
5480  LOG_TARGET_DEBUG(target, "resuming (operation=%s)",
5481  step ? "single-step" : "resume");
5482 
5484  return ERROR_FAIL;
5485 
5487 
5488  dm013_info_t *dm = get_dm(target);
5489  /* Issue the resume command, and then wait for the current hart to resume. */
5490  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_RESUMEREQ;
5491  dmcontrol = set_dmcontrol_hartsel(dmcontrol, dm->current_hartid);
5492  /* `resumereq` should not be issued if `abstractcs.busy` is set. */
5493  int result = wait_for_idle_if_needed(target);
5494  if (result != ERROR_OK)
5495  return result;
5496  dm_write(target, DM_DMCONTROL, dmcontrol);
5497 
5498  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_RESUMEREQ, 0);
5499 
5500  uint32_t dmstatus;
5501  for (size_t i = 0; i < 256; ++i) {
5502  usleep(10);
5503  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5504  return ERROR_FAIL;
5505  if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL))
5506  return ERROR_FAIL;
5507  if (get_field(dmstatus, DM_DMSTATUS_ALLRESUMEACK) == 0)
5508  continue;
5509  if (step && get_field(dmstatus, DM_DMSTATUS_ALLHALTED) == 0)
5510  continue;
5511 
5512  dm_write(target, DM_DMCONTROL, dmcontrol);
5513  return ERROR_OK;
5514  }
5515 
5516  LOG_TARGET_ERROR(target, "Failed to %s. dmstatus=0x%08x",
5517  step ? "single-step" : "resume", dmstatus);
5518 
5519  dm_write(target, DM_DMCONTROL, dmcontrol);
5521  " cancelling the resume request (dmcontrol.resumereq <- 0)");
5522 
5523  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5524  return ERROR_FAIL;
5525 
5526  LOG_TARGET_ERROR(target, " dmstatus after cancellation=0x%08x", dmstatus);
5527 
5528  if (step) {
5530  " trying to recover from a failed single-step, by requesting halt");
5531  if (riscv_halt(target) == ERROR_OK)
5532  LOG_TARGET_ERROR(target, " halt completed after failed single-step");
5533  else
5534  LOG_TARGET_ERROR(target, " could not halt, something is wrong with the taget");
5535  // TODO: returning ERROR_OK is questionable, this code needs to be revised
5536  return ERROR_OK;
5537  }
5538 
5539  return ERROR_FAIL;
5540 }
5541 
5543 {
5544  uint32_t abstractcs;
5545  int result = wait_for_idle(target, &abstractcs);
5546  /* Clear the error status, even if busy is still set. */
5548  result = ERROR_FAIL;
5549  return result;
5550 }
#define IS_PWR_OF_2(x)
Definition: align.h:24
const char * group
Definition: armv4_5.c:367
bool riscv_batch_was_batch_busy(const struct riscv_batch *batch)
Definition: batch.c:438
uint32_t riscv_batch_get_dmi_read_op(const struct riscv_batch *batch, size_t key)
Definition: batch.c:389
struct riscv_batch * riscv_batch_alloc(struct target *target, size_t scans)
Definition: batch.c:31
void riscv_batch_add_nop(struct riscv_batch *batch)
Definition: batch.c:409
void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint32_t address, uint32_t data, bool read_back, enum riscv_scan_delay_class delay_class)
Definition: batch.c:331
size_t riscv_batch_available_scans(struct riscv_batch *batch)
Definition: batch.c:432
uint32_t riscv_batch_get_dmi_read_data(const struct riscv_batch *batch, size_t key)
Definition: batch.c:399
size_t riscv_batch_finished_scans(const struct riscv_batch *batch)
Definition: batch.c:446
void riscv_batch_free(struct riscv_batch *batch)
Definition: batch.c:96
size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, uint32_t address, enum riscv_scan_delay_class delay_class)
Definition: batch.c:361
int riscv_batch_run_from(struct riscv_batch *batch, size_t start_idx, const struct riscv_scan_delays *delays, bool resets_delays, size_t reset_delays_after)
Definition: batch.c:278
static int riscv_scan_increase_delay(struct riscv_scan_delays *delays, enum riscv_scan_delay_class delay_class)
Definition: batch.h:105
riscv_scan_delay_class
Definition: batch.h:20
@ RISCV_DELAY_ABSTRACT_COMMAND
Definition: batch.h:24
@ RISCV_DELAY_SYSBUS_READ
Definition: batch.h:26
@ RISCV_DELAY_BASE
Definition: batch.h:22
@ RISCV_DELAY_SYSBUS_WRITE
Definition: batch.h:28
static size_t riscv_batch_add_dm_read(struct riscv_batch *batch, uint32_t address, enum riscv_scan_delay_class delay_type)
Definition: batch.h:212
static void riscv_scan_set_delay(struct riscv_scan_delays *delays, enum riscv_scan_delay_class delay_class, unsigned int delay)
Definition: batch.h:82
static unsigned int riscv_scan_get_delay(const struct riscv_scan_delays *delays, enum riscv_scan_delay_class delay_class)
Definition: batch.h:65
static void riscv_batch_add_dm_write(struct riscv_batch *batch, uint32_t address, uint32_t data, bool read_back, enum riscv_scan_delay_class delay_type)
Definition: batch.h:197
static const char * riscv_scan_delay_class_name(enum riscv_scan_delay_class delay_class)
Definition: batch.h:32
bool buf_eq(const void *_buf1, const void *_buf2, unsigned int size)
Definition: binarybuffer.c:70
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
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define DM_ABSTRACTAUTO_AUTOEXECDATA_OFFSET
#define AC_ACCESS_REGISTER_TRANSFER
#define DM_DATA0
#define CSR_DCSR_EBREAKM
#define AC_ACCESS_REGISTER_POSTEXEC
#define CSR_DCSR_MPRVEN_ENABLED
#define DM_AUTHDATA
#define DM_DMCONTROL_ACKHAVERESET
#define DM_DMSTATUS_ANYHAVERESET
#define CSR_DCSR_CAUSE_GROUP
#define DM_DMSTATUS_ALLHALTED
#define DM_SBCS_SBACCESS64
#define DM_SBCS_SBVERSION
#define DM_DMCONTROL_RESUMEREQ
#define DM_SBDATA3
#define DM_DMCS2_HGWRITE
#define DM_ABSTRACTCS
#define DM_ABSTRACTCS_BUSY
#define DM_DMSTATUS_ALLRESUMEACK
#define DM_DMCONTROL_HARTSELLO_LENGTH
#define DM_DMCONTROL
#define DM_SBCS_SBACCESS
#define DM_NEXTDM
#define DM_SBDATA2
#define DM_SBCS
#define DM_SBCS_SBBUSY
#define DM_SBCS_SBBUSYERROR
#define DM_DMCONTROL_HASEL_SINGLE
#define DTM_DTMCS_IDLE
#define DM_ABSTRACTCS_CMDERR
#define DM_HARTINFO_DATASIZE
#define AC_ACCESS_REGISTER_REGNO
#define CSR_DCSR_EBREAKVU
#define DM_ABSTRACTCS_PROGBUFSIZE
#define DM_SBDATA0
#define DM_DMCONTROL_HASEL_MULTIPLE
#define DM_PROGBUF1
#define CSR_DCSR_CAUSE_STEP
#define DM_DMSTATUS_ALLUNAVAIL
#define DM_ABSTRACTCS_DATACOUNT
#define DTM_DMI_DATA_OFFSET
#define DM_DATA1
#define DM_HAWINDOWSEL
#define DM_DMSTATUS_AUTHENTICATED
#define DM_SBCS_SBAUTOINCREMENT
#define DM_SBADDRESS1
#define DTM_DMI_OP_WRITE
#define DM_SBCS_SBERROR_NONE
#define DM_SBCS_SBASIZE
#define VIRT_PRIV_PRV
#define DM_SBDATA1
#define AC_ACCESS_MEMORY_WRITE
#define DM_DMCONTROL_HARTSELLO
#define DM_DMCONTROL_NDMRESET
#define AC_ACCESS_REGISTER_WRITE
#define DM_DMSTATUS_ALLRUNNING
#define DM_SBADDRESS3
#define DTM_DMI_OP_OFFSET
#define CSR_DCSR_CAUSE_HALTREQ
#define AC_ACCESS_MEMORY_CMDTYPE
#define DM_HARTINFO_DATAACCESS
#define DTM_DTMCS_VERSION
#define CSR_DCSR_EBREAKS
#define DTM_DMI_OP_FAILED
#define DM_DMSTATUS_IMPEBREAK
#define DTM_DTMCS_ABITS
#define DM_DMSTATUS_ANYNONEXISTENT
#define DM_DMCONTROL_DMACTIVE
#define CSR_DCSR_EBREAKVS
#define DM_DMCONTROL_HASEL
riscv_debug_reg_ordinal
@ AC_ACCESS_MEMORY_ORDINAL
@ AC_QUICK_ACCESS_ORDINAL
@ AC_ACCESS_REGISTER_ORDINAL
#define CSR_DCSR_V
#define DTM_DMI_ADDRESS_OFFSET
#define DM_SBCS_SBACCESS8
#define DTM_DTMCS_DMIRESET
Definition: debug_defines.h:84
#define DM_DMSTATUS
#define CSR_DCSR_MPRVEN
#define DM_DMCONTROL_HARTSELHI_LENGTH
#define CSR_DCSR_STEP
#define CSR_DCSR_EBREAKU
#define DM_DMCS2
#define AC_ACCESS_REGISTER_AARSIZE
#define CSR_DCSR_CAUSE_EBREAK
#define DM_COMMAND
#define DM_SBCS_SBERROR
#define VIRT_PRIV_V
#define DM_DMSTATUS_VERSION
#define DM_DMSTATUS_AUTHBUSY
#define DM_DMCONTROL_HARTSELHI
#define DM_HARTINFO
#define AC_ACCESS_MEMORY_AAMPOSTINCREMENT
#define DTM_DMI_OP_BUSY
#define DM_SBCS_SBACCESS16
#define DM_PROGBUF0
#define DM_ABSTRACTAUTO
#define DM_SBCS_SBREADONADDR
#define DM_DMSTATUS_ALLHAVERESET
#define DTM_DMI_OP_NOP
#define DM_SBCS_SBACCESS32
#define AC_ACCESS_MEMORY_AAMSIZE
#define CSR_DCSR_PRV
#define DM_SBCS_SBREADONDATA
#define DM_DMSTATUS_ALLNONEXISTENT
#define CSR_DCSR_CAUSE_TRIGGER
#define DTM_DMI_OP_READ
#define DM_HARTINFO_DATAADDR
#define DM_DMCONTROL_HALTREQ
#define DM_DMCS2_GROUPTYPE
#define DTM_DMI_DATA_LENGTH
#define DM_SBADDRESS2
#define CSR_DCSR_CAUSE_RESETHALTREQ
#define DM_ABSTRACTAUTO_AUTOEXECDATA
#define AC_ACCESS_MEMORY_AAMVIRTUAL
#define DM_DMCS2_GROUP
#define DM_SBCS_SBACCESS128
#define DTM_DMI_OP_LENGTH
#define DTM_DTMCS
Definition: debug_defines.h:32
#define CSR_DCSR_CAUSE
#define DTM_DMI_OP_SUCCESS
#define DM_DMSTATUS_ANYRUNNING
#define DM_COMMAND_CMDTYPE
#define DM_HAWINDOW
#define DM_SBADDRESS0
unsigned int riscv_debug_reg_to_s(char *buf, enum riscv_debug_reg_ordinal reg_ordinal, struct riscv_debug_reg_ctx context, uint64_t value, enum riscv_debug_reg_show show)
This function is used to fill a buffer with a decoded string representation of register's value.
@ RISCV_DEBUG_REG_HIDE_UNNAMED_0
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
unsigned short width
Definition: embeddedice.c:47
#define MSTATUS_VS
Definition: encoding.h:22
#define MSTATUS_MPP
Definition: encoding.h:23
#define CSR_VTYPE
Definition: encoding.h:2831
#define CSR_FRM
Definition: encoding.h:2790
#define CSR_VL
Definition: encoding.h:2830
#define MSTATUS_FS
Definition: encoding.h:24
#define CSR_FCSR
Definition: encoding.h:2791
#define CSR_FFLAGS
Definition: encoding.h:2789
#define MSTATUS_MPRV
Definition: encoding.h:26
#define PRV_M
Definition: encoding.h:236
enum esirisc_reg_num number
Definition: esirisc.c:87
static uint64_t set_field(uint64_t reg, uint64_t mask, uint64_t val)
Definition: field_helpers.h:21
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
gdb_regno
Definition: gdb_regs.h:10
@ GDB_REGNO_CSR0
Definition: gdb_regs.h:82
@ GDB_REGNO_MSTATUS
Definition: gdb_regs.h:102
@ GDB_REGNO_VXRM
Definition: gdb_regs.h:88
@ GDB_REGNO_ZERO
Definition: gdb_regs.h:11
@ GDB_REGNO_VTYPE
Definition: gdb_regs.h:92
@ GDB_REGNO_VXSAT
Definition: gdb_regs.h:87
@ GDB_REGNO_S1
Definition: gdb_regs.h:21
@ GDB_REGNO_FPR31
Definition: gdb_regs.h:81
@ GDB_REGNO_FPR0
Definition: gdb_regs.h:48
@ GDB_REGNO_V0
Definition: gdb_regs.h:117
@ GDB_REGNO_VL
Definition: gdb_regs.h:91
@ GDB_REGNO_VSTART
Definition: gdb_regs.h:86
@ GDB_REGNO_XPR31
Definition: gdb_regs.h:45
@ GDB_REGNO_A0
Definition: gdb_regs.h:22
@ GDB_REGNO_S0
Definition: gdb_regs.h:19
@ GDB_REGNO_VLENB
Definition: gdb_regs.h:90
@ GDB_REGNO_V31
Definition: gdb_regs.h:124
@ GDB_REGNO_PRIV
Definition: gdb_regs.h:112
@ GDB_REGNO_VCSR
Definition: gdb_regs.h:89
@ GDB_REGNO_CSR4095
Definition: gdb_regs.h:111
@ GDB_REGNO_COUNT
Definition: gdb_regs.h:125
@ GDB_REGNO_DCSR
Definition: gdb_regs.h:100
const char * jtag_tap_name(const struct jtag_tap *tap)
Definition: jtag/core.c:277
struct jtag_tap * jtag_tap_next_enabled(struct jtag_tap *p)
Definition: jtag/core.c:266
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
static int list_empty(const struct list_head *head)
Definition: list.h:61
#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
void log_printf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format,...)
Definition: log.c:201
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:167
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:173
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#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 ERROR_TIMEOUT_REACHED
Definition: log.h:191
#define LOG_LEVEL_IS(FOO)
Definition: log.h:112
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_DEBUG
Definition: log.h:55
@ LOG_LVL_WARNING
Definition: log.h:53
static uint32_t fmv_d_x(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:327
static uint32_t csrr(unsigned int rd, unsigned int csr) __attribute__((unused))
Definition: opcodes.h:211
#define S0
Definition: opcodes.h:13
static uint32_t vsetvl(unsigned int rd, unsigned int rs1, unsigned int rs2) __attribute__((unused))
Definition: opcodes.h:410
#define S1
Definition: opcodes.h:14
static uint32_t vmv_x_s(unsigned int rd, unsigned int vs2) __attribute__((unused))
Definition: opcodes.h:420
static uint32_t fsd(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:270
static uint32_t fmv_x_w(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:300
static uint32_t fmv_w_x(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:318
static uint32_t vslide1down_vx(unsigned int vd, unsigned int vs2, unsigned int rs1, bool vm) __attribute__((unused))
Definition: opcodes.h:439
#define ZERO
Definition: opcodes.h:11
static uint32_t auipc(unsigned int dest) __attribute__((unused))
Definition: opcodes.h:392
static uint32_t sw(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:112
static uint32_t fmv_x_d(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:309
static uint32_t fld(unsigned int dest, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:290
int riscv_program_fence_i(struct riscv_program *p)
Definition: program.c:171
int riscv_program_write(struct riscv_program *program)
Definition: program.c:30
int riscv_program_fence_rw_rw(struct riscv_program *p)
Definition: program.c:176
int riscv_program_store(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int16_t offset, unsigned int size)
Definition: program.c:93
int riscv_program_addi(struct riscv_program *p, enum gdb_regno d, enum gdb_regno s, int16_t u)
Definition: program.c:192
int riscv_program_insert(struct riscv_program *p, riscv_insn_t i)
Definition: program.c:197
int riscv_program_load(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int16_t offset, unsigned int size)
Definition: program.c:130
int riscv_program_csrr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno csr)
Definition: program.c:159
int riscv_program_init(struct riscv_program *p, struct target *target)
Definition: program.c:17
int riscv_program_csrw(struct riscv_program *p, enum gdb_regno s, enum gdb_regno csr)
Definition: program.c:165
int riscv_program_ebreak(struct riscv_program *p)
Definition: program.c:181
int riscv_program_exec(struct riscv_program *p, struct target *t)
Add ebreak and execute the program.
Definition: program.c:42
#define RISCV013_MAX_PROGBUF_SIZE
Definition: program.h:8
@ RISCV_PROGBUF_EXEC_RESULT_EXCEPTION
Definition: program.h:13
#define MIN(a, b)
Definition: replacements.h:22
#define MAX(a, b)
Definition: replacements.h:25
static int step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv-011.c:1466
static int riscv013_write_progbuf(struct target *target, unsigned int index, riscv_insn_t d)
Definition: riscv-013.c:5374
static int register_write_abstract(struct target *target, enum gdb_regno number, riscv_reg_t value)
Definition: riscv-013.c:962
static int dmi_write(struct target *target, uint32_t address, uint32_t value)
Definition: riscv-013.c:519
static void batch_fill_sb_write_address(const struct target *target, struct riscv_batch *batch, target_addr_t address, enum riscv_scan_delay_class sbaddr0_delay)
Definition: riscv-013.c:2480
static int read_word_from_dm_data_regs(struct target *target, const struct riscv_mem_access_args args, uint32_t index)
Definition: riscv-013.c:4286
static int scratch_write64(struct target *target, scratch_mem_t *scratch, uint64_t value)
Definition: riscv-013.c:1294
static int examine_dm(struct target *target)
Definition: riscv-013.c:1901
static riscv_reg_t abstract_data_get_from_batch(struct riscv_batch *batch, unsigned int index, unsigned int size_bits)
Definition: riscv-013.c:799
static int examine_progbuf(struct target *target)
Definition: riscv-013.c:1042
static int write_memory_bus_v1(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4666
static struct mem_access_result mem_access_result(enum mem_access_result_enum value)
Definition: riscv-013.c:3660
static int csr_write_progbuf(struct target *target, enum gdb_regno number, riscv_reg_t value)
Definition: riscv-013.c:1555
static struct mem_access_result read_memory_progbuf_inner(struct target *target, const struct riscv_mem_access_args args)
Read the requested memory, taking care to minimize the number of reads and re-read the data only if a...
Definition: riscv-013.c:4352
static int read_memory_progbuf_inner_run_and_process_batch(struct target *target, struct riscv_batch *batch, const struct riscv_mem_access_args args, uint32_t start_index, uint32_t elements_to_read, uint32_t *elements_read)
This function reads a batch of elements from memory.
Definition: riscv-013.c:4142
static int riscv013_step_current_hart(struct target *target)
Definition: riscv-013.c:5325
static void riscv013_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
Definition: riscv-013.c:5435
static int riscv013_step_or_resume_current_hart(struct target *target, bool step)
Definition: riscv-013.c:5472
static int write_memory_progbuf_startup(struct target *target, target_addr_t *address_p, const uint8_t *buffer, uint32_t size)
This function is used to start the memory-writing pipeline.
Definition: riscv-013.c:4812
static uint32_t sb_sbaccess(unsigned int size_bytes)
Definition: riscv-013.c:2455
int riscv013_set_register_buf(struct target *target, enum gdb_regno regno, const uint8_t *value)
Definition: riscv-013.c:2416
static dm013_info_t * get_dm(struct target *target)
Return the DM structure for this target.
Definition: riscv-013.c:276
static struct mem_access_result access_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4465
static int read_memory_bus_word(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: riscv-013.c:3143
static int dm_write(struct target *target, uint32_t address, uint32_t value)
Definition: riscv-013.c:529
static void abstract_data_write_fill_batch(struct riscv_batch *batch, riscv_reg_t value, unsigned int index, unsigned int size_bits)
Queue scans into a batch that write the value to abstract data registers: data[index] (and data[index...
Definition: riscv-013.c:838
dmi_status_t
Definition: riscv-013.c:93
@ DMI_STATUS_SUCCESS
Definition: riscv-013.c:94
@ DMI_STATUS_FAILED
Definition: riscv-013.c:95
@ DMI_STATUS_BUSY
Definition: riscv-013.c:96
static unsigned int register_size(struct target *target, enum gdb_regno number)
Return register size in bits.
Definition: riscv-013.c:1335
static int cleanup_after_register_access(struct target *target, riscv_reg_t mstatus, enum gdb_regno regno)
Definition: riscv-013.c:1156
static int riscv013_on_step_or_resume(struct target *target, bool step)
Definition: riscv-013.c:5458
static int vl_write_progbuf(struct target *target, riscv_reg_t value)
Definition: riscv-013.c:1534
static int riscv013_on_step(struct target *target)
Definition: riscv-013.c:5336
static struct mem_access_result write_memory_progbuf_inner(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:5016
static int abstract_cmd_batch_check_and_clear_cmderr(struct target *target, const struct riscv_batch *batch, size_t abstractcs_read_key, uint32_t *cmderr)
Definition: riscv-013.c:673
static struct mem_access_result read_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3778
static target_addr_t write_memory_progbuf_fill_batch(struct riscv_batch *batch, target_addr_t start_address, target_addr_t end_address, uint32_t size, const uint8_t *buffer)
This function fills the batch with DMI writes (but does not execute the batch).
Definition: riscv-013.c:4898
bool is_mem_access_failed(struct mem_access_result status)
Definition: riscv-013.c:3616
static int fpr_read_progbuf(struct target *target, uint64_t *value, enum gdb_regno number)
Definition: riscv-013.c:1381
static int select_prepped_harts(struct target *target)
Definition: riscv-013.c:5160
#define CMDERR_NOT_SUPPORTED
Definition: riscv-013.c:104
static struct mem_access_result access_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4533
static bool has_sufficient_progbuf(struct target *target, unsigned int size)
Definition: riscv-013.c:1345
static int fpr_write_progbuf(struct target *target, enum gdb_regno number, riscv_reg_t value)
Definition: riscv-013.c:1480
static int csr_read_progbuf(struct target *target, uint64_t *value, enum gdb_regno number)
Definition: riscv-013.c:1413
static void riscv013_dm_free(struct target *target)
Definition: riscv-013.c:328
static int read_memory_progbuf_inner_extract_batch_data(struct target *target, const struct riscv_batch *batch, uint32_t start_index, uint32_t elements_to_read, uint32_t *elements_read, const struct riscv_mem_access_args args)
This function extracts the data from the batch.
Definition: riscv-013.c:4083
static struct mem_access_result mem_should_skip_abstract(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3743
static int register_read_direct(struct target *target, riscv_reg_t *value, enum gdb_regno number)
Actually read registers from the target right now.
Definition: riscv-013.c:1630
#define CMDERR_BUSY
Definition: riscv-013.c:103
static int scratch_reserve(struct target *target, scratch_mem_t *scratch, struct riscv_program *program, unsigned int size_bytes)
Find some scratch memory to be used with the given program.
Definition: riscv-013.c:1186
static void riscv013_fill_dm_nop(const struct target *target, uint8_t *buf)
Definition: riscv-013.c:5443
struct target_type riscv013_target
Definition: riscv-013.c:5077
static int wait_for_idle(struct target *target, uint32_t *abstractcs)
Definition: riscv-013.c:611
static void ac_cache_insert(struct ac_cache *cache, uint32_t command)
Definition: riscv-013.c:179
static int dm013_select_hart(struct target *target, int hart_index)
Definition: riscv-013.c:5134
static int is_vector_reg(enum gdb_regno gdb_regno)
Definition: riscv-013.c:1105
static int dm_read(struct target *target, uint32_t *value, uint32_t address)
Definition: riscv-013.c:499
static int register_read_progbuf(struct target *target, uint64_t *value, enum gdb_regno number)
This function reads a register by writing a program to program buffer and executing it.
Definition: riscv-013.c:1436
static int sb_write_address(struct target *target, target_addr_t address, enum riscv_scan_delay_class sbaddr0_delay)
Definition: riscv-013.c:2500
static int examine(struct target *target)
Definition: riscv-013.c:1991
static int restore_privilege_from_virt2phys_mode(struct target *target, riscv_reg_t mstatus, riscv_reg_t mstatus_old, riscv_reg_t dcsr, riscv_reg_t dcsr_old)
Definition: riscv-013.c:3240
static void mark_command_as_unsupported(struct target *target, uint32_t command)
Definition: riscv-013.c:728
dmi_op_t
Definition: riscv-013.c:88
@ DMI_OP_NOP
Definition: riscv-013.c:89
@ DMI_OP_READ
Definition: riscv-013.c:90
@ DMI_OP_WRITE
Definition: riscv-013.c:91
static int reset_dm(struct target *target)
Definition: riscv-013.c:1828
static int ac_cache_elem_comparator(const void *p_lhs, const void *p_rhs)
Definition: riscv-013.c:152
static int deassert_reset(struct target *target)
Definition: riscv-013.c:2955
static void select_dmi(struct jtag_tap *tap)
Definition: riscv-013.c:408
memory_space_t
Definition: riscv-013.c:1167
@ SPACE_DMI_PROGBUF
Definition: riscv-013.c:1169
@ SPACE_DM_DATA
Definition: riscv-013.c:1168
@ SPACE_DMI_RAM
Definition: riscv-013.c:1170
grouptype
Definition: riscv-013.c:70
@ RESUME_GROUP
Definition: riscv-013.c:72
@ HALT_GROUP
Definition: riscv-013.c:71
static struct mem_access_result mem_should_skip_progbuf(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3666
int riscv013_set_register(struct target *target, enum gdb_regno rid, riscv_reg_t value)
Definition: riscv-013.c:5122
bool is_mem_access_ok(struct mem_access_result status)
Definition: riscv-013.c:3600
static int riscv013_halt_go(struct target *target)
Definition: riscv-013.c:5230
static int vtype_write_progbuf(struct target *target, riscv_reg_t value)
Definition: riscv-013.c:1513
static int cleanup_after_vector_access(struct target *target, riscv_reg_t mstatus, riscv_reg_t vtype, riscv_reg_t vl)
Definition: riscv-013.c:2350
static OOCD_LIST_HEAD(dm_list)
static int assert_reset(struct target *target)
Definition: riscv-013.c:2904
static int write_memory_progbuf_run_batch(struct target *target, struct riscv_batch *batch, target_addr_t *address_p, target_addr_t end_address, uint32_t size, const uint8_t *buffer)
This function runs the batch of writes and updates address_p with the address of the next write.
Definition: riscv-013.c:4929
static int batch_run(struct target *target, struct riscv_batch *batch)
Definition: riscv-013.c:2511
static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr)
Definition: riscv-013.c:5414
static uint32_t __attribute__((unused))
Definition: riscv-013.c:596
static int write_memory_progbuf_handle_busy(struct target *target, target_addr_t *address_p, target_addr_t end_address, uint32_t size, const uint8_t *buffer)
This function attempts to restore the pipeline after a busy on abstract access or a DMI busy by readi...
Definition: riscv-013.c:4866
static int register_write_progbuf(struct target *target, enum gdb_regno number, riscv_reg_t value)
This function writes a register by writing a program to program buffer and executing it.
Definition: riscv-013.c:1578
mem_access_result_type
Definition: riscv-013.c:3521
@ MEM_ACCESS_RESULT_TYPE_OK
Definition: riscv-013.c:3522
@ MEM_ACCESS_RESULT_TYPE_ENUM_SIZE
Definition: riscv-013.c:3526
@ MEM_ACCESS_RESULT_TYPE_SKIPPED
Definition: riscv-013.c:3524
@ MEM_ACCESS_RESULT_TYPE_FAILED
Definition: riscv-013.c:3525
@ MEM_ACCESS_RESULT_TYPE_DISABLED
Definition: riscv-013.c:3523
static int riscv013_invalidate_cached_progbuf(struct target *target)
Definition: riscv-013.c:5401
static int handle_became_unavailable(struct target *target, enum riscv_hart_state previous_riscv_state)
Definition: riscv-013.c:2826
static int read_memory_progbuf_inner_fill_progbuf(struct target *target, uint32_t increment, uint32_t size)
Definition: riscv-013.c:4311
static int read_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3259
mem_access_result_enum
Definition: riscv-013.c:3589
static struct mem_access_result read_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
Read the requested memory, silently handling memory access errors.
Definition: riscv-013.c:4446
static void log_debug_reg(struct target *target, enum riscv_debug_reg_ordinal reg, riscv_reg_t value, const char *file, unsigned int line, const char *func)
Definition: riscv-013.c:367
static int register_read_abstract_with_size(struct target *target, riscv_reg_t *value, enum gdb_regno number, unsigned int size)
Definition: riscv-013.c:931
int riscv013_get_register(struct target *target, riscv_reg_t *value, enum gdb_regno rid)
Definition: riscv-013.c:5093
static struct mem_access_result read_memory_progbuf_inner_one(struct target *target, const struct riscv_mem_access_args args)
Only need to save/restore one GPR to read a single word, and the progbuf program doesn't need to incr...
Definition: riscv-013.c:4410
static void riscv013_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
Definition: riscv-013.c:5427
static enum riscv_halt_reason riscv013_halt_reason(struct target *target)
Definition: riscv-013.c:5341
bool is_mem_access_skipped(struct mem_access_result status)
Definition: riscv-013.c:3632
static unsigned int get_sbaadress_reg_count(const struct target *target)
Definition: riscv-013.c:2473
static int dmstatus_read(struct target *target, uint32_t *dmstatus, bool authenticated)
Definition: riscv-013.c:568
#define ABSTRACT_COMMAND_BATCH_SIZE
Definition: riscv-013.c:661
#define RISCV013_INFO(r)
Since almost everything can be accomplish by scanning the dbus register, all functions here assume db...
Definition: riscv-013.c:84
static int batch_run_timeout(struct target *target, struct riscv_batch *batch)
Definition: riscv-013.c:2535
static int riscv013_access_memory(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4552
static riscv_insn_t riscv013_read_progbuf(struct target *target, unsigned int index)
Definition: riscv-013.c:5392
static int write_abstract_arg(struct target *target, unsigned int index, riscv_reg_t value, unsigned int size_bits)
Definition: riscv-013.c:856
static uint32_t riscv013_get_dmi_address(const struct target *target, uint32_t address)
Definition: riscv-013.c:476
static int dmi_read(struct target *target, uint32_t *value, uint32_t address)
Definition: riscv-013.c:488
static int wait_for_idle_if_needed(struct target *target)
Definition: riscv-013.c:1808
static int read_memory_bus_v1(struct target *target, const struct riscv_mem_access_args args)
Read the requested memory using the system bus interface.
Definition: riscv-013.c:3349
static int set_group(struct target *target, bool *supported, unsigned int group, enum grouptype grouptype)
static int read_memory_progbuf_inner_startup(struct target *target, target_addr_t address, uint32_t increment, uint32_t index)
This function is used to start the memory-reading pipeline.
Definition: riscv-013.c:3934
static int sba_supports_access(struct target *target, unsigned int size_bytes)
Definition: riscv-013.c:2584
static size_t abstract_cmd_fill_batch(struct riscv_batch *batch, uint32_t command)
Definition: riscv-013.c:663
static int set_dcsr_ebreak(struct target *target, bool step)
Definition: riscv-013.c:1681
static int init_target(struct command_context *cmd_ctx, struct target *target)
Definition: riscv-013.c:2851
static struct mem_access_result access_memory_sysbus(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4505
static bool dcsr_ebreak_config_equals_reset_value(const struct target *target)
Definition: riscv-013.c:2946
static int is_fpu_reg(enum gdb_regno gdb_regno)
Definition: riscv-013.c:1097
static int dm_read_exec(struct target *target, uint32_t *value, uint32_t address)
Definition: riscv-013.c:504
static unsigned int riscv013_data_bits(struct target *target)
Definition: riscv-013.c:2218
static int riscv013_resume_prep(struct target *target)
Definition: riscv-013.c:5330
static void abstract_data_read_fill_batch(struct riscv_batch *batch, unsigned int index, unsigned int size_bits)
Queue scans into a batch that read the value from abstract data registers: data[index] (and data[inde...
Definition: riscv-013.c:786
static int scratch_read64(struct target *target, scratch_mem_t *scratch, uint64_t *value)
Definition: riscv-013.c:1253
const char * mem_access_result_to_str(struct mem_access_result status)
Definition: riscv-013.c:3647
static bool is_command_unsupported(struct target *target, uint32_t command)
Definition: riscv-013.c:918
static int internal_register_write64_progbuf_scratch(struct target *target, struct riscv_program *program, riscv_reg_t value)
This function is used to write a 64-bit value to a register by executing a program.
Definition: riscv-013.c:1457
static int read_memory_progbuf_inner_ensure_forward_progress(struct target *target, const struct riscv_mem_access_args args, uint32_t start_index)
read_memory_progbuf_inner_startup() must be called before calling this function with the address argu...
Definition: riscv-013.c:4236
static struct mem_access_result write_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3855
static int prep_for_register_access(struct target *target, riscv_reg_t *orig_mstatus, enum gdb_regno regno)
Definition: riscv-013.c:1117
static int execute_autofence(struct target *target)
Definition: riscv-013.c:3026
static int dm013_select_target(struct target *target)
Definition: riscv-013.c:655
static struct mem_access_result read_word_from_s1(struct target *target, const struct riscv_mem_access_args args, uint32_t index)
Definition: riscv-013.c:4298
static riscv013_info_t * get_info(const struct target *target)
Definition: riscv-013.c:263
static void decrement_reset_delays_counter(struct target *target, size_t finished_scans)
Definition: riscv-013.c:459
static int read_abstract_arg(struct target *target, riscv_reg_t *value, unsigned int index, unsigned int size_bits)
Definition: riscv-013.c:814
static int riscv013_authdata_write(struct target *target, uint32_t value, unsigned int index)
Definition: riscv-013.c:2183
#define HART_INDEX_UNKNOWN
Definition: riscv-013.c:110
static int riscv013_authdata_read(struct target *target, uint32_t *value, unsigned int index)
Definition: riscv-013.c:2170
static int riscv013_get_hart_state(struct target *target, enum riscv_hart_state *state)
Definition: riscv-013.c:2775
static void set_buffer_and_log_read(const struct riscv_mem_access_args args, uint32_t index, uint64_t value)
Definition: riscv-013.c:4270
static uint32_t abstract_memory_size(unsigned int width)
Definition: riscv-013.c:1007
uint32_t riscv013_access_register_command(struct target *target, uint32_t number, unsigned int size, uint32_t flags)
Definition: riscv-013.c:876
static int register_write_direct(struct target *target, enum gdb_regno number, riscv_reg_t value)
Immediately write the new value to the requested register.
Definition: riscv-013.c:1601
static int internal_register_read64_progbuf_scratch(struct target *target, struct riscv_program *program, riscv_reg_t *value)
This function is used to read a 64-bit value from a register by executing a program.
Definition: riscv-013.c:1357
static int halt_set_dcsr_ebreak(struct target *target)
Definition: riscv-013.c:1708
static int riscv013_halt_prep(struct target *target)
Definition: riscv-013.c:5225
static uint32_t access_memory_command(struct target *target, bool virtual, unsigned int width, bool postincrement, bool is_write)
Definition: riscv-013.c:1029
static int riscv013_clear_abstract_error(struct target *target)
Definition: riscv-013.c:5542
static int write_memory_progbuf_fill_progbuf(struct target *target, uint32_t size)
Definition: riscv-013.c:4993
static target_addr_t sb_read_address(struct target *target)
Definition: riscv-013.c:3160
int riscv013_get_register_buf(struct target *target, uint8_t *value, enum gdb_regno regno)
Definition: riscv-013.c:2361
static struct riscv_debug_reg_ctx get_riscv_debug_reg_ctx(const struct target *target)
Definition: riscv-013.c:351
#define HART_INDEX_MULTIPLE
Definition: riscv-013.c:109
static void reset_learned_delays(struct target *target)
Definition: riscv-013.c:452
static void log_memory_access64(target_addr_t address, uint64_t value, unsigned int size_bytes, bool is_read)
Definition: riscv-013.c:3102
#define CMDERR_NONE
Definition: riscv-013.c:102
static int modify_privilege_for_virt2phys_mode(struct target *target, riscv_reg_t *mstatus, riscv_reg_t *mstatus_old, riscv_reg_t *dcsr, riscv_reg_t *dcsr_old)
Definition: riscv-013.c:3194
static int write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4612
static unsigned int riscv013_get_dmi_address_bits(const struct target *target)
Definition: riscv-013.c:5451
static int riscv013_resume_go(struct target *target)
Definition: riscv-013.c:5317
static struct ac_cache ac_cache_construct(void)
Definition: riscv-013.c:163
static int write_memory_progbuf_teardown(struct target *target)
This function reverts the changes made by write_memory_progbuf_startup()
Definition: riscv-013.c:4856
static void log_memory_access(target_addr_t address, uint32_t *sbvalue, unsigned int size_bytes, bool is_read)
Definition: riscv-013.c:3128
static int read_memory_progbuf_inner_try_to_read(struct target *target, const struct riscv_mem_access_args args, uint32_t *elements_read, uint32_t index, uint32_t loop_count)
Definition: riscv-013.c:4213
#define LIST_OF_MEM_ACCESS_RESULTS
Definition: riscv-013.c:3529
#define LOG_DEBUG_REG(t, r, v)
Definition: riscv-013.c:383
static int sample_memory_bus_v1(struct target *target, struct riscv_sample_buf *buf, const riscv_sample_config_t *config, int64_t until_ms)
Definition: riscv-013.c:2603
static uint32_t set_dmcontrol_hartsel(uint32_t initial, int hart_index)
Definition: riscv-013.c:385
static struct mem_access_result write_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:5052
static int read_memory_progbuf_inner_on_dmi_busy(struct target *target, uint32_t start_index, uint32_t next_start_index, const struct riscv_mem_access_args args)
This function attempts to restore the pipeline after a dmi busy.
Definition: riscv-013.c:4063
static bool check_dbgbase_exists(struct target *target)
Definition: riscv-013.c:534
int riscv013_execute_abstract_command(struct target *target, uint32_t command, uint32_t *cmderr)
Definition: riscv-013.c:737
static void deinit_target(struct target *target)
Definition: riscv-013.c:1773
static int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
Definition: riscv-013.c:1658
static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
Definition: riscv-013.c:3176
static int tick(struct target *target)
Definition: riscv-013.c:2841
static int register_read_abstract(struct target *target, riscv_reg_t *value, enum gdb_regno number)
Definition: riscv-013.c:954
static int read_memory_progbuf_inner_on_ac_busy(struct target *target, uint32_t start_index, uint32_t *elements_read, const struct riscv_mem_access_args args)
This function attempts to restore the pipeline after a busy on abstract access.
Definition: riscv-013.c:4004
enum riscv_debug_reg_ordinal get_cmdtype(uint32_t command)
Definition: riscv-013.c:713
static void log_memory_access128(target_addr_t address, uint64_t value_h, uint64_t value_l, bool is_read)
Definition: riscv-013.c:3090
static unsigned int riscv013_get_progbufsize(const struct target *target)
Definition: riscv-013.c:5070
static COMMAND_HELPER(riscv013_print_info, struct target *target)
Definition: riscv-013.c:2253
static int try_set_vsew(struct target *target, unsigned int *debug_vsew)
Definition: riscv-013.c:2287
static int increase_ac_busy_delay(struct target *target)
Definition: riscv-013.c:589
static int prep_for_vector_access(struct target *target, riscv_reg_t *orig_mstatus, riscv_reg_t *orig_vtype, riscv_reg_t *orig_vl, unsigned int *debug_vl, unsigned int *debug_vsew)
Definition: riscv-013.c:2316
static uint32_t read_memory_progbuf_inner_fill_batch(struct riscv_batch *batch, uint32_t count, uint32_t size)
Definition: riscv-013.c:4190
static int increase_dmi_busy_delay(struct target *target)
Definition: riscv-013.c:440
static bool riscv013_get_impebreak(const struct target *target)
Definition: riscv-013.c:5064
static struct mem_access_result mem_should_skip_sysbus(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3710
static int sample_memory(struct target *target, struct riscv_sample_buf *buf, riscv_sample_config_t *config, int64_t until_ms)
Definition: riscv-013.c:2764
static bool ac_cache_contains(const struct ac_cache *cache, uint32_t command)
Definition: riscv-013.c:201
static void ac_cache_free(struct ac_cache *cache)
Definition: riscv-013.c:172
static int scratch_release(struct target *target, scratch_mem_t *scratch)
Definition: riscv-013.c:1247
static void log_mem_access_result(struct target *target, bool success, enum riscv_mem_access_method method, bool is_read)
Definition: riscv-013.c:3495
static int write_memory_progbuf_try_to_write(struct target *target, target_addr_t *address_p, target_addr_t end_address, uint32_t size, const uint8_t *buffer)
Definition: riscv-013.c:4976
int riscv013_reg_examine_all(struct target *target)
This function assumes target's DM to be initialized (target is able to access DMs registers,...
int riscv013_reg_save(struct target *target, enum gdb_regno regid)
This function is used to save the value of a register in cache.
unsigned int riscv_xlen(const struct target *target)
Definition: riscv.c:6094
struct scan_field select_dbus
Definition: riscv.c:48
bool riscv_supports_extension(const struct target *target, char letter)
Definition: riscv.c:6081
void select_dmi_via_bscan(struct jtag_tap *tap)
Definition: riscv.c:319
int riscv_halt(struct target *target)
Definition: riscv.c:2716
int riscv_get_hart_state(struct target *target, enum riscv_hart_state *state)
Definition: riscv.c:6106
bool riscv_virt2phys_mode_is_hw(const struct target *target)
Definition: riscv.c:144
uint8_t bscan_tunnel_ir_width
Definition: riscv.c:60
int dtmcs_scan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr)
Definition: riscv.c:416
int riscv_openocd_poll(struct target *target)
Definition: riscv.c:4020
int riscv_get_command_timeout_sec(void)
Definition: riscv.c:179
int riscv_enumerate_triggers(struct target *target)
Count triggers, and initialize trigger_count for each hart.
Definition: riscv.c:6250
int riscv_openocd_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv.c:4291
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_INFO(R)
Definition: riscv.h:426
static struct riscv_info * riscv_info(const struct target *target) __attribute__((unused))
Definition: riscv.h:421
#define RISCV013_DTMCS_ABITS_MIN
Definition: riscv.h:128
riscv_mem_access_method
Definition: riscv.h:56
@ RISCV_MEM_ACCESS_SYSBUS
Definition: riscv.h:58
@ RISCV_MEM_ACCESS_PROGBUF
Definition: riscv.h:57
@ RISCV_MEM_ACCESS_ABSTRACT
Definition: riscv.h:59
#define RISCV_MAX_DMS
Definition: riscv.h:23
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
#define RISCV013_DTMCS_ABITS_MAX
Definition: riscv.h:129
@ 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
uint64_t riscv_reg_t
Definition: riscv.h:46
#define RISCV_MAX_HARTS
Definition: riscv.h:20
static bool riscv_mem_access_is_write(const struct riscv_mem_access_args args)
Definition: riscv.h:161
static bool riscv_mem_access_is_read(const struct riscv_mem_access_args args)
Definition: riscv.h:154
static struct riscv_private_config * riscv_private_config(const struct target *target)
Definition: riscv.h:384
yes_no_maybe
Definition: riscv.h:50
@ YNM_YES
Definition: riscv.h:52
@ YNM_MAYBE
Definition: riscv.h:51
@ YNM_NO
Definition: riscv.h:53
uint32_t riscv_insn_t
Definition: riscv.h:47
riscv_halt_reason
Definition: riscv.h:71
@ RISCV_HALT_INTERRUPT
Definition: riscv.h:72
@ RISCV_HALT_SINGLESTEP
Definition: riscv.h:74
@ RISCV_HALT_EBREAK
Definition: riscv.h:73
@ RISCV_HALT_UNKNOWN
Definition: riscv.h:76
@ RISCV_HALT_GROUP
Definition: riscv.h:77
@ RISCV_HALT_TRIGGER
Definition: riscv.h:75
uint64_t riscv_addr_t
Definition: riscv.h:48
#define RISCV_BATCH_ALLOC_SIZE
Definition: riscv.h:38
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
int riscv_reg_write(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:935
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
struct target * target
Definition: rtt/rtt.c:26
size_t size
Definition: riscv-013.c:149
uint32_t * commands
Definition: riscv-013.c:148
int hart_count
Definition: riscv-013.c:118
struct list_head list
Definition: riscv-013.c:113
struct list_head target_list
Definition: riscv-013.c:124
uint32_t base
Definition: riscv-013.c:116
uint32_t progbuf_cache[16]
Definition: riscv-013.c:133
bool was_examined
Definition: riscv-013.c:120
int current_hartid
Definition: riscv-013.c:127
bool abstract_cmd_maybe_busy
Definition: riscv-013.c:139
bool hasel_supported
Definition: riscv-013.c:129
unsigned int abs_chain_position
Definition: riscv-013.c:114
bool was_reset
Definition: riscv-013.c:122
Definition: jtag.h:101
uint8_t * cur_instr
current instruction
Definition: jtag.h:132
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
unsigned int abs_chain_position
Definition: jtag.h:105
bool enabled
Is this TAP currently enabled?
Definition: jtag.h:109
Definition: list.h:41
enum mem_access_result_enum value
Definition: riscv-013.c:3597
struct reg * reg_list
Definition: register.h:147
Definition: register.h:111
uint32_t size
Definition: register.h:132
void * arch_info
Definition: register.h:140
unsigned int datacount
Definition: riscv-013.c:213
int16_t dataaddr
Definition: riscv-013.c:242
bool haltgroup_supported
Definition: riscv-013.c:258
unsigned int hartsellen
Definition: riscv-013.c:245
unsigned int index
Definition: riscv-013.c:209
bool dcsr_ebreak_is_set
Definition: riscv-013.c:255
struct ac_cache ac_not_supported_cache
Definition: riscv-013.c:237
unsigned int abits
Definition: riscv-013.c:211
unsigned int progbufsize
Definition: riscv-013.c:215
uint8_t dataaccess
Definition: riscv-013.c:241
dm013_info_t * dm
Definition: riscv-013.c:248
riscv_addr_t progbuf_address
Definition: riscv-013.c:224
uint8_t datasize
Definition: riscv-013.c:240
size_t read_keys_used
Definition: batch.h:151
size_t used_scans
Definition: batch.h:131
struct riscv_debug_reg_ctx::@124 XLEN
uint32_t increment
Definition: riscv.h:144
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
enum riscv_progbuf_exec_result execution_result
Definition: program.h:31
unsigned int instruction_count
Definition: program.h:27
unsigned int custom_number
Definition: riscv.h:99
unsigned int size
Definition: riscv.h:107
uint8_t * buf
Definition: riscv.h:105
unsigned int used
Definition: riscv.h:106
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
riscv_addr_t debug_address
Definition: riscv-013.c:1179
riscv_addr_t hart_address
Definition: riscv-013.c:1177
struct working_area * area
Definition: riscv-013.c:1180
memory_space_t memory_space
Definition: riscv-013.c:1175
struct list_head list
Definition: riscv-013.c:143
struct target * target
Definition: riscv-013.c:144
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
const char * name
Name of this type of target.
Definition: target_type.h:31
Definition: target.h:119
int32_t coreid
Definition: target.h:123
struct jtag_tap * tap
Definition: target.h:122
bool dbgbase_set
Definition: target.h:184
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
uint32_t dbgbase
Definition: target.h:185
struct reg_cache * reg_cache
Definition: target.h:168
unsigned int smp
Definition: target.h:200
void * arch_info
Definition: target.h:174
bool reset_halt
Definition: target.h:154
target_addr_t address
Definition: target.h:89
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2090
int target_examine_one(struct target *target)
Examine the specified target, letting it perform any Initialisation that requires JTAG access.
Definition: target.c:685
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2148
bool target_has_event_action(const struct target *target, enum target_event event)
Returns true only if the target has a handler for the specified event.
Definition: target.c:4811
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4625
@ DBG_REASON_UNDEFINED
Definition: target.h:80
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:817
static bool target_was_examined(const struct target *target)
Definition: target.h:443
@ TARGET_EVENT_RESET_ASSERT
Definition: target.h:277
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:246
@ TARGET_RESET
Definition: target.h:59
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_UNAVAILABLE
Definition: target.h:61
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
int64_t timeval_ms(void)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
uint64_t target_addr_t
Definition: types.h:279
#define TARGET_PRIxADDR
Definition: types.h:284
static struct ublast_lowlevel_priv info
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t rid[2]
Definition: vdebug.c:15
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22