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.
 
 
 
 
 
 

4272 lines
116 KiB

  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #ifdef HAVE_CONFIG_H
  6. #include "config.h"
  7. #endif
  8. #include "target/target.h"
  9. #include "target/algorithm.h"
  10. #include "target/target_type.h"
  11. #include "log.h"
  12. #include "jtag/jtag.h"
  13. #include "target/register.h"
  14. #include "target/breakpoints.h"
  15. #include "helper/time_support.h"
  16. #include "riscv.h"
  17. #include "gdb_regs.h"
  18. #include "rtos/rtos.h"
  19. #define get_field(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
  20. #define set_field(reg, mask, val) (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
  21. /* Constants for legacy SiFive hardware breakpoints. */
  22. #define CSR_BPCONTROL_X (1<<0)
  23. #define CSR_BPCONTROL_W (1<<1)
  24. #define CSR_BPCONTROL_R (1<<2)
  25. #define CSR_BPCONTROL_U (1<<3)
  26. #define CSR_BPCONTROL_S (1<<4)
  27. #define CSR_BPCONTROL_H (1<<5)
  28. #define CSR_BPCONTROL_M (1<<6)
  29. #define CSR_BPCONTROL_BPMATCH (0xf<<7)
  30. #define CSR_BPCONTROL_BPACTION (0xff<<11)
  31. #define DEBUG_ROM_START 0x800
  32. #define DEBUG_ROM_RESUME (DEBUG_ROM_START + 4)
  33. #define DEBUG_ROM_EXCEPTION (DEBUG_ROM_START + 8)
  34. #define DEBUG_RAM_START 0x400
  35. #define SETHALTNOT 0x10c
  36. /*** JTAG registers. ***/
  37. #define DTMCONTROL 0x10
  38. #define DTMCONTROL_DBUS_RESET (1<<16)
  39. #define DTMCONTROL_IDLE (7<<10)
  40. #define DTMCONTROL_ADDRBITS (0xf<<4)
  41. #define DTMCONTROL_VERSION (0xf)
  42. #define DBUS 0x11
  43. #define DBUS_OP_START 0
  44. #define DBUS_OP_SIZE 2
  45. typedef enum {
  46. DBUS_OP_NOP = 0,
  47. DBUS_OP_READ = 1,
  48. DBUS_OP_WRITE = 2
  49. } dbus_op_t;
  50. typedef enum {
  51. DBUS_STATUS_SUCCESS = 0,
  52. DBUS_STATUS_FAILED = 2,
  53. DBUS_STATUS_BUSY = 3
  54. } dbus_status_t;
  55. #define DBUS_DATA_START 2
  56. #define DBUS_DATA_SIZE 34
  57. #define DBUS_ADDRESS_START 36
  58. typedef enum slot {
  59. SLOT0,
  60. SLOT1,
  61. SLOT_LAST,
  62. } slot_t;
  63. /*** Debug Bus registers. ***/
  64. #define DMCONTROL 0x10
  65. #define DMCONTROL_INTERRUPT (((uint64_t)1)<<33)
  66. #define DMCONTROL_HALTNOT (((uint64_t)1)<<32)
  67. #define DMCONTROL_BUSERROR (7<<19)
  68. #define DMCONTROL_SERIAL (3<<16)
  69. #define DMCONTROL_AUTOINCREMENT (1<<15)
  70. #define DMCONTROL_ACCESS (7<<12)
  71. #define DMCONTROL_HARTID (0x3ff<<2)
  72. #define DMCONTROL_NDRESET (1<<1)
  73. #define DMCONTROL_FULLRESET 1
  74. #define DMINFO 0x11
  75. #define DMINFO_ABUSSIZE (0x7fU<<25)
  76. #define DMINFO_SERIALCOUNT (0xf<<21)
  77. #define DMINFO_ACCESS128 (1<<20)
  78. #define DMINFO_ACCESS64 (1<<19)
  79. #define DMINFO_ACCESS32 (1<<18)
  80. #define DMINFO_ACCESS16 (1<<17)
  81. #define DMINFO_ACCESS8 (1<<16)
  82. #define DMINFO_DRAMSIZE (0x3f<<10)
  83. #define DMINFO_AUTHENTICATED (1<<5)
  84. #define DMINFO_AUTHBUSY (1<<4)
  85. #define DMINFO_AUTHTYPE (3<<2)
  86. #define DMINFO_VERSION 3
  87. /*** Info about the core being debugged. ***/
  88. #define DBUS_ADDRESS_UNKNOWN 0xffff
  89. #define MAX_HWBPS 16
  90. #define DRAM_CACHE_SIZE 16
  91. uint8_t ir_dtmcontrol[4] = {DTMCONTROL};
  92. struct scan_field select_dtmcontrol = {
  93. .in_value = NULL,
  94. .out_value = ir_dtmcontrol
  95. };
  96. uint8_t ir_dbus[4] = {DBUS};
  97. struct scan_field select_dbus = {
  98. .in_value = NULL,
  99. .out_value = ir_dbus
  100. };
  101. uint8_t ir_idcode[4] = {0x1};
  102. struct scan_field select_idcode = {
  103. .in_value = NULL,
  104. .out_value = ir_idcode
  105. };
  106. bscan_tunnel_type_t bscan_tunnel_type;
  107. int bscan_tunnel_ir_width; /* if zero, then tunneling is not present/active */
  108. static uint8_t bscan_zero[4] = {0};
  109. static uint8_t bscan_one[4] = {1};
  110. uint8_t ir_user4[4] = {0x23};
  111. struct scan_field select_user4 = {
  112. .in_value = NULL,
  113. .out_value = ir_user4
  114. };
  115. uint8_t bscan_tunneled_ir_width[4] = {5}; /* overridden by assignment in riscv_init_target */
  116. struct scan_field _bscan_tunnel_data_register_select_dmi[] = {
  117. {
  118. .num_bits = 3,
  119. .out_value = bscan_zero,
  120. .in_value = NULL,
  121. },
  122. {
  123. .num_bits = 5, /* initialized in riscv_init_target to ir width of DM */
  124. .out_value = ir_dbus,
  125. .in_value = NULL,
  126. },
  127. {
  128. .num_bits = 7,
  129. .out_value = bscan_tunneled_ir_width,
  130. .in_value = NULL,
  131. },
  132. {
  133. .num_bits = 1,
  134. .out_value = bscan_zero,
  135. .in_value = NULL,
  136. }
  137. };
  138. struct scan_field _bscan_tunnel_nested_tap_select_dmi[] = {
  139. {
  140. .num_bits = 1,
  141. .out_value = bscan_zero,
  142. .in_value = NULL,
  143. },
  144. {
  145. .num_bits = 7,
  146. .out_value = bscan_tunneled_ir_width,
  147. .in_value = NULL,
  148. },
  149. {
  150. .num_bits = 0, /* initialized in riscv_init_target to ir width of DM */
  151. .out_value = ir_dbus,
  152. .in_value = NULL,
  153. },
  154. {
  155. .num_bits = 3,
  156. .out_value = bscan_zero,
  157. .in_value = NULL,
  158. }
  159. };
  160. struct scan_field *bscan_tunnel_nested_tap_select_dmi = _bscan_tunnel_nested_tap_select_dmi;
  161. uint32_t bscan_tunnel_nested_tap_select_dmi_num_fields = ARRAY_SIZE(_bscan_tunnel_nested_tap_select_dmi);
  162. struct scan_field *bscan_tunnel_data_register_select_dmi = _bscan_tunnel_data_register_select_dmi;
  163. uint32_t bscan_tunnel_data_register_select_dmi_num_fields = ARRAY_SIZE(_bscan_tunnel_data_register_select_dmi);
  164. struct trigger {
  165. uint64_t address;
  166. uint32_t length;
  167. uint64_t mask;
  168. uint64_t value;
  169. bool read, write, execute;
  170. int unique_id;
  171. };
  172. /* Wall-clock timeout for a command/access. Settable via RISC-V Target commands.*/
  173. int riscv_command_timeout_sec = DEFAULT_COMMAND_TIMEOUT_SEC;
  174. /* Wall-clock timeout after reset. Settable via RISC-V Target commands.*/
  175. int riscv_reset_timeout_sec = DEFAULT_RESET_TIMEOUT_SEC;
  176. bool riscv_prefer_sba;
  177. bool riscv_enable_virt2phys = true;
  178. bool riscv_ebreakm = true;
  179. bool riscv_ebreaks = true;
  180. bool riscv_ebreaku = true;
  181. bool riscv_enable_virtual;
  182. typedef struct {
  183. uint16_t low, high;
  184. } range_t;
  185. /* In addition to the ones in the standard spec, we'll also expose additional
  186. * CSRs in this list.
  187. * The list is either NULL, or a series of ranges (inclusive), terminated with
  188. * 1,0. */
  189. range_t *expose_csr;
  190. /* Same, but for custom registers. */
  191. range_t *expose_custom;
  192. static enum {
  193. RO_NORMAL,
  194. RO_REVERSED
  195. } resume_order;
  196. virt2phys_info_t sv32 = {
  197. .name = "Sv32",
  198. .va_bits = 32,
  199. .level = 2,
  200. .pte_shift = 2,
  201. .vpn_shift = {12, 22},
  202. .vpn_mask = {0x3ff, 0x3ff},
  203. .pte_ppn_shift = {10, 20},
  204. .pte_ppn_mask = {0x3ff, 0xfff},
  205. .pa_ppn_shift = {12, 22},
  206. .pa_ppn_mask = {0x3ff, 0xfff},
  207. };
  208. virt2phys_info_t sv39 = {
  209. .name = "Sv39",
  210. .va_bits = 39,
  211. .level = 3,
  212. .pte_shift = 3,
  213. .vpn_shift = {12, 21, 30},
  214. .vpn_mask = {0x1ff, 0x1ff, 0x1ff},
  215. .pte_ppn_shift = {10, 19, 28},
  216. .pte_ppn_mask = {0x1ff, 0x1ff, 0x3ffffff},
  217. .pa_ppn_shift = {12, 21, 30},
  218. .pa_ppn_mask = {0x1ff, 0x1ff, 0x3ffffff},
  219. };
  220. virt2phys_info_t sv48 = {
  221. .name = "Sv48",
  222. .va_bits = 48,
  223. .level = 4,
  224. .pte_shift = 3,
  225. .vpn_shift = {12, 21, 30, 39},
  226. .vpn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ff},
  227. .pte_ppn_shift = {10, 19, 28, 37},
  228. .pte_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
  229. .pa_ppn_shift = {12, 21, 30, 39},
  230. .pa_ppn_mask = {0x1ff, 0x1ff, 0x1ff, 0x1ffff},
  231. };
  232. static int riscv_resume_go_all_harts(struct target *target);
  233. void select_dmi_via_bscan(struct target *target)
  234. {
  235. jtag_add_ir_scan(target->tap, &select_user4, TAP_IDLE);
  236. if (bscan_tunnel_type == BSCAN_TUNNEL_DATA_REGISTER)
  237. jtag_add_dr_scan(target->tap, bscan_tunnel_data_register_select_dmi_num_fields,
  238. bscan_tunnel_data_register_select_dmi, TAP_IDLE);
  239. else /* BSCAN_TUNNEL_NESTED_TAP */
  240. jtag_add_dr_scan(target->tap, bscan_tunnel_nested_tap_select_dmi_num_fields,
  241. bscan_tunnel_nested_tap_select_dmi, TAP_IDLE);
  242. }
  243. uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out)
  244. {
  245. /* On BSCAN TAP: Select IR=USER4, issue tunneled IR scan via BSCAN TAP's DR */
  246. uint8_t tunneled_ir_width[4] = {bscan_tunnel_ir_width};
  247. uint8_t tunneled_dr_width[4] = {32};
  248. uint8_t out_value[5] = {0};
  249. uint8_t in_value[5] = {0};
  250. buf_set_u32(out_value, 0, 32, out);
  251. struct scan_field tunneled_ir[4] = {};
  252. struct scan_field tunneled_dr[4] = {};
  253. if (bscan_tunnel_type == BSCAN_TUNNEL_DATA_REGISTER) {
  254. tunneled_ir[0].num_bits = 3;
  255. tunneled_ir[0].out_value = bscan_zero;
  256. tunneled_ir[0].in_value = NULL;
  257. tunneled_ir[1].num_bits = bscan_tunnel_ir_width;
  258. tunneled_ir[1].out_value = ir_dtmcontrol;
  259. tunneled_ir[1].in_value = NULL;
  260. tunneled_ir[2].num_bits = 7;
  261. tunneled_ir[2].out_value = tunneled_ir_width;
  262. tunneled_ir[2].in_value = NULL;
  263. tunneled_ir[3].num_bits = 1;
  264. tunneled_ir[3].out_value = bscan_zero;
  265. tunneled_ir[3].in_value = NULL;
  266. tunneled_dr[0].num_bits = 3;
  267. tunneled_dr[0].out_value = bscan_zero;
  268. tunneled_dr[0].in_value = NULL;
  269. tunneled_dr[1].num_bits = 32 + 1;
  270. tunneled_dr[1].out_value = out_value;
  271. tunneled_dr[1].in_value = in_value;
  272. tunneled_dr[2].num_bits = 7;
  273. tunneled_dr[2].out_value = tunneled_dr_width;
  274. tunneled_dr[2].in_value = NULL;
  275. tunneled_dr[3].num_bits = 1;
  276. tunneled_dr[3].out_value = bscan_one;
  277. tunneled_dr[3].in_value = NULL;
  278. } else {
  279. /* BSCAN_TUNNEL_NESTED_TAP */
  280. tunneled_ir[3].num_bits = 3;
  281. tunneled_ir[3].out_value = bscan_zero;
  282. tunneled_ir[3].in_value = NULL;
  283. tunneled_ir[2].num_bits = bscan_tunnel_ir_width;
  284. tunneled_ir[2].out_value = ir_dtmcontrol;
  285. tunneled_ir[1].in_value = NULL;
  286. tunneled_ir[1].num_bits = 7;
  287. tunneled_ir[1].out_value = tunneled_ir_width;
  288. tunneled_ir[2].in_value = NULL;
  289. tunneled_ir[0].num_bits = 1;
  290. tunneled_ir[0].out_value = bscan_zero;
  291. tunneled_ir[0].in_value = NULL;
  292. tunneled_dr[3].num_bits = 3;
  293. tunneled_dr[3].out_value = bscan_zero;
  294. tunneled_dr[3].in_value = NULL;
  295. tunneled_dr[2].num_bits = 32 + 1;
  296. tunneled_dr[2].out_value = out_value;
  297. tunneled_dr[2].in_value = in_value;
  298. tunneled_dr[1].num_bits = 7;
  299. tunneled_dr[1].out_value = tunneled_dr_width;
  300. tunneled_dr[1].in_value = NULL;
  301. tunneled_dr[0].num_bits = 1;
  302. tunneled_dr[0].out_value = bscan_one;
  303. tunneled_dr[0].in_value = NULL;
  304. }
  305. jtag_add_ir_scan(target->tap, &select_user4, TAP_IDLE);
  306. jtag_add_dr_scan(target->tap, ARRAY_SIZE(tunneled_ir), tunneled_ir, TAP_IDLE);
  307. jtag_add_dr_scan(target->tap, ARRAY_SIZE(tunneled_dr), tunneled_dr, TAP_IDLE);
  308. select_dmi_via_bscan(target);
  309. int retval = jtag_execute_queue();
  310. if (retval != ERROR_OK) {
  311. LOG_ERROR("failed jtag scan: %d", retval);
  312. return retval;
  313. }
  314. /* Note the starting offset is bit 1, not bit 0. In BSCAN tunnel, there is a one-bit TCK skew between
  315. output and input */
  316. uint32_t in = buf_get_u32(in_value, 1, 32);
  317. LOG_DEBUG("DTMCS: 0x%x -> 0x%x", out, in);
  318. return in;
  319. }
  320. static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
  321. {
  322. struct scan_field field;
  323. uint8_t in_value[4];
  324. uint8_t out_value[4] = { 0 };
  325. if (bscan_tunnel_ir_width != 0)
  326. return dtmcontrol_scan_via_bscan(target, out);
  327. buf_set_u32(out_value, 0, 32, out);
  328. jtag_add_ir_scan(target->tap, &select_dtmcontrol, TAP_IDLE);
  329. field.num_bits = 32;
  330. field.out_value = out_value;
  331. field.in_value = in_value;
  332. jtag_add_dr_scan(target->tap, 1, &field, TAP_IDLE);
  333. /* Always return to dbus. */
  334. jtag_add_ir_scan(target->tap, &select_dbus, TAP_IDLE);
  335. int retval = jtag_execute_queue();
  336. if (retval != ERROR_OK) {
  337. LOG_ERROR("failed jtag scan: %d", retval);
  338. return retval;
  339. }
  340. uint32_t in = buf_get_u32(field.in_value, 0, 32);
  341. LOG_DEBUG("DTMCONTROL: 0x%x -> 0x%x", out, in);
  342. return in;
  343. }
  344. static struct target_type *get_target_type(struct target *target)
  345. {
  346. riscv_info_t *info = (riscv_info_t *) target->arch_info;
  347. if (!info) {
  348. LOG_ERROR("Target has not been initialized");
  349. return NULL;
  350. }
  351. switch (info->dtm_version) {
  352. case 0:
  353. return &riscv011_target;
  354. case 1:
  355. return &riscv013_target;
  356. default:
  357. LOG_ERROR("Unsupported DTM version: %d", info->dtm_version);
  358. return NULL;
  359. }
  360. }
  361. static int riscv_init_target(struct command_context *cmd_ctx,
  362. struct target *target)
  363. {
  364. LOG_DEBUG("riscv_init_target()");
  365. target->arch_info = calloc(1, sizeof(riscv_info_t));
  366. if (!target->arch_info)
  367. return ERROR_FAIL;
  368. riscv_info_t *info = (riscv_info_t *) target->arch_info;
  369. riscv_info_init(target, info);
  370. info->cmd_ctx = cmd_ctx;
  371. select_dtmcontrol.num_bits = target->tap->ir_length;
  372. select_dbus.num_bits = target->tap->ir_length;
  373. select_idcode.num_bits = target->tap->ir_length;
  374. if (bscan_tunnel_ir_width != 0) {
  375. select_user4.num_bits = target->tap->ir_length;
  376. bscan_tunneled_ir_width[0] = bscan_tunnel_ir_width;
  377. if (bscan_tunnel_type == BSCAN_TUNNEL_DATA_REGISTER)
  378. bscan_tunnel_data_register_select_dmi[1].num_bits = bscan_tunnel_ir_width;
  379. else /* BSCAN_TUNNEL_NESTED_TAP */
  380. bscan_tunnel_nested_tap_select_dmi[2].num_bits = bscan_tunnel_ir_width;
  381. }
  382. riscv_semihosting_init(target);
  383. target->debug_reason = DBG_REASON_DBGRQ;
  384. return ERROR_OK;
  385. }
  386. static void riscv_free_registers(struct target *target)
  387. {
  388. /* Free the shared structure use for most registers. */
  389. if (target->reg_cache) {
  390. if (target->reg_cache->reg_list) {
  391. free(target->reg_cache->reg_list[0].arch_info);
  392. /* Free the ones we allocated separately. */
  393. for (unsigned i = GDB_REGNO_COUNT; i < target->reg_cache->num_regs; i++)
  394. free(target->reg_cache->reg_list[i].arch_info);
  395. free(target->reg_cache->reg_list);
  396. }
  397. free(target->reg_cache);
  398. }
  399. }
  400. static void riscv_deinit_target(struct target *target)
  401. {
  402. LOG_DEBUG("riscv_deinit_target()");
  403. struct target_type *tt = get_target_type(target);
  404. if (tt) {
  405. tt->deinit_target(target);
  406. riscv_info_t *info = (riscv_info_t *) target->arch_info;
  407. free(info->reg_names);
  408. free(info);
  409. }
  410. riscv_free_registers(target);
  411. target->arch_info = NULL;
  412. }
  413. static void trigger_from_breakpoint(struct trigger *trigger,
  414. const struct breakpoint *breakpoint)
  415. {
  416. trigger->address = breakpoint->address;
  417. trigger->length = breakpoint->length;
  418. trigger->mask = ~0LL;
  419. trigger->read = false;
  420. trigger->write = false;
  421. trigger->execute = true;
  422. /* unique_id is unique across both breakpoints and watchpoints. */
  423. trigger->unique_id = breakpoint->unique_id;
  424. }
  425. static int maybe_add_trigger_t1(struct target *target, unsigned hartid,
  426. struct trigger *trigger, uint64_t tdata1)
  427. {
  428. RISCV_INFO(r);
  429. const uint32_t bpcontrol_x = 1<<0;
  430. const uint32_t bpcontrol_w = 1<<1;
  431. const uint32_t bpcontrol_r = 1<<2;
  432. const uint32_t bpcontrol_u = 1<<3;
  433. const uint32_t bpcontrol_s = 1<<4;
  434. const uint32_t bpcontrol_h = 1<<5;
  435. const uint32_t bpcontrol_m = 1<<6;
  436. const uint32_t bpcontrol_bpmatch = 0xf << 7;
  437. const uint32_t bpcontrol_bpaction = 0xff << 11;
  438. if (tdata1 & (bpcontrol_r | bpcontrol_w | bpcontrol_x)) {
  439. /* Trigger is already in use, presumably by user code. */
  440. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  441. }
  442. tdata1 = set_field(tdata1, bpcontrol_r, trigger->read);
  443. tdata1 = set_field(tdata1, bpcontrol_w, trigger->write);
  444. tdata1 = set_field(tdata1, bpcontrol_x, trigger->execute);
  445. tdata1 = set_field(tdata1, bpcontrol_u,
  446. !!(r->misa[hartid] & (1 << ('U' - 'A'))));
  447. tdata1 = set_field(tdata1, bpcontrol_s,
  448. !!(r->misa[hartid] & (1 << ('S' - 'A'))));
  449. tdata1 = set_field(tdata1, bpcontrol_h,
  450. !!(r->misa[hartid] & (1 << ('H' - 'A'))));
  451. tdata1 |= bpcontrol_m;
  452. tdata1 = set_field(tdata1, bpcontrol_bpmatch, 0); /* exact match */
  453. tdata1 = set_field(tdata1, bpcontrol_bpaction, 0); /* cause bp exception */
  454. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, tdata1);
  455. riscv_reg_t tdata1_rb;
  456. if (riscv_get_register_on_hart(target, &tdata1_rb, hartid,
  457. GDB_REGNO_TDATA1) != ERROR_OK)
  458. return ERROR_FAIL;
  459. LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
  460. if (tdata1 != tdata1_rb) {
  461. LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
  462. PRIx64 " to tdata1 it contains 0x%" PRIx64,
  463. tdata1, tdata1_rb);
  464. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
  465. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  466. }
  467. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA2, trigger->address);
  468. return ERROR_OK;
  469. }
  470. static int maybe_add_trigger_t2(struct target *target, unsigned hartid,
  471. struct trigger *trigger, uint64_t tdata1)
  472. {
  473. RISCV_INFO(r);
  474. /* tselect is already set */
  475. if (tdata1 & (MCONTROL_EXECUTE | MCONTROL_STORE | MCONTROL_LOAD)) {
  476. /* Trigger is already in use, presumably by user code. */
  477. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  478. }
  479. /* address/data match trigger */
  480. tdata1 |= MCONTROL_DMODE(riscv_xlen(target));
  481. tdata1 = set_field(tdata1, MCONTROL_ACTION,
  482. MCONTROL_ACTION_DEBUG_MODE);
  483. tdata1 = set_field(tdata1, MCONTROL_MATCH, MCONTROL_MATCH_EQUAL);
  484. tdata1 |= MCONTROL_M;
  485. if (r->misa[hartid] & (1 << ('H' - 'A')))
  486. tdata1 |= MCONTROL_H;
  487. if (r->misa[hartid] & (1 << ('S' - 'A')))
  488. tdata1 |= MCONTROL_S;
  489. if (r->misa[hartid] & (1 << ('U' - 'A')))
  490. tdata1 |= MCONTROL_U;
  491. if (trigger->execute)
  492. tdata1 |= MCONTROL_EXECUTE;
  493. if (trigger->read)
  494. tdata1 |= MCONTROL_LOAD;
  495. if (trigger->write)
  496. tdata1 |= MCONTROL_STORE;
  497. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, tdata1);
  498. uint64_t tdata1_rb;
  499. int result = riscv_get_register_on_hart(target, &tdata1_rb, hartid, GDB_REGNO_TDATA1);
  500. if (result != ERROR_OK)
  501. return result;
  502. LOG_DEBUG("tdata1=0x%" PRIx64, tdata1_rb);
  503. if (tdata1 != tdata1_rb) {
  504. LOG_DEBUG("Trigger doesn't support what we need; After writing 0x%"
  505. PRIx64 " to tdata1 it contains 0x%" PRIx64,
  506. tdata1, tdata1_rb);
  507. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
  508. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  509. }
  510. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA2, trigger->address);
  511. return ERROR_OK;
  512. }
  513. static int add_trigger(struct target *target, struct trigger *trigger)
  514. {
  515. RISCV_INFO(r);
  516. if (riscv_enumerate_triggers(target) != ERROR_OK)
  517. return ERROR_FAIL;
  518. /* In RTOS mode, we need to set the same trigger in the same slot on every
  519. * hart, to keep up the illusion that each hart is a thread running on the
  520. * same core. */
  521. /* Otherwise, we just set the trigger on the one hart this target deals
  522. * with. */
  523. riscv_reg_t tselect[RISCV_MAX_HARTS];
  524. int first_hart = -1;
  525. for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
  526. if (!riscv_hart_enabled(target, hartid))
  527. continue;
  528. if (first_hart < 0)
  529. first_hart = hartid;
  530. int result = riscv_get_register_on_hart(target, &tselect[hartid],
  531. hartid, GDB_REGNO_TSELECT);
  532. if (result != ERROR_OK)
  533. return result;
  534. }
  535. assert(first_hart >= 0);
  536. unsigned int i;
  537. for (i = 0; i < r->trigger_count[first_hart]; i++) {
  538. if (r->trigger_unique_id[i] != -1)
  539. continue;
  540. riscv_set_register_on_hart(target, first_hart, GDB_REGNO_TSELECT, i);
  541. uint64_t tdata1;
  542. int result = riscv_get_register_on_hart(target, &tdata1, first_hart,
  543. GDB_REGNO_TDATA1);
  544. if (result != ERROR_OK)
  545. return result;
  546. int type = get_field(tdata1, MCONTROL_TYPE(riscv_xlen(target)));
  547. result = ERROR_OK;
  548. for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
  549. if (!riscv_hart_enabled(target, hartid))
  550. continue;
  551. if (hartid > first_hart)
  552. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, i);
  553. switch (type) {
  554. case 1:
  555. result = maybe_add_trigger_t1(target, hartid, trigger, tdata1);
  556. break;
  557. case 2:
  558. result = maybe_add_trigger_t2(target, hartid, trigger, tdata1);
  559. break;
  560. default:
  561. LOG_DEBUG("trigger %d has unknown type %d", i, type);
  562. continue;
  563. }
  564. if (result != ERROR_OK)
  565. continue;
  566. }
  567. if (result != ERROR_OK)
  568. continue;
  569. LOG_DEBUG("[%d] Using trigger %d (type %d) for bp %d", target->coreid,
  570. i, type, trigger->unique_id);
  571. r->trigger_unique_id[i] = trigger->unique_id;
  572. break;
  573. }
  574. for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
  575. if (!riscv_hart_enabled(target, hartid))
  576. continue;
  577. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT,
  578. tselect[hartid]);
  579. }
  580. if (i >= r->trigger_count[first_hart]) {
  581. LOG_ERROR("Couldn't find an available hardware trigger.");
  582. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  583. }
  584. return ERROR_OK;
  585. }
  586. int riscv_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
  587. {
  588. LOG_DEBUG("[%d] @0x%" TARGET_PRIxADDR, target->coreid, breakpoint->address);
  589. assert(breakpoint);
  590. if (breakpoint->type == BKPT_SOFT) {
  591. /** @todo check RVC for size/alignment */
  592. if (!(breakpoint->length == 4 || breakpoint->length == 2)) {
  593. LOG_ERROR("Invalid breakpoint length %d", breakpoint->length);
  594. return ERROR_FAIL;
  595. }
  596. if (0 != (breakpoint->address % 2)) {
  597. LOG_ERROR("Invalid breakpoint alignment for address 0x%" TARGET_PRIxADDR, breakpoint->address);
  598. return ERROR_FAIL;
  599. }
  600. if (target_read_memory(target, breakpoint->address, 2, breakpoint->length / 2,
  601. breakpoint->orig_instr) != ERROR_OK) {
  602. LOG_ERROR("Failed to read original instruction at 0x%" TARGET_PRIxADDR,
  603. breakpoint->address);
  604. return ERROR_FAIL;
  605. }
  606. uint8_t buff[4] = { 0 };
  607. buf_set_u32(buff, 0, breakpoint->length * CHAR_BIT, breakpoint->length == 4 ? ebreak() : ebreak_c());
  608. int const retval = target_write_memory(target, breakpoint->address, 2, breakpoint->length / 2, buff);
  609. if (retval != ERROR_OK) {
  610. LOG_ERROR("Failed to write %d-byte breakpoint instruction at 0x%"
  611. TARGET_PRIxADDR, breakpoint->length, breakpoint->address);
  612. return ERROR_FAIL;
  613. }
  614. } else if (breakpoint->type == BKPT_HARD) {
  615. struct trigger trigger;
  616. trigger_from_breakpoint(&trigger, breakpoint);
  617. int const result = add_trigger(target, &trigger);
  618. if (result != ERROR_OK)
  619. return result;
  620. } else {
  621. LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
  622. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  623. }
  624. breakpoint->set = true;
  625. return ERROR_OK;
  626. }
  627. static int remove_trigger(struct target *target, struct trigger *trigger)
  628. {
  629. RISCV_INFO(r);
  630. if (riscv_enumerate_triggers(target) != ERROR_OK)
  631. return ERROR_FAIL;
  632. int first_hart = -1;
  633. for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
  634. if (!riscv_hart_enabled(target, hartid))
  635. continue;
  636. if (first_hart < 0) {
  637. first_hart = hartid;
  638. break;
  639. }
  640. }
  641. assert(first_hart >= 0);
  642. unsigned int i;
  643. for (i = 0; i < r->trigger_count[first_hart]; i++) {
  644. if (r->trigger_unique_id[i] == trigger->unique_id)
  645. break;
  646. }
  647. if (i >= r->trigger_count[first_hart]) {
  648. LOG_ERROR("Couldn't find the hardware resources used by hardware "
  649. "trigger.");
  650. return ERROR_FAIL;
  651. }
  652. LOG_DEBUG("[%d] Stop using resource %d for bp %d", target->coreid, i,
  653. trigger->unique_id);
  654. for (int hartid = first_hart; hartid < riscv_count_harts(target); ++hartid) {
  655. if (!riscv_hart_enabled(target, hartid))
  656. continue;
  657. riscv_reg_t tselect;
  658. int result = riscv_get_register_on_hart(target, &tselect, hartid, GDB_REGNO_TSELECT);
  659. if (result != ERROR_OK)
  660. return result;
  661. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, i);
  662. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
  663. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, tselect);
  664. }
  665. r->trigger_unique_id[i] = -1;
  666. return ERROR_OK;
  667. }
  668. int riscv_remove_breakpoint(struct target *target,
  669. struct breakpoint *breakpoint)
  670. {
  671. if (breakpoint->type == BKPT_SOFT) {
  672. if (target_write_memory(target, breakpoint->address, 2, breakpoint->length / 2,
  673. breakpoint->orig_instr) != ERROR_OK) {
  674. LOG_ERROR("Failed to restore instruction for %d-byte breakpoint at "
  675. "0x%" TARGET_PRIxADDR, breakpoint->length, breakpoint->address);
  676. return ERROR_FAIL;
  677. }
  678. } else if (breakpoint->type == BKPT_HARD) {
  679. struct trigger trigger;
  680. trigger_from_breakpoint(&trigger, breakpoint);
  681. int result = remove_trigger(target, &trigger);
  682. if (result != ERROR_OK)
  683. return result;
  684. } else {
  685. LOG_INFO("OpenOCD only supports hardware and software breakpoints.");
  686. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  687. }
  688. breakpoint->set = false;
  689. return ERROR_OK;
  690. }
  691. static void trigger_from_watchpoint(struct trigger *trigger,
  692. const struct watchpoint *watchpoint)
  693. {
  694. trigger->address = watchpoint->address;
  695. trigger->length = watchpoint->length;
  696. trigger->mask = watchpoint->mask;
  697. trigger->value = watchpoint->value;
  698. trigger->read = (watchpoint->rw == WPT_READ || watchpoint->rw == WPT_ACCESS);
  699. trigger->write = (watchpoint->rw == WPT_WRITE || watchpoint->rw == WPT_ACCESS);
  700. trigger->execute = false;
  701. /* unique_id is unique across both breakpoints and watchpoints. */
  702. trigger->unique_id = watchpoint->unique_id;
  703. }
  704. int riscv_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
  705. {
  706. struct trigger trigger;
  707. trigger_from_watchpoint(&trigger, watchpoint);
  708. int result = add_trigger(target, &trigger);
  709. if (result != ERROR_OK)
  710. return result;
  711. watchpoint->set = true;
  712. return ERROR_OK;
  713. }
  714. int riscv_remove_watchpoint(struct target *target,
  715. struct watchpoint *watchpoint)
  716. {
  717. LOG_DEBUG("[%d] @0x%" TARGET_PRIxADDR, target->coreid, watchpoint->address);
  718. struct trigger trigger;
  719. trigger_from_watchpoint(&trigger, watchpoint);
  720. int result = remove_trigger(target, &trigger);
  721. if (result != ERROR_OK)
  722. return result;
  723. watchpoint->set = false;
  724. return ERROR_OK;
  725. }
  726. /* Sets *hit_watchpoint to the first watchpoint identified as causing the
  727. * current halt.
  728. *
  729. * The GDB server uses this information to tell GDB what data address has
  730. * been hit, which enables GDB to print the hit variable along with its old
  731. * and new value. */
  732. int riscv_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
  733. {
  734. struct watchpoint *wp = target->watchpoints;
  735. if (riscv_rtos_enabled(target))
  736. riscv_set_current_hartid(target, target->rtos->current_thread - 1);
  737. LOG_DEBUG("Current hartid = %d", riscv_current_hartid(target));
  738. /*TODO instead of disassembling the instruction that we think caused the
  739. * trigger, check the hit bit of each watchpoint first. The hit bit is
  740. * simpler and more reliable to check but as it is optional and relatively
  741. * new, not all hardware will implement it */
  742. riscv_reg_t dpc;
  743. riscv_get_register(target, &dpc, GDB_REGNO_DPC);
  744. const uint8_t length = 4;
  745. LOG_DEBUG("dpc is 0x%" PRIx64, dpc);
  746. /* fetch the instruction at dpc */
  747. uint8_t buffer[length];
  748. if (target_read_buffer(target, dpc, length, buffer) != ERROR_OK) {
  749. LOG_ERROR("Failed to read instruction at dpc 0x%" PRIx64, dpc);
  750. return ERROR_FAIL;
  751. }
  752. uint32_t instruction = 0;
  753. for (int i = 0; i < length; i++) {
  754. LOG_DEBUG("Next byte is %x", buffer[i]);
  755. instruction += (buffer[i] << 8 * i);
  756. }
  757. LOG_DEBUG("Full instruction is %x", instruction);
  758. /* find out which memory address is accessed by the instruction at dpc */
  759. /* opcode is first 7 bits of the instruction */
  760. uint8_t opcode = instruction & 0x7F;
  761. uint32_t rs1;
  762. int16_t imm;
  763. riscv_reg_t mem_addr;
  764. if (opcode == MATCH_LB || opcode == MATCH_SB) {
  765. rs1 = (instruction & 0xf8000) >> 15;
  766. riscv_get_register(target, &mem_addr, rs1);
  767. if (opcode == MATCH_SB) {
  768. LOG_DEBUG("%x is store instruction", instruction);
  769. imm = ((instruction & 0xf80) >> 7) | ((instruction & 0xfe000000) >> 20);
  770. } else {
  771. LOG_DEBUG("%x is load instruction", instruction);
  772. imm = (instruction & 0xfff00000) >> 20;
  773. }
  774. /* sign extend 12-bit imm to 16-bits */
  775. if (imm & (1 << 11))
  776. imm |= 0xf000;
  777. mem_addr += imm;
  778. LOG_DEBUG("memory address=0x%" PRIx64, mem_addr);
  779. } else {
  780. LOG_DEBUG("%x is not a RV32I load or store", instruction);
  781. return ERROR_FAIL;
  782. }
  783. while (wp) {
  784. /*TODO support length/mask */
  785. if (wp->address == mem_addr) {
  786. *hit_watchpoint = wp;
  787. LOG_DEBUG("Hit address=%" TARGET_PRIxADDR, wp->address);
  788. return ERROR_OK;
  789. }
  790. wp = wp->next;
  791. }
  792. /* No match found - either we hit a watchpoint caused by an instruction that
  793. * this function does not yet disassemble, or we hit a breakpoint.
  794. *
  795. * OpenOCD will behave as if this function had never been implemented i.e.
  796. * report the halt to GDB with no address information. */
  797. return ERROR_FAIL;
  798. }
  799. static int oldriscv_step(struct target *target, int current, uint32_t address,
  800. int handle_breakpoints)
  801. {
  802. struct target_type *tt = get_target_type(target);
  803. return tt->step(target, current, address, handle_breakpoints);
  804. }
  805. static int old_or_new_riscv_step(struct target *target, int current,
  806. target_addr_t address, int handle_breakpoints)
  807. {
  808. RISCV_INFO(r);
  809. LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints);
  810. if (!r->is_halted)
  811. return oldriscv_step(target, current, address, handle_breakpoints);
  812. else
  813. return riscv_openocd_step(target, current, address, handle_breakpoints);
  814. }
  815. static int riscv_examine(struct target *target)
  816. {
  817. LOG_DEBUG("riscv_examine()");
  818. if (target_was_examined(target)) {
  819. LOG_DEBUG("Target was already examined.");
  820. return ERROR_OK;
  821. }
  822. /* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
  823. riscv_info_t *info = (riscv_info_t *) target->arch_info;
  824. uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
  825. LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol);
  826. info->dtm_version = get_field(dtmcontrol, DTMCONTROL_VERSION);
  827. LOG_DEBUG(" version=0x%x", info->dtm_version);
  828. struct target_type *tt = get_target_type(target);
  829. if (!tt)
  830. return ERROR_FAIL;
  831. int result = tt->init_target(info->cmd_ctx, target);
  832. if (result != ERROR_OK)
  833. return result;
  834. return tt->examine(target);
  835. }
  836. static int oldriscv_poll(struct target *target)
  837. {
  838. struct target_type *tt = get_target_type(target);
  839. return tt->poll(target);
  840. }
  841. static int old_or_new_riscv_poll(struct target *target)
  842. {
  843. RISCV_INFO(r);
  844. if (!r->is_halted)
  845. return oldriscv_poll(target);
  846. else
  847. return riscv_openocd_poll(target);
  848. }
  849. int halt_prep(struct target *target)
  850. {
  851. RISCV_INFO(r);
  852. for (int i = 0; i < riscv_count_harts(target); ++i) {
  853. if (!riscv_hart_enabled(target, i))
  854. continue;
  855. LOG_DEBUG("[%s] prep hart, debug_reason=%d", target_name(target),
  856. target->debug_reason);
  857. if (riscv_set_current_hartid(target, i) != ERROR_OK)
  858. return ERROR_FAIL;
  859. if (riscv_is_halted(target)) {
  860. LOG_DEBUG("Hart %d is already halted (reason=%d).", i,
  861. target->debug_reason);
  862. } else {
  863. if (r->halt_prep(target) != ERROR_OK)
  864. return ERROR_FAIL;
  865. r->prepped = true;
  866. }
  867. }
  868. return ERROR_OK;
  869. }
  870. int riscv_halt_go_all_harts(struct target *target)
  871. {
  872. RISCV_INFO(r);
  873. for (int i = 0; i < riscv_count_harts(target); ++i) {
  874. if (!riscv_hart_enabled(target, i))
  875. continue;
  876. if (riscv_set_current_hartid(target, i) != ERROR_OK)
  877. return ERROR_FAIL;
  878. if (riscv_is_halted(target)) {
  879. LOG_DEBUG("Hart %d is already halted.", i);
  880. } else {
  881. if (r->halt_go(target) != ERROR_OK)
  882. return ERROR_FAIL;
  883. }
  884. }
  885. riscv_invalidate_register_cache(target);
  886. return ERROR_OK;
  887. }
  888. int halt_go(struct target *target)
  889. {
  890. riscv_info_t *r = riscv_info(target);
  891. int result;
  892. if (!r->is_halted) {
  893. struct target_type *tt = get_target_type(target);
  894. result = tt->halt(target);
  895. } else {
  896. result = riscv_halt_go_all_harts(target);
  897. }
  898. target->state = TARGET_HALTED;
  899. if (target->debug_reason == DBG_REASON_NOTHALTED)
  900. target->debug_reason = DBG_REASON_DBGRQ;
  901. return result;
  902. }
  903. static int halt_finish(struct target *target)
  904. {
  905. return target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  906. }
  907. int riscv_halt(struct target *target)
  908. {
  909. RISCV_INFO(r);
  910. if (!r->is_halted) {
  911. struct target_type *tt = get_target_type(target);
  912. return tt->halt(target);
  913. }
  914. LOG_DEBUG("[%d] halting all harts", target->coreid);
  915. int result = ERROR_OK;
  916. if (target->smp) {
  917. for (struct target_list *tlist = target->head; tlist; tlist = tlist->next) {
  918. struct target *t = tlist->target;
  919. if (halt_prep(t) != ERROR_OK)
  920. result = ERROR_FAIL;
  921. }
  922. for (struct target_list *tlist = target->head; tlist; tlist = tlist->next) {
  923. struct target *t = tlist->target;
  924. riscv_info_t *i = riscv_info(t);
  925. if (i->prepped) {
  926. if (halt_go(t) != ERROR_OK)
  927. result = ERROR_FAIL;
  928. }
  929. }
  930. for (struct target_list *tlist = target->head; tlist; tlist = tlist->next) {
  931. struct target *t = tlist->target;
  932. if (halt_finish(t) != ERROR_OK)
  933. return ERROR_FAIL;
  934. }
  935. } else {
  936. if (halt_prep(target) != ERROR_OK)
  937. result = ERROR_FAIL;
  938. if (halt_go(target) != ERROR_OK)
  939. result = ERROR_FAIL;
  940. if (halt_finish(target) != ERROR_OK)
  941. return ERROR_FAIL;
  942. }
  943. if (riscv_rtos_enabled(target)) {
  944. if (r->rtos_hartid != -1) {
  945. LOG_DEBUG("halt requested on RTOS hartid %d", r->rtos_hartid);
  946. target->rtos->current_threadid = r->rtos_hartid + 1;
  947. target->rtos->current_thread = r->rtos_hartid + 1;
  948. } else
  949. LOG_DEBUG("halt requested, but no known RTOS hartid");
  950. }
  951. return result;
  952. }
  953. static int riscv_assert_reset(struct target *target)
  954. {
  955. LOG_DEBUG("[%d]", target->coreid);
  956. struct target_type *tt = get_target_type(target);
  957. riscv_invalidate_register_cache(target);
  958. return tt->assert_reset(target);
  959. }
  960. static int riscv_deassert_reset(struct target *target)
  961. {
  962. LOG_DEBUG("[%d]", target->coreid);
  963. struct target_type *tt = get_target_type(target);
  964. return tt->deassert_reset(target);
  965. }
  966. int riscv_resume_prep_all_harts(struct target *target)
  967. {
  968. RISCV_INFO(r);
  969. for (int i = 0; i < riscv_count_harts(target); ++i) {
  970. if (!riscv_hart_enabled(target, i))
  971. continue;
  972. LOG_DEBUG("prep hart %d", i);
  973. if (riscv_set_current_hartid(target, i) != ERROR_OK)
  974. return ERROR_FAIL;
  975. if (riscv_is_halted(target)) {
  976. if (r->resume_prep(target) != ERROR_OK)
  977. return ERROR_FAIL;
  978. } else {
  979. LOG_DEBUG(" hart %d requested resume, but was already resumed", i);
  980. }
  981. }
  982. LOG_DEBUG("[%d] mark as prepped", target->coreid);
  983. r->prepped = true;
  984. return ERROR_OK;
  985. }
  986. /* state must be riscv_reg_t state[RISCV_MAX_HWBPS] = {0}; */
  987. static int disable_triggers(struct target *target, riscv_reg_t *state)
  988. {
  989. RISCV_INFO(r);
  990. LOG_DEBUG("deal with triggers");
  991. if (riscv_enumerate_triggers(target) != ERROR_OK)
  992. return ERROR_FAIL;
  993. int hartid = riscv_current_hartid(target);
  994. if (r->manual_hwbp_set) {
  995. /* Look at every trigger that may have been set. */
  996. riscv_reg_t tselect;
  997. if (riscv_get_register(target, &tselect, GDB_REGNO_TSELECT) != ERROR_OK)
  998. return ERROR_FAIL;
  999. for (unsigned t = 0; t < r->trigger_count[hartid]; t++) {
  1000. if (riscv_set_register(target, GDB_REGNO_TSELECT, t) != ERROR_OK)
  1001. return ERROR_FAIL;
  1002. riscv_reg_t tdata1;
  1003. if (riscv_get_register(target, &tdata1, GDB_REGNO_TDATA1) != ERROR_OK)
  1004. return ERROR_FAIL;
  1005. if (tdata1 & MCONTROL_DMODE(riscv_xlen(target))) {
  1006. state[t] = tdata1;
  1007. if (riscv_set_register(target, GDB_REGNO_TDATA1, 0) != ERROR_OK)
  1008. return ERROR_FAIL;
  1009. }
  1010. }
  1011. if (riscv_set_register(target, GDB_REGNO_TSELECT, tselect) != ERROR_OK)
  1012. return ERROR_FAIL;
  1013. } else {
  1014. /* Just go through the triggers we manage. */
  1015. struct watchpoint *watchpoint = target->watchpoints;
  1016. int i = 0;
  1017. while (watchpoint) {
  1018. LOG_DEBUG("watchpoint %d: set=%d", i, watchpoint->set);
  1019. state[i] = watchpoint->set;
  1020. if (watchpoint->set) {
  1021. if (riscv_remove_watchpoint(target, watchpoint) != ERROR_OK)
  1022. return ERROR_FAIL;
  1023. }
  1024. watchpoint = watchpoint->next;
  1025. i++;
  1026. }
  1027. }
  1028. return ERROR_OK;
  1029. }
  1030. static int enable_triggers(struct target *target, riscv_reg_t *state)
  1031. {
  1032. RISCV_INFO(r);
  1033. int hartid = riscv_current_hartid(target);
  1034. if (r->manual_hwbp_set) {
  1035. /* Look at every trigger that may have been set. */
  1036. riscv_reg_t tselect;
  1037. if (riscv_get_register(target, &tselect, GDB_REGNO_TSELECT) != ERROR_OK)
  1038. return ERROR_FAIL;
  1039. for (unsigned t = 0; t < r->trigger_count[hartid]; t++) {
  1040. if (state[t] != 0) {
  1041. if (riscv_set_register(target, GDB_REGNO_TSELECT, t) != ERROR_OK)
  1042. return ERROR_FAIL;
  1043. if (riscv_set_register(target, GDB_REGNO_TDATA1, state[t]) != ERROR_OK)
  1044. return ERROR_FAIL;
  1045. }
  1046. }
  1047. if (riscv_set_register(target, GDB_REGNO_TSELECT, tselect) != ERROR_OK)
  1048. return ERROR_FAIL;
  1049. } else {
  1050. struct watchpoint *watchpoint = target->watchpoints;
  1051. int i = 0;
  1052. while (watchpoint) {
  1053. LOG_DEBUG("watchpoint %d: cleared=%" PRId64, i, state[i]);
  1054. if (state[i]) {
  1055. if (riscv_add_watchpoint(target, watchpoint) != ERROR_OK)
  1056. return ERROR_FAIL;
  1057. }
  1058. watchpoint = watchpoint->next;
  1059. i++;
  1060. }
  1061. }
  1062. return ERROR_OK;
  1063. }
  1064. /**
  1065. * Get everything ready to resume.
  1066. */
  1067. static int resume_prep(struct target *target, int current,
  1068. target_addr_t address, int handle_breakpoints, int debug_execution)
  1069. {
  1070. RISCV_INFO(r);
  1071. LOG_DEBUG("[%d]", target->coreid);
  1072. if (!current)
  1073. riscv_set_register(target, GDB_REGNO_PC, address);
  1074. if (target->debug_reason == DBG_REASON_WATCHPOINT) {
  1075. /* To be able to run off a trigger, disable all the triggers, step, and
  1076. * then resume as usual. */
  1077. riscv_reg_t trigger_state[RISCV_MAX_HWBPS] = {0};
  1078. if (disable_triggers(target, trigger_state) != ERROR_OK)
  1079. return ERROR_FAIL;
  1080. if (old_or_new_riscv_step(target, true, 0, false) != ERROR_OK)
  1081. return ERROR_FAIL;
  1082. if (enable_triggers(target, trigger_state) != ERROR_OK)
  1083. return ERROR_FAIL;
  1084. }
  1085. if (r->is_halted) {
  1086. if (riscv_resume_prep_all_harts(target) != ERROR_OK)
  1087. return ERROR_FAIL;
  1088. }
  1089. LOG_DEBUG("[%d] mark as prepped", target->coreid);
  1090. r->prepped = true;
  1091. return ERROR_OK;
  1092. }
  1093. /**
  1094. * Resume all the harts that have been prepped, as close to instantaneous as
  1095. * possible.
  1096. */
  1097. static int resume_go(struct target *target, int current,
  1098. target_addr_t address, int handle_breakpoints, int debug_execution)
  1099. {
  1100. riscv_info_t *r = riscv_info(target);
  1101. int result;
  1102. if (!r->is_halted) {
  1103. struct target_type *tt = get_target_type(target);
  1104. result = tt->resume(target, current, address, handle_breakpoints,
  1105. debug_execution);
  1106. } else {
  1107. result = riscv_resume_go_all_harts(target);
  1108. }
  1109. return result;
  1110. }
  1111. static int resume_finish(struct target *target)
  1112. {
  1113. register_cache_invalidate(target->reg_cache);
  1114. target->state = TARGET_RUNNING;
  1115. target->debug_reason = DBG_REASON_NOTHALTED;
  1116. return target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  1117. }
  1118. /**
  1119. * @par single_hart When true, only resume a single hart even if SMP is
  1120. * configured. This is used to run algorithms on just one hart.
  1121. */
  1122. int riscv_resume(
  1123. struct target *target,
  1124. int current,
  1125. target_addr_t address,
  1126. int handle_breakpoints,
  1127. int debug_execution,
  1128. bool single_hart)
  1129. {
  1130. LOG_DEBUG("handle_breakpoints=%d", handle_breakpoints);
  1131. int result = ERROR_OK;
  1132. if (target->smp && !single_hart) {
  1133. for (struct target_list *tlist = target->head; tlist; tlist = tlist->next) {
  1134. struct target *t = tlist->target;
  1135. if (resume_prep(t, current, address, handle_breakpoints,
  1136. debug_execution) != ERROR_OK)
  1137. result = ERROR_FAIL;
  1138. }
  1139. for (struct target_list *tlist = target->head; tlist; tlist = tlist->next) {
  1140. struct target *t = tlist->target;
  1141. riscv_info_t *i = riscv_info(t);
  1142. if (i->prepped) {
  1143. if (resume_go(t, current, address, handle_breakpoints,
  1144. debug_execution) != ERROR_OK)
  1145. result = ERROR_FAIL;
  1146. }
  1147. }
  1148. for (struct target_list *tlist = target->head; tlist; tlist = tlist->next) {
  1149. struct target *t = tlist->target;
  1150. if (resume_finish(t) != ERROR_OK)
  1151. return ERROR_FAIL;
  1152. }
  1153. } else {
  1154. if (resume_prep(target, current, address, handle_breakpoints,
  1155. debug_execution) != ERROR_OK)
  1156. result = ERROR_FAIL;
  1157. if (resume_go(target, current, address, handle_breakpoints,
  1158. debug_execution) != ERROR_OK)
  1159. result = ERROR_FAIL;
  1160. if (resume_finish(target) != ERROR_OK)
  1161. return ERROR_FAIL;
  1162. }
  1163. return result;
  1164. }
  1165. static int riscv_target_resume(struct target *target, int current, target_addr_t address,
  1166. int handle_breakpoints, int debug_execution)
  1167. {
  1168. return riscv_resume(target, current, address, handle_breakpoints,
  1169. debug_execution, false);
  1170. }
  1171. static int riscv_select_current_hart(struct target *target)
  1172. {
  1173. RISCV_INFO(r);
  1174. if (riscv_rtos_enabled(target)) {
  1175. if (r->rtos_hartid == -1)
  1176. r->rtos_hartid = target->rtos->current_threadid - 1;
  1177. return riscv_set_current_hartid(target, r->rtos_hartid);
  1178. } else
  1179. return riscv_set_current_hartid(target, target->coreid);
  1180. }
  1181. static int riscv_mmu(struct target *target, int *enabled)
  1182. {
  1183. if (!riscv_enable_virt2phys) {
  1184. *enabled = 0;
  1185. return ERROR_OK;
  1186. }
  1187. if (riscv_rtos_enabled(target))
  1188. riscv_set_current_hartid(target, target->rtos->current_thread - 1);
  1189. /* Don't use MMU in explicit or effective M (machine) mode */
  1190. riscv_reg_t priv;
  1191. if (riscv_get_register(target, &priv, GDB_REGNO_PRIV) != ERROR_OK) {
  1192. LOG_ERROR("Failed to read priv register.");
  1193. return ERROR_FAIL;
  1194. }
  1195. riscv_reg_t mstatus;
  1196. if (riscv_get_register(target, &mstatus, GDB_REGNO_MSTATUS) != ERROR_OK) {
  1197. LOG_ERROR("Failed to read mstatus register.");
  1198. return ERROR_FAIL;
  1199. }
  1200. if ((get_field(mstatus, MSTATUS_MPRV) ? get_field(mstatus, MSTATUS_MPP) : priv) == PRV_M) {
  1201. LOG_DEBUG("SATP/MMU ignored in Machine mode (mstatus=0x%" PRIx64 ").", mstatus);
  1202. *enabled = 0;
  1203. return ERROR_OK;
  1204. }
  1205. riscv_reg_t satp;
  1206. if (riscv_get_register(target, &satp, GDB_REGNO_SATP) != ERROR_OK) {
  1207. LOG_DEBUG("Couldn't read SATP.");
  1208. /* If we can't read SATP, then there must not be an MMU. */
  1209. *enabled = 0;
  1210. return ERROR_OK;
  1211. }
  1212. if (get_field(satp, RISCV_SATP_MODE(riscv_xlen(target))) == SATP_MODE_OFF) {
  1213. LOG_DEBUG("MMU is disabled.");
  1214. *enabled = 0;
  1215. } else {
  1216. LOG_DEBUG("MMU is enabled.");
  1217. *enabled = 1;
  1218. }
  1219. return ERROR_OK;
  1220. }
  1221. static int riscv_address_translate(struct target *target,
  1222. target_addr_t virtual, target_addr_t *physical)
  1223. {
  1224. RISCV_INFO(r);
  1225. riscv_reg_t satp_value;
  1226. int mode;
  1227. uint64_t ppn_value;
  1228. target_addr_t table_address;
  1229. virt2phys_info_t *info;
  1230. uint64_t pte = 0;
  1231. int i;
  1232. if (riscv_rtos_enabled(target))
  1233. riscv_set_current_hartid(target, target->rtos->current_thread - 1);
  1234. int result = riscv_get_register(target, &satp_value, GDB_REGNO_SATP);
  1235. if (result != ERROR_OK)
  1236. return result;
  1237. unsigned xlen = riscv_xlen(target);
  1238. mode = get_field(satp_value, RISCV_SATP_MODE(xlen));
  1239. switch (mode) {
  1240. case SATP_MODE_SV32:
  1241. info = &sv32;
  1242. break;
  1243. case SATP_MODE_SV39:
  1244. info = &sv39;
  1245. break;
  1246. case SATP_MODE_SV48:
  1247. info = &sv48;
  1248. break;
  1249. case SATP_MODE_OFF:
  1250. LOG_ERROR("No translation or protection." \
  1251. " (satp: 0x%" PRIx64 ")", satp_value);
  1252. return ERROR_FAIL;
  1253. default:
  1254. LOG_ERROR("The translation mode is not supported." \
  1255. " (satp: 0x%" PRIx64 ")", satp_value);
  1256. return ERROR_FAIL;
  1257. }
  1258. LOG_DEBUG("virtual=0x%" TARGET_PRIxADDR "; mode=%s", virtual, info->name);
  1259. /* verify bits xlen-1:va_bits-1 are all equal */
  1260. target_addr_t mask = ((target_addr_t)1 << (xlen - (info->va_bits - 1))) - 1;
  1261. target_addr_t masked_msbs = (virtual >> (info->va_bits - 1)) & mask;
  1262. if (masked_msbs != 0 && masked_msbs != mask) {
  1263. LOG_ERROR("Virtual address 0x%" TARGET_PRIxADDR " is not sign-extended "
  1264. "for %s mode.", virtual, info->name);
  1265. return ERROR_FAIL;
  1266. }
  1267. ppn_value = get_field(satp_value, RISCV_SATP_PPN(xlen));
  1268. table_address = ppn_value << RISCV_PGSHIFT;
  1269. i = info->level - 1;
  1270. while (i >= 0) {
  1271. uint64_t vpn = virtual >> info->vpn_shift[i];
  1272. vpn &= info->vpn_mask[i];
  1273. target_addr_t pte_address = table_address +
  1274. (vpn << info->pte_shift);
  1275. uint8_t buffer[8];
  1276. assert(info->pte_shift <= 3);
  1277. int retval = r->read_memory(target, pte_address,
  1278. 4, (1 << info->pte_shift) / 4, buffer, 4);
  1279. if (retval != ERROR_OK)
  1280. return ERROR_FAIL;
  1281. if (info->pte_shift == 2)
  1282. pte = buf_get_u32(buffer, 0, 32);
  1283. else
  1284. pte = buf_get_u64(buffer, 0, 64);
  1285. LOG_DEBUG("i=%d; PTE @0x%" TARGET_PRIxADDR " = 0x%" PRIx64, i,
  1286. pte_address, pte);
  1287. if (!(pte & PTE_V) || (!(pte & PTE_R) && (pte & PTE_W)))
  1288. return ERROR_FAIL;
  1289. if ((pte & PTE_R) || (pte & PTE_X)) /* Found leaf PTE. */
  1290. break;
  1291. i--;
  1292. if (i < 0)
  1293. break;
  1294. ppn_value = pte >> PTE_PPN_SHIFT;
  1295. table_address = ppn_value << RISCV_PGSHIFT;
  1296. }
  1297. if (i < 0) {
  1298. LOG_ERROR("Couldn't find the PTE.");
  1299. return ERROR_FAIL;
  1300. }
  1301. /* Make sure to clear out the high bits that may be set. */
  1302. *physical = virtual & (((target_addr_t)1 << info->va_bits) - 1);
  1303. while (i < info->level) {
  1304. ppn_value = pte >> info->pte_ppn_shift[i];
  1305. ppn_value &= info->pte_ppn_mask[i];
  1306. *physical &= ~(((target_addr_t)info->pa_ppn_mask[i]) <<
  1307. info->pa_ppn_shift[i]);
  1308. *physical |= (ppn_value << info->pa_ppn_shift[i]);
  1309. i++;
  1310. }
  1311. LOG_DEBUG("0x%" TARGET_PRIxADDR " -> 0x%" TARGET_PRIxADDR, virtual,
  1312. *physical);
  1313. return ERROR_OK;
  1314. }
  1315. static int riscv_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
  1316. {
  1317. int enabled;
  1318. if (riscv_mmu(target, &enabled) == ERROR_OK) {
  1319. if (!enabled)
  1320. return ERROR_FAIL;
  1321. if (riscv_address_translate(target, virtual, physical) == ERROR_OK)
  1322. return ERROR_OK;
  1323. }
  1324. return ERROR_FAIL;
  1325. }
  1326. static int riscv_read_phys_memory(struct target *target, target_addr_t phys_address,
  1327. uint32_t size, uint32_t count, uint8_t *buffer)
  1328. {
  1329. RISCV_INFO(r);
  1330. if (riscv_select_current_hart(target) != ERROR_OK)
  1331. return ERROR_FAIL;
  1332. return r->read_memory(target, phys_address, size, count, buffer, size);
  1333. }
  1334. static int riscv_read_memory(struct target *target, target_addr_t address,
  1335. uint32_t size, uint32_t count, uint8_t *buffer)
  1336. {
  1337. if (count == 0) {
  1338. LOG_WARNING("0-length read from 0x%" TARGET_PRIxADDR, address);
  1339. return ERROR_OK;
  1340. }
  1341. if (riscv_select_current_hart(target) != ERROR_OK)
  1342. return ERROR_FAIL;
  1343. target_addr_t physical_addr;
  1344. if (target->type->virt2phys(target, address, &physical_addr) == ERROR_OK)
  1345. address = physical_addr;
  1346. RISCV_INFO(r);
  1347. return r->read_memory(target, address, size, count, buffer, size);
  1348. }
  1349. static int riscv_write_phys_memory(struct target *target, target_addr_t phys_address,
  1350. uint32_t size, uint32_t count, const uint8_t *buffer)
  1351. {
  1352. if (riscv_select_current_hart(target) != ERROR_OK)
  1353. return ERROR_FAIL;
  1354. struct target_type *tt = get_target_type(target);
  1355. return tt->write_memory(target, phys_address, size, count, buffer);
  1356. }
  1357. static int riscv_write_memory(struct target *target, target_addr_t address,
  1358. uint32_t size, uint32_t count, const uint8_t *buffer)
  1359. {
  1360. if (count == 0) {
  1361. LOG_WARNING("0-length write to 0x%" TARGET_PRIxADDR, address);
  1362. return ERROR_OK;
  1363. }
  1364. if (riscv_select_current_hart(target) != ERROR_OK)
  1365. return ERROR_FAIL;
  1366. target_addr_t physical_addr;
  1367. if (target->type->virt2phys(target, address, &physical_addr) == ERROR_OK)
  1368. address = physical_addr;
  1369. struct target_type *tt = get_target_type(target);
  1370. return tt->write_memory(target, address, size, count, buffer);
  1371. }
  1372. const char *riscv_get_gdb_arch(struct target *target)
  1373. {
  1374. switch (riscv_xlen(target)) {
  1375. case 32:
  1376. return "riscv:rv32";
  1377. case 64:
  1378. return "riscv:rv64";
  1379. }
  1380. LOG_ERROR("Unsupported xlen: %d", riscv_xlen(target));
  1381. return NULL;
  1382. }
  1383. static int riscv_get_gdb_reg_list_internal(struct target *target,
  1384. struct reg **reg_list[], int *reg_list_size,
  1385. enum target_register_class reg_class, bool read)
  1386. {
  1387. RISCV_INFO(r);
  1388. LOG_DEBUG("rtos_hartid=%d, current_hartid=%d, reg_class=%d, read=%d",
  1389. r->rtos_hartid, r->current_hartid, reg_class, read);
  1390. if (!target->reg_cache) {
  1391. LOG_ERROR("Target not initialized. Return ERROR_FAIL.");
  1392. return ERROR_FAIL;
  1393. }
  1394. if (riscv_select_current_hart(target) != ERROR_OK)
  1395. return ERROR_FAIL;
  1396. switch (reg_class) {
  1397. case REG_CLASS_GENERAL:
  1398. *reg_list_size = 33;
  1399. break;
  1400. case REG_CLASS_ALL:
  1401. *reg_list_size = target->reg_cache->num_regs;
  1402. break;
  1403. default:
  1404. LOG_ERROR("Unsupported reg_class: %d", reg_class);
  1405. return ERROR_FAIL;
  1406. }
  1407. *reg_list = calloc(*reg_list_size, sizeof(struct reg *));
  1408. if (!*reg_list)
  1409. return ERROR_FAIL;
  1410. for (int i = 0; i < *reg_list_size; i++) {
  1411. assert(!target->reg_cache->reg_list[i].valid ||
  1412. target->reg_cache->reg_list[i].size > 0);
  1413. (*reg_list)[i] = &target->reg_cache->reg_list[i];
  1414. if (read &&
  1415. target->reg_cache->reg_list[i].exist &&
  1416. !target->reg_cache->reg_list[i].valid) {
  1417. if (target->reg_cache->reg_list[i].type->get(
  1418. &target->reg_cache->reg_list[i]) != ERROR_OK)
  1419. return ERROR_FAIL;
  1420. }
  1421. }
  1422. return ERROR_OK;
  1423. }
  1424. static int riscv_get_gdb_reg_list_noread(struct target *target,
  1425. struct reg **reg_list[], int *reg_list_size,
  1426. enum target_register_class reg_class)
  1427. {
  1428. return riscv_get_gdb_reg_list_internal(target, reg_list, reg_list_size,
  1429. reg_class, false);
  1430. }
  1431. static int riscv_get_gdb_reg_list(struct target *target,
  1432. struct reg **reg_list[], int *reg_list_size,
  1433. enum target_register_class reg_class)
  1434. {
  1435. return riscv_get_gdb_reg_list_internal(target, reg_list, reg_list_size,
  1436. reg_class, true);
  1437. }
  1438. static int riscv_arch_state(struct target *target)
  1439. {
  1440. struct target_type *tt = get_target_type(target);
  1441. return tt->arch_state(target);
  1442. }
  1443. /* Algorithm must end with a software breakpoint instruction. */
  1444. static int riscv_run_algorithm(struct target *target, int num_mem_params,
  1445. struct mem_param *mem_params, int num_reg_params,
  1446. struct reg_param *reg_params, target_addr_t entry_point,
  1447. target_addr_t exit_point, int timeout_ms, void *arch_info)
  1448. {
  1449. riscv_info_t *info = (riscv_info_t *) target->arch_info;
  1450. int hartid = riscv_current_hartid(target);
  1451. if (num_mem_params > 0) {
  1452. LOG_ERROR("Memory parameters are not supported for RISC-V algorithms.");
  1453. return ERROR_FAIL;
  1454. }
  1455. if (target->state != TARGET_HALTED) {
  1456. LOG_WARNING("target not halted");
  1457. return ERROR_TARGET_NOT_HALTED;
  1458. }
  1459. /* Save registers */
  1460. struct reg *reg_pc = register_get_by_name(target->reg_cache, "pc", true);
  1461. if (!reg_pc || reg_pc->type->get(reg_pc) != ERROR_OK)
  1462. return ERROR_FAIL;
  1463. uint64_t saved_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
  1464. LOG_DEBUG("saved_pc=0x%" PRIx64, saved_pc);
  1465. uint64_t saved_regs[32];
  1466. for (int i = 0; i < num_reg_params; i++) {
  1467. LOG_DEBUG("save %s", reg_params[i].reg_name);
  1468. struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
  1469. if (!r) {
  1470. LOG_ERROR("Couldn't find register named '%s'", reg_params[i].reg_name);
  1471. return ERROR_FAIL;
  1472. }
  1473. if (r->size != reg_params[i].size) {
  1474. LOG_ERROR("Register %s is %d bits instead of %d bits.",
  1475. reg_params[i].reg_name, r->size, reg_params[i].size);
  1476. return ERROR_FAIL;
  1477. }
  1478. if (r->number > GDB_REGNO_XPR31) {
  1479. LOG_ERROR("Only GPRs can be use as argument registers.");
  1480. return ERROR_FAIL;
  1481. }
  1482. if (r->type->get(r) != ERROR_OK)
  1483. return ERROR_FAIL;
  1484. saved_regs[r->number] = buf_get_u64(r->value, 0, r->size);
  1485. if (reg_params[i].direction == PARAM_OUT || reg_params[i].direction == PARAM_IN_OUT) {
  1486. if (r->type->set(r, reg_params[i].value) != ERROR_OK)
  1487. return ERROR_FAIL;
  1488. }
  1489. }
  1490. /* Disable Interrupts before attempting to run the algorithm. */
  1491. uint64_t current_mstatus;
  1492. uint8_t mstatus_bytes[8] = { 0 };
  1493. LOG_DEBUG("Disabling Interrupts");
  1494. struct reg *reg_mstatus = register_get_by_name(target->reg_cache,
  1495. "mstatus", true);
  1496. if (!reg_mstatus) {
  1497. LOG_ERROR("Couldn't find mstatus!");
  1498. return ERROR_FAIL;
  1499. }
  1500. reg_mstatus->type->get(reg_mstatus);
  1501. current_mstatus = buf_get_u64(reg_mstatus->value, 0, reg_mstatus->size);
  1502. uint64_t ie_mask = MSTATUS_MIE | MSTATUS_HIE | MSTATUS_SIE | MSTATUS_UIE;
  1503. buf_set_u64(mstatus_bytes, 0, info->xlen[0], set_field(current_mstatus,
  1504. ie_mask, 0));
  1505. reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
  1506. /* Run algorithm */
  1507. LOG_DEBUG("resume at 0x%" TARGET_PRIxADDR, entry_point);
  1508. if (riscv_resume(target, 0, entry_point, 0, 0, true) != ERROR_OK)
  1509. return ERROR_FAIL;
  1510. int64_t start = timeval_ms();
  1511. while (target->state != TARGET_HALTED) {
  1512. LOG_DEBUG("poll()");
  1513. int64_t now = timeval_ms();
  1514. if (now - start > timeout_ms) {
  1515. LOG_ERROR("Algorithm timed out after %" PRId64 " ms.", now - start);
  1516. riscv_halt(target);
  1517. old_or_new_riscv_poll(target);
  1518. enum gdb_regno regnums[] = {
  1519. GDB_REGNO_RA, GDB_REGNO_SP, GDB_REGNO_GP, GDB_REGNO_TP,
  1520. GDB_REGNO_T0, GDB_REGNO_T1, GDB_REGNO_T2, GDB_REGNO_FP,
  1521. GDB_REGNO_S1, GDB_REGNO_A0, GDB_REGNO_A1, GDB_REGNO_A2,
  1522. GDB_REGNO_A3, GDB_REGNO_A4, GDB_REGNO_A5, GDB_REGNO_A6,
  1523. GDB_REGNO_A7, GDB_REGNO_S2, GDB_REGNO_S3, GDB_REGNO_S4,
  1524. GDB_REGNO_S5, GDB_REGNO_S6, GDB_REGNO_S7, GDB_REGNO_S8,
  1525. GDB_REGNO_S9, GDB_REGNO_S10, GDB_REGNO_S11, GDB_REGNO_T3,
  1526. GDB_REGNO_T4, GDB_REGNO_T5, GDB_REGNO_T6,
  1527. GDB_REGNO_PC,
  1528. GDB_REGNO_MSTATUS, GDB_REGNO_MEPC, GDB_REGNO_MCAUSE,
  1529. };
  1530. for (unsigned i = 0; i < ARRAY_SIZE(regnums); i++) {
  1531. enum gdb_regno regno = regnums[i];
  1532. riscv_reg_t reg_value;
  1533. if (riscv_get_register(target, &reg_value, regno) != ERROR_OK)
  1534. break;
  1535. LOG_ERROR("%s = 0x%" PRIx64, gdb_regno_name(regno), reg_value);
  1536. }
  1537. return ERROR_TARGET_TIMEOUT;
  1538. }
  1539. int result = old_or_new_riscv_poll(target);
  1540. if (result != ERROR_OK)
  1541. return result;
  1542. }
  1543. /* The current hart id might have been changed in poll(). */
  1544. if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
  1545. return ERROR_FAIL;
  1546. if (reg_pc->type->get(reg_pc) != ERROR_OK)
  1547. return ERROR_FAIL;
  1548. uint64_t final_pc = buf_get_u64(reg_pc->value, 0, reg_pc->size);
  1549. if (exit_point && final_pc != exit_point) {
  1550. LOG_ERROR("PC ended up at 0x%" PRIx64 " instead of 0x%"
  1551. TARGET_PRIxADDR, final_pc, exit_point);
  1552. return ERROR_FAIL;
  1553. }
  1554. /* Restore Interrupts */
  1555. LOG_DEBUG("Restoring Interrupts");
  1556. buf_set_u64(mstatus_bytes, 0, info->xlen[0], current_mstatus);
  1557. reg_mstatus->type->set(reg_mstatus, mstatus_bytes);
  1558. /* Restore registers */
  1559. uint8_t buf[8] = { 0 };
  1560. buf_set_u64(buf, 0, info->xlen[0], saved_pc);
  1561. if (reg_pc->type->set(reg_pc, buf) != ERROR_OK)
  1562. return ERROR_FAIL;
  1563. for (int i = 0; i < num_reg_params; i++) {
  1564. if (reg_params[i].direction == PARAM_IN ||
  1565. reg_params[i].direction == PARAM_IN_OUT) {
  1566. struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
  1567. if (r->type->get(r) != ERROR_OK) {
  1568. LOG_ERROR("get(%s) failed", r->name);
  1569. return ERROR_FAIL;
  1570. }
  1571. buf_cpy(r->value, reg_params[i].value, reg_params[i].size);
  1572. }
  1573. LOG_DEBUG("restore %s", reg_params[i].reg_name);
  1574. struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, false);
  1575. buf_set_u64(buf, 0, info->xlen[0], saved_regs[r->number]);
  1576. if (r->type->set(r, buf) != ERROR_OK) {
  1577. LOG_ERROR("set(%s) failed", r->name);
  1578. return ERROR_FAIL;
  1579. }
  1580. }
  1581. return ERROR_OK;
  1582. }
  1583. static int riscv_checksum_memory(struct target *target,
  1584. target_addr_t address, uint32_t count,
  1585. uint32_t *checksum)
  1586. {
  1587. struct working_area *crc_algorithm;
  1588. struct reg_param reg_params[2];
  1589. int retval;
  1590. LOG_DEBUG("address=0x%" TARGET_PRIxADDR "; count=0x%" PRIx32, address, count);
  1591. static const uint8_t riscv32_crc_code[] = {
  1592. #include "../../contrib/loaders/checksum/riscv32_crc.inc"
  1593. };
  1594. static const uint8_t riscv64_crc_code[] = {
  1595. #include "../../contrib/loaders/checksum/riscv64_crc.inc"
  1596. };
  1597. static const uint8_t *crc_code;
  1598. unsigned xlen = riscv_xlen(target);
  1599. unsigned crc_code_size;
  1600. if (xlen == 32) {
  1601. crc_code = riscv32_crc_code;
  1602. crc_code_size = sizeof(riscv32_crc_code);
  1603. } else {
  1604. crc_code = riscv64_crc_code;
  1605. crc_code_size = sizeof(riscv64_crc_code);
  1606. }
  1607. if (count < crc_code_size * 4) {
  1608. /* Don't use the algorithm for relatively small buffers. It's faster
  1609. * just to read the memory. target_checksum_memory() will take care of
  1610. * that if we fail. */
  1611. return ERROR_FAIL;
  1612. }
  1613. retval = target_alloc_working_area(target, crc_code_size, &crc_algorithm);
  1614. if (retval != ERROR_OK)
  1615. return retval;
  1616. if (crc_algorithm->address + crc_algorithm->size > address &&
  1617. crc_algorithm->address < address + count) {
  1618. /* Region to checksum overlaps with the work area we've been assigned.
  1619. * Bail. (Would be better to manually checksum what we read there, and
  1620. * use the algorithm for the rest.) */
  1621. target_free_working_area(target, crc_algorithm);
  1622. return ERROR_FAIL;
  1623. }
  1624. retval = target_write_buffer(target, crc_algorithm->address, crc_code_size,
  1625. crc_code);
  1626. if (retval != ERROR_OK) {
  1627. LOG_ERROR("Failed to write code to " TARGET_ADDR_FMT ": %d",
  1628. crc_algorithm->address, retval);
  1629. target_free_working_area(target, crc_algorithm);
  1630. return retval;
  1631. }
  1632. init_reg_param(&reg_params[0], "a0", xlen, PARAM_IN_OUT);
  1633. init_reg_param(&reg_params[1], "a1", xlen, PARAM_OUT);
  1634. buf_set_u64(reg_params[0].value, 0, xlen, address);
  1635. buf_set_u64(reg_params[1].value, 0, xlen, count);
  1636. /* 20 second timeout/megabyte */
  1637. int timeout = 20000 * (1 + (count / (1024 * 1024)));
  1638. retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
  1639. crc_algorithm->address,
  1640. 0, /* Leave exit point unspecified because we don't know. */
  1641. timeout, NULL);
  1642. if (retval == ERROR_OK)
  1643. *checksum = buf_get_u32(reg_params[0].value, 0, 32);
  1644. else
  1645. LOG_ERROR("error executing RISC-V CRC algorithm");
  1646. destroy_reg_param(&reg_params[0]);
  1647. destroy_reg_param(&reg_params[1]);
  1648. target_free_working_area(target, crc_algorithm);
  1649. LOG_DEBUG("checksum=0x%" PRIx32 ", result=%d", *checksum, retval);
  1650. return retval;
  1651. }
  1652. /*** OpenOCD Helper Functions ***/
  1653. enum riscv_poll_hart {
  1654. RPH_NO_CHANGE,
  1655. RPH_DISCOVERED_HALTED,
  1656. RPH_DISCOVERED_RUNNING,
  1657. RPH_ERROR
  1658. };
  1659. static enum riscv_poll_hart riscv_poll_hart(struct target *target, int hartid)
  1660. {
  1661. RISCV_INFO(r);
  1662. if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
  1663. return RPH_ERROR;
  1664. LOG_DEBUG("polling hart %d, target->state=%d", hartid, target->state);
  1665. /* If OpenOCD thinks we're running but this hart is halted then it's time
  1666. * to raise an event. */
  1667. bool halted = riscv_is_halted(target);
  1668. if (target->state != TARGET_HALTED && halted) {
  1669. LOG_DEBUG(" triggered a halt");
  1670. r->on_halt(target);
  1671. return RPH_DISCOVERED_HALTED;
  1672. } else if (target->state != TARGET_RUNNING && !halted) {
  1673. LOG_DEBUG(" triggered running");
  1674. target->state = TARGET_RUNNING;
  1675. target->debug_reason = DBG_REASON_NOTHALTED;
  1676. return RPH_DISCOVERED_RUNNING;
  1677. }
  1678. return RPH_NO_CHANGE;
  1679. }
  1680. int set_debug_reason(struct target *target, enum riscv_halt_reason halt_reason)
  1681. {
  1682. switch (halt_reason) {
  1683. case RISCV_HALT_BREAKPOINT:
  1684. target->debug_reason = DBG_REASON_BREAKPOINT;
  1685. break;
  1686. case RISCV_HALT_TRIGGER:
  1687. target->debug_reason = DBG_REASON_WATCHPOINT;
  1688. break;
  1689. case RISCV_HALT_INTERRUPT:
  1690. case RISCV_HALT_GROUP:
  1691. target->debug_reason = DBG_REASON_DBGRQ;
  1692. break;
  1693. case RISCV_HALT_SINGLESTEP:
  1694. target->debug_reason = DBG_REASON_SINGLESTEP;
  1695. break;
  1696. case RISCV_HALT_UNKNOWN:
  1697. target->debug_reason = DBG_REASON_UNDEFINED;
  1698. break;
  1699. case RISCV_HALT_ERROR:
  1700. return ERROR_FAIL;
  1701. }
  1702. LOG_DEBUG("[%s] debug_reason=%d", target_name(target), target->debug_reason);
  1703. return ERROR_OK;
  1704. }
  1705. /*** OpenOCD Interface ***/
  1706. int riscv_openocd_poll(struct target *target)
  1707. {
  1708. LOG_DEBUG("polling all harts");
  1709. int halted_hart = -1;
  1710. if (riscv_rtos_enabled(target)) {
  1711. /* Check every hart for an event. */
  1712. for (int i = 0; i < riscv_count_harts(target); ++i) {
  1713. enum riscv_poll_hart out = riscv_poll_hart(target, i);
  1714. switch (out) {
  1715. case RPH_NO_CHANGE:
  1716. case RPH_DISCOVERED_RUNNING:
  1717. continue;
  1718. case RPH_DISCOVERED_HALTED:
  1719. halted_hart = i;
  1720. break;
  1721. case RPH_ERROR:
  1722. return ERROR_FAIL;
  1723. }
  1724. }
  1725. if (halted_hart == -1) {
  1726. LOG_DEBUG(" no harts just halted, target->state=%d", target->state);
  1727. return ERROR_OK;
  1728. }
  1729. LOG_DEBUG(" hart %d halted", halted_hart);
  1730. target->state = TARGET_HALTED;
  1731. enum riscv_halt_reason halt_reason = riscv_halt_reason(target, halted_hart);
  1732. if (set_debug_reason(target, halt_reason) != ERROR_OK)
  1733. return ERROR_FAIL;
  1734. target->rtos->current_threadid = halted_hart + 1;
  1735. target->rtos->current_thread = halted_hart + 1;
  1736. riscv_set_rtos_hartid(target, halted_hart);
  1737. /* If we're here then at least one hart triggered. That means we want
  1738. * to go and halt _every_ hart (configured with -rtos riscv) in the
  1739. * system, as that's the invariant we hold here. Some harts might have
  1740. * already halted (as we're either in single-step mode or they also
  1741. * triggered a breakpoint), so don't attempt to halt those harts.
  1742. * riscv_halt() will do all that for us. */
  1743. riscv_halt(target);
  1744. } else if (target->smp) {
  1745. unsigned halts_discovered = 0;
  1746. unsigned total_targets = 0;
  1747. unsigned should_remain_halted = 0;
  1748. unsigned should_resume = 0;
  1749. unsigned i = 0;
  1750. for (struct target_list *list = target->head; list;
  1751. list = list->next, i++) {
  1752. total_targets++;
  1753. struct target *t = list->target;
  1754. riscv_info_t *r = riscv_info(t);
  1755. enum riscv_poll_hart out = riscv_poll_hart(t, r->current_hartid);
  1756. switch (out) {
  1757. case RPH_NO_CHANGE:
  1758. break;
  1759. case RPH_DISCOVERED_RUNNING:
  1760. t->state = TARGET_RUNNING;
  1761. t->debug_reason = DBG_REASON_NOTHALTED;
  1762. break;
  1763. case RPH_DISCOVERED_HALTED:
  1764. halts_discovered++;
  1765. t->state = TARGET_HALTED;
  1766. enum riscv_halt_reason halt_reason =
  1767. riscv_halt_reason(t, r->current_hartid);
  1768. if (set_debug_reason(t, halt_reason) != ERROR_OK)
  1769. return ERROR_FAIL;
  1770. if (halt_reason == RISCV_HALT_BREAKPOINT) {
  1771. int retval;
  1772. switch (riscv_semihosting(t, &retval)) {
  1773. case SEMI_NONE:
  1774. case SEMI_WAITING:
  1775. /* This hart should remain halted. */
  1776. should_remain_halted++;
  1777. break;
  1778. case SEMI_HANDLED:
  1779. /* This hart should be resumed, along with any other
  1780. * harts that halted due to haltgroups. */
  1781. should_resume++;
  1782. break;
  1783. case SEMI_ERROR:
  1784. return retval;
  1785. }
  1786. } else if (halt_reason != RISCV_HALT_GROUP) {
  1787. should_remain_halted++;
  1788. }
  1789. break;
  1790. case RPH_ERROR:
  1791. return ERROR_FAIL;
  1792. }
  1793. }
  1794. LOG_DEBUG("should_remain_halted=%d, should_resume=%d",
  1795. should_remain_halted, should_resume);
  1796. if (should_remain_halted && should_resume) {
  1797. LOG_WARNING("%d harts should remain halted, and %d should resume.",
  1798. should_remain_halted, should_resume);
  1799. }
  1800. if (should_remain_halted) {
  1801. LOG_DEBUG("halt all");
  1802. riscv_halt(target);
  1803. } else if (should_resume) {
  1804. LOG_DEBUG("resume all");
  1805. riscv_resume(target, true, 0, 0, 0, false);
  1806. }
  1807. return ERROR_OK;
  1808. } else {
  1809. enum riscv_poll_hart out = riscv_poll_hart(target,
  1810. riscv_current_hartid(target));
  1811. if (out == RPH_NO_CHANGE || out == RPH_DISCOVERED_RUNNING)
  1812. return ERROR_OK;
  1813. else if (out == RPH_ERROR)
  1814. return ERROR_FAIL;
  1815. halted_hart = riscv_current_hartid(target);
  1816. LOG_DEBUG(" hart %d halted", halted_hart);
  1817. enum riscv_halt_reason halt_reason = riscv_halt_reason(target, halted_hart);
  1818. if (set_debug_reason(target, halt_reason) != ERROR_OK)
  1819. return ERROR_FAIL;
  1820. target->state = TARGET_HALTED;
  1821. }
  1822. if (target->debug_reason == DBG_REASON_BREAKPOINT) {
  1823. int retval;
  1824. switch (riscv_semihosting(target, &retval)) {
  1825. case SEMI_NONE:
  1826. case SEMI_WAITING:
  1827. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  1828. break;
  1829. case SEMI_HANDLED:
  1830. if (riscv_resume(target, true, 0, 0, 0, false) != ERROR_OK)
  1831. return ERROR_FAIL;
  1832. break;
  1833. case SEMI_ERROR:
  1834. return retval;
  1835. }
  1836. } else {
  1837. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  1838. }
  1839. return ERROR_OK;
  1840. }
  1841. int riscv_openocd_step(struct target *target, int current,
  1842. target_addr_t address, int handle_breakpoints)
  1843. {
  1844. LOG_DEBUG("stepping rtos hart");
  1845. if (!current)
  1846. riscv_set_register(target, GDB_REGNO_PC, address);
  1847. riscv_reg_t trigger_state[RISCV_MAX_HWBPS] = {0};
  1848. if (disable_triggers(target, trigger_state) != ERROR_OK)
  1849. return ERROR_FAIL;
  1850. int out = riscv_step_rtos_hart(target);
  1851. if (out != ERROR_OK) {
  1852. LOG_ERROR("unable to step rtos hart");
  1853. return out;
  1854. }
  1855. register_cache_invalidate(target->reg_cache);
  1856. if (enable_triggers(target, trigger_state) != ERROR_OK)
  1857. return ERROR_FAIL;
  1858. target->state = TARGET_RUNNING;
  1859. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  1860. target->state = TARGET_HALTED;
  1861. target->debug_reason = DBG_REASON_SINGLESTEP;
  1862. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  1863. return out;
  1864. }
  1865. /* Command Handlers */
  1866. COMMAND_HANDLER(riscv_set_command_timeout_sec)
  1867. {
  1868. if (CMD_ARGC != 1) {
  1869. LOG_ERROR("Command takes exactly 1 parameter");
  1870. return ERROR_COMMAND_SYNTAX_ERROR;
  1871. }
  1872. int timeout = atoi(CMD_ARGV[0]);
  1873. if (timeout <= 0) {
  1874. LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
  1875. return ERROR_FAIL;
  1876. }
  1877. riscv_command_timeout_sec = timeout;
  1878. return ERROR_OK;
  1879. }
  1880. COMMAND_HANDLER(riscv_set_reset_timeout_sec)
  1881. {
  1882. if (CMD_ARGC != 1) {
  1883. LOG_ERROR("Command takes exactly 1 parameter");
  1884. return ERROR_COMMAND_SYNTAX_ERROR;
  1885. }
  1886. int timeout = atoi(CMD_ARGV[0]);
  1887. if (timeout <= 0) {
  1888. LOG_ERROR("%s is not a valid integer argument for command.", CMD_ARGV[0]);
  1889. return ERROR_FAIL;
  1890. }
  1891. riscv_reset_timeout_sec = timeout;
  1892. return ERROR_OK;
  1893. }
  1894. COMMAND_HANDLER(riscv_test_compliance) {
  1895. struct target *target = get_current_target(CMD_CTX);
  1896. RISCV_INFO(r);
  1897. if (CMD_ARGC > 0) {
  1898. LOG_ERROR("Command does not take any parameters.");
  1899. return ERROR_COMMAND_SYNTAX_ERROR;
  1900. }
  1901. if (r->test_compliance) {
  1902. return r->test_compliance(target);
  1903. } else {
  1904. LOG_ERROR("This target does not support this command (may implement an older version of the spec).");
  1905. return ERROR_FAIL;
  1906. }
  1907. }
  1908. COMMAND_HANDLER(riscv_set_prefer_sba)
  1909. {
  1910. if (CMD_ARGC != 1) {
  1911. LOG_ERROR("Command takes exactly 1 parameter");
  1912. return ERROR_COMMAND_SYNTAX_ERROR;
  1913. }
  1914. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], riscv_prefer_sba);
  1915. return ERROR_OK;
  1916. }
  1917. COMMAND_HANDLER(riscv_set_enable_virtual)
  1918. {
  1919. if (CMD_ARGC != 1) {
  1920. LOG_ERROR("Command takes exactly 1 parameter");
  1921. return ERROR_COMMAND_SYNTAX_ERROR;
  1922. }
  1923. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], riscv_enable_virtual);
  1924. return ERROR_OK;
  1925. }
  1926. void parse_error(const char *string, char c, unsigned position)
  1927. {
  1928. char buf[position+2];
  1929. for (unsigned i = 0; i < position; i++)
  1930. buf[i] = ' ';
  1931. buf[position] = '^';
  1932. buf[position + 1] = 0;
  1933. LOG_ERROR("Parse error at character %c in:", c);
  1934. LOG_ERROR("%s", string);
  1935. LOG_ERROR("%s", buf);
  1936. }
  1937. int parse_ranges(range_t **ranges, const char **argv)
  1938. {
  1939. for (unsigned pass = 0; pass < 2; pass++) {
  1940. unsigned range = 0;
  1941. unsigned low = 0;
  1942. bool parse_low = true;
  1943. unsigned high = 0;
  1944. for (unsigned i = 0; i == 0 || argv[0][i-1]; i++) {
  1945. char c = argv[0][i];
  1946. if (isspace(c)) {
  1947. /* Ignore whitespace. */
  1948. continue;
  1949. }
  1950. if (parse_low) {
  1951. if (isdigit(c)) {
  1952. low *= 10;
  1953. low += c - '0';
  1954. } else if (c == '-') {
  1955. parse_low = false;
  1956. } else if (c == ',' || c == 0) {
  1957. if (pass == 1) {
  1958. (*ranges)[range].low = low;
  1959. (*ranges)[range].high = low;
  1960. }
  1961. low = 0;
  1962. range++;
  1963. } else {
  1964. parse_error(argv[0], c, i);
  1965. return ERROR_COMMAND_SYNTAX_ERROR;
  1966. }
  1967. } else {
  1968. if (isdigit(c)) {
  1969. high *= 10;
  1970. high += c - '0';
  1971. } else if (c == ',' || c == 0) {
  1972. parse_low = true;
  1973. if (pass == 1) {
  1974. (*ranges)[range].low = low;
  1975. (*ranges)[range].high = high;
  1976. }
  1977. low = 0;
  1978. high = 0;
  1979. range++;
  1980. } else {
  1981. parse_error(argv[0], c, i);
  1982. return ERROR_COMMAND_SYNTAX_ERROR;
  1983. }
  1984. }
  1985. }
  1986. if (pass == 0) {
  1987. free(*ranges);
  1988. *ranges = calloc(range + 2, sizeof(range_t));
  1989. if (!*ranges)
  1990. return ERROR_FAIL;
  1991. } else {
  1992. (*ranges)[range].low = 1;
  1993. (*ranges)[range].high = 0;
  1994. }
  1995. }
  1996. return ERROR_OK;
  1997. }
  1998. COMMAND_HANDLER(riscv_set_expose_csrs)
  1999. {
  2000. if (CMD_ARGC != 1) {
  2001. LOG_ERROR("Command takes exactly 1 parameter");
  2002. return ERROR_COMMAND_SYNTAX_ERROR;
  2003. }
  2004. return parse_ranges(&expose_csr, CMD_ARGV);
  2005. }
  2006. COMMAND_HANDLER(riscv_set_expose_custom)
  2007. {
  2008. if (CMD_ARGC != 1) {
  2009. LOG_ERROR("Command takes exactly 1 parameter");
  2010. return ERROR_COMMAND_SYNTAX_ERROR;
  2011. }
  2012. return parse_ranges(&expose_custom, CMD_ARGV);
  2013. }
  2014. COMMAND_HANDLER(riscv_authdata_read)
  2015. {
  2016. if (CMD_ARGC != 0) {
  2017. LOG_ERROR("Command takes no parameters");
  2018. return ERROR_COMMAND_SYNTAX_ERROR;
  2019. }
  2020. struct target *target = get_current_target(CMD_CTX);
  2021. if (!target) {
  2022. LOG_ERROR("target is NULL!");
  2023. return ERROR_FAIL;
  2024. }
  2025. RISCV_INFO(r);
  2026. if (!r) {
  2027. LOG_ERROR("riscv_info is NULL!");
  2028. return ERROR_FAIL;
  2029. }
  2030. if (r->authdata_read) {
  2031. uint32_t value;
  2032. if (r->authdata_read(target, &value) != ERROR_OK)
  2033. return ERROR_FAIL;
  2034. command_print_sameline(CMD, "0x%08" PRIx32, value);
  2035. return ERROR_OK;
  2036. } else {
  2037. LOG_ERROR("authdata_read is not implemented for this target.");
  2038. return ERROR_FAIL;
  2039. }
  2040. }
  2041. COMMAND_HANDLER(riscv_authdata_write)
  2042. {
  2043. if (CMD_ARGC != 1) {
  2044. LOG_ERROR("Command takes exactly 1 argument");
  2045. return ERROR_COMMAND_SYNTAX_ERROR;
  2046. }
  2047. struct target *target = get_current_target(CMD_CTX);
  2048. RISCV_INFO(r);
  2049. uint32_t value;
  2050. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], value);
  2051. if (r->authdata_write) {
  2052. return r->authdata_write(target, value);
  2053. } else {
  2054. LOG_ERROR("authdata_write is not implemented for this target.");
  2055. return ERROR_FAIL;
  2056. }
  2057. }
  2058. COMMAND_HANDLER(riscv_dmi_read)
  2059. {
  2060. if (CMD_ARGC != 1) {
  2061. LOG_ERROR("Command takes 1 parameter");
  2062. return ERROR_COMMAND_SYNTAX_ERROR;
  2063. }
  2064. struct target *target = get_current_target(CMD_CTX);
  2065. if (!target) {
  2066. LOG_ERROR("target is NULL!");
  2067. return ERROR_FAIL;
  2068. }
  2069. RISCV_INFO(r);
  2070. if (!r) {
  2071. LOG_ERROR("riscv_info is NULL!");
  2072. return ERROR_FAIL;
  2073. }
  2074. if (r->dmi_read) {
  2075. uint32_t address, value;
  2076. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  2077. if (r->dmi_read(target, &value, address) != ERROR_OK)
  2078. return ERROR_FAIL;
  2079. command_print(CMD, "0x%" PRIx32, value);
  2080. return ERROR_OK;
  2081. } else {
  2082. LOG_ERROR("dmi_read is not implemented for this target.");
  2083. return ERROR_FAIL;
  2084. }
  2085. }
  2086. COMMAND_HANDLER(riscv_dmi_write)
  2087. {
  2088. if (CMD_ARGC != 2) {
  2089. LOG_ERROR("Command takes exactly 2 arguments");
  2090. return ERROR_COMMAND_SYNTAX_ERROR;
  2091. }
  2092. struct target *target = get_current_target(CMD_CTX);
  2093. RISCV_INFO(r);
  2094. uint32_t address, value;
  2095. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  2096. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  2097. if (r->dmi_write) {
  2098. return r->dmi_write(target, address, value);
  2099. } else {
  2100. LOG_ERROR("dmi_write is not implemented for this target.");
  2101. return ERROR_FAIL;
  2102. }
  2103. }
  2104. COMMAND_HANDLER(riscv_test_sba_config_reg)
  2105. {
  2106. if (CMD_ARGC != 4) {
  2107. LOG_ERROR("Command takes exactly 4 arguments");
  2108. return ERROR_COMMAND_SYNTAX_ERROR;
  2109. }
  2110. struct target *target = get_current_target(CMD_CTX);
  2111. RISCV_INFO(r);
  2112. target_addr_t legal_address;
  2113. uint32_t num_words;
  2114. target_addr_t illegal_address;
  2115. bool run_sbbusyerror_test;
  2116. COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[0], legal_address);
  2117. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], num_words);
  2118. COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[2], illegal_address);
  2119. COMMAND_PARSE_ON_OFF(CMD_ARGV[3], run_sbbusyerror_test);
  2120. if (r->test_sba_config_reg) {
  2121. return r->test_sba_config_reg(target, legal_address, num_words,
  2122. illegal_address, run_sbbusyerror_test);
  2123. } else {
  2124. LOG_ERROR("test_sba_config_reg is not implemented for this target.");
  2125. return ERROR_FAIL;
  2126. }
  2127. }
  2128. COMMAND_HANDLER(riscv_reset_delays)
  2129. {
  2130. int wait = 0;
  2131. if (CMD_ARGC > 1) {
  2132. LOG_ERROR("Command takes at most one argument");
  2133. return ERROR_COMMAND_SYNTAX_ERROR;
  2134. }
  2135. if (CMD_ARGC == 1)
  2136. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], wait);
  2137. struct target *target = get_current_target(CMD_CTX);
  2138. RISCV_INFO(r);
  2139. r->reset_delays_wait = wait;
  2140. return ERROR_OK;
  2141. }
  2142. COMMAND_HANDLER(riscv_set_ir)
  2143. {
  2144. if (CMD_ARGC != 2) {
  2145. LOG_ERROR("Command takes exactly 2 arguments");
  2146. return ERROR_COMMAND_SYNTAX_ERROR;
  2147. }
  2148. uint32_t value;
  2149. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  2150. if (!strcmp(CMD_ARGV[0], "idcode"))
  2151. buf_set_u32(ir_idcode, 0, 32, value);
  2152. else if (!strcmp(CMD_ARGV[0], "dtmcs"))
  2153. buf_set_u32(ir_dtmcontrol, 0, 32, value);
  2154. else if (!strcmp(CMD_ARGV[0], "dmi"))
  2155. buf_set_u32(ir_dbus, 0, 32, value);
  2156. else
  2157. return ERROR_FAIL;
  2158. return ERROR_OK;
  2159. }
  2160. COMMAND_HANDLER(riscv_resume_order)
  2161. {
  2162. if (CMD_ARGC > 1) {
  2163. LOG_ERROR("Command takes at most one argument");
  2164. return ERROR_COMMAND_SYNTAX_ERROR;
  2165. }
  2166. if (!strcmp(CMD_ARGV[0], "normal")) {
  2167. resume_order = RO_NORMAL;
  2168. } else if (!strcmp(CMD_ARGV[0], "reversed")) {
  2169. resume_order = RO_REVERSED;
  2170. } else {
  2171. LOG_ERROR("Unsupported resume order: %s", CMD_ARGV[0]);
  2172. return ERROR_FAIL;
  2173. }
  2174. return ERROR_OK;
  2175. }
  2176. COMMAND_HANDLER(riscv_use_bscan_tunnel)
  2177. {
  2178. int irwidth = 0;
  2179. int tunnel_type = BSCAN_TUNNEL_NESTED_TAP;
  2180. if (CMD_ARGC > 2) {
  2181. LOG_ERROR("Command takes at most two arguments");
  2182. return ERROR_COMMAND_SYNTAX_ERROR;
  2183. } else if (CMD_ARGC == 1) {
  2184. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], irwidth);
  2185. } else if (CMD_ARGC == 2) {
  2186. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], irwidth);
  2187. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tunnel_type);
  2188. }
  2189. if (tunnel_type == BSCAN_TUNNEL_NESTED_TAP)
  2190. LOG_INFO("Nested Tap based Bscan Tunnel Selected");
  2191. else if (tunnel_type == BSCAN_TUNNEL_DATA_REGISTER)
  2192. LOG_INFO("Simple Register based Bscan Tunnel Selected");
  2193. else
  2194. LOG_INFO("Invalid Tunnel type selected ! : selecting default Nested Tap Type");
  2195. bscan_tunnel_type = tunnel_type;
  2196. bscan_tunnel_ir_width = irwidth;
  2197. return ERROR_OK;
  2198. }
  2199. COMMAND_HANDLER(riscv_set_enable_virt2phys)
  2200. {
  2201. if (CMD_ARGC != 1) {
  2202. LOG_ERROR("Command takes exactly 1 parameter");
  2203. return ERROR_COMMAND_SYNTAX_ERROR;
  2204. }
  2205. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], riscv_enable_virt2phys);
  2206. return ERROR_OK;
  2207. }
  2208. COMMAND_HANDLER(riscv_set_ebreakm)
  2209. {
  2210. if (CMD_ARGC != 1) {
  2211. LOG_ERROR("Command takes exactly 1 parameter");
  2212. return ERROR_COMMAND_SYNTAX_ERROR;
  2213. }
  2214. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], riscv_ebreakm);
  2215. return ERROR_OK;
  2216. }
  2217. COMMAND_HANDLER(riscv_set_ebreaks)
  2218. {
  2219. if (CMD_ARGC != 1) {
  2220. LOG_ERROR("Command takes exactly 1 parameter");
  2221. return ERROR_COMMAND_SYNTAX_ERROR;
  2222. }
  2223. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], riscv_ebreaks);
  2224. return ERROR_OK;
  2225. }
  2226. COMMAND_HANDLER(riscv_set_ebreaku)
  2227. {
  2228. if (CMD_ARGC != 1) {
  2229. LOG_ERROR("Command takes exactly 1 parameter");
  2230. return ERROR_COMMAND_SYNTAX_ERROR;
  2231. }
  2232. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], riscv_ebreaku);
  2233. return ERROR_OK;
  2234. }
  2235. static const struct command_registration riscv_exec_command_handlers[] = {
  2236. {
  2237. .name = "test_compliance",
  2238. .handler = riscv_test_compliance,
  2239. .usage = "",
  2240. .mode = COMMAND_EXEC,
  2241. .help = "Runs a basic compliance test suite against the RISC-V Debug Spec."
  2242. },
  2243. {
  2244. .name = "set_command_timeout_sec",
  2245. .handler = riscv_set_command_timeout_sec,
  2246. .mode = COMMAND_ANY,
  2247. .usage = "[sec]",
  2248. .help = "Set the wall-clock timeout (in seconds) for individual commands"
  2249. },
  2250. {
  2251. .name = "set_reset_timeout_sec",
  2252. .handler = riscv_set_reset_timeout_sec,
  2253. .mode = COMMAND_ANY,
  2254. .usage = "[sec]",
  2255. .help = "Set the wall-clock timeout (in seconds) after reset is deasserted"
  2256. },
  2257. {
  2258. .name = "set_prefer_sba",
  2259. .handler = riscv_set_prefer_sba,
  2260. .mode = COMMAND_ANY,
  2261. .usage = "on|off",
  2262. .help = "When on, prefer to use System Bus Access to access memory. "
  2263. "When off (default), prefer to use the Program Buffer to access memory."
  2264. },
  2265. {
  2266. .name = "set_enable_virtual",
  2267. .handler = riscv_set_enable_virtual,
  2268. .mode = COMMAND_ANY,
  2269. .usage = "on|off",
  2270. .help = "When on, memory accesses are performed on physical or virtual "
  2271. "memory depending on the current system configuration. "
  2272. "When off (default), all memory accessses are performed on physical memory."
  2273. },
  2274. {
  2275. .name = "expose_csrs",
  2276. .handler = riscv_set_expose_csrs,
  2277. .mode = COMMAND_ANY,
  2278. .usage = "n0[-m0][,n1[-m1]]...",
  2279. .help = "Configure a list of inclusive ranges for CSRs to expose in "
  2280. "addition to the standard ones. This must be executed before "
  2281. "`init`."
  2282. },
  2283. {
  2284. .name = "expose_custom",
  2285. .handler = riscv_set_expose_custom,
  2286. .mode = COMMAND_ANY,
  2287. .usage = "n0[-m0][,n1[-m1]]...",
  2288. .help = "Configure a list of inclusive ranges for custom registers to "
  2289. "expose. custom0 is accessed as abstract register number 0xc000, "
  2290. "etc. This must be executed before `init`."
  2291. },
  2292. {
  2293. .name = "authdata_read",
  2294. .handler = riscv_authdata_read,
  2295. .usage = "",
  2296. .mode = COMMAND_ANY,
  2297. .help = "Return the 32-bit value read from authdata."
  2298. },
  2299. {
  2300. .name = "authdata_write",
  2301. .handler = riscv_authdata_write,
  2302. .mode = COMMAND_ANY,
  2303. .usage = "value",
  2304. .help = "Write the 32-bit value to authdata."
  2305. },
  2306. {
  2307. .name = "dmi_read",
  2308. .handler = riscv_dmi_read,
  2309. .mode = COMMAND_ANY,
  2310. .usage = "address",
  2311. .help = "Perform a 32-bit DMI read at address, returning the value."
  2312. },
  2313. {
  2314. .name = "dmi_write",
  2315. .handler = riscv_dmi_write,
  2316. .mode = COMMAND_ANY,
  2317. .usage = "address value",
  2318. .help = "Perform a 32-bit DMI write of value at address."
  2319. },
  2320. {
  2321. .name = "test_sba_config_reg",
  2322. .handler = riscv_test_sba_config_reg,
  2323. .mode = COMMAND_ANY,
  2324. .usage = "legal_address num_words "
  2325. "illegal_address run_sbbusyerror_test[on/off]",
  2326. .help = "Perform a series of tests on the SBCS register. "
  2327. "Inputs are a legal, 128-byte aligned address and a number of words to "
  2328. "read/write starting at that address (i.e., address range [legal address, "
  2329. "legal_address+word_size*num_words) must be legally readable/writable), "
  2330. "an illegal, 128-byte aligned address for error flag/handling cases, "
  2331. "and whether sbbusyerror test should be run."
  2332. },
  2333. {
  2334. .name = "reset_delays",
  2335. .handler = riscv_reset_delays,
  2336. .mode = COMMAND_ANY,
  2337. .usage = "[wait]",
  2338. .help = "OpenOCD learns how many Run-Test/Idle cycles are required "
  2339. "between scans to avoid encountering the target being busy. This "
  2340. "command resets those learned values after `wait` scans. It's only "
  2341. "useful for testing OpenOCD itself."
  2342. },
  2343. {
  2344. .name = "resume_order",
  2345. .handler = riscv_resume_order,
  2346. .mode = COMMAND_ANY,
  2347. .usage = "normal|reversed",
  2348. .help = "Choose the order that harts are resumed in when `hasel` is not "
  2349. "supported. Normal order is from lowest hart index to highest. "
  2350. "Reversed order is from highest hart index to lowest."
  2351. },
  2352. {
  2353. .name = "set_ir",
  2354. .handler = riscv_set_ir,
  2355. .mode = COMMAND_ANY,
  2356. .usage = "[idcode|dtmcs|dmi] value",
  2357. .help = "Set IR value for specified JTAG register."
  2358. },
  2359. {
  2360. .name = "use_bscan_tunnel",
  2361. .handler = riscv_use_bscan_tunnel,
  2362. .mode = COMMAND_ANY,
  2363. .usage = "value [type]",
  2364. .help = "Enable or disable use of a BSCAN tunnel to reach DM. Supply "
  2365. "the width of the DM transport TAP's instruction register to "
  2366. "enable. Supply a value of 0 to disable. Pass A second argument "
  2367. "(optional) to indicate Bscan Tunnel Type {0:(default) NESTED_TAP , "
  2368. "1: DATA_REGISTER}"
  2369. },
  2370. {
  2371. .name = "set_enable_virt2phys",
  2372. .handler = riscv_set_enable_virt2phys,
  2373. .mode = COMMAND_ANY,
  2374. .usage = "on|off",
  2375. .help = "When on (default), enable translation from virtual address to "
  2376. "physical address."
  2377. },
  2378. {
  2379. .name = "set_ebreakm",
  2380. .handler = riscv_set_ebreakm,
  2381. .mode = COMMAND_ANY,
  2382. .usage = "on|off",
  2383. .help = "Control dcsr.ebreakm. When off, M-mode ebreak instructions "
  2384. "don't trap to OpenOCD. Defaults to on."
  2385. },
  2386. {
  2387. .name = "set_ebreaks",
  2388. .handler = riscv_set_ebreaks,
  2389. .mode = COMMAND_ANY,
  2390. .usage = "on|off",
  2391. .help = "Control dcsr.ebreaks. When off, S-mode ebreak instructions "
  2392. "don't trap to OpenOCD. Defaults to on."
  2393. },
  2394. {
  2395. .name = "set_ebreaku",
  2396. .handler = riscv_set_ebreaku,
  2397. .mode = COMMAND_ANY,
  2398. .usage = "on|off",
  2399. .help = "Control dcsr.ebreaku. When off, U-mode ebreak instructions "
  2400. "don't trap to OpenOCD. Defaults to on."
  2401. },
  2402. COMMAND_REGISTRATION_DONE
  2403. };
  2404. /*
  2405. * To be noted that RISC-V targets use the same semihosting commands as
  2406. * ARM targets.
  2407. *
  2408. * The main reason is compatibility with existing tools. For example the
  2409. * Eclipse OpenOCD/SEGGER J-Link/QEMU plug-ins have several widgets to
  2410. * configure semihosting, which generate commands like `arm semihosting
  2411. * enable`.
  2412. * A secondary reason is the fact that the protocol used is exactly the
  2413. * one specified by ARM. If RISC-V will ever define its own semihosting
  2414. * protocol, then a command like `riscv semihosting enable` will make
  2415. * sense, but for now all semihosting commands are prefixed with `arm`.
  2416. */
  2417. extern const struct command_registration semihosting_common_handlers[];
  2418. const struct command_registration riscv_command_handlers[] = {
  2419. {
  2420. .name = "riscv",
  2421. .mode = COMMAND_ANY,
  2422. .help = "RISC-V Command Group",
  2423. .usage = "",
  2424. .chain = riscv_exec_command_handlers
  2425. },
  2426. {
  2427. .name = "arm",
  2428. .mode = COMMAND_ANY,
  2429. .help = "ARM Command Group",
  2430. .usage = "",
  2431. .chain = semihosting_common_handlers
  2432. },
  2433. COMMAND_REGISTRATION_DONE
  2434. };
  2435. static unsigned riscv_xlen_nonconst(struct target *target)
  2436. {
  2437. return riscv_xlen(target);
  2438. }
  2439. static unsigned int riscv_data_bits(struct target *target)
  2440. {
  2441. RISCV_INFO(r);
  2442. if (r->data_bits)
  2443. return r->data_bits(target);
  2444. return riscv_xlen(target);
  2445. }
  2446. struct target_type riscv_target = {
  2447. .name = "riscv",
  2448. .init_target = riscv_init_target,
  2449. .deinit_target = riscv_deinit_target,
  2450. .examine = riscv_examine,
  2451. /* poll current target status */
  2452. .poll = old_or_new_riscv_poll,
  2453. .halt = riscv_halt,
  2454. .resume = riscv_target_resume,
  2455. .step = old_or_new_riscv_step,
  2456. .assert_reset = riscv_assert_reset,
  2457. .deassert_reset = riscv_deassert_reset,
  2458. .read_memory = riscv_read_memory,
  2459. .write_memory = riscv_write_memory,
  2460. .read_phys_memory = riscv_read_phys_memory,
  2461. .write_phys_memory = riscv_write_phys_memory,
  2462. .checksum_memory = riscv_checksum_memory,
  2463. .mmu = riscv_mmu,
  2464. .virt2phys = riscv_virt2phys,
  2465. .get_gdb_arch = riscv_get_gdb_arch,
  2466. .get_gdb_reg_list = riscv_get_gdb_reg_list,
  2467. .get_gdb_reg_list_noread = riscv_get_gdb_reg_list_noread,
  2468. .add_breakpoint = riscv_add_breakpoint,
  2469. .remove_breakpoint = riscv_remove_breakpoint,
  2470. .add_watchpoint = riscv_add_watchpoint,
  2471. .remove_watchpoint = riscv_remove_watchpoint,
  2472. .hit_watchpoint = riscv_hit_watchpoint,
  2473. .arch_state = riscv_arch_state,
  2474. .run_algorithm = riscv_run_algorithm,
  2475. .commands = riscv_command_handlers,
  2476. .address_bits = riscv_xlen_nonconst,
  2477. .data_bits = riscv_data_bits
  2478. };
  2479. /*** RISC-V Interface ***/
  2480. void riscv_info_init(struct target *target, riscv_info_t *r)
  2481. {
  2482. memset(r, 0, sizeof(*r));
  2483. r->dtm_version = 1;
  2484. r->registers_initialized = false;
  2485. r->current_hartid = target->coreid;
  2486. memset(r->trigger_unique_id, 0xff, sizeof(r->trigger_unique_id));
  2487. for (size_t h = 0; h < RISCV_MAX_HARTS; ++h)
  2488. r->xlen[h] = -1;
  2489. }
  2490. static int riscv_resume_go_all_harts(struct target *target)
  2491. {
  2492. RISCV_INFO(r);
  2493. /* Dummy variables to make mingw32-gcc happy. */
  2494. int first = 0;
  2495. int last = 1;
  2496. int step = 1;
  2497. switch (resume_order) {
  2498. case RO_NORMAL:
  2499. first = 0;
  2500. last = riscv_count_harts(target) - 1;
  2501. step = 1;
  2502. break;
  2503. case RO_REVERSED:
  2504. first = riscv_count_harts(target) - 1;
  2505. last = 0;
  2506. step = -1;
  2507. break;
  2508. default:
  2509. assert(0);
  2510. }
  2511. for (int i = first; i != last + step; i += step) {
  2512. if (!riscv_hart_enabled(target, i))
  2513. continue;
  2514. LOG_DEBUG("resuming hart %d", i);
  2515. if (riscv_set_current_hartid(target, i) != ERROR_OK)
  2516. return ERROR_FAIL;
  2517. if (riscv_is_halted(target)) {
  2518. if (r->resume_go(target) != ERROR_OK)
  2519. return ERROR_FAIL;
  2520. } else {
  2521. LOG_DEBUG(" hart %d requested resume, but was already resumed", i);
  2522. }
  2523. }
  2524. riscv_invalidate_register_cache(target);
  2525. return ERROR_OK;
  2526. }
  2527. int riscv_step_rtos_hart(struct target *target)
  2528. {
  2529. RISCV_INFO(r);
  2530. int hartid = r->current_hartid;
  2531. if (riscv_rtos_enabled(target)) {
  2532. hartid = r->rtos_hartid;
  2533. if (hartid == -1) {
  2534. LOG_DEBUG("GDB has asked me to step \"any\" thread, so I'm stepping hart 0.");
  2535. hartid = 0;
  2536. }
  2537. }
  2538. if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
  2539. return ERROR_FAIL;
  2540. LOG_DEBUG("stepping hart %d", hartid);
  2541. if (!riscv_is_halted(target)) {
  2542. LOG_ERROR("Hart isn't halted before single step!");
  2543. return ERROR_FAIL;
  2544. }
  2545. riscv_invalidate_register_cache(target);
  2546. r->on_step(target);
  2547. if (r->step_current_hart(target) != ERROR_OK)
  2548. return ERROR_FAIL;
  2549. riscv_invalidate_register_cache(target);
  2550. r->on_halt(target);
  2551. if (!riscv_is_halted(target)) {
  2552. LOG_ERROR("Hart was not halted after single step!");
  2553. return ERROR_FAIL;
  2554. }
  2555. return ERROR_OK;
  2556. }
  2557. bool riscv_supports_extension(struct target *target, int hartid, char letter)
  2558. {
  2559. RISCV_INFO(r);
  2560. unsigned num;
  2561. if (letter >= 'a' && letter <= 'z')
  2562. num = letter - 'a';
  2563. else if (letter >= 'A' && letter <= 'Z')
  2564. num = letter - 'A';
  2565. else
  2566. return false;
  2567. return r->misa[hartid] & (1 << num);
  2568. }
  2569. unsigned riscv_xlen(const struct target *target)
  2570. {
  2571. return riscv_xlen_of_hart(target, riscv_current_hartid(target));
  2572. }
  2573. int riscv_xlen_of_hart(const struct target *target, int hartid)
  2574. {
  2575. RISCV_INFO(r);
  2576. assert(r->xlen[hartid] != -1);
  2577. return r->xlen[hartid];
  2578. }
  2579. bool riscv_rtos_enabled(const struct target *target)
  2580. {
  2581. return false;
  2582. }
  2583. int riscv_set_current_hartid(struct target *target, int hartid)
  2584. {
  2585. RISCV_INFO(r);
  2586. if (!r->select_current_hart)
  2587. return ERROR_OK;
  2588. int previous_hartid = riscv_current_hartid(target);
  2589. r->current_hartid = hartid;
  2590. assert(riscv_hart_enabled(target, hartid));
  2591. LOG_DEBUG("setting hartid to %d, was %d", hartid, previous_hartid);
  2592. if (r->select_current_hart(target) != ERROR_OK)
  2593. return ERROR_FAIL;
  2594. /* This might get called during init, in which case we shouldn't be
  2595. * setting up the register cache. */
  2596. if (target_was_examined(target) && riscv_rtos_enabled(target))
  2597. riscv_invalidate_register_cache(target);
  2598. return ERROR_OK;
  2599. }
  2600. void riscv_invalidate_register_cache(struct target *target)
  2601. {
  2602. RISCV_INFO(r);
  2603. LOG_DEBUG("[%d]", target->coreid);
  2604. register_cache_invalidate(target->reg_cache);
  2605. for (size_t i = 0; i < target->reg_cache->num_regs; ++i) {
  2606. struct reg *reg = &target->reg_cache->reg_list[i];
  2607. reg->valid = false;
  2608. }
  2609. r->registers_initialized = true;
  2610. }
  2611. int riscv_current_hartid(const struct target *target)
  2612. {
  2613. RISCV_INFO(r);
  2614. return r->current_hartid;
  2615. }
  2616. void riscv_set_all_rtos_harts(struct target *target)
  2617. {
  2618. RISCV_INFO(r);
  2619. r->rtos_hartid = -1;
  2620. }
  2621. void riscv_set_rtos_hartid(struct target *target, int hartid)
  2622. {
  2623. LOG_DEBUG("setting RTOS hartid %d", hartid);
  2624. RISCV_INFO(r);
  2625. r->rtos_hartid = hartid;
  2626. }
  2627. int riscv_count_harts(struct target *target)
  2628. {
  2629. if (!target)
  2630. return 1;
  2631. RISCV_INFO(r);
  2632. if (!r || !r->hart_count)
  2633. return 1;
  2634. return r->hart_count(target);
  2635. }
  2636. bool riscv_has_register(struct target *target, int hartid, int regid)
  2637. {
  2638. return 1;
  2639. }
  2640. /**
  2641. * If write is true:
  2642. * return true iff we are guaranteed that the register will contain exactly
  2643. * the value we just wrote when it's read.
  2644. * If write is false:
  2645. * return true iff we are guaranteed that the register will read the same
  2646. * value in the future as the value we just read.
  2647. */
  2648. static bool gdb_regno_cacheable(enum gdb_regno regno, bool write)
  2649. {
  2650. /* GPRs, FPRs, vector registers are just normal data stores. */
  2651. if (regno <= GDB_REGNO_XPR31 ||
  2652. (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31) ||
  2653. (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31))
  2654. return true;
  2655. /* Most CSRs won't change value on us, but we can't assume it about rbitrary
  2656. * CSRs. */
  2657. switch (regno) {
  2658. case GDB_REGNO_DPC:
  2659. return true;
  2660. case GDB_REGNO_VSTART:
  2661. case GDB_REGNO_VXSAT:
  2662. case GDB_REGNO_VXRM:
  2663. case GDB_REGNO_VLENB:
  2664. case GDB_REGNO_VL:
  2665. case GDB_REGNO_VTYPE:
  2666. case GDB_REGNO_MISA:
  2667. case GDB_REGNO_DCSR:
  2668. case GDB_REGNO_DSCRATCH0:
  2669. case GDB_REGNO_MSTATUS:
  2670. case GDB_REGNO_MEPC:
  2671. case GDB_REGNO_MCAUSE:
  2672. case GDB_REGNO_SATP:
  2673. /*
  2674. * WARL registers might not contain the value we just wrote, but
  2675. * these ones won't spontaneously change their value either. *
  2676. */
  2677. return !write;
  2678. case GDB_REGNO_TSELECT: /* I think this should be above, but then it doesn't work. */
  2679. case GDB_REGNO_TDATA1: /* Changes value when tselect is changed. */
  2680. case GDB_REGNO_TDATA2: /* Changse value when tselect is changed. */
  2681. default:
  2682. return false;
  2683. }
  2684. }
  2685. /**
  2686. * This function is called when the debug user wants to change the value of a
  2687. * register. The new value may be cached, and may not be written until the hart
  2688. * is resumed. */
  2689. int riscv_set_register(struct target *target, enum gdb_regno r, riscv_reg_t v)
  2690. {
  2691. return riscv_set_register_on_hart(target, riscv_current_hartid(target), r, v);
  2692. }
  2693. int riscv_set_register_on_hart(struct target *target, int hartid,
  2694. enum gdb_regno regid, uint64_t value)
  2695. {
  2696. RISCV_INFO(r);
  2697. LOG_DEBUG("{%d} %s <- %" PRIx64, hartid, gdb_regno_name(regid), value);
  2698. assert(r->set_register);
  2699. /* TODO: Hack to deal with gdb that thinks these registers still exist. */
  2700. if (regid > GDB_REGNO_XPR15 && regid <= GDB_REGNO_XPR31 && value == 0 &&
  2701. riscv_supports_extension(target, hartid, 'E'))
  2702. return ERROR_OK;
  2703. struct reg *reg = &target->reg_cache->reg_list[regid];
  2704. buf_set_u64(reg->value, 0, reg->size, value);
  2705. int result = r->set_register(target, hartid, regid, value);
  2706. if (result == ERROR_OK)
  2707. reg->valid = gdb_regno_cacheable(regid, true);
  2708. else
  2709. reg->valid = false;
  2710. LOG_DEBUG("[%s]{%d} wrote 0x%" PRIx64 " to %s valid=%d",
  2711. target_name(target), hartid, value, reg->name, reg->valid);
  2712. return result;
  2713. }
  2714. int riscv_get_register(struct target *target, riscv_reg_t *value,
  2715. enum gdb_regno r)
  2716. {
  2717. return riscv_get_register_on_hart(target, value,
  2718. riscv_current_hartid(target), r);
  2719. }
  2720. int riscv_get_register_on_hart(struct target *target, riscv_reg_t *value,
  2721. int hartid, enum gdb_regno regid)
  2722. {
  2723. RISCV_INFO(r);
  2724. struct reg *reg = &target->reg_cache->reg_list[regid];
  2725. if (!reg->exist) {
  2726. LOG_DEBUG("[%s]{%d} %s does not exist.",
  2727. target_name(target), hartid, gdb_regno_name(regid));
  2728. return ERROR_FAIL;
  2729. }
  2730. if (reg && reg->valid && hartid == riscv_current_hartid(target)) {
  2731. *value = buf_get_u64(reg->value, 0, reg->size);
  2732. LOG_DEBUG("{%d} %s: %" PRIx64 " (cached)", hartid,
  2733. gdb_regno_name(regid), *value);
  2734. return ERROR_OK;
  2735. }
  2736. /* TODO: Hack to deal with gdb that thinks these registers still exist. */
  2737. if (regid > GDB_REGNO_XPR15 && regid <= GDB_REGNO_XPR31 &&
  2738. riscv_supports_extension(target, hartid, 'E')) {
  2739. *value = 0;
  2740. return ERROR_OK;
  2741. }
  2742. int result = r->get_register(target, value, hartid, regid);
  2743. if (result == ERROR_OK)
  2744. reg->valid = gdb_regno_cacheable(regid, false);
  2745. LOG_DEBUG("{%d} %s: %" PRIx64, hartid, gdb_regno_name(regid), *value);
  2746. return result;
  2747. }
  2748. bool riscv_is_halted(struct target *target)
  2749. {
  2750. RISCV_INFO(r);
  2751. assert(r->is_halted);
  2752. return r->is_halted(target);
  2753. }
  2754. enum riscv_halt_reason riscv_halt_reason(struct target *target, int hartid)
  2755. {
  2756. RISCV_INFO(r);
  2757. if (riscv_set_current_hartid(target, hartid) != ERROR_OK)
  2758. return RISCV_HALT_ERROR;
  2759. if (!riscv_is_halted(target)) {
  2760. LOG_ERROR("Hart is not halted!");
  2761. return RISCV_HALT_UNKNOWN;
  2762. }
  2763. return r->halt_reason(target);
  2764. }
  2765. size_t riscv_debug_buffer_size(struct target *target)
  2766. {
  2767. RISCV_INFO(r);
  2768. return r->debug_buffer_size[riscv_current_hartid(target)];
  2769. }
  2770. int riscv_write_debug_buffer(struct target *target, int index, riscv_insn_t insn)
  2771. {
  2772. RISCV_INFO(r);
  2773. r->write_debug_buffer(target, index, insn);
  2774. return ERROR_OK;
  2775. }
  2776. riscv_insn_t riscv_read_debug_buffer(struct target *target, int index)
  2777. {
  2778. RISCV_INFO(r);
  2779. return r->read_debug_buffer(target, index);
  2780. }
  2781. int riscv_execute_debug_buffer(struct target *target)
  2782. {
  2783. RISCV_INFO(r);
  2784. return r->execute_debug_buffer(target);
  2785. }
  2786. void riscv_fill_dmi_write_u64(struct target *target, char *buf, int a, uint64_t d)
  2787. {
  2788. RISCV_INFO(r);
  2789. r->fill_dmi_write_u64(target, buf, a, d);
  2790. }
  2791. void riscv_fill_dmi_read_u64(struct target *target, char *buf, int a)
  2792. {
  2793. RISCV_INFO(r);
  2794. r->fill_dmi_read_u64(target, buf, a);
  2795. }
  2796. void riscv_fill_dmi_nop_u64(struct target *target, char *buf)
  2797. {
  2798. RISCV_INFO(r);
  2799. r->fill_dmi_nop_u64(target, buf);
  2800. }
  2801. int riscv_dmi_write_u64_bits(struct target *target)
  2802. {
  2803. RISCV_INFO(r);
  2804. return r->dmi_write_u64_bits(target);
  2805. }
  2806. bool riscv_hart_enabled(struct target *target, int hartid)
  2807. {
  2808. /* FIXME: Add a hart mask to the RTOS. */
  2809. if (riscv_rtos_enabled(target))
  2810. return hartid < riscv_count_harts(target);
  2811. return hartid == target->coreid;
  2812. }
  2813. /**
  2814. * Count triggers, and initialize trigger_count for each hart.
  2815. * trigger_count is initialized even if this function fails to discover
  2816. * something.
  2817. * Disable any hardware triggers that have dmode set. We can't have set them
  2818. * ourselves. Maybe they're left over from some killed debug session.
  2819. * */
  2820. int riscv_enumerate_triggers(struct target *target)
  2821. {
  2822. RISCV_INFO(r);
  2823. if (r->triggers_enumerated)
  2824. return ERROR_OK;
  2825. r->triggers_enumerated = true; /* At the very least we tried. */
  2826. for (int hartid = 0; hartid < riscv_count_harts(target); ++hartid) {
  2827. if (!riscv_hart_enabled(target, hartid))
  2828. continue;
  2829. riscv_reg_t tselect;
  2830. int result = riscv_get_register_on_hart(target, &tselect, hartid,
  2831. GDB_REGNO_TSELECT);
  2832. if (result != ERROR_OK)
  2833. return result;
  2834. for (unsigned t = 0; t < RISCV_MAX_TRIGGERS; ++t) {
  2835. r->trigger_count[hartid] = t;
  2836. /* If we can't write tselect, then this hart does not support triggers. */
  2837. if (riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, t) != ERROR_OK)
  2838. break;
  2839. uint64_t tselect_rb;
  2840. result = riscv_get_register_on_hart(target, &tselect_rb, hartid,
  2841. GDB_REGNO_TSELECT);
  2842. if (result != ERROR_OK)
  2843. return result;
  2844. /* Mask off the top bit, which is used as tdrmode in old
  2845. * implementations. */
  2846. tselect_rb &= ~(1ULL << (riscv_xlen(target)-1));
  2847. if (tselect_rb != t)
  2848. break;
  2849. uint64_t tdata1;
  2850. result = riscv_get_register_on_hart(target, &tdata1, hartid,
  2851. GDB_REGNO_TDATA1);
  2852. if (result != ERROR_OK)
  2853. return result;
  2854. int type = get_field(tdata1, MCONTROL_TYPE(riscv_xlen(target)));
  2855. if (type == 0)
  2856. break;
  2857. switch (type) {
  2858. case 1:
  2859. /* On these older cores we don't support software using
  2860. * triggers. */
  2861. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
  2862. break;
  2863. case 2:
  2864. if (tdata1 & MCONTROL_DMODE(riscv_xlen(target)))
  2865. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TDATA1, 0);
  2866. break;
  2867. }
  2868. }
  2869. riscv_set_register_on_hart(target, hartid, GDB_REGNO_TSELECT, tselect);
  2870. LOG_INFO("[%d] Found %d triggers", hartid, r->trigger_count[hartid]);
  2871. }
  2872. return ERROR_OK;
  2873. }
  2874. const char *gdb_regno_name(enum gdb_regno regno)
  2875. {
  2876. static char buf[32];
  2877. switch (regno) {
  2878. case GDB_REGNO_ZERO:
  2879. return "zero";
  2880. case GDB_REGNO_RA:
  2881. return "ra";
  2882. case GDB_REGNO_SP:
  2883. return "sp";
  2884. case GDB_REGNO_GP:
  2885. return "gp";
  2886. case GDB_REGNO_TP:
  2887. return "tp";
  2888. case GDB_REGNO_T0:
  2889. return "t0";
  2890. case GDB_REGNO_T1:
  2891. return "t1";
  2892. case GDB_REGNO_T2:
  2893. return "t2";
  2894. case GDB_REGNO_S0:
  2895. return "s0";
  2896. case GDB_REGNO_S1:
  2897. return "s1";
  2898. case GDB_REGNO_A0:
  2899. return "a0";
  2900. case GDB_REGNO_A1:
  2901. return "a1";
  2902. case GDB_REGNO_A2:
  2903. return "a2";
  2904. case GDB_REGNO_A3:
  2905. return "a3";
  2906. case GDB_REGNO_A4:
  2907. return "a4";
  2908. case GDB_REGNO_A5:
  2909. return "a5";
  2910. case GDB_REGNO_A6:
  2911. return "a6";
  2912. case GDB_REGNO_A7:
  2913. return "a7";
  2914. case GDB_REGNO_S2:
  2915. return "s2";
  2916. case GDB_REGNO_S3:
  2917. return "s3";
  2918. case GDB_REGNO_S4:
  2919. return "s4";
  2920. case GDB_REGNO_S5:
  2921. return "s5";
  2922. case GDB_REGNO_S6:
  2923. return "s6";
  2924. case GDB_REGNO_S7:
  2925. return "s7";
  2926. case GDB_REGNO_S8:
  2927. return "s8";
  2928. case GDB_REGNO_S9:
  2929. return "s9";
  2930. case GDB_REGNO_S10:
  2931. return "s10";
  2932. case GDB_REGNO_S11:
  2933. return "s11";
  2934. case GDB_REGNO_T3:
  2935. return "t3";
  2936. case GDB_REGNO_T4:
  2937. return "t4";
  2938. case GDB_REGNO_T5:
  2939. return "t5";
  2940. case GDB_REGNO_T6:
  2941. return "t6";
  2942. case GDB_REGNO_PC:
  2943. return "pc";
  2944. case GDB_REGNO_FPR0:
  2945. return "fpr0";
  2946. case GDB_REGNO_FPR31:
  2947. return "fpr31";
  2948. case GDB_REGNO_CSR0:
  2949. return "csr0";
  2950. case GDB_REGNO_TSELECT:
  2951. return "tselect";
  2952. case GDB_REGNO_TDATA1:
  2953. return "tdata1";
  2954. case GDB_REGNO_TDATA2:
  2955. return "tdata2";
  2956. case GDB_REGNO_MISA:
  2957. return "misa";
  2958. case GDB_REGNO_DPC:
  2959. return "dpc";
  2960. case GDB_REGNO_DCSR:
  2961. return "dcsr";
  2962. case GDB_REGNO_DSCRATCH0:
  2963. return "dscratch0";
  2964. case GDB_REGNO_MSTATUS:
  2965. return "mstatus";
  2966. case GDB_REGNO_MEPC:
  2967. return "mepc";
  2968. case GDB_REGNO_MCAUSE:
  2969. return "mcause";
  2970. case GDB_REGNO_PRIV:
  2971. return "priv";
  2972. case GDB_REGNO_SATP:
  2973. return "satp";
  2974. case GDB_REGNO_VTYPE:
  2975. return "vtype";
  2976. case GDB_REGNO_VL:
  2977. return "vl";
  2978. case GDB_REGNO_V0:
  2979. return "v0";
  2980. case GDB_REGNO_V1:
  2981. return "v1";
  2982. case GDB_REGNO_V2:
  2983. return "v2";
  2984. case GDB_REGNO_V3:
  2985. return "v3";
  2986. case GDB_REGNO_V4:
  2987. return "v4";
  2988. case GDB_REGNO_V5:
  2989. return "v5";
  2990. case GDB_REGNO_V6:
  2991. return "v6";
  2992. case GDB_REGNO_V7:
  2993. return "v7";
  2994. case GDB_REGNO_V8:
  2995. return "v8";
  2996. case GDB_REGNO_V9:
  2997. return "v9";
  2998. case GDB_REGNO_V10:
  2999. return "v10";
  3000. case GDB_REGNO_V11:
  3001. return "v11";
  3002. case GDB_REGNO_V12:
  3003. return "v12";
  3004. case GDB_REGNO_V13:
  3005. return "v13";
  3006. case GDB_REGNO_V14:
  3007. return "v14";
  3008. case GDB_REGNO_V15:
  3009. return "v15";
  3010. case GDB_REGNO_V16:
  3011. return "v16";
  3012. case GDB_REGNO_V17:
  3013. return "v17";
  3014. case GDB_REGNO_V18:
  3015. return "v18";
  3016. case GDB_REGNO_V19:
  3017. return "v19";
  3018. case GDB_REGNO_V20:
  3019. return "v20";
  3020. case GDB_REGNO_V21:
  3021. return "v21";
  3022. case GDB_REGNO_V22:
  3023. return "v22";
  3024. case GDB_REGNO_V23:
  3025. return "v23";
  3026. case GDB_REGNO_V24:
  3027. return "v24";
  3028. case GDB_REGNO_V25:
  3029. return "v25";
  3030. case GDB_REGNO_V26:
  3031. return "v26";
  3032. case GDB_REGNO_V27:
  3033. return "v27";
  3034. case GDB_REGNO_V28:
  3035. return "v28";
  3036. case GDB_REGNO_V29:
  3037. return "v29";
  3038. case GDB_REGNO_V30:
  3039. return "v30";
  3040. case GDB_REGNO_V31:
  3041. return "v31";
  3042. default:
  3043. if (regno <= GDB_REGNO_XPR31)
  3044. sprintf(buf, "x%d", regno - GDB_REGNO_ZERO);
  3045. else if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095)
  3046. sprintf(buf, "csr%d", regno - GDB_REGNO_CSR0);
  3047. else if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31)
  3048. sprintf(buf, "f%d", regno - GDB_REGNO_FPR0);
  3049. else
  3050. sprintf(buf, "gdb_regno_%d", regno);
  3051. return buf;
  3052. }
  3053. }
  3054. static int register_get(struct reg *reg)
  3055. {
  3056. riscv_reg_info_t *reg_info = reg->arch_info;
  3057. struct target *target = reg_info->target;
  3058. RISCV_INFO(r);
  3059. if (reg->number >= GDB_REGNO_V0 && reg->number <= GDB_REGNO_V31) {
  3060. if (!r->get_register_buf) {
  3061. LOG_ERROR("Reading register %s not supported on this RISC-V target.",
  3062. gdb_regno_name(reg->number));
  3063. return ERROR_FAIL;
  3064. }
  3065. if (r->get_register_buf(target, reg->value, reg->number) != ERROR_OK)
  3066. return ERROR_FAIL;
  3067. } else {
  3068. uint64_t value;
  3069. int result = riscv_get_register(target, &value, reg->number);
  3070. if (result != ERROR_OK)
  3071. return result;
  3072. buf_set_u64(reg->value, 0, reg->size, value);
  3073. }
  3074. reg->valid = gdb_regno_cacheable(reg->number, false);
  3075. char *str = buf_to_hex_str(reg->value, reg->size);
  3076. LOG_DEBUG("[%d]{%d} read 0x%s from %s (valid=%d)", target->coreid,
  3077. riscv_current_hartid(target), str, reg->name, reg->valid);
  3078. free(str);
  3079. return ERROR_OK;
  3080. }
  3081. static int register_set(struct reg *reg, uint8_t *buf)
  3082. {
  3083. riscv_reg_info_t *reg_info = reg->arch_info;
  3084. struct target *target = reg_info->target;
  3085. RISCV_INFO(r);
  3086. char *str = buf_to_hex_str(buf, reg->size);
  3087. LOG_DEBUG("[%d]{%d} write 0x%s to %s (valid=%d)", target->coreid,
  3088. riscv_current_hartid(target), str, reg->name, reg->valid);
  3089. free(str);
  3090. memcpy(reg->value, buf, DIV_ROUND_UP(reg->size, 8));
  3091. reg->valid = gdb_regno_cacheable(reg->number, true);
  3092. if (reg->number == GDB_REGNO_TDATA1 ||
  3093. reg->number == GDB_REGNO_TDATA2) {
  3094. r->manual_hwbp_set = true;
  3095. /* When enumerating triggers, we clear any triggers with DMODE set,
  3096. * assuming they were left over from a previous debug session. So make
  3097. * sure that is done before a user might be setting their own triggers.
  3098. */
  3099. if (riscv_enumerate_triggers(target) != ERROR_OK)
  3100. return ERROR_FAIL;
  3101. }
  3102. if (reg->number >= GDB_REGNO_V0 && reg->number <= GDB_REGNO_V31) {
  3103. if (!r->set_register_buf) {
  3104. LOG_ERROR("Writing register %s not supported on this RISC-V target.",
  3105. gdb_regno_name(reg->number));
  3106. return ERROR_FAIL;
  3107. }
  3108. if (r->set_register_buf(target, reg->number, reg->value) != ERROR_OK)
  3109. return ERROR_FAIL;
  3110. } else {
  3111. uint64_t value = buf_get_u64(buf, 0, reg->size);
  3112. if (riscv_set_register(target, reg->number, value) != ERROR_OK)
  3113. return ERROR_FAIL;
  3114. }
  3115. return ERROR_OK;
  3116. }
  3117. static struct reg_arch_type riscv_reg_arch_type = {
  3118. .get = register_get,
  3119. .set = register_set
  3120. };
  3121. struct csr_info {
  3122. unsigned number;
  3123. const char *name;
  3124. };
  3125. static int cmp_csr_info(const void *p1, const void *p2)
  3126. {
  3127. return (int) (((struct csr_info *)p1)->number) - (int) (((struct csr_info *)p2)->number);
  3128. }
  3129. int riscv_init_registers(struct target *target)
  3130. {
  3131. RISCV_INFO(info);
  3132. riscv_free_registers(target);
  3133. target->reg_cache = calloc(1, sizeof(*target->reg_cache));
  3134. if (!target->reg_cache)
  3135. return ERROR_FAIL;
  3136. target->reg_cache->name = "RISC-V Registers";
  3137. target->reg_cache->num_regs = GDB_REGNO_COUNT;
  3138. if (expose_custom) {
  3139. for (unsigned i = 0; expose_custom[i].low <= expose_custom[i].high; i++) {
  3140. for (unsigned number = expose_custom[i].low;
  3141. number <= expose_custom[i].high;
  3142. number++)
  3143. target->reg_cache->num_regs++;
  3144. }
  3145. }
  3146. LOG_DEBUG("create register cache for %d registers",
  3147. target->reg_cache->num_regs);
  3148. target->reg_cache->reg_list =
  3149. calloc(target->reg_cache->num_regs, sizeof(struct reg));
  3150. if (!target->reg_cache->reg_list)
  3151. return ERROR_FAIL;
  3152. const unsigned int max_reg_name_len = 12;
  3153. free(info->reg_names);
  3154. info->reg_names =
  3155. calloc(target->reg_cache->num_regs, max_reg_name_len);
  3156. if (!info->reg_names)
  3157. return ERROR_FAIL;
  3158. char *reg_name = info->reg_names;
  3159. int hartid = riscv_current_hartid(target);
  3160. static struct reg_feature feature_cpu = {
  3161. .name = "org.gnu.gdb.riscv.cpu"
  3162. };
  3163. static struct reg_feature feature_fpu = {
  3164. .name = "org.gnu.gdb.riscv.fpu"
  3165. };
  3166. static struct reg_feature feature_csr = {
  3167. .name = "org.gnu.gdb.riscv.csr"
  3168. };
  3169. static struct reg_feature feature_vector = {
  3170. .name = "org.gnu.gdb.riscv.vector"
  3171. };
  3172. static struct reg_feature feature_virtual = {
  3173. .name = "org.gnu.gdb.riscv.virtual"
  3174. };
  3175. static struct reg_feature feature_custom = {
  3176. .name = "org.gnu.gdb.riscv.custom"
  3177. };
  3178. /* These types are built into gdb. */
  3179. static struct reg_data_type type_ieee_single = { .type = REG_TYPE_IEEE_SINGLE, .id = "ieee_single" };
  3180. static struct reg_data_type type_ieee_double = { .type = REG_TYPE_IEEE_DOUBLE, .id = "ieee_double" };
  3181. static struct reg_data_type_union_field single_double_fields[] = {
  3182. {"float", &type_ieee_single, single_double_fields + 1},
  3183. {"double", &type_ieee_double, NULL},
  3184. };
  3185. static struct reg_data_type_union single_double_union = {
  3186. .fields = single_double_fields
  3187. };
  3188. static struct reg_data_type type_ieee_single_double = {
  3189. .type = REG_TYPE_ARCH_DEFINED,
  3190. .id = "FPU_FD",
  3191. .type_class = REG_TYPE_CLASS_UNION,
  3192. .reg_type_union = &single_double_union
  3193. };
  3194. static struct reg_data_type type_uint8 = { .type = REG_TYPE_UINT8, .id = "uint8" };
  3195. static struct reg_data_type type_uint16 = { .type = REG_TYPE_UINT16, .id = "uint16" };
  3196. static struct reg_data_type type_uint32 = { .type = REG_TYPE_UINT32, .id = "uint32" };
  3197. static struct reg_data_type type_uint64 = { .type = REG_TYPE_UINT64, .id = "uint64" };
  3198. static struct reg_data_type type_uint128 = { .type = REG_TYPE_UINT128, .id = "uint128" };
  3199. /* This is roughly the XML we want:
  3200. * <vector id="bytes" type="uint8" count="16"/>
  3201. * <vector id="shorts" type="uint16" count="8"/>
  3202. * <vector id="words" type="uint32" count="4"/>
  3203. * <vector id="longs" type="uint64" count="2"/>
  3204. * <vector id="quads" type="uint128" count="1"/>
  3205. * <union id="riscv_vector_type">
  3206. * <field name="b" type="bytes"/>
  3207. * <field name="s" type="shorts"/>
  3208. * <field name="w" type="words"/>
  3209. * <field name="l" type="longs"/>
  3210. * <field name="q" type="quads"/>
  3211. * </union>
  3212. */
  3213. info->vector_uint8.type = &type_uint8;
  3214. info->vector_uint8.count = info->vlenb[hartid];
  3215. info->type_uint8_vector.type = REG_TYPE_ARCH_DEFINED;
  3216. info->type_uint8_vector.id = "bytes";
  3217. info->type_uint8_vector.type_class = REG_TYPE_CLASS_VECTOR;
  3218. info->type_uint8_vector.reg_type_vector = &info->vector_uint8;
  3219. info->vector_uint16.type = &type_uint16;
  3220. info->vector_uint16.count = info->vlenb[hartid] / 2;
  3221. info->type_uint16_vector.type = REG_TYPE_ARCH_DEFINED;
  3222. info->type_uint16_vector.id = "shorts";
  3223. info->type_uint16_vector.type_class = REG_TYPE_CLASS_VECTOR;
  3224. info->type_uint16_vector.reg_type_vector = &info->vector_uint16;
  3225. info->vector_uint32.type = &type_uint32;
  3226. info->vector_uint32.count = info->vlenb[hartid] / 4;
  3227. info->type_uint32_vector.type = REG_TYPE_ARCH_DEFINED;
  3228. info->type_uint32_vector.id = "words";
  3229. info->type_uint32_vector.type_class = REG_TYPE_CLASS_VECTOR;
  3230. info->type_uint32_vector.reg_type_vector = &info->vector_uint32;
  3231. info->vector_uint64.type = &type_uint64;
  3232. info->vector_uint64.count = info->vlenb[hartid] / 8;
  3233. info->type_uint64_vector.type = REG_TYPE_ARCH_DEFINED;
  3234. info->type_uint64_vector.id = "longs";
  3235. info->type_uint64_vector.type_class = REG_TYPE_CLASS_VECTOR;
  3236. info->type_uint64_vector.reg_type_vector = &info->vector_uint64;
  3237. info->vector_uint128.type = &type_uint128;
  3238. info->vector_uint128.count = info->vlenb[hartid] / 16;
  3239. info->type_uint128_vector.type = REG_TYPE_ARCH_DEFINED;
  3240. info->type_uint128_vector.id = "quads";
  3241. info->type_uint128_vector.type_class = REG_TYPE_CLASS_VECTOR;
  3242. info->type_uint128_vector.reg_type_vector = &info->vector_uint128;
  3243. info->vector_fields[0].name = "b";
  3244. info->vector_fields[0].type = &info->type_uint8_vector;
  3245. if (info->vlenb[hartid] >= 2) {
  3246. info->vector_fields[0].next = info->vector_fields + 1;
  3247. info->vector_fields[1].name = "s";
  3248. info->vector_fields[1].type = &info->type_uint16_vector;
  3249. } else {
  3250. info->vector_fields[0].next = NULL;
  3251. }
  3252. if (info->vlenb[hartid] >= 4) {
  3253. info->vector_fields[1].next = info->vector_fields + 2;
  3254. info->vector_fields[2].name = "w";
  3255. info->vector_fields[2].type = &info->type_uint32_vector;
  3256. } else {
  3257. info->vector_fields[1].next = NULL;
  3258. }
  3259. if (info->vlenb[hartid] >= 8) {
  3260. info->vector_fields[2].next = info->vector_fields + 3;
  3261. info->vector_fields[3].name = "l";
  3262. info->vector_fields[3].type = &info->type_uint64_vector;
  3263. } else {
  3264. info->vector_fields[2].next = NULL;
  3265. }
  3266. if (info->vlenb[hartid] >= 16) {
  3267. info->vector_fields[3].next = info->vector_fields + 4;
  3268. info->vector_fields[4].name = "q";
  3269. info->vector_fields[4].type = &info->type_uint128_vector;
  3270. } else {
  3271. info->vector_fields[3].next = NULL;
  3272. }
  3273. info->vector_fields[4].next = NULL;
  3274. info->vector_union.fields = info->vector_fields;
  3275. info->type_vector.type = REG_TYPE_ARCH_DEFINED;
  3276. info->type_vector.id = "riscv_vector";
  3277. info->type_vector.type_class = REG_TYPE_CLASS_UNION;
  3278. info->type_vector.reg_type_union = &info->vector_union;
  3279. struct csr_info csr_info[] = {
  3280. #define DECLARE_CSR(name, number) { number, #name },
  3281. #include "encoding.h"
  3282. #undef DECLARE_CSR
  3283. };
  3284. /* encoding.h does not contain the registers in sorted order. */
  3285. qsort(csr_info, ARRAY_SIZE(csr_info), sizeof(*csr_info), cmp_csr_info);
  3286. unsigned csr_info_index = 0;
  3287. unsigned custom_range_index = 0;
  3288. int custom_within_range = 0;
  3289. riscv_reg_info_t *shared_reg_info = calloc(1, sizeof(riscv_reg_info_t));
  3290. if (!shared_reg_info)
  3291. return ERROR_FAIL;
  3292. shared_reg_info->target = target;
  3293. /* When gdb requests register N, gdb_get_register_packet() assumes that this
  3294. * is register at index N in reg_list. So if there are certain registers
  3295. * that don't exist, we need to leave holes in the list (or renumber, but
  3296. * it would be nice not to have yet another set of numbers to translate
  3297. * between). */
  3298. for (uint32_t number = 0; number < target->reg_cache->num_regs; number++) {
  3299. struct reg *r = &target->reg_cache->reg_list[number];
  3300. r->dirty = false;
  3301. r->valid = false;
  3302. r->exist = true;
  3303. r->type = &riscv_reg_arch_type;
  3304. r->arch_info = shared_reg_info;
  3305. r->number = number;
  3306. r->size = riscv_xlen(target);
  3307. /* r->size is set in riscv_invalidate_register_cache, maybe because the
  3308. * target is in theory allowed to change XLEN on us. But I expect a lot
  3309. * of other things to break in that case as well. */
  3310. if (number <= GDB_REGNO_XPR31) {
  3311. r->exist = number <= GDB_REGNO_XPR15 ||
  3312. !riscv_supports_extension(target, hartid, 'E');
  3313. /* TODO: For now we fake that all GPRs exist because otherwise gdb
  3314. * doesn't work. */
  3315. r->exist = true;
  3316. r->caller_save = true;
  3317. switch (number) {
  3318. case GDB_REGNO_ZERO:
  3319. r->name = "zero";
  3320. break;
  3321. case GDB_REGNO_RA:
  3322. r->name = "ra";
  3323. break;
  3324. case GDB_REGNO_SP:
  3325. r->name = "sp";
  3326. break;
  3327. case GDB_REGNO_GP:
  3328. r->name = "gp";
  3329. break;
  3330. case GDB_REGNO_TP:
  3331. r->name = "tp";
  3332. break;
  3333. case GDB_REGNO_T0:
  3334. r->name = "t0";
  3335. break;
  3336. case GDB_REGNO_T1:
  3337. r->name = "t1";
  3338. break;
  3339. case GDB_REGNO_T2:
  3340. r->name = "t2";
  3341. break;
  3342. case GDB_REGNO_FP:
  3343. r->name = "fp";
  3344. break;
  3345. case GDB_REGNO_S1:
  3346. r->name = "s1";
  3347. break;
  3348. case GDB_REGNO_A0:
  3349. r->name = "a0";
  3350. break;
  3351. case GDB_REGNO_A1:
  3352. r->name = "a1";
  3353. break;
  3354. case GDB_REGNO_A2:
  3355. r->name = "a2";
  3356. break;
  3357. case GDB_REGNO_A3:
  3358. r->name = "a3";
  3359. break;
  3360. case GDB_REGNO_A4:
  3361. r->name = "a4";
  3362. break;
  3363. case GDB_REGNO_A5:
  3364. r->name = "a5";
  3365. break;
  3366. case GDB_REGNO_A6:
  3367. r->name = "a6";
  3368. break;
  3369. case GDB_REGNO_A7:
  3370. r->name = "a7";
  3371. break;
  3372. case GDB_REGNO_S2:
  3373. r->name = "s2";
  3374. break;
  3375. case GDB_REGNO_S3:
  3376. r->name = "s3";
  3377. break;
  3378. case GDB_REGNO_S4:
  3379. r->name = "s4";
  3380. break;
  3381. case GDB_REGNO_S5:
  3382. r->name = "s5";
  3383. break;
  3384. case GDB_REGNO_S6:
  3385. r->name = "s6";
  3386. break;
  3387. case GDB_REGNO_S7:
  3388. r->name = "s7";
  3389. break;
  3390. case GDB_REGNO_S8:
  3391. r->name = "s8";
  3392. break;
  3393. case GDB_REGNO_S9:
  3394. r->name = "s9";
  3395. break;
  3396. case GDB_REGNO_S10:
  3397. r->name = "s10";
  3398. break;
  3399. case GDB_REGNO_S11:
  3400. r->name = "s11";
  3401. break;
  3402. case GDB_REGNO_T3:
  3403. r->name = "t3";
  3404. break;
  3405. case GDB_REGNO_T4:
  3406. r->name = "t4";
  3407. break;
  3408. case GDB_REGNO_T5:
  3409. r->name = "t5";
  3410. break;
  3411. case GDB_REGNO_T6:
  3412. r->name = "t6";
  3413. break;
  3414. }
  3415. r->group = "general";
  3416. r->feature = &feature_cpu;
  3417. } else if (number == GDB_REGNO_PC) {
  3418. r->caller_save = true;
  3419. sprintf(reg_name, "pc");
  3420. r->group = "general";
  3421. r->feature = &feature_cpu;
  3422. } else if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
  3423. r->caller_save = true;
  3424. if (riscv_supports_extension(target, hartid, 'D')) {
  3425. r->size = 64;
  3426. if (riscv_supports_extension(target, hartid, 'F'))
  3427. r->reg_data_type = &type_ieee_single_double;
  3428. else
  3429. r->reg_data_type = &type_ieee_double;
  3430. } else if (riscv_supports_extension(target, hartid, 'F')) {
  3431. r->reg_data_type = &type_ieee_single;
  3432. r->size = 32;
  3433. } else {
  3434. r->exist = false;
  3435. }
  3436. switch (number) {
  3437. case GDB_REGNO_FT0:
  3438. r->name = "ft0";
  3439. break;
  3440. case GDB_REGNO_FT1:
  3441. r->name = "ft1";
  3442. break;
  3443. case GDB_REGNO_FT2:
  3444. r->name = "ft2";
  3445. break;
  3446. case GDB_REGNO_FT3:
  3447. r->name = "ft3";
  3448. break;
  3449. case GDB_REGNO_FT4:
  3450. r->name = "ft4";
  3451. break;
  3452. case GDB_REGNO_FT5:
  3453. r->name = "ft5";
  3454. break;
  3455. case GDB_REGNO_FT6:
  3456. r->name = "ft6";
  3457. break;
  3458. case GDB_REGNO_FT7:
  3459. r->name = "ft7";
  3460. break;
  3461. case GDB_REGNO_FS0:
  3462. r->name = "fs0";
  3463. break;
  3464. case GDB_REGNO_FS1:
  3465. r->name = "fs1";
  3466. break;
  3467. case GDB_REGNO_FA0:
  3468. r->name = "fa0";
  3469. break;
  3470. case GDB_REGNO_FA1:
  3471. r->name = "fa1";
  3472. break;
  3473. case GDB_REGNO_FA2:
  3474. r->name = "fa2";
  3475. break;
  3476. case GDB_REGNO_FA3:
  3477. r->name = "fa3";
  3478. break;
  3479. case GDB_REGNO_FA4:
  3480. r->name = "fa4";
  3481. break;
  3482. case GDB_REGNO_FA5:
  3483. r->name = "fa5";
  3484. break;
  3485. case GDB_REGNO_FA6:
  3486. r->name = "fa6";
  3487. break;
  3488. case GDB_REGNO_FA7:
  3489. r->name = "fa7";
  3490. break;
  3491. case GDB_REGNO_FS2:
  3492. r->name = "fs2";
  3493. break;
  3494. case GDB_REGNO_FS3:
  3495. r->name = "fs3";
  3496. break;
  3497. case GDB_REGNO_FS4:
  3498. r->name = "fs4";
  3499. break;
  3500. case GDB_REGNO_FS5:
  3501. r->name = "fs5";
  3502. break;
  3503. case GDB_REGNO_FS6:
  3504. r->name = "fs6";
  3505. break;
  3506. case GDB_REGNO_FS7:
  3507. r->name = "fs7";
  3508. break;
  3509. case GDB_REGNO_FS8:
  3510. r->name = "fs8";
  3511. break;
  3512. case GDB_REGNO_FS9:
  3513. r->name = "fs9";
  3514. break;
  3515. case GDB_REGNO_FS10:
  3516. r->name = "fs10";
  3517. break;
  3518. case GDB_REGNO_FS11:
  3519. r->name = "fs11";
  3520. break;
  3521. case GDB_REGNO_FT8:
  3522. r->name = "ft8";
  3523. break;
  3524. case GDB_REGNO_FT9:
  3525. r->name = "ft9";
  3526. break;
  3527. case GDB_REGNO_FT10:
  3528. r->name = "ft10";
  3529. break;
  3530. case GDB_REGNO_FT11:
  3531. r->name = "ft11";
  3532. break;
  3533. }
  3534. r->group = "float";
  3535. r->feature = &feature_fpu;
  3536. } else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
  3537. r->group = "csr";
  3538. r->feature = &feature_csr;
  3539. unsigned csr_number = number - GDB_REGNO_CSR0;
  3540. while (csr_info[csr_info_index].number < csr_number &&
  3541. csr_info_index < ARRAY_SIZE(csr_info) - 1) {
  3542. csr_info_index++;
  3543. }
  3544. if (csr_info[csr_info_index].number == csr_number) {
  3545. r->name = csr_info[csr_info_index].name;
  3546. } else {
  3547. sprintf(reg_name, "csr%d", csr_number);
  3548. /* Assume unnamed registers don't exist, unless we have some
  3549. * configuration that tells us otherwise. That's important
  3550. * because eg. Eclipse crashes if a target has too many
  3551. * registers, and apparently has no way of only showing a
  3552. * subset of registers in any case. */
  3553. r->exist = false;
  3554. }
  3555. switch (csr_number) {
  3556. case CSR_FFLAGS:
  3557. case CSR_FRM:
  3558. case CSR_FCSR:
  3559. r->exist = riscv_supports_extension(target, hartid, 'F');
  3560. r->group = "float";
  3561. r->feature = &feature_fpu;
  3562. break;
  3563. case CSR_SSTATUS:
  3564. case CSR_STVEC:
  3565. case CSR_SIP:
  3566. case CSR_SIE:
  3567. case CSR_SCOUNTEREN:
  3568. case CSR_SSCRATCH:
  3569. case CSR_SEPC:
  3570. case CSR_SCAUSE:
  3571. case CSR_STVAL:
  3572. case CSR_SATP:
  3573. r->exist = riscv_supports_extension(target, hartid, 'S');
  3574. break;
  3575. case CSR_MEDELEG:
  3576. case CSR_MIDELEG:
  3577. /* "In systems with only M-mode, or with both M-mode and
  3578. * U-mode but without U-mode trap support, the medeleg and
  3579. * mideleg registers should not exist." */
  3580. r->exist = riscv_supports_extension(target, hartid, 'S') ||
  3581. riscv_supports_extension(target, hartid, 'N');
  3582. break;
  3583. case CSR_PMPCFG1:
  3584. case CSR_PMPCFG3:
  3585. case CSR_CYCLEH:
  3586. case CSR_TIMEH:
  3587. case CSR_INSTRETH:
  3588. case CSR_HPMCOUNTER3H:
  3589. case CSR_HPMCOUNTER4H:
  3590. case CSR_HPMCOUNTER5H:
  3591. case CSR_HPMCOUNTER6H:
  3592. case CSR_HPMCOUNTER7H:
  3593. case CSR_HPMCOUNTER8H:
  3594. case CSR_HPMCOUNTER9H:
  3595. case CSR_HPMCOUNTER10H:
  3596. case CSR_HPMCOUNTER11H:
  3597. case CSR_HPMCOUNTER12H:
  3598. case CSR_HPMCOUNTER13H:
  3599. case CSR_HPMCOUNTER14H:
  3600. case CSR_HPMCOUNTER15H:
  3601. case CSR_HPMCOUNTER16H:
  3602. case CSR_HPMCOUNTER17H:
  3603. case CSR_HPMCOUNTER18H:
  3604. case CSR_HPMCOUNTER19H:
  3605. case CSR_HPMCOUNTER20H:
  3606. case CSR_HPMCOUNTER21H:
  3607. case CSR_HPMCOUNTER22H:
  3608. case CSR_HPMCOUNTER23H:
  3609. case CSR_HPMCOUNTER24H:
  3610. case CSR_HPMCOUNTER25H:
  3611. case CSR_HPMCOUNTER26H:
  3612. case CSR_HPMCOUNTER27H:
  3613. case CSR_HPMCOUNTER28H:
  3614. case CSR_HPMCOUNTER29H:
  3615. case CSR_HPMCOUNTER30H:
  3616. case CSR_HPMCOUNTER31H:
  3617. case CSR_MCYCLEH:
  3618. case CSR_MINSTRETH:
  3619. case CSR_MHPMCOUNTER3H:
  3620. case CSR_MHPMCOUNTER4H:
  3621. case CSR_MHPMCOUNTER5H:
  3622. case CSR_MHPMCOUNTER6H:
  3623. case CSR_MHPMCOUNTER7H:
  3624. case CSR_MHPMCOUNTER8H:
  3625. case CSR_MHPMCOUNTER9H:
  3626. case CSR_MHPMCOUNTER10H:
  3627. case CSR_MHPMCOUNTER11H:
  3628. case CSR_MHPMCOUNTER12H:
  3629. case CSR_MHPMCOUNTER13H:
  3630. case CSR_MHPMCOUNTER14H:
  3631. case CSR_MHPMCOUNTER15H:
  3632. case CSR_MHPMCOUNTER16H:
  3633. case CSR_MHPMCOUNTER17H:
  3634. case CSR_MHPMCOUNTER18H:
  3635. case CSR_MHPMCOUNTER19H:
  3636. case CSR_MHPMCOUNTER20H:
  3637. case CSR_MHPMCOUNTER21H:
  3638. case CSR_MHPMCOUNTER22H:
  3639. case CSR_MHPMCOUNTER23H:
  3640. case CSR_MHPMCOUNTER24H:
  3641. case CSR_MHPMCOUNTER25H:
  3642. case CSR_MHPMCOUNTER26H:
  3643. case CSR_MHPMCOUNTER27H:
  3644. case CSR_MHPMCOUNTER28H:
  3645. case CSR_MHPMCOUNTER29H:
  3646. case CSR_MHPMCOUNTER30H:
  3647. case CSR_MHPMCOUNTER31H:
  3648. r->exist = riscv_xlen(target) == 32;
  3649. break;
  3650. case CSR_VSTART:
  3651. case CSR_VXSAT:
  3652. case CSR_VXRM:
  3653. case CSR_VL:
  3654. case CSR_VTYPE:
  3655. case CSR_VLENB:
  3656. r->exist = riscv_supports_extension(target, hartid, 'V');
  3657. break;
  3658. }
  3659. if (!r->exist && expose_csr) {
  3660. for (unsigned i = 0; expose_csr[i].low <= expose_csr[i].high; i++) {
  3661. if (csr_number >= expose_csr[i].low && csr_number <= expose_csr[i].high) {
  3662. LOG_INFO("Exposing additional CSR %d", csr_number);
  3663. r->exist = true;
  3664. break;
  3665. }
  3666. }
  3667. }
  3668. } else if (number == GDB_REGNO_PRIV) {
  3669. sprintf(reg_name, "priv");
  3670. r->group = "general";
  3671. r->feature = &feature_virtual;
  3672. r->size = 8;
  3673. } else if (number >= GDB_REGNO_V0 && number <= GDB_REGNO_V31) {
  3674. r->caller_save = false;
  3675. r->exist = riscv_supports_extension(target, hartid, 'V') && info->vlenb[hartid];
  3676. r->size = info->vlenb[hartid] * 8;
  3677. sprintf(reg_name, "v%d", number - GDB_REGNO_V0);
  3678. r->group = "vector";
  3679. r->feature = &feature_vector;
  3680. r->reg_data_type = &info->type_vector;
  3681. } else if (number >= GDB_REGNO_COUNT) {
  3682. /* Custom registers. */
  3683. assert(expose_custom);
  3684. range_t *range = &expose_custom[custom_range_index];
  3685. assert(range->low <= range->high);
  3686. unsigned custom_number = range->low + custom_within_range;
  3687. r->group = "custom";
  3688. r->feature = &feature_custom;
  3689. r->arch_info = calloc(1, sizeof(riscv_reg_info_t));
  3690. if (!r->arch_info)
  3691. return ERROR_FAIL;
  3692. ((riscv_reg_info_t *) r->arch_info)->target = target;
  3693. ((riscv_reg_info_t *) r->arch_info)->custom_number = custom_number;
  3694. sprintf(reg_name, "custom%d", custom_number);
  3695. custom_within_range++;
  3696. if (custom_within_range > range->high - range->low) {
  3697. custom_within_range = 0;
  3698. custom_range_index++;
  3699. }
  3700. }
  3701. if (reg_name[0])
  3702. r->name = reg_name;
  3703. reg_name += strlen(reg_name) + 1;
  3704. assert(reg_name < info->reg_names + target->reg_cache->num_regs *
  3705. max_reg_name_len);
  3706. r->value = info->reg_cache_values[number];
  3707. }
  3708. return ERROR_OK;
  3709. }
  3710. void riscv_add_bscan_tunneled_scan(struct target *target, struct scan_field *field,
  3711. riscv_bscan_tunneled_scan_context_t *ctxt)
  3712. {
  3713. jtag_add_ir_scan(target->tap, &select_user4, TAP_IDLE);
  3714. memset(ctxt->tunneled_dr, 0, sizeof(ctxt->tunneled_dr));
  3715. if (bscan_tunnel_type == BSCAN_TUNNEL_DATA_REGISTER) {
  3716. ctxt->tunneled_dr[3].num_bits = 1;
  3717. ctxt->tunneled_dr[3].out_value = bscan_one;
  3718. ctxt->tunneled_dr[2].num_bits = 7;
  3719. ctxt->tunneled_dr_width = field->num_bits;
  3720. ctxt->tunneled_dr[2].out_value = &ctxt->tunneled_dr_width;
  3721. /* for BSCAN tunnel, there is a one-TCK skew between shift in and shift out, so
  3722. scanning num_bits + 1, and then will right shift the input field after executing the queues */
  3723. ctxt->tunneled_dr[1].num_bits = field->num_bits + 1;
  3724. ctxt->tunneled_dr[1].out_value = field->out_value;
  3725. ctxt->tunneled_dr[1].in_value = field->in_value;
  3726. ctxt->tunneled_dr[0].num_bits = 3;
  3727. ctxt->tunneled_dr[0].out_value = bscan_zero;
  3728. } else {
  3729. /* BSCAN_TUNNEL_NESTED_TAP */
  3730. ctxt->tunneled_dr[0].num_bits = 1;
  3731. ctxt->tunneled_dr[0].out_value = bscan_one;
  3732. ctxt->tunneled_dr[1].num_bits = 7;
  3733. ctxt->tunneled_dr_width = field->num_bits;
  3734. ctxt->tunneled_dr[1].out_value = &ctxt->tunneled_dr_width;
  3735. /* for BSCAN tunnel, there is a one-TCK skew between shift in and shift out, so
  3736. scanning num_bits + 1, and then will right shift the input field after executing the queues */
  3737. ctxt->tunneled_dr[2].num_bits = field->num_bits + 1;
  3738. ctxt->tunneled_dr[2].out_value = field->out_value;
  3739. ctxt->tunneled_dr[2].in_value = field->in_value;
  3740. ctxt->tunneled_dr[3].num_bits = 3;
  3741. ctxt->tunneled_dr[3].out_value = bscan_zero;
  3742. }
  3743. jtag_add_dr_scan(target->tap, ARRAY_SIZE(ctxt->tunneled_dr), ctxt->tunneled_dr, TAP_IDLE);
  3744. }