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