My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

unwarm_arm.cpp 18KB

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