OpenOCD
esirisc.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2018 by Square, Inc. *
5  * Steven Stallion <stallion@squareup.com> *
6  * James Zhao <hjz@squareup.com> *
7  ***************************************************************************/
8 
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12 
13 #include <helper/binarybuffer.h>
14 #include <helper/command.h>
15 #include <helper/log.h>
16 #include <helper/time_support.h>
17 #include <helper/types.h>
18 #include <jtag/interface.h>
19 #include <target/breakpoints.h>
20 #include <target/register.h>
21 #include <target/target.h>
22 #include <target/target_type.h>
23 
24 #include "esirisc.h"
25 
26 #define RESET_TIMEOUT 5000 /* 5s */
27 #define STEP_TIMEOUT 1000 /* 1s */
28 
29 /*
30  * eSi-RISC targets support a configurable number of interrupts;
31  * up to 32 interrupts are supported.
32  */
33 static const char * const esirisc_exception_strings[] = {
34  [EID_RESET] = "Reset",
35  [EID_HARDWARE_FAILURE] = "HardwareFailure",
36  [EID_NMI] = "NMI",
37  [EID_INST_BREAKPOINT] = "InstBreakpoint",
38  [EID_DATA_BREAKPOINT] = "DataBreakpoint",
39  [EID_UNSUPPORTED] = "Unsupported",
40  [EID_PRIVILEGE_VIOLATION] = "PrivilegeViolation",
41  [EID_INST_BUS_ERROR] = "InstBusError",
42  [EID_DATA_BUS_ERROR] = "DataBusError",
43  [EID_ALIGNMENT_ERROR] = "AlignmentError",
44  [EID_ARITHMETIC_ERROR] = "ArithmeticError",
45  [EID_SYSTEM_CALL] = "SystemCall",
46  [EID_MEMORY_MANAGEMENT] = "MemoryManagement",
47  [EID_UNRECOVERABLE] = "Unrecoverable",
48  [EID_INTERRUPT_N+0] = "Interrupt0",
49  [EID_INTERRUPT_N+1] = "Interrupt1",
50  [EID_INTERRUPT_N+2] = "Interrupt2",
51  [EID_INTERRUPT_N+3] = "Interrupt3",
52  [EID_INTERRUPT_N+4] = "Interrupt4",
53  [EID_INTERRUPT_N+5] = "Interrupt5",
54  [EID_INTERRUPT_N+6] = "Interrupt6",
55  [EID_INTERRUPT_N+7] = "Interrupt7",
56  [EID_INTERRUPT_N+8] = "Interrupt8",
57  [EID_INTERRUPT_N+9] = "Interrupt9",
58  [EID_INTERRUPT_N+10] = "Interrupt10",
59  [EID_INTERRUPT_N+11] = "Interrupt11",
60  [EID_INTERRUPT_N+12] = "Interrupt12",
61  [EID_INTERRUPT_N+13] = "Interrupt13",
62  [EID_INTERRUPT_N+14] = "Interrupt14",
63  [EID_INTERRUPT_N+15] = "Interrupt15",
64  [EID_INTERRUPT_N+16] = "Interrupt16",
65  [EID_INTERRUPT_N+17] = "Interrupt17",
66  [EID_INTERRUPT_N+18] = "Interrupt18",
67  [EID_INTERRUPT_N+19] = "Interrupt19",
68  [EID_INTERRUPT_N+20] = "Interrupt20",
69  [EID_INTERRUPT_N+21] = "Interrupt21",
70  [EID_INTERRUPT_N+22] = "Interrupt22",
71  [EID_INTERRUPT_N+23] = "Interrupt23",
72  [EID_INTERRUPT_N+24] = "Interrupt24",
73  [EID_INTERRUPT_N+25] = "Interrupt25",
74  [EID_INTERRUPT_N+26] = "Interrupt26",
75  [EID_INTERRUPT_N+27] = "Interrupt27",
76  [EID_INTERRUPT_N+28] = "Interrupt28",
77  [EID_INTERRUPT_N+29] = "Interrupt29",
78  [EID_INTERRUPT_N+30] = "Interrupt30",
79  [EID_INTERRUPT_N+31] = "Interrupt31",
80 };
81 
82 /*
83  * eSi-RISC targets support a configurable number of general purpose
84  * registers; 8, 16, and 32 registers are supported.
85  */
86 static const struct {
88  const char *name;
89  enum reg_type type;
90  const char *group;
91 } esirisc_regs[] = {
92  { ESIRISC_SP, "sp", REG_TYPE_DATA_PTR, "general" },
93  { ESIRISC_RA, "ra", REG_TYPE_INT, "general" },
94  { ESIRISC_R2, "r2", REG_TYPE_INT, "general" },
95  { ESIRISC_R3, "r3", REG_TYPE_INT, "general" },
96  { ESIRISC_R4, "r4", REG_TYPE_INT, "general" },
97  { ESIRISC_R5, "r5", REG_TYPE_INT, "general" },
98  { ESIRISC_R6, "r6", REG_TYPE_INT, "general" },
99  { ESIRISC_R7, "r7", REG_TYPE_INT, "general" },
100  { ESIRISC_R8, "r8", REG_TYPE_INT, "general" },
101  { ESIRISC_R9, "r9", REG_TYPE_INT, "general" },
102  { ESIRISC_R10, "r10", REG_TYPE_INT, "general" },
103  { ESIRISC_R11, "r11", REG_TYPE_INT, "general" },
104  { ESIRISC_R12, "r12", REG_TYPE_INT, "general" },
105  { ESIRISC_R13, "r13", REG_TYPE_INT, "general" },
106  { ESIRISC_R14, "r14", REG_TYPE_INT, "general" },
107  { ESIRISC_R15, "r15", REG_TYPE_INT, "general" },
108  { ESIRISC_R16, "r16", REG_TYPE_INT, "general" },
109  { ESIRISC_R17, "r17", REG_TYPE_INT, "general" },
110  { ESIRISC_R18, "r18", REG_TYPE_INT, "general" },
111  { ESIRISC_R19, "r19", REG_TYPE_INT, "general" },
112  { ESIRISC_R20, "r20", REG_TYPE_INT, "general" },
113  { ESIRISC_R21, "r21", REG_TYPE_INT, "general" },
114  { ESIRISC_R22, "r22", REG_TYPE_INT, "general" },
115  { ESIRISC_R23, "r23", REG_TYPE_INT, "general" },
116  { ESIRISC_R24, "r24", REG_TYPE_INT, "general" },
117  { ESIRISC_R25, "r25", REG_TYPE_INT, "general" },
118  { ESIRISC_R26, "r26", REG_TYPE_INT, "general" },
119  { ESIRISC_R27, "r27", REG_TYPE_INT, "general" },
120  { ESIRISC_R28, "r28", REG_TYPE_INT, "general" },
121  { ESIRISC_R29, "r29", REG_TYPE_INT, "general" },
122  { ESIRISC_R30, "r30", REG_TYPE_INT, "general" },
123  { ESIRISC_R31, "r31", REG_TYPE_INT, "general" },
124 };
125 
126 /*
127  * Control and Status Registers (CSRs) are largely defined as belonging
128  * to the system register group. The exception to this rule are the PC
129  * and CAS registers, which belong to the general group. While debug is
130  * active, EPC, ECAS, and ETC must be used to read and write the PC,
131  * CAS, and TC CSRs, respectively.
132  */
133 static const struct {
134  enum esirisc_reg_num number;
135  uint8_t bank;
136  uint8_t csr;
137  const char *name;
138  enum reg_type type;
139  const char *group;
140 } esirisc_csrs[] = {
141  { ESIRISC_PC, CSR_THREAD, CSR_THREAD_EPC, "PC", REG_TYPE_CODE_PTR, "general" }, /* PC -> EPC */
142  { ESIRISC_CAS, CSR_THREAD, CSR_THREAD_ECAS, "CAS", REG_TYPE_INT, "general" }, /* CAS -> ECAS */
143  { ESIRISC_TC, CSR_THREAD, CSR_THREAD_ETC, "TC", REG_TYPE_INT, "system" }, /* TC -> ETC */
144  { ESIRISC_ETA, CSR_THREAD, CSR_THREAD_ETA, "ETA", REG_TYPE_INT, "system" },
145  { ESIRISC_ETC, CSR_THREAD, CSR_THREAD_ETC, "ETC", REG_TYPE_INT, "system" },
146  { ESIRISC_EPC, CSR_THREAD, CSR_THREAD_EPC, "EPC", REG_TYPE_CODE_PTR, "system" },
147  { ESIRISC_ECAS, CSR_THREAD, CSR_THREAD_ECAS, "ECAS", REG_TYPE_INT, "system" },
148  { ESIRISC_EID, CSR_THREAD, CSR_THREAD_EID, "EID", REG_TYPE_INT, "system" },
149  { ESIRISC_ED, CSR_THREAD, CSR_THREAD_ED, "ED", REG_TYPE_INT, "system" },
154 };
155 
157 {
158  struct esirisc_common *esirisc = target_to_esirisc(target);
159  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
160  uint32_t etc;
161  int retval;
162 
163  LOG_DEBUG("-");
164 
165  retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, &etc);
166  if (retval != ERROR_OK) {
167  LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
168  return retval;
169  }
170 
171  etc &= ~(1<<0); /* TC.I */
172 
173  retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, etc);
174  if (retval != ERROR_OK) {
175  LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
176  return retval;
177  }
178 
179  return ERROR_OK;
180 }
181 
182 #if 0
183 static int esirisc_enable_interrupts(struct target *target)
184 {
185  struct esirisc_common *esirisc = target_to_esirisc(target);
186  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
187  uint32_t etc;
188  int retval;
189 
190  LOG_DEBUG("-");
191 
192  retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, &etc);
193  if (retval != ERROR_OK) {
194  LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
195  return retval;
196  }
197 
198  etc |= (1<<0); /* TC.I */
199 
200  retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, etc);
201  if (retval != ERROR_OK) {
202  LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
203  return retval;
204  }
205 
206  return ERROR_OK;
207 }
208 #endif
209 
211 {
212  struct esirisc_common *esirisc = target_to_esirisc(target);
213  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
214 
215  LOG_DEBUG("-");
216 
217  int retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC,
218  &esirisc->etc_save);
219  if (retval != ERROR_OK) {
220  LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
221  return retval;
222  }
223 
224  return ERROR_OK;
225 }
226 
228 {
229  struct esirisc_common *esirisc = target_to_esirisc(target);
230  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
231 
232  LOG_DEBUG("-");
233 
234  int retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC,
235  esirisc->etc_save);
236  if (retval != ERROR_OK) {
237  LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
238  return retval;
239  }
240 
241  return ERROR_OK;
242 }
243 
244 #if 0
245 static int esirisc_save_hwdc(struct target *target)
246 {
247  struct esirisc_common *esirisc = target_to_esirisc(target);
248  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
249 
250  LOG_DEBUG("-");
251 
252  int retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_HWDC,
253  &esirisc->hwdc_save);
254  if (retval != ERROR_OK) {
255  LOG_ERROR("%s: failed to read Thread CSR: HWDC", target_name(target));
256  return retval;
257  }
258 
259  return ERROR_OK;
260 }
261 #endif
262 
263 static int esirisc_restore_hwdc(struct target *target)
264 {
265  struct esirisc_common *esirisc = target_to_esirisc(target);
266  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
267 
268  LOG_DEBUG("-");
269 
270  int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_HWDC,
271  esirisc->hwdc_save);
272  if (retval != ERROR_OK) {
273  LOG_ERROR("%s: failed to write Debug CSR: HWDC", target_name(target));
274  return retval;
275  }
276 
277  return ERROR_OK;
278 }
279 
280 static int esirisc_save_context(struct target *target)
281 {
282  struct esirisc_common *esirisc = target_to_esirisc(target);
283 
284  LOG_DEBUG("-");
285 
286  for (unsigned i = 0; i < esirisc->reg_cache->num_regs; ++i) {
287  struct reg *reg = esirisc->reg_cache->reg_list + i;
288  struct esirisc_reg *reg_info = reg->arch_info;
289 
290  if (reg->exist && !reg->valid)
291  reg_info->read(reg);
292  }
293 
294  return ERROR_OK;
295 }
296 
298 {
299  struct esirisc_common *esirisc = target_to_esirisc(target);
300 
301  LOG_DEBUG("-");
302 
303  for (unsigned i = 0; i < esirisc->reg_cache->num_regs; ++i) {
304  struct reg *reg = esirisc->reg_cache->reg_list + i;
305  struct esirisc_reg *reg_info = reg->arch_info;
306 
307  if (reg->exist && reg->dirty)
308  reg_info->write(reg);
309  }
310 
311  return ERROR_OK;
312 }
313 
314 static int esirisc_flush_caches(struct target *target)
315 {
316  struct esirisc_common *esirisc = target_to_esirisc(target);
317  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
318 
319  LOG_DEBUG("-");
320 
321  if (target->state != TARGET_HALTED)
323 
324  int retval = esirisc_jtag_flush_caches(jtag_info);
325  if (retval != ERROR_OK) {
326  LOG_ERROR("%s: failed to flush caches", target_name(target));
327  return retval;
328  }
329 
330  return ERROR_OK;
331 }
332 
333 static int esirisc_wait_debug_active(struct esirisc_common *esirisc, int ms)
334 {
335  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
336  int64_t t;
337 
338  LOG_DEBUG("-");
339 
340  t = timeval_ms();
341  for (;;) {
342  int retval = esirisc_jtag_enable_debug(jtag_info);
343  if (retval == ERROR_OK && esirisc_jtag_is_debug_active(jtag_info))
344  return retval;
345 
346  if ((timeval_ms() - t) > ms)
347  return ERROR_TARGET_TIMEOUT;
348 
349  alive_sleep(100);
350  }
351 }
352 
353 static int esirisc_read_memory(struct target *target, target_addr_t address,
354  uint32_t size, uint32_t count, uint8_t *buffer)
355 {
356  struct esirisc_common *esirisc = target_to_esirisc(target);
357  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
358  int retval;
359 
360  LOG_DEBUG("-");
361 
362  int num_bits = 8 * size;
363  for (uint32_t i = 0; i < count; ++i) {
364  union esirisc_memory value;
365  void *value_p;
366 
367  switch (size) {
368  case sizeof(value.word):
369  value_p = &value.word;
370  retval = esirisc_jtag_read_word(jtag_info, address, value_p);
371  break;
372 
373  case sizeof(value.hword):
374  value_p = &value.hword;
375  retval = esirisc_jtag_read_hword(jtag_info, address, value_p);
376  break;
377 
378  case sizeof(value.byte):
379  value_p = &value.byte;
380  retval = esirisc_jtag_read_byte(jtag_info, address, value_p);
381  break;
382 
383  default:
384  LOG_ERROR("%s: unsupported size: %" PRIu32, target_name(target), size);
385  return ERROR_FAIL;
386  }
387 
388  if (retval != ERROR_OK) {
389  LOG_ERROR("%s: failed to read address: 0x%" TARGET_PRIxADDR, target_name(target),
390  address);
391  return retval;
392  }
393 
394  buf_cpy(value_p, buffer, num_bits);
395  address += size;
396  buffer += size;
397  }
398 
399  return ERROR_OK;
400 }
401 
402 static int esirisc_write_memory(struct target *target, target_addr_t address,
403  uint32_t size, uint32_t count, const uint8_t *buffer)
404 {
405  struct esirisc_common *esirisc = target_to_esirisc(target);
406  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
407  int retval;
408 
409  LOG_DEBUG("-");
410 
411  int num_bits = 8 * size;
412  for (uint32_t i = 0; i < count; ++i) {
413  union esirisc_memory value;
414 
415  switch (size) {
416  case sizeof(value.word):
417  value.word = buf_get_u32(buffer, 0, num_bits);
418  retval = esirisc_jtag_write_word(jtag_info, address, value.word);
419  break;
420 
421  case sizeof(value.hword):
422  value.hword = buf_get_u32(buffer, 0, num_bits);
423  retval = esirisc_jtag_write_hword(jtag_info, address, value.hword);
424  break;
425 
426  case sizeof(value.byte):
427  value.byte = buf_get_u32(buffer, 0, num_bits);
428  retval = esirisc_jtag_write_byte(jtag_info, address, value.byte);
429  break;
430 
431  default:
432  LOG_ERROR("%s: unsupported size: %" PRIu32, target_name(target), size);
433  return ERROR_FAIL;
434  }
435 
436  if (retval != ERROR_OK) {
437  LOG_ERROR("%s: failed to write address: 0x%" TARGET_PRIxADDR, target_name(target),
438  address);
439  return retval;
440  }
441 
442  address += size;
443  buffer += size;
444  }
445 
446  return ERROR_OK;
447 }
448 
450  uint32_t count, uint32_t *checksum)
451 {
452  return ERROR_FAIL; /* not supported */
453 }
454 
456 {
457  struct esirisc_common *esirisc = target_to_esirisc(target);
458  struct breakpoint **breakpoints_p = esirisc->breakpoints_p;
459  struct breakpoint **breakpoints_e = breakpoints_p + esirisc->num_breakpoints;
460 
461  LOG_DEBUG("-");
462 
463  for (int bp_index = 0; breakpoints_p < breakpoints_e; ++breakpoints_p, ++bp_index)
464  if (!*breakpoints_p)
465  return bp_index;
466 
467  return -1;
468 }
469 
471 {
472  struct esirisc_common *esirisc = target_to_esirisc(target);
473  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
474  int bp_index;
475  uint32_t ibc;
476  int retval;
477 
478  LOG_DEBUG("-");
479 
480  /*
481  * The default linker scripts provided by the eSi-RISC toolchain do
482  * not specify attributes on memory regions, which results in
483  * incorrect application of software breakpoints by GDB. Targets
484  * must be configured with `gdb_breakpoint_override hard` as
485  * software breakpoints are not supported.
486  */
487  if (breakpoint->type != BKPT_HARD)
489 
490  bp_index = esirisc_next_breakpoint(target);
491  if (bp_index < 0) {
492  LOG_ERROR("%s: out of hardware breakpoints", target_name(target));
494  }
495 
496  breakpoint_hw_set(breakpoint, bp_index);
497  esirisc->breakpoints_p[bp_index] = breakpoint;
498 
499  /* specify instruction breakpoint address */
500  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBA_N + bp_index,
502  if (retval != ERROR_OK) {
503  LOG_ERROR("%s: failed to write Debug CSR: IBA", target_name(target));
504  return retval;
505  }
506 
507  /* enable instruction breakpoint */
508  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, &ibc);
509  if (retval != ERROR_OK) {
510  LOG_ERROR("%s: failed to read Debug CSR: IBC", target_name(target));
511  return retval;
512  }
513 
514  ibc |= (1 << bp_index); /* IBC.In */
515 
516  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, ibc);
517  if (retval != ERROR_OK) {
518  LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
519  return retval;
520  }
521 
522  return ERROR_OK;
523 }
524 
526 {
528 
529  LOG_DEBUG("-");
530 
531  while (breakpoint) {
532  if (!breakpoint->is_set)
534 
536  }
537 
538  return ERROR_OK;
539 }
540 
542 {
543  struct esirisc_common *esirisc = target_to_esirisc(target);
544  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
545  unsigned int bp_index = breakpoint->number;
546  uint32_t ibc;
547  int retval;
548 
549  LOG_DEBUG("-");
550 
551  /* disable instruction breakpoint */
552  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, &ibc);
553  if (retval != ERROR_OK) {
554  LOG_ERROR("%s: failed to read Debug CSR: IBC", target_name(target));
555  return retval;
556  }
557 
558  ibc &= ~(1 << bp_index); /* IBC.In */
559 
560  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, ibc);
561  if (retval != ERROR_OK) {
562  LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
563  return retval;
564  }
565 
566  esirisc->breakpoints_p[bp_index] = NULL;
567  breakpoint->is_set = false;
568 
569  return ERROR_OK;
570 }
571 
573 {
574  struct esirisc_common *esirisc = target_to_esirisc(target);
575  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
576 
577  LOG_DEBUG("-");
578 
579  /* clear instruction breakpoints */
580  int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, 0);
581  if (retval != ERROR_OK) {
582  LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
583  return retval;
584  }
585 
586  memset(esirisc->breakpoints_p, 0, sizeof(esirisc->breakpoints_p));
587 
588  return ERROR_OK;
589 }
590 
592 {
593  struct esirisc_common *esirisc = target_to_esirisc(target);
594  struct watchpoint **watchpoints_p = esirisc->watchpoints_p;
595  struct watchpoint **watchpoints_e = watchpoints_p + esirisc->num_watchpoints;
596 
597  LOG_DEBUG("-");
598 
599  for (int wp_index = 0; watchpoints_p < watchpoints_e; ++watchpoints_p, ++wp_index)
600  if (!*watchpoints_p)
601  return wp_index;
602 
603  return -1;
604 }
605 
607 {
608  struct esirisc_common *esirisc = target_to_esirisc(target);
609  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
610  int wp_index;
611  uint32_t dbs, dbc;
612  int retval;
613 
614  LOG_DEBUG("-");
615 
616  wp_index = esirisc_next_watchpoint(target);
617  if (wp_index < 0) {
618  LOG_ERROR("%s: out of hardware watchpoints", target_name(target));
619  return ERROR_FAIL;
620  }
621 
622  watchpoint_set(watchpoint, wp_index);
623  esirisc->watchpoints_p[wp_index] = watchpoint;
624 
625  /* specify data breakpoint address */
626  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBA_N + wp_index,
628  if (retval != ERROR_OK) {
629  LOG_ERROR("%s: failed to write Debug CSR: DBA", target_name(target));
630  return retval;
631  }
632 
633  /* specify data breakpoint size */
634  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBS, &dbs);
635  if (retval != ERROR_OK) {
636  LOG_ERROR("%s: failed to read Debug CSR: DBS", target_name(target));
637  return retval;
638  }
639 
640  uint32_t sn;
641  switch (watchpoint->length) {
642  case sizeof(uint64_t):
643  sn = 0x3;
644  break;
645  case sizeof(uint32_t):
646  sn = 0x2;
647  break;
648 
649  case sizeof(uint16_t):
650  sn = 0x1;
651  break;
652 
653  case sizeof(uint8_t):
654  sn = 0x0;
655  break;
656 
657  default:
658  LOG_ERROR("%s: unsupported length: %" PRIu32, target_name(target),
659  watchpoint->length);
660  return ERROR_FAIL;
661  }
662 
663  dbs |= (sn << (2 * wp_index)); /* DBS.Sn */
664 
665  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBS, dbs);
666  if (retval != ERROR_OK) {
667  LOG_ERROR("%s: failed to write Debug CSR: DBS", target_name(target));
668  return retval;
669  }
670 
671  /* enable data breakpoint */
672  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
673  if (retval != ERROR_OK) {
674  LOG_ERROR("%s: failed to read Debug CSR: DBC", target_name(target));
675  return retval;
676  }
677 
678  uint32_t dn;
679  switch (watchpoint->rw) {
680  case WPT_READ:
681  dn = 0x1;
682  break;
683 
684  case WPT_WRITE:
685  dn = 0x2;
686  break;
687 
688  case WPT_ACCESS:
689  dn = 0x3;
690  break;
691 
692  default:
693  LOG_ERROR("%s: unsupported rw: %" PRId32, target_name(target),
694  watchpoint->rw);
695  return ERROR_FAIL;
696  }
697 
698  dbc |= (dn << (2 * wp_index)); /* DBC.Dn */
699 
700  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
701  if (retval != ERROR_OK) {
702  LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
703  return retval;
704  }
705 
706  return ERROR_OK;
707 }
708 
710 {
712 
713  LOG_DEBUG("-");
714 
715  while (watchpoint) {
716  if (!watchpoint->is_set)
718 
720  }
721 
722  return ERROR_OK;
723 }
724 
726 {
727  struct esirisc_common *esirisc = target_to_esirisc(target);
728  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
729  unsigned int wp_index = watchpoint->number;
730  uint32_t dbc;
731  int retval;
732 
733  LOG_DEBUG("-");
734 
735  /* disable data breakpoint */
736  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
737  if (retval != ERROR_OK) {
738  LOG_ERROR("%s: failed to read Debug CSR: DBC", target_name(target));
739  return retval;
740  }
741 
742  dbc &= ~(0x3 << (2 * wp_index)); /* DBC.Dn */
743 
744  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
745  if (retval != ERROR_OK) {
746  LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
747  return retval;
748  }
749 
750  esirisc->watchpoints_p[wp_index] = NULL;
751  watchpoint->is_set = false;
752 
753  return ERROR_OK;
754 }
755 
757 {
758  struct esirisc_common *esirisc = target_to_esirisc(target);
759  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
760 
761  LOG_DEBUG("-");
762 
763  /* clear data breakpoints */
764  int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, 0);
765  if (retval != ERROR_OK) {
766  LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
767  return retval;
768  }
769 
770  memset(esirisc->watchpoints_p, 0, sizeof(esirisc->watchpoints_p));
771 
772  return ERROR_OK;
773 }
774 
775 static int esirisc_halt(struct target *target)
776 {
777  struct esirisc_common *esirisc = target_to_esirisc(target);
778  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
779 
780  LOG_DEBUG("-");
781 
782  if (target->state == TARGET_HALTED)
783  return ERROR_OK;
784 
785  int retval = esirisc_jtag_break(jtag_info);
786  if (retval != ERROR_OK) {
787  LOG_ERROR("%s: failed to halt target", target_name(target));
788  return retval;
789  }
790 
792 
793  return ERROR_OK;
794 }
795 
796 static int esirisc_disable_step(struct target *target)
797 {
798  struct esirisc_common *esirisc = target_to_esirisc(target);
799  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
800  uint32_t dc;
801  int retval;
802 
803  LOG_DEBUG("-");
804 
805  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
806  if (retval != ERROR_OK) {
807  LOG_ERROR("%s: failed to read Debug CSR: DC", target_name(target));
808  return retval;
809  }
810 
811  dc &= ~(1<<0); /* DC.S */
812 
813  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
814  if (retval != ERROR_OK) {
815  LOG_ERROR("%s: failed to write Debug CSR: DC", target_name(target));
816  return retval;
817  }
818 
819  return ERROR_OK;
820 }
821 
822 static int esirisc_enable_step(struct target *target)
823 {
824  struct esirisc_common *esirisc = target_to_esirisc(target);
825  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
826  uint32_t dc;
827  int retval;
828 
829  LOG_DEBUG("-");
830 
831  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
832  if (retval != ERROR_OK) {
833  LOG_ERROR("%s: failed to read Debug CSR: DC", target_name(target));
834  return retval;
835  }
836 
837  dc |= (1<<0); /* DC.S */
838 
839  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
840  if (retval != ERROR_OK) {
841  LOG_ERROR("%s: failed to write Debug CSR: DC", target_name(target));
842  return retval;
843  }
844 
845  return ERROR_OK;
846 }
847 
848 static int esirisc_resume_or_step(struct target *target, int current, target_addr_t address,
849  int handle_breakpoints, int debug_execution, bool step)
850 {
851  struct esirisc_common *esirisc = target_to_esirisc(target);
852  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
853  struct breakpoint *breakpoint = NULL;
854  int retval;
855 
856  LOG_DEBUG("-");
857 
858  if (target->state != TARGET_HALTED)
860 
861  if (!debug_execution) {
865  }
866 
867  if (current)
868  address = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
869  else {
870  buf_set_u32(esirisc->epc->value, 0, esirisc->epc->size, address);
871  esirisc->epc->dirty = true;
872  esirisc->epc->valid = true;
873  }
874 
876 
877  if (esirisc_has_cache(esirisc))
879 
880  if (handle_breakpoints) {
882  if (breakpoint)
884  }
885 
886  if (step) {
890  } else {
894  }
895 
897 
898  retval = esirisc_jtag_continue(jtag_info);
899  if (retval != ERROR_OK) {
900  LOG_ERROR("%s: failed to resume target", target_name(target));
901  return retval;
902  }
903 
905 
906  if (!debug_execution) {
909  } else {
912  }
913 
914  return ERROR_OK;
915 }
916 
917 static int esirisc_resume(struct target *target, int current, target_addr_t address,
918  int handle_breakpoints, int debug_execution)
919 {
920  LOG_DEBUG("-");
921 
922  return esirisc_resume_or_step(target, current, address,
923  handle_breakpoints, debug_execution, false);
924 }
925 
926 static int esirisc_step(struct target *target, int current, target_addr_t address,
927  int handle_breakpoints)
928 {
929  LOG_DEBUG("-");
930 
931  return esirisc_resume_or_step(target, current, address,
932  handle_breakpoints, 0, true);
933 }
934 
935 static int esirisc_debug_step(struct target *target)
936 {
937  struct esirisc_common *esirisc = target_to_esirisc(target);
938  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
939  int retval;
940 
941  LOG_DEBUG("-");
942 
945 
946  retval = esirisc_jtag_continue(jtag_info);
947  if (retval != ERROR_OK) {
948  LOG_ERROR("%s: failed to resume target", target_name(target));
949  return retval;
950  }
951 
952  retval = esirisc_wait_debug_active(esirisc, STEP_TIMEOUT);
953  if (retval != ERROR_OK) {
954  LOG_ERROR("%s: step timed out", target_name(target));
955  return retval;
956  }
957 
960 
961  return ERROR_OK;
962 }
963 
964 static int esirisc_debug_reset(struct target *target)
965 {
966  struct esirisc_common *esirisc = target_to_esirisc(target);
967  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
968  int retval;
969 
970  LOG_DEBUG("-");
971 
972  retval = esirisc_jtag_assert_reset(jtag_info);
973  if (retval != ERROR_OK) {
974  LOG_ERROR("%s: failed to assert reset", target_name(target));
975  return retval;
976  }
977 
978  retval = esirisc_jtag_deassert_reset(jtag_info);
979  if (retval != ERROR_OK) {
980  LOG_ERROR("%s: failed to deassert reset", target_name(target));
981  return retval;
982  }
983 
984  retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
985  if (retval != ERROR_OK) {
986  LOG_ERROR("%s: reset timed out", target_name(target));
987  return retval;
988  }
989 
990  return ERROR_OK;
991 }
992 
993 static int esirisc_debug_enable(struct target *target)
994 {
995  struct esirisc_common *esirisc = target_to_esirisc(target);
996  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
997  int retval;
998 
999  LOG_DEBUG("-");
1000 
1001  retval = esirisc_jtag_enable_debug(jtag_info);
1002  if (retval != ERROR_OK) {
1003  LOG_ERROR("%s: failed to enable debug mode", target_name(target));
1004  return retval;
1005  }
1006 
1007  /*
1008  * The debug clock is inactive until the first command is sent.
1009  * If the target is stopped, we must first issue a reset before
1010  * attempting further communication. This also handles unpowered
1011  * targets, which will respond with all ones and appear active.
1012  */
1013  if (esirisc_jtag_is_stopped(jtag_info)) {
1014  LOG_INFO("%s: debug clock inactive; attempting debug reset", target_name(target));
1015  retval = esirisc_debug_reset(target);
1016  if (retval != ERROR_OK)
1017  return retval;
1018 
1019  if (esirisc_jtag_is_stopped(jtag_info)) {
1020  LOG_ERROR("%s: target unresponsive; giving up", target_name(target));
1021  return ERROR_FAIL;
1022  }
1023  }
1024 
1025  return ERROR_OK;
1026 }
1027 
1028 static int esirisc_debug_entry(struct target *target)
1029 {
1030  struct esirisc_common *esirisc = target_to_esirisc(target);
1031  struct breakpoint *breakpoint;
1032 
1033  LOG_DEBUG("-");
1034 
1036 
1037  if (esirisc_has_cache(esirisc))
1039 
1042 
1043  uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
1044  switch (eid) {
1045  /*
1046  * InstBreakpoint exceptions are also raised when a core is
1047  * halted for debugging. The following is required to
1048  * determine if a breakpoint was encountered.
1049  */
1050  case EID_INST_BREAKPOINT:
1052  buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size));
1055  break;
1056 
1057  /*
1058  * eSi-RISC treats watchpoints similarly to breakpoints,
1059  * however GDB will not request to step over the current
1060  * instruction when a watchpoint fires. The following is
1061  * required to resume the target.
1062  */
1063  case EID_DATA_BREAKPOINT:
1068  break;
1069 
1070  default:
1072  }
1073  }
1074 
1075  return ERROR_OK;
1076 }
1077 
1078 static int esirisc_poll(struct target *target)
1079 {
1080  struct esirisc_common *esirisc = target_to_esirisc(target);
1081  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1082  int retval;
1083 
1084  retval = esirisc_jtag_enable_debug(jtag_info);
1085  if (retval != ERROR_OK) {
1086  LOG_ERROR("%s: failed to poll target", target_name(target));
1087  return retval;
1088  }
1089 
1090  if (esirisc_jtag_is_stopped(jtag_info)) {
1091  LOG_ERROR("%s: target has stopped; reset required", target_name(target));
1093  return ERROR_TARGET_FAILURE;
1094  }
1095 
1096  if (esirisc_jtag_is_debug_active(jtag_info)) {
1099 
1100  retval = esirisc_debug_entry(target);
1101  if (retval != ERROR_OK)
1102  return retval;
1103 
1105  }
1106 
1107  } else if (target->state == TARGET_HALTED || target->state == TARGET_RESET) {
1110  }
1111 
1112  return ERROR_OK;
1113 }
1114 
1116 {
1117  struct esirisc_common *esirisc = target_to_esirisc(target);
1118  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1119  int retval;
1120 
1121  LOG_DEBUG("-");
1122 
1124  jtag_add_reset(1, 1);
1126  jtag_add_reset(0, 1);
1127  } else {
1130 
1131  retval = esirisc_jtag_assert_reset(jtag_info);
1132  if (retval != ERROR_OK) {
1133  LOG_ERROR("%s: failed to assert reset", target_name(target));
1134  return retval;
1135  }
1136  }
1137 
1139 
1141 
1142  return ERROR_OK;
1143 }
1144 
1145 static int esirisc_reset_entry(struct target *target)
1146 {
1147  struct esirisc_common *esirisc = target_to_esirisc(target);
1148  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1149  uint32_t eta, epc;
1150  int retval;
1151 
1152  LOG_DEBUG("-");
1153 
1154  /* read exception table address */
1155  retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETA, &eta);
1156  if (retval != ERROR_OK) {
1157  LOG_ERROR("%s: failed to read Thread CSR: ETA", target_name(target));
1158  return retval;
1159  }
1160 
1161  /* read reset entry point */
1162  retval = esirisc_jtag_read_word(jtag_info, eta + ENTRY_RESET, &epc);
1163  if (retval != ERROR_OK) {
1164  LOG_ERROR("%s: failed to read address: 0x%" TARGET_PRIxADDR, target_name(target),
1165  (target_addr_t)epc);
1166  return retval;
1167  }
1168 
1169  /* write reset entry point */
1170  retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_EPC, epc);
1171  if (retval != ERROR_OK) {
1172  LOG_ERROR("%s: failed to write Thread CSR: EPC", target_name(target));
1173  return retval;
1174  }
1175 
1176  return ERROR_OK;
1177 }
1178 
1180 {
1181  struct esirisc_common *esirisc = target_to_esirisc(target);
1182  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1183  int retval;
1184 
1185  LOG_DEBUG("-");
1186 
1188  jtag_add_reset(0, 0);
1189 
1190  retval = esirisc_debug_enable(target);
1191  if (retval != ERROR_OK)
1192  return retval;
1193 
1194  retval = esirisc_debug_reset(target);
1195  if (retval != ERROR_OK)
1196  return retval;
1197 
1198  } else {
1199  retval = esirisc_jtag_deassert_reset(jtag_info);
1200  if (retval != ERROR_OK) {
1201  LOG_ERROR("%s: failed to deassert reset", target_name(target));
1202  return retval;
1203  }
1204  }
1205 
1206  retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
1207  if (retval != ERROR_OK) {
1208  LOG_ERROR("%s: reset timed out", target_name(target));
1209  return retval;
1210  }
1211 
1212  retval = esirisc_reset_entry(target);
1213  if (retval != ERROR_OK)
1214  return retval;
1215 
1218 
1220 
1221  if (!target->reset_halt) {
1222  retval = esirisc_jtag_continue(jtag_info);
1223  if (retval != ERROR_OK) {
1224  LOG_ERROR("%s: failed to resume target", target_name(target));
1225  return retval;
1226  }
1227  }
1228 
1229  return ERROR_OK;
1230 }
1231 
1232 static int esirisc_arch_state(struct target *target)
1233 {
1234  struct esirisc_common *esirisc = target_to_esirisc(target);
1235  uint32_t epc = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
1236  uint32_t ecas = buf_get_u32(esirisc->ecas->value, 0, esirisc->ecas->size);
1237  uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
1238  uint32_t ed = buf_get_u32(esirisc->ed->value, 0, esirisc->ed->size);
1239 
1240  LOG_USER("target halted due to %s, exception: %s\n"
1241  "EPC: 0x%" PRIx32 ", ECAS: 0x%" PRIx32 ", EID: 0x%" PRIx32 ", ED: 0x%" PRIx32,
1243 
1244  return ERROR_OK;
1245 }
1246 
1247 static const char *esirisc_get_gdb_arch(struct target *target)
1248 {
1249  struct esirisc_common *esirisc = target_to_esirisc(target);
1250 
1251  LOG_DEBUG("-");
1252 
1253  /*
1254  * Targets with the UNIFIED_ADDRESS_SPACE option disabled employ a
1255  * Harvard architecture. This option is not exposed in a CSR, which
1256  * requires additional configuration to properly interact with these
1257  * targets in GDB (also see: `esirisc cache_arch`).
1258  */
1259  if (!esirisc->gdb_arch && target_was_examined(target))
1260  esirisc->gdb_arch = alloc_printf("esirisc:%d_bit_%d_reg_%s",
1261  esirisc->num_bits, esirisc->num_regs, esirisc_cache_arch_name(esirisc));
1262 
1263  return esirisc->gdb_arch;
1264 }
1265 
1266 static int esirisc_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
1267  int *reg_list_size, enum target_register_class reg_class)
1268 {
1269  struct esirisc_common *esirisc = target_to_esirisc(target);
1270 
1271  LOG_DEBUG("-");
1272 
1273  *reg_list_size = ESIRISC_NUM_REGS;
1274 
1275  *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
1276  if (!*reg_list)
1277  return ERROR_FAIL;
1278 
1279  if (reg_class == REG_CLASS_ALL)
1280  for (int i = 0; i < *reg_list_size; ++i)
1281  (*reg_list)[i] = esirisc->reg_cache->reg_list + i;
1282  else {
1283  for (int i = 0; i < esirisc->num_regs; ++i)
1284  (*reg_list)[i] = esirisc->reg_cache->reg_list + i;
1285 
1286  (*reg_list)[ESIRISC_PC] = esirisc->reg_cache->reg_list + ESIRISC_PC;
1287  (*reg_list)[ESIRISC_CAS] = esirisc->reg_cache->reg_list + ESIRISC_CAS;
1288  }
1289 
1290  return ERROR_OK;
1291 }
1292 
1293 static int esirisc_read_reg(struct reg *reg)
1294 {
1295  struct esirisc_reg *reg_info = reg->arch_info;
1296  struct esirisc_common *esirisc = reg_info->esirisc;
1297  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1298  struct target *target = esirisc->target;
1299  uint32_t data;
1300 
1301  LOG_DEBUG("-");
1302 
1303  int retval = esirisc_jtag_read_reg(jtag_info, reg->number, &data);
1304  if (retval != ERROR_OK) {
1305  LOG_ERROR("%s: failed to read register: %s", target_name(target), reg->name);
1306  return retval;
1307  }
1308 
1309  buf_set_u32(reg->value, 0, reg->size, data);
1310  reg->dirty = false;
1311  reg->valid = true;
1312 
1313  return ERROR_OK;
1314 }
1315 
1316 static int esirisc_write_reg(struct reg *reg)
1317 {
1318  struct esirisc_reg *reg_info = reg->arch_info;
1319  struct esirisc_common *esirisc = reg_info->esirisc;
1320  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1321  struct target *target = esirisc->target;
1322  uint32_t data = buf_get_u32(reg->value, 0, reg->size);
1323 
1324  LOG_DEBUG("-");
1325 
1326  int retval = esirisc_jtag_write_reg(jtag_info, reg->number, data);
1327  if (retval != ERROR_OK) {
1328  LOG_ERROR("%s: failed to write register: %s", target_name(target), reg->name);
1329  return retval;
1330  }
1331 
1332  reg->dirty = false;
1333  reg->valid = true;
1334 
1335  return ERROR_OK;
1336 }
1337 
1338 static int esirisc_read_csr(struct reg *reg)
1339 {
1340  struct esirisc_reg *reg_info = reg->arch_info;
1341  struct esirisc_common *esirisc = reg_info->esirisc;
1342  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1343  struct target *target = esirisc->target;
1344  uint32_t data;
1345 
1346  LOG_DEBUG("-");
1347 
1348  int retval = esirisc_jtag_read_csr(jtag_info, reg_info->bank, reg_info->csr, &data);
1349  if (retval != ERROR_OK) {
1350  LOG_ERROR("%s: failed to read CSR: %s", target_name(target), reg->name);
1351  return retval;
1352  }
1353 
1354  buf_set_u32(reg->value, 0, reg->size, data);
1355  reg->dirty = false;
1356  reg->valid = true;
1357 
1358  return ERROR_OK;
1359 }
1360 
1361 static int esirisc_write_csr(struct reg *reg)
1362 {
1363  struct esirisc_reg *reg_info = reg->arch_info;
1364  struct esirisc_common *esirisc = reg_info->esirisc;
1365  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1366  struct target *target = esirisc->target;
1367  uint32_t data = buf_get_u32(reg->value, 0, reg->size);
1368 
1369  LOG_DEBUG("-");
1370 
1371  int retval = esirisc_jtag_write_csr(jtag_info, reg_info->bank, reg_info->csr, data);
1372  if (retval != ERROR_OK) {
1373  LOG_ERROR("%s: failed to write CSR: %s", target_name(target), reg->name);
1374  return retval;
1375  }
1376 
1377  reg->dirty = false;
1378  reg->valid = true;
1379 
1380  return ERROR_OK;
1381 }
1382 
1383 static int esirisc_get_reg(struct reg *reg)
1384 {
1385  struct esirisc_reg *reg_info = reg->arch_info;
1386  struct esirisc_common *esirisc = reg_info->esirisc;
1387  struct target *target = esirisc->target;
1388 
1389  LOG_DEBUG("-");
1390 
1391  if (target->state != TARGET_HALTED)
1392  return ERROR_TARGET_NOT_HALTED;
1393 
1394  return reg_info->read(reg);
1395 }
1396 
1397 static int esirisc_set_reg(struct reg *reg, uint8_t *buf)
1398 {
1399  struct esirisc_reg *reg_info = reg->arch_info;
1400  struct esirisc_common *esirisc = reg_info->esirisc;
1401  struct target *target = esirisc->target;
1402  uint32_t value = buf_get_u32(buf, 0, reg->size);
1403 
1404  LOG_DEBUG("-");
1405 
1406  if (target->state != TARGET_HALTED)
1407  return ERROR_TARGET_NOT_HALTED;
1408 
1409  buf_set_u32(reg->value, 0, reg->size, value);
1410  reg->dirty = true;
1411  reg->valid = true;
1412 
1413  return ERROR_OK;
1414 }
1415 
1416 static const struct reg_arch_type esirisc_reg_type = {
1417  .get = esirisc_get_reg,
1418  .set = esirisc_set_reg,
1419 };
1420 
1422 {
1423  struct esirisc_common *esirisc = target_to_esirisc(target);
1424  struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
1425  struct reg_cache *cache = malloc(sizeof(struct reg_cache));
1426  struct reg *reg_list = calloc(ESIRISC_NUM_REGS, sizeof(struct reg));
1427 
1428  LOG_DEBUG("-");
1429 
1430  cache->name = "eSi-RISC registers";
1431  cache->next = NULL;
1432  cache->reg_list = reg_list;
1433  cache->num_regs = ESIRISC_NUM_REGS;
1434  (*cache_p) = cache;
1435 
1436  esirisc->reg_cache = cache;
1437  esirisc->epc = reg_list + ESIRISC_EPC;
1438  esirisc->ecas = reg_list + ESIRISC_ECAS;
1439  esirisc->eid = reg_list + ESIRISC_EID;
1440  esirisc->ed = reg_list + ESIRISC_ED;
1441 
1442  for (int i = 0; i < esirisc->num_regs; ++i) {
1443  struct reg *reg = reg_list + esirisc_regs[i].number;
1444  struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));
1445 
1446  reg->name = esirisc_regs[i].name;
1447  reg->number = esirisc_regs[i].number;
1448  reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
1449  reg->size = esirisc->num_bits;
1450  reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
1451  reg->reg_data_type->type = esirisc_regs[i].type;
1452  reg->group = esirisc_regs[i].group;
1453  reg_info->esirisc = esirisc;
1454  reg_info->read = esirisc_read_reg;
1455  reg_info->write = esirisc_write_reg;
1456  reg->arch_info = reg_info;
1458  reg->exist = true;
1459  }
1460 
1461  for (size_t i = 0; i < ARRAY_SIZE(esirisc_csrs); ++i) {
1462  struct reg *reg = reg_list + esirisc_csrs[i].number;
1463  struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));
1464 
1465  reg->name = esirisc_csrs[i].name;
1466  reg->number = esirisc_csrs[i].number;
1467  reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
1468  reg->size = esirisc->num_bits;
1469  reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
1470  reg->reg_data_type->type = esirisc_csrs[i].type;
1471  reg->group = esirisc_csrs[i].group;
1472  reg_info->esirisc = esirisc;
1473  reg_info->bank = esirisc_csrs[i].bank;
1474  reg_info->csr = esirisc_csrs[i].csr;
1475  reg_info->read = esirisc_read_csr;
1476  reg_info->write = esirisc_write_csr;
1477  reg->arch_info = reg_info;
1479  reg->exist = true;
1480  }
1481 
1482  return cache;
1483 }
1484 
1485 static int esirisc_identify(struct target *target)
1486 {
1487  struct esirisc_common *esirisc = target_to_esirisc(target);
1488  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1489  uint32_t csr;
1490  int retval;
1491 
1492  LOG_DEBUG("-");
1493 
1494  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_ARCH0, &csr);
1495  if (retval != ERROR_OK) {
1496  LOG_ERROR("%s: failed to read Configuration CSR: ARCH0", target_name(target));
1497  return retval;
1498  }
1499 
1500  esirisc->num_bits = (csr >> 0) & 0x3f; /* ARCH0.B */
1501  esirisc->num_regs = (csr >> 10) & 0x3f; /* ARCH0.R */
1502 
1503  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_MEM, &csr);
1504  if (retval != ERROR_OK) {
1505  LOG_ERROR("%s: failed to read Configuration CSR: MEM", target_name(target));
1506  return retval;
1507  }
1508 
1509  target->endianness = (csr & 1<<0) ? /* MEM.E */
1511 
1512  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_IC, &csr);
1513  if (retval != ERROR_OK) {
1514  LOG_ERROR("%s: failed to read Configuration CSR: IC", target_name(target));
1515  return retval;
1516  }
1517 
1518  esirisc->has_icache = !!(csr & 1<<0); /* IC.E */
1519 
1520  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DC, &csr);
1521  if (retval != ERROR_OK) {
1522  LOG_ERROR("%s: failed to read Configuration CSR: DC", target_name(target));
1523  return retval;
1524  }
1525 
1526  esirisc->has_dcache = !!(csr & 1<<0); /* DC.E */
1527 
1528  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DBG, &csr);
1529  if (retval != ERROR_OK) {
1530  LOG_ERROR("%s: failed to read Configuration CSR: DBG", target_name(target));
1531  return retval;
1532  }
1533 
1534  esirisc->num_breakpoints = (csr >> 7) & 0xf; /* DBG.BP */
1535  esirisc->num_watchpoints = (csr >> 12) & 0xf; /* DBG.WP */
1536 
1537  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_TRACE, &csr);
1538  if (retval != ERROR_OK) {
1539  LOG_ERROR("%s: failed to read Configuration CSR: TRACE", target_name(target));
1540  return retval;
1541  }
1542 
1543  esirisc->has_trace = !!(csr & 1<<0); /* TRACE.T */
1544 
1545  return ERROR_OK;
1546 }
1547 
1548 static int esirisc_target_create(struct target *target, Jim_Interp *interp)
1549 {
1550  struct jtag_tap *tap = target->tap;
1551  struct esirisc_common *esirisc;
1552 
1553  if (!tap)
1554  return ERROR_FAIL;
1555 
1556  if (tap->ir_length != INSTR_LENGTH) {
1557  LOG_ERROR("%s: invalid IR length; expected %d", target_name(target),
1558  INSTR_LENGTH);
1559  return ERROR_FAIL;
1560  }
1561 
1562  esirisc = calloc(1, sizeof(struct esirisc_common));
1563  if (!esirisc)
1564  return ERROR_FAIL;
1565 
1566  esirisc->target = target;
1567  esirisc->jtag_info.tap = tap;
1568  target->arch_info = esirisc;
1569 
1570  return ERROR_OK;
1571 }
1572 
1573 static int esirisc_init_target(struct command_context *cmd_ctx, struct target *target)
1574 {
1575  struct esirisc_common *esirisc = target_to_esirisc(target);
1576 
1577  /* trap reset, error, and debug exceptions */
1578  esirisc->hwdc_save = HWDC_R | HWDC_E | HWDC_D;
1579 
1580  return ERROR_OK;
1581 }
1582 
1583 static int esirisc_examine(struct target *target)
1584 {
1585  struct esirisc_common *esirisc = target_to_esirisc(target);
1586  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1587  int retval;
1588 
1589  LOG_DEBUG("-");
1590 
1591  if (!target_was_examined(target)) {
1592  retval = esirisc_debug_enable(target);
1593  if (retval != ERROR_OK)
1594  return retval;
1595 
1596  /*
1597  * In order to identify the target we must first halt the core.
1598  * We quietly resume once identification has completed for those
1599  * targets that were running when target_examine was called.
1600  */
1601  if (esirisc_jtag_is_debug_active(jtag_info)) {
1602  if (target->state == TARGET_UNKNOWN)
1604 
1606  } else {
1607  retval = esirisc_jtag_break(jtag_info);
1608  if (retval != ERROR_OK) {
1609  LOG_ERROR("%s: failed to halt target", target_name(target));
1610  return retval;
1611  }
1612 
1614  }
1615 
1616  retval = esirisc_identify(target);
1617  if (retval != ERROR_OK) {
1618  LOG_ERROR("%s: failed to identify target", target_name(target));
1619  return retval;
1620  }
1621 
1623 
1626 
1629 
1630  if (target->state == TARGET_HALTED)
1632  else {
1633  retval = esirisc_jtag_continue(jtag_info);
1634  if (retval != ERROR_OK) {
1635  LOG_ERROR("%s: failed to resume target", target_name(target));
1636  return retval;
1637  }
1638  }
1639 
1641 
1642  LOG_INFO("%s: %d bit, %d registers, %s%s%s", target_name(target),
1643  esirisc->num_bits, esirisc->num_regs,
1645  esirisc->has_icache ? ", icache" : "",
1646  esirisc->has_dcache ? ", dcache" : "");
1647 
1648  LOG_INFO("%s: hardware has %d breakpoints, %d watchpoints%s", target_name(target),
1649  esirisc->num_breakpoints, esirisc->num_watchpoints,
1650  esirisc->has_trace ? ", trace" : "");
1651  }
1652 
1653  return ERROR_OK;
1654 }
1655 
1656 COMMAND_HANDLER(handle_esirisc_cache_arch_command)
1657 {
1659  struct esirisc_common *esirisc = target_to_esirisc(target);
1660 
1661  if (CMD_ARGC > 0) {
1662  if (strcmp(*CMD_ARGV, "harvard") == 0)
1663  esirisc->cache_arch = ESIRISC_CACHE_HARVARD;
1664  else if (strcmp(*CMD_ARGV, "von_neumann") == 0)
1666  else {
1667  LOG_ERROR("invalid cache_arch: %s", *CMD_ARGV);
1669  }
1670  }
1671 
1672  command_print(CMD, "esirisc cache_arch %s", esirisc_cache_arch_name(esirisc));
1673 
1674  return ERROR_OK;
1675 }
1676 
1677 COMMAND_HANDLER(handle_esirisc_flush_caches_command)
1678 {
1680  struct esirisc_common *esirisc = target_to_esirisc(target);
1681  int retval;
1682 
1683  if (!esirisc_has_cache(esirisc)) {
1684  LOG_ERROR("target does not support caching");
1685  return ERROR_FAIL;
1686  }
1687 
1688  retval = esirisc_flush_caches(target);
1689 
1690  command_print(CMD, "cache flush %s",
1691  (retval == ERROR_OK) ? "successful" : "failed");
1692 
1693  return retval;
1694 }
1695 
1696 static const struct {
1697  const char *name;
1698  int mask;
1699 } esirisc_hwdc_masks[] = {
1700  { "reset", HWDC_R },
1701  { "interrupt", HWDC_I },
1702  { "syscall", HWDC_S },
1703  { "error", HWDC_E },
1704  { "debug", HWDC_D },
1705 };
1706 
1707 static int esirisc_find_hwdc_mask(const char *name)
1708 {
1709  for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
1710  if (strcmp(esirisc_hwdc_masks[i].name, name) == 0)
1711  return esirisc_hwdc_masks[i].mask;
1712 
1713  return -1;
1714 }
1715 
1716 COMMAND_HANDLER(handle_esirisc_hwdc_command)
1717 {
1719  struct esirisc_common *esirisc = target_to_esirisc(target);
1720 
1721  if (CMD_ARGC > 0) {
1722  if (strcmp(CMD_ARGV[0], "all") == 0)
1723  esirisc->hwdc_save = HWDC_R | HWDC_I | HWDC_S | HWDC_E | HWDC_D;
1724  else {
1725  esirisc->hwdc_save = 0;
1726  if (strcmp(CMD_ARGV[0], "none") != 0) {
1727  while (CMD_ARGC-- > 0) {
1729  if (mask < 0) {
1730  LOG_ERROR("invalid mask: %s", CMD_ARGV[CMD_ARGC]);
1732  }
1733  esirisc->hwdc_save |= mask;
1734  }
1735  }
1736  }
1737  }
1738 
1739  for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
1740  command_print(CMD, "%9s: %s", esirisc_hwdc_masks[i].name,
1741  (esirisc->hwdc_save & esirisc_hwdc_masks[i].mask) ? "enabled" : "disabled");
1742 
1743  return ERROR_OK;
1744 }
1745 
1746 static const struct command_registration esirisc_exec_command_handlers[] = {
1747  {
1748  .name = "flush_caches",
1749  .handler = handle_esirisc_flush_caches_command,
1750  .mode = COMMAND_EXEC,
1751  .help = "flush instruction and data caches",
1752  .usage = "",
1753  },
1755 };
1756 
1757 static const struct command_registration esirisc_any_command_handlers[] = {
1758  {
1759  .name = "cache_arch",
1760  .handler = handle_esirisc_cache_arch_command,
1761  .mode = COMMAND_ANY,
1762  .help = "configure cache architecture",
1763  .usage = "['harvard'|'von_neumann']",
1764  },
1765  {
1766  .name = "hwdc",
1767  .handler = handle_esirisc_hwdc_command,
1768  .mode = COMMAND_ANY,
1769  .help = "configure hardware debug control",
1770  .usage = "['all'|'none'|mask ...]",
1771  },
1772  {
1774  },
1775  {
1777  },
1779 };
1780 
1781 static const struct command_registration esirisc_command_handlers[] = {
1782  {
1783  .name = "esirisc",
1784  .mode = COMMAND_ANY,
1785  .help = "eSi-RISC command group",
1786  .usage = "",
1788  },
1790 };
1791 
1792 struct target_type esirisc_target = {
1793  .name = "esirisc",
1794 
1795  .poll = esirisc_poll,
1796  .arch_state = esirisc_arch_state,
1797 
1798  .halt = esirisc_halt,
1799  .resume = esirisc_resume,
1800  .step = esirisc_step,
1801 
1802  .assert_reset = esirisc_assert_reset,
1803  .deassert_reset = esirisc_deassert_reset,
1804 
1805  .get_gdb_arch = esirisc_get_gdb_arch,
1806  .get_gdb_reg_list = esirisc_get_gdb_reg_list,
1807 
1808  .read_memory = esirisc_read_memory,
1809  .write_memory = esirisc_write_memory,
1810  .checksum_memory = esirisc_checksum_memory,
1811 
1812  .add_breakpoint = esirisc_add_breakpoint,
1813  .remove_breakpoint = esirisc_remove_breakpoint,
1814  .add_watchpoint = esirisc_add_watchpoint,
1815  .remove_watchpoint = esirisc_remove_watchpoint,
1816 
1817  .commands = esirisc_command_handlers,
1818 
1819  .target_create = esirisc_target_create,
1820  .init_target = esirisc_init_target,
1821  .examine = esirisc_examine,
1822 };
void * buf_cpy(const void *from, void *_to, unsigned size)
Copies size bits out of from and into to.
Definition: binarybuffer.c:43
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:98
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:30
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:382
@ BKPT_HARD
Definition: breakpoints.h:18
static void watchpoint_set(struct watchpoint *watchpoint, unsigned int number)
Definition: breakpoints.h:79
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:63
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:473
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:140
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:145
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static int esirisc_identify(struct target *target)
Definition: esirisc.c:1485
static int esirisc_set_reg(struct reg *reg, uint8_t *buf)
Definition: esirisc.c:1397
static int esirisc_disable_step(struct target *target)
Definition: esirisc.c:796
static int esirisc_examine(struct target *target)
Definition: esirisc.c:1583
COMMAND_HANDLER(handle_esirisc_cache_arch_command)
Definition: esirisc.c:1656
static int esirisc_poll(struct target *target)
Definition: esirisc.c:1078
static const char *const esirisc_exception_strings[]
Definition: esirisc.c:33
static const struct @91 esirisc_hwdc_masks[]
static int esirisc_enable_step(struct target *target)
Definition: esirisc.c:822
static int esirisc_wait_debug_active(struct esirisc_common *esirisc, int ms)
Definition: esirisc.c:333
struct target_type esirisc_target
Definition: esirisc.c:1792
static const struct reg_arch_type esirisc_reg_type
Definition: esirisc.c:1416
#define STEP_TIMEOUT
Definition: esirisc.c:27
static int esirisc_disable_interrupts(struct target *target)
Definition: esirisc.c:156
static int esirisc_restore_hwdc(struct target *target)
Definition: esirisc.c:263
static int esirisc_find_hwdc_mask(const char *name)
Definition: esirisc.c:1707
static const struct command_registration esirisc_any_command_handlers[]
Definition: esirisc.c:1757
static int esirisc_read_csr(struct reg *reg)
Definition: esirisc.c:1338
#define RESET_TIMEOUT
Definition: esirisc.c:26
static int esirisc_next_watchpoint(struct target *target)
Definition: esirisc.c:591
static int esirisc_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: esirisc.c:541
static int esirisc_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: esirisc.c:1573
enum esirisc_reg_num number
Definition: esirisc.c:87
static int esirisc_next_breakpoint(struct target *target)
Definition: esirisc.c:455
static int esirisc_write_reg(struct reg *reg)
Definition: esirisc.c:1316
static const char * esirisc_get_gdb_arch(struct target *target)
Definition: esirisc.c:1247
static int esirisc_flush_caches(struct target *target)
Definition: esirisc.c:314
static const struct @89 esirisc_regs[]
static int esirisc_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: esirisc.c:606
const char * group
Definition: esirisc.c:90
static int esirisc_read_reg(struct reg *reg)
Definition: esirisc.c:1293
static int esirisc_save_interrupts(struct target *target)
Definition: esirisc.c:210
static int esirisc_add_watchpoints(struct target *target)
Definition: esirisc.c:709
static int esirisc_deassert_reset(struct target *target)
Definition: esirisc.c:1179
static int esirisc_get_reg(struct reg *reg)
Definition: esirisc.c:1383
uint8_t csr
Definition: esirisc.c:136
static int esirisc_debug_reset(struct target *target)
Definition: esirisc.c:964
static int esirisc_target_create(struct target *target, Jim_Interp *interp)
Definition: esirisc.c:1548
const char * name
Definition: esirisc.c:88
static int esirisc_step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
Definition: esirisc.c:926
static int esirisc_reset_entry(struct target *target)
Definition: esirisc.c:1145
static int esirisc_write_csr(struct reg *reg)
Definition: esirisc.c:1361
static int esirisc_save_context(struct target *target)
Definition: esirisc.c:280
static int esirisc_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Definition: esirisc.c:1266
static int esirisc_halt(struct target *target)
Definition: esirisc.c:775
static int esirisc_add_breakpoints(struct target *target)
Definition: esirisc.c:525
static int esirisc_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: esirisc.c:470
static int esirisc_arch_state(struct target *target)
Definition: esirisc.c:1232
enum reg_type type
Definition: esirisc.c:89
uint8_t bank
Definition: esirisc.c:135
static int esirisc_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: esirisc.c:449
static int esirisc_assert_reset(struct target *target)
Definition: esirisc.c:1115
static int esirisc_resume(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution)
Definition: esirisc.c:917
int mask
Definition: esirisc.c:1698
static int esirisc_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: esirisc.c:725
static const struct @90 esirisc_csrs[]
static const struct command_registration esirisc_exec_command_handlers[]
Definition: esirisc.c:1746
static int esirisc_restore_interrupts(struct target *target)
Definition: esirisc.c:227
static const struct command_registration esirisc_command_handlers[]
Definition: esirisc.c:1781
static int esirisc_resume_or_step(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution, bool step)
Definition: esirisc.c:848
static int esirisc_remove_watchpoints(struct target *target)
Definition: esirisc.c:756
static int esirisc_debug_enable(struct target *target)
Definition: esirisc.c:993
static int esirisc_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: esirisc.c:353
static int esirisc_debug_step(struct target *target)
Definition: esirisc.c:935
static int esirisc_restore_context(struct target *target)
Definition: esirisc.c:297
static int esirisc_debug_entry(struct target *target)
Definition: esirisc.c:1028
static int esirisc_remove_breakpoints(struct target *target)
Definition: esirisc.c:572
static struct reg_cache * esirisc_build_reg_cache(struct target *target)
Definition: esirisc.c:1421
static int esirisc_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: esirisc.c:402
#define EID_INST_BUS_ERROR
Definition: esirisc.h:32
#define EID_NMI
Definition: esirisc.h:27
#define HWDC_D
Definition: esirisc.h:57
#define EID_ARITHMETIC_ERROR
Definition: esirisc.h:35
#define EID_UNSUPPORTED
Definition: esirisc.h:30
#define EID_UNRECOVERABLE
Definition: esirisc.h:38
#define ENTRY_RESET
Definition: esirisc.h:42
#define HWDC_S
Definition: esirisc.h:55
#define HWDC_E
Definition: esirisc.h:56
#define EID_DATA_BREAKPOINT
Definition: esirisc.h:29
#define EID_INST_BREAKPOINT
Definition: esirisc.h:28
#define HWDC_R
Definition: esirisc.h:53
static struct esirisc_common * target_to_esirisc(struct target *target)
Definition: esirisc.h:109
#define EID_HARDWARE_FAILURE
Definition: esirisc.h:26
static char * esirisc_cache_arch_name(struct esirisc_common *esirisc)
Definition: esirisc.h:114
#define EID_DATA_BUS_ERROR
Definition: esirisc.h:33
#define EID_PRIVILEGE_VIOLATION
Definition: esirisc.h:31
#define EID_ALIGNMENT_ERROR
Definition: esirisc.h:34
@ ESIRISC_CACHE_HARVARD
Definition: esirisc.h:61
@ ESIRISC_CACHE_VON_NEUMANN
Definition: esirisc.h:60
#define EID_INTERRUPT_N
Definition: esirisc.h:39
#define HWDC_I
Definition: esirisc.h:54
#define EID_SYSTEM_CALL
Definition: esirisc.h:36
#define EID_MEMORY_MANAGEMENT
Definition: esirisc.h:37
#define EID_RESET
Definition: esirisc.h:25
static bool esirisc_has_cache(struct esirisc_common *esirisc)
Definition: esirisc.h:119
int esirisc_jtag_read_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t *data)
Definition: esirisc_jtag.c:264
int esirisc_jtag_read_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t *data)
Definition: esirisc_jtag.c:428
int esirisc_jtag_deassert_reset(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:502
int esirisc_jtag_read_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t *data)
Definition: esirisc_jtag.c:382
int esirisc_jtag_read_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t *data)
Definition: esirisc_jtag.c:236
int esirisc_jtag_write_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t data)
Definition: esirisc_jtag.c:320
int esirisc_jtag_continue(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:512
int esirisc_jtag_flush_caches(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:517
bool esirisc_jtag_is_debug_active(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:213
int esirisc_jtag_write_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t data)
Definition: esirisc_jtag.c:456
int esirisc_jtag_write_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t data)
Definition: esirisc_jtag.c:408
int esirisc_jtag_read_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t *data)
Definition: esirisc_jtag.c:292
bool esirisc_jtag_is_stopped(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:218
int esirisc_jtag_assert_reset(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:497
int esirisc_jtag_write_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t data)
Definition: esirisc_jtag.c:361
int esirisc_jtag_write_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t data)
Definition: esirisc_jtag.c:340
int esirisc_jtag_enable_debug(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:487
int esirisc_jtag_break(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:507
#define INSTR_LENGTH
Definition: esirisc_jtag.h:19
#define CSR_DEBUG_DBC
Definition: esirisc_regs.h:136
#define CSR_CONFIG_IC
Definition: esirisc_regs.h:149
#define CSR_DEBUG_IBC
Definition: esirisc_regs.h:135
#define CSR_DEBUG_DBS
Definition: esirisc_regs.h:138
#define CSR_CONFIG_TRACE
Definition: esirisc_regs.h:158
#define CSR_DEBUG_IBA_N
Definition: esirisc_regs.h:140
#define CSR_INTERRUPT_IS
Definition: esirisc_regs.h:130
#define CSR_DEBUG
Definition: esirisc_regs.h:108
#define CSR_INTERRUPT_IM
Definition: esirisc_regs.h:129
#define CSR_CONFIG_ARCH0
Definition: esirisc_regs.h:144
#define CSR_CONFIG_DC
Definition: esirisc_regs.h:150
#define CSR_DEBUG_DC
Definition: esirisc_regs.h:134
esirisc_reg_num
Definition: esirisc_regs.h:12
@ ESIRISC_R4
Definition: esirisc_regs.h:17
@ ESIRISC_R13
Definition: esirisc_regs.h:26
@ ESIRISC_IS
Definition: esirisc_regs.h:99
@ ESIRISC_R7
Definition: esirisc_regs.h:20
@ ESIRISC_SP
Definition: esirisc_regs.h:13
@ ESIRISC_R20
Definition: esirisc_regs.h:33
@ ESIRISC_R27
Definition: esirisc_regs.h:40
@ ESIRISC_R30
Definition: esirisc_regs.h:43
@ ESIRISC_R21
Definition: esirisc_regs.h:34
@ ESIRISC_IT
Definition: esirisc_regs.h:100
@ ESIRISC_R11
Definition: esirisc_regs.h:24
@ ESIRISC_R14
Definition: esirisc_regs.h:27
@ ESIRISC_CAS
Definition: esirisc_regs.h:89
@ ESIRISC_IM
Definition: esirisc_regs.h:98
@ ESIRISC_PC
Definition: esirisc_regs.h:88
@ ESIRISC_R28
Definition: esirisc_regs.h:41
@ ESIRISC_ECAS
Definition: esirisc_regs.h:94
@ ESIRISC_R10
Definition: esirisc_regs.h:23
@ ESIRISC_R9
Definition: esirisc_regs.h:22
@ ESIRISC_NUM_REGS
Definition: esirisc_regs.h:102
@ ESIRISC_R31
Definition: esirisc_regs.h:44
@ ESIRISC_R25
Definition: esirisc_regs.h:38
@ ESIRISC_R29
Definition: esirisc_regs.h:42
@ ESIRISC_R23
Definition: esirisc_regs.h:36
@ ESIRISC_IP
Definition: esirisc_regs.h:97
@ ESIRISC_R24
Definition: esirisc_regs.h:37
@ ESIRISC_ETC
Definition: esirisc_regs.h:92
@ ESIRISC_RA
Definition: esirisc_regs.h:14
@ ESIRISC_R22
Definition: esirisc_regs.h:35
@ ESIRISC_TC
Definition: esirisc_regs.h:90
@ ESIRISC_R12
Definition: esirisc_regs.h:25
@ ESIRISC_R19
Definition: esirisc_regs.h:32
@ ESIRISC_R15
Definition: esirisc_regs.h:28
@ ESIRISC_R26
Definition: esirisc_regs.h:39
@ ESIRISC_EPC
Definition: esirisc_regs.h:93
@ ESIRISC_R17
Definition: esirisc_regs.h:30
@ ESIRISC_EID
Definition: esirisc_regs.h:95
@ ESIRISC_R3
Definition: esirisc_regs.h:16
@ ESIRISC_R8
Definition: esirisc_regs.h:21
@ ESIRISC_R2
Definition: esirisc_regs.h:15
@ ESIRISC_ED
Definition: esirisc_regs.h:96
@ ESIRISC_ETA
Definition: esirisc_regs.h:91
@ ESIRISC_R18
Definition: esirisc_regs.h:31
@ ESIRISC_R16
Definition: esirisc_regs.h:29
@ ESIRISC_R6
Definition: esirisc_regs.h:19
@ ESIRISC_R5
Definition: esirisc_regs.h:18
#define CSR_THREAD_ETA
Definition: esirisc_regs.h:119
#define CSR_INTERRUPT_IP
Definition: esirisc_regs.h:127
#define CSR_THREAD_ETC
Definition: esirisc_regs.h:120
#define CSR_THREAD
Definition: esirisc_regs.h:106
#define CSR_THREAD_ECAS
Definition: esirisc_regs.h:122
#define CSR_DEBUG_DBA_N
Definition: esirisc_regs.h:141
#define CSR_CONFIG_MEM
Definition: esirisc_regs.h:148
#define CSR_CONFIG_DBG
Definition: esirisc_regs.h:153
#define CSR_DEBUG_HWDC
Definition: esirisc_regs.h:137
#define CSR_INTERRUPT
Definition: esirisc_regs.h:107
#define CSR_THREAD_EPC
Definition: esirisc_regs.h:121
#define CSR_CONFIG
Definition: esirisc_regs.h:109
#define CSR_INTERRUPT_IT
Definition: esirisc_regs.h:131
#define CSR_THREAD_ED
Definition: esirisc_regs.h:124
#define CSR_THREAD_EID
Definition: esirisc_regs.h:123
const struct command_registration esirisc_trace_command_handlers[]
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
A reset of the TAP state machine can be requested.
Definition: jtag/core.c:758
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1734
@ RESET_HAS_SRST
Definition: jtag.h:215
@ RESET_SRST_PULLS_TRST
Definition: jtag.h:217
void alive_sleep(uint64_t ms)
Definition: log.c:460
char * alloc_printf(const char *format,...)
Definition: log.c:366
#define LOG_USER(expr ...)
Definition: log.h:126
#define ERROR_FAIL
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_INFO(expr ...)
Definition: log.h:117
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
struct reg_cache ** register_get_last_cache_p(struct reg_cache **first)
Definition: register.c:72
void register_cache_invalidate(struct reg_cache *cache)
Marks the contents of the register cache as invalid (and clean).
Definition: register.c:94
reg_type
Definition: register.h:19
@ REG_TYPE_INT
Definition: register.h:21
@ REG_TYPE_CODE_PTR
Definition: register.h:33
@ REG_TYPE_DATA_PTR
Definition: register.h:34
static int step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
Definition: riscv-011.c:1442
struct target * target
Definition: rtt/rtt.c:26
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
struct breakpoint * next
Definition: breakpoints.h:34
enum breakpoint_type type
Definition: breakpoints.h:30
bool is_set
Definition: breakpoints.h:31
unsigned int number
Definition: breakpoints.h:32
target_addr_t address
Definition: breakpoints.h:27
const char * name
Definition: command.h:229
const struct command_registration * chain
If non-NULL, the commands in chain will be registered in the same context and scope of this registrat...
Definition: command.h:243
struct watchpoint * watchpoints_p[MAX_WATCHPOINTS]
Definition: esirisc.h:88
struct target * target
Definition: esirisc.h:65
struct reg * eid
Definition: esirisc.h:73
struct breakpoint * breakpoints_p[MAX_BREAKPOINTS]
Definition: esirisc.h:85
int num_regs
Definition: esirisc.h:79
char * gdb_arch
Definition: esirisc.h:68
int num_breakpoints
Definition: esirisc.h:84
struct reg_cache * reg_cache
Definition: esirisc.h:70
struct esirisc_jtag jtag_info
Definition: esirisc.h:66
bool has_trace
Definition: esirisc.h:82
uint32_t etc_save
Definition: esirisc.h:75
enum esirisc_cache cache_arch
Definition: esirisc.h:67
uint32_t hwdc_save
Definition: esirisc.h:76
int num_watchpoints
Definition: esirisc.h:87
struct reg * ecas
Definition: esirisc.h:72
int num_bits
Definition: esirisc.h:78
struct reg * epc
Definition: esirisc.h:71
bool has_icache
Definition: esirisc.h:80
struct reg * ed
Definition: esirisc.h:74
bool has_dcache
Definition: esirisc.h:81
struct jtag_tap * tap
Definition: esirisc_jtag.h:51
uint8_t csr
Definition: esirisc.h:103
int(* write)(struct reg *reg)
Definition: esirisc.h:106
int(* read)(struct reg *reg)
Definition: esirisc.h:105
struct esirisc_common * esirisc
Definition: esirisc.h:100
uint8_t bank
Definition: esirisc.h:102
Definition: jtag.h:100
int ir_length
size of instruction register
Definition: jtag.h:109
int(* get)(struct reg *reg)
Definition: register.h:152
const char * name
Definition: register.h:145
unsigned num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
struct reg_cache * next
Definition: register.h:146
enum reg_type type
Definition: register.h:100
Definition: register.h:111
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
const char * group
Definition: register.h:138
uint8_t * value
Definition: register.h:122
struct reg_data_type * reg_data_type
Definition: register.h:135
uint32_t number
Definition: register.h:115
void * arch_info
Definition: register.h:140
bool dirty
Definition: register.h:124
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
const char * name
Name of this type of target.
Definition: target_type.h:31
Definition: target.h:120
struct jtag_tap * tap
Definition: target.h:124
enum target_debug_reason debug_reason
Definition: target.h:159
enum target_state state
Definition: target.h:162
enum target_endianness endianness
Definition: target.h:160
struct reg_cache * reg_cache
Definition: target.h:163
struct breakpoint * breakpoints
Definition: target.h:164
struct watchpoint * watchpoints
Definition: target.h:165
void * arch_info
Definition: target.h:169
bool reset_halt
Definition: target.h:149
enum watchpoint_rw rw
Definition: breakpoints.h:44
bool is_set
Definition: breakpoints.h:45
struct watchpoint * next
Definition: breakpoints.h:47
unsigned int number
Definition: breakpoints.h:46
uint32_t length
Definition: breakpoints.h:41
target_addr_t address
Definition: breakpoints.h:40
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1833
void target_free_all_working_areas(struct target *target)
Definition: target.c:2219
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:536
const char * debug_reason_name(struct target *t)
Definition: target.c:289
@ DBG_REASON_NOTHALTED
Definition: target.h:78
@ DBG_REASON_DBGRQ
Definition: target.h:73
@ DBG_REASON_SINGLESTEP
Definition: target.h:77
@ DBG_REASON_WATCHPOINT
Definition: target.h:75
@ DBG_REASON_BREAKPOINT
Definition: target.h:74
target_register_class
Definition: target.h:114
@ REG_CLASS_ALL
Definition: target.h:115
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:792
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:273
@ TARGET_EVENT_HALTED
Definition: target.h:253
@ TARGET_EVENT_RESUMED
Definition: target.h:254
@ TARGET_RESET
Definition: target.h:56
@ TARGET_DEBUG_RUNNING
Definition: target.h:57
@ TARGET_UNKNOWN
Definition: target.h:53
@ TARGET_HALTED
Definition: target.h:55
@ TARGET_RUNNING
Definition: target.h:54
target_endianness
Definition: target.h:84
@ TARGET_BIG_ENDIAN
Definition: target.h:86
@ TARGET_LITTLE_ENDIAN
Definition: target.h:86
static const char * target_name(struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:234
#define ERROR_TARGET_TIMEOUT
Definition: target.h:791
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:796
static void target_set_examined(struct target *target)
Sets the examined flag for the given target.
Definition: target.h:445
static bool target_was_examined(struct target *target)
Definition: target.h:438
#define ERROR_TARGET_FAILURE
Definition: target.h:793
int64_t timeval_ms(void)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
uint64_t target_addr_t
Definition: types.h:335
#define TARGET_PRIxADDR
Definition: types.h:340
uint32_t word
Definition: esirisc.h:94
uint8_t byte
Definition: esirisc.h:96
uint16_t hword
Definition: esirisc.h:95
#define NULL
Definition: usb.h:16
uint8_t count[4]
Definition: vdebug.c:22