cortexm_stub.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * CORTEXM GDB support
  3. * arch-specific portion of GDB stub
  4. *
  5. * File : cortexm_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. static Gdb_SavedRegisters *regs;
  21. unsigned long single_step_basepri = 0;
  22. void gdb_remove_all_hw_break();
  23. void gdb_enable_hw_debug();
  24. void gdb_disable_hw_debug();
  25. int gdb_set_hw_break(unsigned long, int, enum gdb_bptype);
  26. int gdb_remove_hw_break(unsigned long, int, enum gdb_bptype);
  27. /*struct gdb_arch - Describe architecture specific values.*/
  28. struct gdb_arch arch_gdb_ops = {
  29. /* Breakpoint instruction: */
  30. .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}, //Little-Endian
  31. .flags = RT_GDB_HAVE_HWBP,
  32. .set_hw_breakpoint = gdb_set_hw_break,
  33. .remove_hw_breakpoint = gdb_remove_hw_break,
  34. .remove_all_hw_break = gdb_remove_all_hw_break
  35. };
  36. static struct hw_breakpoint {
  37. int enabled;
  38. unsigned long addr;
  39. } breakinfo[HBP_NUM];
  40. static struct hw_watchpoint {
  41. int enabled;
  42. unsigned long addr;
  43. int len;
  44. enum gdb_bptype type;
  45. } watchinfo[HWP_NUM];
  46. //The following table defines the memory areas that GDB is allow to touch
  47. static const struct {
  48. unsigned long start;
  49. unsigned long end;
  50. } data_access[] =
  51. {
  52. { 0x20000000, 0x40000000-1}, // On-chip ram
  53. { 0x60000000, 0xa0000000-1}, // External ram
  54. { 0x00000000, 0x20000000-1}, // On-chip flash
  55. { 0x60000000, 0xa0000000-1}, // External flash
  56. { 0xE0000000, 0x00000000-1}, // cortex-M peripheral
  57. { 0x40000000, 0x60000000-1}, // mcu peripheral
  58. };
  59. int gdb_permit_data_access(unsigned long addr, unsigned long count)
  60. {
  61. unsigned char i;
  62. for (i = 0; i < sizeof(data_access)/sizeof(data_access[0]); i++) {
  63. if ((addr >= data_access[i].start) && (addr + count) <= data_access[i].end) {
  64. return 0;
  65. }
  66. }
  67. return -1;
  68. }
  69. /*we need to block all pending interrupts by swtting basepri
  70. * before doing the steo
  71. */
  72. void gdb_single_step()
  73. {
  74. volatile unsigned long *base;
  75. //mask all interrupts
  76. single_step_basepri = regs->basepri;
  77. regs->basepri = GDB_CORTEXM_PRIORITY_MAX;
  78. //When MON_EN = 1, this steps the core
  79. base = (unsigned long*)(GDB_DEBUG_REG_BASE + GDB_DEBUG_REG_DEMCR);
  80. *base |= GDB_DEBUG_REG_DEMCR_MON_STEP;
  81. /* Clear any bits set in DFSR*/
  82. base = (unsigned long*)(GDB_NVIC_REG_BASE + GDB_NVIC_REG_DFSR);
  83. *base = 0xffffffff;
  84. }
  85. void gdb_clear_single_step()
  86. {
  87. volatile unsigned long *base;
  88. regs->basepri = single_step_basepri;
  89. /*clear single step*/
  90. base = (unsigned long*)(GDB_DEBUG_REG_BASE + GDB_DEBUG_REG_DEMCR);
  91. *base &= ~GDB_DEBUG_REG_DEMCR_MON_STEP;
  92. // Clear any bits set in DFSR
  93. base = (unsigned long*)(GDB_NVIC_REG_BASE + GDB_NVIC_REG_DFSR);
  94. *base = 0xffffffff;
  95. }
  96. /**
  97. * gdb_breakpoint - generate a breadk
  98. * It is used to sync up with a debugger and stop progarm
  99. */
  100. void gdb_breakpoint()
  101. {
  102. volatile unsigned long *base;
  103. // Enable the FPB-FLASH PATCH BREAKPOINT
  104. base = (unsigned long*)(GDB_FPB_REG_BASE + GDB_FPB_REG_CTRL);
  105. *base |= GDB_FPB_REG_CTRL_KEY | GDB_FPB_REG_CTRL_ENABLE ;
  106. base = (unsigned long*)(GDB_DEBUG_REG_BASE + GDB_DEBUG_REG_DEMCR);
  107. /*
  108. * Enable the debug monitor. When enabled, the System handler priority
  109. * register controls its priority level.
  110. * If disabled, then all debug events go
  111. * to Hard fault
  112. */
  113. *base |= GDB_DEBUG_REG_DEMCR_MON_EN;
  114. // Enable DWT
  115. *base |= GDB_DEBUG_REG_DEMCR_TRCENA ;
  116. //Fall into debug monitor
  117. *base |= GDB_DEBUG_REG_DEMCR_MON_PEND;
  118. }
  119. void gdb_set_register(void *hw_regs)
  120. {
  121. regs = hw_regs;
  122. }
  123. void gdb_get_register(unsigned long *gdb_regs)
  124. {
  125. int regno;
  126. /* Initialize all to zero. */
  127. for (regno = 0; regno < GDB_MAX_REGS; regno++)
  128. gdb_regs[regno] = 0;
  129. gdb_regs[GDB_R0] = regs->r0;
  130. gdb_regs[GDB_R1] = regs->r1;
  131. gdb_regs[GDB_R2] = regs->r2;
  132. gdb_regs[GDB_R3] = regs->r3;
  133. gdb_regs[GDB_R4] = regs->r4;
  134. gdb_regs[GDB_R5] = regs->r5;
  135. gdb_regs[GDB_R6] = regs->r6;
  136. gdb_regs[GDB_R7] = regs->r7;
  137. gdb_regs[GDB_R8] = regs->r8;
  138. gdb_regs[GDB_R9] = regs->r9;
  139. gdb_regs[GDB_R10] = regs->r10;
  140. gdb_regs[GDB_FP] = regs->r11;
  141. gdb_regs[GDB_IP] = regs->r12;
  142. gdb_regs[GDB_SPT] = regs->sp;
  143. gdb_regs[GDB_LR] = regs->lr;
  144. gdb_regs[GDB_PC] = regs->pc;
  145. gdb_regs[GDB_CPSR] = regs->psr;
  146. };
  147. void gdb_put_register(unsigned long *gdb_regs)
  148. {
  149. regs->r0 = gdb_regs[GDB_R0];
  150. regs->r1 = gdb_regs[GDB_R1];
  151. regs->r2 = gdb_regs[GDB_R2];
  152. regs->r3 = gdb_regs[GDB_R3];
  153. regs->r4 = gdb_regs[GDB_R4];
  154. regs->r5 = gdb_regs[GDB_R5];
  155. regs->r6 = gdb_regs[GDB_R6];
  156. regs->r7 = gdb_regs[GDB_R7];
  157. regs->r8 = gdb_regs[GDB_R8];
  158. regs->r9 = gdb_regs[GDB_R9];
  159. regs->r10 = gdb_regs[GDB_R10];
  160. regs->r11 = gdb_regs[GDB_FP];
  161. regs->r12 = gdb_regs[GDB_IP];
  162. regs->sp = gdb_regs[GDB_SPT];
  163. regs->lr = gdb_regs[GDB_LR];
  164. regs->pc = gdb_regs[GDB_PC];
  165. regs->psr = gdb_regs[GDB_CPSR];
  166. }
  167. /* It will be called during process_packet */
  168. int gdb_arch_handle_exception(char *remcom_in_buffer,
  169. char *remcom_out_buffer)
  170. {
  171. unsigned long addr;
  172. char *ptr;
  173. static int step = 0;
  174. if (step){
  175. gdb_clear_single_step();
  176. step = 0;
  177. }
  178. switch (remcom_in_buffer[0]) {
  179. case 'D':
  180. case 'k':
  181. case 'c':
  182. /*
  183. * If this was a compiled breakpoint, we need to move
  184. * to the next instruction or we will breakpoint
  185. * over and over again
  186. */
  187. ptr = &remcom_in_buffer[1];
  188. if (gdb_hex2long(&ptr, &addr))
  189. regs->pc = addr;
  190. return 0;
  191. case 's':
  192. ptr = &remcom_in_buffer[1];
  193. if (gdb_hex2long(&ptr, &addr))
  194. regs->pc = addr;
  195. gdb_single_step();
  196. step = 1;
  197. return 0;
  198. }
  199. return -1;
  200. }
  201. int gdb_set_hw_break(unsigned long addr, int len, enum gdb_bptype bptype)
  202. {
  203. int i;
  204. if (bptype == BP_HARDWARE_BREAKPOINT) {
  205. for (i = 0; i < HBP_NUM; i++)
  206. if (!breakinfo[i].enabled)
  207. break;
  208. if (i == HBP_NUM)
  209. return -1;
  210. breakinfo[i].addr = addr;
  211. breakinfo[i].enabled = 1;
  212. }
  213. else if (bptype == BP_WRITE_WATCHPOINT) {
  214. for (i = 0; i < HWP_NUM; i++)
  215. if (!watchinfo[i].enabled)
  216. break;
  217. if (i == HWP_NUM)
  218. return -1;
  219. watchinfo[i].addr = addr;
  220. watchinfo[i].len = len;
  221. watchinfo[i].type = BP_WRITE_WATCHPOINT;
  222. watchinfo[i].enabled = 1;
  223. }
  224. else if (bptype == BP_READ_WATCHPOINT) {
  225. for (i = 0; i < HWP_NUM; i++)
  226. if (!watchinfo[i].enabled)
  227. break;
  228. if (i == HWP_NUM)
  229. return -1;
  230. watchinfo[i].addr = addr;
  231. watchinfo[i].len = len;
  232. watchinfo[i].type = BP_READ_WATCHPOINT;
  233. watchinfo[i].enabled = 1;
  234. }
  235. else if (bptype == BP_ACCESS_WATCHPOINT) {
  236. for (i = 0; i < HWP_NUM; i++)
  237. if (!watchinfo[i].enabled)
  238. break;
  239. if (i == HWP_NUM)
  240. return -1;
  241. watchinfo[i].addr = addr;
  242. watchinfo[i].len = len;
  243. watchinfo[i].type = BP_ACCESS_WATCHPOINT;
  244. watchinfo[i].enabled = 1;
  245. }
  246. return 0;
  247. }
  248. int gdb_remove_hw_break(unsigned long addr, int len, enum gdb_bptype bptype)
  249. {
  250. int i;
  251. if (bptype == BP_HARDWARE_BREAKPOINT) {
  252. for (i = 0; i < HBP_NUM; i++)
  253. if (breakinfo[i].addr == addr && breakinfo[i].enabled)
  254. break;
  255. if (i == HBP_NUM)
  256. return -1;
  257. breakinfo[i].enabled = 0;
  258. }
  259. else if (bptype == BP_WRITE_WATCHPOINT) {
  260. for (i = 0; i < HWP_NUM; i++)
  261. if (watchinfo[i].addr == addr && watchinfo[i].enabled && watchinfo[i].type == bptype)
  262. break;
  263. if (i == HWP_NUM)
  264. return -1;
  265. watchinfo[i].enabled = 0;
  266. }
  267. else if (bptype == BP_READ_WATCHPOINT) {
  268. for (i = 0; i < HWP_NUM; i++)
  269. if (watchinfo[i].addr == addr && watchinfo[i].enabled && watchinfo[i].type == bptype)
  270. break;
  271. if (i == HWP_NUM)
  272. return -1;
  273. watchinfo[i].enabled = 0;
  274. }
  275. else if (bptype == BP_ACCESS_WATCHPOINT) {
  276. for (i = 0; i < HWP_NUM; i++)
  277. if (watchinfo[i].addr == addr && watchinfo[i].enabled && watchinfo[i].type == bptype)
  278. break;
  279. if (i == HWP_NUM)
  280. return -1;
  281. watchinfo[i].enabled = 0;
  282. }
  283. return 0;
  284. }
  285. void gdb_remove_all_hw_break()
  286. {
  287. int i;
  288. volatile unsigned long *base;
  289. // Disable hardware break
  290. for (i = 0; i < HBP_NUM; i++) {
  291. if (!breakinfo[i].enabled)
  292. break;
  293. base = (unsigned long*)(GDB_FPB_REG_BASE + GDB_FPB_REG_COMP + i * 4);
  294. *base &= ~GDB_FPB_REG_COMP_ENABLE ;
  295. }
  296. // Disable watchpoint
  297. for (i = 0; i < HWP_NUM; i++) {
  298. if (!watchinfo[i].enabled)
  299. break;
  300. base = (unsigned long*)(GDB_DWT_REG_BASE + GDB_DWT_REG_FUNCTION + i * 12);
  301. *base &= ~GDB_DWT_REG_FUNCTION_FUC ;
  302. }
  303. }
  304. void gdb_arch_late()
  305. {
  306. gdb_remove_all_hw_break();
  307. }
  308. void gdb_arch_exit()
  309. {
  310. volatile unsigned long *base;
  311. char num = 1;
  312. int i;
  313. // Install the hardware break
  314. for (i = 0; i < HBP_NUM; i++) {
  315. if (breakinfo[i].enabled) {
  316. base = (unsigned long*)(GDB_FPB_REG_BASE + GDB_FPB_REG_COMP + i * 4);
  317. *base = GDB_FPB_REG_COMP_ADDR & ((unsigned long)(breakinfo[i].addr));
  318. if (breakinfo[i].addr & 2)
  319. *base |= (1UL << 31); //set BKPT on upper halfword
  320. else
  321. *base |= (1UL << 30); //set BKPT on lower halfword,
  322. *base |= GDB_FPB_REG_COMP_ENABLE ;
  323. }
  324. }
  325. // Install the watchpoint
  326. for (i = 0; i < HWP_NUM; i++) {
  327. if (watchinfo[i].enabled) {
  328. base = (unsigned long*)(GDB_DWT_REG_BASE + GDB_DWT_REG_COMP + i * 12);
  329. *base = watchinfo[i].addr;
  330. base = (unsigned long*)(GDB_DWT_REG_BASE + GDB_DWT_REG_MASK + i * 12);
  331. while (watchinfo[i].len >> num) {
  332. num++;
  333. }
  334. *base = num - 1; //DWT matching is performed as:(ADDR & (~0 << MASK)) == COMP
  335. base = (unsigned long*)(GDB_DWT_REG_BASE + GDB_DWT_REG_FUNCTION + i * 12);
  336. if (watchinfo[i].type == BP_WRITE_WATCHPOINT)
  337. *base = (*base & ~GDB_DWT_REG_FUNCTION_FUC) + 0x05;
  338. else if (watchinfo[i].type == BP_READ_WATCHPOINT)
  339. *base = (*base & ~GDB_DWT_REG_FUNCTION_FUC) + 0x06;
  340. else if (watchinfo[i].type == BP_ACCESS_WATCHPOINT)
  341. *base = (*base & ~GDB_DWT_REG_FUNCTION_FUC) + 0x07;
  342. }
  343. }
  344. }