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 
1441  int res;
1442  uint64_t new_value;
1444  res = fpr_read_progbuf(target, &new_value, number);
1445  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
1446  res = csr_read_progbuf(target, &new_value, number);
1447  } else {
1448  LOG_TARGET_ERROR(target, "Unexpected read of %s via program buffer.",
1450  return ERROR_FAIL;
1451  }
1452  if (res != ERROR_OK)
1453  return res;
1454 
1455  unsigned int size_bits = register_size(target, number);
1456  unsigned int value_bits = sizeof(*value) * CHAR_BIT;
1457  assert(size_bits <= value_bits);
1458  if (size_bits == value_bits || new_value >> size_bits == 0) {
1459  *value = new_value;
1460  return ERROR_OK;
1461  }
1462  LOG_TARGET_ERROR(target, "Value 0x%" PRIx64 " read from register %s"
1463  " exceeds the size of the register (%u bits). This is a HW bug."
1464  " Discarding the value", new_value,
1465  riscv_reg_gdb_regno_name(target, number), size_bits);
1466  return ERROR_FAIL;
1467 }
1468 
1476  struct riscv_program *program, riscv_reg_t value)
1477 {
1478  scratch_mem_t scratch;
1479 
1480  if (scratch_reserve(target, &scratch, program, 8) != ERROR_OK)
1481  return ERROR_FAIL;
1482 
1484  != ERROR_OK) {
1485  scratch_release(target, &scratch);
1486  return ERROR_FAIL;
1487  }
1488  if (scratch_write64(target, &scratch, value) != ERROR_OK) {
1489  scratch_release(target, &scratch);
1490  return ERROR_FAIL;
1491  }
1492  int result = riscv_program_exec(program, target);
1493 
1494  scratch_release(target, &scratch);
1495  return result;
1496 }
1497 
1499  riscv_reg_t value)
1500 {
1501  assert(target->state == TARGET_HALTED);
1502  assert(number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31);
1503  const unsigned int freg = number - GDB_REGNO_FPR0;
1504 
1506  return ERROR_FAIL;
1507 
1508  struct riscv_program program;
1509  riscv_program_init(&program, target);
1510 
1511  if (riscv_supports_extension(target, 'D') && riscv_xlen(target) < 64) {
1512  /* There are no instructions to move all the bits from a register,
1513  * so we need to use some scratch RAM.
1514  */
1515  if (riscv_program_insert(&program, fld(freg, S0, 0)) != ERROR_OK)
1516  return ERROR_FAIL;
1517  return internal_register_write64_progbuf_scratch(target, &program, value);
1518  }
1519 
1521  return ERROR_FAIL;
1522 
1523  if (riscv_program_insert(&program,
1525  fmv_d_x(freg, S0) : fmv_w_x(freg, S0)) != ERROR_OK)
1526  return ERROR_FAIL;
1527 
1528  return riscv_program_exec(&program, target);
1529 }
1530 
1531 static int vtype_write_progbuf(struct target *target, riscv_reg_t value)
1532 {
1533  assert(target->state == TARGET_HALTED);
1534 
1536  return ERROR_FAIL;
1538  return ERROR_FAIL;
1540  return ERROR_FAIL;
1541 
1542  struct riscv_program program;
1543  riscv_program_init(&program, target);
1544  if (riscv_program_insert(&program, csrr(S1, CSR_VL)) != ERROR_OK)
1545  return ERROR_FAIL;
1546  if (riscv_program_insert(&program, vsetvl(ZERO, S1, S0)) != ERROR_OK)
1547  return ERROR_FAIL;
1548 
1549  return riscv_program_exec(&program, target);
1550 }
1551 
1552 static int vl_write_progbuf(struct target *target, riscv_reg_t value)
1553 {
1554  assert(target->state == TARGET_HALTED);
1555 
1557  return ERROR_FAIL;
1559  return ERROR_FAIL;
1561  return ERROR_FAIL;
1562 
1563  struct riscv_program program;
1564  riscv_program_init(&program, target);
1565  if (riscv_program_insert(&program, csrr(S1, CSR_VTYPE)) != ERROR_OK)
1566  return ERROR_FAIL;
1567  if (riscv_program_insert(&program, vsetvl(ZERO, S0, S1)) != ERROR_OK)
1568  return ERROR_FAIL;
1569 
1570  return riscv_program_exec(&program, target);
1571 }
1572 
1574  riscv_reg_t value)
1575 {
1576  assert(target->state == TARGET_HALTED);
1577  assert(number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095);
1578 
1580  return ERROR_FAIL;
1582  return ERROR_FAIL;
1583 
1584  struct riscv_program program;
1585  riscv_program_init(&program, target);
1586  if (riscv_program_csrw(&program, S0, number) != ERROR_OK)
1587  return ERROR_FAIL;
1588 
1589  return riscv_program_exec(&program, target);
1590 }
1591 
1597  riscv_reg_t value)
1598 {
1599  assert(target->state == TARGET_HALTED);
1600 
1602  return fpr_write_progbuf(target, number, value);
1603  else if (number == GDB_REGNO_VTYPE)
1604  return vtype_write_progbuf(target, value);
1605  else if (number == GDB_REGNO_VL)
1606  return vl_write_progbuf(target, value);
1607  else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095)
1608  return csr_write_progbuf(target, number, value);
1609 
1610  LOG_TARGET_ERROR(target, "Unexpected write to %s via program buffer.",
1612  return ERROR_FAIL;
1613 }
1614 
1620  riscv_reg_t value)
1621 {
1622  LOG_TARGET_DEBUG(target, "Writing 0x%" PRIx64 " to %s", value,
1624 
1625  if (target->state != TARGET_HALTED)
1626  return register_write_abstract(target, number, value);
1627 
1628  riscv_reg_t mstatus;
1629  if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1630  return ERROR_FAIL;
1631 
1632  int result = register_write_abstract(target, number, value);
1633 
1634  if (result != ERROR_OK && target->state == TARGET_HALTED)
1635  result = register_write_progbuf(target, number, value);
1636 
1638  return ERROR_FAIL;
1639 
1640  if (result == ERROR_OK)
1642  value);
1643 
1644  return result;
1645 }
1646 
1648 static int register_read_direct(struct target *target, riscv_reg_t *value,
1649  enum gdb_regno number)
1650 {
1652 
1653  if (target->state != TARGET_HALTED)
1654  return register_read_abstract(target, value, number);
1655 
1656  riscv_reg_t mstatus;
1657 
1658  if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1659  return ERROR_FAIL;
1660 
1661  int result = register_read_abstract(target, value, number);
1662 
1663  if (result != ERROR_OK && target->state == TARGET_HALTED)
1664  result = register_read_progbuf(target, value, number);
1665 
1667  return ERROR_FAIL;
1668 
1669  if (result == ERROR_OK)
1671  *value);
1672 
1673  return result;
1674 }
1675 
1676 static int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
1677 {
1678  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1679  while (1) {
1680  uint32_t value;
1681  if (dmstatus_read(target, &value, false) != ERROR_OK)
1682  return ERROR_FAIL;
1683  if (dmstatus)
1684  *dmstatus = value;
1685  if (!get_field(value, DM_DMSTATUS_AUTHBUSY))
1686  break;
1687  if (timeval_ms() > then) {
1688  LOG_TARGET_ERROR(target, "Timed out after %ds waiting for authbusy to go low (dmstatus=0x%x). "
1689  "Increase the timeout with riscv set_command_timeout_sec.",
1691  value);
1692  return ERROR_FAIL;
1693  }
1694  }
1695 
1696  return ERROR_OK;
1697 }
1698 
1699 static int set_dcsr_ebreak(struct target *target, bool step)
1700 {
1701  LOG_TARGET_DEBUG(target, "Set dcsr.ebreak*");
1702 
1704  return ERROR_FAIL;
1705 
1707  riscv_reg_t original_dcsr, dcsr;
1708  /* We want to twiddle some bits in the debug CSR so debugging works. */
1709  if (riscv_reg_get(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
1710  return ERROR_FAIL;
1711  original_dcsr = dcsr;
1712  dcsr = set_field(dcsr, CSR_DCSR_STEP, step);
1713  const struct riscv_private_config * const config = riscv_private_config(target);
1714  dcsr = set_field(dcsr, CSR_DCSR_EBREAKM, config->dcsr_ebreak_fields[RISCV_MODE_M]);
1715  dcsr = set_field(dcsr, CSR_DCSR_EBREAKS, config->dcsr_ebreak_fields[RISCV_MODE_S]);
1716  dcsr = set_field(dcsr, CSR_DCSR_EBREAKU, config->dcsr_ebreak_fields[RISCV_MODE_U]);
1717  dcsr = set_field(dcsr, CSR_DCSR_EBREAKVS, config->dcsr_ebreak_fields[RISCV_MODE_VS]);
1718  dcsr = set_field(dcsr, CSR_DCSR_EBREAKVU, config->dcsr_ebreak_fields[RISCV_MODE_VU]);
1719  if (dcsr != original_dcsr &&
1721  return ERROR_FAIL;
1722  info->dcsr_ebreak_is_set = true;
1723  return ERROR_OK;
1724 }
1725 
1727 {
1728  RISCV_INFO(r);
1730  LOG_TARGET_DEBUG(target, "Halt to set DCSR.ebreak*");
1731 
1732  /* Remove this hart from the halt group. This won't work on all targets
1733  * because the debug spec allows halt groups to be hard-coded, but I
1734  * haven't actually encountered those in the wild yet.
1735  *
1736  * There is a possible race condition when another hart halts, and
1737  * this one is expected to also halt because it's supposed to be in the
1738  * same halt group. Or when this hart is halted when that happens.
1739  *
1740  * A better solution might be to leave the halt groups alone, and track
1741  * why we're halting when a halt occurs. When there are halt groups,
1742  * that leads to extra halting if not all harts need to set dcsr.ebreak
1743  * at the same time. It also makes for more complicated code.
1744  *
1745  * The perfect solution would be Quick Access, but I'm not aware of any
1746  * hardware that implements it.
1747  *
1748  * We don't need a perfect solution, because we only get here when a
1749  * hart spontaneously resets, or when it powers down and back up again.
1750  * Those are both relatively rare. (At least I hope so. Maybe some
1751  * design just powers each hart down for 90ms out of every 100ms)
1752  */
1753 
1754 
1755  if (info->haltgroup_supported) {
1756  bool supported;
1757  if (set_group(target, &supported, 0, HALT_GROUP) != ERROR_OK)
1758  return ERROR_FAIL;
1759  if (!supported)
1760  LOG_TARGET_ERROR(target, "Couldn't place hart in halt group 0. "
1761  "Some harts may be unexpectedly halted.");
1762  }
1763 
1764  int result = ERROR_OK;
1765 
1766  r->prepped = true;
1767  if (riscv013_halt_go(target) != ERROR_OK ||
1768  set_dcsr_ebreak(target, false) != ERROR_OK ||
1770  result = ERROR_FAIL;
1771  } else {
1774  }
1775 
1776  /* Add it back to the halt group. */
1777  if (info->haltgroup_supported) {
1778  bool supported;
1779  if (set_group(target, &supported, target->smp, HALT_GROUP) != ERROR_OK)
1780  return ERROR_FAIL;
1781  if (!supported)
1782  LOG_TARGET_ERROR(target, "Couldn't place hart back in halt group %d. "
1783  "Some harts may be unexpectedly halted.", target->smp);
1784  }
1785 
1786  return result;
1787 }
1788 
1789 /*** OpenOCD target functions. ***/
1790 
1791 static void deinit_target(struct target *target)
1792 {
1793  LOG_TARGET_DEBUG(target, "Deinitializing target.");
1794  struct riscv_info *info = target->arch_info;
1795  if (!info)
1796  return;
1797 
1798  riscv013_info_t *vsinfo = info->version_specific;
1799  if (vsinfo)
1801 
1803 
1804  free(info->version_specific);
1805  /* TODO: free register arch_info */
1806  info->version_specific = NULL;
1807 }
1808 
1809 static int set_group(struct target *target, bool *supported, unsigned int group,
1810  enum grouptype grouptype)
1811 {
1812  uint32_t write_val = DM_DMCS2_HGWRITE;
1813  assert(group <= 31);
1814  write_val = set_field(write_val, DM_DMCS2_GROUP, group);
1815  write_val = set_field(write_val, DM_DMCS2_GROUPTYPE, (grouptype == HALT_GROUP) ? 0 : 1);
1816  if (dm_write(target, DM_DMCS2, write_val) != ERROR_OK)
1817  return ERROR_FAIL;
1818  uint32_t read_val;
1819  if (dm_read(target, &read_val, DM_DMCS2) != ERROR_OK)
1820  return ERROR_FAIL;
1821  if (supported)
1822  *supported = (get_field(read_val, DM_DMCS2_GROUP) == group);
1823  return ERROR_OK;
1824 }
1825 
1827 {
1828  dm013_info_t *dm = get_dm(target);
1829  if (!dm)
1830  return ERROR_FAIL;
1831  if (!dm->abstract_cmd_maybe_busy)
1832  /* The previous abstract command ended correctly
1833  * and busy was cleared. No need to do anything. */
1834  return ERROR_OK;
1835 
1836  /* The previous abstract command timed out and abstractcs.busy
1837  * may have remained set. Wait for it to get cleared. */
1838  uint32_t abstractcs;
1839  int result = wait_for_idle(target, &abstractcs);
1840  if (result != ERROR_OK)
1841  return result;
1842  LOG_DEBUG_REG(target, DM_ABSTRACTCS, abstractcs);
1843  return ERROR_OK;
1844 }
1845 
1846 static int reset_dm(struct target *target)
1847 {
1848  /* TODO: This function returns an error when a DMI operation fails.
1849  * However, [3.14.2. Debug Module Control] states:
1850  * > 0 (inactive): ... Any accesses to the module may fail.
1851  *
1852  * Ignoring failures may introduce incompatibility with 0.13.
1853  * See https://github.com/riscv/riscv-debug-spec/issues/1021
1854  */
1855  dm013_info_t *dm = get_dm(target);
1856  assert(dm && "DM is expected to be already allocated.");
1857  assert(!dm->was_reset && "Attempt to reset an already-reset debug module.");
1858  /* `dmcontrol.hartsel` should be read first, in order not to
1859  * change it when requesting the reset, since changing it
1860  * without checking that `abstractcs.busy` is low is
1861  * prohibited.
1862  */
1863  uint32_t dmcontrol;
1864  int result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1865  if (result != ERROR_OK)
1866  return result;
1867 
1868  if (get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE)) {
1869  /* `dmcontrol.hartsel` is not changed. */
1870  dmcontrol = (dmcontrol & DM_DMCONTROL_HARTSELLO) |
1871  (dmcontrol & DM_DMCONTROL_HARTSELHI);
1872  LOG_TARGET_DEBUG(target, "Initiating DM reset.");
1873  result = dm_write(target, DM_DMCONTROL, dmcontrol);
1874  if (result != ERROR_OK)
1875  return result;
1876 
1877  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1878  LOG_TARGET_DEBUG(target, "Waiting for the DM to acknowledge reset.");
1879  do {
1880  result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1881  if (result != ERROR_OK)
1882  return result;
1883 
1884  if (timeval_ms() > then) {
1885  LOG_TARGET_ERROR(target, "DM didn't acknowledge reset in %d s. "
1886  "Increase the timeout with 'riscv set_command_timeout_sec'.",
1888  return ERROR_TIMEOUT_REACHED;
1889  }
1890  } while (get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE));
1891  LOG_TARGET_DEBUG(target, "DM reset initiated.");
1892  }
1893 
1894  LOG_TARGET_DEBUG(target, "Activating the DM.");
1896  if (result != ERROR_OK)
1897  return result;
1898 
1899  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1900  LOG_TARGET_DEBUG(target, "Waiting for the DM to come out of reset.");
1901  do {
1902  result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1903  if (result != ERROR_OK)
1904  return result;
1905 
1906  if (timeval_ms() > then) {
1907  LOG_TARGET_ERROR(target, "Debug Module did not become active in %d s. "
1908  "Increase the timeout with 'riscv set_command_timeout_sec'.",
1910  return ERROR_TIMEOUT_REACHED;
1911  }
1912  } while (!get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE));
1913 
1914  LOG_TARGET_DEBUG(target, "DM successfully reset.");
1915  dm->was_reset = true;
1916  return ERROR_OK;
1917 }
1918 
1919 static int examine_dm(struct target *target)
1920 {
1921  dm013_info_t *dm = get_dm(target);
1922  if (!dm)
1923  return ERROR_FAIL;
1924  if (dm->was_examined)
1925  return ERROR_OK;
1926 
1927  int result = ERROR_FAIL;
1928 
1929  if (dm->was_reset) {
1930  /* The DM was already reset when examining a different hart.
1931  * No need to reset it again. But for safety, assume that an abstract
1932  * command might be in progress at the moment.
1933  */
1934  dm->abstract_cmd_maybe_busy = true;
1935  } else {
1936  result = reset_dm(target);
1937  if (result != ERROR_OK)
1938  return result;
1939  }
1940 
1942 
1946  if (result != ERROR_OK)
1947  return result;
1948 
1949  uint32_t dmcontrol;
1950  result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1951  if (result != ERROR_OK)
1952  return result;
1953 
1954  dm->hasel_supported = get_field(dmcontrol, DM_DMCONTROL_HASEL);
1955 
1956  uint32_t hartsel =
1957  (get_field(dmcontrol, DM_DMCONTROL_HARTSELHI) <<
1959  get_field(dmcontrol, DM_DMCONTROL_HARTSELLO);
1960 
1961  /* Before doing anything else we must first enumerate the harts. */
1962  const int max_hart_count = MIN(RISCV_MAX_HARTS, hartsel + 1);
1963  if (dm->hart_count < 0) {
1964  for (int i = 0; i < max_hart_count; ++i) {
1965  /* TODO: This is extremely similar to
1966  * riscv013_get_hart_state().
1967  * It would be best to reuse the code.
1968  */
1969  result = dm013_select_hart(target, i);
1970  if (result != ERROR_OK)
1971  return result;
1972 
1973  uint32_t s;
1974  result = dmstatus_read(target, &s, /*authenticated*/ true);
1975  if (result != ERROR_OK)
1976  return result;
1977 
1979  break;
1980 
1981  dm->hart_count = i + 1;
1982 
1985  /* If `abstractcs.busy` is set, debugger should not
1986  * change `hartsel`.
1987  */
1988  result = wait_for_idle_if_needed(target);
1989  if (result != ERROR_OK)
1990  return result;
1991  dmcontrol = set_dmcontrol_hartsel(dmcontrol, i);
1992  result = dm_write(target, DM_DMCONTROL, dmcontrol);
1993  if (result != ERROR_OK)
1994  return result;
1995  }
1996  }
1997  LOG_TARGET_DEBUG(target, "Detected %d harts.", dm->hart_count);
1998  }
1999 
2000  if (dm->hart_count <= 0) {
2001  LOG_TARGET_ERROR(target, "No harts found!");
2002  return ERROR_FAIL;
2003  }
2004 
2005  dm->was_examined = true;
2006  return ERROR_OK;
2007 }
2008 
2009 static int examine(struct target *target)
2010 {
2011  /* We reset target state in case if something goes wrong during examine:
2012  * DTM/DM scans could fail or hart may fail to halt. */
2015 
2016  /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
2017  LOG_TARGET_DEBUG(target, "dbgbase=0x%x", target->dbgbase);
2018 
2019  uint32_t dtmcontrol;
2020  if (dtmcs_scan(target->tap, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
2021  LOG_TARGET_ERROR(target, "Could not scan dtmcontrol. Check JTAG connectivity/board power.");
2022  return ERROR_FAIL;
2023  }
2024 
2025  LOG_TARGET_DEBUG(target, "dtmcontrol=0x%x", dtmcontrol);
2026  LOG_DEBUG_REG(target, DTM_DTMCS, dtmcontrol);
2027 
2028  if (get_field(dtmcontrol, DTM_DTMCS_VERSION) != 1) {
2029  LOG_TARGET_ERROR(target, "Unsupported DTM version %" PRIu32 ". (dtmcontrol=0x%" PRIx32 ")",
2030  get_field32(dtmcontrol, DTM_DTMCS_VERSION), dtmcontrol);
2031  return ERROR_FAIL;
2032  }
2033 
2035 
2036  info->index = target->coreid;
2037  info->abits = get_field(dtmcontrol, DTM_DTMCS_ABITS);
2038  info->dtmcs_idle = get_field(dtmcontrol, DTM_DTMCS_IDLE);
2039 
2040  if (info->abits > RISCV013_DTMCS_ABITS_MAX) {
2041  /* Max. address width given by the debug specification is exceeded */
2042  LOG_TARGET_ERROR(target, "The target's debug bus (DMI) address width exceeds "
2043  "the maximum:");
2044  LOG_TARGET_ERROR(target, " found dtmcs.abits = %d; maximum is abits = %d.",
2045  info->abits, RISCV013_DTMCS_ABITS_MAX);
2046  return ERROR_FAIL;
2047  }
2048 
2049  if (info->abits == 0) {
2051  "dtmcs.abits is zero. Check JTAG connectivity/board power");
2052  return ERROR_FAIL;
2053  }
2054  if (info->abits < RISCV013_DTMCS_ABITS_MIN) {
2055  /* The requirement for minimum DMI address width of 7 bits is part of
2056  * the RISC-V Debug spec since Jan-20-2017 (commit 03df6ee7). However,
2057  * implementations exist that implement narrower DMI address. For example
2058  * Spike as of Q1/2025 uses dmi.abits = 6.
2059  *
2060  * For that reason, warn the user but continue.
2061  */
2062  LOG_TARGET_WARNING(target, "The target's debug bus (DMI) address width is "
2063  "lower than the minimum:");
2064  LOG_TARGET_WARNING(target, " found dtmcs.abits = %d; minimum is abits = %d.",
2065  info->abits, RISCV013_DTMCS_ABITS_MIN);
2066  }
2067 
2068  if (!check_dbgbase_exists(target)) {
2069  LOG_TARGET_ERROR(target, "Could not find debug module with DMI base address (dbgbase) = 0x%x", target->dbgbase);
2070  return ERROR_FAIL;
2071  }
2072 
2073  int result = examine_dm(target);
2074  if (result != ERROR_OK)
2075  return result;
2076 
2077  result = dm013_select_target(target);
2078  if (result != ERROR_OK)
2079  return result;
2080 
2081  /* We're here because we're uncertain about the state of the target. That
2082  * includes our progbuf cache. */
2084 
2085  uint32_t dmstatus;
2086  if (dmstatus_read(target, &dmstatus, false) != ERROR_OK)
2087  return ERROR_FAIL;
2088  LOG_TARGET_DEBUG(target, "dmstatus: 0x%08x", dmstatus);
2089  int dmstatus_version = get_field(dmstatus, DM_DMSTATUS_VERSION);
2090  if (dmstatus_version != 2 && dmstatus_version != 3) {
2091  /* Error was already printed out in dmstatus_read(). */
2092  return ERROR_FAIL;
2093  }
2094 
2095  uint32_t hartinfo;
2096  if (dm_read(target, &hartinfo, DM_HARTINFO) != ERROR_OK)
2097  return ERROR_FAIL;
2098 
2099  info->datasize = get_field(hartinfo, DM_HARTINFO_DATASIZE);
2100  info->dataaccess = get_field(hartinfo, DM_HARTINFO_DATAACCESS);
2101  info->dataaddr = get_field(hartinfo, DM_HARTINFO_DATAADDR);
2102 
2103  if (!get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED)) {
2104  LOG_TARGET_ERROR(target, "Debugger is not authenticated to target Debug Module. "
2105  "(dmstatus=0x%x). Use `riscv authdata_read` and "
2106  "`riscv authdata_write` commands to authenticate.", dmstatus);
2107  return ERROR_FAIL;
2108  }
2109 
2110  if (dm_read(target, &info->sbcs, DM_SBCS) != ERROR_OK)
2111  return ERROR_FAIL;
2112 
2113  /* Check that abstract data registers are accessible. */
2114  uint32_t abstractcs;
2115  if (dm_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
2116  return ERROR_FAIL;
2117  info->datacount = get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT);
2118  info->progbufsize = get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE);
2119 
2120  LOG_TARGET_INFO(target, "datacount=%d progbufsize=%d",
2121  info->datacount, info->progbufsize);
2122 
2123  info->impebreak = get_field(dmstatus, DM_DMSTATUS_IMPEBREAK);
2124 
2125  if (!has_sufficient_progbuf(target, 2)) {
2126  LOG_TARGET_WARNING(target, "We won't be able to execute fence instructions on this "
2127  "target. Memory may not always appear consistent. "
2128  "(progbufsize=%d, impebreak=%d)", info->progbufsize,
2129  info->impebreak);
2130  }
2131 
2132  /* Don't call any riscv_* functions until after we've counted the number of
2133  * cores and initialized registers. */
2134 
2135  enum riscv_hart_state state_at_examine_start;
2136  if (riscv_get_hart_state(target, &state_at_examine_start) != ERROR_OK)
2137  return ERROR_FAIL;
2138 
2139  RISCV_INFO(r);
2140  const bool hart_halted_at_examine_start = state_at_examine_start == RISCV_STATE_HALTED;
2141  if (!hart_halted_at_examine_start) {
2142  r->prepped = true;
2143  if (riscv013_halt_go(target) != ERROR_OK) {
2144  LOG_TARGET_ERROR(target, "Fatal: Hart %d failed to halt during %s",
2145  info->index, __func__);
2146  return ERROR_FAIL;
2147  }
2148  }
2149 
2151  target->debug_reason = hart_halted_at_examine_start ? DBG_REASON_UNDEFINED : DBG_REASON_DBGRQ;
2152 
2153  result = riscv013_reg_examine_all(target);
2154  if (result != ERROR_OK)
2155  return result;
2156 
2157  if (set_dcsr_ebreak(target, false) != ERROR_OK)
2158  return ERROR_FAIL;
2159 
2160  if (state_at_examine_start == RISCV_STATE_RUNNING) {
2164  } else if (state_at_examine_start == RISCV_STATE_HALTED) {
2167  }
2168 
2169  if (target->smp) {
2170  if (set_group(target, &info->haltgroup_supported, target->smp, HALT_GROUP) != ERROR_OK)
2171  return ERROR_FAIL;
2172  if (info->haltgroup_supported)
2173  LOG_TARGET_INFO(target, "Core %d made part of halt group %d.", info->index,
2174  target->smp);
2175  else
2176  LOG_TARGET_INFO(target, "Core %d could not be made part of halt group %d.",
2177  info->index, target->smp);
2178  }
2179 
2180  /* Some regression suites rely on seeing 'Examined RISC-V core' to know
2181  * when they can connect with gdb/telnet.
2182  * We will need to update those suites if we want to change that text. */
2183  LOG_TARGET_INFO(target, "Examined RISC-V core");
2184  LOG_TARGET_INFO(target, " XLEN=%d, misa=0x%" PRIx64, r->xlen, r->misa);
2185  return ERROR_OK;
2186 }
2187 
2188 static int riscv013_authdata_read(struct target *target, uint32_t *value, unsigned int index)
2189 {
2190  if (index > 0) {
2191  LOG_TARGET_ERROR(target, "Spec 0.13 only has a single authdata register.");
2192  return ERROR_FAIL;
2193  }
2194 
2196  return ERROR_FAIL;
2197 
2198  return dm_read(target, value, DM_AUTHDATA);
2199 }
2200 
2201 static int riscv013_authdata_write(struct target *target, uint32_t value, unsigned int index)
2202 {
2203  if (index > 0) {
2204  LOG_TARGET_ERROR(target, "Spec 0.13 only has a single authdata register.");
2205  return ERROR_FAIL;
2206  }
2207 
2208  uint32_t before, after;
2209  if (wait_for_authbusy(target, &before) != ERROR_OK)
2210  return ERROR_FAIL;
2211 
2212  dm_write(target, DM_AUTHDATA, value);
2213 
2214  if (wait_for_authbusy(target, &after) != ERROR_OK)
2215  return ERROR_FAIL;
2216 
2217  if (!get_field(before, DM_DMSTATUS_AUTHENTICATED) &&
2219  LOG_TARGET_INFO(target, "authdata_write resulted in successful authentication");
2220  int result = ERROR_OK;
2221  dm013_info_t *dm = get_dm(target);
2222  if (!dm)
2223  return ERROR_FAIL;
2224  target_list_t *entry;
2225  list_for_each_entry(entry, &dm->target_list, list) {
2226  if (target_examine_one(entry->target) != ERROR_OK)
2227  result = ERROR_FAIL;
2228  }
2229  return result;
2230  }
2231 
2232  return ERROR_OK;
2233 }
2234 
2235 /* Try to find out the widest memory access size depending on the selected memory access methods. */
2236 static unsigned int riscv013_data_bits(struct target *target)
2237 {
2239  RISCV_INFO(r);
2240 
2241  for (unsigned int i = 0; i < r->num_enabled_mem_access_methods; i++) {
2242  enum riscv_mem_access_method method = r->mem_access_methods[i];
2243 
2244  if (method == RISCV_MEM_ACCESS_PROGBUF) {
2246  return riscv_xlen(target);
2247  } else if (method == RISCV_MEM_ACCESS_SYSBUS) {
2248  if (get_field(info->sbcs, DM_SBCS_SBACCESS128))
2249  return 128;
2250  if (get_field(info->sbcs, DM_SBCS_SBACCESS64))
2251  return 64;
2252  if (get_field(info->sbcs, DM_SBCS_SBACCESS32))
2253  return 32;
2254  if (get_field(info->sbcs, DM_SBCS_SBACCESS16))
2255  return 16;
2256  if (get_field(info->sbcs, DM_SBCS_SBACCESS8))
2257  return 8;
2258  } else if (method == RISCV_MEM_ACCESS_ABSTRACT) {
2259  /* TODO: Once there is a spec for discovering abstract commands, we can
2260  * take those into account as well. For now we assume abstract commands
2261  * support XLEN-wide accesses. */
2262  return riscv_xlen(target);
2263  } else {
2264  assert(false);
2265  }
2266  }
2267  LOG_TARGET_ERROR(target, "Unable to determine supported data bits on this target. Assuming 32 bits.");
2268  return 32;
2269 }
2270 
2271 static COMMAND_HELPER(riscv013_print_info, struct target *target)
2272 {
2274 
2275  /* Abstract description. */
2276  riscv_print_info_line(CMD, "target", "memory.read_while_running8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2277  riscv_print_info_line(CMD, "target", "memory.write_while_running8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2278  riscv_print_info_line(CMD, "target", "memory.read_while_running16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2279  riscv_print_info_line(CMD, "target", "memory.write_while_running16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2280  riscv_print_info_line(CMD, "target", "memory.read_while_running32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2281  riscv_print_info_line(CMD, "target", "memory.write_while_running32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2282  riscv_print_info_line(CMD, "target", "memory.read_while_running64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2283  riscv_print_info_line(CMD, "target", "memory.write_while_running64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2284  riscv_print_info_line(CMD, "target", "memory.read_while_running128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2285  riscv_print_info_line(CMD, "target", "memory.write_while_running128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2286 
2287  /* Lower level description. */
2288  riscv_print_info_line(CMD, "dm", "abits", info->abits);
2289  riscv_print_info_line(CMD, "dm", "progbufsize", info->progbufsize);
2290  riscv_print_info_line(CMD, "dm", "sbversion", get_field(info->sbcs, DM_SBCS_SBVERSION));
2291  riscv_print_info_line(CMD, "dm", "sbasize", get_field(info->sbcs, DM_SBCS_SBASIZE));
2292  riscv_print_info_line(CMD, "dm", "sbaccess128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2293  riscv_print_info_line(CMD, "dm", "sbaccess64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2294  riscv_print_info_line(CMD, "dm", "sbaccess32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2295  riscv_print_info_line(CMD, "dm", "sbaccess16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2296  riscv_print_info_line(CMD, "dm", "sbaccess8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2297 
2298  uint32_t dmstatus;
2299  if (dmstatus_read(target, &dmstatus, false) == ERROR_OK)
2300  riscv_print_info_line(CMD, "dm", "authenticated", get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED));
2301 
2302  return 0;
2303 }
2304 
2305 static int try_set_vsew(struct target *target, unsigned int *debug_vsew)
2306 {
2307  RISCV_INFO(r);
2308  unsigned int encoded_vsew =
2309  (riscv_xlen(target) == 64 && r->vsew64_supported != YNM_NO) ? 3 : 2;
2310 
2311  /* Set standard element width to match XLEN, for vmv instruction to move
2312  * the least significant bits into a GPR.
2313  */
2314  if (riscv_reg_write(target, GDB_REGNO_VTYPE, encoded_vsew << 3) != ERROR_OK)
2315  return ERROR_FAIL;
2316 
2317  if (encoded_vsew == 3 && r->vsew64_supported == YNM_MAYBE) {
2318  /* Check that it's supported. */
2319  riscv_reg_t vtype;
2320 
2321  if (riscv_reg_get(target, &vtype, GDB_REGNO_VTYPE) != ERROR_OK)
2322  return ERROR_FAIL;
2323  if (vtype >> (riscv_xlen(target) - 1)) {
2324  r->vsew64_supported = YNM_NO;
2325  /* Try again. */
2326  return try_set_vsew(target, debug_vsew);
2327  }
2328  r->vsew64_supported = YNM_YES;
2329  }
2330  *debug_vsew = encoded_vsew == 3 ? 64 : 32;
2331  return ERROR_OK;
2332 }
2333 
2335  riscv_reg_t *orig_mstatus, riscv_reg_t *orig_vtype, riscv_reg_t *orig_vl,
2336  unsigned int *debug_vl, unsigned int *debug_vsew)
2337 {
2338  assert(orig_mstatus);
2339  assert(orig_vtype);
2340  assert(orig_vl);
2341  assert(debug_vl);
2342  assert(debug_vsew);
2343 
2344  RISCV_INFO(r);
2345  if (target->state != TARGET_HALTED) {
2347  "Unable to access vector register: target not halted");
2348  return ERROR_TARGET_NOT_HALTED;
2349  }
2350  if (prep_for_register_access(target, orig_mstatus, GDB_REGNO_VL) != ERROR_OK)
2351  return ERROR_FAIL;
2352 
2353  /* Save vtype and vl. */
2354  if (riscv_reg_get(target, orig_vtype, GDB_REGNO_VTYPE) != ERROR_OK)
2355  return ERROR_FAIL;
2356  if (riscv_reg_get(target, orig_vl, GDB_REGNO_VL) != ERROR_OK)
2357  return ERROR_FAIL;
2358 
2359  if (try_set_vsew(target, debug_vsew) != ERROR_OK)
2360  return ERROR_FAIL;
2361  /* Set the number of elements to be updated with results from a vector
2362  * instruction, for the vslide1down instruction.
2363  * Set it so the entire V register is updated. */
2364  *debug_vl = DIV_ROUND_UP(r->vlenb * 8, *debug_vsew);
2365  return riscv_reg_write(target, GDB_REGNO_VL, *debug_vl);
2366 }
2367 
2369  riscv_reg_t mstatus, riscv_reg_t vtype, riscv_reg_t vl)
2370 {
2371  /* Restore vtype and vl. */
2373  return ERROR_FAIL;
2375  return ERROR_FAIL;
2377 }
2378 
2379 int riscv013_get_register_buf(struct target *target, uint8_t *value,
2380  enum gdb_regno regno)
2381 {
2382  assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
2383 
2385  return ERROR_FAIL;
2386 
2387  riscv_reg_t mstatus, vtype, vl;
2388  unsigned int debug_vl, debug_vsew;
2389 
2390  if (prep_for_vector_access(target, &mstatus, &vtype, &vl,
2391  &debug_vl, &debug_vsew) != ERROR_OK)
2392  return ERROR_FAIL;
2393 
2395  return ERROR_FAIL;
2396 
2397  unsigned int vnum = regno - GDB_REGNO_V0;
2398 
2399  int result = ERROR_OK;
2400  for (unsigned int i = 0; i < debug_vl; i++) {
2401  /* Can't reuse the same program because riscv_program_exec() adds
2402  * ebreak to the end every time. */
2403  struct riscv_program program;
2404  riscv_program_init(&program, target);
2405  riscv_program_insert(&program, vmv_x_s(S0, vnum));
2406  riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
2407 
2408  /* Executing the program might result in an exception if there is some
2409  * issue with the vector implementation/instructions we're using. If that
2410  * happens, attempt to restore as usual. We may have clobbered the
2411  * vector register we tried to read already.
2412  * For other failures, we just return error because things are probably
2413  * so messed up that attempting to restore isn't going to help. */
2414  result = riscv_program_exec(&program, target);
2415  if (result == ERROR_OK) {
2416  riscv_reg_t v;
2418  return ERROR_FAIL;
2419  buf_set_u64(value, debug_vsew * i, debug_vsew, v);
2420  } else {
2422  "Failed to execute vmv/vslide1down while reading %s",
2424  break;
2425  }
2426  }
2427 
2428  if (cleanup_after_vector_access(target, mstatus, vtype, vl) != ERROR_OK)
2429  return ERROR_FAIL;
2430 
2431  return result;
2432 }
2433 
2435  const uint8_t *value)
2436 {
2437  assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
2438 
2440  return ERROR_FAIL;
2441 
2442  riscv_reg_t mstatus, vtype, vl;
2443  unsigned int debug_vl, debug_vsew;
2444 
2445  if (prep_for_vector_access(target, &mstatus, &vtype, &vl,
2446  &debug_vl, &debug_vsew) != ERROR_OK)
2447  return ERROR_FAIL;
2448 
2450  return ERROR_FAIL;
2451 
2452  unsigned int vnum = regno - GDB_REGNO_V0;
2453 
2454  struct riscv_program program;
2455  riscv_program_init(&program, target);
2456  riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
2457  int result = ERROR_OK;
2458  for (unsigned int i = 0; i < debug_vl; i++) {
2460  buf_get_u64(value, debug_vsew * i, debug_vsew)) != ERROR_OK)
2461  return ERROR_FAIL;
2462  result = riscv_program_exec(&program, target);
2463  if (result != ERROR_OK)
2464  break;
2465  }
2466 
2467  if (cleanup_after_vector_access(target, mstatus, vtype, vl) != ERROR_OK)
2468  return ERROR_FAIL;
2469 
2470  return result;
2471 }
2472 
2473 static uint32_t sb_sbaccess(unsigned int size_bytes)
2474 {
2475  switch (size_bytes) {
2476  case 1:
2477  return set_field(0, DM_SBCS_SBACCESS, 0);
2478  case 2:
2479  return set_field(0, DM_SBCS_SBACCESS, 1);
2480  case 4:
2481  return set_field(0, DM_SBCS_SBACCESS, 2);
2482  case 8:
2483  return set_field(0, DM_SBCS_SBACCESS, 3);
2484  case 16:
2485  return set_field(0, DM_SBCS_SBACCESS, 4);
2486  }
2487  assert(0);
2488  return 0;
2489 }
2490 
2491 static unsigned int get_sbaadress_reg_count(const struct target *target)
2492 {
2494  const unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2495  return DIV_ROUND_UP(sbasize, 32);
2496 }
2497 
2498 static void batch_fill_sb_write_address(const struct target *target,
2499  struct riscv_batch *batch, target_addr_t address,
2500  enum riscv_scan_delay_class sbaddr0_delay)
2501 {
2502  /* There currently is no support for >64-bit addresses in OpenOCD. */
2503  assert(sizeof(target_addr_t) == sizeof(uint64_t));
2504  const uint32_t addresses[] = {DM_SBADDRESS0, DM_SBADDRESS1, DM_SBADDRESS2, DM_SBADDRESS3};
2505  const uint32_t values[] = {(uint32_t)address, (uint32_t)(address >> 32), 0, 0};
2506  const unsigned int reg_count = get_sbaadress_reg_count(target);
2507  assert(reg_count > 0);
2508  assert(reg_count <= ARRAY_SIZE(addresses));
2509  assert(ARRAY_SIZE(addresses) == ARRAY_SIZE(values));
2510 
2511  for (unsigned int i = reg_count - 1; i > 0; --i)
2512  riscv_batch_add_dm_write(batch, addresses[i], values[i], /* read back */ true,
2514  riscv_batch_add_dm_write(batch, addresses[0], values[0], /* read back */ true,
2515  sbaddr0_delay);
2516 }
2517 
2519  enum riscv_scan_delay_class sbaddr0_delay)
2520 {
2521  struct riscv_batch *batch = riscv_batch_alloc(target,
2523  batch_fill_sb_write_address(target, batch, address, sbaddr0_delay);
2524  const int res = batch_run_timeout(target, batch);
2525  riscv_batch_free(batch);
2526  return res;
2527 }
2528 
2529 static int batch_run(struct target *target, struct riscv_batch *batch)
2530 {
2531  RISCV_INFO(r);
2533  select_dmi(target->tap);
2534  riscv_batch_add_nop(batch);
2535  const int result = riscv_batch_run_from(batch, 0, &info->learned_delays,
2536  /*resets_delays*/ r->reset_delays_wait >= 0,
2537  r->reset_delays_wait);
2538  if (result != ERROR_OK)
2539  return result;
2540  /* TODO: To use `riscv_batch_finished_scans()` here, it is needed for
2541  * all scans to not discard input, meaning
2542  * "riscv_batch_add_dm_write(..., false)" should not be used. */
2543  const size_t finished_scans = batch->used_scans;
2544  decrement_reset_delays_counter(target, finished_scans);
2545  if (riscv_batch_was_batch_busy(batch))
2547  return ERROR_OK;
2548 }
2549 
2550 /* It is expected that during creation of the batch
2551  * "riscv_batch_add_dm_write(..., false)" was not used.
2552  */
2553 static int batch_run_timeout(struct target *target, struct riscv_batch *batch)
2554 {
2556  select_dmi(target->tap);
2557  riscv_batch_add_nop(batch);
2558 
2559  size_t finished_scans = 0;
2560  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
2561  const unsigned int old_base_delay = riscv_scan_get_delay(&info->learned_delays,
2563  int result;
2564  do {
2565  RISCV_INFO(r);
2566  result = riscv_batch_run_from(batch, finished_scans,
2567  &info->learned_delays,
2568  /*resets_delays*/ r->reset_delays_wait >= 0,
2569  r->reset_delays_wait);
2570  if (result != ERROR_OK)
2571  return result;
2572  const size_t new_finished_scans = riscv_batch_finished_scans(batch);
2573  assert(new_finished_scans >= finished_scans);
2574  decrement_reset_delays_counter(target, new_finished_scans - finished_scans);
2575  finished_scans = new_finished_scans;
2576  if (!riscv_batch_was_batch_busy(batch)) {
2577  assert(finished_scans == batch->used_scans);
2578  return ERROR_OK;
2579  }
2580  result = increase_dmi_busy_delay(target);
2581  if (result != ERROR_OK)
2582  return result;
2583  } while (timeval_ms() < then);
2584 
2585  assert(result == ERROR_OK);
2586  assert(riscv_batch_was_batch_busy(batch));
2587 
2588  /* Reset dmi_busy_delay, so the value doesn't get too big. */
2589  LOG_TARGET_DEBUG(target, "%s delay is restored to %u.",
2591  old_base_delay);
2592  riscv_scan_set_delay(&info->learned_delays, RISCV_DELAY_BASE,
2593  old_base_delay);
2594 
2595  LOG_TARGET_ERROR(target, "DMI operation didn't complete in %d seconds. "
2596  "The target is either really slow or broken. You could increase "
2597  "the timeout with riscv set_command_timeout_sec.",
2599  return ERROR_TIMEOUT_REACHED;
2600 }
2601 
2602 static int sba_supports_access(struct target *target, unsigned int size_bytes)
2603 {
2605  switch (size_bytes) {
2606  case 1:
2607  return get_field(info->sbcs, DM_SBCS_SBACCESS8);
2608  case 2:
2609  return get_field(info->sbcs, DM_SBCS_SBACCESS16);
2610  case 4:
2611  return get_field(info->sbcs, DM_SBCS_SBACCESS32);
2612  case 8:
2613  return get_field(info->sbcs, DM_SBCS_SBACCESS64);
2614  case 16:
2615  return get_field(info->sbcs, DM_SBCS_SBACCESS128);
2616  default:
2617  return 0;
2618  }
2619 }
2620 
2622  struct riscv_sample_buf *buf,
2624  int64_t until_ms)
2625 {
2627  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2628  if (sbasize == 0 || sbasize > 64) {
2629  LOG_TARGET_ERROR(target, "Memory sampling is only implemented for non-zero sbasize <= 64.");
2630  return ERROR_NOT_IMPLEMENTED;
2631  }
2632 
2633  if (get_field(info->sbcs, DM_SBCS_SBVERSION) != 1) {
2634  LOG_TARGET_ERROR(target, "Memory sampling is only implemented for SBA version 1.");
2635  return ERROR_NOT_IMPLEMENTED;
2636  }
2637 
2638  uint32_t sbcs = 0;
2639  uint32_t sbcs_valid = false;
2640 
2641  uint32_t sbaddress0 = 0;
2642  bool sbaddress0_valid = false;
2643  uint32_t sbaddress1 = 0;
2644  bool sbaddress1_valid = false;
2645 
2646  /* How often to read each value in a batch. */
2647  const unsigned int repeat = 5;
2648 
2649  unsigned int enabled_count = 0;
2650  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2651  if (config->bucket[i].enabled)
2652  enabled_count++;
2653  }
2654 
2655  while (timeval_ms() < until_ms) {
2656  /*
2657  * batch_run() adds to the batch, so we can't simply reuse the same
2658  * batch over and over. So we create a new one every time through the
2659  * loop.
2660  */
2661  struct riscv_batch *batch = riscv_batch_alloc(
2662  target, 1 + enabled_count * 5 * repeat);
2663  if (!batch)
2664  return ERROR_FAIL;
2665 
2666  unsigned int result_bytes = 0;
2667  for (unsigned int n = 0; n < repeat; n++) {
2668  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2669  if (config->bucket[i].enabled) {
2670  if (!sba_supports_access(target, config->bucket[i].size_bytes)) {
2671  LOG_TARGET_ERROR(target, "Hardware does not support SBA access for %d-byte memory sampling.",
2672  config->bucket[i].size_bytes);
2673  return ERROR_NOT_IMPLEMENTED;
2674  }
2675 
2676  uint32_t sbcs_write = DM_SBCS_SBREADONADDR;
2677  if (enabled_count == 1)
2678  sbcs_write |= DM_SBCS_SBREADONDATA;
2679  sbcs_write |= sb_sbaccess(config->bucket[i].size_bytes);
2680  if (!sbcs_valid || sbcs_write != sbcs) {
2681  riscv_batch_add_dm_write(batch, DM_SBCS, sbcs_write,
2682  true, RISCV_DELAY_BASE);
2683  sbcs = sbcs_write;
2684  sbcs_valid = true;
2685  }
2686 
2687  if (sbasize > 32 &&
2688  (!sbaddress1_valid ||
2689  sbaddress1 != config->bucket[i].address >> 32)) {
2690  sbaddress1 = config->bucket[i].address >> 32;
2692  sbaddress1, true, RISCV_DELAY_BASE);
2693  sbaddress1_valid = true;
2694  }
2695  if (!sbaddress0_valid ||
2696  sbaddress0 != (config->bucket[i].address & 0xffffffff)) {
2697  sbaddress0 = config->bucket[i].address;
2699  sbaddress0, true,
2701  sbaddress0_valid = true;
2702  }
2703  if (config->bucket[i].size_bytes > 4)
2708  result_bytes += 1 + config->bucket[i].size_bytes;
2709  }
2710  }
2711  }
2712 
2713  if (buf->used + result_bytes >= buf->size) {
2714  riscv_batch_free(batch);
2715  break;
2716  }
2717 
2718  size_t sbcs_read_index = riscv_batch_add_dm_read(batch, DM_SBCS,
2720 
2721  int result = batch_run(target, batch);
2722  if (result != ERROR_OK) {
2723  riscv_batch_free(batch);
2724  return result;
2725  }
2726 
2727  /* Discard the batch when we encounter a busy state on the DMI level.
2728  * It's too much hassle to try to recover partial data. We'll try again
2729  * with a larger DMI delay. */
2730  const uint32_t sbcs_read_op = riscv_batch_get_dmi_read_op(batch, sbcs_read_index);
2731  if (sbcs_read_op == DTM_DMI_OP_BUSY) {
2732  result = increase_dmi_busy_delay(target);
2733  if (result != ERROR_OK) {
2734  riscv_batch_free(batch);
2735  return result;
2736  }
2737  continue;
2738  }
2739 
2740  uint32_t sbcs_read = riscv_batch_get_dmi_read_data(batch, sbcs_read_index);
2741  if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
2742  /* Discard this batch when we encounter "busy error" state on the System Bus level.
2743  * We'll try next time with a larger System Bus read delay. */
2745  int res = riscv_scan_increase_delay(&info->learned_delays,
2747  riscv_batch_free(batch);
2748  if (res != ERROR_OK)
2749  return res;
2750  continue;
2751  }
2752  if (get_field(sbcs_read, DM_SBCS_SBERROR)) {
2753  /* The memory we're sampling was unreadable, somehow. Give up. */
2755  riscv_batch_free(batch);
2756  return ERROR_FAIL;
2757  }
2758 
2759  unsigned int read_count = 0;
2760  for (unsigned int n = 0; n < repeat; n++) {
2761  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2762  if (config->bucket[i].enabled) {
2764  uint64_t value = 0;
2765  if (config->bucket[i].size_bytes > 4)
2766  value = ((uint64_t)riscv_batch_get_dmi_read_data(batch, read_count++)) << 32;
2767  value |= riscv_batch_get_dmi_read_data(batch, read_count++);
2768 
2769  buf->buf[buf->used] = i;
2770  buf_set_u64(buf->buf + buf->used + 1, 0, config->bucket[i].size_bytes * 8, value);
2771  buf->used += 1 + config->bucket[i].size_bytes;
2772  }
2773  }
2774  }
2775 
2776  riscv_batch_free(batch);
2777  }
2778 
2779  return ERROR_OK;
2780 }
2781 
2782 static int sample_memory(struct target *target,
2783  struct riscv_sample_buf *buf,
2785  int64_t until_ms)
2786 {
2787  if (!config->enabled)
2788  return ERROR_OK;
2789 
2790  return sample_memory_bus_v1(target, buf, config, until_ms);
2791 }
2792 
2794 {
2797  return ERROR_FAIL;
2798 
2799  uint32_t dmstatus;
2800  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
2801  return ERROR_FAIL;
2802  if (get_field(dmstatus, DM_DMSTATUS_ANYHAVERESET)) {
2803  LOG_TARGET_INFO(target, "Hart unexpectedly reset!");
2804  info->dcsr_ebreak_is_set = false;
2805  /* TODO: Can we make this more obvious to eg. a gdb user? */
2806  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE |
2808  dmcontrol = set_dmcontrol_hartsel(dmcontrol, info->index);
2809  /* If we had been halted when we reset, request another halt. If we
2810  * ended up running out of reset, then the user will (hopefully) get a
2811  * message that a reset happened, that the target is running, and then
2812  * that it is halted again once the request goes through.
2813  */
2814  if (target->state == TARGET_HALTED) {
2815  dmcontrol |= DM_DMCONTROL_HALTREQ;
2816  /* `haltreq` should not be issued if `abstractcs.busy`
2817  * is set. */
2818  int result = wait_for_idle_if_needed(target);
2819  if (result != ERROR_OK)
2820  return result;
2821  }
2822  dm_write(target, DM_DMCONTROL, dmcontrol);
2823  }
2824  if (get_field(dmstatus, DM_DMSTATUS_ALLNONEXISTENT)) {
2826  return ERROR_OK;
2827  }
2828  if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
2830  return ERROR_OK;
2831  }
2832  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED)) {
2834  return ERROR_OK;
2835  }
2836  if (get_field(dmstatus, DM_DMSTATUS_ALLRUNNING)) {
2838  return ERROR_OK;
2839  }
2840  LOG_TARGET_ERROR(target, "Couldn't determine state. dmstatus=0x%x", dmstatus);
2841  return ERROR_FAIL;
2842 }
2843 
2845  enum riscv_hart_state previous_riscv_state)
2846 {
2848 
2850  LOG_TARGET_WARNING(target, "Discarding values of dirty registers "
2851  "(due to target becoming unavailable).");
2852 
2854 
2855  info->dcsr_ebreak_is_set = false;
2856  return ERROR_OK;
2857 }
2858 
2859 static int tick(struct target *target)
2860 {
2862  if (!info->dcsr_ebreak_is_set &&
2863  target->state == TARGET_RUNNING &&
2865  return halt_set_dcsr_ebreak(target);
2866  return ERROR_OK;
2867 }
2868 
2869 static int init_target(struct command_context *cmd_ctx,
2870  struct target *target)
2871 {
2872  LOG_TARGET_DEBUG(target, "Init.");
2873  RISCV_INFO(generic_info);
2874 
2875  generic_info->select_target = &dm013_select_target;
2876  generic_info->get_hart_state = &riscv013_get_hart_state;
2877  generic_info->resume_go = &riscv013_resume_go;
2878  generic_info->step_current_hart = &riscv013_step_current_hart;
2879  generic_info->resume_prep = &riscv013_resume_prep;
2880  generic_info->halt_prep = &riscv013_halt_prep;
2881  generic_info->halt_go = &riscv013_halt_go;
2882  generic_info->on_step = &riscv013_on_step;
2883  generic_info->halt_reason = &riscv013_halt_reason;
2884  generic_info->read_progbuf = &riscv013_read_progbuf;
2885  generic_info->write_progbuf = &riscv013_write_progbuf;
2886  generic_info->execute_progbuf = &riscv013_execute_progbuf;
2887  generic_info->invalidate_cached_progbuf = &riscv013_invalidate_cached_progbuf;
2888  generic_info->fill_dmi_write = &riscv013_fill_dmi_write;
2889  generic_info->fill_dmi_read = &riscv013_fill_dmi_read;
2890  generic_info->fill_dm_nop = &riscv013_fill_dm_nop;
2891  generic_info->get_dmi_address_bits = &riscv013_get_dmi_address_bits;
2892  generic_info->authdata_read = &riscv013_authdata_read;
2893  generic_info->authdata_write = &riscv013_authdata_write;
2894  generic_info->dmi_read = &dmi_read;
2895  generic_info->dmi_write = &dmi_write;
2896  generic_info->get_dmi_address = &riscv013_get_dmi_address;
2897  generic_info->access_memory = &riscv013_access_memory;
2898  generic_info->data_bits = &riscv013_data_bits;
2899  generic_info->print_info = &riscv013_print_info;
2900  generic_info->get_impebreak = &riscv013_get_impebreak;
2901  generic_info->get_progbufsize = &riscv013_get_progbufsize;
2902 
2903  generic_info->handle_became_unavailable = &handle_became_unavailable;
2904  generic_info->tick = &tick;
2905 
2906  if (!generic_info->version_specific) {
2907  generic_info->version_specific = calloc(1, sizeof(riscv013_info_t));
2908  if (!generic_info->version_specific)
2909  return ERROR_FAIL;
2910  }
2911  generic_info->sample_memory = sample_memory;
2913 
2914  info->progbufsize = -1;
2916 
2917  info->ac_not_supported_cache = ac_cache_construct();
2918 
2919  return ERROR_OK;
2920 }
2921 
2922 static int assert_reset(struct target *target)
2923 {
2925  int result;
2926 
2927  select_dmi(target->tap);
2928 
2930  /* Run the user-supplied script if there is one. */
2932  } else {
2933  dm013_info_t *dm = get_dm(target);
2934  if (!dm)
2935  return ERROR_FAIL;
2936 
2937  uint32_t control = set_field(0, DM_DMCONTROL_DMACTIVE, 1);
2938  control = set_dmcontrol_hartsel(control, info->index);
2939  control = set_field(control, DM_DMCONTROL_HALTREQ,
2940  target->reset_halt ? 1 : 0);
2941  control = set_field(control, DM_DMCONTROL_NDMRESET, 1);
2942  /* If `abstractcs.busy` is set, debugger should not
2943  * change `hartsel` or set `haltreq`
2944  */
2945  const bool hartsel_changed = (int)info->index != dm->current_hartid;
2946  if (hartsel_changed || target->reset_halt) {
2947  result = wait_for_idle_if_needed(target);
2948  if (result != ERROR_OK)
2949  return result;
2950  }
2951  result = dm_write(target, DM_DMCONTROL, control);
2952  if (result != ERROR_OK)
2953  return result;
2954  }
2955 
2957 
2958  /* The DM might have gotten reset if OpenOCD called us in some reset that
2959  * involves SRST being toggled. So clear our cache which may be out of
2960  * date. */
2962 }
2963 
2965 {
2966  const struct riscv_private_config * const config = riscv_private_config(target);
2967  for (int i = 0; i < N_RISCV_MODE; ++i)
2968  if (config->dcsr_ebreak_fields[i])
2969  return false;
2970  return true;
2971 }
2972 
2973 static int deassert_reset(struct target *target)
2974 {
2976  dm013_info_t *dm = get_dm(target);
2977  if (!dm)
2978  return ERROR_FAIL;
2979  int result;
2980 
2981  select_dmi(target->tap);
2982  /* Clear the reset, but make sure haltreq is still set */
2983  uint32_t control = 0;
2984  control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
2985  control = set_field(control, DM_DMCONTROL_HALTREQ, target->reset_halt ? 1 : 0);
2986  control = set_dmcontrol_hartsel(control, info->index);
2987  /* If `abstractcs.busy` is set, debugger should not
2988  * change `hartsel`.
2989  */
2990  const bool hartsel_changed = (int)info->index != dm->current_hartid;
2991  if (hartsel_changed) {
2992  result = wait_for_idle_if_needed(target);
2993  if (result != ERROR_OK)
2994  return result;
2995  }
2996  result = dm_write(target, DM_DMCONTROL, control);
2997  if (result != ERROR_OK)
2998  return result;
2999 
3000  uint32_t dmstatus;
3001  const unsigned int orig_base_delay = riscv_scan_get_delay(&info->learned_delays,
3003  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
3004  LOG_TARGET_DEBUG(target, "Waiting for hart to come out of reset.");
3005  do {
3006  result = dmstatus_read(target, &dmstatus, true);
3007  if (result != ERROR_OK)
3008  return result;
3009 
3010  if (timeval_ms() > then) {
3011  LOG_TARGET_ERROR(target, "Hart didn't leave reset in %ds; "
3012  "dmstatus=0x%x (allunavail=%s, allhavereset=%s); "
3013  "Increase the timeout with riscv set_command_timeout_sec.",
3014  riscv_get_command_timeout_sec(), dmstatus,
3015  get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL) ? "true" : "false",
3016  get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET) ? "true" : "false");
3017  return ERROR_TIMEOUT_REACHED;
3018  }
3019  } while (!get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET));
3020 
3021  riscv_scan_set_delay(&info->learned_delays, RISCV_DELAY_BASE,
3022  orig_base_delay);
3023 
3024  /* Ack reset and clear DM_DMCONTROL_HALTREQ if previously set */
3025  control = 0;
3026  control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
3027  control = set_field(control, DM_DMCONTROL_ACKHAVERESET, 1);
3028  control = set_dmcontrol_hartsel(control, info->index);
3029  result = dm_write(target, DM_DMCONTROL, control);
3030  if (result != ERROR_OK)
3031  return result;
3032 
3033  if (target->reset_halt) {
3036  } else {
3039  }
3040  info->dcsr_ebreak_is_set = dcsr_ebreak_config_equals_reset_value(target);
3041  return ERROR_OK;
3042 }
3043 
3044 static int execute_autofence(struct target *target)
3045 {
3047  return ERROR_FAIL;
3048 
3049  RISCV_INFO(r);
3050  if (!r->autofence)
3051  return ERROR_OK;
3052 
3053  /* FIXME: For non-coherent systems we need to flush the caches right
3054  * here, but there's no ISA-defined way of doing that. */
3055  struct riscv_program program;
3056 
3057  /* program.execution_result may indicate RISCV_PROGBUF_EXEC_RESULT_EXCEPTION -
3058  * currently, we ignore this error since most likely this is an indication
3059  * that target does not support a fence instruction (execution of an
3060  * unsupported instruction results in "Illegal instruction" exception on
3061  * targets that comply with riscv-privilege spec).
3062  * Currently, RISC-V specification does not provide us with a portable and
3063  * less invasive way to detect if a fence is supported by the target. We may
3064  * revise this code once the spec allows us to do this */
3065  if (has_sufficient_progbuf(target, 3)) {
3066  riscv_program_init(&program, target);
3067  riscv_program_fence_i(&program);
3068  riscv_program_fence_rw_rw(&program);
3069  if (riscv_program_exec(&program, target) != ERROR_OK) {
3071  LOG_TARGET_ERROR(target, "Unexpected error during fence execution");
3072  return ERROR_FAIL;
3073  }
3074  LOG_TARGET_DEBUG(target, "Unable to execute fence.i and fence rw, rw");
3075  }
3076  LOG_TARGET_DEBUG(target, "Successfully executed fence.i and fence rw, rw");
3077  return ERROR_OK;
3078  }
3079 
3080  if (has_sufficient_progbuf(target, 2)) {
3081  riscv_program_init(&program, target);
3082  riscv_program_fence_i(&program);
3083  if (riscv_program_exec(&program, target) != ERROR_OK) {
3085  LOG_TARGET_ERROR(target, "Unexpected error during fence.i execution");
3086  return ERROR_FAIL;
3087  }
3088  LOG_TARGET_DEBUG(target, "Unable to execute fence.i");
3089  }
3090  LOG_TARGET_DEBUG(target, "Successfully executed fence.i");
3091 
3092  riscv_program_init(&program, target);
3093  riscv_program_fence_rw_rw(&program);
3094  if (riscv_program_exec(&program, target) != ERROR_OK) {
3096  LOG_TARGET_ERROR(target, "Unexpected error during fence rw, rw execution");
3097  return ERROR_FAIL;
3098  }
3099  LOG_TARGET_DEBUG(target, "Unable to execute fence rw, rw");
3100  }
3101  LOG_TARGET_DEBUG(target, "Successfully executed fence rw, rw");
3102  return ERROR_OK;
3103  }
3104 
3105  return ERROR_FAIL;
3106 }
3107 
3108 static void log_memory_access128(target_addr_t address, uint64_t value_h,
3109  uint64_t value_l, bool is_read)
3110 {
3112  return;
3113 
3114  char fmt[80];
3115  sprintf(fmt, "M[0x%" TARGET_PRIxADDR "] %ss 0x%%016" PRIx64 "%%016" PRIx64,
3116  address, is_read ? "read" : "write");
3117  LOG_DEBUG(fmt, value_h, value_l);
3118 }
3119 
3120 static void log_memory_access64(target_addr_t address, uint64_t value,
3121  unsigned int size_bytes, bool is_read)
3122 {
3124  return;
3125 
3126  char fmt[80];
3127  sprintf(fmt, "M[0x%" TARGET_PRIxADDR "] %ss 0x%%0%d" PRIx64,
3128  address, is_read ? "read" : "write", size_bytes * 2);
3129  switch (size_bytes) {
3130  case 1:
3131  value &= 0xff;
3132  break;
3133  case 2:
3134  value &= 0xffff;
3135  break;
3136  case 4:
3137  value &= 0xffffffffUL;
3138  break;
3139  case 8:
3140  break;
3141  default:
3142  assert(false);
3143  }
3144  LOG_DEBUG(fmt, value);
3145 }
3146 static void log_memory_access(target_addr_t address, uint32_t *sbvalue,
3147  unsigned int size_bytes, bool is_read)
3148 {
3149  if (size_bytes == 16) {
3150  uint64_t value_h = ((uint64_t)sbvalue[3] << 32) | sbvalue[2];
3151  uint64_t value_l = ((uint64_t)sbvalue[1] << 32) | sbvalue[0];
3152  log_memory_access128(address, value_h, value_l, is_read);
3153  } else {
3154  uint64_t value = ((uint64_t)sbvalue[1] << 32) | sbvalue[0];
3155  log_memory_access64(address, value, size_bytes, is_read);
3156  }
3157 }
3158 
3159 /* Read the relevant sbdata regs depending on size, and put the results into
3160  * buffer. */
3162  uint32_t size, uint8_t *buffer)
3163 {
3164  int result;
3165  uint32_t sbvalue[4] = { 0 };
3166  static int sbdata[4] = { DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3 };
3167  assert(size <= 16);
3168  for (int i = (size - 1) / 4; i >= 0; i--) {
3169  result = dm_read(target, &sbvalue[i], sbdata[i]);
3170  if (result != ERROR_OK)
3171  return result;
3172  buf_set_u32(buffer + i * 4, 0, 8 * MIN(size, 4), sbvalue[i]);
3173  }
3174  log_memory_access(address, sbvalue, size, true);
3175  return ERROR_OK;
3176 }
3177 
3179 {
3181  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
3182  target_addr_t address = 0;
3183  uint32_t v;
3184  if (sbasize > 32) {
3185  if (dm_read(target, &v, DM_SBADDRESS1) == ERROR_OK)
3186  address |= v;
3187  address <<= 32;
3188  }
3189  if (dm_read(target, &v, DM_SBADDRESS0) == ERROR_OK)
3190  address |= v;
3191  return address;
3192 }
3193 
3194 static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
3195 {
3196  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
3197  while (1) {
3198  if (dm_read(target, sbcs, DM_SBCS) != ERROR_OK)
3199  return ERROR_FAIL;
3200  if (!get_field(*sbcs, DM_SBCS_SBBUSY))
3201  return ERROR_OK;
3202  if (timeval_ms() > then) {
3203  LOG_TARGET_ERROR(target, "Timed out after %ds waiting for sbbusy to go low (sbcs=0x%x). "
3204  "Increase the timeout with riscv set_command_timeout_sec.",
3206  return ERROR_FAIL;
3207  }
3208  }
3209 }
3210 
3211 /* TODO: return struct mem_access_result */
3212 static int modify_privilege_for_virt2phys_mode(struct target *target, riscv_reg_t *mstatus, riscv_reg_t *mstatus_old,
3213  riscv_reg_t *dcsr, riscv_reg_t *dcsr_old)
3214 {
3215  assert(mstatus);
3216  assert(mstatus_old);
3217  assert(dcsr);
3218  assert(dcsr_old);
3220  return ERROR_OK;
3221 
3222  /* Read and save DCSR */
3224  return ERROR_FAIL;
3225  *dcsr_old = *dcsr;
3226 
3227  /* Read and save MSTATUS */
3228  if (riscv_reg_get(target, mstatus, GDB_REGNO_MSTATUS) != ERROR_OK)
3229  return ERROR_FAIL;
3230  *mstatus_old = *mstatus;
3231 
3232  /* If we come from m-mode with mprv set, we want to keep mpp */
3233  if (get_field(*dcsr, CSR_DCSR_PRV) == PRV_M)
3234  return ERROR_OK;
3235 
3236  /* mstatus.mpp <- dcsr.prv */
3237  *mstatus = set_field(*mstatus, MSTATUS_MPP, get_field(*dcsr, CSR_DCSR_PRV));
3238 
3239  /* mstatus.mprv <- 1 */
3240  *mstatus = set_field(*mstatus, MSTATUS_MPRV, 1);
3241 
3242  /* Write MSTATUS */
3243  if (*mstatus != *mstatus_old &&
3245  return ERROR_FAIL;
3246 
3247  /* dcsr.mprven <- 1 */
3249 
3250  /* Write DCSR */
3251  if (*dcsr != *dcsr_old &&
3253  return ERROR_FAIL;
3254 
3255  return ERROR_OK;
3256 }
3257 
3259  riscv_reg_t dcsr, riscv_reg_t dcsr_old)
3260 {
3262  return ERROR_OK;
3263 
3264  /* Restore MSTATUS */
3265  if (mstatus != mstatus_old &&
3266  riscv_reg_set(target, GDB_REGNO_MSTATUS, mstatus_old) != ERROR_OK)
3267  return ERROR_FAIL;
3268 
3269  /* Restore DCSR */
3270  if (dcsr != dcsr_old &&
3272  return ERROR_FAIL;
3273 
3274  return ERROR_OK;
3275 }
3276 
3277 static int read_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
3278 {
3279  assert(riscv_mem_access_is_read(args));
3280 
3281  if (args.size != args.increment) {
3282  LOG_TARGET_ERROR(target, "sba v0 reads only support size==increment");
3283  return ERROR_NOT_IMPLEMENTED;
3284  }
3285 
3286  LOG_TARGET_DEBUG(target, "System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
3287  TARGET_PRIxADDR, args.size, args.count, args.address);
3288  uint8_t *t_buffer = args.read_buffer;
3289  riscv_addr_t cur_addr = args.address;
3290  riscv_addr_t fin_addr = args.address + (args.count * args.size);
3291  uint32_t access = 0;
3292 
3293  const int DM_SBCS_SBSINGLEREAD_OFFSET = 20;
3294  const uint32_t DM_SBCS_SBSINGLEREAD = (0x1U << DM_SBCS_SBSINGLEREAD_OFFSET);
3295 
3296  const int DM_SBCS_SBAUTOREAD_OFFSET = 15;
3297  const uint32_t DM_SBCS_SBAUTOREAD = (0x1U << DM_SBCS_SBAUTOREAD_OFFSET);
3298 
3299  /* ww favorise one off reading if there is an issue */
3300  if (args.count == 1) {
3301  for (uint32_t i = 0; i < args.count; i++) {
3302  if (dm_read(target, &access, DM_SBCS) != ERROR_OK)
3303  return ERROR_FAIL;
3304  dm_write(target, DM_SBADDRESS0, cur_addr);
3305  /* size/2 matching the bit sbaccess of the spec 0.13 */
3306  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
3307  access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
3308  LOG_TARGET_DEBUG(target, "read_memory: sab: access: 0x%08x", access);
3309  dm_write(target, DM_SBCS, access);
3310  /* 3) read */
3311  uint32_t value;
3312  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3313  return ERROR_FAIL;
3314  LOG_TARGET_DEBUG(target, "read_memory: sab: value: 0x%08x", value);
3315  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3316  t_buffer += args.size;
3317  cur_addr += args.size;
3318  }
3319  return ERROR_OK;
3320  }
3321 
3322  /* has to be the same size if we want to read a block */
3323  LOG_TARGET_DEBUG(target, "Reading block until final address 0x%" PRIx64, fin_addr);
3324  if (dm_read(target, &access, DM_SBCS) != ERROR_OK)
3325  return ERROR_FAIL;
3326  /* set current address */
3327  dm_write(target, DM_SBADDRESS0, cur_addr);
3328  /* 2) write sbaccess=2, sbsingleread,sbautoread,sbautoincrement
3329  * size/2 matching the bit access of the spec 0.13 */
3330  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
3331  access = set_field(access, DM_SBCS_SBAUTOREAD, 1);
3332  access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
3333  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
3334  LOG_TARGET_DEBUG(target, "access: 0x%08x", access);
3335  dm_write(target, DM_SBCS, access);
3336 
3337  while (cur_addr < fin_addr) {
3338  LOG_TARGET_DEBUG(target, "sab:autoincrement:\r\n\tsize: %d\tcount:%d\taddress: 0x%08"
3339  PRIx64, args.size, args.count, cur_addr);
3340  /* read */
3341  uint32_t value;
3342  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3343  return ERROR_FAIL;
3344  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3345  cur_addr += args.size;
3346  t_buffer += args.size;
3347 
3348  /* if we are reaching last address, we must clear autoread */
3349  if (cur_addr == fin_addr && args.count != 1) {
3350  dm_write(target, DM_SBCS, 0);
3351  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3352  return ERROR_FAIL;
3353  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3354  }
3355  }
3356 
3357  uint32_t sbcs;
3358  if (dm_read(target, &sbcs, DM_SBCS) != ERROR_OK)
3359  return ERROR_FAIL;
3360 
3361  return ERROR_OK;
3362 }
3363 
3367 static int read_memory_bus_v1(struct target *target, const struct riscv_mem_access_args args)
3368 {
3369  assert(riscv_mem_access_is_read(args));
3370 
3371  const target_addr_t address = args.address;
3372  const uint32_t increment = args.increment;
3373  const uint32_t count = args.count;
3374  const uint32_t size = args.size;
3375  uint8_t *buffer = args.read_buffer;
3376 
3377  if (increment != size && increment != 0) {
3378  LOG_TARGET_ERROR(target, "sba v1 reads only support increment of size or 0");
3379  return ERROR_NOT_IMPLEMENTED;
3380  }
3381 
3382  assert(size <= 16);
3383  assert(IS_PWR_OF_2(size));
3384 
3385  dm013_info_t *dm = get_dm(target);
3386  if (!dm)
3387  return ERROR_FAIL;
3388 
3390  target_addr_t next_address = address;
3391  target_addr_t end_address = address + (increment ? count : 1) * size;
3392 
3393  /* TODO: Reading all the elements in a single batch will boost the
3394  * performance.
3395  */
3396  while (next_address < end_address) {
3397  uint32_t sbcs_write = set_field(0, DM_SBCS_SBREADONADDR, 1);
3398  sbcs_write |= sb_sbaccess(size);
3399  if (increment == size)
3400  sbcs_write = set_field(sbcs_write, DM_SBCS_SBAUTOINCREMENT, 1);
3401  if (count > 1)
3402  sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, count > 1);
3403  if (dm_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
3404  return ERROR_FAIL;
3405 
3406  /* This address write will trigger the first read. */
3408  return ERROR_FAIL;
3409 
3410  /* First read has been started. Optimistically assume that it has
3411  * completed. */
3412 
3413  static int sbdata[4] = {DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3};
3414  /* TODO: The only purpose of "sbvalue" is to be passed to
3415  * "log_memory_access()". If "log_memory_access()" were to
3416  * accept "uint8_t *" instead of "uint32_t *", "sbvalue" would
3417  * be unnecessary.
3418  */
3419  uint32_t sbvalue[4] = {0};
3420  for (uint32_t i = (next_address - address) / size; i < count - 1; i++) {
3421  const uint32_t size_in_words = DIV_ROUND_UP(size, 4);
3422  struct riscv_batch *batch = riscv_batch_alloc(target, size_in_words);
3423  /* Read of sbdata0 must be performed as last because it
3424  * starts the new bus data transfer
3425  * (in case "sbcs.sbreadondata" was set above).
3426  * We don't want to start the next bus read before we
3427  * fetch all the data from the last bus read. */
3428  for (uint32_t j = size_in_words - 1; j > 0; --j)
3429  riscv_batch_add_dm_read(batch, sbdata[j], RISCV_DELAY_BASE);
3431 
3432  int res = batch_run_timeout(target, batch);
3433  if (res != ERROR_OK) {
3434  riscv_batch_free(batch);
3435  return res;
3436  }
3437 
3438  const size_t last_key = batch->read_keys_used - 1;
3439  for (size_t k = 0; k <= last_key; ++k) {
3440  sbvalue[k] = riscv_batch_get_dmi_read_data(batch, last_key - k);
3441  buf_set_u32(buffer + i * size + k * 4, 0, MIN(32, 8 * size), sbvalue[k]);
3442  }
3443 
3444  riscv_batch_free(batch);
3445  const target_addr_t read_addr = address + i * increment;
3446  log_memory_access(read_addr, sbvalue, size, true);
3447  }
3448 
3449  uint32_t sbcs_read = 0;
3450  if (count > 1) {
3451  /* "Writes to sbcs while sbbusy is high result in undefined behavior.
3452  * A debugger must not write to sbcs until it reads sbbusy as 0." */
3453  if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
3454  return ERROR_FAIL;
3455 
3456  sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, 0);
3457  if (dm_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
3458  return ERROR_FAIL;
3459  }
3460 
3461  /* Read the last word, after we disabled sbreadondata if necessary. */
3462  if (!get_field(sbcs_read, DM_SBCS_SBERROR) &&
3463  !get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
3464  if (read_memory_bus_word(target, address + (count - 1) * increment, size,
3465  buffer + (count - 1) * size) != ERROR_OK)
3466  return ERROR_FAIL;
3467 
3468  if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
3469  return ERROR_FAIL;
3470  }
3471 
3472  if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
3473  /* We read while the target was busy. Slow down and try again.
3474  * Clear sbbusyerror, as well as readondata or readonaddr. */
3476  return ERROR_FAIL;
3477 
3478  if (get_field(sbcs_read, DM_SBCS_SBERROR) == DM_SBCS_SBERROR_NONE) {
3479  /* Read the address whose read was last completed. */
3480  next_address = sb_read_address(target);
3481 
3482  /* Read the value for the last address. It's
3483  * sitting in the register for us, but we read it
3484  * too early (sbbusyerror became set). */
3485  target_addr_t current_address = next_address - (increment ? size : 0);
3486  if (read_memory_bus_word(target, current_address, size,
3487  buffer + current_address - address) != ERROR_OK)
3488  return ERROR_FAIL;
3489  }
3490 
3491  int res = riscv_scan_increase_delay(&info->learned_delays,
3493  if (res != ERROR_OK)
3494  return res;
3495  continue;
3496  }
3497 
3498  unsigned int error = get_field(sbcs_read, DM_SBCS_SBERROR);
3499  if (error == DM_SBCS_SBERROR_NONE) {
3500  next_address = end_address;
3501  } else {
3502  /* Some error indicating the bus access failed, but not because of
3503  * something we did wrong. */
3505  return ERROR_FAIL;
3506  return ERROR_FAIL;
3507  }
3508  }
3509 
3510  return ERROR_OK;
3511 }
3512 
3513 static void log_mem_access_result(struct target *target, bool success,
3514  enum riscv_mem_access_method method, bool is_read)
3515 {
3516  RISCV_INFO(r);
3517  bool warn = false;
3518  char msg[60];
3519 
3520  /* Compose the message */
3521  snprintf(msg, 60, "%s to %s memory via %s.",
3522  success ? "Succeeded" : "Failed",
3523  is_read ? "read" : "write",
3524  (method == RISCV_MEM_ACCESS_PROGBUF) ? "program buffer" :
3525  (method == RISCV_MEM_ACCESS_SYSBUS) ? "system bus" : "abstract access");
3526 
3527  /* Determine the log message severity. Show warnings only once. */
3528  if (!success) {
3529  warn = r->mem_access_warn[method];
3530  r->mem_access_warn[method] = false;
3531  }
3532 
3533  if (warn)
3534  LOG_TARGET_WARNING(target, "%s", msg);
3535  else
3536  LOG_TARGET_DEBUG(target, "%s", msg);
3537 }
3538 
3545 };
3546 
3547 #define LIST_OF_MEM_ACCESS_RESULTS \
3548  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_OK, OK, "ok") \
3549  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_DISABLED, DISABLED, "disabled") \
3550  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED, SKIPPED, "skipped") \
3551  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR, \
3552  SKIPPED, "skipped (abstract access cmderr)") \
3553  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_NOT_PRESENT, \
3554  SKIPPED, "skipped (progbuf not present)") \
3555  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_INSUFFICIENT, \
3556  SKIPPED, "skipped (insufficient progbuf)") \
3557  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE, \
3558  SKIPPED, "skipped (unsupported access size)") \
3559  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_XLEN_TOO_SHORT, \
3560  SKIPPED, "skipped (xlen too short)") \
3561  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TARGET_NOT_HALTED, \
3562  SKIPPED, "skipped (target not halted)") \
3563  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS, \
3564  SKIPPED, "skipped (address too large)") \
3565  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE, \
3566  SKIPPED, "skipped (increment size not supported)") \
3567  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TARGET_SELECT_FAILED, \
3568  SKIPPED, "skipped (dm target select failed)") \
3569  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_FENCE_EXEC_FAILED, \
3570  SKIPPED, "skipped (fence execution failed)") \
3571  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_SYSBUS_ACCESS_FAILED, \
3572  SKIPPED, "skipped (sysbus access failed)") \
3573  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_REG_SAVE_FAILED, \
3574  SKIPPED, "skipped (register save failed)") \
3575  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNKNOWN_SYSBUS_VERSION, \
3576  SKIPPED, "skipped (unknown sysbus version)") \
3577  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGRAM_WRITE_FAILED, \
3578  SKIPPED, "skipped (program write failed)") \
3579  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED, \
3580  SKIPPED, "skipped (progbuf fill failed)") \
3581  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_WRITE_ABSTRACT_ARG_FAILED, \
3582  SKIPPED, "skipped (abstract command argument write failed)") \
3583  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PRIV_MOD_FAILED, \
3584  SKIPPED, "skipped (privilege modification failed)") \
3585  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED, FAILED, "failed") \
3586  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_DM_ACCESS_FAILED, \
3587  FAILED, "failed (DM register access failed)") \
3588  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PRIV_MOD_FAILED, \
3589  FAILED, "failed (privilege modification failed)") \
3590  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_REG_READ_FAILED, \
3591  FAILED, "failed (register read failed)") \
3592  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED, \
3593  FAILED, "failed (progbuf startup failed)") \
3594  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED, \
3595  FAILED, "failed (progbuf inner failed)") \
3596  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_TEARDOWN_FAILED, \
3597  FAILED, "failed (progbuf teardown failed)") \
3598  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_EXECUTE_ABSTRACT_FAILED, \
3599  FAILED, "failed (execute abstract failed)") \
3600  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_NO_FORWARD_PROGRESS, \
3601  FAILED, "failed (no forward progress)") \
3602  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_FENCE_EXEC_FAILED, \
3603  FAILED, "failed (fence execution failed)") \
3604 
3605 
3606 #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) name,
3609 };
3610 #undef MEM_ACCESS_RESULT_HANDLER
3611 
3612 /* Structure is intentionally used to contain the memory access result,
3613  for type safety - to avoid implicit conversions to integers. */
3616 };
3617 
3619 {
3620  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3621  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3622  == MEM_ACCESS_RESULT_TYPE_OK;
3623 
3624  switch (status.value) {
3626  }
3627  #undef MEM_ACCESS_RESULT_HANDLER
3628 
3629  LOG_ERROR("Unknown memory access status: %d", status.value);
3630  assert(false && "Unknown memory access status");
3631  return false;
3632 }
3633 
3635 {
3636  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3637  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3638  == MEM_ACCESS_RESULT_TYPE_FAILED;
3639 
3640  switch (status.value) {
3642  }
3643  #undef MEM_ACCESS_RESULT_HANDLER
3644 
3645  LOG_ERROR("Unknown memory access status: %d", status.value);
3646  assert(false && "Unknown memory access status");
3647  return true;
3648 }
3649 
3651 {
3652  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3653  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3654  == MEM_ACCESS_RESULT_TYPE_SKIPPED;
3655 
3656  switch (status.value) {
3658  }
3659  #undef MEM_ACCESS_RESULT_HANDLER
3660  LOG_ERROR("Unknown memory access status: %d", status.value);
3661  assert(false && "Unknown memory access status");
3662  return true;
3663 }
3664 
3666 {
3667  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3668  [name] = msg,
3669  static const char * const table[] = {
3671  };
3672  #undef MEM_ACCESS_RESULT_HANDLER
3673 
3674  assert(status.value < ARRAY_SIZE(table));
3675  return table[status.value];
3676 }
3677 
3679 {
3680  struct mem_access_result result = {.value = value};
3681  return result;
3682 }
3683 
3685  const struct riscv_mem_access_args args)
3686 {
3687  assert(riscv_mem_access_is_valid(args));
3688  const char *const access_type =
3689  riscv_mem_access_is_read(args) ? "read" : "write";
3690 
3691  if (!has_sufficient_progbuf(target, 1)) {
3692  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf "
3693  "- progbuf not present", access_type);
3694  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_NOT_PRESENT);
3695  }
3696  if (!has_sufficient_progbuf(target, 3)) {
3697  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3698  "insufficient progbuf size.", access_type);
3699  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_INSUFFICIENT);
3700  }
3701  if (target->state != TARGET_HALTED) {
3702  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3703  "target not halted.", access_type);
3704  return mem_access_result(MEM_ACCESS_SKIPPED_TARGET_NOT_HALTED);
3705  }
3706  if (riscv_xlen(target) < args.size * 8) {
3707  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3708  "XLEN (%d) is too short for %d-bit memory args.",
3709  access_type, riscv_xlen(target), args.size * 8);
3710  return mem_access_result(MEM_ACCESS_SKIPPED_XLEN_TOO_SHORT);
3711  }
3712  if (args.size > 8) {
3713  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3714  "unsupported size.", access_type);
3715  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3716  }
3717  if ((sizeof(args.address) * 8 > riscv_xlen(target))
3718  && (args.address >> riscv_xlen(target))) {
3719  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3720  "progbuf only supports %u-bit address.", access_type, riscv_xlen(target));
3721  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3722  }
3723 
3724  return mem_access_result(MEM_ACCESS_OK);
3725 }
3726 
3727 static struct mem_access_result
3728 mem_should_skip_sysbus(struct target *target, const struct riscv_mem_access_args args)
3729 {
3730  assert(riscv_mem_access_is_valid(args));
3731 
3733  const bool is_read = riscv_mem_access_is_read(args);
3734  const char *const access_type = is_read ? "read" : "write";
3735 
3736  if (!sba_supports_access(target, args.size)) {
3737  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3738  "unsupported size.", access_type);
3739  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3740  }
3741  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
3742  if ((sizeof(args.address) * 8 > sbasize)
3743  && (args.address >> sbasize)) {
3744  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3745  "sba only supports %u-bit address.", access_type, sbasize);
3746  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3747  }
3748  if (is_read && args.increment != args.size
3749  && (get_field(info->sbcs, DM_SBCS_SBVERSION) == 0
3750  || args.increment != 0)) {
3751  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3752  "sba %ss only support (size == increment) or also "
3753  "size==0 for sba v1.", access_type, access_type);
3754  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE);
3755  }
3756 
3757  return mem_access_result(MEM_ACCESS_OK);
3758 }
3759 
3760 static struct mem_access_result
3761 mem_should_skip_abstract(struct target *target, const struct riscv_mem_access_args args)
3762 {
3763  assert(riscv_mem_access_is_valid(args));
3764 
3765  const bool is_read = riscv_mem_access_is_read(args);
3766  const char *const access_type = is_read ? "read" : "write";
3767  if (args.size > 8) {
3768  /* TODO: Add 128b support if it's ever used. Involves modifying
3769  read/write_abstract_arg() to work on two 64b values. */
3770  LOG_TARGET_DEBUG(target, "Skipping mem %s via abstract access - "
3771  "unsupported size: %d bits", access_type, args.size * 8);
3772  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3773  }
3774  if ((sizeof(args.address) * 8 > riscv_xlen(target))
3775  && (args.address >> riscv_xlen(target))) {
3776  LOG_TARGET_DEBUG(target, "Skipping mem %s via abstract access - "
3777  "abstract access only supports %u-bit address.",
3778  access_type, riscv_xlen(target));
3779  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3780  }
3781  if (is_read && args.size != args.increment) {
3782  LOG_TARGET_ERROR(target, "Skipping mem %s via abstract access - "
3783  "abstract command %ss only support (size == increment).",
3784  access_type, access_type);
3785  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE);
3786  }
3787  return mem_access_result(MEM_ACCESS_OK);
3788 }
3789 
3790 /*
3791  * Performs a memory read using memory access abstract commands. The read sizes
3792  * supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16 byte
3793  * aamsize fields in the memory access abstract command.
3794  */
3795 static struct mem_access_result
3796 read_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
3797 {
3798  assert(riscv_mem_access_is_read(args));
3799 
3800  memset(args.read_buffer, 0, args.count * args.size);
3801 
3802  /* Convert the size (bytes) to width (bits) */
3803  unsigned int width = args.size << 3;
3804 
3805  uint32_t command = access_memory_command(target, /* virtual = */ false,
3806  width, /* postincrement = */ true, /* is_write = */ false);
3807  bool use_aampostincrement = !is_command_unsupported(target, command);
3808  if (!use_aampostincrement)
3809  /* It is already known that this abstract memory
3810  * access with aampostincrement=1 is not supported.
3811  * So try aampostincrement=0 right away.
3812  *
3813  * TODO: check if new command is supported */
3814  command = access_memory_command(target, /* virtual = */ false,
3815  width, /* postincrement = */ false, /* is_write = */ false);
3816 
3817  /* Execute the reads */
3818  uint8_t *p = args.read_buffer;
3819  int result = ERROR_OK;
3820  bool updateaddr = true;
3821  unsigned int width32 = MAX(width, 32);
3822  for (uint32_t c = 0; c < args.count; c++) {
3823  /* Update the address if it is the first time or aampostincrement is not supported by the target. */
3824  if (updateaddr) {
3825  /* Set arg1 to the address: address + c * size */
3826  result = write_abstract_arg(target, 1, args.address + c * args.size, riscv_xlen(target));
3827  if (result != ERROR_OK) {
3828  LOG_TARGET_ERROR(target, "Failed to write arg1.");
3829  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3830  }
3831  }
3832 
3833  /* Execute the command */
3834  uint32_t cmderr;
3835  result = riscv013_execute_abstract_command(target, command, &cmderr);
3836  if (use_aampostincrement && result != ERROR_OK &&
3837  cmderr == CMDERR_NOT_SUPPORTED) {
3838  LOG_TARGET_DEBUG(target, "Trying the same abstract memory "
3839  "read command, but without aampostincrement");
3840  use_aampostincrement = false;
3841  command = access_memory_command(target, /* virtual = */ false,
3842  width, /* postincrement = */ false, /* is_write = */ false);
3843  result = riscv013_execute_abstract_command(target, command, &cmderr);
3844  }
3845 
3846  /* TODO:
3847  * (1) Only the 1st access can result in a 'skip'
3848  * (2) Analyze cmderr value */
3849  if (result != ERROR_OK)
3850  return mem_access_result(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR);
3851 
3852  /* Copy arg0 to buffer (rounded width up to nearest 32) */
3853  riscv_reg_t value;
3854  result = read_abstract_arg(target, &value, 0, width32);
3855  if (result != ERROR_OK)
3856  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3857  buf_set_u64(p, 0, 8 * args.size, value);
3858 
3859  if (use_aampostincrement)
3860  updateaddr = false;
3861  p += args.size;
3862  }
3863 
3864  return mem_access_result(MEM_ACCESS_OK);
3865 }
3866 
3867 /*
3868  * Performs a memory write using memory access abstract commands. The write
3869  * sizes supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16
3870  * byte aamsize fields in the memory access abstract command.
3871  */
3872 static struct mem_access_result
3873 write_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
3874 {
3875  assert(riscv_mem_access_is_write(args));
3876 
3877  int result = ERROR_OK;
3878 
3879  /* Convert the size (bytes) to width (bits) */
3880  unsigned int width = args.size << 3;
3881 
3882  uint32_t command = access_memory_command(target, /* virtual = */ false,
3883  width, /* postincrement = */ true, /* is_write = */ true);
3884  bool use_aampostincrement = !is_command_unsupported(target, command);
3885  if (!use_aampostincrement)
3886  /* It is already known that this abstract memory
3887  * access with aampostincrement=1 is not supported.
3888  * So try aampostincrement=0 right away.
3889  *
3890  * TODO: check if new command is supported */
3891  command = access_memory_command(target, /* virtual = */ false,
3892  width, /* postincrement = */ false, /* is_write = */ true);
3893 
3894  /* Execute the writes */
3895  const uint8_t *p = args.write_buffer;
3896  bool updateaddr = true;
3897  for (uint32_t c = 0; c < args.count; c++) {
3898  /* Move data to arg0 */
3899  riscv_reg_t value = buf_get_u64(p, 0, 8 * args.size);
3900  result = write_abstract_arg(target, 0, value, riscv_xlen(target));
3901  if (result != ERROR_OK) {
3902  LOG_TARGET_ERROR(target, "Failed to write arg0.");
3903  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3904  }
3905 
3906  /* Update the address if it is the first time or aampostincrement is not supported by the target. */
3907  if (updateaddr) {
3908  /* Set arg1 to the address: address + c * size */
3909  result = write_abstract_arg(target, 1, args.address + c * args.size, riscv_xlen(target));
3910  if (result != ERROR_OK) {
3911  LOG_TARGET_ERROR(target, "Failed to write arg1.");
3912  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3913  }
3914  }
3915 
3916  /* Execute the command */
3917  uint32_t cmderr;
3918  result = riscv013_execute_abstract_command(target, command, &cmderr);
3919  if (use_aampostincrement && result != ERROR_OK &&
3920  cmderr == CMDERR_NOT_SUPPORTED) {
3921  LOG_TARGET_DEBUG(target, "Trying the same abstract memory "
3922  "write command, but without aampostincrement");
3923  use_aampostincrement = false;
3924  command = access_memory_command(target, /* virtual = */ false,
3925  width, /* postincrement = */ false, /* is_write = */ true);
3926  result = riscv013_execute_abstract_command(target, command, &cmderr);
3927  }
3928 
3929  /* TODO:
3930  * (1) Only the 1st access can result in a 'skip'
3931  * (2) Analyze cmderr value */
3932  if (result != ERROR_OK)
3933  return mem_access_result(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR);
3934 
3935  if (use_aampostincrement)
3936  updateaddr = false;
3937  p += args.size;
3938  }
3939 
3940  return mem_access_result(MEM_ACCESS_OK);
3941 }
3942 
3953  target_addr_t address, uint32_t increment, uint32_t index)
3954 {
3955  /* s0 holds the next address to read from.
3956  * s1 holds the next data value read.
3957  * a0 is a counter in case increment is 0.
3958  */
3959  if (register_write_direct(target, GDB_REGNO_S0, address + index * increment)
3960  != ERROR_OK)
3961  return ERROR_FAIL;
3962 
3963  if (/*is_repeated_read*/ increment == 0 &&
3965  return ERROR_FAIL;
3966 
3967  /* AC_ACCESS_REGISTER_POSTEXEC is used to trigger first stage of the
3968  * pipeline (memory -> s1) whenever this command is executed.
3969  */
3970  const uint32_t startup_command = riscv013_access_register_command(target,
3973  uint32_t cmderr;
3974  if (riscv013_execute_abstract_command(target, startup_command, &cmderr) != ERROR_OK)
3975  return ERROR_FAIL;
3976  /* TODO: we need to modify error handling here. */
3977  /* NOTE: in case of timeout cmderr is set to CMDERR_NONE */
3978 
3979  /* First read has just triggered. Result is in s1.
3980  * dm_data registers contain the previous value of s1 (garbage).
3981  */
3984  return ERROR_FAIL;
3985 
3986  /* Read garbage from dm_data0, which triggers another execution of the
3987  * program. Now dm_data contains the first good result (from s1),
3988  * and s1 the next memory value.
3989  */
3991  goto clear_abstractauto_and_fail;
3992 
3993  uint32_t abstractcs;
3994  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
3995  goto clear_abstractauto_and_fail;
3996 
3997  cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
3998  switch (cmderr) {
3999  case CMDERR_NONE:
4000  return ERROR_OK;
4001  case CMDERR_BUSY:
4002  LOG_TARGET_ERROR(target, "Unexpected busy error. This is probably a hardware bug.");
4003  /* fall through */
4004  default:
4005  LOG_TARGET_DEBUG(target, "error when reading memory, cmderr=0x%" PRIx32, cmderr);
4007  goto clear_abstractauto_and_fail;
4008  }
4009 clear_abstractauto_and_fail:
4011  return ERROR_FAIL;
4012 }
4013 
4023  uint32_t start_index, uint32_t *elements_read,
4024  const struct riscv_mem_access_args args)
4025 {
4026  assert(riscv_mem_access_is_read(args));
4027 
4029  if (res != ERROR_OK)
4030  return res;
4032  if (res != ERROR_OK)
4033  return res;
4034 
4036  return ERROR_FAIL;
4037 
4038  /* See how far we got by reading s0/a0 */
4039  uint32_t index_on_target;
4040 
4041  if (/*is_repeated_read*/ args.increment == 0) {
4042  /* s0 is constant, a0 is incremented by one each execution */
4043  riscv_reg_t counter;
4044 
4045  if (register_read_direct(target, &counter, GDB_REGNO_A0) != ERROR_OK)
4046  return ERROR_FAIL;
4047  index_on_target = counter;
4048  } else {
4049  target_addr_t address_on_target;
4050 
4051  if (register_read_direct(target, &address_on_target, GDB_REGNO_S0) != ERROR_OK)
4052  return ERROR_FAIL;
4053  index_on_target = (address_on_target - args.address) /
4054  args.increment;
4055  }
4056 
4057  /* According to the spec, if an abstract command fails, one can't make any
4058  * assumptions about dm_data registers, so all the values in the pipeline
4059  * are clobbered now and need to be reread.
4060  */
4061  const uint32_t min_index_on_target = start_index + 2;
4062  if (index_on_target < min_index_on_target) {
4063  LOG_TARGET_ERROR(target, "Arithmetic does not work correctly on the target");
4064  return ERROR_FAIL;
4065  } else if (index_on_target == min_index_on_target) {
4066  LOG_TARGET_DEBUG(target, "No forward progress");
4067  }
4068  const uint32_t next_index = (index_on_target - 2);
4069  *elements_read = next_index - start_index;
4070  LOG_TARGET_WARNING(target, "Re-reading memory from addresses 0x%"
4071  TARGET_PRIxADDR " and 0x%" TARGET_PRIxADDR ".",
4072  args.address + args.increment * next_index,
4073  args.address + args.increment * (next_index + 1));
4075  args.increment, next_index);
4076 }
4077 
4082  uint32_t start_index, uint32_t next_start_index,
4083  const struct riscv_mem_access_args args)
4084 {
4085  assert(riscv_mem_access_is_read(args));
4086 
4087  LOG_TARGET_DEBUG(target, "DMI_STATUS_BUSY encountered in batch. Memory read [%"
4088  PRIu32 ", %" PRIu32 ")", start_index, next_start_index);
4089  if (start_index == next_start_index)
4090  LOG_TARGET_DEBUG(target, "No forward progress");
4091 
4093  return ERROR_FAIL;
4095  args.increment, next_start_index);
4096 }
4097 
4102  const struct riscv_batch *batch,
4103  uint32_t start_index, uint32_t elements_to_read, uint32_t *elements_read,
4104  const struct riscv_mem_access_args args)
4105 {
4106  assert(riscv_mem_access_is_read(args));
4107 
4108  const bool two_reads_per_element = args.size > 4;
4109  const uint32_t reads_per_element = (two_reads_per_element ? 2 : 1);
4110  assert(!two_reads_per_element || riscv_xlen(target) == 64);
4111  assert(elements_to_read <= UINT32_MAX / reads_per_element);
4112  const uint32_t nreads = elements_to_read * reads_per_element;
4113  for (uint32_t curr_idx = start_index, read = 0; read < nreads; ++read) {
4114  switch (riscv_batch_get_dmi_read_op(batch, read)) {
4115  case DMI_STATUS_BUSY:
4116  *elements_read = curr_idx - start_index;
4117  return read_memory_progbuf_inner_on_dmi_busy(target, start_index, curr_idx
4118  , args);
4119  case DMI_STATUS_FAILED:
4121  "Batch memory read encountered DMI_STATUS_FAILED on read %"
4122  PRIu32, read);
4123  return ERROR_FAIL;
4124  case DMI_STATUS_SUCCESS:
4125  break;
4126  default:
4127  assert(0);
4128  }
4129  const uint32_t value = riscv_batch_get_dmi_read_data(batch, read);
4130  uint8_t * const curr_buff = args.read_buffer +
4131  curr_idx * args.size;
4132  const target_addr_t curr_addr = args.address +
4133  curr_idx * args.increment;
4134  const uint32_t size = args.size;
4135 
4136  assert(size <= 8);
4137  const bool is_odd_read = read % 2;
4138 
4139  if (two_reads_per_element && !is_odd_read) {
4140  buf_set_u32(curr_buff + 4, 0, (size * 8) - 32, value);
4141  continue;
4142  }
4143  const bool is_second_read = two_reads_per_element;
4144 
4145  buf_set_u32(curr_buff, 0, is_second_read ? 32 : (size * 8), value);
4146  log_memory_access64(curr_addr, buf_get_u64(curr_buff, 0, size * 8),
4147  size, /*is_read*/ true);
4148  ++curr_idx;
4149  }
4150  *elements_read = elements_to_read;
4151  return ERROR_OK;
4152 }
4153 
4161  struct riscv_batch *batch, const struct riscv_mem_access_args args,
4162  uint32_t start_index, uint32_t elements_to_read, uint32_t *elements_read)
4163 {
4164  assert(riscv_mem_access_is_read(args));
4165 
4166  dm013_info_t *dm = get_dm(target);
4167  if (!dm)
4168  return ERROR_FAIL;
4169 
4170  /* Abstract commands are executed while running the batch. */
4171  dm->abstract_cmd_maybe_busy = true;
4172  if (batch_run(target, batch) != ERROR_OK)
4173  return ERROR_FAIL;
4174 
4175  uint32_t abstractcs;
4176  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
4177  return ERROR_FAIL;
4178 
4179  uint32_t elements_to_extract_from_batch;
4180 
4181  uint32_t cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
4182  switch (cmderr) {
4183  case CMDERR_NONE:
4184  LOG_TARGET_DEBUG(target, "successful (partial?) memory read [%"
4185  PRIu32 ", %" PRIu32 ")", start_index, start_index + elements_to_read);
4186  elements_to_extract_from_batch = elements_to_read;
4187  break;
4188  case CMDERR_BUSY:
4189  LOG_TARGET_DEBUG(target, "memory read resulted in busy response");
4191  &elements_to_extract_from_batch, args)
4192  != ERROR_OK)
4193  return ERROR_FAIL;
4194  break;
4195  default:
4196  LOG_TARGET_DEBUG(target, "error when reading memory, cmderr=0x%" PRIx32, cmderr);
4198  return ERROR_FAIL;
4199  }
4200 
4201  if (read_memory_progbuf_inner_extract_batch_data(target, batch, start_index,
4202  elements_to_extract_from_batch, elements_read, args) != ERROR_OK)
4203  return ERROR_FAIL;
4204 
4205  return ERROR_OK;
4206 }
4207 
4209  uint32_t count, uint32_t size)
4210 {
4211  assert(size <= 8);
4212  const uint32_t two_regs_used[] = {DM_DATA1, DM_DATA0};
4213  const uint32_t one_reg_used[] = {DM_DATA0};
4214  const uint32_t reads_per_element = size > 4 ? 2 : 1;
4215  const uint32_t * const used_regs = size > 4 ? two_regs_used : one_reg_used;
4216  const uint32_t batch_capacity = riscv_batch_available_scans(batch) / reads_per_element;
4217  const uint32_t end = MIN(batch_capacity, count);
4218 
4219  for (uint32_t j = 0; j < end; ++j) {
4220  /* TODO: reuse "abstract_data_read_fill_batch()" here.
4221  * TODO: Only the read of "DM_DATA0" starts an abstract
4222  * command, so the other read can use "RISCV_DELAY_BASE"
4223  */
4224  for (uint32_t i = 0; i < reads_per_element; ++i)
4225  riscv_batch_add_dm_read(batch, used_regs[i],
4227  }
4228  return end;
4229 }
4230 
4232  const struct riscv_mem_access_args args, uint32_t *elements_read,
4233  uint32_t index, uint32_t loop_count)
4234 {
4235  assert(riscv_mem_access_is_read(args));
4236 
4238  if (!batch)
4239  return ERROR_FAIL;
4240 
4241  const uint32_t elements_to_read = read_memory_progbuf_inner_fill_batch(batch,
4242  loop_count - index, args.size);
4243 
4245  args, index, elements_to_read, elements_read);
4246  riscv_batch_free(batch);
4247  return result;
4248 }
4249 
4255  const struct riscv_mem_access_args args, uint32_t start_index)
4256 {
4257  assert(riscv_mem_access_is_read(args));
4258 
4260  "Executing one loop iteration to ensure forward progress (index=%"
4261  PRIu32 ")", start_index);
4262  const target_addr_t curr_target_address = args.address +
4263  start_index * args.increment;
4264  uint8_t * const curr_buffer_address = args.read_buffer +
4265  start_index * args.size;
4266  const struct riscv_mem_access_args curr_access = {
4267  .read_buffer = curr_buffer_address,
4268  .address = curr_target_address,
4269  .size = args.size,
4270  .increment = args.increment,
4271  };
4272  uint32_t elements_read;
4273  if (read_memory_progbuf_inner_try_to_read(target, curr_access, &elements_read,
4274  /*index*/ 0, /*loop_count*/ 1) != ERROR_OK)
4275  return ERROR_FAIL;
4276 
4277  if (elements_read != 1) {
4278  assert(elements_read == 0);
4279  LOG_TARGET_DEBUG(target, "Can not ensure forward progress");
4280  /* FIXME: Here it would be better to retry the read and fail only if the
4281  * delay is greater then some threshold.
4282  */
4283  return ERROR_FAIL;
4284  }
4285  return ERROR_OK;
4286 }
4287 
4288 static void set_buffer_and_log_read(const struct riscv_mem_access_args args,
4289  uint32_t index, uint64_t value)
4290 {
4291  assert(riscv_mem_access_is_read(args));
4292 
4293  uint8_t * const buffer = args.read_buffer;
4294  const uint32_t size = args.size;
4295  const uint32_t increment = args.increment;
4296  const target_addr_t address = args.address;
4297 
4298  assert(size <= 8);
4299  buf_set_u64(buffer + index * size, 0, 8 * size, value);
4300  log_memory_access64(address + index * increment, value, size,
4301  /*is_read*/ true);
4302 }
4303 
4305  const struct riscv_mem_access_args args, uint32_t index)
4306 {
4307  assert(args.size <= 8);
4308  uint64_t value;
4309  int result = read_abstract_arg(target, &value, /*index*/ 0,
4310  args.size > 4 ? 64 : 32);
4311  if (result == ERROR_OK)
4312  set_buffer_and_log_read(args, index, value);
4313  return result;
4314 }
4315 
4316 static struct mem_access_result read_word_from_s1(struct target *target,
4317  const struct riscv_mem_access_args args, uint32_t index)
4318 {
4319  assert(riscv_mem_access_is_read(args));
4320 
4321  uint64_t value;
4322 
4324  return mem_access_result(MEM_ACCESS_FAILED_REG_READ_FAILED);
4325  set_buffer_and_log_read(args, index, value);
4326  return mem_access_result(MEM_ACCESS_OK);
4327 }
4328 
4330  uint32_t increment, uint32_t size)
4331 {
4332  const bool is_repeated_read = increment == 0;
4333 
4335  return ERROR_FAIL;
4337  return ERROR_FAIL;
4338  if (is_repeated_read && riscv013_reg_save(target, GDB_REGNO_A0) != ERROR_OK)
4339  return ERROR_FAIL;
4340 
4341  struct riscv_program program;
4342 
4343  riscv_program_init(&program, target);
4344  if (riscv_program_load(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0, size) != ERROR_OK)
4345  return ERROR_FAIL;
4346  if (is_repeated_read) {
4347  if (riscv_program_addi(&program, GDB_REGNO_A0, GDB_REGNO_A0, 1)
4348  != ERROR_OK)
4349  return ERROR_FAIL;
4350  } else {
4352  increment)
4353  != ERROR_OK)
4354  return ERROR_FAIL;
4355  }
4356  if (riscv_program_ebreak(&program) != ERROR_OK)
4357  return ERROR_FAIL;
4358  if (riscv_program_write(&program) != ERROR_OK)
4359  return ERROR_FAIL;
4360 
4361  return ERROR_OK;
4362 }
4363 
4369 static struct mem_access_result
4371 {
4372  assert(riscv_mem_access_is_read(args));
4373  assert(args.count > 1 && "If count == 1, read_memory_progbuf_inner_one must be called");
4374 
4376  args.increment, args.size) != ERROR_OK)
4377  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
4378 
4379  if (read_memory_progbuf_inner_startup(target, args.address,
4380  args.increment, /*index*/ 0) != ERROR_OK)
4381  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED);
4382  /* The program in program buffer is executed twice during
4383  * read_memory_progbuf_inner_startup().
4384  * Here:
4385  * dm_data[0:1] == M[address]
4386  * s1 == M[address + increment]
4387  * s0 == address + increment * 2
4388  * `count - 2` program executions are performed in this loop.
4389  * No need to execute the program any more, since S1 will already contain
4390  * M[address + increment * (count - 1)] and we can read it directly.
4391  */
4392  const uint32_t loop_count = args.count - 2;
4393 
4394  for (uint32_t index = 0; index < loop_count;) {
4395  uint32_t elements_read;
4396  if (read_memory_progbuf_inner_try_to_read(target, args, &elements_read,
4397  index, loop_count) != ERROR_OK) {
4399  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED);
4400  }
4401  if (elements_read == 0) {
4403  index) != ERROR_OK) {
4405  return mem_access_result(MEM_ACCESS_FAILED_NO_FORWARD_PROGRESS);
4406  }
4407  elements_read = 1;
4408  }
4409  index += elements_read;
4410  assert(index <= loop_count);
4411  }
4413  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
4414 
4415  /* Read the penultimate word. */
4417  args, args.count - 2) != ERROR_OK)
4418  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
4419  /* Read the last word. */
4420  return read_word_from_s1(target, args, args.count - 1);
4421 }
4422 
4427 static struct mem_access_result
4429 {
4430  assert(riscv_mem_access_is_read(args));
4431 
4433  return mem_access_result(MEM_ACCESS_SKIPPED_REG_SAVE_FAILED);
4434 
4435  struct riscv_program program;
4436 
4437  riscv_program_init(&program, target);
4439  /* offset = */ 0, args.size) != ERROR_OK
4440  || riscv_program_ebreak(&program) != ERROR_OK)
4441  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
4442 
4443  if (riscv_program_write(&program) != ERROR_OK)
4444  return mem_access_result(MEM_ACCESS_SKIPPED_PROGRAM_WRITE_FAILED);
4445 
4446  /* Write address to S1, and execute buffer. */
4447  if (write_abstract_arg(target, /* index = */ 0,
4448  args.address, riscv_xlen(target)) != ERROR_OK)
4449  return mem_access_result(MEM_ACCESS_SKIPPED_WRITE_ABSTRACT_ARG_FAILED);
4453  uint32_t cmderr;
4455  return mem_access_result(MEM_ACCESS_FAILED_EXECUTE_ABSTRACT_FAILED);
4456 
4457  return read_word_from_s1(target, args, 0);
4458 }
4459 
4463 static struct mem_access_result
4464 read_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
4465 {
4466  assert(riscv_mem_access_is_read(args));
4467 
4468  select_dmi(target->tap);
4469  memset(args.read_buffer, 0, args.count * args.size);
4470 
4472  return mem_access_result(MEM_ACCESS_SKIPPED_FENCE_EXEC_FAILED);
4473 
4474  return (args.count == 1) ?
4477 }
4478 
4479 static struct mem_access_result
4480 write_memory_progbuf(struct target *target, const struct riscv_mem_access_args args);
4481 
4482 static struct mem_access_result
4483 access_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
4484 {
4485  struct mem_access_result skip_reason = mem_should_skip_progbuf(target, args);
4486  if (!is_mem_access_ok(skip_reason))
4487  return skip_reason;
4488 
4489  const bool is_read = riscv_mem_access_is_read(args);
4490  const char *const access_type = is_read ? "reading" : "writing";
4491  LOG_TARGET_DEBUG(target, "%s %" PRIu32 " words of %" PRIu32
4492  " bytes at 0x%" TARGET_PRIxADDR, access_type, args.count,
4493  args.size, args.address);
4494 
4496  return mem_access_result(MEM_ACCESS_SKIPPED_TARGET_SELECT_FAILED);
4497 
4498  riscv_reg_t mstatus = 0;
4499  riscv_reg_t mstatus_old = 0;
4500  riscv_reg_t dcsr = 0;
4501  riscv_reg_t dcsr_old = 0;
4503  &mstatus, &mstatus_old, &dcsr, &dcsr_old) != ERROR_OK)
4504  return mem_access_result(MEM_ACCESS_SKIPPED_PRIV_MOD_FAILED);
4505 
4506  struct mem_access_result result = is_read ?
4507  read_memory_progbuf(target, args) :
4509 
4511  mstatus, mstatus_old, dcsr, dcsr_old) != ERROR_OK)
4512  return mem_access_result(MEM_ACCESS_FAILED_PRIV_MOD_FAILED);
4513 
4514  return result;
4515 }
4516 
4517 static int
4518 write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args);
4519 static int
4521 
4522 static struct mem_access_result
4523 access_memory_sysbus(struct target *target, const struct riscv_mem_access_args args)
4524 {
4525  assert(riscv_mem_access_is_valid(args));
4526 
4527  struct mem_access_result skip_reason = mem_should_skip_sysbus(target, args);
4528  if (!is_mem_access_ok(skip_reason))
4529  return skip_reason;
4530 
4532  int ret = ERROR_FAIL;
4533  const bool is_read = riscv_mem_access_is_read(args);
4534  const uint64_t sbver = get_field(info->sbcs, DM_SBCS_SBVERSION);
4535  if (sbver == 0) {
4536  ret = is_read ? read_memory_bus_v0(target, args) :
4537  write_memory_bus_v0(target, args);
4538  } else if (sbver == 1) {
4539  ret = is_read ? read_memory_bus_v1(target, args) :
4540  write_memory_bus_v1(target, args);
4541  } else {
4542  LOG_TARGET_ERROR(target, "Unknown system bus version: %" PRIu64, sbver);
4543  return mem_access_result(MEM_ACCESS_SKIPPED_UNKNOWN_SYSBUS_VERSION);
4544  }
4545 
4546  return mem_access_result(ret == ERROR_OK ?
4547  MEM_ACCESS_OK : MEM_ACCESS_SKIPPED_SYSBUS_ACCESS_FAILED);
4548 }
4549 
4550 static struct mem_access_result
4551 access_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
4552 {
4553  assert(riscv_mem_access_is_valid(args));
4554 
4555  struct mem_access_result skip_reason = mem_should_skip_abstract(target, args);
4556  if (!is_mem_access_ok(skip_reason))
4557  return skip_reason;
4558 
4559  const bool is_read = riscv_mem_access_is_read(args);
4560  const char *const access_type = is_read ? "reading" : "writing";
4561  LOG_TARGET_DEBUG(target, "%s %d words of %d bytes at 0x%"
4562  TARGET_PRIxADDR, access_type, args.count,
4563  args.size, args.address);
4564 
4565  return is_read ? read_memory_abstract(target, args) :
4567 }
4568 
4569 static int
4571 {
4572  assert(riscv_mem_access_is_valid(args));
4573 
4574  const bool is_read = riscv_mem_access_is_read(args);
4575  const char *const access_type = is_read ? "read" : "write";
4576  if (!is_read && args.increment != args.size) {
4577  LOG_TARGET_ERROR(target, "Write increment size has to be equal to element size");
4578  return ERROR_NOT_IMPLEMENTED;
4579  }
4580 
4581  if (!IS_PWR_OF_2(args.size) || args.size < 1 || args.size > 16) {
4582  LOG_TARGET_ERROR(target, "BUG: Unsupported size for "
4583  "memory %s: %d", access_type, args.size);
4584  return ERROR_FAIL;
4585  }
4586 
4587  struct mem_access_result skip_reason[] = {
4588  [RISCV_MEM_ACCESS_PROGBUF] = mem_access_result(MEM_ACCESS_DISABLED),
4589  [RISCV_MEM_ACCESS_SYSBUS] = mem_access_result(MEM_ACCESS_DISABLED),
4590  [RISCV_MEM_ACCESS_ABSTRACT] = mem_access_result(MEM_ACCESS_DISABLED),
4591  };
4592 
4593  RISCV_INFO(r);
4594  for (unsigned int i = 0; i < r->num_enabled_mem_access_methods; ++i) {
4595  enum riscv_mem_access_method method = r->mem_access_methods[i];
4596  switch (method) {
4598  skip_reason[method] = access_memory_progbuf(target, args);
4599  break;
4601  skip_reason[method] = access_memory_sysbus(target, args);
4602  break;
4604  skip_reason[method] = access_memory_abstract(target, args);
4605  break;
4606  default:
4607  LOG_TARGET_ERROR(target, "Unknown memory access method: %d", method);
4608  assert(false && "Unknown memory access method");
4609  goto failure;
4610  }
4611 
4612  if (is_mem_access_failed(skip_reason[method]))
4613  goto failure;
4614 
4615  const bool success = is_mem_access_ok(skip_reason[method]);
4616  log_mem_access_result(target, success, method, is_read);
4617  if (success)
4618  return ERROR_OK;
4619  }
4620 
4621 failure:
4622  LOG_TARGET_ERROR(target, "Failed to %s memory (addr=0x%" PRIx64 ")\n"
4623  " progbuf=%s, sysbus=%s, abstract=%s", access_type, args.address,
4627  return ERROR_FAIL;
4628 }
4629 
4630 static int write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
4631 {
4632  assert(riscv_mem_access_is_write(args));
4633 
4634  /*1) write sbaddress: for singlewrite and autoincrement, we need to write the address once*/
4635  LOG_TARGET_DEBUG(target, "System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
4636  TARGET_PRIxADDR, args.size, args.count, args.address);
4638  int64_t value = 0;
4639  int64_t access = 0;
4640  riscv_addr_t offset = 0;
4641  riscv_addr_t t_addr = 0;
4642  const uint8_t *t_buffer = args.write_buffer + offset;
4643 
4644  /* B.8 Writing Memory, single write check if we write in one go */
4645  if (args.count == 1) { /* count is in bytes here */
4646  value = buf_get_u64(t_buffer, 0, 8 * args.size);
4647 
4648  access = 0;
4649  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
4650  dm_write(target, DM_SBCS, access);
4651  LOG_TARGET_DEBUG(target, " access: 0x%08" PRIx64, access);
4652  LOG_TARGET_DEBUG(target, " write_memory:SAB: ONE OFF: value 0x%08" PRIx64, value);
4654  return ERROR_OK;
4655  }
4656 
4657  /*B.8 Writing Memory, using autoincrement*/
4658 
4659  access = 0;
4660  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
4661  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
4662  LOG_TARGET_DEBUG(target, " access: 0x%08" PRIx64, access);
4663  dm_write(target, DM_SBCS, access);
4664 
4665  /*2)set the value according to the size required and write*/
4666  for (riscv_addr_t i = 0; i < args.count; ++i) {
4667  offset = args.size * i;
4668  /* for monitoring only */
4669  t_addr = args.address + offset;
4670  t_buffer = args.write_buffer + offset;
4671 
4672  value = buf_get_u64(t_buffer, 0, 8 * args.size);
4673  LOG_TARGET_DEBUG(target, "SAB:autoincrement: expected address: 0x%08x value: 0x%08x"
4674  PRIx64, (uint32_t)t_addr, (uint32_t)value);
4676  }
4677  /*reset the autoincrement when finished (something weird is happening if this is not done at the end*/
4678  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 0);
4679  dm_write(target, DM_SBCS, access);
4680 
4681  return ERROR_OK;
4682 }
4683 
4684 static int write_memory_bus_v1(struct target *target, const struct riscv_mem_access_args args)
4685 {
4686  assert(riscv_mem_access_is_write(args));
4687 
4689  uint32_t sbcs = sb_sbaccess(args.size);
4690  sbcs = set_field(sbcs, DM_SBCS_SBAUTOINCREMENT, 1);
4691  dm_write(target, DM_SBCS, sbcs);
4692 
4693  target_addr_t next_address = args.address;
4694  target_addr_t end_address = args.address + args.count * args.size;
4695 
4696  int result = sb_write_address(target, next_address, RISCV_DELAY_BASE);
4697  if (result != ERROR_OK)
4698  return result;
4699 
4700  while (next_address < end_address) {
4701  LOG_TARGET_DEBUG(target, "Transferring burst starting at address 0x%" TARGET_PRIxADDR,
4702  next_address);
4703 
4705  if (!batch)
4706  return ERROR_FAIL;
4707 
4708  for (uint32_t i = (next_address - args.address) / args.size; i < args.count; i++) {
4709  const uint8_t *p = args.write_buffer + i * args.size;
4710 
4711  if (riscv_batch_available_scans(batch) < (args.size + 3) / 4)
4712  break;
4713 
4714  uint32_t sbvalue[4] = { 0 };
4715  if (args.size > 12) {
4716  sbvalue[3] = buf_get_u32(&p[12],
4717  /* first = */ 0, /* bit_num = */ 32);
4718  riscv_batch_add_dm_write(batch, DM_SBDATA3, sbvalue[3], false,
4720  }
4721 
4722  if (args.size > 8) {
4723  sbvalue[2] = buf_get_u32(&p[8],
4724  /* first = */ 0, /* bit_num = */ 32);
4725  riscv_batch_add_dm_write(batch, DM_SBDATA2, sbvalue[2], false,
4727  }
4728  if (args.size > 4) {
4729  sbvalue[1] = buf_get_u32(&p[4],
4730  /* first = */ 0, /* bit_num = */ 32);
4731  riscv_batch_add_dm_write(batch, DM_SBDATA1, sbvalue[1], false,
4733  }
4734 
4735  sbvalue[0] = p[0];
4736  if (args.size > 2) {
4737  sbvalue[0] |= ((uint32_t)p[2]) << 16;
4738  sbvalue[0] |= ((uint32_t)p[3]) << 24;
4739  }
4740  if (args.size > 1)
4741  sbvalue[0] |= ((uint32_t)p[1]) << 8;
4742 
4743  riscv_batch_add_dm_write(batch, DM_SBDATA0, sbvalue[0], false,
4745 
4746  log_memory_access(args.address + i * args.size, sbvalue, args.size, false);
4747 
4748  next_address += args.size;
4749  }
4750 
4751  /* Execute the batch of writes */
4752  result = batch_run(target, batch);
4753  if (result != ERROR_OK) {
4754  riscv_batch_free(batch);
4755  return result;
4756  }
4757 
4758  bool dmi_busy_encountered = riscv_batch_was_batch_busy(batch);
4759  riscv_batch_free(batch);
4760  if (dmi_busy_encountered)
4761  LOG_TARGET_DEBUG(target, "DMI busy encountered during system bus write.");
4762 
4763  result = read_sbcs_nonbusy(target, &sbcs);
4764  if (result != ERROR_OK)
4765  return result;
4766 
4767  if (get_field(sbcs, DM_SBCS_SBBUSYERROR)) {
4768  /* We wrote while the target was busy. */
4769  LOG_TARGET_DEBUG(target, "Sbbusyerror encountered during system bus write.");
4770  /* Clear the sticky error flag. */
4772  /* Slow down before trying again.
4773  * FIXME: Possible overflow is ignored here.
4774  */
4775  riscv_scan_increase_delay(&info->learned_delays,
4777  }
4778 
4779  if (get_field(sbcs, DM_SBCS_SBBUSYERROR) || dmi_busy_encountered) {
4780  /* Recover from the case when the write commands were issued too fast.
4781  * Determine the address from which to resume writing. */
4782  next_address = sb_read_address(target);
4783  if (next_address < args.address) {
4784  /* This should never happen, probably buggy hardware. */
4785  LOG_TARGET_DEBUG(target, "unexpected sbaddress=0x%" TARGET_PRIxADDR
4786  " - buggy sbautoincrement in hw?", next_address);
4787  /* Fail the whole operation. */
4788  return ERROR_FAIL;
4789  }
4790  /* Try again - resume writing. */
4791  continue;
4792  }
4793 
4794  unsigned int sberror = get_field(sbcs, DM_SBCS_SBERROR);
4795  if (sberror != 0) {
4796  /* Sberror indicates the bus access failed, but not because we issued the writes
4797  * too fast. Cannot recover. Sbaddress holds the address where the error occurred
4798  * (unless sbautoincrement in the HW is buggy).
4799  */
4800  target_addr_t sbaddress = sb_read_address(target);
4801  LOG_TARGET_DEBUG(target, "System bus access failed with sberror=%u (sbaddress=0x%" TARGET_PRIxADDR ")",
4802  sberror, sbaddress);
4803  if (sbaddress < args.address) {
4804  /* This should never happen, probably buggy hardware.
4805  * Make a note to the user not to trust the sbaddress value. */
4806  LOG_TARGET_DEBUG(target, "unexpected sbaddress=0x%" TARGET_PRIxADDR
4807  " - buggy sbautoincrement in hw?", next_address);
4808  }
4809  /* Clear the sticky error flag */
4811  /* Fail the whole operation */
4812  return ERROR_FAIL;
4813  }
4814  }
4815 
4816  return ERROR_OK;
4817 }
4818 
4831  const uint8_t *buffer, uint32_t size)
4832 {
4833  /* TODO: There is potential to gain some performance if the operations below are
4834  * executed inside the first DMI batch (not separately). */
4835  if (register_write_direct(target, GDB_REGNO_S0, *address_p) != ERROR_OK)
4836  return ERROR_FAIL;
4837 
4838  /* Write the first item to data0 [, data1] */
4839  assert(size <= 8);
4840  const uint64_t value = buf_get_u64(buffer, 0, 8 * size);
4841  if (write_abstract_arg(target, /*index*/ 0, value, size > 4 ? 64 : 32)
4842  != ERROR_OK)
4843  return ERROR_FAIL;
4844 
4845  /* Write and execute command that moves the value from data0 [, data1]
4846  * into S1 and executes program buffer. */
4852 
4853  uint32_t cmderr;
4855  return ERROR_FAIL;
4856 
4857  log_memory_access64(*address_p, value, size, /*is_read*/ false);
4858 
4859  /* The execution of the command succeeded, which means:
4860  * - write of the first item to memory succeeded
4861  * - address on the target (S0) was incremented
4862  */
4863  *address_p += size;
4864 
4865  /* TODO: Setting abstractauto.autoexecdata is not necessary for a write
4866  * of one element. */
4869 }
4870 
4875 {
4876  return dm_write(target, DM_ABSTRACTAUTO, 0);
4877 }
4878 
4885  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
4886  const uint8_t *buffer)
4887 {
4889  if (res != ERROR_OK)
4890  return res;
4892  if (res != ERROR_OK)
4893  return res;
4894 
4896  return ERROR_FAIL;
4897 
4898  target_addr_t address_on_target;
4899  if (register_read_direct(target, &address_on_target, GDB_REGNO_S0) != ERROR_OK)
4900  return ERROR_FAIL;
4901  const uint8_t * const curr_buff = buffer + (address_on_target - *address_p);
4902  *address_p = address_on_target;
4903  if (*address_p == end_address) {
4904  LOG_TARGET_DEBUG(target, "Got busy while reading after reading the last element");
4905  return ERROR_OK;
4906  }
4907  LOG_TARGET_DEBUG(target, "Restarting from 0x%" TARGET_PRIxADDR, *address_p);
4908  /* This restores the pipeline and ensures one item gets reliably written */
4909  return write_memory_progbuf_startup(target, address_p, curr_buff, size);
4910 }
4911 
4917  target_addr_t start_address, target_addr_t end_address, uint32_t size,
4918  const uint8_t *buffer)
4919 {
4920  assert(size <= 8);
4921  const unsigned int writes_per_element = size > 4 ? 2 : 1;
4922  const size_t batch_capacity = riscv_batch_available_scans(batch) / writes_per_element;
4923  /* This is safe even for the edge case when writing at the very top of
4924  * the 64-bit address space (in which case end_address overflows to 0).
4925  */
4926  const target_addr_t batch_end_address = start_address +
4927  MIN((target_addr_t)batch_capacity * size,
4928  end_address - start_address);
4929  for (target_addr_t address = start_address; address != batch_end_address;
4930  address += size, buffer += size) {
4931  assert(size <= 8);
4932  const uint64_t value = buf_get_u64(buffer, 0, 8 * size);
4933  log_memory_access64(address, value, size, /*is_read*/ false);
4934  if (writes_per_element == 2)
4936  (uint32_t)(value >> 32), false, RISCV_DELAY_BASE);
4937  riscv_batch_add_dm_write(batch, DM_DATA0, (uint32_t)value, false,
4939  }
4940  return batch_end_address;
4941 }
4942 
4947 static int write_memory_progbuf_run_batch(struct target *target, struct riscv_batch *batch,
4948  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
4949  const uint8_t *buffer)
4950 {
4951  dm013_info_t *dm = get_dm(target);
4952  if (!dm)
4953  return ERROR_FAIL;
4954 
4955  /* Abstract commands are executed while running the batch. */
4956  dm->abstract_cmd_maybe_busy = true;
4957  if (batch_run(target, batch) != ERROR_OK)
4958  return ERROR_FAIL;
4959 
4960  /* Note that if the scan resulted in a Busy DMI response, it
4961  * is this call to wait_for_idle() that will cause the dmi_busy_delay
4962  * to be incremented if necessary. */
4963  uint32_t abstractcs;
4964 
4965  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
4966  return ERROR_FAIL;
4967 
4968  uint32_t cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
4969  const bool dmi_busy_encountered = riscv_batch_was_batch_busy(batch);
4970  if (cmderr == CMDERR_NONE && !dmi_busy_encountered) {
4971  LOG_TARGET_DEBUG(target, "Successfully written memory block M[0x%" TARGET_PRIxADDR
4972  ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
4973  *address_p = end_address;
4974  return ERROR_OK;
4975  } else if (cmderr == CMDERR_BUSY || dmi_busy_encountered) {
4976  if (cmderr == CMDERR_BUSY)
4977  LOG_TARGET_DEBUG(target, "Encountered abstract command busy response while writing block M[0x%"
4978  TARGET_PRIxADDR ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
4979  if (dmi_busy_encountered)
4980  LOG_TARGET_DEBUG(target, "Encountered DMI busy response while writing block M[0x%"
4981  TARGET_PRIxADDR ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
4982  /* TODO: If dmi busy is encountered, the address of the last
4983  * successful write can be deduced by analysing the batch.
4984  */
4985  return write_memory_progbuf_handle_busy(target, address_p, end_address,
4986  size, buffer);
4987  }
4988  LOG_TARGET_ERROR(target, "Error when writing memory, abstractcs=0x%" PRIx32,
4989  abstractcs);
4991  return ERROR_FAIL;
4992 }
4993 
4995  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
4996  const uint8_t *buffer)
4997 {
4999  if (!batch)
5000  return ERROR_FAIL;
5001 
5002  const target_addr_t batch_end_addr = write_memory_progbuf_fill_batch(batch,
5003  *address_p, end_address, size, buffer);
5004 
5005  int result = write_memory_progbuf_run_batch(target, batch, address_p,
5006  batch_end_addr, size, buffer);
5007  riscv_batch_free(batch);
5008  return result;
5009 }
5010 
5012 {
5014  return ERROR_FAIL;
5016  return ERROR_FAIL;
5017 
5018  struct riscv_program program;
5019 
5020  riscv_program_init(&program, target);
5022  return ERROR_FAIL;
5023 
5024  if (riscv_program_addi(&program, GDB_REGNO_S0, GDB_REGNO_S0, (int16_t)size) != ERROR_OK)
5025  return ERROR_FAIL;
5026 
5027  if (riscv_program_ebreak(&program) != ERROR_OK)
5028  return ERROR_FAIL;
5029 
5030  return riscv_program_write(&program);
5031 }
5032 
5033 static struct mem_access_result
5035  const struct riscv_mem_access_args args)
5036 {
5037  assert(riscv_mem_access_is_write(args));
5038 
5040  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
5041 
5042  target_addr_t addr_on_target = args.address;
5043  if (write_memory_progbuf_startup(target, &addr_on_target,
5044  args.write_buffer, args.size) != ERROR_OK)
5045  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED);
5046 
5047  const target_addr_t end_addr = args.address + (target_addr_t)args.size * args.count;
5048 
5049  for (target_addr_t next_addr_on_target = addr_on_target; addr_on_target != end_addr;
5050  addr_on_target = next_addr_on_target) {
5051  const uint8_t * const curr_buff = args.write_buffer + (addr_on_target - args.address);
5052  if (write_memory_progbuf_try_to_write(target, &next_addr_on_target,
5053  end_addr, args.size, curr_buff) != ERROR_OK) {
5055  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED);
5056  }
5057  /* write_memory_progbuf_try_to_write() ensures that at least one item
5058  * gets successfully written even when busy condition is encountered.
5059  * These assertions shuld hold when next_address_on_target overflows. */
5060  assert(next_addr_on_target - addr_on_target > 0);
5061  assert(next_addr_on_target - args.address <= (target_addr_t)args.size * args.count);
5062  }
5063 
5065  mem_access_result(MEM_ACCESS_OK) :
5066  mem_access_result(MEM_ACCESS_FAILED_PROGBUF_TEARDOWN_FAILED);
5067 }
5068 
5069 static struct mem_access_result
5070 write_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
5071 {
5072  assert(riscv_mem_access_is_write(args));
5073 
5074  struct mem_access_result result = write_memory_progbuf_inner(target, args);
5075 
5077  return mem_access_result(MEM_ACCESS_FAILED_FENCE_EXEC_FAILED);
5078 
5079  return result;
5080 }
5081 
5082 static bool riscv013_get_impebreak(const struct target *target)
5083 {
5084  RISCV013_INFO(r);
5085  return r->impebreak;
5086 }
5087 
5088 static unsigned int riscv013_get_progbufsize(const struct target *target)
5089 {
5090  RISCV013_INFO(r);
5091  return r->progbufsize;
5092 }
5093 
5094 
5095 struct target_type riscv013_target = {
5096  .name = "riscv",
5097 
5098  .init_target = init_target,
5099  .deinit_target = deinit_target,
5100  .examine = examine,
5101 
5102  .poll = &riscv_openocd_poll,
5103  .halt = &riscv_halt,
5104  .step = &riscv_openocd_step,
5105 
5106  .assert_reset = assert_reset,
5107  .deassert_reset = deassert_reset,
5108 };
5109 
5110 /*** 0.13-specific implementations of various RISC-V helper functions. ***/
5112  riscv_reg_t *value, enum gdb_regno rid)
5113 {
5114  /* It would be beneficial to move this redirection to the
5115  * version-independent section, but there is a conflict:
5116  * `dcsr[5]` is `dcsr.v` in current spec, but it is `dcsr.debugint` in 0.11.
5117  */
5118  if (rid == GDB_REGNO_PRIV) {
5119  uint64_t dcsr;
5120  if (riscv_reg_get(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
5121  return ERROR_FAIL;
5122  *value = set_field(0, VIRT_PRIV_V, get_field(dcsr, CSR_DCSR_V));
5123  *value = set_field(*value, VIRT_PRIV_PRV, get_field(dcsr, CSR_DCSR_PRV));
5124  return ERROR_OK;
5125  }
5126 
5127  LOG_TARGET_DEBUG(target, "reading register %s", riscv_reg_gdb_regno_name(target, rid));
5128 
5130  return ERROR_FAIL;
5131 
5132  if (register_read_direct(target, value, rid) != ERROR_OK) {
5133  *value = -1;
5134  return ERROR_FAIL;
5135  }
5136 
5137  return ERROR_OK;
5138 }
5139 
5141  riscv_reg_t value)
5142 {
5143  LOG_TARGET_DEBUG(target, "writing 0x%" PRIx64 " to register %s",
5145 
5147  return ERROR_FAIL;
5148 
5149  return register_write_direct(target, rid, value);
5150 }
5151 
5152 static int dm013_select_hart(struct target *target, int hart_index)
5153 {
5154  dm013_info_t *dm = get_dm(target);
5155  if (!dm)
5156  return ERROR_FAIL;
5157  if (hart_index == dm->current_hartid)
5158  return ERROR_OK;
5159 
5160  /* `hartsel` should not be changed if `abstractcs.busy` is set. */
5161  int result = wait_for_idle_if_needed(target);
5162  if (result != ERROR_OK)
5163  return result;
5164 
5165  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE;
5166  dmcontrol = set_dmcontrol_hartsel(dmcontrol, hart_index);
5167  if (dm_write(target, DM_DMCONTROL, dmcontrol) != ERROR_OK) {
5168  /* Who knows what the state is? */
5170  return ERROR_FAIL;
5171  }
5172  dm->current_hartid = hart_index;
5173  return ERROR_OK;
5174 }
5175 
5176 /* Select all harts that were prepped and that are selectable, clearing the
5177  * prepped flag on the harts that actually were selected. */
5179 {
5180  RISCV_INFO(r);
5181  dm013_info_t *dm = get_dm(target);
5182  if (!dm)
5183  return ERROR_FAIL;
5184  if (!dm->hasel_supported) {
5185  r->prepped = false;
5186  return dm013_select_target(target);
5187  }
5188 
5189  assert(dm->hart_count);
5190  unsigned int hawindow_count = (dm->hart_count + 31) / 32;
5191  uint32_t *hawindow = calloc(hawindow_count, sizeof(uint32_t));
5192  if (!hawindow)
5193  return ERROR_FAIL;
5194 
5195  target_list_t *entry;
5196  unsigned int total_selected = 0;
5197  unsigned int selected_index = 0;
5198  list_for_each_entry(entry, &dm->target_list, list) {
5199  struct target *t = entry->target;
5200  struct riscv_info *info = riscv_info(t);
5201  riscv013_info_t *info_013 = get_info(t);
5202  unsigned int index = info_013->index;
5203  LOG_TARGET_DEBUG(target, "index=%d, prepped=%d", index, info->prepped);
5204  if (info->prepped) {
5205  info_013->selected = true;
5206  hawindow[index / 32] |= 1 << (index % 32);
5207  info->prepped = false;
5208  total_selected++;
5209  selected_index = index;
5210  }
5211  }
5212 
5213  if (total_selected == 0) {
5214  LOG_TARGET_ERROR(target, "No harts were prepped!");
5215  free(hawindow);
5216  return ERROR_FAIL;
5217  } else if (total_selected == 1) {
5218  /* Don't use hasel if we only need to talk to one hart. */
5219  free(hawindow);
5220  return dm013_select_hart(target, selected_index);
5221  }
5222 
5224  free(hawindow);
5225  return ERROR_FAIL;
5226  }
5227 
5228  for (unsigned int i = 0; i < hawindow_count; i++) {
5229  if (dm_write(target, DM_HAWINDOWSEL, i) != ERROR_OK) {
5230  free(hawindow);
5231  return ERROR_FAIL;
5232  }
5233  if (dm_write(target, DM_HAWINDOW, hawindow[i]) != ERROR_OK) {
5234  free(hawindow);
5235  return ERROR_FAIL;
5236  }
5237  }
5238 
5239  free(hawindow);
5240  return ERROR_OK;
5241 }
5242 
5243 static int riscv013_halt_prep(struct target *target)
5244 {
5245  return ERROR_OK;
5246 }
5247 
5248 static int riscv013_halt_go(struct target *target)
5249 {
5250  dm013_info_t *dm = get_dm(target);
5251  if (!dm)
5252  return ERROR_FAIL;
5253 
5255  return ERROR_FAIL;
5256 
5257  LOG_TARGET_DEBUG(target, "halting hart");
5258 
5259  /* `haltreq` should not be issued if `abstractcs.busy` is set. */
5260  int result = wait_for_idle_if_needed(target);
5261  if (result != ERROR_OK)
5262  return result;
5263 
5264  /* Issue the halt command, and then wait for the current hart to halt. */
5265  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_HALTREQ;
5266  dmcontrol = set_dmcontrol_hartsel(dmcontrol, dm->current_hartid);
5267  dm_write(target, DM_DMCONTROL, dmcontrol);
5268  uint32_t dmstatus;
5269  for (size_t i = 0; i < 256; ++i) {
5270  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5271  return ERROR_FAIL;
5272  /* When no harts are running, there's no point in continuing this loop. */
5273  if (!get_field(dmstatus, DM_DMSTATUS_ANYRUNNING))
5274  break;
5275  }
5276 
5277  /* We declare success if no harts are running. One or more of them may be
5278  * unavailable, though. */
5279 
5280  if ((get_field(dmstatus, DM_DMSTATUS_ANYRUNNING))) {
5281  if (dm_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
5282  return ERROR_FAIL;
5283 
5284  LOG_TARGET_ERROR(target, "Unable to halt. dmcontrol=0x%08x, dmstatus=0x%08x",
5285  dmcontrol, dmstatus);
5286  return ERROR_FAIL;
5287  }
5288 
5289  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_HALTREQ, 0);
5290  dm_write(target, DM_DMCONTROL, dmcontrol);
5291 
5292  if (dm->current_hartid == HART_INDEX_MULTIPLE) {
5293  target_list_t *entry;
5294  list_for_each_entry(entry, &dm->target_list, list) {
5295  struct target *t = entry->target;
5296  uint32_t t_dmstatus;
5297  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED) ||
5298  get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5299  /* All harts are either halted or unavailable. No
5300  * need to read dmstatus for each hart. */
5301  t_dmstatus = dmstatus;
5302  } else {
5303  /* Only some harts were halted/unavailable. Read
5304  * dmstatus for this one to see what its status
5305  * is. */
5307  return ERROR_FAIL;
5308  if (dm_read(target, &t_dmstatus, DM_DMSTATUS) != ERROR_OK)
5309  return ERROR_FAIL;
5310  }
5311  /* Set state for the current target based on its dmstatus. */
5312  if (get_field(t_dmstatus, DM_DMSTATUS_ALLHALTED)) {
5313  t->state = TARGET_HALTED;
5316  } else if (get_field(t_dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5318  }
5319  }
5320 
5321  } else {
5322  /* Set state for the current target based on its dmstatus. */
5323  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED)) {
5327  } else if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5329  }
5330  }
5331 
5332  return ERROR_OK;
5333 }
5334 
5335 static int riscv013_resume_go(struct target *target)
5336 {
5338  return ERROR_FAIL;
5339 
5341 }
5342 
5344 {
5346 }
5347 
5349 {
5350  assert(target->state == TARGET_HALTED);
5351  return riscv013_on_step_or_resume(target, false);
5352 }
5353 
5354 static int riscv013_on_step(struct target *target)
5355 {
5356  return riscv013_on_step_or_resume(target, true);
5357 }
5358 
5360 {
5361  riscv_reg_t dcsr;
5362  int result = register_read_direct(target, &dcsr, GDB_REGNO_DCSR);
5363  if (result != ERROR_OK)
5364  return RISCV_HALT_UNKNOWN;
5365 
5366  LOG_TARGET_DEBUG(target, "dcsr.cause: 0x%" PRIx64, get_field(dcsr, CSR_DCSR_CAUSE));
5367 
5368  switch (get_field(dcsr, CSR_DCSR_CAUSE)) {
5369  case CSR_DCSR_CAUSE_EBREAK:
5370  return RISCV_HALT_EBREAK;
5372  /* We could get here before triggers are enumerated if a trigger was
5373  * already set when we connected. Force enumeration now, which has the
5374  * side effect of clearing any triggers we did not set. */
5376  LOG_TARGET_DEBUG(target, "halted because of trigger");
5377  return RISCV_HALT_TRIGGER;
5378  case CSR_DCSR_CAUSE_STEP:
5379  return RISCV_HALT_SINGLESTEP;
5382  return RISCV_HALT_INTERRUPT;
5383  case CSR_DCSR_CAUSE_GROUP:
5384  return RISCV_HALT_GROUP;
5385  }
5386 
5387  LOG_TARGET_ERROR(target, "Unknown DCSR cause field: 0x%" PRIx64, get_field(dcsr, CSR_DCSR_CAUSE));
5388  LOG_TARGET_ERROR(target, " dcsr=0x%" PRIx32, (uint32_t)dcsr);
5389  return RISCV_HALT_UNKNOWN;
5390 }
5391 
5392 static int riscv013_write_progbuf(struct target *target, unsigned int index, riscv_insn_t data)
5393 {
5394  assert(index < RISCV013_MAX_PROGBUF_SIZE);
5395 
5396  dm013_info_t *dm = get_dm(target);
5397  if (!dm)
5398  return ERROR_FAIL;
5399 
5400  if (dm->progbuf_cache[index] != data) {
5401  if (dm_write(target, DM_PROGBUF0 + index, data) != ERROR_OK)
5402  return ERROR_FAIL;
5403  dm->progbuf_cache[index] = data;
5404  } else {
5405  LOG_TARGET_DEBUG(target, "Cache hit for 0x%" PRIx32 " @%d", data, index);
5406  }
5407  return ERROR_OK;
5408 }
5409 
5410 static riscv_insn_t riscv013_read_progbuf(struct target *target, unsigned int index)
5411 {
5412  uint32_t value;
5413  if (dm_read(target, &value, DM_PROGBUF0 + index) == ERROR_OK)
5414  return value;
5415  else
5416  return 0;
5417 }
5418 
5420 {
5421  dm013_info_t *dm = get_dm(target);
5422  if (!dm) {
5423  LOG_TARGET_DEBUG(target, "No DM is specified for the target");
5424  return ERROR_FAIL;
5425  }
5426 
5427  LOG_TARGET_DEBUG(target, "Invalidating progbuf cache");
5428  memset(dm->progbuf_cache, 0, sizeof(dm->progbuf_cache));
5429  return ERROR_OK;
5430 }
5431 
5432 static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr)
5433 {
5435  return ERROR_FAIL;
5436  uint32_t run_program = 0;
5437  run_program = set_field(run_program, AC_ACCESS_REGISTER_AARSIZE, 2);
5438  run_program = set_field(run_program, AC_ACCESS_REGISTER_POSTEXEC, 1);
5439  run_program = set_field(run_program, AC_ACCESS_REGISTER_TRANSFER, 0);
5440  run_program = set_field(run_program, AC_ACCESS_REGISTER_REGNO, 0x1000);
5441 
5442  return riscv013_execute_abstract_command(target, run_program, cmderr);
5443 }
5444 
5445 static void riscv013_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
5446 {
5450  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
5451 }
5452 
5453 static void riscv013_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
5454 {
5458  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
5459 }
5460 
5461 static void riscv013_fill_dm_nop(const struct target *target, uint8_t *buf)
5462 {
5466  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, 0);
5467 }
5468 
5469 static unsigned int riscv013_get_dmi_address_bits(const struct target *target)
5470 {
5472  return info->abits;
5473 }
5474 
5475 /* Helper Functions. */
5477 {
5480  return ERROR_FAIL;
5481 
5483  return ERROR_FAIL;
5484 
5486  return ERROR_FAIL;
5487  return ERROR_OK;
5488 }
5489 
5491  bool step)
5492 {
5493  if (target->state != TARGET_HALTED) {
5494  LOG_TARGET_ERROR(target, "Hart is not halted!");
5495  return ERROR_TARGET_NOT_HALTED;
5496  }
5497 
5498  LOG_TARGET_DEBUG(target, "resuming (operation=%s)",
5499  step ? "single-step" : "resume");
5500 
5502  return ERROR_FAIL;
5503 
5505 
5506  dm013_info_t *dm = get_dm(target);
5507  /* Issue the resume command, and then wait for the current hart to resume. */
5508  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_RESUMEREQ;
5509  dmcontrol = set_dmcontrol_hartsel(dmcontrol, dm->current_hartid);
5510  /* `resumereq` should not be issued if `abstractcs.busy` is set. */
5511  int result = wait_for_idle_if_needed(target);
5512  if (result != ERROR_OK)
5513  return result;
5514  dm_write(target, DM_DMCONTROL, dmcontrol);
5515 
5516  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_RESUMEREQ, 0);
5517 
5518  uint32_t dmstatus;
5519  for (size_t i = 0; i < 256; ++i) {
5520  usleep(10);
5521  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5522  return ERROR_FAIL;
5523  if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL))
5524  return ERROR_FAIL;
5525  if (get_field(dmstatus, DM_DMSTATUS_ALLRESUMEACK) == 0)
5526  continue;
5527  if (step && get_field(dmstatus, DM_DMSTATUS_ALLHALTED) == 0)
5528  continue;
5529 
5530  dm_write(target, DM_DMCONTROL, dmcontrol);
5531  return ERROR_OK;
5532  }
5533 
5534  LOG_TARGET_ERROR(target, "Failed to %s. dmstatus=0x%08x",
5535  step ? "single-step" : "resume", dmstatus);
5536 
5537  dm_write(target, DM_DMCONTROL, dmcontrol);
5539  " cancelling the resume request (dmcontrol.resumereq <- 0)");
5540 
5541  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5542  return ERROR_FAIL;
5543 
5544  LOG_TARGET_ERROR(target, " dmstatus after cancellation=0x%08x", dmstatus);
5545 
5546  if (step) {
5548  " trying to recover from a failed single-step, by requesting halt");
5549  if (riscv_halt(target) == ERROR_OK)
5550  LOG_TARGET_ERROR(target, " halt completed after failed single-step");
5551  else
5552  LOG_TARGET_ERROR(target, " could not halt, something is wrong with the taget");
5553  // TODO: returning ERROR_OK is questionable, this code needs to be revised
5554  return ERROR_OK;
5555  }
5556 
5557  return ERROR_FAIL;
5558 }
5559 
5561 {
5562  uint32_t abstractcs;
5563  int result = wait_for_idle(target, &abstractcs);
5564  /* Clear the error status, even if busy is still set. */
5566  result = ERROR_FAIL;
5567  return result;
5568 }
#define IS_PWR_OF_2(x)
Definition: align.h:24
const char * group
Definition: armv4_5.c:366
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:103
@ 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:118
@ 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:125
@ GDB_REGNO_PRIV
Definition: gdb_regs.h:113
@ GDB_REGNO_VCSR
Definition: gdb_regs.h:89
@ GDB_REGNO_CSR4095
Definition: gdb_regs.h:112
@ GDB_REGNO_COUNT
Definition: gdb_regs.h:126
@ 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:5392
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:2498
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:4304
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:1919
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:4684
static struct mem_access_result mem_access_result(enum mem_access_result_enum value)
Definition: riscv-013.c:3678
static int csr_write_progbuf(struct target *target, enum gdb_regno number, riscv_reg_t value)
Definition: riscv-013.c:1573
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:4370
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:4160
static int riscv013_step_current_hart(struct target *target)
Definition: riscv-013.c:5343
static void riscv013_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
Definition: riscv-013.c:5453
static int riscv013_step_or_resume_current_hart(struct target *target, bool step)
Definition: riscv-013.c:5490
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:4830
static uint32_t sb_sbaccess(unsigned int size_bytes)
Definition: riscv-013.c:2473
int riscv013_set_register_buf(struct target *target, enum gdb_regno regno, const uint8_t *value)
Definition: riscv-013.c:2434
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:4483
static int read_memory_bus_word(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: riscv-013.c:3161
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:5476
static int vl_write_progbuf(struct target *target, riscv_reg_t value)
Definition: riscv-013.c:1552
static int riscv013_on_step(struct target *target)
Definition: riscv-013.c:5354
static struct mem_access_result write_memory_progbuf_inner(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:5034
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:3796
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:4916
bool is_mem_access_failed(struct mem_access_result status)
Definition: riscv-013.c:3634
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:5178
#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:4551
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:1498
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:4101
static struct mem_access_result mem_should_skip_abstract(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3761
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:1648
#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:5461
struct target_type riscv013_target
Definition: riscv-013.c:5095
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:5152
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:2518
static int examine(struct target *target)
Definition: riscv-013.c:2009
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:3258
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:1846
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:2973
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:3684
int riscv013_set_register(struct target *target, enum gdb_regno rid, riscv_reg_t value)
Definition: riscv-013.c:5140
bool is_mem_access_ok(struct mem_access_result status)
Definition: riscv-013.c:3618
static int riscv013_halt_go(struct target *target)
Definition: riscv-013.c:5248
static int vtype_write_progbuf(struct target *target, riscv_reg_t value)
Definition: riscv-013.c:1531
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:2368
static OOCD_LIST_HEAD(dm_list)
static int assert_reset(struct target *target)
Definition: riscv-013.c:2922
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:4947
static int batch_run(struct target *target, struct riscv_batch *batch)
Definition: riscv-013.c:2529
static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr)
Definition: riscv-013.c:5432
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:4884
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:1596
mem_access_result_type
Definition: riscv-013.c:3539
@ MEM_ACCESS_RESULT_TYPE_OK
Definition: riscv-013.c:3540
@ MEM_ACCESS_RESULT_TYPE_ENUM_SIZE
Definition: riscv-013.c:3544
@ MEM_ACCESS_RESULT_TYPE_SKIPPED
Definition: riscv-013.c:3542
@ MEM_ACCESS_RESULT_TYPE_FAILED
Definition: riscv-013.c:3543
@ MEM_ACCESS_RESULT_TYPE_DISABLED
Definition: riscv-013.c:3541
static int riscv013_invalidate_cached_progbuf(struct target *target)
Definition: riscv-013.c:5419
static int handle_became_unavailable(struct target *target, enum riscv_hart_state previous_riscv_state)
Definition: riscv-013.c:2844
static int read_memory_progbuf_inner_fill_progbuf(struct target *target, uint32_t increment, uint32_t size)
Definition: riscv-013.c:4329
static int read_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3277
mem_access_result_enum
Definition: riscv-013.c:3607
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:4464
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:5111
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:4428
static void riscv013_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
Definition: riscv-013.c:5445
static enum riscv_halt_reason riscv013_halt_reason(struct target *target)
Definition: riscv-013.c:5359
bool is_mem_access_skipped(struct mem_access_result status)
Definition: riscv-013.c:3650
static unsigned int get_sbaadress_reg_count(const struct target *target)
Definition: riscv-013.c:2491
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:2553
static int riscv013_access_memory(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4570
static riscv_insn_t riscv013_read_progbuf(struct target *target, unsigned int index)
Definition: riscv-013.c:5410
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:1826
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:3367
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:3952
static int sba_supports_access(struct target *target, unsigned int size_bytes)
Definition: riscv-013.c:2602
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:1699
static int init_target(struct command_context *cmd_ctx, struct target *target)
Definition: riscv-013.c:2869
static struct mem_access_result access_memory_sysbus(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4523
static bool dcsr_ebreak_config_equals_reset_value(const struct target *target)
Definition: riscv-013.c:2964
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:2236
static int riscv013_resume_prep(struct target *target)
Definition: riscv-013.c:5348
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:3665
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:1475
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:4254
static struct mem_access_result write_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3873
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:3044
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:4316
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:2201
#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:2188
static int riscv013_get_hart_state(struct target *target, enum riscv_hart_state *state)
Definition: riscv-013.c:2793
static void set_buffer_and_log_read(const struct riscv_mem_access_args args, uint32_t index, uint64_t value)
Definition: riscv-013.c:4288
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:1619
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:1726
static int riscv013_halt_prep(struct target *target)
Definition: riscv-013.c:5243
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:5560
static int write_memory_progbuf_fill_progbuf(struct target *target, uint32_t size)
Definition: riscv-013.c:5011
static target_addr_t sb_read_address(struct target *target)
Definition: riscv-013.c:3178
int riscv013_get_register_buf(struct target *target, uint8_t *value, enum gdb_regno regno)
Definition: riscv-013.c:2379
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:3120
#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:3212
static int write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4630
static unsigned int riscv013_get_dmi_address_bits(const struct target *target)
Definition: riscv-013.c:5469
static int riscv013_resume_go(struct target *target)
Definition: riscv-013.c:5335
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:4874
static void log_memory_access(target_addr_t address, uint32_t *sbvalue, unsigned int size_bytes, bool is_read)
Definition: riscv-013.c:3146
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:4231
#define LIST_OF_MEM_ACCESS_RESULTS
Definition: riscv-013.c:3547
#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:2621
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:5070
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:4081
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:1791
static int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
Definition: riscv-013.c:1676
static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
Definition: riscv-013.c:3194
static int tick(struct target *target)
Definition: riscv-013.c:2859
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:4022
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:3108
static unsigned int riscv013_get_progbufsize(const struct target *target)
Definition: riscv-013.c:5088
static COMMAND_HELPER(riscv013_print_info, struct target *target)
Definition: riscv-013.c:2271
static int try_set_vsew(struct target *target, unsigned int *debug_vsew)
Definition: riscv-013.c:2305
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:2334
static uint32_t read_memory_progbuf_inner_fill_batch(struct riscv_batch *batch, uint32_t count, uint32_t size)
Definition: riscv-013.c:4208
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:5082
static struct mem_access_result mem_should_skip_sysbus(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3728
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:2782
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:3513
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:4994
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:6121
struct scan_field select_dbus
Definition: riscv.c:48
bool riscv_supports_extension(const struct target *target, char letter)
Definition: riscv.c:6108
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:6133
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:6277
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:3615
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::@125 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:27
const char * name
Name of this type of target.
Definition: target_type.h:32
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:2112
int target_examine_one(struct target *target)
Examine the specified target, letting it perform any Initialisation that requires JTAG access.
Definition: target.c:686
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2170
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:4881
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4695
@ 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