emulate.c 147 KB

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