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