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 <stdlib.h>
10 #include <time.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/log.h>
20 #include "jtag/jtag.h"
21 #include "target/register.h"
22 #include "target/breakpoints.h"
23 #include "helper/time_support.h"
24 #include "helper/list.h"
25 #include "riscv.h"
26 #include "debug_defines.h"
27 #include "rtos/rtos.h"
28 #include "program.h"
29 #include "asm.h"
30 #include "batch.h"
31 
32 static int riscv013_on_step_or_resume(struct target *target, bool step);
34  bool step, bool use_hasel);
35 static void riscv013_clear_abstract_error(struct target *target);
36 
37 /* Implementations of the functions in struct riscv_info. */
38 static int riscv013_get_register(struct target *target,
39  riscv_reg_t *value, int rid);
40 static int riscv013_set_register(struct target *target, int regid, uint64_t value);
41 static int riscv013_select_current_hart(struct target *target);
42 static int riscv013_halt_prep(struct target *target);
43 static int riscv013_halt_go(struct target *target);
44 static int riscv013_resume_go(struct target *target);
45 static int riscv013_step_current_hart(struct target *target);
46 static int riscv013_on_halt(struct target *target);
47 static int riscv013_on_step(struct target *target);
48 static int riscv013_resume_prep(struct target *target);
49 static bool riscv013_is_halted(struct target *target);
51 static int riscv013_write_debug_buffer(struct target *target, unsigned int index,
52  riscv_insn_t d);
53 static riscv_insn_t riscv013_read_debug_buffer(struct target *target, unsigned int index);
54 static int riscv013_execute_debug_buffer(struct target *target);
55 static void riscv013_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d);
56 static void riscv013_fill_dmi_read_u64(struct target *target, char *buf, int a);
57 static int riscv013_dmi_write_u64_bits(struct target *target);
58 static void riscv013_fill_dmi_nop_u64(struct target *target, char *buf);
59 static int register_read(struct target *target, uint64_t *value, uint32_t number);
60 static int register_read_direct(struct target *target, uint64_t *value, uint32_t number);
61 static int register_write_direct(struct target *target, unsigned int number,
62  uint64_t value);
63 static int read_memory(struct target *target, target_addr_t address,
64  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment);
65 static int write_memory(struct target *target, target_addr_t address,
66  uint32_t size, uint32_t count, const uint8_t *buffer);
67 
75 #define get_field(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
76 #define set_field(reg, mask, val) (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
77 
78 #define CSR_DCSR_CAUSE_SWBP 1
79 #define CSR_DCSR_CAUSE_TRIGGER 2
80 #define CSR_DCSR_CAUSE_DEBUGINT 3
81 #define CSR_DCSR_CAUSE_STEP 4
82 #define CSR_DCSR_CAUSE_HALT 5
83 #define CSR_DCSR_CAUSE_GROUP 6
84 
85 #define RISCV013_INFO(r) riscv013_info_t *r = get_info(target)
86 
87 /*** JTAG registers. ***/
88 
89 typedef enum {
92  DMI_OP_WRITE = 2
94 typedef enum {
97  DMI_STATUS_BUSY = 3
99 
100 typedef enum slot {
105 
106 /*** Debug Bus registers. ***/
107 
108 #define CMDERR_NONE 0
109 #define CMDERR_BUSY 1
110 #define CMDERR_NOT_SUPPORTED 2
111 #define CMDERR_EXCEPTION 3
112 #define CMDERR_HALT_RESUME 4
113 #define CMDERR_OTHER 7
114 
115 /*** Info about the core being debugged. ***/
116 
117 struct trigger {
118  uint64_t address;
119  uint32_t length;
120  uint64_t mask;
121  uint64_t value;
122  bool read, write, execute;
124 };
125 
126 typedef enum {
129  YNM_NO
131 
132 typedef struct {
133  struct list_head list;
134  unsigned int abs_chain_position;
135 
136  /* The number of harts connected to this DM. */
138  /* Indicates we already reset this DM, so don't need to do it again. */
139  bool was_reset;
140  /* Targets that are connected to this DM. */
141  struct list_head target_list;
142  /* The currently selected hartid on this DM. */
145 
146  /* The program buffer stores executable code. 0 is an illegal instruction,
147  * so we use 0 to mean the cached value is invalid. */
148  uint32_t progbuf_cache[16];
149 } dm013_info_t;
150 
151 typedef struct {
152  struct list_head list;
153  struct target *target;
154 } target_list_t;
155 
156 typedef struct {
157  /* The indexed used to address this hart in its DM. */
158  unsigned int index;
159  /* Number of address bits in the dbus register. */
160  unsigned int abits;
161  /* Number of abstract command data registers. */
162  unsigned int datacount;
163  /* Number of words in the Program Buffer. */
164  unsigned int progbufsize;
165 
166  /* We cache the read-only bits of sbcs here. */
167  uint32_t sbcs;
168 
170  /* We only need the address so that we know the alignment of the buffer. */
172 
173  /* Number of run-test/idle cycles the target requests we do after each dbus
174  * access. */
175  unsigned int dtmcs_idle;
176 
177  /* This value is incremented every time a dbus access comes back as "busy".
178  * It's used to determine how many run-test/idle cycles to feed the target
179  * in between accesses. */
180  unsigned int dmi_busy_delay;
181 
182  /* Number of run-test/idle cycles to add between consecutive bus master
183  * reads/writes respectively. */
184  unsigned int bus_master_write_delay, bus_master_read_delay;
185 
186  /* This value is increased every time we tried to execute two commands
187  * consecutively, and the second one failed because the previous hadn't
188  * completed yet. It's used to add extra run-test/idle cycles after
189  * starting a command, so we don't have to waste time checking for busy to
190  * go low. */
191  unsigned int ac_busy_delay;
192 
197 
199 
200  /* When a function returns some error due to a failure indicated by the
201  * target in cmderr, the caller can look here to see what that error was.
202  * (Compare with errno.) */
203  uint8_t cmderr;
204 
205  /* Some fields from hartinfo. */
206  uint8_t datasize;
207  uint8_t dataaccess;
208  int16_t dataaddr;
209 
210  /* The width of the hartsel field. */
211  unsigned int hartsellen;
212 
213  /* DM that provides access to this target. */
216 
217 static LIST_HEAD(dm_list);
218 
219 static riscv013_info_t *get_info(const struct target *target)
220 {
221  struct riscv_info *info = target->arch_info;
222  assert(info);
223  assert(info->version_specific);
224  return info->version_specific;
225 }
226 
233 {
235  if (info->dm)
236  return info->dm;
237 
238  unsigned int abs_chain_position = target->tap->abs_chain_position;
239 
240  dm013_info_t *entry;
241  dm013_info_t *dm = NULL;
242  list_for_each_entry(entry, &dm_list, list) {
243  if (entry->abs_chain_position == abs_chain_position) {
244  dm = entry;
245  break;
246  }
247  }
248 
249  if (!dm) {
250  LOG_DEBUG("[%d] Allocating new DM", target->coreid);
251  dm = calloc(1, sizeof(dm013_info_t));
252  if (!dm)
253  return NULL;
254  dm->abs_chain_position = abs_chain_position;
255  dm->current_hartid = -1;
256  dm->hart_count = -1;
258  list_add(&dm->list, &dm_list);
259  }
260 
261  info->dm = dm;
262  target_list_t *target_entry;
263  list_for_each_entry(target_entry, &dm->target_list, list) {
264  if (target_entry->target == target)
265  return dm;
266  }
267  target_entry = calloc(1, sizeof(*target_entry));
268  if (!target_entry) {
269  info->dm = NULL;
270  return NULL;
271  }
272  target_entry->target = target;
273  list_add(&target_entry->list, &dm->target_list);
274 
275  return dm;
276 }
277 
278 static uint32_t set_hartsel(uint32_t initial, uint32_t index)
279 {
280  initial &= ~DM_DMCONTROL_HARTSELLO;
281  initial &= ~DM_DMCONTROL_HARTSELHI;
282 
283  uint32_t index_lo = index & ((1 << DM_DMCONTROL_HARTSELLO_LENGTH) - 1);
284  initial |= index_lo << DM_DMCONTROL_HARTSELLO_OFFSET;
285  uint32_t index_hi = index >> DM_DMCONTROL_HARTSELLO_LENGTH;
286  assert(index_hi < 1 << DM_DMCONTROL_HARTSELHI_LENGTH);
287  initial |= index_hi << DM_DMCONTROL_HARTSELHI_OFFSET;
288 
289  return initial;
290 }
291 
292 static void decode_dmi(char *text, unsigned int address, unsigned int data)
293 {
294  static const struct {
295  unsigned int address;
296  uint64_t mask;
297  const char *name;
298  } description[] = {
299  { DM_DMCONTROL, DM_DMCONTROL_HALTREQ, "haltreq" },
300  { DM_DMCONTROL, DM_DMCONTROL_RESUMEREQ, "resumereq" },
301  { DM_DMCONTROL, DM_DMCONTROL_HARTRESET, "hartreset" },
302  { DM_DMCONTROL, DM_DMCONTROL_HASEL, "hasel" },
303  { DM_DMCONTROL, DM_DMCONTROL_HARTSELHI, "hartselhi" },
304  { DM_DMCONTROL, DM_DMCONTROL_HARTSELLO, "hartsello" },
305  { DM_DMCONTROL, DM_DMCONTROL_NDMRESET, "ndmreset" },
306  { DM_DMCONTROL, DM_DMCONTROL_DMACTIVE, "dmactive" },
307  { DM_DMCONTROL, DM_DMCONTROL_ACKHAVERESET, "ackhavereset" },
308 
309  { DM_DMSTATUS, DM_DMSTATUS_IMPEBREAK, "impebreak" },
310  { DM_DMSTATUS, DM_DMSTATUS_ALLHAVERESET, "allhavereset" },
311  { DM_DMSTATUS, DM_DMSTATUS_ANYHAVERESET, "anyhavereset" },
312  { DM_DMSTATUS, DM_DMSTATUS_ALLRESUMEACK, "allresumeack" },
313  { DM_DMSTATUS, DM_DMSTATUS_ANYRESUMEACK, "anyresumeack" },
314  { DM_DMSTATUS, DM_DMSTATUS_ALLNONEXISTENT, "allnonexistent" },
315  { DM_DMSTATUS, DM_DMSTATUS_ANYNONEXISTENT, "anynonexistent" },
316  { DM_DMSTATUS, DM_DMSTATUS_ALLUNAVAIL, "allunavail" },
317  { DM_DMSTATUS, DM_DMSTATUS_ANYUNAVAIL, "anyunavail" },
318  { DM_DMSTATUS, DM_DMSTATUS_ALLRUNNING, "allrunning" },
319  { DM_DMSTATUS, DM_DMSTATUS_ANYRUNNING, "anyrunning" },
320  { DM_DMSTATUS, DM_DMSTATUS_ALLHALTED, "allhalted" },
321  { DM_DMSTATUS, DM_DMSTATUS_ANYHALTED, "anyhalted" },
322  { DM_DMSTATUS, DM_DMSTATUS_AUTHENTICATED, "authenticated" },
323  { DM_DMSTATUS, DM_DMSTATUS_AUTHBUSY, "authbusy" },
324  { DM_DMSTATUS, DM_DMSTATUS_HASRESETHALTREQ, "hasresethaltreq" },
325  { DM_DMSTATUS, DM_DMSTATUS_CONFSTRPTRVALID, "confstrptrvalid" },
326  { DM_DMSTATUS, DM_DMSTATUS_VERSION, "version" },
327 
328  { DM_ABSTRACTCS, DM_ABSTRACTCS_PROGBUFSIZE, "progbufsize" },
329  { DM_ABSTRACTCS, DM_ABSTRACTCS_BUSY, "busy" },
330  { DM_ABSTRACTCS, DM_ABSTRACTCS_CMDERR, "cmderr" },
331  { DM_ABSTRACTCS, DM_ABSTRACTCS_DATACOUNT, "datacount" },
332 
333  { DM_COMMAND, DM_COMMAND_CMDTYPE, "cmdtype" },
334 
335  { DM_SBCS, DM_SBCS_SBVERSION, "sbversion" },
336  { DM_SBCS, DM_SBCS_SBBUSYERROR, "sbbusyerror" },
337  { DM_SBCS, DM_SBCS_SBBUSY, "sbbusy" },
338  { DM_SBCS, DM_SBCS_SBREADONADDR, "sbreadonaddr" },
339  { DM_SBCS, DM_SBCS_SBACCESS, "sbaccess" },
340  { DM_SBCS, DM_SBCS_SBAUTOINCREMENT, "sbautoincrement" },
341  { DM_SBCS, DM_SBCS_SBREADONDATA, "sbreadondata" },
342  { DM_SBCS, DM_SBCS_SBERROR, "sberror" },
343  { DM_SBCS, DM_SBCS_SBASIZE, "sbasize" },
344  { DM_SBCS, DM_SBCS_SBACCESS128, "sbaccess128" },
345  { DM_SBCS, DM_SBCS_SBACCESS64, "sbaccess64" },
346  { DM_SBCS, DM_SBCS_SBACCESS32, "sbaccess32" },
347  { DM_SBCS, DM_SBCS_SBACCESS16, "sbaccess16" },
348  { DM_SBCS, DM_SBCS_SBACCESS8, "sbaccess8" },
349  };
350 
351  text[0] = 0;
352  for (unsigned int i = 0; i < ARRAY_SIZE(description); i++) {
353  if (description[i].address == address) {
354  uint64_t mask = description[i].mask;
355  unsigned int value = get_field(data, mask);
356  if (value) {
357  if (i > 0)
358  *(text++) = ' ';
359  if (mask & (mask >> 1)) {
360  /* If the field is more than 1 bit wide. */
361  sprintf(text, "%s=%d", description[i].name, value);
362  } else {
363  strcpy(text, description[i].name);
364  }
365  text += strlen(text);
366  }
367  }
368  }
369 }
370 
371 static void dump_field(int idle, const struct scan_field *field)
372 {
373  static const char * const op_string[] = {"-", "r", "w", "?"};
374  static const char * const status_string[] = {"+", "?", "F", "b"};
375 
377  return;
378 
379  uint64_t out = buf_get_u64(field->out_value, 0, field->num_bits);
380  unsigned int out_op = get_field(out, DTM_DMI_OP);
381  unsigned int out_data = get_field(out, DTM_DMI_DATA);
382  unsigned int out_address = out >> DTM_DMI_ADDRESS_OFFSET;
383 
384  uint64_t in = buf_get_u64(field->in_value, 0, field->num_bits);
385  unsigned int in_op = get_field(in, DTM_DMI_OP);
386  unsigned int in_data = get_field(in, DTM_DMI_DATA);
387  unsigned int in_address = in >> DTM_DMI_ADDRESS_OFFSET;
388 
390  __FILE__, __LINE__, "scan",
391  "%ub %s %08x @%02x -> %s %08x @%02x; %di",
392  field->num_bits, op_string[out_op], out_data, out_address,
393  status_string[in_op], in_data, in_address, idle);
394 
395  char out_text[500];
396  char in_text[500];
397  decode_dmi(out_text, out_address, out_data);
398  decode_dmi(in_text, in_address, in_data);
399  if (in_text[0] || out_text[0]) {
400  log_printf_lf(LOG_LVL_DEBUG, __FILE__, __LINE__, "scan", "%s -> %s",
401  out_text, in_text);
402  }
403 }
404 
405 /*** Utility functions. ***/
406 
407 static void select_dmi(struct target *target)
408 {
409  if (bscan_tunnel_ir_width != 0) {
411  return;
412  }
414 }
415 
416 static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
417 {
418  struct scan_field field;
419  uint8_t in_value[4];
420  uint8_t out_value[4] = { 0 };
421 
422  if (bscan_tunnel_ir_width != 0)
423  return dtmcontrol_scan_via_bscan(target, out);
424 
425  buf_set_u32(out_value, 0, 32, out);
426 
428 
429  field.num_bits = 32;
430  field.out_value = out_value;
431  field.in_value = in_value;
432  jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
433 
434  /* Always return to dmi. */
436 
437  int retval = jtag_execute_queue();
438  if (retval != ERROR_OK) {
439  LOG_ERROR("failed jtag scan: %d", retval);
440  return retval;
441  }
442 
443  uint32_t in = buf_get_u32(field.in_value, 0, 32);
444  LOG_DEBUG("DTMCS: 0x%x -> 0x%x", out, in);
445 
446  return in;
447 }
448 
450 {
452  info->dmi_busy_delay += info->dmi_busy_delay / 10 + 1;
453  LOG_DEBUG("dtmcs_idle=%d, dmi_busy_delay=%d, ac_busy_delay=%d",
454  info->dtmcs_idle, info->dmi_busy_delay,
455  info->ac_busy_delay);
456 
458 }
459 
464 static dmi_status_t dmi_scan(struct target *target, uint32_t *address_in,
465  uint32_t *data_in, dmi_op_t op, uint32_t address_out, uint32_t data_out,
466  bool exec)
467 {
469  RISCV_INFO(r);
470  unsigned int num_bits = info->abits + DTM_DMI_OP_LENGTH + DTM_DMI_DATA_LENGTH;
471  size_t num_bytes = (num_bits + 7) / 8;
472  uint8_t in[num_bytes];
473  uint8_t out[num_bytes];
474  struct scan_field field = {
475  .num_bits = num_bits,
476  .out_value = out,
477  .in_value = in
478  };
480 
481  if (r->reset_delays_wait >= 0) {
482  r->reset_delays_wait--;
483  if (r->reset_delays_wait < 0) {
484  info->dmi_busy_delay = 0;
485  info->ac_busy_delay = 0;
486  }
487  }
488 
489  memset(in, 0, num_bytes);
490  memset(out, 0, num_bytes);
491 
492  assert(info->abits != 0);
493 
496  buf_set_u32(out, DTM_DMI_ADDRESS_OFFSET, info->abits, address_out);
497 
498  /* I wanted to place this code in a different function, but the way JTAG command
499  queueing works in the jtag handling functions, the scan fields either have to be
500  heap allocated, global/static, or else they need to stay on the stack until
501  the jtag_execute_queue() call. Heap or static fields in this case doesn't seem
502  the best fit. Declaring stack based field values in a subsidiary function call wouldn't
503  work. */
504  if (bscan_tunnel_ir_width != 0) {
505  riscv_add_bscan_tunneled_scan(target, &field, &bscan_ctxt);
506  } else {
507  /* Assume dbus is already selected. */
508  jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
509  }
510 
511  int idle_count = info->dmi_busy_delay;
512  if (exec)
513  idle_count += info->ac_busy_delay;
514 
515  if (idle_count)
516  jtag_add_runtest(idle_count, TAP_IDLE);
517 
518  int retval = jtag_execute_queue();
519  if (retval != ERROR_OK) {
520  LOG_ERROR("dmi_scan failed jtag scan");
521  if (data_in)
522  *data_in = ~0;
523  return DMI_STATUS_FAILED;
524  }
525 
526  if (bscan_tunnel_ir_width != 0) {
527  /* need to right-shift "in" by one bit, because of clock skew between BSCAN TAP and DM TAP */
528  buffer_shr(in, num_bytes, 1);
529  }
530 
531  if (data_in)
533 
534  if (address_in)
535  *address_in = buf_get_u32(in, DTM_DMI_ADDRESS_OFFSET, info->abits);
536  dump_field(idle_count, &field);
538 }
539 
556 static int dmi_op_timeout(struct target *target, uint32_t *data_in,
557  bool *dmi_busy_encountered, int dmi_op, uint32_t address,
558  uint32_t data_out, int timeout_sec, bool exec, bool ensure_success)
559 {
561 
563  uint32_t address_in;
564 
565  if (dmi_busy_encountered)
566  *dmi_busy_encountered = false;
567 
568  const char *op_name;
569  switch (dmi_op) {
570  case DMI_OP_NOP:
571  op_name = "nop";
572  break;
573  case DMI_OP_READ:
574  op_name = "read";
575  break;
576  case DMI_OP_WRITE:
577  op_name = "write";
578  break;
579  default:
580  LOG_ERROR("Invalid DMI operation: %d", dmi_op);
581  return ERROR_FAIL;
582  }
583 
584  keep_alive();
585 
586  time_t start = time(NULL);
587  /* This first loop performs the request. Note that if for some reason this
588  * stays busy, it is actually due to the previous access. */
589  while (1) {
590  status = dmi_scan(target, NULL, NULL, dmi_op, address, data_out,
591  exec);
592  if (status == DMI_STATUS_BUSY) {
594  if (dmi_busy_encountered)
595  *dmi_busy_encountered = true;
596  } else if (status == DMI_STATUS_SUCCESS) {
597  break;
598  } else {
599  LOG_ERROR("failed %s at 0x%x, status=%d", op_name, address, status);
601  return ERROR_FAIL;
602  }
603  if (time(NULL) - start > timeout_sec)
604  return ERROR_TIMEOUT_REACHED;
605  }
606 
607  if (status != DMI_STATUS_SUCCESS) {
608  LOG_ERROR("Failed %s at 0x%x; status=%d", op_name, address, status);
609  return ERROR_FAIL;
610  }
611 
612  if (ensure_success) {
613  /* This second loop ensures the request succeeded, and gets back data.
614  * Note that NOP can result in a 'busy' result as well, but that would be
615  * noticed on the next DMI access we do. */
616  while (1) {
617  status = dmi_scan(target, &address_in, data_in, DMI_OP_NOP, address, 0,
618  false);
619  if (status == DMI_STATUS_BUSY) {
621  if (dmi_busy_encountered)
622  *dmi_busy_encountered = true;
623  } else if (status == DMI_STATUS_SUCCESS) {
624  break;
625  } else {
626  if (data_in) {
627  LOG_ERROR("Failed %s (NOP) at 0x%x; value=0x%x, status=%d",
628  op_name, address, *data_in, status);
629  } else {
630  LOG_ERROR("Failed %s (NOP) at 0x%x; status=%d", op_name, address,
631  status);
632  }
634  return ERROR_FAIL;
635  }
636  if (time(NULL) - start > timeout_sec)
637  return ERROR_TIMEOUT_REACHED;
638  }
639  }
640 
641  return ERROR_OK;
642 }
643 
644 static int dmi_op(struct target *target, uint32_t *data_in,
645  bool *dmi_busy_encountered, int dmi_op, uint32_t address,
646  uint32_t data_out, bool exec, bool ensure_success)
647 {
648  int result = dmi_op_timeout(target, data_in, dmi_busy_encountered, dmi_op,
649  address, data_out, riscv_command_timeout_sec, exec, ensure_success);
650  if (result == ERROR_TIMEOUT_REACHED) {
651  LOG_ERROR("DMI operation didn't complete in %d seconds. The target is "
652  "either really slow or broken. You could increase the "
653  "timeout with riscv set_command_timeout_sec.",
655  return ERROR_FAIL;
656  }
657  return result;
658 }
659 
660 static int dmi_read(struct target *target, uint32_t *value, uint32_t address)
661 {
662  return dmi_op(target, value, NULL, DMI_OP_READ, address, 0, false, true);
663 }
664 
665 static int dmi_read_exec(struct target *target, uint32_t *value, uint32_t address)
666 {
667  return dmi_op(target, value, NULL, DMI_OP_READ, address, 0, true, true);
668 }
669 
670 static int dmi_write(struct target *target, uint32_t address, uint32_t value)
671 {
672  return dmi_op(target, NULL, NULL, DMI_OP_WRITE, address, value, false, true);
673 }
674 
675 static int dmi_write_exec(struct target *target, uint32_t address,
676  uint32_t value, bool ensure_success)
677 {
678  return dmi_op(target, NULL, NULL, DMI_OP_WRITE, address, value, true, ensure_success);
679 }
680 
681 static int dmstatus_read_timeout(struct target *target, uint32_t *dmstatus,
682  bool authenticated, unsigned int timeout_sec)
683 {
684  int result = dmi_op_timeout(target, dmstatus, NULL, DMI_OP_READ,
685  DM_DMSTATUS, 0, timeout_sec, false, true);
686  if (result != ERROR_OK)
687  return result;
688  int dmstatus_version = get_field(*dmstatus, DM_DMSTATUS_VERSION);
689  if (dmstatus_version != 2 && dmstatus_version != 3) {
690  LOG_ERROR("OpenOCD only supports Debug Module version 2 (0.13) and 3 (1.0), not "
691  "%d (dmstatus=0x%x). This error might be caused by a JTAG "
692  "signal issue. Try reducing the JTAG clock speed.",
693  get_field(*dmstatus, DM_DMSTATUS_VERSION), *dmstatus);
694  } else if (authenticated && !get_field(*dmstatus, DM_DMSTATUS_AUTHENTICATED)) {
695  LOG_ERROR("Debugger is not authenticated to target Debug Module. "
696  "(dmstatus=0x%x). Use `riscv authdata_read` and "
697  "`riscv authdata_write` commands to authenticate.", *dmstatus);
698  return ERROR_FAIL;
699  }
700  return ERROR_OK;
701 }
702 
703 static int dmstatus_read(struct target *target, uint32_t *dmstatus,
704  bool authenticated)
705 {
706  return dmstatus_read_timeout(target, dmstatus, authenticated,
708 }
709 
711 {
713  info->ac_busy_delay += info->ac_busy_delay / 10 + 1;
714  LOG_DEBUG("dtmcs_idle=%d, dmi_busy_delay=%d, ac_busy_delay=%d",
715  info->dtmcs_idle, info->dmi_busy_delay,
716  info->ac_busy_delay);
717 }
718 
719 static uint32_t __attribute__((unused)) abstract_register_size(unsigned int width)
720 {
721  switch (width) {
722  case 32:
724  case 64:
726  case 128:
728  default:
729  LOG_ERROR("Unsupported register width: %d", width);
730  return 0;
731  }
732 }
733 
734 static int wait_for_idle(struct target *target, uint32_t *abstractcs)
735 {
737  time_t start = time(NULL);
738  while (1) {
739  if (dmi_read(target, abstractcs, DM_ABSTRACTCS) != ERROR_OK)
740  return ERROR_FAIL;
741 
742  if (get_field(*abstractcs, DM_ABSTRACTCS_BUSY) == 0)
743  return ERROR_OK;
744 
745  if (time(NULL) - start > riscv_command_timeout_sec) {
746  info->cmderr = get_field(*abstractcs, DM_ABSTRACTCS_CMDERR);
747  if (info->cmderr != CMDERR_NONE) {
748  const char *errors[8] = {
749  "none",
750  "busy",
751  "not supported",
752  "exception",
753  "halt/resume",
754  "reserved",
755  "reserved",
756  "other" };
757 
758  LOG_ERROR("Abstract command ended in error '%s' (abstractcs=0x%x)",
759  errors[info->cmderr], *abstractcs);
760  }
761 
762  LOG_ERROR("Timed out after %ds waiting for busy to go low (abstractcs=0x%x). "
763  "Increase the timeout with riscv set_command_timeout_sec.",
765  *abstractcs);
766  return ERROR_FAIL;
767  }
768  }
769 }
770 
771 static int execute_abstract_command(struct target *target, uint32_t command)
772 {
774  if (debug_level >= LOG_LVL_DEBUG) {
776  case 0:
777  LOG_DEBUG("command=0x%x; access register, size=%d, postexec=%d, "
778  "transfer=%d, write=%d, regno=0x%x",
779  command,
785  break;
786  default:
787  LOG_DEBUG("command=0x%x", command);
788  break;
789  }
790  }
791 
793  return ERROR_FAIL;
794 
795  uint32_t abstractcs = 0;
796  int result = wait_for_idle(target, &abstractcs);
797 
798  info->cmderr = get_field(abstractcs, DM_ABSTRACTCS_CMDERR);
799  if (info->cmderr != 0 || result != ERROR_OK) {
800  LOG_DEBUG("command 0x%x failed; abstractcs=0x%x", command, abstractcs);
801  /* Clear the error. */
803  return ERROR_FAIL;
804  }
805 
806  return ERROR_OK;
807 }
808 
809 static riscv_reg_t read_abstract_arg(struct target *target, unsigned int index,
810  unsigned int size_bits)
811 {
812  riscv_reg_t value = 0;
813  uint32_t v;
814  unsigned int offset = index * size_bits / 32;
815  switch (size_bits) {
816  default:
817  LOG_ERROR("Unsupported size: %d bits", size_bits);
818  return ~0;
819  case 64:
820  dmi_read(target, &v, DM_DATA0 + offset + 1);
821  value |= ((uint64_t) v) << 32;
822  /* falls through */
823  case 32:
824  dmi_read(target, &v, DM_DATA0 + offset);
825  value |= v;
826  }
827  return value;
828 }
829 
830 static int write_abstract_arg(struct target *target, unsigned int index,
831  riscv_reg_t value, unsigned int size_bits)
832 {
833  unsigned int offset = index * size_bits / 32;
834  switch (size_bits) {
835  default:
836  LOG_ERROR("Unsupported size: %d bits", size_bits);
837  return ERROR_FAIL;
838  case 64:
839  dmi_write(target, DM_DATA0 + offset + 1, value >> 32);
840  /* falls through */
841  case 32:
842  dmi_write(target, DM_DATA0 + offset, value);
843  }
844  return ERROR_OK;
845 }
846 
850 static uint32_t access_register_command(struct target *target, uint32_t number,
851  unsigned int size, uint32_t flags)
852 {
853  uint32_t command = set_field(0, DM_COMMAND_CMDTYPE, 0);
854  switch (size) {
855  case 32:
857  break;
858  case 64:
860  break;
861  default:
862  LOG_ERROR("%d-bit register %s not supported.", size,
864  assert(0);
865  }
866 
867  if (number <= GDB_REGNO_XPR31) {
869  0x1000 + number - GDB_REGNO_ZERO);
870  } else if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
872  0x1020 + number - GDB_REGNO_FPR0);
873  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
876  } else if (number >= GDB_REGNO_COUNT) {
877  /* Custom register. */
880  assert(reg_info);
882  0xc000 + reg_info->custom_number);
883  } else {
884  assert(0);
885  }
886 
887  command |= flags;
888 
889  return command;
890 }
891 
892 static int register_read_abstract(struct target *target, uint64_t *value,
893  uint32_t number, unsigned int size)
894 {
896 
898  !info->abstract_read_fpr_supported)
899  return ERROR_FAIL;
901  !info->abstract_read_csr_supported)
902  return ERROR_FAIL;
903  /* The spec doesn't define abstract register numbers for vector registers. */
905  return ERROR_FAIL;
906 
909 
910  int result = execute_abstract_command(target, command);
911  if (result != ERROR_OK) {
912  if (info->cmderr == CMDERR_NOT_SUPPORTED) {
914  info->abstract_read_fpr_supported = false;
915  LOG_INFO("Disabling abstract command reads from FPRs.");
916  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
917  info->abstract_read_csr_supported = false;
918  LOG_INFO("Disabling abstract command reads from CSRs.");
919  }
920  }
921  return result;
922  }
923 
924  if (value)
925  *value = read_abstract_arg(target, 0, size);
926 
927  return ERROR_OK;
928 }
929 
930 static int register_write_abstract(struct target *target, uint32_t number,
931  uint64_t value, unsigned int size)
932 {
934 
936  !info->abstract_write_fpr_supported)
937  return ERROR_FAIL;
939  !info->abstract_write_csr_supported)
940  return ERROR_FAIL;
941 
945 
946  if (write_abstract_arg(target, 0, value, size) != ERROR_OK)
947  return ERROR_FAIL;
948 
949  int result = execute_abstract_command(target, command);
950  if (result != ERROR_OK) {
951  if (info->cmderr == CMDERR_NOT_SUPPORTED) {
953  info->abstract_write_fpr_supported = false;
954  LOG_INFO("Disabling abstract command writes to FPRs.");
955  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
956  info->abstract_write_csr_supported = false;
957  LOG_INFO("Disabling abstract command writes to CSRs.");
958  }
959  }
960  return result;
961  }
962 
963  return ERROR_OK;
964 }
965 
966 /*
967  * Sets the AAMSIZE field of a memory access abstract command based on
968  * the width (bits).
969  */
970 static uint32_t abstract_memory_size(unsigned int width)
971 {
972  switch (width) {
973  case 8:
974  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 0);
975  case 16:
976  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 1);
977  case 32:
978  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 2);
979  case 64:
980  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 3);
981  case 128:
982  return set_field(0, AC_ACCESS_MEMORY_AAMSIZE, 4);
983  default:
984  LOG_ERROR("Unsupported memory width: %d", width);
985  return 0;
986  }
987 }
988 
989 /*
990  * Creates a memory access abstract command.
991  */
992 static uint32_t access_memory_command(struct target *target, bool virtual,
993  unsigned int width, bool postincrement, bool write)
994 {
995  uint32_t command = set_field(0, AC_ACCESS_MEMORY_CMDTYPE, 2);
999  postincrement);
1001 
1002  return command;
1003 }
1004 
1005 static int examine_progbuf(struct target *target)
1006 {
1008 
1009  if (info->progbuf_writable != YNM_MAYBE)
1010  return ERROR_OK;
1011 
1012  /* Figure out if progbuf is writable. */
1013 
1014  if (info->progbufsize < 1) {
1015  info->progbuf_writable = YNM_NO;
1016  LOG_INFO("No program buffer present.");
1017  return ERROR_OK;
1018  }
1019 
1020  uint64_t s0;
1022  return ERROR_FAIL;
1023 
1024  struct riscv_program program;
1025  riscv_program_init(&program, target);
1026  riscv_program_insert(&program, auipc(S0));
1027  if (riscv_program_exec(&program, target) != ERROR_OK)
1028  return ERROR_FAIL;
1029 
1030  if (register_read_direct(target, &info->progbuf_address, GDB_REGNO_S0) != ERROR_OK)
1031  return ERROR_FAIL;
1032 
1033  riscv_program_init(&program, target);
1034  riscv_program_insert(&program, sw(S0, S0, 0));
1035  int result = riscv_program_exec(&program, target);
1036 
1038  return ERROR_FAIL;
1039 
1040  if (result != ERROR_OK) {
1041  /* This program might have failed if the program buffer is not
1042  * writable. */
1043  info->progbuf_writable = YNM_NO;
1044  return ERROR_OK;
1045  }
1046 
1047  uint32_t written;
1048  if (dmi_read(target, &written, DM_PROGBUF0) != ERROR_OK)
1049  return ERROR_FAIL;
1050  if (written == (uint32_t) info->progbuf_address) {
1051  LOG_INFO("progbuf is writable at 0x%" PRIx64,
1052  info->progbuf_address);
1053  info->progbuf_writable = YNM_YES;
1054 
1055  } else {
1056  LOG_INFO("progbuf is not writeable at 0x%" PRIx64,
1057  info->progbuf_address);
1058  info->progbuf_writable = YNM_NO;
1059  }
1060 
1061  return ERROR_OK;
1062 }
1063 
1064 static int is_fpu_reg(uint32_t gdb_regno)
1065 {
1066  return (gdb_regno >= GDB_REGNO_FPR0 && gdb_regno <= GDB_REGNO_FPR31) ||
1068  (gdb_regno == GDB_REGNO_CSR0 + CSR_FRM) ||
1070 }
1071 
1072 static int is_vector_reg(uint32_t gdb_regno)
1073 {
1074  return (gdb_regno >= GDB_REGNO_V0 && gdb_regno <= GDB_REGNO_V31) ||
1078  gdb_regno == GDB_REGNO_VL ||
1081 }
1082 
1083 static int prep_for_register_access(struct target *target, uint64_t *mstatus,
1084  int regno)
1085 {
1086  if (is_fpu_reg(regno) || is_vector_reg(regno)) {
1087  if (register_read(target, mstatus, GDB_REGNO_MSTATUS) != ERROR_OK)
1088  return ERROR_FAIL;
1089  if (is_fpu_reg(regno) && (*mstatus & MSTATUS_FS) == 0) {
1091  set_field(*mstatus, MSTATUS_FS, 1)) != ERROR_OK)
1092  return ERROR_FAIL;
1093  } else if (is_vector_reg(regno) && (*mstatus & MSTATUS_VS) == 0) {
1095  set_field(*mstatus, MSTATUS_VS, 1)) != ERROR_OK)
1096  return ERROR_FAIL;
1097  }
1098  } else {
1099  *mstatus = 0;
1100  }
1101  return ERROR_OK;
1102 }
1103 
1105  uint64_t mstatus, int regno)
1106 {
1107  if ((is_fpu_reg(regno) && (mstatus & MSTATUS_FS) == 0) ||
1108  (is_vector_reg(regno) && (mstatus & MSTATUS_VS) == 0))
1110  return ERROR_FAIL;
1111  return ERROR_OK;
1112 }
1113 
1114 typedef enum {
1119 
1120 typedef struct {
1121  /* How can the debugger access this memory? */
1123  /* Memory address to access the scratch memory from the hart. */
1125  /* Memory address to access the scratch memory from the debugger. */
1128 } scratch_mem_t;
1129 
1133 static int scratch_reserve(struct target *target,
1134  scratch_mem_t *scratch,
1135  struct riscv_program *program,
1136  unsigned int size_bytes)
1137 {
1138  riscv_addr_t alignment = 1;
1139  while (alignment < size_bytes)
1140  alignment *= 2;
1141 
1142  scratch->area = NULL;
1143 
1145 
1146  /* Option 1: See if data# registers can be used as the scratch memory */
1147  if (info->dataaccess == 1) {
1148  /* Sign extend dataaddr. */
1149  scratch->hart_address = info->dataaddr;
1150  if (info->dataaddr & (1<<11))
1151  scratch->hart_address |= 0xfffffffffffff000ULL;
1152  /* Align. */
1153  scratch->hart_address = (scratch->hart_address + alignment - 1) & ~(alignment - 1);
1154 
1155  if ((size_bytes + scratch->hart_address - info->dataaddr + 3) / 4 >=
1156  info->datasize) {
1157  scratch->memory_space = SPACE_DM_DATA;
1158  scratch->debug_address = (scratch->hart_address - info->dataaddr) / 4;
1159  return ERROR_OK;
1160  }
1161  }
1162 
1163  /* Option 2: See if progbuf can be used as the scratch memory */
1165  return ERROR_FAIL;
1166 
1167  /* Allow for ebreak at the end of the program. */
1168  unsigned int program_size = (program->instruction_count + 1) * 4;
1169  scratch->hart_address = (info->progbuf_address + program_size + alignment - 1) &
1170  ~(alignment - 1);
1171  if ((info->progbuf_writable == YNM_YES) &&
1172  ((size_bytes + scratch->hart_address - info->progbuf_address + 3) / 4 >=
1173  info->progbufsize)) {
1174  scratch->memory_space = SPACE_DMI_PROGBUF;
1175  scratch->debug_address = (scratch->hart_address - info->progbuf_address) / 4;
1176  return ERROR_OK;
1177  }
1178 
1179  /* Option 3: User-configured memory area as scratch RAM */
1180  if (target_alloc_working_area(target, size_bytes + alignment - 1,
1181  &scratch->area) == ERROR_OK) {
1182  scratch->hart_address = (scratch->area->address + alignment - 1) &
1183  ~(alignment - 1);
1184  scratch->memory_space = SPACE_DMI_RAM;
1185  scratch->debug_address = scratch->hart_address;
1186  return ERROR_OK;
1187  }
1188 
1189  LOG_ERROR("Couldn't find %d bytes of scratch RAM to use. Please configure "
1190  "a work area with 'configure -work-area-phys'.", size_bytes);
1191  return ERROR_FAIL;
1192 }
1193 
1194 static int scratch_release(struct target *target,
1195  scratch_mem_t *scratch)
1196 {
1197  return target_free_working_area(target, scratch->area);
1198 }
1199 
1200 static int scratch_read64(struct target *target, scratch_mem_t *scratch,
1201  uint64_t *value)
1202 {
1203  uint32_t v;
1204  switch (scratch->memory_space) {
1205  case SPACE_DM_DATA:
1206  if (dmi_read(target, &v, DM_DATA0 + scratch->debug_address) != ERROR_OK)
1207  return ERROR_FAIL;
1208  *value = v;
1209  if (dmi_read(target, &v, DM_DATA1 + scratch->debug_address) != ERROR_OK)
1210  return ERROR_FAIL;
1211  *value |= ((uint64_t) v) << 32;
1212  break;
1213  case SPACE_DMI_PROGBUF:
1214  if (dmi_read(target, &v, DM_PROGBUF0 + scratch->debug_address) != ERROR_OK)
1215  return ERROR_FAIL;
1216  *value = v;
1217  if (dmi_read(target, &v, DM_PROGBUF1 + scratch->debug_address) != ERROR_OK)
1218  return ERROR_FAIL;
1219  *value |= ((uint64_t) v) << 32;
1220  break;
1221  case SPACE_DMI_RAM:
1222  {
1223  uint8_t buffer[8] = {0};
1224  if (read_memory(target, scratch->debug_address, 4, 2, buffer, 4) != ERROR_OK)
1225  return ERROR_FAIL;
1226  *value = buffer[0] |
1227  (((uint64_t) buffer[1]) << 8) |
1228  (((uint64_t) buffer[2]) << 16) |
1229  (((uint64_t) buffer[3]) << 24) |
1230  (((uint64_t) buffer[4]) << 32) |
1231  (((uint64_t) buffer[5]) << 40) |
1232  (((uint64_t) buffer[6]) << 48) |
1233  (((uint64_t) buffer[7]) << 56);
1234  }
1235  break;
1236  }
1237  return ERROR_OK;
1238 }
1239 
1240 static int scratch_write64(struct target *target, scratch_mem_t *scratch,
1241  uint64_t value)
1242 {
1243  switch (scratch->memory_space) {
1244  case SPACE_DM_DATA:
1245  dmi_write(target, DM_DATA0 + scratch->debug_address, value);
1246  dmi_write(target, DM_DATA1 + scratch->debug_address, value >> 32);
1247  break;
1248  case SPACE_DMI_PROGBUF:
1249  dmi_write(target, DM_PROGBUF0 + scratch->debug_address, value);
1250  dmi_write(target, DM_PROGBUF1 + scratch->debug_address, value >> 32);
1251  break;
1252  case SPACE_DMI_RAM:
1253  {
1254  uint8_t buffer[8] = {
1255  value,
1256  value >> 8,
1257  value >> 16,
1258  value >> 24,
1259  value >> 32,
1260  value >> 40,
1261  value >> 48,
1262  value >> 56
1263  };
1264  if (write_memory(target, scratch->debug_address, 4, 2, buffer) != ERROR_OK)
1265  return ERROR_FAIL;
1266  }
1267  break;
1268  }
1269  return ERROR_OK;
1270 }
1271 
1273 static unsigned int register_size(struct target *target, unsigned int number)
1274 {
1275  /* If reg_cache hasn't been initialized yet, make a guess. We need this for
1276  * when this function is called during examine(). */
1277  if (target->reg_cache)
1278  return target->reg_cache->reg_list[number].size;
1279  else
1280  return riscv_xlen(target);
1281 }
1282 
1283 static bool has_sufficient_progbuf(struct target *target, unsigned int size)
1284 {
1286  RISCV_INFO(r);
1287 
1288  return info->progbufsize + r->impebreak >= size;
1289 }
1290 
1295 static int register_write_direct(struct target *target, unsigned int number,
1296  uint64_t value)
1297 {
1298  LOG_DEBUG("{%d} %s <- 0x%" PRIx64, riscv_current_hartid(target),
1299  gdb_regno_name(number), value);
1300 
1301  int result = register_write_abstract(target, number, value,
1303  if (result == ERROR_OK || !has_sufficient_progbuf(target, 2) ||
1305  return result;
1306 
1307  struct riscv_program program;
1308  riscv_program_init(&program, target);
1309 
1310  uint64_t s0;
1312  return ERROR_FAIL;
1313 
1314  uint64_t mstatus;
1315  if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1316  return ERROR_FAIL;
1317 
1318  scratch_mem_t scratch;
1319  bool use_scratch = false;
1322  riscv_xlen(target) < 64) {
1323  /* There are no instructions to move all the bits from a register, so
1324  * we need to use some scratch RAM. */
1325  use_scratch = true;
1326  riscv_program_insert(&program, fld(number - GDB_REGNO_FPR0, S0, 0));
1327 
1328  if (scratch_reserve(target, &scratch, &program, 8) != ERROR_OK)
1329  return ERROR_FAIL;
1330 
1332  != ERROR_OK) {
1333  scratch_release(target, &scratch);
1334  return ERROR_FAIL;
1335  }
1336 
1337  if (scratch_write64(target, &scratch, value) != ERROR_OK) {
1338  scratch_release(target, &scratch);
1339  return ERROR_FAIL;
1340  }
1341 
1342  } else if (number == GDB_REGNO_VTYPE) {
1343  riscv_program_insert(&program, csrr(S0, CSR_VL));
1344  riscv_program_insert(&program, vsetvli(ZERO, S0, value));
1345 
1346  } else {
1348  return ERROR_FAIL;
1349 
1351  if (riscv_supports_extension(target, 'D'))
1353  else
1355  } else if (number == GDB_REGNO_VL) {
1356  /* "The XLEN-bit-wide read-only vl CSR can only be updated by the
1357  * vsetvli and vsetvl instructions, and the fault-only-rst vector
1358  * load instruction variants." */
1359  riscv_reg_t vtype;
1360  if (register_read(target, &vtype, GDB_REGNO_VTYPE) != ERROR_OK)
1361  return ERROR_FAIL;
1362  if (riscv_program_insert(&program, vsetvli(ZERO, S0, vtype)) != ERROR_OK)
1363  return ERROR_FAIL;
1364  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
1365  riscv_program_csrw(&program, S0, number);
1366  } else {
1367  LOG_ERROR("Unsupported register (enum gdb_regno)(%d)", number);
1368  return ERROR_FAIL;
1369  }
1370  }
1371 
1372  int exec_out = riscv_program_exec(&program, target);
1373  /* Don't message on error. Probably the register doesn't exist. */
1374  if (exec_out == ERROR_OK && target->reg_cache) {
1375  struct reg *reg = &target->reg_cache->reg_list[number];
1376  buf_set_u64(reg->value, 0, reg->size, value);
1377  }
1378 
1379  if (use_scratch)
1380  scratch_release(target, &scratch);
1381 
1383  return ERROR_FAIL;
1384 
1385  /* Restore S0. */
1387  return ERROR_FAIL;
1388 
1389  return exec_out;
1390 }
1391 
1393 static int register_read(struct target *target, uint64_t *value, uint32_t number)
1394 {
1395  if (number == GDB_REGNO_ZERO) {
1396  *value = 0;
1397  return ERROR_OK;
1398  }
1399  int result = register_read_direct(target, value, number);
1400  if (result != ERROR_OK)
1401  return ERROR_FAIL;
1402  if (target->reg_cache) {
1403  struct reg *reg = &target->reg_cache->reg_list[number];
1404  buf_set_u64(reg->value, 0, reg->size, *value);
1405  }
1406  return ERROR_OK;
1407 }
1408 
1410 static int register_read_direct(struct target *target, uint64_t *value, uint32_t number)
1411 {
1412  int result = register_read_abstract(target, value, number,
1414 
1415  if (result != ERROR_OK &&
1417  number > GDB_REGNO_XPR31) {
1418  struct riscv_program program;
1419  riscv_program_init(&program, target);
1420 
1421  scratch_mem_t scratch;
1422  bool use_scratch = false;
1423 
1424  riscv_reg_t s0;
1426  return ERROR_FAIL;
1427 
1428  /* Write program to move data into s0. */
1429 
1430  uint64_t mstatus;
1431  if (prep_for_register_access(target, &mstatus, number) != ERROR_OK)
1432  return ERROR_FAIL;
1433 
1436  && riscv_xlen(target) < 64) {
1437  /* There are no instructions to move all the bits from a
1438  * register, so we need to use some scratch RAM. */
1440  0));
1441 
1442  if (scratch_reserve(target, &scratch, &program, 8) != ERROR_OK)
1443  return ERROR_FAIL;
1444  use_scratch = true;
1445 
1447  scratch.hart_address) != ERROR_OK) {
1448  scratch_release(target, &scratch);
1449  return ERROR_FAIL;
1450  }
1451  } else if (riscv_supports_extension(target, 'D')) {
1453  } else {
1455  }
1456  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
1457  riscv_program_csrr(&program, S0, number);
1458  } else {
1459  LOG_ERROR("Unsupported register: %s", gdb_regno_name(number));
1460  return ERROR_FAIL;
1461  }
1462 
1463  /* Execute program. */
1464  result = riscv_program_exec(&program, target);
1465  /* Don't message on error. Probably the register doesn't exist. */
1466 
1467  if (use_scratch) {
1468  result = scratch_read64(target, &scratch, value);
1469  scratch_release(target, &scratch);
1470  if (result != ERROR_OK)
1471  return result;
1472  } else {
1473  /* Read S0 */
1475  return ERROR_FAIL;
1476  }
1477 
1479  return ERROR_FAIL;
1480 
1481  /* Restore S0. */
1483  return ERROR_FAIL;
1484  }
1485 
1486  if (result == ERROR_OK) {
1487  LOG_DEBUG("{%d} %s = 0x%" PRIx64, riscv_current_hartid(target),
1488  gdb_regno_name(number), *value);
1489  }
1490 
1491  return result;
1492 }
1493 
1494 static int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
1495 {
1496  time_t start = time(NULL);
1497  while (1) {
1498  uint32_t value;
1499  if (dmstatus_read(target, &value, false) != ERROR_OK)
1500  return ERROR_FAIL;
1501  if (dmstatus)
1502  *dmstatus = value;
1503  if (!get_field(value, DM_DMSTATUS_AUTHBUSY))
1504  break;
1505  if (time(NULL) - start > riscv_command_timeout_sec) {
1506  LOG_ERROR("Timed out after %ds waiting for authbusy to go low (dmstatus=0x%x). "
1507  "Increase the timeout with riscv set_command_timeout_sec.",
1509  value);
1510  return ERROR_FAIL;
1511  }
1512  }
1513 
1514  return ERROR_OK;
1515 }
1516 
1517 /*** OpenOCD target functions. ***/
1518 
1519 static void deinit_target(struct target *target)
1520 {
1521  LOG_DEBUG("riscv_deinit_target()");
1522  struct riscv_info *info = target->arch_info;
1523  if (!info)
1524  return;
1525 
1526  free(info->version_specific);
1527  /* TODO: free register arch_info */
1528  info->version_specific = NULL;
1529 }
1530 
1531 static int set_haltgroup(struct target *target, bool *supported)
1532 {
1533  uint32_t write = set_field(DM_DMCS2_HGWRITE, DM_DMCS2_GROUP, target->smp);
1534  if (dmi_write(target, DM_DMCS2, write) != ERROR_OK)
1535  return ERROR_FAIL;
1536  uint32_t read;
1537  if (dmi_read(target, &read, DM_DMCS2) != ERROR_OK)
1538  return ERROR_FAIL;
1539  *supported = get_field(read, DM_DMCS2_GROUP) == target->smp;
1540  return ERROR_OK;
1541 }
1542 
1543 static int discover_vlenb(struct target *target)
1544 {
1545  RISCV_INFO(r);
1547 
1549  LOG_WARNING("Couldn't read vlenb for %s; vector register access won't work.",
1550  target_name(target));
1551  r->vlenb = 0;
1552  return ERROR_OK;
1553  }
1554  r->vlenb = vlenb;
1555 
1556  LOG_INFO("Vector support with vlenb=%d", r->vlenb);
1557 
1558  return ERROR_OK;
1559 }
1560 
1561 static int examine(struct target *target)
1562 {
1563  /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
1564 
1565  uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
1566  LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol);
1567  LOG_DEBUG(" dmireset=%d", get_field(dtmcontrol, DTM_DTMCS_DMIRESET));
1568  LOG_DEBUG(" idle=%d", get_field(dtmcontrol, DTM_DTMCS_IDLE));
1569  LOG_DEBUG(" dmistat=%d", get_field(dtmcontrol, DTM_DTMCS_DMISTAT));
1570  LOG_DEBUG(" abits=%d", get_field(dtmcontrol, DTM_DTMCS_ABITS));
1571  LOG_DEBUG(" version=%d", get_field(dtmcontrol, DTM_DTMCS_VERSION));
1572  if (dtmcontrol == 0) {
1573  LOG_ERROR("dtmcontrol is 0. Check JTAG connectivity/board power.");
1574  return ERROR_FAIL;
1575  }
1576  if (get_field(dtmcontrol, DTM_DTMCS_VERSION) != 1) {
1577  LOG_ERROR("Unsupported DTM version %d. (dtmcontrol=0x%x)",
1578  get_field(dtmcontrol, DTM_DTMCS_VERSION), dtmcontrol);
1579  return ERROR_FAIL;
1580  }
1581 
1583  /* TODO: This won't be true if there are multiple DMs. */
1584  info->index = target->coreid;
1585  info->abits = get_field(dtmcontrol, DTM_DTMCS_ABITS);
1586  info->dtmcs_idle = get_field(dtmcontrol, DTM_DTMCS_IDLE);
1587 
1588  /* Reset the Debug Module. */
1589  dm013_info_t *dm = get_dm(target);
1590  if (!dm)
1591  return ERROR_FAIL;
1592  if (!dm->was_reset) {
1595  dm->was_reset = true;
1596  }
1597 
1601  uint32_t dmcontrol;
1602  if (dmi_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
1603  return ERROR_FAIL;
1604 
1605  if (!get_field(dmcontrol, DM_DMCONTROL_DMACTIVE)) {
1606  LOG_ERROR("Debug Module did not become active. dmcontrol=0x%x",
1607  dmcontrol);
1608  return ERROR_FAIL;
1609  }
1610 
1611  dm->hasel_supported = get_field(dmcontrol, DM_DMCONTROL_HASEL);
1612 
1613  uint32_t dmstatus;
1614  if (dmstatus_read(target, &dmstatus, false) != ERROR_OK)
1615  return ERROR_FAIL;
1616  LOG_DEBUG("dmstatus: 0x%08x", dmstatus);
1617  int dmstatus_version = get_field(dmstatus, DM_DMSTATUS_VERSION);
1618  if (dmstatus_version != 2 && dmstatus_version != 3) {
1619  /* Error was already printed out in dmstatus_read(). */
1620  return ERROR_FAIL;
1621  }
1622 
1623  uint32_t hartsel =
1624  (get_field(dmcontrol, DM_DMCONTROL_HARTSELHI) <<
1626  get_field(dmcontrol, DM_DMCONTROL_HARTSELLO);
1627  info->hartsellen = 0;
1628  while (hartsel & 1) {
1629  info->hartsellen++;
1630  hartsel >>= 1;
1631  }
1632  LOG_DEBUG("hartsellen=%d", info->hartsellen);
1633 
1634  uint32_t hartinfo;
1635  if (dmi_read(target, &hartinfo, DM_HARTINFO) != ERROR_OK)
1636  return ERROR_FAIL;
1637 
1638  info->datasize = get_field(hartinfo, DM_HARTINFO_DATASIZE);
1639  info->dataaccess = get_field(hartinfo, DM_HARTINFO_DATAACCESS);
1640  info->dataaddr = get_field(hartinfo, DM_HARTINFO_DATAADDR);
1641 
1642  if (!get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED)) {
1643  LOG_ERROR("Debugger is not authenticated to target Debug Module. "
1644  "(dmstatus=0x%x). Use `riscv authdata_read` and "
1645  "`riscv authdata_write` commands to authenticate.", dmstatus);
1646  /* If we return ERROR_FAIL here, then in a multicore setup the next
1647  * core won't be examined, which means we won't set up the
1648  * authentication commands for them, which means the config script
1649  * needs to be a lot more complex. */
1650  return ERROR_OK;
1651  }
1652 
1653  if (dmi_read(target, &info->sbcs, DM_SBCS) != ERROR_OK)
1654  return ERROR_FAIL;
1655 
1656  /* Check that abstract data registers are accessible. */
1657  uint32_t abstractcs;
1658  if (dmi_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
1659  return ERROR_FAIL;
1660  info->datacount = get_field(abstractcs, DM_ABSTRACTCS_DATACOUNT);
1661  info->progbufsize = get_field(abstractcs, DM_ABSTRACTCS_PROGBUFSIZE);
1662 
1663  LOG_INFO("datacount=%d progbufsize=%d", info->datacount, info->progbufsize);
1664 
1665  RISCV_INFO(r);
1666  r->impebreak = get_field(dmstatus, DM_DMSTATUS_IMPEBREAK);
1667 
1668  if (!has_sufficient_progbuf(target, 2)) {
1669  LOG_WARNING("We won't be able to execute fence instructions on this "
1670  "target. Memory may not always appear consistent. "
1671  "(progbufsize=%d, impebreak=%d)", info->progbufsize,
1672  r->impebreak);
1673  }
1674 
1675  if (info->progbufsize < 4 && riscv_enable_virtual) {
1676  LOG_ERROR("set_enable_virtual is not available on this target. It "
1677  "requires a program buffer size of at least 4. (progbufsize=%d) "
1678  "Use `riscv set_enable_virtual off` to continue."
1679  , info->progbufsize);
1680  }
1681 
1682  /* Before doing anything else we must first enumerate the harts. */
1683  if (dm->hart_count < 0) {
1684  for (int i = 0; i < MIN(RISCV_MAX_HARTS, 1 << info->hartsellen); ++i) {
1685  r->current_hartid = i;
1687  return ERROR_FAIL;
1688 
1689  uint32_t s;
1690  if (dmstatus_read(target, &s, true) != ERROR_OK)
1691  return ERROR_FAIL;
1693  break;
1694  dm->hart_count = i + 1;
1695 
1699  }
1700 
1701  LOG_DEBUG("Detected %d harts.", dm->hart_count);
1702  }
1703 
1704  r->current_hartid = target->coreid;
1705 
1706  if (dm->hart_count == 0) {
1707  LOG_ERROR("No harts found!");
1708  return ERROR_FAIL;
1709  }
1710 
1711  /* Don't call any riscv_* functions until after we've counted the number of
1712  * cores and initialized registers. */
1713 
1715  return ERROR_FAIL;
1716 
1717  bool halted = riscv_is_halted(target);
1718  if (!halted) {
1719  if (riscv013_halt_go(target) != ERROR_OK) {
1720  LOG_ERROR("Fatal: Hart %d failed to halt during examine()", r->current_hartid);
1721  return ERROR_FAIL;
1722  }
1723  }
1724 
1725  /* Without knowing anything else we can at least mess with the
1726  * program buffer. */
1727  r->debug_buffer_size = info->progbufsize;
1728 
1729  int result = register_read_abstract(target, NULL, GDB_REGNO_S0, 64);
1730  if (result == ERROR_OK)
1731  r->xlen = 64;
1732  else
1733  r->xlen = 32;
1734 
1735  if (register_read(target, &r->misa, GDB_REGNO_MISA)) {
1736  LOG_ERROR("Fatal: Failed to read MISA from hart %d.", r->current_hartid);
1737  return ERROR_FAIL;
1738  }
1739 
1740  if (riscv_supports_extension(target, 'V')) {
1741  if (discover_vlenb(target) != ERROR_OK)
1742  return ERROR_FAIL;
1743  }
1744 
1745  /* Now init registers based on what we discovered. */
1747  return ERROR_FAIL;
1748 
1749  /* Display this as early as possible to help people who are using
1750  * really slow simulators. */
1751  LOG_DEBUG(" hart %d: XLEN=%d, misa=0x%" PRIx64, r->current_hartid, r->xlen,
1752  r->misa);
1753 
1754  if (!halted)
1756 
1758 
1759  if (target->smp) {
1760  bool haltgroup_supported;
1761  if (set_haltgroup(target, &haltgroup_supported) != ERROR_OK)
1762  return ERROR_FAIL;
1763  if (haltgroup_supported)
1764  LOG_INFO("Core %d made part of halt group %d.", target->coreid,
1765  target->smp);
1766  else
1767  LOG_INFO("Core %d could not be made part of halt group %d.",
1768  target->coreid, target->smp);
1769  }
1770 
1771  /* Some regression suites rely on seeing 'Examined RISC-V core' to know
1772  * when they can connect with gdb/telnet.
1773  * We will need to update those suites if we want to change that text. */
1774  LOG_INFO("Examined RISC-V core; found %d harts",
1776  LOG_INFO(" hart %d: XLEN=%d, misa=0x%" PRIx64, r->current_hartid, r->xlen,
1777  r->misa);
1778  return ERROR_OK;
1779 }
1780 
1781 static int riscv013_authdata_read(struct target *target, uint32_t *value, unsigned int index)
1782 {
1783  if (index > 0) {
1784  LOG_ERROR("Spec 0.13 only has a single authdata register.");
1785  return ERROR_FAIL;
1786  }
1787 
1789  return ERROR_FAIL;
1790 
1791  return dmi_read(target, value, DM_AUTHDATA);
1792 }
1793 
1794 static int riscv013_authdata_write(struct target *target, uint32_t value, unsigned int index)
1795 {
1796  if (index > 0) {
1797  LOG_ERROR("Spec 0.13 only has a single authdata register.");
1798  return ERROR_FAIL;
1799  }
1800 
1801  uint32_t before, after;
1802  if (wait_for_authbusy(target, &before) != ERROR_OK)
1803  return ERROR_FAIL;
1804 
1805  dmi_write(target, DM_AUTHDATA, value);
1806 
1807  if (wait_for_authbusy(target, &after) != ERROR_OK)
1808  return ERROR_FAIL;
1809 
1810  if (!get_field(before, DM_DMSTATUS_AUTHENTICATED) &&
1812  LOG_INFO("authdata_write resulted in successful authentication");
1813  int result = ERROR_OK;
1814  dm013_info_t *dm = get_dm(target);
1815  if (!dm)
1816  return ERROR_FAIL;
1817  target_list_t *entry;
1818  list_for_each_entry(entry, &dm->target_list, list) {
1819  if (examine(entry->target) != ERROR_OK)
1820  result = ERROR_FAIL;
1821  }
1822  return result;
1823  }
1824 
1825  return ERROR_OK;
1826 }
1827 
1828 static int riscv013_hart_count(struct target *target)
1829 {
1830  dm013_info_t *dm = get_dm(target);
1831  assert(dm);
1832  return dm->hart_count;
1833 }
1834 
1835 /* Try to find out the widest memory access size depending on the selected memory access methods. */
1836 static unsigned int riscv013_data_bits(struct target *target)
1837 {
1839  RISCV_INFO(r);
1840 
1841  for (unsigned int i = 0; i < RISCV_NUM_MEM_ACCESS_METHODS; i++) {
1842  int method = r->mem_access_methods[i];
1843 
1844  if (method == RISCV_MEM_ACCESS_PROGBUF) {
1846  return riscv_xlen(target);
1847  } else if (method == RISCV_MEM_ACCESS_SYSBUS) {
1848  if (get_field(info->sbcs, DM_SBCS_SBACCESS128))
1849  return 128;
1850  if (get_field(info->sbcs, DM_SBCS_SBACCESS64))
1851  return 64;
1852  if (get_field(info->sbcs, DM_SBCS_SBACCESS32))
1853  return 32;
1854  if (get_field(info->sbcs, DM_SBCS_SBACCESS16))
1855  return 16;
1856  if (get_field(info->sbcs, DM_SBCS_SBACCESS8))
1857  return 8;
1858  } else if (method == RISCV_MEM_ACCESS_ABSTRACT) {
1859  /* TODO: Once there is a spec for discovering abstract commands, we can
1860  * take those into account as well. For now we assume abstract commands
1861  * support XLEN-wide accesses. */
1862  return riscv_xlen(target);
1863  } else if (method == RISCV_MEM_ACCESS_UNSPECIFIED)
1864  /* No further mem access method to try. */
1865  break;
1866  }
1867  LOG_ERROR("Unable to determine supported data bits on this target. Assuming 32 bits.");
1868  return 32;
1869 }
1870 
1871 static COMMAND_HELPER(riscv013_print_info, struct target *target)
1872 {
1874 
1875  /* Abstract description. */
1876  riscv_print_info_line(CMD, "target", "memory.read_while_running8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
1877  riscv_print_info_line(CMD, "target", "memory.write_while_running8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
1878  riscv_print_info_line(CMD, "target", "memory.read_while_running16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
1879  riscv_print_info_line(CMD, "target", "memory.write_while_running16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
1880  riscv_print_info_line(CMD, "target", "memory.read_while_running32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
1881  riscv_print_info_line(CMD, "target", "memory.write_while_running32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
1882  riscv_print_info_line(CMD, "target", "memory.read_while_running64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
1883  riscv_print_info_line(CMD, "target", "memory.write_while_running64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
1884  riscv_print_info_line(CMD, "target", "memory.read_while_running128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
1885  riscv_print_info_line(CMD, "target", "memory.write_while_running128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
1886 
1887  /* Lower level description. */
1888  riscv_print_info_line(CMD, "dm", "abits", info->abits);
1889  riscv_print_info_line(CMD, "dm", "progbufsize", info->progbufsize);
1890  riscv_print_info_line(CMD, "dm", "sbversion", get_field(info->sbcs, DM_SBCS_SBVERSION));
1891  riscv_print_info_line(CMD, "dm", "sbasize", get_field(info->sbcs, DM_SBCS_SBASIZE));
1892  riscv_print_info_line(CMD, "dm", "sbaccess128", get_field(info->sbcs, DM_SBCS_SBACCESS128));
1893  riscv_print_info_line(CMD, "dm", "sbaccess64", get_field(info->sbcs, DM_SBCS_SBACCESS64));
1894  riscv_print_info_line(CMD, "dm", "sbaccess32", get_field(info->sbcs, DM_SBCS_SBACCESS32));
1895  riscv_print_info_line(CMD, "dm", "sbaccess16", get_field(info->sbcs, DM_SBCS_SBACCESS16));
1896  riscv_print_info_line(CMD, "dm", "sbaccess8", get_field(info->sbcs, DM_SBCS_SBACCESS8));
1897 
1898  uint32_t dmstatus;
1899  if (dmstatus_read(target, &dmstatus, false) == ERROR_OK)
1900  riscv_print_info_line(CMD, "dm", "authenticated", get_field(dmstatus, DM_DMSTATUS_AUTHENTICATED));
1901 
1902  return 0;
1903 }
1904 
1905 static int prep_for_vector_access(struct target *target, uint64_t *vtype,
1906  uint64_t *vl, unsigned int *debug_vl)
1907 {
1908  RISCV_INFO(r);
1909  /* TODO: this continuous save/restore is terrible for performance. */
1910  /* Write vtype and vl. */
1911  unsigned int encoded_vsew;
1912  switch (riscv_xlen(target)) {
1913  case 32:
1914  encoded_vsew = 2;
1915  break;
1916  case 64:
1917  encoded_vsew = 3;
1918  break;
1919  default:
1920  LOG_ERROR("Unsupported xlen: %d", riscv_xlen(target));
1921  return ERROR_FAIL;
1922  }
1923 
1924  /* Save vtype and vl. */
1925  if (register_read(target, vtype, GDB_REGNO_VTYPE) != ERROR_OK)
1926  return ERROR_FAIL;
1928  return ERROR_FAIL;
1929 
1930  if (register_write_direct(target, GDB_REGNO_VTYPE, encoded_vsew << 3) != ERROR_OK)
1931  return ERROR_FAIL;
1932  *debug_vl = DIV_ROUND_UP(r->vlenb * 8, riscv_xlen(target));
1933  if (register_write_direct(target, GDB_REGNO_VL, *debug_vl) != ERROR_OK)
1934  return ERROR_FAIL;
1935 
1936  return ERROR_OK;
1937 }
1938 
1939 static int cleanup_after_vector_access(struct target *target, uint64_t vtype,
1940  uint64_t vl)
1941 {
1942  /* Restore vtype and vl. */
1944  return ERROR_FAIL;
1946  return ERROR_FAIL;
1947  return ERROR_OK;
1948 }
1949 
1951  uint8_t *value, int regno)
1952 {
1953  assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
1954 
1956  return ERROR_FAIL;
1957 
1958  riscv_reg_t s0;
1960  return ERROR_FAIL;
1961 
1962  uint64_t mstatus;
1963  if (prep_for_register_access(target, &mstatus, regno) != ERROR_OK)
1964  return ERROR_FAIL;
1965 
1966  uint64_t vtype, vl;
1967  unsigned int debug_vl;
1968  if (prep_for_vector_access(target, &vtype, &vl, &debug_vl) != ERROR_OK)
1969  return ERROR_FAIL;
1970 
1971  unsigned int vnum = regno - GDB_REGNO_V0;
1972  unsigned int xlen = riscv_xlen(target);
1973 
1974  struct riscv_program program;
1975  riscv_program_init(&program, target);
1976  riscv_program_insert(&program, vmv_x_s(S0, vnum));
1977  riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
1978 
1979  int result = ERROR_OK;
1980  for (unsigned int i = 0; i < debug_vl; i++) {
1981  /* Executing the program might result in an exception if there is some
1982  * issue with the vector implementation/instructions we're using. If that
1983  * happens, attempt to restore as usual. We may have clobbered the
1984  * vector register we tried to read already.
1985  * For other failures, we just return error because things are probably
1986  * so messed up that attempting to restore isn't going to help. */
1987  result = riscv_program_exec(&program, target);
1988  if (result == ERROR_OK) {
1989  uint64_t v;
1991  return ERROR_FAIL;
1992  buf_set_u64(value, xlen * i, xlen, v);
1993  } else {
1994  break;
1995  }
1996  }
1997 
1998  if (cleanup_after_vector_access(target, vtype, vl) != ERROR_OK)
1999  return ERROR_FAIL;
2000 
2001  if (cleanup_after_register_access(target, mstatus, regno) != ERROR_OK)
2002  return ERROR_FAIL;
2004  return ERROR_FAIL;
2005 
2006  return result;
2007 }
2008 
2010  int regno, const uint8_t *value)
2011 {
2012  assert(regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31);
2013 
2015  return ERROR_FAIL;
2016 
2017  riscv_reg_t s0;
2019  return ERROR_FAIL;
2020 
2021  uint64_t mstatus;
2022  if (prep_for_register_access(target, &mstatus, regno) != ERROR_OK)
2023  return ERROR_FAIL;
2024 
2025  uint64_t vtype, vl;
2026  unsigned int debug_vl;
2027  if (prep_for_vector_access(target, &vtype, &vl, &debug_vl) != ERROR_OK)
2028  return ERROR_FAIL;
2029 
2030  unsigned int vnum = regno - GDB_REGNO_V0;
2031  unsigned int xlen = riscv_xlen(target);
2032 
2033  struct riscv_program program;
2034  riscv_program_init(&program, target);
2035  riscv_program_insert(&program, vslide1down_vx(vnum, vnum, S0, true));
2036  int result = ERROR_OK;
2037  for (unsigned int i = 0; i < debug_vl; i++) {
2039  buf_get_u64(value, xlen * i, xlen)) != ERROR_OK)
2040  return ERROR_FAIL;
2041  result = riscv_program_exec(&program, target);
2042  if (result != ERROR_OK)
2043  break;
2044  }
2045 
2046  if (cleanup_after_vector_access(target, vtype, vl) != ERROR_OK)
2047  return ERROR_FAIL;
2048 
2049  if (cleanup_after_register_access(target, mstatus, regno) != ERROR_OK)
2050  return ERROR_FAIL;
2052  return ERROR_FAIL;
2053 
2054  return result;
2055 }
2056 
2057 static uint32_t sb_sbaccess(unsigned int size_bytes)
2058 {
2059  switch (size_bytes) {
2060  case 1:
2061  return set_field(0, DM_SBCS_SBACCESS, 0);
2062  case 2:
2063  return set_field(0, DM_SBCS_SBACCESS, 1);
2064  case 4:
2065  return set_field(0, DM_SBCS_SBACCESS, 2);
2066  case 8:
2067  return set_field(0, DM_SBCS_SBACCESS, 3);
2068  case 16:
2069  return set_field(0, DM_SBCS_SBACCESS, 4);
2070  }
2071  assert(0);
2072  return 0;
2073 }
2074 
2075 static int sb_write_address(struct target *target, target_addr_t address,
2076  bool ensure_success)
2077 {
2079  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2080  /* There currently is no support for >64-bit addresses in OpenOCD. */
2081  if (sbasize > 96)
2082  dmi_op(target, NULL, NULL, DMI_OP_WRITE, DM_SBADDRESS3, 0, false, false);
2083  if (sbasize > 64)
2084  dmi_op(target, NULL, NULL, DMI_OP_WRITE, DM_SBADDRESS2, 0, false, false);
2085  if (sbasize > 32)
2086  dmi_op(target, NULL, NULL, DMI_OP_WRITE, DM_SBADDRESS1, address >> 32, false, false);
2087  return dmi_op(target, NULL, NULL, DMI_OP_WRITE, DM_SBADDRESS0, address,
2088  false, ensure_success);
2089 }
2090 
2091 static int batch_run(const struct target *target, struct riscv_batch *batch)
2092 {
2094  RISCV_INFO(r);
2095  if (r->reset_delays_wait >= 0) {
2096  r->reset_delays_wait -= batch->used_scans;
2097  if (r->reset_delays_wait <= 0) {
2098  batch->idle_count = 0;
2099  info->dmi_busy_delay = 0;
2100  info->ac_busy_delay = 0;
2101  }
2102  }
2103  return riscv_batch_run(batch);
2104 }
2105 
2106 static int sba_supports_access(struct target *target, unsigned int size_bytes)
2107 {
2109  switch (size_bytes) {
2110  case 1:
2111  return get_field(info->sbcs, DM_SBCS_SBACCESS8);
2112  case 2:
2113  return get_field(info->sbcs, DM_SBCS_SBACCESS16);
2114  case 4:
2115  return get_field(info->sbcs, DM_SBCS_SBACCESS32);
2116  case 8:
2117  return get_field(info->sbcs, DM_SBCS_SBACCESS64);
2118  case 16:
2119  return get_field(info->sbcs, DM_SBCS_SBACCESS128);
2120  default:
2121  return 0;
2122  }
2123 }
2124 
2126  struct riscv_sample_buf *buf,
2128  int64_t until_ms)
2129 {
2131  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2132  if (sbasize > 64) {
2133  LOG_ERROR("Memory sampling is only implemented for sbasize <= 64.");
2134  return ERROR_NOT_IMPLEMENTED;
2135  }
2136 
2137  if (get_field(info->sbcs, DM_SBCS_SBVERSION) != 1) {
2138  LOG_ERROR("Memory sampling is only implemented for SBA version 1.");
2139  return ERROR_NOT_IMPLEMENTED;
2140  }
2141 
2142  uint32_t sbcs = 0;
2143  uint32_t sbcs_valid = false;
2144 
2145  uint32_t sbaddress0 = 0;
2146  bool sbaddress0_valid = false;
2147  uint32_t sbaddress1 = 0;
2148  bool sbaddress1_valid = false;
2149 
2150  /* How often to read each value in a batch. */
2151  const unsigned int repeat = 5;
2152 
2153  unsigned int enabled_count = 0;
2154  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2155  if (config->bucket[i].enabled)
2156  enabled_count++;
2157  }
2158 
2159  while (timeval_ms() < until_ms) {
2160  /*
2161  * batch_run() adds to the batch, so we can't simply reuse the same
2162  * batch over and over. So we create a new one every time through the
2163  * loop.
2164  */
2165  struct riscv_batch *batch = riscv_batch_alloc(
2166  target, 1 + enabled_count * 5 * repeat,
2167  info->dmi_busy_delay + info->bus_master_read_delay);
2168  if (!batch)
2169  return ERROR_FAIL;
2170 
2171  unsigned int result_bytes = 0;
2172  for (unsigned int n = 0; n < repeat; n++) {
2173  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2174  if (config->bucket[i].enabled) {
2175  if (!sba_supports_access(target, config->bucket[i].size_bytes)) {
2176  LOG_ERROR("Hardware does not support SBA access for %d-byte memory sampling.",
2177  config->bucket[i].size_bytes);
2178  return ERROR_NOT_IMPLEMENTED;
2179  }
2180 
2181  uint32_t sbcs_write = DM_SBCS_SBREADONADDR;
2182  if (enabled_count == 1)
2183  sbcs_write |= DM_SBCS_SBREADONDATA;
2184  sbcs_write |= sb_sbaccess(config->bucket[i].size_bytes);
2185  if (!sbcs_valid || sbcs_write != sbcs) {
2186  riscv_batch_add_dmi_write(batch, DM_SBCS, sbcs_write);
2187  sbcs = sbcs_write;
2188  sbcs_valid = true;
2189  }
2190 
2191  if (sbasize > 32 &&
2192  (!sbaddress1_valid ||
2193  sbaddress1 != config->bucket[i].address >> 32)) {
2194  sbaddress1 = config->bucket[i].address >> 32;
2195  riscv_batch_add_dmi_write(batch, DM_SBADDRESS1, sbaddress1);
2196  sbaddress1_valid = true;
2197  }
2198  if (!sbaddress0_valid ||
2199  sbaddress0 != (config->bucket[i].address & 0xffffffff)) {
2200  sbaddress0 = config->bucket[i].address;
2201  riscv_batch_add_dmi_write(batch, DM_SBADDRESS0, sbaddress0);
2202  sbaddress0_valid = true;
2203  }
2204  if (config->bucket[i].size_bytes > 4)
2207  result_bytes += 1 + config->bucket[i].size_bytes;
2208  }
2209  }
2210  }
2211 
2212  if (buf->used + result_bytes >= buf->size) {
2213  riscv_batch_free(batch);
2214  break;
2215  }
2216 
2217  size_t sbcs_key = riscv_batch_add_dmi_read(batch, DM_SBCS);
2218 
2219  int result = batch_run(target, batch);
2220  if (result != ERROR_OK)
2221  return result;
2222 
2223  uint32_t sbcs_read = riscv_batch_get_dmi_read_data(batch, sbcs_key);
2224  if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
2225  /* Discard this batch (too much hassle to try to recover partial
2226  * data) and try again with a larger delay. */
2227  info->bus_master_read_delay += info->bus_master_read_delay / 10 + 1;
2229  riscv_batch_free(batch);
2230  continue;
2231  }
2232  if (get_field(sbcs_read, DM_SBCS_SBERROR)) {
2233  /* The memory we're sampling was unreadable, somehow. Give up. */
2235  riscv_batch_free(batch);
2236  return ERROR_FAIL;
2237  }
2238 
2239  unsigned int read = 0;
2240  for (unsigned int n = 0; n < repeat; n++) {
2241  for (unsigned int i = 0; i < ARRAY_SIZE(config->bucket); i++) {
2242  if (config->bucket[i].enabled) {
2244  uint64_t value = 0;
2245  if (config->bucket[i].size_bytes > 4)
2246  value = ((uint64_t)riscv_batch_get_dmi_read_data(batch, read++)) << 32;
2247  value |= riscv_batch_get_dmi_read_data(batch, read++);
2248 
2249  buf->buf[buf->used] = i;
2250  buf_set_u64(buf->buf + buf->used + 1, 0, config->bucket[i].size_bytes * 8, value);
2251  buf->used += 1 + config->bucket[i].size_bytes;
2252  }
2253  }
2254  }
2255 
2256  riscv_batch_free(batch);
2257  }
2258 
2259  return ERROR_OK;
2260 }
2261 
2262 static int sample_memory(struct target *target,
2263  struct riscv_sample_buf *buf,
2265  int64_t until_ms)
2266 {
2267  if (!config->enabled)
2268  return ERROR_OK;
2269 
2270  return sample_memory_bus_v1(target, buf, config, until_ms);
2271 }
2272 
2273 static int init_target(struct command_context *cmd_ctx,
2274  struct target *target)
2275 {
2276  LOG_DEBUG("init");
2277  RISCV_INFO(generic_info);
2278 
2279  generic_info->get_register = &riscv013_get_register;
2280  generic_info->set_register = &riscv013_set_register;
2281  generic_info->get_register_buf = &riscv013_get_register_buf;
2282  generic_info->set_register_buf = &riscv013_set_register_buf;
2283  generic_info->select_current_hart = &riscv013_select_current_hart;
2284  generic_info->is_halted = &riscv013_is_halted;
2285  generic_info->resume_go = &riscv013_resume_go;
2286  generic_info->step_current_hart = &riscv013_step_current_hart;
2287  generic_info->on_halt = &riscv013_on_halt;
2288  generic_info->resume_prep = &riscv013_resume_prep;
2289  generic_info->halt_prep = &riscv013_halt_prep;
2290  generic_info->halt_go = &riscv013_halt_go;
2291  generic_info->on_step = &riscv013_on_step;
2292  generic_info->halt_reason = &riscv013_halt_reason;
2293  generic_info->read_debug_buffer = &riscv013_read_debug_buffer;
2294  generic_info->write_debug_buffer = &riscv013_write_debug_buffer;
2295  generic_info->execute_debug_buffer = &riscv013_execute_debug_buffer;
2296  generic_info->fill_dmi_write_u64 = &riscv013_fill_dmi_write_u64;
2297  generic_info->fill_dmi_read_u64 = &riscv013_fill_dmi_read_u64;
2298  generic_info->fill_dmi_nop_u64 = &riscv013_fill_dmi_nop_u64;
2299  generic_info->dmi_write_u64_bits = &riscv013_dmi_write_u64_bits;
2300  generic_info->authdata_read = &riscv013_authdata_read;
2301  generic_info->authdata_write = &riscv013_authdata_write;
2302  generic_info->dmi_read = &dmi_read;
2303  generic_info->dmi_write = &dmi_write;
2304  generic_info->read_memory = read_memory;
2305  generic_info->hart_count = &riscv013_hart_count;
2306  generic_info->data_bits = &riscv013_data_bits;
2307  generic_info->print_info = &riscv013_print_info;
2308  if (!generic_info->version_specific) {
2309  generic_info->version_specific = calloc(1, sizeof(riscv013_info_t));
2310  if (!generic_info->version_specific)
2311  return ERROR_FAIL;
2312  }
2313  generic_info->sample_memory = sample_memory;
2315 
2316  info->progbufsize = -1;
2317 
2318  info->dmi_busy_delay = 0;
2319  info->bus_master_read_delay = 0;
2320  info->bus_master_write_delay = 0;
2321  info->ac_busy_delay = 0;
2322 
2323  /* Assume all these abstract commands are supported until we learn
2324  * otherwise.
2325  * TODO: The spec allows eg. one CSR to be able to be accessed abstractly
2326  * while another one isn't. We don't track that this closely here, but in
2327  * the future we probably should. */
2328  info->abstract_read_csr_supported = true;
2329  info->abstract_write_csr_supported = true;
2330  info->abstract_read_fpr_supported = true;
2331  info->abstract_write_fpr_supported = true;
2332 
2333  info->has_aampostincrement = YNM_MAYBE;
2334 
2335  return ERROR_OK;
2336 }
2337 
2338 static int assert_reset(struct target *target)
2339 {
2340  RISCV_INFO(r);
2341 
2342  select_dmi(target);
2343 
2344  uint32_t control_base = set_field(0, DM_DMCONTROL_DMACTIVE, 1);
2345 
2347  /* Run the user-supplied script if there is one. */
2349  } else if (target->rtos) {
2350  /* There's only one target, and OpenOCD thinks each hart is a thread.
2351  * We must reset them all. */
2352 
2353  /* TODO: Try to use hasel in dmcontrol */
2354 
2355  /* Set haltreq for each hart. */
2356  uint32_t control = set_hartsel(control_base, target->coreid);
2357  control = set_field(control, DM_DMCONTROL_HALTREQ,
2358  target->reset_halt ? 1 : 0);
2359  dmi_write(target, DM_DMCONTROL, control);
2360 
2361  /* Assert ndmreset */
2362  control = set_field(control, DM_DMCONTROL_NDMRESET, 1);
2363  dmi_write(target, DM_DMCONTROL, control);
2364 
2365  } else {
2366  /* Reset just this hart. */
2367  uint32_t control = set_hartsel(control_base, r->current_hartid);
2368  control = set_field(control, DM_DMCONTROL_HALTREQ,
2369  target->reset_halt ? 1 : 0);
2370  control = set_field(control, DM_DMCONTROL_NDMRESET, 1);
2371  dmi_write(target, DM_DMCONTROL, control);
2372  }
2373 
2375 
2376  dm013_info_t *dm = get_dm(target);
2377  if (!dm)
2378  return ERROR_FAIL;
2379 
2380  /* The DM might have gotten reset if OpenOCD called us in some reset that
2381  * involves SRST being toggled. So clear our cache which may be out of
2382  * date. */
2383  memset(dm->progbuf_cache, 0, sizeof(dm->progbuf_cache));
2384 
2385  return ERROR_OK;
2386 }
2387 
2388 static int deassert_reset(struct target *target)
2389 {
2390  RISCV_INFO(r);
2392  select_dmi(target);
2393 
2394  /* Clear the reset, but make sure haltreq is still set */
2395  uint32_t control = 0, control_haltreq;
2396  control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
2397  control_haltreq = set_field(control, DM_DMCONTROL_HALTREQ, target->reset_halt ? 1 : 0);
2399  set_hartsel(control_haltreq, r->current_hartid));
2400 
2401  uint32_t dmstatus;
2402  int dmi_busy_delay = info->dmi_busy_delay;
2403  time_t start = time(NULL);
2404 
2405  for (int i = 0; i < riscv_count_harts(target); ++i) {
2406  int index = i;
2407  if (target->rtos) {
2408  if (index != target->coreid)
2409  continue;
2411  set_hartsel(control_haltreq, index));
2412  } else {
2413  index = r->current_hartid;
2414  }
2415 
2416  LOG_DEBUG("Waiting for hart %d to come out of reset.", index);
2417  while (1) {
2418  int result = dmstatus_read_timeout(target, &dmstatus, true,
2420  if (result == ERROR_TIMEOUT_REACHED)
2421  LOG_ERROR("Hart %d didn't complete a DMI read coming out of "
2422  "reset in %ds; Increase the timeout with riscv "
2423  "set_reset_timeout_sec.",
2424  index, riscv_reset_timeout_sec);
2425  if (result != ERROR_OK)
2426  return result;
2427  /* Certain debug modules, like the one in GD32VF103
2428  * MCUs, violate the specification's requirement that
2429  * each hart is in "exactly one of four states" and,
2430  * during reset, report harts as both unavailable and
2431  * halted/running. To work around this, we check for
2432  * the absence of the unavailable state rather than
2433  * the presence of any other state. */
2434  if (!get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL))
2435  break;
2436  if (time(NULL) - start > riscv_reset_timeout_sec) {
2437  LOG_ERROR("Hart %d didn't leave reset in %ds; "
2438  "dmstatus=0x%x; "
2439  "Increase the timeout with riscv set_reset_timeout_sec.",
2440  index, riscv_reset_timeout_sec, dmstatus);
2441  return ERROR_FAIL;
2442  }
2443  }
2445 
2446  if (get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET)) {
2447  /* Ack reset and clear DM_DMCONTROL_HALTREQ if previously set */
2449  set_hartsel(control, index) |
2451  }
2452 
2453  if (!target->rtos)
2454  break;
2455  }
2456  info->dmi_busy_delay = dmi_busy_delay;
2457  return ERROR_OK;
2458 }
2459 
2460 static int execute_fence(struct target *target)
2461 {
2462  /* FIXME: For non-coherent systems we need to flush the caches right
2463  * here, but there's no ISA-defined way of doing that. */
2464  {
2465  struct riscv_program program;
2466  riscv_program_init(&program, target);
2467  riscv_program_fence_i(&program);
2468  riscv_program_fence(&program);
2469  int result = riscv_program_exec(&program, target);
2470  if (result != ERROR_OK)
2471  LOG_DEBUG("Unable to execute pre-fence");
2472  }
2473 
2474  return ERROR_OK;
2475 }
2476 
2477 static void log_memory_access(target_addr_t address, uint64_t value,
2478  unsigned int size_bytes, bool read)
2479 {
2480  if (debug_level < LOG_LVL_DEBUG)
2481  return;
2482 
2483  char fmt[80];
2484  sprintf(fmt, "M[0x%" TARGET_PRIxADDR "] %ss 0x%%0%d" PRIx64,
2485  address, read ? "read" : "write", size_bytes * 2);
2486  switch (size_bytes) {
2487  case 1:
2488  value &= 0xff;
2489  break;
2490  case 2:
2491  value &= 0xffff;
2492  break;
2493  case 4:
2494  value &= 0xffffffffUL;
2495  break;
2496  case 8:
2497  break;
2498  default:
2499  assert(false);
2500  }
2501  LOG_DEBUG(fmt, value);
2502 }
2503 
2504 /* Read the relevant sbdata regs depending on size, and put the results into
2505  * buffer. */
2506 static int read_memory_bus_word(struct target *target, target_addr_t address,
2507  uint32_t size, uint8_t *buffer)
2508 {
2509  uint32_t value;
2510  int result;
2511  static int sbdata[4] = { DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3 };
2512  assert(size <= 16);
2513  for (int i = (size - 1) / 4; i >= 0; i--) {
2514  result = dmi_op(target, &value, NULL, DMI_OP_READ, sbdata[i], 0, false, true);
2515  if (result != ERROR_OK)
2516  return result;
2517  buf_set_u32(buffer + i * 4, 0, 8 * MIN(size, 4), value);
2518  log_memory_access(address + i * 4, value, MIN(size, 4), true);
2519  }
2520  return ERROR_OK;
2521 }
2522 
2524 {
2526  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2527  target_addr_t address = 0;
2528  uint32_t v;
2529  if (sbasize > 32) {
2531  address |= v;
2532  address <<= 32;
2533  }
2535  address |= v;
2536  return address;
2537 }
2538 
2539 static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
2540 {
2541  time_t start = time(NULL);
2542  while (1) {
2543  if (dmi_read(target, sbcs, DM_SBCS) != ERROR_OK)
2544  return ERROR_FAIL;
2545  if (!get_field(*sbcs, DM_SBCS_SBBUSY))
2546  return ERROR_OK;
2547  if (time(NULL) - start > riscv_command_timeout_sec) {
2548  LOG_ERROR("Timed out after %ds waiting for sbbusy to go low (sbcs=0x%x). "
2549  "Increase the timeout with riscv set_command_timeout_sec.",
2550  riscv_command_timeout_sec, *sbcs);
2551  return ERROR_FAIL;
2552  }
2553  }
2554 }
2555 
2556 static int modify_privilege(struct target *target, uint64_t *mstatus, uint64_t *mstatus_old)
2557 {
2559  /* Read DCSR */
2560  uint64_t dcsr;
2561  if (register_read(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
2562  return ERROR_FAIL;
2563 
2564  /* Read and save MSTATUS */
2565  if (register_read(target, mstatus, GDB_REGNO_MSTATUS) != ERROR_OK)
2566  return ERROR_FAIL;
2567  *mstatus_old = *mstatus;
2568 
2569  /* If we come from m-mode with mprv set, we want to keep mpp */
2570  if (get_field(dcsr, DCSR_PRV) < 3) {
2571  /* MPP = PRIV */
2572  *mstatus = set_field(*mstatus, MSTATUS_MPP, get_field(dcsr, DCSR_PRV));
2573 
2574  /* MPRV = 1 */
2575  *mstatus = set_field(*mstatus, MSTATUS_MPRV, 1);
2576 
2577  /* Write MSTATUS */
2578  if (*mstatus != *mstatus_old)
2580  return ERROR_FAIL;
2581  }
2582  }
2583 
2584  return ERROR_OK;
2585 }
2586 
2587 static int read_memory_bus_v0(struct target *target, target_addr_t address,
2588  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
2589 {
2590  if (size != increment) {
2591  LOG_ERROR("sba v0 reads only support size==increment");
2592  return ERROR_NOT_IMPLEMENTED;
2593  }
2594 
2595  LOG_DEBUG("System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
2596  TARGET_PRIxADDR, size, count, address);
2597  uint8_t *t_buffer = buffer;
2598  riscv_addr_t cur_addr = address;
2599  riscv_addr_t fin_addr = address + (count * size);
2600  uint32_t access = 0;
2601 
2602  const int DM_SBCS_SBSINGLEREAD_OFFSET = 20;
2603  const uint32_t DM_SBCS_SBSINGLEREAD = (0x1U << DM_SBCS_SBSINGLEREAD_OFFSET);
2604 
2605  const int DM_SBCS_SBAUTOREAD_OFFSET = 15;
2606  const uint32_t DM_SBCS_SBAUTOREAD = (0x1U << DM_SBCS_SBAUTOREAD_OFFSET);
2607 
2608  /* ww favorise one off reading if there is an issue */
2609  if (count == 1) {
2610  for (uint32_t i = 0; i < count; i++) {
2611  if (dmi_read(target, &access, DM_SBCS) != ERROR_OK)
2612  return ERROR_FAIL;
2613  dmi_write(target, DM_SBADDRESS0, cur_addr);
2614  /* size/2 matching the bit access of the spec 0.13 */
2615  access = set_field(access, DM_SBCS_SBACCESS, size/2);
2616  access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
2617  LOG_DEBUG("\r\nread_memory: sab: access: 0x%08x", access);
2618  dmi_write(target, DM_SBCS, access);
2619  /* 3) read */
2620  uint32_t value;
2621  if (dmi_read(target, &value, DM_SBDATA0) != ERROR_OK)
2622  return ERROR_FAIL;
2623  LOG_DEBUG("\r\nread_memory: sab: value: 0x%08x", value);
2624  buf_set_u32(t_buffer, 0, 8 * size, value);
2625  t_buffer += size;
2626  cur_addr += size;
2627  }
2628  return ERROR_OK;
2629  }
2630 
2631  /* has to be the same size if we want to read a block */
2632  LOG_DEBUG("reading block until final address 0x%" PRIx64, fin_addr);
2633  if (dmi_read(target, &access, DM_SBCS) != ERROR_OK)
2634  return ERROR_FAIL;
2635  /* set current address */
2636  dmi_write(target, DM_SBADDRESS0, cur_addr);
2637  /* 2) write sbaccess=2, sbsingleread,sbautoread,sbautoincrement
2638  * size/2 matching the bit access of the spec 0.13 */
2639  access = set_field(access, DM_SBCS_SBACCESS, size/2);
2640  access = set_field(access, DM_SBCS_SBAUTOREAD, 1);
2641  access = set_field(access, DM_SBCS_SBSINGLEREAD, 1);
2642  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
2643  LOG_DEBUG("\r\naccess: 0x%08x", access);
2644  dmi_write(target, DM_SBCS, access);
2645 
2646  while (cur_addr < fin_addr) {
2647  LOG_DEBUG("\r\nsab:autoincrement: \r\n size: %d\tcount:%d\taddress: 0x%08"
2648  PRIx64, size, count, cur_addr);
2649  /* read */
2650  uint32_t value;
2651  if (dmi_read(target, &value, DM_SBDATA0) != ERROR_OK)
2652  return ERROR_FAIL;
2653  buf_set_u32(t_buffer, 0, 8 * size, value);
2654  cur_addr += size;
2655  t_buffer += size;
2656 
2657  /* if we are reaching last address, we must clear autoread */
2658  if (cur_addr == fin_addr && count != 1) {
2659  dmi_write(target, DM_SBCS, 0);
2660  if (dmi_read(target, &value, DM_SBDATA0) != ERROR_OK)
2661  return ERROR_FAIL;
2662  buf_set_u32(t_buffer, 0, 8 * size, value);
2663  }
2664  }
2665 
2666  uint32_t sbcs;
2667  if (dmi_read(target, &sbcs, DM_SBCS) != ERROR_OK)
2668  return ERROR_FAIL;
2669 
2670  return ERROR_OK;
2671 }
2672 
2676 static int read_memory_bus_v1(struct target *target, target_addr_t address,
2677  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
2678 {
2679  if (increment != size && increment != 0) {
2680  LOG_ERROR("sba v1 reads only support increment of size or 0");
2681  return ERROR_NOT_IMPLEMENTED;
2682  }
2683 
2685  target_addr_t next_address = address;
2686  target_addr_t end_address = address + count * size;
2687 
2688  while (next_address < end_address) {
2689  uint32_t sbcs_write = set_field(0, DM_SBCS_SBREADONADDR, 1);
2690  sbcs_write |= sb_sbaccess(size);
2691  if (increment == size)
2692  sbcs_write = set_field(sbcs_write, DM_SBCS_SBAUTOINCREMENT, 1);
2693  if (count > 1)
2694  sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, count > 1);
2695  if (dmi_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
2696  return ERROR_FAIL;
2697 
2698  /* This address write will trigger the first read. */
2699  if (sb_write_address(target, next_address, true) != ERROR_OK)
2700  return ERROR_FAIL;
2701 
2702  if (info->bus_master_read_delay) {
2703  jtag_add_runtest(info->bus_master_read_delay, TAP_IDLE);
2704  if (jtag_execute_queue() != ERROR_OK) {
2705  LOG_ERROR("Failed to scan idle sequence");
2706  return ERROR_FAIL;
2707  }
2708  }
2709 
2710  /* First value has been read, and is waiting for us to issue a DMI read
2711  * to get it. */
2712 
2713  static int sbdata[4] = {DM_SBDATA0, DM_SBDATA1, DM_SBDATA2, DM_SBDATA3};
2714  assert(size <= 16);
2715  target_addr_t next_read = address - 1;
2716  for (uint32_t i = (next_address - address) / size; i < count - 1; i++) {
2717  for (int j = (size - 1) / 4; j >= 0; j--) {
2718  uint32_t value;
2719  unsigned int attempt = 0;
2720  while (1) {
2721  if (attempt++ > 100) {
2722  LOG_ERROR("DMI keeps being busy in while reading memory just past " TARGET_ADDR_FMT,
2723  next_read);
2724  return ERROR_FAIL;
2725  }
2726  keep_alive();
2727  dmi_status_t status = dmi_scan(target, NULL, &value,
2728  DMI_OP_READ, sbdata[j], 0, false);
2729  if (status == DMI_STATUS_BUSY)
2731  else if (status == DMI_STATUS_SUCCESS)
2732  break;
2733  else
2734  return ERROR_FAIL;
2735  }
2736  if (next_read != address - 1) {
2737  buf_set_u32(buffer + next_read - address, 0, 8 * MIN(size, 4), value);
2738  log_memory_access(next_read, value, MIN(size, 4), true);
2739  }
2740  next_read = address + i * size + j * 4;
2741  }
2742  }
2743 
2744  uint32_t sbcs_read = 0;
2745  if (count > 1) {
2746  uint32_t value;
2747  unsigned int attempt = 0;
2748  while (1) {
2749  if (attempt++ > 100) {
2750  LOG_ERROR("DMI keeps being busy in while reading memory just past " TARGET_ADDR_FMT,
2751  next_read);
2752  return ERROR_FAIL;
2753  }
2754  dmi_status_t status = dmi_scan(target, NULL, &value, DMI_OP_NOP, 0, 0, false);
2755  if (status == DMI_STATUS_BUSY)
2757  else if (status == DMI_STATUS_SUCCESS)
2758  break;
2759  else
2760  return ERROR_FAIL;
2761  }
2762  buf_set_u32(buffer + next_read - address, 0, 8 * MIN(size, 4), value);
2763  log_memory_access(next_read, value, MIN(size, 4), true);
2764 
2765  /* "Writes to sbcs while sbbusy is high result in undefined behavior.
2766  * A debugger must not write to sbcs until it reads sbbusy as 0." */
2767  if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
2768  return ERROR_FAIL;
2769 
2770  sbcs_write = set_field(sbcs_write, DM_SBCS_SBREADONDATA, 0);
2771  if (dmi_write(target, DM_SBCS, sbcs_write) != ERROR_OK)
2772  return ERROR_FAIL;
2773  }
2774 
2775  /* Read the last word, after we disabled sbreadondata if necessary. */
2776  if (!get_field(sbcs_read, DM_SBCS_SBERROR) &&
2777  !get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
2778  if (read_memory_bus_word(target, address + (count - 1) * size, size,
2779  buffer + (count - 1) * size) != ERROR_OK)
2780  return ERROR_FAIL;
2781 
2782  if (read_sbcs_nonbusy(target, &sbcs_read) != ERROR_OK)
2783  return ERROR_FAIL;
2784  }
2785 
2786  if (get_field(sbcs_read, DM_SBCS_SBBUSYERROR)) {
2787  /* We read while the target was busy. Slow down and try again. */
2788  if (dmi_write(target, DM_SBCS, sbcs_read | DM_SBCS_SBBUSYERROR) != ERROR_OK)
2789  return ERROR_FAIL;
2790  next_address = sb_read_address(target);
2791  info->bus_master_read_delay += info->bus_master_read_delay / 10 + 1;
2792  continue;
2793  }
2794 
2795  unsigned int error = get_field(sbcs_read, DM_SBCS_SBERROR);
2796  if (error == 0) {
2797  next_address = end_address;
2798  } else {
2799  /* Some error indicating the bus access failed, but not because of
2800  * something we did wrong. */
2802  return ERROR_FAIL;
2803  return ERROR_FAIL;
2804  }
2805  }
2806 
2807  return ERROR_OK;
2808 }
2809 
2810 static void log_mem_access_result(struct target *target, bool success, int method, bool read)
2811 {
2812  RISCV_INFO(r);
2813  bool warn = false;
2814  char msg[60];
2815 
2816  /* Compose the message */
2817  snprintf(msg, 60, "%s to %s memory via %s.",
2818  success ? "Succeeded" : "Failed",
2819  read ? "read" : "write",
2820  (method == RISCV_MEM_ACCESS_PROGBUF) ? "program buffer" :
2821  (method == RISCV_MEM_ACCESS_SYSBUS) ? "system bus" : "abstract access");
2822 
2823  /* Determine the log message severity. Show warnings only once. */
2824  if (!success) {
2825  if (method == RISCV_MEM_ACCESS_PROGBUF) {
2826  warn = r->mem_access_progbuf_warn;
2827  r->mem_access_progbuf_warn = false;
2828  }
2829  if (method == RISCV_MEM_ACCESS_SYSBUS) {
2830  warn = r->mem_access_sysbus_warn;
2831  r->mem_access_sysbus_warn = false;
2832  }
2833  if (method == RISCV_MEM_ACCESS_ABSTRACT) {
2834  warn = r->mem_access_abstract_warn;
2835  r->mem_access_abstract_warn = false;
2836  }
2837  }
2838 
2839  if (warn)
2840  LOG_WARNING("%s", msg);
2841  else
2842  LOG_DEBUG("%s", msg);
2843 }
2844 
2846  uint32_t size, bool read, char **skip_reason)
2847 {
2848  assert(skip_reason);
2849 
2850  if (!has_sufficient_progbuf(target, 3)) {
2851  LOG_DEBUG("Skipping mem %s via progbuf - insufficient progbuf size.",
2852  read ? "read" : "write");
2853  *skip_reason = "skipped (insufficient progbuf)";
2854  return true;
2855  }
2856  if (target->state != TARGET_HALTED) {
2857  LOG_DEBUG("Skipping mem %s via progbuf - target not halted.",
2858  read ? "read" : "write");
2859  *skip_reason = "skipped (target not halted)";
2860  return true;
2861  }
2862  if (riscv_xlen(target) < size * 8) {
2863  LOG_DEBUG("Skipping mem %s via progbuf - XLEN (%d) is too short for %d-bit memory access.",
2864  read ? "read" : "write", riscv_xlen(target), size * 8);
2865  *skip_reason = "skipped (XLEN too short)";
2866  return true;
2867  }
2868  if (size > 8) {
2869  LOG_DEBUG("Skipping mem %s via progbuf - unsupported size.",
2870  read ? "read" : "write");
2871  *skip_reason = "skipped (unsupported size)";
2872  return true;
2873  }
2874  if ((sizeof(address) * 8 > riscv_xlen(target)) && (address >> riscv_xlen(target))) {
2875  LOG_DEBUG("Skipping mem %s via progbuf - progbuf only supports %u-bit address.",
2876  read ? "read" : "write", riscv_xlen(target));
2877  *skip_reason = "skipped (too large address)";
2878  return true;
2879  }
2880 
2881  return false;
2882 }
2883 
2884 static bool mem_should_skip_sysbus(struct target *target, target_addr_t address,
2885  uint32_t size, uint32_t increment, bool read, char **skip_reason)
2886 {
2887  assert(skip_reason);
2888 
2890  if (!sba_supports_access(target, size)) {
2891  LOG_DEBUG("Skipping mem %s via system bus - unsupported size.",
2892  read ? "read" : "write");
2893  *skip_reason = "skipped (unsupported size)";
2894  return true;
2895  }
2896  unsigned int sbasize = get_field(info->sbcs, DM_SBCS_SBASIZE);
2897  if ((sizeof(address) * 8 > sbasize) && (address >> sbasize)) {
2898  LOG_DEBUG("Skipping mem %s via system bus - sba only supports %u-bit address.",
2899  read ? "read" : "write", sbasize);
2900  *skip_reason = "skipped (too large address)";
2901  return true;
2902  }
2903  if (read && increment != size && (get_field(info->sbcs, DM_SBCS_SBVERSION) == 0 || increment != 0)) {
2904  LOG_DEBUG("Skipping mem read via system bus - "
2905  "sba reads only support size==increment or also size==0 for sba v1.");
2906  *skip_reason = "skipped (unsupported increment)";
2907  return true;
2908  }
2909 
2910  return false;
2911 }
2912 
2914  uint32_t size, uint32_t increment, bool read, char **skip_reason)
2915 {
2916  assert(skip_reason);
2917 
2918  if (size > 8) {
2919  /* TODO: Add 128b support if it's ever used. Involves modifying
2920  read/write_abstract_arg() to work on two 64b values. */
2921  LOG_DEBUG("Skipping mem %s via abstract access - unsupported size: %d bits",
2922  read ? "read" : "write", size * 8);
2923  *skip_reason = "skipped (unsupported size)";
2924  return true;
2925  }
2926  if ((sizeof(address) * 8 > riscv_xlen(target)) && (address >> riscv_xlen(target))) {
2927  LOG_DEBUG("Skipping mem %s via abstract access - abstract access only supports %u-bit address.",
2928  read ? "read" : "write", riscv_xlen(target));
2929  *skip_reason = "skipped (too large address)";
2930  return true;
2931  }
2932  if (read && size != increment) {
2933  LOG_ERROR("Skipping mem read via abstract access - "
2934  "abstract command reads only support size==increment.");
2935  *skip_reason = "skipped (unsupported increment)";
2936  return true;
2937  }
2938 
2939  return false;
2940 }
2941 
2942 /*
2943  * Performs a memory read using memory access abstract commands. The read sizes
2944  * supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16 byte
2945  * aamsize fields in the memory access abstract command.
2946  */
2947 static int read_memory_abstract(struct target *target, target_addr_t address,
2948  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
2949 {
2951 
2952  int result = ERROR_OK;
2953  bool use_aampostincrement = info->has_aampostincrement != YNM_NO;
2954 
2955  LOG_DEBUG("reading %d words of %d bytes from 0x%" TARGET_PRIxADDR, count,
2956  size, address);
2957 
2958  memset(buffer, 0, count * size);
2959 
2960  /* Convert the size (bytes) to width (bits) */
2961  unsigned int width = size << 3;
2962 
2963  /* Create the command (physical address, postincrement, read) */
2964  uint32_t command = access_memory_command(target, false, width, use_aampostincrement, false);
2965 
2966  /* Execute the reads */
2967  uint8_t *p = buffer;
2968  bool updateaddr = true;
2969  unsigned int width32 = (width < 32) ? 32 : width;
2970  for (uint32_t c = 0; c < count; c++) {
2971  /* Update the address if it is the first time or aampostincrement is not supported by the target. */
2972  if (updateaddr) {
2973  /* Set arg1 to the address: address + c * size */
2974  result = write_abstract_arg(target, 1, address + c * size, riscv_xlen(target));
2975  if (result != ERROR_OK) {
2976  LOG_ERROR("Failed to write arg1 during read_memory_abstract().");
2977  return result;
2978  }
2979  }
2980 
2981  /* Execute the command */
2983 
2984  if (info->has_aampostincrement == YNM_MAYBE) {
2985  if (result == ERROR_OK) {
2986  /* Safety: double-check that the address was really auto-incremented */
2988  if (new_address == address + size) {
2989  LOG_DEBUG("aampostincrement is supported on this target.");
2990  info->has_aampostincrement = YNM_YES;
2991  } else {
2992  LOG_WARNING("Buggy aampostincrement! Address not incremented correctly.");
2993  info->has_aampostincrement = YNM_NO;
2994  }
2995  } else {
2996  /* Try the same access but with postincrement disabled. */
2997  command = access_memory_command(target, false, width, false, false);
2999  if (result == ERROR_OK) {
3000  LOG_DEBUG("aampostincrement is not supported on this target.");
3001  info->has_aampostincrement = YNM_NO;
3002  }
3003  }
3004  }
3005 
3006  if (result != ERROR_OK)
3007  return result;
3008 
3009  /* Copy arg0 to buffer (rounded width up to nearest 32) */
3010  riscv_reg_t value = read_abstract_arg(target, 0, width32);
3011  buf_set_u64(p, 0, 8 * size, value);
3012 
3013  if (info->has_aampostincrement == YNM_YES)
3014  updateaddr = false;
3015  p += size;
3016  }
3017 
3018  return result;
3019 }
3020 
3021 /*
3022  * Performs a memory write using memory access abstract commands. The write
3023  * sizes supported are 1, 2, and 4 bytes despite the spec's support of 8 and 16
3024  * byte aamsize fields in the memory access abstract command.
3025  */
3026 static int write_memory_abstract(struct target *target, target_addr_t address,
3027  uint32_t size, uint32_t count, const uint8_t *buffer)
3028 {
3030  int result = ERROR_OK;
3031  bool use_aampostincrement = info->has_aampostincrement != YNM_NO;
3032 
3033  LOG_DEBUG("writing %d words of %d bytes from 0x%" TARGET_PRIxADDR, count,
3034  size, address);
3035 
3036  /* Convert the size (bytes) to width (bits) */
3037  unsigned int width = size << 3;
3038 
3039  /* Create the command (physical address, postincrement, write) */
3040  uint32_t command = access_memory_command(target, false, width, use_aampostincrement, true);
3041 
3042  /* Execute the writes */
3043  const uint8_t *p = buffer;
3044  bool updateaddr = true;
3045  for (uint32_t c = 0; c < count; c++) {
3046  /* Move data to arg0 */
3047  riscv_reg_t value = buf_get_u64(p, 0, 8 * size);
3048  result = write_abstract_arg(target, 0, value, riscv_xlen(target));
3049  if (result != ERROR_OK) {
3050  LOG_ERROR("Failed to write arg0 during write_memory_abstract().");
3051  return result;
3052  }
3053 
3054  /* Update the address if it is the first time or aampostincrement is not supported by the target. */
3055  if (updateaddr) {
3056  /* Set arg1 to the address: address + c * size */
3057  result = write_abstract_arg(target, 1, address + c * size, riscv_xlen(target));
3058  if (result != ERROR_OK) {
3059  LOG_ERROR("Failed to write arg1 during write_memory_abstract().");
3060  return result;
3061  }
3062  }
3063 
3064  /* Execute the command */
3066 
3067  if (info->has_aampostincrement == YNM_MAYBE) {
3068  if (result == ERROR_OK) {
3069  /* Safety: double-check that the address was really auto-incremented */
3071  if (new_address == address + size) {
3072  LOG_DEBUG("aampostincrement is supported on this target.");
3073  info->has_aampostincrement = YNM_YES;
3074  } else {
3075  LOG_WARNING("Buggy aampostincrement! Address not incremented correctly.");
3076  info->has_aampostincrement = YNM_NO;
3077  }
3078  } else {
3079  /* Try the same access but with postincrement disabled. */
3080  command = access_memory_command(target, false, width, false, true);
3082  if (result == ERROR_OK) {
3083  LOG_DEBUG("aampostincrement is not supported on this target.");
3084  info->has_aampostincrement = YNM_NO;
3085  }
3086  }
3087  }
3088 
3089  if (result != ERROR_OK)
3090  return result;
3091 
3092  if (info->has_aampostincrement == YNM_YES)
3093  updateaddr = false;
3094  p += size;
3095  }
3096 
3097  return result;
3098 }
3099 
3105  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
3106 {
3108 
3109  int result = ERROR_OK;
3110 
3111  /* Write address to S0. */
3112  result = register_write_direct(target, GDB_REGNO_S0, address);
3113  if (result != ERROR_OK)
3114  return result;
3115 
3116  if (increment == 0 &&
3118  return ERROR_FAIL;
3119 
3121  riscv_xlen(target),
3124  return ERROR_FAIL;
3125 
3126  /* First read has just triggered. Result is in s1. */
3127  if (count == 1) {
3128  uint64_t value;
3130  return ERROR_FAIL;
3131  buf_set_u64(buffer, 0, 8 * size, value);
3132  log_memory_access(address, value, size, true);
3133  return ERROR_OK;
3134  }
3135 
3138  goto error;
3139  /* Read garbage from dmi_data0, which triggers another execution of the
3140  * program. Now dmi_data0 contains the first good result, and s1 the next
3141  * memory value. */
3143  goto error;
3144 
3145  /* read_addr is the next address that the hart will read from, which is the
3146  * value in s0. */
3147  unsigned int index = 2;
3148  while (index < count) {
3149  riscv_addr_t read_addr = address + index * increment;
3150  LOG_DEBUG("i=%d, count=%d, read_addr=0x%" PRIx64, index, count, read_addr);
3151  /* The pipeline looks like this:
3152  * memory -> s1 -> dm_data0 -> debugger
3153  * Right now:
3154  * s0 contains read_addr
3155  * s1 contains mem[read_addr-size]
3156  * dm_data0 contains[read_addr-size*2]
3157  */
3158 
3159  struct riscv_batch *batch = riscv_batch_alloc(target, 32,
3160  info->dmi_busy_delay + info->ac_busy_delay);
3161  if (!batch)
3162  return ERROR_FAIL;
3163 
3164  unsigned int reads = 0;
3165  for (unsigned int j = index; j < count; j++) {
3166  if (size > 4)
3169 
3170  reads++;
3171  if (riscv_batch_full(batch))
3172  break;
3173  }
3174 
3175  batch_run(target, batch);
3176 
3177  /* Wait for the target to finish performing the last abstract command,
3178  * and update our copy of cmderr. If we see that DMI is busy here,
3179  * dmi_busy_delay will be incremented. */
3180  uint32_t abstractcs;
3181  if (dmi_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
3182  return ERROR_FAIL;
3183  while (get_field(abstractcs, DM_ABSTRACTCS_BUSY))
3184  if (dmi_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
3185  return ERROR_FAIL;
3186  info->cmderr = get_field(abstractcs, DM_ABSTRACTCS_CMDERR);
3187 
3188  unsigned int next_index;
3189  unsigned int ignore_last = 0;
3190  switch (info->cmderr) {
3191  case CMDERR_NONE:
3192  LOG_DEBUG("successful (partial?) memory read");
3193  next_index = index + reads;
3194  break;
3195  case CMDERR_BUSY:
3196  LOG_DEBUG("memory read resulted in busy response");
3197 
3200 
3202 
3203  uint32_t dmi_data0, dmi_data1 = 0;
3204  /* This is definitely a good version of the value that we
3205  * attempted to read when we discovered that the target was
3206  * busy. */
3207  if (dmi_read(target, &dmi_data0, DM_DATA0) != ERROR_OK) {
3208  riscv_batch_free(batch);
3209  goto error;
3210  }
3211  if (size > 4 && dmi_read(target, &dmi_data1, DM_DATA1) != ERROR_OK) {
3212  riscv_batch_free(batch);
3213  goto error;
3214  }
3215 
3216  /* See how far we got, clobbering dmi_data0. */
3217  if (increment == 0) {
3218  uint64_t counter;
3219  result = register_read_direct(target, &counter, GDB_REGNO_S2);
3220  next_index = counter;
3221  } else {
3222  uint64_t next_read_addr;
3223  result = register_read_direct(target, &next_read_addr,
3224  GDB_REGNO_S0);
3225  next_index = (next_read_addr - address) / increment;
3226  }
3227  if (result != ERROR_OK) {
3228  riscv_batch_free(batch);
3229  goto error;
3230  }
3231 
3232  uint64_t value64 = (((uint64_t)dmi_data1) << 32) | dmi_data0;
3233  buf_set_u64(buffer + (next_index - 2) * size, 0, 8 * size, value64);
3234  log_memory_access(address + (next_index - 2) * size, value64, size, true);
3235 
3236  /* Restore the command, and execute it.
3237  * Now DM_DATA0 contains the next value just as it would if no
3238  * error had occurred. */
3240  next_index++;
3241 
3244 
3245  ignore_last = 1;
3246 
3247  break;
3248  default:
3249  LOG_DEBUG("error when reading memory, abstractcs=0x%08lx", (long)abstractcs);
3251  riscv_batch_free(batch);
3252  result = ERROR_FAIL;
3253  goto error;
3254  }
3255 
3256  /* Now read whatever we got out of the batch. */
3258  unsigned int read = 0;
3259  assert(index >= 2);
3260  for (unsigned int j = index - 2; j < index + reads; j++) {
3261  assert(j < count);
3262  LOG_DEBUG("index=%d, reads=%d, next_index=%d, ignore_last=%d, j=%d",
3263  index, reads, next_index, ignore_last, j);
3264  if (j + 3 + ignore_last > next_index)
3265  break;
3266 
3267  status = riscv_batch_get_dmi_read_op(batch, read);
3268  uint64_t value = riscv_batch_get_dmi_read_data(batch, read);
3269  read++;
3270  if (status != DMI_STATUS_SUCCESS) {
3271  /* If we're here because of busy count, dmi_busy_delay will
3272  * already have been increased and busy state will have been
3273  * cleared in dmi_read(). */
3274  /* In at least some implementations, we issue a read, and then
3275  * can get busy back when we try to scan out the read result,
3276  * and the actual read value is lost forever. Since this is
3277  * rare in any case, we return error here and rely on our
3278  * caller to reread the entire block. */
3279  LOG_WARNING("Batch memory read encountered DMI error %d. "
3280  "Falling back on slower reads.", status);
3281  riscv_batch_free(batch);
3282  result = ERROR_FAIL;
3283  goto error;
3284  }
3285  if (size > 4) {
3286  status = riscv_batch_get_dmi_read_op(batch, read);
3287  if (status != DMI_STATUS_SUCCESS) {
3288  LOG_WARNING("Batch memory read encountered DMI error %d. "
3289  "Falling back on slower reads.", status);
3290  riscv_batch_free(batch);
3291  result = ERROR_FAIL;
3292  goto error;
3293  }
3294  value <<= 32;
3295  value |= riscv_batch_get_dmi_read_data(batch, read);
3296  read++;
3297  }
3298  riscv_addr_t offset = j * size;
3299  buf_set_u64(buffer + offset, 0, 8 * size, value);
3300  log_memory_access(address + j * increment, value, size, true);
3301  }
3302 
3303  index = next_index;
3304 
3305  riscv_batch_free(batch);
3306  }
3307 
3309 
3310  if (count > 1) {
3311  /* Read the penultimate word. */
3312  uint32_t dmi_data0, dmi_data1 = 0;
3313  if (dmi_read(target, &dmi_data0, DM_DATA0) != ERROR_OK)
3314  return ERROR_FAIL;
3315  if (size > 4 && dmi_read(target, &dmi_data1, DM_DATA1) != ERROR_OK)
3316  return ERROR_FAIL;
3317  uint64_t value64 = (((uint64_t)dmi_data1) << 32) | dmi_data0;
3318  buf_set_u64(buffer + size * (count - 2), 0, 8 * size, value64);
3319  log_memory_access(address + size * (count - 2), value64, size, true);
3320  }
3321 
3322  /* Read the last word. */
3323  uint64_t value;
3324  result = register_read_direct(target, &value, GDB_REGNO_S1);
3325  if (result != ERROR_OK)
3326  goto error;
3327  buf_set_u64(buffer + size * (count-1), 0, 8 * size, value);
3328  log_memory_access(address + size * (count-1), value, size, true);
3329 
3330  return ERROR_OK;
3331 
3332 error:
3334 
3335  return result;
3336 }
3337 
3338 /* Only need to save/restore one GPR to read a single word, and the progbuf
3339  * program doesn't need to increment. */
3341  uint32_t size, uint8_t *buffer)
3342 {
3343  uint64_t mstatus = 0;
3344  uint64_t mstatus_old = 0;
3345  if (modify_privilege(target, &mstatus, &mstatus_old) != ERROR_OK)
3346  return ERROR_FAIL;
3347 
3348  uint64_t s0;
3349  int result = ERROR_FAIL;
3350 
3352  goto restore_mstatus;
3353 
3354  /* Write the program (load, increment) */
3355  struct riscv_program program;
3356  riscv_program_init(&program, target);
3359  switch (size) {
3360  case 1:
3362  break;
3363  case 2:
3365  break;
3366  case 4:
3368  break;
3369  case 8:
3371  break;
3372  default:
3373  LOG_ERROR("Unsupported size: %d", size);
3374  goto restore_mstatus;
3375  }
3378 
3379  if (riscv_program_ebreak(&program) != ERROR_OK)
3380  goto restore_mstatus;
3381  if (riscv_program_write(&program) != ERROR_OK)
3382  goto restore_mstatus;
3383 
3384  /* Write address to S0, and execute buffer. */
3385  if (write_abstract_arg(target, 0, address, riscv_xlen(target)) != ERROR_OK)
3386  goto restore_mstatus;
3391  goto restore_s0;
3392 
3393  uint64_t value;
3394  if (register_read(target, &value, GDB_REGNO_S0) != ERROR_OK)
3395  goto restore_s0;
3396  buf_set_u64(buffer, 0, 8 * size, value);
3397  log_memory_access(address, value, size, true);
3398  result = ERROR_OK;
3399 
3400 restore_s0:
3402  result = ERROR_FAIL;
3403 
3404 restore_mstatus:
3405  if (mstatus != mstatus_old)
3406  if (register_write_direct(target, GDB_REGNO_MSTATUS, mstatus_old))
3407  result = ERROR_FAIL;
3408 
3409  return result;
3410 }
3411 
3415 static int read_memory_progbuf(struct target *target, target_addr_t address,
3416  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
3417 {
3418  if (riscv_xlen(target) < size * 8) {
3419  LOG_ERROR("XLEN (%d) is too short for %d-bit memory read.",
3420  riscv_xlen(target), size * 8);
3421  return ERROR_FAIL;
3422  }
3423 
3424  int result = ERROR_OK;
3425 
3426  LOG_DEBUG("reading %d words of %d bytes from 0x%" TARGET_PRIxADDR, count,
3427  size, address);
3428 
3429  select_dmi(target);
3430 
3431  memset(buffer, 0, count*size);
3432 
3433  if (execute_fence(target) != ERROR_OK)
3434  return ERROR_FAIL;
3435 
3436  if (count == 1)
3437  return read_memory_progbuf_one(target, address, size, buffer);
3438 
3439  uint64_t mstatus = 0;
3440  uint64_t mstatus_old = 0;
3441  if (modify_privilege(target, &mstatus, &mstatus_old) != ERROR_OK)
3442  return ERROR_FAIL;
3443 
3444  /* s0 holds the next address to read from
3445  * s1 holds the next data value read
3446  * s2 is a counter in case increment is 0
3447  */
3448  uint64_t s0, s1, s2;
3450  return ERROR_FAIL;
3452  return ERROR_FAIL;
3453  if (increment == 0 && register_read(target, &s2, GDB_REGNO_S2) != ERROR_OK)
3454  return ERROR_FAIL;
3455 
3456  /* Write the program (load, increment) */
3457  struct riscv_program program;
3458  riscv_program_init(&program, target);
3461 
3462  switch (size) {
3463  case 1:
3465  break;
3466  case 2:
3468  break;
3469  case 4:
3471  break;
3472  case 8:
3474  break;
3475  default:
3476  LOG_ERROR("Unsupported size: %d", size);
3477  return ERROR_FAIL;
3478  }
3479 
3482  if (increment == 0)
3484  else
3485  riscv_program_addi(&program, GDB_REGNO_S0, GDB_REGNO_S0, increment);
3486 
3487  if (riscv_program_ebreak(&program) != ERROR_OK)
3488  return ERROR_FAIL;
3489  if (riscv_program_write(&program) != ERROR_OK)
3490  return ERROR_FAIL;
3491 
3492  result = read_memory_progbuf_inner(target, address, size, count, buffer, increment);
3493 
3494  if (result != ERROR_OK) {
3495  /* The full read did not succeed, so we will try to read each word individually. */
3496  /* This will not be fast, but reading outside actual memory is a special case anyway. */
3497  /* It will make the toolchain happier, especially Eclipse Memory View as it reads ahead. */
3498  target_addr_t address_i = address;
3499  uint32_t count_i = 1;
3500  uint8_t *buffer_i = buffer;
3501 
3502  for (uint32_t i = 0; i < count; i++, address_i += increment, buffer_i += size) {
3503  /* TODO: This is much slower than it needs to be because we end up
3504  * writing the address to read for every word we read. */
3505  result = read_memory_progbuf_inner(target, address_i, size, count_i, buffer_i, increment);
3506 
3507  /* The read of a single word failed, so we will just return 0 for that instead */
3508  if (result != ERROR_OK) {
3509  LOG_DEBUG("error reading single word of %d bytes from 0x%" TARGET_PRIxADDR,
3510  size, address_i);
3511 
3512  buf_set_u64(buffer_i, 0, 8 * size, 0);
3513  }
3514  }
3515  result = ERROR_OK;
3516  }
3517 
3520  if (increment == 0)
3522 
3523  /* Restore MSTATUS */
3524  if (mstatus != mstatus_old)
3525  if (register_write_direct(target, GDB_REGNO_MSTATUS, mstatus_old))
3526  return ERROR_FAIL;
3527 
3528  return result;
3529 }
3530 
3531 static int read_memory(struct target *target, target_addr_t address,
3532  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
3533 {
3534  if (count == 0)
3535  return ERROR_OK;
3536 
3537  if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16) {
3538  LOG_ERROR("BUG: Unsupported size for memory read: %d", size);
3539  return ERROR_FAIL;
3540  }
3541 
3542  int ret = ERROR_FAIL;
3543  RISCV_INFO(r);
3545 
3546  char *progbuf_result = "disabled";
3547  char *sysbus_result = "disabled";
3548  char *abstract_result = "disabled";
3549 
3550  for (unsigned int i = 0; i < RISCV_NUM_MEM_ACCESS_METHODS; i++) {
3551  int method = r->mem_access_methods[i];
3552 
3553  if (method == RISCV_MEM_ACCESS_PROGBUF) {
3554  if (mem_should_skip_progbuf(target, address, size, true, &progbuf_result))
3555  continue;
3556 
3557  ret = read_memory_progbuf(target, address, size, count, buffer, increment);
3558 
3559  if (ret != ERROR_OK)
3560  progbuf_result = "failed";
3561  } else if (method == RISCV_MEM_ACCESS_SYSBUS) {
3562  if (mem_should_skip_sysbus(target, address, size, increment, true, &sysbus_result))
3563  continue;
3564 
3565  if (get_field(info->sbcs, DM_SBCS_SBVERSION) == 0)
3566  ret = read_memory_bus_v0(target, address, size, count, buffer, increment);
3567  else if (get_field(info->sbcs, DM_SBCS_SBVERSION) == 1)
3568  ret = read_memory_bus_v1(target, address, size, count, buffer, increment);
3569 
3570  if (ret != ERROR_OK)
3571  sysbus_result = "failed";
3572  } else if (method == RISCV_MEM_ACCESS_ABSTRACT) {
3573  if (mem_should_skip_abstract(target, address, size, increment, true, &abstract_result))
3574  continue;
3575 
3576  ret = read_memory_abstract(target, address, size, count, buffer, increment);
3577 
3578  if (ret != ERROR_OK)
3579  abstract_result = "failed";
3580  } else if (method == RISCV_MEM_ACCESS_UNSPECIFIED)
3581  /* No further mem access method to try. */
3582  break;
3583 
3584  log_mem_access_result(target, ret == ERROR_OK, method, true);
3585 
3586  if (ret == ERROR_OK)
3587  return ret;
3588  }
3589 
3590  LOG_ERROR("Target %s: Failed to read memory (addr=0x%" PRIx64 ")", target_name(target), address);
3591  LOG_ERROR(" progbuf=%s, sysbus=%s, abstract=%s", progbuf_result, sysbus_result, abstract_result);
3592  return ret;
3593 }
3594 
3595 static int write_memory_bus_v0(struct target *target, target_addr_t address,
3596  uint32_t size, uint32_t count, const uint8_t *buffer)
3597 {
3598  /*1) write sbaddress: for singlewrite and autoincrement, we need to write the address once*/
3599  LOG_DEBUG("System Bus Access: size: %d\tcount:%d\tstart address: 0x%08"
3600  TARGET_PRIxADDR, size, count, address);
3601  dmi_write(target, DM_SBADDRESS0, address);
3602  int64_t value = 0;
3603  int64_t access = 0;
3604  riscv_addr_t offset = 0;
3605  riscv_addr_t t_addr = 0;
3606  const uint8_t *t_buffer = buffer + offset;
3607 
3608  /* B.8 Writing Memory, single write check if we write in one go */
3609  if (count == 1) { /* count is in bytes here */
3610  value = buf_get_u64(t_buffer, 0, 8 * size);
3611 
3612  access = 0;
3613  access = set_field(access, DM_SBCS_SBACCESS, size/2);
3614  dmi_write(target, DM_SBCS, access);
3615  LOG_DEBUG("\r\naccess: 0x%08" PRIx64, access);
3616  LOG_DEBUG("\r\nwrite_memory:SAB: ONE OFF: value 0x%08" PRIx64, value);
3617  dmi_write(target, DM_SBDATA0, value);
3618  return ERROR_OK;
3619  }
3620 
3621  /*B.8 Writing Memory, using autoincrement*/
3622 
3623  access = 0;
3624  access = set_field(access, DM_SBCS_SBACCESS, size/2);
3625  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 1);
3626  LOG_DEBUG("\r\naccess: 0x%08" PRIx64, access);
3627  dmi_write(target, DM_SBCS, access);
3628 
3629  /*2)set the value according to the size required and write*/
3630  for (riscv_addr_t i = 0; i < count; ++i) {
3631  offset = size*i;
3632  /* for monitoring only */
3633  t_addr = address + offset;
3634  t_buffer = buffer + offset;
3635 
3636  value = buf_get_u64(t_buffer, 0, 8 * size);
3637  LOG_DEBUG("SAB:autoincrement: expected address: 0x%08x value: 0x%08x"
3638  PRIx64, (uint32_t)t_addr, (uint32_t)value);
3639  dmi_write(target, DM_SBDATA0, value);
3640  }
3641  /*reset the autoincrement when finished (something weird is happening if this is not done at the end*/
3642  access = set_field(access, DM_SBCS_SBAUTOINCREMENT, 0);
3643  dmi_write(target, DM_SBCS, access);
3644 
3645  return ERROR_OK;
3646 }
3647 
3648 static int write_memory_bus_v1(struct target *target, target_addr_t address,
3649  uint32_t size, uint32_t count, const uint8_t *buffer)
3650 {
3652  uint32_t sbcs = sb_sbaccess(size);
3653  sbcs = set_field(sbcs, DM_SBCS_SBAUTOINCREMENT, 1);
3654  dmi_write(target, DM_SBCS, sbcs);
3655 
3656  target_addr_t next_address = address;
3657  target_addr_t end_address = address + count * size;
3658 
3659  int result;
3660 
3661  sb_write_address(target, next_address, true);
3662  while (next_address < end_address) {
3663  LOG_DEBUG("transferring burst starting at address 0x%" TARGET_PRIxADDR,
3664  next_address);
3665 
3666  struct riscv_batch *batch = riscv_batch_alloc(
3667  target,
3668  32,
3669  info->dmi_busy_delay + info->bus_master_write_delay);
3670  if (!batch)
3671  return ERROR_FAIL;
3672 
3673  for (uint32_t i = (next_address - address) / size; i < count; i++) {
3674  const uint8_t *p = buffer + i * size;
3675 
3676  if (riscv_batch_available_scans(batch) < (size + 3) / 4)
3677  break;
3678 
3679  if (size > 12)
3681  ((uint32_t) p[12]) |
3682  (((uint32_t) p[13]) << 8) |
3683  (((uint32_t) p[14]) << 16) |
3684  (((uint32_t) p[15]) << 24));
3685 
3686  if (size > 8)
3688  ((uint32_t) p[8]) |
3689  (((uint32_t) p[9]) << 8) |
3690  (((uint32_t) p[10]) << 16) |
3691  (((uint32_t) p[11]) << 24));
3692  if (size > 4)
3694  ((uint32_t) p[4]) |
3695  (((uint32_t) p[5]) << 8) |
3696  (((uint32_t) p[6]) << 16) |
3697  (((uint32_t) p[7]) << 24));
3698  uint32_t value = p[0];
3699  if (size > 2) {
3700  value |= ((uint32_t) p[2]) << 16;
3701  value |= ((uint32_t) p[3]) << 24;
3702  }
3703  if (size > 1)
3704  value |= ((uint32_t) p[1]) << 8;
3705  riscv_batch_add_dmi_write(batch, DM_SBDATA0, value);
3706 
3707  log_memory_access(address + i * size, value, size, false);
3708  next_address += size;
3709  }
3710 
3711  /* Execute the batch of writes */
3712  result = batch_run(target, batch);
3713  riscv_batch_free(batch);
3714  if (result != ERROR_OK)
3715  return result;
3716 
3717  /* Read sbcs value.
3718  * At the same time, detect if DMI busy has occurred during the batch write. */
3719  bool dmi_busy_encountered;
3720  if (dmi_op(target, &sbcs, &dmi_busy_encountered, DMI_OP_READ,
3721  DM_SBCS, 0, false, true) != ERROR_OK)
3722  return ERROR_FAIL;
3723  if (dmi_busy_encountered)
3724  LOG_DEBUG("DMI busy encountered during system bus write.");
3725 
3726  /* Wait until sbbusy goes low */
3727  time_t start = time(NULL);
3728  while (get_field(sbcs, DM_SBCS_SBBUSY)) {
3729  if (time(NULL) - start > riscv_command_timeout_sec) {
3730  LOG_ERROR("Timed out after %ds waiting for sbbusy to go low (sbcs=0x%x). "
3731  "Increase the timeout with riscv set_command_timeout_sec.",
3733  return ERROR_FAIL;
3734  }
3735  if (dmi_read(target, &sbcs, DM_SBCS) != ERROR_OK)
3736  return ERROR_FAIL;
3737  }
3738 
3739  if (get_field(sbcs, DM_SBCS_SBBUSYERROR)) {
3740  /* We wrote while the target was busy. */
3741  LOG_DEBUG("Sbbusyerror encountered during system bus write.");
3742  /* Clear the sticky error flag. */
3744  /* Slow down before trying again. */
3745  info->bus_master_write_delay += info->bus_master_write_delay / 10 + 1;
3746  }
3747 
3748  if (get_field(sbcs, DM_SBCS_SBBUSYERROR) || dmi_busy_encountered) {
3749  /* Recover from the case when the write commands were issued too fast.
3750  * Determine the address from which to resume writing. */
3751  next_address = sb_read_address(target);
3752  if (next_address < address) {
3753  /* This should never happen, probably buggy hardware. */
3754  LOG_DEBUG("unexpected sbaddress=0x%" TARGET_PRIxADDR
3755  " - buggy sbautoincrement in hw?", next_address);
3756  /* Fail the whole operation. */
3757  return ERROR_FAIL;
3758  }
3759  /* Try again - resume writing. */
3760  continue;
3761  }
3762 
3763  unsigned int sberror = get_field(sbcs, DM_SBCS_SBERROR);
3764  if (sberror != 0) {
3765  /* Sberror indicates the bus access failed, but not because we issued the writes
3766  * too fast. Cannot recover. Sbaddress holds the address where the error occurred
3767  * (unless sbautoincrement in the HW is buggy).
3768  */
3769  target_addr_t sbaddress = sb_read_address(target);
3770  LOG_DEBUG("System bus access failed with sberror=%u (sbaddress=0x%" TARGET_PRIxADDR ")",
3771  sberror, sbaddress);
3772  if (sbaddress < address) {
3773  /* This should never happen, probably buggy hardware.
3774  * Make a note to the user not to trust the sbaddress value. */
3775  LOG_DEBUG("unexpected sbaddress=0x%" TARGET_PRIxADDR
3776  " - buggy sbautoincrement in hw?", next_address);
3777  }
3778  /* Clear the sticky error flag */
3780  /* Fail the whole operation */
3781  return ERROR_FAIL;
3782  }
3783  }
3784 
3785  return ERROR_OK;
3786 }
3787 
3788 static int write_memory_progbuf(struct target *target, target_addr_t address,
3789  uint32_t size, uint32_t count, const uint8_t *buffer)
3790 {
3792 
3793  if (riscv_xlen(target) < size * 8) {
3794  LOG_ERROR("XLEN (%d) is too short for %d-bit memory write.",
3795  riscv_xlen(target), size * 8);
3796  return ERROR_FAIL;
3797  }
3798 
3799  LOG_DEBUG("writing %d words of %d bytes to 0x%08lx", count, size, (long)address);
3800 
3801  select_dmi(target);
3802 
3803  uint64_t mstatus = 0;
3804  uint64_t mstatus_old = 0;
3805  if (modify_privilege(target, &mstatus, &mstatus_old) != ERROR_OK)
3806  return ERROR_FAIL;
3807 
3808  /* s0 holds the next address to write to
3809  * s1 holds the next data value to write
3810  */
3811 
3812  int result = ERROR_OK;
3813  uint64_t s0, s1;
3815  return ERROR_FAIL;
3817  return ERROR_FAIL;
3818 
3819  /* Write the program (store, increment) */
3820  struct riscv_program program;
3821  riscv_program_init(&program, target);
3824 
3825  switch (size) {
3826  case 1:
3828  break;
3829  case 2:
3831  break;
3832  case 4:
3834  break;
3835  case 8:
3837  break;
3838  default:
3839  LOG_ERROR("write_memory_progbuf(): Unsupported size: %d", size);
3840  result = ERROR_FAIL;
3841  goto error;
3842  }
3843 
3847 
3848  result = riscv_program_ebreak(&program);
3849  if (result != ERROR_OK)
3850  goto error;
3851  riscv_program_write(&program);
3852 
3853  riscv_addr_t cur_addr = address;
3854  riscv_addr_t fin_addr = address + (count * size);
3855  bool setup_needed = true;
3856  LOG_DEBUG("writing until final address 0x%016" PRIx64, fin_addr);
3857  while (cur_addr < fin_addr) {
3858  LOG_DEBUG("transferring burst starting at address 0x%016" PRIx64,
3859  cur_addr);
3860 
3861  struct riscv_batch *batch = riscv_batch_alloc(
3862  target,
3863  32,
3864  info->dmi_busy_delay + info->ac_busy_delay);
3865  if (!batch)
3866  goto error;
3867 
3868  /* To write another word, we put it in S1 and execute the program. */
3869  unsigned int start = (cur_addr - address) / size;
3870  for (unsigned int i = start; i < count; ++i) {
3871  unsigned int offset = size * i;
3872  const uint8_t *t_buffer = buffer + offset;
3873 
3874  uint64_t value = buf_get_u64(t_buffer, 0, 8 * size);
3875 
3876  log_memory_access(address + offset, value, size, false);
3877  cur_addr += size;
3878 
3879  if (setup_needed) {
3881  address + offset);
3882  if (result != ERROR_OK) {
3883  riscv_batch_free(batch);
3884  goto error;
3885  }
3886 
3887  /* Write value. */
3888  if (size > 4)
3889  dmi_write(target, DM_DATA1, value >> 32);
3890  dmi_write(target, DM_DATA0, value);
3891 
3892  /* Write and execute command that moves value into S1 and
3893  * executes program buffer. */
3900  if (result != ERROR_OK) {
3901  riscv_batch_free(batch);
3902  goto error;
3903  }
3904 
3905  /* Turn on autoexec */
3908 
3909  setup_needed = false;
3910  } else {
3911  if (size > 4)
3912  riscv_batch_add_dmi_write(batch, DM_DATA1, value >> 32);
3913  riscv_batch_add_dmi_write(batch, DM_DATA0, value);
3914  if (riscv_batch_full(batch))
3915  break;
3916  }
3917  }
3918 
3919  result = batch_run(target, batch);
3920  riscv_batch_free(batch);
3921  if (result != ERROR_OK)
3922  goto error;
3923 
3924  /* Note that if the scan resulted in a Busy DMI response, it
3925  * is this read to abstractcs that will cause the dmi_busy_delay
3926  * to be incremented if necessary. */
3927 
3928  uint32_t abstractcs;
3929  bool dmi_busy_encountered;
3930  result = dmi_op(target, &abstractcs, &dmi_busy_encountered,
3931  DMI_OP_READ, DM_ABSTRACTCS, 0, false, true);
3932  if (result != ERROR_OK)
3933  goto error;
3934  while (get_field(abstractcs, DM_ABSTRACTCS_BUSY))
3935  if (dmi_read(target, &abstractcs, DM_ABSTRACTCS) != ERROR_OK)
3936  return ERROR_FAIL;
3937  info->cmderr = get_field(abstractcs, DM_ABSTRACTCS_CMDERR);
3938  if (info->cmderr == CMDERR_NONE && !dmi_busy_encountered) {
3939  LOG_DEBUG("successful (partial?) memory write");
3940  } else if (info->cmderr == CMDERR_BUSY || dmi_busy_encountered) {
3941  if (info->cmderr == CMDERR_BUSY)
3942  LOG_DEBUG("Memory write resulted in abstract command busy response.");
3943  else if (dmi_busy_encountered)
3944  LOG_DEBUG("Memory write resulted in DMI busy response.");
3947 
3949  result = register_read_direct(target, &cur_addr, GDB_REGNO_S0);
3950  if (result != ERROR_OK)
3951  goto error;
3952  setup_needed = true;
3953  } else {
3954  LOG_ERROR("error when writing memory, abstractcs=0x%08lx", (long)abstractcs);
3956  result = ERROR_FAIL;
3957  goto error;
3958  }
3959  }
3960 
3961 error:
3963 
3965  return ERROR_FAIL;
3967  return ERROR_FAIL;
3968 
3969  /* Restore MSTATUS */
3970  if (mstatus != mstatus_old)
3971  if (register_write_direct(target, GDB_REGNO_MSTATUS, mstatus_old))
3972  return ERROR_FAIL;
3973 
3974  if (execute_fence(target) != ERROR_OK)
3975  return ERROR_FAIL;
3976 
3977  return result;
3978 }
3979 
3980 static int write_memory(struct target *target, target_addr_t address,
3981  uint32_t size, uint32_t count, const uint8_t *buffer)
3982 {
3983  if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16) {
3984  LOG_ERROR("BUG: Unsupported size for memory write: %d", size);
3985  return ERROR_FAIL;
3986  }
3987 
3988  int ret = ERROR_FAIL;
3989  RISCV_INFO(r);
3991 
3992  char *progbuf_result = "disabled";
3993  char *sysbus_result = "disabled";
3994  char *abstract_result = "disabled";
3995 
3996  for (unsigned int i = 0; i < RISCV_NUM_MEM_ACCESS_METHODS; i++) {
3997  int method = r->mem_access_methods[i];
3998 
3999  if (method == RISCV_MEM_ACCESS_PROGBUF) {
4000  if (mem_should_skip_progbuf(target, address, size, false, &progbuf_result))
4001  continue;
4002 
4003  ret = write_memory_progbuf(target, address, size, count, buffer);
4004 
4005  if (ret != ERROR_OK)
4006  progbuf_result = "failed";
4007  } else if (method == RISCV_MEM_ACCESS_SYSBUS) {
4008  if (mem_should_skip_sysbus(target, address, size, 0, false, &sysbus_result))
4009  continue;
4010 
4011  if (get_field(info->sbcs, DM_SBCS_SBVERSION) == 0)
4012  ret = write_memory_bus_v0(target, address, size, count, buffer);
4013  else if (get_field(info->sbcs, DM_SBCS_SBVERSION) == 1)
4014  ret = write_memory_bus_v1(target, address, size, count, buffer);
4015 
4016  if (ret != ERROR_OK)
4017  sysbus_result = "failed";
4018  } else if (method == RISCV_MEM_ACCESS_ABSTRACT) {
4019  if (mem_should_skip_abstract(target, address, size, 0, false, &abstract_result))
4020  continue;
4021 
4022  ret = write_memory_abstract(target, address, size, count, buffer);
4023 
4024  if (ret != ERROR_OK)
4025  abstract_result = "failed";
4026  } else if (method == RISCV_MEM_ACCESS_UNSPECIFIED)
4027  /* No further mem access method to try. */
4028  break;
4029 
4030  log_mem_access_result(target, ret == ERROR_OK, method, false);
4031 
4032  if (ret == ERROR_OK)
4033  return ret;
4034  }
4035 
4036  LOG_ERROR("Target %s: Failed to write memory (addr=0x%" PRIx64 ")", target_name(target), address);
4037  LOG_ERROR(" progbuf=%s, sysbus=%s, abstract=%s", progbuf_result, sysbus_result, abstract_result);
4038  return ret;
4039 }
4040 
4041 static int arch_state(struct target *target)
4042 {
4043  return ERROR_OK;
4044 }
4045 
4046 struct target_type riscv013_target = {
4047  .name = "riscv",
4048 
4049  .init_target = init_target,
4050  .deinit_target = deinit_target,
4051  .examine = examine,
4052 
4053  .poll = &riscv_openocd_poll,
4054  .halt = &riscv_halt,
4055  .step = &riscv_openocd_step,
4056 
4057  .assert_reset = assert_reset,
4058  .deassert_reset = deassert_reset,
4059 
4060  .write_memory = write_memory,
4061 
4062  .arch_state = arch_state
4063 };
4064 
4065 /*** 0.13-specific implementations of various RISC-V helper functions. ***/
4067  riscv_reg_t *value, int rid)
4068 {
4069  LOG_DEBUG("[%s] reading register %s", target_name(target),
4070  gdb_regno_name(rid));
4071 
4073  return ERROR_FAIL;
4074 
4075  int result = ERROR_OK;
4076  if (rid == GDB_REGNO_PC) {
4077  /* TODO: move this into riscv.c. */
4078  result = register_read(target, value, GDB_REGNO_DPC);
4079  LOG_DEBUG("[%d] read PC from DPC: 0x%" PRIx64, target->coreid, *value);
4080  } else if (rid == GDB_REGNO_PRIV) {
4081  uint64_t dcsr;
4082  /* TODO: move this into riscv.c. */
4083  result = register_read(target, &dcsr, GDB_REGNO_DCSR);
4084  *value = set_field(0, VIRT_PRIV_V, get_field(dcsr, CSR_DCSR_V));
4085  *value = set_field(*value, VIRT_PRIV_PRV, get_field(dcsr, CSR_DCSR_PRV));
4086  } else {
4087  result = register_read(target, value, rid);
4088  if (result != ERROR_OK)
4089  *value = -1;
4090  }
4091 
4092  return result;
4093 }
4094 
4095 static int riscv013_set_register(struct target *target, int rid, uint64_t value)
4096 {
4098  LOG_DEBUG("[%d] writing 0x%" PRIx64 " to register %s",
4099  target->coreid, value, gdb_regno_name(rid));
4100 
4101  if (rid <= GDB_REGNO_XPR31) {
4102  return register_write_direct(target, rid, value);
4103  } else if (rid == GDB_REGNO_PC) {
4104  LOG_DEBUG("[%d] writing PC to DPC: 0x%" PRIx64, target->coreid, value);
4106  uint64_t actual_value;
4107  register_read_direct(target, &actual_value, GDB_REGNO_DPC);
4108  LOG_DEBUG("[%d] actual DPC written: 0x%016" PRIx64, target->coreid, actual_value);
4109  if (value != actual_value) {
4110  LOG_ERROR("Written PC (0x%" PRIx64 ") does not match read back "
4111  "value (0x%" PRIx64 ")", value, actual_value);
4112  return ERROR_FAIL;
4113  }
4114  } else if (rid == GDB_REGNO_PRIV) {
4115  uint64_t dcsr;
4117  dcsr = set_field(dcsr, CSR_DCSR_PRV, get_field(value, VIRT_PRIV_PRV));
4118  dcsr = set_field(dcsr, CSR_DCSR_V, get_field(value, VIRT_PRIV_V));
4120  } else {
4121  return register_write_direct(target, rid, value);
4122  }
4123 
4124  return ERROR_OK;
4125 }
4126 
4128 {
4129  RISCV_INFO(r);
4130 
4131  dm013_info_t *dm = get_dm(target);
4132  if (!dm)
4133  return ERROR_FAIL;
4134  if (r->current_hartid == dm->current_hartid)
4135  return ERROR_OK;
4136 
4137  uint32_t dmcontrol;
4138  /* TODO: can't we just "dmcontrol = DMI_DMACTIVE"? */
4139  if (dmi_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
4140  return ERROR_FAIL;
4141  dmcontrol = set_hartsel(dmcontrol, r->current_hartid);
4142  int result = dmi_write(target, DM_DMCONTROL, dmcontrol);
4143  dm->current_hartid = r->current_hartid;
4144  return result;
4145 }
4146 
4147 /* Select all harts that were prepped and that are selectable, clearing the
4148  * prepped flag on the harts that actually were selected. */
4149 static int select_prepped_harts(struct target *target, bool *use_hasel)
4150 {
4151  dm013_info_t *dm = get_dm(target);
4152  if (!dm)
4153  return ERROR_FAIL;
4154  if (!dm->hasel_supported) {
4155  RISCV_INFO(r);
4156  r->prepped = false;
4157  *use_hasel = false;
4158  return ERROR_OK;
4159  }
4160 
4161  assert(dm->hart_count);
4162  unsigned int hawindow_count = (dm->hart_count + 31) / 32;
4163  uint32_t hawindow[hawindow_count];
4164 
4165  memset(hawindow, 0, sizeof(uint32_t) * hawindow_count);
4166 
4167  target_list_t *entry;
4168  unsigned int total_selected = 0;
4169  list_for_each_entry(entry, &dm->target_list, list) {
4170  struct target *t = entry->target;
4171  struct riscv_info *r = riscv_info(t);
4173  unsigned int index = info->index;
4174  LOG_DEBUG("index=%d, coreid=%d, prepped=%d", index, t->coreid, r->prepped);
4175  r->selected = r->prepped;
4176  if (r->prepped) {
4177  hawindow[index / 32] |= 1 << (index % 32);
4178  r->prepped = false;
4179  total_selected++;
4180  }
4181  index++;
4182  }
4183 
4184  /* Don't use hasel if we only need to talk to one hart. */
4185  if (total_selected <= 1) {
4186  *use_hasel = false;
4187  return ERROR_OK;
4188  }
4189 
4190  for (unsigned int i = 0; i < hawindow_count; i++) {
4192  return ERROR_FAIL;
4193  if (dmi_write(target, DM_HAWINDOW, hawindow[i]) != ERROR_OK)
4194  return ERROR_FAIL;
4195  }
4196 
4197  *use_hasel = true;
4198  return ERROR_OK;
4199 }
4200 
4201 static int riscv013_halt_prep(struct target *target)
4202 {
4203  return ERROR_OK;
4204 }
4205 
4206 static int riscv013_halt_go(struct target *target)
4207 {
4208  bool use_hasel = false;
4209  if (select_prepped_harts(target, &use_hasel) != ERROR_OK)
4210  return ERROR_FAIL;
4211 
4212  RISCV_INFO(r);
4213  LOG_DEBUG("halting hart %d", r->current_hartid);
4214 
4215  /* Issue the halt command, and then wait for the current hart to halt. */
4216  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_HALTREQ;
4217  if (use_hasel)
4218  dmcontrol |= DM_DMCONTROL_HASEL;
4219  dmcontrol = set_hartsel(dmcontrol, r->current_hartid);
4220  dmi_write(target, DM_DMCONTROL, dmcontrol);
4221  for (size_t i = 0; i < 256; ++i)
4222  if (riscv_is_halted(target))
4223  break;
4224 
4225  if (!riscv_is_halted(target)) {
4226  uint32_t dmstatus;
4227  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
4228  return ERROR_FAIL;
4229  if (dmi_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK)
4230  return ERROR_FAIL;
4231 
4232  LOG_ERROR("unable to halt hart %d", r->current_hartid);
4233  LOG_ERROR(" dmcontrol=0x%08x", dmcontrol);
4234  LOG_ERROR(" dmstatus =0x%08x", dmstatus);
4235  return ERROR_FAIL;
4236  }
4237 
4238  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_HALTREQ, 0);
4239  dmi_write(target, DM_DMCONTROL, dmcontrol);
4240 
4241  if (use_hasel) {
4242  target_list_t *entry;
4243  dm013_info_t *dm = get_dm(target);
4244  if (!dm)
4245  return ERROR_FAIL;
4246  list_for_each_entry(entry, &dm->target_list, list) {
4247  struct target *t = entry->target;
4248  t->state = TARGET_HALTED;
4251  }
4252  }
4253  /* The "else" case is handled in halt_go(). */
4254 
4255  return ERROR_OK;
4256 }
4257 
4258 static int riscv013_resume_go(struct target *target)
4259 {
4260  bool use_hasel = false;
4261  if (select_prepped_harts(target, &use_hasel) != ERROR_OK)
4262  return ERROR_FAIL;
4263 
4264  return riscv013_step_or_resume_current_hart(target, false, use_hasel);
4265 }
4266 
4268 {
4269  return riscv013_step_or_resume_current_hart(target, true, false);
4270 }
4271 
4273 {
4274  return riscv013_on_step_or_resume(target, false);
4275 }
4276 
4277 static int riscv013_on_step(struct target *target)
4278 {
4279  return riscv013_on_step_or_resume(target, true);
4280 }
4281 
4282 static int riscv013_on_halt(struct target *target)
4283 {
4284  return ERROR_OK;
4285 }
4286 
4287 static bool riscv013_is_halted(struct target *target)
4288 {
4289  uint32_t dmstatus;
4290  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
4291  return false;
4292  if (get_field(dmstatus, DM_DMSTATUS_ANYUNAVAIL))
4293  LOG_ERROR("Hart %d is unavailable.", riscv_current_hartid(target));
4294  if (get_field(dmstatus, DM_DMSTATUS_ANYNONEXISTENT))
4295  LOG_ERROR("Hart %d doesn't exist.", riscv_current_hartid(target));
4296  if (get_field(dmstatus, DM_DMSTATUS_ANYHAVERESET)) {
4297  int hartid = riscv_current_hartid(target);
4298  LOG_INFO("Hart %d unexpectedly reset!", hartid);
4299  /* TODO: Can we make this more obvious to eg. a gdb user? */
4300  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE |
4302  dmcontrol = set_hartsel(dmcontrol, hartid);
4303  /* If we had been halted when we reset, request another halt. If we
4304  * ended up running out of reset, then the user will (hopefully) get a
4305  * message that a reset happened, that the target is running, and then
4306  * that it is halted again once the request goes through.
4307  */
4308  if (target->state == TARGET_HALTED)
4309  dmcontrol |= DM_DMCONTROL_HALTREQ;
4310  dmi_write(target, DM_DMCONTROL, dmcontrol);
4311  }
4312  return get_field(dmstatus, DM_DMSTATUS_ALLHALTED);
4313 }
4314 
4316 {
4317  riscv_reg_t dcsr;
4318  int result = register_read(target, &dcsr, GDB_REGNO_DCSR);
4319  if (result != ERROR_OK)
4320  return RISCV_HALT_UNKNOWN;
4321 
4322  LOG_DEBUG("dcsr.cause: 0x%" PRIx64, get_field(dcsr, CSR_DCSR_CAUSE));
4323 
4324  switch (get_field(dcsr, CSR_DCSR_CAUSE)) {
4325  case CSR_DCSR_CAUSE_SWBP:
4326  return RISCV_HALT_BREAKPOINT;
4328  /* We could get here before triggers are enumerated if a trigger was
4329  * already set when we connected. Force enumeration now, which has the
4330  * side effect of clearing any triggers we did not set. */
4332  LOG_DEBUG("{%d} halted because of trigger", target->coreid);
4333  return RISCV_HALT_TRIGGER;
4334  case CSR_DCSR_CAUSE_STEP:
4335  return RISCV_HALT_SINGLESTEP;
4337  case CSR_DCSR_CAUSE_HALT:
4338  return RISCV_HALT_INTERRUPT;
4339  case CSR_DCSR_CAUSE_GROUP:
4340  return RISCV_HALT_GROUP;
4341  }
4342 
4343  LOG_ERROR("Unknown DCSR cause field: 0x%" PRIx64, get_field(dcsr, CSR_DCSR_CAUSE));
4344  LOG_ERROR(" dcsr=0x%016lx", (long)dcsr);
4345  return RISCV_HALT_UNKNOWN;
4346 }
4347 
4348 int riscv013_write_debug_buffer(struct target *target, unsigned int index, riscv_insn_t data)
4349 {
4350  dm013_info_t *dm = get_dm(target);
4351  if (!dm)
4352  return ERROR_FAIL;
4353  if (dm->progbuf_cache[index] != data) {
4354  if (dmi_write(target, DM_PROGBUF0 + index, data) != ERROR_OK)
4355  return ERROR_FAIL;
4356  dm->progbuf_cache[index] = data;
4357  } else {
4358  LOG_DEBUG("cache hit for 0x%" PRIx32 " @%d", data, index);
4359  }
4360  return ERROR_OK;
4361 }
4362 
4364 {
4365  uint32_t value;
4366  dmi_read(target, &value, DM_PROGBUF0 + index);
4367  return value;
4368 }
4369 
4371 {
4372  uint32_t run_program = 0;
4373  run_program = set_field(run_program, AC_ACCESS_REGISTER_AARSIZE, 2);
4374  run_program = set_field(run_program, AC_ACCESS_REGISTER_POSTEXEC, 1);
4375  run_program = set_field(run_program, AC_ACCESS_REGISTER_TRANSFER, 0);
4376  run_program = set_field(run_program, AC_ACCESS_REGISTER_REGNO, 0x1000);
4377 
4378  return execute_abstract_command(target, run_program);
4379 }
4380 
4381 void riscv013_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d)
4382 {
4385  buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, d);
4386  buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
4387 }
4388 
4389 void riscv013_fill_dmi_read_u64(struct target *target, char *buf, int a)
4390 {
4393  buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, 0);
4394  buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, a);
4395 }
4396 
4397 void riscv013_fill_dmi_nop_u64(struct target *target, char *buf)
4398 {
4400  buf_set_u64((unsigned char *)buf, DTM_DMI_OP_OFFSET, DTM_DMI_OP_LENGTH, DMI_OP_NOP);
4401  buf_set_u64((unsigned char *)buf, DTM_DMI_DATA_OFFSET, DTM_DMI_DATA_LENGTH, 0);
4402  buf_set_u64((unsigned char *)buf, DTM_DMI_ADDRESS_OFFSET, info->abits, 0);
4403 }
4404 
4406 {
4408  return info->abits + DTM_DMI_DATA_LENGTH + DTM_DMI_OP_LENGTH;
4409 }
4410 
4412 {
4414  return execute_fence(target);
4415  return ERROR_OK;
4416 }
4417 
4418 /* Helper Functions. */
4420 {
4422  return ERROR_FAIL;
4423 
4424  /* We want to twiddle some bits in the debug CSR so debugging works. */
4425  riscv_reg_t dcsr;
4426  int result = register_read(target, &dcsr, GDB_REGNO_DCSR);
4427  if (result != ERROR_OK)
4428  return result;
4429  dcsr = set_field(dcsr, CSR_DCSR_STEP, step);
4430  dcsr = set_field(dcsr, CSR_DCSR_EBREAKM, riscv_ebreakm);
4431  dcsr = set_field(dcsr, CSR_DCSR_EBREAKS, riscv_ebreaks);
4432  dcsr = set_field(dcsr, CSR_DCSR_EBREAKU, riscv_ebreaku);
4433  return riscv_set_register(target, GDB_REGNO_DCSR, dcsr);
4434 }
4435 
4437  bool step, bool use_hasel)
4438 {
4439  RISCV_INFO(r);
4440  LOG_DEBUG("resuming hart %d (for step?=%d)", r->current_hartid, step);
4441  if (!riscv_is_halted(target)) {
4442  LOG_ERROR("Hart %d is not halted!", r->current_hartid);
4443  return ERROR_FAIL;
4444  }
4445 
4446  /* Issue the resume command, and then wait for the current hart to resume. */
4447  uint32_t dmcontrol = DM_DMCONTROL_DMACTIVE | DM_DMCONTROL_RESUMEREQ;
4448  if (use_hasel)
4449  dmcontrol |= DM_DMCONTROL_HASEL;
4450  dmcontrol = set_hartsel(dmcontrol, r->current_hartid);
4451  dmi_write(target, DM_DMCONTROL, dmcontrol);
4452 
4453  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_HASEL, 0);
4454  dmcontrol = set_field(dmcontrol, DM_DMCONTROL_RESUMEREQ, 0);
4455 
4456  uint32_t dmstatus;
4457  for (size_t i = 0; i < 256; ++i) {
4458  usleep(10);
4459  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
4460  return ERROR_FAIL;
4461  if (get_field(dmstatus, DM_DMSTATUS_ALLRESUMEACK) == 0)
4462  continue;
4463  if (step && get_field(dmstatus, DM_DMSTATUS_ALLHALTED) == 0)
4464  continue;
4465 
4466  dmi_write(target, DM_DMCONTROL, dmcontrol);
4467  return ERROR_OK;
4468  }
4469 
4470  dmi_write(target, DM_DMCONTROL, dmcontrol);
4471 
4472  LOG_ERROR("unable to resume hart %d", r->current_hartid);
4473  if (dmstatus_read(target, &dmstatus, true) != ERROR_OK)
4474  return ERROR_FAIL;
4475  LOG_ERROR(" dmstatus =0x%08x", dmstatus);
4476 
4477  if (step) {
4478  LOG_ERROR(" was stepping, halting");
4479  riscv_halt(target);
4480  return ERROR_OK;
4481  }
4482 
4483  return ERROR_FAIL;
4484 }
4485 
4487 {
4488  /* Wait for busy to go away. */
4489  time_t start = time(NULL);
4490  uint32_t abstractcs;
4491  dmi_read(target, &abstractcs, DM_ABSTRACTCS);
4492  while (get_field(abstractcs, DM_ABSTRACTCS_BUSY)) {
4493  dmi_read(target, &abstractcs, DM_ABSTRACTCS);
4494 
4495  if (time(NULL) - start > riscv_command_timeout_sec) {
4496  LOG_ERROR("abstractcs.busy is not going low after %d seconds "
4497  "(abstractcs=0x%x). The target is either really slow or "
4498  "broken. You could increase the timeout with riscv "
4499  "set_command_timeout_sec.",
4500  riscv_command_timeout_sec, abstractcs);
4501  break;
4502  }
4503  }
4504  /* Clear the error status. */
4506 }
const char * description
Definition: arm_adi_v5.c:1010
const char * name
Definition: armv4_5.c:76
uint32_t riscv_batch_get_dmi_read_data(struct riscv_batch *batch, size_t key)
Definition: batch.c:169
size_t riscv_batch_add_dmi_read(struct riscv_batch *batch, unsigned int address)
Definition: batch.c:143
uint32_t riscv_batch_get_dmi_read_op(struct riscv_batch *batch, size_t key)
Definition: batch.c:159
size_t riscv_batch_available_scans(struct riscv_batch *batch)
Definition: batch.c:224
struct riscv_batch * riscv_batch_alloc(struct target *target, size_t scans, size_t idle)
Definition: batch.c:20
int riscv_batch_run(struct riscv_batch *batch)
Definition: batch.c:88
void riscv_batch_add_dmi_write(struct riscv_batch *batch, unsigned int address, uint64_t data)
Definition: batch.c:130
void riscv_batch_free(struct riscv_batch *batch)
Definition: batch.c:73
bool riscv_batch_full(struct riscv_batch *batch)
Definition: batch.c:83
void buffer_shr(void *_buf, unsigned int buf_len, unsigned int count)
Definition: binarybuffer.c:398
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:141
static int halted(struct target *target, const char *label)
Definition: davinci.c:58
#define DM_ABSTRACTAUTO_AUTOEXECDATA_OFFSET
#define AC_ACCESS_REGISTER_TRANSFER
#define DM_DATA0
#define CSR_DCSR_EBREAKM
#define AC_ACCESS_REGISTER_POSTEXEC
#define DM_AUTHDATA
#define DM_DMCONTROL_ACKHAVERESET
#define DM_DMSTATUS_ANYHAVERESET
#define DM_DMSTATUS_CONFSTRPTRVALID
#define DM_DMSTATUS_ALLHALTED
#define DM_SBCS_SBACCESS64
#define DM_SBCS_SBVERSION
#define DM_DMCONTROL_RESUMEREQ
#define DM_SBDATA3
#define DM_DMCONTROL_HARTSELHI_OFFSET
#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_SBDATA2
#define DM_SBCS
#define DM_SBCS_SBBUSY
#define DM_SBCS_SBBUSYERROR
#define DTM_DTMCS_IDLE
Definition: debug_defines.h:69
#define DM_ABSTRACTCS_CMDERR
#define DM_HARTINFO_DATASIZE
#define AC_ACCESS_REGISTER_REGNO
#define DM_ABSTRACTCS_PROGBUFSIZE
#define DM_SBDATA0
#define DM_PROGBUF1
#define DTM_DMI_OP
#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 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 DM_DMCONTROL_HARTRESET
#define DTM_DMI_OP_OFFSET
#define AC_ACCESS_MEMORY_CMDTYPE
#define DM_HARTINFO_DATAACCESS
#define DTM_DTMCS_VERSION
Definition: debug_defines.h:84
#define CSR_DCSR_EBREAKS
#define DM_DMSTATUS_IMPEBREAK
#define DTM_DTMCS_ABITS
Definition: debug_defines.h:81
#define DM_DMSTATUS_ANYNONEXISTENT
#define DM_DMCONTROL_DMACTIVE
#define DM_DMSTATUS_HASRESETHALTREQ
#define DM_DMSTATUS_ANYRESUMEACK
#define DM_DMCONTROL_HASEL
#define CSR_DCSR_V
#define DTM_DMI_ADDRESS_OFFSET
#define DM_SBCS_SBACCESS8
#define DTM_DTMCS_DMIRESET
Definition: debug_defines.h:51
#define DM_DMSTATUS
#define CSR_DCSR_MPRVEN
#define DM_DMCONTROL_HARTSELHI_LENGTH
#define CSR_DCSR_STEP
#define CSR_DCSR_EBREAKU
#define DM_DMSTATUS_ANYUNAVAIL
#define DM_DMCS2
#define AC_ACCESS_REGISTER_AARSIZE
#define DM_COMMAND
#define DM_SBCS_SBERROR
#define VIRT_PRIV_V
#define DM_DMSTATUS_VERSION
#define DM_DMSTATUS_ANYHALTED
#define DM_DMSTATUS_AUTHBUSY
#define DM_DMCONTROL_HARTSELHI
#define DM_HARTINFO
#define AC_ACCESS_MEMORY_AAMPOSTINCREMENT
#define DM_SBCS_SBACCESS16
#define DM_PROGBUF0
#define DM_ABSTRACTAUTO
#define DM_SBCS_SBREADONADDR
#define DM_DMSTATUS_ALLHAVERESET
#define DM_SBCS_SBACCESS32
#define DTM_DTMCS_DMISTAT
Definition: debug_defines.h:75
#define AC_ACCESS_MEMORY_AAMSIZE
#define CSR_DCSR_PRV
#define DM_SBCS_SBREADONDATA
#define DM_DMSTATUS_ALLNONEXISTENT
#define DM_HARTINFO_DATAADDR
#define DM_DMCONTROL_HALTREQ
#define DTM_DMI_DATA_LENGTH
#define DM_SBADDRESS2
#define DTM_DMI_DATA
#define AC_ACCESS_MEMORY_AAMVIRTUAL
#define DM_DMCONTROL_HARTSELLO_OFFSET
#define DM_DMCS2_GROUP
#define DM_SBCS_SBACCESS128
#define DTM_DMI_OP_LENGTH
#define CSR_DCSR_CAUSE
#define DM_DMSTATUS_ANYRUNNING
#define DM_COMMAND_CMDTYPE
#define DM_HAWINDOW
#define DM_SBADDRESS0
unsigned short width
Definition: embeddedice.c:47
#define DCSR_PRV
Definition: encoding.h:88
#define MSTATUS_VS
Definition: encoding.h:22
#define MSTATUS_MPP
Definition: encoding.h:23
#define CSR_FRM
Definition: encoding.h:2789
#define CSR_VL
Definition: encoding.h:2828
#define MSTATUS_FS
Definition: encoding.h:24
#define CSR_FCSR
Definition: encoding.h:2790
#define CSR_FFLAGS
Definition: encoding.h:2788
#define MSTATUS_MPRV
Definition: encoding.h:26
enum esirisc_reg_num number
Definition: esirisc.c:87
int mask
Definition: esirisc.c:1741
const char * gdb_regno_name(enum gdb_regno regno)
Definition: riscv.c:3515
gdb_regno
Definition: gdb_regs.h:8
@ GDB_REGNO_DPC
Definition: gdb_regs.h:91
@ GDB_REGNO_CSR0
Definition: gdb_regs.h:80
@ GDB_REGNO_MSTATUS
Definition: gdb_regs.h:94
@ GDB_REGNO_VXRM
Definition: gdb_regs.h:83
@ GDB_REGNO_ZERO
Definition: gdb_regs.h:9
@ GDB_REGNO_VTYPE
Definition: gdb_regs.h:86
@ GDB_REGNO_VXSAT
Definition: gdb_regs.h:82
@ GDB_REGNO_S1
Definition: gdb_regs.h:19
@ GDB_REGNO_FPR31
Definition: gdb_regs.h:79
@ GDB_REGNO_FPR0
Definition: gdb_regs.h:46
@ GDB_REGNO_V0
Definition: gdb_regs.h:104
@ GDB_REGNO_VL
Definition: gdb_regs.h:85
@ GDB_REGNO_VSTART
Definition: gdb_regs.h:81
@ GDB_REGNO_XPR31
Definition: gdb_regs.h:43
@ GDB_REGNO_PC
Definition: gdb_regs.h:45
@ GDB_REGNO_S0
Definition: gdb_regs.h:17
@ GDB_REGNO_VLENB
Definition: gdb_regs.h:84
@ GDB_REGNO_V31
Definition: gdb_regs.h:111
@ GDB_REGNO_PRIV
Definition: gdb_regs.h:99
@ GDB_REGNO_S2
Definition: gdb_regs.h:29
@ GDB_REGNO_CSR4095
Definition: gdb_regs.h:98
@ GDB_REGNO_COUNT
Definition: gdb_regs.h:112
@ GDB_REGNO_MISA
Definition: gdb_regs.h:90
@ GDB_REGNO_DCSR
Definition: gdb_regs.h:92
void jtag_add_runtest(unsigned int num_cycles, tap_state_t state)
Goes to TAP_IDLE (if we're not already there), cycle precisely num_cycles in the TAP_IDLE state,...
Definition: jtag/core.c:598
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1043
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:380
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:457
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_IDLE
Definition: jtag.h:53
uint64_t op
Definition: lakemont.c:68
static void list_add(struct list_head *new, struct list_head *head)
Definition: list.h:193
#define list_for_each_entry(p, h, field)
Definition: list.h:151
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:53
void log_printf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format,...)
Definition: log.c:183
int debug_level
Definition: log.c:35
void keep_alive(void)
Definition: log.c:415
static int64_t start
Definition: log.c:42
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:174
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define ERROR_TIMEOUT_REACHED
Definition: log.h:173
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
@ LOG_LVL_DEBUG
Definition: log.h:47
#define s1
Definition: mips32.c:206
#define s2
Definition: mips32.c:207
#define s0
Definition: mips32.c:205
static uint32_t fmv_d_x(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:213
static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset) __attribute__((unused))
Definition: opcodes.h:177
static uint32_t csrr(unsigned int rd, unsigned int csr) __attribute__((unused))
Definition: opcodes.h:141
static uint32_t sw(unsigned int src, unsigned int base, uint16_t offset) __attribute__((unused))
Definition: opcodes.h:81
#define S0
Definition: opcodes.h:7
static uint32_t vmv_x_s(unsigned int rd, unsigned int vs2) __attribute__((unused))
Definition: opcodes.h:316
static uint32_t fld(unsigned int dest, unsigned int base, uint16_t offset) __attribute__((unused))
Definition: opcodes.h:189
static uint32_t vsetvli(unsigned int dest, unsigned int src, uint16_t imm) __attribute__((unused))
Definition: opcodes.h:310
static uint32_t fmv_x_w(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:195
static uint32_t vslide1down_vx(unsigned int vd, unsigned int vs2, unsigned int rs1, unsigned int vm) __attribute__((unused))
Definition: opcodes.h:329
static uint32_t fmv_w_x(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:207
#define ZERO
Definition: opcodes.h:5
static uint32_t auipc(unsigned int dest) __attribute__((unused))
Definition: opcodes.h:304
static uint32_t fmv_x_d(unsigned int dest, unsigned int src) __attribute__((unused))
Definition: opcodes.h:201
int riscv_program_fence_i(struct riscv_program *p)
Definition: program.c:149
int riscv_program_lbr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int offset)
Definition: program.c:120
int riscv_program_write(struct riscv_program *program)
Definition: program.c:32
int riscv_program_fence(struct riscv_program *p)
Definition: program.c:154
int riscv_program_csrrsi(struct riscv_program *p, enum gdb_regno d, unsigned int z, enum gdb_regno csr)
Definition: program.c:125
int riscv_program_addi(struct riscv_program *p, enum gdb_regno d, enum gdb_regno s, int16_t u)
Definition: program.c:170
int riscv_program_ldr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int offset)
Definition: program.c:105
int riscv_program_insert(struct riscv_program *p, riscv_insn_t i)
Definition: program.c:175
int riscv_program_csrr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno csr)
Definition: program.c:137
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:143
int riscv_program_lhr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int offset)
Definition: program.c:115
int riscv_program_ebreak(struct riscv_program *p)
Definition: program.c:159
int riscv_program_swr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int offset)
Definition: program.c:90
int riscv_program_lwr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int offset)
Definition: program.c:110
int riscv_program_sdr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int offset)
Definition: program.c:85
int riscv_program_shr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int offset)
Definition: program.c:95
int riscv_program_exec(struct riscv_program *p, struct target *t)
Add ebreak and execute the program.
Definition: program.c:44
int riscv_program_csrrci(struct riscv_program *p, enum gdb_regno d, unsigned int z, enum gdb_regno csr)
Definition: program.c:131
int riscv_program_sbr(struct riscv_program *p, enum gdb_regno d, enum gdb_regno b, int offset)
Definition: program.c:100
#define MIN(a, b)
Definition: replacements.h:22
slot
Definition: riscv-011.c:122
static int step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
Definition: riscv-011.c:1442
static int dmi_write(struct target *target, uint32_t address, uint32_t value)
Definition: riscv-013.c:670
static int dmi_read_exec(struct target *target, uint32_t *value, uint32_t address)
Definition: riscv-013.c:665
static int register_write_abstract(struct target *target, uint32_t number, uint64_t value, unsigned int size)
Definition: riscv-013.c:930
static int scratch_write64(struct target *target, scratch_mem_t *scratch, uint64_t value)
Definition: riscv-013.c:1240
static int is_fpu_reg(uint32_t gdb_regno)
Definition: riscv-013.c:1064
static void increase_dmi_busy_delay(struct target *target)
Definition: riscv-013.c:449
static int riscv013_step_or_resume_current_hart(struct target *target, bool step, bool use_hasel)
Definition: riscv-013.c:4436
static int dmi_write_exec(struct target *target, uint32_t address, uint32_t value, bool ensure_success)
Definition: riscv-013.c:675
static int maybe_execute_fence_i(struct target *target)
Definition: riscv-013.c:4411
static int register_read_abstract(struct target *target, uint64_t *value, uint32_t number, unsigned int size)
Definition: riscv-013.c:892
static int examine_progbuf(struct target *target)
Definition: riscv-013.c:1005
#define CSR_DCSR_CAUSE_GROUP
Definition: riscv-013.c:83
static int register_read(struct target *target, uint64_t *value, uint32_t number)
Read register value from the target.
Definition: riscv-013.c:1393
static int riscv013_execute_debug_buffer(struct target *target)
Definition: riscv-013.c:4370
static int riscv013_step_current_hart(struct target *target)
Definition: riscv-013.c:4267
static uint32_t set_hartsel(uint32_t initial, uint32_t index)
Definition: riscv-013.c:278
static void dump_field(int idle, const struct scan_field *field)
Definition: riscv-013.c:371
static void select_dmi(struct target *target)
Definition: riscv-013.c:407
static uint32_t sb_sbaccess(unsigned int size_bytes)
Definition: riscv-013.c:2057
static int riscv013_select_current_hart(struct target *target)
Definition: riscv-013.c:4127
static int register_read_direct(struct target *target, uint64_t *value, uint32_t number)
Actually read registers from the target right now.
Definition: riscv-013.c:1410
static dm013_info_t * get_dm(struct target *target)
Return the DM structure for this target.
Definition: riscv-013.c:232
static int write_memory_progbuf(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: riscv-013.c:3788
static int read_memory_bus_word(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: riscv-013.c:2506
dmi_status_t
Definition: riscv-013.c:94
@ DMI_STATUS_SUCCESS
Definition: riscv-013.c:95
@ DMI_STATUS_FAILED
Definition: riscv-013.c:96
@ DMI_STATUS_BUSY
Definition: riscv-013.c:97
static int cleanup_after_vector_access(struct target *target, uint64_t vtype, uint64_t vl)
Definition: riscv-013.c:1939
static int riscv013_set_register_buf(struct target *target, int regno, const uint8_t *value)
Definition: riscv-013.c:2009
static int riscv013_on_step_or_resume(struct target *target, bool step)
Definition: riscv-013.c:4419
static int read_memory_progbuf_inner(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
Read the requested memory, taking care to execute every read exactly once, even if cmderr=busy is enc...
Definition: riscv-013.c:3104
static int execute_fence(struct target *target)
Definition: riscv-013.c:2460
static int riscv013_on_step(struct target *target)
Definition: riscv-013.c:4277
static int select_prepped_harts(struct target *target, bool *use_hasel)
Definition: riscv-013.c:4149
static int read_memory_progbuf(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
Read the requested memory, silently handling memory access errors.
Definition: riscv-013.c:3415
static void log_mem_access_result(struct target *target, bool success, int method, bool read)
Definition: riscv-013.c:2810
#define CMDERR_NOT_SUPPORTED
Definition: riscv-013.c:110
static int dmstatus_read_timeout(struct target *target, uint32_t *dmstatus, bool authenticated, unsigned int timeout_sec)
Definition: riscv-013.c:681
static bool has_sufficient_progbuf(struct target *target, unsigned int size)
Definition: riscv-013.c:1283
#define CSR_DCSR_CAUSE_STEP
Definition: riscv-013.c:81
static int riscv013_dmi_write_u64_bits(struct target *target)
Definition: riscv-013.c:4405
static void riscv013_clear_abstract_error(struct target *target)
Definition: riscv-013.c:4486
static int read_memory_progbuf_one(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: riscv-013.c:3340
#define CMDERR_BUSY
Definition: riscv-013.c:109
static int riscv013_get_register_buf(struct target *target, uint8_t *value, int regno)
Definition: riscv-013.c:1950
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:1133
static bool mem_should_skip_sysbus(struct target *target, target_addr_t address, uint32_t size, uint32_t increment, bool read, char **skip_reason)
Definition: riscv-013.c:2884
struct target_type riscv013_target
Definition: riscv-013.c:4046
static int wait_for_idle(struct target *target, uint32_t *abstractcs)
Definition: riscv-013.c:734
static bool mem_should_skip_progbuf(struct target *target, target_addr_t address, uint32_t size, bool read, char **skip_reason)
Definition: riscv-013.c:2845
yes_no_maybe_t
Definition: riscv-013.c:126
@ YNM_YES
Definition: riscv-013.c:128
@ YNM_MAYBE
Definition: riscv-013.c:127
@ YNM_NO
Definition: riscv-013.c:129
static int write_memory_bus_v0(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: riscv-013.c:3595
enum slot slot_t
static int examine(struct target *target)
Definition: riscv-013.c:1561
dmi_op_t
Definition: riscv-013.c:89
@ DMI_OP_NOP
Definition: riscv-013.c:90
@ DMI_OP_READ
Definition: riscv-013.c:91
@ DMI_OP_WRITE
Definition: riscv-013.c:92
static void log_memory_access(target_addr_t address, uint64_t value, unsigned int size_bytes, bool read)
Definition: riscv-013.c:2477
static int deassert_reset(struct target *target)
Definition: riscv-013.c:2388
memory_space_t
Definition: riscv-013.c:1114
@ SPACE_DMI_PROGBUF
Definition: riscv-013.c:1116
@ SPACE_DM_DATA
Definition: riscv-013.c:1115
@ SPACE_DMI_RAM
Definition: riscv-013.c:1117
#define CSR_DCSR_CAUSE_SWBP
Definition: riscv-013.c:78
static int cleanup_after_register_access(struct target *target, uint64_t mstatus, int regno)
Definition: riscv-013.c:1104
static int prep_for_register_access(struct target *target, uint64_t *mstatus, int regno)
Definition: riscv-013.c:1083
static riscv_reg_t read_abstract_arg(struct target *target, unsigned int index, unsigned int size_bits)
Definition: riscv-013.c:809
static int modify_privilege(struct target *target, uint64_t *mstatus, uint64_t *mstatus_old)
Definition: riscv-013.c:2556
static LIST_HEAD(dm_list)
static int riscv013_halt_go(struct target *target)
Definition: riscv-013.c:4206
static int assert_reset(struct target *target)
Definition: riscv-013.c:2338
@ SLOT0
Definition: riscv-013.c:101
@ SLOT1
Definition: riscv-013.c:102
@ SLOT_LAST
Definition: riscv-013.c:103
static bool riscv013_is_halted(struct target *target)
Definition: riscv-013.c:4287
static uint32_t __attribute__((unused))
Definition: riscv-013.c:719
#define get_field(reg, mask)
Since almost everything can be accomplish by scanning the dbus register, all functions here assume db...
Definition: riscv-013.c:75
static int sb_write_address(struct target *target, target_addr_t address, bool ensure_success)
Definition: riscv-013.c:2075
static void increase_ac_busy_delay(struct target *target)
Definition: riscv-013.c:710
static int riscv013_hart_count(struct target *target)
Definition: riscv-013.c:1828
static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
Definition: riscv-013.c:416
static void decode_dmi(char *text, unsigned int address, unsigned int data)
Definition: riscv-013.c:292
static int discover_vlenb(struct target *target)
Definition: riscv-013.c:1543
static enum riscv_halt_reason riscv013_halt_reason(struct target *target)
Definition: riscv-013.c:4315
static int read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
Definition: riscv-013.c:3531
static int riscv013_write_debug_buffer(struct target *target, unsigned int index, riscv_insn_t d)
Definition: riscv-013.c:4348
static int dmstatus_read(struct target *target, uint32_t *dmstatus, bool authenticated)
Definition: riscv-013.c:703
#define RISCV013_INFO(r)
Definition: riscv-013.c:85
static int write_abstract_arg(struct target *target, unsigned int index, riscv_reg_t value, unsigned int size_bits)
Definition: riscv-013.c:830
static int dmi_read(struct target *target, uint32_t *value, uint32_t address)
Definition: riscv-013.c:660
static int sba_supports_access(struct target *target, unsigned int size_bytes)
Definition: riscv-013.c:2106
static int dmi_op_timeout(struct target *target, uint32_t *data_in, bool *dmi_busy_encountered, int dmi_op, uint32_t address, uint32_t data_out, int timeout_sec, bool exec, bool ensure_success)
Definition: riscv-013.c:556
static int init_target(struct command_context *cmd_ctx, struct target *target)
Definition: riscv-013.c:2273
static int arch_state(struct target *target)
Definition: riscv-013.c:4041
static unsigned int riscv013_data_bits(struct target *target)
Definition: riscv-013.c:1836
static int riscv013_resume_prep(struct target *target)
Definition: riscv-013.c:4272
static int scratch_read64(struct target *target, scratch_mem_t *scratch, uint64_t *value)
Definition: riscv-013.c:1200
static uint32_t access_memory_command(struct target *target, bool virtual, unsigned int width, bool postincrement, bool write)
Definition: riscv-013.c:992
static int riscv013_set_register(struct target *target, int regid, uint64_t value)
Definition: riscv-013.c:4095
static int write_memory_bus_v1(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: riscv-013.c:3648
static riscv013_info_t * get_info(const struct target *target)
Definition: riscv-013.c:219
static void riscv013_fill_dmi_nop_u64(struct target *target, char *buf)
Definition: riscv-013.c:4397
static int riscv013_on_halt(struct target *target)
Definition: riscv-013.c:4282
static int riscv013_authdata_write(struct target *target, uint32_t value, unsigned int index)
Definition: riscv-013.c:1794
static int riscv013_authdata_read(struct target *target, uint32_t *value, unsigned int index)
Definition: riscv-013.c:1781
static uint32_t abstract_memory_size(unsigned int width)
Definition: riscv-013.c:970
static int is_vector_reg(uint32_t gdb_regno)
Definition: riscv-013.c:1072
static int riscv013_halt_prep(struct target *target)
Definition: riscv-013.c:4201
#define CSR_DCSR_CAUSE_DEBUGINT
Definition: riscv-013.c:80
static int set_haltgroup(struct target *target, bool *supported)
Definition: riscv-013.c:1531
static target_addr_t sb_read_address(struct target *target)
Definition: riscv-013.c:2523
static int prep_for_vector_access(struct target *target, uint64_t *vtype, uint64_t *vl, unsigned int *debug_vl)
Definition: riscv-013.c:1905
static unsigned int register_size(struct target *target, unsigned int number)
Return register size in bits.
Definition: riscv-013.c:1273
static int register_write_direct(struct target *target, unsigned int number, uint64_t value)
Immediately write the new value to the requested register.
Definition: riscv-013.c:1295
#define CMDERR_NONE
Definition: riscv-013.c:108
static riscv_insn_t riscv013_read_debug_buffer(struct target *target, unsigned int index)
Definition: riscv-013.c:4363
static int riscv013_resume_go(struct target *target)
Definition: riscv-013.c:4258
static int read_memory_abstract(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
Definition: riscv-013.c:2947
#define CSR_DCSR_CAUSE_TRIGGER
Definition: riscv-013.c:79
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:2125
static int dmi_op(struct target *target, uint32_t *data_in, bool *dmi_busy_encountered, int dmi_op, uint32_t address, uint32_t data_out, bool exec, bool ensure_success)
Definition: riscv-013.c:644
static int batch_run(const struct target *target, struct riscv_batch *batch)
Definition: riscv-013.c:2091
static void deinit_target(struct target *target)
Definition: riscv-013.c:1519
static int wait_for_authbusy(struct target *target, uint32_t *dmstatus)
Definition: riscv-013.c:1494
static int read_sbcs_nonbusy(struct target *target, uint32_t *sbcs)
Definition: riscv-013.c:2539
static COMMAND_HELPER(riscv013_print_info, struct target *target)
Definition: riscv-013.c:1871
static uint32_t access_register_command(struct target *target, uint32_t number, unsigned int size, uint32_t flags)
Definition: riscv-013.c:850
static int write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: riscv-013.c:3980
static int read_memory_bus_v1(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
Read the requested memory using the system bus interface.
Definition: riscv-013.c:2676
static int read_memory_bus_v0(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer, uint32_t increment)
Definition: riscv-013.c:2587
static void riscv013_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d)
Definition: riscv-013.c:4381
#define set_field(reg, mask, val)
Definition: riscv-013.c:76
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:2262
static void riscv013_fill_dmi_read_u64(struct target *target, char *buf, int a)
Definition: riscv-013.c:4389
#define CSR_DCSR_CAUSE_HALT
Definition: riscv-013.c:82
static int write_memory_abstract(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: riscv-013.c:3026
static int scratch_release(struct target *target, scratch_mem_t *scratch)
Definition: riscv-013.c:1194
static dmi_status_t dmi_scan(struct target *target, uint32_t *address_in, uint32_t *data_in, dmi_op_t op, uint32_t address_out, uint32_t data_out, bool exec)
exec: If this is set, assume the scan results in an execution, so more run-test/idle cycles may be re...
Definition: riscv-013.c:464
static bool mem_should_skip_abstract(struct target *target, target_addr_t address, uint32_t size, uint32_t increment, bool read, char **skip_reason)
Definition: riscv-013.c:2913
static int execute_abstract_command(struct target *target, uint32_t command)
Definition: riscv-013.c:771
static int riscv013_get_register(struct target *target, riscv_reg_t *value, int rid)
Definition: riscv-013.c:4066
int riscv_reset_timeout_sec
Definition: riscv.c:206
bool riscv_ebreaks
Definition: riscv.c:210
unsigned int riscv_xlen(const struct target *target)
Definition: riscv.c:3207
int riscv_set_register(struct target *target, enum gdb_regno regid, riscv_reg_t value)
This function is called when the debug user wants to change the value of a register.
Definition: riscv.c:3308
struct scan_field select_dbus
Definition: riscv.c:118
bool riscv_is_halted(struct target *target)
Definition: riscv.c:3372
int riscv_init_registers(struct target *target)
Definition: riscv.c:3790
int riscv_halt(struct target *target)
Definition: riscv.c:1226
bool riscv_ebreakm
Definition: riscv.c:209
void riscv_add_bscan_tunneled_scan(struct target *target, struct scan_field *field, riscv_bscan_tunneled_scan_context_t *ctxt)
Definition: riscv.c:4418
int riscv_current_hartid(const struct target *target)
Definition: riscv.c:3239
int riscv_select_current_hart(struct target *target)
Definition: riscv.c:1161
struct scan_field select_dtmcontrol
Definition: riscv.c:113
uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out)
Definition: riscv.c:293
int riscv_openocd_step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
Definition: riscv.c:2303
int riscv_openocd_poll(struct target *target)
Definition: riscv.c:2184
void select_dmi_via_bscan(struct target *target)
Definition: riscv.c:282
bool riscv_enable_virtual
Definition: riscv.c:213
bool riscv_supports_extension(struct target *target, char letter)
Definition: riscv.c:3194
bool riscv_ebreaku
Definition: riscv.c:211
int riscv_enumerate_triggers(struct target *target)
Count triggers, and initialize trigger_count for each hart.
Definition: riscv.c:3447
int riscv_command_timeout_sec
Definition: riscv.c:203
int riscv_count_harts(struct target *target)
Definition: riscv.c:3245
int bscan_tunnel_ir_width
Definition: riscv.c:129
#define RISCV_SAMPLE_BUF_TIMESTAMP_BEFORE
Definition: riscv.h:67
#define RISCV_INFO(R)
Definition: riscv.h:271
static struct riscv_info * riscv_info(const struct target *target) __attribute__((unused))
Definition: riscv.h:266
@ RISCV_MEM_ACCESS_UNSPECIFIED
Definition: riscv.h:46
@ RISCV_MEM_ACCESS_SYSBUS
Definition: riscv.h:48
@ RISCV_MEM_ACCESS_PROGBUF
Definition: riscv.h:47
@ RISCV_MEM_ACCESS_ABSTRACT
Definition: riscv.h:49
#define RISCV_NUM_MEM_ACCESS_METHODS
Definition: riscv.h:33
uint64_t riscv_reg_t
Definition: riscv.h:41
#define RISCV_MAX_HARTS
Definition: riscv.h:19
uint32_t riscv_insn_t
Definition: riscv.h:42
riscv_halt_reason
Definition: riscv.h:52
@ RISCV_HALT_INTERRUPT
Definition: riscv.h:53
@ RISCV_HALT_BREAKPOINT
Definition: riscv.h:54
@ RISCV_HALT_SINGLESTEP
Definition: riscv.h:55
@ RISCV_HALT_UNKNOWN
Definition: riscv.h:57
@ RISCV_HALT_GROUP
Definition: riscv.h:58
@ RISCV_HALT_TRIGGER
Definition: riscv.h:56
uint64_t riscv_addr_t
Definition: riscv.h:43
struct target * target
Definition: rtt/rtt.c:26
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
int hart_count
Definition: riscv-013.c:137
struct list_head list
Definition: riscv-013.c:133
struct list_head target_list
Definition: riscv-013.c:141
uint32_t progbuf_cache[16]
Definition: riscv-013.c:148
int current_hartid
Definition: riscv-013.c:143
bool hasel_supported
Definition: riscv-013.c:144
unsigned int abs_chain_position
Definition: riscv-013.c:134
bool was_reset
Definition: riscv-013.c:139
unsigned int abs_chain_position
Definition: jtag.h:105
Definition: list.h:40
struct reg * reg_list
Definition: register.h:147
Definition: register.h:111
uint32_t size
Definition: register.h:132
uint8_t * value
Definition: register.h:122
void * arch_info
Definition: register.h:140
unsigned int datacount
Definition: riscv-013.c:162
int16_t dataaddr
Definition: riscv-013.c:208
bool abstract_write_fpr_supported
Definition: riscv-013.c:196
unsigned int hartsellen
Definition: riscv-013.c:211
yes_no_maybe_t has_aampostincrement
Definition: riscv-013.c:198
unsigned int index
Definition: riscv-013.c:158
bool abstract_write_csr_supported
Definition: riscv-013.c:194
yes_no_maybe_t progbuf_writable
Definition: riscv-013.c:169
unsigned int abits
Definition: riscv-013.c:160
uint8_t cmderr
Definition: riscv-013.c:203
unsigned int progbufsize
Definition: riscv-013.c:164
unsigned int bus_master_read_delay
Definition: riscv-013.c:184
uint8_t dataaccess
Definition: riscv-013.c:207
dm013_info_t * dm
Definition: riscv-013.c:214
bool abstract_read_csr_supported
Definition: riscv-013.c:193
bool abstract_read_fpr_supported
Definition: riscv-013.c:195
unsigned int dtmcs_idle
Definition: riscv-013.c:175
uint32_t sbcs
Definition: riscv-013.c:167
unsigned int ac_busy_delay
Definition: riscv-013.c:191
unsigned int dmi_busy_delay
Definition: riscv-013.c:180
riscv_addr_t progbuf_address
Definition: riscv-013.c:171
uint8_t datasize
Definition: riscv-013.c:206
size_t idle_count
Definition: batch.h:27
size_t used_scans
Definition: batch.h:25
bool selected
Definition: riscv.h:138
int xlen
Definition: riscv.h:109
unsigned int vlenb
Definition: riscv.h:112
bool prepped
Definition: riscv.h:136
size_t instruction_count
Definition: program.h:21
unsigned int custom_number
Definition: riscv.h:64
unsigned int size
Definition: riscv.h:72
uint8_t * buf
Definition: riscv.h:70
unsigned int used
Definition: riscv.h:71
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
riscv_addr_t debug_address
Definition: riscv-013.c:1126
riscv_addr_t hart_address
Definition: riscv-013.c:1124
struct working_area * area
Definition: riscv-013.c:1127
memory_space_t memory_space
Definition: riscv-013.c:1122
struct list_head list
Definition: riscv-013.c:152
struct target * target
Definition: riscv-013.c:153
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
const char * name
Name of this type of target.
Definition: target_type.h:31
Definition: target.h:116
int32_t coreid
Definition: target.h:120
struct jtag_tap * tap
Definition: target.h:119
enum target_debug_reason debug_reason
Definition: target.h:154
enum target_state state
Definition: target.h:157
struct reg_cache * reg_cache
Definition: target.h:158
struct rtos * rtos
Definition: target.h:183
unsigned int smp
Definition: target.h:187
void * arch_info
Definition: target.h:164
bool reset_halt
Definition: target.h:144
uint64_t value
Definition: riscv-011.c:168
bool read
Definition: riscv-011.c:169
uint64_t address
Definition: riscv-011.c:165
uint32_t length
Definition: riscv-011.c:166
uint64_t mask
Definition: riscv-011.c:167
int unique_id
Definition: riscv-011.c:170
bool execute
Definition: riscv-011.c:169
bool write
Definition: riscv-011.c:169
target_addr_t address
Definition: target.h:86
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2060
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2118
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:4860
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4667
@ DBG_REASON_NOTHALTED
Definition: target.h:74
@ DBG_REASON_DBGRQ
Definition: target.h:69
@ TARGET_EVENT_RESET_ASSERT
Definition: target.h:264
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:233
@ TARGET_RESET
Definition: target.h:57
@ TARGET_HALTED
Definition: target.h:56
static void target_set_examined(struct target *target)
Sets the examined flag for the given target.
Definition: target.h:443
int64_t timeval_ms(void)
#define TARGET_ADDR_FMT
Definition: types.h:342
#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:335
#define TARGET_PRIxADDR
Definition: types.h:340
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 count[4]
Definition: vdebug.c:22