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.
 
 
 
 
 
 

719 lines
13 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "protocol.h"
  21. .text
  22. .align 4
  23. @ Disable thumb mode
  24. .code 32
  25. @ send word to debugger
  26. .macro m_send_to_debugger reg
  27. 1:
  28. mrc p14, 0, r15, c14, c0, 0
  29. bvs 1b
  30. mcr p14, 0, \reg, c8, c0, 0
  31. .endm
  32. @ receive word from debugger
  33. .macro m_receive_from_debugger reg
  34. 1:
  35. mrc p14, 0, r15, c14, c0, 0
  36. bpl 1b
  37. mrc p14, 0, \reg, c9, c0, 0
  38. .endm
  39. @ save register on debugger, small
  40. .macro m_small_save_reg reg
  41. mov r0, \reg
  42. bl send_to_debugger
  43. .endm
  44. @ save status register on debugger, small
  45. .macro m_small_save_psr
  46. mrs r0, spsr
  47. bl send_to_debugger
  48. .endm
  49. @ wait for all outstanding coprocessor accesses to complete
  50. .macro m_cpwait
  51. mrc p15, 0, r0, c2, c0, 0
  52. mov r0, r0
  53. sub pc, pc, #4
  54. .endm
  55. .global reset_handler
  56. .global undef_handler
  57. .global swi_handler
  58. .global prefetch_abort_handler
  59. .global data_abort_handler
  60. .global irq_handler
  61. .global fiq_handler
  62. .section .part1 , "ax"
  63. reset_handler:
  64. @ read DCSR
  65. mrc p14, 0, r13, c10, c0
  66. @ check if global enable bit (GE) is set
  67. ands r13, r13, #0x80000000
  68. bne debug_handler
  69. @ set global enable bit (GE)
  70. mov r13, #0xc0000000
  71. mcr p14, 0, r13, c10, c0
  72. debug_handler:
  73. @ save r0 without modifying other registers
  74. m_send_to_debugger r0
  75. @ save lr (program PC) without branching (use macro)
  76. m_send_to_debugger r14
  77. @ save non-banked registers and spsr (program CPSR)
  78. m_small_save_reg r1
  79. m_small_save_reg r2
  80. m_small_save_reg r3
  81. m_small_save_reg r4
  82. m_small_save_reg r5
  83. m_small_save_reg r6
  84. m_small_save_reg r7
  85. m_small_save_psr
  86. mrs r0, spsr
  87. @ prepare program PSR for debug use (clear Thumb, set I/F to disable interrupts)
  88. bic r0, r0, #PSR_T
  89. orr r0, r0, #(PSR_I | PSR_F)
  90. @ examine mode bits
  91. and r1, r0, #MODE_MASK
  92. cmp r1, #MODE_USR
  93. bne not_user_mode
  94. @ replace USR mode with SYS
  95. bic r0, r0, #MODE_MASK
  96. orr r0, r0, #MODE_SYS
  97. not_user_mode:
  98. b save_banked_registers
  99. @ command loop
  100. @ wait for command from debugger, than execute desired function
  101. get_command:
  102. bl receive_from_debugger
  103. @ 0x0n - register access
  104. cmp r0, #0x0
  105. beq get_banked_registers
  106. cmp r0, #0x1
  107. beq set_banked_registers
  108. @ 0x1n - read memory
  109. cmp r0, #0x11
  110. beq read_byte
  111. cmp r0, #0x12
  112. beq read_half_word
  113. cmp r0, #0x14
  114. beq read_word
  115. @ 0x2n - write memory
  116. cmp r0, #0x21
  117. beq write_byte
  118. cmp r0, #0x22
  119. beq write_half_word
  120. cmp r0, #0x24
  121. beq write_word
  122. @ 0x3n - program execution
  123. cmp r0, #0x30
  124. beq resume
  125. cmp r0, #0x31
  126. beq resume_w_trace
  127. @ 0x4n - coprocessor access
  128. cmp r0, #0x40
  129. beq read_cp_reg
  130. cmp r0, #0x41
  131. beq write_cp_reg
  132. @ 0x5n - cache and mmu functions
  133. cmp r0, #0x50
  134. beq clean_d_cache
  135. cmp r0, #0x51
  136. beq invalidate_d_cache
  137. cmp r0, #0x52
  138. beq invalidate_i_cache
  139. cmp r0, #0x53
  140. beq cpwait
  141. @ 0x6n - misc functions
  142. cmp r0, #0x60
  143. beq clear_sa
  144. cmp r0, #0x61
  145. beq read_trace_buffer
  146. cmp r0, #0x62
  147. beq clean_trace_buffer
  148. @ return (back to get_command)
  149. b get_command
  150. @ ----
  151. @ resume program execution
  152. resume:
  153. @ restore CPSR (SPSR_dbg)
  154. bl receive_from_debugger
  155. msr spsr, r0
  156. @ restore registers (r7 - r0)
  157. bl receive_from_debugger @ r7
  158. mov r7, r0
  159. bl receive_from_debugger @ r6
  160. mov r6, r0
  161. bl receive_from_debugger @ r5
  162. mov r5, r0
  163. bl receive_from_debugger @ r4
  164. mov r4, r0
  165. bl receive_from_debugger @ r3
  166. mov r3, r0
  167. bl receive_from_debugger @ r2
  168. mov r2, r0
  169. bl receive_from_debugger @ r1
  170. mov r1, r0
  171. bl receive_from_debugger @ r0
  172. @ resume addresss
  173. m_receive_from_debugger lr
  174. @ branch back to application code, restoring CPSR
  175. subs pc, lr, #0
  176. @ get banked registers
  177. @ receive mode bits from host, then run into save_banked_registers to
  178. get_banked_registers:
  179. bl receive_from_debugger
  180. @ save banked registers
  181. @ r0[4:0]: desired mode bits
  182. save_banked_registers:
  183. @ backup CPSR
  184. mrs r7, cpsr
  185. msr cpsr_c, r0
  186. nop
  187. @ keep current mode bits in r1 for later use
  188. and r1, r0, #MODE_MASK
  189. @ backup banked registers
  190. m_send_to_debugger r8
  191. m_send_to_debugger r9
  192. m_send_to_debugger r10
  193. m_send_to_debugger r11
  194. m_send_to_debugger r12
  195. m_send_to_debugger r13
  196. m_send_to_debugger r14
  197. @ if not in SYS mode (or USR, which we replaced with SYS before)
  198. cmp r1, #MODE_SYS
  199. beq no_spsr_to_save
  200. @ backup SPSR
  201. mrs r0, spsr
  202. m_send_to_debugger r0
  203. no_spsr_to_save:
  204. @ restore CPSR for SDS
  205. msr cpsr_c, r7
  206. nop
  207. @ return
  208. b get_command
  209. @ ----
  210. @ set banked registers
  211. @ receive mode bits from host, then run into save_banked_registers to
  212. set_banked_registers:
  213. bl receive_from_debugger
  214. @ restore banked registers
  215. @ r0[4:0]: desired mode bits
  216. restore_banked_registers:
  217. @ backup CPSR
  218. mrs r7, cpsr
  219. msr cpsr_c, r0
  220. nop
  221. @ keep current mode bits in r1 for later use
  222. and r1, r0, #MODE_MASK
  223. @ set banked registers
  224. m_receive_from_debugger r8
  225. m_receive_from_debugger r9
  226. m_receive_from_debugger r10
  227. m_receive_from_debugger r11
  228. m_receive_from_debugger r12
  229. m_receive_from_debugger r13
  230. m_receive_from_debugger r14
  231. @ if not in SYS mode (or USR, which we replaced with SYS before)
  232. cmp r1, #MODE_SYS
  233. beq no_spsr_to_restore
  234. @ set SPSR
  235. m_receive_from_debugger r0
  236. msr spsr, r0
  237. no_spsr_to_restore:
  238. @ restore CPSR for SDS
  239. msr cpsr_c, r7
  240. nop
  241. @ return
  242. b get_command
  243. @ ----
  244. read_byte:
  245. @ r2: address
  246. bl receive_from_debugger
  247. mov r2, r0
  248. @ r1: count
  249. bl receive_from_debugger
  250. mov r1, r0
  251. rb_loop:
  252. ldrb r0, [r2], #1
  253. @ drain write- (and fill-) buffer to work around XScale errata
  254. mcr p15, 0, r8, c7, c10, 4
  255. bl send_to_debugger
  256. subs r1, r1, #1
  257. bne rb_loop
  258. @ return
  259. b get_command
  260. @ ----
  261. read_half_word:
  262. @ r2: address
  263. bl receive_from_debugger
  264. mov r2, r0
  265. @ r1: count
  266. bl receive_from_debugger
  267. mov r1, r0
  268. rh_loop:
  269. ldrh r0, [r2], #2
  270. @ drain write- (and fill-) buffer to work around XScale errata
  271. mcr p15, 0, r8, c7, c10, 4
  272. bl send_to_debugger
  273. subs r1, r1, #1
  274. bne rh_loop
  275. @ return
  276. b get_command
  277. @ ----
  278. read_word:
  279. @ r2: address
  280. bl receive_from_debugger
  281. mov r2, r0
  282. @ r1: count
  283. bl receive_from_debugger
  284. mov r1, r0
  285. rw_loop:
  286. ldr r0, [r2], #4
  287. @ drain write- (and fill-) buffer to work around XScale errata
  288. mcr p15, 0, r8, c7, c10, 4
  289. bl send_to_debugger
  290. subs r1, r1, #1
  291. bne rw_loop
  292. @ return
  293. b get_command
  294. @ ----
  295. write_byte:
  296. @ r2: address
  297. bl receive_from_debugger
  298. mov r2, r0
  299. @ r1: count
  300. bl receive_from_debugger
  301. mov r1, r0
  302. wb_loop:
  303. bl receive_from_debugger
  304. strb r0, [r2], #1
  305. @ drain write- (and fill-) buffer to work around XScale errata
  306. mcr p15, 0, r8, c7, c10, 4
  307. subs r1, r1, #1
  308. bne wb_loop
  309. @ return
  310. b get_command
  311. @ ----
  312. write_half_word:
  313. @ r2: address
  314. bl receive_from_debugger
  315. mov r2, r0
  316. @ r1: count
  317. bl receive_from_debugger
  318. mov r1, r0
  319. wh_loop:
  320. bl receive_from_debugger
  321. strh r0, [r2], #2
  322. @ drain write- (and fill-) buffer to work around XScale errata
  323. mcr p15, 0, r8, c7, c10, 4
  324. subs r1, r1, #1
  325. bne wh_loop
  326. @ return
  327. b get_command
  328. @ ----
  329. write_word:
  330. @ r2: address
  331. bl receive_from_debugger
  332. mov r2, r0
  333. @ r1: count
  334. bl receive_from_debugger
  335. mov r1, r0
  336. ww_loop:
  337. bl receive_from_debugger
  338. str r0, [r2], #4
  339. @ drain write- (and fill-) buffer to work around XScale errata
  340. mcr p15, 0, r8, c7, c10, 4
  341. subs r1, r1, #1
  342. bne ww_loop
  343. @ return
  344. b get_command
  345. @ ----
  346. clear_sa:
  347. @ read DCSR
  348. mrc p14, 0, r0, c10, c0
  349. @ clear SA bit
  350. bic r0, r0, #0x20
  351. @ write DCSR
  352. mcr p14, 0, r0, c10, c0
  353. @ return
  354. b get_command
  355. @ ----
  356. clean_d_cache:
  357. @ r0: cache clean area
  358. bl receive_from_debugger
  359. mov r1, #1024
  360. clean_loop:
  361. mcr p15, 0, r0, c7, c2, 5
  362. add r0, r0, #32
  363. subs r1, r1, #1
  364. bne clean_loop
  365. @ return
  366. b get_command
  367. @ ----
  368. invalidate_d_cache:
  369. mcr p15, 0, r0, c7, c6, 0
  370. @ return
  371. b get_command
  372. @ ----
  373. invalidate_i_cache:
  374. mcr p15, 0, r0, c7, c5, 0
  375. @ return
  376. b get_command
  377. @ ----
  378. cpwait:
  379. m_cpwait
  380. @return
  381. b get_command
  382. @ ----
  383. .section .part2 , "ax"
  384. read_cp_reg:
  385. @ requested cp register
  386. bl receive_from_debugger
  387. adr r1, read_cp_table
  388. add pc, r1, r0, lsl #3
  389. read_cp_table:
  390. mrc p15, 0, r0, c0, c0, 0 @ XSCALE_MAINID
  391. b read_cp_reg_reply
  392. mrc p15, 0, r0, c0, c0, 1 @ XSCALE_CACHETYPE
  393. b read_cp_reg_reply
  394. mrc p15, 0, r0, c1, c0, 0 @ XSCALE_CTRL
  395. b read_cp_reg_reply
  396. mrc p15, 0, r0, c1, c0, 1 @ XSCALE_AUXCTRL
  397. b read_cp_reg_reply
  398. mrc p15, 0, r0, c2, c0, 0 @ XSCALE_TTB
  399. b read_cp_reg_reply
  400. mrc p15, 0, r0, c3, c0, 0 @ XSCALE_DAC
  401. b read_cp_reg_reply
  402. mrc p15, 0, r0, c5, c0, 0 @ XSCALE_FSR
  403. b read_cp_reg_reply
  404. mrc p15, 0, r0, c6, c0, 0 @ XSCALE_FAR
  405. b read_cp_reg_reply
  406. mrc p15, 0, r0, c13, c0, 0 @ XSCALE_PID
  407. b read_cp_reg_reply
  408. mrc p15, 0, r0, c15, c0, 0 @ XSCALE_CP_ACCESS
  409. b read_cp_reg_reply
  410. mrc p15, 0, r0, c14, c8, 0 @ XSCALE_IBCR0
  411. b read_cp_reg_reply
  412. mrc p15, 0, r0, c14, c9, 0 @ XSCALE_IBCR1
  413. b read_cp_reg_reply
  414. mrc p15, 0, r0, c14, c0, 0 @ XSCALE_DBR0
  415. b read_cp_reg_reply
  416. mrc p15, 0, r0, c14, c3, 0 @ XSCALE_DBR1
  417. b read_cp_reg_reply
  418. mrc p15, 0, r0, c14, c4, 0 @ XSCALE_DBCON
  419. b read_cp_reg_reply
  420. mrc p14, 0, r0, c11, c0, 0 @ XSCALE_TBREG
  421. b read_cp_reg_reply
  422. mrc p14, 0, r0, c12, c0, 0 @ XSCALE_CHKPT0
  423. b read_cp_reg_reply
  424. mrc p14, 0, r0, c13, c0, 0 @ XSCALE_CHKPT1
  425. b read_cp_reg_reply
  426. mrc p14, 0, r0, c10, c0, 0 @ XSCALE_DCSR
  427. b read_cp_reg_reply
  428. read_cp_reg_reply:
  429. bl send_to_debugger
  430. @ return
  431. b get_command
  432. @ ----
  433. write_cp_reg:
  434. @ requested cp register
  435. bl receive_from_debugger
  436. mov r1, r0
  437. @ value to be written
  438. bl receive_from_debugger
  439. adr r2, write_cp_table
  440. add pc, r2, r1, lsl #3
  441. write_cp_table:
  442. mcr p15, 0, r0, c0, c0, 0 @ XSCALE_MAINID (0x0)
  443. b get_command
  444. mcr p15, 0, r0, c0, c0, 1 @ XSCALE_CACHETYPE (0x1)
  445. b get_command
  446. mcr p15, 0, r0, c1, c0, 0 @ XSCALE_CTRL (0x2)
  447. b get_command
  448. mcr p15, 0, r0, c1, c0, 1 @ XSCALE_AUXCTRL (0x3)
  449. b get_command
  450. mcr p15, 0, r0, c2, c0, 0 @ XSCALE_TTB (0x4)
  451. b get_command
  452. mcr p15, 0, r0, c3, c0, 0 @ XSCALE_DAC (0x5)
  453. b get_command
  454. mcr p15, 0, r0, c5, c0, 0 @ XSCALE_FSR (0x6)
  455. b get_command
  456. mcr p15, 0, r0, c6, c0, 0 @ XSCALE_FAR (0x7)
  457. b get_command
  458. mcr p15, 0, r0, c13, c0, 0 @ XSCALE_PID (0x8)
  459. b get_command
  460. mcr p15, 0, r0, c15, c0, 0 @ XSCALE_CP_ACCESS (0x9)
  461. b get_command
  462. mcr p15, 0, r0, c14, c8, 0 @ XSCALE_IBCR0 (0xa)
  463. b get_command
  464. mcr p15, 0, r0, c14, c9, 0 @ XSCALE_IBCR1 (0xb)
  465. b get_command
  466. mcr p15, 0, r0, c14, c0, 0 @ XSCALE_DBR0 (0xc)
  467. b get_command
  468. mcr p15, 0, r0, c14, c3, 0 @ XSCALE_DBR1 (0xd)
  469. b get_command
  470. mcr p15, 0, r0, c14, c4, 0 @ XSCALE_DBCON (0xe)
  471. b get_command
  472. mcr p14, 0, r0, c11, c0, 0 @ XSCALE_TBREG (0xf)
  473. b get_command
  474. mcr p14, 0, r0, c12, c0, 0 @ XSCALE_CHKPT0 (0x10)
  475. b get_command
  476. mcr p14, 0, r0, c13, c0, 0 @ XSCALE_CHKPT1 (0x11)
  477. b get_command
  478. mcr p14, 0, r0, c10, c0, 0 @ XSCALE_DCSR (0x12)
  479. b get_command
  480. @ ----
  481. read_trace_buffer:
  482. @ dump 256 entries from trace buffer
  483. mov r1, #256
  484. read_tb_loop:
  485. mrc p14, 0, r0, c11, c0, 0 @ XSCALE_TBREG
  486. bl send_to_debugger
  487. subs r1, r1, #1
  488. bne read_tb_loop
  489. @ dump checkpoint register 0
  490. mrc p14, 0, r0, c12, c0, 0 @ XSCALE_CHKPT0 (0x10)
  491. bl send_to_debugger
  492. @ dump checkpoint register 1
  493. mrc p14, 0, r0, c13, c0, 0 @ XSCALE_CHKPT1 (0x11)
  494. bl send_to_debugger
  495. @ return
  496. b get_command
  497. @ ----
  498. clean_trace_buffer:
  499. @ clean 256 entries from trace buffer
  500. mov r1, #256
  501. clean_tb_loop:
  502. mrc p14, 0, r0, c11, c0, 0 @ XSCALE_TBREG
  503. subs r1, r1, #1
  504. bne clean_tb_loop
  505. @ return
  506. b get_command
  507. @ ----
  508. @ resume program execution with trace buffer enabled
  509. resume_w_trace:
  510. @ restore CPSR (SPSR_dbg)
  511. bl receive_from_debugger
  512. msr spsr, r0
  513. @ restore registers (r7 - r0)
  514. bl receive_from_debugger @ r7
  515. mov r7, r0
  516. bl receive_from_debugger @ r6
  517. mov r6, r0
  518. bl receive_from_debugger @ r5
  519. mov r5, r0
  520. bl receive_from_debugger @ r4
  521. mov r4, r0
  522. bl receive_from_debugger @ r3
  523. mov r3, r0
  524. bl receive_from_debugger @ r2
  525. mov r2, r0
  526. bl receive_from_debugger @ r1
  527. mov r1, r0
  528. bl receive_from_debugger @ r0
  529. @ resume addresss
  530. m_receive_from_debugger lr
  531. mrc p14, 0, r13, c10, c0, 0 @ XSCALE_DCSR
  532. orr r13, r13, #1
  533. mcr p14, 0, r13, c10, c0, 0 @ XSCALE_DCSR
  534. @ branch back to application code, restoring CPSR
  535. subs pc, lr, #0
  536. undef_handler:
  537. swi_handler:
  538. prefetch_abort_handler:
  539. data_abort_handler:
  540. irq_handler:
  541. fiq_handler:
  542. 1:
  543. b 1b
  544. send_to_debugger:
  545. m_send_to_debugger r0
  546. mov pc, lr
  547. receive_from_debugger:
  548. m_receive_from_debugger r0
  549. mov pc, lr