emulate.c 113 KB

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