thm32dis.c 78 KB

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