vmx.c 206 KB

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