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