vmx.c 386 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686
  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 <linux/frame.h>
  35. #include <linux/nospec.h>
  36. #include "kvm_cache_regs.h"
  37. #include "x86.h"
  38. #include <asm/cpu.h>
  39. #include <asm/io.h>
  40. #include <asm/desc.h>
  41. #include <asm/vmx.h>
  42. #include <asm/virtext.h>
  43. #include <asm/mce.h>
  44. #include <asm/fpu/internal.h>
  45. #include <asm/perf_event.h>
  46. #include <asm/debugreg.h>
  47. #include <asm/kexec.h>
  48. #include <asm/apic.h>
  49. #include <asm/irq_remapping.h>
  50. #include <asm/mmu_context.h>
  51. #include <asm/spec-ctrl.h>
  52. #include <asm/mshyperv.h>
  53. #include "trace.h"
  54. #include "pmu.h"
  55. #include "vmx_evmcs.h"
  56. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  57. #define __ex_clear(x, reg) \
  58. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  59. MODULE_AUTHOR("Qumranet");
  60. MODULE_LICENSE("GPL");
  61. static const struct x86_cpu_id vmx_cpu_id[] = {
  62. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  63. {}
  64. };
  65. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  66. static bool __read_mostly enable_vpid = 1;
  67. module_param_named(vpid, enable_vpid, bool, 0444);
  68. static bool __read_mostly enable_vnmi = 1;
  69. module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
  70. static bool __read_mostly flexpriority_enabled = 1;
  71. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  72. static bool __read_mostly enable_ept = 1;
  73. module_param_named(ept, enable_ept, bool, S_IRUGO);
  74. static bool __read_mostly enable_unrestricted_guest = 1;
  75. module_param_named(unrestricted_guest,
  76. enable_unrestricted_guest, bool, S_IRUGO);
  77. static bool __read_mostly enable_ept_ad_bits = 1;
  78. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  79. static bool __read_mostly emulate_invalid_guest_state = true;
  80. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  81. static bool __read_mostly fasteoi = 1;
  82. module_param(fasteoi, bool, S_IRUGO);
  83. static bool __read_mostly enable_apicv = 1;
  84. module_param(enable_apicv, bool, S_IRUGO);
  85. static bool __read_mostly enable_shadow_vmcs = 1;
  86. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  87. /*
  88. * If nested=1, nested virtualization is supported, i.e., guests may use
  89. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  90. * use VMX instructions.
  91. */
  92. static bool __read_mostly nested = 0;
  93. module_param(nested, bool, S_IRUGO);
  94. static u64 __read_mostly host_xss;
  95. static bool __read_mostly enable_pml = 1;
  96. module_param_named(pml, enable_pml, bool, S_IRUGO);
  97. #define MSR_TYPE_R 1
  98. #define MSR_TYPE_W 2
  99. #define MSR_TYPE_RW 3
  100. #define MSR_BITMAP_MODE_X2APIC 1
  101. #define MSR_BITMAP_MODE_X2APIC_APICV 2
  102. #define MSR_BITMAP_MODE_LM 4
  103. #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
  104. /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
  105. static int __read_mostly cpu_preemption_timer_multi;
  106. static bool __read_mostly enable_preemption_timer = 1;
  107. #ifdef CONFIG_X86_64
  108. module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
  109. #endif
  110. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  111. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
  112. #define KVM_VM_CR0_ALWAYS_ON \
  113. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
  114. X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
  115. #define KVM_CR4_GUEST_OWNED_BITS \
  116. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  117. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
  118. #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
  119. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  120. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  121. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  122. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  123. /*
  124. * Hyper-V requires all of these, so mark them as supported even though
  125. * they are just treated the same as all-context.
  126. */
  127. #define VMX_VPID_EXTENT_SUPPORTED_MASK \
  128. (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
  129. VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
  130. VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
  131. VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
  132. /*
  133. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  134. * ple_gap: upper bound on the amount of time between two successive
  135. * executions of PAUSE in a loop. Also indicate if ple enabled.
  136. * According to test, this time is usually smaller than 128 cycles.
  137. * ple_window: upper bound on the amount of time a guest is allowed to execute
  138. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  139. * less than 2^12 cycles
  140. * Time is measured based on a counter that runs at the same rate as the TSC,
  141. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  142. */
  143. static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
  144. static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  145. module_param(ple_window, uint, 0444);
  146. /* Default doubles per-vcpu window every exit. */
  147. static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  148. module_param(ple_window_grow, uint, 0444);
  149. /* Default resets per-vcpu window every exit to ple_window. */
  150. static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  151. module_param(ple_window_shrink, uint, 0444);
  152. /* Default is to compute the maximum so we can never overflow. */
  153. static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  154. module_param(ple_window_max, uint, 0444);
  155. extern const ulong vmx_return;
  156. struct kvm_vmx {
  157. struct kvm kvm;
  158. unsigned int tss_addr;
  159. bool ept_identity_pagetable_done;
  160. gpa_t ept_identity_map_addr;
  161. };
  162. #define NR_AUTOLOAD_MSRS 8
  163. struct vmcs_hdr {
  164. u32 revision_id:31;
  165. u32 shadow_vmcs:1;
  166. };
  167. struct vmcs {
  168. struct vmcs_hdr hdr;
  169. u32 abort;
  170. char data[0];
  171. };
  172. /*
  173. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  174. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  175. * loaded on this CPU (so we can clear them if the CPU goes down).
  176. */
  177. struct loaded_vmcs {
  178. struct vmcs *vmcs;
  179. struct vmcs *shadow_vmcs;
  180. int cpu;
  181. bool launched;
  182. bool nmi_known_unmasked;
  183. unsigned long vmcs_host_cr3; /* May not match real cr3 */
  184. unsigned long vmcs_host_cr4; /* May not match real cr4 */
  185. /* Support for vnmi-less CPUs */
  186. int soft_vnmi_blocked;
  187. ktime_t entry_time;
  188. s64 vnmi_blocked_time;
  189. unsigned long *msr_bitmap;
  190. struct list_head loaded_vmcss_on_cpu_link;
  191. };
  192. struct shared_msr_entry {
  193. unsigned index;
  194. u64 data;
  195. u64 mask;
  196. };
  197. /*
  198. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  199. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  200. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  201. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  202. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  203. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  204. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  205. * underlying hardware which will be used to run L2.
  206. * This structure is packed to ensure that its layout is identical across
  207. * machines (necessary for live migration).
  208. *
  209. * IMPORTANT: Changing the layout of existing fields in this structure
  210. * will break save/restore compatibility with older kvm releases. When
  211. * adding new fields, either use space in the reserved padding* arrays
  212. * or add the new fields to the end of the structure.
  213. */
  214. typedef u64 natural_width;
  215. struct __packed vmcs12 {
  216. /* According to the Intel spec, a VMCS region must start with the
  217. * following two fields. Then follow implementation-specific data.
  218. */
  219. struct vmcs_hdr hdr;
  220. u32 abort;
  221. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  222. u32 padding[7]; /* room for future expansion */
  223. u64 io_bitmap_a;
  224. u64 io_bitmap_b;
  225. u64 msr_bitmap;
  226. u64 vm_exit_msr_store_addr;
  227. u64 vm_exit_msr_load_addr;
  228. u64 vm_entry_msr_load_addr;
  229. u64 tsc_offset;
  230. u64 virtual_apic_page_addr;
  231. u64 apic_access_addr;
  232. u64 posted_intr_desc_addr;
  233. u64 ept_pointer;
  234. u64 eoi_exit_bitmap0;
  235. u64 eoi_exit_bitmap1;
  236. u64 eoi_exit_bitmap2;
  237. u64 eoi_exit_bitmap3;
  238. u64 xss_exit_bitmap;
  239. u64 guest_physical_address;
  240. u64 vmcs_link_pointer;
  241. u64 guest_ia32_debugctl;
  242. u64 guest_ia32_pat;
  243. u64 guest_ia32_efer;
  244. u64 guest_ia32_perf_global_ctrl;
  245. u64 guest_pdptr0;
  246. u64 guest_pdptr1;
  247. u64 guest_pdptr2;
  248. u64 guest_pdptr3;
  249. u64 guest_bndcfgs;
  250. u64 host_ia32_pat;
  251. u64 host_ia32_efer;
  252. u64 host_ia32_perf_global_ctrl;
  253. u64 vmread_bitmap;
  254. u64 vmwrite_bitmap;
  255. u64 vm_function_control;
  256. u64 eptp_list_address;
  257. u64 pml_address;
  258. u64 padding64[3]; /* room for future expansion */
  259. /*
  260. * To allow migration of L1 (complete with its L2 guests) between
  261. * machines of different natural widths (32 or 64 bit), we cannot have
  262. * unsigned long fields with no explict size. We use u64 (aliased
  263. * natural_width) instead. Luckily, x86 is little-endian.
  264. */
  265. natural_width cr0_guest_host_mask;
  266. natural_width cr4_guest_host_mask;
  267. natural_width cr0_read_shadow;
  268. natural_width cr4_read_shadow;
  269. natural_width cr3_target_value0;
  270. natural_width cr3_target_value1;
  271. natural_width cr3_target_value2;
  272. natural_width cr3_target_value3;
  273. natural_width exit_qualification;
  274. natural_width guest_linear_address;
  275. natural_width guest_cr0;
  276. natural_width guest_cr3;
  277. natural_width guest_cr4;
  278. natural_width guest_es_base;
  279. natural_width guest_cs_base;
  280. natural_width guest_ss_base;
  281. natural_width guest_ds_base;
  282. natural_width guest_fs_base;
  283. natural_width guest_gs_base;
  284. natural_width guest_ldtr_base;
  285. natural_width guest_tr_base;
  286. natural_width guest_gdtr_base;
  287. natural_width guest_idtr_base;
  288. natural_width guest_dr7;
  289. natural_width guest_rsp;
  290. natural_width guest_rip;
  291. natural_width guest_rflags;
  292. natural_width guest_pending_dbg_exceptions;
  293. natural_width guest_sysenter_esp;
  294. natural_width guest_sysenter_eip;
  295. natural_width host_cr0;
  296. natural_width host_cr3;
  297. natural_width host_cr4;
  298. natural_width host_fs_base;
  299. natural_width host_gs_base;
  300. natural_width host_tr_base;
  301. natural_width host_gdtr_base;
  302. natural_width host_idtr_base;
  303. natural_width host_ia32_sysenter_esp;
  304. natural_width host_ia32_sysenter_eip;
  305. natural_width host_rsp;
  306. natural_width host_rip;
  307. natural_width paddingl[8]; /* room for future expansion */
  308. u32 pin_based_vm_exec_control;
  309. u32 cpu_based_vm_exec_control;
  310. u32 exception_bitmap;
  311. u32 page_fault_error_code_mask;
  312. u32 page_fault_error_code_match;
  313. u32 cr3_target_count;
  314. u32 vm_exit_controls;
  315. u32 vm_exit_msr_store_count;
  316. u32 vm_exit_msr_load_count;
  317. u32 vm_entry_controls;
  318. u32 vm_entry_msr_load_count;
  319. u32 vm_entry_intr_info_field;
  320. u32 vm_entry_exception_error_code;
  321. u32 vm_entry_instruction_len;
  322. u32 tpr_threshold;
  323. u32 secondary_vm_exec_control;
  324. u32 vm_instruction_error;
  325. u32 vm_exit_reason;
  326. u32 vm_exit_intr_info;
  327. u32 vm_exit_intr_error_code;
  328. u32 idt_vectoring_info_field;
  329. u32 idt_vectoring_error_code;
  330. u32 vm_exit_instruction_len;
  331. u32 vmx_instruction_info;
  332. u32 guest_es_limit;
  333. u32 guest_cs_limit;
  334. u32 guest_ss_limit;
  335. u32 guest_ds_limit;
  336. u32 guest_fs_limit;
  337. u32 guest_gs_limit;
  338. u32 guest_ldtr_limit;
  339. u32 guest_tr_limit;
  340. u32 guest_gdtr_limit;
  341. u32 guest_idtr_limit;
  342. u32 guest_es_ar_bytes;
  343. u32 guest_cs_ar_bytes;
  344. u32 guest_ss_ar_bytes;
  345. u32 guest_ds_ar_bytes;
  346. u32 guest_fs_ar_bytes;
  347. u32 guest_gs_ar_bytes;
  348. u32 guest_ldtr_ar_bytes;
  349. u32 guest_tr_ar_bytes;
  350. u32 guest_interruptibility_info;
  351. u32 guest_activity_state;
  352. u32 guest_sysenter_cs;
  353. u32 host_ia32_sysenter_cs;
  354. u32 vmx_preemption_timer_value;
  355. u32 padding32[7]; /* room for future expansion */
  356. u16 virtual_processor_id;
  357. u16 posted_intr_nv;
  358. u16 guest_es_selector;
  359. u16 guest_cs_selector;
  360. u16 guest_ss_selector;
  361. u16 guest_ds_selector;
  362. u16 guest_fs_selector;
  363. u16 guest_gs_selector;
  364. u16 guest_ldtr_selector;
  365. u16 guest_tr_selector;
  366. u16 guest_intr_status;
  367. u16 host_es_selector;
  368. u16 host_cs_selector;
  369. u16 host_ss_selector;
  370. u16 host_ds_selector;
  371. u16 host_fs_selector;
  372. u16 host_gs_selector;
  373. u16 host_tr_selector;
  374. u16 guest_pml_index;
  375. };
  376. /*
  377. * For save/restore compatibility, the vmcs12 field offsets must not change.
  378. */
  379. #define CHECK_OFFSET(field, loc) \
  380. BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
  381. "Offset of " #field " in struct vmcs12 has changed.")
  382. static inline void vmx_check_vmcs12_offsets(void) {
  383. CHECK_OFFSET(hdr, 0);
  384. CHECK_OFFSET(abort, 4);
  385. CHECK_OFFSET(launch_state, 8);
  386. CHECK_OFFSET(io_bitmap_a, 40);
  387. CHECK_OFFSET(io_bitmap_b, 48);
  388. CHECK_OFFSET(msr_bitmap, 56);
  389. CHECK_OFFSET(vm_exit_msr_store_addr, 64);
  390. CHECK_OFFSET(vm_exit_msr_load_addr, 72);
  391. CHECK_OFFSET(vm_entry_msr_load_addr, 80);
  392. CHECK_OFFSET(tsc_offset, 88);
  393. CHECK_OFFSET(virtual_apic_page_addr, 96);
  394. CHECK_OFFSET(apic_access_addr, 104);
  395. CHECK_OFFSET(posted_intr_desc_addr, 112);
  396. CHECK_OFFSET(ept_pointer, 120);
  397. CHECK_OFFSET(eoi_exit_bitmap0, 128);
  398. CHECK_OFFSET(eoi_exit_bitmap1, 136);
  399. CHECK_OFFSET(eoi_exit_bitmap2, 144);
  400. CHECK_OFFSET(eoi_exit_bitmap3, 152);
  401. CHECK_OFFSET(xss_exit_bitmap, 160);
  402. CHECK_OFFSET(guest_physical_address, 168);
  403. CHECK_OFFSET(vmcs_link_pointer, 176);
  404. CHECK_OFFSET(guest_ia32_debugctl, 184);
  405. CHECK_OFFSET(guest_ia32_pat, 192);
  406. CHECK_OFFSET(guest_ia32_efer, 200);
  407. CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
  408. CHECK_OFFSET(guest_pdptr0, 216);
  409. CHECK_OFFSET(guest_pdptr1, 224);
  410. CHECK_OFFSET(guest_pdptr2, 232);
  411. CHECK_OFFSET(guest_pdptr3, 240);
  412. CHECK_OFFSET(guest_bndcfgs, 248);
  413. CHECK_OFFSET(host_ia32_pat, 256);
  414. CHECK_OFFSET(host_ia32_efer, 264);
  415. CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
  416. CHECK_OFFSET(vmread_bitmap, 280);
  417. CHECK_OFFSET(vmwrite_bitmap, 288);
  418. CHECK_OFFSET(vm_function_control, 296);
  419. CHECK_OFFSET(eptp_list_address, 304);
  420. CHECK_OFFSET(pml_address, 312);
  421. CHECK_OFFSET(cr0_guest_host_mask, 344);
  422. CHECK_OFFSET(cr4_guest_host_mask, 352);
  423. CHECK_OFFSET(cr0_read_shadow, 360);
  424. CHECK_OFFSET(cr4_read_shadow, 368);
  425. CHECK_OFFSET(cr3_target_value0, 376);
  426. CHECK_OFFSET(cr3_target_value1, 384);
  427. CHECK_OFFSET(cr3_target_value2, 392);
  428. CHECK_OFFSET(cr3_target_value3, 400);
  429. CHECK_OFFSET(exit_qualification, 408);
  430. CHECK_OFFSET(guest_linear_address, 416);
  431. CHECK_OFFSET(guest_cr0, 424);
  432. CHECK_OFFSET(guest_cr3, 432);
  433. CHECK_OFFSET(guest_cr4, 440);
  434. CHECK_OFFSET(guest_es_base, 448);
  435. CHECK_OFFSET(guest_cs_base, 456);
  436. CHECK_OFFSET(guest_ss_base, 464);
  437. CHECK_OFFSET(guest_ds_base, 472);
  438. CHECK_OFFSET(guest_fs_base, 480);
  439. CHECK_OFFSET(guest_gs_base, 488);
  440. CHECK_OFFSET(guest_ldtr_base, 496);
  441. CHECK_OFFSET(guest_tr_base, 504);
  442. CHECK_OFFSET(guest_gdtr_base, 512);
  443. CHECK_OFFSET(guest_idtr_base, 520);
  444. CHECK_OFFSET(guest_dr7, 528);
  445. CHECK_OFFSET(guest_rsp, 536);
  446. CHECK_OFFSET(guest_rip, 544);
  447. CHECK_OFFSET(guest_rflags, 552);
  448. CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
  449. CHECK_OFFSET(guest_sysenter_esp, 568);
  450. CHECK_OFFSET(guest_sysenter_eip, 576);
  451. CHECK_OFFSET(host_cr0, 584);
  452. CHECK_OFFSET(host_cr3, 592);
  453. CHECK_OFFSET(host_cr4, 600);
  454. CHECK_OFFSET(host_fs_base, 608);
  455. CHECK_OFFSET(host_gs_base, 616);
  456. CHECK_OFFSET(host_tr_base, 624);
  457. CHECK_OFFSET(host_gdtr_base, 632);
  458. CHECK_OFFSET(host_idtr_base, 640);
  459. CHECK_OFFSET(host_ia32_sysenter_esp, 648);
  460. CHECK_OFFSET(host_ia32_sysenter_eip, 656);
  461. CHECK_OFFSET(host_rsp, 664);
  462. CHECK_OFFSET(host_rip, 672);
  463. CHECK_OFFSET(pin_based_vm_exec_control, 744);
  464. CHECK_OFFSET(cpu_based_vm_exec_control, 748);
  465. CHECK_OFFSET(exception_bitmap, 752);
  466. CHECK_OFFSET(page_fault_error_code_mask, 756);
  467. CHECK_OFFSET(page_fault_error_code_match, 760);
  468. CHECK_OFFSET(cr3_target_count, 764);
  469. CHECK_OFFSET(vm_exit_controls, 768);
  470. CHECK_OFFSET(vm_exit_msr_store_count, 772);
  471. CHECK_OFFSET(vm_exit_msr_load_count, 776);
  472. CHECK_OFFSET(vm_entry_controls, 780);
  473. CHECK_OFFSET(vm_entry_msr_load_count, 784);
  474. CHECK_OFFSET(vm_entry_intr_info_field, 788);
  475. CHECK_OFFSET(vm_entry_exception_error_code, 792);
  476. CHECK_OFFSET(vm_entry_instruction_len, 796);
  477. CHECK_OFFSET(tpr_threshold, 800);
  478. CHECK_OFFSET(secondary_vm_exec_control, 804);
  479. CHECK_OFFSET(vm_instruction_error, 808);
  480. CHECK_OFFSET(vm_exit_reason, 812);
  481. CHECK_OFFSET(vm_exit_intr_info, 816);
  482. CHECK_OFFSET(vm_exit_intr_error_code, 820);
  483. CHECK_OFFSET(idt_vectoring_info_field, 824);
  484. CHECK_OFFSET(idt_vectoring_error_code, 828);
  485. CHECK_OFFSET(vm_exit_instruction_len, 832);
  486. CHECK_OFFSET(vmx_instruction_info, 836);
  487. CHECK_OFFSET(guest_es_limit, 840);
  488. CHECK_OFFSET(guest_cs_limit, 844);
  489. CHECK_OFFSET(guest_ss_limit, 848);
  490. CHECK_OFFSET(guest_ds_limit, 852);
  491. CHECK_OFFSET(guest_fs_limit, 856);
  492. CHECK_OFFSET(guest_gs_limit, 860);
  493. CHECK_OFFSET(guest_ldtr_limit, 864);
  494. CHECK_OFFSET(guest_tr_limit, 868);
  495. CHECK_OFFSET(guest_gdtr_limit, 872);
  496. CHECK_OFFSET(guest_idtr_limit, 876);
  497. CHECK_OFFSET(guest_es_ar_bytes, 880);
  498. CHECK_OFFSET(guest_cs_ar_bytes, 884);
  499. CHECK_OFFSET(guest_ss_ar_bytes, 888);
  500. CHECK_OFFSET(guest_ds_ar_bytes, 892);
  501. CHECK_OFFSET(guest_fs_ar_bytes, 896);
  502. CHECK_OFFSET(guest_gs_ar_bytes, 900);
  503. CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
  504. CHECK_OFFSET(guest_tr_ar_bytes, 908);
  505. CHECK_OFFSET(guest_interruptibility_info, 912);
  506. CHECK_OFFSET(guest_activity_state, 916);
  507. CHECK_OFFSET(guest_sysenter_cs, 920);
  508. CHECK_OFFSET(host_ia32_sysenter_cs, 924);
  509. CHECK_OFFSET(vmx_preemption_timer_value, 928);
  510. CHECK_OFFSET(virtual_processor_id, 960);
  511. CHECK_OFFSET(posted_intr_nv, 962);
  512. CHECK_OFFSET(guest_es_selector, 964);
  513. CHECK_OFFSET(guest_cs_selector, 966);
  514. CHECK_OFFSET(guest_ss_selector, 968);
  515. CHECK_OFFSET(guest_ds_selector, 970);
  516. CHECK_OFFSET(guest_fs_selector, 972);
  517. CHECK_OFFSET(guest_gs_selector, 974);
  518. CHECK_OFFSET(guest_ldtr_selector, 976);
  519. CHECK_OFFSET(guest_tr_selector, 978);
  520. CHECK_OFFSET(guest_intr_status, 980);
  521. CHECK_OFFSET(host_es_selector, 982);
  522. CHECK_OFFSET(host_cs_selector, 984);
  523. CHECK_OFFSET(host_ss_selector, 986);
  524. CHECK_OFFSET(host_ds_selector, 988);
  525. CHECK_OFFSET(host_fs_selector, 990);
  526. CHECK_OFFSET(host_gs_selector, 992);
  527. CHECK_OFFSET(host_tr_selector, 994);
  528. CHECK_OFFSET(guest_pml_index, 996);
  529. }
  530. /*
  531. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  532. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  533. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  534. *
  535. * IMPORTANT: Changing this value will break save/restore compatibility with
  536. * older kvm releases.
  537. */
  538. #define VMCS12_REVISION 0x11e57ed0
  539. /*
  540. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  541. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  542. * current implementation, 4K are reserved to avoid future complications.
  543. */
  544. #define VMCS12_SIZE 0x1000
  545. /*
  546. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  547. * supported VMCS12 field encoding.
  548. */
  549. #define VMCS12_MAX_FIELD_INDEX 0x17
  550. struct nested_vmx_msrs {
  551. /*
  552. * We only store the "true" versions of the VMX capability MSRs. We
  553. * generate the "non-true" versions by setting the must-be-1 bits
  554. * according to the SDM.
  555. */
  556. u32 procbased_ctls_low;
  557. u32 procbased_ctls_high;
  558. u32 secondary_ctls_low;
  559. u32 secondary_ctls_high;
  560. u32 pinbased_ctls_low;
  561. u32 pinbased_ctls_high;
  562. u32 exit_ctls_low;
  563. u32 exit_ctls_high;
  564. u32 entry_ctls_low;
  565. u32 entry_ctls_high;
  566. u32 misc_low;
  567. u32 misc_high;
  568. u32 ept_caps;
  569. u32 vpid_caps;
  570. u64 basic;
  571. u64 cr0_fixed0;
  572. u64 cr0_fixed1;
  573. u64 cr4_fixed0;
  574. u64 cr4_fixed1;
  575. u64 vmcs_enum;
  576. u64 vmfunc_controls;
  577. };
  578. /*
  579. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  580. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  581. */
  582. struct nested_vmx {
  583. /* Has the level1 guest done vmxon? */
  584. bool vmxon;
  585. gpa_t vmxon_ptr;
  586. bool pml_full;
  587. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  588. gpa_t current_vmptr;
  589. /*
  590. * Cache of the guest's VMCS, existing outside of guest memory.
  591. * Loaded from guest memory during VMPTRLD. Flushed to guest
  592. * memory during VMCLEAR and VMPTRLD.
  593. */
  594. struct vmcs12 *cached_vmcs12;
  595. /*
  596. * Cache of the guest's shadow VMCS, existing outside of guest
  597. * memory. Loaded from guest memory during VM entry. Flushed
  598. * to guest memory during VM exit.
  599. */
  600. struct vmcs12 *cached_shadow_vmcs12;
  601. /*
  602. * Indicates if the shadow vmcs must be updated with the
  603. * data hold by vmcs12
  604. */
  605. bool sync_shadow_vmcs;
  606. bool dirty_vmcs12;
  607. bool change_vmcs01_virtual_apic_mode;
  608. /* L2 must run next, and mustn't decide to exit to L1. */
  609. bool nested_run_pending;
  610. struct loaded_vmcs vmcs02;
  611. /*
  612. * Guest pages referred to in the vmcs02 with host-physical
  613. * pointers, so we must keep them pinned while L2 runs.
  614. */
  615. struct page *apic_access_page;
  616. struct page *virtual_apic_page;
  617. struct page *pi_desc_page;
  618. struct pi_desc *pi_desc;
  619. bool pi_pending;
  620. u16 posted_intr_nv;
  621. struct hrtimer preemption_timer;
  622. bool preemption_timer_expired;
  623. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  624. u64 vmcs01_debugctl;
  625. u16 vpid02;
  626. u16 last_vpid;
  627. struct nested_vmx_msrs msrs;
  628. /* SMM related state */
  629. struct {
  630. /* in VMX operation on SMM entry? */
  631. bool vmxon;
  632. /* in guest mode on SMM entry? */
  633. bool guest_mode;
  634. } smm;
  635. };
  636. #define POSTED_INTR_ON 0
  637. #define POSTED_INTR_SN 1
  638. /* Posted-Interrupt Descriptor */
  639. struct pi_desc {
  640. u32 pir[8]; /* Posted interrupt requested */
  641. union {
  642. struct {
  643. /* bit 256 - Outstanding Notification */
  644. u16 on : 1,
  645. /* bit 257 - Suppress Notification */
  646. sn : 1,
  647. /* bit 271:258 - Reserved */
  648. rsvd_1 : 14;
  649. /* bit 279:272 - Notification Vector */
  650. u8 nv;
  651. /* bit 287:280 - Reserved */
  652. u8 rsvd_2;
  653. /* bit 319:288 - Notification Destination */
  654. u32 ndst;
  655. };
  656. u64 control;
  657. };
  658. u32 rsvd[6];
  659. } __aligned(64);
  660. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  661. {
  662. return test_and_set_bit(POSTED_INTR_ON,
  663. (unsigned long *)&pi_desc->control);
  664. }
  665. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  666. {
  667. return test_and_clear_bit(POSTED_INTR_ON,
  668. (unsigned long *)&pi_desc->control);
  669. }
  670. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  671. {
  672. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  673. }
  674. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  675. {
  676. return clear_bit(POSTED_INTR_SN,
  677. (unsigned long *)&pi_desc->control);
  678. }
  679. static inline void pi_set_sn(struct pi_desc *pi_desc)
  680. {
  681. return set_bit(POSTED_INTR_SN,
  682. (unsigned long *)&pi_desc->control);
  683. }
  684. static inline void pi_clear_on(struct pi_desc *pi_desc)
  685. {
  686. clear_bit(POSTED_INTR_ON,
  687. (unsigned long *)&pi_desc->control);
  688. }
  689. static inline int pi_test_on(struct pi_desc *pi_desc)
  690. {
  691. return test_bit(POSTED_INTR_ON,
  692. (unsigned long *)&pi_desc->control);
  693. }
  694. static inline int pi_test_sn(struct pi_desc *pi_desc)
  695. {
  696. return test_bit(POSTED_INTR_SN,
  697. (unsigned long *)&pi_desc->control);
  698. }
  699. struct vcpu_vmx {
  700. struct kvm_vcpu vcpu;
  701. unsigned long host_rsp;
  702. u8 fail;
  703. u8 msr_bitmap_mode;
  704. u32 exit_intr_info;
  705. u32 idt_vectoring_info;
  706. ulong rflags;
  707. struct shared_msr_entry *guest_msrs;
  708. int nmsrs;
  709. int save_nmsrs;
  710. unsigned long host_idt_base;
  711. #ifdef CONFIG_X86_64
  712. u64 msr_host_kernel_gs_base;
  713. u64 msr_guest_kernel_gs_base;
  714. #endif
  715. u64 arch_capabilities;
  716. u64 spec_ctrl;
  717. u32 vm_entry_controls_shadow;
  718. u32 vm_exit_controls_shadow;
  719. u32 secondary_exec_control;
  720. /*
  721. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  722. * non-nested (L1) guest, it always points to vmcs01. For a nested
  723. * guest (L2), it points to a different VMCS.
  724. */
  725. struct loaded_vmcs vmcs01;
  726. struct loaded_vmcs *loaded_vmcs;
  727. bool __launched; /* temporary, used in vmx_vcpu_run */
  728. struct msr_autoload {
  729. unsigned nr;
  730. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  731. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  732. } msr_autoload;
  733. struct {
  734. int loaded;
  735. u16 fs_sel, gs_sel, ldt_sel;
  736. #ifdef CONFIG_X86_64
  737. u16 ds_sel, es_sel;
  738. #endif
  739. int gs_ldt_reload_needed;
  740. int fs_reload_needed;
  741. } host_state;
  742. struct {
  743. int vm86_active;
  744. ulong save_rflags;
  745. struct kvm_segment segs[8];
  746. } rmode;
  747. struct {
  748. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  749. struct kvm_save_segment {
  750. u16 selector;
  751. unsigned long base;
  752. u32 limit;
  753. u32 ar;
  754. } seg[8];
  755. } segment_cache;
  756. int vpid;
  757. bool emulation_required;
  758. u32 exit_reason;
  759. /* Posted interrupt descriptor */
  760. struct pi_desc pi_desc;
  761. /* Support for a guest hypervisor (nested VMX) */
  762. struct nested_vmx nested;
  763. /* Dynamic PLE window. */
  764. int ple_window;
  765. bool ple_window_dirty;
  766. /* Support for PML */
  767. #define PML_ENTITY_NUM 512
  768. struct page *pml_pg;
  769. /* apic deadline value in host tsc */
  770. u64 hv_deadline_tsc;
  771. u64 current_tsc_ratio;
  772. u32 host_pkru;
  773. unsigned long host_debugctlmsr;
  774. /*
  775. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  776. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  777. * in msr_ia32_feature_control_valid_bits.
  778. */
  779. u64 msr_ia32_feature_control;
  780. u64 msr_ia32_feature_control_valid_bits;
  781. };
  782. enum segment_cache_field {
  783. SEG_FIELD_SEL = 0,
  784. SEG_FIELD_BASE = 1,
  785. SEG_FIELD_LIMIT = 2,
  786. SEG_FIELD_AR = 3,
  787. SEG_FIELD_NR = 4
  788. };
  789. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  790. {
  791. return container_of(kvm, struct kvm_vmx, kvm);
  792. }
  793. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  794. {
  795. return container_of(vcpu, struct vcpu_vmx, vcpu);
  796. }
  797. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  798. {
  799. return &(to_vmx(vcpu)->pi_desc);
  800. }
  801. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  802. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  803. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  804. #define FIELD64(number, name) \
  805. FIELD(number, name), \
  806. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  807. static u16 shadow_read_only_fields[] = {
  808. #define SHADOW_FIELD_RO(x) x,
  809. #include "vmx_shadow_fields.h"
  810. };
  811. static int max_shadow_read_only_fields =
  812. ARRAY_SIZE(shadow_read_only_fields);
  813. static u16 shadow_read_write_fields[] = {
  814. #define SHADOW_FIELD_RW(x) x,
  815. #include "vmx_shadow_fields.h"
  816. };
  817. static int max_shadow_read_write_fields =
  818. ARRAY_SIZE(shadow_read_write_fields);
  819. static const unsigned short vmcs_field_to_offset_table[] = {
  820. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  821. FIELD(POSTED_INTR_NV, posted_intr_nv),
  822. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  823. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  824. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  825. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  826. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  827. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  828. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  829. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  830. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  831. FIELD(GUEST_PML_INDEX, guest_pml_index),
  832. FIELD(HOST_ES_SELECTOR, host_es_selector),
  833. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  834. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  835. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  836. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  837. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  838. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  839. FIELD64(IO_BITMAP_A, io_bitmap_a),
  840. FIELD64(IO_BITMAP_B, io_bitmap_b),
  841. FIELD64(MSR_BITMAP, msr_bitmap),
  842. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  843. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  844. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  845. FIELD64(PML_ADDRESS, pml_address),
  846. FIELD64(TSC_OFFSET, tsc_offset),
  847. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  848. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  849. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  850. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  851. FIELD64(EPT_POINTER, ept_pointer),
  852. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  853. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  854. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  855. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  856. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  857. FIELD64(VMREAD_BITMAP, vmread_bitmap),
  858. FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
  859. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  860. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  861. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  862. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  863. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  864. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  865. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  866. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  867. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  868. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  869. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  870. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  871. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  872. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  873. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  874. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  875. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  876. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  877. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  878. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  879. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  880. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  881. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  882. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  883. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  884. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  885. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  886. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  887. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  888. FIELD(TPR_THRESHOLD, tpr_threshold),
  889. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  890. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  891. FIELD(VM_EXIT_REASON, vm_exit_reason),
  892. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  893. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  894. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  895. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  896. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  897. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  898. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  899. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  900. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  901. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  902. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  903. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  904. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  905. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  906. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  907. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  908. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  909. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  910. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  911. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  912. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  913. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  914. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  915. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  916. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  917. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  918. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  919. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  920. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  921. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  922. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  923. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  924. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  925. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  926. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  927. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  928. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  929. FIELD(EXIT_QUALIFICATION, exit_qualification),
  930. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  931. FIELD(GUEST_CR0, guest_cr0),
  932. FIELD(GUEST_CR3, guest_cr3),
  933. FIELD(GUEST_CR4, guest_cr4),
  934. FIELD(GUEST_ES_BASE, guest_es_base),
  935. FIELD(GUEST_CS_BASE, guest_cs_base),
  936. FIELD(GUEST_SS_BASE, guest_ss_base),
  937. FIELD(GUEST_DS_BASE, guest_ds_base),
  938. FIELD(GUEST_FS_BASE, guest_fs_base),
  939. FIELD(GUEST_GS_BASE, guest_gs_base),
  940. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  941. FIELD(GUEST_TR_BASE, guest_tr_base),
  942. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  943. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  944. FIELD(GUEST_DR7, guest_dr7),
  945. FIELD(GUEST_RSP, guest_rsp),
  946. FIELD(GUEST_RIP, guest_rip),
  947. FIELD(GUEST_RFLAGS, guest_rflags),
  948. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  949. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  950. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  951. FIELD(HOST_CR0, host_cr0),
  952. FIELD(HOST_CR3, host_cr3),
  953. FIELD(HOST_CR4, host_cr4),
  954. FIELD(HOST_FS_BASE, host_fs_base),
  955. FIELD(HOST_GS_BASE, host_gs_base),
  956. FIELD(HOST_TR_BASE, host_tr_base),
  957. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  958. FIELD(HOST_IDTR_BASE, host_idtr_base),
  959. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  960. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  961. FIELD(HOST_RSP, host_rsp),
  962. FIELD(HOST_RIP, host_rip),
  963. };
  964. static inline short vmcs_field_to_offset(unsigned long field)
  965. {
  966. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  967. unsigned short offset;
  968. unsigned index;
  969. if (field >> 15)
  970. return -ENOENT;
  971. index = ROL16(field, 6);
  972. if (index >= size)
  973. return -ENOENT;
  974. index = array_index_nospec(index, size);
  975. offset = vmcs_field_to_offset_table[index];
  976. if (offset == 0)
  977. return -ENOENT;
  978. return offset;
  979. }
  980. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  981. {
  982. return to_vmx(vcpu)->nested.cached_vmcs12;
  983. }
  984. static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
  985. {
  986. return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
  987. }
  988. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  989. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  990. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  991. static bool vmx_xsaves_supported(void);
  992. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  993. struct kvm_segment *var, int seg);
  994. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  995. struct kvm_segment *var, int seg);
  996. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  997. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  998. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  999. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  1000. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  1001. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  1002. u16 error_code);
  1003. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  1004. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  1005. u32 msr, int type);
  1006. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  1007. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  1008. /*
  1009. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  1010. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  1011. */
  1012. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  1013. /*
  1014. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  1015. * can find which vCPU should be waken up.
  1016. */
  1017. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  1018. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  1019. enum {
  1020. VMX_VMREAD_BITMAP,
  1021. VMX_VMWRITE_BITMAP,
  1022. VMX_BITMAP_NR
  1023. };
  1024. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  1025. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  1026. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  1027. static bool cpu_has_load_ia32_efer;
  1028. static bool cpu_has_load_perf_global_ctrl;
  1029. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1030. static DEFINE_SPINLOCK(vmx_vpid_lock);
  1031. static struct vmcs_config {
  1032. int size;
  1033. int order;
  1034. u32 basic_cap;
  1035. u32 revision_id;
  1036. u32 pin_based_exec_ctrl;
  1037. u32 cpu_based_exec_ctrl;
  1038. u32 cpu_based_2nd_exec_ctrl;
  1039. u32 vmexit_ctrl;
  1040. u32 vmentry_ctrl;
  1041. struct nested_vmx_msrs nested;
  1042. } vmcs_config;
  1043. static struct vmx_capability {
  1044. u32 ept;
  1045. u32 vpid;
  1046. } vmx_capability;
  1047. #define VMX_SEGMENT_FIELD(seg) \
  1048. [VCPU_SREG_##seg] = { \
  1049. .selector = GUEST_##seg##_SELECTOR, \
  1050. .base = GUEST_##seg##_BASE, \
  1051. .limit = GUEST_##seg##_LIMIT, \
  1052. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  1053. }
  1054. static const struct kvm_vmx_segment_field {
  1055. unsigned selector;
  1056. unsigned base;
  1057. unsigned limit;
  1058. unsigned ar_bytes;
  1059. } kvm_vmx_segment_fields[] = {
  1060. VMX_SEGMENT_FIELD(CS),
  1061. VMX_SEGMENT_FIELD(DS),
  1062. VMX_SEGMENT_FIELD(ES),
  1063. VMX_SEGMENT_FIELD(FS),
  1064. VMX_SEGMENT_FIELD(GS),
  1065. VMX_SEGMENT_FIELD(SS),
  1066. VMX_SEGMENT_FIELD(TR),
  1067. VMX_SEGMENT_FIELD(LDTR),
  1068. };
  1069. static u64 host_efer;
  1070. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  1071. /*
  1072. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  1073. * away by decrementing the array size.
  1074. */
  1075. static const u32 vmx_msr_index[] = {
  1076. #ifdef CONFIG_X86_64
  1077. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  1078. #endif
  1079. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  1080. };
  1081. DEFINE_STATIC_KEY_FALSE(enable_evmcs);
  1082. #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
  1083. #define KVM_EVMCS_VERSION 1
  1084. #if IS_ENABLED(CONFIG_HYPERV)
  1085. static bool __read_mostly enlightened_vmcs = true;
  1086. module_param(enlightened_vmcs, bool, 0444);
  1087. static inline void evmcs_write64(unsigned long field, u64 value)
  1088. {
  1089. u16 clean_field;
  1090. int offset = get_evmcs_offset(field, &clean_field);
  1091. if (offset < 0)
  1092. return;
  1093. *(u64 *)((char *)current_evmcs + offset) = value;
  1094. current_evmcs->hv_clean_fields &= ~clean_field;
  1095. }
  1096. static inline void evmcs_write32(unsigned long field, u32 value)
  1097. {
  1098. u16 clean_field;
  1099. int offset = get_evmcs_offset(field, &clean_field);
  1100. if (offset < 0)
  1101. return;
  1102. *(u32 *)((char *)current_evmcs + offset) = value;
  1103. current_evmcs->hv_clean_fields &= ~clean_field;
  1104. }
  1105. static inline void evmcs_write16(unsigned long field, u16 value)
  1106. {
  1107. u16 clean_field;
  1108. int offset = get_evmcs_offset(field, &clean_field);
  1109. if (offset < 0)
  1110. return;
  1111. *(u16 *)((char *)current_evmcs + offset) = value;
  1112. current_evmcs->hv_clean_fields &= ~clean_field;
  1113. }
  1114. static inline u64 evmcs_read64(unsigned long field)
  1115. {
  1116. int offset = get_evmcs_offset(field, NULL);
  1117. if (offset < 0)
  1118. return 0;
  1119. return *(u64 *)((char *)current_evmcs + offset);
  1120. }
  1121. static inline u32 evmcs_read32(unsigned long field)
  1122. {
  1123. int offset = get_evmcs_offset(field, NULL);
  1124. if (offset < 0)
  1125. return 0;
  1126. return *(u32 *)((char *)current_evmcs + offset);
  1127. }
  1128. static inline u16 evmcs_read16(unsigned long field)
  1129. {
  1130. int offset = get_evmcs_offset(field, NULL);
  1131. if (offset < 0)
  1132. return 0;
  1133. return *(u16 *)((char *)current_evmcs + offset);
  1134. }
  1135. static inline void evmcs_touch_msr_bitmap(void)
  1136. {
  1137. if (unlikely(!current_evmcs))
  1138. return;
  1139. if (current_evmcs->hv_enlightenments_control.msr_bitmap)
  1140. current_evmcs->hv_clean_fields &=
  1141. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
  1142. }
  1143. static void evmcs_load(u64 phys_addr)
  1144. {
  1145. struct hv_vp_assist_page *vp_ap =
  1146. hv_get_vp_assist_page(smp_processor_id());
  1147. vp_ap->current_nested_vmcs = phys_addr;
  1148. vp_ap->enlighten_vmentry = 1;
  1149. }
  1150. static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
  1151. {
  1152. /*
  1153. * Enlightened VMCSv1 doesn't support these:
  1154. *
  1155. * POSTED_INTR_NV = 0x00000002,
  1156. * GUEST_INTR_STATUS = 0x00000810,
  1157. * APIC_ACCESS_ADDR = 0x00002014,
  1158. * POSTED_INTR_DESC_ADDR = 0x00002016,
  1159. * EOI_EXIT_BITMAP0 = 0x0000201c,
  1160. * EOI_EXIT_BITMAP1 = 0x0000201e,
  1161. * EOI_EXIT_BITMAP2 = 0x00002020,
  1162. * EOI_EXIT_BITMAP3 = 0x00002022,
  1163. */
  1164. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  1165. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1166. ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1167. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1168. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1169. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1170. ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1171. /*
  1172. * GUEST_PML_INDEX = 0x00000812,
  1173. * PML_ADDRESS = 0x0000200e,
  1174. */
  1175. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
  1176. /* VM_FUNCTION_CONTROL = 0x00002018, */
  1177. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
  1178. /*
  1179. * EPTP_LIST_ADDRESS = 0x00002024,
  1180. * VMREAD_BITMAP = 0x00002026,
  1181. * VMWRITE_BITMAP = 0x00002028,
  1182. */
  1183. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
  1184. /*
  1185. * TSC_MULTIPLIER = 0x00002032,
  1186. */
  1187. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
  1188. /*
  1189. * PLE_GAP = 0x00004020,
  1190. * PLE_WINDOW = 0x00004022,
  1191. */
  1192. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1193. /*
  1194. * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
  1195. */
  1196. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  1197. /*
  1198. * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
  1199. * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
  1200. */
  1201. vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
  1202. vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
  1203. /*
  1204. * Currently unsupported in KVM:
  1205. * GUEST_IA32_RTIT_CTL = 0x00002814,
  1206. */
  1207. }
  1208. #else /* !IS_ENABLED(CONFIG_HYPERV) */
  1209. static inline void evmcs_write64(unsigned long field, u64 value) {}
  1210. static inline void evmcs_write32(unsigned long field, u32 value) {}
  1211. static inline void evmcs_write16(unsigned long field, u16 value) {}
  1212. static inline u64 evmcs_read64(unsigned long field) { return 0; }
  1213. static inline u32 evmcs_read32(unsigned long field) { return 0; }
  1214. static inline u16 evmcs_read16(unsigned long field) { return 0; }
  1215. static inline void evmcs_load(u64 phys_addr) {}
  1216. static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
  1217. static inline void evmcs_touch_msr_bitmap(void) {}
  1218. #endif /* IS_ENABLED(CONFIG_HYPERV) */
  1219. static inline bool is_exception_n(u32 intr_info, u8 vector)
  1220. {
  1221. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1222. INTR_INFO_VALID_MASK)) ==
  1223. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  1224. }
  1225. static inline bool is_debug(u32 intr_info)
  1226. {
  1227. return is_exception_n(intr_info, DB_VECTOR);
  1228. }
  1229. static inline bool is_breakpoint(u32 intr_info)
  1230. {
  1231. return is_exception_n(intr_info, BP_VECTOR);
  1232. }
  1233. static inline bool is_page_fault(u32 intr_info)
  1234. {
  1235. return is_exception_n(intr_info, PF_VECTOR);
  1236. }
  1237. static inline bool is_no_device(u32 intr_info)
  1238. {
  1239. return is_exception_n(intr_info, NM_VECTOR);
  1240. }
  1241. static inline bool is_invalid_opcode(u32 intr_info)
  1242. {
  1243. return is_exception_n(intr_info, UD_VECTOR);
  1244. }
  1245. static inline bool is_gp_fault(u32 intr_info)
  1246. {
  1247. return is_exception_n(intr_info, GP_VECTOR);
  1248. }
  1249. static inline bool is_external_interrupt(u32 intr_info)
  1250. {
  1251. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1252. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  1253. }
  1254. static inline bool is_machine_check(u32 intr_info)
  1255. {
  1256. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1257. INTR_INFO_VALID_MASK)) ==
  1258. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  1259. }
  1260. /* Undocumented: icebp/int1 */
  1261. static inline bool is_icebp(u32 intr_info)
  1262. {
  1263. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1264. == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
  1265. }
  1266. static inline bool cpu_has_vmx_msr_bitmap(void)
  1267. {
  1268. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  1269. }
  1270. static inline bool cpu_has_vmx_tpr_shadow(void)
  1271. {
  1272. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  1273. }
  1274. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  1275. {
  1276. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  1277. }
  1278. static inline bool cpu_has_secondary_exec_ctrls(void)
  1279. {
  1280. return vmcs_config.cpu_based_exec_ctrl &
  1281. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1282. }
  1283. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  1284. {
  1285. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1286. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1287. }
  1288. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  1289. {
  1290. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1291. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  1292. }
  1293. static inline bool cpu_has_vmx_apic_register_virt(void)
  1294. {
  1295. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1296. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1297. }
  1298. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  1299. {
  1300. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1301. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1302. }
  1303. /*
  1304. * Comment's format: document - errata name - stepping - processor name.
  1305. * Refer from
  1306. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  1307. */
  1308. static u32 vmx_preemption_cpu_tfms[] = {
  1309. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  1310. 0x000206E6,
  1311. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  1312. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  1313. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  1314. 0x00020652,
  1315. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  1316. 0x00020655,
  1317. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  1318. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  1319. /*
  1320. * 320767.pdf - AAP86 - B1 -
  1321. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  1322. */
  1323. 0x000106E5,
  1324. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  1325. 0x000106A0,
  1326. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  1327. 0x000106A1,
  1328. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  1329. 0x000106A4,
  1330. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  1331. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  1332. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  1333. 0x000106A5,
  1334. };
  1335. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1336. {
  1337. u32 eax = cpuid_eax(0x00000001), i;
  1338. /* Clear the reserved bits */
  1339. eax &= ~(0x3U << 14 | 0xfU << 28);
  1340. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1341. if (eax == vmx_preemption_cpu_tfms[i])
  1342. return true;
  1343. return false;
  1344. }
  1345. static inline bool cpu_has_vmx_preemption_timer(void)
  1346. {
  1347. return vmcs_config.pin_based_exec_ctrl &
  1348. PIN_BASED_VMX_PREEMPTION_TIMER;
  1349. }
  1350. static inline bool cpu_has_vmx_posted_intr(void)
  1351. {
  1352. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1353. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1354. }
  1355. static inline bool cpu_has_vmx_apicv(void)
  1356. {
  1357. return cpu_has_vmx_apic_register_virt() &&
  1358. cpu_has_vmx_virtual_intr_delivery() &&
  1359. cpu_has_vmx_posted_intr();
  1360. }
  1361. static inline bool cpu_has_vmx_flexpriority(void)
  1362. {
  1363. return cpu_has_vmx_tpr_shadow() &&
  1364. cpu_has_vmx_virtualize_apic_accesses();
  1365. }
  1366. static inline bool cpu_has_vmx_ept_execute_only(void)
  1367. {
  1368. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1369. }
  1370. static inline bool cpu_has_vmx_ept_2m_page(void)
  1371. {
  1372. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1373. }
  1374. static inline bool cpu_has_vmx_ept_1g_page(void)
  1375. {
  1376. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1377. }
  1378. static inline bool cpu_has_vmx_ept_4levels(void)
  1379. {
  1380. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1381. }
  1382. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1383. {
  1384. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1385. }
  1386. static inline bool cpu_has_vmx_ept_5levels(void)
  1387. {
  1388. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1389. }
  1390. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1391. {
  1392. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1393. }
  1394. static inline bool cpu_has_vmx_invept_context(void)
  1395. {
  1396. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1397. }
  1398. static inline bool cpu_has_vmx_invept_global(void)
  1399. {
  1400. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1401. }
  1402. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  1403. {
  1404. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  1405. }
  1406. static inline bool cpu_has_vmx_invvpid_single(void)
  1407. {
  1408. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1409. }
  1410. static inline bool cpu_has_vmx_invvpid_global(void)
  1411. {
  1412. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1413. }
  1414. static inline bool cpu_has_vmx_invvpid(void)
  1415. {
  1416. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1417. }
  1418. static inline bool cpu_has_vmx_ept(void)
  1419. {
  1420. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1421. SECONDARY_EXEC_ENABLE_EPT;
  1422. }
  1423. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1424. {
  1425. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1426. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1427. }
  1428. static inline bool cpu_has_vmx_ple(void)
  1429. {
  1430. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1431. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1432. }
  1433. static inline bool cpu_has_vmx_basic_inout(void)
  1434. {
  1435. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1436. }
  1437. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1438. {
  1439. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1440. }
  1441. static inline bool cpu_has_vmx_vpid(void)
  1442. {
  1443. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1444. SECONDARY_EXEC_ENABLE_VPID;
  1445. }
  1446. static inline bool cpu_has_vmx_rdtscp(void)
  1447. {
  1448. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1449. SECONDARY_EXEC_RDTSCP;
  1450. }
  1451. static inline bool cpu_has_vmx_invpcid(void)
  1452. {
  1453. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1454. SECONDARY_EXEC_ENABLE_INVPCID;
  1455. }
  1456. static inline bool cpu_has_virtual_nmis(void)
  1457. {
  1458. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1459. }
  1460. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1461. {
  1462. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1463. SECONDARY_EXEC_WBINVD_EXITING;
  1464. }
  1465. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1466. {
  1467. u64 vmx_msr;
  1468. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1469. /* check if the cpu supports writing r/o exit information fields */
  1470. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1471. return false;
  1472. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1473. SECONDARY_EXEC_SHADOW_VMCS;
  1474. }
  1475. static inline bool cpu_has_vmx_pml(void)
  1476. {
  1477. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1478. }
  1479. static inline bool cpu_has_vmx_tsc_scaling(void)
  1480. {
  1481. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1482. SECONDARY_EXEC_TSC_SCALING;
  1483. }
  1484. static inline bool cpu_has_vmx_vmfunc(void)
  1485. {
  1486. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1487. SECONDARY_EXEC_ENABLE_VMFUNC;
  1488. }
  1489. static bool vmx_umip_emulated(void)
  1490. {
  1491. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1492. SECONDARY_EXEC_DESC;
  1493. }
  1494. static inline bool report_flexpriority(void)
  1495. {
  1496. return flexpriority_enabled;
  1497. }
  1498. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1499. {
  1500. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  1501. }
  1502. /*
  1503. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  1504. * to modify any valid field of the VMCS, or are the VM-exit
  1505. * information fields read-only?
  1506. */
  1507. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  1508. {
  1509. return to_vmx(vcpu)->nested.msrs.misc_low &
  1510. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  1511. }
  1512. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  1513. {
  1514. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  1515. }
  1516. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  1517. {
  1518. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  1519. CPU_BASED_MONITOR_TRAP_FLAG;
  1520. }
  1521. static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
  1522. {
  1523. return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  1524. SECONDARY_EXEC_SHADOW_VMCS;
  1525. }
  1526. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1527. {
  1528. return vmcs12->cpu_based_vm_exec_control & bit;
  1529. }
  1530. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1531. {
  1532. return (vmcs12->cpu_based_vm_exec_control &
  1533. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1534. (vmcs12->secondary_vm_exec_control & bit);
  1535. }
  1536. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1537. {
  1538. return vmcs12->pin_based_vm_exec_control &
  1539. PIN_BASED_VMX_PREEMPTION_TIMER;
  1540. }
  1541. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  1542. {
  1543. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  1544. }
  1545. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1546. {
  1547. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1548. }
  1549. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1550. {
  1551. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1552. }
  1553. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1554. {
  1555. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1556. }
  1557. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1558. {
  1559. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1560. }
  1561. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1562. {
  1563. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1564. }
  1565. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1566. {
  1567. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1568. }
  1569. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1570. {
  1571. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1572. }
  1573. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1574. {
  1575. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1576. }
  1577. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1578. {
  1579. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1580. }
  1581. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1582. {
  1583. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1584. }
  1585. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1586. {
  1587. return nested_cpu_has_vmfunc(vmcs12) &&
  1588. (vmcs12->vm_function_control &
  1589. VMX_VMFUNC_EPTP_SWITCHING);
  1590. }
  1591. static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
  1592. {
  1593. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
  1594. }
  1595. static inline bool is_nmi(u32 intr_info)
  1596. {
  1597. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1598. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1599. }
  1600. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1601. u32 exit_intr_info,
  1602. unsigned long exit_qualification);
  1603. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1604. struct vmcs12 *vmcs12,
  1605. u32 reason, unsigned long qualification);
  1606. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1607. {
  1608. int i;
  1609. for (i = 0; i < vmx->nmsrs; ++i)
  1610. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1611. return i;
  1612. return -1;
  1613. }
  1614. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  1615. {
  1616. struct {
  1617. u64 vpid : 16;
  1618. u64 rsvd : 48;
  1619. u64 gva;
  1620. } operand = { vpid, 0, gva };
  1621. asm volatile (__ex(ASM_VMX_INVVPID)
  1622. /* CF==1 or ZF==1 --> rc = -1 */
  1623. "; ja 1f ; ud2 ; 1:"
  1624. : : "a"(&operand), "c"(ext) : "cc", "memory");
  1625. }
  1626. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1627. {
  1628. struct {
  1629. u64 eptp, gpa;
  1630. } operand = {eptp, gpa};
  1631. asm volatile (__ex(ASM_VMX_INVEPT)
  1632. /* CF==1 or ZF==1 --> rc = -1 */
  1633. "; ja 1f ; ud2 ; 1:\n"
  1634. : : "a" (&operand), "c" (ext) : "cc", "memory");
  1635. }
  1636. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1637. {
  1638. int i;
  1639. i = __find_msr_index(vmx, msr);
  1640. if (i >= 0)
  1641. return &vmx->guest_msrs[i];
  1642. return NULL;
  1643. }
  1644. static void vmcs_clear(struct vmcs *vmcs)
  1645. {
  1646. u64 phys_addr = __pa(vmcs);
  1647. u8 error;
  1648. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  1649. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1650. : "cc", "memory");
  1651. if (error)
  1652. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1653. vmcs, phys_addr);
  1654. }
  1655. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1656. {
  1657. vmcs_clear(loaded_vmcs->vmcs);
  1658. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1659. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1660. loaded_vmcs->cpu = -1;
  1661. loaded_vmcs->launched = 0;
  1662. }
  1663. static void vmcs_load(struct vmcs *vmcs)
  1664. {
  1665. u64 phys_addr = __pa(vmcs);
  1666. u8 error;
  1667. if (static_branch_unlikely(&enable_evmcs))
  1668. return evmcs_load(phys_addr);
  1669. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  1670. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1671. : "cc", "memory");
  1672. if (error)
  1673. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1674. vmcs, phys_addr);
  1675. }
  1676. #ifdef CONFIG_KEXEC_CORE
  1677. /*
  1678. * This bitmap is used to indicate whether the vmclear
  1679. * operation is enabled on all cpus. All disabled by
  1680. * default.
  1681. */
  1682. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1683. static inline void crash_enable_local_vmclear(int cpu)
  1684. {
  1685. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1686. }
  1687. static inline void crash_disable_local_vmclear(int cpu)
  1688. {
  1689. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1690. }
  1691. static inline int crash_local_vmclear_enabled(int cpu)
  1692. {
  1693. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1694. }
  1695. static void crash_vmclear_local_loaded_vmcss(void)
  1696. {
  1697. int cpu = raw_smp_processor_id();
  1698. struct loaded_vmcs *v;
  1699. if (!crash_local_vmclear_enabled(cpu))
  1700. return;
  1701. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1702. loaded_vmcss_on_cpu_link)
  1703. vmcs_clear(v->vmcs);
  1704. }
  1705. #else
  1706. static inline void crash_enable_local_vmclear(int cpu) { }
  1707. static inline void crash_disable_local_vmclear(int cpu) { }
  1708. #endif /* CONFIG_KEXEC_CORE */
  1709. static void __loaded_vmcs_clear(void *arg)
  1710. {
  1711. struct loaded_vmcs *loaded_vmcs = arg;
  1712. int cpu = raw_smp_processor_id();
  1713. if (loaded_vmcs->cpu != cpu)
  1714. return; /* vcpu migration can race with cpu offline */
  1715. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1716. per_cpu(current_vmcs, cpu) = NULL;
  1717. crash_disable_local_vmclear(cpu);
  1718. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1719. /*
  1720. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1721. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1722. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1723. * then adds the vmcs into percpu list before it is deleted.
  1724. */
  1725. smp_wmb();
  1726. loaded_vmcs_init(loaded_vmcs);
  1727. crash_enable_local_vmclear(cpu);
  1728. }
  1729. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1730. {
  1731. int cpu = loaded_vmcs->cpu;
  1732. if (cpu != -1)
  1733. smp_call_function_single(cpu,
  1734. __loaded_vmcs_clear, loaded_vmcs, 1);
  1735. }
  1736. static inline void vpid_sync_vcpu_single(int vpid)
  1737. {
  1738. if (vpid == 0)
  1739. return;
  1740. if (cpu_has_vmx_invvpid_single())
  1741. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1742. }
  1743. static inline void vpid_sync_vcpu_global(void)
  1744. {
  1745. if (cpu_has_vmx_invvpid_global())
  1746. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1747. }
  1748. static inline void vpid_sync_context(int vpid)
  1749. {
  1750. if (cpu_has_vmx_invvpid_single())
  1751. vpid_sync_vcpu_single(vpid);
  1752. else
  1753. vpid_sync_vcpu_global();
  1754. }
  1755. static inline void ept_sync_global(void)
  1756. {
  1757. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1758. }
  1759. static inline void ept_sync_context(u64 eptp)
  1760. {
  1761. if (cpu_has_vmx_invept_context())
  1762. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1763. else
  1764. ept_sync_global();
  1765. }
  1766. static __always_inline void vmcs_check16(unsigned long field)
  1767. {
  1768. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1769. "16-bit accessor invalid for 64-bit field");
  1770. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1771. "16-bit accessor invalid for 64-bit high field");
  1772. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1773. "16-bit accessor invalid for 32-bit high field");
  1774. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1775. "16-bit accessor invalid for natural width field");
  1776. }
  1777. static __always_inline void vmcs_check32(unsigned long field)
  1778. {
  1779. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1780. "32-bit accessor invalid for 16-bit field");
  1781. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1782. "32-bit accessor invalid for natural width field");
  1783. }
  1784. static __always_inline void vmcs_check64(unsigned long field)
  1785. {
  1786. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1787. "64-bit accessor invalid for 16-bit field");
  1788. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1789. "64-bit accessor invalid for 64-bit high field");
  1790. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1791. "64-bit accessor invalid for 32-bit field");
  1792. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1793. "64-bit accessor invalid for natural width field");
  1794. }
  1795. static __always_inline void vmcs_checkl(unsigned long field)
  1796. {
  1797. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1798. "Natural width accessor invalid for 16-bit field");
  1799. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1800. "Natural width accessor invalid for 64-bit field");
  1801. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1802. "Natural width accessor invalid for 64-bit high field");
  1803. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1804. "Natural width accessor invalid for 32-bit field");
  1805. }
  1806. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  1807. {
  1808. unsigned long value;
  1809. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1810. : "=a"(value) : "d"(field) : "cc");
  1811. return value;
  1812. }
  1813. static __always_inline u16 vmcs_read16(unsigned long field)
  1814. {
  1815. vmcs_check16(field);
  1816. if (static_branch_unlikely(&enable_evmcs))
  1817. return evmcs_read16(field);
  1818. return __vmcs_readl(field);
  1819. }
  1820. static __always_inline u32 vmcs_read32(unsigned long field)
  1821. {
  1822. vmcs_check32(field);
  1823. if (static_branch_unlikely(&enable_evmcs))
  1824. return evmcs_read32(field);
  1825. return __vmcs_readl(field);
  1826. }
  1827. static __always_inline u64 vmcs_read64(unsigned long field)
  1828. {
  1829. vmcs_check64(field);
  1830. if (static_branch_unlikely(&enable_evmcs))
  1831. return evmcs_read64(field);
  1832. #ifdef CONFIG_X86_64
  1833. return __vmcs_readl(field);
  1834. #else
  1835. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  1836. #endif
  1837. }
  1838. static __always_inline unsigned long vmcs_readl(unsigned long field)
  1839. {
  1840. vmcs_checkl(field);
  1841. if (static_branch_unlikely(&enable_evmcs))
  1842. return evmcs_read64(field);
  1843. return __vmcs_readl(field);
  1844. }
  1845. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  1846. {
  1847. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  1848. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  1849. dump_stack();
  1850. }
  1851. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  1852. {
  1853. u8 error;
  1854. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  1855. : "=q"(error) : "a"(value), "d"(field) : "cc");
  1856. if (unlikely(error))
  1857. vmwrite_error(field, value);
  1858. }
  1859. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  1860. {
  1861. vmcs_check16(field);
  1862. if (static_branch_unlikely(&enable_evmcs))
  1863. return evmcs_write16(field, value);
  1864. __vmcs_writel(field, value);
  1865. }
  1866. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  1867. {
  1868. vmcs_check32(field);
  1869. if (static_branch_unlikely(&enable_evmcs))
  1870. return evmcs_write32(field, value);
  1871. __vmcs_writel(field, value);
  1872. }
  1873. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  1874. {
  1875. vmcs_check64(field);
  1876. if (static_branch_unlikely(&enable_evmcs))
  1877. return evmcs_write64(field, value);
  1878. __vmcs_writel(field, value);
  1879. #ifndef CONFIG_X86_64
  1880. asm volatile ("");
  1881. __vmcs_writel(field+1, value >> 32);
  1882. #endif
  1883. }
  1884. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  1885. {
  1886. vmcs_checkl(field);
  1887. if (static_branch_unlikely(&enable_evmcs))
  1888. return evmcs_write64(field, value);
  1889. __vmcs_writel(field, value);
  1890. }
  1891. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  1892. {
  1893. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1894. "vmcs_clear_bits does not support 64-bit fields");
  1895. if (static_branch_unlikely(&enable_evmcs))
  1896. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  1897. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  1898. }
  1899. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  1900. {
  1901. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1902. "vmcs_set_bits does not support 64-bit fields");
  1903. if (static_branch_unlikely(&enable_evmcs))
  1904. return evmcs_write32(field, evmcs_read32(field) | mask);
  1905. __vmcs_writel(field, __vmcs_readl(field) | mask);
  1906. }
  1907. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  1908. {
  1909. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  1910. }
  1911. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  1912. {
  1913. vmcs_write32(VM_ENTRY_CONTROLS, val);
  1914. vmx->vm_entry_controls_shadow = val;
  1915. }
  1916. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  1917. {
  1918. if (vmx->vm_entry_controls_shadow != val)
  1919. vm_entry_controls_init(vmx, val);
  1920. }
  1921. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  1922. {
  1923. return vmx->vm_entry_controls_shadow;
  1924. }
  1925. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1926. {
  1927. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  1928. }
  1929. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1930. {
  1931. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  1932. }
  1933. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  1934. {
  1935. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  1936. }
  1937. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  1938. {
  1939. vmcs_write32(VM_EXIT_CONTROLS, val);
  1940. vmx->vm_exit_controls_shadow = val;
  1941. }
  1942. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  1943. {
  1944. if (vmx->vm_exit_controls_shadow != val)
  1945. vm_exit_controls_init(vmx, val);
  1946. }
  1947. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  1948. {
  1949. return vmx->vm_exit_controls_shadow;
  1950. }
  1951. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1952. {
  1953. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  1954. }
  1955. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1956. {
  1957. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  1958. }
  1959. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1960. {
  1961. vmx->segment_cache.bitmask = 0;
  1962. }
  1963. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1964. unsigned field)
  1965. {
  1966. bool ret;
  1967. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1968. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1969. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1970. vmx->segment_cache.bitmask = 0;
  1971. }
  1972. ret = vmx->segment_cache.bitmask & mask;
  1973. vmx->segment_cache.bitmask |= mask;
  1974. return ret;
  1975. }
  1976. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1977. {
  1978. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1979. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1980. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1981. return *p;
  1982. }
  1983. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1984. {
  1985. ulong *p = &vmx->segment_cache.seg[seg].base;
  1986. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1987. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1988. return *p;
  1989. }
  1990. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1991. {
  1992. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1993. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1994. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1995. return *p;
  1996. }
  1997. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1998. {
  1999. u32 *p = &vmx->segment_cache.seg[seg].ar;
  2000. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  2001. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  2002. return *p;
  2003. }
  2004. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  2005. {
  2006. u32 eb;
  2007. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  2008. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  2009. /*
  2010. * Guest access to VMware backdoor ports could legitimately
  2011. * trigger #GP because of TSS I/O permission bitmap.
  2012. * We intercept those #GP and allow access to them anyway
  2013. * as VMware does.
  2014. */
  2015. if (enable_vmware_backdoor)
  2016. eb |= (1u << GP_VECTOR);
  2017. if ((vcpu->guest_debug &
  2018. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  2019. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  2020. eb |= 1u << BP_VECTOR;
  2021. if (to_vmx(vcpu)->rmode.vm86_active)
  2022. eb = ~0;
  2023. if (enable_ept)
  2024. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  2025. /* When we are running a nested L2 guest and L1 specified for it a
  2026. * certain exception bitmap, we must trap the same exceptions and pass
  2027. * them to L1. When running L2, we will only handle the exceptions
  2028. * specified above if L1 did not want them.
  2029. */
  2030. if (is_guest_mode(vcpu))
  2031. eb |= get_vmcs12(vcpu)->exception_bitmap;
  2032. vmcs_write32(EXCEPTION_BITMAP, eb);
  2033. }
  2034. /*
  2035. * Check if MSR is intercepted for currently loaded MSR bitmap.
  2036. */
  2037. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  2038. {
  2039. unsigned long *msr_bitmap;
  2040. int f = sizeof(unsigned long);
  2041. if (!cpu_has_vmx_msr_bitmap())
  2042. return true;
  2043. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  2044. if (msr <= 0x1fff) {
  2045. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2046. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2047. msr &= 0x1fff;
  2048. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2049. }
  2050. return true;
  2051. }
  2052. /*
  2053. * Check if MSR is intercepted for L01 MSR bitmap.
  2054. */
  2055. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  2056. {
  2057. unsigned long *msr_bitmap;
  2058. int f = sizeof(unsigned long);
  2059. if (!cpu_has_vmx_msr_bitmap())
  2060. return true;
  2061. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  2062. if (msr <= 0x1fff) {
  2063. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2064. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2065. msr &= 0x1fff;
  2066. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2067. }
  2068. return true;
  2069. }
  2070. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2071. unsigned long entry, unsigned long exit)
  2072. {
  2073. vm_entry_controls_clearbit(vmx, entry);
  2074. vm_exit_controls_clearbit(vmx, exit);
  2075. }
  2076. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  2077. {
  2078. unsigned i;
  2079. struct msr_autoload *m = &vmx->msr_autoload;
  2080. switch (msr) {
  2081. case MSR_EFER:
  2082. if (cpu_has_load_ia32_efer) {
  2083. clear_atomic_switch_msr_special(vmx,
  2084. VM_ENTRY_LOAD_IA32_EFER,
  2085. VM_EXIT_LOAD_IA32_EFER);
  2086. return;
  2087. }
  2088. break;
  2089. case MSR_CORE_PERF_GLOBAL_CTRL:
  2090. if (cpu_has_load_perf_global_ctrl) {
  2091. clear_atomic_switch_msr_special(vmx,
  2092. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2093. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2094. return;
  2095. }
  2096. break;
  2097. }
  2098. for (i = 0; i < m->nr; ++i)
  2099. if (m->guest[i].index == msr)
  2100. break;
  2101. if (i == m->nr)
  2102. return;
  2103. --m->nr;
  2104. m->guest[i] = m->guest[m->nr];
  2105. m->host[i] = m->host[m->nr];
  2106. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  2107. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  2108. }
  2109. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2110. unsigned long entry, unsigned long exit,
  2111. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  2112. u64 guest_val, u64 host_val)
  2113. {
  2114. vmcs_write64(guest_val_vmcs, guest_val);
  2115. vmcs_write64(host_val_vmcs, host_val);
  2116. vm_entry_controls_setbit(vmx, entry);
  2117. vm_exit_controls_setbit(vmx, exit);
  2118. }
  2119. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  2120. u64 guest_val, u64 host_val)
  2121. {
  2122. unsigned i;
  2123. struct msr_autoload *m = &vmx->msr_autoload;
  2124. switch (msr) {
  2125. case MSR_EFER:
  2126. if (cpu_has_load_ia32_efer) {
  2127. add_atomic_switch_msr_special(vmx,
  2128. VM_ENTRY_LOAD_IA32_EFER,
  2129. VM_EXIT_LOAD_IA32_EFER,
  2130. GUEST_IA32_EFER,
  2131. HOST_IA32_EFER,
  2132. guest_val, host_val);
  2133. return;
  2134. }
  2135. break;
  2136. case MSR_CORE_PERF_GLOBAL_CTRL:
  2137. if (cpu_has_load_perf_global_ctrl) {
  2138. add_atomic_switch_msr_special(vmx,
  2139. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2140. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  2141. GUEST_IA32_PERF_GLOBAL_CTRL,
  2142. HOST_IA32_PERF_GLOBAL_CTRL,
  2143. guest_val, host_val);
  2144. return;
  2145. }
  2146. break;
  2147. case MSR_IA32_PEBS_ENABLE:
  2148. /* PEBS needs a quiescent period after being disabled (to write
  2149. * a record). Disabling PEBS through VMX MSR swapping doesn't
  2150. * provide that period, so a CPU could write host's record into
  2151. * guest's memory.
  2152. */
  2153. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  2154. }
  2155. for (i = 0; i < m->nr; ++i)
  2156. if (m->guest[i].index == msr)
  2157. break;
  2158. if (i == NR_AUTOLOAD_MSRS) {
  2159. printk_once(KERN_WARNING "Not enough msr switch entries. "
  2160. "Can't add msr %x\n", msr);
  2161. return;
  2162. } else if (i == m->nr) {
  2163. ++m->nr;
  2164. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  2165. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  2166. }
  2167. m->guest[i].index = msr;
  2168. m->guest[i].value = guest_val;
  2169. m->host[i].index = msr;
  2170. m->host[i].value = host_val;
  2171. }
  2172. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  2173. {
  2174. u64 guest_efer = vmx->vcpu.arch.efer;
  2175. u64 ignore_bits = 0;
  2176. if (!enable_ept) {
  2177. /*
  2178. * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
  2179. * host CPUID is more efficient than testing guest CPUID
  2180. * or CR4. Host SMEP is anyway a requirement for guest SMEP.
  2181. */
  2182. if (boot_cpu_has(X86_FEATURE_SMEP))
  2183. guest_efer |= EFER_NX;
  2184. else if (!(guest_efer & EFER_NX))
  2185. ignore_bits |= EFER_NX;
  2186. }
  2187. /*
  2188. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  2189. */
  2190. ignore_bits |= EFER_SCE;
  2191. #ifdef CONFIG_X86_64
  2192. ignore_bits |= EFER_LMA | EFER_LME;
  2193. /* SCE is meaningful only in long mode on Intel */
  2194. if (guest_efer & EFER_LMA)
  2195. ignore_bits &= ~(u64)EFER_SCE;
  2196. #endif
  2197. clear_atomic_switch_msr(vmx, MSR_EFER);
  2198. /*
  2199. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  2200. * On CPUs that support "load IA32_EFER", always switch EFER
  2201. * atomically, since it's faster than switching it manually.
  2202. */
  2203. if (cpu_has_load_ia32_efer ||
  2204. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  2205. if (!(guest_efer & EFER_LMA))
  2206. guest_efer &= ~EFER_LME;
  2207. if (guest_efer != host_efer)
  2208. add_atomic_switch_msr(vmx, MSR_EFER,
  2209. guest_efer, host_efer);
  2210. return false;
  2211. } else {
  2212. guest_efer &= ~ignore_bits;
  2213. guest_efer |= host_efer & ignore_bits;
  2214. vmx->guest_msrs[efer_offset].data = guest_efer;
  2215. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  2216. return true;
  2217. }
  2218. }
  2219. #ifdef CONFIG_X86_32
  2220. /*
  2221. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  2222. * VMCS rather than the segment table. KVM uses this helper to figure
  2223. * out the current bases to poke them into the VMCS before entry.
  2224. */
  2225. static unsigned long segment_base(u16 selector)
  2226. {
  2227. struct desc_struct *table;
  2228. unsigned long v;
  2229. if (!(selector & ~SEGMENT_RPL_MASK))
  2230. return 0;
  2231. table = get_current_gdt_ro();
  2232. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  2233. u16 ldt_selector = kvm_read_ldt();
  2234. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  2235. return 0;
  2236. table = (struct desc_struct *)segment_base(ldt_selector);
  2237. }
  2238. v = get_desc_base(&table[selector >> 3]);
  2239. return v;
  2240. }
  2241. #endif
  2242. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  2243. {
  2244. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2245. #ifdef CONFIG_X86_64
  2246. int cpu = raw_smp_processor_id();
  2247. unsigned long fs_base, kernel_gs_base;
  2248. #endif
  2249. int i;
  2250. if (vmx->host_state.loaded)
  2251. return;
  2252. vmx->host_state.loaded = 1;
  2253. /*
  2254. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  2255. * allow segment selectors with cpl > 0 or ti == 1.
  2256. */
  2257. vmx->host_state.ldt_sel = kvm_read_ldt();
  2258. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  2259. #ifdef CONFIG_X86_64
  2260. if (likely(is_64bit_mm(current->mm))) {
  2261. save_fsgs_for_kvm();
  2262. vmx->host_state.fs_sel = current->thread.fsindex;
  2263. vmx->host_state.gs_sel = current->thread.gsindex;
  2264. fs_base = current->thread.fsbase;
  2265. kernel_gs_base = current->thread.gsbase;
  2266. } else {
  2267. #endif
  2268. savesegment(fs, vmx->host_state.fs_sel);
  2269. savesegment(gs, vmx->host_state.gs_sel);
  2270. #ifdef CONFIG_X86_64
  2271. fs_base = read_msr(MSR_FS_BASE);
  2272. kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
  2273. }
  2274. #endif
  2275. if (!(vmx->host_state.fs_sel & 7)) {
  2276. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  2277. vmx->host_state.fs_reload_needed = 0;
  2278. } else {
  2279. vmcs_write16(HOST_FS_SELECTOR, 0);
  2280. vmx->host_state.fs_reload_needed = 1;
  2281. }
  2282. if (!(vmx->host_state.gs_sel & 7))
  2283. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  2284. else {
  2285. vmcs_write16(HOST_GS_SELECTOR, 0);
  2286. vmx->host_state.gs_ldt_reload_needed = 1;
  2287. }
  2288. #ifdef CONFIG_X86_64
  2289. savesegment(ds, vmx->host_state.ds_sel);
  2290. savesegment(es, vmx->host_state.es_sel);
  2291. vmcs_writel(HOST_FS_BASE, fs_base);
  2292. vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
  2293. vmx->msr_host_kernel_gs_base = kernel_gs_base;
  2294. if (is_long_mode(&vmx->vcpu))
  2295. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2296. #else
  2297. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  2298. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  2299. #endif
  2300. for (i = 0; i < vmx->save_nmsrs; ++i)
  2301. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  2302. vmx->guest_msrs[i].data,
  2303. vmx->guest_msrs[i].mask);
  2304. }
  2305. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  2306. {
  2307. if (!vmx->host_state.loaded)
  2308. return;
  2309. ++vmx->vcpu.stat.host_state_reload;
  2310. vmx->host_state.loaded = 0;
  2311. #ifdef CONFIG_X86_64
  2312. if (is_long_mode(&vmx->vcpu))
  2313. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2314. #endif
  2315. if (vmx->host_state.gs_ldt_reload_needed) {
  2316. kvm_load_ldt(vmx->host_state.ldt_sel);
  2317. #ifdef CONFIG_X86_64
  2318. load_gs_index(vmx->host_state.gs_sel);
  2319. #else
  2320. loadsegment(gs, vmx->host_state.gs_sel);
  2321. #endif
  2322. }
  2323. if (vmx->host_state.fs_reload_needed)
  2324. loadsegment(fs, vmx->host_state.fs_sel);
  2325. #ifdef CONFIG_X86_64
  2326. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  2327. loadsegment(ds, vmx->host_state.ds_sel);
  2328. loadsegment(es, vmx->host_state.es_sel);
  2329. }
  2330. #endif
  2331. invalidate_tss_limit();
  2332. #ifdef CONFIG_X86_64
  2333. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  2334. #endif
  2335. load_fixmap_gdt(raw_smp_processor_id());
  2336. }
  2337. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  2338. {
  2339. preempt_disable();
  2340. __vmx_load_host_state(vmx);
  2341. preempt_enable();
  2342. }
  2343. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  2344. {
  2345. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2346. struct pi_desc old, new;
  2347. unsigned int dest;
  2348. /*
  2349. * In case of hot-plug or hot-unplug, we may have to undo
  2350. * vmx_vcpu_pi_put even if there is no assigned device. And we
  2351. * always keep PI.NDST up to date for simplicity: it makes the
  2352. * code easier, and CPU migration is not a fast path.
  2353. */
  2354. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  2355. return;
  2356. /*
  2357. * First handle the simple case where no cmpxchg is necessary; just
  2358. * allow posting non-urgent interrupts.
  2359. *
  2360. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  2361. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  2362. * expects the VCPU to be on the blocked_vcpu_list that matches
  2363. * PI.NDST.
  2364. */
  2365. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  2366. vcpu->cpu == cpu) {
  2367. pi_clear_sn(pi_desc);
  2368. return;
  2369. }
  2370. /* The full case. */
  2371. do {
  2372. old.control = new.control = pi_desc->control;
  2373. dest = cpu_physical_id(cpu);
  2374. if (x2apic_enabled())
  2375. new.ndst = dest;
  2376. else
  2377. new.ndst = (dest << 8) & 0xFF00;
  2378. new.sn = 0;
  2379. } while (cmpxchg64(&pi_desc->control, old.control,
  2380. new.control) != old.control);
  2381. }
  2382. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  2383. {
  2384. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  2385. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  2386. }
  2387. /*
  2388. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  2389. * vcpu mutex is already taken.
  2390. */
  2391. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2392. {
  2393. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2394. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  2395. if (!already_loaded) {
  2396. loaded_vmcs_clear(vmx->loaded_vmcs);
  2397. local_irq_disable();
  2398. crash_disable_local_vmclear(cpu);
  2399. /*
  2400. * Read loaded_vmcs->cpu should be before fetching
  2401. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  2402. * See the comments in __loaded_vmcs_clear().
  2403. */
  2404. smp_rmb();
  2405. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  2406. &per_cpu(loaded_vmcss_on_cpu, cpu));
  2407. crash_enable_local_vmclear(cpu);
  2408. local_irq_enable();
  2409. }
  2410. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  2411. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  2412. vmcs_load(vmx->loaded_vmcs->vmcs);
  2413. indirect_branch_prediction_barrier();
  2414. }
  2415. if (!already_loaded) {
  2416. void *gdt = get_current_gdt_ro();
  2417. unsigned long sysenter_esp;
  2418. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2419. /*
  2420. * Linux uses per-cpu TSS and GDT, so set these when switching
  2421. * processors. See 22.2.4.
  2422. */
  2423. vmcs_writel(HOST_TR_BASE,
  2424. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2425. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2426. /*
  2427. * VM exits change the host TR limit to 0x67 after a VM
  2428. * exit. This is okay, since 0x67 covers everything except
  2429. * the IO bitmap and have have code to handle the IO bitmap
  2430. * being lost after a VM exit.
  2431. */
  2432. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2433. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2434. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2435. vmx->loaded_vmcs->cpu = cpu;
  2436. }
  2437. /* Setup TSC multiplier */
  2438. if (kvm_has_tsc_control &&
  2439. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2440. decache_tsc_multiplier(vmx);
  2441. vmx_vcpu_pi_load(vcpu, cpu);
  2442. vmx->host_pkru = read_pkru();
  2443. vmx->host_debugctlmsr = get_debugctlmsr();
  2444. }
  2445. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2446. {
  2447. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2448. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2449. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2450. !kvm_vcpu_apicv_active(vcpu))
  2451. return;
  2452. /* Set SN when the vCPU is preempted */
  2453. if (vcpu->preempted)
  2454. pi_set_sn(pi_desc);
  2455. }
  2456. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2457. {
  2458. vmx_vcpu_pi_put(vcpu);
  2459. __vmx_load_host_state(to_vmx(vcpu));
  2460. }
  2461. static bool emulation_required(struct kvm_vcpu *vcpu)
  2462. {
  2463. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2464. }
  2465. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2466. /*
  2467. * Return the cr0 value that a nested guest would read. This is a combination
  2468. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2469. * its hypervisor (cr0_read_shadow).
  2470. */
  2471. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2472. {
  2473. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2474. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2475. }
  2476. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2477. {
  2478. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2479. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2480. }
  2481. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2482. {
  2483. unsigned long rflags, save_rflags;
  2484. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2485. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2486. rflags = vmcs_readl(GUEST_RFLAGS);
  2487. if (to_vmx(vcpu)->rmode.vm86_active) {
  2488. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2489. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2490. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2491. }
  2492. to_vmx(vcpu)->rflags = rflags;
  2493. }
  2494. return to_vmx(vcpu)->rflags;
  2495. }
  2496. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2497. {
  2498. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2499. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2500. to_vmx(vcpu)->rflags = rflags;
  2501. if (to_vmx(vcpu)->rmode.vm86_active) {
  2502. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2503. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2504. }
  2505. vmcs_writel(GUEST_RFLAGS, rflags);
  2506. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2507. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2508. }
  2509. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2510. {
  2511. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2512. int ret = 0;
  2513. if (interruptibility & GUEST_INTR_STATE_STI)
  2514. ret |= KVM_X86_SHADOW_INT_STI;
  2515. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2516. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2517. return ret;
  2518. }
  2519. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2520. {
  2521. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2522. u32 interruptibility = interruptibility_old;
  2523. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2524. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2525. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2526. else if (mask & KVM_X86_SHADOW_INT_STI)
  2527. interruptibility |= GUEST_INTR_STATE_STI;
  2528. if ((interruptibility != interruptibility_old))
  2529. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2530. }
  2531. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2532. {
  2533. unsigned long rip;
  2534. rip = kvm_rip_read(vcpu);
  2535. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2536. kvm_rip_write(vcpu, rip);
  2537. /* skipping an emulated instruction also counts */
  2538. vmx_set_interrupt_shadow(vcpu, 0);
  2539. }
  2540. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2541. unsigned long exit_qual)
  2542. {
  2543. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2544. unsigned int nr = vcpu->arch.exception.nr;
  2545. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2546. if (vcpu->arch.exception.has_error_code) {
  2547. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2548. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2549. }
  2550. if (kvm_exception_is_soft(nr))
  2551. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2552. else
  2553. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2554. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2555. vmx_get_nmi_mask(vcpu))
  2556. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2557. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2558. }
  2559. /*
  2560. * KVM wants to inject page-faults which it got to the guest. This function
  2561. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2562. */
  2563. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2564. {
  2565. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2566. unsigned int nr = vcpu->arch.exception.nr;
  2567. if (nr == PF_VECTOR) {
  2568. if (vcpu->arch.exception.nested_apf) {
  2569. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2570. return 1;
  2571. }
  2572. /*
  2573. * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
  2574. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  2575. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
  2576. * can be written only when inject_pending_event runs. This should be
  2577. * conditional on a new capability---if the capability is disabled,
  2578. * kvm_multiple_exception would write the ancillary information to
  2579. * CR2 or DR6, for backwards ABI-compatibility.
  2580. */
  2581. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2582. vcpu->arch.exception.error_code)) {
  2583. *exit_qual = vcpu->arch.cr2;
  2584. return 1;
  2585. }
  2586. } else {
  2587. if (vmcs12->exception_bitmap & (1u << nr)) {
  2588. if (nr == DB_VECTOR)
  2589. *exit_qual = vcpu->arch.dr6;
  2590. else
  2591. *exit_qual = 0;
  2592. return 1;
  2593. }
  2594. }
  2595. return 0;
  2596. }
  2597. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  2598. {
  2599. /*
  2600. * Ensure that we clear the HLT state in the VMCS. We don't need to
  2601. * explicitly skip the instruction because if the HLT state is set,
  2602. * then the instruction is already executing and RIP has already been
  2603. * advanced.
  2604. */
  2605. if (kvm_hlt_in_guest(vcpu->kvm) &&
  2606. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  2607. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2608. }
  2609. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2610. {
  2611. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2612. unsigned nr = vcpu->arch.exception.nr;
  2613. bool has_error_code = vcpu->arch.exception.has_error_code;
  2614. u32 error_code = vcpu->arch.exception.error_code;
  2615. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2616. if (has_error_code) {
  2617. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2618. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2619. }
  2620. if (vmx->rmode.vm86_active) {
  2621. int inc_eip = 0;
  2622. if (kvm_exception_is_soft(nr))
  2623. inc_eip = vcpu->arch.event_exit_inst_len;
  2624. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2625. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2626. return;
  2627. }
  2628. WARN_ON_ONCE(vmx->emulation_required);
  2629. if (kvm_exception_is_soft(nr)) {
  2630. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2631. vmx->vcpu.arch.event_exit_inst_len);
  2632. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2633. } else
  2634. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2635. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2636. vmx_clear_hlt(vcpu);
  2637. }
  2638. static bool vmx_rdtscp_supported(void)
  2639. {
  2640. return cpu_has_vmx_rdtscp();
  2641. }
  2642. static bool vmx_invpcid_supported(void)
  2643. {
  2644. return cpu_has_vmx_invpcid() && enable_ept;
  2645. }
  2646. /*
  2647. * Swap MSR entry in host/guest MSR entry array.
  2648. */
  2649. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2650. {
  2651. struct shared_msr_entry tmp;
  2652. tmp = vmx->guest_msrs[to];
  2653. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2654. vmx->guest_msrs[from] = tmp;
  2655. }
  2656. /*
  2657. * Set up the vmcs to automatically save and restore system
  2658. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2659. * mode, as fiddling with msrs is very expensive.
  2660. */
  2661. static void setup_msrs(struct vcpu_vmx *vmx)
  2662. {
  2663. int save_nmsrs, index;
  2664. save_nmsrs = 0;
  2665. #ifdef CONFIG_X86_64
  2666. if (is_long_mode(&vmx->vcpu)) {
  2667. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2668. if (index >= 0)
  2669. move_msr_up(vmx, index, save_nmsrs++);
  2670. index = __find_msr_index(vmx, MSR_LSTAR);
  2671. if (index >= 0)
  2672. move_msr_up(vmx, index, save_nmsrs++);
  2673. index = __find_msr_index(vmx, MSR_CSTAR);
  2674. if (index >= 0)
  2675. move_msr_up(vmx, index, save_nmsrs++);
  2676. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2677. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2678. move_msr_up(vmx, index, save_nmsrs++);
  2679. /*
  2680. * MSR_STAR is only needed on long mode guests, and only
  2681. * if efer.sce is enabled.
  2682. */
  2683. index = __find_msr_index(vmx, MSR_STAR);
  2684. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2685. move_msr_up(vmx, index, save_nmsrs++);
  2686. }
  2687. #endif
  2688. index = __find_msr_index(vmx, MSR_EFER);
  2689. if (index >= 0 && update_transition_efer(vmx, index))
  2690. move_msr_up(vmx, index, save_nmsrs++);
  2691. vmx->save_nmsrs = save_nmsrs;
  2692. if (cpu_has_vmx_msr_bitmap())
  2693. vmx_update_msr_bitmap(&vmx->vcpu);
  2694. }
  2695. static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  2696. {
  2697. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2698. if (is_guest_mode(vcpu) &&
  2699. (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
  2700. return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
  2701. return vcpu->arch.tsc_offset;
  2702. }
  2703. /*
  2704. * writes 'offset' into guest's timestamp counter offset register
  2705. */
  2706. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2707. {
  2708. if (is_guest_mode(vcpu)) {
  2709. /*
  2710. * We're here if L1 chose not to trap WRMSR to TSC. According
  2711. * to the spec, this should set L1's TSC; The offset that L1
  2712. * set for L2 remains unchanged, and still needs to be added
  2713. * to the newly set TSC to get L2's TSC.
  2714. */
  2715. struct vmcs12 *vmcs12;
  2716. /* recalculate vmcs02.TSC_OFFSET: */
  2717. vmcs12 = get_vmcs12(vcpu);
  2718. vmcs_write64(TSC_OFFSET, offset +
  2719. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  2720. vmcs12->tsc_offset : 0));
  2721. } else {
  2722. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2723. vmcs_read64(TSC_OFFSET), offset);
  2724. vmcs_write64(TSC_OFFSET, offset);
  2725. }
  2726. }
  2727. /*
  2728. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2729. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2730. * all guests if the "nested" module option is off, and can also be disabled
  2731. * for a single guest by disabling its VMX cpuid bit.
  2732. */
  2733. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2734. {
  2735. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2736. }
  2737. /*
  2738. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2739. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2740. * The same values should also be used to verify that vmcs12 control fields are
  2741. * valid during nested entry from L1 to L2.
  2742. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2743. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2744. * bit in the high half is on if the corresponding bit in the control field
  2745. * may be on. See also vmx_control_verify().
  2746. */
  2747. static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
  2748. {
  2749. if (!nested) {
  2750. memset(msrs, 0, sizeof(*msrs));
  2751. return;
  2752. }
  2753. /*
  2754. * Note that as a general rule, the high half of the MSRs (bits in
  2755. * the control fields which may be 1) should be initialized by the
  2756. * intersection of the underlying hardware's MSR (i.e., features which
  2757. * can be supported) and the list of features we want to expose -
  2758. * because they are known to be properly supported in our code.
  2759. * Also, usually, the low half of the MSRs (bits which must be 1) can
  2760. * be set to 0, meaning that L1 may turn off any of these bits. The
  2761. * reason is that if one of these bits is necessary, it will appear
  2762. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  2763. * fields of vmcs01 and vmcs02, will turn these bits off - and
  2764. * nested_vmx_exit_reflected() will not pass related exits to L1.
  2765. * These rules have exceptions below.
  2766. */
  2767. /* pin-based controls */
  2768. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2769. msrs->pinbased_ctls_low,
  2770. msrs->pinbased_ctls_high);
  2771. msrs->pinbased_ctls_low |=
  2772. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2773. msrs->pinbased_ctls_high &=
  2774. PIN_BASED_EXT_INTR_MASK |
  2775. PIN_BASED_NMI_EXITING |
  2776. PIN_BASED_VIRTUAL_NMIS |
  2777. (apicv ? PIN_BASED_POSTED_INTR : 0);
  2778. msrs->pinbased_ctls_high |=
  2779. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2780. PIN_BASED_VMX_PREEMPTION_TIMER;
  2781. /* exit controls */
  2782. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  2783. msrs->exit_ctls_low,
  2784. msrs->exit_ctls_high);
  2785. msrs->exit_ctls_low =
  2786. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2787. msrs->exit_ctls_high &=
  2788. #ifdef CONFIG_X86_64
  2789. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  2790. #endif
  2791. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  2792. msrs->exit_ctls_high |=
  2793. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  2794. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  2795. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  2796. if (kvm_mpx_supported())
  2797. msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  2798. /* We support free control of debug control saving. */
  2799. msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  2800. /* entry controls */
  2801. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  2802. msrs->entry_ctls_low,
  2803. msrs->entry_ctls_high);
  2804. msrs->entry_ctls_low =
  2805. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2806. msrs->entry_ctls_high &=
  2807. #ifdef CONFIG_X86_64
  2808. VM_ENTRY_IA32E_MODE |
  2809. #endif
  2810. VM_ENTRY_LOAD_IA32_PAT;
  2811. msrs->entry_ctls_high |=
  2812. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  2813. if (kvm_mpx_supported())
  2814. msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  2815. /* We support free control of debug control loading. */
  2816. msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2817. /* cpu-based controls */
  2818. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  2819. msrs->procbased_ctls_low,
  2820. msrs->procbased_ctls_high);
  2821. msrs->procbased_ctls_low =
  2822. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2823. msrs->procbased_ctls_high &=
  2824. CPU_BASED_VIRTUAL_INTR_PENDING |
  2825. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  2826. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  2827. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  2828. CPU_BASED_CR3_STORE_EXITING |
  2829. #ifdef CONFIG_X86_64
  2830. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  2831. #endif
  2832. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  2833. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  2834. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  2835. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  2836. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2837. /*
  2838. * We can allow some features even when not supported by the
  2839. * hardware. For example, L1 can specify an MSR bitmap - and we
  2840. * can use it to avoid exits to L1 - even when L0 runs L2
  2841. * without MSR bitmaps.
  2842. */
  2843. msrs->procbased_ctls_high |=
  2844. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2845. CPU_BASED_USE_MSR_BITMAPS;
  2846. /* We support free control of CR3 access interception. */
  2847. msrs->procbased_ctls_low &=
  2848. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  2849. /*
  2850. * secondary cpu-based controls. Do not include those that
  2851. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  2852. */
  2853. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  2854. msrs->secondary_ctls_low,
  2855. msrs->secondary_ctls_high);
  2856. msrs->secondary_ctls_low = 0;
  2857. msrs->secondary_ctls_high &=
  2858. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2859. SECONDARY_EXEC_DESC |
  2860. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2861. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2862. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2863. SECONDARY_EXEC_WBINVD_EXITING;
  2864. /*
  2865. * We can emulate "VMCS shadowing," even if the hardware
  2866. * doesn't support it.
  2867. */
  2868. msrs->secondary_ctls_high |=
  2869. SECONDARY_EXEC_SHADOW_VMCS;
  2870. if (enable_ept) {
  2871. /* nested EPT: emulate EPT also to L1 */
  2872. msrs->secondary_ctls_high |=
  2873. SECONDARY_EXEC_ENABLE_EPT;
  2874. msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  2875. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  2876. if (cpu_has_vmx_ept_execute_only())
  2877. msrs->ept_caps |=
  2878. VMX_EPT_EXECUTE_ONLY_BIT;
  2879. msrs->ept_caps &= vmx_capability.ept;
  2880. msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  2881. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  2882. VMX_EPT_1GB_PAGE_BIT;
  2883. if (enable_ept_ad_bits) {
  2884. msrs->secondary_ctls_high |=
  2885. SECONDARY_EXEC_ENABLE_PML;
  2886. msrs->ept_caps |= VMX_EPT_AD_BIT;
  2887. }
  2888. }
  2889. if (cpu_has_vmx_vmfunc()) {
  2890. msrs->secondary_ctls_high |=
  2891. SECONDARY_EXEC_ENABLE_VMFUNC;
  2892. /*
  2893. * Advertise EPTP switching unconditionally
  2894. * since we emulate it
  2895. */
  2896. if (enable_ept)
  2897. msrs->vmfunc_controls =
  2898. VMX_VMFUNC_EPTP_SWITCHING;
  2899. }
  2900. /*
  2901. * Old versions of KVM use the single-context version without
  2902. * checking for support, so declare that it is supported even
  2903. * though it is treated as global context. The alternative is
  2904. * not failing the single-context invvpid, and it is worse.
  2905. */
  2906. if (enable_vpid) {
  2907. msrs->secondary_ctls_high |=
  2908. SECONDARY_EXEC_ENABLE_VPID;
  2909. msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
  2910. VMX_VPID_EXTENT_SUPPORTED_MASK;
  2911. }
  2912. if (enable_unrestricted_guest)
  2913. msrs->secondary_ctls_high |=
  2914. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2915. /* miscellaneous data */
  2916. rdmsr(MSR_IA32_VMX_MISC,
  2917. msrs->misc_low,
  2918. msrs->misc_high);
  2919. msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
  2920. msrs->misc_low |=
  2921. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
  2922. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  2923. VMX_MISC_ACTIVITY_HLT;
  2924. msrs->misc_high = 0;
  2925. /*
  2926. * This MSR reports some information about VMX support. We
  2927. * should return information about the VMX we emulate for the
  2928. * guest, and the VMCS structure we give it - not about the
  2929. * VMX support of the underlying hardware.
  2930. */
  2931. msrs->basic =
  2932. VMCS12_REVISION |
  2933. VMX_BASIC_TRUE_CTLS |
  2934. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  2935. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  2936. if (cpu_has_vmx_basic_inout())
  2937. msrs->basic |= VMX_BASIC_INOUT;
  2938. /*
  2939. * These MSRs specify bits which the guest must keep fixed on
  2940. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  2941. * We picked the standard core2 setting.
  2942. */
  2943. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  2944. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  2945. msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
  2946. msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
  2947. /* These MSRs specify bits which the guest must keep fixed off. */
  2948. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
  2949. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
  2950. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  2951. msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  2952. }
  2953. /*
  2954. * if fixed0[i] == 1: val[i] must be 1
  2955. * if fixed1[i] == 0: val[i] must be 0
  2956. */
  2957. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  2958. {
  2959. return ((val & fixed1) | fixed0) == val;
  2960. }
  2961. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  2962. {
  2963. return fixed_bits_valid(control, low, high);
  2964. }
  2965. static inline u64 vmx_control_msr(u32 low, u32 high)
  2966. {
  2967. return low | ((u64)high << 32);
  2968. }
  2969. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  2970. {
  2971. superset &= mask;
  2972. subset &= mask;
  2973. return (superset | subset) == superset;
  2974. }
  2975. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  2976. {
  2977. const u64 feature_and_reserved =
  2978. /* feature (except bit 48; see below) */
  2979. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  2980. /* reserved */
  2981. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  2982. u64 vmx_basic = vmx->nested.msrs.basic;
  2983. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  2984. return -EINVAL;
  2985. /*
  2986. * KVM does not emulate a version of VMX that constrains physical
  2987. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  2988. */
  2989. if (data & BIT_ULL(48))
  2990. return -EINVAL;
  2991. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  2992. vmx_basic_vmcs_revision_id(data))
  2993. return -EINVAL;
  2994. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  2995. return -EINVAL;
  2996. vmx->nested.msrs.basic = data;
  2997. return 0;
  2998. }
  2999. static int
  3000. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3001. {
  3002. u64 supported;
  3003. u32 *lowp, *highp;
  3004. switch (msr_index) {
  3005. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3006. lowp = &vmx->nested.msrs.pinbased_ctls_low;
  3007. highp = &vmx->nested.msrs.pinbased_ctls_high;
  3008. break;
  3009. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3010. lowp = &vmx->nested.msrs.procbased_ctls_low;
  3011. highp = &vmx->nested.msrs.procbased_ctls_high;
  3012. break;
  3013. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3014. lowp = &vmx->nested.msrs.exit_ctls_low;
  3015. highp = &vmx->nested.msrs.exit_ctls_high;
  3016. break;
  3017. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3018. lowp = &vmx->nested.msrs.entry_ctls_low;
  3019. highp = &vmx->nested.msrs.entry_ctls_high;
  3020. break;
  3021. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3022. lowp = &vmx->nested.msrs.secondary_ctls_low;
  3023. highp = &vmx->nested.msrs.secondary_ctls_high;
  3024. break;
  3025. default:
  3026. BUG();
  3027. }
  3028. supported = vmx_control_msr(*lowp, *highp);
  3029. /* Check must-be-1 bits are still 1. */
  3030. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  3031. return -EINVAL;
  3032. /* Check must-be-0 bits are still 0. */
  3033. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  3034. return -EINVAL;
  3035. *lowp = data;
  3036. *highp = data >> 32;
  3037. return 0;
  3038. }
  3039. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  3040. {
  3041. const u64 feature_and_reserved_bits =
  3042. /* feature */
  3043. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  3044. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  3045. /* reserved */
  3046. GENMASK_ULL(13, 9) | BIT_ULL(31);
  3047. u64 vmx_misc;
  3048. vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
  3049. vmx->nested.msrs.misc_high);
  3050. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  3051. return -EINVAL;
  3052. if ((vmx->nested.msrs.pinbased_ctls_high &
  3053. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  3054. vmx_misc_preemption_timer_rate(data) !=
  3055. vmx_misc_preemption_timer_rate(vmx_misc))
  3056. return -EINVAL;
  3057. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  3058. return -EINVAL;
  3059. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  3060. return -EINVAL;
  3061. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  3062. return -EINVAL;
  3063. vmx->nested.msrs.misc_low = data;
  3064. vmx->nested.msrs.misc_high = data >> 32;
  3065. /*
  3066. * If L1 has read-only VM-exit information fields, use the
  3067. * less permissive vmx_vmwrite_bitmap to specify write
  3068. * permissions for the shadow VMCS.
  3069. */
  3070. if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  3071. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3072. return 0;
  3073. }
  3074. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  3075. {
  3076. u64 vmx_ept_vpid_cap;
  3077. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
  3078. vmx->nested.msrs.vpid_caps);
  3079. /* Every bit is either reserved or a feature bit. */
  3080. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  3081. return -EINVAL;
  3082. vmx->nested.msrs.ept_caps = data;
  3083. vmx->nested.msrs.vpid_caps = data >> 32;
  3084. return 0;
  3085. }
  3086. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3087. {
  3088. u64 *msr;
  3089. switch (msr_index) {
  3090. case MSR_IA32_VMX_CR0_FIXED0:
  3091. msr = &vmx->nested.msrs.cr0_fixed0;
  3092. break;
  3093. case MSR_IA32_VMX_CR4_FIXED0:
  3094. msr = &vmx->nested.msrs.cr4_fixed0;
  3095. break;
  3096. default:
  3097. BUG();
  3098. }
  3099. /*
  3100. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  3101. * must be 1 in the restored value.
  3102. */
  3103. if (!is_bitwise_subset(data, *msr, -1ULL))
  3104. return -EINVAL;
  3105. *msr = data;
  3106. return 0;
  3107. }
  3108. /*
  3109. * Called when userspace is restoring VMX MSRs.
  3110. *
  3111. * Returns 0 on success, non-0 otherwise.
  3112. */
  3113. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  3114. {
  3115. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3116. /*
  3117. * Don't allow changes to the VMX capability MSRs while the vCPU
  3118. * is in VMX operation.
  3119. */
  3120. if (vmx->nested.vmxon)
  3121. return -EBUSY;
  3122. switch (msr_index) {
  3123. case MSR_IA32_VMX_BASIC:
  3124. return vmx_restore_vmx_basic(vmx, data);
  3125. case MSR_IA32_VMX_PINBASED_CTLS:
  3126. case MSR_IA32_VMX_PROCBASED_CTLS:
  3127. case MSR_IA32_VMX_EXIT_CTLS:
  3128. case MSR_IA32_VMX_ENTRY_CTLS:
  3129. /*
  3130. * The "non-true" VMX capability MSRs are generated from the
  3131. * "true" MSRs, so we do not support restoring them directly.
  3132. *
  3133. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  3134. * should restore the "true" MSRs with the must-be-1 bits
  3135. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  3136. * DEFAULT SETTINGS".
  3137. */
  3138. return -EINVAL;
  3139. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3140. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3141. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3142. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3143. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3144. return vmx_restore_control_msr(vmx, msr_index, data);
  3145. case MSR_IA32_VMX_MISC:
  3146. return vmx_restore_vmx_misc(vmx, data);
  3147. case MSR_IA32_VMX_CR0_FIXED0:
  3148. case MSR_IA32_VMX_CR4_FIXED0:
  3149. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  3150. case MSR_IA32_VMX_CR0_FIXED1:
  3151. case MSR_IA32_VMX_CR4_FIXED1:
  3152. /*
  3153. * These MSRs are generated based on the vCPU's CPUID, so we
  3154. * do not support restoring them directly.
  3155. */
  3156. return -EINVAL;
  3157. case MSR_IA32_VMX_EPT_VPID_CAP:
  3158. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  3159. case MSR_IA32_VMX_VMCS_ENUM:
  3160. vmx->nested.msrs.vmcs_enum = data;
  3161. return 0;
  3162. default:
  3163. /*
  3164. * The rest of the VMX capability MSRs do not support restore.
  3165. */
  3166. return -EINVAL;
  3167. }
  3168. }
  3169. /* Returns 0 on success, non-0 otherwise. */
  3170. static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
  3171. {
  3172. switch (msr_index) {
  3173. case MSR_IA32_VMX_BASIC:
  3174. *pdata = msrs->basic;
  3175. break;
  3176. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3177. case MSR_IA32_VMX_PINBASED_CTLS:
  3178. *pdata = vmx_control_msr(
  3179. msrs->pinbased_ctls_low,
  3180. msrs->pinbased_ctls_high);
  3181. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  3182. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3183. break;
  3184. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3185. case MSR_IA32_VMX_PROCBASED_CTLS:
  3186. *pdata = vmx_control_msr(
  3187. msrs->procbased_ctls_low,
  3188. msrs->procbased_ctls_high);
  3189. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  3190. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3191. break;
  3192. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3193. case MSR_IA32_VMX_EXIT_CTLS:
  3194. *pdata = vmx_control_msr(
  3195. msrs->exit_ctls_low,
  3196. msrs->exit_ctls_high);
  3197. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  3198. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3199. break;
  3200. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3201. case MSR_IA32_VMX_ENTRY_CTLS:
  3202. *pdata = vmx_control_msr(
  3203. msrs->entry_ctls_low,
  3204. msrs->entry_ctls_high);
  3205. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  3206. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3207. break;
  3208. case MSR_IA32_VMX_MISC:
  3209. *pdata = vmx_control_msr(
  3210. msrs->misc_low,
  3211. msrs->misc_high);
  3212. break;
  3213. case MSR_IA32_VMX_CR0_FIXED0:
  3214. *pdata = msrs->cr0_fixed0;
  3215. break;
  3216. case MSR_IA32_VMX_CR0_FIXED1:
  3217. *pdata = msrs->cr0_fixed1;
  3218. break;
  3219. case MSR_IA32_VMX_CR4_FIXED0:
  3220. *pdata = msrs->cr4_fixed0;
  3221. break;
  3222. case MSR_IA32_VMX_CR4_FIXED1:
  3223. *pdata = msrs->cr4_fixed1;
  3224. break;
  3225. case MSR_IA32_VMX_VMCS_ENUM:
  3226. *pdata = msrs->vmcs_enum;
  3227. break;
  3228. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3229. *pdata = vmx_control_msr(
  3230. msrs->secondary_ctls_low,
  3231. msrs->secondary_ctls_high);
  3232. break;
  3233. case MSR_IA32_VMX_EPT_VPID_CAP:
  3234. *pdata = msrs->ept_caps |
  3235. ((u64)msrs->vpid_caps << 32);
  3236. break;
  3237. case MSR_IA32_VMX_VMFUNC:
  3238. *pdata = msrs->vmfunc_controls;
  3239. break;
  3240. default:
  3241. return 1;
  3242. }
  3243. return 0;
  3244. }
  3245. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  3246. uint64_t val)
  3247. {
  3248. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  3249. return !(val & ~valid_bits);
  3250. }
  3251. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  3252. {
  3253. switch (msr->index) {
  3254. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3255. if (!nested)
  3256. return 1;
  3257. return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
  3258. default:
  3259. return 1;
  3260. }
  3261. return 0;
  3262. }
  3263. /*
  3264. * Reads an msr value (of 'msr_index') into 'pdata'.
  3265. * Returns 0 on success, non-0 otherwise.
  3266. * Assumes vcpu_load() was already called.
  3267. */
  3268. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3269. {
  3270. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3271. struct shared_msr_entry *msr;
  3272. switch (msr_info->index) {
  3273. #ifdef CONFIG_X86_64
  3274. case MSR_FS_BASE:
  3275. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  3276. break;
  3277. case MSR_GS_BASE:
  3278. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  3279. break;
  3280. case MSR_KERNEL_GS_BASE:
  3281. vmx_load_host_state(vmx);
  3282. msr_info->data = vmx->msr_guest_kernel_gs_base;
  3283. break;
  3284. #endif
  3285. case MSR_EFER:
  3286. return kvm_get_msr_common(vcpu, msr_info);
  3287. case MSR_IA32_SPEC_CTRL:
  3288. if (!msr_info->host_initiated &&
  3289. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3290. return 1;
  3291. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  3292. break;
  3293. case MSR_IA32_ARCH_CAPABILITIES:
  3294. if (!msr_info->host_initiated &&
  3295. !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
  3296. return 1;
  3297. msr_info->data = to_vmx(vcpu)->arch_capabilities;
  3298. break;
  3299. case MSR_IA32_SYSENTER_CS:
  3300. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  3301. break;
  3302. case MSR_IA32_SYSENTER_EIP:
  3303. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  3304. break;
  3305. case MSR_IA32_SYSENTER_ESP:
  3306. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  3307. break;
  3308. case MSR_IA32_BNDCFGS:
  3309. if (!kvm_mpx_supported() ||
  3310. (!msr_info->host_initiated &&
  3311. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3312. return 1;
  3313. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  3314. break;
  3315. case MSR_IA32_MCG_EXT_CTL:
  3316. if (!msr_info->host_initiated &&
  3317. !(vmx->msr_ia32_feature_control &
  3318. FEATURE_CONTROL_LMCE))
  3319. return 1;
  3320. msr_info->data = vcpu->arch.mcg_ext_ctl;
  3321. break;
  3322. case MSR_IA32_FEATURE_CONTROL:
  3323. msr_info->data = vmx->msr_ia32_feature_control;
  3324. break;
  3325. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3326. if (!nested_vmx_allowed(vcpu))
  3327. return 1;
  3328. return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
  3329. &msr_info->data);
  3330. case MSR_IA32_XSS:
  3331. if (!vmx_xsaves_supported())
  3332. return 1;
  3333. msr_info->data = vcpu->arch.ia32_xss;
  3334. break;
  3335. case MSR_TSC_AUX:
  3336. if (!msr_info->host_initiated &&
  3337. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3338. return 1;
  3339. /* Otherwise falls through */
  3340. default:
  3341. msr = find_msr_entry(vmx, msr_info->index);
  3342. if (msr) {
  3343. msr_info->data = msr->data;
  3344. break;
  3345. }
  3346. return kvm_get_msr_common(vcpu, msr_info);
  3347. }
  3348. return 0;
  3349. }
  3350. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  3351. /*
  3352. * Writes msr value into into the appropriate "register".
  3353. * Returns 0 on success, non-0 otherwise.
  3354. * Assumes vcpu_load() was already called.
  3355. */
  3356. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3357. {
  3358. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3359. struct shared_msr_entry *msr;
  3360. int ret = 0;
  3361. u32 msr_index = msr_info->index;
  3362. u64 data = msr_info->data;
  3363. switch (msr_index) {
  3364. case MSR_EFER:
  3365. ret = kvm_set_msr_common(vcpu, msr_info);
  3366. break;
  3367. #ifdef CONFIG_X86_64
  3368. case MSR_FS_BASE:
  3369. vmx_segment_cache_clear(vmx);
  3370. vmcs_writel(GUEST_FS_BASE, data);
  3371. break;
  3372. case MSR_GS_BASE:
  3373. vmx_segment_cache_clear(vmx);
  3374. vmcs_writel(GUEST_GS_BASE, data);
  3375. break;
  3376. case MSR_KERNEL_GS_BASE:
  3377. vmx_load_host_state(vmx);
  3378. vmx->msr_guest_kernel_gs_base = data;
  3379. break;
  3380. #endif
  3381. case MSR_IA32_SYSENTER_CS:
  3382. vmcs_write32(GUEST_SYSENTER_CS, data);
  3383. break;
  3384. case MSR_IA32_SYSENTER_EIP:
  3385. vmcs_writel(GUEST_SYSENTER_EIP, data);
  3386. break;
  3387. case MSR_IA32_SYSENTER_ESP:
  3388. vmcs_writel(GUEST_SYSENTER_ESP, data);
  3389. break;
  3390. case MSR_IA32_BNDCFGS:
  3391. if (!kvm_mpx_supported() ||
  3392. (!msr_info->host_initiated &&
  3393. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3394. return 1;
  3395. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  3396. (data & MSR_IA32_BNDCFGS_RSVD))
  3397. return 1;
  3398. vmcs_write64(GUEST_BNDCFGS, data);
  3399. break;
  3400. case MSR_IA32_SPEC_CTRL:
  3401. if (!msr_info->host_initiated &&
  3402. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3403. return 1;
  3404. /* The STIBP bit doesn't fault even if it's not advertised */
  3405. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3406. return 1;
  3407. vmx->spec_ctrl = data;
  3408. if (!data)
  3409. break;
  3410. /*
  3411. * For non-nested:
  3412. * When it's written (to non-zero) for the first time, pass
  3413. * it through.
  3414. *
  3415. * For nested:
  3416. * The handling of the MSR bitmap for L2 guests is done in
  3417. * nested_vmx_merge_msr_bitmap. We should not touch the
  3418. * vmcs02.msr_bitmap here since it gets completely overwritten
  3419. * in the merging. We update the vmcs01 here for L1 as well
  3420. * since it will end up touching the MSR anyway now.
  3421. */
  3422. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  3423. MSR_IA32_SPEC_CTRL,
  3424. MSR_TYPE_RW);
  3425. break;
  3426. case MSR_IA32_PRED_CMD:
  3427. if (!msr_info->host_initiated &&
  3428. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3429. return 1;
  3430. if (data & ~PRED_CMD_IBPB)
  3431. return 1;
  3432. if (!data)
  3433. break;
  3434. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3435. /*
  3436. * For non-nested:
  3437. * When it's written (to non-zero) for the first time, pass
  3438. * it through.
  3439. *
  3440. * For nested:
  3441. * The handling of the MSR bitmap for L2 guests is done in
  3442. * nested_vmx_merge_msr_bitmap. We should not touch the
  3443. * vmcs02.msr_bitmap here since it gets completely overwritten
  3444. * in the merging.
  3445. */
  3446. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  3447. MSR_TYPE_W);
  3448. break;
  3449. case MSR_IA32_ARCH_CAPABILITIES:
  3450. if (!msr_info->host_initiated)
  3451. return 1;
  3452. vmx->arch_capabilities = data;
  3453. break;
  3454. case MSR_IA32_CR_PAT:
  3455. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3456. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3457. return 1;
  3458. vmcs_write64(GUEST_IA32_PAT, data);
  3459. vcpu->arch.pat = data;
  3460. break;
  3461. }
  3462. ret = kvm_set_msr_common(vcpu, msr_info);
  3463. break;
  3464. case MSR_IA32_TSC_ADJUST:
  3465. ret = kvm_set_msr_common(vcpu, msr_info);
  3466. break;
  3467. case MSR_IA32_MCG_EXT_CTL:
  3468. if ((!msr_info->host_initiated &&
  3469. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3470. FEATURE_CONTROL_LMCE)) ||
  3471. (data & ~MCG_EXT_CTL_LMCE_EN))
  3472. return 1;
  3473. vcpu->arch.mcg_ext_ctl = data;
  3474. break;
  3475. case MSR_IA32_FEATURE_CONTROL:
  3476. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3477. (to_vmx(vcpu)->msr_ia32_feature_control &
  3478. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3479. return 1;
  3480. vmx->msr_ia32_feature_control = data;
  3481. if (msr_info->host_initiated && data == 0)
  3482. vmx_leave_nested(vcpu);
  3483. break;
  3484. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3485. if (!msr_info->host_initiated)
  3486. return 1; /* they are read-only */
  3487. if (!nested_vmx_allowed(vcpu))
  3488. return 1;
  3489. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3490. case MSR_IA32_XSS:
  3491. if (!vmx_xsaves_supported())
  3492. return 1;
  3493. /*
  3494. * The only supported bit as of Skylake is bit 8, but
  3495. * it is not supported on KVM.
  3496. */
  3497. if (data != 0)
  3498. return 1;
  3499. vcpu->arch.ia32_xss = data;
  3500. if (vcpu->arch.ia32_xss != host_xss)
  3501. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3502. vcpu->arch.ia32_xss, host_xss);
  3503. else
  3504. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3505. break;
  3506. case MSR_TSC_AUX:
  3507. if (!msr_info->host_initiated &&
  3508. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3509. return 1;
  3510. /* Check reserved bit, higher 32 bits should be zero */
  3511. if ((data >> 32) != 0)
  3512. return 1;
  3513. /* Otherwise falls through */
  3514. default:
  3515. msr = find_msr_entry(vmx, msr_index);
  3516. if (msr) {
  3517. u64 old_msr_data = msr->data;
  3518. msr->data = data;
  3519. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3520. preempt_disable();
  3521. ret = kvm_set_shared_msr(msr->index, msr->data,
  3522. msr->mask);
  3523. preempt_enable();
  3524. if (ret)
  3525. msr->data = old_msr_data;
  3526. }
  3527. break;
  3528. }
  3529. ret = kvm_set_msr_common(vcpu, msr_info);
  3530. }
  3531. return ret;
  3532. }
  3533. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3534. {
  3535. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3536. switch (reg) {
  3537. case VCPU_REGS_RSP:
  3538. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3539. break;
  3540. case VCPU_REGS_RIP:
  3541. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3542. break;
  3543. case VCPU_EXREG_PDPTR:
  3544. if (enable_ept)
  3545. ept_save_pdptrs(vcpu);
  3546. break;
  3547. default:
  3548. break;
  3549. }
  3550. }
  3551. static __init int cpu_has_kvm_support(void)
  3552. {
  3553. return cpu_has_vmx();
  3554. }
  3555. static __init int vmx_disabled_by_bios(void)
  3556. {
  3557. u64 msr;
  3558. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3559. if (msr & FEATURE_CONTROL_LOCKED) {
  3560. /* launched w/ TXT and VMX disabled */
  3561. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3562. && tboot_enabled())
  3563. return 1;
  3564. /* launched w/o TXT and VMX only enabled w/ TXT */
  3565. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3566. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3567. && !tboot_enabled()) {
  3568. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3569. "activate TXT before enabling KVM\n");
  3570. return 1;
  3571. }
  3572. /* launched w/o TXT and VMX disabled */
  3573. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3574. && !tboot_enabled())
  3575. return 1;
  3576. }
  3577. return 0;
  3578. }
  3579. static void kvm_cpu_vmxon(u64 addr)
  3580. {
  3581. cr4_set_bits(X86_CR4_VMXE);
  3582. intel_pt_handle_vmx(1);
  3583. asm volatile (ASM_VMX_VMXON_RAX
  3584. : : "a"(&addr), "m"(addr)
  3585. : "memory", "cc");
  3586. }
  3587. static int hardware_enable(void)
  3588. {
  3589. int cpu = raw_smp_processor_id();
  3590. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3591. u64 old, test_bits;
  3592. if (cr4_read_shadow() & X86_CR4_VMXE)
  3593. return -EBUSY;
  3594. /*
  3595. * This can happen if we hot-added a CPU but failed to allocate
  3596. * VP assist page for it.
  3597. */
  3598. if (static_branch_unlikely(&enable_evmcs) &&
  3599. !hv_get_vp_assist_page(cpu))
  3600. return -EFAULT;
  3601. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3602. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3603. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3604. /*
  3605. * Now we can enable the vmclear operation in kdump
  3606. * since the loaded_vmcss_on_cpu list on this cpu
  3607. * has been initialized.
  3608. *
  3609. * Though the cpu is not in VMX operation now, there
  3610. * is no problem to enable the vmclear operation
  3611. * for the loaded_vmcss_on_cpu list is empty!
  3612. */
  3613. crash_enable_local_vmclear(cpu);
  3614. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3615. test_bits = FEATURE_CONTROL_LOCKED;
  3616. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3617. if (tboot_enabled())
  3618. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3619. if ((old & test_bits) != test_bits) {
  3620. /* enable and lock */
  3621. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3622. }
  3623. kvm_cpu_vmxon(phys_addr);
  3624. if (enable_ept)
  3625. ept_sync_global();
  3626. return 0;
  3627. }
  3628. static void vmclear_local_loaded_vmcss(void)
  3629. {
  3630. int cpu = raw_smp_processor_id();
  3631. struct loaded_vmcs *v, *n;
  3632. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3633. loaded_vmcss_on_cpu_link)
  3634. __loaded_vmcs_clear(v);
  3635. }
  3636. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3637. * tricks.
  3638. */
  3639. static void kvm_cpu_vmxoff(void)
  3640. {
  3641. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  3642. intel_pt_handle_vmx(0);
  3643. cr4_clear_bits(X86_CR4_VMXE);
  3644. }
  3645. static void hardware_disable(void)
  3646. {
  3647. vmclear_local_loaded_vmcss();
  3648. kvm_cpu_vmxoff();
  3649. }
  3650. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3651. u32 msr, u32 *result)
  3652. {
  3653. u32 vmx_msr_low, vmx_msr_high;
  3654. u32 ctl = ctl_min | ctl_opt;
  3655. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3656. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3657. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3658. /* Ensure minimum (required) set of control bits are supported. */
  3659. if (ctl_min & ~ctl)
  3660. return -EIO;
  3661. *result = ctl;
  3662. return 0;
  3663. }
  3664. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3665. {
  3666. u32 vmx_msr_low, vmx_msr_high;
  3667. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3668. return vmx_msr_high & ctl;
  3669. }
  3670. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3671. {
  3672. u32 vmx_msr_low, vmx_msr_high;
  3673. u32 min, opt, min2, opt2;
  3674. u32 _pin_based_exec_control = 0;
  3675. u32 _cpu_based_exec_control = 0;
  3676. u32 _cpu_based_2nd_exec_control = 0;
  3677. u32 _vmexit_control = 0;
  3678. u32 _vmentry_control = 0;
  3679. memset(vmcs_conf, 0, sizeof(*vmcs_conf));
  3680. min = CPU_BASED_HLT_EXITING |
  3681. #ifdef CONFIG_X86_64
  3682. CPU_BASED_CR8_LOAD_EXITING |
  3683. CPU_BASED_CR8_STORE_EXITING |
  3684. #endif
  3685. CPU_BASED_CR3_LOAD_EXITING |
  3686. CPU_BASED_CR3_STORE_EXITING |
  3687. CPU_BASED_UNCOND_IO_EXITING |
  3688. CPU_BASED_MOV_DR_EXITING |
  3689. CPU_BASED_USE_TSC_OFFSETING |
  3690. CPU_BASED_MWAIT_EXITING |
  3691. CPU_BASED_MONITOR_EXITING |
  3692. CPU_BASED_INVLPG_EXITING |
  3693. CPU_BASED_RDPMC_EXITING;
  3694. opt = CPU_BASED_TPR_SHADOW |
  3695. CPU_BASED_USE_MSR_BITMAPS |
  3696. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3697. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3698. &_cpu_based_exec_control) < 0)
  3699. return -EIO;
  3700. #ifdef CONFIG_X86_64
  3701. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3702. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3703. ~CPU_BASED_CR8_STORE_EXITING;
  3704. #endif
  3705. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3706. min2 = 0;
  3707. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3708. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3709. SECONDARY_EXEC_WBINVD_EXITING |
  3710. SECONDARY_EXEC_ENABLE_VPID |
  3711. SECONDARY_EXEC_ENABLE_EPT |
  3712. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3713. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3714. SECONDARY_EXEC_DESC |
  3715. SECONDARY_EXEC_RDTSCP |
  3716. SECONDARY_EXEC_ENABLE_INVPCID |
  3717. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3718. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3719. SECONDARY_EXEC_SHADOW_VMCS |
  3720. SECONDARY_EXEC_XSAVES |
  3721. SECONDARY_EXEC_RDSEED_EXITING |
  3722. SECONDARY_EXEC_RDRAND_EXITING |
  3723. SECONDARY_EXEC_ENABLE_PML |
  3724. SECONDARY_EXEC_TSC_SCALING |
  3725. SECONDARY_EXEC_ENABLE_VMFUNC;
  3726. if (adjust_vmx_controls(min2, opt2,
  3727. MSR_IA32_VMX_PROCBASED_CTLS2,
  3728. &_cpu_based_2nd_exec_control) < 0)
  3729. return -EIO;
  3730. }
  3731. #ifndef CONFIG_X86_64
  3732. if (!(_cpu_based_2nd_exec_control &
  3733. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3734. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3735. #endif
  3736. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3737. _cpu_based_2nd_exec_control &= ~(
  3738. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3739. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3740. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3741. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3742. &vmx_capability.ept, &vmx_capability.vpid);
  3743. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  3744. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  3745. enabled */
  3746. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  3747. CPU_BASED_CR3_STORE_EXITING |
  3748. CPU_BASED_INVLPG_EXITING);
  3749. } else if (vmx_capability.ept) {
  3750. vmx_capability.ept = 0;
  3751. pr_warn_once("EPT CAP should not exist if not support "
  3752. "1-setting enable EPT VM-execution control\n");
  3753. }
  3754. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  3755. vmx_capability.vpid) {
  3756. vmx_capability.vpid = 0;
  3757. pr_warn_once("VPID CAP should not exist if not support "
  3758. "1-setting enable VPID VM-execution control\n");
  3759. }
  3760. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  3761. #ifdef CONFIG_X86_64
  3762. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  3763. #endif
  3764. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  3765. VM_EXIT_CLEAR_BNDCFGS;
  3766. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  3767. &_vmexit_control) < 0)
  3768. return -EIO;
  3769. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  3770. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  3771. PIN_BASED_VMX_PREEMPTION_TIMER;
  3772. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  3773. &_pin_based_exec_control) < 0)
  3774. return -EIO;
  3775. if (cpu_has_broken_vmx_preemption_timer())
  3776. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  3777. if (!(_cpu_based_2nd_exec_control &
  3778. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  3779. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  3780. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3781. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  3782. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  3783. &_vmentry_control) < 0)
  3784. return -EIO;
  3785. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  3786. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  3787. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  3788. return -EIO;
  3789. #ifdef CONFIG_X86_64
  3790. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  3791. if (vmx_msr_high & (1u<<16))
  3792. return -EIO;
  3793. #endif
  3794. /* Require Write-Back (WB) memory type for VMCS accesses. */
  3795. if (((vmx_msr_high >> 18) & 15) != 6)
  3796. return -EIO;
  3797. vmcs_conf->size = vmx_msr_high & 0x1fff;
  3798. vmcs_conf->order = get_order(vmcs_conf->size);
  3799. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  3800. vmcs_conf->revision_id = vmx_msr_low;
  3801. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  3802. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  3803. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  3804. vmcs_conf->vmexit_ctrl = _vmexit_control;
  3805. vmcs_conf->vmentry_ctrl = _vmentry_control;
  3806. if (static_branch_unlikely(&enable_evmcs))
  3807. evmcs_sanitize_exec_ctrls(vmcs_conf);
  3808. cpu_has_load_ia32_efer =
  3809. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3810. VM_ENTRY_LOAD_IA32_EFER)
  3811. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3812. VM_EXIT_LOAD_IA32_EFER);
  3813. cpu_has_load_perf_global_ctrl =
  3814. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3815. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  3816. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3817. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  3818. /*
  3819. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  3820. * but due to errata below it can't be used. Workaround is to use
  3821. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  3822. *
  3823. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  3824. *
  3825. * AAK155 (model 26)
  3826. * AAP115 (model 30)
  3827. * AAT100 (model 37)
  3828. * BC86,AAY89,BD102 (model 44)
  3829. * BA97 (model 46)
  3830. *
  3831. */
  3832. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  3833. switch (boot_cpu_data.x86_model) {
  3834. case 26:
  3835. case 30:
  3836. case 37:
  3837. case 44:
  3838. case 46:
  3839. cpu_has_load_perf_global_ctrl = false;
  3840. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  3841. "does not work properly. Using workaround\n");
  3842. break;
  3843. default:
  3844. break;
  3845. }
  3846. }
  3847. if (boot_cpu_has(X86_FEATURE_XSAVES))
  3848. rdmsrl(MSR_IA32_XSS, host_xss);
  3849. return 0;
  3850. }
  3851. static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
  3852. {
  3853. int node = cpu_to_node(cpu);
  3854. struct page *pages;
  3855. struct vmcs *vmcs;
  3856. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  3857. if (!pages)
  3858. return NULL;
  3859. vmcs = page_address(pages);
  3860. memset(vmcs, 0, vmcs_config.size);
  3861. /* KVM supports Enlightened VMCS v1 only */
  3862. if (static_branch_unlikely(&enable_evmcs))
  3863. vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
  3864. else
  3865. vmcs->hdr.revision_id = vmcs_config.revision_id;
  3866. if (shadow)
  3867. vmcs->hdr.shadow_vmcs = 1;
  3868. return vmcs;
  3869. }
  3870. static void free_vmcs(struct vmcs *vmcs)
  3871. {
  3872. free_pages((unsigned long)vmcs, vmcs_config.order);
  3873. }
  3874. /*
  3875. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  3876. */
  3877. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3878. {
  3879. if (!loaded_vmcs->vmcs)
  3880. return;
  3881. loaded_vmcs_clear(loaded_vmcs);
  3882. free_vmcs(loaded_vmcs->vmcs);
  3883. loaded_vmcs->vmcs = NULL;
  3884. if (loaded_vmcs->msr_bitmap)
  3885. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  3886. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  3887. }
  3888. static struct vmcs *alloc_vmcs(bool shadow)
  3889. {
  3890. return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
  3891. }
  3892. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3893. {
  3894. loaded_vmcs->vmcs = alloc_vmcs(false);
  3895. if (!loaded_vmcs->vmcs)
  3896. return -ENOMEM;
  3897. loaded_vmcs->shadow_vmcs = NULL;
  3898. loaded_vmcs_init(loaded_vmcs);
  3899. if (cpu_has_vmx_msr_bitmap()) {
  3900. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  3901. if (!loaded_vmcs->msr_bitmap)
  3902. goto out_vmcs;
  3903. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  3904. if (IS_ENABLED(CONFIG_HYPERV) &&
  3905. static_branch_unlikely(&enable_evmcs) &&
  3906. (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
  3907. struct hv_enlightened_vmcs *evmcs =
  3908. (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
  3909. evmcs->hv_enlightenments_control.msr_bitmap = 1;
  3910. }
  3911. }
  3912. return 0;
  3913. out_vmcs:
  3914. free_loaded_vmcs(loaded_vmcs);
  3915. return -ENOMEM;
  3916. }
  3917. static void free_kvm_area(void)
  3918. {
  3919. int cpu;
  3920. for_each_possible_cpu(cpu) {
  3921. free_vmcs(per_cpu(vmxarea, cpu));
  3922. per_cpu(vmxarea, cpu) = NULL;
  3923. }
  3924. }
  3925. enum vmcs_field_width {
  3926. VMCS_FIELD_WIDTH_U16 = 0,
  3927. VMCS_FIELD_WIDTH_U64 = 1,
  3928. VMCS_FIELD_WIDTH_U32 = 2,
  3929. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  3930. };
  3931. static inline int vmcs_field_width(unsigned long field)
  3932. {
  3933. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  3934. return VMCS_FIELD_WIDTH_U32;
  3935. return (field >> 13) & 0x3 ;
  3936. }
  3937. static inline int vmcs_field_readonly(unsigned long field)
  3938. {
  3939. return (((field >> 10) & 0x3) == 1);
  3940. }
  3941. static void init_vmcs_shadow_fields(void)
  3942. {
  3943. int i, j;
  3944. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  3945. u16 field = shadow_read_only_fields[i];
  3946. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3947. (i + 1 == max_shadow_read_only_fields ||
  3948. shadow_read_only_fields[i + 1] != field + 1))
  3949. pr_err("Missing field from shadow_read_only_field %x\n",
  3950. field + 1);
  3951. clear_bit(field, vmx_vmread_bitmap);
  3952. #ifdef CONFIG_X86_64
  3953. if (field & 1)
  3954. continue;
  3955. #endif
  3956. if (j < i)
  3957. shadow_read_only_fields[j] = field;
  3958. j++;
  3959. }
  3960. max_shadow_read_only_fields = j;
  3961. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  3962. u16 field = shadow_read_write_fields[i];
  3963. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3964. (i + 1 == max_shadow_read_write_fields ||
  3965. shadow_read_write_fields[i + 1] != field + 1))
  3966. pr_err("Missing field from shadow_read_write_field %x\n",
  3967. field + 1);
  3968. /*
  3969. * PML and the preemption timer can be emulated, but the
  3970. * processor cannot vmwrite to fields that don't exist
  3971. * on bare metal.
  3972. */
  3973. switch (field) {
  3974. case GUEST_PML_INDEX:
  3975. if (!cpu_has_vmx_pml())
  3976. continue;
  3977. break;
  3978. case VMX_PREEMPTION_TIMER_VALUE:
  3979. if (!cpu_has_vmx_preemption_timer())
  3980. continue;
  3981. break;
  3982. case GUEST_INTR_STATUS:
  3983. if (!cpu_has_vmx_apicv())
  3984. continue;
  3985. break;
  3986. default:
  3987. break;
  3988. }
  3989. clear_bit(field, vmx_vmwrite_bitmap);
  3990. clear_bit(field, vmx_vmread_bitmap);
  3991. #ifdef CONFIG_X86_64
  3992. if (field & 1)
  3993. continue;
  3994. #endif
  3995. if (j < i)
  3996. shadow_read_write_fields[j] = field;
  3997. j++;
  3998. }
  3999. max_shadow_read_write_fields = j;
  4000. }
  4001. static __init int alloc_kvm_area(void)
  4002. {
  4003. int cpu;
  4004. for_each_possible_cpu(cpu) {
  4005. struct vmcs *vmcs;
  4006. vmcs = alloc_vmcs_cpu(false, cpu);
  4007. if (!vmcs) {
  4008. free_kvm_area();
  4009. return -ENOMEM;
  4010. }
  4011. /*
  4012. * When eVMCS is enabled, alloc_vmcs_cpu() sets
  4013. * vmcs->revision_id to KVM_EVMCS_VERSION instead of
  4014. * revision_id reported by MSR_IA32_VMX_BASIC.
  4015. *
  4016. * However, even though not explictly documented by
  4017. * TLFS, VMXArea passed as VMXON argument should
  4018. * still be marked with revision_id reported by
  4019. * physical CPU.
  4020. */
  4021. if (static_branch_unlikely(&enable_evmcs))
  4022. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4023. per_cpu(vmxarea, cpu) = vmcs;
  4024. }
  4025. return 0;
  4026. }
  4027. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  4028. struct kvm_segment *save)
  4029. {
  4030. if (!emulate_invalid_guest_state) {
  4031. /*
  4032. * CS and SS RPL should be equal during guest entry according
  4033. * to VMX spec, but in reality it is not always so. Since vcpu
  4034. * is in the middle of the transition from real mode to
  4035. * protected mode it is safe to assume that RPL 0 is a good
  4036. * default value.
  4037. */
  4038. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  4039. save->selector &= ~SEGMENT_RPL_MASK;
  4040. save->dpl = save->selector & SEGMENT_RPL_MASK;
  4041. save->s = 1;
  4042. }
  4043. vmx_set_segment(vcpu, save, seg);
  4044. }
  4045. static void enter_pmode(struct kvm_vcpu *vcpu)
  4046. {
  4047. unsigned long flags;
  4048. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4049. /*
  4050. * Update real mode segment cache. It may be not up-to-date if sement
  4051. * register was written while vcpu was in a guest mode.
  4052. */
  4053. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4054. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4055. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4056. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4057. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4058. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4059. vmx->rmode.vm86_active = 0;
  4060. vmx_segment_cache_clear(vmx);
  4061. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4062. flags = vmcs_readl(GUEST_RFLAGS);
  4063. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  4064. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  4065. vmcs_writel(GUEST_RFLAGS, flags);
  4066. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  4067. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  4068. update_exception_bitmap(vcpu);
  4069. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4070. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4071. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4072. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4073. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4074. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4075. }
  4076. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  4077. {
  4078. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4079. struct kvm_segment var = *save;
  4080. var.dpl = 0x3;
  4081. if (seg == VCPU_SREG_CS)
  4082. var.type = 0x3;
  4083. if (!emulate_invalid_guest_state) {
  4084. var.selector = var.base >> 4;
  4085. var.base = var.base & 0xffff0;
  4086. var.limit = 0xffff;
  4087. var.g = 0;
  4088. var.db = 0;
  4089. var.present = 1;
  4090. var.s = 1;
  4091. var.l = 0;
  4092. var.unusable = 0;
  4093. var.type = 0x3;
  4094. var.avl = 0;
  4095. if (save->base & 0xf)
  4096. printk_once(KERN_WARNING "kvm: segment base is not "
  4097. "paragraph aligned when entering "
  4098. "protected mode (seg=%d)", seg);
  4099. }
  4100. vmcs_write16(sf->selector, var.selector);
  4101. vmcs_writel(sf->base, var.base);
  4102. vmcs_write32(sf->limit, var.limit);
  4103. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  4104. }
  4105. static void enter_rmode(struct kvm_vcpu *vcpu)
  4106. {
  4107. unsigned long flags;
  4108. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4109. struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
  4110. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4111. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4112. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4113. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4114. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4115. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4116. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4117. vmx->rmode.vm86_active = 1;
  4118. /*
  4119. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  4120. * vcpu. Warn the user that an update is overdue.
  4121. */
  4122. if (!kvm_vmx->tss_addr)
  4123. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  4124. "called before entering vcpu\n");
  4125. vmx_segment_cache_clear(vmx);
  4126. vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
  4127. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  4128. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4129. flags = vmcs_readl(GUEST_RFLAGS);
  4130. vmx->rmode.save_rflags = flags;
  4131. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  4132. vmcs_writel(GUEST_RFLAGS, flags);
  4133. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  4134. update_exception_bitmap(vcpu);
  4135. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4136. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4137. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4138. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4139. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4140. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4141. kvm_mmu_reset_context(vcpu);
  4142. }
  4143. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  4144. {
  4145. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4146. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  4147. if (!msr)
  4148. return;
  4149. /*
  4150. * Force kernel_gs_base reloading before EFER changes, as control
  4151. * of this msr depends on is_long_mode().
  4152. */
  4153. vmx_load_host_state(to_vmx(vcpu));
  4154. vcpu->arch.efer = efer;
  4155. if (efer & EFER_LMA) {
  4156. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4157. msr->data = efer;
  4158. } else {
  4159. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4160. msr->data = efer & ~EFER_LME;
  4161. }
  4162. setup_msrs(vmx);
  4163. }
  4164. #ifdef CONFIG_X86_64
  4165. static void enter_lmode(struct kvm_vcpu *vcpu)
  4166. {
  4167. u32 guest_tr_ar;
  4168. vmx_segment_cache_clear(to_vmx(vcpu));
  4169. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  4170. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  4171. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  4172. __func__);
  4173. vmcs_write32(GUEST_TR_AR_BYTES,
  4174. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  4175. | VMX_AR_TYPE_BUSY_64_TSS);
  4176. }
  4177. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  4178. }
  4179. static void exit_lmode(struct kvm_vcpu *vcpu)
  4180. {
  4181. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4182. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  4183. }
  4184. #endif
  4185. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  4186. bool invalidate_gpa)
  4187. {
  4188. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  4189. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  4190. return;
  4191. ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
  4192. } else {
  4193. vpid_sync_context(vpid);
  4194. }
  4195. }
  4196. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4197. {
  4198. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  4199. }
  4200. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  4201. {
  4202. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  4203. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  4204. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  4205. }
  4206. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  4207. {
  4208. if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
  4209. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  4210. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4211. }
  4212. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  4213. {
  4214. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  4215. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  4216. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  4217. }
  4218. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  4219. {
  4220. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4221. if (!test_bit(VCPU_EXREG_PDPTR,
  4222. (unsigned long *)&vcpu->arch.regs_dirty))
  4223. return;
  4224. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4225. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  4226. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  4227. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  4228. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  4229. }
  4230. }
  4231. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  4232. {
  4233. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4234. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4235. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  4236. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  4237. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  4238. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  4239. }
  4240. __set_bit(VCPU_EXREG_PDPTR,
  4241. (unsigned long *)&vcpu->arch.regs_avail);
  4242. __set_bit(VCPU_EXREG_PDPTR,
  4243. (unsigned long *)&vcpu->arch.regs_dirty);
  4244. }
  4245. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4246. {
  4247. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4248. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4249. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4250. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  4251. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4252. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4253. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  4254. return fixed_bits_valid(val, fixed0, fixed1);
  4255. }
  4256. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4257. {
  4258. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4259. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4260. return fixed_bits_valid(val, fixed0, fixed1);
  4261. }
  4262. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4263. {
  4264. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  4265. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  4266. return fixed_bits_valid(val, fixed0, fixed1);
  4267. }
  4268. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  4269. #define nested_guest_cr4_valid nested_cr4_valid
  4270. #define nested_host_cr4_valid nested_cr4_valid
  4271. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  4272. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  4273. unsigned long cr0,
  4274. struct kvm_vcpu *vcpu)
  4275. {
  4276. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  4277. vmx_decache_cr3(vcpu);
  4278. if (!(cr0 & X86_CR0_PG)) {
  4279. /* From paging/starting to nonpaging */
  4280. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4281. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  4282. (CPU_BASED_CR3_LOAD_EXITING |
  4283. CPU_BASED_CR3_STORE_EXITING));
  4284. vcpu->arch.cr0 = cr0;
  4285. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4286. } else if (!is_paging(vcpu)) {
  4287. /* From nonpaging to paging */
  4288. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4289. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  4290. ~(CPU_BASED_CR3_LOAD_EXITING |
  4291. CPU_BASED_CR3_STORE_EXITING));
  4292. vcpu->arch.cr0 = cr0;
  4293. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4294. }
  4295. if (!(cr0 & X86_CR0_WP))
  4296. *hw_cr0 &= ~X86_CR0_WP;
  4297. }
  4298. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  4299. {
  4300. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4301. unsigned long hw_cr0;
  4302. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  4303. if (enable_unrestricted_guest)
  4304. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  4305. else {
  4306. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  4307. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  4308. enter_pmode(vcpu);
  4309. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  4310. enter_rmode(vcpu);
  4311. }
  4312. #ifdef CONFIG_X86_64
  4313. if (vcpu->arch.efer & EFER_LME) {
  4314. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  4315. enter_lmode(vcpu);
  4316. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  4317. exit_lmode(vcpu);
  4318. }
  4319. #endif
  4320. if (enable_ept && !enable_unrestricted_guest)
  4321. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  4322. vmcs_writel(CR0_READ_SHADOW, cr0);
  4323. vmcs_writel(GUEST_CR0, hw_cr0);
  4324. vcpu->arch.cr0 = cr0;
  4325. /* depends on vcpu->arch.cr0 to be set to a new value */
  4326. vmx->emulation_required = emulation_required(vcpu);
  4327. }
  4328. static int get_ept_level(struct kvm_vcpu *vcpu)
  4329. {
  4330. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  4331. return 5;
  4332. return 4;
  4333. }
  4334. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  4335. {
  4336. u64 eptp = VMX_EPTP_MT_WB;
  4337. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  4338. if (enable_ept_ad_bits &&
  4339. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  4340. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  4341. eptp |= (root_hpa & PAGE_MASK);
  4342. return eptp;
  4343. }
  4344. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  4345. {
  4346. unsigned long guest_cr3;
  4347. u64 eptp;
  4348. guest_cr3 = cr3;
  4349. if (enable_ept) {
  4350. eptp = construct_eptp(vcpu, cr3);
  4351. vmcs_write64(EPT_POINTER, eptp);
  4352. if (enable_unrestricted_guest || is_paging(vcpu) ||
  4353. is_guest_mode(vcpu))
  4354. guest_cr3 = kvm_read_cr3(vcpu);
  4355. else
  4356. guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
  4357. ept_load_pdptrs(vcpu);
  4358. }
  4359. vmx_flush_tlb(vcpu, true);
  4360. vmcs_writel(GUEST_CR3, guest_cr3);
  4361. }
  4362. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  4363. {
  4364. /*
  4365. * Pass through host's Machine Check Enable value to hw_cr4, which
  4366. * is in force while we are in guest mode. Do not let guests control
  4367. * this bit, even if host CR4.MCE == 0.
  4368. */
  4369. unsigned long hw_cr4;
  4370. hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
  4371. if (enable_unrestricted_guest)
  4372. hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
  4373. else if (to_vmx(vcpu)->rmode.vm86_active)
  4374. hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
  4375. else
  4376. hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
  4377. if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
  4378. if (cr4 & X86_CR4_UMIP) {
  4379. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4380. SECONDARY_EXEC_DESC);
  4381. hw_cr4 &= ~X86_CR4_UMIP;
  4382. } else if (!is_guest_mode(vcpu) ||
  4383. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  4384. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4385. SECONDARY_EXEC_DESC);
  4386. }
  4387. if (cr4 & X86_CR4_VMXE) {
  4388. /*
  4389. * To use VMXON (and later other VMX instructions), a guest
  4390. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  4391. * So basically the check on whether to allow nested VMX
  4392. * is here.
  4393. */
  4394. if (!nested_vmx_allowed(vcpu))
  4395. return 1;
  4396. }
  4397. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  4398. return 1;
  4399. vcpu->arch.cr4 = cr4;
  4400. if (!enable_unrestricted_guest) {
  4401. if (enable_ept) {
  4402. if (!is_paging(vcpu)) {
  4403. hw_cr4 &= ~X86_CR4_PAE;
  4404. hw_cr4 |= X86_CR4_PSE;
  4405. } else if (!(cr4 & X86_CR4_PAE)) {
  4406. hw_cr4 &= ~X86_CR4_PAE;
  4407. }
  4408. }
  4409. /*
  4410. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  4411. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  4412. * to be manually disabled when guest switches to non-paging
  4413. * mode.
  4414. *
  4415. * If !enable_unrestricted_guest, the CPU is always running
  4416. * with CR0.PG=1 and CR4 needs to be modified.
  4417. * If enable_unrestricted_guest, the CPU automatically
  4418. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  4419. */
  4420. if (!is_paging(vcpu))
  4421. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  4422. }
  4423. vmcs_writel(CR4_READ_SHADOW, cr4);
  4424. vmcs_writel(GUEST_CR4, hw_cr4);
  4425. return 0;
  4426. }
  4427. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  4428. struct kvm_segment *var, int seg)
  4429. {
  4430. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4431. u32 ar;
  4432. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4433. *var = vmx->rmode.segs[seg];
  4434. if (seg == VCPU_SREG_TR
  4435. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  4436. return;
  4437. var->base = vmx_read_guest_seg_base(vmx, seg);
  4438. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4439. return;
  4440. }
  4441. var->base = vmx_read_guest_seg_base(vmx, seg);
  4442. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  4443. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4444. ar = vmx_read_guest_seg_ar(vmx, seg);
  4445. var->unusable = (ar >> 16) & 1;
  4446. var->type = ar & 15;
  4447. var->s = (ar >> 4) & 1;
  4448. var->dpl = (ar >> 5) & 3;
  4449. /*
  4450. * Some userspaces do not preserve unusable property. Since usable
  4451. * segment has to be present according to VMX spec we can use present
  4452. * property to amend userspace bug by making unusable segment always
  4453. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  4454. * segment as unusable.
  4455. */
  4456. var->present = !var->unusable;
  4457. var->avl = (ar >> 12) & 1;
  4458. var->l = (ar >> 13) & 1;
  4459. var->db = (ar >> 14) & 1;
  4460. var->g = (ar >> 15) & 1;
  4461. }
  4462. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4463. {
  4464. struct kvm_segment s;
  4465. if (to_vmx(vcpu)->rmode.vm86_active) {
  4466. vmx_get_segment(vcpu, &s, seg);
  4467. return s.base;
  4468. }
  4469. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  4470. }
  4471. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  4472. {
  4473. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4474. if (unlikely(vmx->rmode.vm86_active))
  4475. return 0;
  4476. else {
  4477. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  4478. return VMX_AR_DPL(ar);
  4479. }
  4480. }
  4481. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  4482. {
  4483. u32 ar;
  4484. if (var->unusable || !var->present)
  4485. ar = 1 << 16;
  4486. else {
  4487. ar = var->type & 15;
  4488. ar |= (var->s & 1) << 4;
  4489. ar |= (var->dpl & 3) << 5;
  4490. ar |= (var->present & 1) << 7;
  4491. ar |= (var->avl & 1) << 12;
  4492. ar |= (var->l & 1) << 13;
  4493. ar |= (var->db & 1) << 14;
  4494. ar |= (var->g & 1) << 15;
  4495. }
  4496. return ar;
  4497. }
  4498. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4499. struct kvm_segment *var, int seg)
  4500. {
  4501. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4502. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4503. vmx_segment_cache_clear(vmx);
  4504. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4505. vmx->rmode.segs[seg] = *var;
  4506. if (seg == VCPU_SREG_TR)
  4507. vmcs_write16(sf->selector, var->selector);
  4508. else if (var->s)
  4509. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4510. goto out;
  4511. }
  4512. vmcs_writel(sf->base, var->base);
  4513. vmcs_write32(sf->limit, var->limit);
  4514. vmcs_write16(sf->selector, var->selector);
  4515. /*
  4516. * Fix the "Accessed" bit in AR field of segment registers for older
  4517. * qemu binaries.
  4518. * IA32 arch specifies that at the time of processor reset the
  4519. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4520. * is setting it to 0 in the userland code. This causes invalid guest
  4521. * state vmexit when "unrestricted guest" mode is turned on.
  4522. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4523. * tree. Newer qemu binaries with that qemu fix would not need this
  4524. * kvm hack.
  4525. */
  4526. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4527. var->type |= 0x1; /* Accessed */
  4528. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4529. out:
  4530. vmx->emulation_required = emulation_required(vcpu);
  4531. }
  4532. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4533. {
  4534. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4535. *db = (ar >> 14) & 1;
  4536. *l = (ar >> 13) & 1;
  4537. }
  4538. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4539. {
  4540. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4541. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4542. }
  4543. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4544. {
  4545. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4546. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4547. }
  4548. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4549. {
  4550. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4551. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4552. }
  4553. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4554. {
  4555. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4556. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4557. }
  4558. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4559. {
  4560. struct kvm_segment var;
  4561. u32 ar;
  4562. vmx_get_segment(vcpu, &var, seg);
  4563. var.dpl = 0x3;
  4564. if (seg == VCPU_SREG_CS)
  4565. var.type = 0x3;
  4566. ar = vmx_segment_access_rights(&var);
  4567. if (var.base != (var.selector << 4))
  4568. return false;
  4569. if (var.limit != 0xffff)
  4570. return false;
  4571. if (ar != 0xf3)
  4572. return false;
  4573. return true;
  4574. }
  4575. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4576. {
  4577. struct kvm_segment cs;
  4578. unsigned int cs_rpl;
  4579. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4580. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4581. if (cs.unusable)
  4582. return false;
  4583. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4584. return false;
  4585. if (!cs.s)
  4586. return false;
  4587. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4588. if (cs.dpl > cs_rpl)
  4589. return false;
  4590. } else {
  4591. if (cs.dpl != cs_rpl)
  4592. return false;
  4593. }
  4594. if (!cs.present)
  4595. return false;
  4596. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4597. return true;
  4598. }
  4599. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4600. {
  4601. struct kvm_segment ss;
  4602. unsigned int ss_rpl;
  4603. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4604. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4605. if (ss.unusable)
  4606. return true;
  4607. if (ss.type != 3 && ss.type != 7)
  4608. return false;
  4609. if (!ss.s)
  4610. return false;
  4611. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4612. return false;
  4613. if (!ss.present)
  4614. return false;
  4615. return true;
  4616. }
  4617. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4618. {
  4619. struct kvm_segment var;
  4620. unsigned int rpl;
  4621. vmx_get_segment(vcpu, &var, seg);
  4622. rpl = var.selector & SEGMENT_RPL_MASK;
  4623. if (var.unusable)
  4624. return true;
  4625. if (!var.s)
  4626. return false;
  4627. if (!var.present)
  4628. return false;
  4629. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4630. if (var.dpl < rpl) /* DPL < RPL */
  4631. return false;
  4632. }
  4633. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4634. * rights flags
  4635. */
  4636. return true;
  4637. }
  4638. static bool tr_valid(struct kvm_vcpu *vcpu)
  4639. {
  4640. struct kvm_segment tr;
  4641. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4642. if (tr.unusable)
  4643. return false;
  4644. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4645. return false;
  4646. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4647. return false;
  4648. if (!tr.present)
  4649. return false;
  4650. return true;
  4651. }
  4652. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4653. {
  4654. struct kvm_segment ldtr;
  4655. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4656. if (ldtr.unusable)
  4657. return true;
  4658. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4659. return false;
  4660. if (ldtr.type != 2)
  4661. return false;
  4662. if (!ldtr.present)
  4663. return false;
  4664. return true;
  4665. }
  4666. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4667. {
  4668. struct kvm_segment cs, ss;
  4669. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4670. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4671. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4672. (ss.selector & SEGMENT_RPL_MASK));
  4673. }
  4674. /*
  4675. * Check if guest state is valid. Returns true if valid, false if
  4676. * not.
  4677. * We assume that registers are always usable
  4678. */
  4679. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4680. {
  4681. if (enable_unrestricted_guest)
  4682. return true;
  4683. /* real mode guest state checks */
  4684. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4685. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4686. return false;
  4687. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4688. return false;
  4689. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4690. return false;
  4691. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4692. return false;
  4693. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4694. return false;
  4695. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4696. return false;
  4697. } else {
  4698. /* protected mode guest state checks */
  4699. if (!cs_ss_rpl_check(vcpu))
  4700. return false;
  4701. if (!code_segment_valid(vcpu))
  4702. return false;
  4703. if (!stack_segment_valid(vcpu))
  4704. return false;
  4705. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4706. return false;
  4707. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4708. return false;
  4709. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4710. return false;
  4711. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4712. return false;
  4713. if (!tr_valid(vcpu))
  4714. return false;
  4715. if (!ldtr_valid(vcpu))
  4716. return false;
  4717. }
  4718. /* TODO:
  4719. * - Add checks on RIP
  4720. * - Add checks on RFLAGS
  4721. */
  4722. return true;
  4723. }
  4724. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4725. {
  4726. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4727. }
  4728. static int init_rmode_tss(struct kvm *kvm)
  4729. {
  4730. gfn_t fn;
  4731. u16 data = 0;
  4732. int idx, r;
  4733. idx = srcu_read_lock(&kvm->srcu);
  4734. fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
  4735. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4736. if (r < 0)
  4737. goto out;
  4738. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  4739. r = kvm_write_guest_page(kvm, fn++, &data,
  4740. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  4741. if (r < 0)
  4742. goto out;
  4743. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  4744. if (r < 0)
  4745. goto out;
  4746. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4747. if (r < 0)
  4748. goto out;
  4749. data = ~0;
  4750. r = kvm_write_guest_page(kvm, fn, &data,
  4751. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  4752. sizeof(u8));
  4753. out:
  4754. srcu_read_unlock(&kvm->srcu, idx);
  4755. return r;
  4756. }
  4757. static int init_rmode_identity_map(struct kvm *kvm)
  4758. {
  4759. struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
  4760. int i, idx, r = 0;
  4761. kvm_pfn_t identity_map_pfn;
  4762. u32 tmp;
  4763. /* Protect kvm_vmx->ept_identity_pagetable_done. */
  4764. mutex_lock(&kvm->slots_lock);
  4765. if (likely(kvm_vmx->ept_identity_pagetable_done))
  4766. goto out2;
  4767. if (!kvm_vmx->ept_identity_map_addr)
  4768. kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  4769. identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
  4770. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  4771. kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
  4772. if (r < 0)
  4773. goto out2;
  4774. idx = srcu_read_lock(&kvm->srcu);
  4775. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  4776. if (r < 0)
  4777. goto out;
  4778. /* Set up identity-mapping pagetable for EPT in real mode */
  4779. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  4780. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  4781. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  4782. r = kvm_write_guest_page(kvm, identity_map_pfn,
  4783. &tmp, i * sizeof(tmp), sizeof(tmp));
  4784. if (r < 0)
  4785. goto out;
  4786. }
  4787. kvm_vmx->ept_identity_pagetable_done = true;
  4788. out:
  4789. srcu_read_unlock(&kvm->srcu, idx);
  4790. out2:
  4791. mutex_unlock(&kvm->slots_lock);
  4792. return r;
  4793. }
  4794. static void seg_setup(int seg)
  4795. {
  4796. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4797. unsigned int ar;
  4798. vmcs_write16(sf->selector, 0);
  4799. vmcs_writel(sf->base, 0);
  4800. vmcs_write32(sf->limit, 0xffff);
  4801. ar = 0x93;
  4802. if (seg == VCPU_SREG_CS)
  4803. ar |= 0x08; /* code segment */
  4804. vmcs_write32(sf->ar_bytes, ar);
  4805. }
  4806. static int alloc_apic_access_page(struct kvm *kvm)
  4807. {
  4808. struct page *page;
  4809. int r = 0;
  4810. mutex_lock(&kvm->slots_lock);
  4811. if (kvm->arch.apic_access_page_done)
  4812. goto out;
  4813. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  4814. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  4815. if (r)
  4816. goto out;
  4817. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  4818. if (is_error_page(page)) {
  4819. r = -EFAULT;
  4820. goto out;
  4821. }
  4822. /*
  4823. * Do not pin the page in memory, so that memory hot-unplug
  4824. * is able to migrate it.
  4825. */
  4826. put_page(page);
  4827. kvm->arch.apic_access_page_done = true;
  4828. out:
  4829. mutex_unlock(&kvm->slots_lock);
  4830. return r;
  4831. }
  4832. static int allocate_vpid(void)
  4833. {
  4834. int vpid;
  4835. if (!enable_vpid)
  4836. return 0;
  4837. spin_lock(&vmx_vpid_lock);
  4838. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  4839. if (vpid < VMX_NR_VPIDS)
  4840. __set_bit(vpid, vmx_vpid_bitmap);
  4841. else
  4842. vpid = 0;
  4843. spin_unlock(&vmx_vpid_lock);
  4844. return vpid;
  4845. }
  4846. static void free_vpid(int vpid)
  4847. {
  4848. if (!enable_vpid || vpid == 0)
  4849. return;
  4850. spin_lock(&vmx_vpid_lock);
  4851. __clear_bit(vpid, vmx_vpid_bitmap);
  4852. spin_unlock(&vmx_vpid_lock);
  4853. }
  4854. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  4855. u32 msr, int type)
  4856. {
  4857. int f = sizeof(unsigned long);
  4858. if (!cpu_has_vmx_msr_bitmap())
  4859. return;
  4860. if (static_branch_unlikely(&enable_evmcs))
  4861. evmcs_touch_msr_bitmap();
  4862. /*
  4863. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4864. * have the write-low and read-high bitmap offsets the wrong way round.
  4865. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4866. */
  4867. if (msr <= 0x1fff) {
  4868. if (type & MSR_TYPE_R)
  4869. /* read-low */
  4870. __clear_bit(msr, msr_bitmap + 0x000 / f);
  4871. if (type & MSR_TYPE_W)
  4872. /* write-low */
  4873. __clear_bit(msr, msr_bitmap + 0x800 / f);
  4874. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4875. msr &= 0x1fff;
  4876. if (type & MSR_TYPE_R)
  4877. /* read-high */
  4878. __clear_bit(msr, msr_bitmap + 0x400 / f);
  4879. if (type & MSR_TYPE_W)
  4880. /* write-high */
  4881. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  4882. }
  4883. }
  4884. static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  4885. u32 msr, int type)
  4886. {
  4887. int f = sizeof(unsigned long);
  4888. if (!cpu_has_vmx_msr_bitmap())
  4889. return;
  4890. if (static_branch_unlikely(&enable_evmcs))
  4891. evmcs_touch_msr_bitmap();
  4892. /*
  4893. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4894. * have the write-low and read-high bitmap offsets the wrong way round.
  4895. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4896. */
  4897. if (msr <= 0x1fff) {
  4898. if (type & MSR_TYPE_R)
  4899. /* read-low */
  4900. __set_bit(msr, msr_bitmap + 0x000 / f);
  4901. if (type & MSR_TYPE_W)
  4902. /* write-low */
  4903. __set_bit(msr, msr_bitmap + 0x800 / f);
  4904. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4905. msr &= 0x1fff;
  4906. if (type & MSR_TYPE_R)
  4907. /* read-high */
  4908. __set_bit(msr, msr_bitmap + 0x400 / f);
  4909. if (type & MSR_TYPE_W)
  4910. /* write-high */
  4911. __set_bit(msr, msr_bitmap + 0xc00 / f);
  4912. }
  4913. }
  4914. static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  4915. u32 msr, int type, bool value)
  4916. {
  4917. if (value)
  4918. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  4919. else
  4920. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  4921. }
  4922. /*
  4923. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  4924. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  4925. */
  4926. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  4927. unsigned long *msr_bitmap_nested,
  4928. u32 msr, int type)
  4929. {
  4930. int f = sizeof(unsigned long);
  4931. /*
  4932. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4933. * have the write-low and read-high bitmap offsets the wrong way round.
  4934. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4935. */
  4936. if (msr <= 0x1fff) {
  4937. if (type & MSR_TYPE_R &&
  4938. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  4939. /* read-low */
  4940. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  4941. if (type & MSR_TYPE_W &&
  4942. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  4943. /* write-low */
  4944. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  4945. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4946. msr &= 0x1fff;
  4947. if (type & MSR_TYPE_R &&
  4948. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  4949. /* read-high */
  4950. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  4951. if (type & MSR_TYPE_W &&
  4952. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  4953. /* write-high */
  4954. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  4955. }
  4956. }
  4957. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  4958. {
  4959. u8 mode = 0;
  4960. if (cpu_has_secondary_exec_ctrls() &&
  4961. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  4962. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  4963. mode |= MSR_BITMAP_MODE_X2APIC;
  4964. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  4965. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  4966. }
  4967. if (is_long_mode(vcpu))
  4968. mode |= MSR_BITMAP_MODE_LM;
  4969. return mode;
  4970. }
  4971. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  4972. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  4973. u8 mode)
  4974. {
  4975. int msr;
  4976. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  4977. unsigned word = msr / BITS_PER_LONG;
  4978. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  4979. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  4980. }
  4981. if (mode & MSR_BITMAP_MODE_X2APIC) {
  4982. /*
  4983. * TPR reads and writes can be virtualized even if virtual interrupt
  4984. * delivery is not in use.
  4985. */
  4986. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  4987. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  4988. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  4989. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  4990. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  4991. }
  4992. }
  4993. }
  4994. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  4995. {
  4996. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4997. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  4998. u8 mode = vmx_msr_bitmap_mode(vcpu);
  4999. u8 changed = mode ^ vmx->msr_bitmap_mode;
  5000. if (!changed)
  5001. return;
  5002. vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
  5003. !(mode & MSR_BITMAP_MODE_LM));
  5004. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  5005. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  5006. vmx->msr_bitmap_mode = mode;
  5007. }
  5008. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  5009. {
  5010. return enable_apicv;
  5011. }
  5012. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  5013. {
  5014. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5015. gfn_t gfn;
  5016. /*
  5017. * Don't need to mark the APIC access page dirty; it is never
  5018. * written to by the CPU during APIC virtualization.
  5019. */
  5020. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  5021. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  5022. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5023. }
  5024. if (nested_cpu_has_posted_intr(vmcs12)) {
  5025. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  5026. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5027. }
  5028. }
  5029. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  5030. {
  5031. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5032. int max_irr;
  5033. void *vapic_page;
  5034. u16 status;
  5035. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  5036. return;
  5037. vmx->nested.pi_pending = false;
  5038. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  5039. return;
  5040. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  5041. if (max_irr != 256) {
  5042. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5043. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  5044. vapic_page, &max_irr);
  5045. kunmap(vmx->nested.virtual_apic_page);
  5046. status = vmcs_read16(GUEST_INTR_STATUS);
  5047. if ((u8)max_irr > ((u8)status & 0xff)) {
  5048. status &= ~0xff;
  5049. status |= (u8)max_irr;
  5050. vmcs_write16(GUEST_INTR_STATUS, status);
  5051. }
  5052. }
  5053. nested_mark_vmcs12_pages_dirty(vcpu);
  5054. }
  5055. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  5056. bool nested)
  5057. {
  5058. #ifdef CONFIG_SMP
  5059. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  5060. if (vcpu->mode == IN_GUEST_MODE) {
  5061. /*
  5062. * The vector of interrupt to be delivered to vcpu had
  5063. * been set in PIR before this function.
  5064. *
  5065. * Following cases will be reached in this block, and
  5066. * we always send a notification event in all cases as
  5067. * explained below.
  5068. *
  5069. * Case 1: vcpu keeps in non-root mode. Sending a
  5070. * notification event posts the interrupt to vcpu.
  5071. *
  5072. * Case 2: vcpu exits to root mode and is still
  5073. * runnable. PIR will be synced to vIRR before the
  5074. * next vcpu entry. Sending a notification event in
  5075. * this case has no effect, as vcpu is not in root
  5076. * mode.
  5077. *
  5078. * Case 3: vcpu exits to root mode and is blocked.
  5079. * vcpu_block() has already synced PIR to vIRR and
  5080. * never blocks vcpu if vIRR is not cleared. Therefore,
  5081. * a blocked vcpu here does not wait for any requested
  5082. * interrupts in PIR, and sending a notification event
  5083. * which has no effect is safe here.
  5084. */
  5085. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  5086. return true;
  5087. }
  5088. #endif
  5089. return false;
  5090. }
  5091. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  5092. int vector)
  5093. {
  5094. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5095. if (is_guest_mode(vcpu) &&
  5096. vector == vmx->nested.posted_intr_nv) {
  5097. /*
  5098. * If a posted intr is not recognized by hardware,
  5099. * we will accomplish it in the next vmentry.
  5100. */
  5101. vmx->nested.pi_pending = true;
  5102. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5103. /* the PIR and ON have been set by L1. */
  5104. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  5105. kvm_vcpu_kick(vcpu);
  5106. return 0;
  5107. }
  5108. return -1;
  5109. }
  5110. /*
  5111. * Send interrupt to vcpu via posted interrupt way.
  5112. * 1. If target vcpu is running(non-root mode), send posted interrupt
  5113. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  5114. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  5115. * interrupt from PIR in next vmentry.
  5116. */
  5117. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  5118. {
  5119. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5120. int r;
  5121. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  5122. if (!r)
  5123. return;
  5124. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  5125. return;
  5126. /* If a previous notification has sent the IPI, nothing to do. */
  5127. if (pi_test_and_set_on(&vmx->pi_desc))
  5128. return;
  5129. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  5130. kvm_vcpu_kick(vcpu);
  5131. }
  5132. /*
  5133. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  5134. * will not change in the lifetime of the guest.
  5135. * Note that host-state that does change is set elsewhere. E.g., host-state
  5136. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  5137. */
  5138. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  5139. {
  5140. u32 low32, high32;
  5141. unsigned long tmpl;
  5142. struct desc_ptr dt;
  5143. unsigned long cr0, cr3, cr4;
  5144. cr0 = read_cr0();
  5145. WARN_ON(cr0 & X86_CR0_TS);
  5146. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  5147. /*
  5148. * Save the most likely value for this task's CR3 in the VMCS.
  5149. * We can't use __get_current_cr3_fast() because we're not atomic.
  5150. */
  5151. cr3 = __read_cr3();
  5152. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  5153. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  5154. /* Save the most likely value for this task's CR4 in the VMCS. */
  5155. cr4 = cr4_read_shadow();
  5156. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  5157. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  5158. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  5159. #ifdef CONFIG_X86_64
  5160. /*
  5161. * Load null selectors, so we can avoid reloading them in
  5162. * __vmx_load_host_state(), in case userspace uses the null selectors
  5163. * too (the expected case).
  5164. */
  5165. vmcs_write16(HOST_DS_SELECTOR, 0);
  5166. vmcs_write16(HOST_ES_SELECTOR, 0);
  5167. #else
  5168. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5169. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5170. #endif
  5171. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5172. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  5173. store_idt(&dt);
  5174. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  5175. vmx->host_idt_base = dt.address;
  5176. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  5177. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  5178. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  5179. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  5180. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  5181. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  5182. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  5183. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  5184. }
  5185. }
  5186. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  5187. {
  5188. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  5189. if (enable_ept)
  5190. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  5191. if (is_guest_mode(&vmx->vcpu))
  5192. vmx->vcpu.arch.cr4_guest_owned_bits &=
  5193. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  5194. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  5195. }
  5196. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  5197. {
  5198. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  5199. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  5200. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  5201. if (!enable_vnmi)
  5202. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  5203. /* Enable the preemption timer dynamically */
  5204. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  5205. return pin_based_exec_ctrl;
  5206. }
  5207. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  5208. {
  5209. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5210. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5211. if (cpu_has_secondary_exec_ctrls()) {
  5212. if (kvm_vcpu_apicv_active(vcpu))
  5213. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  5214. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5215. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5216. else
  5217. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  5218. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5219. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5220. }
  5221. if (cpu_has_vmx_msr_bitmap())
  5222. vmx_update_msr_bitmap(vcpu);
  5223. }
  5224. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  5225. {
  5226. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  5227. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  5228. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  5229. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  5230. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5231. #ifdef CONFIG_X86_64
  5232. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  5233. CPU_BASED_CR8_LOAD_EXITING;
  5234. #endif
  5235. }
  5236. if (!enable_ept)
  5237. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  5238. CPU_BASED_CR3_LOAD_EXITING |
  5239. CPU_BASED_INVLPG_EXITING;
  5240. if (kvm_mwait_in_guest(vmx->vcpu.kvm))
  5241. exec_control &= ~(CPU_BASED_MWAIT_EXITING |
  5242. CPU_BASED_MONITOR_EXITING);
  5243. if (kvm_hlt_in_guest(vmx->vcpu.kvm))
  5244. exec_control &= ~CPU_BASED_HLT_EXITING;
  5245. return exec_control;
  5246. }
  5247. static bool vmx_rdrand_supported(void)
  5248. {
  5249. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5250. SECONDARY_EXEC_RDRAND_EXITING;
  5251. }
  5252. static bool vmx_rdseed_supported(void)
  5253. {
  5254. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5255. SECONDARY_EXEC_RDSEED_EXITING;
  5256. }
  5257. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  5258. {
  5259. struct kvm_vcpu *vcpu = &vmx->vcpu;
  5260. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  5261. if (!cpu_need_virtualize_apic_accesses(vcpu))
  5262. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5263. if (vmx->vpid == 0)
  5264. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  5265. if (!enable_ept) {
  5266. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  5267. enable_unrestricted_guest = 0;
  5268. /* Enable INVPCID for non-ept guests may cause performance regression. */
  5269. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5270. }
  5271. if (!enable_unrestricted_guest)
  5272. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  5273. if (kvm_pause_in_guest(vmx->vcpu.kvm))
  5274. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  5275. if (!kvm_vcpu_apicv_active(vcpu))
  5276. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5277. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5278. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  5279. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  5280. * in vmx_set_cr4. */
  5281. exec_control &= ~SECONDARY_EXEC_DESC;
  5282. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  5283. (handle_vmptrld).
  5284. We can NOT enable shadow_vmcs here because we don't have yet
  5285. a current VMCS12
  5286. */
  5287. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5288. if (!enable_pml)
  5289. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  5290. if (vmx_xsaves_supported()) {
  5291. /* Exposing XSAVES only when XSAVE is exposed */
  5292. bool xsaves_enabled =
  5293. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  5294. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  5295. if (!xsaves_enabled)
  5296. exec_control &= ~SECONDARY_EXEC_XSAVES;
  5297. if (nested) {
  5298. if (xsaves_enabled)
  5299. vmx->nested.msrs.secondary_ctls_high |=
  5300. SECONDARY_EXEC_XSAVES;
  5301. else
  5302. vmx->nested.msrs.secondary_ctls_high &=
  5303. ~SECONDARY_EXEC_XSAVES;
  5304. }
  5305. }
  5306. if (vmx_rdtscp_supported()) {
  5307. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  5308. if (!rdtscp_enabled)
  5309. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5310. if (nested) {
  5311. if (rdtscp_enabled)
  5312. vmx->nested.msrs.secondary_ctls_high |=
  5313. SECONDARY_EXEC_RDTSCP;
  5314. else
  5315. vmx->nested.msrs.secondary_ctls_high &=
  5316. ~SECONDARY_EXEC_RDTSCP;
  5317. }
  5318. }
  5319. if (vmx_invpcid_supported()) {
  5320. /* Exposing INVPCID only when PCID is exposed */
  5321. bool invpcid_enabled =
  5322. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  5323. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  5324. if (!invpcid_enabled) {
  5325. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5326. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  5327. }
  5328. if (nested) {
  5329. if (invpcid_enabled)
  5330. vmx->nested.msrs.secondary_ctls_high |=
  5331. SECONDARY_EXEC_ENABLE_INVPCID;
  5332. else
  5333. vmx->nested.msrs.secondary_ctls_high &=
  5334. ~SECONDARY_EXEC_ENABLE_INVPCID;
  5335. }
  5336. }
  5337. if (vmx_rdrand_supported()) {
  5338. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  5339. if (rdrand_enabled)
  5340. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  5341. if (nested) {
  5342. if (rdrand_enabled)
  5343. vmx->nested.msrs.secondary_ctls_high |=
  5344. SECONDARY_EXEC_RDRAND_EXITING;
  5345. else
  5346. vmx->nested.msrs.secondary_ctls_high &=
  5347. ~SECONDARY_EXEC_RDRAND_EXITING;
  5348. }
  5349. }
  5350. if (vmx_rdseed_supported()) {
  5351. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  5352. if (rdseed_enabled)
  5353. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  5354. if (nested) {
  5355. if (rdseed_enabled)
  5356. vmx->nested.msrs.secondary_ctls_high |=
  5357. SECONDARY_EXEC_RDSEED_EXITING;
  5358. else
  5359. vmx->nested.msrs.secondary_ctls_high &=
  5360. ~SECONDARY_EXEC_RDSEED_EXITING;
  5361. }
  5362. }
  5363. vmx->secondary_exec_control = exec_control;
  5364. }
  5365. static void ept_set_mmio_spte_mask(void)
  5366. {
  5367. /*
  5368. * EPT Misconfigurations can be generated if the value of bits 2:0
  5369. * of an EPT paging-structure entry is 110b (write/execute).
  5370. */
  5371. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  5372. VMX_EPT_MISCONFIG_WX_VALUE);
  5373. }
  5374. #define VMX_XSS_EXIT_BITMAP 0
  5375. /*
  5376. * Sets up the vmcs for emulated real mode.
  5377. */
  5378. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  5379. {
  5380. #ifdef CONFIG_X86_64
  5381. unsigned long a;
  5382. #endif
  5383. int i;
  5384. if (enable_shadow_vmcs) {
  5385. /*
  5386. * At vCPU creation, "VMWRITE to any supported field
  5387. * in the VMCS" is supported, so use the more
  5388. * permissive vmx_vmread_bitmap to specify both read
  5389. * and write permissions for the shadow VMCS.
  5390. */
  5391. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  5392. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
  5393. }
  5394. if (cpu_has_vmx_msr_bitmap())
  5395. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  5396. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  5397. /* Control */
  5398. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5399. vmx->hv_deadline_tsc = -1;
  5400. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  5401. if (cpu_has_secondary_exec_ctrls()) {
  5402. vmx_compute_secondary_exec_control(vmx);
  5403. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5404. vmx->secondary_exec_control);
  5405. }
  5406. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  5407. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  5408. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  5409. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  5410. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  5411. vmcs_write16(GUEST_INTR_STATUS, 0);
  5412. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  5413. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  5414. }
  5415. if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
  5416. vmcs_write32(PLE_GAP, ple_gap);
  5417. vmx->ple_window = ple_window;
  5418. vmx->ple_window_dirty = true;
  5419. }
  5420. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  5421. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  5422. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  5423. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  5424. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  5425. vmx_set_constant_host_state(vmx);
  5426. #ifdef CONFIG_X86_64
  5427. rdmsrl(MSR_FS_BASE, a);
  5428. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  5429. rdmsrl(MSR_GS_BASE, a);
  5430. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  5431. #else
  5432. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  5433. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  5434. #endif
  5435. if (cpu_has_vmx_vmfunc())
  5436. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  5437. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  5438. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  5439. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  5440. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  5441. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  5442. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5443. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5444. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  5445. u32 index = vmx_msr_index[i];
  5446. u32 data_low, data_high;
  5447. int j = vmx->nmsrs;
  5448. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  5449. continue;
  5450. if (wrmsr_safe(index, data_low, data_high) < 0)
  5451. continue;
  5452. vmx->guest_msrs[j].index = i;
  5453. vmx->guest_msrs[j].data = 0;
  5454. vmx->guest_msrs[j].mask = -1ull;
  5455. ++vmx->nmsrs;
  5456. }
  5457. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
  5458. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
  5459. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  5460. /* 22.2.1, 20.8.1 */
  5461. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  5462. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  5463. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  5464. set_cr4_guest_host_mask(vmx);
  5465. if (vmx_xsaves_supported())
  5466. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  5467. if (enable_pml) {
  5468. ASSERT(vmx->pml_pg);
  5469. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  5470. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  5471. }
  5472. }
  5473. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  5474. {
  5475. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5476. struct msr_data apic_base_msr;
  5477. u64 cr0;
  5478. vmx->rmode.vm86_active = 0;
  5479. vmx->spec_ctrl = 0;
  5480. vcpu->arch.microcode_version = 0x100000000ULL;
  5481. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  5482. kvm_set_cr8(vcpu, 0);
  5483. if (!init_event) {
  5484. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  5485. MSR_IA32_APICBASE_ENABLE;
  5486. if (kvm_vcpu_is_reset_bsp(vcpu))
  5487. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  5488. apic_base_msr.host_initiated = true;
  5489. kvm_set_apic_base(vcpu, &apic_base_msr);
  5490. }
  5491. vmx_segment_cache_clear(vmx);
  5492. seg_setup(VCPU_SREG_CS);
  5493. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  5494. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  5495. seg_setup(VCPU_SREG_DS);
  5496. seg_setup(VCPU_SREG_ES);
  5497. seg_setup(VCPU_SREG_FS);
  5498. seg_setup(VCPU_SREG_GS);
  5499. seg_setup(VCPU_SREG_SS);
  5500. vmcs_write16(GUEST_TR_SELECTOR, 0);
  5501. vmcs_writel(GUEST_TR_BASE, 0);
  5502. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  5503. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  5504. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  5505. vmcs_writel(GUEST_LDTR_BASE, 0);
  5506. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5507. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5508. if (!init_event) {
  5509. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5510. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5511. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5512. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5513. }
  5514. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5515. kvm_rip_write(vcpu, 0xfff0);
  5516. vmcs_writel(GUEST_GDTR_BASE, 0);
  5517. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5518. vmcs_writel(GUEST_IDTR_BASE, 0);
  5519. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5520. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5521. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5522. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5523. if (kvm_mpx_supported())
  5524. vmcs_write64(GUEST_BNDCFGS, 0);
  5525. setup_msrs(vmx);
  5526. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5527. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5528. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5529. if (cpu_need_tpr_shadow(vcpu))
  5530. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5531. __pa(vcpu->arch.apic->regs));
  5532. vmcs_write32(TPR_THRESHOLD, 0);
  5533. }
  5534. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5535. if (vmx->vpid != 0)
  5536. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5537. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5538. vmx->vcpu.arch.cr0 = cr0;
  5539. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5540. vmx_set_cr4(vcpu, 0);
  5541. vmx_set_efer(vcpu, 0);
  5542. update_exception_bitmap(vcpu);
  5543. vpid_sync_context(vmx->vpid);
  5544. if (init_event)
  5545. vmx_clear_hlt(vcpu);
  5546. }
  5547. /*
  5548. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5549. * For most existing hypervisors, this will always return true.
  5550. */
  5551. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5552. {
  5553. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5554. PIN_BASED_EXT_INTR_MASK;
  5555. }
  5556. /*
  5557. * In nested virtualization, check if L1 has set
  5558. * VM_EXIT_ACK_INTR_ON_EXIT
  5559. */
  5560. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5561. {
  5562. return get_vmcs12(vcpu)->vm_exit_controls &
  5563. VM_EXIT_ACK_INTR_ON_EXIT;
  5564. }
  5565. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5566. {
  5567. return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
  5568. }
  5569. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5570. {
  5571. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5572. CPU_BASED_VIRTUAL_INTR_PENDING);
  5573. }
  5574. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5575. {
  5576. if (!enable_vnmi ||
  5577. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5578. enable_irq_window(vcpu);
  5579. return;
  5580. }
  5581. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5582. CPU_BASED_VIRTUAL_NMI_PENDING);
  5583. }
  5584. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5585. {
  5586. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5587. uint32_t intr;
  5588. int irq = vcpu->arch.interrupt.nr;
  5589. trace_kvm_inj_virq(irq);
  5590. ++vcpu->stat.irq_injections;
  5591. if (vmx->rmode.vm86_active) {
  5592. int inc_eip = 0;
  5593. if (vcpu->arch.interrupt.soft)
  5594. inc_eip = vcpu->arch.event_exit_inst_len;
  5595. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5596. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5597. return;
  5598. }
  5599. intr = irq | INTR_INFO_VALID_MASK;
  5600. if (vcpu->arch.interrupt.soft) {
  5601. intr |= INTR_TYPE_SOFT_INTR;
  5602. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5603. vmx->vcpu.arch.event_exit_inst_len);
  5604. } else
  5605. intr |= INTR_TYPE_EXT_INTR;
  5606. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5607. vmx_clear_hlt(vcpu);
  5608. }
  5609. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5610. {
  5611. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5612. if (!enable_vnmi) {
  5613. /*
  5614. * Tracking the NMI-blocked state in software is built upon
  5615. * finding the next open IRQ window. This, in turn, depends on
  5616. * well-behaving guests: They have to keep IRQs disabled at
  5617. * least as long as the NMI handler runs. Otherwise we may
  5618. * cause NMI nesting, maybe breaking the guest. But as this is
  5619. * highly unlikely, we can live with the residual risk.
  5620. */
  5621. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5622. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5623. }
  5624. ++vcpu->stat.nmi_injections;
  5625. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5626. if (vmx->rmode.vm86_active) {
  5627. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5628. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5629. return;
  5630. }
  5631. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5632. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5633. vmx_clear_hlt(vcpu);
  5634. }
  5635. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5636. {
  5637. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5638. bool masked;
  5639. if (!enable_vnmi)
  5640. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5641. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5642. return false;
  5643. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5644. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5645. return masked;
  5646. }
  5647. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5648. {
  5649. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5650. if (!enable_vnmi) {
  5651. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5652. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5653. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5654. }
  5655. } else {
  5656. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5657. if (masked)
  5658. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5659. GUEST_INTR_STATE_NMI);
  5660. else
  5661. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5662. GUEST_INTR_STATE_NMI);
  5663. }
  5664. }
  5665. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5666. {
  5667. if (to_vmx(vcpu)->nested.nested_run_pending)
  5668. return 0;
  5669. if (!enable_vnmi &&
  5670. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5671. return 0;
  5672. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5673. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5674. | GUEST_INTR_STATE_NMI));
  5675. }
  5676. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5677. {
  5678. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  5679. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5680. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5681. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5682. }
  5683. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5684. {
  5685. int ret;
  5686. if (enable_unrestricted_guest)
  5687. return 0;
  5688. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5689. PAGE_SIZE * 3);
  5690. if (ret)
  5691. return ret;
  5692. to_kvm_vmx(kvm)->tss_addr = addr;
  5693. return init_rmode_tss(kvm);
  5694. }
  5695. static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
  5696. {
  5697. to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
  5698. return 0;
  5699. }
  5700. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5701. {
  5702. switch (vec) {
  5703. case BP_VECTOR:
  5704. /*
  5705. * Update instruction length as we may reinject the exception
  5706. * from user space while in guest debugging mode.
  5707. */
  5708. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5709. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5710. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5711. return false;
  5712. /* fall through */
  5713. case DB_VECTOR:
  5714. if (vcpu->guest_debug &
  5715. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  5716. return false;
  5717. /* fall through */
  5718. case DE_VECTOR:
  5719. case OF_VECTOR:
  5720. case BR_VECTOR:
  5721. case UD_VECTOR:
  5722. case DF_VECTOR:
  5723. case SS_VECTOR:
  5724. case GP_VECTOR:
  5725. case MF_VECTOR:
  5726. return true;
  5727. break;
  5728. }
  5729. return false;
  5730. }
  5731. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  5732. int vec, u32 err_code)
  5733. {
  5734. /*
  5735. * Instruction with address size override prefix opcode 0x67
  5736. * Cause the #SS fault with 0 error code in VM86 mode.
  5737. */
  5738. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  5739. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  5740. if (vcpu->arch.halt_request) {
  5741. vcpu->arch.halt_request = 0;
  5742. return kvm_vcpu_halt(vcpu);
  5743. }
  5744. return 1;
  5745. }
  5746. return 0;
  5747. }
  5748. /*
  5749. * Forward all other exceptions that are valid in real mode.
  5750. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  5751. * the required debugging infrastructure rework.
  5752. */
  5753. kvm_queue_exception(vcpu, vec);
  5754. return 1;
  5755. }
  5756. /*
  5757. * Trigger machine check on the host. We assume all the MSRs are already set up
  5758. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  5759. * We pass a fake environment to the machine check handler because we want
  5760. * the guest to be always treated like user space, no matter what context
  5761. * it used internally.
  5762. */
  5763. static void kvm_machine_check(void)
  5764. {
  5765. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  5766. struct pt_regs regs = {
  5767. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  5768. .flags = X86_EFLAGS_IF,
  5769. };
  5770. do_machine_check(&regs, 0);
  5771. #endif
  5772. }
  5773. static int handle_machine_check(struct kvm_vcpu *vcpu)
  5774. {
  5775. /* already handled by vcpu_run */
  5776. return 1;
  5777. }
  5778. static int handle_exception(struct kvm_vcpu *vcpu)
  5779. {
  5780. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5781. struct kvm_run *kvm_run = vcpu->run;
  5782. u32 intr_info, ex_no, error_code;
  5783. unsigned long cr2, rip, dr6;
  5784. u32 vect_info;
  5785. enum emulation_result er;
  5786. vect_info = vmx->idt_vectoring_info;
  5787. intr_info = vmx->exit_intr_info;
  5788. if (is_machine_check(intr_info))
  5789. return handle_machine_check(vcpu);
  5790. if (is_nmi(intr_info))
  5791. return 1; /* already handled by vmx_vcpu_run() */
  5792. if (is_invalid_opcode(intr_info))
  5793. return handle_ud(vcpu);
  5794. error_code = 0;
  5795. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  5796. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  5797. if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
  5798. WARN_ON_ONCE(!enable_vmware_backdoor);
  5799. er = emulate_instruction(vcpu,
  5800. EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
  5801. if (er == EMULATE_USER_EXIT)
  5802. return 0;
  5803. else if (er != EMULATE_DONE)
  5804. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  5805. return 1;
  5806. }
  5807. /*
  5808. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  5809. * MMIO, it is better to report an internal error.
  5810. * See the comments in vmx_handle_exit.
  5811. */
  5812. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  5813. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  5814. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5815. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  5816. vcpu->run->internal.ndata = 3;
  5817. vcpu->run->internal.data[0] = vect_info;
  5818. vcpu->run->internal.data[1] = intr_info;
  5819. vcpu->run->internal.data[2] = error_code;
  5820. return 0;
  5821. }
  5822. if (is_page_fault(intr_info)) {
  5823. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  5824. /* EPT won't cause page fault directly */
  5825. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  5826. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  5827. }
  5828. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  5829. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  5830. return handle_rmode_exception(vcpu, ex_no, error_code);
  5831. switch (ex_no) {
  5832. case AC_VECTOR:
  5833. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  5834. return 1;
  5835. case DB_VECTOR:
  5836. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  5837. if (!(vcpu->guest_debug &
  5838. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  5839. vcpu->arch.dr6 &= ~15;
  5840. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  5841. if (is_icebp(intr_info))
  5842. skip_emulated_instruction(vcpu);
  5843. kvm_queue_exception(vcpu, DB_VECTOR);
  5844. return 1;
  5845. }
  5846. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  5847. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  5848. /* fall through */
  5849. case BP_VECTOR:
  5850. /*
  5851. * Update instruction length as we may reinject #BP from
  5852. * user space while in guest debugging mode. Reading it for
  5853. * #DB as well causes no harm, it is not used in that case.
  5854. */
  5855. vmx->vcpu.arch.event_exit_inst_len =
  5856. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5857. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  5858. rip = kvm_rip_read(vcpu);
  5859. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  5860. kvm_run->debug.arch.exception = ex_no;
  5861. break;
  5862. default:
  5863. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  5864. kvm_run->ex.exception = ex_no;
  5865. kvm_run->ex.error_code = error_code;
  5866. break;
  5867. }
  5868. return 0;
  5869. }
  5870. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  5871. {
  5872. ++vcpu->stat.irq_exits;
  5873. return 1;
  5874. }
  5875. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  5876. {
  5877. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  5878. vcpu->mmio_needed = 0;
  5879. return 0;
  5880. }
  5881. static int handle_io(struct kvm_vcpu *vcpu)
  5882. {
  5883. unsigned long exit_qualification;
  5884. int size, in, string;
  5885. unsigned port;
  5886. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5887. string = (exit_qualification & 16) != 0;
  5888. ++vcpu->stat.io_exits;
  5889. if (string)
  5890. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5891. port = exit_qualification >> 16;
  5892. size = (exit_qualification & 7) + 1;
  5893. in = (exit_qualification & 8) != 0;
  5894. return kvm_fast_pio(vcpu, size, port, in);
  5895. }
  5896. static void
  5897. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  5898. {
  5899. /*
  5900. * Patch in the VMCALL instruction:
  5901. */
  5902. hypercall[0] = 0x0f;
  5903. hypercall[1] = 0x01;
  5904. hypercall[2] = 0xc1;
  5905. }
  5906. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  5907. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  5908. {
  5909. if (is_guest_mode(vcpu)) {
  5910. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5911. unsigned long orig_val = val;
  5912. /*
  5913. * We get here when L2 changed cr0 in a way that did not change
  5914. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  5915. * but did change L0 shadowed bits. So we first calculate the
  5916. * effective cr0 value that L1 would like to write into the
  5917. * hardware. It consists of the L2-owned bits from the new
  5918. * value combined with the L1-owned bits from L1's guest_cr0.
  5919. */
  5920. val = (val & ~vmcs12->cr0_guest_host_mask) |
  5921. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  5922. if (!nested_guest_cr0_valid(vcpu, val))
  5923. return 1;
  5924. if (kvm_set_cr0(vcpu, val))
  5925. return 1;
  5926. vmcs_writel(CR0_READ_SHADOW, orig_val);
  5927. return 0;
  5928. } else {
  5929. if (to_vmx(vcpu)->nested.vmxon &&
  5930. !nested_host_cr0_valid(vcpu, val))
  5931. return 1;
  5932. return kvm_set_cr0(vcpu, val);
  5933. }
  5934. }
  5935. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  5936. {
  5937. if (is_guest_mode(vcpu)) {
  5938. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5939. unsigned long orig_val = val;
  5940. /* analogously to handle_set_cr0 */
  5941. val = (val & ~vmcs12->cr4_guest_host_mask) |
  5942. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  5943. if (kvm_set_cr4(vcpu, val))
  5944. return 1;
  5945. vmcs_writel(CR4_READ_SHADOW, orig_val);
  5946. return 0;
  5947. } else
  5948. return kvm_set_cr4(vcpu, val);
  5949. }
  5950. static int handle_desc(struct kvm_vcpu *vcpu)
  5951. {
  5952. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  5953. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5954. }
  5955. static int handle_cr(struct kvm_vcpu *vcpu)
  5956. {
  5957. unsigned long exit_qualification, val;
  5958. int cr;
  5959. int reg;
  5960. int err;
  5961. int ret;
  5962. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5963. cr = exit_qualification & 15;
  5964. reg = (exit_qualification >> 8) & 15;
  5965. switch ((exit_qualification >> 4) & 3) {
  5966. case 0: /* mov to cr */
  5967. val = kvm_register_readl(vcpu, reg);
  5968. trace_kvm_cr_write(cr, val);
  5969. switch (cr) {
  5970. case 0:
  5971. err = handle_set_cr0(vcpu, val);
  5972. return kvm_complete_insn_gp(vcpu, err);
  5973. case 3:
  5974. WARN_ON_ONCE(enable_unrestricted_guest);
  5975. err = kvm_set_cr3(vcpu, val);
  5976. return kvm_complete_insn_gp(vcpu, err);
  5977. case 4:
  5978. err = handle_set_cr4(vcpu, val);
  5979. return kvm_complete_insn_gp(vcpu, err);
  5980. case 8: {
  5981. u8 cr8_prev = kvm_get_cr8(vcpu);
  5982. u8 cr8 = (u8)val;
  5983. err = kvm_set_cr8(vcpu, cr8);
  5984. ret = kvm_complete_insn_gp(vcpu, err);
  5985. if (lapic_in_kernel(vcpu))
  5986. return ret;
  5987. if (cr8_prev <= cr8)
  5988. return ret;
  5989. /*
  5990. * TODO: we might be squashing a
  5991. * KVM_GUESTDBG_SINGLESTEP-triggered
  5992. * KVM_EXIT_DEBUG here.
  5993. */
  5994. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  5995. return 0;
  5996. }
  5997. }
  5998. break;
  5999. case 2: /* clts */
  6000. WARN_ONCE(1, "Guest should always own CR0.TS");
  6001. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  6002. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  6003. return kvm_skip_emulated_instruction(vcpu);
  6004. case 1: /*mov from cr*/
  6005. switch (cr) {
  6006. case 3:
  6007. WARN_ON_ONCE(enable_unrestricted_guest);
  6008. val = kvm_read_cr3(vcpu);
  6009. kvm_register_write(vcpu, reg, val);
  6010. trace_kvm_cr_read(cr, val);
  6011. return kvm_skip_emulated_instruction(vcpu);
  6012. case 8:
  6013. val = kvm_get_cr8(vcpu);
  6014. kvm_register_write(vcpu, reg, val);
  6015. trace_kvm_cr_read(cr, val);
  6016. return kvm_skip_emulated_instruction(vcpu);
  6017. }
  6018. break;
  6019. case 3: /* lmsw */
  6020. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  6021. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  6022. kvm_lmsw(vcpu, val);
  6023. return kvm_skip_emulated_instruction(vcpu);
  6024. default:
  6025. break;
  6026. }
  6027. vcpu->run->exit_reason = 0;
  6028. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  6029. (int)(exit_qualification >> 4) & 3, cr);
  6030. return 0;
  6031. }
  6032. static int handle_dr(struct kvm_vcpu *vcpu)
  6033. {
  6034. unsigned long exit_qualification;
  6035. int dr, dr7, reg;
  6036. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6037. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  6038. /* First, if DR does not exist, trigger UD */
  6039. if (!kvm_require_dr(vcpu, dr))
  6040. return 1;
  6041. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  6042. if (!kvm_require_cpl(vcpu, 0))
  6043. return 1;
  6044. dr7 = vmcs_readl(GUEST_DR7);
  6045. if (dr7 & DR7_GD) {
  6046. /*
  6047. * As the vm-exit takes precedence over the debug trap, we
  6048. * need to emulate the latter, either for the host or the
  6049. * guest debugging itself.
  6050. */
  6051. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  6052. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  6053. vcpu->run->debug.arch.dr7 = dr7;
  6054. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  6055. vcpu->run->debug.arch.exception = DB_VECTOR;
  6056. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  6057. return 0;
  6058. } else {
  6059. vcpu->arch.dr6 &= ~15;
  6060. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  6061. kvm_queue_exception(vcpu, DB_VECTOR);
  6062. return 1;
  6063. }
  6064. }
  6065. if (vcpu->guest_debug == 0) {
  6066. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6067. CPU_BASED_MOV_DR_EXITING);
  6068. /*
  6069. * No more DR vmexits; force a reload of the debug registers
  6070. * and reenter on this instruction. The next vmexit will
  6071. * retrieve the full state of the debug registers.
  6072. */
  6073. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  6074. return 1;
  6075. }
  6076. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  6077. if (exit_qualification & TYPE_MOV_FROM_DR) {
  6078. unsigned long val;
  6079. if (kvm_get_dr(vcpu, dr, &val))
  6080. return 1;
  6081. kvm_register_write(vcpu, reg, val);
  6082. } else
  6083. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  6084. return 1;
  6085. return kvm_skip_emulated_instruction(vcpu);
  6086. }
  6087. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  6088. {
  6089. return vcpu->arch.dr6;
  6090. }
  6091. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  6092. {
  6093. }
  6094. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  6095. {
  6096. get_debugreg(vcpu->arch.db[0], 0);
  6097. get_debugreg(vcpu->arch.db[1], 1);
  6098. get_debugreg(vcpu->arch.db[2], 2);
  6099. get_debugreg(vcpu->arch.db[3], 3);
  6100. get_debugreg(vcpu->arch.dr6, 6);
  6101. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  6102. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  6103. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  6104. }
  6105. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  6106. {
  6107. vmcs_writel(GUEST_DR7, val);
  6108. }
  6109. static int handle_cpuid(struct kvm_vcpu *vcpu)
  6110. {
  6111. return kvm_emulate_cpuid(vcpu);
  6112. }
  6113. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  6114. {
  6115. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6116. struct msr_data msr_info;
  6117. msr_info.index = ecx;
  6118. msr_info.host_initiated = false;
  6119. if (vmx_get_msr(vcpu, &msr_info)) {
  6120. trace_kvm_msr_read_ex(ecx);
  6121. kvm_inject_gp(vcpu, 0);
  6122. return 1;
  6123. }
  6124. trace_kvm_msr_read(ecx, msr_info.data);
  6125. /* FIXME: handling of bits 32:63 of rax, rdx */
  6126. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  6127. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  6128. return kvm_skip_emulated_instruction(vcpu);
  6129. }
  6130. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  6131. {
  6132. struct msr_data msr;
  6133. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6134. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  6135. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  6136. msr.data = data;
  6137. msr.index = ecx;
  6138. msr.host_initiated = false;
  6139. if (kvm_set_msr(vcpu, &msr) != 0) {
  6140. trace_kvm_msr_write_ex(ecx, data);
  6141. kvm_inject_gp(vcpu, 0);
  6142. return 1;
  6143. }
  6144. trace_kvm_msr_write(ecx, data);
  6145. return kvm_skip_emulated_instruction(vcpu);
  6146. }
  6147. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  6148. {
  6149. kvm_apic_update_ppr(vcpu);
  6150. return 1;
  6151. }
  6152. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  6153. {
  6154. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6155. CPU_BASED_VIRTUAL_INTR_PENDING);
  6156. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6157. ++vcpu->stat.irq_window_exits;
  6158. return 1;
  6159. }
  6160. static int handle_halt(struct kvm_vcpu *vcpu)
  6161. {
  6162. return kvm_emulate_halt(vcpu);
  6163. }
  6164. static int handle_vmcall(struct kvm_vcpu *vcpu)
  6165. {
  6166. return kvm_emulate_hypercall(vcpu);
  6167. }
  6168. static int handle_invd(struct kvm_vcpu *vcpu)
  6169. {
  6170. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6171. }
  6172. static int handle_invlpg(struct kvm_vcpu *vcpu)
  6173. {
  6174. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6175. kvm_mmu_invlpg(vcpu, exit_qualification);
  6176. return kvm_skip_emulated_instruction(vcpu);
  6177. }
  6178. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  6179. {
  6180. int err;
  6181. err = kvm_rdpmc(vcpu);
  6182. return kvm_complete_insn_gp(vcpu, err);
  6183. }
  6184. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  6185. {
  6186. return kvm_emulate_wbinvd(vcpu);
  6187. }
  6188. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  6189. {
  6190. u64 new_bv = kvm_read_edx_eax(vcpu);
  6191. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  6192. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  6193. return kvm_skip_emulated_instruction(vcpu);
  6194. return 1;
  6195. }
  6196. static int handle_xsaves(struct kvm_vcpu *vcpu)
  6197. {
  6198. kvm_skip_emulated_instruction(vcpu);
  6199. WARN(1, "this should never happen\n");
  6200. return 1;
  6201. }
  6202. static int handle_xrstors(struct kvm_vcpu *vcpu)
  6203. {
  6204. kvm_skip_emulated_instruction(vcpu);
  6205. WARN(1, "this should never happen\n");
  6206. return 1;
  6207. }
  6208. static int handle_apic_access(struct kvm_vcpu *vcpu)
  6209. {
  6210. if (likely(fasteoi)) {
  6211. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6212. int access_type, offset;
  6213. access_type = exit_qualification & APIC_ACCESS_TYPE;
  6214. offset = exit_qualification & APIC_ACCESS_OFFSET;
  6215. /*
  6216. * Sane guest uses MOV to write EOI, with written value
  6217. * not cared. So make a short-circuit here by avoiding
  6218. * heavy instruction emulation.
  6219. */
  6220. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  6221. (offset == APIC_EOI)) {
  6222. kvm_lapic_set_eoi(vcpu);
  6223. return kvm_skip_emulated_instruction(vcpu);
  6224. }
  6225. }
  6226. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6227. }
  6228. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  6229. {
  6230. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6231. int vector = exit_qualification & 0xff;
  6232. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  6233. kvm_apic_set_eoi_accelerated(vcpu, vector);
  6234. return 1;
  6235. }
  6236. static int handle_apic_write(struct kvm_vcpu *vcpu)
  6237. {
  6238. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6239. u32 offset = exit_qualification & 0xfff;
  6240. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  6241. kvm_apic_write_nodecode(vcpu, offset);
  6242. return 1;
  6243. }
  6244. static int handle_task_switch(struct kvm_vcpu *vcpu)
  6245. {
  6246. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6247. unsigned long exit_qualification;
  6248. bool has_error_code = false;
  6249. u32 error_code = 0;
  6250. u16 tss_selector;
  6251. int reason, type, idt_v, idt_index;
  6252. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  6253. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  6254. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  6255. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6256. reason = (u32)exit_qualification >> 30;
  6257. if (reason == TASK_SWITCH_GATE && idt_v) {
  6258. switch (type) {
  6259. case INTR_TYPE_NMI_INTR:
  6260. vcpu->arch.nmi_injected = false;
  6261. vmx_set_nmi_mask(vcpu, true);
  6262. break;
  6263. case INTR_TYPE_EXT_INTR:
  6264. case INTR_TYPE_SOFT_INTR:
  6265. kvm_clear_interrupt_queue(vcpu);
  6266. break;
  6267. case INTR_TYPE_HARD_EXCEPTION:
  6268. if (vmx->idt_vectoring_info &
  6269. VECTORING_INFO_DELIVER_CODE_MASK) {
  6270. has_error_code = true;
  6271. error_code =
  6272. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6273. }
  6274. /* fall through */
  6275. case INTR_TYPE_SOFT_EXCEPTION:
  6276. kvm_clear_exception_queue(vcpu);
  6277. break;
  6278. default:
  6279. break;
  6280. }
  6281. }
  6282. tss_selector = exit_qualification;
  6283. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  6284. type != INTR_TYPE_EXT_INTR &&
  6285. type != INTR_TYPE_NMI_INTR))
  6286. skip_emulated_instruction(vcpu);
  6287. if (kvm_task_switch(vcpu, tss_selector,
  6288. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  6289. has_error_code, error_code) == EMULATE_FAIL) {
  6290. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6291. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6292. vcpu->run->internal.ndata = 0;
  6293. return 0;
  6294. }
  6295. /*
  6296. * TODO: What about debug traps on tss switch?
  6297. * Are we supposed to inject them and update dr6?
  6298. */
  6299. return 1;
  6300. }
  6301. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  6302. {
  6303. unsigned long exit_qualification;
  6304. gpa_t gpa;
  6305. u64 error_code;
  6306. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6307. /*
  6308. * EPT violation happened while executing iret from NMI,
  6309. * "blocked by NMI" bit has to be set before next VM entry.
  6310. * There are errata that may cause this bit to not be set:
  6311. * AAK134, BY25.
  6312. */
  6313. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6314. enable_vnmi &&
  6315. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6316. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  6317. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6318. trace_kvm_page_fault(gpa, exit_qualification);
  6319. /* Is it a read fault? */
  6320. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  6321. ? PFERR_USER_MASK : 0;
  6322. /* Is it a write fault? */
  6323. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  6324. ? PFERR_WRITE_MASK : 0;
  6325. /* Is it a fetch fault? */
  6326. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  6327. ? PFERR_FETCH_MASK : 0;
  6328. /* ept page table entry is present? */
  6329. error_code |= (exit_qualification &
  6330. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  6331. EPT_VIOLATION_EXECUTABLE))
  6332. ? PFERR_PRESENT_MASK : 0;
  6333. error_code |= (exit_qualification & 0x100) != 0 ?
  6334. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  6335. vcpu->arch.exit_qualification = exit_qualification;
  6336. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  6337. }
  6338. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  6339. {
  6340. gpa_t gpa;
  6341. /*
  6342. * A nested guest cannot optimize MMIO vmexits, because we have an
  6343. * nGPA here instead of the required GPA.
  6344. */
  6345. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6346. if (!is_guest_mode(vcpu) &&
  6347. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  6348. trace_kvm_fast_mmio(gpa);
  6349. /*
  6350. * Doing kvm_skip_emulated_instruction() depends on undefined
  6351. * behavior: Intel's manual doesn't mandate
  6352. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  6353. * occurs and while on real hardware it was observed to be set,
  6354. * other hypervisors (namely Hyper-V) don't set it, we end up
  6355. * advancing IP with some random value. Disable fast mmio when
  6356. * running nested and keep it for real hardware in hope that
  6357. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  6358. */
  6359. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  6360. return kvm_skip_emulated_instruction(vcpu);
  6361. else
  6362. return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
  6363. NULL, 0) == EMULATE_DONE;
  6364. }
  6365. return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  6366. }
  6367. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  6368. {
  6369. WARN_ON_ONCE(!enable_vnmi);
  6370. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6371. CPU_BASED_VIRTUAL_NMI_PENDING);
  6372. ++vcpu->stat.nmi_window_exits;
  6373. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6374. return 1;
  6375. }
  6376. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  6377. {
  6378. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6379. enum emulation_result err = EMULATE_DONE;
  6380. int ret = 1;
  6381. u32 cpu_exec_ctrl;
  6382. bool intr_window_requested;
  6383. unsigned count = 130;
  6384. /*
  6385. * We should never reach the point where we are emulating L2
  6386. * due to invalid guest state as that means we incorrectly
  6387. * allowed a nested VMEntry with an invalid vmcs12.
  6388. */
  6389. WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
  6390. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  6391. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  6392. while (vmx->emulation_required && count-- != 0) {
  6393. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  6394. return handle_interrupt_window(&vmx->vcpu);
  6395. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  6396. return 1;
  6397. err = emulate_instruction(vcpu, 0);
  6398. if (err == EMULATE_USER_EXIT) {
  6399. ++vcpu->stat.mmio_exits;
  6400. ret = 0;
  6401. goto out;
  6402. }
  6403. if (err != EMULATE_DONE)
  6404. goto emulation_error;
  6405. if (vmx->emulation_required && !vmx->rmode.vm86_active &&
  6406. vcpu->arch.exception.pending)
  6407. goto emulation_error;
  6408. if (vcpu->arch.halt_request) {
  6409. vcpu->arch.halt_request = 0;
  6410. ret = kvm_vcpu_halt(vcpu);
  6411. goto out;
  6412. }
  6413. if (signal_pending(current))
  6414. goto out;
  6415. if (need_resched())
  6416. schedule();
  6417. }
  6418. out:
  6419. return ret;
  6420. emulation_error:
  6421. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6422. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6423. vcpu->run->internal.ndata = 0;
  6424. return 0;
  6425. }
  6426. static void grow_ple_window(struct kvm_vcpu *vcpu)
  6427. {
  6428. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6429. int old = vmx->ple_window;
  6430. vmx->ple_window = __grow_ple_window(old, ple_window,
  6431. ple_window_grow,
  6432. ple_window_max);
  6433. if (vmx->ple_window != old)
  6434. vmx->ple_window_dirty = true;
  6435. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  6436. }
  6437. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  6438. {
  6439. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6440. int old = vmx->ple_window;
  6441. vmx->ple_window = __shrink_ple_window(old, ple_window,
  6442. ple_window_shrink,
  6443. ple_window);
  6444. if (vmx->ple_window != old)
  6445. vmx->ple_window_dirty = true;
  6446. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  6447. }
  6448. /*
  6449. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  6450. */
  6451. static void wakeup_handler(void)
  6452. {
  6453. struct kvm_vcpu *vcpu;
  6454. int cpu = smp_processor_id();
  6455. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6456. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  6457. blocked_vcpu_list) {
  6458. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  6459. if (pi_test_on(pi_desc) == 1)
  6460. kvm_vcpu_kick(vcpu);
  6461. }
  6462. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6463. }
  6464. static void vmx_enable_tdp(void)
  6465. {
  6466. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  6467. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  6468. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  6469. 0ull, VMX_EPT_EXECUTABLE_MASK,
  6470. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  6471. VMX_EPT_RWX_MASK, 0ull);
  6472. ept_set_mmio_spte_mask();
  6473. kvm_enable_tdp();
  6474. }
  6475. static __init int hardware_setup(void)
  6476. {
  6477. unsigned long host_bndcfgs;
  6478. int r = -ENOMEM, i;
  6479. rdmsrl_safe(MSR_EFER, &host_efer);
  6480. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  6481. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6482. for (i = 0; i < VMX_BITMAP_NR; i++) {
  6483. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  6484. if (!vmx_bitmap[i])
  6485. goto out;
  6486. }
  6487. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6488. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6489. if (setup_vmcs_config(&vmcs_config) < 0) {
  6490. r = -EIO;
  6491. goto out;
  6492. }
  6493. if (boot_cpu_has(X86_FEATURE_NX))
  6494. kvm_enable_efer_bits(EFER_NX);
  6495. if (boot_cpu_has(X86_FEATURE_MPX)) {
  6496. rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
  6497. WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
  6498. }
  6499. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6500. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6501. enable_vpid = 0;
  6502. if (!cpu_has_vmx_ept() ||
  6503. !cpu_has_vmx_ept_4levels() ||
  6504. !cpu_has_vmx_ept_mt_wb() ||
  6505. !cpu_has_vmx_invept_global())
  6506. enable_ept = 0;
  6507. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6508. enable_ept_ad_bits = 0;
  6509. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6510. enable_unrestricted_guest = 0;
  6511. if (!cpu_has_vmx_flexpriority())
  6512. flexpriority_enabled = 0;
  6513. if (!cpu_has_virtual_nmis())
  6514. enable_vnmi = 0;
  6515. /*
  6516. * set_apic_access_page_addr() is used to reload apic access
  6517. * page upon invalidation. No need to do anything if not
  6518. * using the APIC_ACCESS_ADDR VMCS field.
  6519. */
  6520. if (!flexpriority_enabled)
  6521. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6522. if (!cpu_has_vmx_tpr_shadow())
  6523. kvm_x86_ops->update_cr8_intercept = NULL;
  6524. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6525. kvm_disable_largepages();
  6526. if (!cpu_has_vmx_ple()) {
  6527. ple_gap = 0;
  6528. ple_window = 0;
  6529. ple_window_grow = 0;
  6530. ple_window_max = 0;
  6531. ple_window_shrink = 0;
  6532. }
  6533. if (!cpu_has_vmx_apicv()) {
  6534. enable_apicv = 0;
  6535. kvm_x86_ops->sync_pir_to_irr = NULL;
  6536. }
  6537. if (cpu_has_vmx_tsc_scaling()) {
  6538. kvm_has_tsc_control = true;
  6539. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6540. kvm_tsc_scaling_ratio_frac_bits = 48;
  6541. }
  6542. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6543. if (enable_ept)
  6544. vmx_enable_tdp();
  6545. else
  6546. kvm_disable_tdp();
  6547. if (!nested) {
  6548. kvm_x86_ops->get_nested_state = NULL;
  6549. kvm_x86_ops->set_nested_state = NULL;
  6550. }
  6551. /*
  6552. * Only enable PML when hardware supports PML feature, and both EPT
  6553. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6554. */
  6555. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6556. enable_pml = 0;
  6557. if (!enable_pml) {
  6558. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6559. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6560. kvm_x86_ops->flush_log_dirty = NULL;
  6561. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6562. }
  6563. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6564. u64 vmx_msr;
  6565. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6566. cpu_preemption_timer_multi =
  6567. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6568. } else {
  6569. kvm_x86_ops->set_hv_timer = NULL;
  6570. kvm_x86_ops->cancel_hv_timer = NULL;
  6571. }
  6572. if (!cpu_has_vmx_shadow_vmcs())
  6573. enable_shadow_vmcs = 0;
  6574. if (enable_shadow_vmcs)
  6575. init_vmcs_shadow_fields();
  6576. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6577. nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
  6578. kvm_mce_cap_supported |= MCG_LMCE_P;
  6579. return alloc_kvm_area();
  6580. out:
  6581. for (i = 0; i < VMX_BITMAP_NR; i++)
  6582. free_page((unsigned long)vmx_bitmap[i]);
  6583. return r;
  6584. }
  6585. static __exit void hardware_unsetup(void)
  6586. {
  6587. int i;
  6588. for (i = 0; i < VMX_BITMAP_NR; i++)
  6589. free_page((unsigned long)vmx_bitmap[i]);
  6590. free_kvm_area();
  6591. }
  6592. /*
  6593. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6594. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6595. */
  6596. static int handle_pause(struct kvm_vcpu *vcpu)
  6597. {
  6598. if (!kvm_pause_in_guest(vcpu->kvm))
  6599. grow_ple_window(vcpu);
  6600. /*
  6601. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6602. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6603. * never set PAUSE_EXITING and just set PLE if supported,
  6604. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6605. */
  6606. kvm_vcpu_on_spin(vcpu, true);
  6607. return kvm_skip_emulated_instruction(vcpu);
  6608. }
  6609. static int handle_nop(struct kvm_vcpu *vcpu)
  6610. {
  6611. return kvm_skip_emulated_instruction(vcpu);
  6612. }
  6613. static int handle_mwait(struct kvm_vcpu *vcpu)
  6614. {
  6615. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6616. return handle_nop(vcpu);
  6617. }
  6618. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6619. {
  6620. kvm_queue_exception(vcpu, UD_VECTOR);
  6621. return 1;
  6622. }
  6623. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6624. {
  6625. return 1;
  6626. }
  6627. static int handle_monitor(struct kvm_vcpu *vcpu)
  6628. {
  6629. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6630. return handle_nop(vcpu);
  6631. }
  6632. /*
  6633. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6634. * set the success or error code of an emulated VMX instruction, as specified
  6635. * by Vol 2B, VMX Instruction Reference, "Conventions".
  6636. */
  6637. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6638. {
  6639. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6640. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6641. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6642. }
  6643. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6644. {
  6645. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6646. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6647. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6648. | X86_EFLAGS_CF);
  6649. }
  6650. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6651. u32 vm_instruction_error)
  6652. {
  6653. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  6654. /*
  6655. * failValid writes the error number to the current VMCS, which
  6656. * can't be done there isn't a current VMCS.
  6657. */
  6658. nested_vmx_failInvalid(vcpu);
  6659. return;
  6660. }
  6661. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6662. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6663. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6664. | X86_EFLAGS_ZF);
  6665. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6666. /*
  6667. * We don't need to force a shadow sync because
  6668. * VM_INSTRUCTION_ERROR is not shadowed
  6669. */
  6670. }
  6671. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6672. {
  6673. /* TODO: not to reset guest simply here. */
  6674. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6675. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6676. }
  6677. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6678. {
  6679. struct vcpu_vmx *vmx =
  6680. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6681. vmx->nested.preemption_timer_expired = true;
  6682. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6683. kvm_vcpu_kick(&vmx->vcpu);
  6684. return HRTIMER_NORESTART;
  6685. }
  6686. /*
  6687. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6688. * exit caused by such an instruction (run by a guest hypervisor).
  6689. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6690. * #UD or #GP.
  6691. */
  6692. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6693. unsigned long exit_qualification,
  6694. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6695. {
  6696. gva_t off;
  6697. bool exn;
  6698. struct kvm_segment s;
  6699. /*
  6700. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6701. * Execution", on an exit, vmx_instruction_info holds most of the
  6702. * addressing components of the operand. Only the displacement part
  6703. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6704. * For how an actual address is calculated from all these components,
  6705. * refer to Vol. 1, "Operand Addressing".
  6706. */
  6707. int scaling = vmx_instruction_info & 3;
  6708. int addr_size = (vmx_instruction_info >> 7) & 7;
  6709. bool is_reg = vmx_instruction_info & (1u << 10);
  6710. int seg_reg = (vmx_instruction_info >> 15) & 7;
  6711. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  6712. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  6713. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  6714. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  6715. if (is_reg) {
  6716. kvm_queue_exception(vcpu, UD_VECTOR);
  6717. return 1;
  6718. }
  6719. /* Addr = segment_base + offset */
  6720. /* offset = base + [index * scale] + displacement */
  6721. off = exit_qualification; /* holds the displacement */
  6722. if (base_is_valid)
  6723. off += kvm_register_read(vcpu, base_reg);
  6724. if (index_is_valid)
  6725. off += kvm_register_read(vcpu, index_reg)<<scaling;
  6726. vmx_get_segment(vcpu, &s, seg_reg);
  6727. *ret = s.base + off;
  6728. if (addr_size == 1) /* 32 bit */
  6729. *ret &= 0xffffffff;
  6730. /* Checks for #GP/#SS exceptions. */
  6731. exn = false;
  6732. if (is_long_mode(vcpu)) {
  6733. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  6734. * non-canonical form. This is the only check on the memory
  6735. * destination for long mode!
  6736. */
  6737. exn = is_noncanonical_address(*ret, vcpu);
  6738. } else if (is_protmode(vcpu)) {
  6739. /* Protected mode: apply checks for segment validity in the
  6740. * following order:
  6741. * - segment type check (#GP(0) may be thrown)
  6742. * - usability check (#GP(0)/#SS(0))
  6743. * - limit check (#GP(0)/#SS(0))
  6744. */
  6745. if (wr)
  6746. /* #GP(0) if the destination operand is located in a
  6747. * read-only data segment or any code segment.
  6748. */
  6749. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  6750. else
  6751. /* #GP(0) if the source operand is located in an
  6752. * execute-only code segment
  6753. */
  6754. exn = ((s.type & 0xa) == 8);
  6755. if (exn) {
  6756. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  6757. return 1;
  6758. }
  6759. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  6760. */
  6761. exn = (s.unusable != 0);
  6762. /* Protected mode: #GP(0)/#SS(0) if the memory
  6763. * operand is outside the segment limit.
  6764. */
  6765. exn = exn || (off + sizeof(u64) > s.limit);
  6766. }
  6767. if (exn) {
  6768. kvm_queue_exception_e(vcpu,
  6769. seg_reg == VCPU_SREG_SS ?
  6770. SS_VECTOR : GP_VECTOR,
  6771. 0);
  6772. return 1;
  6773. }
  6774. return 0;
  6775. }
  6776. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  6777. {
  6778. gva_t gva;
  6779. struct x86_exception e;
  6780. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6781. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  6782. return 1;
  6783. if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
  6784. kvm_inject_page_fault(vcpu, &e);
  6785. return 1;
  6786. }
  6787. return 0;
  6788. }
  6789. /*
  6790. * Allocate a shadow VMCS and associate it with the currently loaded
  6791. * VMCS, unless such a shadow VMCS already exists. The newly allocated
  6792. * VMCS is also VMCLEARed, so that it is ready for use.
  6793. */
  6794. static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
  6795. {
  6796. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6797. struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
  6798. /*
  6799. * We should allocate a shadow vmcs for vmcs01 only when L1
  6800. * executes VMXON and free it when L1 executes VMXOFF.
  6801. * As it is invalid to execute VMXON twice, we shouldn't reach
  6802. * here when vmcs01 already have an allocated shadow vmcs.
  6803. */
  6804. WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
  6805. if (!loaded_vmcs->shadow_vmcs) {
  6806. loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
  6807. if (loaded_vmcs->shadow_vmcs)
  6808. vmcs_clear(loaded_vmcs->shadow_vmcs);
  6809. }
  6810. return loaded_vmcs->shadow_vmcs;
  6811. }
  6812. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  6813. {
  6814. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6815. int r;
  6816. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  6817. if (r < 0)
  6818. goto out_vmcs02;
  6819. vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  6820. if (!vmx->nested.cached_vmcs12)
  6821. goto out_cached_vmcs12;
  6822. vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  6823. if (!vmx->nested.cached_shadow_vmcs12)
  6824. goto out_cached_shadow_vmcs12;
  6825. if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
  6826. goto out_shadow_vmcs;
  6827. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  6828. HRTIMER_MODE_REL_PINNED);
  6829. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  6830. vmx->nested.vmxon = true;
  6831. return 0;
  6832. out_shadow_vmcs:
  6833. kfree(vmx->nested.cached_shadow_vmcs12);
  6834. out_cached_shadow_vmcs12:
  6835. kfree(vmx->nested.cached_vmcs12);
  6836. out_cached_vmcs12:
  6837. free_loaded_vmcs(&vmx->nested.vmcs02);
  6838. out_vmcs02:
  6839. return -ENOMEM;
  6840. }
  6841. /*
  6842. * Emulate the VMXON instruction.
  6843. * Currently, we just remember that VMX is active, and do not save or even
  6844. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  6845. * do not currently need to store anything in that guest-allocated memory
  6846. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  6847. * argument is different from the VMXON pointer (which the spec says they do).
  6848. */
  6849. static int handle_vmon(struct kvm_vcpu *vcpu)
  6850. {
  6851. int ret;
  6852. gpa_t vmptr;
  6853. struct page *page;
  6854. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6855. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  6856. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  6857. /*
  6858. * The Intel VMX Instruction Reference lists a bunch of bits that are
  6859. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  6860. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  6861. * Otherwise, we should fail with #UD. But most faulting conditions
  6862. * have already been checked by hardware, prior to the VM-exit for
  6863. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  6864. * that bit set to 1 in non-root mode.
  6865. */
  6866. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  6867. kvm_queue_exception(vcpu, UD_VECTOR);
  6868. return 1;
  6869. }
  6870. /* CPL=0 must be checked manually. */
  6871. if (vmx_get_cpl(vcpu)) {
  6872. kvm_queue_exception(vcpu, UD_VECTOR);
  6873. return 1;
  6874. }
  6875. if (vmx->nested.vmxon) {
  6876. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  6877. return kvm_skip_emulated_instruction(vcpu);
  6878. }
  6879. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  6880. != VMXON_NEEDED_FEATURES) {
  6881. kvm_inject_gp(vcpu, 0);
  6882. return 1;
  6883. }
  6884. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6885. return 1;
  6886. /*
  6887. * SDM 3: 24.11.5
  6888. * The first 4 bytes of VMXON region contain the supported
  6889. * VMCS revision identifier
  6890. *
  6891. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  6892. * which replaces physical address width with 32
  6893. */
  6894. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6895. nested_vmx_failInvalid(vcpu);
  6896. return kvm_skip_emulated_instruction(vcpu);
  6897. }
  6898. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  6899. if (is_error_page(page)) {
  6900. nested_vmx_failInvalid(vcpu);
  6901. return kvm_skip_emulated_instruction(vcpu);
  6902. }
  6903. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  6904. kunmap(page);
  6905. kvm_release_page_clean(page);
  6906. nested_vmx_failInvalid(vcpu);
  6907. return kvm_skip_emulated_instruction(vcpu);
  6908. }
  6909. kunmap(page);
  6910. kvm_release_page_clean(page);
  6911. vmx->nested.vmxon_ptr = vmptr;
  6912. ret = enter_vmx_operation(vcpu);
  6913. if (ret)
  6914. return ret;
  6915. nested_vmx_succeed(vcpu);
  6916. return kvm_skip_emulated_instruction(vcpu);
  6917. }
  6918. /*
  6919. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  6920. * for running VMX instructions (except VMXON, whose prerequisites are
  6921. * slightly different). It also specifies what exception to inject otherwise.
  6922. * Note that many of these exceptions have priority over VM exits, so they
  6923. * don't have to be checked again here.
  6924. */
  6925. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  6926. {
  6927. if (vmx_get_cpl(vcpu)) {
  6928. kvm_queue_exception(vcpu, UD_VECTOR);
  6929. return 0;
  6930. }
  6931. if (!to_vmx(vcpu)->nested.vmxon) {
  6932. kvm_queue_exception(vcpu, UD_VECTOR);
  6933. return 0;
  6934. }
  6935. return 1;
  6936. }
  6937. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  6938. {
  6939. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  6940. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  6941. }
  6942. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  6943. {
  6944. if (vmx->nested.current_vmptr == -1ull)
  6945. return;
  6946. if (enable_shadow_vmcs) {
  6947. /* copy to memory all shadowed fields in case
  6948. they were modified */
  6949. copy_shadow_to_vmcs12(vmx);
  6950. vmx->nested.sync_shadow_vmcs = false;
  6951. vmx_disable_shadow_vmcs(vmx);
  6952. }
  6953. vmx->nested.posted_intr_nv = -1;
  6954. /* Flush VMCS12 to guest memory */
  6955. kvm_vcpu_write_guest_page(&vmx->vcpu,
  6956. vmx->nested.current_vmptr >> PAGE_SHIFT,
  6957. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  6958. vmx->nested.current_vmptr = -1ull;
  6959. }
  6960. /*
  6961. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  6962. * just stops using VMX.
  6963. */
  6964. static void free_nested(struct vcpu_vmx *vmx)
  6965. {
  6966. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  6967. return;
  6968. vmx->nested.vmxon = false;
  6969. vmx->nested.smm.vmxon = false;
  6970. free_vpid(vmx->nested.vpid02);
  6971. vmx->nested.posted_intr_nv = -1;
  6972. vmx->nested.current_vmptr = -1ull;
  6973. if (enable_shadow_vmcs) {
  6974. vmx_disable_shadow_vmcs(vmx);
  6975. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  6976. free_vmcs(vmx->vmcs01.shadow_vmcs);
  6977. vmx->vmcs01.shadow_vmcs = NULL;
  6978. }
  6979. kfree(vmx->nested.cached_vmcs12);
  6980. kfree(vmx->nested.cached_shadow_vmcs12);
  6981. /* Unpin physical memory we referred to in the vmcs02 */
  6982. if (vmx->nested.apic_access_page) {
  6983. kvm_release_page_dirty(vmx->nested.apic_access_page);
  6984. vmx->nested.apic_access_page = NULL;
  6985. }
  6986. if (vmx->nested.virtual_apic_page) {
  6987. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  6988. vmx->nested.virtual_apic_page = NULL;
  6989. }
  6990. if (vmx->nested.pi_desc_page) {
  6991. kunmap(vmx->nested.pi_desc_page);
  6992. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  6993. vmx->nested.pi_desc_page = NULL;
  6994. vmx->nested.pi_desc = NULL;
  6995. }
  6996. free_loaded_vmcs(&vmx->nested.vmcs02);
  6997. }
  6998. /* Emulate the VMXOFF instruction */
  6999. static int handle_vmoff(struct kvm_vcpu *vcpu)
  7000. {
  7001. if (!nested_vmx_check_permission(vcpu))
  7002. return 1;
  7003. free_nested(to_vmx(vcpu));
  7004. nested_vmx_succeed(vcpu);
  7005. return kvm_skip_emulated_instruction(vcpu);
  7006. }
  7007. /* Emulate the VMCLEAR instruction */
  7008. static int handle_vmclear(struct kvm_vcpu *vcpu)
  7009. {
  7010. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7011. u32 zero = 0;
  7012. gpa_t vmptr;
  7013. if (!nested_vmx_check_permission(vcpu))
  7014. return 1;
  7015. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7016. return 1;
  7017. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7018. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  7019. return kvm_skip_emulated_instruction(vcpu);
  7020. }
  7021. if (vmptr == vmx->nested.vmxon_ptr) {
  7022. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
  7023. return kvm_skip_emulated_instruction(vcpu);
  7024. }
  7025. if (vmptr == vmx->nested.current_vmptr)
  7026. nested_release_vmcs12(vmx);
  7027. kvm_vcpu_write_guest(vcpu,
  7028. vmptr + offsetof(struct vmcs12, launch_state),
  7029. &zero, sizeof(zero));
  7030. nested_vmx_succeed(vcpu);
  7031. return kvm_skip_emulated_instruction(vcpu);
  7032. }
  7033. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  7034. /* Emulate the VMLAUNCH instruction */
  7035. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  7036. {
  7037. return nested_vmx_run(vcpu, true);
  7038. }
  7039. /* Emulate the VMRESUME instruction */
  7040. static int handle_vmresume(struct kvm_vcpu *vcpu)
  7041. {
  7042. return nested_vmx_run(vcpu, false);
  7043. }
  7044. /*
  7045. * Read a vmcs12 field. Since these can have varying lengths and we return
  7046. * one type, we chose the biggest type (u64) and zero-extend the return value
  7047. * to that size. Note that the caller, handle_vmread, might need to use only
  7048. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  7049. * 64-bit fields are to be returned).
  7050. */
  7051. static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
  7052. unsigned long field, u64 *ret)
  7053. {
  7054. short offset = vmcs_field_to_offset(field);
  7055. char *p;
  7056. if (offset < 0)
  7057. return offset;
  7058. p = (char *)vmcs12 + offset;
  7059. switch (vmcs_field_width(field)) {
  7060. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7061. *ret = *((natural_width *)p);
  7062. return 0;
  7063. case VMCS_FIELD_WIDTH_U16:
  7064. *ret = *((u16 *)p);
  7065. return 0;
  7066. case VMCS_FIELD_WIDTH_U32:
  7067. *ret = *((u32 *)p);
  7068. return 0;
  7069. case VMCS_FIELD_WIDTH_U64:
  7070. *ret = *((u64 *)p);
  7071. return 0;
  7072. default:
  7073. WARN_ON(1);
  7074. return -ENOENT;
  7075. }
  7076. }
  7077. static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
  7078. unsigned long field, u64 field_value){
  7079. short offset = vmcs_field_to_offset(field);
  7080. char *p = (char *)vmcs12 + offset;
  7081. if (offset < 0)
  7082. return offset;
  7083. switch (vmcs_field_width(field)) {
  7084. case VMCS_FIELD_WIDTH_U16:
  7085. *(u16 *)p = field_value;
  7086. return 0;
  7087. case VMCS_FIELD_WIDTH_U32:
  7088. *(u32 *)p = field_value;
  7089. return 0;
  7090. case VMCS_FIELD_WIDTH_U64:
  7091. *(u64 *)p = field_value;
  7092. return 0;
  7093. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7094. *(natural_width *)p = field_value;
  7095. return 0;
  7096. default:
  7097. WARN_ON(1);
  7098. return -ENOENT;
  7099. }
  7100. }
  7101. /*
  7102. * Copy the writable VMCS shadow fields back to the VMCS12, in case
  7103. * they have been modified by the L1 guest. Note that the "read-only"
  7104. * VM-exit information fields are actually writable if the vCPU is
  7105. * configured to support "VMWRITE to any supported field in the VMCS."
  7106. */
  7107. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  7108. {
  7109. const u16 *fields[] = {
  7110. shadow_read_write_fields,
  7111. shadow_read_only_fields
  7112. };
  7113. const int max_fields[] = {
  7114. max_shadow_read_write_fields,
  7115. max_shadow_read_only_fields
  7116. };
  7117. int i, q;
  7118. unsigned long field;
  7119. u64 field_value;
  7120. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7121. preempt_disable();
  7122. vmcs_load(shadow_vmcs);
  7123. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7124. for (i = 0; i < max_fields[q]; i++) {
  7125. field = fields[q][i];
  7126. field_value = __vmcs_readl(field);
  7127. vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
  7128. }
  7129. /*
  7130. * Skip the VM-exit information fields if they are read-only.
  7131. */
  7132. if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  7133. break;
  7134. }
  7135. vmcs_clear(shadow_vmcs);
  7136. vmcs_load(vmx->loaded_vmcs->vmcs);
  7137. preempt_enable();
  7138. }
  7139. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  7140. {
  7141. const u16 *fields[] = {
  7142. shadow_read_write_fields,
  7143. shadow_read_only_fields
  7144. };
  7145. const int max_fields[] = {
  7146. max_shadow_read_write_fields,
  7147. max_shadow_read_only_fields
  7148. };
  7149. int i, q;
  7150. unsigned long field;
  7151. u64 field_value = 0;
  7152. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7153. vmcs_load(shadow_vmcs);
  7154. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7155. for (i = 0; i < max_fields[q]; i++) {
  7156. field = fields[q][i];
  7157. vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
  7158. __vmcs_writel(field, field_value);
  7159. }
  7160. }
  7161. vmcs_clear(shadow_vmcs);
  7162. vmcs_load(vmx->loaded_vmcs->vmcs);
  7163. }
  7164. /*
  7165. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  7166. * used before) all generate the same failure when it is missing.
  7167. */
  7168. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  7169. {
  7170. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7171. if (vmx->nested.current_vmptr == -1ull) {
  7172. nested_vmx_failInvalid(vcpu);
  7173. return 0;
  7174. }
  7175. return 1;
  7176. }
  7177. static int handle_vmread(struct kvm_vcpu *vcpu)
  7178. {
  7179. unsigned long field;
  7180. u64 field_value;
  7181. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7182. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7183. gva_t gva = 0;
  7184. struct vmcs12 *vmcs12;
  7185. if (!nested_vmx_check_permission(vcpu))
  7186. return 1;
  7187. if (!nested_vmx_check_vmcs12(vcpu))
  7188. return kvm_skip_emulated_instruction(vcpu);
  7189. if (!is_guest_mode(vcpu))
  7190. vmcs12 = get_vmcs12(vcpu);
  7191. else {
  7192. /*
  7193. * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
  7194. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7195. */
  7196. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
  7197. nested_vmx_failInvalid(vcpu);
  7198. return kvm_skip_emulated_instruction(vcpu);
  7199. }
  7200. vmcs12 = get_shadow_vmcs12(vcpu);
  7201. }
  7202. /* Decode instruction info and find the field to read */
  7203. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7204. /* Read the field, zero-extended to a u64 field_value */
  7205. if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
  7206. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7207. return kvm_skip_emulated_instruction(vcpu);
  7208. }
  7209. /*
  7210. * Now copy part of this value to register or memory, as requested.
  7211. * Note that the number of bits actually copied is 32 or 64 depending
  7212. * on the guest's mode (32 or 64 bit), not on the given field's length.
  7213. */
  7214. if (vmx_instruction_info & (1u << 10)) {
  7215. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  7216. field_value);
  7217. } else {
  7218. if (get_vmx_mem_address(vcpu, exit_qualification,
  7219. vmx_instruction_info, true, &gva))
  7220. return 1;
  7221. /* _system ok, nested_vmx_check_permission has verified cpl=0 */
  7222. kvm_write_guest_virt_system(vcpu, gva, &field_value,
  7223. (is_long_mode(vcpu) ? 8 : 4), NULL);
  7224. }
  7225. nested_vmx_succeed(vcpu);
  7226. return kvm_skip_emulated_instruction(vcpu);
  7227. }
  7228. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  7229. {
  7230. unsigned long field;
  7231. gva_t gva;
  7232. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7233. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7234. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7235. /* The value to write might be 32 or 64 bits, depending on L1's long
  7236. * mode, and eventually we need to write that into a field of several
  7237. * possible lengths. The code below first zero-extends the value to 64
  7238. * bit (field_value), and then copies only the appropriate number of
  7239. * bits into the vmcs12 field.
  7240. */
  7241. u64 field_value = 0;
  7242. struct x86_exception e;
  7243. struct vmcs12 *vmcs12;
  7244. if (!nested_vmx_check_permission(vcpu))
  7245. return 1;
  7246. if (!nested_vmx_check_vmcs12(vcpu))
  7247. return kvm_skip_emulated_instruction(vcpu);
  7248. if (vmx_instruction_info & (1u << 10))
  7249. field_value = kvm_register_readl(vcpu,
  7250. (((vmx_instruction_info) >> 3) & 0xf));
  7251. else {
  7252. if (get_vmx_mem_address(vcpu, exit_qualification,
  7253. vmx_instruction_info, false, &gva))
  7254. return 1;
  7255. if (kvm_read_guest_virt(vcpu, gva, &field_value,
  7256. (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  7257. kvm_inject_page_fault(vcpu, &e);
  7258. return 1;
  7259. }
  7260. }
  7261. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7262. /*
  7263. * If the vCPU supports "VMWRITE to any supported field in the
  7264. * VMCS," then the "read-only" fields are actually read/write.
  7265. */
  7266. if (vmcs_field_readonly(field) &&
  7267. !nested_cpu_has_vmwrite_any_field(vcpu)) {
  7268. nested_vmx_failValid(vcpu,
  7269. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  7270. return kvm_skip_emulated_instruction(vcpu);
  7271. }
  7272. if (!is_guest_mode(vcpu))
  7273. vmcs12 = get_vmcs12(vcpu);
  7274. else {
  7275. /*
  7276. * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
  7277. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7278. */
  7279. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
  7280. nested_vmx_failInvalid(vcpu);
  7281. return kvm_skip_emulated_instruction(vcpu);
  7282. }
  7283. vmcs12 = get_shadow_vmcs12(vcpu);
  7284. }
  7285. if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
  7286. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7287. return kvm_skip_emulated_instruction(vcpu);
  7288. }
  7289. /*
  7290. * Do not track vmcs12 dirty-state if in guest-mode
  7291. * as we actually dirty shadow vmcs12 instead of vmcs12.
  7292. */
  7293. if (!is_guest_mode(vcpu)) {
  7294. switch (field) {
  7295. #define SHADOW_FIELD_RW(x) case x:
  7296. #include "vmx_shadow_fields.h"
  7297. /*
  7298. * The fields that can be updated by L1 without a vmexit are
  7299. * always updated in the vmcs02, the others go down the slow
  7300. * path of prepare_vmcs02.
  7301. */
  7302. break;
  7303. default:
  7304. vmx->nested.dirty_vmcs12 = true;
  7305. break;
  7306. }
  7307. }
  7308. nested_vmx_succeed(vcpu);
  7309. return kvm_skip_emulated_instruction(vcpu);
  7310. }
  7311. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  7312. {
  7313. vmx->nested.current_vmptr = vmptr;
  7314. if (enable_shadow_vmcs) {
  7315. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  7316. SECONDARY_EXEC_SHADOW_VMCS);
  7317. vmcs_write64(VMCS_LINK_POINTER,
  7318. __pa(vmx->vmcs01.shadow_vmcs));
  7319. vmx->nested.sync_shadow_vmcs = true;
  7320. }
  7321. vmx->nested.dirty_vmcs12 = true;
  7322. }
  7323. /* Emulate the VMPTRLD instruction */
  7324. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  7325. {
  7326. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7327. gpa_t vmptr;
  7328. if (!nested_vmx_check_permission(vcpu))
  7329. return 1;
  7330. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7331. return 1;
  7332. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7333. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  7334. return kvm_skip_emulated_instruction(vcpu);
  7335. }
  7336. if (vmptr == vmx->nested.vmxon_ptr) {
  7337. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
  7338. return kvm_skip_emulated_instruction(vcpu);
  7339. }
  7340. if (vmx->nested.current_vmptr != vmptr) {
  7341. struct vmcs12 *new_vmcs12;
  7342. struct page *page;
  7343. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7344. if (is_error_page(page)) {
  7345. nested_vmx_failInvalid(vcpu);
  7346. return kvm_skip_emulated_instruction(vcpu);
  7347. }
  7348. new_vmcs12 = kmap(page);
  7349. if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  7350. (new_vmcs12->hdr.shadow_vmcs &&
  7351. !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
  7352. kunmap(page);
  7353. kvm_release_page_clean(page);
  7354. nested_vmx_failValid(vcpu,
  7355. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  7356. return kvm_skip_emulated_instruction(vcpu);
  7357. }
  7358. nested_release_vmcs12(vmx);
  7359. /*
  7360. * Load VMCS12 from guest memory since it is not already
  7361. * cached.
  7362. */
  7363. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  7364. kunmap(page);
  7365. kvm_release_page_clean(page);
  7366. set_current_vmptr(vmx, vmptr);
  7367. }
  7368. nested_vmx_succeed(vcpu);
  7369. return kvm_skip_emulated_instruction(vcpu);
  7370. }
  7371. /* Emulate the VMPTRST instruction */
  7372. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  7373. {
  7374. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7375. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7376. gva_t vmcs_gva;
  7377. struct x86_exception e;
  7378. if (!nested_vmx_check_permission(vcpu))
  7379. return 1;
  7380. if (get_vmx_mem_address(vcpu, exit_qualification,
  7381. vmx_instruction_info, true, &vmcs_gva))
  7382. return 1;
  7383. /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
  7384. if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
  7385. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  7386. sizeof(u64), &e)) {
  7387. kvm_inject_page_fault(vcpu, &e);
  7388. return 1;
  7389. }
  7390. nested_vmx_succeed(vcpu);
  7391. return kvm_skip_emulated_instruction(vcpu);
  7392. }
  7393. /* Emulate the INVEPT instruction */
  7394. static int handle_invept(struct kvm_vcpu *vcpu)
  7395. {
  7396. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7397. u32 vmx_instruction_info, types;
  7398. unsigned long type;
  7399. gva_t gva;
  7400. struct x86_exception e;
  7401. struct {
  7402. u64 eptp, gpa;
  7403. } operand;
  7404. if (!(vmx->nested.msrs.secondary_ctls_high &
  7405. SECONDARY_EXEC_ENABLE_EPT) ||
  7406. !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
  7407. kvm_queue_exception(vcpu, UD_VECTOR);
  7408. return 1;
  7409. }
  7410. if (!nested_vmx_check_permission(vcpu))
  7411. return 1;
  7412. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7413. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7414. types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  7415. if (type >= 32 || !(types & (1 << type))) {
  7416. nested_vmx_failValid(vcpu,
  7417. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7418. return kvm_skip_emulated_instruction(vcpu);
  7419. }
  7420. /* According to the Intel VMX instruction reference, the memory
  7421. * operand is read even if it isn't needed (e.g., for type==global)
  7422. */
  7423. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7424. vmx_instruction_info, false, &gva))
  7425. return 1;
  7426. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7427. kvm_inject_page_fault(vcpu, &e);
  7428. return 1;
  7429. }
  7430. switch (type) {
  7431. case VMX_EPT_EXTENT_GLOBAL:
  7432. /*
  7433. * TODO: track mappings and invalidate
  7434. * single context requests appropriately
  7435. */
  7436. case VMX_EPT_EXTENT_CONTEXT:
  7437. kvm_mmu_sync_roots(vcpu);
  7438. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7439. nested_vmx_succeed(vcpu);
  7440. break;
  7441. default:
  7442. BUG_ON(1);
  7443. break;
  7444. }
  7445. return kvm_skip_emulated_instruction(vcpu);
  7446. }
  7447. static int handle_invvpid(struct kvm_vcpu *vcpu)
  7448. {
  7449. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7450. u32 vmx_instruction_info;
  7451. unsigned long type, types;
  7452. gva_t gva;
  7453. struct x86_exception e;
  7454. struct {
  7455. u64 vpid;
  7456. u64 gla;
  7457. } operand;
  7458. if (!(vmx->nested.msrs.secondary_ctls_high &
  7459. SECONDARY_EXEC_ENABLE_VPID) ||
  7460. !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
  7461. kvm_queue_exception(vcpu, UD_VECTOR);
  7462. return 1;
  7463. }
  7464. if (!nested_vmx_check_permission(vcpu))
  7465. return 1;
  7466. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7467. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7468. types = (vmx->nested.msrs.vpid_caps &
  7469. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  7470. if (type >= 32 || !(types & (1 << type))) {
  7471. nested_vmx_failValid(vcpu,
  7472. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7473. return kvm_skip_emulated_instruction(vcpu);
  7474. }
  7475. /* according to the intel vmx instruction reference, the memory
  7476. * operand is read even if it isn't needed (e.g., for type==global)
  7477. */
  7478. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7479. vmx_instruction_info, false, &gva))
  7480. return 1;
  7481. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7482. kvm_inject_page_fault(vcpu, &e);
  7483. return 1;
  7484. }
  7485. if (operand.vpid >> 16) {
  7486. nested_vmx_failValid(vcpu,
  7487. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7488. return kvm_skip_emulated_instruction(vcpu);
  7489. }
  7490. switch (type) {
  7491. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  7492. if (!operand.vpid ||
  7493. is_noncanonical_address(operand.gla, vcpu)) {
  7494. nested_vmx_failValid(vcpu,
  7495. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7496. return kvm_skip_emulated_instruction(vcpu);
  7497. }
  7498. if (cpu_has_vmx_invvpid_individual_addr() &&
  7499. vmx->nested.vpid02) {
  7500. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
  7501. vmx->nested.vpid02, operand.gla);
  7502. } else
  7503. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7504. break;
  7505. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  7506. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  7507. if (!operand.vpid) {
  7508. nested_vmx_failValid(vcpu,
  7509. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7510. return kvm_skip_emulated_instruction(vcpu);
  7511. }
  7512. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7513. break;
  7514. case VMX_VPID_EXTENT_ALL_CONTEXT:
  7515. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7516. break;
  7517. default:
  7518. WARN_ON_ONCE(1);
  7519. return kvm_skip_emulated_instruction(vcpu);
  7520. }
  7521. nested_vmx_succeed(vcpu);
  7522. return kvm_skip_emulated_instruction(vcpu);
  7523. }
  7524. static int handle_pml_full(struct kvm_vcpu *vcpu)
  7525. {
  7526. unsigned long exit_qualification;
  7527. trace_kvm_pml_full(vcpu->vcpu_id);
  7528. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7529. /*
  7530. * PML buffer FULL happened while executing iret from NMI,
  7531. * "blocked by NMI" bit has to be set before next VM entry.
  7532. */
  7533. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  7534. enable_vnmi &&
  7535. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  7536. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  7537. GUEST_INTR_STATE_NMI);
  7538. /*
  7539. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  7540. * here.., and there's no userspace involvement needed for PML.
  7541. */
  7542. return 1;
  7543. }
  7544. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  7545. {
  7546. kvm_lapic_expired_hv_timer(vcpu);
  7547. return 1;
  7548. }
  7549. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  7550. {
  7551. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7552. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  7553. /* Check for memory type validity */
  7554. switch (address & VMX_EPTP_MT_MASK) {
  7555. case VMX_EPTP_MT_UC:
  7556. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
  7557. return false;
  7558. break;
  7559. case VMX_EPTP_MT_WB:
  7560. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
  7561. return false;
  7562. break;
  7563. default:
  7564. return false;
  7565. }
  7566. /* only 4 levels page-walk length are valid */
  7567. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  7568. return false;
  7569. /* Reserved bits should not be set */
  7570. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  7571. return false;
  7572. /* AD, if set, should be supported */
  7573. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  7574. if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
  7575. return false;
  7576. }
  7577. return true;
  7578. }
  7579. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  7580. struct vmcs12 *vmcs12)
  7581. {
  7582. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  7583. u64 address;
  7584. bool accessed_dirty;
  7585. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  7586. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  7587. !nested_cpu_has_ept(vmcs12))
  7588. return 1;
  7589. if (index >= VMFUNC_EPTP_ENTRIES)
  7590. return 1;
  7591. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  7592. &address, index * 8, 8))
  7593. return 1;
  7594. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  7595. /*
  7596. * If the (L2) guest does a vmfunc to the currently
  7597. * active ept pointer, we don't have to do anything else
  7598. */
  7599. if (vmcs12->ept_pointer != address) {
  7600. if (!valid_ept_address(vcpu, address))
  7601. return 1;
  7602. kvm_mmu_unload(vcpu);
  7603. mmu->ept_ad = accessed_dirty;
  7604. mmu->base_role.ad_disabled = !accessed_dirty;
  7605. vmcs12->ept_pointer = address;
  7606. /*
  7607. * TODO: Check what's the correct approach in case
  7608. * mmu reload fails. Currently, we just let the next
  7609. * reload potentially fail
  7610. */
  7611. kvm_mmu_reload(vcpu);
  7612. }
  7613. return 0;
  7614. }
  7615. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  7616. {
  7617. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7618. struct vmcs12 *vmcs12;
  7619. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  7620. /*
  7621. * VMFUNC is only supported for nested guests, but we always enable the
  7622. * secondary control for simplicity; for non-nested mode, fake that we
  7623. * didn't by injecting #UD.
  7624. */
  7625. if (!is_guest_mode(vcpu)) {
  7626. kvm_queue_exception(vcpu, UD_VECTOR);
  7627. return 1;
  7628. }
  7629. vmcs12 = get_vmcs12(vcpu);
  7630. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  7631. goto fail;
  7632. switch (function) {
  7633. case 0:
  7634. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  7635. goto fail;
  7636. break;
  7637. default:
  7638. goto fail;
  7639. }
  7640. return kvm_skip_emulated_instruction(vcpu);
  7641. fail:
  7642. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  7643. vmcs_read32(VM_EXIT_INTR_INFO),
  7644. vmcs_readl(EXIT_QUALIFICATION));
  7645. return 1;
  7646. }
  7647. /*
  7648. * The exit handlers return 1 if the exit was handled fully and guest execution
  7649. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  7650. * to be done to userspace and return 0.
  7651. */
  7652. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  7653. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  7654. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  7655. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  7656. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  7657. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  7658. [EXIT_REASON_CR_ACCESS] = handle_cr,
  7659. [EXIT_REASON_DR_ACCESS] = handle_dr,
  7660. [EXIT_REASON_CPUID] = handle_cpuid,
  7661. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  7662. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  7663. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  7664. [EXIT_REASON_HLT] = handle_halt,
  7665. [EXIT_REASON_INVD] = handle_invd,
  7666. [EXIT_REASON_INVLPG] = handle_invlpg,
  7667. [EXIT_REASON_RDPMC] = handle_rdpmc,
  7668. [EXIT_REASON_VMCALL] = handle_vmcall,
  7669. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  7670. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  7671. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  7672. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  7673. [EXIT_REASON_VMREAD] = handle_vmread,
  7674. [EXIT_REASON_VMRESUME] = handle_vmresume,
  7675. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  7676. [EXIT_REASON_VMOFF] = handle_vmoff,
  7677. [EXIT_REASON_VMON] = handle_vmon,
  7678. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  7679. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  7680. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  7681. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  7682. [EXIT_REASON_WBINVD] = handle_wbinvd,
  7683. [EXIT_REASON_XSETBV] = handle_xsetbv,
  7684. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  7685. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  7686. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  7687. [EXIT_REASON_LDTR_TR] = handle_desc,
  7688. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  7689. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  7690. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  7691. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  7692. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  7693. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  7694. [EXIT_REASON_INVEPT] = handle_invept,
  7695. [EXIT_REASON_INVVPID] = handle_invvpid,
  7696. [EXIT_REASON_RDRAND] = handle_invalid_op,
  7697. [EXIT_REASON_RDSEED] = handle_invalid_op,
  7698. [EXIT_REASON_XSAVES] = handle_xsaves,
  7699. [EXIT_REASON_XRSTORS] = handle_xrstors,
  7700. [EXIT_REASON_PML_FULL] = handle_pml_full,
  7701. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  7702. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  7703. };
  7704. static const int kvm_vmx_max_exit_handlers =
  7705. ARRAY_SIZE(kvm_vmx_exit_handlers);
  7706. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  7707. struct vmcs12 *vmcs12)
  7708. {
  7709. unsigned long exit_qualification;
  7710. gpa_t bitmap, last_bitmap;
  7711. unsigned int port;
  7712. int size;
  7713. u8 b;
  7714. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  7715. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  7716. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7717. port = exit_qualification >> 16;
  7718. size = (exit_qualification & 7) + 1;
  7719. last_bitmap = (gpa_t)-1;
  7720. b = -1;
  7721. while (size > 0) {
  7722. if (port < 0x8000)
  7723. bitmap = vmcs12->io_bitmap_a;
  7724. else if (port < 0x10000)
  7725. bitmap = vmcs12->io_bitmap_b;
  7726. else
  7727. return true;
  7728. bitmap += (port & 0x7fff) / 8;
  7729. if (last_bitmap != bitmap)
  7730. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  7731. return true;
  7732. if (b & (1 << (port & 7)))
  7733. return true;
  7734. port++;
  7735. size--;
  7736. last_bitmap = bitmap;
  7737. }
  7738. return false;
  7739. }
  7740. /*
  7741. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  7742. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  7743. * disinterest in the current event (read or write a specific MSR) by using an
  7744. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  7745. */
  7746. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  7747. struct vmcs12 *vmcs12, u32 exit_reason)
  7748. {
  7749. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  7750. gpa_t bitmap;
  7751. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  7752. return true;
  7753. /*
  7754. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  7755. * for the four combinations of read/write and low/high MSR numbers.
  7756. * First we need to figure out which of the four to use:
  7757. */
  7758. bitmap = vmcs12->msr_bitmap;
  7759. if (exit_reason == EXIT_REASON_MSR_WRITE)
  7760. bitmap += 2048;
  7761. if (msr_index >= 0xc0000000) {
  7762. msr_index -= 0xc0000000;
  7763. bitmap += 1024;
  7764. }
  7765. /* Then read the msr_index'th bit from this bitmap: */
  7766. if (msr_index < 1024*8) {
  7767. unsigned char b;
  7768. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  7769. return true;
  7770. return 1 & (b >> (msr_index & 7));
  7771. } else
  7772. return true; /* let L1 handle the wrong parameter */
  7773. }
  7774. /*
  7775. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  7776. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  7777. * intercept (via guest_host_mask etc.) the current event.
  7778. */
  7779. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  7780. struct vmcs12 *vmcs12)
  7781. {
  7782. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7783. int cr = exit_qualification & 15;
  7784. int reg;
  7785. unsigned long val;
  7786. switch ((exit_qualification >> 4) & 3) {
  7787. case 0: /* mov to cr */
  7788. reg = (exit_qualification >> 8) & 15;
  7789. val = kvm_register_readl(vcpu, reg);
  7790. switch (cr) {
  7791. case 0:
  7792. if (vmcs12->cr0_guest_host_mask &
  7793. (val ^ vmcs12->cr0_read_shadow))
  7794. return true;
  7795. break;
  7796. case 3:
  7797. if ((vmcs12->cr3_target_count >= 1 &&
  7798. vmcs12->cr3_target_value0 == val) ||
  7799. (vmcs12->cr3_target_count >= 2 &&
  7800. vmcs12->cr3_target_value1 == val) ||
  7801. (vmcs12->cr3_target_count >= 3 &&
  7802. vmcs12->cr3_target_value2 == val) ||
  7803. (vmcs12->cr3_target_count >= 4 &&
  7804. vmcs12->cr3_target_value3 == val))
  7805. return false;
  7806. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  7807. return true;
  7808. break;
  7809. case 4:
  7810. if (vmcs12->cr4_guest_host_mask &
  7811. (vmcs12->cr4_read_shadow ^ val))
  7812. return true;
  7813. break;
  7814. case 8:
  7815. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  7816. return true;
  7817. break;
  7818. }
  7819. break;
  7820. case 2: /* clts */
  7821. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  7822. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  7823. return true;
  7824. break;
  7825. case 1: /* mov from cr */
  7826. switch (cr) {
  7827. case 3:
  7828. if (vmcs12->cpu_based_vm_exec_control &
  7829. CPU_BASED_CR3_STORE_EXITING)
  7830. return true;
  7831. break;
  7832. case 8:
  7833. if (vmcs12->cpu_based_vm_exec_control &
  7834. CPU_BASED_CR8_STORE_EXITING)
  7835. return true;
  7836. break;
  7837. }
  7838. break;
  7839. case 3: /* lmsw */
  7840. /*
  7841. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  7842. * cr0. Other attempted changes are ignored, with no exit.
  7843. */
  7844. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  7845. if (vmcs12->cr0_guest_host_mask & 0xe &
  7846. (val ^ vmcs12->cr0_read_shadow))
  7847. return true;
  7848. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  7849. !(vmcs12->cr0_read_shadow & 0x1) &&
  7850. (val & 0x1))
  7851. return true;
  7852. break;
  7853. }
  7854. return false;
  7855. }
  7856. static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
  7857. struct vmcs12 *vmcs12, gpa_t bitmap)
  7858. {
  7859. u32 vmx_instruction_info;
  7860. unsigned long field;
  7861. u8 b;
  7862. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  7863. return true;
  7864. /* Decode instruction info and find the field to access */
  7865. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7866. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7867. /* Out-of-range fields always cause a VM exit from L2 to L1 */
  7868. if (field >> 15)
  7869. return true;
  7870. if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
  7871. return true;
  7872. return 1 & (b >> (field & 7));
  7873. }
  7874. /*
  7875. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  7876. * should handle it ourselves in L0 (and then continue L2). Only call this
  7877. * when in is_guest_mode (L2).
  7878. */
  7879. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  7880. {
  7881. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7882. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7883. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7884. if (vmx->nested.nested_run_pending)
  7885. return false;
  7886. if (unlikely(vmx->fail)) {
  7887. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  7888. vmcs_read32(VM_INSTRUCTION_ERROR));
  7889. return true;
  7890. }
  7891. /*
  7892. * The host physical addresses of some pages of guest memory
  7893. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  7894. * Page). The CPU may write to these pages via their host
  7895. * physical address while L2 is running, bypassing any
  7896. * address-translation-based dirty tracking (e.g. EPT write
  7897. * protection).
  7898. *
  7899. * Mark them dirty on every exit from L2 to prevent them from
  7900. * getting out of sync with dirty tracking.
  7901. */
  7902. nested_mark_vmcs12_pages_dirty(vcpu);
  7903. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  7904. vmcs_readl(EXIT_QUALIFICATION),
  7905. vmx->idt_vectoring_info,
  7906. intr_info,
  7907. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  7908. KVM_ISA_VMX);
  7909. switch (exit_reason) {
  7910. case EXIT_REASON_EXCEPTION_NMI:
  7911. if (is_nmi(intr_info))
  7912. return false;
  7913. else if (is_page_fault(intr_info))
  7914. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  7915. else if (is_no_device(intr_info) &&
  7916. !(vmcs12->guest_cr0 & X86_CR0_TS))
  7917. return false;
  7918. else if (is_debug(intr_info) &&
  7919. vcpu->guest_debug &
  7920. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  7921. return false;
  7922. else if (is_breakpoint(intr_info) &&
  7923. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  7924. return false;
  7925. return vmcs12->exception_bitmap &
  7926. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  7927. case EXIT_REASON_EXTERNAL_INTERRUPT:
  7928. return false;
  7929. case EXIT_REASON_TRIPLE_FAULT:
  7930. return true;
  7931. case EXIT_REASON_PENDING_INTERRUPT:
  7932. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  7933. case EXIT_REASON_NMI_WINDOW:
  7934. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  7935. case EXIT_REASON_TASK_SWITCH:
  7936. return true;
  7937. case EXIT_REASON_CPUID:
  7938. return true;
  7939. case EXIT_REASON_HLT:
  7940. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  7941. case EXIT_REASON_INVD:
  7942. return true;
  7943. case EXIT_REASON_INVLPG:
  7944. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7945. case EXIT_REASON_RDPMC:
  7946. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  7947. case EXIT_REASON_RDRAND:
  7948. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  7949. case EXIT_REASON_RDSEED:
  7950. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  7951. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  7952. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  7953. case EXIT_REASON_VMREAD:
  7954. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  7955. vmcs12->vmread_bitmap);
  7956. case EXIT_REASON_VMWRITE:
  7957. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  7958. vmcs12->vmwrite_bitmap);
  7959. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  7960. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  7961. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
  7962. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  7963. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  7964. /*
  7965. * VMX instructions trap unconditionally. This allows L1 to
  7966. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  7967. */
  7968. return true;
  7969. case EXIT_REASON_CR_ACCESS:
  7970. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  7971. case EXIT_REASON_DR_ACCESS:
  7972. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  7973. case EXIT_REASON_IO_INSTRUCTION:
  7974. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  7975. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  7976. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  7977. case EXIT_REASON_MSR_READ:
  7978. case EXIT_REASON_MSR_WRITE:
  7979. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  7980. case EXIT_REASON_INVALID_STATE:
  7981. return true;
  7982. case EXIT_REASON_MWAIT_INSTRUCTION:
  7983. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  7984. case EXIT_REASON_MONITOR_TRAP_FLAG:
  7985. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  7986. case EXIT_REASON_MONITOR_INSTRUCTION:
  7987. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  7988. case EXIT_REASON_PAUSE_INSTRUCTION:
  7989. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  7990. nested_cpu_has2(vmcs12,
  7991. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  7992. case EXIT_REASON_MCE_DURING_VMENTRY:
  7993. return false;
  7994. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  7995. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  7996. case EXIT_REASON_APIC_ACCESS:
  7997. case EXIT_REASON_APIC_WRITE:
  7998. case EXIT_REASON_EOI_INDUCED:
  7999. /*
  8000. * The controls for "virtualize APIC accesses," "APIC-
  8001. * register virtualization," and "virtual-interrupt
  8002. * delivery" only come from vmcs12.
  8003. */
  8004. return true;
  8005. case EXIT_REASON_EPT_VIOLATION:
  8006. /*
  8007. * L0 always deals with the EPT violation. If nested EPT is
  8008. * used, and the nested mmu code discovers that the address is
  8009. * missing in the guest EPT table (EPT12), the EPT violation
  8010. * will be injected with nested_ept_inject_page_fault()
  8011. */
  8012. return false;
  8013. case EXIT_REASON_EPT_MISCONFIG:
  8014. /*
  8015. * L2 never uses directly L1's EPT, but rather L0's own EPT
  8016. * table (shadow on EPT) or a merged EPT table that L0 built
  8017. * (EPT on EPT). So any problems with the structure of the
  8018. * table is L0's fault.
  8019. */
  8020. return false;
  8021. case EXIT_REASON_INVPCID:
  8022. return
  8023. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  8024. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8025. case EXIT_REASON_WBINVD:
  8026. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  8027. case EXIT_REASON_XSETBV:
  8028. return true;
  8029. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  8030. /*
  8031. * This should never happen, since it is not possible to
  8032. * set XSS to a non-zero value---neither in L1 nor in L2.
  8033. * If if it were, XSS would have to be checked against
  8034. * the XSS exit bitmap in vmcs12.
  8035. */
  8036. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  8037. case EXIT_REASON_PREEMPTION_TIMER:
  8038. return false;
  8039. case EXIT_REASON_PML_FULL:
  8040. /* We emulate PML support to L1. */
  8041. return false;
  8042. case EXIT_REASON_VMFUNC:
  8043. /* VM functions are emulated through L2->L0 vmexits. */
  8044. return false;
  8045. default:
  8046. return true;
  8047. }
  8048. }
  8049. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  8050. {
  8051. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8052. /*
  8053. * At this point, the exit interruption info in exit_intr_info
  8054. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  8055. * we need to query the in-kernel LAPIC.
  8056. */
  8057. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  8058. if ((exit_intr_info &
  8059. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  8060. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  8061. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8062. vmcs12->vm_exit_intr_error_code =
  8063. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  8064. }
  8065. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  8066. vmcs_readl(EXIT_QUALIFICATION));
  8067. return 1;
  8068. }
  8069. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  8070. {
  8071. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  8072. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  8073. }
  8074. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  8075. {
  8076. if (vmx->pml_pg) {
  8077. __free_page(vmx->pml_pg);
  8078. vmx->pml_pg = NULL;
  8079. }
  8080. }
  8081. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  8082. {
  8083. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8084. u64 *pml_buf;
  8085. u16 pml_idx;
  8086. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  8087. /* Do nothing if PML buffer is empty */
  8088. if (pml_idx == (PML_ENTITY_NUM - 1))
  8089. return;
  8090. /* PML index always points to next available PML buffer entity */
  8091. if (pml_idx >= PML_ENTITY_NUM)
  8092. pml_idx = 0;
  8093. else
  8094. pml_idx++;
  8095. pml_buf = page_address(vmx->pml_pg);
  8096. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  8097. u64 gpa;
  8098. gpa = pml_buf[pml_idx];
  8099. WARN_ON(gpa & (PAGE_SIZE - 1));
  8100. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  8101. }
  8102. /* reset PML index */
  8103. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  8104. }
  8105. /*
  8106. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  8107. * Called before reporting dirty_bitmap to userspace.
  8108. */
  8109. static void kvm_flush_pml_buffers(struct kvm *kvm)
  8110. {
  8111. int i;
  8112. struct kvm_vcpu *vcpu;
  8113. /*
  8114. * We only need to kick vcpu out of guest mode here, as PML buffer
  8115. * is flushed at beginning of all VMEXITs, and it's obvious that only
  8116. * vcpus running in guest are possible to have unflushed GPAs in PML
  8117. * buffer.
  8118. */
  8119. kvm_for_each_vcpu(i, vcpu, kvm)
  8120. kvm_vcpu_kick(vcpu);
  8121. }
  8122. static void vmx_dump_sel(char *name, uint32_t sel)
  8123. {
  8124. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  8125. name, vmcs_read16(sel),
  8126. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  8127. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  8128. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  8129. }
  8130. static void vmx_dump_dtsel(char *name, uint32_t limit)
  8131. {
  8132. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  8133. name, vmcs_read32(limit),
  8134. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  8135. }
  8136. static void dump_vmcs(void)
  8137. {
  8138. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  8139. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  8140. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  8141. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  8142. u32 secondary_exec_control = 0;
  8143. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  8144. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  8145. int i, n;
  8146. if (cpu_has_secondary_exec_ctrls())
  8147. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8148. pr_err("*** Guest State ***\n");
  8149. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8150. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  8151. vmcs_readl(CR0_GUEST_HOST_MASK));
  8152. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8153. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  8154. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  8155. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  8156. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  8157. {
  8158. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  8159. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  8160. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  8161. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  8162. }
  8163. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  8164. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  8165. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  8166. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  8167. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8168. vmcs_readl(GUEST_SYSENTER_ESP),
  8169. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  8170. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  8171. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  8172. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  8173. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  8174. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  8175. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  8176. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  8177. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  8178. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  8179. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  8180. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  8181. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  8182. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8183. efer, vmcs_read64(GUEST_IA32_PAT));
  8184. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  8185. vmcs_read64(GUEST_IA32_DEBUGCTL),
  8186. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  8187. if (cpu_has_load_perf_global_ctrl &&
  8188. vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  8189. pr_err("PerfGlobCtl = 0x%016llx\n",
  8190. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  8191. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  8192. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  8193. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  8194. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  8195. vmcs_read32(GUEST_ACTIVITY_STATE));
  8196. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  8197. pr_err("InterruptStatus = %04x\n",
  8198. vmcs_read16(GUEST_INTR_STATUS));
  8199. pr_err("*** Host State ***\n");
  8200. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  8201. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  8202. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  8203. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  8204. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  8205. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  8206. vmcs_read16(HOST_TR_SELECTOR));
  8207. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  8208. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  8209. vmcs_readl(HOST_TR_BASE));
  8210. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  8211. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  8212. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  8213. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  8214. vmcs_readl(HOST_CR4));
  8215. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8216. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  8217. vmcs_read32(HOST_IA32_SYSENTER_CS),
  8218. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  8219. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  8220. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8221. vmcs_read64(HOST_IA32_EFER),
  8222. vmcs_read64(HOST_IA32_PAT));
  8223. if (cpu_has_load_perf_global_ctrl &&
  8224. vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8225. pr_err("PerfGlobCtl = 0x%016llx\n",
  8226. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  8227. pr_err("*** Control State ***\n");
  8228. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  8229. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  8230. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  8231. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  8232. vmcs_read32(EXCEPTION_BITMAP),
  8233. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  8234. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  8235. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  8236. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8237. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  8238. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  8239. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  8240. vmcs_read32(VM_EXIT_INTR_INFO),
  8241. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8242. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  8243. pr_err(" reason=%08x qualification=%016lx\n",
  8244. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  8245. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  8246. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  8247. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  8248. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  8249. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  8250. pr_err("TSC Multiplier = 0x%016llx\n",
  8251. vmcs_read64(TSC_MULTIPLIER));
  8252. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  8253. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  8254. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  8255. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  8256. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  8257. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  8258. n = vmcs_read32(CR3_TARGET_COUNT);
  8259. for (i = 0; i + 1 < n; i += 4)
  8260. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  8261. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  8262. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  8263. if (i < n)
  8264. pr_err("CR3 target%u=%016lx\n",
  8265. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  8266. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  8267. pr_err("PLE Gap=%08x Window=%08x\n",
  8268. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  8269. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  8270. pr_err("Virtual processor ID = 0x%04x\n",
  8271. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  8272. }
  8273. /*
  8274. * The guest has exited. See if we can fix it or if we need userspace
  8275. * assistance.
  8276. */
  8277. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  8278. {
  8279. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8280. u32 exit_reason = vmx->exit_reason;
  8281. u32 vectoring_info = vmx->idt_vectoring_info;
  8282. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  8283. /*
  8284. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  8285. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  8286. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  8287. * mode as if vcpus is in root mode, the PML buffer must has been
  8288. * flushed already.
  8289. */
  8290. if (enable_pml)
  8291. vmx_flush_pml_buffer(vcpu);
  8292. /* If guest state is invalid, start emulating */
  8293. if (vmx->emulation_required)
  8294. return handle_invalid_guest_state(vcpu);
  8295. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  8296. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  8297. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  8298. dump_vmcs();
  8299. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8300. vcpu->run->fail_entry.hardware_entry_failure_reason
  8301. = exit_reason;
  8302. return 0;
  8303. }
  8304. if (unlikely(vmx->fail)) {
  8305. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8306. vcpu->run->fail_entry.hardware_entry_failure_reason
  8307. = vmcs_read32(VM_INSTRUCTION_ERROR);
  8308. return 0;
  8309. }
  8310. /*
  8311. * Note:
  8312. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  8313. * delivery event since it indicates guest is accessing MMIO.
  8314. * The vm-exit can be triggered again after return to guest that
  8315. * will cause infinite loop.
  8316. */
  8317. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  8318. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  8319. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  8320. exit_reason != EXIT_REASON_PML_FULL &&
  8321. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  8322. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  8323. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  8324. vcpu->run->internal.ndata = 3;
  8325. vcpu->run->internal.data[0] = vectoring_info;
  8326. vcpu->run->internal.data[1] = exit_reason;
  8327. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  8328. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  8329. vcpu->run->internal.ndata++;
  8330. vcpu->run->internal.data[3] =
  8331. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  8332. }
  8333. return 0;
  8334. }
  8335. if (unlikely(!enable_vnmi &&
  8336. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  8337. if (vmx_interrupt_allowed(vcpu)) {
  8338. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8339. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  8340. vcpu->arch.nmi_pending) {
  8341. /*
  8342. * This CPU don't support us in finding the end of an
  8343. * NMI-blocked window if the guest runs with IRQs
  8344. * disabled. So we pull the trigger after 1 s of
  8345. * futile waiting, but inform the user about this.
  8346. */
  8347. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  8348. "state on VCPU %d after 1 s timeout\n",
  8349. __func__, vcpu->vcpu_id);
  8350. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8351. }
  8352. }
  8353. if (exit_reason < kvm_vmx_max_exit_handlers
  8354. && kvm_vmx_exit_handlers[exit_reason])
  8355. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  8356. else {
  8357. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  8358. exit_reason);
  8359. kvm_queue_exception(vcpu, UD_VECTOR);
  8360. return 1;
  8361. }
  8362. }
  8363. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  8364. {
  8365. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8366. if (is_guest_mode(vcpu) &&
  8367. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8368. return;
  8369. if (irr == -1 || tpr < irr) {
  8370. vmcs_write32(TPR_THRESHOLD, 0);
  8371. return;
  8372. }
  8373. vmcs_write32(TPR_THRESHOLD, irr);
  8374. }
  8375. static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  8376. {
  8377. u32 sec_exec_control;
  8378. if (!lapic_in_kernel(vcpu))
  8379. return;
  8380. /* Postpone execution until vmcs01 is the current VMCS. */
  8381. if (is_guest_mode(vcpu)) {
  8382. to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
  8383. return;
  8384. }
  8385. if (!cpu_need_tpr_shadow(vcpu))
  8386. return;
  8387. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8388. sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8389. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  8390. switch (kvm_get_apic_mode(vcpu)) {
  8391. case LAPIC_MODE_INVALID:
  8392. WARN_ONCE(true, "Invalid local APIC state");
  8393. case LAPIC_MODE_DISABLED:
  8394. break;
  8395. case LAPIC_MODE_XAPIC:
  8396. if (flexpriority_enabled) {
  8397. sec_exec_control |=
  8398. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  8399. vmx_flush_tlb(vcpu, true);
  8400. }
  8401. break;
  8402. case LAPIC_MODE_X2APIC:
  8403. if (cpu_has_vmx_virtualize_x2apic_mode())
  8404. sec_exec_control |=
  8405. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  8406. break;
  8407. }
  8408. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  8409. vmx_update_msr_bitmap(vcpu);
  8410. }
  8411. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  8412. {
  8413. if (!is_guest_mode(vcpu)) {
  8414. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  8415. vmx_flush_tlb(vcpu, true);
  8416. }
  8417. }
  8418. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  8419. {
  8420. u16 status;
  8421. u8 old;
  8422. if (max_isr == -1)
  8423. max_isr = 0;
  8424. status = vmcs_read16(GUEST_INTR_STATUS);
  8425. old = status >> 8;
  8426. if (max_isr != old) {
  8427. status &= 0xff;
  8428. status |= max_isr << 8;
  8429. vmcs_write16(GUEST_INTR_STATUS, status);
  8430. }
  8431. }
  8432. static void vmx_set_rvi(int vector)
  8433. {
  8434. u16 status;
  8435. u8 old;
  8436. if (vector == -1)
  8437. vector = 0;
  8438. status = vmcs_read16(GUEST_INTR_STATUS);
  8439. old = (u8)status & 0xff;
  8440. if ((u8)vector != old) {
  8441. status &= ~0xff;
  8442. status |= (u8)vector;
  8443. vmcs_write16(GUEST_INTR_STATUS, status);
  8444. }
  8445. }
  8446. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  8447. {
  8448. /*
  8449. * When running L2, updating RVI is only relevant when
  8450. * vmcs12 virtual-interrupt-delivery enabled.
  8451. * However, it can be enabled only when L1 also
  8452. * intercepts external-interrupts and in that case
  8453. * we should not update vmcs02 RVI but instead intercept
  8454. * interrupt. Therefore, do nothing when running L2.
  8455. */
  8456. if (!is_guest_mode(vcpu))
  8457. vmx_set_rvi(max_irr);
  8458. }
  8459. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  8460. {
  8461. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8462. int max_irr;
  8463. bool max_irr_updated;
  8464. WARN_ON(!vcpu->arch.apicv_active);
  8465. if (pi_test_on(&vmx->pi_desc)) {
  8466. pi_clear_on(&vmx->pi_desc);
  8467. /*
  8468. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  8469. * But on x86 this is just a compiler barrier anyway.
  8470. */
  8471. smp_mb__after_atomic();
  8472. max_irr_updated =
  8473. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  8474. /*
  8475. * If we are running L2 and L1 has a new pending interrupt
  8476. * which can be injected, we should re-evaluate
  8477. * what should be done with this new L1 interrupt.
  8478. * If L1 intercepts external-interrupts, we should
  8479. * exit from L2 to L1. Otherwise, interrupt should be
  8480. * delivered directly to L2.
  8481. */
  8482. if (is_guest_mode(vcpu) && max_irr_updated) {
  8483. if (nested_exit_on_intr(vcpu))
  8484. kvm_vcpu_exiting_guest_mode(vcpu);
  8485. else
  8486. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8487. }
  8488. } else {
  8489. max_irr = kvm_lapic_find_highest_irr(vcpu);
  8490. }
  8491. vmx_hwapic_irr_update(vcpu, max_irr);
  8492. return max_irr;
  8493. }
  8494. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  8495. {
  8496. if (!kvm_vcpu_apicv_active(vcpu))
  8497. return;
  8498. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  8499. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  8500. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  8501. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  8502. }
  8503. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  8504. {
  8505. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8506. pi_clear_on(&vmx->pi_desc);
  8507. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  8508. }
  8509. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  8510. {
  8511. u32 exit_intr_info = 0;
  8512. u16 basic_exit_reason = (u16)vmx->exit_reason;
  8513. if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  8514. || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
  8515. return;
  8516. if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8517. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8518. vmx->exit_intr_info = exit_intr_info;
  8519. /* if exit due to PF check for async PF */
  8520. if (is_page_fault(exit_intr_info))
  8521. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  8522. /* Handle machine checks before interrupts are enabled */
  8523. if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
  8524. is_machine_check(exit_intr_info))
  8525. kvm_machine_check();
  8526. /* We need to handle NMIs before interrupts are enabled */
  8527. if (is_nmi(exit_intr_info)) {
  8528. kvm_before_interrupt(&vmx->vcpu);
  8529. asm("int $2");
  8530. kvm_after_interrupt(&vmx->vcpu);
  8531. }
  8532. }
  8533. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  8534. {
  8535. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8536. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  8537. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  8538. unsigned int vector;
  8539. unsigned long entry;
  8540. gate_desc *desc;
  8541. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8542. #ifdef CONFIG_X86_64
  8543. unsigned long tmp;
  8544. #endif
  8545. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  8546. desc = (gate_desc *)vmx->host_idt_base + vector;
  8547. entry = gate_offset(desc);
  8548. asm volatile(
  8549. #ifdef CONFIG_X86_64
  8550. "mov %%" _ASM_SP ", %[sp]\n\t"
  8551. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  8552. "push $%c[ss]\n\t"
  8553. "push %[sp]\n\t"
  8554. #endif
  8555. "pushf\n\t"
  8556. __ASM_SIZE(push) " $%c[cs]\n\t"
  8557. CALL_NOSPEC
  8558. :
  8559. #ifdef CONFIG_X86_64
  8560. [sp]"=&r"(tmp),
  8561. #endif
  8562. ASM_CALL_CONSTRAINT
  8563. :
  8564. THUNK_TARGET(entry),
  8565. [ss]"i"(__KERNEL_DS),
  8566. [cs]"i"(__KERNEL_CS)
  8567. );
  8568. }
  8569. }
  8570. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  8571. static bool vmx_has_emulated_msr(int index)
  8572. {
  8573. switch (index) {
  8574. case MSR_IA32_SMBASE:
  8575. /*
  8576. * We cannot do SMM unless we can run the guest in big
  8577. * real mode.
  8578. */
  8579. return enable_unrestricted_guest || emulate_invalid_guest_state;
  8580. case MSR_AMD64_VIRT_SPEC_CTRL:
  8581. /* This is AMD only. */
  8582. return false;
  8583. default:
  8584. return true;
  8585. }
  8586. }
  8587. static bool vmx_mpx_supported(void)
  8588. {
  8589. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  8590. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  8591. }
  8592. static bool vmx_xsaves_supported(void)
  8593. {
  8594. return vmcs_config.cpu_based_2nd_exec_ctrl &
  8595. SECONDARY_EXEC_XSAVES;
  8596. }
  8597. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  8598. {
  8599. u32 exit_intr_info;
  8600. bool unblock_nmi;
  8601. u8 vector;
  8602. bool idtv_info_valid;
  8603. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  8604. if (enable_vnmi) {
  8605. if (vmx->loaded_vmcs->nmi_known_unmasked)
  8606. return;
  8607. /*
  8608. * Can't use vmx->exit_intr_info since we're not sure what
  8609. * the exit reason is.
  8610. */
  8611. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8612. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  8613. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  8614. /*
  8615. * SDM 3: 27.7.1.2 (September 2008)
  8616. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  8617. * a guest IRET fault.
  8618. * SDM 3: 23.2.2 (September 2008)
  8619. * Bit 12 is undefined in any of the following cases:
  8620. * If the VM exit sets the valid bit in the IDT-vectoring
  8621. * information field.
  8622. * If the VM exit is due to a double fault.
  8623. */
  8624. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  8625. vector != DF_VECTOR && !idtv_info_valid)
  8626. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  8627. GUEST_INTR_STATE_NMI);
  8628. else
  8629. vmx->loaded_vmcs->nmi_known_unmasked =
  8630. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  8631. & GUEST_INTR_STATE_NMI);
  8632. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  8633. vmx->loaded_vmcs->vnmi_blocked_time +=
  8634. ktime_to_ns(ktime_sub(ktime_get(),
  8635. vmx->loaded_vmcs->entry_time));
  8636. }
  8637. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  8638. u32 idt_vectoring_info,
  8639. int instr_len_field,
  8640. int error_code_field)
  8641. {
  8642. u8 vector;
  8643. int type;
  8644. bool idtv_info_valid;
  8645. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  8646. vcpu->arch.nmi_injected = false;
  8647. kvm_clear_exception_queue(vcpu);
  8648. kvm_clear_interrupt_queue(vcpu);
  8649. if (!idtv_info_valid)
  8650. return;
  8651. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8652. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  8653. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  8654. switch (type) {
  8655. case INTR_TYPE_NMI_INTR:
  8656. vcpu->arch.nmi_injected = true;
  8657. /*
  8658. * SDM 3: 27.7.1.2 (September 2008)
  8659. * Clear bit "block by NMI" before VM entry if a NMI
  8660. * delivery faulted.
  8661. */
  8662. vmx_set_nmi_mask(vcpu, false);
  8663. break;
  8664. case INTR_TYPE_SOFT_EXCEPTION:
  8665. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8666. /* fall through */
  8667. case INTR_TYPE_HARD_EXCEPTION:
  8668. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  8669. u32 err = vmcs_read32(error_code_field);
  8670. kvm_requeue_exception_e(vcpu, vector, err);
  8671. } else
  8672. kvm_requeue_exception(vcpu, vector);
  8673. break;
  8674. case INTR_TYPE_SOFT_INTR:
  8675. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8676. /* fall through */
  8677. case INTR_TYPE_EXT_INTR:
  8678. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  8679. break;
  8680. default:
  8681. break;
  8682. }
  8683. }
  8684. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  8685. {
  8686. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  8687. VM_EXIT_INSTRUCTION_LEN,
  8688. IDT_VECTORING_ERROR_CODE);
  8689. }
  8690. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  8691. {
  8692. __vmx_complete_interrupts(vcpu,
  8693. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8694. VM_ENTRY_INSTRUCTION_LEN,
  8695. VM_ENTRY_EXCEPTION_ERROR_CODE);
  8696. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  8697. }
  8698. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  8699. {
  8700. int i, nr_msrs;
  8701. struct perf_guest_switch_msr *msrs;
  8702. msrs = perf_guest_get_msrs(&nr_msrs);
  8703. if (!msrs)
  8704. return;
  8705. for (i = 0; i < nr_msrs; i++)
  8706. if (msrs[i].host == msrs[i].guest)
  8707. clear_atomic_switch_msr(vmx, msrs[i].msr);
  8708. else
  8709. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  8710. msrs[i].host);
  8711. }
  8712. static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
  8713. {
  8714. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8715. u64 tscl;
  8716. u32 delta_tsc;
  8717. if (vmx->hv_deadline_tsc == -1)
  8718. return;
  8719. tscl = rdtsc();
  8720. if (vmx->hv_deadline_tsc > tscl)
  8721. /* sure to be 32 bit only because checked on set_hv_timer */
  8722. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  8723. cpu_preemption_timer_multi);
  8724. else
  8725. delta_tsc = 0;
  8726. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
  8727. }
  8728. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  8729. {
  8730. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8731. unsigned long cr3, cr4, evmcs_rsp;
  8732. /* Record the guest's net vcpu time for enforced NMI injections. */
  8733. if (unlikely(!enable_vnmi &&
  8734. vmx->loaded_vmcs->soft_vnmi_blocked))
  8735. vmx->loaded_vmcs->entry_time = ktime_get();
  8736. /* Don't enter VMX if guest state is invalid, let the exit handler
  8737. start emulation until we arrive back to a valid state */
  8738. if (vmx->emulation_required)
  8739. return;
  8740. if (vmx->ple_window_dirty) {
  8741. vmx->ple_window_dirty = false;
  8742. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  8743. }
  8744. if (vmx->nested.sync_shadow_vmcs) {
  8745. copy_vmcs12_to_shadow(vmx);
  8746. vmx->nested.sync_shadow_vmcs = false;
  8747. }
  8748. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  8749. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  8750. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  8751. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  8752. cr3 = __get_current_cr3_fast();
  8753. if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
  8754. vmcs_writel(HOST_CR3, cr3);
  8755. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  8756. }
  8757. cr4 = cr4_read_shadow();
  8758. if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
  8759. vmcs_writel(HOST_CR4, cr4);
  8760. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  8761. }
  8762. /* When single-stepping over STI and MOV SS, we must clear the
  8763. * corresponding interruptibility bits in the guest state. Otherwise
  8764. * vmentry fails as it then expects bit 14 (BS) in pending debug
  8765. * exceptions being set, but that's not correct for the guest debugging
  8766. * case. */
  8767. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  8768. vmx_set_interrupt_shadow(vcpu, 0);
  8769. if (static_cpu_has(X86_FEATURE_PKU) &&
  8770. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  8771. vcpu->arch.pkru != vmx->host_pkru)
  8772. __write_pkru(vcpu->arch.pkru);
  8773. atomic_switch_perf_msrs(vmx);
  8774. vmx_arm_hv_timer(vcpu);
  8775. /*
  8776. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  8777. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  8778. * is no need to worry about the conditional branch over the wrmsr
  8779. * being speculatively taken.
  8780. */
  8781. x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
  8782. vmx->__launched = vmx->loaded_vmcs->launched;
  8783. evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
  8784. (unsigned long)&current_evmcs->host_rsp : 0;
  8785. asm(
  8786. /* Store host registers */
  8787. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  8788. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  8789. "push %%" _ASM_CX " \n\t"
  8790. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8791. "je 1f \n\t"
  8792. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8793. /* Avoid VMWRITE when Enlightened VMCS is in use */
  8794. "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
  8795. "jz 2f \n\t"
  8796. "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
  8797. "jmp 1f \n\t"
  8798. "2: \n\t"
  8799. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  8800. "1: \n\t"
  8801. /* Reload cr2 if changed */
  8802. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  8803. "mov %%cr2, %%" _ASM_DX " \n\t"
  8804. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  8805. "je 3f \n\t"
  8806. "mov %%" _ASM_AX", %%cr2 \n\t"
  8807. "3: \n\t"
  8808. /* Check if vmlaunch of vmresume is needed */
  8809. "cmpl $0, %c[launched](%0) \n\t"
  8810. /* Load guest registers. Don't clobber flags. */
  8811. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  8812. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  8813. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  8814. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  8815. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  8816. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  8817. #ifdef CONFIG_X86_64
  8818. "mov %c[r8](%0), %%r8 \n\t"
  8819. "mov %c[r9](%0), %%r9 \n\t"
  8820. "mov %c[r10](%0), %%r10 \n\t"
  8821. "mov %c[r11](%0), %%r11 \n\t"
  8822. "mov %c[r12](%0), %%r12 \n\t"
  8823. "mov %c[r13](%0), %%r13 \n\t"
  8824. "mov %c[r14](%0), %%r14 \n\t"
  8825. "mov %c[r15](%0), %%r15 \n\t"
  8826. #endif
  8827. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  8828. /* Enter guest mode */
  8829. "jne 1f \n\t"
  8830. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  8831. "jmp 2f \n\t"
  8832. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  8833. "2: "
  8834. /* Save guest registers, load host registers, keep flags */
  8835. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  8836. "pop %0 \n\t"
  8837. "setbe %c[fail](%0)\n\t"
  8838. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  8839. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  8840. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  8841. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  8842. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  8843. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  8844. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  8845. #ifdef CONFIG_X86_64
  8846. "mov %%r8, %c[r8](%0) \n\t"
  8847. "mov %%r9, %c[r9](%0) \n\t"
  8848. "mov %%r10, %c[r10](%0) \n\t"
  8849. "mov %%r11, %c[r11](%0) \n\t"
  8850. "mov %%r12, %c[r12](%0) \n\t"
  8851. "mov %%r13, %c[r13](%0) \n\t"
  8852. "mov %%r14, %c[r14](%0) \n\t"
  8853. "mov %%r15, %c[r15](%0) \n\t"
  8854. "xor %%r8d, %%r8d \n\t"
  8855. "xor %%r9d, %%r9d \n\t"
  8856. "xor %%r10d, %%r10d \n\t"
  8857. "xor %%r11d, %%r11d \n\t"
  8858. "xor %%r12d, %%r12d \n\t"
  8859. "xor %%r13d, %%r13d \n\t"
  8860. "xor %%r14d, %%r14d \n\t"
  8861. "xor %%r15d, %%r15d \n\t"
  8862. #endif
  8863. "mov %%cr2, %%" _ASM_AX " \n\t"
  8864. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  8865. "xor %%eax, %%eax \n\t"
  8866. "xor %%ebx, %%ebx \n\t"
  8867. "xor %%esi, %%esi \n\t"
  8868. "xor %%edi, %%edi \n\t"
  8869. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  8870. ".pushsection .rodata \n\t"
  8871. ".global vmx_return \n\t"
  8872. "vmx_return: " _ASM_PTR " 2b \n\t"
  8873. ".popsection"
  8874. : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
  8875. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  8876. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  8877. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  8878. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  8879. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  8880. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  8881. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  8882. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  8883. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  8884. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  8885. #ifdef CONFIG_X86_64
  8886. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  8887. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  8888. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  8889. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  8890. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  8891. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  8892. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  8893. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  8894. #endif
  8895. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  8896. [wordsize]"i"(sizeof(ulong))
  8897. : "cc", "memory"
  8898. #ifdef CONFIG_X86_64
  8899. , "rax", "rbx", "rdi"
  8900. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  8901. #else
  8902. , "eax", "ebx", "edi"
  8903. #endif
  8904. );
  8905. /*
  8906. * We do not use IBRS in the kernel. If this vCPU has used the
  8907. * SPEC_CTRL MSR it may have left it on; save the value and
  8908. * turn it off. This is much more efficient than blindly adding
  8909. * it to the atomic save/restore list. Especially as the former
  8910. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  8911. *
  8912. * For non-nested case:
  8913. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  8914. * save it.
  8915. *
  8916. * For nested case:
  8917. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  8918. * save it.
  8919. */
  8920. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  8921. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  8922. x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
  8923. /* Eliminate branch target predictions from guest mode */
  8924. vmexit_fill_RSB();
  8925. /* All fields are clean at this point */
  8926. if (static_branch_unlikely(&enable_evmcs))
  8927. current_evmcs->hv_clean_fields |=
  8928. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  8929. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  8930. if (vmx->host_debugctlmsr)
  8931. update_debugctlmsr(vmx->host_debugctlmsr);
  8932. #ifndef CONFIG_X86_64
  8933. /*
  8934. * The sysexit path does not restore ds/es, so we must set them to
  8935. * a reasonable value ourselves.
  8936. *
  8937. * We can't defer this to vmx_load_host_state() since that function
  8938. * may be executed in interrupt context, which saves and restore segments
  8939. * around it, nullifying its effect.
  8940. */
  8941. loadsegment(ds, __USER_DS);
  8942. loadsegment(es, __USER_DS);
  8943. #endif
  8944. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  8945. | (1 << VCPU_EXREG_RFLAGS)
  8946. | (1 << VCPU_EXREG_PDPTR)
  8947. | (1 << VCPU_EXREG_SEGMENTS)
  8948. | (1 << VCPU_EXREG_CR3));
  8949. vcpu->arch.regs_dirty = 0;
  8950. /*
  8951. * eager fpu is enabled if PKEY is supported and CR4 is switched
  8952. * back on host, so it is safe to read guest PKRU from current
  8953. * XSAVE.
  8954. */
  8955. if (static_cpu_has(X86_FEATURE_PKU) &&
  8956. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  8957. vcpu->arch.pkru = __read_pkru();
  8958. if (vcpu->arch.pkru != vmx->host_pkru)
  8959. __write_pkru(vmx->host_pkru);
  8960. }
  8961. vmx->nested.nested_run_pending = 0;
  8962. vmx->idt_vectoring_info = 0;
  8963. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  8964. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8965. return;
  8966. vmx->loaded_vmcs->launched = 1;
  8967. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  8968. vmx_complete_atomic_exit(vmx);
  8969. vmx_recover_nmi_blocking(vmx);
  8970. vmx_complete_interrupts(vmx);
  8971. }
  8972. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  8973. static struct kvm *vmx_vm_alloc(void)
  8974. {
  8975. struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
  8976. return &kvm_vmx->kvm;
  8977. }
  8978. static void vmx_vm_free(struct kvm *kvm)
  8979. {
  8980. vfree(to_kvm_vmx(kvm));
  8981. }
  8982. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  8983. {
  8984. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8985. int cpu;
  8986. if (vmx->loaded_vmcs == vmcs)
  8987. return;
  8988. cpu = get_cpu();
  8989. vmx->loaded_vmcs = vmcs;
  8990. vmx_vcpu_put(vcpu);
  8991. vmx_vcpu_load(vcpu, cpu);
  8992. put_cpu();
  8993. }
  8994. /*
  8995. * Ensure that the current vmcs of the logical processor is the
  8996. * vmcs01 of the vcpu before calling free_nested().
  8997. */
  8998. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  8999. {
  9000. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9001. vcpu_load(vcpu);
  9002. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  9003. free_nested(vmx);
  9004. vcpu_put(vcpu);
  9005. }
  9006. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  9007. {
  9008. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9009. if (enable_pml)
  9010. vmx_destroy_pml_buffer(vmx);
  9011. free_vpid(vmx->vpid);
  9012. leave_guest_mode(vcpu);
  9013. vmx_free_vcpu_nested(vcpu);
  9014. free_loaded_vmcs(vmx->loaded_vmcs);
  9015. kfree(vmx->guest_msrs);
  9016. kvm_vcpu_uninit(vcpu);
  9017. kmem_cache_free(kvm_vcpu_cache, vmx);
  9018. }
  9019. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  9020. {
  9021. int err;
  9022. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  9023. unsigned long *msr_bitmap;
  9024. int cpu;
  9025. if (!vmx)
  9026. return ERR_PTR(-ENOMEM);
  9027. vmx->vpid = allocate_vpid();
  9028. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  9029. if (err)
  9030. goto free_vcpu;
  9031. err = -ENOMEM;
  9032. /*
  9033. * If PML is turned on, failure on enabling PML just results in failure
  9034. * of creating the vcpu, therefore we can simplify PML logic (by
  9035. * avoiding dealing with cases, such as enabling PML partially on vcpus
  9036. * for the guest, etc.
  9037. */
  9038. if (enable_pml) {
  9039. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  9040. if (!vmx->pml_pg)
  9041. goto uninit_vcpu;
  9042. }
  9043. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  9044. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  9045. > PAGE_SIZE);
  9046. if (!vmx->guest_msrs)
  9047. goto free_pml;
  9048. err = alloc_loaded_vmcs(&vmx->vmcs01);
  9049. if (err < 0)
  9050. goto free_msrs;
  9051. msr_bitmap = vmx->vmcs01.msr_bitmap;
  9052. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  9053. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  9054. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  9055. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  9056. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  9057. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  9058. vmx->msr_bitmap_mode = 0;
  9059. vmx->loaded_vmcs = &vmx->vmcs01;
  9060. cpu = get_cpu();
  9061. vmx_vcpu_load(&vmx->vcpu, cpu);
  9062. vmx->vcpu.cpu = cpu;
  9063. vmx_vcpu_setup(vmx);
  9064. vmx_vcpu_put(&vmx->vcpu);
  9065. put_cpu();
  9066. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  9067. err = alloc_apic_access_page(kvm);
  9068. if (err)
  9069. goto free_vmcs;
  9070. }
  9071. if (enable_ept && !enable_unrestricted_guest) {
  9072. err = init_rmode_identity_map(kvm);
  9073. if (err)
  9074. goto free_vmcs;
  9075. }
  9076. if (nested) {
  9077. nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
  9078. kvm_vcpu_apicv_active(&vmx->vcpu));
  9079. vmx->nested.vpid02 = allocate_vpid();
  9080. }
  9081. vmx->nested.posted_intr_nv = -1;
  9082. vmx->nested.current_vmptr = -1ull;
  9083. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  9084. /*
  9085. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  9086. * or POSTED_INTR_WAKEUP_VECTOR.
  9087. */
  9088. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  9089. vmx->pi_desc.sn = 1;
  9090. return &vmx->vcpu;
  9091. free_vmcs:
  9092. free_vpid(vmx->nested.vpid02);
  9093. free_loaded_vmcs(vmx->loaded_vmcs);
  9094. free_msrs:
  9095. kfree(vmx->guest_msrs);
  9096. free_pml:
  9097. vmx_destroy_pml_buffer(vmx);
  9098. uninit_vcpu:
  9099. kvm_vcpu_uninit(&vmx->vcpu);
  9100. free_vcpu:
  9101. free_vpid(vmx->vpid);
  9102. kmem_cache_free(kvm_vcpu_cache, vmx);
  9103. return ERR_PTR(err);
  9104. }
  9105. static int vmx_vm_init(struct kvm *kvm)
  9106. {
  9107. if (!ple_gap)
  9108. kvm->arch.pause_in_guest = true;
  9109. return 0;
  9110. }
  9111. static void __init vmx_check_processor_compat(void *rtn)
  9112. {
  9113. struct vmcs_config vmcs_conf;
  9114. *(int *)rtn = 0;
  9115. if (setup_vmcs_config(&vmcs_conf) < 0)
  9116. *(int *)rtn = -EIO;
  9117. nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
  9118. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  9119. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  9120. smp_processor_id());
  9121. *(int *)rtn = -EIO;
  9122. }
  9123. }
  9124. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  9125. {
  9126. u8 cache;
  9127. u64 ipat = 0;
  9128. /* For VT-d and EPT combination
  9129. * 1. MMIO: always map as UC
  9130. * 2. EPT with VT-d:
  9131. * a. VT-d without snooping control feature: can't guarantee the
  9132. * result, try to trust guest.
  9133. * b. VT-d with snooping control feature: snooping control feature of
  9134. * VT-d engine can guarantee the cache correctness. Just set it
  9135. * to WB to keep consistent with host. So the same as item 3.
  9136. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  9137. * consistent with host MTRR
  9138. */
  9139. if (is_mmio) {
  9140. cache = MTRR_TYPE_UNCACHABLE;
  9141. goto exit;
  9142. }
  9143. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  9144. ipat = VMX_EPT_IPAT_BIT;
  9145. cache = MTRR_TYPE_WRBACK;
  9146. goto exit;
  9147. }
  9148. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  9149. ipat = VMX_EPT_IPAT_BIT;
  9150. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  9151. cache = MTRR_TYPE_WRBACK;
  9152. else
  9153. cache = MTRR_TYPE_UNCACHABLE;
  9154. goto exit;
  9155. }
  9156. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  9157. exit:
  9158. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  9159. }
  9160. static int vmx_get_lpage_level(void)
  9161. {
  9162. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  9163. return PT_DIRECTORY_LEVEL;
  9164. else
  9165. /* For shadow and EPT supported 1GB page */
  9166. return PT_PDPE_LEVEL;
  9167. }
  9168. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  9169. {
  9170. /*
  9171. * These bits in the secondary execution controls field
  9172. * are dynamic, the others are mostly based on the hypervisor
  9173. * architecture and the guest's CPUID. Do not touch the
  9174. * dynamic bits.
  9175. */
  9176. u32 mask =
  9177. SECONDARY_EXEC_SHADOW_VMCS |
  9178. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  9179. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9180. SECONDARY_EXEC_DESC;
  9181. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  9182. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  9183. (new_ctl & ~mask) | (cur_ctl & mask));
  9184. }
  9185. /*
  9186. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  9187. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  9188. */
  9189. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  9190. {
  9191. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9192. struct kvm_cpuid_entry2 *entry;
  9193. vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
  9194. vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
  9195. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  9196. if (entry && (entry->_reg & (_cpuid_mask))) \
  9197. vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
  9198. } while (0)
  9199. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  9200. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  9201. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  9202. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  9203. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  9204. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  9205. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  9206. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  9207. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  9208. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  9209. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  9210. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  9211. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  9212. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  9213. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  9214. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  9215. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  9216. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  9217. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  9218. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  9219. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  9220. #undef cr4_fixed1_update
  9221. }
  9222. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  9223. {
  9224. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9225. if (cpu_has_secondary_exec_ctrls()) {
  9226. vmx_compute_secondary_exec_control(vmx);
  9227. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  9228. }
  9229. if (nested_vmx_allowed(vcpu))
  9230. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  9231. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9232. else
  9233. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  9234. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9235. if (nested_vmx_allowed(vcpu))
  9236. nested_vmx_cr_fixed1_bits_update(vcpu);
  9237. }
  9238. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  9239. {
  9240. if (func == 1 && nested)
  9241. entry->ecx |= bit(X86_FEATURE_VMX);
  9242. }
  9243. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  9244. struct x86_exception *fault)
  9245. {
  9246. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9247. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9248. u32 exit_reason;
  9249. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  9250. if (vmx->nested.pml_full) {
  9251. exit_reason = EXIT_REASON_PML_FULL;
  9252. vmx->nested.pml_full = false;
  9253. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  9254. } else if (fault->error_code & PFERR_RSVD_MASK)
  9255. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  9256. else
  9257. exit_reason = EXIT_REASON_EPT_VIOLATION;
  9258. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  9259. vmcs12->guest_physical_address = fault->address;
  9260. }
  9261. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  9262. {
  9263. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  9264. }
  9265. /* Callbacks for nested_ept_init_mmu_context: */
  9266. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  9267. {
  9268. /* return the page table to be shadowed - in our case, EPT12 */
  9269. return get_vmcs12(vcpu)->ept_pointer;
  9270. }
  9271. static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  9272. {
  9273. WARN_ON(mmu_is_nested(vcpu));
  9274. if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
  9275. return 1;
  9276. kvm_init_shadow_ept_mmu(vcpu,
  9277. to_vmx(vcpu)->nested.msrs.ept_caps &
  9278. VMX_EPT_EXECUTE_ONLY_BIT,
  9279. nested_ept_ad_enabled(vcpu),
  9280. nested_ept_get_cr3(vcpu));
  9281. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  9282. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  9283. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  9284. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  9285. return 0;
  9286. }
  9287. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  9288. {
  9289. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  9290. }
  9291. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  9292. u16 error_code)
  9293. {
  9294. bool inequality, bit;
  9295. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  9296. inequality =
  9297. (error_code & vmcs12->page_fault_error_code_mask) !=
  9298. vmcs12->page_fault_error_code_match;
  9299. return inequality ^ bit;
  9300. }
  9301. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  9302. struct x86_exception *fault)
  9303. {
  9304. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9305. WARN_ON(!is_guest_mode(vcpu));
  9306. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  9307. !to_vmx(vcpu)->nested.nested_run_pending) {
  9308. vmcs12->vm_exit_intr_error_code = fault->error_code;
  9309. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  9310. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  9311. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  9312. fault->address);
  9313. } else {
  9314. kvm_inject_page_fault(vcpu, fault);
  9315. }
  9316. }
  9317. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9318. struct vmcs12 *vmcs12);
  9319. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
  9320. {
  9321. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9322. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9323. struct page *page;
  9324. u64 hpa;
  9325. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9326. /*
  9327. * Translate L1 physical address to host physical
  9328. * address for vmcs02. Keep the page pinned, so this
  9329. * physical address remains valid. We keep a reference
  9330. * to it so we can release it later.
  9331. */
  9332. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  9333. kvm_release_page_dirty(vmx->nested.apic_access_page);
  9334. vmx->nested.apic_access_page = NULL;
  9335. }
  9336. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  9337. /*
  9338. * If translation failed, no matter: This feature asks
  9339. * to exit when accessing the given address, and if it
  9340. * can never be accessed, this feature won't do
  9341. * anything anyway.
  9342. */
  9343. if (!is_error_page(page)) {
  9344. vmx->nested.apic_access_page = page;
  9345. hpa = page_to_phys(vmx->nested.apic_access_page);
  9346. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  9347. } else {
  9348. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  9349. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  9350. }
  9351. }
  9352. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  9353. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  9354. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  9355. vmx->nested.virtual_apic_page = NULL;
  9356. }
  9357. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  9358. /*
  9359. * If translation failed, VM entry will fail because
  9360. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  9361. * Failing the vm entry is _not_ what the processor
  9362. * does but it's basically the only possibility we
  9363. * have. We could still enter the guest if CR8 load
  9364. * exits are enabled, CR8 store exits are enabled, and
  9365. * virtualize APIC access is disabled; in this case
  9366. * the processor would never use the TPR shadow and we
  9367. * could simply clear the bit from the execution
  9368. * control. But such a configuration is useless, so
  9369. * let's keep the code simple.
  9370. */
  9371. if (!is_error_page(page)) {
  9372. vmx->nested.virtual_apic_page = page;
  9373. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  9374. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  9375. }
  9376. }
  9377. if (nested_cpu_has_posted_intr(vmcs12)) {
  9378. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  9379. kunmap(vmx->nested.pi_desc_page);
  9380. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  9381. vmx->nested.pi_desc_page = NULL;
  9382. }
  9383. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  9384. if (is_error_page(page))
  9385. return;
  9386. vmx->nested.pi_desc_page = page;
  9387. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  9388. vmx->nested.pi_desc =
  9389. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  9390. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9391. (PAGE_SIZE - 1)));
  9392. vmcs_write64(POSTED_INTR_DESC_ADDR,
  9393. page_to_phys(vmx->nested.pi_desc_page) +
  9394. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9395. (PAGE_SIZE - 1)));
  9396. }
  9397. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  9398. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  9399. CPU_BASED_USE_MSR_BITMAPS);
  9400. else
  9401. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  9402. CPU_BASED_USE_MSR_BITMAPS);
  9403. }
  9404. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  9405. {
  9406. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  9407. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9408. if (vcpu->arch.virtual_tsc_khz == 0)
  9409. return;
  9410. /* Make sure short timeouts reliably trigger an immediate vmexit.
  9411. * hrtimer_start does not guarantee this. */
  9412. if (preemption_timeout <= 1) {
  9413. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  9414. return;
  9415. }
  9416. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  9417. preemption_timeout *= 1000000;
  9418. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  9419. hrtimer_start(&vmx->nested.preemption_timer,
  9420. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  9421. }
  9422. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  9423. struct vmcs12 *vmcs12)
  9424. {
  9425. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  9426. return 0;
  9427. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  9428. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  9429. return -EINVAL;
  9430. return 0;
  9431. }
  9432. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  9433. struct vmcs12 *vmcs12)
  9434. {
  9435. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9436. return 0;
  9437. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  9438. return -EINVAL;
  9439. return 0;
  9440. }
  9441. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  9442. struct vmcs12 *vmcs12)
  9443. {
  9444. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9445. return 0;
  9446. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  9447. return -EINVAL;
  9448. return 0;
  9449. }
  9450. /*
  9451. * Merge L0's and L1's MSR bitmap, return false to indicate that
  9452. * we do not use the hardware.
  9453. */
  9454. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9455. struct vmcs12 *vmcs12)
  9456. {
  9457. int msr;
  9458. struct page *page;
  9459. unsigned long *msr_bitmap_l1;
  9460. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  9461. /*
  9462. * pred_cmd & spec_ctrl are trying to verify two things:
  9463. *
  9464. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  9465. * ensures that we do not accidentally generate an L02 MSR bitmap
  9466. * from the L12 MSR bitmap that is too permissive.
  9467. * 2. That L1 or L2s have actually used the MSR. This avoids
  9468. * unnecessarily merging of the bitmap if the MSR is unused. This
  9469. * works properly because we only update the L01 MSR bitmap lazily.
  9470. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  9471. * updated to reflect this when L1 (or its L2s) actually write to
  9472. * the MSR.
  9473. */
  9474. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  9475. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  9476. /* Nothing to do if the MSR bitmap is not in use. */
  9477. if (!cpu_has_vmx_msr_bitmap() ||
  9478. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9479. return false;
  9480. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9481. !pred_cmd && !spec_ctrl)
  9482. return false;
  9483. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  9484. if (is_error_page(page))
  9485. return false;
  9486. msr_bitmap_l1 = (unsigned long *)kmap(page);
  9487. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  9488. /*
  9489. * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
  9490. * just lets the processor take the value from the virtual-APIC page;
  9491. * take those 256 bits directly from the L1 bitmap.
  9492. */
  9493. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  9494. unsigned word = msr / BITS_PER_LONG;
  9495. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  9496. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  9497. }
  9498. } else {
  9499. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  9500. unsigned word = msr / BITS_PER_LONG;
  9501. msr_bitmap_l0[word] = ~0;
  9502. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  9503. }
  9504. }
  9505. nested_vmx_disable_intercept_for_msr(
  9506. msr_bitmap_l1, msr_bitmap_l0,
  9507. X2APIC_MSR(APIC_TASKPRI),
  9508. MSR_TYPE_W);
  9509. if (nested_cpu_has_vid(vmcs12)) {
  9510. nested_vmx_disable_intercept_for_msr(
  9511. msr_bitmap_l1, msr_bitmap_l0,
  9512. X2APIC_MSR(APIC_EOI),
  9513. MSR_TYPE_W);
  9514. nested_vmx_disable_intercept_for_msr(
  9515. msr_bitmap_l1, msr_bitmap_l0,
  9516. X2APIC_MSR(APIC_SELF_IPI),
  9517. MSR_TYPE_W);
  9518. }
  9519. if (spec_ctrl)
  9520. nested_vmx_disable_intercept_for_msr(
  9521. msr_bitmap_l1, msr_bitmap_l0,
  9522. MSR_IA32_SPEC_CTRL,
  9523. MSR_TYPE_R | MSR_TYPE_W);
  9524. if (pred_cmd)
  9525. nested_vmx_disable_intercept_for_msr(
  9526. msr_bitmap_l1, msr_bitmap_l0,
  9527. MSR_IA32_PRED_CMD,
  9528. MSR_TYPE_W);
  9529. kunmap(page);
  9530. kvm_release_page_clean(page);
  9531. return true;
  9532. }
  9533. static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
  9534. struct vmcs12 *vmcs12)
  9535. {
  9536. struct vmcs12 *shadow;
  9537. struct page *page;
  9538. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  9539. vmcs12->vmcs_link_pointer == -1ull)
  9540. return;
  9541. shadow = get_shadow_vmcs12(vcpu);
  9542. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  9543. memcpy(shadow, kmap(page), VMCS12_SIZE);
  9544. kunmap(page);
  9545. kvm_release_page_clean(page);
  9546. }
  9547. static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
  9548. struct vmcs12 *vmcs12)
  9549. {
  9550. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9551. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  9552. vmcs12->vmcs_link_pointer == -1ull)
  9553. return;
  9554. kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
  9555. get_shadow_vmcs12(vcpu), VMCS12_SIZE);
  9556. }
  9557. static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
  9558. struct vmcs12 *vmcs12)
  9559. {
  9560. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  9561. !page_address_valid(vcpu, vmcs12->apic_access_addr))
  9562. return -EINVAL;
  9563. else
  9564. return 0;
  9565. }
  9566. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  9567. struct vmcs12 *vmcs12)
  9568. {
  9569. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9570. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  9571. !nested_cpu_has_vid(vmcs12) &&
  9572. !nested_cpu_has_posted_intr(vmcs12))
  9573. return 0;
  9574. /*
  9575. * If virtualize x2apic mode is enabled,
  9576. * virtualize apic access must be disabled.
  9577. */
  9578. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9579. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  9580. return -EINVAL;
  9581. /*
  9582. * If virtual interrupt delivery is enabled,
  9583. * we must exit on external interrupts.
  9584. */
  9585. if (nested_cpu_has_vid(vmcs12) &&
  9586. !nested_exit_on_intr(vcpu))
  9587. return -EINVAL;
  9588. /*
  9589. * bits 15:8 should be zero in posted_intr_nv,
  9590. * the descriptor address has been already checked
  9591. * in nested_get_vmcs12_pages.
  9592. */
  9593. if (nested_cpu_has_posted_intr(vmcs12) &&
  9594. (!nested_cpu_has_vid(vmcs12) ||
  9595. !nested_exit_intr_ack_set(vcpu) ||
  9596. vmcs12->posted_intr_nv & 0xff00))
  9597. return -EINVAL;
  9598. /* tpr shadow is needed by all apicv features. */
  9599. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9600. return -EINVAL;
  9601. return 0;
  9602. }
  9603. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  9604. unsigned long count_field,
  9605. unsigned long addr_field)
  9606. {
  9607. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9608. int maxphyaddr;
  9609. u64 count, addr;
  9610. if (vmcs12_read_any(vmcs12, count_field, &count) ||
  9611. vmcs12_read_any(vmcs12, addr_field, &addr)) {
  9612. WARN_ON(1);
  9613. return -EINVAL;
  9614. }
  9615. if (count == 0)
  9616. return 0;
  9617. maxphyaddr = cpuid_maxphyaddr(vcpu);
  9618. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  9619. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  9620. pr_debug_ratelimited(
  9621. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  9622. addr_field, maxphyaddr, count, addr);
  9623. return -EINVAL;
  9624. }
  9625. return 0;
  9626. }
  9627. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  9628. struct vmcs12 *vmcs12)
  9629. {
  9630. if (vmcs12->vm_exit_msr_load_count == 0 &&
  9631. vmcs12->vm_exit_msr_store_count == 0 &&
  9632. vmcs12->vm_entry_msr_load_count == 0)
  9633. return 0; /* Fast path */
  9634. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  9635. VM_EXIT_MSR_LOAD_ADDR) ||
  9636. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  9637. VM_EXIT_MSR_STORE_ADDR) ||
  9638. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  9639. VM_ENTRY_MSR_LOAD_ADDR))
  9640. return -EINVAL;
  9641. return 0;
  9642. }
  9643. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  9644. struct vmcs12 *vmcs12)
  9645. {
  9646. u64 address = vmcs12->pml_address;
  9647. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  9648. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
  9649. if (!nested_cpu_has_ept(vmcs12) ||
  9650. !IS_ALIGNED(address, 4096) ||
  9651. address >> maxphyaddr)
  9652. return -EINVAL;
  9653. }
  9654. return 0;
  9655. }
  9656. static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
  9657. struct vmcs12 *vmcs12)
  9658. {
  9659. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  9660. return 0;
  9661. if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
  9662. !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
  9663. return -EINVAL;
  9664. return 0;
  9665. }
  9666. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  9667. struct vmx_msr_entry *e)
  9668. {
  9669. /* x2APIC MSR accesses are not allowed */
  9670. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  9671. return -EINVAL;
  9672. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  9673. e->index == MSR_IA32_UCODE_REV)
  9674. return -EINVAL;
  9675. if (e->reserved != 0)
  9676. return -EINVAL;
  9677. return 0;
  9678. }
  9679. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  9680. struct vmx_msr_entry *e)
  9681. {
  9682. if (e->index == MSR_FS_BASE ||
  9683. e->index == MSR_GS_BASE ||
  9684. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  9685. nested_vmx_msr_check_common(vcpu, e))
  9686. return -EINVAL;
  9687. return 0;
  9688. }
  9689. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  9690. struct vmx_msr_entry *e)
  9691. {
  9692. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  9693. nested_vmx_msr_check_common(vcpu, e))
  9694. return -EINVAL;
  9695. return 0;
  9696. }
  9697. /*
  9698. * Load guest's/host's msr at nested entry/exit.
  9699. * return 0 for success, entry index for failure.
  9700. */
  9701. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9702. {
  9703. u32 i;
  9704. struct vmx_msr_entry e;
  9705. struct msr_data msr;
  9706. msr.host_initiated = false;
  9707. for (i = 0; i < count; i++) {
  9708. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  9709. &e, sizeof(e))) {
  9710. pr_debug_ratelimited(
  9711. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9712. __func__, i, gpa + i * sizeof(e));
  9713. goto fail;
  9714. }
  9715. if (nested_vmx_load_msr_check(vcpu, &e)) {
  9716. pr_debug_ratelimited(
  9717. "%s check failed (%u, 0x%x, 0x%x)\n",
  9718. __func__, i, e.index, e.reserved);
  9719. goto fail;
  9720. }
  9721. msr.index = e.index;
  9722. msr.data = e.value;
  9723. if (kvm_set_msr(vcpu, &msr)) {
  9724. pr_debug_ratelimited(
  9725. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9726. __func__, i, e.index, e.value);
  9727. goto fail;
  9728. }
  9729. }
  9730. return 0;
  9731. fail:
  9732. return i + 1;
  9733. }
  9734. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9735. {
  9736. u32 i;
  9737. struct vmx_msr_entry e;
  9738. for (i = 0; i < count; i++) {
  9739. struct msr_data msr_info;
  9740. if (kvm_vcpu_read_guest(vcpu,
  9741. gpa + i * sizeof(e),
  9742. &e, 2 * sizeof(u32))) {
  9743. pr_debug_ratelimited(
  9744. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9745. __func__, i, gpa + i * sizeof(e));
  9746. return -EINVAL;
  9747. }
  9748. if (nested_vmx_store_msr_check(vcpu, &e)) {
  9749. pr_debug_ratelimited(
  9750. "%s check failed (%u, 0x%x, 0x%x)\n",
  9751. __func__, i, e.index, e.reserved);
  9752. return -EINVAL;
  9753. }
  9754. msr_info.host_initiated = false;
  9755. msr_info.index = e.index;
  9756. if (kvm_get_msr(vcpu, &msr_info)) {
  9757. pr_debug_ratelimited(
  9758. "%s cannot read MSR (%u, 0x%x)\n",
  9759. __func__, i, e.index);
  9760. return -EINVAL;
  9761. }
  9762. if (kvm_vcpu_write_guest(vcpu,
  9763. gpa + i * sizeof(e) +
  9764. offsetof(struct vmx_msr_entry, value),
  9765. &msr_info.data, sizeof(msr_info.data))) {
  9766. pr_debug_ratelimited(
  9767. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9768. __func__, i, e.index, msr_info.data);
  9769. return -EINVAL;
  9770. }
  9771. }
  9772. return 0;
  9773. }
  9774. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  9775. {
  9776. unsigned long invalid_mask;
  9777. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  9778. return (val & invalid_mask) == 0;
  9779. }
  9780. /*
  9781. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  9782. * emulating VM entry into a guest with EPT enabled.
  9783. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9784. * is assigned to entry_failure_code on failure.
  9785. */
  9786. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  9787. u32 *entry_failure_code)
  9788. {
  9789. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  9790. if (!nested_cr3_valid(vcpu, cr3)) {
  9791. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9792. return 1;
  9793. }
  9794. /*
  9795. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  9796. * must not be dereferenced.
  9797. */
  9798. if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
  9799. !nested_ept) {
  9800. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  9801. *entry_failure_code = ENTRY_FAIL_PDPTE;
  9802. return 1;
  9803. }
  9804. }
  9805. }
  9806. if (!nested_ept)
  9807. kvm_mmu_new_cr3(vcpu, cr3);
  9808. vcpu->arch.cr3 = cr3;
  9809. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  9810. kvm_init_mmu(vcpu, false);
  9811. return 0;
  9812. }
  9813. static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9814. {
  9815. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9816. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  9817. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  9818. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  9819. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  9820. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  9821. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  9822. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  9823. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  9824. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  9825. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  9826. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  9827. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  9828. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  9829. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  9830. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  9831. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  9832. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  9833. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  9834. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  9835. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  9836. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  9837. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  9838. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  9839. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  9840. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  9841. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  9842. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  9843. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  9844. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  9845. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  9846. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  9847. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  9848. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  9849. vmcs12->guest_pending_dbg_exceptions);
  9850. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  9851. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  9852. if (nested_cpu_has_xsaves(vmcs12))
  9853. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  9854. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  9855. if (cpu_has_vmx_posted_intr())
  9856. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  9857. /*
  9858. * Whether page-faults are trapped is determined by a combination of
  9859. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  9860. * If enable_ept, L0 doesn't care about page faults and we should
  9861. * set all of these to L1's desires. However, if !enable_ept, L0 does
  9862. * care about (at least some) page faults, and because it is not easy
  9863. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  9864. * to exit on each and every L2 page fault. This is done by setting
  9865. * MASK=MATCH=0 and (see below) EB.PF=1.
  9866. * Note that below we don't need special code to set EB.PF beyond the
  9867. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  9868. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  9869. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  9870. */
  9871. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  9872. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  9873. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  9874. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  9875. /* All VMFUNCs are currently emulated through L0 vmexits. */
  9876. if (cpu_has_vmx_vmfunc())
  9877. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  9878. if (cpu_has_vmx_apicv()) {
  9879. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  9880. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  9881. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  9882. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  9883. }
  9884. /*
  9885. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  9886. * Some constant fields are set here by vmx_set_constant_host_state().
  9887. * Other fields are different per CPU, and will be set later when
  9888. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  9889. */
  9890. vmx_set_constant_host_state(vmx);
  9891. /*
  9892. * Set the MSR load/store lists to match L0's settings.
  9893. */
  9894. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  9895. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9896. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  9897. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9898. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  9899. set_cr4_guest_host_mask(vmx);
  9900. if (vmx_mpx_supported())
  9901. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  9902. if (enable_vpid) {
  9903. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  9904. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  9905. else
  9906. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  9907. }
  9908. /*
  9909. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  9910. */
  9911. if (enable_ept) {
  9912. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  9913. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  9914. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  9915. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  9916. }
  9917. if (cpu_has_vmx_msr_bitmap())
  9918. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  9919. }
  9920. /*
  9921. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  9922. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  9923. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  9924. * guest in a way that will both be appropriate to L1's requests, and our
  9925. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  9926. * function also has additional necessary side-effects, like setting various
  9927. * vcpu->arch fields.
  9928. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9929. * is assigned to entry_failure_code on failure.
  9930. */
  9931. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9932. u32 *entry_failure_code)
  9933. {
  9934. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9935. u32 exec_control, vmcs12_exec_ctrl;
  9936. if (vmx->nested.dirty_vmcs12) {
  9937. prepare_vmcs02_full(vcpu, vmcs12);
  9938. vmx->nested.dirty_vmcs12 = false;
  9939. }
  9940. /*
  9941. * First, the fields that are shadowed. This must be kept in sync
  9942. * with vmx_shadow_fields.h.
  9943. */
  9944. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  9945. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  9946. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  9947. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  9948. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  9949. /*
  9950. * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
  9951. * HOST_FS_BASE, HOST_GS_BASE.
  9952. */
  9953. if (vmx->nested.nested_run_pending &&
  9954. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  9955. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  9956. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  9957. } else {
  9958. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  9959. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  9960. }
  9961. if (vmx->nested.nested_run_pending) {
  9962. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  9963. vmcs12->vm_entry_intr_info_field);
  9964. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  9965. vmcs12->vm_entry_exception_error_code);
  9966. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  9967. vmcs12->vm_entry_instruction_len);
  9968. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  9969. vmcs12->guest_interruptibility_info);
  9970. vmx->loaded_vmcs->nmi_known_unmasked =
  9971. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  9972. } else {
  9973. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9974. }
  9975. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  9976. exec_control = vmcs12->pin_based_vm_exec_control;
  9977. /* Preemption timer setting is only taken from vmcs01. */
  9978. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9979. exec_control |= vmcs_config.pin_based_exec_ctrl;
  9980. if (vmx->hv_deadline_tsc == -1)
  9981. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9982. /* Posted interrupts setting is only taken from vmcs12. */
  9983. if (nested_cpu_has_posted_intr(vmcs12)) {
  9984. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  9985. vmx->nested.pi_pending = false;
  9986. } else {
  9987. exec_control &= ~PIN_BASED_POSTED_INTR;
  9988. }
  9989. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  9990. vmx->nested.preemption_timer_expired = false;
  9991. if (nested_cpu_has_preemption_timer(vmcs12))
  9992. vmx_start_preemption_timer(vcpu);
  9993. if (cpu_has_secondary_exec_ctrls()) {
  9994. exec_control = vmx->secondary_exec_control;
  9995. /* Take the following fields only from vmcs12 */
  9996. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9997. SECONDARY_EXEC_ENABLE_INVPCID |
  9998. SECONDARY_EXEC_RDTSCP |
  9999. SECONDARY_EXEC_XSAVES |
  10000. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  10001. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  10002. SECONDARY_EXEC_ENABLE_VMFUNC);
  10003. if (nested_cpu_has(vmcs12,
  10004. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  10005. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  10006. ~SECONDARY_EXEC_ENABLE_PML;
  10007. exec_control |= vmcs12_exec_ctrl;
  10008. }
  10009. /* VMCS shadowing for L2 is emulated for now */
  10010. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  10011. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  10012. vmcs_write16(GUEST_INTR_STATUS,
  10013. vmcs12->guest_intr_status);
  10014. /*
  10015. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  10016. * nested_get_vmcs12_pages will either fix it up or
  10017. * remove the VM execution control.
  10018. */
  10019. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  10020. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  10021. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  10022. }
  10023. /*
  10024. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  10025. * entry, but only if the current (host) sp changed from the value
  10026. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  10027. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  10028. * here we just force the write to happen on entry.
  10029. */
  10030. vmx->host_rsp = 0;
  10031. exec_control = vmx_exec_control(vmx); /* L0's desires */
  10032. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  10033. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  10034. exec_control &= ~CPU_BASED_TPR_SHADOW;
  10035. exec_control |= vmcs12->cpu_based_vm_exec_control;
  10036. /*
  10037. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  10038. * nested_get_vmcs12_pages can't fix it up, the illegal value
  10039. * will result in a VM entry failure.
  10040. */
  10041. if (exec_control & CPU_BASED_TPR_SHADOW) {
  10042. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  10043. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  10044. } else {
  10045. #ifdef CONFIG_X86_64
  10046. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  10047. CPU_BASED_CR8_STORE_EXITING;
  10048. #endif
  10049. }
  10050. /*
  10051. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  10052. * for I/O port accesses.
  10053. */
  10054. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  10055. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  10056. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  10057. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  10058. * bitwise-or of what L1 wants to trap for L2, and what we want to
  10059. * trap. Note that CR0.TS also needs updating - we do this later.
  10060. */
  10061. update_exception_bitmap(vcpu);
  10062. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  10063. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  10064. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  10065. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  10066. * bits are further modified by vmx_set_efer() below.
  10067. */
  10068. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  10069. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  10070. * emulated by vmx_set_efer(), below.
  10071. */
  10072. vm_entry_controls_init(vmx,
  10073. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  10074. ~VM_ENTRY_IA32E_MODE) |
  10075. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  10076. if (vmx->nested.nested_run_pending &&
  10077. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  10078. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  10079. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  10080. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  10081. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  10082. }
  10083. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10084. if (kvm_has_tsc_control)
  10085. decache_tsc_multiplier(vmx);
  10086. if (enable_vpid) {
  10087. /*
  10088. * There is no direct mapping between vpid02 and vpid12, the
  10089. * vpid02 is per-vCPU for L0 and reused while the value of
  10090. * vpid12 is changed w/ one invvpid during nested vmentry.
  10091. * The vpid12 is allocated by L1 for L2, so it will not
  10092. * influence global bitmap(for vpid01 and vpid02 allocation)
  10093. * even if spawn a lot of nested vCPUs.
  10094. */
  10095. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
  10096. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  10097. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  10098. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  10099. }
  10100. } else {
  10101. vmx_flush_tlb(vcpu, true);
  10102. }
  10103. }
  10104. if (enable_pml) {
  10105. /*
  10106. * Conceptually we want to copy the PML address and index from
  10107. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  10108. * since we always flush the log on each vmexit, this happens
  10109. * to be equivalent to simply resetting the fields in vmcs02.
  10110. */
  10111. ASSERT(vmx->pml_pg);
  10112. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  10113. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  10114. }
  10115. if (nested_cpu_has_ept(vmcs12)) {
  10116. if (nested_ept_init_mmu_context(vcpu)) {
  10117. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10118. return 1;
  10119. }
  10120. } else if (nested_cpu_has2(vmcs12,
  10121. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10122. vmx_flush_tlb(vcpu, true);
  10123. }
  10124. /*
  10125. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  10126. * bits which we consider mandatory enabled.
  10127. * The CR0_READ_SHADOW is what L2 should have expected to read given
  10128. * the specifications by L1; It's not enough to take
  10129. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  10130. * have more bits than L1 expected.
  10131. */
  10132. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  10133. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  10134. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  10135. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  10136. if (vmx->nested.nested_run_pending &&
  10137. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  10138. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  10139. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  10140. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  10141. else
  10142. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  10143. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  10144. vmx_set_efer(vcpu, vcpu->arch.efer);
  10145. /*
  10146. * Guest state is invalid and unrestricted guest is disabled,
  10147. * which means L1 attempted VMEntry to L2 with invalid state.
  10148. * Fail the VMEntry.
  10149. */
  10150. if (vmx->emulation_required) {
  10151. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10152. return 1;
  10153. }
  10154. /* Shadow page tables on either EPT or shadow page tables. */
  10155. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  10156. entry_failure_code))
  10157. return 1;
  10158. if (!enable_ept)
  10159. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  10160. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  10161. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  10162. return 0;
  10163. }
  10164. static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
  10165. {
  10166. if (!nested_cpu_has_nmi_exiting(vmcs12) &&
  10167. nested_cpu_has_virtual_nmis(vmcs12))
  10168. return -EINVAL;
  10169. if (!nested_cpu_has_virtual_nmis(vmcs12) &&
  10170. nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
  10171. return -EINVAL;
  10172. return 0;
  10173. }
  10174. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10175. {
  10176. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10177. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  10178. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  10179. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10180. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  10181. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10182. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  10183. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10184. if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
  10185. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10186. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  10187. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10188. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  10189. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10190. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  10191. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10192. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  10193. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10194. if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
  10195. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10196. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  10197. vmx->nested.msrs.procbased_ctls_low,
  10198. vmx->nested.msrs.procbased_ctls_high) ||
  10199. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  10200. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  10201. vmx->nested.msrs.secondary_ctls_low,
  10202. vmx->nested.msrs.secondary_ctls_high)) ||
  10203. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  10204. vmx->nested.msrs.pinbased_ctls_low,
  10205. vmx->nested.msrs.pinbased_ctls_high) ||
  10206. !vmx_control_verify(vmcs12->vm_exit_controls,
  10207. vmx->nested.msrs.exit_ctls_low,
  10208. vmx->nested.msrs.exit_ctls_high) ||
  10209. !vmx_control_verify(vmcs12->vm_entry_controls,
  10210. vmx->nested.msrs.entry_ctls_low,
  10211. vmx->nested.msrs.entry_ctls_high))
  10212. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10213. if (nested_vmx_check_nmi_controls(vmcs12))
  10214. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10215. if (nested_cpu_has_vmfunc(vmcs12)) {
  10216. if (vmcs12->vm_function_control &
  10217. ~vmx->nested.msrs.vmfunc_controls)
  10218. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10219. if (nested_cpu_has_eptp_switching(vmcs12)) {
  10220. if (!nested_cpu_has_ept(vmcs12) ||
  10221. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  10222. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10223. }
  10224. }
  10225. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  10226. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10227. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  10228. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  10229. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  10230. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  10231. /*
  10232. * From the Intel SDM, volume 3:
  10233. * Fields relevant to VM-entry event injection must be set properly.
  10234. * These fields are the VM-entry interruption-information field, the
  10235. * VM-entry exception error code, and the VM-entry instruction length.
  10236. */
  10237. if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
  10238. u32 intr_info = vmcs12->vm_entry_intr_info_field;
  10239. u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
  10240. u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
  10241. bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
  10242. bool should_have_error_code;
  10243. bool urg = nested_cpu_has2(vmcs12,
  10244. SECONDARY_EXEC_UNRESTRICTED_GUEST);
  10245. bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
  10246. /* VM-entry interruption-info field: interruption type */
  10247. if (intr_type == INTR_TYPE_RESERVED ||
  10248. (intr_type == INTR_TYPE_OTHER_EVENT &&
  10249. !nested_cpu_supports_monitor_trap_flag(vcpu)))
  10250. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10251. /* VM-entry interruption-info field: vector */
  10252. if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
  10253. (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
  10254. (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
  10255. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10256. /* VM-entry interruption-info field: deliver error code */
  10257. should_have_error_code =
  10258. intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
  10259. x86_exception_has_error_code(vector);
  10260. if (has_error_code != should_have_error_code)
  10261. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10262. /* VM-entry exception error code */
  10263. if (has_error_code &&
  10264. vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
  10265. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10266. /* VM-entry interruption-info field: reserved bits */
  10267. if (intr_info & INTR_INFO_RESVD_BITS_MASK)
  10268. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10269. /* VM-entry instruction length */
  10270. switch (intr_type) {
  10271. case INTR_TYPE_SOFT_EXCEPTION:
  10272. case INTR_TYPE_SOFT_INTR:
  10273. case INTR_TYPE_PRIV_SW_EXCEPTION:
  10274. if ((vmcs12->vm_entry_instruction_len > 15) ||
  10275. (vmcs12->vm_entry_instruction_len == 0 &&
  10276. !nested_cpu_has_zero_length_injection(vcpu)))
  10277. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10278. }
  10279. }
  10280. return 0;
  10281. }
  10282. static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
  10283. struct vmcs12 *vmcs12)
  10284. {
  10285. int r;
  10286. struct page *page;
  10287. struct vmcs12 *shadow;
  10288. if (vmcs12->vmcs_link_pointer == -1ull)
  10289. return 0;
  10290. if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
  10291. return -EINVAL;
  10292. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  10293. if (is_error_page(page))
  10294. return -EINVAL;
  10295. r = 0;
  10296. shadow = kmap(page);
  10297. if (shadow->hdr.revision_id != VMCS12_REVISION ||
  10298. shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
  10299. r = -EINVAL;
  10300. kunmap(page);
  10301. kvm_release_page_clean(page);
  10302. return r;
  10303. }
  10304. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10305. u32 *exit_qual)
  10306. {
  10307. bool ia32e;
  10308. *exit_qual = ENTRY_FAIL_DEFAULT;
  10309. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  10310. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  10311. return 1;
  10312. if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
  10313. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  10314. return 1;
  10315. }
  10316. /*
  10317. * If the load IA32_EFER VM-entry control is 1, the following checks
  10318. * are performed on the field for the IA32_EFER MSR:
  10319. * - Bits reserved in the IA32_EFER MSR must be 0.
  10320. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  10321. * the IA-32e mode guest VM-exit control. It must also be identical
  10322. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  10323. * CR0.PG) is 1.
  10324. */
  10325. if (to_vmx(vcpu)->nested.nested_run_pending &&
  10326. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  10327. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  10328. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  10329. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  10330. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  10331. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  10332. return 1;
  10333. }
  10334. /*
  10335. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  10336. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  10337. * the values of the LMA and LME bits in the field must each be that of
  10338. * the host address-space size VM-exit control.
  10339. */
  10340. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  10341. ia32e = (vmcs12->vm_exit_controls &
  10342. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  10343. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  10344. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  10345. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  10346. return 1;
  10347. }
  10348. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  10349. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  10350. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  10351. return 1;
  10352. return 0;
  10353. }
  10354. /*
  10355. * If exit_qual is NULL, this is being called from state restore (either RSM
  10356. * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
  10357. */
  10358. static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
  10359. {
  10360. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10361. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10362. bool from_vmentry = !!exit_qual;
  10363. u32 dummy_exit_qual;
  10364. int r = 0;
  10365. enter_guest_mode(vcpu);
  10366. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  10367. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10368. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  10369. vmx_segment_cache_clear(vmx);
  10370. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10371. vcpu->arch.tsc_offset += vmcs12->tsc_offset;
  10372. r = EXIT_REASON_INVALID_STATE;
  10373. if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
  10374. goto fail;
  10375. if (from_vmentry) {
  10376. nested_get_vmcs12_pages(vcpu);
  10377. r = EXIT_REASON_MSR_LOAD_FAIL;
  10378. *exit_qual = nested_vmx_load_msr(vcpu,
  10379. vmcs12->vm_entry_msr_load_addr,
  10380. vmcs12->vm_entry_msr_load_count);
  10381. if (*exit_qual)
  10382. goto fail;
  10383. } else {
  10384. /*
  10385. * The MMU is not initialized to point at the right entities yet and
  10386. * "get pages" would need to read data from the guest (i.e. we will
  10387. * need to perform gpa to hpa translation). Request a call
  10388. * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
  10389. * have already been set at vmentry time and should not be reset.
  10390. */
  10391. kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
  10392. }
  10393. /*
  10394. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  10395. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  10396. * returned as far as L1 is concerned. It will only return (and set
  10397. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  10398. */
  10399. return 0;
  10400. fail:
  10401. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10402. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  10403. leave_guest_mode(vcpu);
  10404. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10405. return r;
  10406. }
  10407. /*
  10408. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  10409. * for running an L2 nested guest.
  10410. */
  10411. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  10412. {
  10413. struct vmcs12 *vmcs12;
  10414. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10415. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  10416. u32 exit_qual;
  10417. int ret;
  10418. if (!nested_vmx_check_permission(vcpu))
  10419. return 1;
  10420. if (!nested_vmx_check_vmcs12(vcpu))
  10421. goto out;
  10422. vmcs12 = get_vmcs12(vcpu);
  10423. /*
  10424. * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
  10425. * that there *is* a valid VMCS pointer, RFLAGS.CF is set
  10426. * rather than RFLAGS.ZF, and no error number is stored to the
  10427. * VM-instruction error field.
  10428. */
  10429. if (vmcs12->hdr.shadow_vmcs) {
  10430. nested_vmx_failInvalid(vcpu);
  10431. goto out;
  10432. }
  10433. if (enable_shadow_vmcs)
  10434. copy_shadow_to_vmcs12(vmx);
  10435. /*
  10436. * The nested entry process starts with enforcing various prerequisites
  10437. * on vmcs12 as required by the Intel SDM, and act appropriately when
  10438. * they fail: As the SDM explains, some conditions should cause the
  10439. * instruction to fail, while others will cause the instruction to seem
  10440. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  10441. * To speed up the normal (success) code path, we should avoid checking
  10442. * for misconfigurations which will anyway be caught by the processor
  10443. * when using the merged vmcs02.
  10444. */
  10445. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
  10446. nested_vmx_failValid(vcpu,
  10447. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  10448. goto out;
  10449. }
  10450. if (vmcs12->launch_state == launch) {
  10451. nested_vmx_failValid(vcpu,
  10452. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  10453. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  10454. goto out;
  10455. }
  10456. ret = check_vmentry_prereqs(vcpu, vmcs12);
  10457. if (ret) {
  10458. nested_vmx_failValid(vcpu, ret);
  10459. goto out;
  10460. }
  10461. /*
  10462. * After this point, the trap flag no longer triggers a singlestep trap
  10463. * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
  10464. * This is not 100% correct; for performance reasons, we delegate most
  10465. * of the checks on host state to the processor. If those fail,
  10466. * the singlestep trap is missed.
  10467. */
  10468. skip_emulated_instruction(vcpu);
  10469. ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
  10470. if (ret) {
  10471. nested_vmx_entry_failure(vcpu, vmcs12,
  10472. EXIT_REASON_INVALID_STATE, exit_qual);
  10473. return 1;
  10474. }
  10475. /*
  10476. * We're finally done with prerequisite checking, and can start with
  10477. * the nested entry.
  10478. */
  10479. vmx->nested.nested_run_pending = 1;
  10480. ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
  10481. if (ret) {
  10482. nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
  10483. vmx->nested.nested_run_pending = 0;
  10484. return 1;
  10485. }
  10486. /*
  10487. * Must happen outside of enter_vmx_non_root_mode() as it will
  10488. * also be used as part of restoring nVMX state for
  10489. * snapshot restore (migration).
  10490. *
  10491. * In this flow, it is assumed that vmcs12 cache was
  10492. * trasferred as part of captured nVMX state and should
  10493. * therefore not be read from guest memory (which may not
  10494. * exist on destination host yet).
  10495. */
  10496. nested_cache_shadow_vmcs12(vcpu, vmcs12);
  10497. /*
  10498. * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
  10499. * by event injection, halt vcpu.
  10500. */
  10501. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  10502. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
  10503. vmx->nested.nested_run_pending = 0;
  10504. return kvm_vcpu_halt(vcpu);
  10505. }
  10506. return 1;
  10507. out:
  10508. return kvm_skip_emulated_instruction(vcpu);
  10509. }
  10510. /*
  10511. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  10512. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  10513. * This function returns the new value we should put in vmcs12.guest_cr0.
  10514. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  10515. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  10516. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  10517. * didn't trap the bit, because if L1 did, so would L0).
  10518. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  10519. * been modified by L2, and L1 knows it. So just leave the old value of
  10520. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  10521. * isn't relevant, because if L0 traps this bit it can set it to anything.
  10522. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  10523. * changed these bits, and therefore they need to be updated, but L0
  10524. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  10525. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  10526. */
  10527. static inline unsigned long
  10528. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10529. {
  10530. return
  10531. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  10532. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  10533. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  10534. vcpu->arch.cr0_guest_owned_bits));
  10535. }
  10536. static inline unsigned long
  10537. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10538. {
  10539. return
  10540. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  10541. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  10542. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  10543. vcpu->arch.cr4_guest_owned_bits));
  10544. }
  10545. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  10546. struct vmcs12 *vmcs12)
  10547. {
  10548. u32 idt_vectoring;
  10549. unsigned int nr;
  10550. if (vcpu->arch.exception.injected) {
  10551. nr = vcpu->arch.exception.nr;
  10552. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  10553. if (kvm_exception_is_soft(nr)) {
  10554. vmcs12->vm_exit_instruction_len =
  10555. vcpu->arch.event_exit_inst_len;
  10556. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  10557. } else
  10558. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  10559. if (vcpu->arch.exception.has_error_code) {
  10560. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  10561. vmcs12->idt_vectoring_error_code =
  10562. vcpu->arch.exception.error_code;
  10563. }
  10564. vmcs12->idt_vectoring_info_field = idt_vectoring;
  10565. } else if (vcpu->arch.nmi_injected) {
  10566. vmcs12->idt_vectoring_info_field =
  10567. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  10568. } else if (vcpu->arch.interrupt.injected) {
  10569. nr = vcpu->arch.interrupt.nr;
  10570. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  10571. if (vcpu->arch.interrupt.soft) {
  10572. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  10573. vmcs12->vm_entry_instruction_len =
  10574. vcpu->arch.event_exit_inst_len;
  10575. } else
  10576. idt_vectoring |= INTR_TYPE_EXT_INTR;
  10577. vmcs12->idt_vectoring_info_field = idt_vectoring;
  10578. }
  10579. }
  10580. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  10581. {
  10582. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10583. unsigned long exit_qual;
  10584. bool block_nested_events =
  10585. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  10586. if (vcpu->arch.exception.pending &&
  10587. nested_vmx_check_exception(vcpu, &exit_qual)) {
  10588. if (block_nested_events)
  10589. return -EBUSY;
  10590. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  10591. return 0;
  10592. }
  10593. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  10594. vmx->nested.preemption_timer_expired) {
  10595. if (block_nested_events)
  10596. return -EBUSY;
  10597. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  10598. return 0;
  10599. }
  10600. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  10601. if (block_nested_events)
  10602. return -EBUSY;
  10603. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  10604. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  10605. INTR_INFO_VALID_MASK, 0);
  10606. /*
  10607. * The NMI-triggered VM exit counts as injection:
  10608. * clear this one and block further NMIs.
  10609. */
  10610. vcpu->arch.nmi_pending = 0;
  10611. vmx_set_nmi_mask(vcpu, true);
  10612. return 0;
  10613. }
  10614. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  10615. nested_exit_on_intr(vcpu)) {
  10616. if (block_nested_events)
  10617. return -EBUSY;
  10618. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  10619. return 0;
  10620. }
  10621. vmx_complete_nested_posted_interrupt(vcpu);
  10622. return 0;
  10623. }
  10624. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  10625. {
  10626. ktime_t remaining =
  10627. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  10628. u64 value;
  10629. if (ktime_to_ns(remaining) <= 0)
  10630. return 0;
  10631. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  10632. do_div(value, 1000000);
  10633. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  10634. }
  10635. /*
  10636. * Update the guest state fields of vmcs12 to reflect changes that
  10637. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  10638. * VM-entry controls is also updated, since this is really a guest
  10639. * state bit.)
  10640. */
  10641. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10642. {
  10643. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  10644. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  10645. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  10646. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  10647. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  10648. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  10649. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  10650. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  10651. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  10652. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  10653. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  10654. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  10655. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  10656. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  10657. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  10658. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  10659. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  10660. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  10661. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  10662. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  10663. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  10664. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  10665. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  10666. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  10667. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  10668. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  10669. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  10670. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  10671. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  10672. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  10673. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  10674. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  10675. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  10676. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  10677. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  10678. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  10679. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  10680. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  10681. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  10682. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  10683. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  10684. vmcs12->guest_interruptibility_info =
  10685. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  10686. vmcs12->guest_pending_dbg_exceptions =
  10687. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  10688. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  10689. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  10690. else
  10691. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  10692. if (nested_cpu_has_preemption_timer(vmcs12)) {
  10693. if (vmcs12->vm_exit_controls &
  10694. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  10695. vmcs12->vmx_preemption_timer_value =
  10696. vmx_get_preemption_timer_value(vcpu);
  10697. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  10698. }
  10699. /*
  10700. * In some cases (usually, nested EPT), L2 is allowed to change its
  10701. * own CR3 without exiting. If it has changed it, we must keep it.
  10702. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  10703. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  10704. *
  10705. * Additionally, restore L2's PDPTR to vmcs12.
  10706. */
  10707. if (enable_ept) {
  10708. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  10709. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  10710. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  10711. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  10712. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  10713. }
  10714. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  10715. if (nested_cpu_has_vid(vmcs12))
  10716. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  10717. vmcs12->vm_entry_controls =
  10718. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  10719. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  10720. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  10721. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  10722. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10723. }
  10724. /* TODO: These cannot have changed unless we have MSR bitmaps and
  10725. * the relevant bit asks not to trap the change */
  10726. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  10727. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  10728. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  10729. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  10730. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  10731. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  10732. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  10733. if (kvm_mpx_supported())
  10734. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  10735. }
  10736. /*
  10737. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  10738. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  10739. * and this function updates it to reflect the changes to the guest state while
  10740. * L2 was running (and perhaps made some exits which were handled directly by L0
  10741. * without going back to L1), and to reflect the exit reason.
  10742. * Note that we do not have to copy here all VMCS fields, just those that
  10743. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  10744. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  10745. * which already writes to vmcs12 directly.
  10746. */
  10747. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10748. u32 exit_reason, u32 exit_intr_info,
  10749. unsigned long exit_qualification)
  10750. {
  10751. /* update guest state fields: */
  10752. sync_vmcs12(vcpu, vmcs12);
  10753. /* update exit information fields: */
  10754. vmcs12->vm_exit_reason = exit_reason;
  10755. vmcs12->exit_qualification = exit_qualification;
  10756. vmcs12->vm_exit_intr_info = exit_intr_info;
  10757. vmcs12->idt_vectoring_info_field = 0;
  10758. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  10759. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  10760. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  10761. vmcs12->launch_state = 1;
  10762. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  10763. * instead of reading the real value. */
  10764. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  10765. /*
  10766. * Transfer the event that L0 or L1 may wanted to inject into
  10767. * L2 to IDT_VECTORING_INFO_FIELD.
  10768. */
  10769. vmcs12_save_pending_event(vcpu, vmcs12);
  10770. }
  10771. /*
  10772. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  10773. * preserved above and would only end up incorrectly in L1.
  10774. */
  10775. vcpu->arch.nmi_injected = false;
  10776. kvm_clear_exception_queue(vcpu);
  10777. kvm_clear_interrupt_queue(vcpu);
  10778. }
  10779. static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
  10780. struct vmcs12 *vmcs12)
  10781. {
  10782. u32 entry_failure_code;
  10783. nested_ept_uninit_mmu_context(vcpu);
  10784. /*
  10785. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  10786. * couldn't have changed.
  10787. */
  10788. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  10789. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  10790. if (!enable_ept)
  10791. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  10792. }
  10793. /*
  10794. * A part of what we need to when the nested L2 guest exits and we want to
  10795. * run its L1 parent, is to reset L1's guest state to the host state specified
  10796. * in vmcs12.
  10797. * This function is to be called not only on normal nested exit, but also on
  10798. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  10799. * Failures During or After Loading Guest State").
  10800. * This function should be called when the active VMCS is L1's (vmcs01).
  10801. */
  10802. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  10803. struct vmcs12 *vmcs12)
  10804. {
  10805. struct kvm_segment seg;
  10806. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  10807. vcpu->arch.efer = vmcs12->host_ia32_efer;
  10808. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  10809. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  10810. else
  10811. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  10812. vmx_set_efer(vcpu, vcpu->arch.efer);
  10813. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  10814. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  10815. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  10816. /*
  10817. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  10818. * actually changed, because vmx_set_cr0 refers to efer set above.
  10819. *
  10820. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  10821. * (KVM doesn't change it);
  10822. */
  10823. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  10824. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  10825. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  10826. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  10827. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  10828. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10829. /*
  10830. * If vmcs01 don't use VPID, CPU flushes TLB on every
  10831. * VMEntry/VMExit. Thus, no need to flush TLB.
  10832. *
  10833. * If vmcs12 uses VPID, TLB entries populated by L2 are
  10834. * tagged with vmx->nested.vpid02 while L1 entries are tagged
  10835. * with vmx->vpid. Thus, no need to flush TLB.
  10836. *
  10837. * Therefore, flush TLB only in case vmcs01 uses VPID and
  10838. * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
  10839. * are both tagged with vmx->vpid.
  10840. */
  10841. if (enable_vpid &&
  10842. !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
  10843. vmx_flush_tlb(vcpu, true);
  10844. }
  10845. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  10846. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  10847. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  10848. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  10849. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  10850. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  10851. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  10852. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  10853. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  10854. vmcs_write64(GUEST_BNDCFGS, 0);
  10855. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  10856. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  10857. vcpu->arch.pat = vmcs12->host_ia32_pat;
  10858. }
  10859. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  10860. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  10861. vmcs12->host_ia32_perf_global_ctrl);
  10862. /* Set L1 segment info according to Intel SDM
  10863. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  10864. seg = (struct kvm_segment) {
  10865. .base = 0,
  10866. .limit = 0xFFFFFFFF,
  10867. .selector = vmcs12->host_cs_selector,
  10868. .type = 11,
  10869. .present = 1,
  10870. .s = 1,
  10871. .g = 1
  10872. };
  10873. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  10874. seg.l = 1;
  10875. else
  10876. seg.db = 1;
  10877. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  10878. seg = (struct kvm_segment) {
  10879. .base = 0,
  10880. .limit = 0xFFFFFFFF,
  10881. .type = 3,
  10882. .present = 1,
  10883. .s = 1,
  10884. .db = 1,
  10885. .g = 1
  10886. };
  10887. seg.selector = vmcs12->host_ds_selector;
  10888. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  10889. seg.selector = vmcs12->host_es_selector;
  10890. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  10891. seg.selector = vmcs12->host_ss_selector;
  10892. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  10893. seg.selector = vmcs12->host_fs_selector;
  10894. seg.base = vmcs12->host_fs_base;
  10895. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  10896. seg.selector = vmcs12->host_gs_selector;
  10897. seg.base = vmcs12->host_gs_base;
  10898. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  10899. seg = (struct kvm_segment) {
  10900. .base = vmcs12->host_tr_base,
  10901. .limit = 0x67,
  10902. .selector = vmcs12->host_tr_selector,
  10903. .type = 11,
  10904. .present = 1
  10905. };
  10906. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  10907. kvm_set_dr(vcpu, 7, 0x400);
  10908. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  10909. if (cpu_has_vmx_msr_bitmap())
  10910. vmx_update_msr_bitmap(vcpu);
  10911. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  10912. vmcs12->vm_exit_msr_load_count))
  10913. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  10914. }
  10915. /*
  10916. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  10917. * and modify vmcs12 to make it see what it would expect to see there if
  10918. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  10919. */
  10920. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  10921. u32 exit_intr_info,
  10922. unsigned long exit_qualification)
  10923. {
  10924. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10925. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10926. /* trying to cancel vmlaunch/vmresume is a bug */
  10927. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  10928. /*
  10929. * The only expected VM-instruction error is "VM entry with
  10930. * invalid control field(s)." Anything else indicates a
  10931. * problem with L0.
  10932. */
  10933. WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
  10934. VMXERR_ENTRY_INVALID_CONTROL_FIELD));
  10935. leave_guest_mode(vcpu);
  10936. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10937. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  10938. if (likely(!vmx->fail)) {
  10939. if (exit_reason == -1)
  10940. sync_vmcs12(vcpu, vmcs12);
  10941. else
  10942. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  10943. exit_qualification);
  10944. /*
  10945. * Must happen outside of sync_vmcs12() as it will
  10946. * also be used to capture vmcs12 cache as part of
  10947. * capturing nVMX state for snapshot (migration).
  10948. *
  10949. * Otherwise, this flush will dirty guest memory at a
  10950. * point it is already assumed by user-space to be
  10951. * immutable.
  10952. */
  10953. nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
  10954. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  10955. vmcs12->vm_exit_msr_store_count))
  10956. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  10957. }
  10958. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10959. vm_entry_controls_reset_shadow(vmx);
  10960. vm_exit_controls_reset_shadow(vmx);
  10961. vmx_segment_cache_clear(vmx);
  10962. /* Update any VMCS fields that might have changed while L2 ran */
  10963. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10964. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10965. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10966. if (vmx->hv_deadline_tsc == -1)
  10967. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10968. PIN_BASED_VMX_PREEMPTION_TIMER);
  10969. else
  10970. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10971. PIN_BASED_VMX_PREEMPTION_TIMER);
  10972. if (kvm_has_tsc_control)
  10973. decache_tsc_multiplier(vmx);
  10974. if (vmx->nested.change_vmcs01_virtual_apic_mode) {
  10975. vmx->nested.change_vmcs01_virtual_apic_mode = false;
  10976. vmx_set_virtual_apic_mode(vcpu);
  10977. } else if (!nested_cpu_has_ept(vmcs12) &&
  10978. nested_cpu_has2(vmcs12,
  10979. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10980. vmx_flush_tlb(vcpu, true);
  10981. }
  10982. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  10983. vmx->host_rsp = 0;
  10984. /* Unpin physical memory we referred to in vmcs02 */
  10985. if (vmx->nested.apic_access_page) {
  10986. kvm_release_page_dirty(vmx->nested.apic_access_page);
  10987. vmx->nested.apic_access_page = NULL;
  10988. }
  10989. if (vmx->nested.virtual_apic_page) {
  10990. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  10991. vmx->nested.virtual_apic_page = NULL;
  10992. }
  10993. if (vmx->nested.pi_desc_page) {
  10994. kunmap(vmx->nested.pi_desc_page);
  10995. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  10996. vmx->nested.pi_desc_page = NULL;
  10997. vmx->nested.pi_desc = NULL;
  10998. }
  10999. /*
  11000. * We are now running in L2, mmu_notifier will force to reload the
  11001. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  11002. */
  11003. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  11004. if (enable_shadow_vmcs && exit_reason != -1)
  11005. vmx->nested.sync_shadow_vmcs = true;
  11006. /* in case we halted in L2 */
  11007. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  11008. if (likely(!vmx->fail)) {
  11009. /*
  11010. * TODO: SDM says that with acknowledge interrupt on
  11011. * exit, bit 31 of the VM-exit interrupt information
  11012. * (valid interrupt) is always set to 1 on
  11013. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  11014. * need kvm_cpu_has_interrupt(). See the commit
  11015. * message for details.
  11016. */
  11017. if (nested_exit_intr_ack_set(vcpu) &&
  11018. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  11019. kvm_cpu_has_interrupt(vcpu)) {
  11020. int irq = kvm_cpu_get_interrupt(vcpu);
  11021. WARN_ON(irq < 0);
  11022. vmcs12->vm_exit_intr_info = irq |
  11023. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  11024. }
  11025. if (exit_reason != -1)
  11026. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  11027. vmcs12->exit_qualification,
  11028. vmcs12->idt_vectoring_info_field,
  11029. vmcs12->vm_exit_intr_info,
  11030. vmcs12->vm_exit_intr_error_code,
  11031. KVM_ISA_VMX);
  11032. load_vmcs12_host_state(vcpu, vmcs12);
  11033. return;
  11034. }
  11035. /*
  11036. * After an early L2 VM-entry failure, we're now back
  11037. * in L1 which thinks it just finished a VMLAUNCH or
  11038. * VMRESUME instruction, so we need to set the failure
  11039. * flag and the VM-instruction error field of the VMCS
  11040. * accordingly.
  11041. */
  11042. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  11043. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  11044. /*
  11045. * The emulated instruction was already skipped in
  11046. * nested_vmx_run, but the updated RIP was never
  11047. * written back to the vmcs01.
  11048. */
  11049. skip_emulated_instruction(vcpu);
  11050. vmx->fail = 0;
  11051. }
  11052. /*
  11053. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  11054. */
  11055. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  11056. {
  11057. if (is_guest_mode(vcpu)) {
  11058. to_vmx(vcpu)->nested.nested_run_pending = 0;
  11059. nested_vmx_vmexit(vcpu, -1, 0, 0);
  11060. }
  11061. free_nested(to_vmx(vcpu));
  11062. }
  11063. /*
  11064. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  11065. * 23.7 "VM-entry failures during or after loading guest state" (this also
  11066. * lists the acceptable exit-reason and exit-qualification parameters).
  11067. * It should only be called before L2 actually succeeded to run, and when
  11068. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  11069. */
  11070. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  11071. struct vmcs12 *vmcs12,
  11072. u32 reason, unsigned long qualification)
  11073. {
  11074. load_vmcs12_host_state(vcpu, vmcs12);
  11075. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  11076. vmcs12->exit_qualification = qualification;
  11077. nested_vmx_succeed(vcpu);
  11078. if (enable_shadow_vmcs)
  11079. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  11080. }
  11081. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  11082. struct x86_instruction_info *info,
  11083. enum x86_intercept_stage stage)
  11084. {
  11085. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11086. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  11087. /*
  11088. * RDPID causes #UD if disabled through secondary execution controls.
  11089. * Because it is marked as EmulateOnUD, we need to intercept it here.
  11090. */
  11091. if (info->intercept == x86_intercept_rdtscp &&
  11092. !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  11093. ctxt->exception.vector = UD_VECTOR;
  11094. ctxt->exception.error_code_valid = false;
  11095. return X86EMUL_PROPAGATE_FAULT;
  11096. }
  11097. /* TODO: check more intercepts... */
  11098. return X86EMUL_CONTINUE;
  11099. }
  11100. #ifdef CONFIG_X86_64
  11101. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  11102. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  11103. u64 divisor, u64 *result)
  11104. {
  11105. u64 low = a << shift, high = a >> (64 - shift);
  11106. /* To avoid the overflow on divq */
  11107. if (high >= divisor)
  11108. return 1;
  11109. /* Low hold the result, high hold rem which is discarded */
  11110. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  11111. "rm" (divisor), "0" (low), "1" (high));
  11112. *result = low;
  11113. return 0;
  11114. }
  11115. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  11116. {
  11117. struct vcpu_vmx *vmx;
  11118. u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
  11119. if (kvm_mwait_in_guest(vcpu->kvm))
  11120. return -EOPNOTSUPP;
  11121. vmx = to_vmx(vcpu);
  11122. tscl = rdtsc();
  11123. guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  11124. delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  11125. lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
  11126. if (delta_tsc > lapic_timer_advance_cycles)
  11127. delta_tsc -= lapic_timer_advance_cycles;
  11128. else
  11129. delta_tsc = 0;
  11130. /* Convert to host delta tsc if tsc scaling is enabled */
  11131. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  11132. u64_shl_div_u64(delta_tsc,
  11133. kvm_tsc_scaling_ratio_frac_bits,
  11134. vcpu->arch.tsc_scaling_ratio,
  11135. &delta_tsc))
  11136. return -ERANGE;
  11137. /*
  11138. * If the delta tsc can't fit in the 32 bit after the multi shift,
  11139. * we can't use the preemption timer.
  11140. * It's possible that it fits on later vmentries, but checking
  11141. * on every vmentry is costly so we just use an hrtimer.
  11142. */
  11143. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  11144. return -ERANGE;
  11145. vmx->hv_deadline_tsc = tscl + delta_tsc;
  11146. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  11147. PIN_BASED_VMX_PREEMPTION_TIMER);
  11148. return delta_tsc == 0;
  11149. }
  11150. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  11151. {
  11152. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11153. vmx->hv_deadline_tsc = -1;
  11154. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  11155. PIN_BASED_VMX_PREEMPTION_TIMER);
  11156. }
  11157. #endif
  11158. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  11159. {
  11160. if (!kvm_pause_in_guest(vcpu->kvm))
  11161. shrink_ple_window(vcpu);
  11162. }
  11163. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  11164. struct kvm_memory_slot *slot)
  11165. {
  11166. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  11167. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  11168. }
  11169. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  11170. struct kvm_memory_slot *slot)
  11171. {
  11172. kvm_mmu_slot_set_dirty(kvm, slot);
  11173. }
  11174. static void vmx_flush_log_dirty(struct kvm *kvm)
  11175. {
  11176. kvm_flush_pml_buffers(kvm);
  11177. }
  11178. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  11179. {
  11180. struct vmcs12 *vmcs12;
  11181. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11182. gpa_t gpa;
  11183. struct page *page = NULL;
  11184. u64 *pml_address;
  11185. if (is_guest_mode(vcpu)) {
  11186. WARN_ON_ONCE(vmx->nested.pml_full);
  11187. /*
  11188. * Check if PML is enabled for the nested guest.
  11189. * Whether eptp bit 6 is set is already checked
  11190. * as part of A/D emulation.
  11191. */
  11192. vmcs12 = get_vmcs12(vcpu);
  11193. if (!nested_cpu_has_pml(vmcs12))
  11194. return 0;
  11195. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  11196. vmx->nested.pml_full = true;
  11197. return 1;
  11198. }
  11199. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  11200. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  11201. if (is_error_page(page))
  11202. return 0;
  11203. pml_address = kmap(page);
  11204. pml_address[vmcs12->guest_pml_index--] = gpa;
  11205. kunmap(page);
  11206. kvm_release_page_clean(page);
  11207. }
  11208. return 0;
  11209. }
  11210. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  11211. struct kvm_memory_slot *memslot,
  11212. gfn_t offset, unsigned long mask)
  11213. {
  11214. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  11215. }
  11216. static void __pi_post_block(struct kvm_vcpu *vcpu)
  11217. {
  11218. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  11219. struct pi_desc old, new;
  11220. unsigned int dest;
  11221. do {
  11222. old.control = new.control = pi_desc->control;
  11223. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  11224. "Wakeup handler not enabled while the VCPU is blocked\n");
  11225. dest = cpu_physical_id(vcpu->cpu);
  11226. if (x2apic_enabled())
  11227. new.ndst = dest;
  11228. else
  11229. new.ndst = (dest << 8) & 0xFF00;
  11230. /* set 'NV' to 'notification vector' */
  11231. new.nv = POSTED_INTR_VECTOR;
  11232. } while (cmpxchg64(&pi_desc->control, old.control,
  11233. new.control) != old.control);
  11234. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  11235. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11236. list_del(&vcpu->blocked_vcpu_list);
  11237. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11238. vcpu->pre_pcpu = -1;
  11239. }
  11240. }
  11241. /*
  11242. * This routine does the following things for vCPU which is going
  11243. * to be blocked if VT-d PI is enabled.
  11244. * - Store the vCPU to the wakeup list, so when interrupts happen
  11245. * we can find the right vCPU to wake up.
  11246. * - Change the Posted-interrupt descriptor as below:
  11247. * 'NDST' <-- vcpu->pre_pcpu
  11248. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  11249. * - If 'ON' is set during this process, which means at least one
  11250. * interrupt is posted for this vCPU, we cannot block it, in
  11251. * this case, return 1, otherwise, return 0.
  11252. *
  11253. */
  11254. static int pi_pre_block(struct kvm_vcpu *vcpu)
  11255. {
  11256. unsigned int dest;
  11257. struct pi_desc old, new;
  11258. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  11259. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  11260. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  11261. !kvm_vcpu_apicv_active(vcpu))
  11262. return 0;
  11263. WARN_ON(irqs_disabled());
  11264. local_irq_disable();
  11265. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  11266. vcpu->pre_pcpu = vcpu->cpu;
  11267. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11268. list_add_tail(&vcpu->blocked_vcpu_list,
  11269. &per_cpu(blocked_vcpu_on_cpu,
  11270. vcpu->pre_pcpu));
  11271. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11272. }
  11273. do {
  11274. old.control = new.control = pi_desc->control;
  11275. WARN((pi_desc->sn == 1),
  11276. "Warning: SN field of posted-interrupts "
  11277. "is set before blocking\n");
  11278. /*
  11279. * Since vCPU can be preempted during this process,
  11280. * vcpu->cpu could be different with pre_pcpu, we
  11281. * need to set pre_pcpu as the destination of wakeup
  11282. * notification event, then we can find the right vCPU
  11283. * to wakeup in wakeup handler if interrupts happen
  11284. * when the vCPU is in blocked state.
  11285. */
  11286. dest = cpu_physical_id(vcpu->pre_pcpu);
  11287. if (x2apic_enabled())
  11288. new.ndst = dest;
  11289. else
  11290. new.ndst = (dest << 8) & 0xFF00;
  11291. /* set 'NV' to 'wakeup vector' */
  11292. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  11293. } while (cmpxchg64(&pi_desc->control, old.control,
  11294. new.control) != old.control);
  11295. /* We should not block the vCPU if an interrupt is posted for it. */
  11296. if (pi_test_on(pi_desc) == 1)
  11297. __pi_post_block(vcpu);
  11298. local_irq_enable();
  11299. return (vcpu->pre_pcpu == -1);
  11300. }
  11301. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  11302. {
  11303. if (pi_pre_block(vcpu))
  11304. return 1;
  11305. if (kvm_lapic_hv_timer_in_use(vcpu))
  11306. kvm_lapic_switch_to_sw_timer(vcpu);
  11307. return 0;
  11308. }
  11309. static void pi_post_block(struct kvm_vcpu *vcpu)
  11310. {
  11311. if (vcpu->pre_pcpu == -1)
  11312. return;
  11313. WARN_ON(irqs_disabled());
  11314. local_irq_disable();
  11315. __pi_post_block(vcpu);
  11316. local_irq_enable();
  11317. }
  11318. static void vmx_post_block(struct kvm_vcpu *vcpu)
  11319. {
  11320. if (kvm_x86_ops->set_hv_timer)
  11321. kvm_lapic_switch_to_hv_timer(vcpu);
  11322. pi_post_block(vcpu);
  11323. }
  11324. /*
  11325. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  11326. *
  11327. * @kvm: kvm
  11328. * @host_irq: host irq of the interrupt
  11329. * @guest_irq: gsi of the interrupt
  11330. * @set: set or unset PI
  11331. * returns 0 on success, < 0 on failure
  11332. */
  11333. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  11334. uint32_t guest_irq, bool set)
  11335. {
  11336. struct kvm_kernel_irq_routing_entry *e;
  11337. struct kvm_irq_routing_table *irq_rt;
  11338. struct kvm_lapic_irq irq;
  11339. struct kvm_vcpu *vcpu;
  11340. struct vcpu_data vcpu_info;
  11341. int idx, ret = 0;
  11342. if (!kvm_arch_has_assigned_device(kvm) ||
  11343. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  11344. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  11345. return 0;
  11346. idx = srcu_read_lock(&kvm->irq_srcu);
  11347. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  11348. if (guest_irq >= irq_rt->nr_rt_entries ||
  11349. hlist_empty(&irq_rt->map[guest_irq])) {
  11350. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  11351. guest_irq, irq_rt->nr_rt_entries);
  11352. goto out;
  11353. }
  11354. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  11355. if (e->type != KVM_IRQ_ROUTING_MSI)
  11356. continue;
  11357. /*
  11358. * VT-d PI cannot support posting multicast/broadcast
  11359. * interrupts to a vCPU, we still use interrupt remapping
  11360. * for these kind of interrupts.
  11361. *
  11362. * For lowest-priority interrupts, we only support
  11363. * those with single CPU as the destination, e.g. user
  11364. * configures the interrupts via /proc/irq or uses
  11365. * irqbalance to make the interrupts single-CPU.
  11366. *
  11367. * We will support full lowest-priority interrupt later.
  11368. */
  11369. kvm_set_msi_irq(kvm, e, &irq);
  11370. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  11371. /*
  11372. * Make sure the IRTE is in remapped mode if
  11373. * we don't handle it in posted mode.
  11374. */
  11375. ret = irq_set_vcpu_affinity(host_irq, NULL);
  11376. if (ret < 0) {
  11377. printk(KERN_INFO
  11378. "failed to back to remapped mode, irq: %u\n",
  11379. host_irq);
  11380. goto out;
  11381. }
  11382. continue;
  11383. }
  11384. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  11385. vcpu_info.vector = irq.vector;
  11386. trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
  11387. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  11388. if (set)
  11389. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  11390. else
  11391. ret = irq_set_vcpu_affinity(host_irq, NULL);
  11392. if (ret < 0) {
  11393. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  11394. __func__);
  11395. goto out;
  11396. }
  11397. }
  11398. ret = 0;
  11399. out:
  11400. srcu_read_unlock(&kvm->irq_srcu, idx);
  11401. return ret;
  11402. }
  11403. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  11404. {
  11405. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  11406. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  11407. FEATURE_CONTROL_LMCE;
  11408. else
  11409. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  11410. ~FEATURE_CONTROL_LMCE;
  11411. }
  11412. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  11413. {
  11414. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  11415. if (to_vmx(vcpu)->nested.nested_run_pending)
  11416. return 0;
  11417. return 1;
  11418. }
  11419. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  11420. {
  11421. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11422. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  11423. if (vmx->nested.smm.guest_mode)
  11424. nested_vmx_vmexit(vcpu, -1, 0, 0);
  11425. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  11426. vmx->nested.vmxon = false;
  11427. vmx_clear_hlt(vcpu);
  11428. return 0;
  11429. }
  11430. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  11431. {
  11432. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11433. int ret;
  11434. if (vmx->nested.smm.vmxon) {
  11435. vmx->nested.vmxon = true;
  11436. vmx->nested.smm.vmxon = false;
  11437. }
  11438. if (vmx->nested.smm.guest_mode) {
  11439. vcpu->arch.hflags &= ~HF_SMM_MASK;
  11440. ret = enter_vmx_non_root_mode(vcpu, NULL);
  11441. vcpu->arch.hflags |= HF_SMM_MASK;
  11442. if (ret)
  11443. return ret;
  11444. vmx->nested.smm.guest_mode = false;
  11445. }
  11446. return 0;
  11447. }
  11448. static int enable_smi_window(struct kvm_vcpu *vcpu)
  11449. {
  11450. return 0;
  11451. }
  11452. static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
  11453. struct kvm_nested_state __user *user_kvm_nested_state,
  11454. u32 user_data_size)
  11455. {
  11456. struct vcpu_vmx *vmx;
  11457. struct vmcs12 *vmcs12;
  11458. struct kvm_nested_state kvm_state = {
  11459. .flags = 0,
  11460. .format = 0,
  11461. .size = sizeof(kvm_state),
  11462. .vmx.vmxon_pa = -1ull,
  11463. .vmx.vmcs_pa = -1ull,
  11464. };
  11465. if (!vcpu)
  11466. return kvm_state.size + 2 * VMCS12_SIZE;
  11467. vmx = to_vmx(vcpu);
  11468. vmcs12 = get_vmcs12(vcpu);
  11469. if (nested_vmx_allowed(vcpu) &&
  11470. (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
  11471. kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
  11472. kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
  11473. if (vmx->nested.current_vmptr != -1ull) {
  11474. kvm_state.size += VMCS12_SIZE;
  11475. if (is_guest_mode(vcpu) &&
  11476. nested_cpu_has_shadow_vmcs(vmcs12) &&
  11477. vmcs12->vmcs_link_pointer != -1ull)
  11478. kvm_state.size += VMCS12_SIZE;
  11479. }
  11480. if (vmx->nested.smm.vmxon)
  11481. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
  11482. if (vmx->nested.smm.guest_mode)
  11483. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
  11484. if (is_guest_mode(vcpu)) {
  11485. kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
  11486. if (vmx->nested.nested_run_pending)
  11487. kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
  11488. }
  11489. }
  11490. if (user_data_size < kvm_state.size)
  11491. goto out;
  11492. if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
  11493. return -EFAULT;
  11494. if (vmx->nested.current_vmptr == -1ull)
  11495. goto out;
  11496. /*
  11497. * When running L2, the authoritative vmcs12 state is in the
  11498. * vmcs02. When running L1, the authoritative vmcs12 state is
  11499. * in the shadow vmcs linked to vmcs01, unless
  11500. * sync_shadow_vmcs is set, in which case, the authoritative
  11501. * vmcs12 state is in the vmcs12 already.
  11502. */
  11503. if (is_guest_mode(vcpu))
  11504. sync_vmcs12(vcpu, vmcs12);
  11505. else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
  11506. copy_shadow_to_vmcs12(vmx);
  11507. if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
  11508. return -EFAULT;
  11509. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  11510. vmcs12->vmcs_link_pointer != -1ull) {
  11511. if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
  11512. get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
  11513. return -EFAULT;
  11514. }
  11515. out:
  11516. return kvm_state.size;
  11517. }
  11518. static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
  11519. struct kvm_nested_state __user *user_kvm_nested_state,
  11520. struct kvm_nested_state *kvm_state)
  11521. {
  11522. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11523. struct vmcs12 *vmcs12;
  11524. u32 exit_qual;
  11525. int ret;
  11526. if (kvm_state->format != 0)
  11527. return -EINVAL;
  11528. if (!nested_vmx_allowed(vcpu))
  11529. return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
  11530. if (kvm_state->vmx.vmxon_pa == -1ull) {
  11531. if (kvm_state->vmx.smm.flags)
  11532. return -EINVAL;
  11533. if (kvm_state->vmx.vmcs_pa != -1ull)
  11534. return -EINVAL;
  11535. vmx_leave_nested(vcpu);
  11536. return 0;
  11537. }
  11538. if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
  11539. return -EINVAL;
  11540. if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
  11541. return -EINVAL;
  11542. if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
  11543. !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
  11544. return -EINVAL;
  11545. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  11546. (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  11547. return -EINVAL;
  11548. if (kvm_state->vmx.smm.flags &
  11549. ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
  11550. return -EINVAL;
  11551. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  11552. !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
  11553. return -EINVAL;
  11554. vmx_leave_nested(vcpu);
  11555. if (kvm_state->vmx.vmxon_pa == -1ull)
  11556. return 0;
  11557. vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
  11558. ret = enter_vmx_operation(vcpu);
  11559. if (ret)
  11560. return ret;
  11561. set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
  11562. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
  11563. vmx->nested.smm.vmxon = true;
  11564. vmx->nested.vmxon = false;
  11565. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
  11566. vmx->nested.smm.guest_mode = true;
  11567. }
  11568. vmcs12 = get_vmcs12(vcpu);
  11569. if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
  11570. return -EFAULT;
  11571. if (vmcs12->hdr.revision_id != VMCS12_REVISION)
  11572. return -EINVAL;
  11573. if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  11574. return 0;
  11575. vmx->nested.nested_run_pending =
  11576. !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
  11577. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  11578. vmcs12->vmcs_link_pointer != -1ull) {
  11579. struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
  11580. if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
  11581. return -EINVAL;
  11582. if (copy_from_user(shadow_vmcs12,
  11583. user_kvm_nested_state->data + VMCS12_SIZE,
  11584. sizeof(*vmcs12)))
  11585. return -EFAULT;
  11586. if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  11587. !shadow_vmcs12->hdr.shadow_vmcs)
  11588. return -EINVAL;
  11589. }
  11590. if (check_vmentry_prereqs(vcpu, vmcs12) ||
  11591. check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
  11592. return -EINVAL;
  11593. if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
  11594. vmx->nested.nested_run_pending = 1;
  11595. vmx->nested.dirty_vmcs12 = true;
  11596. ret = enter_vmx_non_root_mode(vcpu, NULL);
  11597. if (ret)
  11598. return -EINVAL;
  11599. return 0;
  11600. }
  11601. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  11602. .cpu_has_kvm_support = cpu_has_kvm_support,
  11603. .disabled_by_bios = vmx_disabled_by_bios,
  11604. .hardware_setup = hardware_setup,
  11605. .hardware_unsetup = hardware_unsetup,
  11606. .check_processor_compatibility = vmx_check_processor_compat,
  11607. .hardware_enable = hardware_enable,
  11608. .hardware_disable = hardware_disable,
  11609. .cpu_has_accelerated_tpr = report_flexpriority,
  11610. .has_emulated_msr = vmx_has_emulated_msr,
  11611. .vm_init = vmx_vm_init,
  11612. .vm_alloc = vmx_vm_alloc,
  11613. .vm_free = vmx_vm_free,
  11614. .vcpu_create = vmx_create_vcpu,
  11615. .vcpu_free = vmx_free_vcpu,
  11616. .vcpu_reset = vmx_vcpu_reset,
  11617. .prepare_guest_switch = vmx_save_host_state,
  11618. .vcpu_load = vmx_vcpu_load,
  11619. .vcpu_put = vmx_vcpu_put,
  11620. .update_bp_intercept = update_exception_bitmap,
  11621. .get_msr_feature = vmx_get_msr_feature,
  11622. .get_msr = vmx_get_msr,
  11623. .set_msr = vmx_set_msr,
  11624. .get_segment_base = vmx_get_segment_base,
  11625. .get_segment = vmx_get_segment,
  11626. .set_segment = vmx_set_segment,
  11627. .get_cpl = vmx_get_cpl,
  11628. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  11629. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  11630. .decache_cr3 = vmx_decache_cr3,
  11631. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  11632. .set_cr0 = vmx_set_cr0,
  11633. .set_cr3 = vmx_set_cr3,
  11634. .set_cr4 = vmx_set_cr4,
  11635. .set_efer = vmx_set_efer,
  11636. .get_idt = vmx_get_idt,
  11637. .set_idt = vmx_set_idt,
  11638. .get_gdt = vmx_get_gdt,
  11639. .set_gdt = vmx_set_gdt,
  11640. .get_dr6 = vmx_get_dr6,
  11641. .set_dr6 = vmx_set_dr6,
  11642. .set_dr7 = vmx_set_dr7,
  11643. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  11644. .cache_reg = vmx_cache_reg,
  11645. .get_rflags = vmx_get_rflags,
  11646. .set_rflags = vmx_set_rflags,
  11647. .tlb_flush = vmx_flush_tlb,
  11648. .run = vmx_vcpu_run,
  11649. .handle_exit = vmx_handle_exit,
  11650. .skip_emulated_instruction = skip_emulated_instruction,
  11651. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  11652. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  11653. .patch_hypercall = vmx_patch_hypercall,
  11654. .set_irq = vmx_inject_irq,
  11655. .set_nmi = vmx_inject_nmi,
  11656. .queue_exception = vmx_queue_exception,
  11657. .cancel_injection = vmx_cancel_injection,
  11658. .interrupt_allowed = vmx_interrupt_allowed,
  11659. .nmi_allowed = vmx_nmi_allowed,
  11660. .get_nmi_mask = vmx_get_nmi_mask,
  11661. .set_nmi_mask = vmx_set_nmi_mask,
  11662. .enable_nmi_window = enable_nmi_window,
  11663. .enable_irq_window = enable_irq_window,
  11664. .update_cr8_intercept = update_cr8_intercept,
  11665. .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
  11666. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  11667. .get_enable_apicv = vmx_get_enable_apicv,
  11668. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  11669. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  11670. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  11671. .hwapic_irr_update = vmx_hwapic_irr_update,
  11672. .hwapic_isr_update = vmx_hwapic_isr_update,
  11673. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  11674. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  11675. .set_tss_addr = vmx_set_tss_addr,
  11676. .set_identity_map_addr = vmx_set_identity_map_addr,
  11677. .get_tdp_level = get_ept_level,
  11678. .get_mt_mask = vmx_get_mt_mask,
  11679. .get_exit_info = vmx_get_exit_info,
  11680. .get_lpage_level = vmx_get_lpage_level,
  11681. .cpuid_update = vmx_cpuid_update,
  11682. .rdtscp_supported = vmx_rdtscp_supported,
  11683. .invpcid_supported = vmx_invpcid_supported,
  11684. .set_supported_cpuid = vmx_set_supported_cpuid,
  11685. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  11686. .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
  11687. .write_tsc_offset = vmx_write_tsc_offset,
  11688. .set_tdp_cr3 = vmx_set_cr3,
  11689. .check_intercept = vmx_check_intercept,
  11690. .handle_external_intr = vmx_handle_external_intr,
  11691. .mpx_supported = vmx_mpx_supported,
  11692. .xsaves_supported = vmx_xsaves_supported,
  11693. .umip_emulated = vmx_umip_emulated,
  11694. .check_nested_events = vmx_check_nested_events,
  11695. .sched_in = vmx_sched_in,
  11696. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  11697. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  11698. .flush_log_dirty = vmx_flush_log_dirty,
  11699. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  11700. .write_log_dirty = vmx_write_pml_buffer,
  11701. .pre_block = vmx_pre_block,
  11702. .post_block = vmx_post_block,
  11703. .pmu_ops = &intel_pmu_ops,
  11704. .update_pi_irte = vmx_update_pi_irte,
  11705. #ifdef CONFIG_X86_64
  11706. .set_hv_timer = vmx_set_hv_timer,
  11707. .cancel_hv_timer = vmx_cancel_hv_timer,
  11708. #endif
  11709. .setup_mce = vmx_setup_mce,
  11710. .get_nested_state = vmx_get_nested_state,
  11711. .set_nested_state = vmx_set_nested_state,
  11712. .get_vmcs12_pages = nested_get_vmcs12_pages,
  11713. .smi_allowed = vmx_smi_allowed,
  11714. .pre_enter_smm = vmx_pre_enter_smm,
  11715. .pre_leave_smm = vmx_pre_leave_smm,
  11716. .enable_smi_window = enable_smi_window,
  11717. };
  11718. static int __init vmx_init(void)
  11719. {
  11720. int r;
  11721. #if IS_ENABLED(CONFIG_HYPERV)
  11722. /*
  11723. * Enlightened VMCS usage should be recommended and the host needs
  11724. * to support eVMCS v1 or above. We can also disable eVMCS support
  11725. * with module parameter.
  11726. */
  11727. if (enlightened_vmcs &&
  11728. ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
  11729. (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
  11730. KVM_EVMCS_VERSION) {
  11731. int cpu;
  11732. /* Check that we have assist pages on all online CPUs */
  11733. for_each_online_cpu(cpu) {
  11734. if (!hv_get_vp_assist_page(cpu)) {
  11735. enlightened_vmcs = false;
  11736. break;
  11737. }
  11738. }
  11739. if (enlightened_vmcs) {
  11740. pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
  11741. static_branch_enable(&enable_evmcs);
  11742. }
  11743. } else {
  11744. enlightened_vmcs = false;
  11745. }
  11746. #endif
  11747. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  11748. __alignof__(struct vcpu_vmx), THIS_MODULE);
  11749. if (r)
  11750. return r;
  11751. #ifdef CONFIG_KEXEC_CORE
  11752. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  11753. crash_vmclear_local_loaded_vmcss);
  11754. #endif
  11755. vmx_check_vmcs12_offsets();
  11756. return 0;
  11757. }
  11758. static void __exit vmx_exit(void)
  11759. {
  11760. #ifdef CONFIG_KEXEC_CORE
  11761. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  11762. synchronize_rcu();
  11763. #endif
  11764. kvm_exit();
  11765. #if IS_ENABLED(CONFIG_HYPERV)
  11766. if (static_branch_unlikely(&enable_evmcs)) {
  11767. int cpu;
  11768. struct hv_vp_assist_page *vp_ap;
  11769. /*
  11770. * Reset everything to support using non-enlightened VMCS
  11771. * access later (e.g. when we reload the module with
  11772. * enlightened_vmcs=0)
  11773. */
  11774. for_each_online_cpu(cpu) {
  11775. vp_ap = hv_get_vp_assist_page(cpu);
  11776. if (!vp_ap)
  11777. continue;
  11778. vp_ap->current_nested_vmcs = 0;
  11779. vp_ap->enlighten_vmentry = 0;
  11780. }
  11781. static_branch_disable(&enable_evmcs);
  11782. }
  11783. #endif
  11784. }
  11785. module_init(vmx_init)
  11786. module_exit(vmx_exit)