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