thm32dis.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. thm32dis.c
  5. Abstract:
  6. This module implements support for disassembling 32-bit Thumb-2
  7. instructions.
  8. Author:
  9. Evan Green 27-Apr-2014
  10. Environment:
  11. Debug
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include "dbgrtl.h"
  17. #include "disasm.h"
  18. #include "armdis.h"
  19. #include "thmdis.h"
  20. #include <assert.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. //
  27. // ------------------------------------------------------ Data Type Definitions
  28. //
  29. //
  30. // ----------------------------------------------- Internal Function Prototypes
  31. //
  32. VOID
  33. DbgpThumb32DecodeLoadStoreMultiple (
  34. PARM_DISASSEMBLY Context
  35. );
  36. VOID
  37. DbgpThumb32DecodeLoadStoreDualExclusive (
  38. PARM_DISASSEMBLY Context
  39. );
  40. VOID
  41. DbgpThumb32DecodeLdrexStrex (
  42. PARM_DISASSEMBLY Context
  43. );
  44. VOID
  45. DbgpThumb32DecodeLdrdStrd (
  46. PARM_DISASSEMBLY Context
  47. );
  48. VOID
  49. DbgpThumb32DecodeLoadStoreExclusiveFunkySize (
  50. PARM_DISASSEMBLY Context
  51. );
  52. VOID
  53. DbgpThumb32DecodeTableBranch (
  54. PARM_DISASSEMBLY Context
  55. );
  56. VOID
  57. DbgpThumb32DecodeDataProcessingShiftedRegister (
  58. PARM_DISASSEMBLY Context
  59. );
  60. VOID
  61. DbgpThumb32DecodeCoprocessorSimdFloatingPoint (
  62. PARM_DISASSEMBLY Context
  63. );
  64. VOID
  65. DbgpThumb32DecodeUndefined (
  66. PARM_DISASSEMBLY Context
  67. );
  68. VOID
  69. DbgpThumb32DecodeSimdDataProcessing (
  70. PARM_DISASSEMBLY Context
  71. );
  72. VOID
  73. DbgpThumb32DecodeDataModifiedImmediate (
  74. PARM_DISASSEMBLY Context
  75. );
  76. VOID
  77. DbgpThumb32DecodeDataPlainImmediate (
  78. PARM_DISASSEMBLY Context
  79. );
  80. VOID
  81. DbgpThumb32DecodeBranchAndMiscellaneous (
  82. PARM_DISASSEMBLY Context
  83. );
  84. VOID
  85. DbgpThumb32DecodeMsr (
  86. PARM_DISASSEMBLY Context
  87. );
  88. VOID
  89. DbgpThumb32DecodeCpsAndHints (
  90. PARM_DISASSEMBLY Context
  91. );
  92. VOID
  93. DbgpThumb32DecodeMiscellaneousControl (
  94. PARM_DISASSEMBLY Context
  95. );
  96. VOID
  97. DbgpThumb32DecodeBxj (
  98. PARM_DISASSEMBLY Context
  99. );
  100. VOID
  101. DbgpThumb32DecodeExceptionReturn (
  102. PARM_DISASSEMBLY Context
  103. );
  104. VOID
  105. DbgpThumb32DecodeMrs (
  106. PARM_DISASSEMBLY Context
  107. );
  108. VOID
  109. DbgpThumb32DecodeHvc (
  110. PARM_DISASSEMBLY Context
  111. );
  112. VOID
  113. DbgpThumb32DecodeSmc (
  114. PARM_DISASSEMBLY Context
  115. );
  116. VOID
  117. DbgpThumb32DecodeBranch (
  118. PARM_DISASSEMBLY Context
  119. );
  120. VOID
  121. DbgpThumb32DecodeUdf (
  122. PARM_DISASSEMBLY Context
  123. );
  124. VOID
  125. DbgpThumb32DecodeBranchWithLink (
  126. PARM_DISASSEMBLY Context
  127. );
  128. VOID
  129. DbgpThumb32DecodeLoadStoreSingleItem (
  130. PARM_DISASSEMBLY Context
  131. );
  132. VOID
  133. DbgpThumb32DecodeLoadStoreImmediate (
  134. PARM_DISASSEMBLY Context
  135. );
  136. VOID
  137. DbgpThumb32DecodeLoadStoreRegister (
  138. PARM_DISASSEMBLY Context
  139. );
  140. VOID
  141. DbgpThumb32DecodeDataProcessingRegister (
  142. PARM_DISASSEMBLY Context
  143. );
  144. VOID
  145. DbgpThumb32DecodeMultiplyAccumulate (
  146. PARM_DISASSEMBLY Context
  147. );
  148. VOID
  149. DbgpThumb32DecodeLongMultiplyDivide (
  150. PARM_DISASSEMBLY Context
  151. );
  152. VOID
  153. DbgpThumbDecodeImmediateShift (
  154. PSTR Destination,
  155. ULONG DestinationSize,
  156. ULONG Register,
  157. ULONG Type,
  158. ULONG Immediate
  159. );
  160. ULONG
  161. DbgpThumb32DecodeModifiedImmediate (
  162. ULONG Immediate12
  163. );
  164. //
  165. // -------------------------------------------------------------------- Globals
  166. //
  167. //
  168. // Define mnemonic tables.
  169. //
  170. PSTR DbgThumb32DataProcessingMnemonics[2][16] = {
  171. {
  172. "and.w",
  173. "bic.w",
  174. "orr.w",
  175. "orn.w",
  176. "eor.w",
  177. "",
  178. "",
  179. "",
  180. "add.w",
  181. "",
  182. "adc.w",
  183. "sbc.w",
  184. "",
  185. "sub.w",
  186. "rsb.w",
  187. "",
  188. },
  189. {
  190. "ands.w",
  191. "bics.w",
  192. "orrs.w",
  193. "orns.w",
  194. "eors.w",
  195. "",
  196. "",
  197. "",
  198. "adds.w",
  199. "",
  200. "adcs.w",
  201. "sbcs.w",
  202. "",
  203. "subs.w",
  204. "rsbs.w",
  205. "",
  206. },
  207. };
  208. PSTR DbgThumb32DataProcessingShiftMnemonics[2][5] = {
  209. {
  210. "lsl.w",
  211. "lsr.w",
  212. "asr.w",
  213. "ror.w",
  214. "rrx.w"
  215. },
  216. {
  217. "lsls.w",
  218. "lsrs.w",
  219. "asrs.w",
  220. "rors.w",
  221. "rrxs.w"
  222. }
  223. };
  224. PSTR DbgThumb32MovMnemonics[2] = {
  225. "mov.w",
  226. "movs.w"
  227. };
  228. PSTR DbgThumb32MvnwMnemonics[2] = {
  229. "mvn.w",
  230. "mvns.w"
  231. };
  232. PSTR DbgThumb32HintMnemonics[] = {
  233. "nop.w",
  234. "yield.w",
  235. "wfe.w",
  236. "wfi.w",
  237. "sev.w"
  238. };
  239. PSTR DbgThumb32LoadStoreMnemonics[2][4] = {
  240. {
  241. "strb.w",
  242. "strh.w",
  243. "str.w",
  244. "Undef str.w"
  245. },
  246. {
  247. "ldrb.w",
  248. "ldrh.w",
  249. "ldr.w",
  250. "Undef ldr.w"
  251. }
  252. };
  253. PSTR DbgThumb32LoadSetFlagsMnemonics[4] = {
  254. "ldsrb.w",
  255. "ldrsh.w",
  256. "ldrs.w",
  257. "Undef ldrs.w"
  258. };
  259. PSTR DbgThumb32LoadStoreUnprivilegedMnemonics[2][4] = {
  260. {
  261. "strbt",
  262. "strht",
  263. "strt",
  264. "Undef strt"
  265. },
  266. {
  267. "ldrbt",
  268. "ldrht",
  269. "ldrt",
  270. "Undef ldrt"
  271. }
  272. };
  273. PSTR DbgThumb32LoadSetFlagsUnprivilegedMnemonics[4] = {
  274. "ldrsbt",
  275. "ldrsht",
  276. "ldrst",
  277. "Undef ldrst"
  278. };
  279. PSTR DbgThumb32PreloadMnemonics[4] = {
  280. "pli",
  281. "pldw",
  282. "pld",
  283. "Undef pld"
  284. };
  285. PSTR DbgThumb32ExtendAndAddMnemonics[2][6] = {
  286. {
  287. "sxtah",
  288. "uxtah",
  289. "sxtab16",
  290. "uxtab16",
  291. "sxtab",
  292. "uxtab"
  293. },
  294. {
  295. "sxth",
  296. "uxth",
  297. "sxtb16",
  298. "uxtb16",
  299. "sxtb",
  300. "uxtb"
  301. }
  302. };
  303. PSTR DbgThumb32ParallelArithmeticMnemonics[2][24] = {
  304. {
  305. "sadd8",
  306. "sadd16",
  307. "sasx",
  308. "",
  309. "ssub8",
  310. "ssub16",
  311. "ssax",
  312. "",
  313. "qadd8",
  314. "qadd16",
  315. "qasx",
  316. "",
  317. "qsub8",
  318. "qsub16",
  319. "qsax",
  320. "",
  321. "shadd8",
  322. "shadd16",
  323. "shasx",
  324. "",
  325. "shsub8",
  326. "shsub16",
  327. "shsax",
  328. "",
  329. },
  330. {
  331. "uadd8",
  332. "uadd16",
  333. "uasx",
  334. "",
  335. "usub8",
  336. "usub16",
  337. "usax",
  338. "",
  339. "uqadd8",
  340. "uqadd16",
  341. "uqasx",
  342. "",
  343. "uqsub8",
  344. "uqsub16",
  345. "uqsax",
  346. "",
  347. "uhadd8",
  348. "uhadd16",
  349. "uhasx",
  350. "",
  351. "uhsub8",
  352. "uhsub16",
  353. "uhsax",
  354. "",
  355. },
  356. };
  357. PSTR DbgThumb32DataProcessingMiscellaneousMnemonics[] = {
  358. "qadd",
  359. "qdadd",
  360. "qsub",
  361. "qdsub",
  362. "rev.w",
  363. "rev16.w",
  364. "rbit",
  365. "revsh.w",
  366. "sel",
  367. "",
  368. "",
  369. "",
  370. "clz",
  371. "",
  372. "",
  373. ""
  374. };
  375. PSTR DbgThumb32MultiplyMnemonics[2][8] = {
  376. {
  377. "mla",
  378. "smla",
  379. "smlad",
  380. "smlaw",
  381. "smlsd",
  382. "smmla",
  383. "smmls",
  384. "usada8"
  385. },
  386. {
  387. "mul",
  388. "smul",
  389. "smuad",
  390. "smulw",
  391. "smusd",
  392. "smmul",
  393. "smmls",
  394. "usad8"
  395. }
  396. };
  397. PSTR DbgThumb32MultiplyTopBottomMnemonics[2] = {
  398. "b",
  399. "t",
  400. };
  401. PSTR DbgThumb32LongMultiplyMnemonics[8] = {
  402. "smull",
  403. "sdiv",
  404. "umull",
  405. "udiv",
  406. "smlal",
  407. "smlsld",
  408. "umlal",
  409. ""
  410. };
  411. //
  412. // Define decode tables.
  413. //
  414. THUMB_DECODE_BRANCH DbgThumb32TopLevelTable[] = {
  415. {0x1E400000, 0x08000000, 0, DbgpThumb32DecodeLoadStoreMultiple},
  416. {0x1E400000, 0x08400000, 0, DbgpThumb32DecodeLoadStoreDualExclusive},
  417. {0x1E000000, 0x0A000000, 0, DbgpThumb32DecodeDataProcessingShiftedRegister},
  418. {0x1C000000, 0x0C000000, 0, DbgpThumb32DecodeCoprocessorSimdFloatingPoint},
  419. {0x1A008000, 0x10000000, 0, DbgpThumb32DecodeDataModifiedImmediate},
  420. {0x1A008000, 0x12000000, 0, DbgpThumb32DecodeDataPlainImmediate},
  421. {0x18008000, 0x10008000, 0, DbgpThumb32DecodeBranchAndMiscellaneous},
  422. {0x1F100000, 0x18000000, 0, DbgpThumb32DecodeLoadStoreSingleItem},
  423. {0x1E700000, 0x18100000, 0, DbgpThumb32DecodeLoadStoreSingleItem},
  424. {0x1E700000, 0x18300000, 0, DbgpThumb32DecodeLoadStoreSingleItem},
  425. {0x1E700000, 0x18500000, 0, DbgpThumb32DecodeLoadStoreSingleItem},
  426. {0x1E700000, 0x18700000, 0, DbgpThumb32DecodeUndefined},
  427. {0x1F100000, 0x19000000, 0, DbgpArmDecodeSimdElementLoadStore},
  428. {0x1F000000, 0x1A000000, 0, DbgpThumb32DecodeDataProcessingRegister},
  429. {0x1F800000, 0x1B000000, 0, DbgpThumb32DecodeMultiplyAccumulate},
  430. {0x1F800000, 0x1B800000, 0, DbgpThumb32DecodeLongMultiplyDivide},
  431. {0x1C000000, 0x1C000000, 0, DbgpThumb32DecodeCoprocessorSimdFloatingPoint},
  432. };
  433. THUMB_DECODE_BRANCH DbgThumb32LoadStoreDualExclusiveTable[] = {
  434. {0x01B00000, 0x00000000, 0, DbgpThumb32DecodeLdrexStrex},
  435. {0x01B00000, 0x00100000, 0, DbgpThumb32DecodeLdrexStrex},
  436. {0x01300000, 0x00200000, 0, DbgpThumb32DecodeLdrdStrd},
  437. {0x01100000, 0x01000000, 0, DbgpThumb32DecodeLdrdStrd},
  438. {0x01300000, 0x00300000, 0, DbgpThumb32DecodeLdrdStrd},
  439. {0x01100000, 0x01100000, 0, DbgpThumb32DecodeLdrdStrd},
  440. {0x01B000F0, 0x00800040, 0, DbgpThumb32DecodeLoadStoreExclusiveFunkySize},
  441. {0x01B000F0, 0x00800050, 0, DbgpThumb32DecodeLoadStoreExclusiveFunkySize},
  442. {0x01B000F0, 0x00800070, 0, DbgpThumb32DecodeLoadStoreExclusiveFunkySize},
  443. {0x01B000F0, 0x00900000, 0, DbgpThumb32DecodeTableBranch},
  444. {0x01B000F0, 0x00900010, 0, DbgpThumb32DecodeTableBranch},
  445. {0x01B000F0, 0x00900040, 0, DbgpThumb32DecodeLoadStoreExclusiveFunkySize},
  446. {0x01B000F0, 0x00900050, 0, DbgpThumb32DecodeLoadStoreExclusiveFunkySize},
  447. {0x01B000F0, 0x00900070, 0, DbgpThumb32DecodeLoadStoreExclusiveFunkySize},
  448. };
  449. THUMB_DECODE_BRANCH DbgThumb32CoprocessorSimdFloatingPointTable[] = {
  450. {0x03E00000, 0x00000000, 0, DbgpThumb32DecodeUndefined},
  451. {0x03000000, 0x03000000, 0, DbgpThumb32DecodeSimdDataProcessing},
  452. {0x03E00E00, 0x00400A00, 0, DbgpArmDecodeSimd64BitTransfers},
  453. {0x02000E00, 0x00000A00, 0, DbgpArmDecodeSimdLoadStore},
  454. {0x03000E10, 0x02000A00, 0, DbgpArmDecodeFloatingPoint},
  455. {0x03000E10, 0x02000A10, 0, DbgpArmDecodeSimdSmallTransfers},
  456. {0x03E00000, 0x00400000, 0, DbgpArmDecodeCoprocessorMoveTwo},
  457. {0x02000000, 0x00000000, 0, DbgpArmDecodeCoprocessorLoadStore},
  458. {0x03000010, 0x02000000, 0, DbgpArmDecodeCoprocessorMove},
  459. {0x03000010, 0x02000010, 0, DbgpArmDecodeCoprocessorMove},
  460. };
  461. THUMB_DECODE_BRANCH DbgThumb32BranchAndMiscellaneousTable[] = {
  462. {0x07E05000, 0x03800000, 0, DbgpThumb32DecodeMsr},
  463. {0x07F05000, 0x03A00000, 0, DbgpThumb32DecodeCpsAndHints},
  464. {0x07F05000, 0x03B00000, 0, DbgpThumb32DecodeMiscellaneousControl},
  465. {0x07F05000, 0x03C00000, 0, DbgpThumb32DecodeBxj},
  466. {0x07F05000, 0x03D00000, 0, DbgpThumb32DecodeExceptionReturn},
  467. {0x07E05000, 0x03E00000, 0, DbgpThumb32DecodeMrs},
  468. {0x07F07000, 0x07E00000, 0, DbgpThumb32DecodeHvc},
  469. {0x07F07000, 0x07F00000, 0, DbgpThumb32DecodeSmc},
  470. {0x00005000, 0x00001000, 0, DbgpThumb32DecodeBranch},
  471. {0x00005000, 0x00000000, 0, DbgpThumb32DecodeBranch},
  472. {0x07F07000, 0x07F02000, 0, DbgpThumb32DecodeUdf},
  473. {0x00004000, 0x00004000, 0, DbgpThumb32DecodeBranchWithLink},
  474. };
  475. //
  476. // ------------------------------------------------------------------ Functions
  477. //
  478. VOID
  479. DbgpThumb32Decode (
  480. PARM_DISASSEMBLY Context
  481. )
  482. /*++
  483. Routine Description:
  484. This routine decodes the 32-bit portion of the Thumb-2 instruction set.
  485. Arguments:
  486. Context - Supplies a pointer to the disassembly context.
  487. Return Value:
  488. None.
  489. --*/
  490. {
  491. //
  492. // Swap the half words, then decode using the table.
  493. //
  494. Context->Instruction = ((Context->Instruction << 16) & 0xFFFF0000) |
  495. ((Context->Instruction >> 16) & 0x0000FFFF);
  496. THUMB_DECODE_WITH_TABLE(Context, DbgThumb32TopLevelTable);
  497. return;
  498. }
  499. //
  500. // --------------------------------------------------------- Internal Functions
  501. //
  502. VOID
  503. DbgpThumb32DecodeLoadStoreMultiple (
  504. PARM_DISASSEMBLY Context
  505. )
  506. /*++
  507. Routine Description:
  508. This routine decodes the 32-bit load/store multiple instructions.
  509. Arguments:
  510. Context - Supplies a pointer to the disassembly context.
  511. Return Value:
  512. None.
  513. --*/
  514. {
  515. ULONG Instruction;
  516. ULONG Mode;
  517. ULONG Op;
  518. ULONG RegisterList;
  519. ULONG Rn;
  520. Instruction = Context->Instruction;
  521. Op = (Instruction >> THUMB32_LOAD_STORE_MULTIPLE_OP_SHIFT) &
  522. THUMB32_LOAD_STORE_MULTIPLE_OP_MASK;
  523. Rn = (Instruction >> THUMB32_LOAD_STORE_MULTIPLE_RN_SHIFT) &
  524. THUMB_REGISTER16_MASK;
  525. //
  526. // The instruction is either rfe/srs, or ldm/stm.
  527. //
  528. switch (Op) {
  529. case THUMB32_LOAD_STORE_RETURN_STATE_OP:
  530. case THUMB32_LOAD_STORE_RETURN_STATE_OP2:
  531. Mode = Instruction & THUMB32_LOAD_STORE_MODE_MASK;
  532. if ((Instruction & THUMB32_LOAD_BIT) != 0) {
  533. strcpy(Context->Mnemonic, THUMB_RFE_MNEMONIC);
  534. } else {
  535. strcpy(Context->Mnemonic, THUMB_SRS_MNEMONIC);
  536. DbgpArmPrintMode(Context->Operand2, Mode);
  537. }
  538. break;
  539. case THUMB32_LOAD_STORE_MULTIPLE_OP:
  540. case THUMB32_LOAD_STORE_MULTIPLE_OP2:
  541. default:
  542. if ((Instruction & THUMB32_LOAD_BIT) != 0) {
  543. strcpy(Context->Mnemonic, THUMB_LDM_MNEMONIC);
  544. } else {
  545. strcpy(Context->Mnemonic, THUMB_STM_MNEMONIC);
  546. }
  547. RegisterList = Instruction & THUMB_REGISTER16_LIST;
  548. DbgpArmDecodeRegisterList(Context->Operand2,
  549. sizeof(Context->Operand2),
  550. RegisterList);
  551. break;
  552. }
  553. //
  554. // Add the decrement-before or increment-after suffix.
  555. //
  556. if ((Instruction & THUMB32_LOAD_STORE_INCREMENT) != 0) {
  557. strcat(Context->Mnemonic, THUMB_IA_SUFFIX);
  558. } else {
  559. strcat(Context->Mnemonic, THUMB_DB_SUFFIX);
  560. }
  561. //
  562. // Print operand one, the register.
  563. //
  564. if ((Instruction & THUMB32_LOAD_STORE_MULTIPLE_WRITE_BACK_BIT) != 0) {
  565. snprintf(Context->Operand1,
  566. sizeof(Context->Operand1),
  567. "%s!",
  568. DbgArmRegisterNames[Rn]);
  569. } else {
  570. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  571. }
  572. return;
  573. }
  574. VOID
  575. DbgpThumb32DecodeLoadStoreDualExclusive (
  576. PARM_DISASSEMBLY Context
  577. )
  578. /*++
  579. Routine Description:
  580. This routine decodes the 32-bit load/store dual, load/store exclusive,
  581. and table branch instructions.
  582. Arguments:
  583. Context - Supplies a pointer to the disassembly context.
  584. Return Value:
  585. None.
  586. --*/
  587. {
  588. THUMB_DECODE_WITH_TABLE(Context, DbgThumb32LoadStoreDualExclusiveTable);
  589. return;
  590. }
  591. VOID
  592. DbgpThumb32DecodeLdrexStrex (
  593. PARM_DISASSEMBLY Context
  594. )
  595. /*++
  596. Routine Description:
  597. This routine decodes the 32-bit load/store exclusive (32-bit data)
  598. instructions.
  599. Arguments:
  600. Context - Supplies a pointer to the disassembly context.
  601. Return Value:
  602. None.
  603. --*/
  604. {
  605. ULONG Immediate8;
  606. ULONG Instruction;
  607. ULONG Rd;
  608. ULONG Rn;
  609. PSTR RnOperand;
  610. ULONG Rt;
  611. PSTR RtOperand;
  612. Instruction = Context->Instruction;
  613. Rd = (Instruction >> THUMB32_EXCLUSIVE_RD_SHIFT) & THUMB_REGISTER16_MASK;
  614. Rn = (Instruction >> THUMB32_EXCLUSIVE_RN_SHIFT) & THUMB_REGISTER16_MASK;
  615. Rt = (Instruction >> THUMB32_EXCLUSIVE_RT_SHIFT) & THUMB_REGISTER16_MASK;
  616. Immediate8 = (Instruction >> THUMB32_EXCLUSIVE_IMMEDIATE8_SHIFT) &
  617. THUMB_IMMEDIATE8_MASK;
  618. Immediate8 <<= 2;
  619. if ((Instruction & THUMB32_LOAD_BIT) != 0) {
  620. strcpy(Context->Mnemonic, THUMB_LDREX_MNEMONIC);
  621. RtOperand = &(Context->Operand1[0]);
  622. RnOperand = &(Context->Operand2[0]);
  623. } else {
  624. strcpy(Context->Mnemonic, THUMB_STREX_MNEMONIC);
  625. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  626. RtOperand = &(Context->Operand2[0]);
  627. RnOperand = &(Context->Operand3[0]);
  628. }
  629. strcpy(RtOperand, DbgArmRegisterNames[Rt]);
  630. if (Immediate8 == 0) {
  631. sprintf(RnOperand, "[%s]", DbgArmRegisterNames[Rn]);
  632. } else {
  633. sprintf(RnOperand,
  634. "[%s, #%d]",
  635. DbgArmRegisterNames[Rn],
  636. Immediate8 * 4);
  637. }
  638. return;
  639. }
  640. VOID
  641. DbgpThumb32DecodeLdrdStrd (
  642. PARM_DISASSEMBLY Context
  643. )
  644. /*++
  645. Routine Description:
  646. This routine decodes the 32-bit load/store dual (64-bit data).
  647. Arguments:
  648. Context - Supplies a pointer to the disassembly context.
  649. Return Value:
  650. None.
  651. --*/
  652. {
  653. ULONG Immediate8;
  654. ULONG Instruction;
  655. ULONG Rn;
  656. ULONG Rt;
  657. ULONG Rt2;
  658. Instruction = Context->Instruction;
  659. if ((Instruction & THUMB32_LOAD_BIT) != 0) {
  660. strcpy(Context->Mnemonic, THUMB_LDRD_MNEMONIC);
  661. } else {
  662. strcpy(Context->Mnemonic, THUMB_STRD_MNEMONIC);
  663. }
  664. Rn = (Instruction >> THUMB32_DUAL_RN_SHIFT) & THUMB_REGISTER16_MASK;
  665. Rt = (Instruction >> THUMB32_DUAL_RT_SHIFT) & THUMB_REGISTER16_MASK;
  666. Rt2 = (Instruction >> THUMB32_DUAL_RT2_SHIFT) & THUMB_REGISTER16_MASK;
  667. Immediate8 = Instruction & THUMB_IMMEDIATE8_MASK;
  668. Immediate8 <<= 2;
  669. strcpy(Context->Operand1, DbgArmRegisterNames[Rt]);
  670. strcpy(Context->Operand2, DbgArmRegisterNames[Rt2]);
  671. if ((Instruction & THUMB32_PREINDEX_BIT) != 0) {
  672. if ((Instruction & THUMB32_WRITE_BACK_BIT) != 0) {
  673. snprintf(Context->Operand3,
  674. sizeof(Context->Operand3),
  675. "[%s, #%d]!",
  676. DbgArmRegisterNames[Rn],
  677. Immediate8);
  678. } else {
  679. if (Immediate8 != 0) {
  680. snprintf(Context->Operand3,
  681. sizeof(Context->Operand3),
  682. "[%s, #%d]",
  683. DbgArmRegisterNames[Rn],
  684. Immediate8);
  685. } else {
  686. snprintf(Context->Operand3,
  687. sizeof(Context->Operand3),
  688. "[%s]",
  689. DbgArmRegisterNames[Rn]);
  690. }
  691. }
  692. //
  693. // If pre-index is not set, then update is assumed to be set.
  694. //
  695. } else {
  696. snprintf(Context->Operand3,
  697. sizeof(Context->Operand3),
  698. "[%s] #%d",
  699. DbgArmRegisterNames[Rn],
  700. Immediate8);
  701. }
  702. return;
  703. }
  704. VOID
  705. DbgpThumb32DecodeLoadStoreExclusiveFunkySize (
  706. PARM_DISASSEMBLY Context
  707. )
  708. /*++
  709. Routine Description:
  710. This routine decodes the 32-bit load/store exclusive instructions for
  711. non-native sizes (8, 16 and 64 bits).
  712. Arguments:
  713. Context - Supplies a pointer to the disassembly context.
  714. Return Value:
  715. None.
  716. --*/
  717. {
  718. ULONG Instruction;
  719. PSTR Mnemonic;
  720. ULONG Op;
  721. ULONG Rd;
  722. ULONG Rn;
  723. PSTR RnRegister;
  724. ULONG Rt;
  725. ULONG Rt2;
  726. Instruction = Context->Instruction;
  727. Rd = (Instruction >> THUMB32_EXCLUSIVE_FUNKY_RD_SHIFT) &
  728. THUMB_REGISTER16_MASK;
  729. Rn = (Instruction >> THUMB32_EXCLUSIVE_FUNKY_RN_SHIFT) &
  730. THUMB_REGISTER16_MASK;
  731. Rt = (Instruction >> THUMB32_EXCLUSIVE_FUNKY_RT_SHIFT) &
  732. THUMB_REGISTER16_MASK;
  733. Rt2 = (Instruction >> THUMB32_EXCLUSIVE_FUNKY_RT2_SHIFT) &
  734. THUMB_REGISTER16_MASK;
  735. //
  736. // Get the mnemonic. Load instructions look like ldr Rt, [Rn]. Store
  737. // instructions look like str Rd, Rt, [Rn]. Dual instructions stick Rt2
  738. // after Rt.
  739. //
  740. Op = (Instruction >> THUMB32_EXCLUSIVE_FUNKY_OP_SHIFT) &
  741. THUMB32_EXCLUSIVE_FUNKY_OP_MASK;
  742. if ((Instruction & THUMB32_LOAD_BIT) != 0) {
  743. strcpy(Context->Operand1, DbgArmRegisterNames[Rt]);
  744. RnRegister = &(Context->Operand2[0]);
  745. if (Op == THUMB32_EXCLUSIVE_FUNKY_OP_BYTE) {
  746. Mnemonic = THUMB_LDREXB_MNEMONIC;
  747. } else if (Op == THUMB32_EXCLUSIVE_FUNKY_OP_HALF_WORD) {
  748. Mnemonic = THUMB_LDREXH_MNEMONIC;
  749. } else {
  750. assert(Op == THUMB32_EXCLUSIVE_FUNKY_OP_DUAL);
  751. Mnemonic = THUMB_LDREXD_MNEMONIC;
  752. strcpy(Context->Operand2, DbgArmRegisterNames[Rt2]);
  753. RnRegister = &(Context->Operand3[0]);
  754. }
  755. } else {
  756. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  757. strcpy(Context->Operand2, DbgArmRegisterNames[Rt]);
  758. RnRegister = &(Context->Operand3[0]);
  759. if (Op == THUMB32_EXCLUSIVE_FUNKY_OP_BYTE) {
  760. Mnemonic = THUMB_STREXB_MNEMONIC;
  761. } else if (Op == THUMB32_EXCLUSIVE_FUNKY_OP_HALF_WORD) {
  762. Mnemonic = THUMB_STREXH_MNEMONIC;
  763. } else {
  764. assert(Op == THUMB32_EXCLUSIVE_FUNKY_OP_DUAL);
  765. Mnemonic = THUMB_STREXD_MNEMONIC;
  766. strcpy(Context->Operand3, DbgArmRegisterNames[Rt2]);
  767. RnRegister = &(Context->Operand4[0]);
  768. }
  769. }
  770. strcpy(Context->Mnemonic, Mnemonic);
  771. sprintf(RnRegister, "[%s]", DbgArmRegisterNames[Rn]);
  772. return;
  773. }
  774. VOID
  775. DbgpThumb32DecodeTableBranch (
  776. PARM_DISASSEMBLY Context
  777. )
  778. /*++
  779. Routine Description:
  780. This routine decodes the 32-bit load/store exclusive instructions for
  781. non-native sizes (8, 16 and 64 bits).
  782. Arguments:
  783. Context - Supplies a pointer to the disassembly context.
  784. Return Value:
  785. None.
  786. --*/
  787. {
  788. ULONG Instruction;
  789. ULONG Rm;
  790. ULONG Rn;
  791. Instruction = Context->Instruction;
  792. Rm = (Instruction >> THUMB32_TABLE_BRANCH_RM_SHIFT) & THUMB_REGISTER16_MASK;
  793. Rn = (Instruction >> THUMB32_TABLE_BRANCH_RN_SHIFT) & THUMB_REGISTER16_MASK;
  794. if ((Instruction & THUMB32_TABLE_BRANCH_HALF_WORD) != 0) {
  795. strcpy(Context->Mnemonic, THUMB_TBH_MNEMONIC);
  796. snprintf(Context->Operand1,
  797. sizeof(Context->Operand1),
  798. "[%s, %s, lsl #1]",
  799. DbgArmRegisterNames[Rn],
  800. DbgArmRegisterNames[Rm]);
  801. } else {
  802. strcpy(Context->Mnemonic, THUMB_TBB_MNEMONIC);
  803. snprintf(Context->Operand1,
  804. sizeof(Context->Operand1),
  805. "[%s, %s]",
  806. DbgArmRegisterNames[Rn],
  807. DbgArmRegisterNames[Rm]);
  808. }
  809. return;
  810. }
  811. VOID
  812. DbgpThumb32DecodeDataProcessingShiftedRegister (
  813. PARM_DISASSEMBLY Context
  814. )
  815. /*++
  816. Routine Description:
  817. This routine decodes the 32-bit data processing (shifted register)
  818. instructions.
  819. Arguments:
  820. Context - Supplies a pointer to the disassembly context.
  821. Return Value:
  822. None.
  823. --*/
  824. {
  825. ULONG Immediate5;
  826. ULONG Instruction;
  827. PSTR Mnemonic;
  828. ULONG Op;
  829. ULONG Rd;
  830. ULONG Rm;
  831. ULONG Rn;
  832. ULONG SetFlags;
  833. BOOL StandardParameters;
  834. ULONG Type;
  835. Instruction = Context->Instruction;
  836. Rd = (Instruction >> THUMB32_DATA_SHIFTED_REGISTER_RD_SHIFT) &
  837. THUMB_REGISTER16_MASK;
  838. Rm = (Instruction >> THUMB32_DATA_SHIFTED_REGISTER_RM_SHIFT) &
  839. THUMB_REGISTER16_MASK;
  840. Rn = (Instruction >> THUMB32_DATA_SHIFTED_REGISTER_RN_SHIFT) &
  841. THUMB_REGISTER16_MASK;
  842. Type = (Instruction >> THUMB32_DATA_SHIFTED_REGISTER_TYPE_SHIFT) &
  843. THUMB32_DATA_SHIFTED_REGISTER_TYPE_MASK;
  844. Immediate5 = (Instruction >>
  845. THUMB32_DATA_SHIFTED_REGISTER_IMMEDIATE2_SHIFT) &
  846. THUMB32_DATA_SHIFTED_REGISTER_IMMEDIATE2_MASK;
  847. Immediate5 |= ((Instruction >>
  848. THUMB32_DATA_SHIFTED_REGISTER_IMMEDIATE3_SHIFT) &
  849. THUMB32_DATA_SHIFTED_REGISTER_IMMEDIATE3_MASK) << 2;
  850. SetFlags = Instruction & THUMB32_DATA_SET_FLAGS;
  851. Op = (Instruction >> THUMB32_DATA_SHIFTED_REGISTER_OP_SHIFT) &
  852. THUMB32_DATA_SHIFTED_REGISTER_OP_MASK;
  853. StandardParameters = TRUE;
  854. if (SetFlags != 0) {
  855. SetFlags = 1;
  856. }
  857. //
  858. // This decoding follows a standard pattern, but there are several
  859. // exceptions that kick in when 1111 is specified for one of the registers.
  860. // The exceptions are listed below in this switch statement.
  861. //
  862. Mnemonic = DbgThumb32DataProcessingMnemonics[SetFlags][Op];
  863. switch (Op) {
  864. case THUMB32_DATA_AND:
  865. if ((Rd == 0xF) && (SetFlags != 0)) {
  866. StandardParameters = FALSE;
  867. Mnemonic = THUMB_TST_W_MNEMONIC;
  868. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  869. DbgpThumbDecodeImmediateShift(Context->Operand2,
  870. sizeof(Context->Operand2),
  871. Rm,
  872. Type,
  873. Immediate5);
  874. }
  875. break;
  876. case THUMB32_DATA_ORR:
  877. if (Rn == 0xF) {
  878. StandardParameters = FALSE;
  879. Mnemonic = DbgThumb32DataProcessingShiftMnemonics[SetFlags][Type];
  880. switch (Type) {
  881. case THUMB_SHIFT_TYPE_LSL:
  882. if (Immediate5 == 0) {
  883. Mnemonic = DbgThumb32MovMnemonics[SetFlags];
  884. }
  885. break;
  886. case THUMB_SHIFT_TYPE_ROR:
  887. if (Immediate5 == 0) {
  888. Type += 1;
  889. Mnemonic =
  890. DbgThumb32DataProcessingShiftMnemonics[SetFlags][Type];
  891. }
  892. break;
  893. default:
  894. break;
  895. }
  896. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  897. strcpy(Context->Operand2, DbgArmRegisterNames[Rm]);
  898. if (Immediate5 != 0) {
  899. snprintf(Context->Operand3,
  900. sizeof(Context->Operand3),
  901. "#%d",
  902. Immediate5);
  903. }
  904. }
  905. break;
  906. case THUMB32_DATA_ORN:
  907. if ((Rd == 0xF) && (SetFlags != 0)) {
  908. StandardParameters = FALSE;
  909. Mnemonic = DbgThumb32MvnwMnemonics[SetFlags];
  910. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  911. DbgpThumbDecodeImmediateShift(Context->Operand2,
  912. sizeof(Context->Operand2),
  913. Rm,
  914. Type,
  915. Immediate5);
  916. }
  917. break;
  918. case THUMB32_DATA_EOR:
  919. if ((Rd == 0xF) && (SetFlags != 0)) {
  920. StandardParameters = FALSE;
  921. Mnemonic = THUMB_TEQ_W_MNEMONIC;
  922. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  923. DbgpThumbDecodeImmediateShift(Context->Operand2,
  924. sizeof(Context->Operand2),
  925. Rm,
  926. Type,
  927. Immediate5);
  928. }
  929. break;
  930. case THUMB32_DATA_PKH:
  931. Type &= ~0x1;
  932. Mnemonic = THUMB_PKHBT_MNEMONIC;
  933. if ((Instruction & THUMB32_PACK_HALF_WORD_TB) != 0) {
  934. Mnemonic = THUMB_PKHTB_MNEMONIC;
  935. }
  936. break;
  937. case THUMB32_DATA_ADD:
  938. if ((Rd == 0xF) && (SetFlags != 0)) {
  939. StandardParameters = FALSE;
  940. Mnemonic = THUMB_CMN_W_MNEMONIC;
  941. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  942. DbgpThumbDecodeImmediateShift(Context->Operand2,
  943. sizeof(Context->Operand2),
  944. Rm,
  945. Type,
  946. Immediate5);
  947. }
  948. break;
  949. case THUMB32_DATA_SUB:
  950. if ((Rd == 0xF) && (SetFlags != 0)) {
  951. StandardParameters = FALSE;
  952. Mnemonic = THUMB_CMP_W_MNEMONIC;
  953. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  954. DbgpThumbDecodeImmediateShift(Context->Operand2,
  955. sizeof(Context->Operand2),
  956. Rm,
  957. Type,
  958. Immediate5);
  959. }
  960. break;
  961. default:
  962. break;
  963. }
  964. strcpy(Context->Mnemonic, Mnemonic);
  965. //
  966. // If the switch statement didn't apply, copy in the regular parameters.
  967. // The pack half-word is a special case, it changed the opcode but still
  968. // follows the standard parameters.
  969. //
  970. if (StandardParameters != FALSE) {
  971. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  972. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  973. DbgpThumbDecodeImmediateShift(Context->Operand3,
  974. sizeof(Context->Operand3),
  975. Rm,
  976. Type,
  977. Immediate5);
  978. }
  979. return;
  980. }
  981. VOID
  982. DbgpThumb32DecodeCoprocessorSimdFloatingPoint (
  983. PARM_DISASSEMBLY Context
  984. )
  985. /*++
  986. Routine Description:
  987. This routine decodes coprocessor, advanced SIMD, and floating point
  988. instructions.
  989. Arguments:
  990. Context - Supplies a pointer to the disassembly context.
  991. Return Value:
  992. None.
  993. --*/
  994. {
  995. THUMB_DECODE_WITH_TABLE(Context,
  996. DbgThumb32CoprocessorSimdFloatingPointTable);
  997. return;
  998. }
  999. VOID
  1000. DbgpThumb32DecodeUndefined (
  1001. PARM_DISASSEMBLY Context
  1002. )
  1003. /*++
  1004. Routine Description:
  1005. This routine catches undefined corners of the instruction space.
  1006. Arguments:
  1007. Context - Supplies a pointer to the disassembly context.
  1008. Return Value:
  1009. None.
  1010. --*/
  1011. {
  1012. strcpy(Context->Mnemonic, "Undefined");
  1013. return;
  1014. }
  1015. VOID
  1016. DbgpThumb32DecodeSimdDataProcessing (
  1017. PARM_DISASSEMBLY Context
  1018. )
  1019. /*++
  1020. Routine Description:
  1021. This routine decodes SIMD data processing instructions.
  1022. Arguments:
  1023. Context - Supplies a pointer to the disassembly context.
  1024. Return Value:
  1025. None.
  1026. --*/
  1027. {
  1028. ULONG Instruction;
  1029. //
  1030. // The 32-bit Thumb instruction and the ARM instruction only differ by one
  1031. // bit. Move the bit in ths 32-bit Thumb instruction and use the ARM
  1032. // decoder.
  1033. //
  1034. Instruction = Context->Instruction;
  1035. if ((Instruction & THUMB32_SIMD_DATA_PROCESSING_UNSIGNED) != 0) {
  1036. Context->Instruction |= ARM_SIMD_DATA_PROCESSING_UNSIGNED;
  1037. } else {
  1038. Context->Instruction &= ~ARM_SIMD_DATA_PROCESSING_UNSIGNED;
  1039. }
  1040. DbgpArmDecodeSimdDataProcessing(Context);
  1041. Context->Instruction = Instruction;
  1042. return;
  1043. }
  1044. VOID
  1045. DbgpThumb32DecodeDataModifiedImmediate (
  1046. PARM_DISASSEMBLY Context
  1047. )
  1048. /*++
  1049. Routine Description:
  1050. This routine decodes data processing (modified immediate) instructions.
  1051. Arguments:
  1052. Context - Supplies a pointer to the disassembly context.
  1053. Return Value:
  1054. None.
  1055. --*/
  1056. {
  1057. ULONG Immediate12;
  1058. ULONG Instruction;
  1059. PSTR Mnemonic;
  1060. ULONG ModifiedImmediate;
  1061. ULONG Op;
  1062. ULONG Rd;
  1063. ULONG Rn;
  1064. ULONG SetFlags;
  1065. BOOL StandardParameters;
  1066. Instruction = Context->Instruction;
  1067. Immediate12 = (Instruction >>
  1068. THUMB32_DATA_MODIFIED_IMMEDIATE_IMMEDIATE8_SHIFT) &
  1069. THUMB_IMMEDIATE8_MASK;
  1070. Immediate12 |= ((Instruction >>
  1071. THUMB32_DATA_MODIFIED_IMMEDIATE_IMMEDIATE3_SHIFT) &
  1072. THUMB_IMMEDIATE3_MASK) << 8;
  1073. if ((Instruction & THUMB32_DATA_MODIFIED_IMMEDIATE_IMMEDIATE12) != 0) {
  1074. Immediate12 |= 1 << 11;
  1075. }
  1076. Rd = (Instruction >> THUMB32_DATA_MODIFIED_IMMEDIATE_RD_SHIFT) &
  1077. THUMB_REGISTER16_MASK;
  1078. Rn = (Instruction >> THUMB32_DATA_MODIFIED_IMMEDIATE_RN_SHIFT) &
  1079. THUMB_REGISTER16_MASK;
  1080. SetFlags = 0;
  1081. if ((Instruction & THUMB32_DATA_SET_FLAGS) != 0) {
  1082. SetFlags = 1;
  1083. }
  1084. Op = (Instruction >> THUMB32_DATA_MODIFIED_IMMEDIATE_OP_SHIFT) &
  1085. THUMB32_DATA_MODIFIED_IMMEDIATE_OP_MASK;
  1086. ModifiedImmediate = DbgpThumb32DecodeModifiedImmediate(Immediate12);
  1087. StandardParameters = TRUE;
  1088. Mnemonic = DbgThumb32DataProcessingMnemonics[SetFlags][Op];
  1089. switch (Op) {
  1090. case THUMB32_DATA_AND:
  1091. if ((Rd == 0xF) && (SetFlags != 0)) {
  1092. StandardParameters = FALSE;
  1093. Mnemonic = THUMB_TST_W_MNEMONIC;
  1094. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  1095. snprintf(Context->Operand2,
  1096. sizeof(Context->Operand2),
  1097. "#%d",
  1098. ModifiedImmediate);
  1099. }
  1100. break;
  1101. case THUMB32_DATA_ORR:
  1102. if (Rn == 0xF) {
  1103. StandardParameters = FALSE;
  1104. Mnemonic = DbgThumb32MovMnemonics[SetFlags];
  1105. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1106. snprintf(Context->Operand2,
  1107. sizeof(Context->Operand2),
  1108. "#%d",
  1109. ModifiedImmediate);
  1110. }
  1111. break;
  1112. case THUMB32_DATA_ORN:
  1113. if ((Rd == 0xF) && (SetFlags != 0)) {
  1114. StandardParameters = FALSE;
  1115. Mnemonic = DbgThumb32MvnwMnemonics[SetFlags];
  1116. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1117. snprintf(Context->Operand2,
  1118. sizeof(Context->Operand2),
  1119. "#%d",
  1120. ModifiedImmediate);
  1121. }
  1122. break;
  1123. case THUMB32_DATA_EOR:
  1124. if ((Rd == 0xF) && (SetFlags != 0)) {
  1125. StandardParameters = FALSE;
  1126. Mnemonic = THUMB_TEQ_W_MNEMONIC;
  1127. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  1128. snprintf(Context->Operand2,
  1129. sizeof(Context->Operand2),
  1130. "#%d",
  1131. ModifiedImmediate);
  1132. }
  1133. break;
  1134. case THUMB32_DATA_ADD:
  1135. if ((Rd == 0xF) && (SetFlags != 0)) {
  1136. StandardParameters = FALSE;
  1137. Mnemonic = THUMB_CMN_MNEMONIC;
  1138. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  1139. snprintf(Context->Operand2,
  1140. sizeof(Context->Operand2),
  1141. "#%d",
  1142. ModifiedImmediate);
  1143. }
  1144. break;
  1145. case THUMB32_DATA_SUB:
  1146. if ((Rd == 0xF) && (SetFlags != 0)) {
  1147. StandardParameters = FALSE;
  1148. Mnemonic = THUMB_CMP_W_MNEMONIC;
  1149. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  1150. snprintf(Context->Operand2,
  1151. sizeof(Context->Operand2),
  1152. "#%d",
  1153. ModifiedImmediate);
  1154. }
  1155. break;
  1156. default:
  1157. break;
  1158. }
  1159. strcpy(Context->Mnemonic, Mnemonic);
  1160. //
  1161. // If the switch statement didn't apply, copy in the regular parameters.
  1162. // The pack half-word is a special case, it changed the opcode but still
  1163. // follows the standard parameters.
  1164. //
  1165. if (StandardParameters != FALSE) {
  1166. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1167. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  1168. snprintf(Context->Operand3,
  1169. sizeof(Context->Operand3),
  1170. "#%d",
  1171. ModifiedImmediate);
  1172. }
  1173. return;
  1174. }
  1175. VOID
  1176. DbgpThumb32DecodeDataPlainImmediate (
  1177. PARM_DISASSEMBLY Context
  1178. )
  1179. /*++
  1180. Routine Description:
  1181. This routine decodes data processing (plain Jane immediate) instructions.
  1182. Arguments:
  1183. Context - Supplies a pointer to the disassembly context.
  1184. Return Value:
  1185. None.
  1186. --*/
  1187. {
  1188. ULONG Immediate;
  1189. ULONG Immediate12;
  1190. ULONG Immediate3;
  1191. ULONG Immediate5;
  1192. ULONG Instruction;
  1193. PSTR LsbString;
  1194. PSTR Mnemonic;
  1195. PSTR *Mnemonics;
  1196. ULONG Op;
  1197. ULONGLONG OperandAddress;
  1198. ULONG Rd;
  1199. ULONG Rn;
  1200. ULONG SetFlags;
  1201. PSTR ShiftMnemonic;
  1202. LONG SignedImmediate;
  1203. ULONG Width;
  1204. PSTR WidthString;
  1205. Instruction = Context->Instruction;
  1206. Rd = (Instruction >> THUMB32_DATA_PLAIN_IMMEDIATE_RD_SHIFT) &
  1207. THUMB_REGISTER16_MASK;
  1208. Rn = (Instruction >> THUMB32_DATA_PLAIN_IMMEDIATE_RN_SHIFT) &
  1209. THUMB_REGISTER16_MASK;
  1210. Op = (Instruction >> THUMB32_DATA_PLAIN_IMMEDIATE_OP_SHIFT) &
  1211. THUMB32_DATA_PLAIN_IMMEDIATE_OP_MASK;
  1212. Immediate3 = (Instruction >>
  1213. THUMB32_DATA_MODIFIED_IMMEDIATE_IMMEDIATE3_SHIFT) &
  1214. THUMB_IMMEDIATE3_MASK;
  1215. Immediate5 = (Instruction >>
  1216. THUMB32_DATA_PLAIN_IMMEDIATE_IMMEDIATE2_SHIFT) &
  1217. THUMB_IMMEDIATE2_MASK;
  1218. Immediate5 |= Immediate3 << 2;
  1219. Immediate12 = (Instruction >>
  1220. THUMB32_DATA_MODIFIED_IMMEDIATE_IMMEDIATE8_SHIFT) &
  1221. THUMB_IMMEDIATE8_MASK;
  1222. Immediate12 |= Immediate3 << 8;
  1223. if ((Instruction & THUMB32_DATA_MODIFIED_IMMEDIATE_IMMEDIATE12) != 0) {
  1224. Immediate12 |= 1 << 11;
  1225. }
  1226. SetFlags = 0;
  1227. if ((Instruction & THUMB32_DATA_SET_FLAGS) != 0) {
  1228. SetFlags = 1;
  1229. }
  1230. Mnemonic = "Unknown thumb.";
  1231. switch (Op) {
  1232. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_ADD:
  1233. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_SUB:
  1234. if (Rn == 0xF) {
  1235. Mnemonic = THUMB_ADR_W_MNEMONIC;
  1236. SignedImmediate = Immediate12;
  1237. strcpy(Context->Operand1, DbgArmRegisterNames[Rn]);
  1238. if (Op == THUMB32_DATA_PLAIN_IMMEDIATE_OP_SUB) {
  1239. SignedImmediate = -SignedImmediate;
  1240. }
  1241. //
  1242. // Calculate the operand address. The immediate is relative to the
  1243. // current PC aligned down to a four-byte boundary.
  1244. //
  1245. OperandAddress = Context->InstructionPointer + 4;
  1246. OperandAddress = THUMB_ALIGN_4(OperandAddress);
  1247. OperandAddress += (LONGLONG)SignedImmediate;
  1248. snprintf(Context->Operand2,
  1249. sizeof(Context->Operand2),
  1250. "[0x%08llx]",
  1251. OperandAddress);
  1252. Context->Result->OperandAddress = OperandAddress;
  1253. Context->Result->AddressIsDestination = FALSE;
  1254. Context->Result->AddressIsValid = TRUE;
  1255. } else {
  1256. Mnemonics = DbgThumb32DataProcessingMnemonics[SetFlags];
  1257. if (Op == THUMB32_DATA_PLAIN_IMMEDIATE_OP_ADD) {
  1258. Mnemonic = Mnemonics[THUMB32_DATA_ADD];
  1259. } else {
  1260. Mnemonic = Mnemonics[THUMB32_DATA_SUB];
  1261. }
  1262. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1263. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  1264. snprintf(Context->Operand3,
  1265. sizeof(Context->Operand3),
  1266. "#%d",
  1267. Immediate12);
  1268. }
  1269. break;
  1270. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_MOV:
  1271. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_MOVT:
  1272. if (Op == THUMB32_DATA_PLAIN_IMMEDIATE_OP_MOV) {
  1273. Mnemonic = THUMB_MOVW_MNEMONIC;
  1274. } else {
  1275. Mnemonic = THUMB_MOVT_MNEMONIC;
  1276. }
  1277. Immediate = Immediate12 |
  1278. (((Instruction >>
  1279. THUMB32_DATA_PLAIN_IMMEDIATE_IMMEDIATE4_SHIFT) &
  1280. THUMB_IMMEDIATE4_MASK) << 12);
  1281. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1282. snprintf(Context->Operand2,
  1283. sizeof(Context->Operand2),
  1284. "#%d",
  1285. Immediate);
  1286. break;
  1287. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_SSAT:
  1288. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_SSAT16:
  1289. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_USAT:
  1290. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_USAT16:
  1291. Immediate = (Instruction >>
  1292. THUMB32_DATA_PLAIN_IMMEDIATE_SAT_IMMEDIATE_SHIFT);
  1293. if (Immediate5 == 0) {
  1294. Immediate &= THUMB32_DATA_PLAIN_IMMEDIATE_SAT_IMMEDIATE4_MASK;
  1295. } else {
  1296. Immediate &= THUMB32_DATA_PLAIN_IMMEDIATE_SAT_IMMEDIATE5_MASK;
  1297. }
  1298. if ((Instruction & THUMB32_DATA_PLAIN_IMMEDIATE_UNSIGNED) != 0) {
  1299. if (Immediate5 == 0) {
  1300. Mnemonic = THUMB_USAT16_MNEMONIC;
  1301. } else {
  1302. Mnemonic = THUMB_USAT_MNEMONIC;
  1303. }
  1304. } else {
  1305. if (Immediate5 == 0) {
  1306. Mnemonic = THUMB_SSAT16_MNEMONIC;
  1307. } else {
  1308. Mnemonic = THUMB_SSAT_MNEMONIC;
  1309. }
  1310. Immediate += 1;
  1311. }
  1312. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1313. sprintf(Context->Operand2, "#%d", Immediate);
  1314. strcpy(Context->Operand3, DbgArmRegisterNames[Rn]);
  1315. if (Immediate5 != 0) {
  1316. ShiftMnemonic = ARM_LSL_MNEMONIC;
  1317. if ((Instruction & THUMB32_DATA_PLAIN_IMMEDIATE_SHIFT_RIGHT) != 0) {
  1318. ShiftMnemonic = ARM_ASR_MNEMONIC;
  1319. }
  1320. sprintf(Context->Operand4, "%s #%d", ShiftMnemonic, Immediate5);
  1321. }
  1322. break;
  1323. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_BFIC:
  1324. if (Rn == 0xF) {
  1325. Mnemonic = THUMB_BFC_MNEMONIC;
  1326. LsbString = Context->Operand2;
  1327. WidthString = Context->Operand3;
  1328. } else {
  1329. Mnemonic = THUMB_BFI_MNEMONIC;
  1330. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  1331. LsbString = Context->Operand3;
  1332. WidthString = Context->Operand4;
  1333. }
  1334. Width = (Instruction >> THUMB32_DATA_PLAIN_IMMEDIATE_MSB_SHIFT) &
  1335. THUMB32_DATA_PLAIN_IMMEDIATE_MSB_MASK;
  1336. Width = Width + 1 - Immediate5;
  1337. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1338. sprintf(LsbString, "#%d", Immediate5);
  1339. sprintf(WidthString, "#%d", Width);
  1340. break;
  1341. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_SBFX:
  1342. case THUMB32_DATA_PLAIN_IMMEDIATE_OP_UBFX:
  1343. if ((Instruction & THUMB32_DATA_PLAIN_IMMEDIATE_UNSIGNED) != 0) {
  1344. Mnemonic = THUMB_UBFX_MNEMONIC;
  1345. } else {
  1346. Mnemonic = THUMB_SBFX_MNEMONIC;
  1347. }
  1348. Width = (Instruction >>
  1349. THUMB32_DATA_PLAIN_IMMEDIATE_WIDTH_MINUS_1_SHIFT) &
  1350. THUMB32_DATA_PLAIN_IMMEDIATE_WIDTH_MINUS_1_MASK;
  1351. Width += 1;
  1352. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1353. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  1354. sprintf(Context->Operand3, "#%d", Immediate5);
  1355. sprintf(Context->Operand4, "#%d", Width);
  1356. break;
  1357. default:
  1358. break;
  1359. }
  1360. strcpy(Context->Mnemonic, Mnemonic);
  1361. return;
  1362. }
  1363. VOID
  1364. DbgpThumb32DecodeBranchAndMiscellaneous (
  1365. PARM_DISASSEMBLY Context
  1366. )
  1367. /*++
  1368. Routine Description:
  1369. This routine decodes branch and miscellaneous instructions.
  1370. Arguments:
  1371. Context - Supplies a pointer to the disassembly context.
  1372. Return Value:
  1373. None.
  1374. --*/
  1375. {
  1376. THUMB_DECODE_WITH_TABLE(Context, DbgThumb32BranchAndMiscellaneousTable);
  1377. return;
  1378. }
  1379. VOID
  1380. DbgpThumb32DecodeMsr (
  1381. PARM_DISASSEMBLY Context
  1382. )
  1383. /*++
  1384. Routine Description:
  1385. This routine decodes MSR (move to status from ARM) instructions.
  1386. Arguments:
  1387. Context - Supplies a pointer to the disassembly context.
  1388. Return Value:
  1389. None.
  1390. --*/
  1391. {
  1392. ULONG Instruction;
  1393. ULONG Mask;
  1394. ULONG Mode;
  1395. PSTR Register;
  1396. ULONG Rn;
  1397. Instruction = Context->Instruction;
  1398. Rn = (Instruction >> THUMB32_MSR_RN_SHIFT) & THUMB_REGISTER16_MASK;
  1399. strcpy(Context->Mnemonic, THUMB_MSR_MNEMONIC);
  1400. if ((Instruction & THUMB32_MSR_BANKED_REGISTER) != 0) {
  1401. Mode = (Instruction >> THUMB32_MSR_MODE_SHIFT) & THUMB32_MSR_MODE_MASK;
  1402. if ((Instruction & THUMB32_MSR_MODE4) != 0) {
  1403. Mode |= 1 << 4;
  1404. }
  1405. if ((Instruction & THUMB32_MSR_SPSR) != 0) {
  1406. Mode |= 1 << 5;
  1407. }
  1408. strcpy(Context->Operand1, DbgArmBankedRegisters[Mode]);
  1409. } else {
  1410. Mask = (Instruction >> THUMB32_MSR_MASK_SHIFT) & THUMB32_MSR_MASK_MASK;
  1411. Register = THUMB_CPSR_STRING;
  1412. if ((Instruction & THUMB32_MSR_SPSR) != 0) {
  1413. Register = THUMB_SPSR_STRING;
  1414. }
  1415. strcpy(Context->Operand1, Register);
  1416. strcat(Context->Operand1, "_");
  1417. if ((Mask & THUMB32_MSR_MASK_C) != 0) {
  1418. strcat(Context->Operand1, "c");
  1419. }
  1420. if ((Mask & THUMB32_MSR_MASK_X) != 0) {
  1421. strcat(Context->Operand1, "x");
  1422. }
  1423. if ((Mask & THUMB32_MSR_MASK_S) != 0) {
  1424. strcat(Context->Operand1, "s");
  1425. }
  1426. if ((Mask & THUMB32_MSR_MASK_F) != 0) {
  1427. strcat(Context->Operand1, "f");
  1428. }
  1429. }
  1430. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  1431. return;
  1432. }
  1433. VOID
  1434. DbgpThumb32DecodeCpsAndHints (
  1435. PARM_DISASSEMBLY Context
  1436. )
  1437. /*++
  1438. Routine Description:
  1439. This routine decodes the CPS (change processor state) instruction, as well
  1440. as memory hints.
  1441. Arguments:
  1442. Context - Supplies a pointer to the disassembly context.
  1443. Return Value:
  1444. None.
  1445. --*/
  1446. {
  1447. ULONG HintOp;
  1448. ULONG Instruction;
  1449. ULONG Mode;
  1450. ULONG Option;
  1451. Instruction = Context->Instruction;
  1452. //
  1453. // If bits 8:6 are zero, then this is CPS.
  1454. //
  1455. if ((Instruction & THUMB32_CPS_MASK) == THUMB32_CPS_VALUE) {
  1456. Mode = Instruction & THUMB32_CPS_MODE_MASK;
  1457. if ((Instruction & THUMB32_CPS_DISABLE) != 0) {
  1458. strcpy(Context->Mnemonic, THUMB_CPS_DISABLE_W_MNEMONIC);
  1459. } else {
  1460. strcpy(Context->Mnemonic, THUMB_CPS_ENABLE_W_MNEMONIC);
  1461. }
  1462. strcpy(Context->Operand1, "");
  1463. if ((Instruction & THUMB32_CPS_FLAG_A) != 0) {
  1464. strcat(Context->Operand1, ARM_CPS_FLAG_A_STRING);
  1465. }
  1466. if ((Instruction & THUMB32_CPS_FLAG_I) != 0) {
  1467. strcat(Context->Operand1, ARM_CPS_FLAG_I_STRING);
  1468. }
  1469. if ((Instruction & THUMB32_CPS_FLAG_F) != 0) {
  1470. strcat(Context->Operand1, ARM_CPS_FLAG_F_STRING);
  1471. }
  1472. if ((Instruction & THUMB32_CPS_CHANGE_MODE) != 0) {
  1473. DbgpArmPrintMode(Context->Operand2, Mode);
  1474. }
  1475. //
  1476. // This is a hint instruction.
  1477. //
  1478. } else {
  1479. HintOp = Instruction & THUMB32_HINT_MASK;
  1480. if ((HintOp & THUMB32_HINT_DBG_MASK) == THUMB32_HINT_DBG_VALUE) {
  1481. Option = Instruction & THUMB32_DBG_OPTION_MASK;
  1482. strcpy(Context->Mnemonic, THUMB_DBG_MNEMONIC);
  1483. snprintf(Context->Operand1,
  1484. sizeof(Context->Operand1),
  1485. "#%d",
  1486. Option);
  1487. } else {
  1488. if (HintOp >= THUMB32_HINT_OP_COUNT) {
  1489. strcpy(Context->Mnemonic, "Undef hint");
  1490. } else {
  1491. strcpy(Context->Mnemonic, DbgThumb32HintMnemonics[HintOp]);
  1492. }
  1493. }
  1494. }
  1495. return;
  1496. }
  1497. VOID
  1498. DbgpThumb32DecodeMiscellaneousControl (
  1499. PARM_DISASSEMBLY Context
  1500. )
  1501. /*++
  1502. Routine Description:
  1503. This routine decodes 32-bit Thumb miscellaneous control instructions.
  1504. Arguments:
  1505. Context - Supplies a pointer to the disassembly context.
  1506. Return Value:
  1507. None.
  1508. --*/
  1509. {
  1510. ULONG Instruction;
  1511. PSTR Mnemonic;
  1512. ULONG Mode;
  1513. ULONG Op;
  1514. Instruction = Context->Instruction;
  1515. Op = (Instruction >> THUMB32_MISCELLANEOUS_CONTROL_OP_SHIFT) &
  1516. THUMB32_MISCELLANEOUS_CONTROL_OP_MASK;
  1517. Mode = Instruction & THUMB32_BARRIER_MODE_MASK;
  1518. if (Op == THUMB32_MISCELLANEOUS_CONTROL_OP_ENTERX) {
  1519. Mnemonic = THUMB_ENTERX_MNEMONIC;
  1520. } else if (Op == THUMB32_MISCELLANEOUS_CONTROL_OP_LEAVEX) {
  1521. Mnemonic = THUMB_LEAVEX_MNEMONIC;
  1522. } else if (Op == THUMB32_MISCELLANEOUS_CONTROL_OP_CLREX) {
  1523. Mnemonic = THUMB_CLREX_MNEMONIC;
  1524. } else if (Op == THUMB32_MISCELLANEOUS_CONTROL_OP_DSB) {
  1525. Mnemonic = THUMB_DSB_MNEMONIC;
  1526. DbgpArmPrintBarrierMode(Context->Operand1, Mode);
  1527. } else if (Op == THUMB32_MISCELLANEOUS_CONTROL_OP_DMB) {
  1528. Mnemonic = THUMB_DMB_MNEMONIC;
  1529. DbgpArmPrintBarrierMode(Context->Operand1, Mode);
  1530. } else if (Op == THUMB32_MISCELLANEOUS_CONTROL_OP_ISB) {
  1531. Mnemonic = THUMB_ISB_MNEMONIC;
  1532. DbgpArmPrintBarrierMode(Context->Operand1, Mode);
  1533. } else {
  1534. Mnemonic = "Undef Misc control";
  1535. }
  1536. strcpy(Context->Mnemonic, Mnemonic);
  1537. return;
  1538. }
  1539. VOID
  1540. DbgpThumb32DecodeBxj (
  1541. PARM_DISASSEMBLY Context
  1542. )
  1543. /*++
  1544. Routine Description:
  1545. This routine decodes 32-bit Thumb BXJ instruction.
  1546. Arguments:
  1547. Context - Supplies a pointer to the disassembly context.
  1548. Return Value:
  1549. None.
  1550. --*/
  1551. {
  1552. ULONG Rm;
  1553. Rm = (Context->Instruction >> THUMB32_BXJ_RM_SHIFT) & THUMB_REGISTER16_MASK;
  1554. strcpy(Context->Mnemonic, THUMB_BXJ_MNEMONIC);
  1555. strcpy(Context->Operand1, DbgArmRegisterNames[Rm]);
  1556. return;
  1557. }
  1558. VOID
  1559. DbgpThumb32DecodeExceptionReturn (
  1560. PARM_DISASSEMBLY Context
  1561. )
  1562. /*++
  1563. Routine Description:
  1564. This routine decodes 32-bit Thumb ERET (exception return) and SUBS pc, lr.
  1565. Arguments:
  1566. Context - Supplies a pointer to the disassembly context.
  1567. Return Value:
  1568. None.
  1569. --*/
  1570. {
  1571. ULONG Immediate8;
  1572. Immediate8 = Context->Instruction & THUMB_IMMEDIATE8_MASK;
  1573. if (Immediate8 == 0) {
  1574. strcpy(Context->Mnemonic, THUMB_ERET_MNEMONIC);
  1575. } else {
  1576. strcpy(Context->Mnemonic, THUMB_SUBS_MNEMONIC);
  1577. strcpy(Context->Operand1, DbgArmRegisterNames[15]);
  1578. strcpy(Context->Operand2, DbgArmRegisterNames[13]);
  1579. snprintf(Context->Operand3,
  1580. sizeof(Context->Operand3),
  1581. "#%d",
  1582. Immediate8);
  1583. }
  1584. return;
  1585. }
  1586. VOID
  1587. DbgpThumb32DecodeMrs (
  1588. PARM_DISASSEMBLY Context
  1589. )
  1590. /*++
  1591. Routine Description:
  1592. This routine decodes 32-bit Thumb MRS (Move to ARM from Status register)
  1593. instructions.
  1594. Arguments:
  1595. Context - Supplies a pointer to the disassembly context.
  1596. Return Value:
  1597. None.
  1598. --*/
  1599. {
  1600. ULONG Instruction;
  1601. ULONG Mode;
  1602. ULONG Rd;
  1603. PSTR Register;
  1604. Instruction = Context->Instruction;
  1605. Rd = (Instruction >> THUMB32_MRS_RD_SHIFT) & THUMB_REGISTER16_MASK;
  1606. strcpy(Context->Mnemonic, THUMB_MRS_MNEMONIC);
  1607. if ((Instruction & THUMB32_MRS_BANKED_REGISTER) != 0) {
  1608. Mode = (Instruction >> THUMB32_MRS_MODE_SHIFT) & THUMB32_MRS_MODE_MASK;
  1609. if ((Instruction & THUMB32_MRS_MODE4) != 0) {
  1610. Mode |= 1 << 4;
  1611. }
  1612. if ((Instruction & THUMB32_MRS_SPSR) != 0) {
  1613. Mode |= 1 << 5;
  1614. }
  1615. strcpy(Context->Operand2, DbgArmBankedRegisters[Mode]);
  1616. } else {
  1617. Register = THUMB_CPSR_STRING;
  1618. if ((Instruction & THUMB32_MRS_SPSR) != 0) {
  1619. Register = THUMB_SPSR_STRING;
  1620. }
  1621. strcpy(Context->Operand2, Register);
  1622. }
  1623. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  1624. return;
  1625. }
  1626. VOID
  1627. DbgpThumb32DecodeHvc (
  1628. PARM_DISASSEMBLY Context
  1629. )
  1630. /*++
  1631. Routine Description:
  1632. This routine decodes 32-bit Thumb HVC (hypervisor call) instruction.
  1633. Arguments:
  1634. Context - Supplies a pointer to the disassembly context.
  1635. Return Value:
  1636. None.
  1637. --*/
  1638. {
  1639. ULONG Immediate16;
  1640. ULONG Instruction;
  1641. Instruction = Context->Instruction;
  1642. Immediate16 = (Instruction & THUMB32_HVC_IMMEDIATE12_MASK) |
  1643. ((Instruction >> THUMB32_HVC_IMMEDIATE4_SHIFT) &
  1644. THUMB32_HVC_IMMEDIATE4_MASK);
  1645. strcpy(Context->Mnemonic, THUMB_HVC_MNEMONIC);
  1646. snprintf(Context->Operand1,
  1647. sizeof(Context->Operand1),
  1648. "#%d",
  1649. Immediate16);
  1650. return;
  1651. }
  1652. VOID
  1653. DbgpThumb32DecodeSmc (
  1654. PARM_DISASSEMBLY Context
  1655. )
  1656. /*++
  1657. Routine Description:
  1658. This routine decodes 32-bit Thumb SMC (secure monitor call) instruction.
  1659. Arguments:
  1660. Context - Supplies a pointer to the disassembly context.
  1661. Return Value:
  1662. None.
  1663. --*/
  1664. {
  1665. ULONG Immediate4;
  1666. ULONG Instruction;
  1667. Instruction = Context->Instruction;
  1668. Immediate4 = (Instruction >> THUMB32_SMC_IMMEDIATE4_SHIFT) &
  1669. THUMB32_SMC_IMMEDIATE4_MASK;
  1670. strcpy(Context->Mnemonic, THUMB_SMC_MNEMONIC);
  1671. snprintf(Context->Operand1,
  1672. sizeof(Context->Operand1),
  1673. "#%d",
  1674. Immediate4);
  1675. return;
  1676. }
  1677. VOID
  1678. DbgpThumb32DecodeBranch (
  1679. PARM_DISASSEMBLY Context
  1680. )
  1681. /*++
  1682. Routine Description:
  1683. This routine decodes 32-bit Thumb branch (both conditional and
  1684. unconditional) instructions.
  1685. Arguments:
  1686. Context - Supplies a pointer to the disassembly context.
  1687. Return Value:
  1688. None.
  1689. --*/
  1690. {
  1691. ULONG Bit;
  1692. ULONG Condition;
  1693. PSTR ConditionString;
  1694. LONG Immediate;
  1695. ULONG Instruction;
  1696. ULONGLONG OperandAddress;
  1697. ULONG SBit;
  1698. Instruction = Context->Instruction;
  1699. Immediate = (Instruction >> THUMB32_B_IMMEDIATE11_SHIFT) &
  1700. THUMB32_B_IMMEDIATE11_MASK;
  1701. Condition = (Instruction >> THUMB32_B_CONDITION_SHIFT) &
  1702. THUMB32_B_CONDITION_MASK;
  1703. ConditionString = "";
  1704. //
  1705. // Handle an unconditional branch, which has a larger range.
  1706. //
  1707. if ((Instruction & THUMB32_B_UNCONDITIONAL_MASK) ==
  1708. THUMB32_B_UNCONDITIONAL_VALUE) {
  1709. Immediate |= ((Instruction >> THUMB32_B_IMMEDIATE10_SHIFT) &
  1710. THUMB_IMMEDIATE10_MASK) << 11;
  1711. //
  1712. // The next two bits are NOT(J2 EOR S) and NOT(J1 EOR S).
  1713. //
  1714. SBit = 0;
  1715. if ((Instruction & THUMB32_B_S_BIT) != 0) {
  1716. SBit = 1;
  1717. }
  1718. Bit = 0;
  1719. if ((Instruction & THUMB32_B_J1_BIT) != 0) {
  1720. Bit = 1;
  1721. }
  1722. Bit = !(Bit ^ SBit);
  1723. if (Bit != 0) {
  1724. Immediate |= 1 << 21;
  1725. }
  1726. Bit = 0;
  1727. if ((Instruction & THUMB32_B_J2_BIT) != 0) {
  1728. Bit = 1;
  1729. }
  1730. Bit = !(Bit ^ SBit);
  1731. if (Bit != 0) {
  1732. Immediate |= 1 << 22;
  1733. }
  1734. if (SBit != 0) {
  1735. Immediate |= 1 << 23;
  1736. }
  1737. Immediate <<= 1;
  1738. //
  1739. // Sign extend.
  1740. //
  1741. if ((Immediate & 0x01000000) != 0) {
  1742. Immediate |= 0xFE000000;
  1743. }
  1744. //
  1745. // Conditional branches sacrifice some range for the encoded condition.
  1746. //
  1747. } else {
  1748. ConditionString = DbgArmConditionCodes[Condition];
  1749. Immediate |= ((Instruction >> THUMB32_B_IMMEDIATE6_SHIFT) &
  1750. THUMB_IMMEDIATE6_MASK) << 11;
  1751. if ((Instruction & THUMB32_B_J1_BIT) != 0) {
  1752. Immediate |= (1 << 17);
  1753. }
  1754. if ((Instruction & THUMB32_B_J2_BIT) != 0) {
  1755. Immediate |= (1 << 18);
  1756. }
  1757. if ((Instruction & THUMB32_B_S_BIT) != 0) {
  1758. Immediate |= (1 << 19);
  1759. }
  1760. Immediate <<= 1;
  1761. //
  1762. // Sign extend.
  1763. //
  1764. if ((Immediate & 0x00100000) != 0) {
  1765. Immediate |= 0xFFE00000;
  1766. }
  1767. }
  1768. snprintf(Context->Mnemonic,
  1769. sizeof(Context->Mnemonic),
  1770. THUMB_B_W_MNEMONIC_FORMAT,
  1771. ConditionString);
  1772. //
  1773. // All of these branches are relative to the PC, which is 4 ahead of the
  1774. // instruction pointer. Calculate the absolute operand address.
  1775. //
  1776. OperandAddress = Context->InstructionPointer + 4;
  1777. OperandAddress += (LONGLONG)Immediate;
  1778. snprintf(Context->Operand1,
  1779. sizeof(Context->Operand1),
  1780. "[0x%08llx]",
  1781. OperandAddress);
  1782. Context->Result->OperandAddress = OperandAddress;
  1783. Context->Result->AddressIsDestination = TRUE;
  1784. Context->Result->AddressIsValid = TRUE;
  1785. return;
  1786. }
  1787. VOID
  1788. DbgpThumb32DecodeUdf (
  1789. PARM_DISASSEMBLY Context
  1790. )
  1791. /*++
  1792. Routine Description:
  1793. This routine decodes 32-bit Thumb undefined instruction (like THE undefined
  1794. instruction).
  1795. Arguments:
  1796. Context - Supplies a pointer to the disassembly context.
  1797. Return Value:
  1798. None.
  1799. --*/
  1800. {
  1801. ULONG Immediate20;
  1802. ULONG Instruction;
  1803. Instruction = Context->Instruction;
  1804. Immediate20 = (Instruction & THUMB_IMMEDIATE12_MASK) |
  1805. ((Instruction >> THUMB32_UDF_IMMEDIATE4_SHIFT) &
  1806. THUMB_IMMEDIATE4_MASK);
  1807. strcpy(Context->Mnemonic, THUMB_UDF_W_MNEMONIC);
  1808. snprintf(Context->Operand1,
  1809. sizeof(Context->Operand1),
  1810. "#%d",
  1811. Immediate20);
  1812. return;
  1813. }
  1814. VOID
  1815. DbgpThumb32DecodeBranchWithLink (
  1816. PARM_DISASSEMBLY Context
  1817. )
  1818. /*++
  1819. Routine Description:
  1820. This routine decodes 32-bit Thumb branch with link instructions.
  1821. Arguments:
  1822. Context - Supplies a pointer to the disassembly context.
  1823. Return Value:
  1824. None.
  1825. --*/
  1826. {
  1827. ULONG Bit;
  1828. LONG Immediate25;
  1829. ULONG Instruction;
  1830. ULONGLONG OperandAddress;
  1831. ULONG SBit;
  1832. Instruction = Context->Instruction;
  1833. Immediate25 = ((Instruction >> THUMB32_BL_IMMEDIATE11_SHIFT) &
  1834. THUMB_IMMEDIATE11_MASK) |
  1835. (((Instruction >> THUMB32_BL_IMMEDIATE10_SHIFT) &
  1836. THUMB_IMMEDIATE10_MASK) << 11);
  1837. if ((Instruction & THUMB32_BL_X_BIT) == 0) {
  1838. Immediate25 &= ~THUMB32_BL_THUMB_BIT;
  1839. }
  1840. //
  1841. // The next two bits are NOT(J1 EOR S) and NOT(J2 EOR S).
  1842. //
  1843. SBit = 0;
  1844. if ((Instruction & THUMB32_B_S_BIT) != 0) {
  1845. SBit = 1;
  1846. }
  1847. Bit = 0;
  1848. if ((Instruction & THUMB32_B_J2_BIT) != 0) {
  1849. Bit = 1;
  1850. }
  1851. Bit = !(Bit ^ SBit);
  1852. if (Bit != 0) {
  1853. Immediate25 |= 1 << 21;
  1854. }
  1855. Bit = 0;
  1856. if ((Instruction & THUMB32_B_J1_BIT) != 0) {
  1857. Bit = 1;
  1858. }
  1859. Bit = !(Bit ^ SBit);
  1860. if (Bit != 0) {
  1861. Immediate25 |= 1 << 22;
  1862. }
  1863. if (SBit != 0) {
  1864. Immediate25 |= 1 << 23;
  1865. }
  1866. Immediate25 <<= 1;
  1867. //
  1868. // Sign extend.
  1869. //
  1870. if ((Immediate25 & 0x00200000) != 0) {
  1871. Immediate25 |= 0xFFC00000;
  1872. }
  1873. //
  1874. // For the BLX encoding, the immediate is relative to "Align(PC, 4)". The
  1875. // PC is four bytes ahead of the instruction pointer and it is an align
  1876. // down operation. The align-down action also strips the low bit from the
  1877. // Thumb instruction point, resulting in the correct ARM address. This is
  1878. // necessay because the destination mode of BLX is ARM.
  1879. //
  1880. OperandAddress = Context->InstructionPointer + 4;
  1881. if ((Instruction & THUMB32_BL_X_BIT) == 0) {
  1882. strcpy(Context->Mnemonic, THUMB_BLX_MNEMONIC);
  1883. OperandAddress = THUMB_ALIGN_4(OperandAddress);
  1884. //
  1885. // BL is relative to the PC.
  1886. //
  1887. } else {
  1888. strcpy(Context->Mnemonic, THUMB_BL_MNEMONIC);
  1889. }
  1890. OperandAddress += (LONGLONG)Immediate25;
  1891. snprintf(Context->Operand1,
  1892. sizeof(Context->Operand1),
  1893. "[0x%08llx]",
  1894. OperandAddress);
  1895. Context->Result->OperandAddress = OperandAddress;
  1896. Context->Result->AddressIsDestination = TRUE;
  1897. Context->Result->AddressIsValid = TRUE;
  1898. return;
  1899. }
  1900. VOID
  1901. DbgpThumb32DecodeLoadStoreSingleItem (
  1902. PARM_DISASSEMBLY Context
  1903. )
  1904. /*++
  1905. Routine Description:
  1906. This routine decodes 32-bit Thumb load/store instructions.
  1907. Arguments:
  1908. Context - Supplies a pointer to the disassembly context.
  1909. Return Value:
  1910. None.
  1911. --*/
  1912. {
  1913. ULONG Instruction;
  1914. Instruction = Context->Instruction;
  1915. if ((Instruction & THUMB32_LOAD_STORE_REGISTER_MASK) ==
  1916. THUMB32_LOAD_STORE_REGISTER_VALUE) {
  1917. DbgpThumb32DecodeLoadStoreRegister(Context);
  1918. } else {
  1919. DbgpThumb32DecodeLoadStoreImmediate(Context);
  1920. }
  1921. return;
  1922. }
  1923. VOID
  1924. DbgpThumb32DecodeLoadStoreImmediate (
  1925. PARM_DISASSEMBLY Context
  1926. )
  1927. /*++
  1928. Routine Description:
  1929. This routine decodes 32-bit Thumb load/store immediate instructions.
  1930. Arguments:
  1931. Context - Supplies a pointer to the disassembly context.
  1932. Return Value:
  1933. None.
  1934. --*/
  1935. {
  1936. LONG Immediate;
  1937. ULONG Instruction;
  1938. ULONG Load;
  1939. ULONG Op;
  1940. ULONGLONG OperandAddress;
  1941. ULONG Rn;
  1942. ULONG Rt;
  1943. Instruction = Context->Instruction;
  1944. Op = (Instruction >> THUMB32_LOAD_STORE_OP_SHIFT) &
  1945. THUMB32_LOAD_STORE_OP_MASK;
  1946. Rn = (Instruction >> THUMB32_LOAD_STORE_IMMEDIATE_RN_SHIFT) &
  1947. THUMB_REGISTER16_MASK;
  1948. Rt = (Instruction >> THUMB32_LOAD_STORE_IMMEDIATE_RT_SHIFT) &
  1949. THUMB_REGISTER16_MASK;
  1950. Load = 0;
  1951. if ((Instruction & THUMB32_LOAD_BIT) != 0) {
  1952. Load = 1;
  1953. }
  1954. //
  1955. // Assume the mnemonic is not unprivileged. This may get altered later.
  1956. //
  1957. if ((Load != 0) && ((Instruction & THUMB32_LOAD_SET_FLAGS) != 0)) {
  1958. strcpy(Context->Mnemonic, DbgThumb32LoadSetFlagsMnemonics[Op]);
  1959. } else {
  1960. strcpy(Context->Mnemonic,
  1961. DbgThumb32LoadStoreMnemonics[Load][Op]);
  1962. }
  1963. //
  1964. // If bit 23 is set, then the pre-index is an immediate12.
  1965. //
  1966. if ((Instruction & THUMB32_LOAD_STORE_IMMEDIATE_LARGE) != 0) {
  1967. Immediate = Instruction & THUMB_IMMEDIATE12_MASK;
  1968. snprintf(Context->Operand2,
  1969. sizeof(Context->Operand2),
  1970. "[%s, #%d]",
  1971. DbgArmRegisterNames[Rn],
  1972. Immediate);
  1973. //
  1974. // There are a few addressing modes, and an immediate8.
  1975. //
  1976. } else {
  1977. Immediate = Instruction & THUMB_IMMEDIATE8_MASK;
  1978. if ((Instruction & THUMB32_LOAD_STORE_IMMEDIATE_ADD) == 0) {
  1979. Immediate = -Immediate;
  1980. }
  1981. if ((Instruction & THUMB32_LOAD_STORE_IMMEDIATE_PREINDEX) != 0) {
  1982. if ((Instruction & THUMB32_LOAD_STORE_IMMEDIATE_WRITE_BACK) != 0) {
  1983. snprintf(Context->Operand2,
  1984. sizeof(Context->Operand2),
  1985. "[%s, #%d]!",
  1986. DbgArmRegisterNames[Rn],
  1987. Immediate);
  1988. } else {
  1989. snprintf(Context->Operand2,
  1990. sizeof(Context->Operand2),
  1991. "[%s, #%d]",
  1992. DbgArmRegisterNames[Rn],
  1993. Immediate);
  1994. }
  1995. } else {
  1996. snprintf(Context->Operand2,
  1997. sizeof(Context->Operand2),
  1998. "[%s], #%d",
  1999. DbgArmRegisterNames[Rn],
  2000. Immediate);
  2001. }
  2002. //
  2003. // It's an unprivileged instruction if both the P (preindex) and U (add)
  2004. // bits are set.
  2005. //
  2006. if (((Instruction & THUMB32_LOAD_STORE_IMMEDIATE_PREINDEX) != 0) &&
  2007. ((Instruction & THUMB32_LOAD_STORE_IMMEDIATE_ADD) != 0)) {
  2008. if ((Load != 0) && ((Instruction & THUMB32_LOAD_SET_FLAGS) != 0)) {
  2009. strcpy(Context->Mnemonic,
  2010. DbgThumb32LoadSetFlagsUnprivilegedMnemonics[Op]);
  2011. } else {
  2012. strcpy(Context->Mnemonic,
  2013. DbgThumb32LoadStoreUnprivilegedMnemonics[Load][Op]);
  2014. }
  2015. }
  2016. }
  2017. //
  2018. // If this is a load relative to the PC, then calculate the absolute
  2019. // operand address and override the second operand with the absolute
  2020. // address.
  2021. //
  2022. if ((Load != 0) && (Rn == 15)) {
  2023. //
  2024. // The address is relative to the PC aligned down to a 4-byte boundary.
  2025. //
  2026. OperandAddress = Context->InstructionPointer + 4;
  2027. OperandAddress = THUMB_ALIGN_4(OperandAddress);
  2028. OperandAddress += Immediate;
  2029. Context->Result->OperandAddress = OperandAddress;
  2030. Context->Result->AddressIsDestination = FALSE;
  2031. Context->Result->AddressIsValid = TRUE;
  2032. snprintf(Context->Operand2,
  2033. sizeof(Context->Operand2),
  2034. "[0x%08llx]",
  2035. OperandAddress);
  2036. }
  2037. //
  2038. // If Rt is 15, then this is actually a preload operation. Copy the second
  2039. // operand to the first.
  2040. //
  2041. if (Rt == 15) {
  2042. strcpy(Context->Mnemonic, DbgThumb32PreloadMnemonics[Op]);
  2043. strcpy(Context->Operand1, Context->Operand2);
  2044. Context->Operand2[0] = '\0';
  2045. } else {
  2046. strcpy(Context->Operand1, DbgArmRegisterNames[Rt]);
  2047. }
  2048. return;
  2049. }
  2050. VOID
  2051. DbgpThumb32DecodeLoadStoreRegister (
  2052. PARM_DISASSEMBLY Context
  2053. )
  2054. /*++
  2055. Routine Description:
  2056. This routine decodes 32-bit Thumb load/store register instructions.
  2057. Arguments:
  2058. Context - Supplies a pointer to the disassembly context.
  2059. Return Value:
  2060. None.
  2061. --*/
  2062. {
  2063. ULONG Immediate2;
  2064. ULONG Instruction;
  2065. ULONG Load;
  2066. ULONG Op;
  2067. ULONG Rm;
  2068. ULONG Rn;
  2069. ULONG Rt;
  2070. Instruction = Context->Instruction;
  2071. Op = (Instruction >> THUMB32_LOAD_STORE_OP_SHIFT) &
  2072. THUMB32_LOAD_STORE_OP_MASK;
  2073. Rm = (Instruction >> THUMB32_LOAD_STORE_REGISTER_RM_SHIFT) &
  2074. THUMB_REGISTER16_MASK;
  2075. Rn = (Instruction >> THUMB32_LOAD_STORE_REGISTER_RN_SHIFT) &
  2076. THUMB_REGISTER16_MASK;
  2077. Rt = (Instruction >> THUMB32_LOAD_STORE_REGISTER_RT_SHIFT) &
  2078. THUMB_REGISTER16_MASK;
  2079. Immediate2 = (Instruction >> THUMB32_LOAD_STORE_REGISTER_IMMEDIATE2_SHIFT) &
  2080. THUMB_IMMEDIATE2_MASK;
  2081. Load = 0;
  2082. if ((Instruction & THUMB32_LOAD_BIT) != 0) {
  2083. Load = 1;
  2084. }
  2085. if ((Load != 0) && ((Instruction & THUMB32_LOAD_SET_FLAGS) != 0)) {
  2086. strcpy(Context->Mnemonic, DbgThumb32LoadSetFlagsMnemonics[Op]);
  2087. } else {
  2088. strcpy(Context->Mnemonic, DbgThumb32LoadStoreMnemonics[Load][Op]);
  2089. }
  2090. if (Immediate2 == 0) {
  2091. snprintf(Context->Operand2,
  2092. sizeof(Context->Operand2),
  2093. "[%s, %s]",
  2094. DbgArmRegisterNames[Rn],
  2095. DbgArmRegisterNames[Rm]);
  2096. } else {
  2097. snprintf(Context->Operand2,
  2098. sizeof(Context->Operand2),
  2099. "[%s, %s, %s #%d]",
  2100. DbgArmRegisterNames[Rn],
  2101. THUMB_SHIFT_TYPE_LSL_STRING,
  2102. DbgArmRegisterNames[Rm],
  2103. Immediate2);
  2104. }
  2105. //
  2106. // If Rt is 15, then this is actually a preload operation. Copy the second
  2107. // operand to the first.
  2108. //
  2109. if (Rt == 15) {
  2110. strcpy(Context->Mnemonic, DbgThumb32PreloadMnemonics[Op]);
  2111. strcpy(Context->Operand1, Context->Operand2);
  2112. strcpy(Context->Operand2, "");
  2113. } else {
  2114. strcpy(Context->Operand1, DbgArmRegisterNames[Rt]);
  2115. }
  2116. return;
  2117. }
  2118. VOID
  2119. DbgpThumb32DecodeDataProcessingRegister (
  2120. PARM_DISASSEMBLY Context
  2121. )
  2122. /*++
  2123. Routine Description:
  2124. This routine decodes 32-bit Thumb data processing (register) instructions.
  2125. Arguments:
  2126. Context - Supplies a pointer to the disassembly context.
  2127. Return Value:
  2128. None.
  2129. --*/
  2130. {
  2131. ULONG Instruction;
  2132. ULONG MiscellaneousOp;
  2133. ULONG Op1;
  2134. ULONG ParallelOp;
  2135. ULONG Rd;
  2136. ULONG Rm;
  2137. ULONG Rn;
  2138. ULONG Rotate;
  2139. ULONG SetFlags;
  2140. ULONG Unsigned;
  2141. Instruction = Context->Instruction;
  2142. SetFlags = 0;
  2143. if ((Instruction & THUMB32_DATA_SET_FLAGS) != 0) {
  2144. SetFlags = 1;
  2145. }
  2146. Op1 = (Instruction >> THUMB32_DATA_PROCESSING_REGISTER_OP1_SHIFT) &
  2147. THUMB32_DATA_PROCESSING_REGISTER_OP1_MASK;
  2148. Rd = (Instruction >> THUMB32_DATA_PROCESSING_REGISTER_RD_SHIFT) &
  2149. THUMB_REGISTER16_MASK;
  2150. Rm = (Instruction >> THUMB32_DATA_PROCESSING_REGISTER_RM_SHIFT) &
  2151. THUMB_REGISTER16_MASK;
  2152. Rn = (Instruction >> THUMB32_DATA_PROCESSING_REGISTER_RN_SHIFT) &
  2153. THUMB_REGISTER16_MASK;
  2154. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  2155. //
  2156. // Handle shift/rotate instructions.
  2157. //
  2158. if ((Instruction & THUMB32_DATA_PROCESSING_REGISTER_SHIFT_MASK) ==
  2159. THUMB32_DATA_PROCESSING_REGISTER_SHIFT_VALUE) {
  2160. strcpy(Context->Mnemonic,
  2161. DbgThumb32DataProcessingShiftMnemonics[SetFlags][Op1 >> 1]);
  2162. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  2163. strcpy(Context->Operand3, DbgArmRegisterNames[Rm]);
  2164. //
  2165. // Handle signed and unsigned extend and add.
  2166. //
  2167. } else if ((Op1 & THUMB32_DATA_PROCESSING_REGISTER_OP1_EXTEND) == 0) {
  2168. Rotate = (Instruction >>
  2169. THUMB32_DATA_PROCESSING_REGISTER_ROTATE_SHIFT) &
  2170. THUMB32_DATA_PROCESSING_REGISTER_ROTATE_MASK;
  2171. Rotate <<= 3;
  2172. if (Op1 < THUMB32_DATA_PROCESSING_REGISTER_OP1_EXTEND_COUNT) {
  2173. if (Rn == 15) {
  2174. strcpy(Context->Mnemonic,
  2175. DbgThumb32ExtendAndAddMnemonics[1][Op1]);
  2176. strcpy(Context->Operand2, DbgArmRegisterNames[Rm]);
  2177. if (Rotate != 0) {
  2178. snprintf(Context->Operand3,
  2179. sizeof(Context->Operand4),
  2180. "ror #%d",
  2181. Rotate);
  2182. }
  2183. } else {
  2184. strcpy(Context->Mnemonic,
  2185. DbgThumb32ExtendAndAddMnemonics[0][Op1]);
  2186. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  2187. strcpy(Context->Operand3, DbgArmRegisterNames[Rm]);
  2188. if (Rotate != 0) {
  2189. snprintf(Context->Operand4,
  2190. sizeof(Context->Operand4),
  2191. "ror #%d",
  2192. Rotate);
  2193. }
  2194. }
  2195. }
  2196. //
  2197. // Handle parallel addition and subtraction, both signed and unsigned.
  2198. //
  2199. } else if ((Instruction & THUMB32_DATA_PROCESSING_REGISTER_PARALLEL) == 0) {
  2200. Unsigned = 0;
  2201. if ((Instruction & THUMB32_DATA_PROCESSING_REGISTER_UNSIGNED) != 0) {
  2202. Unsigned = 1;
  2203. }
  2204. ParallelOp = (Instruction >>
  2205. THUMB32_DATA_PROCESSING_PARALLEL_OP1_SHIFT) &
  2206. THUMB32_DATA_PROCESSING_PARALLEL_OP1_MASK;
  2207. ParallelOp |= ((Instruction >>
  2208. THUMB32_DATA_PROCESSING_PARALLEL_OP2_SHIFT) &
  2209. THUMB32_DATA_PROCESSING_PARALLEL_OP2_MASK) << 3;
  2210. if (ParallelOp < THUMB32_DATA_PROCESSING_PARALLEL_OP_COUNT) {
  2211. strcpy(Context->Mnemonic,
  2212. DbgThumb32ParallelArithmeticMnemonics[Unsigned][ParallelOp]);
  2213. }
  2214. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  2215. strcpy(Context->Operand3, DbgArmRegisterNames[Rm]);
  2216. //
  2217. // Handle miscellaneous instructions.
  2218. //
  2219. } else {
  2220. MiscellaneousOp = (Instruction >>
  2221. THUMB32_DATA_PROCESSING_MISCELLANEOUS_OP2_SHIFT) &
  2222. THUMB32_DATA_PROCESSING_MISCELLANEOUS_OP2_MASK;
  2223. MiscellaneousOp |= ((Instruction >>
  2224. THUMB32_DATA_PROCESSING_MISCELLANEOUS_OP1_SHIFT) &
  2225. THUMB32_DATA_PROCESSING_MISCELLANEOUS_OP1_MASK) <<
  2226. 2;
  2227. strcpy(Context->Mnemonic,
  2228. DbgThumb32DataProcessingMiscellaneousMnemonics[MiscellaneousOp]);
  2229. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  2230. if (Rn != Rm) {
  2231. strcpy(Context->Operand3, DbgArmRegisterNames[Rm]);
  2232. }
  2233. }
  2234. return;
  2235. }
  2236. VOID
  2237. DbgpThumb32DecodeMultiplyAccumulate (
  2238. PARM_DISASSEMBLY Context
  2239. )
  2240. /*++
  2241. Routine Description:
  2242. This routine decodes 32-bit Thumb multiply and multiply/accumulate
  2243. instructions.
  2244. Arguments:
  2245. Context - Supplies a pointer to the disassembly context.
  2246. Return Value:
  2247. None.
  2248. --*/
  2249. {
  2250. ULONG Instruction;
  2251. ULONG Op1;
  2252. ULONG Op2;
  2253. ULONG Ra;
  2254. ULONG Rd;
  2255. ULONG Rm;
  2256. ULONG Rn;
  2257. ULONG Top;
  2258. Instruction = Context->Instruction;
  2259. Ra = (Instruction >> THUMB32_MULTIPLY_RA_SHIFT) & THUMB_REGISTER16_MASK;
  2260. Rd = (Instruction >> THUMB32_MULTIPLY_RD_SHIFT) & THUMB_REGISTER16_MASK;
  2261. Rm = (Instruction >> THUMB32_MULTIPLY_RN_SHIFT) & THUMB_REGISTER16_MASK;
  2262. Rn = (Instruction >> THUMB32_MULTIPLY_RM_SHIFT) & THUMB_REGISTER16_MASK;
  2263. Op1 = (Instruction >> THUMB32_MULTIPLY_OP1_SHIFT) &
  2264. THUMB32_MULTIPLY_OP1_MASK;
  2265. Op2 = (Instruction >> THUMB32_MULTIPLY_OP2_SHIFT) &
  2266. THUMB32_MULTIPLY_OP2_MASK;
  2267. strcpy(Context->Operand1, DbgArmRegisterNames[Rd]);
  2268. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  2269. strcpy(Context->Operand3, DbgArmRegisterNames[Rm]);
  2270. if (Ra != 15) {
  2271. strcpy(Context->Mnemonic, DbgThumb32MultiplyMnemonics[0][Op1]);
  2272. strcpy(Context->Operand4, DbgArmRegisterNames[Ra]);
  2273. } else {
  2274. strcpy(Context->Mnemonic, DbgThumb32MultiplyMnemonics[1][Op1]);
  2275. }
  2276. if ((Op1 == THUMB32_MULTIPLY_OP1_MLS) &&
  2277. (Op2 == THUMB32_MULTIPLY_OP2_MLS)) {
  2278. strcpy(Context->Mnemonic, THUMB_MLS_MNEMONIC);
  2279. }
  2280. //
  2281. // Instructions that operate on only the top or bottom half of some
  2282. // registers (Rn and maybe Rm) get endings for top or bottom.
  2283. //
  2284. if (Op1 == THUMB32_MULTIPLY_OP1_HALF_HALF) {
  2285. Top = 0;
  2286. if ((Instruction & THUMB32_MULTIPLY_RN_TOP) != 0) {
  2287. Top = 1;
  2288. }
  2289. strcat(Context->Mnemonic, DbgThumb32MultiplyTopBottomMnemonics[Top]);
  2290. }
  2291. if ((Op1 == THUMB32_MULTIPLY_OP1_HALF_HALF) ||
  2292. (Op1 == THUMB32_MULTIPLY_OP1_WORD_HALF)) {
  2293. Top = 0;
  2294. if ((Instruction & THUMB32_MULTIPLY_RM_TOP) != 0) {
  2295. Top = 1;
  2296. }
  2297. strcat(Context->Mnemonic, DbgThumb32MultiplyTopBottomMnemonics[Top]);
  2298. }
  2299. //
  2300. // A couple of instructions have an optional X or R tagged on the end.
  2301. //
  2302. if ((Op1 == THUMB32_MULTIPLY_OP1_SMAD) ||
  2303. (Op1 == THUMB32_MULTIPLY_OP1_SMSD)) {
  2304. if ((Instruction & THUMB32_MULTIPLY_DUAL_CROSS) != 0) {
  2305. strcat(Context->Mnemonic, THUMB_MULTIPLY_CROSS_MNEMONIC);
  2306. }
  2307. } else if (Op1 == THUMB32_MULTIPLY_OP1_SMML) {
  2308. if ((Instruction & THUMB32_MULTIPLY_ROUND) != 0) {
  2309. strcat(Context->Mnemonic, THUMB_MULTIPLY_ROUND_MNEMONIC);
  2310. }
  2311. }
  2312. return;
  2313. }
  2314. VOID
  2315. DbgpThumb32DecodeLongMultiplyDivide (
  2316. PARM_DISASSEMBLY Context
  2317. )
  2318. /*++
  2319. Routine Description:
  2320. This routine decodes 32-bit Thumb long multiply and divide instructions.
  2321. Arguments:
  2322. Context - Supplies a pointer to the disassembly context.
  2323. Return Value:
  2324. None.
  2325. --*/
  2326. {
  2327. ULONG Cross;
  2328. ULONG Instruction;
  2329. ULONG Op1;
  2330. ULONG Op2;
  2331. ULONG RdHigh;
  2332. ULONG RdLow;
  2333. ULONG Rm;
  2334. ULONG Rn;
  2335. ULONG Top;
  2336. Instruction = Context->Instruction;
  2337. RdHigh = (Instruction >> THUMB32_LONG_MULTIPLY_RD_HIGH_SHIFT) &
  2338. THUMB_REGISTER16_MASK;
  2339. RdLow = (Instruction >> THUMB32_LONG_MULTIPLY_RD_LOW_SHIFT) &
  2340. THUMB_REGISTER16_MASK;
  2341. Rm = (Instruction >> THUMB32_LONG_MULTIPLY_RM_SHIFT) &
  2342. THUMB_REGISTER16_MASK;
  2343. Rn = (Instruction >> THUMB32_LONG_MULTIPLY_RN_SHIFT) &
  2344. THUMB_REGISTER16_MASK;
  2345. Op1 = (Instruction >> THUMB32_LONG_MULTIPLY_OP1_SHIFT) &
  2346. THUMB32_LONG_MULTIPLY_OP1_MASK;
  2347. Op2 = (Instruction >> THUMB32_LONG_MULTIPLY_OP2_SHIFT) &
  2348. THUMB32_LONG_MULTIPLY_OP2_MASK;
  2349. Cross = 0;
  2350. strcpy(Context->Mnemonic, DbgThumb32LongMultiplyMnemonics[Op1]);
  2351. if (Op1 == THUMB32_LONG_MULTIPLY_OP1_SMLA) {
  2352. if ((Op2 & THUMB32_LONG_MULTIPLY_OP2_SMLA_HALF_MASK) ==
  2353. THUMB32_LONG_MULTIPLY_OP2_SMLA_HALF_VALUE) {
  2354. Top = 0;
  2355. if ((Instruction & THUMB32_MULTIPLY_RN_TOP) != 0) {
  2356. Top = 1;
  2357. }
  2358. strcat(Context->Mnemonic,
  2359. DbgThumb32MultiplyTopBottomMnemonics[Top]);
  2360. Top = 0;
  2361. if ((Instruction & THUMB32_MULTIPLY_RM_TOP) != 0) {
  2362. Top = 1;
  2363. }
  2364. strcat(Context->Mnemonic,
  2365. DbgThumb32MultiplyTopBottomMnemonics[Top]);
  2366. } else if ((Op2 & THUMB32_LONG_MULTIPLY_OP2_SMLALD_MASK) ==
  2367. THUMB32_LONG_MULTIPLY_OP2_SMLALD_VALUE) {
  2368. strcpy(Context->Mnemonic, THUMB_SMLALD_MNEMONIC);
  2369. Cross = Instruction & THUMB32_MULTIPLY_DUAL_CROSS;
  2370. }
  2371. } else if (Op1 == THUMB32_LONG_MULTIPLY_OP1_SMLSLD) {
  2372. Cross = Instruction & THUMB32_MULTIPLY_DUAL_CROSS;
  2373. }
  2374. if (Cross != 0) {
  2375. strcat(Context->Mnemonic, THUMB_MULTIPLY_CROSS_MNEMONIC);
  2376. }
  2377. strcpy(Context->Operand1, DbgArmRegisterNames[RdHigh]);
  2378. if (RdLow != 15) {
  2379. strcpy(Context->Operand2, DbgArmRegisterNames[RdLow]);
  2380. strcpy(Context->Operand3, DbgArmRegisterNames[Rn]);
  2381. strcpy(Context->Operand4, DbgArmRegisterNames[Rm]);
  2382. } else {
  2383. strcpy(Context->Operand2, DbgArmRegisterNames[Rn]);
  2384. strcpy(Context->Operand3, DbgArmRegisterNames[Rm]);
  2385. }
  2386. return;
  2387. }
  2388. VOID
  2389. DbgpThumbDecodeImmediateShift (
  2390. PSTR Destination,
  2391. ULONG DestinationSize,
  2392. ULONG Register,
  2393. ULONG Type,
  2394. ULONG Immediate
  2395. )
  2396. /*++
  2397. Routine Description:
  2398. This routine performs the operation known in the ARM ARM as
  2399. DecodeImmShift().
  2400. Arguments:
  2401. Destination - Supplies the destination to write to.
  2402. DestinationSize - Supplies the size of the destination.
  2403. Register - Supplies the base register.
  2404. Type - Supplies the shift type.
  2405. Immediate - Supplies the shift value.
  2406. Return Value:
  2407. None.
  2408. --*/
  2409. {
  2410. PSTR ShiftTypeString;
  2411. switch (Type) {
  2412. case THUMB_SHIFT_TYPE_LSL:
  2413. if (Immediate == 0) {
  2414. snprintf(Destination,
  2415. DestinationSize,
  2416. "%s",
  2417. DbgArmRegisterNames[Register]);
  2418. } else {
  2419. snprintf(Destination,
  2420. DestinationSize,
  2421. "%s, %s #%d",
  2422. DbgArmRegisterNames[Register],
  2423. THUMB_SHIFT_TYPE_LSL_STRING,
  2424. Immediate);
  2425. }
  2426. break;
  2427. case THUMB_SHIFT_TYPE_LSR:
  2428. if (Immediate == 0) {
  2429. Immediate = 32;
  2430. }
  2431. snprintf(Destination,
  2432. DestinationSize,
  2433. "%s, %s #%d",
  2434. DbgArmRegisterNames[Register],
  2435. THUMB_SHIFT_TYPE_LSR_STRING,
  2436. Immediate);
  2437. break;
  2438. case THUMB_SHIFT_TYPE_ASR:
  2439. if (Immediate == 0) {
  2440. Immediate = 32;
  2441. }
  2442. snprintf(Destination,
  2443. DestinationSize,
  2444. "%s, %s #%d",
  2445. DbgArmRegisterNames[Register],
  2446. THUMB_SHIFT_TYPE_ASR_STRING,
  2447. Immediate);
  2448. break;
  2449. case THUMB_SHIFT_TYPE_ROR:
  2450. default:
  2451. ShiftTypeString = THUMB_SHIFT_TYPE_ROR_STRING;
  2452. if (Immediate == 0) {
  2453. Immediate = 1;
  2454. ShiftTypeString = THUMB_SHIFT_TYPE_RRX_STRING;
  2455. }
  2456. snprintf(Destination,
  2457. DestinationSize,
  2458. "%s, %s #%d",
  2459. DbgArmRegisterNames[Register],
  2460. ShiftTypeString,
  2461. Immediate);
  2462. break;
  2463. }
  2464. return;
  2465. }
  2466. ULONG
  2467. DbgpThumb32DecodeModifiedImmediate (
  2468. ULONG Immediate12
  2469. )
  2470. /*++
  2471. Routine Description:
  2472. This routine performs the operation known in the ARM ARM as
  2473. ThumbExpandImm(), expanding a modified immediate.
  2474. Arguments:
  2475. Immediate12 - Supplies the 12 bit immediate.
  2476. Return Value:
  2477. Returns the expanded immediate.
  2478. --*/
  2479. {
  2480. ULONG Result;
  2481. ULONG RotateCount;
  2482. if ((Immediate12 & THUMB32_MODIFIED_IMMEDIATE_OP_MASK) ==
  2483. THUMB32_MODIFIED_IMMEDIATE_OP_NO_ROTATE) {
  2484. Result = Immediate12 & THUMB_IMMEDIATE8_MASK;
  2485. switch ((Immediate12 >> 8) & 0x3) {
  2486. //
  2487. // 00000000 00000000 00000000 abcdefgh
  2488. //
  2489. case 0x0:
  2490. break;
  2491. //
  2492. // 00000000 abcdefgh 00000000 abcdefgh
  2493. //
  2494. case 0x1:
  2495. Result |= Result << 16;
  2496. break;
  2497. //
  2498. // abcdefgh 00000000 abcdefgh 00000000
  2499. //
  2500. case 0x2:
  2501. Result |= Result << 16;
  2502. Result <<= 8;
  2503. break;
  2504. //
  2505. // abcdefgh abcdefgh abcdefgh abcdefgh
  2506. //
  2507. case 0x3:
  2508. Result |= Result << 16;
  2509. Result |= Result << 8;
  2510. break;
  2511. default:
  2512. break;
  2513. }
  2514. //
  2515. // Rotate bits 6:0 (with a 1 tacked on the MSB) by the amount specified in
  2516. // bits 7-11.
  2517. //
  2518. } else {
  2519. Result = (Immediate12 & THUMB32_MODIFIED_IMMEDIATE_CONSTANT_MASK) |
  2520. THUMB32_MODIFIED_IMMEDIATE_EXTRA_ONE;
  2521. RotateCount = (Immediate12 >> THUMB32_MODIFIED_IMMEDIATE_ROTATE_SHIFT) &
  2522. THUMB32_MODIFIED_IMMEDIATE_ROTATE_MASK;
  2523. //
  2524. // Perform the rotate.
  2525. //
  2526. Result = (Result >> RotateCount) | (Result << (32 - RotateCount));
  2527. }
  2528. return Result;
  2529. }