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