arm_stub.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * ARM GDB support
  3. * arch-specific portion of GDB stub
  4. *
  5. * File : arm_stub.c
  6. * This file is part of RT-Thread RTOS
  7. * COPYRIGHT (C) 2006, RT-Thread Develop Team
  8. *
  9. * The license and distribution terms for this file may be
  10. * found in the file LICENSE in this distribution or at
  11. * http://www.rt-thread.org/license/LICENSE
  12. *
  13. * Change Logs:
  14. * Date Author Notes
  15. * 2014-07-04 Wzyy2 first version
  16. */
  17. #include <rtthread.h>
  18. #include <rthw.h>
  19. #include <gdb_stub.h>
  20. #include <arch_gdb.h>
  21. #define PS_N 0x80000000
  22. #define PS_Z 0x40000000
  23. #define PS_C 0x20000000
  24. #define PS_V 0x10000000
  25. #define IS_THUMB_ADDR(addr) ((addr) & 1)
  26. #define MAKE_THUMB_ADDR(addr) ((addr) | 1)
  27. #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
  28. static int compiled_break = 0;
  29. static unsigned long step_addr = 0;
  30. static int ins_will_execute(unsigned long ins);
  31. static unsigned long target_ins(unsigned long *pc, unsigned long ins);
  32. /*struct gdb_arch - Describe architecture specific values.*/
  33. struct gdb_arch arch_gdb_ops = {
  34. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7} //Little-Endian
  35. };
  36. struct rt_gdb_register
  37. {
  38. rt_uint32_t r0;
  39. rt_uint32_t r1;
  40. rt_uint32_t r2;
  41. rt_uint32_t r3;
  42. rt_uint32_t r4;
  43. rt_uint32_t r5;
  44. rt_uint32_t r6;
  45. rt_uint32_t r7;
  46. rt_uint32_t r8;
  47. rt_uint32_t r9;
  48. rt_uint32_t r10;
  49. rt_uint32_t fp;
  50. rt_uint32_t ip;
  51. rt_uint32_t sp;
  52. rt_uint32_t lr;
  53. rt_uint32_t pc;
  54. rt_uint32_t cpsr;
  55. rt_uint32_t ORIG_r0;
  56. }*regs;
  57. /**
  58. * gdb_breakpoint - generate a compiled_breadk
  59. * It is used to sync up with a debugger and stop progarm
  60. */
  61. void gdb_breakpoint()
  62. {
  63. asm(".word 0xe7ffdeff");
  64. }
  65. void gdb_set_register(void *hw_regs)
  66. {
  67. regs = (struct rt_gdb_register *)hw_regs;
  68. }
  69. void gdb_get_register(unsigned long *gdb_regs)
  70. {
  71. int regno;
  72. /* Initialize all to zero. */
  73. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  74. gdb_regs[regno] = 0;
  75. gdb_regs[GDB_R0] = regs->r0;
  76. gdb_regs[GDB_R1] = regs->r1;
  77. gdb_regs[GDB_R2] = regs->r2;
  78. gdb_regs[GDB_R3] = regs->r3;
  79. gdb_regs[GDB_R4] = regs->r4;
  80. gdb_regs[GDB_R5] = regs->r5;
  81. gdb_regs[GDB_R6] = regs->r6;
  82. gdb_regs[GDB_R7] = regs->r7;
  83. gdb_regs[GDB_R8] = regs->r8;
  84. gdb_regs[GDB_R9] = regs->r9;
  85. gdb_regs[GDB_R10] = regs->r10;
  86. gdb_regs[GDB_FP] = regs->fp;
  87. gdb_regs[GDB_IP] = regs->ip;
  88. gdb_regs[GDB_SPT] = regs->sp;
  89. gdb_regs[GDB_LR] = regs->lr;
  90. gdb_regs[GDB_PC] = regs->pc;
  91. gdb_regs[GDB_CPSR] = regs->cpsr;
  92. };
  93. void gdb_put_register(unsigned long *gdb_regs)
  94. {
  95. regs->r0 = gdb_regs[GDB_R0];
  96. regs->r1 = gdb_regs[GDB_R1];
  97. regs->r2 = gdb_regs[GDB_R2];
  98. regs->r3 = gdb_regs[GDB_R3];
  99. regs->r4 = gdb_regs[GDB_R4];
  100. regs->r5 = gdb_regs[GDB_R5];
  101. regs->r6 = gdb_regs[GDB_R6];
  102. regs->r7 = gdb_regs[GDB_R7];
  103. regs->r8 = gdb_regs[GDB_R8];
  104. regs->r9 = gdb_regs[GDB_R9];
  105. regs->r10 = gdb_regs[GDB_R10];
  106. regs->fp = gdb_regs[GDB_FP];
  107. regs->ip = gdb_regs[GDB_IP];
  108. regs->sp = gdb_regs[GDB_SPT];
  109. regs->lr = gdb_regs[GDB_LR];
  110. regs->pc = gdb_regs[GDB_PC];
  111. regs->cpsr = gdb_regs[GDB_CPSR];
  112. }
  113. /* It will be called during process_packet */
  114. int gdb_arch_handle_exception(char *remcom_in_buffer,
  115. char *remcom_out_buffer)
  116. {
  117. unsigned long addr,curins;
  118. char *ptr;
  119. /*clear single step*/
  120. if (step_addr) {
  121. gdb_remove_sw_break(step_addr);
  122. step_addr = 0;
  123. }
  124. switch (remcom_in_buffer[0]) {
  125. case 'D':
  126. case 'k':
  127. case 'c':
  128. /*
  129. * If this was a compiled breakpoint, we need to move
  130. * to the next instruction or we will breakpoint
  131. * over and over again
  132. */
  133. ptr = &remcom_in_buffer[1];
  134. if (gdb_hex2long(&ptr, &addr))
  135. regs->pc = addr;
  136. else if (compiled_break == 1)
  137. regs->pc += 4;
  138. compiled_break = 0;
  139. return 0;
  140. case 's':
  141. ptr = &remcom_in_buffer[1];
  142. if (gdb_hex2long(&ptr, &addr))
  143. regs->pc = addr;
  144. curins = *(unsigned long*)(regs->pc);
  145. if (ins_will_execute(curins))
  146. //Decode instruction to decide what the next pc will be
  147. step_addr = target_ins((unsigned long *)regs->pc, curins);
  148. else
  149. step_addr = regs->pc + 4;
  150. #ifdef RT_GDB_DEBUG
  151. rt_kprintf("\n next will be %x \n",step_addr);
  152. #endif
  153. gdb_set_sw_break(step_addr);
  154. if (compiled_break == 1)
  155. regs->pc += 4;
  156. compiled_break = 0;
  157. return 0;
  158. }
  159. return -1;
  160. }
  161. /* flush icache to let the sw breakpoint working */
  162. void gdb_flush_icache_range(unsigned long start, unsigned long end)
  163. {
  164. #ifdef RT_GDB_ICACHE
  165. extern void mmu_invalidate_icache();
  166. mmu_invalidate_icache(); //for arm,wo can only invalidate it
  167. #endif
  168. }
  169. /* register a hook in undef*/
  170. int gdb_undef_hook(void *regs)
  171. {
  172. struct rt_gdb_register *tmp_reg = (struct rt_gdb_register *)regs;
  173. unsigned long *tmp_pc = (unsigned long *)tmp_reg->pc;
  174. /* it is a compiled break */
  175. if (*tmp_pc == GDB_COMPILED_BREAK) {
  176. compiled_break = 1;
  177. gdb_handle_exception(SIGTRAP, regs);
  178. return 1;
  179. }
  180. /* it is a sw break */
  181. else if (*tmp_pc == GDB_BREAKINST) {
  182. gdb_handle_exception(SIGTRAP, regs);
  183. return 1;
  184. }
  185. /*or we just go */
  186. return 0;
  187. }
  188. static unsigned long gdb_arch_regs[GDB_MAX_REGS];
  189. static int ins_will_execute(unsigned long ins)
  190. {
  191. unsigned long psr = regs->cpsr; // condition codes
  192. int res = 0;
  193. switch ((ins & 0xF0000000) >> 28) {
  194. case 0x0: // EQ
  195. res = (psr & PS_Z) != 0;
  196. break;
  197. case 0x1: // NE
  198. res = (psr & PS_Z) == 0;
  199. break;
  200. case 0x2: // CS
  201. res = (psr & PS_C) != 0;
  202. break;
  203. case 0x3: // CC
  204. res = (psr & PS_C) == 0;
  205. break;
  206. case 0x4: // MI
  207. res = (psr & PS_N) != 0;
  208. break;
  209. case 0x5: // PL
  210. res = (psr & PS_N) == 0;
  211. break;
  212. case 0x6: // VS
  213. res = (psr & PS_V) != 0;
  214. break;
  215. case 0x7: // VC
  216. res = (psr & PS_V) == 0;
  217. break;
  218. case 0x8: // HI
  219. res = ((psr & PS_C) != 0) && ((psr & PS_Z) == 0);
  220. break;
  221. case 0x9: // LS
  222. res = ((psr & PS_C) == 0) || ((psr & PS_Z) != 0);
  223. break;
  224. case 0xA: // GE
  225. res = ((psr & (PS_N|PS_V)) == (PS_N|PS_V)) ||
  226. ((psr & (PS_N|PS_V)) == 0);
  227. break;
  228. case 0xB: // LT
  229. res = ((psr & (PS_N|PS_V)) == PS_N) ||
  230. ((psr & (PS_N|PS_V)) == PS_V);
  231. break;
  232. case 0xC: // GT
  233. res = ((psr & (PS_N|PS_V)) == (PS_N|PS_V)) ||
  234. ((psr & (PS_N|PS_V)) == 0);
  235. res = ((psr & PS_Z) == 0) && res;
  236. break;
  237. case 0xD: // LE
  238. res = ((psr & (PS_N|PS_V)) == PS_N) ||
  239. ((psr & (PS_N|PS_V)) == PS_V);
  240. res = ((psr & PS_Z) == PS_Z) || res;
  241. break;
  242. case 0xE: // AL
  243. res = 1;
  244. break;
  245. case 0xF: // NV
  246. if (((ins & 0x0E000000) >> 24) == 0xA)
  247. res = 1;
  248. else
  249. res = 0;
  250. break;
  251. }
  252. return res;
  253. }
  254. static unsigned long RmShifted(int shift)
  255. {
  256. unsigned long Rm = gdb_arch_regs[shift & 0x00F];
  257. int shift_count;
  258. if ((shift & 0x010) == 0) {
  259. shift_count = (shift & 0xF80) >> 7;
  260. } else {
  261. shift_count = gdb_arch_regs[(shift & 0xF00) >> 8];
  262. }
  263. switch ((shift & 0x060) >> 5) {
  264. case 0x0: // Logical left
  265. Rm <<= shift_count;
  266. break;
  267. case 0x1: // Logical right
  268. Rm >>= shift_count;
  269. break;
  270. case 0x2: // Arithmetic right
  271. Rm = (unsigned long)((long)Rm >> shift_count);
  272. break;
  273. case 0x3: // Rotate right
  274. if (shift_count == 0) {
  275. // Special case, RORx
  276. Rm >>= 1;
  277. if (gdb_arch_regs[GDB_CPSR] & PS_C) Rm |= 0x80000000;
  278. } else {
  279. Rm = (Rm >> shift_count) | (Rm << (32-shift_count));
  280. }
  281. break;
  282. }
  283. return Rm;
  284. }
  285. // Decide the next instruction to be executed for a given instruction
  286. static unsigned long target_ins(unsigned long *pc, unsigned long ins)
  287. {
  288. unsigned long new_pc, offset, op2;
  289. unsigned long Rn;
  290. int i, reg_count, c;
  291. gdb_get_register(gdb_arch_regs);
  292. switch ((ins & 0x0C000000) >> 26) {
  293. case 0x0:
  294. // BX or BLX
  295. if ((ins & 0x0FFFFFD0) == 0x012FFF10) {
  296. new_pc = (unsigned long)gdb_arch_regs[ins & 0x0000000F];
  297. return new_pc;
  298. }
  299. // Data processing
  300. new_pc = (unsigned long)(pc+1);
  301. if ((ins & 0x0000F000) == 0x0000F000) {
  302. // Destination register is PC
  303. if ((ins & 0x0FBF0000) != 0x010F0000) {
  304. Rn = (unsigned long)gdb_arch_regs[(ins & 0x000F0000) >> 16];
  305. if ((ins & 0x000F0000) == 0x000F0000) Rn += 8; // PC prefetch!
  306. if ((ins & 0x02000000) == 0) {
  307. op2 = RmShifted(ins & 0x00000FFF);
  308. } else {
  309. op2 = ins & 0x000000FF;
  310. i = (ins & 0x00000F00) >> 8; // Rotate count
  311. op2 = (op2 >> (i*2)) | (op2 << (32-(i*2)));
  312. }
  313. switch ((ins & 0x01E00000) >> 21) {
  314. case 0x0: // AND
  315. new_pc = Rn & op2;
  316. break;
  317. case 0x1: // EOR
  318. new_pc = Rn ^ op2;
  319. break;
  320. case 0x2: // SUB
  321. new_pc = Rn - op2;
  322. break;
  323. case 0x3: // RSB
  324. new_pc = op2 - Rn;
  325. break;
  326. case 0x4: // ADD
  327. new_pc = Rn + op2;
  328. break;
  329. case 0x5: // ADC
  330. c = (gdb_arch_regs[GDB_CPSR] & PS_C) != 0;
  331. new_pc = Rn + op2 + c;
  332. break;
  333. case 0x6: // SBC
  334. c = (gdb_arch_regs[GDB_CPSR] & PS_C) != 0;
  335. new_pc = Rn - op2 + c - 1;
  336. break;
  337. case 0x7: // RSC
  338. c = (gdb_arch_regs[GDB_CPSR] & PS_C) != 0;
  339. new_pc = op2 - Rn +c - 1;
  340. break;
  341. case 0x8: // TST
  342. case 0x9: // TEQ
  343. case 0xA: // CMP
  344. case 0xB: // CMN
  345. break; // PC doesn't change
  346. case 0xC: // ORR
  347. new_pc = Rn | op2;
  348. break;
  349. case 0xD: // MOV
  350. new_pc = op2;
  351. break;
  352. case 0xE: // BIC
  353. new_pc = Rn & ~op2;
  354. break;
  355. case 0xF: // MVN
  356. new_pc = ~op2;
  357. break;
  358. }
  359. }
  360. }
  361. return new_pc;
  362. case 0x1:
  363. if ((ins & 0x02000010) == 0x02000010) {
  364. // Undefined!
  365. return (unsigned long)(pc+1);
  366. } else {
  367. if ((ins & 0x00100000) == 0) {
  368. // STR
  369. return (unsigned long)(pc+1);
  370. } else {
  371. // LDR
  372. if ((ins & 0x0000F000) != 0x0000F000) {
  373. // Rd not PC
  374. return (unsigned long)(pc+1);
  375. } else {
  376. Rn = (unsigned long)gdb_arch_regs[(ins & 0x000F0000) >> 16];
  377. if ((ins & 0x000F0000) == 0x000F0000) Rn += 8; // PC prefetch!
  378. if (ins & 0x01000000) {
  379. // Add/subtract offset before
  380. if ((ins & 0x02000000) == 0) {
  381. // Immediate offset
  382. if (ins & 0x00800000) {
  383. // Add offset
  384. Rn += (ins & 0x00000FFF);
  385. } else {
  386. // Subtract offset
  387. Rn -= (ins & 0x00000FFF);
  388. }
  389. } else {
  390. // Offset is in a register
  391. if (ins & 0x00800000) {
  392. // Add offset
  393. Rn += RmShifted(ins & 0x00000FFF);
  394. } else {
  395. // Subtract offset
  396. Rn -= RmShifted(ins & 0x00000FFF);
  397. }
  398. }
  399. }
  400. return *(unsigned long *)Rn;
  401. }
  402. }
  403. }
  404. return (unsigned long)(pc+1);
  405. case 0x2: // Branch, LDM/STM
  406. if ((ins & 0x02000000) == 0) {
  407. // LDM/STM
  408. if ((ins & 0x00100000) == 0) {
  409. // STM
  410. return (unsigned long)(pc+1);
  411. } else {
  412. // LDM
  413. if ((ins & 0x00008000) == 0) {
  414. // PC not in list
  415. return (unsigned long)(pc+1);
  416. } else {
  417. Rn = (unsigned long)gdb_arch_regs[(ins & 0x000F0000) >> 16];
  418. if ((ins & 0x000F0000) == 0x000F0000) Rn += 8; // PC prefetch!
  419. offset = ins & 0x0000FFFF;
  420. reg_count = 0;
  421. for (i = 0; i < 15; i++) {
  422. if (offset & (1<<i)) reg_count++;
  423. }
  424. if (ins & 0x00800000) {
  425. // Add offset
  426. Rn += reg_count*4;
  427. } else {
  428. // Subtract offset
  429. Rn -= 4;
  430. }
  431. return *(unsigned long *)Rn;
  432. }
  433. }
  434. } else {
  435. // Branch
  436. if (ins_will_execute(ins)) {
  437. offset = (ins & 0x00FFFFFF) << 2;
  438. if (ins & 0x00800000)
  439. offset |= 0xFC000000; // sign extend
  440. new_pc = (unsigned long)(pc+2) + offset;
  441. // If its BLX, make new_pc a thumb address.
  442. if ((ins & 0xFE000000) == 0xFA000000) {
  443. if ((ins & 0x01000000) == 0x01000000)
  444. new_pc |= 2;
  445. new_pc = MAKE_THUMB_ADDR(new_pc);
  446. }
  447. return new_pc;
  448. } else {
  449. // Falls through
  450. return (unsigned long)(pc+1);
  451. }
  452. }
  453. case 0x3: // Coprocessor & SWI
  454. if (((ins & 0x03000000) == 0x03000000) && ins_will_execute(ins)) {
  455. // SWI
  456. // TODO(wzyy2) some problems.
  457. extern unsigned long vector_swi;
  458. return vector_swi;
  459. } else {
  460. return (unsigned long)(pc+1);
  461. }
  462. default:
  463. // Never reached - but fixes compiler warning.
  464. return 0;
  465. }
  466. }