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