cm_backtrace.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * This file is part of the CmBacktrace Library.
  3. *
  4. * Copyright (c) 2016-2019, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: Initialize function and other general function.
  26. * Created on: 2016-12-15
  27. */
  28. #include <cm_backtrace.h>
  29. #include <stdbool.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #if __STDC_VERSION__ < 199901L
  33. #error "must be C99 or higher. try to add '-std=c99' to compile parameters"
  34. #endif
  35. #if defined(__CC_ARM) || defined(__CLANG_ARM)
  36. #define SECTION_START(_name_) _name_##$$Base
  37. #define SECTION_END(_name_) _name_##$$Limit
  38. #define IMAGE_SECTION_START(_name_) Image$$##_name_##$$Base
  39. #define IMAGE_SECTION_END(_name_) Image$$##_name_##$$Limit
  40. #define CSTACK_BLOCK_START(_name_) SECTION_START(_name_)
  41. #define CSTACK_BLOCK_END(_name_) SECTION_END(_name_)
  42. #define CODE_SECTION_START(_name_) IMAGE_SECTION_START(_name_)
  43. #define CODE_SECTION_END(_name_) IMAGE_SECTION_END(_name_)
  44. extern const int CSTACK_BLOCK_START(CMB_CSTACK_BLOCK_NAME);
  45. extern const int CSTACK_BLOCK_END(CMB_CSTACK_BLOCK_NAME);
  46. extern const int CODE_SECTION_START(CMB_CODE_SECTION_NAME);
  47. extern const int CODE_SECTION_END(CMB_CODE_SECTION_NAME);
  48. #elif defined(__ICCARM__)
  49. #pragma section=CMB_CSTACK_BLOCK_NAME
  50. #pragma section=CMB_CODE_SECTION_NAME
  51. #elif defined(__GNUC__)
  52. extern const int CMB_CSTACK_BLOCK_START;
  53. extern const int CMB_CSTACK_BLOCK_END;
  54. extern const int CMB_CODE_SECTION_START;
  55. extern const int CMB_CODE_SECTION_END;
  56. #else
  57. #error "not supported compiler"
  58. #endif
  59. enum {
  60. PRINT_MAIN_STACK_CFG_ERROR,
  61. PRINT_FIRMWARE_INFO,
  62. PRINT_ASSERT_ON_THREAD,
  63. PRINT_ASSERT_ON_HANDLER,
  64. PRINT_THREAD_STACK_INFO,
  65. PRINT_MAIN_STACK_INFO,
  66. PRINT_THREAD_STACK_OVERFLOW,
  67. PRINT_MAIN_STACK_OVERFLOW,
  68. PRINT_CALL_STACK_INFO,
  69. PRINT_CALL_STACK_ERR,
  70. PRINT_FAULT_ON_THREAD,
  71. PRINT_FAULT_ON_HANDLER,
  72. PRINT_REGS_TITLE,
  73. PRINT_HFSR_VECTBL,
  74. PRINT_MFSR_IACCVIOL,
  75. PRINT_MFSR_DACCVIOL,
  76. PRINT_MFSR_MUNSTKERR,
  77. PRINT_MFSR_MSTKERR,
  78. PRINT_MFSR_MLSPERR,
  79. PRINT_BFSR_IBUSERR,
  80. PRINT_BFSR_PRECISERR,
  81. PRINT_BFSR_IMPREISERR,
  82. PRINT_BFSR_UNSTKERR,
  83. PRINT_BFSR_STKERR,
  84. PRINT_BFSR_LSPERR,
  85. PRINT_UFSR_UNDEFINSTR,
  86. PRINT_UFSR_INVSTATE,
  87. PRINT_UFSR_INVPC,
  88. PRINT_UFSR_NOCP,
  89. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  90. PRINT_UFSR_STKOF,
  91. #endif
  92. PRINT_UFSR_UNALIGNED,
  93. PRINT_UFSR_DIVBYZERO0,
  94. PRINT_DFSR_HALTED,
  95. PRINT_DFSR_BKPT,
  96. PRINT_DFSR_DWTTRAP,
  97. PRINT_DFSR_VCATCH,
  98. PRINT_DFSR_EXTERNAL,
  99. PRINT_MMAR,
  100. PRINT_BFAR,
  101. };
  102. static const char * const print_info[] = {
  103. #if (CMB_PRINT_LANGUAGE == CMB_PRINT_LANGUAGE_ENGLISH)
  104. #include "Languages/en-US/cmb_en_US.h"
  105. #elif (CMB_PRINT_LANGUAGE == CMB_PRINT_LANGUAGE_CHINESE)
  106. #include "Languages/zh-CN/cmb_zh_CN.h"
  107. #elif (CMB_PRINT_LANGUAGE == CMB_PRINT_LANGUAGE_CHINESE_UTF8)
  108. #include "Languages/zh-CN/cmb_zh_CN_UTF8.h"
  109. #else
  110. #error "CMB_PRINT_LANGUAGE defined error in 'cmb_cfg.h'"
  111. #endif
  112. };
  113. static char fw_name[CMB_NAME_MAX] = {0};
  114. static char hw_ver[CMB_NAME_MAX] = {0};
  115. static char sw_ver[CMB_NAME_MAX] = {0};
  116. static uint32_t main_stack_start_addr = 0;
  117. static size_t main_stack_size = 0;
  118. static uint32_t code_start_addr = 0;
  119. static size_t code_size = 0;
  120. static bool init_ok = false;
  121. static char call_stack_info[CMB_CALL_STACK_MAX_DEPTH * (8 + 1)] = { 0 };
  122. static bool on_fault = false;
  123. static bool stack_is_overflow = false;
  124. static struct cmb_hard_fault_regs regs;
  125. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  126. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  127. static bool statck_has_fpu_regs = false;
  128. #endif
  129. static bool on_thread_before_fault = false;
  130. /**
  131. * library initialize
  132. */
  133. void cm_backtrace_init(const char *firmware_name, const char *hardware_ver, const char *software_ver) {
  134. strncpy(fw_name, firmware_name, CMB_NAME_MAX);
  135. strncpy(hw_ver, hardware_ver, CMB_NAME_MAX);
  136. strncpy(sw_ver, software_ver, CMB_NAME_MAX);
  137. #if defined(__CC_ARM) || defined(__CLANG_ARM)
  138. main_stack_start_addr = (uint32_t)&CSTACK_BLOCK_START(CMB_CSTACK_BLOCK_NAME);
  139. main_stack_size = (uint32_t)&CSTACK_BLOCK_END(CMB_CSTACK_BLOCK_NAME) - main_stack_start_addr;
  140. code_start_addr = (uint32_t)&CODE_SECTION_START(CMB_CODE_SECTION_NAME);
  141. code_size = (uint32_t)&CODE_SECTION_END(CMB_CODE_SECTION_NAME) - code_start_addr;
  142. #elif defined(__ICCARM__)
  143. main_stack_start_addr = (uint32_t)__section_begin(CMB_CSTACK_BLOCK_NAME);
  144. main_stack_size = (uint32_t)__section_end(CMB_CSTACK_BLOCK_NAME) - main_stack_start_addr;
  145. code_start_addr = (uint32_t)__section_begin(CMB_CODE_SECTION_NAME);
  146. code_size = (uint32_t)__section_end(CMB_CODE_SECTION_NAME) - code_start_addr;
  147. #elif defined(__GNUC__)
  148. main_stack_start_addr = (uint32_t)(&CMB_CSTACK_BLOCK_START);
  149. main_stack_size = (uint32_t)(&CMB_CSTACK_BLOCK_END) - main_stack_start_addr;
  150. code_start_addr = (uint32_t)(&CMB_CODE_SECTION_START);
  151. code_size = (uint32_t)(&CMB_CODE_SECTION_END) - code_start_addr;
  152. #else
  153. #error "not supported compiler"
  154. #endif
  155. if (main_stack_size == 0) {
  156. cmb_println(print_info[PRINT_MAIN_STACK_CFG_ERROR]);
  157. return;
  158. }
  159. init_ok = true;
  160. }
  161. /**
  162. * print firmware information, such as: firmware name, hardware version, software version
  163. */
  164. void cm_backtrace_firmware_info(void) {
  165. cmb_println(print_info[PRINT_FIRMWARE_INFO], fw_name, hw_ver, sw_ver);
  166. }
  167. #ifdef CMB_USING_OS_PLATFORM
  168. /**
  169. * Get current thread stack information
  170. *
  171. * @param sp stack current pointer
  172. * @param start_addr stack start address
  173. * @param size stack size
  174. */
  175. static void get_cur_thread_stack_info(uint32_t sp, uint32_t *start_addr, size_t *size) {
  176. CMB_ASSERT(start_addr);
  177. CMB_ASSERT(size);
  178. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  179. *start_addr = (uint32_t) rt_thread_self()->stack_addr;
  180. *size = rt_thread_self()->stack_size;
  181. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  182. extern OS_TCB *OSTCBCur;
  183. *start_addr = (uint32_t) OSTCBCur->OSTCBStkBottom;
  184. *size = OSTCBCur->OSTCBStkSize * sizeof(OS_STK);
  185. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  186. extern OS_TCB *OSTCBCurPtr;
  187. *start_addr = (uint32_t) OSTCBCurPtr->StkBasePtr;
  188. *size = OSTCBCurPtr->StkSize * sizeof(CPU_STK_SIZE);
  189. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  190. *start_addr = (uint32_t)vTaskStackAddr();
  191. *size = vTaskStackSize() * sizeof( StackType_t );
  192. #endif
  193. }
  194. /**
  195. * Get current thread name
  196. */
  197. static const char *get_cur_thread_name(void) {
  198. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  199. return rt_thread_self()->name;
  200. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  201. extern OS_TCB *OSTCBCur;
  202. #if OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0
  203. return (const char *)OSTCBCur->OSTCBTaskName;
  204. #else
  205. return NULL;
  206. #endif /* OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0 */
  207. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  208. extern OS_TCB *OSTCBCurPtr;
  209. return (const char *)OSTCBCurPtr->NamePtr;
  210. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  211. return vTaskName();
  212. #endif
  213. }
  214. #endif /* CMB_USING_OS_PLATFORM */
  215. #ifdef CMB_USING_DUMP_STACK_INFO
  216. /**
  217. * dump current stack information
  218. */
  219. static void dump_stack(uint32_t stack_start_addr, size_t stack_size, uint32_t *stack_pointer) {
  220. if (stack_is_overflow) {
  221. if (on_thread_before_fault) {
  222. cmb_println(print_info[PRINT_THREAD_STACK_OVERFLOW], stack_pointer);
  223. } else {
  224. cmb_println(print_info[PRINT_MAIN_STACK_OVERFLOW], stack_pointer);
  225. }
  226. if ((uint32_t) stack_pointer < stack_start_addr) {
  227. stack_pointer = (uint32_t *) stack_start_addr;
  228. } else if ((uint32_t) stack_pointer > stack_start_addr + stack_size) {
  229. stack_pointer = (uint32_t *) (stack_start_addr + stack_size);
  230. }
  231. }
  232. cmb_println(print_info[PRINT_THREAD_STACK_INFO]);
  233. for (; (uint32_t) stack_pointer < stack_start_addr + stack_size; stack_pointer++) {
  234. cmb_println(" addr: %08x data: %08x", stack_pointer, *stack_pointer);
  235. }
  236. cmb_println("====================================");
  237. }
  238. #endif /* CMB_USING_DUMP_STACK_INFO */
  239. /* check the disassembly instruction is 'BL' or 'BLX' */
  240. static bool disassembly_ins_is_bl_blx(uint32_t addr) {
  241. uint16_t ins1 = *((uint16_t *)addr);
  242. uint16_t ins2 = *((uint16_t *)(addr + 2));
  243. #define BL_INS_MASK 0xF800
  244. #define BL_INS_HIGH 0xF800
  245. #define BL_INS_LOW 0xF000
  246. #define BLX_INX_MASK 0xFF00
  247. #define BLX_INX 0x4700
  248. if ((ins2 & BL_INS_MASK) == BL_INS_HIGH && (ins1 & BL_INS_MASK) == BL_INS_LOW) {
  249. return true;
  250. } else if ((ins2 & BLX_INX_MASK) == BLX_INX) {
  251. return true;
  252. } else {
  253. return false;
  254. }
  255. }
  256. /**
  257. * backtrace function call stack
  258. *
  259. * @param buffer call stack buffer
  260. * @param size buffer size
  261. * @param sp stack pointer
  262. *
  263. * @return depth
  264. */
  265. size_t cm_backtrace_call_stack(uint32_t *buffer, size_t size, uint32_t sp) {
  266. uint32_t stack_start_addr = main_stack_start_addr, pc;
  267. size_t depth = 0, stack_size = main_stack_size;
  268. bool regs_saved_lr_is_valid = false;
  269. if (on_fault) {
  270. if (!stack_is_overflow) {
  271. /* first depth is PC */
  272. buffer[depth++] = regs.saved.pc;
  273. /* fix the LR address in thumb mode */
  274. pc = regs.saved.lr - 1;
  275. if ((pc >= code_start_addr) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  276. && (depth < size)) {
  277. buffer[depth++] = pc;
  278. regs_saved_lr_is_valid = true;
  279. }
  280. }
  281. #ifdef CMB_USING_OS_PLATFORM
  282. /* program is running on thread before fault */
  283. if (on_thread_before_fault) {
  284. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  285. }
  286. } else {
  287. /* OS environment */
  288. if (cmb_get_sp() == cmb_get_psp()) {
  289. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  290. }
  291. #endif /* CMB_USING_OS_PLATFORM */
  292. }
  293. if (stack_is_overflow) {
  294. if (sp < stack_start_addr) {
  295. sp = stack_start_addr;
  296. } else if (sp > stack_start_addr + stack_size) {
  297. sp = stack_start_addr + stack_size;
  298. }
  299. }
  300. /* copy called function address */
  301. for (; sp < stack_start_addr + stack_size; sp += sizeof(size_t)) {
  302. /* the *sp value may be LR, so need decrease a word to PC */
  303. pc = *((uint32_t *) sp) - sizeof(size_t);
  304. /* the Cortex-M using thumb instruction, so the pc must be an odd number */
  305. if (pc % 2 == 0) {
  306. continue;
  307. }
  308. /* fix the PC address in thumb mode */
  309. pc = *((uint32_t *) sp) - 1;
  310. if ((pc >= code_start_addr + sizeof(size_t)) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  311. /* check the the instruction before PC address is 'BL' or 'BLX' */
  312. && disassembly_ins_is_bl_blx(pc - sizeof(size_t)) && (depth < size)) {
  313. /* the second depth function may be already saved, so need ignore repeat */
  314. if ((depth == 2) && regs_saved_lr_is_valid && (pc == buffer[1])) {
  315. continue;
  316. }
  317. buffer[depth++] = pc;
  318. }
  319. }
  320. return depth;
  321. }
  322. /**
  323. * dump function call stack
  324. *
  325. * @param sp stack pointer
  326. */
  327. static void print_call_stack(uint32_t sp) {
  328. size_t i, cur_depth = 0;
  329. uint32_t call_stack_buf[CMB_CALL_STACK_MAX_DEPTH] = {0};
  330. cur_depth = cm_backtrace_call_stack(call_stack_buf, CMB_CALL_STACK_MAX_DEPTH, sp);
  331. for (i = 0; i < cur_depth; i++) {
  332. sprintf(call_stack_info + i * (8 + 1), "%08lx", (unsigned long)call_stack_buf[i]);
  333. call_stack_info[i * (8 + 1) + 8] = ' ';
  334. }
  335. if (cur_depth) {
  336. cmb_println(print_info[PRINT_CALL_STACK_INFO], fw_name, CMB_ELF_FILE_EXTENSION_NAME, cur_depth * (8 + 1),
  337. call_stack_info);
  338. } else {
  339. cmb_println(print_info[PRINT_CALL_STACK_ERR]);
  340. }
  341. }
  342. /**
  343. * backtrace for assert
  344. *
  345. * @param sp the stack pointer when on assert occurred
  346. */
  347. void cm_backtrace_assert(uint32_t sp) {
  348. CMB_ASSERT(init_ok);
  349. #ifdef CMB_USING_OS_PLATFORM
  350. uint32_t cur_stack_pointer = cmb_get_sp();
  351. #endif
  352. cmb_println("");
  353. cm_backtrace_firmware_info();
  354. #ifdef CMB_USING_OS_PLATFORM
  355. /* OS environment */
  356. if (cur_stack_pointer == cmb_get_msp()) {
  357. cmb_println(print_info[PRINT_ASSERT_ON_HANDLER]);
  358. #ifdef CMB_USING_DUMP_STACK_INFO
  359. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  360. #endif /* CMB_USING_DUMP_STACK_INFO */
  361. } else if (cur_stack_pointer == cmb_get_psp()) {
  362. cmb_println(print_info[PRINT_ASSERT_ON_THREAD], get_cur_thread_name());
  363. #ifdef CMB_USING_DUMP_STACK_INFO
  364. uint32_t stack_start_addr;
  365. size_t stack_size;
  366. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  367. dump_stack(stack_start_addr, stack_size, (uint32_t *) sp);
  368. #endif /* CMB_USING_DUMP_STACK_INFO */
  369. }
  370. #else
  371. /* bare metal(no OS) environment */
  372. #ifdef CMB_USING_DUMP_STACK_INFO
  373. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  374. #endif /* CMB_USING_DUMP_STACK_INFO */
  375. #endif /* CMB_USING_OS_PLATFORM */
  376. print_call_stack(sp);
  377. }
  378. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  379. /**
  380. * fault diagnosis then print cause of fault
  381. */
  382. static void fault_diagnosis(void) {
  383. if (regs.hfsr.bits.VECTBL) {
  384. cmb_println(print_info[PRINT_HFSR_VECTBL]);
  385. }
  386. if (regs.hfsr.bits.FORCED) {
  387. /* Memory Management Fault */
  388. if (regs.mfsr.value) {
  389. if (regs.mfsr.bits.IACCVIOL) {
  390. cmb_println(print_info[PRINT_MFSR_IACCVIOL]);
  391. }
  392. if (regs.mfsr.bits.DACCVIOL) {
  393. cmb_println(print_info[PRINT_MFSR_DACCVIOL]);
  394. }
  395. if (regs.mfsr.bits.MUNSTKERR) {
  396. cmb_println(print_info[PRINT_MFSR_MUNSTKERR]);
  397. }
  398. if (regs.mfsr.bits.MSTKERR) {
  399. cmb_println(print_info[PRINT_MFSR_MSTKERR]);
  400. }
  401. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  402. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  403. if (regs.mfsr.bits.MLSPERR) {
  404. cmb_println(print_info[PRINT_MFSR_MLSPERR]);
  405. }
  406. #endif
  407. if (regs.mfsr.bits.MMARVALID) {
  408. if (regs.mfsr.bits.IACCVIOL || regs.mfsr.bits.DACCVIOL) {
  409. cmb_println(print_info[PRINT_MMAR], regs.mmar);
  410. }
  411. }
  412. }
  413. /* Bus Fault */
  414. if (regs.bfsr.value) {
  415. if (regs.bfsr.bits.IBUSERR) {
  416. cmb_println(print_info[PRINT_BFSR_IBUSERR]);
  417. }
  418. if (regs.bfsr.bits.PRECISERR) {
  419. cmb_println(print_info[PRINT_BFSR_PRECISERR]);
  420. }
  421. if (regs.bfsr.bits.IMPREISERR) {
  422. cmb_println(print_info[PRINT_BFSR_IMPREISERR]);
  423. }
  424. if (regs.bfsr.bits.UNSTKERR) {
  425. cmb_println(print_info[PRINT_BFSR_UNSTKERR]);
  426. }
  427. if (regs.bfsr.bits.STKERR) {
  428. cmb_println(print_info[PRINT_BFSR_STKERR]);
  429. }
  430. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  431. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  432. if (regs.bfsr.bits.LSPERR) {
  433. cmb_println(print_info[PRINT_BFSR_LSPERR]);
  434. }
  435. #endif
  436. if (regs.bfsr.bits.BFARVALID) {
  437. if (regs.bfsr.bits.PRECISERR) {
  438. cmb_println(print_info[PRINT_BFAR], regs.bfar);
  439. }
  440. }
  441. }
  442. /* Usage Fault */
  443. if (regs.ufsr.value) {
  444. if (regs.ufsr.bits.UNDEFINSTR) {
  445. cmb_println(print_info[PRINT_UFSR_UNDEFINSTR]);
  446. }
  447. if (regs.ufsr.bits.INVSTATE) {
  448. cmb_println(print_info[PRINT_UFSR_INVSTATE]);
  449. }
  450. if (regs.ufsr.bits.INVPC) {
  451. cmb_println(print_info[PRINT_UFSR_INVPC]);
  452. }
  453. if (regs.ufsr.bits.NOCP) {
  454. cmb_println(print_info[PRINT_UFSR_NOCP]);
  455. }
  456. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  457. if (regs.ufsr.bits.STKOF) {
  458. cmb_println(print_info[PRINT_UFSR_STKOF]);
  459. }
  460. #endif
  461. if (regs.ufsr.bits.UNALIGNED) {
  462. cmb_println(print_info[PRINT_UFSR_UNALIGNED]);
  463. }
  464. if (regs.ufsr.bits.DIVBYZERO0) {
  465. cmb_println(print_info[PRINT_UFSR_DIVBYZERO0]);
  466. }
  467. }
  468. }
  469. /* Debug Fault */
  470. if (regs.hfsr.bits.DEBUGEVT) {
  471. if (regs.dfsr.value) {
  472. if (regs.dfsr.bits.HALTED) {
  473. cmb_println(print_info[PRINT_DFSR_HALTED]);
  474. }
  475. if (regs.dfsr.bits.BKPT) {
  476. cmb_println(print_info[PRINT_DFSR_BKPT]);
  477. }
  478. if (regs.dfsr.bits.DWTTRAP) {
  479. cmb_println(print_info[PRINT_DFSR_DWTTRAP]);
  480. }
  481. if (regs.dfsr.bits.VCATCH) {
  482. cmb_println(print_info[PRINT_DFSR_VCATCH]);
  483. }
  484. if (regs.dfsr.bits.EXTERNAL) {
  485. cmb_println(print_info[PRINT_DFSR_EXTERNAL]);
  486. }
  487. }
  488. }
  489. }
  490. #endif /* (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0) */
  491. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  492. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  493. static uint32_t statck_del_fpu_regs(uint32_t fault_handler_lr, uint32_t sp) {
  494. statck_has_fpu_regs = (fault_handler_lr & (1UL << 4)) == 0 ? true : false;
  495. /* the stack has S0~S15 and FPSCR registers when statck_has_fpu_regs is true, double word align */
  496. return statck_has_fpu_regs == true ? sp + sizeof(size_t) * 18 : sp;
  497. }
  498. #endif
  499. /**
  500. * backtrace for fault
  501. * @note only call once
  502. *
  503. * @param fault_handler_lr the LR register value on fault handler
  504. * @param fault_handler_sp the stack pointer on fault handler
  505. */
  506. void cm_backtrace_fault(uint32_t fault_handler_lr, uint32_t fault_handler_sp) {
  507. uint32_t stack_pointer = fault_handler_sp, saved_regs_addr = stack_pointer;
  508. const char *regs_name[] = { "R0 ", "R1 ", "R2 ", "R3 ", "R12", "LR ", "PC ", "PSR" };
  509. #ifdef CMB_USING_DUMP_STACK_INFO
  510. uint32_t stack_start_addr = main_stack_start_addr;
  511. size_t stack_size = main_stack_size;
  512. #endif
  513. CMB_ASSERT(init_ok);
  514. /* only call once */
  515. CMB_ASSERT(!on_fault);
  516. on_fault = true;
  517. cmb_println("");
  518. cm_backtrace_firmware_info();
  519. #ifdef CMB_USING_OS_PLATFORM
  520. on_thread_before_fault = fault_handler_lr & (1UL << 2);
  521. /* check which stack was used before (MSP or PSP) */
  522. if (on_thread_before_fault) {
  523. cmb_println(print_info[PRINT_FAULT_ON_THREAD], get_cur_thread_name() != NULL ? get_cur_thread_name() : "NO_NAME");
  524. saved_regs_addr = stack_pointer = cmb_get_psp();
  525. #ifdef CMB_USING_DUMP_STACK_INFO
  526. get_cur_thread_stack_info(stack_pointer, &stack_start_addr, &stack_size);
  527. #endif /* CMB_USING_DUMP_STACK_INFO */
  528. } else {
  529. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  530. }
  531. #else
  532. /* bare metal(no OS) environment */
  533. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  534. #endif /* CMB_USING_OS_PLATFORM */
  535. /* delete saved R0~R3, R12, LR,PC,xPSR registers space */
  536. stack_pointer += sizeof(size_t) * 8;
  537. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  538. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  539. stack_pointer = statck_del_fpu_regs(fault_handler_lr, stack_pointer);
  540. #endif /* (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) */
  541. #ifdef CMB_USING_DUMP_STACK_INFO
  542. /* check stack overflow */
  543. if (stack_pointer < stack_start_addr || stack_pointer > stack_start_addr + stack_size) {
  544. stack_is_overflow = true;
  545. }
  546. /* dump stack information */
  547. dump_stack(stack_start_addr, stack_size, (uint32_t *) stack_pointer);
  548. #endif /* CMB_USING_DUMP_STACK_INFO */
  549. /* the stack frame may be get failed when it is overflow */
  550. if (!stack_is_overflow) {
  551. /* dump register */
  552. cmb_println(print_info[PRINT_REGS_TITLE]);
  553. regs.saved.r0 = ((uint32_t *)saved_regs_addr)[0]; // Register R0
  554. regs.saved.r1 = ((uint32_t *)saved_regs_addr)[1]; // Register R1
  555. regs.saved.r2 = ((uint32_t *)saved_regs_addr)[2]; // Register R2
  556. regs.saved.r3 = ((uint32_t *)saved_regs_addr)[3]; // Register R3
  557. regs.saved.r12 = ((uint32_t *)saved_regs_addr)[4]; // Register R12
  558. regs.saved.lr = ((uint32_t *)saved_regs_addr)[5]; // Link register LR
  559. regs.saved.pc = ((uint32_t *)saved_regs_addr)[6]; // Program counter PC
  560. regs.saved.psr.value = ((uint32_t *)saved_regs_addr)[7]; // Program status word PSR
  561. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[0], regs.saved.r0,
  562. regs_name[1], regs.saved.r1,
  563. regs_name[2], regs.saved.r2,
  564. regs_name[3], regs.saved.r3);
  565. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[4], regs.saved.r12,
  566. regs_name[5], regs.saved.lr,
  567. regs_name[6], regs.saved.pc,
  568. regs_name[7], regs.saved.psr.value);
  569. cmb_println("==============================================================");
  570. }
  571. /* the Cortex-M0 is not support fault diagnosis */
  572. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  573. regs.syshndctrl.value = CMB_SYSHND_CTRL; // System Handler Control and State Register
  574. regs.mfsr.value = CMB_NVIC_MFSR; // Memory Fault Status Register
  575. regs.mmar = CMB_NVIC_MMAR; // Memory Management Fault Address Register
  576. regs.bfsr.value = CMB_NVIC_BFSR; // Bus Fault Status Register
  577. regs.bfar = CMB_NVIC_BFAR; // Bus Fault Manage Address Register
  578. regs.ufsr.value = CMB_NVIC_UFSR; // Usage Fault Status Register
  579. regs.hfsr.value = CMB_NVIC_HFSR; // Hard Fault Status Register
  580. regs.dfsr.value = CMB_NVIC_DFSR; // Debug Fault Status Register
  581. regs.afsr = CMB_NVIC_AFSR; // Auxiliary Fault Status Register
  582. fault_diagnosis();
  583. #endif
  584. print_call_stack(stack_pointer);
  585. }