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