emulate.c 108 KB

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