vmx.c 319 KB

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