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_thumb.c 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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 interpretation for Thumb mode.
  13. **************************************************************************/
  14. #ifdef ARDUINO_ARCH_SAM
  15. #define MODULE_NAME "UNWARM_THUMB"
  16. #include <stdio.h>
  17. #include "unwarm.h"
  18. /** Sign extend an 11 bit value.
  19. * This function simply inspects bit 11 of the input \a value, and if
  20. * set, the top 5 bits are set to give a 2's compliment signed value.
  21. * \param value The value to sign extend.
  22. * \return The signed-11 bit value stored in a 16bit data type.
  23. */
  24. static int16_t signExtend11(uint16_t value) {
  25. if(value & 0x400) {
  26. value |= 0xf800;
  27. }
  28. return value;
  29. }
  30. UnwResult UnwStartThumb(UnwState * const state) {
  31. bool found = false;
  32. uint16_t t = UNW_MAX_INSTR_COUNT;
  33. uint32_t lastJumpAddr = 0; // Last JUMP address, to try to detect infinite loops
  34. bool loopDetected = false; // If a loop was detected
  35. do {
  36. uint16_t instr;
  37. /* Attempt to read the instruction */
  38. if(!state->cb->readH(state->regData[15].v & (~0x1), &instr)) {
  39. return UNWIND_IREAD_H_FAIL;
  40. }
  41. UnwPrintd4("T %x %x %04x:", state->regData[13].v, state->regData[15].v, instr);
  42. /* Check that the PC is still on Thumb alignment */
  43. if(!(state->regData[15].v & 0x1)) {
  44. UnwPrintd1("\nError: PC misalignment\n");
  45. return UNWIND_INCONSISTENT;
  46. }
  47. /* Check that the SP and PC have not been invalidated */
  48. if(!M_IsOriginValid(state->regData[13].o) || !M_IsOriginValid(state->regData[15].o)) {
  49. UnwPrintd1("\nError: PC or SP invalidated\n");
  50. return UNWIND_INCONSISTENT;
  51. }
  52. /*
  53. * Detect 32bit thumb instructions
  54. */
  55. if ((instr & 0xe000) == 0xe000 && (instr & 0x1800) != 0) {
  56. uint16_t instr2;
  57. /* Check next address */
  58. state->regData[15].v += 2;
  59. /* Attempt to read the 2nd part of the instruction */
  60. if(!state->cb->readH(state->regData[15].v & (~0x1), &instr2)) {
  61. return UNWIND_IREAD_H_FAIL;
  62. }
  63. UnwPrintd3(" %x %04x:", state->regData[15].v, instr2);
  64. /*
  65. * Load/Store multiple: Only interpret
  66. * PUSH and POP
  67. */
  68. if ((instr & 0xfe6f) == 0xe82d) {
  69. bool L = (instr & 0x10) ? true : false;
  70. uint16_t rList = instr2;
  71. if(L) {
  72. uint8_t r;
  73. /* Load from memory: POP */
  74. UnwPrintd1("POP {Rlist}\n");
  75. /* Load registers from stack */
  76. for(r = 0; r < 16; r++) {
  77. if(rList & (0x1 << r)) {
  78. /* Read the word */
  79. if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r])) {
  80. return UNWIND_DREAD_W_FAIL;
  81. }
  82. /* Alter the origin to be from the stack if it was valid */
  83. if(M_IsOriginValid(state->regData[r].o)) {
  84. state->regData[r].o = REG_VAL_FROM_STACK;
  85. /* If restoring the PC */
  86. if (r == 15) {
  87. /* The bottom bit should have been set to indicate that
  88. * the caller was from Thumb. This would allow return
  89. * by BX for interworking APCS.
  90. */
  91. if((state->regData[15].v & 0x1) == 0) {
  92. UnwPrintd2("Warning: Return address not to Thumb: 0x%08x\n", state->regData[15].v);
  93. /* Pop into the PC will not switch mode */
  94. return UNWIND_INCONSISTENT;
  95. }
  96. /* Store the return address */
  97. if(!UnwReportRetAddr(state, state->regData[15].v)) {
  98. return UNWIND_TRUNCATED;
  99. }
  100. /* Now have the return address */
  101. UnwPrintd2(" Return PC=%x\n", state->regData[15].v);
  102. /* Compensate for the auto-increment, which isn't needed here */
  103. state->regData[15].v -= 2;
  104. }
  105. } else {
  106. if (r == 15) {
  107. /* Return address is not valid */
  108. UnwPrintd1("PC popped with invalid address\n");
  109. return UNWIND_FAILURE;
  110. }
  111. }
  112. state->regData[13].v += 4;
  113. UnwPrintd3(" r%d = 0x%08x\n", r, state->regData[r].v);
  114. }
  115. }
  116. }
  117. else {
  118. int8_t r;
  119. /* Store to memory: PUSH */
  120. UnwPrintd1("PUSH {Rlist}");
  121. for(r = 15; r >= 0; r--) {
  122. if(rList & (0x1 << r)) {
  123. UnwPrintd4("\n r%d = 0x%08x\t; %s", r, state->regData[r].v, M_Origin2Str(state->regData[r].o));
  124. state->regData[13].v -= 4;
  125. if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
  126. return UNWIND_DWRITE_W_FAIL;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /*
  133. * PUSH register
  134. */
  135. else if (instr == 0xf84d && (instr2 & 0x0fff) == 0x0d04) {
  136. uint8_t r = instr2 >> 12;
  137. /* Store to memory: PUSH */
  138. UnwPrintd2("PUSH {R%d}\n", r);
  139. UnwPrintd4("\n r%d = 0x%08x\t; %s", r, state->regData[r].v, M_Origin2Str(state->regData[r].o));
  140. state->regData[13].v -= 4;
  141. if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
  142. return UNWIND_DWRITE_W_FAIL;
  143. }
  144. }
  145. /*
  146. * POP register
  147. */
  148. else if (instr == 0xf85d && (instr2 & 0x0fff) == 0x0b04) {
  149. uint8_t r = instr2 >> 12;
  150. /* Load from memory: POP */
  151. UnwPrintd2("POP {R%d}\n", r);
  152. /* Read the word */
  153. if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r])) {
  154. return UNWIND_DREAD_W_FAIL;
  155. }
  156. /* Alter the origin to be from the stack if it was valid */
  157. if(M_IsOriginValid(state->regData[r].o)) {
  158. state->regData[r].o = REG_VAL_FROM_STACK;
  159. /* If restoring the PC */
  160. if (r == 15) {
  161. /* The bottom bit should have been set to indicate that
  162. * the caller was from Thumb. This would allow return
  163. * by BX for interworking APCS.
  164. */
  165. if((state->regData[15].v & 0x1) == 0) {
  166. UnwPrintd2("Warning: Return address not to Thumb: 0x%08x\n", state->regData[15].v);
  167. /* Pop into the PC will not switch mode */
  168. return UNWIND_INCONSISTENT;
  169. }
  170. /* Store the return address */
  171. if(!UnwReportRetAddr(state, state->regData[15].v)) {
  172. return UNWIND_TRUNCATED;
  173. }
  174. /* Now have the return address */
  175. UnwPrintd2(" Return PC=%x\n", state->regData[15].v);
  176. /* Compensate for the auto-increment, which isn't needed here */
  177. state->regData[15].v -= 2;
  178. }
  179. } else {
  180. if (r == 15) {
  181. /* Return address is not valid */
  182. UnwPrintd1("PC popped with invalid address\n");
  183. return UNWIND_FAILURE;
  184. }
  185. }
  186. state->regData[13].v += 4;
  187. UnwPrintd3(" r%d = 0x%08x\n", r, state->regData[r].v);
  188. }
  189. /*
  190. * Unconditional branch
  191. */
  192. else if ((instr & 0xf800) == 0xf000 && (instr2 & 0xd000) == 0x9000) {
  193. uint32_t v;
  194. uint8_t S = (instr & 0x400) >> 10;
  195. uint16_t imm10 = (instr & 0x3ff);
  196. uint8_t J1 = (instr2 & 0x2000) >> 13;
  197. uint8_t J2 = (instr2 & 0x0800) >> 11;
  198. uint16_t imm11 = (instr2 & 0x7ff);
  199. uint8_t I1 = J1 ^ S ^ 1;
  200. uint8_t I2 = J2 ^ S ^ 1;
  201. uint32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) |(imm10 << 12) | (imm11 << 1);
  202. if (S) imm32 |= 0xfe000000;
  203. UnwPrintd2("B %d \n", imm32);
  204. /* Update PC */
  205. state->regData[15].v += imm32;
  206. /* Need to advance by a word to account for pre-fetch.
  207. * Advance by a half word here, allowing the normal address
  208. * advance to account for the other half word.
  209. */
  210. state->regData[15].v += 2;
  211. /* Compute the jump address */
  212. v = state->regData[15].v + 2;
  213. /* Display PC of next instruction */
  214. UnwPrintd2(" New PC=%x", v);
  215. /* Did we detect an infinite loop ? */
  216. loopDetected = lastJumpAddr == v;
  217. /* Remember the last address we jumped to */
  218. lastJumpAddr = v;
  219. }
  220. /*
  221. * Branch with link
  222. */
  223. else if ((instr & 0xf800) == 0xf000 && (instr2 & 0xd000) == 0xd000) {
  224. uint8_t S = (instr & 0x400) >> 10;
  225. uint16_t imm10 = (instr & 0x3ff);
  226. uint8_t J1 = (instr2 & 0x2000) >> 13;
  227. uint8_t J2 = (instr2 & 0x0800) >> 11;
  228. uint16_t imm11 = (instr2 & 0x7ff);
  229. uint8_t I1 = J1 ^ S ^ 1;
  230. uint8_t I2 = J2 ^ S ^ 1;
  231. uint32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) |(imm10 << 12) | (imm11 << 1);
  232. if (S) imm32 |= 0xfe000000;
  233. UnwPrintd2("BL %d \n", imm32);
  234. /* Never taken, as we are unwinding the stack */
  235. if (0) {
  236. /* Store return address in LR register */
  237. state->regData[14].v = state->regData[15].v + 2;
  238. state->regData[14].o = REG_VAL_FROM_CONST;
  239. /* Update PC */
  240. state->regData[15].v += imm32;
  241. /* Need to advance by a word to account for pre-fetch.
  242. * Advance by a half word here, allowing the normal address
  243. * advance to account for the other half word.
  244. */
  245. state->regData[15].v += 2;
  246. /* Display PC of next instruction */
  247. UnwPrintd2(" Return PC=%x", state->regData[15].v);
  248. /* Report the return address, including mode bit */
  249. if(!UnwReportRetAddr(state, state->regData[15].v)) {
  250. return UNWIND_TRUNCATED;
  251. }
  252. /* Determine the new mode */
  253. if(state->regData[15].v & 0x1) {
  254. /* Branching to THUMB */
  255. /* Account for the auto-increment which isn't needed */
  256. state->regData[15].v -= 2;
  257. }
  258. else {
  259. /* Branch to ARM */
  260. return UnwStartArm(state);
  261. }
  262. }
  263. }
  264. /*
  265. * Conditional branches. Usually not taken, unless infinite loop is detected
  266. */
  267. else if ((instr & 0xf800) == 0xf000 && (instr2 & 0xd000) == 0x8000) {
  268. uint8_t S = (instr & 0x400) >> 10;
  269. uint16_t imm6 = (instr & 0x3f);
  270. uint8_t J1 = (instr2 & 0x2000) >> 13;
  271. uint8_t J2 = (instr2 & 0x0800) >> 11;
  272. uint16_t imm11 = (instr2 & 0x7ff);
  273. uint8_t I1 = J1 ^ S ^ 1;
  274. uint8_t I2 = J2 ^ S ^ 1;
  275. uint32_t imm32 = (S << 20) | (I1 << 19) | (I2 << 18) |(imm6 << 12) | (imm11 << 1);
  276. if (S) imm32 |= 0xffe00000;
  277. UnwPrintd2("Bcond %d\n", imm32);
  278. /* Take the jump only if a loop is detected */
  279. if (loopDetected) {
  280. /* Update PC */
  281. state->regData[15].v += imm32;
  282. /* Need to advance by a word to account for pre-fetch.
  283. * Advance by a half word here, allowing the normal address
  284. * advance to account for the other half word.
  285. */
  286. state->regData[15].v += 2;
  287. /* Display PC of next instruction */
  288. UnwPrintd2(" New PC=%x", state->regData[15].v + 2);
  289. }
  290. }
  291. else {
  292. UnwPrintd1("???? (32)");
  293. /* Unknown/undecoded. May alter some register, so invalidate file */
  294. UnwInvalidateRegisterFile(state->regData);
  295. }
  296. /* End of thumb 32bit code */
  297. }
  298. /* Format 1: Move shifted register
  299. * LSL Rd, Rs, #Offset5
  300. * LSR Rd, Rs, #Offset5
  301. * ASR Rd, Rs, #Offset5
  302. */
  303. else if((instr & 0xe000) == 0x0000 && (instr & 0x1800) != 0x1800) {
  304. bool signExtend;
  305. uint8_t op = (instr & 0x1800) >> 11;
  306. uint8_t offset5 = (instr & 0x07c0) >> 6;
  307. uint8_t rs = (instr & 0x0038) >> 3;
  308. uint8_t rd = (instr & 0x0007);
  309. switch(op) {
  310. case 0: /* LSL */
  311. UnwPrintd6("LSL r%d, r%d, #%d\t; r%d %s", rd, rs, offset5, rs, M_Origin2Str(state->regData[rs].o));
  312. state->regData[rd].v = state->regData[rs].v << offset5;
  313. state->regData[rd].o = state->regData[rs].o;
  314. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  315. break;
  316. case 1: /* LSR */
  317. UnwPrintd6("LSR r%d, r%d, #%d\t; r%d %s", rd, rs, offset5, rs, M_Origin2Str(state->regData[rs].o));
  318. state->regData[rd].v = state->regData[rs].v >> offset5;
  319. state->regData[rd].o = state->regData[rs].o;
  320. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  321. break;
  322. case 2: /* ASR */
  323. UnwPrintd6("ASL r%d, r%d, #%d\t; r%d %s", rd, rs, offset5, rs, M_Origin2Str(state->regData[rs].o));
  324. signExtend = (state->regData[rs].v & 0x8000) ? true : false;
  325. state->regData[rd].v = state->regData[rs].v >> offset5;
  326. if(signExtend) {
  327. state->regData[rd].v |= 0xffffffff << (32 - offset5);
  328. }
  329. state->regData[rd].o = state->regData[rs].o;
  330. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  331. break;
  332. }
  333. }
  334. /* Format 2: add/subtract
  335. * ADD Rd, Rs, Rn
  336. * ADD Rd, Rs, #Offset3
  337. * SUB Rd, Rs, Rn
  338. * SUB Rd, Rs, #Offset3
  339. */
  340. else if((instr & 0xf800) == 0x1800) {
  341. bool I = (instr & 0x0400) ? true : false;
  342. bool op = (instr & 0x0200) ? true : false;
  343. uint8_t rn = (instr & 0x01c0) >> 6;
  344. uint8_t rs = (instr & 0x0038) >> 3;
  345. uint8_t rd = (instr & 0x0007);
  346. /* Print decoding */
  347. UnwPrintd6("%s r%d, r%d, %c%d\t;",op ? "SUB" : "ADD",rd, rs,I ? '#' : 'r',rn);
  348. UnwPrintd5("r%d %s, r%d %s",rd, M_Origin2Str(state->regData[rd].o),rs, M_Origin2Str(state->regData[rs].o));
  349. if(!I) {
  350. UnwPrintd3(", r%d %s", rn, M_Origin2Str(state->regData[rn].o));
  351. /* Perform calculation */
  352. if(op) {
  353. state->regData[rd].v = state->regData[rs].v - state->regData[rn].v;
  354. }
  355. else {
  356. state->regData[rd].v = state->regData[rs].v + state->regData[rn].v;
  357. }
  358. /* Propagate the origin */
  359. if(M_IsOriginValid(state->regData[rs].v) &&
  360. M_IsOriginValid(state->regData[rn].v)) {
  361. state->regData[rd].o = state->regData[rs].o;
  362. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  363. }
  364. else {
  365. state->regData[rd].o = REG_VAL_INVALID;
  366. }
  367. }
  368. else {
  369. /* Perform calculation */
  370. if(op) {
  371. state->regData[rd].v = state->regData[rs].v - rn;
  372. }
  373. else {
  374. state->regData[rd].v = state->regData[rs].v + rn;
  375. }
  376. /* Propagate the origin */
  377. state->regData[rd].o = state->regData[rs].o;
  378. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  379. }
  380. }
  381. /* Format 3: move/compare/add/subtract immediate
  382. * MOV Rd, #Offset8
  383. * CMP Rd, #Offset8
  384. * ADD Rd, #Offset8
  385. * SUB Rd, #Offset8
  386. */
  387. else if((instr & 0xe000) == 0x2000) {
  388. uint8_t op = (instr & 0x1800) >> 11;
  389. uint8_t rd = (instr & 0x0700) >> 8;
  390. uint8_t offset8 = (instr & 0x00ff);
  391. switch(op) {
  392. case 0: /* MOV */
  393. UnwPrintd3("MOV r%d, #0x%x", rd, offset8);
  394. state->regData[rd].v = offset8;
  395. state->regData[rd].o = REG_VAL_FROM_CONST;
  396. break;
  397. case 1: /* CMP */
  398. /* Irrelevant to unwinding */
  399. UnwPrintd1("CMP ???");
  400. break;
  401. case 2: /* ADD */
  402. UnwPrintd5("ADD r%d, #0x%x\t; r%d %s", rd, offset8, rd, M_Origin2Str(state->regData[rd].o));
  403. state->regData[rd].v += offset8;
  404. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  405. break;
  406. case 3: /* SUB */
  407. UnwPrintd5("SUB r%d, #0x%d\t; r%d %s", rd, offset8, rd, M_Origin2Str(state->regData[rd].o));
  408. state->regData[rd].v -= offset8;
  409. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  410. break;
  411. }
  412. }
  413. /* Format 4: ALU operations
  414. * AND Rd, Rs
  415. * EOR Rd, Rs
  416. * LSL Rd, Rs
  417. * LSR Rd, Rs
  418. * ASR Rd, Rs
  419. * ADC Rd, Rs
  420. * SBC Rd, Rs
  421. * ROR Rd, Rs
  422. * TST Rd, Rs
  423. * NEG Rd, Rs
  424. * CMP Rd, Rs
  425. * CMN Rd, Rs
  426. * ORR Rd, Rs
  427. * MUL Rd, Rs
  428. * BIC Rd, Rs
  429. * MVN Rd, Rs
  430. */
  431. else if((instr & 0xfc00) == 0x4000) {
  432. uint8_t op = (instr & 0x03c0) >> 6;
  433. uint8_t rs = (instr & 0x0038) >> 3;
  434. uint8_t rd = (instr & 0x0007);
  435. #if defined(UNW_DEBUG)
  436. static const char * const mnu[16] = {
  437. "AND", "EOR", "LSL", "LSR",
  438. "ASR", "ADC", "SBC", "ROR",
  439. "TST", "NEG", "CMP", "CMN",
  440. "ORR", "MUL", "BIC", "MVN" };
  441. #endif
  442. /* Print the mnemonic and registers */
  443. switch(op) {
  444. case 0: /* AND */
  445. case 1: /* EOR */
  446. case 2: /* LSL */
  447. case 3: /* LSR */
  448. case 4: /* ASR */
  449. case 7: /* ROR */
  450. case 9: /* NEG */
  451. case 12: /* ORR */
  452. case 13: /* MUL */
  453. case 15: /* MVN */
  454. UnwPrintd8("%s r%d ,r%d\t; r%d %s, r%d %s",mnu[op],rd, rs, rd, M_Origin2Str(state->regData[rd].o), rs, M_Origin2Str(state->regData[rs].o));
  455. break;
  456. case 5: /* ADC */
  457. case 6: /* SBC */
  458. UnwPrintd4("%s r%d, r%d", mnu[op], rd, rs);
  459. break;
  460. case 8: /* TST */
  461. case 10: /* CMP */
  462. case 11: /* CMN */
  463. /* Irrelevant to unwinding */
  464. UnwPrintd2("%s ???", mnu[op]);
  465. break;
  466. case 14: /* BIC */
  467. UnwPrintd5("r%d ,r%d\t; r%d %s", rd, rs, rs, M_Origin2Str(state->regData[rs].o));
  468. break;
  469. }
  470. /* Perform operation */
  471. switch(op) {
  472. case 0: /* AND */
  473. state->regData[rd].v &= state->regData[rs].v;
  474. break;
  475. case 1: /* EOR */
  476. state->regData[rd].v ^= state->regData[rs].v;
  477. break;
  478. case 2: /* LSL */
  479. state->regData[rd].v <<= state->regData[rs].v;
  480. break;
  481. case 3: /* LSR */
  482. state->regData[rd].v >>= state->regData[rs].v;
  483. break;
  484. case 4: /* ASR */
  485. if(state->regData[rd].v & 0x80000000) {
  486. state->regData[rd].v >>= state->regData[rs].v;
  487. state->regData[rd].v |= 0xffffffff << (32 - state->regData[rs].v);
  488. }
  489. else {
  490. state->regData[rd].v >>= state->regData[rs].v;
  491. }
  492. break;
  493. case 5: /* ADC */
  494. case 6: /* SBC */
  495. case 8: /* TST */
  496. case 10: /* CMP */
  497. case 11: /* CMN */
  498. break;
  499. case 7: /* ROR */
  500. state->regData[rd].v = (state->regData[rd].v >> state->regData[rs].v) |
  501. (state->regData[rd].v << (32 - state->regData[rs].v));
  502. break;
  503. case 9: /* NEG */
  504. state->regData[rd].v = -state->regData[rs].v;
  505. break;
  506. case 12: /* ORR */
  507. state->regData[rd].v |= state->regData[rs].v;
  508. break;
  509. case 13: /* MUL */
  510. state->regData[rd].v *= state->regData[rs].v;
  511. break;
  512. case 14: /* BIC */
  513. state->regData[rd].v &= ~state->regData[rs].v;
  514. break;
  515. case 15: /* MVN */
  516. state->regData[rd].v = ~state->regData[rs].v;
  517. break;
  518. }
  519. /* Propagate data origins */
  520. switch(op) {
  521. case 0: /* AND */
  522. case 1: /* EOR */
  523. case 2: /* LSL */
  524. case 3: /* LSR */
  525. case 4: /* ASR */
  526. case 7: /* ROR */
  527. case 12: /* ORR */
  528. case 13: /* MUL */
  529. case 14: /* BIC */
  530. if(M_IsOriginValid(state->regData[rd].o) && M_IsOriginValid(state->regData[rs].o)) {
  531. state->regData[rd].o = state->regData[rs].o;
  532. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  533. }
  534. else {
  535. state->regData[rd].o = REG_VAL_INVALID;
  536. }
  537. break;
  538. case 5: /* ADC */
  539. case 6: /* SBC */
  540. /* C-bit not tracked */
  541. state->regData[rd].o = REG_VAL_INVALID;
  542. break;
  543. case 8: /* TST */
  544. case 10: /* CMP */
  545. case 11: /* CMN */
  546. /* Nothing propagated */
  547. break;
  548. case 9: /* NEG */
  549. case 15: /* MVN */
  550. state->regData[rd].o = state->regData[rs].o;
  551. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  552. break;
  553. }
  554. }
  555. /* Format 5: Hi register operations/branch exchange
  556. * ADD Rd, Hs
  557. * CMP Hd, Rs
  558. * MOV Hd, Hs
  559. */
  560. else if((instr & 0xfc00) == 0x4400) {
  561. uint8_t op = (instr & 0x0300) >> 8;
  562. bool h1 = (instr & 0x0080) ? true: false;
  563. bool h2 = (instr & 0x0040) ? true: false;
  564. uint8_t rhs = (instr & 0x0038) >> 3;
  565. uint8_t rhd = (instr & 0x0007);
  566. /* Adjust the register numbers */
  567. if(h2)
  568. rhs += 8;
  569. if(h1)
  570. rhd += 8;
  571. switch(op) {
  572. case 0: /* ADD */
  573. UnwPrintd5("ADD r%d, r%d\t; r%d %s", rhd, rhs, rhs, M_Origin2Str(state->regData[rhs].o));
  574. state->regData[rhd].v += state->regData[rhs].v;
  575. state->regData[rhd].o = state->regData[rhs].o;
  576. state->regData[rhd].o |= REG_VAL_ARITHMETIC;
  577. break;
  578. case 1: /* CMP */
  579. /* Irrelevant to unwinding */
  580. UnwPrintd1("CMP ???");
  581. break;
  582. case 2: /* MOV */
  583. UnwPrintd5("MOV r%d, r%d\t; r%d %s", rhd, rhs, rhd, M_Origin2Str(state->regData[rhs].o));
  584. state->regData[rhd].v = state->regData[rhs].v;
  585. state->regData[rhd].o = state->regData[rhd].o;
  586. break;
  587. case 3: /* BX */
  588. UnwPrintd4("BX r%d\t; r%d %s\n", rhs, rhs, M_Origin2Str(state->regData[rhs].o));
  589. /* Only follow BX if the data was from the stack */
  590. if(state->regData[rhs].o == REG_VAL_FROM_STACK) {
  591. UnwPrintd2(" Return PC=0x%x\n", state->regData[rhs].v & (~0x1));
  592. /* Report the return address, including mode bit */
  593. if(!UnwReportRetAddr(state, state->regData[rhs].v)) {
  594. return UNWIND_TRUNCATED;
  595. }
  596. /* Store return address in LR register */
  597. state->regData[14].v = state->regData[15].v + 2;
  598. state->regData[14].o = REG_VAL_FROM_CONST;
  599. /* Update the PC */
  600. state->regData[15].v = state->regData[rhs].v;
  601. /* Determine the new mode */
  602. if(state->regData[rhs].v & 0x1) {
  603. /* Branching to THUMB */
  604. /* Account for the auto-increment which isn't needed */
  605. state->regData[15].v -= 2;
  606. }
  607. else {
  608. /* Branch to ARM */
  609. return UnwStartArm(state);
  610. }
  611. }
  612. else {
  613. UnwPrintd4("\nError: BX to invalid register: r%d = 0x%x (%s)\n", rhs, state->regData[rhs].o, M_Origin2Str(state->regData[rhs].o));
  614. return UNWIND_FAILURE;
  615. }
  616. }
  617. }
  618. /* Format 9: PC-relative load
  619. * LDR Rd,[PC, #imm]
  620. */
  621. else if((instr & 0xf800) == 0x4800) {
  622. uint8_t rd = (instr & 0x0700) >> 8;
  623. uint8_t word8 = (instr & 0x00ff);
  624. uint32_t address;
  625. /* Compute load address, adding a word to account for prefetch */
  626. address = (state->regData[15].v & (~0x3)) + 4 + (word8 << 2);
  627. UnwPrintd3("LDR r%d, 0x%08x", rd, address);
  628. if(!UnwMemReadRegister(state, address, &state->regData[rd])) {
  629. return UNWIND_DREAD_W_FAIL;
  630. }
  631. }
  632. /* Format 13: add offset to Stack Pointer
  633. * ADD sp,#+imm
  634. * ADD sp,#-imm
  635. */
  636. else if((instr & 0xff00) == 0xB000) {
  637. uint8_t value = (instr & 0x7f) * 4;
  638. /* Check the negative bit */
  639. if((instr & 0x80) != 0) {
  640. UnwPrintd2("SUB sp,#0x%x", value);
  641. state->regData[13].v -= value;
  642. }
  643. else {
  644. UnwPrintd2("ADD sp,#0x%x", value);
  645. state->regData[13].v += value;
  646. }
  647. }
  648. /* Format 14: push/pop registers
  649. * PUSH {Rlist}
  650. * PUSH {Rlist, LR}
  651. * POP {Rlist}
  652. * POP {Rlist, PC}
  653. */
  654. else if((instr & 0xf600) == 0xb400) {
  655. bool L = (instr & 0x0800) ? true : false;
  656. bool R = (instr & 0x0100) ? true : false;
  657. uint8_t rList = (instr & 0x00ff);
  658. if(L) {
  659. uint8_t r;
  660. /* Load from memory: POP */
  661. UnwPrintd2("POP {Rlist%s}\n", R ? ", PC" : "");
  662. for(r = 0; r < 8; r++) {
  663. if(rList & (0x1 << r)) {
  664. /* Read the word */
  665. if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r])) {
  666. return UNWIND_DREAD_W_FAIL;
  667. }
  668. /* Alter the origin to be from the stack if it was valid */
  669. if(M_IsOriginValid(state->regData[r].o)) {
  670. state->regData[r].o = REG_VAL_FROM_STACK;
  671. }
  672. state->regData[13].v += 4;
  673. UnwPrintd3(" r%d = 0x%08x\n", r, state->regData[r].v);
  674. }
  675. }
  676. /* Check if the PC is to be popped */
  677. if(R) {
  678. /* Get the return address */
  679. if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[15])) {
  680. return UNWIND_DREAD_W_FAIL;
  681. }
  682. /* Alter the origin to be from the stack if it was valid */
  683. if(!M_IsOriginValid(state->regData[15].o)) {
  684. /* Return address is not valid */
  685. UnwPrintd1("PC popped with invalid address\n");
  686. return UNWIND_FAILURE;
  687. }
  688. else {
  689. /* The bottom bit should have been set to indicate that
  690. * the caller was from Thumb. This would allow return
  691. * by BX for interworking APCS.
  692. */
  693. if((state->regData[15].v & 0x1) == 0) {
  694. UnwPrintd2("Warning: Return address not to Thumb: 0x%08x\n", state->regData[15].v);
  695. /* Pop into the PC will not switch mode */
  696. return UNWIND_INCONSISTENT;
  697. }
  698. /* Store the return address */
  699. if(!UnwReportRetAddr(state, state->regData[15].v)) {
  700. return UNWIND_TRUNCATED;
  701. }
  702. /* Now have the return address */
  703. UnwPrintd2(" Return PC=%x\n", state->regData[15].v);
  704. /* Update the pc */
  705. state->regData[13].v += 4;
  706. /* Compensate for the auto-increment, which isn't needed here */
  707. state->regData[15].v -= 2;
  708. }
  709. }
  710. }
  711. else {
  712. int8_t r;
  713. /* Store to memory: PUSH */
  714. UnwPrintd2("PUSH {Rlist%s}", R ? ", LR" : "");
  715. /* Check if the LR is to be pushed */
  716. if(R) {
  717. UnwPrintd3("\n lr = 0x%08x\t; %s", state->regData[14].v, M_Origin2Str(state->regData[14].o));
  718. state->regData[13].v -= 4;
  719. /* Write the register value to memory */
  720. if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[14])) {
  721. return UNWIND_DWRITE_W_FAIL;
  722. }
  723. }
  724. for(r = 7; r >= 0; r--) {
  725. if(rList & (0x1 << r)) {
  726. UnwPrintd4("\n r%d = 0x%08x\t; %s", r, state->regData[r].v, M_Origin2Str(state->regData[r].o));
  727. state->regData[13].v -= 4;
  728. if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
  729. return UNWIND_DWRITE_W_FAIL;
  730. }
  731. }
  732. }
  733. }
  734. }
  735. /*
  736. * Conditional branches
  737. * Bcond
  738. */
  739. else if((instr & 0xf000) == 0xd000) {
  740. int32_t branchValue = (instr & 0xff);
  741. if (branchValue & 0x80) branchValue |= 0xffffff00;
  742. /* Branch distance is twice that specified in the instruction. */
  743. branchValue *= 2;
  744. UnwPrintd2("Bcond %d \n", branchValue);
  745. /* Only take the branch if a loop was detected */
  746. if (loopDetected) {
  747. /* Update PC */
  748. state->regData[15].v += branchValue;
  749. /* Need to advance by a word to account for pre-fetch.
  750. * Advance by a half word here, allowing the normal address
  751. * advance to account for the other half word.
  752. */
  753. state->regData[15].v += 2;
  754. /* Display PC of next instruction */
  755. UnwPrintd2(" New PC=%x", state->regData[15].v + 2);
  756. }
  757. }
  758. /* Format 18: unconditional branch
  759. * B label
  760. */
  761. else if((instr & 0xf800) == 0xe000) {
  762. uint32_t v;
  763. int16_t branchValue = signExtend11(instr & 0x07ff);
  764. /* Branch distance is twice that specified in the instruction. */
  765. branchValue *= 2;
  766. UnwPrintd2("B %d \n", branchValue);
  767. /* Update PC */
  768. state->regData[15].v += branchValue;
  769. /* Need to advance by a word to account for pre-fetch.
  770. * Advance by a half word here, allowing the normal address
  771. * advance to account for the other half word.
  772. */
  773. state->regData[15].v += 2;
  774. /* Compute the jump address */
  775. v = state->regData[15].v + 2;
  776. /* Display PC of next instruction */
  777. UnwPrintd2(" New PC=%x", v);
  778. /* Did we detect an infinite loop ? */
  779. loopDetected = lastJumpAddr == v;
  780. /* Remember the last address we jumped to */
  781. lastJumpAddr = v;
  782. }
  783. else {
  784. UnwPrintd1("????");
  785. /* Unknown/undecoded. May alter some register, so invalidate file */
  786. UnwInvalidateRegisterFile(state->regData);
  787. }
  788. UnwPrintd1("\n");
  789. /* Should never hit the reset vector */
  790. if(state->regData[15].v == 0)
  791. return UNWIND_RESET;
  792. /* Check next address */
  793. state->regData[15].v += 2;
  794. /* Garbage collect the memory hash (used only for the stack) */
  795. UnwMemHashGC(state);
  796. t--;
  797. if(t == 0)
  798. return UNWIND_EXHAUSTED;
  799. } while(!found);
  800. return UNWIND_SUCCESS;
  801. }
  802. #endif