Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

armv7m.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-08-16 Rbb666 first version
  9. */
  10. #include "coredump.h"
  11. #include "registers.h"
  12. #define FPU_CPACR 0xE000ED88
  13. int is_vfp_addressable(void)
  14. {
  15. uint32_t reg_cpacr = *((volatile uint32_t *)FPU_CPACR);
  16. if (reg_cpacr & 0x00F00000)
  17. return 1;
  18. else
  19. return 0;
  20. }
  21. #if defined(__CC_ARM)
  22. /* clang-format off */
  23. __asm void mcd_mini_dump()
  24. {
  25. extern get_cur_core_regset_address;
  26. extern get_cur_fp_regset_address;
  27. extern mcd_mini_dump_ops;
  28. extern mcd_gen_coredump;
  29. extern is_vfp_addressable;
  30. PRESERVE8
  31. push {r7, lr}
  32. sub sp, sp, #24
  33. add r7, sp, #0
  34. get_regset
  35. bl get_cur_core_regset_address
  36. str r0, [r0, #0]
  37. add r0, r0, #4
  38. stmia r0!, {r1 - r12}
  39. mov r1, sp
  40. add r1, #32
  41. str r1, [r0, #0]
  42. ldr r1, [sp, #28]
  43. str r1, [r0, #4]
  44. mov r1, pc
  45. str r1, [r0, #8]
  46. mrs r1, xpsr
  47. str r1, [r0, #12]
  48. bl is_vfp_addressable
  49. cmp r0, #0
  50. beq get_reg_done
  51. bl get_cur_fp_regset_address
  52. vstmia r0!, {d0 - d15}
  53. vmrs r1, fpscr
  54. str r1, [r0, #0]
  55. get_reg_done
  56. mov r0, r7
  57. bl mcd_mini_dump_ops
  58. mov r0, r7
  59. bl mcd_gen_coredump
  60. nop
  61. adds r7, r7, #24
  62. mov sp, r7
  63. pop {r7, pc}
  64. nop
  65. nop
  66. }
  67. __asm void mcd_multi_dump(void)
  68. {
  69. extern get_cur_core_regset_address;
  70. extern get_cur_fp_regset_address;
  71. extern mcd_rtos_thread_ops;
  72. extern mcd_gen_coredump;
  73. extern is_vfp_addressable;
  74. PRESERVE8
  75. push {r7, lr}
  76. sub sp, sp, #24
  77. add r7, sp, #0
  78. get_regset1
  79. bl get_cur_core_regset_address
  80. str r0, [r0, #0]
  81. add r0, r0, #4
  82. stmia r0!, {r1 - r12}
  83. mov r1, sp
  84. add r1, #32
  85. str r1, [r0, #0]
  86. ldr r1, [sp, #28]
  87. str r1, [r0, #4]
  88. mov r1, pc
  89. str r1, [r0, #8]
  90. mrs r1, xpsr
  91. str r1, [r0, #12]
  92. bl is_vfp_addressable
  93. cmp r0, #0
  94. beq get_reg_done
  95. bl get_cur_fp_regset_address
  96. vstmia r0!, {d0 - d15}
  97. vmrs r1, fpscr
  98. str r1, [r0, #0]
  99. get_reg_done1
  100. mov r0, r7
  101. bl mcd_rtos_thread_ops
  102. mov r0, r7
  103. bl mcd_gen_coredump
  104. nop
  105. adds r7, r7, #24
  106. mov sp, r7
  107. pop {r7, pc}
  108. nop
  109. nop
  110. }
  111. #elif 1 //defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) || defined(__GNUC__)
  112. #define mcd_get_regset(regset) \
  113. __asm volatile(" mov r0, %0 \n" \
  114. " str r0, [r0 , #0] \n" \
  115. " add r0, r0, #4 \n" \
  116. " stmia r0!, {r1 - r12} \n" \
  117. " mov r1, sp \n" \
  118. " str r1, [r0, #0] \n" \
  119. " mov r1, lr \n" \
  120. " str r1, [r0, #4] \n" \
  121. " mov r1, pc \n" \
  122. " str r1, [r0, #8] \n" \
  123. " mrs r1, xpsr \n" \
  124. " str r1, [r0, #12] \n" ::"r"(regset) \
  125. : "memory", "cc");
  126. #define mcd_get_fpregset(regset) \
  127. __asm volatile(" mov r0, %0 \n" \
  128. " vstmia r0!, {d0 - d15} \n" \
  129. " vmrs r1, fpscr \n" \
  130. " str r1, [r0, #0] \n" ::"r"(regset) \
  131. : "memory", "cc");
  132. // 不更新当前寄存器到全局变量(在异常中断中已填充)
  133. void mcd_hard_fault_exception_dump(void)
  134. {
  135. struct thread_info_ops ops;
  136. mcd_rtos_thread_ops(&ops);
  137. mcd_gen_coredump(&ops);
  138. if (ops.print_info_string)
  139. ops.print_info_string(&ops);
  140. }
  141. // 只保存当前线程或非线程(主栈):更新当前寄存器到全局变量
  142. void mcd_mini_dump(void)
  143. {
  144. struct thread_info_ops ops;
  145. mcd_get_regset((uint32_t *)get_cur_core_regset_address());
  146. #if MCD_FPU_SUPPORT
  147. if (is_vfp_addressable())
  148. mcd_get_fpregset((uint32_t *)get_cur_fp_regset_address());
  149. #endif
  150. mcd_mini_dump_ops(&ops);
  151. mcd_gen_coredump(&ops);
  152. if (ops.print_info_string)
  153. ops.print_info_string(&ops);
  154. }
  155. // 保存RTOS所有线程:更新当前寄存器到全局变量
  156. void mcd_multi_dump(void)
  157. {
  158. struct thread_info_ops ops;
  159. mcd_get_regset((uint32_t *)get_cur_core_regset_address());
  160. #if MCD_FPU_SUPPORT
  161. if (is_vfp_addressable())
  162. mcd_get_fpregset((uint32_t *)get_cur_fp_regset_address());
  163. #endif
  164. mcd_rtos_thread_ops(&ops);
  165. mcd_gen_coredump(&ops);
  166. if (ops.print_info_string)
  167. ops.print_info_string(&ops);
  168. }
  169. #endif
  170. void print_registers_armv7m(core_regset_type *reg, fp_regset_type *fp_reg, char *thread)
  171. {
  172. mcd_print("Registers@%s\n", thread);
  173. mcd_print("R0: 0x%08x\n", reg->r0);
  174. mcd_print("R1: 0x%08x\n", reg->r1);
  175. mcd_print("R2: 0x%08x\n", reg->r2);
  176. mcd_print("R3: 0x%08x\n", reg->r3);
  177. mcd_print("R4: 0x%08x\n", reg->r4);
  178. mcd_print("R5: 0x%08x\n", reg->r5);
  179. mcd_print("R6: 0x%08x\n", reg->r6);
  180. mcd_print("R7: 0x%08x\n", reg->r7);
  181. mcd_print("R8: 0x%08x\n", reg->r8);
  182. mcd_print("R9: 0x%08x\n", reg->r9);
  183. mcd_print("R10: 0x%08x\n", reg->r10);
  184. mcd_print("R11: 0x%08x\n", reg->r11);
  185. mcd_print("R12: 0x%08x\n", reg->r12);
  186. mcd_print("R13(SP): 0x%08x\n", reg->sp);
  187. mcd_print("R14(LR): 0x%08x\n", reg->lr);
  188. mcd_print("R15(PC): 0x%08x\n", reg->pc);
  189. mcd_print("xPSR: 0x%08x\n", reg->xpsr);
  190. }
  191. void print_mem_armv7m(uint32_t addr, uint32_t len)
  192. {
  193. uint32_t *paddr;
  194. uint32_t i;
  195. if (len == 0)
  196. return;
  197. paddr = (uint32_t *)addr;
  198. mcd_print("mem@0x%08x,0x%08x\n", addr, len);
  199. for (i = 0; i < len;)
  200. {
  201. mcd_print("0x%08x", *paddr);
  202. paddr++;
  203. i += 4;
  204. if (i % 16 == 0)
  205. mcd_print("\n");
  206. else
  207. mcd_print(" ");
  208. }
  209. mcd_print("\n");
  210. if (len % 16 != 0)
  211. mcd_print("\n");
  212. }
  213. /**
  214. * @brief Collect ARM Cortex-M4 registers from RT-Thread stack frame
  215. *
  216. * This function extracts register values from the stack frame created by
  217. * RT-Thread's context switch mechanism (PendSV_Handler) or exception handling.
  218. *
  219. * RT-Thread Stack Frame Layout (from low to high memory address):
  220. * +-------------------+ <- stack_top (input parameter)
  221. * | FPU flag | (4 bytes, if MCD_FPU_SUPPORT enabled)
  222. * +-------------------+
  223. * | r4 | (4 bytes, software saved)
  224. * | r5 | (4 bytes, software saved)
  225. * | r6 | (4 bytes, software saved)
  226. * | r7 | (4 bytes, software saved)
  227. * | r8 | (4 bytes, software saved)
  228. * | r9 | (4 bytes, software saved)
  229. * | r10 | (4 bytes, software saved)
  230. * | r11 | (4 bytes, software saved)
  231. * +-------------------+
  232. * | FPU s16-s31 | (64 bytes, if FPU context active)
  233. * +-------------------+
  234. * | r0 | (4 bytes, hardware saved)
  235. * | r1 | (4 bytes, hardware saved)
  236. * | r2 | (4 bytes, hardware saved)
  237. * | r3 | (4 bytes, hardware saved)
  238. * | r12 | (4 bytes, hardware saved)
  239. * | lr | (4 bytes, hardware saved)
  240. * | pc | (4 bytes, hardware saved)
  241. * | xpsr | (4 bytes, hardware saved)
  242. * +-------------------+
  243. * | FPU s0-s15 | (64 bytes, if FPU context active)
  244. * | FPSCR | (4 bytes, if FPU context active)
  245. * | NO_NAME | (4 bytes, if FPU context active)
  246. * +-------------------+ <- current SP after context save
  247. *
  248. * @param stack_top Pointer to the beginning of the stack frame (FPU flag position)
  249. * @param core_regset Pointer to structure for storing ARM core registers
  250. * @param fp_regset Pointer to structure for storing FPU registers
  251. */
  252. // 通过sp指针得到寄存器信息
  253. void collect_registers_armv7m(uint32_t *stack_top, core_regset_type *core_regset, fp_regset_type *fp_regset)
  254. {
  255. /*
  256. * This function uses the same stack frame parsing approach as collect_registers_armv7ms
  257. * for consistency. Both PendSV_Handler and HardFault_Handler now use identical
  258. * stacking order after the modifications.
  259. *
  260. * Expected stack layout starting from stack_top:
  261. * [FPU flag] -> [r4-r11] -> [FPU s16-s31] -> [exception frame] -> [FPU s0-s15,FPSCR]
  262. */
  263. uint32_t *current_ptr = stack_top;
  264. /* Clear both register sets first to ensure clean state */
  265. mcd_memset(core_regset, 0, sizeof(core_regset_type));
  266. mcd_memset(fp_regset, 0, sizeof(fp_regset_type));
  267. #if MCD_FPU_SUPPORT
  268. /* Read FPU flag first - indicates if FPU context was saved */
  269. uint32_t fpu_flag = *current_ptr++;
  270. #endif
  271. /* Extract core registers r4-r11 (software saved by RT-Thread) */
  272. core_regset->r4 = *current_ptr++;
  273. core_regset->r5 = *current_ptr++;
  274. core_regset->r6 = *current_ptr++;
  275. core_regset->r7 = *current_ptr++;
  276. core_regset->r8 = *current_ptr++;
  277. core_regset->r9 = *current_ptr++;
  278. core_regset->r10 = *current_ptr++;
  279. core_regset->r11 = *current_ptr++;
  280. #if MCD_FPU_SUPPORT
  281. /* If FPU context is active, s16-s31 registers are saved after r4-r11 */
  282. if (fpu_flag)
  283. {
  284. /* Copy FPU s16-s31 registers (software saved by RT-Thread) */
  285. for (int i = 16; i < 32; i++)
  286. {
  287. ((uint32_t *)fp_regset)[i] = *current_ptr++;
  288. }
  289. }
  290. #endif
  291. /* Extract hardware exception frame (automatically saved by ARM Cortex-M) */
  292. core_regset->r0 = *current_ptr++;
  293. core_regset->r1 = *current_ptr++;
  294. core_regset->r2 = *current_ptr++;
  295. core_regset->r3 = *current_ptr++;
  296. core_regset->r12 = *current_ptr++;
  297. core_regset->lr = *current_ptr++;
  298. core_regset->pc = *current_ptr++;
  299. core_regset->xpsr = *current_ptr++;
  300. #if MCD_FPU_SUPPORT
  301. /* If FPU context is active, s0-s15 and FPSCR are saved after exception frame */
  302. if (fpu_flag)
  303. {
  304. /* Copy FPU s0-s15 registers (hardware saved by ARM Cortex-M) */
  305. for (int i = 0; i < 16; i++)
  306. {
  307. ((uint32_t *)fp_regset)[i] = *current_ptr++;
  308. }
  309. /* Copy FPSCR register (FPU status and control) */
  310. fp_regset->fpscr = *current_ptr++;
  311. /* Skip NO_NAME field (reserved/alignment) */
  312. current_ptr++;
  313. }
  314. #endif
  315. /* SP should point to the current stack pointer position after all saved data */
  316. core_regset->sp = (uintptr_t)current_ptr;
  317. }
  318. /**
  319. * @brief ARM Cortex-M specific hard fault exception handler for MCoreDump
  320. *
  321. * This function handles ARM Cortex-M specific stack frame processing when a
  322. * hard fault occurs. It calculates the proper stack pointer position and
  323. * extracts register context for coredump generation.
  324. *
  325. * HardFault Stack Frame Layout (created by HardFault_Handler):
  326. * +-------------------+ <- Exception occurs here
  327. * | Hardware Exception| (32 bytes: r0,r1,r2,r3,r12,lr,pc,xpsr)
  328. * | Stack Frame | (+ optional 72 bytes FPU: s0-s15,FPSCR,NO_NAME)
  329. * +-------------------+ <- context parameter points here
  330. * | r11 | (4 bytes, software saved in HardFault_Handler)
  331. * | r10 | (4 bytes, software saved in HardFault_Handler)
  332. * | ... | (...)
  333. * | r4 | (4 bytes, software saved in HardFault_Handler)
  334. * +-------------------+
  335. * | FPU s31 | (4 bytes, if FPU context active)
  336. * | FPU s30 | (4 bytes, if FPU context active)
  337. * | ... | (...)
  338. * | FPU s16 | (4 bytes, if FPU context active)
  339. * +-------------------+
  340. * | FPU flag | (4 bytes, if MCD_FPU_SUPPORT enabled)
  341. * +-------------------+
  342. * | EXC_RETURN | (4 bytes, contains exception return information)
  343. * +-------------------+ <- Final stack pointer position
  344. *
  345. * @param context Pointer to exception_stack_frame from HardFault_Handler
  346. * @return int Always returns -1 to indicate fault condition
  347. */
  348. int armv7m_hard_fault_exception_hook(void *context)
  349. {
  350. /*
  351. * context points to exception_stack_frame created by HardFault_Handler.
  352. * We need to calculate the complete stack frame position to extract all registers.
  353. * Since HardFault_Handler now uses the same stacking order as PendSV_Handler,
  354. * we can directly use collect_registers_armv7m function.
  355. */
  356. #if 0
  357. struct exception_stack_frame *exception_stack = (struct exception_stack_frame *)context;
  358. /* Calculate stack pointer to the beginning of the saved context */
  359. uint32_t *stack_ptr = (uint32_t *)exception_stack;
  360. /*
  361. * Move backward through the stack to reach the beginning of saved context.
  362. * Stack layout (working backwards from exception_stack):
  363. * exception_stack -> r4-r11 (8 words) -> [s16-s31] -> [fpu_flag] -> [exc_return]
  364. */
  365. stack_ptr -= 8; /* Move backward 8 uint32_t positions to reach r4 */
  366. #if MCD_FPU_SUPPORT
  367. /* Point to FPU flag position (collect_registers_armv7m expects this as start) */
  368. stack_ptr -= 1; /* fpu flag */
  369. #else
  370. /* If no FPU support, skip EXC_RETURN and point to r4 directly */
  371. stack_ptr -= 1; /* exc_return */
  372. #endif
  373. #else
  374. uint32_t *stack_ptr = context;
  375. stack_ptr += 1; /* 跳过:exc_return */
  376. // stack_ptr += 1; // 如开启fpuflag
  377. #endif
  378. /*
  379. * Now stack_ptr points to where collect_registers_armv7m expects:
  380. * - With FPU: points to FPU flag (first field to be read)
  381. * - Without FPU: points to r4 (first register to be read)
  382. */
  383. collect_registers_armv7m(stack_ptr, get_cur_core_regset_address(), get_cur_fp_regset_address());
  384. /* Generate coredump using memory mode */
  385. mcd_faultdump(MCD_OUTPUT_FAULT_SERIAL);
  386. return 0;
  387. }