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