emulate.c 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840
  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 <asm/kvm_emulate.h>
  25. #include <linux/stringify.h>
  26. #include <asm/debugreg.h>
  27. #include <asm/nospec-branch.h>
  28. #include "x86.h"
  29. #include "tss.h"
  30. #include "mmu.h"
  31. #include "pmu.h"
  32. /*
  33. * Operand types
  34. */
  35. #define OpNone 0ull
  36. #define OpImplicit 1ull /* No generic decode */
  37. #define OpReg 2ull /* Register */
  38. #define OpMem 3ull /* Memory */
  39. #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */
  40. #define OpDI 5ull /* ES:DI/EDI/RDI */
  41. #define OpMem64 6ull /* Memory, 64-bit */
  42. #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */
  43. #define OpDX 8ull /* DX register */
  44. #define OpCL 9ull /* CL register (for shifts) */
  45. #define OpImmByte 10ull /* 8-bit sign extended immediate */
  46. #define OpOne 11ull /* Implied 1 */
  47. #define OpImm 12ull /* Sign extended up to 32-bit immediate */
  48. #define OpMem16 13ull /* Memory operand (16-bit). */
  49. #define OpMem32 14ull /* Memory operand (32-bit). */
  50. #define OpImmU 15ull /* Immediate operand, zero extended */
  51. #define OpSI 16ull /* SI/ESI/RSI */
  52. #define OpImmFAddr 17ull /* Immediate far address */
  53. #define OpMemFAddr 18ull /* Far address in memory */
  54. #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */
  55. #define OpES 20ull /* ES */
  56. #define OpCS 21ull /* CS */
  57. #define OpSS 22ull /* SS */
  58. #define OpDS 23ull /* DS */
  59. #define OpFS 24ull /* FS */
  60. #define OpGS 25ull /* GS */
  61. #define OpMem8 26ull /* 8-bit zero extended memory operand */
  62. #define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */
  63. #define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
  64. #define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
  65. #define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
  66. #define OpBits 5 /* Width of operand field */
  67. #define OpMask ((1ull << OpBits) - 1)
  68. /*
  69. * Opcode effective-address decode tables.
  70. * Note that we only emulate instructions that have at least one memory
  71. * operand (excluding implicit stack references). We assume that stack
  72. * references and instruction fetches will never occur in special memory
  73. * areas that require emulation. So, for example, 'mov <imm>,<reg>' need
  74. * not be handled.
  75. */
  76. /* Operand sizes: 8-bit operands or specified/overridden size. */
  77. #define ByteOp (1<<0) /* 8-bit operands. */
  78. /* Destination operand type. */
  79. #define DstShift 1
  80. #define ImplicitOps (OpImplicit << DstShift)
  81. #define DstReg (OpReg << DstShift)
  82. #define DstMem (OpMem << DstShift)
  83. #define DstAcc (OpAcc << DstShift)
  84. #define DstDI (OpDI << DstShift)
  85. #define DstMem64 (OpMem64 << DstShift)
  86. #define DstMem16 (OpMem16 << DstShift)
  87. #define DstImmUByte (OpImmUByte << DstShift)
  88. #define DstDX (OpDX << DstShift)
  89. #define DstAccLo (OpAccLo << DstShift)
  90. #define DstMask (OpMask << DstShift)
  91. /* Source operand type. */
  92. #define SrcShift 6
  93. #define SrcNone (OpNone << SrcShift)
  94. #define SrcReg (OpReg << SrcShift)
  95. #define SrcMem (OpMem << SrcShift)
  96. #define SrcMem16 (OpMem16 << SrcShift)
  97. #define SrcMem32 (OpMem32 << SrcShift)
  98. #define SrcImm (OpImm << SrcShift)
  99. #define SrcImmByte (OpImmByte << SrcShift)
  100. #define SrcOne (OpOne << SrcShift)
  101. #define SrcImmUByte (OpImmUByte << SrcShift)
  102. #define SrcImmU (OpImmU << SrcShift)
  103. #define SrcSI (OpSI << SrcShift)
  104. #define SrcXLat (OpXLat << SrcShift)
  105. #define SrcImmFAddr (OpImmFAddr << SrcShift)
  106. #define SrcMemFAddr (OpMemFAddr << SrcShift)
  107. #define SrcAcc (OpAcc << SrcShift)
  108. #define SrcImmU16 (OpImmU16 << SrcShift)
  109. #define SrcImm64 (OpImm64 << SrcShift)
  110. #define SrcDX (OpDX << SrcShift)
  111. #define SrcMem8 (OpMem8 << SrcShift)
  112. #define SrcAccHi (OpAccHi << SrcShift)
  113. #define SrcMask (OpMask << SrcShift)
  114. #define BitOp (1<<11)
  115. #define MemAbs (1<<12) /* Memory operand is absolute displacement */
  116. #define String (1<<13) /* String instruction (rep capable) */
  117. #define Stack (1<<14) /* Stack instruction (push/pop) */
  118. #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */
  119. #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */
  120. #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */
  121. #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */
  122. #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */
  123. #define Escape (5<<15) /* Escape to coprocessor instruction */
  124. #define InstrDual (6<<15) /* Alternate instruction decoding of mod == 3 */
  125. #define ModeDual (7<<15) /* Different instruction for 32/64 bit */
  126. #define Sse (1<<18) /* SSE Vector instruction */
  127. /* Generic ModRM decode. */
  128. #define ModRM (1<<19)
  129. /* Destination is only written; never read. */
  130. #define Mov (1<<20)
  131. /* Misc flags */
  132. #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */
  133. #define EmulateOnUD (1<<22) /* Emulate if unsupported by the host */
  134. #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */
  135. #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */
  136. #define Undefined (1<<25) /* No Such Instruction */
  137. #define Lock (1<<26) /* lock prefix is allowed for the instruction */
  138. #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */
  139. #define No64 (1<<28)
  140. #define PageTable (1 << 29) /* instruction used to write page table */
  141. #define NotImpl (1 << 30) /* instruction is not implemented */
  142. /* Source 2 operand type */
  143. #define Src2Shift (31)
  144. #define Src2None (OpNone << Src2Shift)
  145. #define Src2Mem (OpMem << Src2Shift)
  146. #define Src2CL (OpCL << Src2Shift)
  147. #define Src2ImmByte (OpImmByte << Src2Shift)
  148. #define Src2One (OpOne << Src2Shift)
  149. #define Src2Imm (OpImm << Src2Shift)
  150. #define Src2ES (OpES << Src2Shift)
  151. #define Src2CS (OpCS << Src2Shift)
  152. #define Src2SS (OpSS << Src2Shift)
  153. #define Src2DS (OpDS << Src2Shift)
  154. #define Src2FS (OpFS << Src2Shift)
  155. #define Src2GS (OpGS << Src2Shift)
  156. #define Src2Mask (OpMask << Src2Shift)
  157. #define Mmx ((u64)1 << 40) /* MMX Vector instruction */
  158. #define AlignMask ((u64)7 << 41)
  159. #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
  160. #define Unaligned ((u64)2 << 41) /* Explicitly unaligned (e.g. MOVDQU) */
  161. #define Avx ((u64)3 << 41) /* Advanced Vector Extensions */
  162. #define Aligned16 ((u64)4 << 41) /* Aligned to 16 byte boundary (e.g. FXSAVE) */
  163. #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */
  164. #define NoWrite ((u64)1 << 45) /* No writeback */
  165. #define SrcWrite ((u64)1 << 46) /* Write back src operand */
  166. #define NoMod ((u64)1 << 47) /* Mod field is ignored */
  167. #define Intercept ((u64)1 << 48) /* Has valid intercept field */
  168. #define CheckPerm ((u64)1 << 49) /* Has valid check_perm field */
  169. #define PrivUD ((u64)1 << 51) /* #UD instead of #GP on CPL > 0 */
  170. #define NearBranch ((u64)1 << 52) /* Near branches */
  171. #define No16 ((u64)1 << 53) /* No 16 bit operand */
  172. #define IncSP ((u64)1 << 54) /* SP is incremented before ModRM calc */
  173. #define TwoMemOp ((u64)1 << 55) /* Instruction has two memory operand */
  174. #define DstXacc (DstAccLo | SrcAccHi | SrcWrite)
  175. #define X2(x...) x, x
  176. #define X3(x...) X2(x), x
  177. #define X4(x...) X2(x), X2(x)
  178. #define X5(x...) X4(x), x
  179. #define X6(x...) X4(x), X2(x)
  180. #define X7(x...) X4(x), X3(x)
  181. #define X8(x...) X4(x), X4(x)
  182. #define X16(x...) X8(x), X8(x)
  183. #define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
  184. #define FASTOP_SIZE 8
  185. /*
  186. * fastop functions have a special calling convention:
  187. *
  188. * dst: rax (in/out)
  189. * src: rdx (in/out)
  190. * src2: rcx (in)
  191. * flags: rflags (in/out)
  192. * ex: rsi (in:fastop pointer, out:zero if exception)
  193. *
  194. * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
  195. * different operand sizes can be reached by calculation, rather than a jump
  196. * table (which would be bigger than the code).
  197. *
  198. * fastop functions are declared as taking a never-defined fastop parameter,
  199. * so they can't be called from C directly.
  200. */
  201. struct fastop;
  202. struct opcode {
  203. u64 flags : 56;
  204. u64 intercept : 8;
  205. union {
  206. int (*execute)(struct x86_emulate_ctxt *ctxt);
  207. const struct opcode *group;
  208. const struct group_dual *gdual;
  209. const struct gprefix *gprefix;
  210. const struct escape *esc;
  211. const struct instr_dual *idual;
  212. const struct mode_dual *mdual;
  213. void (*fastop)(struct fastop *fake);
  214. } u;
  215. int (*check_perm)(struct x86_emulate_ctxt *ctxt);
  216. };
  217. struct group_dual {
  218. struct opcode mod012[8];
  219. struct opcode mod3[8];
  220. };
  221. struct gprefix {
  222. struct opcode pfx_no;
  223. struct opcode pfx_66;
  224. struct opcode pfx_f2;
  225. struct opcode pfx_f3;
  226. };
  227. struct escape {
  228. struct opcode op[8];
  229. struct opcode high[64];
  230. };
  231. struct instr_dual {
  232. struct opcode mod012;
  233. struct opcode mod3;
  234. };
  235. struct mode_dual {
  236. struct opcode mode32;
  237. struct opcode mode64;
  238. };
  239. #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a
  240. enum x86_transfer_type {
  241. X86_TRANSFER_NONE,
  242. X86_TRANSFER_CALL_JMP,
  243. X86_TRANSFER_RET,
  244. X86_TRANSFER_TASK_SWITCH,
  245. };
  246. static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
  247. {
  248. if (!(ctxt->regs_valid & (1 << nr))) {
  249. ctxt->regs_valid |= 1 << nr;
  250. ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
  251. }
  252. return ctxt->_regs[nr];
  253. }
  254. static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
  255. {
  256. ctxt->regs_valid |= 1 << nr;
  257. ctxt->regs_dirty |= 1 << nr;
  258. return &ctxt->_regs[nr];
  259. }
  260. static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
  261. {
  262. reg_read(ctxt, nr);
  263. return reg_write(ctxt, nr);
  264. }
  265. static void writeback_registers(struct x86_emulate_ctxt *ctxt)
  266. {
  267. unsigned reg;
  268. for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16)
  269. ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]);
  270. }
  271. static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
  272. {
  273. ctxt->regs_dirty = 0;
  274. ctxt->regs_valid = 0;
  275. }
  276. /*
  277. * These EFLAGS bits are restored from saved value during emulation, and
  278. * any changes are written back to the saved value after emulation.
  279. */
  280. #define EFLAGS_MASK (X86_EFLAGS_OF|X86_EFLAGS_SF|X86_EFLAGS_ZF|X86_EFLAGS_AF|\
  281. X86_EFLAGS_PF|X86_EFLAGS_CF)
  282. #ifdef CONFIG_X86_64
  283. #define ON64(x) x
  284. #else
  285. #define ON64(x)
  286. #endif
  287. static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *));
  288. #define FOP_FUNC(name) \
  289. ".align " __stringify(FASTOP_SIZE) " \n\t" \
  290. ".type " name ", @function \n\t" \
  291. name ":\n\t"
  292. #define FOP_RET "ret \n\t"
  293. #define FOP_START(op) \
  294. extern void em_##op(struct fastop *fake); \
  295. asm(".pushsection .text, \"ax\" \n\t" \
  296. ".global em_" #op " \n\t" \
  297. FOP_FUNC("em_" #op)
  298. #define FOP_END \
  299. ".popsection")
  300. #define FOPNOP() \
  301. FOP_FUNC(__stringify(__UNIQUE_ID(nop))) \
  302. FOP_RET
  303. #define FOP1E(op, dst) \
  304. FOP_FUNC(#op "_" #dst) \
  305. "10: " #op " %" #dst " \n\t" FOP_RET
  306. #define FOP1EEX(op, dst) \
  307. FOP1E(op, dst) _ASM_EXTABLE(10b, kvm_fastop_exception)
  308. #define FASTOP1(op) \
  309. FOP_START(op) \
  310. FOP1E(op##b, al) \
  311. FOP1E(op##w, ax) \
  312. FOP1E(op##l, eax) \
  313. ON64(FOP1E(op##q, rax)) \
  314. FOP_END
  315. /* 1-operand, using src2 (for MUL/DIV r/m) */
  316. #define FASTOP1SRC2(op, name) \
  317. FOP_START(name) \
  318. FOP1E(op, cl) \
  319. FOP1E(op, cx) \
  320. FOP1E(op, ecx) \
  321. ON64(FOP1E(op, rcx)) \
  322. FOP_END
  323. /* 1-operand, using src2 (for MUL/DIV r/m), with exceptions */
  324. #define FASTOP1SRC2EX(op, name) \
  325. FOP_START(name) \
  326. FOP1EEX(op, cl) \
  327. FOP1EEX(op, cx) \
  328. FOP1EEX(op, ecx) \
  329. ON64(FOP1EEX(op, rcx)) \
  330. FOP_END
  331. #define FOP2E(op, dst, src) \
  332. FOP_FUNC(#op "_" #dst "_" #src) \
  333. #op " %" #src ", %" #dst " \n\t" FOP_RET
  334. #define FASTOP2(op) \
  335. FOP_START(op) \
  336. FOP2E(op##b, al, dl) \
  337. FOP2E(op##w, ax, dx) \
  338. FOP2E(op##l, eax, edx) \
  339. ON64(FOP2E(op##q, rax, rdx)) \
  340. FOP_END
  341. /* 2 operand, word only */
  342. #define FASTOP2W(op) \
  343. FOP_START(op) \
  344. FOPNOP() \
  345. FOP2E(op##w, ax, dx) \
  346. FOP2E(op##l, eax, edx) \
  347. ON64(FOP2E(op##q, rax, rdx)) \
  348. FOP_END
  349. /* 2 operand, src is CL */
  350. #define FASTOP2CL(op) \
  351. FOP_START(op) \
  352. FOP2E(op##b, al, cl) \
  353. FOP2E(op##w, ax, cl) \
  354. FOP2E(op##l, eax, cl) \
  355. ON64(FOP2E(op##q, rax, cl)) \
  356. FOP_END
  357. /* 2 operand, src and dest are reversed */
  358. #define FASTOP2R(op, name) \
  359. FOP_START(name) \
  360. FOP2E(op##b, dl, al) \
  361. FOP2E(op##w, dx, ax) \
  362. FOP2E(op##l, edx, eax) \
  363. ON64(FOP2E(op##q, rdx, rax)) \
  364. FOP_END
  365. #define FOP3E(op, dst, src, src2) \
  366. FOP_FUNC(#op "_" #dst "_" #src "_" #src2) \
  367. #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET
  368. /* 3-operand, word-only, src2=cl */
  369. #define FASTOP3WCL(op) \
  370. FOP_START(op) \
  371. FOPNOP() \
  372. FOP3E(op##w, ax, dx, cl) \
  373. FOP3E(op##l, eax, edx, cl) \
  374. ON64(FOP3E(op##q, rax, rdx, cl)) \
  375. FOP_END
  376. /* Special case for SETcc - 1 instruction per cc */
  377. #define FOP_SETCC(op) \
  378. ".align 4 \n\t" \
  379. ".type " #op ", @function \n\t" \
  380. #op ": \n\t" \
  381. #op " %al \n\t" \
  382. FOP_RET
  383. asm(".pushsection .fixup, \"ax\"\n"
  384. ".global kvm_fastop_exception \n"
  385. "kvm_fastop_exception: xor %esi, %esi; ret\n"
  386. ".popsection");
  387. FOP_START(setcc)
  388. FOP_SETCC(seto)
  389. FOP_SETCC(setno)
  390. FOP_SETCC(setc)
  391. FOP_SETCC(setnc)
  392. FOP_SETCC(setz)
  393. FOP_SETCC(setnz)
  394. FOP_SETCC(setbe)
  395. FOP_SETCC(setnbe)
  396. FOP_SETCC(sets)
  397. FOP_SETCC(setns)
  398. FOP_SETCC(setp)
  399. FOP_SETCC(setnp)
  400. FOP_SETCC(setl)
  401. FOP_SETCC(setnl)
  402. FOP_SETCC(setle)
  403. FOP_SETCC(setnle)
  404. FOP_END;
  405. FOP_START(salc) "pushf; sbb %al, %al; popf \n\t" FOP_RET
  406. FOP_END;
  407. /*
  408. * XXX: inoutclob user must know where the argument is being expanded.
  409. * Relying on CONFIG_CC_HAS_ASM_GOTO would allow us to remove _fault.
  410. */
  411. #define asm_safe(insn, inoutclob...) \
  412. ({ \
  413. int _fault = 0; \
  414. \
  415. asm volatile("1:" insn "\n" \
  416. "2:\n" \
  417. ".pushsection .fixup, \"ax\"\n" \
  418. "3: movl $1, %[_fault]\n" \
  419. " jmp 2b\n" \
  420. ".popsection\n" \
  421. _ASM_EXTABLE(1b, 3b) \
  422. : [_fault] "+qm"(_fault) inoutclob ); \
  423. \
  424. _fault ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; \
  425. })
  426. static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
  427. enum x86_intercept intercept,
  428. enum x86_intercept_stage stage)
  429. {
  430. struct x86_instruction_info info = {
  431. .intercept = intercept,
  432. .rep_prefix = ctxt->rep_prefix,
  433. .modrm_mod = ctxt->modrm_mod,
  434. .modrm_reg = ctxt->modrm_reg,
  435. .modrm_rm = ctxt->modrm_rm,
  436. .src_val = ctxt->src.val64,
  437. .dst_val = ctxt->dst.val64,
  438. .src_bytes = ctxt->src.bytes,
  439. .dst_bytes = ctxt->dst.bytes,
  440. .ad_bytes = ctxt->ad_bytes,
  441. .next_rip = ctxt->eip,
  442. };
  443. return ctxt->ops->intercept(ctxt, &info, stage);
  444. }
  445. static void assign_masked(ulong *dest, ulong src, ulong mask)
  446. {
  447. *dest = (*dest & ~mask) | (src & mask);
  448. }
  449. static void assign_register(unsigned long *reg, u64 val, int bytes)
  450. {
  451. /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
  452. switch (bytes) {
  453. case 1:
  454. *(u8 *)reg = (u8)val;
  455. break;
  456. case 2:
  457. *(u16 *)reg = (u16)val;
  458. break;
  459. case 4:
  460. *reg = (u32)val;
  461. break; /* 64b: zero-extend */
  462. case 8:
  463. *reg = val;
  464. break;
  465. }
  466. }
  467. static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt)
  468. {
  469. return (1UL << (ctxt->ad_bytes << 3)) - 1;
  470. }
  471. static ulong stack_mask(struct x86_emulate_ctxt *ctxt)
  472. {
  473. u16 sel;
  474. struct desc_struct ss;
  475. if (ctxt->mode == X86EMUL_MODE_PROT64)
  476. return ~0UL;
  477. ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS);
  478. return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */
  479. }
  480. static int stack_size(struct x86_emulate_ctxt *ctxt)
  481. {
  482. return (__fls(stack_mask(ctxt)) + 1) >> 3;
  483. }
  484. /* Access/update address held in a register, based on addressing mode. */
  485. static inline unsigned long
  486. address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg)
  487. {
  488. if (ctxt->ad_bytes == sizeof(unsigned long))
  489. return reg;
  490. else
  491. return reg & ad_mask(ctxt);
  492. }
  493. static inline unsigned long
  494. register_address(struct x86_emulate_ctxt *ctxt, int reg)
  495. {
  496. return address_mask(ctxt, reg_read(ctxt, reg));
  497. }
  498. static void masked_increment(ulong *reg, ulong mask, int inc)
  499. {
  500. assign_masked(reg, *reg + inc, mask);
  501. }
  502. static inline void
  503. register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc)
  504. {
  505. ulong *preg = reg_rmw(ctxt, reg);
  506. assign_register(preg, *preg + inc, ctxt->ad_bytes);
  507. }
  508. static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc)
  509. {
  510. masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc);
  511. }
  512. static u32 desc_limit_scaled(struct desc_struct *desc)
  513. {
  514. u32 limit = get_desc_limit(desc);
  515. return desc->g ? (limit << 12) | 0xfff : limit;
  516. }
  517. static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
  518. {
  519. if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
  520. return 0;
  521. return ctxt->ops->get_cached_segment_base(ctxt, seg);
  522. }
  523. static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
  524. u32 error, bool valid)
  525. {
  526. WARN_ON(vec > 0x1f);
  527. ctxt->exception.vector = vec;
  528. ctxt->exception.error_code = error;
  529. ctxt->exception.error_code_valid = valid;
  530. return X86EMUL_PROPAGATE_FAULT;
  531. }
  532. static int emulate_db(struct x86_emulate_ctxt *ctxt)
  533. {
  534. return emulate_exception(ctxt, DB_VECTOR, 0, false);
  535. }
  536. static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)
  537. {
  538. return emulate_exception(ctxt, GP_VECTOR, err, true);
  539. }
  540. static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err)
  541. {
  542. return emulate_exception(ctxt, SS_VECTOR, err, true);
  543. }
  544. static int emulate_ud(struct x86_emulate_ctxt *ctxt)
  545. {
  546. return emulate_exception(ctxt, UD_VECTOR, 0, false);
  547. }
  548. static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err)
  549. {
  550. return emulate_exception(ctxt, TS_VECTOR, err, true);
  551. }
  552. static int emulate_de(struct x86_emulate_ctxt *ctxt)
  553. {
  554. return emulate_exception(ctxt, DE_VECTOR, 0, false);
  555. }
  556. static int emulate_nm(struct x86_emulate_ctxt *ctxt)
  557. {
  558. return emulate_exception(ctxt, NM_VECTOR, 0, false);
  559. }
  560. static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg)
  561. {
  562. u16 selector;
  563. struct desc_struct desc;
  564. ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg);
  565. return selector;
  566. }
  567. static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
  568. unsigned seg)
  569. {
  570. u16 dummy;
  571. u32 base3;
  572. struct desc_struct desc;
  573. ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg);
  574. ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
  575. }
  576. /*
  577. * x86 defines three classes of vector instructions: explicitly
  578. * aligned, explicitly unaligned, and the rest, which change behaviour
  579. * depending on whether they're AVX encoded or not.
  580. *
  581. * Also included is CMPXCHG16B which is not a vector instruction, yet it is
  582. * subject to the same check. FXSAVE and FXRSTOR are checked here too as their
  583. * 512 bytes of data must be aligned to a 16 byte boundary.
  584. */
  585. static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
  586. {
  587. u64 alignment = ctxt->d & AlignMask;
  588. if (likely(size < 16))
  589. return 1;
  590. switch (alignment) {
  591. case Unaligned:
  592. case Avx:
  593. return 1;
  594. case Aligned16:
  595. return 16;
  596. case Aligned:
  597. default:
  598. return size;
  599. }
  600. }
  601. static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
  602. struct segmented_address addr,
  603. unsigned *max_size, unsigned size,
  604. bool write, bool fetch,
  605. enum x86emul_mode mode, ulong *linear)
  606. {
  607. struct desc_struct desc;
  608. bool usable;
  609. ulong la;
  610. u32 lim;
  611. u16 sel;
  612. u8 va_bits;
  613. la = seg_base(ctxt, addr.seg) + addr.ea;
  614. *max_size = 0;
  615. switch (mode) {
  616. case X86EMUL_MODE_PROT64:
  617. *linear = la;
  618. va_bits = ctxt_virt_addr_bits(ctxt);
  619. if (get_canonical(la, va_bits) != la)
  620. goto bad;
  621. *max_size = min_t(u64, ~0u, (1ull << va_bits) - la);
  622. if (size > *max_size)
  623. goto bad;
  624. break;
  625. default:
  626. *linear = la = (u32)la;
  627. usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL,
  628. addr.seg);
  629. if (!usable)
  630. goto bad;
  631. /* code segment in protected mode or read-only data segment */
  632. if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8))
  633. || !(desc.type & 2)) && write)
  634. goto bad;
  635. /* unreadable code segment */
  636. if (!fetch && (desc.type & 8) && !(desc.type & 2))
  637. goto bad;
  638. lim = desc_limit_scaled(&desc);
  639. if (!(desc.type & 8) && (desc.type & 4)) {
  640. /* expand-down segment */
  641. if (addr.ea <= lim)
  642. goto bad;
  643. lim = desc.d ? 0xffffffff : 0xffff;
  644. }
  645. if (addr.ea > lim)
  646. goto bad;
  647. if (lim == 0xffffffff)
  648. *max_size = ~0u;
  649. else {
  650. *max_size = (u64)lim + 1 - addr.ea;
  651. if (size > *max_size)
  652. goto bad;
  653. }
  654. break;
  655. }
  656. if (la & (insn_alignment(ctxt, size) - 1))
  657. return emulate_gp(ctxt, 0);
  658. return X86EMUL_CONTINUE;
  659. bad:
  660. if (addr.seg == VCPU_SREG_SS)
  661. return emulate_ss(ctxt, 0);
  662. else
  663. return emulate_gp(ctxt, 0);
  664. }
  665. static int linearize(struct x86_emulate_ctxt *ctxt,
  666. struct segmented_address addr,
  667. unsigned size, bool write,
  668. ulong *linear)
  669. {
  670. unsigned max_size;
  671. return __linearize(ctxt, addr, &max_size, size, write, false,
  672. ctxt->mode, linear);
  673. }
  674. static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst,
  675. enum x86emul_mode mode)
  676. {
  677. ulong linear;
  678. int rc;
  679. unsigned max_size;
  680. struct segmented_address addr = { .seg = VCPU_SREG_CS,
  681. .ea = dst };
  682. if (ctxt->op_bytes != sizeof(unsigned long))
  683. addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
  684. rc = __linearize(ctxt, addr, &max_size, 1, false, true, mode, &linear);
  685. if (rc == X86EMUL_CONTINUE)
  686. ctxt->_eip = addr.ea;
  687. return rc;
  688. }
  689. static inline int assign_eip_near(struct x86_emulate_ctxt *ctxt, ulong dst)
  690. {
  691. return assign_eip(ctxt, dst, ctxt->mode);
  692. }
  693. static int assign_eip_far(struct x86_emulate_ctxt *ctxt, ulong dst,
  694. const struct desc_struct *cs_desc)
  695. {
  696. enum x86emul_mode mode = ctxt->mode;
  697. int rc;
  698. #ifdef CONFIG_X86_64
  699. if (ctxt->mode >= X86EMUL_MODE_PROT16) {
  700. if (cs_desc->l) {
  701. u64 efer = 0;
  702. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  703. if (efer & EFER_LMA)
  704. mode = X86EMUL_MODE_PROT64;
  705. } else
  706. mode = X86EMUL_MODE_PROT32; /* temporary value */
  707. }
  708. #endif
  709. if (mode == X86EMUL_MODE_PROT16 || mode == X86EMUL_MODE_PROT32)
  710. mode = cs_desc->d ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  711. rc = assign_eip(ctxt, dst, mode);
  712. if (rc == X86EMUL_CONTINUE)
  713. ctxt->mode = mode;
  714. return rc;
  715. }
  716. static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
  717. {
  718. return assign_eip_near(ctxt, ctxt->_eip + rel);
  719. }
  720. static int linear_read_system(struct x86_emulate_ctxt *ctxt, ulong linear,
  721. void *data, unsigned size)
  722. {
  723. return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, true);
  724. }
  725. static int linear_write_system(struct x86_emulate_ctxt *ctxt,
  726. ulong linear, void *data,
  727. unsigned int size)
  728. {
  729. return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, true);
  730. }
  731. static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
  732. struct segmented_address addr,
  733. void *data,
  734. unsigned size)
  735. {
  736. int rc;
  737. ulong linear;
  738. rc = linearize(ctxt, addr, size, false, &linear);
  739. if (rc != X86EMUL_CONTINUE)
  740. return rc;
  741. return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, false);
  742. }
  743. static int segmented_write_std(struct x86_emulate_ctxt *ctxt,
  744. struct segmented_address addr,
  745. void *data,
  746. unsigned int size)
  747. {
  748. int rc;
  749. ulong linear;
  750. rc = linearize(ctxt, addr, size, true, &linear);
  751. if (rc != X86EMUL_CONTINUE)
  752. return rc;
  753. return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, false);
  754. }
  755. /*
  756. * Prefetch the remaining bytes of the instruction without crossing page
  757. * boundary if they are not in fetch_cache yet.
  758. */
  759. static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
  760. {
  761. int rc;
  762. unsigned size, max_size;
  763. unsigned long linear;
  764. int cur_size = ctxt->fetch.end - ctxt->fetch.data;
  765. struct segmented_address addr = { .seg = VCPU_SREG_CS,
  766. .ea = ctxt->eip + cur_size };
  767. /*
  768. * We do not know exactly how many bytes will be needed, and
  769. * __linearize is expensive, so fetch as much as possible. We
  770. * just have to avoid going beyond the 15 byte limit, the end
  771. * of the segment, or the end of the page.
  772. *
  773. * __linearize is called with size 0 so that it does not do any
  774. * boundary check itself. Instead, we use max_size to check
  775. * against op_size.
  776. */
  777. rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
  778. &linear);
  779. if (unlikely(rc != X86EMUL_CONTINUE))
  780. return rc;
  781. size = min_t(unsigned, 15UL ^ cur_size, max_size);
  782. size = min_t(unsigned, size, PAGE_SIZE - offset_in_page(linear));
  783. /*
  784. * One instruction can only straddle two pages,
  785. * and one has been loaded at the beginning of
  786. * x86_decode_insn. So, if not enough bytes
  787. * still, we must have hit the 15-byte boundary.
  788. */
  789. if (unlikely(size < op_size))
  790. return emulate_gp(ctxt, 0);
  791. rc = ctxt->ops->fetch(ctxt, linear, ctxt->fetch.end,
  792. size, &ctxt->exception);
  793. if (unlikely(rc != X86EMUL_CONTINUE))
  794. return rc;
  795. ctxt->fetch.end += size;
  796. return X86EMUL_CONTINUE;
  797. }
  798. static __always_inline int do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt,
  799. unsigned size)
  800. {
  801. unsigned done_size = ctxt->fetch.end - ctxt->fetch.ptr;
  802. if (unlikely(done_size < size))
  803. return __do_insn_fetch_bytes(ctxt, size - done_size);
  804. else
  805. return X86EMUL_CONTINUE;
  806. }
  807. /* Fetch next part of the instruction being emulated. */
  808. #define insn_fetch(_type, _ctxt) \
  809. ({ _type _x; \
  810. \
  811. rc = do_insn_fetch_bytes(_ctxt, sizeof(_type)); \
  812. if (rc != X86EMUL_CONTINUE) \
  813. goto done; \
  814. ctxt->_eip += sizeof(_type); \
  815. memcpy(&_x, ctxt->fetch.ptr, sizeof(_type)); \
  816. ctxt->fetch.ptr += sizeof(_type); \
  817. _x; \
  818. })
  819. #define insn_fetch_arr(_arr, _size, _ctxt) \
  820. ({ \
  821. rc = do_insn_fetch_bytes(_ctxt, _size); \
  822. if (rc != X86EMUL_CONTINUE) \
  823. goto done; \
  824. ctxt->_eip += (_size); \
  825. memcpy(_arr, ctxt->fetch.ptr, _size); \
  826. ctxt->fetch.ptr += (_size); \
  827. })
  828. /*
  829. * Given the 'reg' portion of a ModRM byte, and a register block, return a
  830. * pointer into the block that addresses the relevant register.
  831. * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
  832. */
  833. static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
  834. int byteop)
  835. {
  836. void *p;
  837. int highbyte_regs = (ctxt->rex_prefix == 0) && byteop;
  838. if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8)
  839. p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1;
  840. else
  841. p = reg_rmw(ctxt, modrm_reg);
  842. return p;
  843. }
  844. static int read_descriptor(struct x86_emulate_ctxt *ctxt,
  845. struct segmented_address addr,
  846. u16 *size, unsigned long *address, int op_bytes)
  847. {
  848. int rc;
  849. if (op_bytes == 2)
  850. op_bytes = 3;
  851. *address = 0;
  852. rc = segmented_read_std(ctxt, addr, size, 2);
  853. if (rc != X86EMUL_CONTINUE)
  854. return rc;
  855. addr.ea += 2;
  856. rc = segmented_read_std(ctxt, addr, address, op_bytes);
  857. return rc;
  858. }
  859. FASTOP2(add);
  860. FASTOP2(or);
  861. FASTOP2(adc);
  862. FASTOP2(sbb);
  863. FASTOP2(and);
  864. FASTOP2(sub);
  865. FASTOP2(xor);
  866. FASTOP2(cmp);
  867. FASTOP2(test);
  868. FASTOP1SRC2(mul, mul_ex);
  869. FASTOP1SRC2(imul, imul_ex);
  870. FASTOP1SRC2EX(div, div_ex);
  871. FASTOP1SRC2EX(idiv, idiv_ex);
  872. FASTOP3WCL(shld);
  873. FASTOP3WCL(shrd);
  874. FASTOP2W(imul);
  875. FASTOP1(not);
  876. FASTOP1(neg);
  877. FASTOP1(inc);
  878. FASTOP1(dec);
  879. FASTOP2CL(rol);
  880. FASTOP2CL(ror);
  881. FASTOP2CL(rcl);
  882. FASTOP2CL(rcr);
  883. FASTOP2CL(shl);
  884. FASTOP2CL(shr);
  885. FASTOP2CL(sar);
  886. FASTOP2W(bsf);
  887. FASTOP2W(bsr);
  888. FASTOP2W(bt);
  889. FASTOP2W(bts);
  890. FASTOP2W(btr);
  891. FASTOP2W(btc);
  892. FASTOP2(xadd);
  893. FASTOP2R(cmp, cmp_r);
  894. static int em_bsf_c(struct x86_emulate_ctxt *ctxt)
  895. {
  896. /* If src is zero, do not writeback, but update flags */
  897. if (ctxt->src.val == 0)
  898. ctxt->dst.type = OP_NONE;
  899. return fastop(ctxt, em_bsf);
  900. }
  901. static int em_bsr_c(struct x86_emulate_ctxt *ctxt)
  902. {
  903. /* If src is zero, do not writeback, but update flags */
  904. if (ctxt->src.val == 0)
  905. ctxt->dst.type = OP_NONE;
  906. return fastop(ctxt, em_bsr);
  907. }
  908. static __always_inline u8 test_cc(unsigned int condition, unsigned long flags)
  909. {
  910. u8 rc;
  911. void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf);
  912. flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF;
  913. asm("push %[flags]; popf; " CALL_NOSPEC
  914. : "=a"(rc) : [thunk_target]"r"(fop), [flags]"r"(flags));
  915. return rc;
  916. }
  917. static void fetch_register_operand(struct operand *op)
  918. {
  919. switch (op->bytes) {
  920. case 1:
  921. op->val = *(u8 *)op->addr.reg;
  922. break;
  923. case 2:
  924. op->val = *(u16 *)op->addr.reg;
  925. break;
  926. case 4:
  927. op->val = *(u32 *)op->addr.reg;
  928. break;
  929. case 8:
  930. op->val = *(u64 *)op->addr.reg;
  931. break;
  932. }
  933. }
  934. static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg)
  935. {
  936. switch (reg) {
  937. case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break;
  938. case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break;
  939. case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break;
  940. case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break;
  941. case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break;
  942. case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break;
  943. case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break;
  944. case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break;
  945. #ifdef CONFIG_X86_64
  946. case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break;
  947. case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break;
  948. case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break;
  949. case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break;
  950. case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break;
  951. case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break;
  952. case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break;
  953. case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break;
  954. #endif
  955. default: BUG();
  956. }
  957. }
  958. static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data,
  959. int reg)
  960. {
  961. switch (reg) {
  962. case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break;
  963. case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break;
  964. case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break;
  965. case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break;
  966. case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break;
  967. case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break;
  968. case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break;
  969. case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break;
  970. #ifdef CONFIG_X86_64
  971. case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break;
  972. case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break;
  973. case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break;
  974. case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break;
  975. case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break;
  976. case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break;
  977. case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break;
  978. case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break;
  979. #endif
  980. default: BUG();
  981. }
  982. }
  983. static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
  984. {
  985. switch (reg) {
  986. case 0: asm("movq %%mm0, %0" : "=m"(*data)); break;
  987. case 1: asm("movq %%mm1, %0" : "=m"(*data)); break;
  988. case 2: asm("movq %%mm2, %0" : "=m"(*data)); break;
  989. case 3: asm("movq %%mm3, %0" : "=m"(*data)); break;
  990. case 4: asm("movq %%mm4, %0" : "=m"(*data)); break;
  991. case 5: asm("movq %%mm5, %0" : "=m"(*data)); break;
  992. case 6: asm("movq %%mm6, %0" : "=m"(*data)); break;
  993. case 7: asm("movq %%mm7, %0" : "=m"(*data)); break;
  994. default: BUG();
  995. }
  996. }
  997. static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg)
  998. {
  999. switch (reg) {
  1000. case 0: asm("movq %0, %%mm0" : : "m"(*data)); break;
  1001. case 1: asm("movq %0, %%mm1" : : "m"(*data)); break;
  1002. case 2: asm("movq %0, %%mm2" : : "m"(*data)); break;
  1003. case 3: asm("movq %0, %%mm3" : : "m"(*data)); break;
  1004. case 4: asm("movq %0, %%mm4" : : "m"(*data)); break;
  1005. case 5: asm("movq %0, %%mm5" : : "m"(*data)); break;
  1006. case 6: asm("movq %0, %%mm6" : : "m"(*data)); break;
  1007. case 7: asm("movq %0, %%mm7" : : "m"(*data)); break;
  1008. default: BUG();
  1009. }
  1010. }
  1011. static int em_fninit(struct x86_emulate_ctxt *ctxt)
  1012. {
  1013. if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
  1014. return emulate_nm(ctxt);
  1015. asm volatile("fninit");
  1016. return X86EMUL_CONTINUE;
  1017. }
  1018. static int em_fnstcw(struct x86_emulate_ctxt *ctxt)
  1019. {
  1020. u16 fcw;
  1021. if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
  1022. return emulate_nm(ctxt);
  1023. asm volatile("fnstcw %0": "+m"(fcw));
  1024. ctxt->dst.val = fcw;
  1025. return X86EMUL_CONTINUE;
  1026. }
  1027. static int em_fnstsw(struct x86_emulate_ctxt *ctxt)
  1028. {
  1029. u16 fsw;
  1030. if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
  1031. return emulate_nm(ctxt);
  1032. asm volatile("fnstsw %0": "+m"(fsw));
  1033. ctxt->dst.val = fsw;
  1034. return X86EMUL_CONTINUE;
  1035. }
  1036. static void decode_register_operand(struct x86_emulate_ctxt *ctxt,
  1037. struct operand *op)
  1038. {
  1039. unsigned reg = ctxt->modrm_reg;
  1040. if (!(ctxt->d & ModRM))
  1041. reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3);
  1042. if (ctxt->d & Sse) {
  1043. op->type = OP_XMM;
  1044. op->bytes = 16;
  1045. op->addr.xmm = reg;
  1046. read_sse_reg(ctxt, &op->vec_val, reg);
  1047. return;
  1048. }
  1049. if (ctxt->d & Mmx) {
  1050. reg &= 7;
  1051. op->type = OP_MM;
  1052. op->bytes = 8;
  1053. op->addr.mm = reg;
  1054. return;
  1055. }
  1056. op->type = OP_REG;
  1057. op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
  1058. op->addr.reg = decode_register(ctxt, reg, ctxt->d & ByteOp);
  1059. fetch_register_operand(op);
  1060. op->orig_val = op->val;
  1061. }
  1062. static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg)
  1063. {
  1064. if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP)
  1065. ctxt->modrm_seg = VCPU_SREG_SS;
  1066. }
  1067. static int decode_modrm(struct x86_emulate_ctxt *ctxt,
  1068. struct operand *op)
  1069. {
  1070. u8 sib;
  1071. int index_reg, base_reg, scale;
  1072. int rc = X86EMUL_CONTINUE;
  1073. ulong modrm_ea = 0;
  1074. ctxt->modrm_reg = ((ctxt->rex_prefix << 1) & 8); /* REX.R */
  1075. index_reg = (ctxt->rex_prefix << 2) & 8; /* REX.X */
  1076. base_reg = (ctxt->rex_prefix << 3) & 8; /* REX.B */
  1077. ctxt->modrm_mod = (ctxt->modrm & 0xc0) >> 6;
  1078. ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3;
  1079. ctxt->modrm_rm = base_reg | (ctxt->modrm & 0x07);
  1080. ctxt->modrm_seg = VCPU_SREG_DS;
  1081. if (ctxt->modrm_mod == 3 || (ctxt->d & NoMod)) {
  1082. op->type = OP_REG;
  1083. op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
  1084. op->addr.reg = decode_register(ctxt, ctxt->modrm_rm,
  1085. ctxt->d & ByteOp);
  1086. if (ctxt->d & Sse) {
  1087. op->type = OP_XMM;
  1088. op->bytes = 16;
  1089. op->addr.xmm = ctxt->modrm_rm;
  1090. read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm);
  1091. return rc;
  1092. }
  1093. if (ctxt->d & Mmx) {
  1094. op->type = OP_MM;
  1095. op->bytes = 8;
  1096. op->addr.mm = ctxt->modrm_rm & 7;
  1097. return rc;
  1098. }
  1099. fetch_register_operand(op);
  1100. return rc;
  1101. }
  1102. op->type = OP_MEM;
  1103. if (ctxt->ad_bytes == 2) {
  1104. unsigned bx = reg_read(ctxt, VCPU_REGS_RBX);
  1105. unsigned bp = reg_read(ctxt, VCPU_REGS_RBP);
  1106. unsigned si = reg_read(ctxt, VCPU_REGS_RSI);
  1107. unsigned di = reg_read(ctxt, VCPU_REGS_RDI);
  1108. /* 16-bit ModR/M decode. */
  1109. switch (ctxt->modrm_mod) {
  1110. case 0:
  1111. if (ctxt->modrm_rm == 6)
  1112. modrm_ea += insn_fetch(u16, ctxt);
  1113. break;
  1114. case 1:
  1115. modrm_ea += insn_fetch(s8, ctxt);
  1116. break;
  1117. case 2:
  1118. modrm_ea += insn_fetch(u16, ctxt);
  1119. break;
  1120. }
  1121. switch (ctxt->modrm_rm) {
  1122. case 0:
  1123. modrm_ea += bx + si;
  1124. break;
  1125. case 1:
  1126. modrm_ea += bx + di;
  1127. break;
  1128. case 2:
  1129. modrm_ea += bp + si;
  1130. break;
  1131. case 3:
  1132. modrm_ea += bp + di;
  1133. break;
  1134. case 4:
  1135. modrm_ea += si;
  1136. break;
  1137. case 5:
  1138. modrm_ea += di;
  1139. break;
  1140. case 6:
  1141. if (ctxt->modrm_mod != 0)
  1142. modrm_ea += bp;
  1143. break;
  1144. case 7:
  1145. modrm_ea += bx;
  1146. break;
  1147. }
  1148. if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 ||
  1149. (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0))
  1150. ctxt->modrm_seg = VCPU_SREG_SS;
  1151. modrm_ea = (u16)modrm_ea;
  1152. } else {
  1153. /* 32/64-bit ModR/M decode. */
  1154. if ((ctxt->modrm_rm & 7) == 4) {
  1155. sib = insn_fetch(u8, ctxt);
  1156. index_reg |= (sib >> 3) & 7;
  1157. base_reg |= sib & 7;
  1158. scale = sib >> 6;
  1159. if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0)
  1160. modrm_ea += insn_fetch(s32, ctxt);
  1161. else {
  1162. modrm_ea += reg_read(ctxt, base_reg);
  1163. adjust_modrm_seg(ctxt, base_reg);
  1164. /* Increment ESP on POP [ESP] */
  1165. if ((ctxt->d & IncSP) &&
  1166. base_reg == VCPU_REGS_RSP)
  1167. modrm_ea += ctxt->op_bytes;
  1168. }
  1169. if (index_reg != 4)
  1170. modrm_ea += reg_read(ctxt, index_reg) << scale;
  1171. } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) {
  1172. modrm_ea += insn_fetch(s32, ctxt);
  1173. if (ctxt->mode == X86EMUL_MODE_PROT64)
  1174. ctxt->rip_relative = 1;
  1175. } else {
  1176. base_reg = ctxt->modrm_rm;
  1177. modrm_ea += reg_read(ctxt, base_reg);
  1178. adjust_modrm_seg(ctxt, base_reg);
  1179. }
  1180. switch (ctxt->modrm_mod) {
  1181. case 1:
  1182. modrm_ea += insn_fetch(s8, ctxt);
  1183. break;
  1184. case 2:
  1185. modrm_ea += insn_fetch(s32, ctxt);
  1186. break;
  1187. }
  1188. }
  1189. op->addr.mem.ea = modrm_ea;
  1190. if (ctxt->ad_bytes != 8)
  1191. ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea;
  1192. done:
  1193. return rc;
  1194. }
  1195. static int decode_abs(struct x86_emulate_ctxt *ctxt,
  1196. struct operand *op)
  1197. {
  1198. int rc = X86EMUL_CONTINUE;
  1199. op->type = OP_MEM;
  1200. switch (ctxt->ad_bytes) {
  1201. case 2:
  1202. op->addr.mem.ea = insn_fetch(u16, ctxt);
  1203. break;
  1204. case 4:
  1205. op->addr.mem.ea = insn_fetch(u32, ctxt);
  1206. break;
  1207. case 8:
  1208. op->addr.mem.ea = insn_fetch(u64, ctxt);
  1209. break;
  1210. }
  1211. done:
  1212. return rc;
  1213. }
  1214. static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt)
  1215. {
  1216. long sv = 0, mask;
  1217. if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) {
  1218. mask = ~((long)ctxt->dst.bytes * 8 - 1);
  1219. if (ctxt->src.bytes == 2)
  1220. sv = (s16)ctxt->src.val & (s16)mask;
  1221. else if (ctxt->src.bytes == 4)
  1222. sv = (s32)ctxt->src.val & (s32)mask;
  1223. else
  1224. sv = (s64)ctxt->src.val & (s64)mask;
  1225. ctxt->dst.addr.mem.ea = address_mask(ctxt,
  1226. ctxt->dst.addr.mem.ea + (sv >> 3));
  1227. }
  1228. /* only subword offset */
  1229. ctxt->src.val &= (ctxt->dst.bytes << 3) - 1;
  1230. }
  1231. static int read_emulated(struct x86_emulate_ctxt *ctxt,
  1232. unsigned long addr, void *dest, unsigned size)
  1233. {
  1234. int rc;
  1235. struct read_cache *mc = &ctxt->mem_read;
  1236. if (mc->pos < mc->end)
  1237. goto read_cached;
  1238. WARN_ON((mc->end + size) >= sizeof(mc->data));
  1239. rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size,
  1240. &ctxt->exception);
  1241. if (rc != X86EMUL_CONTINUE)
  1242. return rc;
  1243. mc->end += size;
  1244. read_cached:
  1245. memcpy(dest, mc->data + mc->pos, size);
  1246. mc->pos += size;
  1247. return X86EMUL_CONTINUE;
  1248. }
  1249. static int segmented_read(struct x86_emulate_ctxt *ctxt,
  1250. struct segmented_address addr,
  1251. void *data,
  1252. unsigned size)
  1253. {
  1254. int rc;
  1255. ulong linear;
  1256. rc = linearize(ctxt, addr, size, false, &linear);
  1257. if (rc != X86EMUL_CONTINUE)
  1258. return rc;
  1259. return read_emulated(ctxt, linear, data, size);
  1260. }
  1261. static int segmented_write(struct x86_emulate_ctxt *ctxt,
  1262. struct segmented_address addr,
  1263. const void *data,
  1264. unsigned size)
  1265. {
  1266. int rc;
  1267. ulong linear;
  1268. rc = linearize(ctxt, addr, size, true, &linear);
  1269. if (rc != X86EMUL_CONTINUE)
  1270. return rc;
  1271. return ctxt->ops->write_emulated(ctxt, linear, data, size,
  1272. &ctxt->exception);
  1273. }
  1274. static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,
  1275. struct segmented_address addr,
  1276. const void *orig_data, const void *data,
  1277. unsigned size)
  1278. {
  1279. int rc;
  1280. ulong linear;
  1281. rc = linearize(ctxt, addr, size, true, &linear);
  1282. if (rc != X86EMUL_CONTINUE)
  1283. return rc;
  1284. return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data,
  1285. size, &ctxt->exception);
  1286. }
  1287. static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
  1288. unsigned int size, unsigned short port,
  1289. void *dest)
  1290. {
  1291. struct read_cache *rc = &ctxt->io_read;
  1292. if (rc->pos == rc->end) { /* refill pio read ahead */
  1293. unsigned int in_page, n;
  1294. unsigned int count = ctxt->rep_prefix ?
  1295. address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1;
  1296. in_page = (ctxt->eflags & X86_EFLAGS_DF) ?
  1297. offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) :
  1298. PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI));
  1299. n = min3(in_page, (unsigned int)sizeof(rc->data) / size, count);
  1300. if (n == 0)
  1301. n = 1;
  1302. rc->pos = rc->end = 0;
  1303. if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n))
  1304. return 0;
  1305. rc->end = n * size;
  1306. }
  1307. if (ctxt->rep_prefix && (ctxt->d & String) &&
  1308. !(ctxt->eflags & X86_EFLAGS_DF)) {
  1309. ctxt->dst.data = rc->data + rc->pos;
  1310. ctxt->dst.type = OP_MEM_STR;
  1311. ctxt->dst.count = (rc->end - rc->pos) / size;
  1312. rc->pos = rc->end;
  1313. } else {
  1314. memcpy(dest, rc->data + rc->pos, size);
  1315. rc->pos += size;
  1316. }
  1317. return 1;
  1318. }
  1319. static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt,
  1320. u16 index, struct desc_struct *desc)
  1321. {
  1322. struct desc_ptr dt;
  1323. ulong addr;
  1324. ctxt->ops->get_idt(ctxt, &dt);
  1325. if (dt.size < index * 8 + 7)
  1326. return emulate_gp(ctxt, index << 3 | 0x2);
  1327. addr = dt.address + index * 8;
  1328. return linear_read_system(ctxt, addr, desc, sizeof *desc);
  1329. }
  1330. static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt,
  1331. u16 selector, struct desc_ptr *dt)
  1332. {
  1333. const struct x86_emulate_ops *ops = ctxt->ops;
  1334. u32 base3 = 0;
  1335. if (selector & 1 << 2) {
  1336. struct desc_struct desc;
  1337. u16 sel;
  1338. memset (dt, 0, sizeof *dt);
  1339. if (!ops->get_segment(ctxt, &sel, &desc, &base3,
  1340. VCPU_SREG_LDTR))
  1341. return;
  1342. dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */
  1343. dt->address = get_desc_base(&desc) | ((u64)base3 << 32);
  1344. } else
  1345. ops->get_gdt(ctxt, dt);
  1346. }
  1347. static int get_descriptor_ptr(struct x86_emulate_ctxt *ctxt,
  1348. u16 selector, ulong *desc_addr_p)
  1349. {
  1350. struct desc_ptr dt;
  1351. u16 index = selector >> 3;
  1352. ulong addr;
  1353. get_descriptor_table_ptr(ctxt, selector, &dt);
  1354. if (dt.size < index * 8 + 7)
  1355. return emulate_gp(ctxt, selector & 0xfffc);
  1356. addr = dt.address + index * 8;
  1357. #ifdef CONFIG_X86_64
  1358. if (addr >> 32 != 0) {
  1359. u64 efer = 0;
  1360. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  1361. if (!(efer & EFER_LMA))
  1362. addr &= (u32)-1;
  1363. }
  1364. #endif
  1365. *desc_addr_p = addr;
  1366. return X86EMUL_CONTINUE;
  1367. }
  1368. /* allowed just for 8 bytes segments */
  1369. static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt,
  1370. u16 selector, struct desc_struct *desc,
  1371. ulong *desc_addr_p)
  1372. {
  1373. int rc;
  1374. rc = get_descriptor_ptr(ctxt, selector, desc_addr_p);
  1375. if (rc != X86EMUL_CONTINUE)
  1376. return rc;
  1377. return linear_read_system(ctxt, *desc_addr_p, desc, sizeof(*desc));
  1378. }
  1379. /* allowed just for 8 bytes segments */
  1380. static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
  1381. u16 selector, struct desc_struct *desc)
  1382. {
  1383. int rc;
  1384. ulong addr;
  1385. rc = get_descriptor_ptr(ctxt, selector, &addr);
  1386. if (rc != X86EMUL_CONTINUE)
  1387. return rc;
  1388. return linear_write_system(ctxt, addr, desc, sizeof *desc);
  1389. }
  1390. static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
  1391. u16 selector, int seg, u8 cpl,
  1392. enum x86_transfer_type transfer,
  1393. struct desc_struct *desc)
  1394. {
  1395. struct desc_struct seg_desc, old_desc;
  1396. u8 dpl, rpl;
  1397. unsigned err_vec = GP_VECTOR;
  1398. u32 err_code = 0;
  1399. bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
  1400. ulong desc_addr;
  1401. int ret;
  1402. u16 dummy;
  1403. u32 base3 = 0;
  1404. memset(&seg_desc, 0, sizeof seg_desc);
  1405. if (ctxt->mode == X86EMUL_MODE_REAL) {
  1406. /* set real mode segment descriptor (keep limit etc. for
  1407. * unreal mode) */
  1408. ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg);
  1409. set_desc_base(&seg_desc, selector << 4);
  1410. goto load;
  1411. } else if (seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) {
  1412. /* VM86 needs a clean new segment descriptor */
  1413. set_desc_base(&seg_desc, selector << 4);
  1414. set_desc_limit(&seg_desc, 0xffff);
  1415. seg_desc.type = 3;
  1416. seg_desc.p = 1;
  1417. seg_desc.s = 1;
  1418. seg_desc.dpl = 3;
  1419. goto load;
  1420. }
  1421. rpl = selector & 3;
  1422. /* TR should be in GDT only */
  1423. if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
  1424. goto exception;
  1425. /* NULL selector is not valid for TR, CS and (except for long mode) SS */
  1426. if (null_selector) {
  1427. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_TR)
  1428. goto exception;
  1429. if (seg == VCPU_SREG_SS) {
  1430. if (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)
  1431. goto exception;
  1432. /*
  1433. * ctxt->ops->set_segment expects the CPL to be in
  1434. * SS.DPL, so fake an expand-up 32-bit data segment.
  1435. */
  1436. seg_desc.type = 3;
  1437. seg_desc.p = 1;
  1438. seg_desc.s = 1;
  1439. seg_desc.dpl = cpl;
  1440. seg_desc.d = 1;
  1441. seg_desc.g = 1;
  1442. }
  1443. /* Skip all following checks */
  1444. goto load;
  1445. }
  1446. ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr);
  1447. if (ret != X86EMUL_CONTINUE)
  1448. return ret;
  1449. err_code = selector & 0xfffc;
  1450. err_vec = (transfer == X86_TRANSFER_TASK_SWITCH) ? TS_VECTOR :
  1451. GP_VECTOR;
  1452. /* can't load system descriptor into segment selector */
  1453. if (seg <= VCPU_SREG_GS && !seg_desc.s) {
  1454. if (transfer == X86_TRANSFER_CALL_JMP)
  1455. return X86EMUL_UNHANDLEABLE;
  1456. goto exception;
  1457. }
  1458. if (!seg_desc.p) {
  1459. err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
  1460. goto exception;
  1461. }
  1462. dpl = seg_desc.dpl;
  1463. switch (seg) {
  1464. case VCPU_SREG_SS:
  1465. /*
  1466. * segment is not a writable data segment or segment
  1467. * selector's RPL != CPL or segment selector's RPL != CPL
  1468. */
  1469. if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl)
  1470. goto exception;
  1471. break;
  1472. case VCPU_SREG_CS:
  1473. if (!(seg_desc.type & 8))
  1474. goto exception;
  1475. if (seg_desc.type & 4) {
  1476. /* conforming */
  1477. if (dpl > cpl)
  1478. goto exception;
  1479. } else {
  1480. /* nonconforming */
  1481. if (rpl > cpl || dpl != cpl)
  1482. goto exception;
  1483. }
  1484. /* in long-mode d/b must be clear if l is set */
  1485. if (seg_desc.d && seg_desc.l) {
  1486. u64 efer = 0;
  1487. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  1488. if (efer & EFER_LMA)
  1489. goto exception;
  1490. }
  1491. /* CS(RPL) <- CPL */
  1492. selector = (selector & 0xfffc) | cpl;
  1493. break;
  1494. case VCPU_SREG_TR:
  1495. if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9))
  1496. goto exception;
  1497. old_desc = seg_desc;
  1498. seg_desc.type |= 2; /* busy */
  1499. ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc,
  1500. sizeof(seg_desc), &ctxt->exception);
  1501. if (ret != X86EMUL_CONTINUE)
  1502. return ret;
  1503. break;
  1504. case VCPU_SREG_LDTR:
  1505. if (seg_desc.s || seg_desc.type != 2)
  1506. goto exception;
  1507. break;
  1508. default: /* DS, ES, FS, or GS */
  1509. /*
  1510. * segment is not a data or readable code segment or
  1511. * ((segment is a data or nonconforming code segment)
  1512. * and (both RPL and CPL > DPL))
  1513. */
  1514. if ((seg_desc.type & 0xa) == 0x8 ||
  1515. (((seg_desc.type & 0xc) != 0xc) &&
  1516. (rpl > dpl && cpl > dpl)))
  1517. goto exception;
  1518. break;
  1519. }
  1520. if (seg_desc.s) {
  1521. /* mark segment as accessed */
  1522. if (!(seg_desc.type & 1)) {
  1523. seg_desc.type |= 1;
  1524. ret = write_segment_descriptor(ctxt, selector,
  1525. &seg_desc);
  1526. if (ret != X86EMUL_CONTINUE)
  1527. return ret;
  1528. }
  1529. } else if (ctxt->mode == X86EMUL_MODE_PROT64) {
  1530. ret = linear_read_system(ctxt, desc_addr+8, &base3, sizeof(base3));
  1531. if (ret != X86EMUL_CONTINUE)
  1532. return ret;
  1533. if (emul_is_noncanonical_address(get_desc_base(&seg_desc) |
  1534. ((u64)base3 << 32), ctxt))
  1535. return emulate_gp(ctxt, 0);
  1536. }
  1537. load:
  1538. ctxt->ops->set_segment(ctxt, selector, &seg_desc, base3, seg);
  1539. if (desc)
  1540. *desc = seg_desc;
  1541. return X86EMUL_CONTINUE;
  1542. exception:
  1543. return emulate_exception(ctxt, err_vec, err_code, true);
  1544. }
  1545. static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
  1546. u16 selector, int seg)
  1547. {
  1548. u8 cpl = ctxt->ops->cpl(ctxt);
  1549. /*
  1550. * None of MOV, POP and LSS can load a NULL selector in CPL=3, but
  1551. * they can load it at CPL<3 (Intel's manual says only LSS can,
  1552. * but it's wrong).
  1553. *
  1554. * However, the Intel manual says that putting IST=1/DPL=3 in
  1555. * an interrupt gate will result in SS=3 (the AMD manual instead
  1556. * says it doesn't), so allow SS=3 in __load_segment_descriptor
  1557. * and only forbid it here.
  1558. */
  1559. if (seg == VCPU_SREG_SS && selector == 3 &&
  1560. ctxt->mode == X86EMUL_MODE_PROT64)
  1561. return emulate_exception(ctxt, GP_VECTOR, 0, true);
  1562. return __load_segment_descriptor(ctxt, selector, seg, cpl,
  1563. X86_TRANSFER_NONE, NULL);
  1564. }
  1565. static void write_register_operand(struct operand *op)
  1566. {
  1567. return assign_register(op->addr.reg, op->val, op->bytes);
  1568. }
  1569. static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op)
  1570. {
  1571. switch (op->type) {
  1572. case OP_REG:
  1573. write_register_operand(op);
  1574. break;
  1575. case OP_MEM:
  1576. if (ctxt->lock_prefix)
  1577. return segmented_cmpxchg(ctxt,
  1578. op->addr.mem,
  1579. &op->orig_val,
  1580. &op->val,
  1581. op->bytes);
  1582. else
  1583. return segmented_write(ctxt,
  1584. op->addr.mem,
  1585. &op->val,
  1586. op->bytes);
  1587. break;
  1588. case OP_MEM_STR:
  1589. return segmented_write(ctxt,
  1590. op->addr.mem,
  1591. op->data,
  1592. op->bytes * op->count);
  1593. break;
  1594. case OP_XMM:
  1595. write_sse_reg(ctxt, &op->vec_val, op->addr.xmm);
  1596. break;
  1597. case OP_MM:
  1598. write_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
  1599. break;
  1600. case OP_NONE:
  1601. /* no writeback */
  1602. break;
  1603. default:
  1604. break;
  1605. }
  1606. return X86EMUL_CONTINUE;
  1607. }
  1608. static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes)
  1609. {
  1610. struct segmented_address addr;
  1611. rsp_increment(ctxt, -bytes);
  1612. addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
  1613. addr.seg = VCPU_SREG_SS;
  1614. return segmented_write(ctxt, addr, data, bytes);
  1615. }
  1616. static int em_push(struct x86_emulate_ctxt *ctxt)
  1617. {
  1618. /* Disable writeback. */
  1619. ctxt->dst.type = OP_NONE;
  1620. return push(ctxt, &ctxt->src.val, ctxt->op_bytes);
  1621. }
  1622. static int emulate_pop(struct x86_emulate_ctxt *ctxt,
  1623. void *dest, int len)
  1624. {
  1625. int rc;
  1626. struct segmented_address addr;
  1627. addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt);
  1628. addr.seg = VCPU_SREG_SS;
  1629. rc = segmented_read(ctxt, addr, dest, len);
  1630. if (rc != X86EMUL_CONTINUE)
  1631. return rc;
  1632. rsp_increment(ctxt, len);
  1633. return rc;
  1634. }
  1635. static int em_pop(struct x86_emulate_ctxt *ctxt)
  1636. {
  1637. return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes);
  1638. }
  1639. static int emulate_popf(struct x86_emulate_ctxt *ctxt,
  1640. void *dest, int len)
  1641. {
  1642. int rc;
  1643. unsigned long val, change_mask;
  1644. int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
  1645. int cpl = ctxt->ops->cpl(ctxt);
  1646. rc = emulate_pop(ctxt, &val, len);
  1647. if (rc != X86EMUL_CONTINUE)
  1648. return rc;
  1649. change_mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  1650. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF |
  1651. X86_EFLAGS_TF | X86_EFLAGS_DF | X86_EFLAGS_NT |
  1652. X86_EFLAGS_AC | X86_EFLAGS_ID;
  1653. switch(ctxt->mode) {
  1654. case X86EMUL_MODE_PROT64:
  1655. case X86EMUL_MODE_PROT32:
  1656. case X86EMUL_MODE_PROT16:
  1657. if (cpl == 0)
  1658. change_mask |= X86_EFLAGS_IOPL;
  1659. if (cpl <= iopl)
  1660. change_mask |= X86_EFLAGS_IF;
  1661. break;
  1662. case X86EMUL_MODE_VM86:
  1663. if (iopl < 3)
  1664. return emulate_gp(ctxt, 0);
  1665. change_mask |= X86_EFLAGS_IF;
  1666. break;
  1667. default: /* real mode */
  1668. change_mask |= (X86_EFLAGS_IOPL | X86_EFLAGS_IF);
  1669. break;
  1670. }
  1671. *(unsigned long *)dest =
  1672. (ctxt->eflags & ~change_mask) | (val & change_mask);
  1673. return rc;
  1674. }
  1675. static int em_popf(struct x86_emulate_ctxt *ctxt)
  1676. {
  1677. ctxt->dst.type = OP_REG;
  1678. ctxt->dst.addr.reg = &ctxt->eflags;
  1679. ctxt->dst.bytes = ctxt->op_bytes;
  1680. return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes);
  1681. }
  1682. static int em_enter(struct x86_emulate_ctxt *ctxt)
  1683. {
  1684. int rc;
  1685. unsigned frame_size = ctxt->src.val;
  1686. unsigned nesting_level = ctxt->src2.val & 31;
  1687. ulong rbp;
  1688. if (nesting_level)
  1689. return X86EMUL_UNHANDLEABLE;
  1690. rbp = reg_read(ctxt, VCPU_REGS_RBP);
  1691. rc = push(ctxt, &rbp, stack_size(ctxt));
  1692. if (rc != X86EMUL_CONTINUE)
  1693. return rc;
  1694. assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP),
  1695. stack_mask(ctxt));
  1696. assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP),
  1697. reg_read(ctxt, VCPU_REGS_RSP) - frame_size,
  1698. stack_mask(ctxt));
  1699. return X86EMUL_CONTINUE;
  1700. }
  1701. static int em_leave(struct x86_emulate_ctxt *ctxt)
  1702. {
  1703. assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP),
  1704. stack_mask(ctxt));
  1705. return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes);
  1706. }
  1707. static int em_push_sreg(struct x86_emulate_ctxt *ctxt)
  1708. {
  1709. int seg = ctxt->src2.val;
  1710. ctxt->src.val = get_segment_selector(ctxt, seg);
  1711. if (ctxt->op_bytes == 4) {
  1712. rsp_increment(ctxt, -2);
  1713. ctxt->op_bytes = 2;
  1714. }
  1715. return em_push(ctxt);
  1716. }
  1717. static int em_pop_sreg(struct x86_emulate_ctxt *ctxt)
  1718. {
  1719. int seg = ctxt->src2.val;
  1720. unsigned long selector;
  1721. int rc;
  1722. rc = emulate_pop(ctxt, &selector, 2);
  1723. if (rc != X86EMUL_CONTINUE)
  1724. return rc;
  1725. if (ctxt->modrm_reg == VCPU_SREG_SS)
  1726. ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
  1727. if (ctxt->op_bytes > 2)
  1728. rsp_increment(ctxt, ctxt->op_bytes - 2);
  1729. rc = load_segment_descriptor(ctxt, (u16)selector, seg);
  1730. return rc;
  1731. }
  1732. static int em_pusha(struct x86_emulate_ctxt *ctxt)
  1733. {
  1734. unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP);
  1735. int rc = X86EMUL_CONTINUE;
  1736. int reg = VCPU_REGS_RAX;
  1737. while (reg <= VCPU_REGS_RDI) {
  1738. (reg == VCPU_REGS_RSP) ?
  1739. (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg));
  1740. rc = em_push(ctxt);
  1741. if (rc != X86EMUL_CONTINUE)
  1742. return rc;
  1743. ++reg;
  1744. }
  1745. return rc;
  1746. }
  1747. static int em_pushf(struct x86_emulate_ctxt *ctxt)
  1748. {
  1749. ctxt->src.val = (unsigned long)ctxt->eflags & ~X86_EFLAGS_VM;
  1750. return em_push(ctxt);
  1751. }
  1752. static int em_popa(struct x86_emulate_ctxt *ctxt)
  1753. {
  1754. int rc = X86EMUL_CONTINUE;
  1755. int reg = VCPU_REGS_RDI;
  1756. u32 val;
  1757. while (reg >= VCPU_REGS_RAX) {
  1758. if (reg == VCPU_REGS_RSP) {
  1759. rsp_increment(ctxt, ctxt->op_bytes);
  1760. --reg;
  1761. }
  1762. rc = emulate_pop(ctxt, &val, ctxt->op_bytes);
  1763. if (rc != X86EMUL_CONTINUE)
  1764. break;
  1765. assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes);
  1766. --reg;
  1767. }
  1768. return rc;
  1769. }
  1770. static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
  1771. {
  1772. const struct x86_emulate_ops *ops = ctxt->ops;
  1773. int rc;
  1774. struct desc_ptr dt;
  1775. gva_t cs_addr;
  1776. gva_t eip_addr;
  1777. u16 cs, eip;
  1778. /* TODO: Add limit checks */
  1779. ctxt->src.val = ctxt->eflags;
  1780. rc = em_push(ctxt);
  1781. if (rc != X86EMUL_CONTINUE)
  1782. return rc;
  1783. ctxt->eflags &= ~(X86_EFLAGS_IF | X86_EFLAGS_TF | X86_EFLAGS_AC);
  1784. ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS);
  1785. rc = em_push(ctxt);
  1786. if (rc != X86EMUL_CONTINUE)
  1787. return rc;
  1788. ctxt->src.val = ctxt->_eip;
  1789. rc = em_push(ctxt);
  1790. if (rc != X86EMUL_CONTINUE)
  1791. return rc;
  1792. ops->get_idt(ctxt, &dt);
  1793. eip_addr = dt.address + (irq << 2);
  1794. cs_addr = dt.address + (irq << 2) + 2;
  1795. rc = linear_read_system(ctxt, cs_addr, &cs, 2);
  1796. if (rc != X86EMUL_CONTINUE)
  1797. return rc;
  1798. rc = linear_read_system(ctxt, eip_addr, &eip, 2);
  1799. if (rc != X86EMUL_CONTINUE)
  1800. return rc;
  1801. rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS);
  1802. if (rc != X86EMUL_CONTINUE)
  1803. return rc;
  1804. ctxt->_eip = eip;
  1805. return rc;
  1806. }
  1807. int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)
  1808. {
  1809. int rc;
  1810. invalidate_registers(ctxt);
  1811. rc = __emulate_int_real(ctxt, irq);
  1812. if (rc == X86EMUL_CONTINUE)
  1813. writeback_registers(ctxt);
  1814. return rc;
  1815. }
  1816. static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq)
  1817. {
  1818. switch(ctxt->mode) {
  1819. case X86EMUL_MODE_REAL:
  1820. return __emulate_int_real(ctxt, irq);
  1821. case X86EMUL_MODE_VM86:
  1822. case X86EMUL_MODE_PROT16:
  1823. case X86EMUL_MODE_PROT32:
  1824. case X86EMUL_MODE_PROT64:
  1825. default:
  1826. /* Protected mode interrupts unimplemented yet */
  1827. return X86EMUL_UNHANDLEABLE;
  1828. }
  1829. }
  1830. static int emulate_iret_real(struct x86_emulate_ctxt *ctxt)
  1831. {
  1832. int rc = X86EMUL_CONTINUE;
  1833. unsigned long temp_eip = 0;
  1834. unsigned long temp_eflags = 0;
  1835. unsigned long cs = 0;
  1836. unsigned long mask = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  1837. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_TF |
  1838. X86_EFLAGS_IF | X86_EFLAGS_DF | X86_EFLAGS_OF |
  1839. X86_EFLAGS_IOPL | X86_EFLAGS_NT | X86_EFLAGS_RF |
  1840. X86_EFLAGS_AC | X86_EFLAGS_ID |
  1841. X86_EFLAGS_FIXED;
  1842. unsigned long vm86_mask = X86_EFLAGS_VM | X86_EFLAGS_VIF |
  1843. X86_EFLAGS_VIP;
  1844. /* TODO: Add stack limit check */
  1845. rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes);
  1846. if (rc != X86EMUL_CONTINUE)
  1847. return rc;
  1848. if (temp_eip & ~0xffff)
  1849. return emulate_gp(ctxt, 0);
  1850. rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
  1851. if (rc != X86EMUL_CONTINUE)
  1852. return rc;
  1853. rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes);
  1854. if (rc != X86EMUL_CONTINUE)
  1855. return rc;
  1856. rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS);
  1857. if (rc != X86EMUL_CONTINUE)
  1858. return rc;
  1859. ctxt->_eip = temp_eip;
  1860. if (ctxt->op_bytes == 4)
  1861. ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask));
  1862. else if (ctxt->op_bytes == 2) {
  1863. ctxt->eflags &= ~0xffff;
  1864. ctxt->eflags |= temp_eflags;
  1865. }
  1866. ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */
  1867. ctxt->eflags |= X86_EFLAGS_FIXED;
  1868. ctxt->ops->set_nmi_mask(ctxt, false);
  1869. return rc;
  1870. }
  1871. static int em_iret(struct x86_emulate_ctxt *ctxt)
  1872. {
  1873. switch(ctxt->mode) {
  1874. case X86EMUL_MODE_REAL:
  1875. return emulate_iret_real(ctxt);
  1876. case X86EMUL_MODE_VM86:
  1877. case X86EMUL_MODE_PROT16:
  1878. case X86EMUL_MODE_PROT32:
  1879. case X86EMUL_MODE_PROT64:
  1880. default:
  1881. /* iret from protected mode unimplemented yet */
  1882. return X86EMUL_UNHANDLEABLE;
  1883. }
  1884. }
  1885. static int em_jmp_far(struct x86_emulate_ctxt *ctxt)
  1886. {
  1887. int rc;
  1888. unsigned short sel;
  1889. struct desc_struct new_desc;
  1890. u8 cpl = ctxt->ops->cpl(ctxt);
  1891. memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
  1892. rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
  1893. X86_TRANSFER_CALL_JMP,
  1894. &new_desc);
  1895. if (rc != X86EMUL_CONTINUE)
  1896. return rc;
  1897. rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
  1898. /* Error handling is not implemented. */
  1899. if (rc != X86EMUL_CONTINUE)
  1900. return X86EMUL_UNHANDLEABLE;
  1901. return rc;
  1902. }
  1903. static int em_jmp_abs(struct x86_emulate_ctxt *ctxt)
  1904. {
  1905. return assign_eip_near(ctxt, ctxt->src.val);
  1906. }
  1907. static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)
  1908. {
  1909. int rc;
  1910. long int old_eip;
  1911. old_eip = ctxt->_eip;
  1912. rc = assign_eip_near(ctxt, ctxt->src.val);
  1913. if (rc != X86EMUL_CONTINUE)
  1914. return rc;
  1915. ctxt->src.val = old_eip;
  1916. rc = em_push(ctxt);
  1917. return rc;
  1918. }
  1919. static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt)
  1920. {
  1921. u64 old = ctxt->dst.orig_val64;
  1922. if (ctxt->dst.bytes == 16)
  1923. return X86EMUL_UNHANDLEABLE;
  1924. if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) ||
  1925. ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) {
  1926. *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0);
  1927. *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32);
  1928. ctxt->eflags &= ~X86_EFLAGS_ZF;
  1929. } else {
  1930. ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) |
  1931. (u32) reg_read(ctxt, VCPU_REGS_RBX);
  1932. ctxt->eflags |= X86_EFLAGS_ZF;
  1933. }
  1934. return X86EMUL_CONTINUE;
  1935. }
  1936. static int em_ret(struct x86_emulate_ctxt *ctxt)
  1937. {
  1938. int rc;
  1939. unsigned long eip;
  1940. rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
  1941. if (rc != X86EMUL_CONTINUE)
  1942. return rc;
  1943. return assign_eip_near(ctxt, eip);
  1944. }
  1945. static int em_ret_far(struct x86_emulate_ctxt *ctxt)
  1946. {
  1947. int rc;
  1948. unsigned long eip, cs;
  1949. int cpl = ctxt->ops->cpl(ctxt);
  1950. struct desc_struct new_desc;
  1951. rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
  1952. if (rc != X86EMUL_CONTINUE)
  1953. return rc;
  1954. rc = emulate_pop(ctxt, &cs, ctxt->op_bytes);
  1955. if (rc != X86EMUL_CONTINUE)
  1956. return rc;
  1957. /* Outer-privilege level return is not implemented */
  1958. if (ctxt->mode >= X86EMUL_MODE_PROT16 && (cs & 3) > cpl)
  1959. return X86EMUL_UNHANDLEABLE;
  1960. rc = __load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS, cpl,
  1961. X86_TRANSFER_RET,
  1962. &new_desc);
  1963. if (rc != X86EMUL_CONTINUE)
  1964. return rc;
  1965. rc = assign_eip_far(ctxt, eip, &new_desc);
  1966. /* Error handling is not implemented. */
  1967. if (rc != X86EMUL_CONTINUE)
  1968. return X86EMUL_UNHANDLEABLE;
  1969. return rc;
  1970. }
  1971. static int em_ret_far_imm(struct x86_emulate_ctxt *ctxt)
  1972. {
  1973. int rc;
  1974. rc = em_ret_far(ctxt);
  1975. if (rc != X86EMUL_CONTINUE)
  1976. return rc;
  1977. rsp_increment(ctxt, ctxt->src.val);
  1978. return X86EMUL_CONTINUE;
  1979. }
  1980. static int em_cmpxchg(struct x86_emulate_ctxt *ctxt)
  1981. {
  1982. /* Save real source value, then compare EAX against destination. */
  1983. ctxt->dst.orig_val = ctxt->dst.val;
  1984. ctxt->dst.val = reg_read(ctxt, VCPU_REGS_RAX);
  1985. ctxt->src.orig_val = ctxt->src.val;
  1986. ctxt->src.val = ctxt->dst.orig_val;
  1987. fastop(ctxt, em_cmp);
  1988. if (ctxt->eflags & X86_EFLAGS_ZF) {
  1989. /* Success: write back to memory; no update of EAX */
  1990. ctxt->src.type = OP_NONE;
  1991. ctxt->dst.val = ctxt->src.orig_val;
  1992. } else {
  1993. /* Failure: write the value we saw to EAX. */
  1994. ctxt->src.type = OP_REG;
  1995. ctxt->src.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
  1996. ctxt->src.val = ctxt->dst.orig_val;
  1997. /* Create write-cycle to dest by writing the same value */
  1998. ctxt->dst.val = ctxt->dst.orig_val;
  1999. }
  2000. return X86EMUL_CONTINUE;
  2001. }
  2002. static int em_lseg(struct x86_emulate_ctxt *ctxt)
  2003. {
  2004. int seg = ctxt->src2.val;
  2005. unsigned short sel;
  2006. int rc;
  2007. memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
  2008. rc = load_segment_descriptor(ctxt, sel, seg);
  2009. if (rc != X86EMUL_CONTINUE)
  2010. return rc;
  2011. ctxt->dst.val = ctxt->src.val;
  2012. return rc;
  2013. }
  2014. static int emulator_has_longmode(struct x86_emulate_ctxt *ctxt)
  2015. {
  2016. #ifdef CONFIG_X86_64
  2017. u32 eax, ebx, ecx, edx;
  2018. eax = 0x80000001;
  2019. ecx = 0;
  2020. ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
  2021. return edx & bit(X86_FEATURE_LM);
  2022. #else
  2023. return false;
  2024. #endif
  2025. }
  2026. #define GET_SMSTATE(type, smbase, offset) \
  2027. ({ \
  2028. type __val; \
  2029. int r = ctxt->ops->read_phys(ctxt, smbase + offset, &__val, \
  2030. sizeof(__val)); \
  2031. if (r != X86EMUL_CONTINUE) \
  2032. return X86EMUL_UNHANDLEABLE; \
  2033. __val; \
  2034. })
  2035. static void rsm_set_desc_flags(struct desc_struct *desc, u32 flags)
  2036. {
  2037. desc->g = (flags >> 23) & 1;
  2038. desc->d = (flags >> 22) & 1;
  2039. desc->l = (flags >> 21) & 1;
  2040. desc->avl = (flags >> 20) & 1;
  2041. desc->p = (flags >> 15) & 1;
  2042. desc->dpl = (flags >> 13) & 3;
  2043. desc->s = (flags >> 12) & 1;
  2044. desc->type = (flags >> 8) & 15;
  2045. }
  2046. static int rsm_load_seg_32(struct x86_emulate_ctxt *ctxt, u64 smbase, int n)
  2047. {
  2048. struct desc_struct desc;
  2049. int offset;
  2050. u16 selector;
  2051. selector = GET_SMSTATE(u32, smbase, 0x7fa8 + n * 4);
  2052. if (n < 3)
  2053. offset = 0x7f84 + n * 12;
  2054. else
  2055. offset = 0x7f2c + (n - 3) * 12;
  2056. set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8));
  2057. set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4));
  2058. rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, offset));
  2059. ctxt->ops->set_segment(ctxt, selector, &desc, 0, n);
  2060. return X86EMUL_CONTINUE;
  2061. }
  2062. #ifdef CONFIG_X86_64
  2063. static int rsm_load_seg_64(struct x86_emulate_ctxt *ctxt, u64 smbase, int n)
  2064. {
  2065. struct desc_struct desc;
  2066. int offset;
  2067. u16 selector;
  2068. u32 base3;
  2069. offset = 0x7e00 + n * 16;
  2070. selector = GET_SMSTATE(u16, smbase, offset);
  2071. rsm_set_desc_flags(&desc, GET_SMSTATE(u16, smbase, offset + 2) << 8);
  2072. set_desc_limit(&desc, GET_SMSTATE(u32, smbase, offset + 4));
  2073. set_desc_base(&desc, GET_SMSTATE(u32, smbase, offset + 8));
  2074. base3 = GET_SMSTATE(u32, smbase, offset + 12);
  2075. ctxt->ops->set_segment(ctxt, selector, &desc, base3, n);
  2076. return X86EMUL_CONTINUE;
  2077. }
  2078. #endif
  2079. static int rsm_enter_protected_mode(struct x86_emulate_ctxt *ctxt,
  2080. u64 cr0, u64 cr3, u64 cr4)
  2081. {
  2082. int bad;
  2083. u64 pcid;
  2084. /* In order to later set CR4.PCIDE, CR3[11:0] must be zero. */
  2085. pcid = 0;
  2086. if (cr4 & X86_CR4_PCIDE) {
  2087. pcid = cr3 & 0xfff;
  2088. cr3 &= ~0xfff;
  2089. }
  2090. bad = ctxt->ops->set_cr(ctxt, 3, cr3);
  2091. if (bad)
  2092. return X86EMUL_UNHANDLEABLE;
  2093. /*
  2094. * First enable PAE, long mode needs it before CR0.PG = 1 is set.
  2095. * Then enable protected mode. However, PCID cannot be enabled
  2096. * if EFER.LMA=0, so set it separately.
  2097. */
  2098. bad = ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
  2099. if (bad)
  2100. return X86EMUL_UNHANDLEABLE;
  2101. bad = ctxt->ops->set_cr(ctxt, 0, cr0);
  2102. if (bad)
  2103. return X86EMUL_UNHANDLEABLE;
  2104. if (cr4 & X86_CR4_PCIDE) {
  2105. bad = ctxt->ops->set_cr(ctxt, 4, cr4);
  2106. if (bad)
  2107. return X86EMUL_UNHANDLEABLE;
  2108. if (pcid) {
  2109. bad = ctxt->ops->set_cr(ctxt, 3, cr3 | pcid);
  2110. if (bad)
  2111. return X86EMUL_UNHANDLEABLE;
  2112. }
  2113. }
  2114. return X86EMUL_CONTINUE;
  2115. }
  2116. static int rsm_load_state_32(struct x86_emulate_ctxt *ctxt, u64 smbase)
  2117. {
  2118. struct desc_struct desc;
  2119. struct desc_ptr dt;
  2120. u16 selector;
  2121. u32 val, cr0, cr3, cr4;
  2122. int i;
  2123. cr0 = GET_SMSTATE(u32, smbase, 0x7ffc);
  2124. cr3 = GET_SMSTATE(u32, smbase, 0x7ff8);
  2125. ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7ff4) | X86_EFLAGS_FIXED;
  2126. ctxt->_eip = GET_SMSTATE(u32, smbase, 0x7ff0);
  2127. for (i = 0; i < 8; i++)
  2128. *reg_write(ctxt, i) = GET_SMSTATE(u32, smbase, 0x7fd0 + i * 4);
  2129. val = GET_SMSTATE(u32, smbase, 0x7fcc);
  2130. ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
  2131. val = GET_SMSTATE(u32, smbase, 0x7fc8);
  2132. ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
  2133. selector = GET_SMSTATE(u32, smbase, 0x7fc4);
  2134. set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f64));
  2135. set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f60));
  2136. rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f5c));
  2137. ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_TR);
  2138. selector = GET_SMSTATE(u32, smbase, 0x7fc0);
  2139. set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7f80));
  2140. set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7f7c));
  2141. rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7f78));
  2142. ctxt->ops->set_segment(ctxt, selector, &desc, 0, VCPU_SREG_LDTR);
  2143. dt.address = GET_SMSTATE(u32, smbase, 0x7f74);
  2144. dt.size = GET_SMSTATE(u32, smbase, 0x7f70);
  2145. ctxt->ops->set_gdt(ctxt, &dt);
  2146. dt.address = GET_SMSTATE(u32, smbase, 0x7f58);
  2147. dt.size = GET_SMSTATE(u32, smbase, 0x7f54);
  2148. ctxt->ops->set_idt(ctxt, &dt);
  2149. for (i = 0; i < 6; i++) {
  2150. int r = rsm_load_seg_32(ctxt, smbase, i);
  2151. if (r != X86EMUL_CONTINUE)
  2152. return r;
  2153. }
  2154. cr4 = GET_SMSTATE(u32, smbase, 0x7f14);
  2155. ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7ef8));
  2156. return rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
  2157. }
  2158. #ifdef CONFIG_X86_64
  2159. static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, u64 smbase)
  2160. {
  2161. struct desc_struct desc;
  2162. struct desc_ptr dt;
  2163. u64 val, cr0, cr3, cr4;
  2164. u32 base3;
  2165. u16 selector;
  2166. int i, r;
  2167. for (i = 0; i < 16; i++)
  2168. *reg_write(ctxt, i) = GET_SMSTATE(u64, smbase, 0x7ff8 - i * 8);
  2169. ctxt->_eip = GET_SMSTATE(u64, smbase, 0x7f78);
  2170. ctxt->eflags = GET_SMSTATE(u32, smbase, 0x7f70) | X86_EFLAGS_FIXED;
  2171. val = GET_SMSTATE(u32, smbase, 0x7f68);
  2172. ctxt->ops->set_dr(ctxt, 6, (val & DR6_VOLATILE) | DR6_FIXED_1);
  2173. val = GET_SMSTATE(u32, smbase, 0x7f60);
  2174. ctxt->ops->set_dr(ctxt, 7, (val & DR7_VOLATILE) | DR7_FIXED_1);
  2175. cr0 = GET_SMSTATE(u64, smbase, 0x7f58);
  2176. cr3 = GET_SMSTATE(u64, smbase, 0x7f50);
  2177. cr4 = GET_SMSTATE(u64, smbase, 0x7f48);
  2178. ctxt->ops->set_smbase(ctxt, GET_SMSTATE(u32, smbase, 0x7f00));
  2179. val = GET_SMSTATE(u64, smbase, 0x7ed0);
  2180. ctxt->ops->set_msr(ctxt, MSR_EFER, val & ~EFER_LMA);
  2181. selector = GET_SMSTATE(u32, smbase, 0x7e90);
  2182. rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e92) << 8);
  2183. set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e94));
  2184. set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e98));
  2185. base3 = GET_SMSTATE(u32, smbase, 0x7e9c);
  2186. ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_TR);
  2187. dt.size = GET_SMSTATE(u32, smbase, 0x7e84);
  2188. dt.address = GET_SMSTATE(u64, smbase, 0x7e88);
  2189. ctxt->ops->set_idt(ctxt, &dt);
  2190. selector = GET_SMSTATE(u32, smbase, 0x7e70);
  2191. rsm_set_desc_flags(&desc, GET_SMSTATE(u32, smbase, 0x7e72) << 8);
  2192. set_desc_limit(&desc, GET_SMSTATE(u32, smbase, 0x7e74));
  2193. set_desc_base(&desc, GET_SMSTATE(u32, smbase, 0x7e78));
  2194. base3 = GET_SMSTATE(u32, smbase, 0x7e7c);
  2195. ctxt->ops->set_segment(ctxt, selector, &desc, base3, VCPU_SREG_LDTR);
  2196. dt.size = GET_SMSTATE(u32, smbase, 0x7e64);
  2197. dt.address = GET_SMSTATE(u64, smbase, 0x7e68);
  2198. ctxt->ops->set_gdt(ctxt, &dt);
  2199. r = rsm_enter_protected_mode(ctxt, cr0, cr3, cr4);
  2200. if (r != X86EMUL_CONTINUE)
  2201. return r;
  2202. for (i = 0; i < 6; i++) {
  2203. r = rsm_load_seg_64(ctxt, smbase, i);
  2204. if (r != X86EMUL_CONTINUE)
  2205. return r;
  2206. }
  2207. return X86EMUL_CONTINUE;
  2208. }
  2209. #endif
  2210. static int em_rsm(struct x86_emulate_ctxt *ctxt)
  2211. {
  2212. unsigned long cr0, cr4, efer;
  2213. u64 smbase;
  2214. int ret;
  2215. if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
  2216. return emulate_ud(ctxt);
  2217. /*
  2218. * Get back to real mode, to prepare a safe state in which to load
  2219. * CR0/CR3/CR4/EFER. It's all a bit more complicated if the vCPU
  2220. * supports long mode.
  2221. */
  2222. if (emulator_has_longmode(ctxt)) {
  2223. struct desc_struct cs_desc;
  2224. /* Zero CR4.PCIDE before CR0.PG. */
  2225. cr4 = ctxt->ops->get_cr(ctxt, 4);
  2226. if (cr4 & X86_CR4_PCIDE)
  2227. ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PCIDE);
  2228. /* A 32-bit code segment is required to clear EFER.LMA. */
  2229. memset(&cs_desc, 0, sizeof(cs_desc));
  2230. cs_desc.type = 0xb;
  2231. cs_desc.s = cs_desc.g = cs_desc.p = 1;
  2232. ctxt->ops->set_segment(ctxt, 0, &cs_desc, 0, VCPU_SREG_CS);
  2233. }
  2234. /* For the 64-bit case, this will clear EFER.LMA. */
  2235. cr0 = ctxt->ops->get_cr(ctxt, 0);
  2236. if (cr0 & X86_CR0_PE)
  2237. ctxt->ops->set_cr(ctxt, 0, cr0 & ~(X86_CR0_PG | X86_CR0_PE));
  2238. if (emulator_has_longmode(ctxt)) {
  2239. /* Clear CR4.PAE before clearing EFER.LME. */
  2240. cr4 = ctxt->ops->get_cr(ctxt, 4);
  2241. if (cr4 & X86_CR4_PAE)
  2242. ctxt->ops->set_cr(ctxt, 4, cr4 & ~X86_CR4_PAE);
  2243. /* And finally go back to 32-bit mode. */
  2244. efer = 0;
  2245. ctxt->ops->set_msr(ctxt, MSR_EFER, efer);
  2246. }
  2247. smbase = ctxt->ops->get_smbase(ctxt);
  2248. /*
  2249. * Give pre_leave_smm() a chance to make ISA-specific changes to the
  2250. * vCPU state (e.g. enter guest mode) before loading state from the SMM
  2251. * state-save area.
  2252. */
  2253. if (ctxt->ops->pre_leave_smm(ctxt, smbase))
  2254. return X86EMUL_UNHANDLEABLE;
  2255. #ifdef CONFIG_X86_64
  2256. if (emulator_has_longmode(ctxt))
  2257. ret = rsm_load_state_64(ctxt, smbase + 0x8000);
  2258. else
  2259. #endif
  2260. ret = rsm_load_state_32(ctxt, smbase + 0x8000);
  2261. if (ret != X86EMUL_CONTINUE) {
  2262. /* FIXME: should triple fault */
  2263. return X86EMUL_UNHANDLEABLE;
  2264. }
  2265. if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_INSIDE_NMI_MASK) == 0)
  2266. ctxt->ops->set_nmi_mask(ctxt, false);
  2267. ctxt->ops->set_hflags(ctxt, ctxt->ops->get_hflags(ctxt) &
  2268. ~(X86EMUL_SMM_INSIDE_NMI_MASK | X86EMUL_SMM_MASK));
  2269. return X86EMUL_CONTINUE;
  2270. }
  2271. static void
  2272. setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
  2273. struct desc_struct *cs, struct desc_struct *ss)
  2274. {
  2275. cs->l = 0; /* will be adjusted later */
  2276. set_desc_base(cs, 0); /* flat segment */
  2277. cs->g = 1; /* 4kb granularity */
  2278. set_desc_limit(cs, 0xfffff); /* 4GB limit */
  2279. cs->type = 0x0b; /* Read, Execute, Accessed */
  2280. cs->s = 1;
  2281. cs->dpl = 0; /* will be adjusted later */
  2282. cs->p = 1;
  2283. cs->d = 1;
  2284. cs->avl = 0;
  2285. set_desc_base(ss, 0); /* flat segment */
  2286. set_desc_limit(ss, 0xfffff); /* 4GB limit */
  2287. ss->g = 1; /* 4kb granularity */
  2288. ss->s = 1;
  2289. ss->type = 0x03; /* Read/Write, Accessed */
  2290. ss->d = 1; /* 32bit stack segment */
  2291. ss->dpl = 0;
  2292. ss->p = 1;
  2293. ss->l = 0;
  2294. ss->avl = 0;
  2295. }
  2296. static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
  2297. {
  2298. u32 eax, ebx, ecx, edx;
  2299. eax = ecx = 0;
  2300. ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
  2301. return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
  2302. && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
  2303. && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
  2304. }
  2305. static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
  2306. {
  2307. const struct x86_emulate_ops *ops = ctxt->ops;
  2308. u32 eax, ebx, ecx, edx;
  2309. /*
  2310. * syscall should always be enabled in longmode - so only become
  2311. * vendor specific (cpuid) if other modes are active...
  2312. */
  2313. if (ctxt->mode == X86EMUL_MODE_PROT64)
  2314. return true;
  2315. eax = 0x00000000;
  2316. ecx = 0x00000000;
  2317. ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
  2318. /*
  2319. * Intel ("GenuineIntel")
  2320. * remark: Intel CPUs only support "syscall" in 64bit
  2321. * longmode. Also an 64bit guest with a
  2322. * 32bit compat-app running will #UD !! While this
  2323. * behaviour can be fixed (by emulating) into AMD
  2324. * response - CPUs of AMD can't behave like Intel.
  2325. */
  2326. if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
  2327. ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
  2328. edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx)
  2329. return false;
  2330. /* AMD ("AuthenticAMD") */
  2331. if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
  2332. ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
  2333. edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx)
  2334. return true;
  2335. /* AMD ("AMDisbetter!") */
  2336. if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
  2337. ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
  2338. edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx)
  2339. return true;
  2340. /* default: (not Intel, not AMD), apply Intel's stricter rules... */
  2341. return false;
  2342. }
  2343. static int em_syscall(struct x86_emulate_ctxt *ctxt)
  2344. {
  2345. const struct x86_emulate_ops *ops = ctxt->ops;
  2346. struct desc_struct cs, ss;
  2347. u64 msr_data;
  2348. u16 cs_sel, ss_sel;
  2349. u64 efer = 0;
  2350. /* syscall is not available in real mode */
  2351. if (ctxt->mode == X86EMUL_MODE_REAL ||
  2352. ctxt->mode == X86EMUL_MODE_VM86)
  2353. return emulate_ud(ctxt);
  2354. if (!(em_syscall_is_enabled(ctxt)))
  2355. return emulate_ud(ctxt);
  2356. ops->get_msr(ctxt, MSR_EFER, &efer);
  2357. setup_syscalls_segments(ctxt, &cs, &ss);
  2358. if (!(efer & EFER_SCE))
  2359. return emulate_ud(ctxt);
  2360. ops->get_msr(ctxt, MSR_STAR, &msr_data);
  2361. msr_data >>= 32;
  2362. cs_sel = (u16)(msr_data & 0xfffc);
  2363. ss_sel = (u16)(msr_data + 8);
  2364. if (efer & EFER_LMA) {
  2365. cs.d = 0;
  2366. cs.l = 1;
  2367. }
  2368. ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
  2369. ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
  2370. *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip;
  2371. if (efer & EFER_LMA) {
  2372. #ifdef CONFIG_X86_64
  2373. *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags;
  2374. ops->get_msr(ctxt,
  2375. ctxt->mode == X86EMUL_MODE_PROT64 ?
  2376. MSR_LSTAR : MSR_CSTAR, &msr_data);
  2377. ctxt->_eip = msr_data;
  2378. ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data);
  2379. ctxt->eflags &= ~msr_data;
  2380. ctxt->eflags |= X86_EFLAGS_FIXED;
  2381. #endif
  2382. } else {
  2383. /* legacy mode */
  2384. ops->get_msr(ctxt, MSR_STAR, &msr_data);
  2385. ctxt->_eip = (u32)msr_data;
  2386. ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
  2387. }
  2388. ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
  2389. return X86EMUL_CONTINUE;
  2390. }
  2391. static int em_sysenter(struct x86_emulate_ctxt *ctxt)
  2392. {
  2393. const struct x86_emulate_ops *ops = ctxt->ops;
  2394. struct desc_struct cs, ss;
  2395. u64 msr_data;
  2396. u16 cs_sel, ss_sel;
  2397. u64 efer = 0;
  2398. ops->get_msr(ctxt, MSR_EFER, &efer);
  2399. /* inject #GP if in real mode */
  2400. if (ctxt->mode == X86EMUL_MODE_REAL)
  2401. return emulate_gp(ctxt, 0);
  2402. /*
  2403. * Not recognized on AMD in compat mode (but is recognized in legacy
  2404. * mode).
  2405. */
  2406. if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA)
  2407. && !vendor_intel(ctxt))
  2408. return emulate_ud(ctxt);
  2409. /* sysenter/sysexit have not been tested in 64bit mode. */
  2410. if (ctxt->mode == X86EMUL_MODE_PROT64)
  2411. return X86EMUL_UNHANDLEABLE;
  2412. setup_syscalls_segments(ctxt, &cs, &ss);
  2413. ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
  2414. if ((msr_data & 0xfffc) == 0x0)
  2415. return emulate_gp(ctxt, 0);
  2416. ctxt->eflags &= ~(X86_EFLAGS_VM | X86_EFLAGS_IF);
  2417. cs_sel = (u16)msr_data & ~SEGMENT_RPL_MASK;
  2418. ss_sel = cs_sel + 8;
  2419. if (efer & EFER_LMA) {
  2420. cs.d = 0;
  2421. cs.l = 1;
  2422. }
  2423. ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
  2424. ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
  2425. ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data);
  2426. ctxt->_eip = (efer & EFER_LMA) ? msr_data : (u32)msr_data;
  2427. ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data);
  2428. *reg_write(ctxt, VCPU_REGS_RSP) = (efer & EFER_LMA) ? msr_data :
  2429. (u32)msr_data;
  2430. return X86EMUL_CONTINUE;
  2431. }
  2432. static int em_sysexit(struct x86_emulate_ctxt *ctxt)
  2433. {
  2434. const struct x86_emulate_ops *ops = ctxt->ops;
  2435. struct desc_struct cs, ss;
  2436. u64 msr_data, rcx, rdx;
  2437. int usermode;
  2438. u16 cs_sel = 0, ss_sel = 0;
  2439. /* inject #GP if in real mode or Virtual 8086 mode */
  2440. if (ctxt->mode == X86EMUL_MODE_REAL ||
  2441. ctxt->mode == X86EMUL_MODE_VM86)
  2442. return emulate_gp(ctxt, 0);
  2443. setup_syscalls_segments(ctxt, &cs, &ss);
  2444. if ((ctxt->rex_prefix & 0x8) != 0x0)
  2445. usermode = X86EMUL_MODE_PROT64;
  2446. else
  2447. usermode = X86EMUL_MODE_PROT32;
  2448. rcx = reg_read(ctxt, VCPU_REGS_RCX);
  2449. rdx = reg_read(ctxt, VCPU_REGS_RDX);
  2450. cs.dpl = 3;
  2451. ss.dpl = 3;
  2452. ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
  2453. switch (usermode) {
  2454. case X86EMUL_MODE_PROT32:
  2455. cs_sel = (u16)(msr_data + 16);
  2456. if ((msr_data & 0xfffc) == 0x0)
  2457. return emulate_gp(ctxt, 0);
  2458. ss_sel = (u16)(msr_data + 24);
  2459. rcx = (u32)rcx;
  2460. rdx = (u32)rdx;
  2461. break;
  2462. case X86EMUL_MODE_PROT64:
  2463. cs_sel = (u16)(msr_data + 32);
  2464. if (msr_data == 0x0)
  2465. return emulate_gp(ctxt, 0);
  2466. ss_sel = cs_sel + 8;
  2467. cs.d = 0;
  2468. cs.l = 1;
  2469. if (emul_is_noncanonical_address(rcx, ctxt) ||
  2470. emul_is_noncanonical_address(rdx, ctxt))
  2471. return emulate_gp(ctxt, 0);
  2472. break;
  2473. }
  2474. cs_sel |= SEGMENT_RPL_MASK;
  2475. ss_sel |= SEGMENT_RPL_MASK;
  2476. ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS);
  2477. ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS);
  2478. ctxt->_eip = rdx;
  2479. *reg_write(ctxt, VCPU_REGS_RSP) = rcx;
  2480. return X86EMUL_CONTINUE;
  2481. }
  2482. static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt)
  2483. {
  2484. int iopl;
  2485. if (ctxt->mode == X86EMUL_MODE_REAL)
  2486. return false;
  2487. if (ctxt->mode == X86EMUL_MODE_VM86)
  2488. return true;
  2489. iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> X86_EFLAGS_IOPL_BIT;
  2490. return ctxt->ops->cpl(ctxt) > iopl;
  2491. }
  2492. #define VMWARE_PORT_VMPORT (0x5658)
  2493. #define VMWARE_PORT_VMRPC (0x5659)
  2494. static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt,
  2495. u16 port, u16 len)
  2496. {
  2497. const struct x86_emulate_ops *ops = ctxt->ops;
  2498. struct desc_struct tr_seg;
  2499. u32 base3;
  2500. int r;
  2501. u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7;
  2502. unsigned mask = (1 << len) - 1;
  2503. unsigned long base;
  2504. /*
  2505. * VMware allows access to these ports even if denied
  2506. * by TSS I/O permission bitmap. Mimic behavior.
  2507. */
  2508. if (enable_vmware_backdoor &&
  2509. ((port == VMWARE_PORT_VMPORT) || (port == VMWARE_PORT_VMRPC)))
  2510. return true;
  2511. ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR);
  2512. if (!tr_seg.p)
  2513. return false;
  2514. if (desc_limit_scaled(&tr_seg) < 103)
  2515. return false;
  2516. base = get_desc_base(&tr_seg);
  2517. #ifdef CONFIG_X86_64
  2518. base |= ((u64)base3) << 32;
  2519. #endif
  2520. r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL, true);
  2521. if (r != X86EMUL_CONTINUE)
  2522. return false;
  2523. if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg))
  2524. return false;
  2525. r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL, true);
  2526. if (r != X86EMUL_CONTINUE)
  2527. return false;
  2528. if ((perm >> bit_idx) & mask)
  2529. return false;
  2530. return true;
  2531. }
  2532. static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,
  2533. u16 port, u16 len)
  2534. {
  2535. if (ctxt->perm_ok)
  2536. return true;
  2537. if (emulator_bad_iopl(ctxt))
  2538. if (!emulator_io_port_access_allowed(ctxt, port, len))
  2539. return false;
  2540. ctxt->perm_ok = true;
  2541. return true;
  2542. }
  2543. static void string_registers_quirk(struct x86_emulate_ctxt *ctxt)
  2544. {
  2545. /*
  2546. * Intel CPUs mask the counter and pointers in quite strange
  2547. * manner when ECX is zero due to REP-string optimizations.
  2548. */
  2549. #ifdef CONFIG_X86_64
  2550. if (ctxt->ad_bytes != 4 || !vendor_intel(ctxt))
  2551. return;
  2552. *reg_write(ctxt, VCPU_REGS_RCX) = 0;
  2553. switch (ctxt->b) {
  2554. case 0xa4: /* movsb */
  2555. case 0xa5: /* movsd/w */
  2556. *reg_rmw(ctxt, VCPU_REGS_RSI) &= (u32)-1;
  2557. /* fall through */
  2558. case 0xaa: /* stosb */
  2559. case 0xab: /* stosd/w */
  2560. *reg_rmw(ctxt, VCPU_REGS_RDI) &= (u32)-1;
  2561. }
  2562. #endif
  2563. }
  2564. static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt,
  2565. struct tss_segment_16 *tss)
  2566. {
  2567. tss->ip = ctxt->_eip;
  2568. tss->flag = ctxt->eflags;
  2569. tss->ax = reg_read(ctxt, VCPU_REGS_RAX);
  2570. tss->cx = reg_read(ctxt, VCPU_REGS_RCX);
  2571. tss->dx = reg_read(ctxt, VCPU_REGS_RDX);
  2572. tss->bx = reg_read(ctxt, VCPU_REGS_RBX);
  2573. tss->sp = reg_read(ctxt, VCPU_REGS_RSP);
  2574. tss->bp = reg_read(ctxt, VCPU_REGS_RBP);
  2575. tss->si = reg_read(ctxt, VCPU_REGS_RSI);
  2576. tss->di = reg_read(ctxt, VCPU_REGS_RDI);
  2577. tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
  2578. tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
  2579. tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
  2580. tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
  2581. tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR);
  2582. }
  2583. static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt,
  2584. struct tss_segment_16 *tss)
  2585. {
  2586. int ret;
  2587. u8 cpl;
  2588. ctxt->_eip = tss->ip;
  2589. ctxt->eflags = tss->flag | 2;
  2590. *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax;
  2591. *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx;
  2592. *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx;
  2593. *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx;
  2594. *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp;
  2595. *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp;
  2596. *reg_write(ctxt, VCPU_REGS_RSI) = tss->si;
  2597. *reg_write(ctxt, VCPU_REGS_RDI) = tss->di;
  2598. /*
  2599. * SDM says that segment selectors are loaded before segment
  2600. * descriptors
  2601. */
  2602. set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR);
  2603. set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
  2604. set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
  2605. set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
  2606. set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
  2607. cpl = tss->cs & 3;
  2608. /*
  2609. * Now load segment descriptors. If fault happens at this stage
  2610. * it is handled in a context of new task
  2611. */
  2612. ret = __load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR, cpl,
  2613. X86_TRANSFER_TASK_SWITCH, NULL);
  2614. if (ret != X86EMUL_CONTINUE)
  2615. return ret;
  2616. ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
  2617. X86_TRANSFER_TASK_SWITCH, NULL);
  2618. if (ret != X86EMUL_CONTINUE)
  2619. return ret;
  2620. ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
  2621. X86_TRANSFER_TASK_SWITCH, NULL);
  2622. if (ret != X86EMUL_CONTINUE)
  2623. return ret;
  2624. ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
  2625. X86_TRANSFER_TASK_SWITCH, NULL);
  2626. if (ret != X86EMUL_CONTINUE)
  2627. return ret;
  2628. ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
  2629. X86_TRANSFER_TASK_SWITCH, NULL);
  2630. if (ret != X86EMUL_CONTINUE)
  2631. return ret;
  2632. return X86EMUL_CONTINUE;
  2633. }
  2634. static int task_switch_16(struct x86_emulate_ctxt *ctxt,
  2635. u16 tss_selector, u16 old_tss_sel,
  2636. ulong old_tss_base, struct desc_struct *new_desc)
  2637. {
  2638. struct tss_segment_16 tss_seg;
  2639. int ret;
  2640. u32 new_tss_base = get_desc_base(new_desc);
  2641. ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof tss_seg);
  2642. if (ret != X86EMUL_CONTINUE)
  2643. return ret;
  2644. save_state_to_tss16(ctxt, &tss_seg);
  2645. ret = linear_write_system(ctxt, old_tss_base, &tss_seg, sizeof tss_seg);
  2646. if (ret != X86EMUL_CONTINUE)
  2647. return ret;
  2648. ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof tss_seg);
  2649. if (ret != X86EMUL_CONTINUE)
  2650. return ret;
  2651. if (old_tss_sel != 0xffff) {
  2652. tss_seg.prev_task_link = old_tss_sel;
  2653. ret = linear_write_system(ctxt, new_tss_base,
  2654. &tss_seg.prev_task_link,
  2655. sizeof tss_seg.prev_task_link);
  2656. if (ret != X86EMUL_CONTINUE)
  2657. return ret;
  2658. }
  2659. return load_state_from_tss16(ctxt, &tss_seg);
  2660. }
  2661. static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt,
  2662. struct tss_segment_32 *tss)
  2663. {
  2664. /* CR3 and ldt selector are not saved intentionally */
  2665. tss->eip = ctxt->_eip;
  2666. tss->eflags = ctxt->eflags;
  2667. tss->eax = reg_read(ctxt, VCPU_REGS_RAX);
  2668. tss->ecx = reg_read(ctxt, VCPU_REGS_RCX);
  2669. tss->edx = reg_read(ctxt, VCPU_REGS_RDX);
  2670. tss->ebx = reg_read(ctxt, VCPU_REGS_RBX);
  2671. tss->esp = reg_read(ctxt, VCPU_REGS_RSP);
  2672. tss->ebp = reg_read(ctxt, VCPU_REGS_RBP);
  2673. tss->esi = reg_read(ctxt, VCPU_REGS_RSI);
  2674. tss->edi = reg_read(ctxt, VCPU_REGS_RDI);
  2675. tss->es = get_segment_selector(ctxt, VCPU_SREG_ES);
  2676. tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS);
  2677. tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS);
  2678. tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS);
  2679. tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS);
  2680. tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS);
  2681. }
  2682. static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt,
  2683. struct tss_segment_32 *tss)
  2684. {
  2685. int ret;
  2686. u8 cpl;
  2687. if (ctxt->ops->set_cr(ctxt, 3, tss->cr3))
  2688. return emulate_gp(ctxt, 0);
  2689. ctxt->_eip = tss->eip;
  2690. ctxt->eflags = tss->eflags | 2;
  2691. /* General purpose registers */
  2692. *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax;
  2693. *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx;
  2694. *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx;
  2695. *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx;
  2696. *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp;
  2697. *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp;
  2698. *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi;
  2699. *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi;
  2700. /*
  2701. * SDM says that segment selectors are loaded before segment
  2702. * descriptors. This is important because CPL checks will
  2703. * use CS.RPL.
  2704. */
  2705. set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR);
  2706. set_segment_selector(ctxt, tss->es, VCPU_SREG_ES);
  2707. set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS);
  2708. set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS);
  2709. set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS);
  2710. set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS);
  2711. set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS);
  2712. /*
  2713. * If we're switching between Protected Mode and VM86, we need to make
  2714. * sure to update the mode before loading the segment descriptors so
  2715. * that the selectors are interpreted correctly.
  2716. */
  2717. if (ctxt->eflags & X86_EFLAGS_VM) {
  2718. ctxt->mode = X86EMUL_MODE_VM86;
  2719. cpl = 3;
  2720. } else {
  2721. ctxt->mode = X86EMUL_MODE_PROT32;
  2722. cpl = tss->cs & 3;
  2723. }
  2724. /*
  2725. * Now load segment descriptors. If fault happenes at this stage
  2726. * it is handled in a context of new task
  2727. */
  2728. ret = __load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR,
  2729. cpl, X86_TRANSFER_TASK_SWITCH, NULL);
  2730. if (ret != X86EMUL_CONTINUE)
  2731. return ret;
  2732. ret = __load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES, cpl,
  2733. X86_TRANSFER_TASK_SWITCH, NULL);
  2734. if (ret != X86EMUL_CONTINUE)
  2735. return ret;
  2736. ret = __load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS, cpl,
  2737. X86_TRANSFER_TASK_SWITCH, NULL);
  2738. if (ret != X86EMUL_CONTINUE)
  2739. return ret;
  2740. ret = __load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS, cpl,
  2741. X86_TRANSFER_TASK_SWITCH, NULL);
  2742. if (ret != X86EMUL_CONTINUE)
  2743. return ret;
  2744. ret = __load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS, cpl,
  2745. X86_TRANSFER_TASK_SWITCH, NULL);
  2746. if (ret != X86EMUL_CONTINUE)
  2747. return ret;
  2748. ret = __load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS, cpl,
  2749. X86_TRANSFER_TASK_SWITCH, NULL);
  2750. if (ret != X86EMUL_CONTINUE)
  2751. return ret;
  2752. ret = __load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS, cpl,
  2753. X86_TRANSFER_TASK_SWITCH, NULL);
  2754. return ret;
  2755. }
  2756. static int task_switch_32(struct x86_emulate_ctxt *ctxt,
  2757. u16 tss_selector, u16 old_tss_sel,
  2758. ulong old_tss_base, struct desc_struct *new_desc)
  2759. {
  2760. struct tss_segment_32 tss_seg;
  2761. int ret;
  2762. u32 new_tss_base = get_desc_base(new_desc);
  2763. u32 eip_offset = offsetof(struct tss_segment_32, eip);
  2764. u32 ldt_sel_offset = offsetof(struct tss_segment_32, ldt_selector);
  2765. ret = linear_read_system(ctxt, old_tss_base, &tss_seg, sizeof tss_seg);
  2766. if (ret != X86EMUL_CONTINUE)
  2767. return ret;
  2768. save_state_to_tss32(ctxt, &tss_seg);
  2769. /* Only GP registers and segment selectors are saved */
  2770. ret = linear_write_system(ctxt, old_tss_base + eip_offset, &tss_seg.eip,
  2771. ldt_sel_offset - eip_offset);
  2772. if (ret != X86EMUL_CONTINUE)
  2773. return ret;
  2774. ret = linear_read_system(ctxt, new_tss_base, &tss_seg, sizeof tss_seg);
  2775. if (ret != X86EMUL_CONTINUE)
  2776. return ret;
  2777. if (old_tss_sel != 0xffff) {
  2778. tss_seg.prev_task_link = old_tss_sel;
  2779. ret = linear_write_system(ctxt, new_tss_base,
  2780. &tss_seg.prev_task_link,
  2781. sizeof tss_seg.prev_task_link);
  2782. if (ret != X86EMUL_CONTINUE)
  2783. return ret;
  2784. }
  2785. return load_state_from_tss32(ctxt, &tss_seg);
  2786. }
  2787. static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt,
  2788. u16 tss_selector, int idt_index, int reason,
  2789. bool has_error_code, u32 error_code)
  2790. {
  2791. const struct x86_emulate_ops *ops = ctxt->ops;
  2792. struct desc_struct curr_tss_desc, next_tss_desc;
  2793. int ret;
  2794. u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR);
  2795. ulong old_tss_base =
  2796. ops->get_cached_segment_base(ctxt, VCPU_SREG_TR);
  2797. u32 desc_limit;
  2798. ulong desc_addr, dr7;
  2799. /* FIXME: old_tss_base == ~0 ? */
  2800. ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr);
  2801. if (ret != X86EMUL_CONTINUE)
  2802. return ret;
  2803. ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr);
  2804. if (ret != X86EMUL_CONTINUE)
  2805. return ret;
  2806. /* FIXME: check that next_tss_desc is tss */
  2807. /*
  2808. * Check privileges. The three cases are task switch caused by...
  2809. *
  2810. * 1. jmp/call/int to task gate: Check against DPL of the task gate
  2811. * 2. Exception/IRQ/iret: No check is performed
  2812. * 3. jmp/call to TSS/task-gate: No check is performed since the
  2813. * hardware checks it before exiting.
  2814. */
  2815. if (reason == TASK_SWITCH_GATE) {
  2816. if (idt_index != -1) {
  2817. /* Software interrupts */
  2818. struct desc_struct task_gate_desc;
  2819. int dpl;
  2820. ret = read_interrupt_descriptor(ctxt, idt_index,
  2821. &task_gate_desc);
  2822. if (ret != X86EMUL_CONTINUE)
  2823. return ret;
  2824. dpl = task_gate_desc.dpl;
  2825. if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl)
  2826. return emulate_gp(ctxt, (idt_index << 3) | 0x2);
  2827. }
  2828. }
  2829. desc_limit = desc_limit_scaled(&next_tss_desc);
  2830. if (!next_tss_desc.p ||
  2831. ((desc_limit < 0x67 && (next_tss_desc.type & 8)) ||
  2832. desc_limit < 0x2b)) {
  2833. return emulate_ts(ctxt, tss_selector & 0xfffc);
  2834. }
  2835. if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
  2836. curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */
  2837. write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc);
  2838. }
  2839. if (reason == TASK_SWITCH_IRET)
  2840. ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT;
  2841. /* set back link to prev task only if NT bit is set in eflags
  2842. note that old_tss_sel is not used after this point */
  2843. if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
  2844. old_tss_sel = 0xffff;
  2845. if (next_tss_desc.type & 8)
  2846. ret = task_switch_32(ctxt, tss_selector, old_tss_sel,
  2847. old_tss_base, &next_tss_desc);
  2848. else
  2849. ret = task_switch_16(ctxt, tss_selector, old_tss_sel,
  2850. old_tss_base, &next_tss_desc);
  2851. if (ret != X86EMUL_CONTINUE)
  2852. return ret;
  2853. if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE)
  2854. ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT;
  2855. if (reason != TASK_SWITCH_IRET) {
  2856. next_tss_desc.type |= (1 << 1); /* set busy flag */
  2857. write_segment_descriptor(ctxt, tss_selector, &next_tss_desc);
  2858. }
  2859. ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS);
  2860. ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR);
  2861. if (has_error_code) {
  2862. ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2;
  2863. ctxt->lock_prefix = 0;
  2864. ctxt->src.val = (unsigned long) error_code;
  2865. ret = em_push(ctxt);
  2866. }
  2867. ops->get_dr(ctxt, 7, &dr7);
  2868. ops->set_dr(ctxt, 7, dr7 & ~(DR_LOCAL_ENABLE_MASK | DR_LOCAL_SLOWDOWN));
  2869. return ret;
  2870. }
  2871. int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
  2872. u16 tss_selector, int idt_index, int reason,
  2873. bool has_error_code, u32 error_code)
  2874. {
  2875. int rc;
  2876. invalidate_registers(ctxt);
  2877. ctxt->_eip = ctxt->eip;
  2878. ctxt->dst.type = OP_NONE;
  2879. rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason,
  2880. has_error_code, error_code);
  2881. if (rc == X86EMUL_CONTINUE) {
  2882. ctxt->eip = ctxt->_eip;
  2883. writeback_registers(ctxt);
  2884. }
  2885. return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
  2886. }
  2887. static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg,
  2888. struct operand *op)
  2889. {
  2890. int df = (ctxt->eflags & X86_EFLAGS_DF) ? -op->count : op->count;
  2891. register_address_increment(ctxt, reg, df * op->bytes);
  2892. op->addr.mem.ea = register_address(ctxt, reg);
  2893. }
  2894. static int em_das(struct x86_emulate_ctxt *ctxt)
  2895. {
  2896. u8 al, old_al;
  2897. bool af, cf, old_cf;
  2898. cf = ctxt->eflags & X86_EFLAGS_CF;
  2899. al = ctxt->dst.val;
  2900. old_al = al;
  2901. old_cf = cf;
  2902. cf = false;
  2903. af = ctxt->eflags & X86_EFLAGS_AF;
  2904. if ((al & 0x0f) > 9 || af) {
  2905. al -= 6;
  2906. cf = old_cf | (al >= 250);
  2907. af = true;
  2908. } else {
  2909. af = false;
  2910. }
  2911. if (old_al > 0x99 || old_cf) {
  2912. al -= 0x60;
  2913. cf = true;
  2914. }
  2915. ctxt->dst.val = al;
  2916. /* Set PF, ZF, SF */
  2917. ctxt->src.type = OP_IMM;
  2918. ctxt->src.val = 0;
  2919. ctxt->src.bytes = 1;
  2920. fastop(ctxt, em_or);
  2921. ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF);
  2922. if (cf)
  2923. ctxt->eflags |= X86_EFLAGS_CF;
  2924. if (af)
  2925. ctxt->eflags |= X86_EFLAGS_AF;
  2926. return X86EMUL_CONTINUE;
  2927. }
  2928. static int em_aam(struct x86_emulate_ctxt *ctxt)
  2929. {
  2930. u8 al, ah;
  2931. if (ctxt->src.val == 0)
  2932. return emulate_de(ctxt);
  2933. al = ctxt->dst.val & 0xff;
  2934. ah = al / ctxt->src.val;
  2935. al %= ctxt->src.val;
  2936. ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al | (ah << 8);
  2937. /* Set PF, ZF, SF */
  2938. ctxt->src.type = OP_IMM;
  2939. ctxt->src.val = 0;
  2940. ctxt->src.bytes = 1;
  2941. fastop(ctxt, em_or);
  2942. return X86EMUL_CONTINUE;
  2943. }
  2944. static int em_aad(struct x86_emulate_ctxt *ctxt)
  2945. {
  2946. u8 al = ctxt->dst.val & 0xff;
  2947. u8 ah = (ctxt->dst.val >> 8) & 0xff;
  2948. al = (al + (ah * ctxt->src.val)) & 0xff;
  2949. ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al;
  2950. /* Set PF, ZF, SF */
  2951. ctxt->src.type = OP_IMM;
  2952. ctxt->src.val = 0;
  2953. ctxt->src.bytes = 1;
  2954. fastop(ctxt, em_or);
  2955. return X86EMUL_CONTINUE;
  2956. }
  2957. static int em_call(struct x86_emulate_ctxt *ctxt)
  2958. {
  2959. int rc;
  2960. long rel = ctxt->src.val;
  2961. ctxt->src.val = (unsigned long)ctxt->_eip;
  2962. rc = jmp_rel(ctxt, rel);
  2963. if (rc != X86EMUL_CONTINUE)
  2964. return rc;
  2965. return em_push(ctxt);
  2966. }
  2967. static int em_call_far(struct x86_emulate_ctxt *ctxt)
  2968. {
  2969. u16 sel, old_cs;
  2970. ulong old_eip;
  2971. int rc;
  2972. struct desc_struct old_desc, new_desc;
  2973. const struct x86_emulate_ops *ops = ctxt->ops;
  2974. int cpl = ctxt->ops->cpl(ctxt);
  2975. enum x86emul_mode prev_mode = ctxt->mode;
  2976. old_eip = ctxt->_eip;
  2977. ops->get_segment(ctxt, &old_cs, &old_desc, NULL, VCPU_SREG_CS);
  2978. memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2);
  2979. rc = __load_segment_descriptor(ctxt, sel, VCPU_SREG_CS, cpl,
  2980. X86_TRANSFER_CALL_JMP, &new_desc);
  2981. if (rc != X86EMUL_CONTINUE)
  2982. return rc;
  2983. rc = assign_eip_far(ctxt, ctxt->src.val, &new_desc);
  2984. if (rc != X86EMUL_CONTINUE)
  2985. goto fail;
  2986. ctxt->src.val = old_cs;
  2987. rc = em_push(ctxt);
  2988. if (rc != X86EMUL_CONTINUE)
  2989. goto fail;
  2990. ctxt->src.val = old_eip;
  2991. rc = em_push(ctxt);
  2992. /* If we failed, we tainted the memory, but the very least we should
  2993. restore cs */
  2994. if (rc != X86EMUL_CONTINUE) {
  2995. pr_warn_once("faulting far call emulation tainted memory\n");
  2996. goto fail;
  2997. }
  2998. return rc;
  2999. fail:
  3000. ops->set_segment(ctxt, old_cs, &old_desc, 0, VCPU_SREG_CS);
  3001. ctxt->mode = prev_mode;
  3002. return rc;
  3003. }
  3004. static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt)
  3005. {
  3006. int rc;
  3007. unsigned long eip;
  3008. rc = emulate_pop(ctxt, &eip, ctxt->op_bytes);
  3009. if (rc != X86EMUL_CONTINUE)
  3010. return rc;
  3011. rc = assign_eip_near(ctxt, eip);
  3012. if (rc != X86EMUL_CONTINUE)
  3013. return rc;
  3014. rsp_increment(ctxt, ctxt->src.val);
  3015. return X86EMUL_CONTINUE;
  3016. }
  3017. static int em_xchg(struct x86_emulate_ctxt *ctxt)
  3018. {
  3019. /* Write back the register source. */
  3020. ctxt->src.val = ctxt->dst.val;
  3021. write_register_operand(&ctxt->src);
  3022. /* Write back the memory destination with implicit LOCK prefix. */
  3023. ctxt->dst.val = ctxt->src.orig_val;
  3024. ctxt->lock_prefix = 1;
  3025. return X86EMUL_CONTINUE;
  3026. }
  3027. static int em_imul_3op(struct x86_emulate_ctxt *ctxt)
  3028. {
  3029. ctxt->dst.val = ctxt->src2.val;
  3030. return fastop(ctxt, em_imul);
  3031. }
  3032. static int em_cwd(struct x86_emulate_ctxt *ctxt)
  3033. {
  3034. ctxt->dst.type = OP_REG;
  3035. ctxt->dst.bytes = ctxt->src.bytes;
  3036. ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
  3037. ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1);
  3038. return X86EMUL_CONTINUE;
  3039. }
  3040. static int em_rdpid(struct x86_emulate_ctxt *ctxt)
  3041. {
  3042. u64 tsc_aux = 0;
  3043. if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux))
  3044. return emulate_gp(ctxt, 0);
  3045. ctxt->dst.val = tsc_aux;
  3046. return X86EMUL_CONTINUE;
  3047. }
  3048. static int em_rdtsc(struct x86_emulate_ctxt *ctxt)
  3049. {
  3050. u64 tsc = 0;
  3051. ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc);
  3052. *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc;
  3053. *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32;
  3054. return X86EMUL_CONTINUE;
  3055. }
  3056. static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
  3057. {
  3058. u64 pmc;
  3059. if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc))
  3060. return emulate_gp(ctxt, 0);
  3061. *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc;
  3062. *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32;
  3063. return X86EMUL_CONTINUE;
  3064. }
  3065. static int em_mov(struct x86_emulate_ctxt *ctxt)
  3066. {
  3067. memcpy(ctxt->dst.valptr, ctxt->src.valptr, sizeof(ctxt->src.valptr));
  3068. return X86EMUL_CONTINUE;
  3069. }
  3070. #define FFL(x) bit(X86_FEATURE_##x)
  3071. static int em_movbe(struct x86_emulate_ctxt *ctxt)
  3072. {
  3073. u32 ebx, ecx, edx, eax = 1;
  3074. u16 tmp;
  3075. /*
  3076. * Check MOVBE is set in the guest-visible CPUID leaf.
  3077. */
  3078. ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
  3079. if (!(ecx & FFL(MOVBE)))
  3080. return emulate_ud(ctxt);
  3081. switch (ctxt->op_bytes) {
  3082. case 2:
  3083. /*
  3084. * From MOVBE definition: "...When the operand size is 16 bits,
  3085. * the upper word of the destination register remains unchanged
  3086. * ..."
  3087. *
  3088. * Both casting ->valptr and ->val to u16 breaks strict aliasing
  3089. * rules so we have to do the operation almost per hand.
  3090. */
  3091. tmp = (u16)ctxt->src.val;
  3092. ctxt->dst.val &= ~0xffffUL;
  3093. ctxt->dst.val |= (unsigned long)swab16(tmp);
  3094. break;
  3095. case 4:
  3096. ctxt->dst.val = swab32((u32)ctxt->src.val);
  3097. break;
  3098. case 8:
  3099. ctxt->dst.val = swab64(ctxt->src.val);
  3100. break;
  3101. default:
  3102. BUG();
  3103. }
  3104. return X86EMUL_CONTINUE;
  3105. }
  3106. static int em_cr_write(struct x86_emulate_ctxt *ctxt)
  3107. {
  3108. if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
  3109. return emulate_gp(ctxt, 0);
  3110. /* Disable writeback. */
  3111. ctxt->dst.type = OP_NONE;
  3112. return X86EMUL_CONTINUE;
  3113. }
  3114. static int em_dr_write(struct x86_emulate_ctxt *ctxt)
  3115. {
  3116. unsigned long val;
  3117. if (ctxt->mode == X86EMUL_MODE_PROT64)
  3118. val = ctxt->src.val & ~0ULL;
  3119. else
  3120. val = ctxt->src.val & ~0U;
  3121. /* #UD condition is already handled. */
  3122. if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0)
  3123. return emulate_gp(ctxt, 0);
  3124. /* Disable writeback. */
  3125. ctxt->dst.type = OP_NONE;
  3126. return X86EMUL_CONTINUE;
  3127. }
  3128. static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
  3129. {
  3130. u64 msr_data;
  3131. msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX)
  3132. | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
  3133. if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data))
  3134. return emulate_gp(ctxt, 0);
  3135. return X86EMUL_CONTINUE;
  3136. }
  3137. static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
  3138. {
  3139. u64 msr_data;
  3140. if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data))
  3141. return emulate_gp(ctxt, 0);
  3142. *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
  3143. *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
  3144. return X86EMUL_CONTINUE;
  3145. }
  3146. static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
  3147. {
  3148. if (segment > VCPU_SREG_GS &&
  3149. (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
  3150. ctxt->ops->cpl(ctxt) > 0)
  3151. return emulate_gp(ctxt, 0);
  3152. ctxt->dst.val = get_segment_selector(ctxt, segment);
  3153. if (ctxt->dst.bytes == 4 && ctxt->dst.type == OP_MEM)
  3154. ctxt->dst.bytes = 2;
  3155. return X86EMUL_CONTINUE;
  3156. }
  3157. static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt)
  3158. {
  3159. if (ctxt->modrm_reg > VCPU_SREG_GS)
  3160. return emulate_ud(ctxt);
  3161. return em_store_sreg(ctxt, ctxt->modrm_reg);
  3162. }
  3163. static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
  3164. {
  3165. u16 sel = ctxt->src.val;
  3166. if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS)
  3167. return emulate_ud(ctxt);
  3168. if (ctxt->modrm_reg == VCPU_SREG_SS)
  3169. ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS;
  3170. /* Disable writeback. */
  3171. ctxt->dst.type = OP_NONE;
  3172. return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
  3173. }
  3174. static int em_sldt(struct x86_emulate_ctxt *ctxt)
  3175. {
  3176. return em_store_sreg(ctxt, VCPU_SREG_LDTR);
  3177. }
  3178. static int em_lldt(struct x86_emulate_ctxt *ctxt)
  3179. {
  3180. u16 sel = ctxt->src.val;
  3181. /* Disable writeback. */
  3182. ctxt->dst.type = OP_NONE;
  3183. return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR);
  3184. }
  3185. static int em_str(struct x86_emulate_ctxt *ctxt)
  3186. {
  3187. return em_store_sreg(ctxt, VCPU_SREG_TR);
  3188. }
  3189. static int em_ltr(struct x86_emulate_ctxt *ctxt)
  3190. {
  3191. u16 sel = ctxt->src.val;
  3192. /* Disable writeback. */
  3193. ctxt->dst.type = OP_NONE;
  3194. return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR);
  3195. }
  3196. static int em_invlpg(struct x86_emulate_ctxt *ctxt)
  3197. {
  3198. int rc;
  3199. ulong linear;
  3200. rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
  3201. if (rc == X86EMUL_CONTINUE)
  3202. ctxt->ops->invlpg(ctxt, linear);
  3203. /* Disable writeback. */
  3204. ctxt->dst.type = OP_NONE;
  3205. return X86EMUL_CONTINUE;
  3206. }
  3207. static int em_clts(struct x86_emulate_ctxt *ctxt)
  3208. {
  3209. ulong cr0;
  3210. cr0 = ctxt->ops->get_cr(ctxt, 0);
  3211. cr0 &= ~X86_CR0_TS;
  3212. ctxt->ops->set_cr(ctxt, 0, cr0);
  3213. return X86EMUL_CONTINUE;
  3214. }
  3215. static int em_hypercall(struct x86_emulate_ctxt *ctxt)
  3216. {
  3217. int rc = ctxt->ops->fix_hypercall(ctxt);
  3218. if (rc != X86EMUL_CONTINUE)
  3219. return rc;
  3220. /* Let the processor re-execute the fixed hypercall */
  3221. ctxt->_eip = ctxt->eip;
  3222. /* Disable writeback. */
  3223. ctxt->dst.type = OP_NONE;
  3224. return X86EMUL_CONTINUE;
  3225. }
  3226. static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
  3227. void (*get)(struct x86_emulate_ctxt *ctxt,
  3228. struct desc_ptr *ptr))
  3229. {
  3230. struct desc_ptr desc_ptr;
  3231. if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
  3232. ctxt->ops->cpl(ctxt) > 0)
  3233. return emulate_gp(ctxt, 0);
  3234. if (ctxt->mode == X86EMUL_MODE_PROT64)
  3235. ctxt->op_bytes = 8;
  3236. get(ctxt, &desc_ptr);
  3237. if (ctxt->op_bytes == 2) {
  3238. ctxt->op_bytes = 4;
  3239. desc_ptr.address &= 0x00ffffff;
  3240. }
  3241. /* Disable writeback. */
  3242. ctxt->dst.type = OP_NONE;
  3243. return segmented_write_std(ctxt, ctxt->dst.addr.mem,
  3244. &desc_ptr, 2 + ctxt->op_bytes);
  3245. }
  3246. static int em_sgdt(struct x86_emulate_ctxt *ctxt)
  3247. {
  3248. return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt);
  3249. }
  3250. static int em_sidt(struct x86_emulate_ctxt *ctxt)
  3251. {
  3252. return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt);
  3253. }
  3254. static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
  3255. {
  3256. struct desc_ptr desc_ptr;
  3257. int rc;
  3258. if (ctxt->mode == X86EMUL_MODE_PROT64)
  3259. ctxt->op_bytes = 8;
  3260. rc = read_descriptor(ctxt, ctxt->src.addr.mem,
  3261. &desc_ptr.size, &desc_ptr.address,
  3262. ctxt->op_bytes);
  3263. if (rc != X86EMUL_CONTINUE)
  3264. return rc;
  3265. if (ctxt->mode == X86EMUL_MODE_PROT64 &&
  3266. emul_is_noncanonical_address(desc_ptr.address, ctxt))
  3267. return emulate_gp(ctxt, 0);
  3268. if (lgdt)
  3269. ctxt->ops->set_gdt(ctxt, &desc_ptr);
  3270. else
  3271. ctxt->ops->set_idt(ctxt, &desc_ptr);
  3272. /* Disable writeback. */
  3273. ctxt->dst.type = OP_NONE;
  3274. return X86EMUL_CONTINUE;
  3275. }
  3276. static int em_lgdt(struct x86_emulate_ctxt *ctxt)
  3277. {
  3278. return em_lgdt_lidt(ctxt, true);
  3279. }
  3280. static int em_lidt(struct x86_emulate_ctxt *ctxt)
  3281. {
  3282. return em_lgdt_lidt(ctxt, false);
  3283. }
  3284. static int em_smsw(struct x86_emulate_ctxt *ctxt)
  3285. {
  3286. if ((ctxt->ops->get_cr(ctxt, 4) & X86_CR4_UMIP) &&
  3287. ctxt->ops->cpl(ctxt) > 0)
  3288. return emulate_gp(ctxt, 0);
  3289. if (ctxt->dst.type == OP_MEM)
  3290. ctxt->dst.bytes = 2;
  3291. ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0);
  3292. return X86EMUL_CONTINUE;
  3293. }
  3294. static int em_lmsw(struct x86_emulate_ctxt *ctxt)
  3295. {
  3296. ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul)
  3297. | (ctxt->src.val & 0x0f));
  3298. ctxt->dst.type = OP_NONE;
  3299. return X86EMUL_CONTINUE;
  3300. }
  3301. static int em_loop(struct x86_emulate_ctxt *ctxt)
  3302. {
  3303. int rc = X86EMUL_CONTINUE;
  3304. register_address_increment(ctxt, VCPU_REGS_RCX, -1);
  3305. if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) &&
  3306. (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags)))
  3307. rc = jmp_rel(ctxt, ctxt->src.val);
  3308. return rc;
  3309. }
  3310. static int em_jcxz(struct x86_emulate_ctxt *ctxt)
  3311. {
  3312. int rc = X86EMUL_CONTINUE;
  3313. if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0)
  3314. rc = jmp_rel(ctxt, ctxt->src.val);
  3315. return rc;
  3316. }
  3317. static int em_in(struct x86_emulate_ctxt *ctxt)
  3318. {
  3319. if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val,
  3320. &ctxt->dst.val))
  3321. return X86EMUL_IO_NEEDED;
  3322. return X86EMUL_CONTINUE;
  3323. }
  3324. static int em_out(struct x86_emulate_ctxt *ctxt)
  3325. {
  3326. ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val,
  3327. &ctxt->src.val, 1);
  3328. /* Disable writeback. */
  3329. ctxt->dst.type = OP_NONE;
  3330. return X86EMUL_CONTINUE;
  3331. }
  3332. static int em_cli(struct x86_emulate_ctxt *ctxt)
  3333. {
  3334. if (emulator_bad_iopl(ctxt))
  3335. return emulate_gp(ctxt, 0);
  3336. ctxt->eflags &= ~X86_EFLAGS_IF;
  3337. return X86EMUL_CONTINUE;
  3338. }
  3339. static int em_sti(struct x86_emulate_ctxt *ctxt)
  3340. {
  3341. if (emulator_bad_iopl(ctxt))
  3342. return emulate_gp(ctxt, 0);
  3343. ctxt->interruptibility = KVM_X86_SHADOW_INT_STI;
  3344. ctxt->eflags |= X86_EFLAGS_IF;
  3345. return X86EMUL_CONTINUE;
  3346. }
  3347. static int em_cpuid(struct x86_emulate_ctxt *ctxt)
  3348. {
  3349. u32 eax, ebx, ecx, edx;
  3350. u64 msr = 0;
  3351. ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
  3352. if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
  3353. ctxt->ops->cpl(ctxt)) {
  3354. return emulate_gp(ctxt, 0);
  3355. }
  3356. eax = reg_read(ctxt, VCPU_REGS_RAX);
  3357. ecx = reg_read(ctxt, VCPU_REGS_RCX);
  3358. ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, true);
  3359. *reg_write(ctxt, VCPU_REGS_RAX) = eax;
  3360. *reg_write(ctxt, VCPU_REGS_RBX) = ebx;
  3361. *reg_write(ctxt, VCPU_REGS_RCX) = ecx;
  3362. *reg_write(ctxt, VCPU_REGS_RDX) = edx;
  3363. return X86EMUL_CONTINUE;
  3364. }
  3365. static int em_sahf(struct x86_emulate_ctxt *ctxt)
  3366. {
  3367. u32 flags;
  3368. flags = X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  3369. X86_EFLAGS_SF;
  3370. flags &= *reg_rmw(ctxt, VCPU_REGS_RAX) >> 8;
  3371. ctxt->eflags &= ~0xffUL;
  3372. ctxt->eflags |= flags | X86_EFLAGS_FIXED;
  3373. return X86EMUL_CONTINUE;
  3374. }
  3375. static int em_lahf(struct x86_emulate_ctxt *ctxt)
  3376. {
  3377. *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL;
  3378. *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8;
  3379. return X86EMUL_CONTINUE;
  3380. }
  3381. static int em_bswap(struct x86_emulate_ctxt *ctxt)
  3382. {
  3383. switch (ctxt->op_bytes) {
  3384. #ifdef CONFIG_X86_64
  3385. case 8:
  3386. asm("bswap %0" : "+r"(ctxt->dst.val));
  3387. break;
  3388. #endif
  3389. default:
  3390. asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val));
  3391. break;
  3392. }
  3393. return X86EMUL_CONTINUE;
  3394. }
  3395. static int em_clflush(struct x86_emulate_ctxt *ctxt)
  3396. {
  3397. /* emulating clflush regardless of cpuid */
  3398. return X86EMUL_CONTINUE;
  3399. }
  3400. static int em_movsxd(struct x86_emulate_ctxt *ctxt)
  3401. {
  3402. ctxt->dst.val = (s32) ctxt->src.val;
  3403. return X86EMUL_CONTINUE;
  3404. }
  3405. static int check_fxsr(struct x86_emulate_ctxt *ctxt)
  3406. {
  3407. u32 eax = 1, ebx, ecx = 0, edx;
  3408. ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx, false);
  3409. if (!(edx & FFL(FXSR)))
  3410. return emulate_ud(ctxt);
  3411. if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM))
  3412. return emulate_nm(ctxt);
  3413. /*
  3414. * Don't emulate a case that should never be hit, instead of working
  3415. * around a lack of fxsave64/fxrstor64 on old compilers.
  3416. */
  3417. if (ctxt->mode >= X86EMUL_MODE_PROT64)
  3418. return X86EMUL_UNHANDLEABLE;
  3419. return X86EMUL_CONTINUE;
  3420. }
  3421. /*
  3422. * Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
  3423. * and restore MXCSR.
  3424. */
  3425. static size_t __fxstate_size(int nregs)
  3426. {
  3427. return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
  3428. }
  3429. static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
  3430. {
  3431. bool cr4_osfxsr;
  3432. if (ctxt->mode == X86EMUL_MODE_PROT64)
  3433. return __fxstate_size(16);
  3434. cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
  3435. return __fxstate_size(cr4_osfxsr ? 8 : 0);
  3436. }
  3437. /*
  3438. * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
  3439. * 1) 16 bit mode
  3440. * 2) 32 bit mode
  3441. * - like (1), but FIP and FDP (foo) are only 16 bit. At least Intel CPUs
  3442. * preserve whole 32 bit values, though, so (1) and (2) are the same wrt.
  3443. * save and restore
  3444. * 3) 64-bit mode with REX.W prefix
  3445. * - like (2), but XMM 8-15 are being saved and restored
  3446. * 4) 64-bit mode without REX.W prefix
  3447. * - like (3), but FIP and FDP are 64 bit
  3448. *
  3449. * Emulation uses (3) for (1) and (2) and preserves XMM 8-15 to reach the
  3450. * desired result. (4) is not emulated.
  3451. *
  3452. * Note: Guest and host CPUID.(EAX=07H,ECX=0H):EBX[bit 13] (deprecate FPU CS
  3453. * and FPU DS) should match.
  3454. */
  3455. static int em_fxsave(struct x86_emulate_ctxt *ctxt)
  3456. {
  3457. struct fxregs_state fx_state;
  3458. int rc;
  3459. rc = check_fxsr(ctxt);
  3460. if (rc != X86EMUL_CONTINUE)
  3461. return rc;
  3462. rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
  3463. if (rc != X86EMUL_CONTINUE)
  3464. return rc;
  3465. return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
  3466. fxstate_size(ctxt));
  3467. }
  3468. /*
  3469. * FXRSTOR might restore XMM registers not provided by the guest. Fill
  3470. * in the host registers (via FXSAVE) instead, so they won't be modified.
  3471. * (preemption has to stay disabled until FXRSTOR).
  3472. *
  3473. * Use noinline to keep the stack for other functions called by callers small.
  3474. */
  3475. static noinline int fxregs_fixup(struct fxregs_state *fx_state,
  3476. const size_t used_size)
  3477. {
  3478. struct fxregs_state fx_tmp;
  3479. int rc;
  3480. rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_tmp));
  3481. memcpy((void *)fx_state + used_size, (void *)&fx_tmp + used_size,
  3482. __fxstate_size(16) - used_size);
  3483. return rc;
  3484. }
  3485. static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
  3486. {
  3487. struct fxregs_state fx_state;
  3488. int rc;
  3489. size_t size;
  3490. rc = check_fxsr(ctxt);
  3491. if (rc != X86EMUL_CONTINUE)
  3492. return rc;
  3493. size = fxstate_size(ctxt);
  3494. rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
  3495. if (rc != X86EMUL_CONTINUE)
  3496. return rc;
  3497. if (size < __fxstate_size(16)) {
  3498. rc = fxregs_fixup(&fx_state, size);
  3499. if (rc != X86EMUL_CONTINUE)
  3500. goto out;
  3501. }
  3502. if (fx_state.mxcsr >> 16) {
  3503. rc = emulate_gp(ctxt, 0);
  3504. goto out;
  3505. }
  3506. if (rc == X86EMUL_CONTINUE)
  3507. rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
  3508. out:
  3509. return rc;
  3510. }
  3511. static bool valid_cr(int nr)
  3512. {
  3513. switch (nr) {
  3514. case 0:
  3515. case 2 ... 4:
  3516. case 8:
  3517. return true;
  3518. default:
  3519. return false;
  3520. }
  3521. }
  3522. static int check_cr_read(struct x86_emulate_ctxt *ctxt)
  3523. {
  3524. if (!valid_cr(ctxt->modrm_reg))
  3525. return emulate_ud(ctxt);
  3526. return X86EMUL_CONTINUE;
  3527. }
  3528. static int check_cr_write(struct x86_emulate_ctxt *ctxt)
  3529. {
  3530. u64 new_val = ctxt->src.val64;
  3531. int cr = ctxt->modrm_reg;
  3532. u64 efer = 0;
  3533. static u64 cr_reserved_bits[] = {
  3534. 0xffffffff00000000ULL,
  3535. 0, 0, 0, /* CR3 checked later */
  3536. CR4_RESERVED_BITS,
  3537. 0, 0, 0,
  3538. CR8_RESERVED_BITS,
  3539. };
  3540. if (!valid_cr(cr))
  3541. return emulate_ud(ctxt);
  3542. if (new_val & cr_reserved_bits[cr])
  3543. return emulate_gp(ctxt, 0);
  3544. switch (cr) {
  3545. case 0: {
  3546. u64 cr4;
  3547. if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
  3548. ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
  3549. return emulate_gp(ctxt, 0);
  3550. cr4 = ctxt->ops->get_cr(ctxt, 4);
  3551. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  3552. if ((new_val & X86_CR0_PG) && (efer & EFER_LME) &&
  3553. !(cr4 & X86_CR4_PAE))
  3554. return emulate_gp(ctxt, 0);
  3555. break;
  3556. }
  3557. case 3: {
  3558. u64 rsvd = 0;
  3559. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  3560. if (efer & EFER_LMA) {
  3561. u64 maxphyaddr;
  3562. u32 eax, ebx, ecx, edx;
  3563. eax = 0x80000008;
  3564. ecx = 0;
  3565. if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
  3566. &edx, false))
  3567. maxphyaddr = eax & 0xff;
  3568. else
  3569. maxphyaddr = 36;
  3570. rsvd = rsvd_bits(maxphyaddr, 63);
  3571. if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PCIDE)
  3572. rsvd &= ~X86_CR3_PCID_NOFLUSH;
  3573. }
  3574. if (new_val & rsvd)
  3575. return emulate_gp(ctxt, 0);
  3576. break;
  3577. }
  3578. case 4: {
  3579. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  3580. if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE))
  3581. return emulate_gp(ctxt, 0);
  3582. break;
  3583. }
  3584. }
  3585. return X86EMUL_CONTINUE;
  3586. }
  3587. static int check_dr7_gd(struct x86_emulate_ctxt *ctxt)
  3588. {
  3589. unsigned long dr7;
  3590. ctxt->ops->get_dr(ctxt, 7, &dr7);
  3591. /* Check if DR7.Global_Enable is set */
  3592. return dr7 & (1 << 13);
  3593. }
  3594. static int check_dr_read(struct x86_emulate_ctxt *ctxt)
  3595. {
  3596. int dr = ctxt->modrm_reg;
  3597. u64 cr4;
  3598. if (dr > 7)
  3599. return emulate_ud(ctxt);
  3600. cr4 = ctxt->ops->get_cr(ctxt, 4);
  3601. if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5))
  3602. return emulate_ud(ctxt);
  3603. if (check_dr7_gd(ctxt)) {
  3604. ulong dr6;
  3605. ctxt->ops->get_dr(ctxt, 6, &dr6);
  3606. dr6 &= ~15;
  3607. dr6 |= DR6_BD | DR6_RTM;
  3608. ctxt->ops->set_dr(ctxt, 6, dr6);
  3609. return emulate_db(ctxt);
  3610. }
  3611. return X86EMUL_CONTINUE;
  3612. }
  3613. static int check_dr_write(struct x86_emulate_ctxt *ctxt)
  3614. {
  3615. u64 new_val = ctxt->src.val64;
  3616. int dr = ctxt->modrm_reg;
  3617. if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL))
  3618. return emulate_gp(ctxt, 0);
  3619. return check_dr_read(ctxt);
  3620. }
  3621. static int check_svme(struct x86_emulate_ctxt *ctxt)
  3622. {
  3623. u64 efer = 0;
  3624. ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
  3625. if (!(efer & EFER_SVME))
  3626. return emulate_ud(ctxt);
  3627. return X86EMUL_CONTINUE;
  3628. }
  3629. static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
  3630. {
  3631. u64 rax = reg_read(ctxt, VCPU_REGS_RAX);
  3632. /* Valid physical address? */
  3633. if (rax & 0xffff000000000000ULL)
  3634. return emulate_gp(ctxt, 0);
  3635. return check_svme(ctxt);
  3636. }
  3637. static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
  3638. {
  3639. u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
  3640. if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt))
  3641. return emulate_ud(ctxt);
  3642. return X86EMUL_CONTINUE;
  3643. }
  3644. static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
  3645. {
  3646. u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
  3647. u64 rcx = reg_read(ctxt, VCPU_REGS_RCX);
  3648. /*
  3649. * VMware allows access to these Pseduo-PMCs even when read via RDPMC
  3650. * in Ring3 when CR4.PCE=0.
  3651. */
  3652. if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx))
  3653. return X86EMUL_CONTINUE;
  3654. if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
  3655. ctxt->ops->check_pmc(ctxt, rcx))
  3656. return emulate_gp(ctxt, 0);
  3657. return X86EMUL_CONTINUE;
  3658. }
  3659. static int check_perm_in(struct x86_emulate_ctxt *ctxt)
  3660. {
  3661. ctxt->dst.bytes = min(ctxt->dst.bytes, 4u);
  3662. if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes))
  3663. return emulate_gp(ctxt, 0);
  3664. return X86EMUL_CONTINUE;
  3665. }
  3666. static int check_perm_out(struct x86_emulate_ctxt *ctxt)
  3667. {
  3668. ctxt->src.bytes = min(ctxt->src.bytes, 4u);
  3669. if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes))
  3670. return emulate_gp(ctxt, 0);
  3671. return X86EMUL_CONTINUE;
  3672. }
  3673. #define D(_y) { .flags = (_y) }
  3674. #define DI(_y, _i) { .flags = (_y)|Intercept, .intercept = x86_intercept_##_i }
  3675. #define DIP(_y, _i, _p) { .flags = (_y)|Intercept|CheckPerm, \
  3676. .intercept = x86_intercept_##_i, .check_perm = (_p) }
  3677. #define N D(NotImpl)
  3678. #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
  3679. #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) }
  3680. #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) }
  3681. #define ID(_f, _i) { .flags = ((_f) | InstrDual | ModRM), .u.idual = (_i) }
  3682. #define MD(_f, _m) { .flags = ((_f) | ModeDual), .u.mdual = (_m) }
  3683. #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) }
  3684. #define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
  3685. #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) }
  3686. #define II(_f, _e, _i) \
  3687. { .flags = (_f)|Intercept, .u.execute = (_e), .intercept = x86_intercept_##_i }
  3688. #define IIP(_f, _e, _i, _p) \
  3689. { .flags = (_f)|Intercept|CheckPerm, .u.execute = (_e), \
  3690. .intercept = x86_intercept_##_i, .check_perm = (_p) }
  3691. #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) }
  3692. #define D2bv(_f) D((_f) | ByteOp), D(_f)
  3693. #define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p)
  3694. #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e)
  3695. #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e)
  3696. #define I2bvIP(_f, _e, _i, _p) \
  3697. IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p)
  3698. #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \
  3699. F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \
  3700. F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e)
  3701. static const struct opcode group7_rm0[] = {
  3702. N,
  3703. I(SrcNone | Priv | EmulateOnUD, em_hypercall),
  3704. N, N, N, N, N, N,
  3705. };
  3706. static const struct opcode group7_rm1[] = {
  3707. DI(SrcNone | Priv, monitor),
  3708. DI(SrcNone | Priv, mwait),
  3709. N, N, N, N, N, N,
  3710. };
  3711. static const struct opcode group7_rm3[] = {
  3712. DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa),
  3713. II(SrcNone | Prot | EmulateOnUD, em_hypercall, vmmcall),
  3714. DIP(SrcNone | Prot | Priv, vmload, check_svme_pa),
  3715. DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa),
  3716. DIP(SrcNone | Prot | Priv, stgi, check_svme),
  3717. DIP(SrcNone | Prot | Priv, clgi, check_svme),
  3718. DIP(SrcNone | Prot | Priv, skinit, check_svme),
  3719. DIP(SrcNone | Prot | Priv, invlpga, check_svme),
  3720. };
  3721. static const struct opcode group7_rm7[] = {
  3722. N,
  3723. DIP(SrcNone, rdtscp, check_rdtsc),
  3724. N, N, N, N, N, N,
  3725. };
  3726. static const struct opcode group1[] = {
  3727. F(Lock, em_add),
  3728. F(Lock | PageTable, em_or),
  3729. F(Lock, em_adc),
  3730. F(Lock, em_sbb),
  3731. F(Lock | PageTable, em_and),
  3732. F(Lock, em_sub),
  3733. F(Lock, em_xor),
  3734. F(NoWrite, em_cmp),
  3735. };
  3736. static const struct opcode group1A[] = {
  3737. I(DstMem | SrcNone | Mov | Stack | IncSP | TwoMemOp, em_pop), N, N, N, N, N, N, N,
  3738. };
  3739. static const struct opcode group2[] = {
  3740. F(DstMem | ModRM, em_rol),
  3741. F(DstMem | ModRM, em_ror),
  3742. F(DstMem | ModRM, em_rcl),
  3743. F(DstMem | ModRM, em_rcr),
  3744. F(DstMem | ModRM, em_shl),
  3745. F(DstMem | ModRM, em_shr),
  3746. F(DstMem | ModRM, em_shl),
  3747. F(DstMem | ModRM, em_sar),
  3748. };
  3749. static const struct opcode group3[] = {
  3750. F(DstMem | SrcImm | NoWrite, em_test),
  3751. F(DstMem | SrcImm | NoWrite, em_test),
  3752. F(DstMem | SrcNone | Lock, em_not),
  3753. F(DstMem | SrcNone | Lock, em_neg),
  3754. F(DstXacc | Src2Mem, em_mul_ex),
  3755. F(DstXacc | Src2Mem, em_imul_ex),
  3756. F(DstXacc | Src2Mem, em_div_ex),
  3757. F(DstXacc | Src2Mem, em_idiv_ex),
  3758. };
  3759. static const struct opcode group4[] = {
  3760. F(ByteOp | DstMem | SrcNone | Lock, em_inc),
  3761. F(ByteOp | DstMem | SrcNone | Lock, em_dec),
  3762. N, N, N, N, N, N,
  3763. };
  3764. static const struct opcode group5[] = {
  3765. F(DstMem | SrcNone | Lock, em_inc),
  3766. F(DstMem | SrcNone | Lock, em_dec),
  3767. I(SrcMem | NearBranch, em_call_near_abs),
  3768. I(SrcMemFAddr | ImplicitOps, em_call_far),
  3769. I(SrcMem | NearBranch, em_jmp_abs),
  3770. I(SrcMemFAddr | ImplicitOps, em_jmp_far),
  3771. I(SrcMem | Stack | TwoMemOp, em_push), D(Undefined),
  3772. };
  3773. static const struct opcode group6[] = {
  3774. II(Prot | DstMem, em_sldt, sldt),
  3775. II(Prot | DstMem, em_str, str),
  3776. II(Prot | Priv | SrcMem16, em_lldt, lldt),
  3777. II(Prot | Priv | SrcMem16, em_ltr, ltr),
  3778. N, N, N, N,
  3779. };
  3780. static const struct group_dual group7 = { {
  3781. II(Mov | DstMem, em_sgdt, sgdt),
  3782. II(Mov | DstMem, em_sidt, sidt),
  3783. II(SrcMem | Priv, em_lgdt, lgdt),
  3784. II(SrcMem | Priv, em_lidt, lidt),
  3785. II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
  3786. II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
  3787. II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg),
  3788. }, {
  3789. EXT(0, group7_rm0),
  3790. EXT(0, group7_rm1),
  3791. N, EXT(0, group7_rm3),
  3792. II(SrcNone | DstMem | Mov, em_smsw, smsw), N,
  3793. II(SrcMem16 | Mov | Priv, em_lmsw, lmsw),
  3794. EXT(0, group7_rm7),
  3795. } };
  3796. static const struct opcode group8[] = {
  3797. N, N, N, N,
  3798. F(DstMem | SrcImmByte | NoWrite, em_bt),
  3799. F(DstMem | SrcImmByte | Lock | PageTable, em_bts),
  3800. F(DstMem | SrcImmByte | Lock, em_btr),
  3801. F(DstMem | SrcImmByte | Lock | PageTable, em_btc),
  3802. };
  3803. /*
  3804. * The "memory" destination is actually always a register, since we come
  3805. * from the register case of group9.
  3806. */
  3807. static const struct gprefix pfx_0f_c7_7 = {
  3808. N, N, N, II(DstMem | ModRM | Op3264 | EmulateOnUD, em_rdpid, rdtscp),
  3809. };
  3810. static const struct group_dual group9 = { {
  3811. N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N,
  3812. }, {
  3813. N, N, N, N, N, N, N,
  3814. GP(0, &pfx_0f_c7_7),
  3815. } };
  3816. static const struct opcode group11[] = {
  3817. I(DstMem | SrcImm | Mov | PageTable, em_mov),
  3818. X7(D(Undefined)),
  3819. };
  3820. static const struct gprefix pfx_0f_ae_7 = {
  3821. I(SrcMem | ByteOp, em_clflush), N, N, N,
  3822. };
  3823. static const struct group_dual group15 = { {
  3824. I(ModRM | Aligned16, em_fxsave),
  3825. I(ModRM | Aligned16, em_fxrstor),
  3826. N, N, N, N, N, GP(0, &pfx_0f_ae_7),
  3827. }, {
  3828. N, N, N, N, N, N, N, N,
  3829. } };
  3830. static const struct gprefix pfx_0f_6f_0f_7f = {
  3831. I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
  3832. };
  3833. static const struct instr_dual instr_dual_0f_2b = {
  3834. I(0, em_mov), N
  3835. };
  3836. static const struct gprefix pfx_0f_2b = {
  3837. ID(0, &instr_dual_0f_2b), ID(0, &instr_dual_0f_2b), N, N,
  3838. };
  3839. static const struct gprefix pfx_0f_10_0f_11 = {
  3840. I(Unaligned, em_mov), I(Unaligned, em_mov), N, N,
  3841. };
  3842. static const struct gprefix pfx_0f_28_0f_29 = {
  3843. I(Aligned, em_mov), I(Aligned, em_mov), N, N,
  3844. };
  3845. static const struct gprefix pfx_0f_e7 = {
  3846. N, I(Sse, em_mov), N, N,
  3847. };
  3848. static const struct escape escape_d9 = { {
  3849. N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstcw),
  3850. }, {
  3851. /* 0xC0 - 0xC7 */
  3852. N, N, N, N, N, N, N, N,
  3853. /* 0xC8 - 0xCF */
  3854. N, N, N, N, N, N, N, N,
  3855. /* 0xD0 - 0xC7 */
  3856. N, N, N, N, N, N, N, N,
  3857. /* 0xD8 - 0xDF */
  3858. N, N, N, N, N, N, N, N,
  3859. /* 0xE0 - 0xE7 */
  3860. N, N, N, N, N, N, N, N,
  3861. /* 0xE8 - 0xEF */
  3862. N, N, N, N, N, N, N, N,
  3863. /* 0xF0 - 0xF7 */
  3864. N, N, N, N, N, N, N, N,
  3865. /* 0xF8 - 0xFF */
  3866. N, N, N, N, N, N, N, N,
  3867. } };
  3868. static const struct escape escape_db = { {
  3869. N, N, N, N, N, N, N, N,
  3870. }, {
  3871. /* 0xC0 - 0xC7 */
  3872. N, N, N, N, N, N, N, N,
  3873. /* 0xC8 - 0xCF */
  3874. N, N, N, N, N, N, N, N,
  3875. /* 0xD0 - 0xC7 */
  3876. N, N, N, N, N, N, N, N,
  3877. /* 0xD8 - 0xDF */
  3878. N, N, N, N, N, N, N, N,
  3879. /* 0xE0 - 0xE7 */
  3880. N, N, N, I(ImplicitOps, em_fninit), N, N, N, N,
  3881. /* 0xE8 - 0xEF */
  3882. N, N, N, N, N, N, N, N,
  3883. /* 0xF0 - 0xF7 */
  3884. N, N, N, N, N, N, N, N,
  3885. /* 0xF8 - 0xFF */
  3886. N, N, N, N, N, N, N, N,
  3887. } };
  3888. static const struct escape escape_dd = { {
  3889. N, N, N, N, N, N, N, I(DstMem16 | Mov, em_fnstsw),
  3890. }, {
  3891. /* 0xC0 - 0xC7 */
  3892. N, N, N, N, N, N, N, N,
  3893. /* 0xC8 - 0xCF */
  3894. N, N, N, N, N, N, N, N,
  3895. /* 0xD0 - 0xC7 */
  3896. N, N, N, N, N, N, N, N,
  3897. /* 0xD8 - 0xDF */
  3898. N, N, N, N, N, N, N, N,
  3899. /* 0xE0 - 0xE7 */
  3900. N, N, N, N, N, N, N, N,
  3901. /* 0xE8 - 0xEF */
  3902. N, N, N, N, N, N, N, N,
  3903. /* 0xF0 - 0xF7 */
  3904. N, N, N, N, N, N, N, N,
  3905. /* 0xF8 - 0xFF */
  3906. N, N, N, N, N, N, N, N,
  3907. } };
  3908. static const struct instr_dual instr_dual_0f_c3 = {
  3909. I(DstMem | SrcReg | ModRM | No16 | Mov, em_mov), N
  3910. };
  3911. static const struct mode_dual mode_dual_63 = {
  3912. N, I(DstReg | SrcMem32 | ModRM | Mov, em_movsxd)
  3913. };
  3914. static const struct opcode opcode_table[256] = {
  3915. /* 0x00 - 0x07 */
  3916. F6ALU(Lock, em_add),
  3917. I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg),
  3918. I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg),
  3919. /* 0x08 - 0x0F */
  3920. F6ALU(Lock | PageTable, em_or),
  3921. I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg),
  3922. N,
  3923. /* 0x10 - 0x17 */
  3924. F6ALU(Lock, em_adc),
  3925. I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg),
  3926. I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg),
  3927. /* 0x18 - 0x1F */
  3928. F6ALU(Lock, em_sbb),
  3929. I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg),
  3930. I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg),
  3931. /* 0x20 - 0x27 */
  3932. F6ALU(Lock | PageTable, em_and), N, N,
  3933. /* 0x28 - 0x2F */
  3934. F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das),
  3935. /* 0x30 - 0x37 */
  3936. F6ALU(Lock, em_xor), N, N,
  3937. /* 0x38 - 0x3F */
  3938. F6ALU(NoWrite, em_cmp), N, N,
  3939. /* 0x40 - 0x4F */
  3940. X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)),
  3941. /* 0x50 - 0x57 */
  3942. X8(I(SrcReg | Stack, em_push)),
  3943. /* 0x58 - 0x5F */
  3944. X8(I(DstReg | Stack, em_pop)),
  3945. /* 0x60 - 0x67 */
  3946. I(ImplicitOps | Stack | No64, em_pusha),
  3947. I(ImplicitOps | Stack | No64, em_popa),
  3948. N, MD(ModRM, &mode_dual_63),
  3949. N, N, N, N,
  3950. /* 0x68 - 0x6F */
  3951. I(SrcImm | Mov | Stack, em_push),
  3952. I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op),
  3953. I(SrcImmByte | Mov | Stack, em_push),
  3954. I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op),
  3955. I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */
  3956. I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */
  3957. /* 0x70 - 0x7F */
  3958. X16(D(SrcImmByte | NearBranch)),
  3959. /* 0x80 - 0x87 */
  3960. G(ByteOp | DstMem | SrcImm, group1),
  3961. G(DstMem | SrcImm, group1),
  3962. G(ByteOp | DstMem | SrcImm | No64, group1),
  3963. G(DstMem | SrcImmByte, group1),
  3964. F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test),
  3965. I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg),
  3966. /* 0x88 - 0x8F */
  3967. I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),
  3968. I2bv(DstReg | SrcMem | ModRM | Mov, em_mov),
  3969. I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg),
  3970. D(ModRM | SrcMem | NoAccess | DstReg),
  3971. I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm),
  3972. G(0, group1A),
  3973. /* 0x90 - 0x97 */
  3974. DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)),
  3975. /* 0x98 - 0x9F */
  3976. D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
  3977. I(SrcImmFAddr | No64, em_call_far), N,
  3978. II(ImplicitOps | Stack, em_pushf, pushf),
  3979. II(ImplicitOps | Stack, em_popf, popf),
  3980. I(ImplicitOps, em_sahf), I(ImplicitOps, em_lahf),
  3981. /* 0xA0 - 0xA7 */
  3982. I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
  3983. I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov),
  3984. I2bv(SrcSI | DstDI | Mov | String | TwoMemOp, em_mov),
  3985. F2bv(SrcSI | DstDI | String | NoWrite | TwoMemOp, em_cmp_r),
  3986. /* 0xA8 - 0xAF */
  3987. F2bv(DstAcc | SrcImm | NoWrite, em_test),
  3988. I2bv(SrcAcc | DstDI | Mov | String, em_mov),
  3989. I2bv(SrcSI | DstAcc | Mov | String, em_mov),
  3990. F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp_r),
  3991. /* 0xB0 - 0xB7 */
  3992. X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)),
  3993. /* 0xB8 - 0xBF */
  3994. X8(I(DstReg | SrcImm64 | Mov, em_mov)),
  3995. /* 0xC0 - 0xC7 */
  3996. G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2),
  3997. I(ImplicitOps | NearBranch | SrcImmU16, em_ret_near_imm),
  3998. I(ImplicitOps | NearBranch, em_ret),
  3999. I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg),
  4000. I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg),
  4001. G(ByteOp, group11), G(0, group11),
  4002. /* 0xC8 - 0xCF */
  4003. I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave),
  4004. I(ImplicitOps | SrcImmU16, em_ret_far_imm),
  4005. I(ImplicitOps, em_ret_far),
  4006. D(ImplicitOps), DI(SrcImmByte, intn),
  4007. D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret),
  4008. /* 0xD0 - 0xD7 */
  4009. G(Src2One | ByteOp, group2), G(Src2One, group2),
  4010. G(Src2CL | ByteOp, group2), G(Src2CL, group2),
  4011. I(DstAcc | SrcImmUByte | No64, em_aam),
  4012. I(DstAcc | SrcImmUByte | No64, em_aad),
  4013. F(DstAcc | ByteOp | No64, em_salc),
  4014. I(DstAcc | SrcXLat | ByteOp, em_mov),
  4015. /* 0xD8 - 0xDF */
  4016. N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N,
  4017. /* 0xE0 - 0xE7 */
  4018. X3(I(SrcImmByte | NearBranch, em_loop)),
  4019. I(SrcImmByte | NearBranch, em_jcxz),
  4020. I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in),
  4021. I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out),
  4022. /* 0xE8 - 0xEF */
  4023. I(SrcImm | NearBranch, em_call), D(SrcImm | ImplicitOps | NearBranch),
  4024. I(SrcImmFAddr | No64, em_jmp_far),
  4025. D(SrcImmByte | ImplicitOps | NearBranch),
  4026. I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in),
  4027. I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out),
  4028. /* 0xF0 - 0xF7 */
  4029. N, DI(ImplicitOps, icebp), N, N,
  4030. DI(ImplicitOps | Priv, hlt), D(ImplicitOps),
  4031. G(ByteOp, group3), G(0, group3),
  4032. /* 0xF8 - 0xFF */
  4033. D(ImplicitOps), D(ImplicitOps),
  4034. I(ImplicitOps, em_cli), I(ImplicitOps, em_sti),
  4035. D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5),
  4036. };
  4037. static const struct opcode twobyte_table[256] = {
  4038. /* 0x00 - 0x0F */
  4039. G(0, group6), GD(0, &group7), N, N,
  4040. N, I(ImplicitOps | EmulateOnUD, em_syscall),
  4041. II(ImplicitOps | Priv, em_clts, clts), N,
  4042. DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N,
  4043. N, D(ImplicitOps | ModRM | SrcMem | NoAccess), N, N,
  4044. /* 0x10 - 0x1F */
  4045. GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_10_0f_11),
  4046. GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_10_0f_11),
  4047. N, N, N, N, N, N,
  4048. D(ImplicitOps | ModRM | SrcMem | NoAccess),
  4049. N, N, N, N, N, N, D(ImplicitOps | ModRM | SrcMem | NoAccess),
  4050. /* 0x20 - 0x2F */
  4051. DIP(ModRM | DstMem | Priv | Op3264 | NoMod, cr_read, check_cr_read),
  4052. DIP(ModRM | DstMem | Priv | Op3264 | NoMod, dr_read, check_dr_read),
  4053. IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_cr_write, cr_write,
  4054. check_cr_write),
  4055. IIP(ModRM | SrcMem | Priv | Op3264 | NoMod, em_dr_write, dr_write,
  4056. check_dr_write),
  4057. N, N, N, N,
  4058. GP(ModRM | DstReg | SrcMem | Mov | Sse, &pfx_0f_28_0f_29),
  4059. GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_28_0f_29),
  4060. N, GP(ModRM | DstMem | SrcReg | Mov | Sse, &pfx_0f_2b),
  4061. N, N, N, N,
  4062. /* 0x30 - 0x3F */
  4063. II(ImplicitOps | Priv, em_wrmsr, wrmsr),
  4064. IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
  4065. II(ImplicitOps | Priv, em_rdmsr, rdmsr),
  4066. IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),
  4067. I(ImplicitOps | EmulateOnUD, em_sysenter),
  4068. I(ImplicitOps | Priv | EmulateOnUD, em_sysexit),
  4069. N, N,
  4070. N, N, N, N, N, N, N, N,
  4071. /* 0x40 - 0x4F */
  4072. X16(D(DstReg | SrcMem | ModRM)),
  4073. /* 0x50 - 0x5F */
  4074. N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
  4075. /* 0x60 - 0x6F */
  4076. N, N, N, N,
  4077. N, N, N, N,
  4078. N, N, N, N,
  4079. N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),
  4080. /* 0x70 - 0x7F */
  4081. N, N, N, N,
  4082. N, N, N, N,
  4083. N, N, N, N,
  4084. N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),
  4085. /* 0x80 - 0x8F */
  4086. X16(D(SrcImm | NearBranch)),
  4087. /* 0x90 - 0x9F */
  4088. X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
  4089. /* 0xA0 - 0xA7 */
  4090. I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
  4091. II(ImplicitOps, em_cpuid, cpuid),
  4092. F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
  4093. F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
  4094. F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
  4095. /* 0xA8 - 0xAF */
  4096. I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg),
  4097. II(EmulateOnUD | ImplicitOps, em_rsm, rsm),
  4098. F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts),
  4099. F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd),
  4100. F(DstMem | SrcReg | Src2CL | ModRM, em_shrd),
  4101. GD(0, &group15), F(DstReg | SrcMem | ModRM, em_imul),
  4102. /* 0xB0 - 0xB7 */
  4103. I2bv(DstMem | SrcReg | ModRM | Lock | PageTable | SrcWrite, em_cmpxchg),
  4104. I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg),
  4105. F(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr),
  4106. I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg),
  4107. I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg),
  4108. D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
  4109. /* 0xB8 - 0xBF */
  4110. N, N,
  4111. G(BitOp, group8),
  4112. F(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc),
  4113. I(DstReg | SrcMem | ModRM, em_bsf_c),
  4114. I(DstReg | SrcMem | ModRM, em_bsr_c),
  4115. D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov),
  4116. /* 0xC0 - 0xC7 */
  4117. F2bv(DstMem | SrcReg | ModRM | SrcWrite | Lock, em_xadd),
  4118. N, ID(0, &instr_dual_0f_c3),
  4119. N, N, N, GD(0, &group9),
  4120. /* 0xC8 - 0xCF */
  4121. X8(I(DstReg, em_bswap)),
  4122. /* 0xD0 - 0xDF */
  4123. N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
  4124. /* 0xE0 - 0xEF */
  4125. N, N, N, N, N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7),
  4126. N, N, N, N, N, N, N, N,
  4127. /* 0xF0 - 0xFF */
  4128. N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N
  4129. };
  4130. static const struct instr_dual instr_dual_0f_38_f0 = {
  4131. I(DstReg | SrcMem | Mov, em_movbe), N
  4132. };
  4133. static const struct instr_dual instr_dual_0f_38_f1 = {
  4134. I(DstMem | SrcReg | Mov, em_movbe), N
  4135. };
  4136. static const struct gprefix three_byte_0f_38_f0 = {
  4137. ID(0, &instr_dual_0f_38_f0), N, N, N
  4138. };
  4139. static const struct gprefix three_byte_0f_38_f1 = {
  4140. ID(0, &instr_dual_0f_38_f1), N, N, N
  4141. };
  4142. /*
  4143. * Insns below are selected by the prefix which indexed by the third opcode
  4144. * byte.
  4145. */
  4146. static const struct opcode opcode_map_0f_38[256] = {
  4147. /* 0x00 - 0x7f */
  4148. X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
  4149. /* 0x80 - 0xef */
  4150. X16(N), X16(N), X16(N), X16(N), X16(N), X16(N), X16(N),
  4151. /* 0xf0 - 0xf1 */
  4152. GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
  4153. GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
  4154. /* 0xf2 - 0xff */
  4155. N, N, X4(N), X8(N)
  4156. };
  4157. #undef D
  4158. #undef N
  4159. #undef G
  4160. #undef GD
  4161. #undef I
  4162. #undef GP
  4163. #undef EXT
  4164. #undef MD
  4165. #undef ID
  4166. #undef D2bv
  4167. #undef D2bvIP
  4168. #undef I2bv
  4169. #undef I2bvIP
  4170. #undef I6ALU
  4171. static unsigned imm_size(struct x86_emulate_ctxt *ctxt)
  4172. {
  4173. unsigned size;
  4174. size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
  4175. if (size == 8)
  4176. size = 4;
  4177. return size;
  4178. }
  4179. static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op,
  4180. unsigned size, bool sign_extension)
  4181. {
  4182. int rc = X86EMUL_CONTINUE;
  4183. op->type = OP_IMM;
  4184. op->bytes = size;
  4185. op->addr.mem.ea = ctxt->_eip;
  4186. /* NB. Immediates are sign-extended as necessary. */
  4187. switch (op->bytes) {
  4188. case 1:
  4189. op->val = insn_fetch(s8, ctxt);
  4190. break;
  4191. case 2:
  4192. op->val = insn_fetch(s16, ctxt);
  4193. break;
  4194. case 4:
  4195. op->val = insn_fetch(s32, ctxt);
  4196. break;
  4197. case 8:
  4198. op->val = insn_fetch(s64, ctxt);
  4199. break;
  4200. }
  4201. if (!sign_extension) {
  4202. switch (op->bytes) {
  4203. case 1:
  4204. op->val &= 0xff;
  4205. break;
  4206. case 2:
  4207. op->val &= 0xffff;
  4208. break;
  4209. case 4:
  4210. op->val &= 0xffffffff;
  4211. break;
  4212. }
  4213. }
  4214. done:
  4215. return rc;
  4216. }
  4217. static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,
  4218. unsigned d)
  4219. {
  4220. int rc = X86EMUL_CONTINUE;
  4221. switch (d) {
  4222. case OpReg:
  4223. decode_register_operand(ctxt, op);
  4224. break;
  4225. case OpImmUByte:
  4226. rc = decode_imm(ctxt, op, 1, false);
  4227. break;
  4228. case OpMem:
  4229. ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
  4230. mem_common:
  4231. *op = ctxt->memop;
  4232. ctxt->memopp = op;
  4233. if (ctxt->d & BitOp)
  4234. fetch_bit_operand(ctxt);
  4235. op->orig_val = op->val;
  4236. break;
  4237. case OpMem64:
  4238. ctxt->memop.bytes = (ctxt->op_bytes == 8) ? 16 : 8;
  4239. goto mem_common;
  4240. case OpAcc:
  4241. op->type = OP_REG;
  4242. op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
  4243. op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
  4244. fetch_register_operand(op);
  4245. op->orig_val = op->val;
  4246. break;
  4247. case OpAccLo:
  4248. op->type = OP_REG;
  4249. op->bytes = (ctxt->d & ByteOp) ? 2 : ctxt->op_bytes;
  4250. op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX);
  4251. fetch_register_operand(op);
  4252. op->orig_val = op->val;
  4253. break;
  4254. case OpAccHi:
  4255. if (ctxt->d & ByteOp) {
  4256. op->type = OP_NONE;
  4257. break;
  4258. }
  4259. op->type = OP_REG;
  4260. op->bytes = ctxt->op_bytes;
  4261. op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
  4262. fetch_register_operand(op);
  4263. op->orig_val = op->val;
  4264. break;
  4265. case OpDI:
  4266. op->type = OP_MEM;
  4267. op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
  4268. op->addr.mem.ea =
  4269. register_address(ctxt, VCPU_REGS_RDI);
  4270. op->addr.mem.seg = VCPU_SREG_ES;
  4271. op->val = 0;
  4272. op->count = 1;
  4273. break;
  4274. case OpDX:
  4275. op->type = OP_REG;
  4276. op->bytes = 2;
  4277. op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX);
  4278. fetch_register_operand(op);
  4279. break;
  4280. case OpCL:
  4281. op->type = OP_IMM;
  4282. op->bytes = 1;
  4283. op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff;
  4284. break;
  4285. case OpImmByte:
  4286. rc = decode_imm(ctxt, op, 1, true);
  4287. break;
  4288. case OpOne:
  4289. op->type = OP_IMM;
  4290. op->bytes = 1;
  4291. op->val = 1;
  4292. break;
  4293. case OpImm:
  4294. rc = decode_imm(ctxt, op, imm_size(ctxt), true);
  4295. break;
  4296. case OpImm64:
  4297. rc = decode_imm(ctxt, op, ctxt->op_bytes, true);
  4298. break;
  4299. case OpMem8:
  4300. ctxt->memop.bytes = 1;
  4301. if (ctxt->memop.type == OP_REG) {
  4302. ctxt->memop.addr.reg = decode_register(ctxt,
  4303. ctxt->modrm_rm, true);
  4304. fetch_register_operand(&ctxt->memop);
  4305. }
  4306. goto mem_common;
  4307. case OpMem16:
  4308. ctxt->memop.bytes = 2;
  4309. goto mem_common;
  4310. case OpMem32:
  4311. ctxt->memop.bytes = 4;
  4312. goto mem_common;
  4313. case OpImmU16:
  4314. rc = decode_imm(ctxt, op, 2, false);
  4315. break;
  4316. case OpImmU:
  4317. rc = decode_imm(ctxt, op, imm_size(ctxt), false);
  4318. break;
  4319. case OpSI:
  4320. op->type = OP_MEM;
  4321. op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
  4322. op->addr.mem.ea =
  4323. register_address(ctxt, VCPU_REGS_RSI);
  4324. op->addr.mem.seg = ctxt->seg_override;
  4325. op->val = 0;
  4326. op->count = 1;
  4327. break;
  4328. case OpXLat:
  4329. op->type = OP_MEM;
  4330. op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes;
  4331. op->addr.mem.ea =
  4332. address_mask(ctxt,
  4333. reg_read(ctxt, VCPU_REGS_RBX) +
  4334. (reg_read(ctxt, VCPU_REGS_RAX) & 0xff));
  4335. op->addr.mem.seg = ctxt->seg_override;
  4336. op->val = 0;
  4337. break;
  4338. case OpImmFAddr:
  4339. op->type = OP_IMM;
  4340. op->addr.mem.ea = ctxt->_eip;
  4341. op->bytes = ctxt->op_bytes + 2;
  4342. insn_fetch_arr(op->valptr, op->bytes, ctxt);
  4343. break;
  4344. case OpMemFAddr:
  4345. ctxt->memop.bytes = ctxt->op_bytes + 2;
  4346. goto mem_common;
  4347. case OpES:
  4348. op->type = OP_IMM;
  4349. op->val = VCPU_SREG_ES;
  4350. break;
  4351. case OpCS:
  4352. op->type = OP_IMM;
  4353. op->val = VCPU_SREG_CS;
  4354. break;
  4355. case OpSS:
  4356. op->type = OP_IMM;
  4357. op->val = VCPU_SREG_SS;
  4358. break;
  4359. case OpDS:
  4360. op->type = OP_IMM;
  4361. op->val = VCPU_SREG_DS;
  4362. break;
  4363. case OpFS:
  4364. op->type = OP_IMM;
  4365. op->val = VCPU_SREG_FS;
  4366. break;
  4367. case OpGS:
  4368. op->type = OP_IMM;
  4369. op->val = VCPU_SREG_GS;
  4370. break;
  4371. case OpImplicit:
  4372. /* Special instructions do their own operand decoding. */
  4373. default:
  4374. op->type = OP_NONE; /* Disable writeback. */
  4375. break;
  4376. }
  4377. done:
  4378. return rc;
  4379. }
  4380. int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
  4381. {
  4382. int rc = X86EMUL_CONTINUE;
  4383. int mode = ctxt->mode;
  4384. int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
  4385. bool op_prefix = false;
  4386. bool has_seg_override = false;
  4387. struct opcode opcode;
  4388. u16 dummy;
  4389. struct desc_struct desc;
  4390. ctxt->memop.type = OP_NONE;
  4391. ctxt->memopp = NULL;
  4392. ctxt->_eip = ctxt->eip;
  4393. ctxt->fetch.ptr = ctxt->fetch.data;
  4394. ctxt->fetch.end = ctxt->fetch.data + insn_len;
  4395. ctxt->opcode_len = 1;
  4396. ctxt->intercept = x86_intercept_none;
  4397. if (insn_len > 0)
  4398. memcpy(ctxt->fetch.data, insn, insn_len);
  4399. else {
  4400. rc = __do_insn_fetch_bytes(ctxt, 1);
  4401. if (rc != X86EMUL_CONTINUE)
  4402. return rc;
  4403. }
  4404. switch (mode) {
  4405. case X86EMUL_MODE_REAL:
  4406. case X86EMUL_MODE_VM86:
  4407. def_op_bytes = def_ad_bytes = 2;
  4408. ctxt->ops->get_segment(ctxt, &dummy, &desc, NULL, VCPU_SREG_CS);
  4409. if (desc.d)
  4410. def_op_bytes = def_ad_bytes = 4;
  4411. break;
  4412. case X86EMUL_MODE_PROT16:
  4413. def_op_bytes = def_ad_bytes = 2;
  4414. break;
  4415. case X86EMUL_MODE_PROT32:
  4416. def_op_bytes = def_ad_bytes = 4;
  4417. break;
  4418. #ifdef CONFIG_X86_64
  4419. case X86EMUL_MODE_PROT64:
  4420. def_op_bytes = 4;
  4421. def_ad_bytes = 8;
  4422. break;
  4423. #endif
  4424. default:
  4425. return EMULATION_FAILED;
  4426. }
  4427. ctxt->op_bytes = def_op_bytes;
  4428. ctxt->ad_bytes = def_ad_bytes;
  4429. /* Legacy prefixes. */
  4430. for (;;) {
  4431. switch (ctxt->b = insn_fetch(u8, ctxt)) {
  4432. case 0x66: /* operand-size override */
  4433. op_prefix = true;
  4434. /* switch between 2/4 bytes */
  4435. ctxt->op_bytes = def_op_bytes ^ 6;
  4436. break;
  4437. case 0x67: /* address-size override */
  4438. if (mode == X86EMUL_MODE_PROT64)
  4439. /* switch between 4/8 bytes */
  4440. ctxt->ad_bytes = def_ad_bytes ^ 12;
  4441. else
  4442. /* switch between 2/4 bytes */
  4443. ctxt->ad_bytes = def_ad_bytes ^ 6;
  4444. break;
  4445. case 0x26: /* ES override */
  4446. has_seg_override = true;
  4447. ctxt->seg_override = VCPU_SREG_ES;
  4448. break;
  4449. case 0x2e: /* CS override */
  4450. has_seg_override = true;
  4451. ctxt->seg_override = VCPU_SREG_CS;
  4452. break;
  4453. case 0x36: /* SS override */
  4454. has_seg_override = true;
  4455. ctxt->seg_override = VCPU_SREG_SS;
  4456. break;
  4457. case 0x3e: /* DS override */
  4458. has_seg_override = true;
  4459. ctxt->seg_override = VCPU_SREG_DS;
  4460. break;
  4461. case 0x64: /* FS override */
  4462. has_seg_override = true;
  4463. ctxt->seg_override = VCPU_SREG_FS;
  4464. break;
  4465. case 0x65: /* GS override */
  4466. has_seg_override = true;
  4467. ctxt->seg_override = VCPU_SREG_GS;
  4468. break;
  4469. case 0x40 ... 0x4f: /* REX */
  4470. if (mode != X86EMUL_MODE_PROT64)
  4471. goto done_prefixes;
  4472. ctxt->rex_prefix = ctxt->b;
  4473. continue;
  4474. case 0xf0: /* LOCK */
  4475. ctxt->lock_prefix = 1;
  4476. break;
  4477. case 0xf2: /* REPNE/REPNZ */
  4478. case 0xf3: /* REP/REPE/REPZ */
  4479. ctxt->rep_prefix = ctxt->b;
  4480. break;
  4481. default:
  4482. goto done_prefixes;
  4483. }
  4484. /* Any legacy prefix after a REX prefix nullifies its effect. */
  4485. ctxt->rex_prefix = 0;
  4486. }
  4487. done_prefixes:
  4488. /* REX prefix. */
  4489. if (ctxt->rex_prefix & 8)
  4490. ctxt->op_bytes = 8; /* REX.W */
  4491. /* Opcode byte(s). */
  4492. opcode = opcode_table[ctxt->b];
  4493. /* Two-byte opcode? */
  4494. if (ctxt->b == 0x0f) {
  4495. ctxt->opcode_len = 2;
  4496. ctxt->b = insn_fetch(u8, ctxt);
  4497. opcode = twobyte_table[ctxt->b];
  4498. /* 0F_38 opcode map */
  4499. if (ctxt->b == 0x38) {
  4500. ctxt->opcode_len = 3;
  4501. ctxt->b = insn_fetch(u8, ctxt);
  4502. opcode = opcode_map_0f_38[ctxt->b];
  4503. }
  4504. }
  4505. ctxt->d = opcode.flags;
  4506. if (ctxt->d & ModRM)
  4507. ctxt->modrm = insn_fetch(u8, ctxt);
  4508. /* vex-prefix instructions are not implemented */
  4509. if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
  4510. (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
  4511. ctxt->d = NotImpl;
  4512. }
  4513. while (ctxt->d & GroupMask) {
  4514. switch (ctxt->d & GroupMask) {
  4515. case Group:
  4516. goffset = (ctxt->modrm >> 3) & 7;
  4517. opcode = opcode.u.group[goffset];
  4518. break;
  4519. case GroupDual:
  4520. goffset = (ctxt->modrm >> 3) & 7;
  4521. if ((ctxt->modrm >> 6) == 3)
  4522. opcode = opcode.u.gdual->mod3[goffset];
  4523. else
  4524. opcode = opcode.u.gdual->mod012[goffset];
  4525. break;
  4526. case RMExt:
  4527. goffset = ctxt->modrm & 7;
  4528. opcode = opcode.u.group[goffset];
  4529. break;
  4530. case Prefix:
  4531. if (ctxt->rep_prefix && op_prefix)
  4532. return EMULATION_FAILED;
  4533. simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix;
  4534. switch (simd_prefix) {
  4535. case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
  4536. case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
  4537. case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
  4538. case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
  4539. }
  4540. break;
  4541. case Escape:
  4542. if (ctxt->modrm > 0xbf) {
  4543. size_t size = ARRAY_SIZE(opcode.u.esc->high);
  4544. u32 index = array_index_nospec(
  4545. ctxt->modrm - 0xc0, size);
  4546. opcode = opcode.u.esc->high[index];
  4547. } else {
  4548. opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
  4549. }
  4550. break;
  4551. case InstrDual:
  4552. if ((ctxt->modrm >> 6) == 3)
  4553. opcode = opcode.u.idual->mod3;
  4554. else
  4555. opcode = opcode.u.idual->mod012;
  4556. break;
  4557. case ModeDual:
  4558. if (ctxt->mode == X86EMUL_MODE_PROT64)
  4559. opcode = opcode.u.mdual->mode64;
  4560. else
  4561. opcode = opcode.u.mdual->mode32;
  4562. break;
  4563. default:
  4564. return EMULATION_FAILED;
  4565. }
  4566. ctxt->d &= ~(u64)GroupMask;
  4567. ctxt->d |= opcode.flags;
  4568. }
  4569. /* Unrecognised? */
  4570. if (ctxt->d == 0)
  4571. return EMULATION_FAILED;
  4572. ctxt->execute = opcode.u.execute;
  4573. if (unlikely(ctxt->ud) && likely(!(ctxt->d & EmulateOnUD)))
  4574. return EMULATION_FAILED;
  4575. if (unlikely(ctxt->d &
  4576. (NotImpl|Stack|Op3264|Sse|Mmx|Intercept|CheckPerm|NearBranch|
  4577. No16))) {
  4578. /*
  4579. * These are copied unconditionally here, and checked unconditionally
  4580. * in x86_emulate_insn.
  4581. */
  4582. ctxt->check_perm = opcode.check_perm;
  4583. ctxt->intercept = opcode.intercept;
  4584. if (ctxt->d & NotImpl)
  4585. return EMULATION_FAILED;
  4586. if (mode == X86EMUL_MODE_PROT64) {
  4587. if (ctxt->op_bytes == 4 && (ctxt->d & Stack))
  4588. ctxt->op_bytes = 8;
  4589. else if (ctxt->d & NearBranch)
  4590. ctxt->op_bytes = 8;
  4591. }
  4592. if (ctxt->d & Op3264) {
  4593. if (mode == X86EMUL_MODE_PROT64)
  4594. ctxt->op_bytes = 8;
  4595. else
  4596. ctxt->op_bytes = 4;
  4597. }
  4598. if ((ctxt->d & No16) && ctxt->op_bytes == 2)
  4599. ctxt->op_bytes = 4;
  4600. if (ctxt->d & Sse)
  4601. ctxt->op_bytes = 16;
  4602. else if (ctxt->d & Mmx)
  4603. ctxt->op_bytes = 8;
  4604. }
  4605. /* ModRM and SIB bytes. */
  4606. if (ctxt->d & ModRM) {
  4607. rc = decode_modrm(ctxt, &ctxt->memop);
  4608. if (!has_seg_override) {
  4609. has_seg_override = true;
  4610. ctxt->seg_override = ctxt->modrm_seg;
  4611. }
  4612. } else if (ctxt->d & MemAbs)
  4613. rc = decode_abs(ctxt, &ctxt->memop);
  4614. if (rc != X86EMUL_CONTINUE)
  4615. goto done;
  4616. if (!has_seg_override)
  4617. ctxt->seg_override = VCPU_SREG_DS;
  4618. ctxt->memop.addr.mem.seg = ctxt->seg_override;
  4619. /*
  4620. * Decode and fetch the source operand: register, memory
  4621. * or immediate.
  4622. */
  4623. rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask);
  4624. if (rc != X86EMUL_CONTINUE)
  4625. goto done;
  4626. /*
  4627. * Decode and fetch the second source operand: register, memory
  4628. * or immediate.
  4629. */
  4630. rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask);
  4631. if (rc != X86EMUL_CONTINUE)
  4632. goto done;
  4633. /* Decode and fetch the destination operand: register or memory. */
  4634. rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask);
  4635. if (ctxt->rip_relative && likely(ctxt->memopp))
  4636. ctxt->memopp->addr.mem.ea = address_mask(ctxt,
  4637. ctxt->memopp->addr.mem.ea + ctxt->_eip);
  4638. done:
  4639. if (rc == X86EMUL_PROPAGATE_FAULT)
  4640. ctxt->have_exception = true;
  4641. return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK;
  4642. }
  4643. bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt)
  4644. {
  4645. return ctxt->d & PageTable;
  4646. }
  4647. static bool string_insn_completed(struct x86_emulate_ctxt *ctxt)
  4648. {
  4649. /* The second termination condition only applies for REPE
  4650. * and REPNE. Test if the repeat string operation prefix is
  4651. * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the
  4652. * corresponding termination condition according to:
  4653. * - if REPE/REPZ and ZF = 0 then done
  4654. * - if REPNE/REPNZ and ZF = 1 then done
  4655. */
  4656. if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) ||
  4657. (ctxt->b == 0xae) || (ctxt->b == 0xaf))
  4658. && (((ctxt->rep_prefix == REPE_PREFIX) &&
  4659. ((ctxt->eflags & X86_EFLAGS_ZF) == 0))
  4660. || ((ctxt->rep_prefix == REPNE_PREFIX) &&
  4661. ((ctxt->eflags & X86_EFLAGS_ZF) == X86_EFLAGS_ZF))))
  4662. return true;
  4663. return false;
  4664. }
  4665. static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt)
  4666. {
  4667. int rc;
  4668. rc = asm_safe("fwait");
  4669. if (unlikely(rc != X86EMUL_CONTINUE))
  4670. return emulate_exception(ctxt, MF_VECTOR, 0, false);
  4671. return X86EMUL_CONTINUE;
  4672. }
  4673. static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt,
  4674. struct operand *op)
  4675. {
  4676. if (op->type == OP_MM)
  4677. read_mmx_reg(ctxt, &op->mm_val, op->addr.mm);
  4678. }
  4679. static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *))
  4680. {
  4681. ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF;
  4682. if (!(ctxt->d & ByteOp))
  4683. fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE;
  4684. asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n"
  4685. : "+a"(ctxt->dst.val), "+d"(ctxt->src.val), [flags]"+D"(flags),
  4686. [thunk_target]"+S"(fop), ASM_CALL_CONSTRAINT
  4687. : "c"(ctxt->src2.val));
  4688. ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK);
  4689. if (!fop) /* exception is returned in fop variable */
  4690. return emulate_de(ctxt);
  4691. return X86EMUL_CONTINUE;
  4692. }
  4693. void init_decode_cache(struct x86_emulate_ctxt *ctxt)
  4694. {
  4695. memset(&ctxt->rip_relative, 0,
  4696. (void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
  4697. ctxt->io_read.pos = 0;
  4698. ctxt->io_read.end = 0;
  4699. ctxt->mem_read.end = 0;
  4700. }
  4701. int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
  4702. {
  4703. const struct x86_emulate_ops *ops = ctxt->ops;
  4704. int rc = X86EMUL_CONTINUE;
  4705. int saved_dst_type = ctxt->dst.type;
  4706. unsigned emul_flags;
  4707. ctxt->mem_read.pos = 0;
  4708. /* LOCK prefix is allowed only with some instructions */
  4709. if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) {
  4710. rc = emulate_ud(ctxt);
  4711. goto done;
  4712. }
  4713. if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) {
  4714. rc = emulate_ud(ctxt);
  4715. goto done;
  4716. }
  4717. emul_flags = ctxt->ops->get_hflags(ctxt);
  4718. if (unlikely(ctxt->d &
  4719. (No64|Undefined|Sse|Mmx|Intercept|CheckPerm|Priv|Prot|String))) {
  4720. if ((ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) ||
  4721. (ctxt->d & Undefined)) {
  4722. rc = emulate_ud(ctxt);
  4723. goto done;
  4724. }
  4725. if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM)))
  4726. || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) {
  4727. rc = emulate_ud(ctxt);
  4728. goto done;
  4729. }
  4730. if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) {
  4731. rc = emulate_nm(ctxt);
  4732. goto done;
  4733. }
  4734. if (ctxt->d & Mmx) {
  4735. rc = flush_pending_x87_faults(ctxt);
  4736. if (rc != X86EMUL_CONTINUE)
  4737. goto done;
  4738. /*
  4739. * Now that we know the fpu is exception safe, we can fetch
  4740. * operands from it.
  4741. */
  4742. fetch_possible_mmx_operand(ctxt, &ctxt->src);
  4743. fetch_possible_mmx_operand(ctxt, &ctxt->src2);
  4744. if (!(ctxt->d & Mov))
  4745. fetch_possible_mmx_operand(ctxt, &ctxt->dst);
  4746. }
  4747. if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && ctxt->intercept) {
  4748. rc = emulator_check_intercept(ctxt, ctxt->intercept,
  4749. X86_ICPT_PRE_EXCEPT);
  4750. if (rc != X86EMUL_CONTINUE)
  4751. goto done;
  4752. }
  4753. /* Instruction can only be executed in protected mode */
  4754. if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) {
  4755. rc = emulate_ud(ctxt);
  4756. goto done;
  4757. }
  4758. /* Privileged instruction can be executed only in CPL=0 */
  4759. if ((ctxt->d & Priv) && ops->cpl(ctxt)) {
  4760. if (ctxt->d & PrivUD)
  4761. rc = emulate_ud(ctxt);
  4762. else
  4763. rc = emulate_gp(ctxt, 0);
  4764. goto done;
  4765. }
  4766. /* Do instruction specific permission checks */
  4767. if (ctxt->d & CheckPerm) {
  4768. rc = ctxt->check_perm(ctxt);
  4769. if (rc != X86EMUL_CONTINUE)
  4770. goto done;
  4771. }
  4772. if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
  4773. rc = emulator_check_intercept(ctxt, ctxt->intercept,
  4774. X86_ICPT_POST_EXCEPT);
  4775. if (rc != X86EMUL_CONTINUE)
  4776. goto done;
  4777. }
  4778. if (ctxt->rep_prefix && (ctxt->d & String)) {
  4779. /* All REP prefixes have the same first termination condition */
  4780. if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) {
  4781. string_registers_quirk(ctxt);
  4782. ctxt->eip = ctxt->_eip;
  4783. ctxt->eflags &= ~X86_EFLAGS_RF;
  4784. goto done;
  4785. }
  4786. }
  4787. }
  4788. if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) {
  4789. rc = segmented_read(ctxt, ctxt->src.addr.mem,
  4790. ctxt->src.valptr, ctxt->src.bytes);
  4791. if (rc != X86EMUL_CONTINUE)
  4792. goto done;
  4793. ctxt->src.orig_val64 = ctxt->src.val64;
  4794. }
  4795. if (ctxt->src2.type == OP_MEM) {
  4796. rc = segmented_read(ctxt, ctxt->src2.addr.mem,
  4797. &ctxt->src2.val, ctxt->src2.bytes);
  4798. if (rc != X86EMUL_CONTINUE)
  4799. goto done;
  4800. }
  4801. if ((ctxt->d & DstMask) == ImplicitOps)
  4802. goto special_insn;
  4803. if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) {
  4804. /* optimisation - avoid slow emulated read if Mov */
  4805. rc = segmented_read(ctxt, ctxt->dst.addr.mem,
  4806. &ctxt->dst.val, ctxt->dst.bytes);
  4807. if (rc != X86EMUL_CONTINUE) {
  4808. if (!(ctxt->d & NoWrite) &&
  4809. rc == X86EMUL_PROPAGATE_FAULT &&
  4810. ctxt->exception.vector == PF_VECTOR)
  4811. ctxt->exception.error_code |= PFERR_WRITE_MASK;
  4812. goto done;
  4813. }
  4814. }
  4815. /* Copy full 64-bit value for CMPXCHG8B. */
  4816. ctxt->dst.orig_val64 = ctxt->dst.val64;
  4817. special_insn:
  4818. if (unlikely(emul_flags & X86EMUL_GUEST_MASK) && (ctxt->d & Intercept)) {
  4819. rc = emulator_check_intercept(ctxt, ctxt->intercept,
  4820. X86_ICPT_POST_MEMACCESS);
  4821. if (rc != X86EMUL_CONTINUE)
  4822. goto done;
  4823. }
  4824. if (ctxt->rep_prefix && (ctxt->d & String))
  4825. ctxt->eflags |= X86_EFLAGS_RF;
  4826. else
  4827. ctxt->eflags &= ~X86_EFLAGS_RF;
  4828. if (ctxt->execute) {
  4829. if (ctxt->d & Fastop) {
  4830. void (*fop)(struct fastop *) = (void *)ctxt->execute;
  4831. rc = fastop(ctxt, fop);
  4832. if (rc != X86EMUL_CONTINUE)
  4833. goto done;
  4834. goto writeback;
  4835. }
  4836. rc = ctxt->execute(ctxt);
  4837. if (rc != X86EMUL_CONTINUE)
  4838. goto done;
  4839. goto writeback;
  4840. }
  4841. if (ctxt->opcode_len == 2)
  4842. goto twobyte_insn;
  4843. else if (ctxt->opcode_len == 3)
  4844. goto threebyte_insn;
  4845. switch (ctxt->b) {
  4846. case 0x70 ... 0x7f: /* jcc (short) */
  4847. if (test_cc(ctxt->b, ctxt->eflags))
  4848. rc = jmp_rel(ctxt, ctxt->src.val);
  4849. break;
  4850. case 0x8d: /* lea r16/r32, m */
  4851. ctxt->dst.val = ctxt->src.addr.mem.ea;
  4852. break;
  4853. case 0x90 ... 0x97: /* nop / xchg reg, rax */
  4854. if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX))
  4855. ctxt->dst.type = OP_NONE;
  4856. else
  4857. rc = em_xchg(ctxt);
  4858. break;
  4859. case 0x98: /* cbw/cwde/cdqe */
  4860. switch (ctxt->op_bytes) {
  4861. case 2: ctxt->dst.val = (s8)ctxt->dst.val; break;
  4862. case 4: ctxt->dst.val = (s16)ctxt->dst.val; break;
  4863. case 8: ctxt->dst.val = (s32)ctxt->dst.val; break;
  4864. }
  4865. break;
  4866. case 0xcc: /* int3 */
  4867. rc = emulate_int(ctxt, 3);
  4868. break;
  4869. case 0xcd: /* int n */
  4870. rc = emulate_int(ctxt, ctxt->src.val);
  4871. break;
  4872. case 0xce: /* into */
  4873. if (ctxt->eflags & X86_EFLAGS_OF)
  4874. rc = emulate_int(ctxt, 4);
  4875. break;
  4876. case 0xe9: /* jmp rel */
  4877. case 0xeb: /* jmp rel short */
  4878. rc = jmp_rel(ctxt, ctxt->src.val);
  4879. ctxt->dst.type = OP_NONE; /* Disable writeback. */
  4880. break;
  4881. case 0xf4: /* hlt */
  4882. ctxt->ops->halt(ctxt);
  4883. break;
  4884. case 0xf5: /* cmc */
  4885. /* complement carry flag from eflags reg */
  4886. ctxt->eflags ^= X86_EFLAGS_CF;
  4887. break;
  4888. case 0xf8: /* clc */
  4889. ctxt->eflags &= ~X86_EFLAGS_CF;
  4890. break;
  4891. case 0xf9: /* stc */
  4892. ctxt->eflags |= X86_EFLAGS_CF;
  4893. break;
  4894. case 0xfc: /* cld */
  4895. ctxt->eflags &= ~X86_EFLAGS_DF;
  4896. break;
  4897. case 0xfd: /* std */
  4898. ctxt->eflags |= X86_EFLAGS_DF;
  4899. break;
  4900. default:
  4901. goto cannot_emulate;
  4902. }
  4903. if (rc != X86EMUL_CONTINUE)
  4904. goto done;
  4905. writeback:
  4906. if (ctxt->d & SrcWrite) {
  4907. BUG_ON(ctxt->src.type == OP_MEM || ctxt->src.type == OP_MEM_STR);
  4908. rc = writeback(ctxt, &ctxt->src);
  4909. if (rc != X86EMUL_CONTINUE)
  4910. goto done;
  4911. }
  4912. if (!(ctxt->d & NoWrite)) {
  4913. rc = writeback(ctxt, &ctxt->dst);
  4914. if (rc != X86EMUL_CONTINUE)
  4915. goto done;
  4916. }
  4917. /*
  4918. * restore dst type in case the decoding will be reused
  4919. * (happens for string instruction )
  4920. */
  4921. ctxt->dst.type = saved_dst_type;
  4922. if ((ctxt->d & SrcMask) == SrcSI)
  4923. string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src);
  4924. if ((ctxt->d & DstMask) == DstDI)
  4925. string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst);
  4926. if (ctxt->rep_prefix && (ctxt->d & String)) {
  4927. unsigned int count;
  4928. struct read_cache *r = &ctxt->io_read;
  4929. if ((ctxt->d & SrcMask) == SrcSI)
  4930. count = ctxt->src.count;
  4931. else
  4932. count = ctxt->dst.count;
  4933. register_address_increment(ctxt, VCPU_REGS_RCX, -count);
  4934. if (!string_insn_completed(ctxt)) {
  4935. /*
  4936. * Re-enter guest when pio read ahead buffer is empty
  4937. * or, if it is not used, after each 1024 iteration.
  4938. */
  4939. if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) &&
  4940. (r->end == 0 || r->end != r->pos)) {
  4941. /*
  4942. * Reset read cache. Usually happens before
  4943. * decode, but since instruction is restarted
  4944. * we have to do it here.
  4945. */
  4946. ctxt->mem_read.end = 0;
  4947. writeback_registers(ctxt);
  4948. return EMULATION_RESTART;
  4949. }
  4950. goto done; /* skip rip writeback */
  4951. }
  4952. ctxt->eflags &= ~X86_EFLAGS_RF;
  4953. }
  4954. ctxt->eip = ctxt->_eip;
  4955. done:
  4956. if (rc == X86EMUL_PROPAGATE_FAULT) {
  4957. WARN_ON(ctxt->exception.vector > 0x1f);
  4958. ctxt->have_exception = true;
  4959. }
  4960. if (rc == X86EMUL_INTERCEPTED)
  4961. return EMULATION_INTERCEPTED;
  4962. if (rc == X86EMUL_CONTINUE)
  4963. writeback_registers(ctxt);
  4964. return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK;
  4965. twobyte_insn:
  4966. switch (ctxt->b) {
  4967. case 0x09: /* wbinvd */
  4968. (ctxt->ops->wbinvd)(ctxt);
  4969. break;
  4970. case 0x08: /* invd */
  4971. case 0x0d: /* GrpP (prefetch) */
  4972. case 0x18: /* Grp16 (prefetch/nop) */
  4973. case 0x1f: /* nop */
  4974. break;
  4975. case 0x20: /* mov cr, reg */
  4976. ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg);
  4977. break;
  4978. case 0x21: /* mov from dr to reg */
  4979. ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val);
  4980. break;
  4981. case 0x40 ... 0x4f: /* cmov */
  4982. if (test_cc(ctxt->b, ctxt->eflags))
  4983. ctxt->dst.val = ctxt->src.val;
  4984. else if (ctxt->op_bytes != 4)
  4985. ctxt->dst.type = OP_NONE; /* no writeback */
  4986. break;
  4987. case 0x80 ... 0x8f: /* jnz rel, etc*/
  4988. if (test_cc(ctxt->b, ctxt->eflags))
  4989. rc = jmp_rel(ctxt, ctxt->src.val);
  4990. break;
  4991. case 0x90 ... 0x9f: /* setcc r/m8 */
  4992. ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags);
  4993. break;
  4994. case 0xb6 ... 0xb7: /* movzx */
  4995. ctxt->dst.bytes = ctxt->op_bytes;
  4996. ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val
  4997. : (u16) ctxt->src.val;
  4998. break;
  4999. case 0xbe ... 0xbf: /* movsx */
  5000. ctxt->dst.bytes = ctxt->op_bytes;
  5001. ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val :
  5002. (s16) ctxt->src.val;
  5003. break;
  5004. default:
  5005. goto cannot_emulate;
  5006. }
  5007. threebyte_insn:
  5008. if (rc != X86EMUL_CONTINUE)
  5009. goto done;
  5010. goto writeback;
  5011. cannot_emulate:
  5012. return EMULATION_FAILED;
  5013. }
  5014. void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt)
  5015. {
  5016. invalidate_registers(ctxt);
  5017. }
  5018. void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt)
  5019. {
  5020. writeback_registers(ctxt);
  5021. }
  5022. bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt)
  5023. {
  5024. if (ctxt->rep_prefix && (ctxt->d & String))
  5025. return false;
  5026. if (ctxt->d & TwoMemOp)
  5027. return false;
  5028. return true;
  5029. }