OpenOCD
riscv-011.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.11. This was never an officially adopted
5  * spec, but SiFive made some silicon that uses it.
6  */
7 
8 #include <assert.h>
9 #include <stdlib.h>
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 
15 #include "riscv-011.h"
16 
17 #include "target/target.h"
18 #include "target/algorithm.h"
19 #include "target/target_type.h"
20 #include <helper/log.h>
21 #include "jtag/jtag.h"
22 #include "target/register.h"
23 #include "target/breakpoints.h"
24 #include "helper/time_support.h"
25 #include "riscv.h"
26 #include "riscv_reg.h"
27 #include "riscv-011_reg.h"
28 #include "gdb_regs.h"
29 #include "field_helpers.h"
30 
73 static int handle_halt(struct target *target, bool announce);
74 
75 /* Constants for legacy SiFive hardware breakpoints. */
76 #define CSR_BPCONTROL_X (1<<0)
77 #define CSR_BPCONTROL_W (1<<1)
78 #define CSR_BPCONTROL_R (1<<2)
79 #define CSR_BPCONTROL_U (1<<3)
80 #define CSR_BPCONTROL_S (1<<4)
81 #define CSR_BPCONTROL_H (1<<5)
82 #define CSR_BPCONTROL_M (1<<6)
83 #define CSR_BPCONTROL_BPMATCH (0xf<<7)
84 #define CSR_BPCONTROL_BPACTION (0xff<<11)
85 
86 #define DEBUG_ROM_START 0x800
87 #define DEBUG_ROM_RESUME (DEBUG_ROM_START + 4)
88 #define DEBUG_ROM_EXCEPTION (DEBUG_ROM_START + 8)
89 #define DEBUG_RAM_START 0x400
90 
91 #define SETHALTNOT 0x10c
92 
93 /*** JTAG registers. ***/
94 
95 #define DTMCONTROL 0x10
96 #define DTMCONTROL_DBUS_RESET (1<<16)
97 #define DTMCONTROL_IDLE (7<<10)
98 #define DTMCONTROL_ADDRBITS (0xf<<4)
99 #define DTMCONTROL_VERSION (0xf)
100 
101 #define DBUS 0x11
102 #define DBUS_OP_START 0
103 #define DBUS_OP_SIZE 2
104 typedef enum {
107  DBUS_OP_WRITE = 2
109 typedef enum {
112  DBUS_STATUS_BUSY = 3
114 #define DBUS_DATA_START 2
115 #define DBUS_DATA_SIZE 34
116 #define DBUS_ADDRESS_START 36
117 
118 typedef enum {
121  RE_AGAIN
123 
124 typedef enum slot {
129 
130 /*** Debug Bus registers. ***/
131 
132 #define DMCONTROL 0x10
133 #define DMCONTROL_INTERRUPT (((uint64_t)1)<<33)
134 #define DMCONTROL_HALTNOT (((uint64_t)1)<<32)
135 #define DMCONTROL_BUSERROR (7<<19)
136 #define DMCONTROL_SERIAL (3<<16)
137 #define DMCONTROL_AUTOINCREMENT (1<<15)
138 #define DMCONTROL_ACCESS (7<<12)
139 #define DMCONTROL_HARTID (0x3ff<<2)
140 #define DMCONTROL_NDRESET (1<<1)
141 #define DMCONTROL_FULLRESET 1
142 
143 #define DMINFO 0x11
144 #define DMINFO_ABUSSIZE (0x7fU<<25)
145 #define DMINFO_SERIALCOUNT (0xf<<21)
146 #define DMINFO_ACCESS128 (1<<20)
147 #define DMINFO_ACCESS64 (1<<19)
148 #define DMINFO_ACCESS32 (1<<18)
149 #define DMINFO_ACCESS16 (1<<17)
150 #define DMINFO_ACCESS8 (1<<16)
151 #define DMINFO_DRAMSIZE (0x3f<<10)
152 #define DMINFO_AUTHENTICATED (1<<5)
153 #define DMINFO_AUTHBUSY (1<<4)
154 #define DMINFO_AUTHTYPE (3<<2)
155 #define DMINFO_VERSION 3
156 
157 #define DMAUTHDATA0 0x12
158 #define DMAUTHDATA1 0x13
159 
160 /*** Info about the core being debugged. ***/
161 
162 #define DBUS_ADDRESS_UNKNOWN 0xffff
163 
164 #define DRAM_CACHE_SIZE 16
165 
167  uint32_t data;
168  bool valid;
169  bool dirty;
170 };
171 
172 typedef struct {
173  /* Number of address bits in the dbus register. */
174  uint8_t addrbits;
175  /* Number of words in Debug RAM. */
176  unsigned int dramsize;
177  uint64_t dcsr;
178  uint64_t dpc;
179  uint64_t tselect;
181  /* The value that mstatus actually has on the target right now. This is not
182  * the value we present to the user. That one may be stored in the
183  * reg_cache. */
184  uint64_t mstatus_actual;
185 
186  struct memory_cache_line dram_cache[DRAM_CACHE_SIZE];
187 
188  /* Number of run-test/idle cycles the target requests we do after each dbus
189  * access. */
190  unsigned int dtmcontrol_idle;
191 
192  /* This value is incremented every time a dbus access comes back as "busy".
193  * It's used to determine how many run-test/idle cycles to feed the target
194  * in between accesses. */
195  unsigned int dbus_busy_delay;
196 
197  /* This value is incremented every time we read the debug interrupt as
198  * high. It's used to add extra run-test/idle cycles after setting debug
199  * interrupt high, so ideally we never have to perform a whole extra scan
200  * before the interrupt is cleared. */
201  unsigned int interrupt_high_delay;
202 
205 
206 typedef struct {
207  bool haltnot;
208  bool interrupt;
209 } bits_t;
210 
211 /*** Necessary prototypes. ***/
212 
213 static int poll_target(struct target *target, bool announce);
214 static int riscv011_poll(struct target *target);
215 
216 /*** Utility functions. ***/
217 
218 #define DEBUG_LENGTH 264
219 
220 static riscv011_info_t *get_info(const struct target *target)
221 {
222  struct riscv_info *info = target->arch_info;
223  assert(info);
224  assert(info->version_specific);
225  return info->version_specific;
226 }
227 
228 static unsigned int slot_offset(const struct target *target, slot_t slot)
229 {
231  switch (riscv_xlen(target)) {
232  case 32:
233  switch (slot) {
234  case SLOT0:
235  return 4;
236  case SLOT1:
237  return 5;
238  case SLOT_LAST:
239  return info->dramsize - 1;
240  }
241  break;
242  case 64:
243  switch (slot) {
244  case SLOT0:
245  return 4;
246  case SLOT1:
247  return 6;
248  case SLOT_LAST:
249  return info->dramsize - 2;
250  }
251  }
252  LOG_ERROR("slot_offset called with xlen=%d, slot=%d",
254  assert(0);
255  return 0; /* Silence -Werror=return-type */
256 }
257 
258 static uint32_t load(const struct target *target, unsigned int rd,
259  unsigned int base, int16_t offset)
260 {
261  switch (riscv_xlen(target)) {
262  case 32:
263  return lw(rd, base, offset);
264  case 64:
265  return ld(rd, base, offset);
266  }
267  assert(0);
268  return 0; /* Silence -Werror=return-type */
269 }
270 
271 static uint32_t store(const struct target *target, unsigned int src,
272  unsigned int base, int16_t offset)
273 {
274  switch (riscv_xlen(target)) {
275  case 32:
276  return sw(src, base, offset);
277  case 64:
278  return sd(src, base, offset);
279  }
280  assert(0);
281  return 0; /* Silence -Werror=return-type */
282 }
283 
284 static uint32_t load_slot(const struct target *target, unsigned int dest,
285  slot_t slot)
286 {
287  unsigned int offset = DEBUG_RAM_START + 4 * slot_offset(target, slot);
288  assert(offset <= MAX_INT12);
289  return load(target, dest, ZERO, (int16_t)offset);
290 }
291 
292 static uint32_t store_slot(const struct target *target, unsigned int src,
293  slot_t slot)
294 {
295  unsigned int offset = DEBUG_RAM_START + 4 * slot_offset(target, slot);
296  assert(offset <= MAX_INT12);
297  return store(target, src, ZERO, (int16_t)offset);
298 }
299 
300 static uint16_t dram_address(unsigned int index)
301 {
302  if (index < 0x10)
303  return index;
304  else
305  return 0x40 + index - 0x10;
306 }
307 
308 static uint32_t idcode_scan(struct target *target)
309 {
310  struct scan_field field;
311  uint8_t in_value[4];
312 
314 
315  field.num_bits = 32;
316  field.out_value = NULL;
317  field.in_value = in_value;
318  jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
319 
320  int retval = jtag_execute_queue();
321  if (retval != ERROR_OK) {
322  LOG_ERROR("failed jtag scan: %d", retval);
323  return retval;
324  }
325 
326  /* Always return to dbus. */
328 
329  uint32_t in = buf_get_u32(field.in_value, 0, 32);
330  LOG_DEBUG("IDCODE: 0x0 -> 0x%x", in);
331 
332  return in;
333 }
334 
336 {
338  info->dbus_busy_delay += info->dbus_busy_delay / 10 + 1;
339  LOG_DEBUG("dtmcontrol_idle=%d, dbus_busy_delay=%d, interrupt_high_delay=%d",
340  info->dtmcontrol_idle, info->dbus_busy_delay,
341  info->interrupt_high_delay);
342 
343  dtmcs_scan(target->tap, DTMCONTROL_DBUS_RESET, NULL /* discard value */);
344 }
345 
347 {
349  info->interrupt_high_delay += info->interrupt_high_delay / 10 + 1;
350  LOG_DEBUG("dtmcontrol_idle=%d, dbus_busy_delay=%d, interrupt_high_delay=%d",
351  info->dtmcontrol_idle, info->dbus_busy_delay,
352  info->interrupt_high_delay);
353 }
354 
355 static void add_dbus_scan(const struct target *target, struct scan_field *field,
356  uint8_t *out_value, uint8_t *in_value, dbus_op_t op,
357  uint16_t address, uint64_t data)
358 {
360  RISCV_INFO(r);
361 
362  if (r->reset_delays_wait >= 0) {
363  r->reset_delays_wait--;
364  if (r->reset_delays_wait < 0) {
365  info->dbus_busy_delay = 0;
366  info->interrupt_high_delay = 0;
367  }
368  }
369 
370  field->num_bits = info->addrbits + DBUS_OP_SIZE + DBUS_DATA_SIZE;
371  field->in_value = in_value;
372  field->out_value = out_value;
373 
377 
378  jtag_add_dr_scan(target->tap, 1, field, TAP_IDLE);
379 
380  int idle_count = info->dtmcontrol_idle + info->dbus_busy_delay;
381  if (data & DMCONTROL_INTERRUPT)
382  idle_count += info->interrupt_high_delay;
383 
384  if (idle_count)
385  jtag_add_runtest(idle_count, TAP_IDLE);
386 }
387 
388 static void dump_field(const struct scan_field *field)
389 {
390  static const char * const op_string[] = {"nop", "r", "w", "?"};
391  static const char * const status_string[] = {"+", "?", "F", "b"};
392 
394  return;
395 
396  uint64_t out = buf_get_u64(field->out_value, 0, field->num_bits);
397  unsigned int out_op = (out >> DBUS_OP_START) & ((1 << DBUS_OP_SIZE) - 1);
398  char out_interrupt = ((out >> DBUS_DATA_START) & DMCONTROL_INTERRUPT) ? 'i' : '.';
399  char out_haltnot = ((out >> DBUS_DATA_START) & DMCONTROL_HALTNOT) ? 'h' : '.';
400  unsigned int out_data = out >> 2;
401  unsigned int out_address = out >> DBUS_ADDRESS_START;
402  uint64_t in = buf_get_u64(field->in_value, 0, field->num_bits);
403  unsigned int in_op = (in >> DBUS_OP_START) & ((1 << DBUS_OP_SIZE) - 1);
404  char in_interrupt = ((in >> DBUS_DATA_START) & DMCONTROL_INTERRUPT) ? 'i' : '.';
405  char in_haltnot = ((in >> DBUS_DATA_START) & DMCONTROL_HALTNOT) ? 'h' : '.';
406  unsigned int in_data = in >> 2;
407  unsigned int in_address = in >> DBUS_ADDRESS_START;
408 
410  __FILE__, __LINE__, "scan",
411  "%ub %s %c%c:%08x @%02x -> %s %c%c:%08x @%02x",
412  field->num_bits,
413  op_string[out_op], out_interrupt, out_haltnot, out_data,
414  out_address,
415  status_string[in_op], in_interrupt, in_haltnot, in_data,
416  in_address);
417 }
418 
419 static dbus_status_t dbus_scan(struct target *target, uint16_t *address_in,
420  uint64_t *data_in, dbus_op_t op, uint16_t address_out, uint64_t data_out)
421 {
423  uint8_t in[8] = {0};
424  uint8_t out[8] = {0};
425  struct scan_field field = {
426  .num_bits = info->addrbits + DBUS_OP_SIZE + DBUS_DATA_SIZE,
427  .out_value = out,
428  .in_value = in
429  };
430  if (address_in)
431  *address_in = 0;
432 
433  if (info->addrbits == 0) {
434  LOG_TARGET_ERROR(target, "Can't access DMI because addrbits=0.");
435  return DBUS_STATUS_FAILED;
436  }
437 
439  buf_set_u64(out, DBUS_DATA_START, DBUS_DATA_SIZE, data_out);
440  buf_set_u64(out, DBUS_ADDRESS_START, info->addrbits, address_out);
441 
442  /* Assume dbus is already selected. */
443  jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
444 
445  int idle_count = info->dtmcontrol_idle + info->dbus_busy_delay;
446 
447  if (idle_count)
448  jtag_add_runtest(idle_count, TAP_IDLE);
449 
450  int retval = jtag_execute_queue();
451  if (retval != ERROR_OK) {
452  LOG_ERROR("dbus_scan failed jtag scan");
453  return DBUS_STATUS_FAILED;
454  }
455 
456  if (data_in)
457  *data_in = buf_get_u64(in, DBUS_DATA_START, DBUS_DATA_SIZE);
458 
459  if (address_in)
460  *address_in = buf_get_u32(in, DBUS_ADDRESS_START, info->addrbits);
461 
462  dump_field(&field);
463 
465 }
466 
467 static uint64_t dbus_read(struct target *target, uint16_t address)
468 {
469  uint64_t value;
471  uint16_t address_in;
472 
473  /* If the previous read/write was to the same address, we will get the read data
474  * from the previous access.
475  * While somewhat nonintuitive, this is an efficient way to get the data.
476  */
477 
478  unsigned int i = 0;
479  do {
480  status = dbus_scan(target, &address_in, &value, DBUS_OP_READ, address, 0);
481  if (status == DBUS_STATUS_BUSY)
483  if (status == DBUS_STATUS_FAILED) {
484  LOG_ERROR("dbus_read(0x%x) failed!", address);
485  return 0;
486  }
487  } while (((status == DBUS_STATUS_BUSY) || (address_in != address)) &&
488  i++ < 256);
489 
491  LOG_ERROR("failed read from 0x%x; value=0x%" PRIx64 ", status=%d\n", address, value, status);
492 
493  return value;
494 }
495 
496 static void dbus_write(struct target *target, uint16_t address, uint64_t value)
497 {
499  unsigned int i = 0;
500  while (status == DBUS_STATUS_BUSY && i++ < 256) {
502  if (status == DBUS_STATUS_BUSY)
504  }
506  LOG_ERROR("failed to write 0x%" PRIx64 " to 0x%x; status=%d\n", value, address, status);
507 }
508 
509 /*** scans "class" ***/
510 
511 typedef struct {
512  /* Number of scans that space is reserved for. */
513  unsigned int scan_count;
514  /* Size reserved in memory for each scan, in bytes. */
515  unsigned int scan_size;
516  unsigned int next_scan;
517  uint8_t *in;
518  uint8_t *out;
519  struct scan_field *field;
520  const struct target *target;
521 } scans_t;
522 
523 static scans_t *scans_new(struct target *target, unsigned int scan_count)
524 {
525  scans_t *scans = malloc(sizeof(scans_t));
526  if (!scans)
527  goto error0;
528  scans->scan_count = scan_count;
529  /* This code also gets called before xlen is detected. */
530  if (riscv_xlen(target))
531  scans->scan_size = 2 + riscv_xlen(target) / 8;
532  else
533  scans->scan_size = 2 + 128 / 8;
534  scans->next_scan = 0;
535  scans->in = calloc(scans->scan_size, scans->scan_count);
536  if (!scans->in)
537  goto error1;
538  scans->out = calloc(scans->scan_size, scans->scan_count);
539  if (!scans->out)
540  goto error2;
541  scans->field = calloc(scans->scan_count, sizeof(struct scan_field));
542  if (!scans->field)
543  goto error3;
544  scans->target = target;
545  return scans;
546 
547 error3:
548  free(scans->out);
549 error2:
550  free(scans->in);
551 error1:
552  free(scans);
553 error0:
554  return NULL;
555 }
556 
557 static scans_t *scans_delete(scans_t *scans)
558 {
559  assert(scans);
560  free(scans->field);
561  free(scans->out);
562  free(scans->in);
563  free(scans);
564  return NULL;
565 }
566 
567 static void scans_reset(scans_t *scans)
568 {
569  scans->next_scan = 0;
570 }
571 
572 static void scans_dump(scans_t *scans)
573 {
574  for (unsigned int i = 0; i < scans->next_scan; i++)
575  dump_field(&scans->field[i]);
576 }
577 
578 static int scans_execute(scans_t *scans)
579 {
580  int retval = jtag_execute_queue();
581  if (retval != ERROR_OK) {
582  LOG_ERROR("failed jtag scan: %d", retval);
583  return retval;
584  }
585 
586  scans_dump(scans);
587 
588  return ERROR_OK;
589 }
590 
592 static void scans_add_write32(scans_t *scans, uint16_t address, uint32_t data,
593  bool set_interrupt)
594 {
595  const unsigned int i = scans->next_scan;
596  int data_offset = scans->scan_size * i;
597  add_dbus_scan(scans->target, &scans->field[i], scans->out + data_offset,
598  scans->in + data_offset, DBUS_OP_WRITE, address,
599  (set_interrupt ? DMCONTROL_INTERRUPT : 0) | DMCONTROL_HALTNOT | data);
600  scans->next_scan++;
601  assert(scans->next_scan <= scans->scan_count);
602 }
603 
606 static void scans_add_write_jump(scans_t *scans, uint16_t address,
607  bool set_interrupt)
608 {
609  unsigned int jump_offset = DEBUG_ROM_RESUME - (DEBUG_RAM_START + 4 * address);
610  assert(jump_offset <= MAX_INT21);
611  scans_add_write32(scans, address, jal(0, (int32_t)jump_offset), set_interrupt);
612 }
613 
616 static void scans_add_write_load(scans_t *scans, uint16_t address,
617  unsigned int reg, slot_t slot, bool set_interrupt)
618 {
619  scans_add_write32(scans, address, load_slot(scans->target, reg, slot),
620  set_interrupt);
621 }
622 
625 static void scans_add_write_store(scans_t *scans, uint16_t address,
626  unsigned int reg, slot_t slot, bool set_interrupt)
627 {
629  set_interrupt);
630 }
631 
633 static void scans_add_read32(scans_t *scans, uint16_t address, bool set_interrupt)
634 {
635  assert(scans->next_scan < scans->scan_count);
636  const unsigned int i = scans->next_scan;
637  int data_offset = scans->scan_size * i;
638  add_dbus_scan(scans->target, &scans->field[i], scans->out + data_offset,
639  scans->in + data_offset, DBUS_OP_READ, address,
640  (set_interrupt ? DMCONTROL_INTERRUPT : 0) | DMCONTROL_HALTNOT);
641  scans->next_scan++;
642 }
643 
645 static void scans_add_read(scans_t *scans, slot_t slot, bool set_interrupt)
646 {
647  const struct target *target = scans->target;
648  switch (riscv_xlen(target)) {
649  case 32:
650  scans_add_read32(scans, slot_offset(target, slot), set_interrupt);
651  break;
652  case 64:
653  scans_add_read32(scans, slot_offset(target, slot), false);
654  scans_add_read32(scans, slot_offset(target, slot) + 1, set_interrupt);
655  break;
656  }
657 }
658 
659 static uint32_t scans_get_u32(scans_t *scans, unsigned int index,
660  unsigned int first, unsigned int num)
661 {
662  return buf_get_u32(scans->in + scans->scan_size * index, first, num);
663 }
664 
665 static uint64_t scans_get_u64(scans_t *scans, unsigned int index,
666  unsigned int first, unsigned int num)
667 {
668  return buf_get_u64(scans->in + scans->scan_size * index, first, num);
669 }
670 
671 /*** end of scans class ***/
672 
673 static uint32_t dram_read32(struct target *target, unsigned int index)
674 {
675  uint16_t address = dram_address(index);
676  uint32_t value = dbus_read(target, address);
677  return value;
678 }
679 
680 static void dram_write32(struct target *target, unsigned int index, uint32_t value,
681  bool set_interrupt)
682 {
683  uint64_t dbus_value = DMCONTROL_HALTNOT | value;
684  if (set_interrupt)
685  dbus_value |= DMCONTROL_INTERRUPT;
686  dbus_write(target, dram_address(index), dbus_value);
687 }
688 
690 static int read_bits(struct target *target, bits_t *result)
691 {
692  uint64_t value;
694  uint16_t address_in;
696 
697  do {
698  unsigned int i = 0;
699  do {
700  status = dbus_scan(target, &address_in, &value, DBUS_OP_READ, 0, 0);
701  if (status == DBUS_STATUS_BUSY) {
702  if (address_in == (1<<info->addrbits) - 1 &&
703  value == (1ULL<<DBUS_DATA_SIZE) - 1) {
704  LOG_ERROR("TDO seems to be stuck high.");
705  return ERROR_FAIL;
706  }
708  }
709  } while (status == DBUS_STATUS_BUSY && i++ < 256);
710 
711  if (status != DBUS_STATUS_SUCCESS) {
712  LOG_ERROR("Failed to read from 0x%x; status=%d", address_in, status);
713  return ERROR_FAIL;
714  }
715  } while (address_in > 0x10 && address_in != DMCONTROL);
716 
717  if (result) {
718  result->haltnot = get_field(value, DMCONTROL_HALTNOT);
719  result->interrupt = get_field(value, DMCONTROL_INTERRUPT);
720  }
721  return ERROR_OK;
722 }
723 
724 static int wait_for_debugint_clear(struct target *target, bool ignore_first)
725 {
726  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
727  if (ignore_first) {
728  /* Throw away the results of the first read, since they'll contain the
729  * result of the read that happened just before debugint was set.
730  * (Assuming the last scan before calling this function was one that
731  * sets debugint.) */
733  }
734  while (1) {
735  bits_t bits = {
736  .haltnot = 0,
737  .interrupt = 0
738  };
739  if (read_bits(target, &bits) != ERROR_OK)
740  return ERROR_FAIL;
741 
742  if (!bits.interrupt)
743  return ERROR_OK;
744  if (timeval_ms() > then) {
745  LOG_ERROR("Timed out waiting for debug int to clear."
746  "Increase timeout with riscv set_command_timeout_sec.");
747  return ERROR_FAIL;
748  }
749  }
750 }
751 
752 static int dram_check32(struct target *target, unsigned int index,
753  uint32_t expected)
754 {
755  uint16_t address = dram_address(index);
756  uint32_t actual = dbus_read(target, address);
757  if (expected != actual) {
758  LOG_ERROR("Wrote 0x%x to Debug RAM at %d, but read back 0x%x",
759  expected, index, actual);
760  return ERROR_FAIL;
761  }
762  return ERROR_OK;
763 }
764 
765 static void cache_set32(struct target *target, unsigned int index, uint32_t data)
766 {
768  if (info->dram_cache[index].valid &&
769  info->dram_cache[index].data == data) {
770  /* This is already preset on the target. */
771  LOG_DEBUG("cache[0x%x] = 0x%08x: DASM(0x%x) (hit)", index, data, data);
772  return;
773  }
774  LOG_DEBUG("cache[0x%x] = 0x%08x: DASM(0x%x)", index, data, data);
775  info->dram_cache[index].data = data;
776  info->dram_cache[index].valid = true;
777  info->dram_cache[index].dirty = true;
778 }
779 
780 static void cache_set(struct target *target, slot_t slot, uint64_t data)
781 {
782  unsigned int offset = slot_offset(target, slot);
783  cache_set32(target, offset, data);
784  if (riscv_xlen(target) > 32)
785  cache_set32(target, offset + 1, data >> 32);
786 }
787 
788 static void cache_set_jump(struct target *target, unsigned int index)
789 {
790  unsigned int jump_offset = DEBUG_ROM_RESUME - (DEBUG_RAM_START + 4 * index);
791  assert(jump_offset <= MAX_INT21);
792  cache_set32(target, index, jal(0, (int32_t)jump_offset));
793 }
794 
795 static void cache_set_load(struct target *target, unsigned int index,
796  unsigned int reg, slot_t slot)
797 {
798  unsigned int offset = DEBUG_RAM_START + 4 * slot_offset(target, slot);
799  assert(offset <= MAX_INT12);
800  cache_set32(target, index, load(target, reg, ZERO, (int16_t)offset));
801 }
802 
803 static void cache_set_store(struct target *target, unsigned int index,
804  unsigned int reg, slot_t slot)
805 {
806  unsigned int offset = DEBUG_RAM_START + 4 * slot_offset(target, slot);
807  assert(offset <= MAX_INT12);
808  cache_set32(target, index, store(target, reg, ZERO, (int16_t)offset));
809 }
810 
811 static void dump_debug_ram(struct target *target)
812 {
813  for (unsigned int i = 0; i < DRAM_CACHE_SIZE; i++) {
814  uint32_t value = dram_read32(target, i);
815  LOG_ERROR("Debug RAM 0x%x: 0x%08x", i, value);
816  }
817 }
818 
819 /* Call this if the code you just ran writes to debug RAM entries 0 through 3. */
820 static void cache_invalidate(struct target *target)
821 {
823  for (unsigned int i = 0; i < info->dramsize; i++) {
824  info->dram_cache[i].valid = false;
825  info->dram_cache[i].dirty = false;
826  }
827 }
828 
829 /* Called by cache_write() after the program has run. Also call this if you're
830  * running programs without calling cache_write(). */
831 static void cache_clean(struct target *target)
832 {
834  for (unsigned int i = 0; i < info->dramsize; i++) {
835  if (i >= 4)
836  info->dram_cache[i].valid = false;
837  info->dram_cache[i].dirty = false;
838  }
839 }
840 
841 static int cache_check(struct target *target)
842 {
844  int error = 0;
845 
846  for (unsigned int i = 0; i < info->dramsize; i++) {
847  if (info->dram_cache[i].valid && !info->dram_cache[i].dirty) {
848  if (dram_check32(target, i, info->dram_cache[i].data) != ERROR_OK)
849  error++;
850  }
851  }
852 
853  if (error) {
855  return ERROR_FAIL;
856  }
857 
858  return ERROR_OK;
859 }
860 
863 #define CACHE_NO_READ 128
864 static int cache_write(struct target *target, unsigned int address, bool run)
865 {
866  LOG_DEBUG("enter");
868  scans_t *scans = scans_new(target, info->dramsize + 2);
869  if (!scans)
870  return ERROR_FAIL;
871 
872  unsigned int last = info->dramsize;
873  for (unsigned int i = 0; i < info->dramsize; i++) {
874  if (info->dram_cache[i].dirty)
875  last = i;
876  }
877 
878  if (last == info->dramsize) {
879  /* Nothing needs to be written to RAM. */
881 
882  } else {
883  for (unsigned int i = 0; i < info->dramsize; i++) {
884  if (info->dram_cache[i].dirty) {
885  bool set_interrupt = (i == last && run);
886  scans_add_write32(scans, i, info->dram_cache[i].data,
887  set_interrupt);
888  }
889  }
890  }
891 
892  if (run || address < CACHE_NO_READ) {
893  /* Throw away the results of the first read, since it'll contain the
894  * result of the read that happened just before debugint was set. */
895  scans_add_read32(scans, address, false);
896 
897  /* This scan contains the results of the read the caller requested, as
898  * well as an interrupt bit worth looking at. */
899  scans_add_read32(scans, address, false);
900  }
901 
902  int retval = scans_execute(scans);
903  if (retval != ERROR_OK) {
904  scans_delete(scans);
905  LOG_ERROR("JTAG execute failed.");
906  return retval;
907  }
908 
909  int errors = 0;
910  for (unsigned int i = 0; i < scans->next_scan; i++) {
912  DBUS_OP_SIZE);
913  switch (status) {
914  case DBUS_STATUS_SUCCESS:
915  break;
916  case DBUS_STATUS_FAILED:
917  LOG_ERROR("Debug RAM write failed. Hardware error?");
918  scans_delete(scans);
919  return ERROR_FAIL;
920  case DBUS_STATUS_BUSY:
921  errors++;
922  break;
923  default:
924  LOG_ERROR("Got invalid bus access status: %d", status);
925  scans_delete(scans);
926  return ERROR_FAIL;
927  }
928  }
929 
930  if (errors) {
932 
933  /* Try again, using the slow careful code.
934  * Write all RAM, just to be extra cautious. */
935  for (unsigned int i = 0; i < info->dramsize; i++) {
936  if (i == last && run)
937  dram_write32(target, last, info->dram_cache[last].data, true);
938  else
939  dram_write32(target, i, info->dram_cache[i].data, false);
940  info->dram_cache[i].dirty = false;
941  }
942  if (run)
944 
945  if (wait_for_debugint_clear(target, true) != ERROR_OK) {
946  LOG_ERROR("Debug interrupt didn't clear.");
948  scans_delete(scans);
949  return ERROR_FAIL;
950  }
951 
952  } else {
953  if (run) {
955  } else {
956  for (unsigned int i = 0; i < info->dramsize; i++)
957  info->dram_cache[i].dirty = false;
958  }
959 
960  if (run || address < CACHE_NO_READ) {
961  int interrupt = scans_get_u32(scans, scans->next_scan-1,
962  DBUS_DATA_START + 33, 1);
963  if (interrupt) {
965  /* Slow path wait for it to clear. */
966  if (wait_for_debugint_clear(target, false) != ERROR_OK) {
967  LOG_ERROR("Debug interrupt didn't clear.");
969  scans_delete(scans);
970  return ERROR_FAIL;
971  }
972  } else {
973  /* We read a useful value in that last scan. */
974  unsigned int read_addr = scans_get_u32(scans, scans->next_scan-1,
975  DBUS_ADDRESS_START, info->addrbits);
976  if (read_addr != address) {
977  LOG_INFO("Got data from 0x%x but expected it from 0x%x",
978  read_addr, address);
979  }
980  info->dram_cache[read_addr].data =
981  scans_get_u32(scans, scans->next_scan-1, DBUS_DATA_START, 32);
982  info->dram_cache[read_addr].valid = true;
983  }
984  }
985  }
986 
987  scans_delete(scans);
988  LOG_DEBUG("exit");
989 
990  return ERROR_OK;
991 }
992 
993 static uint32_t cache_get32(struct target *target, unsigned int address)
994 {
996  if (!info->dram_cache[address].valid) {
997  info->dram_cache[address].data = dram_read32(target, address);
998  info->dram_cache[address].valid = true;
999  }
1000  return info->dram_cache[address].data;
1001 }
1002 
1003 static uint64_t cache_get(struct target *target, slot_t slot)
1004 {
1005  unsigned int offset = slot_offset(target, slot);
1006  uint64_t value = cache_get32(target, offset);
1007  if (riscv_xlen(target) > 32)
1008  value |= ((uint64_t) cache_get32(target, offset + 1)) << 32;
1009  return value;
1010 }
1011 
1012 /* Write instruction that jumps from the specified word in Debug RAM to resume
1013  * in Debug ROM. */
1014 static void dram_write_jump(struct target *target, unsigned int index,
1015  bool set_interrupt)
1016 {
1017  unsigned int jump_offset = DEBUG_ROM_RESUME - (DEBUG_RAM_START + 4 * index);
1018  assert(jump_offset <= MAX_INT21);
1019  dram_write32(target, index, jal(0, (int32_t)jump_offset), set_interrupt);
1020 }
1021 
1022 static int wait_for_state(struct target *target, enum target_state state)
1023 {
1024  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1025  while (1) {
1026  int result = riscv011_poll(target);
1027  if (result != ERROR_OK)
1028  return result;
1029  if (target->state == state)
1030  return ERROR_OK;
1031  if (timeval_ms() > then) {
1032  LOG_ERROR("Timed out waiting for state %d. "
1033  "Increase timeout with riscv set_command_timeout_sec.", state);
1034  return ERROR_FAIL;
1035  }
1036  }
1037 }
1038 
1039 static int read_remote_csr(struct target *target, uint64_t *value, uint32_t csr)
1040 {
1042  cache_set32(target, 0, csrr(S0, csr));
1044  cache_set_jump(target, 2);
1045  if (cache_write(target, 4, true) != ERROR_OK)
1046  return ERROR_FAIL;
1047  *value = cache_get(target, SLOT0);
1048  LOG_DEBUG("csr 0x%x = 0x%" PRIx64, csr, *value);
1049 
1050  uint32_t exception = cache_get32(target, info->dramsize-1);
1051  if (exception) {
1052  LOG_WARNING("Got exception 0x%x when reading %s", exception,
1054  *value = ~0;
1055  return ERROR_FAIL;
1056  }
1057 
1058  return ERROR_OK;
1059 }
1060 
1061 static int write_remote_csr(struct target *target, uint32_t csr, uint64_t value)
1062 {
1063  LOG_DEBUG("csr 0x%x <- 0x%" PRIx64, csr, value);
1064  cache_set_load(target, 0, S0, SLOT0);
1065  cache_set32(target, 1, csrw(S0, csr));
1066  cache_set_jump(target, 2);
1067  cache_set(target, SLOT0, value);
1068  if (cache_write(target, 4, true) != ERROR_OK)
1069  return ERROR_FAIL;
1070 
1071  return ERROR_OK;
1072 }
1073 
1074 static int write_gpr(struct target *target, unsigned int gpr, uint64_t value)
1075 {
1076  cache_set_load(target, 0, gpr, SLOT0);
1077  cache_set_jump(target, 1);
1078  cache_set(target, SLOT0, value);
1079  if (cache_write(target, 4, true) != ERROR_OK)
1080  return ERROR_FAIL;
1081  return ERROR_OK;
1082 }
1083 
1084 static int maybe_read_tselect(struct target *target)
1085 {
1087 
1088  if (info->tselect_dirty) {
1089  int result = read_remote_csr(target, &info->tselect, CSR_TSELECT);
1090  if (result != ERROR_OK)
1091  return result;
1092  info->tselect_dirty = false;
1093  }
1094 
1095  return ERROR_OK;
1096 }
1097 
1098 static int maybe_write_tselect(struct target *target)
1099 {
1101 
1102  if (!info->tselect_dirty) {
1103  int result = write_remote_csr(target, CSR_TSELECT, info->tselect);
1104  if (result != ERROR_OK)
1105  return result;
1106  info->tselect_dirty = true;
1107  }
1108 
1109  return ERROR_OK;
1110 }
1111 
1112 static uint64_t set_ebreakx_fields(uint64_t dcsr, const struct target *target)
1113 {
1114  const struct riscv_private_config * const config = riscv_private_config(target);
1115  dcsr = set_field(dcsr, DCSR_EBREAKM, config->dcsr_ebreak_fields[RISCV_MODE_M]);
1116  dcsr = set_field(dcsr, DCSR_EBREAKS, config->dcsr_ebreak_fields[RISCV_MODE_S]);
1117  dcsr = set_field(dcsr, DCSR_EBREAKU, config->dcsr_ebreak_fields[RISCV_MODE_U]);
1118  dcsr = set_field(dcsr, DCSR_EBREAKH, 1);
1119  return dcsr;
1120 }
1121 
1122 static int execute_resume(struct target *target, bool step)
1123 {
1125 
1126  LOG_DEBUG("step=%d", step);
1127 
1129  return ERROR_FAIL;
1130 
1132 
1133  /* TODO: check if dpc is dirty (which also is true if an exception was hit
1134  * at any time) */
1135  cache_set_load(target, 0, S0, SLOT0);
1136  cache_set32(target, 1, csrw(S0, CSR_DPC));
1137  cache_set_jump(target, 2);
1138  cache_set(target, SLOT0, info->dpc);
1139  if (cache_write(target, 4, true) != ERROR_OK)
1140  return ERROR_FAIL;
1141 
1142  struct reg *mstatus_reg = &target->reg_cache->reg_list[GDB_REGNO_MSTATUS];
1143  if (mstatus_reg->valid) {
1144  uint64_t mstatus_user = buf_get_u64(mstatus_reg->value, 0, riscv_xlen(target));
1145  if (mstatus_user != info->mstatus_actual) {
1146  cache_set_load(target, 0, S0, SLOT0);
1148  cache_set_jump(target, 2);
1149  cache_set(target, SLOT0, mstatus_user);
1150  if (cache_write(target, 4, true) != ERROR_OK)
1151  return ERROR_FAIL;
1152  }
1153  }
1154 
1155  info->dcsr = set_ebreakx_fields(info->dcsr, target);
1156  info->dcsr &= ~DCSR_HALT;
1157 
1158  if (step)
1159  info->dcsr |= DCSR_STEP;
1160  else
1161  info->dcsr &= ~DCSR_STEP;
1162 
1163  dram_write32(target, 0, lw(S0, ZERO, DEBUG_RAM_START + 16), false);
1164  dram_write32(target, 1, csrw(S0, CSR_DCSR), false);
1165  dram_write32(target, 2, fence_i(), false);
1166  dram_write_jump(target, 3, false);
1167 
1168  /* Write DCSR value, set interrupt and clear haltnot. */
1169  uint64_t dbus_value = DMCONTROL_INTERRUPT | info->dcsr;
1170  dbus_write(target, dram_address(4), dbus_value);
1171 
1173 
1174  if (wait_for_debugint_clear(target, true) != ERROR_OK) {
1175  LOG_ERROR("Debug interrupt didn't clear.");
1176  return ERROR_FAIL;
1177  }
1178 
1181 
1182  return ERROR_OK;
1183 }
1184 
1185 /* Execute a step, and wait for reentry into Debug Mode. */
1186 static int full_step(struct target *target, bool announce)
1187 {
1188  int result = execute_resume(target, true);
1189  if (result != ERROR_OK)
1190  return result;
1191  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
1192  while (1) {
1193  result = poll_target(target, announce);
1194  if (result != ERROR_OK)
1195  return result;
1197  break;
1198  if (timeval_ms() > then) {
1199  LOG_ERROR("Timed out waiting for step to complete."
1200  "Increase timeout with riscv set_command_timeout_sec");
1201  return ERROR_FAIL;
1202  }
1203  }
1204  return handle_halt(target, announce);
1205 }
1206 
1207 static uint64_t reg_cache_get(struct target *target, unsigned int number)
1208 {
1209  struct reg *r = &target->reg_cache->reg_list[number];
1210  if (!r->valid) {
1211  LOG_ERROR("Register cache entry for %d is invalid!", number);
1212  assert(r->valid);
1213  }
1214  uint64_t value = buf_get_u64(r->value, 0, r->size);
1215  LOG_DEBUG("%s = 0x%" PRIx64, r->name, value);
1216  return value;
1217 }
1218 
1219 static void reg_cache_set(struct target *target, unsigned int number,
1220  uint64_t value)
1221 {
1222  struct reg *r = &target->reg_cache->reg_list[number];
1223  LOG_DEBUG("%s <= 0x%" PRIx64, r->name, value);
1224  r->valid = true;
1225  buf_set_u64(r->value, 0, r->size, value);
1226 }
1227 
1229 {
1230  struct reg *mstatus_reg = &target->reg_cache->reg_list[GDB_REGNO_MSTATUS];
1231  if (mstatus_reg->valid) {
1232  /* We previously made it valid. */
1233  return ERROR_OK;
1234  }
1235 
1236  /* Force reading the register. In that process mstatus_actual will be
1237  * updated. */
1238  riscv_reg_t mstatus;
1239  return riscv011_get_register(target, &mstatus, GDB_REGNO_MSTATUS);
1240 }
1241 
1242 /*** OpenOCD target functions. ***/
1243 
1244 static int register_read(struct target *target, riscv_reg_t *value, int regnum)
1245 {
1247  if (regnum >= GDB_REGNO_CSR0 && regnum <= GDB_REGNO_CSR4095) {
1248  cache_set32(target, 0, csrr(S0, regnum - GDB_REGNO_CSR0));
1250  cache_set_jump(target, 2);
1251  } else {
1252  LOG_ERROR("Don't know how to read register %d", regnum);
1253  return ERROR_FAIL;
1254  }
1255 
1256  if (cache_write(target, 4, true) != ERROR_OK)
1257  return ERROR_FAIL;
1258 
1259  uint32_t exception = cache_get32(target, info->dramsize-1);
1260  if (exception) {
1261  LOG_WARNING("Got exception 0x%x when reading %s", exception, riscv_reg_gdb_regno_name(target, regnum));
1262  *value = ~0;
1263  return ERROR_FAIL;
1264  }
1265 
1266  *value = cache_get(target, SLOT0);
1267  LOG_DEBUG("reg[%d]=0x%" PRIx64, regnum, *value);
1268 
1269  if (regnum == GDB_REGNO_MSTATUS)
1270  info->mstatus_actual = *value;
1271 
1272  return ERROR_OK;
1273 }
1274 
1275 /* Write the register. */
1276 static int register_write(struct target *target, unsigned int number,
1277  uint64_t value)
1278 {
1280 
1282 
1283  if (number == S0) {
1284  cache_set_load(target, 0, S0, SLOT0);
1286  cache_set_jump(target, 2);
1287  } else if (number == S1) {
1288  cache_set_load(target, 0, S0, SLOT0);
1290  cache_set_jump(target, 2);
1291  } else if (number <= GDB_REGNO_XPR31) {
1293  cache_set_jump(target, 1);
1294  } else if (number == GDB_REGNO_PC || number == GDB_REGNO_DPC) {
1295  info->dpc = value;
1296  return ERROR_OK;
1297  } else if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
1298  int result = update_mstatus_actual(target);
1299  if (result != ERROR_OK)
1300  return result;
1301  unsigned int i = 0;
1302  if ((info->mstatus_actual & MSTATUS_FS) == 0) {
1303  info->mstatus_actual = set_field(info->mstatus_actual, MSTATUS_FS, 1);
1304  cache_set_load(target, i++, S0, SLOT1);
1306  cache_set(target, SLOT1, info->mstatus_actual);
1307  }
1308 
1309  if (riscv_xlen(target) == 32)
1311  else
1313  cache_set_jump(target, i++);
1314  } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
1315  cache_set_load(target, 0, S0, SLOT0);
1317  cache_set_jump(target, 2);
1318 
1319  if (number == GDB_REGNO_MSTATUS)
1320  info->mstatus_actual = value;
1321  } else if (number == GDB_REGNO_PRIV) {
1322  info->dcsr = set_field(info->dcsr, DCSR_PRV, value);
1323  return ERROR_OK;
1324  } else {
1325  LOG_ERROR("Don't know how to write register %d", number);
1326  return ERROR_FAIL;
1327  }
1328 
1330  if (cache_write(target, info->dramsize - 1, true) != ERROR_OK)
1331  return ERROR_FAIL;
1332 
1333  uint32_t exception = cache_get32(target, info->dramsize-1);
1334  if (exception) {
1335  LOG_WARNING("Got exception 0x%x when writing %s", exception,
1337  return ERROR_FAIL;
1338  }
1339 
1340  return ERROR_OK;
1341 }
1342 
1344  enum gdb_regno regid)
1345 {
1347 
1349 
1350  if (regid <= GDB_REGNO_XPR31) {
1351  /* FIXME: Here the implementation assumes that the value
1352  * written to GPR will be the same as the value read back. This
1353  * is not true for a write of a non-zero value to x0.
1354  */
1355  *value = reg_cache_get(target, regid);
1356  } else if (regid == GDB_REGNO_PC || regid == GDB_REGNO_DPC) {
1357  *value = info->dpc;
1358  } else if (regid >= GDB_REGNO_FPR0 && regid <= GDB_REGNO_FPR31) {
1359  int result = update_mstatus_actual(target);
1360  if (result != ERROR_OK)
1361  return result;
1362  unsigned int i = 0;
1363  if ((info->mstatus_actual & MSTATUS_FS) == 0) {
1364  info->mstatus_actual = set_field(info->mstatus_actual, MSTATUS_FS, 1);
1365  cache_set_load(target, i++, S0, SLOT1);
1367  cache_set(target, SLOT1, info->mstatus_actual);
1368  }
1369 
1370  if (riscv_xlen(target) == 32)
1371  cache_set32(target, i++, fsw(regid - GDB_REGNO_FPR0, 0, DEBUG_RAM_START + 16));
1372  else
1373  cache_set32(target, i++, fsd(regid - GDB_REGNO_FPR0, 0, DEBUG_RAM_START + 16));
1374  cache_set_jump(target, i++);
1375 
1376  if (cache_write(target, 4, true) != ERROR_OK)
1377  return ERROR_FAIL;
1378  } else if (regid == GDB_REGNO_PRIV) {
1379  *value = get_field(info->dcsr, DCSR_PRV);
1380  } else {
1381  int result = register_read(target, value, regid);
1382  if (result != ERROR_OK)
1383  return result;
1384  }
1385 
1386  return ERROR_OK;
1387 }
1388 
1389 /* This function is intended to handle accesses to registers through register
1390  * cache. */
1393 {
1394  assert(target->reg_cache);
1395  assert(target->reg_cache->reg_list);
1396  struct reg * const reg = &target->reg_cache->reg_list[regid];
1397  assert(reg);
1398  /* On RISC-V 0.11 targets valid value of some registers (e.g. `dcsr`)
1399  * is stored in `riscv011_info_t` itself, not in register cache. This
1400  * complicates register cache implementation.
1401  * Therefore, for now, caching registers in register cache is disabled
1402  * for all registers, except for reads of GPRs.
1403  */
1404  assert(!reg->dirty);
1405  int result = register_write(target, regid, value);
1406  if (result != ERROR_OK)
1407  return result;
1408  reg_cache_set(target, regid, value);
1409  /* FIXME: x0 (zero) should not be cached on writes. */
1410  reg->valid = regid <= GDB_REGNO_XPR31;
1411  return ERROR_OK;
1412 }
1413 
1414 static int halt(struct target *target)
1415 {
1416  LOG_DEBUG("riscv_halt()");
1418 
1422  cache_set_jump(target, 3);
1423 
1424  if (cache_write(target, 4, true) != ERROR_OK) {
1425  LOG_ERROR("cache_write() failed.");
1426  return ERROR_FAIL;
1427  }
1428 
1429  return ERROR_OK;
1430 }
1431 
1432 static void deinit_target(struct target *target)
1433 {
1434  LOG_DEBUG("riscv_deinit_target()");
1435  struct riscv_info *info = target->arch_info;
1436  if (!info)
1437  return;
1438 
1439  free(info->version_specific);
1440  info->version_specific = NULL;
1441 }
1442 
1443 static int strict_step(struct target *target, bool announce)
1444 {
1445  LOG_DEBUG("enter");
1446 
1448  while (watchpoint) {
1451  }
1452 
1453  int result = full_step(target, announce);
1454  if (result != ERROR_OK)
1455  return result;
1456 
1458  while (watchpoint) {
1461  }
1462 
1463  return ERROR_OK;
1464 }
1465 
1466 static int step(struct target *target, bool current, target_addr_t address,
1467  bool handle_breakpoints)
1468 {
1470 
1471  if (!current) {
1472  if (riscv_xlen(target) > 32) {
1473  LOG_WARNING("Asked to resume at 32-bit PC on %d-bit target.",
1474  riscv_xlen(target));
1475  }
1476  int result = register_write(target, GDB_REGNO_PC, address);
1477  if (result != ERROR_OK)
1478  return result;
1479  }
1480 
1481  if (handle_breakpoints) {
1482  int result = strict_step(target, true);
1483  if (result != ERROR_OK)
1484  return result;
1485  } else {
1486  return full_step(target, false);
1487  }
1488 
1489  return ERROR_OK;
1490 }
1491 
1492 static int examine(struct target *target)
1493 {
1494  /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
1495  uint32_t dtmcontrol;
1496  if (dtmcs_scan(target->tap, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
1497  LOG_ERROR("Could not scan dtmcontrol. Check JTAG connectivity/board power.");
1498  return ERROR_FAIL;
1499  }
1500 
1501  LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol);
1502  LOG_DEBUG(" addrbits=%d", get_field32(dtmcontrol, DTMCONTROL_ADDRBITS));
1503  LOG_DEBUG(" version=%d", get_field32(dtmcontrol, DTMCONTROL_VERSION));
1504  LOG_DEBUG(" idle=%d", get_field32(dtmcontrol, DTMCONTROL_IDLE));
1505 
1506  if (get_field(dtmcontrol, DTMCONTROL_VERSION) != 0) {
1507  LOG_ERROR("Unsupported DTM version %d. (dtmcontrol=0x%x)",
1508  get_field32(dtmcontrol, DTMCONTROL_VERSION), dtmcontrol);
1509  return ERROR_FAIL;
1510  }
1511 
1512  RISCV_INFO(r);
1513 
1515  info->addrbits = get_field(dtmcontrol, DTMCONTROL_ADDRBITS);
1516  info->dtmcontrol_idle = get_field(dtmcontrol, DTMCONTROL_IDLE);
1517  if (info->dtmcontrol_idle == 0) {
1518  /* Some old SiFive cores don't set idle but need it to be 1. */
1519  uint32_t idcode = idcode_scan(target);
1520  if (idcode == 0x10e31913)
1521  info->dtmcontrol_idle = 1;
1522  }
1523 
1524  uint32_t dminfo = dbus_read(target, DMINFO);
1525  LOG_DEBUG("dminfo: 0x%08x", dminfo);
1526  LOG_DEBUG(" abussize=0x%x", get_field32(dminfo, DMINFO_ABUSSIZE));
1527  LOG_DEBUG(" serialcount=0x%x", get_field32(dminfo, DMINFO_SERIALCOUNT));
1528  LOG_DEBUG(" access128=%d", get_field32(dminfo, DMINFO_ACCESS128));
1529  LOG_DEBUG(" access64=%d", get_field32(dminfo, DMINFO_ACCESS64));
1530  LOG_DEBUG(" access32=%d", get_field32(dminfo, DMINFO_ACCESS32));
1531  LOG_DEBUG(" access16=%d", get_field32(dminfo, DMINFO_ACCESS16));
1532  LOG_DEBUG(" access8=%d", get_field32(dminfo, DMINFO_ACCESS8));
1533  LOG_DEBUG(" dramsize=0x%x", get_field32(dminfo, DMINFO_DRAMSIZE));
1534  LOG_DEBUG(" authenticated=0x%x", get_field32(dminfo, DMINFO_AUTHENTICATED));
1535  LOG_DEBUG(" authbusy=0x%x", get_field32(dminfo, DMINFO_AUTHBUSY));
1536  LOG_DEBUG(" authtype=0x%x", get_field32(dminfo, DMINFO_AUTHTYPE));
1537  LOG_DEBUG(" version=0x%x", get_field32(dminfo, DMINFO_VERSION));
1538 
1539  if (get_field(dminfo, DMINFO_VERSION) != 1) {
1540  LOG_ERROR("OpenOCD only supports Debug Module version 1, not %d "
1541  "(dminfo=0x%x)", get_field32(dminfo, DMINFO_VERSION), dminfo);
1542  return ERROR_FAIL;
1543  }
1544 
1545  info->dramsize = get_field(dminfo, DMINFO_DRAMSIZE) + 1;
1546 
1547  if (get_field(dminfo, DMINFO_AUTHTYPE) != 0) {
1548  LOG_ERROR("Authentication required by RISC-V core but not "
1549  "supported by OpenOCD. dminfo=0x%x", dminfo);
1550  return ERROR_FAIL;
1551  }
1552 
1553  /* Pretend this is a 32-bit system until we have found out the true value. */
1554  r->xlen = 32;
1555 
1556  /* Figure out XLEN, and test writing all of Debug RAM while we're at it. */
1557  cache_set32(target, 0, xori(S1, ZERO, -1));
1558  /* 0xffffffff 0xffffffff:ffffffff 0xffffffff:ffffffff:ffffffff:ffffffff */
1559  cache_set32(target, 1, srli(S1, S1, 31));
1560  /* 0x00000001 0x00000001:ffffffff 0x00000001:ffffffff:ffffffff:ffffffff */
1562  cache_set32(target, 3, srli(S1, S1, 31));
1563  /* 0x00000000 0x00000000:00000003 0x00000000:00000003:ffffffff:ffffffff */
1565  cache_set_jump(target, 5);
1566  for (unsigned int i = 6; i < info->dramsize; i++)
1567  cache_set32(target, i, i * 0x01020304);
1568 
1569  cache_write(target, 0, false);
1570 
1571  /* Check that we can actually read/write dram. */
1572  if (cache_check(target) != ERROR_OK)
1573  return ERROR_FAIL;
1574 
1575  cache_write(target, 0, true);
1577 
1578  uint32_t word0 = cache_get32(target, 0);
1579  uint32_t word1 = cache_get32(target, 1);
1580  struct riscv_info *generic_info = riscv_info(target);
1581  if (word0 == 1 && word1 == 0) {
1582  generic_info->xlen = 32;
1583  } else if (word0 == 0xffffffff && word1 == 3) {
1584  generic_info->xlen = 64;
1585  } else if (word0 == 0xffffffff && word1 == 0xffffffff) {
1586  generic_info->xlen = 128;
1587  } else {
1588  uint32_t exception = cache_get32(target, info->dramsize-1);
1589  LOG_ERROR("Failed to discover xlen; word0=0x%x, word1=0x%x, exception=0x%x",
1590  word0, word1, exception);
1592  return ERROR_FAIL;
1593  }
1594  LOG_DEBUG("Discovered XLEN is %d", riscv_xlen(target));
1595 
1596  if (read_remote_csr(target, &r->misa, CSR_MISA) != ERROR_OK) {
1597  const unsigned int old_csr_misa = 0xf10;
1598  LOG_WARNING("Failed to read misa at 0x%x; trying 0x%x.", CSR_MISA,
1599  old_csr_misa);
1600  if (read_remote_csr(target, &r->misa, old_csr_misa) != ERROR_OK) {
1601  /* Maybe this is an old core that still has $misa at the old
1602  * address. */
1603  LOG_ERROR("Failed to read misa at 0x%x.", old_csr_misa);
1604  return ERROR_FAIL;
1605  }
1606  }
1607 
1608  /* Update register list to match discovered XLEN/supported extensions. */
1610 
1611  info->never_halted = true;
1612 
1613  int result = riscv011_poll(target);
1614  if (result != ERROR_OK)
1615  return result;
1616 
1618  LOG_INFO("Examined RISCV core; XLEN=%d, misa=0x%" PRIx64,
1619  riscv_xlen(target), r->misa);
1620 
1621  return ERROR_OK;
1622 }
1623 
1625 {
1627 
1628  scans_t *scans = scans_new(target, 256);
1629  if (!scans)
1630  return RE_FAIL;
1631 
1632  /* Read all GPRs as fast as we can, because gdb is going to ask for them
1633  * anyway. Reading them one at a time is much slower. */
1634 
1635  /* Write the jump back to address 1. */
1636  scans_add_write_jump(scans, 1, false);
1637  for (int reg = 1; reg < 32; reg++) {
1638  if (reg == S0 || reg == S1)
1639  continue;
1640 
1641  /* Write store instruction. */
1642  scans_add_write_store(scans, 0, reg, SLOT0, true);
1643 
1644  /* Read value. */
1645  scans_add_read(scans, SLOT0, false);
1646  }
1647 
1648  /* Write store of s0 at index 1. */
1649  scans_add_write_store(scans, 1, S0, SLOT0, false);
1650  /* Write jump at index 2. */
1651  scans_add_write_jump(scans, 2, false);
1652 
1653  /* Read S1 from debug RAM */
1654  scans_add_write_load(scans, 0, S0, SLOT_LAST, true);
1655  /* Read value. */
1656  scans_add_read(scans, SLOT0, false);
1657 
1658  /* Read S0 from dscratch */
1659  unsigned int csr[] = {CSR_DSCRATCH0, CSR_DPC, CSR_DCSR};
1660  for (unsigned int i = 0; i < ARRAY_SIZE(csr); i++) {
1661  scans_add_write32(scans, 0, csrr(S0, csr[i]), true);
1662  scans_add_read(scans, SLOT0, false);
1663  }
1664 
1665  /* Final read to get the last value out. */
1666  scans_add_read32(scans, 4, false);
1667 
1668  int retval = scans_execute(scans);
1669  if (retval != ERROR_OK) {
1670  LOG_ERROR("JTAG execute failed: %d", retval);
1671  goto error;
1672  }
1673 
1674  unsigned int dbus_busy = 0;
1675  unsigned int interrupt_set = 0;
1676  unsigned int result = 0;
1677  uint64_t value = 0;
1678  reg_cache_set(target, 0, 0);
1679  /* The first scan result is the result from something old we don't care
1680  * about. */
1681  for (unsigned int i = 1; i < scans->next_scan && dbus_busy == 0; i++) {
1683  DBUS_OP_SIZE);
1684  uint64_t data = scans_get_u64(scans, i, DBUS_DATA_START, DBUS_DATA_SIZE);
1685  uint32_t address = scans_get_u32(scans, i, DBUS_ADDRESS_START,
1686  info->addrbits);
1687  switch (status) {
1688  case DBUS_STATUS_SUCCESS:
1689  break;
1690  case DBUS_STATUS_FAILED:
1691  LOG_ERROR("Debug access failed. Hardware error?");
1692  goto error;
1693  case DBUS_STATUS_BUSY:
1694  dbus_busy++;
1695  break;
1696  default:
1697  LOG_ERROR("Got invalid bus access status: %d", status);
1698  goto error;
1699  }
1700  if (data & DMCONTROL_INTERRUPT) {
1701  interrupt_set++;
1702  break;
1703  }
1704  if (address == 4 || address == 5) {
1705  unsigned int reg;
1706  switch (result) {
1707  case 0:
1708  reg = 1;
1709  break;
1710  case 1:
1711  reg = 2;
1712  break;
1713  case 2:
1714  reg = 3;
1715  break;
1716  case 3:
1717  reg = 4;
1718  break;
1719  case 4:
1720  reg = 5;
1721  break;
1722  case 5:
1723  reg = 6;
1724  break;
1725  case 6:
1726  reg = 7;
1727  break;
1728  /* S0 */
1729  /* S1 */
1730  case 7:
1731  reg = 10;
1732  break;
1733  case 8:
1734  reg = 11;
1735  break;
1736  case 9:
1737  reg = 12;
1738  break;
1739  case 10:
1740  reg = 13;
1741  break;
1742  case 11:
1743  reg = 14;
1744  break;
1745  case 12:
1746  reg = 15;
1747  break;
1748  case 13:
1749  reg = 16;
1750  break;
1751  case 14:
1752  reg = 17;
1753  break;
1754  case 15:
1755  reg = 18;
1756  break;
1757  case 16:
1758  reg = 19;
1759  break;
1760  case 17:
1761  reg = 20;
1762  break;
1763  case 18:
1764  reg = 21;
1765  break;
1766  case 19:
1767  reg = 22;
1768  break;
1769  case 20:
1770  reg = 23;
1771  break;
1772  case 21:
1773  reg = 24;
1774  break;
1775  case 22:
1776  reg = 25;
1777  break;
1778  case 23:
1779  reg = 26;
1780  break;
1781  case 24:
1782  reg = 27;
1783  break;
1784  case 25:
1785  reg = 28;
1786  break;
1787  case 26:
1788  reg = 29;
1789  break;
1790  case 27:
1791  reg = 30;
1792  break;
1793  case 28:
1794  reg = 31;
1795  break;
1796  case 29:
1797  reg = S1;
1798  break;
1799  case 30:
1800  reg = S0;
1801  break;
1802  case 31:
1803  reg = GDB_REGNO_DPC;
1804  break;
1805  case 32:
1806  reg = GDB_REGNO_DCSR;
1807  break;
1808  default:
1809  assert(0);
1810  LOG_ERROR("Got invalid register result %d", result);
1811  goto error;
1812  }
1813  if (riscv_xlen(target) == 32) {
1814  reg_cache_set(target, reg, data & 0xffffffff);
1815  result++;
1816  } else if (riscv_xlen(target) == 64) {
1817  if (address == 4) {
1818  value = data & 0xffffffff;
1819  } else if (address == 5) {
1820  reg_cache_set(target, reg, ((data & 0xffffffff) << 32) | value);
1821  value = 0;
1822  result++;
1823  }
1824  }
1825  }
1826  }
1827 
1828  scans_delete(scans);
1829 
1830  if (dbus_busy) {
1832  return RE_AGAIN;
1833  }
1834  if (interrupt_set) {
1836  return RE_AGAIN;
1837  }
1838 
1839  /* TODO: get rid of those 2 variables and talk to the cache directly. */
1842 
1844 
1845  return RE_OK;
1846 
1847 error:
1848  scans_delete(scans);
1849  return RE_FAIL;
1850 }
1851 
1852 static int handle_halt(struct target *target, bool announce)
1853 {
1856 
1857  riscv_error_t re;
1858  do {
1860  } while (re == RE_AGAIN);
1861  if (re != RE_OK) {
1862  LOG_ERROR("handle_halt_routine failed");
1863  return ERROR_FAIL;
1864  }
1865 
1866  int cause = get_field(info->dcsr, DCSR_CAUSE);
1867  switch (cause) {
1868  case DCSR_CAUSE_SWBP:
1870  break;
1871  case DCSR_CAUSE_HWBP:
1873  break;
1874  case DCSR_CAUSE_DEBUGINT:
1876  break;
1877  case DCSR_CAUSE_STEP:
1879  break;
1880  case DCSR_CAUSE_HALT:
1881  default:
1882  LOG_ERROR("Invalid halt cause %d in DCSR (0x%" PRIx64 ")",
1883  cause, info->dcsr);
1884  }
1885 
1886  if (info->never_halted) {
1887  info->never_halted = false;
1888 
1889  int result = maybe_read_tselect(target);
1890  if (result != ERROR_OK)
1891  return result;
1893  }
1894 
1896  int retval;
1897  /* Hotfix: Don't try to handle semihosting before the target is marked as examined. */
1898  /* TODO: The code should be rearranged so that:
1899  * - Semihosting is not attempted before the target is examined.
1900  * - When the target is already halted on a semihosting magic sequence
1901  * at the time when OpenOCD connects to it, this semihosting attempt
1902  * gets handled right after the examination.
1903  */
1905  if (riscv_semihosting(target, &retval) != SEMIHOSTING_NONE)
1906  return retval;
1907  }
1908 
1909  if (announce)
1911 
1912  const char *cause_string[] = {
1913  "none",
1914  "software breakpoint",
1915  "hardware trigger",
1916  "debug interrupt",
1917  "step",
1918  "halt"
1919  };
1920  /* This is logged to the user so that gdb will show it when a user types
1921  * 'monitor reset init'. At that time gdb appears to have the pc cached
1922  * still so if a user manually inspects the pc it will still have the old
1923  * value. */
1924  LOG_USER("halted at 0x%" PRIx64 " due to %s", info->dpc, cause_string[cause]);
1925 
1926  return ERROR_OK;
1927 }
1928 
1929 static int poll_target(struct target *target, bool announce)
1930 {
1932 
1933  bits_t bits = {
1934  .haltnot = 0,
1935  .interrupt = 0
1936  };
1937  if (read_bits(target, &bits) != ERROR_OK)
1938  return ERROR_FAIL;
1939 
1940  if (bits.haltnot && bits.interrupt) {
1942  LOG_DEBUG("debug running");
1943  } else if (bits.haltnot && !bits.interrupt) {
1944  if (target->state != TARGET_HALTED)
1945  return handle_halt(target, announce);
1946  } else if (!bits.haltnot && bits.interrupt) {
1947  /* Target is halting. There is no state for that, so don't change anything. */
1948  LOG_DEBUG("halting");
1949  } else if (!bits.haltnot && !bits.interrupt) {
1951  }
1952 
1953  return ERROR_OK;
1954 }
1955 
1956 static int riscv011_poll(struct target *target)
1957 {
1958  return poll_target(target, true);
1959 }
1960 
1961 static int riscv011_resume(struct target *target, bool current,
1962  target_addr_t address, bool handle_breakpoints,
1963  bool debug_execution)
1964 {
1965  RISCV_INFO(r);
1967 
1968  r->prepped = false;
1969  return execute_resume(target, false);
1970 }
1971 
1972 static int assert_reset(struct target *target)
1973 {
1975  /* TODO: Maybe what I implemented here is more like soft_reset_halt()? */
1976 
1978 
1979  /* The only assumption we can make is that the TAP was reset. */
1980  if (wait_for_debugint_clear(target, true) != ERROR_OK) {
1981  LOG_ERROR("Debug interrupt didn't clear.");
1982  return ERROR_FAIL;
1983  }
1984 
1985  /* Not sure what we should do when there are multiple cores.
1986  * Here just reset the single hart we're talking to. */
1987  info->dcsr = set_ebreakx_fields(info->dcsr, target);
1988  info->dcsr |= DCSR_HALT;
1989  if (target->reset_halt)
1990  info->dcsr |= DCSR_NDRESET;
1991  else
1992  info->dcsr |= DCSR_FULLRESET;
1993  dram_write32(target, 0, lw(S0, ZERO, DEBUG_RAM_START + 16), false);
1994  dram_write32(target, 1, csrw(S0, CSR_DCSR), false);
1995  /* We shouldn't actually need the jump because a reset should happen. */
1996  dram_write_jump(target, 2, false);
1997  dram_write32(target, 4, info->dcsr, true);
1999 
2001 
2002  return ERROR_OK;
2003 }
2004 
2005 static int deassert_reset(struct target *target)
2006 {
2008  if (target->reset_halt)
2010  else
2012 }
2013 
2014 static int read_memory(struct target *target, const struct riscv_mem_access_args args)
2015 {
2016  assert(riscv_mem_access_is_read(args));
2017 
2018  const target_addr_t address = args.address;
2019  const uint32_t size = args.size;
2020  const uint32_t count = args.count;
2021  const uint32_t increment = args.increment;
2022  uint8_t * const buffer = args.read_buffer;
2023 
2024  if (increment != size) {
2025  LOG_ERROR("read_memory with custom increment not implemented");
2026  return ERROR_NOT_IMPLEMENTED;
2027  }
2028 
2030 
2031  cache_set32(target, 0, lw(S0, ZERO, DEBUG_RAM_START + 16));
2032  switch (size) {
2033  case 1:
2034  cache_set32(target, 1, lb(S1, S0, 0));
2035  cache_set32(target, 2, sw(S1, ZERO, DEBUG_RAM_START + 16));
2036  break;
2037  case 2:
2038  cache_set32(target, 1, lh(S1, S0, 0));
2039  cache_set32(target, 2, sw(S1, ZERO, DEBUG_RAM_START + 16));
2040  break;
2041  case 4:
2042  cache_set32(target, 1, lw(S1, S0, 0));
2043  cache_set32(target, 2, sw(S1, ZERO, DEBUG_RAM_START + 16));
2044  break;
2045  default:
2046  LOG_ERROR("Unsupported size: %d", size);
2047  return ERROR_FAIL;
2048  }
2049  cache_set_jump(target, 3);
2050  cache_write(target, CACHE_NO_READ, false);
2051 
2053  const unsigned int max_batch_size = 256;
2054  scans_t *scans = scans_new(target, max_batch_size);
2055  if (!scans)
2056  return ERROR_FAIL;
2057 
2058  uint32_t result_value = 0x777;
2059  uint32_t i = 0;
2060  while (i < count + 3) {
2061  unsigned int batch_size = MIN(count + 3 - i, max_batch_size);
2062  scans_reset(scans);
2063 
2064  for (unsigned int j = 0; j < batch_size; j++) {
2065  if (i + j == count) {
2066  /* Just insert a read so we can scan out the last value. */
2067  scans_add_read32(scans, 4, false);
2068  } else if (i + j >= count + 1) {
2069  /* And check for errors. */
2070  scans_add_read32(scans, info->dramsize-1, false);
2071  } else {
2072  /* Write the next address and set interrupt. */
2073  uint32_t offset = size * (i + j);
2074  scans_add_write32(scans, 4, address + offset, true);
2075  }
2076  }
2077 
2078  int retval = scans_execute(scans);
2079  if (retval != ERROR_OK) {
2080  LOG_ERROR("JTAG execute failed: %d", retval);
2081  goto error;
2082  }
2083 
2084  int dbus_busy = 0;
2085  int execute_busy = 0;
2086  for (unsigned int j = 0; j < batch_size; j++) {
2088  DBUS_OP_SIZE);
2089  switch (status) {
2090  case DBUS_STATUS_SUCCESS:
2091  break;
2092  case DBUS_STATUS_FAILED:
2093  LOG_ERROR("Debug RAM write failed. Hardware error?");
2094  goto error;
2095  case DBUS_STATUS_BUSY:
2096  dbus_busy++;
2097  break;
2098  default:
2099  LOG_ERROR("Got invalid bus access status: %d", status);
2100  return ERROR_FAIL;
2101  }
2102  uint64_t data = scans_get_u64(scans, j, DBUS_DATA_START,
2103  DBUS_DATA_SIZE);
2104  if (data & DMCONTROL_INTERRUPT)
2105  execute_busy++;
2106  if (i + j == count + 2) {
2107  result_value = data;
2108  } else if (i + j > 1) {
2109  uint32_t offset = size * (i + j - 2);
2110  switch (size) {
2111  case 1:
2112  buffer[offset] = data;
2113  break;
2114  case 2:
2115  buffer[offset] = data;
2116  buffer[offset + 1] = data >> 8;
2117  break;
2118  case 4:
2119  buffer[offset] = data;
2120  buffer[offset + 1] = data >> 8;
2121  buffer[offset + 2] = data >> 16;
2122  buffer[offset + 3] = data >> 24;
2123  break;
2124  }
2125  }
2126  LOG_DEBUG("j=%d status=%d data=%09" PRIx64, j, status, data);
2127  }
2128  if (dbus_busy)
2130  if (execute_busy)
2132  if (dbus_busy || execute_busy) {
2134 
2135  /* Retry. */
2136  LOG_INFO("Retrying memory read starting from 0x%" TARGET_PRIxADDR
2137  " with more delays", address + size * i);
2138  } else {
2139  i += batch_size;
2140  }
2141  }
2142 
2143  if (result_value != 0) {
2144  LOG_USER("Core got an exception (0x%x) while reading from 0x%"
2145  TARGET_PRIxADDR, result_value, address + size * (count-1));
2146  if (count > 1) {
2147  LOG_USER("(It may have failed between 0x%" TARGET_PRIxADDR
2148  " and 0x%" TARGET_PRIxADDR " as well, but we "
2149  "didn't check then.)",
2150  address, address + size * (count-2) + size - 1);
2151  }
2152  goto error;
2153  }
2154 
2155  scans_delete(scans);
2157  return ERROR_OK;
2158 
2159 error:
2160  scans_delete(scans);
2162  return ERROR_FAIL;
2163 }
2164 
2165 static int setup_write_memory(struct target *target, uint32_t size)
2166 {
2167  switch (size) {
2168  case 1:
2169  cache_set32(target, 0, lb(S0, ZERO, DEBUG_RAM_START + 16));
2170  cache_set32(target, 1, sb(S0, T0, 0));
2171  break;
2172  case 2:
2173  cache_set32(target, 0, lh(S0, ZERO, DEBUG_RAM_START + 16));
2174  cache_set32(target, 1, sh(S0, T0, 0));
2175  break;
2176  case 4:
2177  cache_set32(target, 0, lw(S0, ZERO, DEBUG_RAM_START + 16));
2178  cache_set32(target, 1, sw(S0, T0, 0));
2179  break;
2180  default:
2181  LOG_ERROR("Unsupported size: %d", size);
2182  return ERROR_FAIL;
2183  }
2184  cache_set32(target, 2, addi(T0, T0, size));
2185  cache_set_jump(target, 3);
2186  cache_write(target, 4, false);
2187 
2188  return ERROR_OK;
2189 }
2190 
2191 static int write_memory(struct target *target, const struct riscv_mem_access_args args)
2192 {
2193  assert(riscv_mem_access_is_write(args));
2194 
2195  if (args.increment != args.size) {
2196  LOG_TARGET_ERROR(target, "Write increment size has to be equal to element size");
2197  return ERROR_NOT_IMPLEMENTED;
2198  }
2199 
2200  const target_addr_t address = args.address;
2201  const uint32_t size = args.size;
2202  const uint32_t count = args.count;
2203  const uint8_t * const buffer = args.write_buffer;
2204 
2207 
2208  /* Set up the address. */
2210  cache_set_load(target, 1, T0, SLOT0);
2211  cache_set_jump(target, 2);
2213  if (cache_write(target, 5, true) != ERROR_OK)
2214  return ERROR_FAIL;
2215 
2216  uint64_t t0 = cache_get(target, SLOT1);
2217  LOG_DEBUG("t0 is 0x%" PRIx64, t0);
2218 
2220  return ERROR_FAIL;
2221 
2222  const unsigned int max_batch_size = 256;
2223  scans_t *scans = scans_new(target, max_batch_size);
2224  if (!scans)
2225  return ERROR_FAIL;
2226 
2227  uint32_t result_value = 0x777;
2228  uint32_t i = 0;
2229  while (i < count + 2) {
2230  unsigned int batch_size = MIN(count + 2 - i, max_batch_size);
2231  scans_reset(scans);
2232 
2233  for (unsigned int j = 0; j < batch_size; j++) {
2234  if (i + j >= count) {
2235  /* Check for an exception. */
2236  scans_add_read32(scans, info->dramsize-1, false);
2237  } else {
2238  /* Write the next value and set interrupt. */
2239  uint32_t value;
2240  uint32_t offset = size * (i + j);
2241  switch (size) {
2242  case 1:
2243  value = buffer[offset];
2244  break;
2245  case 2:
2246  value = buffer[offset] |
2247  (buffer[offset + 1] << 8);
2248  break;
2249  case 4:
2250  value = buffer[offset] |
2251  ((uint32_t)buffer[offset + 1] << 8) |
2252  ((uint32_t)buffer[offset + 2] << 16) |
2253  ((uint32_t)buffer[offset + 3] << 24);
2254  break;
2255  default:
2256  goto error;
2257  }
2258 
2259  scans_add_write32(scans, 4, value, true);
2260  }
2261  }
2262 
2263  int retval = scans_execute(scans);
2264  if (retval != ERROR_OK) {
2265  LOG_ERROR("JTAG execute failed: %d", retval);
2266  goto error;
2267  }
2268 
2269  int dbus_busy = 0;
2270  int execute_busy = 0;
2271  for (unsigned int j = 0; j < batch_size; j++) {
2273  DBUS_OP_SIZE);
2274  switch (status) {
2275  case DBUS_STATUS_SUCCESS:
2276  break;
2277  case DBUS_STATUS_FAILED:
2278  LOG_ERROR("Debug RAM write failed. Hardware error?");
2279  goto error;
2280  case DBUS_STATUS_BUSY:
2281  dbus_busy++;
2282  break;
2283  default:
2284  LOG_ERROR("Got invalid bus access status: %d", status);
2285  return ERROR_FAIL;
2286  }
2287  int interrupt = scans_get_u32(scans, j, DBUS_DATA_START + 33, 1);
2288  if (interrupt)
2289  execute_busy++;
2290  if (i + j == count + 1)
2291  result_value = scans_get_u32(scans, j, DBUS_DATA_START, 32);
2292  }
2293  if (dbus_busy)
2295  if (execute_busy)
2297  if (dbus_busy || execute_busy) {
2299 
2300  /* Retry.
2301  * Set t0 back to what it should have been at the beginning of this
2302  * batch. */
2303  LOG_INFO("Retrying memory write starting from 0x%" TARGET_PRIxADDR
2304  " with more delays", address + size * i);
2305 
2307 
2308  if (write_gpr(target, T0, address + size * i) != ERROR_OK)
2309  goto error;
2310 
2312  goto error;
2313  } else {
2314  i += batch_size;
2315  }
2316  }
2317 
2318  if (result_value != 0) {
2319  LOG_ERROR("Core got an exception (0x%x) while writing to 0x%"
2320  TARGET_PRIxADDR, result_value, address + size * (count-1));
2321  if (count > 1) {
2322  LOG_ERROR("(It may have failed between 0x%" TARGET_PRIxADDR
2323  " and 0x%" TARGET_PRIxADDR " as well, but we "
2324  "didn't check then.)",
2325  address, address + size * (count-2) + size - 1);
2326  }
2327  goto error;
2328  }
2329 
2330  scans_delete(scans);
2332  return register_write(target, T0, t0);
2333 
2334 error:
2335  scans_delete(scans);
2337  return ERROR_FAIL;
2338 }
2339 
2340 static int access_memory(struct target *target, const struct riscv_mem_access_args args)
2341 {
2342  assert(riscv_mem_access_is_valid(args));
2343  const bool is_write = riscv_mem_access_is_write(args);
2344  if (is_write)
2345  return write_memory(target, args);
2346  return read_memory(target, args);
2347 }
2348 
2349 static COMMAND_HELPER(riscv011_print_info, struct target *target)
2350 {
2351  /* Abstract description. */
2352  riscv_print_info_line(CMD, "target", "memory.read_while_running8", 0);
2353  riscv_print_info_line(CMD, "target", "memory.write_while_running8", 0);
2354  riscv_print_info_line(CMD, "target", "memory.read_while_running16", 0);
2355  riscv_print_info_line(CMD, "target", "memory.write_while_running16", 0);
2356  riscv_print_info_line(CMD, "target", "memory.read_while_running32", 0);
2357  riscv_print_info_line(CMD, "target", "memory.write_while_running32", 0);
2358  riscv_print_info_line(CMD, "target", "memory.read_while_running64", 0);
2359  riscv_print_info_line(CMD, "target", "memory.write_while_running64", 0);
2360  riscv_print_info_line(CMD, "target", "memory.read_while_running128", 0);
2361  riscv_print_info_line(CMD, "target", "memory.write_while_running128", 0);
2362 
2363  uint32_t dminfo = dbus_read(target, DMINFO);
2364  riscv_print_info_line(CMD, "dm", "authenticated", get_field(dminfo, DMINFO_AUTHENTICATED));
2365 
2366  return 0;
2367 }
2368 
2369 static int wait_for_authbusy(struct target *target)
2370 {
2371  int64_t then = timeval_ms() + 1000 * riscv_get_command_timeout_sec();
2372  while (1) {
2373  uint32_t dminfo = dbus_read(target, DMINFO);
2374  if (!get_field(dminfo, DMINFO_AUTHBUSY))
2375  break;
2376  if (timeval_ms() > then) {
2377  LOG_ERROR("Timed out after %ds waiting for authbusy to go low (dminfo=0x%x). "
2378  "Increase the timeout with riscv set_command_timeout_sec.",
2380  dminfo);
2381  return ERROR_FAIL;
2382  }
2383  }
2384 
2385  return ERROR_OK;
2386 }
2387 
2388 static int riscv011_authdata_read(struct target *target, uint32_t *value, unsigned int index)
2389 {
2390  if (index > 1) {
2391  LOG_ERROR("Spec 0.11 only has a two authdata registers.");
2392  return ERROR_FAIL;
2393  }
2394 
2396  return ERROR_FAIL;
2397 
2398  uint16_t authdata_address = index ? DMAUTHDATA1 : DMAUTHDATA0;
2399  *value = dbus_read(target, authdata_address);
2400 
2401  return ERROR_OK;
2402 }
2403 
2404 static int riscv011_authdata_write(struct target *target, uint32_t value, unsigned int index)
2405 {
2406  if (index > 1) {
2407  LOG_ERROR("Spec 0.11 only has a two authdata registers.");
2408  return ERROR_FAIL;
2409  }
2410 
2412  return ERROR_FAIL;
2413 
2414  uint16_t authdata_address = index ? DMAUTHDATA1 : DMAUTHDATA0;
2415  dbus_write(target, authdata_address, value);
2416 
2417  return ERROR_OK;
2418 }
2419 
2420 static bool riscv011_get_impebreak(const struct target *target)
2421 {
2422  return false;
2423 }
2424 
2425 static unsigned int riscv011_get_progbufsize(const struct target *target)
2426 {
2427  return 0;
2428 }
2429 
2431  struct target *target)
2432 {
2433  LOG_DEBUG("init");
2434  RISCV_INFO(generic_info);
2435  generic_info->access_memory = access_memory;
2436  generic_info->authdata_read = &riscv011_authdata_read;
2437  generic_info->authdata_write = &riscv011_authdata_write;
2438  generic_info->print_info = &riscv011_print_info;
2439  generic_info->get_impebreak = &riscv011_get_impebreak;
2440  generic_info->get_progbufsize = &riscv011_get_progbufsize;
2441 
2442  generic_info->version_specific = calloc(1, sizeof(riscv011_info_t));
2443  if (!generic_info->version_specific)
2444  return ERROR_FAIL;
2445 
2446  /* Assume 32-bit until we discover the real value in examine(). */
2447  generic_info->xlen = 32;
2449 
2450  return ERROR_OK;
2451 }
2452 
2453 struct target_type riscv011_target = {
2454  .name = "riscv",
2455 
2456  .init_target = init_target,
2457  .deinit_target = deinit_target,
2458  .examine = examine,
2459 
2460  /* poll current target status */
2461  .poll = riscv011_poll,
2462 
2463  .halt = halt,
2464  .resume = riscv011_resume,
2465  .step = step,
2466 
2467  .assert_reset = assert_reset,
2468  .deassert_reset = deassert_reset,
2469 };
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 uint64_t buf_get_u64(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 64-bit word.
Definition: binarybuffer.h:134
static void buf_set_u64(uint8_t *_buffer, unsigned int first, unsigned int num, uint64_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:65
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CSR_TSELECT
#define CSR_DPC
#define CSR_DCSR
#define CSR_DSCRATCH0
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
#define DCSR_CAUSE_SWBP
Definition: encoding.h:91
#define DCSR_PRV
Definition: encoding.h:88
#define DCSR_EBREAKH
Definition: encoding.h:79
#define DCSR_HALT
Definition: encoding.h:86
#define DCSR_CAUSE_DEBUGINT
Definition: encoding.h:93
#define DCSR_CAUSE_STEP
Definition: encoding.h:94
#define CSR_MHARTID
Definition: encoding.h:3090
#define DCSR_STEP
Definition: encoding.h:87
#define CSR_MSTATUS
Definition: encoding.h:2909
#define DCSR_NDRESET
Definition: encoding.h:76
#define DCSR_CAUSE_HWBP
Definition: encoding.h:92
#define DCSR_CAUSE
Definition: encoding.h:84
#define DCSR_EBREAKS
Definition: encoding.h:80
#define CSR_MISA
Definition: encoding.h:2910
#define MSTATUS_FS
Definition: encoding.h:24
#define DCSR_CAUSE_HALT
Definition: encoding.h:95
#define DCSR_EBREAKU
Definition: encoding.h:81
#define DCSR_FULLRESET
Definition: encoding.h:77
#define DCSR_EBREAKM
Definition: encoding.h:78
enum esirisc_reg_num number
Definition: esirisc.c:87
uint8_t csr
Definition: esirisc.c:136
static uint64_t set_field(uint64_t reg, uint64_t mask, uint64_t val)
Definition: field_helpers.h:21
static uint32_t get_field32(uint64_t reg, uint64_t mask)
Definition: field_helpers.h:14
static uint64_t get_field(uint64_t reg, uint64_t mask)
Definition: field_helpers.h:9
gdb_regno
Definition: gdb_regs.h:10
@ GDB_REGNO_DPC
Definition: gdb_regs.h:99
@ GDB_REGNO_CSR0
Definition: gdb_regs.h:82
@ GDB_REGNO_MSTATUS
Definition: gdb_regs.h:102
@ GDB_REGNO_ZERO
Definition: gdb_regs.h:11
@ GDB_REGNO_FPR31
Definition: gdb_regs.h:81
@ GDB_REGNO_FPR0
Definition: gdb_regs.h:48
@ GDB_REGNO_XPR31
Definition: gdb_regs.h:45
@ GDB_REGNO_PC
Definition: gdb_regs.h:47
@ GDB_REGNO_PRIV
Definition: gdb_regs.h:112
@ GDB_REGNO_CSR4095
Definition: gdb_regs.h:111
@ GDB_REGNO_DCSR
Definition: gdb_regs.h:100
void jtag_add_runtest(unsigned int num_cycles, enum tap_state state)
Goes to TAP_IDLE (if we're not already there), cycle precisely num_cycles in the TAP_IDLE state,...
Definition: jtag/core.c:593
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1045
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, enum tap_state state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:452
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, enum tap_state state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:375
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_IDLE
Definition: jtag.h:53
uint64_t op
Definition: lakemont.c:68
void log_printf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format,...)
Definition: log.c:201
#define LOG_USER(expr ...)
Definition: log.h:150
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_LEVEL_IS(FOO)
Definition: log.h:112
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_DEBUG
Definition: log.h:55
#define t0
Definition: mips32.c:192
static uint32_t sb(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:142
static uint32_t sd(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:122
static uint32_t csrw(unsigned int source, unsigned int csr) __attribute__((unused))
Definition: opcodes.h:192
static uint32_t ld(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:152
static uint32_t fence_i(void) __attribute__((unused))
Definition: opcodes.h:350
static uint32_t fsw(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:260
static uint32_t lw(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:162
static uint32_t lh(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:172
#define MAX_INT12
Definition: opcodes.h:22
static uint32_t csrr(unsigned int rd, unsigned int csr) __attribute__((unused))
Definition: opcodes.h:211
#define S0
Definition: opcodes.h:13
static uint32_t csrsi(unsigned int csr, uint8_t imm) __attribute__((unused))
Definition: opcodes.h:103
#define S1
Definition: opcodes.h:14
static uint32_t flw(unsigned int dest, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:280
static uint32_t fsd(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:270
static uint32_t srli(unsigned int dest, unsigned int src, uint8_t shamt) __attribute__((unused))
Definition: opcodes.h:375
static uint32_t jal(unsigned int rd, int32_t imm) __attribute__((unused))
Definition: opcodes.h:93
#define ZERO
Definition: opcodes.h:11
static uint32_t xori(unsigned int dest, unsigned int src, int16_t imm) __attribute__((unused))
Definition: opcodes.h:365
static uint32_t sh(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:132
static uint32_t addi(unsigned int dest, unsigned int src, int16_t imm) __attribute__((unused))
Definition: opcodes.h:201
#define MAX_INT21
Definition: opcodes.h:28
static uint32_t sw(unsigned int src, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:112
static uint32_t lb(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:182
static uint32_t fld(unsigned int dest, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:290
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
#define T0
Definition: reg_ezusb.h:332
#define MIN(a, b)
Definition: replacements.h:22
static void scans_add_write32(scans_t *scans, uint16_t address, uint32_t data, bool set_interrupt)
Add a 32-bit dbus write to the scans structure.
Definition: riscv-011.c:592
static riscv_error_t handle_halt_routine(struct target *target)
Definition: riscv-011.c:1624
static void scans_add_write_load(scans_t *scans, uint16_t address, unsigned int reg, slot_t slot, bool set_interrupt)
Add a 32-bit dbus write for an instruction that loads from the indicated slot.
Definition: riscv-011.c:616
int riscv011_get_register(struct target *target, riscv_reg_t *value, enum gdb_regno regid)
Definition: riscv-011.c:1343
#define DMINFO_AUTHTYPE
Definition: riscv-011.c:154
struct target_type riscv011_target
Definition: riscv-011.c:2453
#define DMINFO_DRAMSIZE
Definition: riscv-011.c:151
static uint64_t reg_cache_get(struct target *target, unsigned int number)
Definition: riscv-011.c:1207
#define DBUS_OP_SIZE
Definition: riscv-011.c:103
static int maybe_read_tselect(struct target *target)
Definition: riscv-011.c:1084
static int riscv011_poll(struct target *target)
Definition: riscv-011.c:1956
static int access_memory(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-011.c:2340
static int register_write(struct target *target, unsigned int number, uint64_t value)
Definition: riscv-011.c:1276
static unsigned int slot_offset(const struct target *target, slot_t slot)
Definition: riscv-011.c:228
static int wait_for_authbusy(struct target *target)
Definition: riscv-011.c:2369
static void scans_add_write_store(scans_t *scans, uint16_t address, unsigned int reg, slot_t slot, bool set_interrupt)
Add a 32-bit dbus write for an instruction that stores to the indicated slot.
Definition: riscv-011.c:625
static int strict_step(struct target *target, bool announce)
Definition: riscv-011.c:1443
static uint64_t scans_get_u64(scans_t *scans, unsigned int index, unsigned int first, unsigned int num)
Definition: riscv-011.c:665
static int read_remote_csr(struct target *target, uint64_t *value, uint32_t csr)
Definition: riscv-011.c:1039
#define DMCONTROL_INTERRUPT
Definition: riscv-011.c:133
static void dump_field(const struct scan_field *field)
Definition: riscv-011.c:388
static void increase_dbus_busy_delay(struct target *target)
Definition: riscv-011.c:335
static uint32_t load_slot(const struct target *target, unsigned int dest, slot_t slot)
Definition: riscv-011.c:284
static void scans_dump(scans_t *scans)
Definition: riscv-011.c:572
#define DMCONTROL_HALTNOT
Definition: riscv-011.c:134
static int dram_check32(struct target *target, unsigned int index, uint32_t expected)
Definition: riscv-011.c:752
static uint32_t cache_get32(struct target *target, unsigned int address)
Definition: riscv-011.c:993
static dbus_status_t dbus_scan(struct target *target, uint16_t *address_in, uint64_t *data_in, dbus_op_t op, uint16_t address_out, uint64_t data_out)
Definition: riscv-011.c:419
static void cache_set(struct target *target, slot_t slot, uint64_t data)
Definition: riscv-011.c:780
#define DMCONTROL
Definition: riscv-011.c:132
#define DRAM_CACHE_SIZE
Definition: riscv-011.c:164
static uint32_t load(const struct target *target, unsigned int rd, unsigned int base, int16_t offset)
Definition: riscv-011.c:258
#define DBUS_DATA_START
Definition: riscv-011.c:114
static uint32_t scans_get_u32(scans_t *scans, unsigned int index, unsigned int first, unsigned int num)
Definition: riscv-011.c:659
static uint64_t dbus_read(struct target *target, uint16_t address)
Definition: riscv-011.c:467
#define DTMCONTROL_DBUS_RESET
Definition: riscv-011.c:96
static void cache_clean(struct target *target)
Definition: riscv-011.c:831
enum slot slot_t
static uint16_t dram_address(unsigned int index)
Definition: riscv-011.c:300
#define DBUS_ADDRESS_START
Definition: riscv-011.c:116
#define DMINFO_ACCESS128
Definition: riscv-011.c:146
static int examine(struct target *target)
Definition: riscv-011.c:1492
dbus_status_t
Definition: riscv-011.c:109
@ DBUS_STATUS_BUSY
Definition: riscv-011.c:112
@ DBUS_STATUS_FAILED
Definition: riscv-011.c:111
@ DBUS_STATUS_SUCCESS
Definition: riscv-011.c:110
static int maybe_write_tselect(struct target *target)
Definition: riscv-011.c:1098
static int deassert_reset(struct target *target)
Definition: riscv-011.c:2005
#define DMINFO_AUTHBUSY
Definition: riscv-011.c:153
static int register_read(struct target *target, riscv_reg_t *value, int regnum)
Definition: riscv-011.c:1244
static void scans_add_read(scans_t *scans, slot_t slot, bool set_interrupt)
Add one or more scans to read the indicated slot.
Definition: riscv-011.c:645
static uint32_t store_slot(const struct target *target, unsigned int src, slot_t slot)
Definition: riscv-011.c:292
static int full_step(struct target *target, bool announce)
Definition: riscv-011.c:1186
riscv_error_t
Definition: riscv-011.c:118
@ RE_OK
Definition: riscv-011.c:119
@ RE_AGAIN
Definition: riscv-011.c:121
@ RE_FAIL
Definition: riscv-011.c:120
#define DTMCONTROL_ADDRBITS
Definition: riscv-011.c:98
static int assert_reset(struct target *target)
Definition: riscv-011.c:1972
#define DMINFO_ABUSSIZE
Definition: riscv-011.c:144
slot
Definition: riscv-011.c:124
@ SLOT0
Definition: riscv-011.c:125
@ SLOT1
Definition: riscv-011.c:126
@ SLOT_LAST
Definition: riscv-011.c:127
#define DEBUG_RAM_START
Definition: riscv-011.c:89
#define DMINFO_VERSION
Definition: riscv-011.c:155
static int read_bits(struct target *target, bits_t *result)
Read the haltnot and interrupt bits.
Definition: riscv-011.c:690
static void cache_invalidate(struct target *target)
Definition: riscv-011.c:820
#define DTMCONTROL_VERSION
Definition: riscv-011.c:99
#define DMINFO_ACCESS16
Definition: riscv-011.c:149
static int poll_target(struct target *target, bool announce)
Definition: riscv-011.c:1929
static scans_t * scans_new(struct target *target, unsigned int scan_count)
Definition: riscv-011.c:523
static int write_memory(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-011.c:2191
static void cache_set_load(struct target *target, unsigned int index, unsigned int reg, slot_t slot)
Definition: riscv-011.c:795
static int cache_write(struct target *target, unsigned int address, bool run)
Definition: riscv-011.c:864
#define DMINFO_ACCESS32
Definition: riscv-011.c:148
static int init_target(struct command_context *cmd_ctx, struct target *target)
Definition: riscv-011.c:2430
static riscv011_info_t * get_info(const struct target *target)
Definition: riscv-011.c:220
static uint32_t dram_read32(struct target *target, unsigned int index)
Definition: riscv-011.c:673
#define DMINFO_ACCESS8
Definition: riscv-011.c:150
int riscv011_set_register(struct target *target, enum gdb_regno regid, riscv_reg_t value)
Definition: riscv-011.c:1391
static int setup_write_memory(struct target *target, uint32_t size)
Definition: riscv-011.c:2165
static int write_gpr(struct target *target, unsigned int gpr, uint64_t value)
Definition: riscv-011.c:1074
static int cache_check(struct target *target)
Definition: riscv-011.c:841
#define DBUS_DATA_SIZE
Definition: riscv-011.c:115
#define DEBUG_ROM_RESUME
Definition: riscv-011.c:87
#define DMAUTHDATA1
Definition: riscv-011.c:158
#define DMINFO_ACCESS64
Definition: riscv-011.c:147
static COMMAND_HELPER(riscv011_print_info, struct target *target)
Definition: riscv-011.c:2349
static uint64_t cache_get(struct target *target, slot_t slot)
Definition: riscv-011.c:1003
static void scans_add_read32(scans_t *scans, uint16_t address, bool set_interrupt)
Add a 32-bit dbus read.
Definition: riscv-011.c:633
#define DMINFO
Definition: riscv-011.c:143
#define DTMCONTROL_IDLE
Definition: riscv-011.c:97
static int update_mstatus_actual(struct target *target)
Definition: riscv-011.c:1228
static int riscv011_authdata_read(struct target *target, uint32_t *value, unsigned int index)
Definition: riscv-011.c:2388
#define CACHE_NO_READ
Write cache to the target, and optionally run the program.
Definition: riscv-011.c:863
static uint64_t set_ebreakx_fields(uint64_t dcsr, const struct target *target)
Definition: riscv-011.c:1112
static int write_remote_csr(struct target *target, uint32_t csr, uint64_t value)
Definition: riscv-011.c:1061
static int halt(struct target *target)
Definition: riscv-011.c:1414
static void add_dbus_scan(const struct target *target, struct scan_field *field, uint8_t *out_value, uint8_t *in_value, dbus_op_t op, uint16_t address, uint64_t data)
Definition: riscv-011.c:355
static int wait_for_debugint_clear(struct target *target, bool ignore_first)
Definition: riscv-011.c:724
static int step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv-011.c:1466
static int read_memory(struct target *target, const struct riscv_mem_access_args args)
Definition: riscv-011.c:2014
static void dbus_write(struct target *target, uint16_t address, uint64_t value)
Definition: riscv-011.c:496
static bool riscv011_get_impebreak(const struct target *target)
Definition: riscv-011.c:2420
static void dram_write32(struct target *target, unsigned int index, uint32_t value, bool set_interrupt)
Definition: riscv-011.c:680
static void cache_set32(struct target *target, unsigned int index, uint32_t data)
Definition: riscv-011.c:765
static void reg_cache_set(struct target *target, unsigned int number, uint64_t value)
Definition: riscv-011.c:1219
#define DMAUTHDATA0
Definition: riscv-011.c:157
#define DBUS_OP_START
Definition: riscv-011.c:102
static void dump_debug_ram(struct target *target)
Definition: riscv-011.c:811
static void scans_add_write_jump(scans_t *scans, uint16_t address, bool set_interrupt)
Add a 32-bit dbus write for an instruction that jumps to the beginning of debug RAM.
Definition: riscv-011.c:606
static uint32_t idcode_scan(struct target *target)
Definition: riscv-011.c:308
static void cache_set_jump(struct target *target, unsigned int index)
Definition: riscv-011.c:788
static int riscv011_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: riscv-011.c:1961
static void increase_interrupt_high_delay(struct target *target)
Definition: riscv-011.c:346
static int wait_for_state(struct target *target, enum target_state state)
Definition: riscv-011.c:1022
static void deinit_target(struct target *target)
Definition: riscv-011.c:1432
static void cache_set_store(struct target *target, unsigned int index, unsigned int reg, slot_t slot)
Definition: riscv-011.c:803
static int execute_resume(struct target *target, bool step)
Definition: riscv-011.c:1122
dbus_op_t
Definition: riscv-011.c:104
@ DBUS_OP_NOP
Definition: riscv-011.c:105
@ DBUS_OP_WRITE
Definition: riscv-011.c:107
@ DBUS_OP_READ
Definition: riscv-011.c:106
static int scans_execute(scans_t *scans)
Definition: riscv-011.c:578
static int handle_halt(struct target *target, bool announce)
Since almost everything can be accomplish by scanning the dbus register, all functions here assume db...
Definition: riscv-011.c:1852
static unsigned int riscv011_get_progbufsize(const struct target *target)
Definition: riscv-011.c:2425
#define DMINFO_AUTHENTICATED
Definition: riscv-011.c:152
static void scans_reset(scans_t *scans)
Definition: riscv-011.c:567
#define SETHALTNOT
Definition: riscv-011.c:91
#define DMINFO_SERIALCOUNT
Definition: riscv-011.c:145
static void dram_write_jump(struct target *target, unsigned int index, bool set_interrupt)
Definition: riscv-011.c:1014
static scans_t * scans_delete(scans_t *scans)
Definition: riscv-011.c:557
static int riscv011_authdata_write(struct target *target, uint32_t value, unsigned int index)
Definition: riscv-011.c:2404
static uint32_t store(const struct target *target, unsigned int src, unsigned int base, int16_t offset)
Definition: riscv-011.c:271
int riscv011_reg_init_all(struct target *target)
This file describes additional register cache interface available to the RISC-V Debug Specification v...
Definition: riscv-011_reg.c:40
struct scan_field select_idcode
Definition: riscv.c:53
unsigned int riscv_xlen(const struct target *target)
Definition: riscv.c:6094
struct scan_field select_dbus
Definition: riscv.c:48
int dtmcs_scan(struct jtag_tap *tap, uint32_t out, uint32_t *in_ptr)
Definition: riscv.c:416
int riscv_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: riscv.c:1735
int riscv_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: riscv.c:1755
int riscv_get_command_timeout_sec(void)
Definition: riscv.c:179
int riscv_enumerate_triggers(struct target *target)
Count triggers, and initialize trigger_count for each hart.
Definition: riscv.c:6250
static bool riscv_mem_access_is_valid(const struct riscv_mem_access_args args)
Definition: riscv.h:148
#define RISCV_INFO(R)
Definition: riscv.h:426
static struct riscv_info * riscv_info(const struct target *target) __attribute__((unused))
Definition: riscv.h:421
enum semihosting_result riscv_semihosting(struct target *target, int *retval)
Check for and process a semihosting request using the ARM protocol).
@ RISCV_MODE_M
Definition: riscv.h:371
@ RISCV_MODE_U
Definition: riscv.h:373
@ RISCV_MODE_S
Definition: riscv.h:372
uint64_t riscv_reg_t
Definition: riscv.h:46
static bool riscv_mem_access_is_write(const struct riscv_mem_access_args args)
Definition: riscv.h:161
static bool riscv_mem_access_is_read(const struct riscv_mem_access_args args)
Definition: riscv.h:154
static struct riscv_private_config * riscv_private_config(const struct target *target)
Definition: riscv.h:384
void riscv_reg_cache_invalidate_all(struct target *target)
Invalidate all registers - forget their cached register values.
Definition: riscv_reg.c:899
const char * riscv_reg_gdb_regno_name(const struct target *target, enum gdb_regno regno)
This file describes the register cache interface available to the RISC-V target.
Definition: riscv_reg.c:171
int riscv_reg_flush_all(struct target *target)
Write all dirty registers to the target.
Definition: riscv_reg.c:776
struct target * target
Definition: rtt/rtt.c:26
@ SEMIHOSTING_NONE
bool haltnot
Definition: riscv-011.c:207
bool interrupt
Definition: riscv-011.c:208
struct reg * reg_list
Definition: register.h:147
Definition: register.h:111
bool valid
Definition: register.h:126
uint32_t size
Definition: register.h:132
uint8_t * value
Definition: register.h:122
bool dirty
Definition: register.h:124
const char * name
Definition: register.h:113
unsigned int dramsize
Definition: riscv-011.c:176
uint64_t tselect
Definition: riscv-011.c:179
unsigned int dbus_busy_delay
Definition: riscv-011.c:195
bool tselect_dirty
Definition: riscv-011.c:180
unsigned int interrupt_high_delay
Definition: riscv-011.c:201
uint64_t dcsr
Definition: riscv-011.c:177
uint8_t addrbits
Definition: riscv-011.c:174
unsigned int dtmcontrol_idle
Definition: riscv-011.c:190
uint64_t dpc
Definition: riscv-011.c:178
int xlen
Definition: riscv.h:180
struct command_context * cmd_ctx
Definition: riscv.h:173
uint32_t increment
Definition: riscv.h:144
uint8_t * read_buffer
Definition: riscv.h:140
const uint8_t * write_buffer
Definition: riscv.h:139
target_addr_t address
Definition: riscv.h:137
uint32_t count
Definition: riscv.h:143
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
unsigned int scan_count
Definition: riscv-011.c:513
unsigned int scan_size
Definition: riscv-011.c:515
struct scan_field * field
Definition: riscv-011.c:519
const struct target * target
Definition: riscv-011.c:520
uint8_t * out
Definition: riscv-011.c:518
unsigned int next_scan
Definition: riscv-011.c:516
uint8_t * in
Definition: riscv-011.c:517
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:119
struct jtag_tap * tap
Definition: target.h:122
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
struct reg_cache * reg_cache
Definition: target.h:168
struct watchpoint * watchpoints
Definition: target.h:170
void * arch_info
Definition: target.h:174
bool reset_halt
Definition: target.h:154
struct watchpoint * next
Definition: breakpoints.h:49
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1794
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_WATCHPOINT
Definition: target.h:74
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
static bool target_was_examined(const struct target *target)
Definition: target.h:443
@ TARGET_EVENT_HALTED
Definition: target.h:265
target_state
Definition: target.h:55
@ TARGET_RESET
Definition: target.h:59
@ TARGET_DEBUG_RUNNING
Definition: target.h:60
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
static void target_set_examined(struct target *target)
Sets the examined and active_polled flags for the given target.
Definition: target.h:460
int64_t timeval_ms(void)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
uint64_t target_addr_t
Definition: types.h:279
#define TARGET_PRIxADDR
Definition: types.h:284
static struct ublast_lowlevel_priv info
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22