vmx.c 295 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. */
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include "cpuid.h"
  21. #include <linux/kvm_host.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/highmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/trace_events.h>
  30. #include <linux/slab.h>
  31. #include <linux/tboot.h>
  32. #include <linux/hrtimer.h>
  33. #include "kvm_cache_regs.h"
  34. #include "x86.h"
  35. #include <asm/io.h>
  36. #include <asm/desc.h>
  37. #include <asm/vmx.h>
  38. #include <asm/virtext.h>
  39. #include <asm/mce.h>
  40. #include <asm/fpu/internal.h>
  41. #include <asm/perf_event.h>
  42. #include <asm/debugreg.h>
  43. #include <asm/kexec.h>
  44. #include <asm/apic.h>
  45. #include "trace.h"
  46. #include "pmu.h"
  47. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  48. #define __ex_clear(x, reg) \
  49. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  50. MODULE_AUTHOR("Qumranet");
  51. MODULE_LICENSE("GPL");
  52. static const struct x86_cpu_id vmx_cpu_id[] = {
  53. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  54. {}
  55. };
  56. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  57. static bool __read_mostly enable_vpid = 1;
  58. module_param_named(vpid, enable_vpid, bool, 0444);
  59. static bool __read_mostly flexpriority_enabled = 1;
  60. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  61. static bool __read_mostly enable_ept = 1;
  62. module_param_named(ept, enable_ept, bool, S_IRUGO);
  63. static bool __read_mostly enable_unrestricted_guest = 1;
  64. module_param_named(unrestricted_guest,
  65. enable_unrestricted_guest, bool, S_IRUGO);
  66. static bool __read_mostly enable_ept_ad_bits = 1;
  67. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  68. static bool __read_mostly emulate_invalid_guest_state = true;
  69. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  70. static bool __read_mostly vmm_exclusive = 1;
  71. module_param(vmm_exclusive, bool, S_IRUGO);
  72. static bool __read_mostly fasteoi = 1;
  73. module_param(fasteoi, bool, S_IRUGO);
  74. static bool __read_mostly enable_apicv = 1;
  75. module_param(enable_apicv, bool, S_IRUGO);
  76. static bool __read_mostly enable_shadow_vmcs = 1;
  77. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  78. /*
  79. * If nested=1, nested virtualization is supported, i.e., guests may use
  80. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  81. * use VMX instructions.
  82. */
  83. static bool __read_mostly nested = 0;
  84. module_param(nested, bool, S_IRUGO);
  85. static u64 __read_mostly host_xss;
  86. static bool __read_mostly enable_pml = 1;
  87. module_param_named(pml, enable_pml, bool, S_IRUGO);
  88. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  89. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
  90. #define KVM_VM_CR0_ALWAYS_ON \
  91. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  92. #define KVM_CR4_GUEST_OWNED_BITS \
  93. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  94. | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
  95. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  96. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  97. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  98. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  99. /*
  100. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  101. * ple_gap: upper bound on the amount of time between two successive
  102. * executions of PAUSE in a loop. Also indicate if ple enabled.
  103. * According to test, this time is usually smaller than 128 cycles.
  104. * ple_window: upper bound on the amount of time a guest is allowed to execute
  105. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  106. * less than 2^12 cycles
  107. * Time is measured based on a counter that runs at the same rate as the TSC,
  108. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  109. */
  110. #define KVM_VMX_DEFAULT_PLE_GAP 128
  111. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  112. #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
  113. #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
  114. #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
  115. INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
  116. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  117. module_param(ple_gap, int, S_IRUGO);
  118. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  119. module_param(ple_window, int, S_IRUGO);
  120. /* Default doubles per-vcpu window every exit. */
  121. static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
  122. module_param(ple_window_grow, int, S_IRUGO);
  123. /* Default resets per-vcpu window every exit to ple_window. */
  124. static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
  125. module_param(ple_window_shrink, int, S_IRUGO);
  126. /* Default is to compute the maximum so we can never overflow. */
  127. static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  128. static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  129. module_param(ple_window_max, int, S_IRUGO);
  130. extern const ulong vmx_return;
  131. #define NR_AUTOLOAD_MSRS 8
  132. #define VMCS02_POOL_SIZE 1
  133. struct vmcs {
  134. u32 revision_id;
  135. u32 abort;
  136. char data[0];
  137. };
  138. /*
  139. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  140. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  141. * loaded on this CPU (so we can clear them if the CPU goes down).
  142. */
  143. struct loaded_vmcs {
  144. struct vmcs *vmcs;
  145. int cpu;
  146. int launched;
  147. struct list_head loaded_vmcss_on_cpu_link;
  148. };
  149. struct shared_msr_entry {
  150. unsigned index;
  151. u64 data;
  152. u64 mask;
  153. };
  154. /*
  155. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  156. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  157. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  158. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  159. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  160. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  161. * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
  162. * underlying hardware which will be used to run L2.
  163. * This structure is packed to ensure that its layout is identical across
  164. * machines (necessary for live migration).
  165. * If there are changes in this struct, VMCS12_REVISION must be changed.
  166. */
  167. typedef u64 natural_width;
  168. struct __packed vmcs12 {
  169. /* According to the Intel spec, a VMCS region must start with the
  170. * following two fields. Then follow implementation-specific data.
  171. */
  172. u32 revision_id;
  173. u32 abort;
  174. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  175. u32 padding[7]; /* room for future expansion */
  176. u64 io_bitmap_a;
  177. u64 io_bitmap_b;
  178. u64 msr_bitmap;
  179. u64 vm_exit_msr_store_addr;
  180. u64 vm_exit_msr_load_addr;
  181. u64 vm_entry_msr_load_addr;
  182. u64 tsc_offset;
  183. u64 virtual_apic_page_addr;
  184. u64 apic_access_addr;
  185. u64 posted_intr_desc_addr;
  186. u64 ept_pointer;
  187. u64 eoi_exit_bitmap0;
  188. u64 eoi_exit_bitmap1;
  189. u64 eoi_exit_bitmap2;
  190. u64 eoi_exit_bitmap3;
  191. u64 xss_exit_bitmap;
  192. u64 guest_physical_address;
  193. u64 vmcs_link_pointer;
  194. u64 guest_ia32_debugctl;
  195. u64 guest_ia32_pat;
  196. u64 guest_ia32_efer;
  197. u64 guest_ia32_perf_global_ctrl;
  198. u64 guest_pdptr0;
  199. u64 guest_pdptr1;
  200. u64 guest_pdptr2;
  201. u64 guest_pdptr3;
  202. u64 guest_bndcfgs;
  203. u64 host_ia32_pat;
  204. u64 host_ia32_efer;
  205. u64 host_ia32_perf_global_ctrl;
  206. u64 padding64[8]; /* room for future expansion */
  207. /*
  208. * To allow migration of L1 (complete with its L2 guests) between
  209. * machines of different natural widths (32 or 64 bit), we cannot have
  210. * unsigned long fields with no explict size. We use u64 (aliased
  211. * natural_width) instead. Luckily, x86 is little-endian.
  212. */
  213. natural_width cr0_guest_host_mask;
  214. natural_width cr4_guest_host_mask;
  215. natural_width cr0_read_shadow;
  216. natural_width cr4_read_shadow;
  217. natural_width cr3_target_value0;
  218. natural_width cr3_target_value1;
  219. natural_width cr3_target_value2;
  220. natural_width cr3_target_value3;
  221. natural_width exit_qualification;
  222. natural_width guest_linear_address;
  223. natural_width guest_cr0;
  224. natural_width guest_cr3;
  225. natural_width guest_cr4;
  226. natural_width guest_es_base;
  227. natural_width guest_cs_base;
  228. natural_width guest_ss_base;
  229. natural_width guest_ds_base;
  230. natural_width guest_fs_base;
  231. natural_width guest_gs_base;
  232. natural_width guest_ldtr_base;
  233. natural_width guest_tr_base;
  234. natural_width guest_gdtr_base;
  235. natural_width guest_idtr_base;
  236. natural_width guest_dr7;
  237. natural_width guest_rsp;
  238. natural_width guest_rip;
  239. natural_width guest_rflags;
  240. natural_width guest_pending_dbg_exceptions;
  241. natural_width guest_sysenter_esp;
  242. natural_width guest_sysenter_eip;
  243. natural_width host_cr0;
  244. natural_width host_cr3;
  245. natural_width host_cr4;
  246. natural_width host_fs_base;
  247. natural_width host_gs_base;
  248. natural_width host_tr_base;
  249. natural_width host_gdtr_base;
  250. natural_width host_idtr_base;
  251. natural_width host_ia32_sysenter_esp;
  252. natural_width host_ia32_sysenter_eip;
  253. natural_width host_rsp;
  254. natural_width host_rip;
  255. natural_width paddingl[8]; /* room for future expansion */
  256. u32 pin_based_vm_exec_control;
  257. u32 cpu_based_vm_exec_control;
  258. u32 exception_bitmap;
  259. u32 page_fault_error_code_mask;
  260. u32 page_fault_error_code_match;
  261. u32 cr3_target_count;
  262. u32 vm_exit_controls;
  263. u32 vm_exit_msr_store_count;
  264. u32 vm_exit_msr_load_count;
  265. u32 vm_entry_controls;
  266. u32 vm_entry_msr_load_count;
  267. u32 vm_entry_intr_info_field;
  268. u32 vm_entry_exception_error_code;
  269. u32 vm_entry_instruction_len;
  270. u32 tpr_threshold;
  271. u32 secondary_vm_exec_control;
  272. u32 vm_instruction_error;
  273. u32 vm_exit_reason;
  274. u32 vm_exit_intr_info;
  275. u32 vm_exit_intr_error_code;
  276. u32 idt_vectoring_info_field;
  277. u32 idt_vectoring_error_code;
  278. u32 vm_exit_instruction_len;
  279. u32 vmx_instruction_info;
  280. u32 guest_es_limit;
  281. u32 guest_cs_limit;
  282. u32 guest_ss_limit;
  283. u32 guest_ds_limit;
  284. u32 guest_fs_limit;
  285. u32 guest_gs_limit;
  286. u32 guest_ldtr_limit;
  287. u32 guest_tr_limit;
  288. u32 guest_gdtr_limit;
  289. u32 guest_idtr_limit;
  290. u32 guest_es_ar_bytes;
  291. u32 guest_cs_ar_bytes;
  292. u32 guest_ss_ar_bytes;
  293. u32 guest_ds_ar_bytes;
  294. u32 guest_fs_ar_bytes;
  295. u32 guest_gs_ar_bytes;
  296. u32 guest_ldtr_ar_bytes;
  297. u32 guest_tr_ar_bytes;
  298. u32 guest_interruptibility_info;
  299. u32 guest_activity_state;
  300. u32 guest_sysenter_cs;
  301. u32 host_ia32_sysenter_cs;
  302. u32 vmx_preemption_timer_value;
  303. u32 padding32[7]; /* room for future expansion */
  304. u16 virtual_processor_id;
  305. u16 posted_intr_nv;
  306. u16 guest_es_selector;
  307. u16 guest_cs_selector;
  308. u16 guest_ss_selector;
  309. u16 guest_ds_selector;
  310. u16 guest_fs_selector;
  311. u16 guest_gs_selector;
  312. u16 guest_ldtr_selector;
  313. u16 guest_tr_selector;
  314. u16 guest_intr_status;
  315. u16 host_es_selector;
  316. u16 host_cs_selector;
  317. u16 host_ss_selector;
  318. u16 host_ds_selector;
  319. u16 host_fs_selector;
  320. u16 host_gs_selector;
  321. u16 host_tr_selector;
  322. };
  323. /*
  324. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  325. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  326. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  327. */
  328. #define VMCS12_REVISION 0x11e57ed0
  329. /*
  330. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  331. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  332. * current implementation, 4K are reserved to avoid future complications.
  333. */
  334. #define VMCS12_SIZE 0x1000
  335. /* Used to remember the last vmcs02 used for some recently used vmcs12s */
  336. struct vmcs02_list {
  337. struct list_head list;
  338. gpa_t vmptr;
  339. struct loaded_vmcs vmcs02;
  340. };
  341. /*
  342. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  343. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  344. */
  345. struct nested_vmx {
  346. /* Has the level1 guest done vmxon? */
  347. bool vmxon;
  348. gpa_t vmxon_ptr;
  349. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  350. gpa_t current_vmptr;
  351. /* The host-usable pointer to the above */
  352. struct page *current_vmcs12_page;
  353. struct vmcs12 *current_vmcs12;
  354. struct vmcs *current_shadow_vmcs;
  355. /*
  356. * Indicates if the shadow vmcs must be updated with the
  357. * data hold by vmcs12
  358. */
  359. bool sync_shadow_vmcs;
  360. /* vmcs02_list cache of VMCSs recently used to run L2 guests */
  361. struct list_head vmcs02_pool;
  362. int vmcs02_num;
  363. u64 vmcs01_tsc_offset;
  364. /* L2 must run next, and mustn't decide to exit to L1. */
  365. bool nested_run_pending;
  366. /*
  367. * Guest pages referred to in vmcs02 with host-physical pointers, so
  368. * we must keep them pinned while L2 runs.
  369. */
  370. struct page *apic_access_page;
  371. struct page *virtual_apic_page;
  372. struct page *pi_desc_page;
  373. struct pi_desc *pi_desc;
  374. bool pi_pending;
  375. u16 posted_intr_nv;
  376. u64 msr_ia32_feature_control;
  377. struct hrtimer preemption_timer;
  378. bool preemption_timer_expired;
  379. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  380. u64 vmcs01_debugctl;
  381. u32 nested_vmx_procbased_ctls_low;
  382. u32 nested_vmx_procbased_ctls_high;
  383. u32 nested_vmx_true_procbased_ctls_low;
  384. u32 nested_vmx_secondary_ctls_low;
  385. u32 nested_vmx_secondary_ctls_high;
  386. u32 nested_vmx_pinbased_ctls_low;
  387. u32 nested_vmx_pinbased_ctls_high;
  388. u32 nested_vmx_exit_ctls_low;
  389. u32 nested_vmx_exit_ctls_high;
  390. u32 nested_vmx_true_exit_ctls_low;
  391. u32 nested_vmx_entry_ctls_low;
  392. u32 nested_vmx_entry_ctls_high;
  393. u32 nested_vmx_true_entry_ctls_low;
  394. u32 nested_vmx_misc_low;
  395. u32 nested_vmx_misc_high;
  396. u32 nested_vmx_ept_caps;
  397. };
  398. #define POSTED_INTR_ON 0
  399. /* Posted-Interrupt Descriptor */
  400. struct pi_desc {
  401. u32 pir[8]; /* Posted interrupt requested */
  402. u32 control; /* bit 0 of control is outstanding notification bit */
  403. u32 rsvd[7];
  404. } __aligned(64);
  405. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  406. {
  407. return test_and_set_bit(POSTED_INTR_ON,
  408. (unsigned long *)&pi_desc->control);
  409. }
  410. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  411. {
  412. return test_and_clear_bit(POSTED_INTR_ON,
  413. (unsigned long *)&pi_desc->control);
  414. }
  415. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  416. {
  417. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  418. }
  419. struct vcpu_vmx {
  420. struct kvm_vcpu vcpu;
  421. unsigned long host_rsp;
  422. u8 fail;
  423. bool nmi_known_unmasked;
  424. u32 exit_intr_info;
  425. u32 idt_vectoring_info;
  426. ulong rflags;
  427. struct shared_msr_entry *guest_msrs;
  428. int nmsrs;
  429. int save_nmsrs;
  430. unsigned long host_idt_base;
  431. #ifdef CONFIG_X86_64
  432. u64 msr_host_kernel_gs_base;
  433. u64 msr_guest_kernel_gs_base;
  434. #endif
  435. u32 vm_entry_controls_shadow;
  436. u32 vm_exit_controls_shadow;
  437. /*
  438. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  439. * non-nested (L1) guest, it always points to vmcs01. For a nested
  440. * guest (L2), it points to a different VMCS.
  441. */
  442. struct loaded_vmcs vmcs01;
  443. struct loaded_vmcs *loaded_vmcs;
  444. bool __launched; /* temporary, used in vmx_vcpu_run */
  445. struct msr_autoload {
  446. unsigned nr;
  447. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  448. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  449. } msr_autoload;
  450. struct {
  451. int loaded;
  452. u16 fs_sel, gs_sel, ldt_sel;
  453. #ifdef CONFIG_X86_64
  454. u16 ds_sel, es_sel;
  455. #endif
  456. int gs_ldt_reload_needed;
  457. int fs_reload_needed;
  458. u64 msr_host_bndcfgs;
  459. unsigned long vmcs_host_cr4; /* May not match real cr4 */
  460. } host_state;
  461. struct {
  462. int vm86_active;
  463. ulong save_rflags;
  464. struct kvm_segment segs[8];
  465. } rmode;
  466. struct {
  467. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  468. struct kvm_save_segment {
  469. u16 selector;
  470. unsigned long base;
  471. u32 limit;
  472. u32 ar;
  473. } seg[8];
  474. } segment_cache;
  475. int vpid;
  476. bool emulation_required;
  477. /* Support for vnmi-less CPUs */
  478. int soft_vnmi_blocked;
  479. ktime_t entry_time;
  480. s64 vnmi_blocked_time;
  481. u32 exit_reason;
  482. bool rdtscp_enabled;
  483. /* Posted interrupt descriptor */
  484. struct pi_desc pi_desc;
  485. /* Support for a guest hypervisor (nested VMX) */
  486. struct nested_vmx nested;
  487. /* Dynamic PLE window. */
  488. int ple_window;
  489. bool ple_window_dirty;
  490. /* Support for PML */
  491. #define PML_ENTITY_NUM 512
  492. struct page *pml_pg;
  493. };
  494. enum segment_cache_field {
  495. SEG_FIELD_SEL = 0,
  496. SEG_FIELD_BASE = 1,
  497. SEG_FIELD_LIMIT = 2,
  498. SEG_FIELD_AR = 3,
  499. SEG_FIELD_NR = 4
  500. };
  501. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  502. {
  503. return container_of(vcpu, struct vcpu_vmx, vcpu);
  504. }
  505. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  506. #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
  507. #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
  508. [number##_HIGH] = VMCS12_OFFSET(name)+4
  509. static unsigned long shadow_read_only_fields[] = {
  510. /*
  511. * We do NOT shadow fields that are modified when L0
  512. * traps and emulates any vmx instruction (e.g. VMPTRLD,
  513. * VMXON...) executed by L1.
  514. * For example, VM_INSTRUCTION_ERROR is read
  515. * by L1 if a vmx instruction fails (part of the error path).
  516. * Note the code assumes this logic. If for some reason
  517. * we start shadowing these fields then we need to
  518. * force a shadow sync when L0 emulates vmx instructions
  519. * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
  520. * by nested_vmx_failValid)
  521. */
  522. VM_EXIT_REASON,
  523. VM_EXIT_INTR_INFO,
  524. VM_EXIT_INSTRUCTION_LEN,
  525. IDT_VECTORING_INFO_FIELD,
  526. IDT_VECTORING_ERROR_CODE,
  527. VM_EXIT_INTR_ERROR_CODE,
  528. EXIT_QUALIFICATION,
  529. GUEST_LINEAR_ADDRESS,
  530. GUEST_PHYSICAL_ADDRESS
  531. };
  532. static int max_shadow_read_only_fields =
  533. ARRAY_SIZE(shadow_read_only_fields);
  534. static unsigned long shadow_read_write_fields[] = {
  535. TPR_THRESHOLD,
  536. GUEST_RIP,
  537. GUEST_RSP,
  538. GUEST_CR0,
  539. GUEST_CR3,
  540. GUEST_CR4,
  541. GUEST_INTERRUPTIBILITY_INFO,
  542. GUEST_RFLAGS,
  543. GUEST_CS_SELECTOR,
  544. GUEST_CS_AR_BYTES,
  545. GUEST_CS_LIMIT,
  546. GUEST_CS_BASE,
  547. GUEST_ES_BASE,
  548. GUEST_BNDCFGS,
  549. CR0_GUEST_HOST_MASK,
  550. CR0_READ_SHADOW,
  551. CR4_READ_SHADOW,
  552. TSC_OFFSET,
  553. EXCEPTION_BITMAP,
  554. CPU_BASED_VM_EXEC_CONTROL,
  555. VM_ENTRY_EXCEPTION_ERROR_CODE,
  556. VM_ENTRY_INTR_INFO_FIELD,
  557. VM_ENTRY_INSTRUCTION_LEN,
  558. VM_ENTRY_EXCEPTION_ERROR_CODE,
  559. HOST_FS_BASE,
  560. HOST_GS_BASE,
  561. HOST_FS_SELECTOR,
  562. HOST_GS_SELECTOR
  563. };
  564. static int max_shadow_read_write_fields =
  565. ARRAY_SIZE(shadow_read_write_fields);
  566. static const unsigned short vmcs_field_to_offset_table[] = {
  567. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  568. FIELD(POSTED_INTR_NV, posted_intr_nv),
  569. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  570. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  571. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  572. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  573. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  574. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  575. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  576. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  577. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  578. FIELD(HOST_ES_SELECTOR, host_es_selector),
  579. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  580. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  581. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  582. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  583. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  584. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  585. FIELD64(IO_BITMAP_A, io_bitmap_a),
  586. FIELD64(IO_BITMAP_B, io_bitmap_b),
  587. FIELD64(MSR_BITMAP, msr_bitmap),
  588. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  589. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  590. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  591. FIELD64(TSC_OFFSET, tsc_offset),
  592. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  593. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  594. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  595. FIELD64(EPT_POINTER, ept_pointer),
  596. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  597. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  598. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  599. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  600. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  601. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  602. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  603. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  604. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  605. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  606. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  607. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  608. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  609. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  610. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  611. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  612. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  613. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  614. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  615. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  616. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  617. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  618. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  619. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  620. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  621. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  622. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  623. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  624. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  625. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  626. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  627. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  628. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  629. FIELD(TPR_THRESHOLD, tpr_threshold),
  630. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  631. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  632. FIELD(VM_EXIT_REASON, vm_exit_reason),
  633. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  634. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  635. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  636. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  637. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  638. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  639. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  640. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  641. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  642. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  643. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  644. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  645. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  646. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  647. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  648. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  649. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  650. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  651. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  652. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  653. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  654. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  655. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  656. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  657. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  658. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  659. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  660. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  661. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  662. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  663. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  664. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  665. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  666. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  667. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  668. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  669. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  670. FIELD(EXIT_QUALIFICATION, exit_qualification),
  671. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  672. FIELD(GUEST_CR0, guest_cr0),
  673. FIELD(GUEST_CR3, guest_cr3),
  674. FIELD(GUEST_CR4, guest_cr4),
  675. FIELD(GUEST_ES_BASE, guest_es_base),
  676. FIELD(GUEST_CS_BASE, guest_cs_base),
  677. FIELD(GUEST_SS_BASE, guest_ss_base),
  678. FIELD(GUEST_DS_BASE, guest_ds_base),
  679. FIELD(GUEST_FS_BASE, guest_fs_base),
  680. FIELD(GUEST_GS_BASE, guest_gs_base),
  681. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  682. FIELD(GUEST_TR_BASE, guest_tr_base),
  683. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  684. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  685. FIELD(GUEST_DR7, guest_dr7),
  686. FIELD(GUEST_RSP, guest_rsp),
  687. FIELD(GUEST_RIP, guest_rip),
  688. FIELD(GUEST_RFLAGS, guest_rflags),
  689. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  690. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  691. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  692. FIELD(HOST_CR0, host_cr0),
  693. FIELD(HOST_CR3, host_cr3),
  694. FIELD(HOST_CR4, host_cr4),
  695. FIELD(HOST_FS_BASE, host_fs_base),
  696. FIELD(HOST_GS_BASE, host_gs_base),
  697. FIELD(HOST_TR_BASE, host_tr_base),
  698. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  699. FIELD(HOST_IDTR_BASE, host_idtr_base),
  700. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  701. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  702. FIELD(HOST_RSP, host_rsp),
  703. FIELD(HOST_RIP, host_rip),
  704. };
  705. static inline short vmcs_field_to_offset(unsigned long field)
  706. {
  707. BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
  708. if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
  709. vmcs_field_to_offset_table[field] == 0)
  710. return -ENOENT;
  711. return vmcs_field_to_offset_table[field];
  712. }
  713. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  714. {
  715. return to_vmx(vcpu)->nested.current_vmcs12;
  716. }
  717. static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
  718. {
  719. struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
  720. if (is_error_page(page))
  721. return NULL;
  722. return page;
  723. }
  724. static void nested_release_page(struct page *page)
  725. {
  726. kvm_release_page_dirty(page);
  727. }
  728. static void nested_release_page_clean(struct page *page)
  729. {
  730. kvm_release_page_clean(page);
  731. }
  732. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  733. static u64 construct_eptp(unsigned long root_hpa);
  734. static void kvm_cpu_vmxon(u64 addr);
  735. static void kvm_cpu_vmxoff(void);
  736. static bool vmx_mpx_supported(void);
  737. static bool vmx_xsaves_supported(void);
  738. static int vmx_vm_has_apicv(struct kvm *kvm);
  739. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
  740. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  741. struct kvm_segment *var, int seg);
  742. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  743. struct kvm_segment *var, int seg);
  744. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  745. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  746. static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
  747. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
  748. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  749. static int alloc_identity_pagetable(struct kvm *kvm);
  750. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  751. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  752. /*
  753. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  754. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  755. */
  756. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  757. static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
  758. static unsigned long *vmx_io_bitmap_a;
  759. static unsigned long *vmx_io_bitmap_b;
  760. static unsigned long *vmx_msr_bitmap_legacy;
  761. static unsigned long *vmx_msr_bitmap_longmode;
  762. static unsigned long *vmx_msr_bitmap_legacy_x2apic;
  763. static unsigned long *vmx_msr_bitmap_longmode_x2apic;
  764. static unsigned long *vmx_msr_bitmap_nested;
  765. static unsigned long *vmx_vmread_bitmap;
  766. static unsigned long *vmx_vmwrite_bitmap;
  767. static bool cpu_has_load_ia32_efer;
  768. static bool cpu_has_load_perf_global_ctrl;
  769. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  770. static DEFINE_SPINLOCK(vmx_vpid_lock);
  771. static struct vmcs_config {
  772. int size;
  773. int order;
  774. u32 revision_id;
  775. u32 pin_based_exec_ctrl;
  776. u32 cpu_based_exec_ctrl;
  777. u32 cpu_based_2nd_exec_ctrl;
  778. u32 vmexit_ctrl;
  779. u32 vmentry_ctrl;
  780. } vmcs_config;
  781. static struct vmx_capability {
  782. u32 ept;
  783. u32 vpid;
  784. } vmx_capability;
  785. #define VMX_SEGMENT_FIELD(seg) \
  786. [VCPU_SREG_##seg] = { \
  787. .selector = GUEST_##seg##_SELECTOR, \
  788. .base = GUEST_##seg##_BASE, \
  789. .limit = GUEST_##seg##_LIMIT, \
  790. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  791. }
  792. static const struct kvm_vmx_segment_field {
  793. unsigned selector;
  794. unsigned base;
  795. unsigned limit;
  796. unsigned ar_bytes;
  797. } kvm_vmx_segment_fields[] = {
  798. VMX_SEGMENT_FIELD(CS),
  799. VMX_SEGMENT_FIELD(DS),
  800. VMX_SEGMENT_FIELD(ES),
  801. VMX_SEGMENT_FIELD(FS),
  802. VMX_SEGMENT_FIELD(GS),
  803. VMX_SEGMENT_FIELD(SS),
  804. VMX_SEGMENT_FIELD(TR),
  805. VMX_SEGMENT_FIELD(LDTR),
  806. };
  807. static u64 host_efer;
  808. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  809. /*
  810. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  811. * away by decrementing the array size.
  812. */
  813. static const u32 vmx_msr_index[] = {
  814. #ifdef CONFIG_X86_64
  815. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  816. #endif
  817. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  818. };
  819. static inline bool is_page_fault(u32 intr_info)
  820. {
  821. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  822. INTR_INFO_VALID_MASK)) ==
  823. (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  824. }
  825. static inline bool is_no_device(u32 intr_info)
  826. {
  827. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  828. INTR_INFO_VALID_MASK)) ==
  829. (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  830. }
  831. static inline bool is_invalid_opcode(u32 intr_info)
  832. {
  833. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  834. INTR_INFO_VALID_MASK)) ==
  835. (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  836. }
  837. static inline bool is_external_interrupt(u32 intr_info)
  838. {
  839. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  840. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  841. }
  842. static inline bool is_machine_check(u32 intr_info)
  843. {
  844. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  845. INTR_INFO_VALID_MASK)) ==
  846. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  847. }
  848. static inline bool cpu_has_vmx_msr_bitmap(void)
  849. {
  850. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  851. }
  852. static inline bool cpu_has_vmx_tpr_shadow(void)
  853. {
  854. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  855. }
  856. static inline bool vm_need_tpr_shadow(struct kvm *kvm)
  857. {
  858. return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
  859. }
  860. static inline bool cpu_has_secondary_exec_ctrls(void)
  861. {
  862. return vmcs_config.cpu_based_exec_ctrl &
  863. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  864. }
  865. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  866. {
  867. return vmcs_config.cpu_based_2nd_exec_ctrl &
  868. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  869. }
  870. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  871. {
  872. return vmcs_config.cpu_based_2nd_exec_ctrl &
  873. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  874. }
  875. static inline bool cpu_has_vmx_apic_register_virt(void)
  876. {
  877. return vmcs_config.cpu_based_2nd_exec_ctrl &
  878. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  879. }
  880. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  881. {
  882. return vmcs_config.cpu_based_2nd_exec_ctrl &
  883. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  884. }
  885. static inline bool cpu_has_vmx_posted_intr(void)
  886. {
  887. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  888. }
  889. static inline bool cpu_has_vmx_apicv(void)
  890. {
  891. return cpu_has_vmx_apic_register_virt() &&
  892. cpu_has_vmx_virtual_intr_delivery() &&
  893. cpu_has_vmx_posted_intr();
  894. }
  895. static inline bool cpu_has_vmx_flexpriority(void)
  896. {
  897. return cpu_has_vmx_tpr_shadow() &&
  898. cpu_has_vmx_virtualize_apic_accesses();
  899. }
  900. static inline bool cpu_has_vmx_ept_execute_only(void)
  901. {
  902. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  903. }
  904. static inline bool cpu_has_vmx_ept_2m_page(void)
  905. {
  906. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  907. }
  908. static inline bool cpu_has_vmx_ept_1g_page(void)
  909. {
  910. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  911. }
  912. static inline bool cpu_has_vmx_ept_4levels(void)
  913. {
  914. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  915. }
  916. static inline bool cpu_has_vmx_ept_ad_bits(void)
  917. {
  918. return vmx_capability.ept & VMX_EPT_AD_BIT;
  919. }
  920. static inline bool cpu_has_vmx_invept_context(void)
  921. {
  922. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  923. }
  924. static inline bool cpu_has_vmx_invept_global(void)
  925. {
  926. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  927. }
  928. static inline bool cpu_has_vmx_invvpid_single(void)
  929. {
  930. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  931. }
  932. static inline bool cpu_has_vmx_invvpid_global(void)
  933. {
  934. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  935. }
  936. static inline bool cpu_has_vmx_ept(void)
  937. {
  938. return vmcs_config.cpu_based_2nd_exec_ctrl &
  939. SECONDARY_EXEC_ENABLE_EPT;
  940. }
  941. static inline bool cpu_has_vmx_unrestricted_guest(void)
  942. {
  943. return vmcs_config.cpu_based_2nd_exec_ctrl &
  944. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  945. }
  946. static inline bool cpu_has_vmx_ple(void)
  947. {
  948. return vmcs_config.cpu_based_2nd_exec_ctrl &
  949. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  950. }
  951. static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
  952. {
  953. return flexpriority_enabled && irqchip_in_kernel(kvm);
  954. }
  955. static inline bool cpu_has_vmx_vpid(void)
  956. {
  957. return vmcs_config.cpu_based_2nd_exec_ctrl &
  958. SECONDARY_EXEC_ENABLE_VPID;
  959. }
  960. static inline bool cpu_has_vmx_rdtscp(void)
  961. {
  962. return vmcs_config.cpu_based_2nd_exec_ctrl &
  963. SECONDARY_EXEC_RDTSCP;
  964. }
  965. static inline bool cpu_has_vmx_invpcid(void)
  966. {
  967. return vmcs_config.cpu_based_2nd_exec_ctrl &
  968. SECONDARY_EXEC_ENABLE_INVPCID;
  969. }
  970. static inline bool cpu_has_virtual_nmis(void)
  971. {
  972. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  973. }
  974. static inline bool cpu_has_vmx_wbinvd_exit(void)
  975. {
  976. return vmcs_config.cpu_based_2nd_exec_ctrl &
  977. SECONDARY_EXEC_WBINVD_EXITING;
  978. }
  979. static inline bool cpu_has_vmx_shadow_vmcs(void)
  980. {
  981. u64 vmx_msr;
  982. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  983. /* check if the cpu supports writing r/o exit information fields */
  984. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  985. return false;
  986. return vmcs_config.cpu_based_2nd_exec_ctrl &
  987. SECONDARY_EXEC_SHADOW_VMCS;
  988. }
  989. static inline bool cpu_has_vmx_pml(void)
  990. {
  991. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  992. }
  993. static inline bool report_flexpriority(void)
  994. {
  995. return flexpriority_enabled;
  996. }
  997. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  998. {
  999. return vmcs12->cpu_based_vm_exec_control & bit;
  1000. }
  1001. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1002. {
  1003. return (vmcs12->cpu_based_vm_exec_control &
  1004. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1005. (vmcs12->secondary_vm_exec_control & bit);
  1006. }
  1007. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1008. {
  1009. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1010. }
  1011. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1012. {
  1013. return vmcs12->pin_based_vm_exec_control &
  1014. PIN_BASED_VMX_PREEMPTION_TIMER;
  1015. }
  1016. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1017. {
  1018. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1019. }
  1020. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1021. {
  1022. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
  1023. vmx_xsaves_supported();
  1024. }
  1025. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1026. {
  1027. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1028. }
  1029. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1030. {
  1031. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1032. }
  1033. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1034. {
  1035. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1036. }
  1037. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1038. {
  1039. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1040. }
  1041. static inline bool is_exception(u32 intr_info)
  1042. {
  1043. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1044. == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
  1045. }
  1046. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1047. u32 exit_intr_info,
  1048. unsigned long exit_qualification);
  1049. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1050. struct vmcs12 *vmcs12,
  1051. u32 reason, unsigned long qualification);
  1052. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1053. {
  1054. int i;
  1055. for (i = 0; i < vmx->nmsrs; ++i)
  1056. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1057. return i;
  1058. return -1;
  1059. }
  1060. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  1061. {
  1062. struct {
  1063. u64 vpid : 16;
  1064. u64 rsvd : 48;
  1065. u64 gva;
  1066. } operand = { vpid, 0, gva };
  1067. asm volatile (__ex(ASM_VMX_INVVPID)
  1068. /* CF==1 or ZF==1 --> rc = -1 */
  1069. "; ja 1f ; ud2 ; 1:"
  1070. : : "a"(&operand), "c"(ext) : "cc", "memory");
  1071. }
  1072. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1073. {
  1074. struct {
  1075. u64 eptp, gpa;
  1076. } operand = {eptp, gpa};
  1077. asm volatile (__ex(ASM_VMX_INVEPT)
  1078. /* CF==1 or ZF==1 --> rc = -1 */
  1079. "; ja 1f ; ud2 ; 1:\n"
  1080. : : "a" (&operand), "c" (ext) : "cc", "memory");
  1081. }
  1082. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1083. {
  1084. int i;
  1085. i = __find_msr_index(vmx, msr);
  1086. if (i >= 0)
  1087. return &vmx->guest_msrs[i];
  1088. return NULL;
  1089. }
  1090. static void vmcs_clear(struct vmcs *vmcs)
  1091. {
  1092. u64 phys_addr = __pa(vmcs);
  1093. u8 error;
  1094. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  1095. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1096. : "cc", "memory");
  1097. if (error)
  1098. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1099. vmcs, phys_addr);
  1100. }
  1101. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1102. {
  1103. vmcs_clear(loaded_vmcs->vmcs);
  1104. loaded_vmcs->cpu = -1;
  1105. loaded_vmcs->launched = 0;
  1106. }
  1107. static void vmcs_load(struct vmcs *vmcs)
  1108. {
  1109. u64 phys_addr = __pa(vmcs);
  1110. u8 error;
  1111. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  1112. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1113. : "cc", "memory");
  1114. if (error)
  1115. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1116. vmcs, phys_addr);
  1117. }
  1118. #ifdef CONFIG_KEXEC_CORE
  1119. /*
  1120. * This bitmap is used to indicate whether the vmclear
  1121. * operation is enabled on all cpus. All disabled by
  1122. * default.
  1123. */
  1124. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1125. static inline void crash_enable_local_vmclear(int cpu)
  1126. {
  1127. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1128. }
  1129. static inline void crash_disable_local_vmclear(int cpu)
  1130. {
  1131. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1132. }
  1133. static inline int crash_local_vmclear_enabled(int cpu)
  1134. {
  1135. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1136. }
  1137. static void crash_vmclear_local_loaded_vmcss(void)
  1138. {
  1139. int cpu = raw_smp_processor_id();
  1140. struct loaded_vmcs *v;
  1141. if (!crash_local_vmclear_enabled(cpu))
  1142. return;
  1143. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1144. loaded_vmcss_on_cpu_link)
  1145. vmcs_clear(v->vmcs);
  1146. }
  1147. #else
  1148. static inline void crash_enable_local_vmclear(int cpu) { }
  1149. static inline void crash_disable_local_vmclear(int cpu) { }
  1150. #endif /* CONFIG_KEXEC_CORE */
  1151. static void __loaded_vmcs_clear(void *arg)
  1152. {
  1153. struct loaded_vmcs *loaded_vmcs = arg;
  1154. int cpu = raw_smp_processor_id();
  1155. if (loaded_vmcs->cpu != cpu)
  1156. return; /* vcpu migration can race with cpu offline */
  1157. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1158. per_cpu(current_vmcs, cpu) = NULL;
  1159. crash_disable_local_vmclear(cpu);
  1160. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1161. /*
  1162. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1163. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1164. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1165. * then adds the vmcs into percpu list before it is deleted.
  1166. */
  1167. smp_wmb();
  1168. loaded_vmcs_init(loaded_vmcs);
  1169. crash_enable_local_vmclear(cpu);
  1170. }
  1171. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1172. {
  1173. int cpu = loaded_vmcs->cpu;
  1174. if (cpu != -1)
  1175. smp_call_function_single(cpu,
  1176. __loaded_vmcs_clear, loaded_vmcs, 1);
  1177. }
  1178. static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
  1179. {
  1180. if (vmx->vpid == 0)
  1181. return;
  1182. if (cpu_has_vmx_invvpid_single())
  1183. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  1184. }
  1185. static inline void vpid_sync_vcpu_global(void)
  1186. {
  1187. if (cpu_has_vmx_invvpid_global())
  1188. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1189. }
  1190. static inline void vpid_sync_context(struct vcpu_vmx *vmx)
  1191. {
  1192. if (cpu_has_vmx_invvpid_single())
  1193. vpid_sync_vcpu_single(vmx);
  1194. else
  1195. vpid_sync_vcpu_global();
  1196. }
  1197. static inline void ept_sync_global(void)
  1198. {
  1199. if (cpu_has_vmx_invept_global())
  1200. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1201. }
  1202. static inline void ept_sync_context(u64 eptp)
  1203. {
  1204. if (enable_ept) {
  1205. if (cpu_has_vmx_invept_context())
  1206. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1207. else
  1208. ept_sync_global();
  1209. }
  1210. }
  1211. static __always_inline unsigned long vmcs_readl(unsigned long field)
  1212. {
  1213. unsigned long value;
  1214. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1215. : "=a"(value) : "d"(field) : "cc");
  1216. return value;
  1217. }
  1218. static __always_inline u16 vmcs_read16(unsigned long field)
  1219. {
  1220. return vmcs_readl(field);
  1221. }
  1222. static __always_inline u32 vmcs_read32(unsigned long field)
  1223. {
  1224. return vmcs_readl(field);
  1225. }
  1226. static __always_inline u64 vmcs_read64(unsigned long field)
  1227. {
  1228. #ifdef CONFIG_X86_64
  1229. return vmcs_readl(field);
  1230. #else
  1231. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  1232. #endif
  1233. }
  1234. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  1235. {
  1236. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  1237. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  1238. dump_stack();
  1239. }
  1240. static void vmcs_writel(unsigned long field, unsigned long value)
  1241. {
  1242. u8 error;
  1243. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  1244. : "=q"(error) : "a"(value), "d"(field) : "cc");
  1245. if (unlikely(error))
  1246. vmwrite_error(field, value);
  1247. }
  1248. static void vmcs_write16(unsigned long field, u16 value)
  1249. {
  1250. vmcs_writel(field, value);
  1251. }
  1252. static void vmcs_write32(unsigned long field, u32 value)
  1253. {
  1254. vmcs_writel(field, value);
  1255. }
  1256. static void vmcs_write64(unsigned long field, u64 value)
  1257. {
  1258. vmcs_writel(field, value);
  1259. #ifndef CONFIG_X86_64
  1260. asm volatile ("");
  1261. vmcs_writel(field+1, value >> 32);
  1262. #endif
  1263. }
  1264. static void vmcs_clear_bits(unsigned long field, u32 mask)
  1265. {
  1266. vmcs_writel(field, vmcs_readl(field) & ~mask);
  1267. }
  1268. static void vmcs_set_bits(unsigned long field, u32 mask)
  1269. {
  1270. vmcs_writel(field, vmcs_readl(field) | mask);
  1271. }
  1272. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  1273. {
  1274. vmcs_write32(VM_ENTRY_CONTROLS, val);
  1275. vmx->vm_entry_controls_shadow = val;
  1276. }
  1277. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  1278. {
  1279. if (vmx->vm_entry_controls_shadow != val)
  1280. vm_entry_controls_init(vmx, val);
  1281. }
  1282. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  1283. {
  1284. return vmx->vm_entry_controls_shadow;
  1285. }
  1286. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1287. {
  1288. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  1289. }
  1290. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1291. {
  1292. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  1293. }
  1294. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  1295. {
  1296. vmcs_write32(VM_EXIT_CONTROLS, val);
  1297. vmx->vm_exit_controls_shadow = val;
  1298. }
  1299. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  1300. {
  1301. if (vmx->vm_exit_controls_shadow != val)
  1302. vm_exit_controls_init(vmx, val);
  1303. }
  1304. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  1305. {
  1306. return vmx->vm_exit_controls_shadow;
  1307. }
  1308. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1309. {
  1310. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  1311. }
  1312. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1313. {
  1314. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  1315. }
  1316. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1317. {
  1318. vmx->segment_cache.bitmask = 0;
  1319. }
  1320. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1321. unsigned field)
  1322. {
  1323. bool ret;
  1324. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1325. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1326. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1327. vmx->segment_cache.bitmask = 0;
  1328. }
  1329. ret = vmx->segment_cache.bitmask & mask;
  1330. vmx->segment_cache.bitmask |= mask;
  1331. return ret;
  1332. }
  1333. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1334. {
  1335. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1336. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1337. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1338. return *p;
  1339. }
  1340. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1341. {
  1342. ulong *p = &vmx->segment_cache.seg[seg].base;
  1343. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1344. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1345. return *p;
  1346. }
  1347. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1348. {
  1349. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1350. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1351. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1352. return *p;
  1353. }
  1354. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1355. {
  1356. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1357. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1358. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1359. return *p;
  1360. }
  1361. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1362. {
  1363. u32 eb;
  1364. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1365. (1u << NM_VECTOR) | (1u << DB_VECTOR);
  1366. if ((vcpu->guest_debug &
  1367. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1368. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1369. eb |= 1u << BP_VECTOR;
  1370. if (to_vmx(vcpu)->rmode.vm86_active)
  1371. eb = ~0;
  1372. if (enable_ept)
  1373. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  1374. if (vcpu->fpu_active)
  1375. eb &= ~(1u << NM_VECTOR);
  1376. /* When we are running a nested L2 guest and L1 specified for it a
  1377. * certain exception bitmap, we must trap the same exceptions and pass
  1378. * them to L1. When running L2, we will only handle the exceptions
  1379. * specified above if L1 did not want them.
  1380. */
  1381. if (is_guest_mode(vcpu))
  1382. eb |= get_vmcs12(vcpu)->exception_bitmap;
  1383. vmcs_write32(EXCEPTION_BITMAP, eb);
  1384. }
  1385. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1386. unsigned long entry, unsigned long exit)
  1387. {
  1388. vm_entry_controls_clearbit(vmx, entry);
  1389. vm_exit_controls_clearbit(vmx, exit);
  1390. }
  1391. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  1392. {
  1393. unsigned i;
  1394. struct msr_autoload *m = &vmx->msr_autoload;
  1395. switch (msr) {
  1396. case MSR_EFER:
  1397. if (cpu_has_load_ia32_efer) {
  1398. clear_atomic_switch_msr_special(vmx,
  1399. VM_ENTRY_LOAD_IA32_EFER,
  1400. VM_EXIT_LOAD_IA32_EFER);
  1401. return;
  1402. }
  1403. break;
  1404. case MSR_CORE_PERF_GLOBAL_CTRL:
  1405. if (cpu_has_load_perf_global_ctrl) {
  1406. clear_atomic_switch_msr_special(vmx,
  1407. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1408. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  1409. return;
  1410. }
  1411. break;
  1412. }
  1413. for (i = 0; i < m->nr; ++i)
  1414. if (m->guest[i].index == msr)
  1415. break;
  1416. if (i == m->nr)
  1417. return;
  1418. --m->nr;
  1419. m->guest[i] = m->guest[m->nr];
  1420. m->host[i] = m->host[m->nr];
  1421. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1422. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1423. }
  1424. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1425. unsigned long entry, unsigned long exit,
  1426. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  1427. u64 guest_val, u64 host_val)
  1428. {
  1429. vmcs_write64(guest_val_vmcs, guest_val);
  1430. vmcs_write64(host_val_vmcs, host_val);
  1431. vm_entry_controls_setbit(vmx, entry);
  1432. vm_exit_controls_setbit(vmx, exit);
  1433. }
  1434. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  1435. u64 guest_val, u64 host_val)
  1436. {
  1437. unsigned i;
  1438. struct msr_autoload *m = &vmx->msr_autoload;
  1439. switch (msr) {
  1440. case MSR_EFER:
  1441. if (cpu_has_load_ia32_efer) {
  1442. add_atomic_switch_msr_special(vmx,
  1443. VM_ENTRY_LOAD_IA32_EFER,
  1444. VM_EXIT_LOAD_IA32_EFER,
  1445. GUEST_IA32_EFER,
  1446. HOST_IA32_EFER,
  1447. guest_val, host_val);
  1448. return;
  1449. }
  1450. break;
  1451. case MSR_CORE_PERF_GLOBAL_CTRL:
  1452. if (cpu_has_load_perf_global_ctrl) {
  1453. add_atomic_switch_msr_special(vmx,
  1454. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1455. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  1456. GUEST_IA32_PERF_GLOBAL_CTRL,
  1457. HOST_IA32_PERF_GLOBAL_CTRL,
  1458. guest_val, host_val);
  1459. return;
  1460. }
  1461. break;
  1462. }
  1463. for (i = 0; i < m->nr; ++i)
  1464. if (m->guest[i].index == msr)
  1465. break;
  1466. if (i == NR_AUTOLOAD_MSRS) {
  1467. printk_once(KERN_WARNING "Not enough msr switch entries. "
  1468. "Can't add msr %x\n", msr);
  1469. return;
  1470. } else if (i == m->nr) {
  1471. ++m->nr;
  1472. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1473. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1474. }
  1475. m->guest[i].index = msr;
  1476. m->guest[i].value = guest_val;
  1477. m->host[i].index = msr;
  1478. m->host[i].value = host_val;
  1479. }
  1480. static void reload_tss(void)
  1481. {
  1482. /*
  1483. * VT restores TR but not its size. Useless.
  1484. */
  1485. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1486. struct desc_struct *descs;
  1487. descs = (void *)gdt->address;
  1488. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  1489. load_TR_desc();
  1490. }
  1491. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  1492. {
  1493. u64 guest_efer;
  1494. u64 ignore_bits;
  1495. guest_efer = vmx->vcpu.arch.efer;
  1496. /*
  1497. * NX is emulated; LMA and LME handled by hardware; SCE meaningless
  1498. * outside long mode
  1499. */
  1500. ignore_bits = EFER_NX | EFER_SCE;
  1501. #ifdef CONFIG_X86_64
  1502. ignore_bits |= EFER_LMA | EFER_LME;
  1503. /* SCE is meaningful only in long mode on Intel */
  1504. if (guest_efer & EFER_LMA)
  1505. ignore_bits &= ~(u64)EFER_SCE;
  1506. #endif
  1507. guest_efer &= ~ignore_bits;
  1508. guest_efer |= host_efer & ignore_bits;
  1509. vmx->guest_msrs[efer_offset].data = guest_efer;
  1510. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  1511. clear_atomic_switch_msr(vmx, MSR_EFER);
  1512. /*
  1513. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  1514. * On CPUs that support "load IA32_EFER", always switch EFER
  1515. * atomically, since it's faster than switching it manually.
  1516. */
  1517. if (cpu_has_load_ia32_efer ||
  1518. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  1519. guest_efer = vmx->vcpu.arch.efer;
  1520. if (!(guest_efer & EFER_LMA))
  1521. guest_efer &= ~EFER_LME;
  1522. if (guest_efer != host_efer)
  1523. add_atomic_switch_msr(vmx, MSR_EFER,
  1524. guest_efer, host_efer);
  1525. return false;
  1526. }
  1527. return true;
  1528. }
  1529. static unsigned long segment_base(u16 selector)
  1530. {
  1531. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1532. struct desc_struct *d;
  1533. unsigned long table_base;
  1534. unsigned long v;
  1535. if (!(selector & ~3))
  1536. return 0;
  1537. table_base = gdt->address;
  1538. if (selector & 4) { /* from ldt */
  1539. u16 ldt_selector = kvm_read_ldt();
  1540. if (!(ldt_selector & ~3))
  1541. return 0;
  1542. table_base = segment_base(ldt_selector);
  1543. }
  1544. d = (struct desc_struct *)(table_base + (selector & ~7));
  1545. v = get_desc_base(d);
  1546. #ifdef CONFIG_X86_64
  1547. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  1548. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  1549. #endif
  1550. return v;
  1551. }
  1552. static inline unsigned long kvm_read_tr_base(void)
  1553. {
  1554. u16 tr;
  1555. asm("str %0" : "=g"(tr));
  1556. return segment_base(tr);
  1557. }
  1558. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  1559. {
  1560. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1561. int i;
  1562. if (vmx->host_state.loaded)
  1563. return;
  1564. vmx->host_state.loaded = 1;
  1565. /*
  1566. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  1567. * allow segment selectors with cpl > 0 or ti == 1.
  1568. */
  1569. vmx->host_state.ldt_sel = kvm_read_ldt();
  1570. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  1571. savesegment(fs, vmx->host_state.fs_sel);
  1572. if (!(vmx->host_state.fs_sel & 7)) {
  1573. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  1574. vmx->host_state.fs_reload_needed = 0;
  1575. } else {
  1576. vmcs_write16(HOST_FS_SELECTOR, 0);
  1577. vmx->host_state.fs_reload_needed = 1;
  1578. }
  1579. savesegment(gs, vmx->host_state.gs_sel);
  1580. if (!(vmx->host_state.gs_sel & 7))
  1581. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  1582. else {
  1583. vmcs_write16(HOST_GS_SELECTOR, 0);
  1584. vmx->host_state.gs_ldt_reload_needed = 1;
  1585. }
  1586. #ifdef CONFIG_X86_64
  1587. savesegment(ds, vmx->host_state.ds_sel);
  1588. savesegment(es, vmx->host_state.es_sel);
  1589. #endif
  1590. #ifdef CONFIG_X86_64
  1591. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  1592. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  1593. #else
  1594. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  1595. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  1596. #endif
  1597. #ifdef CONFIG_X86_64
  1598. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1599. if (is_long_mode(&vmx->vcpu))
  1600. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1601. #endif
  1602. if (boot_cpu_has(X86_FEATURE_MPX))
  1603. rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1604. for (i = 0; i < vmx->save_nmsrs; ++i)
  1605. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  1606. vmx->guest_msrs[i].data,
  1607. vmx->guest_msrs[i].mask);
  1608. }
  1609. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  1610. {
  1611. if (!vmx->host_state.loaded)
  1612. return;
  1613. ++vmx->vcpu.stat.host_state_reload;
  1614. vmx->host_state.loaded = 0;
  1615. #ifdef CONFIG_X86_64
  1616. if (is_long_mode(&vmx->vcpu))
  1617. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1618. #endif
  1619. if (vmx->host_state.gs_ldt_reload_needed) {
  1620. kvm_load_ldt(vmx->host_state.ldt_sel);
  1621. #ifdef CONFIG_X86_64
  1622. load_gs_index(vmx->host_state.gs_sel);
  1623. #else
  1624. loadsegment(gs, vmx->host_state.gs_sel);
  1625. #endif
  1626. }
  1627. if (vmx->host_state.fs_reload_needed)
  1628. loadsegment(fs, vmx->host_state.fs_sel);
  1629. #ifdef CONFIG_X86_64
  1630. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  1631. loadsegment(ds, vmx->host_state.ds_sel);
  1632. loadsegment(es, vmx->host_state.es_sel);
  1633. }
  1634. #endif
  1635. reload_tss();
  1636. #ifdef CONFIG_X86_64
  1637. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1638. #endif
  1639. if (vmx->host_state.msr_host_bndcfgs)
  1640. wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1641. /*
  1642. * If the FPU is not active (through the host task or
  1643. * the guest vcpu), then restore the cr0.TS bit.
  1644. */
  1645. if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
  1646. stts();
  1647. load_gdt(this_cpu_ptr(&host_gdt));
  1648. }
  1649. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  1650. {
  1651. preempt_disable();
  1652. __vmx_load_host_state(vmx);
  1653. preempt_enable();
  1654. }
  1655. /*
  1656. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  1657. * vcpu mutex is already taken.
  1658. */
  1659. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1660. {
  1661. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1662. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  1663. if (!vmm_exclusive)
  1664. kvm_cpu_vmxon(phys_addr);
  1665. else if (vmx->loaded_vmcs->cpu != cpu)
  1666. loaded_vmcs_clear(vmx->loaded_vmcs);
  1667. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  1668. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  1669. vmcs_load(vmx->loaded_vmcs->vmcs);
  1670. }
  1671. if (vmx->loaded_vmcs->cpu != cpu) {
  1672. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1673. unsigned long sysenter_esp;
  1674. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1675. local_irq_disable();
  1676. crash_disable_local_vmclear(cpu);
  1677. /*
  1678. * Read loaded_vmcs->cpu should be before fetching
  1679. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  1680. * See the comments in __loaded_vmcs_clear().
  1681. */
  1682. smp_rmb();
  1683. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  1684. &per_cpu(loaded_vmcss_on_cpu, cpu));
  1685. crash_enable_local_vmclear(cpu);
  1686. local_irq_enable();
  1687. /*
  1688. * Linux uses per-cpu TSS and GDT, so set these when switching
  1689. * processors.
  1690. */
  1691. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  1692. vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
  1693. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  1694. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  1695. vmx->loaded_vmcs->cpu = cpu;
  1696. }
  1697. }
  1698. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  1699. {
  1700. __vmx_load_host_state(to_vmx(vcpu));
  1701. if (!vmm_exclusive) {
  1702. __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
  1703. vcpu->cpu = -1;
  1704. kvm_cpu_vmxoff();
  1705. }
  1706. }
  1707. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  1708. {
  1709. ulong cr0;
  1710. if (vcpu->fpu_active)
  1711. return;
  1712. vcpu->fpu_active = 1;
  1713. cr0 = vmcs_readl(GUEST_CR0);
  1714. cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
  1715. cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
  1716. vmcs_writel(GUEST_CR0, cr0);
  1717. update_exception_bitmap(vcpu);
  1718. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  1719. if (is_guest_mode(vcpu))
  1720. vcpu->arch.cr0_guest_owned_bits &=
  1721. ~get_vmcs12(vcpu)->cr0_guest_host_mask;
  1722. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1723. }
  1724. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  1725. /*
  1726. * Return the cr0 value that a nested guest would read. This is a combination
  1727. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  1728. * its hypervisor (cr0_read_shadow).
  1729. */
  1730. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  1731. {
  1732. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  1733. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  1734. }
  1735. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  1736. {
  1737. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  1738. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  1739. }
  1740. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  1741. {
  1742. /* Note that there is no vcpu->fpu_active = 0 here. The caller must
  1743. * set this *before* calling this function.
  1744. */
  1745. vmx_decache_cr0_guest_bits(vcpu);
  1746. vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
  1747. update_exception_bitmap(vcpu);
  1748. vcpu->arch.cr0_guest_owned_bits = 0;
  1749. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1750. if (is_guest_mode(vcpu)) {
  1751. /*
  1752. * L1's specified read shadow might not contain the TS bit,
  1753. * so now that we turned on shadowing of this bit, we need to
  1754. * set this bit of the shadow. Like in nested_vmx_run we need
  1755. * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
  1756. * up-to-date here because we just decached cr0.TS (and we'll
  1757. * only update vmcs12->guest_cr0 on nested exit).
  1758. */
  1759. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1760. vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
  1761. (vcpu->arch.cr0 & X86_CR0_TS);
  1762. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  1763. } else
  1764. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  1765. }
  1766. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  1767. {
  1768. unsigned long rflags, save_rflags;
  1769. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  1770. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1771. rflags = vmcs_readl(GUEST_RFLAGS);
  1772. if (to_vmx(vcpu)->rmode.vm86_active) {
  1773. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1774. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  1775. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1776. }
  1777. to_vmx(vcpu)->rflags = rflags;
  1778. }
  1779. return to_vmx(vcpu)->rflags;
  1780. }
  1781. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1782. {
  1783. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1784. to_vmx(vcpu)->rflags = rflags;
  1785. if (to_vmx(vcpu)->rmode.vm86_active) {
  1786. to_vmx(vcpu)->rmode.save_rflags = rflags;
  1787. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1788. }
  1789. vmcs_writel(GUEST_RFLAGS, rflags);
  1790. }
  1791. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  1792. {
  1793. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1794. int ret = 0;
  1795. if (interruptibility & GUEST_INTR_STATE_STI)
  1796. ret |= KVM_X86_SHADOW_INT_STI;
  1797. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  1798. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  1799. return ret;
  1800. }
  1801. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1802. {
  1803. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1804. u32 interruptibility = interruptibility_old;
  1805. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  1806. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  1807. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  1808. else if (mask & KVM_X86_SHADOW_INT_STI)
  1809. interruptibility |= GUEST_INTR_STATE_STI;
  1810. if ((interruptibility != interruptibility_old))
  1811. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  1812. }
  1813. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  1814. {
  1815. unsigned long rip;
  1816. rip = kvm_rip_read(vcpu);
  1817. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  1818. kvm_rip_write(vcpu, rip);
  1819. /* skipping an emulated instruction also counts */
  1820. vmx_set_interrupt_shadow(vcpu, 0);
  1821. }
  1822. /*
  1823. * KVM wants to inject page-faults which it got to the guest. This function
  1824. * checks whether in a nested guest, we need to inject them to L1 or L2.
  1825. */
  1826. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
  1827. {
  1828. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1829. if (!(vmcs12->exception_bitmap & (1u << nr)))
  1830. return 0;
  1831. nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
  1832. vmcs_read32(VM_EXIT_INTR_INFO),
  1833. vmcs_readl(EXIT_QUALIFICATION));
  1834. return 1;
  1835. }
  1836. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  1837. bool has_error_code, u32 error_code,
  1838. bool reinject)
  1839. {
  1840. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1841. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  1842. if (!reinject && is_guest_mode(vcpu) &&
  1843. nested_vmx_check_exception(vcpu, nr))
  1844. return;
  1845. if (has_error_code) {
  1846. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  1847. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  1848. }
  1849. if (vmx->rmode.vm86_active) {
  1850. int inc_eip = 0;
  1851. if (kvm_exception_is_soft(nr))
  1852. inc_eip = vcpu->arch.event_exit_inst_len;
  1853. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  1854. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  1855. return;
  1856. }
  1857. if (kvm_exception_is_soft(nr)) {
  1858. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  1859. vmx->vcpu.arch.event_exit_inst_len);
  1860. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  1861. } else
  1862. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  1863. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  1864. }
  1865. static bool vmx_rdtscp_supported(void)
  1866. {
  1867. return cpu_has_vmx_rdtscp();
  1868. }
  1869. static bool vmx_invpcid_supported(void)
  1870. {
  1871. return cpu_has_vmx_invpcid() && enable_ept;
  1872. }
  1873. /*
  1874. * Swap MSR entry in host/guest MSR entry array.
  1875. */
  1876. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  1877. {
  1878. struct shared_msr_entry tmp;
  1879. tmp = vmx->guest_msrs[to];
  1880. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  1881. vmx->guest_msrs[from] = tmp;
  1882. }
  1883. static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
  1884. {
  1885. unsigned long *msr_bitmap;
  1886. if (is_guest_mode(vcpu))
  1887. msr_bitmap = vmx_msr_bitmap_nested;
  1888. else if (vcpu->arch.apic_base & X2APIC_ENABLE) {
  1889. if (is_long_mode(vcpu))
  1890. msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
  1891. else
  1892. msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
  1893. } else {
  1894. if (is_long_mode(vcpu))
  1895. msr_bitmap = vmx_msr_bitmap_longmode;
  1896. else
  1897. msr_bitmap = vmx_msr_bitmap_legacy;
  1898. }
  1899. vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
  1900. }
  1901. /*
  1902. * Set up the vmcs to automatically save and restore system
  1903. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  1904. * mode, as fiddling with msrs is very expensive.
  1905. */
  1906. static void setup_msrs(struct vcpu_vmx *vmx)
  1907. {
  1908. int save_nmsrs, index;
  1909. save_nmsrs = 0;
  1910. #ifdef CONFIG_X86_64
  1911. if (is_long_mode(&vmx->vcpu)) {
  1912. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  1913. if (index >= 0)
  1914. move_msr_up(vmx, index, save_nmsrs++);
  1915. index = __find_msr_index(vmx, MSR_LSTAR);
  1916. if (index >= 0)
  1917. move_msr_up(vmx, index, save_nmsrs++);
  1918. index = __find_msr_index(vmx, MSR_CSTAR);
  1919. if (index >= 0)
  1920. move_msr_up(vmx, index, save_nmsrs++);
  1921. index = __find_msr_index(vmx, MSR_TSC_AUX);
  1922. if (index >= 0 && vmx->rdtscp_enabled)
  1923. move_msr_up(vmx, index, save_nmsrs++);
  1924. /*
  1925. * MSR_STAR is only needed on long mode guests, and only
  1926. * if efer.sce is enabled.
  1927. */
  1928. index = __find_msr_index(vmx, MSR_STAR);
  1929. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  1930. move_msr_up(vmx, index, save_nmsrs++);
  1931. }
  1932. #endif
  1933. index = __find_msr_index(vmx, MSR_EFER);
  1934. if (index >= 0 && update_transition_efer(vmx, index))
  1935. move_msr_up(vmx, index, save_nmsrs++);
  1936. vmx->save_nmsrs = save_nmsrs;
  1937. if (cpu_has_vmx_msr_bitmap())
  1938. vmx_set_msr_bitmap(&vmx->vcpu);
  1939. }
  1940. /*
  1941. * reads and returns guest's timestamp counter "register"
  1942. * guest_tsc = host_tsc + tsc_offset -- 21.3
  1943. */
  1944. static u64 guest_read_tsc(void)
  1945. {
  1946. u64 host_tsc, tsc_offset;
  1947. host_tsc = rdtsc();
  1948. tsc_offset = vmcs_read64(TSC_OFFSET);
  1949. return host_tsc + tsc_offset;
  1950. }
  1951. /*
  1952. * Like guest_read_tsc, but always returns L1's notion of the timestamp
  1953. * counter, even if a nested guest (L2) is currently running.
  1954. */
  1955. static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
  1956. {
  1957. u64 tsc_offset;
  1958. tsc_offset = is_guest_mode(vcpu) ?
  1959. to_vmx(vcpu)->nested.vmcs01_tsc_offset :
  1960. vmcs_read64(TSC_OFFSET);
  1961. return host_tsc + tsc_offset;
  1962. }
  1963. /*
  1964. * Engage any workarounds for mis-matched TSC rates. Currently limited to
  1965. * software catchup for faster rates on slower CPUs.
  1966. */
  1967. static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
  1968. {
  1969. if (!scale)
  1970. return;
  1971. if (user_tsc_khz > tsc_khz) {
  1972. vcpu->arch.tsc_catchup = 1;
  1973. vcpu->arch.tsc_always_catchup = 1;
  1974. } else
  1975. WARN(1, "user requested TSC rate below hardware speed\n");
  1976. }
  1977. static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
  1978. {
  1979. return vmcs_read64(TSC_OFFSET);
  1980. }
  1981. /*
  1982. * writes 'offset' into guest's timestamp counter offset register
  1983. */
  1984. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1985. {
  1986. if (is_guest_mode(vcpu)) {
  1987. /*
  1988. * We're here if L1 chose not to trap WRMSR to TSC. According
  1989. * to the spec, this should set L1's TSC; The offset that L1
  1990. * set for L2 remains unchanged, and still needs to be added
  1991. * to the newly set TSC to get L2's TSC.
  1992. */
  1993. struct vmcs12 *vmcs12;
  1994. to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
  1995. /* recalculate vmcs02.TSC_OFFSET: */
  1996. vmcs12 = get_vmcs12(vcpu);
  1997. vmcs_write64(TSC_OFFSET, offset +
  1998. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  1999. vmcs12->tsc_offset : 0));
  2000. } else {
  2001. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2002. vmcs_read64(TSC_OFFSET), offset);
  2003. vmcs_write64(TSC_OFFSET, offset);
  2004. }
  2005. }
  2006. static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
  2007. {
  2008. u64 offset = vmcs_read64(TSC_OFFSET);
  2009. vmcs_write64(TSC_OFFSET, offset + adjustment);
  2010. if (is_guest_mode(vcpu)) {
  2011. /* Even when running L2, the adjustment needs to apply to L1 */
  2012. to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
  2013. } else
  2014. trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
  2015. offset + adjustment);
  2016. }
  2017. static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  2018. {
  2019. return target_tsc - rdtsc();
  2020. }
  2021. static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
  2022. {
  2023. struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
  2024. return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
  2025. }
  2026. /*
  2027. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2028. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2029. * all guests if the "nested" module option is off, and can also be disabled
  2030. * for a single guest by disabling its VMX cpuid bit.
  2031. */
  2032. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2033. {
  2034. return nested && guest_cpuid_has_vmx(vcpu);
  2035. }
  2036. /*
  2037. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2038. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2039. * The same values should also be used to verify that vmcs12 control fields are
  2040. * valid during nested entry from L1 to L2.
  2041. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2042. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2043. * bit in the high half is on if the corresponding bit in the control field
  2044. * may be on. See also vmx_control_verify().
  2045. */
  2046. static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
  2047. {
  2048. /*
  2049. * Note that as a general rule, the high half of the MSRs (bits in
  2050. * the control fields which may be 1) should be initialized by the
  2051. * intersection of the underlying hardware's MSR (i.e., features which
  2052. * can be supported) and the list of features we want to expose -
  2053. * because they are known to be properly supported in our code.
  2054. * Also, usually, the low half of the MSRs (bits which must be 1) can
  2055. * be set to 0, meaning that L1 may turn off any of these bits. The
  2056. * reason is that if one of these bits is necessary, it will appear
  2057. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  2058. * fields of vmcs01 and vmcs02, will turn these bits off - and
  2059. * nested_vmx_exit_handled() will not pass related exits to L1.
  2060. * These rules have exceptions below.
  2061. */
  2062. /* pin-based controls */
  2063. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2064. vmx->nested.nested_vmx_pinbased_ctls_low,
  2065. vmx->nested.nested_vmx_pinbased_ctls_high);
  2066. vmx->nested.nested_vmx_pinbased_ctls_low |=
  2067. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2068. vmx->nested.nested_vmx_pinbased_ctls_high &=
  2069. PIN_BASED_EXT_INTR_MASK |
  2070. PIN_BASED_NMI_EXITING |
  2071. PIN_BASED_VIRTUAL_NMIS;
  2072. vmx->nested.nested_vmx_pinbased_ctls_high |=
  2073. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2074. PIN_BASED_VMX_PREEMPTION_TIMER;
  2075. if (vmx_vm_has_apicv(vmx->vcpu.kvm))
  2076. vmx->nested.nested_vmx_pinbased_ctls_high |=
  2077. PIN_BASED_POSTED_INTR;
  2078. /* exit controls */
  2079. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  2080. vmx->nested.nested_vmx_exit_ctls_low,
  2081. vmx->nested.nested_vmx_exit_ctls_high);
  2082. vmx->nested.nested_vmx_exit_ctls_low =
  2083. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2084. vmx->nested.nested_vmx_exit_ctls_high &=
  2085. #ifdef CONFIG_X86_64
  2086. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  2087. #endif
  2088. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  2089. vmx->nested.nested_vmx_exit_ctls_high |=
  2090. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  2091. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  2092. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  2093. if (vmx_mpx_supported())
  2094. vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  2095. /* We support free control of debug control saving. */
  2096. vmx->nested.nested_vmx_true_exit_ctls_low =
  2097. vmx->nested.nested_vmx_exit_ctls_low &
  2098. ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  2099. /* entry controls */
  2100. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  2101. vmx->nested.nested_vmx_entry_ctls_low,
  2102. vmx->nested.nested_vmx_entry_ctls_high);
  2103. vmx->nested.nested_vmx_entry_ctls_low =
  2104. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2105. vmx->nested.nested_vmx_entry_ctls_high &=
  2106. #ifdef CONFIG_X86_64
  2107. VM_ENTRY_IA32E_MODE |
  2108. #endif
  2109. VM_ENTRY_LOAD_IA32_PAT;
  2110. vmx->nested.nested_vmx_entry_ctls_high |=
  2111. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  2112. if (vmx_mpx_supported())
  2113. vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  2114. /* We support free control of debug control loading. */
  2115. vmx->nested.nested_vmx_true_entry_ctls_low =
  2116. vmx->nested.nested_vmx_entry_ctls_low &
  2117. ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2118. /* cpu-based controls */
  2119. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  2120. vmx->nested.nested_vmx_procbased_ctls_low,
  2121. vmx->nested.nested_vmx_procbased_ctls_high);
  2122. vmx->nested.nested_vmx_procbased_ctls_low =
  2123. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2124. vmx->nested.nested_vmx_procbased_ctls_high &=
  2125. CPU_BASED_VIRTUAL_INTR_PENDING |
  2126. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  2127. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  2128. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  2129. CPU_BASED_CR3_STORE_EXITING |
  2130. #ifdef CONFIG_X86_64
  2131. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  2132. #endif
  2133. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  2134. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  2135. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  2136. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  2137. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2138. /*
  2139. * We can allow some features even when not supported by the
  2140. * hardware. For example, L1 can specify an MSR bitmap - and we
  2141. * can use it to avoid exits to L1 - even when L0 runs L2
  2142. * without MSR bitmaps.
  2143. */
  2144. vmx->nested.nested_vmx_procbased_ctls_high |=
  2145. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2146. CPU_BASED_USE_MSR_BITMAPS;
  2147. /* We support free control of CR3 access interception. */
  2148. vmx->nested.nested_vmx_true_procbased_ctls_low =
  2149. vmx->nested.nested_vmx_procbased_ctls_low &
  2150. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  2151. /* secondary cpu-based controls */
  2152. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  2153. vmx->nested.nested_vmx_secondary_ctls_low,
  2154. vmx->nested.nested_vmx_secondary_ctls_high);
  2155. vmx->nested.nested_vmx_secondary_ctls_low = 0;
  2156. vmx->nested.nested_vmx_secondary_ctls_high &=
  2157. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2158. SECONDARY_EXEC_RDTSCP |
  2159. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2160. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2161. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2162. SECONDARY_EXEC_WBINVD_EXITING |
  2163. SECONDARY_EXEC_XSAVES;
  2164. if (enable_ept) {
  2165. /* nested EPT: emulate EPT also to L1 */
  2166. vmx->nested.nested_vmx_secondary_ctls_high |=
  2167. SECONDARY_EXEC_ENABLE_EPT;
  2168. vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  2169. VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
  2170. VMX_EPT_INVEPT_BIT;
  2171. vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
  2172. /*
  2173. * For nested guests, we don't do anything specific
  2174. * for single context invalidation. Hence, only advertise
  2175. * support for global context invalidation.
  2176. */
  2177. vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT;
  2178. } else
  2179. vmx->nested.nested_vmx_ept_caps = 0;
  2180. if (enable_unrestricted_guest)
  2181. vmx->nested.nested_vmx_secondary_ctls_high |=
  2182. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2183. /* miscellaneous data */
  2184. rdmsr(MSR_IA32_VMX_MISC,
  2185. vmx->nested.nested_vmx_misc_low,
  2186. vmx->nested.nested_vmx_misc_high);
  2187. vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
  2188. vmx->nested.nested_vmx_misc_low |=
  2189. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  2190. VMX_MISC_ACTIVITY_HLT;
  2191. vmx->nested.nested_vmx_misc_high = 0;
  2192. }
  2193. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  2194. {
  2195. /*
  2196. * Bits 0 in high must be 0, and bits 1 in low must be 1.
  2197. */
  2198. return ((control & high) | low) == control;
  2199. }
  2200. static inline u64 vmx_control_msr(u32 low, u32 high)
  2201. {
  2202. return low | ((u64)high << 32);
  2203. }
  2204. /* Returns 0 on success, non-0 otherwise. */
  2205. static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  2206. {
  2207. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2208. switch (msr_index) {
  2209. case MSR_IA32_VMX_BASIC:
  2210. /*
  2211. * This MSR reports some information about VMX support. We
  2212. * should return information about the VMX we emulate for the
  2213. * guest, and the VMCS structure we give it - not about the
  2214. * VMX support of the underlying hardware.
  2215. */
  2216. *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
  2217. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  2218. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  2219. break;
  2220. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2221. case MSR_IA32_VMX_PINBASED_CTLS:
  2222. *pdata = vmx_control_msr(
  2223. vmx->nested.nested_vmx_pinbased_ctls_low,
  2224. vmx->nested.nested_vmx_pinbased_ctls_high);
  2225. break;
  2226. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2227. *pdata = vmx_control_msr(
  2228. vmx->nested.nested_vmx_true_procbased_ctls_low,
  2229. vmx->nested.nested_vmx_procbased_ctls_high);
  2230. break;
  2231. case MSR_IA32_VMX_PROCBASED_CTLS:
  2232. *pdata = vmx_control_msr(
  2233. vmx->nested.nested_vmx_procbased_ctls_low,
  2234. vmx->nested.nested_vmx_procbased_ctls_high);
  2235. break;
  2236. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2237. *pdata = vmx_control_msr(
  2238. vmx->nested.nested_vmx_true_exit_ctls_low,
  2239. vmx->nested.nested_vmx_exit_ctls_high);
  2240. break;
  2241. case MSR_IA32_VMX_EXIT_CTLS:
  2242. *pdata = vmx_control_msr(
  2243. vmx->nested.nested_vmx_exit_ctls_low,
  2244. vmx->nested.nested_vmx_exit_ctls_high);
  2245. break;
  2246. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2247. *pdata = vmx_control_msr(
  2248. vmx->nested.nested_vmx_true_entry_ctls_low,
  2249. vmx->nested.nested_vmx_entry_ctls_high);
  2250. break;
  2251. case MSR_IA32_VMX_ENTRY_CTLS:
  2252. *pdata = vmx_control_msr(
  2253. vmx->nested.nested_vmx_entry_ctls_low,
  2254. vmx->nested.nested_vmx_entry_ctls_high);
  2255. break;
  2256. case MSR_IA32_VMX_MISC:
  2257. *pdata = vmx_control_msr(
  2258. vmx->nested.nested_vmx_misc_low,
  2259. vmx->nested.nested_vmx_misc_high);
  2260. break;
  2261. /*
  2262. * These MSRs specify bits which the guest must keep fixed (on or off)
  2263. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  2264. * We picked the standard core2 setting.
  2265. */
  2266. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  2267. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  2268. case MSR_IA32_VMX_CR0_FIXED0:
  2269. *pdata = VMXON_CR0_ALWAYSON;
  2270. break;
  2271. case MSR_IA32_VMX_CR0_FIXED1:
  2272. *pdata = -1ULL;
  2273. break;
  2274. case MSR_IA32_VMX_CR4_FIXED0:
  2275. *pdata = VMXON_CR4_ALWAYSON;
  2276. break;
  2277. case MSR_IA32_VMX_CR4_FIXED1:
  2278. *pdata = -1ULL;
  2279. break;
  2280. case MSR_IA32_VMX_VMCS_ENUM:
  2281. *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  2282. break;
  2283. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2284. *pdata = vmx_control_msr(
  2285. vmx->nested.nested_vmx_secondary_ctls_low,
  2286. vmx->nested.nested_vmx_secondary_ctls_high);
  2287. break;
  2288. case MSR_IA32_VMX_EPT_VPID_CAP:
  2289. /* Currently, no nested vpid support */
  2290. *pdata = vmx->nested.nested_vmx_ept_caps;
  2291. break;
  2292. default:
  2293. return 1;
  2294. }
  2295. return 0;
  2296. }
  2297. /*
  2298. * Reads an msr value (of 'msr_index') into 'pdata'.
  2299. * Returns 0 on success, non-0 otherwise.
  2300. * Assumes vcpu_load() was already called.
  2301. */
  2302. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2303. {
  2304. struct shared_msr_entry *msr;
  2305. switch (msr_info->index) {
  2306. #ifdef CONFIG_X86_64
  2307. case MSR_FS_BASE:
  2308. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  2309. break;
  2310. case MSR_GS_BASE:
  2311. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  2312. break;
  2313. case MSR_KERNEL_GS_BASE:
  2314. vmx_load_host_state(to_vmx(vcpu));
  2315. msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
  2316. break;
  2317. #endif
  2318. case MSR_EFER:
  2319. return kvm_get_msr_common(vcpu, msr_info);
  2320. case MSR_IA32_TSC:
  2321. msr_info->data = guest_read_tsc();
  2322. break;
  2323. case MSR_IA32_SYSENTER_CS:
  2324. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  2325. break;
  2326. case MSR_IA32_SYSENTER_EIP:
  2327. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  2328. break;
  2329. case MSR_IA32_SYSENTER_ESP:
  2330. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  2331. break;
  2332. case MSR_IA32_BNDCFGS:
  2333. if (!vmx_mpx_supported())
  2334. return 1;
  2335. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  2336. break;
  2337. case MSR_IA32_FEATURE_CONTROL:
  2338. if (!nested_vmx_allowed(vcpu))
  2339. return 1;
  2340. msr_info->data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
  2341. break;
  2342. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  2343. if (!nested_vmx_allowed(vcpu))
  2344. return 1;
  2345. return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
  2346. case MSR_IA32_XSS:
  2347. if (!vmx_xsaves_supported())
  2348. return 1;
  2349. msr_info->data = vcpu->arch.ia32_xss;
  2350. break;
  2351. case MSR_TSC_AUX:
  2352. if (!to_vmx(vcpu)->rdtscp_enabled)
  2353. return 1;
  2354. /* Otherwise falls through */
  2355. default:
  2356. msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
  2357. if (msr) {
  2358. msr_info->data = msr->data;
  2359. break;
  2360. }
  2361. return kvm_get_msr_common(vcpu, msr_info);
  2362. }
  2363. return 0;
  2364. }
  2365. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  2366. /*
  2367. * Writes msr value into into the appropriate "register".
  2368. * Returns 0 on success, non-0 otherwise.
  2369. * Assumes vcpu_load() was already called.
  2370. */
  2371. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2372. {
  2373. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2374. struct shared_msr_entry *msr;
  2375. int ret = 0;
  2376. u32 msr_index = msr_info->index;
  2377. u64 data = msr_info->data;
  2378. switch (msr_index) {
  2379. case MSR_EFER:
  2380. ret = kvm_set_msr_common(vcpu, msr_info);
  2381. break;
  2382. #ifdef CONFIG_X86_64
  2383. case MSR_FS_BASE:
  2384. vmx_segment_cache_clear(vmx);
  2385. vmcs_writel(GUEST_FS_BASE, data);
  2386. break;
  2387. case MSR_GS_BASE:
  2388. vmx_segment_cache_clear(vmx);
  2389. vmcs_writel(GUEST_GS_BASE, data);
  2390. break;
  2391. case MSR_KERNEL_GS_BASE:
  2392. vmx_load_host_state(vmx);
  2393. vmx->msr_guest_kernel_gs_base = data;
  2394. break;
  2395. #endif
  2396. case MSR_IA32_SYSENTER_CS:
  2397. vmcs_write32(GUEST_SYSENTER_CS, data);
  2398. break;
  2399. case MSR_IA32_SYSENTER_EIP:
  2400. vmcs_writel(GUEST_SYSENTER_EIP, data);
  2401. break;
  2402. case MSR_IA32_SYSENTER_ESP:
  2403. vmcs_writel(GUEST_SYSENTER_ESP, data);
  2404. break;
  2405. case MSR_IA32_BNDCFGS:
  2406. if (!vmx_mpx_supported())
  2407. return 1;
  2408. vmcs_write64(GUEST_BNDCFGS, data);
  2409. break;
  2410. case MSR_IA32_TSC:
  2411. kvm_write_tsc(vcpu, msr_info);
  2412. break;
  2413. case MSR_IA32_CR_PAT:
  2414. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  2415. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  2416. return 1;
  2417. vmcs_write64(GUEST_IA32_PAT, data);
  2418. vcpu->arch.pat = data;
  2419. break;
  2420. }
  2421. ret = kvm_set_msr_common(vcpu, msr_info);
  2422. break;
  2423. case MSR_IA32_TSC_ADJUST:
  2424. ret = kvm_set_msr_common(vcpu, msr_info);
  2425. break;
  2426. case MSR_IA32_FEATURE_CONTROL:
  2427. if (!nested_vmx_allowed(vcpu) ||
  2428. (to_vmx(vcpu)->nested.msr_ia32_feature_control &
  2429. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  2430. return 1;
  2431. vmx->nested.msr_ia32_feature_control = data;
  2432. if (msr_info->host_initiated && data == 0)
  2433. vmx_leave_nested(vcpu);
  2434. break;
  2435. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  2436. return 1; /* they are read-only */
  2437. case MSR_IA32_XSS:
  2438. if (!vmx_xsaves_supported())
  2439. return 1;
  2440. /*
  2441. * The only supported bit as of Skylake is bit 8, but
  2442. * it is not supported on KVM.
  2443. */
  2444. if (data != 0)
  2445. return 1;
  2446. vcpu->arch.ia32_xss = data;
  2447. if (vcpu->arch.ia32_xss != host_xss)
  2448. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  2449. vcpu->arch.ia32_xss, host_xss);
  2450. else
  2451. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  2452. break;
  2453. case MSR_TSC_AUX:
  2454. if (!vmx->rdtscp_enabled)
  2455. return 1;
  2456. /* Check reserved bit, higher 32 bits should be zero */
  2457. if ((data >> 32) != 0)
  2458. return 1;
  2459. /* Otherwise falls through */
  2460. default:
  2461. msr = find_msr_entry(vmx, msr_index);
  2462. if (msr) {
  2463. u64 old_msr_data = msr->data;
  2464. msr->data = data;
  2465. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  2466. preempt_disable();
  2467. ret = kvm_set_shared_msr(msr->index, msr->data,
  2468. msr->mask);
  2469. preempt_enable();
  2470. if (ret)
  2471. msr->data = old_msr_data;
  2472. }
  2473. break;
  2474. }
  2475. ret = kvm_set_msr_common(vcpu, msr_info);
  2476. }
  2477. return ret;
  2478. }
  2479. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  2480. {
  2481. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  2482. switch (reg) {
  2483. case VCPU_REGS_RSP:
  2484. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  2485. break;
  2486. case VCPU_REGS_RIP:
  2487. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  2488. break;
  2489. case VCPU_EXREG_PDPTR:
  2490. if (enable_ept)
  2491. ept_save_pdptrs(vcpu);
  2492. break;
  2493. default:
  2494. break;
  2495. }
  2496. }
  2497. static __init int cpu_has_kvm_support(void)
  2498. {
  2499. return cpu_has_vmx();
  2500. }
  2501. static __init int vmx_disabled_by_bios(void)
  2502. {
  2503. u64 msr;
  2504. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  2505. if (msr & FEATURE_CONTROL_LOCKED) {
  2506. /* launched w/ TXT and VMX disabled */
  2507. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2508. && tboot_enabled())
  2509. return 1;
  2510. /* launched w/o TXT and VMX only enabled w/ TXT */
  2511. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2512. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2513. && !tboot_enabled()) {
  2514. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  2515. "activate TXT before enabling KVM\n");
  2516. return 1;
  2517. }
  2518. /* launched w/o TXT and VMX disabled */
  2519. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2520. && !tboot_enabled())
  2521. return 1;
  2522. }
  2523. return 0;
  2524. }
  2525. static void kvm_cpu_vmxon(u64 addr)
  2526. {
  2527. asm volatile (ASM_VMX_VMXON_RAX
  2528. : : "a"(&addr), "m"(addr)
  2529. : "memory", "cc");
  2530. }
  2531. static int hardware_enable(void)
  2532. {
  2533. int cpu = raw_smp_processor_id();
  2534. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  2535. u64 old, test_bits;
  2536. if (cr4_read_shadow() & X86_CR4_VMXE)
  2537. return -EBUSY;
  2538. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  2539. /*
  2540. * Now we can enable the vmclear operation in kdump
  2541. * since the loaded_vmcss_on_cpu list on this cpu
  2542. * has been initialized.
  2543. *
  2544. * Though the cpu is not in VMX operation now, there
  2545. * is no problem to enable the vmclear operation
  2546. * for the loaded_vmcss_on_cpu list is empty!
  2547. */
  2548. crash_enable_local_vmclear(cpu);
  2549. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  2550. test_bits = FEATURE_CONTROL_LOCKED;
  2551. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  2552. if (tboot_enabled())
  2553. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  2554. if ((old & test_bits) != test_bits) {
  2555. /* enable and lock */
  2556. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  2557. }
  2558. cr4_set_bits(X86_CR4_VMXE);
  2559. if (vmm_exclusive) {
  2560. kvm_cpu_vmxon(phys_addr);
  2561. ept_sync_global();
  2562. }
  2563. native_store_gdt(this_cpu_ptr(&host_gdt));
  2564. return 0;
  2565. }
  2566. static void vmclear_local_loaded_vmcss(void)
  2567. {
  2568. int cpu = raw_smp_processor_id();
  2569. struct loaded_vmcs *v, *n;
  2570. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  2571. loaded_vmcss_on_cpu_link)
  2572. __loaded_vmcs_clear(v);
  2573. }
  2574. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  2575. * tricks.
  2576. */
  2577. static void kvm_cpu_vmxoff(void)
  2578. {
  2579. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  2580. }
  2581. static void hardware_disable(void)
  2582. {
  2583. if (vmm_exclusive) {
  2584. vmclear_local_loaded_vmcss();
  2585. kvm_cpu_vmxoff();
  2586. }
  2587. cr4_clear_bits(X86_CR4_VMXE);
  2588. }
  2589. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  2590. u32 msr, u32 *result)
  2591. {
  2592. u32 vmx_msr_low, vmx_msr_high;
  2593. u32 ctl = ctl_min | ctl_opt;
  2594. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2595. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  2596. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  2597. /* Ensure minimum (required) set of control bits are supported. */
  2598. if (ctl_min & ~ctl)
  2599. return -EIO;
  2600. *result = ctl;
  2601. return 0;
  2602. }
  2603. static __init bool allow_1_setting(u32 msr, u32 ctl)
  2604. {
  2605. u32 vmx_msr_low, vmx_msr_high;
  2606. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2607. return vmx_msr_high & ctl;
  2608. }
  2609. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  2610. {
  2611. u32 vmx_msr_low, vmx_msr_high;
  2612. u32 min, opt, min2, opt2;
  2613. u32 _pin_based_exec_control = 0;
  2614. u32 _cpu_based_exec_control = 0;
  2615. u32 _cpu_based_2nd_exec_control = 0;
  2616. u32 _vmexit_control = 0;
  2617. u32 _vmentry_control = 0;
  2618. min = CPU_BASED_HLT_EXITING |
  2619. #ifdef CONFIG_X86_64
  2620. CPU_BASED_CR8_LOAD_EXITING |
  2621. CPU_BASED_CR8_STORE_EXITING |
  2622. #endif
  2623. CPU_BASED_CR3_LOAD_EXITING |
  2624. CPU_BASED_CR3_STORE_EXITING |
  2625. CPU_BASED_USE_IO_BITMAPS |
  2626. CPU_BASED_MOV_DR_EXITING |
  2627. CPU_BASED_USE_TSC_OFFSETING |
  2628. CPU_BASED_MWAIT_EXITING |
  2629. CPU_BASED_MONITOR_EXITING |
  2630. CPU_BASED_INVLPG_EXITING |
  2631. CPU_BASED_RDPMC_EXITING;
  2632. opt = CPU_BASED_TPR_SHADOW |
  2633. CPU_BASED_USE_MSR_BITMAPS |
  2634. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2635. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  2636. &_cpu_based_exec_control) < 0)
  2637. return -EIO;
  2638. #ifdef CONFIG_X86_64
  2639. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  2640. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  2641. ~CPU_BASED_CR8_STORE_EXITING;
  2642. #endif
  2643. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  2644. min2 = 0;
  2645. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2646. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2647. SECONDARY_EXEC_WBINVD_EXITING |
  2648. SECONDARY_EXEC_ENABLE_VPID |
  2649. SECONDARY_EXEC_ENABLE_EPT |
  2650. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  2651. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  2652. SECONDARY_EXEC_RDTSCP |
  2653. SECONDARY_EXEC_ENABLE_INVPCID |
  2654. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2655. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2656. SECONDARY_EXEC_SHADOW_VMCS |
  2657. SECONDARY_EXEC_XSAVES |
  2658. SECONDARY_EXEC_ENABLE_PML;
  2659. if (adjust_vmx_controls(min2, opt2,
  2660. MSR_IA32_VMX_PROCBASED_CTLS2,
  2661. &_cpu_based_2nd_exec_control) < 0)
  2662. return -EIO;
  2663. }
  2664. #ifndef CONFIG_X86_64
  2665. if (!(_cpu_based_2nd_exec_control &
  2666. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  2667. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  2668. #endif
  2669. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  2670. _cpu_based_2nd_exec_control &= ~(
  2671. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2672. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2673. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  2674. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  2675. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  2676. enabled */
  2677. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  2678. CPU_BASED_CR3_STORE_EXITING |
  2679. CPU_BASED_INVLPG_EXITING);
  2680. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  2681. vmx_capability.ept, vmx_capability.vpid);
  2682. }
  2683. min = VM_EXIT_SAVE_DEBUG_CONTROLS;
  2684. #ifdef CONFIG_X86_64
  2685. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  2686. #endif
  2687. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  2688. VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
  2689. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  2690. &_vmexit_control) < 0)
  2691. return -EIO;
  2692. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  2693. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
  2694. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  2695. &_pin_based_exec_control) < 0)
  2696. return -EIO;
  2697. if (!(_cpu_based_2nd_exec_control &
  2698. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
  2699. !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
  2700. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  2701. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2702. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  2703. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  2704. &_vmentry_control) < 0)
  2705. return -EIO;
  2706. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  2707. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  2708. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  2709. return -EIO;
  2710. #ifdef CONFIG_X86_64
  2711. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  2712. if (vmx_msr_high & (1u<<16))
  2713. return -EIO;
  2714. #endif
  2715. /* Require Write-Back (WB) memory type for VMCS accesses. */
  2716. if (((vmx_msr_high >> 18) & 15) != 6)
  2717. return -EIO;
  2718. vmcs_conf->size = vmx_msr_high & 0x1fff;
  2719. vmcs_conf->order = get_order(vmcs_config.size);
  2720. vmcs_conf->revision_id = vmx_msr_low;
  2721. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  2722. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  2723. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  2724. vmcs_conf->vmexit_ctrl = _vmexit_control;
  2725. vmcs_conf->vmentry_ctrl = _vmentry_control;
  2726. cpu_has_load_ia32_efer =
  2727. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2728. VM_ENTRY_LOAD_IA32_EFER)
  2729. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2730. VM_EXIT_LOAD_IA32_EFER);
  2731. cpu_has_load_perf_global_ctrl =
  2732. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2733. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  2734. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2735. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2736. /*
  2737. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  2738. * but due to arrata below it can't be used. Workaround is to use
  2739. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  2740. *
  2741. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  2742. *
  2743. * AAK155 (model 26)
  2744. * AAP115 (model 30)
  2745. * AAT100 (model 37)
  2746. * BC86,AAY89,BD102 (model 44)
  2747. * BA97 (model 46)
  2748. *
  2749. */
  2750. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  2751. switch (boot_cpu_data.x86_model) {
  2752. case 26:
  2753. case 30:
  2754. case 37:
  2755. case 44:
  2756. case 46:
  2757. cpu_has_load_perf_global_ctrl = false;
  2758. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  2759. "does not work properly. Using workaround\n");
  2760. break;
  2761. default:
  2762. break;
  2763. }
  2764. }
  2765. if (cpu_has_xsaves)
  2766. rdmsrl(MSR_IA32_XSS, host_xss);
  2767. return 0;
  2768. }
  2769. static struct vmcs *alloc_vmcs_cpu(int cpu)
  2770. {
  2771. int node = cpu_to_node(cpu);
  2772. struct page *pages;
  2773. struct vmcs *vmcs;
  2774. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  2775. if (!pages)
  2776. return NULL;
  2777. vmcs = page_address(pages);
  2778. memset(vmcs, 0, vmcs_config.size);
  2779. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  2780. return vmcs;
  2781. }
  2782. static struct vmcs *alloc_vmcs(void)
  2783. {
  2784. return alloc_vmcs_cpu(raw_smp_processor_id());
  2785. }
  2786. static void free_vmcs(struct vmcs *vmcs)
  2787. {
  2788. free_pages((unsigned long)vmcs, vmcs_config.order);
  2789. }
  2790. /*
  2791. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  2792. */
  2793. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  2794. {
  2795. if (!loaded_vmcs->vmcs)
  2796. return;
  2797. loaded_vmcs_clear(loaded_vmcs);
  2798. free_vmcs(loaded_vmcs->vmcs);
  2799. loaded_vmcs->vmcs = NULL;
  2800. }
  2801. static void free_kvm_area(void)
  2802. {
  2803. int cpu;
  2804. for_each_possible_cpu(cpu) {
  2805. free_vmcs(per_cpu(vmxarea, cpu));
  2806. per_cpu(vmxarea, cpu) = NULL;
  2807. }
  2808. }
  2809. static void init_vmcs_shadow_fields(void)
  2810. {
  2811. int i, j;
  2812. /* No checks for read only fields yet */
  2813. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  2814. switch (shadow_read_write_fields[i]) {
  2815. case GUEST_BNDCFGS:
  2816. if (!vmx_mpx_supported())
  2817. continue;
  2818. break;
  2819. default:
  2820. break;
  2821. }
  2822. if (j < i)
  2823. shadow_read_write_fields[j] =
  2824. shadow_read_write_fields[i];
  2825. j++;
  2826. }
  2827. max_shadow_read_write_fields = j;
  2828. /* shadowed fields guest access without vmexit */
  2829. for (i = 0; i < max_shadow_read_write_fields; i++) {
  2830. clear_bit(shadow_read_write_fields[i],
  2831. vmx_vmwrite_bitmap);
  2832. clear_bit(shadow_read_write_fields[i],
  2833. vmx_vmread_bitmap);
  2834. }
  2835. for (i = 0; i < max_shadow_read_only_fields; i++)
  2836. clear_bit(shadow_read_only_fields[i],
  2837. vmx_vmread_bitmap);
  2838. }
  2839. static __init int alloc_kvm_area(void)
  2840. {
  2841. int cpu;
  2842. for_each_possible_cpu(cpu) {
  2843. struct vmcs *vmcs;
  2844. vmcs = alloc_vmcs_cpu(cpu);
  2845. if (!vmcs) {
  2846. free_kvm_area();
  2847. return -ENOMEM;
  2848. }
  2849. per_cpu(vmxarea, cpu) = vmcs;
  2850. }
  2851. return 0;
  2852. }
  2853. static bool emulation_required(struct kvm_vcpu *vcpu)
  2854. {
  2855. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2856. }
  2857. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  2858. struct kvm_segment *save)
  2859. {
  2860. if (!emulate_invalid_guest_state) {
  2861. /*
  2862. * CS and SS RPL should be equal during guest entry according
  2863. * to VMX spec, but in reality it is not always so. Since vcpu
  2864. * is in the middle of the transition from real mode to
  2865. * protected mode it is safe to assume that RPL 0 is a good
  2866. * default value.
  2867. */
  2868. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  2869. save->selector &= ~SEGMENT_RPL_MASK;
  2870. save->dpl = save->selector & SEGMENT_RPL_MASK;
  2871. save->s = 1;
  2872. }
  2873. vmx_set_segment(vcpu, save, seg);
  2874. }
  2875. static void enter_pmode(struct kvm_vcpu *vcpu)
  2876. {
  2877. unsigned long flags;
  2878. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2879. /*
  2880. * Update real mode segment cache. It may be not up-to-date if sement
  2881. * register was written while vcpu was in a guest mode.
  2882. */
  2883. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  2884. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  2885. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  2886. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  2887. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  2888. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  2889. vmx->rmode.vm86_active = 0;
  2890. vmx_segment_cache_clear(vmx);
  2891. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  2892. flags = vmcs_readl(GUEST_RFLAGS);
  2893. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2894. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2895. vmcs_writel(GUEST_RFLAGS, flags);
  2896. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  2897. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  2898. update_exception_bitmap(vcpu);
  2899. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  2900. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  2901. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  2902. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  2903. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  2904. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  2905. }
  2906. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  2907. {
  2908. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2909. struct kvm_segment var = *save;
  2910. var.dpl = 0x3;
  2911. if (seg == VCPU_SREG_CS)
  2912. var.type = 0x3;
  2913. if (!emulate_invalid_guest_state) {
  2914. var.selector = var.base >> 4;
  2915. var.base = var.base & 0xffff0;
  2916. var.limit = 0xffff;
  2917. var.g = 0;
  2918. var.db = 0;
  2919. var.present = 1;
  2920. var.s = 1;
  2921. var.l = 0;
  2922. var.unusable = 0;
  2923. var.type = 0x3;
  2924. var.avl = 0;
  2925. if (save->base & 0xf)
  2926. printk_once(KERN_WARNING "kvm: segment base is not "
  2927. "paragraph aligned when entering "
  2928. "protected mode (seg=%d)", seg);
  2929. }
  2930. vmcs_write16(sf->selector, var.selector);
  2931. vmcs_write32(sf->base, var.base);
  2932. vmcs_write32(sf->limit, var.limit);
  2933. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  2934. }
  2935. static void enter_rmode(struct kvm_vcpu *vcpu)
  2936. {
  2937. unsigned long flags;
  2938. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2939. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  2940. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  2941. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  2942. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  2943. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  2944. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  2945. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  2946. vmx->rmode.vm86_active = 1;
  2947. /*
  2948. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  2949. * vcpu. Warn the user that an update is overdue.
  2950. */
  2951. if (!vcpu->kvm->arch.tss_addr)
  2952. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  2953. "called before entering vcpu\n");
  2954. vmx_segment_cache_clear(vmx);
  2955. vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
  2956. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  2957. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2958. flags = vmcs_readl(GUEST_RFLAGS);
  2959. vmx->rmode.save_rflags = flags;
  2960. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2961. vmcs_writel(GUEST_RFLAGS, flags);
  2962. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  2963. update_exception_bitmap(vcpu);
  2964. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  2965. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  2966. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  2967. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  2968. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  2969. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  2970. kvm_mmu_reset_context(vcpu);
  2971. }
  2972. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  2973. {
  2974. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2975. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  2976. if (!msr)
  2977. return;
  2978. /*
  2979. * Force kernel_gs_base reloading before EFER changes, as control
  2980. * of this msr depends on is_long_mode().
  2981. */
  2982. vmx_load_host_state(to_vmx(vcpu));
  2983. vcpu->arch.efer = efer;
  2984. if (efer & EFER_LMA) {
  2985. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  2986. msr->data = efer;
  2987. } else {
  2988. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  2989. msr->data = efer & ~EFER_LME;
  2990. }
  2991. setup_msrs(vmx);
  2992. }
  2993. #ifdef CONFIG_X86_64
  2994. static void enter_lmode(struct kvm_vcpu *vcpu)
  2995. {
  2996. u32 guest_tr_ar;
  2997. vmx_segment_cache_clear(to_vmx(vcpu));
  2998. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  2999. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  3000. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  3001. __func__);
  3002. vmcs_write32(GUEST_TR_AR_BYTES,
  3003. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  3004. | VMX_AR_TYPE_BUSY_64_TSS);
  3005. }
  3006. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  3007. }
  3008. static void exit_lmode(struct kvm_vcpu *vcpu)
  3009. {
  3010. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  3011. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  3012. }
  3013. #endif
  3014. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  3015. {
  3016. vpid_sync_context(to_vmx(vcpu));
  3017. if (enable_ept) {
  3018. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  3019. return;
  3020. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  3021. }
  3022. }
  3023. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  3024. {
  3025. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  3026. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  3027. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  3028. }
  3029. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  3030. {
  3031. if (enable_ept && is_paging(vcpu))
  3032. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  3033. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  3034. }
  3035. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  3036. {
  3037. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  3038. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  3039. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  3040. }
  3041. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  3042. {
  3043. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  3044. if (!test_bit(VCPU_EXREG_PDPTR,
  3045. (unsigned long *)&vcpu->arch.regs_dirty))
  3046. return;
  3047. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  3048. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  3049. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  3050. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  3051. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  3052. }
  3053. }
  3054. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  3055. {
  3056. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  3057. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  3058. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  3059. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  3060. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  3061. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  3062. }
  3063. __set_bit(VCPU_EXREG_PDPTR,
  3064. (unsigned long *)&vcpu->arch.regs_avail);
  3065. __set_bit(VCPU_EXREG_PDPTR,
  3066. (unsigned long *)&vcpu->arch.regs_dirty);
  3067. }
  3068. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  3069. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  3070. unsigned long cr0,
  3071. struct kvm_vcpu *vcpu)
  3072. {
  3073. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  3074. vmx_decache_cr3(vcpu);
  3075. if (!(cr0 & X86_CR0_PG)) {
  3076. /* From paging/starting to nonpaging */
  3077. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3078. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  3079. (CPU_BASED_CR3_LOAD_EXITING |
  3080. CPU_BASED_CR3_STORE_EXITING));
  3081. vcpu->arch.cr0 = cr0;
  3082. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3083. } else if (!is_paging(vcpu)) {
  3084. /* From nonpaging to paging */
  3085. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3086. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  3087. ~(CPU_BASED_CR3_LOAD_EXITING |
  3088. CPU_BASED_CR3_STORE_EXITING));
  3089. vcpu->arch.cr0 = cr0;
  3090. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3091. }
  3092. if (!(cr0 & X86_CR0_WP))
  3093. *hw_cr0 &= ~X86_CR0_WP;
  3094. }
  3095. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  3096. {
  3097. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3098. unsigned long hw_cr0;
  3099. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  3100. if (enable_unrestricted_guest)
  3101. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  3102. else {
  3103. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  3104. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  3105. enter_pmode(vcpu);
  3106. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  3107. enter_rmode(vcpu);
  3108. }
  3109. #ifdef CONFIG_X86_64
  3110. if (vcpu->arch.efer & EFER_LME) {
  3111. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  3112. enter_lmode(vcpu);
  3113. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  3114. exit_lmode(vcpu);
  3115. }
  3116. #endif
  3117. if (enable_ept)
  3118. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  3119. if (!vcpu->fpu_active)
  3120. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  3121. vmcs_writel(CR0_READ_SHADOW, cr0);
  3122. vmcs_writel(GUEST_CR0, hw_cr0);
  3123. vcpu->arch.cr0 = cr0;
  3124. /* depends on vcpu->arch.cr0 to be set to a new value */
  3125. vmx->emulation_required = emulation_required(vcpu);
  3126. }
  3127. static u64 construct_eptp(unsigned long root_hpa)
  3128. {
  3129. u64 eptp;
  3130. /* TODO write the value reading from MSR */
  3131. eptp = VMX_EPT_DEFAULT_MT |
  3132. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  3133. if (enable_ept_ad_bits)
  3134. eptp |= VMX_EPT_AD_ENABLE_BIT;
  3135. eptp |= (root_hpa & PAGE_MASK);
  3136. return eptp;
  3137. }
  3138. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  3139. {
  3140. unsigned long guest_cr3;
  3141. u64 eptp;
  3142. guest_cr3 = cr3;
  3143. if (enable_ept) {
  3144. eptp = construct_eptp(cr3);
  3145. vmcs_write64(EPT_POINTER, eptp);
  3146. if (is_paging(vcpu) || is_guest_mode(vcpu))
  3147. guest_cr3 = kvm_read_cr3(vcpu);
  3148. else
  3149. guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
  3150. ept_load_pdptrs(vcpu);
  3151. }
  3152. vmx_flush_tlb(vcpu);
  3153. vmcs_writel(GUEST_CR3, guest_cr3);
  3154. }
  3155. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  3156. {
  3157. /*
  3158. * Pass through host's Machine Check Enable value to hw_cr4, which
  3159. * is in force while we are in guest mode. Do not let guests control
  3160. * this bit, even if host CR4.MCE == 0.
  3161. */
  3162. unsigned long hw_cr4 =
  3163. (cr4_read_shadow() & X86_CR4_MCE) |
  3164. (cr4 & ~X86_CR4_MCE) |
  3165. (to_vmx(vcpu)->rmode.vm86_active ?
  3166. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  3167. if (cr4 & X86_CR4_VMXE) {
  3168. /*
  3169. * To use VMXON (and later other VMX instructions), a guest
  3170. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  3171. * So basically the check on whether to allow nested VMX
  3172. * is here.
  3173. */
  3174. if (!nested_vmx_allowed(vcpu))
  3175. return 1;
  3176. }
  3177. if (to_vmx(vcpu)->nested.vmxon &&
  3178. ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
  3179. return 1;
  3180. vcpu->arch.cr4 = cr4;
  3181. if (enable_ept) {
  3182. if (!is_paging(vcpu)) {
  3183. hw_cr4 &= ~X86_CR4_PAE;
  3184. hw_cr4 |= X86_CR4_PSE;
  3185. /*
  3186. * SMEP/SMAP is disabled if CPU is in non-paging mode
  3187. * in hardware. However KVM always uses paging mode to
  3188. * emulate guest non-paging mode with TDP.
  3189. * To emulate this behavior, SMEP/SMAP needs to be
  3190. * manually disabled when guest switches to non-paging
  3191. * mode.
  3192. */
  3193. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
  3194. } else if (!(cr4 & X86_CR4_PAE)) {
  3195. hw_cr4 &= ~X86_CR4_PAE;
  3196. }
  3197. }
  3198. vmcs_writel(CR4_READ_SHADOW, cr4);
  3199. vmcs_writel(GUEST_CR4, hw_cr4);
  3200. return 0;
  3201. }
  3202. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  3203. struct kvm_segment *var, int seg)
  3204. {
  3205. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3206. u32 ar;
  3207. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  3208. *var = vmx->rmode.segs[seg];
  3209. if (seg == VCPU_SREG_TR
  3210. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  3211. return;
  3212. var->base = vmx_read_guest_seg_base(vmx, seg);
  3213. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3214. return;
  3215. }
  3216. var->base = vmx_read_guest_seg_base(vmx, seg);
  3217. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  3218. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3219. ar = vmx_read_guest_seg_ar(vmx, seg);
  3220. var->unusable = (ar >> 16) & 1;
  3221. var->type = ar & 15;
  3222. var->s = (ar >> 4) & 1;
  3223. var->dpl = (ar >> 5) & 3;
  3224. /*
  3225. * Some userspaces do not preserve unusable property. Since usable
  3226. * segment has to be present according to VMX spec we can use present
  3227. * property to amend userspace bug by making unusable segment always
  3228. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  3229. * segment as unusable.
  3230. */
  3231. var->present = !var->unusable;
  3232. var->avl = (ar >> 12) & 1;
  3233. var->l = (ar >> 13) & 1;
  3234. var->db = (ar >> 14) & 1;
  3235. var->g = (ar >> 15) & 1;
  3236. }
  3237. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3238. {
  3239. struct kvm_segment s;
  3240. if (to_vmx(vcpu)->rmode.vm86_active) {
  3241. vmx_get_segment(vcpu, &s, seg);
  3242. return s.base;
  3243. }
  3244. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  3245. }
  3246. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  3247. {
  3248. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3249. if (unlikely(vmx->rmode.vm86_active))
  3250. return 0;
  3251. else {
  3252. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  3253. return VMX_AR_DPL(ar);
  3254. }
  3255. }
  3256. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  3257. {
  3258. u32 ar;
  3259. if (var->unusable || !var->present)
  3260. ar = 1 << 16;
  3261. else {
  3262. ar = var->type & 15;
  3263. ar |= (var->s & 1) << 4;
  3264. ar |= (var->dpl & 3) << 5;
  3265. ar |= (var->present & 1) << 7;
  3266. ar |= (var->avl & 1) << 12;
  3267. ar |= (var->l & 1) << 13;
  3268. ar |= (var->db & 1) << 14;
  3269. ar |= (var->g & 1) << 15;
  3270. }
  3271. return ar;
  3272. }
  3273. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  3274. struct kvm_segment *var, int seg)
  3275. {
  3276. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3277. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3278. vmx_segment_cache_clear(vmx);
  3279. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  3280. vmx->rmode.segs[seg] = *var;
  3281. if (seg == VCPU_SREG_TR)
  3282. vmcs_write16(sf->selector, var->selector);
  3283. else if (var->s)
  3284. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  3285. goto out;
  3286. }
  3287. vmcs_writel(sf->base, var->base);
  3288. vmcs_write32(sf->limit, var->limit);
  3289. vmcs_write16(sf->selector, var->selector);
  3290. /*
  3291. * Fix the "Accessed" bit in AR field of segment registers for older
  3292. * qemu binaries.
  3293. * IA32 arch specifies that at the time of processor reset the
  3294. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  3295. * is setting it to 0 in the userland code. This causes invalid guest
  3296. * state vmexit when "unrestricted guest" mode is turned on.
  3297. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  3298. * tree. Newer qemu binaries with that qemu fix would not need this
  3299. * kvm hack.
  3300. */
  3301. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  3302. var->type |= 0x1; /* Accessed */
  3303. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  3304. out:
  3305. vmx->emulation_required = emulation_required(vcpu);
  3306. }
  3307. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  3308. {
  3309. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  3310. *db = (ar >> 14) & 1;
  3311. *l = (ar >> 13) & 1;
  3312. }
  3313. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3314. {
  3315. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  3316. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  3317. }
  3318. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3319. {
  3320. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  3321. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  3322. }
  3323. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3324. {
  3325. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  3326. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  3327. }
  3328. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3329. {
  3330. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  3331. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  3332. }
  3333. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  3334. {
  3335. struct kvm_segment var;
  3336. u32 ar;
  3337. vmx_get_segment(vcpu, &var, seg);
  3338. var.dpl = 0x3;
  3339. if (seg == VCPU_SREG_CS)
  3340. var.type = 0x3;
  3341. ar = vmx_segment_access_rights(&var);
  3342. if (var.base != (var.selector << 4))
  3343. return false;
  3344. if (var.limit != 0xffff)
  3345. return false;
  3346. if (ar != 0xf3)
  3347. return false;
  3348. return true;
  3349. }
  3350. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  3351. {
  3352. struct kvm_segment cs;
  3353. unsigned int cs_rpl;
  3354. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3355. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  3356. if (cs.unusable)
  3357. return false;
  3358. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  3359. return false;
  3360. if (!cs.s)
  3361. return false;
  3362. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  3363. if (cs.dpl > cs_rpl)
  3364. return false;
  3365. } else {
  3366. if (cs.dpl != cs_rpl)
  3367. return false;
  3368. }
  3369. if (!cs.present)
  3370. return false;
  3371. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  3372. return true;
  3373. }
  3374. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  3375. {
  3376. struct kvm_segment ss;
  3377. unsigned int ss_rpl;
  3378. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  3379. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  3380. if (ss.unusable)
  3381. return true;
  3382. if (ss.type != 3 && ss.type != 7)
  3383. return false;
  3384. if (!ss.s)
  3385. return false;
  3386. if (ss.dpl != ss_rpl) /* DPL != RPL */
  3387. return false;
  3388. if (!ss.present)
  3389. return false;
  3390. return true;
  3391. }
  3392. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  3393. {
  3394. struct kvm_segment var;
  3395. unsigned int rpl;
  3396. vmx_get_segment(vcpu, &var, seg);
  3397. rpl = var.selector & SEGMENT_RPL_MASK;
  3398. if (var.unusable)
  3399. return true;
  3400. if (!var.s)
  3401. return false;
  3402. if (!var.present)
  3403. return false;
  3404. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  3405. if (var.dpl < rpl) /* DPL < RPL */
  3406. return false;
  3407. }
  3408. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  3409. * rights flags
  3410. */
  3411. return true;
  3412. }
  3413. static bool tr_valid(struct kvm_vcpu *vcpu)
  3414. {
  3415. struct kvm_segment tr;
  3416. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  3417. if (tr.unusable)
  3418. return false;
  3419. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  3420. return false;
  3421. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  3422. return false;
  3423. if (!tr.present)
  3424. return false;
  3425. return true;
  3426. }
  3427. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  3428. {
  3429. struct kvm_segment ldtr;
  3430. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  3431. if (ldtr.unusable)
  3432. return true;
  3433. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  3434. return false;
  3435. if (ldtr.type != 2)
  3436. return false;
  3437. if (!ldtr.present)
  3438. return false;
  3439. return true;
  3440. }
  3441. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  3442. {
  3443. struct kvm_segment cs, ss;
  3444. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3445. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  3446. return ((cs.selector & SEGMENT_RPL_MASK) ==
  3447. (ss.selector & SEGMENT_RPL_MASK));
  3448. }
  3449. /*
  3450. * Check if guest state is valid. Returns true if valid, false if
  3451. * not.
  3452. * We assume that registers are always usable
  3453. */
  3454. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  3455. {
  3456. if (enable_unrestricted_guest)
  3457. return true;
  3458. /* real mode guest state checks */
  3459. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  3460. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  3461. return false;
  3462. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  3463. return false;
  3464. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  3465. return false;
  3466. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  3467. return false;
  3468. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  3469. return false;
  3470. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  3471. return false;
  3472. } else {
  3473. /* protected mode guest state checks */
  3474. if (!cs_ss_rpl_check(vcpu))
  3475. return false;
  3476. if (!code_segment_valid(vcpu))
  3477. return false;
  3478. if (!stack_segment_valid(vcpu))
  3479. return false;
  3480. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  3481. return false;
  3482. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  3483. return false;
  3484. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  3485. return false;
  3486. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  3487. return false;
  3488. if (!tr_valid(vcpu))
  3489. return false;
  3490. if (!ldtr_valid(vcpu))
  3491. return false;
  3492. }
  3493. /* TODO:
  3494. * - Add checks on RIP
  3495. * - Add checks on RFLAGS
  3496. */
  3497. return true;
  3498. }
  3499. static int init_rmode_tss(struct kvm *kvm)
  3500. {
  3501. gfn_t fn;
  3502. u16 data = 0;
  3503. int idx, r;
  3504. idx = srcu_read_lock(&kvm->srcu);
  3505. fn = kvm->arch.tss_addr >> PAGE_SHIFT;
  3506. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  3507. if (r < 0)
  3508. goto out;
  3509. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  3510. r = kvm_write_guest_page(kvm, fn++, &data,
  3511. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  3512. if (r < 0)
  3513. goto out;
  3514. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  3515. if (r < 0)
  3516. goto out;
  3517. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  3518. if (r < 0)
  3519. goto out;
  3520. data = ~0;
  3521. r = kvm_write_guest_page(kvm, fn, &data,
  3522. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  3523. sizeof(u8));
  3524. out:
  3525. srcu_read_unlock(&kvm->srcu, idx);
  3526. return r;
  3527. }
  3528. static int init_rmode_identity_map(struct kvm *kvm)
  3529. {
  3530. int i, idx, r = 0;
  3531. pfn_t identity_map_pfn;
  3532. u32 tmp;
  3533. if (!enable_ept)
  3534. return 0;
  3535. /* Protect kvm->arch.ept_identity_pagetable_done. */
  3536. mutex_lock(&kvm->slots_lock);
  3537. if (likely(kvm->arch.ept_identity_pagetable_done))
  3538. goto out2;
  3539. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  3540. r = alloc_identity_pagetable(kvm);
  3541. if (r < 0)
  3542. goto out2;
  3543. idx = srcu_read_lock(&kvm->srcu);
  3544. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  3545. if (r < 0)
  3546. goto out;
  3547. /* Set up identity-mapping pagetable for EPT in real mode */
  3548. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  3549. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  3550. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  3551. r = kvm_write_guest_page(kvm, identity_map_pfn,
  3552. &tmp, i * sizeof(tmp), sizeof(tmp));
  3553. if (r < 0)
  3554. goto out;
  3555. }
  3556. kvm->arch.ept_identity_pagetable_done = true;
  3557. out:
  3558. srcu_read_unlock(&kvm->srcu, idx);
  3559. out2:
  3560. mutex_unlock(&kvm->slots_lock);
  3561. return r;
  3562. }
  3563. static void seg_setup(int seg)
  3564. {
  3565. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3566. unsigned int ar;
  3567. vmcs_write16(sf->selector, 0);
  3568. vmcs_writel(sf->base, 0);
  3569. vmcs_write32(sf->limit, 0xffff);
  3570. ar = 0x93;
  3571. if (seg == VCPU_SREG_CS)
  3572. ar |= 0x08; /* code segment */
  3573. vmcs_write32(sf->ar_bytes, ar);
  3574. }
  3575. static int alloc_apic_access_page(struct kvm *kvm)
  3576. {
  3577. struct page *page;
  3578. struct kvm_userspace_memory_region kvm_userspace_mem;
  3579. int r = 0;
  3580. mutex_lock(&kvm->slots_lock);
  3581. if (kvm->arch.apic_access_page_done)
  3582. goto out;
  3583. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  3584. kvm_userspace_mem.flags = 0;
  3585. kvm_userspace_mem.guest_phys_addr = APIC_DEFAULT_PHYS_BASE;
  3586. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3587. r = __x86_set_memory_region(kvm, &kvm_userspace_mem);
  3588. if (r)
  3589. goto out;
  3590. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  3591. if (is_error_page(page)) {
  3592. r = -EFAULT;
  3593. goto out;
  3594. }
  3595. /*
  3596. * Do not pin the page in memory, so that memory hot-unplug
  3597. * is able to migrate it.
  3598. */
  3599. put_page(page);
  3600. kvm->arch.apic_access_page_done = true;
  3601. out:
  3602. mutex_unlock(&kvm->slots_lock);
  3603. return r;
  3604. }
  3605. static int alloc_identity_pagetable(struct kvm *kvm)
  3606. {
  3607. /* Called with kvm->slots_lock held. */
  3608. struct kvm_userspace_memory_region kvm_userspace_mem;
  3609. int r = 0;
  3610. BUG_ON(kvm->arch.ept_identity_pagetable_done);
  3611. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  3612. kvm_userspace_mem.flags = 0;
  3613. kvm_userspace_mem.guest_phys_addr =
  3614. kvm->arch.ept_identity_map_addr;
  3615. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3616. r = __x86_set_memory_region(kvm, &kvm_userspace_mem);
  3617. return r;
  3618. }
  3619. static void allocate_vpid(struct vcpu_vmx *vmx)
  3620. {
  3621. int vpid;
  3622. vmx->vpid = 0;
  3623. if (!enable_vpid)
  3624. return;
  3625. spin_lock(&vmx_vpid_lock);
  3626. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  3627. if (vpid < VMX_NR_VPIDS) {
  3628. vmx->vpid = vpid;
  3629. __set_bit(vpid, vmx_vpid_bitmap);
  3630. }
  3631. spin_unlock(&vmx_vpid_lock);
  3632. }
  3633. static void free_vpid(struct vcpu_vmx *vmx)
  3634. {
  3635. if (!enable_vpid)
  3636. return;
  3637. spin_lock(&vmx_vpid_lock);
  3638. if (vmx->vpid != 0)
  3639. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  3640. spin_unlock(&vmx_vpid_lock);
  3641. }
  3642. #define MSR_TYPE_R 1
  3643. #define MSR_TYPE_W 2
  3644. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  3645. u32 msr, int type)
  3646. {
  3647. int f = sizeof(unsigned long);
  3648. if (!cpu_has_vmx_msr_bitmap())
  3649. return;
  3650. /*
  3651. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3652. * have the write-low and read-high bitmap offsets the wrong way round.
  3653. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3654. */
  3655. if (msr <= 0x1fff) {
  3656. if (type & MSR_TYPE_R)
  3657. /* read-low */
  3658. __clear_bit(msr, msr_bitmap + 0x000 / f);
  3659. if (type & MSR_TYPE_W)
  3660. /* write-low */
  3661. __clear_bit(msr, msr_bitmap + 0x800 / f);
  3662. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3663. msr &= 0x1fff;
  3664. if (type & MSR_TYPE_R)
  3665. /* read-high */
  3666. __clear_bit(msr, msr_bitmap + 0x400 / f);
  3667. if (type & MSR_TYPE_W)
  3668. /* write-high */
  3669. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  3670. }
  3671. }
  3672. static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  3673. u32 msr, int type)
  3674. {
  3675. int f = sizeof(unsigned long);
  3676. if (!cpu_has_vmx_msr_bitmap())
  3677. return;
  3678. /*
  3679. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3680. * have the write-low and read-high bitmap offsets the wrong way round.
  3681. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3682. */
  3683. if (msr <= 0x1fff) {
  3684. if (type & MSR_TYPE_R)
  3685. /* read-low */
  3686. __set_bit(msr, msr_bitmap + 0x000 / f);
  3687. if (type & MSR_TYPE_W)
  3688. /* write-low */
  3689. __set_bit(msr, msr_bitmap + 0x800 / f);
  3690. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3691. msr &= 0x1fff;
  3692. if (type & MSR_TYPE_R)
  3693. /* read-high */
  3694. __set_bit(msr, msr_bitmap + 0x400 / f);
  3695. if (type & MSR_TYPE_W)
  3696. /* write-high */
  3697. __set_bit(msr, msr_bitmap + 0xc00 / f);
  3698. }
  3699. }
  3700. /*
  3701. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  3702. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  3703. */
  3704. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  3705. unsigned long *msr_bitmap_nested,
  3706. u32 msr, int type)
  3707. {
  3708. int f = sizeof(unsigned long);
  3709. if (!cpu_has_vmx_msr_bitmap()) {
  3710. WARN_ON(1);
  3711. return;
  3712. }
  3713. /*
  3714. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3715. * have the write-low and read-high bitmap offsets the wrong way round.
  3716. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3717. */
  3718. if (msr <= 0x1fff) {
  3719. if (type & MSR_TYPE_R &&
  3720. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  3721. /* read-low */
  3722. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  3723. if (type & MSR_TYPE_W &&
  3724. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  3725. /* write-low */
  3726. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  3727. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3728. msr &= 0x1fff;
  3729. if (type & MSR_TYPE_R &&
  3730. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  3731. /* read-high */
  3732. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  3733. if (type & MSR_TYPE_W &&
  3734. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  3735. /* write-high */
  3736. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  3737. }
  3738. }
  3739. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  3740. {
  3741. if (!longmode_only)
  3742. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
  3743. msr, MSR_TYPE_R | MSR_TYPE_W);
  3744. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
  3745. msr, MSR_TYPE_R | MSR_TYPE_W);
  3746. }
  3747. static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
  3748. {
  3749. __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3750. msr, MSR_TYPE_R);
  3751. __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3752. msr, MSR_TYPE_R);
  3753. }
  3754. static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
  3755. {
  3756. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3757. msr, MSR_TYPE_R);
  3758. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3759. msr, MSR_TYPE_R);
  3760. }
  3761. static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
  3762. {
  3763. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3764. msr, MSR_TYPE_W);
  3765. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3766. msr, MSR_TYPE_W);
  3767. }
  3768. static int vmx_vm_has_apicv(struct kvm *kvm)
  3769. {
  3770. return enable_apicv && irqchip_in_kernel(kvm);
  3771. }
  3772. static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  3773. {
  3774. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3775. int max_irr;
  3776. void *vapic_page;
  3777. u16 status;
  3778. if (vmx->nested.pi_desc &&
  3779. vmx->nested.pi_pending) {
  3780. vmx->nested.pi_pending = false;
  3781. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  3782. return 0;
  3783. max_irr = find_last_bit(
  3784. (unsigned long *)vmx->nested.pi_desc->pir, 256);
  3785. if (max_irr == 256)
  3786. return 0;
  3787. vapic_page = kmap(vmx->nested.virtual_apic_page);
  3788. if (!vapic_page) {
  3789. WARN_ON(1);
  3790. return -ENOMEM;
  3791. }
  3792. __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
  3793. kunmap(vmx->nested.virtual_apic_page);
  3794. status = vmcs_read16(GUEST_INTR_STATUS);
  3795. if ((u8)max_irr > ((u8)status & 0xff)) {
  3796. status &= ~0xff;
  3797. status |= (u8)max_irr;
  3798. vmcs_write16(GUEST_INTR_STATUS, status);
  3799. }
  3800. }
  3801. return 0;
  3802. }
  3803. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
  3804. {
  3805. #ifdef CONFIG_SMP
  3806. if (vcpu->mode == IN_GUEST_MODE) {
  3807. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
  3808. POSTED_INTR_VECTOR);
  3809. return true;
  3810. }
  3811. #endif
  3812. return false;
  3813. }
  3814. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  3815. int vector)
  3816. {
  3817. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3818. if (is_guest_mode(vcpu) &&
  3819. vector == vmx->nested.posted_intr_nv) {
  3820. /* the PIR and ON have been set by L1. */
  3821. kvm_vcpu_trigger_posted_interrupt(vcpu);
  3822. /*
  3823. * If a posted intr is not recognized by hardware,
  3824. * we will accomplish it in the next vmentry.
  3825. */
  3826. vmx->nested.pi_pending = true;
  3827. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3828. return 0;
  3829. }
  3830. return -1;
  3831. }
  3832. /*
  3833. * Send interrupt to vcpu via posted interrupt way.
  3834. * 1. If target vcpu is running(non-root mode), send posted interrupt
  3835. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  3836. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  3837. * interrupt from PIR in next vmentry.
  3838. */
  3839. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  3840. {
  3841. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3842. int r;
  3843. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  3844. if (!r)
  3845. return;
  3846. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  3847. return;
  3848. r = pi_test_and_set_on(&vmx->pi_desc);
  3849. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3850. if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
  3851. kvm_vcpu_kick(vcpu);
  3852. }
  3853. static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  3854. {
  3855. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3856. if (!pi_test_and_clear_on(&vmx->pi_desc))
  3857. return;
  3858. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
  3859. }
  3860. static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
  3861. {
  3862. return;
  3863. }
  3864. /*
  3865. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  3866. * will not change in the lifetime of the guest.
  3867. * Note that host-state that does change is set elsewhere. E.g., host-state
  3868. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  3869. */
  3870. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  3871. {
  3872. u32 low32, high32;
  3873. unsigned long tmpl;
  3874. struct desc_ptr dt;
  3875. unsigned long cr4;
  3876. vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
  3877. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  3878. /* Save the most likely value for this task's CR4 in the VMCS. */
  3879. cr4 = cr4_read_shadow();
  3880. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  3881. vmx->host_state.vmcs_host_cr4 = cr4;
  3882. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  3883. #ifdef CONFIG_X86_64
  3884. /*
  3885. * Load null selectors, so we can avoid reloading them in
  3886. * __vmx_load_host_state(), in case userspace uses the null selectors
  3887. * too (the expected case).
  3888. */
  3889. vmcs_write16(HOST_DS_SELECTOR, 0);
  3890. vmcs_write16(HOST_ES_SELECTOR, 0);
  3891. #else
  3892. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3893. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3894. #endif
  3895. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3896. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  3897. native_store_idt(&dt);
  3898. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  3899. vmx->host_idt_base = dt.address;
  3900. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  3901. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  3902. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  3903. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  3904. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  3905. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  3906. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  3907. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  3908. }
  3909. }
  3910. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  3911. {
  3912. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  3913. if (enable_ept)
  3914. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  3915. if (is_guest_mode(&vmx->vcpu))
  3916. vmx->vcpu.arch.cr4_guest_owned_bits &=
  3917. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  3918. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  3919. }
  3920. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  3921. {
  3922. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  3923. if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
  3924. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  3925. return pin_based_exec_ctrl;
  3926. }
  3927. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  3928. {
  3929. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  3930. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  3931. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  3932. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  3933. exec_control &= ~CPU_BASED_TPR_SHADOW;
  3934. #ifdef CONFIG_X86_64
  3935. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  3936. CPU_BASED_CR8_LOAD_EXITING;
  3937. #endif
  3938. }
  3939. if (!enable_ept)
  3940. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  3941. CPU_BASED_CR3_LOAD_EXITING |
  3942. CPU_BASED_INVLPG_EXITING;
  3943. return exec_control;
  3944. }
  3945. static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
  3946. {
  3947. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  3948. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  3949. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3950. if (vmx->vpid == 0)
  3951. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  3952. if (!enable_ept) {
  3953. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  3954. enable_unrestricted_guest = 0;
  3955. /* Enable INVPCID for non-ept guests may cause performance regression. */
  3956. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  3957. }
  3958. if (!enable_unrestricted_guest)
  3959. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3960. if (!ple_gap)
  3961. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  3962. if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
  3963. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3964. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3965. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  3966. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  3967. (handle_vmptrld).
  3968. We can NOT enable shadow_vmcs here because we don't have yet
  3969. a current VMCS12
  3970. */
  3971. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  3972. /* PML is enabled/disabled in creating/destorying vcpu */
  3973. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  3974. return exec_control;
  3975. }
  3976. static void ept_set_mmio_spte_mask(void)
  3977. {
  3978. /*
  3979. * EPT Misconfigurations can be generated if the value of bits 2:0
  3980. * of an EPT paging-structure entry is 110b (write/execute).
  3981. * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
  3982. * spte.
  3983. */
  3984. kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
  3985. }
  3986. #define VMX_XSS_EXIT_BITMAP 0
  3987. /*
  3988. * Sets up the vmcs for emulated real mode.
  3989. */
  3990. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  3991. {
  3992. #ifdef CONFIG_X86_64
  3993. unsigned long a;
  3994. #endif
  3995. int i;
  3996. /* I/O */
  3997. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  3998. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  3999. if (enable_shadow_vmcs) {
  4000. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  4001. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  4002. }
  4003. if (cpu_has_vmx_msr_bitmap())
  4004. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  4005. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  4006. /* Control */
  4007. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  4008. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  4009. if (cpu_has_secondary_exec_ctrls()) {
  4010. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  4011. vmx_secondary_exec_control(vmx));
  4012. }
  4013. if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
  4014. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  4015. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  4016. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  4017. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  4018. vmcs_write16(GUEST_INTR_STATUS, 0);
  4019. vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  4020. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  4021. }
  4022. if (ple_gap) {
  4023. vmcs_write32(PLE_GAP, ple_gap);
  4024. vmx->ple_window = ple_window;
  4025. vmx->ple_window_dirty = true;
  4026. }
  4027. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  4028. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  4029. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  4030. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  4031. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  4032. vmx_set_constant_host_state(vmx);
  4033. #ifdef CONFIG_X86_64
  4034. rdmsrl(MSR_FS_BASE, a);
  4035. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  4036. rdmsrl(MSR_GS_BASE, a);
  4037. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  4038. #else
  4039. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  4040. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  4041. #endif
  4042. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  4043. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  4044. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  4045. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  4046. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  4047. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  4048. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  4049. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  4050. u32 index = vmx_msr_index[i];
  4051. u32 data_low, data_high;
  4052. int j = vmx->nmsrs;
  4053. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  4054. continue;
  4055. if (wrmsr_safe(index, data_low, data_high) < 0)
  4056. continue;
  4057. vmx->guest_msrs[j].index = i;
  4058. vmx->guest_msrs[j].data = 0;
  4059. vmx->guest_msrs[j].mask = -1ull;
  4060. ++vmx->nmsrs;
  4061. }
  4062. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  4063. /* 22.2.1, 20.8.1 */
  4064. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  4065. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  4066. set_cr4_guest_host_mask(vmx);
  4067. if (vmx_xsaves_supported())
  4068. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  4069. return 0;
  4070. }
  4071. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  4072. {
  4073. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4074. struct msr_data apic_base_msr;
  4075. u64 cr0;
  4076. vmx->rmode.vm86_active = 0;
  4077. vmx->soft_vnmi_blocked = 0;
  4078. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  4079. kvm_set_cr8(vcpu, 0);
  4080. if (!init_event) {
  4081. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  4082. MSR_IA32_APICBASE_ENABLE;
  4083. if (kvm_vcpu_is_reset_bsp(vcpu))
  4084. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  4085. apic_base_msr.host_initiated = true;
  4086. kvm_set_apic_base(vcpu, &apic_base_msr);
  4087. }
  4088. vmx_segment_cache_clear(vmx);
  4089. seg_setup(VCPU_SREG_CS);
  4090. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  4091. vmcs_write32(GUEST_CS_BASE, 0xffff0000);
  4092. seg_setup(VCPU_SREG_DS);
  4093. seg_setup(VCPU_SREG_ES);
  4094. seg_setup(VCPU_SREG_FS);
  4095. seg_setup(VCPU_SREG_GS);
  4096. seg_setup(VCPU_SREG_SS);
  4097. vmcs_write16(GUEST_TR_SELECTOR, 0);
  4098. vmcs_writel(GUEST_TR_BASE, 0);
  4099. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  4100. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4101. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  4102. vmcs_writel(GUEST_LDTR_BASE, 0);
  4103. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  4104. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  4105. if (!init_event) {
  4106. vmcs_write32(GUEST_SYSENTER_CS, 0);
  4107. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  4108. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  4109. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  4110. }
  4111. vmcs_writel(GUEST_RFLAGS, 0x02);
  4112. kvm_rip_write(vcpu, 0xfff0);
  4113. vmcs_writel(GUEST_GDTR_BASE, 0);
  4114. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  4115. vmcs_writel(GUEST_IDTR_BASE, 0);
  4116. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  4117. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  4118. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  4119. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  4120. setup_msrs(vmx);
  4121. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  4122. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  4123. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  4124. if (vm_need_tpr_shadow(vcpu->kvm))
  4125. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  4126. __pa(vcpu->arch.apic->regs));
  4127. vmcs_write32(TPR_THRESHOLD, 0);
  4128. }
  4129. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  4130. if (vmx_vm_has_apicv(vcpu->kvm))
  4131. memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
  4132. if (vmx->vpid != 0)
  4133. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  4134. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  4135. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  4136. vmx->vcpu.arch.cr0 = cr0;
  4137. vmx_set_cr4(vcpu, 0);
  4138. if (!init_event)
  4139. vmx_set_efer(vcpu, 0);
  4140. vmx_fpu_activate(vcpu);
  4141. update_exception_bitmap(vcpu);
  4142. vpid_sync_context(vmx);
  4143. }
  4144. /*
  4145. * In nested virtualization, check if L1 asked to exit on external interrupts.
  4146. * For most existing hypervisors, this will always return true.
  4147. */
  4148. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  4149. {
  4150. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  4151. PIN_BASED_EXT_INTR_MASK;
  4152. }
  4153. /*
  4154. * In nested virtualization, check if L1 has set
  4155. * VM_EXIT_ACK_INTR_ON_EXIT
  4156. */
  4157. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  4158. {
  4159. return get_vmcs12(vcpu)->vm_exit_controls &
  4160. VM_EXIT_ACK_INTR_ON_EXIT;
  4161. }
  4162. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  4163. {
  4164. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  4165. PIN_BASED_NMI_EXITING;
  4166. }
  4167. static void enable_irq_window(struct kvm_vcpu *vcpu)
  4168. {
  4169. u32 cpu_based_vm_exec_control;
  4170. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4171. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  4172. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4173. }
  4174. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  4175. {
  4176. u32 cpu_based_vm_exec_control;
  4177. if (!cpu_has_virtual_nmis() ||
  4178. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  4179. enable_irq_window(vcpu);
  4180. return;
  4181. }
  4182. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4183. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  4184. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4185. }
  4186. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  4187. {
  4188. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4189. uint32_t intr;
  4190. int irq = vcpu->arch.interrupt.nr;
  4191. trace_kvm_inj_virq(irq);
  4192. ++vcpu->stat.irq_injections;
  4193. if (vmx->rmode.vm86_active) {
  4194. int inc_eip = 0;
  4195. if (vcpu->arch.interrupt.soft)
  4196. inc_eip = vcpu->arch.event_exit_inst_len;
  4197. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  4198. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  4199. return;
  4200. }
  4201. intr = irq | INTR_INFO_VALID_MASK;
  4202. if (vcpu->arch.interrupt.soft) {
  4203. intr |= INTR_TYPE_SOFT_INTR;
  4204. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  4205. vmx->vcpu.arch.event_exit_inst_len);
  4206. } else
  4207. intr |= INTR_TYPE_EXT_INTR;
  4208. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  4209. }
  4210. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  4211. {
  4212. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4213. if (is_guest_mode(vcpu))
  4214. return;
  4215. if (!cpu_has_virtual_nmis()) {
  4216. /*
  4217. * Tracking the NMI-blocked state in software is built upon
  4218. * finding the next open IRQ window. This, in turn, depends on
  4219. * well-behaving guests: They have to keep IRQs disabled at
  4220. * least as long as the NMI handler runs. Otherwise we may
  4221. * cause NMI nesting, maybe breaking the guest. But as this is
  4222. * highly unlikely, we can live with the residual risk.
  4223. */
  4224. vmx->soft_vnmi_blocked = 1;
  4225. vmx->vnmi_blocked_time = 0;
  4226. }
  4227. ++vcpu->stat.nmi_injections;
  4228. vmx->nmi_known_unmasked = false;
  4229. if (vmx->rmode.vm86_active) {
  4230. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  4231. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  4232. return;
  4233. }
  4234. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  4235. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  4236. }
  4237. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  4238. {
  4239. if (!cpu_has_virtual_nmis())
  4240. return to_vmx(vcpu)->soft_vnmi_blocked;
  4241. if (to_vmx(vcpu)->nmi_known_unmasked)
  4242. return false;
  4243. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  4244. }
  4245. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  4246. {
  4247. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4248. if (!cpu_has_virtual_nmis()) {
  4249. if (vmx->soft_vnmi_blocked != masked) {
  4250. vmx->soft_vnmi_blocked = masked;
  4251. vmx->vnmi_blocked_time = 0;
  4252. }
  4253. } else {
  4254. vmx->nmi_known_unmasked = !masked;
  4255. if (masked)
  4256. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  4257. GUEST_INTR_STATE_NMI);
  4258. else
  4259. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  4260. GUEST_INTR_STATE_NMI);
  4261. }
  4262. }
  4263. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  4264. {
  4265. if (to_vmx(vcpu)->nested.nested_run_pending)
  4266. return 0;
  4267. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  4268. return 0;
  4269. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  4270. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  4271. | GUEST_INTR_STATE_NMI));
  4272. }
  4273. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  4274. {
  4275. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  4276. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  4277. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  4278. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  4279. }
  4280. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  4281. {
  4282. int ret;
  4283. struct kvm_userspace_memory_region tss_mem = {
  4284. .slot = TSS_PRIVATE_MEMSLOT,
  4285. .guest_phys_addr = addr,
  4286. .memory_size = PAGE_SIZE * 3,
  4287. .flags = 0,
  4288. };
  4289. ret = x86_set_memory_region(kvm, &tss_mem);
  4290. if (ret)
  4291. return ret;
  4292. kvm->arch.tss_addr = addr;
  4293. return init_rmode_tss(kvm);
  4294. }
  4295. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  4296. {
  4297. switch (vec) {
  4298. case BP_VECTOR:
  4299. /*
  4300. * Update instruction length as we may reinject the exception
  4301. * from user space while in guest debugging mode.
  4302. */
  4303. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  4304. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  4305. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  4306. return false;
  4307. /* fall through */
  4308. case DB_VECTOR:
  4309. if (vcpu->guest_debug &
  4310. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  4311. return false;
  4312. /* fall through */
  4313. case DE_VECTOR:
  4314. case OF_VECTOR:
  4315. case BR_VECTOR:
  4316. case UD_VECTOR:
  4317. case DF_VECTOR:
  4318. case SS_VECTOR:
  4319. case GP_VECTOR:
  4320. case MF_VECTOR:
  4321. return true;
  4322. break;
  4323. }
  4324. return false;
  4325. }
  4326. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  4327. int vec, u32 err_code)
  4328. {
  4329. /*
  4330. * Instruction with address size override prefix opcode 0x67
  4331. * Cause the #SS fault with 0 error code in VM86 mode.
  4332. */
  4333. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  4334. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  4335. if (vcpu->arch.halt_request) {
  4336. vcpu->arch.halt_request = 0;
  4337. return kvm_vcpu_halt(vcpu);
  4338. }
  4339. return 1;
  4340. }
  4341. return 0;
  4342. }
  4343. /*
  4344. * Forward all other exceptions that are valid in real mode.
  4345. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  4346. * the required debugging infrastructure rework.
  4347. */
  4348. kvm_queue_exception(vcpu, vec);
  4349. return 1;
  4350. }
  4351. /*
  4352. * Trigger machine check on the host. We assume all the MSRs are already set up
  4353. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  4354. * We pass a fake environment to the machine check handler because we want
  4355. * the guest to be always treated like user space, no matter what context
  4356. * it used internally.
  4357. */
  4358. static void kvm_machine_check(void)
  4359. {
  4360. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  4361. struct pt_regs regs = {
  4362. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  4363. .flags = X86_EFLAGS_IF,
  4364. };
  4365. do_machine_check(&regs, 0);
  4366. #endif
  4367. }
  4368. static int handle_machine_check(struct kvm_vcpu *vcpu)
  4369. {
  4370. /* already handled by vcpu_run */
  4371. return 1;
  4372. }
  4373. static int handle_exception(struct kvm_vcpu *vcpu)
  4374. {
  4375. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4376. struct kvm_run *kvm_run = vcpu->run;
  4377. u32 intr_info, ex_no, error_code;
  4378. unsigned long cr2, rip, dr6;
  4379. u32 vect_info;
  4380. enum emulation_result er;
  4381. vect_info = vmx->idt_vectoring_info;
  4382. intr_info = vmx->exit_intr_info;
  4383. if (is_machine_check(intr_info))
  4384. return handle_machine_check(vcpu);
  4385. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  4386. return 1; /* already handled by vmx_vcpu_run() */
  4387. if (is_no_device(intr_info)) {
  4388. vmx_fpu_activate(vcpu);
  4389. return 1;
  4390. }
  4391. if (is_invalid_opcode(intr_info)) {
  4392. if (is_guest_mode(vcpu)) {
  4393. kvm_queue_exception(vcpu, UD_VECTOR);
  4394. return 1;
  4395. }
  4396. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  4397. if (er != EMULATE_DONE)
  4398. kvm_queue_exception(vcpu, UD_VECTOR);
  4399. return 1;
  4400. }
  4401. error_code = 0;
  4402. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  4403. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  4404. /*
  4405. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  4406. * MMIO, it is better to report an internal error.
  4407. * See the comments in vmx_handle_exit.
  4408. */
  4409. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  4410. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  4411. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4412. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  4413. vcpu->run->internal.ndata = 3;
  4414. vcpu->run->internal.data[0] = vect_info;
  4415. vcpu->run->internal.data[1] = intr_info;
  4416. vcpu->run->internal.data[2] = error_code;
  4417. return 0;
  4418. }
  4419. if (is_page_fault(intr_info)) {
  4420. /* EPT won't cause page fault directly */
  4421. BUG_ON(enable_ept);
  4422. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  4423. trace_kvm_page_fault(cr2, error_code);
  4424. if (kvm_event_needs_reinjection(vcpu))
  4425. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  4426. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  4427. }
  4428. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  4429. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  4430. return handle_rmode_exception(vcpu, ex_no, error_code);
  4431. switch (ex_no) {
  4432. case DB_VECTOR:
  4433. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  4434. if (!(vcpu->guest_debug &
  4435. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  4436. vcpu->arch.dr6 &= ~15;
  4437. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  4438. if (!(dr6 & ~DR6_RESERVED)) /* icebp */
  4439. skip_emulated_instruction(vcpu);
  4440. kvm_queue_exception(vcpu, DB_VECTOR);
  4441. return 1;
  4442. }
  4443. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  4444. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  4445. /* fall through */
  4446. case BP_VECTOR:
  4447. /*
  4448. * Update instruction length as we may reinject #BP from
  4449. * user space while in guest debugging mode. Reading it for
  4450. * #DB as well causes no harm, it is not used in that case.
  4451. */
  4452. vmx->vcpu.arch.event_exit_inst_len =
  4453. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  4454. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  4455. rip = kvm_rip_read(vcpu);
  4456. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  4457. kvm_run->debug.arch.exception = ex_no;
  4458. break;
  4459. default:
  4460. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  4461. kvm_run->ex.exception = ex_no;
  4462. kvm_run->ex.error_code = error_code;
  4463. break;
  4464. }
  4465. return 0;
  4466. }
  4467. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  4468. {
  4469. ++vcpu->stat.irq_exits;
  4470. return 1;
  4471. }
  4472. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  4473. {
  4474. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  4475. return 0;
  4476. }
  4477. static int handle_io(struct kvm_vcpu *vcpu)
  4478. {
  4479. unsigned long exit_qualification;
  4480. int size, in, string;
  4481. unsigned port;
  4482. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4483. string = (exit_qualification & 16) != 0;
  4484. in = (exit_qualification & 8) != 0;
  4485. ++vcpu->stat.io_exits;
  4486. if (string || in)
  4487. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4488. port = exit_qualification >> 16;
  4489. size = (exit_qualification & 7) + 1;
  4490. skip_emulated_instruction(vcpu);
  4491. return kvm_fast_pio_out(vcpu, size, port);
  4492. }
  4493. static void
  4494. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  4495. {
  4496. /*
  4497. * Patch in the VMCALL instruction:
  4498. */
  4499. hypercall[0] = 0x0f;
  4500. hypercall[1] = 0x01;
  4501. hypercall[2] = 0xc1;
  4502. }
  4503. static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4504. {
  4505. unsigned long always_on = VMXON_CR0_ALWAYSON;
  4506. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4507. if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
  4508. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4509. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4510. always_on &= ~(X86_CR0_PE | X86_CR0_PG);
  4511. return (val & always_on) == always_on;
  4512. }
  4513. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  4514. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  4515. {
  4516. if (is_guest_mode(vcpu)) {
  4517. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4518. unsigned long orig_val = val;
  4519. /*
  4520. * We get here when L2 changed cr0 in a way that did not change
  4521. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  4522. * but did change L0 shadowed bits. So we first calculate the
  4523. * effective cr0 value that L1 would like to write into the
  4524. * hardware. It consists of the L2-owned bits from the new
  4525. * value combined with the L1-owned bits from L1's guest_cr0.
  4526. */
  4527. val = (val & ~vmcs12->cr0_guest_host_mask) |
  4528. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  4529. if (!nested_cr0_valid(vcpu, val))
  4530. return 1;
  4531. if (kvm_set_cr0(vcpu, val))
  4532. return 1;
  4533. vmcs_writel(CR0_READ_SHADOW, orig_val);
  4534. return 0;
  4535. } else {
  4536. if (to_vmx(vcpu)->nested.vmxon &&
  4537. ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
  4538. return 1;
  4539. return kvm_set_cr0(vcpu, val);
  4540. }
  4541. }
  4542. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  4543. {
  4544. if (is_guest_mode(vcpu)) {
  4545. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4546. unsigned long orig_val = val;
  4547. /* analogously to handle_set_cr0 */
  4548. val = (val & ~vmcs12->cr4_guest_host_mask) |
  4549. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  4550. if (kvm_set_cr4(vcpu, val))
  4551. return 1;
  4552. vmcs_writel(CR4_READ_SHADOW, orig_val);
  4553. return 0;
  4554. } else
  4555. return kvm_set_cr4(vcpu, val);
  4556. }
  4557. /* called to set cr0 as approriate for clts instruction exit. */
  4558. static void handle_clts(struct kvm_vcpu *vcpu)
  4559. {
  4560. if (is_guest_mode(vcpu)) {
  4561. /*
  4562. * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
  4563. * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
  4564. * just pretend it's off (also in arch.cr0 for fpu_activate).
  4565. */
  4566. vmcs_writel(CR0_READ_SHADOW,
  4567. vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
  4568. vcpu->arch.cr0 &= ~X86_CR0_TS;
  4569. } else
  4570. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  4571. }
  4572. static int handle_cr(struct kvm_vcpu *vcpu)
  4573. {
  4574. unsigned long exit_qualification, val;
  4575. int cr;
  4576. int reg;
  4577. int err;
  4578. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4579. cr = exit_qualification & 15;
  4580. reg = (exit_qualification >> 8) & 15;
  4581. switch ((exit_qualification >> 4) & 3) {
  4582. case 0: /* mov to cr */
  4583. val = kvm_register_readl(vcpu, reg);
  4584. trace_kvm_cr_write(cr, val);
  4585. switch (cr) {
  4586. case 0:
  4587. err = handle_set_cr0(vcpu, val);
  4588. kvm_complete_insn_gp(vcpu, err);
  4589. return 1;
  4590. case 3:
  4591. err = kvm_set_cr3(vcpu, val);
  4592. kvm_complete_insn_gp(vcpu, err);
  4593. return 1;
  4594. case 4:
  4595. err = handle_set_cr4(vcpu, val);
  4596. kvm_complete_insn_gp(vcpu, err);
  4597. return 1;
  4598. case 8: {
  4599. u8 cr8_prev = kvm_get_cr8(vcpu);
  4600. u8 cr8 = (u8)val;
  4601. err = kvm_set_cr8(vcpu, cr8);
  4602. kvm_complete_insn_gp(vcpu, err);
  4603. if (irqchip_in_kernel(vcpu->kvm))
  4604. return 1;
  4605. if (cr8_prev <= cr8)
  4606. return 1;
  4607. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  4608. return 0;
  4609. }
  4610. }
  4611. break;
  4612. case 2: /* clts */
  4613. handle_clts(vcpu);
  4614. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  4615. skip_emulated_instruction(vcpu);
  4616. vmx_fpu_activate(vcpu);
  4617. return 1;
  4618. case 1: /*mov from cr*/
  4619. switch (cr) {
  4620. case 3:
  4621. val = kvm_read_cr3(vcpu);
  4622. kvm_register_write(vcpu, reg, val);
  4623. trace_kvm_cr_read(cr, val);
  4624. skip_emulated_instruction(vcpu);
  4625. return 1;
  4626. case 8:
  4627. val = kvm_get_cr8(vcpu);
  4628. kvm_register_write(vcpu, reg, val);
  4629. trace_kvm_cr_read(cr, val);
  4630. skip_emulated_instruction(vcpu);
  4631. return 1;
  4632. }
  4633. break;
  4634. case 3: /* lmsw */
  4635. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  4636. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  4637. kvm_lmsw(vcpu, val);
  4638. skip_emulated_instruction(vcpu);
  4639. return 1;
  4640. default:
  4641. break;
  4642. }
  4643. vcpu->run->exit_reason = 0;
  4644. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  4645. (int)(exit_qualification >> 4) & 3, cr);
  4646. return 0;
  4647. }
  4648. static int handle_dr(struct kvm_vcpu *vcpu)
  4649. {
  4650. unsigned long exit_qualification;
  4651. int dr, dr7, reg;
  4652. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4653. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  4654. /* First, if DR does not exist, trigger UD */
  4655. if (!kvm_require_dr(vcpu, dr))
  4656. return 1;
  4657. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  4658. if (!kvm_require_cpl(vcpu, 0))
  4659. return 1;
  4660. dr7 = vmcs_readl(GUEST_DR7);
  4661. if (dr7 & DR7_GD) {
  4662. /*
  4663. * As the vm-exit takes precedence over the debug trap, we
  4664. * need to emulate the latter, either for the host or the
  4665. * guest debugging itself.
  4666. */
  4667. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  4668. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  4669. vcpu->run->debug.arch.dr7 = dr7;
  4670. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  4671. vcpu->run->debug.arch.exception = DB_VECTOR;
  4672. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  4673. return 0;
  4674. } else {
  4675. vcpu->arch.dr6 &= ~15;
  4676. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  4677. kvm_queue_exception(vcpu, DB_VECTOR);
  4678. return 1;
  4679. }
  4680. }
  4681. if (vcpu->guest_debug == 0) {
  4682. u32 cpu_based_vm_exec_control;
  4683. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4684. cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  4685. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4686. /*
  4687. * No more DR vmexits; force a reload of the debug registers
  4688. * and reenter on this instruction. The next vmexit will
  4689. * retrieve the full state of the debug registers.
  4690. */
  4691. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  4692. return 1;
  4693. }
  4694. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  4695. if (exit_qualification & TYPE_MOV_FROM_DR) {
  4696. unsigned long val;
  4697. if (kvm_get_dr(vcpu, dr, &val))
  4698. return 1;
  4699. kvm_register_write(vcpu, reg, val);
  4700. } else
  4701. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  4702. return 1;
  4703. skip_emulated_instruction(vcpu);
  4704. return 1;
  4705. }
  4706. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  4707. {
  4708. return vcpu->arch.dr6;
  4709. }
  4710. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  4711. {
  4712. }
  4713. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  4714. {
  4715. u32 cpu_based_vm_exec_control;
  4716. get_debugreg(vcpu->arch.db[0], 0);
  4717. get_debugreg(vcpu->arch.db[1], 1);
  4718. get_debugreg(vcpu->arch.db[2], 2);
  4719. get_debugreg(vcpu->arch.db[3], 3);
  4720. get_debugreg(vcpu->arch.dr6, 6);
  4721. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  4722. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  4723. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4724. cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
  4725. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4726. }
  4727. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  4728. {
  4729. vmcs_writel(GUEST_DR7, val);
  4730. }
  4731. static int handle_cpuid(struct kvm_vcpu *vcpu)
  4732. {
  4733. kvm_emulate_cpuid(vcpu);
  4734. return 1;
  4735. }
  4736. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  4737. {
  4738. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  4739. struct msr_data msr_info;
  4740. msr_info.index = ecx;
  4741. msr_info.host_initiated = false;
  4742. if (vmx_get_msr(vcpu, &msr_info)) {
  4743. trace_kvm_msr_read_ex(ecx);
  4744. kvm_inject_gp(vcpu, 0);
  4745. return 1;
  4746. }
  4747. trace_kvm_msr_read(ecx, msr_info.data);
  4748. /* FIXME: handling of bits 32:63 of rax, rdx */
  4749. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  4750. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  4751. skip_emulated_instruction(vcpu);
  4752. return 1;
  4753. }
  4754. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  4755. {
  4756. struct msr_data msr;
  4757. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  4758. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  4759. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  4760. msr.data = data;
  4761. msr.index = ecx;
  4762. msr.host_initiated = false;
  4763. if (kvm_set_msr(vcpu, &msr) != 0) {
  4764. trace_kvm_msr_write_ex(ecx, data);
  4765. kvm_inject_gp(vcpu, 0);
  4766. return 1;
  4767. }
  4768. trace_kvm_msr_write(ecx, data);
  4769. skip_emulated_instruction(vcpu);
  4770. return 1;
  4771. }
  4772. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  4773. {
  4774. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4775. return 1;
  4776. }
  4777. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  4778. {
  4779. u32 cpu_based_vm_exec_control;
  4780. /* clear pending irq */
  4781. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4782. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  4783. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4784. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4785. ++vcpu->stat.irq_window_exits;
  4786. /*
  4787. * If the user space waits to inject interrupts, exit as soon as
  4788. * possible
  4789. */
  4790. if (!irqchip_in_kernel(vcpu->kvm) &&
  4791. vcpu->run->request_interrupt_window &&
  4792. !kvm_cpu_has_interrupt(vcpu)) {
  4793. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  4794. return 0;
  4795. }
  4796. return 1;
  4797. }
  4798. static int handle_halt(struct kvm_vcpu *vcpu)
  4799. {
  4800. return kvm_emulate_halt(vcpu);
  4801. }
  4802. static int handle_vmcall(struct kvm_vcpu *vcpu)
  4803. {
  4804. kvm_emulate_hypercall(vcpu);
  4805. return 1;
  4806. }
  4807. static int handle_invd(struct kvm_vcpu *vcpu)
  4808. {
  4809. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4810. }
  4811. static int handle_invlpg(struct kvm_vcpu *vcpu)
  4812. {
  4813. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4814. kvm_mmu_invlpg(vcpu, exit_qualification);
  4815. skip_emulated_instruction(vcpu);
  4816. return 1;
  4817. }
  4818. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  4819. {
  4820. int err;
  4821. err = kvm_rdpmc(vcpu);
  4822. kvm_complete_insn_gp(vcpu, err);
  4823. return 1;
  4824. }
  4825. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  4826. {
  4827. kvm_emulate_wbinvd(vcpu);
  4828. return 1;
  4829. }
  4830. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  4831. {
  4832. u64 new_bv = kvm_read_edx_eax(vcpu);
  4833. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4834. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  4835. skip_emulated_instruction(vcpu);
  4836. return 1;
  4837. }
  4838. static int handle_xsaves(struct kvm_vcpu *vcpu)
  4839. {
  4840. skip_emulated_instruction(vcpu);
  4841. WARN(1, "this should never happen\n");
  4842. return 1;
  4843. }
  4844. static int handle_xrstors(struct kvm_vcpu *vcpu)
  4845. {
  4846. skip_emulated_instruction(vcpu);
  4847. WARN(1, "this should never happen\n");
  4848. return 1;
  4849. }
  4850. static int handle_apic_access(struct kvm_vcpu *vcpu)
  4851. {
  4852. if (likely(fasteoi)) {
  4853. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4854. int access_type, offset;
  4855. access_type = exit_qualification & APIC_ACCESS_TYPE;
  4856. offset = exit_qualification & APIC_ACCESS_OFFSET;
  4857. /*
  4858. * Sane guest uses MOV to write EOI, with written value
  4859. * not cared. So make a short-circuit here by avoiding
  4860. * heavy instruction emulation.
  4861. */
  4862. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  4863. (offset == APIC_EOI)) {
  4864. kvm_lapic_set_eoi(vcpu);
  4865. skip_emulated_instruction(vcpu);
  4866. return 1;
  4867. }
  4868. }
  4869. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4870. }
  4871. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  4872. {
  4873. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4874. int vector = exit_qualification & 0xff;
  4875. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  4876. kvm_apic_set_eoi_accelerated(vcpu, vector);
  4877. return 1;
  4878. }
  4879. static int handle_apic_write(struct kvm_vcpu *vcpu)
  4880. {
  4881. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4882. u32 offset = exit_qualification & 0xfff;
  4883. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  4884. kvm_apic_write_nodecode(vcpu, offset);
  4885. return 1;
  4886. }
  4887. static int handle_task_switch(struct kvm_vcpu *vcpu)
  4888. {
  4889. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4890. unsigned long exit_qualification;
  4891. bool has_error_code = false;
  4892. u32 error_code = 0;
  4893. u16 tss_selector;
  4894. int reason, type, idt_v, idt_index;
  4895. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  4896. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  4897. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  4898. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4899. reason = (u32)exit_qualification >> 30;
  4900. if (reason == TASK_SWITCH_GATE && idt_v) {
  4901. switch (type) {
  4902. case INTR_TYPE_NMI_INTR:
  4903. vcpu->arch.nmi_injected = false;
  4904. vmx_set_nmi_mask(vcpu, true);
  4905. break;
  4906. case INTR_TYPE_EXT_INTR:
  4907. case INTR_TYPE_SOFT_INTR:
  4908. kvm_clear_interrupt_queue(vcpu);
  4909. break;
  4910. case INTR_TYPE_HARD_EXCEPTION:
  4911. if (vmx->idt_vectoring_info &
  4912. VECTORING_INFO_DELIVER_CODE_MASK) {
  4913. has_error_code = true;
  4914. error_code =
  4915. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  4916. }
  4917. /* fall through */
  4918. case INTR_TYPE_SOFT_EXCEPTION:
  4919. kvm_clear_exception_queue(vcpu);
  4920. break;
  4921. default:
  4922. break;
  4923. }
  4924. }
  4925. tss_selector = exit_qualification;
  4926. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  4927. type != INTR_TYPE_EXT_INTR &&
  4928. type != INTR_TYPE_NMI_INTR))
  4929. skip_emulated_instruction(vcpu);
  4930. if (kvm_task_switch(vcpu, tss_selector,
  4931. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  4932. has_error_code, error_code) == EMULATE_FAIL) {
  4933. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4934. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4935. vcpu->run->internal.ndata = 0;
  4936. return 0;
  4937. }
  4938. /*
  4939. * TODO: What about debug traps on tss switch?
  4940. * Are we supposed to inject them and update dr6?
  4941. */
  4942. return 1;
  4943. }
  4944. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  4945. {
  4946. unsigned long exit_qualification;
  4947. gpa_t gpa;
  4948. u32 error_code;
  4949. int gla_validity;
  4950. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4951. gla_validity = (exit_qualification >> 7) & 0x3;
  4952. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  4953. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  4954. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  4955. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  4956. vmcs_readl(GUEST_LINEAR_ADDRESS));
  4957. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  4958. (long unsigned int)exit_qualification);
  4959. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  4960. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  4961. return 0;
  4962. }
  4963. /*
  4964. * EPT violation happened while executing iret from NMI,
  4965. * "blocked by NMI" bit has to be set before next VM entry.
  4966. * There are errata that may cause this bit to not be set:
  4967. * AAK134, BY25.
  4968. */
  4969. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  4970. cpu_has_virtual_nmis() &&
  4971. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  4972. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  4973. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4974. trace_kvm_page_fault(gpa, exit_qualification);
  4975. /* It is a write fault? */
  4976. error_code = exit_qualification & PFERR_WRITE_MASK;
  4977. /* It is a fetch fault? */
  4978. error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
  4979. /* ept page table is present? */
  4980. error_code |= (exit_qualification >> 3) & PFERR_PRESENT_MASK;
  4981. vcpu->arch.exit_qualification = exit_qualification;
  4982. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  4983. }
  4984. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  4985. {
  4986. int ret;
  4987. gpa_t gpa;
  4988. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4989. if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  4990. skip_emulated_instruction(vcpu);
  4991. return 1;
  4992. }
  4993. ret = handle_mmio_page_fault_common(vcpu, gpa, true);
  4994. if (likely(ret == RET_MMIO_PF_EMULATE))
  4995. return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
  4996. EMULATE_DONE;
  4997. if (unlikely(ret == RET_MMIO_PF_INVALID))
  4998. return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
  4999. if (unlikely(ret == RET_MMIO_PF_RETRY))
  5000. return 1;
  5001. /* It is the real ept misconfig */
  5002. WARN_ON(1);
  5003. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  5004. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  5005. return 0;
  5006. }
  5007. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  5008. {
  5009. u32 cpu_based_vm_exec_control;
  5010. /* clear pending NMI */
  5011. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  5012. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  5013. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  5014. ++vcpu->stat.nmi_window_exits;
  5015. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5016. return 1;
  5017. }
  5018. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  5019. {
  5020. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5021. enum emulation_result err = EMULATE_DONE;
  5022. int ret = 1;
  5023. u32 cpu_exec_ctrl;
  5024. bool intr_window_requested;
  5025. unsigned count = 130;
  5026. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  5027. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  5028. while (vmx->emulation_required && count-- != 0) {
  5029. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  5030. return handle_interrupt_window(&vmx->vcpu);
  5031. if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
  5032. return 1;
  5033. err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
  5034. if (err == EMULATE_USER_EXIT) {
  5035. ++vcpu->stat.mmio_exits;
  5036. ret = 0;
  5037. goto out;
  5038. }
  5039. if (err != EMULATE_DONE) {
  5040. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5041. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  5042. vcpu->run->internal.ndata = 0;
  5043. return 0;
  5044. }
  5045. if (vcpu->arch.halt_request) {
  5046. vcpu->arch.halt_request = 0;
  5047. ret = kvm_vcpu_halt(vcpu);
  5048. goto out;
  5049. }
  5050. if (signal_pending(current))
  5051. goto out;
  5052. if (need_resched())
  5053. schedule();
  5054. }
  5055. out:
  5056. return ret;
  5057. }
  5058. static int __grow_ple_window(int val)
  5059. {
  5060. if (ple_window_grow < 1)
  5061. return ple_window;
  5062. val = min(val, ple_window_actual_max);
  5063. if (ple_window_grow < ple_window)
  5064. val *= ple_window_grow;
  5065. else
  5066. val += ple_window_grow;
  5067. return val;
  5068. }
  5069. static int __shrink_ple_window(int val, int modifier, int minimum)
  5070. {
  5071. if (modifier < 1)
  5072. return ple_window;
  5073. if (modifier < ple_window)
  5074. val /= modifier;
  5075. else
  5076. val -= modifier;
  5077. return max(val, minimum);
  5078. }
  5079. static void grow_ple_window(struct kvm_vcpu *vcpu)
  5080. {
  5081. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5082. int old = vmx->ple_window;
  5083. vmx->ple_window = __grow_ple_window(old);
  5084. if (vmx->ple_window != old)
  5085. vmx->ple_window_dirty = true;
  5086. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  5087. }
  5088. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  5089. {
  5090. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5091. int old = vmx->ple_window;
  5092. vmx->ple_window = __shrink_ple_window(old,
  5093. ple_window_shrink, ple_window);
  5094. if (vmx->ple_window != old)
  5095. vmx->ple_window_dirty = true;
  5096. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  5097. }
  5098. /*
  5099. * ple_window_actual_max is computed to be one grow_ple_window() below
  5100. * ple_window_max. (See __grow_ple_window for the reason.)
  5101. * This prevents overflows, because ple_window_max is int.
  5102. * ple_window_max effectively rounded down to a multiple of ple_window_grow in
  5103. * this process.
  5104. * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
  5105. */
  5106. static void update_ple_window_actual_max(void)
  5107. {
  5108. ple_window_actual_max =
  5109. __shrink_ple_window(max(ple_window_max, ple_window),
  5110. ple_window_grow, INT_MIN);
  5111. }
  5112. static __init int hardware_setup(void)
  5113. {
  5114. int r = -ENOMEM, i, msr;
  5115. rdmsrl_safe(MSR_EFER, &host_efer);
  5116. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  5117. kvm_define_shared_msr(i, vmx_msr_index[i]);
  5118. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  5119. if (!vmx_io_bitmap_a)
  5120. return r;
  5121. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  5122. if (!vmx_io_bitmap_b)
  5123. goto out;
  5124. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  5125. if (!vmx_msr_bitmap_legacy)
  5126. goto out1;
  5127. vmx_msr_bitmap_legacy_x2apic =
  5128. (unsigned long *)__get_free_page(GFP_KERNEL);
  5129. if (!vmx_msr_bitmap_legacy_x2apic)
  5130. goto out2;
  5131. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  5132. if (!vmx_msr_bitmap_longmode)
  5133. goto out3;
  5134. vmx_msr_bitmap_longmode_x2apic =
  5135. (unsigned long *)__get_free_page(GFP_KERNEL);
  5136. if (!vmx_msr_bitmap_longmode_x2apic)
  5137. goto out4;
  5138. if (nested) {
  5139. vmx_msr_bitmap_nested =
  5140. (unsigned long *)__get_free_page(GFP_KERNEL);
  5141. if (!vmx_msr_bitmap_nested)
  5142. goto out5;
  5143. }
  5144. vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  5145. if (!vmx_vmread_bitmap)
  5146. goto out6;
  5147. vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  5148. if (!vmx_vmwrite_bitmap)
  5149. goto out7;
  5150. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  5151. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  5152. /*
  5153. * Allow direct access to the PC debug port (it is often used for I/O
  5154. * delays, but the vmexits simply slow things down).
  5155. */
  5156. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  5157. clear_bit(0x80, vmx_io_bitmap_a);
  5158. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  5159. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  5160. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  5161. if (nested)
  5162. memset(vmx_msr_bitmap_nested, 0xff, PAGE_SIZE);
  5163. if (setup_vmcs_config(&vmcs_config) < 0) {
  5164. r = -EIO;
  5165. goto out8;
  5166. }
  5167. if (boot_cpu_has(X86_FEATURE_NX))
  5168. kvm_enable_efer_bits(EFER_NX);
  5169. if (!cpu_has_vmx_vpid())
  5170. enable_vpid = 0;
  5171. if (!cpu_has_vmx_shadow_vmcs())
  5172. enable_shadow_vmcs = 0;
  5173. if (enable_shadow_vmcs)
  5174. init_vmcs_shadow_fields();
  5175. if (!cpu_has_vmx_ept() ||
  5176. !cpu_has_vmx_ept_4levels()) {
  5177. enable_ept = 0;
  5178. enable_unrestricted_guest = 0;
  5179. enable_ept_ad_bits = 0;
  5180. }
  5181. if (!cpu_has_vmx_ept_ad_bits())
  5182. enable_ept_ad_bits = 0;
  5183. if (!cpu_has_vmx_unrestricted_guest())
  5184. enable_unrestricted_guest = 0;
  5185. if (!cpu_has_vmx_flexpriority())
  5186. flexpriority_enabled = 0;
  5187. /*
  5188. * set_apic_access_page_addr() is used to reload apic access
  5189. * page upon invalidation. No need to do anything if not
  5190. * using the APIC_ACCESS_ADDR VMCS field.
  5191. */
  5192. if (!flexpriority_enabled)
  5193. kvm_x86_ops->set_apic_access_page_addr = NULL;
  5194. if (!cpu_has_vmx_tpr_shadow())
  5195. kvm_x86_ops->update_cr8_intercept = NULL;
  5196. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  5197. kvm_disable_largepages();
  5198. if (!cpu_has_vmx_ple())
  5199. ple_gap = 0;
  5200. if (!cpu_has_vmx_apicv())
  5201. enable_apicv = 0;
  5202. if (enable_apicv)
  5203. kvm_x86_ops->update_cr8_intercept = NULL;
  5204. else {
  5205. kvm_x86_ops->hwapic_irr_update = NULL;
  5206. kvm_x86_ops->hwapic_isr_update = NULL;
  5207. kvm_x86_ops->deliver_posted_interrupt = NULL;
  5208. kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
  5209. }
  5210. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  5211. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  5212. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  5213. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  5214. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  5215. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  5216. vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
  5217. memcpy(vmx_msr_bitmap_legacy_x2apic,
  5218. vmx_msr_bitmap_legacy, PAGE_SIZE);
  5219. memcpy(vmx_msr_bitmap_longmode_x2apic,
  5220. vmx_msr_bitmap_longmode, PAGE_SIZE);
  5221. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  5222. if (enable_apicv) {
  5223. for (msr = 0x800; msr <= 0x8ff; msr++)
  5224. vmx_disable_intercept_msr_read_x2apic(msr);
  5225. /* According SDM, in x2apic mode, the whole id reg is used.
  5226. * But in KVM, it only use the highest eight bits. Need to
  5227. * intercept it */
  5228. vmx_enable_intercept_msr_read_x2apic(0x802);
  5229. /* TMCCT */
  5230. vmx_enable_intercept_msr_read_x2apic(0x839);
  5231. /* TPR */
  5232. vmx_disable_intercept_msr_write_x2apic(0x808);
  5233. /* EOI */
  5234. vmx_disable_intercept_msr_write_x2apic(0x80b);
  5235. /* SELF-IPI */
  5236. vmx_disable_intercept_msr_write_x2apic(0x83f);
  5237. }
  5238. if (enable_ept) {
  5239. kvm_mmu_set_mask_ptes(0ull,
  5240. (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
  5241. (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
  5242. 0ull, VMX_EPT_EXECUTABLE_MASK);
  5243. ept_set_mmio_spte_mask();
  5244. kvm_enable_tdp();
  5245. } else
  5246. kvm_disable_tdp();
  5247. update_ple_window_actual_max();
  5248. /*
  5249. * Only enable PML when hardware supports PML feature, and both EPT
  5250. * and EPT A/D bit features are enabled -- PML depends on them to work.
  5251. */
  5252. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  5253. enable_pml = 0;
  5254. if (!enable_pml) {
  5255. kvm_x86_ops->slot_enable_log_dirty = NULL;
  5256. kvm_x86_ops->slot_disable_log_dirty = NULL;
  5257. kvm_x86_ops->flush_log_dirty = NULL;
  5258. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  5259. }
  5260. return alloc_kvm_area();
  5261. out8:
  5262. free_page((unsigned long)vmx_vmwrite_bitmap);
  5263. out7:
  5264. free_page((unsigned long)vmx_vmread_bitmap);
  5265. out6:
  5266. if (nested)
  5267. free_page((unsigned long)vmx_msr_bitmap_nested);
  5268. out5:
  5269. free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
  5270. out4:
  5271. free_page((unsigned long)vmx_msr_bitmap_longmode);
  5272. out3:
  5273. free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
  5274. out2:
  5275. free_page((unsigned long)vmx_msr_bitmap_legacy);
  5276. out1:
  5277. free_page((unsigned long)vmx_io_bitmap_b);
  5278. out:
  5279. free_page((unsigned long)vmx_io_bitmap_a);
  5280. return r;
  5281. }
  5282. static __exit void hardware_unsetup(void)
  5283. {
  5284. free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
  5285. free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
  5286. free_page((unsigned long)vmx_msr_bitmap_legacy);
  5287. free_page((unsigned long)vmx_msr_bitmap_longmode);
  5288. free_page((unsigned long)vmx_io_bitmap_b);
  5289. free_page((unsigned long)vmx_io_bitmap_a);
  5290. free_page((unsigned long)vmx_vmwrite_bitmap);
  5291. free_page((unsigned long)vmx_vmread_bitmap);
  5292. if (nested)
  5293. free_page((unsigned long)vmx_msr_bitmap_nested);
  5294. free_kvm_area();
  5295. }
  5296. /*
  5297. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  5298. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  5299. */
  5300. static int handle_pause(struct kvm_vcpu *vcpu)
  5301. {
  5302. if (ple_gap)
  5303. grow_ple_window(vcpu);
  5304. skip_emulated_instruction(vcpu);
  5305. kvm_vcpu_on_spin(vcpu);
  5306. return 1;
  5307. }
  5308. static int handle_nop(struct kvm_vcpu *vcpu)
  5309. {
  5310. skip_emulated_instruction(vcpu);
  5311. return 1;
  5312. }
  5313. static int handle_mwait(struct kvm_vcpu *vcpu)
  5314. {
  5315. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  5316. return handle_nop(vcpu);
  5317. }
  5318. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  5319. {
  5320. return 1;
  5321. }
  5322. static int handle_monitor(struct kvm_vcpu *vcpu)
  5323. {
  5324. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  5325. return handle_nop(vcpu);
  5326. }
  5327. /*
  5328. * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
  5329. * We could reuse a single VMCS for all the L2 guests, but we also want the
  5330. * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
  5331. * allows keeping them loaded on the processor, and in the future will allow
  5332. * optimizations where prepare_vmcs02 doesn't need to set all the fields on
  5333. * every entry if they never change.
  5334. * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
  5335. * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
  5336. *
  5337. * The following functions allocate and free a vmcs02 in this pool.
  5338. */
  5339. /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
  5340. static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
  5341. {
  5342. struct vmcs02_list *item;
  5343. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  5344. if (item->vmptr == vmx->nested.current_vmptr) {
  5345. list_move(&item->list, &vmx->nested.vmcs02_pool);
  5346. return &item->vmcs02;
  5347. }
  5348. if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
  5349. /* Recycle the least recently used VMCS. */
  5350. item = list_entry(vmx->nested.vmcs02_pool.prev,
  5351. struct vmcs02_list, list);
  5352. item->vmptr = vmx->nested.current_vmptr;
  5353. list_move(&item->list, &vmx->nested.vmcs02_pool);
  5354. return &item->vmcs02;
  5355. }
  5356. /* Create a new VMCS */
  5357. item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
  5358. if (!item)
  5359. return NULL;
  5360. item->vmcs02.vmcs = alloc_vmcs();
  5361. if (!item->vmcs02.vmcs) {
  5362. kfree(item);
  5363. return NULL;
  5364. }
  5365. loaded_vmcs_init(&item->vmcs02);
  5366. item->vmptr = vmx->nested.current_vmptr;
  5367. list_add(&(item->list), &(vmx->nested.vmcs02_pool));
  5368. vmx->nested.vmcs02_num++;
  5369. return &item->vmcs02;
  5370. }
  5371. /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
  5372. static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
  5373. {
  5374. struct vmcs02_list *item;
  5375. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  5376. if (item->vmptr == vmptr) {
  5377. free_loaded_vmcs(&item->vmcs02);
  5378. list_del(&item->list);
  5379. kfree(item);
  5380. vmx->nested.vmcs02_num--;
  5381. return;
  5382. }
  5383. }
  5384. /*
  5385. * Free all VMCSs saved for this vcpu, except the one pointed by
  5386. * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
  5387. * must be &vmx->vmcs01.
  5388. */
  5389. static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
  5390. {
  5391. struct vmcs02_list *item, *n;
  5392. WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
  5393. list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
  5394. /*
  5395. * Something will leak if the above WARN triggers. Better than
  5396. * a use-after-free.
  5397. */
  5398. if (vmx->loaded_vmcs == &item->vmcs02)
  5399. continue;
  5400. free_loaded_vmcs(&item->vmcs02);
  5401. list_del(&item->list);
  5402. kfree(item);
  5403. vmx->nested.vmcs02_num--;
  5404. }
  5405. }
  5406. /*
  5407. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  5408. * set the success or error code of an emulated VMX instruction, as specified
  5409. * by Vol 2B, VMX Instruction Reference, "Conventions".
  5410. */
  5411. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  5412. {
  5413. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  5414. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  5415. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  5416. }
  5417. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  5418. {
  5419. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  5420. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  5421. X86_EFLAGS_SF | X86_EFLAGS_OF))
  5422. | X86_EFLAGS_CF);
  5423. }
  5424. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  5425. u32 vm_instruction_error)
  5426. {
  5427. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  5428. /*
  5429. * failValid writes the error number to the current VMCS, which
  5430. * can't be done there isn't a current VMCS.
  5431. */
  5432. nested_vmx_failInvalid(vcpu);
  5433. return;
  5434. }
  5435. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  5436. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  5437. X86_EFLAGS_SF | X86_EFLAGS_OF))
  5438. | X86_EFLAGS_ZF);
  5439. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  5440. /*
  5441. * We don't need to force a shadow sync because
  5442. * VM_INSTRUCTION_ERROR is not shadowed
  5443. */
  5444. }
  5445. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  5446. {
  5447. /* TODO: not to reset guest simply here. */
  5448. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5449. pr_warn("kvm: nested vmx abort, indicator %d\n", indicator);
  5450. }
  5451. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  5452. {
  5453. struct vcpu_vmx *vmx =
  5454. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  5455. vmx->nested.preemption_timer_expired = true;
  5456. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  5457. kvm_vcpu_kick(&vmx->vcpu);
  5458. return HRTIMER_NORESTART;
  5459. }
  5460. /*
  5461. * Decode the memory-address operand of a vmx instruction, as recorded on an
  5462. * exit caused by such an instruction (run by a guest hypervisor).
  5463. * On success, returns 0. When the operand is invalid, returns 1 and throws
  5464. * #UD or #GP.
  5465. */
  5466. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  5467. unsigned long exit_qualification,
  5468. u32 vmx_instruction_info, bool wr, gva_t *ret)
  5469. {
  5470. gva_t off;
  5471. bool exn;
  5472. struct kvm_segment s;
  5473. /*
  5474. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  5475. * Execution", on an exit, vmx_instruction_info holds most of the
  5476. * addressing components of the operand. Only the displacement part
  5477. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  5478. * For how an actual address is calculated from all these components,
  5479. * refer to Vol. 1, "Operand Addressing".
  5480. */
  5481. int scaling = vmx_instruction_info & 3;
  5482. int addr_size = (vmx_instruction_info >> 7) & 7;
  5483. bool is_reg = vmx_instruction_info & (1u << 10);
  5484. int seg_reg = (vmx_instruction_info >> 15) & 7;
  5485. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  5486. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  5487. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  5488. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  5489. if (is_reg) {
  5490. kvm_queue_exception(vcpu, UD_VECTOR);
  5491. return 1;
  5492. }
  5493. /* Addr = segment_base + offset */
  5494. /* offset = base + [index * scale] + displacement */
  5495. off = exit_qualification; /* holds the displacement */
  5496. if (base_is_valid)
  5497. off += kvm_register_read(vcpu, base_reg);
  5498. if (index_is_valid)
  5499. off += kvm_register_read(vcpu, index_reg)<<scaling;
  5500. vmx_get_segment(vcpu, &s, seg_reg);
  5501. *ret = s.base + off;
  5502. if (addr_size == 1) /* 32 bit */
  5503. *ret &= 0xffffffff;
  5504. /* Checks for #GP/#SS exceptions. */
  5505. exn = false;
  5506. if (is_protmode(vcpu)) {
  5507. /* Protected mode: apply checks for segment validity in the
  5508. * following order:
  5509. * - segment type check (#GP(0) may be thrown)
  5510. * - usability check (#GP(0)/#SS(0))
  5511. * - limit check (#GP(0)/#SS(0))
  5512. */
  5513. if (wr)
  5514. /* #GP(0) if the destination operand is located in a
  5515. * read-only data segment or any code segment.
  5516. */
  5517. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  5518. else
  5519. /* #GP(0) if the source operand is located in an
  5520. * execute-only code segment
  5521. */
  5522. exn = ((s.type & 0xa) == 8);
  5523. }
  5524. if (exn) {
  5525. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  5526. return 1;
  5527. }
  5528. if (is_long_mode(vcpu)) {
  5529. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  5530. * non-canonical form. This is an only check for long mode.
  5531. */
  5532. exn = is_noncanonical_address(*ret);
  5533. } else if (is_protmode(vcpu)) {
  5534. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  5535. */
  5536. exn = (s.unusable != 0);
  5537. /* Protected mode: #GP(0)/#SS(0) if the memory
  5538. * operand is outside the segment limit.
  5539. */
  5540. exn = exn || (off + sizeof(u64) > s.limit);
  5541. }
  5542. if (exn) {
  5543. kvm_queue_exception_e(vcpu,
  5544. seg_reg == VCPU_SREG_SS ?
  5545. SS_VECTOR : GP_VECTOR,
  5546. 0);
  5547. return 1;
  5548. }
  5549. return 0;
  5550. }
  5551. /*
  5552. * This function performs the various checks including
  5553. * - if it's 4KB aligned
  5554. * - No bits beyond the physical address width are set
  5555. * - Returns 0 on success or else 1
  5556. * (Intel SDM Section 30.3)
  5557. */
  5558. static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
  5559. gpa_t *vmpointer)
  5560. {
  5561. gva_t gva;
  5562. gpa_t vmptr;
  5563. struct x86_exception e;
  5564. struct page *page;
  5565. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5566. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  5567. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  5568. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  5569. return 1;
  5570. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
  5571. sizeof(vmptr), &e)) {
  5572. kvm_inject_page_fault(vcpu, &e);
  5573. return 1;
  5574. }
  5575. switch (exit_reason) {
  5576. case EXIT_REASON_VMON:
  5577. /*
  5578. * SDM 3: 24.11.5
  5579. * The first 4 bytes of VMXON region contain the supported
  5580. * VMCS revision identifier
  5581. *
  5582. * Note - IA32_VMX_BASIC[48] will never be 1
  5583. * for the nested case;
  5584. * which replaces physical address width with 32
  5585. *
  5586. */
  5587. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5588. nested_vmx_failInvalid(vcpu);
  5589. skip_emulated_instruction(vcpu);
  5590. return 1;
  5591. }
  5592. page = nested_get_page(vcpu, vmptr);
  5593. if (page == NULL ||
  5594. *(u32 *)kmap(page) != VMCS12_REVISION) {
  5595. nested_vmx_failInvalid(vcpu);
  5596. kunmap(page);
  5597. skip_emulated_instruction(vcpu);
  5598. return 1;
  5599. }
  5600. kunmap(page);
  5601. vmx->nested.vmxon_ptr = vmptr;
  5602. break;
  5603. case EXIT_REASON_VMCLEAR:
  5604. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5605. nested_vmx_failValid(vcpu,
  5606. VMXERR_VMCLEAR_INVALID_ADDRESS);
  5607. skip_emulated_instruction(vcpu);
  5608. return 1;
  5609. }
  5610. if (vmptr == vmx->nested.vmxon_ptr) {
  5611. nested_vmx_failValid(vcpu,
  5612. VMXERR_VMCLEAR_VMXON_POINTER);
  5613. skip_emulated_instruction(vcpu);
  5614. return 1;
  5615. }
  5616. break;
  5617. case EXIT_REASON_VMPTRLD:
  5618. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5619. nested_vmx_failValid(vcpu,
  5620. VMXERR_VMPTRLD_INVALID_ADDRESS);
  5621. skip_emulated_instruction(vcpu);
  5622. return 1;
  5623. }
  5624. if (vmptr == vmx->nested.vmxon_ptr) {
  5625. nested_vmx_failValid(vcpu,
  5626. VMXERR_VMCLEAR_VMXON_POINTER);
  5627. skip_emulated_instruction(vcpu);
  5628. return 1;
  5629. }
  5630. break;
  5631. default:
  5632. return 1; /* shouldn't happen */
  5633. }
  5634. if (vmpointer)
  5635. *vmpointer = vmptr;
  5636. return 0;
  5637. }
  5638. /*
  5639. * Emulate the VMXON instruction.
  5640. * Currently, we just remember that VMX is active, and do not save or even
  5641. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  5642. * do not currently need to store anything in that guest-allocated memory
  5643. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  5644. * argument is different from the VMXON pointer (which the spec says they do).
  5645. */
  5646. static int handle_vmon(struct kvm_vcpu *vcpu)
  5647. {
  5648. struct kvm_segment cs;
  5649. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5650. struct vmcs *shadow_vmcs;
  5651. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  5652. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  5653. /* The Intel VMX Instruction Reference lists a bunch of bits that
  5654. * are prerequisite to running VMXON, most notably cr4.VMXE must be
  5655. * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
  5656. * Otherwise, we should fail with #UD. We test these now:
  5657. */
  5658. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
  5659. !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
  5660. (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  5661. kvm_queue_exception(vcpu, UD_VECTOR);
  5662. return 1;
  5663. }
  5664. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  5665. if (is_long_mode(vcpu) && !cs.l) {
  5666. kvm_queue_exception(vcpu, UD_VECTOR);
  5667. return 1;
  5668. }
  5669. if (vmx_get_cpl(vcpu)) {
  5670. kvm_inject_gp(vcpu, 0);
  5671. return 1;
  5672. }
  5673. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
  5674. return 1;
  5675. if (vmx->nested.vmxon) {
  5676. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  5677. skip_emulated_instruction(vcpu);
  5678. return 1;
  5679. }
  5680. if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  5681. != VMXON_NEEDED_FEATURES) {
  5682. kvm_inject_gp(vcpu, 0);
  5683. return 1;
  5684. }
  5685. if (enable_shadow_vmcs) {
  5686. shadow_vmcs = alloc_vmcs();
  5687. if (!shadow_vmcs)
  5688. return -ENOMEM;
  5689. /* mark vmcs as shadow */
  5690. shadow_vmcs->revision_id |= (1u << 31);
  5691. /* init shadow vmcs */
  5692. vmcs_clear(shadow_vmcs);
  5693. vmx->nested.current_shadow_vmcs = shadow_vmcs;
  5694. }
  5695. INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
  5696. vmx->nested.vmcs02_num = 0;
  5697. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  5698. HRTIMER_MODE_REL);
  5699. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  5700. vmx->nested.vmxon = true;
  5701. skip_emulated_instruction(vcpu);
  5702. nested_vmx_succeed(vcpu);
  5703. return 1;
  5704. }
  5705. /*
  5706. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  5707. * for running VMX instructions (except VMXON, whose prerequisites are
  5708. * slightly different). It also specifies what exception to inject otherwise.
  5709. */
  5710. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  5711. {
  5712. struct kvm_segment cs;
  5713. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5714. if (!vmx->nested.vmxon) {
  5715. kvm_queue_exception(vcpu, UD_VECTOR);
  5716. return 0;
  5717. }
  5718. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  5719. if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
  5720. (is_long_mode(vcpu) && !cs.l)) {
  5721. kvm_queue_exception(vcpu, UD_VECTOR);
  5722. return 0;
  5723. }
  5724. if (vmx_get_cpl(vcpu)) {
  5725. kvm_inject_gp(vcpu, 0);
  5726. return 0;
  5727. }
  5728. return 1;
  5729. }
  5730. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  5731. {
  5732. u32 exec_control;
  5733. if (vmx->nested.current_vmptr == -1ull)
  5734. return;
  5735. /* current_vmptr and current_vmcs12 are always set/reset together */
  5736. if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
  5737. return;
  5738. if (enable_shadow_vmcs) {
  5739. /* copy to memory all shadowed fields in case
  5740. they were modified */
  5741. copy_shadow_to_vmcs12(vmx);
  5742. vmx->nested.sync_shadow_vmcs = false;
  5743. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  5744. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5745. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  5746. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  5747. }
  5748. vmx->nested.posted_intr_nv = -1;
  5749. kunmap(vmx->nested.current_vmcs12_page);
  5750. nested_release_page(vmx->nested.current_vmcs12_page);
  5751. vmx->nested.current_vmptr = -1ull;
  5752. vmx->nested.current_vmcs12 = NULL;
  5753. }
  5754. /*
  5755. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  5756. * just stops using VMX.
  5757. */
  5758. static void free_nested(struct vcpu_vmx *vmx)
  5759. {
  5760. if (!vmx->nested.vmxon)
  5761. return;
  5762. vmx->nested.vmxon = false;
  5763. nested_release_vmcs12(vmx);
  5764. if (enable_shadow_vmcs)
  5765. free_vmcs(vmx->nested.current_shadow_vmcs);
  5766. /* Unpin physical memory we referred to in current vmcs02 */
  5767. if (vmx->nested.apic_access_page) {
  5768. nested_release_page(vmx->nested.apic_access_page);
  5769. vmx->nested.apic_access_page = NULL;
  5770. }
  5771. if (vmx->nested.virtual_apic_page) {
  5772. nested_release_page(vmx->nested.virtual_apic_page);
  5773. vmx->nested.virtual_apic_page = NULL;
  5774. }
  5775. if (vmx->nested.pi_desc_page) {
  5776. kunmap(vmx->nested.pi_desc_page);
  5777. nested_release_page(vmx->nested.pi_desc_page);
  5778. vmx->nested.pi_desc_page = NULL;
  5779. vmx->nested.pi_desc = NULL;
  5780. }
  5781. nested_free_all_saved_vmcss(vmx);
  5782. }
  5783. /* Emulate the VMXOFF instruction */
  5784. static int handle_vmoff(struct kvm_vcpu *vcpu)
  5785. {
  5786. if (!nested_vmx_check_permission(vcpu))
  5787. return 1;
  5788. free_nested(to_vmx(vcpu));
  5789. skip_emulated_instruction(vcpu);
  5790. nested_vmx_succeed(vcpu);
  5791. return 1;
  5792. }
  5793. /* Emulate the VMCLEAR instruction */
  5794. static int handle_vmclear(struct kvm_vcpu *vcpu)
  5795. {
  5796. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5797. gpa_t vmptr;
  5798. struct vmcs12 *vmcs12;
  5799. struct page *page;
  5800. if (!nested_vmx_check_permission(vcpu))
  5801. return 1;
  5802. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
  5803. return 1;
  5804. if (vmptr == vmx->nested.current_vmptr)
  5805. nested_release_vmcs12(vmx);
  5806. page = nested_get_page(vcpu, vmptr);
  5807. if (page == NULL) {
  5808. /*
  5809. * For accurate processor emulation, VMCLEAR beyond available
  5810. * physical memory should do nothing at all. However, it is
  5811. * possible that a nested vmx bug, not a guest hypervisor bug,
  5812. * resulted in this case, so let's shut down before doing any
  5813. * more damage:
  5814. */
  5815. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5816. return 1;
  5817. }
  5818. vmcs12 = kmap(page);
  5819. vmcs12->launch_state = 0;
  5820. kunmap(page);
  5821. nested_release_page(page);
  5822. nested_free_vmcs02(vmx, vmptr);
  5823. skip_emulated_instruction(vcpu);
  5824. nested_vmx_succeed(vcpu);
  5825. return 1;
  5826. }
  5827. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  5828. /* Emulate the VMLAUNCH instruction */
  5829. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  5830. {
  5831. return nested_vmx_run(vcpu, true);
  5832. }
  5833. /* Emulate the VMRESUME instruction */
  5834. static int handle_vmresume(struct kvm_vcpu *vcpu)
  5835. {
  5836. return nested_vmx_run(vcpu, false);
  5837. }
  5838. enum vmcs_field_type {
  5839. VMCS_FIELD_TYPE_U16 = 0,
  5840. VMCS_FIELD_TYPE_U64 = 1,
  5841. VMCS_FIELD_TYPE_U32 = 2,
  5842. VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
  5843. };
  5844. static inline int vmcs_field_type(unsigned long field)
  5845. {
  5846. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  5847. return VMCS_FIELD_TYPE_U32;
  5848. return (field >> 13) & 0x3 ;
  5849. }
  5850. static inline int vmcs_field_readonly(unsigned long field)
  5851. {
  5852. return (((field >> 10) & 0x3) == 1);
  5853. }
  5854. /*
  5855. * Read a vmcs12 field. Since these can have varying lengths and we return
  5856. * one type, we chose the biggest type (u64) and zero-extend the return value
  5857. * to that size. Note that the caller, handle_vmread, might need to use only
  5858. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  5859. * 64-bit fields are to be returned).
  5860. */
  5861. static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
  5862. unsigned long field, u64 *ret)
  5863. {
  5864. short offset = vmcs_field_to_offset(field);
  5865. char *p;
  5866. if (offset < 0)
  5867. return offset;
  5868. p = ((char *)(get_vmcs12(vcpu))) + offset;
  5869. switch (vmcs_field_type(field)) {
  5870. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5871. *ret = *((natural_width *)p);
  5872. return 0;
  5873. case VMCS_FIELD_TYPE_U16:
  5874. *ret = *((u16 *)p);
  5875. return 0;
  5876. case VMCS_FIELD_TYPE_U32:
  5877. *ret = *((u32 *)p);
  5878. return 0;
  5879. case VMCS_FIELD_TYPE_U64:
  5880. *ret = *((u64 *)p);
  5881. return 0;
  5882. default:
  5883. WARN_ON(1);
  5884. return -ENOENT;
  5885. }
  5886. }
  5887. static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
  5888. unsigned long field, u64 field_value){
  5889. short offset = vmcs_field_to_offset(field);
  5890. char *p = ((char *) get_vmcs12(vcpu)) + offset;
  5891. if (offset < 0)
  5892. return offset;
  5893. switch (vmcs_field_type(field)) {
  5894. case VMCS_FIELD_TYPE_U16:
  5895. *(u16 *)p = field_value;
  5896. return 0;
  5897. case VMCS_FIELD_TYPE_U32:
  5898. *(u32 *)p = field_value;
  5899. return 0;
  5900. case VMCS_FIELD_TYPE_U64:
  5901. *(u64 *)p = field_value;
  5902. return 0;
  5903. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5904. *(natural_width *)p = field_value;
  5905. return 0;
  5906. default:
  5907. WARN_ON(1);
  5908. return -ENOENT;
  5909. }
  5910. }
  5911. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  5912. {
  5913. int i;
  5914. unsigned long field;
  5915. u64 field_value;
  5916. struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
  5917. const unsigned long *fields = shadow_read_write_fields;
  5918. const int num_fields = max_shadow_read_write_fields;
  5919. preempt_disable();
  5920. vmcs_load(shadow_vmcs);
  5921. for (i = 0; i < num_fields; i++) {
  5922. field = fields[i];
  5923. switch (vmcs_field_type(field)) {
  5924. case VMCS_FIELD_TYPE_U16:
  5925. field_value = vmcs_read16(field);
  5926. break;
  5927. case VMCS_FIELD_TYPE_U32:
  5928. field_value = vmcs_read32(field);
  5929. break;
  5930. case VMCS_FIELD_TYPE_U64:
  5931. field_value = vmcs_read64(field);
  5932. break;
  5933. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5934. field_value = vmcs_readl(field);
  5935. break;
  5936. default:
  5937. WARN_ON(1);
  5938. continue;
  5939. }
  5940. vmcs12_write_any(&vmx->vcpu, field, field_value);
  5941. }
  5942. vmcs_clear(shadow_vmcs);
  5943. vmcs_load(vmx->loaded_vmcs->vmcs);
  5944. preempt_enable();
  5945. }
  5946. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  5947. {
  5948. const unsigned long *fields[] = {
  5949. shadow_read_write_fields,
  5950. shadow_read_only_fields
  5951. };
  5952. const int max_fields[] = {
  5953. max_shadow_read_write_fields,
  5954. max_shadow_read_only_fields
  5955. };
  5956. int i, q;
  5957. unsigned long field;
  5958. u64 field_value = 0;
  5959. struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
  5960. vmcs_load(shadow_vmcs);
  5961. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  5962. for (i = 0; i < max_fields[q]; i++) {
  5963. field = fields[q][i];
  5964. vmcs12_read_any(&vmx->vcpu, field, &field_value);
  5965. switch (vmcs_field_type(field)) {
  5966. case VMCS_FIELD_TYPE_U16:
  5967. vmcs_write16(field, (u16)field_value);
  5968. break;
  5969. case VMCS_FIELD_TYPE_U32:
  5970. vmcs_write32(field, (u32)field_value);
  5971. break;
  5972. case VMCS_FIELD_TYPE_U64:
  5973. vmcs_write64(field, (u64)field_value);
  5974. break;
  5975. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5976. vmcs_writel(field, (long)field_value);
  5977. break;
  5978. default:
  5979. WARN_ON(1);
  5980. break;
  5981. }
  5982. }
  5983. }
  5984. vmcs_clear(shadow_vmcs);
  5985. vmcs_load(vmx->loaded_vmcs->vmcs);
  5986. }
  5987. /*
  5988. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  5989. * used before) all generate the same failure when it is missing.
  5990. */
  5991. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  5992. {
  5993. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5994. if (vmx->nested.current_vmptr == -1ull) {
  5995. nested_vmx_failInvalid(vcpu);
  5996. skip_emulated_instruction(vcpu);
  5997. return 0;
  5998. }
  5999. return 1;
  6000. }
  6001. static int handle_vmread(struct kvm_vcpu *vcpu)
  6002. {
  6003. unsigned long field;
  6004. u64 field_value;
  6005. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6006. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6007. gva_t gva = 0;
  6008. if (!nested_vmx_check_permission(vcpu) ||
  6009. !nested_vmx_check_vmcs12(vcpu))
  6010. return 1;
  6011. /* Decode instruction info and find the field to read */
  6012. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  6013. /* Read the field, zero-extended to a u64 field_value */
  6014. if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
  6015. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  6016. skip_emulated_instruction(vcpu);
  6017. return 1;
  6018. }
  6019. /*
  6020. * Now copy part of this value to register or memory, as requested.
  6021. * Note that the number of bits actually copied is 32 or 64 depending
  6022. * on the guest's mode (32 or 64 bit), not on the given field's length.
  6023. */
  6024. if (vmx_instruction_info & (1u << 10)) {
  6025. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  6026. field_value);
  6027. } else {
  6028. if (get_vmx_mem_address(vcpu, exit_qualification,
  6029. vmx_instruction_info, true, &gva))
  6030. return 1;
  6031. /* _system ok, as nested_vmx_check_permission verified cpl=0 */
  6032. kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
  6033. &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
  6034. }
  6035. nested_vmx_succeed(vcpu);
  6036. skip_emulated_instruction(vcpu);
  6037. return 1;
  6038. }
  6039. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  6040. {
  6041. unsigned long field;
  6042. gva_t gva;
  6043. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6044. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6045. /* The value to write might be 32 or 64 bits, depending on L1's long
  6046. * mode, and eventually we need to write that into a field of several
  6047. * possible lengths. The code below first zero-extends the value to 64
  6048. * bit (field_value), and then copies only the approriate number of
  6049. * bits into the vmcs12 field.
  6050. */
  6051. u64 field_value = 0;
  6052. struct x86_exception e;
  6053. if (!nested_vmx_check_permission(vcpu) ||
  6054. !nested_vmx_check_vmcs12(vcpu))
  6055. return 1;
  6056. if (vmx_instruction_info & (1u << 10))
  6057. field_value = kvm_register_readl(vcpu,
  6058. (((vmx_instruction_info) >> 3) & 0xf));
  6059. else {
  6060. if (get_vmx_mem_address(vcpu, exit_qualification,
  6061. vmx_instruction_info, false, &gva))
  6062. return 1;
  6063. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
  6064. &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  6065. kvm_inject_page_fault(vcpu, &e);
  6066. return 1;
  6067. }
  6068. }
  6069. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  6070. if (vmcs_field_readonly(field)) {
  6071. nested_vmx_failValid(vcpu,
  6072. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  6073. skip_emulated_instruction(vcpu);
  6074. return 1;
  6075. }
  6076. if (vmcs12_write_any(vcpu, field, field_value) < 0) {
  6077. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  6078. skip_emulated_instruction(vcpu);
  6079. return 1;
  6080. }
  6081. nested_vmx_succeed(vcpu);
  6082. skip_emulated_instruction(vcpu);
  6083. return 1;
  6084. }
  6085. /* Emulate the VMPTRLD instruction */
  6086. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  6087. {
  6088. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6089. gpa_t vmptr;
  6090. u32 exec_control;
  6091. if (!nested_vmx_check_permission(vcpu))
  6092. return 1;
  6093. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
  6094. return 1;
  6095. if (vmx->nested.current_vmptr != vmptr) {
  6096. struct vmcs12 *new_vmcs12;
  6097. struct page *page;
  6098. page = nested_get_page(vcpu, vmptr);
  6099. if (page == NULL) {
  6100. nested_vmx_failInvalid(vcpu);
  6101. skip_emulated_instruction(vcpu);
  6102. return 1;
  6103. }
  6104. new_vmcs12 = kmap(page);
  6105. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  6106. kunmap(page);
  6107. nested_release_page_clean(page);
  6108. nested_vmx_failValid(vcpu,
  6109. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  6110. skip_emulated_instruction(vcpu);
  6111. return 1;
  6112. }
  6113. nested_release_vmcs12(vmx);
  6114. vmx->nested.current_vmptr = vmptr;
  6115. vmx->nested.current_vmcs12 = new_vmcs12;
  6116. vmx->nested.current_vmcs12_page = page;
  6117. if (enable_shadow_vmcs) {
  6118. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6119. exec_control |= SECONDARY_EXEC_SHADOW_VMCS;
  6120. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  6121. vmcs_write64(VMCS_LINK_POINTER,
  6122. __pa(vmx->nested.current_shadow_vmcs));
  6123. vmx->nested.sync_shadow_vmcs = true;
  6124. }
  6125. }
  6126. nested_vmx_succeed(vcpu);
  6127. skip_emulated_instruction(vcpu);
  6128. return 1;
  6129. }
  6130. /* Emulate the VMPTRST instruction */
  6131. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  6132. {
  6133. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6134. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6135. gva_t vmcs_gva;
  6136. struct x86_exception e;
  6137. if (!nested_vmx_check_permission(vcpu))
  6138. return 1;
  6139. if (get_vmx_mem_address(vcpu, exit_qualification,
  6140. vmx_instruction_info, true, &vmcs_gva))
  6141. return 1;
  6142. /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
  6143. if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
  6144. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  6145. sizeof(u64), &e)) {
  6146. kvm_inject_page_fault(vcpu, &e);
  6147. return 1;
  6148. }
  6149. nested_vmx_succeed(vcpu);
  6150. skip_emulated_instruction(vcpu);
  6151. return 1;
  6152. }
  6153. /* Emulate the INVEPT instruction */
  6154. static int handle_invept(struct kvm_vcpu *vcpu)
  6155. {
  6156. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6157. u32 vmx_instruction_info, types;
  6158. unsigned long type;
  6159. gva_t gva;
  6160. struct x86_exception e;
  6161. struct {
  6162. u64 eptp, gpa;
  6163. } operand;
  6164. if (!(vmx->nested.nested_vmx_secondary_ctls_high &
  6165. SECONDARY_EXEC_ENABLE_EPT) ||
  6166. !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
  6167. kvm_queue_exception(vcpu, UD_VECTOR);
  6168. return 1;
  6169. }
  6170. if (!nested_vmx_check_permission(vcpu))
  6171. return 1;
  6172. if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
  6173. kvm_queue_exception(vcpu, UD_VECTOR);
  6174. return 1;
  6175. }
  6176. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6177. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  6178. types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  6179. if (!(types & (1UL << type))) {
  6180. nested_vmx_failValid(vcpu,
  6181. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6182. return 1;
  6183. }
  6184. /* According to the Intel VMX instruction reference, the memory
  6185. * operand is read even if it isn't needed (e.g., for type==global)
  6186. */
  6187. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6188. vmx_instruction_info, false, &gva))
  6189. return 1;
  6190. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
  6191. sizeof(operand), &e)) {
  6192. kvm_inject_page_fault(vcpu, &e);
  6193. return 1;
  6194. }
  6195. switch (type) {
  6196. case VMX_EPT_EXTENT_GLOBAL:
  6197. kvm_mmu_sync_roots(vcpu);
  6198. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  6199. nested_vmx_succeed(vcpu);
  6200. break;
  6201. default:
  6202. /* Trap single context invalidation invept calls */
  6203. BUG_ON(1);
  6204. break;
  6205. }
  6206. skip_emulated_instruction(vcpu);
  6207. return 1;
  6208. }
  6209. static int handle_invvpid(struct kvm_vcpu *vcpu)
  6210. {
  6211. kvm_queue_exception(vcpu, UD_VECTOR);
  6212. return 1;
  6213. }
  6214. static int handle_pml_full(struct kvm_vcpu *vcpu)
  6215. {
  6216. unsigned long exit_qualification;
  6217. trace_kvm_pml_full(vcpu->vcpu_id);
  6218. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6219. /*
  6220. * PML buffer FULL happened while executing iret from NMI,
  6221. * "blocked by NMI" bit has to be set before next VM entry.
  6222. */
  6223. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6224. cpu_has_virtual_nmis() &&
  6225. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6226. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  6227. GUEST_INTR_STATE_NMI);
  6228. /*
  6229. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  6230. * here.., and there's no userspace involvement needed for PML.
  6231. */
  6232. return 1;
  6233. }
  6234. /*
  6235. * The exit handlers return 1 if the exit was handled fully and guest execution
  6236. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  6237. * to be done to userspace and return 0.
  6238. */
  6239. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  6240. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  6241. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  6242. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  6243. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  6244. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  6245. [EXIT_REASON_CR_ACCESS] = handle_cr,
  6246. [EXIT_REASON_DR_ACCESS] = handle_dr,
  6247. [EXIT_REASON_CPUID] = handle_cpuid,
  6248. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  6249. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  6250. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  6251. [EXIT_REASON_HLT] = handle_halt,
  6252. [EXIT_REASON_INVD] = handle_invd,
  6253. [EXIT_REASON_INVLPG] = handle_invlpg,
  6254. [EXIT_REASON_RDPMC] = handle_rdpmc,
  6255. [EXIT_REASON_VMCALL] = handle_vmcall,
  6256. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  6257. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  6258. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  6259. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  6260. [EXIT_REASON_VMREAD] = handle_vmread,
  6261. [EXIT_REASON_VMRESUME] = handle_vmresume,
  6262. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  6263. [EXIT_REASON_VMOFF] = handle_vmoff,
  6264. [EXIT_REASON_VMON] = handle_vmon,
  6265. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  6266. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  6267. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  6268. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  6269. [EXIT_REASON_WBINVD] = handle_wbinvd,
  6270. [EXIT_REASON_XSETBV] = handle_xsetbv,
  6271. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  6272. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  6273. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  6274. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  6275. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  6276. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  6277. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  6278. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  6279. [EXIT_REASON_INVEPT] = handle_invept,
  6280. [EXIT_REASON_INVVPID] = handle_invvpid,
  6281. [EXIT_REASON_XSAVES] = handle_xsaves,
  6282. [EXIT_REASON_XRSTORS] = handle_xrstors,
  6283. [EXIT_REASON_PML_FULL] = handle_pml_full,
  6284. };
  6285. static const int kvm_vmx_max_exit_handlers =
  6286. ARRAY_SIZE(kvm_vmx_exit_handlers);
  6287. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  6288. struct vmcs12 *vmcs12)
  6289. {
  6290. unsigned long exit_qualification;
  6291. gpa_t bitmap, last_bitmap;
  6292. unsigned int port;
  6293. int size;
  6294. u8 b;
  6295. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  6296. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  6297. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6298. port = exit_qualification >> 16;
  6299. size = (exit_qualification & 7) + 1;
  6300. last_bitmap = (gpa_t)-1;
  6301. b = -1;
  6302. while (size > 0) {
  6303. if (port < 0x8000)
  6304. bitmap = vmcs12->io_bitmap_a;
  6305. else if (port < 0x10000)
  6306. bitmap = vmcs12->io_bitmap_b;
  6307. else
  6308. return true;
  6309. bitmap += (port & 0x7fff) / 8;
  6310. if (last_bitmap != bitmap)
  6311. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  6312. return true;
  6313. if (b & (1 << (port & 7)))
  6314. return true;
  6315. port++;
  6316. size--;
  6317. last_bitmap = bitmap;
  6318. }
  6319. return false;
  6320. }
  6321. /*
  6322. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  6323. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  6324. * disinterest in the current event (read or write a specific MSR) by using an
  6325. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  6326. */
  6327. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  6328. struct vmcs12 *vmcs12, u32 exit_reason)
  6329. {
  6330. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  6331. gpa_t bitmap;
  6332. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  6333. return true;
  6334. /*
  6335. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  6336. * for the four combinations of read/write and low/high MSR numbers.
  6337. * First we need to figure out which of the four to use:
  6338. */
  6339. bitmap = vmcs12->msr_bitmap;
  6340. if (exit_reason == EXIT_REASON_MSR_WRITE)
  6341. bitmap += 2048;
  6342. if (msr_index >= 0xc0000000) {
  6343. msr_index -= 0xc0000000;
  6344. bitmap += 1024;
  6345. }
  6346. /* Then read the msr_index'th bit from this bitmap: */
  6347. if (msr_index < 1024*8) {
  6348. unsigned char b;
  6349. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  6350. return true;
  6351. return 1 & (b >> (msr_index & 7));
  6352. } else
  6353. return true; /* let L1 handle the wrong parameter */
  6354. }
  6355. /*
  6356. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  6357. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  6358. * intercept (via guest_host_mask etc.) the current event.
  6359. */
  6360. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  6361. struct vmcs12 *vmcs12)
  6362. {
  6363. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6364. int cr = exit_qualification & 15;
  6365. int reg = (exit_qualification >> 8) & 15;
  6366. unsigned long val = kvm_register_readl(vcpu, reg);
  6367. switch ((exit_qualification >> 4) & 3) {
  6368. case 0: /* mov to cr */
  6369. switch (cr) {
  6370. case 0:
  6371. if (vmcs12->cr0_guest_host_mask &
  6372. (val ^ vmcs12->cr0_read_shadow))
  6373. return true;
  6374. break;
  6375. case 3:
  6376. if ((vmcs12->cr3_target_count >= 1 &&
  6377. vmcs12->cr3_target_value0 == val) ||
  6378. (vmcs12->cr3_target_count >= 2 &&
  6379. vmcs12->cr3_target_value1 == val) ||
  6380. (vmcs12->cr3_target_count >= 3 &&
  6381. vmcs12->cr3_target_value2 == val) ||
  6382. (vmcs12->cr3_target_count >= 4 &&
  6383. vmcs12->cr3_target_value3 == val))
  6384. return false;
  6385. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  6386. return true;
  6387. break;
  6388. case 4:
  6389. if (vmcs12->cr4_guest_host_mask &
  6390. (vmcs12->cr4_read_shadow ^ val))
  6391. return true;
  6392. break;
  6393. case 8:
  6394. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  6395. return true;
  6396. break;
  6397. }
  6398. break;
  6399. case 2: /* clts */
  6400. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  6401. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  6402. return true;
  6403. break;
  6404. case 1: /* mov from cr */
  6405. switch (cr) {
  6406. case 3:
  6407. if (vmcs12->cpu_based_vm_exec_control &
  6408. CPU_BASED_CR3_STORE_EXITING)
  6409. return true;
  6410. break;
  6411. case 8:
  6412. if (vmcs12->cpu_based_vm_exec_control &
  6413. CPU_BASED_CR8_STORE_EXITING)
  6414. return true;
  6415. break;
  6416. }
  6417. break;
  6418. case 3: /* lmsw */
  6419. /*
  6420. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  6421. * cr0. Other attempted changes are ignored, with no exit.
  6422. */
  6423. if (vmcs12->cr0_guest_host_mask & 0xe &
  6424. (val ^ vmcs12->cr0_read_shadow))
  6425. return true;
  6426. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  6427. !(vmcs12->cr0_read_shadow & 0x1) &&
  6428. (val & 0x1))
  6429. return true;
  6430. break;
  6431. }
  6432. return false;
  6433. }
  6434. /*
  6435. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  6436. * should handle it ourselves in L0 (and then continue L2). Only call this
  6437. * when in is_guest_mode (L2).
  6438. */
  6439. static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
  6440. {
  6441. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6442. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6443. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6444. u32 exit_reason = vmx->exit_reason;
  6445. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  6446. vmcs_readl(EXIT_QUALIFICATION),
  6447. vmx->idt_vectoring_info,
  6448. intr_info,
  6449. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  6450. KVM_ISA_VMX);
  6451. if (vmx->nested.nested_run_pending)
  6452. return false;
  6453. if (unlikely(vmx->fail)) {
  6454. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  6455. vmcs_read32(VM_INSTRUCTION_ERROR));
  6456. return true;
  6457. }
  6458. switch (exit_reason) {
  6459. case EXIT_REASON_EXCEPTION_NMI:
  6460. if (!is_exception(intr_info))
  6461. return false;
  6462. else if (is_page_fault(intr_info))
  6463. return enable_ept;
  6464. else if (is_no_device(intr_info) &&
  6465. !(vmcs12->guest_cr0 & X86_CR0_TS))
  6466. return false;
  6467. return vmcs12->exception_bitmap &
  6468. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  6469. case EXIT_REASON_EXTERNAL_INTERRUPT:
  6470. return false;
  6471. case EXIT_REASON_TRIPLE_FAULT:
  6472. return true;
  6473. case EXIT_REASON_PENDING_INTERRUPT:
  6474. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  6475. case EXIT_REASON_NMI_WINDOW:
  6476. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  6477. case EXIT_REASON_TASK_SWITCH:
  6478. return true;
  6479. case EXIT_REASON_CPUID:
  6480. if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
  6481. return false;
  6482. return true;
  6483. case EXIT_REASON_HLT:
  6484. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  6485. case EXIT_REASON_INVD:
  6486. return true;
  6487. case EXIT_REASON_INVLPG:
  6488. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  6489. case EXIT_REASON_RDPMC:
  6490. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  6491. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  6492. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  6493. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  6494. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  6495. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  6496. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  6497. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  6498. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  6499. /*
  6500. * VMX instructions trap unconditionally. This allows L1 to
  6501. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  6502. */
  6503. return true;
  6504. case EXIT_REASON_CR_ACCESS:
  6505. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  6506. case EXIT_REASON_DR_ACCESS:
  6507. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  6508. case EXIT_REASON_IO_INSTRUCTION:
  6509. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  6510. case EXIT_REASON_MSR_READ:
  6511. case EXIT_REASON_MSR_WRITE:
  6512. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  6513. case EXIT_REASON_INVALID_STATE:
  6514. return true;
  6515. case EXIT_REASON_MWAIT_INSTRUCTION:
  6516. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  6517. case EXIT_REASON_MONITOR_TRAP_FLAG:
  6518. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  6519. case EXIT_REASON_MONITOR_INSTRUCTION:
  6520. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  6521. case EXIT_REASON_PAUSE_INSTRUCTION:
  6522. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  6523. nested_cpu_has2(vmcs12,
  6524. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  6525. case EXIT_REASON_MCE_DURING_VMENTRY:
  6526. return false;
  6527. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  6528. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  6529. case EXIT_REASON_APIC_ACCESS:
  6530. return nested_cpu_has2(vmcs12,
  6531. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  6532. case EXIT_REASON_APIC_WRITE:
  6533. case EXIT_REASON_EOI_INDUCED:
  6534. /* apic_write and eoi_induced should exit unconditionally. */
  6535. return true;
  6536. case EXIT_REASON_EPT_VIOLATION:
  6537. /*
  6538. * L0 always deals with the EPT violation. If nested EPT is
  6539. * used, and the nested mmu code discovers that the address is
  6540. * missing in the guest EPT table (EPT12), the EPT violation
  6541. * will be injected with nested_ept_inject_page_fault()
  6542. */
  6543. return false;
  6544. case EXIT_REASON_EPT_MISCONFIG:
  6545. /*
  6546. * L2 never uses directly L1's EPT, but rather L0's own EPT
  6547. * table (shadow on EPT) or a merged EPT table that L0 built
  6548. * (EPT on EPT). So any problems with the structure of the
  6549. * table is L0's fault.
  6550. */
  6551. return false;
  6552. case EXIT_REASON_WBINVD:
  6553. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  6554. case EXIT_REASON_XSETBV:
  6555. return true;
  6556. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  6557. /*
  6558. * This should never happen, since it is not possible to
  6559. * set XSS to a non-zero value---neither in L1 nor in L2.
  6560. * If if it were, XSS would have to be checked against
  6561. * the XSS exit bitmap in vmcs12.
  6562. */
  6563. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  6564. default:
  6565. return true;
  6566. }
  6567. }
  6568. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  6569. {
  6570. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  6571. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  6572. }
  6573. static int vmx_enable_pml(struct vcpu_vmx *vmx)
  6574. {
  6575. struct page *pml_pg;
  6576. u32 exec_control;
  6577. pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  6578. if (!pml_pg)
  6579. return -ENOMEM;
  6580. vmx->pml_pg = pml_pg;
  6581. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  6582. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  6583. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6584. exec_control |= SECONDARY_EXEC_ENABLE_PML;
  6585. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  6586. return 0;
  6587. }
  6588. static void vmx_disable_pml(struct vcpu_vmx *vmx)
  6589. {
  6590. u32 exec_control;
  6591. ASSERT(vmx->pml_pg);
  6592. __free_page(vmx->pml_pg);
  6593. vmx->pml_pg = NULL;
  6594. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6595. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  6596. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  6597. }
  6598. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  6599. {
  6600. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6601. u64 *pml_buf;
  6602. u16 pml_idx;
  6603. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  6604. /* Do nothing if PML buffer is empty */
  6605. if (pml_idx == (PML_ENTITY_NUM - 1))
  6606. return;
  6607. /* PML index always points to next available PML buffer entity */
  6608. if (pml_idx >= PML_ENTITY_NUM)
  6609. pml_idx = 0;
  6610. else
  6611. pml_idx++;
  6612. pml_buf = page_address(vmx->pml_pg);
  6613. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  6614. u64 gpa;
  6615. gpa = pml_buf[pml_idx];
  6616. WARN_ON(gpa & (PAGE_SIZE - 1));
  6617. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  6618. }
  6619. /* reset PML index */
  6620. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  6621. }
  6622. /*
  6623. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  6624. * Called before reporting dirty_bitmap to userspace.
  6625. */
  6626. static void kvm_flush_pml_buffers(struct kvm *kvm)
  6627. {
  6628. int i;
  6629. struct kvm_vcpu *vcpu;
  6630. /*
  6631. * We only need to kick vcpu out of guest mode here, as PML buffer
  6632. * is flushed at beginning of all VMEXITs, and it's obvious that only
  6633. * vcpus running in guest are possible to have unflushed GPAs in PML
  6634. * buffer.
  6635. */
  6636. kvm_for_each_vcpu(i, vcpu, kvm)
  6637. kvm_vcpu_kick(vcpu);
  6638. }
  6639. static void vmx_dump_sel(char *name, uint32_t sel)
  6640. {
  6641. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  6642. name, vmcs_read32(sel),
  6643. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  6644. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  6645. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  6646. }
  6647. static void vmx_dump_dtsel(char *name, uint32_t limit)
  6648. {
  6649. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  6650. name, vmcs_read32(limit),
  6651. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  6652. }
  6653. static void dump_vmcs(void)
  6654. {
  6655. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  6656. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  6657. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  6658. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  6659. u32 secondary_exec_control = 0;
  6660. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  6661. u64 efer = vmcs_readl(GUEST_IA32_EFER);
  6662. int i, n;
  6663. if (cpu_has_secondary_exec_ctrls())
  6664. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6665. pr_err("*** Guest State ***\n");
  6666. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  6667. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  6668. vmcs_readl(CR0_GUEST_HOST_MASK));
  6669. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  6670. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  6671. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  6672. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  6673. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  6674. {
  6675. pr_err("PDPTR0 = 0x%016lx PDPTR1 = 0x%016lx\n",
  6676. vmcs_readl(GUEST_PDPTR0), vmcs_readl(GUEST_PDPTR1));
  6677. pr_err("PDPTR2 = 0x%016lx PDPTR3 = 0x%016lx\n",
  6678. vmcs_readl(GUEST_PDPTR2), vmcs_readl(GUEST_PDPTR3));
  6679. }
  6680. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  6681. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  6682. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  6683. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  6684. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  6685. vmcs_readl(GUEST_SYSENTER_ESP),
  6686. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  6687. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  6688. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  6689. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  6690. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  6691. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  6692. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  6693. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  6694. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  6695. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  6696. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  6697. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  6698. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  6699. pr_err("EFER = 0x%016llx PAT = 0x%016lx\n",
  6700. efer, vmcs_readl(GUEST_IA32_PAT));
  6701. pr_err("DebugCtl = 0x%016lx DebugExceptions = 0x%016lx\n",
  6702. vmcs_readl(GUEST_IA32_DEBUGCTL),
  6703. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  6704. if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  6705. pr_err("PerfGlobCtl = 0x%016lx\n",
  6706. vmcs_readl(GUEST_IA32_PERF_GLOBAL_CTRL));
  6707. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  6708. pr_err("BndCfgS = 0x%016lx\n", vmcs_readl(GUEST_BNDCFGS));
  6709. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  6710. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  6711. vmcs_read32(GUEST_ACTIVITY_STATE));
  6712. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  6713. pr_err("InterruptStatus = %04x\n",
  6714. vmcs_read16(GUEST_INTR_STATUS));
  6715. pr_err("*** Host State ***\n");
  6716. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  6717. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  6718. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  6719. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  6720. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  6721. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  6722. vmcs_read16(HOST_TR_SELECTOR));
  6723. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  6724. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  6725. vmcs_readl(HOST_TR_BASE));
  6726. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  6727. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  6728. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  6729. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  6730. vmcs_readl(HOST_CR4));
  6731. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  6732. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  6733. vmcs_read32(HOST_IA32_SYSENTER_CS),
  6734. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  6735. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  6736. pr_err("EFER = 0x%016lx PAT = 0x%016lx\n",
  6737. vmcs_readl(HOST_IA32_EFER), vmcs_readl(HOST_IA32_PAT));
  6738. if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  6739. pr_err("PerfGlobCtl = 0x%016lx\n",
  6740. vmcs_readl(HOST_IA32_PERF_GLOBAL_CTRL));
  6741. pr_err("*** Control State ***\n");
  6742. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  6743. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  6744. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  6745. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  6746. vmcs_read32(EXCEPTION_BITMAP),
  6747. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  6748. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  6749. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  6750. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  6751. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  6752. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  6753. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  6754. vmcs_read32(VM_EXIT_INTR_INFO),
  6755. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  6756. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  6757. pr_err(" reason=%08x qualification=%016lx\n",
  6758. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  6759. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  6760. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  6761. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  6762. pr_err("TSC Offset = 0x%016lx\n", vmcs_readl(TSC_OFFSET));
  6763. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  6764. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  6765. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  6766. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  6767. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  6768. pr_err("EPT pointer = 0x%016lx\n", vmcs_readl(EPT_POINTER));
  6769. n = vmcs_read32(CR3_TARGET_COUNT);
  6770. for (i = 0; i + 1 < n; i += 4)
  6771. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  6772. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  6773. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  6774. if (i < n)
  6775. pr_err("CR3 target%u=%016lx\n",
  6776. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  6777. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  6778. pr_err("PLE Gap=%08x Window=%08x\n",
  6779. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  6780. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  6781. pr_err("Virtual processor ID = 0x%04x\n",
  6782. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  6783. }
  6784. /*
  6785. * The guest has exited. See if we can fix it or if we need userspace
  6786. * assistance.
  6787. */
  6788. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  6789. {
  6790. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6791. u32 exit_reason = vmx->exit_reason;
  6792. u32 vectoring_info = vmx->idt_vectoring_info;
  6793. /*
  6794. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  6795. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  6796. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  6797. * mode as if vcpus is in root mode, the PML buffer must has been
  6798. * flushed already.
  6799. */
  6800. if (enable_pml)
  6801. vmx_flush_pml_buffer(vcpu);
  6802. /* If guest state is invalid, start emulating */
  6803. if (vmx->emulation_required)
  6804. return handle_invalid_guest_state(vcpu);
  6805. if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
  6806. nested_vmx_vmexit(vcpu, exit_reason,
  6807. vmcs_read32(VM_EXIT_INTR_INFO),
  6808. vmcs_readl(EXIT_QUALIFICATION));
  6809. return 1;
  6810. }
  6811. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  6812. dump_vmcs();
  6813. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  6814. vcpu->run->fail_entry.hardware_entry_failure_reason
  6815. = exit_reason;
  6816. return 0;
  6817. }
  6818. if (unlikely(vmx->fail)) {
  6819. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  6820. vcpu->run->fail_entry.hardware_entry_failure_reason
  6821. = vmcs_read32(VM_INSTRUCTION_ERROR);
  6822. return 0;
  6823. }
  6824. /*
  6825. * Note:
  6826. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  6827. * delivery event since it indicates guest is accessing MMIO.
  6828. * The vm-exit can be triggered again after return to guest that
  6829. * will cause infinite loop.
  6830. */
  6831. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6832. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  6833. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  6834. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  6835. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6836. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  6837. vcpu->run->internal.ndata = 2;
  6838. vcpu->run->internal.data[0] = vectoring_info;
  6839. vcpu->run->internal.data[1] = exit_reason;
  6840. return 0;
  6841. }
  6842. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
  6843. !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
  6844. get_vmcs12(vcpu))))) {
  6845. if (vmx_interrupt_allowed(vcpu)) {
  6846. vmx->soft_vnmi_blocked = 0;
  6847. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  6848. vcpu->arch.nmi_pending) {
  6849. /*
  6850. * This CPU don't support us in finding the end of an
  6851. * NMI-blocked window if the guest runs with IRQs
  6852. * disabled. So we pull the trigger after 1 s of
  6853. * futile waiting, but inform the user about this.
  6854. */
  6855. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  6856. "state on VCPU %d after 1 s timeout\n",
  6857. __func__, vcpu->vcpu_id);
  6858. vmx->soft_vnmi_blocked = 0;
  6859. }
  6860. }
  6861. if (exit_reason < kvm_vmx_max_exit_handlers
  6862. && kvm_vmx_exit_handlers[exit_reason])
  6863. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  6864. else {
  6865. WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
  6866. kvm_queue_exception(vcpu, UD_VECTOR);
  6867. return 1;
  6868. }
  6869. }
  6870. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  6871. {
  6872. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6873. if (is_guest_mode(vcpu) &&
  6874. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  6875. return;
  6876. if (irr == -1 || tpr < irr) {
  6877. vmcs_write32(TPR_THRESHOLD, 0);
  6878. return;
  6879. }
  6880. vmcs_write32(TPR_THRESHOLD, irr);
  6881. }
  6882. static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
  6883. {
  6884. u32 sec_exec_control;
  6885. /*
  6886. * There is not point to enable virtualize x2apic without enable
  6887. * apicv
  6888. */
  6889. if (!cpu_has_vmx_virtualize_x2apic_mode() ||
  6890. !vmx_vm_has_apicv(vcpu->kvm))
  6891. return;
  6892. if (!vm_need_tpr_shadow(vcpu->kvm))
  6893. return;
  6894. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6895. if (set) {
  6896. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  6897. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  6898. } else {
  6899. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  6900. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  6901. }
  6902. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  6903. vmx_set_msr_bitmap(vcpu);
  6904. }
  6905. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  6906. {
  6907. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6908. /*
  6909. * Currently we do not handle the nested case where L2 has an
  6910. * APIC access page of its own; that page is still pinned.
  6911. * Hence, we skip the case where the VCPU is in guest mode _and_
  6912. * L1 prepared an APIC access page for L2.
  6913. *
  6914. * For the case where L1 and L2 share the same APIC access page
  6915. * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
  6916. * in the vmcs12), this function will only update either the vmcs01
  6917. * or the vmcs02. If the former, the vmcs02 will be updated by
  6918. * prepare_vmcs02. If the latter, the vmcs01 will be updated in
  6919. * the next L2->L1 exit.
  6920. */
  6921. if (!is_guest_mode(vcpu) ||
  6922. !nested_cpu_has2(vmx->nested.current_vmcs12,
  6923. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  6924. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  6925. }
  6926. static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
  6927. {
  6928. u16 status;
  6929. u8 old;
  6930. if (isr == -1)
  6931. isr = 0;
  6932. status = vmcs_read16(GUEST_INTR_STATUS);
  6933. old = status >> 8;
  6934. if (isr != old) {
  6935. status &= 0xff;
  6936. status |= isr << 8;
  6937. vmcs_write16(GUEST_INTR_STATUS, status);
  6938. }
  6939. }
  6940. static void vmx_set_rvi(int vector)
  6941. {
  6942. u16 status;
  6943. u8 old;
  6944. if (vector == -1)
  6945. vector = 0;
  6946. status = vmcs_read16(GUEST_INTR_STATUS);
  6947. old = (u8)status & 0xff;
  6948. if ((u8)vector != old) {
  6949. status &= ~0xff;
  6950. status |= (u8)vector;
  6951. vmcs_write16(GUEST_INTR_STATUS, status);
  6952. }
  6953. }
  6954. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  6955. {
  6956. if (!is_guest_mode(vcpu)) {
  6957. vmx_set_rvi(max_irr);
  6958. return;
  6959. }
  6960. if (max_irr == -1)
  6961. return;
  6962. /*
  6963. * In guest mode. If a vmexit is needed, vmx_check_nested_events
  6964. * handles it.
  6965. */
  6966. if (nested_exit_on_intr(vcpu))
  6967. return;
  6968. /*
  6969. * Else, fall back to pre-APICv interrupt injection since L2
  6970. * is run without virtual interrupt delivery.
  6971. */
  6972. if (!kvm_event_needs_reinjection(vcpu) &&
  6973. vmx_interrupt_allowed(vcpu)) {
  6974. kvm_queue_interrupt(vcpu, max_irr, false);
  6975. vmx_inject_irq(vcpu);
  6976. }
  6977. }
  6978. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  6979. {
  6980. if (!vmx_vm_has_apicv(vcpu->kvm))
  6981. return;
  6982. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  6983. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  6984. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  6985. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  6986. }
  6987. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  6988. {
  6989. u32 exit_intr_info;
  6990. if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  6991. || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
  6992. return;
  6993. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6994. exit_intr_info = vmx->exit_intr_info;
  6995. /* Handle machine checks before interrupts are enabled */
  6996. if (is_machine_check(exit_intr_info))
  6997. kvm_machine_check();
  6998. /* We need to handle NMIs before interrupts are enabled */
  6999. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  7000. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  7001. kvm_before_handle_nmi(&vmx->vcpu);
  7002. asm("int $2");
  7003. kvm_after_handle_nmi(&vmx->vcpu);
  7004. }
  7005. }
  7006. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  7007. {
  7008. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7009. /*
  7010. * If external interrupt exists, IF bit is set in rflags/eflags on the
  7011. * interrupt stack frame, and interrupt will be enabled on a return
  7012. * from interrupt handler.
  7013. */
  7014. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  7015. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  7016. unsigned int vector;
  7017. unsigned long entry;
  7018. gate_desc *desc;
  7019. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7020. #ifdef CONFIG_X86_64
  7021. unsigned long tmp;
  7022. #endif
  7023. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  7024. desc = (gate_desc *)vmx->host_idt_base + vector;
  7025. entry = gate_offset(*desc);
  7026. asm volatile(
  7027. #ifdef CONFIG_X86_64
  7028. "mov %%" _ASM_SP ", %[sp]\n\t"
  7029. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  7030. "push $%c[ss]\n\t"
  7031. "push %[sp]\n\t"
  7032. #endif
  7033. "pushf\n\t"
  7034. "orl $0x200, (%%" _ASM_SP ")\n\t"
  7035. __ASM_SIZE(push) " $%c[cs]\n\t"
  7036. "call *%[entry]\n\t"
  7037. :
  7038. #ifdef CONFIG_X86_64
  7039. [sp]"=&r"(tmp)
  7040. #endif
  7041. :
  7042. [entry]"r"(entry),
  7043. [ss]"i"(__KERNEL_DS),
  7044. [cs]"i"(__KERNEL_CS)
  7045. );
  7046. } else
  7047. local_irq_enable();
  7048. }
  7049. static bool vmx_has_high_real_mode_segbase(void)
  7050. {
  7051. return enable_unrestricted_guest || emulate_invalid_guest_state;
  7052. }
  7053. static bool vmx_mpx_supported(void)
  7054. {
  7055. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  7056. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  7057. }
  7058. static bool vmx_xsaves_supported(void)
  7059. {
  7060. return vmcs_config.cpu_based_2nd_exec_ctrl &
  7061. SECONDARY_EXEC_XSAVES;
  7062. }
  7063. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  7064. {
  7065. u32 exit_intr_info;
  7066. bool unblock_nmi;
  7067. u8 vector;
  7068. bool idtv_info_valid;
  7069. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  7070. if (cpu_has_virtual_nmis()) {
  7071. if (vmx->nmi_known_unmasked)
  7072. return;
  7073. /*
  7074. * Can't use vmx->exit_intr_info since we're not sure what
  7075. * the exit reason is.
  7076. */
  7077. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7078. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  7079. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  7080. /*
  7081. * SDM 3: 27.7.1.2 (September 2008)
  7082. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  7083. * a guest IRET fault.
  7084. * SDM 3: 23.2.2 (September 2008)
  7085. * Bit 12 is undefined in any of the following cases:
  7086. * If the VM exit sets the valid bit in the IDT-vectoring
  7087. * information field.
  7088. * If the VM exit is due to a double fault.
  7089. */
  7090. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  7091. vector != DF_VECTOR && !idtv_info_valid)
  7092. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  7093. GUEST_INTR_STATE_NMI);
  7094. else
  7095. vmx->nmi_known_unmasked =
  7096. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  7097. & GUEST_INTR_STATE_NMI);
  7098. } else if (unlikely(vmx->soft_vnmi_blocked))
  7099. vmx->vnmi_blocked_time +=
  7100. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  7101. }
  7102. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  7103. u32 idt_vectoring_info,
  7104. int instr_len_field,
  7105. int error_code_field)
  7106. {
  7107. u8 vector;
  7108. int type;
  7109. bool idtv_info_valid;
  7110. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  7111. vcpu->arch.nmi_injected = false;
  7112. kvm_clear_exception_queue(vcpu);
  7113. kvm_clear_interrupt_queue(vcpu);
  7114. if (!idtv_info_valid)
  7115. return;
  7116. kvm_make_request(KVM_REQ_EVENT, vcpu);
  7117. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  7118. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  7119. switch (type) {
  7120. case INTR_TYPE_NMI_INTR:
  7121. vcpu->arch.nmi_injected = true;
  7122. /*
  7123. * SDM 3: 27.7.1.2 (September 2008)
  7124. * Clear bit "block by NMI" before VM entry if a NMI
  7125. * delivery faulted.
  7126. */
  7127. vmx_set_nmi_mask(vcpu, false);
  7128. break;
  7129. case INTR_TYPE_SOFT_EXCEPTION:
  7130. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  7131. /* fall through */
  7132. case INTR_TYPE_HARD_EXCEPTION:
  7133. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  7134. u32 err = vmcs_read32(error_code_field);
  7135. kvm_requeue_exception_e(vcpu, vector, err);
  7136. } else
  7137. kvm_requeue_exception(vcpu, vector);
  7138. break;
  7139. case INTR_TYPE_SOFT_INTR:
  7140. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  7141. /* fall through */
  7142. case INTR_TYPE_EXT_INTR:
  7143. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  7144. break;
  7145. default:
  7146. break;
  7147. }
  7148. }
  7149. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  7150. {
  7151. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  7152. VM_EXIT_INSTRUCTION_LEN,
  7153. IDT_VECTORING_ERROR_CODE);
  7154. }
  7155. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  7156. {
  7157. __vmx_complete_interrupts(vcpu,
  7158. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  7159. VM_ENTRY_INSTRUCTION_LEN,
  7160. VM_ENTRY_EXCEPTION_ERROR_CODE);
  7161. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  7162. }
  7163. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  7164. {
  7165. int i, nr_msrs;
  7166. struct perf_guest_switch_msr *msrs;
  7167. msrs = perf_guest_get_msrs(&nr_msrs);
  7168. if (!msrs)
  7169. return;
  7170. for (i = 0; i < nr_msrs; i++)
  7171. if (msrs[i].host == msrs[i].guest)
  7172. clear_atomic_switch_msr(vmx, msrs[i].msr);
  7173. else
  7174. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  7175. msrs[i].host);
  7176. }
  7177. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  7178. {
  7179. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7180. unsigned long debugctlmsr, cr4;
  7181. /* Record the guest's net vcpu time for enforced NMI injections. */
  7182. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  7183. vmx->entry_time = ktime_get();
  7184. /* Don't enter VMX if guest state is invalid, let the exit handler
  7185. start emulation until we arrive back to a valid state */
  7186. if (vmx->emulation_required)
  7187. return;
  7188. if (vmx->ple_window_dirty) {
  7189. vmx->ple_window_dirty = false;
  7190. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  7191. }
  7192. if (vmx->nested.sync_shadow_vmcs) {
  7193. copy_vmcs12_to_shadow(vmx);
  7194. vmx->nested.sync_shadow_vmcs = false;
  7195. }
  7196. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  7197. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  7198. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  7199. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  7200. cr4 = cr4_read_shadow();
  7201. if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
  7202. vmcs_writel(HOST_CR4, cr4);
  7203. vmx->host_state.vmcs_host_cr4 = cr4;
  7204. }
  7205. /* When single-stepping over STI and MOV SS, we must clear the
  7206. * corresponding interruptibility bits in the guest state. Otherwise
  7207. * vmentry fails as it then expects bit 14 (BS) in pending debug
  7208. * exceptions being set, but that's not correct for the guest debugging
  7209. * case. */
  7210. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  7211. vmx_set_interrupt_shadow(vcpu, 0);
  7212. atomic_switch_perf_msrs(vmx);
  7213. debugctlmsr = get_debugctlmsr();
  7214. vmx->__launched = vmx->loaded_vmcs->launched;
  7215. asm(
  7216. /* Store host registers */
  7217. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  7218. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  7219. "push %%" _ASM_CX " \n\t"
  7220. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  7221. "je 1f \n\t"
  7222. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  7223. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  7224. "1: \n\t"
  7225. /* Reload cr2 if changed */
  7226. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  7227. "mov %%cr2, %%" _ASM_DX " \n\t"
  7228. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  7229. "je 2f \n\t"
  7230. "mov %%" _ASM_AX", %%cr2 \n\t"
  7231. "2: \n\t"
  7232. /* Check if vmlaunch of vmresume is needed */
  7233. "cmpl $0, %c[launched](%0) \n\t"
  7234. /* Load guest registers. Don't clobber flags. */
  7235. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  7236. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  7237. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  7238. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  7239. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  7240. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  7241. #ifdef CONFIG_X86_64
  7242. "mov %c[r8](%0), %%r8 \n\t"
  7243. "mov %c[r9](%0), %%r9 \n\t"
  7244. "mov %c[r10](%0), %%r10 \n\t"
  7245. "mov %c[r11](%0), %%r11 \n\t"
  7246. "mov %c[r12](%0), %%r12 \n\t"
  7247. "mov %c[r13](%0), %%r13 \n\t"
  7248. "mov %c[r14](%0), %%r14 \n\t"
  7249. "mov %c[r15](%0), %%r15 \n\t"
  7250. #endif
  7251. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  7252. /* Enter guest mode */
  7253. "jne 1f \n\t"
  7254. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  7255. "jmp 2f \n\t"
  7256. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  7257. "2: "
  7258. /* Save guest registers, load host registers, keep flags */
  7259. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  7260. "pop %0 \n\t"
  7261. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  7262. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  7263. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  7264. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  7265. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  7266. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  7267. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  7268. #ifdef CONFIG_X86_64
  7269. "mov %%r8, %c[r8](%0) \n\t"
  7270. "mov %%r9, %c[r9](%0) \n\t"
  7271. "mov %%r10, %c[r10](%0) \n\t"
  7272. "mov %%r11, %c[r11](%0) \n\t"
  7273. "mov %%r12, %c[r12](%0) \n\t"
  7274. "mov %%r13, %c[r13](%0) \n\t"
  7275. "mov %%r14, %c[r14](%0) \n\t"
  7276. "mov %%r15, %c[r15](%0) \n\t"
  7277. #endif
  7278. "mov %%cr2, %%" _ASM_AX " \n\t"
  7279. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  7280. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  7281. "setbe %c[fail](%0) \n\t"
  7282. ".pushsection .rodata \n\t"
  7283. ".global vmx_return \n\t"
  7284. "vmx_return: " _ASM_PTR " 2b \n\t"
  7285. ".popsection"
  7286. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  7287. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  7288. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  7289. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  7290. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  7291. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  7292. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  7293. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  7294. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  7295. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  7296. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  7297. #ifdef CONFIG_X86_64
  7298. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  7299. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  7300. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  7301. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  7302. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  7303. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  7304. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  7305. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  7306. #endif
  7307. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  7308. [wordsize]"i"(sizeof(ulong))
  7309. : "cc", "memory"
  7310. #ifdef CONFIG_X86_64
  7311. , "rax", "rbx", "rdi", "rsi"
  7312. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  7313. #else
  7314. , "eax", "ebx", "edi", "esi"
  7315. #endif
  7316. );
  7317. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  7318. if (debugctlmsr)
  7319. update_debugctlmsr(debugctlmsr);
  7320. #ifndef CONFIG_X86_64
  7321. /*
  7322. * The sysexit path does not restore ds/es, so we must set them to
  7323. * a reasonable value ourselves.
  7324. *
  7325. * We can't defer this to vmx_load_host_state() since that function
  7326. * may be executed in interrupt context, which saves and restore segments
  7327. * around it, nullifying its effect.
  7328. */
  7329. loadsegment(ds, __USER_DS);
  7330. loadsegment(es, __USER_DS);
  7331. #endif
  7332. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  7333. | (1 << VCPU_EXREG_RFLAGS)
  7334. | (1 << VCPU_EXREG_PDPTR)
  7335. | (1 << VCPU_EXREG_SEGMENTS)
  7336. | (1 << VCPU_EXREG_CR3));
  7337. vcpu->arch.regs_dirty = 0;
  7338. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  7339. vmx->loaded_vmcs->launched = 1;
  7340. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  7341. trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
  7342. /*
  7343. * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
  7344. * we did not inject a still-pending event to L1 now because of
  7345. * nested_run_pending, we need to re-enable this bit.
  7346. */
  7347. if (vmx->nested.nested_run_pending)
  7348. kvm_make_request(KVM_REQ_EVENT, vcpu);
  7349. vmx->nested.nested_run_pending = 0;
  7350. vmx_complete_atomic_exit(vmx);
  7351. vmx_recover_nmi_blocking(vmx);
  7352. vmx_complete_interrupts(vmx);
  7353. }
  7354. static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
  7355. {
  7356. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7357. int cpu;
  7358. if (vmx->loaded_vmcs == &vmx->vmcs01)
  7359. return;
  7360. cpu = get_cpu();
  7361. vmx->loaded_vmcs = &vmx->vmcs01;
  7362. vmx_vcpu_put(vcpu);
  7363. vmx_vcpu_load(vcpu, cpu);
  7364. vcpu->cpu = cpu;
  7365. put_cpu();
  7366. }
  7367. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  7368. {
  7369. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7370. if (enable_pml)
  7371. vmx_disable_pml(vmx);
  7372. free_vpid(vmx);
  7373. leave_guest_mode(vcpu);
  7374. vmx_load_vmcs01(vcpu);
  7375. free_nested(vmx);
  7376. free_loaded_vmcs(vmx->loaded_vmcs);
  7377. kfree(vmx->guest_msrs);
  7378. kvm_vcpu_uninit(vcpu);
  7379. kmem_cache_free(kvm_vcpu_cache, vmx);
  7380. }
  7381. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  7382. {
  7383. int err;
  7384. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  7385. int cpu;
  7386. if (!vmx)
  7387. return ERR_PTR(-ENOMEM);
  7388. allocate_vpid(vmx);
  7389. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  7390. if (err)
  7391. goto free_vcpu;
  7392. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  7393. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  7394. > PAGE_SIZE);
  7395. err = -ENOMEM;
  7396. if (!vmx->guest_msrs) {
  7397. goto uninit_vcpu;
  7398. }
  7399. vmx->loaded_vmcs = &vmx->vmcs01;
  7400. vmx->loaded_vmcs->vmcs = alloc_vmcs();
  7401. if (!vmx->loaded_vmcs->vmcs)
  7402. goto free_msrs;
  7403. if (!vmm_exclusive)
  7404. kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
  7405. loaded_vmcs_init(vmx->loaded_vmcs);
  7406. if (!vmm_exclusive)
  7407. kvm_cpu_vmxoff();
  7408. cpu = get_cpu();
  7409. vmx_vcpu_load(&vmx->vcpu, cpu);
  7410. vmx->vcpu.cpu = cpu;
  7411. err = vmx_vcpu_setup(vmx);
  7412. vmx_vcpu_put(&vmx->vcpu);
  7413. put_cpu();
  7414. if (err)
  7415. goto free_vmcs;
  7416. if (vm_need_virtualize_apic_accesses(kvm)) {
  7417. err = alloc_apic_access_page(kvm);
  7418. if (err)
  7419. goto free_vmcs;
  7420. }
  7421. if (enable_ept) {
  7422. if (!kvm->arch.ept_identity_map_addr)
  7423. kvm->arch.ept_identity_map_addr =
  7424. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  7425. err = init_rmode_identity_map(kvm);
  7426. if (err)
  7427. goto free_vmcs;
  7428. }
  7429. if (nested)
  7430. nested_vmx_setup_ctls_msrs(vmx);
  7431. vmx->nested.posted_intr_nv = -1;
  7432. vmx->nested.current_vmptr = -1ull;
  7433. vmx->nested.current_vmcs12 = NULL;
  7434. /*
  7435. * If PML is turned on, failure on enabling PML just results in failure
  7436. * of creating the vcpu, therefore we can simplify PML logic (by
  7437. * avoiding dealing with cases, such as enabling PML partially on vcpus
  7438. * for the guest, etc.
  7439. */
  7440. if (enable_pml) {
  7441. err = vmx_enable_pml(vmx);
  7442. if (err)
  7443. goto free_vmcs;
  7444. }
  7445. return &vmx->vcpu;
  7446. free_vmcs:
  7447. free_loaded_vmcs(vmx->loaded_vmcs);
  7448. free_msrs:
  7449. kfree(vmx->guest_msrs);
  7450. uninit_vcpu:
  7451. kvm_vcpu_uninit(&vmx->vcpu);
  7452. free_vcpu:
  7453. free_vpid(vmx);
  7454. kmem_cache_free(kvm_vcpu_cache, vmx);
  7455. return ERR_PTR(err);
  7456. }
  7457. static void __init vmx_check_processor_compat(void *rtn)
  7458. {
  7459. struct vmcs_config vmcs_conf;
  7460. *(int *)rtn = 0;
  7461. if (setup_vmcs_config(&vmcs_conf) < 0)
  7462. *(int *)rtn = -EIO;
  7463. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  7464. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  7465. smp_processor_id());
  7466. *(int *)rtn = -EIO;
  7467. }
  7468. }
  7469. static int get_ept_level(void)
  7470. {
  7471. return VMX_EPT_DEFAULT_GAW + 1;
  7472. }
  7473. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  7474. {
  7475. u8 cache;
  7476. u64 ipat = 0;
  7477. /* For VT-d and EPT combination
  7478. * 1. MMIO: always map as UC
  7479. * 2. EPT with VT-d:
  7480. * a. VT-d without snooping control feature: can't guarantee the
  7481. * result, try to trust guest.
  7482. * b. VT-d with snooping control feature: snooping control feature of
  7483. * VT-d engine can guarantee the cache correctness. Just set it
  7484. * to WB to keep consistent with host. So the same as item 3.
  7485. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  7486. * consistent with host MTRR
  7487. */
  7488. if (is_mmio) {
  7489. cache = MTRR_TYPE_UNCACHABLE;
  7490. goto exit;
  7491. }
  7492. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  7493. ipat = VMX_EPT_IPAT_BIT;
  7494. cache = MTRR_TYPE_WRBACK;
  7495. goto exit;
  7496. }
  7497. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  7498. ipat = VMX_EPT_IPAT_BIT;
  7499. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  7500. cache = MTRR_TYPE_WRBACK;
  7501. else
  7502. cache = MTRR_TYPE_UNCACHABLE;
  7503. goto exit;
  7504. }
  7505. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  7506. exit:
  7507. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  7508. }
  7509. static int vmx_get_lpage_level(void)
  7510. {
  7511. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  7512. return PT_DIRECTORY_LEVEL;
  7513. else
  7514. /* For shadow and EPT supported 1GB page */
  7515. return PT_PDPE_LEVEL;
  7516. }
  7517. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  7518. {
  7519. struct kvm_cpuid_entry2 *best;
  7520. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7521. u32 exec_control;
  7522. vmx->rdtscp_enabled = false;
  7523. if (vmx_rdtscp_supported()) {
  7524. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7525. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  7526. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  7527. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  7528. vmx->rdtscp_enabled = true;
  7529. else {
  7530. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  7531. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  7532. exec_control);
  7533. }
  7534. }
  7535. if (nested && !vmx->rdtscp_enabled)
  7536. vmx->nested.nested_vmx_secondary_ctls_high &=
  7537. ~SECONDARY_EXEC_RDTSCP;
  7538. }
  7539. /* Exposing INVPCID only when PCID is exposed */
  7540. best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  7541. if (vmx_invpcid_supported() &&
  7542. best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
  7543. guest_cpuid_has_pcid(vcpu)) {
  7544. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7545. exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
  7546. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  7547. exec_control);
  7548. } else {
  7549. if (cpu_has_secondary_exec_ctrls()) {
  7550. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7551. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  7552. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  7553. exec_control);
  7554. }
  7555. if (best)
  7556. best->ebx &= ~bit(X86_FEATURE_INVPCID);
  7557. }
  7558. }
  7559. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  7560. {
  7561. if (func == 1 && nested)
  7562. entry->ecx |= bit(X86_FEATURE_VMX);
  7563. }
  7564. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  7565. struct x86_exception *fault)
  7566. {
  7567. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7568. u32 exit_reason;
  7569. if (fault->error_code & PFERR_RSVD_MASK)
  7570. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  7571. else
  7572. exit_reason = EXIT_REASON_EPT_VIOLATION;
  7573. nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
  7574. vmcs12->guest_physical_address = fault->address;
  7575. }
  7576. /* Callbacks for nested_ept_init_mmu_context: */
  7577. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  7578. {
  7579. /* return the page table to be shadowed - in our case, EPT12 */
  7580. return get_vmcs12(vcpu)->ept_pointer;
  7581. }
  7582. static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  7583. {
  7584. WARN_ON(mmu_is_nested(vcpu));
  7585. kvm_init_shadow_ept_mmu(vcpu,
  7586. to_vmx(vcpu)->nested.nested_vmx_ept_caps &
  7587. VMX_EPT_EXECUTE_ONLY_BIT);
  7588. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  7589. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  7590. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  7591. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  7592. }
  7593. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  7594. {
  7595. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  7596. }
  7597. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  7598. u16 error_code)
  7599. {
  7600. bool inequality, bit;
  7601. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  7602. inequality =
  7603. (error_code & vmcs12->page_fault_error_code_mask) !=
  7604. vmcs12->page_fault_error_code_match;
  7605. return inequality ^ bit;
  7606. }
  7607. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  7608. struct x86_exception *fault)
  7609. {
  7610. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7611. WARN_ON(!is_guest_mode(vcpu));
  7612. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
  7613. nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
  7614. vmcs_read32(VM_EXIT_INTR_INFO),
  7615. vmcs_readl(EXIT_QUALIFICATION));
  7616. else
  7617. kvm_inject_page_fault(vcpu, fault);
  7618. }
  7619. static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
  7620. struct vmcs12 *vmcs12)
  7621. {
  7622. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7623. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  7624. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  7625. if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
  7626. vmcs12->apic_access_addr >> maxphyaddr)
  7627. return false;
  7628. /*
  7629. * Translate L1 physical address to host physical
  7630. * address for vmcs02. Keep the page pinned, so this
  7631. * physical address remains valid. We keep a reference
  7632. * to it so we can release it later.
  7633. */
  7634. if (vmx->nested.apic_access_page) /* shouldn't happen */
  7635. nested_release_page(vmx->nested.apic_access_page);
  7636. vmx->nested.apic_access_page =
  7637. nested_get_page(vcpu, vmcs12->apic_access_addr);
  7638. }
  7639. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  7640. if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
  7641. vmcs12->virtual_apic_page_addr >> maxphyaddr)
  7642. return false;
  7643. if (vmx->nested.virtual_apic_page) /* shouldn't happen */
  7644. nested_release_page(vmx->nested.virtual_apic_page);
  7645. vmx->nested.virtual_apic_page =
  7646. nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
  7647. /*
  7648. * Failing the vm entry is _not_ what the processor does
  7649. * but it's basically the only possibility we have.
  7650. * We could still enter the guest if CR8 load exits are
  7651. * enabled, CR8 store exits are enabled, and virtualize APIC
  7652. * access is disabled; in this case the processor would never
  7653. * use the TPR shadow and we could simply clear the bit from
  7654. * the execution control. But such a configuration is useless,
  7655. * so let's keep the code simple.
  7656. */
  7657. if (!vmx->nested.virtual_apic_page)
  7658. return false;
  7659. }
  7660. if (nested_cpu_has_posted_intr(vmcs12)) {
  7661. if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
  7662. vmcs12->posted_intr_desc_addr >> maxphyaddr)
  7663. return false;
  7664. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  7665. kunmap(vmx->nested.pi_desc_page);
  7666. nested_release_page(vmx->nested.pi_desc_page);
  7667. }
  7668. vmx->nested.pi_desc_page =
  7669. nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
  7670. if (!vmx->nested.pi_desc_page)
  7671. return false;
  7672. vmx->nested.pi_desc =
  7673. (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
  7674. if (!vmx->nested.pi_desc) {
  7675. nested_release_page_clean(vmx->nested.pi_desc_page);
  7676. return false;
  7677. }
  7678. vmx->nested.pi_desc =
  7679. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  7680. (unsigned long)(vmcs12->posted_intr_desc_addr &
  7681. (PAGE_SIZE - 1)));
  7682. }
  7683. return true;
  7684. }
  7685. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  7686. {
  7687. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  7688. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7689. if (vcpu->arch.virtual_tsc_khz == 0)
  7690. return;
  7691. /* Make sure short timeouts reliably trigger an immediate vmexit.
  7692. * hrtimer_start does not guarantee this. */
  7693. if (preemption_timeout <= 1) {
  7694. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  7695. return;
  7696. }
  7697. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  7698. preemption_timeout *= 1000000;
  7699. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  7700. hrtimer_start(&vmx->nested.preemption_timer,
  7701. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  7702. }
  7703. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  7704. struct vmcs12 *vmcs12)
  7705. {
  7706. int maxphyaddr;
  7707. u64 addr;
  7708. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  7709. return 0;
  7710. if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
  7711. WARN_ON(1);
  7712. return -EINVAL;
  7713. }
  7714. maxphyaddr = cpuid_maxphyaddr(vcpu);
  7715. if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
  7716. ((addr + PAGE_SIZE) >> maxphyaddr))
  7717. return -EINVAL;
  7718. return 0;
  7719. }
  7720. /*
  7721. * Merge L0's and L1's MSR bitmap, return false to indicate that
  7722. * we do not use the hardware.
  7723. */
  7724. static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
  7725. struct vmcs12 *vmcs12)
  7726. {
  7727. int msr;
  7728. struct page *page;
  7729. unsigned long *msr_bitmap;
  7730. if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
  7731. return false;
  7732. page = nested_get_page(vcpu, vmcs12->msr_bitmap);
  7733. if (!page) {
  7734. WARN_ON(1);
  7735. return false;
  7736. }
  7737. msr_bitmap = (unsigned long *)kmap(page);
  7738. if (!msr_bitmap) {
  7739. nested_release_page_clean(page);
  7740. WARN_ON(1);
  7741. return false;
  7742. }
  7743. if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
  7744. if (nested_cpu_has_apic_reg_virt(vmcs12))
  7745. for (msr = 0x800; msr <= 0x8ff; msr++)
  7746. nested_vmx_disable_intercept_for_msr(
  7747. msr_bitmap,
  7748. vmx_msr_bitmap_nested,
  7749. msr, MSR_TYPE_R);
  7750. /* TPR is allowed */
  7751. nested_vmx_disable_intercept_for_msr(msr_bitmap,
  7752. vmx_msr_bitmap_nested,
  7753. APIC_BASE_MSR + (APIC_TASKPRI >> 4),
  7754. MSR_TYPE_R | MSR_TYPE_W);
  7755. if (nested_cpu_has_vid(vmcs12)) {
  7756. /* EOI and self-IPI are allowed */
  7757. nested_vmx_disable_intercept_for_msr(
  7758. msr_bitmap,
  7759. vmx_msr_bitmap_nested,
  7760. APIC_BASE_MSR + (APIC_EOI >> 4),
  7761. MSR_TYPE_W);
  7762. nested_vmx_disable_intercept_for_msr(
  7763. msr_bitmap,
  7764. vmx_msr_bitmap_nested,
  7765. APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
  7766. MSR_TYPE_W);
  7767. }
  7768. } else {
  7769. /*
  7770. * Enable reading intercept of all the x2apic
  7771. * MSRs. We should not rely on vmcs12 to do any
  7772. * optimizations here, it may have been modified
  7773. * by L1.
  7774. */
  7775. for (msr = 0x800; msr <= 0x8ff; msr++)
  7776. __vmx_enable_intercept_for_msr(
  7777. vmx_msr_bitmap_nested,
  7778. msr,
  7779. MSR_TYPE_R);
  7780. __vmx_enable_intercept_for_msr(
  7781. vmx_msr_bitmap_nested,
  7782. APIC_BASE_MSR + (APIC_TASKPRI >> 4),
  7783. MSR_TYPE_W);
  7784. __vmx_enable_intercept_for_msr(
  7785. vmx_msr_bitmap_nested,
  7786. APIC_BASE_MSR + (APIC_EOI >> 4),
  7787. MSR_TYPE_W);
  7788. __vmx_enable_intercept_for_msr(
  7789. vmx_msr_bitmap_nested,
  7790. APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
  7791. MSR_TYPE_W);
  7792. }
  7793. kunmap(page);
  7794. nested_release_page_clean(page);
  7795. return true;
  7796. }
  7797. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  7798. struct vmcs12 *vmcs12)
  7799. {
  7800. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  7801. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  7802. !nested_cpu_has_vid(vmcs12) &&
  7803. !nested_cpu_has_posted_intr(vmcs12))
  7804. return 0;
  7805. /*
  7806. * If virtualize x2apic mode is enabled,
  7807. * virtualize apic access must be disabled.
  7808. */
  7809. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  7810. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  7811. return -EINVAL;
  7812. /*
  7813. * If virtual interrupt delivery is enabled,
  7814. * we must exit on external interrupts.
  7815. */
  7816. if (nested_cpu_has_vid(vmcs12) &&
  7817. !nested_exit_on_intr(vcpu))
  7818. return -EINVAL;
  7819. /*
  7820. * bits 15:8 should be zero in posted_intr_nv,
  7821. * the descriptor address has been already checked
  7822. * in nested_get_vmcs12_pages.
  7823. */
  7824. if (nested_cpu_has_posted_intr(vmcs12) &&
  7825. (!nested_cpu_has_vid(vmcs12) ||
  7826. !nested_exit_intr_ack_set(vcpu) ||
  7827. vmcs12->posted_intr_nv & 0xff00))
  7828. return -EINVAL;
  7829. /* tpr shadow is needed by all apicv features. */
  7830. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  7831. return -EINVAL;
  7832. return 0;
  7833. }
  7834. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  7835. unsigned long count_field,
  7836. unsigned long addr_field)
  7837. {
  7838. int maxphyaddr;
  7839. u64 count, addr;
  7840. if (vmcs12_read_any(vcpu, count_field, &count) ||
  7841. vmcs12_read_any(vcpu, addr_field, &addr)) {
  7842. WARN_ON(1);
  7843. return -EINVAL;
  7844. }
  7845. if (count == 0)
  7846. return 0;
  7847. maxphyaddr = cpuid_maxphyaddr(vcpu);
  7848. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  7849. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  7850. pr_warn_ratelimited(
  7851. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  7852. addr_field, maxphyaddr, count, addr);
  7853. return -EINVAL;
  7854. }
  7855. return 0;
  7856. }
  7857. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  7858. struct vmcs12 *vmcs12)
  7859. {
  7860. if (vmcs12->vm_exit_msr_load_count == 0 &&
  7861. vmcs12->vm_exit_msr_store_count == 0 &&
  7862. vmcs12->vm_entry_msr_load_count == 0)
  7863. return 0; /* Fast path */
  7864. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  7865. VM_EXIT_MSR_LOAD_ADDR) ||
  7866. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  7867. VM_EXIT_MSR_STORE_ADDR) ||
  7868. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  7869. VM_ENTRY_MSR_LOAD_ADDR))
  7870. return -EINVAL;
  7871. return 0;
  7872. }
  7873. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  7874. struct vmx_msr_entry *e)
  7875. {
  7876. /* x2APIC MSR accesses are not allowed */
  7877. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  7878. return -EINVAL;
  7879. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  7880. e->index == MSR_IA32_UCODE_REV)
  7881. return -EINVAL;
  7882. if (e->reserved != 0)
  7883. return -EINVAL;
  7884. return 0;
  7885. }
  7886. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  7887. struct vmx_msr_entry *e)
  7888. {
  7889. if (e->index == MSR_FS_BASE ||
  7890. e->index == MSR_GS_BASE ||
  7891. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  7892. nested_vmx_msr_check_common(vcpu, e))
  7893. return -EINVAL;
  7894. return 0;
  7895. }
  7896. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  7897. struct vmx_msr_entry *e)
  7898. {
  7899. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  7900. nested_vmx_msr_check_common(vcpu, e))
  7901. return -EINVAL;
  7902. return 0;
  7903. }
  7904. /*
  7905. * Load guest's/host's msr at nested entry/exit.
  7906. * return 0 for success, entry index for failure.
  7907. */
  7908. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  7909. {
  7910. u32 i;
  7911. struct vmx_msr_entry e;
  7912. struct msr_data msr;
  7913. msr.host_initiated = false;
  7914. for (i = 0; i < count; i++) {
  7915. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  7916. &e, sizeof(e))) {
  7917. pr_warn_ratelimited(
  7918. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  7919. __func__, i, gpa + i * sizeof(e));
  7920. goto fail;
  7921. }
  7922. if (nested_vmx_load_msr_check(vcpu, &e)) {
  7923. pr_warn_ratelimited(
  7924. "%s check failed (%u, 0x%x, 0x%x)\n",
  7925. __func__, i, e.index, e.reserved);
  7926. goto fail;
  7927. }
  7928. msr.index = e.index;
  7929. msr.data = e.value;
  7930. if (kvm_set_msr(vcpu, &msr)) {
  7931. pr_warn_ratelimited(
  7932. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  7933. __func__, i, e.index, e.value);
  7934. goto fail;
  7935. }
  7936. }
  7937. return 0;
  7938. fail:
  7939. return i + 1;
  7940. }
  7941. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  7942. {
  7943. u32 i;
  7944. struct vmx_msr_entry e;
  7945. for (i = 0; i < count; i++) {
  7946. struct msr_data msr_info;
  7947. if (kvm_vcpu_read_guest(vcpu,
  7948. gpa + i * sizeof(e),
  7949. &e, 2 * sizeof(u32))) {
  7950. pr_warn_ratelimited(
  7951. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  7952. __func__, i, gpa + i * sizeof(e));
  7953. return -EINVAL;
  7954. }
  7955. if (nested_vmx_store_msr_check(vcpu, &e)) {
  7956. pr_warn_ratelimited(
  7957. "%s check failed (%u, 0x%x, 0x%x)\n",
  7958. __func__, i, e.index, e.reserved);
  7959. return -EINVAL;
  7960. }
  7961. msr_info.host_initiated = false;
  7962. msr_info.index = e.index;
  7963. if (kvm_get_msr(vcpu, &msr_info)) {
  7964. pr_warn_ratelimited(
  7965. "%s cannot read MSR (%u, 0x%x)\n",
  7966. __func__, i, e.index);
  7967. return -EINVAL;
  7968. }
  7969. if (kvm_vcpu_write_guest(vcpu,
  7970. gpa + i * sizeof(e) +
  7971. offsetof(struct vmx_msr_entry, value),
  7972. &msr_info.data, sizeof(msr_info.data))) {
  7973. pr_warn_ratelimited(
  7974. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  7975. __func__, i, e.index, msr_info.data);
  7976. return -EINVAL;
  7977. }
  7978. }
  7979. return 0;
  7980. }
  7981. /*
  7982. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  7983. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  7984. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  7985. * guest in a way that will both be appropriate to L1's requests, and our
  7986. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  7987. * function also has additional necessary side-effects, like setting various
  7988. * vcpu->arch fields.
  7989. */
  7990. static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  7991. {
  7992. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7993. u32 exec_control;
  7994. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  7995. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  7996. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  7997. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  7998. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  7999. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  8000. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  8001. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  8002. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  8003. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  8004. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  8005. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  8006. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  8007. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  8008. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  8009. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  8010. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  8011. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  8012. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  8013. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  8014. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  8015. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  8016. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  8017. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  8018. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  8019. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  8020. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  8021. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  8022. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  8023. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  8024. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  8025. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  8026. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  8027. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  8028. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  8029. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  8030. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
  8031. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  8032. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  8033. } else {
  8034. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  8035. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  8036. }
  8037. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  8038. vmcs12->vm_entry_intr_info_field);
  8039. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  8040. vmcs12->vm_entry_exception_error_code);
  8041. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  8042. vmcs12->vm_entry_instruction_len);
  8043. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  8044. vmcs12->guest_interruptibility_info);
  8045. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  8046. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  8047. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  8048. vmcs12->guest_pending_dbg_exceptions);
  8049. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  8050. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  8051. if (nested_cpu_has_xsaves(vmcs12))
  8052. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  8053. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  8054. exec_control = vmcs12->pin_based_vm_exec_control;
  8055. exec_control |= vmcs_config.pin_based_exec_ctrl;
  8056. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  8057. if (nested_cpu_has_posted_intr(vmcs12)) {
  8058. /*
  8059. * Note that we use L0's vector here and in
  8060. * vmx_deliver_nested_posted_interrupt.
  8061. */
  8062. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  8063. vmx->nested.pi_pending = false;
  8064. vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  8065. vmcs_write64(POSTED_INTR_DESC_ADDR,
  8066. page_to_phys(vmx->nested.pi_desc_page) +
  8067. (unsigned long)(vmcs12->posted_intr_desc_addr &
  8068. (PAGE_SIZE - 1)));
  8069. } else
  8070. exec_control &= ~PIN_BASED_POSTED_INTR;
  8071. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  8072. vmx->nested.preemption_timer_expired = false;
  8073. if (nested_cpu_has_preemption_timer(vmcs12))
  8074. vmx_start_preemption_timer(vcpu);
  8075. /*
  8076. * Whether page-faults are trapped is determined by a combination of
  8077. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  8078. * If enable_ept, L0 doesn't care about page faults and we should
  8079. * set all of these to L1's desires. However, if !enable_ept, L0 does
  8080. * care about (at least some) page faults, and because it is not easy
  8081. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  8082. * to exit on each and every L2 page fault. This is done by setting
  8083. * MASK=MATCH=0 and (see below) EB.PF=1.
  8084. * Note that below we don't need special code to set EB.PF beyond the
  8085. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  8086. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  8087. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  8088. *
  8089. * A problem with this approach (when !enable_ept) is that L1 may be
  8090. * injected with more page faults than it asked for. This could have
  8091. * caused problems, but in practice existing hypervisors don't care.
  8092. * To fix this, we will need to emulate the PFEC checking (on the L1
  8093. * page tables), using walk_addr(), when injecting PFs to L1.
  8094. */
  8095. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  8096. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  8097. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  8098. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  8099. if (cpu_has_secondary_exec_ctrls()) {
  8100. exec_control = vmx_secondary_exec_control(vmx);
  8101. if (!vmx->rdtscp_enabled)
  8102. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  8103. /* Take the following fields only from vmcs12 */
  8104. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8105. SECONDARY_EXEC_RDTSCP |
  8106. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  8107. SECONDARY_EXEC_APIC_REGISTER_VIRT);
  8108. if (nested_cpu_has(vmcs12,
  8109. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
  8110. exec_control |= vmcs12->secondary_vm_exec_control;
  8111. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
  8112. /*
  8113. * If translation failed, no matter: This feature asks
  8114. * to exit when accessing the given address, and if it
  8115. * can never be accessed, this feature won't do
  8116. * anything anyway.
  8117. */
  8118. if (!vmx->nested.apic_access_page)
  8119. exec_control &=
  8120. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  8121. else
  8122. vmcs_write64(APIC_ACCESS_ADDR,
  8123. page_to_phys(vmx->nested.apic_access_page));
  8124. } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
  8125. (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))) {
  8126. exec_control |=
  8127. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  8128. kvm_vcpu_reload_apic_access_page(vcpu);
  8129. }
  8130. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
  8131. vmcs_write64(EOI_EXIT_BITMAP0,
  8132. vmcs12->eoi_exit_bitmap0);
  8133. vmcs_write64(EOI_EXIT_BITMAP1,
  8134. vmcs12->eoi_exit_bitmap1);
  8135. vmcs_write64(EOI_EXIT_BITMAP2,
  8136. vmcs12->eoi_exit_bitmap2);
  8137. vmcs_write64(EOI_EXIT_BITMAP3,
  8138. vmcs12->eoi_exit_bitmap3);
  8139. vmcs_write16(GUEST_INTR_STATUS,
  8140. vmcs12->guest_intr_status);
  8141. }
  8142. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  8143. }
  8144. /*
  8145. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  8146. * Some constant fields are set here by vmx_set_constant_host_state().
  8147. * Other fields are different per CPU, and will be set later when
  8148. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  8149. */
  8150. vmx_set_constant_host_state(vmx);
  8151. /*
  8152. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  8153. * entry, but only if the current (host) sp changed from the value
  8154. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  8155. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  8156. * here we just force the write to happen on entry.
  8157. */
  8158. vmx->host_rsp = 0;
  8159. exec_control = vmx_exec_control(vmx); /* L0's desires */
  8160. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  8161. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  8162. exec_control &= ~CPU_BASED_TPR_SHADOW;
  8163. exec_control |= vmcs12->cpu_based_vm_exec_control;
  8164. if (exec_control & CPU_BASED_TPR_SHADOW) {
  8165. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  8166. page_to_phys(vmx->nested.virtual_apic_page));
  8167. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  8168. }
  8169. if (cpu_has_vmx_msr_bitmap() &&
  8170. exec_control & CPU_BASED_USE_MSR_BITMAPS) {
  8171. nested_vmx_merge_msr_bitmap(vcpu, vmcs12);
  8172. /* MSR_BITMAP will be set by following vmx_set_efer. */
  8173. } else
  8174. exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
  8175. /*
  8176. * Merging of IO bitmap not currently supported.
  8177. * Rather, exit every time.
  8178. */
  8179. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  8180. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  8181. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  8182. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  8183. * bitwise-or of what L1 wants to trap for L2, and what we want to
  8184. * trap. Note that CR0.TS also needs updating - we do this later.
  8185. */
  8186. update_exception_bitmap(vcpu);
  8187. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  8188. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  8189. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  8190. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  8191. * bits are further modified by vmx_set_efer() below.
  8192. */
  8193. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  8194. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  8195. * emulated by vmx_set_efer(), below.
  8196. */
  8197. vm_entry_controls_init(vmx,
  8198. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  8199. ~VM_ENTRY_IA32E_MODE) |
  8200. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  8201. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
  8202. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  8203. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  8204. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  8205. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  8206. set_cr4_guest_host_mask(vmx);
  8207. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
  8208. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  8209. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  8210. vmcs_write64(TSC_OFFSET,
  8211. vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
  8212. else
  8213. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  8214. if (enable_vpid) {
  8215. /*
  8216. * Trivially support vpid by letting L2s share their parent
  8217. * L1's vpid. TODO: move to a more elaborate solution, giving
  8218. * each L2 its own vpid and exposing the vpid feature to L1.
  8219. */
  8220. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  8221. vmx_flush_tlb(vcpu);
  8222. }
  8223. if (nested_cpu_has_ept(vmcs12)) {
  8224. kvm_mmu_unload(vcpu);
  8225. nested_ept_init_mmu_context(vcpu);
  8226. }
  8227. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
  8228. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  8229. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  8230. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  8231. else
  8232. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  8233. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  8234. vmx_set_efer(vcpu, vcpu->arch.efer);
  8235. /*
  8236. * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
  8237. * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
  8238. * The CR0_READ_SHADOW is what L2 should have expected to read given
  8239. * the specifications by L1; It's not enough to take
  8240. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  8241. * have more bits than L1 expected.
  8242. */
  8243. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  8244. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  8245. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  8246. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  8247. /* shadow page tables on either EPT or shadow page tables */
  8248. kvm_set_cr3(vcpu, vmcs12->guest_cr3);
  8249. kvm_mmu_reset_context(vcpu);
  8250. if (!enable_ept)
  8251. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  8252. /*
  8253. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  8254. */
  8255. if (enable_ept) {
  8256. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  8257. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  8258. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  8259. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  8260. }
  8261. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  8262. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  8263. }
  8264. /*
  8265. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  8266. * for running an L2 nested guest.
  8267. */
  8268. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  8269. {
  8270. struct vmcs12 *vmcs12;
  8271. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8272. int cpu;
  8273. struct loaded_vmcs *vmcs02;
  8274. bool ia32e;
  8275. u32 msr_entry_idx;
  8276. if (!nested_vmx_check_permission(vcpu) ||
  8277. !nested_vmx_check_vmcs12(vcpu))
  8278. return 1;
  8279. skip_emulated_instruction(vcpu);
  8280. vmcs12 = get_vmcs12(vcpu);
  8281. if (enable_shadow_vmcs)
  8282. copy_shadow_to_vmcs12(vmx);
  8283. /*
  8284. * The nested entry process starts with enforcing various prerequisites
  8285. * on vmcs12 as required by the Intel SDM, and act appropriately when
  8286. * they fail: As the SDM explains, some conditions should cause the
  8287. * instruction to fail, while others will cause the instruction to seem
  8288. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  8289. * To speed up the normal (success) code path, we should avoid checking
  8290. * for misconfigurations which will anyway be caught by the processor
  8291. * when using the merged vmcs02.
  8292. */
  8293. if (vmcs12->launch_state == launch) {
  8294. nested_vmx_failValid(vcpu,
  8295. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  8296. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  8297. return 1;
  8298. }
  8299. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  8300. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
  8301. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8302. return 1;
  8303. }
  8304. if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
  8305. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8306. return 1;
  8307. }
  8308. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
  8309. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8310. return 1;
  8311. }
  8312. if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
  8313. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8314. return 1;
  8315. }
  8316. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
  8317. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8318. return 1;
  8319. }
  8320. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  8321. vmx->nested.nested_vmx_true_procbased_ctls_low,
  8322. vmx->nested.nested_vmx_procbased_ctls_high) ||
  8323. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  8324. vmx->nested.nested_vmx_secondary_ctls_low,
  8325. vmx->nested.nested_vmx_secondary_ctls_high) ||
  8326. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  8327. vmx->nested.nested_vmx_pinbased_ctls_low,
  8328. vmx->nested.nested_vmx_pinbased_ctls_high) ||
  8329. !vmx_control_verify(vmcs12->vm_exit_controls,
  8330. vmx->nested.nested_vmx_true_exit_ctls_low,
  8331. vmx->nested.nested_vmx_exit_ctls_high) ||
  8332. !vmx_control_verify(vmcs12->vm_entry_controls,
  8333. vmx->nested.nested_vmx_true_entry_ctls_low,
  8334. vmx->nested.nested_vmx_entry_ctls_high))
  8335. {
  8336. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  8337. return 1;
  8338. }
  8339. if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
  8340. ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  8341. nested_vmx_failValid(vcpu,
  8342. VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
  8343. return 1;
  8344. }
  8345. if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  8346. ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  8347. nested_vmx_entry_failure(vcpu, vmcs12,
  8348. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  8349. return 1;
  8350. }
  8351. if (vmcs12->vmcs_link_pointer != -1ull) {
  8352. nested_vmx_entry_failure(vcpu, vmcs12,
  8353. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
  8354. return 1;
  8355. }
  8356. /*
  8357. * If the load IA32_EFER VM-entry control is 1, the following checks
  8358. * are performed on the field for the IA32_EFER MSR:
  8359. * - Bits reserved in the IA32_EFER MSR must be 0.
  8360. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  8361. * the IA-32e mode guest VM-exit control. It must also be identical
  8362. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  8363. * CR0.PG) is 1.
  8364. */
  8365. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
  8366. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  8367. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  8368. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  8369. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  8370. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
  8371. nested_vmx_entry_failure(vcpu, vmcs12,
  8372. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  8373. return 1;
  8374. }
  8375. }
  8376. /*
  8377. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  8378. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  8379. * the values of the LMA and LME bits in the field must each be that of
  8380. * the host address-space size VM-exit control.
  8381. */
  8382. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  8383. ia32e = (vmcs12->vm_exit_controls &
  8384. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  8385. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  8386. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  8387. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
  8388. nested_vmx_entry_failure(vcpu, vmcs12,
  8389. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  8390. return 1;
  8391. }
  8392. }
  8393. /*
  8394. * We're finally done with prerequisite checking, and can start with
  8395. * the nested entry.
  8396. */
  8397. vmcs02 = nested_get_current_vmcs02(vmx);
  8398. if (!vmcs02)
  8399. return -ENOMEM;
  8400. enter_guest_mode(vcpu);
  8401. vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
  8402. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  8403. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  8404. cpu = get_cpu();
  8405. vmx->loaded_vmcs = vmcs02;
  8406. vmx_vcpu_put(vcpu);
  8407. vmx_vcpu_load(vcpu, cpu);
  8408. vcpu->cpu = cpu;
  8409. put_cpu();
  8410. vmx_segment_cache_clear(vmx);
  8411. prepare_vmcs02(vcpu, vmcs12);
  8412. msr_entry_idx = nested_vmx_load_msr(vcpu,
  8413. vmcs12->vm_entry_msr_load_addr,
  8414. vmcs12->vm_entry_msr_load_count);
  8415. if (msr_entry_idx) {
  8416. leave_guest_mode(vcpu);
  8417. vmx_load_vmcs01(vcpu);
  8418. nested_vmx_entry_failure(vcpu, vmcs12,
  8419. EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
  8420. return 1;
  8421. }
  8422. vmcs12->launch_state = 1;
  8423. if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
  8424. return kvm_vcpu_halt(vcpu);
  8425. vmx->nested.nested_run_pending = 1;
  8426. /*
  8427. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  8428. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  8429. * returned as far as L1 is concerned. It will only return (and set
  8430. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  8431. */
  8432. return 1;
  8433. }
  8434. /*
  8435. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  8436. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  8437. * This function returns the new value we should put in vmcs12.guest_cr0.
  8438. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  8439. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  8440. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  8441. * didn't trap the bit, because if L1 did, so would L0).
  8442. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  8443. * been modified by L2, and L1 knows it. So just leave the old value of
  8444. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  8445. * isn't relevant, because if L0 traps this bit it can set it to anything.
  8446. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  8447. * changed these bits, and therefore they need to be updated, but L0
  8448. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  8449. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  8450. */
  8451. static inline unsigned long
  8452. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  8453. {
  8454. return
  8455. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  8456. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  8457. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  8458. vcpu->arch.cr0_guest_owned_bits));
  8459. }
  8460. static inline unsigned long
  8461. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  8462. {
  8463. return
  8464. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  8465. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  8466. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  8467. vcpu->arch.cr4_guest_owned_bits));
  8468. }
  8469. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  8470. struct vmcs12 *vmcs12)
  8471. {
  8472. u32 idt_vectoring;
  8473. unsigned int nr;
  8474. if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
  8475. nr = vcpu->arch.exception.nr;
  8476. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  8477. if (kvm_exception_is_soft(nr)) {
  8478. vmcs12->vm_exit_instruction_len =
  8479. vcpu->arch.event_exit_inst_len;
  8480. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  8481. } else
  8482. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  8483. if (vcpu->arch.exception.has_error_code) {
  8484. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  8485. vmcs12->idt_vectoring_error_code =
  8486. vcpu->arch.exception.error_code;
  8487. }
  8488. vmcs12->idt_vectoring_info_field = idt_vectoring;
  8489. } else if (vcpu->arch.nmi_injected) {
  8490. vmcs12->idt_vectoring_info_field =
  8491. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  8492. } else if (vcpu->arch.interrupt.pending) {
  8493. nr = vcpu->arch.interrupt.nr;
  8494. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  8495. if (vcpu->arch.interrupt.soft) {
  8496. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  8497. vmcs12->vm_entry_instruction_len =
  8498. vcpu->arch.event_exit_inst_len;
  8499. } else
  8500. idt_vectoring |= INTR_TYPE_EXT_INTR;
  8501. vmcs12->idt_vectoring_info_field = idt_vectoring;
  8502. }
  8503. }
  8504. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  8505. {
  8506. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8507. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  8508. vmx->nested.preemption_timer_expired) {
  8509. if (vmx->nested.nested_run_pending)
  8510. return -EBUSY;
  8511. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  8512. return 0;
  8513. }
  8514. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  8515. if (vmx->nested.nested_run_pending ||
  8516. vcpu->arch.interrupt.pending)
  8517. return -EBUSY;
  8518. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  8519. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  8520. INTR_INFO_VALID_MASK, 0);
  8521. /*
  8522. * The NMI-triggered VM exit counts as injection:
  8523. * clear this one and block further NMIs.
  8524. */
  8525. vcpu->arch.nmi_pending = 0;
  8526. vmx_set_nmi_mask(vcpu, true);
  8527. return 0;
  8528. }
  8529. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  8530. nested_exit_on_intr(vcpu)) {
  8531. if (vmx->nested.nested_run_pending)
  8532. return -EBUSY;
  8533. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  8534. return 0;
  8535. }
  8536. return vmx_complete_nested_posted_interrupt(vcpu);
  8537. }
  8538. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  8539. {
  8540. ktime_t remaining =
  8541. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  8542. u64 value;
  8543. if (ktime_to_ns(remaining) <= 0)
  8544. return 0;
  8545. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  8546. do_div(value, 1000000);
  8547. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  8548. }
  8549. /*
  8550. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  8551. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  8552. * and this function updates it to reflect the changes to the guest state while
  8553. * L2 was running (and perhaps made some exits which were handled directly by L0
  8554. * without going back to L1), and to reflect the exit reason.
  8555. * Note that we do not have to copy here all VMCS fields, just those that
  8556. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  8557. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  8558. * which already writes to vmcs12 directly.
  8559. */
  8560. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  8561. u32 exit_reason, u32 exit_intr_info,
  8562. unsigned long exit_qualification)
  8563. {
  8564. /* update guest state fields: */
  8565. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  8566. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  8567. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  8568. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  8569. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  8570. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  8571. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  8572. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  8573. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  8574. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  8575. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  8576. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  8577. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  8578. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  8579. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  8580. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  8581. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  8582. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  8583. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  8584. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  8585. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  8586. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  8587. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  8588. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  8589. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  8590. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  8591. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  8592. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  8593. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  8594. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  8595. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  8596. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  8597. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  8598. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  8599. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  8600. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  8601. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  8602. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  8603. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  8604. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  8605. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  8606. vmcs12->guest_interruptibility_info =
  8607. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  8608. vmcs12->guest_pending_dbg_exceptions =
  8609. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  8610. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  8611. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  8612. else
  8613. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  8614. if (nested_cpu_has_preemption_timer(vmcs12)) {
  8615. if (vmcs12->vm_exit_controls &
  8616. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  8617. vmcs12->vmx_preemption_timer_value =
  8618. vmx_get_preemption_timer_value(vcpu);
  8619. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  8620. }
  8621. /*
  8622. * In some cases (usually, nested EPT), L2 is allowed to change its
  8623. * own CR3 without exiting. If it has changed it, we must keep it.
  8624. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  8625. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  8626. *
  8627. * Additionally, restore L2's PDPTR to vmcs12.
  8628. */
  8629. if (enable_ept) {
  8630. vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
  8631. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  8632. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  8633. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  8634. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  8635. }
  8636. if (nested_cpu_has_vid(vmcs12))
  8637. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  8638. vmcs12->vm_entry_controls =
  8639. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  8640. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  8641. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  8642. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  8643. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  8644. }
  8645. /* TODO: These cannot have changed unless we have MSR bitmaps and
  8646. * the relevant bit asks not to trap the change */
  8647. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  8648. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  8649. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  8650. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  8651. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  8652. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  8653. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  8654. if (vmx_mpx_supported())
  8655. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  8656. if (nested_cpu_has_xsaves(vmcs12))
  8657. vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
  8658. /* update exit information fields: */
  8659. vmcs12->vm_exit_reason = exit_reason;
  8660. vmcs12->exit_qualification = exit_qualification;
  8661. vmcs12->vm_exit_intr_info = exit_intr_info;
  8662. if ((vmcs12->vm_exit_intr_info &
  8663. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  8664. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
  8665. vmcs12->vm_exit_intr_error_code =
  8666. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  8667. vmcs12->idt_vectoring_info_field = 0;
  8668. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  8669. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8670. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  8671. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  8672. * instead of reading the real value. */
  8673. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  8674. /*
  8675. * Transfer the event that L0 or L1 may wanted to inject into
  8676. * L2 to IDT_VECTORING_INFO_FIELD.
  8677. */
  8678. vmcs12_save_pending_event(vcpu, vmcs12);
  8679. }
  8680. /*
  8681. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  8682. * preserved above and would only end up incorrectly in L1.
  8683. */
  8684. vcpu->arch.nmi_injected = false;
  8685. kvm_clear_exception_queue(vcpu);
  8686. kvm_clear_interrupt_queue(vcpu);
  8687. }
  8688. /*
  8689. * A part of what we need to when the nested L2 guest exits and we want to
  8690. * run its L1 parent, is to reset L1's guest state to the host state specified
  8691. * in vmcs12.
  8692. * This function is to be called not only on normal nested exit, but also on
  8693. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  8694. * Failures During or After Loading Guest State").
  8695. * This function should be called when the active VMCS is L1's (vmcs01).
  8696. */
  8697. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  8698. struct vmcs12 *vmcs12)
  8699. {
  8700. struct kvm_segment seg;
  8701. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  8702. vcpu->arch.efer = vmcs12->host_ia32_efer;
  8703. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  8704. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  8705. else
  8706. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  8707. vmx_set_efer(vcpu, vcpu->arch.efer);
  8708. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  8709. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  8710. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  8711. /*
  8712. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  8713. * actually changed, because it depends on the current state of
  8714. * fpu_active (which may have changed).
  8715. * Note that vmx_set_cr0 refers to efer set above.
  8716. */
  8717. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  8718. /*
  8719. * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
  8720. * to apply the same changes to L1's vmcs. We just set cr0 correctly,
  8721. * but we also need to update cr0_guest_host_mask and exception_bitmap.
  8722. */
  8723. update_exception_bitmap(vcpu);
  8724. vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
  8725. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  8726. /*
  8727. * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
  8728. * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
  8729. */
  8730. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  8731. kvm_set_cr4(vcpu, vmcs12->host_cr4);
  8732. nested_ept_uninit_mmu_context(vcpu);
  8733. kvm_set_cr3(vcpu, vmcs12->host_cr3);
  8734. kvm_mmu_reset_context(vcpu);
  8735. if (!enable_ept)
  8736. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  8737. if (enable_vpid) {
  8738. /*
  8739. * Trivially support vpid by letting L2s share their parent
  8740. * L1's vpid. TODO: move to a more elaborate solution, giving
  8741. * each L2 its own vpid and exposing the vpid feature to L1.
  8742. */
  8743. vmx_flush_tlb(vcpu);
  8744. }
  8745. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  8746. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  8747. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  8748. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  8749. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  8750. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  8751. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  8752. vmcs_write64(GUEST_BNDCFGS, 0);
  8753. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  8754. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  8755. vcpu->arch.pat = vmcs12->host_ia32_pat;
  8756. }
  8757. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8758. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  8759. vmcs12->host_ia32_perf_global_ctrl);
  8760. /* Set L1 segment info according to Intel SDM
  8761. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  8762. seg = (struct kvm_segment) {
  8763. .base = 0,
  8764. .limit = 0xFFFFFFFF,
  8765. .selector = vmcs12->host_cs_selector,
  8766. .type = 11,
  8767. .present = 1,
  8768. .s = 1,
  8769. .g = 1
  8770. };
  8771. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  8772. seg.l = 1;
  8773. else
  8774. seg.db = 1;
  8775. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  8776. seg = (struct kvm_segment) {
  8777. .base = 0,
  8778. .limit = 0xFFFFFFFF,
  8779. .type = 3,
  8780. .present = 1,
  8781. .s = 1,
  8782. .db = 1,
  8783. .g = 1
  8784. };
  8785. seg.selector = vmcs12->host_ds_selector;
  8786. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  8787. seg.selector = vmcs12->host_es_selector;
  8788. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  8789. seg.selector = vmcs12->host_ss_selector;
  8790. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  8791. seg.selector = vmcs12->host_fs_selector;
  8792. seg.base = vmcs12->host_fs_base;
  8793. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  8794. seg.selector = vmcs12->host_gs_selector;
  8795. seg.base = vmcs12->host_gs_base;
  8796. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  8797. seg = (struct kvm_segment) {
  8798. .base = vmcs12->host_tr_base,
  8799. .limit = 0x67,
  8800. .selector = vmcs12->host_tr_selector,
  8801. .type = 11,
  8802. .present = 1
  8803. };
  8804. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  8805. kvm_set_dr(vcpu, 7, 0x400);
  8806. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  8807. if (cpu_has_vmx_msr_bitmap())
  8808. vmx_set_msr_bitmap(vcpu);
  8809. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  8810. vmcs12->vm_exit_msr_load_count))
  8811. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  8812. }
  8813. /*
  8814. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  8815. * and modify vmcs12 to make it see what it would expect to see there if
  8816. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  8817. */
  8818. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  8819. u32 exit_intr_info,
  8820. unsigned long exit_qualification)
  8821. {
  8822. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8823. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8824. /* trying to cancel vmlaunch/vmresume is a bug */
  8825. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  8826. leave_guest_mode(vcpu);
  8827. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  8828. exit_qualification);
  8829. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  8830. vmcs12->vm_exit_msr_store_count))
  8831. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  8832. vmx_load_vmcs01(vcpu);
  8833. if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
  8834. && nested_exit_intr_ack_set(vcpu)) {
  8835. int irq = kvm_cpu_get_interrupt(vcpu);
  8836. WARN_ON(irq < 0);
  8837. vmcs12->vm_exit_intr_info = irq |
  8838. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  8839. }
  8840. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  8841. vmcs12->exit_qualification,
  8842. vmcs12->idt_vectoring_info_field,
  8843. vmcs12->vm_exit_intr_info,
  8844. vmcs12->vm_exit_intr_error_code,
  8845. KVM_ISA_VMX);
  8846. vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
  8847. vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
  8848. vmx_segment_cache_clear(vmx);
  8849. /* if no vmcs02 cache requested, remove the one we used */
  8850. if (VMCS02_POOL_SIZE == 0)
  8851. nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
  8852. load_vmcs12_host_state(vcpu, vmcs12);
  8853. /* Update TSC_OFFSET if TSC was changed while L2 ran */
  8854. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  8855. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  8856. vmx->host_rsp = 0;
  8857. /* Unpin physical memory we referred to in vmcs02 */
  8858. if (vmx->nested.apic_access_page) {
  8859. nested_release_page(vmx->nested.apic_access_page);
  8860. vmx->nested.apic_access_page = NULL;
  8861. }
  8862. if (vmx->nested.virtual_apic_page) {
  8863. nested_release_page(vmx->nested.virtual_apic_page);
  8864. vmx->nested.virtual_apic_page = NULL;
  8865. }
  8866. if (vmx->nested.pi_desc_page) {
  8867. kunmap(vmx->nested.pi_desc_page);
  8868. nested_release_page(vmx->nested.pi_desc_page);
  8869. vmx->nested.pi_desc_page = NULL;
  8870. vmx->nested.pi_desc = NULL;
  8871. }
  8872. /*
  8873. * We are now running in L2, mmu_notifier will force to reload the
  8874. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  8875. */
  8876. kvm_vcpu_reload_apic_access_page(vcpu);
  8877. /*
  8878. * Exiting from L2 to L1, we're now back to L1 which thinks it just
  8879. * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
  8880. * success or failure flag accordingly.
  8881. */
  8882. if (unlikely(vmx->fail)) {
  8883. vmx->fail = 0;
  8884. nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
  8885. } else
  8886. nested_vmx_succeed(vcpu);
  8887. if (enable_shadow_vmcs)
  8888. vmx->nested.sync_shadow_vmcs = true;
  8889. /* in case we halted in L2 */
  8890. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  8891. }
  8892. /*
  8893. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  8894. */
  8895. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  8896. {
  8897. if (is_guest_mode(vcpu))
  8898. nested_vmx_vmexit(vcpu, -1, 0, 0);
  8899. free_nested(to_vmx(vcpu));
  8900. }
  8901. /*
  8902. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  8903. * 23.7 "VM-entry failures during or after loading guest state" (this also
  8904. * lists the acceptable exit-reason and exit-qualification parameters).
  8905. * It should only be called before L2 actually succeeded to run, and when
  8906. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  8907. */
  8908. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  8909. struct vmcs12 *vmcs12,
  8910. u32 reason, unsigned long qualification)
  8911. {
  8912. load_vmcs12_host_state(vcpu, vmcs12);
  8913. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  8914. vmcs12->exit_qualification = qualification;
  8915. nested_vmx_succeed(vcpu);
  8916. if (enable_shadow_vmcs)
  8917. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  8918. }
  8919. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  8920. struct x86_instruction_info *info,
  8921. enum x86_intercept_stage stage)
  8922. {
  8923. return X86EMUL_CONTINUE;
  8924. }
  8925. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  8926. {
  8927. if (ple_gap)
  8928. shrink_ple_window(vcpu);
  8929. }
  8930. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  8931. struct kvm_memory_slot *slot)
  8932. {
  8933. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  8934. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  8935. }
  8936. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  8937. struct kvm_memory_slot *slot)
  8938. {
  8939. kvm_mmu_slot_set_dirty(kvm, slot);
  8940. }
  8941. static void vmx_flush_log_dirty(struct kvm *kvm)
  8942. {
  8943. kvm_flush_pml_buffers(kvm);
  8944. }
  8945. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  8946. struct kvm_memory_slot *memslot,
  8947. gfn_t offset, unsigned long mask)
  8948. {
  8949. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  8950. }
  8951. static struct kvm_x86_ops vmx_x86_ops = {
  8952. .cpu_has_kvm_support = cpu_has_kvm_support,
  8953. .disabled_by_bios = vmx_disabled_by_bios,
  8954. .hardware_setup = hardware_setup,
  8955. .hardware_unsetup = hardware_unsetup,
  8956. .check_processor_compatibility = vmx_check_processor_compat,
  8957. .hardware_enable = hardware_enable,
  8958. .hardware_disable = hardware_disable,
  8959. .cpu_has_accelerated_tpr = report_flexpriority,
  8960. .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
  8961. .vcpu_create = vmx_create_vcpu,
  8962. .vcpu_free = vmx_free_vcpu,
  8963. .vcpu_reset = vmx_vcpu_reset,
  8964. .prepare_guest_switch = vmx_save_host_state,
  8965. .vcpu_load = vmx_vcpu_load,
  8966. .vcpu_put = vmx_vcpu_put,
  8967. .update_db_bp_intercept = update_exception_bitmap,
  8968. .get_msr = vmx_get_msr,
  8969. .set_msr = vmx_set_msr,
  8970. .get_segment_base = vmx_get_segment_base,
  8971. .get_segment = vmx_get_segment,
  8972. .set_segment = vmx_set_segment,
  8973. .get_cpl = vmx_get_cpl,
  8974. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  8975. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  8976. .decache_cr3 = vmx_decache_cr3,
  8977. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  8978. .set_cr0 = vmx_set_cr0,
  8979. .set_cr3 = vmx_set_cr3,
  8980. .set_cr4 = vmx_set_cr4,
  8981. .set_efer = vmx_set_efer,
  8982. .get_idt = vmx_get_idt,
  8983. .set_idt = vmx_set_idt,
  8984. .get_gdt = vmx_get_gdt,
  8985. .set_gdt = vmx_set_gdt,
  8986. .get_dr6 = vmx_get_dr6,
  8987. .set_dr6 = vmx_set_dr6,
  8988. .set_dr7 = vmx_set_dr7,
  8989. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  8990. .cache_reg = vmx_cache_reg,
  8991. .get_rflags = vmx_get_rflags,
  8992. .set_rflags = vmx_set_rflags,
  8993. .fpu_activate = vmx_fpu_activate,
  8994. .fpu_deactivate = vmx_fpu_deactivate,
  8995. .tlb_flush = vmx_flush_tlb,
  8996. .run = vmx_vcpu_run,
  8997. .handle_exit = vmx_handle_exit,
  8998. .skip_emulated_instruction = skip_emulated_instruction,
  8999. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  9000. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  9001. .patch_hypercall = vmx_patch_hypercall,
  9002. .set_irq = vmx_inject_irq,
  9003. .set_nmi = vmx_inject_nmi,
  9004. .queue_exception = vmx_queue_exception,
  9005. .cancel_injection = vmx_cancel_injection,
  9006. .interrupt_allowed = vmx_interrupt_allowed,
  9007. .nmi_allowed = vmx_nmi_allowed,
  9008. .get_nmi_mask = vmx_get_nmi_mask,
  9009. .set_nmi_mask = vmx_set_nmi_mask,
  9010. .enable_nmi_window = enable_nmi_window,
  9011. .enable_irq_window = enable_irq_window,
  9012. .update_cr8_intercept = update_cr8_intercept,
  9013. .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
  9014. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  9015. .vm_has_apicv = vmx_vm_has_apicv,
  9016. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  9017. .hwapic_irr_update = vmx_hwapic_irr_update,
  9018. .hwapic_isr_update = vmx_hwapic_isr_update,
  9019. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  9020. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  9021. .set_tss_addr = vmx_set_tss_addr,
  9022. .get_tdp_level = get_ept_level,
  9023. .get_mt_mask = vmx_get_mt_mask,
  9024. .get_exit_info = vmx_get_exit_info,
  9025. .get_lpage_level = vmx_get_lpage_level,
  9026. .cpuid_update = vmx_cpuid_update,
  9027. .rdtscp_supported = vmx_rdtscp_supported,
  9028. .invpcid_supported = vmx_invpcid_supported,
  9029. .set_supported_cpuid = vmx_set_supported_cpuid,
  9030. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  9031. .set_tsc_khz = vmx_set_tsc_khz,
  9032. .read_tsc_offset = vmx_read_tsc_offset,
  9033. .write_tsc_offset = vmx_write_tsc_offset,
  9034. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  9035. .compute_tsc_offset = vmx_compute_tsc_offset,
  9036. .read_l1_tsc = vmx_read_l1_tsc,
  9037. .set_tdp_cr3 = vmx_set_cr3,
  9038. .check_intercept = vmx_check_intercept,
  9039. .handle_external_intr = vmx_handle_external_intr,
  9040. .mpx_supported = vmx_mpx_supported,
  9041. .xsaves_supported = vmx_xsaves_supported,
  9042. .check_nested_events = vmx_check_nested_events,
  9043. .sched_in = vmx_sched_in,
  9044. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  9045. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  9046. .flush_log_dirty = vmx_flush_log_dirty,
  9047. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  9048. .pmu_ops = &intel_pmu_ops,
  9049. };
  9050. static int __init vmx_init(void)
  9051. {
  9052. int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  9053. __alignof__(struct vcpu_vmx), THIS_MODULE);
  9054. if (r)
  9055. return r;
  9056. #ifdef CONFIG_KEXEC_CORE
  9057. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  9058. crash_vmclear_local_loaded_vmcss);
  9059. #endif
  9060. return 0;
  9061. }
  9062. static void __exit vmx_exit(void)
  9063. {
  9064. #ifdef CONFIG_KEXEC_CORE
  9065. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  9066. synchronize_rcu();
  9067. #endif
  9068. kvm_exit();
  9069. }
  9070. module_init(vmx_init)
  9071. module_exit(vmx_exit)