My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unwarmbytab.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Libbacktrace
  3. * Copyright 2015 Stephen Street <stephen@redrocketcomputing.com>
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://www.mozilla.org/en-US/MPL/2.0/
  8. *
  9. * This library was modified, some bugs fixed, stack address validated
  10. * and adapted to be used in Marlin 3D printer firmware as backtracer
  11. * for exceptions for debugging purposes in 2018 by Eduardo José Tagle.
  12. */
  13. #if defined(__arm__) || defined(__thumb__)
  14. #include "unwarmbytab.h"
  15. #include <stdint.h>
  16. #include <string.h>
  17. /* These symbols point to the unwind index and should be provide by the linker script */
  18. extern "C" const UnwTabEntry __exidx_start[];
  19. extern "C" const UnwTabEntry __exidx_end[];
  20. /* This prevents the linking of libgcc unwinder code */
  21. void __aeabi_unwind_cpp_pr0() {};
  22. void __aeabi_unwind_cpp_pr1() {};
  23. void __aeabi_unwind_cpp_pr2() {};
  24. static inline __attribute__((always_inline)) uint32_t prel31_to_addr(const uint32_t *prel31) {
  25. uint32_t offset = (((uint32_t)(*prel31)) << 1) >> 1;
  26. return ((uint32_t)prel31 + offset) & 0x7FFFFFFF;
  27. }
  28. static const UnwTabEntry *UnwTabSearchIndex(const UnwTabEntry *start, const UnwTabEntry *end, uint32_t ip) {
  29. const UnwTabEntry *middle;
  30. /* Perform a binary search of the unwind index */
  31. while (start < end - 1) {
  32. middle = start + ((end - start + 1) >> 1);
  33. if (ip < prel31_to_addr(&middle->addr_offset))
  34. end = middle;
  35. else
  36. start = middle;
  37. }
  38. return start;
  39. }
  40. /*
  41. * Get the function name or nullptr if not found
  42. */
  43. static const char *UnwTabGetFunctionName(const UnwindCallbacks *cb, uint32_t address) {
  44. uint32_t flag_word = 0;
  45. if (!cb->readW(address-4,&flag_word))
  46. return nullptr;
  47. if ((flag_word & 0xFF000000) == 0xFF000000) {
  48. return (const char *)(address - 4 - (flag_word & 0x00FFFFFF));
  49. }
  50. return nullptr;
  51. }
  52. /**
  53. * Get the next frame unwinding instruction
  54. *
  55. * Return either the instruction or -1 to signal no more instructions
  56. * are available
  57. */
  58. static int UnwTabGetNextInstruction(const UnwindCallbacks *cb, UnwTabState *ucb) {
  59. int instruction;
  60. /* Are there more instructions */
  61. if (ucb->remaining == 0)
  62. return -1;
  63. /* Extract the current instruction */
  64. uint32_t v = 0;
  65. if (!cb->readW(ucb->current, &v))
  66. return -1;
  67. instruction = (v >> (ucb->byte << 3)) & 0xFF;
  68. /* Move the next byte */
  69. --ucb->byte;
  70. if (ucb->byte < 0) {
  71. ucb->current += 4;
  72. ucb->byte = 3;
  73. }
  74. --ucb->remaining;
  75. return instruction;
  76. }
  77. /**
  78. * Initialize the frame unwinding state
  79. */
  80. static UnwResult UnwTabStateInit(const UnwindCallbacks *cb, UnwTabState *ucb, uint32_t instructions, const UnwindFrame *frame) {
  81. /* Initialize control block */
  82. memset(ucb, 0, sizeof(UnwTabState));
  83. ucb->current = instructions;
  84. /* Is a short unwind description */
  85. uint32_t v = 0;
  86. if (!cb->readW(instructions, &v))
  87. return UNWIND_DREAD_W_FAIL;
  88. if ((v & 0xFF000000) == 0x80000000) {
  89. ucb->remaining = 3;
  90. ucb->byte = 2;
  91. /* Is a long unwind description */
  92. } else if ((v & 0xFF000000) == 0x81000000) {
  93. ucb->remaining = ((v & 0x00FF0000) >> 14) + 2;
  94. ucb->byte = 1;
  95. } else
  96. return UNWIND_UNSUPPORTED_DWARF_PERSONALITY;
  97. /* Initialize the virtual register set */
  98. ucb->vrs[7] = frame->fp;
  99. ucb->vrs[13] = frame->sp;
  100. ucb->vrs[14] = frame->lr;
  101. ucb->vrs[15] = 0;
  102. /* All good */
  103. return UNWIND_SUCCESS;
  104. }
  105. /*
  106. * Execute unwinding instructions
  107. */
  108. static UnwResult UnwTabExecuteInstructions(const UnwindCallbacks *cb, UnwTabState *ucb) {
  109. int instruction;
  110. uint32_t mask;
  111. uint32_t reg;
  112. uint32_t vsp;
  113. /* Consume all instruction byte */
  114. while ((instruction = UnwTabGetNextInstruction(cb, ucb)) != -1) {
  115. if ((instruction & 0xC0) == 0x00) { // ARM_EXIDX_CMD_DATA_POP
  116. /* vsp = vsp + (xxxxxx << 2) + 4 */
  117. ucb->vrs[13] += ((instruction & 0x3F) << 2) + 4;
  118. } else
  119. if ((instruction & 0xC0) == 0x40) { // ARM_EXIDX_CMD_DATA_PUSH
  120. /* vsp = vsp - (xxxxxx << 2) - 4 */
  121. ucb->vrs[13] -= ((instruction & 0x3F) << 2) - 4;
  122. } else
  123. if ((instruction & 0xF0) == 0x80) {
  124. /* pop under mask {r15-r12},{r11-r4} or refuse to unwind */
  125. instruction = instruction << 8 | UnwTabGetNextInstruction(cb, ucb);
  126. /* Check for refuse to unwind */
  127. if (instruction == 0x8000) // ARM_EXIDX_CMD_REFUSED
  128. return UNWIND_REFUSED;
  129. /* Pop registers using mask */ // ARM_EXIDX_CMD_REG_POP
  130. vsp = ucb->vrs[13];
  131. mask = instruction & 0xFFF;
  132. reg = 4;
  133. while (mask) {
  134. if ((mask & 1) != 0) {
  135. uint32_t v;
  136. if (!cb->readW(vsp,&v))
  137. return UNWIND_DREAD_W_FAIL;
  138. ucb->vrs[reg] = v;
  139. v += 4;
  140. }
  141. mask >>= 1;
  142. ++reg;
  143. }
  144. /* Patch up the vrs sp if it was in the mask */
  145. if ((instruction & (1 << (13 - 4))) != 0)
  146. ucb->vrs[13] = vsp;
  147. } else
  148. if ((instruction & 0xF0) == 0x90 && // ARM_EXIDX_CMD_REG_TO_SP
  149. instruction != 0x9D &&
  150. instruction != 0x9F) {
  151. /* vsp = r[nnnn] */
  152. ucb->vrs[13] = ucb->vrs[instruction & 0x0F];
  153. } else
  154. if ((instruction & 0xF0) == 0xA0) { // ARM_EXIDX_CMD_REG_POP
  155. /* pop r4-r[4+nnn] or pop r4-r[4+nnn], r14*/
  156. vsp = ucb->vrs[13];
  157. for (reg = 4; reg <= uint32_t((instruction & 0x07) + 4); ++reg) {
  158. uint32_t v;
  159. if (!cb->readW(vsp,&v))
  160. return UNWIND_DREAD_W_FAIL;
  161. ucb->vrs[reg] = v;
  162. vsp += 4;
  163. }
  164. if (instruction & 0x08) { // ARM_EXIDX_CMD_REG_POP
  165. uint32_t v;
  166. if (!cb->readW(vsp,&v))
  167. return UNWIND_DREAD_W_FAIL;
  168. ucb->vrs[14] = v;
  169. vsp += 4;
  170. }
  171. ucb->vrs[13] = vsp;
  172. } else
  173. if (instruction == 0xB0) { // ARM_EXIDX_CMD_FINISH
  174. /* finished */
  175. if (ucb->vrs[15] == 0)
  176. ucb->vrs[15] = ucb->vrs[14];
  177. /* All done unwinding */
  178. return UNWIND_SUCCESS;
  179. } else
  180. if (instruction == 0xB1) { // ARM_EXIDX_CMD_REG_POP
  181. /* pop register under mask {r3,r2,r1,r0} */
  182. vsp = ucb->vrs[13];
  183. mask = UnwTabGetNextInstruction(cb, ucb);
  184. reg = 0;
  185. while (mask) {
  186. if ((mask & 1) != 0) {
  187. uint32_t v;
  188. if (!cb->readW(vsp,&v))
  189. return UNWIND_DREAD_W_FAIL;
  190. ucb->vrs[reg] = v;
  191. vsp += 4;
  192. }
  193. mask >>= 1;
  194. ++reg;
  195. }
  196. ucb->vrs[13] = (uint32_t)vsp;
  197. } else
  198. if (instruction == 0xB2) { // ARM_EXIDX_CMD_DATA_POP
  199. /* vps = vsp + 0x204 + (uleb128 << 2) */
  200. ucb->vrs[13] += 0x204 + (UnwTabGetNextInstruction(cb, ucb) << 2);
  201. } else
  202. if (instruction == 0xB3 || // ARM_EXIDX_CMD_VFP_POP
  203. instruction == 0xC8 ||
  204. instruction == 0xC9) {
  205. /* pop VFP double-precision registers */
  206. vsp = ucb->vrs[13];
  207. /* D[ssss]-D[ssss+cccc] */
  208. uint32_t v;
  209. if (!cb->readW(vsp,&v))
  210. return UNWIND_DREAD_W_FAIL;
  211. ucb->vrs[14] = v;
  212. vsp += 4;
  213. if (instruction == 0xC8) {
  214. /* D[16+sssss]-D[16+ssss+cccc] */
  215. ucb->vrs[14] |= 1 << 16;
  216. }
  217. if (instruction != 0xB3) {
  218. /* D[sssss]-D[ssss+cccc] */
  219. ucb->vrs[14] |= 1 << 17;
  220. }
  221. ucb->vrs[13] = vsp;
  222. } else
  223. if ((instruction & 0xF8) == 0xB8 ||
  224. (instruction & 0xF8) == 0xD0) {
  225. /* Pop VFP double precision registers D[8]-D[8+nnn] */
  226. ucb->vrs[14] = 0x80 | (instruction & 0x07);
  227. if ((instruction & 0xF8) == 0xD0) {
  228. ucb->vrs[14] = 1 << 17;
  229. }
  230. } else
  231. return UNWIND_UNSUPPORTED_DWARF_INSTR;
  232. }
  233. return UNWIND_SUCCESS;
  234. }
  235. static inline __attribute__((always_inline)) uint32_t read_psp() {
  236. /* Read the current PSP and return its value as a pointer */
  237. uint32_t psp;
  238. __asm__ volatile (
  239. " mrs %0, psp \n"
  240. : "=r" (psp) : :
  241. );
  242. return psp;
  243. }
  244. /*
  245. * Unwind the specified frame and goto the previous one
  246. */
  247. static UnwResult UnwTabUnwindFrame(const UnwindCallbacks *cb, UnwindFrame *frame) {
  248. UnwResult err;
  249. UnwTabState ucb;
  250. const UnwTabEntry *index;
  251. uint32_t instructions;
  252. /* Search the unwind index for the matching unwind table */
  253. index = UnwTabSearchIndex(__exidx_start, __exidx_end, frame->pc);
  254. /* Make sure we can unwind this frame */
  255. if (index->insn == 0x00000001)
  256. return UNWIND_SUCCESS;
  257. /* Get the pointer to the first unwind instruction */
  258. if (index->insn & 0x80000000)
  259. instructions = (uint32_t)&index->insn;
  260. else
  261. instructions = prel31_to_addr(&index->insn);
  262. /* Initialize the unwind control block */
  263. if ((err = UnwTabStateInit(cb, &ucb, instructions, frame)) < 0)
  264. return err;
  265. /* Execute the unwind instructions */
  266. err = UnwTabExecuteInstructions(cb, &ucb);
  267. if (err < 0)
  268. return err;
  269. /* Set the virtual pc to the virtual lr if this is the first unwind */
  270. if (ucb.vrs[15] == 0)
  271. ucb.vrs[15] = ucb.vrs[14];
  272. /* Check for exception return */
  273. /* TODO Test with other ARM processors to verify this method. */
  274. if ((ucb.vrs[15] & 0xF0000000) == 0xF0000000) {
  275. /* According to the Cortex Programming Manual (p.44), the stack address is always 8-byte aligned (Cortex-M7).
  276. Depending on where the exception came from (MSP or PSP), we need the right SP value to work with.
  277. ucb.vrs[7] contains the right value, so take it and align it by 8 bytes, store it as the current
  278. SP to work with (ucb.vrs[13]) which is then saved as the current (virtual) frame's SP.
  279. */
  280. uint32_t stack;
  281. ucb.vrs[13] = (ucb.vrs[7] & ~7);
  282. /* If we need to start from the MSP, we need to go down X words to find the PC, where:
  283. X=2 if it was a non-floating-point exception
  284. X=20 if it was a floating-point (VFP) exception
  285. If we need to start from the PSP, we need to go up exactly 6 words to find the PC.
  286. See the ARMv7-M Architecture Reference Manual p.594 and Cortex-M7 Processor Programming Manual p.44/p.45 for details.
  287. */
  288. if ((ucb.vrs[15] & 0xC) == 0) {
  289. /* Return to Handler Mode: MSP (0xFFFFFF-1) */
  290. stack = ucb.vrs[13];
  291. /* The PC is always 2 words down from the MSP, if it was a non-floating-point exception */
  292. stack -= 2*4;
  293. /* If there was a VFP exception (0xFFFFFFE1), the PC is located another 18 words down */
  294. if ((ucb.vrs[15] & 0xF0) == 0xE0) {
  295. stack -= 18*4;
  296. }
  297. }
  298. else {
  299. /* Return to Thread Mode: PSP (0xFFFFFF-d) */
  300. stack = read_psp();
  301. /* The PC is always 6 words up from the PSP */
  302. stack += 6*4;
  303. }
  304. /* Store the PC */
  305. uint32_t v;
  306. if (!cb->readW(stack,&v))
  307. return UNWIND_DREAD_W_FAIL;
  308. ucb.vrs[15] = v;
  309. stack -= 4;
  310. /* Store the LR */
  311. if (!cb->readW(stack,&v))
  312. return UNWIND_DREAD_W_FAIL;
  313. ucb.vrs[14] = v;
  314. stack -= 4;
  315. }
  316. /* We are done if current frame pc is equal to the virtual pc, prevent infinite loop */
  317. if (frame->pc == ucb.vrs[15])
  318. return UNWIND_SUCCESS;
  319. /* Update the frame */
  320. frame->fp = ucb.vrs[7];
  321. frame->sp = ucb.vrs[13];
  322. frame->lr = ucb.vrs[14];
  323. frame->pc = ucb.vrs[15];
  324. /* All good - Continue unwinding */
  325. return UNWIND_MORE_AVAILABLE;
  326. }
  327. UnwResult UnwindByTableStart(UnwindFrame* frame, const UnwindCallbacks *cb, void *data) {
  328. UnwResult err = UNWIND_SUCCESS;
  329. UnwReport entry;
  330. /* Use DWARF unwind information to unwind frames */
  331. do {
  332. if (frame->pc == 0) {
  333. /* Reached __exidx_end. */
  334. break;
  335. }
  336. if (frame->pc == 0x00000001) {
  337. /* Reached .cantunwind instruction. */
  338. break;
  339. }
  340. /* Find the unwind index of the current frame pc */
  341. const UnwTabEntry *index = UnwTabSearchIndex(__exidx_start, __exidx_end, frame->pc);
  342. /* Clear last bit (Thumb indicator) */
  343. frame->pc &= 0xFFFFFFFEU;
  344. /* Generate the backtrace information */
  345. entry.address = frame->pc;
  346. entry.function = prel31_to_addr(&index->addr_offset);
  347. entry.name = UnwTabGetFunctionName(cb, entry.function);
  348. if (!cb->report(data,&entry))
  349. break;
  350. /* Unwind frame and repeat */
  351. } while ((err = UnwTabUnwindFrame(cb, frame)) == UNWIND_MORE_AVAILABLE);
  352. /* All done */
  353. return err;
  354. }
  355. #endif // __arm__ || __thumb__