emulate.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347
  1. /******************************************************************************
  2. * emulate.c
  3. *
  4. * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
  5. *
  6. * Copyright (c) 2005 Keir Fraser
  7. *
  8. * Linux coding style, mod r/m decoder, segment base fixes, real-mode
  9. * privileged instructions:
  10. *
  11. * Copyright (C) 2006 Qumranet
  12. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  13. *
  14. * Avi Kivity <avi@qumranet.com>
  15. * Yaniv Kamay <yaniv@qumranet.com>
  16. *
  17. * This work is licensed under the terms of the GNU GPL, version 2. See
  18. * the COPYING file in the top-level directory.
  19. *
  20. * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
  21. */
  22. #include <linux/kvm_host.h>
  23. #include "kvm_cache_regs.h"
  24. #include <linux/module.h>
  25. #include <asm/kvm_emulate.h>
  26. #include "x86.h"
  27. #include "tss.h"
  28. /*
  29. * Opcode effective-address decode tables.
  30. * Note that we only emulate instructions that have at least one memory
  31. * operand (excluding implicit stack references). We assume that stack
  32. * references and instruction fetches will never occur in special memory
  33. * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
  34. * not be handled.
  35. */
  36. /* Operand sizes: 8-bit operands or specified/overridden size. */
  37. #define ByteOp (1<<0) /* 8-bit operands. */
  38. /* Destination operand type. */
  39. #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */
  40. #define DstReg (2<<1) /* Register operand. */
  41. #define DstMem (3<<1) /* Memory operand. */
  42. #define DstAcc (4<<1) /* Destination Accumulator */
  43. #define DstDI (5<<1) /* Destination is in ES:(E)DI */
  44. #define DstMem64 (6<<1) /* 64bit memory operand */
  45. #define DstImmUByte (7<<1) /* 8-bit unsigned immediate operand */
  46. #define DstDX (8<<1) /* Destination is in DX register */
  47. #define DstMask (0xf<<1)
  48. /* Source operand type. */
  49. #define SrcNone (0<<5) /* No source operand. */
  50. #define SrcReg (1<<5) /* Register operand. */
  51. #define SrcMem (2<<5) /* Memory operand. */
  52. #define SrcMem16 (3<<5) /* Memory operand (16-bit). */
  53. #define SrcMem32 (4<<5) /* Memory operand (32-bit). */
  54. #define SrcImm (5<<5) /* Immediate operand. */
  55. #define SrcImmByte (6<<5) /* 8-bit sign-extended immediate operand. */
  56. #define SrcOne (7<<5) /* Implied '1' */
  57. #define SrcImmUByte (8<<5) /* 8-bit unsigned immediate operand. */
  58. #define SrcImmU (9<<5) /* Immediate operand, unsigned */
  59. #define SrcSI (0xa<<5) /* Source is in the DS:RSI */
  60. #define SrcImmFAddr (0xb<<5) /* Source is immediate far address */
  61. #define SrcMemFAddr (0xc<<5) /* Source is far address in memory */
  62. #define SrcAcc (0xd<<5) /* Source Accumulator */
  63. #define SrcImmU16 (0xe<<5) /* Immediate operand, unsigned, 16 bits */
  64. #define SrcDX (0xf<<5) /* Source is in DX register */
  65. #define SrcMask (0xf<<5)
  66. /* Generic ModRM decode. */
  67. #define ModRM (1<<9)
  68. /* Destination is only written; never read. */
  69. #define Mov (1<<10)
  70. #define BitOp (1<<11)
  71. #define MemAbs (1<<12) /* Memory operand is absolute displacement */
  72. #define String (1<<13) /* String instruction (rep capable) */
  73. #define Stack (1<<14) /* Stack instruction (push/pop) */
  74. #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
  75. #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
  76. #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
  77. #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
  78. #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
  79. #define Sse (1<<18) /* SSE Vector instruction */
  80. /* Misc flags */
  81. #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
  82. #define VendorSpecific (1<<22) /* Vendor specific instruction */
  83. #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
  84. #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
  85. #define Undefined (1<<25) /* No Such Instruction */
  86. #define Lock (1<<26) /* lock prefix is allowed for the instruction */
  87. #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
  88. #define No64 (1<<28)
  89. /* Source 2 operand type */
  90. #define Src2None (0<<29)
  91. #define Src2CL (1<<29)
  92. #define Src2ImmByte (2<<29)
  93. #define Src2One (3<<29)
  94. #define Src2Imm (4<<29)
  95. #define Src2Mask (7<<29)
  96. #define X2(x...) x, x
  97. #define X3(x...) X2(x), x
  98. #define X4(x...) X2(x), X2(x)
  99. #define X5(x...) X4(x), x
  100. #define X6(x...) X4(x), X2(x)
  101. #define X7(x...) X4(x), X3(x)
  102. #define X8(x...) X4(x), X4(x)
  103. #define X16(x...) X8(x), X8(x)
  104. struct opcode {
  105. u32 flags;
  106. u8 intercept;
  107. union {
  108. int (*execute)(struct x86_emulate_ctxt *ctxt);
  109. struct opcode *group;
  110. struct group_dual *gdual;
  111. struct gprefix *gprefix;
  112. } u;
  113. int (*check_perm)(struct x86_emulate_ctxt *ctxt);
  114. };
  115. struct group_dual {
  116. struct opcode mod012[8];
  117. struct opcode mod3[8];
  118. };
  119. struct gprefix {
  120. struct opcode pfx_no;
  121. struct opcode pfx_66;
  122. struct opcode pfx_f2;
  123. struct opcode pfx_f3;
  124. };
  125. /* EFLAGS bit definitions. */
  126. #define EFLG_ID (1<<21)
  127. #define EFLG_VIP (1<<20)
  128. #define EFLG_VIF (1<<19)
  129. #define EFLG_AC (1<<18)
  130. #define EFLG_VM (1<<17)
  131. #define EFLG_RF (1<<16)
  132. #define EFLG_IOPL (3<<12)
  133. #define EFLG_NT (1<<14)
  134. #define EFLG_OF (1<<11)
  135. #define EFLG_DF (1<<10)
  136. #define EFLG_IF (1<<9)
  137. #define EFLG_TF (1<<8)
  138. #define EFLG_SF (1<<7)
  139. #define EFLG_ZF (1<<6)
  140. #define EFLG_AF (1<<4)
  141. #define EFLG_PF (1<<2)
  142. #define EFLG_CF (1<<0)
  143. #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
  144. #define EFLG_RESERVED_ONE_MASK 2
  145. /*
  146. * Instruction emulation:
  147. * Most instructions are emulated directly via a fragment of inline assembly
  148. * code. This allows us to save/restore EFLAGS and thus very easily pick up
  149. * any modified flags.
  150. */
  151. #if defined(CONFIG_X86_64)
  152. #define _LO32 "k" /* force 32-bit operand */
  153. #define _STK "%%rsp" /* stack pointer */
  154. #elif defined(__i386__)
  155. #define _LO32 "" /* force 32-bit operand */
  156. #define _STK "%%esp" /* stack pointer */
  157. #endif
  158. /*
  159. * These EFLAGS bits are restored from saved value during emulation, and
  160. * any changes are written back to the saved value after emulation.
  161. */
  162. #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF)
  163. /* Before executing instruction: restore necessary bits in EFLAGS. */
  164. #define _PRE_EFLAGS(_sav, _msk, _tmp) \
  165. /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \
  166. "movl %"_sav",%"_LO32 _tmp"; " \
  167. "push %"_tmp"; " \
  168. "push %"_tmp"; " \
  169. "movl %"_msk",%"_LO32 _tmp"; " \
  170. "andl %"_LO32 _tmp",("_STK"); " \
  171. "pushf; " \
  172. "notl %"_LO32 _tmp"; " \
  173. "andl %"_LO32 _tmp",("_STK"); " \
  174. "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \
  175. "pop %"_tmp"; " \
  176. "orl %"_LO32 _tmp",("_STK"); " \
  177. "popf; " \
  178. "pop %"_sav"; "
  179. /* After executing instruction: write-back necessary bits in EFLAGS. */
  180. #define _POST_EFLAGS(_sav, _msk, _tmp) \
  181. /* _sav |= EFLAGS & _msk; */ \
  182. "pushf; " \
  183. "pop %"_tmp"; " \
  184. "andl %"_msk",%"_LO32 _tmp"; " \
  185. "orl %"_LO32 _tmp",%"_sav"; "
  186. #ifdef CONFIG_X86_64
  187. #define ON64(x) x
  188. #else
  189. #define ON64(x)
  190. #endif
  191. #define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix, _dsttype) \
  192. do { \
  193. __asm__ __volatile__ ( \
  194. _PRE_EFLAGS("0", "4", "2") \
  195. _op _suffix " %"_x"3,%1; " \
  196. _POST_EFLAGS("0", "4", "2") \
  197. : "=m" (_eflags), "+q" (*(_dsttype*)&(_dst).val),\
  198. "=&r" (_tmp) \
  199. : _y ((_src).val), "i" (EFLAGS_MASK)); \
  200. } while (0)
  201. /* Raw emulation: instruction has two explicit operands. */
  202. #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \
  203. do { \
  204. unsigned long _tmp; \
  205. \
  206. switch ((_dst).bytes) { \
  207. case 2: \
  208. ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w",u16);\
  209. break; \
  210. case 4: \
  211. ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l",u32);\
  212. break; \
  213. case 8: \
  214. ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q",u64)); \
  215. break; \
  216. } \
  217. } while (0)
  218. #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \
  219. do { \
  220. unsigned long _tmp; \
  221. switch ((_dst).bytes) { \
  222. case 1: \
  223. ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b",u8); \
  224. break; \
  225. default: \
  226. __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
  227. _wx, _wy, _lx, _ly, _qx, _qy); \
  228. break; \
  229. } \
  230. } while (0)
  231. /* Source operand is byte-sized and may be restricted to just %cl. */
  232. #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \
  233. __emulate_2op(_op, _src, _dst, _eflags, \
  234. "b", "c", "b", "c", "b", "c", "b", "c")
  235. /* Source operand is byte, word, long or quad sized. */
  236. #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \
  237. __emulate_2op(_op, _src, _dst, _eflags, \
  238. "b", "q", "w", "r", _LO32, "r", "", "r")
  239. /* Source operand is word, long or quad sized. */
  240. #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \
  241. __emulate_2op_nobyte(_op, _src, _dst, _eflags, \
  242. "w", "r", _LO32, "r", "", "r")
  243. /* Instruction has three operands and one operand is stored in ECX register */
  244. #define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \
  245. do { \
  246. unsigned long _tmp; \
  247. _type _clv = (_cl).val; \
  248. _type _srcv = (_src).val; \
  249. _type _dstv = (_dst).val; \
  250. \
  251. __asm__ __volatile__ ( \
  252. _PRE_EFLAGS("0", "5", "2") \
  253. _op _suffix " %4,%1 \n" \
  254. _POST_EFLAGS("0", "5", "2") \
  255. : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \
  256. : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \
  257. ); \
  258. \
  259. (_cl).val = (unsigned long) _clv; \
  260. (_src).val = (unsigned long) _srcv; \
  261. (_dst).val = (unsigned long) _dstv; \
  262. } while (0)
  263. #define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \
  264. do { \
  265. switch ((_dst).bytes) { \
  266. case 2: \
  267. __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
  268. "w", unsigned short); \
  269. break; \
  270. case 4: \
  271. __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
  272. "l", unsigned int); \
  273. break; \
  274. case 8: \
  275. ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \
  276. "q", unsigned long)); \
  277. break; \
  278. } \
  279. } while (0)
  280. #define __emulate_1op(_op, _dst, _eflags, _suffix) \
  281. do { \
  282. unsigned long _tmp; \
  283. \
  284. __asm__ __volatile__ ( \
  285. _PRE_EFLAGS("0", "3", "2") \
  286. _op _suffix " %1; " \
  287. _POST_EFLAGS("0", "3", "2") \
  288. : "=m" (_eflags), "+m" ((_dst).val), \
  289. "=&r" (_tmp) \
  290. : "i" (EFLAGS_MASK)); \
  291. } while (0)
  292. /* Instruction has only one explicit operand (no source operand). */
  293. #define emulate_1op(_op, _dst, _eflags) \
  294. do { \
  295. switch ((_dst).bytes) { \
  296. case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \
  297. case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \
  298. case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \
  299. case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \
  300. } \
  301. } while (0)
  302. #define __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags, _suffix) \
  303. do { \
  304. unsigned long _tmp; \
  305. \
  306. __asm__ __volatile__ ( \
  307. _PRE_EFLAGS("0", "4", "1") \
  308. _op _suffix " %5; " \
  309. _POST_EFLAGS("0", "4", "1") \
  310. : "=m" (_eflags), "=&r" (_tmp), \
  311. "+a" (_rax), "+d" (_rdx) \
  312. : "i" (EFLAGS_MASK), "m" ((_src).val), \
  313. "a" (_rax), "d" (_rdx)); \
  314. } while (0)
  315. #define __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _suffix, _ex) \
  316. do { \
  317. unsigned long _tmp; \
  318. \
  319. __asm__ __volatile__ ( \
  320. _PRE_EFLAGS("0", "5", "1") \
  321. "1: \n\t" \
  322. _op _suffix " %6; " \
  323. "2: \n\t" \
  324. _POST_EFLAGS("0", "5", "1") \
  325. ".pushsection .fixup,\"ax\" \n\t" \
  326. "3: movb $1, %4 \n\t" \
  327. "jmp 2b \n\t" \
  328. ".popsection \n\t" \
  329. _ASM_EXTABLE(1b, 3b) \
  330. : "=m" (_eflags), "=&r" (_tmp), \
  331. "+a" (_rax), "+d" (_rdx), "+qm"(_ex) \
  332. : "i" (EFLAGS_MASK), "m" ((_src).val), \
  333. "a" (_rax), "d" (_rdx)); \
  334. } while (0)
  335. /* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */
  336. #define emulate_1op_rax_rdx(_op, _src, _rax, _rdx, _eflags) \
  337. do { \
  338. switch((_src).bytes) { \
  339. case 1: \
  340. __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
  341. _eflags, "b"); \
  342. break; \
  343. case 2: \
  344. __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
  345. _eflags, "w"); \
  346. break; \
  347. case 4: \
  348. __emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
  349. _eflags, "l"); \
  350. break; \
  351. case 8: \
  352. ON64(__emulate_1op_rax_rdx(_op, _src, _rax, _rdx, \
  353. _eflags, "q")); \
  354. break; \
  355. } \
  356. } while (0)
  357. #define emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, _eflags, _ex) \
  358. do { \
  359. switch((_src).bytes) { \
  360. case 1: \
  361. __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
  362. _eflags, "b", _ex); \
  363. break; \
  364. case 2: \
  365. __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
  366. _eflags, "w", _ex); \
  367. break; \
  368. case 4: \
  369. __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
  370. _eflags, "l", _ex); \
  371. break; \
  372. case 8: ON64( \
  373. __emulate_1op_rax_rdx_ex(_op, _src, _rax, _rdx, \
  374. _eflags, "q", _ex)); \
  375. break; \
  376. } \
  377. } while (0)
  378. static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
  379. enum x86_intercept intercept,
  380. enum x86_intercept_stage stage)
  381. {
  382. struct x86_instruction_info info = {
  383. .intercept = intercept,
  384. .rep_prefix = ctxt->decode.rep_prefix,
  385. .modrm_mod = ctxt->decode.modrm_mod,
  386. .modrm_reg = ctxt->decode.modrm_reg,
  387. .modrm_rm = ctxt->decode.modrm_rm,
  388. .src_val = ctxt->decode.src.val64,
  389. .src_bytes = ctxt->decode.src.bytes,
  390. .dst_bytes = ctxt->decode.dst.bytes,
  391. .ad_bytes = ctxt->decode.ad_bytes,
  392. .next_rip = ctxt->eip,
  393. };
  394. return ctxt->ops->intercept(ctxt, &info, stage);
  395. }
  396. static inline unsigned long ad_mask(struct decode_cache *c)
  397. {
  398. return (1UL << (c->ad_bytes << 3)) - 1;
  399. }
  400. /* Access/update address held in a register, based on addressing mode. */
  401. static inline unsigned long
  402. address_mask(struct decode_cache *c, unsigned long reg)
  403. {
  404. if (c->ad_bytes == sizeof(unsigned long))
  405. return reg;
  406. else
  407. return reg & ad_mask(c);
  408. }
  409. static inline unsigned long
  410. register_address(struct decode_cache *c, unsigned long reg)
  411. {
  412. return address_mask(c, reg);
  413. }
  414. static inline void
  415. register_address_increment(struct decode_cache *c, unsigned long *reg, int inc)
  416. {
  417. if (c->ad_bytes == sizeof(unsigned long))
  418. *reg += inc;
  419. else
  420. *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c));
  421. }
  422. static inline void jmp_rel(struct decode_cache *c, int rel)
  423. {
  424. register_address_increment(c, &c->eip, rel);
  425. }
  426. static u32 desc_limit_scaled(struct desc_struct *desc)
  427. {
  428. u32 limit = get_desc_limit(desc);
  429. return desc->g ? (limit << 12) | 0xfff : limit;
  430. }
  431. static void set_seg_override(struct decode_cache *c, int seg)
  432. {
  433. c->has_seg_override = true;
  434. c->seg_override = seg;
  435. }
  436. static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
  437. {
  438. if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
  439. return 0;
  440. return ctxt->ops->get_cached_segment_base(ctxt, seg);
  441. }
  442. static unsigned seg_override(struct x86_emulate_ctxt *ctxt,
  443. struct decode_cache *c)
  444. {
  445. if (!c->has_seg_override)
  446. return 0;
  447. return c->seg_override;
  448. }
  449. static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
  450. u32 error, bool valid)
  451. {
  452. ctxt->exception.vector = vec;
  453. ctxt->exception.error_code = error;
  454. ctxt->exception.error_code_valid = valid;
  455. return X86EMUL_PROPAGATE_FAULT;
  456. }
  457. static int emulate_db(struct x86_emulate_ctxt *ctxt)
  458. {
  459. return emulate_exception(ctxt, DB_VECTOR, 0, false);
  460. }
  461. static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
  462. {
  463. return emulate_exception(ctxt, GP_VECTOR, err, true);
  464. }
  465. static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
  466. {
  467. return emulate_exception(ctxt, SS_VECTOR, err, true);
  468. }
  469. static int emulate_ud(struct x86_emulate_ctxt *ctxt)
  470. {
  471. return emulate_exception(ctxt, UD_VECTOR, 0, false);
  472. }
  473. static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
  474. {
  475. return emulate_exception(ctxt, TS_VECTOR, err, true);
  476. }
  477. static int emulate_de(struct x86_emulate_ctxt *ctxt)
  478. {
  479. return emulate_exception(ctxt, DE_VECTOR, 0, false);
  480. }
  481. static int emulate_nm(struct x86_emulate_ctxt *ctxt)
  482. {
  483. return emulate_exception(ctxt, NM_VECTOR, 0, false);
  484. }
  485. static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
  486. {
  487. u16 selector;
  488. struct desc_struct desc;
  489. ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
  490. return selector;
  491. }
  492. static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
  493. unsigned seg)
  494. {
  495. u16 dummy;
  496. u32 base3;
  497. struct desc_struct desc;
  498. ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
  499. ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
  500. }
  501. static int __linearize(struct x86_emulate_ctxt *ctxt,
  502. struct segmented_address addr,
  503. unsigned size, bool write, bool fetch,
  504. ulong *linear)
  505. {
  506. struct decode_cache *c = &ctxt->decode;
  507. struct desc_struct desc;
  508. bool usable;
  509. ulong la;
  510. u32 lim;
  511. u16 sel;
  512. unsigned cpl, rpl;
  513. la = seg_base(ctxt, addr.seg) + addr.ea;
  514. switch (ctxt->mode) {
  515. case X86EMUL_MODE_REAL:
  516. break;
  517. case X86EMUL_MODE_PROT64:
  518. if (((signed long)la << 16) >> 16 != la)
  519. return emulate_gp(ctxt, 0);
  520. break;
  521. default:
  522. usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
  523. addr.seg);
  524. if (!usable)
  525. goto bad;
  526. /* code segment or read-only data segment */
  527. if (((desc.type & 8) || !(desc.type & 2)) && write)
  528. goto bad;
  529. /* unreadable code segment */
  530. if (!fetch && (desc.type & 8) && !(desc.type & 2))
  531. goto bad;
  532. lim = desc_limit_scaled(&desc);
  533. if ((desc.type & 8) || !(desc.type & 4)) {
  534. /* expand-up segment */
  535. if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
  536. goto bad;
  537. } else {
  538. /* exapand-down segment */
  539. if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim)
  540. goto bad;
  541. lim = desc.d ? 0xffffffff : 0xffff;
  542. if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim)
  543. goto bad;
  544. }
  545. cpl = ctxt->ops->cpl(ctxt);
  546. rpl = sel & 3;
  547. cpl = max(cpl, rpl);
  548. if (!(desc.type & 8)) {
  549. /* data segment */
  550. if (cpl > desc.dpl)
  551. goto bad;
  552. } else if ((desc.type & 8) && !(desc.type & 4)) {
  553. /* nonconforming code segment */
  554. if (cpl != desc.dpl)
  555. goto bad;
  556. } else if ((desc.type & 8) && (desc.type & 4)) {
  557. /* conforming code segment */
  558. if (cpl < desc.dpl)
  559. goto bad;
  560. }
  561. break;
  562. }
  563. if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : c->ad_bytes != 8)
  564. la &= (u32)-1;
  565. *linear = la;
  566. return X86EMUL_CONTINUE;
  567. bad:
  568. if (addr.seg == VCPU_SREG_SS)
  569. return emulate_ss(ctxt, addr.seg);
  570. else
  571. return emulate_gp(ctxt, addr.seg);
  572. }
  573. static int linearize(struct x86_emulate_ctxt *ctxt,
  574. struct segmented_address addr,
  575. unsigned size, bool write,
  576. ulong *linear)
  577. {
  578. return __linearize(ctxt, addr, size, write, false, linear);
  579. }
  580. static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
  581. struct segmented_address addr,
  582. void *data,
  583. unsigned size)
  584. {
  585. int rc;
  586. ulong linear;
  587. rc = linearize(ctxt, addr, size, false, &linear);
  588. if (rc != X86EMUL_CONTINUE)
  589. return rc;
  590. return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception);
  591. }
  592. static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt,
  593. unsigned long eip, u8 *dest)
  594. {
  595. struct fetch_cache *fc = &ctxt->decode.fetch;
  596. int rc;
  597. int size, cur_size;
  598. if (eip == fc->end) {
  599. unsigned long linear;
  600. struct segmented_address addr = { .seg=VCPU_SREG_CS, .ea=eip};
  601. cur_size = fc->end - fc->start;
  602. size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip));
  603. rc = __linearize(ctxt, addr, size, false, true, &linear);
  604. if (rc != X86EMUL_CONTINUE)
  605. return rc;
  606. rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size,
  607. size, &ctxt->exception);
  608. if (rc != X86EMUL_CONTINUE)
  609. return rc;
  610. fc->end += size;
  611. }
  612. *dest = fc->data[eip - fc->start];
  613. return X86EMUL_CONTINUE;
  614. }
  615. static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
  616. unsigned long eip, void *dest, unsigned size)
  617. {
  618. int rc;
  619. /* x86 instructions are limited to 15 bytes. */
  620. if (eip + size - ctxt->eip > 15)
  621. return X86EMUL_UNHANDLEABLE;
  622. while (size--) {
  623. rc = do_insn_fetch_byte(ctxt, eip++, dest++);
  624. if (rc != X86EMUL_CONTINUE)
  625. return rc;
  626. }
  627. return X86EMUL_CONTINUE;
  628. }
  629. /* Fetch next part of the instruction being emulated. */
  630. #define insn_fetch(_type, _size, _eip) \
  631. ({ unsigned long _x; \
  632. rc = do_insn_fetch(ctxt, (_eip), &_x, (_size)); \
  633. if (rc != X86EMUL_CONTINUE) \
  634. goto done; \
  635. (_eip) += (_size); \
  636. (_type)_x; \
  637. })
  638. #define insn_fetch_arr(_arr, _size, _eip) \
  639. ({ rc = do_insn_fetch(ctxt, (_eip), _arr, (_size)); \
  640. if (rc != X86EMUL_CONTINUE) \
  641. goto done; \
  642. (_eip) += (_size); \
  643. })
  644. /*
  645. * Given the 'reg' portion of a ModRM byte, and a register block, return a
  646. * pointer into the block that addresses the relevant register.
  647. * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
  648. */
  649. static void *decode_register(u8 modrm_reg, unsigned long *regs,
  650. int highbyte_regs)
  651. {
  652. void *p;
  653. p = &regs[modrm_reg];
  654. if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
  655. p = (unsigned char *)&regs[modrm_reg & 3] + 1;
  656. return p;
  657. }
  658. static int read_descriptor(struct x86_emulate_ctxt *ctxt,
  659. struct segmented_address addr,
  660. u16 *size, unsigned long *address, int op_bytes)
  661. {
  662. int rc;
  663. if (op_bytes == 2)
  664. op_bytes = 3;
  665. *address = 0;
  666. rc = segmented_read_std(ctxt, addr, size, 2);
  667. if (rc != X86EMUL_CONTINUE)
  668. return rc;
  669. addr.ea += 2;
  670. rc = segmented_read_std(ctxt, addr, address, op_bytes);
  671. return rc;
  672. }
  673. static int test_cc(unsigned int condition, unsigned int flags)
  674. {
  675. int rc = 0;
  676. switch ((condition & 15) >> 1) {
  677. case 0: /* o */
  678. rc |= (flags & EFLG_OF);
  679. break;
  680. case 1: /* b/c/nae */
  681. rc |= (flags & EFLG_CF);
  682. break;
  683. case 2: /* z/e */
  684. rc |= (flags & EFLG_ZF);
  685. break;
  686. case 3: /* be/na */
  687. rc |= (flags & (EFLG_CF|EFLG_ZF));
  688. break;
  689. case 4: /* s */
  690. rc |= (flags & EFLG_SF);
  691. break;
  692. case 5: /* p/pe */
  693. rc |= (flags & EFLG_PF);
  694. break;
  695. case 7: /* le/ng */
  696. rc |= (flags & EFLG_ZF);
  697. /* fall through */
  698. case 6: /* l/nge */
  699. rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF));
  700. break;
  701. }
  702. /* Odd condition identifiers (lsb == 1) have inverted sense. */
  703. return (!!rc ^ (condition & 1));
  704. }
  705. static void fetch_register_operand(struct operand *op)
  706. {
  707. switch (op->bytes) {
  708. case 1:
  709. op->val = *(u8 *)op->addr.reg;
  710. break;
  711. case 2:
  712. op->val = *(u16 *)op->addr.reg;
  713. break;
  714. case 4:
  715. op->val = *(u32 *)op->addr.reg;
  716. break;
  717. case 8:
  718. op->val = *(u64 *)op->addr.reg;
  719. break;
  720. }
  721. }
  722. static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
  723. {
  724. ctxt->ops->get_fpu(ctxt);
  725. switch (reg) {
  726. case 0: asm("movdqu %%xmm0, %0" : "=m"(*data)); break;
  727. case 1: asm("movdqu %%xmm1, %0" : "=m"(*data)); break;
  728. case 2: asm("movdqu %%xmm2, %0" : "=m"(*data)); break;
  729. case 3: asm("movdqu %%xmm3, %0" : "=m"(*data)); break;
  730. case 4: asm("movdqu %%xmm4, %0" : "=m"(*data)); break;
  731. case 5: asm("movdqu %%xmm5, %0" : "=m"(*data)); break;
  732. case 6: asm("movdqu %%xmm6, %0" : "=m"(*data)); break;
  733. case 7: asm("movdqu %%xmm7, %0" : "=m"(*data)); break;
  734. #ifdef CONFIG_X86_64
  735. case 8: asm("movdqu %%xmm8, %0" : "=m"(*data)); break;
  736. case 9: asm("movdqu %%xmm9, %0" : "=m"(*data)); break;
  737. case 10: asm("movdqu %%xmm10, %0" : "=m"(*data)); break;
  738. case 11: asm("movdqu %%xmm11, %0" : "=m"(*data)); break;
  739. case 12: asm("movdqu %%xmm12, %0" : "=m"(*data)); break;
  740. case 13: asm("movdqu %%xmm13, %0" : "=m"(*data)); break;
  741. case 14: asm("movdqu %%xmm14, %0" : "=m"(*data)); break;
  742. case 15: asm("movdqu %%xmm15, %0" : "=m"(*data)); break;
  743. #endif
  744. default: BUG();
  745. }
  746. ctxt->ops->put_fpu(ctxt);
  747. }
  748. static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
  749. int reg)
  750. {
  751. ctxt->ops->get_fpu(ctxt);
  752. switch (reg) {
  753. case 0: asm("movdqu %0, %%xmm0" : : "m"(*data)); break;
  754. case 1: asm("movdqu %0, %%xmm1" : : "m"(*data)); break;
  755. case 2: asm("movdqu %0, %%xmm2" : : "m"(*data)); break;
  756. case 3: asm("movdqu %0, %%xmm3" : : "m"(*data)); break;
  757. case 4: asm("movdqu %0, %%xmm4" : : "m"(*data)); break;
  758. case 5: asm("movdqu %0, %%xmm5" : : "m"(*data)); break;
  759. case 6: asm("movdqu %0, %%xmm6" : : "m"(*data)); break;
  760. case 7: asm("movdqu %0, %%xmm7" : : "m"(*data)); break;
  761. #ifdef CONFIG_X86_64
  762. case 8: asm("movdqu %0, %%xmm8" : : "m"(*data)); break;
  763. case 9: asm("movdqu %0, %%xmm9" : : "m"(*data)); break;
  764. case 10: asm("movdqu %0, %%xmm10" : : "m"(*data)); break;
  765. case 11: asm("movdqu %0, %%xmm11" : : "m"(*data)); break;
  766. case 12: asm("movdqu %0, %%xmm12" : : "m"(*data)); break;
  767. case 13: asm("movdqu %0, %%xmm13" : : "m"(*data)); break;
  768. case 14: asm("movdqu %0, %%xmm14" : : "m"(*data)); break;
  769. case 15: asm("movdqu %0, %%xmm15" : : "m"(*data)); break;
  770. #endif
  771. default: BUG();
  772. }
  773. ctxt->ops->put_fpu(ctxt);
  774. }
  775. static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
  776. struct operand *op,
  777. struct decode_cache *c,
  778. int inhibit_bytereg)
  779. {
  780. unsigned reg = c->modrm_reg;
  781. int highbyte_regs = c->rex_prefix == 0;
  782. if (!(c->d & ModRM))
  783. reg = (c->b & 7) | ((c->rex_prefix & 1) << 3);
  784. if (c->d & Sse) {
  785. op->type = OP_XMM;
  786. op->bytes = 16;
  787. op->addr.xmm = reg;
  788. read_sse_reg(ctxt, &op->vec_val, reg);
  789. return;
  790. }
  791. op->type = OP_REG;
  792. if ((c->d & ByteOp) && !inhibit_bytereg) {
  793. op->addr.reg = decode_register(reg, c->regs, highbyte_regs);
  794. op->bytes = 1;
  795. } else {
  796. op->addr.reg = decode_register(reg, c->regs, 0);
  797. op->bytes = c->op_bytes;
  798. }
  799. fetch_register_operand(op);
  800. op->orig_val = op->val;
  801. }
  802. static int decode_modrm(struct x86_emulate_ctxt *ctxt,
  803. struct operand *op)
  804. {
  805. struct decode_cache *c = &ctxt->decode;
  806. u8 sib;
  807. int index_reg = 0, base_reg = 0, scale;
  808. int rc = X86EMUL_CONTINUE;
  809. ulong modrm_ea = 0;
  810. if (c->rex_prefix) {
  811. c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */
  812. index_reg = (c->rex_prefix & 2) << 2; /* REX.X */
  813. c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */
  814. }
  815. c->modrm = insn_fetch(u8, 1, c->eip);
  816. c->modrm_mod |= (c->modrm & 0xc0) >> 6;
  817. c->modrm_reg |= (c->modrm & 0x38) >> 3;
  818. c->modrm_rm |= (c->modrm & 0x07);
  819. c->modrm_seg = VCPU_SREG_DS;
  820. if (c->modrm_mod == 3) {
  821. op->type = OP_REG;
  822. op->bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  823. op->addr.reg = decode_register(c->modrm_rm,
  824. c->regs, c->d & ByteOp);
  825. if (c->d & Sse) {
  826. op->type = OP_XMM;
  827. op->bytes = 16;
  828. op->addr.xmm = c->modrm_rm;
  829. read_sse_reg(ctxt, &op->vec_val, c->modrm_rm);
  830. return rc;
  831. }
  832. fetch_register_operand(op);
  833. return rc;
  834. }
  835. op->type = OP_MEM;
  836. if (c->ad_bytes == 2) {
  837. unsigned bx = c->regs[VCPU_REGS_RBX];
  838. unsigned bp = c->regs[VCPU_REGS_RBP];
  839. unsigned si = c->regs[VCPU_REGS_RSI];
  840. unsigned di = c->regs[VCPU_REGS_RDI];
  841. /* 16-bit ModR/M decode. */
  842. switch (c->modrm_mod) {
  843. case 0:
  844. if (c->modrm_rm == 6)
  845. modrm_ea += insn_fetch(u16, 2, c->eip);
  846. break;
  847. case 1:
  848. modrm_ea += insn_fetch(s8, 1, c->eip);
  849. break;
  850. case 2:
  851. modrm_ea += insn_fetch(u16, 2, c->eip);
  852. break;
  853. }
  854. switch (c->modrm_rm) {
  855. case 0:
  856. modrm_ea += bx + si;
  857. break;
  858. case 1:
  859. modrm_ea += bx + di;
  860. break;
  861. case 2:
  862. modrm_ea += bp + si;
  863. break;
  864. case 3:
  865. modrm_ea += bp + di;
  866. break;
  867. case 4:
  868. modrm_ea += si;
  869. break;
  870. case 5:
  871. modrm_ea += di;
  872. break;
  873. case 6:
  874. if (c->modrm_mod != 0)
  875. modrm_ea += bp;
  876. break;
  877. case 7:
  878. modrm_ea += bx;
  879. break;
  880. }
  881. if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
  882. (c->modrm_rm == 6 && c->modrm_mod != 0))
  883. c->modrm_seg = VCPU_SREG_SS;
  884. modrm_ea = (u16)modrm_ea;
  885. } else {
  886. /* 32/64-bit ModR/M decode. */
  887. if ((c->modrm_rm & 7) == 4) {
  888. sib = insn_fetch(u8, 1, c->eip);
  889. index_reg |= (sib >> 3) & 7;
  890. base_reg |= sib & 7;
  891. scale = sib >> 6;
  892. if ((base_reg & 7) == 5 && c->modrm_mod == 0)
  893. modrm_ea += insn_fetch(s32, 4, c->eip);
  894. else
  895. modrm_ea += c->regs[base_reg];
  896. if (index_reg != 4)
  897. modrm_ea += c->regs[index_reg] << scale;
  898. } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) {
  899. if (ctxt->mode == X86EMUL_MODE_PROT64)
  900. c->rip_relative = 1;
  901. } else
  902. modrm_ea += c->regs[c->modrm_rm];
  903. switch (c->modrm_mod) {
  904. case 0:
  905. if (c->modrm_rm == 5)
  906. modrm_ea += insn_fetch(s32, 4, c->eip);
  907. break;
  908. case 1:
  909. modrm_ea += insn_fetch(s8, 1, c->eip);
  910. break;
  911. case 2:
  912. modrm_ea += insn_fetch(s32, 4, c->eip);
  913. break;
  914. }
  915. }
  916. op->addr.mem.ea = modrm_ea;
  917. done:
  918. return rc;
  919. }
  920. static int decode_abs(struct x86_emulate_ctxt *ctxt,
  921. struct operand *op)
  922. {
  923. struct decode_cache *c = &ctxt->decode;
  924. int rc = X86EMUL_CONTINUE;
  925. op->type = OP_MEM;
  926. switch (c->ad_bytes) {
  927. case 2:
  928. op->addr.mem.ea = insn_fetch(u16, 2, c->eip);
  929. break;
  930. case 4:
  931. op->addr.mem.ea = insn_fetch(u32, 4, c->eip);
  932. break;
  933. case 8:
  934. op->addr.mem.ea = insn_fetch(u64, 8, c->eip);
  935. break;
  936. }
  937. done:
  938. return rc;
  939. }
  940. static void fetch_bit_operand(struct decode_cache *c)
  941. {
  942. long sv = 0, mask;
  943. if (c->dst.type == OP_MEM && c->src.type == OP_REG) {
  944. mask = ~(c->dst.bytes * 8 - 1);
  945. if (c->src.bytes == 2)
  946. sv = (s16)c->src.val & (s16)mask;
  947. else if (c->src.bytes == 4)
  948. sv = (s32)c->src.val & (s32)mask;
  949. c->dst.addr.mem.ea += (sv >> 3);
  950. }
  951. /* only subword offset */
  952. c->src.val &= (c->dst.bytes << 3) - 1;
  953. }
  954. static int read_emulated(struct x86_emulate_ctxt *ctxt,
  955. unsigned long addr, void *dest, unsigned size)
  956. {
  957. int rc;
  958. struct read_cache *mc = &ctxt->decode.mem_read;
  959. while (size) {
  960. int n = min(size, 8u);
  961. size -= n;
  962. if (mc->pos < mc->end)
  963. goto read_cached;
  964. rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, n,
  965. &ctxt->exception);
  966. if (rc != X86EMUL_CONTINUE)
  967. return rc;
  968. mc->end += n;
  969. read_cached:
  970. memcpy(dest, mc->data + mc->pos, n);
  971. mc->pos += n;
  972. dest += n;
  973. addr += n;
  974. }
  975. return X86EMUL_CONTINUE;
  976. }
  977. static int segmented_read(struct x86_emulate_ctxt *ctxt,
  978. struct segmented_address addr,
  979. void *data,
  980. unsigned size)
  981. {
  982. int rc;
  983. ulong linear;
  984. rc = linearize(ctxt, addr, size, false, &linear);
  985. if (rc != X86EMUL_CONTINUE)
  986. return rc;
  987. return read_emulated(ctxt, linear, data, size);
  988. }
  989. static int segmented_write(struct x86_emulate_ctxt *ctxt,
  990. struct segmented_address addr,
  991. const void *data,
  992. unsigned size)
  993. {
  994. int rc;
  995. ulong linear;
  996. rc = linearize(ctxt, addr, size, true, &linear);
  997. if (rc != X86EMUL_CONTINUE)
  998. return rc;
  999. return ctxt->ops->write_emulated(ctxt, linear, data, size,
  1000. &ctxt->exception);
  1001. }
  1002. static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
  1003. struct segmented_address addr,
  1004. const void *orig_data, const void *data,
  1005. unsigned size)
  1006. {
  1007. int rc;
  1008. ulong linear;
  1009. rc = linearize(ctxt, addr, size, true, &linear);
  1010. if (rc != X86EMUL_CONTINUE)
  1011. return rc;
  1012. return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
  1013. size, &ctxt->exception);
  1014. }
  1015. static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
  1016. unsigned int size, unsigned short port,
  1017. void *dest)
  1018. {
  1019. struct read_cache *rc = &ctxt->decode.io_read;
  1020. if (rc->pos == rc->end) { /* refill pio read ahead */
  1021. struct decode_cache *c = &ctxt->decode;
  1022. unsigned int in_page, n;
  1023. unsigned int count = c->rep_prefix ?
  1024. address_mask(c, c->regs[VCPU_REGS_RCX]) : 1;
  1025. in_page = (ctxt->eflags & EFLG_DF) ?
  1026. offset_in_page(c->regs[VCPU_REGS_RDI]) :
  1027. PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]);
  1028. n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size,
  1029. count);
  1030. if (n == 0)
  1031. n = 1;
  1032. rc->pos = rc->end = 0;
  1033. if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
  1034. return 0;
  1035. rc->end = n * size;
  1036. }
  1037. memcpy(dest, rc->data + rc->pos, size);
  1038. rc->pos += size;
  1039. return 1;
  1040. }
  1041. static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
  1042. u16 selector, struct desc_ptr *dt)
  1043. {
  1044. struct x86_emulate_ops *ops = ctxt->ops;
  1045. if (selector & 1 << 2) {
  1046. struct desc_struct desc;
  1047. u16 sel;
  1048. memset (dt, 0, sizeof *dt);
  1049. if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR))
  1050. return;
  1051. dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
  1052. dt->address = get_desc_base(&desc);
  1053. } else
  1054. ops->get_gdt(ctxt, dt);
  1055. }
  1056. /* allowed just for 8 bytes segments */
  1057. static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
  1058. u16 selector, struct desc_struct *desc)
  1059. {
  1060. struct desc_ptr dt;
  1061. u16 index = selector >> 3;
  1062. ulong addr;
  1063. get_descriptor_table_ptr(ctxt, selector, &dt);
  1064. if (dt.size < index * 8 + 7)
  1065. return emulate_gp(ctxt, selector & 0xfffc);
  1066. addr = dt.address + index * 8;
  1067. return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc,
  1068. &ctxt->exception);
  1069. }
  1070. /* allowed just for 8 bytes segments */
  1071. static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
  1072. u16 selector, struct desc_struct *desc)
  1073. {
  1074. struct desc_ptr dt;
  1075. u16 index = selector >> 3;
  1076. ulong addr;
  1077. get_descriptor_table_ptr(ctxt, selector, &dt);
  1078. if (dt.size < index * 8 + 7)
  1079. return emulate_gp(ctxt, selector & 0xfffc);
  1080. addr = dt.address + index * 8;
  1081. return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc,
  1082. &ctxt->exception);
  1083. }
  1084. /* Does not support long mode */
  1085. static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
  1086. u16 selector, int seg)
  1087. {
  1088. struct desc_struct seg_desc;
  1089. u8 dpl, rpl, cpl;
  1090. unsigned err_vec = GP_VECTOR;
  1091. u32 err_code = 0;
  1092. bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
  1093. int ret;
  1094. memset(&seg_desc, 0, sizeof seg_desc);
  1095. if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86)
  1096. || ctxt->mode == X86EMUL_MODE_REAL) {
  1097. /* set real mode segment descriptor */
  1098. set_desc_base(&seg_desc, selector << 4);
  1099. set_desc_limit(&seg_desc, 0xffff);
  1100. seg_desc.type = 3;
  1101. seg_desc.p = 1;
  1102. seg_desc.s = 1;
  1103. goto load;
  1104. }
  1105. /* NULL selector is not valid for TR, CS and SS */
  1106. if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
  1107. && null_selector)
  1108. goto exception;
  1109. /* TR should be in GDT only */
  1110. if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
  1111. goto exception;
  1112. if (null_selector) /* for NULL selector skip all following checks */
  1113. goto load;
  1114. ret = read_segment_descriptor(ctxt, selector, &seg_desc);
  1115. if (ret != X86EMUL_CONTINUE)
  1116. return ret;
  1117. err_code = selector & 0xfffc;
  1118. err_vec = GP_VECTOR;
  1119. /* can't load system descriptor into segment selecor */
  1120. if (seg <= VCPU_SREG_GS && !seg_desc.s)
  1121. goto exception;
  1122. if (!seg_desc.p) {
  1123. err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
  1124. goto exception;
  1125. }
  1126. rpl = selector & 3;
  1127. dpl = seg_desc.dpl;
  1128. cpl = ctxt->ops->cpl(ctxt);
  1129. switch (seg) {
  1130. case VCPU_SREG_SS:
  1131. /*
  1132. * segment is not a writable data segment or segment
  1133. * selector's RPL != CPL or segment selector's RPL != CPL
  1134. */
  1135. if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
  1136. goto exception;
  1137. break;
  1138. case VCPU_SREG_CS:
  1139. if (!(seg_desc.type & 8))
  1140. goto exception;
  1141. if (seg_desc.type & 4) {
  1142. /* conforming */
  1143. if (dpl > cpl)
  1144. goto exception;
  1145. } else {
  1146. /* nonconforming */
  1147. if (rpl > cpl || dpl != cpl)
  1148. goto exception;
  1149. }
  1150. /* CS(RPL) <- CPL */
  1151. selector = (selector & 0xfffc) | cpl;
  1152. break;
  1153. case VCPU_SREG_TR:
  1154. if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
  1155. goto exception;
  1156. break;
  1157. case VCPU_SREG_LDTR:
  1158. if (seg_desc.s || seg_desc.type != 2)
  1159. goto exception;
  1160. break;
  1161. default: /* DS, ES, FS, or GS */
  1162. /*
  1163. * segment is not a data or readable code segment or
  1164. * ((segment is a data or nonconforming code segment)
  1165. * and (both RPL and CPL > DPL))
  1166. */
  1167. if ((seg_desc.type & 0xa) == 0x8 ||
  1168. (((seg_desc.type & 0xc) != 0xc) &&
  1169. (rpl > dpl && cpl > dpl)))
  1170. goto exception;
  1171. break;
  1172. }
  1173. if (seg_desc.s) {
  1174. /* mark segment as accessed */
  1175. seg_desc.type |= 1;
  1176. ret = write_segment_descriptor(ctxt, selector, &seg_desc);
  1177. if (ret != X86EMUL_CONTINUE)
  1178. return ret;
  1179. }
  1180. load:
  1181. ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg);
  1182. return X86EMUL_CONTINUE;
  1183. exception:
  1184. emulate_exception(ctxt, err_vec, err_code, true);
  1185. return X86EMUL_PROPAGATE_FAULT;
  1186. }
  1187. static void write_register_operand(struct operand *op)
  1188. {
  1189. /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
  1190. switch (op->bytes) {
  1191. case 1:
  1192. *(u8 *)op->addr.reg = (u8)op->val;
  1193. break;
  1194. case 2:
  1195. *(u16 *)op->addr.reg = (u16)op->val;
  1196. break;
  1197. case 4:
  1198. *op->addr.reg = (u32)op->val;
  1199. break; /* 64b: zero-extend */
  1200. case 8:
  1201. *op->addr.reg = op->val;
  1202. break;
  1203. }
  1204. }
  1205. static int writeback(struct x86_emulate_ctxt *ctxt)
  1206. {
  1207. int rc;
  1208. struct decode_cache *c = &ctxt->decode;
  1209. switch (c->dst.type) {
  1210. case OP_REG:
  1211. write_register_operand(&c->dst);
  1212. break;
  1213. case OP_MEM:
  1214. if (c->lock_prefix)
  1215. rc = segmented_cmpxchg(ctxt,
  1216. c->dst.addr.mem,
  1217. &c->dst.orig_val,
  1218. &c->dst.val,
  1219. c->dst.bytes);
  1220. else
  1221. rc = segmented_write(ctxt,
  1222. c->dst.addr.mem,
  1223. &c->dst.val,
  1224. c->dst.bytes);
  1225. if (rc != X86EMUL_CONTINUE)
  1226. return rc;
  1227. break;
  1228. case OP_XMM:
  1229. write_sse_reg(ctxt, &c->dst.vec_val, c->dst.addr.xmm);
  1230. break;
  1231. case OP_NONE:
  1232. /* no writeback */
  1233. break;
  1234. default:
  1235. break;
  1236. }
  1237. return X86EMUL_CONTINUE;
  1238. }
  1239. static int em_push(struct x86_emulate_ctxt *ctxt)
  1240. {
  1241. struct decode_cache *c = &ctxt->decode;
  1242. struct segmented_address addr;
  1243. register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
  1244. addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
  1245. addr.seg = VCPU_SREG_SS;
  1246. /* Disable writeback. */
  1247. c->dst.type = OP_NONE;
  1248. return segmented_write(ctxt, addr, &c->src.val, c->op_bytes);
  1249. }
  1250. static int emulate_pop(struct x86_emulate_ctxt *ctxt,
  1251. void *dest, int len)
  1252. {
  1253. struct decode_cache *c = &ctxt->decode;
  1254. int rc;
  1255. struct segmented_address addr;
  1256. addr.ea = register_address(c, c->regs[VCPU_REGS_RSP]);
  1257. addr.seg = VCPU_SREG_SS;
  1258. rc = segmented_read(ctxt, addr, dest, len);
  1259. if (rc != X86EMUL_CONTINUE)
  1260. return rc;
  1261. register_address_increment(c, &c->regs[VCPU_REGS_RSP], len);
  1262. return rc;
  1263. }
  1264. static int em_pop(struct x86_emulate_ctxt *ctxt)
  1265. {
  1266. struct decode_cache *c = &ctxt->decode;
  1267. return emulate_pop(ctxt, &c->dst.val, c->op_bytes);
  1268. }
  1269. static int emulate_popf(struct x86_emulate_ctxt *ctxt,
  1270. void *dest, int len)
  1271. {
  1272. int rc;
  1273. unsigned long val, change_mask;
  1274. int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
  1275. int cpl = ctxt->ops->cpl(ctxt);
  1276. rc = emulate_pop(ctxt, &val, len);
  1277. if (rc != X86EMUL_CONTINUE)
  1278. return rc;
  1279. change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF
  1280. | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID;
  1281. switch(ctxt->mode) {
  1282. case X86EMUL_MODE_PROT64:
  1283. case X86EMUL_MODE_PROT32:
  1284. case X86EMUL_MODE_PROT16:
  1285. if (cpl == 0)
  1286. change_mask |= EFLG_IOPL;
  1287. if (cpl <= iopl)
  1288. change_mask |= EFLG_IF;
  1289. break;
  1290. case X86EMUL_MODE_VM86:
  1291. if (iopl < 3)
  1292. return emulate_gp(ctxt, 0);
  1293. change_mask |= EFLG_IF;
  1294. break;
  1295. default: /* real mode */
  1296. change_mask |= (EFLG_IOPL | EFLG_IF);
  1297. break;
  1298. }
  1299. *(unsigned long *)dest =
  1300. (ctxt->eflags & ~change_mask) | (val & change_mask);
  1301. return rc;
  1302. }
  1303. static int em_popf(struct x86_emulate_ctxt *ctxt)
  1304. {
  1305. struct decode_cache *c = &ctxt->decode;
  1306. c->dst.type = OP_REG;
  1307. c->dst.addr.reg = &ctxt->eflags;
  1308. c->dst.bytes = c->op_bytes;
  1309. return emulate_popf(ctxt, &c->dst.val, c->op_bytes);
  1310. }
  1311. static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg)
  1312. {
  1313. struct decode_cache *c = &ctxt->decode;
  1314. c->src.val = get_segment_selector(ctxt, seg);
  1315. return em_push(ctxt);
  1316. }
  1317. static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, int seg)
  1318. {
  1319. struct decode_cache *c = &ctxt->decode;
  1320. unsigned long selector;
  1321. int rc;
  1322. rc = emulate_pop(ctxt, &selector, c->op_bytes);
  1323. if (rc != X86EMUL_CONTINUE)
  1324. return rc;
  1325. rc = load_segment_descriptor(ctxt, (u16)selector, seg);
  1326. return rc;
  1327. }
  1328. static int em_pusha(struct x86_emulate_ctxt *ctxt)
  1329. {
  1330. struct decode_cache *c = &ctxt->decode;
  1331. unsigned long old_esp = c->regs[VCPU_REGS_RSP];
  1332. int rc = X86EMUL_CONTINUE;
  1333. int reg = VCPU_REGS_RAX;
  1334. while (reg <= VCPU_REGS_RDI) {
  1335. (reg == VCPU_REGS_RSP) ?
  1336. (c->src.val = old_esp) : (c->src.val = c->regs[reg]);
  1337. rc = em_push(ctxt);
  1338. if (rc != X86EMUL_CONTINUE)
  1339. return rc;
  1340. ++reg;
  1341. }
  1342. return rc;
  1343. }
  1344. static int em_pushf(struct x86_emulate_ctxt *ctxt)
  1345. {
  1346. struct decode_cache *c = &ctxt->decode;
  1347. c->src.val = (unsigned long)ctxt->eflags;
  1348. return em_push(ctxt);
  1349. }
  1350. static int em_popa(struct x86_emulate_ctxt *ctxt)
  1351. {
  1352. struct decode_cache *c = &ctxt->decode;
  1353. int rc = X86EMUL_CONTINUE;
  1354. int reg = VCPU_REGS_RDI;
  1355. while (reg >= VCPU_REGS_RAX) {
  1356. if (reg == VCPU_REGS_RSP) {
  1357. register_address_increment(c, &c->regs[VCPU_REGS_RSP],
  1358. c->op_bytes);
  1359. --reg;
  1360. }
  1361. rc = emulate_pop(ctxt, &c->regs[reg], c->op_bytes);
  1362. if (rc != X86EMUL_CONTINUE)
  1363. break;
  1364. --reg;
  1365. }
  1366. return rc;
  1367. }
  1368. int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
  1369. {
  1370. struct decode_cache *c = &ctxt->decode;
  1371. struct x86_emulate_ops *ops = ctxt->ops;
  1372. int rc;
  1373. struct desc_ptr dt;
  1374. gva_t cs_addr;
  1375. gva_t eip_addr;
  1376. u16 cs, eip;
  1377. /* TODO: Add limit checks */
  1378. c->src.val = ctxt->eflags;
  1379. rc = em_push(ctxt);
  1380. if (rc != X86EMUL_CONTINUE)
  1381. return rc;
  1382. ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC);
  1383. c->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
  1384. rc = em_push(ctxt);
  1385. if (rc != X86EMUL_CONTINUE)
  1386. return rc;
  1387. c->src.val = c->eip;
  1388. rc = em_push(ctxt);
  1389. if (rc != X86EMUL_CONTINUE)
  1390. return rc;
  1391. ops->get_idt(ctxt, &dt);
  1392. eip_addr = dt.address + (irq << 2);
  1393. cs_addr = dt.address + (irq << 2) + 2;
  1394. rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception);
  1395. if (rc != X86EMUL_CONTINUE)
  1396. return rc;
  1397. rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception);
  1398. if (rc != X86EMUL_CONTINUE)
  1399. return rc;
  1400. rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
  1401. if (rc != X86EMUL_CONTINUE)
  1402. return rc;
  1403. c->eip = eip;
  1404. return rc;
  1405. }
  1406. static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
  1407. {
  1408. switch(ctxt->mode) {
  1409. case X86EMUL_MODE_REAL:
  1410. return emulate_int_real(ctxt, irq);
  1411. case X86EMUL_MODE_VM86:
  1412. case X86EMUL_MODE_PROT16:
  1413. case X86EMUL_MODE_PROT32:
  1414. case X86EMUL_MODE_PROT64:
  1415. default:
  1416. /* Protected mode interrupts unimplemented yet */
  1417. return X86EMUL_UNHANDLEABLE;
  1418. }
  1419. }
  1420. static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
  1421. {
  1422. struct decode_cache *c = &ctxt->decode;
  1423. int rc = X86EMUL_CONTINUE;
  1424. unsigned long temp_eip = 0;
  1425. unsigned long temp_eflags = 0;
  1426. unsigned long cs = 0;
  1427. unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF |
  1428. EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF |
  1429. EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */
  1430. unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP;
  1431. /* TODO: Add stack limit check */
  1432. rc = emulate_pop(ctxt, &temp_eip, c->op_bytes);
  1433. if (rc != X86EMUL_CONTINUE)
  1434. return rc;
  1435. if (temp_eip & ~0xffff)
  1436. return emulate_gp(ctxt, 0);
  1437. rc = emulate_pop(ctxt, &cs, c->op_bytes);
  1438. if (rc != X86EMUL_CONTINUE)
  1439. return rc;
  1440. rc = emulate_pop(ctxt, &temp_eflags, c->op_bytes);
  1441. if (rc != X86EMUL_CONTINUE)
  1442. return rc;
  1443. rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
  1444. if (rc != X86EMUL_CONTINUE)
  1445. return rc;
  1446. c->eip = temp_eip;
  1447. if (c->op_bytes == 4)
  1448. ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
  1449. else if (c->op_bytes == 2) {
  1450. ctxt->eflags &= ~0xffff;
  1451. ctxt->eflags |= temp_eflags;
  1452. }
  1453. ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
  1454. ctxt->eflags |= EFLG_RESERVED_ONE_MASK;
  1455. return rc;
  1456. }
  1457. static int em_iret(struct x86_emulate_ctxt *ctxt)
  1458. {
  1459. switch(ctxt->mode) {
  1460. case X86EMUL_MODE_REAL:
  1461. return emulate_iret_real(ctxt);
  1462. case X86EMUL_MODE_VM86:
  1463. case X86EMUL_MODE_PROT16:
  1464. case X86EMUL_MODE_PROT32:
  1465. case X86EMUL_MODE_PROT64:
  1466. default:
  1467. /* iret from protected mode unimplemented yet */
  1468. return X86EMUL_UNHANDLEABLE;
  1469. }
  1470. }
  1471. static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
  1472. {
  1473. struct decode_cache *c = &ctxt->decode;
  1474. int rc;
  1475. unsigned short sel;
  1476. memcpy(&sel, c->src.valptr + c->op_bytes, 2);
  1477. rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS);
  1478. if (rc != X86EMUL_CONTINUE)
  1479. return rc;
  1480. c->eip = 0;
  1481. memcpy(&c->eip, c->src.valptr, c->op_bytes);
  1482. return X86EMUL_CONTINUE;
  1483. }
  1484. static int em_grp1a(struct x86_emulate_ctxt *ctxt)
  1485. {
  1486. struct decode_cache *c = &ctxt->decode;
  1487. return emulate_pop(ctxt, &c->dst.val, c->dst.bytes);
  1488. }
  1489. static int em_grp2(struct x86_emulate_ctxt *ctxt)
  1490. {
  1491. struct decode_cache *c = &ctxt->decode;
  1492. switch (c->modrm_reg) {
  1493. case 0: /* rol */
  1494. emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags);
  1495. break;
  1496. case 1: /* ror */
  1497. emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags);
  1498. break;
  1499. case 2: /* rcl */
  1500. emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags);
  1501. break;
  1502. case 3: /* rcr */
  1503. emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags);
  1504. break;
  1505. case 4: /* sal/shl */
  1506. case 6: /* sal/shl */
  1507. emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags);
  1508. break;
  1509. case 5: /* shr */
  1510. emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags);
  1511. break;
  1512. case 7: /* sar */
  1513. emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags);
  1514. break;
  1515. }
  1516. return X86EMUL_CONTINUE;
  1517. }
  1518. static int em_grp3(struct x86_emulate_ctxt *ctxt)
  1519. {
  1520. struct decode_cache *c = &ctxt->decode;
  1521. unsigned long *rax = &c->regs[VCPU_REGS_RAX];
  1522. unsigned long *rdx = &c->regs[VCPU_REGS_RDX];
  1523. u8 de = 0;
  1524. switch (c->modrm_reg) {
  1525. case 0 ... 1: /* test */
  1526. emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
  1527. break;
  1528. case 2: /* not */
  1529. c->dst.val = ~c->dst.val;
  1530. break;
  1531. case 3: /* neg */
  1532. emulate_1op("neg", c->dst, ctxt->eflags);
  1533. break;
  1534. case 4: /* mul */
  1535. emulate_1op_rax_rdx("mul", c->src, *rax, *rdx, ctxt->eflags);
  1536. break;
  1537. case 5: /* imul */
  1538. emulate_1op_rax_rdx("imul", c->src, *rax, *rdx, ctxt->eflags);
  1539. break;
  1540. case 6: /* div */
  1541. emulate_1op_rax_rdx_ex("div", c->src, *rax, *rdx,
  1542. ctxt->eflags, de);
  1543. break;
  1544. case 7: /* idiv */
  1545. emulate_1op_rax_rdx_ex("idiv", c->src, *rax, *rdx,
  1546. ctxt->eflags, de);
  1547. break;
  1548. default:
  1549. return X86EMUL_UNHANDLEABLE;
  1550. }
  1551. if (de)
  1552. return emulate_de(ctxt);
  1553. return X86EMUL_CONTINUE;
  1554. }
  1555. static int em_grp45(struct x86_emulate_ctxt *ctxt)
  1556. {
  1557. struct decode_cache *c = &ctxt->decode;
  1558. int rc = X86EMUL_CONTINUE;
  1559. switch (c->modrm_reg) {
  1560. case 0: /* inc */
  1561. emulate_1op("inc", c->dst, ctxt->eflags);
  1562. break;
  1563. case 1: /* dec */
  1564. emulate_1op("dec", c->dst, ctxt->eflags);
  1565. break;
  1566. case 2: /* call near abs */ {
  1567. long int old_eip;
  1568. old_eip = c->eip;
  1569. c->eip = c->src.val;
  1570. c->src.val = old_eip;
  1571. rc = em_push(ctxt);
  1572. break;
  1573. }
  1574. case 4: /* jmp abs */
  1575. c->eip = c->src.val;
  1576. break;
  1577. case 5: /* jmp far */
  1578. rc = em_jmp_far(ctxt);
  1579. break;
  1580. case 6: /* push */
  1581. rc = em_push(ctxt);
  1582. break;
  1583. }
  1584. return rc;
  1585. }
  1586. static int em_grp9(struct x86_emulate_ctxt *ctxt)
  1587. {
  1588. struct decode_cache *c = &ctxt->decode;
  1589. u64 old = c->dst.orig_val64;
  1590. if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) ||
  1591. ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) {
  1592. c->regs[VCPU_REGS_RAX] = (u32) (old >> 0);
  1593. c->regs[VCPU_REGS_RDX] = (u32) (old >> 32);
  1594. ctxt->eflags &= ~EFLG_ZF;
  1595. } else {
  1596. c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) |
  1597. (u32) c->regs[VCPU_REGS_RBX];
  1598. ctxt->eflags |= EFLG_ZF;
  1599. }
  1600. return X86EMUL_CONTINUE;
  1601. }
  1602. static int em_ret(struct x86_emulate_ctxt *ctxt)
  1603. {
  1604. struct decode_cache *c = &ctxt->decode;
  1605. c->dst.type = OP_REG;
  1606. c->dst.addr.reg = &c->eip;
  1607. c->dst.bytes = c->op_bytes;
  1608. return em_pop(ctxt);
  1609. }
  1610. static int em_ret_far(struct x86_emulate_ctxt *ctxt)
  1611. {
  1612. struct decode_cache *c = &ctxt->decode;
  1613. int rc;
  1614. unsigned long cs;
  1615. rc = emulate_pop(ctxt, &c->eip, c->op_bytes);
  1616. if (rc != X86EMUL_CONTINUE)
  1617. return rc;
  1618. if (c->op_bytes == 4)
  1619. c->eip = (u32)c->eip;
  1620. rc = emulate_pop(ctxt, &cs, c->op_bytes);
  1621. if (rc != X86EMUL_CONTINUE)
  1622. return rc;
  1623. rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
  1624. return rc;
  1625. }
  1626. static int emulate_load_segment(struct x86_emulate_ctxt *ctxt, int seg)
  1627. {
  1628. struct decode_cache *c = &ctxt->decode;
  1629. unsigned short sel;
  1630. int rc;
  1631. memcpy(&sel, c->src.valptr + c->op_bytes, 2);
  1632. rc = load_segment_descriptor(ctxt, sel, seg);
  1633. if (rc != X86EMUL_CONTINUE)
  1634. return rc;
  1635. c->dst.val = c->src.val;
  1636. return rc;
  1637. }
  1638. static void
  1639. setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
  1640. struct desc_struct *cs, struct desc_struct *ss)
  1641. {
  1642. u16 selector;
  1643. memset(cs, 0, sizeof(struct desc_struct));
  1644. ctxt->ops->get_segment(ctxt, &selector, cs, NULL, VCPU_SREG_CS);
  1645. memset(ss, 0, sizeof(struct desc_struct));
  1646. cs->l = 0; /* will be adjusted later */
  1647. set_desc_base(cs, 0); /* flat segment */
  1648. cs->g = 1; /* 4kb granularity */
  1649. set_desc_limit(cs, 0xfffff); /* 4GB limit */
  1650. cs->type = 0x0b; /* Read, Execute, Accessed */
  1651. cs->s = 1;
  1652. cs->dpl = 0; /* will be adjusted later */
  1653. cs->p = 1;
  1654. cs->d = 1;
  1655. set_desc_base(ss, 0); /* flat segment */
  1656. set_desc_limit(ss, 0xfffff); /* 4GB limit */
  1657. ss->g = 1; /* 4kb granularity */
  1658. ss->s = 1;
  1659. ss->type = 0x03; /* Read/Write, Accessed */
  1660. ss->d = 1; /* 32bit stack segment */
  1661. ss->dpl = 0;
  1662. ss->p = 1;
  1663. }
  1664. static int em_syscall(struct x86_emulate_ctxt *ctxt)
  1665. {
  1666. struct decode_cache *c = &ctxt->decode;
  1667. struct x86_emulate_ops *ops = ctxt->ops;
  1668. struct desc_struct cs, ss;
  1669. u64 msr_data;
  1670. u16 cs_sel, ss_sel;
  1671. u64 efer = 0;
  1672. /* syscall is not available in real mode */
  1673. if (ctxt->mode == X86EMUL_MODE_REAL ||
  1674. ctxt->mode == X86EMUL_MODE_VM86)
  1675. return emulate_ud(ctxt);
  1676. ops->get_msr(ctxt, MSR_EFER, &efer);
  1677. setup_syscalls_segments(ctxt, &cs, &ss);
  1678. ops->get_msr(ctxt, MSR_STAR, &msr_data);
  1679. msr_data >>= 32;
  1680. cs_sel = (u16)(msr_data & 0xfffc);
  1681. ss_sel = (u16)(msr_data + 8);
  1682. if (efer & EFER_LMA) {
  1683. cs.d = 0;
  1684. cs.l = 1;
  1685. }
  1686. ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
  1687. ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
  1688. c->regs[VCPU_REGS_RCX] = c->eip;
  1689. if (efer & EFER_LMA) {
  1690. #ifdef CONFIG_X86_64
  1691. c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
  1692. ops->get_msr(ctxt,
  1693. ctxt->mode == X86EMUL_MODE_PROT64 ?
  1694. MSR_LSTAR : MSR_CSTAR, &msr_data);
  1695. c->eip = msr_data;
  1696. ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
  1697. ctxt->eflags &= ~(msr_data | EFLG_RF);
  1698. #endif
  1699. } else {
  1700. /* legacy mode */
  1701. ops->get_msr(ctxt, MSR_STAR, &msr_data);
  1702. c->eip = (u32)msr_data;
  1703. ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
  1704. }
  1705. return X86EMUL_CONTINUE;
  1706. }
  1707. static int em_sysenter(struct x86_emulate_ctxt *ctxt)
  1708. {
  1709. struct decode_cache *c = &ctxt->decode;
  1710. struct x86_emulate_ops *ops = ctxt->ops;
  1711. struct desc_struct cs, ss;
  1712. u64 msr_data;
  1713. u16 cs_sel, ss_sel;
  1714. u64 efer = 0;
  1715. ops->get_msr(ctxt, MSR_EFER, &efer);
  1716. /* inject #GP if in real mode */
  1717. if (ctxt->mode == X86EMUL_MODE_REAL)
  1718. return emulate_gp(ctxt, 0);
  1719. /* XXX sysenter/sysexit have not been tested in 64bit mode.
  1720. * Therefore, we inject an #UD.
  1721. */
  1722. if (ctxt->mode == X86EMUL_MODE_PROT64)
  1723. return emulate_ud(ctxt);
  1724. setup_syscalls_segments(ctxt, &cs, &ss);
  1725. ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
  1726. switch (ctxt->mode) {
  1727. case X86EMUL_MODE_PROT32:
  1728. if ((msr_data & 0xfffc) == 0x0)
  1729. return emulate_gp(ctxt, 0);
  1730. break;
  1731. case X86EMUL_MODE_PROT64:
  1732. if (msr_data == 0x0)
  1733. return emulate_gp(ctxt, 0);
  1734. break;
  1735. }
  1736. ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF);
  1737. cs_sel = (u16)msr_data;
  1738. cs_sel &= ~SELECTOR_RPL_MASK;
  1739. ss_sel = cs_sel + 8;
  1740. ss_sel &= ~SELECTOR_RPL_MASK;
  1741. if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
  1742. cs.d = 0;
  1743. cs.l = 1;
  1744. }
  1745. ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
  1746. ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
  1747. ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
  1748. c->eip = msr_data;
  1749. ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
  1750. c->regs[VCPU_REGS_RSP] = msr_data;
  1751. return X86EMUL_CONTINUE;
  1752. }
  1753. static int em_sysexit(struct x86_emulate_ctxt *ctxt)
  1754. {
  1755. struct decode_cache *c = &ctxt->decode;
  1756. struct x86_emulate_ops *ops = ctxt->ops;
  1757. struct desc_struct cs, ss;
  1758. u64 msr_data;
  1759. int usermode;
  1760. u16 cs_sel = 0, ss_sel = 0;
  1761. /* inject #GP if in real mode or Virtual 8086 mode */
  1762. if (ctxt->mode == X86EMUL_MODE_REAL ||
  1763. ctxt->mode == X86EMUL_MODE_VM86)
  1764. return emulate_gp(ctxt, 0);
  1765. setup_syscalls_segments(ctxt, &cs, &ss);
  1766. if ((c->rex_prefix & 0x8) != 0x0)
  1767. usermode = X86EMUL_MODE_PROT64;
  1768. else
  1769. usermode = X86EMUL_MODE_PROT32;
  1770. cs.dpl = 3;
  1771. ss.dpl = 3;
  1772. ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
  1773. switch (usermode) {
  1774. case X86EMUL_MODE_PROT32:
  1775. cs_sel = (u16)(msr_data + 16);
  1776. if ((msr_data & 0xfffc) == 0x0)
  1777. return emulate_gp(ctxt, 0);
  1778. ss_sel = (u16)(msr_data + 24);
  1779. break;
  1780. case X86EMUL_MODE_PROT64:
  1781. cs_sel = (u16)(msr_data + 32);
  1782. if (msr_data == 0x0)
  1783. return emulate_gp(ctxt, 0);
  1784. ss_sel = cs_sel + 8;
  1785. cs.d = 0;
  1786. cs.l = 1;
  1787. break;
  1788. }
  1789. cs_sel |= SELECTOR_RPL_MASK;
  1790. ss_sel |= SELECTOR_RPL_MASK;
  1791. ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
  1792. ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
  1793. c->eip = c->regs[VCPU_REGS_RDX];
  1794. c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX];
  1795. return X86EMUL_CONTINUE;
  1796. }
  1797. static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
  1798. {
  1799. int iopl;
  1800. if (ctxt->mode == X86EMUL_MODE_REAL)
  1801. return false;
  1802. if (ctxt->mode == X86EMUL_MODE_VM86)
  1803. return true;
  1804. iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
  1805. return ctxt->ops->cpl(ctxt) > iopl;
  1806. }
  1807. static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
  1808. u16 port, u16 len)
  1809. {
  1810. struct x86_emulate_ops *ops = ctxt->ops;
  1811. struct desc_struct tr_seg;
  1812. u32 base3;
  1813. int r;
  1814. u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
  1815. unsigned mask = (1 << len) - 1;
  1816. unsigned long base;
  1817. ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
  1818. if (!tr_seg.p)
  1819. return false;
  1820. if (desc_limit_scaled(&tr_seg) < 103)
  1821. return false;
  1822. base = get_desc_base(&tr_seg);
  1823. #ifdef CONFIG_X86_64
  1824. base |= ((u64)base3) << 32;
  1825. #endif
  1826. r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL);
  1827. if (r != X86EMUL_CONTINUE)
  1828. return false;
  1829. if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
  1830. return false;
  1831. r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL);
  1832. if (r != X86EMUL_CONTINUE)
  1833. return false;
  1834. if ((perm >> bit_idx) & mask)
  1835. return false;
  1836. return true;
  1837. }
  1838. static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
  1839. u16 port, u16 len)
  1840. {
  1841. if (ctxt->perm_ok)
  1842. return true;
  1843. if (emulator_bad_iopl(ctxt))
  1844. if (!emulator_io_port_access_allowed(ctxt, port, len))
  1845. return false;
  1846. ctxt->perm_ok = true;
  1847. return true;
  1848. }
  1849. static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
  1850. struct tss_segment_16 *tss)
  1851. {
  1852. struct decode_cache *c = &ctxt->decode;
  1853. tss->ip = c->eip;
  1854. tss->flag = ctxt->eflags;
  1855. tss->ax = c->regs[VCPU_REGS_RAX];
  1856. tss->cx = c->regs[VCPU_REGS_RCX];
  1857. tss->dx = c->regs[VCPU_REGS_RDX];
  1858. tss->bx = c->regs[VCPU_REGS_RBX];
  1859. tss->sp = c->regs[VCPU_REGS_RSP];
  1860. tss->bp = c->regs[VCPU_REGS_RBP];
  1861. tss->si = c->regs[VCPU_REGS_RSI];
  1862. tss->di = c->regs[VCPU_REGS_RDI];
  1863. tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
  1864. tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
  1865. tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
  1866. tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
  1867. tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
  1868. }
  1869. static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
  1870. struct tss_segment_16 *tss)
  1871. {
  1872. struct decode_cache *c = &ctxt->decode;
  1873. int ret;
  1874. c->eip = tss->ip;
  1875. ctxt->eflags = tss->flag | 2;
  1876. c->regs[VCPU_REGS_RAX] = tss->ax;
  1877. c->regs[VCPU_REGS_RCX] = tss->cx;
  1878. c->regs[VCPU_REGS_RDX] = tss->dx;
  1879. c->regs[VCPU_REGS_RBX] = tss->bx;
  1880. c->regs[VCPU_REGS_RSP] = tss->sp;
  1881. c->regs[VCPU_REGS_RBP] = tss->bp;
  1882. c->regs[VCPU_REGS_RSI] = tss->si;
  1883. c->regs[VCPU_REGS_RDI] = tss->di;
  1884. /*
  1885. * SDM says that segment selectors are loaded before segment
  1886. * descriptors
  1887. */
  1888. set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
  1889. set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
  1890. set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
  1891. set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
  1892. set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
  1893. /*
  1894. * Now load segment descriptors. If fault happenes at this stage
  1895. * it is handled in a context of new task
  1896. */
  1897. ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR);
  1898. if (ret != X86EMUL_CONTINUE)
  1899. return ret;
  1900. ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
  1901. if (ret != X86EMUL_CONTINUE)
  1902. return ret;
  1903. ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
  1904. if (ret != X86EMUL_CONTINUE)
  1905. return ret;
  1906. ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
  1907. if (ret != X86EMUL_CONTINUE)
  1908. return ret;
  1909. ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
  1910. if (ret != X86EMUL_CONTINUE)
  1911. return ret;
  1912. return X86EMUL_CONTINUE;
  1913. }
  1914. static int task_switch_16(struct x86_emulate_ctxt *ctxt,
  1915. u16 tss_selector, u16 old_tss_sel,
  1916. ulong old_tss_base, struct desc_struct *new_desc)
  1917. {
  1918. struct x86_emulate_ops *ops = ctxt->ops;
  1919. struct tss_segment_16 tss_seg;
  1920. int ret;
  1921. u32 new_tss_base = get_desc_base(new_desc);
  1922. ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
  1923. &ctxt->exception);
  1924. if (ret != X86EMUL_CONTINUE)
  1925. /* FIXME: need to provide precise fault address */
  1926. return ret;
  1927. save_state_to_tss16(ctxt, &tss_seg);
  1928. ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
  1929. &ctxt->exception);
  1930. if (ret != X86EMUL_CONTINUE)
  1931. /* FIXME: need to provide precise fault address */
  1932. return ret;
  1933. ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
  1934. &ctxt->exception);
  1935. if (ret != X86EMUL_CONTINUE)
  1936. /* FIXME: need to provide precise fault address */
  1937. return ret;
  1938. if (old_tss_sel != 0xffff) {
  1939. tss_seg.prev_task_link = old_tss_sel;
  1940. ret = ops->write_std(ctxt, new_tss_base,
  1941. &tss_seg.prev_task_link,
  1942. sizeof tss_seg.prev_task_link,
  1943. &ctxt->exception);
  1944. if (ret != X86EMUL_CONTINUE)
  1945. /* FIXME: need to provide precise fault address */
  1946. return ret;
  1947. }
  1948. return load_state_from_tss16(ctxt, &tss_seg);
  1949. }
  1950. static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
  1951. struct tss_segment_32 *tss)
  1952. {
  1953. struct decode_cache *c = &ctxt->decode;
  1954. tss->cr3 = ctxt->ops->get_cr(ctxt, 3);
  1955. tss->eip = c->eip;
  1956. tss->eflags = ctxt->eflags;
  1957. tss->eax = c->regs[VCPU_REGS_RAX];
  1958. tss->ecx = c->regs[VCPU_REGS_RCX];
  1959. tss->edx = c->regs[VCPU_REGS_RDX];
  1960. tss->ebx = c->regs[VCPU_REGS_RBX];
  1961. tss->esp = c->regs[VCPU_REGS_RSP];
  1962. tss->ebp = c->regs[VCPU_REGS_RBP];
  1963. tss->esi = c->regs[VCPU_REGS_RSI];
  1964. tss->edi = c->regs[VCPU_REGS_RDI];
  1965. tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
  1966. tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
  1967. tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
  1968. tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
  1969. tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
  1970. tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
  1971. tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR);
  1972. }
  1973. static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
  1974. struct tss_segment_32 *tss)
  1975. {
  1976. struct decode_cache *c = &ctxt->decode;
  1977. int ret;
  1978. if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
  1979. return emulate_gp(ctxt, 0);
  1980. c->eip = tss->eip;
  1981. ctxt->eflags = tss->eflags | 2;
  1982. c->regs[VCPU_REGS_RAX] = tss->eax;
  1983. c->regs[VCPU_REGS_RCX] = tss->ecx;
  1984. c->regs[VCPU_REGS_RDX] = tss->edx;
  1985. c->regs[VCPU_REGS_RBX] = tss->ebx;
  1986. c->regs[VCPU_REGS_RSP] = tss->esp;
  1987. c->regs[VCPU_REGS_RBP] = tss->ebp;
  1988. c->regs[VCPU_REGS_RSI] = tss->esi;
  1989. c->regs[VCPU_REGS_RDI] = tss->edi;
  1990. /*
  1991. * SDM says that segment selectors are loaded before segment
  1992. * descriptors
  1993. */
  1994. set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
  1995. set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
  1996. set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
  1997. set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
  1998. set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
  1999. set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
  2000. set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
  2001. /*
  2002. * Now load segment descriptors. If fault happenes at this stage
  2003. * it is handled in a context of new task
  2004. */
  2005. ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
  2006. if (ret != X86EMUL_CONTINUE)
  2007. return ret;
  2008. ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES);
  2009. if (ret != X86EMUL_CONTINUE)
  2010. return ret;
  2011. ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS);
  2012. if (ret != X86EMUL_CONTINUE)
  2013. return ret;
  2014. ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS);
  2015. if (ret != X86EMUL_CONTINUE)
  2016. return ret;
  2017. ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS);
  2018. if (ret != X86EMUL_CONTINUE)
  2019. return ret;
  2020. ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS);
  2021. if (ret != X86EMUL_CONTINUE)
  2022. return ret;
  2023. ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS);
  2024. if (ret != X86EMUL_CONTINUE)
  2025. return ret;
  2026. return X86EMUL_CONTINUE;
  2027. }
  2028. static int task_switch_32(struct x86_emulate_ctxt *ctxt,
  2029. u16 tss_selector, u16 old_tss_sel,
  2030. ulong old_tss_base, struct desc_struct *new_desc)
  2031. {
  2032. struct x86_emulate_ops *ops = ctxt->ops;
  2033. struct tss_segment_32 tss_seg;
  2034. int ret;
  2035. u32 new_tss_base = get_desc_base(new_desc);
  2036. ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
  2037. &ctxt->exception);
  2038. if (ret != X86EMUL_CONTINUE)
  2039. /* FIXME: need to provide precise fault address */
  2040. return ret;
  2041. save_state_to_tss32(ctxt, &tss_seg);
  2042. ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg,
  2043. &ctxt->exception);
  2044. if (ret != X86EMUL_CONTINUE)
  2045. /* FIXME: need to provide precise fault address */
  2046. return ret;
  2047. ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg,
  2048. &ctxt->exception);
  2049. if (ret != X86EMUL_CONTINUE)
  2050. /* FIXME: need to provide precise fault address */
  2051. return ret;
  2052. if (old_tss_sel != 0xffff) {
  2053. tss_seg.prev_task_link = old_tss_sel;
  2054. ret = ops->write_std(ctxt, new_tss_base,
  2055. &tss_seg.prev_task_link,
  2056. sizeof tss_seg.prev_task_link,
  2057. &ctxt->exception);
  2058. if (ret != X86EMUL_CONTINUE)
  2059. /* FIXME: need to provide precise fault address */
  2060. return ret;
  2061. }
  2062. return load_state_from_tss32(ctxt, &tss_seg);
  2063. }
  2064. static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
  2065. u16 tss_selector, int reason,
  2066. bool has_error_code, u32 error_code)
  2067. {
  2068. struct x86_emulate_ops *ops = ctxt->ops;
  2069. struct desc_struct curr_tss_desc, next_tss_desc;
  2070. int ret;
  2071. u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
  2072. ulong old_tss_base =
  2073. ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
  2074. u32 desc_limit;
  2075. /* FIXME: old_tss_base == ~0 ? */
  2076. ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
  2077. if (ret != X86EMUL_CONTINUE)
  2078. return ret;
  2079. ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
  2080. if (ret != X86EMUL_CONTINUE)
  2081. return ret;
  2082. /* FIXME: check that next_tss_desc is tss */
  2083. if (reason != TASK_SWITCH_IRET) {
  2084. if ((tss_selector & 3) > next_tss_desc.dpl ||
  2085. ops->cpl(ctxt) > next_tss_desc.dpl)
  2086. return emulate_gp(ctxt, 0);
  2087. }
  2088. desc_limit = desc_limit_scaled(&next_tss_desc);
  2089. if (!next_tss_desc.p ||
  2090. ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
  2091. desc_limit < 0x2b)) {
  2092. emulate_ts(ctxt, tss_selector & 0xfffc);
  2093. return X86EMUL_PROPAGATE_FAULT;
  2094. }
  2095. if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
  2096. curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
  2097. write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
  2098. }
  2099. if (reason == TASK_SWITCH_IRET)
  2100. ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
  2101. /* set back link to prev task only if NT bit is set in eflags
  2102. note that old_tss_sel is not used afetr this point */
  2103. if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
  2104. old_tss_sel = 0xffff;
  2105. if (next_tss_desc.type & 8)
  2106. ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
  2107. old_tss_base, &next_tss_desc);
  2108. else
  2109. ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
  2110. old_tss_base, &next_tss_desc);
  2111. if (ret != X86EMUL_CONTINUE)
  2112. return ret;
  2113. if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
  2114. ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
  2115. if (reason != TASK_SWITCH_IRET) {
  2116. next_tss_desc.type |= (1 << 1); /* set busy flag */
  2117. write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
  2118. }
  2119. ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
  2120. ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
  2121. if (has_error_code) {
  2122. struct decode_cache *c = &ctxt->decode;
  2123. c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
  2124. c->lock_prefix = 0;
  2125. c->src.val = (unsigned long) error_code;
  2126. ret = em_push(ctxt);
  2127. }
  2128. return ret;
  2129. }
  2130. int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
  2131. u16 tss_selector, int reason,
  2132. bool has_error_code, u32 error_code)
  2133. {
  2134. struct decode_cache *c = &ctxt->decode;
  2135. int rc;
  2136. c->eip = ctxt->eip;
  2137. c->dst.type = OP_NONE;
  2138. rc = emulator_do_task_switch(ctxt, tss_selector, reason,
  2139. has_error_code, error_code);
  2140. if (rc == X86EMUL_CONTINUE)
  2141. ctxt->eip = c->eip;
  2142. return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
  2143. }
  2144. static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned seg,
  2145. int reg, struct operand *op)
  2146. {
  2147. struct decode_cache *c = &ctxt->decode;
  2148. int df = (ctxt->eflags & EFLG_DF) ? -1 : 1;
  2149. register_address_increment(c, &c->regs[reg], df * op->bytes);
  2150. op->addr.mem.ea = register_address(c, c->regs[reg]);
  2151. op->addr.mem.seg = seg;
  2152. }
  2153. static int em_das(struct x86_emulate_ctxt *ctxt)
  2154. {
  2155. struct decode_cache *c = &ctxt->decode;
  2156. u8 al, old_al;
  2157. bool af, cf, old_cf;
  2158. cf = ctxt->eflags & X86_EFLAGS_CF;
  2159. al = c->dst.val;
  2160. old_al = al;
  2161. old_cf = cf;
  2162. cf = false;
  2163. af = ctxt->eflags & X86_EFLAGS_AF;
  2164. if ((al & 0x0f) > 9 || af) {
  2165. al -= 6;
  2166. cf = old_cf | (al >= 250);
  2167. af = true;
  2168. } else {
  2169. af = false;
  2170. }
  2171. if (old_al > 0x99 || old_cf) {
  2172. al -= 0x60;
  2173. cf = true;
  2174. }
  2175. c->dst.val = al;
  2176. /* Set PF, ZF, SF */
  2177. c->src.type = OP_IMM;
  2178. c->src.val = 0;
  2179. c->src.bytes = 1;
  2180. emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
  2181. ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
  2182. if (cf)
  2183. ctxt->eflags |= X86_EFLAGS_CF;
  2184. if (af)
  2185. ctxt->eflags |= X86_EFLAGS_AF;
  2186. return X86EMUL_CONTINUE;
  2187. }
  2188. static int em_call_far(struct x86_emulate_ctxt *ctxt)
  2189. {
  2190. struct decode_cache *c = &ctxt->decode;
  2191. u16 sel, old_cs;
  2192. ulong old_eip;
  2193. int rc;
  2194. old_cs = get_segment_selector(ctxt, VCPU_SREG_CS);
  2195. old_eip = c->eip;
  2196. memcpy(&sel, c->src.valptr + c->op_bytes, 2);
  2197. if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS))
  2198. return X86EMUL_CONTINUE;
  2199. c->eip = 0;
  2200. memcpy(&c->eip, c->src.valptr, c->op_bytes);
  2201. c->src.val = old_cs;
  2202. rc = em_push(ctxt);
  2203. if (rc != X86EMUL_CONTINUE)
  2204. return rc;
  2205. c->src.val = old_eip;
  2206. return em_push(ctxt);
  2207. }
  2208. static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
  2209. {
  2210. struct decode_cache *c = &ctxt->decode;
  2211. int rc;
  2212. c->dst.type = OP_REG;
  2213. c->dst.addr.reg = &c->eip;
  2214. c->dst.bytes = c->op_bytes;
  2215. rc = emulate_pop(ctxt, &c->dst.val, c->op_bytes);
  2216. if (rc != X86EMUL_CONTINUE)
  2217. return rc;
  2218. register_address_increment(c, &c->regs[VCPU_REGS_RSP], c->src.val);
  2219. return X86EMUL_CONTINUE;
  2220. }
  2221. static int em_add(struct x86_emulate_ctxt *ctxt)
  2222. {
  2223. struct decode_cache *c = &ctxt->decode;
  2224. emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
  2225. return X86EMUL_CONTINUE;
  2226. }
  2227. static int em_or(struct x86_emulate_ctxt *ctxt)
  2228. {
  2229. struct decode_cache *c = &ctxt->decode;
  2230. emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags);
  2231. return X86EMUL_CONTINUE;
  2232. }
  2233. static int em_adc(struct x86_emulate_ctxt *ctxt)
  2234. {
  2235. struct decode_cache *c = &ctxt->decode;
  2236. emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags);
  2237. return X86EMUL_CONTINUE;
  2238. }
  2239. static int em_sbb(struct x86_emulate_ctxt *ctxt)
  2240. {
  2241. struct decode_cache *c = &ctxt->decode;
  2242. emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags);
  2243. return X86EMUL_CONTINUE;
  2244. }
  2245. static int em_and(struct x86_emulate_ctxt *ctxt)
  2246. {
  2247. struct decode_cache *c = &ctxt->decode;
  2248. emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags);
  2249. return X86EMUL_CONTINUE;
  2250. }
  2251. static int em_sub(struct x86_emulate_ctxt *ctxt)
  2252. {
  2253. struct decode_cache *c = &ctxt->decode;
  2254. emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags);
  2255. return X86EMUL_CONTINUE;
  2256. }
  2257. static int em_xor(struct x86_emulate_ctxt *ctxt)
  2258. {
  2259. struct decode_cache *c = &ctxt->decode;
  2260. emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags);
  2261. return X86EMUL_CONTINUE;
  2262. }
  2263. static int em_cmp(struct x86_emulate_ctxt *ctxt)
  2264. {
  2265. struct decode_cache *c = &ctxt->decode;
  2266. emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
  2267. /* Disable writeback. */
  2268. c->dst.type = OP_NONE;
  2269. return X86EMUL_CONTINUE;
  2270. }
  2271. static int em_test(struct x86_emulate_ctxt *ctxt)
  2272. {
  2273. struct decode_cache *c = &ctxt->decode;
  2274. emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags);
  2275. return X86EMUL_CONTINUE;
  2276. }
  2277. static int em_xchg(struct x86_emulate_ctxt *ctxt)
  2278. {
  2279. struct decode_cache *c = &ctxt->decode;
  2280. /* Write back the register source. */
  2281. c->src.val = c->dst.val;
  2282. write_register_operand(&c->src);
  2283. /* Write back the memory destination with implicit LOCK prefix. */
  2284. c->dst.val = c->src.orig_val;
  2285. c->lock_prefix = 1;
  2286. return X86EMUL_CONTINUE;
  2287. }
  2288. static int em_imul(struct x86_emulate_ctxt *ctxt)
  2289. {
  2290. struct decode_cache *c = &ctxt->decode;
  2291. emulate_2op_SrcV_nobyte("imul", c->src, c->dst, ctxt->eflags);
  2292. return X86EMUL_CONTINUE;
  2293. }
  2294. static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
  2295. {
  2296. struct decode_cache *c = &ctxt->decode;
  2297. c->dst.val = c->src2.val;
  2298. return em_imul(ctxt);
  2299. }
  2300. static int em_cwd(struct x86_emulate_ctxt *ctxt)
  2301. {
  2302. struct decode_cache *c = &ctxt->decode;
  2303. c->dst.type = OP_REG;
  2304. c->dst.bytes = c->src.bytes;
  2305. c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
  2306. c->dst.val = ~((c->src.val >> (c->src.bytes * 8 - 1)) - 1);
  2307. return X86EMUL_CONTINUE;
  2308. }
  2309. static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
  2310. {
  2311. struct decode_cache *c = &ctxt->decode;
  2312. u64 tsc = 0;
  2313. ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
  2314. c->regs[VCPU_REGS_RAX] = (u32)tsc;
  2315. c->regs[VCPU_REGS_RDX] = tsc >> 32;
  2316. return X86EMUL_CONTINUE;
  2317. }
  2318. static int em_mov(struct x86_emulate_ctxt *ctxt)
  2319. {
  2320. struct decode_cache *c = &ctxt->decode;
  2321. c->dst.val = c->src.val;
  2322. return X86EMUL_CONTINUE;
  2323. }
  2324. static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
  2325. {
  2326. struct decode_cache *c = &ctxt->decode;
  2327. if (c->modrm_reg > VCPU_SREG_GS)
  2328. return emulate_ud(ctxt);
  2329. c->dst.val = get_segment_selector(ctxt, c->modrm_reg);
  2330. return X86EMUL_CONTINUE;
  2331. }
  2332. static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
  2333. {
  2334. struct decode_cache *c = &ctxt->decode;
  2335. u16 sel = c->src.val;
  2336. if (c->modrm_reg == VCPU_SREG_CS || c->modrm_reg > VCPU_SREG_GS)
  2337. return emulate_ud(ctxt);
  2338. if (c->modrm_reg == VCPU_SREG_SS)
  2339. ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
  2340. /* Disable writeback. */
  2341. c->dst.type = OP_NONE;
  2342. return load_segment_descriptor(ctxt, sel, c->modrm_reg);
  2343. }
  2344. static int em_movdqu(struct x86_emulate_ctxt *ctxt)
  2345. {
  2346. struct decode_cache *c = &ctxt->decode;
  2347. memcpy(&c->dst.vec_val, &c->src.vec_val, c->op_bytes);
  2348. return X86EMUL_CONTINUE;
  2349. }
  2350. static int em_invlpg(struct x86_emulate_ctxt *ctxt)
  2351. {
  2352. struct decode_cache *c = &ctxt->decode;
  2353. int rc;
  2354. ulong linear;
  2355. rc = linearize(ctxt, c->src.addr.mem, 1, false, &linear);
  2356. if (rc == X86EMUL_CONTINUE)
  2357. ctxt->ops->invlpg(ctxt, linear);
  2358. /* Disable writeback. */
  2359. c->dst.type = OP_NONE;
  2360. return X86EMUL_CONTINUE;
  2361. }
  2362. static int em_clts(struct x86_emulate_ctxt *ctxt)
  2363. {
  2364. ulong cr0;
  2365. cr0 = ctxt->ops->get_cr(ctxt, 0);
  2366. cr0 &= ~X86_CR0_TS;
  2367. ctxt->ops->set_cr(ctxt, 0, cr0);
  2368. return X86EMUL_CONTINUE;
  2369. }
  2370. static int em_vmcall(struct x86_emulate_ctxt *ctxt)
  2371. {
  2372. struct decode_cache *c = &ctxt->decode;
  2373. int rc;
  2374. if (c->modrm_mod != 3 || c->modrm_rm != 1)
  2375. return X86EMUL_UNHANDLEABLE;
  2376. rc = ctxt->ops->fix_hypercall(ctxt);
  2377. if (rc != X86EMUL_CONTINUE)
  2378. return rc;
  2379. /* Let the processor re-execute the fixed hypercall */
  2380. c->eip = ctxt->eip;
  2381. /* Disable writeback. */
  2382. c->dst.type = OP_NONE;
  2383. return X86EMUL_CONTINUE;
  2384. }
  2385. static int em_lgdt(struct x86_emulate_ctxt *ctxt)
  2386. {
  2387. struct decode_cache *c = &ctxt->decode;
  2388. struct desc_ptr desc_ptr;
  2389. int rc;
  2390. rc = read_descriptor(ctxt, c->src.addr.mem,
  2391. &desc_ptr.size, &desc_ptr.address,
  2392. c->op_bytes);
  2393. if (rc != X86EMUL_CONTINUE)
  2394. return rc;
  2395. ctxt->ops->set_gdt(ctxt, &desc_ptr);
  2396. /* Disable writeback. */
  2397. c->dst.type = OP_NONE;
  2398. return X86EMUL_CONTINUE;
  2399. }
  2400. static int em_vmmcall(struct x86_emulate_ctxt *ctxt)
  2401. {
  2402. struct decode_cache *c = &ctxt->decode;
  2403. int rc;
  2404. rc = ctxt->ops->fix_hypercall(ctxt);
  2405. /* Disable writeback. */
  2406. c->dst.type = OP_NONE;
  2407. return rc;
  2408. }
  2409. static int em_lidt(struct x86_emulate_ctxt *ctxt)
  2410. {
  2411. struct decode_cache *c = &ctxt->decode;
  2412. struct desc_ptr desc_ptr;
  2413. int rc;
  2414. rc = read_descriptor(ctxt, c->src.addr.mem,
  2415. &desc_ptr.size, &desc_ptr.address,
  2416. c->op_bytes);
  2417. if (rc != X86EMUL_CONTINUE)
  2418. return rc;
  2419. ctxt->ops->set_idt(ctxt, &desc_ptr);
  2420. /* Disable writeback. */
  2421. c->dst.type = OP_NONE;
  2422. return X86EMUL_CONTINUE;
  2423. }
  2424. static int em_smsw(struct x86_emulate_ctxt *ctxt)
  2425. {
  2426. struct decode_cache *c = &ctxt->decode;
  2427. c->dst.bytes = 2;
  2428. c->dst.val = ctxt->ops->get_cr(ctxt, 0);
  2429. return X86EMUL_CONTINUE;
  2430. }
  2431. static int em_lmsw(struct x86_emulate_ctxt *ctxt)
  2432. {
  2433. struct decode_cache *c = &ctxt->decode;
  2434. ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
  2435. | (c->src.val & 0x0f));
  2436. c->dst.type = OP_NONE;
  2437. return X86EMUL_CONTINUE;
  2438. }
  2439. static int em_loop(struct x86_emulate_ctxt *ctxt)
  2440. {
  2441. struct decode_cache *c = &ctxt->decode;
  2442. register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
  2443. if ((address_mask(c, c->regs[VCPU_REGS_RCX]) != 0) &&
  2444. (c->b == 0xe2 || test_cc(c->b ^ 0x5, ctxt->eflags)))
  2445. jmp_rel(c, c->src.val);
  2446. return X86EMUL_CONTINUE;
  2447. }
  2448. static int em_jcxz(struct x86_emulate_ctxt *ctxt)
  2449. {
  2450. struct decode_cache *c = &ctxt->decode;
  2451. if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0)
  2452. jmp_rel(c, c->src.val);
  2453. return X86EMUL_CONTINUE;
  2454. }
  2455. static bool valid_cr(int nr)
  2456. {
  2457. switch (nr) {
  2458. case 0:
  2459. case 2 ... 4:
  2460. case 8:
  2461. return true;
  2462. default:
  2463. return false;
  2464. }
  2465. }
  2466. static int check_cr_read(struct x86_emulate_ctxt *ctxt)
  2467. {
  2468. struct decode_cache *c = &ctxt->decode;
  2469. if (!valid_cr(c->modrm_reg))
  2470. return emulate_ud(ctxt);
  2471. return X86EMUL_CONTINUE;
  2472. }
  2473. static int check_cr_write(struct x86_emulate_ctxt *ctxt)
  2474. {
  2475. struct decode_cache *c = &ctxt->decode;
  2476. u64 new_val = c->src.val64;
  2477. int cr = c->modrm_reg;
  2478. u64 efer = 0;
  2479. static u64 cr_reserved_bits[] = {
  2480. 0xffffffff00000000ULL,
  2481. 0, 0, 0, /* CR3 checked later */
  2482. CR4_RESERVED_BITS,
  2483. 0, 0, 0,
  2484. CR8_RESERVED_BITS,
  2485. };
  2486. if (!valid_cr(cr))
  2487. return emulate_ud(ctxt);
  2488. if (new_val & cr_reserved_bits[cr])
  2489. return emulate_gp(ctxt, 0);
  2490. switch (cr) {
  2491. case 0: {
  2492. u64 cr4;
  2493. if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
  2494. ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
  2495. return emulate_gp(ctxt, 0);
  2496. cr4 = ctxt->ops->get_cr(ctxt, 4);
  2497. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  2498. if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
  2499. !(cr4 & X86_CR4_PAE))
  2500. return emulate_gp(ctxt, 0);
  2501. break;
  2502. }
  2503. case 3: {
  2504. u64 rsvd = 0;
  2505. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  2506. if (efer & EFER_LMA)
  2507. rsvd = CR3_L_MODE_RESERVED_BITS;
  2508. else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
  2509. rsvd = CR3_PAE_RESERVED_BITS;
  2510. else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
  2511. rsvd = CR3_NONPAE_RESERVED_BITS;
  2512. if (new_val & rsvd)
  2513. return emulate_gp(ctxt, 0);
  2514. break;
  2515. }
  2516. case 4: {
  2517. u64 cr4;
  2518. cr4 = ctxt->ops->get_cr(ctxt, 4);
  2519. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  2520. if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
  2521. return emulate_gp(ctxt, 0);
  2522. break;
  2523. }
  2524. }
  2525. return X86EMUL_CONTINUE;
  2526. }
  2527. static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
  2528. {
  2529. unsigned long dr7;
  2530. ctxt->ops->get_dr(ctxt, 7, &dr7);
  2531. /* Check if DR7.Global_Enable is set */
  2532. return dr7 & (1 << 13);
  2533. }
  2534. static int check_dr_read(struct x86_emulate_ctxt *ctxt)
  2535. {
  2536. struct decode_cache *c = &ctxt->decode;
  2537. int dr = c->modrm_reg;
  2538. u64 cr4;
  2539. if (dr > 7)
  2540. return emulate_ud(ctxt);
  2541. cr4 = ctxt->ops->get_cr(ctxt, 4);
  2542. if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
  2543. return emulate_ud(ctxt);
  2544. if (check_dr7_gd(ctxt))
  2545. return emulate_db(ctxt);
  2546. return X86EMUL_CONTINUE;
  2547. }
  2548. static int check_dr_write(struct x86_emulate_ctxt *ctxt)
  2549. {
  2550. struct decode_cache *c = &ctxt->decode;
  2551. u64 new_val = c->src.val64;
  2552. int dr = c->modrm_reg;
  2553. if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
  2554. return emulate_gp(ctxt, 0);
  2555. return check_dr_read(ctxt);
  2556. }
  2557. static int check_svme(struct x86_emulate_ctxt *ctxt)
  2558. {
  2559. u64 efer;
  2560. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  2561. if (!(efer & EFER_SVME))
  2562. return emulate_ud(ctxt);
  2563. return X86EMUL_CONTINUE;
  2564. }
  2565. static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
  2566. {
  2567. u64 rax = ctxt->decode.regs[VCPU_REGS_RAX];
  2568. /* Valid physical address? */
  2569. if (rax & 0xffff000000000000ULL)
  2570. return emulate_gp(ctxt, 0);
  2571. return check_svme(ctxt);
  2572. }
  2573. static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
  2574. {
  2575. u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
  2576. if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
  2577. return emulate_ud(ctxt);
  2578. return X86EMUL_CONTINUE;
  2579. }
  2580. static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
  2581. {
  2582. u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
  2583. u64 rcx = ctxt->decode.regs[VCPU_REGS_RCX];
  2584. if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
  2585. (rcx > 3))
  2586. return emulate_gp(ctxt, 0);
  2587. return X86EMUL_CONTINUE;
  2588. }
  2589. static int check_perm_in(struct x86_emulate_ctxt *ctxt)
  2590. {
  2591. struct decode_cache *c = &ctxt->decode;
  2592. c->dst.bytes = min(c->dst.bytes, 4u);
  2593. if (!emulator_io_permited(ctxt, c->src.val, c->dst.bytes))
  2594. return emulate_gp(ctxt, 0);
  2595. return X86EMUL_CONTINUE;
  2596. }
  2597. static int check_perm_out(struct x86_emulate_ctxt *ctxt)
  2598. {
  2599. struct decode_cache *c = &ctxt->decode;
  2600. c->src.bytes = min(c->src.bytes, 4u);
  2601. if (!emulator_io_permited(ctxt, c->dst.val, c->src.bytes))
  2602. return emulate_gp(ctxt, 0);
  2603. return X86EMUL_CONTINUE;
  2604. }
  2605. #define D(_y) { .flags = (_y) }
  2606. #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i }
  2607. #define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \
  2608. .check_perm = (_p) }
  2609. #define N D(0)
  2610. #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
  2611. #define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
  2612. #define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
  2613. #define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
  2614. #define II(_f, _e, _i) \
  2615. { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
  2616. #define IIP(_f, _e, _i, _p) \
  2617. { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \
  2618. .check_perm = (_p) }
  2619. #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
  2620. #define D2bv(_f) D((_f) | ByteOp), D(_f)
  2621. #define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
  2622. #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
  2623. #define I6ALU(_f, _e) I2bv((_f) | DstMem | SrcReg | ModRM, _e), \
  2624. I2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
  2625. I2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
  2626. static struct opcode group7_rm1[] = {
  2627. DI(SrcNone | ModRM | Priv, monitor),
  2628. DI(SrcNone | ModRM | Priv, mwait),
  2629. N, N, N, N, N, N,
  2630. };
  2631. static struct opcode group7_rm3[] = {
  2632. DIP(SrcNone | ModRM | Prot | Priv, vmrun, check_svme_pa),
  2633. II(SrcNone | ModRM | Prot | VendorSpecific, em_vmmcall, vmmcall),
  2634. DIP(SrcNone | ModRM | Prot | Priv, vmload, check_svme_pa),
  2635. DIP(SrcNone | ModRM | Prot | Priv, vmsave, check_svme_pa),
  2636. DIP(SrcNone | ModRM | Prot | Priv, stgi, check_svme),
  2637. DIP(SrcNone | ModRM | Prot | Priv, clgi, check_svme),
  2638. DIP(SrcNone | ModRM | Prot | Priv, skinit, check_svme),
  2639. DIP(SrcNone | ModRM | Prot | Priv, invlpga, check_svme),
  2640. };
  2641. static struct opcode group7_rm7[] = {
  2642. N,
  2643. DIP(SrcNone | ModRM, rdtscp, check_rdtsc),
  2644. N, N, N, N, N, N,
  2645. };
  2646. static struct opcode group1[] = {
  2647. I(Lock, em_add),
  2648. I(Lock, em_or),
  2649. I(Lock, em_adc),
  2650. I(Lock, em_sbb),
  2651. I(Lock, em_and),
  2652. I(Lock, em_sub),
  2653. I(Lock, em_xor),
  2654. I(0, em_cmp),
  2655. };
  2656. static struct opcode group1A[] = {
  2657. D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N,
  2658. };
  2659. static struct opcode group3[] = {
  2660. D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM),
  2661. D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
  2662. X4(D(SrcMem | ModRM)),
  2663. };
  2664. static struct opcode group4[] = {
  2665. D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock),
  2666. N, N, N, N, N, N,
  2667. };
  2668. static struct opcode group5[] = {
  2669. D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock),
  2670. D(SrcMem | ModRM | Stack),
  2671. I(SrcMemFAddr | ModRM | ImplicitOps | Stack, em_call_far),
  2672. D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps),
  2673. D(SrcMem | ModRM | Stack), N,
  2674. };
  2675. static struct opcode group6[] = {
  2676. DI(ModRM | Prot, sldt),
  2677. DI(ModRM | Prot, str),
  2678. DI(ModRM | Prot | Priv, lldt),
  2679. DI(ModRM | Prot | Priv, ltr),
  2680. N, N, N, N,
  2681. };
  2682. static struct group_dual group7 = { {
  2683. DI(ModRM | Mov | DstMem | Priv, sgdt),
  2684. DI(ModRM | Mov | DstMem | Priv, sidt),
  2685. II(ModRM | SrcMem | Priv, em_lgdt, lgdt),
  2686. II(ModRM | SrcMem | Priv, em_lidt, lidt),
  2687. II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
  2688. II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw),
  2689. II(SrcMem | ModRM | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
  2690. }, {
  2691. I(SrcNone | ModRM | Priv | VendorSpecific, em_vmcall),
  2692. EXT(0, group7_rm1),
  2693. N, EXT(0, group7_rm3),
  2694. II(SrcNone | ModRM | DstMem | Mov, em_smsw, smsw), N,
  2695. II(SrcMem16 | ModRM | Mov | Priv, em_lmsw, lmsw), EXT(0, group7_rm7),
  2696. } };
  2697. static struct opcode group8[] = {
  2698. N, N, N, N,
  2699. D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock),
  2700. D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock),
  2701. };
  2702. static struct group_dual group9 = { {
  2703. N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N,
  2704. }, {
  2705. N, N, N, N, N, N, N, N,
  2706. } };
  2707. static struct opcode group11[] = {
  2708. I(DstMem | SrcImm | ModRM | Mov, em_mov), X7(D(Undefined)),
  2709. };
  2710. static struct gprefix pfx_0f_6f_0f_7f = {
  2711. N, N, N, I(Sse, em_movdqu),
  2712. };
  2713. static struct opcode opcode_table[256] = {
  2714. /* 0x00 - 0x07 */
  2715. I6ALU(Lock, em_add),
  2716. D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
  2717. /* 0x08 - 0x0F */
  2718. I6ALU(Lock, em_or),
  2719. D(ImplicitOps | Stack | No64), N,
  2720. /* 0x10 - 0x17 */
  2721. I6ALU(Lock, em_adc),
  2722. D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
  2723. /* 0x18 - 0x1F */
  2724. I6ALU(Lock, em_sbb),
  2725. D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
  2726. /* 0x20 - 0x27 */
  2727. I6ALU(Lock, em_and), N, N,
  2728. /* 0x28 - 0x2F */
  2729. I6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
  2730. /* 0x30 - 0x37 */
  2731. I6ALU(Lock, em_xor), N, N,
  2732. /* 0x38 - 0x3F */
  2733. I6ALU(0, em_cmp), N, N,
  2734. /* 0x40 - 0x4F */
  2735. X16(D(DstReg)),
  2736. /* 0x50 - 0x57 */
  2737. X8(I(SrcReg | Stack, em_push)),
  2738. /* 0x58 - 0x5F */
  2739. X8(I(DstReg | Stack, em_pop)),
  2740. /* 0x60 - 0x67 */
  2741. I(ImplicitOps | Stack | No64, em_pusha),
  2742. I(ImplicitOps | Stack | No64, em_popa),
  2743. N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
  2744. N, N, N, N,
  2745. /* 0x68 - 0x6F */
  2746. I(SrcImm | Mov | Stack, em_push),
  2747. I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
  2748. I(SrcImmByte | Mov | Stack, em_push),
  2749. I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
  2750. D2bvIP(DstDI | SrcDX | Mov | String, ins, check_perm_in), /* insb, insw/insd */
  2751. D2bvIP(SrcSI | DstDX | String, outs, check_perm_out), /* outsb, outsw/outsd */
  2752. /* 0x70 - 0x7F */
  2753. X16(D(SrcImmByte)),
  2754. /* 0x80 - 0x87 */
  2755. G(ByteOp | DstMem | SrcImm | ModRM | Group, group1),
  2756. G(DstMem | SrcImm | ModRM | Group, group1),
  2757. G(ByteOp | DstMem | SrcImm | ModRM | No64 | Group, group1),
  2758. G(DstMem | SrcImmByte | ModRM | Group, group1),
  2759. I2bv(DstMem | SrcReg | ModRM, em_test),
  2760. I2bv(DstMem | SrcReg | ModRM | Lock, em_xchg),
  2761. /* 0x88 - 0x8F */
  2762. I2bv(DstMem | SrcReg | ModRM | Mov, em_mov),
  2763. I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
  2764. I(DstMem | SrcNone | ModRM | Mov, em_mov_rm_sreg),
  2765. D(ModRM | SrcMem | NoAccess | DstReg),
  2766. I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
  2767. G(0, group1A),
  2768. /* 0x90 - 0x97 */
  2769. DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
  2770. /* 0x98 - 0x9F */
  2771. D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
  2772. I(SrcImmFAddr | No64, em_call_far), N,
  2773. II(ImplicitOps | Stack, em_pushf, pushf),
  2774. II(ImplicitOps | Stack, em_popf, popf), N, N,
  2775. /* 0xA0 - 0xA7 */
  2776. I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
  2777. I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
  2778. I2bv(SrcSI | DstDI | Mov | String, em_mov),
  2779. I2bv(SrcSI | DstDI | String, em_cmp),
  2780. /* 0xA8 - 0xAF */
  2781. I2bv(DstAcc | SrcImm, em_test),
  2782. I2bv(SrcAcc | DstDI | Mov | String, em_mov),
  2783. I2bv(SrcSI | DstAcc | Mov | String, em_mov),
  2784. I2bv(SrcAcc | DstDI | String, em_cmp),
  2785. /* 0xB0 - 0xB7 */
  2786. X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
  2787. /* 0xB8 - 0xBF */
  2788. X8(I(DstReg | SrcImm | Mov, em_mov)),
  2789. /* 0xC0 - 0xC7 */
  2790. D2bv(DstMem | SrcImmByte | ModRM),
  2791. I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm),
  2792. I(ImplicitOps | Stack, em_ret),
  2793. D(DstReg | SrcMemFAddr | ModRM | No64), D(DstReg | SrcMemFAddr | ModRM | No64),
  2794. G(ByteOp, group11), G(0, group11),
  2795. /* 0xC8 - 0xCF */
  2796. N, N, N, I(ImplicitOps | Stack, em_ret_far),
  2797. D(ImplicitOps), DI(SrcImmByte, intn),
  2798. D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
  2799. /* 0xD0 - 0xD7 */
  2800. D2bv(DstMem | SrcOne | ModRM), D2bv(DstMem | ModRM),
  2801. N, N, N, N,
  2802. /* 0xD8 - 0xDF */
  2803. N, N, N, N, N, N, N, N,
  2804. /* 0xE0 - 0xE7 */
  2805. X3(I(SrcImmByte, em_loop)),
  2806. I(SrcImmByte, em_jcxz),
  2807. D2bvIP(SrcImmUByte | DstAcc, in, check_perm_in),
  2808. D2bvIP(SrcAcc | DstImmUByte, out, check_perm_out),
  2809. /* 0xE8 - 0xEF */
  2810. D(SrcImm | Stack), D(SrcImm | ImplicitOps),
  2811. I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps),
  2812. D2bvIP(SrcDX | DstAcc, in, check_perm_in),
  2813. D2bvIP(SrcAcc | DstDX, out, check_perm_out),
  2814. /* 0xF0 - 0xF7 */
  2815. N, DI(ImplicitOps, icebp), N, N,
  2816. DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
  2817. G(ByteOp, group3), G(0, group3),
  2818. /* 0xF8 - 0xFF */
  2819. D(ImplicitOps), D(ImplicitOps), D(ImplicitOps), D(ImplicitOps),
  2820. D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
  2821. };
  2822. static struct opcode twobyte_table[256] = {
  2823. /* 0x00 - 0x0F */
  2824. G(0, group6), GD(0, &group7), N, N,
  2825. N, I(ImplicitOps | VendorSpecific, em_syscall),
  2826. II(ImplicitOps | Priv, em_clts, clts), N,
  2827. DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
  2828. N, D(ImplicitOps | ModRM), N, N,
  2829. /* 0x10 - 0x1F */
  2830. N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N,
  2831. /* 0x20 - 0x2F */
  2832. DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read),
  2833. DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read),
  2834. DIP(ModRM | SrcMem | Priv | Op3264, cr_write, check_cr_write),
  2835. DIP(ModRM | SrcMem | Priv | Op3264, dr_write, check_dr_write),
  2836. N, N, N, N,
  2837. N, N, N, N, N, N, N, N,
  2838. /* 0x30 - 0x3F */
  2839. DI(ImplicitOps | Priv, wrmsr),
  2840. IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
  2841. DI(ImplicitOps | Priv, rdmsr),
  2842. DIP(ImplicitOps | Priv, rdpmc, check_rdpmc),
  2843. I(ImplicitOps | VendorSpecific, em_sysenter),
  2844. I(ImplicitOps | Priv | VendorSpecific, em_sysexit),
  2845. N, N,
  2846. N, N, N, N, N, N, N, N,
  2847. /* 0x40 - 0x4F */
  2848. X16(D(DstReg | SrcMem | ModRM | Mov)),
  2849. /* 0x50 - 0x5F */
  2850. N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
  2851. /* 0x60 - 0x6F */
  2852. N, N, N, N,
  2853. N, N, N, N,
  2854. N, N, N, N,
  2855. N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
  2856. /* 0x70 - 0x7F */
  2857. N, N, N, N,
  2858. N, N, N, N,
  2859. N, N, N, N,
  2860. N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
  2861. /* 0x80 - 0x8F */
  2862. X16(D(SrcImm)),
  2863. /* 0x90 - 0x9F */
  2864. X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
  2865. /* 0xA0 - 0xA7 */
  2866. D(ImplicitOps | Stack), D(ImplicitOps | Stack),
  2867. DI(ImplicitOps, cpuid), D(DstMem | SrcReg | ModRM | BitOp),
  2868. D(DstMem | SrcReg | Src2ImmByte | ModRM),
  2869. D(DstMem | SrcReg | Src2CL | ModRM), N, N,
  2870. /* 0xA8 - 0xAF */
  2871. D(ImplicitOps | Stack), D(ImplicitOps | Stack),
  2872. DI(ImplicitOps, rsm), D(DstMem | SrcReg | ModRM | BitOp | Lock),
  2873. D(DstMem | SrcReg | Src2ImmByte | ModRM),
  2874. D(DstMem | SrcReg | Src2CL | ModRM),
  2875. D(ModRM), I(DstReg | SrcMem | ModRM, em_imul),
  2876. /* 0xB0 - 0xB7 */
  2877. D2bv(DstMem | SrcReg | ModRM | Lock),
  2878. D(DstReg | SrcMemFAddr | ModRM), D(DstMem | SrcReg | ModRM | BitOp | Lock),
  2879. D(DstReg | SrcMemFAddr | ModRM), D(DstReg | SrcMemFAddr | ModRM),
  2880. D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
  2881. /* 0xB8 - 0xBF */
  2882. N, N,
  2883. G(BitOp, group8), D(DstMem | SrcReg | ModRM | BitOp | Lock),
  2884. D(DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM),
  2885. D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
  2886. /* 0xC0 - 0xCF */
  2887. D2bv(DstMem | SrcReg | ModRM | Lock),
  2888. N, D(DstMem | SrcReg | ModRM | Mov),
  2889. N, N, N, GD(0, &group9),
  2890. N, N, N, N, N, N, N, N,
  2891. /* 0xD0 - 0xDF */
  2892. N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
  2893. /* 0xE0 - 0xEF */
  2894. N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
  2895. /* 0xF0 - 0xFF */
  2896. N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
  2897. };
  2898. #undef D
  2899. #undef N
  2900. #undef G
  2901. #undef GD
  2902. #undef I
  2903. #undef GP
  2904. #undef EXT
  2905. #undef D2bv
  2906. #undef D2bvIP
  2907. #undef I2bv
  2908. #undef I6ALU
  2909. static unsigned imm_size(struct decode_cache *c)
  2910. {
  2911. unsigned size;
  2912. size = (c->d & ByteOp) ? 1 : c->op_bytes;
  2913. if (size == 8)
  2914. size = 4;
  2915. return size;
  2916. }
  2917. static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
  2918. unsigned size, bool sign_extension)
  2919. {
  2920. struct decode_cache *c = &ctxt->decode;
  2921. int rc = X86EMUL_CONTINUE;
  2922. op->type = OP_IMM;
  2923. op->bytes = size;
  2924. op->addr.mem.ea = c->eip;
  2925. /* NB. Immediates are sign-extended as necessary. */
  2926. switch (op->bytes) {
  2927. case 1:
  2928. op->val = insn_fetch(s8, 1, c->eip);
  2929. break;
  2930. case 2:
  2931. op->val = insn_fetch(s16, 2, c->eip);
  2932. break;
  2933. case 4:
  2934. op->val = insn_fetch(s32, 4, c->eip);
  2935. break;
  2936. }
  2937. if (!sign_extension) {
  2938. switch (op->bytes) {
  2939. case 1:
  2940. op->val &= 0xff;
  2941. break;
  2942. case 2:
  2943. op->val &= 0xffff;
  2944. break;
  2945. case 4:
  2946. op->val &= 0xffffffff;
  2947. break;
  2948. }
  2949. }
  2950. done:
  2951. return rc;
  2952. }
  2953. int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
  2954. {
  2955. struct decode_cache *c = &ctxt->decode;
  2956. int rc = X86EMUL_CONTINUE;
  2957. int mode = ctxt->mode;
  2958. int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
  2959. bool op_prefix = false;
  2960. struct opcode opcode;
  2961. struct operand memop = { .type = OP_NONE }, *memopp = NULL;
  2962. c->eip = ctxt->eip;
  2963. c->fetch.start = c->eip;
  2964. c->fetch.end = c->fetch.start + insn_len;
  2965. if (insn_len > 0)
  2966. memcpy(c->fetch.data, insn, insn_len);
  2967. switch (mode) {
  2968. case X86EMUL_MODE_REAL:
  2969. case X86EMUL_MODE_VM86:
  2970. case X86EMUL_MODE_PROT16:
  2971. def_op_bytes = def_ad_bytes = 2;
  2972. break;
  2973. case X86EMUL_MODE_PROT32:
  2974. def_op_bytes = def_ad_bytes = 4;
  2975. break;
  2976. #ifdef CONFIG_X86_64
  2977. case X86EMUL_MODE_PROT64:
  2978. def_op_bytes = 4;
  2979. def_ad_bytes = 8;
  2980. break;
  2981. #endif
  2982. default:
  2983. return -1;
  2984. }
  2985. c->op_bytes = def_op_bytes;
  2986. c->ad_bytes = def_ad_bytes;
  2987. /* Legacy prefixes. */
  2988. for (;;) {
  2989. switch (c->b = insn_fetch(u8, 1, c->eip)) {
  2990. case 0x66: /* operand-size override */
  2991. op_prefix = true;
  2992. /* switch between 2/4 bytes */
  2993. c->op_bytes = def_op_bytes ^ 6;
  2994. break;
  2995. case 0x67: /* address-size override */
  2996. if (mode == X86EMUL_MODE_PROT64)
  2997. /* switch between 4/8 bytes */
  2998. c->ad_bytes = def_ad_bytes ^ 12;
  2999. else
  3000. /* switch between 2/4 bytes */
  3001. c->ad_bytes = def_ad_bytes ^ 6;
  3002. break;
  3003. case 0x26: /* ES override */
  3004. case 0x2e: /* CS override */
  3005. case 0x36: /* SS override */
  3006. case 0x3e: /* DS override */
  3007. set_seg_override(c, (c->b >> 3) & 3);
  3008. break;
  3009. case 0x64: /* FS override */
  3010. case 0x65: /* GS override */
  3011. set_seg_override(c, c->b & 7);
  3012. break;
  3013. case 0x40 ... 0x4f: /* REX */
  3014. if (mode != X86EMUL_MODE_PROT64)
  3015. goto done_prefixes;
  3016. c->rex_prefix = c->b;
  3017. continue;
  3018. case 0xf0: /* LOCK */
  3019. c->lock_prefix = 1;
  3020. break;
  3021. case 0xf2: /* REPNE/REPNZ */
  3022. case 0xf3: /* REP/REPE/REPZ */
  3023. c->rep_prefix = c->b;
  3024. break;
  3025. default:
  3026. goto done_prefixes;
  3027. }
  3028. /* Any legacy prefix after a REX prefix nullifies its effect. */
  3029. c->rex_prefix = 0;
  3030. }
  3031. done_prefixes:
  3032. /* REX prefix. */
  3033. if (c->rex_prefix & 8)
  3034. c->op_bytes = 8; /* REX.W */
  3035. /* Opcode byte(s). */
  3036. opcode = opcode_table[c->b];
  3037. /* Two-byte opcode? */
  3038. if (c->b == 0x0f) {
  3039. c->twobyte = 1;
  3040. c->b = insn_fetch(u8, 1, c->eip);
  3041. opcode = twobyte_table[c->b];
  3042. }
  3043. c->d = opcode.flags;
  3044. while (c->d & GroupMask) {
  3045. switch (c->d & GroupMask) {
  3046. case Group:
  3047. c->modrm = insn_fetch(u8, 1, c->eip);
  3048. --c->eip;
  3049. goffset = (c->modrm >> 3) & 7;
  3050. opcode = opcode.u.group[goffset];
  3051. break;
  3052. case GroupDual:
  3053. c->modrm = insn_fetch(u8, 1, c->eip);
  3054. --c->eip;
  3055. goffset = (c->modrm >> 3) & 7;
  3056. if ((c->modrm >> 6) == 3)
  3057. opcode = opcode.u.gdual->mod3[goffset];
  3058. else
  3059. opcode = opcode.u.gdual->mod012[goffset];
  3060. break;
  3061. case RMExt:
  3062. goffset = c->modrm & 7;
  3063. opcode = opcode.u.group[goffset];
  3064. break;
  3065. case Prefix:
  3066. if (c->rep_prefix && op_prefix)
  3067. return X86EMUL_UNHANDLEABLE;
  3068. simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
  3069. switch (simd_prefix) {
  3070. case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
  3071. case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
  3072. case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
  3073. case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
  3074. }
  3075. break;
  3076. default:
  3077. return X86EMUL_UNHANDLEABLE;
  3078. }
  3079. c->d &= ~GroupMask;
  3080. c->d |= opcode.flags;
  3081. }
  3082. c->execute = opcode.u.execute;
  3083. c->check_perm = opcode.check_perm;
  3084. c->intercept = opcode.intercept;
  3085. /* Unrecognised? */
  3086. if (c->d == 0 || (c->d & Undefined))
  3087. return -1;
  3088. if (!(c->d & VendorSpecific) && ctxt->only_vendor_specific_insn)
  3089. return -1;
  3090. if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
  3091. c->op_bytes = 8;
  3092. if (c->d & Op3264) {
  3093. if (mode == X86EMUL_MODE_PROT64)
  3094. c->op_bytes = 8;
  3095. else
  3096. c->op_bytes = 4;
  3097. }
  3098. if (c->d & Sse)
  3099. c->op_bytes = 16;
  3100. /* ModRM and SIB bytes. */
  3101. if (c->d & ModRM) {
  3102. rc = decode_modrm(ctxt, &memop);
  3103. if (!c->has_seg_override)
  3104. set_seg_override(c, c->modrm_seg);
  3105. } else if (c->d & MemAbs)
  3106. rc = decode_abs(ctxt, &memop);
  3107. if (rc != X86EMUL_CONTINUE)
  3108. goto done;
  3109. if (!c->has_seg_override)
  3110. set_seg_override(c, VCPU_SREG_DS);
  3111. memop.addr.mem.seg = seg_override(ctxt, c);
  3112. if (memop.type == OP_MEM && c->ad_bytes != 8)
  3113. memop.addr.mem.ea = (u32)memop.addr.mem.ea;
  3114. /*
  3115. * Decode and fetch the source operand: register, memory
  3116. * or immediate.
  3117. */
  3118. switch (c->d & SrcMask) {
  3119. case SrcNone:
  3120. break;
  3121. case SrcReg:
  3122. decode_register_operand(ctxt, &c->src, c, 0);
  3123. break;
  3124. case SrcMem16:
  3125. memop.bytes = 2;
  3126. goto srcmem_common;
  3127. case SrcMem32:
  3128. memop.bytes = 4;
  3129. goto srcmem_common;
  3130. case SrcMem:
  3131. memop.bytes = (c->d & ByteOp) ? 1 :
  3132. c->op_bytes;
  3133. srcmem_common:
  3134. c->src = memop;
  3135. memopp = &c->src;
  3136. break;
  3137. case SrcImmU16:
  3138. rc = decode_imm(ctxt, &c->src, 2, false);
  3139. break;
  3140. case SrcImm:
  3141. rc = decode_imm(ctxt, &c->src, imm_size(c), true);
  3142. break;
  3143. case SrcImmU:
  3144. rc = decode_imm(ctxt, &c->src, imm_size(c), false);
  3145. break;
  3146. case SrcImmByte:
  3147. rc = decode_imm(ctxt, &c->src, 1, true);
  3148. break;
  3149. case SrcImmUByte:
  3150. rc = decode_imm(ctxt, &c->src, 1, false);
  3151. break;
  3152. case SrcAcc:
  3153. c->src.type = OP_REG;
  3154. c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  3155. c->src.addr.reg = &c->regs[VCPU_REGS_RAX];
  3156. fetch_register_operand(&c->src);
  3157. break;
  3158. case SrcOne:
  3159. c->src.bytes = 1;
  3160. c->src.val = 1;
  3161. break;
  3162. case SrcSI:
  3163. c->src.type = OP_MEM;
  3164. c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  3165. c->src.addr.mem.ea =
  3166. register_address(c, c->regs[VCPU_REGS_RSI]);
  3167. c->src.addr.mem.seg = seg_override(ctxt, c);
  3168. c->src.val = 0;
  3169. break;
  3170. case SrcImmFAddr:
  3171. c->src.type = OP_IMM;
  3172. c->src.addr.mem.ea = c->eip;
  3173. c->src.bytes = c->op_bytes + 2;
  3174. insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip);
  3175. break;
  3176. case SrcMemFAddr:
  3177. memop.bytes = c->op_bytes + 2;
  3178. goto srcmem_common;
  3179. break;
  3180. case SrcDX:
  3181. c->src.type = OP_REG;
  3182. c->src.bytes = 2;
  3183. c->src.addr.reg = &c->regs[VCPU_REGS_RDX];
  3184. fetch_register_operand(&c->src);
  3185. break;
  3186. }
  3187. if (rc != X86EMUL_CONTINUE)
  3188. goto done;
  3189. /*
  3190. * Decode and fetch the second source operand: register, memory
  3191. * or immediate.
  3192. */
  3193. switch (c->d & Src2Mask) {
  3194. case Src2None:
  3195. break;
  3196. case Src2CL:
  3197. c->src2.bytes = 1;
  3198. c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8;
  3199. break;
  3200. case Src2ImmByte:
  3201. rc = decode_imm(ctxt, &c->src2, 1, true);
  3202. break;
  3203. case Src2One:
  3204. c->src2.bytes = 1;
  3205. c->src2.val = 1;
  3206. break;
  3207. case Src2Imm:
  3208. rc = decode_imm(ctxt, &c->src2, imm_size(c), true);
  3209. break;
  3210. }
  3211. if (rc != X86EMUL_CONTINUE)
  3212. goto done;
  3213. /* Decode and fetch the destination operand: register or memory. */
  3214. switch (c->d & DstMask) {
  3215. case DstReg:
  3216. decode_register_operand(ctxt, &c->dst, c,
  3217. c->twobyte && (c->b == 0xb6 || c->b == 0xb7));
  3218. break;
  3219. case DstImmUByte:
  3220. c->dst.type = OP_IMM;
  3221. c->dst.addr.mem.ea = c->eip;
  3222. c->dst.bytes = 1;
  3223. c->dst.val = insn_fetch(u8, 1, c->eip);
  3224. break;
  3225. case DstMem:
  3226. case DstMem64:
  3227. c->dst = memop;
  3228. memopp = &c->dst;
  3229. if ((c->d & DstMask) == DstMem64)
  3230. c->dst.bytes = 8;
  3231. else
  3232. c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  3233. if (c->d & BitOp)
  3234. fetch_bit_operand(c);
  3235. c->dst.orig_val = c->dst.val;
  3236. break;
  3237. case DstAcc:
  3238. c->dst.type = OP_REG;
  3239. c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  3240. c->dst.addr.reg = &c->regs[VCPU_REGS_RAX];
  3241. fetch_register_operand(&c->dst);
  3242. c->dst.orig_val = c->dst.val;
  3243. break;
  3244. case DstDI:
  3245. c->dst.type = OP_MEM;
  3246. c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
  3247. c->dst.addr.mem.ea =
  3248. register_address(c, c->regs[VCPU_REGS_RDI]);
  3249. c->dst.addr.mem.seg = VCPU_SREG_ES;
  3250. c->dst.val = 0;
  3251. break;
  3252. case DstDX:
  3253. c->dst.type = OP_REG;
  3254. c->dst.bytes = 2;
  3255. c->dst.addr.reg = &c->regs[VCPU_REGS_RDX];
  3256. fetch_register_operand(&c->dst);
  3257. break;
  3258. case ImplicitOps:
  3259. /* Special instructions do their own operand decoding. */
  3260. default:
  3261. c->dst.type = OP_NONE; /* Disable writeback. */
  3262. break;
  3263. }
  3264. done:
  3265. if (memopp && memopp->type == OP_MEM && c->rip_relative)
  3266. memopp->addr.mem.ea += c->eip;
  3267. return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
  3268. }
  3269. static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
  3270. {
  3271. struct decode_cache *c = &ctxt->decode;
  3272. /* The second termination condition only applies for REPE
  3273. * and REPNE. Test if the repeat string operation prefix is
  3274. * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
  3275. * corresponding termination condition according to:
  3276. * - if REPE/REPZ and ZF = 0 then done
  3277. * - if REPNE/REPNZ and ZF = 1 then done
  3278. */
  3279. if (((c->b == 0xa6) || (c->b == 0xa7) ||
  3280. (c->b == 0xae) || (c->b == 0xaf))
  3281. && (((c->rep_prefix == REPE_PREFIX) &&
  3282. ((ctxt->eflags & EFLG_ZF) == 0))
  3283. || ((c->rep_prefix == REPNE_PREFIX) &&
  3284. ((ctxt->eflags & EFLG_ZF) == EFLG_ZF))))
  3285. return true;
  3286. return false;
  3287. }
  3288. int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
  3289. {
  3290. struct x86_emulate_ops *ops = ctxt->ops;
  3291. u64 msr_data;
  3292. struct decode_cache *c = &ctxt->decode;
  3293. int rc = X86EMUL_CONTINUE;
  3294. int saved_dst_type = c->dst.type;
  3295. c->mem_read.pos = 0;
  3296. if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) {
  3297. rc = emulate_ud(ctxt);
  3298. goto done;
  3299. }
  3300. /* LOCK prefix is allowed only with some instructions */
  3301. if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) {
  3302. rc = emulate_ud(ctxt);
  3303. goto done;
  3304. }
  3305. if ((c->d & SrcMask) == SrcMemFAddr && c->src.type != OP_MEM) {
  3306. rc = emulate_ud(ctxt);
  3307. goto done;
  3308. }
  3309. if ((c->d & Sse)
  3310. && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)
  3311. || !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
  3312. rc = emulate_ud(ctxt);
  3313. goto done;
  3314. }
  3315. if ((c->d & Sse) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
  3316. rc = emulate_nm(ctxt);
  3317. goto done;
  3318. }
  3319. if (unlikely(ctxt->guest_mode) && c->intercept) {
  3320. rc = emulator_check_intercept(ctxt, c->intercept,
  3321. X86_ICPT_PRE_EXCEPT);
  3322. if (rc != X86EMUL_CONTINUE)
  3323. goto done;
  3324. }
  3325. /* Privileged instruction can be executed only in CPL=0 */
  3326. if ((c->d & Priv) && ops->cpl(ctxt)) {
  3327. rc = emulate_gp(ctxt, 0);
  3328. goto done;
  3329. }
  3330. /* Instruction can only be executed in protected mode */
  3331. if ((c->d & Prot) && !(ctxt->mode & X86EMUL_MODE_PROT)) {
  3332. rc = emulate_ud(ctxt);
  3333. goto done;
  3334. }
  3335. /* Do instruction specific permission checks */
  3336. if (c->check_perm) {
  3337. rc = c->check_perm(ctxt);
  3338. if (rc != X86EMUL_CONTINUE)
  3339. goto done;
  3340. }
  3341. if (unlikely(ctxt->guest_mode) && c->intercept) {
  3342. rc = emulator_check_intercept(ctxt, c->intercept,
  3343. X86_ICPT_POST_EXCEPT);
  3344. if (rc != X86EMUL_CONTINUE)
  3345. goto done;
  3346. }
  3347. if (c->rep_prefix && (c->d & String)) {
  3348. /* All REP prefixes have the same first termination condition */
  3349. if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) {
  3350. ctxt->eip = c->eip;
  3351. goto done;
  3352. }
  3353. }
  3354. if ((c->src.type == OP_MEM) && !(c->d & NoAccess)) {
  3355. rc = segmented_read(ctxt, c->src.addr.mem,
  3356. c->src.valptr, c->src.bytes);
  3357. if (rc != X86EMUL_CONTINUE)
  3358. goto done;
  3359. c->src.orig_val64 = c->src.val64;
  3360. }
  3361. if (c->src2.type == OP_MEM) {
  3362. rc = segmented_read(ctxt, c->src2.addr.mem,
  3363. &c->src2.val, c->src2.bytes);
  3364. if (rc != X86EMUL_CONTINUE)
  3365. goto done;
  3366. }
  3367. if ((c->d & DstMask) == ImplicitOps)
  3368. goto special_insn;
  3369. if ((c->dst.type == OP_MEM) && !(c->d & Mov)) {
  3370. /* optimisation - avoid slow emulated read if Mov */
  3371. rc = segmented_read(ctxt, c->dst.addr.mem,
  3372. &c->dst.val, c->dst.bytes);
  3373. if (rc != X86EMUL_CONTINUE)
  3374. goto done;
  3375. }
  3376. c->dst.orig_val = c->dst.val;
  3377. special_insn:
  3378. if (unlikely(ctxt->guest_mode) && c->intercept) {
  3379. rc = emulator_check_intercept(ctxt, c->intercept,
  3380. X86_ICPT_POST_MEMACCESS);
  3381. if (rc != X86EMUL_CONTINUE)
  3382. goto done;
  3383. }
  3384. if (c->execute) {
  3385. rc = c->execute(ctxt);
  3386. if (rc != X86EMUL_CONTINUE)
  3387. goto done;
  3388. goto writeback;
  3389. }
  3390. if (c->twobyte)
  3391. goto twobyte_insn;
  3392. switch (c->b) {
  3393. case 0x06: /* push es */
  3394. rc = emulate_push_sreg(ctxt, VCPU_SREG_ES);
  3395. break;
  3396. case 0x07: /* pop es */
  3397. rc = emulate_pop_sreg(ctxt, VCPU_SREG_ES);
  3398. break;
  3399. case 0x0e: /* push cs */
  3400. rc = emulate_push_sreg(ctxt, VCPU_SREG_CS);
  3401. break;
  3402. case 0x16: /* push ss */
  3403. rc = emulate_push_sreg(ctxt, VCPU_SREG_SS);
  3404. break;
  3405. case 0x17: /* pop ss */
  3406. rc = emulate_pop_sreg(ctxt, VCPU_SREG_SS);
  3407. break;
  3408. case 0x1e: /* push ds */
  3409. rc = emulate_push_sreg(ctxt, VCPU_SREG_DS);
  3410. break;
  3411. case 0x1f: /* pop ds */
  3412. rc = emulate_pop_sreg(ctxt, VCPU_SREG_DS);
  3413. break;
  3414. case 0x40 ... 0x47: /* inc r16/r32 */
  3415. emulate_1op("inc", c->dst, ctxt->eflags);
  3416. break;
  3417. case 0x48 ... 0x4f: /* dec r16/r32 */
  3418. emulate_1op("dec", c->dst, ctxt->eflags);
  3419. break;
  3420. case 0x63: /* movsxd */
  3421. if (ctxt->mode != X86EMUL_MODE_PROT64)
  3422. goto cannot_emulate;
  3423. c->dst.val = (s32) c->src.val;
  3424. break;
  3425. case 0x6c: /* insb */
  3426. case 0x6d: /* insw/insd */
  3427. c->src.val = c->regs[VCPU_REGS_RDX];
  3428. goto do_io_in;
  3429. case 0x6e: /* outsb */
  3430. case 0x6f: /* outsw/outsd */
  3431. c->dst.val = c->regs[VCPU_REGS_RDX];
  3432. goto do_io_out;
  3433. break;
  3434. case 0x70 ... 0x7f: /* jcc (short) */
  3435. if (test_cc(c->b, ctxt->eflags))
  3436. jmp_rel(c, c->src.val);
  3437. break;
  3438. case 0x8d: /* lea r16/r32, m */
  3439. c->dst.val = c->src.addr.mem.ea;
  3440. break;
  3441. case 0x8f: /* pop (sole member of Grp1a) */
  3442. rc = em_grp1a(ctxt);
  3443. break;
  3444. case 0x90 ... 0x97: /* nop / xchg reg, rax */
  3445. if (c->dst.addr.reg == &c->regs[VCPU_REGS_RAX])
  3446. break;
  3447. rc = em_xchg(ctxt);
  3448. break;
  3449. case 0x98: /* cbw/cwde/cdqe */
  3450. switch (c->op_bytes) {
  3451. case 2: c->dst.val = (s8)c->dst.val; break;
  3452. case 4: c->dst.val = (s16)c->dst.val; break;
  3453. case 8: c->dst.val = (s32)c->dst.val; break;
  3454. }
  3455. break;
  3456. case 0xc0 ... 0xc1:
  3457. rc = em_grp2(ctxt);
  3458. break;
  3459. case 0xc4: /* les */
  3460. rc = emulate_load_segment(ctxt, VCPU_SREG_ES);
  3461. break;
  3462. case 0xc5: /* lds */
  3463. rc = emulate_load_segment(ctxt, VCPU_SREG_DS);
  3464. break;
  3465. case 0xcc: /* int3 */
  3466. rc = emulate_int(ctxt, 3);
  3467. break;
  3468. case 0xcd: /* int n */
  3469. rc = emulate_int(ctxt, c->src.val);
  3470. break;
  3471. case 0xce: /* into */
  3472. if (ctxt->eflags & EFLG_OF)
  3473. rc = emulate_int(ctxt, 4);
  3474. break;
  3475. case 0xd0 ... 0xd1: /* Grp2 */
  3476. rc = em_grp2(ctxt);
  3477. break;
  3478. case 0xd2 ... 0xd3: /* Grp2 */
  3479. c->src.val = c->regs[VCPU_REGS_RCX];
  3480. rc = em_grp2(ctxt);
  3481. break;
  3482. case 0xe4: /* inb */
  3483. case 0xe5: /* in */
  3484. goto do_io_in;
  3485. case 0xe6: /* outb */
  3486. case 0xe7: /* out */
  3487. goto do_io_out;
  3488. case 0xe8: /* call (near) */ {
  3489. long int rel = c->src.val;
  3490. c->src.val = (unsigned long) c->eip;
  3491. jmp_rel(c, rel);
  3492. rc = em_push(ctxt);
  3493. break;
  3494. }
  3495. case 0xe9: /* jmp rel */
  3496. case 0xeb: /* jmp rel short */
  3497. jmp_rel(c, c->src.val);
  3498. c->dst.type = OP_NONE; /* Disable writeback. */
  3499. break;
  3500. case 0xec: /* in al,dx */
  3501. case 0xed: /* in (e/r)ax,dx */
  3502. do_io_in:
  3503. if (!pio_in_emulated(ctxt, c->dst.bytes, c->src.val,
  3504. &c->dst.val))
  3505. goto done; /* IO is needed */
  3506. break;
  3507. case 0xee: /* out dx,al */
  3508. case 0xef: /* out dx,(e/r)ax */
  3509. do_io_out:
  3510. ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
  3511. &c->src.val, 1);
  3512. c->dst.type = OP_NONE; /* Disable writeback. */
  3513. break;
  3514. case 0xf4: /* hlt */
  3515. ctxt->ops->halt(ctxt);
  3516. break;
  3517. case 0xf5: /* cmc */
  3518. /* complement carry flag from eflags reg */
  3519. ctxt->eflags ^= EFLG_CF;
  3520. break;
  3521. case 0xf6 ... 0xf7: /* Grp3 */
  3522. rc = em_grp3(ctxt);
  3523. break;
  3524. case 0xf8: /* clc */
  3525. ctxt->eflags &= ~EFLG_CF;
  3526. break;
  3527. case 0xf9: /* stc */
  3528. ctxt->eflags |= EFLG_CF;
  3529. break;
  3530. case 0xfa: /* cli */
  3531. if (emulator_bad_iopl(ctxt)) {
  3532. rc = emulate_gp(ctxt, 0);
  3533. goto done;
  3534. } else
  3535. ctxt->eflags &= ~X86_EFLAGS_IF;
  3536. break;
  3537. case 0xfb: /* sti */
  3538. if (emulator_bad_iopl(ctxt)) {
  3539. rc = emulate_gp(ctxt, 0);
  3540. goto done;
  3541. } else {
  3542. ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
  3543. ctxt->eflags |= X86_EFLAGS_IF;
  3544. }
  3545. break;
  3546. case 0xfc: /* cld */
  3547. ctxt->eflags &= ~EFLG_DF;
  3548. break;
  3549. case 0xfd: /* std */
  3550. ctxt->eflags |= EFLG_DF;
  3551. break;
  3552. case 0xfe: /* Grp4 */
  3553. rc = em_grp45(ctxt);
  3554. break;
  3555. case 0xff: /* Grp5 */
  3556. rc = em_grp45(ctxt);
  3557. break;
  3558. default:
  3559. goto cannot_emulate;
  3560. }
  3561. if (rc != X86EMUL_CONTINUE)
  3562. goto done;
  3563. writeback:
  3564. rc = writeback(ctxt);
  3565. if (rc != X86EMUL_CONTINUE)
  3566. goto done;
  3567. /*
  3568. * restore dst type in case the decoding will be reused
  3569. * (happens for string instruction )
  3570. */
  3571. c->dst.type = saved_dst_type;
  3572. if ((c->d & SrcMask) == SrcSI)
  3573. string_addr_inc(ctxt, seg_override(ctxt, c),
  3574. VCPU_REGS_RSI, &c->src);
  3575. if ((c->d & DstMask) == DstDI)
  3576. string_addr_inc(ctxt, VCPU_SREG_ES, VCPU_REGS_RDI,
  3577. &c->dst);
  3578. if (c->rep_prefix && (c->d & String)) {
  3579. struct read_cache *r = &c->io_read;
  3580. register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1);
  3581. if (!string_insn_completed(ctxt)) {
  3582. /*
  3583. * Re-enter guest when pio read ahead buffer is empty
  3584. * or, if it is not used, after each 1024 iteration.
  3585. */
  3586. if ((r->end != 0 || c->regs[VCPU_REGS_RCX] & 0x3ff) &&
  3587. (r->end == 0 || r->end != r->pos)) {
  3588. /*
  3589. * Reset read cache. Usually happens before
  3590. * decode, but since instruction is restarted
  3591. * we have to do it here.
  3592. */
  3593. c->mem_read.end = 0;
  3594. return EMULATION_RESTART;
  3595. }
  3596. goto done; /* skip rip writeback */
  3597. }
  3598. }
  3599. ctxt->eip = c->eip;
  3600. done:
  3601. if (rc == X86EMUL_PROPAGATE_FAULT)
  3602. ctxt->have_exception = true;
  3603. if (rc == X86EMUL_INTERCEPTED)
  3604. return EMULATION_INTERCEPTED;
  3605. return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
  3606. twobyte_insn:
  3607. switch (c->b) {
  3608. case 0x09: /* wbinvd */
  3609. (ctxt->ops->wbinvd)(ctxt);
  3610. break;
  3611. case 0x08: /* invd */
  3612. case 0x0d: /* GrpP (prefetch) */
  3613. case 0x18: /* Grp16 (prefetch/nop) */
  3614. break;
  3615. case 0x20: /* mov cr, reg */
  3616. c->dst.val = ops->get_cr(ctxt, c->modrm_reg);
  3617. break;
  3618. case 0x21: /* mov from dr to reg */
  3619. ops->get_dr(ctxt, c->modrm_reg, &c->dst.val);
  3620. break;
  3621. case 0x22: /* mov reg, cr */
  3622. if (ops->set_cr(ctxt, c->modrm_reg, c->src.val)) {
  3623. emulate_gp(ctxt, 0);
  3624. rc = X86EMUL_PROPAGATE_FAULT;
  3625. goto done;
  3626. }
  3627. c->dst.type = OP_NONE;
  3628. break;
  3629. case 0x23: /* mov from reg to dr */
  3630. if (ops->set_dr(ctxt, c->modrm_reg, c->src.val &
  3631. ((ctxt->mode == X86EMUL_MODE_PROT64) ?
  3632. ~0ULL : ~0U)) < 0) {
  3633. /* #UD condition is already handled by the code above */
  3634. emulate_gp(ctxt, 0);
  3635. rc = X86EMUL_PROPAGATE_FAULT;
  3636. goto done;
  3637. }
  3638. c->dst.type = OP_NONE; /* no writeback */
  3639. break;
  3640. case 0x30:
  3641. /* wrmsr */
  3642. msr_data = (u32)c->regs[VCPU_REGS_RAX]
  3643. | ((u64)c->regs[VCPU_REGS_RDX] << 32);
  3644. if (ops->set_msr(ctxt, c->regs[VCPU_REGS_RCX], msr_data)) {
  3645. emulate_gp(ctxt, 0);
  3646. rc = X86EMUL_PROPAGATE_FAULT;
  3647. goto done;
  3648. }
  3649. rc = X86EMUL_CONTINUE;
  3650. break;
  3651. case 0x32:
  3652. /* rdmsr */
  3653. if (ops->get_msr(ctxt, c->regs[VCPU_REGS_RCX], &msr_data)) {
  3654. emulate_gp(ctxt, 0);
  3655. rc = X86EMUL_PROPAGATE_FAULT;
  3656. goto done;
  3657. } else {
  3658. c->regs[VCPU_REGS_RAX] = (u32)msr_data;
  3659. c->regs[VCPU_REGS_RDX] = msr_data >> 32;
  3660. }
  3661. rc = X86EMUL_CONTINUE;
  3662. break;
  3663. case 0x40 ... 0x4f: /* cmov */
  3664. c->dst.val = c->dst.orig_val = c->src.val;
  3665. if (!test_cc(c->b, ctxt->eflags))
  3666. c->dst.type = OP_NONE; /* no writeback */
  3667. break;
  3668. case 0x80 ... 0x8f: /* jnz rel, etc*/
  3669. if (test_cc(c->b, ctxt->eflags))
  3670. jmp_rel(c, c->src.val);
  3671. break;
  3672. case 0x90 ... 0x9f: /* setcc r/m8 */
  3673. c->dst.val = test_cc(c->b, ctxt->eflags);
  3674. break;
  3675. case 0xa0: /* push fs */
  3676. rc = emulate_push_sreg(ctxt, VCPU_SREG_FS);
  3677. break;
  3678. case 0xa1: /* pop fs */
  3679. rc = emulate_pop_sreg(ctxt, VCPU_SREG_FS);
  3680. break;
  3681. case 0xa3:
  3682. bt: /* bt */
  3683. c->dst.type = OP_NONE;
  3684. /* only subword offset */
  3685. c->src.val &= (c->dst.bytes << 3) - 1;
  3686. emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags);
  3687. break;
  3688. case 0xa4: /* shld imm8, r, r/m */
  3689. case 0xa5: /* shld cl, r, r/m */
  3690. emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags);
  3691. break;
  3692. case 0xa8: /* push gs */
  3693. rc = emulate_push_sreg(ctxt, VCPU_SREG_GS);
  3694. break;
  3695. case 0xa9: /* pop gs */
  3696. rc = emulate_pop_sreg(ctxt, VCPU_SREG_GS);
  3697. break;
  3698. case 0xab:
  3699. bts: /* bts */
  3700. emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags);
  3701. break;
  3702. case 0xac: /* shrd imm8, r, r/m */
  3703. case 0xad: /* shrd cl, r, r/m */
  3704. emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags);
  3705. break;
  3706. case 0xae: /* clflush */
  3707. break;
  3708. case 0xb0 ... 0xb1: /* cmpxchg */
  3709. /*
  3710. * Save real source value, then compare EAX against
  3711. * destination.
  3712. */
  3713. c->src.orig_val = c->src.val;
  3714. c->src.val = c->regs[VCPU_REGS_RAX];
  3715. emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags);
  3716. if (ctxt->eflags & EFLG_ZF) {
  3717. /* Success: write back to memory. */
  3718. c->dst.val = c->src.orig_val;
  3719. } else {
  3720. /* Failure: write the value we saw to EAX. */
  3721. c->dst.type = OP_REG;
  3722. c->dst.addr.reg = (unsigned long *)&c->regs[VCPU_REGS_RAX];
  3723. }
  3724. break;
  3725. case 0xb2: /* lss */
  3726. rc = emulate_load_segment(ctxt, VCPU_SREG_SS);
  3727. break;
  3728. case 0xb3:
  3729. btr: /* btr */
  3730. emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags);
  3731. break;
  3732. case 0xb4: /* lfs */
  3733. rc = emulate_load_segment(ctxt, VCPU_SREG_FS);
  3734. break;
  3735. case 0xb5: /* lgs */
  3736. rc = emulate_load_segment(ctxt, VCPU_SREG_GS);
  3737. break;
  3738. case 0xb6 ... 0xb7: /* movzx */
  3739. c->dst.bytes = c->op_bytes;
  3740. c->dst.val = (c->d & ByteOp) ? (u8) c->src.val
  3741. : (u16) c->src.val;
  3742. break;
  3743. case 0xba: /* Grp8 */
  3744. switch (c->modrm_reg & 3) {
  3745. case 0:
  3746. goto bt;
  3747. case 1:
  3748. goto bts;
  3749. case 2:
  3750. goto btr;
  3751. case 3:
  3752. goto btc;
  3753. }
  3754. break;
  3755. case 0xbb:
  3756. btc: /* btc */
  3757. emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags);
  3758. break;
  3759. case 0xbc: { /* bsf */
  3760. u8 zf;
  3761. __asm__ ("bsf %2, %0; setz %1"
  3762. : "=r"(c->dst.val), "=q"(zf)
  3763. : "r"(c->src.val));
  3764. ctxt->eflags &= ~X86_EFLAGS_ZF;
  3765. if (zf) {
  3766. ctxt->eflags |= X86_EFLAGS_ZF;
  3767. c->dst.type = OP_NONE; /* Disable writeback. */
  3768. }
  3769. break;
  3770. }
  3771. case 0xbd: { /* bsr */
  3772. u8 zf;
  3773. __asm__ ("bsr %2, %0; setz %1"
  3774. : "=r"(c->dst.val), "=q"(zf)
  3775. : "r"(c->src.val));
  3776. ctxt->eflags &= ~X86_EFLAGS_ZF;
  3777. if (zf) {
  3778. ctxt->eflags |= X86_EFLAGS_ZF;
  3779. c->dst.type = OP_NONE; /* Disable writeback. */
  3780. }
  3781. break;
  3782. }
  3783. case 0xbe ... 0xbf: /* movsx */
  3784. c->dst.bytes = c->op_bytes;
  3785. c->dst.val = (c->d & ByteOp) ? (s8) c->src.val :
  3786. (s16) c->src.val;
  3787. break;
  3788. case 0xc0 ... 0xc1: /* xadd */
  3789. emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags);
  3790. /* Write back the register source. */
  3791. c->src.val = c->dst.orig_val;
  3792. write_register_operand(&c->src);
  3793. break;
  3794. case 0xc3: /* movnti */
  3795. c->dst.bytes = c->op_bytes;
  3796. c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val :
  3797. (u64) c->src.val;
  3798. break;
  3799. case 0xc7: /* Grp9 (cmpxchg8b) */
  3800. rc = em_grp9(ctxt);
  3801. break;
  3802. default:
  3803. goto cannot_emulate;
  3804. }
  3805. if (rc != X86EMUL_CONTINUE)
  3806. goto done;
  3807. goto writeback;
  3808. cannot_emulate:
  3809. return EMULATION_FAILED;
  3810. }