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_TARGET_DEBUG(target, "-");
164 
165  retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, &etc);
166  if (retval != ERROR_OK) {
167  LOG_TARGET_ERROR(target, "failed to read Thread CSR: ETC");
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_TARGET_ERROR(target, "failed to write Thread CSR: ETC");
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_TARGET_DEBUG(target, "-");
191 
192  retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, &etc);
193  if (retval != ERROR_OK) {
194  LOG_TARGET_ERROR(target, "failed to read Thread CSR: ETC");
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_TARGET_ERROR(target, "failed to write Thread CSR: ETC");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "failed to read Thread CSR: ETC");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "failed to write Thread CSR: ETC");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "failed to read Thread CSR: HWDC");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "failed to write Debug CSR: HWDC");
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_TARGET_DEBUG(target, "-");
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_TARGET_DEBUG(target, "-");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "failed to flush caches");
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_TARGET_DEBUG(esirisc->target, "-");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "unsupported size: %" PRIu32, size);
387  return ERROR_FAIL;
388  }
389 
390  if (retval != ERROR_OK) {
391  LOG_TARGET_ERROR(target, "failed to read address: 0x%" TARGET_PRIxADDR,
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "unsupported size: %" PRIu32, size);
435  return ERROR_FAIL;
436  }
437 
438  if (retval != ERROR_OK) {
439  LOG_TARGET_ERROR(target, "failed to write address: 0x%" TARGET_PRIxADDR,
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_TARGET_DEBUG(target, "-");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "out of hardware breakpoints");
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_TARGET_ERROR(target, "failed to write Debug CSR: IBA");
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_TARGET_ERROR(target, "failed to read Debug CSR: IBC");
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_TARGET_ERROR(target, "failed to write Debug CSR: IBC");
521  return retval;
522  }
523 
524  return ERROR_OK;
525 }
526 
528 {
530 
531  LOG_TARGET_DEBUG(target, "-");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "failed to read Debug CSR: IBC");
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_TARGET_ERROR(target, "failed to write Debug CSR: IBC");
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_TARGET_DEBUG(target, "-");
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_TARGET_ERROR(target, "failed to write Debug CSR: IBC");
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_TARGET_DEBUG(target, "-");
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_TARGET_DEBUG(target, "-");
617 
618  wp_index = esirisc_next_watchpoint(target);
619  if (wp_index < 0) {
620  LOG_TARGET_ERROR(target, "out of hardware watchpoints");
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_TARGET_ERROR(target, "failed to write Debug CSR: DBA");
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_TARGET_ERROR(target, "failed to read Debug CSR: DBS");
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_TARGET_ERROR(target, "unsupported length: %" PRIu32,
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_TARGET_ERROR(target, "failed to write Debug CSR: DBS");
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_TARGET_ERROR(target, "failed to read Debug CSR: DBC");
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_TARGET_ERROR(target, "unsupported rw: %" PRId32, watchpoint->rw);
696  return ERROR_FAIL;
697  }
698 
699  dbc |= (dn << (2 * wp_index)); /* DBC.Dn */
700 
701  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
702  if (retval != ERROR_OK) {
703  LOG_TARGET_ERROR(target, "failed to write Debug CSR: DBC");
704  return retval;
705  }
706 
707  return ERROR_OK;
708 }
709 
711 {
713 
714  LOG_TARGET_DEBUG(target, "-");
715 
716  while (watchpoint) {
717  if (!watchpoint->is_set)
719 
721  }
722 
723  return ERROR_OK;
724 }
725 
727 {
728  struct esirisc_common *esirisc = target_to_esirisc(target);
729  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
730  unsigned int wp_index = watchpoint->number;
731  uint32_t dbc;
732  int retval;
733 
734  LOG_TARGET_DEBUG(target, "-");
735 
736  /* disable data breakpoint */
737  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
738  if (retval != ERROR_OK) {
739  LOG_TARGET_ERROR(target, "failed to read Debug CSR: DBC");
740  return retval;
741  }
742 
743  dbc &= ~(0x3 << (2 * wp_index)); /* DBC.Dn */
744 
745  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
746  if (retval != ERROR_OK) {
747  LOG_TARGET_ERROR(target, "failed to write Debug CSR: DBC");
748  return retval;
749  }
750 
751  esirisc->watchpoints_p[wp_index] = NULL;
752  watchpoint->is_set = false;
753 
754  return ERROR_OK;
755 }
756 
758 {
759  struct esirisc_common *esirisc = target_to_esirisc(target);
760  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
761 
762  LOG_TARGET_DEBUG(target, "-");
763 
764  /* clear data breakpoints */
765  int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, 0);
766  if (retval != ERROR_OK) {
767  LOG_TARGET_ERROR(target, "failed to write Debug CSR: DBC");
768  return retval;
769  }
770 
771  memset(esirisc->watchpoints_p, 0, sizeof(esirisc->watchpoints_p));
772 
773  return ERROR_OK;
774 }
775 
776 static int esirisc_halt(struct target *target)
777 {
778  struct esirisc_common *esirisc = target_to_esirisc(target);
779  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
780 
781  LOG_TARGET_DEBUG(target, "-");
782 
783  if (target->state == TARGET_HALTED)
784  return ERROR_OK;
785 
786  int retval = esirisc_jtag_break(jtag_info);
787  if (retval != ERROR_OK) {
788  LOG_TARGET_ERROR(target, "failed to halt target");
789  return retval;
790  }
791 
793 
794  return ERROR_OK;
795 }
796 
797 static int esirisc_disable_step(struct target *target)
798 {
799  struct esirisc_common *esirisc = target_to_esirisc(target);
800  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
801  uint32_t dc;
802  int retval;
803 
804  LOG_TARGET_DEBUG(target, "-");
805 
806  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
807  if (retval != ERROR_OK) {
808  LOG_TARGET_ERROR(target, "failed to read Debug CSR: DC");
809  return retval;
810  }
811 
812  dc &= ~(1<<0); /* DC.S */
813 
814  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
815  if (retval != ERROR_OK) {
816  LOG_TARGET_ERROR(target, "failed to write Debug CSR: DC");
817  return retval;
818  }
819 
820  return ERROR_OK;
821 }
822 
823 static int esirisc_enable_step(struct target *target)
824 {
825  struct esirisc_common *esirisc = target_to_esirisc(target);
826  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
827  uint32_t dc;
828  int retval;
829 
830  LOG_TARGET_DEBUG(target, "-");
831 
832  retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
833  if (retval != ERROR_OK) {
834  LOG_TARGET_ERROR(target, "failed to read Debug CSR: DC");
835  return retval;
836  }
837 
838  dc |= (1<<0); /* DC.S */
839 
840  retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
841  if (retval != ERROR_OK) {
842  LOG_TARGET_ERROR(target, "failed to write Debug CSR: DC");
843  return retval;
844  }
845 
846  return ERROR_OK;
847 }
848 
849 static int esirisc_resume_or_step(struct target *target, int current, target_addr_t address,
850  int handle_breakpoints, int debug_execution, bool step)
851 {
852  struct esirisc_common *esirisc = target_to_esirisc(target);
853  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
854  struct breakpoint *breakpoint = NULL;
855  int retval;
856 
857  LOG_TARGET_DEBUG(target, "-");
858 
859  if (target->state != TARGET_HALTED) {
860  LOG_TARGET_ERROR(target, "not halted");
862  }
863 
864  if (!debug_execution) {
868  }
869 
870  if (current)
871  address = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
872  else {
873  buf_set_u32(esirisc->epc->value, 0, esirisc->epc->size, address);
874  esirisc->epc->dirty = true;
875  esirisc->epc->valid = true;
876  }
877 
879 
880  if (esirisc_has_cache(esirisc))
882 
883  if (handle_breakpoints) {
885  if (breakpoint)
887  }
888 
889  if (step) {
893  } else {
897  }
898 
900 
901  retval = esirisc_jtag_continue(jtag_info);
902  if (retval != ERROR_OK) {
903  LOG_TARGET_ERROR(target, "failed to resume target");
904  return retval;
905  }
906 
908 
909  if (!debug_execution) {
912  } else {
915  }
916 
917  return ERROR_OK;
918 }
919 
920 static int esirisc_resume(struct target *target, int current, target_addr_t address,
921  int handle_breakpoints, int debug_execution)
922 {
923  LOG_TARGET_DEBUG(target, "-");
924 
925  return esirisc_resume_or_step(target, current, address,
926  handle_breakpoints, debug_execution, false);
927 }
928 
929 static int esirisc_step(struct target *target, int current, target_addr_t address,
930  int handle_breakpoints)
931 {
932  LOG_TARGET_DEBUG(target, "-");
933 
934  return esirisc_resume_or_step(target, current, address,
935  handle_breakpoints, 0, true);
936 }
937 
938 static int esirisc_debug_step(struct target *target)
939 {
940  struct esirisc_common *esirisc = target_to_esirisc(target);
941  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
942  int retval;
943 
944  LOG_TARGET_DEBUG(target, "-");
945 
948 
949  retval = esirisc_jtag_continue(jtag_info);
950  if (retval != ERROR_OK) {
951  LOG_TARGET_ERROR(target, "failed to resume target");
952  return retval;
953  }
954 
955  retval = esirisc_wait_debug_active(esirisc, STEP_TIMEOUT);
956  if (retval != ERROR_OK) {
957  LOG_TARGET_ERROR(target, "step timed out");
958  return retval;
959  }
960 
963 
964  return ERROR_OK;
965 }
966 
967 static int esirisc_debug_reset(struct target *target)
968 {
969  struct esirisc_common *esirisc = target_to_esirisc(target);
970  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
971  int retval;
972 
973  LOG_TARGET_DEBUG(target, "-");
974 
975  retval = esirisc_jtag_assert_reset(jtag_info);
976  if (retval != ERROR_OK) {
977  LOG_TARGET_ERROR(target, "failed to assert reset");
978  return retval;
979  }
980 
981  retval = esirisc_jtag_deassert_reset(jtag_info);
982  if (retval != ERROR_OK) {
983  LOG_TARGET_ERROR(target, "failed to deassert reset");
984  return retval;
985  }
986 
987  retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
988  if (retval != ERROR_OK) {
989  LOG_TARGET_ERROR(target, "reset timed out");
990  return retval;
991  }
992 
993  return ERROR_OK;
994 }
995 
996 static int esirisc_debug_enable(struct target *target)
997 {
998  struct esirisc_common *esirisc = target_to_esirisc(target);
999  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1000  int retval;
1001 
1002  LOG_TARGET_DEBUG(target, "-");
1003 
1004  retval = esirisc_jtag_enable_debug(jtag_info);
1005  if (retval != ERROR_OK) {
1006  LOG_TARGET_ERROR(target, "failed to enable debug mode");
1007  return retval;
1008  }
1009 
1010  /*
1011  * The debug clock is inactive until the first command is sent.
1012  * If the target is stopped, we must first issue a reset before
1013  * attempting further communication. This also handles unpowered
1014  * targets, which will respond with all ones and appear active.
1015  */
1016  if (esirisc_jtag_is_stopped(jtag_info)) {
1017  LOG_TARGET_INFO(target, "debug clock inactive; attempting debug reset");
1018  retval = esirisc_debug_reset(target);
1019  if (retval != ERROR_OK)
1020  return retval;
1021 
1022  if (esirisc_jtag_is_stopped(jtag_info)) {
1023  LOG_TARGET_ERROR(target, "target unresponsive; giving up");
1024  return ERROR_FAIL;
1025  }
1026  }
1027 
1028  return ERROR_OK;
1029 }
1030 
1031 static int esirisc_debug_entry(struct target *target)
1032 {
1033  struct esirisc_common *esirisc = target_to_esirisc(target);
1034  struct breakpoint *breakpoint;
1035 
1036  LOG_TARGET_DEBUG(target, "-");
1037 
1039 
1040  if (esirisc_has_cache(esirisc))
1042 
1045 
1046  uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
1047  switch (eid) {
1048  /*
1049  * InstBreakpoint exceptions are also raised when a core is
1050  * halted for debugging. The following is required to
1051  * determine if a breakpoint was encountered.
1052  */
1053  case EID_INST_BREAKPOINT:
1055  buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size));
1058  break;
1059 
1060  /*
1061  * eSi-RISC treats watchpoints similarly to breakpoints,
1062  * however GDB will not request to step over the current
1063  * instruction when a watchpoint fires. The following is
1064  * required to resume the target.
1065  */
1066  case EID_DATA_BREAKPOINT:
1071  break;
1072 
1073  default:
1075  }
1076  }
1077 
1078  return ERROR_OK;
1079 }
1080 
1081 static int esirisc_poll(struct target *target)
1082 {
1083  struct esirisc_common *esirisc = target_to_esirisc(target);
1084  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1085  int retval;
1086 
1087  retval = esirisc_jtag_enable_debug(jtag_info);
1088  if (retval != ERROR_OK) {
1089  LOG_TARGET_ERROR(target, "failed to poll target");
1090  return retval;
1091  }
1092 
1093  if (esirisc_jtag_is_stopped(jtag_info)) {
1094  LOG_TARGET_ERROR(target, "target has stopped; reset required");
1096  return ERROR_TARGET_FAILURE;
1097  }
1098 
1099  if (esirisc_jtag_is_debug_active(jtag_info)) {
1102 
1103  retval = esirisc_debug_entry(target);
1104  if (retval != ERROR_OK)
1105  return retval;
1106 
1108  }
1109 
1110  } else if (target->state == TARGET_HALTED || target->state == TARGET_RESET) {
1113  }
1114 
1115  return ERROR_OK;
1116 }
1117 
1119 {
1120  struct esirisc_common *esirisc = target_to_esirisc(target);
1121  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1122  int retval;
1123 
1124  LOG_TARGET_DEBUG(target, "-");
1125 
1127  jtag_add_reset(1, 1);
1129  jtag_add_reset(0, 1);
1130  } else {
1133 
1134  retval = esirisc_jtag_assert_reset(jtag_info);
1135  if (retval != ERROR_OK) {
1136  LOG_TARGET_ERROR(target, "failed to assert reset");
1137  return retval;
1138  }
1139  }
1140 
1142 
1144 
1145  return ERROR_OK;
1146 }
1147 
1148 static int esirisc_reset_entry(struct target *target)
1149 {
1150  struct esirisc_common *esirisc = target_to_esirisc(target);
1151  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1152  uint32_t eta, epc;
1153  int retval;
1154 
1155  LOG_TARGET_DEBUG(target, "-");
1156 
1157  /* read exception table address */
1158  retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETA, &eta);
1159  if (retval != ERROR_OK) {
1160  LOG_TARGET_ERROR(target, "failed to read Thread CSR: ETA");
1161  return retval;
1162  }
1163 
1164  /* read reset entry point */
1165  retval = esirisc_jtag_read_word(jtag_info, eta + ENTRY_RESET, &epc);
1166  if (retval != ERROR_OK) {
1167  LOG_TARGET_ERROR(target, "failed to read address: 0x%" TARGET_PRIxADDR,
1168  (target_addr_t)epc);
1169  return retval;
1170  }
1171 
1172  /* write reset entry point */
1173  retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_EPC, epc);
1174  if (retval != ERROR_OK) {
1175  LOG_TARGET_ERROR(target, "failed to write Thread CSR: EPC");
1176  return retval;
1177  }
1178 
1179  return ERROR_OK;
1180 }
1181 
1183 {
1184  struct esirisc_common *esirisc = target_to_esirisc(target);
1185  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1186  int retval;
1187 
1188  LOG_TARGET_DEBUG(target, "-");
1189 
1191  jtag_add_reset(0, 0);
1192 
1193  retval = esirisc_debug_enable(target);
1194  if (retval != ERROR_OK)
1195  return retval;
1196 
1197  retval = esirisc_debug_reset(target);
1198  if (retval != ERROR_OK)
1199  return retval;
1200 
1201  } else {
1202  retval = esirisc_jtag_deassert_reset(jtag_info);
1203  if (retval != ERROR_OK) {
1204  LOG_TARGET_ERROR(target, "failed to deassert reset");
1205  return retval;
1206  }
1207  }
1208 
1209  retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
1210  if (retval != ERROR_OK) {
1211  LOG_TARGET_ERROR(target, "reset timed out");
1212  return retval;
1213  }
1214 
1215  retval = esirisc_reset_entry(target);
1216  if (retval != ERROR_OK)
1217  return retval;
1218 
1221 
1223 
1224  if (!target->reset_halt) {
1225  retval = esirisc_jtag_continue(jtag_info);
1226  if (retval != ERROR_OK) {
1227  LOG_TARGET_ERROR(target, "failed to resume target");
1228  return retval;
1229  }
1230  }
1231 
1232  return ERROR_OK;
1233 }
1234 
1235 static int esirisc_arch_state(struct target *target)
1236 {
1237  struct esirisc_common *esirisc = target_to_esirisc(target);
1238  uint32_t epc = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
1239  uint32_t ecas = buf_get_u32(esirisc->ecas->value, 0, esirisc->ecas->size);
1240  uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
1241  uint32_t ed = buf_get_u32(esirisc->ed->value, 0, esirisc->ed->size);
1242 
1243  LOG_TARGET_USER(target, "target halted due to %s, exception: %s\n"
1244  "EPC: 0x%" PRIx32 ", ECAS: 0x%" PRIx32 ", EID: 0x%" PRIx32 ", ED: 0x%" PRIx32,
1246 
1247  return ERROR_OK;
1248 }
1249 
1250 static const char *esirisc_get_gdb_arch(const struct target *target)
1251 {
1252  struct esirisc_common *esirisc = target_to_esirisc(target);
1253 
1254  LOG_TARGET_DEBUG(target, "-");
1255 
1256  /*
1257  * Targets with the UNIFIED_ADDRESS_SPACE option disabled employ a
1258  * Harvard architecture. This option is not exposed in a CSR, which
1259  * requires additional configuration to properly interact with these
1260  * targets in GDB (also see: `esirisc cache_arch`).
1261  */
1262  if (!esirisc->gdb_arch && target_was_examined(target))
1263  esirisc->gdb_arch = alloc_printf("esirisc:%d_bit_%d_reg_%s",
1264  esirisc->num_bits, esirisc->num_regs, esirisc_cache_arch_name(esirisc));
1265 
1266  return esirisc->gdb_arch;
1267 }
1268 
1269 static int esirisc_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
1270  int *reg_list_size, enum target_register_class reg_class)
1271 {
1272  struct esirisc_common *esirisc = target_to_esirisc(target);
1273 
1274  LOG_TARGET_DEBUG(target, "-");
1275 
1276  *reg_list_size = ESIRISC_NUM_REGS;
1277 
1278  *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
1279  if (!*reg_list)
1280  return ERROR_FAIL;
1281 
1282  if (reg_class == REG_CLASS_ALL)
1283  for (int i = 0; i < *reg_list_size; ++i)
1284  (*reg_list)[i] = esirisc->reg_cache->reg_list + i;
1285  else {
1286  for (int i = 0; i < esirisc->num_regs; ++i)
1287  (*reg_list)[i] = esirisc->reg_cache->reg_list + i;
1288 
1289  (*reg_list)[ESIRISC_PC] = esirisc->reg_cache->reg_list + ESIRISC_PC;
1290  (*reg_list)[ESIRISC_CAS] = esirisc->reg_cache->reg_list + ESIRISC_CAS;
1291  }
1292 
1293  return ERROR_OK;
1294 }
1295 
1296 static int esirisc_read_reg(struct reg *reg)
1297 {
1298  struct esirisc_reg *reg_info = reg->arch_info;
1299  struct esirisc_common *esirisc = reg_info->esirisc;
1300  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1301  struct target *target = esirisc->target;
1302  uint32_t data;
1303 
1304  LOG_TARGET_DEBUG(target, "-");
1305 
1306  int retval = esirisc_jtag_read_reg(jtag_info, reg->number, &data);
1307  if (retval != ERROR_OK) {
1308  LOG_TARGET_ERROR(target, "failed to read register: %s", reg->name);
1309  return retval;
1310  }
1311 
1312  buf_set_u32(reg->value, 0, reg->size, data);
1313  reg->dirty = false;
1314  reg->valid = true;
1315 
1316  return ERROR_OK;
1317 }
1318 
1319 static int esirisc_write_reg(struct reg *reg)
1320 {
1321  struct esirisc_reg *reg_info = reg->arch_info;
1322  struct esirisc_common *esirisc = reg_info->esirisc;
1323  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1324  struct target *target = esirisc->target;
1325  uint32_t data = buf_get_u32(reg->value, 0, reg->size);
1326 
1327  LOG_TARGET_DEBUG(target, "-");
1328 
1329  int retval = esirisc_jtag_write_reg(jtag_info, reg->number, data);
1330  if (retval != ERROR_OK) {
1331  LOG_TARGET_ERROR(target, "failed to write register: %s", reg->name);
1332  return retval;
1333  }
1334 
1335  reg->dirty = false;
1336  reg->valid = true;
1337 
1338  return ERROR_OK;
1339 }
1340 
1341 static int esirisc_read_csr(struct reg *reg)
1342 {
1343  struct esirisc_reg *reg_info = reg->arch_info;
1344  struct esirisc_common *esirisc = reg_info->esirisc;
1345  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1346  struct target *target = esirisc->target;
1347  uint32_t data;
1348 
1349  LOG_TARGET_DEBUG(target, "-");
1350 
1351  int retval = esirisc_jtag_read_csr(jtag_info, reg_info->bank, reg_info->csr, &data);
1352  if (retval != ERROR_OK) {
1353  LOG_TARGET_ERROR(target, "failed to read CSR: %s", reg->name);
1354  return retval;
1355  }
1356 
1357  buf_set_u32(reg->value, 0, reg->size, data);
1358  reg->dirty = false;
1359  reg->valid = true;
1360 
1361  return ERROR_OK;
1362 }
1363 
1364 static int esirisc_write_csr(struct reg *reg)
1365 {
1366  struct esirisc_reg *reg_info = reg->arch_info;
1367  struct esirisc_common *esirisc = reg_info->esirisc;
1368  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1369  struct target *target = esirisc->target;
1370  uint32_t data = buf_get_u32(reg->value, 0, reg->size);
1371 
1372  LOG_TARGET_DEBUG(target, "-");
1373 
1374  int retval = esirisc_jtag_write_csr(jtag_info, reg_info->bank, reg_info->csr, data);
1375  if (retval != ERROR_OK) {
1376  LOG_TARGET_ERROR(target, "failed to write CSR: %s", reg->name);
1377  return retval;
1378  }
1379 
1380  reg->dirty = false;
1381  reg->valid = true;
1382 
1383  return ERROR_OK;
1384 }
1385 
1386 static int esirisc_get_reg(struct reg *reg)
1387 {
1388  struct esirisc_reg *reg_info = reg->arch_info;
1389  struct esirisc_common *esirisc = reg_info->esirisc;
1390  struct target *target = esirisc->target;
1391 
1392  LOG_TARGET_DEBUG(target, "-");
1393 
1394  if (target->state != TARGET_HALTED)
1395  return ERROR_TARGET_NOT_HALTED;
1396 
1397  return reg_info->read(reg);
1398 }
1399 
1400 static int esirisc_set_reg(struct reg *reg, uint8_t *buf)
1401 {
1402  struct esirisc_reg *reg_info = reg->arch_info;
1403  struct esirisc_common *esirisc = reg_info->esirisc;
1404  struct target *target = esirisc->target;
1405  uint32_t value = buf_get_u32(buf, 0, reg->size);
1406 
1407  LOG_TARGET_DEBUG(target, "-");
1408 
1409  if (target->state != TARGET_HALTED)
1410  return ERROR_TARGET_NOT_HALTED;
1411 
1412  buf_set_u32(reg->value, 0, reg->size, value);
1413  reg->dirty = true;
1414  reg->valid = true;
1415 
1416  return ERROR_OK;
1417 }
1418 
1419 static const struct reg_arch_type esirisc_reg_type = {
1420  .get = esirisc_get_reg,
1421  .set = esirisc_set_reg,
1422 };
1423 
1425 {
1426  struct esirisc_common *esirisc = target_to_esirisc(target);
1427  struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
1428  struct reg_cache *cache = malloc(sizeof(struct reg_cache));
1429  struct reg *reg_list = calloc(ESIRISC_NUM_REGS, sizeof(struct reg));
1430 
1431  LOG_TARGET_DEBUG(target, "-");
1432 
1433  cache->name = "eSi-RISC registers";
1434  cache->next = NULL;
1435  cache->reg_list = reg_list;
1436  cache->num_regs = ESIRISC_NUM_REGS;
1437  (*cache_p) = cache;
1438 
1439  esirisc->reg_cache = cache;
1440  esirisc->epc = reg_list + ESIRISC_EPC;
1441  esirisc->ecas = reg_list + ESIRISC_ECAS;
1442  esirisc->eid = reg_list + ESIRISC_EID;
1443  esirisc->ed = reg_list + ESIRISC_ED;
1444 
1445  for (int i = 0; i < esirisc->num_regs; ++i) {
1446  struct reg *reg = reg_list + esirisc_regs[i].number;
1447  struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));
1448 
1449  reg->name = esirisc_regs[i].name;
1450  reg->number = esirisc_regs[i].number;
1451  reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
1452  reg->size = esirisc->num_bits;
1453  reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
1454  reg->reg_data_type->type = esirisc_regs[i].type;
1455  reg->group = esirisc_regs[i].group;
1456  reg_info->esirisc = esirisc;
1457  reg_info->read = esirisc_read_reg;
1458  reg_info->write = esirisc_write_reg;
1459  reg->arch_info = reg_info;
1461  reg->exist = true;
1462  }
1463 
1464  for (size_t i = 0; i < ARRAY_SIZE(esirisc_csrs); ++i) {
1465  struct reg *reg = reg_list + esirisc_csrs[i].number;
1466  struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));
1467 
1468  reg->name = esirisc_csrs[i].name;
1469  reg->number = esirisc_csrs[i].number;
1470  reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
1471  reg->size = esirisc->num_bits;
1472  reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
1473  reg->reg_data_type->type = esirisc_csrs[i].type;
1474  reg->group = esirisc_csrs[i].group;
1475  reg_info->esirisc = esirisc;
1476  reg_info->bank = esirisc_csrs[i].bank;
1477  reg_info->csr = esirisc_csrs[i].csr;
1478  reg_info->read = esirisc_read_csr;
1479  reg_info->write = esirisc_write_csr;
1480  reg->arch_info = reg_info;
1482  reg->exist = true;
1483  }
1484 
1485  return cache;
1486 }
1487 
1489 {
1490  struct esirisc_common *esirisc = target_to_esirisc(target);
1491  struct reg_cache *cache = esirisc->reg_cache;
1492  struct reg *reg_list = cache->reg_list;
1493 
1494  for (int i = 0; i < esirisc->num_regs; ++i) {
1495  struct reg *reg = reg_list + esirisc_regs[i].number;
1496 
1497  free(reg->arch_info);
1498  free(reg->value);
1499  free(reg->reg_data_type);
1500  }
1501 
1502  for (size_t i = 0; i < ARRAY_SIZE(esirisc_csrs); ++i) {
1503  struct reg *reg = reg_list + esirisc_csrs[i].number;
1504 
1505  free(reg->arch_info);
1506  free(reg->value);
1507  free(reg->reg_data_type);
1508  }
1509 
1510  free(reg_list);
1511  free(cache);
1512 }
1513 
1514 static int esirisc_identify(struct target *target)
1515 {
1516  struct esirisc_common *esirisc = target_to_esirisc(target);
1517  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1518  uint32_t csr;
1519  int retval;
1520 
1521  LOG_TARGET_DEBUG(target, "-");
1522 
1523  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_ARCH0, &csr);
1524  if (retval != ERROR_OK) {
1525  LOG_TARGET_ERROR(target, "failed to read Configuration CSR: ARCH0");
1526  return retval;
1527  }
1528 
1529  esirisc->num_bits = (csr >> 0) & 0x3f; /* ARCH0.B */
1530  esirisc->num_regs = (csr >> 10) & 0x3f; /* ARCH0.R */
1531 
1532  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_MEM, &csr);
1533  if (retval != ERROR_OK) {
1534  LOG_TARGET_ERROR(target, "failed to read Configuration CSR: MEM");
1535  return retval;
1536  }
1537 
1538  target->endianness = (csr & 1<<0) ? /* MEM.E */
1540 
1541  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_IC, &csr);
1542  if (retval != ERROR_OK) {
1543  LOG_TARGET_ERROR(target, "failed to read Configuration CSR: IC");
1544  return retval;
1545  }
1546 
1547  esirisc->has_icache = !!(csr & 1<<0); /* IC.E */
1548 
1549  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DC, &csr);
1550  if (retval != ERROR_OK) {
1551  LOG_TARGET_ERROR(target, "failed to read Configuration CSR: DC");
1552  return retval;
1553  }
1554 
1555  esirisc->has_dcache = !!(csr & 1<<0); /* DC.E */
1556 
1557  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DBG, &csr);
1558  if (retval != ERROR_OK) {
1559  LOG_TARGET_ERROR(target, "failed to read Configuration CSR: DBG");
1560  return retval;
1561  }
1562 
1563  esirisc->num_breakpoints = (csr >> 7) & 0xf; /* DBG.BP */
1564  esirisc->num_watchpoints = (csr >> 12) & 0xf; /* DBG.WP */
1565 
1566  retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_TRACE, &csr);
1567  if (retval != ERROR_OK) {
1568  LOG_TARGET_ERROR(target, "failed to read Configuration CSR: TRACE");
1569  return retval;
1570  }
1571 
1572  esirisc->has_trace = !!(csr & 1<<0); /* TRACE.T */
1573 
1574  return ERROR_OK;
1575 }
1576 
1577 static int esirisc_target_create(struct target *target, Jim_Interp *interp)
1578 {
1579  struct jtag_tap *tap = target->tap;
1580  struct esirisc_common *esirisc;
1581 
1582  if (!tap)
1583  return ERROR_FAIL;
1584 
1585  if (tap->ir_length != INSTR_LENGTH) {
1586  LOG_TARGET_ERROR(target, "invalid IR length; expected %d", INSTR_LENGTH);
1587  return ERROR_FAIL;
1588  }
1589 
1590  esirisc = calloc(1, sizeof(struct esirisc_common));
1591  if (!esirisc)
1592  return ERROR_FAIL;
1593 
1594  esirisc->target = target;
1595  esirisc->jtag_info.tap = tap;
1596  target->arch_info = esirisc;
1597 
1598  return ERROR_OK;
1599 }
1600 
1601 static int esirisc_init_target(struct command_context *cmd_ctx, struct target *target)
1602 {
1603  struct esirisc_common *esirisc = target_to_esirisc(target);
1604 
1605  /* trap reset, error, and debug exceptions */
1606  esirisc->hwdc_save = HWDC_R | HWDC_E | HWDC_D;
1607 
1608  return ERROR_OK;
1609 }
1610 
1612 {
1613  struct esirisc_common *esirisc = target_to_esirisc(target);
1614 
1616  return;
1617 
1619 
1620  free(esirisc->gdb_arch);
1621  free(esirisc);
1622 }
1623 
1624 static int esirisc_examine(struct target *target)
1625 {
1626  struct esirisc_common *esirisc = target_to_esirisc(target);
1627  struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
1628  int retval;
1629 
1630  LOG_TARGET_DEBUG(target, "-");
1631 
1632  if (!target_was_examined(target)) {
1633  retval = esirisc_debug_enable(target);
1634  if (retval != ERROR_OK)
1635  return retval;
1636 
1637  /*
1638  * In order to identify the target we must first halt the core.
1639  * We quietly resume once identification has completed for those
1640  * targets that were running when target_examine was called.
1641  */
1642  if (esirisc_jtag_is_debug_active(jtag_info)) {
1643  if (target->state == TARGET_UNKNOWN)
1645 
1647  } else {
1648  retval = esirisc_jtag_break(jtag_info);
1649  if (retval != ERROR_OK) {
1650  LOG_TARGET_ERROR(target, "failed to halt target");
1651  return retval;
1652  }
1653 
1655  }
1656 
1657  retval = esirisc_identify(target);
1658  if (retval != ERROR_OK) {
1659  LOG_TARGET_ERROR(target, "failed to identify target");
1660  return retval;
1661  }
1662 
1664 
1667 
1670 
1671  if (target->state == TARGET_HALTED)
1673  else {
1674  retval = esirisc_jtag_continue(jtag_info);
1675  if (retval != ERROR_OK) {
1676  LOG_TARGET_ERROR(target, "failed to resume target");
1677  return retval;
1678  }
1679  }
1680 
1682 
1683  LOG_TARGET_INFO(target, "%d bit, %d registers, %s%s%s",
1684  esirisc->num_bits, esirisc->num_regs,
1686  esirisc->has_icache ? ", icache" : "",
1687  esirisc->has_dcache ? ", dcache" : "");
1688 
1689  LOG_TARGET_INFO(target, "hardware has %d breakpoints, %d watchpoints%s",
1690  esirisc->num_breakpoints, esirisc->num_watchpoints,
1691  esirisc->has_trace ? ", trace" : "");
1692  }
1693 
1694  return ERROR_OK;
1695 }
1696 
1697 COMMAND_HANDLER(handle_esirisc_cache_arch_command)
1698 {
1700  struct esirisc_common *esirisc = target_to_esirisc(target);
1701 
1702  if (CMD_ARGC > 0) {
1703  if (strcmp(*CMD_ARGV, "harvard") == 0)
1704  esirisc->cache_arch = ESIRISC_CACHE_HARVARD;
1705  else if (strcmp(*CMD_ARGV, "von_neumann") == 0)
1707  else {
1708  LOG_TARGET_ERROR(target, "invalid cache_arch: %s", *CMD_ARGV);
1710  }
1711  }
1712 
1713  command_print(CMD, "esirisc cache_arch %s", esirisc_cache_arch_name(esirisc));
1714 
1715  return ERROR_OK;
1716 }
1717 
1718 COMMAND_HANDLER(handle_esirisc_flush_caches_command)
1719 {
1721  struct esirisc_common *esirisc = target_to_esirisc(target);
1722  int retval;
1723 
1724  if (!esirisc_has_cache(esirisc)) {
1725  LOG_TARGET_ERROR(target, "target does not support caching");
1726  return ERROR_FAIL;
1727  }
1728 
1729  retval = esirisc_flush_caches(target);
1730 
1731  command_print(CMD, "cache flush %s",
1732  (retval == ERROR_OK) ? "successful" : "failed");
1733 
1734  return retval;
1735 }
1736 
1737 static const struct {
1738  const char *name;
1739  int mask;
1740 } esirisc_hwdc_masks[] = {
1741  { "reset", HWDC_R },
1742  { "interrupt", HWDC_I },
1743  { "syscall", HWDC_S },
1744  { "error", HWDC_E },
1745  { "debug", HWDC_D },
1746 };
1747 
1748 static int esirisc_find_hwdc_mask(const char *name)
1749 {
1750  for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
1751  if (strcmp(esirisc_hwdc_masks[i].name, name) == 0)
1752  return esirisc_hwdc_masks[i].mask;
1753 
1754  return -1;
1755 }
1756 
1757 COMMAND_HANDLER(handle_esirisc_hwdc_command)
1758 {
1760  struct esirisc_common *esirisc = target_to_esirisc(target);
1761 
1762  if (CMD_ARGC > 0) {
1763  if (strcmp(CMD_ARGV[0], "all") == 0)
1764  esirisc->hwdc_save = HWDC_R | HWDC_I | HWDC_S | HWDC_E | HWDC_D;
1765  else {
1766  esirisc->hwdc_save = 0;
1767  if (strcmp(CMD_ARGV[0], "none") != 0) {
1768  while (CMD_ARGC-- > 0) {
1770  if (mask < 0) {
1771  LOG_TARGET_ERROR(target, "invalid mask: %s", CMD_ARGV[CMD_ARGC]);
1773  }
1774  esirisc->hwdc_save |= mask;
1775  }
1776  }
1777  }
1778  }
1779 
1780  for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
1781  command_print(CMD, "%9s: %s", esirisc_hwdc_masks[i].name,
1782  (esirisc->hwdc_save & esirisc_hwdc_masks[i].mask) ? "enabled" : "disabled");
1783 
1784  return ERROR_OK;
1785 }
1786 
1787 static const struct command_registration esirisc_exec_command_handlers[] = {
1788  {
1789  .name = "flush_caches",
1790  .handler = handle_esirisc_flush_caches_command,
1791  .mode = COMMAND_EXEC,
1792  .help = "flush instruction and data caches",
1793  .usage = "",
1794  },
1796 };
1797 
1798 static const struct command_registration esirisc_any_command_handlers[] = {
1799  {
1800  .name = "cache_arch",
1801  .handler = handle_esirisc_cache_arch_command,
1802  .mode = COMMAND_ANY,
1803  .help = "configure cache architecture",
1804  .usage = "['harvard'|'von_neumann']",
1805  },
1806  {
1807  .name = "hwdc",
1808  .handler = handle_esirisc_hwdc_command,
1809  .mode = COMMAND_ANY,
1810  .help = "configure hardware debug control",
1811  .usage = "['all'|'none'|mask ...]",
1812  },
1813  {
1815  },
1816  {
1818  },
1820 };
1821 
1822 static const struct command_registration esirisc_command_handlers[] = {
1823  {
1824  .name = "esirisc",
1825  .mode = COMMAND_ANY,
1826  .help = "eSi-RISC command group",
1827  .usage = "",
1829  },
1831 };
1832 
1833 struct target_type esirisc_target = {
1834  .name = "esirisc",
1835 
1836  .poll = esirisc_poll,
1837  .arch_state = esirisc_arch_state,
1838 
1839  .halt = esirisc_halt,
1840  .resume = esirisc_resume,
1841  .step = esirisc_step,
1842 
1843  .assert_reset = esirisc_assert_reset,
1844  .deassert_reset = esirisc_deassert_reset,
1845 
1846  .get_gdb_arch = esirisc_get_gdb_arch,
1847  .get_gdb_reg_list = esirisc_get_gdb_reg_list,
1848 
1849  .read_memory = esirisc_read_memory,
1850  .write_memory = esirisc_write_memory,
1851  .checksum_memory = esirisc_checksum_memory,
1852 
1853  .add_breakpoint = esirisc_add_breakpoint,
1854  .remove_breakpoint = esirisc_remove_breakpoint,
1855  .add_watchpoint = esirisc_add_watchpoint,
1856  .remove_watchpoint = esirisc_remove_watchpoint,
1857 
1858  .commands = esirisc_command_handlers,
1859 
1860  .target_create = esirisc_target_create,
1861  .init_target = esirisc_init_target,
1862  .deinit_target = esirisc_deinit_target,
1863  .examine = esirisc_examine,
1864 };
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:1514
static int esirisc_set_reg(struct reg *reg, uint8_t *buf)
Definition: esirisc.c:1400
static int esirisc_disable_step(struct target *target)
Definition: esirisc.c:797
static int esirisc_examine(struct target *target)
Definition: esirisc.c:1624
COMMAND_HANDLER(handle_esirisc_cache_arch_command)
Definition: esirisc.c:1697
static int esirisc_poll(struct target *target)
Definition: esirisc.c:1081
static const char *const esirisc_exception_strings[]
Definition: esirisc.c:33
static int esirisc_enable_step(struct target *target)
Definition: esirisc.c:823
static int esirisc_wait_debug_active(struct esirisc_common *esirisc, int ms)
Definition: esirisc.c:335
struct target_type esirisc_target
Definition: esirisc.c:1833
static const char * esirisc_get_gdb_arch(const struct target *target)
Definition: esirisc.c:1250
static const struct reg_arch_type esirisc_reg_type
Definition: esirisc.c:1419
#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:1748
static const struct command_registration esirisc_any_command_handlers[]
Definition: esirisc.c:1798
static int esirisc_read_csr(struct reg *reg)
Definition: esirisc.c:1341
static void esirisc_free_reg_cache(struct target *target)
Definition: esirisc.c:1488
#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:1601
static void esirisc_deinit_target(struct target *target)
Definition: esirisc.c:1611
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:1319
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:1296
static int esirisc_save_interrupts(struct target *target)
Definition: esirisc.c:210
static int esirisc_add_watchpoints(struct target *target)
Definition: esirisc.c:710
static int esirisc_deassert_reset(struct target *target)
Definition: esirisc.c:1182
static int esirisc_get_reg(struct reg *reg)
Definition: esirisc.c:1386
uint8_t csr
Definition: esirisc.c:136
static int esirisc_debug_reset(struct target *target)
Definition: esirisc.c:967
static int esirisc_target_create(struct target *target, Jim_Interp *interp)
Definition: esirisc.c:1577
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:929
static int esirisc_reset_entry(struct target *target)
Definition: esirisc.c:1148
static int esirisc_write_csr(struct reg *reg)
Definition: esirisc.c:1364
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:1269
static int esirisc_halt(struct target *target)
Definition: esirisc.c:776
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:1235
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:1118
static int esirisc_resume(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution)
Definition: esirisc.c:920
static const struct @93 esirisc_regs[]
int mask
Definition: esirisc.c:1739
static int esirisc_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: esirisc.c:726
static const struct command_registration esirisc_exec_command_handlers[]
Definition: esirisc.c:1787
static int esirisc_restore_interrupts(struct target *target)
Definition: esirisc.c:227
static const struct command_registration esirisc_command_handlers[]
Definition: esirisc.c:1822
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:849
static int esirisc_remove_watchpoints(struct target *target)
Definition: esirisc.c:757
static int esirisc_debug_enable(struct target *target)
Definition: esirisc.c:996
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:938
static int esirisc_restore_context(struct target *target)
Definition: esirisc.c:297
static int esirisc_debug_entry(struct target *target)
Definition: esirisc.c:1031
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:1424
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:267
int esirisc_jtag_read_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t *data)
Definition: esirisc_jtag.c:431
int esirisc_jtag_deassert_reset(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:505
int esirisc_jtag_read_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t *data)
Definition: esirisc_jtag.c:385
int esirisc_jtag_read_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t *data)
Definition: esirisc_jtag.c:239
int esirisc_jtag_write_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t data)
Definition: esirisc_jtag.c:323
int esirisc_jtag_continue(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:515
int esirisc_jtag_flush_caches(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:520
bool esirisc_jtag_is_debug_active(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:216
int esirisc_jtag_write_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t data)
Definition: esirisc_jtag.c:459
int esirisc_jtag_write_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t data)
Definition: esirisc_jtag.c:411
int esirisc_jtag_read_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t *data)
Definition: esirisc_jtag.c:295
bool esirisc_jtag_is_stopped(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:221
int esirisc_jtag_assert_reset(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:500
int esirisc_jtag_write_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t data)
Definition: esirisc_jtag.c:364
int esirisc_jtag_write_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t data)
Definition: esirisc_jtag.c:343
int esirisc_jtag_enable_debug(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:490
int esirisc_jtag_break(struct esirisc_jtag *jtag_info)
Definition: esirisc_jtag.c:510
#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:764
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1740
@ RESET_HAS_SRST
Definition: jtag.h:218
@ RESET_SRST_PULLS_TRST
Definition: jtag.h:220
void alive_sleep(uint64_t ms)
Definition: log.c:456
char * alloc_printf(const char *format,...)
Definition: log.c:364
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:152
#define LOG_TARGET_USER(target, fmt_str,...)
Definition: log.h:155
#define ERROR_FAIL
Definition: log.h:173
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:161
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:149
#define ERROR_OK
Definition: log.h:167
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
@ 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