cm_backtrace.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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(__ARMCC_VERSION)
  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(__ARMCC_VERSION)
  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. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTX5)
  193. osRtxThread_t *thread = osRtxInfo.thread.run.curr;
  194. *start_addr = (uint32_t)thread->stack_mem;
  195. *size = thread->stack_size;
  196. #endif
  197. }
  198. /**
  199. * Get current thread name
  200. */
  201. static const char *get_cur_thread_name(void) {
  202. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  203. return rt_thread_self()->name;
  204. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  205. extern OS_TCB *OSTCBCur;
  206. #if OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0
  207. return (const char *)OSTCBCur->OSTCBTaskName;
  208. #else
  209. return NULL;
  210. #endif /* OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0 */
  211. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  212. extern OS_TCB *OSTCBCurPtr;
  213. return (const char *)OSTCBCurPtr->NamePtr;
  214. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  215. return vTaskName();
  216. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTX5)
  217. osThreadId_t id = osThreadGetId();
  218. return osThreadGetName(id);
  219. #endif
  220. }
  221. #endif /* CMB_USING_OS_PLATFORM */
  222. #ifdef CMB_USING_DUMP_STACK_INFO
  223. /**
  224. * dump current stack information
  225. */
  226. static void dump_stack(uint32_t stack_start_addr, size_t stack_size, uint32_t *stack_pointer) {
  227. if (stack_is_overflow) {
  228. if (on_thread_before_fault) {
  229. cmb_println(print_info[PRINT_THREAD_STACK_OVERFLOW], stack_pointer);
  230. } else {
  231. cmb_println(print_info[PRINT_MAIN_STACK_OVERFLOW], stack_pointer);
  232. }
  233. if ((uint32_t) stack_pointer < stack_start_addr) {
  234. stack_pointer = (uint32_t *) stack_start_addr;
  235. } else if ((uint32_t) stack_pointer > stack_start_addr + stack_size) {
  236. stack_pointer = (uint32_t *) (stack_start_addr + stack_size);
  237. }
  238. }
  239. cmb_println(print_info[PRINT_THREAD_STACK_INFO]);
  240. for (; (uint32_t) stack_pointer < stack_start_addr + stack_size; stack_pointer++) {
  241. cmb_println(" addr: %08x data: %08x", stack_pointer, *stack_pointer);
  242. }
  243. cmb_println("====================================");
  244. }
  245. #endif /* CMB_USING_DUMP_STACK_INFO */
  246. /* check the disassembly instruction is 'BL' or 'BLX' */
  247. static bool disassembly_ins_is_bl_blx(uint32_t addr) {
  248. uint16_t ins1 = *((uint16_t *)addr);
  249. uint16_t ins2 = *((uint16_t *)(addr + 2));
  250. #define BL_INS_MASK 0xF800
  251. #define BL_INS_HIGH 0xF800
  252. #define BL_INS_LOW 0xF000
  253. #define BLX_INX_MASK 0xFF00
  254. #define BLX_INX 0x4700
  255. if ((ins2 & BL_INS_MASK) == BL_INS_HIGH && (ins1 & BL_INS_MASK) == BL_INS_LOW) {
  256. return true;
  257. } else if ((ins2 & BLX_INX_MASK) == BLX_INX) {
  258. return true;
  259. } else {
  260. return false;
  261. }
  262. }
  263. /**
  264. * backtrace function call stack
  265. *
  266. * @param buffer call stack buffer
  267. * @param size buffer size
  268. * @param sp stack pointer
  269. *
  270. * @return depth
  271. */
  272. size_t cm_backtrace_call_stack(uint32_t *buffer, size_t size, uint32_t sp) {
  273. uint32_t stack_start_addr = main_stack_start_addr, pc;
  274. size_t depth = 0, stack_size = main_stack_size;
  275. bool regs_saved_lr_is_valid = false;
  276. if (on_fault) {
  277. if (!stack_is_overflow) {
  278. /* first depth is PC */
  279. buffer[depth++] = regs.saved.pc;
  280. /* fix the LR address in thumb mode */
  281. pc = regs.saved.lr - 1;
  282. if ((pc >= code_start_addr) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  283. && (depth < size)) {
  284. buffer[depth++] = pc;
  285. regs_saved_lr_is_valid = true;
  286. }
  287. }
  288. #ifdef CMB_USING_OS_PLATFORM
  289. /* program is running on thread before fault */
  290. if (on_thread_before_fault) {
  291. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  292. }
  293. } else {
  294. /* OS environment */
  295. if (cmb_get_sp() == cmb_get_psp()) {
  296. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  297. }
  298. #endif /* CMB_USING_OS_PLATFORM */
  299. }
  300. if (stack_is_overflow) {
  301. if (sp < stack_start_addr) {
  302. sp = stack_start_addr;
  303. } else if (sp > stack_start_addr + stack_size) {
  304. sp = stack_start_addr + stack_size;
  305. }
  306. }
  307. /* copy called function address */
  308. for (; sp < stack_start_addr + stack_size; sp += sizeof(size_t)) {
  309. /* the *sp value may be LR, so need decrease a word to PC */
  310. pc = *((uint32_t *) sp) - sizeof(size_t);
  311. /* the Cortex-M using thumb instruction, so the pc must be an odd number */
  312. if (pc % 2 == 0) {
  313. continue;
  314. }
  315. /* fix the PC address in thumb mode */
  316. pc = *((uint32_t *) sp) - 1;
  317. if ((pc >= code_start_addr + sizeof(size_t)) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  318. /* check the the instruction before PC address is 'BL' or 'BLX' */
  319. && disassembly_ins_is_bl_blx(pc - sizeof(size_t)) && (depth < size)) {
  320. /* the second depth function may be already saved, so need ignore repeat */
  321. if ((depth == 2) && regs_saved_lr_is_valid && (pc == buffer[1])) {
  322. continue;
  323. }
  324. buffer[depth++] = pc;
  325. }
  326. }
  327. return depth;
  328. }
  329. /**
  330. * dump function call stack
  331. *
  332. * @param sp stack pointer
  333. */
  334. static void print_call_stack(uint32_t sp) {
  335. size_t i, cur_depth = 0;
  336. uint32_t call_stack_buf[CMB_CALL_STACK_MAX_DEPTH] = {0};
  337. cur_depth = cm_backtrace_call_stack(call_stack_buf, CMB_CALL_STACK_MAX_DEPTH, sp);
  338. for (i = 0; i < cur_depth; i++) {
  339. sprintf(call_stack_info + i * (8 + 1), "%08lx", (unsigned long)call_stack_buf[i]);
  340. call_stack_info[i * (8 + 1) + 8] = ' ';
  341. }
  342. if (cur_depth) {
  343. call_stack_info[cur_depth * (8 + 1) - 1] = '\0';
  344. cmb_println(print_info[PRINT_CALL_STACK_INFO], fw_name, CMB_ELF_FILE_EXTENSION_NAME, call_stack_info);
  345. } else {
  346. cmb_println(print_info[PRINT_CALL_STACK_ERR]);
  347. }
  348. }
  349. /**
  350. * backtrace for assert
  351. *
  352. * @param sp the stack pointer when on assert occurred
  353. */
  354. void cm_backtrace_assert(uint32_t sp) {
  355. CMB_ASSERT(init_ok);
  356. #ifdef CMB_USING_OS_PLATFORM
  357. uint32_t cur_stack_pointer = cmb_get_sp();
  358. #endif
  359. cmb_println("");
  360. cm_backtrace_firmware_info();
  361. #ifdef CMB_USING_OS_PLATFORM
  362. /* OS environment */
  363. if (cur_stack_pointer == cmb_get_msp()) {
  364. cmb_println(print_info[PRINT_ASSERT_ON_HANDLER]);
  365. #ifdef CMB_USING_DUMP_STACK_INFO
  366. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  367. #endif /* CMB_USING_DUMP_STACK_INFO */
  368. } else if (cur_stack_pointer == cmb_get_psp()) {
  369. cmb_println(print_info[PRINT_ASSERT_ON_THREAD], get_cur_thread_name());
  370. #ifdef CMB_USING_DUMP_STACK_INFO
  371. uint32_t stack_start_addr;
  372. size_t stack_size;
  373. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  374. dump_stack(stack_start_addr, stack_size, (uint32_t *) sp);
  375. #endif /* CMB_USING_DUMP_STACK_INFO */
  376. }
  377. #else
  378. /* bare metal(no OS) environment */
  379. #ifdef CMB_USING_DUMP_STACK_INFO
  380. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  381. #endif /* CMB_USING_DUMP_STACK_INFO */
  382. #endif /* CMB_USING_OS_PLATFORM */
  383. print_call_stack(sp);
  384. }
  385. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  386. /**
  387. * fault diagnosis then print cause of fault
  388. */
  389. static void fault_diagnosis(void) {
  390. if (regs.hfsr.bits.VECTBL) {
  391. cmb_println(print_info[PRINT_HFSR_VECTBL]);
  392. }
  393. if (regs.hfsr.bits.FORCED) {
  394. /* Memory Management Fault */
  395. if (regs.mfsr.value) {
  396. if (regs.mfsr.bits.IACCVIOL) {
  397. cmb_println(print_info[PRINT_MFSR_IACCVIOL]);
  398. }
  399. if (regs.mfsr.bits.DACCVIOL) {
  400. cmb_println(print_info[PRINT_MFSR_DACCVIOL]);
  401. }
  402. if (regs.mfsr.bits.MUNSTKERR) {
  403. cmb_println(print_info[PRINT_MFSR_MUNSTKERR]);
  404. }
  405. if (regs.mfsr.bits.MSTKERR) {
  406. cmb_println(print_info[PRINT_MFSR_MSTKERR]);
  407. }
  408. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  409. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  410. if (regs.mfsr.bits.MLSPERR) {
  411. cmb_println(print_info[PRINT_MFSR_MLSPERR]);
  412. }
  413. #endif
  414. if (regs.mfsr.bits.MMARVALID) {
  415. if (regs.mfsr.bits.IACCVIOL || regs.mfsr.bits.DACCVIOL) {
  416. cmb_println(print_info[PRINT_MMAR], regs.mmar);
  417. }
  418. }
  419. }
  420. /* Bus Fault */
  421. if (regs.bfsr.value) {
  422. if (regs.bfsr.bits.IBUSERR) {
  423. cmb_println(print_info[PRINT_BFSR_IBUSERR]);
  424. }
  425. if (regs.bfsr.bits.PRECISERR) {
  426. cmb_println(print_info[PRINT_BFSR_PRECISERR]);
  427. }
  428. if (regs.bfsr.bits.IMPREISERR) {
  429. cmb_println(print_info[PRINT_BFSR_IMPREISERR]);
  430. }
  431. if (regs.bfsr.bits.UNSTKERR) {
  432. cmb_println(print_info[PRINT_BFSR_UNSTKERR]);
  433. }
  434. if (regs.bfsr.bits.STKERR) {
  435. cmb_println(print_info[PRINT_BFSR_STKERR]);
  436. }
  437. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  438. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  439. if (regs.bfsr.bits.LSPERR) {
  440. cmb_println(print_info[PRINT_BFSR_LSPERR]);
  441. }
  442. #endif
  443. if (regs.bfsr.bits.BFARVALID) {
  444. if (regs.bfsr.bits.PRECISERR) {
  445. cmb_println(print_info[PRINT_BFAR], regs.bfar);
  446. }
  447. }
  448. }
  449. /* Usage Fault */
  450. if (regs.ufsr.value) {
  451. if (regs.ufsr.bits.UNDEFINSTR) {
  452. cmb_println(print_info[PRINT_UFSR_UNDEFINSTR]);
  453. }
  454. if (regs.ufsr.bits.INVSTATE) {
  455. cmb_println(print_info[PRINT_UFSR_INVSTATE]);
  456. }
  457. if (regs.ufsr.bits.INVPC) {
  458. cmb_println(print_info[PRINT_UFSR_INVPC]);
  459. }
  460. if (regs.ufsr.bits.NOCP) {
  461. cmb_println(print_info[PRINT_UFSR_NOCP]);
  462. }
  463. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  464. if (regs.ufsr.bits.STKOF) {
  465. cmb_println(print_info[PRINT_UFSR_STKOF]);
  466. }
  467. #endif
  468. if (regs.ufsr.bits.UNALIGNED) {
  469. cmb_println(print_info[PRINT_UFSR_UNALIGNED]);
  470. }
  471. if (regs.ufsr.bits.DIVBYZERO0) {
  472. cmb_println(print_info[PRINT_UFSR_DIVBYZERO0]);
  473. }
  474. }
  475. }
  476. /* Debug Fault */
  477. if (regs.hfsr.bits.DEBUGEVT) {
  478. if (regs.dfsr.value) {
  479. if (regs.dfsr.bits.HALTED) {
  480. cmb_println(print_info[PRINT_DFSR_HALTED]);
  481. }
  482. if (regs.dfsr.bits.BKPT) {
  483. cmb_println(print_info[PRINT_DFSR_BKPT]);
  484. }
  485. if (regs.dfsr.bits.DWTTRAP) {
  486. cmb_println(print_info[PRINT_DFSR_DWTTRAP]);
  487. }
  488. if (regs.dfsr.bits.VCATCH) {
  489. cmb_println(print_info[PRINT_DFSR_VCATCH]);
  490. }
  491. if (regs.dfsr.bits.EXTERNAL) {
  492. cmb_println(print_info[PRINT_DFSR_EXTERNAL]);
  493. }
  494. }
  495. }
  496. }
  497. #endif /* (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0) */
  498. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  499. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  500. static uint32_t statck_del_fpu_regs(uint32_t fault_handler_lr, uint32_t sp) {
  501. statck_has_fpu_regs = (fault_handler_lr & (1UL << 4)) == 0 ? true : false;
  502. /* the stack has S0~S15 and FPSCR registers when statck_has_fpu_regs is true, double word align */
  503. return statck_has_fpu_regs == true ? sp + sizeof(size_t) * 18 : sp;
  504. }
  505. #endif
  506. /**
  507. * backtrace for fault
  508. * @note only call once
  509. *
  510. * @param fault_handler_lr the LR register value on fault handler
  511. * @param fault_handler_sp the stack pointer on fault handler
  512. */
  513. void cm_backtrace_fault(uint32_t fault_handler_lr, uint32_t fault_handler_sp) {
  514. uint32_t stack_pointer = fault_handler_sp, saved_regs_addr = stack_pointer;
  515. const char *regs_name[] = { "R0 ", "R1 ", "R2 ", "R3 ", "R12", "LR ", "PC ", "PSR" };
  516. #ifdef CMB_USING_DUMP_STACK_INFO
  517. uint32_t stack_start_addr = main_stack_start_addr;
  518. size_t stack_size = main_stack_size;
  519. #endif
  520. CMB_ASSERT(init_ok);
  521. /* only call once */
  522. CMB_ASSERT(!on_fault);
  523. on_fault = true;
  524. cmb_println("");
  525. cm_backtrace_firmware_info();
  526. #ifdef CMB_USING_OS_PLATFORM
  527. on_thread_before_fault = fault_handler_lr & (1UL << 2);
  528. /* check which stack was used before (MSP or PSP) */
  529. if (on_thread_before_fault) {
  530. cmb_println(print_info[PRINT_FAULT_ON_THREAD], get_cur_thread_name() != NULL ? get_cur_thread_name() : "NO_NAME");
  531. saved_regs_addr = stack_pointer = cmb_get_psp();
  532. #ifdef CMB_USING_DUMP_STACK_INFO
  533. get_cur_thread_stack_info(stack_pointer, &stack_start_addr, &stack_size);
  534. #endif /* CMB_USING_DUMP_STACK_INFO */
  535. } else {
  536. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  537. }
  538. #else
  539. /* bare metal(no OS) environment */
  540. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  541. #endif /* CMB_USING_OS_PLATFORM */
  542. /* delete saved R0~R3, R12, LR,PC,xPSR registers space */
  543. stack_pointer += sizeof(size_t) * 8;
  544. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  545. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  546. stack_pointer = statck_del_fpu_regs(fault_handler_lr, stack_pointer);
  547. #endif /* (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) */
  548. #ifdef CMB_USING_DUMP_STACK_INFO
  549. /* check stack overflow */
  550. if (stack_pointer < stack_start_addr || stack_pointer > stack_start_addr + stack_size) {
  551. stack_is_overflow = true;
  552. }
  553. /* dump stack information */
  554. dump_stack(stack_start_addr, stack_size, (uint32_t *) stack_pointer);
  555. #endif /* CMB_USING_DUMP_STACK_INFO */
  556. /* the stack frame may be get failed when it is overflow */
  557. if (!stack_is_overflow) {
  558. /* dump register */
  559. cmb_println(print_info[PRINT_REGS_TITLE]);
  560. regs.saved.r0 = ((uint32_t *)saved_regs_addr)[0]; // Register R0
  561. regs.saved.r1 = ((uint32_t *)saved_regs_addr)[1]; // Register R1
  562. regs.saved.r2 = ((uint32_t *)saved_regs_addr)[2]; // Register R2
  563. regs.saved.r3 = ((uint32_t *)saved_regs_addr)[3]; // Register R3
  564. regs.saved.r12 = ((uint32_t *)saved_regs_addr)[4]; // Register R12
  565. regs.saved.lr = ((uint32_t *)saved_regs_addr)[5]; // Link register LR
  566. regs.saved.pc = ((uint32_t *)saved_regs_addr)[6]; // Program counter PC
  567. regs.saved.psr.value = ((uint32_t *)saved_regs_addr)[7]; // Program status word PSR
  568. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[0], regs.saved.r0,
  569. regs_name[1], regs.saved.r1,
  570. regs_name[2], regs.saved.r2,
  571. regs_name[3], regs.saved.r3);
  572. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[4], regs.saved.r12,
  573. regs_name[5], regs.saved.lr,
  574. regs_name[6], regs.saved.pc,
  575. regs_name[7], regs.saved.psr.value);
  576. cmb_println("==============================================================");
  577. }
  578. /* the Cortex-M0 is not support fault diagnosis */
  579. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  580. regs.syshndctrl.value = CMB_SYSHND_CTRL; // System Handler Control and State Register
  581. regs.mfsr.value = CMB_NVIC_MFSR; // Memory Fault Status Register
  582. regs.mmar = CMB_NVIC_MMAR; // Memory Management Fault Address Register
  583. regs.bfsr.value = CMB_NVIC_BFSR; // Bus Fault Status Register
  584. regs.bfar = CMB_NVIC_BFAR; // Bus Fault Manage Address Register
  585. regs.ufsr.value = CMB_NVIC_UFSR; // Usage Fault Status Register
  586. regs.hfsr.value = CMB_NVIC_HFSR; // Hard Fault Status Register
  587. regs.dfsr.value = CMB_NVIC_DFSR; // Debug Fault Status Register
  588. regs.afsr = CMB_NVIC_AFSR; // Auxiliary Fault Status Register
  589. fault_diagnosis();
  590. #endif
  591. print_call_stack(stack_pointer);
  592. }