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