Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

armv7m.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. void mcd_mini_dump(void)
  133. {
  134. struct thread_info_ops ops;
  135. mcd_get_regset((uint32_t *)get_cur_core_regset_address());
  136. #if MCD_FPU_SUPPORT
  137. if (is_vfp_addressable())
  138. mcd_get_fpregset((uint32_t *)get_cur_fp_regset_address());
  139. #endif
  140. mcd_mini_dump_ops(&ops);
  141. mcd_gen_coredump(&ops);
  142. }
  143. void mcd_multi_dump(void)
  144. {
  145. struct thread_info_ops ops;
  146. mcd_get_regset((uint32_t *)get_cur_core_regset_address());
  147. #if MCD_FPU_SUPPORT
  148. if (is_vfp_addressable())
  149. mcd_get_fpregset((uint32_t *)get_cur_fp_regset_address());
  150. #endif
  151. mcd_rtos_thread_ops(&ops);
  152. mcd_gen_coredump(&ops);
  153. }
  154. #endif
  155. /**
  156. * @brief Collect ARM Cortex-M4 registers from RT-Thread stack frame
  157. *
  158. * This function extracts register values from the stack frame created by
  159. * RT-Thread's context switch mechanism (PendSV_Handler) or exception handling.
  160. *
  161. * RT-Thread Stack Frame Layout (from low to high memory address):
  162. * +-------------------+ <- stack_top (input parameter)
  163. * | FPU flag | (4 bytes, if MCD_FPU_SUPPORT enabled)
  164. * +-------------------+
  165. * | r4 | (4 bytes, software saved)
  166. * | r5 | (4 bytes, software saved)
  167. * | r6 | (4 bytes, software saved)
  168. * | r7 | (4 bytes, software saved)
  169. * | r8 | (4 bytes, software saved)
  170. * | r9 | (4 bytes, software saved)
  171. * | r10 | (4 bytes, software saved)
  172. * | r11 | (4 bytes, software saved)
  173. * +-------------------+
  174. * | FPU s16-s31 | (64 bytes, if FPU context active)
  175. * +-------------------+
  176. * | r0 | (4 bytes, hardware saved)
  177. * | r1 | (4 bytes, hardware saved)
  178. * | r2 | (4 bytes, hardware saved)
  179. * | r3 | (4 bytes, hardware saved)
  180. * | r12 | (4 bytes, hardware saved)
  181. * | lr | (4 bytes, hardware saved)
  182. * | pc | (4 bytes, hardware saved)
  183. * | xpsr | (4 bytes, hardware saved)
  184. * +-------------------+
  185. * | FPU s0-s15 | (64 bytes, if FPU context active)
  186. * | FPSCR | (4 bytes, if FPU context active)
  187. * | NO_NAME | (4 bytes, if FPU context active)
  188. * +-------------------+ <- current SP after context save
  189. *
  190. * @param stack_top Pointer to the beginning of the stack frame (FPU flag position)
  191. * @param core_regset Pointer to structure for storing ARM core registers
  192. * @param fp_regset Pointer to structure for storing FPU registers
  193. */
  194. void collect_registers_armv7m(uint32_t *stack_top,
  195. core_regset_type *core_regset,
  196. fp_regset_type *fp_regset)
  197. {
  198. /*
  199. * This function uses the same stack frame parsing approach as collect_registers_armv7ms
  200. * for consistency. Both PendSV_Handler and HardFault_Handler now use identical
  201. * stacking order after the modifications.
  202. *
  203. * Expected stack layout starting from stack_top:
  204. * [FPU flag] -> [r4-r11] -> [FPU s16-s31] -> [exception frame] -> [FPU s0-s15,FPSCR]
  205. */
  206. uint32_t *current_ptr = stack_top;
  207. /* Clear both register sets first to ensure clean state */
  208. mcd_memset(core_regset, 0, sizeof(core_regset_type));
  209. mcd_memset(fp_regset, 0, sizeof(fp_regset_type));
  210. #if MCD_FPU_SUPPORT
  211. /* Read FPU flag first - indicates if FPU context was saved */
  212. uint32_t fpu_flag = *current_ptr++;
  213. #endif
  214. /* Extract core registers r4-r11 (software saved by RT-Thread) */
  215. core_regset->r4 = *current_ptr++;
  216. core_regset->r5 = *current_ptr++;
  217. core_regset->r6 = *current_ptr++;
  218. core_regset->r7 = *current_ptr++;
  219. core_regset->r8 = *current_ptr++;
  220. core_regset->r9 = *current_ptr++;
  221. core_regset->r10 = *current_ptr++;
  222. core_regset->r11 = *current_ptr++;
  223. #if MCD_FPU_SUPPORT
  224. /* If FPU context is active, s16-s31 registers are saved after r4-r11 */
  225. if (fpu_flag)
  226. {
  227. /* Copy FPU s16-s31 registers (software saved by RT-Thread) */
  228. for (int i = 16; i < 32; i++)
  229. {
  230. ((uint32_t *)fp_regset)[i] = *current_ptr++;
  231. }
  232. }
  233. #endif
  234. /* Extract hardware exception frame (automatically saved by ARM Cortex-M) */
  235. core_regset->r0 = *current_ptr++;
  236. core_regset->r1 = *current_ptr++;
  237. core_regset->r2 = *current_ptr++;
  238. core_regset->r3 = *current_ptr++;
  239. core_regset->r12 = *current_ptr++;
  240. core_regset->lr = *current_ptr++;
  241. core_regset->pc = *current_ptr++;
  242. core_regset->xpsr = *current_ptr++;
  243. #if MCD_FPU_SUPPORT
  244. /* If FPU context is active, s0-s15 and FPSCR are saved after exception frame */
  245. if (fpu_flag)
  246. {
  247. /* Copy FPU s0-s15 registers (hardware saved by ARM Cortex-M) */
  248. for (int i = 0; i < 16; i++)
  249. {
  250. ((uint32_t *)fp_regset)[i] = *current_ptr++;
  251. }
  252. /* Copy FPSCR register (FPU status and control) */
  253. fp_regset->fpscr = *current_ptr++;
  254. /* Skip NO_NAME field (reserved/alignment) */
  255. current_ptr++;
  256. }
  257. #endif
  258. /* SP should point to the current stack pointer position after all saved data */
  259. core_regset->sp = (uintptr_t)current_ptr;
  260. }
  261. /**
  262. * @brief ARM Cortex-M specific hard fault exception handler for MCoreDump
  263. *
  264. * This function handles ARM Cortex-M specific stack frame processing when a
  265. * hard fault occurs. It calculates the proper stack pointer position and
  266. * extracts register context for coredump generation.
  267. *
  268. * HardFault Stack Frame Layout (created by HardFault_Handler):
  269. * +-------------------+ <- Exception occurs here
  270. * | Hardware Exception| (32 bytes: r0,r1,r2,r3,r12,lr,pc,xpsr)
  271. * | Stack Frame | (+ optional 72 bytes FPU: s0-s15,FPSCR,NO_NAME)
  272. * +-------------------+ <- context parameter points here
  273. * | r11 | (4 bytes, software saved in HardFault_Handler)
  274. * | r10 | (4 bytes, software saved in HardFault_Handler)
  275. * | ... | (...)
  276. * | r4 | (4 bytes, software saved in HardFault_Handler)
  277. * +-------------------+
  278. * | FPU s31 | (4 bytes, if FPU context active)
  279. * | FPU s30 | (4 bytes, if FPU context active)
  280. * | ... | (...)
  281. * | FPU s16 | (4 bytes, if FPU context active)
  282. * +-------------------+
  283. * | FPU flag | (4 bytes, if MCD_FPU_SUPPORT enabled)
  284. * +-------------------+
  285. * | EXC_RETURN | (4 bytes, contains exception return information)
  286. * +-------------------+ <- Final stack pointer position
  287. *
  288. * @param context Pointer to exception_stack_frame from HardFault_Handler
  289. * @return int Always returns -1 to indicate fault condition
  290. */
  291. int armv7m_hard_fault_exception_hook(void *context)
  292. {
  293. /*
  294. * context points to exception_stack_frame created by HardFault_Handler.
  295. * We need to calculate the complete stack frame position to extract all registers.
  296. * Since HardFault_Handler now uses the same stacking order as PendSV_Handler,
  297. * we can directly use collect_registers_armv7m function.
  298. */
  299. struct exception_stack_frame *exception_stack = (struct exception_stack_frame *)context;
  300. /* Calculate stack pointer to the beginning of the saved context */
  301. uint32_t *stack_ptr = (uint32_t *)exception_stack;
  302. #ifdef RT_USING_FINSH
  303. extern long list_thread(void);
  304. list_thread();
  305. #endif
  306. /*
  307. * Move backward through the stack to reach the beginning of saved context.
  308. * Stack layout (working backwards from exception_stack):
  309. * exception_stack -> r4-r11 (8 words) -> [s16-s31] -> [fpu_flag] -> [exc_return]
  310. */
  311. stack_ptr -= 8; /* Move backward 8 uint32_t positions to reach r4 */
  312. #if MCD_FPU_SUPPORT
  313. /* Point to FPU flag position (collect_registers_armv7m expects this as start) */
  314. stack_ptr -= 1; /* fpu flag */
  315. #else
  316. /* If no FPU support, skip EXC_RETURN and point to r4 directly */
  317. stack_ptr -= 1; /* exc_return */
  318. #endif
  319. /*
  320. * Now stack_ptr points to where collect_registers_armv7m expects:
  321. * - With FPU: points to FPU flag (first field to be read)
  322. * - Without FPU: points to r4 (first register to be read)
  323. */
  324. collect_registers_armv7m(stack_ptr,
  325. get_cur_core_regset_address(),
  326. get_cur_fp_regset_address());
  327. /* Generate coredump using memory mode */
  328. mcd_faultdump(MCD_OUTPUT_MEMORY);
  329. return 0;
  330. }