You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1834 lines
50 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2018 by Square, Inc. *
  3. * Steven Stallion <stallion@squareup.com> *
  4. * James Zhao <hjz@squareup.com> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <helper/binarybuffer.h>
  23. #include <helper/command.h>
  24. #include <helper/log.h>
  25. #include <helper/time_support.h>
  26. #include <helper/types.h>
  27. #include <jtag/interface.h>
  28. #include <target/breakpoints.h>
  29. #include <target/register.h>
  30. #include <target/target.h>
  31. #include <target/target_type.h>
  32. #include "esirisc.h"
  33. #define RESET_TIMEOUT 5000 /* 5s */
  34. #define STEP_TIMEOUT 1000 /* 1s */
  35. /*
  36. * eSi-RISC targets support a configurable number of interrupts;
  37. * up to 32 interrupts are supported.
  38. */
  39. static const char * const esirisc_exception_strings[] = {
  40. [EID_RESET] = "Reset",
  41. [EID_HARDWARE_FAILURE] = "HardwareFailure",
  42. [EID_NMI] = "NMI",
  43. [EID_INST_BREAKPOINT] = "InstBreakpoint",
  44. [EID_DATA_BREAKPOINT] = "DataBreakpoint",
  45. [EID_UNSUPPORTED] = "Unsupported",
  46. [EID_PRIVILEGE_VIOLATION] = "PrivilegeViolation",
  47. [EID_INST_BUS_ERROR] = "InstBusError",
  48. [EID_DATA_BUS_ERROR] = "DataBusError",
  49. [EID_ALIGNMENT_ERROR] = "AlignmentError",
  50. [EID_ARITHMETIC_ERROR] = "ArithmeticError",
  51. [EID_SYSTEM_CALL] = "SystemCall",
  52. [EID_MEMORY_MANAGEMENT] = "MemoryManagement",
  53. [EID_UNRECOVERABLE] = "Unrecoverable",
  54. [EID_INTERRUPT_N+0] = "Interrupt0",
  55. [EID_INTERRUPT_N+1] = "Interrupt1",
  56. [EID_INTERRUPT_N+2] = "Interrupt2",
  57. [EID_INTERRUPT_N+3] = "Interrupt3",
  58. [EID_INTERRUPT_N+4] = "Interrupt4",
  59. [EID_INTERRUPT_N+5] = "Interrupt5",
  60. [EID_INTERRUPT_N+6] = "Interrupt6",
  61. [EID_INTERRUPT_N+7] = "Interrupt7",
  62. [EID_INTERRUPT_N+8] = "Interrupt8",
  63. [EID_INTERRUPT_N+9] = "Interrupt9",
  64. [EID_INTERRUPT_N+10] = "Interrupt10",
  65. [EID_INTERRUPT_N+11] = "Interrupt11",
  66. [EID_INTERRUPT_N+12] = "Interrupt12",
  67. [EID_INTERRUPT_N+13] = "Interrupt13",
  68. [EID_INTERRUPT_N+14] = "Interrupt14",
  69. [EID_INTERRUPT_N+15] = "Interrupt15",
  70. [EID_INTERRUPT_N+16] = "Interrupt16",
  71. [EID_INTERRUPT_N+17] = "Interrupt17",
  72. [EID_INTERRUPT_N+18] = "Interrupt18",
  73. [EID_INTERRUPT_N+19] = "Interrupt19",
  74. [EID_INTERRUPT_N+20] = "Interrupt20",
  75. [EID_INTERRUPT_N+21] = "Interrupt21",
  76. [EID_INTERRUPT_N+22] = "Interrupt22",
  77. [EID_INTERRUPT_N+23] = "Interrupt23",
  78. [EID_INTERRUPT_N+24] = "Interrupt24",
  79. [EID_INTERRUPT_N+25] = "Interrupt25",
  80. [EID_INTERRUPT_N+26] = "Interrupt26",
  81. [EID_INTERRUPT_N+27] = "Interrupt27",
  82. [EID_INTERRUPT_N+28] = "Interrupt28",
  83. [EID_INTERRUPT_N+29] = "Interrupt29",
  84. [EID_INTERRUPT_N+30] = "Interrupt30",
  85. [EID_INTERRUPT_N+31] = "Interrupt31",
  86. };
  87. /*
  88. * eSi-RISC targets support a configurable number of general purpose
  89. * registers; 8, 16, and 32 registers are supported.
  90. */
  91. static const struct {
  92. enum esirisc_reg_num number;
  93. const char *name;
  94. enum reg_type type;
  95. const char *group;
  96. } esirisc_regs[] = {
  97. { ESIRISC_SP, "sp", REG_TYPE_DATA_PTR, "general" },
  98. { ESIRISC_RA, "ra", REG_TYPE_INT, "general" },
  99. { ESIRISC_R2, "r2", REG_TYPE_INT, "general" },
  100. { ESIRISC_R3, "r3", REG_TYPE_INT, "general" },
  101. { ESIRISC_R4, "r4", REG_TYPE_INT, "general" },
  102. { ESIRISC_R5, "r5", REG_TYPE_INT, "general" },
  103. { ESIRISC_R6, "r6", REG_TYPE_INT, "general" },
  104. { ESIRISC_R7, "r7", REG_TYPE_INT, "general" },
  105. { ESIRISC_R8, "r8", REG_TYPE_INT, "general" },
  106. { ESIRISC_R9, "r9", REG_TYPE_INT, "general" },
  107. { ESIRISC_R10, "r10", REG_TYPE_INT, "general" },
  108. { ESIRISC_R11, "r11", REG_TYPE_INT, "general" },
  109. { ESIRISC_R12, "r12", REG_TYPE_INT, "general" },
  110. { ESIRISC_R13, "r13", REG_TYPE_INT, "general" },
  111. { ESIRISC_R14, "r14", REG_TYPE_INT, "general" },
  112. { ESIRISC_R15, "r15", REG_TYPE_INT, "general" },
  113. { ESIRISC_R16, "r16", REG_TYPE_INT, "general" },
  114. { ESIRISC_R17, "r17", REG_TYPE_INT, "general" },
  115. { ESIRISC_R18, "r18", REG_TYPE_INT, "general" },
  116. { ESIRISC_R19, "r19", REG_TYPE_INT, "general" },
  117. { ESIRISC_R20, "r20", REG_TYPE_INT, "general" },
  118. { ESIRISC_R21, "r21", REG_TYPE_INT, "general" },
  119. { ESIRISC_R22, "r22", REG_TYPE_INT, "general" },
  120. { ESIRISC_R23, "r23", REG_TYPE_INT, "general" },
  121. { ESIRISC_R24, "r24", REG_TYPE_INT, "general" },
  122. { ESIRISC_R25, "r25", REG_TYPE_INT, "general" },
  123. { ESIRISC_R26, "r26", REG_TYPE_INT, "general" },
  124. { ESIRISC_R27, "r27", REG_TYPE_INT, "general" },
  125. { ESIRISC_R28, "r28", REG_TYPE_INT, "general" },
  126. { ESIRISC_R29, "r29", REG_TYPE_INT, "general" },
  127. { ESIRISC_R30, "r30", REG_TYPE_INT, "general" },
  128. { ESIRISC_R31, "r31", REG_TYPE_INT, "general" },
  129. };
  130. /*
  131. * Control and Status Registers (CSRs) are largely defined as belonging
  132. * to the system register group. The exception to this rule are the PC
  133. * and CAS registers, which belong to the general group. While debug is
  134. * active, EPC, ECAS, and ETC must be used to read and write the PC,
  135. * CAS, and TC CSRs, respectively.
  136. */
  137. static const struct {
  138. enum esirisc_reg_num number;
  139. uint8_t bank;
  140. uint8_t csr;
  141. const char *name;
  142. enum reg_type type;
  143. const char *group;
  144. } esirisc_csrs[] = {
  145. { ESIRISC_PC, CSR_THREAD, CSR_THREAD_EPC, "PC", REG_TYPE_CODE_PTR, "general" }, /* PC -> EPC */
  146. { ESIRISC_CAS, CSR_THREAD, CSR_THREAD_ECAS, "CAS", REG_TYPE_INT, "general" }, /* CAS -> ECAS */
  147. { ESIRISC_TC, CSR_THREAD, CSR_THREAD_ETC, "TC", REG_TYPE_INT, "system" }, /* TC -> ETC */
  148. { ESIRISC_ETA, CSR_THREAD, CSR_THREAD_ETA, "ETA", REG_TYPE_INT, "system" },
  149. { ESIRISC_ETC, CSR_THREAD, CSR_THREAD_ETC, "ETC", REG_TYPE_INT, "system" },
  150. { ESIRISC_EPC, CSR_THREAD, CSR_THREAD_EPC, "EPC", REG_TYPE_CODE_PTR, "system" },
  151. { ESIRISC_ECAS, CSR_THREAD, CSR_THREAD_ECAS, "ECAS", REG_TYPE_INT, "system" },
  152. { ESIRISC_EID, CSR_THREAD, CSR_THREAD_EID, "EID", REG_TYPE_INT, "system" },
  153. { ESIRISC_ED, CSR_THREAD, CSR_THREAD_ED, "ED", REG_TYPE_INT, "system" },
  154. { ESIRISC_IP, CSR_INTERRUPT, CSR_INTERRUPT_IP, "IP", REG_TYPE_INT, "system"},
  155. { ESIRISC_IM, CSR_INTERRUPT, CSR_INTERRUPT_IM, "IM", REG_TYPE_INT, "system"},
  156. { ESIRISC_IS, CSR_INTERRUPT, CSR_INTERRUPT_IS, "IS", REG_TYPE_INT, "system"},
  157. { ESIRISC_IT, CSR_INTERRUPT, CSR_INTERRUPT_IT, "IT", REG_TYPE_INT, "system"},
  158. };
  159. static int esirisc_disable_interrupts(struct target *target)
  160. {
  161. struct esirisc_common *esirisc = target_to_esirisc(target);
  162. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  163. uint32_t etc;
  164. int retval;
  165. LOG_DEBUG("-");
  166. retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, &etc);
  167. if (retval != ERROR_OK) {
  168. LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
  169. return retval;
  170. }
  171. etc &= ~(1<<0); /* TC.I */
  172. retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, etc);
  173. if (retval != ERROR_OK) {
  174. LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
  175. return retval;
  176. }
  177. return ERROR_OK;
  178. }
  179. #if 0
  180. static int esirisc_enable_interrupts(struct target *target)
  181. {
  182. struct esirisc_common *esirisc = target_to_esirisc(target);
  183. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  184. uint32_t etc;
  185. int retval;
  186. LOG_DEBUG("-");
  187. retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, &etc);
  188. if (retval != ERROR_OK) {
  189. LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
  190. return retval;
  191. }
  192. etc |= (1<<0); /* TC.I */
  193. retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, etc);
  194. if (retval != ERROR_OK) {
  195. LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
  196. return retval;
  197. }
  198. return ERROR_OK;
  199. }
  200. #endif
  201. static int esirisc_save_interrupts(struct target *target)
  202. {
  203. struct esirisc_common *esirisc = target_to_esirisc(target);
  204. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  205. LOG_DEBUG("-");
  206. int retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC,
  207. &esirisc->etc_save);
  208. if (retval != ERROR_OK) {
  209. LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
  210. return retval;
  211. }
  212. return ERROR_OK;
  213. }
  214. static int esirisc_restore_interrupts(struct target *target)
  215. {
  216. struct esirisc_common *esirisc = target_to_esirisc(target);
  217. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  218. LOG_DEBUG("-");
  219. int retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC,
  220. esirisc->etc_save);
  221. if (retval != ERROR_OK) {
  222. LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
  223. return retval;
  224. }
  225. return ERROR_OK;
  226. }
  227. #if 0
  228. static int esirisc_save_hwdc(struct target *target)
  229. {
  230. struct esirisc_common *esirisc = target_to_esirisc(target);
  231. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  232. LOG_DEBUG("-");
  233. int retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_HWDC,
  234. &esirisc->hwdc_save);
  235. if (retval != ERROR_OK) {
  236. LOG_ERROR("%s: failed to read Thread CSR: HWDC", target_name(target));
  237. return retval;
  238. }
  239. return ERROR_OK;
  240. }
  241. #endif
  242. static int esirisc_restore_hwdc(struct target *target)
  243. {
  244. struct esirisc_common *esirisc = target_to_esirisc(target);
  245. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  246. LOG_DEBUG("-");
  247. int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_HWDC,
  248. esirisc->hwdc_save);
  249. if (retval != ERROR_OK) {
  250. LOG_ERROR("%s: failed to write Debug CSR: HWDC", target_name(target));
  251. return retval;
  252. }
  253. return ERROR_OK;
  254. }
  255. static int esirisc_save_context(struct target *target)
  256. {
  257. struct esirisc_common *esirisc = target_to_esirisc(target);
  258. LOG_DEBUG("-");
  259. for (unsigned i = 0; i < esirisc->reg_cache->num_regs; ++i) {
  260. struct reg *reg = esirisc->reg_cache->reg_list + i;
  261. struct esirisc_reg *reg_info = reg->arch_info;
  262. if (reg->exist && !reg->valid)
  263. reg_info->read(reg);
  264. }
  265. return ERROR_OK;
  266. }
  267. static int esirisc_restore_context(struct target *target)
  268. {
  269. struct esirisc_common *esirisc = target_to_esirisc(target);
  270. LOG_DEBUG("-");
  271. for (unsigned i = 0; i < esirisc->reg_cache->num_regs; ++i) {
  272. struct reg *reg = esirisc->reg_cache->reg_list + i;
  273. struct esirisc_reg *reg_info = reg->arch_info;
  274. if (reg->exist && reg->dirty)
  275. reg_info->write(reg);
  276. }
  277. return ERROR_OK;
  278. }
  279. static int esirisc_flush_caches(struct target *target)
  280. {
  281. struct esirisc_common *esirisc = target_to_esirisc(target);
  282. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  283. LOG_DEBUG("-");
  284. if (target->state != TARGET_HALTED)
  285. return ERROR_TARGET_NOT_HALTED;
  286. int retval = esirisc_jtag_flush_caches(jtag_info);
  287. if (retval != ERROR_OK) {
  288. LOG_ERROR("%s: failed to flush caches", target_name(target));
  289. return retval;
  290. }
  291. return ERROR_OK;
  292. }
  293. static int esirisc_wait_debug_active(struct esirisc_common *esirisc, int ms)
  294. {
  295. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  296. int64_t t;
  297. LOG_DEBUG("-");
  298. t = timeval_ms();
  299. for (;;) {
  300. int retval = esirisc_jtag_enable_debug(jtag_info);
  301. if (retval == ERROR_OK && esirisc_jtag_is_debug_active(jtag_info))
  302. return retval;
  303. if ((timeval_ms() - t) > ms)
  304. return ERROR_TARGET_TIMEOUT;
  305. alive_sleep(100);
  306. }
  307. }
  308. static int esirisc_read_memory(struct target *target, target_addr_t address,
  309. uint32_t size, uint32_t count, uint8_t *buffer)
  310. {
  311. struct esirisc_common *esirisc = target_to_esirisc(target);
  312. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  313. int retval;
  314. LOG_DEBUG("-");
  315. int num_bits = 8 * size;
  316. for (uint32_t i = 0; i < count; ++i) {
  317. union esirisc_memory value;
  318. void *value_p;
  319. switch (size) {
  320. case sizeof(value.word):
  321. value_p = &value.word;
  322. retval = esirisc_jtag_read_word(jtag_info, address, value_p);
  323. break;
  324. case sizeof(value.hword):
  325. value_p = &value.hword;
  326. retval = esirisc_jtag_read_hword(jtag_info, address, value_p);
  327. break;
  328. case sizeof(value.byte):
  329. value_p = &value.byte;
  330. retval = esirisc_jtag_read_byte(jtag_info, address, value_p);
  331. break;
  332. default:
  333. LOG_ERROR("%s: unsupported size: %" PRIu32, target_name(target), size);
  334. return ERROR_FAIL;
  335. }
  336. if (retval != ERROR_OK) {
  337. LOG_ERROR("%s: failed to read address: 0x%" TARGET_PRIxADDR, target_name(target),
  338. address);
  339. return retval;
  340. }
  341. buf_cpy(value_p, buffer, num_bits);
  342. address += size;
  343. buffer += size;
  344. }
  345. return ERROR_OK;
  346. }
  347. static int esirisc_write_memory(struct target *target, target_addr_t address,
  348. uint32_t size, uint32_t count, const uint8_t *buffer)
  349. {
  350. struct esirisc_common *esirisc = target_to_esirisc(target);
  351. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  352. int retval;
  353. LOG_DEBUG("-");
  354. int num_bits = 8 * size;
  355. for (uint32_t i = 0; i < count; ++i) {
  356. union esirisc_memory value;
  357. switch (size) {
  358. case sizeof(value.word):
  359. value.word = buf_get_u32(buffer, 0, num_bits);
  360. retval = esirisc_jtag_write_word(jtag_info, address, value.word);
  361. break;
  362. case sizeof(value.hword):
  363. value.hword = buf_get_u32(buffer, 0, num_bits);
  364. retval = esirisc_jtag_write_hword(jtag_info, address, value.hword);
  365. break;
  366. case sizeof(value.byte):
  367. value.byte = buf_get_u32(buffer, 0, num_bits);
  368. retval = esirisc_jtag_write_byte(jtag_info, address, value.byte);
  369. break;
  370. default:
  371. LOG_ERROR("%s: unsupported size: %" PRIu32, target_name(target), size);
  372. return ERROR_FAIL;
  373. }
  374. if (retval != ERROR_OK) {
  375. LOG_ERROR("%s: failed to write address: 0x%" TARGET_PRIxADDR, target_name(target),
  376. address);
  377. return retval;
  378. }
  379. address += size;
  380. buffer += size;
  381. }
  382. return ERROR_OK;
  383. }
  384. static int esirisc_checksum_memory(struct target *target, target_addr_t address,
  385. uint32_t count, uint32_t *checksum)
  386. {
  387. return ERROR_FAIL; /* not supported */
  388. }
  389. static int esirisc_next_breakpoint(struct target *target)
  390. {
  391. struct esirisc_common *esirisc = target_to_esirisc(target);
  392. struct breakpoint **breakpoints_p = esirisc->breakpoints_p;
  393. struct breakpoint **breakpoints_e = breakpoints_p + esirisc->num_breakpoints;
  394. LOG_DEBUG("-");
  395. for (int bp_index = 0; breakpoints_p < breakpoints_e; ++breakpoints_p, ++bp_index)
  396. if (!*breakpoints_p)
  397. return bp_index;
  398. return -1;
  399. }
  400. static int esirisc_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
  401. {
  402. struct esirisc_common *esirisc = target_to_esirisc(target);
  403. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  404. int bp_index;
  405. uint32_t ibc;
  406. int retval;
  407. LOG_DEBUG("-");
  408. /*
  409. * The default linker scripts provided by the eSi-RISC toolchain do
  410. * not specify attributes on memory regions, which results in
  411. * incorrect application of software breakpoints by GDB. Targets
  412. * must be configured with `gdb_breakpoint_override hard` as
  413. * software breakpoints are not supported.
  414. */
  415. if (breakpoint->type != BKPT_HARD)
  416. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  417. bp_index = esirisc_next_breakpoint(target);
  418. if (bp_index < 0) {
  419. LOG_ERROR("%s: out of hardware breakpoints", target_name(target));
  420. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  421. }
  422. breakpoint->set = bp_index + 1;
  423. esirisc->breakpoints_p[bp_index] = breakpoint;
  424. /* specify instruction breakpoint address */
  425. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBA_N + bp_index,
  426. breakpoint->address);
  427. if (retval != ERROR_OK) {
  428. LOG_ERROR("%s: failed to write Debug CSR: IBA", target_name(target));
  429. return retval;
  430. }
  431. /* enable instruction breakpoint */
  432. retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, &ibc);
  433. if (retval != ERROR_OK) {
  434. LOG_ERROR("%s: failed to read Debug CSR: IBC", target_name(target));
  435. return retval;
  436. }
  437. ibc |= (1 << bp_index); /* IBC.In */
  438. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, ibc);
  439. if (retval != ERROR_OK) {
  440. LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
  441. return retval;
  442. }
  443. return ERROR_OK;
  444. }
  445. static int esirisc_add_breakpoints(struct target *target)
  446. {
  447. struct breakpoint *breakpoint = target->breakpoints;
  448. LOG_DEBUG("-");
  449. while (breakpoint) {
  450. if (breakpoint->set == 0)
  451. esirisc_add_breakpoint(target, breakpoint);
  452. breakpoint = breakpoint->next;
  453. }
  454. return ERROR_OK;
  455. }
  456. static int esirisc_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
  457. {
  458. struct esirisc_common *esirisc = target_to_esirisc(target);
  459. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  460. int bp_index = breakpoint->set - 1;
  461. uint32_t ibc;
  462. int retval;
  463. LOG_DEBUG("-");
  464. /* disable instruction breakpoint */
  465. retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, &ibc);
  466. if (retval != ERROR_OK) {
  467. LOG_ERROR("%s: failed to read Debug CSR: IBC", target_name(target));
  468. return retval;
  469. }
  470. ibc &= ~(1 << bp_index); /* IBC.In */
  471. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, ibc);
  472. if (retval != ERROR_OK) {
  473. LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
  474. return retval;
  475. }
  476. esirisc->breakpoints_p[bp_index] = NULL;
  477. breakpoint->set = 0;
  478. return ERROR_OK;
  479. }
  480. static int esirisc_remove_breakpoints(struct target *target)
  481. {
  482. struct esirisc_common *esirisc = target_to_esirisc(target);
  483. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  484. LOG_DEBUG("-");
  485. /* clear instruction breakpoints */
  486. int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, 0);
  487. if (retval != ERROR_OK) {
  488. LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
  489. return retval;
  490. }
  491. memset(esirisc->breakpoints_p, 0, sizeof(esirisc->breakpoints_p));
  492. return ERROR_OK;
  493. }
  494. static int esirisc_next_watchpoint(struct target *target)
  495. {
  496. struct esirisc_common *esirisc = target_to_esirisc(target);
  497. struct watchpoint **watchpoints_p = esirisc->watchpoints_p;
  498. struct watchpoint **watchpoints_e = watchpoints_p + esirisc->num_watchpoints;
  499. LOG_DEBUG("-");
  500. for (int wp_index = 0; watchpoints_p < watchpoints_e; ++watchpoints_p, ++wp_index)
  501. if (!*watchpoints_p)
  502. return wp_index;
  503. return -1;
  504. }
  505. static int esirisc_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
  506. {
  507. struct esirisc_common *esirisc = target_to_esirisc(target);
  508. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  509. int wp_index;
  510. uint32_t dbs, dbc;
  511. int retval;
  512. LOG_DEBUG("-");
  513. wp_index = esirisc_next_watchpoint(target);
  514. if (wp_index < 0) {
  515. LOG_ERROR("%s: out of hardware watchpoints", target_name(target));
  516. return ERROR_FAIL;
  517. }
  518. watchpoint->set = wp_index + 1;
  519. esirisc->watchpoints_p[wp_index] = watchpoint;
  520. /* specify data breakpoint address */
  521. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBA_N + wp_index,
  522. watchpoint->address);
  523. if (retval != ERROR_OK) {
  524. LOG_ERROR("%s: failed to write Debug CSR: DBA", target_name(target));
  525. return retval;
  526. }
  527. /* specify data breakpoint size */
  528. retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBS, &dbs);
  529. if (retval != ERROR_OK) {
  530. LOG_ERROR("%s: failed to read Debug CSR: DBS", target_name(target));
  531. return retval;
  532. }
  533. uint32_t sn;
  534. switch (watchpoint->length) {
  535. case sizeof(uint64_t):
  536. sn = 0x3;
  537. break;
  538. case sizeof(uint32_t):
  539. sn = 0x2;
  540. break;
  541. case sizeof(uint16_t):
  542. sn = 0x1;
  543. break;
  544. case sizeof(uint8_t):
  545. sn = 0x0;
  546. break;
  547. default:
  548. LOG_ERROR("%s: unsupported length: %" PRIu32, target_name(target),
  549. watchpoint->length);
  550. return ERROR_FAIL;
  551. }
  552. dbs |= (sn << (2 * wp_index)); /* DBS.Sn */
  553. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBS, dbs);
  554. if (retval != ERROR_OK) {
  555. LOG_ERROR("%s: failed to write Debug CSR: DBS", target_name(target));
  556. return retval;
  557. }
  558. /* enable data breakpoint */
  559. retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
  560. if (retval != ERROR_OK) {
  561. LOG_ERROR("%s: failed to read Debug CSR: DBC", target_name(target));
  562. return retval;
  563. }
  564. uint32_t dn;
  565. switch (watchpoint->rw) {
  566. case WPT_READ:
  567. dn = 0x1;
  568. break;
  569. case WPT_WRITE:
  570. dn = 0x2;
  571. break;
  572. case WPT_ACCESS:
  573. dn = 0x3;
  574. break;
  575. default:
  576. LOG_ERROR("%s: unsupported rw: %" PRId32, target_name(target),
  577. watchpoint->rw);
  578. return ERROR_FAIL;
  579. }
  580. dbc |= (dn << (2 * wp_index)); /* DBC.Dn */
  581. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
  582. if (retval != ERROR_OK) {
  583. LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
  584. return retval;
  585. }
  586. return ERROR_OK;
  587. }
  588. static int esirisc_add_watchpoints(struct target *target)
  589. {
  590. struct watchpoint *watchpoint = target->watchpoints;
  591. LOG_DEBUG("-");
  592. while (watchpoint) {
  593. if (watchpoint->set == 0)
  594. esirisc_add_watchpoint(target, watchpoint);
  595. watchpoint = watchpoint->next;
  596. }
  597. return ERROR_OK;
  598. }
  599. static int esirisc_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
  600. {
  601. struct esirisc_common *esirisc = target_to_esirisc(target);
  602. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  603. int wp_index = watchpoint->set - 1;
  604. uint32_t dbc;
  605. int retval;
  606. LOG_DEBUG("-");
  607. /* disable data breakpoint */
  608. retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
  609. if (retval != ERROR_OK) {
  610. LOG_ERROR("%s: failed to read Debug CSR: DBC", target_name(target));
  611. return retval;
  612. }
  613. dbc &= ~(0x3 << (2 * wp_index)); /* DBC.Dn */
  614. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
  615. if (retval != ERROR_OK) {
  616. LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
  617. return retval;
  618. }
  619. esirisc->watchpoints_p[wp_index] = NULL;
  620. watchpoint->set = 0;
  621. return ERROR_OK;
  622. }
  623. static int esirisc_remove_watchpoints(struct target *target)
  624. {
  625. struct esirisc_common *esirisc = target_to_esirisc(target);
  626. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  627. LOG_DEBUG("-");
  628. /* clear data breakpoints */
  629. int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, 0);
  630. if (retval != ERROR_OK) {
  631. LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
  632. return retval;
  633. }
  634. memset(esirisc->watchpoints_p, 0, sizeof(esirisc->watchpoints_p));
  635. return ERROR_OK;
  636. }
  637. static int esirisc_halt(struct target *target)
  638. {
  639. struct esirisc_common *esirisc = target_to_esirisc(target);
  640. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  641. LOG_DEBUG("-");
  642. if (target->state == TARGET_HALTED)
  643. return ERROR_OK;
  644. int retval = esirisc_jtag_break(jtag_info);
  645. if (retval != ERROR_OK) {
  646. LOG_ERROR("%s: failed to halt target", target_name(target));
  647. return retval;
  648. }
  649. target->debug_reason = DBG_REASON_DBGRQ;
  650. return ERROR_OK;
  651. }
  652. static int esirisc_disable_step(struct target *target)
  653. {
  654. struct esirisc_common *esirisc = target_to_esirisc(target);
  655. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  656. uint32_t dc;
  657. int retval;
  658. LOG_DEBUG("-");
  659. retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
  660. if (retval != ERROR_OK) {
  661. LOG_ERROR("%s: failed to read Debug CSR: DC", target_name(target));
  662. return retval;
  663. }
  664. dc &= ~(1<<0); /* DC.S */
  665. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
  666. if (retval != ERROR_OK) {
  667. LOG_ERROR("%s: failed to write Debug CSR: DC", target_name(target));
  668. return retval;
  669. }
  670. return ERROR_OK;
  671. }
  672. static int esirisc_enable_step(struct target *target)
  673. {
  674. struct esirisc_common *esirisc = target_to_esirisc(target);
  675. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  676. uint32_t dc;
  677. int retval;
  678. LOG_DEBUG("-");
  679. retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
  680. if (retval != ERROR_OK) {
  681. LOG_ERROR("%s: failed to read Debug CSR: DC", target_name(target));
  682. return retval;
  683. }
  684. dc |= (1<<0); /* DC.S */
  685. retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
  686. if (retval != ERROR_OK) {
  687. LOG_ERROR("%s: failed to write Debug CSR: DC", target_name(target));
  688. return retval;
  689. }
  690. return ERROR_OK;
  691. }
  692. static int esirisc_resume_or_step(struct target *target, int current, target_addr_t address,
  693. int handle_breakpoints, int debug_execution, bool step)
  694. {
  695. struct esirisc_common *esirisc = target_to_esirisc(target);
  696. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  697. struct breakpoint *breakpoint = NULL;
  698. int retval;
  699. LOG_DEBUG("-");
  700. if (target->state != TARGET_HALTED)
  701. return ERROR_TARGET_NOT_HALTED;
  702. if (!debug_execution) {
  703. target_free_all_working_areas(target);
  704. esirisc_add_breakpoints(target);
  705. esirisc_add_watchpoints(target);
  706. }
  707. if (current)
  708. address = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
  709. else {
  710. buf_set_u32(esirisc->epc->value, 0, esirisc->epc->size, address);
  711. esirisc->epc->dirty = true;
  712. esirisc->epc->valid = true;
  713. }
  714. esirisc_restore_context(target);
  715. if (esirisc_has_cache(esirisc))
  716. esirisc_flush_caches(target);
  717. if (handle_breakpoints) {
  718. breakpoint = breakpoint_find(target, address);
  719. if (breakpoint)
  720. esirisc_remove_breakpoint(target, breakpoint);
  721. }
  722. if (step) {
  723. esirisc_disable_interrupts(target);
  724. esirisc_enable_step(target);
  725. target->debug_reason = DBG_REASON_SINGLESTEP;
  726. } else {
  727. esirisc_disable_step(target);
  728. esirisc_restore_interrupts(target);
  729. target->debug_reason = DBG_REASON_NOTHALTED;
  730. }
  731. esirisc_restore_hwdc(target);
  732. retval = esirisc_jtag_continue(jtag_info);
  733. if (retval != ERROR_OK) {
  734. LOG_ERROR("%s: failed to resume target", target_name(target));
  735. return retval;
  736. }
  737. register_cache_invalidate(esirisc->reg_cache);
  738. if (!debug_execution) {
  739. target->state = TARGET_RUNNING;
  740. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  741. } else {
  742. target->state = TARGET_DEBUG_RUNNING;
  743. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  744. }
  745. return ERROR_OK;
  746. }
  747. static int esirisc_resume(struct target *target, int current, target_addr_t address,
  748. int handle_breakpoints, int debug_execution)
  749. {
  750. LOG_DEBUG("-");
  751. return esirisc_resume_or_step(target, current, address,
  752. handle_breakpoints, debug_execution, false);
  753. }
  754. static int esirisc_step(struct target *target, int current, target_addr_t address,
  755. int handle_breakpoints)
  756. {
  757. LOG_DEBUG("-");
  758. return esirisc_resume_or_step(target, current, address,
  759. handle_breakpoints, 0, true);
  760. }
  761. static int esirisc_debug_step(struct target *target)
  762. {
  763. struct esirisc_common *esirisc = target_to_esirisc(target);
  764. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  765. int retval;
  766. LOG_DEBUG("-");
  767. esirisc_disable_interrupts(target);
  768. esirisc_enable_step(target);
  769. retval = esirisc_jtag_continue(jtag_info);
  770. if (retval != ERROR_OK) {
  771. LOG_ERROR("%s: failed to resume target", target_name(target));
  772. return retval;
  773. }
  774. retval = esirisc_wait_debug_active(esirisc, STEP_TIMEOUT);
  775. if (retval != ERROR_OK) {
  776. LOG_ERROR("%s: step timed out", target_name(target));
  777. return retval;
  778. }
  779. esirisc_disable_step(target);
  780. esirisc_restore_interrupts(target);
  781. return ERROR_OK;
  782. }
  783. static int esirisc_debug_reset(struct target *target)
  784. {
  785. struct esirisc_common *esirisc = target_to_esirisc(target);
  786. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  787. int retval;
  788. LOG_DEBUG("-");
  789. retval = esirisc_jtag_assert_reset(jtag_info);
  790. if (retval != ERROR_OK) {
  791. LOG_ERROR("%s: failed to assert reset", target_name(target));
  792. return retval;
  793. }
  794. retval = esirisc_jtag_deassert_reset(jtag_info);
  795. if (retval != ERROR_OK) {
  796. LOG_ERROR("%s: failed to deassert reset", target_name(target));
  797. return retval;
  798. }
  799. retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
  800. if (retval != ERROR_OK) {
  801. LOG_ERROR("%s: reset timed out", target_name(target));
  802. return retval;
  803. }
  804. return ERROR_OK;
  805. }
  806. static int esirisc_debug_enable(struct target *target)
  807. {
  808. struct esirisc_common *esirisc = target_to_esirisc(target);
  809. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  810. int retval;
  811. LOG_DEBUG("-");
  812. retval = esirisc_jtag_enable_debug(jtag_info);
  813. if (retval != ERROR_OK) {
  814. LOG_ERROR("%s: failed to enable debug mode", target_name(target));
  815. return retval;
  816. }
  817. /*
  818. * The debug clock is inactive until the first command is sent.
  819. * If the target is stopped, we must first issue a reset before
  820. * attempting further communication. This also handles unpowered
  821. * targets, which will respond with all ones and appear active.
  822. */
  823. if (esirisc_jtag_is_stopped(jtag_info)) {
  824. LOG_INFO("%s: debug clock inactive; attempting debug reset", target_name(target));
  825. retval = esirisc_debug_reset(target);
  826. if (retval != ERROR_OK)
  827. return retval;
  828. if (esirisc_jtag_is_stopped(jtag_info)) {
  829. LOG_ERROR("%s: target unresponsive; giving up", target_name(target));
  830. return ERROR_FAIL;
  831. }
  832. }
  833. return ERROR_OK;
  834. }
  835. static int esirisc_debug_entry(struct target *target)
  836. {
  837. struct esirisc_common *esirisc = target_to_esirisc(target);
  838. struct breakpoint *breakpoint;
  839. LOG_DEBUG("-");
  840. esirisc_save_context(target);
  841. if (esirisc_has_cache(esirisc))
  842. esirisc_flush_caches(target);
  843. if (target->debug_reason != DBG_REASON_SINGLESTEP) {
  844. esirisc_save_interrupts(target);
  845. uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
  846. switch (eid) {
  847. /*
  848. * InstBreakpoint exceptions are also raised when a core is
  849. * halted for debugging. The following is required to
  850. * determine if a breakpoint was encountered.
  851. */
  852. case EID_INST_BREAKPOINT:
  853. breakpoint = breakpoint_find(target,
  854. buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size));
  855. target->debug_reason = (breakpoint) ?
  856. DBG_REASON_BREAKPOINT : DBG_REASON_DBGRQ;
  857. break;
  858. /*
  859. * eSi-RISC treats watchpoints similarly to breakpoints,
  860. * however GDB will not request to step over the current
  861. * instruction when a watchpoint fires. The following is
  862. * required to resume the target.
  863. */
  864. case EID_DATA_BREAKPOINT:
  865. esirisc_remove_watchpoints(target);
  866. esirisc_debug_step(target);
  867. esirisc_add_watchpoints(target);
  868. target->debug_reason = DBG_REASON_WATCHPOINT;
  869. break;
  870. default:
  871. target->debug_reason = DBG_REASON_DBGRQ;
  872. }
  873. }
  874. return ERROR_OK;
  875. }
  876. static int esirisc_poll(struct target *target)
  877. {
  878. struct esirisc_common *esirisc = target_to_esirisc(target);
  879. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  880. int retval;
  881. retval = esirisc_jtag_enable_debug(jtag_info);
  882. if (retval != ERROR_OK) {
  883. LOG_ERROR("%s: failed to poll target", target_name(target));
  884. return retval;
  885. }
  886. if (esirisc_jtag_is_stopped(jtag_info)) {
  887. LOG_ERROR("%s: target has stopped; reset required", target_name(target));
  888. target->state = TARGET_UNKNOWN;
  889. return ERROR_TARGET_FAILURE;
  890. }
  891. if (esirisc_jtag_is_debug_active(jtag_info)) {
  892. if (target->state == TARGET_RUNNING || target->state == TARGET_RESET) {
  893. target->state = TARGET_HALTED;
  894. retval = esirisc_debug_entry(target);
  895. if (retval != ERROR_OK)
  896. return retval;
  897. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  898. }
  899. } else if (target->state == TARGET_HALTED || target->state == TARGET_RESET) {
  900. target->state = TARGET_RUNNING;
  901. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  902. }
  903. return ERROR_OK;
  904. }
  905. static int esirisc_assert_reset(struct target *target)
  906. {
  907. struct esirisc_common *esirisc = target_to_esirisc(target);
  908. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  909. int retval;
  910. LOG_DEBUG("-");
  911. if (jtag_get_reset_config() & RESET_HAS_SRST) {
  912. jtag_add_reset(1, 1);
  913. if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) == 0)
  914. jtag_add_reset(0, 1);
  915. } else {
  916. esirisc_remove_breakpoints(target);
  917. esirisc_remove_watchpoints(target);
  918. retval = esirisc_jtag_assert_reset(jtag_info);
  919. if (retval != ERROR_OK) {
  920. LOG_ERROR("%s: failed to assert reset", target_name(target));
  921. return retval;
  922. }
  923. }
  924. target->state = TARGET_RESET;
  925. register_cache_invalidate(esirisc->reg_cache);
  926. return ERROR_OK;
  927. }
  928. static int esirisc_reset_entry(struct target *target)
  929. {
  930. struct esirisc_common *esirisc = target_to_esirisc(target);
  931. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  932. uint32_t eta, epc;
  933. int retval;
  934. LOG_DEBUG("-");
  935. /* read exception table address */
  936. retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETA, &eta);
  937. if (retval != ERROR_OK) {
  938. LOG_ERROR("%s: failed to read Thread CSR: ETA", target_name(target));
  939. return retval;
  940. }
  941. /* read reset entry point */
  942. retval = esirisc_jtag_read_word(jtag_info, eta + ENTRY_RESET, &epc);
  943. if (retval != ERROR_OK) {
  944. LOG_ERROR("%s: failed to read address: 0x%" TARGET_PRIxADDR, target_name(target),
  945. (target_addr_t)epc);
  946. return retval;
  947. }
  948. /* write reset entry point */
  949. retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_EPC, epc);
  950. if (retval != ERROR_OK) {
  951. LOG_ERROR("%s: failed to write Thread CSR: EPC", target_name(target));
  952. return retval;
  953. }
  954. return ERROR_OK;
  955. }
  956. static int esirisc_deassert_reset(struct target *target)
  957. {
  958. struct esirisc_common *esirisc = target_to_esirisc(target);
  959. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  960. int retval;
  961. LOG_DEBUG("-");
  962. if (jtag_get_reset_config() & RESET_HAS_SRST) {
  963. jtag_add_reset(0, 0);
  964. retval = esirisc_debug_enable(target);
  965. if (retval != ERROR_OK)
  966. return retval;
  967. retval = esirisc_debug_reset(target);
  968. if (retval != ERROR_OK)
  969. return retval;
  970. } else {
  971. retval = esirisc_jtag_deassert_reset(jtag_info);
  972. if (retval != ERROR_OK) {
  973. LOG_ERROR("%s: failed to deassert reset", target_name(target));
  974. return retval;
  975. }
  976. }
  977. retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
  978. if (retval != ERROR_OK) {
  979. LOG_ERROR("%s: reset timed out", target_name(target));
  980. return retval;
  981. }
  982. retval = esirisc_reset_entry(target);
  983. if (retval != ERROR_OK)
  984. return retval;
  985. esirisc_add_breakpoints(target);
  986. esirisc_add_watchpoints(target);
  987. esirisc_restore_hwdc(target);
  988. if (!target->reset_halt) {
  989. retval = esirisc_jtag_continue(jtag_info);
  990. if (retval != ERROR_OK) {
  991. LOG_ERROR("%s: failed to resume target", target_name(target));
  992. return retval;
  993. }
  994. }
  995. return ERROR_OK;
  996. }
  997. static int esirisc_arch_state(struct target *target)
  998. {
  999. struct esirisc_common *esirisc = target_to_esirisc(target);
  1000. uint32_t epc = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
  1001. uint32_t ecas = buf_get_u32(esirisc->ecas->value, 0, esirisc->ecas->size);
  1002. uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
  1003. uint32_t ed = buf_get_u32(esirisc->ed->value, 0, esirisc->ed->size);
  1004. LOG_USER("target halted due to %s, exception: %s\n"
  1005. "EPC: 0x%" PRIx32 ", ECAS: 0x%" PRIx32 ", EID: 0x%" PRIx32 ", ED: 0x%" PRIx32,
  1006. debug_reason_name(target), esirisc_exception_strings[eid], epc, ecas, eid, ed);
  1007. return ERROR_OK;
  1008. }
  1009. static const char *esirisc_get_gdb_arch(struct target *target)
  1010. {
  1011. struct esirisc_common *esirisc = target_to_esirisc(target);
  1012. LOG_DEBUG("-");
  1013. /*
  1014. * Targets with the UNIFIED_ADDRESS_SPACE option disabled employ a
  1015. * Harvard architecture. This option is not exposed in a CSR, which
  1016. * requires additional configuration to properly interact with these
  1017. * targets in GDB (also see: `esirisc cache_arch`).
  1018. */
  1019. if (!esirisc->gdb_arch && target_was_examined(target))
  1020. esirisc->gdb_arch = alloc_printf("esirisc:%d_bit_%d_reg_%s",
  1021. esirisc->num_bits, esirisc->num_regs, esirisc_cache_arch_name(esirisc));
  1022. return esirisc->gdb_arch;
  1023. }
  1024. static int esirisc_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
  1025. int *reg_list_size, enum target_register_class reg_class)
  1026. {
  1027. struct esirisc_common *esirisc = target_to_esirisc(target);
  1028. LOG_DEBUG("-");
  1029. *reg_list_size = ESIRISC_NUM_REGS;
  1030. *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
  1031. if (!*reg_list)
  1032. return ERROR_FAIL;
  1033. if (reg_class == REG_CLASS_ALL)
  1034. for (int i = 0; i < *reg_list_size; ++i)
  1035. (*reg_list)[i] = esirisc->reg_cache->reg_list + i;
  1036. else {
  1037. for (int i = 0; i < esirisc->num_regs; ++i)
  1038. (*reg_list)[i] = esirisc->reg_cache->reg_list + i;
  1039. (*reg_list)[ESIRISC_PC] = esirisc->reg_cache->reg_list + ESIRISC_PC;
  1040. (*reg_list)[ESIRISC_CAS] = esirisc->reg_cache->reg_list + ESIRISC_CAS;
  1041. }
  1042. return ERROR_OK;
  1043. }
  1044. static int esirisc_read_reg(struct reg *reg)
  1045. {
  1046. struct esirisc_reg *reg_info = reg->arch_info;
  1047. struct esirisc_common *esirisc = reg_info->esirisc;
  1048. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  1049. struct target *target = esirisc->target;
  1050. uint32_t data;
  1051. LOG_DEBUG("-");
  1052. int retval = esirisc_jtag_read_reg(jtag_info, reg->number, &data);
  1053. if (retval != ERROR_OK) {
  1054. LOG_ERROR("%s: failed to read register: %s", target_name(target), reg->name);
  1055. return retval;
  1056. }
  1057. buf_set_u32(reg->value, 0, reg->size, data);
  1058. reg->dirty = false;
  1059. reg->valid = true;
  1060. return ERROR_OK;
  1061. }
  1062. static int esirisc_write_reg(struct reg *reg)
  1063. {
  1064. struct esirisc_reg *reg_info = reg->arch_info;
  1065. struct esirisc_common *esirisc = reg_info->esirisc;
  1066. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  1067. struct target *target = esirisc->target;
  1068. uint32_t data = buf_get_u32(reg->value, 0, reg->size);
  1069. LOG_DEBUG("-");
  1070. int retval = esirisc_jtag_write_reg(jtag_info, reg->number, data);
  1071. if (retval != ERROR_OK) {
  1072. LOG_ERROR("%s: failed to write register: %s", target_name(target), reg->name);
  1073. return retval;
  1074. }
  1075. reg->dirty = false;
  1076. reg->valid = true;
  1077. return ERROR_OK;
  1078. }
  1079. static int esirisc_read_csr(struct reg *reg)
  1080. {
  1081. struct esirisc_reg *reg_info = reg->arch_info;
  1082. struct esirisc_common *esirisc = reg_info->esirisc;
  1083. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  1084. struct target *target = esirisc->target;
  1085. uint32_t data;
  1086. LOG_DEBUG("-");
  1087. int retval = esirisc_jtag_read_csr(jtag_info, reg_info->bank, reg_info->csr, &data);
  1088. if (retval != ERROR_OK) {
  1089. LOG_ERROR("%s: failed to read CSR: %s", target_name(target), reg->name);
  1090. return retval;
  1091. }
  1092. buf_set_u32(reg->value, 0, reg->size, data);
  1093. reg->dirty = false;
  1094. reg->valid = true;
  1095. return ERROR_OK;
  1096. }
  1097. static int esirisc_write_csr(struct reg *reg)
  1098. {
  1099. struct esirisc_reg *reg_info = reg->arch_info;
  1100. struct esirisc_common *esirisc = reg_info->esirisc;
  1101. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  1102. struct target *target = esirisc->target;
  1103. uint32_t data = buf_get_u32(reg->value, 0, reg->size);
  1104. LOG_DEBUG("-");
  1105. int retval = esirisc_jtag_write_csr(jtag_info, reg_info->bank, reg_info->csr, data);
  1106. if (retval != ERROR_OK) {
  1107. LOG_ERROR("%s: failed to write CSR: %s", target_name(target), reg->name);
  1108. return retval;
  1109. }
  1110. reg->dirty = false;
  1111. reg->valid = true;
  1112. return ERROR_OK;
  1113. }
  1114. static int esirisc_get_reg(struct reg *reg)
  1115. {
  1116. struct esirisc_reg *reg_info = reg->arch_info;
  1117. struct esirisc_common *esirisc = reg_info->esirisc;
  1118. struct target *target = esirisc->target;
  1119. LOG_DEBUG("-");
  1120. if (target->state != TARGET_HALTED)
  1121. return ERROR_TARGET_NOT_HALTED;
  1122. return reg_info->read(reg);
  1123. }
  1124. static int esirisc_set_reg(struct reg *reg, uint8_t *buf)
  1125. {
  1126. struct esirisc_reg *reg_info = reg->arch_info;
  1127. struct esirisc_common *esirisc = reg_info->esirisc;
  1128. struct target *target = esirisc->target;
  1129. uint32_t value = buf_get_u32(buf, 0, reg->size);
  1130. LOG_DEBUG("-");
  1131. if (target->state != TARGET_HALTED)
  1132. return ERROR_TARGET_NOT_HALTED;
  1133. buf_set_u32(reg->value, 0, reg->size, value);
  1134. reg->dirty = true;
  1135. reg->valid = true;
  1136. return ERROR_OK;
  1137. }
  1138. static const struct reg_arch_type esirisc_reg_type = {
  1139. .get = esirisc_get_reg,
  1140. .set = esirisc_set_reg,
  1141. };
  1142. static struct reg_cache *esirisc_build_reg_cache(struct target *target)
  1143. {
  1144. struct esirisc_common *esirisc = target_to_esirisc(target);
  1145. struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
  1146. struct reg_cache *cache = malloc(sizeof(struct reg_cache));
  1147. struct reg *reg_list = calloc(ESIRISC_NUM_REGS, sizeof(struct reg));
  1148. LOG_DEBUG("-");
  1149. cache->name = "eSi-RISC registers";
  1150. cache->next = NULL;
  1151. cache->reg_list = reg_list;
  1152. cache->num_regs = ESIRISC_NUM_REGS;
  1153. (*cache_p) = cache;
  1154. esirisc->reg_cache = cache;
  1155. esirisc->epc = reg_list + ESIRISC_EPC;
  1156. esirisc->ecas = reg_list + ESIRISC_ECAS;
  1157. esirisc->eid = reg_list + ESIRISC_EID;
  1158. esirisc->ed = reg_list + ESIRISC_ED;
  1159. for (int i = 0; i < esirisc->num_regs; ++i) {
  1160. struct reg *reg = reg_list + esirisc_regs[i].number;
  1161. struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));
  1162. reg->name = esirisc_regs[i].name;
  1163. reg->number = esirisc_regs[i].number;
  1164. reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
  1165. reg->size = esirisc->num_bits;
  1166. reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
  1167. reg->reg_data_type->type = esirisc_regs[i].type;
  1168. reg->group = esirisc_regs[i].group;
  1169. reg_info->esirisc = esirisc;
  1170. reg_info->read = esirisc_read_reg;
  1171. reg_info->write = esirisc_write_reg;
  1172. reg->arch_info = reg_info;
  1173. reg->type = &esirisc_reg_type;
  1174. reg->exist = true;
  1175. }
  1176. for (size_t i = 0; i < ARRAY_SIZE(esirisc_csrs); ++i) {
  1177. struct reg *reg = reg_list + esirisc_csrs[i].number;
  1178. struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));
  1179. reg->name = esirisc_csrs[i].name;
  1180. reg->number = esirisc_csrs[i].number;
  1181. reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
  1182. reg->size = esirisc->num_bits;
  1183. reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
  1184. reg->reg_data_type->type = esirisc_csrs[i].type;
  1185. reg->group = esirisc_csrs[i].group;
  1186. reg_info->esirisc = esirisc;
  1187. reg_info->bank = esirisc_csrs[i].bank;
  1188. reg_info->csr = esirisc_csrs[i].csr;
  1189. reg_info->read = esirisc_read_csr;
  1190. reg_info->write = esirisc_write_csr;
  1191. reg->arch_info = reg_info;
  1192. reg->type = &esirisc_reg_type;
  1193. reg->exist = true;
  1194. }
  1195. return cache;
  1196. }
  1197. static int esirisc_identify(struct target *target)
  1198. {
  1199. struct esirisc_common *esirisc = target_to_esirisc(target);
  1200. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  1201. uint32_t csr;
  1202. int retval;
  1203. LOG_DEBUG("-");
  1204. retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_ARCH0, &csr);
  1205. if (retval != ERROR_OK) {
  1206. LOG_ERROR("%s: failed to read Configuration CSR: ARCH0", target_name(target));
  1207. return retval;
  1208. }
  1209. esirisc->num_bits = (csr >> 0) & 0x3f; /* ARCH0.B */
  1210. esirisc->num_regs = (csr >> 10) & 0x3f; /* ARCH0.R */
  1211. retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_MEM, &csr);
  1212. if (retval != ERROR_OK) {
  1213. LOG_ERROR("%s: failed to read Configuration CSR: MEM", target_name(target));
  1214. return retval;
  1215. }
  1216. target->endianness = (csr & 1<<0) ? /* MEM.E */
  1217. TARGET_BIG_ENDIAN : TARGET_LITTLE_ENDIAN;
  1218. retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_IC, &csr);
  1219. if (retval != ERROR_OK) {
  1220. LOG_ERROR("%s: failed to read Configuration CSR: IC", target_name(target));
  1221. return retval;
  1222. }
  1223. esirisc->has_icache = !!(csr & 1<<0); /* IC.E */
  1224. retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DC, &csr);
  1225. if (retval != ERROR_OK) {
  1226. LOG_ERROR("%s: failed to read Configuration CSR: DC", target_name(target));
  1227. return retval;
  1228. }
  1229. esirisc->has_dcache = !!(csr & 1<<0); /* DC.E */
  1230. retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DBG, &csr);
  1231. if (retval != ERROR_OK) {
  1232. LOG_ERROR("%s: failed to read Configuration CSR: DBG", target_name(target));
  1233. return retval;
  1234. }
  1235. esirisc->num_breakpoints = (csr >> 7) & 0xf; /* DBG.BP */
  1236. esirisc->num_watchpoints = (csr >> 12) & 0xf; /* DBG.WP */
  1237. retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_TRACE, &csr);
  1238. if (retval != ERROR_OK) {
  1239. LOG_ERROR("%s: failed to read Configuration CSR: TRACE", target_name(target));
  1240. return retval;
  1241. }
  1242. esirisc->has_trace = !!(csr & 1<<0); /* TRACE.T */
  1243. return ERROR_OK;
  1244. }
  1245. static int esirisc_target_create(struct target *target, Jim_Interp *interp)
  1246. {
  1247. struct jtag_tap *tap = target->tap;
  1248. struct esirisc_common *esirisc;
  1249. if (!tap)
  1250. return ERROR_FAIL;
  1251. if (tap->ir_length != INSTR_LENGTH) {
  1252. LOG_ERROR("%s: invalid IR length; expected %d", target_name(target),
  1253. INSTR_LENGTH);
  1254. return ERROR_FAIL;
  1255. }
  1256. esirisc = calloc(1, sizeof(struct esirisc_common));
  1257. if (!esirisc)
  1258. return ERROR_FAIL;
  1259. esirisc->target = target;
  1260. esirisc->jtag_info.tap = tap;
  1261. target->arch_info = esirisc;
  1262. return ERROR_OK;
  1263. }
  1264. static int esirisc_init_target(struct command_context *cmd_ctx, struct target *target)
  1265. {
  1266. struct esirisc_common *esirisc = target_to_esirisc(target);
  1267. /* trap reset, error, and debug exceptions */
  1268. esirisc->hwdc_save = HWDC_R | HWDC_E | HWDC_D;
  1269. return ERROR_OK;
  1270. }
  1271. static int esirisc_examine(struct target *target)
  1272. {
  1273. struct esirisc_common *esirisc = target_to_esirisc(target);
  1274. struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
  1275. int retval;
  1276. LOG_DEBUG("-");
  1277. if (!target_was_examined(target)) {
  1278. retval = esirisc_debug_enable(target);
  1279. if (retval != ERROR_OK)
  1280. return retval;
  1281. /*
  1282. * In order to identify the target we must first halt the core.
  1283. * We quietly resume once identification has completed for those
  1284. * targets that were running when target_examine was called.
  1285. */
  1286. if (esirisc_jtag_is_debug_active(jtag_info)) {
  1287. if (target->state == TARGET_UNKNOWN)
  1288. target->debug_reason = DBG_REASON_DBGRQ;
  1289. target->state = TARGET_HALTED;
  1290. } else {
  1291. retval = esirisc_jtag_break(jtag_info);
  1292. if (retval != ERROR_OK) {
  1293. LOG_ERROR("%s: failed to halt target", target_name(target));
  1294. return retval;
  1295. }
  1296. target->state = TARGET_RUNNING;
  1297. }
  1298. retval = esirisc_identify(target);
  1299. if (retval != ERROR_OK) {
  1300. LOG_ERROR("%s: failed to identify target", target_name(target));
  1301. return retval;
  1302. }
  1303. esirisc_build_reg_cache(target);
  1304. esirisc_remove_breakpoints(target);
  1305. esirisc_remove_watchpoints(target);
  1306. esirisc_disable_step(target);
  1307. esirisc_restore_hwdc(target);
  1308. if (target->state == TARGET_HALTED)
  1309. esirisc_save_interrupts(target);
  1310. else {
  1311. retval = esirisc_jtag_continue(jtag_info);
  1312. if (retval != ERROR_OK) {
  1313. LOG_ERROR("%s: failed to resume target", target_name(target));
  1314. return retval;
  1315. }
  1316. }
  1317. target_set_examined(target);
  1318. LOG_INFO("%s: %d bit, %d registers, %s%s%s", target_name(target),
  1319. esirisc->num_bits, esirisc->num_regs,
  1320. target_endianness(target),
  1321. esirisc->has_icache ? ", icache" : "",
  1322. esirisc->has_dcache ? ", dcache" : "");
  1323. LOG_INFO("%s: hardware has %d breakpoints, %d watchpoints%s", target_name(target),
  1324. esirisc->num_breakpoints, esirisc->num_watchpoints,
  1325. esirisc->has_trace ? ", trace" : "");
  1326. }
  1327. return ERROR_OK;
  1328. }
  1329. COMMAND_HANDLER(handle_esirisc_cache_arch_command)
  1330. {
  1331. struct target *target = get_current_target(CMD_CTX);
  1332. struct esirisc_common *esirisc = target_to_esirisc(target);
  1333. if (CMD_ARGC > 0) {
  1334. if (strcmp(*CMD_ARGV, "harvard") == 0)
  1335. esirisc->cache_arch = ESIRISC_CACHE_HARVARD;
  1336. else if (strcmp(*CMD_ARGV, "von_neumann") == 0)
  1337. esirisc->cache_arch = ESIRISC_CACHE_VON_NEUMANN;
  1338. else {
  1339. LOG_ERROR("invalid cache_arch: %s", *CMD_ARGV);
  1340. return ERROR_COMMAND_SYNTAX_ERROR;
  1341. }
  1342. }
  1343. command_print(CMD, "esirisc cache_arch %s", esirisc_cache_arch_name(esirisc));
  1344. return ERROR_OK;
  1345. }
  1346. COMMAND_HANDLER(handle_esirisc_flush_caches_command)
  1347. {
  1348. struct target *target = get_current_target(CMD_CTX);
  1349. struct esirisc_common *esirisc = target_to_esirisc(target);
  1350. int retval;
  1351. if (!esirisc_has_cache(esirisc)) {
  1352. LOG_ERROR("target does not support caching");
  1353. return ERROR_FAIL;
  1354. }
  1355. retval = esirisc_flush_caches(target);
  1356. command_print(CMD, "cache flush %s",
  1357. (retval == ERROR_OK) ? "successful" : "failed");
  1358. return retval;
  1359. }
  1360. static const struct {
  1361. const char *name;
  1362. int mask;
  1363. } esirisc_hwdc_masks[] = {
  1364. { "reset", HWDC_R },
  1365. { "interrupt", HWDC_I },
  1366. { "syscall", HWDC_S },
  1367. { "error", HWDC_E },
  1368. { "debug", HWDC_D },
  1369. };
  1370. static int esirisc_find_hwdc_mask(const char *name)
  1371. {
  1372. for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
  1373. if (strcmp(esirisc_hwdc_masks[i].name, name) == 0)
  1374. return esirisc_hwdc_masks[i].mask;
  1375. return -1;
  1376. }
  1377. COMMAND_HANDLER(handle_esirisc_hwdc_command)
  1378. {
  1379. struct target *target = get_current_target(CMD_CTX);
  1380. struct esirisc_common *esirisc = target_to_esirisc(target);
  1381. if (CMD_ARGC > 0) {
  1382. if (strcmp(CMD_ARGV[0], "all") == 0)
  1383. esirisc->hwdc_save = HWDC_R | HWDC_I | HWDC_S | HWDC_E | HWDC_D;
  1384. else {
  1385. esirisc->hwdc_save = 0;
  1386. if (strcmp(CMD_ARGV[0], "none") != 0) {
  1387. while (CMD_ARGC-- > 0) {
  1388. int mask = esirisc_find_hwdc_mask(CMD_ARGV[CMD_ARGC]);
  1389. if (mask < 0) {
  1390. LOG_ERROR("invalid mask: %s", CMD_ARGV[CMD_ARGC]);
  1391. return ERROR_COMMAND_SYNTAX_ERROR;
  1392. }
  1393. esirisc->hwdc_save |= mask;
  1394. }
  1395. }
  1396. }
  1397. }
  1398. for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
  1399. command_print(CMD, "%9s: %s", esirisc_hwdc_masks[i].name,
  1400. (esirisc->hwdc_save & esirisc_hwdc_masks[i].mask) ? "enabled" : "disabled");
  1401. return ERROR_OK;
  1402. }
  1403. static const struct command_registration esirisc_exec_command_handlers[] = {
  1404. {
  1405. .name = "flush_caches",
  1406. .handler = handle_esirisc_flush_caches_command,
  1407. .mode = COMMAND_EXEC,
  1408. .help = "flush instruction and data caches",
  1409. .usage = "",
  1410. },
  1411. COMMAND_REGISTRATION_DONE
  1412. };
  1413. static const struct command_registration esirisc_any_command_handlers[] = {
  1414. {
  1415. .name = "cache_arch",
  1416. .handler = handle_esirisc_cache_arch_command,
  1417. .mode = COMMAND_ANY,
  1418. .help = "configure cache architecture",
  1419. .usage = "['harvard'|'von_neumann']",
  1420. },
  1421. {
  1422. .name = "hwdc",
  1423. .handler = handle_esirisc_hwdc_command,
  1424. .mode = COMMAND_ANY,
  1425. .help = "configure hardware debug control",
  1426. .usage = "['all'|'none'|mask ...]",
  1427. },
  1428. {
  1429. .chain = esirisc_exec_command_handlers
  1430. },
  1431. {
  1432. .chain = esirisc_trace_command_handlers
  1433. },
  1434. COMMAND_REGISTRATION_DONE
  1435. };
  1436. static const struct command_registration esirisc_command_handlers[] = {
  1437. {
  1438. .name = "esirisc",
  1439. .mode = COMMAND_ANY,
  1440. .help = "eSi-RISC command group",
  1441. .usage = "",
  1442. .chain = esirisc_any_command_handlers,
  1443. },
  1444. COMMAND_REGISTRATION_DONE
  1445. };
  1446. struct target_type esirisc_target = {
  1447. .name = "esirisc",
  1448. .poll = esirisc_poll,
  1449. .arch_state = esirisc_arch_state,
  1450. .halt = esirisc_halt,
  1451. .resume = esirisc_resume,
  1452. .step = esirisc_step,
  1453. .assert_reset = esirisc_assert_reset,
  1454. .deassert_reset = esirisc_deassert_reset,
  1455. .get_gdb_arch = esirisc_get_gdb_arch,
  1456. .get_gdb_reg_list = esirisc_get_gdb_reg_list,
  1457. .read_memory = esirisc_read_memory,
  1458. .write_memory = esirisc_write_memory,
  1459. .checksum_memory = esirisc_checksum_memory,
  1460. .add_breakpoint = esirisc_add_breakpoint,
  1461. .remove_breakpoint = esirisc_remove_breakpoint,
  1462. .add_watchpoint = esirisc_add_watchpoint,
  1463. .remove_watchpoint = esirisc_remove_watchpoint,
  1464. .commands = esirisc_command_handlers,
  1465. .target_create = esirisc_target_create,
  1466. .init_target = esirisc_init_target,
  1467. .examine = esirisc_examine,
  1468. };