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.

unwarm_arm.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /***************************************************************************
  2. * ARM Stack Unwinder, Michael.McTernan.2001@cs.bris.ac.uk
  3. * Updated, adapted and several bug fixes on 2018 by Eduardo José Tagle
  4. *
  5. * This program is PUBLIC DOMAIN.
  6. * This means that there is no copyright and anyone is able to take a copy
  7. * for free and use it as they wish, with or without modifications, and in
  8. * any context, commercially or otherwise. The only limitation is that I
  9. * don't guarantee that the software is fit for any purpose or accept any
  10. * liability for it's use or misuse - this software is without warranty.
  11. ***************************************************************************
  12. * File Description: Abstract interpreter for ARM mode.
  13. **************************************************************************/
  14. #if defined(__arm__) || defined(__thumb__)
  15. #define MODULE_NAME "UNWARM_ARM"
  16. #include <stdio.h>
  17. #include "unwarm.h"
  18. /** Check if some instruction is a data-processing instruction.
  19. * Decodes the passed instruction, checks if it is a data-processing and
  20. * verifies that the parameters and operation really indicate a data-
  21. * processing instruction. This is needed because some parts of the
  22. * instruction space under this instruction can be extended or represent
  23. * other operations such as MRS, MSR.
  24. *
  25. * \param[in] inst The instruction word.
  26. * \retval true Further decoding of the instruction indicates that this is
  27. * a valid data-processing instruction.
  28. * \retval false This is not a data-processing instruction,
  29. */
  30. static bool isDataProc(uint32_t instr) {
  31. uint8_t opcode = (instr & 0x01E00000) >> 21;
  32. bool S = (instr & 0x00100000) ? true : false;
  33. if ((instr & 0xFC000000) != 0xE0000000) {
  34. return false;
  35. }
  36. else if (!S && opcode >= 8 && opcode <= 11) {
  37. /* TST, TEQ, CMP and CMN all require S to be set */
  38. return false;
  39. }
  40. else
  41. return true;
  42. }
  43. UnwResult UnwStartArm(UnwState * const state) {
  44. bool found = false;
  45. uint16_t t = UNW_MAX_INSTR_COUNT;
  46. do {
  47. uint32_t instr;
  48. /* Attempt to read the instruction */
  49. if (!state->cb->readW(state->regData[15].v, &instr)) {
  50. return UNWIND_IREAD_W_FAIL;
  51. }
  52. UnwPrintd4("A %x %x %08x:", state->regData[13].v, state->regData[15].v, instr);
  53. /* Check that the PC is still on Arm alignment */
  54. if (state->regData[15].v & 0x3) {
  55. UnwPrintd1("\nError: PC misalignment\n");
  56. return UNWIND_INCONSISTENT;
  57. }
  58. /* Check that the SP and PC have not been invalidated */
  59. if (!M_IsOriginValid(state->regData[13].o) || !M_IsOriginValid(state->regData[15].o)) {
  60. UnwPrintd1("\nError: PC or SP invalidated\n");
  61. return UNWIND_INCONSISTENT;
  62. }
  63. /* Branch and Exchange (BX)
  64. * This is tested prior to data processing to prevent
  65. * mis-interpretation as an invalid TEQ instruction.
  66. */
  67. if ((instr & 0xFFFFFFF0) == 0xE12FFF10) {
  68. uint8_t rn = instr & 0xF;
  69. UnwPrintd4("BX r%d\t ; r%d %s\n", rn, rn, M_Origin2Str(state->regData[rn].o));
  70. if (!M_IsOriginValid(state->regData[rn].o)) {
  71. UnwPrintd1("\nUnwind failure: BX to untracked register\n");
  72. return UNWIND_FAILURE;
  73. }
  74. /* Set the new PC value */
  75. state->regData[15].v = state->regData[rn].v;
  76. /* Check if the return value is from the stack */
  77. if (state->regData[rn].o == REG_VAL_FROM_STACK) {
  78. /* Now have the return address */
  79. UnwPrintd2(" Return PC=%x\n", state->regData[15].v & (~0x1));
  80. /* Report the return address */
  81. if (!UnwReportRetAddr(state, state->regData[rn].v))
  82. return UNWIND_TRUNCATED;
  83. }
  84. /* Determine the return mode */
  85. if (state->regData[rn].v & 0x1) {
  86. /* Branching to THUMB */
  87. return UnwStartThumb(state);
  88. }
  89. else {
  90. /* Branch to ARM */
  91. /* Account for the auto-increment which isn't needed */
  92. state->regData[15].v -= 4;
  93. }
  94. }
  95. /* Branch */
  96. else if ((instr & 0xFF000000) == 0xEA000000) {
  97. int32_t offset = (instr & 0x00FFFFFF);
  98. /* Shift value */
  99. offset = offset << 2;
  100. /* Sign extend if needed */
  101. if (offset & 0x02000000) {
  102. offset |= 0xFC000000;
  103. }
  104. UnwPrintd2("B %d\n", offset);
  105. /* Adjust PC */
  106. state->regData[15].v += offset;
  107. /* Account for pre-fetch, where normally the PC is 8 bytes
  108. * ahead of the instruction just executed.
  109. */
  110. state->regData[15].v += 4;
  111. }
  112. /* MRS */
  113. else if ((instr & 0xFFBF0FFF) == 0xE10F0000) {
  114. #if defined(UNW_DEBUG)
  115. bool R = (instr & 0x00400000) ? true : false;
  116. #endif
  117. uint8_t rd = (instr & 0x0000F000) >> 12;
  118. UnwPrintd4("MRS r%d,%s\t; r%d invalidated", rd, R ? "SPSR" : "CPSR", rd);
  119. /* Status registers untracked */
  120. state->regData[rd].o = REG_VAL_INVALID;
  121. }
  122. /* MSR */
  123. else if ((instr & 0xFFB0F000) == 0xE120F000) {
  124. #if defined(UNW_DEBUG)
  125. bool R = (instr & 0x00400000) ? true : false;
  126. UnwPrintd2("MSR %s_?, ???", R ? "SPSR" : "CPSR");
  127. #endif
  128. /* Status registers untracked.
  129. * Potentially this could change processor mode and switch
  130. * banked registers r8-r14. Most likely is that r13 (sp) will
  131. * be banked. However, invalidating r13 will stop unwinding
  132. * when potentially this write is being used to disable/enable
  133. * interrupts (a common case). Therefore no invalidation is
  134. * performed.
  135. */
  136. }
  137. /* Data processing */
  138. else if (isDataProc(instr)) {
  139. bool I = (instr & 0x02000000) ? true : false;
  140. uint8_t opcode = (instr & 0x01E00000) >> 21;
  141. #if defined(UNW_DEBUG)
  142. bool S = (instr & 0x00100000) ? true : false;
  143. #endif
  144. uint8_t rn = (instr & 0x000F0000) >> 16;
  145. uint8_t rd = (instr & 0x0000F000) >> 12;
  146. uint16_t operand2 = (instr & 0x00000FFF);
  147. uint32_t op2val;
  148. int op2origin;
  149. switch(opcode) {
  150. case 0: UnwPrintd4("AND%s r%d,r%d,", S ? "S" : "", rd, rn); break;
  151. case 1: UnwPrintd4("EOR%s r%d,r%d,", S ? "S" : "", rd, rn); break;
  152. case 2: UnwPrintd4("SUB%s r%d,r%d,", S ? "S" : "", rd, rn); break;
  153. case 3: UnwPrintd4("RSB%s r%d,r%d,", S ? "S" : "", rd, rn); break;
  154. case 4: UnwPrintd4("ADD%s r%d,r%d,", S ? "S" : "", rd, rn); break;
  155. case 5: UnwPrintd4("ADC%s r%d,r%d,", S ? "S" : "", rd, rn); break;
  156. case 6: UnwPrintd4("SBC%s r%d,r%d,", S ? "S" : "", rd, rn); break;
  157. case 7: UnwPrintd4("RSC%s r%d,r%d,", S ? "S" : "", rd, rn); break;
  158. case 8: UnwPrintd3("TST%s r%d,", S ? "S" : "", rn); break;
  159. case 9: UnwPrintd3("TEQ%s r%d,", S ? "S" : "", rn); break;
  160. case 10: UnwPrintd3("CMP%s r%d,", S ? "S" : "", rn); break;
  161. case 11: UnwPrintd3("CMN%s r%d,", S ? "S" : "", rn); break;
  162. case 12: UnwPrintd3("ORR%s r%d,", S ? "S" : "", rn); break;
  163. case 13: UnwPrintd3("MOV%s r%d,", S ? "S" : "", rd); break;
  164. case 14: UnwPrintd4("BIC%s r%d,r%d", S ? "S" : "", rd, rn); break;
  165. case 15: UnwPrintd3("MVN%s r%d,", S ? "S" : "", rd); break;
  166. }
  167. /* Decode operand 2 */
  168. if (I) {
  169. uint8_t shiftDist = (operand2 & 0x0F00) >> 8;
  170. uint8_t shiftConst = (operand2 & 0x00FF);
  171. /* rotate const right by 2 * shiftDist */
  172. shiftDist *= 2;
  173. op2val = (shiftConst >> shiftDist) |
  174. (shiftConst << (32 - shiftDist));
  175. op2origin = REG_VAL_FROM_CONST;
  176. UnwPrintd2("#0x%x", op2val);
  177. }
  178. else {
  179. /* Register and shift */
  180. uint8_t rm = (operand2 & 0x000F);
  181. uint8_t regShift = (operand2 & 0x0010) ? true : false;
  182. uint8_t shiftType = (operand2 & 0x0060) >> 5;
  183. uint32_t shiftDist;
  184. #if defined(UNW_DEBUG)
  185. const char * const shiftMnu[4] = { "LSL", "LSR", "ASR", "ROR" };
  186. #endif
  187. UnwPrintd2("r%d ", rm);
  188. /* Get the shift distance */
  189. if (regShift) {
  190. uint8_t rs = (operand2 & 0x0F00) >> 8;
  191. if (operand2 & 0x00800) {
  192. UnwPrintd1("\nError: Bit should be zero\n");
  193. return UNWIND_ILLEGAL_INSTR;
  194. }
  195. else if (rs == 15) {
  196. UnwPrintd1("\nError: Cannot use R15 with register shift\n");
  197. return UNWIND_ILLEGAL_INSTR;
  198. }
  199. /* Get shift distance */
  200. shiftDist = state->regData[rs].v;
  201. op2origin = state->regData[rs].o;
  202. UnwPrintd7("%s r%d\t; r%d %s r%d %s", shiftMnu[shiftType], rs, rm, M_Origin2Str(state->regData[rm].o), rs, M_Origin2Str(state->regData[rs].o));
  203. }
  204. else {
  205. shiftDist = (operand2 & 0x0F80) >> 7;
  206. op2origin = REG_VAL_FROM_CONST;
  207. if (shiftDist) {
  208. UnwPrintd3("%s #%d", shiftMnu[shiftType], shiftDist);
  209. }
  210. UnwPrintd3("\t; r%d %s", rm, M_Origin2Str(state->regData[rm].o));
  211. }
  212. /* Apply the shift type to the source register */
  213. switch(shiftType) {
  214. case 0: /* logical left */
  215. op2val = state->regData[rm].v << shiftDist;
  216. break;
  217. case 1: /* logical right */
  218. if (!regShift && shiftDist == 0) {
  219. shiftDist = 32;
  220. }
  221. op2val = state->regData[rm].v >> shiftDist;
  222. break;
  223. case 2: /* arithmetic right */
  224. if (!regShift && shiftDist == 0) {
  225. shiftDist = 32;
  226. }
  227. if (state->regData[rm].v & 0x80000000) {
  228. /* Register shifts maybe greater than 32 */
  229. if (shiftDist >= 32) {
  230. op2val = 0xFFFFFFFF;
  231. }
  232. else {
  233. op2val = state->regData[rm].v >> shiftDist;
  234. op2val |= 0xFFFFFFFF << (32 - shiftDist);
  235. }
  236. }
  237. else {
  238. op2val = state->regData[rm].v >> shiftDist;
  239. }
  240. break;
  241. case 3: /* rotate right */
  242. if (!regShift && shiftDist == 0) {
  243. /* Rotate right with extend.
  244. * This uses the carry bit and so always has an
  245. * untracked result.
  246. */
  247. op2origin = REG_VAL_INVALID;
  248. op2val = 0;
  249. }
  250. else {
  251. /* Limit shift distance to 0-31 incase of register shift */
  252. shiftDist &= 0x1F;
  253. op2val = (state->regData[rm].v >> shiftDist) |
  254. (state->regData[rm].v << (32 - shiftDist));
  255. }
  256. break;
  257. default:
  258. UnwPrintd2("\nError: Invalid shift type: %d\n", shiftType);
  259. return UNWIND_FAILURE;
  260. }
  261. /* Decide the data origin */
  262. if (M_IsOriginValid(op2origin) &&
  263. M_IsOriginValid(state->regData[rm].o)) {
  264. op2origin = state->regData[rm].o;
  265. op2origin |= REG_VAL_ARITHMETIC;
  266. }
  267. else {
  268. op2origin = REG_VAL_INVALID;
  269. }
  270. }
  271. /* Propagate register validity */
  272. switch(opcode) {
  273. case 0: /* AND: Rd := Op1 AND Op2 */
  274. case 1: /* EOR: Rd := Op1 EOR Op2 */
  275. case 2: /* SUB: Rd:= Op1 - Op2 */
  276. case 3: /* RSB: Rd:= Op2 - Op1 */
  277. case 4: /* ADD: Rd:= Op1 + Op2 */
  278. case 12: /* ORR: Rd:= Op1 OR Op2 */
  279. case 14: /* BIC: Rd:= Op1 AND NOT Op2 */
  280. if (!M_IsOriginValid(state->regData[rn].o) ||
  281. !M_IsOriginValid(op2origin)) {
  282. state->regData[rd].o = REG_VAL_INVALID;
  283. }
  284. else {
  285. state->regData[rd].o = state->regData[rn].o;
  286. state->regData[rd].o = (RegValOrigin)(state->regData[rd].o | op2origin);
  287. }
  288. break;
  289. case 5: /* ADC: Rd:= Op1 + Op2 + C */
  290. case 6: /* SBC: Rd:= Op1 - Op2 + C */
  291. case 7: /* RSC: Rd:= Op2 - Op1 + C */
  292. /* CPSR is not tracked */
  293. state->regData[rd].o = REG_VAL_INVALID;
  294. break;
  295. case 8: /* TST: set condition codes on Op1 AND Op2 */
  296. case 9: /* TEQ: set condition codes on Op1 EOR Op2 */
  297. case 10: /* CMP: set condition codes on Op1 - Op2 */
  298. case 11: /* CMN: set condition codes on Op1 + Op2 */
  299. break;
  300. case 13: /* MOV: Rd:= Op2 */
  301. case 15: /* MVN: Rd:= NOT Op2 */
  302. state->regData[rd].o = (RegValOrigin) op2origin;
  303. break;
  304. }
  305. /* Account for pre-fetch by temporarily adjusting PC */
  306. if (rn == 15) {
  307. /* If the shift amount is specified in the instruction,
  308. * the PC will be 8 bytes ahead. If a register is used
  309. * to specify the shift amount the PC will be 12 bytes
  310. * ahead.
  311. */
  312. if (!I && (operand2 & 0x0010))
  313. state->regData[rn].v += 12;
  314. else
  315. state->regData[rn].v += 8;
  316. }
  317. /* Compute values */
  318. switch(opcode) {
  319. case 0: /* AND: Rd := Op1 AND Op2 */
  320. state->regData[rd].v = state->regData[rn].v & op2val;
  321. break;
  322. case 1: /* EOR: Rd := Op1 EOR Op2 */
  323. state->regData[rd].v = state->regData[rn].v ^ op2val;
  324. break;
  325. case 2: /* SUB: Rd:= Op1 - Op2 */
  326. state->regData[rd].v = state->regData[rn].v - op2val;
  327. break;
  328. case 3: /* RSB: Rd:= Op2 - Op1 */
  329. state->regData[rd].v = op2val - state->regData[rn].v;
  330. break;
  331. case 4: /* ADD: Rd:= Op1 + Op2 */
  332. state->regData[rd].v = state->regData[rn].v + op2val;
  333. break;
  334. case 5: /* ADC: Rd:= Op1 + Op2 + C */
  335. case 6: /* SBC: Rd:= Op1 - Op2 + C */
  336. case 7: /* RSC: Rd:= Op2 - Op1 + C */
  337. case 8: /* TST: set condition codes on Op1 AND Op2 */
  338. case 9: /* TEQ: set condition codes on Op1 EOR Op2 */
  339. case 10: /* CMP: set condition codes on Op1 - Op2 */
  340. case 11: /* CMN: set condition codes on Op1 + Op2 */
  341. UnwPrintd1("\t; ????");
  342. break;
  343. case 12: /* ORR: Rd:= Op1 OR Op2 */
  344. state->regData[rd].v = state->regData[rn].v | op2val;
  345. break;
  346. case 13: /* MOV: Rd:= Op2 */
  347. state->regData[rd].v = op2val;
  348. break;
  349. case 14: /* BIC: Rd:= Op1 AND NOT Op2 */
  350. state->regData[rd].v = state->regData[rn].v & (~op2val);
  351. break;
  352. case 15: /* MVN: Rd:= NOT Op2 */
  353. state->regData[rd].v = ~op2val;
  354. break;
  355. }
  356. /* Remove the prefetch offset from the PC */
  357. if (rd != 15 && rn == 15) {
  358. if (!I && (operand2 & 0x0010))
  359. state->regData[rn].v -= 12;
  360. else
  361. state->regData[rn].v -= 8;
  362. }
  363. }
  364. /* Block Data Transfer
  365. * LDM, STM
  366. */
  367. else if ((instr & 0xFE000000) == 0xE8000000) {
  368. bool P = (instr & 0x01000000) ? true : false;
  369. bool U = (instr & 0x00800000) ? true : false;
  370. bool S = (instr & 0x00400000) ? true : false;
  371. bool W = (instr & 0x00200000) ? true : false;
  372. bool L = (instr & 0x00100000) ? true : false;
  373. uint16_t baseReg = (instr & 0x000F0000) >> 16;
  374. uint16_t regList = (instr & 0x0000FFFF);
  375. uint32_t addr = state->regData[baseReg].v;
  376. bool addrValid = M_IsOriginValid(state->regData[baseReg].o);
  377. int8_t r;
  378. #if defined(UNW_DEBUG)
  379. /* Display the instruction */
  380. if (L) {
  381. UnwPrintd6("LDM%c%c r%d%s, {reglist}%s\n", P ? 'E' : 'F', U ? 'D' : 'A', baseReg, W ? "!" : "", S ? "^" : "");
  382. }
  383. else {
  384. UnwPrintd6("STM%c%c r%d%s, {reglist}%s\n", !P ? 'E' : 'F', !U ? 'D' : 'A', baseReg, W ? "!" : "", S ? "^" : "");
  385. }
  386. #endif
  387. /* S indicates that banked registers (untracked) are used, unless
  388. * this is a load including the PC when the S-bit indicates that
  389. * that CPSR is loaded from SPSR (also untracked, but ignored).
  390. */
  391. if (S && (!L || (regList & (0x01 << 15)) == 0)) {
  392. UnwPrintd1("\nError:S-bit set requiring banked registers\n");
  393. return UNWIND_FAILURE;
  394. }
  395. else if (baseReg == 15) {
  396. UnwPrintd1("\nError: r15 used as base register\n");
  397. return UNWIND_FAILURE;
  398. }
  399. else if (regList == 0) {
  400. UnwPrintd1("\nError: Register list empty\n");
  401. return UNWIND_FAILURE;
  402. }
  403. /* Check if ascending or descending.
  404. * Registers are loaded/stored in order of address.
  405. * i.e. r0 is at the lowest address, r15 at the highest.
  406. */
  407. r = U ? 0 : 15;
  408. do {
  409. /* Check if the register is to be transferred */
  410. if (regList & (0x01 << r)) {
  411. if (P)
  412. addr += U ? 4 : -4;
  413. if (L) {
  414. if (addrValid) {
  415. if (!UnwMemReadRegister(state, addr, &state->regData[r])) {
  416. return UNWIND_DREAD_W_FAIL;
  417. }
  418. /* Update the origin if read via the stack pointer */
  419. if (M_IsOriginValid(state->regData[r].o) && baseReg == 13) {
  420. state->regData[r].o = REG_VAL_FROM_STACK;
  421. }
  422. UnwPrintd5(" R%d = 0x%08x\t; r%d %s\n",r,state->regData[r].v,r, M_Origin2Str(state->regData[r].o));
  423. }
  424. else {
  425. /* Invalidate the register as the base reg was invalid */
  426. state->regData[r].o = REG_VAL_INVALID;
  427. UnwPrintd2(" R%d = ???\n", r);
  428. }
  429. }
  430. else {
  431. if (addrValid) {
  432. if (!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
  433. return UNWIND_DWRITE_W_FAIL;
  434. }
  435. }
  436. UnwPrintd2(" R%d = 0x%08x\n", r);
  437. }
  438. if (!P)
  439. addr += U ? 4 : -4;
  440. }
  441. /* Check the next register */
  442. r += U ? 1 : -1;
  443. } while (r >= 0 && r <= 15);
  444. /* Check the writeback bit */
  445. if (W)
  446. state->regData[baseReg].v = addr;
  447. /* Check if the PC was loaded */
  448. if (L && (regList & (0x01 << 15))) {
  449. if (!M_IsOriginValid(state->regData[15].o)) {
  450. /* Return address is not valid */
  451. UnwPrintd1("PC popped with invalid address\n");
  452. return UNWIND_FAILURE;
  453. }
  454. else {
  455. /* Store the return address */
  456. if (!UnwReportRetAddr(state, state->regData[15].v)) {
  457. return UNWIND_TRUNCATED;
  458. }
  459. UnwPrintd2(" Return PC=0x%x", state->regData[15].v);
  460. /* Determine the return mode */
  461. if (state->regData[15].v & 0x1) {
  462. /* Branching to THUMB */
  463. return UnwStartThumb(state);
  464. }
  465. else {
  466. /* Branch to ARM */
  467. /* Account for the auto-increment which isn't needed */
  468. state->regData[15].v -= 4;
  469. }
  470. }
  471. }
  472. }
  473. else {
  474. UnwPrintd1("????");
  475. /* Unknown/undecoded. May alter some register, so invalidate file */
  476. UnwInvalidateRegisterFile(state->regData);
  477. }
  478. UnwPrintd1("\n");
  479. /* Should never hit the reset vector */
  480. if (state->regData[15].v == 0) return UNWIND_RESET;
  481. /* Check next address */
  482. state->regData[15].v += 4;
  483. /* Garbage collect the memory hash (used only for the stack) */
  484. UnwMemHashGC(state);
  485. t--;
  486. if (t == 0)
  487. return UNWIND_EXHAUSTED;
  488. } while (!found);
  489. return UNWIND_UNSUPPORTED;
  490. }
  491. #endif // __arm__ || __thumb__