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