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 & BIT(DM_HARTINFO_DATAADDR_LENGTH - 1))
1231  scratch->hart_address |=
1233  /* Align. */
1234  scratch->hart_address = ALIGN_UP(scratch->hart_address, alignment);
1235 
1236  if (DIV_ROUND_UP(size_bytes + scratch->hart_address - info->dataaddr, 4) <=
1237  info->datasize) {
1238  scratch->memory_space = SPACE_DM_DATA;
1239  scratch->debug_address = (scratch->hart_address - info->dataaddr) / 4;
1240  return ERROR_OK;
1241  }
1242  }
1243 
1244  /* Option 2: See if progbuf can be used as the scratch memory */
1246  return ERROR_FAIL;
1247 
1248  /* Allow for ebreak at the end of the program. */
1249  unsigned int program_size = (program->instruction_count + 1) * 4;
1250  scratch->hart_address = ALIGN_UP(info->progbuf_address + program_size, alignment);
1251  if ((info->progbuf_writable == YNM_YES) &&
1252  (DIV_ROUND_UP(size_bytes + scratch->hart_address - info->progbuf_address, 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 = ALIGN_UP(scratch->area->address, alignment);
1263  scratch->memory_space = SPACE_DMI_RAM;
1264  scratch->debug_address = scratch->hart_address;
1265  return ERROR_OK;
1266  }
1267 
1268  LOG_TARGET_ERROR(target, "Couldn't find %d bytes of scratch RAM to use. Please configure "
1269  "a work area with 'configure -work-area-phys'.", size_bytes);
1270  return ERROR_FAIL;
1271 }
1272 
1273 static int scratch_release(struct target *target,
1274  scratch_mem_t *scratch)
1275 {
1276  return target_free_working_area(target, scratch->area);
1277 }
1278 
1279 static int scratch_read64(struct target *target, scratch_mem_t *scratch,
1280  uint64_t *value)
1281 {
1282  uint32_t v;
1283  switch (scratch->memory_space) {
1284  case SPACE_DM_DATA:
1285  if (dm_read(target, &v, DM_DATA0 + scratch->debug_address) != ERROR_OK)
1286  return ERROR_FAIL;
1287  *value = v;
1288  if (dm_read(target, &v, DM_DATA1 + scratch->debug_address) != ERROR_OK)
1289  return ERROR_FAIL;
1290  *value |= ((uint64_t)v) << 32;
1291  break;
1292  case SPACE_DMI_PROGBUF:
1293  if (dm_read(target, &v, DM_PROGBUF0 + scratch->debug_address) != ERROR_OK)
1294  return ERROR_FAIL;
1295  *value = v;
1296  if (dm_read(target, &v, DM_PROGBUF1 + scratch->debug_address) != ERROR_OK)
1297  return ERROR_FAIL;
1298  *value |= ((uint64_t)v) << 32;
1299  break;
1300  case SPACE_DMI_RAM:
1301  {
1302  uint8_t buffer[8] = {0};
1303  const struct riscv_mem_access_args args = {
1304  .address = scratch->debug_address,
1305  .read_buffer = buffer,
1306  .size = 4,
1307  .count = 2,
1308  .increment = 4,
1309  };
1310  if (riscv013_access_memory(target, args) != ERROR_OK)
1311  return ERROR_FAIL;
1312  *value = buf_get_u64(buffer,
1313  /* first = */ 0, /* bit_num = */ 64);
1314  }
1315  break;
1316  }
1317  return ERROR_OK;
1318 }
1319 
1320 static int scratch_write64(struct target *target, scratch_mem_t *scratch,
1321  uint64_t value)
1322 {
1323  switch (scratch->memory_space) {
1324  case SPACE_DM_DATA:
1325  dm_write(target, DM_DATA0 + scratch->debug_address, (uint32_t)value);
1326  dm_write(target, DM_DATA1 + scratch->debug_address, (uint32_t)(value >> 32));
1327  break;
1328  case SPACE_DMI_PROGBUF:
1329  dm_write(target, DM_PROGBUF0 + scratch->debug_address, (uint32_t)value);
1330  dm_write(target, DM_PROGBUF1 + scratch->debug_address, (uint32_t)(value >> 32));
1332  break;
1333  case SPACE_DMI_RAM:
1334  {
1335  uint8_t buffer[8] = {
1336  value,
1337  value >> 8,
1338  value >> 16,
1339  value >> 24,
1340  value >> 32,
1341  value >> 40,
1342  value >> 48,
1343  value >> 56
1344  };
1345  const struct riscv_mem_access_args args = {
1346  .address = scratch->debug_address,
1347  .write_buffer = buffer,
1348  .size = 4,
1349  .count = 2,
1350  .increment = 4,
1351  };
1352  if (riscv013_access_memory(target, args) != ERROR_OK)
1353  return ERROR_FAIL;
1354  }
1355  break;
1356  }
1357  return ERROR_OK;
1358 }
1359 
1361 static unsigned int register_size(struct target *target, enum gdb_regno number)
1362 {
1363  /* If reg_cache hasn't been initialized yet, make a guess. We need this for
1364  * when this function is called during examine(). */
1365  if (target->reg_cache)
1366  return target->reg_cache->reg_list[number].size;
1367  else
1368  return riscv_xlen(target);
1369 }
1370 
1371 static bool has_sufficient_progbuf(struct target *target, unsigned int size)
1372 {
1374  return info->progbufsize + info->impebreak >= size;
1375 }
1376 
1384  struct riscv_program *program, riscv_reg_t *value)
1385 {
1386  scratch_mem_t scratch;
1387 
1388  if (scratch_reserve(target, &scratch, program, 8) != ERROR_OK)
1389  return ERROR_FAIL;
1390 
1392  != ERROR_OK) {
1393  scratch_release(target, &scratch);
1394  return ERROR_FAIL;
1395  }
1396  if (riscv_program_exec(program, target) != ERROR_OK) {
1397  scratch_release(target, &scratch);
1398  return ERROR_FAIL;
1399  }
1400 
1401  int result = scratch_read64(target, &scratch, value);
1402 
1403  scratch_release(target, &scratch);
1404  return result;
1405 }
1406 
1407 static int fpr_read_progbuf(struct target *target, uint64_t *value,
1408  enum gdb_regno number)
1409 {
1410  assert(target->state == TARGET_HALTED);
1411  assert(number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31);
1412 
1413  const unsigned int freg = number - GDB_REGNO_FPR0;
1414 
1416  return ERROR_FAIL;
1417 
1418  struct riscv_program program;
1419  riscv_program_init(&program, target);
1420  if (riscv_supports_extension(target, 'D') && riscv_xlen(target) < 64) {
1421  /* There are no instructions to move all the bits from a
1422  * register, so we need to use some scratch RAM.
1423  */
1424  if (riscv_program_insert(&program, fsd(freg, S0, 0)) != ERROR_OK)
1425  return ERROR_FAIL;
1426  return internal_register_read64_progbuf_scratch(target, &program, value);
1427  }
1428  if (riscv_program_insert(&program,
1430  fmv_x_d(S0, freg) : fmv_x_w(S0, freg)) != ERROR_OK)
1431  return ERROR_FAIL;
1432 
1433  if (riscv_program_exec(&program, target) != ERROR_OK)
1434  return ERROR_FAIL;
1435 
1437 }
1438 
1439 static int csr_read_progbuf(struct target *target, uint64_t *value,
1440  enum gdb_regno number)
1441 {
1442  assert(target->state == TARGET_HALTED);
1443  assert(number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095);
1444 
1446  return ERROR_FAIL;
1447 
1448  struct riscv_program program;
1449  riscv_program_init(&program, target);
1450  if (riscv_program_csrr(&program, S0, number) != ERROR_OK)
1451  return ERROR_FAIL;
1452  if (riscv_program_exec(&program, target) != ERROR_OK)
1453  return ERROR_FAIL;
1454 
1456 }
1457 
1462 static int register_read_progbuf(struct target *target, uint64_t *value,
1463  enum gdb_regno number)
1464 {
1465  assert(target->state == TARGET_HALTED);
1466 
1467  int res;
1468  uint64_t new_value;
1470  res = fpr_read_progbuf(target, &new_value, number);
1471  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
1472  res = csr_read_progbuf(target, &new_value, number);
1473  } else {
1474  LOG_TARGET_ERROR(target, "Unexpected read of %s via program buffer.",
1476  return ERROR_FAIL;
1477  }
1478  if (res != ERROR_OK)
1479  return res;
1480 
1481  unsigned int size_bits = register_size(target, number);
1482  unsigned int value_bits = sizeof(*value) * CHAR_BIT;
1483  assert(size_bits <= value_bits);
1484  if (size_bits == value_bits || new_value >> size_bits == 0) {
1485  *value = new_value;
1486  return ERROR_OK;
1487  }
1488  LOG_TARGET_ERROR(target, "Value 0x%" PRIx64 " read from register %s"
1489  " exceeds the size of the register (%u bits). This is a HW bug."
1490  " Discarding the value", new_value,
1491  riscv_reg_gdb_regno_name(target, number), size_bits);
1492  return ERROR_FAIL;
1493 }
1494 
1502  struct riscv_program *program, riscv_reg_t value)
1503 {
1504  scratch_mem_t scratch;
1505 
1506  if (scratch_reserve(target, &scratch, program, 8) != ERROR_OK)
1507  return ERROR_FAIL;
1508 
1510  != ERROR_OK) {
1511  scratch_release(target, &scratch);
1512  return ERROR_FAIL;
1513  }
1514  if (scratch_write64(target, &scratch, value) != ERROR_OK) {
1515  scratch_release(target, &scratch);
1516  return ERROR_FAIL;
1517  }
1518  int result = riscv_program_exec(program, target);
1519 
1520  scratch_release(target, &scratch);
1521  return result;
1522 }
1523 
1525  riscv_reg_t value)
1526 {
1527  assert(target->state == TARGET_HALTED);
1528  assert(number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31);
1529  const unsigned int freg = number - GDB_REGNO_FPR0;
1530 
1532  return ERROR_FAIL;
1533 
1534  struct riscv_program program;
1535  riscv_program_init(&program, target);
1536 
1537  if (riscv_supports_extension(target, 'D') && riscv_xlen(target) < 64) {
1538  /* There are no instructions to move all the bits from a register,
1539  * so we need to use some scratch RAM.
1540  */
1541  if (riscv_program_insert(&program, fld(freg, S0, 0)) != ERROR_OK)
1542  return ERROR_FAIL;
1543  return internal_register_write64_progbuf_scratch(target, &program, value);
1544  }
1545 
1547  return ERROR_FAIL;
1548 
1549  if (riscv_program_insert(&program,
1551  fmv_d_x(freg, S0) : fmv_w_x(freg, S0)) != ERROR_OK)
1552  return ERROR_FAIL;
1553 
1554  return riscv_program_exec(&program, target);
1555 }
1556 
1557 static int vtype_write_progbuf(struct target *target, riscv_reg_t value)
1558 {
1559  assert(target->state == TARGET_HALTED);
1560 
1562  return ERROR_FAIL;
1564  return ERROR_FAIL;
1566  return ERROR_FAIL;
1567 
1568  struct riscv_program program;
1569  riscv_program_init(&program, target);
1570  if (riscv_program_insert(&program, csrr(S1, CSR_VL)) != ERROR_OK)
1571  return ERROR_FAIL;
1572  if (riscv_program_insert(&program, vsetvl(ZERO, S1, S0)) != ERROR_OK)
1573  return ERROR_FAIL;
1574 
1575  return riscv_program_exec(&program, target);
1576 }
1577 
1578 static int vl_write_progbuf(struct target *target, riscv_reg_t value)
1579 {
1580  assert(target->state == TARGET_HALTED);
1581 
1583  return ERROR_FAIL;
1585  return ERROR_FAIL;
1587  return ERROR_FAIL;
1588 
1589  struct riscv_program program;
1590  riscv_program_init(&program, target);
1591  if (riscv_program_insert(&program, csrr(S1, CSR_VTYPE)) != ERROR_OK)
1592  return ERROR_FAIL;
1593  if (riscv_program_insert(&program, vsetvl(ZERO, S0, S1)) != ERROR_OK)
1594  return ERROR_FAIL;
1595 
1596  return riscv_program_exec(&program, target);
1597 }
1598 
1600  riscv_reg_t value)
1601 {
1602  assert(target->state == TARGET_HALTED);
1603  assert(number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095);
1604 
1606  return ERROR_FAIL;
1608  return ERROR_FAIL;
1609 
1610  struct riscv_program program;
1611  riscv_program_init(&program, target);
1612  if (riscv_program_csrw(&program, S0, number) != ERROR_OK)
1613  return ERROR_FAIL;
1614 
1615  return riscv_program_exec(&program, target);
1616 }
1617 
1623  riscv_reg_t value)
1624 {
1625  assert(target->state == TARGET_HALTED);
1626 
1628  return fpr_write_progbuf(target, number, value);
1629  else if (number == GDB_REGNO_VTYPE)
1630  return vtype_write_progbuf(target, value);
1631  else if (number == GDB_REGNO_VL)
1632  return vl_write_progbuf(target, value);
1633  else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095)
1634  return csr_write_progbuf(target, number, value);
1635 
1636  LOG_TARGET_ERROR(target, "Unexpected write to %s via program buffer.",
1638  return ERROR_FAIL;
1639 }
1640 
1646  riscv_reg_t value)
1647 {
1648  LOG_TARGET_DEBUG(target, "Writing 0x%" PRIx64 " to %s", value,
1650 
1651  if (target->state != TARGET_HALTED)
1652  return register_write_abstract(target, number, value);
1653 
1654  riscv_reg_t mstatus;
1655  if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1656  return ERROR_FAIL;
1657 
1658  int result = register_write_abstract(target, number, value);
1659 
1660  if (result != ERROR_OK && target->state == TARGET_HALTED)
1661  result = register_write_progbuf(target, number, value);
1662 
1664  return ERROR_FAIL;
1665 
1666  if (result == ERROR_OK)
1668  value);
1669 
1670  return result;
1671 }
1672 
1674 static int register_read_direct(struct target *target, riscv_reg_t *value,
1675  enum gdb_regno number)
1676 {
1678 
1679  if (target->state != TARGET_HALTED)
1680  return register_read_abstract(target, value, number);
1681 
1682  riscv_reg_t mstatus;
1683 
1684  if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1685  return ERROR_FAIL;
1686 
1687  int result = register_read_abstract(target, value, number);
1688 
1689  if (result != ERROR_OK && target->state == TARGET_HALTED)
1690  result = register_read_progbuf(target, value, number);
1691 
1693  return ERROR_FAIL;
1694 
1695  if (result == ERROR_OK)
1697  *value);
1698 
1699  return result;
1700 }
1701 
1702 static int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
1703 {
1704  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1705  while (1) {
1706  uint32_t value;
1707  if (dmstatus_read(target, &value, false) != ERROR_OK)
1708  return ERROR_FAIL;
1709  if (dmstatus)
1710  *dmstatus = value;
1711  if (!get_field(value, DM_DMSTATUS_AUTHBUSY))
1712  break;
1713  if (timeval_ms() > then) {
1714  LOG_TARGET_ERROR(target, "Timed out after %ds waiting for authbusy to go low (dmstatus=0x%x). "
1715  "Increase the timeout with riscv set_command_timeout_sec.",
1717  value);
1718  return ERROR_FAIL;
1719  }
1720  }
1721 
1722  return ERROR_OK;
1723 }
1724 
1725 static int set_dcsr_config(struct target *target, bool step)
1726 {
1727  LOG_TARGET_DEBUG(target, "Set dcsr config");
1728 
1730  return ERROR_FAIL;
1731 
1733  riscv_reg_t original_dcsr, dcsr;
1734  /* We want to twiddle some bits in the debug CSR so debugging works. */
1735  if (riscv_reg_get(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
1736  return ERROR_FAIL;
1737  original_dcsr = dcsr;
1738  dcsr = set_field(dcsr, CSR_DCSR_STEP, step);
1739  const struct riscv_private_config * const config = riscv_private_config(target);
1740  dcsr = set_field(dcsr, CSR_DCSR_EBREAKM, config->dcsr_ebreak_fields[RISCV_MODE_M]);
1741  dcsr = set_field(dcsr, CSR_DCSR_EBREAKS, config->dcsr_ebreak_fields[RISCV_MODE_S]);
1742  dcsr = set_field(dcsr, CSR_DCSR_EBREAKU, config->dcsr_ebreak_fields[RISCV_MODE_U]);
1743  dcsr = set_field(dcsr, CSR_DCSR_EBREAKVS, config->dcsr_ebreak_fields[RISCV_MODE_VS]);
1744  dcsr = set_field(dcsr, CSR_DCSR_EBREAKVU, config->dcsr_ebreak_fields[RISCV_MODE_VU]);
1745  dcsr = set_field(dcsr, CSR_DCSR_CETRIG, config->dcsr_cetrig);
1746  if (dcsr != original_dcsr &&
1748  return ERROR_FAIL;
1749  // TODO: Read back the DCSR and check if these WARL bits are set as the user intended.
1750  info->dcsr_register_is_set = true;
1751  return ERROR_OK;
1752 }
1753 
1755 {
1756  RISCV_INFO(r);
1758  LOG_TARGET_DEBUG(target, "Halt to set dcsr config");
1759 
1760  /* Remove this hart from the halt group. This won't work on all targets
1761  * because the debug spec allows halt groups to be hard-coded, but I
1762  * haven't actually encountered those in the wild yet.
1763  *
1764  * There is a possible race condition when another hart halts, and
1765  * this one is expected to also halt because it's supposed to be in the
1766  * same halt group. Or when this hart is halted when that happens.
1767  *
1768  * A better solution might be to leave the halt groups alone, and track
1769  * why we're halting when a halt occurs. When there are halt groups,
1770  * that leads to extra halting if not all harts need to set dcsr.ebreak
1771  * at the same time. It also makes for more complicated code.
1772  *
1773  * The perfect solution would be Quick Access, but I'm not aware of any
1774  * hardware that implements it.
1775  *
1776  * We don't need a perfect solution, because we only get here when a
1777  * hart spontaneously resets, or when it powers down and back up again.
1778  * Those are both relatively rare. (At least I hope so. Maybe some
1779  * design just powers each hart down for 90ms out of every 100ms)
1780  */
1781 
1782 
1783  if (info->haltgroup_supported) {
1784  bool supported;
1785  if (set_group(target, &supported, 0, HALT_GROUP) != ERROR_OK)
1786  return ERROR_FAIL;
1787  if (!supported)
1788  LOG_TARGET_ERROR(target, "Couldn't place hart in halt group 0. "
1789  "Some harts may be unexpectedly halted.");
1790  }
1791 
1792  int result = ERROR_OK;
1793 
1794  r->prepped = true;
1795  if (riscv013_halt_go(target) != ERROR_OK ||
1796  set_dcsr_config(target, false) != ERROR_OK ||
1798  result = ERROR_FAIL;
1799  } else {
1802  }
1803 
1804  /* Add it back to the halt group. */
1805  if (info->haltgroup_supported) {
1806  bool supported;
1807  if (set_group(target, &supported, target->smp_id, HALT_GROUP) != ERROR_OK)
1808  return ERROR_FAIL;
1809  if (!supported)
1810  LOG_TARGET_ERROR(target, "Couldn't place hart back in halt group %d. "
1811  "Some harts may be unexpectedly halted.", target->smp_id);
1812  }
1813 
1814  return result;
1815 }
1816 
1817 /*** OpenOCD target functions. ***/
1818 
1819 static void deinit_target(struct target *target)
1820 {
1821  LOG_TARGET_DEBUG(target, "Deinitializing target.");
1822  struct riscv_info *info = target->arch_info;
1823  if (!info)
1824  return;
1825 
1826  riscv013_info_t *vsinfo = info->version_specific;
1827  if (vsinfo)
1829 
1831 
1832  free(info->version_specific);
1833  /* TODO: free register arch_info */
1834  info->version_specific = NULL;
1835 }
1836 
1837 static int set_group(struct target *target, bool *supported, unsigned int group,
1838  enum grouptype grouptype)
1839 {
1840  uint32_t write_val = DM_DMCS2_HGWRITE;
1841  assert(group <= 31);
1842  write_val = set_field(write_val, DM_DMCS2_GROUP, group);
1843  write_val = set_field(write_val, DM_DMCS2_GROUPTYPE, (grouptype == HALT_GROUP) ? 0 : 1);
1844  if (dm_write(target, DM_DMCS2, write_val) != ERROR_OK)
1845  return ERROR_FAIL;
1846  uint32_t read_val;
1847  if (dm_read(target, &read_val, DM_DMCS2) != ERROR_OK)
1848  return ERROR_FAIL;
1849  if (supported)
1850  *supported = (get_field(read_val, DM_DMCS2_GROUP) == group);
1851  return ERROR_OK;
1852 }
1853 
1855 {
1856  dm013_info_t *dm = get_dm(target);
1857  if (!dm)
1858  return ERROR_FAIL;
1859  if (!dm->abstract_cmd_maybe_busy)
1860  /* The previous abstract command ended correctly
1861  * and busy was cleared. No need to do anything. */
1862  return ERROR_OK;
1863 
1864  /* The previous abstract command timed out and abstractcs.busy
1865  * may have remained set. Wait for it to get cleared. */
1866  uint32_t abstractcs;
1867  int result = wait_for_idle(target, &abstractcs);
1868  if (result != ERROR_OK)
1869  return result;
1870  LOG_DEBUG_REG(target, DM_ABSTRACTCS, abstractcs);
1871  return ERROR_OK;
1872 }
1873 
1874 static int reset_dm(struct target *target)
1875 {
1876  /* TODO: This function returns an error when a DMI operation fails.
1877  * However, [3.14.2. Debug Module Control] states:
1878  * > 0 (inactive): ... Any accesses to the module may fail.
1879  *
1880  * Ignoring failures may introduce incompatibility with 0.13.
1881  * See https://github.com/riscv/riscv-debug-spec/issues/1021
1882  */
1883  dm013_info_t *dm = get_dm(target);
1884  assert(dm && "DM is expected to be already allocated.");
1885  assert(!dm->was_reset && "Attempt to reset an already-reset debug module.");
1886  /* `dmcontrol.hartsel` should be read first, in order not to
1887  * change it when requesting the reset, since changing it
1888  * without checking that `abstractcs.busy` is low is
1889  * prohibited.
1890  */
1891  uint32_t dmcontrol;
1892  int result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1893  if (result != ERROR_OK)
1894  return result;
1895 
1896  if (get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE)) {
1897  /* `dmcontrol.hartsel` is not changed. */
1898  dmcontrol = (dmcontrol & DM_DMCONTROL_HARTSELLO) |
1899  (dmcontrol & DM_DMCONTROL_HARTSELHI);
1900  LOG_TARGET_DEBUG(target, "Initiating DM reset.");
1901  result = dm_write(target, DM_DMCONTROL, dmcontrol);
1902  if (result != ERROR_OK)
1903  return result;
1904 
1905  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1906  LOG_TARGET_DEBUG(target, "Waiting for the DM to acknowledge reset.");
1907  do {
1908  result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1909  if (result != ERROR_OK)
1910  return result;
1911 
1912  if (timeval_ms() > then) {
1913  LOG_TARGET_ERROR(target, "DM didn't acknowledge reset in %d s. "
1914  "Increase the timeout with 'riscv set_command_timeout_sec'.",
1916  return ERROR_TIMEOUT_REACHED;
1917  }
1918  } while (get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE));
1919  LOG_TARGET_DEBUG(target, "DM reset initiated.");
1920  }
1921  /* TODO: Move the code above into `deactivate_dm()` function
1922  * (a logical counterpart to activate_dm()). */
1923 
1924  result = activate_dm(target, dm->base);
1925  if (result != ERROR_OK)
1926  return result;
1927 
1928  LOG_TARGET_DEBUG(target, "DM successfully reset.");
1929  dm->was_reset = true;
1930  return ERROR_OK;
1931 }
1932 
1933 static int examine_dm(struct target *target)
1934 {
1935  dm013_info_t *dm = get_dm(target);
1936  if (!dm)
1937  return ERROR_FAIL;
1938  if (dm->was_examined)
1939  return ERROR_OK;
1940 
1941  int result = ERROR_FAIL;
1942 
1943  if (dm->was_reset) {
1944  /* The DM was already reset when examining a different hart.
1945  * No need to reset it again. But for safety, assume that an abstract
1946  * command might be in progress at the moment.
1947  */
1948  dm->abstract_cmd_maybe_busy = true;
1949  } else {
1950  result = reset_dm(target);
1951  if (result != ERROR_OK)
1952  return result;
1953  }
1954 
1956 
1960  if (result != ERROR_OK)
1961  return result;
1962 
1963  uint32_t dmcontrol;
1964  result = dm_read(target, &dmcontrol, DM_DMCONTROL);
1965  if (result != ERROR_OK)
1966  return result;
1967 
1968  dm->hasel_supported = get_field(dmcontrol, DM_DMCONTROL_HASEL);
1969 
1970  uint32_t hartsel =
1971  (get_field(dmcontrol, DM_DMCONTROL_HARTSELHI) <<
1973  get_field(dmcontrol, DM_DMCONTROL_HARTSELLO);
1974 
1975  /* Before doing anything else we must first enumerate the harts. */
1976  if (dm->hart_count < 0) {
1977  for (uint32_t i = 0; i <= hartsel; ++i) {
1978  /* TODO: This is extremely similar to
1979  * riscv013_get_hart_state().
1980  * It would be best to reuse the code.
1981  */
1982  result = dm013_select_hart(target, i);
1983  if (result != ERROR_OK)
1984  return result;
1985 
1986  uint32_t s;
1987  result = dmstatus_read(target, &s, /*authenticated*/ true);
1988  if (result != ERROR_OK)
1989  return result;
1990 
1992  break;
1993 
1994  dm->hart_count = i + 1;
1995 
1998  /* If `abstractcs.busy` is set, debugger should not
1999  * change `hartsel`.
2000  */
2001  result = wait_for_idle_if_needed(target);
2002  if (result != ERROR_OK)
2003  return result;
2004  dmcontrol = set_dmcontrol_hartsel(dmcontrol, i);
2005  result = dm_write(target, DM_DMCONTROL, dmcontrol);
2006  if (result != ERROR_OK)
2007  return result;
2008  }
2009  }
2010  LOG_TARGET_DEBUG(target, "Detected %d harts.", dm->hart_count);
2011  }
2012 
2013  if (dm->hart_count <= 0) {
2014  LOG_TARGET_ERROR(target, "No harts found!");
2015  return ERROR_FAIL;
2016  }
2017 
2018  dm->was_examined = true;
2019  return ERROR_OK;
2020 }
2021 
2022 static int examine(struct target *target)
2023 {
2024  /* We reset target state in case if something goes wrong during examine:
2025  * DTM/DM scans could fail or hart may fail to halt. */
2028 
2029  /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
2030  LOG_TARGET_DEBUG(target, "dbgbase=0x%x", target->dbgbase);
2031 
2032  uint32_t dtmcontrol;
2033  if (dtmcs_scan(target->tap, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
2034  LOG_TARGET_ERROR(target, "Could not scan dtmcontrol. Check JTAG connectivity/board power.");
2035  return ERROR_FAIL;
2036  }
2037 
2038  LOG_TARGET_DEBUG(target, "dtmcontrol=0x%x", dtmcontrol);
2039  LOG_DEBUG_REG(target, DTM_DTMCS, dtmcontrol);
2040 
2041  if (get_field(dtmcontrol, DTM_DTMCS_VERSION) != 1) {
2042  LOG_TARGET_ERROR(target, "Unsupported DTM version %" PRIu32 ". (dtmcontrol=0x%" PRIx32 ")",
2043  get_field32(dtmcontrol, DTM_DTMCS_VERSION), dtmcontrol);
2044  return ERROR_FAIL;
2045  }
2046 
2048 
2049  info->index = target->coreid;
2050  info->abits = get_field(dtmcontrol, DTM_DTMCS_ABITS);
2051  info->dtmcs_idle = get_field(dtmcontrol, DTM_DTMCS_IDLE);
2052 
2053  if (info->abits > RISCV013_DTMCS_ABITS_MAX) {
2054  /* Max. address width given by the debug specification is exceeded */
2055  LOG_TARGET_ERROR(target, "The target's debug bus (DMI) address width exceeds "
2056  "the maximum:");
2057  LOG_TARGET_ERROR(target, " found dtmcs.abits = %d; maximum is abits = %d.",
2058  info->abits, RISCV013_DTMCS_ABITS_MAX);
2059  return ERROR_FAIL;
2060  }
2061 
2062  if (info->abits == 0) {
2064  "dtmcs.abits is zero. Check JTAG connectivity/board power");
2065  return ERROR_FAIL;
2066  }
2067  if (info->abits < RISCV013_DTMCS_ABITS_MIN) {
2068  /* The requirement for minimum DMI address width of 7 bits is part of
2069  * the RISC-V Debug spec since Jan-20-2017 (commit 03df6ee7). However,
2070  * implementations exist that implement narrower DMI address. For example
2071  * Spike as of Q1/2025 uses dmi.abits = 6.
2072  *
2073  * For that reason, warn the user but continue.
2074  */
2075  LOG_TARGET_WARNING(target, "The target's debug bus (DMI) address width is "
2076  "lower than the minimum:");
2077  LOG_TARGET_WARNING(target, " found dtmcs.abits = %d; minimum is abits = %d.",
2078  info->abits, RISCV013_DTMCS_ABITS_MIN);
2079  }
2080 
2082  LOG_TARGET_ERROR(target, "Could not find debug module with DMI base address (dbgbase) = 0x%x", target->dbgbase);
2083  return ERROR_FAIL;
2084  }
2085 
2086  int result = examine_dm(target);
2087  if (result != ERROR_OK)
2088  return result;
2089 
2090  dm013_info_t *dm = get_dm(target);
2091  assert(dm);
2092  if (target->coreid >= dm->hart_count) {
2093  LOG_TARGET_ERROR(target, "Hart index %d is too large. The maximum"
2094  " index for this Debug Module is %d",
2095  target->coreid, dm->hart_count - 1);
2096  return ERROR_FAIL;
2097  }
2098 
2099  struct target_list *entry;
2100  list_for_each_entry(entry, &dm->target_list, lh) {
2101  struct target *t = entry->target;
2102  if (target != t && target->coreid == t->coreid) {
2103  LOG_TARGET_ERROR(target, "Hart index %d is already used by target '%s' in DM list",
2104  target->coreid, target_name(t));
2105  return ERROR_FAIL;
2106  }
2107  }
2108 
2109  result = dm013_select_target(target);
2110  if (result != ERROR_OK)
2111  return result;
2112 
2113  /* We're here because we're uncertain about the state of the target. That
2114  * includes our progbuf cache. */
2116 
2117  uint32_t dmstatus;
2118  if (dmstatus_read(target, &dmstatus, false) != ERROR_OK)
2119  return ERROR_FAIL;
2120  LOG_TARGET_DEBUG(target, "dmstatus: 0x%08x", dmstatus);
2121  int dmstatus_version = get_field(dmstatus, DM_DMSTATUS_VERSION);
2122  if (dmstatus_version != 2 && dmstatus_version != 3) {
2123  /* Error was already printed out in dmstatus_read(). */
2124  return ERROR_FAIL;
2125  }
2126 
2127  uint32_t hartinfo;
2128  if (dm_read(target, &hartinfo, DM_HARTINFO) != ERROR_OK)
2129  return ERROR_FAIL;
2130 
2131  info->datasize = get_field(hartinfo, DM_HARTINFO_DATASIZE);
2132  info->dataaccess = get_field(hartinfo, DM_HARTINFO_DATAACCESS);
2133  info->dataaddr = get_field(hartinfo, DM_HARTINFO_DATAADDR);
2134 
2135  if (!get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED)) {
2136  LOG_TARGET_ERROR(target, "Debugger is not authenticated to target Debug Module. "
2137  "(dmstatus=0x%x). Use `riscv authdata_read` and "
2138  "`riscv authdata_write` commands to authenticate.", dmstatus);
2139  return ERROR_FAIL;
2140  }
2141 
2142  if (dm_read(target, &info->sbcs, DM_SBCS) != ERROR_OK)
2143  return ERROR_FAIL;
2144 
2145  /* Check that abstract data registers are accessible. */
2146  uint32_t abstractcs;
2147  if (dm_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
2148  return ERROR_FAIL;
2149  info->datacount = get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT);
2150  info->progbufsize = get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE);
2151 
2152  LOG_TARGET_INFO(target, "datacount=%d progbufsize=%d",
2153  info->datacount, info->progbufsize);
2154 
2155  info->impebreak = get_field(dmstatus, DM_DMSTATUS_IMPEBREAK);
2156 
2157  if (!has_sufficient_progbuf(target, 2)) {
2158  LOG_TARGET_WARNING(target, "We won't be able to execute fence instructions on this "
2159  "target. Memory may not always appear consistent. "
2160  "(progbufsize=%d, impebreak=%d)", info->progbufsize,
2161  info->impebreak);
2162  }
2163 
2164  /* Don't call any riscv_* functions until after we've counted the number of
2165  * cores and initialized registers. */
2166 
2167  enum riscv_hart_state state_at_examine_start;
2168  if (riscv_get_hart_state(target, &state_at_examine_start) != ERROR_OK)
2169  return ERROR_FAIL;
2170 
2171  if (state_at_examine_start == RISCV_STATE_UNAVAILABLE) {
2173  LOG_TARGET_INFO(target, "unavailable.");
2174  return ERROR_FAIL;
2175  }
2176 
2177  RISCV_INFO(r);
2178  const bool hart_halted_at_examine_start = state_at_examine_start == RISCV_STATE_HALTED;
2179  if (!hart_halted_at_examine_start) {
2180  r->prepped = true;
2181  if (riscv013_halt_go(target) != ERROR_OK) {
2182  LOG_TARGET_ERROR(target, "Fatal: Hart %d failed to halt during %s",
2183  info->index, __func__);
2184  return ERROR_FAIL;
2185  }
2186  }
2187 
2189  target->debug_reason = hart_halted_at_examine_start ? DBG_REASON_UNDEFINED : DBG_REASON_DBGRQ;
2190 
2191  result = riscv013_reg_examine_all(target);
2192  if (result != ERROR_OK)
2193  return result;
2194 
2195  if (set_dcsr_config(target, false) != ERROR_OK)
2196  return ERROR_FAIL;
2197 
2198  if (state_at_examine_start == RISCV_STATE_RUNNING) {
2202  } else if (state_at_examine_start == RISCV_STATE_HALTED) {
2205  }
2206 
2207  if (target->smp) {
2208  if (set_group(target, &info->haltgroup_supported, target->smp_id, HALT_GROUP) != ERROR_OK)
2209  return ERROR_FAIL;
2210  if (info->haltgroup_supported)
2211  LOG_TARGET_INFO(target, "Core %d made part of halt group %d.", info->index,
2212  target->smp_id);
2213  else
2214  LOG_TARGET_INFO(target, "Core %d could not be made part of halt group %d.",
2215  info->index, target->smp_id);
2216  }
2217 
2218  /* Some regression suites rely on seeing 'Examined RISC-V core' to know
2219  * when they can connect with gdb/telnet.
2220  * We will need to update those suites if we want to change that text. */
2221  LOG_TARGET_INFO(target, "Examined RISC-V core");
2222  LOG_TARGET_INFO(target, " XLEN=%d, misa=0x%" PRIx64, r->xlen, r->misa);
2223  return ERROR_OK;
2224 }
2225 
2226 static int riscv013_authdata_read(struct target *target, uint32_t *value, unsigned int index)
2227 {
2228  if (index > 0) {
2229  LOG_TARGET_ERROR(target, "Spec 0.13 only has a single authdata register.");
2230  return ERROR_FAIL;
2231  }
2232 
2234  return ERROR_FAIL;
2235 
2236  return dm_read(target, value, DM_AUTHDATA);
2237 }
2238 
2239 static int riscv013_authdata_write(struct target *target, uint32_t value, unsigned int index)
2240 {
2241  if (index > 0) {
2242  LOG_TARGET_ERROR(target, "Spec 0.13 only has a single authdata register.");
2243  return ERROR_FAIL;
2244  }
2245 
2246  uint32_t before, after;
2247  if (wait_for_authbusy(target, &before) != ERROR_OK)
2248  return ERROR_FAIL;
2249 
2250  dm_write(target, DM_AUTHDATA, value);
2251 
2252  if (wait_for_authbusy(target, &after) != ERROR_OK)
2253  return ERROR_FAIL;
2254 
2255  if (!get_field(before, DM_DMSTATUS_AUTHENTICATED) &&
2257  LOG_TARGET_INFO(target, "authdata_write resulted in successful authentication");
2258  int result = ERROR_OK;
2259  dm013_info_t *dm = get_dm(target);
2260  if (!dm)
2261  return ERROR_FAIL;
2262  struct target_list *entry;
2263  list_for_each_entry(entry, &dm->target_list, lh) {
2264  if (target_examine_one(entry->target) != ERROR_OK)
2265  result = ERROR_FAIL;
2266  }
2267  return result;
2268  }
2269 
2270  return ERROR_OK;
2271 }
2272 
2273 /* Try to find out the widest memory access size depending on the selected memory access methods. */
2274 static unsigned int riscv013_data_bits(struct target *target)
2275 {
2277  RISCV_INFO(r);
2278 
2279  for (unsigned int i = 0; i < r->num_enabled_mem_access_methods; i++) {
2280  enum riscv_mem_access_method method = r->mem_access_methods[i];
2281 
2282  if (method == RISCV_MEM_ACCESS_PROGBUF) {
2284  return riscv_xlen(target);
2285  } else if (method == RISCV_MEM_ACCESS_SYSBUS) {
2286  if (get_field(info->sbcs, DM_SBCS_SBACCESS128))
2287  return 128;
2288  if (get_field(info->sbcs, DM_SBCS_SBACCESS64))
2289  return 64;
2290  if (get_field(info->sbcs, DM_SBCS_SBACCESS32))
2291  return 32;
2292  if (get_field(info->sbcs, DM_SBCS_SBACCESS16))
2293  return 16;
2294  if (get_field(info->sbcs, DM_SBCS_SBACCESS8))
2295  return 8;
2296  } else if (method == RISCV_MEM_ACCESS_ABSTRACT) {
2297  /* TODO: Once there is a spec for discovering abstract commands, we can
2298  * take those into account as well. For now we assume abstract commands
2299  * support XLEN-wide accesses. */
2300  return riscv_xlen(target);
2301  } else {
2302  assert(false);
2303  }
2304  }
2305  LOG_TARGET_ERROR(target, "Unable to determine supported data bits on this target. Assuming 32 bits.");
2306  return 32;
2307 }
2308 
2309 static COMMAND_HELPER(riscv013_print_info, struct target *target)
2310 {
2312 
2313  /* Abstract description. */
2314  riscv_print_info_line(CMD, "target", "memory.read_while_running8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2315  riscv_print_info_line(CMD, "target", "memory.write_while_running8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2316  riscv_print_info_line(CMD, "target", "memory.read_while_running16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2317  riscv_print_info_line(CMD, "target", "memory.write_while_running16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2318  riscv_print_info_line(CMD, "target", "memory.read_while_running32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2319  riscv_print_info_line(CMD, "target", "memory.write_while_running32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2320  riscv_print_info_line(CMD, "target", "memory.read_while_running64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2321  riscv_print_info_line(CMD, "target", "memory.write_while_running64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2322  riscv_print_info_line(CMD, "target", "memory.read_while_running128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2323  riscv_print_info_line(CMD, "target", "memory.write_while_running128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2324 
2325  /* Lower level description. */
2326  riscv_print_info_line(CMD, "dm", "abits", info->abits);
2327  riscv_print_info_line(CMD, "dm", "progbufsize", info->progbufsize);
2328  riscv_print_info_line(CMD, "dm", "sbversion", get_field(info->sbcs, DM_SBCS_SBVERSION));
2329  riscv_print_info_line(CMD, "dm", "sbasize", get_field(info->sbcs, DM_SBCS_SBASIZE));
2330  riscv_print_info_line(CMD, "dm", "sbaccess128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
2331  riscv_print_info_line(CMD, "dm", "sbaccess64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
2332  riscv_print_info_line(CMD, "dm", "sbaccess32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
2333  riscv_print_info_line(CMD, "dm", "sbaccess16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
2334  riscv_print_info_line(CMD, "dm", "sbaccess8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
2335 
2336  uint32_t dmstatus;
2337  if (dmstatus_read(target, &dmstatus, false) == ERROR_OK)
2338  riscv_print_info_line(CMD, "dm", "authenticated", get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED));
2339 
2340  return 0;
2341 }
2342 
2343 static int try_set_vsew(struct target *target, unsigned int *debug_vsew)
2344 {
2345  RISCV_INFO(r);
2346  unsigned int encoded_vsew =
2347  (riscv_xlen(target) == 64 && r->vsew64_supported != YNM_NO) ? 3 : 2;
2348 
2349  /* Set standard element width to match XLEN, for vmv instruction to move
2350  * the least significant bits into a GPR.
2351  */
2352  if (riscv_reg_write(target, GDB_REGNO_VTYPE, encoded_vsew << 3) != ERROR_OK)
2353  return ERROR_FAIL;
2354 
2355  if (encoded_vsew == 3 && r->vsew64_supported == YNM_MAYBE) {
2356  /* Check that it's supported. */
2357  riscv_reg_t vtype;
2358 
2359  if (riscv_reg_get(target, &vtype, GDB_REGNO_VTYPE) != ERROR_OK)
2360  return ERROR_FAIL;
2361  if (vtype >> (riscv_xlen(target) - 1)) {
2362  r->vsew64_supported = YNM_NO;
2363  /* Try again. */
2364  return try_set_vsew(target, debug_vsew);
2365  }
2366  r->vsew64_supported = YNM_YES;
2367  }
2368  *debug_vsew = encoded_vsew == 3 ? 64 : 32;
2369  return ERROR_OK;
2370 }
2371 
2373  riscv_reg_t *orig_mstatus, riscv_reg_t *orig_vtype, riscv_reg_t *orig_vl,
2374  riscv_reg_t *orig_vstart, unsigned int *debug_vl, unsigned int *debug_vsew)
2375 {
2376  assert(orig_mstatus);
2377  assert(orig_vtype);
2378  assert(orig_vl);
2379  assert(debug_vl);
2380  assert(debug_vsew);
2381 
2382  RISCV_INFO(r);
2383  if (target->state != TARGET_HALTED) {
2385  "Unable to access vector register: target not halted");
2386  return ERROR_TARGET_NOT_HALTED;
2387  }
2388  if (prep_for_register_access(target, orig_mstatus, GDB_REGNO_VL) != ERROR_OK)
2389  return ERROR_FAIL;
2390 
2391  /* Save original vstart, vtype and vl values for later restoration */
2392  if (riscv_reg_get(target, orig_vstart, GDB_REGNO_VSTART) != ERROR_OK)
2393  return ERROR_FAIL;
2394  if (riscv_reg_get(target, orig_vtype, GDB_REGNO_VTYPE) != ERROR_OK)
2395  return ERROR_FAIL;
2396  if (riscv_reg_get(target, orig_vl, GDB_REGNO_VL) != ERROR_OK)
2397  return ERROR_FAIL;
2398  /* Note: vstart may be non-zero at this point. Updating vsew (via VTYPE)
2399  * reset vstart to 0. */
2400  if (try_set_vsew(target, debug_vsew) != ERROR_OK)
2401  return ERROR_FAIL;
2402  /* Set the number of elements to be updated with results from a vector
2403  * instruction, for the vslide1down instruction.
2404  * Set it so the entire V register is updated. */
2405  *debug_vl = DIV_ROUND_UP(r->vlenb * 8, *debug_vsew);
2406  return riscv_reg_write(target, GDB_REGNO_VL, *debug_vl);
2407 }
2408 
2410  riscv_reg_t mstatus, riscv_reg_t vtype, riscv_reg_t vl, riscv_reg_t vstart)
2411 {
2412  /* Restore vtype, vl and vstart. */
2414  return ERROR_FAIL;
2416  return ERROR_FAIL;
2418  return ERROR_FAIL;
2420 }
2421 
2422 int riscv013_get_register_buf(struct target *target, uint8_t *value,
2423  enum gdb_regno regno)
2424 {
2425  assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
2426 
2428  return ERROR_FAIL;
2429 
2430  riscv_reg_t mstatus, vtype, vl, vstart;
2431  unsigned int debug_vl, debug_vsew;
2432 
2433  if (prep_for_vector_access(target, &mstatus, &vtype, &vl, &vstart,
2434  &debug_vl, &debug_vsew) != ERROR_OK)
2435  return ERROR_FAIL;
2436 
2438  return ERROR_FAIL;
2439 
2440  unsigned int vnum = regno - GDB_REGNO_V0;
2441 
2442  int result = ERROR_OK;
2443  for (unsigned int i = 0; i < debug_vl; i++) {
2444  /* Can't reuse the same program because riscv_program_exec() adds
2445  * ebreak to the end every time. */
2446  struct riscv_program program;
2447  riscv_program_init(&program, target);
2448  riscv_program_insert(&program, vmv_x_s(S0, vnum));
2449  riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
2450 
2451  /* Executing the program might result in an exception if there is some
2452  * issue with the vector implementation/instructions we're using. If that
2453  * happens, attempt to restore as usual. We may have clobbered the
2454  * vector register we tried to read already.
2455  * For other failures, we just return error because things are probably
2456  * so messed up that attempting to restore isn't going to help. */
2457  result = riscv_program_exec(&program, target);
2458  if (result == ERROR_OK) {
2459  riscv_reg_t v;
2461  return ERROR_FAIL;
2462  buf_set_u64(value, debug_vsew * i, debug_vsew, v);
2463  } else {
2465  "Failed to execute vmv/vslide1down while reading %s",
2467  break;
2468  }
2469  }
2470 
2471  if (cleanup_after_vector_access(target, mstatus, vtype, vl, vstart) != ERROR_OK)
2472  return ERROR_FAIL;
2473 
2474  return result;
2475 }
2476 
2478  const uint8_t *value)
2479 {
2480  assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
2481 
2483  return ERROR_FAIL;
2484 
2485  riscv_reg_t mstatus, vtype, vl, vstart;
2486  unsigned int debug_vl, debug_vsew;
2487 
2488  if (prep_for_vector_access(target, &mstatus, &vtype, &vl, &vstart,
2489  &debug_vl, &debug_vsew) != ERROR_OK)
2490  return ERROR_FAIL;
2491 
2493  return ERROR_FAIL;
2494 
2495  unsigned int vnum = regno - GDB_REGNO_V0;
2496 
2497  struct riscv_program program;
2498  riscv_program_init(&program, target);
2499  riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
2500  int result = ERROR_OK;
2501  for (unsigned int i = 0; i < debug_vl; i++) {
2503  buf_get_u64(value, debug_vsew * i, debug_vsew)) != ERROR_OK)
2504  return ERROR_FAIL;
2505  result = riscv_program_exec(&program, target);
2506  if (result != ERROR_OK)
2507  break;
2508  }
2509 
2510  if (cleanup_after_vector_access(target, mstatus, vtype, vl, vstart) != ERROR_OK)
2511  return ERROR_FAIL;
2512 
2513  return result;
2514 }
2515 
2516 static uint32_t sb_sbaccess(unsigned int size_bytes)
2517 {
2518  switch (size_bytes) {
2519  case 1:
2520  return set_field(0, DM_SBCS_SBACCESS, 0);
2521  case 2:
2522  return set_field(0, DM_SBCS_SBACCESS, 1);
2523  case 4:
2524  return set_field(0, DM_SBCS_SBACCESS, 2);
2525  case 8:
2526  return set_field(0, DM_SBCS_SBACCESS, 3);
2527  case 16:
2528  return set_field(0, DM_SBCS_SBACCESS, 4);
2529  }
2530  assert(0);
2531  return 0;
2532 }
2533 
2534 static unsigned int get_sbaadress_reg_count(const struct target *target)
2535 {
2537  const unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2538  return DIV_ROUND_UP(sbasize, 32);
2539 }
2540 
2541 static void batch_fill_sb_write_address(const struct target *target,
2542  struct riscv_batch *batch, target_addr_t address,
2543  enum riscv_scan_delay_class sbaddr0_delay)
2544 {
2545  /* There currently is no support for >64-bit addresses in OpenOCD. */
2546  assert(sizeof(target_addr_t) == sizeof(uint64_t));
2547  const uint32_t addresses[] = {DM_SBADDRESS0, DM_SBADDRESS1, DM_SBADDRESS2, DM_SBADDRESS3};
2548  const uint32_t values[] = {(uint32_t)address, (uint32_t)(address >> 32), 0, 0};
2549  const unsigned int reg_count = get_sbaadress_reg_count(target);
2550  assert(reg_count > 0);
2551  assert(reg_count <= ARRAY_SIZE(addresses));
2552  assert(ARRAY_SIZE(addresses) == ARRAY_SIZE(values));
2553 
2554  for (unsigned int i = reg_count - 1; i > 0; --i)
2555  riscv_batch_add_dm_write(batch, addresses[i], values[i], /* read back */ true,
2557  riscv_batch_add_dm_write(batch, addresses[0], values[0], /* read back */ true,
2558  sbaddr0_delay);
2559 }
2560 
2562  enum riscv_scan_delay_class sbaddr0_delay)
2563 {
2564  struct riscv_batch *batch = riscv_batch_alloc(target,
2566  batch_fill_sb_write_address(target, batch, address, sbaddr0_delay);
2567  const int res = batch_run_timeout(target, batch);
2568  riscv_batch_free(batch);
2569  return res;
2570 }
2571 
2572 static int batch_run(struct target *target, struct riscv_batch *batch)
2573 {
2574  RISCV_INFO(r);
2576  select_dmi(target->tap);
2577  riscv_batch_add_nop(batch);
2578  const int result = riscv_batch_run_from(batch, 0, &info->learned_delays,
2579  /*resets_delays*/ r->reset_delays_wait >= 0,
2580  r->reset_delays_wait);
2581  if (result != ERROR_OK)
2582  return result;
2583  /* TODO: To use `riscv_batch_finished_scans()` here, it is needed for
2584  * all scans to not discard input, meaning
2585  * "riscv_batch_add_dm_write(..., false)" should not be used. */
2586  const size_t finished_scans = batch->used_scans;
2587  decrement_reset_delays_counter(target, finished_scans);
2588  if (riscv_batch_was_batch_busy(batch))
2590  return ERROR_OK;
2591 }
2592 
2593 /* It is expected that during creation of the batch
2594  * "riscv_batch_add_dm_write(..., false)" was not used.
2595  */
2596 static int batch_run_timeout(struct target *target, struct riscv_batch *batch)
2597 {
2599  select_dmi(target->tap);
2600  riscv_batch_add_nop(batch);
2601 
2602  size_t finished_scans = 0;
2603  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
2604  const unsigned int old_base_delay = riscv_scan_get_delay(&info->learned_delays,
2606  int result;
2607  do {
2608  RISCV_INFO(r);
2609  result = riscv_batch_run_from(batch, finished_scans,
2610  &info->learned_delays,
2611  /*resets_delays*/ r->reset_delays_wait >= 0,
2612  r->reset_delays_wait);
2613  if (result != ERROR_OK)
2614  return result;
2615  const size_t new_finished_scans = riscv_batch_finished_scans(batch);
2616  assert(new_finished_scans >= finished_scans);
2617  decrement_reset_delays_counter(target, new_finished_scans - finished_scans);
2618  finished_scans = new_finished_scans;
2619  if (!riscv_batch_was_batch_busy(batch)) {
2620  assert(finished_scans == batch->used_scans);
2621  return ERROR_OK;
2622  }
2623  result = increase_dmi_busy_delay(target);
2624  if (result != ERROR_OK)
2625  return result;
2626  } while (timeval_ms() < then);
2627 
2628  assert(result == ERROR_OK);
2629  assert(riscv_batch_was_batch_busy(batch));
2630 
2631  /* Reset dmi_busy_delay, so the value doesn't get too big. */
2632  LOG_TARGET_DEBUG(target, "%s delay is restored to %u.",
2634  old_base_delay);
2635  riscv_scan_set_delay(&info->learned_delays, RISCV_DELAY_BASE,
2636  old_base_delay);
2637 
2638  LOG_TARGET_ERROR(target, "DMI operation didn't complete in %d seconds. "
2639  "The target is either really slow or broken. You could increase "
2640  "the timeout with riscv set_command_timeout_sec.",
2642  return ERROR_TIMEOUT_REACHED;
2643 }
2644 
2645 static int sba_supports_access(struct target *target, unsigned int size_bytes)
2646 {
2648  switch (size_bytes) {
2649  case 1:
2650  return get_field(info->sbcs, DM_SBCS_SBACCESS8);
2651  case 2:
2652  return get_field(info->sbcs, DM_SBCS_SBACCESS16);
2653  case 4:
2654  return get_field(info->sbcs, DM_SBCS_SBACCESS32);
2655  case 8:
2656  return get_field(info->sbcs, DM_SBCS_SBACCESS64);
2657  case 16:
2658  return get_field(info->sbcs, DM_SBCS_SBACCESS128);
2659  default:
2660  return 0;
2661  }
2662 }
2663 
2665  struct riscv_sample_buf *buf,
2667  int64_t until_ms)
2668 {
2670  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2671  if (sbasize == 0 || sbasize > 64) {
2672  LOG_TARGET_ERROR(target, "Memory sampling is only implemented for non-zero sbasize <= 64.");
2673  return ERROR_NOT_IMPLEMENTED;
2674  }
2675 
2676  if (get_field(info->sbcs, DM_SBCS_SBVERSION) != 1) {
2677  LOG_TARGET_ERROR(target, "Memory sampling is only implemented for SBA version 1.");
2678  return ERROR_NOT_IMPLEMENTED;
2679  }
2680 
2681  uint32_t sbcs = 0;
2682  uint32_t sbcs_valid = false;
2683 
2684  uint32_t sbaddress0 = 0;
2685  bool sbaddress0_valid = false;
2686  uint32_t sbaddress1 = 0;
2687  bool sbaddress1_valid = false;
2688 
2689  /* How often to read each value in a batch. */
2690  const unsigned int repeat = 5;
2691 
2692  unsigned int enabled_count = 0;
2693  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2694  if (config->bucket[i].enabled)
2695  enabled_count++;
2696  }
2697 
2698  while (timeval_ms() < until_ms) {
2699  /*
2700  * batch_run() adds to the batch, so we can't simply reuse the same
2701  * batch over and over. So we create a new one every time through the
2702  * loop.
2703  */
2704  struct riscv_batch *batch = riscv_batch_alloc(
2705  target, 1 + enabled_count * 5 * repeat);
2706  if (!batch)
2707  return ERROR_FAIL;
2708 
2709  unsigned int result_bytes = 0;
2710  for (unsigned int n = 0; n < repeat; n++) {
2711  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2712  if (config->bucket[i].enabled) {
2713  if (!sba_supports_access(target, config->bucket[i].size_bytes)) {
2714  LOG_TARGET_ERROR(target, "Hardware does not support SBA access for %d-byte memory sampling.",
2715  config->bucket[i].size_bytes);
2716  return ERROR_NOT_IMPLEMENTED;
2717  }
2718 
2719  uint32_t sbcs_write = DM_SBCS_SBREADONADDR;
2720  if (enabled_count == 1)
2721  sbcs_write |= DM_SBCS_SBREADONDATA;
2722  sbcs_write |= sb_sbaccess(config->bucket[i].size_bytes);
2723  if (!sbcs_valid || sbcs_write != sbcs) {
2724  riscv_batch_add_dm_write(batch, DM_SBCS, sbcs_write,
2725  true, RISCV_DELAY_BASE);
2726  sbcs = sbcs_write;
2727  sbcs_valid = true;
2728  }
2729 
2730  if (sbasize > 32 &&
2731  (!sbaddress1_valid ||
2732  sbaddress1 != config->bucket[i].address >> 32)) {
2733  sbaddress1 = config->bucket[i].address >> 32;
2735  sbaddress1, true, RISCV_DELAY_BASE);
2736  sbaddress1_valid = true;
2737  }
2738  if (!sbaddress0_valid ||
2739  sbaddress0 != (config->bucket[i].address & 0xffffffff)) {
2740  sbaddress0 = config->bucket[i].address;
2742  sbaddress0, true,
2744  sbaddress0_valid = true;
2745  }
2746  if (config->bucket[i].size_bytes > 4)
2751  result_bytes += 1 + config->bucket[i].size_bytes;
2752  }
2753  }
2754  }
2755 
2756  if (buf->used + result_bytes >= buf->size) {
2757  riscv_batch_free(batch);
2758  break;
2759  }
2760 
2761  size_t sbcs_read_index = riscv_batch_add_dm_read(batch, DM_SBCS,
2763 
2764  int result = batch_run(target, batch);
2765  if (result != ERROR_OK) {
2766  riscv_batch_free(batch);
2767  return result;
2768  }
2769 
2770  /* Discard the batch when we encounter a busy state on the DMI level.
2771  * It's too much hassle to try to recover partial data. We'll try again
2772  * with a larger DMI delay. */
2773  const uint32_t sbcs_read_op = riscv_batch_get_dmi_read_op(batch, sbcs_read_index);
2774  if (sbcs_read_op == DTM_DMI_OP_BUSY) {
2775  result = increase_dmi_busy_delay(target);
2776  riscv_batch_free(batch);
2777  if (result != ERROR_OK) {
2778  return result;
2779  }
2780  continue;
2781  }
2782 
2783  uint32_t sbcs_read = riscv_batch_get_dmi_read_data(batch, sbcs_read_index);
2784  if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
2785  /* Discard this batch when we encounter "busy error" state on the System Bus level.
2786  * We'll try next time with a larger System Bus read delay. */
2788  int res = riscv_scan_increase_delay(&info->learned_delays,
2790  riscv_batch_free(batch);
2791  if (res != ERROR_OK)
2792  return res;
2793  continue;
2794  }
2795  if (get_field(sbcs_read, DM_SBCS_SBERROR)) {
2796  /* The memory we're sampling was unreadable, somehow. Give up. */
2798  riscv_batch_free(batch);
2799  return ERROR_FAIL;
2800  }
2801 
2802  unsigned int read_count = 0;
2803  for (unsigned int n = 0; n < repeat; n++) {
2804  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2805  if (config->bucket[i].enabled) {
2807  uint64_t value = 0;
2808  if (config->bucket[i].size_bytes > 4)
2809  value = ((uint64_t)riscv_batch_get_dmi_read_data(batch, read_count++)) << 32;
2810  value |= riscv_batch_get_dmi_read_data(batch, read_count++);
2811 
2812  buf->buf[buf->used] = i;
2813  buf_set_u64(buf->buf + buf->used + 1, 0, config->bucket[i].size_bytes * 8, value);
2814  buf->used += 1 + config->bucket[i].size_bytes;
2815  }
2816  }
2817  }
2818 
2819  riscv_batch_free(batch);
2820  }
2821 
2822  return ERROR_OK;
2823 }
2824 
2825 static int sample_memory(struct target *target,
2826  struct riscv_sample_buf *buf,
2828  int64_t until_ms)
2829 {
2830  if (!config->enabled)
2831  return ERROR_OK;
2832 
2833  return sample_memory_bus_v1(target, buf, config, until_ms);
2834 }
2835 
2837 {
2840  return ERROR_FAIL;
2841 
2842  uint32_t dmstatus;
2843  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
2844  return ERROR_FAIL;
2845  if (get_field(dmstatus, DM_DMSTATUS_ANYHAVERESET)) {
2846  LOG_TARGET_INFO(target, "Hart unexpectedly reset!");
2847  info->dcsr_register_is_set = false;
2848  /* TODO: Can we make this more obvious to eg. a gdb user? */
2849  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE |
2851  dmcontrol = set_dmcontrol_hartsel(dmcontrol, info->index);
2852  /* If we had been halted when we reset, request another halt. If we
2853  * ended up running out of reset, then the user will (hopefully) get a
2854  * message that a reset happened, that the target is running, and then
2855  * that it is halted again once the request goes through.
2856  */
2857  if (target->state == TARGET_HALTED) {
2858  dmcontrol |= DM_DMCONTROL_HALTREQ;
2859  /* `haltreq` should not be issued if `abstractcs.busy`
2860  * is set. */
2861  int result = wait_for_idle_if_needed(target);
2862  if (result != ERROR_OK)
2863  return result;
2864  }
2865  dm_write(target, DM_DMCONTROL, dmcontrol);
2866  }
2867  if (get_field(dmstatus, DM_DMSTATUS_ALLNONEXISTENT)) {
2869  return ERROR_OK;
2870  }
2871  if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
2873  return ERROR_OK;
2874  }
2875  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED)) {
2877  return ERROR_OK;
2878  }
2879  if (get_field(dmstatus, DM_DMSTATUS_ALLRUNNING)) {
2881  return ERROR_OK;
2882  }
2883  LOG_TARGET_ERROR(target, "Couldn't determine state. dmstatus=0x%x", dmstatus);
2884  return ERROR_FAIL;
2885 }
2886 
2888  enum riscv_hart_state previous_riscv_state)
2889 {
2891 
2893  LOG_TARGET_WARNING(target, "Discarding values of dirty registers "
2894  "(due to target becoming unavailable).");
2895 
2897 
2898  info->dcsr_register_is_set = false;
2899  return ERROR_OK;
2900 }
2901 
2902 static int tick(struct target *target)
2903 {
2905  if (!info->dcsr_register_is_set &&
2906  target->state == TARGET_RUNNING &&
2908  return halt_set_dcsr_config(target);
2909  return ERROR_OK;
2910 }
2911 
2912 static int init_target(struct command_context *cmd_ctx,
2913  struct target *target)
2914 {
2915  LOG_TARGET_DEBUG(target, "Init.");
2916  RISCV_INFO(generic_info);
2917 
2918  generic_info->select_target = &dm013_select_target;
2919  generic_info->get_hart_state = &riscv013_get_hart_state;
2920  generic_info->resume_go = &riscv013_resume_go;
2921  generic_info->step_current_hart = &riscv013_step_current_hart;
2922  generic_info->resume_prep = &riscv013_resume_prep;
2923  generic_info->halt_prep = &riscv013_halt_prep;
2924  generic_info->halt_go = &riscv013_halt_go;
2925  generic_info->on_step = &riscv013_on_step;
2926  generic_info->halt_reason = &riscv013_halt_reason;
2927  generic_info->read_progbuf = &riscv013_read_progbuf;
2928  generic_info->write_progbuf = &riscv013_write_progbuf;
2929  generic_info->execute_progbuf = &riscv013_execute_progbuf;
2930  generic_info->invalidate_cached_progbuf = &riscv013_invalidate_cached_progbuf;
2931  generic_info->fill_dmi_write = &riscv013_fill_dmi_write;
2932  generic_info->fill_dmi_read = &riscv013_fill_dmi_read;
2933  generic_info->fill_dm_nop = &riscv013_fill_dm_nop;
2934  generic_info->get_dmi_address_bits = &riscv013_get_dmi_address_bits;
2935  generic_info->authdata_read = &riscv013_authdata_read;
2936  generic_info->authdata_write = &riscv013_authdata_write;
2937  generic_info->dmi_read = &dmi_read;
2938  generic_info->dmi_write = &dmi_write;
2939  generic_info->get_dmi_address = &riscv013_get_dmi_address;
2940  generic_info->access_memory = &riscv013_access_memory;
2941  generic_info->data_bits = &riscv013_data_bits;
2942  generic_info->print_info = &riscv013_print_info;
2943  generic_info->get_impebreak = &riscv013_get_impebreak;
2944  generic_info->get_progbufsize = &riscv013_get_progbufsize;
2945 
2946  generic_info->handle_became_unavailable = &handle_became_unavailable;
2947  generic_info->tick = &tick;
2948 
2949  if (!generic_info->version_specific) {
2950  generic_info->version_specific = calloc(1, sizeof(riscv013_info_t));
2951  if (!generic_info->version_specific)
2952  return ERROR_FAIL;
2953  }
2954  generic_info->sample_memory = sample_memory;
2956 
2957  info->progbufsize = -1;
2959 
2960  info->ac_not_supported_cache = ac_cache_construct();
2961 
2962  return ERROR_OK;
2963 }
2964 
2965 static int assert_reset(struct target *target)
2966 {
2968  int result;
2969 
2970  select_dmi(target->tap);
2971 
2973  /* Run the user-supplied script if there is one. */
2975  } else {
2976  dm013_info_t *dm = get_dm(target);
2977  if (!dm)
2978  return ERROR_FAIL;
2979 
2980  uint32_t control = set_field(0, DM_DMCONTROL_DMACTIVE, 1);
2981  control = set_dmcontrol_hartsel(control, info->index);
2982  control = set_field(control, DM_DMCONTROL_HALTREQ,
2983  target->reset_halt ? 1 : 0);
2984  control = set_field(control, DM_DMCONTROL_NDMRESET, 1);
2985  /* If `abstractcs.busy` is set, debugger should not
2986  * change `hartsel` or set `haltreq`
2987  */
2988  const bool hartsel_changed = (int)info->index != dm->current_hartid;
2989  if (hartsel_changed || target->reset_halt) {
2990  result = wait_for_idle_if_needed(target);
2991  if (result != ERROR_OK)
2992  return result;
2993  }
2994  result = dm_write(target, DM_DMCONTROL, control);
2995  if (result != ERROR_OK)
2996  return result;
2997  }
2998 
3000 
3001  /* The DM might have gotten reset if OpenOCD called us in some reset that
3002  * involves SRST being toggled. So clear our cache which may be out of
3003  * date. */
3005 }
3006 
3008 {
3009  const struct riscv_private_config * const config = riscv_private_config(target);
3010  for (int i = 0; i < N_RISCV_MODE; ++i)
3011  if (config->dcsr_ebreak_fields[i])
3012  return false;
3013  return !config->dcsr_cetrig;
3014 }
3015 
3016 static int deassert_reset(struct target *target)
3017 {
3019  dm013_info_t *dm = get_dm(target);
3020  if (!dm)
3021  return ERROR_FAIL;
3022  int result;
3023 
3024  select_dmi(target->tap);
3025  /* Clear the reset, but make sure haltreq is still set */
3026  uint32_t control = 0;
3027  control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
3028  control = set_field(control, DM_DMCONTROL_HALTREQ, target->reset_halt ? 1 : 0);
3029  control = set_dmcontrol_hartsel(control, info->index);
3030  /* If `abstractcs.busy` is set, debugger should not
3031  * change `hartsel`.
3032  */
3033  const bool hartsel_changed = (int)info->index != dm->current_hartid;
3034  if (hartsel_changed) {
3035  result = wait_for_idle_if_needed(target);
3036  if (result != ERROR_OK)
3037  return result;
3038  }
3039  result = dm_write(target, DM_DMCONTROL, control);
3040  if (result != ERROR_OK)
3041  return result;
3042 
3043  uint32_t dmstatus;
3044  const unsigned int orig_base_delay = riscv_scan_get_delay(&info->learned_delays,
3046  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
3047  LOG_TARGET_DEBUG(target, "Waiting for hart to come out of reset.");
3048  do {
3049  result = dmstatus_read(target, &dmstatus, true);
3050  if (result != ERROR_OK)
3051  return result;
3052 
3053  if (timeval_ms() > then) {
3054  LOG_TARGET_ERROR(target, "Hart didn't leave reset in %ds; "
3055  "dmstatus=0x%x (allunavail=%s, allhavereset=%s); "
3056  "Increase the timeout with riscv set_command_timeout_sec.",
3057  riscv_get_command_timeout_sec(), dmstatus,
3058  get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL) ? "true" : "false",
3059  get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET) ? "true" : "false");
3060  return ERROR_TIMEOUT_REACHED;
3061  }
3062  } while (!get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET));
3063 
3064  riscv_scan_set_delay(&info->learned_delays, RISCV_DELAY_BASE,
3065  orig_base_delay);
3066 
3067  /* Ack reset and clear DM_DMCONTROL_HALTREQ if previously set */
3068  control = 0;
3069  control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
3070  control = set_field(control, DM_DMCONTROL_ACKHAVERESET, 1);
3071  control = set_dmcontrol_hartsel(control, info->index);
3072  result = dm_write(target, DM_DMCONTROL, control);
3073  if (result != ERROR_OK)
3074  return result;
3075 
3076  if (target->reset_halt) {
3079  } else {
3082  }
3083  info->dcsr_register_is_set = dcsr_config_equals_reset_value(target);
3084  return ERROR_OK;
3085 }
3086 
3087 static int execute_autofence(struct target *target)
3088 {
3090  return ERROR_FAIL;
3091 
3092  RISCV_INFO(r);
3093  if (!r->autofence)
3094  return ERROR_OK;
3095 
3096  /* FIXME: For non-coherent systems we need to flush the caches right
3097  * here, but there's no ISA-defined way of doing that. */
3098  struct riscv_program program;
3099 
3100  /* program.execution_result may indicate RISCV_PROGBUF_EXEC_RESULT_EXCEPTION -
3101  * currently, we ignore this error since most likely this is an indication
3102  * that target does not support a fence instruction (execution of an
3103  * unsupported instruction results in "Illegal instruction" exception on
3104  * targets that comply with riscv-privilege spec).
3105  * Currently, RISC-V specification does not provide us with a portable and
3106  * less invasive way to detect if a fence is supported by the target. We may
3107  * revise this code once the spec allows us to do this */
3108  if (has_sufficient_progbuf(target, 3)) {
3109  riscv_program_init(&program, target);
3110  riscv_program_fence_i(&program);
3111  riscv_program_fence_rw_rw(&program);
3112  if (riscv_program_exec(&program, target) != ERROR_OK) {
3114  LOG_TARGET_ERROR(target, "Unexpected error during fence execution");
3115  return ERROR_FAIL;
3116  }
3117  LOG_TARGET_DEBUG(target, "Unable to execute fence.i and fence rw, rw");
3118  }
3119  LOG_TARGET_DEBUG(target, "Successfully executed fence.i and fence rw, rw");
3120  return ERROR_OK;
3121  }
3122 
3123  if (has_sufficient_progbuf(target, 2)) {
3124  riscv_program_init(&program, target);
3125  riscv_program_fence_i(&program);
3126  if (riscv_program_exec(&program, target) != ERROR_OK) {
3128  LOG_TARGET_ERROR(target, "Unexpected error during fence.i execution");
3129  return ERROR_FAIL;
3130  }
3131  LOG_TARGET_DEBUG(target, "Unable to execute fence.i");
3132  }
3133  LOG_TARGET_DEBUG(target, "Successfully executed fence.i");
3134 
3135  riscv_program_init(&program, target);
3136  riscv_program_fence_rw_rw(&program);
3137  if (riscv_program_exec(&program, target) != ERROR_OK) {
3139  LOG_TARGET_ERROR(target, "Unexpected error during fence rw, rw execution");
3140  return ERROR_FAIL;
3141  }
3142  LOG_TARGET_DEBUG(target, "Unable to execute fence rw, rw");
3143  }
3144  LOG_TARGET_DEBUG(target, "Successfully executed fence rw, rw");
3145  return ERROR_OK;
3146  }
3147 
3148  return ERROR_FAIL;
3149 }
3150 
3151 static void log_memory_access128(target_addr_t address, uint64_t value_h,
3152  uint64_t value_l, bool is_read)
3153 {
3155  return;
3156 
3157  char fmt[80];
3158  sprintf(fmt, "M[0x%" TARGET_PRIxADDR "] %ss 0x%%016" PRIx64 "%%016" PRIx64,
3159  address, is_read ? "read" : "write");
3160  LOG_DEBUG(fmt, value_h, value_l);
3161 }
3162 
3163 static void log_memory_access64(target_addr_t address, uint64_t value,
3164  unsigned int size_bytes, bool is_read)
3165 {
3167  return;
3168 
3169  char fmt[80];
3170  sprintf(fmt, "M[0x%" TARGET_PRIxADDR "] %ss 0x%%0%d" PRIx64,
3171  address, is_read ? "read" : "write", size_bytes * 2);
3172  switch (size_bytes) {
3173  case 1:
3174  value &= 0xff;
3175  break;
3176  case 2:
3177  value &= 0xffff;
3178  break;
3179  case 4:
3180  value &= 0xffffffffUL;
3181  break;
3182  case 8:
3183  break;
3184  default:
3185  assert(false);
3186  }
3187  LOG_DEBUG(fmt, value);
3188 }
3189 static void log_memory_access(target_addr_t address, uint32_t *sbvalue,
3190  unsigned int size_bytes, bool is_read)
3191 {
3192  if (size_bytes == 16) {
3193  uint64_t value_h = ((uint64_t)sbvalue[3] << 32) | sbvalue[2];
3194  uint64_t value_l = ((uint64_t)sbvalue[1] << 32) | sbvalue[0];
3195  log_memory_access128(address, value_h, value_l, is_read);
3196  } else {
3197  uint64_t value = ((uint64_t)sbvalue[1] << 32) | sbvalue[0];
3198  log_memory_access64(address, value, size_bytes, is_read);
3199  }
3200 }
3201 
3202 /* Read the relevant sbdata regs depending on size, and put the results into
3203  * buffer. */
3205  uint32_t size, uint8_t *buffer)
3206 {
3207  int result;
3208  uint32_t sbvalue[4] = { 0 };
3209  static int sbdata[4] = { DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3 };
3210  assert(size <= 16);
3211  for (int i = (size - 1) / 4; i >= 0; i--) {
3212  result = dm_read(target, &sbvalue[i], sbdata[i]);
3213  if (result != ERROR_OK)
3214  return result;
3215  buf_set_u32(buffer + i * 4, 0, 8 * MIN(size, 4), sbvalue[i]);
3216  }
3217  log_memory_access(address, sbvalue, size, true);
3218  return ERROR_OK;
3219 }
3220 
3222 {
3224  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
3225  target_addr_t address = 0;
3226  uint32_t v;
3227  if (sbasize > 32) {
3228  if (dm_read(target, &v, DM_SBADDRESS1) == ERROR_OK)
3229  address |= v;
3230  address <<= 32;
3231  }
3232  if (dm_read(target, &v, DM_SBADDRESS0) == ERROR_OK)
3233  address |= v;
3234  return address;
3235 }
3236 
3237 static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
3238 {
3239  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
3240  while (1) {
3241  if (dm_read(target, sbcs, DM_SBCS) != ERROR_OK)
3242  return ERROR_FAIL;
3243  if (!get_field(*sbcs, DM_SBCS_SBBUSY))
3244  return ERROR_OK;
3245  if (timeval_ms() > then) {
3246  LOG_TARGET_ERROR(target, "Timed out after %ds waiting for sbbusy to go low (sbcs=0x%x). "
3247  "Increase the timeout with riscv set_command_timeout_sec.",
3249  return ERROR_FAIL;
3250  }
3251  }
3252 }
3253 
3254 /* TODO: return struct mem_access_result */
3255 static int modify_privilege_for_virt2phys_mode(struct target *target, riscv_reg_t *mstatus, riscv_reg_t *mstatus_old,
3256  riscv_reg_t *dcsr, riscv_reg_t *dcsr_old)
3257 {
3258  assert(mstatus);
3259  assert(mstatus_old);
3260  assert(dcsr);
3261  assert(dcsr_old);
3263  return ERROR_OK;
3264 
3265  /* Read and save DCSR */
3267  return ERROR_FAIL;
3268  *dcsr_old = *dcsr;
3269 
3270  /* Read and save MSTATUS */
3271  if (riscv_reg_get(target, mstatus, GDB_REGNO_MSTATUS) != ERROR_OK)
3272  return ERROR_FAIL;
3273  *mstatus_old = *mstatus;
3274 
3275  /* If we come from m-mode with mprv set, we want to keep mpp */
3276  if (get_field(*dcsr, CSR_DCSR_PRV) == PRV_M)
3277  return ERROR_OK;
3278 
3279  /* mstatus.mpp <- dcsr.prv */
3280  *mstatus = set_field(*mstatus, MSTATUS_MPP, get_field(*dcsr, CSR_DCSR_PRV));
3281 
3282  /* mstatus.mprv <- 1 */
3283  *mstatus = set_field(*mstatus, MSTATUS_MPRV, 1);
3284 
3285  /* Write MSTATUS */
3286  if (*mstatus != *mstatus_old &&
3288  return ERROR_FAIL;
3289 
3290  /* dcsr.mprven <- 1 */
3292 
3293  /* Write DCSR */
3294  if (*dcsr != *dcsr_old &&
3296  return ERROR_FAIL;
3297 
3298  return ERROR_OK;
3299 }
3300 
3302  riscv_reg_t dcsr, riscv_reg_t dcsr_old)
3303 {
3305  return ERROR_OK;
3306 
3307  /* Restore MSTATUS */
3308  if (mstatus != mstatus_old &&
3309  riscv_reg_set(target, GDB_REGNO_MSTATUS, mstatus_old) != ERROR_OK)
3310  return ERROR_FAIL;
3311 
3312  /* Restore DCSR */
3313  if (dcsr != dcsr_old &&
3315  return ERROR_FAIL;
3316 
3317  return ERROR_OK;
3318 }
3319 
3320 static int read_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
3321 {
3322  assert(riscv_mem_access_is_read(args));
3323 
3324  if (args.size != args.increment) {
3325  LOG_TARGET_ERROR(target, "sba v0 reads only support size==increment");
3326  return ERROR_NOT_IMPLEMENTED;
3327  }
3328 
3329  LOG_TARGET_DEBUG(target, "System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
3330  TARGET_PRIxADDR, args.size, args.count, args.address);
3331  uint8_t *t_buffer = args.read_buffer;
3332  riscv_addr_t cur_addr = args.address;
3333  riscv_addr_t fin_addr = args.address + (args.count * args.size);
3334  uint32_t access = 0;
3335 
3336  const int DM_SBCS_SBSINGLEREAD_OFFSET = 20;
3337  const uint32_t DM_SBCS_SBSINGLEREAD = (0x1U << DM_SBCS_SBSINGLEREAD_OFFSET);
3338 
3339  const int DM_SBCS_SBAUTOREAD_OFFSET = 15;
3340  const uint32_t DM_SBCS_SBAUTOREAD = (0x1U << DM_SBCS_SBAUTOREAD_OFFSET);
3341 
3342  /* ww favorise one off reading if there is an issue */
3343  if (args.count == 1) {
3344  for (uint32_t i = 0; i < args.count; i++) {
3345  if (dm_read(target, &access, DM_SBCS) != ERROR_OK)
3346  return ERROR_FAIL;
3347  dm_write(target, DM_SBADDRESS0, cur_addr);
3348  /* size/2 matching the bit sbaccess of the spec 0.13 */
3349  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
3350  access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
3351  LOG_TARGET_DEBUG(target, "read_memory: sab: access: 0x%08x", access);
3352  dm_write(target, DM_SBCS, access);
3353  /* 3) read */
3354  uint32_t value;
3355  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3356  return ERROR_FAIL;
3357  LOG_TARGET_DEBUG(target, "read_memory: sab: value: 0x%08x", value);
3358  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3359  t_buffer += args.size;
3360  cur_addr += args.size;
3361  }
3362  return ERROR_OK;
3363  }
3364 
3365  /* has to be the same size if we want to read a block */
3366  LOG_TARGET_DEBUG(target, "Reading block until final address 0x%" PRIx64, fin_addr);
3367  if (dm_read(target, &access, DM_SBCS) != ERROR_OK)
3368  return ERROR_FAIL;
3369  /* set current address */
3370  dm_write(target, DM_SBADDRESS0, cur_addr);
3371  /* 2) write sbaccess=2, sbsingleread,sbautoread,sbautoincrement
3372  * size/2 matching the bit access of the spec 0.13 */
3373  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
3374  access = set_field(access, DM_SBCS_SBAUTOREAD, 1);
3375  access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
3376  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
3377  LOG_TARGET_DEBUG(target, "access: 0x%08x", access);
3378  dm_write(target, DM_SBCS, access);
3379 
3380  while (cur_addr < fin_addr) {
3381  LOG_TARGET_DEBUG(target, "sab:autoincrement:\r\n\tsize: %d\tcount:%d\taddress: 0x%08"
3382  PRIx64, args.size, args.count, cur_addr);
3383  /* read */
3384  uint32_t value;
3385  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3386  return ERROR_FAIL;
3387  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3388  cur_addr += args.size;
3389  t_buffer += args.size;
3390 
3391  /* if we are reaching last address, we must clear autoread */
3392  if (cur_addr == fin_addr && args.count != 1) {
3393  dm_write(target, DM_SBCS, 0);
3394  if (dm_read(target, &value, DM_SBDATA0) != ERROR_OK)
3395  return ERROR_FAIL;
3396  buf_set_u32(t_buffer, 0, 8 * args.size, value);
3397  }
3398  }
3399 
3400  uint32_t sbcs;
3401  if (dm_read(target, &sbcs, DM_SBCS) != ERROR_OK)
3402  return ERROR_FAIL;
3403 
3404  return ERROR_OK;
3405 }
3406 
3410 static int read_memory_bus_v1(struct target *target, const struct riscv_mem_access_args args)
3411 {
3412  assert(riscv_mem_access_is_read(args));
3413 
3414  const target_addr_t address = args.address;
3415  const uint32_t increment = args.increment;
3416  const uint32_t count = args.count;
3417  const uint32_t size = args.size;
3418  uint8_t *buffer = args.read_buffer;
3419 
3420  if (increment != size && increment != 0) {
3421  LOG_TARGET_ERROR(target, "sba v1 reads only support increment of size or 0");
3422  return ERROR_NOT_IMPLEMENTED;
3423  }
3424 
3425  assert(size <= 16);
3426  assert(IS_PWR_OF_2(size));
3427 
3428  dm013_info_t *dm = get_dm(target);
3429  if (!dm)
3430  return ERROR_FAIL;
3431 
3433  target_addr_t next_address = address;
3434  target_addr_t end_address = address + (increment ? count : 1) * size;
3435 
3436  /* TODO: Reading all the elements in a single batch will boost the
3437  * performance.
3438  */
3439  while (next_address < end_address) {
3440  uint32_t sbcs_write = set_field(0, DM_SBCS_SBREADONADDR, 1);
3441  sbcs_write |= sb_sbaccess(size);
3442  if (increment == size)
3443  sbcs_write = set_field(sbcs_write, DM_SBCS_SBAUTOINCREMENT, 1);
3444  if (count > 1)
3445  sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, count > 1);
3446  if (dm_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
3447  return ERROR_FAIL;
3448 
3449  /* This address write will trigger the first read. */
3451  return ERROR_FAIL;
3452 
3453  /* First read has been started. Optimistically assume that it has
3454  * completed. */
3455 
3456  static int sbdata[4] = {DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3};
3457  /* TODO: The only purpose of "sbvalue" is to be passed to
3458  * "log_memory_access()". If "log_memory_access()" were to
3459  * accept "uint8_t *" instead of "uint32_t *", "sbvalue" would
3460  * be unnecessary.
3461  */
3462  uint32_t sbvalue[4] = {0};
3463  for (uint32_t i = (next_address - address) / size; i < count - 1; i++) {
3464  const uint32_t size_in_words = DIV_ROUND_UP(size, 4);
3465  struct riscv_batch *batch = riscv_batch_alloc(target, size_in_words);
3466  /* Read of sbdata0 must be performed as last because it
3467  * starts the new bus data transfer
3468  * (in case "sbcs.sbreadondata" was set above).
3469  * We don't want to start the next bus read before we
3470  * fetch all the data from the last bus read. */
3471  for (uint32_t j = size_in_words - 1; j > 0; --j)
3472  riscv_batch_add_dm_read(batch, sbdata[j], RISCV_DELAY_BASE);
3474 
3475  int res = batch_run_timeout(target, batch);
3476  if (res != ERROR_OK) {
3477  riscv_batch_free(batch);
3478  return res;
3479  }
3480 
3481  const size_t last_key = batch->read_keys_used - 1;
3482  for (size_t k = 0; k <= last_key; ++k) {
3483  sbvalue[k] = riscv_batch_get_dmi_read_data(batch, last_key - k);
3484  buf_set_u32(buffer + i * size + k * 4, 0, MIN(32, 8 * size), sbvalue[k]);
3485  }
3486 
3487  riscv_batch_free(batch);
3488  const target_addr_t read_addr = address + i * increment;
3489  log_memory_access(read_addr, sbvalue, size, true);
3490  }
3491 
3492  uint32_t sbcs_read = 0;
3493  if (count > 1) {
3494  /* "Writes to sbcs while sbbusy is high result in undefined behavior.
3495  * A debugger must not write to sbcs until it reads sbbusy as 0." */
3496  if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
3497  return ERROR_FAIL;
3498 
3499  sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, 0);
3500  if (dm_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
3501  return ERROR_FAIL;
3502  }
3503 
3504  /* Read the last word, after we disabled sbreadondata if necessary. */
3505  if (!get_field(sbcs_read, DM_SBCS_SBERROR) &&
3506  !get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
3507  if (read_memory_bus_word(target, address + (count - 1) * increment, size,
3508  buffer + (count - 1) * size) != ERROR_OK)
3509  return ERROR_FAIL;
3510 
3511  if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
3512  return ERROR_FAIL;
3513  }
3514 
3515  if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
3516  /* We read while the target was busy. Slow down and try again.
3517  * Clear sbbusyerror, as well as readondata or readonaddr. */
3519  return ERROR_FAIL;
3520 
3521  if (get_field(sbcs_read, DM_SBCS_SBERROR) == DM_SBCS_SBERROR_NONE) {
3522  /* Read the address whose read was last completed. */
3523  next_address = sb_read_address(target);
3524 
3525  /* Read the value for the last address. It's
3526  * sitting in the register for us, but we read it
3527  * too early (sbbusyerror became set). */
3528  target_addr_t current_address = next_address - (increment ? size : 0);
3529  if (read_memory_bus_word(target, current_address, size,
3530  buffer + current_address - address) != ERROR_OK)
3531  return ERROR_FAIL;
3532  }
3533 
3534  int res = riscv_scan_increase_delay(&info->learned_delays,
3536  if (res != ERROR_OK)
3537  return res;
3538  continue;
3539  }
3540 
3541  unsigned int error = get_field(sbcs_read, DM_SBCS_SBERROR);
3542  if (error == DM_SBCS_SBERROR_NONE) {
3543  next_address = end_address;
3544  } else {
3545  /* Some error indicating the bus access failed, but not because of
3546  * something we did wrong. */
3548  return ERROR_FAIL;
3549  return ERROR_FAIL;
3550  }
3551  }
3552 
3553  return ERROR_OK;
3554 }
3555 
3556 static void log_mem_access_result(struct target *target, bool success,
3557  enum riscv_mem_access_method method, bool is_read)
3558 {
3559  RISCV_INFO(r);
3560  bool warn = false;
3561  char msg[60];
3562 
3563  /* Compose the message */
3564  snprintf(msg, 60, "%s to %s memory via %s.",
3565  success ? "Succeeded" : "Failed",
3566  is_read ? "read" : "write",
3567  (method == RISCV_MEM_ACCESS_PROGBUF) ? "program buffer" :
3568  (method == RISCV_MEM_ACCESS_SYSBUS) ? "system bus" : "abstract access");
3569 
3570  /* Determine the log message severity. Show warnings only once. */
3571  if (!success) {
3572  warn = r->mem_access_warn[method];
3573  r->mem_access_warn[method] = false;
3574  }
3575 
3576  if (warn)
3577  LOG_TARGET_WARNING(target, "%s", msg);
3578  else
3579  LOG_TARGET_DEBUG(target, "%s", msg);
3580 }
3581 
3588 };
3589 
3590 #define LIST_OF_MEM_ACCESS_RESULTS \
3591  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_OK, OK, "ok") \
3592  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_DISABLED, DISABLED, "disabled") \
3593  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED, SKIPPED, "skipped") \
3594  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR, \
3595  SKIPPED, "skipped (abstract access cmderr)") \
3596  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_NOT_PRESENT, \
3597  SKIPPED, "skipped (progbuf not present)") \
3598  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_INSUFFICIENT, \
3599  SKIPPED, "skipped (insufficient progbuf)") \
3600  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE, \
3601  SKIPPED, "skipped (unsupported access size)") \
3602  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_XLEN_TOO_SHORT, \
3603  SKIPPED, "skipped (xlen too short)") \
3604  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TARGET_NOT_HALTED, \
3605  SKIPPED, "skipped (target not halted)") \
3606  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS, \
3607  SKIPPED, "skipped (address too large)") \
3608  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE, \
3609  SKIPPED, "skipped (increment size not supported)") \
3610  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_TARGET_SELECT_FAILED, \
3611  SKIPPED, "skipped (dm target select failed)") \
3612  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_FENCE_EXEC_FAILED, \
3613  SKIPPED, "skipped (fence execution failed)") \
3614  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_SYSBUS_ACCESS_FAILED, \
3615  SKIPPED, "skipped (sysbus access failed)") \
3616  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_REG_SAVE_FAILED, \
3617  SKIPPED, "skipped (register save failed)") \
3618  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_UNKNOWN_SYSBUS_VERSION, \
3619  SKIPPED, "skipped (unknown sysbus version)") \
3620  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGRAM_WRITE_FAILED, \
3621  SKIPPED, "skipped (program write failed)") \
3622  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED, \
3623  SKIPPED, "skipped (progbuf fill failed)") \
3624  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_WRITE_ABSTRACT_ARG_FAILED, \
3625  SKIPPED, "skipped (abstract command argument write failed)") \
3626  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_SKIPPED_PRIV_MOD_FAILED, \
3627  SKIPPED, "skipped (privilege modification failed)") \
3628  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED, FAILED, "failed") \
3629  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_DM_ACCESS_FAILED, \
3630  FAILED, "failed (DM register access failed)") \
3631  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PRIV_MOD_FAILED, \
3632  FAILED, "failed (privilege modification failed)") \
3633  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_REG_READ_FAILED, \
3634  FAILED, "failed (register read failed)") \
3635  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED, \
3636  FAILED, "failed (progbuf startup failed)") \
3637  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED, \
3638  FAILED, "failed (progbuf inner failed)") \
3639  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_PROGBUF_TEARDOWN_FAILED, \
3640  FAILED, "failed (progbuf teardown failed)") \
3641  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_EXECUTE_ABSTRACT_FAILED, \
3642  FAILED, "failed (execute abstract failed)") \
3643  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_NO_FORWARD_PROGRESS, \
3644  FAILED, "failed (no forward progress)") \
3645  MEM_ACCESS_RESULT_HANDLER(MEM_ACCESS_FAILED_FENCE_EXEC_FAILED, \
3646  FAILED, "failed (fence execution failed)") \
3647 
3648 
3649 #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) name,
3652 };
3653 #undef MEM_ACCESS_RESULT_HANDLER
3654 
3655 /* Structure is intentionally used to contain the memory access result,
3656  for type safety - to avoid implicit conversions to integers. */
3659 };
3660 
3662 {
3663  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3664  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3665  == MEM_ACCESS_RESULT_TYPE_OK;
3666 
3667  switch (status.value) {
3669  }
3670  #undef MEM_ACCESS_RESULT_HANDLER
3671 
3672  LOG_ERROR("Unknown memory access status: %d", status.value);
3673  assert(false && "Unknown memory access status");
3674  return false;
3675 }
3676 
3678 {
3679  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3680  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3681  == MEM_ACCESS_RESULT_TYPE_FAILED;
3682 
3683  switch (status.value) {
3685  }
3686  #undef MEM_ACCESS_RESULT_HANDLER
3687 
3688  LOG_ERROR("Unknown memory access status: %d", status.value);
3689  assert(false && "Unknown memory access status");
3690  return true;
3691 }
3692 
3694 {
3695  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3696  case name: return MEM_ACCESS_RESULT_TYPE_##kind \
3697  == MEM_ACCESS_RESULT_TYPE_SKIPPED;
3698 
3699  switch (status.value) {
3701  }
3702  #undef MEM_ACCESS_RESULT_HANDLER
3703  LOG_ERROR("Unknown memory access status: %d", status.value);
3704  assert(false && "Unknown memory access status");
3705  return true;
3706 }
3707 
3709 {
3710  #define MEM_ACCESS_RESULT_HANDLER(name, kind, msg) \
3711  [name] = msg,
3712  static const char * const table[] = {
3714  };
3715  #undef MEM_ACCESS_RESULT_HANDLER
3716 
3717  assert(status.value < ARRAY_SIZE(table));
3718  return table[status.value];
3719 }
3720 
3722 {
3723  struct mem_access_result result = {.value = value};
3724  return result;
3725 }
3726 
3728  const struct riscv_mem_access_args args)
3729 {
3730  assert(riscv_mem_access_is_valid(args));
3731  const char *const access_type =
3732  riscv_mem_access_is_read(args) ? "read" : "write";
3733 
3734  if (!has_sufficient_progbuf(target, 1)) {
3735  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf "
3736  "- progbuf not present", access_type);
3737  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_NOT_PRESENT);
3738  }
3739  if (!has_sufficient_progbuf(target, 3)) {
3740  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3741  "insufficient progbuf size.", access_type);
3742  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_INSUFFICIENT);
3743  }
3744  if (target->state != TARGET_HALTED) {
3745  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3746  "target not halted.", access_type);
3747  return mem_access_result(MEM_ACCESS_SKIPPED_TARGET_NOT_HALTED);
3748  }
3749  if (riscv_xlen(target) < args.size * 8) {
3750  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3751  "XLEN (%d) is too short for %d-bit memory args.",
3752  access_type, riscv_xlen(target), args.size * 8);
3753  return mem_access_result(MEM_ACCESS_SKIPPED_XLEN_TOO_SHORT);
3754  }
3755  if (args.size > 8) {
3756  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3757  "unsupported size.", access_type);
3758  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3759  }
3760  if ((sizeof(args.address) * 8 > riscv_xlen(target))
3761  && (args.address >> riscv_xlen(target))) {
3762  LOG_TARGET_DEBUG(target, "Skipping mem %s via progbuf - "
3763  "progbuf only supports %u-bit address.", access_type, riscv_xlen(target));
3764  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3765  }
3766 
3767  return mem_access_result(MEM_ACCESS_OK);
3768 }
3769 
3770 static struct mem_access_result
3771 mem_should_skip_sysbus(struct target *target, const struct riscv_mem_access_args args)
3772 {
3773  assert(riscv_mem_access_is_valid(args));
3774 
3776  const bool is_read = riscv_mem_access_is_read(args);
3777  const char *const access_type = is_read ? "read" : "write";
3778 
3779  if (!sba_supports_access(target, args.size)) {
3780  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3781  "unsupported size.", access_type);
3782  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3783  }
3784  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
3785  if ((sizeof(args.address) * 8 > sbasize)
3786  && (args.address >> sbasize)) {
3787  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3788  "sba only supports %u-bit address.", access_type, sbasize);
3789  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3790  }
3791  if (is_read && args.increment != args.size
3792  && (get_field(info->sbcs, DM_SBCS_SBVERSION) == 0
3793  || args.increment != 0)) {
3794  LOG_TARGET_DEBUG(target, "Skipping mem %s via system bus - "
3795  "sba %ss only support (size == increment) or also "
3796  "size==0 for sba v1.", access_type, access_type);
3797  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE);
3798  }
3799 
3800  return mem_access_result(MEM_ACCESS_OK);
3801 }
3802 
3803 static struct mem_access_result
3804 mem_should_skip_abstract(struct target *target, const struct riscv_mem_access_args args)
3805 {
3806  assert(riscv_mem_access_is_valid(args));
3807 
3808  const bool is_read = riscv_mem_access_is_read(args);
3809  const char *const access_type = is_read ? "read" : "write";
3810  if (args.size > 8) {
3811  /* TODO: Add 128b support if it's ever used. Involves modifying
3812  read/write_abstract_arg() to work on two 64b values. */
3813  LOG_TARGET_DEBUG(target, "Skipping mem %s via abstract access - "
3814  "unsupported size: %d bits", access_type, args.size * 8);
3815  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_ACCESS_SIZE);
3816  }
3817  if ((sizeof(args.address) * 8 > riscv_xlen(target))
3818  && (args.address >> riscv_xlen(target))) {
3819  LOG_TARGET_DEBUG(target, "Skipping mem %s via abstract access - "
3820  "abstract access only supports %u-bit address.",
3821  access_type, riscv_xlen(target));
3822  return mem_access_result(MEM_ACCESS_SKIPPED_TOO_LARGE_ADDRESS);
3823  }
3824  if (is_read && args.size != args.increment) {
3825  LOG_TARGET_ERROR(target, "Skipping mem %s via abstract access - "
3826  "abstract command %ss only support (size == increment).",
3827  access_type, access_type);
3828  return mem_access_result(MEM_ACCESS_SKIPPED_UNSUPPORTED_INCREMENT_SIZE);
3829  }
3830  return mem_access_result(MEM_ACCESS_OK);
3831 }
3832 
3833 /*
3834  * Performs a memory read using memory access abstract commands. The read sizes
3835  * supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16 byte
3836  * aamsize fields in the memory access abstract command.
3837  */
3838 static struct mem_access_result
3839 read_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
3840 {
3841  assert(riscv_mem_access_is_read(args));
3842 
3843  memset(args.read_buffer, 0, args.count * args.size);
3844 
3845  /* Convert the size (bytes) to width (bits) */
3846  unsigned int width = args.size << 3;
3847 
3848  uint32_t command = access_memory_command(target, /* virtual = */ false,
3849  width, /* postincrement = */ true, /* is_write = */ false);
3850  bool use_aampostincrement = !is_command_unsupported(target, command);
3851  if (!use_aampostincrement)
3852  /* It is already known that this abstract memory
3853  * access with aampostincrement=1 is not supported.
3854  * So try aampostincrement=0 right away.
3855  *
3856  * TODO: check if new command is supported */
3857  command = access_memory_command(target, /* virtual = */ false,
3858  width, /* postincrement = */ false, /* is_write = */ false);
3859 
3860  /* Execute the reads */
3861  uint8_t *p = args.read_buffer;
3862  int result = ERROR_OK;
3863  bool updateaddr = true;
3864  unsigned int width32 = MAX(width, 32);
3865  for (uint32_t c = 0; c < args.count; c++) {
3866  /* Update the address if it is the first time or aampostincrement is not supported by the target. */
3867  if (updateaddr) {
3868  /* Set arg1 to the address: address + c * size */
3869  result = write_abstract_arg(target, 1, args.address + c * args.size, riscv_xlen(target));
3870  if (result != ERROR_OK) {
3871  LOG_TARGET_ERROR(target, "Failed to write arg1.");
3872  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3873  }
3874  }
3875 
3876  /* Execute the command */
3877  uint32_t cmderr;
3878  result = riscv013_execute_abstract_command(target, command, &cmderr);
3879  if (use_aampostincrement && result != ERROR_OK &&
3880  cmderr == CMDERR_NOT_SUPPORTED) {
3881  LOG_TARGET_DEBUG(target, "Trying the same abstract memory "
3882  "read command, but without aampostincrement");
3883  use_aampostincrement = false;
3884  command = access_memory_command(target, /* virtual = */ false,
3885  width, /* postincrement = */ false, /* is_write = */ false);
3886  result = riscv013_execute_abstract_command(target, command, &cmderr);
3887  }
3888 
3889  /* TODO:
3890  * (1) Only the 1st access can result in a 'skip'
3891  * (2) Analyze cmderr value */
3892  if (result != ERROR_OK)
3893  return mem_access_result(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR);
3894 
3895  /* Copy arg0 to buffer (rounded width up to nearest 32) */
3896  riscv_reg_t value;
3897  result = read_abstract_arg(target, &value, 0, width32);
3898  if (result != ERROR_OK)
3899  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3900  buf_set_u64(p, 0, 8 * args.size, value);
3901 
3902  if (use_aampostincrement)
3903  updateaddr = false;
3904  p += args.size;
3905  }
3906 
3907  return mem_access_result(MEM_ACCESS_OK);
3908 }
3909 
3910 /*
3911  * Performs a memory write using memory access abstract commands. The write
3912  * sizes supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16
3913  * byte aamsize fields in the memory access abstract command.
3914  */
3915 static struct mem_access_result
3916 write_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
3917 {
3918  assert(riscv_mem_access_is_write(args));
3919 
3920  int result = ERROR_OK;
3921 
3922  /* Convert the size (bytes) to width (bits) */
3923  unsigned int width = args.size << 3;
3924 
3925  uint32_t command = access_memory_command(target, /* virtual = */ false,
3926  width, /* postincrement = */ true, /* is_write = */ true);
3927  bool use_aampostincrement = !is_command_unsupported(target, command);
3928  if (!use_aampostincrement)
3929  /* It is already known that this abstract memory
3930  * access with aampostincrement=1 is not supported.
3931  * So try aampostincrement=0 right away.
3932  *
3933  * TODO: check if new command is supported */
3934  command = access_memory_command(target, /* virtual = */ false,
3935  width, /* postincrement = */ false, /* is_write = */ true);
3936 
3937  /* Execute the writes */
3938  const uint8_t *p = args.write_buffer;
3939  bool updateaddr = true;
3940  for (uint32_t c = 0; c < args.count; c++) {
3941  /* Move data to arg0 */
3942  riscv_reg_t value = buf_get_u64(p, 0, 8 * args.size);
3943  result = write_abstract_arg(target, 0, value, riscv_xlen(target));
3944  if (result != ERROR_OK) {
3945  LOG_TARGET_ERROR(target, "Failed to write arg0.");
3946  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3947  }
3948 
3949  /* Update the address if it is the first time or aampostincrement is not supported by the target. */
3950  if (updateaddr) {
3951  /* Set arg1 to the address: address + c * size */
3952  result = write_abstract_arg(target, 1, args.address + c * args.size, riscv_xlen(target));
3953  if (result != ERROR_OK) {
3954  LOG_TARGET_ERROR(target, "Failed to write arg1.");
3955  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
3956  }
3957  }
3958 
3959  /* Execute the command */
3960  uint32_t cmderr;
3961  result = riscv013_execute_abstract_command(target, command, &cmderr);
3962  if (use_aampostincrement && result != ERROR_OK &&
3963  cmderr == CMDERR_NOT_SUPPORTED) {
3964  LOG_TARGET_DEBUG(target, "Trying the same abstract memory "
3965  "write command, but without aampostincrement");
3966  use_aampostincrement = false;
3967  command = access_memory_command(target, /* virtual = */ false,
3968  width, /* postincrement = */ false, /* is_write = */ true);
3969  result = riscv013_execute_abstract_command(target, command, &cmderr);
3970  }
3971 
3972  /* TODO:
3973  * (1) Only the 1st access can result in a 'skip'
3974  * (2) Analyze cmderr value */
3975  if (result != ERROR_OK)
3976  return mem_access_result(MEM_ACCESS_SKIPPED_ABSTRACT_ACCESS_CMDERR);
3977 
3978  if (use_aampostincrement)
3979  updateaddr = false;
3980  p += args.size;
3981  }
3982 
3983  return mem_access_result(MEM_ACCESS_OK);
3984 }
3985 
3996  target_addr_t address, uint32_t increment, uint32_t index)
3997 {
3998  /* s0 holds the next address to read from.
3999  * s1 holds the next data value read.
4000  * a0 is a counter in case increment is 0.
4001  */
4002  if (register_write_direct(target, GDB_REGNO_S0, address + index * increment)
4003  != ERROR_OK)
4004  return ERROR_FAIL;
4005 
4006  if (/*is_repeated_read*/ increment == 0 &&
4008  return ERROR_FAIL;
4009 
4010  /* AC_ACCESS_REGISTER_POSTEXEC is used to trigger first stage of the
4011  * pipeline (memory -> s1) whenever this command is executed.
4012  */
4013  const uint32_t startup_command = riscv013_access_register_command(target,
4016  uint32_t cmderr;
4017  if (riscv013_execute_abstract_command(target, startup_command, &cmderr) != ERROR_OK)
4018  return ERROR_FAIL;
4019  /* TODO: we need to modify error handling here. */
4020  /* NOTE: in case of timeout cmderr is set to CMDERR_NONE */
4021 
4022  /* First read has just triggered. Result is in s1.
4023  * dm_data registers contain the previous value of s1 (garbage).
4024  */
4027  return ERROR_FAIL;
4028 
4029  /* Read garbage from dm_data0, which triggers another execution of the
4030  * program. Now dm_data contains the first good result (from s1),
4031  * and s1 the next memory value.
4032  */
4034  goto clear_abstractauto_and_fail;
4035 
4036  uint32_t abstractcs;
4037  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
4038  goto clear_abstractauto_and_fail;
4039 
4040  cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
4041  switch (cmderr) {
4042  case CMDERR_NONE:
4043  return ERROR_OK;
4044  case CMDERR_BUSY:
4045  LOG_TARGET_ERROR(target, "Unexpected busy error. This is probably a hardware bug.");
4046  /* fall through */
4047  default:
4048  LOG_TARGET_DEBUG(target, "error when reading memory, cmderr=0x%" PRIx32, cmderr);
4050  goto clear_abstractauto_and_fail;
4051  }
4052 clear_abstractauto_and_fail:
4054  return ERROR_FAIL;
4055 }
4056 
4066  uint32_t start_index, uint32_t *elements_read,
4067  const struct riscv_mem_access_args args)
4068 {
4069  assert(riscv_mem_access_is_read(args));
4070 
4072  if (res != ERROR_OK)
4073  return res;
4075  if (res != ERROR_OK)
4076  return res;
4077 
4079  return ERROR_FAIL;
4080 
4081  /* See how far we got by reading s0/a0 */
4082  uint32_t index_on_target;
4083 
4084  if (/*is_repeated_read*/ args.increment == 0) {
4085  /* s0 is constant, a0 is incremented by one each execution */
4086  riscv_reg_t counter;
4087 
4088  if (register_read_direct(target, &counter, GDB_REGNO_A0) != ERROR_OK)
4089  return ERROR_FAIL;
4090  index_on_target = counter;
4091  } else {
4092  target_addr_t address_on_target;
4093 
4094  if (register_read_direct(target, &address_on_target, GDB_REGNO_S0) != ERROR_OK)
4095  return ERROR_FAIL;
4096  index_on_target = (address_on_target - args.address) /
4097  args.increment;
4098  }
4099 
4100  /* According to the spec, if an abstract command fails, one can't make any
4101  * assumptions about dm_data registers, so all the values in the pipeline
4102  * are clobbered now and need to be reread.
4103  */
4104  const uint32_t min_index_on_target = start_index + 2;
4105  if (index_on_target < min_index_on_target) {
4106  LOG_TARGET_ERROR(target, "Arithmetic does not work correctly on the target");
4107  return ERROR_FAIL;
4108  } else if (index_on_target == min_index_on_target) {
4109  LOG_TARGET_DEBUG(target, "No forward progress");
4110  }
4111  const uint32_t next_index = (index_on_target - 2);
4112  *elements_read = next_index - start_index;
4113  LOG_TARGET_WARNING(target, "Re-reading memory from addresses 0x%"
4114  TARGET_PRIxADDR " and 0x%" TARGET_PRIxADDR ".",
4115  args.address + args.increment * next_index,
4116  args.address + args.increment * (next_index + 1));
4118  args.increment, next_index);
4119 }
4120 
4125  uint32_t start_index, uint32_t next_start_index,
4126  const struct riscv_mem_access_args args)
4127 {
4128  assert(riscv_mem_access_is_read(args));
4129 
4130  LOG_TARGET_DEBUG(target, "DMI_STATUS_BUSY encountered in batch. Memory read [%"
4131  PRIu32 ", %" PRIu32 ")", start_index, next_start_index);
4132  if (start_index == next_start_index)
4133  LOG_TARGET_DEBUG(target, "No forward progress");
4134 
4136  return ERROR_FAIL;
4138  args.increment, next_start_index);
4139 }
4140 
4145  const struct riscv_batch *batch,
4146  uint32_t start_index, uint32_t elements_to_read, uint32_t *elements_read,
4147  const struct riscv_mem_access_args args)
4148 {
4149  assert(riscv_mem_access_is_read(args));
4150 
4151  const bool two_reads_per_element = args.size > 4;
4152  const uint32_t reads_per_element = (two_reads_per_element ? 2 : 1);
4153  assert(!two_reads_per_element || riscv_xlen(target) == 64);
4154  assert(elements_to_read <= UINT32_MAX / reads_per_element);
4155  const uint32_t nreads = elements_to_read * reads_per_element;
4156  for (uint32_t curr_idx = start_index, read = 0; read < nreads; ++read) {
4157  switch (riscv_batch_get_dmi_read_op(batch, read)) {
4158  case DMI_STATUS_BUSY:
4159  *elements_read = curr_idx - start_index;
4160  return read_memory_progbuf_inner_on_dmi_busy(target, start_index, curr_idx
4161  , args);
4162  case DMI_STATUS_FAILED:
4164  "Batch memory read encountered DMI_STATUS_FAILED on read %"
4165  PRIu32, read);
4166  return ERROR_FAIL;
4167  case DMI_STATUS_SUCCESS:
4168  break;
4169  default:
4170  assert(0);
4171  }
4172  const uint32_t value = riscv_batch_get_dmi_read_data(batch, read);
4173  uint8_t * const curr_buff = args.read_buffer +
4174  curr_idx * args.size;
4175  const target_addr_t curr_addr = args.address +
4176  curr_idx * args.increment;
4177  const uint32_t size = args.size;
4178 
4179  assert(size <= 8);
4180  const bool is_odd_read = read % 2;
4181 
4182  if (two_reads_per_element && !is_odd_read) {
4183  buf_set_u32(curr_buff + 4, 0, (size * 8) - 32, value);
4184  continue;
4185  }
4186  const bool is_second_read = two_reads_per_element;
4187 
4188  buf_set_u32(curr_buff, 0, is_second_read ? 32 : (size * 8), value);
4189  log_memory_access64(curr_addr, buf_get_u64(curr_buff, 0, size * 8),
4190  size, /*is_read*/ true);
4191  ++curr_idx;
4192  }
4193  *elements_read = elements_to_read;
4194  return ERROR_OK;
4195 }
4196 
4204  struct riscv_batch *batch, const struct riscv_mem_access_args args,
4205  uint32_t start_index, uint32_t elements_to_read, uint32_t *elements_read)
4206 {
4207  assert(riscv_mem_access_is_read(args));
4208 
4209  dm013_info_t *dm = get_dm(target);
4210  if (!dm)
4211  return ERROR_FAIL;
4212 
4213  /* Abstract commands are executed while running the batch. */
4214  dm->abstract_cmd_maybe_busy = true;
4215  if (batch_run(target, batch) != ERROR_OK)
4216  return ERROR_FAIL;
4217 
4218  uint32_t abstractcs;
4219  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
4220  return ERROR_FAIL;
4221 
4222  uint32_t elements_to_extract_from_batch;
4223 
4224  uint32_t cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
4225  switch (cmderr) {
4226  case CMDERR_NONE:
4227  LOG_TARGET_DEBUG(target, "successful (partial?) memory read [%"
4228  PRIu32 ", %" PRIu32 ")", start_index, start_index + elements_to_read);
4229  elements_to_extract_from_batch = elements_to_read;
4230  break;
4231  case CMDERR_BUSY:
4232  LOG_TARGET_DEBUG(target, "memory read resulted in busy response");
4234  &elements_to_extract_from_batch, args)
4235  != ERROR_OK)
4236  return ERROR_FAIL;
4237  break;
4238  default:
4239  LOG_TARGET_DEBUG(target, "error when reading memory, cmderr=0x%" PRIx32, cmderr);
4241  return ERROR_FAIL;
4242  }
4243 
4244  if (read_memory_progbuf_inner_extract_batch_data(target, batch, start_index,
4245  elements_to_extract_from_batch, elements_read, args) != ERROR_OK)
4246  return ERROR_FAIL;
4247 
4248  return ERROR_OK;
4249 }
4250 
4252  uint32_t count, uint32_t size)
4253 {
4254  assert(size <= 8);
4255  const uint32_t two_regs_used[] = {DM_DATA1, DM_DATA0};
4256  const uint32_t one_reg_used[] = {DM_DATA0};
4257  const uint32_t reads_per_element = size > 4 ? 2 : 1;
4258  const uint32_t * const used_regs = size > 4 ? two_regs_used : one_reg_used;
4259  const uint32_t batch_capacity = riscv_batch_available_scans(batch) / reads_per_element;
4260  const uint32_t end = MIN(batch_capacity, count);
4261 
4262  for (uint32_t j = 0; j < end; ++j) {
4263  /* TODO: reuse "abstract_data_read_fill_batch()" here.
4264  * TODO: Only the read of "DM_DATA0" starts an abstract
4265  * command, so the other read can use "RISCV_DELAY_BASE"
4266  */
4267  for (uint32_t i = 0; i < reads_per_element; ++i)
4268  riscv_batch_add_dm_read(batch, used_regs[i],
4270  }
4271  return end;
4272 }
4273 
4275  const struct riscv_mem_access_args args, uint32_t *elements_read,
4276  uint32_t index, uint32_t loop_count)
4277 {
4278  assert(riscv_mem_access_is_read(args));
4279 
4281  if (!batch)
4282  return ERROR_FAIL;
4283 
4284  const uint32_t elements_to_read = read_memory_progbuf_inner_fill_batch(batch,
4285  loop_count - index, args.size);
4286 
4288  args, index, elements_to_read, elements_read);
4289  riscv_batch_free(batch);
4290  return result;
4291 }
4292 
4298  const struct riscv_mem_access_args args, uint32_t start_index)
4299 {
4300  assert(riscv_mem_access_is_read(args));
4301 
4303  "Executing one loop iteration to ensure forward progress (index=%"
4304  PRIu32 ")", start_index);
4305  const target_addr_t curr_target_address = args.address +
4306  start_index * args.increment;
4307  uint8_t * const curr_buffer_address = args.read_buffer +
4308  start_index * args.size;
4309  const struct riscv_mem_access_args curr_access = {
4310  .read_buffer = curr_buffer_address,
4311  .address = curr_target_address,
4312  .size = args.size,
4313  .increment = args.increment,
4314  };
4315  uint32_t elements_read;
4316  if (read_memory_progbuf_inner_try_to_read(target, curr_access, &elements_read,
4317  /*index*/ 0, /*loop_count*/ 1) != ERROR_OK)
4318  return ERROR_FAIL;
4319 
4320  if (elements_read != 1) {
4321  assert(elements_read == 0);
4322  LOG_TARGET_DEBUG(target, "Can not ensure forward progress");
4323  /* FIXME: Here it would be better to retry the read and fail only if the
4324  * delay is greater then some threshold.
4325  */
4326  return ERROR_FAIL;
4327  }
4328  return ERROR_OK;
4329 }
4330 
4331 static void set_buffer_and_log_read(const struct riscv_mem_access_args args,
4332  uint32_t index, uint64_t value)
4333 {
4334  assert(riscv_mem_access_is_read(args));
4335 
4336  uint8_t * const buffer = args.read_buffer;
4337  const uint32_t size = args.size;
4338  const uint32_t increment = args.increment;
4339  const target_addr_t address = args.address;
4340 
4341  assert(size <= 8);
4342  buf_set_u64(buffer + index * size, 0, 8 * size, value);
4343  log_memory_access64(address + index * increment, value, size,
4344  /*is_read*/ true);
4345 }
4346 
4348  const struct riscv_mem_access_args args, uint32_t index)
4349 {
4350  assert(args.size <= 8);
4351  uint64_t value;
4352  int result = read_abstract_arg(target, &value, /*index*/ 0,
4353  args.size > 4 ? 64 : 32);
4354  if (result == ERROR_OK)
4355  set_buffer_and_log_read(args, index, value);
4356  return result;
4357 }
4358 
4359 static struct mem_access_result read_word_from_s1(struct target *target,
4360  const struct riscv_mem_access_args args, uint32_t index)
4361 {
4362  assert(riscv_mem_access_is_read(args));
4363 
4364  uint64_t value;
4365 
4367  return mem_access_result(MEM_ACCESS_FAILED_REG_READ_FAILED);
4368  set_buffer_and_log_read(args, index, value);
4369  return mem_access_result(MEM_ACCESS_OK);
4370 }
4371 
4373  uint32_t increment, uint32_t size)
4374 {
4375  const bool is_repeated_read = increment == 0;
4376 
4378  return ERROR_FAIL;
4380  return ERROR_FAIL;
4381  if (is_repeated_read && riscv013_reg_save(target, GDB_REGNO_A0) != ERROR_OK)
4382  return ERROR_FAIL;
4383 
4384  struct riscv_program program;
4385 
4386  riscv_program_init(&program, target);
4387  if (riscv_program_load(&program, GDB_REGNO_S1, GDB_REGNO_S0, 0, size) != ERROR_OK)
4388  return ERROR_FAIL;
4389  if (is_repeated_read) {
4390  if (riscv_program_addi(&program, GDB_REGNO_A0, GDB_REGNO_A0, 1)
4391  != ERROR_OK)
4392  return ERROR_FAIL;
4393  } else {
4395  increment)
4396  != ERROR_OK)
4397  return ERROR_FAIL;
4398  }
4399  if (riscv_program_ebreak(&program) != ERROR_OK)
4400  return ERROR_FAIL;
4401  if (riscv_program_write(&program) != ERROR_OK)
4402  return ERROR_FAIL;
4403 
4404  return ERROR_OK;
4405 }
4406 
4412 static struct mem_access_result
4414 {
4415  assert(riscv_mem_access_is_read(args));
4416  assert(args.count > 1 && "If count == 1, read_memory_progbuf_inner_one must be called");
4417 
4419  args.increment, args.size) != ERROR_OK)
4420  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
4421 
4422  if (read_memory_progbuf_inner_startup(target, args.address,
4423  args.increment, /*index*/ 0) != ERROR_OK)
4424  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED);
4425  /* The program in program buffer is executed twice during
4426  * read_memory_progbuf_inner_startup().
4427  * Here:
4428  * dm_data[0:1] == M[address]
4429  * s1 == M[address + increment]
4430  * s0 == address + increment * 2
4431  * `count - 2` program executions are performed in this loop.
4432  * No need to execute the program any more, since S1 will already contain
4433  * M[address + increment * (count - 1)] and we can read it directly.
4434  */
4435  const uint32_t loop_count = args.count - 2;
4436 
4437  for (uint32_t index = 0; index < loop_count;) {
4438  uint32_t elements_read;
4439  if (read_memory_progbuf_inner_try_to_read(target, args, &elements_read,
4440  index, loop_count) != ERROR_OK) {
4442  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED);
4443  }
4444  if (elements_read == 0) {
4446  index) != ERROR_OK) {
4448  return mem_access_result(MEM_ACCESS_FAILED_NO_FORWARD_PROGRESS);
4449  }
4450  elements_read = 1;
4451  }
4452  index += elements_read;
4453  assert(index <= loop_count);
4454  }
4456  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
4457 
4458  /* Read the penultimate word. */
4460  args, args.count - 2) != ERROR_OK)
4461  return mem_access_result(MEM_ACCESS_FAILED_DM_ACCESS_FAILED);
4462  /* Read the last word. */
4463  return read_word_from_s1(target, args, args.count - 1);
4464 }
4465 
4470 static struct mem_access_result
4472 {
4473  assert(riscv_mem_access_is_read(args));
4474 
4476  return mem_access_result(MEM_ACCESS_SKIPPED_REG_SAVE_FAILED);
4477 
4478  struct riscv_program program;
4479 
4480  riscv_program_init(&program, target);
4482  /* offset = */ 0, args.size) != ERROR_OK
4483  || riscv_program_ebreak(&program) != ERROR_OK)
4484  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
4485 
4486  if (riscv_program_write(&program) != ERROR_OK)
4487  return mem_access_result(MEM_ACCESS_SKIPPED_PROGRAM_WRITE_FAILED);
4488 
4489  /* Write address to S1, and execute buffer. */
4490  if (write_abstract_arg(target, /* index = */ 0,
4491  args.address, riscv_xlen(target)) != ERROR_OK)
4492  return mem_access_result(MEM_ACCESS_SKIPPED_WRITE_ABSTRACT_ARG_FAILED);
4496  uint32_t cmderr;
4498  return mem_access_result(MEM_ACCESS_FAILED_EXECUTE_ABSTRACT_FAILED);
4499 
4500  return read_word_from_s1(target, args, 0);
4501 }
4502 
4506 static struct mem_access_result
4507 read_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
4508 {
4509  assert(riscv_mem_access_is_read(args));
4510 
4511  select_dmi(target->tap);
4512  memset(args.read_buffer, 0, args.count * args.size);
4513 
4515  return mem_access_result(MEM_ACCESS_SKIPPED_FENCE_EXEC_FAILED);
4516 
4517  return (args.count == 1) ?
4520 }
4521 
4522 static struct mem_access_result
4523 write_memory_progbuf(struct target *target, const struct riscv_mem_access_args args);
4524 
4525 static struct mem_access_result
4526 access_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
4527 {
4528  struct mem_access_result skip_reason = mem_should_skip_progbuf(target, args);
4529  if (!is_mem_access_ok(skip_reason))
4530  return skip_reason;
4531 
4532  const bool is_read = riscv_mem_access_is_read(args);
4533  const char *const access_type = is_read ? "reading" : "writing";
4534  LOG_TARGET_DEBUG(target, "%s %" PRIu32 " words of %" PRIu32
4535  " bytes at 0x%" TARGET_PRIxADDR, access_type, args.count,
4536  args.size, args.address);
4537 
4539  return mem_access_result(MEM_ACCESS_SKIPPED_TARGET_SELECT_FAILED);
4540 
4541  riscv_reg_t mstatus = 0;
4542  riscv_reg_t mstatus_old = 0;
4543  riscv_reg_t dcsr = 0;
4544  riscv_reg_t dcsr_old = 0;
4546  &mstatus, &mstatus_old, &dcsr, &dcsr_old) != ERROR_OK)
4547  return mem_access_result(MEM_ACCESS_SKIPPED_PRIV_MOD_FAILED);
4548 
4549  struct mem_access_result result = is_read ?
4550  read_memory_progbuf(target, args) :
4552 
4554  mstatus, mstatus_old, dcsr, dcsr_old) != ERROR_OK)
4555  return mem_access_result(MEM_ACCESS_FAILED_PRIV_MOD_FAILED);
4556 
4557  return result;
4558 }
4559 
4560 static int
4561 write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args);
4562 static int
4564 
4565 static struct mem_access_result
4566 access_memory_sysbus(struct target *target, const struct riscv_mem_access_args args)
4567 {
4568  assert(riscv_mem_access_is_valid(args));
4569 
4570  struct mem_access_result skip_reason = mem_should_skip_sysbus(target, args);
4571  if (!is_mem_access_ok(skip_reason))
4572  return skip_reason;
4573 
4575  int ret = ERROR_FAIL;
4576  const bool is_read = riscv_mem_access_is_read(args);
4577  const uint64_t sbver = get_field(info->sbcs, DM_SBCS_SBVERSION);
4578  if (sbver == 0) {
4579  ret = is_read ? read_memory_bus_v0(target, args) :
4580  write_memory_bus_v0(target, args);
4581  } else if (sbver == 1) {
4582  ret = is_read ? read_memory_bus_v1(target, args) :
4583  write_memory_bus_v1(target, args);
4584  } else {
4585  LOG_TARGET_ERROR(target, "Unknown system bus version: %" PRIu64, sbver);
4586  return mem_access_result(MEM_ACCESS_SKIPPED_UNKNOWN_SYSBUS_VERSION);
4587  }
4588 
4589  return mem_access_result(ret == ERROR_OK ?
4590  MEM_ACCESS_OK : MEM_ACCESS_SKIPPED_SYSBUS_ACCESS_FAILED);
4591 }
4592 
4593 static struct mem_access_result
4594 access_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
4595 {
4596  assert(riscv_mem_access_is_valid(args));
4597 
4598  struct mem_access_result skip_reason = mem_should_skip_abstract(target, args);
4599  if (!is_mem_access_ok(skip_reason))
4600  return skip_reason;
4601 
4602  const bool is_read = riscv_mem_access_is_read(args);
4603  const char *const access_type = is_read ? "reading" : "writing";
4604  LOG_TARGET_DEBUG(target, "%s %d words of %d bytes at 0x%"
4605  TARGET_PRIxADDR, access_type, args.count,
4606  args.size, args.address);
4607 
4608  return is_read ? read_memory_abstract(target, args) :
4610 }
4611 
4612 static int
4614 {
4615  assert(riscv_mem_access_is_valid(args));
4616 
4617  const bool is_read = riscv_mem_access_is_read(args);
4618  const char *const access_type = is_read ? "read" : "write";
4619  if (!is_read && args.increment != args.size) {
4620  LOG_TARGET_ERROR(target, "Write increment size has to be equal to element size");
4621  return ERROR_NOT_IMPLEMENTED;
4622  }
4623 
4624  if (!IS_PWR_OF_2(args.size) || args.size < 1 || args.size > 16) {
4625  LOG_TARGET_ERROR(target, "BUG: Unsupported size for "
4626  "memory %s: %d", access_type, args.size);
4627  return ERROR_FAIL;
4628  }
4629 
4630  struct mem_access_result skip_reason[] = {
4631  [RISCV_MEM_ACCESS_PROGBUF] = mem_access_result(MEM_ACCESS_DISABLED),
4632  [RISCV_MEM_ACCESS_SYSBUS] = mem_access_result(MEM_ACCESS_DISABLED),
4633  [RISCV_MEM_ACCESS_ABSTRACT] = mem_access_result(MEM_ACCESS_DISABLED),
4634  };
4635 
4636  RISCV_INFO(r);
4637  for (unsigned int i = 0; i < r->num_enabled_mem_access_methods; ++i) {
4638  enum riscv_mem_access_method method = r->mem_access_methods[i];
4639  switch (method) {
4641  skip_reason[method] = access_memory_progbuf(target, args);
4642  break;
4644  skip_reason[method] = access_memory_sysbus(target, args);
4645  break;
4647  skip_reason[method] = access_memory_abstract(target, args);
4648  break;
4649  default:
4650  LOG_TARGET_ERROR(target, "Unknown memory access method: %d", method);
4651  assert(false && "Unknown memory access method");
4652  goto failure;
4653  }
4654 
4655  if (is_mem_access_failed(skip_reason[method]))
4656  goto failure;
4657 
4658  const bool success = is_mem_access_ok(skip_reason[method]);
4659  log_mem_access_result(target, success, method, is_read);
4660  if (success)
4661  return ERROR_OK;
4662  }
4663 
4664 failure:
4665  LOG_TARGET_ERROR(target, "Failed to %s memory (addr=0x%" PRIx64 ")\n"
4666  " progbuf=%s, sysbus=%s, abstract=%s", access_type, args.address,
4670  return ERROR_FAIL;
4671 }
4672 
4673 static int write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
4674 {
4675  assert(riscv_mem_access_is_write(args));
4676 
4677  /*1) write sbaddress: for singlewrite and autoincrement, we need to write the address once*/
4678  LOG_TARGET_DEBUG(target, "System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
4679  TARGET_PRIxADDR, args.size, args.count, args.address);
4681  int64_t value = 0;
4682  int64_t access = 0;
4683  riscv_addr_t offset = 0;
4684  riscv_addr_t t_addr = 0;
4685  const uint8_t *t_buffer = args.write_buffer + offset;
4686 
4687  /* B.8 Writing Memory, single write check if we write in one go */
4688  if (args.count == 1) { /* count is in bytes here */
4689  value = buf_get_u64(t_buffer, 0, 8 * args.size);
4690 
4691  access = 0;
4692  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
4693  dm_write(target, DM_SBCS, access);
4694  LOG_TARGET_DEBUG(target, " access: 0x%08" PRIx64, access);
4695  LOG_TARGET_DEBUG(target, " write_memory:SAB: ONE OFF: value 0x%08" PRIx64, value);
4697  return ERROR_OK;
4698  }
4699 
4700  /*B.8 Writing Memory, using autoincrement*/
4701 
4702  access = 0;
4703  access = set_field(access, DM_SBCS_SBACCESS, args.size / 2);
4704  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
4705  LOG_TARGET_DEBUG(target, " access: 0x%08" PRIx64, access);
4706  dm_write(target, DM_SBCS, access);
4707 
4708  /*2)set the value according to the size required and write*/
4709  for (riscv_addr_t i = 0; i < args.count; ++i) {
4710  offset = args.size * i;
4711  /* for monitoring only */
4712  t_addr = args.address + offset;
4713  t_buffer = args.write_buffer + offset;
4714 
4715  value = buf_get_u64(t_buffer, 0, 8 * args.size);
4716  LOG_TARGET_DEBUG(target, "SAB:autoincrement: expected address: 0x%08x value: 0x%08x"
4717  PRIx64, (uint32_t)t_addr, (uint32_t)value);
4719  }
4720  /*reset the autoincrement when finished (something weird is happening if this is not done at the end*/
4721  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 0);
4722  dm_write(target, DM_SBCS, access);
4723 
4724  return ERROR_OK;
4725 }
4726 
4727 static int write_memory_bus_v1(struct target *target, const struct riscv_mem_access_args args)
4728 {
4729  assert(riscv_mem_access_is_write(args));
4730 
4732  uint32_t sbcs = sb_sbaccess(args.size);
4733  sbcs = set_field(sbcs, DM_SBCS_SBAUTOINCREMENT, 1);
4734  dm_write(target, DM_SBCS, sbcs);
4735 
4736  target_addr_t next_address = args.address;
4737  target_addr_t end_address = args.address + args.count * args.size;
4738 
4739  int result = sb_write_address(target, next_address, RISCV_DELAY_BASE);
4740  if (result != ERROR_OK)
4741  return result;
4742 
4743  while (next_address < end_address) {
4744  LOG_TARGET_DEBUG(target, "Transferring burst starting at address 0x%" TARGET_PRIxADDR,
4745  next_address);
4746 
4748  if (!batch)
4749  return ERROR_FAIL;
4750 
4751  for (uint32_t i = (next_address - args.address) / args.size; i < args.count; i++) {
4752  const uint8_t *p = args.write_buffer + i * args.size;
4753 
4754  if (riscv_batch_available_scans(batch) < (args.size + 3) / 4)
4755  break;
4756 
4757  uint32_t sbvalue[4] = { 0 };
4758  if (args.size > 12) {
4759  sbvalue[3] = buf_get_u32(&p[12],
4760  /* first = */ 0, /* bit_num = */ 32);
4761  riscv_batch_add_dm_write(batch, DM_SBDATA3, sbvalue[3], false,
4763  }
4764 
4765  if (args.size > 8) {
4766  sbvalue[2] = buf_get_u32(&p[8],
4767  /* first = */ 0, /* bit_num = */ 32);
4768  riscv_batch_add_dm_write(batch, DM_SBDATA2, sbvalue[2], false,
4770  }
4771  if (args.size > 4) {
4772  sbvalue[1] = buf_get_u32(&p[4],
4773  /* first = */ 0, /* bit_num = */ 32);
4774  riscv_batch_add_dm_write(batch, DM_SBDATA1, sbvalue[1], false,
4776  }
4777 
4778  sbvalue[0] = p[0];
4779  if (args.size > 2) {
4780  sbvalue[0] |= ((uint32_t)p[2]) << 16;
4781  sbvalue[0] |= ((uint32_t)p[3]) << 24;
4782  }
4783  if (args.size > 1)
4784  sbvalue[0] |= ((uint32_t)p[1]) << 8;
4785 
4786  riscv_batch_add_dm_write(batch, DM_SBDATA0, sbvalue[0], false,
4788 
4789  log_memory_access(args.address + i * args.size, sbvalue, args.size, false);
4790 
4791  next_address += args.size;
4792  }
4793 
4794  /* Execute the batch of writes */
4795  result = batch_run(target, batch);
4796  if (result != ERROR_OK) {
4797  riscv_batch_free(batch);
4798  return result;
4799  }
4800 
4801  bool dmi_busy_encountered = riscv_batch_was_batch_busy(batch);
4802  riscv_batch_free(batch);
4803  if (dmi_busy_encountered)
4804  LOG_TARGET_DEBUG(target, "DMI busy encountered during system bus write.");
4805 
4806  result = read_sbcs_nonbusy(target, &sbcs);
4807  if (result != ERROR_OK)
4808  return result;
4809 
4810  if (get_field(sbcs, DM_SBCS_SBBUSYERROR)) {
4811  /* We wrote while the target was busy. */
4812  LOG_TARGET_DEBUG(target, "Sbbusyerror encountered during system bus write.");
4813  /* Clear the sticky error flag. */
4815  /* Slow down before trying again.
4816  * FIXME: Possible overflow is ignored here.
4817  */
4818  riscv_scan_increase_delay(&info->learned_delays,
4820  }
4821 
4822  if (get_field(sbcs, DM_SBCS_SBBUSYERROR) || dmi_busy_encountered) {
4823  /* Recover from the case when the write commands were issued too fast.
4824  * Determine the address from which to resume writing. */
4825  next_address = sb_read_address(target);
4826  if (next_address < args.address) {
4827  /* This should never happen, probably buggy hardware. */
4828  LOG_TARGET_DEBUG(target, "unexpected sbaddress=0x%" TARGET_PRIxADDR
4829  " - buggy sbautoincrement in hw?", next_address);
4830  /* Fail the whole operation. */
4831  return ERROR_FAIL;
4832  }
4833  /* Try again - resume writing. */
4834  continue;
4835  }
4836 
4837  unsigned int sberror = get_field(sbcs, DM_SBCS_SBERROR);
4838  if (sberror != 0) {
4839  /* Sberror indicates the bus access failed, but not because we issued the writes
4840  * too fast. Cannot recover. Sbaddress holds the address where the error occurred
4841  * (unless sbautoincrement in the HW is buggy).
4842  */
4843  target_addr_t sbaddress = sb_read_address(target);
4844  LOG_TARGET_DEBUG(target, "System bus access failed with sberror=%u (sbaddress=0x%" TARGET_PRIxADDR ")",
4845  sberror, sbaddress);
4846  if (sbaddress < args.address) {
4847  /* This should never happen, probably buggy hardware.
4848  * Make a note to the user not to trust the sbaddress value. */
4849  LOG_TARGET_DEBUG(target, "unexpected sbaddress=0x%" TARGET_PRIxADDR
4850  " - buggy sbautoincrement in hw?", next_address);
4851  }
4852  /* Clear the sticky error flag */
4854  /* Fail the whole operation */
4855  return ERROR_FAIL;
4856  }
4857  }
4858 
4859  return ERROR_OK;
4860 }
4861 
4874  const uint8_t *buffer, uint32_t size)
4875 {
4876  /* TODO: There is potential to gain some performance if the operations below are
4877  * executed inside the first DMI batch (not separately). */
4878  if (register_write_direct(target, GDB_REGNO_S0, *address_p) != ERROR_OK)
4879  return ERROR_FAIL;
4880 
4881  /* Write the first item to data0 [, data1] */
4882  assert(size <= 8);
4883  const uint64_t value = buf_get_u64(buffer, 0, 8 * size);
4884  if (write_abstract_arg(target, /*index*/ 0, value, size > 4 ? 64 : 32)
4885  != ERROR_OK)
4886  return ERROR_FAIL;
4887 
4888  /* Write and execute command that moves the value from data0 [, data1]
4889  * into S1 and executes program buffer. */
4895 
4896  uint32_t cmderr;
4898  return ERROR_FAIL;
4899 
4900  log_memory_access64(*address_p, value, size, /*is_read*/ false);
4901 
4902  /* The execution of the command succeeded, which means:
4903  * - write of the first item to memory succeeded
4904  * - address on the target (S0) was incremented
4905  */
4906  *address_p += size;
4907 
4908  /* TODO: Setting abstractauto.autoexecdata is not necessary for a write
4909  * of one element. */
4912 }
4913 
4918 {
4919  return dm_write(target, DM_ABSTRACTAUTO, 0);
4920 }
4921 
4928  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
4929  const uint8_t *buffer)
4930 {
4932  if (res != ERROR_OK)
4933  return res;
4935  if (res != ERROR_OK)
4936  return res;
4937 
4939  return ERROR_FAIL;
4940 
4941  target_addr_t address_on_target;
4942  if (register_read_direct(target, &address_on_target, GDB_REGNO_S0) != ERROR_OK)
4943  return ERROR_FAIL;
4944  const uint8_t * const curr_buff = buffer + (address_on_target - *address_p);
4945  *address_p = address_on_target;
4946  if (*address_p == end_address) {
4947  LOG_TARGET_DEBUG(target, "Got busy while reading after reading the last element");
4948  return ERROR_OK;
4949  }
4950  LOG_TARGET_DEBUG(target, "Restarting from 0x%" TARGET_PRIxADDR, *address_p);
4951  /* This restores the pipeline and ensures one item gets reliably written */
4952  return write_memory_progbuf_startup(target, address_p, curr_buff, size);
4953 }
4954 
4960  target_addr_t start_address, target_addr_t end_address, uint32_t size,
4961  const uint8_t *buffer)
4962 {
4963  assert(size <= 8);
4964  const unsigned int writes_per_element = size > 4 ? 2 : 1;
4965  const size_t batch_capacity = riscv_batch_available_scans(batch) / writes_per_element;
4966  /* This is safe even for the edge case when writing at the very top of
4967  * the 64-bit address space (in which case end_address overflows to 0).
4968  */
4969  const target_addr_t batch_end_address = start_address +
4970  MIN((target_addr_t)batch_capacity * size,
4971  end_address - start_address);
4972  for (target_addr_t address = start_address; address != batch_end_address;
4973  address += size, buffer += size) {
4974  assert(size <= 8);
4975  const uint64_t value = buf_get_u64(buffer, 0, 8 * size);
4976  log_memory_access64(address, value, size, /*is_read*/ false);
4977  if (writes_per_element == 2)
4979  (uint32_t)(value >> 32), false, RISCV_DELAY_BASE);
4980  riscv_batch_add_dm_write(batch, DM_DATA0, (uint32_t)value, false,
4982  }
4983  return batch_end_address;
4984 }
4985 
4990 static int write_memory_progbuf_run_batch(struct target *target, struct riscv_batch *batch,
4991  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
4992  const uint8_t *buffer)
4993 {
4994  dm013_info_t *dm = get_dm(target);
4995  if (!dm)
4996  return ERROR_FAIL;
4997 
4998  /* Abstract commands are executed while running the batch. */
4999  dm->abstract_cmd_maybe_busy = true;
5000  if (batch_run(target, batch) != ERROR_OK)
5001  return ERROR_FAIL;
5002 
5003  /* Note that if the scan resulted in a Busy DMI response, it
5004  * is this call to wait_for_idle() that will cause the dmi_busy_delay
5005  * to be incremented if necessary. */
5006  uint32_t abstractcs;
5007 
5008  if (wait_for_idle(target, &abstractcs) != ERROR_OK)
5009  return ERROR_FAIL;
5010 
5011  uint32_t cmderr = get_field32(abstractcs, DM_ABSTRACTCS_CMDERR);
5012  const bool dmi_busy_encountered = riscv_batch_was_batch_busy(batch);
5013  if (cmderr == CMDERR_NONE && !dmi_busy_encountered) {
5014  LOG_TARGET_DEBUG(target, "Successfully written memory block M[0x%" TARGET_PRIxADDR
5015  ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
5016  *address_p = end_address;
5017  return ERROR_OK;
5018  } else if (cmderr == CMDERR_BUSY || dmi_busy_encountered) {
5019  if (cmderr == CMDERR_BUSY)
5020  LOG_TARGET_DEBUG(target, "Encountered abstract command busy response while writing block M[0x%"
5021  TARGET_PRIxADDR ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
5022  if (dmi_busy_encountered)
5023  LOG_TARGET_DEBUG(target, "Encountered DMI busy response while writing block M[0x%"
5024  TARGET_PRIxADDR ".. 0x%" TARGET_PRIxADDR ")", *address_p, end_address);
5025  /* TODO: If dmi busy is encountered, the address of the last
5026  * successful write can be deduced by analysing the batch.
5027  */
5028  return write_memory_progbuf_handle_busy(target, address_p, end_address,
5029  size, buffer);
5030  }
5031  LOG_TARGET_ERROR(target, "Error when writing memory, abstractcs=0x%" PRIx32,
5032  abstractcs);
5034  return ERROR_FAIL;
5035 }
5036 
5038  target_addr_t *address_p, target_addr_t end_address, uint32_t size,
5039  const uint8_t *buffer)
5040 {
5042  if (!batch)
5043  return ERROR_FAIL;
5044 
5045  const target_addr_t batch_end_addr = write_memory_progbuf_fill_batch(batch,
5046  *address_p, end_address, size, buffer);
5047 
5048  int result = write_memory_progbuf_run_batch(target, batch, address_p,
5049  batch_end_addr, size, buffer);
5050  riscv_batch_free(batch);
5051  return result;
5052 }
5053 
5055 {
5057  return ERROR_FAIL;
5059  return ERROR_FAIL;
5060 
5061  struct riscv_program program;
5062 
5063  riscv_program_init(&program, target);
5065  return ERROR_FAIL;
5066 
5067  if (riscv_program_addi(&program, GDB_REGNO_S0, GDB_REGNO_S0, (int16_t)size) != ERROR_OK)
5068  return ERROR_FAIL;
5069 
5070  if (riscv_program_ebreak(&program) != ERROR_OK)
5071  return ERROR_FAIL;
5072 
5073  return riscv_program_write(&program);
5074 }
5075 
5076 static struct mem_access_result
5078  const struct riscv_mem_access_args args)
5079 {
5080  assert(riscv_mem_access_is_write(args));
5081 
5083  return mem_access_result(MEM_ACCESS_SKIPPED_PROGBUF_FILL_FAILED);
5084 
5085  target_addr_t addr_on_target = args.address;
5086  if (write_memory_progbuf_startup(target, &addr_on_target,
5087  args.write_buffer, args.size) != ERROR_OK)
5088  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_STARTUP_FAILED);
5089 
5090  const target_addr_t end_addr = args.address + (target_addr_t)args.size * args.count;
5091 
5092  for (target_addr_t next_addr_on_target = addr_on_target; addr_on_target != end_addr;
5093  addr_on_target = next_addr_on_target) {
5094  const uint8_t * const curr_buff = args.write_buffer + (addr_on_target - args.address);
5095  if (write_memory_progbuf_try_to_write(target, &next_addr_on_target,
5096  end_addr, args.size, curr_buff) != ERROR_OK) {
5098  return mem_access_result(MEM_ACCESS_FAILED_PROGBUF_INNER_FAILED);
5099  }
5100  /* write_memory_progbuf_try_to_write() ensures that at least one item
5101  * gets successfully written even when busy condition is encountered.
5102  * These assertions shuld hold when next_address_on_target overflows. */
5103  assert(next_addr_on_target - addr_on_target > 0);
5104  assert(next_addr_on_target - args.address <= (target_addr_t)args.size * args.count);
5105  }
5106 
5108  mem_access_result(MEM_ACCESS_OK) :
5109  mem_access_result(MEM_ACCESS_FAILED_PROGBUF_TEARDOWN_FAILED);
5110 }
5111 
5112 static struct mem_access_result
5113 write_memory_progbuf(struct target *target, const struct riscv_mem_access_args args)
5114 {
5115  assert(riscv_mem_access_is_write(args));
5116 
5117  struct mem_access_result result = write_memory_progbuf_inner(target, args);
5118 
5120  return mem_access_result(MEM_ACCESS_FAILED_FENCE_EXEC_FAILED);
5121 
5122  return result;
5123 }
5124 
5125 static bool riscv013_get_impebreak(const struct target *target)
5126 {
5127  RISCV013_INFO(r);
5128  return r->impebreak;
5129 }
5130 
5131 static unsigned int riscv013_get_progbufsize(const struct target *target)
5132 {
5133  RISCV013_INFO(r);
5134  return r->progbufsize;
5135 }
5136 
5137 
5138 struct target_type riscv013_target = {
5139  .name = "riscv",
5140 
5141  .init_target = init_target,
5142  .deinit_target = deinit_target,
5143  .examine = examine,
5144 
5145  .poll = &riscv_openocd_poll,
5146  .halt = &riscv_halt,
5147  .step = &riscv_openocd_step,
5148 
5149  .assert_reset = assert_reset,
5150  .deassert_reset = deassert_reset,
5151 };
5152 
5153 /*** 0.13-specific implementations of various RISC-V helper functions. ***/
5155  riscv_reg_t *value, enum gdb_regno rid)
5156 {
5157  /* It would be beneficial to move this redirection to the
5158  * version-independent section, but there is a conflict:
5159  * `dcsr[5]` is `dcsr.v` in current spec, but it is `dcsr.debugint` in 0.11.
5160  */
5161  if (rid == GDB_REGNO_PRIV) {
5162  uint64_t dcsr;
5163  if (riscv_reg_get(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
5164  return ERROR_FAIL;
5165  *value = set_field(0, VIRT_PRIV_V, get_field(dcsr, CSR_DCSR_V));
5166  *value = set_field(*value, VIRT_PRIV_PRV, get_field(dcsr, CSR_DCSR_PRV));
5167  return ERROR_OK;
5168  }
5169 
5170  LOG_TARGET_DEBUG(target, "reading register %s", riscv_reg_gdb_regno_name(target, rid));
5171 
5173  return ERROR_FAIL;
5174 
5175  if (register_read_direct(target, value, rid) != ERROR_OK) {
5176  *value = -1;
5177  return ERROR_FAIL;
5178  }
5179 
5180  return ERROR_OK;
5181 }
5182 
5184  riscv_reg_t value)
5185 {
5186  LOG_TARGET_DEBUG(target, "writing 0x%" PRIx64 " to register %s",
5188 
5190  return ERROR_FAIL;
5191 
5192  return register_write_direct(target, rid, value);
5193 }
5194 
5195 static int dm013_select_hart(struct target *target, int hart_index)
5196 {
5197  dm013_info_t *dm = get_dm(target);
5198  if (!dm)
5199  return ERROR_FAIL;
5200  if (hart_index == dm->current_hartid)
5201  return ERROR_OK;
5202 
5203  /* `hartsel` should not be changed if `abstractcs.busy` is set. */
5204  int result = wait_for_idle_if_needed(target);
5205  if (result != ERROR_OK)
5206  return result;
5207 
5208  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE;
5209  dmcontrol = set_dmcontrol_hartsel(dmcontrol, hart_index);
5210  if (dm_write(target, DM_DMCONTROL, dmcontrol) != ERROR_OK) {
5211  /* Who knows what the state is? */
5213  return ERROR_FAIL;
5214  }
5215  dm->current_hartid = hart_index;
5216  return ERROR_OK;
5217 }
5218 
5219 /* Select all harts that were prepped and that are selectable, clearing the
5220  * prepped flag on the harts that actually were selected. */
5222 {
5223  RISCV_INFO(r);
5224  dm013_info_t *dm = get_dm(target);
5225  if (!dm)
5226  return ERROR_FAIL;
5227  if (!dm->hasel_supported) {
5228  r->prepped = false;
5229  return dm013_select_target(target);
5230  }
5231 
5232  assert(dm->hart_count);
5233  unsigned int hawindow_count = (dm->hart_count + 31) / 32;
5234  uint32_t *hawindow = calloc(hawindow_count, sizeof(uint32_t));
5235  if (!hawindow)
5236  return ERROR_FAIL;
5237 
5238  struct target_list *entry;
5239  unsigned int total_selected = 0;
5240  unsigned int selected_index = 0;
5241  list_for_each_entry(entry, &dm->target_list, lh) {
5242  struct target *t = entry->target;
5243  struct riscv_info *info = riscv_info(t);
5244  riscv013_info_t *info_013 = get_info(t);
5245  unsigned int index = info_013->index;
5246  LOG_TARGET_DEBUG(target, "index=%d, prepped=%d", index, info->prepped);
5247  if (info->prepped) {
5248  info_013->selected = true;
5249  hawindow[index / 32] |= 1 << (index % 32);
5250  info->prepped = false;
5251  total_selected++;
5252  selected_index = index;
5253  }
5254  }
5255 
5256  if (total_selected == 0) {
5257  LOG_TARGET_ERROR(target, "No harts were prepped!");
5258  free(hawindow);
5259  return ERROR_FAIL;
5260  } else if (total_selected == 1) {
5261  /* Don't use hasel if we only need to talk to one hart. */
5262  free(hawindow);
5263  return dm013_select_hart(target, selected_index);
5264  }
5265 
5267  free(hawindow);
5268  return ERROR_FAIL;
5269  }
5270 
5271  for (unsigned int i = 0; i < hawindow_count; i++) {
5272  if (dm_write(target, DM_HAWINDOWSEL, i) != ERROR_OK) {
5273  free(hawindow);
5274  return ERROR_FAIL;
5275  }
5276  if (dm_write(target, DM_HAWINDOW, hawindow[i]) != ERROR_OK) {
5277  free(hawindow);
5278  return ERROR_FAIL;
5279  }
5280  }
5281 
5282  free(hawindow);
5283  return ERROR_OK;
5284 }
5285 
5286 static int riscv013_halt_prep(struct target *target)
5287 {
5288  return ERROR_OK;
5289 }
5290 
5291 static int riscv013_halt_go(struct target *target)
5292 {
5293  dm013_info_t *dm = get_dm(target);
5294  if (!dm)
5295  return ERROR_FAIL;
5296 
5298  return ERROR_FAIL;
5299 
5300  LOG_TARGET_DEBUG(target, "halting hart");
5301 
5302  /* `haltreq` should not be issued if `abstractcs.busy` is set. */
5303  int result = wait_for_idle_if_needed(target);
5304  if (result != ERROR_OK)
5305  return result;
5306 
5307  /* Issue the halt command, and then wait for the current hart to halt. */
5308  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_HALTREQ;
5309  dmcontrol = set_dmcontrol_hartsel(dmcontrol, dm->current_hartid);
5310  dm_write(target, DM_DMCONTROL, dmcontrol);
5311  uint32_t dmstatus;
5312  for (size_t i = 0; i < 256; ++i) {
5313  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5314  return ERROR_FAIL;
5315  /* When no harts are running, there's no point in continuing this loop. */
5316  if (!get_field(dmstatus, DM_DMSTATUS_ANYRUNNING))
5317  break;
5318  }
5319 
5320  /* We declare success if no harts are running. One or more of them may be
5321  * unavailable, though. */
5322 
5323  if ((get_field(dmstatus, DM_DMSTATUS_ANYRUNNING))) {
5324  if (dm_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
5325  return ERROR_FAIL;
5326 
5327  LOG_TARGET_ERROR(target, "Unable to halt. dmcontrol=0x%08x, dmstatus=0x%08x",
5328  dmcontrol, dmstatus);
5329  return ERROR_FAIL;
5330  }
5331 
5332  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_HALTREQ, 0);
5333  dm_write(target, DM_DMCONTROL, dmcontrol);
5334 
5335  if (dm->current_hartid == HART_INDEX_MULTIPLE) {
5336  struct target_list *entry;
5337  list_for_each_entry(entry, &dm->target_list, lh) {
5338  struct target *t = entry->target;
5339  uint32_t t_dmstatus;
5340  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED) ||
5341  get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5342  /* All harts are either halted or unavailable. No
5343  * need to read dmstatus for each hart. */
5344  t_dmstatus = dmstatus;
5345  } else {
5346  /* Only some harts were halted/unavailable. Read
5347  * dmstatus for this one to see what its status
5348  * is. */
5350  return ERROR_FAIL;
5351  if (dm_read(target, &t_dmstatus, DM_DMSTATUS) != ERROR_OK)
5352  return ERROR_FAIL;
5353  }
5354  /* Set state for the current target based on its dmstatus. */
5355  if (get_field(t_dmstatus, DM_DMSTATUS_ALLHALTED)) {
5356  t->state = TARGET_HALTED;
5359  } else if (get_field(t_dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5361  }
5362  }
5363 
5364  } else {
5365  /* Set state for the current target based on its dmstatus. */
5366  if (get_field(dmstatus, DM_DMSTATUS_ALLHALTED)) {
5370  } else if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL)) {
5372  }
5373  }
5374 
5375  return ERROR_OK;
5376 }
5377 
5378 static int riscv013_resume_go(struct target *target)
5379 {
5381  return ERROR_FAIL;
5382 
5384 }
5385 
5387 {
5389 }
5390 
5392 {
5393  assert(target->state == TARGET_HALTED);
5394  return riscv013_on_step_or_resume(target, false);
5395 }
5396 
5397 static int riscv013_on_step(struct target *target)
5398 {
5399  return riscv013_on_step_or_resume(target, true);
5400 }
5401 
5403 {
5404  riscv_reg_t dcsr;
5405  int result = register_read_direct(target, &dcsr, GDB_REGNO_DCSR);
5406  if (result != ERROR_OK)
5407  return RISCV_HALT_UNKNOWN;
5408 
5409  LOG_TARGET_DEBUG(target, "dcsr.cause: 0x%" PRIx64, get_field(dcsr, CSR_DCSR_CAUSE));
5410 
5411  switch (get_field(dcsr, CSR_DCSR_CAUSE)) {
5412  case CSR_DCSR_CAUSE_EBREAK:
5413  return RISCV_HALT_EBREAK;
5415  /* We could get here before triggers are enumerated if a trigger was
5416  * already set when we connected. Force enumeration now, which has the
5417  * side effect of clearing any triggers we did not set. */
5419  LOG_TARGET_DEBUG(target, "halted because of trigger");
5420  return RISCV_HALT_TRIGGER;
5421  case CSR_DCSR_CAUSE_STEP:
5422  return RISCV_HALT_SINGLESTEP;
5425  return RISCV_HALT_INTERRUPT;
5426  case CSR_DCSR_CAUSE_GROUP:
5427  return RISCV_HALT_GROUP;
5428  case CSR_DCSR_CAUSE_OTHER:
5429  switch (get_field(dcsr, CSR_DCSR_EXTCAUSE)) {
5430  case 0:
5431  LOG_TARGET_INFO(target, "halted because of hart in a critical error state");
5433  default:
5434  LOG_TARGET_ERROR(target, "Unknown DCSR extcause field: 0x%"
5435  PRIx64, get_field(dcsr, CSR_DCSR_EXTCAUSE));
5436  return RISCV_HALT_UNKNOWN;
5437  }
5438  }
5439 
5440  LOG_TARGET_ERROR(target, "Unknown DCSR cause field: 0x%" PRIx64, get_field(dcsr, CSR_DCSR_CAUSE));
5441  LOG_TARGET_ERROR(target, " dcsr=0x%" PRIx32, (uint32_t)dcsr);
5442  return RISCV_HALT_UNKNOWN;
5443 }
5444 
5445 static int riscv013_write_progbuf(struct target *target, unsigned int index, riscv_insn_t data)
5446 {
5447  assert(index < RISCV013_MAX_PROGBUF_SIZE);
5448 
5449  dm013_info_t *dm = get_dm(target);
5450  if (!dm)
5451  return ERROR_FAIL;
5452 
5453  if (dm->progbuf_cache[index] != data) {
5454  if (dm_write(target, DM_PROGBUF0 + index, data) != ERROR_OK)
5455  return ERROR_FAIL;
5456  dm->progbuf_cache[index] = data;
5457  } else {
5458  LOG_TARGET_DEBUG(target, "Cache hit for 0x%" PRIx32 " @%d", data, index);
5459  }
5460  return ERROR_OK;
5461 }
5462 
5463 static riscv_insn_t riscv013_read_progbuf(struct target *target, unsigned int index)
5464 {
5465  uint32_t value;
5466  if (dm_read(target, &value, DM_PROGBUF0 + index) == ERROR_OK)
5467  return value;
5468  else
5469  return 0;
5470 }
5471 
5473 {
5474  dm013_info_t *dm = get_dm(target);
5475  if (!dm) {
5476  LOG_TARGET_DEBUG(target, "No DM is specified for the target");
5477  return ERROR_FAIL;
5478  }
5479 
5480  LOG_TARGET_DEBUG(target, "Invalidating progbuf cache");
5481  memset(dm->progbuf_cache, 0, sizeof(dm->progbuf_cache));
5482  return ERROR_OK;
5483 }
5484 
5485 static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr)
5486 {
5488  return ERROR_FAIL;
5489  uint32_t run_program = 0;
5490  run_program = set_field(run_program, AC_ACCESS_REGISTER_AARSIZE, 2);
5491  run_program = set_field(run_program, AC_ACCESS_REGISTER_POSTEXEC, 1);
5492  run_program = set_field(run_program, AC_ACCESS_REGISTER_TRANSFER, 0);
5493  run_program = set_field(run_program, AC_ACCESS_REGISTER_REGNO, 0x1000);
5494 
5495  return riscv013_execute_abstract_command(target, run_program, cmderr);
5496 }
5497 
5498 static void riscv013_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
5499 {
5503  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
5504 }
5505 
5506 static void riscv013_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
5507 {
5511  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
5512 }
5513 
5514 static void riscv013_fill_dm_nop(const struct target *target, uint8_t *buf)
5515 {
5519  buf_set_u32(buf, DTM_DMI_ADDRESS_OFFSET, info->abits, 0);
5520 }
5521 
5522 static unsigned int riscv013_get_dmi_address_bits(const struct target *target)
5523 {
5525  return info->abits;
5526 }
5527 
5528 /* Helper Functions. */
5530 {
5533  return ERROR_FAIL;
5534 
5536  return ERROR_FAIL;
5537 
5539  return ERROR_FAIL;
5540  return ERROR_OK;
5541 }
5542 
5544  bool step)
5545 {
5546  if (target->state != TARGET_HALTED) {
5547  LOG_TARGET_ERROR(target, "Hart is not halted!");
5548  return ERROR_TARGET_NOT_HALTED;
5549  }
5550 
5551  LOG_TARGET_DEBUG(target, "resuming (operation=%s)",
5552  step ? "single-step" : "resume");
5553 
5555  return ERROR_FAIL;
5556 
5558 
5559  dm013_info_t *dm = get_dm(target);
5560  /* Issue the resume command, and then wait for the current hart to resume. */
5561  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_RESUMEREQ;
5562  dmcontrol = set_dmcontrol_hartsel(dmcontrol, dm->current_hartid);
5563  /* `resumereq` should not be issued if `abstractcs.busy` is set. */
5564  int result = wait_for_idle_if_needed(target);
5565  if (result != ERROR_OK)
5566  return result;
5567  dm_write(target, DM_DMCONTROL, dmcontrol);
5568 
5569  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_RESUMEREQ, 0);
5570 
5571  uint32_t dmstatus;
5572  for (size_t i = 0; i < 256; ++i) {
5573  usleep(10);
5574  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5575  return ERROR_FAIL;
5576  if (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL))
5577  return ERROR_FAIL;
5578  if (get_field(dmstatus, DM_DMSTATUS_ALLRESUMEACK) == 0)
5579  continue;
5580  if (step && get_field(dmstatus, DM_DMSTATUS_ALLHALTED) == 0)
5581  continue;
5582 
5583  dm_write(target, DM_DMCONTROL, dmcontrol);
5584  return ERROR_OK;
5585  }
5586 
5587  LOG_TARGET_ERROR(target, "Failed to %s. dmstatus=0x%08x",
5588  step ? "single-step" : "resume", dmstatus);
5589 
5590  dm_write(target, DM_DMCONTROL, dmcontrol);
5592  " cancelling the resume request (dmcontrol.resumereq <- 0)");
5593 
5594  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
5595  return ERROR_FAIL;
5596 
5597  LOG_TARGET_ERROR(target, " dmstatus after cancellation=0x%08x", dmstatus);
5598 
5599  if (step) {
5601  " trying to recover from a failed single-step, by requesting halt");
5602  if (riscv_halt(target) == ERROR_OK)
5603  LOG_TARGET_ERROR(target, " halt completed after failed single-step");
5604  else
5605  LOG_TARGET_ERROR(target, " could not halt, something is wrong with the taget");
5606  // TODO: returning ERROR_OK is questionable, this code needs to be revised
5607  return ERROR_OK;
5608  }
5609 
5610  return ERROR_FAIL;
5611 }
5612 
5614 {
5615  uint32_t abstractcs;
5616  int result = wait_for_idle(target, &abstractcs);
5617  /* Clear the error status, even if busy is still set. */
5619  result = ERROR_FAIL;
5620  return result;
5621 }
#define IS_PWR_OF_2(x)
Definition: align.h:24
#define ALIGN_UP(x, a)
Definition: align.h:20
const char * group
Definition: armv4_5.c:366
bool riscv_batch_was_batch_busy(const struct riscv_batch *batch)
Definition: batch.c:438
uint32_t riscv_batch_get_dmi_read_op(const struct riscv_batch *batch, size_t key)
Definition: batch.c:389
struct riscv_batch * riscv_batch_alloc(struct target *target, size_t scans)
Definition: batch.c:31
void riscv_batch_add_nop(struct riscv_batch *batch)
Definition: batch.c:409
void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint32_t address, uint32_t data, bool read_back, enum riscv_scan_delay_class delay_class)
Definition: batch.c:331
size_t riscv_batch_available_scans(struct riscv_batch *batch)
Definition: batch.c:432
uint32_t riscv_batch_get_dmi_read_data(const struct riscv_batch *batch, size_t key)
Definition: batch.c:399
size_t riscv_batch_finished_scans(const struct riscv_batch *batch)
Definition: batch.c:446
void riscv_batch_free(struct riscv_batch *batch)
Definition: batch.c:96
size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, uint32_t address, enum riscv_scan_delay_class delay_class)
Definition: batch.c:361
int riscv_batch_run_from(struct riscv_batch *batch, size_t start_idx, const struct riscv_scan_delays *delays, bool resets_delays, size_t reset_delays_after)
Definition: batch.c:278
static int riscv_scan_increase_delay(struct riscv_scan_delays *delays, enum riscv_scan_delay_class delay_class)
Definition: batch.h:99
riscv_scan_delay_class
Definition: batch.h:20
@ RISCV_DELAY_ABSTRACT_COMMAND
Definition: batch.h:24
@ RISCV_DELAY_SYSBUS_READ
Definition: batch.h:26
@ RISCV_DELAY_BASE
Definition: batch.h:22
@ RISCV_DELAY_SYSBUS_WRITE
Definition: batch.h:28
static size_t riscv_batch_add_dm_read(struct riscv_batch *batch, uint32_t address, enum riscv_scan_delay_class delay_type)
Definition: batch.h:202
static void riscv_scan_set_delay(struct riscv_scan_delays *delays, enum riscv_scan_delay_class delay_class, unsigned int delay)
Definition: batch.h:77
static unsigned int riscv_scan_get_delay(const struct riscv_scan_delays *delays, enum riscv_scan_delay_class delay_class)
Definition: batch.h:60
static void riscv_batch_add_dm_write(struct riscv_batch *batch, uint32_t address, uint32_t data, bool read_back, enum riscv_scan_delay_class delay_type)
Definition: batch.h:187
static const char * riscv_scan_delay_class_name(enum riscv_scan_delay_class delay_class)
Definition: batch.h:32
bool buf_eq(const void *_buf1, const void *_buf2, unsigned int size)
Definition: binarybuffer.c:70
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
static uint64_t buf_get_u64(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 64-bit word.
Definition: binarybuffer.h:134
static void buf_set_u64(uint8_t *_buffer, unsigned int first, unsigned int num, uint64_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:65
#define GENMASK_ULL(h, l)
Definition: bits.h:25
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define DM_ABSTRACTAUTO_AUTOEXECDATA_OFFSET
#define AC_ACCESS_REGISTER_TRANSFER
#define DM_DATA0
#define CSR_DCSR_EBREAKM
#define AC_ACCESS_REGISTER_POSTEXEC
#define CSR_DCSR_MPRVEN_ENABLED
#define DM_AUTHDATA
#define DM_DMCONTROL_ACKHAVERESET
#define DM_DMSTATUS_ANYHAVERESET
#define CSR_DCSR_CAUSE_GROUP
#define DM_DMSTATUS_ALLHALTED
#define DM_SBCS_SBACCESS64
#define DM_SBCS_SBVERSION
#define DM_DMCONTROL_RESUMEREQ
#define DM_SBDATA3
#define CSR_DCSR_CAUSE_OTHER
#define DM_DMCS2_HGWRITE
#define DM_ABSTRACTCS
#define DM_ABSTRACTCS_BUSY
#define DM_DMSTATUS_ALLRESUMEACK
#define DM_DMCONTROL_HARTSELLO_LENGTH
#define DM_DMCONTROL
#define DM_SBCS_SBACCESS
#define DM_NEXTDM
#define DM_SBDATA2
#define DM_SBCS
#define DM_SBCS_SBBUSY
#define DM_SBCS_SBBUSYERROR
#define DM_DMCONTROL_HASEL_SINGLE
#define DTM_DTMCS_IDLE
#define DM_ABSTRACTCS_CMDERR
#define DM_HARTINFO_DATASIZE
#define AC_ACCESS_REGISTER_REGNO
#define CSR_DCSR_EBREAKVU
#define DM_ABSTRACTCS_PROGBUFSIZE
#define DM_SBDATA0
#define DM_DMCONTROL_HASEL_MULTIPLE
#define DM_PROGBUF1
#define CSR_DCSR_CAUSE_STEP
#define DM_DMSTATUS_ALLUNAVAIL
#define DM_ABSTRACTCS_DATACOUNT
#define DTM_DMI_DATA_OFFSET
#define DM_DATA1
#define DM_HAWINDOWSEL
#define DM_HARTINFO_DATAADDR_LENGTH
#define DM_DMSTATUS_AUTHENTICATED
#define DM_SBCS_SBAUTOINCREMENT
#define DM_SBADDRESS1
#define DTM_DMI_OP_WRITE
#define DM_SBCS_SBERROR_NONE
#define DM_SBCS_SBASIZE
#define VIRT_PRIV_PRV
#define DM_SBDATA1
#define AC_ACCESS_MEMORY_WRITE
#define DM_DMCONTROL_HARTSELLO
#define DM_DMCONTROL_NDMRESET
#define AC_ACCESS_REGISTER_WRITE
#define DM_DMSTATUS_ALLRUNNING
#define DM_SBADDRESS3
#define DTM_DMI_OP_OFFSET
#define CSR_DCSR_CAUSE_HALTREQ
#define AC_ACCESS_MEMORY_CMDTYPE
#define CSR_DCSR_EXTCAUSE
#define DM_HARTINFO_DATAACCESS
#define DTM_DTMCS_VERSION
#define CSR_DCSR_EBREAKS
#define DTM_DMI_OP_FAILED
#define DM_DMSTATUS_IMPEBREAK
#define DTM_DTMCS_ABITS
#define DM_DMSTATUS_ANYNONEXISTENT
#define DM_DMCONTROL_DMACTIVE
#define CSR_DCSR_EBREAKVS
#define DM_DMCONTROL_HASEL
riscv_debug_reg_ordinal
@ AC_ACCESS_MEMORY_ORDINAL
@ AC_QUICK_ACCESS_ORDINAL
@ AC_ACCESS_REGISTER_ORDINAL
#define CSR_DCSR_V
#define DTM_DMI_ADDRESS_OFFSET
#define DM_SBCS_SBACCESS8
#define DTM_DTMCS_DMIRESET
Definition: debug_defines.h:84
#define DM_DMSTATUS
#define CSR_DCSR_MPRVEN
#define DM_DMCONTROL_HARTSELHI_LENGTH
#define CSR_DCSR_STEP
#define CSR_DCSR_EBREAKU
#define DM_DMCS2
#define AC_ACCESS_REGISTER_AARSIZE
#define CSR_DCSR_CETRIG
#define CSR_DCSR_CAUSE_EBREAK
#define DM_COMMAND
#define DM_SBCS_SBERROR
#define VIRT_PRIV_V
#define DM_DMSTATUS_VERSION
#define DM_DMSTATUS_AUTHBUSY
#define 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:263
struct jtag_tap * jtag_tap_next_enabled(struct jtag_tap *p)
Definition: jtag/core.c:252
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, enum tap_state state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:361
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_IDLE
Definition: jtag.h:53
static void list_add(struct list_head *new, struct list_head *head)
Definition: list.h:197
static int list_empty(const struct list_head *head)
Definition: list.h:61
#define list_for_each_entry(p, h, field)
Definition: list.h:155
static void list_del(struct list_head *entry)
Definition: list.h:88
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:54
void log_printf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format,...)
Definition: log.c:201
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:167
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:173
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:164
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define ERROR_TIMEOUT_REACHED
Definition: log.h:191
#define LOG_LEVEL_IS(FOO)
Definition: log.h:112
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_DEBUG
Definition: log.h:55
@ LOG_LVL_WARNING
Definition: log.h:53
static uint32_t fmv_d_x(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:327
static uint32_t lh(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:172
static uint32_t csrr(unsigned int rd, unsigned int csr) __attribute__((unused))
Definition: opcodes.h:211
#define S0
Definition: opcodes.h:13
static uint32_t vsetvl(unsigned int rd, unsigned int rs1, unsigned int rs2) __attribute__((unused))
Definition: opcodes.h:410
#define S1
Definition: opcodes.h:14
static uint32_t vmv_x_s(unsigned int rd, unsigned int vs2) __attribute__((unused))
Definition: opcodes.h:420
static uint32_t fsd(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:270
static uint32_t fmv_x_w(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:300
static uint32_t fmv_w_x(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:318
static uint32_t vslide1down_vx(unsigned int vd, unsigned int vs2, unsigned int rs1, bool vm) __attribute__((unused))
Definition: opcodes.h:439
#define ZERO
Definition: opcodes.h:11
static uint32_t auipc(unsigned int dest) __attribute__((unused))
Definition: opcodes.h:392
static uint32_t sw(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:112
static uint32_t fmv_x_d(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:309
static uint32_t fld(unsigned int dest, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:290
int riscv_program_fence_i(struct riscv_program *p)
Definition: program.c:171
int riscv_program_write(struct riscv_program *program)
Definition: program.c:30
int riscv_program_fence_rw_rw(struct riscv_program *p)
Definition: program.c:176
int riscv_program_store(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int16_t offset, unsigned int size)
Definition: program.c:93
int riscv_program_addi(struct riscv_program *p, enum gdb_regno d, enum gdb_regno s, int16_t u)
Definition: program.c:192
int riscv_program_insert(struct riscv_program *p, riscv_insn_t i)
Definition: program.c:197
int riscv_program_load(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int16_t offset, unsigned int size)
Definition: program.c:130
int riscv_program_csrr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno csr)
Definition: program.c:159
int riscv_program_init(struct riscv_program *p, struct target *target)
Definition: program.c:17
int riscv_program_csrw(struct riscv_program *p, enum gdb_regno s, enum gdb_regno csr)
Definition: program.c:165
int riscv_program_ebreak(struct riscv_program *p)
Definition: program.c:181
int riscv_program_exec(struct riscv_program *p, struct target *t)
Add ebreak and execute the program.
Definition: program.c:42
#define RISCV013_MAX_PROGBUF_SIZE
Definition: program.h:8
@ RISCV_PROGBUF_EXEC_RESULT_EXCEPTION
Definition: program.h:13
#define MIN(a, b)
Definition: replacements.h:22
#define MAX(a, b)
Definition: replacements.h:25
static int step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv-011.c:1466
static int riscv013_write_progbuf(struct target *target, unsigned int index, riscv_insn_t d)
Definition: riscv-013.c:5445
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:2541
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:4347
static int scratch_write64(struct target *target, scratch_mem_t *scratch, uint64_t value)
Definition: riscv-013.c:1320
static int examine_dm(struct target *target)
Definition: riscv-013.c:1933
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:2409
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:4727
static int halt_set_dcsr_config(struct target *target)
Definition: riscv-013.c:1754
static struct mem_access_result mem_access_result(enum mem_access_result_enum value)
Definition: riscv-013.c:3721
static int csr_write_progbuf(struct target *target, enum gdb_regno number, riscv_reg_t value)
Definition: riscv-013.c:1599
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:4413
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:4203
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:2372
static int riscv013_step_current_hart(struct target *target)
Definition: riscv-013.c:5386
static void riscv013_fill_dmi_read(const struct target *target, uint8_t *buf, uint32_t a)
Definition: riscv-013.c:5506
static int riscv013_step_or_resume_current_hart(struct target *target, bool step)
Definition: riscv-013.c:5543
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:4873
static uint32_t sb_sbaccess(unsigned int size_bytes)
Definition: riscv-013.c:2516
int riscv013_set_register_buf(struct target *target, enum gdb_regno regno, const uint8_t *value)
Definition: riscv-013.c:2477
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:4526
static int read_memory_bus_word(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: riscv-013.c:3204
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:1361
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:5529
static int vl_write_progbuf(struct target *target, riscv_reg_t value)
Definition: riscv-013.c:1578
static int riscv013_on_step(struct target *target)
Definition: riscv-013.c:5397
static struct mem_access_result write_memory_progbuf_inner(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:5077
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:3839
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:4959
bool is_mem_access_failed(struct mem_access_result status)
Definition: riscv-013.c:3677
static int fpr_read_progbuf(struct target *target, uint64_t *value, enum gdb_regno number)
Definition: riscv-013.c:1407
static int select_prepped_harts(struct target *target)
Definition: riscv-013.c:5221
#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:4594
static bool has_sufficient_progbuf(struct target *target, unsigned int size)
Definition: riscv-013.c:1371
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:1524
static int csr_read_progbuf(struct target *target, uint64_t *value, enum gdb_regno number)
Definition: riscv-013.c:1439
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:4144
static struct mem_access_result mem_should_skip_abstract(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3804
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:1674
#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:5514
struct target_type riscv013_target
Definition: riscv-013.c:5138
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:5195
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:1462
static int sb_write_address(struct target *target, target_addr_t address, enum riscv_scan_delay_class sbaddr0_delay)
Definition: riscv-013.c:2561
static int examine(struct target *target)
Definition: riscv-013.c:2022
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:3301
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:1874
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:3016
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:3727
int riscv013_set_register(struct target *target, enum gdb_regno rid, riscv_reg_t value)
Definition: riscv-013.c:5183
bool is_mem_access_ok(struct mem_access_result status)
Definition: riscv-013.c:3661
static int riscv013_halt_go(struct target *target)
Definition: riscv-013.c:5291
static int vtype_write_progbuf(struct target *target, riscv_reg_t value)
Definition: riscv-013.c:1557
static OOCD_LIST_HEAD(dm_list)
static int assert_reset(struct target *target)
Definition: riscv-013.c:2965
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:4990
static int batch_run(struct target *target, struct riscv_batch *batch)
Definition: riscv-013.c:2572
static int riscv013_execute_progbuf(struct target *target, uint32_t *cmderr)
Definition: riscv-013.c:5485
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:4927
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:1622
mem_access_result_type
Definition: riscv-013.c:3582
@ MEM_ACCESS_RESULT_TYPE_OK
Definition: riscv-013.c:3583
@ MEM_ACCESS_RESULT_TYPE_ENUM_SIZE
Definition: riscv-013.c:3587
@ MEM_ACCESS_RESULT_TYPE_SKIPPED
Definition: riscv-013.c:3585
@ MEM_ACCESS_RESULT_TYPE_FAILED
Definition: riscv-013.c:3586
@ MEM_ACCESS_RESULT_TYPE_DISABLED
Definition: riscv-013.c:3584
static int riscv013_invalidate_cached_progbuf(struct target *target)
Definition: riscv-013.c:5472
static int handle_became_unavailable(struct target *target, enum riscv_hart_state previous_riscv_state)
Definition: riscv-013.c:2887
static int read_memory_progbuf_inner_fill_progbuf(struct target *target, uint32_t increment, uint32_t size)
Definition: riscv-013.c:4372
static int read_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3320
mem_access_result_enum
Definition: riscv-013.c:3650
static int set_dcsr_config(struct target *target, bool step)
Definition: riscv-013.c:1725
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:4507
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:5154
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:4471
static void riscv013_fill_dmi_write(const struct target *target, uint8_t *buf, uint32_t a, uint32_t d)
Definition: riscv-013.c:5498
static enum riscv_halt_reason riscv013_halt_reason(struct target *target)
Definition: riscv-013.c:5402
bool is_mem_access_skipped(struct mem_access_result status)
Definition: riscv-013.c:3693
static unsigned int get_sbaadress_reg_count(const struct target *target)
Definition: riscv-013.c:2534
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:2596
static int riscv013_access_memory(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4613
static riscv_insn_t riscv013_read_progbuf(struct target *target, unsigned int index)
Definition: riscv-013.c:5463
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:1854
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:3410
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:3995
static int sba_supports_access(struct target *target, unsigned int size_bytes)
Definition: riscv-013.c:2645
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:2912
static struct mem_access_result access_memory_sysbus(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4566
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:3007
static unsigned int riscv013_data_bits(struct target *target)
Definition: riscv-013.c:2274
static int riscv013_resume_prep(struct target *target)
Definition: riscv-013.c:5391
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:1279
const char * mem_access_result_to_str(struct mem_access_result status)
Definition: riscv-013.c:3708
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:1501
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:4297
static struct mem_access_result write_memory_abstract(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3916
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:3087
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:4359
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:2239
#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:2226
static int riscv013_get_hart_state(struct target *target, enum riscv_hart_state *state)
Definition: riscv-013.c:2836
static void set_buffer_and_log_read(const struct riscv_mem_access_args args, uint32_t index, uint64_t value)
Definition: riscv-013.c:4331
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:1645
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:1383
static int riscv013_halt_prep(struct target *target)
Definition: riscv-013.c:5286
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:5613
static int write_memory_progbuf_fill_progbuf(struct target *target, uint32_t size)
Definition: riscv-013.c:5054
static target_addr_t sb_read_address(struct target *target)
Definition: riscv-013.c:3221
int riscv013_get_register_buf(struct target *target, uint8_t *value, enum gdb_regno regno)
Definition: riscv-013.c:2422
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:3163
#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:3255
static int write_memory_bus_v0(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:4673
static unsigned int riscv013_get_dmi_address_bits(const struct target *target)
Definition: riscv-013.c:5522
static int riscv013_resume_go(struct target *target)
Definition: riscv-013.c:5378
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:4917
static void log_memory_access(target_addr_t address, uint32_t *sbvalue, unsigned int size_bytes, bool is_read)
Definition: riscv-013.c:3189
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:4274
#define LIST_OF_MEM_ACCESS_RESULTS
Definition: riscv-013.c:3590
#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:2664
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:5113
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:4124
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:1819
static int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
Definition: riscv-013.c:1702
static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
Definition: riscv-013.c:3237
static int tick(struct target *target)
Definition: riscv-013.c:2902
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:4065
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:3151
static unsigned int riscv013_get_progbufsize(const struct target *target)
Definition: riscv-013.c:5131
static COMMAND_HELPER(riscv013_print_info, struct target *target)
Definition: riscv-013.c:2309
static int try_set_vsew(struct target *target, unsigned int *debug_vsew)
Definition: riscv-013.c:2343
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:4251
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:5125
static struct mem_access_result mem_should_skip_sysbus(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-013.c:3771
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:2825
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:1273
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:3556
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:5037
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
#define BIT(nr)
Definition: stm32l4x.h:18
size_t size
Definition: riscv-013.c:144
uint32_t * commands
Definition: riscv-013.c:143
int hart_count
Definition: riscv-013.c:118
struct list_head list
Definition: riscv-013.c:113
struct list_head target_list
Definition: riscv-013.c:124
uint32_t base
Definition: riscv-013.c:116
uint32_t progbuf_cache[16]
Definition: riscv-013.c:133
bool was_examined
Definition: riscv-013.c:120
int current_hartid
Definition: riscv-013.c:127
bool abstract_cmd_maybe_busy
Definition: riscv-013.c:139
bool hasel_supported
Definition: riscv-013.c:129
unsigned int abs_chain_position
Definition: riscv-013.c:114
bool was_reset
Definition: riscv-013.c:122
Definition: jtag.h:101
uint8_t * cur_instr
current instruction
Definition: jtag.h:132
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
unsigned int abs_chain_position
Definition: jtag.h:105
bool enabled
Is this TAP currently enabled?
Definition: jtag.h:109
Definition: list.h:41
enum mem_access_result_enum value
Definition: riscv-013.c:3658
struct reg * reg_list
Definition: register.h:147
Definition: register.h:111
uint32_t size
Definition: register.h:132
void * arch_info
Definition: register.h:140
unsigned int datacount
Definition: riscv-013.c:208
int16_t dataaddr
Definition: riscv-013.c:237
bool haltgroup_supported
Definition: riscv-013.c:253
unsigned int hartsellen
Definition: riscv-013.c:240
unsigned int index
Definition: riscv-013.c:204
bool dcsr_register_is_set
Definition: riscv-013.c:250
struct ac_cache ac_not_supported_cache
Definition: riscv-013.c:232
unsigned int abits
Definition: riscv-013.c:206
unsigned int progbufsize
Definition: riscv-013.c:210
uint8_t dataaccess
Definition: riscv-013.c:236
dm013_info_t * dm
Definition: riscv-013.c:243
riscv_addr_t progbuf_address
Definition: riscv-013.c:219
uint8_t datasize
Definition: riscv-013.c:235
size_t read_keys_used
Definition: batch.h:141
size_t used_scans
Definition: batch.h:121
struct riscv_debug_reg_ctx::@125 XLEN
uint32_t increment
Definition: riscv.h: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:227
struct target * target
Definition: target.h:228
This holds methods shared between all instances of a given target type.
Definition: target_type.h:27
const char * name
Name of this type of target.
Definition: target_type.h:32
Definition: target.h:119
int32_t coreid
Definition: target.h:123
struct jtag_tap * tap
Definition: target.h:122
bool dbgbase_set
Definition: target.h:184
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
uint32_t dbgbase
Definition: target.h:185
struct reg_cache * reg_cache
Definition: target.h:168
bool smp
Definition: target.h:200
unsigned int smp_id
Definition: target.h:201
void * arch_info
Definition: target.h:174
bool reset_halt
Definition: target.h:154
target_addr_t address
Definition: target.h:89
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2118
int target_examine_one(struct target *target)
Examine the specified target, letting it perform any Initialisation that requires JTAG access.
Definition: target.c:686
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2174
bool target_has_event_action(const struct target *target, enum target_event event)
Returns true only if the target has a handler for the specified event.
Definition: target.c:4885
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4699
@ DBG_REASON_UNDEFINED
Definition: target.h:80
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:818
static bool target_was_examined(const struct target *target)
Definition: target.h:444
@ TARGET_EVENT_RESET_ASSERT
Definition: target.h:278
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:247
@ TARGET_RESET
Definition: target.h:59
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_UNAVAILABLE
Definition: target.h:61
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
int64_t timeval_ms(void)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
uint64_t target_addr_t
Definition: types.h:279
#define TARGET_PRIxADDR
Definition: types.h:284
static struct ublast_lowlevel_priv info
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t rid[2]
Definition: vdebug.c:15
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22