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.
 
 
 
 
 
 

1352 lines
36 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2008 digenius technology GmbH. *
  3. * Michael Bruck *
  4. * *
  5. * Copyright (C) 2008,2009 Oyvind Harboe oyvind.harboe@zylin.com *
  6. * *
  7. * Copyright (C) 2008 Georg Acher <acher@in.tum.de> *
  8. * *
  9. * Copyright (C) 2009 David Brownell *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "etm.h"
  30. #include "breakpoints.h"
  31. #include "arm11_dbgtap.h"
  32. #include "arm_simulator.h"
  33. #include <helper/time_support.h>
  34. #include "target_type.h"
  35. #include "algorithm.h"
  36. #include "register.h"
  37. #include "arm_opcodes.h"
  38. #if 0
  39. #define _DEBUG_INSTRUCTION_EXECUTION_
  40. #endif
  41. static int arm11_step(struct target *target, int current,
  42. uint32_t address, int handle_breakpoints);
  43. /** Check and if necessary take control of the system
  44. *
  45. * \param arm11 Target state variable.
  46. */
  47. static int arm11_check_init(struct arm11_common *arm11)
  48. {
  49. CHECK_RETVAL(arm11_read_DSCR(arm11));
  50. if (!(arm11->dscr & DSCR_HALT_DBG_MODE)) {
  51. LOG_DEBUG("DSCR %08x", (unsigned) arm11->dscr);
  52. LOG_DEBUG("Bringing target into debug mode");
  53. arm11->dscr |= DSCR_HALT_DBG_MODE;
  54. CHECK_RETVAL(arm11_write_DSCR(arm11, arm11->dscr));
  55. /* add further reset initialization here */
  56. arm11->simulate_reset_on_next_halt = true;
  57. if (arm11->dscr & DSCR_CORE_HALTED) {
  58. /** \todo TODO: this needs further scrutiny because
  59. * arm11_debug_entry() never gets called. (WHY NOT?)
  60. * As a result we don't read the actual register states from
  61. * the target.
  62. */
  63. arm11->arm.target->state = TARGET_HALTED;
  64. arm_dpm_report_dscr(arm11->arm.dpm, arm11->dscr);
  65. } else {
  66. arm11->arm.target->state = TARGET_RUNNING;
  67. arm11->arm.target->debug_reason = DBG_REASON_NOTHALTED;
  68. }
  69. CHECK_RETVAL(arm11_sc7_clear_vbw(arm11));
  70. }
  71. return ERROR_OK;
  72. }
  73. /**
  74. * Save processor state. This is called after a HALT instruction
  75. * succeeds, and on other occasions the processor enters debug mode
  76. * (breakpoint, watchpoint, etc). Caller has updated arm11->dscr.
  77. */
  78. static int arm11_debug_entry(struct arm11_common *arm11)
  79. {
  80. int retval;
  81. arm11->arm.target->state = TARGET_HALTED;
  82. arm_dpm_report_dscr(arm11->arm.dpm, arm11->dscr);
  83. /* REVISIT entire cache should already be invalid !!! */
  84. register_cache_invalidate(arm11->arm.core_cache);
  85. /* See e.g. ARM1136 TRM, "14.8.4 Entering Debug state" */
  86. /* maybe save wDTR (pending DCC write to debug SW, e.g. libdcc) */
  87. arm11->is_wdtr_saved = !!(arm11->dscr & DSCR_DTR_TX_FULL);
  88. if (arm11->is_wdtr_saved) {
  89. arm11_add_debug_SCAN_N(arm11, 0x05, ARM11_TAP_DEFAULT);
  90. arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
  91. struct scan_field chain5_fields[3];
  92. arm11_setup_field(arm11, 32, NULL,
  93. &arm11->saved_wdtr, chain5_fields + 0);
  94. arm11_setup_field(arm11, 1, NULL, NULL, chain5_fields + 1);
  95. arm11_setup_field(arm11, 1, NULL, NULL, chain5_fields + 2);
  96. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(
  97. chain5_fields), chain5_fields, TAP_DRPAUSE);
  98. }
  99. /* DSCR: set the Execute ARM instruction enable bit.
  100. *
  101. * ARM1176 spec says this is needed only for wDTR/rDTR's "ITR mode",
  102. * but not to issue ITRs(?). The ARMv7 arch spec says it's required
  103. * for executing instructions via ITR.
  104. */
  105. CHECK_RETVAL(arm11_write_DSCR(arm11, DSCR_ITR_EN | arm11->dscr));
  106. /* From the spec:
  107. Before executing any instruction in debug state you have to drain the write buffer.
  108. This ensures that no imprecise Data Aborts can return at a later point:*/
  109. /** \todo TODO: Test drain write buffer. */
  110. #if 0
  111. while (1) {
  112. /* MRC p14,0,R0,c5,c10,0 */
  113. /* arm11_run_instr_no_data1(arm11, / *0xee150e1a* /0xe320f000); */
  114. /* mcr 15, 0, r0, cr7, cr10, {4} */
  115. arm11_run_instr_no_data1(arm11, 0xee070f9a);
  116. uint32_t dscr = arm11_read_DSCR(arm11);
  117. LOG_DEBUG("DRAIN, DSCR %08x", dscr);
  118. if (dscr & ARM11_DSCR_STICKY_IMPRECISE_DATA_ABORT) {
  119. arm11_run_instr_no_data1(arm11, 0xe320f000);
  120. dscr = arm11_read_DSCR(arm11);
  121. LOG_DEBUG("DRAIN, DSCR %08x (DONE)", dscr);
  122. break;
  123. }
  124. }
  125. #endif
  126. /* Save registers.
  127. *
  128. * NOTE: ARM1136 TRM suggests saving just R0 here now, then
  129. * CPSR and PC after the rDTR stuff. We do it all at once.
  130. */
  131. retval = arm_dpm_read_current_registers(&arm11->dpm);
  132. if (retval != ERROR_OK)
  133. LOG_ERROR("DPM REG READ -- fail");
  134. retval = arm11_run_instr_data_prepare(arm11);
  135. if (retval != ERROR_OK)
  136. return retval;
  137. /* maybe save rDTR (pending DCC read from debug SW, e.g. libdcc) */
  138. arm11->is_rdtr_saved = !!(arm11->dscr & DSCR_DTR_RX_FULL);
  139. if (arm11->is_rdtr_saved) {
  140. /* MRC p14,0,R0,c0,c5,0 (move rDTR -> r0 (-> wDTR -> local var)) */
  141. retval = arm11_run_instr_data_from_core_via_r0(arm11,
  142. 0xEE100E15, &arm11->saved_rdtr);
  143. if (retval != ERROR_OK)
  144. return retval;
  145. }
  146. /* REVISIT Now that we've saved core state, there's may also
  147. * be MMU and cache state to care about ...
  148. */
  149. if (arm11->simulate_reset_on_next_halt) {
  150. arm11->simulate_reset_on_next_halt = false;
  151. LOG_DEBUG("Reset c1 Control Register");
  152. /* Write 0 (reset value) to Control register 0 to disable MMU/Cache etc. */
  153. /* MCR p15,0,R0,c1,c0,0 */
  154. retval = arm11_run_instr_data_to_core_via_r0(arm11, 0xee010f10, 0);
  155. if (retval != ERROR_OK)
  156. return retval;
  157. }
  158. if (arm11->arm.target->debug_reason == DBG_REASON_WATCHPOINT) {
  159. uint32_t wfar;
  160. /* MRC p15, 0, <Rd>, c6, c0, 1 ; Read WFAR */
  161. retval = arm11_run_instr_data_from_core_via_r0(arm11,
  162. ARMV4_5_MRC(15, 0, 0, 6, 0, 1),
  163. &wfar);
  164. if (retval != ERROR_OK)
  165. return retval;
  166. arm_dpm_report_wfar(arm11->arm.dpm, wfar);
  167. }
  168. retval = arm11_run_instr_data_finish(arm11);
  169. if (retval != ERROR_OK)
  170. return retval;
  171. return ERROR_OK;
  172. }
  173. /**
  174. * Restore processor state. This is called in preparation for
  175. * the RESTART function.
  176. */
  177. static int arm11_leave_debug_state(struct arm11_common *arm11, bool bpwp)
  178. {
  179. int retval;
  180. /* See e.g. ARM1136 TRM, "14.8.5 Leaving Debug state" */
  181. /* NOTE: the ARM1136 TRM suggests restoring all registers
  182. * except R0/PC/CPSR right now. Instead, we do them all
  183. * at once, just a bit later on.
  184. */
  185. /* REVISIT once we start caring about MMU and cache state,
  186. * address it here ...
  187. */
  188. /* spec says clear wDTR and rDTR; we assume they are clear as
  189. otherwise our programming would be sloppy */
  190. {
  191. CHECK_RETVAL(arm11_read_DSCR(arm11));
  192. if (arm11->dscr & (DSCR_DTR_RX_FULL | DSCR_DTR_TX_FULL)) {
  193. /*
  194. The wDTR/rDTR two registers that are used to send/receive data to/from
  195. the core in tandem with corresponding instruction codes that are
  196. written into the core. The RDTR FULL/WDTR FULL flag indicates that the
  197. registers hold data that was written by one side (CPU or JTAG) and not
  198. read out by the other side.
  199. */
  200. LOG_ERROR("wDTR/rDTR inconsistent (DSCR %08x)",
  201. (unsigned) arm11->dscr);
  202. return ERROR_FAIL;
  203. }
  204. }
  205. /* maybe restore original wDTR */
  206. if (arm11->is_wdtr_saved) {
  207. retval = arm11_run_instr_data_prepare(arm11);
  208. if (retval != ERROR_OK)
  209. return retval;
  210. /* MCR p14,0,R0,c0,c5,0 */
  211. retval = arm11_run_instr_data_to_core_via_r0(arm11,
  212. 0xee000e15, arm11->saved_wdtr);
  213. if (retval != ERROR_OK)
  214. return retval;
  215. retval = arm11_run_instr_data_finish(arm11);
  216. if (retval != ERROR_OK)
  217. return retval;
  218. }
  219. /* restore CPSR, PC, and R0 ... after flushing any modified
  220. * registers.
  221. */
  222. CHECK_RETVAL(arm_dpm_write_dirty_registers(&arm11->dpm, bpwp));
  223. CHECK_RETVAL(arm11_bpwp_flush(arm11));
  224. register_cache_invalidate(arm11->arm.core_cache);
  225. /* restore DSCR */
  226. CHECK_RETVAL(arm11_write_DSCR(arm11, arm11->dscr));
  227. /* maybe restore rDTR */
  228. if (arm11->is_rdtr_saved) {
  229. arm11_add_debug_SCAN_N(arm11, 0x05, ARM11_TAP_DEFAULT);
  230. arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
  231. struct scan_field chain5_fields[3];
  232. uint8_t Ready = 0; /* ignored */
  233. uint8_t Valid = 0; /* ignored */
  234. arm11_setup_field(arm11, 32, &arm11->saved_rdtr,
  235. NULL, chain5_fields + 0);
  236. arm11_setup_field(arm11, 1, &Ready, NULL, chain5_fields + 1);
  237. arm11_setup_field(arm11, 1, &Valid, NULL, chain5_fields + 2);
  238. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(
  239. chain5_fields), chain5_fields, TAP_DRPAUSE);
  240. }
  241. /* now processor is ready to RESTART */
  242. return ERROR_OK;
  243. }
  244. /* poll current target status */
  245. static int arm11_poll(struct target *target)
  246. {
  247. int retval;
  248. struct arm11_common *arm11 = target_to_arm11(target);
  249. CHECK_RETVAL(arm11_check_init(arm11));
  250. if (arm11->dscr & DSCR_CORE_HALTED) {
  251. if (target->state != TARGET_HALTED) {
  252. enum target_state old_state = target->state;
  253. LOG_DEBUG("enter TARGET_HALTED");
  254. retval = arm11_debug_entry(arm11);
  255. if (retval != ERROR_OK)
  256. return retval;
  257. target_call_event_callbacks(target,
  258. (old_state == TARGET_DEBUG_RUNNING)
  259. ? TARGET_EVENT_DEBUG_HALTED
  260. : TARGET_EVENT_HALTED);
  261. }
  262. } else {
  263. if (target->state != TARGET_RUNNING && target->state != TARGET_DEBUG_RUNNING) {
  264. LOG_DEBUG("enter TARGET_RUNNING");
  265. target->state = TARGET_RUNNING;
  266. target->debug_reason = DBG_REASON_NOTHALTED;
  267. }
  268. }
  269. return ERROR_OK;
  270. }
  271. /* architecture specific status reply */
  272. static int arm11_arch_state(struct target *target)
  273. {
  274. struct arm11_common *arm11 = target_to_arm11(target);
  275. int retval;
  276. retval = arm_arch_state(target);
  277. /* REVISIT also display ARM11-specific MMU and cache status ... */
  278. if (target->debug_reason == DBG_REASON_WATCHPOINT)
  279. LOG_USER("Watchpoint triggered at PC %#08x",
  280. (unsigned) arm11->dpm.wp_pc);
  281. return retval;
  282. }
  283. /* target execution control */
  284. static int arm11_halt(struct target *target)
  285. {
  286. struct arm11_common *arm11 = target_to_arm11(target);
  287. LOG_DEBUG("target->state: %s",
  288. target_state_name(target));
  289. if (target->state == TARGET_UNKNOWN)
  290. arm11->simulate_reset_on_next_halt = true;
  291. if (target->state == TARGET_HALTED) {
  292. LOG_DEBUG("target was already halted");
  293. return ERROR_OK;
  294. }
  295. arm11_add_IR(arm11, ARM11_HALT, TAP_IDLE);
  296. CHECK_RETVAL(jtag_execute_queue());
  297. int i = 0;
  298. while (1) {
  299. CHECK_RETVAL(arm11_read_DSCR(arm11));
  300. if (arm11->dscr & DSCR_CORE_HALTED)
  301. break;
  302. long long then = 0;
  303. if (i == 1000)
  304. then = timeval_ms();
  305. if (i >= 1000) {
  306. if ((timeval_ms()-then) > 1000) {
  307. LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
  308. return ERROR_FAIL;
  309. }
  310. }
  311. i++;
  312. }
  313. enum target_state old_state = target->state;
  314. CHECK_RETVAL(arm11_debug_entry(arm11));
  315. CHECK_RETVAL(
  316. target_call_event_callbacks(target,
  317. old_state ==
  318. TARGET_DEBUG_RUNNING ? TARGET_EVENT_DEBUG_HALTED : TARGET_EVENT_HALTED));
  319. return ERROR_OK;
  320. }
  321. static uint32_t arm11_nextpc(struct arm11_common *arm11, int current, uint32_t address)
  322. {
  323. void *value = arm11->arm.pc->value;
  324. if (!current)
  325. buf_set_u32(value, 0, 32, address);
  326. else
  327. address = buf_get_u32(value, 0, 32);
  328. return address;
  329. }
  330. static int arm11_resume(struct target *target, int current,
  331. uint32_t address, int handle_breakpoints, int debug_execution)
  332. {
  333. /* LOG_DEBUG("current %d address %08x handle_breakpoints %d debug_execution %d", */
  334. /* current, address, handle_breakpoints, debug_execution); */
  335. struct arm11_common *arm11 = target_to_arm11(target);
  336. LOG_DEBUG("target->state: %s",
  337. target_state_name(target));
  338. if (target->state != TARGET_HALTED) {
  339. LOG_ERROR("Target not halted");
  340. return ERROR_TARGET_NOT_HALTED;
  341. }
  342. address = arm11_nextpc(arm11, current, address);
  343. LOG_DEBUG("RESUME PC %08" PRIx32 "%s", address, !current ? "!" : "");
  344. /* clear breakpoints/watchpoints and VCR*/
  345. CHECK_RETVAL(arm11_sc7_clear_vbw(arm11));
  346. if (!debug_execution)
  347. target_free_all_working_areas(target);
  348. /* Should we skip over breakpoints matching the PC? */
  349. if (handle_breakpoints) {
  350. struct breakpoint *bp;
  351. for (bp = target->breakpoints; bp; bp = bp->next) {
  352. if (bp->address == address) {
  353. LOG_DEBUG("must step over %08" PRIx32 "", bp->address);
  354. arm11_step(target, 1, 0, 0);
  355. break;
  356. }
  357. }
  358. }
  359. /* activate all breakpoints */
  360. if (true) {
  361. struct breakpoint *bp;
  362. unsigned brp_num = 0;
  363. for (bp = target->breakpoints; bp; bp = bp->next) {
  364. struct arm11_sc7_action brp[2];
  365. brp[0].write = 1;
  366. brp[0].address = ARM11_SC7_BVR0 + brp_num;
  367. brp[0].value = bp->address;
  368. brp[1].write = 1;
  369. brp[1].address = ARM11_SC7_BCR0 + brp_num;
  370. brp[1].value = 0x1 |
  371. (3 <<
  372. 1) | (0x0F << 5) | (0 << 14) | (0 << 16) | (0 << 20) | (0 << 21);
  373. CHECK_RETVAL(arm11_sc7_run(arm11, brp, ARRAY_SIZE(brp)));
  374. LOG_DEBUG("Add BP %d at %08" PRIx32, brp_num,
  375. bp->address);
  376. brp_num++;
  377. }
  378. if (arm11->vcr)
  379. CHECK_RETVAL(arm11_sc7_set_vcr(arm11, arm11->vcr));
  380. }
  381. /* activate all watchpoints and breakpoints */
  382. CHECK_RETVAL(arm11_leave_debug_state(arm11, true));
  383. arm11_add_IR(arm11, ARM11_RESTART, TAP_IDLE);
  384. CHECK_RETVAL(jtag_execute_queue());
  385. int i = 0;
  386. while (1) {
  387. CHECK_RETVAL(arm11_read_DSCR(arm11));
  388. LOG_DEBUG("DSCR %08x", (unsigned) arm11->dscr);
  389. if (arm11->dscr & DSCR_CORE_RESTARTED)
  390. break;
  391. long long then = 0;
  392. if (i == 1000)
  393. then = timeval_ms();
  394. if (i >= 1000) {
  395. if ((timeval_ms()-then) > 1000) {
  396. LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
  397. return ERROR_FAIL;
  398. }
  399. }
  400. i++;
  401. }
  402. target->debug_reason = DBG_REASON_NOTHALTED;
  403. if (!debug_execution)
  404. target->state = TARGET_RUNNING;
  405. else
  406. target->state = TARGET_DEBUG_RUNNING;
  407. CHECK_RETVAL(target_call_event_callbacks(target, TARGET_EVENT_RESUMED));
  408. return ERROR_OK;
  409. }
  410. static int arm11_step(struct target *target, int current,
  411. uint32_t address, int handle_breakpoints)
  412. {
  413. LOG_DEBUG("target->state: %s",
  414. target_state_name(target));
  415. if (target->state != TARGET_HALTED) {
  416. LOG_WARNING("target was not halted");
  417. return ERROR_TARGET_NOT_HALTED;
  418. }
  419. struct arm11_common *arm11 = target_to_arm11(target);
  420. address = arm11_nextpc(arm11, current, address);
  421. LOG_DEBUG("STEP PC %08" PRIx32 "%s", address, !current ? "!" : "");
  422. /** \todo TODO: Thumb not supported here */
  423. uint32_t next_instruction;
  424. CHECK_RETVAL(arm11_read_memory_word(arm11, address, &next_instruction));
  425. /* skip over BKPT */
  426. if ((next_instruction & 0xFFF00070) == 0xe1200070) {
  427. address = arm11_nextpc(arm11, 0, address + 4);
  428. LOG_DEBUG("Skipping BKPT %08" PRIx32, address);
  429. }
  430. /* skip over Wait for interrupt / Standby
  431. * mcr 15, 0, r?, cr7, cr0, {4} */
  432. else if ((next_instruction & 0xFFFF0FFF) == 0xee070f90) {
  433. address = arm11_nextpc(arm11, 0, address + 4);
  434. LOG_DEBUG("Skipping WFI %08" PRIx32, address);
  435. }
  436. /* ignore B to self */
  437. else if ((next_instruction & 0xFEFFFFFF) == 0xeafffffe)
  438. LOG_DEBUG("Not stepping jump to self");
  439. else {
  440. /** \todo TODO: check if break-/watchpoints make any sense at all in combination
  441. * with this. */
  442. /** \todo TODO: check if disabling IRQs might be a good idea here. Alternatively
  443. * the VCR might be something worth looking into. */
  444. /* Set up breakpoint for stepping */
  445. struct arm11_sc7_action brp[2];
  446. brp[0].write = 1;
  447. brp[0].address = ARM11_SC7_BVR0;
  448. brp[1].write = 1;
  449. brp[1].address = ARM11_SC7_BCR0;
  450. if (arm11->hardware_step) {
  451. /* Hardware single stepping ("instruction address
  452. * mismatch") is used if enabled. It's not quite
  453. * exactly "run one instruction"; "branch to here"
  454. * loops won't break, neither will some other cases,
  455. * but it's probably the best default.
  456. *
  457. * Hardware single stepping isn't supported on v6
  458. * debug modules. ARM1176 and v7 can support it...
  459. *
  460. * FIXME Thumb stepping likely needs to use 0x03
  461. * or 0xc0 byte masks, not 0x0f.
  462. */
  463. brp[0].value = address;
  464. brp[1].value = 0x1 | (3 << 1) | (0x0F << 5)
  465. | (0 << 14) | (0 << 16) | (0 << 20)
  466. | (2 << 21);
  467. } else {
  468. /* Sets a breakpoint on the next PC, as calculated
  469. * by instruction set simulation.
  470. *
  471. * REVISIT stepping Thumb on ARM1156 requires Thumb2
  472. * support from the simulator.
  473. */
  474. uint32_t next_pc;
  475. int retval;
  476. retval = arm_simulate_step(target, &next_pc);
  477. if (retval != ERROR_OK)
  478. return retval;
  479. brp[0].value = next_pc;
  480. brp[1].value = 0x1 | (3 << 1) | (0x0F << 5)
  481. | (0 << 14) | (0 << 16) | (0 << 20)
  482. | (0 << 21);
  483. }
  484. CHECK_RETVAL(arm11_sc7_run(arm11, brp, ARRAY_SIZE(brp)));
  485. /* resume */
  486. if (arm11->step_irq_enable)
  487. /* this disable should be redundant ... */
  488. arm11->dscr &= ~DSCR_INT_DIS;
  489. else
  490. arm11->dscr |= DSCR_INT_DIS;
  491. CHECK_RETVAL(arm11_leave_debug_state(arm11, handle_breakpoints));
  492. arm11_add_IR(arm11, ARM11_RESTART, TAP_IDLE);
  493. CHECK_RETVAL(jtag_execute_queue());
  494. /* wait for halt */
  495. int i = 0;
  496. while (1) {
  497. const uint32_t mask = DSCR_CORE_RESTARTED
  498. | DSCR_CORE_HALTED;
  499. CHECK_RETVAL(arm11_read_DSCR(arm11));
  500. LOG_DEBUG("DSCR %08x e", (unsigned) arm11->dscr);
  501. if ((arm11->dscr & mask) == mask)
  502. break;
  503. long long then = 0;
  504. if (i == 1000)
  505. then = timeval_ms();
  506. if (i >= 1000) {
  507. if ((timeval_ms()-then) > 1000) {
  508. LOG_WARNING(
  509. "Timeout (1000ms) waiting for instructions to complete");
  510. return ERROR_FAIL;
  511. }
  512. }
  513. i++;
  514. }
  515. /* clear breakpoint */
  516. CHECK_RETVAL(arm11_sc7_clear_vbw(arm11));
  517. /* save state */
  518. CHECK_RETVAL(arm11_debug_entry(arm11));
  519. /* restore default state */
  520. arm11->dscr &= ~DSCR_INT_DIS;
  521. }
  522. target->debug_reason = DBG_REASON_SINGLESTEP;
  523. CHECK_RETVAL(target_call_event_callbacks(target, TARGET_EVENT_HALTED));
  524. return ERROR_OK;
  525. }
  526. static int arm11_assert_reset(struct target *target)
  527. {
  528. struct arm11_common *arm11 = target_to_arm11(target);
  529. /* optionally catch reset vector */
  530. if (target->reset_halt && !(arm11->vcr & 1))
  531. CHECK_RETVAL(arm11_sc7_set_vcr(arm11, arm11->vcr | 1));
  532. /* Issue some kind of warm reset. */
  533. if (target_has_event_action(target, TARGET_EVENT_RESET_ASSERT))
  534. target_handle_event(target, TARGET_EVENT_RESET_ASSERT);
  535. else if (jtag_get_reset_config() & RESET_HAS_SRST) {
  536. /* REVISIT handle "pulls" cases, if there's
  537. * hardware that needs them to work.
  538. */
  539. jtag_add_reset(0, 1);
  540. } else {
  541. LOG_ERROR("%s: how to reset?", target_name(target));
  542. return ERROR_FAIL;
  543. }
  544. /* registers are now invalid */
  545. register_cache_invalidate(arm11->arm.core_cache);
  546. target->state = TARGET_RESET;
  547. return ERROR_OK;
  548. }
  549. /*
  550. * - There is another bug in the arm11 core. (iMX31 specific again?)
  551. * When you generate an access to external logic (for example DDR
  552. * controller via AHB bus) and that block is not configured (perhaps
  553. * it is still held in reset), that transaction will never complete.
  554. * This will hang arm11 core but it will also hang JTAG controller.
  555. * Nothing short of srst assertion will bring it out of this.
  556. */
  557. static int arm11_deassert_reset(struct target *target)
  558. {
  559. struct arm11_common *arm11 = target_to_arm11(target);
  560. int retval;
  561. /* be certain SRST is off */
  562. jtag_add_reset(0, 0);
  563. /* WORKAROUND i.MX31 problems: SRST goofs the TAP, and resets
  564. * at least DSCR. OMAP24xx doesn't show that problem, though
  565. * SRST-only reset seems to be problematic for other reasons.
  566. * (Secure boot sequences being one likelihood!)
  567. */
  568. jtag_add_tlr();
  569. CHECK_RETVAL(arm11_poll(target));
  570. if (target->reset_halt) {
  571. if (target->state != TARGET_HALTED) {
  572. LOG_WARNING("%s: ran after reset and before halt ...",
  573. target_name(target));
  574. retval = target_halt(target);
  575. if (retval != ERROR_OK)
  576. return retval;
  577. }
  578. }
  579. /* maybe restore vector catch config */
  580. if (target->reset_halt && !(arm11->vcr & 1))
  581. CHECK_RETVAL(arm11_sc7_set_vcr(arm11, arm11->vcr));
  582. return ERROR_OK;
  583. }
  584. /* target memory access
  585. * size: 1 = byte (8bit), 2 = half-word (16bit), 4 = word (32bit)
  586. * count: number of items of <size>
  587. *
  588. * arm11_config_memrw_no_increment - in the future we may want to be able
  589. * to read/write a range of data to a "port". a "port" is an action on
  590. * read memory address for some peripheral.
  591. */
  592. static int arm11_read_memory_inner(struct target *target,
  593. uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer,
  594. bool arm11_config_memrw_no_increment)
  595. {
  596. /** \todo TODO: check if buffer cast to uint32_t* and uint16_t* might cause alignment
  597. *problems */
  598. int retval;
  599. if (target->state != TARGET_HALTED) {
  600. LOG_WARNING("target was not halted");
  601. return ERROR_TARGET_NOT_HALTED;
  602. }
  603. LOG_DEBUG("ADDR %08" PRIx32 " SIZE %08" PRIx32 " COUNT %08" PRIx32 "",
  604. address,
  605. size,
  606. count);
  607. struct arm11_common *arm11 = target_to_arm11(target);
  608. retval = arm11_run_instr_data_prepare(arm11);
  609. if (retval != ERROR_OK)
  610. return retval;
  611. /* MRC p14,0,r0,c0,c5,0 */
  612. retval = arm11_run_instr_data_to_core1(arm11, 0xee100e15, address);
  613. if (retval != ERROR_OK)
  614. return retval;
  615. switch (size) {
  616. case 1:
  617. arm11->arm.core_cache->reg_list[1].dirty = true;
  618. for (size_t i = 0; i < count; i++) {
  619. /* ldrb r1, [r0], #1 */
  620. /* ldrb r1, [r0] */
  621. CHECK_RETVAL(arm11_run_instr_no_data1(arm11,
  622. !arm11_config_memrw_no_increment ? 0xe4d01001 : 0xe5d01000));
  623. uint32_t res;
  624. /* MCR p14,0,R1,c0,c5,0 */
  625. CHECK_RETVAL(arm11_run_instr_data_from_core(arm11, 0xEE001E15, &res, 1));
  626. *buffer++ = res;
  627. }
  628. break;
  629. case 2:
  630. {
  631. arm11->arm.core_cache->reg_list[1].dirty = true;
  632. for (size_t i = 0; i < count; i++) {
  633. /* ldrh r1, [r0], #2 */
  634. CHECK_RETVAL(arm11_run_instr_no_data1(arm11,
  635. !arm11_config_memrw_no_increment ? 0xe0d010b2 : 0xe1d010b0));
  636. uint32_t res;
  637. /* MCR p14,0,R1,c0,c5,0 */
  638. CHECK_RETVAL(arm11_run_instr_data_from_core(arm11, 0xEE001E15, &res, 1));
  639. uint16_t svalue = res;
  640. memcpy(buffer + i * sizeof(uint16_t), &svalue, sizeof(uint16_t));
  641. }
  642. break;
  643. }
  644. case 4:
  645. {
  646. uint32_t instr = !arm11_config_memrw_no_increment ? 0xecb05e01 : 0xed905e00;
  647. /** \todo TODO: buffer cast to uint32_t* causes alignment warnings */
  648. uint32_t *words = (uint32_t *)(void *)buffer;
  649. /* LDC p14,c5,[R0],#4 */
  650. /* LDC p14,c5,[R0] */
  651. CHECK_RETVAL(arm11_run_instr_data_from_core(arm11, instr, words, count));
  652. break;
  653. }
  654. }
  655. return arm11_run_instr_data_finish(arm11);
  656. }
  657. static int arm11_read_memory(struct target *target,
  658. uint32_t address,
  659. uint32_t size,
  660. uint32_t count,
  661. uint8_t *buffer)
  662. {
  663. return arm11_read_memory_inner(target, address, size, count, buffer, false);
  664. }
  665. /*
  666. * no_increment - in the future we may want to be able
  667. * to read/write a range of data to a "port". a "port" is an action on
  668. * read memory address for some peripheral.
  669. */
  670. static int arm11_write_memory_inner(struct target *target,
  671. uint32_t address, uint32_t size,
  672. uint32_t count, const uint8_t *buffer,
  673. bool no_increment)
  674. {
  675. int retval;
  676. if (target->state != TARGET_HALTED) {
  677. LOG_WARNING("target was not halted");
  678. return ERROR_TARGET_NOT_HALTED;
  679. }
  680. LOG_DEBUG("ADDR %08" PRIx32 " SIZE %08" PRIx32 " COUNT %08" PRIx32 "",
  681. address,
  682. size,
  683. count);
  684. struct arm11_common *arm11 = target_to_arm11(target);
  685. retval = arm11_run_instr_data_prepare(arm11);
  686. if (retval != ERROR_OK)
  687. return retval;
  688. /* load r0 with buffer address
  689. * MRC p14,0,r0,c0,c5,0 */
  690. retval = arm11_run_instr_data_to_core1(arm11, 0xee100e15, address);
  691. if (retval != ERROR_OK)
  692. return retval;
  693. /* burst writes are not used for single words as those may well be
  694. * reset init script writes.
  695. *
  696. * The other advantage is that as burst writes are default, we'll
  697. * now exercise both burst and non-burst code paths with the
  698. * default settings, increasing code coverage.
  699. */
  700. bool burst = arm11->memwrite_burst && (count > 1);
  701. switch (size) {
  702. case 1:
  703. {
  704. arm11->arm.core_cache->reg_list[1].dirty = true;
  705. for (size_t i = 0; i < count; i++) {
  706. /* load r1 from DCC with byte data */
  707. /* MRC p14,0,r1,c0,c5,0 */
  708. retval = arm11_run_instr_data_to_core1(arm11, 0xee101e15, *buffer++);
  709. if (retval != ERROR_OK)
  710. return retval;
  711. /* write r1 to memory */
  712. /* strb r1, [r0], #1 */
  713. /* strb r1, [r0] */
  714. retval = arm11_run_instr_no_data1(arm11,
  715. !no_increment ? 0xe4c01001 : 0xe5c01000);
  716. if (retval != ERROR_OK)
  717. return retval;
  718. }
  719. break;
  720. }
  721. case 2:
  722. {
  723. arm11->arm.core_cache->reg_list[1].dirty = true;
  724. for (size_t i = 0; i < count; i++) {
  725. uint16_t value;
  726. memcpy(&value, buffer + i * sizeof(uint16_t), sizeof(uint16_t));
  727. /* load r1 from DCC with halfword data */
  728. /* MRC p14,0,r1,c0,c5,0 */
  729. retval = arm11_run_instr_data_to_core1(arm11, 0xee101e15, value);
  730. if (retval != ERROR_OK)
  731. return retval;
  732. /* write r1 to memory */
  733. /* strh r1, [r0], #2 */
  734. /* strh r1, [r0] */
  735. retval = arm11_run_instr_no_data1(arm11,
  736. !no_increment ? 0xe0c010b2 : 0xe1c010b0);
  737. if (retval != ERROR_OK)
  738. return retval;
  739. }
  740. break;
  741. }
  742. case 4: {
  743. /* stream word data through DCC directly to memory */
  744. /* increment: STC p14,c5,[R0],#4 */
  745. /* no increment: STC p14,c5,[R0]*/
  746. uint32_t instr = !no_increment ? 0xeca05e01 : 0xed805e00;
  747. /** \todo TODO: buffer cast to uint32_t* causes alignment warnings */
  748. uint32_t *words = (uint32_t *)(void *)buffer;
  749. /* "burst" here just means trusting each instruction executes
  750. * fully before we run the next one: per-word roundtrips, to
  751. * check the Ready flag, are not used.
  752. */
  753. if (!burst)
  754. retval = arm11_run_instr_data_to_core(arm11,
  755. instr, words, count);
  756. else
  757. retval = arm11_run_instr_data_to_core_noack(arm11,
  758. instr, words, count);
  759. if (retval != ERROR_OK)
  760. return retval;
  761. break;
  762. }
  763. }
  764. /* r0 verification */
  765. if (!no_increment) {
  766. uint32_t r0;
  767. /* MCR p14,0,R0,c0,c5,0 */
  768. retval = arm11_run_instr_data_from_core(arm11, 0xEE000E15, &r0, 1);
  769. if (retval != ERROR_OK)
  770. return retval;
  771. if (address + size * count != r0) {
  772. LOG_ERROR("Data transfer failed. Expected end "
  773. "address 0x%08x, got 0x%08x",
  774. (unsigned) (address + size * count),
  775. (unsigned) r0);
  776. if (burst)
  777. LOG_ERROR(
  778. "use 'arm11 memwrite burst disable' to disable fast burst mode");
  779. if (arm11->memwrite_error_fatal)
  780. return ERROR_FAIL;
  781. }
  782. }
  783. return arm11_run_instr_data_finish(arm11);
  784. }
  785. static int arm11_write_memory(struct target *target,
  786. uint32_t address, uint32_t size,
  787. uint32_t count, const uint8_t *buffer)
  788. {
  789. /* pointer increment matters only for multi-unit writes ...
  790. * not e.g. to a "reset the chip" controller.
  791. */
  792. return arm11_write_memory_inner(target, address, size,
  793. count, buffer, count == 1);
  794. }
  795. /* target break-/watchpoint control
  796. * rw: 0 = write, 1 = read, 2 = access
  797. */
  798. static int arm11_add_breakpoint(struct target *target,
  799. struct breakpoint *breakpoint)
  800. {
  801. struct arm11_common *arm11 = target_to_arm11(target);
  802. #if 0
  803. if (breakpoint->type == BKPT_SOFT) {
  804. LOG_INFO("sw breakpoint requested, but software breakpoints not enabled");
  805. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  806. }
  807. #endif
  808. if (!arm11->free_brps) {
  809. LOG_DEBUG("no breakpoint unit available for hardware breakpoint");
  810. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  811. }
  812. if (breakpoint->length != 4) {
  813. LOG_DEBUG("only breakpoints of four bytes length supported");
  814. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  815. }
  816. arm11->free_brps--;
  817. return ERROR_OK;
  818. }
  819. static int arm11_remove_breakpoint(struct target *target,
  820. struct breakpoint *breakpoint)
  821. {
  822. struct arm11_common *arm11 = target_to_arm11(target);
  823. arm11->free_brps++;
  824. return ERROR_OK;
  825. }
  826. static int arm11_target_create(struct target *target, Jim_Interp *interp)
  827. {
  828. struct arm11_common *arm11;
  829. if (target->tap == NULL)
  830. return ERROR_FAIL;
  831. if (target->tap->ir_length != 5) {
  832. LOG_ERROR("'target arm11' expects IR LENGTH = 5");
  833. return ERROR_COMMAND_SYNTAX_ERROR;
  834. }
  835. arm11 = calloc(1, sizeof *arm11);
  836. if (!arm11)
  837. return ERROR_FAIL;
  838. arm11->arm.core_type = ARM_MODE_ANY;
  839. arm_init_arch_info(target, &arm11->arm);
  840. arm11->jtag_info.tap = target->tap;
  841. arm11->jtag_info.scann_size = 5;
  842. arm11->jtag_info.scann_instr = ARM11_SCAN_N;
  843. arm11->jtag_info.cur_scan_chain = ~0; /* invalid/unknown */
  844. arm11->jtag_info.intest_instr = ARM11_INTEST;
  845. arm11->memwrite_burst = true;
  846. arm11->memwrite_error_fatal = true;
  847. return ERROR_OK;
  848. }
  849. static int arm11_init_target(struct command_context *cmd_ctx,
  850. struct target *target)
  851. {
  852. /* Initialize anything we can set up without talking to the target */
  853. return ERROR_OK;
  854. }
  855. /* talk to the target and set things up */
  856. static int arm11_examine(struct target *target)
  857. {
  858. int retval;
  859. char *type;
  860. struct arm11_common *arm11 = target_to_arm11(target);
  861. uint32_t didr, device_id;
  862. uint8_t implementor;
  863. /* FIXME split into do-first-time and do-every-time logic ... */
  864. /* check IDCODE */
  865. arm11_add_IR(arm11, ARM11_IDCODE, ARM11_TAP_DEFAULT);
  866. struct scan_field idcode_field;
  867. arm11_setup_field(arm11, 32, NULL, &device_id, &idcode_field);
  868. arm11_add_dr_scan_vc(arm11->arm.target->tap, 1, &idcode_field, TAP_DRPAUSE);
  869. /* check DIDR */
  870. arm11_add_debug_SCAN_N(arm11, 0x00, ARM11_TAP_DEFAULT);
  871. arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
  872. struct scan_field chain0_fields[2];
  873. arm11_setup_field(arm11, 32, NULL, &didr, chain0_fields + 0);
  874. arm11_setup_field(arm11, 8, NULL, &implementor, chain0_fields + 1);
  875. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(
  876. chain0_fields), chain0_fields, TAP_IDLE);
  877. CHECK_RETVAL(jtag_execute_queue());
  878. /* assume the manufacturer id is ok; check the part # */
  879. switch ((device_id >> 12) & 0xFFFF) {
  880. case 0x7B36:
  881. type = "ARM1136";
  882. break;
  883. case 0x7B37:
  884. type = "ARM11 MPCore";
  885. break;
  886. case 0x7B56:
  887. type = "ARM1156";
  888. break;
  889. case 0x7B76:
  890. arm11->arm.core_type = ARM_MODE_MON;
  891. /* NOTE: could default arm11->hardware_step to true */
  892. type = "ARM1176";
  893. break;
  894. default:
  895. LOG_ERROR("unexpected ARM11 ID code");
  896. return ERROR_FAIL;
  897. }
  898. LOG_INFO("found %s", type);
  899. /* unlikely this could ever fail, but ... */
  900. switch ((didr >> 16) & 0x0F) {
  901. case ARM11_DEBUG_V6:
  902. case ARM11_DEBUG_V61: /* supports security extensions */
  903. break;
  904. default:
  905. LOG_ERROR("Only ARM v6 and v6.1 debug supported.");
  906. return ERROR_FAIL;
  907. }
  908. arm11->brp = ((didr >> 24) & 0x0F) + 1;
  909. /** \todo TODO: reserve one brp slot if we allow breakpoints during step */
  910. arm11->free_brps = arm11->brp;
  911. LOG_DEBUG("IDCODE %08" PRIx32 " IMPLEMENTOR %02x DIDR %08" PRIx32,
  912. device_id, implementor, didr);
  913. /* as a side-effect this reads DSCR and thus
  914. * clears the ARM11_DSCR_STICKY_PRECISE_DATA_ABORT / Sticky Precise Data Abort Flag
  915. * as suggested by the spec.
  916. */
  917. retval = arm11_check_init(arm11);
  918. if (retval != ERROR_OK)
  919. return retval;
  920. /* Build register cache "late", after target_init(), since we
  921. * want to know if this core supports Secure Monitor mode.
  922. */
  923. if (!target_was_examined(target))
  924. CHECK_RETVAL(arm11_dpm_init(arm11, didr));
  925. /* ETM on ARM11 still uses original scanchain 6 access mode */
  926. if (arm11->arm.etm && !target_was_examined(target)) {
  927. *register_get_last_cache_p(&target->reg_cache) =
  928. etm_build_reg_cache(target, &arm11->jtag_info,
  929. arm11->arm.etm);
  930. CHECK_RETVAL(etm_setup(target));
  931. }
  932. target_set_examined(target);
  933. return ERROR_OK;
  934. }
  935. #define ARM11_BOOL_WRAPPER(name, print_name) \
  936. COMMAND_HANDLER(arm11_handle_bool_ ## name) \
  937. { \
  938. struct target *target = get_current_target(CMD_CTX); \
  939. struct arm11_common *arm11 = target_to_arm11(target); \
  940. \
  941. return CALL_COMMAND_HANDLER(handle_command_parse_bool, \
  942. &arm11->name, print_name); \
  943. }
  944. ARM11_BOOL_WRAPPER(memwrite_burst, "memory write burst mode")
  945. ARM11_BOOL_WRAPPER(memwrite_error_fatal, "fatal error mode for memory writes")
  946. ARM11_BOOL_WRAPPER(step_irq_enable, "IRQs while stepping")
  947. ARM11_BOOL_WRAPPER(hardware_step, "hardware single step")
  948. /* REVISIT handle the VCR bits like other ARMs: use symbols for
  949. * input and output values.
  950. */
  951. COMMAND_HANDLER(arm11_handle_vcr)
  952. {
  953. struct target *target = get_current_target(CMD_CTX);
  954. struct arm11_common *arm11 = target_to_arm11(target);
  955. switch (CMD_ARGC) {
  956. case 0:
  957. break;
  958. case 1:
  959. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], arm11->vcr);
  960. break;
  961. default:
  962. return ERROR_COMMAND_SYNTAX_ERROR;
  963. }
  964. LOG_INFO("VCR 0x%08" PRIx32 "", arm11->vcr);
  965. return ERROR_OK;
  966. }
  967. static const struct command_registration arm11_mw_command_handlers[] = {
  968. {
  969. .name = "burst",
  970. .handler = arm11_handle_bool_memwrite_burst,
  971. .mode = COMMAND_ANY,
  972. .help = "Display or modify flag controlling potentially "
  973. "risky fast burst mode (default: enabled)",
  974. .usage = "['enable'|'disable']",
  975. },
  976. {
  977. .name = "error_fatal",
  978. .handler = arm11_handle_bool_memwrite_error_fatal,
  979. .mode = COMMAND_ANY,
  980. .help = "Display or modify flag controlling transfer "
  981. "termination on transfer errors"
  982. " (default: enabled)",
  983. .usage = "['enable'|'disable']",
  984. },
  985. COMMAND_REGISTRATION_DONE
  986. };
  987. static const struct command_registration arm11_any_command_handlers[] = {
  988. {
  989. /* "hardware_step" is only here to check if the default
  990. * simulate + breakpoint implementation is broken.
  991. * TEMPORARY! NOT DOCUMENTED! */
  992. .name = "hardware_step",
  993. .handler = arm11_handle_bool_hardware_step,
  994. .mode = COMMAND_ANY,
  995. .help = "DEBUG ONLY - Hardware single stepping"
  996. " (default: disabled)",
  997. .usage = "['enable'|'disable']",
  998. },
  999. {
  1000. .name = "memwrite",
  1001. .mode = COMMAND_ANY,
  1002. .help = "memwrite command group",
  1003. .usage = "",
  1004. .chain = arm11_mw_command_handlers,
  1005. },
  1006. {
  1007. .name = "step_irq_enable",
  1008. .handler = arm11_handle_bool_step_irq_enable,
  1009. .mode = COMMAND_ANY,
  1010. .help = "Display or modify flag controlling interrupt "
  1011. "enable while stepping (default: disabled)",
  1012. .usage = "['enable'|'disable']",
  1013. },
  1014. {
  1015. .name = "vcr",
  1016. .handler = arm11_handle_vcr,
  1017. .mode = COMMAND_ANY,
  1018. .help = "Display or modify Vector Catch Register",
  1019. .usage = "[value]",
  1020. },
  1021. COMMAND_REGISTRATION_DONE
  1022. };
  1023. static const struct command_registration arm11_command_handlers[] = {
  1024. {
  1025. .chain = arm_command_handlers,
  1026. },
  1027. {
  1028. .chain = etm_command_handlers,
  1029. },
  1030. {
  1031. .name = "arm11",
  1032. .mode = COMMAND_ANY,
  1033. .help = "ARM11 command group",
  1034. .usage = "",
  1035. .chain = arm11_any_command_handlers,
  1036. },
  1037. COMMAND_REGISTRATION_DONE
  1038. };
  1039. /** Holds methods for ARM11xx targets. */
  1040. struct target_type arm11_target = {
  1041. .name = "arm11",
  1042. .poll = arm11_poll,
  1043. .arch_state = arm11_arch_state,
  1044. .halt = arm11_halt,
  1045. .resume = arm11_resume,
  1046. .step = arm11_step,
  1047. .assert_reset = arm11_assert_reset,
  1048. .deassert_reset = arm11_deassert_reset,
  1049. .get_gdb_reg_list = arm_get_gdb_reg_list,
  1050. .read_memory = arm11_read_memory,
  1051. .write_memory = arm11_write_memory,
  1052. .checksum_memory = arm_checksum_memory,
  1053. .blank_check_memory = arm_blank_check_memory,
  1054. .add_breakpoint = arm11_add_breakpoint,
  1055. .remove_breakpoint = arm11_remove_breakpoint,
  1056. .run_algorithm = armv4_5_run_algorithm,
  1057. .commands = arm11_command_handlers,
  1058. .target_create = arm11_target_create,
  1059. .init_target = arm11_init_target,
  1060. .examine = arm11_examine,
  1061. };