My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

unwarm_thumb.cpp 34KB

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