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.
 
 
 
 
 
 

1194 lines
30 KiB

  1. /*
  2. * MIPS64 generic target support
  3. *
  4. * Copyright (C) 2014 by Andrey Sidorov <anysidorov@gmail.com>
  5. * Copyright (C) 2014 by Aleksey Kuleshov <rndfax@yandex.ru>
  6. * Copyright (C) 2014-2019 by Peter Mamonov <pmamonov@gmail.com>
  7. *
  8. * Based on the work of:
  9. * Copyright (C) 2008 by Spencer Oliver
  10. * Copyright (C) 2008 by David T.L. Wong
  11. *
  12. * SPDX-License-Identifier: GPL-2.0-or-later
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include "breakpoints.h"
  18. #include "mips32.h"
  19. #include "mips64.h"
  20. #include "mips_mips64.h"
  21. #include "target_type.h"
  22. #include "register.h"
  23. static int mips_mips64_unset_breakpoint(struct target *target,
  24. struct breakpoint *breakpoint);
  25. static uint64_t mips64_extend_sign(uint64_t addr)
  26. {
  27. if (addr >> 32)
  28. return addr;
  29. if (addr >> 31)
  30. return addr | (ULLONG_MAX << 32);
  31. return addr;
  32. }
  33. static int mips_mips64_examine_debug_reason(struct target *target)
  34. {
  35. if ((target->debug_reason != DBG_REASON_DBGRQ)
  36. && (target->debug_reason != DBG_REASON_SINGLESTEP))
  37. target->debug_reason = DBG_REASON_BREAKPOINT;
  38. return ERROR_OK;
  39. }
  40. static int mips_mips64_debug_entry(struct target *target)
  41. {
  42. struct mips64_common *mips64 = target->arch_info;
  43. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  44. struct reg *pc = &mips64->core_cache->reg_list[MIPS64_PC];
  45. mips64_save_context(target);
  46. /* make sure stepping disabled, SSt bit in CP0 debug register cleared */
  47. mips64_ejtag_config_step(ejtag_info, 0);
  48. /* make sure break unit configured */
  49. mips64_configure_break_unit(target);
  50. /* attempt to find halt reason */
  51. mips_mips64_examine_debug_reason(target);
  52. LOG_DEBUG("entered debug state at PC 0x%" PRIx64 ", target->state: %s",
  53. buf_get_u64(pc->value, 0, 64), target_state_name(target));
  54. return ERROR_OK;
  55. }
  56. static int mips_mips64_poll(struct target *target)
  57. {
  58. int retval;
  59. struct mips64_common *mips64 = target->arch_info;
  60. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  61. uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl;
  62. /* read ejtag control reg */
  63. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  64. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  65. /* clear this bit before handling polling
  66. * as after reset registers will read zero */
  67. if (ejtag_ctrl & EJTAG_CTRL_ROCC) {
  68. /* we have detected a reset, clear flag
  69. * otherwise ejtag will not work */
  70. ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_ROCC;
  71. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  72. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  73. LOG_DEBUG("Reset Detected");
  74. }
  75. /* check for processor halted */
  76. if (ejtag_ctrl & EJTAG_CTRL_BRKST) {
  77. if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET)) {
  78. target->state = TARGET_HALTED;
  79. retval = mips_mips64_debug_entry(target);
  80. if (retval != ERROR_OK)
  81. return retval;
  82. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  83. } else if (target->state == TARGET_DEBUG_RUNNING) {
  84. target->state = TARGET_HALTED;
  85. retval = mips_mips64_debug_entry(target);
  86. if (retval != ERROR_OK)
  87. return retval;
  88. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  89. }
  90. } else {
  91. target->state = TARGET_RUNNING;
  92. }
  93. return ERROR_OK;
  94. }
  95. static int mips_mips64_halt(struct target *target)
  96. {
  97. struct mips64_common *mips64 = target->arch_info;
  98. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  99. LOG_DEBUG("target->state: %s",
  100. target_state_name(target));
  101. if (target->state == TARGET_HALTED) {
  102. LOG_DEBUG("target was already halted");
  103. return ERROR_OK;
  104. }
  105. if (target->state == TARGET_UNKNOWN)
  106. LOG_WARNING("target was in unknown state when halt was requested");
  107. if (target->state == TARGET_RESET) {
  108. if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst()) {
  109. LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
  110. return ERROR_TARGET_FAILURE;
  111. } else {
  112. /* we came here in a reset_halt or reset_init sequence
  113. * debug entry was already prepared in mips64_prepare_reset_halt()
  114. */
  115. target->debug_reason = DBG_REASON_DBGRQ;
  116. return ERROR_OK;
  117. }
  118. }
  119. /* break processor */
  120. mips_ejtag_enter_debug(ejtag_info);
  121. target->debug_reason = DBG_REASON_DBGRQ;
  122. return ERROR_OK;
  123. }
  124. static int mips_mips64_assert_reset(struct target *target)
  125. {
  126. struct mips64_common *mips64 = target->arch_info;
  127. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  128. int retval;
  129. LOG_DEBUG("target->state: %s",
  130. target_state_name(target));
  131. enum reset_types jtag_reset_config = jtag_get_reset_config();
  132. if (!(jtag_reset_config & RESET_HAS_SRST)) {
  133. LOG_ERROR("Can't assert SRST");
  134. return ERROR_FAIL;
  135. }
  136. if (target->reset_halt)
  137. /* use hardware to catch reset */
  138. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_EJTAGBOOT);
  139. else
  140. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT);
  141. /* here we should issue a srst only, but we may have to assert trst as well */
  142. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  143. jtag_add_reset(1, 1);
  144. else
  145. jtag_add_reset(0, 1);
  146. target->state = TARGET_RESET;
  147. jtag_add_sleep(5000);
  148. retval = mips64_invalidate_core_regs(target);
  149. if (retval != ERROR_OK)
  150. return retval;
  151. if (target->reset_halt) {
  152. retval = target_halt(target);
  153. if (retval != ERROR_OK)
  154. return retval;
  155. }
  156. return ERROR_OK;
  157. }
  158. static int mips_mips64_deassert_reset(struct target *target)
  159. {
  160. LOG_DEBUG("target->state: %s",
  161. target_state_name(target));
  162. /* deassert reset lines */
  163. jtag_add_reset(0, 0);
  164. return ERROR_OK;
  165. }
  166. static int mips_mips64_soft_reset_halt(struct target *target)
  167. {
  168. /* TODO */
  169. return ERROR_OK;
  170. }
  171. static int mips_mips64_single_step_core(struct target *target)
  172. {
  173. struct mips64_common *mips64 = target->arch_info;
  174. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  175. int retval;
  176. /* configure single step mode */
  177. mips64_ejtag_config_step(ejtag_info, 1);
  178. /* disable interrupts while stepping */
  179. retval = mips64_enable_interrupts(target, false);
  180. if (retval != ERROR_OK)
  181. return retval;
  182. /* exit debug mode */
  183. retval = mips64_ejtag_exit_debug(ejtag_info);
  184. if (retval != ERROR_OK)
  185. return retval;
  186. mips_mips64_debug_entry(target);
  187. return ERROR_OK;
  188. }
  189. /* TODO: HW breakpoints are in EJTAG spec. Should we share it for MIPS32? */
  190. static int mips_mips64_set_hwbp(struct target *target, struct breakpoint *bp)
  191. {
  192. struct mips64_common *mips64 = target->arch_info;
  193. struct mips64_comparator *c, *cl = mips64->inst_break_list;
  194. uint64_t bp_value;
  195. int retval, bp_num = 0;
  196. while (cl[bp_num].used && (bp_num < mips64->num_inst_bpoints))
  197. bp_num++;
  198. if (bp_num >= mips64->num_inst_bpoints) {
  199. LOG_DEBUG("ERROR Can not find free FP Comparator(bpid: %" PRIu32 ")",
  200. bp->unique_id);
  201. LOG_WARNING("ERROR Can not find free FP Comparator");
  202. exit(-1);
  203. }
  204. c = &cl[bp_num];
  205. c->used = true;
  206. c->bp_value = bp->address;
  207. bp_value = bp->address;
  208. /* Instruction Breakpoint Address n (IBAn) Register */
  209. retval = target_write_u64(target, c->reg_address, bp_value);
  210. if (retval != ERROR_OK)
  211. return retval;
  212. /* TODO: use defines */
  213. /* Instruction Breakpoint Address Mask n (IBMn) Register */
  214. retval = target_write_u64(target, c->reg_address + 0x08, 0);
  215. if (retval != ERROR_OK)
  216. return retval;
  217. /* Instruction Breakpoint Control n (IBCn) Register */
  218. retval = target_write_u64(target, c->reg_address + 0x18, 1);
  219. if (retval != ERROR_OK)
  220. return retval;
  221. LOG_DEBUG("bpid: %" PRIu32 ", bp_num %i bp_value 0x%" PRIx64, bp->unique_id,
  222. bp_num, c->bp_value);
  223. return ERROR_OK;
  224. }
  225. /* TODO: is it MIPS64 or MIPS32 instruction. If MIPS32, can it be shared with
  226. * MIPS32 code? */
  227. static int mips_mips64_set_sdbbp(struct target *target, struct breakpoint *bp)
  228. {
  229. uint32_t verify;
  230. int retval;
  231. retval = target_read_memory(target,
  232. bp->address, bp->length, 1,
  233. bp->orig_instr);
  234. if (retval != ERROR_OK)
  235. return retval;
  236. retval = target_write_u32(target, bp->address, MIPS64_SDBBP);
  237. if (retval != ERROR_OK)
  238. return retval;
  239. retval = target_read_u32(target, bp->address, &verify);
  240. if (retval != ERROR_OK)
  241. return retval;
  242. if (verify != MIPS64_SDBBP) {
  243. LOG_ERROR("Unable to set 32bit breakpoint at address %16" PRIx64,
  244. bp->address);
  245. retval = ERROR_FAIL;
  246. }
  247. return retval;
  248. }
  249. /* TODO do MIPS64 support MIPS16 instructions? Can it be shared with MIPS32
  250. * code? */
  251. static int mips_mips16_set_sdbbp(struct target *target, struct breakpoint *bp)
  252. {
  253. uint32_t isa_req = bp->length & 1;
  254. uint16_t verify;
  255. int retval;
  256. retval = target_read_memory(target,
  257. bp->address, bp->length, 1,
  258. bp->orig_instr);
  259. if (retval != ERROR_OK)
  260. return retval;
  261. retval = target_write_u16(target, bp->address, MIPS16_SDBBP(isa_req));
  262. if (retval != ERROR_OK)
  263. return retval;
  264. retval = target_read_u16(target, bp->address, &verify);
  265. if (retval != ERROR_OK)
  266. return retval;
  267. if (verify != MIPS16_SDBBP(isa_req)) {
  268. LOG_ERROR("Unable to set 16bit breakpoint at address %16" PRIx64,
  269. bp->address);
  270. retval = ERROR_FAIL;
  271. }
  272. return retval;
  273. }
  274. static int mips_mips64_set_breakpoint(struct target *target,
  275. struct breakpoint *bp)
  276. {
  277. int retval;
  278. if (bp->set) {
  279. LOG_WARNING("breakpoint already set");
  280. return ERROR_OK;
  281. }
  282. if (bp->type == BKPT_HARD) {
  283. retval = mips_mips64_set_hwbp(target, bp);
  284. } else {
  285. LOG_DEBUG("bpid: %" PRIu32, bp->unique_id);
  286. switch (bp->length) {
  287. case MIPS64_SDBBP_SIZE:
  288. retval = mips_mips64_set_sdbbp(target, bp);
  289. break;
  290. case MIPS16_SDBBP_SIZE:
  291. retval = mips_mips16_set_sdbbp(target, bp);
  292. break;
  293. default:
  294. retval = ERROR_FAIL;
  295. }
  296. }
  297. if (retval != ERROR_OK) {
  298. LOG_ERROR("can't unset breakpoint. Some thing wrong happened");
  299. return retval;
  300. }
  301. bp->set = true;
  302. return ERROR_OK;
  303. }
  304. static int mips_mips64_enable_breakpoints(struct target *target)
  305. {
  306. struct breakpoint *bp = target->breakpoints;
  307. int retval = ERROR_OK;
  308. /* set any pending breakpoints */
  309. while (bp) {
  310. if (!bp->set) {
  311. retval = mips_mips64_set_breakpoint(target, bp);
  312. if (retval != ERROR_OK)
  313. return retval;
  314. }
  315. bp = bp->next;
  316. }
  317. return ERROR_OK;
  318. }
  319. /* TODO: HW data breakpoints are in EJTAG spec. Should we share it for MIPS32? */
  320. static int mips_mips64_set_watchpoint(struct target *target,
  321. struct watchpoint *watchpoint)
  322. {
  323. uint64_t wp_value;
  324. struct mips64_common *mips64 = target->arch_info;
  325. struct mips64_comparator *c, *cl = mips64->data_break_list;
  326. int retval, wp_num = 0;
  327. /*
  328. * watchpoint enabled, ignore all byte lanes in value register
  329. * and exclude both load and store accesses from watchpoint
  330. * condition evaluation
  331. */
  332. int enable = EJTAG_DBCN_NOSB | EJTAG_DBCN_NOLB | EJTAG_DBCN_BE
  333. | (0xff << EJTAG_DBCN_BLM_SHIFT);
  334. if (watchpoint->set) {
  335. LOG_WARNING("watchpoint already set");
  336. return ERROR_OK;
  337. }
  338. while (cl[wp_num].used && (wp_num < mips64->num_data_bpoints))
  339. wp_num++;
  340. if (wp_num >= mips64->num_data_bpoints) {
  341. LOG_ERROR("ERROR Can not find free comparator");
  342. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  343. }
  344. if (watchpoint->length != 4) {
  345. LOG_ERROR("Only watchpoints of length 4 are supported");
  346. return ERROR_TARGET_UNALIGNED_ACCESS;
  347. }
  348. if (watchpoint->address % 4) {
  349. LOG_ERROR("Watchpoints address should be word aligned");
  350. return ERROR_TARGET_UNALIGNED_ACCESS;
  351. }
  352. switch (watchpoint->rw) {
  353. case WPT_READ:
  354. enable &= ~EJTAG_DBCN_NOLB;
  355. break;
  356. case WPT_WRITE:
  357. enable &= ~EJTAG_DBCN_NOSB;
  358. break;
  359. case WPT_ACCESS:
  360. enable &= ~(EJTAG_DBCN_NOLB | EJTAG_DBCN_NOSB);
  361. break;
  362. default:
  363. LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
  364. }
  365. c = &cl[wp_num];
  366. watchpoint->set = wp_num + 1;
  367. c->used = true;
  368. c->bp_value = watchpoint->address;
  369. wp_value = watchpoint->address;
  370. if (wp_value & 0x80000000)
  371. wp_value |= ULLONG_MAX << 32;
  372. retval = target_write_u64(target, c->reg_address, wp_value);
  373. if (retval != ERROR_OK)
  374. return retval;
  375. retval = target_write_u64(target, c->reg_address + 0x08, 0);
  376. if (retval != ERROR_OK)
  377. return retval;
  378. retval = target_write_u64(target, c->reg_address + 0x10, 0);
  379. if (retval != ERROR_OK)
  380. return retval;
  381. retval = target_write_u64(target, c->reg_address + 0x18, enable);
  382. if (retval != ERROR_OK)
  383. return retval;
  384. retval = target_write_u64(target, c->reg_address + 0x20, 0);
  385. if (retval != ERROR_OK)
  386. return retval;
  387. LOG_DEBUG("wp_num %i bp_value 0x%" PRIx64 "", wp_num, c->bp_value);
  388. return ERROR_OK;
  389. }
  390. static int mips_mips64_enable_watchpoints(struct target *target)
  391. {
  392. struct watchpoint *watchpoint = target->watchpoints;
  393. int retval;
  394. /* set any pending watchpoints */
  395. while (watchpoint) {
  396. if (watchpoint->set == 0) {
  397. retval = mips_mips64_set_watchpoint(target, watchpoint);
  398. if (retval != ERROR_OK)
  399. return retval;
  400. }
  401. watchpoint = watchpoint->next;
  402. }
  403. return ERROR_OK;
  404. }
  405. static int mips_mips64_unset_hwbp(struct target *target, struct breakpoint *bp)
  406. {
  407. struct mips64_common *mips64 = target->arch_info;
  408. struct mips64_comparator *comparator_list = mips64->inst_break_list;
  409. int bp_num;
  410. bp_num = bp->set - 1;
  411. if ((bp_num < 0) || (bp_num >= mips64->num_inst_bpoints)) {
  412. LOG_DEBUG("Invalid FP Comparator number in breakpoint (bpid: %" PRIu32 ")",
  413. bp->unique_id);
  414. return ERROR_OK;
  415. }
  416. LOG_DEBUG("bpid: %" PRIu32 " - releasing hw: %d", bp->unique_id, bp_num);
  417. comparator_list[bp_num].used = false;
  418. comparator_list[bp_num].bp_value = 0;
  419. return target_write_u64(target,
  420. comparator_list[bp_num].reg_address + 0x18, 0);
  421. }
  422. static int mips_mips64_unset_sdbbp(struct target *target, struct breakpoint *bp)
  423. {
  424. uint8_t buf[MIPS64_SDBBP_SIZE];
  425. uint32_t instr;
  426. int retval;
  427. retval = target_read_memory(target, bp->address, MIPS64_SDBBP_SIZE, 1,
  428. &buf[0]);
  429. if (retval != ERROR_OK)
  430. return retval;
  431. instr = target_buffer_get_u32(target, &buf[0]);
  432. if (instr != MIPS64_SDBBP)
  433. return ERROR_OK;
  434. return target_write_memory(target, bp->address, MIPS64_SDBBP_SIZE, 1,
  435. bp->orig_instr);
  436. }
  437. static int mips_mips16_unset_sdbbp(struct target *target, struct breakpoint *bp)
  438. {
  439. uint8_t buf[MIPS16_SDBBP_SIZE];
  440. uint16_t instr;
  441. int retval;
  442. retval = target_read_memory(target, bp->address, MIPS16_SDBBP_SIZE, 1,
  443. &buf[0]);
  444. if (retval != ERROR_OK)
  445. return retval;
  446. instr = target_buffer_get_u16(target, &buf[0]);
  447. if (instr != MIPS16_SDBBP(bp->length & 1))
  448. return ERROR_OK;
  449. return target_write_memory(target, bp->address, MIPS16_SDBBP_SIZE, 1,
  450. bp->orig_instr);
  451. }
  452. static int mips_mips64_unset_breakpoint(struct target *target,
  453. struct breakpoint *bp)
  454. {
  455. /* get pointers to arch-specific information */
  456. int retval;
  457. if (!bp->set) {
  458. LOG_WARNING("breakpoint not set");
  459. return ERROR_OK;
  460. }
  461. if (bp->type == BKPT_HARD) {
  462. retval = mips_mips64_unset_hwbp(target, bp);
  463. } else {
  464. LOG_DEBUG("bpid: %" PRIu32, bp->unique_id);
  465. switch (bp->length) {
  466. case MIPS64_SDBBP_SIZE:
  467. retval = mips_mips64_unset_sdbbp(target, bp);
  468. break;
  469. case MIPS16_SDBBP_SIZE:
  470. retval = mips_mips16_unset_sdbbp(target, bp);
  471. break;
  472. default:
  473. retval = ERROR_FAIL;
  474. }
  475. }
  476. if (retval != ERROR_OK) {
  477. LOG_ERROR("can't unset breakpoint. Some thing wrong happened");
  478. return retval;
  479. }
  480. bp->set = false;
  481. return ERROR_OK;
  482. }
  483. static int mips_mips64_resume(struct target *target, int current,
  484. uint64_t address, int handle_breakpoints,
  485. int debug_execution)
  486. {
  487. struct mips64_common *mips64 = target->arch_info;
  488. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  489. int retval = ERROR_OK;
  490. uint64_t resume_pc;
  491. struct reg *pc;
  492. if (mips64->mips64mode32)
  493. address = mips64_extend_sign(address);
  494. if (target->state != TARGET_HALTED) {
  495. LOG_WARNING("target not halted %d", target->state);
  496. return ERROR_TARGET_NOT_HALTED;
  497. }
  498. if (!debug_execution) {
  499. target_free_all_working_areas(target);
  500. retval = mips_mips64_enable_breakpoints(target);
  501. if (retval != ERROR_OK)
  502. return retval;
  503. retval = mips_mips64_enable_watchpoints(target);
  504. if (retval != ERROR_OK)
  505. return retval;
  506. }
  507. pc = &mips64->core_cache->reg_list[MIPS64_PC];
  508. /* current = 1: continue on current pc, otherwise continue at <address> */
  509. if (!current) {
  510. buf_set_u64(pc->value, 0, 64, address);
  511. pc->dirty = 1;
  512. pc->valid = 1;
  513. }
  514. resume_pc = buf_get_u64(pc->value, 0, 64);
  515. retval = mips64_restore_context(target);
  516. if (retval != ERROR_OK)
  517. return retval;
  518. /* the front-end may request us not to handle breakpoints */
  519. if (handle_breakpoints) {
  520. struct breakpoint *bp;
  521. /* Single step past breakpoint at current address */
  522. bp = breakpoint_find(target, (uint64_t) resume_pc);
  523. if (bp) {
  524. LOG_DEBUG("unset breakpoint at 0x%16.16" PRIx64 "",
  525. bp->address);
  526. retval = mips_mips64_unset_breakpoint(target, bp);
  527. if (retval != ERROR_OK)
  528. return retval;
  529. retval = mips_mips64_single_step_core(target);
  530. if (retval != ERROR_OK)
  531. return retval;
  532. retval = mips_mips64_set_breakpoint(target, bp);
  533. if (retval != ERROR_OK)
  534. return retval;
  535. }
  536. }
  537. /* enable interrupts if we are running */
  538. retval = mips64_enable_interrupts(target, !debug_execution);
  539. if (retval != ERROR_OK)
  540. return retval;
  541. /* exit debug mode */
  542. retval = mips64_ejtag_exit_debug(ejtag_info);
  543. if (retval != ERROR_OK)
  544. return retval;
  545. target->debug_reason = DBG_REASON_NOTHALTED;
  546. /* registers are now invalid */
  547. retval = mips64_invalidate_core_regs(target);
  548. if (retval != ERROR_OK)
  549. return retval;
  550. if (!debug_execution) {
  551. target->state = TARGET_RUNNING;
  552. retval = target_call_event_callbacks(target,
  553. TARGET_EVENT_RESUMED);
  554. if (retval != ERROR_OK)
  555. return retval;
  556. LOG_DEBUG("target resumed at 0x%" PRIx64 "", resume_pc);
  557. } else {
  558. target->state = TARGET_DEBUG_RUNNING;
  559. retval = target_call_event_callbacks(target,
  560. TARGET_EVENT_DEBUG_RESUMED);
  561. if (retval != ERROR_OK)
  562. return retval;
  563. LOG_DEBUG("target debug resumed at 0x%" PRIx64 "", resume_pc);
  564. }
  565. return ERROR_OK;
  566. }
  567. static int mips_mips64_step(struct target *target, int current,
  568. uint64_t address, int handle_breakpoints)
  569. {
  570. struct mips64_common *mips64 = target->arch_info;
  571. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  572. struct reg *pc = &mips64->core_cache->reg_list[MIPS64_PC];
  573. struct breakpoint *bp = NULL;
  574. int retval = ERROR_OK;
  575. if (target->state != TARGET_HALTED) {
  576. LOG_WARNING("target not halted");
  577. return ERROR_TARGET_NOT_HALTED;
  578. }
  579. if (mips64->mips64mode32)
  580. address = mips64_extend_sign(address);
  581. /* current = 1: continue on current pc, otherwise continue at
  582. * <address> */
  583. if (!current) {
  584. buf_set_u64(pc->value, 0, 64, address);
  585. pc->dirty = 1;
  586. pc->valid = 1;
  587. }
  588. /* the front-end may request us not to handle breakpoints */
  589. if (handle_breakpoints) {
  590. bp = breakpoint_find(target, buf_get_u64(pc->value, 0, 64));
  591. if (bp) {
  592. retval = mips_mips64_unset_breakpoint(target, bp);
  593. if (retval != ERROR_OK)
  594. return retval;
  595. }
  596. }
  597. retval = mips64_restore_context(target);
  598. if (retval != ERROR_OK)
  599. return retval;
  600. /* configure single step mode */
  601. retval = mips64_ejtag_config_step(ejtag_info, 1);
  602. if (retval != ERROR_OK)
  603. return retval;
  604. target->debug_reason = DBG_REASON_SINGLESTEP;
  605. retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  606. if (retval != ERROR_OK)
  607. return retval;
  608. /* disable interrupts while stepping */
  609. retval = mips64_enable_interrupts(target, false);
  610. if (retval != ERROR_OK)
  611. return retval;
  612. /* exit debug mode */
  613. retval = mips64_ejtag_exit_debug(ejtag_info);
  614. if (retval != ERROR_OK)
  615. return retval;
  616. /* registers are now invalid */
  617. retval = mips64_invalidate_core_regs(target);
  618. if (retval != ERROR_OK)
  619. return retval;
  620. if (bp) {
  621. retval = mips_mips64_set_breakpoint(target, bp);
  622. if (retval != ERROR_OK)
  623. return retval;
  624. }
  625. LOG_DEBUG("target stepped ");
  626. retval = mips_mips64_debug_entry(target);
  627. if (retval != ERROR_OK)
  628. return retval;
  629. return target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  630. }
  631. static int mips_mips64_add_breakpoint(struct target *target,
  632. struct breakpoint *bp)
  633. {
  634. struct mips64_common *mips64 = target->arch_info;
  635. if (mips64->mips64mode32)
  636. bp->address = mips64_extend_sign(bp->address);
  637. if (bp->type == BKPT_HARD) {
  638. if (mips64->num_inst_bpoints_avail < 1) {
  639. LOG_INFO("no hardware breakpoint available");
  640. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  641. }
  642. mips64->num_inst_bpoints_avail--;
  643. }
  644. return mips_mips64_set_breakpoint(target, bp);
  645. }
  646. static int mips_mips64_remove_breakpoint(struct target *target,
  647. struct breakpoint *bp)
  648. {
  649. /* get pointers to arch-specific information */
  650. struct mips64_common *mips64 = target->arch_info;
  651. int retval = ERROR_OK;
  652. if (target->state != TARGET_HALTED) {
  653. LOG_WARNING("target not halted");
  654. return ERROR_TARGET_NOT_HALTED;
  655. }
  656. if (bp->set)
  657. retval = mips_mips64_unset_breakpoint(target, bp);
  658. if (bp->type == BKPT_HARD)
  659. mips64->num_inst_bpoints_avail++;
  660. return retval;
  661. }
  662. static int mips_mips64_unset_watchpoint(struct target *target,
  663. struct watchpoint *watchpoint)
  664. {
  665. /* get pointers to arch-specific information */
  666. struct mips64_common *mips64 = target->arch_info;
  667. struct mips64_comparator *comparator_list = mips64->data_break_list;
  668. if (!watchpoint->set) {
  669. LOG_WARNING("watchpoint not set");
  670. return ERROR_OK;
  671. }
  672. int wp_num = watchpoint->set - 1;
  673. if ((wp_num < 0) || (wp_num >= mips64->num_data_bpoints)) {
  674. LOG_DEBUG("Invalid FP Comparator number in watchpoint");
  675. return ERROR_OK;
  676. }
  677. comparator_list[wp_num].used = false;
  678. comparator_list[wp_num].bp_value = 0;
  679. target_write_u64(target, comparator_list[wp_num].reg_address + 0x18, 0);
  680. watchpoint->set = 0;
  681. return ERROR_OK;
  682. }
  683. static int mips_mips64_add_watchpoint(struct target *target,
  684. struct watchpoint *watchpoint)
  685. {
  686. struct mips64_common *mips64 = target->arch_info;
  687. if (mips64->num_data_bpoints_avail < 1) {
  688. LOG_INFO("no hardware watchpoints available");
  689. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  690. }
  691. mips64->num_data_bpoints_avail--;
  692. return mips_mips64_set_watchpoint(target, watchpoint);
  693. }
  694. static int mips_mips64_remove_watchpoint(struct target *target,
  695. struct watchpoint *watchpoint)
  696. {
  697. /* get pointers to arch-specific information */
  698. struct mips64_common *mips64 = target->arch_info;
  699. int retval = ERROR_OK;
  700. if (target->state != TARGET_HALTED) {
  701. LOG_WARNING("target not halted");
  702. return ERROR_TARGET_NOT_HALTED;
  703. }
  704. if (watchpoint->set)
  705. retval = mips_mips64_unset_watchpoint(target, watchpoint);
  706. mips64->num_data_bpoints_avail++;
  707. return retval;
  708. }
  709. static int mips_mips64_read_memory(struct target *target, uint64_t address,
  710. uint32_t size, uint32_t count, uint8_t *buffer)
  711. {
  712. struct mips64_common *mips64 = target->arch_info;
  713. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  714. int retval;
  715. void *t;
  716. if (target->state != TARGET_HALTED) {
  717. LOG_WARNING("target not halted %d", target->state);
  718. return ERROR_TARGET_NOT_HALTED;
  719. }
  720. if (mips64->mips64mode32)
  721. address = mips64_extend_sign(address);
  722. /* sanitize arguments */
  723. if (((size != 8) && (size != 4) && (size != 2) && (size != 1))
  724. || !count || !buffer)
  725. return ERROR_COMMAND_ARGUMENT_INVALID;
  726. if (((size == 8) && (address & 0x7)) || ((size == 4) && (address & 0x3))
  727. || ((size == 2) && (address & 0x1)))
  728. return ERROR_TARGET_UNALIGNED_ACCESS;
  729. if (size > 1) {
  730. t = calloc(count, size);
  731. if (!t) {
  732. LOG_ERROR("Out of memory");
  733. return ERROR_FAIL;
  734. }
  735. } else
  736. t = buffer;
  737. LOG_DEBUG("address: 0x%16.16" PRIx64 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
  738. address, size, count);
  739. retval = mips64_pracc_read_mem(ejtag_info, address, size, count,
  740. (void *)t);
  741. if (retval != ERROR_OK) {
  742. LOG_ERROR("mips64_pracc_read_mem filed");
  743. goto read_done;
  744. }
  745. switch (size) {
  746. case 8:
  747. target_buffer_set_u64_array(target, buffer, count, t);
  748. break;
  749. case 4:
  750. target_buffer_set_u32_array(target, buffer, count, t);
  751. break;
  752. case 2:
  753. target_buffer_set_u16_array(target, buffer, count, t);
  754. break;
  755. }
  756. read_done:
  757. if (size > 1)
  758. free(t);
  759. return retval;
  760. }
  761. static int mips_mips64_bulk_write_memory(struct target *target,
  762. target_addr_t address, uint32_t count,
  763. const uint8_t *buffer)
  764. {
  765. struct mips64_common *mips64 = target->arch_info;
  766. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  767. struct working_area *fast_data_area;
  768. int retval;
  769. LOG_DEBUG("address: " TARGET_ADDR_FMT ", count: 0x%8.8" PRIx32 "",
  770. address, count);
  771. if (address & 0x7)
  772. return ERROR_TARGET_UNALIGNED_ACCESS;
  773. if (!mips64->fast_data_area) {
  774. /* Get memory for block write handler
  775. * we preserve this area between calls and gain a speed increase
  776. * of about 3kb/sec when writing flash
  777. * this will be released/nulled by the system when the target is resumed or reset */
  778. retval = target_alloc_working_area(target,
  779. MIPS64_FASTDATA_HANDLER_SIZE,
  780. &mips64->fast_data_area);
  781. if (retval != ERROR_OK) {
  782. LOG_ERROR("No working area available");
  783. return retval;
  784. }
  785. /* reset fastadata state so the algo get reloaded */
  786. ejtag_info->fast_access_save = -1;
  787. }
  788. fast_data_area = mips64->fast_data_area;
  789. if (address <= fast_data_area->address + fast_data_area->size &&
  790. fast_data_area->address <= address + count) {
  791. LOG_ERROR("fast_data (" TARGET_ADDR_FMT ") is within write area "
  792. "(" TARGET_ADDR_FMT "-" TARGET_ADDR_FMT ").",
  793. fast_data_area->address, address, address + count);
  794. LOG_ERROR("Change work-area-phys or load_image address!");
  795. return ERROR_FAIL;
  796. }
  797. /* mips32_pracc_fastdata_xfer requires uint32_t in host endianness, */
  798. /* but byte array represents target endianness */
  799. uint64_t *t;
  800. t = calloc(count, sizeof(uint64_t));
  801. if (!t) {
  802. LOG_ERROR("Out of memory");
  803. return ERROR_FAIL;
  804. }
  805. target_buffer_get_u64_array(target, buffer, count, t);
  806. retval = mips64_pracc_fastdata_xfer(ejtag_info, mips64->fast_data_area,
  807. true, address, count, t);
  808. if (retval != ERROR_OK)
  809. LOG_ERROR("Fastdata access Failed");
  810. free(t);
  811. return retval;
  812. }
  813. static int mips_mips64_write_memory(struct target *target, uint64_t address,
  814. uint32_t size, uint32_t count, const uint8_t *buffer)
  815. {
  816. struct mips64_common *mips64 = target->arch_info;
  817. struct mips_ejtag *ejtag_info = &mips64->ejtag_info;
  818. int retval;
  819. if (target->state != TARGET_HALTED) {
  820. LOG_WARNING("target not halted");
  821. return ERROR_TARGET_NOT_HALTED;
  822. }
  823. if (mips64->mips64mode32)
  824. address = mips64_extend_sign(address);
  825. /* sanitize arguments */
  826. if (((size != 8) && (size != 4) && (size != 2) && (size != 1))
  827. || !count || !buffer)
  828. return ERROR_COMMAND_ARGUMENT_INVALID;
  829. if (((size == 8) && (address & 0x7)) || ((size == 4) && (address & 0x3))
  830. || ((size == 2) && (address & 0x1)))
  831. return ERROR_TARGET_UNALIGNED_ACCESS;
  832. if (size == 8 && count > 8) {
  833. retval = mips_mips64_bulk_write_memory(target, address, count,
  834. buffer);
  835. if (retval == ERROR_OK)
  836. return ERROR_OK;
  837. LOG_WARNING("Falling back to non-bulk write");
  838. }
  839. void *t = NULL;
  840. if (size > 1) {
  841. t = calloc(count, size);
  842. if (!t) {
  843. LOG_ERROR("unable to allocate t for write buffer");
  844. return ERROR_FAIL;
  845. }
  846. switch (size) {
  847. case 8:
  848. target_buffer_get_u64_array(target, buffer, count,
  849. (uint64_t *)t);
  850. break;
  851. case 4:
  852. target_buffer_get_u32_array(target, buffer, count,
  853. (uint32_t *)t);
  854. break;
  855. case 2:
  856. target_buffer_get_u16_array(target, buffer, count,
  857. (uint16_t *)t);
  858. break;
  859. }
  860. buffer = t;
  861. }
  862. LOG_DEBUG("address: 0x%16.16" PRIx64 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
  863. address, size, count);
  864. retval = mips64_pracc_write_mem(ejtag_info, address, size, count,
  865. (void *)buffer);
  866. free(t);
  867. return retval;
  868. }
  869. static int mips_mips64_init_target(struct command_context *cmd_ctx,
  870. struct target *target)
  871. {
  872. return mips64_build_reg_cache(target);
  873. }
  874. static int mips_mips64_target_create(struct target *target, Jim_Interp *interp)
  875. {
  876. struct mips_mips64_common *mips_mips64;
  877. struct mips64_common *mips64;
  878. mips_mips64 = calloc(1, sizeof(*mips_mips64));
  879. if (!mips_mips64) {
  880. LOG_ERROR("unable to allocate mips_mips64");
  881. return ERROR_FAIL;
  882. }
  883. mips_mips64->common_magic = MIPS64_COMMON_MAGIC;
  884. mips64 = &mips_mips64->mips64_common;
  885. mips64->arch_info = mips_mips64;
  886. target->arch_info = mips64;
  887. return mips64_init_arch_info(target, mips64, target->tap);
  888. }
  889. static int mips_mips64_examine(struct target *target)
  890. {
  891. int retval;
  892. struct mips64_common *mips64 = target->arch_info;
  893. retval = mips_ejtag_init(&mips64->ejtag_info);
  894. if (retval != ERROR_OK)
  895. return retval;
  896. return mips64_examine(target);
  897. }
  898. static int mips_mips64_checksum_memory(struct target *target, uint64_t address,
  899. uint32_t size, uint32_t *checksum)
  900. {
  901. return ERROR_FAIL; /* use bulk read method */
  902. }
  903. COMMAND_HANDLER(handle_mips64mode32)
  904. {
  905. struct target *target = get_current_target(CMD_CTX);
  906. struct mips64_common *mips64 = target->arch_info;
  907. if (CMD_ARGC > 0)
  908. COMMAND_PARSE_BOOL(CMD_ARGV[0], mips64->mips64mode32, "on", "off");
  909. if (mips64->mips64mode32)
  910. command_print(CMD, "enabled");
  911. else
  912. command_print(CMD, "disabled");
  913. return ERROR_OK;
  914. }
  915. static const struct command_registration mips64_commands_handlers[] = {
  916. {
  917. .name = "mips64mode32",
  918. .mode = COMMAND_EXEC,
  919. .help = "Enable/disable 32 bit mode",
  920. .usage = "[1|0]",
  921. .handler = handle_mips64mode32
  922. },
  923. COMMAND_REGISTRATION_DONE
  924. };
  925. struct target_type mips_mips64_target = {
  926. .name = "mips_mips64",
  927. .poll = mips_mips64_poll,
  928. .arch_state = mips64_arch_state,
  929. .target_request_data = NULL,
  930. .halt = mips_mips64_halt,
  931. .resume = mips_mips64_resume,
  932. .step = mips_mips64_step,
  933. .assert_reset = mips_mips64_assert_reset,
  934. .deassert_reset = mips_mips64_deassert_reset,
  935. .soft_reset_halt = mips_mips64_soft_reset_halt,
  936. .get_gdb_reg_list = mips64_get_gdb_reg_list,
  937. .read_memory = mips_mips64_read_memory,
  938. .write_memory = mips_mips64_write_memory,
  939. .checksum_memory = mips_mips64_checksum_memory,
  940. .blank_check_memory = NULL,
  941. .run_algorithm = mips64_run_algorithm,
  942. .add_breakpoint = mips_mips64_add_breakpoint,
  943. .remove_breakpoint = mips_mips64_remove_breakpoint,
  944. .add_watchpoint = mips_mips64_add_watchpoint,
  945. .remove_watchpoint = mips_mips64_remove_watchpoint,
  946. .target_create = mips_mips64_target_create,
  947. .init_target = mips_mips64_init_target,
  948. .examine = mips_mips64_examine,
  949. .commands = mips64_commands_handlers,
  950. };