vmx.c 206 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. */
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include "cpuid.h"
  21. #include <linux/kvm_host.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/highmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/ftrace_event.h>
  29. #include <linux/slab.h>
  30. #include <linux/tboot.h>
  31. #include "kvm_cache_regs.h"
  32. #include "x86.h"
  33. #include <asm/io.h>
  34. #include <asm/desc.h>
  35. #include <asm/vmx.h>
  36. #include <asm/virtext.h>
  37. #include <asm/mce.h>
  38. #include <asm/i387.h>
  39. #include <asm/xcr.h>
  40. #include <asm/perf_event.h>
  41. #include "trace.h"
  42. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  43. #define __ex_clear(x, reg) \
  44. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  45. MODULE_AUTHOR("Qumranet");
  46. MODULE_LICENSE("GPL");
  47. static bool __read_mostly enable_vpid = 1;
  48. module_param_named(vpid, enable_vpid, bool, 0444);
  49. static bool __read_mostly flexpriority_enabled = 1;
  50. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  51. static bool __read_mostly enable_ept = 1;
  52. module_param_named(ept, enable_ept, bool, S_IRUGO);
  53. static bool __read_mostly enable_unrestricted_guest = 1;
  54. module_param_named(unrestricted_guest,
  55. enable_unrestricted_guest, bool, S_IRUGO);
  56. static bool __read_mostly emulate_invalid_guest_state = 0;
  57. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  58. static bool __read_mostly vmm_exclusive = 1;
  59. module_param(vmm_exclusive, bool, S_IRUGO);
  60. static bool __read_mostly fasteoi = 1;
  61. module_param(fasteoi, bool, S_IRUGO);
  62. /*
  63. * If nested=1, nested virtualization is supported, i.e., guests may use
  64. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  65. * use VMX instructions.
  66. */
  67. static bool __read_mostly nested = 0;
  68. module_param(nested, bool, S_IRUGO);
  69. #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
  70. (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
  71. #define KVM_GUEST_CR0_MASK \
  72. (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  73. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
  74. (X86_CR0_WP | X86_CR0_NE)
  75. #define KVM_VM_CR0_ALWAYS_ON \
  76. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  77. #define KVM_CR4_GUEST_OWNED_BITS \
  78. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  79. | X86_CR4_OSXMMEXCPT)
  80. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  81. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  82. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  83. /*
  84. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  85. * ple_gap: upper bound on the amount of time between two successive
  86. * executions of PAUSE in a loop. Also indicate if ple enabled.
  87. * According to test, this time is usually smaller than 128 cycles.
  88. * ple_window: upper bound on the amount of time a guest is allowed to execute
  89. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  90. * less than 2^12 cycles
  91. * Time is measured based on a counter that runs at the same rate as the TSC,
  92. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  93. */
  94. #define KVM_VMX_DEFAULT_PLE_GAP 128
  95. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  96. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  97. module_param(ple_gap, int, S_IRUGO);
  98. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  99. module_param(ple_window, int, S_IRUGO);
  100. #define NR_AUTOLOAD_MSRS 8
  101. #define VMCS02_POOL_SIZE 1
  102. struct vmcs {
  103. u32 revision_id;
  104. u32 abort;
  105. char data[0];
  106. };
  107. /*
  108. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  109. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  110. * loaded on this CPU (so we can clear them if the CPU goes down).
  111. */
  112. struct loaded_vmcs {
  113. struct vmcs *vmcs;
  114. int cpu;
  115. int launched;
  116. struct list_head loaded_vmcss_on_cpu_link;
  117. };
  118. struct shared_msr_entry {
  119. unsigned index;
  120. u64 data;
  121. u64 mask;
  122. };
  123. /*
  124. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  125. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  126. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  127. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  128. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  129. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  130. * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
  131. * underlying hardware which will be used to run L2.
  132. * This structure is packed to ensure that its layout is identical across
  133. * machines (necessary for live migration).
  134. * If there are changes in this struct, VMCS12_REVISION must be changed.
  135. */
  136. typedef u64 natural_width;
  137. struct __packed vmcs12 {
  138. /* According to the Intel spec, a VMCS region must start with the
  139. * following two fields. Then follow implementation-specific data.
  140. */
  141. u32 revision_id;
  142. u32 abort;
  143. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  144. u32 padding[7]; /* room for future expansion */
  145. u64 io_bitmap_a;
  146. u64 io_bitmap_b;
  147. u64 msr_bitmap;
  148. u64 vm_exit_msr_store_addr;
  149. u64 vm_exit_msr_load_addr;
  150. u64 vm_entry_msr_load_addr;
  151. u64 tsc_offset;
  152. u64 virtual_apic_page_addr;
  153. u64 apic_access_addr;
  154. u64 ept_pointer;
  155. u64 guest_physical_address;
  156. u64 vmcs_link_pointer;
  157. u64 guest_ia32_debugctl;
  158. u64 guest_ia32_pat;
  159. u64 guest_ia32_efer;
  160. u64 guest_ia32_perf_global_ctrl;
  161. u64 guest_pdptr0;
  162. u64 guest_pdptr1;
  163. u64 guest_pdptr2;
  164. u64 guest_pdptr3;
  165. u64 host_ia32_pat;
  166. u64 host_ia32_efer;
  167. u64 host_ia32_perf_global_ctrl;
  168. u64 padding64[8]; /* room for future expansion */
  169. /*
  170. * To allow migration of L1 (complete with its L2 guests) between
  171. * machines of different natural widths (32 or 64 bit), we cannot have
  172. * unsigned long fields with no explict size. We use u64 (aliased
  173. * natural_width) instead. Luckily, x86 is little-endian.
  174. */
  175. natural_width cr0_guest_host_mask;
  176. natural_width cr4_guest_host_mask;
  177. natural_width cr0_read_shadow;
  178. natural_width cr4_read_shadow;
  179. natural_width cr3_target_value0;
  180. natural_width cr3_target_value1;
  181. natural_width cr3_target_value2;
  182. natural_width cr3_target_value3;
  183. natural_width exit_qualification;
  184. natural_width guest_linear_address;
  185. natural_width guest_cr0;
  186. natural_width guest_cr3;
  187. natural_width guest_cr4;
  188. natural_width guest_es_base;
  189. natural_width guest_cs_base;
  190. natural_width guest_ss_base;
  191. natural_width guest_ds_base;
  192. natural_width guest_fs_base;
  193. natural_width guest_gs_base;
  194. natural_width guest_ldtr_base;
  195. natural_width guest_tr_base;
  196. natural_width guest_gdtr_base;
  197. natural_width guest_idtr_base;
  198. natural_width guest_dr7;
  199. natural_width guest_rsp;
  200. natural_width guest_rip;
  201. natural_width guest_rflags;
  202. natural_width guest_pending_dbg_exceptions;
  203. natural_width guest_sysenter_esp;
  204. natural_width guest_sysenter_eip;
  205. natural_width host_cr0;
  206. natural_width host_cr3;
  207. natural_width host_cr4;
  208. natural_width host_fs_base;
  209. natural_width host_gs_base;
  210. natural_width host_tr_base;
  211. natural_width host_gdtr_base;
  212. natural_width host_idtr_base;
  213. natural_width host_ia32_sysenter_esp;
  214. natural_width host_ia32_sysenter_eip;
  215. natural_width host_rsp;
  216. natural_width host_rip;
  217. natural_width paddingl[8]; /* room for future expansion */
  218. u32 pin_based_vm_exec_control;
  219. u32 cpu_based_vm_exec_control;
  220. u32 exception_bitmap;
  221. u32 page_fault_error_code_mask;
  222. u32 page_fault_error_code_match;
  223. u32 cr3_target_count;
  224. u32 vm_exit_controls;
  225. u32 vm_exit_msr_store_count;
  226. u32 vm_exit_msr_load_count;
  227. u32 vm_entry_controls;
  228. u32 vm_entry_msr_load_count;
  229. u32 vm_entry_intr_info_field;
  230. u32 vm_entry_exception_error_code;
  231. u32 vm_entry_instruction_len;
  232. u32 tpr_threshold;
  233. u32 secondary_vm_exec_control;
  234. u32 vm_instruction_error;
  235. u32 vm_exit_reason;
  236. u32 vm_exit_intr_info;
  237. u32 vm_exit_intr_error_code;
  238. u32 idt_vectoring_info_field;
  239. u32 idt_vectoring_error_code;
  240. u32 vm_exit_instruction_len;
  241. u32 vmx_instruction_info;
  242. u32 guest_es_limit;
  243. u32 guest_cs_limit;
  244. u32 guest_ss_limit;
  245. u32 guest_ds_limit;
  246. u32 guest_fs_limit;
  247. u32 guest_gs_limit;
  248. u32 guest_ldtr_limit;
  249. u32 guest_tr_limit;
  250. u32 guest_gdtr_limit;
  251. u32 guest_idtr_limit;
  252. u32 guest_es_ar_bytes;
  253. u32 guest_cs_ar_bytes;
  254. u32 guest_ss_ar_bytes;
  255. u32 guest_ds_ar_bytes;
  256. u32 guest_fs_ar_bytes;
  257. u32 guest_gs_ar_bytes;
  258. u32 guest_ldtr_ar_bytes;
  259. u32 guest_tr_ar_bytes;
  260. u32 guest_interruptibility_info;
  261. u32 guest_activity_state;
  262. u32 guest_sysenter_cs;
  263. u32 host_ia32_sysenter_cs;
  264. u32 padding32[8]; /* room for future expansion */
  265. u16 virtual_processor_id;
  266. u16 guest_es_selector;
  267. u16 guest_cs_selector;
  268. u16 guest_ss_selector;
  269. u16 guest_ds_selector;
  270. u16 guest_fs_selector;
  271. u16 guest_gs_selector;
  272. u16 guest_ldtr_selector;
  273. u16 guest_tr_selector;
  274. u16 host_es_selector;
  275. u16 host_cs_selector;
  276. u16 host_ss_selector;
  277. u16 host_ds_selector;
  278. u16 host_fs_selector;
  279. u16 host_gs_selector;
  280. u16 host_tr_selector;
  281. };
  282. /*
  283. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  284. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  285. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  286. */
  287. #define VMCS12_REVISION 0x11e57ed0
  288. /*
  289. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  290. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  291. * current implementation, 4K are reserved to avoid future complications.
  292. */
  293. #define VMCS12_SIZE 0x1000
  294. /* Used to remember the last vmcs02 used for some recently used vmcs12s */
  295. struct vmcs02_list {
  296. struct list_head list;
  297. gpa_t vmptr;
  298. struct loaded_vmcs vmcs02;
  299. };
  300. /*
  301. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  302. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  303. */
  304. struct nested_vmx {
  305. /* Has the level1 guest done vmxon? */
  306. bool vmxon;
  307. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  308. gpa_t current_vmptr;
  309. /* The host-usable pointer to the above */
  310. struct page *current_vmcs12_page;
  311. struct vmcs12 *current_vmcs12;
  312. /* vmcs02_list cache of VMCSs recently used to run L2 guests */
  313. struct list_head vmcs02_pool;
  314. int vmcs02_num;
  315. u64 vmcs01_tsc_offset;
  316. /* L2 must run next, and mustn't decide to exit to L1. */
  317. bool nested_run_pending;
  318. /*
  319. * Guest pages referred to in vmcs02 with host-physical pointers, so
  320. * we must keep them pinned while L2 runs.
  321. */
  322. struct page *apic_access_page;
  323. };
  324. struct vcpu_vmx {
  325. struct kvm_vcpu vcpu;
  326. unsigned long host_rsp;
  327. u8 fail;
  328. u8 cpl;
  329. bool nmi_known_unmasked;
  330. u32 exit_intr_info;
  331. u32 idt_vectoring_info;
  332. ulong rflags;
  333. struct shared_msr_entry *guest_msrs;
  334. int nmsrs;
  335. int save_nmsrs;
  336. #ifdef CONFIG_X86_64
  337. u64 msr_host_kernel_gs_base;
  338. u64 msr_guest_kernel_gs_base;
  339. #endif
  340. /*
  341. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  342. * non-nested (L1) guest, it always points to vmcs01. For a nested
  343. * guest (L2), it points to a different VMCS.
  344. */
  345. struct loaded_vmcs vmcs01;
  346. struct loaded_vmcs *loaded_vmcs;
  347. bool __launched; /* temporary, used in vmx_vcpu_run */
  348. struct msr_autoload {
  349. unsigned nr;
  350. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  351. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  352. } msr_autoload;
  353. struct {
  354. int loaded;
  355. u16 fs_sel, gs_sel, ldt_sel;
  356. int gs_ldt_reload_needed;
  357. int fs_reload_needed;
  358. } host_state;
  359. struct {
  360. int vm86_active;
  361. ulong save_rflags;
  362. struct kvm_save_segment {
  363. u16 selector;
  364. unsigned long base;
  365. u32 limit;
  366. u32 ar;
  367. } tr, es, ds, fs, gs;
  368. } rmode;
  369. struct {
  370. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  371. struct kvm_save_segment seg[8];
  372. } segment_cache;
  373. int vpid;
  374. bool emulation_required;
  375. /* Support for vnmi-less CPUs */
  376. int soft_vnmi_blocked;
  377. ktime_t entry_time;
  378. s64 vnmi_blocked_time;
  379. u32 exit_reason;
  380. bool rdtscp_enabled;
  381. /* Support for a guest hypervisor (nested VMX) */
  382. struct nested_vmx nested;
  383. };
  384. enum segment_cache_field {
  385. SEG_FIELD_SEL = 0,
  386. SEG_FIELD_BASE = 1,
  387. SEG_FIELD_LIMIT = 2,
  388. SEG_FIELD_AR = 3,
  389. SEG_FIELD_NR = 4
  390. };
  391. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  392. {
  393. return container_of(vcpu, struct vcpu_vmx, vcpu);
  394. }
  395. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  396. #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
  397. #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
  398. [number##_HIGH] = VMCS12_OFFSET(name)+4
  399. static unsigned short vmcs_field_to_offset_table[] = {
  400. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  401. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  402. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  403. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  404. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  405. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  406. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  407. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  408. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  409. FIELD(HOST_ES_SELECTOR, host_es_selector),
  410. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  411. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  412. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  413. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  414. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  415. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  416. FIELD64(IO_BITMAP_A, io_bitmap_a),
  417. FIELD64(IO_BITMAP_B, io_bitmap_b),
  418. FIELD64(MSR_BITMAP, msr_bitmap),
  419. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  420. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  421. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  422. FIELD64(TSC_OFFSET, tsc_offset),
  423. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  424. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  425. FIELD64(EPT_POINTER, ept_pointer),
  426. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  427. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  428. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  429. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  430. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  431. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  432. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  433. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  434. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  435. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  436. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  437. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  438. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  439. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  440. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  441. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  442. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  443. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  444. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  445. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  446. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  447. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  448. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  449. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  450. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  451. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  452. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  453. FIELD(TPR_THRESHOLD, tpr_threshold),
  454. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  455. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  456. FIELD(VM_EXIT_REASON, vm_exit_reason),
  457. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  458. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  459. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  460. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  461. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  462. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  463. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  464. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  465. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  466. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  467. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  468. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  469. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  470. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  471. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  472. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  473. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  474. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  475. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  476. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  477. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  478. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  479. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  480. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  481. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  482. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  483. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  484. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  485. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  486. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  487. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  488. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  489. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  490. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  491. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  492. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  493. FIELD(EXIT_QUALIFICATION, exit_qualification),
  494. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  495. FIELD(GUEST_CR0, guest_cr0),
  496. FIELD(GUEST_CR3, guest_cr3),
  497. FIELD(GUEST_CR4, guest_cr4),
  498. FIELD(GUEST_ES_BASE, guest_es_base),
  499. FIELD(GUEST_CS_BASE, guest_cs_base),
  500. FIELD(GUEST_SS_BASE, guest_ss_base),
  501. FIELD(GUEST_DS_BASE, guest_ds_base),
  502. FIELD(GUEST_FS_BASE, guest_fs_base),
  503. FIELD(GUEST_GS_BASE, guest_gs_base),
  504. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  505. FIELD(GUEST_TR_BASE, guest_tr_base),
  506. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  507. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  508. FIELD(GUEST_DR7, guest_dr7),
  509. FIELD(GUEST_RSP, guest_rsp),
  510. FIELD(GUEST_RIP, guest_rip),
  511. FIELD(GUEST_RFLAGS, guest_rflags),
  512. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  513. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  514. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  515. FIELD(HOST_CR0, host_cr0),
  516. FIELD(HOST_CR3, host_cr3),
  517. FIELD(HOST_CR4, host_cr4),
  518. FIELD(HOST_FS_BASE, host_fs_base),
  519. FIELD(HOST_GS_BASE, host_gs_base),
  520. FIELD(HOST_TR_BASE, host_tr_base),
  521. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  522. FIELD(HOST_IDTR_BASE, host_idtr_base),
  523. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  524. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  525. FIELD(HOST_RSP, host_rsp),
  526. FIELD(HOST_RIP, host_rip),
  527. };
  528. static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
  529. static inline short vmcs_field_to_offset(unsigned long field)
  530. {
  531. if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
  532. return -1;
  533. return vmcs_field_to_offset_table[field];
  534. }
  535. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  536. {
  537. return to_vmx(vcpu)->nested.current_vmcs12;
  538. }
  539. static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
  540. {
  541. struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
  542. if (is_error_page(page)) {
  543. kvm_release_page_clean(page);
  544. return NULL;
  545. }
  546. return page;
  547. }
  548. static void nested_release_page(struct page *page)
  549. {
  550. kvm_release_page_dirty(page);
  551. }
  552. static void nested_release_page_clean(struct page *page)
  553. {
  554. kvm_release_page_clean(page);
  555. }
  556. static u64 construct_eptp(unsigned long root_hpa);
  557. static void kvm_cpu_vmxon(u64 addr);
  558. static void kvm_cpu_vmxoff(void);
  559. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
  560. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
  561. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  562. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  563. /*
  564. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  565. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  566. */
  567. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  568. static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
  569. static unsigned long *vmx_io_bitmap_a;
  570. static unsigned long *vmx_io_bitmap_b;
  571. static unsigned long *vmx_msr_bitmap_legacy;
  572. static unsigned long *vmx_msr_bitmap_longmode;
  573. static bool cpu_has_load_ia32_efer;
  574. static bool cpu_has_load_perf_global_ctrl;
  575. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  576. static DEFINE_SPINLOCK(vmx_vpid_lock);
  577. static struct vmcs_config {
  578. int size;
  579. int order;
  580. u32 revision_id;
  581. u32 pin_based_exec_ctrl;
  582. u32 cpu_based_exec_ctrl;
  583. u32 cpu_based_2nd_exec_ctrl;
  584. u32 vmexit_ctrl;
  585. u32 vmentry_ctrl;
  586. } vmcs_config;
  587. static struct vmx_capability {
  588. u32 ept;
  589. u32 vpid;
  590. } vmx_capability;
  591. #define VMX_SEGMENT_FIELD(seg) \
  592. [VCPU_SREG_##seg] = { \
  593. .selector = GUEST_##seg##_SELECTOR, \
  594. .base = GUEST_##seg##_BASE, \
  595. .limit = GUEST_##seg##_LIMIT, \
  596. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  597. }
  598. static struct kvm_vmx_segment_field {
  599. unsigned selector;
  600. unsigned base;
  601. unsigned limit;
  602. unsigned ar_bytes;
  603. } kvm_vmx_segment_fields[] = {
  604. VMX_SEGMENT_FIELD(CS),
  605. VMX_SEGMENT_FIELD(DS),
  606. VMX_SEGMENT_FIELD(ES),
  607. VMX_SEGMENT_FIELD(FS),
  608. VMX_SEGMENT_FIELD(GS),
  609. VMX_SEGMENT_FIELD(SS),
  610. VMX_SEGMENT_FIELD(TR),
  611. VMX_SEGMENT_FIELD(LDTR),
  612. };
  613. static u64 host_efer;
  614. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  615. /*
  616. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  617. * away by decrementing the array size.
  618. */
  619. static const u32 vmx_msr_index[] = {
  620. #ifdef CONFIG_X86_64
  621. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  622. #endif
  623. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  624. };
  625. #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
  626. static inline bool is_page_fault(u32 intr_info)
  627. {
  628. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  629. INTR_INFO_VALID_MASK)) ==
  630. (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  631. }
  632. static inline bool is_no_device(u32 intr_info)
  633. {
  634. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  635. INTR_INFO_VALID_MASK)) ==
  636. (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  637. }
  638. static inline bool is_invalid_opcode(u32 intr_info)
  639. {
  640. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  641. INTR_INFO_VALID_MASK)) ==
  642. (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  643. }
  644. static inline bool is_external_interrupt(u32 intr_info)
  645. {
  646. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  647. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  648. }
  649. static inline bool is_machine_check(u32 intr_info)
  650. {
  651. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  652. INTR_INFO_VALID_MASK)) ==
  653. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  654. }
  655. static inline bool cpu_has_vmx_msr_bitmap(void)
  656. {
  657. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  658. }
  659. static inline bool cpu_has_vmx_tpr_shadow(void)
  660. {
  661. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  662. }
  663. static inline bool vm_need_tpr_shadow(struct kvm *kvm)
  664. {
  665. return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
  666. }
  667. static inline bool cpu_has_secondary_exec_ctrls(void)
  668. {
  669. return vmcs_config.cpu_based_exec_ctrl &
  670. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  671. }
  672. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  673. {
  674. return vmcs_config.cpu_based_2nd_exec_ctrl &
  675. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  676. }
  677. static inline bool cpu_has_vmx_flexpriority(void)
  678. {
  679. return cpu_has_vmx_tpr_shadow() &&
  680. cpu_has_vmx_virtualize_apic_accesses();
  681. }
  682. static inline bool cpu_has_vmx_ept_execute_only(void)
  683. {
  684. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  685. }
  686. static inline bool cpu_has_vmx_eptp_uncacheable(void)
  687. {
  688. return vmx_capability.ept & VMX_EPTP_UC_BIT;
  689. }
  690. static inline bool cpu_has_vmx_eptp_writeback(void)
  691. {
  692. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  693. }
  694. static inline bool cpu_has_vmx_ept_2m_page(void)
  695. {
  696. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  697. }
  698. static inline bool cpu_has_vmx_ept_1g_page(void)
  699. {
  700. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  701. }
  702. static inline bool cpu_has_vmx_ept_4levels(void)
  703. {
  704. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  705. }
  706. static inline bool cpu_has_vmx_invept_individual_addr(void)
  707. {
  708. return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
  709. }
  710. static inline bool cpu_has_vmx_invept_context(void)
  711. {
  712. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  713. }
  714. static inline bool cpu_has_vmx_invept_global(void)
  715. {
  716. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  717. }
  718. static inline bool cpu_has_vmx_invvpid_single(void)
  719. {
  720. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  721. }
  722. static inline bool cpu_has_vmx_invvpid_global(void)
  723. {
  724. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  725. }
  726. static inline bool cpu_has_vmx_ept(void)
  727. {
  728. return vmcs_config.cpu_based_2nd_exec_ctrl &
  729. SECONDARY_EXEC_ENABLE_EPT;
  730. }
  731. static inline bool cpu_has_vmx_unrestricted_guest(void)
  732. {
  733. return vmcs_config.cpu_based_2nd_exec_ctrl &
  734. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  735. }
  736. static inline bool cpu_has_vmx_ple(void)
  737. {
  738. return vmcs_config.cpu_based_2nd_exec_ctrl &
  739. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  740. }
  741. static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
  742. {
  743. return flexpriority_enabled && irqchip_in_kernel(kvm);
  744. }
  745. static inline bool cpu_has_vmx_vpid(void)
  746. {
  747. return vmcs_config.cpu_based_2nd_exec_ctrl &
  748. SECONDARY_EXEC_ENABLE_VPID;
  749. }
  750. static inline bool cpu_has_vmx_rdtscp(void)
  751. {
  752. return vmcs_config.cpu_based_2nd_exec_ctrl &
  753. SECONDARY_EXEC_RDTSCP;
  754. }
  755. static inline bool cpu_has_virtual_nmis(void)
  756. {
  757. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  758. }
  759. static inline bool cpu_has_vmx_wbinvd_exit(void)
  760. {
  761. return vmcs_config.cpu_based_2nd_exec_ctrl &
  762. SECONDARY_EXEC_WBINVD_EXITING;
  763. }
  764. static inline bool report_flexpriority(void)
  765. {
  766. return flexpriority_enabled;
  767. }
  768. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  769. {
  770. return vmcs12->cpu_based_vm_exec_control & bit;
  771. }
  772. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  773. {
  774. return (vmcs12->cpu_based_vm_exec_control &
  775. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  776. (vmcs12->secondary_vm_exec_control & bit);
  777. }
  778. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12,
  779. struct kvm_vcpu *vcpu)
  780. {
  781. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  782. }
  783. static inline bool is_exception(u32 intr_info)
  784. {
  785. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  786. == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
  787. }
  788. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu);
  789. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  790. struct vmcs12 *vmcs12,
  791. u32 reason, unsigned long qualification);
  792. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  793. {
  794. int i;
  795. for (i = 0; i < vmx->nmsrs; ++i)
  796. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  797. return i;
  798. return -1;
  799. }
  800. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  801. {
  802. struct {
  803. u64 vpid : 16;
  804. u64 rsvd : 48;
  805. u64 gva;
  806. } operand = { vpid, 0, gva };
  807. asm volatile (__ex(ASM_VMX_INVVPID)
  808. /* CF==1 or ZF==1 --> rc = -1 */
  809. "; ja 1f ; ud2 ; 1:"
  810. : : "a"(&operand), "c"(ext) : "cc", "memory");
  811. }
  812. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  813. {
  814. struct {
  815. u64 eptp, gpa;
  816. } operand = {eptp, gpa};
  817. asm volatile (__ex(ASM_VMX_INVEPT)
  818. /* CF==1 or ZF==1 --> rc = -1 */
  819. "; ja 1f ; ud2 ; 1:\n"
  820. : : "a" (&operand), "c" (ext) : "cc", "memory");
  821. }
  822. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  823. {
  824. int i;
  825. i = __find_msr_index(vmx, msr);
  826. if (i >= 0)
  827. return &vmx->guest_msrs[i];
  828. return NULL;
  829. }
  830. static void vmcs_clear(struct vmcs *vmcs)
  831. {
  832. u64 phys_addr = __pa(vmcs);
  833. u8 error;
  834. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  835. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  836. : "cc", "memory");
  837. if (error)
  838. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  839. vmcs, phys_addr);
  840. }
  841. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  842. {
  843. vmcs_clear(loaded_vmcs->vmcs);
  844. loaded_vmcs->cpu = -1;
  845. loaded_vmcs->launched = 0;
  846. }
  847. static void vmcs_load(struct vmcs *vmcs)
  848. {
  849. u64 phys_addr = __pa(vmcs);
  850. u8 error;
  851. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  852. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  853. : "cc", "memory");
  854. if (error)
  855. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  856. vmcs, phys_addr);
  857. }
  858. static void __loaded_vmcs_clear(void *arg)
  859. {
  860. struct loaded_vmcs *loaded_vmcs = arg;
  861. int cpu = raw_smp_processor_id();
  862. if (loaded_vmcs->cpu != cpu)
  863. return; /* vcpu migration can race with cpu offline */
  864. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  865. per_cpu(current_vmcs, cpu) = NULL;
  866. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  867. loaded_vmcs_init(loaded_vmcs);
  868. }
  869. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  870. {
  871. if (loaded_vmcs->cpu != -1)
  872. smp_call_function_single(
  873. loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
  874. }
  875. static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
  876. {
  877. if (vmx->vpid == 0)
  878. return;
  879. if (cpu_has_vmx_invvpid_single())
  880. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  881. }
  882. static inline void vpid_sync_vcpu_global(void)
  883. {
  884. if (cpu_has_vmx_invvpid_global())
  885. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  886. }
  887. static inline void vpid_sync_context(struct vcpu_vmx *vmx)
  888. {
  889. if (cpu_has_vmx_invvpid_single())
  890. vpid_sync_vcpu_single(vmx);
  891. else
  892. vpid_sync_vcpu_global();
  893. }
  894. static inline void ept_sync_global(void)
  895. {
  896. if (cpu_has_vmx_invept_global())
  897. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  898. }
  899. static inline void ept_sync_context(u64 eptp)
  900. {
  901. if (enable_ept) {
  902. if (cpu_has_vmx_invept_context())
  903. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  904. else
  905. ept_sync_global();
  906. }
  907. }
  908. static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
  909. {
  910. if (enable_ept) {
  911. if (cpu_has_vmx_invept_individual_addr())
  912. __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
  913. eptp, gpa);
  914. else
  915. ept_sync_context(eptp);
  916. }
  917. }
  918. static __always_inline unsigned long vmcs_readl(unsigned long field)
  919. {
  920. unsigned long value;
  921. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  922. : "=a"(value) : "d"(field) : "cc");
  923. return value;
  924. }
  925. static __always_inline u16 vmcs_read16(unsigned long field)
  926. {
  927. return vmcs_readl(field);
  928. }
  929. static __always_inline u32 vmcs_read32(unsigned long field)
  930. {
  931. return vmcs_readl(field);
  932. }
  933. static __always_inline u64 vmcs_read64(unsigned long field)
  934. {
  935. #ifdef CONFIG_X86_64
  936. return vmcs_readl(field);
  937. #else
  938. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  939. #endif
  940. }
  941. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  942. {
  943. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  944. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  945. dump_stack();
  946. }
  947. static void vmcs_writel(unsigned long field, unsigned long value)
  948. {
  949. u8 error;
  950. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  951. : "=q"(error) : "a"(value), "d"(field) : "cc");
  952. if (unlikely(error))
  953. vmwrite_error(field, value);
  954. }
  955. static void vmcs_write16(unsigned long field, u16 value)
  956. {
  957. vmcs_writel(field, value);
  958. }
  959. static void vmcs_write32(unsigned long field, u32 value)
  960. {
  961. vmcs_writel(field, value);
  962. }
  963. static void vmcs_write64(unsigned long field, u64 value)
  964. {
  965. vmcs_writel(field, value);
  966. #ifndef CONFIG_X86_64
  967. asm volatile ("");
  968. vmcs_writel(field+1, value >> 32);
  969. #endif
  970. }
  971. static void vmcs_clear_bits(unsigned long field, u32 mask)
  972. {
  973. vmcs_writel(field, vmcs_readl(field) & ~mask);
  974. }
  975. static void vmcs_set_bits(unsigned long field, u32 mask)
  976. {
  977. vmcs_writel(field, vmcs_readl(field) | mask);
  978. }
  979. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  980. {
  981. vmx->segment_cache.bitmask = 0;
  982. }
  983. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  984. unsigned field)
  985. {
  986. bool ret;
  987. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  988. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  989. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  990. vmx->segment_cache.bitmask = 0;
  991. }
  992. ret = vmx->segment_cache.bitmask & mask;
  993. vmx->segment_cache.bitmask |= mask;
  994. return ret;
  995. }
  996. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  997. {
  998. u16 *p = &vmx->segment_cache.seg[seg].selector;
  999. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1000. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1001. return *p;
  1002. }
  1003. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1004. {
  1005. ulong *p = &vmx->segment_cache.seg[seg].base;
  1006. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1007. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1008. return *p;
  1009. }
  1010. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1011. {
  1012. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1013. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1014. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1015. return *p;
  1016. }
  1017. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1018. {
  1019. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1020. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1021. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1022. return *p;
  1023. }
  1024. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1025. {
  1026. u32 eb;
  1027. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1028. (1u << NM_VECTOR) | (1u << DB_VECTOR);
  1029. if ((vcpu->guest_debug &
  1030. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1031. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1032. eb |= 1u << BP_VECTOR;
  1033. if (to_vmx(vcpu)->rmode.vm86_active)
  1034. eb = ~0;
  1035. if (enable_ept)
  1036. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  1037. if (vcpu->fpu_active)
  1038. eb &= ~(1u << NM_VECTOR);
  1039. /* When we are running a nested L2 guest and L1 specified for it a
  1040. * certain exception bitmap, we must trap the same exceptions and pass
  1041. * them to L1. When running L2, we will only handle the exceptions
  1042. * specified above if L1 did not want them.
  1043. */
  1044. if (is_guest_mode(vcpu))
  1045. eb |= get_vmcs12(vcpu)->exception_bitmap;
  1046. vmcs_write32(EXCEPTION_BITMAP, eb);
  1047. }
  1048. static void clear_atomic_switch_msr_special(unsigned long entry,
  1049. unsigned long exit)
  1050. {
  1051. vmcs_clear_bits(VM_ENTRY_CONTROLS, entry);
  1052. vmcs_clear_bits(VM_EXIT_CONTROLS, exit);
  1053. }
  1054. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  1055. {
  1056. unsigned i;
  1057. struct msr_autoload *m = &vmx->msr_autoload;
  1058. switch (msr) {
  1059. case MSR_EFER:
  1060. if (cpu_has_load_ia32_efer) {
  1061. clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
  1062. VM_EXIT_LOAD_IA32_EFER);
  1063. return;
  1064. }
  1065. break;
  1066. case MSR_CORE_PERF_GLOBAL_CTRL:
  1067. if (cpu_has_load_perf_global_ctrl) {
  1068. clear_atomic_switch_msr_special(
  1069. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1070. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  1071. return;
  1072. }
  1073. break;
  1074. }
  1075. for (i = 0; i < m->nr; ++i)
  1076. if (m->guest[i].index == msr)
  1077. break;
  1078. if (i == m->nr)
  1079. return;
  1080. --m->nr;
  1081. m->guest[i] = m->guest[m->nr];
  1082. m->host[i] = m->host[m->nr];
  1083. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1084. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1085. }
  1086. static void add_atomic_switch_msr_special(unsigned long entry,
  1087. unsigned long exit, unsigned long guest_val_vmcs,
  1088. unsigned long host_val_vmcs, u64 guest_val, u64 host_val)
  1089. {
  1090. vmcs_write64(guest_val_vmcs, guest_val);
  1091. vmcs_write64(host_val_vmcs, host_val);
  1092. vmcs_set_bits(VM_ENTRY_CONTROLS, entry);
  1093. vmcs_set_bits(VM_EXIT_CONTROLS, exit);
  1094. }
  1095. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  1096. u64 guest_val, u64 host_val)
  1097. {
  1098. unsigned i;
  1099. struct msr_autoload *m = &vmx->msr_autoload;
  1100. switch (msr) {
  1101. case MSR_EFER:
  1102. if (cpu_has_load_ia32_efer) {
  1103. add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
  1104. VM_EXIT_LOAD_IA32_EFER,
  1105. GUEST_IA32_EFER,
  1106. HOST_IA32_EFER,
  1107. guest_val, host_val);
  1108. return;
  1109. }
  1110. break;
  1111. case MSR_CORE_PERF_GLOBAL_CTRL:
  1112. if (cpu_has_load_perf_global_ctrl) {
  1113. add_atomic_switch_msr_special(
  1114. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1115. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  1116. GUEST_IA32_PERF_GLOBAL_CTRL,
  1117. HOST_IA32_PERF_GLOBAL_CTRL,
  1118. guest_val, host_val);
  1119. return;
  1120. }
  1121. break;
  1122. }
  1123. for (i = 0; i < m->nr; ++i)
  1124. if (m->guest[i].index == msr)
  1125. break;
  1126. if (i == NR_AUTOLOAD_MSRS) {
  1127. printk_once(KERN_WARNING"Not enough mst switch entries. "
  1128. "Can't add msr %x\n", msr);
  1129. return;
  1130. } else if (i == m->nr) {
  1131. ++m->nr;
  1132. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1133. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1134. }
  1135. m->guest[i].index = msr;
  1136. m->guest[i].value = guest_val;
  1137. m->host[i].index = msr;
  1138. m->host[i].value = host_val;
  1139. }
  1140. static void reload_tss(void)
  1141. {
  1142. /*
  1143. * VT restores TR but not its size. Useless.
  1144. */
  1145. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  1146. struct desc_struct *descs;
  1147. descs = (void *)gdt->address;
  1148. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  1149. load_TR_desc();
  1150. }
  1151. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  1152. {
  1153. u64 guest_efer;
  1154. u64 ignore_bits;
  1155. guest_efer = vmx->vcpu.arch.efer;
  1156. /*
  1157. * NX is emulated; LMA and LME handled by hardware; SCE meaninless
  1158. * outside long mode
  1159. */
  1160. ignore_bits = EFER_NX | EFER_SCE;
  1161. #ifdef CONFIG_X86_64
  1162. ignore_bits |= EFER_LMA | EFER_LME;
  1163. /* SCE is meaningful only in long mode on Intel */
  1164. if (guest_efer & EFER_LMA)
  1165. ignore_bits &= ~(u64)EFER_SCE;
  1166. #endif
  1167. guest_efer &= ~ignore_bits;
  1168. guest_efer |= host_efer & ignore_bits;
  1169. vmx->guest_msrs[efer_offset].data = guest_efer;
  1170. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  1171. clear_atomic_switch_msr(vmx, MSR_EFER);
  1172. /* On ept, can't emulate nx, and must switch nx atomically */
  1173. if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
  1174. guest_efer = vmx->vcpu.arch.efer;
  1175. if (!(guest_efer & EFER_LMA))
  1176. guest_efer &= ~EFER_LME;
  1177. add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
  1178. return false;
  1179. }
  1180. return true;
  1181. }
  1182. static unsigned long segment_base(u16 selector)
  1183. {
  1184. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  1185. struct desc_struct *d;
  1186. unsigned long table_base;
  1187. unsigned long v;
  1188. if (!(selector & ~3))
  1189. return 0;
  1190. table_base = gdt->address;
  1191. if (selector & 4) { /* from ldt */
  1192. u16 ldt_selector = kvm_read_ldt();
  1193. if (!(ldt_selector & ~3))
  1194. return 0;
  1195. table_base = segment_base(ldt_selector);
  1196. }
  1197. d = (struct desc_struct *)(table_base + (selector & ~7));
  1198. v = get_desc_base(d);
  1199. #ifdef CONFIG_X86_64
  1200. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  1201. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  1202. #endif
  1203. return v;
  1204. }
  1205. static inline unsigned long kvm_read_tr_base(void)
  1206. {
  1207. u16 tr;
  1208. asm("str %0" : "=g"(tr));
  1209. return segment_base(tr);
  1210. }
  1211. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  1212. {
  1213. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1214. int i;
  1215. if (vmx->host_state.loaded)
  1216. return;
  1217. vmx->host_state.loaded = 1;
  1218. /*
  1219. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  1220. * allow segment selectors with cpl > 0 or ti == 1.
  1221. */
  1222. vmx->host_state.ldt_sel = kvm_read_ldt();
  1223. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  1224. savesegment(fs, vmx->host_state.fs_sel);
  1225. if (!(vmx->host_state.fs_sel & 7)) {
  1226. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  1227. vmx->host_state.fs_reload_needed = 0;
  1228. } else {
  1229. vmcs_write16(HOST_FS_SELECTOR, 0);
  1230. vmx->host_state.fs_reload_needed = 1;
  1231. }
  1232. savesegment(gs, vmx->host_state.gs_sel);
  1233. if (!(vmx->host_state.gs_sel & 7))
  1234. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  1235. else {
  1236. vmcs_write16(HOST_GS_SELECTOR, 0);
  1237. vmx->host_state.gs_ldt_reload_needed = 1;
  1238. }
  1239. #ifdef CONFIG_X86_64
  1240. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  1241. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  1242. #else
  1243. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  1244. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  1245. #endif
  1246. #ifdef CONFIG_X86_64
  1247. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1248. if (is_long_mode(&vmx->vcpu))
  1249. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1250. #endif
  1251. for (i = 0; i < vmx->save_nmsrs; ++i)
  1252. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  1253. vmx->guest_msrs[i].data,
  1254. vmx->guest_msrs[i].mask);
  1255. }
  1256. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  1257. {
  1258. if (!vmx->host_state.loaded)
  1259. return;
  1260. ++vmx->vcpu.stat.host_state_reload;
  1261. vmx->host_state.loaded = 0;
  1262. #ifdef CONFIG_X86_64
  1263. if (is_long_mode(&vmx->vcpu))
  1264. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1265. #endif
  1266. if (vmx->host_state.gs_ldt_reload_needed) {
  1267. kvm_load_ldt(vmx->host_state.ldt_sel);
  1268. #ifdef CONFIG_X86_64
  1269. load_gs_index(vmx->host_state.gs_sel);
  1270. #else
  1271. loadsegment(gs, vmx->host_state.gs_sel);
  1272. #endif
  1273. }
  1274. if (vmx->host_state.fs_reload_needed)
  1275. loadsegment(fs, vmx->host_state.fs_sel);
  1276. reload_tss();
  1277. #ifdef CONFIG_X86_64
  1278. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1279. #endif
  1280. if (user_has_fpu())
  1281. clts();
  1282. load_gdt(&__get_cpu_var(host_gdt));
  1283. }
  1284. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  1285. {
  1286. preempt_disable();
  1287. __vmx_load_host_state(vmx);
  1288. preempt_enable();
  1289. }
  1290. /*
  1291. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  1292. * vcpu mutex is already taken.
  1293. */
  1294. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1295. {
  1296. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1297. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  1298. if (!vmm_exclusive)
  1299. kvm_cpu_vmxon(phys_addr);
  1300. else if (vmx->loaded_vmcs->cpu != cpu)
  1301. loaded_vmcs_clear(vmx->loaded_vmcs);
  1302. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  1303. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  1304. vmcs_load(vmx->loaded_vmcs->vmcs);
  1305. }
  1306. if (vmx->loaded_vmcs->cpu != cpu) {
  1307. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  1308. unsigned long sysenter_esp;
  1309. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1310. local_irq_disable();
  1311. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  1312. &per_cpu(loaded_vmcss_on_cpu, cpu));
  1313. local_irq_enable();
  1314. /*
  1315. * Linux uses per-cpu TSS and GDT, so set these when switching
  1316. * processors.
  1317. */
  1318. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  1319. vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
  1320. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  1321. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  1322. vmx->loaded_vmcs->cpu = cpu;
  1323. }
  1324. }
  1325. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  1326. {
  1327. __vmx_load_host_state(to_vmx(vcpu));
  1328. if (!vmm_exclusive) {
  1329. __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
  1330. vcpu->cpu = -1;
  1331. kvm_cpu_vmxoff();
  1332. }
  1333. }
  1334. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  1335. {
  1336. ulong cr0;
  1337. if (vcpu->fpu_active)
  1338. return;
  1339. vcpu->fpu_active = 1;
  1340. cr0 = vmcs_readl(GUEST_CR0);
  1341. cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
  1342. cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
  1343. vmcs_writel(GUEST_CR0, cr0);
  1344. update_exception_bitmap(vcpu);
  1345. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  1346. if (is_guest_mode(vcpu))
  1347. vcpu->arch.cr0_guest_owned_bits &=
  1348. ~get_vmcs12(vcpu)->cr0_guest_host_mask;
  1349. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1350. }
  1351. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  1352. /*
  1353. * Return the cr0 value that a nested guest would read. This is a combination
  1354. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  1355. * its hypervisor (cr0_read_shadow).
  1356. */
  1357. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  1358. {
  1359. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  1360. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  1361. }
  1362. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  1363. {
  1364. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  1365. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  1366. }
  1367. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  1368. {
  1369. /* Note that there is no vcpu->fpu_active = 0 here. The caller must
  1370. * set this *before* calling this function.
  1371. */
  1372. vmx_decache_cr0_guest_bits(vcpu);
  1373. vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
  1374. update_exception_bitmap(vcpu);
  1375. vcpu->arch.cr0_guest_owned_bits = 0;
  1376. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1377. if (is_guest_mode(vcpu)) {
  1378. /*
  1379. * L1's specified read shadow might not contain the TS bit,
  1380. * so now that we turned on shadowing of this bit, we need to
  1381. * set this bit of the shadow. Like in nested_vmx_run we need
  1382. * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
  1383. * up-to-date here because we just decached cr0.TS (and we'll
  1384. * only update vmcs12->guest_cr0 on nested exit).
  1385. */
  1386. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1387. vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
  1388. (vcpu->arch.cr0 & X86_CR0_TS);
  1389. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  1390. } else
  1391. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  1392. }
  1393. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  1394. {
  1395. unsigned long rflags, save_rflags;
  1396. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  1397. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1398. rflags = vmcs_readl(GUEST_RFLAGS);
  1399. if (to_vmx(vcpu)->rmode.vm86_active) {
  1400. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1401. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  1402. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1403. }
  1404. to_vmx(vcpu)->rflags = rflags;
  1405. }
  1406. return to_vmx(vcpu)->rflags;
  1407. }
  1408. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1409. {
  1410. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1411. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1412. to_vmx(vcpu)->rflags = rflags;
  1413. if (to_vmx(vcpu)->rmode.vm86_active) {
  1414. to_vmx(vcpu)->rmode.save_rflags = rflags;
  1415. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1416. }
  1417. vmcs_writel(GUEST_RFLAGS, rflags);
  1418. }
  1419. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1420. {
  1421. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1422. int ret = 0;
  1423. if (interruptibility & GUEST_INTR_STATE_STI)
  1424. ret |= KVM_X86_SHADOW_INT_STI;
  1425. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  1426. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  1427. return ret & mask;
  1428. }
  1429. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1430. {
  1431. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1432. u32 interruptibility = interruptibility_old;
  1433. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  1434. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  1435. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  1436. else if (mask & KVM_X86_SHADOW_INT_STI)
  1437. interruptibility |= GUEST_INTR_STATE_STI;
  1438. if ((interruptibility != interruptibility_old))
  1439. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  1440. }
  1441. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  1442. {
  1443. unsigned long rip;
  1444. rip = kvm_rip_read(vcpu);
  1445. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  1446. kvm_rip_write(vcpu, rip);
  1447. /* skipping an emulated instruction also counts */
  1448. vmx_set_interrupt_shadow(vcpu, 0);
  1449. }
  1450. /*
  1451. * KVM wants to inject page-faults which it got to the guest. This function
  1452. * checks whether in a nested guest, we need to inject them to L1 or L2.
  1453. * This function assumes it is called with the exit reason in vmcs02 being
  1454. * a #PF exception (this is the only case in which KVM injects a #PF when L2
  1455. * is running).
  1456. */
  1457. static int nested_pf_handled(struct kvm_vcpu *vcpu)
  1458. {
  1459. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1460. /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
  1461. if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
  1462. return 0;
  1463. nested_vmx_vmexit(vcpu);
  1464. return 1;
  1465. }
  1466. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  1467. bool has_error_code, u32 error_code,
  1468. bool reinject)
  1469. {
  1470. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1471. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  1472. if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
  1473. nested_pf_handled(vcpu))
  1474. return;
  1475. if (has_error_code) {
  1476. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  1477. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  1478. }
  1479. if (vmx->rmode.vm86_active) {
  1480. int inc_eip = 0;
  1481. if (kvm_exception_is_soft(nr))
  1482. inc_eip = vcpu->arch.event_exit_inst_len;
  1483. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  1484. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  1485. return;
  1486. }
  1487. if (kvm_exception_is_soft(nr)) {
  1488. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  1489. vmx->vcpu.arch.event_exit_inst_len);
  1490. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  1491. } else
  1492. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  1493. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  1494. }
  1495. static bool vmx_rdtscp_supported(void)
  1496. {
  1497. return cpu_has_vmx_rdtscp();
  1498. }
  1499. /*
  1500. * Swap MSR entry in host/guest MSR entry array.
  1501. */
  1502. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  1503. {
  1504. struct shared_msr_entry tmp;
  1505. tmp = vmx->guest_msrs[to];
  1506. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  1507. vmx->guest_msrs[from] = tmp;
  1508. }
  1509. /*
  1510. * Set up the vmcs to automatically save and restore system
  1511. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  1512. * mode, as fiddling with msrs is very expensive.
  1513. */
  1514. static void setup_msrs(struct vcpu_vmx *vmx)
  1515. {
  1516. int save_nmsrs, index;
  1517. unsigned long *msr_bitmap;
  1518. save_nmsrs = 0;
  1519. #ifdef CONFIG_X86_64
  1520. if (is_long_mode(&vmx->vcpu)) {
  1521. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  1522. if (index >= 0)
  1523. move_msr_up(vmx, index, save_nmsrs++);
  1524. index = __find_msr_index(vmx, MSR_LSTAR);
  1525. if (index >= 0)
  1526. move_msr_up(vmx, index, save_nmsrs++);
  1527. index = __find_msr_index(vmx, MSR_CSTAR);
  1528. if (index >= 0)
  1529. move_msr_up(vmx, index, save_nmsrs++);
  1530. index = __find_msr_index(vmx, MSR_TSC_AUX);
  1531. if (index >= 0 && vmx->rdtscp_enabled)
  1532. move_msr_up(vmx, index, save_nmsrs++);
  1533. /*
  1534. * MSR_STAR is only needed on long mode guests, and only
  1535. * if efer.sce is enabled.
  1536. */
  1537. index = __find_msr_index(vmx, MSR_STAR);
  1538. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  1539. move_msr_up(vmx, index, save_nmsrs++);
  1540. }
  1541. #endif
  1542. index = __find_msr_index(vmx, MSR_EFER);
  1543. if (index >= 0 && update_transition_efer(vmx, index))
  1544. move_msr_up(vmx, index, save_nmsrs++);
  1545. vmx->save_nmsrs = save_nmsrs;
  1546. if (cpu_has_vmx_msr_bitmap()) {
  1547. if (is_long_mode(&vmx->vcpu))
  1548. msr_bitmap = vmx_msr_bitmap_longmode;
  1549. else
  1550. msr_bitmap = vmx_msr_bitmap_legacy;
  1551. vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
  1552. }
  1553. }
  1554. /*
  1555. * reads and returns guest's timestamp counter "register"
  1556. * guest_tsc = host_tsc + tsc_offset -- 21.3
  1557. */
  1558. static u64 guest_read_tsc(void)
  1559. {
  1560. u64 host_tsc, tsc_offset;
  1561. rdtscll(host_tsc);
  1562. tsc_offset = vmcs_read64(TSC_OFFSET);
  1563. return host_tsc + tsc_offset;
  1564. }
  1565. /*
  1566. * Like guest_read_tsc, but always returns L1's notion of the timestamp
  1567. * counter, even if a nested guest (L2) is currently running.
  1568. */
  1569. u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu)
  1570. {
  1571. u64 host_tsc, tsc_offset;
  1572. rdtscll(host_tsc);
  1573. tsc_offset = is_guest_mode(vcpu) ?
  1574. to_vmx(vcpu)->nested.vmcs01_tsc_offset :
  1575. vmcs_read64(TSC_OFFSET);
  1576. return host_tsc + tsc_offset;
  1577. }
  1578. /*
  1579. * Engage any workarounds for mis-matched TSC rates. Currently limited to
  1580. * software catchup for faster rates on slower CPUs.
  1581. */
  1582. static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
  1583. {
  1584. if (!scale)
  1585. return;
  1586. if (user_tsc_khz > tsc_khz) {
  1587. vcpu->arch.tsc_catchup = 1;
  1588. vcpu->arch.tsc_always_catchup = 1;
  1589. } else
  1590. WARN(1, "user requested TSC rate below hardware speed\n");
  1591. }
  1592. /*
  1593. * writes 'offset' into guest's timestamp counter offset register
  1594. */
  1595. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1596. {
  1597. if (is_guest_mode(vcpu)) {
  1598. /*
  1599. * We're here if L1 chose not to trap WRMSR to TSC. According
  1600. * to the spec, this should set L1's TSC; The offset that L1
  1601. * set for L2 remains unchanged, and still needs to be added
  1602. * to the newly set TSC to get L2's TSC.
  1603. */
  1604. struct vmcs12 *vmcs12;
  1605. to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
  1606. /* recalculate vmcs02.TSC_OFFSET: */
  1607. vmcs12 = get_vmcs12(vcpu);
  1608. vmcs_write64(TSC_OFFSET, offset +
  1609. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  1610. vmcs12->tsc_offset : 0));
  1611. } else {
  1612. vmcs_write64(TSC_OFFSET, offset);
  1613. }
  1614. }
  1615. static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
  1616. {
  1617. u64 offset = vmcs_read64(TSC_OFFSET);
  1618. vmcs_write64(TSC_OFFSET, offset + adjustment);
  1619. if (is_guest_mode(vcpu)) {
  1620. /* Even when running L2, the adjustment needs to apply to L1 */
  1621. to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
  1622. }
  1623. }
  1624. static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  1625. {
  1626. return target_tsc - native_read_tsc();
  1627. }
  1628. static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
  1629. {
  1630. struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
  1631. return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
  1632. }
  1633. /*
  1634. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  1635. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  1636. * all guests if the "nested" module option is off, and can also be disabled
  1637. * for a single guest by disabling its VMX cpuid bit.
  1638. */
  1639. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  1640. {
  1641. return nested && guest_cpuid_has_vmx(vcpu);
  1642. }
  1643. /*
  1644. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  1645. * returned for the various VMX controls MSRs when nested VMX is enabled.
  1646. * The same values should also be used to verify that vmcs12 control fields are
  1647. * valid during nested entry from L1 to L2.
  1648. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  1649. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  1650. * bit in the high half is on if the corresponding bit in the control field
  1651. * may be on. See also vmx_control_verify().
  1652. * TODO: allow these variables to be modified (downgraded) by module options
  1653. * or other means.
  1654. */
  1655. static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
  1656. static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
  1657. static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
  1658. static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
  1659. static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
  1660. static __init void nested_vmx_setup_ctls_msrs(void)
  1661. {
  1662. /*
  1663. * Note that as a general rule, the high half of the MSRs (bits in
  1664. * the control fields which may be 1) should be initialized by the
  1665. * intersection of the underlying hardware's MSR (i.e., features which
  1666. * can be supported) and the list of features we want to expose -
  1667. * because they are known to be properly supported in our code.
  1668. * Also, usually, the low half of the MSRs (bits which must be 1) can
  1669. * be set to 0, meaning that L1 may turn off any of these bits. The
  1670. * reason is that if one of these bits is necessary, it will appear
  1671. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  1672. * fields of vmcs01 and vmcs02, will turn these bits off - and
  1673. * nested_vmx_exit_handled() will not pass related exits to L1.
  1674. * These rules have exceptions below.
  1675. */
  1676. /* pin-based controls */
  1677. /*
  1678. * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
  1679. * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
  1680. */
  1681. nested_vmx_pinbased_ctls_low = 0x16 ;
  1682. nested_vmx_pinbased_ctls_high = 0x16 |
  1683. PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
  1684. PIN_BASED_VIRTUAL_NMIS;
  1685. /* exit controls */
  1686. nested_vmx_exit_ctls_low = 0;
  1687. /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
  1688. #ifdef CONFIG_X86_64
  1689. nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
  1690. #else
  1691. nested_vmx_exit_ctls_high = 0;
  1692. #endif
  1693. /* entry controls */
  1694. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  1695. nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
  1696. nested_vmx_entry_ctls_low = 0;
  1697. nested_vmx_entry_ctls_high &=
  1698. VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
  1699. /* cpu-based controls */
  1700. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  1701. nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
  1702. nested_vmx_procbased_ctls_low = 0;
  1703. nested_vmx_procbased_ctls_high &=
  1704. CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  1705. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  1706. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  1707. CPU_BASED_CR3_STORE_EXITING |
  1708. #ifdef CONFIG_X86_64
  1709. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  1710. #endif
  1711. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  1712. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
  1713. CPU_BASED_RDPMC_EXITING |
  1714. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1715. /*
  1716. * We can allow some features even when not supported by the
  1717. * hardware. For example, L1 can specify an MSR bitmap - and we
  1718. * can use it to avoid exits to L1 - even when L0 runs L2
  1719. * without MSR bitmaps.
  1720. */
  1721. nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
  1722. /* secondary cpu-based controls */
  1723. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  1724. nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
  1725. nested_vmx_secondary_ctls_low = 0;
  1726. nested_vmx_secondary_ctls_high &=
  1727. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1728. }
  1729. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  1730. {
  1731. /*
  1732. * Bits 0 in high must be 0, and bits 1 in low must be 1.
  1733. */
  1734. return ((control & high) | low) == control;
  1735. }
  1736. static inline u64 vmx_control_msr(u32 low, u32 high)
  1737. {
  1738. return low | ((u64)high << 32);
  1739. }
  1740. /*
  1741. * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
  1742. * also let it use VMX-specific MSRs.
  1743. * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
  1744. * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
  1745. * like all other MSRs).
  1746. */
  1747. static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1748. {
  1749. if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
  1750. msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
  1751. /*
  1752. * According to the spec, processors which do not support VMX
  1753. * should throw a #GP(0) when VMX capability MSRs are read.
  1754. */
  1755. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  1756. return 1;
  1757. }
  1758. switch (msr_index) {
  1759. case MSR_IA32_FEATURE_CONTROL:
  1760. *pdata = 0;
  1761. break;
  1762. case MSR_IA32_VMX_BASIC:
  1763. /*
  1764. * This MSR reports some information about VMX support. We
  1765. * should return information about the VMX we emulate for the
  1766. * guest, and the VMCS structure we give it - not about the
  1767. * VMX support of the underlying hardware.
  1768. */
  1769. *pdata = VMCS12_REVISION |
  1770. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  1771. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  1772. break;
  1773. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  1774. case MSR_IA32_VMX_PINBASED_CTLS:
  1775. *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
  1776. nested_vmx_pinbased_ctls_high);
  1777. break;
  1778. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  1779. case MSR_IA32_VMX_PROCBASED_CTLS:
  1780. *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
  1781. nested_vmx_procbased_ctls_high);
  1782. break;
  1783. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  1784. case MSR_IA32_VMX_EXIT_CTLS:
  1785. *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
  1786. nested_vmx_exit_ctls_high);
  1787. break;
  1788. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  1789. case MSR_IA32_VMX_ENTRY_CTLS:
  1790. *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
  1791. nested_vmx_entry_ctls_high);
  1792. break;
  1793. case MSR_IA32_VMX_MISC:
  1794. *pdata = 0;
  1795. break;
  1796. /*
  1797. * These MSRs specify bits which the guest must keep fixed (on or off)
  1798. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  1799. * We picked the standard core2 setting.
  1800. */
  1801. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  1802. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  1803. case MSR_IA32_VMX_CR0_FIXED0:
  1804. *pdata = VMXON_CR0_ALWAYSON;
  1805. break;
  1806. case MSR_IA32_VMX_CR0_FIXED1:
  1807. *pdata = -1ULL;
  1808. break;
  1809. case MSR_IA32_VMX_CR4_FIXED0:
  1810. *pdata = VMXON_CR4_ALWAYSON;
  1811. break;
  1812. case MSR_IA32_VMX_CR4_FIXED1:
  1813. *pdata = -1ULL;
  1814. break;
  1815. case MSR_IA32_VMX_VMCS_ENUM:
  1816. *pdata = 0x1f;
  1817. break;
  1818. case MSR_IA32_VMX_PROCBASED_CTLS2:
  1819. *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
  1820. nested_vmx_secondary_ctls_high);
  1821. break;
  1822. case MSR_IA32_VMX_EPT_VPID_CAP:
  1823. /* Currently, no nested ept or nested vpid */
  1824. *pdata = 0;
  1825. break;
  1826. default:
  1827. return 0;
  1828. }
  1829. return 1;
  1830. }
  1831. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1832. {
  1833. if (!nested_vmx_allowed(vcpu))
  1834. return 0;
  1835. if (msr_index == MSR_IA32_FEATURE_CONTROL)
  1836. /* TODO: the right thing. */
  1837. return 1;
  1838. /*
  1839. * No need to treat VMX capability MSRs specially: If we don't handle
  1840. * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
  1841. */
  1842. return 0;
  1843. }
  1844. /*
  1845. * Reads an msr value (of 'msr_index') into 'pdata'.
  1846. * Returns 0 on success, non-0 otherwise.
  1847. * Assumes vcpu_load() was already called.
  1848. */
  1849. static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1850. {
  1851. u64 data;
  1852. struct shared_msr_entry *msr;
  1853. if (!pdata) {
  1854. printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
  1855. return -EINVAL;
  1856. }
  1857. switch (msr_index) {
  1858. #ifdef CONFIG_X86_64
  1859. case MSR_FS_BASE:
  1860. data = vmcs_readl(GUEST_FS_BASE);
  1861. break;
  1862. case MSR_GS_BASE:
  1863. data = vmcs_readl(GUEST_GS_BASE);
  1864. break;
  1865. case MSR_KERNEL_GS_BASE:
  1866. vmx_load_host_state(to_vmx(vcpu));
  1867. data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
  1868. break;
  1869. #endif
  1870. case MSR_EFER:
  1871. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1872. case MSR_IA32_TSC:
  1873. data = guest_read_tsc();
  1874. break;
  1875. case MSR_IA32_SYSENTER_CS:
  1876. data = vmcs_read32(GUEST_SYSENTER_CS);
  1877. break;
  1878. case MSR_IA32_SYSENTER_EIP:
  1879. data = vmcs_readl(GUEST_SYSENTER_EIP);
  1880. break;
  1881. case MSR_IA32_SYSENTER_ESP:
  1882. data = vmcs_readl(GUEST_SYSENTER_ESP);
  1883. break;
  1884. case MSR_TSC_AUX:
  1885. if (!to_vmx(vcpu)->rdtscp_enabled)
  1886. return 1;
  1887. /* Otherwise falls through */
  1888. default:
  1889. if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
  1890. return 0;
  1891. msr = find_msr_entry(to_vmx(vcpu), msr_index);
  1892. if (msr) {
  1893. data = msr->data;
  1894. break;
  1895. }
  1896. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1897. }
  1898. *pdata = data;
  1899. return 0;
  1900. }
  1901. /*
  1902. * Writes msr value into into the appropriate "register".
  1903. * Returns 0 on success, non-0 otherwise.
  1904. * Assumes vcpu_load() was already called.
  1905. */
  1906. static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1907. {
  1908. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1909. struct shared_msr_entry *msr;
  1910. int ret = 0;
  1911. switch (msr_index) {
  1912. case MSR_EFER:
  1913. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1914. break;
  1915. #ifdef CONFIG_X86_64
  1916. case MSR_FS_BASE:
  1917. vmx_segment_cache_clear(vmx);
  1918. vmcs_writel(GUEST_FS_BASE, data);
  1919. break;
  1920. case MSR_GS_BASE:
  1921. vmx_segment_cache_clear(vmx);
  1922. vmcs_writel(GUEST_GS_BASE, data);
  1923. break;
  1924. case MSR_KERNEL_GS_BASE:
  1925. vmx_load_host_state(vmx);
  1926. vmx->msr_guest_kernel_gs_base = data;
  1927. break;
  1928. #endif
  1929. case MSR_IA32_SYSENTER_CS:
  1930. vmcs_write32(GUEST_SYSENTER_CS, data);
  1931. break;
  1932. case MSR_IA32_SYSENTER_EIP:
  1933. vmcs_writel(GUEST_SYSENTER_EIP, data);
  1934. break;
  1935. case MSR_IA32_SYSENTER_ESP:
  1936. vmcs_writel(GUEST_SYSENTER_ESP, data);
  1937. break;
  1938. case MSR_IA32_TSC:
  1939. kvm_write_tsc(vcpu, data);
  1940. break;
  1941. case MSR_IA32_CR_PAT:
  1942. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  1943. vmcs_write64(GUEST_IA32_PAT, data);
  1944. vcpu->arch.pat = data;
  1945. break;
  1946. }
  1947. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1948. break;
  1949. case MSR_TSC_AUX:
  1950. if (!vmx->rdtscp_enabled)
  1951. return 1;
  1952. /* Check reserved bit, higher 32 bits should be zero */
  1953. if ((data >> 32) != 0)
  1954. return 1;
  1955. /* Otherwise falls through */
  1956. default:
  1957. if (vmx_set_vmx_msr(vcpu, msr_index, data))
  1958. break;
  1959. msr = find_msr_entry(vmx, msr_index);
  1960. if (msr) {
  1961. msr->data = data;
  1962. if (msr - vmx->guest_msrs < vmx->save_nmsrs)
  1963. kvm_set_shared_msr(msr->index, msr->data,
  1964. msr->mask);
  1965. break;
  1966. }
  1967. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1968. }
  1969. return ret;
  1970. }
  1971. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1972. {
  1973. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  1974. switch (reg) {
  1975. case VCPU_REGS_RSP:
  1976. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  1977. break;
  1978. case VCPU_REGS_RIP:
  1979. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  1980. break;
  1981. case VCPU_EXREG_PDPTR:
  1982. if (enable_ept)
  1983. ept_save_pdptrs(vcpu);
  1984. break;
  1985. default:
  1986. break;
  1987. }
  1988. }
  1989. static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  1990. {
  1991. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  1992. vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
  1993. else
  1994. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  1995. update_exception_bitmap(vcpu);
  1996. }
  1997. static __init int cpu_has_kvm_support(void)
  1998. {
  1999. return cpu_has_vmx();
  2000. }
  2001. static __init int vmx_disabled_by_bios(void)
  2002. {
  2003. u64 msr;
  2004. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  2005. if (msr & FEATURE_CONTROL_LOCKED) {
  2006. /* launched w/ TXT and VMX disabled */
  2007. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2008. && tboot_enabled())
  2009. return 1;
  2010. /* launched w/o TXT and VMX only enabled w/ TXT */
  2011. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2012. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2013. && !tboot_enabled()) {
  2014. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  2015. "activate TXT before enabling KVM\n");
  2016. return 1;
  2017. }
  2018. /* launched w/o TXT and VMX disabled */
  2019. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2020. && !tboot_enabled())
  2021. return 1;
  2022. }
  2023. return 0;
  2024. }
  2025. static void kvm_cpu_vmxon(u64 addr)
  2026. {
  2027. asm volatile (ASM_VMX_VMXON_RAX
  2028. : : "a"(&addr), "m"(addr)
  2029. : "memory", "cc");
  2030. }
  2031. static int hardware_enable(void *garbage)
  2032. {
  2033. int cpu = raw_smp_processor_id();
  2034. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  2035. u64 old, test_bits;
  2036. if (read_cr4() & X86_CR4_VMXE)
  2037. return -EBUSY;
  2038. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  2039. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  2040. test_bits = FEATURE_CONTROL_LOCKED;
  2041. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  2042. if (tboot_enabled())
  2043. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  2044. if ((old & test_bits) != test_bits) {
  2045. /* enable and lock */
  2046. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  2047. }
  2048. write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
  2049. if (vmm_exclusive) {
  2050. kvm_cpu_vmxon(phys_addr);
  2051. ept_sync_global();
  2052. }
  2053. store_gdt(&__get_cpu_var(host_gdt));
  2054. return 0;
  2055. }
  2056. static void vmclear_local_loaded_vmcss(void)
  2057. {
  2058. int cpu = raw_smp_processor_id();
  2059. struct loaded_vmcs *v, *n;
  2060. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  2061. loaded_vmcss_on_cpu_link)
  2062. __loaded_vmcs_clear(v);
  2063. }
  2064. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  2065. * tricks.
  2066. */
  2067. static void kvm_cpu_vmxoff(void)
  2068. {
  2069. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  2070. }
  2071. static void hardware_disable(void *garbage)
  2072. {
  2073. if (vmm_exclusive) {
  2074. vmclear_local_loaded_vmcss();
  2075. kvm_cpu_vmxoff();
  2076. }
  2077. write_cr4(read_cr4() & ~X86_CR4_VMXE);
  2078. }
  2079. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  2080. u32 msr, u32 *result)
  2081. {
  2082. u32 vmx_msr_low, vmx_msr_high;
  2083. u32 ctl = ctl_min | ctl_opt;
  2084. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2085. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  2086. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  2087. /* Ensure minimum (required) set of control bits are supported. */
  2088. if (ctl_min & ~ctl)
  2089. return -EIO;
  2090. *result = ctl;
  2091. return 0;
  2092. }
  2093. static __init bool allow_1_setting(u32 msr, u32 ctl)
  2094. {
  2095. u32 vmx_msr_low, vmx_msr_high;
  2096. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2097. return vmx_msr_high & ctl;
  2098. }
  2099. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  2100. {
  2101. u32 vmx_msr_low, vmx_msr_high;
  2102. u32 min, opt, min2, opt2;
  2103. u32 _pin_based_exec_control = 0;
  2104. u32 _cpu_based_exec_control = 0;
  2105. u32 _cpu_based_2nd_exec_control = 0;
  2106. u32 _vmexit_control = 0;
  2107. u32 _vmentry_control = 0;
  2108. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  2109. opt = PIN_BASED_VIRTUAL_NMIS;
  2110. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  2111. &_pin_based_exec_control) < 0)
  2112. return -EIO;
  2113. min = CPU_BASED_HLT_EXITING |
  2114. #ifdef CONFIG_X86_64
  2115. CPU_BASED_CR8_LOAD_EXITING |
  2116. CPU_BASED_CR8_STORE_EXITING |
  2117. #endif
  2118. CPU_BASED_CR3_LOAD_EXITING |
  2119. CPU_BASED_CR3_STORE_EXITING |
  2120. CPU_BASED_USE_IO_BITMAPS |
  2121. CPU_BASED_MOV_DR_EXITING |
  2122. CPU_BASED_USE_TSC_OFFSETING |
  2123. CPU_BASED_MWAIT_EXITING |
  2124. CPU_BASED_MONITOR_EXITING |
  2125. CPU_BASED_INVLPG_EXITING |
  2126. CPU_BASED_RDPMC_EXITING;
  2127. opt = CPU_BASED_TPR_SHADOW |
  2128. CPU_BASED_USE_MSR_BITMAPS |
  2129. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2130. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  2131. &_cpu_based_exec_control) < 0)
  2132. return -EIO;
  2133. #ifdef CONFIG_X86_64
  2134. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  2135. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  2136. ~CPU_BASED_CR8_STORE_EXITING;
  2137. #endif
  2138. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  2139. min2 = 0;
  2140. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2141. SECONDARY_EXEC_WBINVD_EXITING |
  2142. SECONDARY_EXEC_ENABLE_VPID |
  2143. SECONDARY_EXEC_ENABLE_EPT |
  2144. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  2145. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  2146. SECONDARY_EXEC_RDTSCP;
  2147. if (adjust_vmx_controls(min2, opt2,
  2148. MSR_IA32_VMX_PROCBASED_CTLS2,
  2149. &_cpu_based_2nd_exec_control) < 0)
  2150. return -EIO;
  2151. }
  2152. #ifndef CONFIG_X86_64
  2153. if (!(_cpu_based_2nd_exec_control &
  2154. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  2155. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  2156. #endif
  2157. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  2158. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  2159. enabled */
  2160. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  2161. CPU_BASED_CR3_STORE_EXITING |
  2162. CPU_BASED_INVLPG_EXITING);
  2163. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  2164. vmx_capability.ept, vmx_capability.vpid);
  2165. }
  2166. min = 0;
  2167. #ifdef CONFIG_X86_64
  2168. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  2169. #endif
  2170. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
  2171. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  2172. &_vmexit_control) < 0)
  2173. return -EIO;
  2174. min = 0;
  2175. opt = VM_ENTRY_LOAD_IA32_PAT;
  2176. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  2177. &_vmentry_control) < 0)
  2178. return -EIO;
  2179. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  2180. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  2181. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  2182. return -EIO;
  2183. #ifdef CONFIG_X86_64
  2184. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  2185. if (vmx_msr_high & (1u<<16))
  2186. return -EIO;
  2187. #endif
  2188. /* Require Write-Back (WB) memory type for VMCS accesses. */
  2189. if (((vmx_msr_high >> 18) & 15) != 6)
  2190. return -EIO;
  2191. vmcs_conf->size = vmx_msr_high & 0x1fff;
  2192. vmcs_conf->order = get_order(vmcs_config.size);
  2193. vmcs_conf->revision_id = vmx_msr_low;
  2194. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  2195. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  2196. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  2197. vmcs_conf->vmexit_ctrl = _vmexit_control;
  2198. vmcs_conf->vmentry_ctrl = _vmentry_control;
  2199. cpu_has_load_ia32_efer =
  2200. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2201. VM_ENTRY_LOAD_IA32_EFER)
  2202. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2203. VM_EXIT_LOAD_IA32_EFER);
  2204. cpu_has_load_perf_global_ctrl =
  2205. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2206. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  2207. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2208. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2209. /*
  2210. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  2211. * but due to arrata below it can't be used. Workaround is to use
  2212. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  2213. *
  2214. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  2215. *
  2216. * AAK155 (model 26)
  2217. * AAP115 (model 30)
  2218. * AAT100 (model 37)
  2219. * BC86,AAY89,BD102 (model 44)
  2220. * BA97 (model 46)
  2221. *
  2222. */
  2223. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  2224. switch (boot_cpu_data.x86_model) {
  2225. case 26:
  2226. case 30:
  2227. case 37:
  2228. case 44:
  2229. case 46:
  2230. cpu_has_load_perf_global_ctrl = false;
  2231. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  2232. "does not work properly. Using workaround\n");
  2233. break;
  2234. default:
  2235. break;
  2236. }
  2237. }
  2238. return 0;
  2239. }
  2240. static struct vmcs *alloc_vmcs_cpu(int cpu)
  2241. {
  2242. int node = cpu_to_node(cpu);
  2243. struct page *pages;
  2244. struct vmcs *vmcs;
  2245. pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
  2246. if (!pages)
  2247. return NULL;
  2248. vmcs = page_address(pages);
  2249. memset(vmcs, 0, vmcs_config.size);
  2250. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  2251. return vmcs;
  2252. }
  2253. static struct vmcs *alloc_vmcs(void)
  2254. {
  2255. return alloc_vmcs_cpu(raw_smp_processor_id());
  2256. }
  2257. static void free_vmcs(struct vmcs *vmcs)
  2258. {
  2259. free_pages((unsigned long)vmcs, vmcs_config.order);
  2260. }
  2261. /*
  2262. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  2263. */
  2264. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  2265. {
  2266. if (!loaded_vmcs->vmcs)
  2267. return;
  2268. loaded_vmcs_clear(loaded_vmcs);
  2269. free_vmcs(loaded_vmcs->vmcs);
  2270. loaded_vmcs->vmcs = NULL;
  2271. }
  2272. static void free_kvm_area(void)
  2273. {
  2274. int cpu;
  2275. for_each_possible_cpu(cpu) {
  2276. free_vmcs(per_cpu(vmxarea, cpu));
  2277. per_cpu(vmxarea, cpu) = NULL;
  2278. }
  2279. }
  2280. static __init int alloc_kvm_area(void)
  2281. {
  2282. int cpu;
  2283. for_each_possible_cpu(cpu) {
  2284. struct vmcs *vmcs;
  2285. vmcs = alloc_vmcs_cpu(cpu);
  2286. if (!vmcs) {
  2287. free_kvm_area();
  2288. return -ENOMEM;
  2289. }
  2290. per_cpu(vmxarea, cpu) = vmcs;
  2291. }
  2292. return 0;
  2293. }
  2294. static __init int hardware_setup(void)
  2295. {
  2296. if (setup_vmcs_config(&vmcs_config) < 0)
  2297. return -EIO;
  2298. if (boot_cpu_has(X86_FEATURE_NX))
  2299. kvm_enable_efer_bits(EFER_NX);
  2300. if (!cpu_has_vmx_vpid())
  2301. enable_vpid = 0;
  2302. if (!cpu_has_vmx_ept() ||
  2303. !cpu_has_vmx_ept_4levels()) {
  2304. enable_ept = 0;
  2305. enable_unrestricted_guest = 0;
  2306. }
  2307. if (!cpu_has_vmx_unrestricted_guest())
  2308. enable_unrestricted_guest = 0;
  2309. if (!cpu_has_vmx_flexpriority())
  2310. flexpriority_enabled = 0;
  2311. if (!cpu_has_vmx_tpr_shadow())
  2312. kvm_x86_ops->update_cr8_intercept = NULL;
  2313. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  2314. kvm_disable_largepages();
  2315. if (!cpu_has_vmx_ple())
  2316. ple_gap = 0;
  2317. if (nested)
  2318. nested_vmx_setup_ctls_msrs();
  2319. return alloc_kvm_area();
  2320. }
  2321. static __exit void hardware_unsetup(void)
  2322. {
  2323. free_kvm_area();
  2324. }
  2325. static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
  2326. {
  2327. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2328. if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
  2329. vmcs_write16(sf->selector, save->selector);
  2330. vmcs_writel(sf->base, save->base);
  2331. vmcs_write32(sf->limit, save->limit);
  2332. vmcs_write32(sf->ar_bytes, save->ar);
  2333. } else {
  2334. u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
  2335. << AR_DPL_SHIFT;
  2336. vmcs_write32(sf->ar_bytes, 0x93 | dpl);
  2337. }
  2338. }
  2339. static void enter_pmode(struct kvm_vcpu *vcpu)
  2340. {
  2341. unsigned long flags;
  2342. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2343. vmx->emulation_required = 1;
  2344. vmx->rmode.vm86_active = 0;
  2345. vmx_segment_cache_clear(vmx);
  2346. vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
  2347. vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
  2348. vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
  2349. vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
  2350. flags = vmcs_readl(GUEST_RFLAGS);
  2351. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2352. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2353. vmcs_writel(GUEST_RFLAGS, flags);
  2354. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  2355. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  2356. update_exception_bitmap(vcpu);
  2357. if (emulate_invalid_guest_state)
  2358. return;
  2359. fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
  2360. fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
  2361. fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
  2362. fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
  2363. vmx_segment_cache_clear(vmx);
  2364. vmcs_write16(GUEST_SS_SELECTOR, 0);
  2365. vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
  2366. vmcs_write16(GUEST_CS_SELECTOR,
  2367. vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
  2368. vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
  2369. }
  2370. static gva_t rmode_tss_base(struct kvm *kvm)
  2371. {
  2372. if (!kvm->arch.tss_addr) {
  2373. struct kvm_memslots *slots;
  2374. struct kvm_memory_slot *slot;
  2375. gfn_t base_gfn;
  2376. slots = kvm_memslots(kvm);
  2377. slot = id_to_memslot(slots, 0);
  2378. base_gfn = slot->base_gfn + slot->npages - 3;
  2379. return base_gfn << PAGE_SHIFT;
  2380. }
  2381. return kvm->arch.tss_addr;
  2382. }
  2383. static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
  2384. {
  2385. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2386. save->selector = vmcs_read16(sf->selector);
  2387. save->base = vmcs_readl(sf->base);
  2388. save->limit = vmcs_read32(sf->limit);
  2389. save->ar = vmcs_read32(sf->ar_bytes);
  2390. vmcs_write16(sf->selector, save->base >> 4);
  2391. vmcs_write32(sf->base, save->base & 0xffff0);
  2392. vmcs_write32(sf->limit, 0xffff);
  2393. vmcs_write32(sf->ar_bytes, 0xf3);
  2394. if (save->base & 0xf)
  2395. printk_once(KERN_WARNING "kvm: segment base is not paragraph"
  2396. " aligned when entering protected mode (seg=%d)",
  2397. seg);
  2398. }
  2399. static void enter_rmode(struct kvm_vcpu *vcpu)
  2400. {
  2401. unsigned long flags;
  2402. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2403. if (enable_unrestricted_guest)
  2404. return;
  2405. vmx->emulation_required = 1;
  2406. vmx->rmode.vm86_active = 1;
  2407. /*
  2408. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  2409. * vcpu. Call it here with phys address pointing 16M below 4G.
  2410. */
  2411. if (!vcpu->kvm->arch.tss_addr) {
  2412. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  2413. "called before entering vcpu\n");
  2414. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  2415. vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
  2416. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  2417. }
  2418. vmx_segment_cache_clear(vmx);
  2419. vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
  2420. vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
  2421. vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
  2422. vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
  2423. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  2424. vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
  2425. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2426. flags = vmcs_readl(GUEST_RFLAGS);
  2427. vmx->rmode.save_rflags = flags;
  2428. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2429. vmcs_writel(GUEST_RFLAGS, flags);
  2430. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  2431. update_exception_bitmap(vcpu);
  2432. if (emulate_invalid_guest_state)
  2433. goto continue_rmode;
  2434. vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
  2435. vmcs_write32(GUEST_SS_LIMIT, 0xffff);
  2436. vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
  2437. vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
  2438. vmcs_write32(GUEST_CS_LIMIT, 0xffff);
  2439. if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
  2440. vmcs_writel(GUEST_CS_BASE, 0xf0000);
  2441. vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
  2442. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
  2443. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
  2444. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
  2445. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
  2446. continue_rmode:
  2447. kvm_mmu_reset_context(vcpu);
  2448. }
  2449. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  2450. {
  2451. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2452. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  2453. if (!msr)
  2454. return;
  2455. /*
  2456. * Force kernel_gs_base reloading before EFER changes, as control
  2457. * of this msr depends on is_long_mode().
  2458. */
  2459. vmx_load_host_state(to_vmx(vcpu));
  2460. vcpu->arch.efer = efer;
  2461. if (efer & EFER_LMA) {
  2462. vmcs_write32(VM_ENTRY_CONTROLS,
  2463. vmcs_read32(VM_ENTRY_CONTROLS) |
  2464. VM_ENTRY_IA32E_MODE);
  2465. msr->data = efer;
  2466. } else {
  2467. vmcs_write32(VM_ENTRY_CONTROLS,
  2468. vmcs_read32(VM_ENTRY_CONTROLS) &
  2469. ~VM_ENTRY_IA32E_MODE);
  2470. msr->data = efer & ~EFER_LME;
  2471. }
  2472. setup_msrs(vmx);
  2473. }
  2474. #ifdef CONFIG_X86_64
  2475. static void enter_lmode(struct kvm_vcpu *vcpu)
  2476. {
  2477. u32 guest_tr_ar;
  2478. vmx_segment_cache_clear(to_vmx(vcpu));
  2479. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  2480. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  2481. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  2482. __func__);
  2483. vmcs_write32(GUEST_TR_AR_BYTES,
  2484. (guest_tr_ar & ~AR_TYPE_MASK)
  2485. | AR_TYPE_BUSY_64_TSS);
  2486. }
  2487. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  2488. }
  2489. static void exit_lmode(struct kvm_vcpu *vcpu)
  2490. {
  2491. vmcs_write32(VM_ENTRY_CONTROLS,
  2492. vmcs_read32(VM_ENTRY_CONTROLS)
  2493. & ~VM_ENTRY_IA32E_MODE);
  2494. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  2495. }
  2496. #endif
  2497. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  2498. {
  2499. vpid_sync_context(to_vmx(vcpu));
  2500. if (enable_ept) {
  2501. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2502. return;
  2503. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  2504. }
  2505. }
  2506. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  2507. {
  2508. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  2509. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  2510. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  2511. }
  2512. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  2513. {
  2514. if (enable_ept && is_paging(vcpu))
  2515. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  2516. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  2517. }
  2518. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  2519. {
  2520. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  2521. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  2522. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  2523. }
  2524. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  2525. {
  2526. if (!test_bit(VCPU_EXREG_PDPTR,
  2527. (unsigned long *)&vcpu->arch.regs_dirty))
  2528. return;
  2529. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  2530. vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
  2531. vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
  2532. vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
  2533. vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
  2534. }
  2535. }
  2536. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  2537. {
  2538. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  2539. vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  2540. vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  2541. vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  2542. vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  2543. }
  2544. __set_bit(VCPU_EXREG_PDPTR,
  2545. (unsigned long *)&vcpu->arch.regs_avail);
  2546. __set_bit(VCPU_EXREG_PDPTR,
  2547. (unsigned long *)&vcpu->arch.regs_dirty);
  2548. }
  2549. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  2550. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  2551. unsigned long cr0,
  2552. struct kvm_vcpu *vcpu)
  2553. {
  2554. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  2555. vmx_decache_cr3(vcpu);
  2556. if (!(cr0 & X86_CR0_PG)) {
  2557. /* From paging/starting to nonpaging */
  2558. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  2559. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  2560. (CPU_BASED_CR3_LOAD_EXITING |
  2561. CPU_BASED_CR3_STORE_EXITING));
  2562. vcpu->arch.cr0 = cr0;
  2563. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  2564. } else if (!is_paging(vcpu)) {
  2565. /* From nonpaging to paging */
  2566. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  2567. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  2568. ~(CPU_BASED_CR3_LOAD_EXITING |
  2569. CPU_BASED_CR3_STORE_EXITING));
  2570. vcpu->arch.cr0 = cr0;
  2571. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  2572. }
  2573. if (!(cr0 & X86_CR0_WP))
  2574. *hw_cr0 &= ~X86_CR0_WP;
  2575. }
  2576. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  2577. {
  2578. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2579. unsigned long hw_cr0;
  2580. if (enable_unrestricted_guest)
  2581. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
  2582. | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  2583. else
  2584. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
  2585. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  2586. enter_pmode(vcpu);
  2587. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  2588. enter_rmode(vcpu);
  2589. #ifdef CONFIG_X86_64
  2590. if (vcpu->arch.efer & EFER_LME) {
  2591. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  2592. enter_lmode(vcpu);
  2593. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  2594. exit_lmode(vcpu);
  2595. }
  2596. #endif
  2597. if (enable_ept)
  2598. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  2599. if (!vcpu->fpu_active)
  2600. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  2601. vmcs_writel(CR0_READ_SHADOW, cr0);
  2602. vmcs_writel(GUEST_CR0, hw_cr0);
  2603. vcpu->arch.cr0 = cr0;
  2604. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2605. }
  2606. static u64 construct_eptp(unsigned long root_hpa)
  2607. {
  2608. u64 eptp;
  2609. /* TODO write the value reading from MSR */
  2610. eptp = VMX_EPT_DEFAULT_MT |
  2611. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  2612. eptp |= (root_hpa & PAGE_MASK);
  2613. return eptp;
  2614. }
  2615. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  2616. {
  2617. unsigned long guest_cr3;
  2618. u64 eptp;
  2619. guest_cr3 = cr3;
  2620. if (enable_ept) {
  2621. eptp = construct_eptp(cr3);
  2622. vmcs_write64(EPT_POINTER, eptp);
  2623. guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
  2624. vcpu->kvm->arch.ept_identity_map_addr;
  2625. ept_load_pdptrs(vcpu);
  2626. }
  2627. vmx_flush_tlb(vcpu);
  2628. vmcs_writel(GUEST_CR3, guest_cr3);
  2629. }
  2630. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  2631. {
  2632. unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
  2633. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  2634. if (cr4 & X86_CR4_VMXE) {
  2635. /*
  2636. * To use VMXON (and later other VMX instructions), a guest
  2637. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  2638. * So basically the check on whether to allow nested VMX
  2639. * is here.
  2640. */
  2641. if (!nested_vmx_allowed(vcpu))
  2642. return 1;
  2643. } else if (to_vmx(vcpu)->nested.vmxon)
  2644. return 1;
  2645. vcpu->arch.cr4 = cr4;
  2646. if (enable_ept) {
  2647. if (!is_paging(vcpu)) {
  2648. hw_cr4 &= ~X86_CR4_PAE;
  2649. hw_cr4 |= X86_CR4_PSE;
  2650. } else if (!(cr4 & X86_CR4_PAE)) {
  2651. hw_cr4 &= ~X86_CR4_PAE;
  2652. }
  2653. }
  2654. vmcs_writel(CR4_READ_SHADOW, cr4);
  2655. vmcs_writel(GUEST_CR4, hw_cr4);
  2656. return 0;
  2657. }
  2658. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  2659. struct kvm_segment *var, int seg)
  2660. {
  2661. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2662. struct kvm_save_segment *save;
  2663. u32 ar;
  2664. if (vmx->rmode.vm86_active
  2665. && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
  2666. || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
  2667. || seg == VCPU_SREG_GS)
  2668. && !emulate_invalid_guest_state) {
  2669. switch (seg) {
  2670. case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
  2671. case VCPU_SREG_ES: save = &vmx->rmode.es; break;
  2672. case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
  2673. case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
  2674. case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
  2675. default: BUG();
  2676. }
  2677. var->selector = save->selector;
  2678. var->base = save->base;
  2679. var->limit = save->limit;
  2680. ar = save->ar;
  2681. if (seg == VCPU_SREG_TR
  2682. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  2683. goto use_saved_rmode_seg;
  2684. }
  2685. var->base = vmx_read_guest_seg_base(vmx, seg);
  2686. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  2687. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  2688. ar = vmx_read_guest_seg_ar(vmx, seg);
  2689. use_saved_rmode_seg:
  2690. if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
  2691. ar = 0;
  2692. var->type = ar & 15;
  2693. var->s = (ar >> 4) & 1;
  2694. var->dpl = (ar >> 5) & 3;
  2695. var->present = (ar >> 7) & 1;
  2696. var->avl = (ar >> 12) & 1;
  2697. var->l = (ar >> 13) & 1;
  2698. var->db = (ar >> 14) & 1;
  2699. var->g = (ar >> 15) & 1;
  2700. var->unusable = (ar >> 16) & 1;
  2701. }
  2702. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  2703. {
  2704. struct kvm_segment s;
  2705. if (to_vmx(vcpu)->rmode.vm86_active) {
  2706. vmx_get_segment(vcpu, &s, seg);
  2707. return s.base;
  2708. }
  2709. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  2710. }
  2711. static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
  2712. {
  2713. if (!is_protmode(vcpu))
  2714. return 0;
  2715. if (!is_long_mode(vcpu)
  2716. && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
  2717. return 3;
  2718. return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
  2719. }
  2720. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  2721. {
  2722. if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
  2723. __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2724. to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
  2725. }
  2726. return to_vmx(vcpu)->cpl;
  2727. }
  2728. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  2729. {
  2730. u32 ar;
  2731. if (var->unusable)
  2732. ar = 1 << 16;
  2733. else {
  2734. ar = var->type & 15;
  2735. ar |= (var->s & 1) << 4;
  2736. ar |= (var->dpl & 3) << 5;
  2737. ar |= (var->present & 1) << 7;
  2738. ar |= (var->avl & 1) << 12;
  2739. ar |= (var->l & 1) << 13;
  2740. ar |= (var->db & 1) << 14;
  2741. ar |= (var->g & 1) << 15;
  2742. }
  2743. if (ar == 0) /* a 0 value means unusable */
  2744. ar = AR_UNUSABLE_MASK;
  2745. return ar;
  2746. }
  2747. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  2748. struct kvm_segment *var, int seg)
  2749. {
  2750. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2751. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2752. u32 ar;
  2753. vmx_segment_cache_clear(vmx);
  2754. if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
  2755. vmcs_write16(sf->selector, var->selector);
  2756. vmx->rmode.tr.selector = var->selector;
  2757. vmx->rmode.tr.base = var->base;
  2758. vmx->rmode.tr.limit = var->limit;
  2759. vmx->rmode.tr.ar = vmx_segment_access_rights(var);
  2760. return;
  2761. }
  2762. vmcs_writel(sf->base, var->base);
  2763. vmcs_write32(sf->limit, var->limit);
  2764. vmcs_write16(sf->selector, var->selector);
  2765. if (vmx->rmode.vm86_active && var->s) {
  2766. /*
  2767. * Hack real-mode segments into vm86 compatibility.
  2768. */
  2769. if (var->base == 0xffff0000 && var->selector == 0xf000)
  2770. vmcs_writel(sf->base, 0xf0000);
  2771. ar = 0xf3;
  2772. } else
  2773. ar = vmx_segment_access_rights(var);
  2774. /*
  2775. * Fix the "Accessed" bit in AR field of segment registers for older
  2776. * qemu binaries.
  2777. * IA32 arch specifies that at the time of processor reset the
  2778. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  2779. * is setting it to 0 in the usedland code. This causes invalid guest
  2780. * state vmexit when "unrestricted guest" mode is turned on.
  2781. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  2782. * tree. Newer qemu binaries with that qemu fix would not need this
  2783. * kvm hack.
  2784. */
  2785. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  2786. ar |= 0x1; /* Accessed */
  2787. vmcs_write32(sf->ar_bytes, ar);
  2788. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2789. }
  2790. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2791. {
  2792. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  2793. *db = (ar >> 14) & 1;
  2794. *l = (ar >> 13) & 1;
  2795. }
  2796. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2797. {
  2798. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  2799. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  2800. }
  2801. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2802. {
  2803. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  2804. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  2805. }
  2806. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2807. {
  2808. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  2809. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  2810. }
  2811. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2812. {
  2813. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  2814. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  2815. }
  2816. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  2817. {
  2818. struct kvm_segment var;
  2819. u32 ar;
  2820. vmx_get_segment(vcpu, &var, seg);
  2821. ar = vmx_segment_access_rights(&var);
  2822. if (var.base != (var.selector << 4))
  2823. return false;
  2824. if (var.limit != 0xffff)
  2825. return false;
  2826. if (ar != 0xf3)
  2827. return false;
  2828. return true;
  2829. }
  2830. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  2831. {
  2832. struct kvm_segment cs;
  2833. unsigned int cs_rpl;
  2834. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2835. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  2836. if (cs.unusable)
  2837. return false;
  2838. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  2839. return false;
  2840. if (!cs.s)
  2841. return false;
  2842. if (cs.type & AR_TYPE_WRITEABLE_MASK) {
  2843. if (cs.dpl > cs_rpl)
  2844. return false;
  2845. } else {
  2846. if (cs.dpl != cs_rpl)
  2847. return false;
  2848. }
  2849. if (!cs.present)
  2850. return false;
  2851. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  2852. return true;
  2853. }
  2854. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  2855. {
  2856. struct kvm_segment ss;
  2857. unsigned int ss_rpl;
  2858. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2859. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  2860. if (ss.unusable)
  2861. return true;
  2862. if (ss.type != 3 && ss.type != 7)
  2863. return false;
  2864. if (!ss.s)
  2865. return false;
  2866. if (ss.dpl != ss_rpl) /* DPL != RPL */
  2867. return false;
  2868. if (!ss.present)
  2869. return false;
  2870. return true;
  2871. }
  2872. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  2873. {
  2874. struct kvm_segment var;
  2875. unsigned int rpl;
  2876. vmx_get_segment(vcpu, &var, seg);
  2877. rpl = var.selector & SELECTOR_RPL_MASK;
  2878. if (var.unusable)
  2879. return true;
  2880. if (!var.s)
  2881. return false;
  2882. if (!var.present)
  2883. return false;
  2884. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  2885. if (var.dpl < rpl) /* DPL < RPL */
  2886. return false;
  2887. }
  2888. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  2889. * rights flags
  2890. */
  2891. return true;
  2892. }
  2893. static bool tr_valid(struct kvm_vcpu *vcpu)
  2894. {
  2895. struct kvm_segment tr;
  2896. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  2897. if (tr.unusable)
  2898. return false;
  2899. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2900. return false;
  2901. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  2902. return false;
  2903. if (!tr.present)
  2904. return false;
  2905. return true;
  2906. }
  2907. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  2908. {
  2909. struct kvm_segment ldtr;
  2910. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  2911. if (ldtr.unusable)
  2912. return true;
  2913. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2914. return false;
  2915. if (ldtr.type != 2)
  2916. return false;
  2917. if (!ldtr.present)
  2918. return false;
  2919. return true;
  2920. }
  2921. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  2922. {
  2923. struct kvm_segment cs, ss;
  2924. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2925. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2926. return ((cs.selector & SELECTOR_RPL_MASK) ==
  2927. (ss.selector & SELECTOR_RPL_MASK));
  2928. }
  2929. /*
  2930. * Check if guest state is valid. Returns true if valid, false if
  2931. * not.
  2932. * We assume that registers are always usable
  2933. */
  2934. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  2935. {
  2936. /* real mode guest state checks */
  2937. if (!is_protmode(vcpu)) {
  2938. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  2939. return false;
  2940. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  2941. return false;
  2942. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  2943. return false;
  2944. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  2945. return false;
  2946. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  2947. return false;
  2948. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  2949. return false;
  2950. } else {
  2951. /* protected mode guest state checks */
  2952. if (!cs_ss_rpl_check(vcpu))
  2953. return false;
  2954. if (!code_segment_valid(vcpu))
  2955. return false;
  2956. if (!stack_segment_valid(vcpu))
  2957. return false;
  2958. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  2959. return false;
  2960. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  2961. return false;
  2962. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  2963. return false;
  2964. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  2965. return false;
  2966. if (!tr_valid(vcpu))
  2967. return false;
  2968. if (!ldtr_valid(vcpu))
  2969. return false;
  2970. }
  2971. /* TODO:
  2972. * - Add checks on RIP
  2973. * - Add checks on RFLAGS
  2974. */
  2975. return true;
  2976. }
  2977. static int init_rmode_tss(struct kvm *kvm)
  2978. {
  2979. gfn_t fn;
  2980. u16 data = 0;
  2981. int r, idx, ret = 0;
  2982. idx = srcu_read_lock(&kvm->srcu);
  2983. fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
  2984. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2985. if (r < 0)
  2986. goto out;
  2987. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  2988. r = kvm_write_guest_page(kvm, fn++, &data,
  2989. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  2990. if (r < 0)
  2991. goto out;
  2992. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  2993. if (r < 0)
  2994. goto out;
  2995. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2996. if (r < 0)
  2997. goto out;
  2998. data = ~0;
  2999. r = kvm_write_guest_page(kvm, fn, &data,
  3000. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  3001. sizeof(u8));
  3002. if (r < 0)
  3003. goto out;
  3004. ret = 1;
  3005. out:
  3006. srcu_read_unlock(&kvm->srcu, idx);
  3007. return ret;
  3008. }
  3009. static int init_rmode_identity_map(struct kvm *kvm)
  3010. {
  3011. int i, idx, r, ret;
  3012. pfn_t identity_map_pfn;
  3013. u32 tmp;
  3014. if (!enable_ept)
  3015. return 1;
  3016. if (unlikely(!kvm->arch.ept_identity_pagetable)) {
  3017. printk(KERN_ERR "EPT: identity-mapping pagetable "
  3018. "haven't been allocated!\n");
  3019. return 0;
  3020. }
  3021. if (likely(kvm->arch.ept_identity_pagetable_done))
  3022. return 1;
  3023. ret = 0;
  3024. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  3025. idx = srcu_read_lock(&kvm->srcu);
  3026. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  3027. if (r < 0)
  3028. goto out;
  3029. /* Set up identity-mapping pagetable for EPT in real mode */
  3030. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  3031. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  3032. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  3033. r = kvm_write_guest_page(kvm, identity_map_pfn,
  3034. &tmp, i * sizeof(tmp), sizeof(tmp));
  3035. if (r < 0)
  3036. goto out;
  3037. }
  3038. kvm->arch.ept_identity_pagetable_done = true;
  3039. ret = 1;
  3040. out:
  3041. srcu_read_unlock(&kvm->srcu, idx);
  3042. return ret;
  3043. }
  3044. static void seg_setup(int seg)
  3045. {
  3046. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3047. unsigned int ar;
  3048. vmcs_write16(sf->selector, 0);
  3049. vmcs_writel(sf->base, 0);
  3050. vmcs_write32(sf->limit, 0xffff);
  3051. if (enable_unrestricted_guest) {
  3052. ar = 0x93;
  3053. if (seg == VCPU_SREG_CS)
  3054. ar |= 0x08; /* code segment */
  3055. } else
  3056. ar = 0xf3;
  3057. vmcs_write32(sf->ar_bytes, ar);
  3058. }
  3059. static int alloc_apic_access_page(struct kvm *kvm)
  3060. {
  3061. struct kvm_userspace_memory_region kvm_userspace_mem;
  3062. int r = 0;
  3063. mutex_lock(&kvm->slots_lock);
  3064. if (kvm->arch.apic_access_page)
  3065. goto out;
  3066. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  3067. kvm_userspace_mem.flags = 0;
  3068. kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
  3069. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3070. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  3071. if (r)
  3072. goto out;
  3073. kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
  3074. out:
  3075. mutex_unlock(&kvm->slots_lock);
  3076. return r;
  3077. }
  3078. static int alloc_identity_pagetable(struct kvm *kvm)
  3079. {
  3080. struct kvm_userspace_memory_region kvm_userspace_mem;
  3081. int r = 0;
  3082. mutex_lock(&kvm->slots_lock);
  3083. if (kvm->arch.ept_identity_pagetable)
  3084. goto out;
  3085. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  3086. kvm_userspace_mem.flags = 0;
  3087. kvm_userspace_mem.guest_phys_addr =
  3088. kvm->arch.ept_identity_map_addr;
  3089. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3090. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  3091. if (r)
  3092. goto out;
  3093. kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
  3094. kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
  3095. out:
  3096. mutex_unlock(&kvm->slots_lock);
  3097. return r;
  3098. }
  3099. static void allocate_vpid(struct vcpu_vmx *vmx)
  3100. {
  3101. int vpid;
  3102. vmx->vpid = 0;
  3103. if (!enable_vpid)
  3104. return;
  3105. spin_lock(&vmx_vpid_lock);
  3106. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  3107. if (vpid < VMX_NR_VPIDS) {
  3108. vmx->vpid = vpid;
  3109. __set_bit(vpid, vmx_vpid_bitmap);
  3110. }
  3111. spin_unlock(&vmx_vpid_lock);
  3112. }
  3113. static void free_vpid(struct vcpu_vmx *vmx)
  3114. {
  3115. if (!enable_vpid)
  3116. return;
  3117. spin_lock(&vmx_vpid_lock);
  3118. if (vmx->vpid != 0)
  3119. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  3120. spin_unlock(&vmx_vpid_lock);
  3121. }
  3122. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
  3123. {
  3124. int f = sizeof(unsigned long);
  3125. if (!cpu_has_vmx_msr_bitmap())
  3126. return;
  3127. /*
  3128. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3129. * have the write-low and read-high bitmap offsets the wrong way round.
  3130. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3131. */
  3132. if (msr <= 0x1fff) {
  3133. __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
  3134. __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
  3135. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3136. msr &= 0x1fff;
  3137. __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
  3138. __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
  3139. }
  3140. }
  3141. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  3142. {
  3143. if (!longmode_only)
  3144. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
  3145. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
  3146. }
  3147. /*
  3148. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  3149. * will not change in the lifetime of the guest.
  3150. * Note that host-state that does change is set elsewhere. E.g., host-state
  3151. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  3152. */
  3153. static void vmx_set_constant_host_state(void)
  3154. {
  3155. u32 low32, high32;
  3156. unsigned long tmpl;
  3157. struct desc_ptr dt;
  3158. vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
  3159. vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
  3160. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  3161. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  3162. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3163. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3164. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3165. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  3166. native_store_idt(&dt);
  3167. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  3168. asm("mov $.Lkvm_vmx_return, %0" : "=r"(tmpl));
  3169. vmcs_writel(HOST_RIP, tmpl); /* 22.2.5 */
  3170. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  3171. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  3172. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  3173. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  3174. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  3175. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  3176. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  3177. }
  3178. }
  3179. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  3180. {
  3181. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  3182. if (enable_ept)
  3183. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  3184. if (is_guest_mode(&vmx->vcpu))
  3185. vmx->vcpu.arch.cr4_guest_owned_bits &=
  3186. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  3187. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  3188. }
  3189. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  3190. {
  3191. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  3192. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  3193. exec_control &= ~CPU_BASED_TPR_SHADOW;
  3194. #ifdef CONFIG_X86_64
  3195. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  3196. CPU_BASED_CR8_LOAD_EXITING;
  3197. #endif
  3198. }
  3199. if (!enable_ept)
  3200. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  3201. CPU_BASED_CR3_LOAD_EXITING |
  3202. CPU_BASED_INVLPG_EXITING;
  3203. return exec_control;
  3204. }
  3205. static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
  3206. {
  3207. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  3208. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  3209. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3210. if (vmx->vpid == 0)
  3211. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  3212. if (!enable_ept) {
  3213. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  3214. enable_unrestricted_guest = 0;
  3215. }
  3216. if (!enable_unrestricted_guest)
  3217. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3218. if (!ple_gap)
  3219. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  3220. return exec_control;
  3221. }
  3222. static void ept_set_mmio_spte_mask(void)
  3223. {
  3224. /*
  3225. * EPT Misconfigurations can be generated if the value of bits 2:0
  3226. * of an EPT paging-structure entry is 110b (write/execute).
  3227. * Also, magic bits (0xffull << 49) is set to quickly identify mmio
  3228. * spte.
  3229. */
  3230. kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull);
  3231. }
  3232. /*
  3233. * Sets up the vmcs for emulated real mode.
  3234. */
  3235. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  3236. {
  3237. #ifdef CONFIG_X86_64
  3238. unsigned long a;
  3239. #endif
  3240. int i;
  3241. /* I/O */
  3242. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  3243. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  3244. if (cpu_has_vmx_msr_bitmap())
  3245. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  3246. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  3247. /* Control */
  3248. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  3249. vmcs_config.pin_based_exec_ctrl);
  3250. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  3251. if (cpu_has_secondary_exec_ctrls()) {
  3252. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  3253. vmx_secondary_exec_control(vmx));
  3254. }
  3255. if (ple_gap) {
  3256. vmcs_write32(PLE_GAP, ple_gap);
  3257. vmcs_write32(PLE_WINDOW, ple_window);
  3258. }
  3259. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  3260. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  3261. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  3262. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  3263. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  3264. vmx_set_constant_host_state();
  3265. #ifdef CONFIG_X86_64
  3266. rdmsrl(MSR_FS_BASE, a);
  3267. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  3268. rdmsrl(MSR_GS_BASE, a);
  3269. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  3270. #else
  3271. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  3272. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  3273. #endif
  3274. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  3275. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  3276. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  3277. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  3278. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  3279. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3280. u32 msr_low, msr_high;
  3281. u64 host_pat;
  3282. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  3283. host_pat = msr_low | ((u64) msr_high << 32);
  3284. /* Write the default value follow host pat */
  3285. vmcs_write64(GUEST_IA32_PAT, host_pat);
  3286. /* Keep arch.pat sync with GUEST_IA32_PAT */
  3287. vmx->vcpu.arch.pat = host_pat;
  3288. }
  3289. for (i = 0; i < NR_VMX_MSR; ++i) {
  3290. u32 index = vmx_msr_index[i];
  3291. u32 data_low, data_high;
  3292. int j = vmx->nmsrs;
  3293. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  3294. continue;
  3295. if (wrmsr_safe(index, data_low, data_high) < 0)
  3296. continue;
  3297. vmx->guest_msrs[j].index = i;
  3298. vmx->guest_msrs[j].data = 0;
  3299. vmx->guest_msrs[j].mask = -1ull;
  3300. ++vmx->nmsrs;
  3301. }
  3302. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  3303. /* 22.2.1, 20.8.1 */
  3304. vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
  3305. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  3306. set_cr4_guest_host_mask(vmx);
  3307. kvm_write_tsc(&vmx->vcpu, 0);
  3308. return 0;
  3309. }
  3310. static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  3311. {
  3312. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3313. u64 msr;
  3314. int ret;
  3315. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  3316. vmx->rmode.vm86_active = 0;
  3317. vmx->soft_vnmi_blocked = 0;
  3318. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  3319. kvm_set_cr8(&vmx->vcpu, 0);
  3320. msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  3321. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  3322. msr |= MSR_IA32_APICBASE_BSP;
  3323. kvm_set_apic_base(&vmx->vcpu, msr);
  3324. ret = fx_init(&vmx->vcpu);
  3325. if (ret != 0)
  3326. goto out;
  3327. vmx_segment_cache_clear(vmx);
  3328. seg_setup(VCPU_SREG_CS);
  3329. /*
  3330. * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
  3331. * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
  3332. */
  3333. if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
  3334. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  3335. vmcs_writel(GUEST_CS_BASE, 0x000f0000);
  3336. } else {
  3337. vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
  3338. vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
  3339. }
  3340. seg_setup(VCPU_SREG_DS);
  3341. seg_setup(VCPU_SREG_ES);
  3342. seg_setup(VCPU_SREG_FS);
  3343. seg_setup(VCPU_SREG_GS);
  3344. seg_setup(VCPU_SREG_SS);
  3345. vmcs_write16(GUEST_TR_SELECTOR, 0);
  3346. vmcs_writel(GUEST_TR_BASE, 0);
  3347. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  3348. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  3349. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  3350. vmcs_writel(GUEST_LDTR_BASE, 0);
  3351. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  3352. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  3353. vmcs_write32(GUEST_SYSENTER_CS, 0);
  3354. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  3355. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  3356. vmcs_writel(GUEST_RFLAGS, 0x02);
  3357. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  3358. kvm_rip_write(vcpu, 0xfff0);
  3359. else
  3360. kvm_rip_write(vcpu, 0);
  3361. kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
  3362. vmcs_writel(GUEST_DR7, 0x400);
  3363. vmcs_writel(GUEST_GDTR_BASE, 0);
  3364. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  3365. vmcs_writel(GUEST_IDTR_BASE, 0);
  3366. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  3367. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  3368. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  3369. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  3370. /* Special registers */
  3371. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  3372. setup_msrs(vmx);
  3373. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  3374. if (cpu_has_vmx_tpr_shadow()) {
  3375. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  3376. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  3377. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  3378. __pa(vmx->vcpu.arch.apic->regs));
  3379. vmcs_write32(TPR_THRESHOLD, 0);
  3380. }
  3381. if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  3382. vmcs_write64(APIC_ACCESS_ADDR,
  3383. page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
  3384. if (vmx->vpid != 0)
  3385. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  3386. vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  3387. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  3388. vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
  3389. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  3390. vmx_set_cr4(&vmx->vcpu, 0);
  3391. vmx_set_efer(&vmx->vcpu, 0);
  3392. vmx_fpu_activate(&vmx->vcpu);
  3393. update_exception_bitmap(&vmx->vcpu);
  3394. vpid_sync_context(vmx);
  3395. ret = 0;
  3396. /* HACK: Don't enable emulation on guest boot/reset */
  3397. vmx->emulation_required = 0;
  3398. out:
  3399. return ret;
  3400. }
  3401. /*
  3402. * In nested virtualization, check if L1 asked to exit on external interrupts.
  3403. * For most existing hypervisors, this will always return true.
  3404. */
  3405. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  3406. {
  3407. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  3408. PIN_BASED_EXT_INTR_MASK;
  3409. }
  3410. static void enable_irq_window(struct kvm_vcpu *vcpu)
  3411. {
  3412. u32 cpu_based_vm_exec_control;
  3413. if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
  3414. /*
  3415. * We get here if vmx_interrupt_allowed() said we can't
  3416. * inject to L1 now because L2 must run. Ask L2 to exit
  3417. * right after entry, so we can inject to L1 more promptly.
  3418. */
  3419. kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
  3420. return;
  3421. }
  3422. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3423. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  3424. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3425. }
  3426. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  3427. {
  3428. u32 cpu_based_vm_exec_control;
  3429. if (!cpu_has_virtual_nmis()) {
  3430. enable_irq_window(vcpu);
  3431. return;
  3432. }
  3433. if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  3434. enable_irq_window(vcpu);
  3435. return;
  3436. }
  3437. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3438. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  3439. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3440. }
  3441. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  3442. {
  3443. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3444. uint32_t intr;
  3445. int irq = vcpu->arch.interrupt.nr;
  3446. trace_kvm_inj_virq(irq);
  3447. ++vcpu->stat.irq_injections;
  3448. if (vmx->rmode.vm86_active) {
  3449. int inc_eip = 0;
  3450. if (vcpu->arch.interrupt.soft)
  3451. inc_eip = vcpu->arch.event_exit_inst_len;
  3452. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  3453. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  3454. return;
  3455. }
  3456. intr = irq | INTR_INFO_VALID_MASK;
  3457. if (vcpu->arch.interrupt.soft) {
  3458. intr |= INTR_TYPE_SOFT_INTR;
  3459. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  3460. vmx->vcpu.arch.event_exit_inst_len);
  3461. } else
  3462. intr |= INTR_TYPE_EXT_INTR;
  3463. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  3464. }
  3465. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  3466. {
  3467. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3468. if (is_guest_mode(vcpu))
  3469. return;
  3470. if (!cpu_has_virtual_nmis()) {
  3471. /*
  3472. * Tracking the NMI-blocked state in software is built upon
  3473. * finding the next open IRQ window. This, in turn, depends on
  3474. * well-behaving guests: They have to keep IRQs disabled at
  3475. * least as long as the NMI handler runs. Otherwise we may
  3476. * cause NMI nesting, maybe breaking the guest. But as this is
  3477. * highly unlikely, we can live with the residual risk.
  3478. */
  3479. vmx->soft_vnmi_blocked = 1;
  3480. vmx->vnmi_blocked_time = 0;
  3481. }
  3482. ++vcpu->stat.nmi_injections;
  3483. vmx->nmi_known_unmasked = false;
  3484. if (vmx->rmode.vm86_active) {
  3485. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  3486. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  3487. return;
  3488. }
  3489. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  3490. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  3491. }
  3492. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  3493. {
  3494. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  3495. return 0;
  3496. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  3497. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  3498. | GUEST_INTR_STATE_NMI));
  3499. }
  3500. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  3501. {
  3502. if (!cpu_has_virtual_nmis())
  3503. return to_vmx(vcpu)->soft_vnmi_blocked;
  3504. if (to_vmx(vcpu)->nmi_known_unmasked)
  3505. return false;
  3506. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  3507. }
  3508. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  3509. {
  3510. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3511. if (!cpu_has_virtual_nmis()) {
  3512. if (vmx->soft_vnmi_blocked != masked) {
  3513. vmx->soft_vnmi_blocked = masked;
  3514. vmx->vnmi_blocked_time = 0;
  3515. }
  3516. } else {
  3517. vmx->nmi_known_unmasked = !masked;
  3518. if (masked)
  3519. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  3520. GUEST_INTR_STATE_NMI);
  3521. else
  3522. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  3523. GUEST_INTR_STATE_NMI);
  3524. }
  3525. }
  3526. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  3527. {
  3528. if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
  3529. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  3530. if (to_vmx(vcpu)->nested.nested_run_pending ||
  3531. (vmcs12->idt_vectoring_info_field &
  3532. VECTORING_INFO_VALID_MASK))
  3533. return 0;
  3534. nested_vmx_vmexit(vcpu);
  3535. vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT;
  3536. vmcs12->vm_exit_intr_info = 0;
  3537. /* fall through to normal code, but now in L1, not L2 */
  3538. }
  3539. return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  3540. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  3541. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  3542. }
  3543. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  3544. {
  3545. int ret;
  3546. struct kvm_userspace_memory_region tss_mem = {
  3547. .slot = TSS_PRIVATE_MEMSLOT,
  3548. .guest_phys_addr = addr,
  3549. .memory_size = PAGE_SIZE * 3,
  3550. .flags = 0,
  3551. };
  3552. ret = kvm_set_memory_region(kvm, &tss_mem, 0);
  3553. if (ret)
  3554. return ret;
  3555. kvm->arch.tss_addr = addr;
  3556. if (!init_rmode_tss(kvm))
  3557. return -ENOMEM;
  3558. return 0;
  3559. }
  3560. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  3561. int vec, u32 err_code)
  3562. {
  3563. /*
  3564. * Instruction with address size override prefix opcode 0x67
  3565. * Cause the #SS fault with 0 error code in VM86 mode.
  3566. */
  3567. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
  3568. if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
  3569. return 1;
  3570. /*
  3571. * Forward all other exceptions that are valid in real mode.
  3572. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  3573. * the required debugging infrastructure rework.
  3574. */
  3575. switch (vec) {
  3576. case DB_VECTOR:
  3577. if (vcpu->guest_debug &
  3578. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  3579. return 0;
  3580. kvm_queue_exception(vcpu, vec);
  3581. return 1;
  3582. case BP_VECTOR:
  3583. /*
  3584. * Update instruction length as we may reinject the exception
  3585. * from user space while in guest debugging mode.
  3586. */
  3587. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  3588. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  3589. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  3590. return 0;
  3591. /* fall through */
  3592. case DE_VECTOR:
  3593. case OF_VECTOR:
  3594. case BR_VECTOR:
  3595. case UD_VECTOR:
  3596. case DF_VECTOR:
  3597. case SS_VECTOR:
  3598. case GP_VECTOR:
  3599. case MF_VECTOR:
  3600. kvm_queue_exception(vcpu, vec);
  3601. return 1;
  3602. }
  3603. return 0;
  3604. }
  3605. /*
  3606. * Trigger machine check on the host. We assume all the MSRs are already set up
  3607. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  3608. * We pass a fake environment to the machine check handler because we want
  3609. * the guest to be always treated like user space, no matter what context
  3610. * it used internally.
  3611. */
  3612. static void kvm_machine_check(void)
  3613. {
  3614. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  3615. struct pt_regs regs = {
  3616. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  3617. .flags = X86_EFLAGS_IF,
  3618. };
  3619. do_machine_check(&regs, 0);
  3620. #endif
  3621. }
  3622. static int handle_machine_check(struct kvm_vcpu *vcpu)
  3623. {
  3624. /* already handled by vcpu_run */
  3625. return 1;
  3626. }
  3627. static int handle_exception(struct kvm_vcpu *vcpu)
  3628. {
  3629. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3630. struct kvm_run *kvm_run = vcpu->run;
  3631. u32 intr_info, ex_no, error_code;
  3632. unsigned long cr2, rip, dr6;
  3633. u32 vect_info;
  3634. enum emulation_result er;
  3635. vect_info = vmx->idt_vectoring_info;
  3636. intr_info = vmx->exit_intr_info;
  3637. if (is_machine_check(intr_info))
  3638. return handle_machine_check(vcpu);
  3639. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  3640. !is_page_fault(intr_info)) {
  3641. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3642. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  3643. vcpu->run->internal.ndata = 2;
  3644. vcpu->run->internal.data[0] = vect_info;
  3645. vcpu->run->internal.data[1] = intr_info;
  3646. return 0;
  3647. }
  3648. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  3649. return 1; /* already handled by vmx_vcpu_run() */
  3650. if (is_no_device(intr_info)) {
  3651. vmx_fpu_activate(vcpu);
  3652. return 1;
  3653. }
  3654. if (is_invalid_opcode(intr_info)) {
  3655. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  3656. if (er != EMULATE_DONE)
  3657. kvm_queue_exception(vcpu, UD_VECTOR);
  3658. return 1;
  3659. }
  3660. error_code = 0;
  3661. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  3662. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  3663. if (is_page_fault(intr_info)) {
  3664. /* EPT won't cause page fault directly */
  3665. BUG_ON(enable_ept);
  3666. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  3667. trace_kvm_page_fault(cr2, error_code);
  3668. if (kvm_event_needs_reinjection(vcpu))
  3669. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  3670. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  3671. }
  3672. if (vmx->rmode.vm86_active &&
  3673. handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
  3674. error_code)) {
  3675. if (vcpu->arch.halt_request) {
  3676. vcpu->arch.halt_request = 0;
  3677. return kvm_emulate_halt(vcpu);
  3678. }
  3679. return 1;
  3680. }
  3681. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  3682. switch (ex_no) {
  3683. case DB_VECTOR:
  3684. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  3685. if (!(vcpu->guest_debug &
  3686. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  3687. vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
  3688. kvm_queue_exception(vcpu, DB_VECTOR);
  3689. return 1;
  3690. }
  3691. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  3692. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  3693. /* fall through */
  3694. case BP_VECTOR:
  3695. /*
  3696. * Update instruction length as we may reinject #BP from
  3697. * user space while in guest debugging mode. Reading it for
  3698. * #DB as well causes no harm, it is not used in that case.
  3699. */
  3700. vmx->vcpu.arch.event_exit_inst_len =
  3701. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  3702. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  3703. rip = kvm_rip_read(vcpu);
  3704. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  3705. kvm_run->debug.arch.exception = ex_no;
  3706. break;
  3707. default:
  3708. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  3709. kvm_run->ex.exception = ex_no;
  3710. kvm_run->ex.error_code = error_code;
  3711. break;
  3712. }
  3713. return 0;
  3714. }
  3715. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  3716. {
  3717. ++vcpu->stat.irq_exits;
  3718. return 1;
  3719. }
  3720. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  3721. {
  3722. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  3723. return 0;
  3724. }
  3725. static int handle_io(struct kvm_vcpu *vcpu)
  3726. {
  3727. unsigned long exit_qualification;
  3728. int size, in, string;
  3729. unsigned port;
  3730. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3731. string = (exit_qualification & 16) != 0;
  3732. in = (exit_qualification & 8) != 0;
  3733. ++vcpu->stat.io_exits;
  3734. if (string || in)
  3735. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3736. port = exit_qualification >> 16;
  3737. size = (exit_qualification & 7) + 1;
  3738. skip_emulated_instruction(vcpu);
  3739. return kvm_fast_pio_out(vcpu, size, port);
  3740. }
  3741. static void
  3742. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  3743. {
  3744. /*
  3745. * Patch in the VMCALL instruction:
  3746. */
  3747. hypercall[0] = 0x0f;
  3748. hypercall[1] = 0x01;
  3749. hypercall[2] = 0xc1;
  3750. }
  3751. /* called to set cr0 as approriate for a mov-to-cr0 exit. */
  3752. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  3753. {
  3754. if (to_vmx(vcpu)->nested.vmxon &&
  3755. ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
  3756. return 1;
  3757. if (is_guest_mode(vcpu)) {
  3758. /*
  3759. * We get here when L2 changed cr0 in a way that did not change
  3760. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  3761. * but did change L0 shadowed bits. This can currently happen
  3762. * with the TS bit: L0 may want to leave TS on (for lazy fpu
  3763. * loading) while pretending to allow the guest to change it.
  3764. */
  3765. if (kvm_set_cr0(vcpu, (val & vcpu->arch.cr0_guest_owned_bits) |
  3766. (vcpu->arch.cr0 & ~vcpu->arch.cr0_guest_owned_bits)))
  3767. return 1;
  3768. vmcs_writel(CR0_READ_SHADOW, val);
  3769. return 0;
  3770. } else
  3771. return kvm_set_cr0(vcpu, val);
  3772. }
  3773. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  3774. {
  3775. if (is_guest_mode(vcpu)) {
  3776. if (kvm_set_cr4(vcpu, (val & vcpu->arch.cr4_guest_owned_bits) |
  3777. (vcpu->arch.cr4 & ~vcpu->arch.cr4_guest_owned_bits)))
  3778. return 1;
  3779. vmcs_writel(CR4_READ_SHADOW, val);
  3780. return 0;
  3781. } else
  3782. return kvm_set_cr4(vcpu, val);
  3783. }
  3784. /* called to set cr0 as approriate for clts instruction exit. */
  3785. static void handle_clts(struct kvm_vcpu *vcpu)
  3786. {
  3787. if (is_guest_mode(vcpu)) {
  3788. /*
  3789. * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
  3790. * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
  3791. * just pretend it's off (also in arch.cr0 for fpu_activate).
  3792. */
  3793. vmcs_writel(CR0_READ_SHADOW,
  3794. vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
  3795. vcpu->arch.cr0 &= ~X86_CR0_TS;
  3796. } else
  3797. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  3798. }
  3799. static int handle_cr(struct kvm_vcpu *vcpu)
  3800. {
  3801. unsigned long exit_qualification, val;
  3802. int cr;
  3803. int reg;
  3804. int err;
  3805. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3806. cr = exit_qualification & 15;
  3807. reg = (exit_qualification >> 8) & 15;
  3808. switch ((exit_qualification >> 4) & 3) {
  3809. case 0: /* mov to cr */
  3810. val = kvm_register_read(vcpu, reg);
  3811. trace_kvm_cr_write(cr, val);
  3812. switch (cr) {
  3813. case 0:
  3814. err = handle_set_cr0(vcpu, val);
  3815. kvm_complete_insn_gp(vcpu, err);
  3816. return 1;
  3817. case 3:
  3818. err = kvm_set_cr3(vcpu, val);
  3819. kvm_complete_insn_gp(vcpu, err);
  3820. return 1;
  3821. case 4:
  3822. err = handle_set_cr4(vcpu, val);
  3823. kvm_complete_insn_gp(vcpu, err);
  3824. return 1;
  3825. case 8: {
  3826. u8 cr8_prev = kvm_get_cr8(vcpu);
  3827. u8 cr8 = kvm_register_read(vcpu, reg);
  3828. err = kvm_set_cr8(vcpu, cr8);
  3829. kvm_complete_insn_gp(vcpu, err);
  3830. if (irqchip_in_kernel(vcpu->kvm))
  3831. return 1;
  3832. if (cr8_prev <= cr8)
  3833. return 1;
  3834. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  3835. return 0;
  3836. }
  3837. };
  3838. break;
  3839. case 2: /* clts */
  3840. handle_clts(vcpu);
  3841. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  3842. skip_emulated_instruction(vcpu);
  3843. vmx_fpu_activate(vcpu);
  3844. return 1;
  3845. case 1: /*mov from cr*/
  3846. switch (cr) {
  3847. case 3:
  3848. val = kvm_read_cr3(vcpu);
  3849. kvm_register_write(vcpu, reg, val);
  3850. trace_kvm_cr_read(cr, val);
  3851. skip_emulated_instruction(vcpu);
  3852. return 1;
  3853. case 8:
  3854. val = kvm_get_cr8(vcpu);
  3855. kvm_register_write(vcpu, reg, val);
  3856. trace_kvm_cr_read(cr, val);
  3857. skip_emulated_instruction(vcpu);
  3858. return 1;
  3859. }
  3860. break;
  3861. case 3: /* lmsw */
  3862. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  3863. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  3864. kvm_lmsw(vcpu, val);
  3865. skip_emulated_instruction(vcpu);
  3866. return 1;
  3867. default:
  3868. break;
  3869. }
  3870. vcpu->run->exit_reason = 0;
  3871. pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  3872. (int)(exit_qualification >> 4) & 3, cr);
  3873. return 0;
  3874. }
  3875. static int handle_dr(struct kvm_vcpu *vcpu)
  3876. {
  3877. unsigned long exit_qualification;
  3878. int dr, reg;
  3879. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  3880. if (!kvm_require_cpl(vcpu, 0))
  3881. return 1;
  3882. dr = vmcs_readl(GUEST_DR7);
  3883. if (dr & DR7_GD) {
  3884. /*
  3885. * As the vm-exit takes precedence over the debug trap, we
  3886. * need to emulate the latter, either for the host or the
  3887. * guest debugging itself.
  3888. */
  3889. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  3890. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  3891. vcpu->run->debug.arch.dr7 = dr;
  3892. vcpu->run->debug.arch.pc =
  3893. vmcs_readl(GUEST_CS_BASE) +
  3894. vmcs_readl(GUEST_RIP);
  3895. vcpu->run->debug.arch.exception = DB_VECTOR;
  3896. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  3897. return 0;
  3898. } else {
  3899. vcpu->arch.dr7 &= ~DR7_GD;
  3900. vcpu->arch.dr6 |= DR6_BD;
  3901. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  3902. kvm_queue_exception(vcpu, DB_VECTOR);
  3903. return 1;
  3904. }
  3905. }
  3906. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3907. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  3908. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  3909. if (exit_qualification & TYPE_MOV_FROM_DR) {
  3910. unsigned long val;
  3911. if (!kvm_get_dr(vcpu, dr, &val))
  3912. kvm_register_write(vcpu, reg, val);
  3913. } else
  3914. kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
  3915. skip_emulated_instruction(vcpu);
  3916. return 1;
  3917. }
  3918. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  3919. {
  3920. vmcs_writel(GUEST_DR7, val);
  3921. }
  3922. static int handle_cpuid(struct kvm_vcpu *vcpu)
  3923. {
  3924. kvm_emulate_cpuid(vcpu);
  3925. return 1;
  3926. }
  3927. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  3928. {
  3929. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  3930. u64 data;
  3931. if (vmx_get_msr(vcpu, ecx, &data)) {
  3932. trace_kvm_msr_read_ex(ecx);
  3933. kvm_inject_gp(vcpu, 0);
  3934. return 1;
  3935. }
  3936. trace_kvm_msr_read(ecx, data);
  3937. /* FIXME: handling of bits 32:63 of rax, rdx */
  3938. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  3939. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  3940. skip_emulated_instruction(vcpu);
  3941. return 1;
  3942. }
  3943. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  3944. {
  3945. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  3946. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  3947. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  3948. if (vmx_set_msr(vcpu, ecx, data) != 0) {
  3949. trace_kvm_msr_write_ex(ecx, data);
  3950. kvm_inject_gp(vcpu, 0);
  3951. return 1;
  3952. }
  3953. trace_kvm_msr_write(ecx, data);
  3954. skip_emulated_instruction(vcpu);
  3955. return 1;
  3956. }
  3957. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  3958. {
  3959. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3960. return 1;
  3961. }
  3962. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  3963. {
  3964. u32 cpu_based_vm_exec_control;
  3965. /* clear pending irq */
  3966. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3967. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  3968. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3969. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3970. ++vcpu->stat.irq_window_exits;
  3971. /*
  3972. * If the user space waits to inject interrupts, exit as soon as
  3973. * possible
  3974. */
  3975. if (!irqchip_in_kernel(vcpu->kvm) &&
  3976. vcpu->run->request_interrupt_window &&
  3977. !kvm_cpu_has_interrupt(vcpu)) {
  3978. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  3979. return 0;
  3980. }
  3981. return 1;
  3982. }
  3983. static int handle_halt(struct kvm_vcpu *vcpu)
  3984. {
  3985. skip_emulated_instruction(vcpu);
  3986. return kvm_emulate_halt(vcpu);
  3987. }
  3988. static int handle_vmcall(struct kvm_vcpu *vcpu)
  3989. {
  3990. skip_emulated_instruction(vcpu);
  3991. kvm_emulate_hypercall(vcpu);
  3992. return 1;
  3993. }
  3994. static int handle_invd(struct kvm_vcpu *vcpu)
  3995. {
  3996. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3997. }
  3998. static int handle_invlpg(struct kvm_vcpu *vcpu)
  3999. {
  4000. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4001. kvm_mmu_invlpg(vcpu, exit_qualification);
  4002. skip_emulated_instruction(vcpu);
  4003. return 1;
  4004. }
  4005. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  4006. {
  4007. int err;
  4008. err = kvm_rdpmc(vcpu);
  4009. kvm_complete_insn_gp(vcpu, err);
  4010. return 1;
  4011. }
  4012. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  4013. {
  4014. skip_emulated_instruction(vcpu);
  4015. kvm_emulate_wbinvd(vcpu);
  4016. return 1;
  4017. }
  4018. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  4019. {
  4020. u64 new_bv = kvm_read_edx_eax(vcpu);
  4021. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4022. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  4023. skip_emulated_instruction(vcpu);
  4024. return 1;
  4025. }
  4026. static int handle_apic_access(struct kvm_vcpu *vcpu)
  4027. {
  4028. if (likely(fasteoi)) {
  4029. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4030. int access_type, offset;
  4031. access_type = exit_qualification & APIC_ACCESS_TYPE;
  4032. offset = exit_qualification & APIC_ACCESS_OFFSET;
  4033. /*
  4034. * Sane guest uses MOV to write EOI, with written value
  4035. * not cared. So make a short-circuit here by avoiding
  4036. * heavy instruction emulation.
  4037. */
  4038. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  4039. (offset == APIC_EOI)) {
  4040. kvm_lapic_set_eoi(vcpu);
  4041. skip_emulated_instruction(vcpu);
  4042. return 1;
  4043. }
  4044. }
  4045. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4046. }
  4047. static int handle_task_switch(struct kvm_vcpu *vcpu)
  4048. {
  4049. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4050. unsigned long exit_qualification;
  4051. bool has_error_code = false;
  4052. u32 error_code = 0;
  4053. u16 tss_selector;
  4054. int reason, type, idt_v, idt_index;
  4055. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  4056. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  4057. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  4058. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4059. reason = (u32)exit_qualification >> 30;
  4060. if (reason == TASK_SWITCH_GATE && idt_v) {
  4061. switch (type) {
  4062. case INTR_TYPE_NMI_INTR:
  4063. vcpu->arch.nmi_injected = false;
  4064. vmx_set_nmi_mask(vcpu, true);
  4065. break;
  4066. case INTR_TYPE_EXT_INTR:
  4067. case INTR_TYPE_SOFT_INTR:
  4068. kvm_clear_interrupt_queue(vcpu);
  4069. break;
  4070. case INTR_TYPE_HARD_EXCEPTION:
  4071. if (vmx->idt_vectoring_info &
  4072. VECTORING_INFO_DELIVER_CODE_MASK) {
  4073. has_error_code = true;
  4074. error_code =
  4075. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  4076. }
  4077. /* fall through */
  4078. case INTR_TYPE_SOFT_EXCEPTION:
  4079. kvm_clear_exception_queue(vcpu);
  4080. break;
  4081. default:
  4082. break;
  4083. }
  4084. }
  4085. tss_selector = exit_qualification;
  4086. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  4087. type != INTR_TYPE_EXT_INTR &&
  4088. type != INTR_TYPE_NMI_INTR))
  4089. skip_emulated_instruction(vcpu);
  4090. if (kvm_task_switch(vcpu, tss_selector,
  4091. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  4092. has_error_code, error_code) == EMULATE_FAIL) {
  4093. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4094. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4095. vcpu->run->internal.ndata = 0;
  4096. return 0;
  4097. }
  4098. /* clear all local breakpoint enable flags */
  4099. vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
  4100. /*
  4101. * TODO: What about debug traps on tss switch?
  4102. * Are we supposed to inject them and update dr6?
  4103. */
  4104. return 1;
  4105. }
  4106. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  4107. {
  4108. unsigned long exit_qualification;
  4109. gpa_t gpa;
  4110. int gla_validity;
  4111. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4112. if (exit_qualification & (1 << 6)) {
  4113. printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
  4114. return -EINVAL;
  4115. }
  4116. gla_validity = (exit_qualification >> 7) & 0x3;
  4117. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  4118. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  4119. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  4120. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  4121. vmcs_readl(GUEST_LINEAR_ADDRESS));
  4122. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  4123. (long unsigned int)exit_qualification);
  4124. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  4125. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  4126. return 0;
  4127. }
  4128. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4129. trace_kvm_page_fault(gpa, exit_qualification);
  4130. return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
  4131. }
  4132. static u64 ept_rsvd_mask(u64 spte, int level)
  4133. {
  4134. int i;
  4135. u64 mask = 0;
  4136. for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
  4137. mask |= (1ULL << i);
  4138. if (level > 2)
  4139. /* bits 7:3 reserved */
  4140. mask |= 0xf8;
  4141. else if (level == 2) {
  4142. if (spte & (1ULL << 7))
  4143. /* 2MB ref, bits 20:12 reserved */
  4144. mask |= 0x1ff000;
  4145. else
  4146. /* bits 6:3 reserved */
  4147. mask |= 0x78;
  4148. }
  4149. return mask;
  4150. }
  4151. static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
  4152. int level)
  4153. {
  4154. printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
  4155. /* 010b (write-only) */
  4156. WARN_ON((spte & 0x7) == 0x2);
  4157. /* 110b (write/execute) */
  4158. WARN_ON((spte & 0x7) == 0x6);
  4159. /* 100b (execute-only) and value not supported by logical processor */
  4160. if (!cpu_has_vmx_ept_execute_only())
  4161. WARN_ON((spte & 0x7) == 0x4);
  4162. /* not 000b */
  4163. if ((spte & 0x7)) {
  4164. u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
  4165. if (rsvd_bits != 0) {
  4166. printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
  4167. __func__, rsvd_bits);
  4168. WARN_ON(1);
  4169. }
  4170. if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
  4171. u64 ept_mem_type = (spte & 0x38) >> 3;
  4172. if (ept_mem_type == 2 || ept_mem_type == 3 ||
  4173. ept_mem_type == 7) {
  4174. printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
  4175. __func__, ept_mem_type);
  4176. WARN_ON(1);
  4177. }
  4178. }
  4179. }
  4180. }
  4181. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  4182. {
  4183. u64 sptes[4];
  4184. int nr_sptes, i, ret;
  4185. gpa_t gpa;
  4186. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4187. ret = handle_mmio_page_fault_common(vcpu, gpa, true);
  4188. if (likely(ret == 1))
  4189. return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
  4190. EMULATE_DONE;
  4191. if (unlikely(!ret))
  4192. return 1;
  4193. /* It is the real ept misconfig */
  4194. printk(KERN_ERR "EPT: Misconfiguration.\n");
  4195. printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
  4196. nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
  4197. for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
  4198. ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
  4199. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  4200. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  4201. return 0;
  4202. }
  4203. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  4204. {
  4205. u32 cpu_based_vm_exec_control;
  4206. /* clear pending NMI */
  4207. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4208. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  4209. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4210. ++vcpu->stat.nmi_window_exits;
  4211. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4212. return 1;
  4213. }
  4214. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  4215. {
  4216. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4217. enum emulation_result err = EMULATE_DONE;
  4218. int ret = 1;
  4219. u32 cpu_exec_ctrl;
  4220. bool intr_window_requested;
  4221. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4222. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  4223. while (!guest_state_valid(vcpu)) {
  4224. if (intr_window_requested
  4225. && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
  4226. return handle_interrupt_window(&vmx->vcpu);
  4227. err = emulate_instruction(vcpu, 0);
  4228. if (err == EMULATE_DO_MMIO) {
  4229. ret = 0;
  4230. goto out;
  4231. }
  4232. if (err != EMULATE_DONE)
  4233. return 0;
  4234. if (signal_pending(current))
  4235. goto out;
  4236. if (need_resched())
  4237. schedule();
  4238. }
  4239. vmx->emulation_required = 0;
  4240. out:
  4241. return ret;
  4242. }
  4243. /*
  4244. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  4245. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  4246. */
  4247. static int handle_pause(struct kvm_vcpu *vcpu)
  4248. {
  4249. skip_emulated_instruction(vcpu);
  4250. kvm_vcpu_on_spin(vcpu);
  4251. return 1;
  4252. }
  4253. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  4254. {
  4255. kvm_queue_exception(vcpu, UD_VECTOR);
  4256. return 1;
  4257. }
  4258. /*
  4259. * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
  4260. * We could reuse a single VMCS for all the L2 guests, but we also want the
  4261. * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
  4262. * allows keeping them loaded on the processor, and in the future will allow
  4263. * optimizations where prepare_vmcs02 doesn't need to set all the fields on
  4264. * every entry if they never change.
  4265. * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
  4266. * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
  4267. *
  4268. * The following functions allocate and free a vmcs02 in this pool.
  4269. */
  4270. /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
  4271. static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
  4272. {
  4273. struct vmcs02_list *item;
  4274. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  4275. if (item->vmptr == vmx->nested.current_vmptr) {
  4276. list_move(&item->list, &vmx->nested.vmcs02_pool);
  4277. return &item->vmcs02;
  4278. }
  4279. if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
  4280. /* Recycle the least recently used VMCS. */
  4281. item = list_entry(vmx->nested.vmcs02_pool.prev,
  4282. struct vmcs02_list, list);
  4283. item->vmptr = vmx->nested.current_vmptr;
  4284. list_move(&item->list, &vmx->nested.vmcs02_pool);
  4285. return &item->vmcs02;
  4286. }
  4287. /* Create a new VMCS */
  4288. item = (struct vmcs02_list *)
  4289. kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
  4290. if (!item)
  4291. return NULL;
  4292. item->vmcs02.vmcs = alloc_vmcs();
  4293. if (!item->vmcs02.vmcs) {
  4294. kfree(item);
  4295. return NULL;
  4296. }
  4297. loaded_vmcs_init(&item->vmcs02);
  4298. item->vmptr = vmx->nested.current_vmptr;
  4299. list_add(&(item->list), &(vmx->nested.vmcs02_pool));
  4300. vmx->nested.vmcs02_num++;
  4301. return &item->vmcs02;
  4302. }
  4303. /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
  4304. static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
  4305. {
  4306. struct vmcs02_list *item;
  4307. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  4308. if (item->vmptr == vmptr) {
  4309. free_loaded_vmcs(&item->vmcs02);
  4310. list_del(&item->list);
  4311. kfree(item);
  4312. vmx->nested.vmcs02_num--;
  4313. return;
  4314. }
  4315. }
  4316. /*
  4317. * Free all VMCSs saved for this vcpu, except the one pointed by
  4318. * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
  4319. * currently used, if running L2), and vmcs01 when running L2.
  4320. */
  4321. static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
  4322. {
  4323. struct vmcs02_list *item, *n;
  4324. list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
  4325. if (vmx->loaded_vmcs != &item->vmcs02)
  4326. free_loaded_vmcs(&item->vmcs02);
  4327. list_del(&item->list);
  4328. kfree(item);
  4329. }
  4330. vmx->nested.vmcs02_num = 0;
  4331. if (vmx->loaded_vmcs != &vmx->vmcs01)
  4332. free_loaded_vmcs(&vmx->vmcs01);
  4333. }
  4334. /*
  4335. * Emulate the VMXON instruction.
  4336. * Currently, we just remember that VMX is active, and do not save or even
  4337. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  4338. * do not currently need to store anything in that guest-allocated memory
  4339. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  4340. * argument is different from the VMXON pointer (which the spec says they do).
  4341. */
  4342. static int handle_vmon(struct kvm_vcpu *vcpu)
  4343. {
  4344. struct kvm_segment cs;
  4345. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4346. /* The Intel VMX Instruction Reference lists a bunch of bits that
  4347. * are prerequisite to running VMXON, most notably cr4.VMXE must be
  4348. * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
  4349. * Otherwise, we should fail with #UD. We test these now:
  4350. */
  4351. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
  4352. !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
  4353. (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4354. kvm_queue_exception(vcpu, UD_VECTOR);
  4355. return 1;
  4356. }
  4357. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4358. if (is_long_mode(vcpu) && !cs.l) {
  4359. kvm_queue_exception(vcpu, UD_VECTOR);
  4360. return 1;
  4361. }
  4362. if (vmx_get_cpl(vcpu)) {
  4363. kvm_inject_gp(vcpu, 0);
  4364. return 1;
  4365. }
  4366. INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
  4367. vmx->nested.vmcs02_num = 0;
  4368. vmx->nested.vmxon = true;
  4369. skip_emulated_instruction(vcpu);
  4370. return 1;
  4371. }
  4372. /*
  4373. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  4374. * for running VMX instructions (except VMXON, whose prerequisites are
  4375. * slightly different). It also specifies what exception to inject otherwise.
  4376. */
  4377. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  4378. {
  4379. struct kvm_segment cs;
  4380. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4381. if (!vmx->nested.vmxon) {
  4382. kvm_queue_exception(vcpu, UD_VECTOR);
  4383. return 0;
  4384. }
  4385. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4386. if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
  4387. (is_long_mode(vcpu) && !cs.l)) {
  4388. kvm_queue_exception(vcpu, UD_VECTOR);
  4389. return 0;
  4390. }
  4391. if (vmx_get_cpl(vcpu)) {
  4392. kvm_inject_gp(vcpu, 0);
  4393. return 0;
  4394. }
  4395. return 1;
  4396. }
  4397. /*
  4398. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  4399. * just stops using VMX.
  4400. */
  4401. static void free_nested(struct vcpu_vmx *vmx)
  4402. {
  4403. if (!vmx->nested.vmxon)
  4404. return;
  4405. vmx->nested.vmxon = false;
  4406. if (vmx->nested.current_vmptr != -1ull) {
  4407. kunmap(vmx->nested.current_vmcs12_page);
  4408. nested_release_page(vmx->nested.current_vmcs12_page);
  4409. vmx->nested.current_vmptr = -1ull;
  4410. vmx->nested.current_vmcs12 = NULL;
  4411. }
  4412. /* Unpin physical memory we referred to in current vmcs02 */
  4413. if (vmx->nested.apic_access_page) {
  4414. nested_release_page(vmx->nested.apic_access_page);
  4415. vmx->nested.apic_access_page = 0;
  4416. }
  4417. nested_free_all_saved_vmcss(vmx);
  4418. }
  4419. /* Emulate the VMXOFF instruction */
  4420. static int handle_vmoff(struct kvm_vcpu *vcpu)
  4421. {
  4422. if (!nested_vmx_check_permission(vcpu))
  4423. return 1;
  4424. free_nested(to_vmx(vcpu));
  4425. skip_emulated_instruction(vcpu);
  4426. return 1;
  4427. }
  4428. /*
  4429. * Decode the memory-address operand of a vmx instruction, as recorded on an
  4430. * exit caused by such an instruction (run by a guest hypervisor).
  4431. * On success, returns 0. When the operand is invalid, returns 1 and throws
  4432. * #UD or #GP.
  4433. */
  4434. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  4435. unsigned long exit_qualification,
  4436. u32 vmx_instruction_info, gva_t *ret)
  4437. {
  4438. /*
  4439. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  4440. * Execution", on an exit, vmx_instruction_info holds most of the
  4441. * addressing components of the operand. Only the displacement part
  4442. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  4443. * For how an actual address is calculated from all these components,
  4444. * refer to Vol. 1, "Operand Addressing".
  4445. */
  4446. int scaling = vmx_instruction_info & 3;
  4447. int addr_size = (vmx_instruction_info >> 7) & 7;
  4448. bool is_reg = vmx_instruction_info & (1u << 10);
  4449. int seg_reg = (vmx_instruction_info >> 15) & 7;
  4450. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  4451. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  4452. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  4453. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  4454. if (is_reg) {
  4455. kvm_queue_exception(vcpu, UD_VECTOR);
  4456. return 1;
  4457. }
  4458. /* Addr = segment_base + offset */
  4459. /* offset = base + [index * scale] + displacement */
  4460. *ret = vmx_get_segment_base(vcpu, seg_reg);
  4461. if (base_is_valid)
  4462. *ret += kvm_register_read(vcpu, base_reg);
  4463. if (index_is_valid)
  4464. *ret += kvm_register_read(vcpu, index_reg)<<scaling;
  4465. *ret += exit_qualification; /* holds the displacement */
  4466. if (addr_size == 1) /* 32 bit */
  4467. *ret &= 0xffffffff;
  4468. /*
  4469. * TODO: throw #GP (and return 1) in various cases that the VM*
  4470. * instructions require it - e.g., offset beyond segment limit,
  4471. * unusable or unreadable/unwritable segment, non-canonical 64-bit
  4472. * address, and so on. Currently these are not checked.
  4473. */
  4474. return 0;
  4475. }
  4476. /*
  4477. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  4478. * set the success or error code of an emulated VMX instruction, as specified
  4479. * by Vol 2B, VMX Instruction Reference, "Conventions".
  4480. */
  4481. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  4482. {
  4483. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  4484. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  4485. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  4486. }
  4487. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  4488. {
  4489. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  4490. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  4491. X86_EFLAGS_SF | X86_EFLAGS_OF))
  4492. | X86_EFLAGS_CF);
  4493. }
  4494. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  4495. u32 vm_instruction_error)
  4496. {
  4497. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  4498. /*
  4499. * failValid writes the error number to the current VMCS, which
  4500. * can't be done there isn't a current VMCS.
  4501. */
  4502. nested_vmx_failInvalid(vcpu);
  4503. return;
  4504. }
  4505. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  4506. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  4507. X86_EFLAGS_SF | X86_EFLAGS_OF))
  4508. | X86_EFLAGS_ZF);
  4509. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  4510. }
  4511. /* Emulate the VMCLEAR instruction */
  4512. static int handle_vmclear(struct kvm_vcpu *vcpu)
  4513. {
  4514. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4515. gva_t gva;
  4516. gpa_t vmptr;
  4517. struct vmcs12 *vmcs12;
  4518. struct page *page;
  4519. struct x86_exception e;
  4520. if (!nested_vmx_check_permission(vcpu))
  4521. return 1;
  4522. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  4523. vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
  4524. return 1;
  4525. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
  4526. sizeof(vmptr), &e)) {
  4527. kvm_inject_page_fault(vcpu, &e);
  4528. return 1;
  4529. }
  4530. if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
  4531. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  4532. skip_emulated_instruction(vcpu);
  4533. return 1;
  4534. }
  4535. if (vmptr == vmx->nested.current_vmptr) {
  4536. kunmap(vmx->nested.current_vmcs12_page);
  4537. nested_release_page(vmx->nested.current_vmcs12_page);
  4538. vmx->nested.current_vmptr = -1ull;
  4539. vmx->nested.current_vmcs12 = NULL;
  4540. }
  4541. page = nested_get_page(vcpu, vmptr);
  4542. if (page == NULL) {
  4543. /*
  4544. * For accurate processor emulation, VMCLEAR beyond available
  4545. * physical memory should do nothing at all. However, it is
  4546. * possible that a nested vmx bug, not a guest hypervisor bug,
  4547. * resulted in this case, so let's shut down before doing any
  4548. * more damage:
  4549. */
  4550. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  4551. return 1;
  4552. }
  4553. vmcs12 = kmap(page);
  4554. vmcs12->launch_state = 0;
  4555. kunmap(page);
  4556. nested_release_page(page);
  4557. nested_free_vmcs02(vmx, vmptr);
  4558. skip_emulated_instruction(vcpu);
  4559. nested_vmx_succeed(vcpu);
  4560. return 1;
  4561. }
  4562. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  4563. /* Emulate the VMLAUNCH instruction */
  4564. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  4565. {
  4566. return nested_vmx_run(vcpu, true);
  4567. }
  4568. /* Emulate the VMRESUME instruction */
  4569. static int handle_vmresume(struct kvm_vcpu *vcpu)
  4570. {
  4571. return nested_vmx_run(vcpu, false);
  4572. }
  4573. enum vmcs_field_type {
  4574. VMCS_FIELD_TYPE_U16 = 0,
  4575. VMCS_FIELD_TYPE_U64 = 1,
  4576. VMCS_FIELD_TYPE_U32 = 2,
  4577. VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
  4578. };
  4579. static inline int vmcs_field_type(unsigned long field)
  4580. {
  4581. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  4582. return VMCS_FIELD_TYPE_U32;
  4583. return (field >> 13) & 0x3 ;
  4584. }
  4585. static inline int vmcs_field_readonly(unsigned long field)
  4586. {
  4587. return (((field >> 10) & 0x3) == 1);
  4588. }
  4589. /*
  4590. * Read a vmcs12 field. Since these can have varying lengths and we return
  4591. * one type, we chose the biggest type (u64) and zero-extend the return value
  4592. * to that size. Note that the caller, handle_vmread, might need to use only
  4593. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  4594. * 64-bit fields are to be returned).
  4595. */
  4596. static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
  4597. unsigned long field, u64 *ret)
  4598. {
  4599. short offset = vmcs_field_to_offset(field);
  4600. char *p;
  4601. if (offset < 0)
  4602. return 0;
  4603. p = ((char *)(get_vmcs12(vcpu))) + offset;
  4604. switch (vmcs_field_type(field)) {
  4605. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  4606. *ret = *((natural_width *)p);
  4607. return 1;
  4608. case VMCS_FIELD_TYPE_U16:
  4609. *ret = *((u16 *)p);
  4610. return 1;
  4611. case VMCS_FIELD_TYPE_U32:
  4612. *ret = *((u32 *)p);
  4613. return 1;
  4614. case VMCS_FIELD_TYPE_U64:
  4615. *ret = *((u64 *)p);
  4616. return 1;
  4617. default:
  4618. return 0; /* can never happen. */
  4619. }
  4620. }
  4621. /*
  4622. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  4623. * used before) all generate the same failure when it is missing.
  4624. */
  4625. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  4626. {
  4627. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4628. if (vmx->nested.current_vmptr == -1ull) {
  4629. nested_vmx_failInvalid(vcpu);
  4630. skip_emulated_instruction(vcpu);
  4631. return 0;
  4632. }
  4633. return 1;
  4634. }
  4635. static int handle_vmread(struct kvm_vcpu *vcpu)
  4636. {
  4637. unsigned long field;
  4638. u64 field_value;
  4639. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4640. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  4641. gva_t gva = 0;
  4642. if (!nested_vmx_check_permission(vcpu) ||
  4643. !nested_vmx_check_vmcs12(vcpu))
  4644. return 1;
  4645. /* Decode instruction info and find the field to read */
  4646. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  4647. /* Read the field, zero-extended to a u64 field_value */
  4648. if (!vmcs12_read_any(vcpu, field, &field_value)) {
  4649. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  4650. skip_emulated_instruction(vcpu);
  4651. return 1;
  4652. }
  4653. /*
  4654. * Now copy part of this value to register or memory, as requested.
  4655. * Note that the number of bits actually copied is 32 or 64 depending
  4656. * on the guest's mode (32 or 64 bit), not on the given field's length.
  4657. */
  4658. if (vmx_instruction_info & (1u << 10)) {
  4659. kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  4660. field_value);
  4661. } else {
  4662. if (get_vmx_mem_address(vcpu, exit_qualification,
  4663. vmx_instruction_info, &gva))
  4664. return 1;
  4665. /* _system ok, as nested_vmx_check_permission verified cpl=0 */
  4666. kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
  4667. &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
  4668. }
  4669. nested_vmx_succeed(vcpu);
  4670. skip_emulated_instruction(vcpu);
  4671. return 1;
  4672. }
  4673. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  4674. {
  4675. unsigned long field;
  4676. gva_t gva;
  4677. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4678. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  4679. char *p;
  4680. short offset;
  4681. /* The value to write might be 32 or 64 bits, depending on L1's long
  4682. * mode, and eventually we need to write that into a field of several
  4683. * possible lengths. The code below first zero-extends the value to 64
  4684. * bit (field_value), and then copies only the approriate number of
  4685. * bits into the vmcs12 field.
  4686. */
  4687. u64 field_value = 0;
  4688. struct x86_exception e;
  4689. if (!nested_vmx_check_permission(vcpu) ||
  4690. !nested_vmx_check_vmcs12(vcpu))
  4691. return 1;
  4692. if (vmx_instruction_info & (1u << 10))
  4693. field_value = kvm_register_read(vcpu,
  4694. (((vmx_instruction_info) >> 3) & 0xf));
  4695. else {
  4696. if (get_vmx_mem_address(vcpu, exit_qualification,
  4697. vmx_instruction_info, &gva))
  4698. return 1;
  4699. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
  4700. &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
  4701. kvm_inject_page_fault(vcpu, &e);
  4702. return 1;
  4703. }
  4704. }
  4705. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  4706. if (vmcs_field_readonly(field)) {
  4707. nested_vmx_failValid(vcpu,
  4708. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  4709. skip_emulated_instruction(vcpu);
  4710. return 1;
  4711. }
  4712. offset = vmcs_field_to_offset(field);
  4713. if (offset < 0) {
  4714. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  4715. skip_emulated_instruction(vcpu);
  4716. return 1;
  4717. }
  4718. p = ((char *) get_vmcs12(vcpu)) + offset;
  4719. switch (vmcs_field_type(field)) {
  4720. case VMCS_FIELD_TYPE_U16:
  4721. *(u16 *)p = field_value;
  4722. break;
  4723. case VMCS_FIELD_TYPE_U32:
  4724. *(u32 *)p = field_value;
  4725. break;
  4726. case VMCS_FIELD_TYPE_U64:
  4727. *(u64 *)p = field_value;
  4728. break;
  4729. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  4730. *(natural_width *)p = field_value;
  4731. break;
  4732. default:
  4733. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  4734. skip_emulated_instruction(vcpu);
  4735. return 1;
  4736. }
  4737. nested_vmx_succeed(vcpu);
  4738. skip_emulated_instruction(vcpu);
  4739. return 1;
  4740. }
  4741. /* Emulate the VMPTRLD instruction */
  4742. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  4743. {
  4744. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4745. gva_t gva;
  4746. gpa_t vmptr;
  4747. struct x86_exception e;
  4748. if (!nested_vmx_check_permission(vcpu))
  4749. return 1;
  4750. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  4751. vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
  4752. return 1;
  4753. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
  4754. sizeof(vmptr), &e)) {
  4755. kvm_inject_page_fault(vcpu, &e);
  4756. return 1;
  4757. }
  4758. if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
  4759. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  4760. skip_emulated_instruction(vcpu);
  4761. return 1;
  4762. }
  4763. if (vmx->nested.current_vmptr != vmptr) {
  4764. struct vmcs12 *new_vmcs12;
  4765. struct page *page;
  4766. page = nested_get_page(vcpu, vmptr);
  4767. if (page == NULL) {
  4768. nested_vmx_failInvalid(vcpu);
  4769. skip_emulated_instruction(vcpu);
  4770. return 1;
  4771. }
  4772. new_vmcs12 = kmap(page);
  4773. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  4774. kunmap(page);
  4775. nested_release_page_clean(page);
  4776. nested_vmx_failValid(vcpu,
  4777. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  4778. skip_emulated_instruction(vcpu);
  4779. return 1;
  4780. }
  4781. if (vmx->nested.current_vmptr != -1ull) {
  4782. kunmap(vmx->nested.current_vmcs12_page);
  4783. nested_release_page(vmx->nested.current_vmcs12_page);
  4784. }
  4785. vmx->nested.current_vmptr = vmptr;
  4786. vmx->nested.current_vmcs12 = new_vmcs12;
  4787. vmx->nested.current_vmcs12_page = page;
  4788. }
  4789. nested_vmx_succeed(vcpu);
  4790. skip_emulated_instruction(vcpu);
  4791. return 1;
  4792. }
  4793. /* Emulate the VMPTRST instruction */
  4794. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  4795. {
  4796. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4797. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  4798. gva_t vmcs_gva;
  4799. struct x86_exception e;
  4800. if (!nested_vmx_check_permission(vcpu))
  4801. return 1;
  4802. if (get_vmx_mem_address(vcpu, exit_qualification,
  4803. vmx_instruction_info, &vmcs_gva))
  4804. return 1;
  4805. /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
  4806. if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
  4807. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  4808. sizeof(u64), &e)) {
  4809. kvm_inject_page_fault(vcpu, &e);
  4810. return 1;
  4811. }
  4812. nested_vmx_succeed(vcpu);
  4813. skip_emulated_instruction(vcpu);
  4814. return 1;
  4815. }
  4816. /*
  4817. * The exit handlers return 1 if the exit was handled fully and guest execution
  4818. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  4819. * to be done to userspace and return 0.
  4820. */
  4821. static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  4822. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  4823. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  4824. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  4825. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  4826. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  4827. [EXIT_REASON_CR_ACCESS] = handle_cr,
  4828. [EXIT_REASON_DR_ACCESS] = handle_dr,
  4829. [EXIT_REASON_CPUID] = handle_cpuid,
  4830. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  4831. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  4832. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  4833. [EXIT_REASON_HLT] = handle_halt,
  4834. [EXIT_REASON_INVD] = handle_invd,
  4835. [EXIT_REASON_INVLPG] = handle_invlpg,
  4836. [EXIT_REASON_RDPMC] = handle_rdpmc,
  4837. [EXIT_REASON_VMCALL] = handle_vmcall,
  4838. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  4839. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  4840. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  4841. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  4842. [EXIT_REASON_VMREAD] = handle_vmread,
  4843. [EXIT_REASON_VMRESUME] = handle_vmresume,
  4844. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  4845. [EXIT_REASON_VMOFF] = handle_vmoff,
  4846. [EXIT_REASON_VMON] = handle_vmon,
  4847. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  4848. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  4849. [EXIT_REASON_WBINVD] = handle_wbinvd,
  4850. [EXIT_REASON_XSETBV] = handle_xsetbv,
  4851. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  4852. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  4853. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  4854. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  4855. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  4856. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
  4857. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
  4858. };
  4859. static const int kvm_vmx_max_exit_handlers =
  4860. ARRAY_SIZE(kvm_vmx_exit_handlers);
  4861. /*
  4862. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  4863. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  4864. * disinterest in the current event (read or write a specific MSR) by using an
  4865. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  4866. */
  4867. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  4868. struct vmcs12 *vmcs12, u32 exit_reason)
  4869. {
  4870. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  4871. gpa_t bitmap;
  4872. if (!nested_cpu_has(get_vmcs12(vcpu), CPU_BASED_USE_MSR_BITMAPS))
  4873. return 1;
  4874. /*
  4875. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  4876. * for the four combinations of read/write and low/high MSR numbers.
  4877. * First we need to figure out which of the four to use:
  4878. */
  4879. bitmap = vmcs12->msr_bitmap;
  4880. if (exit_reason == EXIT_REASON_MSR_WRITE)
  4881. bitmap += 2048;
  4882. if (msr_index >= 0xc0000000) {
  4883. msr_index -= 0xc0000000;
  4884. bitmap += 1024;
  4885. }
  4886. /* Then read the msr_index'th bit from this bitmap: */
  4887. if (msr_index < 1024*8) {
  4888. unsigned char b;
  4889. kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1);
  4890. return 1 & (b >> (msr_index & 7));
  4891. } else
  4892. return 1; /* let L1 handle the wrong parameter */
  4893. }
  4894. /*
  4895. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  4896. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  4897. * intercept (via guest_host_mask etc.) the current event.
  4898. */
  4899. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  4900. struct vmcs12 *vmcs12)
  4901. {
  4902. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4903. int cr = exit_qualification & 15;
  4904. int reg = (exit_qualification >> 8) & 15;
  4905. unsigned long val = kvm_register_read(vcpu, reg);
  4906. switch ((exit_qualification >> 4) & 3) {
  4907. case 0: /* mov to cr */
  4908. switch (cr) {
  4909. case 0:
  4910. if (vmcs12->cr0_guest_host_mask &
  4911. (val ^ vmcs12->cr0_read_shadow))
  4912. return 1;
  4913. break;
  4914. case 3:
  4915. if ((vmcs12->cr3_target_count >= 1 &&
  4916. vmcs12->cr3_target_value0 == val) ||
  4917. (vmcs12->cr3_target_count >= 2 &&
  4918. vmcs12->cr3_target_value1 == val) ||
  4919. (vmcs12->cr3_target_count >= 3 &&
  4920. vmcs12->cr3_target_value2 == val) ||
  4921. (vmcs12->cr3_target_count >= 4 &&
  4922. vmcs12->cr3_target_value3 == val))
  4923. return 0;
  4924. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  4925. return 1;
  4926. break;
  4927. case 4:
  4928. if (vmcs12->cr4_guest_host_mask &
  4929. (vmcs12->cr4_read_shadow ^ val))
  4930. return 1;
  4931. break;
  4932. case 8:
  4933. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  4934. return 1;
  4935. break;
  4936. }
  4937. break;
  4938. case 2: /* clts */
  4939. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  4940. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  4941. return 1;
  4942. break;
  4943. case 1: /* mov from cr */
  4944. switch (cr) {
  4945. case 3:
  4946. if (vmcs12->cpu_based_vm_exec_control &
  4947. CPU_BASED_CR3_STORE_EXITING)
  4948. return 1;
  4949. break;
  4950. case 8:
  4951. if (vmcs12->cpu_based_vm_exec_control &
  4952. CPU_BASED_CR8_STORE_EXITING)
  4953. return 1;
  4954. break;
  4955. }
  4956. break;
  4957. case 3: /* lmsw */
  4958. /*
  4959. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  4960. * cr0. Other attempted changes are ignored, with no exit.
  4961. */
  4962. if (vmcs12->cr0_guest_host_mask & 0xe &
  4963. (val ^ vmcs12->cr0_read_shadow))
  4964. return 1;
  4965. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  4966. !(vmcs12->cr0_read_shadow & 0x1) &&
  4967. (val & 0x1))
  4968. return 1;
  4969. break;
  4970. }
  4971. return 0;
  4972. }
  4973. /*
  4974. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  4975. * should handle it ourselves in L0 (and then continue L2). Only call this
  4976. * when in is_guest_mode (L2).
  4977. */
  4978. static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
  4979. {
  4980. u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
  4981. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  4982. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4983. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4984. if (vmx->nested.nested_run_pending)
  4985. return 0;
  4986. if (unlikely(vmx->fail)) {
  4987. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  4988. vmcs_read32(VM_INSTRUCTION_ERROR));
  4989. return 1;
  4990. }
  4991. switch (exit_reason) {
  4992. case EXIT_REASON_EXCEPTION_NMI:
  4993. if (!is_exception(intr_info))
  4994. return 0;
  4995. else if (is_page_fault(intr_info))
  4996. return enable_ept;
  4997. return vmcs12->exception_bitmap &
  4998. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  4999. case EXIT_REASON_EXTERNAL_INTERRUPT:
  5000. return 0;
  5001. case EXIT_REASON_TRIPLE_FAULT:
  5002. return 1;
  5003. case EXIT_REASON_PENDING_INTERRUPT:
  5004. case EXIT_REASON_NMI_WINDOW:
  5005. /*
  5006. * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit
  5007. * (aka Interrupt Window Exiting) only when L1 turned it on,
  5008. * so if we got a PENDING_INTERRUPT exit, this must be for L1.
  5009. * Same for NMI Window Exiting.
  5010. */
  5011. return 1;
  5012. case EXIT_REASON_TASK_SWITCH:
  5013. return 1;
  5014. case EXIT_REASON_CPUID:
  5015. return 1;
  5016. case EXIT_REASON_HLT:
  5017. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  5018. case EXIT_REASON_INVD:
  5019. return 1;
  5020. case EXIT_REASON_INVLPG:
  5021. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  5022. case EXIT_REASON_RDPMC:
  5023. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  5024. case EXIT_REASON_RDTSC:
  5025. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  5026. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  5027. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  5028. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  5029. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  5030. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  5031. /*
  5032. * VMX instructions trap unconditionally. This allows L1 to
  5033. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  5034. */
  5035. return 1;
  5036. case EXIT_REASON_CR_ACCESS:
  5037. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  5038. case EXIT_REASON_DR_ACCESS:
  5039. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  5040. case EXIT_REASON_IO_INSTRUCTION:
  5041. /* TODO: support IO bitmaps */
  5042. return 1;
  5043. case EXIT_REASON_MSR_READ:
  5044. case EXIT_REASON_MSR_WRITE:
  5045. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  5046. case EXIT_REASON_INVALID_STATE:
  5047. return 1;
  5048. case EXIT_REASON_MWAIT_INSTRUCTION:
  5049. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  5050. case EXIT_REASON_MONITOR_INSTRUCTION:
  5051. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  5052. case EXIT_REASON_PAUSE_INSTRUCTION:
  5053. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  5054. nested_cpu_has2(vmcs12,
  5055. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  5056. case EXIT_REASON_MCE_DURING_VMENTRY:
  5057. return 0;
  5058. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  5059. return 1;
  5060. case EXIT_REASON_APIC_ACCESS:
  5061. return nested_cpu_has2(vmcs12,
  5062. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  5063. case EXIT_REASON_EPT_VIOLATION:
  5064. case EXIT_REASON_EPT_MISCONFIG:
  5065. return 0;
  5066. case EXIT_REASON_WBINVD:
  5067. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  5068. case EXIT_REASON_XSETBV:
  5069. return 1;
  5070. default:
  5071. return 1;
  5072. }
  5073. }
  5074. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  5075. {
  5076. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  5077. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  5078. }
  5079. /*
  5080. * The guest has exited. See if we can fix it or if we need userspace
  5081. * assistance.
  5082. */
  5083. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  5084. {
  5085. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5086. u32 exit_reason = vmx->exit_reason;
  5087. u32 vectoring_info = vmx->idt_vectoring_info;
  5088. /* If guest state is invalid, start emulating */
  5089. if (vmx->emulation_required && emulate_invalid_guest_state)
  5090. return handle_invalid_guest_state(vcpu);
  5091. /*
  5092. * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
  5093. * we did not inject a still-pending event to L1 now because of
  5094. * nested_run_pending, we need to re-enable this bit.
  5095. */
  5096. if (vmx->nested.nested_run_pending)
  5097. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5098. if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
  5099. exit_reason == EXIT_REASON_VMRESUME))
  5100. vmx->nested.nested_run_pending = 1;
  5101. else
  5102. vmx->nested.nested_run_pending = 0;
  5103. if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
  5104. nested_vmx_vmexit(vcpu);
  5105. return 1;
  5106. }
  5107. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  5108. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  5109. vcpu->run->fail_entry.hardware_entry_failure_reason
  5110. = exit_reason;
  5111. return 0;
  5112. }
  5113. if (unlikely(vmx->fail)) {
  5114. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  5115. vcpu->run->fail_entry.hardware_entry_failure_reason
  5116. = vmcs_read32(VM_INSTRUCTION_ERROR);
  5117. return 0;
  5118. }
  5119. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  5120. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  5121. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  5122. exit_reason != EXIT_REASON_TASK_SWITCH))
  5123. printk(KERN_WARNING "%s: unexpected, valid vectoring info "
  5124. "(0x%x) and exit reason is 0x%x\n",
  5125. __func__, vectoring_info, exit_reason);
  5126. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
  5127. !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
  5128. get_vmcs12(vcpu), vcpu)))) {
  5129. if (vmx_interrupt_allowed(vcpu)) {
  5130. vmx->soft_vnmi_blocked = 0;
  5131. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  5132. vcpu->arch.nmi_pending) {
  5133. /*
  5134. * This CPU don't support us in finding the end of an
  5135. * NMI-blocked window if the guest runs with IRQs
  5136. * disabled. So we pull the trigger after 1 s of
  5137. * futile waiting, but inform the user about this.
  5138. */
  5139. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  5140. "state on VCPU %d after 1 s timeout\n",
  5141. __func__, vcpu->vcpu_id);
  5142. vmx->soft_vnmi_blocked = 0;
  5143. }
  5144. }
  5145. if (exit_reason < kvm_vmx_max_exit_handlers
  5146. && kvm_vmx_exit_handlers[exit_reason])
  5147. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  5148. else {
  5149. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  5150. vcpu->run->hw.hardware_exit_reason = exit_reason;
  5151. }
  5152. return 0;
  5153. }
  5154. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  5155. {
  5156. if (irr == -1 || tpr < irr) {
  5157. vmcs_write32(TPR_THRESHOLD, 0);
  5158. return;
  5159. }
  5160. vmcs_write32(TPR_THRESHOLD, irr);
  5161. }
  5162. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  5163. {
  5164. u32 exit_intr_info;
  5165. if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  5166. || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
  5167. return;
  5168. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  5169. exit_intr_info = vmx->exit_intr_info;
  5170. /* Handle machine checks before interrupts are enabled */
  5171. if (is_machine_check(exit_intr_info))
  5172. kvm_machine_check();
  5173. /* We need to handle NMIs before interrupts are enabled */
  5174. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  5175. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  5176. kvm_before_handle_nmi(&vmx->vcpu);
  5177. asm("int $2");
  5178. kvm_after_handle_nmi(&vmx->vcpu);
  5179. }
  5180. }
  5181. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  5182. {
  5183. u32 exit_intr_info;
  5184. bool unblock_nmi;
  5185. u8 vector;
  5186. bool idtv_info_valid;
  5187. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  5188. if (cpu_has_virtual_nmis()) {
  5189. if (vmx->nmi_known_unmasked)
  5190. return;
  5191. /*
  5192. * Can't use vmx->exit_intr_info since we're not sure what
  5193. * the exit reason is.
  5194. */
  5195. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  5196. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  5197. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  5198. /*
  5199. * SDM 3: 27.7.1.2 (September 2008)
  5200. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  5201. * a guest IRET fault.
  5202. * SDM 3: 23.2.2 (September 2008)
  5203. * Bit 12 is undefined in any of the following cases:
  5204. * If the VM exit sets the valid bit in the IDT-vectoring
  5205. * information field.
  5206. * If the VM exit is due to a double fault.
  5207. */
  5208. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  5209. vector != DF_VECTOR && !idtv_info_valid)
  5210. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5211. GUEST_INTR_STATE_NMI);
  5212. else
  5213. vmx->nmi_known_unmasked =
  5214. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  5215. & GUEST_INTR_STATE_NMI);
  5216. } else if (unlikely(vmx->soft_vnmi_blocked))
  5217. vmx->vnmi_blocked_time +=
  5218. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  5219. }
  5220. static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
  5221. u32 idt_vectoring_info,
  5222. int instr_len_field,
  5223. int error_code_field)
  5224. {
  5225. u8 vector;
  5226. int type;
  5227. bool idtv_info_valid;
  5228. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  5229. vmx->vcpu.arch.nmi_injected = false;
  5230. kvm_clear_exception_queue(&vmx->vcpu);
  5231. kvm_clear_interrupt_queue(&vmx->vcpu);
  5232. if (!idtv_info_valid)
  5233. return;
  5234. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  5235. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  5236. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  5237. switch (type) {
  5238. case INTR_TYPE_NMI_INTR:
  5239. vmx->vcpu.arch.nmi_injected = true;
  5240. /*
  5241. * SDM 3: 27.7.1.2 (September 2008)
  5242. * Clear bit "block by NMI" before VM entry if a NMI
  5243. * delivery faulted.
  5244. */
  5245. vmx_set_nmi_mask(&vmx->vcpu, false);
  5246. break;
  5247. case INTR_TYPE_SOFT_EXCEPTION:
  5248. vmx->vcpu.arch.event_exit_inst_len =
  5249. vmcs_read32(instr_len_field);
  5250. /* fall through */
  5251. case INTR_TYPE_HARD_EXCEPTION:
  5252. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  5253. u32 err = vmcs_read32(error_code_field);
  5254. kvm_queue_exception_e(&vmx->vcpu, vector, err);
  5255. } else
  5256. kvm_queue_exception(&vmx->vcpu, vector);
  5257. break;
  5258. case INTR_TYPE_SOFT_INTR:
  5259. vmx->vcpu.arch.event_exit_inst_len =
  5260. vmcs_read32(instr_len_field);
  5261. /* fall through */
  5262. case INTR_TYPE_EXT_INTR:
  5263. kvm_queue_interrupt(&vmx->vcpu, vector,
  5264. type == INTR_TYPE_SOFT_INTR);
  5265. break;
  5266. default:
  5267. break;
  5268. }
  5269. }
  5270. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  5271. {
  5272. if (is_guest_mode(&vmx->vcpu))
  5273. return;
  5274. __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
  5275. VM_EXIT_INSTRUCTION_LEN,
  5276. IDT_VECTORING_ERROR_CODE);
  5277. }
  5278. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  5279. {
  5280. if (is_guest_mode(vcpu))
  5281. return;
  5282. __vmx_complete_interrupts(to_vmx(vcpu),
  5283. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  5284. VM_ENTRY_INSTRUCTION_LEN,
  5285. VM_ENTRY_EXCEPTION_ERROR_CODE);
  5286. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  5287. }
  5288. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  5289. {
  5290. int i, nr_msrs;
  5291. struct perf_guest_switch_msr *msrs;
  5292. msrs = perf_guest_get_msrs(&nr_msrs);
  5293. if (!msrs)
  5294. return;
  5295. for (i = 0; i < nr_msrs; i++)
  5296. if (msrs[i].host == msrs[i].guest)
  5297. clear_atomic_switch_msr(vmx, msrs[i].msr);
  5298. else
  5299. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  5300. msrs[i].host);
  5301. }
  5302. #ifdef CONFIG_X86_64
  5303. #define R "r"
  5304. #define Q "q"
  5305. #else
  5306. #define R "e"
  5307. #define Q "l"
  5308. #endif
  5309. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  5310. {
  5311. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5312. if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) {
  5313. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5314. if (vmcs12->idt_vectoring_info_field &
  5315. VECTORING_INFO_VALID_MASK) {
  5316. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5317. vmcs12->idt_vectoring_info_field);
  5318. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5319. vmcs12->vm_exit_instruction_len);
  5320. if (vmcs12->idt_vectoring_info_field &
  5321. VECTORING_INFO_DELIVER_CODE_MASK)
  5322. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  5323. vmcs12->idt_vectoring_error_code);
  5324. }
  5325. }
  5326. /* Record the guest's net vcpu time for enforced NMI injections. */
  5327. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  5328. vmx->entry_time = ktime_get();
  5329. /* Don't enter VMX if guest state is invalid, let the exit handler
  5330. start emulation until we arrive back to a valid state */
  5331. if (vmx->emulation_required && emulate_invalid_guest_state)
  5332. return;
  5333. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  5334. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  5335. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  5336. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  5337. /* When single-stepping over STI and MOV SS, we must clear the
  5338. * corresponding interruptibility bits in the guest state. Otherwise
  5339. * vmentry fails as it then expects bit 14 (BS) in pending debug
  5340. * exceptions being set, but that's not correct for the guest debugging
  5341. * case. */
  5342. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  5343. vmx_set_interrupt_shadow(vcpu, 0);
  5344. atomic_switch_perf_msrs(vmx);
  5345. vmx->__launched = vmx->loaded_vmcs->launched;
  5346. asm(
  5347. /* Store host registers */
  5348. "push %%"R"dx; push %%"R"bp;"
  5349. "push %%"R"cx \n\t" /* placeholder for guest rcx */
  5350. "push %%"R"cx \n\t"
  5351. "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
  5352. "je 1f \n\t"
  5353. "mov %%"R"sp, %c[host_rsp](%0) \n\t"
  5354. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  5355. "1: \n\t"
  5356. /* Reload cr2 if changed */
  5357. "mov %c[cr2](%0), %%"R"ax \n\t"
  5358. "mov %%cr2, %%"R"dx \n\t"
  5359. "cmp %%"R"ax, %%"R"dx \n\t"
  5360. "je 2f \n\t"
  5361. "mov %%"R"ax, %%cr2 \n\t"
  5362. "2: \n\t"
  5363. /* Check if vmlaunch of vmresume is needed */
  5364. "cmpl $0, %c[launched](%0) \n\t"
  5365. /* Load guest registers. Don't clobber flags. */
  5366. "mov %c[rax](%0), %%"R"ax \n\t"
  5367. "mov %c[rbx](%0), %%"R"bx \n\t"
  5368. "mov %c[rdx](%0), %%"R"dx \n\t"
  5369. "mov %c[rsi](%0), %%"R"si \n\t"
  5370. "mov %c[rdi](%0), %%"R"di \n\t"
  5371. "mov %c[rbp](%0), %%"R"bp \n\t"
  5372. #ifdef CONFIG_X86_64
  5373. "mov %c[r8](%0), %%r8 \n\t"
  5374. "mov %c[r9](%0), %%r9 \n\t"
  5375. "mov %c[r10](%0), %%r10 \n\t"
  5376. "mov %c[r11](%0), %%r11 \n\t"
  5377. "mov %c[r12](%0), %%r12 \n\t"
  5378. "mov %c[r13](%0), %%r13 \n\t"
  5379. "mov %c[r14](%0), %%r14 \n\t"
  5380. "mov %c[r15](%0), %%r15 \n\t"
  5381. #endif
  5382. "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
  5383. /* Enter guest mode */
  5384. "jne .Llaunched \n\t"
  5385. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  5386. "jmp .Lkvm_vmx_return \n\t"
  5387. ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
  5388. ".Lkvm_vmx_return: "
  5389. /* Save guest registers, load host registers, keep flags */
  5390. "mov %0, %c[wordsize](%%"R"sp) \n\t"
  5391. "pop %0 \n\t"
  5392. "mov %%"R"ax, %c[rax](%0) \n\t"
  5393. "mov %%"R"bx, %c[rbx](%0) \n\t"
  5394. "pop"Q" %c[rcx](%0) \n\t"
  5395. "mov %%"R"dx, %c[rdx](%0) \n\t"
  5396. "mov %%"R"si, %c[rsi](%0) \n\t"
  5397. "mov %%"R"di, %c[rdi](%0) \n\t"
  5398. "mov %%"R"bp, %c[rbp](%0) \n\t"
  5399. #ifdef CONFIG_X86_64
  5400. "mov %%r8, %c[r8](%0) \n\t"
  5401. "mov %%r9, %c[r9](%0) \n\t"
  5402. "mov %%r10, %c[r10](%0) \n\t"
  5403. "mov %%r11, %c[r11](%0) \n\t"
  5404. "mov %%r12, %c[r12](%0) \n\t"
  5405. "mov %%r13, %c[r13](%0) \n\t"
  5406. "mov %%r14, %c[r14](%0) \n\t"
  5407. "mov %%r15, %c[r15](%0) \n\t"
  5408. #endif
  5409. "mov %%cr2, %%"R"ax \n\t"
  5410. "mov %%"R"ax, %c[cr2](%0) \n\t"
  5411. "pop %%"R"bp; pop %%"R"dx \n\t"
  5412. "setbe %c[fail](%0) \n\t"
  5413. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  5414. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  5415. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  5416. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  5417. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  5418. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  5419. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  5420. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  5421. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  5422. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  5423. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  5424. #ifdef CONFIG_X86_64
  5425. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  5426. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  5427. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  5428. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  5429. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  5430. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  5431. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  5432. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  5433. #endif
  5434. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  5435. [wordsize]"i"(sizeof(ulong))
  5436. : "cc", "memory"
  5437. , R"ax", R"bx", R"di", R"si"
  5438. #ifdef CONFIG_X86_64
  5439. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  5440. #endif
  5441. );
  5442. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  5443. | (1 << VCPU_EXREG_RFLAGS)
  5444. | (1 << VCPU_EXREG_CPL)
  5445. | (1 << VCPU_EXREG_PDPTR)
  5446. | (1 << VCPU_EXREG_SEGMENTS)
  5447. | (1 << VCPU_EXREG_CR3));
  5448. vcpu->arch.regs_dirty = 0;
  5449. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  5450. if (is_guest_mode(vcpu)) {
  5451. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5452. vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info;
  5453. if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
  5454. vmcs12->idt_vectoring_error_code =
  5455. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  5456. vmcs12->vm_exit_instruction_len =
  5457. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5458. }
  5459. }
  5460. asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
  5461. vmx->loaded_vmcs->launched = 1;
  5462. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  5463. trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
  5464. vmx_complete_atomic_exit(vmx);
  5465. vmx_recover_nmi_blocking(vmx);
  5466. vmx_complete_interrupts(vmx);
  5467. }
  5468. #undef R
  5469. #undef Q
  5470. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  5471. {
  5472. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5473. free_vpid(vmx);
  5474. free_nested(vmx);
  5475. free_loaded_vmcs(vmx->loaded_vmcs);
  5476. kfree(vmx->guest_msrs);
  5477. kvm_vcpu_uninit(vcpu);
  5478. kmem_cache_free(kvm_vcpu_cache, vmx);
  5479. }
  5480. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  5481. {
  5482. int err;
  5483. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  5484. int cpu;
  5485. if (!vmx)
  5486. return ERR_PTR(-ENOMEM);
  5487. allocate_vpid(vmx);
  5488. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  5489. if (err)
  5490. goto free_vcpu;
  5491. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  5492. err = -ENOMEM;
  5493. if (!vmx->guest_msrs) {
  5494. goto uninit_vcpu;
  5495. }
  5496. vmx->loaded_vmcs = &vmx->vmcs01;
  5497. vmx->loaded_vmcs->vmcs = alloc_vmcs();
  5498. if (!vmx->loaded_vmcs->vmcs)
  5499. goto free_msrs;
  5500. if (!vmm_exclusive)
  5501. kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
  5502. loaded_vmcs_init(vmx->loaded_vmcs);
  5503. if (!vmm_exclusive)
  5504. kvm_cpu_vmxoff();
  5505. cpu = get_cpu();
  5506. vmx_vcpu_load(&vmx->vcpu, cpu);
  5507. vmx->vcpu.cpu = cpu;
  5508. err = vmx_vcpu_setup(vmx);
  5509. vmx_vcpu_put(&vmx->vcpu);
  5510. put_cpu();
  5511. if (err)
  5512. goto free_vmcs;
  5513. if (vm_need_virtualize_apic_accesses(kvm))
  5514. err = alloc_apic_access_page(kvm);
  5515. if (err)
  5516. goto free_vmcs;
  5517. if (enable_ept) {
  5518. if (!kvm->arch.ept_identity_map_addr)
  5519. kvm->arch.ept_identity_map_addr =
  5520. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  5521. err = -ENOMEM;
  5522. if (alloc_identity_pagetable(kvm) != 0)
  5523. goto free_vmcs;
  5524. if (!init_rmode_identity_map(kvm))
  5525. goto free_vmcs;
  5526. }
  5527. vmx->nested.current_vmptr = -1ull;
  5528. vmx->nested.current_vmcs12 = NULL;
  5529. return &vmx->vcpu;
  5530. free_vmcs:
  5531. free_vmcs(vmx->loaded_vmcs->vmcs);
  5532. free_msrs:
  5533. kfree(vmx->guest_msrs);
  5534. uninit_vcpu:
  5535. kvm_vcpu_uninit(&vmx->vcpu);
  5536. free_vcpu:
  5537. free_vpid(vmx);
  5538. kmem_cache_free(kvm_vcpu_cache, vmx);
  5539. return ERR_PTR(err);
  5540. }
  5541. static void __init vmx_check_processor_compat(void *rtn)
  5542. {
  5543. struct vmcs_config vmcs_conf;
  5544. *(int *)rtn = 0;
  5545. if (setup_vmcs_config(&vmcs_conf) < 0)
  5546. *(int *)rtn = -EIO;
  5547. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  5548. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  5549. smp_processor_id());
  5550. *(int *)rtn = -EIO;
  5551. }
  5552. }
  5553. static int get_ept_level(void)
  5554. {
  5555. return VMX_EPT_DEFAULT_GAW + 1;
  5556. }
  5557. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  5558. {
  5559. u64 ret;
  5560. /* For VT-d and EPT combination
  5561. * 1. MMIO: always map as UC
  5562. * 2. EPT with VT-d:
  5563. * a. VT-d without snooping control feature: can't guarantee the
  5564. * result, try to trust guest.
  5565. * b. VT-d with snooping control feature: snooping control feature of
  5566. * VT-d engine can guarantee the cache correctness. Just set it
  5567. * to WB to keep consistent with host. So the same as item 3.
  5568. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  5569. * consistent with host MTRR
  5570. */
  5571. if (is_mmio)
  5572. ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
  5573. else if (vcpu->kvm->arch.iommu_domain &&
  5574. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
  5575. ret = kvm_get_guest_memory_type(vcpu, gfn) <<
  5576. VMX_EPT_MT_EPTE_SHIFT;
  5577. else
  5578. ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
  5579. | VMX_EPT_IPAT_BIT;
  5580. return ret;
  5581. }
  5582. static int vmx_get_lpage_level(void)
  5583. {
  5584. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  5585. return PT_DIRECTORY_LEVEL;
  5586. else
  5587. /* For shadow and EPT supported 1GB page */
  5588. return PT_PDPE_LEVEL;
  5589. }
  5590. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  5591. {
  5592. struct kvm_cpuid_entry2 *best;
  5593. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5594. u32 exec_control;
  5595. vmx->rdtscp_enabled = false;
  5596. if (vmx_rdtscp_supported()) {
  5597. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  5598. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  5599. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  5600. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  5601. vmx->rdtscp_enabled = true;
  5602. else {
  5603. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5604. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5605. exec_control);
  5606. }
  5607. }
  5608. }
  5609. }
  5610. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  5611. {
  5612. if (func == 1 && nested)
  5613. entry->ecx |= bit(X86_FEATURE_VMX);
  5614. }
  5615. /*
  5616. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  5617. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  5618. * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
  5619. * guest in a way that will both be appropriate to L1's requests, and our
  5620. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  5621. * function also has additional necessary side-effects, like setting various
  5622. * vcpu->arch fields.
  5623. */
  5624. static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  5625. {
  5626. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5627. u32 exec_control;
  5628. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  5629. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  5630. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  5631. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  5632. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  5633. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  5634. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  5635. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  5636. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  5637. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  5638. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  5639. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  5640. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  5641. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  5642. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  5643. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  5644. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  5645. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  5646. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  5647. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  5648. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  5649. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  5650. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  5651. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  5652. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  5653. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  5654. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  5655. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  5656. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  5657. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  5658. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  5659. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  5660. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  5661. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  5662. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  5663. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  5664. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  5665. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5666. vmcs12->vm_entry_intr_info_field);
  5667. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  5668. vmcs12->vm_entry_exception_error_code);
  5669. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5670. vmcs12->vm_entry_instruction_len);
  5671. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  5672. vmcs12->guest_interruptibility_info);
  5673. vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state);
  5674. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  5675. vmcs_writel(GUEST_DR7, vmcs12->guest_dr7);
  5676. vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags);
  5677. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  5678. vmcs12->guest_pending_dbg_exceptions);
  5679. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  5680. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  5681. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  5682. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  5683. (vmcs_config.pin_based_exec_ctrl |
  5684. vmcs12->pin_based_vm_exec_control));
  5685. /*
  5686. * Whether page-faults are trapped is determined by a combination of
  5687. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  5688. * If enable_ept, L0 doesn't care about page faults and we should
  5689. * set all of these to L1's desires. However, if !enable_ept, L0 does
  5690. * care about (at least some) page faults, and because it is not easy
  5691. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  5692. * to exit on each and every L2 page fault. This is done by setting
  5693. * MASK=MATCH=0 and (see below) EB.PF=1.
  5694. * Note that below we don't need special code to set EB.PF beyond the
  5695. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  5696. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  5697. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  5698. *
  5699. * A problem with this approach (when !enable_ept) is that L1 may be
  5700. * injected with more page faults than it asked for. This could have
  5701. * caused problems, but in practice existing hypervisors don't care.
  5702. * To fix this, we will need to emulate the PFEC checking (on the L1
  5703. * page tables), using walk_addr(), when injecting PFs to L1.
  5704. */
  5705. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  5706. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  5707. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  5708. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  5709. if (cpu_has_secondary_exec_ctrls()) {
  5710. u32 exec_control = vmx_secondary_exec_control(vmx);
  5711. if (!vmx->rdtscp_enabled)
  5712. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5713. /* Take the following fields only from vmcs12 */
  5714. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5715. if (nested_cpu_has(vmcs12,
  5716. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
  5717. exec_control |= vmcs12->secondary_vm_exec_control;
  5718. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
  5719. /*
  5720. * Translate L1 physical address to host physical
  5721. * address for vmcs02. Keep the page pinned, so this
  5722. * physical address remains valid. We keep a reference
  5723. * to it so we can release it later.
  5724. */
  5725. if (vmx->nested.apic_access_page) /* shouldn't happen */
  5726. nested_release_page(vmx->nested.apic_access_page);
  5727. vmx->nested.apic_access_page =
  5728. nested_get_page(vcpu, vmcs12->apic_access_addr);
  5729. /*
  5730. * If translation failed, no matter: This feature asks
  5731. * to exit when accessing the given address, and if it
  5732. * can never be accessed, this feature won't do
  5733. * anything anyway.
  5734. */
  5735. if (!vmx->nested.apic_access_page)
  5736. exec_control &=
  5737. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5738. else
  5739. vmcs_write64(APIC_ACCESS_ADDR,
  5740. page_to_phys(vmx->nested.apic_access_page));
  5741. }
  5742. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  5743. }
  5744. /*
  5745. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  5746. * Some constant fields are set here by vmx_set_constant_host_state().
  5747. * Other fields are different per CPU, and will be set later when
  5748. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  5749. */
  5750. vmx_set_constant_host_state();
  5751. /*
  5752. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  5753. * entry, but only if the current (host) sp changed from the value
  5754. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  5755. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  5756. * here we just force the write to happen on entry.
  5757. */
  5758. vmx->host_rsp = 0;
  5759. exec_control = vmx_exec_control(vmx); /* L0's desires */
  5760. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  5761. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  5762. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5763. exec_control |= vmcs12->cpu_based_vm_exec_control;
  5764. /*
  5765. * Merging of IO and MSR bitmaps not currently supported.
  5766. * Rather, exit every time.
  5767. */
  5768. exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
  5769. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  5770. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  5771. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  5772. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  5773. * bitwise-or of what L1 wants to trap for L2, and what we want to
  5774. * trap. Note that CR0.TS also needs updating - we do this later.
  5775. */
  5776. update_exception_bitmap(vcpu);
  5777. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  5778. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  5779. /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */
  5780. vmcs_write32(VM_EXIT_CONTROLS,
  5781. vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl);
  5782. vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls |
  5783. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  5784. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)
  5785. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  5786. else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5787. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5788. set_cr4_guest_host_mask(vmx);
  5789. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  5790. vmcs_write64(TSC_OFFSET,
  5791. vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
  5792. else
  5793. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  5794. if (enable_vpid) {
  5795. /*
  5796. * Trivially support vpid by letting L2s share their parent
  5797. * L1's vpid. TODO: move to a more elaborate solution, giving
  5798. * each L2 its own vpid and exposing the vpid feature to L1.
  5799. */
  5800. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5801. vmx_flush_tlb(vcpu);
  5802. }
  5803. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
  5804. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  5805. if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  5806. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  5807. else
  5808. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  5809. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  5810. vmx_set_efer(vcpu, vcpu->arch.efer);
  5811. /*
  5812. * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
  5813. * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
  5814. * The CR0_READ_SHADOW is what L2 should have expected to read given
  5815. * the specifications by L1; It's not enough to take
  5816. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  5817. * have more bits than L1 expected.
  5818. */
  5819. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  5820. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  5821. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  5822. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  5823. /* shadow page tables on either EPT or shadow page tables */
  5824. kvm_set_cr3(vcpu, vmcs12->guest_cr3);
  5825. kvm_mmu_reset_context(vcpu);
  5826. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  5827. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  5828. }
  5829. /*
  5830. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  5831. * for running an L2 nested guest.
  5832. */
  5833. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  5834. {
  5835. struct vmcs12 *vmcs12;
  5836. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5837. int cpu;
  5838. struct loaded_vmcs *vmcs02;
  5839. if (!nested_vmx_check_permission(vcpu) ||
  5840. !nested_vmx_check_vmcs12(vcpu))
  5841. return 1;
  5842. skip_emulated_instruction(vcpu);
  5843. vmcs12 = get_vmcs12(vcpu);
  5844. /*
  5845. * The nested entry process starts with enforcing various prerequisites
  5846. * on vmcs12 as required by the Intel SDM, and act appropriately when
  5847. * they fail: As the SDM explains, some conditions should cause the
  5848. * instruction to fail, while others will cause the instruction to seem
  5849. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  5850. * To speed up the normal (success) code path, we should avoid checking
  5851. * for misconfigurations which will anyway be caught by the processor
  5852. * when using the merged vmcs02.
  5853. */
  5854. if (vmcs12->launch_state == launch) {
  5855. nested_vmx_failValid(vcpu,
  5856. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  5857. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  5858. return 1;
  5859. }
  5860. if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
  5861. !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
  5862. /*TODO: Also verify bits beyond physical address width are 0*/
  5863. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  5864. return 1;
  5865. }
  5866. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  5867. !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
  5868. /*TODO: Also verify bits beyond physical address width are 0*/
  5869. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  5870. return 1;
  5871. }
  5872. if (vmcs12->vm_entry_msr_load_count > 0 ||
  5873. vmcs12->vm_exit_msr_load_count > 0 ||
  5874. vmcs12->vm_exit_msr_store_count > 0) {
  5875. pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
  5876. __func__);
  5877. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  5878. return 1;
  5879. }
  5880. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  5881. nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
  5882. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  5883. nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
  5884. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  5885. nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
  5886. !vmx_control_verify(vmcs12->vm_exit_controls,
  5887. nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
  5888. !vmx_control_verify(vmcs12->vm_entry_controls,
  5889. nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
  5890. {
  5891. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  5892. return 1;
  5893. }
  5894. if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
  5895. ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  5896. nested_vmx_failValid(vcpu,
  5897. VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
  5898. return 1;
  5899. }
  5900. if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
  5901. ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  5902. nested_vmx_entry_failure(vcpu, vmcs12,
  5903. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  5904. return 1;
  5905. }
  5906. if (vmcs12->vmcs_link_pointer != -1ull) {
  5907. nested_vmx_entry_failure(vcpu, vmcs12,
  5908. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
  5909. return 1;
  5910. }
  5911. /*
  5912. * We're finally done with prerequisite checking, and can start with
  5913. * the nested entry.
  5914. */
  5915. vmcs02 = nested_get_current_vmcs02(vmx);
  5916. if (!vmcs02)
  5917. return -ENOMEM;
  5918. enter_guest_mode(vcpu);
  5919. vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
  5920. cpu = get_cpu();
  5921. vmx->loaded_vmcs = vmcs02;
  5922. vmx_vcpu_put(vcpu);
  5923. vmx_vcpu_load(vcpu, cpu);
  5924. vcpu->cpu = cpu;
  5925. put_cpu();
  5926. vmcs12->launch_state = 1;
  5927. prepare_vmcs02(vcpu, vmcs12);
  5928. /*
  5929. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  5930. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  5931. * returned as far as L1 is concerned. It will only return (and set
  5932. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  5933. */
  5934. return 1;
  5935. }
  5936. /*
  5937. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  5938. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  5939. * This function returns the new value we should put in vmcs12.guest_cr0.
  5940. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  5941. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  5942. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  5943. * didn't trap the bit, because if L1 did, so would L0).
  5944. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  5945. * been modified by L2, and L1 knows it. So just leave the old value of
  5946. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  5947. * isn't relevant, because if L0 traps this bit it can set it to anything.
  5948. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  5949. * changed these bits, and therefore they need to be updated, but L0
  5950. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  5951. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  5952. */
  5953. static inline unsigned long
  5954. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  5955. {
  5956. return
  5957. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  5958. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  5959. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  5960. vcpu->arch.cr0_guest_owned_bits));
  5961. }
  5962. static inline unsigned long
  5963. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  5964. {
  5965. return
  5966. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  5967. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  5968. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  5969. vcpu->arch.cr4_guest_owned_bits));
  5970. }
  5971. /*
  5972. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  5973. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  5974. * and this function updates it to reflect the changes to the guest state while
  5975. * L2 was running (and perhaps made some exits which were handled directly by L0
  5976. * without going back to L1), and to reflect the exit reason.
  5977. * Note that we do not have to copy here all VMCS fields, just those that
  5978. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  5979. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  5980. * which already writes to vmcs12 directly.
  5981. */
  5982. void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  5983. {
  5984. /* update guest state fields: */
  5985. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  5986. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  5987. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  5988. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  5989. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  5990. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  5991. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  5992. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  5993. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  5994. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  5995. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  5996. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  5997. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  5998. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  5999. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  6000. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  6001. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  6002. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  6003. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  6004. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  6005. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  6006. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  6007. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  6008. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  6009. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  6010. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  6011. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  6012. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  6013. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  6014. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  6015. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  6016. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  6017. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  6018. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  6019. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  6020. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  6021. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  6022. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  6023. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  6024. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  6025. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  6026. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  6027. vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
  6028. vmcs12->guest_interruptibility_info =
  6029. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  6030. vmcs12->guest_pending_dbg_exceptions =
  6031. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  6032. /* TODO: These cannot have changed unless we have MSR bitmaps and
  6033. * the relevant bit asks not to trap the change */
  6034. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  6035. if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT)
  6036. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  6037. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  6038. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  6039. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  6040. /* update exit information fields: */
  6041. vmcs12->vm_exit_reason = vmcs_read32(VM_EXIT_REASON);
  6042. vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6043. vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6044. vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  6045. vmcs12->idt_vectoring_info_field =
  6046. vmcs_read32(IDT_VECTORING_INFO_FIELD);
  6047. vmcs12->idt_vectoring_error_code =
  6048. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6049. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  6050. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6051. /* clear vm-entry fields which are to be cleared on exit */
  6052. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  6053. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  6054. }
  6055. /*
  6056. * A part of what we need to when the nested L2 guest exits and we want to
  6057. * run its L1 parent, is to reset L1's guest state to the host state specified
  6058. * in vmcs12.
  6059. * This function is to be called not only on normal nested exit, but also on
  6060. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  6061. * Failures During or After Loading Guest State").
  6062. * This function should be called when the active VMCS is L1's (vmcs01).
  6063. */
  6064. void load_vmcs12_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  6065. {
  6066. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  6067. vcpu->arch.efer = vmcs12->host_ia32_efer;
  6068. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  6069. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  6070. else
  6071. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  6072. vmx_set_efer(vcpu, vcpu->arch.efer);
  6073. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  6074. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  6075. /*
  6076. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  6077. * actually changed, because it depends on the current state of
  6078. * fpu_active (which may have changed).
  6079. * Note that vmx_set_cr0 refers to efer set above.
  6080. */
  6081. kvm_set_cr0(vcpu, vmcs12->host_cr0);
  6082. /*
  6083. * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
  6084. * to apply the same changes to L1's vmcs. We just set cr0 correctly,
  6085. * but we also need to update cr0_guest_host_mask and exception_bitmap.
  6086. */
  6087. update_exception_bitmap(vcpu);
  6088. vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
  6089. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  6090. /*
  6091. * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
  6092. * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
  6093. */
  6094. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  6095. kvm_set_cr4(vcpu, vmcs12->host_cr4);
  6096. /* shadow page tables on either EPT or shadow page tables */
  6097. kvm_set_cr3(vcpu, vmcs12->host_cr3);
  6098. kvm_mmu_reset_context(vcpu);
  6099. if (enable_vpid) {
  6100. /*
  6101. * Trivially support vpid by letting L2s share their parent
  6102. * L1's vpid. TODO: move to a more elaborate solution, giving
  6103. * each L2 its own vpid and exposing the vpid feature to L1.
  6104. */
  6105. vmx_flush_tlb(vcpu);
  6106. }
  6107. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  6108. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  6109. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  6110. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  6111. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  6112. vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base);
  6113. vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base);
  6114. vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base);
  6115. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector);
  6116. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector);
  6117. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector);
  6118. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector);
  6119. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector);
  6120. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector);
  6121. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector);
  6122. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT)
  6123. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  6124. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  6125. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  6126. vmcs12->host_ia32_perf_global_ctrl);
  6127. }
  6128. /*
  6129. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  6130. * and modify vmcs12 to make it see what it would expect to see there if
  6131. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  6132. */
  6133. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)
  6134. {
  6135. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6136. int cpu;
  6137. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6138. leave_guest_mode(vcpu);
  6139. prepare_vmcs12(vcpu, vmcs12);
  6140. cpu = get_cpu();
  6141. vmx->loaded_vmcs = &vmx->vmcs01;
  6142. vmx_vcpu_put(vcpu);
  6143. vmx_vcpu_load(vcpu, cpu);
  6144. vcpu->cpu = cpu;
  6145. put_cpu();
  6146. /* if no vmcs02 cache requested, remove the one we used */
  6147. if (VMCS02_POOL_SIZE == 0)
  6148. nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
  6149. load_vmcs12_host_state(vcpu, vmcs12);
  6150. /* Update TSC_OFFSET if TSC was changed while L2 ran */
  6151. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  6152. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  6153. vmx->host_rsp = 0;
  6154. /* Unpin physical memory we referred to in vmcs02 */
  6155. if (vmx->nested.apic_access_page) {
  6156. nested_release_page(vmx->nested.apic_access_page);
  6157. vmx->nested.apic_access_page = 0;
  6158. }
  6159. /*
  6160. * Exiting from L2 to L1, we're now back to L1 which thinks it just
  6161. * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
  6162. * success or failure flag accordingly.
  6163. */
  6164. if (unlikely(vmx->fail)) {
  6165. vmx->fail = 0;
  6166. nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
  6167. } else
  6168. nested_vmx_succeed(vcpu);
  6169. }
  6170. /*
  6171. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  6172. * 23.7 "VM-entry failures during or after loading guest state" (this also
  6173. * lists the acceptable exit-reason and exit-qualification parameters).
  6174. * It should only be called before L2 actually succeeded to run, and when
  6175. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  6176. */
  6177. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  6178. struct vmcs12 *vmcs12,
  6179. u32 reason, unsigned long qualification)
  6180. {
  6181. load_vmcs12_host_state(vcpu, vmcs12);
  6182. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  6183. vmcs12->exit_qualification = qualification;
  6184. nested_vmx_succeed(vcpu);
  6185. }
  6186. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  6187. struct x86_instruction_info *info,
  6188. enum x86_intercept_stage stage)
  6189. {
  6190. return X86EMUL_CONTINUE;
  6191. }
  6192. static struct kvm_x86_ops vmx_x86_ops = {
  6193. .cpu_has_kvm_support = cpu_has_kvm_support,
  6194. .disabled_by_bios = vmx_disabled_by_bios,
  6195. .hardware_setup = hardware_setup,
  6196. .hardware_unsetup = hardware_unsetup,
  6197. .check_processor_compatibility = vmx_check_processor_compat,
  6198. .hardware_enable = hardware_enable,
  6199. .hardware_disable = hardware_disable,
  6200. .cpu_has_accelerated_tpr = report_flexpriority,
  6201. .vcpu_create = vmx_create_vcpu,
  6202. .vcpu_free = vmx_free_vcpu,
  6203. .vcpu_reset = vmx_vcpu_reset,
  6204. .prepare_guest_switch = vmx_save_host_state,
  6205. .vcpu_load = vmx_vcpu_load,
  6206. .vcpu_put = vmx_vcpu_put,
  6207. .set_guest_debug = set_guest_debug,
  6208. .get_msr = vmx_get_msr,
  6209. .set_msr = vmx_set_msr,
  6210. .get_segment_base = vmx_get_segment_base,
  6211. .get_segment = vmx_get_segment,
  6212. .set_segment = vmx_set_segment,
  6213. .get_cpl = vmx_get_cpl,
  6214. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  6215. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  6216. .decache_cr3 = vmx_decache_cr3,
  6217. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  6218. .set_cr0 = vmx_set_cr0,
  6219. .set_cr3 = vmx_set_cr3,
  6220. .set_cr4 = vmx_set_cr4,
  6221. .set_efer = vmx_set_efer,
  6222. .get_idt = vmx_get_idt,
  6223. .set_idt = vmx_set_idt,
  6224. .get_gdt = vmx_get_gdt,
  6225. .set_gdt = vmx_set_gdt,
  6226. .set_dr7 = vmx_set_dr7,
  6227. .cache_reg = vmx_cache_reg,
  6228. .get_rflags = vmx_get_rflags,
  6229. .set_rflags = vmx_set_rflags,
  6230. .fpu_activate = vmx_fpu_activate,
  6231. .fpu_deactivate = vmx_fpu_deactivate,
  6232. .tlb_flush = vmx_flush_tlb,
  6233. .run = vmx_vcpu_run,
  6234. .handle_exit = vmx_handle_exit,
  6235. .skip_emulated_instruction = skip_emulated_instruction,
  6236. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  6237. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  6238. .patch_hypercall = vmx_patch_hypercall,
  6239. .set_irq = vmx_inject_irq,
  6240. .set_nmi = vmx_inject_nmi,
  6241. .queue_exception = vmx_queue_exception,
  6242. .cancel_injection = vmx_cancel_injection,
  6243. .interrupt_allowed = vmx_interrupt_allowed,
  6244. .nmi_allowed = vmx_nmi_allowed,
  6245. .get_nmi_mask = vmx_get_nmi_mask,
  6246. .set_nmi_mask = vmx_set_nmi_mask,
  6247. .enable_nmi_window = enable_nmi_window,
  6248. .enable_irq_window = enable_irq_window,
  6249. .update_cr8_intercept = update_cr8_intercept,
  6250. .set_tss_addr = vmx_set_tss_addr,
  6251. .get_tdp_level = get_ept_level,
  6252. .get_mt_mask = vmx_get_mt_mask,
  6253. .get_exit_info = vmx_get_exit_info,
  6254. .get_lpage_level = vmx_get_lpage_level,
  6255. .cpuid_update = vmx_cpuid_update,
  6256. .rdtscp_supported = vmx_rdtscp_supported,
  6257. .set_supported_cpuid = vmx_set_supported_cpuid,
  6258. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  6259. .set_tsc_khz = vmx_set_tsc_khz,
  6260. .write_tsc_offset = vmx_write_tsc_offset,
  6261. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  6262. .compute_tsc_offset = vmx_compute_tsc_offset,
  6263. .read_l1_tsc = vmx_read_l1_tsc,
  6264. .set_tdp_cr3 = vmx_set_cr3,
  6265. .check_intercept = vmx_check_intercept,
  6266. };
  6267. static int __init vmx_init(void)
  6268. {
  6269. int r, i;
  6270. rdmsrl_safe(MSR_EFER, &host_efer);
  6271. for (i = 0; i < NR_VMX_MSR; ++i)
  6272. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6273. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  6274. if (!vmx_io_bitmap_a)
  6275. return -ENOMEM;
  6276. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  6277. if (!vmx_io_bitmap_b) {
  6278. r = -ENOMEM;
  6279. goto out;
  6280. }
  6281. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  6282. if (!vmx_msr_bitmap_legacy) {
  6283. r = -ENOMEM;
  6284. goto out1;
  6285. }
  6286. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  6287. if (!vmx_msr_bitmap_longmode) {
  6288. r = -ENOMEM;
  6289. goto out2;
  6290. }
  6291. /*
  6292. * Allow direct access to the PC debug port (it is often used for I/O
  6293. * delays, but the vmexits simply slow things down).
  6294. */
  6295. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  6296. clear_bit(0x80, vmx_io_bitmap_a);
  6297. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  6298. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  6299. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  6300. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6301. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  6302. __alignof__(struct vcpu_vmx), THIS_MODULE);
  6303. if (r)
  6304. goto out3;
  6305. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  6306. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  6307. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  6308. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  6309. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  6310. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  6311. if (enable_ept) {
  6312. kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
  6313. VMX_EPT_EXECUTABLE_MASK);
  6314. ept_set_mmio_spte_mask();
  6315. kvm_enable_tdp();
  6316. } else
  6317. kvm_disable_tdp();
  6318. return 0;
  6319. out3:
  6320. free_page((unsigned long)vmx_msr_bitmap_longmode);
  6321. out2:
  6322. free_page((unsigned long)vmx_msr_bitmap_legacy);
  6323. out1:
  6324. free_page((unsigned long)vmx_io_bitmap_b);
  6325. out:
  6326. free_page((unsigned long)vmx_io_bitmap_a);
  6327. return r;
  6328. }
  6329. static void __exit vmx_exit(void)
  6330. {
  6331. free_page((unsigned long)vmx_msr_bitmap_legacy);
  6332. free_page((unsigned long)vmx_msr_bitmap_longmode);
  6333. free_page((unsigned long)vmx_io_bitmap_b);
  6334. free_page((unsigned long)vmx_io_bitmap_a);
  6335. kvm_exit();
  6336. }
  6337. module_init(vmx_init)
  6338. module_exit(vmx_exit)