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