My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

unwarm_thumb.cpp 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. #if defined(__arm__) || defined(__thumb__)
  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 int32_t signExtend11(uint16_t value) {
  25. if(value & 0x400) {
  26. value |= 0xfffff800;
  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. * TBB / TBH
  191. */
  192. else if ((instr & 0xfff0) == 0xe8d0 && (instr2 & 0xffe0) == 0xf000) {
  193. /* We are only interested in
  194. * the forms
  195. * TBB [PC, ...]
  196. * TBH [PC, ..., LSL #1]
  197. * as those are used by the C compiler to implement
  198. * the switch clauses
  199. */
  200. uint8_t rn = instr & 0xf;
  201. uint8_t rm = instr2 & 0xf;
  202. bool H = (instr2 & 0x10) ? true : false;
  203. UnwPrintd5("TB%c [r%d,r%d%s]\n", H ? 'H' : 'B', rn, rm, H ? ",LSL #1" : "");
  204. // We are only interested if the RN is the PC. Let's choose the 1st destination
  205. if (rn == 15) {
  206. if (H) {
  207. uint16_t rv;
  208. if(!state->cb->readH((state->regData[15].v & (~1)) + 2, &rv)) {
  209. return UNWIND_DREAD_H_FAIL;
  210. }
  211. state->regData[15].v += rv * 2;
  212. } else {
  213. uint8_t rv;
  214. if(!state->cb->readB((state->regData[15].v & (~1)) + 2, &rv)) {
  215. return UNWIND_DREAD_B_FAIL;
  216. }
  217. state->regData[15].v += rv * 2;
  218. }
  219. }
  220. }
  221. /*
  222. * Unconditional branch
  223. */
  224. else if ((instr & 0xf800) == 0xf000 && (instr2 & 0xd000) == 0x9000) {
  225. uint32_t v;
  226. uint8_t S = (instr & 0x400) >> 10;
  227. uint16_t imm10 = (instr & 0x3ff);
  228. uint8_t J1 = (instr2 & 0x2000) >> 13;
  229. uint8_t J2 = (instr2 & 0x0800) >> 11;
  230. uint16_t imm11 = (instr2 & 0x7ff);
  231. uint8_t I1 = J1 ^ S ^ 1;
  232. uint8_t I2 = J2 ^ S ^ 1;
  233. uint32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) |(imm10 << 12) | (imm11 << 1);
  234. if (S) imm32 |= 0xfe000000;
  235. UnwPrintd2("B %d \n", imm32);
  236. /* Update PC */
  237. state->regData[15].v += imm32;
  238. /* Need to advance by a word to account for pre-fetch.
  239. * Advance by a half word here, allowing the normal address
  240. * advance to account for the other half word.
  241. */
  242. state->regData[15].v += 2;
  243. /* Compute the jump address */
  244. v = state->regData[15].v + 2;
  245. /* Display PC of next instruction */
  246. UnwPrintd2(" New PC=%x", v);
  247. /* Did we detect an infinite loop ? */
  248. loopDetected = lastJumpAddr == v;
  249. /* Remember the last address we jumped to */
  250. lastJumpAddr = v;
  251. }
  252. /*
  253. * Branch with link
  254. */
  255. else if ((instr & 0xf800) == 0xf000 && (instr2 & 0xd000) == 0xd000) {
  256. uint8_t S = (instr & 0x400) >> 10;
  257. uint16_t imm10 = (instr & 0x3ff);
  258. uint8_t J1 = (instr2 & 0x2000) >> 13;
  259. uint8_t J2 = (instr2 & 0x0800) >> 11;
  260. uint16_t imm11 = (instr2 & 0x7ff);
  261. uint8_t I1 = J1 ^ S ^ 1;
  262. uint8_t I2 = J2 ^ S ^ 1;
  263. uint32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) |(imm10 << 12) | (imm11 << 1);
  264. if (S) imm32 |= 0xfe000000;
  265. UnwPrintd2("BL %d \n", imm32);
  266. /* Never taken, as we are unwinding the stack */
  267. if (0) {
  268. /* Store return address in LR register */
  269. state->regData[14].v = state->regData[15].v + 2;
  270. state->regData[14].o = REG_VAL_FROM_CONST;
  271. /* Update PC */
  272. state->regData[15].v += imm32;
  273. /* Need to advance by a word to account for pre-fetch.
  274. * Advance by a half word here, allowing the normal address
  275. * advance to account for the other half word.
  276. */
  277. state->regData[15].v += 2;
  278. /* Display PC of next instruction */
  279. UnwPrintd2(" Return PC=%x", state->regData[15].v);
  280. /* Report the return address, including mode bit */
  281. if(!UnwReportRetAddr(state, state->regData[15].v)) {
  282. return UNWIND_TRUNCATED;
  283. }
  284. /* Determine the new mode */
  285. if(state->regData[15].v & 0x1) {
  286. /* Branching to THUMB */
  287. /* Account for the auto-increment which isn't needed */
  288. state->regData[15].v -= 2;
  289. }
  290. else {
  291. /* Branch to ARM */
  292. return UnwStartArm(state);
  293. }
  294. }
  295. }
  296. /*
  297. * Conditional branches. Usually not taken, unless infinite loop is detected
  298. */
  299. else if ((instr & 0xf800) == 0xf000 && (instr2 & 0xd000) == 0x8000) {
  300. uint8_t S = (instr & 0x400) >> 10;
  301. uint16_t imm6 = (instr & 0x3f);
  302. uint8_t J1 = (instr2 & 0x2000) >> 13;
  303. uint8_t J2 = (instr2 & 0x0800) >> 11;
  304. uint16_t imm11 = (instr2 & 0x7ff);
  305. uint8_t I1 = J1 ^ S ^ 1;
  306. uint8_t I2 = J2 ^ S ^ 1;
  307. uint32_t imm32 = (S << 20) | (I1 << 19) | (I2 << 18) |(imm6 << 12) | (imm11 << 1);
  308. if (S) imm32 |= 0xffe00000;
  309. UnwPrintd2("Bcond %d\n", imm32);
  310. /* Take the jump only if a loop is detected */
  311. if (loopDetected) {
  312. /* Update PC */
  313. state->regData[15].v += imm32;
  314. /* Need to advance by a word to account for pre-fetch.
  315. * Advance by a half word here, allowing the normal address
  316. * advance to account for the other half word.
  317. */
  318. state->regData[15].v += 2;
  319. /* Display PC of next instruction */
  320. UnwPrintd2(" New PC=%x", state->regData[15].v + 2);
  321. }
  322. }
  323. /*
  324. * PC-relative load
  325. * LDR Rd,[PC, #+/-imm]
  326. */
  327. else if((instr & 0xff7f) == 0xf85f) {
  328. uint8_t rt = (instr2 & 0xf000) >> 12;
  329. uint8_t imm12 = (instr2 & 0x0fff);
  330. bool A = (instr & 0x80) ? true : false;
  331. uint32_t address;
  332. /* Compute load address, adding a word to account for prefetch */
  333. address = (state->regData[15].v & (~0x3)) + 4;
  334. if (A) address += imm12;
  335. else address -= imm12;
  336. UnwPrintd4("LDR r%d,[PC #%c0x%08x]", rt, A?'+':'-', address);
  337. if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
  338. return UNWIND_DREAD_W_FAIL;
  339. }
  340. }
  341. /*
  342. * LDR immediate.
  343. * We are only interested when destination is PC.
  344. * LDR Rt,[Rn , #n]
  345. */
  346. else if ((instr & 0xfff0) == 0xf8d0) {
  347. uint8_t rn = (instr & 0xf);
  348. uint8_t rt = (instr2 & 0xf000) >> 12;
  349. uint16_t imm12 = (instr2 & 0xfff);
  350. /* If destination is PC and we don't know the source value, then fail */
  351. if (!M_IsOriginValid(state->regData[rn].o)) {
  352. state->regData[rt].o = state->regData[rn].o;
  353. } else {
  354. uint32_t address = state->regData[rn].v + imm12;
  355. if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
  356. return UNWIND_DREAD_W_FAIL;
  357. }
  358. }
  359. }
  360. /*
  361. * LDR immediate
  362. * We are only interested when destination is PC.
  363. * LDR Rt,[Rn , #-n]
  364. * LDR Rt,[Rn], #+/-n]
  365. * LDR Rt,[Rn, #+/-n]!
  366. */
  367. else if ((instr & 0xfff0) == 0xf850 && (instr2 & 0x0800) == 0x0800) {
  368. uint8_t rn = (instr & 0xf);
  369. uint8_t rt = (instr2 & 0xf000) >> 12;
  370. uint16_t imm8 = (instr2 & 0xff);
  371. bool P = (instr2 & 0x400) ? true : false;
  372. bool U = (instr2 & 0x200) ? true : false;
  373. bool W = (instr2 & 0x100) ? true : false;
  374. if (!M_IsOriginValid(state->regData[rn].o)) {
  375. state->regData[rt].o = state->regData[rn].o;
  376. } else {
  377. uint32_t offaddress = state->regData[rn].v + imm8;
  378. if (U) offaddress += imm8;
  379. else offaddress -= imm8;
  380. uint32_t address;
  381. if (P) {
  382. address = offaddress;
  383. } else {
  384. address = state->regData[rn].v;
  385. }
  386. if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
  387. return UNWIND_DREAD_W_FAIL;
  388. }
  389. if (W) {
  390. state->regData[rn].v = offaddress;
  391. }
  392. }
  393. }
  394. /*
  395. * LDR (register).
  396. * We are interested in the form
  397. * ldr Rt, [Rn, Rm, lsl #x]
  398. * Where Rt is PC, Rn value is known, Rm is not known or unknown
  399. */
  400. else if ((instr & 0xfff0) == 0xf850 && (instr2 & 0x0fc0) == 0x0000) {
  401. uint8_t rn = (instr & 0xf);
  402. uint8_t rt = (instr2 & 0xf000) >> 12;
  403. uint8_t rm = (instr2 & 0xf);
  404. uint8_t imm2 = (instr2 & 0x30) >> 4;
  405. if (!M_IsOriginValid(state->regData[rn].o) ||
  406. !M_IsOriginValid(state->regData[rm].o)) {
  407. /* If Rt is PC, and Rn is known, then do an exception and assume
  408. Rm equals 0 => This takes the first case in a switch() */
  409. if (rt == 15 && M_IsOriginValid(state->regData[rn].o)) {
  410. uint32_t address = state->regData[rn].v;
  411. if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
  412. return UNWIND_DREAD_W_FAIL;
  413. }
  414. } else {
  415. /* Propagate unknown value */
  416. state->regData[rt].o = state->regData[rn].o;
  417. }
  418. } else {
  419. uint32_t address = state->regData[rn].v + (state->regData[rm].v << imm2);
  420. if(!UnwMemReadRegister(state, address, &state->regData[rt])) {
  421. return UNWIND_DREAD_W_FAIL;
  422. }
  423. }
  424. }
  425. else {
  426. UnwPrintd1("???? (32)");
  427. /* Unknown/undecoded. May alter some register, so invalidate file */
  428. UnwInvalidateRegisterFile(state->regData);
  429. }
  430. /* End of thumb 32bit code */
  431. }
  432. /* Format 1: Move shifted register
  433. * LSL Rd, Rs, #Offset5
  434. * LSR Rd, Rs, #Offset5
  435. * ASR Rd, Rs, #Offset5
  436. */
  437. else if((instr & 0xe000) == 0x0000 && (instr & 0x1800) != 0x1800) {
  438. bool signExtend;
  439. uint8_t op = (instr & 0x1800) >> 11;
  440. uint8_t offset5 = (instr & 0x07c0) >> 6;
  441. uint8_t rs = (instr & 0x0038) >> 3;
  442. uint8_t rd = (instr & 0x0007);
  443. switch(op) {
  444. case 0: /* LSL */
  445. UnwPrintd6("LSL r%d, r%d, #%d\t; r%d %s", rd, rs, offset5, rs, M_Origin2Str(state->regData[rs].o));
  446. state->regData[rd].v = state->regData[rs].v << offset5;
  447. state->regData[rd].o = state->regData[rs].o;
  448. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  449. break;
  450. case 1: /* LSR */
  451. UnwPrintd6("LSR r%d, r%d, #%d\t; r%d %s", rd, rs, offset5, rs, M_Origin2Str(state->regData[rs].o));
  452. state->regData[rd].v = state->regData[rs].v >> offset5;
  453. state->regData[rd].o = state->regData[rs].o;
  454. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  455. break;
  456. case 2: /* ASR */
  457. UnwPrintd6("ASL r%d, r%d, #%d\t; r%d %s", rd, rs, offset5, rs, M_Origin2Str(state->regData[rs].o));
  458. signExtend = (state->regData[rs].v & 0x8000) ? true : false;
  459. state->regData[rd].v = state->regData[rs].v >> offset5;
  460. if(signExtend) {
  461. state->regData[rd].v |= 0xffffffff << (32 - offset5);
  462. }
  463. state->regData[rd].o = state->regData[rs].o;
  464. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  465. break;
  466. }
  467. }
  468. /* Format 2: add/subtract
  469. * ADD Rd, Rs, Rn
  470. * ADD Rd, Rs, #Offset3
  471. * SUB Rd, Rs, Rn
  472. * SUB Rd, Rs, #Offset3
  473. */
  474. else if((instr & 0xf800) == 0x1800) {
  475. bool I = (instr & 0x0400) ? true : false;
  476. bool op = (instr & 0x0200) ? true : false;
  477. uint8_t rn = (instr & 0x01c0) >> 6;
  478. uint8_t rs = (instr & 0x0038) >> 3;
  479. uint8_t rd = (instr & 0x0007);
  480. /* Print decoding */
  481. UnwPrintd6("%s r%d, r%d, %c%d\t;",op ? "SUB" : "ADD",rd, rs,I ? '#' : 'r',rn);
  482. UnwPrintd5("r%d %s, r%d %s",rd, M_Origin2Str(state->regData[rd].o),rs, M_Origin2Str(state->regData[rs].o));
  483. if(!I) {
  484. UnwPrintd3(", r%d %s", rn, M_Origin2Str(state->regData[rn].o));
  485. /* Perform calculation */
  486. if(op) {
  487. state->regData[rd].v = state->regData[rs].v - state->regData[rn].v;
  488. }
  489. else {
  490. state->regData[rd].v = state->regData[rs].v + state->regData[rn].v;
  491. }
  492. /* Propagate the origin */
  493. if(M_IsOriginValid(state->regData[rs].o) &&
  494. M_IsOriginValid(state->regData[rn].o)) {
  495. state->regData[rd].o = state->regData[rs].o;
  496. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  497. }
  498. else {
  499. state->regData[rd].o = REG_VAL_INVALID;
  500. }
  501. }
  502. else {
  503. /* Perform calculation */
  504. if(op) {
  505. state->regData[rd].v = state->regData[rs].v - rn;
  506. }
  507. else {
  508. state->regData[rd].v = state->regData[rs].v + rn;
  509. }
  510. /* Propagate the origin */
  511. state->regData[rd].o = state->regData[rs].o;
  512. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  513. }
  514. }
  515. /* Format 3: move/compare/add/subtract immediate
  516. * MOV Rd, #Offset8
  517. * CMP Rd, #Offset8
  518. * ADD Rd, #Offset8
  519. * SUB Rd, #Offset8
  520. */
  521. else if((instr & 0xe000) == 0x2000) {
  522. uint8_t op = (instr & 0x1800) >> 11;
  523. uint8_t rd = (instr & 0x0700) >> 8;
  524. uint8_t offset8 = (instr & 0x00ff);
  525. switch(op) {
  526. case 0: /* MOV */
  527. UnwPrintd3("MOV r%d, #0x%x", rd, offset8);
  528. state->regData[rd].v = offset8;
  529. state->regData[rd].o = REG_VAL_FROM_CONST;
  530. break;
  531. case 1: /* CMP */
  532. /* Irrelevant to unwinding */
  533. UnwPrintd1("CMP ???");
  534. break;
  535. case 2: /* ADD */
  536. UnwPrintd5("ADD r%d, #0x%x\t; r%d %s", rd, offset8, rd, M_Origin2Str(state->regData[rd].o));
  537. state->regData[rd].v += offset8;
  538. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  539. break;
  540. case 3: /* SUB */
  541. UnwPrintd5("SUB r%d, #0x%d\t; r%d %s", rd, offset8, rd, M_Origin2Str(state->regData[rd].o));
  542. state->regData[rd].v -= offset8;
  543. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  544. break;
  545. }
  546. }
  547. /* Format 4: ALU operations
  548. * AND Rd, Rs
  549. * EOR Rd, Rs
  550. * LSL Rd, Rs
  551. * LSR Rd, Rs
  552. * ASR Rd, Rs
  553. * ADC Rd, Rs
  554. * SBC Rd, Rs
  555. * ROR Rd, Rs
  556. * TST Rd, Rs
  557. * NEG Rd, Rs
  558. * CMP Rd, Rs
  559. * CMN Rd, Rs
  560. * ORR Rd, Rs
  561. * MUL Rd, Rs
  562. * BIC Rd, Rs
  563. * MVN Rd, Rs
  564. */
  565. else if((instr & 0xfc00) == 0x4000) {
  566. uint8_t op = (instr & 0x03c0) >> 6;
  567. uint8_t rs = (instr & 0x0038) >> 3;
  568. uint8_t rd = (instr & 0x0007);
  569. #if defined(UNW_DEBUG)
  570. static const char * const mnu[16] = {
  571. "AND", "EOR", "LSL", "LSR",
  572. "ASR", "ADC", "SBC", "ROR",
  573. "TST", "NEG", "CMP", "CMN",
  574. "ORR", "MUL", "BIC", "MVN" };
  575. #endif
  576. /* Print the mnemonic and registers */
  577. switch(op) {
  578. case 0: /* AND */
  579. case 1: /* EOR */
  580. case 2: /* LSL */
  581. case 3: /* LSR */
  582. case 4: /* ASR */
  583. case 7: /* ROR */
  584. case 9: /* NEG */
  585. case 12: /* ORR */
  586. case 13: /* MUL */
  587. case 15: /* MVN */
  588. 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));
  589. break;
  590. case 5: /* ADC */
  591. case 6: /* SBC */
  592. UnwPrintd4("%s r%d, r%d", mnu[op], rd, rs);
  593. break;
  594. case 8: /* TST */
  595. case 10: /* CMP */
  596. case 11: /* CMN */
  597. /* Irrelevant to unwinding */
  598. UnwPrintd2("%s ???", mnu[op]);
  599. break;
  600. case 14: /* BIC */
  601. UnwPrintd5("r%d ,r%d\t; r%d %s", rd, rs, rs, M_Origin2Str(state->regData[rs].o));
  602. break;
  603. }
  604. /* Perform operation */
  605. switch(op) {
  606. case 0: /* AND */
  607. state->regData[rd].v &= state->regData[rs].v;
  608. break;
  609. case 1: /* EOR */
  610. state->regData[rd].v ^= state->regData[rs].v;
  611. break;
  612. case 2: /* LSL */
  613. state->regData[rd].v <<= state->regData[rs].v;
  614. break;
  615. case 3: /* LSR */
  616. state->regData[rd].v >>= state->regData[rs].v;
  617. break;
  618. case 4: /* ASR */
  619. if(state->regData[rd].v & 0x80000000) {
  620. state->regData[rd].v >>= state->regData[rs].v;
  621. state->regData[rd].v |= 0xffffffff << (32 - state->regData[rs].v);
  622. }
  623. else {
  624. state->regData[rd].v >>= state->regData[rs].v;
  625. }
  626. break;
  627. case 5: /* ADC */
  628. case 6: /* SBC */
  629. case 8: /* TST */
  630. case 10: /* CMP */
  631. case 11: /* CMN */
  632. break;
  633. case 7: /* ROR */
  634. state->regData[rd].v = (state->regData[rd].v >> state->regData[rs].v) |
  635. (state->regData[rd].v << (32 - state->regData[rs].v));
  636. break;
  637. case 9: /* NEG */
  638. state->regData[rd].v = -state->regData[rs].v;
  639. break;
  640. case 12: /* ORR */
  641. state->regData[rd].v |= state->regData[rs].v;
  642. break;
  643. case 13: /* MUL */
  644. state->regData[rd].v *= state->regData[rs].v;
  645. break;
  646. case 14: /* BIC */
  647. state->regData[rd].v &= ~state->regData[rs].v;
  648. break;
  649. case 15: /* MVN */
  650. state->regData[rd].v = ~state->regData[rs].v;
  651. break;
  652. }
  653. /* Propagate data origins */
  654. switch(op) {
  655. case 0: /* AND */
  656. case 1: /* EOR */
  657. case 2: /* LSL */
  658. case 3: /* LSR */
  659. case 4: /* ASR */
  660. case 7: /* ROR */
  661. case 12: /* ORR */
  662. case 13: /* MUL */
  663. case 14: /* BIC */
  664. if(M_IsOriginValid(state->regData[rd].o) && M_IsOriginValid(state->regData[rs].o)) {
  665. state->regData[rd].o = state->regData[rs].o;
  666. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  667. }
  668. else {
  669. state->regData[rd].o = REG_VAL_INVALID;
  670. }
  671. break;
  672. case 5: /* ADC */
  673. case 6: /* SBC */
  674. /* C-bit not tracked */
  675. state->regData[rd].o = REG_VAL_INVALID;
  676. break;
  677. case 8: /* TST */
  678. case 10: /* CMP */
  679. case 11: /* CMN */
  680. /* Nothing propagated */
  681. break;
  682. case 9: /* NEG */
  683. case 15: /* MVN */
  684. state->regData[rd].o = state->regData[rs].o;
  685. state->regData[rd].o |= REG_VAL_ARITHMETIC;
  686. break;
  687. }
  688. }
  689. /* Format 5: Hi register operations/branch exchange
  690. * ADD Rd, Hs
  691. * CMP Hd, Rs
  692. * MOV Hd, Hs
  693. */
  694. else if((instr & 0xfc00) == 0x4400) {
  695. uint8_t op = (instr & 0x0300) >> 8;
  696. bool h1 = (instr & 0x0080) ? true: false;
  697. bool h2 = (instr & 0x0040) ? true: false;
  698. uint8_t rhs = (instr & 0x0038) >> 3;
  699. uint8_t rhd = (instr & 0x0007);
  700. /* Adjust the register numbers */
  701. if(h2)
  702. rhs += 8;
  703. if(h1)
  704. rhd += 8;
  705. switch(op) {
  706. case 0: /* ADD */
  707. UnwPrintd5("ADD r%d, r%d\t; r%d %s", rhd, rhs, rhs, M_Origin2Str(state->regData[rhs].o));
  708. state->regData[rhd].v += state->regData[rhs].v;
  709. state->regData[rhd].o = state->regData[rhs].o;
  710. state->regData[rhd].o |= REG_VAL_ARITHMETIC;
  711. break;
  712. case 1: /* CMP */
  713. /* Irrelevant to unwinding */
  714. UnwPrintd1("CMP ???");
  715. break;
  716. case 2: /* MOV */
  717. UnwPrintd5("MOV r%d, r%d\t; r%d %s", rhd, rhs, rhd, M_Origin2Str(state->regData[rhs].o));
  718. state->regData[rhd].v = state->regData[rhs].v;
  719. state->regData[rhd].o = state->regData[rhd].o;
  720. break;
  721. case 3: /* BX */
  722. UnwPrintd4("BX r%d\t; r%d %s\n", rhs, rhs, M_Origin2Str(state->regData[rhs].o));
  723. /* Only follow BX if the data was from the stack or BX LR */
  724. if(rhs == 14 || state->regData[rhs].o == REG_VAL_FROM_STACK) {
  725. UnwPrintd2(" Return PC=0x%x\n", state->regData[rhs].v & (~0x1));
  726. /* Report the return address, including mode bit */
  727. if(!UnwReportRetAddr(state, state->regData[rhs].v)) {
  728. return UNWIND_TRUNCATED;
  729. }
  730. /* Update the PC */
  731. state->regData[15].v = state->regData[rhs].v;
  732. /* Determine the new mode */
  733. if(state->regData[rhs].v & 0x1) {
  734. /* Branching to THUMB */
  735. /* Account for the auto-increment which isn't needed */
  736. state->regData[15].v -= 2;
  737. }
  738. else {
  739. /* Branch to ARM */
  740. return UnwStartArm(state);
  741. }
  742. }
  743. else {
  744. UnwPrintd4("\nError: BX to invalid register: r%d = 0x%x (%s)\n", rhs, state->regData[rhs].o, M_Origin2Str(state->regData[rhs].o));
  745. return UNWIND_FAILURE;
  746. }
  747. }
  748. }
  749. /* Format 9: PC-relative load
  750. * LDR Rd,[PC, #imm]
  751. */
  752. else if((instr & 0xf800) == 0x4800) {
  753. uint8_t rd = (instr & 0x0700) >> 8;
  754. uint8_t word8 = (instr & 0x00ff);
  755. uint32_t address;
  756. /* Compute load address, adding a word to account for prefetch */
  757. address = (state->regData[15].v & (~0x3)) + 4 + (word8 << 2);
  758. UnwPrintd3("LDR r%d, 0x%08x", rd, address);
  759. if(!UnwMemReadRegister(state, address, &state->regData[rd])) {
  760. return UNWIND_DREAD_W_FAIL;
  761. }
  762. }
  763. /* Format 13: add offset to Stack Pointer
  764. * ADD sp,#+imm
  765. * ADD sp,#-imm
  766. */
  767. else if((instr & 0xff00) == 0xB000) {
  768. uint8_t value = (instr & 0x7f) * 4;
  769. /* Check the negative bit */
  770. if((instr & 0x80) != 0) {
  771. UnwPrintd2("SUB sp,#0x%x", value);
  772. state->regData[13].v -= value;
  773. }
  774. else {
  775. UnwPrintd2("ADD sp,#0x%x", value);
  776. state->regData[13].v += value;
  777. }
  778. }
  779. /* Format 14: push/pop registers
  780. * PUSH {Rlist}
  781. * PUSH {Rlist, LR}
  782. * POP {Rlist}
  783. * POP {Rlist, PC}
  784. */
  785. else if((instr & 0xf600) == 0xb400) {
  786. bool L = (instr & 0x0800) ? true : false;
  787. bool R = (instr & 0x0100) ? true : false;
  788. uint8_t rList = (instr & 0x00ff);
  789. if(L) {
  790. uint8_t r;
  791. /* Load from memory: POP */
  792. UnwPrintd2("POP {Rlist%s}\n", R ? ", PC" : "");
  793. for(r = 0; r < 8; r++) {
  794. if(rList & (0x1 << r)) {
  795. /* Read the word */
  796. if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[r])) {
  797. return UNWIND_DREAD_W_FAIL;
  798. }
  799. /* Alter the origin to be from the stack if it was valid */
  800. if(M_IsOriginValid(state->regData[r].o)) {
  801. state->regData[r].o = REG_VAL_FROM_STACK;
  802. }
  803. state->regData[13].v += 4;
  804. UnwPrintd3(" r%d = 0x%08x\n", r, state->regData[r].v);
  805. }
  806. }
  807. /* Check if the PC is to be popped */
  808. if(R) {
  809. /* Get the return address */
  810. if(!UnwMemReadRegister(state, state->regData[13].v, &state->regData[15])) {
  811. return UNWIND_DREAD_W_FAIL;
  812. }
  813. /* Alter the origin to be from the stack if it was valid */
  814. if(!M_IsOriginValid(state->regData[15].o)) {
  815. /* Return address is not valid */
  816. UnwPrintd1("PC popped with invalid address\n");
  817. return UNWIND_FAILURE;
  818. }
  819. else {
  820. /* The bottom bit should have been set to indicate that
  821. * the caller was from Thumb. This would allow return
  822. * by BX for interworking APCS.
  823. */
  824. if((state->regData[15].v & 0x1) == 0) {
  825. UnwPrintd2("Warning: Return address not to Thumb: 0x%08x\n", state->regData[15].v);
  826. /* Pop into the PC will not switch mode */
  827. return UNWIND_INCONSISTENT;
  828. }
  829. /* Store the return address */
  830. if(!UnwReportRetAddr(state, state->regData[15].v)) {
  831. return UNWIND_TRUNCATED;
  832. }
  833. /* Now have the return address */
  834. UnwPrintd2(" Return PC=%x\n", state->regData[15].v);
  835. /* Update the pc */
  836. state->regData[13].v += 4;
  837. /* Compensate for the auto-increment, which isn't needed here */
  838. state->regData[15].v -= 2;
  839. }
  840. }
  841. }
  842. else {
  843. int8_t r;
  844. /* Store to memory: PUSH */
  845. UnwPrintd2("PUSH {Rlist%s}", R ? ", LR" : "");
  846. /* Check if the LR is to be pushed */
  847. if(R) {
  848. UnwPrintd3("\n lr = 0x%08x\t; %s", state->regData[14].v, M_Origin2Str(state->regData[14].o));
  849. state->regData[13].v -= 4;
  850. /* Write the register value to memory */
  851. if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[14])) {
  852. return UNWIND_DWRITE_W_FAIL;
  853. }
  854. }
  855. for(r = 7; r >= 0; r--) {
  856. if(rList & (0x1 << r)) {
  857. UnwPrintd4("\n r%d = 0x%08x\t; %s", r, state->regData[r].v, M_Origin2Str(state->regData[r].o));
  858. state->regData[13].v -= 4;
  859. if(!UnwMemWriteRegister(state, state->regData[13].v, &state->regData[r])) {
  860. return UNWIND_DWRITE_W_FAIL;
  861. }
  862. }
  863. }
  864. }
  865. }
  866. /*
  867. * Conditional branches
  868. * Bcond
  869. */
  870. else if((instr & 0xf000) == 0xd000) {
  871. int32_t branchValue = (instr & 0xff);
  872. if (branchValue & 0x80) branchValue |= 0xffffff00;
  873. /* Branch distance is twice that specified in the instruction. */
  874. branchValue *= 2;
  875. UnwPrintd2("Bcond %d \n", branchValue);
  876. /* Only take the branch if a loop was detected */
  877. if (loopDetected) {
  878. /* Update PC */
  879. state->regData[15].v += branchValue;
  880. /* Need to advance by a word to account for pre-fetch.
  881. * Advance by a half word here, allowing the normal address
  882. * advance to account for the other half word.
  883. */
  884. state->regData[15].v += 2;
  885. /* Display PC of next instruction */
  886. UnwPrintd2(" New PC=%x", state->regData[15].v + 2);
  887. }
  888. }
  889. /* Format 18: unconditional branch
  890. * B label
  891. */
  892. else if((instr & 0xf800) == 0xe000) {
  893. uint32_t v;
  894. int32_t branchValue = signExtend11(instr & 0x07ff);
  895. /* Branch distance is twice that specified in the instruction. */
  896. branchValue *= 2;
  897. UnwPrintd2("B %d \n", branchValue);
  898. /* Update PC */
  899. state->regData[15].v += branchValue;
  900. /* Need to advance by a word to account for pre-fetch.
  901. * Advance by a half word here, allowing the normal address
  902. * advance to account for the other half word.
  903. */
  904. state->regData[15].v += 2;
  905. /* Compute the jump address */
  906. v = state->regData[15].v + 2;
  907. /* Display PC of next instruction */
  908. UnwPrintd2(" New PC=%x", v);
  909. /* Did we detect an infinite loop ? */
  910. loopDetected = lastJumpAddr == v;
  911. /* Remember the last address we jumped to */
  912. lastJumpAddr = v;
  913. }
  914. else {
  915. UnwPrintd1("????");
  916. /* Unknown/undecoded. May alter some register, so invalidate file */
  917. UnwInvalidateRegisterFile(state->regData);
  918. }
  919. UnwPrintd1("\n");
  920. /* Should never hit the reset vector */
  921. if(state->regData[15].v == 0)
  922. return UNWIND_RESET;
  923. /* Check next address */
  924. state->regData[15].v += 2;
  925. /* Garbage collect the memory hash (used only for the stack) */
  926. UnwMemHashGC(state);
  927. t--;
  928. if(t == 0)
  929. return UNWIND_EXHAUSTED;
  930. } while(!found);
  931. return UNWIND_SUCCESS;
  932. }
  933. #endif