emulate.c 147 KB

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