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