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 int 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 int 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) {
322  LOG_TARGET_ERROR(target, "not halted");
324  }
325 
326  int retval = esirisc_jtag_flush_caches(jtag_info);
327  if (retval != ERROR_OK) {
328  LOG_ERROR("%s: failed to flush caches", target_name(target));
329  return retval;
330  }
331 
332  return ERROR_OK;
333 }
334 
335 static int esirisc_wait_debug_active(struct esirisc_common *esirisc, int ms)
336 {
337  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
338  int64_t t;
339 
340  LOG_DEBUG("-");
341 
342  t = timeval_ms();
343  for (;;) {
344  int retval = esirisc_jtag_enable_debug(jtag_info);
345  if (retval == ERROR_OK && esirisc_jtag_is_debug_active(jtag_info))
346  return retval;
347 
348  if ((timeval_ms() - t) > ms)
349  return ERROR_TARGET_TIMEOUT;
350 
351  alive_sleep(100);
352  }
353 }
354 
355 static int esirisc_read_memory(struct target *target, target_addr_t address,
356  uint32_t size, uint32_t count, uint8_t *buffer)
357 {
358  struct esirisc_common *esirisc = target_to_esirisc(target);
359  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
360  int retval;
361 
362  LOG_DEBUG("-");
363 
364  int num_bits = 8 * size;
365  for (uint32_t i = 0; i < count; ++i) {
366  union esirisc_memory value;
367  void *value_p;
368 
369  switch (size) {
370  case sizeof(value.word):
371  value_p = &value.word;
372  retval = esirisc_jtag_read_word(jtag_info, address, value_p);
373  break;
374 
375  case sizeof(value.hword):
376  value_p = &value.hword;
377  retval = esirisc_jtag_read_hword(jtag_info, address, value_p);
378  break;
379 
380  case sizeof(value.byte):
381  value_p = &value.byte;
382  retval = esirisc_jtag_read_byte(jtag_info, address, value_p);
383  break;
384 
385  default:
386  LOG_ERROR("%s: unsupported size: %" PRIu32, target_name(target), size);
387  return ERROR_FAIL;
388  }
389 
390  if (retval != ERROR_OK) {
391  LOG_ERROR("%s: failed to read address: 0x%" TARGET_PRIxADDR, target_name(target),
392  address);
393  return retval;
394  }
395 
396  buf_cpy(value_p, buffer, num_bits);
397  address += size;
398  buffer += size;
399  }
400 
401  return ERROR_OK;
402 }
403 
404 static int esirisc_write_memory(struct target *target, target_addr_t address,
405  uint32_t size, uint32_t count, const uint8_t *buffer)
406 {
407  struct esirisc_common *esirisc = target_to_esirisc(target);
408  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
409  int retval;
410 
411  LOG_DEBUG("-");
412 
413  int num_bits = 8 * size;
414  for (uint32_t i = 0; i < count; ++i) {
415  union esirisc_memory value;
416 
417  switch (size) {
418  case sizeof(value.word):
419  value.word = buf_get_u32(buffer, 0, num_bits);
420  retval = esirisc_jtag_write_word(jtag_info, address, value.word);
421  break;
422 
423  case sizeof(value.hword):
424  value.hword = buf_get_u32(buffer, 0, num_bits);
425  retval = esirisc_jtag_write_hword(jtag_info, address, value.hword);
426  break;
427 
428  case sizeof(value.byte):
429  value.byte = buf_get_u32(buffer, 0, num_bits);
430  retval = esirisc_jtag_write_byte(jtag_info, address, value.byte);
431  break;
432 
433  default:
434  LOG_ERROR("%s: unsupported size: %" PRIu32, target_name(target), size);
435  return ERROR_FAIL;
436  }
437 
438  if (retval != ERROR_OK) {
439  LOG_ERROR("%s: failed to write address: 0x%" TARGET_PRIxADDR, target_name(target),
440  address);
441  return retval;
442  }
443 
444  address += size;
445  buffer += size;
446  }
447 
448  return ERROR_OK;
449 }
450 
452  uint32_t count, uint32_t *checksum)
453 {
454  return ERROR_FAIL; /* not supported */
455 }
456 
458 {
459  struct esirisc_common *esirisc = target_to_esirisc(target);
460  struct breakpoint **breakpoints_p = esirisc->breakpoints_p;
461  struct breakpoint **breakpoints_e = breakpoints_p + esirisc->num_breakpoints;
462 
463  LOG_DEBUG("-");
464 
465  for (int bp_index = 0; breakpoints_p < breakpoints_e; ++breakpoints_p, ++bp_index)
466  if (!*breakpoints_p)
467  return bp_index;
468 
469  return -1;
470 }
471 
473 {
474  struct esirisc_common *esirisc = target_to_esirisc(target);
475  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
476  int bp_index;
477  uint32_t ibc;
478  int retval;
479 
480  LOG_DEBUG("-");
481 
482  /*
483  * The default linker scripts provided by the eSi-RISC toolchain do
484  * not specify attributes on memory regions, which results in
485  * incorrect application of software breakpoints by GDB. Targets
486  * must be configured with `gdb breakpoint_override hard` as
487  * software breakpoints are not supported.
488  */
489  if (breakpoint->type != BKPT_HARD)
491 
492  bp_index = esirisc_next_breakpoint(target);
493  if (bp_index < 0) {
494  LOG_ERROR("%s: out of hardware breakpoints", target_name(target));
496  }
497 
498  breakpoint_hw_set(breakpoint, bp_index);
499  esirisc->breakpoints_p[bp_index] = breakpoint;
500 
501  /* specify instruction breakpoint address */
502  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBA_N + bp_index,
504  if (retval != ERROR_OK) {
505  LOG_ERROR("%s: failed to write Debug CSR: IBA", target_name(target));
506  return retval;
507  }
508 
509  /* enable instruction breakpoint */
510  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, &ibc);
511  if (retval != ERROR_OK) {
512  LOG_ERROR("%s: failed to read Debug CSR: IBC", target_name(target));
513  return retval;
514  }
515 
516  ibc |= (1 << bp_index); /* IBC.In */
517 
518  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, ibc);
519  if (retval != ERROR_OK) {
520  LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
521  return retval;
522  }
523 
524  return ERROR_OK;
525 }
526 
528 {
530 
531  LOG_DEBUG("-");
532 
533  while (breakpoint) {
534  if (!breakpoint->is_set)
536 
538  }
539 
540  return ERROR_OK;
541 }
542 
544 {
545  struct esirisc_common *esirisc = target_to_esirisc(target);
546  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
547  unsigned int bp_index = breakpoint->number;
548  uint32_t ibc;
549  int retval;
550 
551  LOG_DEBUG("-");
552 
553  /* disable instruction breakpoint */
554  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, &ibc);
555  if (retval != ERROR_OK) {
556  LOG_ERROR("%s: failed to read Debug CSR: IBC", target_name(target));
557  return retval;
558  }
559 
560  ibc &= ~(1 << bp_index); /* IBC.In */
561 
562  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, ibc);
563  if (retval != ERROR_OK) {
564  LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
565  return retval;
566  }
567 
568  esirisc->breakpoints_p[bp_index] = NULL;
569  breakpoint->is_set = false;
570 
571  return ERROR_OK;
572 }
573 
575 {
576  struct esirisc_common *esirisc = target_to_esirisc(target);
577  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
578 
579  LOG_DEBUG("-");
580 
581  /* clear instruction breakpoints */
582  int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, 0);
583  if (retval != ERROR_OK) {
584  LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
585  return retval;
586  }
587 
588  memset(esirisc->breakpoints_p, 0, sizeof(esirisc->breakpoints_p));
589 
590  return ERROR_OK;
591 }
592 
594 {
595  struct esirisc_common *esirisc = target_to_esirisc(target);
596  struct watchpoint **watchpoints_p = esirisc->watchpoints_p;
597  struct watchpoint **watchpoints_e = watchpoints_p + esirisc->num_watchpoints;
598 
599  LOG_DEBUG("-");
600 
601  for (int wp_index = 0; watchpoints_p < watchpoints_e; ++watchpoints_p, ++wp_index)
602  if (!*watchpoints_p)
603  return wp_index;
604 
605  return -1;
606 }
607 
609 {
610  struct esirisc_common *esirisc = target_to_esirisc(target);
611  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
612  int wp_index;
613  uint32_t dbs, dbc;
614  int retval;
615 
616  LOG_DEBUG("-");
617 
618  wp_index = esirisc_next_watchpoint(target);
619  if (wp_index < 0) {
620  LOG_ERROR("%s: out of hardware watchpoints", target_name(target));
621  return ERROR_FAIL;
622  }
623 
624  watchpoint_set(watchpoint, wp_index);
625  esirisc->watchpoints_p[wp_index] = watchpoint;
626 
627  /* specify data breakpoint address */
628  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBA_N + wp_index,
630  if (retval != ERROR_OK) {
631  LOG_ERROR("%s: failed to write Debug CSR: DBA", target_name(target));
632  return retval;
633  }
634 
635  /* specify data breakpoint size */
636  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBS, &dbs);
637  if (retval != ERROR_OK) {
638  LOG_ERROR("%s: failed to read Debug CSR: DBS", target_name(target));
639  return retval;
640  }
641 
642  uint32_t sn;
643  switch (watchpoint->length) {
644  case sizeof(uint64_t):
645  sn = 0x3;
646  break;
647  case sizeof(uint32_t):
648  sn = 0x2;
649  break;
650 
651  case sizeof(uint16_t):
652  sn = 0x1;
653  break;
654 
655  case sizeof(uint8_t):
656  sn = 0x0;
657  break;
658 
659  default:
660  LOG_ERROR("%s: unsupported length: %" PRIu32, target_name(target),
661  watchpoint->length);
662  return ERROR_FAIL;
663  }
664 
665  dbs |= (sn << (2 * wp_index)); /* DBS.Sn */
666 
667  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBS, dbs);
668  if (retval != ERROR_OK) {
669  LOG_ERROR("%s: failed to write Debug CSR: DBS", target_name(target));
670  return retval;
671  }
672 
673  /* enable data breakpoint */
674  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
675  if (retval != ERROR_OK) {
676  LOG_ERROR("%s: failed to read Debug CSR: DBC", target_name(target));
677  return retval;
678  }
679 
680  uint32_t dn;
681  switch (watchpoint->rw) {
682  case WPT_READ:
683  dn = 0x1;
684  break;
685 
686  case WPT_WRITE:
687  dn = 0x2;
688  break;
689 
690  case WPT_ACCESS:
691  dn = 0x3;
692  break;
693 
694  default:
695  LOG_ERROR("%s: unsupported rw: %" PRId32, target_name(target),
696  watchpoint->rw);
697  return ERROR_FAIL;
698  }
699 
700  dbc |= (dn << (2 * wp_index)); /* DBC.Dn */
701 
702  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
703  if (retval != ERROR_OK) {
704  LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
705  return retval;
706  }
707 
708  return ERROR_OK;
709 }
710 
712 {
714 
715  LOG_DEBUG("-");
716 
717  while (watchpoint) {
718  if (!watchpoint->is_set)
720 
722  }
723 
724  return ERROR_OK;
725 }
726 
728 {
729  struct esirisc_common *esirisc = target_to_esirisc(target);
730  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
731  unsigned int wp_index = watchpoint->number;
732  uint32_t dbc;
733  int retval;
734 
735  LOG_DEBUG("-");
736 
737  /* disable data breakpoint */
738  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
739  if (retval != ERROR_OK) {
740  LOG_ERROR("%s: failed to read Debug CSR: DBC", target_name(target));
741  return retval;
742  }
743 
744  dbc &= ~(0x3 << (2 * wp_index)); /* DBC.Dn */
745 
746  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
747  if (retval != ERROR_OK) {
748  LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
749  return retval;
750  }
751 
752  esirisc->watchpoints_p[wp_index] = NULL;
753  watchpoint->is_set = false;
754 
755  return ERROR_OK;
756 }
757 
759 {
760  struct esirisc_common *esirisc = target_to_esirisc(target);
761  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
762 
763  LOG_DEBUG("-");
764 
765  /* clear data breakpoints */
766  int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, 0);
767  if (retval != ERROR_OK) {
768  LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
769  return retval;
770  }
771 
772  memset(esirisc->watchpoints_p, 0, sizeof(esirisc->watchpoints_p));
773 
774  return ERROR_OK;
775 }
776 
777 static int esirisc_halt(struct target *target)
778 {
779  struct esirisc_common *esirisc = target_to_esirisc(target);
780  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
781 
782  LOG_DEBUG("-");
783 
784  if (target->state == TARGET_HALTED)
785  return ERROR_OK;
786 
787  int retval = esirisc_jtag_break(jtag_info);
788  if (retval != ERROR_OK) {
789  LOG_ERROR("%s: failed to halt target", target_name(target));
790  return retval;
791  }
792 
794 
795  return ERROR_OK;
796 }
797 
798 static int esirisc_disable_step(struct target *target)
799 {
800  struct esirisc_common *esirisc = target_to_esirisc(target);
801  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
802  uint32_t dc;
803  int retval;
804 
805  LOG_DEBUG("-");
806 
807  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
808  if (retval != ERROR_OK) {
809  LOG_ERROR("%s: failed to read Debug CSR: DC", target_name(target));
810  return retval;
811  }
812 
813  dc &= ~(1<<0); /* DC.S */
814 
815  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
816  if (retval != ERROR_OK) {
817  LOG_ERROR("%s: failed to write Debug CSR: DC", target_name(target));
818  return retval;
819  }
820 
821  return ERROR_OK;
822 }
823 
824 static int esirisc_enable_step(struct target *target)
825 {
826  struct esirisc_common *esirisc = target_to_esirisc(target);
827  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
828  uint32_t dc;
829  int retval;
830 
831  LOG_DEBUG("-");
832 
833  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
834  if (retval != ERROR_OK) {
835  LOG_ERROR("%s: failed to read Debug CSR: DC", target_name(target));
836  return retval;
837  }
838 
839  dc |= (1<<0); /* DC.S */
840 
841  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
842  if (retval != ERROR_OK) {
843  LOG_ERROR("%s: failed to write Debug CSR: DC", target_name(target));
844  return retval;
845  }
846 
847  return ERROR_OK;
848 }
849 
850 static int esirisc_resume_or_step(struct target *target, int current, target_addr_t address,
851  int handle_breakpoints, int debug_execution, bool step)
852 {
853  struct esirisc_common *esirisc = target_to_esirisc(target);
854  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
855  struct breakpoint *breakpoint = NULL;
856  int retval;
857 
858  LOG_DEBUG("-");
859 
860  if (target->state != TARGET_HALTED) {
861  LOG_TARGET_ERROR(target, "not halted");
863  }
864 
865  if (!debug_execution) {
869  }
870 
871  if (current)
872  address = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
873  else {
874  buf_set_u32(esirisc->epc->value, 0, esirisc->epc->size, address);
875  esirisc->epc->dirty = true;
876  esirisc->epc->valid = true;
877  }
878 
880 
881  if (esirisc_has_cache(esirisc))
883 
884  if (handle_breakpoints) {
886  if (breakpoint)
888  }
889 
890  if (step) {
894  } else {
898  }
899 
901 
902  retval = esirisc_jtag_continue(jtag_info);
903  if (retval != ERROR_OK) {
904  LOG_ERROR("%s: failed to resume target", target_name(target));
905  return retval;
906  }
907 
909 
910  if (!debug_execution) {
913  } else {
916  }
917 
918  return ERROR_OK;
919 }
920 
921 static int esirisc_resume(struct target *target, int current, target_addr_t address,
922  int handle_breakpoints, int debug_execution)
923 {
924  LOG_DEBUG("-");
925 
926  return esirisc_resume_or_step(target, current, address,
927  handle_breakpoints, debug_execution, false);
928 }
929 
930 static int esirisc_step(struct target *target, int current, target_addr_t address,
931  int handle_breakpoints)
932 {
933  LOG_DEBUG("-");
934 
935  return esirisc_resume_or_step(target, current, address,
936  handle_breakpoints, 0, true);
937 }
938 
939 static int esirisc_debug_step(struct target *target)
940 {
941  struct esirisc_common *esirisc = target_to_esirisc(target);
942  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
943  int retval;
944 
945  LOG_DEBUG("-");
946 
949 
950  retval = esirisc_jtag_continue(jtag_info);
951  if (retval != ERROR_OK) {
952  LOG_ERROR("%s: failed to resume target", target_name(target));
953  return retval;
954  }
955 
956  retval = esirisc_wait_debug_active(esirisc, STEP_TIMEOUT);
957  if (retval != ERROR_OK) {
958  LOG_ERROR("%s: step timed out", target_name(target));
959  return retval;
960  }
961 
964 
965  return ERROR_OK;
966 }
967 
968 static int esirisc_debug_reset(struct target *target)
969 {
970  struct esirisc_common *esirisc = target_to_esirisc(target);
971  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
972  int retval;
973 
974  LOG_DEBUG("-");
975 
976  retval = esirisc_jtag_assert_reset(jtag_info);
977  if (retval != ERROR_OK) {
978  LOG_ERROR("%s: failed to assert reset", target_name(target));
979  return retval;
980  }
981 
982  retval = esirisc_jtag_deassert_reset(jtag_info);
983  if (retval != ERROR_OK) {
984  LOG_ERROR("%s: failed to deassert reset", target_name(target));
985  return retval;
986  }
987 
988  retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
989  if (retval != ERROR_OK) {
990  LOG_ERROR("%s: reset timed out", target_name(target));
991  return retval;
992  }
993 
994  return ERROR_OK;
995 }
996 
997 static int esirisc_debug_enable(struct target *target)
998 {
999  struct esirisc_common *esirisc = target_to_esirisc(target);
1000  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1001  int retval;
1002 
1003  LOG_DEBUG("-");
1004 
1005  retval = esirisc_jtag_enable_debug(jtag_info);
1006  if (retval != ERROR_OK) {
1007  LOG_ERROR("%s: failed to enable debug mode", target_name(target));
1008  return retval;
1009  }
1010 
1011  /*
1012  * The debug clock is inactive until the first command is sent.
1013  * If the target is stopped, we must first issue a reset before
1014  * attempting further communication. This also handles unpowered
1015  * targets, which will respond with all ones and appear active.
1016  */
1017  if (esirisc_jtag_is_stopped(jtag_info)) {
1018  LOG_INFO("%s: debug clock inactive; attempting debug reset", target_name(target));
1019  retval = esirisc_debug_reset(target);
1020  if (retval != ERROR_OK)
1021  return retval;
1022 
1023  if (esirisc_jtag_is_stopped(jtag_info)) {
1024  LOG_ERROR("%s: target unresponsive; giving up", target_name(target));
1025  return ERROR_FAIL;
1026  }
1027  }
1028 
1029  return ERROR_OK;
1030 }
1031 
1032 static int esirisc_debug_entry(struct target *target)
1033 {
1034  struct esirisc_common *esirisc = target_to_esirisc(target);
1035  struct breakpoint *breakpoint;
1036 
1037  LOG_DEBUG("-");
1038 
1040 
1041  if (esirisc_has_cache(esirisc))
1043 
1046 
1047  uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
1048  switch (eid) {
1049  /*
1050  * InstBreakpoint exceptions are also raised when a core is
1051  * halted for debugging. The following is required to
1052  * determine if a breakpoint was encountered.
1053  */
1054  case EID_INST_BREAKPOINT:
1056  buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size));
1059  break;
1060 
1061  /*
1062  * eSi-RISC treats watchpoints similarly to breakpoints,
1063  * however GDB will not request to step over the current
1064  * instruction when a watchpoint fires. The following is
1065  * required to resume the target.
1066  */
1067  case EID_DATA_BREAKPOINT:
1072  break;
1073 
1074  default:
1076  }
1077  }
1078 
1079  return ERROR_OK;
1080 }
1081 
1082 static int esirisc_poll(struct target *target)
1083 {
1084  struct esirisc_common *esirisc = target_to_esirisc(target);
1085  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1086  int retval;
1087 
1088  retval = esirisc_jtag_enable_debug(jtag_info);
1089  if (retval != ERROR_OK) {
1090  LOG_ERROR("%s: failed to poll target", target_name(target));
1091  return retval;
1092  }
1093 
1094  if (esirisc_jtag_is_stopped(jtag_info)) {
1095  LOG_ERROR("%s: target has stopped; reset required", target_name(target));
1097  return ERROR_TARGET_FAILURE;
1098  }
1099 
1100  if (esirisc_jtag_is_debug_active(jtag_info)) {
1103 
1104  retval = esirisc_debug_entry(target);
1105  if (retval != ERROR_OK)
1106  return retval;
1107 
1109  }
1110 
1111  } else if (target->state == TARGET_HALTED || target->state == TARGET_RESET) {
1114  }
1115 
1116  return ERROR_OK;
1117 }
1118 
1120 {
1121  struct esirisc_common *esirisc = target_to_esirisc(target);
1122  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1123  int retval;
1124 
1125  LOG_DEBUG("-");
1126 
1128  jtag_add_reset(1, 1);
1130  jtag_add_reset(0, 1);
1131  } else {
1134 
1135  retval = esirisc_jtag_assert_reset(jtag_info);
1136  if (retval != ERROR_OK) {
1137  LOG_ERROR("%s: failed to assert reset", target_name(target));
1138  return retval;
1139  }
1140  }
1141 
1143 
1145 
1146  return ERROR_OK;
1147 }
1148 
1149 static int esirisc_reset_entry(struct target *target)
1150 {
1151  struct esirisc_common *esirisc = target_to_esirisc(target);
1152  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1153  uint32_t eta, epc;
1154  int retval;
1155 
1156  LOG_DEBUG("-");
1157 
1158  /* read exception table address */
1159  retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETA, &eta);
1160  if (retval != ERROR_OK) {
1161  LOG_ERROR("%s: failed to read Thread CSR: ETA", target_name(target));
1162  return retval;
1163  }
1164 
1165  /* read reset entry point */
1166  retval = esirisc_jtag_read_word(jtag_info, eta + ENTRY_RESET, &epc);
1167  if (retval != ERROR_OK) {
1168  LOG_ERROR("%s: failed to read address: 0x%" TARGET_PRIxADDR, target_name(target),
1169  (target_addr_t)epc);
1170  return retval;
1171  }
1172 
1173  /* write reset entry point */
1174  retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_EPC, epc);
1175  if (retval != ERROR_OK) {
1176  LOG_ERROR("%s: failed to write Thread CSR: EPC", target_name(target));
1177  return retval;
1178  }
1179 
1180  return ERROR_OK;
1181 }
1182 
1184 {
1185  struct esirisc_common *esirisc = target_to_esirisc(target);
1186  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1187  int retval;
1188 
1189  LOG_DEBUG("-");
1190 
1192  jtag_add_reset(0, 0);
1193 
1194  retval = esirisc_debug_enable(target);
1195  if (retval != ERROR_OK)
1196  return retval;
1197 
1198  retval = esirisc_debug_reset(target);
1199  if (retval != ERROR_OK)
1200  return retval;
1201 
1202  } else {
1203  retval = esirisc_jtag_deassert_reset(jtag_info);
1204  if (retval != ERROR_OK) {
1205  LOG_ERROR("%s: failed to deassert reset", target_name(target));
1206  return retval;
1207  }
1208  }
1209 
1210  retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
1211  if (retval != ERROR_OK) {
1212  LOG_ERROR("%s: reset timed out", target_name(target));
1213  return retval;
1214  }
1215 
1216  retval = esirisc_reset_entry(target);
1217  if (retval != ERROR_OK)
1218  return retval;
1219 
1222 
1224 
1225  if (!target->reset_halt) {
1226  retval = esirisc_jtag_continue(jtag_info);
1227  if (retval != ERROR_OK) {
1228  LOG_ERROR("%s: failed to resume target", target_name(target));
1229  return retval;
1230  }
1231  }
1232 
1233  return ERROR_OK;
1234 }
1235 
1236 static int esirisc_arch_state(struct target *target)
1237 {
1238  struct esirisc_common *esirisc = target_to_esirisc(target);
1239  uint32_t epc = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
1240  uint32_t ecas = buf_get_u32(esirisc->ecas->value, 0, esirisc->ecas->size);
1241  uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
1242  uint32_t ed = buf_get_u32(esirisc->ed->value, 0, esirisc->ed->size);
1243 
1244  LOG_USER("target halted due to %s, exception: %s\n"
1245  "EPC: 0x%" PRIx32 ", ECAS: 0x%" PRIx32 ", EID: 0x%" PRIx32 ", ED: 0x%" PRIx32,
1247 
1248  return ERROR_OK;
1249 }
1250 
1251 static const char *esirisc_get_gdb_arch(const struct target *target)
1252 {
1253  struct esirisc_common *esirisc = target_to_esirisc(target);
1254 
1255  LOG_DEBUG("-");
1256 
1257  /*
1258  * Targets with the UNIFIED_ADDRESS_SPACE option disabled employ a
1259  * Harvard architecture. This option is not exposed in a CSR, which
1260  * requires additional configuration to properly interact with these
1261  * targets in GDB (also see: `esirisc cache_arch`).
1262  */
1263  if (!esirisc->gdb_arch && target_was_examined(target))
1264  esirisc->gdb_arch = alloc_printf("esirisc:%d_bit_%d_reg_%s",
1265  esirisc->num_bits, esirisc->num_regs, esirisc_cache_arch_name(esirisc));
1266 
1267  return esirisc->gdb_arch;
1268 }
1269 
1270 static int esirisc_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
1271  int *reg_list_size, enum target_register_class reg_class)
1272 {
1273  struct esirisc_common *esirisc = target_to_esirisc(target);
1274 
1275  LOG_DEBUG("-");
1276 
1277  *reg_list_size = ESIRISC_NUM_REGS;
1278 
1279  *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
1280  if (!*reg_list)
1281  return ERROR_FAIL;
1282 
1283  if (reg_class == REG_CLASS_ALL)
1284  for (int i = 0; i < *reg_list_size; ++i)
1285  (*reg_list)[i] = esirisc->reg_cache->reg_list + i;
1286  else {
1287  for (int i = 0; i < esirisc->num_regs; ++i)
1288  (*reg_list)[i] = esirisc->reg_cache->reg_list + i;
1289 
1290  (*reg_list)[ESIRISC_PC] = esirisc->reg_cache->reg_list + ESIRISC_PC;
1291  (*reg_list)[ESIRISC_CAS] = esirisc->reg_cache->reg_list + ESIRISC_CAS;
1292  }
1293 
1294  return ERROR_OK;
1295 }
1296 
1297 static int esirisc_read_reg(struct reg *reg)
1298 {
1299  struct esirisc_reg *reg_info = reg->arch_info;
1300  struct esirisc_common *esirisc = reg_info->esirisc;
1301  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1302  struct target *target = esirisc->target;
1303  uint32_t data;
1304 
1305  LOG_DEBUG("-");
1306 
1307  int retval = esirisc_jtag_read_reg(jtag_info, reg->number, &data);
1308  if (retval != ERROR_OK) {
1309  LOG_ERROR("%s: failed to read register: %s", target_name(target), reg->name);
1310  return retval;
1311  }
1312 
1313  buf_set_u32(reg->value, 0, reg->size, data);
1314  reg->dirty = false;
1315  reg->valid = true;
1316 
1317  return ERROR_OK;
1318 }
1319 
1320 static int esirisc_write_reg(struct reg *reg)
1321 {
1322  struct esirisc_reg *reg_info = reg->arch_info;
1323  struct esirisc_common *esirisc = reg_info->esirisc;
1324  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1325  struct target *target = esirisc->target;
1326  uint32_t data = buf_get_u32(reg->value, 0, reg->size);
1327 
1328  LOG_DEBUG("-");
1329 
1330  int retval = esirisc_jtag_write_reg(jtag_info, reg->number, data);
1331  if (retval != ERROR_OK) {
1332  LOG_ERROR("%s: failed to write register: %s", target_name(target), reg->name);
1333  return retval;
1334  }
1335 
1336  reg->dirty = false;
1337  reg->valid = true;
1338 
1339  return ERROR_OK;
1340 }
1341 
1342 static int esirisc_read_csr(struct reg *reg)
1343 {
1344  struct esirisc_reg *reg_info = reg->arch_info;
1345  struct esirisc_common *esirisc = reg_info->esirisc;
1346  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1347  struct target *target = esirisc->target;
1348  uint32_t data;
1349 
1350  LOG_DEBUG("-");
1351 
1352  int retval = esirisc_jtag_read_csr(jtag_info, reg_info->bank, reg_info->csr, &data);
1353  if (retval != ERROR_OK) {
1354  LOG_ERROR("%s: failed to read CSR: %s", target_name(target), reg->name);
1355  return retval;
1356  }
1357 
1358  buf_set_u32(reg->value, 0, reg->size, data);
1359  reg->dirty = false;
1360  reg->valid = true;
1361 
1362  return ERROR_OK;
1363 }
1364 
1365 static int esirisc_write_csr(struct reg *reg)
1366 {
1367  struct esirisc_reg *reg_info = reg->arch_info;
1368  struct esirisc_common *esirisc = reg_info->esirisc;
1369  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1370  struct target *target = esirisc->target;
1371  uint32_t data = buf_get_u32(reg->value, 0, reg->size);
1372 
1373  LOG_DEBUG("-");
1374 
1375  int retval = esirisc_jtag_write_csr(jtag_info, reg_info->bank, reg_info->csr, data);
1376  if (retval != ERROR_OK) {
1377  LOG_ERROR("%s: failed to write CSR: %s", target_name(target), reg->name);
1378  return retval;
1379  }
1380 
1381  reg->dirty = false;
1382  reg->valid = true;
1383 
1384  return ERROR_OK;
1385 }
1386 
1387 static int esirisc_get_reg(struct reg *reg)
1388 {
1389  struct esirisc_reg *reg_info = reg->arch_info;
1390  struct esirisc_common *esirisc = reg_info->esirisc;
1391  struct target *target = esirisc->target;
1392 
1393  LOG_DEBUG("-");
1394 
1395  if (target->state != TARGET_HALTED)
1396  return ERROR_TARGET_NOT_HALTED;
1397 
1398  return reg_info->read(reg);
1399 }
1400 
1401 static int esirisc_set_reg(struct reg *reg, uint8_t *buf)
1402 {
1403  struct esirisc_reg *reg_info = reg->arch_info;
1404  struct esirisc_common *esirisc = reg_info->esirisc;
1405  struct target *target = esirisc->target;
1406  uint32_t value = buf_get_u32(buf, 0, reg->size);
1407 
1408  LOG_DEBUG("-");
1409 
1410  if (target->state != TARGET_HALTED)
1411  return ERROR_TARGET_NOT_HALTED;
1412 
1413  buf_set_u32(reg->value, 0, reg->size, value);
1414  reg->dirty = true;
1415  reg->valid = true;
1416 
1417  return ERROR_OK;
1418 }
1419 
1420 static const struct reg_arch_type esirisc_reg_type = {
1421  .get = esirisc_get_reg,
1422  .set = esirisc_set_reg,
1423 };
1424 
1426 {
1427  struct esirisc_common *esirisc = target_to_esirisc(target);
1428  struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
1429  struct reg_cache *cache = malloc(sizeof(struct reg_cache));
1430  struct reg *reg_list = calloc(ESIRISC_NUM_REGS, sizeof(struct reg));
1431 
1432  LOG_DEBUG("-");
1433 
1434  cache->name = "eSi-RISC registers";
1435  cache->next = NULL;
1436  cache->reg_list = reg_list;
1437  cache->num_regs = ESIRISC_NUM_REGS;
1438  (*cache_p) = cache;
1439 
1440  esirisc->reg_cache = cache;
1441  esirisc->epc = reg_list + ESIRISC_EPC;
1442  esirisc->ecas = reg_list + ESIRISC_ECAS;
1443  esirisc->eid = reg_list + ESIRISC_EID;
1444  esirisc->ed = reg_list + ESIRISC_ED;
1445 
1446  for (int i = 0; i < esirisc->num_regs; ++i) {
1447  struct reg *reg = reg_list + esirisc_regs[i].number;
1448  struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));
1449 
1450  reg->name = esirisc_regs[i].name;
1451  reg->number = esirisc_regs[i].number;
1452  reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
1453  reg->size = esirisc->num_bits;
1454  reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
1455  reg->reg_data_type->type = esirisc_regs[i].type;
1456  reg->group = esirisc_regs[i].group;
1457  reg_info->esirisc = esirisc;
1458  reg_info->read = esirisc_read_reg;
1459  reg_info->write = esirisc_write_reg;
1460  reg->arch_info = reg_info;
1462  reg->exist = true;
1463  }
1464 
1465  for (size_t i = 0; i < ARRAY_SIZE(esirisc_csrs); ++i) {
1466  struct reg *reg = reg_list + esirisc_csrs[i].number;
1467  struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));
1468 
1469  reg->name = esirisc_csrs[i].name;
1470  reg->number = esirisc_csrs[i].number;
1471  reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
1472  reg->size = esirisc->num_bits;
1473  reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
1474  reg->reg_data_type->type = esirisc_csrs[i].type;
1475  reg->group = esirisc_csrs[i].group;
1476  reg_info->esirisc = esirisc;
1477  reg_info->bank = esirisc_csrs[i].bank;
1478  reg_info->csr = esirisc_csrs[i].csr;
1479  reg_info->read = esirisc_read_csr;
1480  reg_info->write = esirisc_write_csr;
1481  reg->arch_info = reg_info;
1483  reg->exist = true;
1484  }
1485 
1486  return cache;
1487 }
1488 
1490 {
1491  struct esirisc_common *esirisc = target_to_esirisc(target);
1492  struct reg_cache *cache = esirisc->reg_cache;
1493  struct reg *reg_list = cache->reg_list;
1494 
1495  for (int i = 0; i < esirisc->num_regs; ++i) {
1496  struct reg *reg = reg_list + esirisc_regs[i].number;
1497 
1498  free(reg->arch_info);
1499  free(reg->value);
1500  free(reg->reg_data_type);
1501  }
1502 
1503  for (size_t i = 0; i < ARRAY_SIZE(esirisc_csrs); ++i) {
1504  struct reg *reg = reg_list + esirisc_csrs[i].number;
1505 
1506  free(reg->arch_info);
1507  free(reg->value);
1508  free(reg->reg_data_type);
1509  }
1510 
1511  free(reg_list);
1512  free(cache);
1513 }
1514 
1515 static int esirisc_identify(struct target *target)
1516 {
1517  struct esirisc_common *esirisc = target_to_esirisc(target);
1518  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1519  uint32_t csr;
1520  int retval;
1521 
1522  LOG_DEBUG("-");
1523 
1524  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_ARCH0, &csr);
1525  if (retval != ERROR_OK) {
1526  LOG_ERROR("%s: failed to read Configuration CSR: ARCH0", target_name(target));
1527  return retval;
1528  }
1529 
1530  esirisc->num_bits = (csr >> 0) & 0x3f; /* ARCH0.B */
1531  esirisc->num_regs = (csr >> 10) & 0x3f; /* ARCH0.R */
1532 
1533  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_MEM, &csr);
1534  if (retval != ERROR_OK) {
1535  LOG_ERROR("%s: failed to read Configuration CSR: MEM", target_name(target));
1536  return retval;
1537  }
1538 
1539  target->endianness = (csr & 1<<0) ? /* MEM.E */
1541 
1542  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_IC, &csr);
1543  if (retval != ERROR_OK) {
1544  LOG_ERROR("%s: failed to read Configuration CSR: IC", target_name(target));
1545  return retval;
1546  }
1547 
1548  esirisc->has_icache = !!(csr & 1<<0); /* IC.E */
1549 
1550  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DC, &csr);
1551  if (retval != ERROR_OK) {
1552  LOG_ERROR("%s: failed to read Configuration CSR: DC", target_name(target));
1553  return retval;
1554  }
1555 
1556  esirisc->has_dcache = !!(csr & 1<<0); /* DC.E */
1557 
1558  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DBG, &csr);
1559  if (retval != ERROR_OK) {
1560  LOG_ERROR("%s: failed to read Configuration CSR: DBG", target_name(target));
1561  return retval;
1562  }
1563 
1564  esirisc->num_breakpoints = (csr >> 7) & 0xf; /* DBG.BP */
1565  esirisc->num_watchpoints = (csr >> 12) & 0xf; /* DBG.WP */
1566 
1567  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_TRACE, &csr);
1568  if (retval != ERROR_OK) {
1569  LOG_ERROR("%s: failed to read Configuration CSR: TRACE", target_name(target));
1570  return retval;
1571  }
1572 
1573  esirisc->has_trace = !!(csr & 1<<0); /* TRACE.T */
1574 
1575  return ERROR_OK;
1576 }
1577 
1578 static int esirisc_target_create(struct target *target, Jim_Interp *interp)
1579 {
1580  struct jtag_tap *tap = target->tap;
1581  struct esirisc_common *esirisc;
1582 
1583  if (!tap)
1584  return ERROR_FAIL;
1585 
1586  if (tap->ir_length != INSTR_LENGTH) {
1587  LOG_ERROR("%s: invalid IR length; expected %d", target_name(target),
1588  INSTR_LENGTH);
1589  return ERROR_FAIL;
1590  }
1591 
1592  esirisc = calloc(1, sizeof(struct esirisc_common));
1593  if (!esirisc)
1594  return ERROR_FAIL;
1595 
1596  esirisc->target = target;
1597  esirisc->jtag_info.tap = tap;
1598  target->arch_info = esirisc;
1599 
1600  return ERROR_OK;
1601 }
1602 
1603 static int esirisc_init_target(struct command_context *cmd_ctx, struct target *target)
1604 {
1605  struct esirisc_common *esirisc = target_to_esirisc(target);
1606 
1607  /* trap reset, error, and debug exceptions */
1608  esirisc->hwdc_save = HWDC_R | HWDC_E | HWDC_D;
1609 
1610  return ERROR_OK;
1611 }
1612 
1614 {
1615  struct esirisc_common *esirisc = target_to_esirisc(target);
1616 
1618  return;
1619 
1621 
1622  free(esirisc->gdb_arch);
1623  free(esirisc);
1624 }
1625 
1626 static int esirisc_examine(struct target *target)
1627 {
1628  struct esirisc_common *esirisc = target_to_esirisc(target);
1629  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1630  int retval;
1631 
1632  LOG_DEBUG("-");
1633 
1634  if (!target_was_examined(target)) {
1635  retval = esirisc_debug_enable(target);
1636  if (retval != ERROR_OK)
1637  return retval;
1638 
1639  /*
1640  * In order to identify the target we must first halt the core.
1641  * We quietly resume once identification has completed for those
1642  * targets that were running when target_examine was called.
1643  */
1644  if (esirisc_jtag_is_debug_active(jtag_info)) {
1645  if (target->state == TARGET_UNKNOWN)
1647 
1649  } else {
1650  retval = esirisc_jtag_break(jtag_info);
1651  if (retval != ERROR_OK) {
1652  LOG_ERROR("%s: failed to halt target", target_name(target));
1653  return retval;
1654  }
1655 
1657  }
1658 
1659  retval = esirisc_identify(target);
1660  if (retval != ERROR_OK) {
1661  LOG_ERROR("%s: failed to identify target", target_name(target));
1662  return retval;
1663  }
1664 
1666 
1669 
1672 
1673  if (target->state == TARGET_HALTED)
1675  else {
1676  retval = esirisc_jtag_continue(jtag_info);
1677  if (retval != ERROR_OK) {
1678  LOG_ERROR("%s: failed to resume target", target_name(target));
1679  return retval;
1680  }
1681  }
1682 
1684 
1685  LOG_INFO("%s: %d bit, %d registers, %s%s%s", target_name(target),
1686  esirisc->num_bits, esirisc->num_regs,
1688  esirisc->has_icache ? ", icache" : "",
1689  esirisc->has_dcache ? ", dcache" : "");
1690 
1691  LOG_INFO("%s: hardware has %d breakpoints, %d watchpoints%s", target_name(target),
1692  esirisc->num_breakpoints, esirisc->num_watchpoints,
1693  esirisc->has_trace ? ", trace" : "");
1694  }
1695 
1696  return ERROR_OK;
1697 }
1698 
1699 COMMAND_HANDLER(handle_esirisc_cache_arch_command)
1700 {
1702  struct esirisc_common *esirisc = target_to_esirisc(target);
1703 
1704  if (CMD_ARGC > 0) {
1705  if (strcmp(*CMD_ARGV, "harvard") == 0)
1706  esirisc->cache_arch = ESIRISC_CACHE_HARVARD;
1707  else if (strcmp(*CMD_ARGV, "von_neumann") == 0)
1709  else {
1710  LOG_ERROR("invalid cache_arch: %s", *CMD_ARGV);
1712  }
1713  }
1714 
1715  command_print(CMD, "esirisc cache_arch %s", esirisc_cache_arch_name(esirisc));
1716 
1717  return ERROR_OK;
1718 }
1719 
1720 COMMAND_HANDLER(handle_esirisc_flush_caches_command)
1721 {
1723  struct esirisc_common *esirisc = target_to_esirisc(target);
1724  int retval;
1725 
1726  if (!esirisc_has_cache(esirisc)) {
1727  LOG_ERROR("target does not support caching");
1728  return ERROR_FAIL;
1729  }
1730 
1731  retval = esirisc_flush_caches(target);
1732 
1733  command_print(CMD, "cache flush %s",
1734  (retval == ERROR_OK) ? "successful" : "failed");
1735 
1736  return retval;
1737 }
1738 
1739 static const struct {
1740  const char *name;
1741  int mask;
1742 } esirisc_hwdc_masks[] = {
1743  { "reset", HWDC_R },
1744  { "interrupt", HWDC_I },
1745  { "syscall", HWDC_S },
1746  { "error", HWDC_E },
1747  { "debug", HWDC_D },
1748 };
1749 
1750 static int esirisc_find_hwdc_mask(const char *name)
1751 {
1752  for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
1753  if (strcmp(esirisc_hwdc_masks[i].name, name) == 0)
1754  return esirisc_hwdc_masks[i].mask;
1755 
1756  return -1;
1757 }
1758 
1759 COMMAND_HANDLER(handle_esirisc_hwdc_command)
1760 {
1762  struct esirisc_common *esirisc = target_to_esirisc(target);
1763 
1764  if (CMD_ARGC > 0) {
1765  if (strcmp(CMD_ARGV[0], "all") == 0)
1766  esirisc->hwdc_save = HWDC_R | HWDC_I | HWDC_S | HWDC_E | HWDC_D;
1767  else {
1768  esirisc->hwdc_save = 0;
1769  if (strcmp(CMD_ARGV[0], "none") != 0) {
1770  while (CMD_ARGC-- > 0) {
1772  if (mask < 0) {
1773  LOG_ERROR("invalid mask: %s", CMD_ARGV[CMD_ARGC]);
1775  }
1776  esirisc->hwdc_save |= mask;
1777  }
1778  }
1779  }
1780  }
1781 
1782  for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
1783  command_print(CMD, "%9s: %s", esirisc_hwdc_masks[i].name,
1784  (esirisc->hwdc_save & esirisc_hwdc_masks[i].mask) ? "enabled" : "disabled");
1785 
1786  return ERROR_OK;
1787 }
1788 
1789 static const struct command_registration esirisc_exec_command_handlers[] = {
1790  {
1791  .name = "flush_caches",
1792  .handler = handle_esirisc_flush_caches_command,
1793  .mode = COMMAND_EXEC,
1794  .help = "flush instruction and data caches",
1795  .usage = "",
1796  },
1798 };
1799 
1800 static const struct command_registration esirisc_any_command_handlers[] = {
1801  {
1802  .name = "cache_arch",
1803  .handler = handle_esirisc_cache_arch_command,
1804  .mode = COMMAND_ANY,
1805  .help = "configure cache architecture",
1806  .usage = "['harvard'|'von_neumann']",
1807  },
1808  {
1809  .name = "hwdc",
1810  .handler = handle_esirisc_hwdc_command,
1811  .mode = COMMAND_ANY,
1812  .help = "configure hardware debug control",
1813  .usage = "['all'|'none'|mask ...]",
1814  },
1815  {
1817  },
1818  {
1820  },
1822 };
1823 
1824 static const struct command_registration esirisc_command_handlers[] = {
1825  {
1826  .name = "esirisc",
1827  .mode = COMMAND_ANY,
1828  .help = "eSi-RISC command group",
1829  .usage = "",
1831  },
1833 };
1834 
1835 struct target_type esirisc_target = {
1836  .name = "esirisc",
1837 
1838  .poll = esirisc_poll,
1839  .arch_state = esirisc_arch_state,
1840 
1841  .halt = esirisc_halt,
1842  .resume = esirisc_resume,
1843  .step = esirisc_step,
1844 
1845  .assert_reset = esirisc_assert_reset,
1846  .deassert_reset = esirisc_deassert_reset,
1847 
1848  .get_gdb_arch = esirisc_get_gdb_arch,
1849  .get_gdb_reg_list = esirisc_get_gdb_reg_list,
1850 
1851  .read_memory = esirisc_read_memory,
1852  .write_memory = esirisc_write_memory,
1853  .checksum_memory = esirisc_checksum_memory,
1854 
1855  .add_breakpoint = esirisc_add_breakpoint,
1856  .remove_breakpoint = esirisc_remove_breakpoint,
1857  .add_watchpoint = esirisc_add_watchpoint,
1858  .remove_watchpoint = esirisc_remove_watchpoint,
1859 
1860  .commands = esirisc_command_handlers,
1861 
1862  .target_create = esirisc_target_create,
1863  .init_target = esirisc_init_target,
1864  .deinit_target = esirisc_deinit_target,
1865  .examine = esirisc_examine,
1866 };
void * buf_cpy(const void *from, void *_to, unsigned int 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 int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:489
@ BKPT_HARD
Definition: breakpoints.h:18
static void watchpoint_set(struct watchpoint *watchpoint, unsigned int number)
Definition: breakpoints.h:83
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:66
@ 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:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static int esirisc_identify(struct target *target)
Definition: esirisc.c:1515
static int esirisc_set_reg(struct reg *reg, uint8_t *buf)
Definition: esirisc.c:1401
static int esirisc_disable_step(struct target *target)
Definition: esirisc.c:798
static int esirisc_examine(struct target *target)
Definition: esirisc.c:1626
COMMAND_HANDLER(handle_esirisc_cache_arch_command)
Definition: esirisc.c:1699
static int esirisc_poll(struct target *target)
Definition: esirisc.c:1082
static const char *const esirisc_exception_strings[]
Definition: esirisc.c:33
static int esirisc_enable_step(struct target *target)
Definition: esirisc.c:824
static int esirisc_wait_debug_active(struct esirisc_common *esirisc, int ms)
Definition: esirisc.c:335
struct target_type esirisc_target
Definition: esirisc.c:1835
static const char * esirisc_get_gdb_arch(const struct target *target)
Definition: esirisc.c:1251
static const struct reg_arch_type esirisc_reg_type
Definition: esirisc.c:1420
#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:1750
static const struct command_registration esirisc_any_command_handlers[]
Definition: esirisc.c:1800
static int esirisc_read_csr(struct reg *reg)
Definition: esirisc.c:1342
static void esirisc_free_reg_cache(struct target *target)
Definition: esirisc.c:1489
#define RESET_TIMEOUT
Definition: esirisc.c:26
static int esirisc_next_watchpoint(struct target *target)
Definition: esirisc.c:593
static int esirisc_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: esirisc.c:543
static int esirisc_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: esirisc.c:1603
static void esirisc_deinit_target(struct target *target)
Definition: esirisc.c:1613
enum esirisc_reg_num number
Definition: esirisc.c:87
static int esirisc_next_breakpoint(struct target *target)
Definition: esirisc.c:457
static int esirisc_write_reg(struct reg *reg)
Definition: esirisc.c:1320
static int esirisc_flush_caches(struct target *target)
Definition: esirisc.c:314
static int esirisc_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: esirisc.c:608
const char * group
Definition: esirisc.c:90
static int esirisc_read_reg(struct reg *reg)
Definition: esirisc.c:1297
static int esirisc_save_interrupts(struct target *target)
Definition: esirisc.c:210
static int esirisc_add_watchpoints(struct target *target)
Definition: esirisc.c:711
static int esirisc_deassert_reset(struct target *target)
Definition: esirisc.c:1183
static int esirisc_get_reg(struct reg *reg)
Definition: esirisc.c:1387
uint8_t csr
Definition: esirisc.c:136
static int esirisc_debug_reset(struct target *target)
Definition: esirisc.c:968
static int esirisc_target_create(struct target *target, Jim_Interp *interp)
Definition: esirisc.c:1578
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:930
static int esirisc_reset_entry(struct target *target)
Definition: esirisc.c:1149
static int esirisc_write_csr(struct reg *reg)
Definition: esirisc.c:1365
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:1270
static int esirisc_halt(struct target *target)
Definition: esirisc.c:777
static const struct @95 esirisc_hwdc_masks[]
static const struct @94 esirisc_csrs[]
static int esirisc_add_breakpoints(struct target *target)
Definition: esirisc.c:527
static int esirisc_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: esirisc.c:472
static int esirisc_arch_state(struct target *target)
Definition: esirisc.c:1236
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:451
static int esirisc_assert_reset(struct target *target)
Definition: esirisc.c:1119
static int esirisc_resume(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution)
Definition: esirisc.c:921
static const struct @93 esirisc_regs[]
int mask
Definition: esirisc.c:1741
static int esirisc_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: esirisc.c:727
static const struct command_registration esirisc_exec_command_handlers[]
Definition: esirisc.c:1789
static int esirisc_restore_interrupts(struct target *target)
Definition: esirisc.c:227
static const struct command_registration esirisc_command_handlers[]
Definition: esirisc.c:1824
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:850
static int esirisc_remove_watchpoints(struct target *target)
Definition: esirisc.c:758
static int esirisc_debug_enable(struct target *target)
Definition: esirisc.c:997
static int esirisc_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: esirisc.c:355
static int esirisc_debug_step(struct target *target)
Definition: esirisc.c:939
static int esirisc_restore_context(struct target *target)
Definition: esirisc.c:297
static int esirisc_debug_entry(struct target *target)
Definition: esirisc.c:1032
static int esirisc_remove_breakpoints(struct target *target)
Definition: esirisc.c:574
static struct reg_cache * esirisc_build_reg_cache(struct target *target)
Definition: esirisc.c:1425
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:404
#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
static struct esirisc_common * target_to_esirisc(const struct target *target)
Definition: esirisc.h:109
#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
#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:265
int esirisc_jtag_read_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t *data)
Definition: esirisc_jtag.c:429
int esirisc_jtag_deassert_reset(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:503
int esirisc_jtag_read_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t *data)
Definition: esirisc_jtag.c:383
int esirisc_jtag_read_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t *data)
Definition: esirisc_jtag.c:237
int esirisc_jtag_write_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t data)
Definition: esirisc_jtag.c:321
int esirisc_jtag_continue(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:513
int esirisc_jtag_flush_caches(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:518
bool esirisc_jtag_is_debug_active(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:214
int esirisc_jtag_write_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t data)
Definition: esirisc_jtag.c:457
int esirisc_jtag_write_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t data)
Definition: esirisc_jtag.c:409
int esirisc_jtag_read_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t *data)
Definition: esirisc_jtag.c:293
bool esirisc_jtag_is_stopped(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:219
int esirisc_jtag_assert_reset(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:498
int esirisc_jtag_write_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t data)
Definition: esirisc_jtag.c:362
int esirisc_jtag_write_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t data)
Definition: esirisc_jtag.c:341
int esirisc_jtag_enable_debug(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:488
int esirisc_jtag_break(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:508
#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:219
@ RESET_SRST_PULLS_TRST
Definition: jtag.h:221
void alive_sleep(uint64_t ms)
Definition: log.c:456
char * alloc_printf(const char *format,...)
Definition: log.c:364
#define LOG_USER(expr ...)
Definition: log.h:135
#define ERROR_FAIL
Definition: log.h:170
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:158
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
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:235
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:249
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:101
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
int(* get)(struct reg *reg)
Definition: register.h:152
const char * name
Definition: register.h:145
unsigned int 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:116
struct jtag_tap * tap
Definition: target.h:119
enum target_debug_reason debug_reason
Definition: target.h:154
enum target_state state
Definition: target.h:157
enum target_endianness endianness
Definition: target.h:155
struct reg_cache * reg_cache
Definition: target.h:158
struct breakpoint * breakpoints
Definition: target.h:159
struct watchpoint * watchpoints
Definition: target.h:160
void * arch_info
Definition: target.h:164
bool reset_halt
Definition: target.h:144
enum watchpoint_rw rw
Definition: breakpoints.h:46
bool is_set
Definition: breakpoints.h:47
struct watchpoint * next
Definition: breakpoints.h:49
unsigned int length
Definition: breakpoints.h:43
unsigned int number
Definition: breakpoints.h:48
target_addr_t address
Definition: breakpoints.h:42
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1764
void target_free_all_working_areas(struct target *target)
Definition: target.c:2150
const char * debug_reason_name(const struct target *t)
Definition: target.c:247
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:458
@ DBG_REASON_NOTHALTED
Definition: target.h:74
@ DBG_REASON_DBGRQ
Definition: target.h:69
@ DBG_REASON_SINGLESTEP
Definition: target.h:73
@ DBG_REASON_WATCHPOINT
Definition: target.h:71
@ DBG_REASON_BREAKPOINT
Definition: target.h:70
target_register_class
Definition: target.h:110
@ REG_CLASS_ALL
Definition: target.h:111
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:790
static bool target_was_examined(const struct target *target)
Definition: target.h:436
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:272
@ TARGET_EVENT_HALTED
Definition: target.h:252
@ TARGET_EVENT_RESUMED
Definition: target.h:253
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:233
@ TARGET_RESET
Definition: target.h:57
@ TARGET_DEBUG_RUNNING
Definition: target.h:58
@ TARGET_UNKNOWN
Definition: target.h:54
@ TARGET_HALTED
Definition: target.h:56
@ TARGET_RUNNING
Definition: target.h:55
target_endianness
Definition: target.h:80
@ TARGET_BIG_ENDIAN
Definition: target.h:82
@ TARGET_LITTLE_ENDIAN
Definition: target.h:82
#define ERROR_TARGET_TIMEOUT
Definition: target.h:789
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:794
static void target_set_examined(struct target *target)
Sets the examined flag for the given target.
Definition: target.h:443
#define ERROR_TARGET_FAILURE
Definition: target.h:791
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