vmx.c 373 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200
  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 {
  164. u32 revision_id;
  165. u32 abort;
  166. char data[0];
  167. };
  168. /*
  169. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  170. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  171. * loaded on this CPU (so we can clear them if the CPU goes down).
  172. */
  173. struct loaded_vmcs {
  174. struct vmcs *vmcs;
  175. struct vmcs *shadow_vmcs;
  176. int cpu;
  177. bool launched;
  178. bool nmi_known_unmasked;
  179. unsigned long vmcs_host_cr3; /* May not match real cr3 */
  180. unsigned long vmcs_host_cr4; /* May not match real cr4 */
  181. /* Support for vnmi-less CPUs */
  182. int soft_vnmi_blocked;
  183. ktime_t entry_time;
  184. s64 vnmi_blocked_time;
  185. unsigned long *msr_bitmap;
  186. struct list_head loaded_vmcss_on_cpu_link;
  187. };
  188. struct shared_msr_entry {
  189. unsigned index;
  190. u64 data;
  191. u64 mask;
  192. };
  193. /*
  194. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  195. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  196. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  197. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  198. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  199. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  200. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  201. * underlying hardware which will be used to run L2.
  202. * This structure is packed to ensure that its layout is identical across
  203. * machines (necessary for live migration).
  204. *
  205. * IMPORTANT: Changing the layout of existing fields in this structure
  206. * will break save/restore compatibility with older kvm releases. When
  207. * adding new fields, either use space in the reserved padding* arrays
  208. * or add the new fields to the end of the structure.
  209. */
  210. typedef u64 natural_width;
  211. struct __packed vmcs12 {
  212. /* According to the Intel spec, a VMCS region must start with the
  213. * following two fields. Then follow implementation-specific data.
  214. */
  215. u32 revision_id;
  216. u32 abort;
  217. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  218. u32 padding[7]; /* room for future expansion */
  219. u64 io_bitmap_a;
  220. u64 io_bitmap_b;
  221. u64 msr_bitmap;
  222. u64 vm_exit_msr_store_addr;
  223. u64 vm_exit_msr_load_addr;
  224. u64 vm_entry_msr_load_addr;
  225. u64 tsc_offset;
  226. u64 virtual_apic_page_addr;
  227. u64 apic_access_addr;
  228. u64 posted_intr_desc_addr;
  229. u64 ept_pointer;
  230. u64 eoi_exit_bitmap0;
  231. u64 eoi_exit_bitmap1;
  232. u64 eoi_exit_bitmap2;
  233. u64 eoi_exit_bitmap3;
  234. u64 xss_exit_bitmap;
  235. u64 guest_physical_address;
  236. u64 vmcs_link_pointer;
  237. u64 guest_ia32_debugctl;
  238. u64 guest_ia32_pat;
  239. u64 guest_ia32_efer;
  240. u64 guest_ia32_perf_global_ctrl;
  241. u64 guest_pdptr0;
  242. u64 guest_pdptr1;
  243. u64 guest_pdptr2;
  244. u64 guest_pdptr3;
  245. u64 guest_bndcfgs;
  246. u64 host_ia32_pat;
  247. u64 host_ia32_efer;
  248. u64 host_ia32_perf_global_ctrl;
  249. u64 vmread_bitmap;
  250. u64 vmwrite_bitmap;
  251. u64 vm_function_control;
  252. u64 eptp_list_address;
  253. u64 pml_address;
  254. u64 padding64[3]; /* room for future expansion */
  255. /*
  256. * To allow migration of L1 (complete with its L2 guests) between
  257. * machines of different natural widths (32 or 64 bit), we cannot have
  258. * unsigned long fields with no explict size. We use u64 (aliased
  259. * natural_width) instead. Luckily, x86 is little-endian.
  260. */
  261. natural_width cr0_guest_host_mask;
  262. natural_width cr4_guest_host_mask;
  263. natural_width cr0_read_shadow;
  264. natural_width cr4_read_shadow;
  265. natural_width cr3_target_value0;
  266. natural_width cr3_target_value1;
  267. natural_width cr3_target_value2;
  268. natural_width cr3_target_value3;
  269. natural_width exit_qualification;
  270. natural_width guest_linear_address;
  271. natural_width guest_cr0;
  272. natural_width guest_cr3;
  273. natural_width guest_cr4;
  274. natural_width guest_es_base;
  275. natural_width guest_cs_base;
  276. natural_width guest_ss_base;
  277. natural_width guest_ds_base;
  278. natural_width guest_fs_base;
  279. natural_width guest_gs_base;
  280. natural_width guest_ldtr_base;
  281. natural_width guest_tr_base;
  282. natural_width guest_gdtr_base;
  283. natural_width guest_idtr_base;
  284. natural_width guest_dr7;
  285. natural_width guest_rsp;
  286. natural_width guest_rip;
  287. natural_width guest_rflags;
  288. natural_width guest_pending_dbg_exceptions;
  289. natural_width guest_sysenter_esp;
  290. natural_width guest_sysenter_eip;
  291. natural_width host_cr0;
  292. natural_width host_cr3;
  293. natural_width host_cr4;
  294. natural_width host_fs_base;
  295. natural_width host_gs_base;
  296. natural_width host_tr_base;
  297. natural_width host_gdtr_base;
  298. natural_width host_idtr_base;
  299. natural_width host_ia32_sysenter_esp;
  300. natural_width host_ia32_sysenter_eip;
  301. natural_width host_rsp;
  302. natural_width host_rip;
  303. natural_width paddingl[8]; /* room for future expansion */
  304. u32 pin_based_vm_exec_control;
  305. u32 cpu_based_vm_exec_control;
  306. u32 exception_bitmap;
  307. u32 page_fault_error_code_mask;
  308. u32 page_fault_error_code_match;
  309. u32 cr3_target_count;
  310. u32 vm_exit_controls;
  311. u32 vm_exit_msr_store_count;
  312. u32 vm_exit_msr_load_count;
  313. u32 vm_entry_controls;
  314. u32 vm_entry_msr_load_count;
  315. u32 vm_entry_intr_info_field;
  316. u32 vm_entry_exception_error_code;
  317. u32 vm_entry_instruction_len;
  318. u32 tpr_threshold;
  319. u32 secondary_vm_exec_control;
  320. u32 vm_instruction_error;
  321. u32 vm_exit_reason;
  322. u32 vm_exit_intr_info;
  323. u32 vm_exit_intr_error_code;
  324. u32 idt_vectoring_info_field;
  325. u32 idt_vectoring_error_code;
  326. u32 vm_exit_instruction_len;
  327. u32 vmx_instruction_info;
  328. u32 guest_es_limit;
  329. u32 guest_cs_limit;
  330. u32 guest_ss_limit;
  331. u32 guest_ds_limit;
  332. u32 guest_fs_limit;
  333. u32 guest_gs_limit;
  334. u32 guest_ldtr_limit;
  335. u32 guest_tr_limit;
  336. u32 guest_gdtr_limit;
  337. u32 guest_idtr_limit;
  338. u32 guest_es_ar_bytes;
  339. u32 guest_cs_ar_bytes;
  340. u32 guest_ss_ar_bytes;
  341. u32 guest_ds_ar_bytes;
  342. u32 guest_fs_ar_bytes;
  343. u32 guest_gs_ar_bytes;
  344. u32 guest_ldtr_ar_bytes;
  345. u32 guest_tr_ar_bytes;
  346. u32 guest_interruptibility_info;
  347. u32 guest_activity_state;
  348. u32 guest_sysenter_cs;
  349. u32 host_ia32_sysenter_cs;
  350. u32 vmx_preemption_timer_value;
  351. u32 padding32[7]; /* room for future expansion */
  352. u16 virtual_processor_id;
  353. u16 posted_intr_nv;
  354. u16 guest_es_selector;
  355. u16 guest_cs_selector;
  356. u16 guest_ss_selector;
  357. u16 guest_ds_selector;
  358. u16 guest_fs_selector;
  359. u16 guest_gs_selector;
  360. u16 guest_ldtr_selector;
  361. u16 guest_tr_selector;
  362. u16 guest_intr_status;
  363. u16 host_es_selector;
  364. u16 host_cs_selector;
  365. u16 host_ss_selector;
  366. u16 host_ds_selector;
  367. u16 host_fs_selector;
  368. u16 host_gs_selector;
  369. u16 host_tr_selector;
  370. u16 guest_pml_index;
  371. };
  372. /*
  373. * For save/restore compatibility, the vmcs12 field offsets must not change.
  374. */
  375. #define CHECK_OFFSET(field, loc) \
  376. BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
  377. "Offset of " #field " in struct vmcs12 has changed.")
  378. static inline void vmx_check_vmcs12_offsets(void) {
  379. CHECK_OFFSET(revision_id, 0);
  380. CHECK_OFFSET(abort, 4);
  381. CHECK_OFFSET(launch_state, 8);
  382. CHECK_OFFSET(io_bitmap_a, 40);
  383. CHECK_OFFSET(io_bitmap_b, 48);
  384. CHECK_OFFSET(msr_bitmap, 56);
  385. CHECK_OFFSET(vm_exit_msr_store_addr, 64);
  386. CHECK_OFFSET(vm_exit_msr_load_addr, 72);
  387. CHECK_OFFSET(vm_entry_msr_load_addr, 80);
  388. CHECK_OFFSET(tsc_offset, 88);
  389. CHECK_OFFSET(virtual_apic_page_addr, 96);
  390. CHECK_OFFSET(apic_access_addr, 104);
  391. CHECK_OFFSET(posted_intr_desc_addr, 112);
  392. CHECK_OFFSET(ept_pointer, 120);
  393. CHECK_OFFSET(eoi_exit_bitmap0, 128);
  394. CHECK_OFFSET(eoi_exit_bitmap1, 136);
  395. CHECK_OFFSET(eoi_exit_bitmap2, 144);
  396. CHECK_OFFSET(eoi_exit_bitmap3, 152);
  397. CHECK_OFFSET(xss_exit_bitmap, 160);
  398. CHECK_OFFSET(guest_physical_address, 168);
  399. CHECK_OFFSET(vmcs_link_pointer, 176);
  400. CHECK_OFFSET(guest_ia32_debugctl, 184);
  401. CHECK_OFFSET(guest_ia32_pat, 192);
  402. CHECK_OFFSET(guest_ia32_efer, 200);
  403. CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
  404. CHECK_OFFSET(guest_pdptr0, 216);
  405. CHECK_OFFSET(guest_pdptr1, 224);
  406. CHECK_OFFSET(guest_pdptr2, 232);
  407. CHECK_OFFSET(guest_pdptr3, 240);
  408. CHECK_OFFSET(guest_bndcfgs, 248);
  409. CHECK_OFFSET(host_ia32_pat, 256);
  410. CHECK_OFFSET(host_ia32_efer, 264);
  411. CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
  412. CHECK_OFFSET(vmread_bitmap, 280);
  413. CHECK_OFFSET(vmwrite_bitmap, 288);
  414. CHECK_OFFSET(vm_function_control, 296);
  415. CHECK_OFFSET(eptp_list_address, 304);
  416. CHECK_OFFSET(pml_address, 312);
  417. CHECK_OFFSET(cr0_guest_host_mask, 344);
  418. CHECK_OFFSET(cr4_guest_host_mask, 352);
  419. CHECK_OFFSET(cr0_read_shadow, 360);
  420. CHECK_OFFSET(cr4_read_shadow, 368);
  421. CHECK_OFFSET(cr3_target_value0, 376);
  422. CHECK_OFFSET(cr3_target_value1, 384);
  423. CHECK_OFFSET(cr3_target_value2, 392);
  424. CHECK_OFFSET(cr3_target_value3, 400);
  425. CHECK_OFFSET(exit_qualification, 408);
  426. CHECK_OFFSET(guest_linear_address, 416);
  427. CHECK_OFFSET(guest_cr0, 424);
  428. CHECK_OFFSET(guest_cr3, 432);
  429. CHECK_OFFSET(guest_cr4, 440);
  430. CHECK_OFFSET(guest_es_base, 448);
  431. CHECK_OFFSET(guest_cs_base, 456);
  432. CHECK_OFFSET(guest_ss_base, 464);
  433. CHECK_OFFSET(guest_ds_base, 472);
  434. CHECK_OFFSET(guest_fs_base, 480);
  435. CHECK_OFFSET(guest_gs_base, 488);
  436. CHECK_OFFSET(guest_ldtr_base, 496);
  437. CHECK_OFFSET(guest_tr_base, 504);
  438. CHECK_OFFSET(guest_gdtr_base, 512);
  439. CHECK_OFFSET(guest_idtr_base, 520);
  440. CHECK_OFFSET(guest_dr7, 528);
  441. CHECK_OFFSET(guest_rsp, 536);
  442. CHECK_OFFSET(guest_rip, 544);
  443. CHECK_OFFSET(guest_rflags, 552);
  444. CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
  445. CHECK_OFFSET(guest_sysenter_esp, 568);
  446. CHECK_OFFSET(guest_sysenter_eip, 576);
  447. CHECK_OFFSET(host_cr0, 584);
  448. CHECK_OFFSET(host_cr3, 592);
  449. CHECK_OFFSET(host_cr4, 600);
  450. CHECK_OFFSET(host_fs_base, 608);
  451. CHECK_OFFSET(host_gs_base, 616);
  452. CHECK_OFFSET(host_tr_base, 624);
  453. CHECK_OFFSET(host_gdtr_base, 632);
  454. CHECK_OFFSET(host_idtr_base, 640);
  455. CHECK_OFFSET(host_ia32_sysenter_esp, 648);
  456. CHECK_OFFSET(host_ia32_sysenter_eip, 656);
  457. CHECK_OFFSET(host_rsp, 664);
  458. CHECK_OFFSET(host_rip, 672);
  459. CHECK_OFFSET(pin_based_vm_exec_control, 744);
  460. CHECK_OFFSET(cpu_based_vm_exec_control, 748);
  461. CHECK_OFFSET(exception_bitmap, 752);
  462. CHECK_OFFSET(page_fault_error_code_mask, 756);
  463. CHECK_OFFSET(page_fault_error_code_match, 760);
  464. CHECK_OFFSET(cr3_target_count, 764);
  465. CHECK_OFFSET(vm_exit_controls, 768);
  466. CHECK_OFFSET(vm_exit_msr_store_count, 772);
  467. CHECK_OFFSET(vm_exit_msr_load_count, 776);
  468. CHECK_OFFSET(vm_entry_controls, 780);
  469. CHECK_OFFSET(vm_entry_msr_load_count, 784);
  470. CHECK_OFFSET(vm_entry_intr_info_field, 788);
  471. CHECK_OFFSET(vm_entry_exception_error_code, 792);
  472. CHECK_OFFSET(vm_entry_instruction_len, 796);
  473. CHECK_OFFSET(tpr_threshold, 800);
  474. CHECK_OFFSET(secondary_vm_exec_control, 804);
  475. CHECK_OFFSET(vm_instruction_error, 808);
  476. CHECK_OFFSET(vm_exit_reason, 812);
  477. CHECK_OFFSET(vm_exit_intr_info, 816);
  478. CHECK_OFFSET(vm_exit_intr_error_code, 820);
  479. CHECK_OFFSET(idt_vectoring_info_field, 824);
  480. CHECK_OFFSET(idt_vectoring_error_code, 828);
  481. CHECK_OFFSET(vm_exit_instruction_len, 832);
  482. CHECK_OFFSET(vmx_instruction_info, 836);
  483. CHECK_OFFSET(guest_es_limit, 840);
  484. CHECK_OFFSET(guest_cs_limit, 844);
  485. CHECK_OFFSET(guest_ss_limit, 848);
  486. CHECK_OFFSET(guest_ds_limit, 852);
  487. CHECK_OFFSET(guest_fs_limit, 856);
  488. CHECK_OFFSET(guest_gs_limit, 860);
  489. CHECK_OFFSET(guest_ldtr_limit, 864);
  490. CHECK_OFFSET(guest_tr_limit, 868);
  491. CHECK_OFFSET(guest_gdtr_limit, 872);
  492. CHECK_OFFSET(guest_idtr_limit, 876);
  493. CHECK_OFFSET(guest_es_ar_bytes, 880);
  494. CHECK_OFFSET(guest_cs_ar_bytes, 884);
  495. CHECK_OFFSET(guest_ss_ar_bytes, 888);
  496. CHECK_OFFSET(guest_ds_ar_bytes, 892);
  497. CHECK_OFFSET(guest_fs_ar_bytes, 896);
  498. CHECK_OFFSET(guest_gs_ar_bytes, 900);
  499. CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
  500. CHECK_OFFSET(guest_tr_ar_bytes, 908);
  501. CHECK_OFFSET(guest_interruptibility_info, 912);
  502. CHECK_OFFSET(guest_activity_state, 916);
  503. CHECK_OFFSET(guest_sysenter_cs, 920);
  504. CHECK_OFFSET(host_ia32_sysenter_cs, 924);
  505. CHECK_OFFSET(vmx_preemption_timer_value, 928);
  506. CHECK_OFFSET(virtual_processor_id, 960);
  507. CHECK_OFFSET(posted_intr_nv, 962);
  508. CHECK_OFFSET(guest_es_selector, 964);
  509. CHECK_OFFSET(guest_cs_selector, 966);
  510. CHECK_OFFSET(guest_ss_selector, 968);
  511. CHECK_OFFSET(guest_ds_selector, 970);
  512. CHECK_OFFSET(guest_fs_selector, 972);
  513. CHECK_OFFSET(guest_gs_selector, 974);
  514. CHECK_OFFSET(guest_ldtr_selector, 976);
  515. CHECK_OFFSET(guest_tr_selector, 978);
  516. CHECK_OFFSET(guest_intr_status, 980);
  517. CHECK_OFFSET(host_es_selector, 982);
  518. CHECK_OFFSET(host_cs_selector, 984);
  519. CHECK_OFFSET(host_ss_selector, 986);
  520. CHECK_OFFSET(host_ds_selector, 988);
  521. CHECK_OFFSET(host_fs_selector, 990);
  522. CHECK_OFFSET(host_gs_selector, 992);
  523. CHECK_OFFSET(host_tr_selector, 994);
  524. CHECK_OFFSET(guest_pml_index, 996);
  525. }
  526. /*
  527. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  528. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  529. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  530. *
  531. * IMPORTANT: Changing this value will break save/restore compatibility with
  532. * older kvm releases.
  533. */
  534. #define VMCS12_REVISION 0x11e57ed0
  535. /*
  536. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  537. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  538. * current implementation, 4K are reserved to avoid future complications.
  539. */
  540. #define VMCS12_SIZE 0x1000
  541. /*
  542. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  543. * supported VMCS12 field encoding.
  544. */
  545. #define VMCS12_MAX_FIELD_INDEX 0x17
  546. struct nested_vmx_msrs {
  547. /*
  548. * We only store the "true" versions of the VMX capability MSRs. We
  549. * generate the "non-true" versions by setting the must-be-1 bits
  550. * according to the SDM.
  551. */
  552. u32 procbased_ctls_low;
  553. u32 procbased_ctls_high;
  554. u32 secondary_ctls_low;
  555. u32 secondary_ctls_high;
  556. u32 pinbased_ctls_low;
  557. u32 pinbased_ctls_high;
  558. u32 exit_ctls_low;
  559. u32 exit_ctls_high;
  560. u32 entry_ctls_low;
  561. u32 entry_ctls_high;
  562. u32 misc_low;
  563. u32 misc_high;
  564. u32 ept_caps;
  565. u32 vpid_caps;
  566. u64 basic;
  567. u64 cr0_fixed0;
  568. u64 cr0_fixed1;
  569. u64 cr4_fixed0;
  570. u64 cr4_fixed1;
  571. u64 vmcs_enum;
  572. u64 vmfunc_controls;
  573. };
  574. /*
  575. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  576. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  577. */
  578. struct nested_vmx {
  579. /* Has the level1 guest done vmxon? */
  580. bool vmxon;
  581. gpa_t vmxon_ptr;
  582. bool pml_full;
  583. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  584. gpa_t current_vmptr;
  585. /*
  586. * Cache of the guest's VMCS, existing outside of guest memory.
  587. * Loaded from guest memory during VMPTRLD. Flushed to guest
  588. * memory during VMCLEAR and VMPTRLD.
  589. */
  590. struct vmcs12 *cached_vmcs12;
  591. /*
  592. * Indicates if the shadow vmcs must be updated with the
  593. * data hold by vmcs12
  594. */
  595. bool sync_shadow_vmcs;
  596. bool dirty_vmcs12;
  597. bool change_vmcs01_virtual_apic_mode;
  598. /* L2 must run next, and mustn't decide to exit to L1. */
  599. bool nested_run_pending;
  600. struct loaded_vmcs vmcs02;
  601. /*
  602. * Guest pages referred to in the vmcs02 with host-physical
  603. * pointers, so we must keep them pinned while L2 runs.
  604. */
  605. struct page *apic_access_page;
  606. struct page *virtual_apic_page;
  607. struct page *pi_desc_page;
  608. struct pi_desc *pi_desc;
  609. bool pi_pending;
  610. u16 posted_intr_nv;
  611. struct hrtimer preemption_timer;
  612. bool preemption_timer_expired;
  613. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  614. u64 vmcs01_debugctl;
  615. u16 vpid02;
  616. u16 last_vpid;
  617. struct nested_vmx_msrs msrs;
  618. /* SMM related state */
  619. struct {
  620. /* in VMX operation on SMM entry? */
  621. bool vmxon;
  622. /* in guest mode on SMM entry? */
  623. bool guest_mode;
  624. } smm;
  625. };
  626. #define POSTED_INTR_ON 0
  627. #define POSTED_INTR_SN 1
  628. /* Posted-Interrupt Descriptor */
  629. struct pi_desc {
  630. u32 pir[8]; /* Posted interrupt requested */
  631. union {
  632. struct {
  633. /* bit 256 - Outstanding Notification */
  634. u16 on : 1,
  635. /* bit 257 - Suppress Notification */
  636. sn : 1,
  637. /* bit 271:258 - Reserved */
  638. rsvd_1 : 14;
  639. /* bit 279:272 - Notification Vector */
  640. u8 nv;
  641. /* bit 287:280 - Reserved */
  642. u8 rsvd_2;
  643. /* bit 319:288 - Notification Destination */
  644. u32 ndst;
  645. };
  646. u64 control;
  647. };
  648. u32 rsvd[6];
  649. } __aligned(64);
  650. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  651. {
  652. return test_and_set_bit(POSTED_INTR_ON,
  653. (unsigned long *)&pi_desc->control);
  654. }
  655. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  656. {
  657. return test_and_clear_bit(POSTED_INTR_ON,
  658. (unsigned long *)&pi_desc->control);
  659. }
  660. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  661. {
  662. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  663. }
  664. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  665. {
  666. return clear_bit(POSTED_INTR_SN,
  667. (unsigned long *)&pi_desc->control);
  668. }
  669. static inline void pi_set_sn(struct pi_desc *pi_desc)
  670. {
  671. return set_bit(POSTED_INTR_SN,
  672. (unsigned long *)&pi_desc->control);
  673. }
  674. static inline void pi_clear_on(struct pi_desc *pi_desc)
  675. {
  676. clear_bit(POSTED_INTR_ON,
  677. (unsigned long *)&pi_desc->control);
  678. }
  679. static inline int pi_test_on(struct pi_desc *pi_desc)
  680. {
  681. return test_bit(POSTED_INTR_ON,
  682. (unsigned long *)&pi_desc->control);
  683. }
  684. static inline int pi_test_sn(struct pi_desc *pi_desc)
  685. {
  686. return test_bit(POSTED_INTR_SN,
  687. (unsigned long *)&pi_desc->control);
  688. }
  689. struct vcpu_vmx {
  690. struct kvm_vcpu vcpu;
  691. unsigned long host_rsp;
  692. u8 fail;
  693. u8 msr_bitmap_mode;
  694. u32 exit_intr_info;
  695. u32 idt_vectoring_info;
  696. ulong rflags;
  697. struct shared_msr_entry *guest_msrs;
  698. int nmsrs;
  699. int save_nmsrs;
  700. unsigned long host_idt_base;
  701. #ifdef CONFIG_X86_64
  702. u64 msr_host_kernel_gs_base;
  703. u64 msr_guest_kernel_gs_base;
  704. #endif
  705. u64 arch_capabilities;
  706. u64 spec_ctrl;
  707. u32 vm_entry_controls_shadow;
  708. u32 vm_exit_controls_shadow;
  709. u32 secondary_exec_control;
  710. /*
  711. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  712. * non-nested (L1) guest, it always points to vmcs01. For a nested
  713. * guest (L2), it points to a different VMCS.
  714. */
  715. struct loaded_vmcs vmcs01;
  716. struct loaded_vmcs *loaded_vmcs;
  717. bool __launched; /* temporary, used in vmx_vcpu_run */
  718. struct msr_autoload {
  719. unsigned nr;
  720. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  721. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  722. } msr_autoload;
  723. struct {
  724. int loaded;
  725. u16 fs_sel, gs_sel, ldt_sel;
  726. #ifdef CONFIG_X86_64
  727. u16 ds_sel, es_sel;
  728. #endif
  729. int gs_ldt_reload_needed;
  730. int fs_reload_needed;
  731. u64 msr_host_bndcfgs;
  732. } host_state;
  733. struct {
  734. int vm86_active;
  735. ulong save_rflags;
  736. struct kvm_segment segs[8];
  737. } rmode;
  738. struct {
  739. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  740. struct kvm_save_segment {
  741. u16 selector;
  742. unsigned long base;
  743. u32 limit;
  744. u32 ar;
  745. } seg[8];
  746. } segment_cache;
  747. int vpid;
  748. bool emulation_required;
  749. u32 exit_reason;
  750. /* Posted interrupt descriptor */
  751. struct pi_desc pi_desc;
  752. /* Support for a guest hypervisor (nested VMX) */
  753. struct nested_vmx nested;
  754. /* Dynamic PLE window. */
  755. int ple_window;
  756. bool ple_window_dirty;
  757. /* Support for PML */
  758. #define PML_ENTITY_NUM 512
  759. struct page *pml_pg;
  760. /* apic deadline value in host tsc */
  761. u64 hv_deadline_tsc;
  762. u64 current_tsc_ratio;
  763. u32 host_pkru;
  764. unsigned long host_debugctlmsr;
  765. /*
  766. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  767. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  768. * in msr_ia32_feature_control_valid_bits.
  769. */
  770. u64 msr_ia32_feature_control;
  771. u64 msr_ia32_feature_control_valid_bits;
  772. };
  773. enum segment_cache_field {
  774. SEG_FIELD_SEL = 0,
  775. SEG_FIELD_BASE = 1,
  776. SEG_FIELD_LIMIT = 2,
  777. SEG_FIELD_AR = 3,
  778. SEG_FIELD_NR = 4
  779. };
  780. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  781. {
  782. return container_of(kvm, struct kvm_vmx, kvm);
  783. }
  784. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  785. {
  786. return container_of(vcpu, struct vcpu_vmx, vcpu);
  787. }
  788. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  789. {
  790. return &(to_vmx(vcpu)->pi_desc);
  791. }
  792. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  793. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  794. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  795. #define FIELD64(number, name) \
  796. FIELD(number, name), \
  797. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  798. static u16 shadow_read_only_fields[] = {
  799. #define SHADOW_FIELD_RO(x) x,
  800. #include "vmx_shadow_fields.h"
  801. };
  802. static int max_shadow_read_only_fields =
  803. ARRAY_SIZE(shadow_read_only_fields);
  804. static u16 shadow_read_write_fields[] = {
  805. #define SHADOW_FIELD_RW(x) x,
  806. #include "vmx_shadow_fields.h"
  807. };
  808. static int max_shadow_read_write_fields =
  809. ARRAY_SIZE(shadow_read_write_fields);
  810. static const unsigned short vmcs_field_to_offset_table[] = {
  811. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  812. FIELD(POSTED_INTR_NV, posted_intr_nv),
  813. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  814. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  815. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  816. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  817. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  818. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  819. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  820. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  821. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  822. FIELD(GUEST_PML_INDEX, guest_pml_index),
  823. FIELD(HOST_ES_SELECTOR, host_es_selector),
  824. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  825. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  826. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  827. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  828. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  829. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  830. FIELD64(IO_BITMAP_A, io_bitmap_a),
  831. FIELD64(IO_BITMAP_B, io_bitmap_b),
  832. FIELD64(MSR_BITMAP, msr_bitmap),
  833. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  834. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  835. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  836. FIELD64(PML_ADDRESS, pml_address),
  837. FIELD64(TSC_OFFSET, tsc_offset),
  838. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  839. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  840. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  841. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  842. FIELD64(EPT_POINTER, ept_pointer),
  843. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  844. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  845. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  846. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  847. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  848. FIELD64(VMREAD_BITMAP, vmread_bitmap),
  849. FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
  850. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  851. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  852. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  853. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  854. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  855. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  856. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  857. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  858. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  859. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  860. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  861. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  862. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  863. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  864. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  865. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  866. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  867. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  868. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  869. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  870. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  871. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  872. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  873. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  874. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  875. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  876. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  877. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  878. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  879. FIELD(TPR_THRESHOLD, tpr_threshold),
  880. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  881. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  882. FIELD(VM_EXIT_REASON, vm_exit_reason),
  883. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  884. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  885. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  886. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  887. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  888. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  889. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  890. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  891. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  892. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  893. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  894. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  895. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  896. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  897. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  898. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  899. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  900. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  901. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  902. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  903. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  904. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  905. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  906. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  907. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  908. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  909. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  910. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  911. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  912. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  913. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  914. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  915. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  916. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  917. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  918. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  919. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  920. FIELD(EXIT_QUALIFICATION, exit_qualification),
  921. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  922. FIELD(GUEST_CR0, guest_cr0),
  923. FIELD(GUEST_CR3, guest_cr3),
  924. FIELD(GUEST_CR4, guest_cr4),
  925. FIELD(GUEST_ES_BASE, guest_es_base),
  926. FIELD(GUEST_CS_BASE, guest_cs_base),
  927. FIELD(GUEST_SS_BASE, guest_ss_base),
  928. FIELD(GUEST_DS_BASE, guest_ds_base),
  929. FIELD(GUEST_FS_BASE, guest_fs_base),
  930. FIELD(GUEST_GS_BASE, guest_gs_base),
  931. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  932. FIELD(GUEST_TR_BASE, guest_tr_base),
  933. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  934. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  935. FIELD(GUEST_DR7, guest_dr7),
  936. FIELD(GUEST_RSP, guest_rsp),
  937. FIELD(GUEST_RIP, guest_rip),
  938. FIELD(GUEST_RFLAGS, guest_rflags),
  939. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  940. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  941. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  942. FIELD(HOST_CR0, host_cr0),
  943. FIELD(HOST_CR3, host_cr3),
  944. FIELD(HOST_CR4, host_cr4),
  945. FIELD(HOST_FS_BASE, host_fs_base),
  946. FIELD(HOST_GS_BASE, host_gs_base),
  947. FIELD(HOST_TR_BASE, host_tr_base),
  948. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  949. FIELD(HOST_IDTR_BASE, host_idtr_base),
  950. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  951. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  952. FIELD(HOST_RSP, host_rsp),
  953. FIELD(HOST_RIP, host_rip),
  954. };
  955. static inline short vmcs_field_to_offset(unsigned long field)
  956. {
  957. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  958. unsigned short offset;
  959. unsigned index;
  960. if (field >> 15)
  961. return -ENOENT;
  962. index = ROL16(field, 6);
  963. if (index >= size)
  964. return -ENOENT;
  965. index = array_index_nospec(index, size);
  966. offset = vmcs_field_to_offset_table[index];
  967. if (offset == 0)
  968. return -ENOENT;
  969. return offset;
  970. }
  971. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  972. {
  973. return to_vmx(vcpu)->nested.cached_vmcs12;
  974. }
  975. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  976. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  977. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  978. static bool vmx_xsaves_supported(void);
  979. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  980. struct kvm_segment *var, int seg);
  981. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  982. struct kvm_segment *var, int seg);
  983. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  984. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  985. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  986. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  987. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  988. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  989. u16 error_code);
  990. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  991. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  992. u32 msr, int type);
  993. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  994. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  995. /*
  996. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  997. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  998. */
  999. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  1000. /*
  1001. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  1002. * can find which vCPU should be waken up.
  1003. */
  1004. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  1005. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  1006. enum {
  1007. VMX_VMREAD_BITMAP,
  1008. VMX_VMWRITE_BITMAP,
  1009. VMX_BITMAP_NR
  1010. };
  1011. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  1012. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  1013. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  1014. static bool cpu_has_load_ia32_efer;
  1015. static bool cpu_has_load_perf_global_ctrl;
  1016. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1017. static DEFINE_SPINLOCK(vmx_vpid_lock);
  1018. static struct vmcs_config {
  1019. int size;
  1020. int order;
  1021. u32 basic_cap;
  1022. u32 revision_id;
  1023. u32 pin_based_exec_ctrl;
  1024. u32 cpu_based_exec_ctrl;
  1025. u32 cpu_based_2nd_exec_ctrl;
  1026. u32 vmexit_ctrl;
  1027. u32 vmentry_ctrl;
  1028. struct nested_vmx_msrs nested;
  1029. } vmcs_config;
  1030. static struct vmx_capability {
  1031. u32 ept;
  1032. u32 vpid;
  1033. } vmx_capability;
  1034. #define VMX_SEGMENT_FIELD(seg) \
  1035. [VCPU_SREG_##seg] = { \
  1036. .selector = GUEST_##seg##_SELECTOR, \
  1037. .base = GUEST_##seg##_BASE, \
  1038. .limit = GUEST_##seg##_LIMIT, \
  1039. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  1040. }
  1041. static const struct kvm_vmx_segment_field {
  1042. unsigned selector;
  1043. unsigned base;
  1044. unsigned limit;
  1045. unsigned ar_bytes;
  1046. } kvm_vmx_segment_fields[] = {
  1047. VMX_SEGMENT_FIELD(CS),
  1048. VMX_SEGMENT_FIELD(DS),
  1049. VMX_SEGMENT_FIELD(ES),
  1050. VMX_SEGMENT_FIELD(FS),
  1051. VMX_SEGMENT_FIELD(GS),
  1052. VMX_SEGMENT_FIELD(SS),
  1053. VMX_SEGMENT_FIELD(TR),
  1054. VMX_SEGMENT_FIELD(LDTR),
  1055. };
  1056. static u64 host_efer;
  1057. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  1058. /*
  1059. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  1060. * away by decrementing the array size.
  1061. */
  1062. static const u32 vmx_msr_index[] = {
  1063. #ifdef CONFIG_X86_64
  1064. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  1065. #endif
  1066. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  1067. };
  1068. DEFINE_STATIC_KEY_FALSE(enable_evmcs);
  1069. #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
  1070. #define KVM_EVMCS_VERSION 1
  1071. #if IS_ENABLED(CONFIG_HYPERV)
  1072. static bool __read_mostly enlightened_vmcs = true;
  1073. module_param(enlightened_vmcs, bool, 0444);
  1074. static inline void evmcs_write64(unsigned long field, u64 value)
  1075. {
  1076. u16 clean_field;
  1077. int offset = get_evmcs_offset(field, &clean_field);
  1078. if (offset < 0)
  1079. return;
  1080. *(u64 *)((char *)current_evmcs + offset) = value;
  1081. current_evmcs->hv_clean_fields &= ~clean_field;
  1082. }
  1083. static inline void evmcs_write32(unsigned long field, u32 value)
  1084. {
  1085. u16 clean_field;
  1086. int offset = get_evmcs_offset(field, &clean_field);
  1087. if (offset < 0)
  1088. return;
  1089. *(u32 *)((char *)current_evmcs + offset) = value;
  1090. current_evmcs->hv_clean_fields &= ~clean_field;
  1091. }
  1092. static inline void evmcs_write16(unsigned long field, u16 value)
  1093. {
  1094. u16 clean_field;
  1095. int offset = get_evmcs_offset(field, &clean_field);
  1096. if (offset < 0)
  1097. return;
  1098. *(u16 *)((char *)current_evmcs + offset) = value;
  1099. current_evmcs->hv_clean_fields &= ~clean_field;
  1100. }
  1101. static inline u64 evmcs_read64(unsigned long field)
  1102. {
  1103. int offset = get_evmcs_offset(field, NULL);
  1104. if (offset < 0)
  1105. return 0;
  1106. return *(u64 *)((char *)current_evmcs + offset);
  1107. }
  1108. static inline u32 evmcs_read32(unsigned long field)
  1109. {
  1110. int offset = get_evmcs_offset(field, NULL);
  1111. if (offset < 0)
  1112. return 0;
  1113. return *(u32 *)((char *)current_evmcs + offset);
  1114. }
  1115. static inline u16 evmcs_read16(unsigned long field)
  1116. {
  1117. int offset = get_evmcs_offset(field, NULL);
  1118. if (offset < 0)
  1119. return 0;
  1120. return *(u16 *)((char *)current_evmcs + offset);
  1121. }
  1122. static inline void evmcs_touch_msr_bitmap(void)
  1123. {
  1124. if (unlikely(!current_evmcs))
  1125. return;
  1126. if (current_evmcs->hv_enlightenments_control.msr_bitmap)
  1127. current_evmcs->hv_clean_fields &=
  1128. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
  1129. }
  1130. static void evmcs_load(u64 phys_addr)
  1131. {
  1132. struct hv_vp_assist_page *vp_ap =
  1133. hv_get_vp_assist_page(smp_processor_id());
  1134. vp_ap->current_nested_vmcs = phys_addr;
  1135. vp_ap->enlighten_vmentry = 1;
  1136. }
  1137. static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
  1138. {
  1139. /*
  1140. * Enlightened VMCSv1 doesn't support these:
  1141. *
  1142. * POSTED_INTR_NV = 0x00000002,
  1143. * GUEST_INTR_STATUS = 0x00000810,
  1144. * APIC_ACCESS_ADDR = 0x00002014,
  1145. * POSTED_INTR_DESC_ADDR = 0x00002016,
  1146. * EOI_EXIT_BITMAP0 = 0x0000201c,
  1147. * EOI_EXIT_BITMAP1 = 0x0000201e,
  1148. * EOI_EXIT_BITMAP2 = 0x00002020,
  1149. * EOI_EXIT_BITMAP3 = 0x00002022,
  1150. */
  1151. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  1152. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1153. ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1154. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1155. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1156. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1157. ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1158. /*
  1159. * GUEST_PML_INDEX = 0x00000812,
  1160. * PML_ADDRESS = 0x0000200e,
  1161. */
  1162. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
  1163. /* VM_FUNCTION_CONTROL = 0x00002018, */
  1164. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
  1165. /*
  1166. * EPTP_LIST_ADDRESS = 0x00002024,
  1167. * VMREAD_BITMAP = 0x00002026,
  1168. * VMWRITE_BITMAP = 0x00002028,
  1169. */
  1170. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
  1171. /*
  1172. * TSC_MULTIPLIER = 0x00002032,
  1173. */
  1174. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
  1175. /*
  1176. * PLE_GAP = 0x00004020,
  1177. * PLE_WINDOW = 0x00004022,
  1178. */
  1179. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1180. /*
  1181. * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
  1182. */
  1183. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  1184. /*
  1185. * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
  1186. * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
  1187. */
  1188. vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
  1189. vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
  1190. /*
  1191. * Currently unsupported in KVM:
  1192. * GUEST_IA32_RTIT_CTL = 0x00002814,
  1193. */
  1194. }
  1195. #else /* !IS_ENABLED(CONFIG_HYPERV) */
  1196. static inline void evmcs_write64(unsigned long field, u64 value) {}
  1197. static inline void evmcs_write32(unsigned long field, u32 value) {}
  1198. static inline void evmcs_write16(unsigned long field, u16 value) {}
  1199. static inline u64 evmcs_read64(unsigned long field) { return 0; }
  1200. static inline u32 evmcs_read32(unsigned long field) { return 0; }
  1201. static inline u16 evmcs_read16(unsigned long field) { return 0; }
  1202. static inline void evmcs_load(u64 phys_addr) {}
  1203. static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
  1204. static inline void evmcs_touch_msr_bitmap(void) {}
  1205. #endif /* IS_ENABLED(CONFIG_HYPERV) */
  1206. static inline bool is_exception_n(u32 intr_info, u8 vector)
  1207. {
  1208. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1209. INTR_INFO_VALID_MASK)) ==
  1210. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  1211. }
  1212. static inline bool is_debug(u32 intr_info)
  1213. {
  1214. return is_exception_n(intr_info, DB_VECTOR);
  1215. }
  1216. static inline bool is_breakpoint(u32 intr_info)
  1217. {
  1218. return is_exception_n(intr_info, BP_VECTOR);
  1219. }
  1220. static inline bool is_page_fault(u32 intr_info)
  1221. {
  1222. return is_exception_n(intr_info, PF_VECTOR);
  1223. }
  1224. static inline bool is_no_device(u32 intr_info)
  1225. {
  1226. return is_exception_n(intr_info, NM_VECTOR);
  1227. }
  1228. static inline bool is_invalid_opcode(u32 intr_info)
  1229. {
  1230. return is_exception_n(intr_info, UD_VECTOR);
  1231. }
  1232. static inline bool is_gp_fault(u32 intr_info)
  1233. {
  1234. return is_exception_n(intr_info, GP_VECTOR);
  1235. }
  1236. static inline bool is_external_interrupt(u32 intr_info)
  1237. {
  1238. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1239. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  1240. }
  1241. static inline bool is_machine_check(u32 intr_info)
  1242. {
  1243. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1244. INTR_INFO_VALID_MASK)) ==
  1245. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  1246. }
  1247. /* Undocumented: icebp/int1 */
  1248. static inline bool is_icebp(u32 intr_info)
  1249. {
  1250. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1251. == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
  1252. }
  1253. static inline bool cpu_has_vmx_msr_bitmap(void)
  1254. {
  1255. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  1256. }
  1257. static inline bool cpu_has_vmx_tpr_shadow(void)
  1258. {
  1259. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  1260. }
  1261. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  1262. {
  1263. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  1264. }
  1265. static inline bool cpu_has_secondary_exec_ctrls(void)
  1266. {
  1267. return vmcs_config.cpu_based_exec_ctrl &
  1268. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1269. }
  1270. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  1271. {
  1272. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1273. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1274. }
  1275. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  1276. {
  1277. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1278. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  1279. }
  1280. static inline bool cpu_has_vmx_apic_register_virt(void)
  1281. {
  1282. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1283. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1284. }
  1285. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  1286. {
  1287. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1288. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1289. }
  1290. /*
  1291. * Comment's format: document - errata name - stepping - processor name.
  1292. * Refer from
  1293. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  1294. */
  1295. static u32 vmx_preemption_cpu_tfms[] = {
  1296. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  1297. 0x000206E6,
  1298. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  1299. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  1300. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  1301. 0x00020652,
  1302. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  1303. 0x00020655,
  1304. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  1305. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  1306. /*
  1307. * 320767.pdf - AAP86 - B1 -
  1308. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  1309. */
  1310. 0x000106E5,
  1311. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  1312. 0x000106A0,
  1313. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  1314. 0x000106A1,
  1315. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  1316. 0x000106A4,
  1317. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  1318. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  1319. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  1320. 0x000106A5,
  1321. };
  1322. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1323. {
  1324. u32 eax = cpuid_eax(0x00000001), i;
  1325. /* Clear the reserved bits */
  1326. eax &= ~(0x3U << 14 | 0xfU << 28);
  1327. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1328. if (eax == vmx_preemption_cpu_tfms[i])
  1329. return true;
  1330. return false;
  1331. }
  1332. static inline bool cpu_has_vmx_preemption_timer(void)
  1333. {
  1334. return vmcs_config.pin_based_exec_ctrl &
  1335. PIN_BASED_VMX_PREEMPTION_TIMER;
  1336. }
  1337. static inline bool cpu_has_vmx_posted_intr(void)
  1338. {
  1339. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1340. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1341. }
  1342. static inline bool cpu_has_vmx_apicv(void)
  1343. {
  1344. return cpu_has_vmx_apic_register_virt() &&
  1345. cpu_has_vmx_virtual_intr_delivery() &&
  1346. cpu_has_vmx_posted_intr();
  1347. }
  1348. static inline bool cpu_has_vmx_flexpriority(void)
  1349. {
  1350. return cpu_has_vmx_tpr_shadow() &&
  1351. cpu_has_vmx_virtualize_apic_accesses();
  1352. }
  1353. static inline bool cpu_has_vmx_ept_execute_only(void)
  1354. {
  1355. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1356. }
  1357. static inline bool cpu_has_vmx_ept_2m_page(void)
  1358. {
  1359. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1360. }
  1361. static inline bool cpu_has_vmx_ept_1g_page(void)
  1362. {
  1363. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1364. }
  1365. static inline bool cpu_has_vmx_ept_4levels(void)
  1366. {
  1367. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1368. }
  1369. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1370. {
  1371. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1372. }
  1373. static inline bool cpu_has_vmx_ept_5levels(void)
  1374. {
  1375. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1376. }
  1377. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1378. {
  1379. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1380. }
  1381. static inline bool cpu_has_vmx_invept_context(void)
  1382. {
  1383. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1384. }
  1385. static inline bool cpu_has_vmx_invept_global(void)
  1386. {
  1387. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1388. }
  1389. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  1390. {
  1391. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  1392. }
  1393. static inline bool cpu_has_vmx_invvpid_single(void)
  1394. {
  1395. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1396. }
  1397. static inline bool cpu_has_vmx_invvpid_global(void)
  1398. {
  1399. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1400. }
  1401. static inline bool cpu_has_vmx_invvpid(void)
  1402. {
  1403. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1404. }
  1405. static inline bool cpu_has_vmx_ept(void)
  1406. {
  1407. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1408. SECONDARY_EXEC_ENABLE_EPT;
  1409. }
  1410. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1411. {
  1412. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1413. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1414. }
  1415. static inline bool cpu_has_vmx_ple(void)
  1416. {
  1417. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1418. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1419. }
  1420. static inline bool cpu_has_vmx_basic_inout(void)
  1421. {
  1422. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1423. }
  1424. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1425. {
  1426. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1427. }
  1428. static inline bool cpu_has_vmx_vpid(void)
  1429. {
  1430. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1431. SECONDARY_EXEC_ENABLE_VPID;
  1432. }
  1433. static inline bool cpu_has_vmx_rdtscp(void)
  1434. {
  1435. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1436. SECONDARY_EXEC_RDTSCP;
  1437. }
  1438. static inline bool cpu_has_vmx_invpcid(void)
  1439. {
  1440. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1441. SECONDARY_EXEC_ENABLE_INVPCID;
  1442. }
  1443. static inline bool cpu_has_virtual_nmis(void)
  1444. {
  1445. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1446. }
  1447. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1448. {
  1449. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1450. SECONDARY_EXEC_WBINVD_EXITING;
  1451. }
  1452. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1453. {
  1454. u64 vmx_msr;
  1455. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1456. /* check if the cpu supports writing r/o exit information fields */
  1457. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1458. return false;
  1459. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1460. SECONDARY_EXEC_SHADOW_VMCS;
  1461. }
  1462. static inline bool cpu_has_vmx_pml(void)
  1463. {
  1464. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1465. }
  1466. static inline bool cpu_has_vmx_tsc_scaling(void)
  1467. {
  1468. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1469. SECONDARY_EXEC_TSC_SCALING;
  1470. }
  1471. static inline bool cpu_has_vmx_vmfunc(void)
  1472. {
  1473. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1474. SECONDARY_EXEC_ENABLE_VMFUNC;
  1475. }
  1476. static bool vmx_umip_emulated(void)
  1477. {
  1478. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1479. SECONDARY_EXEC_DESC;
  1480. }
  1481. static inline bool report_flexpriority(void)
  1482. {
  1483. return flexpriority_enabled;
  1484. }
  1485. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1486. {
  1487. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  1488. }
  1489. /*
  1490. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  1491. * to modify any valid field of the VMCS, or are the VM-exit
  1492. * information fields read-only?
  1493. */
  1494. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  1495. {
  1496. return to_vmx(vcpu)->nested.msrs.misc_low &
  1497. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  1498. }
  1499. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  1500. {
  1501. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  1502. }
  1503. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  1504. {
  1505. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  1506. CPU_BASED_MONITOR_TRAP_FLAG;
  1507. }
  1508. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1509. {
  1510. return vmcs12->cpu_based_vm_exec_control & bit;
  1511. }
  1512. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1513. {
  1514. return (vmcs12->cpu_based_vm_exec_control &
  1515. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1516. (vmcs12->secondary_vm_exec_control & bit);
  1517. }
  1518. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1519. {
  1520. return vmcs12->pin_based_vm_exec_control &
  1521. PIN_BASED_VMX_PREEMPTION_TIMER;
  1522. }
  1523. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  1524. {
  1525. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  1526. }
  1527. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1528. {
  1529. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1530. }
  1531. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1532. {
  1533. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1534. }
  1535. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1536. {
  1537. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1538. }
  1539. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1540. {
  1541. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1542. }
  1543. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1544. {
  1545. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1546. }
  1547. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1548. {
  1549. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1550. }
  1551. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1552. {
  1553. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1554. }
  1555. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1556. {
  1557. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1558. }
  1559. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1560. {
  1561. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1562. }
  1563. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1564. {
  1565. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1566. }
  1567. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1568. {
  1569. return nested_cpu_has_vmfunc(vmcs12) &&
  1570. (vmcs12->vm_function_control &
  1571. VMX_VMFUNC_EPTP_SWITCHING);
  1572. }
  1573. static inline bool is_nmi(u32 intr_info)
  1574. {
  1575. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1576. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1577. }
  1578. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1579. u32 exit_intr_info,
  1580. unsigned long exit_qualification);
  1581. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1582. struct vmcs12 *vmcs12,
  1583. u32 reason, unsigned long qualification);
  1584. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1585. {
  1586. int i;
  1587. for (i = 0; i < vmx->nmsrs; ++i)
  1588. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1589. return i;
  1590. return -1;
  1591. }
  1592. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  1593. {
  1594. struct {
  1595. u64 vpid : 16;
  1596. u64 rsvd : 48;
  1597. u64 gva;
  1598. } operand = { vpid, 0, gva };
  1599. asm volatile (__ex(ASM_VMX_INVVPID)
  1600. /* CF==1 or ZF==1 --> rc = -1 */
  1601. "; ja 1f ; ud2 ; 1:"
  1602. : : "a"(&operand), "c"(ext) : "cc", "memory");
  1603. }
  1604. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1605. {
  1606. struct {
  1607. u64 eptp, gpa;
  1608. } operand = {eptp, gpa};
  1609. asm volatile (__ex(ASM_VMX_INVEPT)
  1610. /* CF==1 or ZF==1 --> rc = -1 */
  1611. "; ja 1f ; ud2 ; 1:\n"
  1612. : : "a" (&operand), "c" (ext) : "cc", "memory");
  1613. }
  1614. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1615. {
  1616. int i;
  1617. i = __find_msr_index(vmx, msr);
  1618. if (i >= 0)
  1619. return &vmx->guest_msrs[i];
  1620. return NULL;
  1621. }
  1622. static void vmcs_clear(struct vmcs *vmcs)
  1623. {
  1624. u64 phys_addr = __pa(vmcs);
  1625. u8 error;
  1626. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  1627. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1628. : "cc", "memory");
  1629. if (error)
  1630. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1631. vmcs, phys_addr);
  1632. }
  1633. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1634. {
  1635. vmcs_clear(loaded_vmcs->vmcs);
  1636. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1637. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1638. loaded_vmcs->cpu = -1;
  1639. loaded_vmcs->launched = 0;
  1640. }
  1641. static void vmcs_load(struct vmcs *vmcs)
  1642. {
  1643. u64 phys_addr = __pa(vmcs);
  1644. u8 error;
  1645. if (static_branch_unlikely(&enable_evmcs))
  1646. return evmcs_load(phys_addr);
  1647. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  1648. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1649. : "cc", "memory");
  1650. if (error)
  1651. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1652. vmcs, phys_addr);
  1653. }
  1654. #ifdef CONFIG_KEXEC_CORE
  1655. /*
  1656. * This bitmap is used to indicate whether the vmclear
  1657. * operation is enabled on all cpus. All disabled by
  1658. * default.
  1659. */
  1660. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1661. static inline void crash_enable_local_vmclear(int cpu)
  1662. {
  1663. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1664. }
  1665. static inline void crash_disable_local_vmclear(int cpu)
  1666. {
  1667. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1668. }
  1669. static inline int crash_local_vmclear_enabled(int cpu)
  1670. {
  1671. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1672. }
  1673. static void crash_vmclear_local_loaded_vmcss(void)
  1674. {
  1675. int cpu = raw_smp_processor_id();
  1676. struct loaded_vmcs *v;
  1677. if (!crash_local_vmclear_enabled(cpu))
  1678. return;
  1679. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1680. loaded_vmcss_on_cpu_link)
  1681. vmcs_clear(v->vmcs);
  1682. }
  1683. #else
  1684. static inline void crash_enable_local_vmclear(int cpu) { }
  1685. static inline void crash_disable_local_vmclear(int cpu) { }
  1686. #endif /* CONFIG_KEXEC_CORE */
  1687. static void __loaded_vmcs_clear(void *arg)
  1688. {
  1689. struct loaded_vmcs *loaded_vmcs = arg;
  1690. int cpu = raw_smp_processor_id();
  1691. if (loaded_vmcs->cpu != cpu)
  1692. return; /* vcpu migration can race with cpu offline */
  1693. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1694. per_cpu(current_vmcs, cpu) = NULL;
  1695. crash_disable_local_vmclear(cpu);
  1696. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1697. /*
  1698. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1699. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1700. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1701. * then adds the vmcs into percpu list before it is deleted.
  1702. */
  1703. smp_wmb();
  1704. loaded_vmcs_init(loaded_vmcs);
  1705. crash_enable_local_vmclear(cpu);
  1706. }
  1707. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1708. {
  1709. int cpu = loaded_vmcs->cpu;
  1710. if (cpu != -1)
  1711. smp_call_function_single(cpu,
  1712. __loaded_vmcs_clear, loaded_vmcs, 1);
  1713. }
  1714. static inline void vpid_sync_vcpu_single(int vpid)
  1715. {
  1716. if (vpid == 0)
  1717. return;
  1718. if (cpu_has_vmx_invvpid_single())
  1719. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1720. }
  1721. static inline void vpid_sync_vcpu_global(void)
  1722. {
  1723. if (cpu_has_vmx_invvpid_global())
  1724. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1725. }
  1726. static inline void vpid_sync_context(int vpid)
  1727. {
  1728. if (cpu_has_vmx_invvpid_single())
  1729. vpid_sync_vcpu_single(vpid);
  1730. else
  1731. vpid_sync_vcpu_global();
  1732. }
  1733. static inline void ept_sync_global(void)
  1734. {
  1735. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1736. }
  1737. static inline void ept_sync_context(u64 eptp)
  1738. {
  1739. if (cpu_has_vmx_invept_context())
  1740. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1741. else
  1742. ept_sync_global();
  1743. }
  1744. static __always_inline void vmcs_check16(unsigned long field)
  1745. {
  1746. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1747. "16-bit accessor invalid for 64-bit field");
  1748. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1749. "16-bit accessor invalid for 64-bit high field");
  1750. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1751. "16-bit accessor invalid for 32-bit high field");
  1752. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1753. "16-bit accessor invalid for natural width field");
  1754. }
  1755. static __always_inline void vmcs_check32(unsigned long field)
  1756. {
  1757. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1758. "32-bit accessor invalid for 16-bit field");
  1759. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1760. "32-bit accessor invalid for natural width field");
  1761. }
  1762. static __always_inline void vmcs_check64(unsigned long field)
  1763. {
  1764. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1765. "64-bit accessor invalid for 16-bit field");
  1766. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1767. "64-bit accessor invalid for 64-bit high field");
  1768. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1769. "64-bit accessor invalid for 32-bit field");
  1770. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1771. "64-bit accessor invalid for natural width field");
  1772. }
  1773. static __always_inline void vmcs_checkl(unsigned long field)
  1774. {
  1775. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1776. "Natural width accessor invalid for 16-bit field");
  1777. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1778. "Natural width accessor invalid for 64-bit field");
  1779. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1780. "Natural width accessor invalid for 64-bit high field");
  1781. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1782. "Natural width accessor invalid for 32-bit field");
  1783. }
  1784. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  1785. {
  1786. unsigned long value;
  1787. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1788. : "=a"(value) : "d"(field) : "cc");
  1789. return value;
  1790. }
  1791. static __always_inline u16 vmcs_read16(unsigned long field)
  1792. {
  1793. vmcs_check16(field);
  1794. if (static_branch_unlikely(&enable_evmcs))
  1795. return evmcs_read16(field);
  1796. return __vmcs_readl(field);
  1797. }
  1798. static __always_inline u32 vmcs_read32(unsigned long field)
  1799. {
  1800. vmcs_check32(field);
  1801. if (static_branch_unlikely(&enable_evmcs))
  1802. return evmcs_read32(field);
  1803. return __vmcs_readl(field);
  1804. }
  1805. static __always_inline u64 vmcs_read64(unsigned long field)
  1806. {
  1807. vmcs_check64(field);
  1808. if (static_branch_unlikely(&enable_evmcs))
  1809. return evmcs_read64(field);
  1810. #ifdef CONFIG_X86_64
  1811. return __vmcs_readl(field);
  1812. #else
  1813. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  1814. #endif
  1815. }
  1816. static __always_inline unsigned long vmcs_readl(unsigned long field)
  1817. {
  1818. vmcs_checkl(field);
  1819. if (static_branch_unlikely(&enable_evmcs))
  1820. return evmcs_read64(field);
  1821. return __vmcs_readl(field);
  1822. }
  1823. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  1824. {
  1825. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  1826. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  1827. dump_stack();
  1828. }
  1829. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  1830. {
  1831. u8 error;
  1832. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  1833. : "=q"(error) : "a"(value), "d"(field) : "cc");
  1834. if (unlikely(error))
  1835. vmwrite_error(field, value);
  1836. }
  1837. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  1838. {
  1839. vmcs_check16(field);
  1840. if (static_branch_unlikely(&enable_evmcs))
  1841. return evmcs_write16(field, value);
  1842. __vmcs_writel(field, value);
  1843. }
  1844. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  1845. {
  1846. vmcs_check32(field);
  1847. if (static_branch_unlikely(&enable_evmcs))
  1848. return evmcs_write32(field, value);
  1849. __vmcs_writel(field, value);
  1850. }
  1851. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  1852. {
  1853. vmcs_check64(field);
  1854. if (static_branch_unlikely(&enable_evmcs))
  1855. return evmcs_write64(field, value);
  1856. __vmcs_writel(field, value);
  1857. #ifndef CONFIG_X86_64
  1858. asm volatile ("");
  1859. __vmcs_writel(field+1, value >> 32);
  1860. #endif
  1861. }
  1862. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  1863. {
  1864. vmcs_checkl(field);
  1865. if (static_branch_unlikely(&enable_evmcs))
  1866. return evmcs_write64(field, value);
  1867. __vmcs_writel(field, value);
  1868. }
  1869. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  1870. {
  1871. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1872. "vmcs_clear_bits does not support 64-bit fields");
  1873. if (static_branch_unlikely(&enable_evmcs))
  1874. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  1875. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  1876. }
  1877. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  1878. {
  1879. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1880. "vmcs_set_bits does not support 64-bit fields");
  1881. if (static_branch_unlikely(&enable_evmcs))
  1882. return evmcs_write32(field, evmcs_read32(field) | mask);
  1883. __vmcs_writel(field, __vmcs_readl(field) | mask);
  1884. }
  1885. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  1886. {
  1887. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  1888. }
  1889. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  1890. {
  1891. vmcs_write32(VM_ENTRY_CONTROLS, val);
  1892. vmx->vm_entry_controls_shadow = val;
  1893. }
  1894. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  1895. {
  1896. if (vmx->vm_entry_controls_shadow != val)
  1897. vm_entry_controls_init(vmx, val);
  1898. }
  1899. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  1900. {
  1901. return vmx->vm_entry_controls_shadow;
  1902. }
  1903. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1904. {
  1905. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  1906. }
  1907. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1908. {
  1909. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  1910. }
  1911. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  1912. {
  1913. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  1914. }
  1915. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  1916. {
  1917. vmcs_write32(VM_EXIT_CONTROLS, val);
  1918. vmx->vm_exit_controls_shadow = val;
  1919. }
  1920. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  1921. {
  1922. if (vmx->vm_exit_controls_shadow != val)
  1923. vm_exit_controls_init(vmx, val);
  1924. }
  1925. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  1926. {
  1927. return vmx->vm_exit_controls_shadow;
  1928. }
  1929. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1930. {
  1931. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  1932. }
  1933. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1934. {
  1935. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  1936. }
  1937. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1938. {
  1939. vmx->segment_cache.bitmask = 0;
  1940. }
  1941. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1942. unsigned field)
  1943. {
  1944. bool ret;
  1945. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1946. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1947. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1948. vmx->segment_cache.bitmask = 0;
  1949. }
  1950. ret = vmx->segment_cache.bitmask & mask;
  1951. vmx->segment_cache.bitmask |= mask;
  1952. return ret;
  1953. }
  1954. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1955. {
  1956. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1957. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1958. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1959. return *p;
  1960. }
  1961. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1962. {
  1963. ulong *p = &vmx->segment_cache.seg[seg].base;
  1964. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1965. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1966. return *p;
  1967. }
  1968. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1969. {
  1970. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1971. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1972. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1973. return *p;
  1974. }
  1975. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1976. {
  1977. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1978. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1979. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1980. return *p;
  1981. }
  1982. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1983. {
  1984. u32 eb;
  1985. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1986. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  1987. /*
  1988. * Guest access to VMware backdoor ports could legitimately
  1989. * trigger #GP because of TSS I/O permission bitmap.
  1990. * We intercept those #GP and allow access to them anyway
  1991. * as VMware does.
  1992. */
  1993. if (enable_vmware_backdoor)
  1994. eb |= (1u << GP_VECTOR);
  1995. if ((vcpu->guest_debug &
  1996. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1997. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1998. eb |= 1u << BP_VECTOR;
  1999. if (to_vmx(vcpu)->rmode.vm86_active)
  2000. eb = ~0;
  2001. if (enable_ept)
  2002. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  2003. /* When we are running a nested L2 guest and L1 specified for it a
  2004. * certain exception bitmap, we must trap the same exceptions and pass
  2005. * them to L1. When running L2, we will only handle the exceptions
  2006. * specified above if L1 did not want them.
  2007. */
  2008. if (is_guest_mode(vcpu))
  2009. eb |= get_vmcs12(vcpu)->exception_bitmap;
  2010. vmcs_write32(EXCEPTION_BITMAP, eb);
  2011. }
  2012. /*
  2013. * Check if MSR is intercepted for currently loaded MSR bitmap.
  2014. */
  2015. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  2016. {
  2017. unsigned long *msr_bitmap;
  2018. int f = sizeof(unsigned long);
  2019. if (!cpu_has_vmx_msr_bitmap())
  2020. return true;
  2021. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  2022. if (msr <= 0x1fff) {
  2023. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2024. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2025. msr &= 0x1fff;
  2026. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2027. }
  2028. return true;
  2029. }
  2030. /*
  2031. * Check if MSR is intercepted for L01 MSR bitmap.
  2032. */
  2033. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  2034. {
  2035. unsigned long *msr_bitmap;
  2036. int f = sizeof(unsigned long);
  2037. if (!cpu_has_vmx_msr_bitmap())
  2038. return true;
  2039. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  2040. if (msr <= 0x1fff) {
  2041. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2042. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2043. msr &= 0x1fff;
  2044. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2045. }
  2046. return true;
  2047. }
  2048. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2049. unsigned long entry, unsigned long exit)
  2050. {
  2051. vm_entry_controls_clearbit(vmx, entry);
  2052. vm_exit_controls_clearbit(vmx, exit);
  2053. }
  2054. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  2055. {
  2056. unsigned i;
  2057. struct msr_autoload *m = &vmx->msr_autoload;
  2058. switch (msr) {
  2059. case MSR_EFER:
  2060. if (cpu_has_load_ia32_efer) {
  2061. clear_atomic_switch_msr_special(vmx,
  2062. VM_ENTRY_LOAD_IA32_EFER,
  2063. VM_EXIT_LOAD_IA32_EFER);
  2064. return;
  2065. }
  2066. break;
  2067. case MSR_CORE_PERF_GLOBAL_CTRL:
  2068. if (cpu_has_load_perf_global_ctrl) {
  2069. clear_atomic_switch_msr_special(vmx,
  2070. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2071. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2072. return;
  2073. }
  2074. break;
  2075. }
  2076. for (i = 0; i < m->nr; ++i)
  2077. if (m->guest[i].index == msr)
  2078. break;
  2079. if (i == m->nr)
  2080. return;
  2081. --m->nr;
  2082. m->guest[i] = m->guest[m->nr];
  2083. m->host[i] = m->host[m->nr];
  2084. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  2085. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  2086. }
  2087. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2088. unsigned long entry, unsigned long exit,
  2089. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  2090. u64 guest_val, u64 host_val)
  2091. {
  2092. vmcs_write64(guest_val_vmcs, guest_val);
  2093. vmcs_write64(host_val_vmcs, host_val);
  2094. vm_entry_controls_setbit(vmx, entry);
  2095. vm_exit_controls_setbit(vmx, exit);
  2096. }
  2097. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  2098. u64 guest_val, u64 host_val)
  2099. {
  2100. unsigned i;
  2101. struct msr_autoload *m = &vmx->msr_autoload;
  2102. switch (msr) {
  2103. case MSR_EFER:
  2104. if (cpu_has_load_ia32_efer) {
  2105. add_atomic_switch_msr_special(vmx,
  2106. VM_ENTRY_LOAD_IA32_EFER,
  2107. VM_EXIT_LOAD_IA32_EFER,
  2108. GUEST_IA32_EFER,
  2109. HOST_IA32_EFER,
  2110. guest_val, host_val);
  2111. return;
  2112. }
  2113. break;
  2114. case MSR_CORE_PERF_GLOBAL_CTRL:
  2115. if (cpu_has_load_perf_global_ctrl) {
  2116. add_atomic_switch_msr_special(vmx,
  2117. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2118. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  2119. GUEST_IA32_PERF_GLOBAL_CTRL,
  2120. HOST_IA32_PERF_GLOBAL_CTRL,
  2121. guest_val, host_val);
  2122. return;
  2123. }
  2124. break;
  2125. case MSR_IA32_PEBS_ENABLE:
  2126. /* PEBS needs a quiescent period after being disabled (to write
  2127. * a record). Disabling PEBS through VMX MSR swapping doesn't
  2128. * provide that period, so a CPU could write host's record into
  2129. * guest's memory.
  2130. */
  2131. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  2132. }
  2133. for (i = 0; i < m->nr; ++i)
  2134. if (m->guest[i].index == msr)
  2135. break;
  2136. if (i == NR_AUTOLOAD_MSRS) {
  2137. printk_once(KERN_WARNING "Not enough msr switch entries. "
  2138. "Can't add msr %x\n", msr);
  2139. return;
  2140. } else if (i == m->nr) {
  2141. ++m->nr;
  2142. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  2143. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  2144. }
  2145. m->guest[i].index = msr;
  2146. m->guest[i].value = guest_val;
  2147. m->host[i].index = msr;
  2148. m->host[i].value = host_val;
  2149. }
  2150. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  2151. {
  2152. u64 guest_efer = vmx->vcpu.arch.efer;
  2153. u64 ignore_bits = 0;
  2154. if (!enable_ept) {
  2155. /*
  2156. * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
  2157. * host CPUID is more efficient than testing guest CPUID
  2158. * or CR4. Host SMEP is anyway a requirement for guest SMEP.
  2159. */
  2160. if (boot_cpu_has(X86_FEATURE_SMEP))
  2161. guest_efer |= EFER_NX;
  2162. else if (!(guest_efer & EFER_NX))
  2163. ignore_bits |= EFER_NX;
  2164. }
  2165. /*
  2166. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  2167. */
  2168. ignore_bits |= EFER_SCE;
  2169. #ifdef CONFIG_X86_64
  2170. ignore_bits |= EFER_LMA | EFER_LME;
  2171. /* SCE is meaningful only in long mode on Intel */
  2172. if (guest_efer & EFER_LMA)
  2173. ignore_bits &= ~(u64)EFER_SCE;
  2174. #endif
  2175. clear_atomic_switch_msr(vmx, MSR_EFER);
  2176. /*
  2177. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  2178. * On CPUs that support "load IA32_EFER", always switch EFER
  2179. * atomically, since it's faster than switching it manually.
  2180. */
  2181. if (cpu_has_load_ia32_efer ||
  2182. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  2183. if (!(guest_efer & EFER_LMA))
  2184. guest_efer &= ~EFER_LME;
  2185. if (guest_efer != host_efer)
  2186. add_atomic_switch_msr(vmx, MSR_EFER,
  2187. guest_efer, host_efer);
  2188. return false;
  2189. } else {
  2190. guest_efer &= ~ignore_bits;
  2191. guest_efer |= host_efer & ignore_bits;
  2192. vmx->guest_msrs[efer_offset].data = guest_efer;
  2193. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  2194. return true;
  2195. }
  2196. }
  2197. #ifdef CONFIG_X86_32
  2198. /*
  2199. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  2200. * VMCS rather than the segment table. KVM uses this helper to figure
  2201. * out the current bases to poke them into the VMCS before entry.
  2202. */
  2203. static unsigned long segment_base(u16 selector)
  2204. {
  2205. struct desc_struct *table;
  2206. unsigned long v;
  2207. if (!(selector & ~SEGMENT_RPL_MASK))
  2208. return 0;
  2209. table = get_current_gdt_ro();
  2210. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  2211. u16 ldt_selector = kvm_read_ldt();
  2212. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  2213. return 0;
  2214. table = (struct desc_struct *)segment_base(ldt_selector);
  2215. }
  2216. v = get_desc_base(&table[selector >> 3]);
  2217. return v;
  2218. }
  2219. #endif
  2220. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  2221. {
  2222. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2223. #ifdef CONFIG_X86_64
  2224. int cpu = raw_smp_processor_id();
  2225. #endif
  2226. int i;
  2227. if (vmx->host_state.loaded)
  2228. return;
  2229. vmx->host_state.loaded = 1;
  2230. /*
  2231. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  2232. * allow segment selectors with cpl > 0 or ti == 1.
  2233. */
  2234. vmx->host_state.ldt_sel = kvm_read_ldt();
  2235. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  2236. #ifdef CONFIG_X86_64
  2237. save_fsgs_for_kvm();
  2238. vmx->host_state.fs_sel = current->thread.fsindex;
  2239. vmx->host_state.gs_sel = current->thread.gsindex;
  2240. #else
  2241. savesegment(fs, vmx->host_state.fs_sel);
  2242. savesegment(gs, vmx->host_state.gs_sel);
  2243. #endif
  2244. if (!(vmx->host_state.fs_sel & 7)) {
  2245. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  2246. vmx->host_state.fs_reload_needed = 0;
  2247. } else {
  2248. vmcs_write16(HOST_FS_SELECTOR, 0);
  2249. vmx->host_state.fs_reload_needed = 1;
  2250. }
  2251. if (!(vmx->host_state.gs_sel & 7))
  2252. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  2253. else {
  2254. vmcs_write16(HOST_GS_SELECTOR, 0);
  2255. vmx->host_state.gs_ldt_reload_needed = 1;
  2256. }
  2257. #ifdef CONFIG_X86_64
  2258. savesegment(ds, vmx->host_state.ds_sel);
  2259. savesegment(es, vmx->host_state.es_sel);
  2260. vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
  2261. vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
  2262. vmx->msr_host_kernel_gs_base = current->thread.gsbase;
  2263. if (is_long_mode(&vmx->vcpu))
  2264. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2265. #else
  2266. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  2267. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  2268. #endif
  2269. if (boot_cpu_has(X86_FEATURE_MPX))
  2270. rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  2271. for (i = 0; i < vmx->save_nmsrs; ++i)
  2272. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  2273. vmx->guest_msrs[i].data,
  2274. vmx->guest_msrs[i].mask);
  2275. }
  2276. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  2277. {
  2278. if (!vmx->host_state.loaded)
  2279. return;
  2280. ++vmx->vcpu.stat.host_state_reload;
  2281. vmx->host_state.loaded = 0;
  2282. #ifdef CONFIG_X86_64
  2283. if (is_long_mode(&vmx->vcpu))
  2284. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2285. #endif
  2286. if (vmx->host_state.gs_ldt_reload_needed) {
  2287. kvm_load_ldt(vmx->host_state.ldt_sel);
  2288. #ifdef CONFIG_X86_64
  2289. load_gs_index(vmx->host_state.gs_sel);
  2290. #else
  2291. loadsegment(gs, vmx->host_state.gs_sel);
  2292. #endif
  2293. }
  2294. if (vmx->host_state.fs_reload_needed)
  2295. loadsegment(fs, vmx->host_state.fs_sel);
  2296. #ifdef CONFIG_X86_64
  2297. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  2298. loadsegment(ds, vmx->host_state.ds_sel);
  2299. loadsegment(es, vmx->host_state.es_sel);
  2300. }
  2301. #endif
  2302. invalidate_tss_limit();
  2303. #ifdef CONFIG_X86_64
  2304. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  2305. #endif
  2306. if (vmx->host_state.msr_host_bndcfgs)
  2307. wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  2308. load_fixmap_gdt(raw_smp_processor_id());
  2309. }
  2310. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  2311. {
  2312. preempt_disable();
  2313. __vmx_load_host_state(vmx);
  2314. preempt_enable();
  2315. }
  2316. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  2317. {
  2318. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2319. struct pi_desc old, new;
  2320. unsigned int dest;
  2321. /*
  2322. * In case of hot-plug or hot-unplug, we may have to undo
  2323. * vmx_vcpu_pi_put even if there is no assigned device. And we
  2324. * always keep PI.NDST up to date for simplicity: it makes the
  2325. * code easier, and CPU migration is not a fast path.
  2326. */
  2327. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  2328. return;
  2329. /*
  2330. * First handle the simple case where no cmpxchg is necessary; just
  2331. * allow posting non-urgent interrupts.
  2332. *
  2333. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  2334. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  2335. * expects the VCPU to be on the blocked_vcpu_list that matches
  2336. * PI.NDST.
  2337. */
  2338. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  2339. vcpu->cpu == cpu) {
  2340. pi_clear_sn(pi_desc);
  2341. return;
  2342. }
  2343. /* The full case. */
  2344. do {
  2345. old.control = new.control = pi_desc->control;
  2346. dest = cpu_physical_id(cpu);
  2347. if (x2apic_enabled())
  2348. new.ndst = dest;
  2349. else
  2350. new.ndst = (dest << 8) & 0xFF00;
  2351. new.sn = 0;
  2352. } while (cmpxchg64(&pi_desc->control, old.control,
  2353. new.control) != old.control);
  2354. }
  2355. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  2356. {
  2357. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  2358. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  2359. }
  2360. /*
  2361. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  2362. * vcpu mutex is already taken.
  2363. */
  2364. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2365. {
  2366. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2367. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  2368. if (!already_loaded) {
  2369. loaded_vmcs_clear(vmx->loaded_vmcs);
  2370. local_irq_disable();
  2371. crash_disable_local_vmclear(cpu);
  2372. /*
  2373. * Read loaded_vmcs->cpu should be before fetching
  2374. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  2375. * See the comments in __loaded_vmcs_clear().
  2376. */
  2377. smp_rmb();
  2378. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  2379. &per_cpu(loaded_vmcss_on_cpu, cpu));
  2380. crash_enable_local_vmclear(cpu);
  2381. local_irq_enable();
  2382. }
  2383. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  2384. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  2385. vmcs_load(vmx->loaded_vmcs->vmcs);
  2386. indirect_branch_prediction_barrier();
  2387. }
  2388. if (!already_loaded) {
  2389. void *gdt = get_current_gdt_ro();
  2390. unsigned long sysenter_esp;
  2391. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2392. /*
  2393. * Linux uses per-cpu TSS and GDT, so set these when switching
  2394. * processors. See 22.2.4.
  2395. */
  2396. vmcs_writel(HOST_TR_BASE,
  2397. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2398. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2399. /*
  2400. * VM exits change the host TR limit to 0x67 after a VM
  2401. * exit. This is okay, since 0x67 covers everything except
  2402. * the IO bitmap and have have code to handle the IO bitmap
  2403. * being lost after a VM exit.
  2404. */
  2405. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2406. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2407. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2408. vmx->loaded_vmcs->cpu = cpu;
  2409. }
  2410. /* Setup TSC multiplier */
  2411. if (kvm_has_tsc_control &&
  2412. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2413. decache_tsc_multiplier(vmx);
  2414. vmx_vcpu_pi_load(vcpu, cpu);
  2415. vmx->host_pkru = read_pkru();
  2416. vmx->host_debugctlmsr = get_debugctlmsr();
  2417. }
  2418. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2419. {
  2420. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2421. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2422. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2423. !kvm_vcpu_apicv_active(vcpu))
  2424. return;
  2425. /* Set SN when the vCPU is preempted */
  2426. if (vcpu->preempted)
  2427. pi_set_sn(pi_desc);
  2428. }
  2429. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2430. {
  2431. vmx_vcpu_pi_put(vcpu);
  2432. __vmx_load_host_state(to_vmx(vcpu));
  2433. }
  2434. static bool emulation_required(struct kvm_vcpu *vcpu)
  2435. {
  2436. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2437. }
  2438. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2439. /*
  2440. * Return the cr0 value that a nested guest would read. This is a combination
  2441. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2442. * its hypervisor (cr0_read_shadow).
  2443. */
  2444. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2445. {
  2446. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2447. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2448. }
  2449. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2450. {
  2451. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2452. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2453. }
  2454. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2455. {
  2456. unsigned long rflags, save_rflags;
  2457. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2458. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2459. rflags = vmcs_readl(GUEST_RFLAGS);
  2460. if (to_vmx(vcpu)->rmode.vm86_active) {
  2461. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2462. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2463. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2464. }
  2465. to_vmx(vcpu)->rflags = rflags;
  2466. }
  2467. return to_vmx(vcpu)->rflags;
  2468. }
  2469. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2470. {
  2471. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2472. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2473. to_vmx(vcpu)->rflags = rflags;
  2474. if (to_vmx(vcpu)->rmode.vm86_active) {
  2475. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2476. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2477. }
  2478. vmcs_writel(GUEST_RFLAGS, rflags);
  2479. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2480. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2481. }
  2482. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2483. {
  2484. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2485. int ret = 0;
  2486. if (interruptibility & GUEST_INTR_STATE_STI)
  2487. ret |= KVM_X86_SHADOW_INT_STI;
  2488. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2489. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2490. return ret;
  2491. }
  2492. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2493. {
  2494. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2495. u32 interruptibility = interruptibility_old;
  2496. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2497. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2498. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2499. else if (mask & KVM_X86_SHADOW_INT_STI)
  2500. interruptibility |= GUEST_INTR_STATE_STI;
  2501. if ((interruptibility != interruptibility_old))
  2502. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2503. }
  2504. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2505. {
  2506. unsigned long rip;
  2507. rip = kvm_rip_read(vcpu);
  2508. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2509. kvm_rip_write(vcpu, rip);
  2510. /* skipping an emulated instruction also counts */
  2511. vmx_set_interrupt_shadow(vcpu, 0);
  2512. }
  2513. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2514. unsigned long exit_qual)
  2515. {
  2516. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2517. unsigned int nr = vcpu->arch.exception.nr;
  2518. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2519. if (vcpu->arch.exception.has_error_code) {
  2520. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2521. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2522. }
  2523. if (kvm_exception_is_soft(nr))
  2524. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2525. else
  2526. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2527. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2528. vmx_get_nmi_mask(vcpu))
  2529. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2530. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2531. }
  2532. /*
  2533. * KVM wants to inject page-faults which it got to the guest. This function
  2534. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2535. */
  2536. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2537. {
  2538. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2539. unsigned int nr = vcpu->arch.exception.nr;
  2540. if (nr == PF_VECTOR) {
  2541. if (vcpu->arch.exception.nested_apf) {
  2542. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2543. return 1;
  2544. }
  2545. /*
  2546. * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
  2547. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  2548. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
  2549. * can be written only when inject_pending_event runs. This should be
  2550. * conditional on a new capability---if the capability is disabled,
  2551. * kvm_multiple_exception would write the ancillary information to
  2552. * CR2 or DR6, for backwards ABI-compatibility.
  2553. */
  2554. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2555. vcpu->arch.exception.error_code)) {
  2556. *exit_qual = vcpu->arch.cr2;
  2557. return 1;
  2558. }
  2559. } else {
  2560. if (vmcs12->exception_bitmap & (1u << nr)) {
  2561. if (nr == DB_VECTOR)
  2562. *exit_qual = vcpu->arch.dr6;
  2563. else
  2564. *exit_qual = 0;
  2565. return 1;
  2566. }
  2567. }
  2568. return 0;
  2569. }
  2570. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  2571. {
  2572. /*
  2573. * Ensure that we clear the HLT state in the VMCS. We don't need to
  2574. * explicitly skip the instruction because if the HLT state is set,
  2575. * then the instruction is already executing and RIP has already been
  2576. * advanced.
  2577. */
  2578. if (kvm_hlt_in_guest(vcpu->kvm) &&
  2579. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  2580. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2581. }
  2582. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2583. {
  2584. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2585. unsigned nr = vcpu->arch.exception.nr;
  2586. bool has_error_code = vcpu->arch.exception.has_error_code;
  2587. u32 error_code = vcpu->arch.exception.error_code;
  2588. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2589. if (has_error_code) {
  2590. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2591. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2592. }
  2593. if (vmx->rmode.vm86_active) {
  2594. int inc_eip = 0;
  2595. if (kvm_exception_is_soft(nr))
  2596. inc_eip = vcpu->arch.event_exit_inst_len;
  2597. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2598. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2599. return;
  2600. }
  2601. WARN_ON_ONCE(vmx->emulation_required);
  2602. if (kvm_exception_is_soft(nr)) {
  2603. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2604. vmx->vcpu.arch.event_exit_inst_len);
  2605. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2606. } else
  2607. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2608. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2609. vmx_clear_hlt(vcpu);
  2610. }
  2611. static bool vmx_rdtscp_supported(void)
  2612. {
  2613. return cpu_has_vmx_rdtscp();
  2614. }
  2615. static bool vmx_invpcid_supported(void)
  2616. {
  2617. return cpu_has_vmx_invpcid() && enable_ept;
  2618. }
  2619. /*
  2620. * Swap MSR entry in host/guest MSR entry array.
  2621. */
  2622. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2623. {
  2624. struct shared_msr_entry tmp;
  2625. tmp = vmx->guest_msrs[to];
  2626. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2627. vmx->guest_msrs[from] = tmp;
  2628. }
  2629. /*
  2630. * Set up the vmcs to automatically save and restore system
  2631. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2632. * mode, as fiddling with msrs is very expensive.
  2633. */
  2634. static void setup_msrs(struct vcpu_vmx *vmx)
  2635. {
  2636. int save_nmsrs, index;
  2637. save_nmsrs = 0;
  2638. #ifdef CONFIG_X86_64
  2639. if (is_long_mode(&vmx->vcpu)) {
  2640. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2641. if (index >= 0)
  2642. move_msr_up(vmx, index, save_nmsrs++);
  2643. index = __find_msr_index(vmx, MSR_LSTAR);
  2644. if (index >= 0)
  2645. move_msr_up(vmx, index, save_nmsrs++);
  2646. index = __find_msr_index(vmx, MSR_CSTAR);
  2647. if (index >= 0)
  2648. move_msr_up(vmx, index, save_nmsrs++);
  2649. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2650. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2651. move_msr_up(vmx, index, save_nmsrs++);
  2652. /*
  2653. * MSR_STAR is only needed on long mode guests, and only
  2654. * if efer.sce is enabled.
  2655. */
  2656. index = __find_msr_index(vmx, MSR_STAR);
  2657. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2658. move_msr_up(vmx, index, save_nmsrs++);
  2659. }
  2660. #endif
  2661. index = __find_msr_index(vmx, MSR_EFER);
  2662. if (index >= 0 && update_transition_efer(vmx, index))
  2663. move_msr_up(vmx, index, save_nmsrs++);
  2664. vmx->save_nmsrs = save_nmsrs;
  2665. if (cpu_has_vmx_msr_bitmap())
  2666. vmx_update_msr_bitmap(&vmx->vcpu);
  2667. }
  2668. static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  2669. {
  2670. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2671. if (is_guest_mode(vcpu) &&
  2672. (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
  2673. return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
  2674. return vcpu->arch.tsc_offset;
  2675. }
  2676. /*
  2677. * writes 'offset' into guest's timestamp counter offset register
  2678. */
  2679. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2680. {
  2681. if (is_guest_mode(vcpu)) {
  2682. /*
  2683. * We're here if L1 chose not to trap WRMSR to TSC. According
  2684. * to the spec, this should set L1's TSC; The offset that L1
  2685. * set for L2 remains unchanged, and still needs to be added
  2686. * to the newly set TSC to get L2's TSC.
  2687. */
  2688. struct vmcs12 *vmcs12;
  2689. /* recalculate vmcs02.TSC_OFFSET: */
  2690. vmcs12 = get_vmcs12(vcpu);
  2691. vmcs_write64(TSC_OFFSET, offset +
  2692. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  2693. vmcs12->tsc_offset : 0));
  2694. } else {
  2695. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2696. vmcs_read64(TSC_OFFSET), offset);
  2697. vmcs_write64(TSC_OFFSET, offset);
  2698. }
  2699. }
  2700. /*
  2701. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2702. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2703. * all guests if the "nested" module option is off, and can also be disabled
  2704. * for a single guest by disabling its VMX cpuid bit.
  2705. */
  2706. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2707. {
  2708. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2709. }
  2710. /*
  2711. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2712. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2713. * The same values should also be used to verify that vmcs12 control fields are
  2714. * valid during nested entry from L1 to L2.
  2715. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2716. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2717. * bit in the high half is on if the corresponding bit in the control field
  2718. * may be on. See also vmx_control_verify().
  2719. */
  2720. static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
  2721. {
  2722. if (!nested) {
  2723. memset(msrs, 0, sizeof(*msrs));
  2724. return;
  2725. }
  2726. /*
  2727. * Note that as a general rule, the high half of the MSRs (bits in
  2728. * the control fields which may be 1) should be initialized by the
  2729. * intersection of the underlying hardware's MSR (i.e., features which
  2730. * can be supported) and the list of features we want to expose -
  2731. * because they are known to be properly supported in our code.
  2732. * Also, usually, the low half of the MSRs (bits which must be 1) can
  2733. * be set to 0, meaning that L1 may turn off any of these bits. The
  2734. * reason is that if one of these bits is necessary, it will appear
  2735. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  2736. * fields of vmcs01 and vmcs02, will turn these bits off - and
  2737. * nested_vmx_exit_reflected() will not pass related exits to L1.
  2738. * These rules have exceptions below.
  2739. */
  2740. /* pin-based controls */
  2741. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2742. msrs->pinbased_ctls_low,
  2743. msrs->pinbased_ctls_high);
  2744. msrs->pinbased_ctls_low |=
  2745. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2746. msrs->pinbased_ctls_high &=
  2747. PIN_BASED_EXT_INTR_MASK |
  2748. PIN_BASED_NMI_EXITING |
  2749. PIN_BASED_VIRTUAL_NMIS |
  2750. (apicv ? PIN_BASED_POSTED_INTR : 0);
  2751. msrs->pinbased_ctls_high |=
  2752. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2753. PIN_BASED_VMX_PREEMPTION_TIMER;
  2754. /* exit controls */
  2755. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  2756. msrs->exit_ctls_low,
  2757. msrs->exit_ctls_high);
  2758. msrs->exit_ctls_low =
  2759. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2760. msrs->exit_ctls_high &=
  2761. #ifdef CONFIG_X86_64
  2762. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  2763. #endif
  2764. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  2765. msrs->exit_ctls_high |=
  2766. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  2767. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  2768. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  2769. if (kvm_mpx_supported())
  2770. msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  2771. /* We support free control of debug control saving. */
  2772. msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  2773. /* entry controls */
  2774. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  2775. msrs->entry_ctls_low,
  2776. msrs->entry_ctls_high);
  2777. msrs->entry_ctls_low =
  2778. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2779. msrs->entry_ctls_high &=
  2780. #ifdef CONFIG_X86_64
  2781. VM_ENTRY_IA32E_MODE |
  2782. #endif
  2783. VM_ENTRY_LOAD_IA32_PAT;
  2784. msrs->entry_ctls_high |=
  2785. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  2786. if (kvm_mpx_supported())
  2787. msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  2788. /* We support free control of debug control loading. */
  2789. msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2790. /* cpu-based controls */
  2791. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  2792. msrs->procbased_ctls_low,
  2793. msrs->procbased_ctls_high);
  2794. msrs->procbased_ctls_low =
  2795. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2796. msrs->procbased_ctls_high &=
  2797. CPU_BASED_VIRTUAL_INTR_PENDING |
  2798. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  2799. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  2800. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  2801. CPU_BASED_CR3_STORE_EXITING |
  2802. #ifdef CONFIG_X86_64
  2803. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  2804. #endif
  2805. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  2806. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  2807. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  2808. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  2809. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2810. /*
  2811. * We can allow some features even when not supported by the
  2812. * hardware. For example, L1 can specify an MSR bitmap - and we
  2813. * can use it to avoid exits to L1 - even when L0 runs L2
  2814. * without MSR bitmaps.
  2815. */
  2816. msrs->procbased_ctls_high |=
  2817. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2818. CPU_BASED_USE_MSR_BITMAPS;
  2819. /* We support free control of CR3 access interception. */
  2820. msrs->procbased_ctls_low &=
  2821. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  2822. /*
  2823. * secondary cpu-based controls. Do not include those that
  2824. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  2825. */
  2826. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  2827. msrs->secondary_ctls_low,
  2828. msrs->secondary_ctls_high);
  2829. msrs->secondary_ctls_low = 0;
  2830. msrs->secondary_ctls_high &=
  2831. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2832. SECONDARY_EXEC_DESC |
  2833. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2834. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2835. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2836. SECONDARY_EXEC_WBINVD_EXITING;
  2837. if (enable_ept) {
  2838. /* nested EPT: emulate EPT also to L1 */
  2839. msrs->secondary_ctls_high |=
  2840. SECONDARY_EXEC_ENABLE_EPT;
  2841. msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  2842. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  2843. if (cpu_has_vmx_ept_execute_only())
  2844. msrs->ept_caps |=
  2845. VMX_EPT_EXECUTE_ONLY_BIT;
  2846. msrs->ept_caps &= vmx_capability.ept;
  2847. msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  2848. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  2849. VMX_EPT_1GB_PAGE_BIT;
  2850. if (enable_ept_ad_bits) {
  2851. msrs->secondary_ctls_high |=
  2852. SECONDARY_EXEC_ENABLE_PML;
  2853. msrs->ept_caps |= VMX_EPT_AD_BIT;
  2854. }
  2855. }
  2856. if (cpu_has_vmx_vmfunc()) {
  2857. msrs->secondary_ctls_high |=
  2858. SECONDARY_EXEC_ENABLE_VMFUNC;
  2859. /*
  2860. * Advertise EPTP switching unconditionally
  2861. * since we emulate it
  2862. */
  2863. if (enable_ept)
  2864. msrs->vmfunc_controls =
  2865. VMX_VMFUNC_EPTP_SWITCHING;
  2866. }
  2867. /*
  2868. * Old versions of KVM use the single-context version without
  2869. * checking for support, so declare that it is supported even
  2870. * though it is treated as global context. The alternative is
  2871. * not failing the single-context invvpid, and it is worse.
  2872. */
  2873. if (enable_vpid) {
  2874. msrs->secondary_ctls_high |=
  2875. SECONDARY_EXEC_ENABLE_VPID;
  2876. msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
  2877. VMX_VPID_EXTENT_SUPPORTED_MASK;
  2878. }
  2879. if (enable_unrestricted_guest)
  2880. msrs->secondary_ctls_high |=
  2881. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2882. /* miscellaneous data */
  2883. rdmsr(MSR_IA32_VMX_MISC,
  2884. msrs->misc_low,
  2885. msrs->misc_high);
  2886. msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
  2887. msrs->misc_low |=
  2888. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
  2889. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  2890. VMX_MISC_ACTIVITY_HLT;
  2891. msrs->misc_high = 0;
  2892. /*
  2893. * This MSR reports some information about VMX support. We
  2894. * should return information about the VMX we emulate for the
  2895. * guest, and the VMCS structure we give it - not about the
  2896. * VMX support of the underlying hardware.
  2897. */
  2898. msrs->basic =
  2899. VMCS12_REVISION |
  2900. VMX_BASIC_TRUE_CTLS |
  2901. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  2902. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  2903. if (cpu_has_vmx_basic_inout())
  2904. msrs->basic |= VMX_BASIC_INOUT;
  2905. /*
  2906. * These MSRs specify bits which the guest must keep fixed on
  2907. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  2908. * We picked the standard core2 setting.
  2909. */
  2910. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  2911. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  2912. msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
  2913. msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
  2914. /* These MSRs specify bits which the guest must keep fixed off. */
  2915. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
  2916. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
  2917. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  2918. msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  2919. }
  2920. /*
  2921. * if fixed0[i] == 1: val[i] must be 1
  2922. * if fixed1[i] == 0: val[i] must be 0
  2923. */
  2924. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  2925. {
  2926. return ((val & fixed1) | fixed0) == val;
  2927. }
  2928. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  2929. {
  2930. return fixed_bits_valid(control, low, high);
  2931. }
  2932. static inline u64 vmx_control_msr(u32 low, u32 high)
  2933. {
  2934. return low | ((u64)high << 32);
  2935. }
  2936. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  2937. {
  2938. superset &= mask;
  2939. subset &= mask;
  2940. return (superset | subset) == superset;
  2941. }
  2942. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  2943. {
  2944. const u64 feature_and_reserved =
  2945. /* feature (except bit 48; see below) */
  2946. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  2947. /* reserved */
  2948. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  2949. u64 vmx_basic = vmx->nested.msrs.basic;
  2950. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  2951. return -EINVAL;
  2952. /*
  2953. * KVM does not emulate a version of VMX that constrains physical
  2954. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  2955. */
  2956. if (data & BIT_ULL(48))
  2957. return -EINVAL;
  2958. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  2959. vmx_basic_vmcs_revision_id(data))
  2960. return -EINVAL;
  2961. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  2962. return -EINVAL;
  2963. vmx->nested.msrs.basic = data;
  2964. return 0;
  2965. }
  2966. static int
  2967. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  2968. {
  2969. u64 supported;
  2970. u32 *lowp, *highp;
  2971. switch (msr_index) {
  2972. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2973. lowp = &vmx->nested.msrs.pinbased_ctls_low;
  2974. highp = &vmx->nested.msrs.pinbased_ctls_high;
  2975. break;
  2976. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2977. lowp = &vmx->nested.msrs.procbased_ctls_low;
  2978. highp = &vmx->nested.msrs.procbased_ctls_high;
  2979. break;
  2980. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2981. lowp = &vmx->nested.msrs.exit_ctls_low;
  2982. highp = &vmx->nested.msrs.exit_ctls_high;
  2983. break;
  2984. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2985. lowp = &vmx->nested.msrs.entry_ctls_low;
  2986. highp = &vmx->nested.msrs.entry_ctls_high;
  2987. break;
  2988. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2989. lowp = &vmx->nested.msrs.secondary_ctls_low;
  2990. highp = &vmx->nested.msrs.secondary_ctls_high;
  2991. break;
  2992. default:
  2993. BUG();
  2994. }
  2995. supported = vmx_control_msr(*lowp, *highp);
  2996. /* Check must-be-1 bits are still 1. */
  2997. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  2998. return -EINVAL;
  2999. /* Check must-be-0 bits are still 0. */
  3000. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  3001. return -EINVAL;
  3002. *lowp = data;
  3003. *highp = data >> 32;
  3004. return 0;
  3005. }
  3006. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  3007. {
  3008. const u64 feature_and_reserved_bits =
  3009. /* feature */
  3010. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  3011. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  3012. /* reserved */
  3013. GENMASK_ULL(13, 9) | BIT_ULL(31);
  3014. u64 vmx_misc;
  3015. vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
  3016. vmx->nested.msrs.misc_high);
  3017. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  3018. return -EINVAL;
  3019. if ((vmx->nested.msrs.pinbased_ctls_high &
  3020. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  3021. vmx_misc_preemption_timer_rate(data) !=
  3022. vmx_misc_preemption_timer_rate(vmx_misc))
  3023. return -EINVAL;
  3024. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  3025. return -EINVAL;
  3026. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  3027. return -EINVAL;
  3028. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  3029. return -EINVAL;
  3030. vmx->nested.msrs.misc_low = data;
  3031. vmx->nested.msrs.misc_high = data >> 32;
  3032. /*
  3033. * If L1 has read-only VM-exit information fields, use the
  3034. * less permissive vmx_vmwrite_bitmap to specify write
  3035. * permissions for the shadow VMCS.
  3036. */
  3037. if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  3038. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3039. return 0;
  3040. }
  3041. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  3042. {
  3043. u64 vmx_ept_vpid_cap;
  3044. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
  3045. vmx->nested.msrs.vpid_caps);
  3046. /* Every bit is either reserved or a feature bit. */
  3047. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  3048. return -EINVAL;
  3049. vmx->nested.msrs.ept_caps = data;
  3050. vmx->nested.msrs.vpid_caps = data >> 32;
  3051. return 0;
  3052. }
  3053. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3054. {
  3055. u64 *msr;
  3056. switch (msr_index) {
  3057. case MSR_IA32_VMX_CR0_FIXED0:
  3058. msr = &vmx->nested.msrs.cr0_fixed0;
  3059. break;
  3060. case MSR_IA32_VMX_CR4_FIXED0:
  3061. msr = &vmx->nested.msrs.cr4_fixed0;
  3062. break;
  3063. default:
  3064. BUG();
  3065. }
  3066. /*
  3067. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  3068. * must be 1 in the restored value.
  3069. */
  3070. if (!is_bitwise_subset(data, *msr, -1ULL))
  3071. return -EINVAL;
  3072. *msr = data;
  3073. return 0;
  3074. }
  3075. /*
  3076. * Called when userspace is restoring VMX MSRs.
  3077. *
  3078. * Returns 0 on success, non-0 otherwise.
  3079. */
  3080. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  3081. {
  3082. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3083. /*
  3084. * Don't allow changes to the VMX capability MSRs while the vCPU
  3085. * is in VMX operation.
  3086. */
  3087. if (vmx->nested.vmxon)
  3088. return -EBUSY;
  3089. switch (msr_index) {
  3090. case MSR_IA32_VMX_BASIC:
  3091. return vmx_restore_vmx_basic(vmx, data);
  3092. case MSR_IA32_VMX_PINBASED_CTLS:
  3093. case MSR_IA32_VMX_PROCBASED_CTLS:
  3094. case MSR_IA32_VMX_EXIT_CTLS:
  3095. case MSR_IA32_VMX_ENTRY_CTLS:
  3096. /*
  3097. * The "non-true" VMX capability MSRs are generated from the
  3098. * "true" MSRs, so we do not support restoring them directly.
  3099. *
  3100. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  3101. * should restore the "true" MSRs with the must-be-1 bits
  3102. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  3103. * DEFAULT SETTINGS".
  3104. */
  3105. return -EINVAL;
  3106. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3107. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3108. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3109. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3110. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3111. return vmx_restore_control_msr(vmx, msr_index, data);
  3112. case MSR_IA32_VMX_MISC:
  3113. return vmx_restore_vmx_misc(vmx, data);
  3114. case MSR_IA32_VMX_CR0_FIXED0:
  3115. case MSR_IA32_VMX_CR4_FIXED0:
  3116. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  3117. case MSR_IA32_VMX_CR0_FIXED1:
  3118. case MSR_IA32_VMX_CR4_FIXED1:
  3119. /*
  3120. * These MSRs are generated based on the vCPU's CPUID, so we
  3121. * do not support restoring them directly.
  3122. */
  3123. return -EINVAL;
  3124. case MSR_IA32_VMX_EPT_VPID_CAP:
  3125. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  3126. case MSR_IA32_VMX_VMCS_ENUM:
  3127. vmx->nested.msrs.vmcs_enum = data;
  3128. return 0;
  3129. default:
  3130. /*
  3131. * The rest of the VMX capability MSRs do not support restore.
  3132. */
  3133. return -EINVAL;
  3134. }
  3135. }
  3136. /* Returns 0 on success, non-0 otherwise. */
  3137. static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
  3138. {
  3139. switch (msr_index) {
  3140. case MSR_IA32_VMX_BASIC:
  3141. *pdata = msrs->basic;
  3142. break;
  3143. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3144. case MSR_IA32_VMX_PINBASED_CTLS:
  3145. *pdata = vmx_control_msr(
  3146. msrs->pinbased_ctls_low,
  3147. msrs->pinbased_ctls_high);
  3148. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  3149. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3150. break;
  3151. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3152. case MSR_IA32_VMX_PROCBASED_CTLS:
  3153. *pdata = vmx_control_msr(
  3154. msrs->procbased_ctls_low,
  3155. msrs->procbased_ctls_high);
  3156. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  3157. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3158. break;
  3159. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3160. case MSR_IA32_VMX_EXIT_CTLS:
  3161. *pdata = vmx_control_msr(
  3162. msrs->exit_ctls_low,
  3163. msrs->exit_ctls_high);
  3164. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  3165. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3166. break;
  3167. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3168. case MSR_IA32_VMX_ENTRY_CTLS:
  3169. *pdata = vmx_control_msr(
  3170. msrs->entry_ctls_low,
  3171. msrs->entry_ctls_high);
  3172. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  3173. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3174. break;
  3175. case MSR_IA32_VMX_MISC:
  3176. *pdata = vmx_control_msr(
  3177. msrs->misc_low,
  3178. msrs->misc_high);
  3179. break;
  3180. case MSR_IA32_VMX_CR0_FIXED0:
  3181. *pdata = msrs->cr0_fixed0;
  3182. break;
  3183. case MSR_IA32_VMX_CR0_FIXED1:
  3184. *pdata = msrs->cr0_fixed1;
  3185. break;
  3186. case MSR_IA32_VMX_CR4_FIXED0:
  3187. *pdata = msrs->cr4_fixed0;
  3188. break;
  3189. case MSR_IA32_VMX_CR4_FIXED1:
  3190. *pdata = msrs->cr4_fixed1;
  3191. break;
  3192. case MSR_IA32_VMX_VMCS_ENUM:
  3193. *pdata = msrs->vmcs_enum;
  3194. break;
  3195. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3196. *pdata = vmx_control_msr(
  3197. msrs->secondary_ctls_low,
  3198. msrs->secondary_ctls_high);
  3199. break;
  3200. case MSR_IA32_VMX_EPT_VPID_CAP:
  3201. *pdata = msrs->ept_caps |
  3202. ((u64)msrs->vpid_caps << 32);
  3203. break;
  3204. case MSR_IA32_VMX_VMFUNC:
  3205. *pdata = msrs->vmfunc_controls;
  3206. break;
  3207. default:
  3208. return 1;
  3209. }
  3210. return 0;
  3211. }
  3212. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  3213. uint64_t val)
  3214. {
  3215. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  3216. return !(val & ~valid_bits);
  3217. }
  3218. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  3219. {
  3220. switch (msr->index) {
  3221. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3222. if (!nested)
  3223. return 1;
  3224. return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
  3225. default:
  3226. return 1;
  3227. }
  3228. return 0;
  3229. }
  3230. /*
  3231. * Reads an msr value (of 'msr_index') into 'pdata'.
  3232. * Returns 0 on success, non-0 otherwise.
  3233. * Assumes vcpu_load() was already called.
  3234. */
  3235. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3236. {
  3237. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3238. struct shared_msr_entry *msr;
  3239. switch (msr_info->index) {
  3240. #ifdef CONFIG_X86_64
  3241. case MSR_FS_BASE:
  3242. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  3243. break;
  3244. case MSR_GS_BASE:
  3245. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  3246. break;
  3247. case MSR_KERNEL_GS_BASE:
  3248. vmx_load_host_state(vmx);
  3249. msr_info->data = vmx->msr_guest_kernel_gs_base;
  3250. break;
  3251. #endif
  3252. case MSR_EFER:
  3253. return kvm_get_msr_common(vcpu, msr_info);
  3254. case MSR_IA32_SPEC_CTRL:
  3255. if (!msr_info->host_initiated &&
  3256. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3257. return 1;
  3258. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  3259. break;
  3260. case MSR_IA32_ARCH_CAPABILITIES:
  3261. if (!msr_info->host_initiated &&
  3262. !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
  3263. return 1;
  3264. msr_info->data = to_vmx(vcpu)->arch_capabilities;
  3265. break;
  3266. case MSR_IA32_SYSENTER_CS:
  3267. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  3268. break;
  3269. case MSR_IA32_SYSENTER_EIP:
  3270. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  3271. break;
  3272. case MSR_IA32_SYSENTER_ESP:
  3273. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  3274. break;
  3275. case MSR_IA32_BNDCFGS:
  3276. if (!kvm_mpx_supported() ||
  3277. (!msr_info->host_initiated &&
  3278. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3279. return 1;
  3280. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  3281. break;
  3282. case MSR_IA32_MCG_EXT_CTL:
  3283. if (!msr_info->host_initiated &&
  3284. !(vmx->msr_ia32_feature_control &
  3285. FEATURE_CONTROL_LMCE))
  3286. return 1;
  3287. msr_info->data = vcpu->arch.mcg_ext_ctl;
  3288. break;
  3289. case MSR_IA32_FEATURE_CONTROL:
  3290. msr_info->data = vmx->msr_ia32_feature_control;
  3291. break;
  3292. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3293. if (!nested_vmx_allowed(vcpu))
  3294. return 1;
  3295. return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
  3296. &msr_info->data);
  3297. case MSR_IA32_XSS:
  3298. if (!vmx_xsaves_supported())
  3299. return 1;
  3300. msr_info->data = vcpu->arch.ia32_xss;
  3301. break;
  3302. case MSR_TSC_AUX:
  3303. if (!msr_info->host_initiated &&
  3304. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3305. return 1;
  3306. /* Otherwise falls through */
  3307. default:
  3308. msr = find_msr_entry(vmx, msr_info->index);
  3309. if (msr) {
  3310. msr_info->data = msr->data;
  3311. break;
  3312. }
  3313. return kvm_get_msr_common(vcpu, msr_info);
  3314. }
  3315. return 0;
  3316. }
  3317. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  3318. /*
  3319. * Writes msr value into into the appropriate "register".
  3320. * Returns 0 on success, non-0 otherwise.
  3321. * Assumes vcpu_load() was already called.
  3322. */
  3323. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3324. {
  3325. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3326. struct shared_msr_entry *msr;
  3327. int ret = 0;
  3328. u32 msr_index = msr_info->index;
  3329. u64 data = msr_info->data;
  3330. switch (msr_index) {
  3331. case MSR_EFER:
  3332. ret = kvm_set_msr_common(vcpu, msr_info);
  3333. break;
  3334. #ifdef CONFIG_X86_64
  3335. case MSR_FS_BASE:
  3336. vmx_segment_cache_clear(vmx);
  3337. vmcs_writel(GUEST_FS_BASE, data);
  3338. break;
  3339. case MSR_GS_BASE:
  3340. vmx_segment_cache_clear(vmx);
  3341. vmcs_writel(GUEST_GS_BASE, data);
  3342. break;
  3343. case MSR_KERNEL_GS_BASE:
  3344. vmx_load_host_state(vmx);
  3345. vmx->msr_guest_kernel_gs_base = data;
  3346. break;
  3347. #endif
  3348. case MSR_IA32_SYSENTER_CS:
  3349. vmcs_write32(GUEST_SYSENTER_CS, data);
  3350. break;
  3351. case MSR_IA32_SYSENTER_EIP:
  3352. vmcs_writel(GUEST_SYSENTER_EIP, data);
  3353. break;
  3354. case MSR_IA32_SYSENTER_ESP:
  3355. vmcs_writel(GUEST_SYSENTER_ESP, data);
  3356. break;
  3357. case MSR_IA32_BNDCFGS:
  3358. if (!kvm_mpx_supported() ||
  3359. (!msr_info->host_initiated &&
  3360. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3361. return 1;
  3362. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  3363. (data & MSR_IA32_BNDCFGS_RSVD))
  3364. return 1;
  3365. vmcs_write64(GUEST_BNDCFGS, data);
  3366. break;
  3367. case MSR_IA32_SPEC_CTRL:
  3368. if (!msr_info->host_initiated &&
  3369. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3370. return 1;
  3371. /* The STIBP bit doesn't fault even if it's not advertised */
  3372. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3373. return 1;
  3374. vmx->spec_ctrl = data;
  3375. if (!data)
  3376. break;
  3377. /*
  3378. * For non-nested:
  3379. * When it's written (to non-zero) for the first time, pass
  3380. * it through.
  3381. *
  3382. * For nested:
  3383. * The handling of the MSR bitmap for L2 guests is done in
  3384. * nested_vmx_merge_msr_bitmap. We should not touch the
  3385. * vmcs02.msr_bitmap here since it gets completely overwritten
  3386. * in the merging. We update the vmcs01 here for L1 as well
  3387. * since it will end up touching the MSR anyway now.
  3388. */
  3389. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  3390. MSR_IA32_SPEC_CTRL,
  3391. MSR_TYPE_RW);
  3392. break;
  3393. case MSR_IA32_PRED_CMD:
  3394. if (!msr_info->host_initiated &&
  3395. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3396. return 1;
  3397. if (data & ~PRED_CMD_IBPB)
  3398. return 1;
  3399. if (!data)
  3400. break;
  3401. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3402. /*
  3403. * For non-nested:
  3404. * When it's written (to non-zero) for the first time, pass
  3405. * it through.
  3406. *
  3407. * For nested:
  3408. * The handling of the MSR bitmap for L2 guests is done in
  3409. * nested_vmx_merge_msr_bitmap. We should not touch the
  3410. * vmcs02.msr_bitmap here since it gets completely overwritten
  3411. * in the merging.
  3412. */
  3413. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  3414. MSR_TYPE_W);
  3415. break;
  3416. case MSR_IA32_ARCH_CAPABILITIES:
  3417. if (!msr_info->host_initiated)
  3418. return 1;
  3419. vmx->arch_capabilities = data;
  3420. break;
  3421. case MSR_IA32_CR_PAT:
  3422. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3423. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3424. return 1;
  3425. vmcs_write64(GUEST_IA32_PAT, data);
  3426. vcpu->arch.pat = data;
  3427. break;
  3428. }
  3429. ret = kvm_set_msr_common(vcpu, msr_info);
  3430. break;
  3431. case MSR_IA32_TSC_ADJUST:
  3432. ret = kvm_set_msr_common(vcpu, msr_info);
  3433. break;
  3434. case MSR_IA32_MCG_EXT_CTL:
  3435. if ((!msr_info->host_initiated &&
  3436. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3437. FEATURE_CONTROL_LMCE)) ||
  3438. (data & ~MCG_EXT_CTL_LMCE_EN))
  3439. return 1;
  3440. vcpu->arch.mcg_ext_ctl = data;
  3441. break;
  3442. case MSR_IA32_FEATURE_CONTROL:
  3443. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3444. (to_vmx(vcpu)->msr_ia32_feature_control &
  3445. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3446. return 1;
  3447. vmx->msr_ia32_feature_control = data;
  3448. if (msr_info->host_initiated && data == 0)
  3449. vmx_leave_nested(vcpu);
  3450. break;
  3451. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3452. if (!msr_info->host_initiated)
  3453. return 1; /* they are read-only */
  3454. if (!nested_vmx_allowed(vcpu))
  3455. return 1;
  3456. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3457. case MSR_IA32_XSS:
  3458. if (!vmx_xsaves_supported())
  3459. return 1;
  3460. /*
  3461. * The only supported bit as of Skylake is bit 8, but
  3462. * it is not supported on KVM.
  3463. */
  3464. if (data != 0)
  3465. return 1;
  3466. vcpu->arch.ia32_xss = data;
  3467. if (vcpu->arch.ia32_xss != host_xss)
  3468. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3469. vcpu->arch.ia32_xss, host_xss);
  3470. else
  3471. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3472. break;
  3473. case MSR_TSC_AUX:
  3474. if (!msr_info->host_initiated &&
  3475. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3476. return 1;
  3477. /* Check reserved bit, higher 32 bits should be zero */
  3478. if ((data >> 32) != 0)
  3479. return 1;
  3480. /* Otherwise falls through */
  3481. default:
  3482. msr = find_msr_entry(vmx, msr_index);
  3483. if (msr) {
  3484. u64 old_msr_data = msr->data;
  3485. msr->data = data;
  3486. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3487. preempt_disable();
  3488. ret = kvm_set_shared_msr(msr->index, msr->data,
  3489. msr->mask);
  3490. preempt_enable();
  3491. if (ret)
  3492. msr->data = old_msr_data;
  3493. }
  3494. break;
  3495. }
  3496. ret = kvm_set_msr_common(vcpu, msr_info);
  3497. }
  3498. return ret;
  3499. }
  3500. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3501. {
  3502. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3503. switch (reg) {
  3504. case VCPU_REGS_RSP:
  3505. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3506. break;
  3507. case VCPU_REGS_RIP:
  3508. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3509. break;
  3510. case VCPU_EXREG_PDPTR:
  3511. if (enable_ept)
  3512. ept_save_pdptrs(vcpu);
  3513. break;
  3514. default:
  3515. break;
  3516. }
  3517. }
  3518. static __init int cpu_has_kvm_support(void)
  3519. {
  3520. return cpu_has_vmx();
  3521. }
  3522. static __init int vmx_disabled_by_bios(void)
  3523. {
  3524. u64 msr;
  3525. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3526. if (msr & FEATURE_CONTROL_LOCKED) {
  3527. /* launched w/ TXT and VMX disabled */
  3528. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3529. && tboot_enabled())
  3530. return 1;
  3531. /* launched w/o TXT and VMX only enabled w/ TXT */
  3532. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3533. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3534. && !tboot_enabled()) {
  3535. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3536. "activate TXT before enabling KVM\n");
  3537. return 1;
  3538. }
  3539. /* launched w/o TXT and VMX disabled */
  3540. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3541. && !tboot_enabled())
  3542. return 1;
  3543. }
  3544. return 0;
  3545. }
  3546. static void kvm_cpu_vmxon(u64 addr)
  3547. {
  3548. cr4_set_bits(X86_CR4_VMXE);
  3549. intel_pt_handle_vmx(1);
  3550. asm volatile (ASM_VMX_VMXON_RAX
  3551. : : "a"(&addr), "m"(addr)
  3552. : "memory", "cc");
  3553. }
  3554. static int hardware_enable(void)
  3555. {
  3556. int cpu = raw_smp_processor_id();
  3557. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3558. u64 old, test_bits;
  3559. if (cr4_read_shadow() & X86_CR4_VMXE)
  3560. return -EBUSY;
  3561. /*
  3562. * This can happen if we hot-added a CPU but failed to allocate
  3563. * VP assist page for it.
  3564. */
  3565. if (static_branch_unlikely(&enable_evmcs) &&
  3566. !hv_get_vp_assist_page(cpu))
  3567. return -EFAULT;
  3568. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3569. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3570. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3571. /*
  3572. * Now we can enable the vmclear operation in kdump
  3573. * since the loaded_vmcss_on_cpu list on this cpu
  3574. * has been initialized.
  3575. *
  3576. * Though the cpu is not in VMX operation now, there
  3577. * is no problem to enable the vmclear operation
  3578. * for the loaded_vmcss_on_cpu list is empty!
  3579. */
  3580. crash_enable_local_vmclear(cpu);
  3581. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3582. test_bits = FEATURE_CONTROL_LOCKED;
  3583. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3584. if (tboot_enabled())
  3585. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3586. if ((old & test_bits) != test_bits) {
  3587. /* enable and lock */
  3588. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3589. }
  3590. kvm_cpu_vmxon(phys_addr);
  3591. if (enable_ept)
  3592. ept_sync_global();
  3593. return 0;
  3594. }
  3595. static void vmclear_local_loaded_vmcss(void)
  3596. {
  3597. int cpu = raw_smp_processor_id();
  3598. struct loaded_vmcs *v, *n;
  3599. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3600. loaded_vmcss_on_cpu_link)
  3601. __loaded_vmcs_clear(v);
  3602. }
  3603. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3604. * tricks.
  3605. */
  3606. static void kvm_cpu_vmxoff(void)
  3607. {
  3608. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  3609. intel_pt_handle_vmx(0);
  3610. cr4_clear_bits(X86_CR4_VMXE);
  3611. }
  3612. static void hardware_disable(void)
  3613. {
  3614. vmclear_local_loaded_vmcss();
  3615. kvm_cpu_vmxoff();
  3616. }
  3617. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3618. u32 msr, u32 *result)
  3619. {
  3620. u32 vmx_msr_low, vmx_msr_high;
  3621. u32 ctl = ctl_min | ctl_opt;
  3622. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3623. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3624. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3625. /* Ensure minimum (required) set of control bits are supported. */
  3626. if (ctl_min & ~ctl)
  3627. return -EIO;
  3628. *result = ctl;
  3629. return 0;
  3630. }
  3631. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3632. {
  3633. u32 vmx_msr_low, vmx_msr_high;
  3634. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3635. return vmx_msr_high & ctl;
  3636. }
  3637. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3638. {
  3639. u32 vmx_msr_low, vmx_msr_high;
  3640. u32 min, opt, min2, opt2;
  3641. u32 _pin_based_exec_control = 0;
  3642. u32 _cpu_based_exec_control = 0;
  3643. u32 _cpu_based_2nd_exec_control = 0;
  3644. u32 _vmexit_control = 0;
  3645. u32 _vmentry_control = 0;
  3646. memset(vmcs_conf, 0, sizeof(*vmcs_conf));
  3647. min = CPU_BASED_HLT_EXITING |
  3648. #ifdef CONFIG_X86_64
  3649. CPU_BASED_CR8_LOAD_EXITING |
  3650. CPU_BASED_CR8_STORE_EXITING |
  3651. #endif
  3652. CPU_BASED_CR3_LOAD_EXITING |
  3653. CPU_BASED_CR3_STORE_EXITING |
  3654. CPU_BASED_UNCOND_IO_EXITING |
  3655. CPU_BASED_MOV_DR_EXITING |
  3656. CPU_BASED_USE_TSC_OFFSETING |
  3657. CPU_BASED_MWAIT_EXITING |
  3658. CPU_BASED_MONITOR_EXITING |
  3659. CPU_BASED_INVLPG_EXITING |
  3660. CPU_BASED_RDPMC_EXITING;
  3661. opt = CPU_BASED_TPR_SHADOW |
  3662. CPU_BASED_USE_MSR_BITMAPS |
  3663. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3664. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3665. &_cpu_based_exec_control) < 0)
  3666. return -EIO;
  3667. #ifdef CONFIG_X86_64
  3668. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3669. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3670. ~CPU_BASED_CR8_STORE_EXITING;
  3671. #endif
  3672. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3673. min2 = 0;
  3674. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3675. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3676. SECONDARY_EXEC_WBINVD_EXITING |
  3677. SECONDARY_EXEC_ENABLE_VPID |
  3678. SECONDARY_EXEC_ENABLE_EPT |
  3679. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3680. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3681. SECONDARY_EXEC_DESC |
  3682. SECONDARY_EXEC_RDTSCP |
  3683. SECONDARY_EXEC_ENABLE_INVPCID |
  3684. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3685. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3686. SECONDARY_EXEC_SHADOW_VMCS |
  3687. SECONDARY_EXEC_XSAVES |
  3688. SECONDARY_EXEC_RDSEED_EXITING |
  3689. SECONDARY_EXEC_RDRAND_EXITING |
  3690. SECONDARY_EXEC_ENABLE_PML |
  3691. SECONDARY_EXEC_TSC_SCALING |
  3692. SECONDARY_EXEC_ENABLE_VMFUNC;
  3693. if (adjust_vmx_controls(min2, opt2,
  3694. MSR_IA32_VMX_PROCBASED_CTLS2,
  3695. &_cpu_based_2nd_exec_control) < 0)
  3696. return -EIO;
  3697. }
  3698. #ifndef CONFIG_X86_64
  3699. if (!(_cpu_based_2nd_exec_control &
  3700. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3701. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3702. #endif
  3703. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3704. _cpu_based_2nd_exec_control &= ~(
  3705. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3706. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3707. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3708. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3709. &vmx_capability.ept, &vmx_capability.vpid);
  3710. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  3711. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  3712. enabled */
  3713. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  3714. CPU_BASED_CR3_STORE_EXITING |
  3715. CPU_BASED_INVLPG_EXITING);
  3716. } else if (vmx_capability.ept) {
  3717. vmx_capability.ept = 0;
  3718. pr_warn_once("EPT CAP should not exist if not support "
  3719. "1-setting enable EPT VM-execution control\n");
  3720. }
  3721. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  3722. vmx_capability.vpid) {
  3723. vmx_capability.vpid = 0;
  3724. pr_warn_once("VPID CAP should not exist if not support "
  3725. "1-setting enable VPID VM-execution control\n");
  3726. }
  3727. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  3728. #ifdef CONFIG_X86_64
  3729. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  3730. #endif
  3731. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  3732. VM_EXIT_CLEAR_BNDCFGS;
  3733. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  3734. &_vmexit_control) < 0)
  3735. return -EIO;
  3736. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  3737. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  3738. PIN_BASED_VMX_PREEMPTION_TIMER;
  3739. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  3740. &_pin_based_exec_control) < 0)
  3741. return -EIO;
  3742. if (cpu_has_broken_vmx_preemption_timer())
  3743. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  3744. if (!(_cpu_based_2nd_exec_control &
  3745. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  3746. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  3747. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3748. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  3749. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  3750. &_vmentry_control) < 0)
  3751. return -EIO;
  3752. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  3753. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  3754. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  3755. return -EIO;
  3756. #ifdef CONFIG_X86_64
  3757. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  3758. if (vmx_msr_high & (1u<<16))
  3759. return -EIO;
  3760. #endif
  3761. /* Require Write-Back (WB) memory type for VMCS accesses. */
  3762. if (((vmx_msr_high >> 18) & 15) != 6)
  3763. return -EIO;
  3764. vmcs_conf->size = vmx_msr_high & 0x1fff;
  3765. vmcs_conf->order = get_order(vmcs_conf->size);
  3766. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  3767. /* KVM supports Enlightened VMCS v1 only */
  3768. if (static_branch_unlikely(&enable_evmcs))
  3769. vmcs_conf->revision_id = KVM_EVMCS_VERSION;
  3770. else
  3771. vmcs_conf->revision_id = vmx_msr_low;
  3772. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  3773. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  3774. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  3775. vmcs_conf->vmexit_ctrl = _vmexit_control;
  3776. vmcs_conf->vmentry_ctrl = _vmentry_control;
  3777. if (static_branch_unlikely(&enable_evmcs))
  3778. evmcs_sanitize_exec_ctrls(vmcs_conf);
  3779. cpu_has_load_ia32_efer =
  3780. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3781. VM_ENTRY_LOAD_IA32_EFER)
  3782. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3783. VM_EXIT_LOAD_IA32_EFER);
  3784. cpu_has_load_perf_global_ctrl =
  3785. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3786. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  3787. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3788. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  3789. /*
  3790. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  3791. * but due to errata below it can't be used. Workaround is to use
  3792. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  3793. *
  3794. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  3795. *
  3796. * AAK155 (model 26)
  3797. * AAP115 (model 30)
  3798. * AAT100 (model 37)
  3799. * BC86,AAY89,BD102 (model 44)
  3800. * BA97 (model 46)
  3801. *
  3802. */
  3803. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  3804. switch (boot_cpu_data.x86_model) {
  3805. case 26:
  3806. case 30:
  3807. case 37:
  3808. case 44:
  3809. case 46:
  3810. cpu_has_load_perf_global_ctrl = false;
  3811. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  3812. "does not work properly. Using workaround\n");
  3813. break;
  3814. default:
  3815. break;
  3816. }
  3817. }
  3818. if (boot_cpu_has(X86_FEATURE_XSAVES))
  3819. rdmsrl(MSR_IA32_XSS, host_xss);
  3820. return 0;
  3821. }
  3822. static struct vmcs *alloc_vmcs_cpu(int cpu)
  3823. {
  3824. int node = cpu_to_node(cpu);
  3825. struct page *pages;
  3826. struct vmcs *vmcs;
  3827. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  3828. if (!pages)
  3829. return NULL;
  3830. vmcs = page_address(pages);
  3831. memset(vmcs, 0, vmcs_config.size);
  3832. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  3833. return vmcs;
  3834. }
  3835. static void free_vmcs(struct vmcs *vmcs)
  3836. {
  3837. free_pages((unsigned long)vmcs, vmcs_config.order);
  3838. }
  3839. /*
  3840. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  3841. */
  3842. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3843. {
  3844. if (!loaded_vmcs->vmcs)
  3845. return;
  3846. loaded_vmcs_clear(loaded_vmcs);
  3847. free_vmcs(loaded_vmcs->vmcs);
  3848. loaded_vmcs->vmcs = NULL;
  3849. if (loaded_vmcs->msr_bitmap)
  3850. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  3851. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  3852. }
  3853. static struct vmcs *alloc_vmcs(void)
  3854. {
  3855. return alloc_vmcs_cpu(raw_smp_processor_id());
  3856. }
  3857. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3858. {
  3859. loaded_vmcs->vmcs = alloc_vmcs();
  3860. if (!loaded_vmcs->vmcs)
  3861. return -ENOMEM;
  3862. loaded_vmcs->shadow_vmcs = NULL;
  3863. loaded_vmcs_init(loaded_vmcs);
  3864. if (cpu_has_vmx_msr_bitmap()) {
  3865. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  3866. if (!loaded_vmcs->msr_bitmap)
  3867. goto out_vmcs;
  3868. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  3869. if (IS_ENABLED(CONFIG_HYPERV) &&
  3870. static_branch_unlikely(&enable_evmcs) &&
  3871. (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
  3872. struct hv_enlightened_vmcs *evmcs =
  3873. (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
  3874. evmcs->hv_enlightenments_control.msr_bitmap = 1;
  3875. }
  3876. }
  3877. return 0;
  3878. out_vmcs:
  3879. free_loaded_vmcs(loaded_vmcs);
  3880. return -ENOMEM;
  3881. }
  3882. static void free_kvm_area(void)
  3883. {
  3884. int cpu;
  3885. for_each_possible_cpu(cpu) {
  3886. free_vmcs(per_cpu(vmxarea, cpu));
  3887. per_cpu(vmxarea, cpu) = NULL;
  3888. }
  3889. }
  3890. enum vmcs_field_width {
  3891. VMCS_FIELD_WIDTH_U16 = 0,
  3892. VMCS_FIELD_WIDTH_U64 = 1,
  3893. VMCS_FIELD_WIDTH_U32 = 2,
  3894. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  3895. };
  3896. static inline int vmcs_field_width(unsigned long field)
  3897. {
  3898. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  3899. return VMCS_FIELD_WIDTH_U32;
  3900. return (field >> 13) & 0x3 ;
  3901. }
  3902. static inline int vmcs_field_readonly(unsigned long field)
  3903. {
  3904. return (((field >> 10) & 0x3) == 1);
  3905. }
  3906. static void init_vmcs_shadow_fields(void)
  3907. {
  3908. int i, j;
  3909. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  3910. u16 field = shadow_read_only_fields[i];
  3911. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3912. (i + 1 == max_shadow_read_only_fields ||
  3913. shadow_read_only_fields[i + 1] != field + 1))
  3914. pr_err("Missing field from shadow_read_only_field %x\n",
  3915. field + 1);
  3916. clear_bit(field, vmx_vmread_bitmap);
  3917. #ifdef CONFIG_X86_64
  3918. if (field & 1)
  3919. continue;
  3920. #endif
  3921. if (j < i)
  3922. shadow_read_only_fields[j] = field;
  3923. j++;
  3924. }
  3925. max_shadow_read_only_fields = j;
  3926. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  3927. u16 field = shadow_read_write_fields[i];
  3928. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3929. (i + 1 == max_shadow_read_write_fields ||
  3930. shadow_read_write_fields[i + 1] != field + 1))
  3931. pr_err("Missing field from shadow_read_write_field %x\n",
  3932. field + 1);
  3933. /*
  3934. * PML and the preemption timer can be emulated, but the
  3935. * processor cannot vmwrite to fields that don't exist
  3936. * on bare metal.
  3937. */
  3938. switch (field) {
  3939. case GUEST_PML_INDEX:
  3940. if (!cpu_has_vmx_pml())
  3941. continue;
  3942. break;
  3943. case VMX_PREEMPTION_TIMER_VALUE:
  3944. if (!cpu_has_vmx_preemption_timer())
  3945. continue;
  3946. break;
  3947. case GUEST_INTR_STATUS:
  3948. if (!cpu_has_vmx_apicv())
  3949. continue;
  3950. break;
  3951. default:
  3952. break;
  3953. }
  3954. clear_bit(field, vmx_vmwrite_bitmap);
  3955. clear_bit(field, vmx_vmread_bitmap);
  3956. #ifdef CONFIG_X86_64
  3957. if (field & 1)
  3958. continue;
  3959. #endif
  3960. if (j < i)
  3961. shadow_read_write_fields[j] = field;
  3962. j++;
  3963. }
  3964. max_shadow_read_write_fields = j;
  3965. }
  3966. static __init int alloc_kvm_area(void)
  3967. {
  3968. int cpu;
  3969. for_each_possible_cpu(cpu) {
  3970. struct vmcs *vmcs;
  3971. vmcs = alloc_vmcs_cpu(cpu);
  3972. if (!vmcs) {
  3973. free_kvm_area();
  3974. return -ENOMEM;
  3975. }
  3976. per_cpu(vmxarea, cpu) = vmcs;
  3977. }
  3978. return 0;
  3979. }
  3980. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  3981. struct kvm_segment *save)
  3982. {
  3983. if (!emulate_invalid_guest_state) {
  3984. /*
  3985. * CS and SS RPL should be equal during guest entry according
  3986. * to VMX spec, but in reality it is not always so. Since vcpu
  3987. * is in the middle of the transition from real mode to
  3988. * protected mode it is safe to assume that RPL 0 is a good
  3989. * default value.
  3990. */
  3991. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  3992. save->selector &= ~SEGMENT_RPL_MASK;
  3993. save->dpl = save->selector & SEGMENT_RPL_MASK;
  3994. save->s = 1;
  3995. }
  3996. vmx_set_segment(vcpu, save, seg);
  3997. }
  3998. static void enter_pmode(struct kvm_vcpu *vcpu)
  3999. {
  4000. unsigned long flags;
  4001. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4002. /*
  4003. * Update real mode segment cache. It may be not up-to-date if sement
  4004. * register was written while vcpu was in a guest mode.
  4005. */
  4006. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4007. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4008. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4009. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4010. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4011. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4012. vmx->rmode.vm86_active = 0;
  4013. vmx_segment_cache_clear(vmx);
  4014. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4015. flags = vmcs_readl(GUEST_RFLAGS);
  4016. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  4017. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  4018. vmcs_writel(GUEST_RFLAGS, flags);
  4019. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  4020. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  4021. update_exception_bitmap(vcpu);
  4022. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4023. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4024. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4025. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4026. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4027. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4028. }
  4029. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  4030. {
  4031. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4032. struct kvm_segment var = *save;
  4033. var.dpl = 0x3;
  4034. if (seg == VCPU_SREG_CS)
  4035. var.type = 0x3;
  4036. if (!emulate_invalid_guest_state) {
  4037. var.selector = var.base >> 4;
  4038. var.base = var.base & 0xffff0;
  4039. var.limit = 0xffff;
  4040. var.g = 0;
  4041. var.db = 0;
  4042. var.present = 1;
  4043. var.s = 1;
  4044. var.l = 0;
  4045. var.unusable = 0;
  4046. var.type = 0x3;
  4047. var.avl = 0;
  4048. if (save->base & 0xf)
  4049. printk_once(KERN_WARNING "kvm: segment base is not "
  4050. "paragraph aligned when entering "
  4051. "protected mode (seg=%d)", seg);
  4052. }
  4053. vmcs_write16(sf->selector, var.selector);
  4054. vmcs_writel(sf->base, var.base);
  4055. vmcs_write32(sf->limit, var.limit);
  4056. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  4057. }
  4058. static void enter_rmode(struct kvm_vcpu *vcpu)
  4059. {
  4060. unsigned long flags;
  4061. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4062. struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
  4063. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4064. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4065. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4066. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4067. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4068. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4069. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4070. vmx->rmode.vm86_active = 1;
  4071. /*
  4072. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  4073. * vcpu. Warn the user that an update is overdue.
  4074. */
  4075. if (!kvm_vmx->tss_addr)
  4076. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  4077. "called before entering vcpu\n");
  4078. vmx_segment_cache_clear(vmx);
  4079. vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
  4080. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  4081. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4082. flags = vmcs_readl(GUEST_RFLAGS);
  4083. vmx->rmode.save_rflags = flags;
  4084. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  4085. vmcs_writel(GUEST_RFLAGS, flags);
  4086. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  4087. update_exception_bitmap(vcpu);
  4088. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4089. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4090. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4091. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4092. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4093. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4094. kvm_mmu_reset_context(vcpu);
  4095. }
  4096. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  4097. {
  4098. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4099. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  4100. if (!msr)
  4101. return;
  4102. /*
  4103. * Force kernel_gs_base reloading before EFER changes, as control
  4104. * of this msr depends on is_long_mode().
  4105. */
  4106. vmx_load_host_state(to_vmx(vcpu));
  4107. vcpu->arch.efer = efer;
  4108. if (efer & EFER_LMA) {
  4109. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4110. msr->data = efer;
  4111. } else {
  4112. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4113. msr->data = efer & ~EFER_LME;
  4114. }
  4115. setup_msrs(vmx);
  4116. }
  4117. #ifdef CONFIG_X86_64
  4118. static void enter_lmode(struct kvm_vcpu *vcpu)
  4119. {
  4120. u32 guest_tr_ar;
  4121. vmx_segment_cache_clear(to_vmx(vcpu));
  4122. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  4123. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  4124. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  4125. __func__);
  4126. vmcs_write32(GUEST_TR_AR_BYTES,
  4127. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  4128. | VMX_AR_TYPE_BUSY_64_TSS);
  4129. }
  4130. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  4131. }
  4132. static void exit_lmode(struct kvm_vcpu *vcpu)
  4133. {
  4134. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4135. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  4136. }
  4137. #endif
  4138. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  4139. bool invalidate_gpa)
  4140. {
  4141. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  4142. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  4143. return;
  4144. ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
  4145. } else {
  4146. vpid_sync_context(vpid);
  4147. }
  4148. }
  4149. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4150. {
  4151. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  4152. }
  4153. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  4154. {
  4155. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  4156. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  4157. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  4158. }
  4159. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  4160. {
  4161. if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
  4162. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  4163. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4164. }
  4165. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  4166. {
  4167. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  4168. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  4169. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  4170. }
  4171. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  4172. {
  4173. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4174. if (!test_bit(VCPU_EXREG_PDPTR,
  4175. (unsigned long *)&vcpu->arch.regs_dirty))
  4176. return;
  4177. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4178. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  4179. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  4180. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  4181. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  4182. }
  4183. }
  4184. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  4185. {
  4186. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4187. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4188. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  4189. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  4190. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  4191. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  4192. }
  4193. __set_bit(VCPU_EXREG_PDPTR,
  4194. (unsigned long *)&vcpu->arch.regs_avail);
  4195. __set_bit(VCPU_EXREG_PDPTR,
  4196. (unsigned long *)&vcpu->arch.regs_dirty);
  4197. }
  4198. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4199. {
  4200. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4201. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4202. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4203. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  4204. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4205. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4206. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  4207. return fixed_bits_valid(val, fixed0, fixed1);
  4208. }
  4209. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4210. {
  4211. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4212. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4213. return fixed_bits_valid(val, fixed0, fixed1);
  4214. }
  4215. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4216. {
  4217. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  4218. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  4219. return fixed_bits_valid(val, fixed0, fixed1);
  4220. }
  4221. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  4222. #define nested_guest_cr4_valid nested_cr4_valid
  4223. #define nested_host_cr4_valid nested_cr4_valid
  4224. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  4225. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  4226. unsigned long cr0,
  4227. struct kvm_vcpu *vcpu)
  4228. {
  4229. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  4230. vmx_decache_cr3(vcpu);
  4231. if (!(cr0 & X86_CR0_PG)) {
  4232. /* From paging/starting to nonpaging */
  4233. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4234. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  4235. (CPU_BASED_CR3_LOAD_EXITING |
  4236. CPU_BASED_CR3_STORE_EXITING));
  4237. vcpu->arch.cr0 = cr0;
  4238. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4239. } else if (!is_paging(vcpu)) {
  4240. /* From nonpaging to paging */
  4241. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4242. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  4243. ~(CPU_BASED_CR3_LOAD_EXITING |
  4244. CPU_BASED_CR3_STORE_EXITING));
  4245. vcpu->arch.cr0 = cr0;
  4246. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4247. }
  4248. if (!(cr0 & X86_CR0_WP))
  4249. *hw_cr0 &= ~X86_CR0_WP;
  4250. }
  4251. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  4252. {
  4253. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4254. unsigned long hw_cr0;
  4255. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  4256. if (enable_unrestricted_guest)
  4257. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  4258. else {
  4259. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  4260. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  4261. enter_pmode(vcpu);
  4262. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  4263. enter_rmode(vcpu);
  4264. }
  4265. #ifdef CONFIG_X86_64
  4266. if (vcpu->arch.efer & EFER_LME) {
  4267. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  4268. enter_lmode(vcpu);
  4269. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  4270. exit_lmode(vcpu);
  4271. }
  4272. #endif
  4273. if (enable_ept && !enable_unrestricted_guest)
  4274. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  4275. vmcs_writel(CR0_READ_SHADOW, cr0);
  4276. vmcs_writel(GUEST_CR0, hw_cr0);
  4277. vcpu->arch.cr0 = cr0;
  4278. /* depends on vcpu->arch.cr0 to be set to a new value */
  4279. vmx->emulation_required = emulation_required(vcpu);
  4280. }
  4281. static int get_ept_level(struct kvm_vcpu *vcpu)
  4282. {
  4283. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  4284. return 5;
  4285. return 4;
  4286. }
  4287. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  4288. {
  4289. u64 eptp = VMX_EPTP_MT_WB;
  4290. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  4291. if (enable_ept_ad_bits &&
  4292. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  4293. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  4294. eptp |= (root_hpa & PAGE_MASK);
  4295. return eptp;
  4296. }
  4297. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  4298. {
  4299. unsigned long guest_cr3;
  4300. u64 eptp;
  4301. guest_cr3 = cr3;
  4302. if (enable_ept) {
  4303. eptp = construct_eptp(vcpu, cr3);
  4304. vmcs_write64(EPT_POINTER, eptp);
  4305. if (enable_unrestricted_guest || is_paging(vcpu) ||
  4306. is_guest_mode(vcpu))
  4307. guest_cr3 = kvm_read_cr3(vcpu);
  4308. else
  4309. guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
  4310. ept_load_pdptrs(vcpu);
  4311. }
  4312. vmx_flush_tlb(vcpu, true);
  4313. vmcs_writel(GUEST_CR3, guest_cr3);
  4314. }
  4315. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  4316. {
  4317. /*
  4318. * Pass through host's Machine Check Enable value to hw_cr4, which
  4319. * is in force while we are in guest mode. Do not let guests control
  4320. * this bit, even if host CR4.MCE == 0.
  4321. */
  4322. unsigned long hw_cr4;
  4323. hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
  4324. if (enable_unrestricted_guest)
  4325. hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
  4326. else if (to_vmx(vcpu)->rmode.vm86_active)
  4327. hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
  4328. else
  4329. hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
  4330. if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
  4331. if (cr4 & X86_CR4_UMIP) {
  4332. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4333. SECONDARY_EXEC_DESC);
  4334. hw_cr4 &= ~X86_CR4_UMIP;
  4335. } else if (!is_guest_mode(vcpu) ||
  4336. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  4337. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4338. SECONDARY_EXEC_DESC);
  4339. }
  4340. if (cr4 & X86_CR4_VMXE) {
  4341. /*
  4342. * To use VMXON (and later other VMX instructions), a guest
  4343. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  4344. * So basically the check on whether to allow nested VMX
  4345. * is here.
  4346. */
  4347. if (!nested_vmx_allowed(vcpu))
  4348. return 1;
  4349. }
  4350. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  4351. return 1;
  4352. vcpu->arch.cr4 = cr4;
  4353. if (!enable_unrestricted_guest) {
  4354. if (enable_ept) {
  4355. if (!is_paging(vcpu)) {
  4356. hw_cr4 &= ~X86_CR4_PAE;
  4357. hw_cr4 |= X86_CR4_PSE;
  4358. } else if (!(cr4 & X86_CR4_PAE)) {
  4359. hw_cr4 &= ~X86_CR4_PAE;
  4360. }
  4361. }
  4362. /*
  4363. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  4364. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  4365. * to be manually disabled when guest switches to non-paging
  4366. * mode.
  4367. *
  4368. * If !enable_unrestricted_guest, the CPU is always running
  4369. * with CR0.PG=1 and CR4 needs to be modified.
  4370. * If enable_unrestricted_guest, the CPU automatically
  4371. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  4372. */
  4373. if (!is_paging(vcpu))
  4374. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  4375. }
  4376. vmcs_writel(CR4_READ_SHADOW, cr4);
  4377. vmcs_writel(GUEST_CR4, hw_cr4);
  4378. return 0;
  4379. }
  4380. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  4381. struct kvm_segment *var, int seg)
  4382. {
  4383. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4384. u32 ar;
  4385. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4386. *var = vmx->rmode.segs[seg];
  4387. if (seg == VCPU_SREG_TR
  4388. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  4389. return;
  4390. var->base = vmx_read_guest_seg_base(vmx, seg);
  4391. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4392. return;
  4393. }
  4394. var->base = vmx_read_guest_seg_base(vmx, seg);
  4395. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  4396. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4397. ar = vmx_read_guest_seg_ar(vmx, seg);
  4398. var->unusable = (ar >> 16) & 1;
  4399. var->type = ar & 15;
  4400. var->s = (ar >> 4) & 1;
  4401. var->dpl = (ar >> 5) & 3;
  4402. /*
  4403. * Some userspaces do not preserve unusable property. Since usable
  4404. * segment has to be present according to VMX spec we can use present
  4405. * property to amend userspace bug by making unusable segment always
  4406. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  4407. * segment as unusable.
  4408. */
  4409. var->present = !var->unusable;
  4410. var->avl = (ar >> 12) & 1;
  4411. var->l = (ar >> 13) & 1;
  4412. var->db = (ar >> 14) & 1;
  4413. var->g = (ar >> 15) & 1;
  4414. }
  4415. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4416. {
  4417. struct kvm_segment s;
  4418. if (to_vmx(vcpu)->rmode.vm86_active) {
  4419. vmx_get_segment(vcpu, &s, seg);
  4420. return s.base;
  4421. }
  4422. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  4423. }
  4424. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  4425. {
  4426. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4427. if (unlikely(vmx->rmode.vm86_active))
  4428. return 0;
  4429. else {
  4430. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  4431. return VMX_AR_DPL(ar);
  4432. }
  4433. }
  4434. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  4435. {
  4436. u32 ar;
  4437. if (var->unusable || !var->present)
  4438. ar = 1 << 16;
  4439. else {
  4440. ar = var->type & 15;
  4441. ar |= (var->s & 1) << 4;
  4442. ar |= (var->dpl & 3) << 5;
  4443. ar |= (var->present & 1) << 7;
  4444. ar |= (var->avl & 1) << 12;
  4445. ar |= (var->l & 1) << 13;
  4446. ar |= (var->db & 1) << 14;
  4447. ar |= (var->g & 1) << 15;
  4448. }
  4449. return ar;
  4450. }
  4451. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4452. struct kvm_segment *var, int seg)
  4453. {
  4454. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4455. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4456. vmx_segment_cache_clear(vmx);
  4457. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4458. vmx->rmode.segs[seg] = *var;
  4459. if (seg == VCPU_SREG_TR)
  4460. vmcs_write16(sf->selector, var->selector);
  4461. else if (var->s)
  4462. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4463. goto out;
  4464. }
  4465. vmcs_writel(sf->base, var->base);
  4466. vmcs_write32(sf->limit, var->limit);
  4467. vmcs_write16(sf->selector, var->selector);
  4468. /*
  4469. * Fix the "Accessed" bit in AR field of segment registers for older
  4470. * qemu binaries.
  4471. * IA32 arch specifies that at the time of processor reset the
  4472. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4473. * is setting it to 0 in the userland code. This causes invalid guest
  4474. * state vmexit when "unrestricted guest" mode is turned on.
  4475. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4476. * tree. Newer qemu binaries with that qemu fix would not need this
  4477. * kvm hack.
  4478. */
  4479. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4480. var->type |= 0x1; /* Accessed */
  4481. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4482. out:
  4483. vmx->emulation_required = emulation_required(vcpu);
  4484. }
  4485. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4486. {
  4487. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4488. *db = (ar >> 14) & 1;
  4489. *l = (ar >> 13) & 1;
  4490. }
  4491. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4492. {
  4493. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4494. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4495. }
  4496. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4497. {
  4498. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4499. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4500. }
  4501. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4502. {
  4503. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4504. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4505. }
  4506. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4507. {
  4508. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4509. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4510. }
  4511. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4512. {
  4513. struct kvm_segment var;
  4514. u32 ar;
  4515. vmx_get_segment(vcpu, &var, seg);
  4516. var.dpl = 0x3;
  4517. if (seg == VCPU_SREG_CS)
  4518. var.type = 0x3;
  4519. ar = vmx_segment_access_rights(&var);
  4520. if (var.base != (var.selector << 4))
  4521. return false;
  4522. if (var.limit != 0xffff)
  4523. return false;
  4524. if (ar != 0xf3)
  4525. return false;
  4526. return true;
  4527. }
  4528. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4529. {
  4530. struct kvm_segment cs;
  4531. unsigned int cs_rpl;
  4532. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4533. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4534. if (cs.unusable)
  4535. return false;
  4536. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4537. return false;
  4538. if (!cs.s)
  4539. return false;
  4540. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4541. if (cs.dpl > cs_rpl)
  4542. return false;
  4543. } else {
  4544. if (cs.dpl != cs_rpl)
  4545. return false;
  4546. }
  4547. if (!cs.present)
  4548. return false;
  4549. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4550. return true;
  4551. }
  4552. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4553. {
  4554. struct kvm_segment ss;
  4555. unsigned int ss_rpl;
  4556. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4557. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4558. if (ss.unusable)
  4559. return true;
  4560. if (ss.type != 3 && ss.type != 7)
  4561. return false;
  4562. if (!ss.s)
  4563. return false;
  4564. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4565. return false;
  4566. if (!ss.present)
  4567. return false;
  4568. return true;
  4569. }
  4570. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4571. {
  4572. struct kvm_segment var;
  4573. unsigned int rpl;
  4574. vmx_get_segment(vcpu, &var, seg);
  4575. rpl = var.selector & SEGMENT_RPL_MASK;
  4576. if (var.unusable)
  4577. return true;
  4578. if (!var.s)
  4579. return false;
  4580. if (!var.present)
  4581. return false;
  4582. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4583. if (var.dpl < rpl) /* DPL < RPL */
  4584. return false;
  4585. }
  4586. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4587. * rights flags
  4588. */
  4589. return true;
  4590. }
  4591. static bool tr_valid(struct kvm_vcpu *vcpu)
  4592. {
  4593. struct kvm_segment tr;
  4594. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4595. if (tr.unusable)
  4596. return false;
  4597. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4598. return false;
  4599. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4600. return false;
  4601. if (!tr.present)
  4602. return false;
  4603. return true;
  4604. }
  4605. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4606. {
  4607. struct kvm_segment ldtr;
  4608. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4609. if (ldtr.unusable)
  4610. return true;
  4611. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4612. return false;
  4613. if (ldtr.type != 2)
  4614. return false;
  4615. if (!ldtr.present)
  4616. return false;
  4617. return true;
  4618. }
  4619. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4620. {
  4621. struct kvm_segment cs, ss;
  4622. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4623. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4624. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4625. (ss.selector & SEGMENT_RPL_MASK));
  4626. }
  4627. /*
  4628. * Check if guest state is valid. Returns true if valid, false if
  4629. * not.
  4630. * We assume that registers are always usable
  4631. */
  4632. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4633. {
  4634. if (enable_unrestricted_guest)
  4635. return true;
  4636. /* real mode guest state checks */
  4637. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4638. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4639. return false;
  4640. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4641. return false;
  4642. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4643. return false;
  4644. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4645. return false;
  4646. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4647. return false;
  4648. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4649. return false;
  4650. } else {
  4651. /* protected mode guest state checks */
  4652. if (!cs_ss_rpl_check(vcpu))
  4653. return false;
  4654. if (!code_segment_valid(vcpu))
  4655. return false;
  4656. if (!stack_segment_valid(vcpu))
  4657. return false;
  4658. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4659. return false;
  4660. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4661. return false;
  4662. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4663. return false;
  4664. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4665. return false;
  4666. if (!tr_valid(vcpu))
  4667. return false;
  4668. if (!ldtr_valid(vcpu))
  4669. return false;
  4670. }
  4671. /* TODO:
  4672. * - Add checks on RIP
  4673. * - Add checks on RFLAGS
  4674. */
  4675. return true;
  4676. }
  4677. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4678. {
  4679. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4680. }
  4681. static int init_rmode_tss(struct kvm *kvm)
  4682. {
  4683. gfn_t fn;
  4684. u16 data = 0;
  4685. int idx, r;
  4686. idx = srcu_read_lock(&kvm->srcu);
  4687. fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
  4688. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4689. if (r < 0)
  4690. goto out;
  4691. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  4692. r = kvm_write_guest_page(kvm, fn++, &data,
  4693. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  4694. if (r < 0)
  4695. goto out;
  4696. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  4697. if (r < 0)
  4698. goto out;
  4699. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4700. if (r < 0)
  4701. goto out;
  4702. data = ~0;
  4703. r = kvm_write_guest_page(kvm, fn, &data,
  4704. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  4705. sizeof(u8));
  4706. out:
  4707. srcu_read_unlock(&kvm->srcu, idx);
  4708. return r;
  4709. }
  4710. static int init_rmode_identity_map(struct kvm *kvm)
  4711. {
  4712. struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
  4713. int i, idx, r = 0;
  4714. kvm_pfn_t identity_map_pfn;
  4715. u32 tmp;
  4716. /* Protect kvm_vmx->ept_identity_pagetable_done. */
  4717. mutex_lock(&kvm->slots_lock);
  4718. if (likely(kvm_vmx->ept_identity_pagetable_done))
  4719. goto out2;
  4720. if (!kvm_vmx->ept_identity_map_addr)
  4721. kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  4722. identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
  4723. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  4724. kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
  4725. if (r < 0)
  4726. goto out2;
  4727. idx = srcu_read_lock(&kvm->srcu);
  4728. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  4729. if (r < 0)
  4730. goto out;
  4731. /* Set up identity-mapping pagetable for EPT in real mode */
  4732. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  4733. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  4734. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  4735. r = kvm_write_guest_page(kvm, identity_map_pfn,
  4736. &tmp, i * sizeof(tmp), sizeof(tmp));
  4737. if (r < 0)
  4738. goto out;
  4739. }
  4740. kvm_vmx->ept_identity_pagetable_done = true;
  4741. out:
  4742. srcu_read_unlock(&kvm->srcu, idx);
  4743. out2:
  4744. mutex_unlock(&kvm->slots_lock);
  4745. return r;
  4746. }
  4747. static void seg_setup(int seg)
  4748. {
  4749. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4750. unsigned int ar;
  4751. vmcs_write16(sf->selector, 0);
  4752. vmcs_writel(sf->base, 0);
  4753. vmcs_write32(sf->limit, 0xffff);
  4754. ar = 0x93;
  4755. if (seg == VCPU_SREG_CS)
  4756. ar |= 0x08; /* code segment */
  4757. vmcs_write32(sf->ar_bytes, ar);
  4758. }
  4759. static int alloc_apic_access_page(struct kvm *kvm)
  4760. {
  4761. struct page *page;
  4762. int r = 0;
  4763. mutex_lock(&kvm->slots_lock);
  4764. if (kvm->arch.apic_access_page_done)
  4765. goto out;
  4766. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  4767. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  4768. if (r)
  4769. goto out;
  4770. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  4771. if (is_error_page(page)) {
  4772. r = -EFAULT;
  4773. goto out;
  4774. }
  4775. /*
  4776. * Do not pin the page in memory, so that memory hot-unplug
  4777. * is able to migrate it.
  4778. */
  4779. put_page(page);
  4780. kvm->arch.apic_access_page_done = true;
  4781. out:
  4782. mutex_unlock(&kvm->slots_lock);
  4783. return r;
  4784. }
  4785. static int allocate_vpid(void)
  4786. {
  4787. int vpid;
  4788. if (!enable_vpid)
  4789. return 0;
  4790. spin_lock(&vmx_vpid_lock);
  4791. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  4792. if (vpid < VMX_NR_VPIDS)
  4793. __set_bit(vpid, vmx_vpid_bitmap);
  4794. else
  4795. vpid = 0;
  4796. spin_unlock(&vmx_vpid_lock);
  4797. return vpid;
  4798. }
  4799. static void free_vpid(int vpid)
  4800. {
  4801. if (!enable_vpid || vpid == 0)
  4802. return;
  4803. spin_lock(&vmx_vpid_lock);
  4804. __clear_bit(vpid, vmx_vpid_bitmap);
  4805. spin_unlock(&vmx_vpid_lock);
  4806. }
  4807. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  4808. u32 msr, int type)
  4809. {
  4810. int f = sizeof(unsigned long);
  4811. if (!cpu_has_vmx_msr_bitmap())
  4812. return;
  4813. if (static_branch_unlikely(&enable_evmcs))
  4814. evmcs_touch_msr_bitmap();
  4815. /*
  4816. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4817. * have the write-low and read-high bitmap offsets the wrong way round.
  4818. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4819. */
  4820. if (msr <= 0x1fff) {
  4821. if (type & MSR_TYPE_R)
  4822. /* read-low */
  4823. __clear_bit(msr, msr_bitmap + 0x000 / f);
  4824. if (type & MSR_TYPE_W)
  4825. /* write-low */
  4826. __clear_bit(msr, msr_bitmap + 0x800 / f);
  4827. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4828. msr &= 0x1fff;
  4829. if (type & MSR_TYPE_R)
  4830. /* read-high */
  4831. __clear_bit(msr, msr_bitmap + 0x400 / f);
  4832. if (type & MSR_TYPE_W)
  4833. /* write-high */
  4834. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  4835. }
  4836. }
  4837. static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  4838. u32 msr, int type)
  4839. {
  4840. int f = sizeof(unsigned long);
  4841. if (!cpu_has_vmx_msr_bitmap())
  4842. return;
  4843. if (static_branch_unlikely(&enable_evmcs))
  4844. evmcs_touch_msr_bitmap();
  4845. /*
  4846. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4847. * have the write-low and read-high bitmap offsets the wrong way round.
  4848. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4849. */
  4850. if (msr <= 0x1fff) {
  4851. if (type & MSR_TYPE_R)
  4852. /* read-low */
  4853. __set_bit(msr, msr_bitmap + 0x000 / f);
  4854. if (type & MSR_TYPE_W)
  4855. /* write-low */
  4856. __set_bit(msr, msr_bitmap + 0x800 / f);
  4857. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4858. msr &= 0x1fff;
  4859. if (type & MSR_TYPE_R)
  4860. /* read-high */
  4861. __set_bit(msr, msr_bitmap + 0x400 / f);
  4862. if (type & MSR_TYPE_W)
  4863. /* write-high */
  4864. __set_bit(msr, msr_bitmap + 0xc00 / f);
  4865. }
  4866. }
  4867. static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  4868. u32 msr, int type, bool value)
  4869. {
  4870. if (value)
  4871. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  4872. else
  4873. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  4874. }
  4875. /*
  4876. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  4877. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  4878. */
  4879. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  4880. unsigned long *msr_bitmap_nested,
  4881. u32 msr, int type)
  4882. {
  4883. int f = sizeof(unsigned long);
  4884. /*
  4885. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4886. * have the write-low and read-high bitmap offsets the wrong way round.
  4887. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4888. */
  4889. if (msr <= 0x1fff) {
  4890. if (type & MSR_TYPE_R &&
  4891. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  4892. /* read-low */
  4893. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  4894. if (type & MSR_TYPE_W &&
  4895. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  4896. /* write-low */
  4897. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  4898. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4899. msr &= 0x1fff;
  4900. if (type & MSR_TYPE_R &&
  4901. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  4902. /* read-high */
  4903. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  4904. if (type & MSR_TYPE_W &&
  4905. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  4906. /* write-high */
  4907. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  4908. }
  4909. }
  4910. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  4911. {
  4912. u8 mode = 0;
  4913. if (cpu_has_secondary_exec_ctrls() &&
  4914. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  4915. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  4916. mode |= MSR_BITMAP_MODE_X2APIC;
  4917. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  4918. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  4919. }
  4920. if (is_long_mode(vcpu))
  4921. mode |= MSR_BITMAP_MODE_LM;
  4922. return mode;
  4923. }
  4924. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  4925. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  4926. u8 mode)
  4927. {
  4928. int msr;
  4929. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  4930. unsigned word = msr / BITS_PER_LONG;
  4931. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  4932. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  4933. }
  4934. if (mode & MSR_BITMAP_MODE_X2APIC) {
  4935. /*
  4936. * TPR reads and writes can be virtualized even if virtual interrupt
  4937. * delivery is not in use.
  4938. */
  4939. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  4940. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  4941. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  4942. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  4943. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  4944. }
  4945. }
  4946. }
  4947. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  4948. {
  4949. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4950. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  4951. u8 mode = vmx_msr_bitmap_mode(vcpu);
  4952. u8 changed = mode ^ vmx->msr_bitmap_mode;
  4953. if (!changed)
  4954. return;
  4955. vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
  4956. !(mode & MSR_BITMAP_MODE_LM));
  4957. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  4958. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  4959. vmx->msr_bitmap_mode = mode;
  4960. }
  4961. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  4962. {
  4963. return enable_apicv;
  4964. }
  4965. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  4966. {
  4967. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4968. gfn_t gfn;
  4969. /*
  4970. * Don't need to mark the APIC access page dirty; it is never
  4971. * written to by the CPU during APIC virtualization.
  4972. */
  4973. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  4974. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  4975. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  4976. }
  4977. if (nested_cpu_has_posted_intr(vmcs12)) {
  4978. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  4979. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  4980. }
  4981. }
  4982. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  4983. {
  4984. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4985. int max_irr;
  4986. void *vapic_page;
  4987. u16 status;
  4988. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  4989. return;
  4990. vmx->nested.pi_pending = false;
  4991. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  4992. return;
  4993. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  4994. if (max_irr != 256) {
  4995. vapic_page = kmap(vmx->nested.virtual_apic_page);
  4996. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  4997. vapic_page, &max_irr);
  4998. kunmap(vmx->nested.virtual_apic_page);
  4999. status = vmcs_read16(GUEST_INTR_STATUS);
  5000. if ((u8)max_irr > ((u8)status & 0xff)) {
  5001. status &= ~0xff;
  5002. status |= (u8)max_irr;
  5003. vmcs_write16(GUEST_INTR_STATUS, status);
  5004. }
  5005. }
  5006. nested_mark_vmcs12_pages_dirty(vcpu);
  5007. }
  5008. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  5009. bool nested)
  5010. {
  5011. #ifdef CONFIG_SMP
  5012. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  5013. if (vcpu->mode == IN_GUEST_MODE) {
  5014. /*
  5015. * The vector of interrupt to be delivered to vcpu had
  5016. * been set in PIR before this function.
  5017. *
  5018. * Following cases will be reached in this block, and
  5019. * we always send a notification event in all cases as
  5020. * explained below.
  5021. *
  5022. * Case 1: vcpu keeps in non-root mode. Sending a
  5023. * notification event posts the interrupt to vcpu.
  5024. *
  5025. * Case 2: vcpu exits to root mode and is still
  5026. * runnable. PIR will be synced to vIRR before the
  5027. * next vcpu entry. Sending a notification event in
  5028. * this case has no effect, as vcpu is not in root
  5029. * mode.
  5030. *
  5031. * Case 3: vcpu exits to root mode and is blocked.
  5032. * vcpu_block() has already synced PIR to vIRR and
  5033. * never blocks vcpu if vIRR is not cleared. Therefore,
  5034. * a blocked vcpu here does not wait for any requested
  5035. * interrupts in PIR, and sending a notification event
  5036. * which has no effect is safe here.
  5037. */
  5038. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  5039. return true;
  5040. }
  5041. #endif
  5042. return false;
  5043. }
  5044. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  5045. int vector)
  5046. {
  5047. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5048. if (is_guest_mode(vcpu) &&
  5049. vector == vmx->nested.posted_intr_nv) {
  5050. /*
  5051. * If a posted intr is not recognized by hardware,
  5052. * we will accomplish it in the next vmentry.
  5053. */
  5054. vmx->nested.pi_pending = true;
  5055. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5056. /* the PIR and ON have been set by L1. */
  5057. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  5058. kvm_vcpu_kick(vcpu);
  5059. return 0;
  5060. }
  5061. return -1;
  5062. }
  5063. /*
  5064. * Send interrupt to vcpu via posted interrupt way.
  5065. * 1. If target vcpu is running(non-root mode), send posted interrupt
  5066. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  5067. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  5068. * interrupt from PIR in next vmentry.
  5069. */
  5070. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  5071. {
  5072. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5073. int r;
  5074. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  5075. if (!r)
  5076. return;
  5077. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  5078. return;
  5079. /* If a previous notification has sent the IPI, nothing to do. */
  5080. if (pi_test_and_set_on(&vmx->pi_desc))
  5081. return;
  5082. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  5083. kvm_vcpu_kick(vcpu);
  5084. }
  5085. /*
  5086. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  5087. * will not change in the lifetime of the guest.
  5088. * Note that host-state that does change is set elsewhere. E.g., host-state
  5089. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  5090. */
  5091. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  5092. {
  5093. u32 low32, high32;
  5094. unsigned long tmpl;
  5095. struct desc_ptr dt;
  5096. unsigned long cr0, cr3, cr4;
  5097. cr0 = read_cr0();
  5098. WARN_ON(cr0 & X86_CR0_TS);
  5099. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  5100. /*
  5101. * Save the most likely value for this task's CR3 in the VMCS.
  5102. * We can't use __get_current_cr3_fast() because we're not atomic.
  5103. */
  5104. cr3 = __read_cr3();
  5105. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  5106. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  5107. /* Save the most likely value for this task's CR4 in the VMCS. */
  5108. cr4 = cr4_read_shadow();
  5109. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  5110. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  5111. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  5112. #ifdef CONFIG_X86_64
  5113. /*
  5114. * Load null selectors, so we can avoid reloading them in
  5115. * __vmx_load_host_state(), in case userspace uses the null selectors
  5116. * too (the expected case).
  5117. */
  5118. vmcs_write16(HOST_DS_SELECTOR, 0);
  5119. vmcs_write16(HOST_ES_SELECTOR, 0);
  5120. #else
  5121. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5122. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5123. #endif
  5124. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5125. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  5126. store_idt(&dt);
  5127. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  5128. vmx->host_idt_base = dt.address;
  5129. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  5130. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  5131. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  5132. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  5133. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  5134. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  5135. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  5136. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  5137. }
  5138. }
  5139. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  5140. {
  5141. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  5142. if (enable_ept)
  5143. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  5144. if (is_guest_mode(&vmx->vcpu))
  5145. vmx->vcpu.arch.cr4_guest_owned_bits &=
  5146. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  5147. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  5148. }
  5149. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  5150. {
  5151. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  5152. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  5153. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  5154. if (!enable_vnmi)
  5155. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  5156. /* Enable the preemption timer dynamically */
  5157. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  5158. return pin_based_exec_ctrl;
  5159. }
  5160. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  5161. {
  5162. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5163. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5164. if (cpu_has_secondary_exec_ctrls()) {
  5165. if (kvm_vcpu_apicv_active(vcpu))
  5166. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  5167. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5168. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5169. else
  5170. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  5171. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5172. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5173. }
  5174. if (cpu_has_vmx_msr_bitmap())
  5175. vmx_update_msr_bitmap(vcpu);
  5176. }
  5177. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  5178. {
  5179. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  5180. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  5181. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  5182. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  5183. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5184. #ifdef CONFIG_X86_64
  5185. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  5186. CPU_BASED_CR8_LOAD_EXITING;
  5187. #endif
  5188. }
  5189. if (!enable_ept)
  5190. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  5191. CPU_BASED_CR3_LOAD_EXITING |
  5192. CPU_BASED_INVLPG_EXITING;
  5193. if (kvm_mwait_in_guest(vmx->vcpu.kvm))
  5194. exec_control &= ~(CPU_BASED_MWAIT_EXITING |
  5195. CPU_BASED_MONITOR_EXITING);
  5196. if (kvm_hlt_in_guest(vmx->vcpu.kvm))
  5197. exec_control &= ~CPU_BASED_HLT_EXITING;
  5198. return exec_control;
  5199. }
  5200. static bool vmx_rdrand_supported(void)
  5201. {
  5202. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5203. SECONDARY_EXEC_RDRAND_EXITING;
  5204. }
  5205. static bool vmx_rdseed_supported(void)
  5206. {
  5207. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5208. SECONDARY_EXEC_RDSEED_EXITING;
  5209. }
  5210. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  5211. {
  5212. struct kvm_vcpu *vcpu = &vmx->vcpu;
  5213. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  5214. if (!cpu_need_virtualize_apic_accesses(vcpu))
  5215. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5216. if (vmx->vpid == 0)
  5217. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  5218. if (!enable_ept) {
  5219. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  5220. enable_unrestricted_guest = 0;
  5221. /* Enable INVPCID for non-ept guests may cause performance regression. */
  5222. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5223. }
  5224. if (!enable_unrestricted_guest)
  5225. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  5226. if (kvm_pause_in_guest(vmx->vcpu.kvm))
  5227. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  5228. if (!kvm_vcpu_apicv_active(vcpu))
  5229. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5230. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5231. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  5232. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  5233. * in vmx_set_cr4. */
  5234. exec_control &= ~SECONDARY_EXEC_DESC;
  5235. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  5236. (handle_vmptrld).
  5237. We can NOT enable shadow_vmcs here because we don't have yet
  5238. a current VMCS12
  5239. */
  5240. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5241. if (!enable_pml)
  5242. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  5243. if (vmx_xsaves_supported()) {
  5244. /* Exposing XSAVES only when XSAVE is exposed */
  5245. bool xsaves_enabled =
  5246. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  5247. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  5248. if (!xsaves_enabled)
  5249. exec_control &= ~SECONDARY_EXEC_XSAVES;
  5250. if (nested) {
  5251. if (xsaves_enabled)
  5252. vmx->nested.msrs.secondary_ctls_high |=
  5253. SECONDARY_EXEC_XSAVES;
  5254. else
  5255. vmx->nested.msrs.secondary_ctls_high &=
  5256. ~SECONDARY_EXEC_XSAVES;
  5257. }
  5258. }
  5259. if (vmx_rdtscp_supported()) {
  5260. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  5261. if (!rdtscp_enabled)
  5262. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5263. if (nested) {
  5264. if (rdtscp_enabled)
  5265. vmx->nested.msrs.secondary_ctls_high |=
  5266. SECONDARY_EXEC_RDTSCP;
  5267. else
  5268. vmx->nested.msrs.secondary_ctls_high &=
  5269. ~SECONDARY_EXEC_RDTSCP;
  5270. }
  5271. }
  5272. if (vmx_invpcid_supported()) {
  5273. /* Exposing INVPCID only when PCID is exposed */
  5274. bool invpcid_enabled =
  5275. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  5276. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  5277. if (!invpcid_enabled) {
  5278. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5279. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  5280. }
  5281. if (nested) {
  5282. if (invpcid_enabled)
  5283. vmx->nested.msrs.secondary_ctls_high |=
  5284. SECONDARY_EXEC_ENABLE_INVPCID;
  5285. else
  5286. vmx->nested.msrs.secondary_ctls_high &=
  5287. ~SECONDARY_EXEC_ENABLE_INVPCID;
  5288. }
  5289. }
  5290. if (vmx_rdrand_supported()) {
  5291. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  5292. if (rdrand_enabled)
  5293. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  5294. if (nested) {
  5295. if (rdrand_enabled)
  5296. vmx->nested.msrs.secondary_ctls_high |=
  5297. SECONDARY_EXEC_RDRAND_EXITING;
  5298. else
  5299. vmx->nested.msrs.secondary_ctls_high &=
  5300. ~SECONDARY_EXEC_RDRAND_EXITING;
  5301. }
  5302. }
  5303. if (vmx_rdseed_supported()) {
  5304. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  5305. if (rdseed_enabled)
  5306. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  5307. if (nested) {
  5308. if (rdseed_enabled)
  5309. vmx->nested.msrs.secondary_ctls_high |=
  5310. SECONDARY_EXEC_RDSEED_EXITING;
  5311. else
  5312. vmx->nested.msrs.secondary_ctls_high &=
  5313. ~SECONDARY_EXEC_RDSEED_EXITING;
  5314. }
  5315. }
  5316. vmx->secondary_exec_control = exec_control;
  5317. }
  5318. static void ept_set_mmio_spte_mask(void)
  5319. {
  5320. /*
  5321. * EPT Misconfigurations can be generated if the value of bits 2:0
  5322. * of an EPT paging-structure entry is 110b (write/execute).
  5323. */
  5324. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  5325. VMX_EPT_MISCONFIG_WX_VALUE);
  5326. }
  5327. #define VMX_XSS_EXIT_BITMAP 0
  5328. /*
  5329. * Sets up the vmcs for emulated real mode.
  5330. */
  5331. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  5332. {
  5333. #ifdef CONFIG_X86_64
  5334. unsigned long a;
  5335. #endif
  5336. int i;
  5337. if (enable_shadow_vmcs) {
  5338. /*
  5339. * At vCPU creation, "VMWRITE to any supported field
  5340. * in the VMCS" is supported, so use the more
  5341. * permissive vmx_vmread_bitmap to specify both read
  5342. * and write permissions for the shadow VMCS.
  5343. */
  5344. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  5345. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
  5346. }
  5347. if (cpu_has_vmx_msr_bitmap())
  5348. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  5349. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  5350. /* Control */
  5351. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5352. vmx->hv_deadline_tsc = -1;
  5353. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  5354. if (cpu_has_secondary_exec_ctrls()) {
  5355. vmx_compute_secondary_exec_control(vmx);
  5356. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5357. vmx->secondary_exec_control);
  5358. }
  5359. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  5360. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  5361. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  5362. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  5363. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  5364. vmcs_write16(GUEST_INTR_STATUS, 0);
  5365. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  5366. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  5367. }
  5368. if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
  5369. vmcs_write32(PLE_GAP, ple_gap);
  5370. vmx->ple_window = ple_window;
  5371. vmx->ple_window_dirty = true;
  5372. }
  5373. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  5374. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  5375. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  5376. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  5377. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  5378. vmx_set_constant_host_state(vmx);
  5379. #ifdef CONFIG_X86_64
  5380. rdmsrl(MSR_FS_BASE, a);
  5381. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  5382. rdmsrl(MSR_GS_BASE, a);
  5383. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  5384. #else
  5385. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  5386. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  5387. #endif
  5388. if (cpu_has_vmx_vmfunc())
  5389. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  5390. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  5391. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  5392. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  5393. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  5394. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  5395. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5396. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5397. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  5398. u32 index = vmx_msr_index[i];
  5399. u32 data_low, data_high;
  5400. int j = vmx->nmsrs;
  5401. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  5402. continue;
  5403. if (wrmsr_safe(index, data_low, data_high) < 0)
  5404. continue;
  5405. vmx->guest_msrs[j].index = i;
  5406. vmx->guest_msrs[j].data = 0;
  5407. vmx->guest_msrs[j].mask = -1ull;
  5408. ++vmx->nmsrs;
  5409. }
  5410. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
  5411. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
  5412. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  5413. /* 22.2.1, 20.8.1 */
  5414. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  5415. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  5416. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  5417. set_cr4_guest_host_mask(vmx);
  5418. if (vmx_xsaves_supported())
  5419. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  5420. if (enable_pml) {
  5421. ASSERT(vmx->pml_pg);
  5422. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  5423. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  5424. }
  5425. }
  5426. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  5427. {
  5428. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5429. struct msr_data apic_base_msr;
  5430. u64 cr0;
  5431. vmx->rmode.vm86_active = 0;
  5432. vmx->spec_ctrl = 0;
  5433. vcpu->arch.microcode_version = 0x100000000ULL;
  5434. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  5435. kvm_set_cr8(vcpu, 0);
  5436. if (!init_event) {
  5437. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  5438. MSR_IA32_APICBASE_ENABLE;
  5439. if (kvm_vcpu_is_reset_bsp(vcpu))
  5440. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  5441. apic_base_msr.host_initiated = true;
  5442. kvm_set_apic_base(vcpu, &apic_base_msr);
  5443. }
  5444. vmx_segment_cache_clear(vmx);
  5445. seg_setup(VCPU_SREG_CS);
  5446. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  5447. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  5448. seg_setup(VCPU_SREG_DS);
  5449. seg_setup(VCPU_SREG_ES);
  5450. seg_setup(VCPU_SREG_FS);
  5451. seg_setup(VCPU_SREG_GS);
  5452. seg_setup(VCPU_SREG_SS);
  5453. vmcs_write16(GUEST_TR_SELECTOR, 0);
  5454. vmcs_writel(GUEST_TR_BASE, 0);
  5455. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  5456. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  5457. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  5458. vmcs_writel(GUEST_LDTR_BASE, 0);
  5459. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5460. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5461. if (!init_event) {
  5462. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5463. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5464. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5465. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5466. }
  5467. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5468. kvm_rip_write(vcpu, 0xfff0);
  5469. vmcs_writel(GUEST_GDTR_BASE, 0);
  5470. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5471. vmcs_writel(GUEST_IDTR_BASE, 0);
  5472. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5473. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5474. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5475. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5476. if (kvm_mpx_supported())
  5477. vmcs_write64(GUEST_BNDCFGS, 0);
  5478. setup_msrs(vmx);
  5479. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5480. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5481. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5482. if (cpu_need_tpr_shadow(vcpu))
  5483. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5484. __pa(vcpu->arch.apic->regs));
  5485. vmcs_write32(TPR_THRESHOLD, 0);
  5486. }
  5487. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5488. if (vmx->vpid != 0)
  5489. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5490. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5491. vmx->vcpu.arch.cr0 = cr0;
  5492. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5493. vmx_set_cr4(vcpu, 0);
  5494. vmx_set_efer(vcpu, 0);
  5495. update_exception_bitmap(vcpu);
  5496. vpid_sync_context(vmx->vpid);
  5497. if (init_event)
  5498. vmx_clear_hlt(vcpu);
  5499. }
  5500. /*
  5501. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5502. * For most existing hypervisors, this will always return true.
  5503. */
  5504. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5505. {
  5506. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5507. PIN_BASED_EXT_INTR_MASK;
  5508. }
  5509. /*
  5510. * In nested virtualization, check if L1 has set
  5511. * VM_EXIT_ACK_INTR_ON_EXIT
  5512. */
  5513. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5514. {
  5515. return get_vmcs12(vcpu)->vm_exit_controls &
  5516. VM_EXIT_ACK_INTR_ON_EXIT;
  5517. }
  5518. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5519. {
  5520. return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
  5521. }
  5522. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5523. {
  5524. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5525. CPU_BASED_VIRTUAL_INTR_PENDING);
  5526. }
  5527. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5528. {
  5529. if (!enable_vnmi ||
  5530. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5531. enable_irq_window(vcpu);
  5532. return;
  5533. }
  5534. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5535. CPU_BASED_VIRTUAL_NMI_PENDING);
  5536. }
  5537. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5538. {
  5539. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5540. uint32_t intr;
  5541. int irq = vcpu->arch.interrupt.nr;
  5542. trace_kvm_inj_virq(irq);
  5543. ++vcpu->stat.irq_injections;
  5544. if (vmx->rmode.vm86_active) {
  5545. int inc_eip = 0;
  5546. if (vcpu->arch.interrupt.soft)
  5547. inc_eip = vcpu->arch.event_exit_inst_len;
  5548. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5549. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5550. return;
  5551. }
  5552. intr = irq | INTR_INFO_VALID_MASK;
  5553. if (vcpu->arch.interrupt.soft) {
  5554. intr |= INTR_TYPE_SOFT_INTR;
  5555. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5556. vmx->vcpu.arch.event_exit_inst_len);
  5557. } else
  5558. intr |= INTR_TYPE_EXT_INTR;
  5559. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5560. vmx_clear_hlt(vcpu);
  5561. }
  5562. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5563. {
  5564. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5565. if (!enable_vnmi) {
  5566. /*
  5567. * Tracking the NMI-blocked state in software is built upon
  5568. * finding the next open IRQ window. This, in turn, depends on
  5569. * well-behaving guests: They have to keep IRQs disabled at
  5570. * least as long as the NMI handler runs. Otherwise we may
  5571. * cause NMI nesting, maybe breaking the guest. But as this is
  5572. * highly unlikely, we can live with the residual risk.
  5573. */
  5574. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5575. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5576. }
  5577. ++vcpu->stat.nmi_injections;
  5578. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5579. if (vmx->rmode.vm86_active) {
  5580. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5581. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5582. return;
  5583. }
  5584. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5585. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5586. vmx_clear_hlt(vcpu);
  5587. }
  5588. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5589. {
  5590. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5591. bool masked;
  5592. if (!enable_vnmi)
  5593. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5594. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5595. return false;
  5596. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5597. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5598. return masked;
  5599. }
  5600. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5601. {
  5602. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5603. if (!enable_vnmi) {
  5604. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5605. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5606. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5607. }
  5608. } else {
  5609. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5610. if (masked)
  5611. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5612. GUEST_INTR_STATE_NMI);
  5613. else
  5614. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5615. GUEST_INTR_STATE_NMI);
  5616. }
  5617. }
  5618. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5619. {
  5620. if (to_vmx(vcpu)->nested.nested_run_pending)
  5621. return 0;
  5622. if (!enable_vnmi &&
  5623. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5624. return 0;
  5625. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5626. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5627. | GUEST_INTR_STATE_NMI));
  5628. }
  5629. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5630. {
  5631. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  5632. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5633. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5634. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5635. }
  5636. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5637. {
  5638. int ret;
  5639. if (enable_unrestricted_guest)
  5640. return 0;
  5641. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5642. PAGE_SIZE * 3);
  5643. if (ret)
  5644. return ret;
  5645. to_kvm_vmx(kvm)->tss_addr = addr;
  5646. return init_rmode_tss(kvm);
  5647. }
  5648. static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
  5649. {
  5650. to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
  5651. return 0;
  5652. }
  5653. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5654. {
  5655. switch (vec) {
  5656. case BP_VECTOR:
  5657. /*
  5658. * Update instruction length as we may reinject the exception
  5659. * from user space while in guest debugging mode.
  5660. */
  5661. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5662. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5663. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5664. return false;
  5665. /* fall through */
  5666. case DB_VECTOR:
  5667. if (vcpu->guest_debug &
  5668. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  5669. return false;
  5670. /* fall through */
  5671. case DE_VECTOR:
  5672. case OF_VECTOR:
  5673. case BR_VECTOR:
  5674. case UD_VECTOR:
  5675. case DF_VECTOR:
  5676. case SS_VECTOR:
  5677. case GP_VECTOR:
  5678. case MF_VECTOR:
  5679. return true;
  5680. break;
  5681. }
  5682. return false;
  5683. }
  5684. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  5685. int vec, u32 err_code)
  5686. {
  5687. /*
  5688. * Instruction with address size override prefix opcode 0x67
  5689. * Cause the #SS fault with 0 error code in VM86 mode.
  5690. */
  5691. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  5692. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  5693. if (vcpu->arch.halt_request) {
  5694. vcpu->arch.halt_request = 0;
  5695. return kvm_vcpu_halt(vcpu);
  5696. }
  5697. return 1;
  5698. }
  5699. return 0;
  5700. }
  5701. /*
  5702. * Forward all other exceptions that are valid in real mode.
  5703. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  5704. * the required debugging infrastructure rework.
  5705. */
  5706. kvm_queue_exception(vcpu, vec);
  5707. return 1;
  5708. }
  5709. /*
  5710. * Trigger machine check on the host. We assume all the MSRs are already set up
  5711. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  5712. * We pass a fake environment to the machine check handler because we want
  5713. * the guest to be always treated like user space, no matter what context
  5714. * it used internally.
  5715. */
  5716. static void kvm_machine_check(void)
  5717. {
  5718. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  5719. struct pt_regs regs = {
  5720. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  5721. .flags = X86_EFLAGS_IF,
  5722. };
  5723. do_machine_check(&regs, 0);
  5724. #endif
  5725. }
  5726. static int handle_machine_check(struct kvm_vcpu *vcpu)
  5727. {
  5728. /* already handled by vcpu_run */
  5729. return 1;
  5730. }
  5731. static int handle_exception(struct kvm_vcpu *vcpu)
  5732. {
  5733. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5734. struct kvm_run *kvm_run = vcpu->run;
  5735. u32 intr_info, ex_no, error_code;
  5736. unsigned long cr2, rip, dr6;
  5737. u32 vect_info;
  5738. enum emulation_result er;
  5739. vect_info = vmx->idt_vectoring_info;
  5740. intr_info = vmx->exit_intr_info;
  5741. if (is_machine_check(intr_info))
  5742. return handle_machine_check(vcpu);
  5743. if (is_nmi(intr_info))
  5744. return 1; /* already handled by vmx_vcpu_run() */
  5745. if (is_invalid_opcode(intr_info))
  5746. return handle_ud(vcpu);
  5747. error_code = 0;
  5748. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  5749. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  5750. if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
  5751. WARN_ON_ONCE(!enable_vmware_backdoor);
  5752. er = emulate_instruction(vcpu,
  5753. EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
  5754. if (er == EMULATE_USER_EXIT)
  5755. return 0;
  5756. else if (er != EMULATE_DONE)
  5757. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  5758. return 1;
  5759. }
  5760. /*
  5761. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  5762. * MMIO, it is better to report an internal error.
  5763. * See the comments in vmx_handle_exit.
  5764. */
  5765. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  5766. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  5767. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5768. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  5769. vcpu->run->internal.ndata = 3;
  5770. vcpu->run->internal.data[0] = vect_info;
  5771. vcpu->run->internal.data[1] = intr_info;
  5772. vcpu->run->internal.data[2] = error_code;
  5773. return 0;
  5774. }
  5775. if (is_page_fault(intr_info)) {
  5776. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  5777. /* EPT won't cause page fault directly */
  5778. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  5779. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  5780. }
  5781. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  5782. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  5783. return handle_rmode_exception(vcpu, ex_no, error_code);
  5784. switch (ex_no) {
  5785. case AC_VECTOR:
  5786. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  5787. return 1;
  5788. case DB_VECTOR:
  5789. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  5790. if (!(vcpu->guest_debug &
  5791. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  5792. vcpu->arch.dr6 &= ~15;
  5793. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  5794. if (is_icebp(intr_info))
  5795. skip_emulated_instruction(vcpu);
  5796. kvm_queue_exception(vcpu, DB_VECTOR);
  5797. return 1;
  5798. }
  5799. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  5800. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  5801. /* fall through */
  5802. case BP_VECTOR:
  5803. /*
  5804. * Update instruction length as we may reinject #BP from
  5805. * user space while in guest debugging mode. Reading it for
  5806. * #DB as well causes no harm, it is not used in that case.
  5807. */
  5808. vmx->vcpu.arch.event_exit_inst_len =
  5809. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5810. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  5811. rip = kvm_rip_read(vcpu);
  5812. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  5813. kvm_run->debug.arch.exception = ex_no;
  5814. break;
  5815. default:
  5816. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  5817. kvm_run->ex.exception = ex_no;
  5818. kvm_run->ex.error_code = error_code;
  5819. break;
  5820. }
  5821. return 0;
  5822. }
  5823. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  5824. {
  5825. ++vcpu->stat.irq_exits;
  5826. return 1;
  5827. }
  5828. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  5829. {
  5830. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  5831. vcpu->mmio_needed = 0;
  5832. return 0;
  5833. }
  5834. static int handle_io(struct kvm_vcpu *vcpu)
  5835. {
  5836. unsigned long exit_qualification;
  5837. int size, in, string;
  5838. unsigned port;
  5839. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5840. string = (exit_qualification & 16) != 0;
  5841. ++vcpu->stat.io_exits;
  5842. if (string)
  5843. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5844. port = exit_qualification >> 16;
  5845. size = (exit_qualification & 7) + 1;
  5846. in = (exit_qualification & 8) != 0;
  5847. return kvm_fast_pio(vcpu, size, port, in);
  5848. }
  5849. static void
  5850. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  5851. {
  5852. /*
  5853. * Patch in the VMCALL instruction:
  5854. */
  5855. hypercall[0] = 0x0f;
  5856. hypercall[1] = 0x01;
  5857. hypercall[2] = 0xc1;
  5858. }
  5859. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  5860. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  5861. {
  5862. if (is_guest_mode(vcpu)) {
  5863. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5864. unsigned long orig_val = val;
  5865. /*
  5866. * We get here when L2 changed cr0 in a way that did not change
  5867. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  5868. * but did change L0 shadowed bits. So we first calculate the
  5869. * effective cr0 value that L1 would like to write into the
  5870. * hardware. It consists of the L2-owned bits from the new
  5871. * value combined with the L1-owned bits from L1's guest_cr0.
  5872. */
  5873. val = (val & ~vmcs12->cr0_guest_host_mask) |
  5874. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  5875. if (!nested_guest_cr0_valid(vcpu, val))
  5876. return 1;
  5877. if (kvm_set_cr0(vcpu, val))
  5878. return 1;
  5879. vmcs_writel(CR0_READ_SHADOW, orig_val);
  5880. return 0;
  5881. } else {
  5882. if (to_vmx(vcpu)->nested.vmxon &&
  5883. !nested_host_cr0_valid(vcpu, val))
  5884. return 1;
  5885. return kvm_set_cr0(vcpu, val);
  5886. }
  5887. }
  5888. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  5889. {
  5890. if (is_guest_mode(vcpu)) {
  5891. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5892. unsigned long orig_val = val;
  5893. /* analogously to handle_set_cr0 */
  5894. val = (val & ~vmcs12->cr4_guest_host_mask) |
  5895. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  5896. if (kvm_set_cr4(vcpu, val))
  5897. return 1;
  5898. vmcs_writel(CR4_READ_SHADOW, orig_val);
  5899. return 0;
  5900. } else
  5901. return kvm_set_cr4(vcpu, val);
  5902. }
  5903. static int handle_desc(struct kvm_vcpu *vcpu)
  5904. {
  5905. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  5906. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5907. }
  5908. static int handle_cr(struct kvm_vcpu *vcpu)
  5909. {
  5910. unsigned long exit_qualification, val;
  5911. int cr;
  5912. int reg;
  5913. int err;
  5914. int ret;
  5915. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5916. cr = exit_qualification & 15;
  5917. reg = (exit_qualification >> 8) & 15;
  5918. switch ((exit_qualification >> 4) & 3) {
  5919. case 0: /* mov to cr */
  5920. val = kvm_register_readl(vcpu, reg);
  5921. trace_kvm_cr_write(cr, val);
  5922. switch (cr) {
  5923. case 0:
  5924. err = handle_set_cr0(vcpu, val);
  5925. return kvm_complete_insn_gp(vcpu, err);
  5926. case 3:
  5927. WARN_ON_ONCE(enable_unrestricted_guest);
  5928. err = kvm_set_cr3(vcpu, val);
  5929. return kvm_complete_insn_gp(vcpu, err);
  5930. case 4:
  5931. err = handle_set_cr4(vcpu, val);
  5932. return kvm_complete_insn_gp(vcpu, err);
  5933. case 8: {
  5934. u8 cr8_prev = kvm_get_cr8(vcpu);
  5935. u8 cr8 = (u8)val;
  5936. err = kvm_set_cr8(vcpu, cr8);
  5937. ret = kvm_complete_insn_gp(vcpu, err);
  5938. if (lapic_in_kernel(vcpu))
  5939. return ret;
  5940. if (cr8_prev <= cr8)
  5941. return ret;
  5942. /*
  5943. * TODO: we might be squashing a
  5944. * KVM_GUESTDBG_SINGLESTEP-triggered
  5945. * KVM_EXIT_DEBUG here.
  5946. */
  5947. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  5948. return 0;
  5949. }
  5950. }
  5951. break;
  5952. case 2: /* clts */
  5953. WARN_ONCE(1, "Guest should always own CR0.TS");
  5954. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  5955. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  5956. return kvm_skip_emulated_instruction(vcpu);
  5957. case 1: /*mov from cr*/
  5958. switch (cr) {
  5959. case 3:
  5960. WARN_ON_ONCE(enable_unrestricted_guest);
  5961. val = kvm_read_cr3(vcpu);
  5962. kvm_register_write(vcpu, reg, val);
  5963. trace_kvm_cr_read(cr, val);
  5964. return kvm_skip_emulated_instruction(vcpu);
  5965. case 8:
  5966. val = kvm_get_cr8(vcpu);
  5967. kvm_register_write(vcpu, reg, val);
  5968. trace_kvm_cr_read(cr, val);
  5969. return kvm_skip_emulated_instruction(vcpu);
  5970. }
  5971. break;
  5972. case 3: /* lmsw */
  5973. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  5974. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  5975. kvm_lmsw(vcpu, val);
  5976. return kvm_skip_emulated_instruction(vcpu);
  5977. default:
  5978. break;
  5979. }
  5980. vcpu->run->exit_reason = 0;
  5981. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  5982. (int)(exit_qualification >> 4) & 3, cr);
  5983. return 0;
  5984. }
  5985. static int handle_dr(struct kvm_vcpu *vcpu)
  5986. {
  5987. unsigned long exit_qualification;
  5988. int dr, dr7, reg;
  5989. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5990. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  5991. /* First, if DR does not exist, trigger UD */
  5992. if (!kvm_require_dr(vcpu, dr))
  5993. return 1;
  5994. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  5995. if (!kvm_require_cpl(vcpu, 0))
  5996. return 1;
  5997. dr7 = vmcs_readl(GUEST_DR7);
  5998. if (dr7 & DR7_GD) {
  5999. /*
  6000. * As the vm-exit takes precedence over the debug trap, we
  6001. * need to emulate the latter, either for the host or the
  6002. * guest debugging itself.
  6003. */
  6004. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  6005. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  6006. vcpu->run->debug.arch.dr7 = dr7;
  6007. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  6008. vcpu->run->debug.arch.exception = DB_VECTOR;
  6009. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  6010. return 0;
  6011. } else {
  6012. vcpu->arch.dr6 &= ~15;
  6013. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  6014. kvm_queue_exception(vcpu, DB_VECTOR);
  6015. return 1;
  6016. }
  6017. }
  6018. if (vcpu->guest_debug == 0) {
  6019. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6020. CPU_BASED_MOV_DR_EXITING);
  6021. /*
  6022. * No more DR vmexits; force a reload of the debug registers
  6023. * and reenter on this instruction. The next vmexit will
  6024. * retrieve the full state of the debug registers.
  6025. */
  6026. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  6027. return 1;
  6028. }
  6029. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  6030. if (exit_qualification & TYPE_MOV_FROM_DR) {
  6031. unsigned long val;
  6032. if (kvm_get_dr(vcpu, dr, &val))
  6033. return 1;
  6034. kvm_register_write(vcpu, reg, val);
  6035. } else
  6036. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  6037. return 1;
  6038. return kvm_skip_emulated_instruction(vcpu);
  6039. }
  6040. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  6041. {
  6042. return vcpu->arch.dr6;
  6043. }
  6044. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  6045. {
  6046. }
  6047. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  6048. {
  6049. get_debugreg(vcpu->arch.db[0], 0);
  6050. get_debugreg(vcpu->arch.db[1], 1);
  6051. get_debugreg(vcpu->arch.db[2], 2);
  6052. get_debugreg(vcpu->arch.db[3], 3);
  6053. get_debugreg(vcpu->arch.dr6, 6);
  6054. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  6055. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  6056. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  6057. }
  6058. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  6059. {
  6060. vmcs_writel(GUEST_DR7, val);
  6061. }
  6062. static int handle_cpuid(struct kvm_vcpu *vcpu)
  6063. {
  6064. return kvm_emulate_cpuid(vcpu);
  6065. }
  6066. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  6067. {
  6068. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6069. struct msr_data msr_info;
  6070. msr_info.index = ecx;
  6071. msr_info.host_initiated = false;
  6072. if (vmx_get_msr(vcpu, &msr_info)) {
  6073. trace_kvm_msr_read_ex(ecx);
  6074. kvm_inject_gp(vcpu, 0);
  6075. return 1;
  6076. }
  6077. trace_kvm_msr_read(ecx, msr_info.data);
  6078. /* FIXME: handling of bits 32:63 of rax, rdx */
  6079. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  6080. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  6081. return kvm_skip_emulated_instruction(vcpu);
  6082. }
  6083. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  6084. {
  6085. struct msr_data msr;
  6086. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6087. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  6088. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  6089. msr.data = data;
  6090. msr.index = ecx;
  6091. msr.host_initiated = false;
  6092. if (kvm_set_msr(vcpu, &msr) != 0) {
  6093. trace_kvm_msr_write_ex(ecx, data);
  6094. kvm_inject_gp(vcpu, 0);
  6095. return 1;
  6096. }
  6097. trace_kvm_msr_write(ecx, data);
  6098. return kvm_skip_emulated_instruction(vcpu);
  6099. }
  6100. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  6101. {
  6102. kvm_apic_update_ppr(vcpu);
  6103. return 1;
  6104. }
  6105. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  6106. {
  6107. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6108. CPU_BASED_VIRTUAL_INTR_PENDING);
  6109. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6110. ++vcpu->stat.irq_window_exits;
  6111. return 1;
  6112. }
  6113. static int handle_halt(struct kvm_vcpu *vcpu)
  6114. {
  6115. return kvm_emulate_halt(vcpu);
  6116. }
  6117. static int handle_vmcall(struct kvm_vcpu *vcpu)
  6118. {
  6119. return kvm_emulate_hypercall(vcpu);
  6120. }
  6121. static int handle_invd(struct kvm_vcpu *vcpu)
  6122. {
  6123. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6124. }
  6125. static int handle_invlpg(struct kvm_vcpu *vcpu)
  6126. {
  6127. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6128. kvm_mmu_invlpg(vcpu, exit_qualification);
  6129. return kvm_skip_emulated_instruction(vcpu);
  6130. }
  6131. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  6132. {
  6133. int err;
  6134. err = kvm_rdpmc(vcpu);
  6135. return kvm_complete_insn_gp(vcpu, err);
  6136. }
  6137. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  6138. {
  6139. return kvm_emulate_wbinvd(vcpu);
  6140. }
  6141. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  6142. {
  6143. u64 new_bv = kvm_read_edx_eax(vcpu);
  6144. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  6145. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  6146. return kvm_skip_emulated_instruction(vcpu);
  6147. return 1;
  6148. }
  6149. static int handle_xsaves(struct kvm_vcpu *vcpu)
  6150. {
  6151. kvm_skip_emulated_instruction(vcpu);
  6152. WARN(1, "this should never happen\n");
  6153. return 1;
  6154. }
  6155. static int handle_xrstors(struct kvm_vcpu *vcpu)
  6156. {
  6157. kvm_skip_emulated_instruction(vcpu);
  6158. WARN(1, "this should never happen\n");
  6159. return 1;
  6160. }
  6161. static int handle_apic_access(struct kvm_vcpu *vcpu)
  6162. {
  6163. if (likely(fasteoi)) {
  6164. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6165. int access_type, offset;
  6166. access_type = exit_qualification & APIC_ACCESS_TYPE;
  6167. offset = exit_qualification & APIC_ACCESS_OFFSET;
  6168. /*
  6169. * Sane guest uses MOV to write EOI, with written value
  6170. * not cared. So make a short-circuit here by avoiding
  6171. * heavy instruction emulation.
  6172. */
  6173. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  6174. (offset == APIC_EOI)) {
  6175. kvm_lapic_set_eoi(vcpu);
  6176. return kvm_skip_emulated_instruction(vcpu);
  6177. }
  6178. }
  6179. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6180. }
  6181. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  6182. {
  6183. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6184. int vector = exit_qualification & 0xff;
  6185. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  6186. kvm_apic_set_eoi_accelerated(vcpu, vector);
  6187. return 1;
  6188. }
  6189. static int handle_apic_write(struct kvm_vcpu *vcpu)
  6190. {
  6191. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6192. u32 offset = exit_qualification & 0xfff;
  6193. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  6194. kvm_apic_write_nodecode(vcpu, offset);
  6195. return 1;
  6196. }
  6197. static int handle_task_switch(struct kvm_vcpu *vcpu)
  6198. {
  6199. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6200. unsigned long exit_qualification;
  6201. bool has_error_code = false;
  6202. u32 error_code = 0;
  6203. u16 tss_selector;
  6204. int reason, type, idt_v, idt_index;
  6205. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  6206. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  6207. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  6208. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6209. reason = (u32)exit_qualification >> 30;
  6210. if (reason == TASK_SWITCH_GATE && idt_v) {
  6211. switch (type) {
  6212. case INTR_TYPE_NMI_INTR:
  6213. vcpu->arch.nmi_injected = false;
  6214. vmx_set_nmi_mask(vcpu, true);
  6215. break;
  6216. case INTR_TYPE_EXT_INTR:
  6217. case INTR_TYPE_SOFT_INTR:
  6218. kvm_clear_interrupt_queue(vcpu);
  6219. break;
  6220. case INTR_TYPE_HARD_EXCEPTION:
  6221. if (vmx->idt_vectoring_info &
  6222. VECTORING_INFO_DELIVER_CODE_MASK) {
  6223. has_error_code = true;
  6224. error_code =
  6225. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6226. }
  6227. /* fall through */
  6228. case INTR_TYPE_SOFT_EXCEPTION:
  6229. kvm_clear_exception_queue(vcpu);
  6230. break;
  6231. default:
  6232. break;
  6233. }
  6234. }
  6235. tss_selector = exit_qualification;
  6236. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  6237. type != INTR_TYPE_EXT_INTR &&
  6238. type != INTR_TYPE_NMI_INTR))
  6239. skip_emulated_instruction(vcpu);
  6240. if (kvm_task_switch(vcpu, tss_selector,
  6241. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  6242. has_error_code, error_code) == EMULATE_FAIL) {
  6243. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6244. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6245. vcpu->run->internal.ndata = 0;
  6246. return 0;
  6247. }
  6248. /*
  6249. * TODO: What about debug traps on tss switch?
  6250. * Are we supposed to inject them and update dr6?
  6251. */
  6252. return 1;
  6253. }
  6254. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  6255. {
  6256. unsigned long exit_qualification;
  6257. gpa_t gpa;
  6258. u64 error_code;
  6259. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6260. /*
  6261. * EPT violation happened while executing iret from NMI,
  6262. * "blocked by NMI" bit has to be set before next VM entry.
  6263. * There are errata that may cause this bit to not be set:
  6264. * AAK134, BY25.
  6265. */
  6266. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6267. enable_vnmi &&
  6268. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6269. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  6270. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6271. trace_kvm_page_fault(gpa, exit_qualification);
  6272. /* Is it a read fault? */
  6273. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  6274. ? PFERR_USER_MASK : 0;
  6275. /* Is it a write fault? */
  6276. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  6277. ? PFERR_WRITE_MASK : 0;
  6278. /* Is it a fetch fault? */
  6279. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  6280. ? PFERR_FETCH_MASK : 0;
  6281. /* ept page table entry is present? */
  6282. error_code |= (exit_qualification &
  6283. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  6284. EPT_VIOLATION_EXECUTABLE))
  6285. ? PFERR_PRESENT_MASK : 0;
  6286. error_code |= (exit_qualification & 0x100) != 0 ?
  6287. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  6288. vcpu->arch.exit_qualification = exit_qualification;
  6289. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  6290. }
  6291. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  6292. {
  6293. gpa_t gpa;
  6294. /*
  6295. * A nested guest cannot optimize MMIO vmexits, because we have an
  6296. * nGPA here instead of the required GPA.
  6297. */
  6298. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6299. if (!is_guest_mode(vcpu) &&
  6300. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  6301. trace_kvm_fast_mmio(gpa);
  6302. /*
  6303. * Doing kvm_skip_emulated_instruction() depends on undefined
  6304. * behavior: Intel's manual doesn't mandate
  6305. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  6306. * occurs and while on real hardware it was observed to be set,
  6307. * other hypervisors (namely Hyper-V) don't set it, we end up
  6308. * advancing IP with some random value. Disable fast mmio when
  6309. * running nested and keep it for real hardware in hope that
  6310. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  6311. */
  6312. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  6313. return kvm_skip_emulated_instruction(vcpu);
  6314. else
  6315. return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
  6316. NULL, 0) == EMULATE_DONE;
  6317. }
  6318. return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  6319. }
  6320. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  6321. {
  6322. WARN_ON_ONCE(!enable_vnmi);
  6323. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6324. CPU_BASED_VIRTUAL_NMI_PENDING);
  6325. ++vcpu->stat.nmi_window_exits;
  6326. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6327. return 1;
  6328. }
  6329. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  6330. {
  6331. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6332. enum emulation_result err = EMULATE_DONE;
  6333. int ret = 1;
  6334. u32 cpu_exec_ctrl;
  6335. bool intr_window_requested;
  6336. unsigned count = 130;
  6337. /*
  6338. * We should never reach the point where we are emulating L2
  6339. * due to invalid guest state as that means we incorrectly
  6340. * allowed a nested VMEntry with an invalid vmcs12.
  6341. */
  6342. WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
  6343. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  6344. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  6345. while (vmx->emulation_required && count-- != 0) {
  6346. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  6347. return handle_interrupt_window(&vmx->vcpu);
  6348. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  6349. return 1;
  6350. err = emulate_instruction(vcpu, 0);
  6351. if (err == EMULATE_USER_EXIT) {
  6352. ++vcpu->stat.mmio_exits;
  6353. ret = 0;
  6354. goto out;
  6355. }
  6356. if (err != EMULATE_DONE)
  6357. goto emulation_error;
  6358. if (vmx->emulation_required && !vmx->rmode.vm86_active &&
  6359. vcpu->arch.exception.pending)
  6360. goto emulation_error;
  6361. if (vcpu->arch.halt_request) {
  6362. vcpu->arch.halt_request = 0;
  6363. ret = kvm_vcpu_halt(vcpu);
  6364. goto out;
  6365. }
  6366. if (signal_pending(current))
  6367. goto out;
  6368. if (need_resched())
  6369. schedule();
  6370. }
  6371. out:
  6372. return ret;
  6373. emulation_error:
  6374. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6375. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6376. vcpu->run->internal.ndata = 0;
  6377. return 0;
  6378. }
  6379. static void grow_ple_window(struct kvm_vcpu *vcpu)
  6380. {
  6381. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6382. int old = vmx->ple_window;
  6383. vmx->ple_window = __grow_ple_window(old, ple_window,
  6384. ple_window_grow,
  6385. ple_window_max);
  6386. if (vmx->ple_window != old)
  6387. vmx->ple_window_dirty = true;
  6388. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  6389. }
  6390. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  6391. {
  6392. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6393. int old = vmx->ple_window;
  6394. vmx->ple_window = __shrink_ple_window(old, ple_window,
  6395. ple_window_shrink,
  6396. ple_window);
  6397. if (vmx->ple_window != old)
  6398. vmx->ple_window_dirty = true;
  6399. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  6400. }
  6401. /*
  6402. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  6403. */
  6404. static void wakeup_handler(void)
  6405. {
  6406. struct kvm_vcpu *vcpu;
  6407. int cpu = smp_processor_id();
  6408. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6409. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  6410. blocked_vcpu_list) {
  6411. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  6412. if (pi_test_on(pi_desc) == 1)
  6413. kvm_vcpu_kick(vcpu);
  6414. }
  6415. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6416. }
  6417. static void vmx_enable_tdp(void)
  6418. {
  6419. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  6420. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  6421. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  6422. 0ull, VMX_EPT_EXECUTABLE_MASK,
  6423. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  6424. VMX_EPT_RWX_MASK, 0ull);
  6425. ept_set_mmio_spte_mask();
  6426. kvm_enable_tdp();
  6427. }
  6428. static __init int hardware_setup(void)
  6429. {
  6430. int r = -ENOMEM, i;
  6431. rdmsrl_safe(MSR_EFER, &host_efer);
  6432. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  6433. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6434. for (i = 0; i < VMX_BITMAP_NR; i++) {
  6435. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  6436. if (!vmx_bitmap[i])
  6437. goto out;
  6438. }
  6439. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6440. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6441. if (setup_vmcs_config(&vmcs_config) < 0) {
  6442. r = -EIO;
  6443. goto out;
  6444. }
  6445. if (boot_cpu_has(X86_FEATURE_NX))
  6446. kvm_enable_efer_bits(EFER_NX);
  6447. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6448. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6449. enable_vpid = 0;
  6450. if (!cpu_has_vmx_ept() ||
  6451. !cpu_has_vmx_ept_4levels() ||
  6452. !cpu_has_vmx_ept_mt_wb() ||
  6453. !cpu_has_vmx_invept_global())
  6454. enable_ept = 0;
  6455. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6456. enable_ept_ad_bits = 0;
  6457. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6458. enable_unrestricted_guest = 0;
  6459. if (!cpu_has_vmx_flexpriority())
  6460. flexpriority_enabled = 0;
  6461. if (!cpu_has_virtual_nmis())
  6462. enable_vnmi = 0;
  6463. /*
  6464. * set_apic_access_page_addr() is used to reload apic access
  6465. * page upon invalidation. No need to do anything if not
  6466. * using the APIC_ACCESS_ADDR VMCS field.
  6467. */
  6468. if (!flexpriority_enabled)
  6469. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6470. if (!cpu_has_vmx_tpr_shadow())
  6471. kvm_x86_ops->update_cr8_intercept = NULL;
  6472. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6473. kvm_disable_largepages();
  6474. if (!cpu_has_vmx_ple()) {
  6475. ple_gap = 0;
  6476. ple_window = 0;
  6477. ple_window_grow = 0;
  6478. ple_window_max = 0;
  6479. ple_window_shrink = 0;
  6480. }
  6481. if (!cpu_has_vmx_apicv()) {
  6482. enable_apicv = 0;
  6483. kvm_x86_ops->sync_pir_to_irr = NULL;
  6484. }
  6485. if (cpu_has_vmx_tsc_scaling()) {
  6486. kvm_has_tsc_control = true;
  6487. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6488. kvm_tsc_scaling_ratio_frac_bits = 48;
  6489. }
  6490. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6491. if (enable_ept)
  6492. vmx_enable_tdp();
  6493. else
  6494. kvm_disable_tdp();
  6495. /*
  6496. * Only enable PML when hardware supports PML feature, and both EPT
  6497. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6498. */
  6499. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6500. enable_pml = 0;
  6501. if (!enable_pml) {
  6502. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6503. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6504. kvm_x86_ops->flush_log_dirty = NULL;
  6505. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6506. }
  6507. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6508. u64 vmx_msr;
  6509. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6510. cpu_preemption_timer_multi =
  6511. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6512. } else {
  6513. kvm_x86_ops->set_hv_timer = NULL;
  6514. kvm_x86_ops->cancel_hv_timer = NULL;
  6515. }
  6516. if (!cpu_has_vmx_shadow_vmcs())
  6517. enable_shadow_vmcs = 0;
  6518. if (enable_shadow_vmcs)
  6519. init_vmcs_shadow_fields();
  6520. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6521. nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
  6522. kvm_mce_cap_supported |= MCG_LMCE_P;
  6523. return alloc_kvm_area();
  6524. out:
  6525. for (i = 0; i < VMX_BITMAP_NR; i++)
  6526. free_page((unsigned long)vmx_bitmap[i]);
  6527. return r;
  6528. }
  6529. static __exit void hardware_unsetup(void)
  6530. {
  6531. int i;
  6532. for (i = 0; i < VMX_BITMAP_NR; i++)
  6533. free_page((unsigned long)vmx_bitmap[i]);
  6534. free_kvm_area();
  6535. }
  6536. /*
  6537. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6538. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6539. */
  6540. static int handle_pause(struct kvm_vcpu *vcpu)
  6541. {
  6542. if (!kvm_pause_in_guest(vcpu->kvm))
  6543. grow_ple_window(vcpu);
  6544. /*
  6545. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6546. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6547. * never set PAUSE_EXITING and just set PLE if supported,
  6548. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6549. */
  6550. kvm_vcpu_on_spin(vcpu, true);
  6551. return kvm_skip_emulated_instruction(vcpu);
  6552. }
  6553. static int handle_nop(struct kvm_vcpu *vcpu)
  6554. {
  6555. return kvm_skip_emulated_instruction(vcpu);
  6556. }
  6557. static int handle_mwait(struct kvm_vcpu *vcpu)
  6558. {
  6559. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6560. return handle_nop(vcpu);
  6561. }
  6562. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6563. {
  6564. kvm_queue_exception(vcpu, UD_VECTOR);
  6565. return 1;
  6566. }
  6567. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6568. {
  6569. return 1;
  6570. }
  6571. static int handle_monitor(struct kvm_vcpu *vcpu)
  6572. {
  6573. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6574. return handle_nop(vcpu);
  6575. }
  6576. /*
  6577. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6578. * set the success or error code of an emulated VMX instruction, as specified
  6579. * by Vol 2B, VMX Instruction Reference, "Conventions".
  6580. */
  6581. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6582. {
  6583. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6584. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6585. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6586. }
  6587. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6588. {
  6589. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6590. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6591. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6592. | X86_EFLAGS_CF);
  6593. }
  6594. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6595. u32 vm_instruction_error)
  6596. {
  6597. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  6598. /*
  6599. * failValid writes the error number to the current VMCS, which
  6600. * can't be done there isn't a current VMCS.
  6601. */
  6602. nested_vmx_failInvalid(vcpu);
  6603. return;
  6604. }
  6605. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6606. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6607. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6608. | X86_EFLAGS_ZF);
  6609. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6610. /*
  6611. * We don't need to force a shadow sync because
  6612. * VM_INSTRUCTION_ERROR is not shadowed
  6613. */
  6614. }
  6615. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6616. {
  6617. /* TODO: not to reset guest simply here. */
  6618. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6619. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6620. }
  6621. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6622. {
  6623. struct vcpu_vmx *vmx =
  6624. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6625. vmx->nested.preemption_timer_expired = true;
  6626. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6627. kvm_vcpu_kick(&vmx->vcpu);
  6628. return HRTIMER_NORESTART;
  6629. }
  6630. /*
  6631. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6632. * exit caused by such an instruction (run by a guest hypervisor).
  6633. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6634. * #UD or #GP.
  6635. */
  6636. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6637. unsigned long exit_qualification,
  6638. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6639. {
  6640. gva_t off;
  6641. bool exn;
  6642. struct kvm_segment s;
  6643. /*
  6644. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6645. * Execution", on an exit, vmx_instruction_info holds most of the
  6646. * addressing components of the operand. Only the displacement part
  6647. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6648. * For how an actual address is calculated from all these components,
  6649. * refer to Vol. 1, "Operand Addressing".
  6650. */
  6651. int scaling = vmx_instruction_info & 3;
  6652. int addr_size = (vmx_instruction_info >> 7) & 7;
  6653. bool is_reg = vmx_instruction_info & (1u << 10);
  6654. int seg_reg = (vmx_instruction_info >> 15) & 7;
  6655. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  6656. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  6657. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  6658. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  6659. if (is_reg) {
  6660. kvm_queue_exception(vcpu, UD_VECTOR);
  6661. return 1;
  6662. }
  6663. /* Addr = segment_base + offset */
  6664. /* offset = base + [index * scale] + displacement */
  6665. off = exit_qualification; /* holds the displacement */
  6666. if (base_is_valid)
  6667. off += kvm_register_read(vcpu, base_reg);
  6668. if (index_is_valid)
  6669. off += kvm_register_read(vcpu, index_reg)<<scaling;
  6670. vmx_get_segment(vcpu, &s, seg_reg);
  6671. *ret = s.base + off;
  6672. if (addr_size == 1) /* 32 bit */
  6673. *ret &= 0xffffffff;
  6674. /* Checks for #GP/#SS exceptions. */
  6675. exn = false;
  6676. if (is_long_mode(vcpu)) {
  6677. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  6678. * non-canonical form. This is the only check on the memory
  6679. * destination for long mode!
  6680. */
  6681. exn = is_noncanonical_address(*ret, vcpu);
  6682. } else if (is_protmode(vcpu)) {
  6683. /* Protected mode: apply checks for segment validity in the
  6684. * following order:
  6685. * - segment type check (#GP(0) may be thrown)
  6686. * - usability check (#GP(0)/#SS(0))
  6687. * - limit check (#GP(0)/#SS(0))
  6688. */
  6689. if (wr)
  6690. /* #GP(0) if the destination operand is located in a
  6691. * read-only data segment or any code segment.
  6692. */
  6693. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  6694. else
  6695. /* #GP(0) if the source operand is located in an
  6696. * execute-only code segment
  6697. */
  6698. exn = ((s.type & 0xa) == 8);
  6699. if (exn) {
  6700. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  6701. return 1;
  6702. }
  6703. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  6704. */
  6705. exn = (s.unusable != 0);
  6706. /* Protected mode: #GP(0)/#SS(0) if the memory
  6707. * operand is outside the segment limit.
  6708. */
  6709. exn = exn || (off + sizeof(u64) > s.limit);
  6710. }
  6711. if (exn) {
  6712. kvm_queue_exception_e(vcpu,
  6713. seg_reg == VCPU_SREG_SS ?
  6714. SS_VECTOR : GP_VECTOR,
  6715. 0);
  6716. return 1;
  6717. }
  6718. return 0;
  6719. }
  6720. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  6721. {
  6722. gva_t gva;
  6723. struct x86_exception e;
  6724. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6725. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  6726. return 1;
  6727. if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
  6728. kvm_inject_page_fault(vcpu, &e);
  6729. return 1;
  6730. }
  6731. return 0;
  6732. }
  6733. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  6734. {
  6735. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6736. struct vmcs *shadow_vmcs;
  6737. int r;
  6738. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  6739. if (r < 0)
  6740. goto out_vmcs02;
  6741. vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  6742. if (!vmx->nested.cached_vmcs12)
  6743. goto out_cached_vmcs12;
  6744. if (enable_shadow_vmcs) {
  6745. shadow_vmcs = alloc_vmcs();
  6746. if (!shadow_vmcs)
  6747. goto out_shadow_vmcs;
  6748. /* mark vmcs as shadow */
  6749. shadow_vmcs->revision_id |= (1u << 31);
  6750. /* init shadow vmcs */
  6751. vmcs_clear(shadow_vmcs);
  6752. vmx->vmcs01.shadow_vmcs = shadow_vmcs;
  6753. }
  6754. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  6755. HRTIMER_MODE_REL_PINNED);
  6756. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  6757. vmx->nested.vmxon = true;
  6758. return 0;
  6759. out_shadow_vmcs:
  6760. kfree(vmx->nested.cached_vmcs12);
  6761. out_cached_vmcs12:
  6762. free_loaded_vmcs(&vmx->nested.vmcs02);
  6763. out_vmcs02:
  6764. return -ENOMEM;
  6765. }
  6766. /*
  6767. * Emulate the VMXON instruction.
  6768. * Currently, we just remember that VMX is active, and do not save or even
  6769. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  6770. * do not currently need to store anything in that guest-allocated memory
  6771. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  6772. * argument is different from the VMXON pointer (which the spec says they do).
  6773. */
  6774. static int handle_vmon(struct kvm_vcpu *vcpu)
  6775. {
  6776. int ret;
  6777. gpa_t vmptr;
  6778. struct page *page;
  6779. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6780. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  6781. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  6782. /*
  6783. * The Intel VMX Instruction Reference lists a bunch of bits that are
  6784. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  6785. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  6786. * Otherwise, we should fail with #UD. But most faulting conditions
  6787. * have already been checked by hardware, prior to the VM-exit for
  6788. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  6789. * that bit set to 1 in non-root mode.
  6790. */
  6791. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  6792. kvm_queue_exception(vcpu, UD_VECTOR);
  6793. return 1;
  6794. }
  6795. /* CPL=0 must be checked manually. */
  6796. if (vmx_get_cpl(vcpu)) {
  6797. kvm_queue_exception(vcpu, UD_VECTOR);
  6798. return 1;
  6799. }
  6800. if (vmx->nested.vmxon) {
  6801. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  6802. return kvm_skip_emulated_instruction(vcpu);
  6803. }
  6804. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  6805. != VMXON_NEEDED_FEATURES) {
  6806. kvm_inject_gp(vcpu, 0);
  6807. return 1;
  6808. }
  6809. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6810. return 1;
  6811. /*
  6812. * SDM 3: 24.11.5
  6813. * The first 4 bytes of VMXON region contain the supported
  6814. * VMCS revision identifier
  6815. *
  6816. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  6817. * which replaces physical address width with 32
  6818. */
  6819. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6820. nested_vmx_failInvalid(vcpu);
  6821. return kvm_skip_emulated_instruction(vcpu);
  6822. }
  6823. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  6824. if (is_error_page(page)) {
  6825. nested_vmx_failInvalid(vcpu);
  6826. return kvm_skip_emulated_instruction(vcpu);
  6827. }
  6828. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  6829. kunmap(page);
  6830. kvm_release_page_clean(page);
  6831. nested_vmx_failInvalid(vcpu);
  6832. return kvm_skip_emulated_instruction(vcpu);
  6833. }
  6834. kunmap(page);
  6835. kvm_release_page_clean(page);
  6836. vmx->nested.vmxon_ptr = vmptr;
  6837. ret = enter_vmx_operation(vcpu);
  6838. if (ret)
  6839. return ret;
  6840. nested_vmx_succeed(vcpu);
  6841. return kvm_skip_emulated_instruction(vcpu);
  6842. }
  6843. /*
  6844. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  6845. * for running VMX instructions (except VMXON, whose prerequisites are
  6846. * slightly different). It also specifies what exception to inject otherwise.
  6847. * Note that many of these exceptions have priority over VM exits, so they
  6848. * don't have to be checked again here.
  6849. */
  6850. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  6851. {
  6852. if (vmx_get_cpl(vcpu)) {
  6853. kvm_queue_exception(vcpu, UD_VECTOR);
  6854. return 0;
  6855. }
  6856. if (!to_vmx(vcpu)->nested.vmxon) {
  6857. kvm_queue_exception(vcpu, UD_VECTOR);
  6858. return 0;
  6859. }
  6860. return 1;
  6861. }
  6862. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  6863. {
  6864. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  6865. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  6866. }
  6867. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  6868. {
  6869. if (vmx->nested.current_vmptr == -1ull)
  6870. return;
  6871. if (enable_shadow_vmcs) {
  6872. /* copy to memory all shadowed fields in case
  6873. they were modified */
  6874. copy_shadow_to_vmcs12(vmx);
  6875. vmx->nested.sync_shadow_vmcs = false;
  6876. vmx_disable_shadow_vmcs(vmx);
  6877. }
  6878. vmx->nested.posted_intr_nv = -1;
  6879. /* Flush VMCS12 to guest memory */
  6880. kvm_vcpu_write_guest_page(&vmx->vcpu,
  6881. vmx->nested.current_vmptr >> PAGE_SHIFT,
  6882. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  6883. vmx->nested.current_vmptr = -1ull;
  6884. }
  6885. /*
  6886. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  6887. * just stops using VMX.
  6888. */
  6889. static void free_nested(struct vcpu_vmx *vmx)
  6890. {
  6891. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  6892. return;
  6893. vmx->nested.vmxon = false;
  6894. vmx->nested.smm.vmxon = false;
  6895. free_vpid(vmx->nested.vpid02);
  6896. vmx->nested.posted_intr_nv = -1;
  6897. vmx->nested.current_vmptr = -1ull;
  6898. if (enable_shadow_vmcs) {
  6899. vmx_disable_shadow_vmcs(vmx);
  6900. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  6901. free_vmcs(vmx->vmcs01.shadow_vmcs);
  6902. vmx->vmcs01.shadow_vmcs = NULL;
  6903. }
  6904. kfree(vmx->nested.cached_vmcs12);
  6905. /* Unpin physical memory we referred to in the vmcs02 */
  6906. if (vmx->nested.apic_access_page) {
  6907. kvm_release_page_dirty(vmx->nested.apic_access_page);
  6908. vmx->nested.apic_access_page = NULL;
  6909. }
  6910. if (vmx->nested.virtual_apic_page) {
  6911. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  6912. vmx->nested.virtual_apic_page = NULL;
  6913. }
  6914. if (vmx->nested.pi_desc_page) {
  6915. kunmap(vmx->nested.pi_desc_page);
  6916. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  6917. vmx->nested.pi_desc_page = NULL;
  6918. vmx->nested.pi_desc = NULL;
  6919. }
  6920. free_loaded_vmcs(&vmx->nested.vmcs02);
  6921. }
  6922. /* Emulate the VMXOFF instruction */
  6923. static int handle_vmoff(struct kvm_vcpu *vcpu)
  6924. {
  6925. if (!nested_vmx_check_permission(vcpu))
  6926. return 1;
  6927. free_nested(to_vmx(vcpu));
  6928. nested_vmx_succeed(vcpu);
  6929. return kvm_skip_emulated_instruction(vcpu);
  6930. }
  6931. /* Emulate the VMCLEAR instruction */
  6932. static int handle_vmclear(struct kvm_vcpu *vcpu)
  6933. {
  6934. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6935. u32 zero = 0;
  6936. gpa_t vmptr;
  6937. if (!nested_vmx_check_permission(vcpu))
  6938. return 1;
  6939. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6940. return 1;
  6941. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6942. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  6943. return kvm_skip_emulated_instruction(vcpu);
  6944. }
  6945. if (vmptr == vmx->nested.vmxon_ptr) {
  6946. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
  6947. return kvm_skip_emulated_instruction(vcpu);
  6948. }
  6949. if (vmptr == vmx->nested.current_vmptr)
  6950. nested_release_vmcs12(vmx);
  6951. kvm_vcpu_write_guest(vcpu,
  6952. vmptr + offsetof(struct vmcs12, launch_state),
  6953. &zero, sizeof(zero));
  6954. nested_vmx_succeed(vcpu);
  6955. return kvm_skip_emulated_instruction(vcpu);
  6956. }
  6957. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  6958. /* Emulate the VMLAUNCH instruction */
  6959. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  6960. {
  6961. return nested_vmx_run(vcpu, true);
  6962. }
  6963. /* Emulate the VMRESUME instruction */
  6964. static int handle_vmresume(struct kvm_vcpu *vcpu)
  6965. {
  6966. return nested_vmx_run(vcpu, false);
  6967. }
  6968. /*
  6969. * Read a vmcs12 field. Since these can have varying lengths and we return
  6970. * one type, we chose the biggest type (u64) and zero-extend the return value
  6971. * to that size. Note that the caller, handle_vmread, might need to use only
  6972. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  6973. * 64-bit fields are to be returned).
  6974. */
  6975. static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
  6976. unsigned long field, u64 *ret)
  6977. {
  6978. short offset = vmcs_field_to_offset(field);
  6979. char *p;
  6980. if (offset < 0)
  6981. return offset;
  6982. p = ((char *)(get_vmcs12(vcpu))) + offset;
  6983. switch (vmcs_field_width(field)) {
  6984. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  6985. *ret = *((natural_width *)p);
  6986. return 0;
  6987. case VMCS_FIELD_WIDTH_U16:
  6988. *ret = *((u16 *)p);
  6989. return 0;
  6990. case VMCS_FIELD_WIDTH_U32:
  6991. *ret = *((u32 *)p);
  6992. return 0;
  6993. case VMCS_FIELD_WIDTH_U64:
  6994. *ret = *((u64 *)p);
  6995. return 0;
  6996. default:
  6997. WARN_ON(1);
  6998. return -ENOENT;
  6999. }
  7000. }
  7001. static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
  7002. unsigned long field, u64 field_value){
  7003. short offset = vmcs_field_to_offset(field);
  7004. char *p = ((char *) get_vmcs12(vcpu)) + offset;
  7005. if (offset < 0)
  7006. return offset;
  7007. switch (vmcs_field_width(field)) {
  7008. case VMCS_FIELD_WIDTH_U16:
  7009. *(u16 *)p = field_value;
  7010. return 0;
  7011. case VMCS_FIELD_WIDTH_U32:
  7012. *(u32 *)p = field_value;
  7013. return 0;
  7014. case VMCS_FIELD_WIDTH_U64:
  7015. *(u64 *)p = field_value;
  7016. return 0;
  7017. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7018. *(natural_width *)p = field_value;
  7019. return 0;
  7020. default:
  7021. WARN_ON(1);
  7022. return -ENOENT;
  7023. }
  7024. }
  7025. /*
  7026. * Copy the writable VMCS shadow fields back to the VMCS12, in case
  7027. * they have been modified by the L1 guest. Note that the "read-only"
  7028. * VM-exit information fields are actually writable if the vCPU is
  7029. * configured to support "VMWRITE to any supported field in the VMCS."
  7030. */
  7031. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  7032. {
  7033. const u16 *fields[] = {
  7034. shadow_read_write_fields,
  7035. shadow_read_only_fields
  7036. };
  7037. const int max_fields[] = {
  7038. max_shadow_read_write_fields,
  7039. max_shadow_read_only_fields
  7040. };
  7041. int i, q;
  7042. unsigned long field;
  7043. u64 field_value;
  7044. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7045. preempt_disable();
  7046. vmcs_load(shadow_vmcs);
  7047. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7048. for (i = 0; i < max_fields[q]; i++) {
  7049. field = fields[q][i];
  7050. field_value = __vmcs_readl(field);
  7051. vmcs12_write_any(&vmx->vcpu, field, field_value);
  7052. }
  7053. /*
  7054. * Skip the VM-exit information fields if they are read-only.
  7055. */
  7056. if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  7057. break;
  7058. }
  7059. vmcs_clear(shadow_vmcs);
  7060. vmcs_load(vmx->loaded_vmcs->vmcs);
  7061. preempt_enable();
  7062. }
  7063. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  7064. {
  7065. const u16 *fields[] = {
  7066. shadow_read_write_fields,
  7067. shadow_read_only_fields
  7068. };
  7069. const int max_fields[] = {
  7070. max_shadow_read_write_fields,
  7071. max_shadow_read_only_fields
  7072. };
  7073. int i, q;
  7074. unsigned long field;
  7075. u64 field_value = 0;
  7076. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7077. vmcs_load(shadow_vmcs);
  7078. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7079. for (i = 0; i < max_fields[q]; i++) {
  7080. field = fields[q][i];
  7081. vmcs12_read_any(&vmx->vcpu, field, &field_value);
  7082. __vmcs_writel(field, field_value);
  7083. }
  7084. }
  7085. vmcs_clear(shadow_vmcs);
  7086. vmcs_load(vmx->loaded_vmcs->vmcs);
  7087. }
  7088. /*
  7089. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  7090. * used before) all generate the same failure when it is missing.
  7091. */
  7092. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  7093. {
  7094. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7095. if (vmx->nested.current_vmptr == -1ull) {
  7096. nested_vmx_failInvalid(vcpu);
  7097. return 0;
  7098. }
  7099. return 1;
  7100. }
  7101. static int handle_vmread(struct kvm_vcpu *vcpu)
  7102. {
  7103. unsigned long field;
  7104. u64 field_value;
  7105. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7106. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7107. gva_t gva = 0;
  7108. if (!nested_vmx_check_permission(vcpu))
  7109. return 1;
  7110. if (!nested_vmx_check_vmcs12(vcpu))
  7111. return kvm_skip_emulated_instruction(vcpu);
  7112. /* Decode instruction info and find the field to read */
  7113. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7114. /* Read the field, zero-extended to a u64 field_value */
  7115. if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
  7116. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7117. return kvm_skip_emulated_instruction(vcpu);
  7118. }
  7119. /*
  7120. * Now copy part of this value to register or memory, as requested.
  7121. * Note that the number of bits actually copied is 32 or 64 depending
  7122. * on the guest's mode (32 or 64 bit), not on the given field's length.
  7123. */
  7124. if (vmx_instruction_info & (1u << 10)) {
  7125. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  7126. field_value);
  7127. } else {
  7128. if (get_vmx_mem_address(vcpu, exit_qualification,
  7129. vmx_instruction_info, true, &gva))
  7130. return 1;
  7131. /* _system ok, nested_vmx_check_permission has verified cpl=0 */
  7132. kvm_write_guest_virt_system(vcpu, gva, &field_value,
  7133. (is_long_mode(vcpu) ? 8 : 4), NULL);
  7134. }
  7135. nested_vmx_succeed(vcpu);
  7136. return kvm_skip_emulated_instruction(vcpu);
  7137. }
  7138. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  7139. {
  7140. unsigned long field;
  7141. gva_t gva;
  7142. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7143. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7144. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7145. /* The value to write might be 32 or 64 bits, depending on L1's long
  7146. * mode, and eventually we need to write that into a field of several
  7147. * possible lengths. The code below first zero-extends the value to 64
  7148. * bit (field_value), and then copies only the appropriate number of
  7149. * bits into the vmcs12 field.
  7150. */
  7151. u64 field_value = 0;
  7152. struct x86_exception e;
  7153. if (!nested_vmx_check_permission(vcpu))
  7154. return 1;
  7155. if (!nested_vmx_check_vmcs12(vcpu))
  7156. return kvm_skip_emulated_instruction(vcpu);
  7157. if (vmx_instruction_info & (1u << 10))
  7158. field_value = kvm_register_readl(vcpu,
  7159. (((vmx_instruction_info) >> 3) & 0xf));
  7160. else {
  7161. if (get_vmx_mem_address(vcpu, exit_qualification,
  7162. vmx_instruction_info, false, &gva))
  7163. return 1;
  7164. if (kvm_read_guest_virt(vcpu, gva, &field_value,
  7165. (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  7166. kvm_inject_page_fault(vcpu, &e);
  7167. return 1;
  7168. }
  7169. }
  7170. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7171. /*
  7172. * If the vCPU supports "VMWRITE to any supported field in the
  7173. * VMCS," then the "read-only" fields are actually read/write.
  7174. */
  7175. if (vmcs_field_readonly(field) &&
  7176. !nested_cpu_has_vmwrite_any_field(vcpu)) {
  7177. nested_vmx_failValid(vcpu,
  7178. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  7179. return kvm_skip_emulated_instruction(vcpu);
  7180. }
  7181. if (vmcs12_write_any(vcpu, field, field_value) < 0) {
  7182. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7183. return kvm_skip_emulated_instruction(vcpu);
  7184. }
  7185. switch (field) {
  7186. #define SHADOW_FIELD_RW(x) case x:
  7187. #include "vmx_shadow_fields.h"
  7188. /*
  7189. * The fields that can be updated by L1 without a vmexit are
  7190. * always updated in the vmcs02, the others go down the slow
  7191. * path of prepare_vmcs02.
  7192. */
  7193. break;
  7194. default:
  7195. vmx->nested.dirty_vmcs12 = true;
  7196. break;
  7197. }
  7198. nested_vmx_succeed(vcpu);
  7199. return kvm_skip_emulated_instruction(vcpu);
  7200. }
  7201. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  7202. {
  7203. vmx->nested.current_vmptr = vmptr;
  7204. if (enable_shadow_vmcs) {
  7205. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  7206. SECONDARY_EXEC_SHADOW_VMCS);
  7207. vmcs_write64(VMCS_LINK_POINTER,
  7208. __pa(vmx->vmcs01.shadow_vmcs));
  7209. vmx->nested.sync_shadow_vmcs = true;
  7210. }
  7211. vmx->nested.dirty_vmcs12 = true;
  7212. }
  7213. /* Emulate the VMPTRLD instruction */
  7214. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  7215. {
  7216. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7217. gpa_t vmptr;
  7218. if (!nested_vmx_check_permission(vcpu))
  7219. return 1;
  7220. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7221. return 1;
  7222. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7223. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  7224. return kvm_skip_emulated_instruction(vcpu);
  7225. }
  7226. if (vmptr == vmx->nested.vmxon_ptr) {
  7227. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
  7228. return kvm_skip_emulated_instruction(vcpu);
  7229. }
  7230. if (vmx->nested.current_vmptr != vmptr) {
  7231. struct vmcs12 *new_vmcs12;
  7232. struct page *page;
  7233. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7234. if (is_error_page(page)) {
  7235. nested_vmx_failInvalid(vcpu);
  7236. return kvm_skip_emulated_instruction(vcpu);
  7237. }
  7238. new_vmcs12 = kmap(page);
  7239. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  7240. kunmap(page);
  7241. kvm_release_page_clean(page);
  7242. nested_vmx_failValid(vcpu,
  7243. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  7244. return kvm_skip_emulated_instruction(vcpu);
  7245. }
  7246. nested_release_vmcs12(vmx);
  7247. /*
  7248. * Load VMCS12 from guest memory since it is not already
  7249. * cached.
  7250. */
  7251. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  7252. kunmap(page);
  7253. kvm_release_page_clean(page);
  7254. set_current_vmptr(vmx, vmptr);
  7255. }
  7256. nested_vmx_succeed(vcpu);
  7257. return kvm_skip_emulated_instruction(vcpu);
  7258. }
  7259. /* Emulate the VMPTRST instruction */
  7260. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  7261. {
  7262. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7263. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7264. gva_t vmcs_gva;
  7265. struct x86_exception e;
  7266. if (!nested_vmx_check_permission(vcpu))
  7267. return 1;
  7268. if (get_vmx_mem_address(vcpu, exit_qualification,
  7269. vmx_instruction_info, true, &vmcs_gva))
  7270. return 1;
  7271. /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
  7272. if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
  7273. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  7274. sizeof(u64), &e)) {
  7275. kvm_inject_page_fault(vcpu, &e);
  7276. return 1;
  7277. }
  7278. nested_vmx_succeed(vcpu);
  7279. return kvm_skip_emulated_instruction(vcpu);
  7280. }
  7281. /* Emulate the INVEPT instruction */
  7282. static int handle_invept(struct kvm_vcpu *vcpu)
  7283. {
  7284. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7285. u32 vmx_instruction_info, types;
  7286. unsigned long type;
  7287. gva_t gva;
  7288. struct x86_exception e;
  7289. struct {
  7290. u64 eptp, gpa;
  7291. } operand;
  7292. if (!(vmx->nested.msrs.secondary_ctls_high &
  7293. SECONDARY_EXEC_ENABLE_EPT) ||
  7294. !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
  7295. kvm_queue_exception(vcpu, UD_VECTOR);
  7296. return 1;
  7297. }
  7298. if (!nested_vmx_check_permission(vcpu))
  7299. return 1;
  7300. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7301. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7302. types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  7303. if (type >= 32 || !(types & (1 << type))) {
  7304. nested_vmx_failValid(vcpu,
  7305. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7306. return kvm_skip_emulated_instruction(vcpu);
  7307. }
  7308. /* According to the Intel VMX instruction reference, the memory
  7309. * operand is read even if it isn't needed (e.g., for type==global)
  7310. */
  7311. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7312. vmx_instruction_info, false, &gva))
  7313. return 1;
  7314. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7315. kvm_inject_page_fault(vcpu, &e);
  7316. return 1;
  7317. }
  7318. switch (type) {
  7319. case VMX_EPT_EXTENT_GLOBAL:
  7320. /*
  7321. * TODO: track mappings and invalidate
  7322. * single context requests appropriately
  7323. */
  7324. case VMX_EPT_EXTENT_CONTEXT:
  7325. kvm_mmu_sync_roots(vcpu);
  7326. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7327. nested_vmx_succeed(vcpu);
  7328. break;
  7329. default:
  7330. BUG_ON(1);
  7331. break;
  7332. }
  7333. return kvm_skip_emulated_instruction(vcpu);
  7334. }
  7335. static int handle_invvpid(struct kvm_vcpu *vcpu)
  7336. {
  7337. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7338. u32 vmx_instruction_info;
  7339. unsigned long type, types;
  7340. gva_t gva;
  7341. struct x86_exception e;
  7342. struct {
  7343. u64 vpid;
  7344. u64 gla;
  7345. } operand;
  7346. if (!(vmx->nested.msrs.secondary_ctls_high &
  7347. SECONDARY_EXEC_ENABLE_VPID) ||
  7348. !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
  7349. kvm_queue_exception(vcpu, UD_VECTOR);
  7350. return 1;
  7351. }
  7352. if (!nested_vmx_check_permission(vcpu))
  7353. return 1;
  7354. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7355. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7356. types = (vmx->nested.msrs.vpid_caps &
  7357. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  7358. if (type >= 32 || !(types & (1 << type))) {
  7359. nested_vmx_failValid(vcpu,
  7360. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7361. return kvm_skip_emulated_instruction(vcpu);
  7362. }
  7363. /* according to the intel vmx instruction reference, the memory
  7364. * operand is read even if it isn't needed (e.g., for type==global)
  7365. */
  7366. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7367. vmx_instruction_info, false, &gva))
  7368. return 1;
  7369. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7370. kvm_inject_page_fault(vcpu, &e);
  7371. return 1;
  7372. }
  7373. if (operand.vpid >> 16) {
  7374. nested_vmx_failValid(vcpu,
  7375. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7376. return kvm_skip_emulated_instruction(vcpu);
  7377. }
  7378. switch (type) {
  7379. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  7380. if (!operand.vpid ||
  7381. is_noncanonical_address(operand.gla, vcpu)) {
  7382. nested_vmx_failValid(vcpu,
  7383. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7384. return kvm_skip_emulated_instruction(vcpu);
  7385. }
  7386. if (cpu_has_vmx_invvpid_individual_addr() &&
  7387. vmx->nested.vpid02) {
  7388. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
  7389. vmx->nested.vpid02, operand.gla);
  7390. } else
  7391. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7392. break;
  7393. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  7394. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  7395. if (!operand.vpid) {
  7396. nested_vmx_failValid(vcpu,
  7397. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7398. return kvm_skip_emulated_instruction(vcpu);
  7399. }
  7400. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7401. break;
  7402. case VMX_VPID_EXTENT_ALL_CONTEXT:
  7403. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7404. break;
  7405. default:
  7406. WARN_ON_ONCE(1);
  7407. return kvm_skip_emulated_instruction(vcpu);
  7408. }
  7409. nested_vmx_succeed(vcpu);
  7410. return kvm_skip_emulated_instruction(vcpu);
  7411. }
  7412. static int handle_pml_full(struct kvm_vcpu *vcpu)
  7413. {
  7414. unsigned long exit_qualification;
  7415. trace_kvm_pml_full(vcpu->vcpu_id);
  7416. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7417. /*
  7418. * PML buffer FULL happened while executing iret from NMI,
  7419. * "blocked by NMI" bit has to be set before next VM entry.
  7420. */
  7421. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  7422. enable_vnmi &&
  7423. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  7424. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  7425. GUEST_INTR_STATE_NMI);
  7426. /*
  7427. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  7428. * here.., and there's no userspace involvement needed for PML.
  7429. */
  7430. return 1;
  7431. }
  7432. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  7433. {
  7434. kvm_lapic_expired_hv_timer(vcpu);
  7435. return 1;
  7436. }
  7437. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  7438. {
  7439. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7440. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  7441. /* Check for memory type validity */
  7442. switch (address & VMX_EPTP_MT_MASK) {
  7443. case VMX_EPTP_MT_UC:
  7444. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
  7445. return false;
  7446. break;
  7447. case VMX_EPTP_MT_WB:
  7448. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
  7449. return false;
  7450. break;
  7451. default:
  7452. return false;
  7453. }
  7454. /* only 4 levels page-walk length are valid */
  7455. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  7456. return false;
  7457. /* Reserved bits should not be set */
  7458. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  7459. return false;
  7460. /* AD, if set, should be supported */
  7461. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  7462. if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
  7463. return false;
  7464. }
  7465. return true;
  7466. }
  7467. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  7468. struct vmcs12 *vmcs12)
  7469. {
  7470. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  7471. u64 address;
  7472. bool accessed_dirty;
  7473. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  7474. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  7475. !nested_cpu_has_ept(vmcs12))
  7476. return 1;
  7477. if (index >= VMFUNC_EPTP_ENTRIES)
  7478. return 1;
  7479. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  7480. &address, index * 8, 8))
  7481. return 1;
  7482. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  7483. /*
  7484. * If the (L2) guest does a vmfunc to the currently
  7485. * active ept pointer, we don't have to do anything else
  7486. */
  7487. if (vmcs12->ept_pointer != address) {
  7488. if (!valid_ept_address(vcpu, address))
  7489. return 1;
  7490. kvm_mmu_unload(vcpu);
  7491. mmu->ept_ad = accessed_dirty;
  7492. mmu->base_role.ad_disabled = !accessed_dirty;
  7493. vmcs12->ept_pointer = address;
  7494. /*
  7495. * TODO: Check what's the correct approach in case
  7496. * mmu reload fails. Currently, we just let the next
  7497. * reload potentially fail
  7498. */
  7499. kvm_mmu_reload(vcpu);
  7500. }
  7501. return 0;
  7502. }
  7503. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  7504. {
  7505. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7506. struct vmcs12 *vmcs12;
  7507. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  7508. /*
  7509. * VMFUNC is only supported for nested guests, but we always enable the
  7510. * secondary control for simplicity; for non-nested mode, fake that we
  7511. * didn't by injecting #UD.
  7512. */
  7513. if (!is_guest_mode(vcpu)) {
  7514. kvm_queue_exception(vcpu, UD_VECTOR);
  7515. return 1;
  7516. }
  7517. vmcs12 = get_vmcs12(vcpu);
  7518. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  7519. goto fail;
  7520. switch (function) {
  7521. case 0:
  7522. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  7523. goto fail;
  7524. break;
  7525. default:
  7526. goto fail;
  7527. }
  7528. return kvm_skip_emulated_instruction(vcpu);
  7529. fail:
  7530. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  7531. vmcs_read32(VM_EXIT_INTR_INFO),
  7532. vmcs_readl(EXIT_QUALIFICATION));
  7533. return 1;
  7534. }
  7535. /*
  7536. * The exit handlers return 1 if the exit was handled fully and guest execution
  7537. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  7538. * to be done to userspace and return 0.
  7539. */
  7540. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  7541. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  7542. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  7543. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  7544. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  7545. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  7546. [EXIT_REASON_CR_ACCESS] = handle_cr,
  7547. [EXIT_REASON_DR_ACCESS] = handle_dr,
  7548. [EXIT_REASON_CPUID] = handle_cpuid,
  7549. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  7550. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  7551. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  7552. [EXIT_REASON_HLT] = handle_halt,
  7553. [EXIT_REASON_INVD] = handle_invd,
  7554. [EXIT_REASON_INVLPG] = handle_invlpg,
  7555. [EXIT_REASON_RDPMC] = handle_rdpmc,
  7556. [EXIT_REASON_VMCALL] = handle_vmcall,
  7557. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  7558. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  7559. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  7560. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  7561. [EXIT_REASON_VMREAD] = handle_vmread,
  7562. [EXIT_REASON_VMRESUME] = handle_vmresume,
  7563. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  7564. [EXIT_REASON_VMOFF] = handle_vmoff,
  7565. [EXIT_REASON_VMON] = handle_vmon,
  7566. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  7567. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  7568. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  7569. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  7570. [EXIT_REASON_WBINVD] = handle_wbinvd,
  7571. [EXIT_REASON_XSETBV] = handle_xsetbv,
  7572. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  7573. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  7574. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  7575. [EXIT_REASON_LDTR_TR] = handle_desc,
  7576. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  7577. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  7578. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  7579. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  7580. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  7581. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  7582. [EXIT_REASON_INVEPT] = handle_invept,
  7583. [EXIT_REASON_INVVPID] = handle_invvpid,
  7584. [EXIT_REASON_RDRAND] = handle_invalid_op,
  7585. [EXIT_REASON_RDSEED] = handle_invalid_op,
  7586. [EXIT_REASON_XSAVES] = handle_xsaves,
  7587. [EXIT_REASON_XRSTORS] = handle_xrstors,
  7588. [EXIT_REASON_PML_FULL] = handle_pml_full,
  7589. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  7590. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  7591. };
  7592. static const int kvm_vmx_max_exit_handlers =
  7593. ARRAY_SIZE(kvm_vmx_exit_handlers);
  7594. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  7595. struct vmcs12 *vmcs12)
  7596. {
  7597. unsigned long exit_qualification;
  7598. gpa_t bitmap, last_bitmap;
  7599. unsigned int port;
  7600. int size;
  7601. u8 b;
  7602. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  7603. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  7604. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7605. port = exit_qualification >> 16;
  7606. size = (exit_qualification & 7) + 1;
  7607. last_bitmap = (gpa_t)-1;
  7608. b = -1;
  7609. while (size > 0) {
  7610. if (port < 0x8000)
  7611. bitmap = vmcs12->io_bitmap_a;
  7612. else if (port < 0x10000)
  7613. bitmap = vmcs12->io_bitmap_b;
  7614. else
  7615. return true;
  7616. bitmap += (port & 0x7fff) / 8;
  7617. if (last_bitmap != bitmap)
  7618. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  7619. return true;
  7620. if (b & (1 << (port & 7)))
  7621. return true;
  7622. port++;
  7623. size--;
  7624. last_bitmap = bitmap;
  7625. }
  7626. return false;
  7627. }
  7628. /*
  7629. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  7630. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  7631. * disinterest in the current event (read or write a specific MSR) by using an
  7632. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  7633. */
  7634. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  7635. struct vmcs12 *vmcs12, u32 exit_reason)
  7636. {
  7637. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  7638. gpa_t bitmap;
  7639. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  7640. return true;
  7641. /*
  7642. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  7643. * for the four combinations of read/write and low/high MSR numbers.
  7644. * First we need to figure out which of the four to use:
  7645. */
  7646. bitmap = vmcs12->msr_bitmap;
  7647. if (exit_reason == EXIT_REASON_MSR_WRITE)
  7648. bitmap += 2048;
  7649. if (msr_index >= 0xc0000000) {
  7650. msr_index -= 0xc0000000;
  7651. bitmap += 1024;
  7652. }
  7653. /* Then read the msr_index'th bit from this bitmap: */
  7654. if (msr_index < 1024*8) {
  7655. unsigned char b;
  7656. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  7657. return true;
  7658. return 1 & (b >> (msr_index & 7));
  7659. } else
  7660. return true; /* let L1 handle the wrong parameter */
  7661. }
  7662. /*
  7663. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  7664. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  7665. * intercept (via guest_host_mask etc.) the current event.
  7666. */
  7667. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  7668. struct vmcs12 *vmcs12)
  7669. {
  7670. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7671. int cr = exit_qualification & 15;
  7672. int reg;
  7673. unsigned long val;
  7674. switch ((exit_qualification >> 4) & 3) {
  7675. case 0: /* mov to cr */
  7676. reg = (exit_qualification >> 8) & 15;
  7677. val = kvm_register_readl(vcpu, reg);
  7678. switch (cr) {
  7679. case 0:
  7680. if (vmcs12->cr0_guest_host_mask &
  7681. (val ^ vmcs12->cr0_read_shadow))
  7682. return true;
  7683. break;
  7684. case 3:
  7685. if ((vmcs12->cr3_target_count >= 1 &&
  7686. vmcs12->cr3_target_value0 == val) ||
  7687. (vmcs12->cr3_target_count >= 2 &&
  7688. vmcs12->cr3_target_value1 == val) ||
  7689. (vmcs12->cr3_target_count >= 3 &&
  7690. vmcs12->cr3_target_value2 == val) ||
  7691. (vmcs12->cr3_target_count >= 4 &&
  7692. vmcs12->cr3_target_value3 == val))
  7693. return false;
  7694. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  7695. return true;
  7696. break;
  7697. case 4:
  7698. if (vmcs12->cr4_guest_host_mask &
  7699. (vmcs12->cr4_read_shadow ^ val))
  7700. return true;
  7701. break;
  7702. case 8:
  7703. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  7704. return true;
  7705. break;
  7706. }
  7707. break;
  7708. case 2: /* clts */
  7709. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  7710. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  7711. return true;
  7712. break;
  7713. case 1: /* mov from cr */
  7714. switch (cr) {
  7715. case 3:
  7716. if (vmcs12->cpu_based_vm_exec_control &
  7717. CPU_BASED_CR3_STORE_EXITING)
  7718. return true;
  7719. break;
  7720. case 8:
  7721. if (vmcs12->cpu_based_vm_exec_control &
  7722. CPU_BASED_CR8_STORE_EXITING)
  7723. return true;
  7724. break;
  7725. }
  7726. break;
  7727. case 3: /* lmsw */
  7728. /*
  7729. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  7730. * cr0. Other attempted changes are ignored, with no exit.
  7731. */
  7732. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  7733. if (vmcs12->cr0_guest_host_mask & 0xe &
  7734. (val ^ vmcs12->cr0_read_shadow))
  7735. return true;
  7736. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  7737. !(vmcs12->cr0_read_shadow & 0x1) &&
  7738. (val & 0x1))
  7739. return true;
  7740. break;
  7741. }
  7742. return false;
  7743. }
  7744. /*
  7745. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  7746. * should handle it ourselves in L0 (and then continue L2). Only call this
  7747. * when in is_guest_mode (L2).
  7748. */
  7749. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  7750. {
  7751. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7752. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7753. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7754. if (vmx->nested.nested_run_pending)
  7755. return false;
  7756. if (unlikely(vmx->fail)) {
  7757. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  7758. vmcs_read32(VM_INSTRUCTION_ERROR));
  7759. return true;
  7760. }
  7761. /*
  7762. * The host physical addresses of some pages of guest memory
  7763. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  7764. * Page). The CPU may write to these pages via their host
  7765. * physical address while L2 is running, bypassing any
  7766. * address-translation-based dirty tracking (e.g. EPT write
  7767. * protection).
  7768. *
  7769. * Mark them dirty on every exit from L2 to prevent them from
  7770. * getting out of sync with dirty tracking.
  7771. */
  7772. nested_mark_vmcs12_pages_dirty(vcpu);
  7773. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  7774. vmcs_readl(EXIT_QUALIFICATION),
  7775. vmx->idt_vectoring_info,
  7776. intr_info,
  7777. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  7778. KVM_ISA_VMX);
  7779. switch (exit_reason) {
  7780. case EXIT_REASON_EXCEPTION_NMI:
  7781. if (is_nmi(intr_info))
  7782. return false;
  7783. else if (is_page_fault(intr_info))
  7784. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  7785. else if (is_no_device(intr_info) &&
  7786. !(vmcs12->guest_cr0 & X86_CR0_TS))
  7787. return false;
  7788. else if (is_debug(intr_info) &&
  7789. vcpu->guest_debug &
  7790. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  7791. return false;
  7792. else if (is_breakpoint(intr_info) &&
  7793. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  7794. return false;
  7795. return vmcs12->exception_bitmap &
  7796. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  7797. case EXIT_REASON_EXTERNAL_INTERRUPT:
  7798. return false;
  7799. case EXIT_REASON_TRIPLE_FAULT:
  7800. return true;
  7801. case EXIT_REASON_PENDING_INTERRUPT:
  7802. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  7803. case EXIT_REASON_NMI_WINDOW:
  7804. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  7805. case EXIT_REASON_TASK_SWITCH:
  7806. return true;
  7807. case EXIT_REASON_CPUID:
  7808. return true;
  7809. case EXIT_REASON_HLT:
  7810. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  7811. case EXIT_REASON_INVD:
  7812. return true;
  7813. case EXIT_REASON_INVLPG:
  7814. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7815. case EXIT_REASON_RDPMC:
  7816. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  7817. case EXIT_REASON_RDRAND:
  7818. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  7819. case EXIT_REASON_RDSEED:
  7820. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  7821. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  7822. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  7823. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  7824. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  7825. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  7826. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  7827. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  7828. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  7829. /*
  7830. * VMX instructions trap unconditionally. This allows L1 to
  7831. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  7832. */
  7833. return true;
  7834. case EXIT_REASON_CR_ACCESS:
  7835. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  7836. case EXIT_REASON_DR_ACCESS:
  7837. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  7838. case EXIT_REASON_IO_INSTRUCTION:
  7839. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  7840. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  7841. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  7842. case EXIT_REASON_MSR_READ:
  7843. case EXIT_REASON_MSR_WRITE:
  7844. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  7845. case EXIT_REASON_INVALID_STATE:
  7846. return true;
  7847. case EXIT_REASON_MWAIT_INSTRUCTION:
  7848. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  7849. case EXIT_REASON_MONITOR_TRAP_FLAG:
  7850. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  7851. case EXIT_REASON_MONITOR_INSTRUCTION:
  7852. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  7853. case EXIT_REASON_PAUSE_INSTRUCTION:
  7854. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  7855. nested_cpu_has2(vmcs12,
  7856. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  7857. case EXIT_REASON_MCE_DURING_VMENTRY:
  7858. return false;
  7859. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  7860. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  7861. case EXIT_REASON_APIC_ACCESS:
  7862. case EXIT_REASON_APIC_WRITE:
  7863. case EXIT_REASON_EOI_INDUCED:
  7864. /*
  7865. * The controls for "virtualize APIC accesses," "APIC-
  7866. * register virtualization," and "virtual-interrupt
  7867. * delivery" only come from vmcs12.
  7868. */
  7869. return true;
  7870. case EXIT_REASON_EPT_VIOLATION:
  7871. /*
  7872. * L0 always deals with the EPT violation. If nested EPT is
  7873. * used, and the nested mmu code discovers that the address is
  7874. * missing in the guest EPT table (EPT12), the EPT violation
  7875. * will be injected with nested_ept_inject_page_fault()
  7876. */
  7877. return false;
  7878. case EXIT_REASON_EPT_MISCONFIG:
  7879. /*
  7880. * L2 never uses directly L1's EPT, but rather L0's own EPT
  7881. * table (shadow on EPT) or a merged EPT table that L0 built
  7882. * (EPT on EPT). So any problems with the structure of the
  7883. * table is L0's fault.
  7884. */
  7885. return false;
  7886. case EXIT_REASON_INVPCID:
  7887. return
  7888. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  7889. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7890. case EXIT_REASON_WBINVD:
  7891. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  7892. case EXIT_REASON_XSETBV:
  7893. return true;
  7894. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  7895. /*
  7896. * This should never happen, since it is not possible to
  7897. * set XSS to a non-zero value---neither in L1 nor in L2.
  7898. * If if it were, XSS would have to be checked against
  7899. * the XSS exit bitmap in vmcs12.
  7900. */
  7901. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  7902. case EXIT_REASON_PREEMPTION_TIMER:
  7903. return false;
  7904. case EXIT_REASON_PML_FULL:
  7905. /* We emulate PML support to L1. */
  7906. return false;
  7907. case EXIT_REASON_VMFUNC:
  7908. /* VM functions are emulated through L2->L0 vmexits. */
  7909. return false;
  7910. default:
  7911. return true;
  7912. }
  7913. }
  7914. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  7915. {
  7916. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7917. /*
  7918. * At this point, the exit interruption info in exit_intr_info
  7919. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  7920. * we need to query the in-kernel LAPIC.
  7921. */
  7922. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  7923. if ((exit_intr_info &
  7924. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  7925. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  7926. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7927. vmcs12->vm_exit_intr_error_code =
  7928. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  7929. }
  7930. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  7931. vmcs_readl(EXIT_QUALIFICATION));
  7932. return 1;
  7933. }
  7934. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  7935. {
  7936. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  7937. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  7938. }
  7939. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  7940. {
  7941. if (vmx->pml_pg) {
  7942. __free_page(vmx->pml_pg);
  7943. vmx->pml_pg = NULL;
  7944. }
  7945. }
  7946. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  7947. {
  7948. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7949. u64 *pml_buf;
  7950. u16 pml_idx;
  7951. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  7952. /* Do nothing if PML buffer is empty */
  7953. if (pml_idx == (PML_ENTITY_NUM - 1))
  7954. return;
  7955. /* PML index always points to next available PML buffer entity */
  7956. if (pml_idx >= PML_ENTITY_NUM)
  7957. pml_idx = 0;
  7958. else
  7959. pml_idx++;
  7960. pml_buf = page_address(vmx->pml_pg);
  7961. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  7962. u64 gpa;
  7963. gpa = pml_buf[pml_idx];
  7964. WARN_ON(gpa & (PAGE_SIZE - 1));
  7965. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  7966. }
  7967. /* reset PML index */
  7968. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  7969. }
  7970. /*
  7971. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  7972. * Called before reporting dirty_bitmap to userspace.
  7973. */
  7974. static void kvm_flush_pml_buffers(struct kvm *kvm)
  7975. {
  7976. int i;
  7977. struct kvm_vcpu *vcpu;
  7978. /*
  7979. * We only need to kick vcpu out of guest mode here, as PML buffer
  7980. * is flushed at beginning of all VMEXITs, and it's obvious that only
  7981. * vcpus running in guest are possible to have unflushed GPAs in PML
  7982. * buffer.
  7983. */
  7984. kvm_for_each_vcpu(i, vcpu, kvm)
  7985. kvm_vcpu_kick(vcpu);
  7986. }
  7987. static void vmx_dump_sel(char *name, uint32_t sel)
  7988. {
  7989. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  7990. name, vmcs_read16(sel),
  7991. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  7992. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  7993. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  7994. }
  7995. static void vmx_dump_dtsel(char *name, uint32_t limit)
  7996. {
  7997. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  7998. name, vmcs_read32(limit),
  7999. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  8000. }
  8001. static void dump_vmcs(void)
  8002. {
  8003. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  8004. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  8005. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  8006. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  8007. u32 secondary_exec_control = 0;
  8008. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  8009. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  8010. int i, n;
  8011. if (cpu_has_secondary_exec_ctrls())
  8012. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8013. pr_err("*** Guest State ***\n");
  8014. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8015. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  8016. vmcs_readl(CR0_GUEST_HOST_MASK));
  8017. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8018. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  8019. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  8020. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  8021. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  8022. {
  8023. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  8024. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  8025. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  8026. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  8027. }
  8028. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  8029. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  8030. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  8031. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  8032. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8033. vmcs_readl(GUEST_SYSENTER_ESP),
  8034. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  8035. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  8036. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  8037. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  8038. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  8039. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  8040. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  8041. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  8042. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  8043. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  8044. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  8045. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  8046. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  8047. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8048. efer, vmcs_read64(GUEST_IA32_PAT));
  8049. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  8050. vmcs_read64(GUEST_IA32_DEBUGCTL),
  8051. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  8052. if (cpu_has_load_perf_global_ctrl &&
  8053. vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  8054. pr_err("PerfGlobCtl = 0x%016llx\n",
  8055. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  8056. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  8057. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  8058. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  8059. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  8060. vmcs_read32(GUEST_ACTIVITY_STATE));
  8061. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  8062. pr_err("InterruptStatus = %04x\n",
  8063. vmcs_read16(GUEST_INTR_STATUS));
  8064. pr_err("*** Host State ***\n");
  8065. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  8066. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  8067. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  8068. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  8069. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  8070. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  8071. vmcs_read16(HOST_TR_SELECTOR));
  8072. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  8073. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  8074. vmcs_readl(HOST_TR_BASE));
  8075. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  8076. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  8077. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  8078. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  8079. vmcs_readl(HOST_CR4));
  8080. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8081. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  8082. vmcs_read32(HOST_IA32_SYSENTER_CS),
  8083. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  8084. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  8085. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8086. vmcs_read64(HOST_IA32_EFER),
  8087. vmcs_read64(HOST_IA32_PAT));
  8088. if (cpu_has_load_perf_global_ctrl &&
  8089. vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8090. pr_err("PerfGlobCtl = 0x%016llx\n",
  8091. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  8092. pr_err("*** Control State ***\n");
  8093. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  8094. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  8095. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  8096. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  8097. vmcs_read32(EXCEPTION_BITMAP),
  8098. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  8099. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  8100. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  8101. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8102. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  8103. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  8104. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  8105. vmcs_read32(VM_EXIT_INTR_INFO),
  8106. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8107. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  8108. pr_err(" reason=%08x qualification=%016lx\n",
  8109. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  8110. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  8111. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  8112. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  8113. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  8114. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  8115. pr_err("TSC Multiplier = 0x%016llx\n",
  8116. vmcs_read64(TSC_MULTIPLIER));
  8117. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  8118. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  8119. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  8120. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  8121. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  8122. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  8123. n = vmcs_read32(CR3_TARGET_COUNT);
  8124. for (i = 0; i + 1 < n; i += 4)
  8125. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  8126. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  8127. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  8128. if (i < n)
  8129. pr_err("CR3 target%u=%016lx\n",
  8130. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  8131. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  8132. pr_err("PLE Gap=%08x Window=%08x\n",
  8133. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  8134. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  8135. pr_err("Virtual processor ID = 0x%04x\n",
  8136. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  8137. }
  8138. /*
  8139. * The guest has exited. See if we can fix it or if we need userspace
  8140. * assistance.
  8141. */
  8142. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  8143. {
  8144. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8145. u32 exit_reason = vmx->exit_reason;
  8146. u32 vectoring_info = vmx->idt_vectoring_info;
  8147. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  8148. /*
  8149. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  8150. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  8151. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  8152. * mode as if vcpus is in root mode, the PML buffer must has been
  8153. * flushed already.
  8154. */
  8155. if (enable_pml)
  8156. vmx_flush_pml_buffer(vcpu);
  8157. /* If guest state is invalid, start emulating */
  8158. if (vmx->emulation_required)
  8159. return handle_invalid_guest_state(vcpu);
  8160. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  8161. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  8162. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  8163. dump_vmcs();
  8164. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8165. vcpu->run->fail_entry.hardware_entry_failure_reason
  8166. = exit_reason;
  8167. return 0;
  8168. }
  8169. if (unlikely(vmx->fail)) {
  8170. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8171. vcpu->run->fail_entry.hardware_entry_failure_reason
  8172. = vmcs_read32(VM_INSTRUCTION_ERROR);
  8173. return 0;
  8174. }
  8175. /*
  8176. * Note:
  8177. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  8178. * delivery event since it indicates guest is accessing MMIO.
  8179. * The vm-exit can be triggered again after return to guest that
  8180. * will cause infinite loop.
  8181. */
  8182. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  8183. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  8184. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  8185. exit_reason != EXIT_REASON_PML_FULL &&
  8186. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  8187. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  8188. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  8189. vcpu->run->internal.ndata = 3;
  8190. vcpu->run->internal.data[0] = vectoring_info;
  8191. vcpu->run->internal.data[1] = exit_reason;
  8192. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  8193. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  8194. vcpu->run->internal.ndata++;
  8195. vcpu->run->internal.data[3] =
  8196. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  8197. }
  8198. return 0;
  8199. }
  8200. if (unlikely(!enable_vnmi &&
  8201. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  8202. if (vmx_interrupt_allowed(vcpu)) {
  8203. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8204. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  8205. vcpu->arch.nmi_pending) {
  8206. /*
  8207. * This CPU don't support us in finding the end of an
  8208. * NMI-blocked window if the guest runs with IRQs
  8209. * disabled. So we pull the trigger after 1 s of
  8210. * futile waiting, but inform the user about this.
  8211. */
  8212. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  8213. "state on VCPU %d after 1 s timeout\n",
  8214. __func__, vcpu->vcpu_id);
  8215. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8216. }
  8217. }
  8218. if (exit_reason < kvm_vmx_max_exit_handlers
  8219. && kvm_vmx_exit_handlers[exit_reason])
  8220. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  8221. else {
  8222. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  8223. exit_reason);
  8224. kvm_queue_exception(vcpu, UD_VECTOR);
  8225. return 1;
  8226. }
  8227. }
  8228. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  8229. {
  8230. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8231. if (is_guest_mode(vcpu) &&
  8232. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8233. return;
  8234. if (irr == -1 || tpr < irr) {
  8235. vmcs_write32(TPR_THRESHOLD, 0);
  8236. return;
  8237. }
  8238. vmcs_write32(TPR_THRESHOLD, irr);
  8239. }
  8240. static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  8241. {
  8242. u32 sec_exec_control;
  8243. if (!lapic_in_kernel(vcpu))
  8244. return;
  8245. /* Postpone execution until vmcs01 is the current VMCS. */
  8246. if (is_guest_mode(vcpu)) {
  8247. to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
  8248. return;
  8249. }
  8250. if (!cpu_need_tpr_shadow(vcpu))
  8251. return;
  8252. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8253. sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8254. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  8255. switch (kvm_get_apic_mode(vcpu)) {
  8256. case LAPIC_MODE_INVALID:
  8257. WARN_ONCE(true, "Invalid local APIC state");
  8258. case LAPIC_MODE_DISABLED:
  8259. break;
  8260. case LAPIC_MODE_XAPIC:
  8261. if (flexpriority_enabled) {
  8262. sec_exec_control |=
  8263. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  8264. vmx_flush_tlb(vcpu, true);
  8265. }
  8266. break;
  8267. case LAPIC_MODE_X2APIC:
  8268. if (cpu_has_vmx_virtualize_x2apic_mode())
  8269. sec_exec_control |=
  8270. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  8271. break;
  8272. }
  8273. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  8274. vmx_update_msr_bitmap(vcpu);
  8275. }
  8276. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  8277. {
  8278. if (!is_guest_mode(vcpu)) {
  8279. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  8280. vmx_flush_tlb(vcpu, true);
  8281. }
  8282. }
  8283. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  8284. {
  8285. u16 status;
  8286. u8 old;
  8287. if (max_isr == -1)
  8288. max_isr = 0;
  8289. status = vmcs_read16(GUEST_INTR_STATUS);
  8290. old = status >> 8;
  8291. if (max_isr != old) {
  8292. status &= 0xff;
  8293. status |= max_isr << 8;
  8294. vmcs_write16(GUEST_INTR_STATUS, status);
  8295. }
  8296. }
  8297. static void vmx_set_rvi(int vector)
  8298. {
  8299. u16 status;
  8300. u8 old;
  8301. if (vector == -1)
  8302. vector = 0;
  8303. status = vmcs_read16(GUEST_INTR_STATUS);
  8304. old = (u8)status & 0xff;
  8305. if ((u8)vector != old) {
  8306. status &= ~0xff;
  8307. status |= (u8)vector;
  8308. vmcs_write16(GUEST_INTR_STATUS, status);
  8309. }
  8310. }
  8311. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  8312. {
  8313. /*
  8314. * When running L2, updating RVI is only relevant when
  8315. * vmcs12 virtual-interrupt-delivery enabled.
  8316. * However, it can be enabled only when L1 also
  8317. * intercepts external-interrupts and in that case
  8318. * we should not update vmcs02 RVI but instead intercept
  8319. * interrupt. Therefore, do nothing when running L2.
  8320. */
  8321. if (!is_guest_mode(vcpu))
  8322. vmx_set_rvi(max_irr);
  8323. }
  8324. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  8325. {
  8326. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8327. int max_irr;
  8328. bool max_irr_updated;
  8329. WARN_ON(!vcpu->arch.apicv_active);
  8330. if (pi_test_on(&vmx->pi_desc)) {
  8331. pi_clear_on(&vmx->pi_desc);
  8332. /*
  8333. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  8334. * But on x86 this is just a compiler barrier anyway.
  8335. */
  8336. smp_mb__after_atomic();
  8337. max_irr_updated =
  8338. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  8339. /*
  8340. * If we are running L2 and L1 has a new pending interrupt
  8341. * which can be injected, we should re-evaluate
  8342. * what should be done with this new L1 interrupt.
  8343. * If L1 intercepts external-interrupts, we should
  8344. * exit from L2 to L1. Otherwise, interrupt should be
  8345. * delivered directly to L2.
  8346. */
  8347. if (is_guest_mode(vcpu) && max_irr_updated) {
  8348. if (nested_exit_on_intr(vcpu))
  8349. kvm_vcpu_exiting_guest_mode(vcpu);
  8350. else
  8351. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8352. }
  8353. } else {
  8354. max_irr = kvm_lapic_find_highest_irr(vcpu);
  8355. }
  8356. vmx_hwapic_irr_update(vcpu, max_irr);
  8357. return max_irr;
  8358. }
  8359. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  8360. {
  8361. if (!kvm_vcpu_apicv_active(vcpu))
  8362. return;
  8363. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  8364. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  8365. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  8366. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  8367. }
  8368. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  8369. {
  8370. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8371. pi_clear_on(&vmx->pi_desc);
  8372. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  8373. }
  8374. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  8375. {
  8376. u32 exit_intr_info = 0;
  8377. u16 basic_exit_reason = (u16)vmx->exit_reason;
  8378. if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  8379. || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
  8380. return;
  8381. if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8382. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8383. vmx->exit_intr_info = exit_intr_info;
  8384. /* if exit due to PF check for async PF */
  8385. if (is_page_fault(exit_intr_info))
  8386. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  8387. /* Handle machine checks before interrupts are enabled */
  8388. if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
  8389. is_machine_check(exit_intr_info))
  8390. kvm_machine_check();
  8391. /* We need to handle NMIs before interrupts are enabled */
  8392. if (is_nmi(exit_intr_info)) {
  8393. kvm_before_interrupt(&vmx->vcpu);
  8394. asm("int $2");
  8395. kvm_after_interrupt(&vmx->vcpu);
  8396. }
  8397. }
  8398. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  8399. {
  8400. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8401. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  8402. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  8403. unsigned int vector;
  8404. unsigned long entry;
  8405. gate_desc *desc;
  8406. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8407. #ifdef CONFIG_X86_64
  8408. unsigned long tmp;
  8409. #endif
  8410. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  8411. desc = (gate_desc *)vmx->host_idt_base + vector;
  8412. entry = gate_offset(desc);
  8413. asm volatile(
  8414. #ifdef CONFIG_X86_64
  8415. "mov %%" _ASM_SP ", %[sp]\n\t"
  8416. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  8417. "push $%c[ss]\n\t"
  8418. "push %[sp]\n\t"
  8419. #endif
  8420. "pushf\n\t"
  8421. __ASM_SIZE(push) " $%c[cs]\n\t"
  8422. CALL_NOSPEC
  8423. :
  8424. #ifdef CONFIG_X86_64
  8425. [sp]"=&r"(tmp),
  8426. #endif
  8427. ASM_CALL_CONSTRAINT
  8428. :
  8429. THUNK_TARGET(entry),
  8430. [ss]"i"(__KERNEL_DS),
  8431. [cs]"i"(__KERNEL_CS)
  8432. );
  8433. }
  8434. }
  8435. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  8436. static bool vmx_has_emulated_msr(int index)
  8437. {
  8438. switch (index) {
  8439. case MSR_IA32_SMBASE:
  8440. /*
  8441. * We cannot do SMM unless we can run the guest in big
  8442. * real mode.
  8443. */
  8444. return enable_unrestricted_guest || emulate_invalid_guest_state;
  8445. case MSR_AMD64_VIRT_SPEC_CTRL:
  8446. /* This is AMD only. */
  8447. return false;
  8448. default:
  8449. return true;
  8450. }
  8451. }
  8452. static bool vmx_mpx_supported(void)
  8453. {
  8454. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  8455. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  8456. }
  8457. static bool vmx_xsaves_supported(void)
  8458. {
  8459. return vmcs_config.cpu_based_2nd_exec_ctrl &
  8460. SECONDARY_EXEC_XSAVES;
  8461. }
  8462. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  8463. {
  8464. u32 exit_intr_info;
  8465. bool unblock_nmi;
  8466. u8 vector;
  8467. bool idtv_info_valid;
  8468. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  8469. if (enable_vnmi) {
  8470. if (vmx->loaded_vmcs->nmi_known_unmasked)
  8471. return;
  8472. /*
  8473. * Can't use vmx->exit_intr_info since we're not sure what
  8474. * the exit reason is.
  8475. */
  8476. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8477. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  8478. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  8479. /*
  8480. * SDM 3: 27.7.1.2 (September 2008)
  8481. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  8482. * a guest IRET fault.
  8483. * SDM 3: 23.2.2 (September 2008)
  8484. * Bit 12 is undefined in any of the following cases:
  8485. * If the VM exit sets the valid bit in the IDT-vectoring
  8486. * information field.
  8487. * If the VM exit is due to a double fault.
  8488. */
  8489. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  8490. vector != DF_VECTOR && !idtv_info_valid)
  8491. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  8492. GUEST_INTR_STATE_NMI);
  8493. else
  8494. vmx->loaded_vmcs->nmi_known_unmasked =
  8495. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  8496. & GUEST_INTR_STATE_NMI);
  8497. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  8498. vmx->loaded_vmcs->vnmi_blocked_time +=
  8499. ktime_to_ns(ktime_sub(ktime_get(),
  8500. vmx->loaded_vmcs->entry_time));
  8501. }
  8502. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  8503. u32 idt_vectoring_info,
  8504. int instr_len_field,
  8505. int error_code_field)
  8506. {
  8507. u8 vector;
  8508. int type;
  8509. bool idtv_info_valid;
  8510. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  8511. vcpu->arch.nmi_injected = false;
  8512. kvm_clear_exception_queue(vcpu);
  8513. kvm_clear_interrupt_queue(vcpu);
  8514. if (!idtv_info_valid)
  8515. return;
  8516. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8517. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  8518. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  8519. switch (type) {
  8520. case INTR_TYPE_NMI_INTR:
  8521. vcpu->arch.nmi_injected = true;
  8522. /*
  8523. * SDM 3: 27.7.1.2 (September 2008)
  8524. * Clear bit "block by NMI" before VM entry if a NMI
  8525. * delivery faulted.
  8526. */
  8527. vmx_set_nmi_mask(vcpu, false);
  8528. break;
  8529. case INTR_TYPE_SOFT_EXCEPTION:
  8530. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8531. /* fall through */
  8532. case INTR_TYPE_HARD_EXCEPTION:
  8533. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  8534. u32 err = vmcs_read32(error_code_field);
  8535. kvm_requeue_exception_e(vcpu, vector, err);
  8536. } else
  8537. kvm_requeue_exception(vcpu, vector);
  8538. break;
  8539. case INTR_TYPE_SOFT_INTR:
  8540. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8541. /* fall through */
  8542. case INTR_TYPE_EXT_INTR:
  8543. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  8544. break;
  8545. default:
  8546. break;
  8547. }
  8548. }
  8549. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  8550. {
  8551. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  8552. VM_EXIT_INSTRUCTION_LEN,
  8553. IDT_VECTORING_ERROR_CODE);
  8554. }
  8555. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  8556. {
  8557. __vmx_complete_interrupts(vcpu,
  8558. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8559. VM_ENTRY_INSTRUCTION_LEN,
  8560. VM_ENTRY_EXCEPTION_ERROR_CODE);
  8561. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  8562. }
  8563. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  8564. {
  8565. int i, nr_msrs;
  8566. struct perf_guest_switch_msr *msrs;
  8567. msrs = perf_guest_get_msrs(&nr_msrs);
  8568. if (!msrs)
  8569. return;
  8570. for (i = 0; i < nr_msrs; i++)
  8571. if (msrs[i].host == msrs[i].guest)
  8572. clear_atomic_switch_msr(vmx, msrs[i].msr);
  8573. else
  8574. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  8575. msrs[i].host);
  8576. }
  8577. static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
  8578. {
  8579. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8580. u64 tscl;
  8581. u32 delta_tsc;
  8582. if (vmx->hv_deadline_tsc == -1)
  8583. return;
  8584. tscl = rdtsc();
  8585. if (vmx->hv_deadline_tsc > tscl)
  8586. /* sure to be 32 bit only because checked on set_hv_timer */
  8587. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  8588. cpu_preemption_timer_multi);
  8589. else
  8590. delta_tsc = 0;
  8591. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
  8592. }
  8593. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  8594. {
  8595. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8596. unsigned long cr3, cr4, evmcs_rsp;
  8597. /* Record the guest's net vcpu time for enforced NMI injections. */
  8598. if (unlikely(!enable_vnmi &&
  8599. vmx->loaded_vmcs->soft_vnmi_blocked))
  8600. vmx->loaded_vmcs->entry_time = ktime_get();
  8601. /* Don't enter VMX if guest state is invalid, let the exit handler
  8602. start emulation until we arrive back to a valid state */
  8603. if (vmx->emulation_required)
  8604. return;
  8605. if (vmx->ple_window_dirty) {
  8606. vmx->ple_window_dirty = false;
  8607. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  8608. }
  8609. if (vmx->nested.sync_shadow_vmcs) {
  8610. copy_vmcs12_to_shadow(vmx);
  8611. vmx->nested.sync_shadow_vmcs = false;
  8612. }
  8613. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  8614. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  8615. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  8616. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  8617. cr3 = __get_current_cr3_fast();
  8618. if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
  8619. vmcs_writel(HOST_CR3, cr3);
  8620. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  8621. }
  8622. cr4 = cr4_read_shadow();
  8623. if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
  8624. vmcs_writel(HOST_CR4, cr4);
  8625. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  8626. }
  8627. /* When single-stepping over STI and MOV SS, we must clear the
  8628. * corresponding interruptibility bits in the guest state. Otherwise
  8629. * vmentry fails as it then expects bit 14 (BS) in pending debug
  8630. * exceptions being set, but that's not correct for the guest debugging
  8631. * case. */
  8632. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  8633. vmx_set_interrupt_shadow(vcpu, 0);
  8634. if (static_cpu_has(X86_FEATURE_PKU) &&
  8635. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  8636. vcpu->arch.pkru != vmx->host_pkru)
  8637. __write_pkru(vcpu->arch.pkru);
  8638. atomic_switch_perf_msrs(vmx);
  8639. vmx_arm_hv_timer(vcpu);
  8640. /*
  8641. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  8642. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  8643. * is no need to worry about the conditional branch over the wrmsr
  8644. * being speculatively taken.
  8645. */
  8646. x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
  8647. vmx->__launched = vmx->loaded_vmcs->launched;
  8648. evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
  8649. (unsigned long)&current_evmcs->host_rsp : 0;
  8650. asm(
  8651. /* Store host registers */
  8652. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  8653. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  8654. "push %%" _ASM_CX " \n\t"
  8655. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8656. "je 1f \n\t"
  8657. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8658. /* Avoid VMWRITE when Enlightened VMCS is in use */
  8659. "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
  8660. "jz 2f \n\t"
  8661. "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
  8662. "jmp 1f \n\t"
  8663. "2: \n\t"
  8664. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  8665. "1: \n\t"
  8666. /* Reload cr2 if changed */
  8667. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  8668. "mov %%cr2, %%" _ASM_DX " \n\t"
  8669. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  8670. "je 3f \n\t"
  8671. "mov %%" _ASM_AX", %%cr2 \n\t"
  8672. "3: \n\t"
  8673. /* Check if vmlaunch of vmresume is needed */
  8674. "cmpl $0, %c[launched](%0) \n\t"
  8675. /* Load guest registers. Don't clobber flags. */
  8676. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  8677. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  8678. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  8679. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  8680. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  8681. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  8682. #ifdef CONFIG_X86_64
  8683. "mov %c[r8](%0), %%r8 \n\t"
  8684. "mov %c[r9](%0), %%r9 \n\t"
  8685. "mov %c[r10](%0), %%r10 \n\t"
  8686. "mov %c[r11](%0), %%r11 \n\t"
  8687. "mov %c[r12](%0), %%r12 \n\t"
  8688. "mov %c[r13](%0), %%r13 \n\t"
  8689. "mov %c[r14](%0), %%r14 \n\t"
  8690. "mov %c[r15](%0), %%r15 \n\t"
  8691. #endif
  8692. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  8693. /* Enter guest mode */
  8694. "jne 1f \n\t"
  8695. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  8696. "jmp 2f \n\t"
  8697. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  8698. "2: "
  8699. /* Save guest registers, load host registers, keep flags */
  8700. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  8701. "pop %0 \n\t"
  8702. "setbe %c[fail](%0)\n\t"
  8703. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  8704. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  8705. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  8706. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  8707. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  8708. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  8709. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  8710. #ifdef CONFIG_X86_64
  8711. "mov %%r8, %c[r8](%0) \n\t"
  8712. "mov %%r9, %c[r9](%0) \n\t"
  8713. "mov %%r10, %c[r10](%0) \n\t"
  8714. "mov %%r11, %c[r11](%0) \n\t"
  8715. "mov %%r12, %c[r12](%0) \n\t"
  8716. "mov %%r13, %c[r13](%0) \n\t"
  8717. "mov %%r14, %c[r14](%0) \n\t"
  8718. "mov %%r15, %c[r15](%0) \n\t"
  8719. "xor %%r8d, %%r8d \n\t"
  8720. "xor %%r9d, %%r9d \n\t"
  8721. "xor %%r10d, %%r10d \n\t"
  8722. "xor %%r11d, %%r11d \n\t"
  8723. "xor %%r12d, %%r12d \n\t"
  8724. "xor %%r13d, %%r13d \n\t"
  8725. "xor %%r14d, %%r14d \n\t"
  8726. "xor %%r15d, %%r15d \n\t"
  8727. #endif
  8728. "mov %%cr2, %%" _ASM_AX " \n\t"
  8729. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  8730. "xor %%eax, %%eax \n\t"
  8731. "xor %%ebx, %%ebx \n\t"
  8732. "xor %%esi, %%esi \n\t"
  8733. "xor %%edi, %%edi \n\t"
  8734. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  8735. ".pushsection .rodata \n\t"
  8736. ".global vmx_return \n\t"
  8737. "vmx_return: " _ASM_PTR " 2b \n\t"
  8738. ".popsection"
  8739. : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
  8740. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  8741. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  8742. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  8743. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  8744. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  8745. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  8746. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  8747. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  8748. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  8749. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  8750. #ifdef CONFIG_X86_64
  8751. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  8752. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  8753. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  8754. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  8755. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  8756. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  8757. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  8758. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  8759. #endif
  8760. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  8761. [wordsize]"i"(sizeof(ulong))
  8762. : "cc", "memory"
  8763. #ifdef CONFIG_X86_64
  8764. , "rax", "rbx", "rdi"
  8765. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  8766. #else
  8767. , "eax", "ebx", "edi"
  8768. #endif
  8769. );
  8770. /*
  8771. * We do not use IBRS in the kernel. If this vCPU has used the
  8772. * SPEC_CTRL MSR it may have left it on; save the value and
  8773. * turn it off. This is much more efficient than blindly adding
  8774. * it to the atomic save/restore list. Especially as the former
  8775. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  8776. *
  8777. * For non-nested case:
  8778. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  8779. * save it.
  8780. *
  8781. * For nested case:
  8782. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  8783. * save it.
  8784. */
  8785. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  8786. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  8787. x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
  8788. /* Eliminate branch target predictions from guest mode */
  8789. vmexit_fill_RSB();
  8790. /* All fields are clean at this point */
  8791. if (static_branch_unlikely(&enable_evmcs))
  8792. current_evmcs->hv_clean_fields |=
  8793. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  8794. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  8795. if (vmx->host_debugctlmsr)
  8796. update_debugctlmsr(vmx->host_debugctlmsr);
  8797. #ifndef CONFIG_X86_64
  8798. /*
  8799. * The sysexit path does not restore ds/es, so we must set them to
  8800. * a reasonable value ourselves.
  8801. *
  8802. * We can't defer this to vmx_load_host_state() since that function
  8803. * may be executed in interrupt context, which saves and restore segments
  8804. * around it, nullifying its effect.
  8805. */
  8806. loadsegment(ds, __USER_DS);
  8807. loadsegment(es, __USER_DS);
  8808. #endif
  8809. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  8810. | (1 << VCPU_EXREG_RFLAGS)
  8811. | (1 << VCPU_EXREG_PDPTR)
  8812. | (1 << VCPU_EXREG_SEGMENTS)
  8813. | (1 << VCPU_EXREG_CR3));
  8814. vcpu->arch.regs_dirty = 0;
  8815. /*
  8816. * eager fpu is enabled if PKEY is supported and CR4 is switched
  8817. * back on host, so it is safe to read guest PKRU from current
  8818. * XSAVE.
  8819. */
  8820. if (static_cpu_has(X86_FEATURE_PKU) &&
  8821. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  8822. vcpu->arch.pkru = __read_pkru();
  8823. if (vcpu->arch.pkru != vmx->host_pkru)
  8824. __write_pkru(vmx->host_pkru);
  8825. }
  8826. vmx->nested.nested_run_pending = 0;
  8827. vmx->idt_vectoring_info = 0;
  8828. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  8829. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8830. return;
  8831. vmx->loaded_vmcs->launched = 1;
  8832. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  8833. vmx_complete_atomic_exit(vmx);
  8834. vmx_recover_nmi_blocking(vmx);
  8835. vmx_complete_interrupts(vmx);
  8836. }
  8837. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  8838. static struct kvm *vmx_vm_alloc(void)
  8839. {
  8840. struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
  8841. return &kvm_vmx->kvm;
  8842. }
  8843. static void vmx_vm_free(struct kvm *kvm)
  8844. {
  8845. vfree(to_kvm_vmx(kvm));
  8846. }
  8847. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  8848. {
  8849. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8850. int cpu;
  8851. if (vmx->loaded_vmcs == vmcs)
  8852. return;
  8853. cpu = get_cpu();
  8854. vmx->loaded_vmcs = vmcs;
  8855. vmx_vcpu_put(vcpu);
  8856. vmx_vcpu_load(vcpu, cpu);
  8857. put_cpu();
  8858. }
  8859. /*
  8860. * Ensure that the current vmcs of the logical processor is the
  8861. * vmcs01 of the vcpu before calling free_nested().
  8862. */
  8863. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  8864. {
  8865. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8866. vcpu_load(vcpu);
  8867. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  8868. free_nested(vmx);
  8869. vcpu_put(vcpu);
  8870. }
  8871. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  8872. {
  8873. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8874. if (enable_pml)
  8875. vmx_destroy_pml_buffer(vmx);
  8876. free_vpid(vmx->vpid);
  8877. leave_guest_mode(vcpu);
  8878. vmx_free_vcpu_nested(vcpu);
  8879. free_loaded_vmcs(vmx->loaded_vmcs);
  8880. kfree(vmx->guest_msrs);
  8881. kvm_vcpu_uninit(vcpu);
  8882. kmem_cache_free(kvm_vcpu_cache, vmx);
  8883. }
  8884. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  8885. {
  8886. int err;
  8887. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  8888. unsigned long *msr_bitmap;
  8889. int cpu;
  8890. if (!vmx)
  8891. return ERR_PTR(-ENOMEM);
  8892. vmx->vpid = allocate_vpid();
  8893. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  8894. if (err)
  8895. goto free_vcpu;
  8896. err = -ENOMEM;
  8897. /*
  8898. * If PML is turned on, failure on enabling PML just results in failure
  8899. * of creating the vcpu, therefore we can simplify PML logic (by
  8900. * avoiding dealing with cases, such as enabling PML partially on vcpus
  8901. * for the guest, etc.
  8902. */
  8903. if (enable_pml) {
  8904. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  8905. if (!vmx->pml_pg)
  8906. goto uninit_vcpu;
  8907. }
  8908. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  8909. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  8910. > PAGE_SIZE);
  8911. if (!vmx->guest_msrs)
  8912. goto free_pml;
  8913. err = alloc_loaded_vmcs(&vmx->vmcs01);
  8914. if (err < 0)
  8915. goto free_msrs;
  8916. msr_bitmap = vmx->vmcs01.msr_bitmap;
  8917. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  8918. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  8919. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  8920. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  8921. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  8922. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  8923. vmx->msr_bitmap_mode = 0;
  8924. vmx->loaded_vmcs = &vmx->vmcs01;
  8925. cpu = get_cpu();
  8926. vmx_vcpu_load(&vmx->vcpu, cpu);
  8927. vmx->vcpu.cpu = cpu;
  8928. vmx_vcpu_setup(vmx);
  8929. vmx_vcpu_put(&vmx->vcpu);
  8930. put_cpu();
  8931. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  8932. err = alloc_apic_access_page(kvm);
  8933. if (err)
  8934. goto free_vmcs;
  8935. }
  8936. if (enable_ept && !enable_unrestricted_guest) {
  8937. err = init_rmode_identity_map(kvm);
  8938. if (err)
  8939. goto free_vmcs;
  8940. }
  8941. if (nested) {
  8942. nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
  8943. kvm_vcpu_apicv_active(&vmx->vcpu));
  8944. vmx->nested.vpid02 = allocate_vpid();
  8945. }
  8946. vmx->nested.posted_intr_nv = -1;
  8947. vmx->nested.current_vmptr = -1ull;
  8948. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  8949. /*
  8950. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  8951. * or POSTED_INTR_WAKEUP_VECTOR.
  8952. */
  8953. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  8954. vmx->pi_desc.sn = 1;
  8955. return &vmx->vcpu;
  8956. free_vmcs:
  8957. free_vpid(vmx->nested.vpid02);
  8958. free_loaded_vmcs(vmx->loaded_vmcs);
  8959. free_msrs:
  8960. kfree(vmx->guest_msrs);
  8961. free_pml:
  8962. vmx_destroy_pml_buffer(vmx);
  8963. uninit_vcpu:
  8964. kvm_vcpu_uninit(&vmx->vcpu);
  8965. free_vcpu:
  8966. free_vpid(vmx->vpid);
  8967. kmem_cache_free(kvm_vcpu_cache, vmx);
  8968. return ERR_PTR(err);
  8969. }
  8970. static int vmx_vm_init(struct kvm *kvm)
  8971. {
  8972. if (!ple_gap)
  8973. kvm->arch.pause_in_guest = true;
  8974. return 0;
  8975. }
  8976. static void __init vmx_check_processor_compat(void *rtn)
  8977. {
  8978. struct vmcs_config vmcs_conf;
  8979. *(int *)rtn = 0;
  8980. if (setup_vmcs_config(&vmcs_conf) < 0)
  8981. *(int *)rtn = -EIO;
  8982. nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
  8983. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  8984. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  8985. smp_processor_id());
  8986. *(int *)rtn = -EIO;
  8987. }
  8988. }
  8989. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  8990. {
  8991. u8 cache;
  8992. u64 ipat = 0;
  8993. /* For VT-d and EPT combination
  8994. * 1. MMIO: always map as UC
  8995. * 2. EPT with VT-d:
  8996. * a. VT-d without snooping control feature: can't guarantee the
  8997. * result, try to trust guest.
  8998. * b. VT-d with snooping control feature: snooping control feature of
  8999. * VT-d engine can guarantee the cache correctness. Just set it
  9000. * to WB to keep consistent with host. So the same as item 3.
  9001. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  9002. * consistent with host MTRR
  9003. */
  9004. if (is_mmio) {
  9005. cache = MTRR_TYPE_UNCACHABLE;
  9006. goto exit;
  9007. }
  9008. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  9009. ipat = VMX_EPT_IPAT_BIT;
  9010. cache = MTRR_TYPE_WRBACK;
  9011. goto exit;
  9012. }
  9013. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  9014. ipat = VMX_EPT_IPAT_BIT;
  9015. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  9016. cache = MTRR_TYPE_WRBACK;
  9017. else
  9018. cache = MTRR_TYPE_UNCACHABLE;
  9019. goto exit;
  9020. }
  9021. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  9022. exit:
  9023. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  9024. }
  9025. static int vmx_get_lpage_level(void)
  9026. {
  9027. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  9028. return PT_DIRECTORY_LEVEL;
  9029. else
  9030. /* For shadow and EPT supported 1GB page */
  9031. return PT_PDPE_LEVEL;
  9032. }
  9033. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  9034. {
  9035. /*
  9036. * These bits in the secondary execution controls field
  9037. * are dynamic, the others are mostly based on the hypervisor
  9038. * architecture and the guest's CPUID. Do not touch the
  9039. * dynamic bits.
  9040. */
  9041. u32 mask =
  9042. SECONDARY_EXEC_SHADOW_VMCS |
  9043. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  9044. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9045. SECONDARY_EXEC_DESC;
  9046. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  9047. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  9048. (new_ctl & ~mask) | (cur_ctl & mask));
  9049. }
  9050. /*
  9051. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  9052. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  9053. */
  9054. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  9055. {
  9056. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9057. struct kvm_cpuid_entry2 *entry;
  9058. vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
  9059. vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
  9060. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  9061. if (entry && (entry->_reg & (_cpuid_mask))) \
  9062. vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
  9063. } while (0)
  9064. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  9065. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  9066. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  9067. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  9068. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  9069. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  9070. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  9071. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  9072. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  9073. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  9074. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  9075. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  9076. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  9077. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  9078. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  9079. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  9080. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  9081. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  9082. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  9083. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  9084. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  9085. #undef cr4_fixed1_update
  9086. }
  9087. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  9088. {
  9089. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9090. if (cpu_has_secondary_exec_ctrls()) {
  9091. vmx_compute_secondary_exec_control(vmx);
  9092. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  9093. }
  9094. if (nested_vmx_allowed(vcpu))
  9095. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  9096. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9097. else
  9098. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  9099. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9100. if (nested_vmx_allowed(vcpu))
  9101. nested_vmx_cr_fixed1_bits_update(vcpu);
  9102. }
  9103. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  9104. {
  9105. if (func == 1 && nested)
  9106. entry->ecx |= bit(X86_FEATURE_VMX);
  9107. }
  9108. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  9109. struct x86_exception *fault)
  9110. {
  9111. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9112. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9113. u32 exit_reason;
  9114. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  9115. if (vmx->nested.pml_full) {
  9116. exit_reason = EXIT_REASON_PML_FULL;
  9117. vmx->nested.pml_full = false;
  9118. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  9119. } else if (fault->error_code & PFERR_RSVD_MASK)
  9120. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  9121. else
  9122. exit_reason = EXIT_REASON_EPT_VIOLATION;
  9123. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  9124. vmcs12->guest_physical_address = fault->address;
  9125. }
  9126. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  9127. {
  9128. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  9129. }
  9130. /* Callbacks for nested_ept_init_mmu_context: */
  9131. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  9132. {
  9133. /* return the page table to be shadowed - in our case, EPT12 */
  9134. return get_vmcs12(vcpu)->ept_pointer;
  9135. }
  9136. static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  9137. {
  9138. WARN_ON(mmu_is_nested(vcpu));
  9139. if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
  9140. return 1;
  9141. kvm_mmu_unload(vcpu);
  9142. kvm_init_shadow_ept_mmu(vcpu,
  9143. to_vmx(vcpu)->nested.msrs.ept_caps &
  9144. VMX_EPT_EXECUTE_ONLY_BIT,
  9145. nested_ept_ad_enabled(vcpu));
  9146. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  9147. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  9148. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  9149. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  9150. return 0;
  9151. }
  9152. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  9153. {
  9154. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  9155. }
  9156. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  9157. u16 error_code)
  9158. {
  9159. bool inequality, bit;
  9160. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  9161. inequality =
  9162. (error_code & vmcs12->page_fault_error_code_mask) !=
  9163. vmcs12->page_fault_error_code_match;
  9164. return inequality ^ bit;
  9165. }
  9166. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  9167. struct x86_exception *fault)
  9168. {
  9169. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9170. WARN_ON(!is_guest_mode(vcpu));
  9171. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  9172. !to_vmx(vcpu)->nested.nested_run_pending) {
  9173. vmcs12->vm_exit_intr_error_code = fault->error_code;
  9174. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  9175. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  9176. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  9177. fault->address);
  9178. } else {
  9179. kvm_inject_page_fault(vcpu, fault);
  9180. }
  9181. }
  9182. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9183. struct vmcs12 *vmcs12);
  9184. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
  9185. struct vmcs12 *vmcs12)
  9186. {
  9187. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9188. struct page *page;
  9189. u64 hpa;
  9190. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9191. /*
  9192. * Translate L1 physical address to host physical
  9193. * address for vmcs02. Keep the page pinned, so this
  9194. * physical address remains valid. We keep a reference
  9195. * to it so we can release it later.
  9196. */
  9197. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  9198. kvm_release_page_dirty(vmx->nested.apic_access_page);
  9199. vmx->nested.apic_access_page = NULL;
  9200. }
  9201. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  9202. /*
  9203. * If translation failed, no matter: This feature asks
  9204. * to exit when accessing the given address, and if it
  9205. * can never be accessed, this feature won't do
  9206. * anything anyway.
  9207. */
  9208. if (!is_error_page(page)) {
  9209. vmx->nested.apic_access_page = page;
  9210. hpa = page_to_phys(vmx->nested.apic_access_page);
  9211. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  9212. } else {
  9213. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  9214. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  9215. }
  9216. }
  9217. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  9218. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  9219. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  9220. vmx->nested.virtual_apic_page = NULL;
  9221. }
  9222. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  9223. /*
  9224. * If translation failed, VM entry will fail because
  9225. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  9226. * Failing the vm entry is _not_ what the processor
  9227. * does but it's basically the only possibility we
  9228. * have. We could still enter the guest if CR8 load
  9229. * exits are enabled, CR8 store exits are enabled, and
  9230. * virtualize APIC access is disabled; in this case
  9231. * the processor would never use the TPR shadow and we
  9232. * could simply clear the bit from the execution
  9233. * control. But such a configuration is useless, so
  9234. * let's keep the code simple.
  9235. */
  9236. if (!is_error_page(page)) {
  9237. vmx->nested.virtual_apic_page = page;
  9238. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  9239. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  9240. }
  9241. }
  9242. if (nested_cpu_has_posted_intr(vmcs12)) {
  9243. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  9244. kunmap(vmx->nested.pi_desc_page);
  9245. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  9246. vmx->nested.pi_desc_page = NULL;
  9247. }
  9248. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  9249. if (is_error_page(page))
  9250. return;
  9251. vmx->nested.pi_desc_page = page;
  9252. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  9253. vmx->nested.pi_desc =
  9254. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  9255. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9256. (PAGE_SIZE - 1)));
  9257. vmcs_write64(POSTED_INTR_DESC_ADDR,
  9258. page_to_phys(vmx->nested.pi_desc_page) +
  9259. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9260. (PAGE_SIZE - 1)));
  9261. }
  9262. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  9263. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  9264. CPU_BASED_USE_MSR_BITMAPS);
  9265. else
  9266. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  9267. CPU_BASED_USE_MSR_BITMAPS);
  9268. }
  9269. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  9270. {
  9271. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  9272. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9273. if (vcpu->arch.virtual_tsc_khz == 0)
  9274. return;
  9275. /* Make sure short timeouts reliably trigger an immediate vmexit.
  9276. * hrtimer_start does not guarantee this. */
  9277. if (preemption_timeout <= 1) {
  9278. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  9279. return;
  9280. }
  9281. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  9282. preemption_timeout *= 1000000;
  9283. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  9284. hrtimer_start(&vmx->nested.preemption_timer,
  9285. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  9286. }
  9287. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  9288. struct vmcs12 *vmcs12)
  9289. {
  9290. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  9291. return 0;
  9292. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  9293. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  9294. return -EINVAL;
  9295. return 0;
  9296. }
  9297. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  9298. struct vmcs12 *vmcs12)
  9299. {
  9300. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9301. return 0;
  9302. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  9303. return -EINVAL;
  9304. return 0;
  9305. }
  9306. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  9307. struct vmcs12 *vmcs12)
  9308. {
  9309. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9310. return 0;
  9311. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  9312. return -EINVAL;
  9313. return 0;
  9314. }
  9315. /*
  9316. * Merge L0's and L1's MSR bitmap, return false to indicate that
  9317. * we do not use the hardware.
  9318. */
  9319. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9320. struct vmcs12 *vmcs12)
  9321. {
  9322. int msr;
  9323. struct page *page;
  9324. unsigned long *msr_bitmap_l1;
  9325. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  9326. /*
  9327. * pred_cmd & spec_ctrl are trying to verify two things:
  9328. *
  9329. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  9330. * ensures that we do not accidentally generate an L02 MSR bitmap
  9331. * from the L12 MSR bitmap that is too permissive.
  9332. * 2. That L1 or L2s have actually used the MSR. This avoids
  9333. * unnecessarily merging of the bitmap if the MSR is unused. This
  9334. * works properly because we only update the L01 MSR bitmap lazily.
  9335. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  9336. * updated to reflect this when L1 (or its L2s) actually write to
  9337. * the MSR.
  9338. */
  9339. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  9340. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  9341. /* Nothing to do if the MSR bitmap is not in use. */
  9342. if (!cpu_has_vmx_msr_bitmap() ||
  9343. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9344. return false;
  9345. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9346. !pred_cmd && !spec_ctrl)
  9347. return false;
  9348. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  9349. if (is_error_page(page))
  9350. return false;
  9351. msr_bitmap_l1 = (unsigned long *)kmap(page);
  9352. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  9353. /*
  9354. * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
  9355. * just lets the processor take the value from the virtual-APIC page;
  9356. * take those 256 bits directly from the L1 bitmap.
  9357. */
  9358. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  9359. unsigned word = msr / BITS_PER_LONG;
  9360. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  9361. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  9362. }
  9363. } else {
  9364. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  9365. unsigned word = msr / BITS_PER_LONG;
  9366. msr_bitmap_l0[word] = ~0;
  9367. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  9368. }
  9369. }
  9370. nested_vmx_disable_intercept_for_msr(
  9371. msr_bitmap_l1, msr_bitmap_l0,
  9372. X2APIC_MSR(APIC_TASKPRI),
  9373. MSR_TYPE_W);
  9374. if (nested_cpu_has_vid(vmcs12)) {
  9375. nested_vmx_disable_intercept_for_msr(
  9376. msr_bitmap_l1, msr_bitmap_l0,
  9377. X2APIC_MSR(APIC_EOI),
  9378. MSR_TYPE_W);
  9379. nested_vmx_disable_intercept_for_msr(
  9380. msr_bitmap_l1, msr_bitmap_l0,
  9381. X2APIC_MSR(APIC_SELF_IPI),
  9382. MSR_TYPE_W);
  9383. }
  9384. if (spec_ctrl)
  9385. nested_vmx_disable_intercept_for_msr(
  9386. msr_bitmap_l1, msr_bitmap_l0,
  9387. MSR_IA32_SPEC_CTRL,
  9388. MSR_TYPE_R | MSR_TYPE_W);
  9389. if (pred_cmd)
  9390. nested_vmx_disable_intercept_for_msr(
  9391. msr_bitmap_l1, msr_bitmap_l0,
  9392. MSR_IA32_PRED_CMD,
  9393. MSR_TYPE_W);
  9394. kunmap(page);
  9395. kvm_release_page_clean(page);
  9396. return true;
  9397. }
  9398. static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
  9399. struct vmcs12 *vmcs12)
  9400. {
  9401. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  9402. !page_address_valid(vcpu, vmcs12->apic_access_addr))
  9403. return -EINVAL;
  9404. else
  9405. return 0;
  9406. }
  9407. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  9408. struct vmcs12 *vmcs12)
  9409. {
  9410. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9411. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  9412. !nested_cpu_has_vid(vmcs12) &&
  9413. !nested_cpu_has_posted_intr(vmcs12))
  9414. return 0;
  9415. /*
  9416. * If virtualize x2apic mode is enabled,
  9417. * virtualize apic access must be disabled.
  9418. */
  9419. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  9420. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  9421. return -EINVAL;
  9422. /*
  9423. * If virtual interrupt delivery is enabled,
  9424. * we must exit on external interrupts.
  9425. */
  9426. if (nested_cpu_has_vid(vmcs12) &&
  9427. !nested_exit_on_intr(vcpu))
  9428. return -EINVAL;
  9429. /*
  9430. * bits 15:8 should be zero in posted_intr_nv,
  9431. * the descriptor address has been already checked
  9432. * in nested_get_vmcs12_pages.
  9433. */
  9434. if (nested_cpu_has_posted_intr(vmcs12) &&
  9435. (!nested_cpu_has_vid(vmcs12) ||
  9436. !nested_exit_intr_ack_set(vcpu) ||
  9437. vmcs12->posted_intr_nv & 0xff00))
  9438. return -EINVAL;
  9439. /* tpr shadow is needed by all apicv features. */
  9440. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9441. return -EINVAL;
  9442. return 0;
  9443. }
  9444. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  9445. unsigned long count_field,
  9446. unsigned long addr_field)
  9447. {
  9448. int maxphyaddr;
  9449. u64 count, addr;
  9450. if (vmcs12_read_any(vcpu, count_field, &count) ||
  9451. vmcs12_read_any(vcpu, addr_field, &addr)) {
  9452. WARN_ON(1);
  9453. return -EINVAL;
  9454. }
  9455. if (count == 0)
  9456. return 0;
  9457. maxphyaddr = cpuid_maxphyaddr(vcpu);
  9458. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  9459. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  9460. pr_debug_ratelimited(
  9461. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  9462. addr_field, maxphyaddr, count, addr);
  9463. return -EINVAL;
  9464. }
  9465. return 0;
  9466. }
  9467. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  9468. struct vmcs12 *vmcs12)
  9469. {
  9470. if (vmcs12->vm_exit_msr_load_count == 0 &&
  9471. vmcs12->vm_exit_msr_store_count == 0 &&
  9472. vmcs12->vm_entry_msr_load_count == 0)
  9473. return 0; /* Fast path */
  9474. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  9475. VM_EXIT_MSR_LOAD_ADDR) ||
  9476. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  9477. VM_EXIT_MSR_STORE_ADDR) ||
  9478. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  9479. VM_ENTRY_MSR_LOAD_ADDR))
  9480. return -EINVAL;
  9481. return 0;
  9482. }
  9483. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  9484. struct vmcs12 *vmcs12)
  9485. {
  9486. u64 address = vmcs12->pml_address;
  9487. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  9488. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
  9489. if (!nested_cpu_has_ept(vmcs12) ||
  9490. !IS_ALIGNED(address, 4096) ||
  9491. address >> maxphyaddr)
  9492. return -EINVAL;
  9493. }
  9494. return 0;
  9495. }
  9496. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  9497. struct vmx_msr_entry *e)
  9498. {
  9499. /* x2APIC MSR accesses are not allowed */
  9500. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  9501. return -EINVAL;
  9502. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  9503. e->index == MSR_IA32_UCODE_REV)
  9504. return -EINVAL;
  9505. if (e->reserved != 0)
  9506. return -EINVAL;
  9507. return 0;
  9508. }
  9509. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  9510. struct vmx_msr_entry *e)
  9511. {
  9512. if (e->index == MSR_FS_BASE ||
  9513. e->index == MSR_GS_BASE ||
  9514. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  9515. nested_vmx_msr_check_common(vcpu, e))
  9516. return -EINVAL;
  9517. return 0;
  9518. }
  9519. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  9520. struct vmx_msr_entry *e)
  9521. {
  9522. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  9523. nested_vmx_msr_check_common(vcpu, e))
  9524. return -EINVAL;
  9525. return 0;
  9526. }
  9527. /*
  9528. * Load guest's/host's msr at nested entry/exit.
  9529. * return 0 for success, entry index for failure.
  9530. */
  9531. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9532. {
  9533. u32 i;
  9534. struct vmx_msr_entry e;
  9535. struct msr_data msr;
  9536. msr.host_initiated = false;
  9537. for (i = 0; i < count; i++) {
  9538. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  9539. &e, sizeof(e))) {
  9540. pr_debug_ratelimited(
  9541. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9542. __func__, i, gpa + i * sizeof(e));
  9543. goto fail;
  9544. }
  9545. if (nested_vmx_load_msr_check(vcpu, &e)) {
  9546. pr_debug_ratelimited(
  9547. "%s check failed (%u, 0x%x, 0x%x)\n",
  9548. __func__, i, e.index, e.reserved);
  9549. goto fail;
  9550. }
  9551. msr.index = e.index;
  9552. msr.data = e.value;
  9553. if (kvm_set_msr(vcpu, &msr)) {
  9554. pr_debug_ratelimited(
  9555. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9556. __func__, i, e.index, e.value);
  9557. goto fail;
  9558. }
  9559. }
  9560. return 0;
  9561. fail:
  9562. return i + 1;
  9563. }
  9564. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9565. {
  9566. u32 i;
  9567. struct vmx_msr_entry e;
  9568. for (i = 0; i < count; i++) {
  9569. struct msr_data msr_info;
  9570. if (kvm_vcpu_read_guest(vcpu,
  9571. gpa + i * sizeof(e),
  9572. &e, 2 * sizeof(u32))) {
  9573. pr_debug_ratelimited(
  9574. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9575. __func__, i, gpa + i * sizeof(e));
  9576. return -EINVAL;
  9577. }
  9578. if (nested_vmx_store_msr_check(vcpu, &e)) {
  9579. pr_debug_ratelimited(
  9580. "%s check failed (%u, 0x%x, 0x%x)\n",
  9581. __func__, i, e.index, e.reserved);
  9582. return -EINVAL;
  9583. }
  9584. msr_info.host_initiated = false;
  9585. msr_info.index = e.index;
  9586. if (kvm_get_msr(vcpu, &msr_info)) {
  9587. pr_debug_ratelimited(
  9588. "%s cannot read MSR (%u, 0x%x)\n",
  9589. __func__, i, e.index);
  9590. return -EINVAL;
  9591. }
  9592. if (kvm_vcpu_write_guest(vcpu,
  9593. gpa + i * sizeof(e) +
  9594. offsetof(struct vmx_msr_entry, value),
  9595. &msr_info.data, sizeof(msr_info.data))) {
  9596. pr_debug_ratelimited(
  9597. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9598. __func__, i, e.index, msr_info.data);
  9599. return -EINVAL;
  9600. }
  9601. }
  9602. return 0;
  9603. }
  9604. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  9605. {
  9606. unsigned long invalid_mask;
  9607. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  9608. return (val & invalid_mask) == 0;
  9609. }
  9610. /*
  9611. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  9612. * emulating VM entry into a guest with EPT enabled.
  9613. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9614. * is assigned to entry_failure_code on failure.
  9615. */
  9616. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  9617. u32 *entry_failure_code)
  9618. {
  9619. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  9620. if (!nested_cr3_valid(vcpu, cr3)) {
  9621. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9622. return 1;
  9623. }
  9624. /*
  9625. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  9626. * must not be dereferenced.
  9627. */
  9628. if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
  9629. !nested_ept) {
  9630. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  9631. *entry_failure_code = ENTRY_FAIL_PDPTE;
  9632. return 1;
  9633. }
  9634. }
  9635. vcpu->arch.cr3 = cr3;
  9636. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  9637. }
  9638. kvm_mmu_reset_context(vcpu);
  9639. return 0;
  9640. }
  9641. static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9642. {
  9643. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9644. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  9645. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  9646. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  9647. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  9648. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  9649. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  9650. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  9651. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  9652. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  9653. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  9654. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  9655. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  9656. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  9657. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  9658. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  9659. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  9660. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  9661. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  9662. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  9663. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  9664. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  9665. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  9666. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  9667. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  9668. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  9669. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  9670. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  9671. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  9672. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  9673. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  9674. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  9675. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  9676. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  9677. vmcs12->guest_pending_dbg_exceptions);
  9678. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  9679. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  9680. if (nested_cpu_has_xsaves(vmcs12))
  9681. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  9682. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  9683. if (cpu_has_vmx_posted_intr())
  9684. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  9685. /*
  9686. * Whether page-faults are trapped is determined by a combination of
  9687. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  9688. * If enable_ept, L0 doesn't care about page faults and we should
  9689. * set all of these to L1's desires. However, if !enable_ept, L0 does
  9690. * care about (at least some) page faults, and because it is not easy
  9691. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  9692. * to exit on each and every L2 page fault. This is done by setting
  9693. * MASK=MATCH=0 and (see below) EB.PF=1.
  9694. * Note that below we don't need special code to set EB.PF beyond the
  9695. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  9696. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  9697. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  9698. */
  9699. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  9700. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  9701. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  9702. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  9703. /* All VMFUNCs are currently emulated through L0 vmexits. */
  9704. if (cpu_has_vmx_vmfunc())
  9705. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  9706. if (cpu_has_vmx_apicv()) {
  9707. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  9708. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  9709. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  9710. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  9711. }
  9712. /*
  9713. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  9714. * Some constant fields are set here by vmx_set_constant_host_state().
  9715. * Other fields are different per CPU, and will be set later when
  9716. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  9717. */
  9718. vmx_set_constant_host_state(vmx);
  9719. /*
  9720. * Set the MSR load/store lists to match L0's settings.
  9721. */
  9722. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  9723. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9724. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  9725. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9726. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  9727. set_cr4_guest_host_mask(vmx);
  9728. if (vmx_mpx_supported())
  9729. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  9730. if (enable_vpid) {
  9731. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  9732. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  9733. else
  9734. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  9735. }
  9736. /*
  9737. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  9738. */
  9739. if (enable_ept) {
  9740. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  9741. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  9742. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  9743. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  9744. }
  9745. if (cpu_has_vmx_msr_bitmap())
  9746. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  9747. }
  9748. /*
  9749. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  9750. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  9751. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  9752. * guest in a way that will both be appropriate to L1's requests, and our
  9753. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  9754. * function also has additional necessary side-effects, like setting various
  9755. * vcpu->arch fields.
  9756. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9757. * is assigned to entry_failure_code on failure.
  9758. */
  9759. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9760. u32 *entry_failure_code)
  9761. {
  9762. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9763. u32 exec_control, vmcs12_exec_ctrl;
  9764. if (vmx->nested.dirty_vmcs12) {
  9765. prepare_vmcs02_full(vcpu, vmcs12);
  9766. vmx->nested.dirty_vmcs12 = false;
  9767. }
  9768. /*
  9769. * First, the fields that are shadowed. This must be kept in sync
  9770. * with vmx_shadow_fields.h.
  9771. */
  9772. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  9773. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  9774. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  9775. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  9776. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  9777. /*
  9778. * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
  9779. * HOST_FS_BASE, HOST_GS_BASE.
  9780. */
  9781. if (vmx->nested.nested_run_pending &&
  9782. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  9783. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  9784. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  9785. } else {
  9786. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  9787. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  9788. }
  9789. if (vmx->nested.nested_run_pending) {
  9790. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  9791. vmcs12->vm_entry_intr_info_field);
  9792. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  9793. vmcs12->vm_entry_exception_error_code);
  9794. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  9795. vmcs12->vm_entry_instruction_len);
  9796. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  9797. vmcs12->guest_interruptibility_info);
  9798. vmx->loaded_vmcs->nmi_known_unmasked =
  9799. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  9800. } else {
  9801. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9802. }
  9803. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  9804. exec_control = vmcs12->pin_based_vm_exec_control;
  9805. /* Preemption timer setting is only taken from vmcs01. */
  9806. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9807. exec_control |= vmcs_config.pin_based_exec_ctrl;
  9808. if (vmx->hv_deadline_tsc == -1)
  9809. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9810. /* Posted interrupts setting is only taken from vmcs12. */
  9811. if (nested_cpu_has_posted_intr(vmcs12)) {
  9812. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  9813. vmx->nested.pi_pending = false;
  9814. } else {
  9815. exec_control &= ~PIN_BASED_POSTED_INTR;
  9816. }
  9817. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  9818. vmx->nested.preemption_timer_expired = false;
  9819. if (nested_cpu_has_preemption_timer(vmcs12))
  9820. vmx_start_preemption_timer(vcpu);
  9821. if (cpu_has_secondary_exec_ctrls()) {
  9822. exec_control = vmx->secondary_exec_control;
  9823. /* Take the following fields only from vmcs12 */
  9824. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9825. SECONDARY_EXEC_ENABLE_INVPCID |
  9826. SECONDARY_EXEC_RDTSCP |
  9827. SECONDARY_EXEC_XSAVES |
  9828. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  9829. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  9830. SECONDARY_EXEC_ENABLE_VMFUNC);
  9831. if (nested_cpu_has(vmcs12,
  9832. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  9833. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  9834. ~SECONDARY_EXEC_ENABLE_PML;
  9835. exec_control |= vmcs12_exec_ctrl;
  9836. }
  9837. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  9838. vmcs_write16(GUEST_INTR_STATUS,
  9839. vmcs12->guest_intr_status);
  9840. /*
  9841. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  9842. * nested_get_vmcs12_pages will either fix it up or
  9843. * remove the VM execution control.
  9844. */
  9845. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  9846. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  9847. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  9848. }
  9849. /*
  9850. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  9851. * entry, but only if the current (host) sp changed from the value
  9852. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  9853. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  9854. * here we just force the write to happen on entry.
  9855. */
  9856. vmx->host_rsp = 0;
  9857. exec_control = vmx_exec_control(vmx); /* L0's desires */
  9858. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  9859. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  9860. exec_control &= ~CPU_BASED_TPR_SHADOW;
  9861. exec_control |= vmcs12->cpu_based_vm_exec_control;
  9862. /*
  9863. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  9864. * nested_get_vmcs12_pages can't fix it up, the illegal value
  9865. * will result in a VM entry failure.
  9866. */
  9867. if (exec_control & CPU_BASED_TPR_SHADOW) {
  9868. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  9869. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  9870. } else {
  9871. #ifdef CONFIG_X86_64
  9872. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  9873. CPU_BASED_CR8_STORE_EXITING;
  9874. #endif
  9875. }
  9876. /*
  9877. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  9878. * for I/O port accesses.
  9879. */
  9880. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  9881. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  9882. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  9883. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  9884. * bitwise-or of what L1 wants to trap for L2, and what we want to
  9885. * trap. Note that CR0.TS also needs updating - we do this later.
  9886. */
  9887. update_exception_bitmap(vcpu);
  9888. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  9889. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  9890. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  9891. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  9892. * bits are further modified by vmx_set_efer() below.
  9893. */
  9894. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  9895. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  9896. * emulated by vmx_set_efer(), below.
  9897. */
  9898. vm_entry_controls_init(vmx,
  9899. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  9900. ~VM_ENTRY_IA32E_MODE) |
  9901. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  9902. if (vmx->nested.nested_run_pending &&
  9903. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  9904. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  9905. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  9906. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  9907. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  9908. }
  9909. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  9910. if (kvm_has_tsc_control)
  9911. decache_tsc_multiplier(vmx);
  9912. if (enable_vpid) {
  9913. /*
  9914. * There is no direct mapping between vpid02 and vpid12, the
  9915. * vpid02 is per-vCPU for L0 and reused while the value of
  9916. * vpid12 is changed w/ one invvpid during nested vmentry.
  9917. * The vpid12 is allocated by L1 for L2, so it will not
  9918. * influence global bitmap(for vpid01 and vpid02 allocation)
  9919. * even if spawn a lot of nested vCPUs.
  9920. */
  9921. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
  9922. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  9923. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  9924. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  9925. }
  9926. } else {
  9927. vmx_flush_tlb(vcpu, true);
  9928. }
  9929. }
  9930. if (enable_pml) {
  9931. /*
  9932. * Conceptually we want to copy the PML address and index from
  9933. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  9934. * since we always flush the log on each vmexit, this happens
  9935. * to be equivalent to simply resetting the fields in vmcs02.
  9936. */
  9937. ASSERT(vmx->pml_pg);
  9938. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  9939. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  9940. }
  9941. if (nested_cpu_has_ept(vmcs12)) {
  9942. if (nested_ept_init_mmu_context(vcpu)) {
  9943. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9944. return 1;
  9945. }
  9946. } else if (nested_cpu_has2(vmcs12,
  9947. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9948. vmx_flush_tlb(vcpu, true);
  9949. }
  9950. /*
  9951. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  9952. * bits which we consider mandatory enabled.
  9953. * The CR0_READ_SHADOW is what L2 should have expected to read given
  9954. * the specifications by L1; It's not enough to take
  9955. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  9956. * have more bits than L1 expected.
  9957. */
  9958. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  9959. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  9960. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  9961. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  9962. if (vmx->nested.nested_run_pending &&
  9963. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  9964. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  9965. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  9966. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  9967. else
  9968. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  9969. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  9970. vmx_set_efer(vcpu, vcpu->arch.efer);
  9971. /*
  9972. * Guest state is invalid and unrestricted guest is disabled,
  9973. * which means L1 attempted VMEntry to L2 with invalid state.
  9974. * Fail the VMEntry.
  9975. */
  9976. if (vmx->emulation_required) {
  9977. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9978. return 1;
  9979. }
  9980. /* Shadow page tables on either EPT or shadow page tables. */
  9981. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  9982. entry_failure_code))
  9983. return 1;
  9984. if (!enable_ept)
  9985. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  9986. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  9987. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  9988. return 0;
  9989. }
  9990. static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
  9991. {
  9992. if (!nested_cpu_has_nmi_exiting(vmcs12) &&
  9993. nested_cpu_has_virtual_nmis(vmcs12))
  9994. return -EINVAL;
  9995. if (!nested_cpu_has_virtual_nmis(vmcs12) &&
  9996. nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
  9997. return -EINVAL;
  9998. return 0;
  9999. }
  10000. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10001. {
  10002. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10003. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  10004. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  10005. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10006. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  10007. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10008. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  10009. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10010. if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
  10011. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10012. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  10013. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10014. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  10015. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10016. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  10017. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10018. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  10019. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10020. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  10021. vmx->nested.msrs.procbased_ctls_low,
  10022. vmx->nested.msrs.procbased_ctls_high) ||
  10023. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  10024. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  10025. vmx->nested.msrs.secondary_ctls_low,
  10026. vmx->nested.msrs.secondary_ctls_high)) ||
  10027. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  10028. vmx->nested.msrs.pinbased_ctls_low,
  10029. vmx->nested.msrs.pinbased_ctls_high) ||
  10030. !vmx_control_verify(vmcs12->vm_exit_controls,
  10031. vmx->nested.msrs.exit_ctls_low,
  10032. vmx->nested.msrs.exit_ctls_high) ||
  10033. !vmx_control_verify(vmcs12->vm_entry_controls,
  10034. vmx->nested.msrs.entry_ctls_low,
  10035. vmx->nested.msrs.entry_ctls_high))
  10036. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10037. if (nested_vmx_check_nmi_controls(vmcs12))
  10038. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10039. if (nested_cpu_has_vmfunc(vmcs12)) {
  10040. if (vmcs12->vm_function_control &
  10041. ~vmx->nested.msrs.vmfunc_controls)
  10042. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10043. if (nested_cpu_has_eptp_switching(vmcs12)) {
  10044. if (!nested_cpu_has_ept(vmcs12) ||
  10045. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  10046. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10047. }
  10048. }
  10049. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  10050. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10051. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  10052. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  10053. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  10054. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  10055. /*
  10056. * From the Intel SDM, volume 3:
  10057. * Fields relevant to VM-entry event injection must be set properly.
  10058. * These fields are the VM-entry interruption-information field, the
  10059. * VM-entry exception error code, and the VM-entry instruction length.
  10060. */
  10061. if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
  10062. u32 intr_info = vmcs12->vm_entry_intr_info_field;
  10063. u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
  10064. u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
  10065. bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
  10066. bool should_have_error_code;
  10067. bool urg = nested_cpu_has2(vmcs12,
  10068. SECONDARY_EXEC_UNRESTRICTED_GUEST);
  10069. bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
  10070. /* VM-entry interruption-info field: interruption type */
  10071. if (intr_type == INTR_TYPE_RESERVED ||
  10072. (intr_type == INTR_TYPE_OTHER_EVENT &&
  10073. !nested_cpu_supports_monitor_trap_flag(vcpu)))
  10074. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10075. /* VM-entry interruption-info field: vector */
  10076. if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
  10077. (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
  10078. (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
  10079. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10080. /* VM-entry interruption-info field: deliver error code */
  10081. should_have_error_code =
  10082. intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
  10083. x86_exception_has_error_code(vector);
  10084. if (has_error_code != should_have_error_code)
  10085. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10086. /* VM-entry exception error code */
  10087. if (has_error_code &&
  10088. vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
  10089. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10090. /* VM-entry interruption-info field: reserved bits */
  10091. if (intr_info & INTR_INFO_RESVD_BITS_MASK)
  10092. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10093. /* VM-entry instruction length */
  10094. switch (intr_type) {
  10095. case INTR_TYPE_SOFT_EXCEPTION:
  10096. case INTR_TYPE_SOFT_INTR:
  10097. case INTR_TYPE_PRIV_SW_EXCEPTION:
  10098. if ((vmcs12->vm_entry_instruction_len > 15) ||
  10099. (vmcs12->vm_entry_instruction_len == 0 &&
  10100. !nested_cpu_has_zero_length_injection(vcpu)))
  10101. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10102. }
  10103. }
  10104. return 0;
  10105. }
  10106. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10107. u32 *exit_qual)
  10108. {
  10109. bool ia32e;
  10110. *exit_qual = ENTRY_FAIL_DEFAULT;
  10111. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  10112. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  10113. return 1;
  10114. if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
  10115. vmcs12->vmcs_link_pointer != -1ull) {
  10116. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  10117. return 1;
  10118. }
  10119. /*
  10120. * If the load IA32_EFER VM-entry control is 1, the following checks
  10121. * are performed on the field for the IA32_EFER MSR:
  10122. * - Bits reserved in the IA32_EFER MSR must be 0.
  10123. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  10124. * the IA-32e mode guest VM-exit control. It must also be identical
  10125. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  10126. * CR0.PG) is 1.
  10127. */
  10128. if (to_vmx(vcpu)->nested.nested_run_pending &&
  10129. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  10130. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  10131. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  10132. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  10133. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  10134. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  10135. return 1;
  10136. }
  10137. /*
  10138. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  10139. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  10140. * the values of the LMA and LME bits in the field must each be that of
  10141. * the host address-space size VM-exit control.
  10142. */
  10143. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  10144. ia32e = (vmcs12->vm_exit_controls &
  10145. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  10146. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  10147. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  10148. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  10149. return 1;
  10150. }
  10151. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  10152. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  10153. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  10154. return 1;
  10155. return 0;
  10156. }
  10157. static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
  10158. {
  10159. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10160. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10161. u32 msr_entry_idx;
  10162. u32 exit_qual;
  10163. int r;
  10164. enter_guest_mode(vcpu);
  10165. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  10166. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10167. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  10168. vmx_segment_cache_clear(vmx);
  10169. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10170. vcpu->arch.tsc_offset += vmcs12->tsc_offset;
  10171. r = EXIT_REASON_INVALID_STATE;
  10172. if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
  10173. goto fail;
  10174. nested_get_vmcs12_pages(vcpu, vmcs12);
  10175. r = EXIT_REASON_MSR_LOAD_FAIL;
  10176. msr_entry_idx = nested_vmx_load_msr(vcpu,
  10177. vmcs12->vm_entry_msr_load_addr,
  10178. vmcs12->vm_entry_msr_load_count);
  10179. if (msr_entry_idx)
  10180. goto fail;
  10181. /*
  10182. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  10183. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  10184. * returned as far as L1 is concerned. It will only return (and set
  10185. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  10186. */
  10187. return 0;
  10188. fail:
  10189. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10190. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  10191. leave_guest_mode(vcpu);
  10192. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10193. nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
  10194. return 1;
  10195. }
  10196. /*
  10197. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  10198. * for running an L2 nested guest.
  10199. */
  10200. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  10201. {
  10202. struct vmcs12 *vmcs12;
  10203. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10204. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  10205. u32 exit_qual;
  10206. int ret;
  10207. if (!nested_vmx_check_permission(vcpu))
  10208. return 1;
  10209. if (!nested_vmx_check_vmcs12(vcpu))
  10210. goto out;
  10211. vmcs12 = get_vmcs12(vcpu);
  10212. if (enable_shadow_vmcs)
  10213. copy_shadow_to_vmcs12(vmx);
  10214. /*
  10215. * The nested entry process starts with enforcing various prerequisites
  10216. * on vmcs12 as required by the Intel SDM, and act appropriately when
  10217. * they fail: As the SDM explains, some conditions should cause the
  10218. * instruction to fail, while others will cause the instruction to seem
  10219. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  10220. * To speed up the normal (success) code path, we should avoid checking
  10221. * for misconfigurations which will anyway be caught by the processor
  10222. * when using the merged vmcs02.
  10223. */
  10224. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
  10225. nested_vmx_failValid(vcpu,
  10226. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  10227. goto out;
  10228. }
  10229. if (vmcs12->launch_state == launch) {
  10230. nested_vmx_failValid(vcpu,
  10231. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  10232. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  10233. goto out;
  10234. }
  10235. ret = check_vmentry_prereqs(vcpu, vmcs12);
  10236. if (ret) {
  10237. nested_vmx_failValid(vcpu, ret);
  10238. goto out;
  10239. }
  10240. /*
  10241. * After this point, the trap flag no longer triggers a singlestep trap
  10242. * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
  10243. * This is not 100% correct; for performance reasons, we delegate most
  10244. * of the checks on host state to the processor. If those fail,
  10245. * the singlestep trap is missed.
  10246. */
  10247. skip_emulated_instruction(vcpu);
  10248. ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
  10249. if (ret) {
  10250. nested_vmx_entry_failure(vcpu, vmcs12,
  10251. EXIT_REASON_INVALID_STATE, exit_qual);
  10252. return 1;
  10253. }
  10254. /*
  10255. * We're finally done with prerequisite checking, and can start with
  10256. * the nested entry.
  10257. */
  10258. vmx->nested.nested_run_pending = 1;
  10259. ret = enter_vmx_non_root_mode(vcpu);
  10260. if (ret) {
  10261. vmx->nested.nested_run_pending = 0;
  10262. return ret;
  10263. }
  10264. /*
  10265. * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
  10266. * by event injection, halt vcpu.
  10267. */
  10268. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  10269. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
  10270. vmx->nested.nested_run_pending = 0;
  10271. return kvm_vcpu_halt(vcpu);
  10272. }
  10273. return 1;
  10274. out:
  10275. return kvm_skip_emulated_instruction(vcpu);
  10276. }
  10277. /*
  10278. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  10279. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  10280. * This function returns the new value we should put in vmcs12.guest_cr0.
  10281. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  10282. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  10283. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  10284. * didn't trap the bit, because if L1 did, so would L0).
  10285. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  10286. * been modified by L2, and L1 knows it. So just leave the old value of
  10287. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  10288. * isn't relevant, because if L0 traps this bit it can set it to anything.
  10289. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  10290. * changed these bits, and therefore they need to be updated, but L0
  10291. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  10292. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  10293. */
  10294. static inline unsigned long
  10295. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10296. {
  10297. return
  10298. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  10299. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  10300. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  10301. vcpu->arch.cr0_guest_owned_bits));
  10302. }
  10303. static inline unsigned long
  10304. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10305. {
  10306. return
  10307. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  10308. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  10309. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  10310. vcpu->arch.cr4_guest_owned_bits));
  10311. }
  10312. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  10313. struct vmcs12 *vmcs12)
  10314. {
  10315. u32 idt_vectoring;
  10316. unsigned int nr;
  10317. if (vcpu->arch.exception.injected) {
  10318. nr = vcpu->arch.exception.nr;
  10319. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  10320. if (kvm_exception_is_soft(nr)) {
  10321. vmcs12->vm_exit_instruction_len =
  10322. vcpu->arch.event_exit_inst_len;
  10323. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  10324. } else
  10325. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  10326. if (vcpu->arch.exception.has_error_code) {
  10327. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  10328. vmcs12->idt_vectoring_error_code =
  10329. vcpu->arch.exception.error_code;
  10330. }
  10331. vmcs12->idt_vectoring_info_field = idt_vectoring;
  10332. } else if (vcpu->arch.nmi_injected) {
  10333. vmcs12->idt_vectoring_info_field =
  10334. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  10335. } else if (vcpu->arch.interrupt.injected) {
  10336. nr = vcpu->arch.interrupt.nr;
  10337. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  10338. if (vcpu->arch.interrupt.soft) {
  10339. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  10340. vmcs12->vm_entry_instruction_len =
  10341. vcpu->arch.event_exit_inst_len;
  10342. } else
  10343. idt_vectoring |= INTR_TYPE_EXT_INTR;
  10344. vmcs12->idt_vectoring_info_field = idt_vectoring;
  10345. }
  10346. }
  10347. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  10348. {
  10349. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10350. unsigned long exit_qual;
  10351. bool block_nested_events =
  10352. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  10353. if (vcpu->arch.exception.pending &&
  10354. nested_vmx_check_exception(vcpu, &exit_qual)) {
  10355. if (block_nested_events)
  10356. return -EBUSY;
  10357. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  10358. return 0;
  10359. }
  10360. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  10361. vmx->nested.preemption_timer_expired) {
  10362. if (block_nested_events)
  10363. return -EBUSY;
  10364. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  10365. return 0;
  10366. }
  10367. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  10368. if (block_nested_events)
  10369. return -EBUSY;
  10370. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  10371. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  10372. INTR_INFO_VALID_MASK, 0);
  10373. /*
  10374. * The NMI-triggered VM exit counts as injection:
  10375. * clear this one and block further NMIs.
  10376. */
  10377. vcpu->arch.nmi_pending = 0;
  10378. vmx_set_nmi_mask(vcpu, true);
  10379. return 0;
  10380. }
  10381. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  10382. nested_exit_on_intr(vcpu)) {
  10383. if (block_nested_events)
  10384. return -EBUSY;
  10385. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  10386. return 0;
  10387. }
  10388. vmx_complete_nested_posted_interrupt(vcpu);
  10389. return 0;
  10390. }
  10391. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  10392. {
  10393. ktime_t remaining =
  10394. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  10395. u64 value;
  10396. if (ktime_to_ns(remaining) <= 0)
  10397. return 0;
  10398. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  10399. do_div(value, 1000000);
  10400. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  10401. }
  10402. /*
  10403. * Update the guest state fields of vmcs12 to reflect changes that
  10404. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  10405. * VM-entry controls is also updated, since this is really a guest
  10406. * state bit.)
  10407. */
  10408. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10409. {
  10410. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  10411. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  10412. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  10413. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  10414. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  10415. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  10416. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  10417. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  10418. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  10419. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  10420. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  10421. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  10422. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  10423. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  10424. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  10425. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  10426. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  10427. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  10428. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  10429. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  10430. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  10431. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  10432. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  10433. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  10434. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  10435. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  10436. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  10437. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  10438. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  10439. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  10440. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  10441. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  10442. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  10443. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  10444. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  10445. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  10446. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  10447. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  10448. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  10449. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  10450. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  10451. vmcs12->guest_interruptibility_info =
  10452. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  10453. vmcs12->guest_pending_dbg_exceptions =
  10454. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  10455. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  10456. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  10457. else
  10458. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  10459. if (nested_cpu_has_preemption_timer(vmcs12)) {
  10460. if (vmcs12->vm_exit_controls &
  10461. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  10462. vmcs12->vmx_preemption_timer_value =
  10463. vmx_get_preemption_timer_value(vcpu);
  10464. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  10465. }
  10466. /*
  10467. * In some cases (usually, nested EPT), L2 is allowed to change its
  10468. * own CR3 without exiting. If it has changed it, we must keep it.
  10469. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  10470. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  10471. *
  10472. * Additionally, restore L2's PDPTR to vmcs12.
  10473. */
  10474. if (enable_ept) {
  10475. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  10476. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  10477. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  10478. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  10479. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  10480. }
  10481. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  10482. if (nested_cpu_has_vid(vmcs12))
  10483. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  10484. vmcs12->vm_entry_controls =
  10485. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  10486. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  10487. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  10488. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  10489. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10490. }
  10491. /* TODO: These cannot have changed unless we have MSR bitmaps and
  10492. * the relevant bit asks not to trap the change */
  10493. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  10494. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  10495. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  10496. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  10497. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  10498. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  10499. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  10500. if (kvm_mpx_supported())
  10501. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  10502. }
  10503. /*
  10504. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  10505. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  10506. * and this function updates it to reflect the changes to the guest state while
  10507. * L2 was running (and perhaps made some exits which were handled directly by L0
  10508. * without going back to L1), and to reflect the exit reason.
  10509. * Note that we do not have to copy here all VMCS fields, just those that
  10510. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  10511. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  10512. * which already writes to vmcs12 directly.
  10513. */
  10514. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10515. u32 exit_reason, u32 exit_intr_info,
  10516. unsigned long exit_qualification)
  10517. {
  10518. /* update guest state fields: */
  10519. sync_vmcs12(vcpu, vmcs12);
  10520. /* update exit information fields: */
  10521. vmcs12->vm_exit_reason = exit_reason;
  10522. vmcs12->exit_qualification = exit_qualification;
  10523. vmcs12->vm_exit_intr_info = exit_intr_info;
  10524. vmcs12->idt_vectoring_info_field = 0;
  10525. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  10526. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  10527. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  10528. vmcs12->launch_state = 1;
  10529. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  10530. * instead of reading the real value. */
  10531. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  10532. /*
  10533. * Transfer the event that L0 or L1 may wanted to inject into
  10534. * L2 to IDT_VECTORING_INFO_FIELD.
  10535. */
  10536. vmcs12_save_pending_event(vcpu, vmcs12);
  10537. }
  10538. /*
  10539. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  10540. * preserved above and would only end up incorrectly in L1.
  10541. */
  10542. vcpu->arch.nmi_injected = false;
  10543. kvm_clear_exception_queue(vcpu);
  10544. kvm_clear_interrupt_queue(vcpu);
  10545. }
  10546. static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
  10547. struct vmcs12 *vmcs12)
  10548. {
  10549. u32 entry_failure_code;
  10550. nested_ept_uninit_mmu_context(vcpu);
  10551. /*
  10552. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  10553. * couldn't have changed.
  10554. */
  10555. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  10556. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  10557. if (!enable_ept)
  10558. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  10559. }
  10560. /*
  10561. * A part of what we need to when the nested L2 guest exits and we want to
  10562. * run its L1 parent, is to reset L1's guest state to the host state specified
  10563. * in vmcs12.
  10564. * This function is to be called not only on normal nested exit, but also on
  10565. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  10566. * Failures During or After Loading Guest State").
  10567. * This function should be called when the active VMCS is L1's (vmcs01).
  10568. */
  10569. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  10570. struct vmcs12 *vmcs12)
  10571. {
  10572. struct kvm_segment seg;
  10573. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  10574. vcpu->arch.efer = vmcs12->host_ia32_efer;
  10575. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  10576. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  10577. else
  10578. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  10579. vmx_set_efer(vcpu, vcpu->arch.efer);
  10580. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  10581. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  10582. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  10583. /*
  10584. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  10585. * actually changed, because vmx_set_cr0 refers to efer set above.
  10586. *
  10587. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  10588. * (KVM doesn't change it);
  10589. */
  10590. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  10591. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  10592. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  10593. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  10594. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  10595. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10596. /*
  10597. * If vmcs01 don't use VPID, CPU flushes TLB on every
  10598. * VMEntry/VMExit. Thus, no need to flush TLB.
  10599. *
  10600. * If vmcs12 uses VPID, TLB entries populated by L2 are
  10601. * tagged with vmx->nested.vpid02 while L1 entries are tagged
  10602. * with vmx->vpid. Thus, no need to flush TLB.
  10603. *
  10604. * Therefore, flush TLB only in case vmcs01 uses VPID and
  10605. * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
  10606. * are both tagged with vmx->vpid.
  10607. */
  10608. if (enable_vpid &&
  10609. !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
  10610. vmx_flush_tlb(vcpu, true);
  10611. }
  10612. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  10613. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  10614. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  10615. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  10616. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  10617. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  10618. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  10619. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  10620. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  10621. vmcs_write64(GUEST_BNDCFGS, 0);
  10622. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  10623. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  10624. vcpu->arch.pat = vmcs12->host_ia32_pat;
  10625. }
  10626. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  10627. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  10628. vmcs12->host_ia32_perf_global_ctrl);
  10629. /* Set L1 segment info according to Intel SDM
  10630. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  10631. seg = (struct kvm_segment) {
  10632. .base = 0,
  10633. .limit = 0xFFFFFFFF,
  10634. .selector = vmcs12->host_cs_selector,
  10635. .type = 11,
  10636. .present = 1,
  10637. .s = 1,
  10638. .g = 1
  10639. };
  10640. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  10641. seg.l = 1;
  10642. else
  10643. seg.db = 1;
  10644. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  10645. seg = (struct kvm_segment) {
  10646. .base = 0,
  10647. .limit = 0xFFFFFFFF,
  10648. .type = 3,
  10649. .present = 1,
  10650. .s = 1,
  10651. .db = 1,
  10652. .g = 1
  10653. };
  10654. seg.selector = vmcs12->host_ds_selector;
  10655. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  10656. seg.selector = vmcs12->host_es_selector;
  10657. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  10658. seg.selector = vmcs12->host_ss_selector;
  10659. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  10660. seg.selector = vmcs12->host_fs_selector;
  10661. seg.base = vmcs12->host_fs_base;
  10662. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  10663. seg.selector = vmcs12->host_gs_selector;
  10664. seg.base = vmcs12->host_gs_base;
  10665. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  10666. seg = (struct kvm_segment) {
  10667. .base = vmcs12->host_tr_base,
  10668. .limit = 0x67,
  10669. .selector = vmcs12->host_tr_selector,
  10670. .type = 11,
  10671. .present = 1
  10672. };
  10673. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  10674. kvm_set_dr(vcpu, 7, 0x400);
  10675. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  10676. if (cpu_has_vmx_msr_bitmap())
  10677. vmx_update_msr_bitmap(vcpu);
  10678. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  10679. vmcs12->vm_exit_msr_load_count))
  10680. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  10681. }
  10682. /*
  10683. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  10684. * and modify vmcs12 to make it see what it would expect to see there if
  10685. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  10686. */
  10687. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  10688. u32 exit_intr_info,
  10689. unsigned long exit_qualification)
  10690. {
  10691. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10692. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10693. /* trying to cancel vmlaunch/vmresume is a bug */
  10694. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  10695. /*
  10696. * The only expected VM-instruction error is "VM entry with
  10697. * invalid control field(s)." Anything else indicates a
  10698. * problem with L0.
  10699. */
  10700. WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
  10701. VMXERR_ENTRY_INVALID_CONTROL_FIELD));
  10702. leave_guest_mode(vcpu);
  10703. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10704. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  10705. if (likely(!vmx->fail)) {
  10706. if (exit_reason == -1)
  10707. sync_vmcs12(vcpu, vmcs12);
  10708. else
  10709. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  10710. exit_qualification);
  10711. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  10712. vmcs12->vm_exit_msr_store_count))
  10713. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  10714. }
  10715. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10716. vm_entry_controls_reset_shadow(vmx);
  10717. vm_exit_controls_reset_shadow(vmx);
  10718. vmx_segment_cache_clear(vmx);
  10719. /* Update any VMCS fields that might have changed while L2 ran */
  10720. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10721. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10722. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10723. if (vmx->hv_deadline_tsc == -1)
  10724. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10725. PIN_BASED_VMX_PREEMPTION_TIMER);
  10726. else
  10727. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10728. PIN_BASED_VMX_PREEMPTION_TIMER);
  10729. if (kvm_has_tsc_control)
  10730. decache_tsc_multiplier(vmx);
  10731. if (vmx->nested.change_vmcs01_virtual_apic_mode) {
  10732. vmx->nested.change_vmcs01_virtual_apic_mode = false;
  10733. vmx_set_virtual_apic_mode(vcpu);
  10734. } else if (!nested_cpu_has_ept(vmcs12) &&
  10735. nested_cpu_has2(vmcs12,
  10736. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10737. vmx_flush_tlb(vcpu, true);
  10738. }
  10739. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  10740. vmx->host_rsp = 0;
  10741. /* Unpin physical memory we referred to in vmcs02 */
  10742. if (vmx->nested.apic_access_page) {
  10743. kvm_release_page_dirty(vmx->nested.apic_access_page);
  10744. vmx->nested.apic_access_page = NULL;
  10745. }
  10746. if (vmx->nested.virtual_apic_page) {
  10747. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  10748. vmx->nested.virtual_apic_page = NULL;
  10749. }
  10750. if (vmx->nested.pi_desc_page) {
  10751. kunmap(vmx->nested.pi_desc_page);
  10752. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  10753. vmx->nested.pi_desc_page = NULL;
  10754. vmx->nested.pi_desc = NULL;
  10755. }
  10756. /*
  10757. * We are now running in L2, mmu_notifier will force to reload the
  10758. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  10759. */
  10760. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  10761. if (enable_shadow_vmcs && exit_reason != -1)
  10762. vmx->nested.sync_shadow_vmcs = true;
  10763. /* in case we halted in L2 */
  10764. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  10765. if (likely(!vmx->fail)) {
  10766. /*
  10767. * TODO: SDM says that with acknowledge interrupt on
  10768. * exit, bit 31 of the VM-exit interrupt information
  10769. * (valid interrupt) is always set to 1 on
  10770. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  10771. * need kvm_cpu_has_interrupt(). See the commit
  10772. * message for details.
  10773. */
  10774. if (nested_exit_intr_ack_set(vcpu) &&
  10775. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  10776. kvm_cpu_has_interrupt(vcpu)) {
  10777. int irq = kvm_cpu_get_interrupt(vcpu);
  10778. WARN_ON(irq < 0);
  10779. vmcs12->vm_exit_intr_info = irq |
  10780. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  10781. }
  10782. if (exit_reason != -1)
  10783. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  10784. vmcs12->exit_qualification,
  10785. vmcs12->idt_vectoring_info_field,
  10786. vmcs12->vm_exit_intr_info,
  10787. vmcs12->vm_exit_intr_error_code,
  10788. KVM_ISA_VMX);
  10789. load_vmcs12_host_state(vcpu, vmcs12);
  10790. return;
  10791. }
  10792. /*
  10793. * After an early L2 VM-entry failure, we're now back
  10794. * in L1 which thinks it just finished a VMLAUNCH or
  10795. * VMRESUME instruction, so we need to set the failure
  10796. * flag and the VM-instruction error field of the VMCS
  10797. * accordingly.
  10798. */
  10799. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  10800. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10801. /*
  10802. * The emulated instruction was already skipped in
  10803. * nested_vmx_run, but the updated RIP was never
  10804. * written back to the vmcs01.
  10805. */
  10806. skip_emulated_instruction(vcpu);
  10807. vmx->fail = 0;
  10808. }
  10809. /*
  10810. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  10811. */
  10812. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  10813. {
  10814. if (is_guest_mode(vcpu)) {
  10815. to_vmx(vcpu)->nested.nested_run_pending = 0;
  10816. nested_vmx_vmexit(vcpu, -1, 0, 0);
  10817. }
  10818. free_nested(to_vmx(vcpu));
  10819. }
  10820. /*
  10821. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  10822. * 23.7 "VM-entry failures during or after loading guest state" (this also
  10823. * lists the acceptable exit-reason and exit-qualification parameters).
  10824. * It should only be called before L2 actually succeeded to run, and when
  10825. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  10826. */
  10827. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  10828. struct vmcs12 *vmcs12,
  10829. u32 reason, unsigned long qualification)
  10830. {
  10831. load_vmcs12_host_state(vcpu, vmcs12);
  10832. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  10833. vmcs12->exit_qualification = qualification;
  10834. nested_vmx_succeed(vcpu);
  10835. if (enable_shadow_vmcs)
  10836. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  10837. }
  10838. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  10839. struct x86_instruction_info *info,
  10840. enum x86_intercept_stage stage)
  10841. {
  10842. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10843. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  10844. /*
  10845. * RDPID causes #UD if disabled through secondary execution controls.
  10846. * Because it is marked as EmulateOnUD, we need to intercept it here.
  10847. */
  10848. if (info->intercept == x86_intercept_rdtscp &&
  10849. !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  10850. ctxt->exception.vector = UD_VECTOR;
  10851. ctxt->exception.error_code_valid = false;
  10852. return X86EMUL_PROPAGATE_FAULT;
  10853. }
  10854. /* TODO: check more intercepts... */
  10855. return X86EMUL_CONTINUE;
  10856. }
  10857. #ifdef CONFIG_X86_64
  10858. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  10859. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  10860. u64 divisor, u64 *result)
  10861. {
  10862. u64 low = a << shift, high = a >> (64 - shift);
  10863. /* To avoid the overflow on divq */
  10864. if (high >= divisor)
  10865. return 1;
  10866. /* Low hold the result, high hold rem which is discarded */
  10867. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  10868. "rm" (divisor), "0" (low), "1" (high));
  10869. *result = low;
  10870. return 0;
  10871. }
  10872. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  10873. {
  10874. struct vcpu_vmx *vmx;
  10875. u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
  10876. if (kvm_mwait_in_guest(vcpu->kvm))
  10877. return -EOPNOTSUPP;
  10878. vmx = to_vmx(vcpu);
  10879. tscl = rdtsc();
  10880. guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  10881. delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  10882. lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
  10883. if (delta_tsc > lapic_timer_advance_cycles)
  10884. delta_tsc -= lapic_timer_advance_cycles;
  10885. else
  10886. delta_tsc = 0;
  10887. /* Convert to host delta tsc if tsc scaling is enabled */
  10888. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  10889. u64_shl_div_u64(delta_tsc,
  10890. kvm_tsc_scaling_ratio_frac_bits,
  10891. vcpu->arch.tsc_scaling_ratio,
  10892. &delta_tsc))
  10893. return -ERANGE;
  10894. /*
  10895. * If the delta tsc can't fit in the 32 bit after the multi shift,
  10896. * we can't use the preemption timer.
  10897. * It's possible that it fits on later vmentries, but checking
  10898. * on every vmentry is costly so we just use an hrtimer.
  10899. */
  10900. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  10901. return -ERANGE;
  10902. vmx->hv_deadline_tsc = tscl + delta_tsc;
  10903. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10904. PIN_BASED_VMX_PREEMPTION_TIMER);
  10905. return delta_tsc == 0;
  10906. }
  10907. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  10908. {
  10909. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10910. vmx->hv_deadline_tsc = -1;
  10911. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10912. PIN_BASED_VMX_PREEMPTION_TIMER);
  10913. }
  10914. #endif
  10915. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  10916. {
  10917. if (!kvm_pause_in_guest(vcpu->kvm))
  10918. shrink_ple_window(vcpu);
  10919. }
  10920. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  10921. struct kvm_memory_slot *slot)
  10922. {
  10923. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  10924. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  10925. }
  10926. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  10927. struct kvm_memory_slot *slot)
  10928. {
  10929. kvm_mmu_slot_set_dirty(kvm, slot);
  10930. }
  10931. static void vmx_flush_log_dirty(struct kvm *kvm)
  10932. {
  10933. kvm_flush_pml_buffers(kvm);
  10934. }
  10935. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  10936. {
  10937. struct vmcs12 *vmcs12;
  10938. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10939. gpa_t gpa;
  10940. struct page *page = NULL;
  10941. u64 *pml_address;
  10942. if (is_guest_mode(vcpu)) {
  10943. WARN_ON_ONCE(vmx->nested.pml_full);
  10944. /*
  10945. * Check if PML is enabled for the nested guest.
  10946. * Whether eptp bit 6 is set is already checked
  10947. * as part of A/D emulation.
  10948. */
  10949. vmcs12 = get_vmcs12(vcpu);
  10950. if (!nested_cpu_has_pml(vmcs12))
  10951. return 0;
  10952. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  10953. vmx->nested.pml_full = true;
  10954. return 1;
  10955. }
  10956. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  10957. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  10958. if (is_error_page(page))
  10959. return 0;
  10960. pml_address = kmap(page);
  10961. pml_address[vmcs12->guest_pml_index--] = gpa;
  10962. kunmap(page);
  10963. kvm_release_page_clean(page);
  10964. }
  10965. return 0;
  10966. }
  10967. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  10968. struct kvm_memory_slot *memslot,
  10969. gfn_t offset, unsigned long mask)
  10970. {
  10971. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  10972. }
  10973. static void __pi_post_block(struct kvm_vcpu *vcpu)
  10974. {
  10975. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  10976. struct pi_desc old, new;
  10977. unsigned int dest;
  10978. do {
  10979. old.control = new.control = pi_desc->control;
  10980. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  10981. "Wakeup handler not enabled while the VCPU is blocked\n");
  10982. dest = cpu_physical_id(vcpu->cpu);
  10983. if (x2apic_enabled())
  10984. new.ndst = dest;
  10985. else
  10986. new.ndst = (dest << 8) & 0xFF00;
  10987. /* set 'NV' to 'notification vector' */
  10988. new.nv = POSTED_INTR_VECTOR;
  10989. } while (cmpxchg64(&pi_desc->control, old.control,
  10990. new.control) != old.control);
  10991. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  10992. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10993. list_del(&vcpu->blocked_vcpu_list);
  10994. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10995. vcpu->pre_pcpu = -1;
  10996. }
  10997. }
  10998. /*
  10999. * This routine does the following things for vCPU which is going
  11000. * to be blocked if VT-d PI is enabled.
  11001. * - Store the vCPU to the wakeup list, so when interrupts happen
  11002. * we can find the right vCPU to wake up.
  11003. * - Change the Posted-interrupt descriptor as below:
  11004. * 'NDST' <-- vcpu->pre_pcpu
  11005. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  11006. * - If 'ON' is set during this process, which means at least one
  11007. * interrupt is posted for this vCPU, we cannot block it, in
  11008. * this case, return 1, otherwise, return 0.
  11009. *
  11010. */
  11011. static int pi_pre_block(struct kvm_vcpu *vcpu)
  11012. {
  11013. unsigned int dest;
  11014. struct pi_desc old, new;
  11015. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  11016. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  11017. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  11018. !kvm_vcpu_apicv_active(vcpu))
  11019. return 0;
  11020. WARN_ON(irqs_disabled());
  11021. local_irq_disable();
  11022. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  11023. vcpu->pre_pcpu = vcpu->cpu;
  11024. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11025. list_add_tail(&vcpu->blocked_vcpu_list,
  11026. &per_cpu(blocked_vcpu_on_cpu,
  11027. vcpu->pre_pcpu));
  11028. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  11029. }
  11030. do {
  11031. old.control = new.control = pi_desc->control;
  11032. WARN((pi_desc->sn == 1),
  11033. "Warning: SN field of posted-interrupts "
  11034. "is set before blocking\n");
  11035. /*
  11036. * Since vCPU can be preempted during this process,
  11037. * vcpu->cpu could be different with pre_pcpu, we
  11038. * need to set pre_pcpu as the destination of wakeup
  11039. * notification event, then we can find the right vCPU
  11040. * to wakeup in wakeup handler if interrupts happen
  11041. * when the vCPU is in blocked state.
  11042. */
  11043. dest = cpu_physical_id(vcpu->pre_pcpu);
  11044. if (x2apic_enabled())
  11045. new.ndst = dest;
  11046. else
  11047. new.ndst = (dest << 8) & 0xFF00;
  11048. /* set 'NV' to 'wakeup vector' */
  11049. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  11050. } while (cmpxchg64(&pi_desc->control, old.control,
  11051. new.control) != old.control);
  11052. /* We should not block the vCPU if an interrupt is posted for it. */
  11053. if (pi_test_on(pi_desc) == 1)
  11054. __pi_post_block(vcpu);
  11055. local_irq_enable();
  11056. return (vcpu->pre_pcpu == -1);
  11057. }
  11058. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  11059. {
  11060. if (pi_pre_block(vcpu))
  11061. return 1;
  11062. if (kvm_lapic_hv_timer_in_use(vcpu))
  11063. kvm_lapic_switch_to_sw_timer(vcpu);
  11064. return 0;
  11065. }
  11066. static void pi_post_block(struct kvm_vcpu *vcpu)
  11067. {
  11068. if (vcpu->pre_pcpu == -1)
  11069. return;
  11070. WARN_ON(irqs_disabled());
  11071. local_irq_disable();
  11072. __pi_post_block(vcpu);
  11073. local_irq_enable();
  11074. }
  11075. static void vmx_post_block(struct kvm_vcpu *vcpu)
  11076. {
  11077. if (kvm_x86_ops->set_hv_timer)
  11078. kvm_lapic_switch_to_hv_timer(vcpu);
  11079. pi_post_block(vcpu);
  11080. }
  11081. /*
  11082. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  11083. *
  11084. * @kvm: kvm
  11085. * @host_irq: host irq of the interrupt
  11086. * @guest_irq: gsi of the interrupt
  11087. * @set: set or unset PI
  11088. * returns 0 on success, < 0 on failure
  11089. */
  11090. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  11091. uint32_t guest_irq, bool set)
  11092. {
  11093. struct kvm_kernel_irq_routing_entry *e;
  11094. struct kvm_irq_routing_table *irq_rt;
  11095. struct kvm_lapic_irq irq;
  11096. struct kvm_vcpu *vcpu;
  11097. struct vcpu_data vcpu_info;
  11098. int idx, ret = 0;
  11099. if (!kvm_arch_has_assigned_device(kvm) ||
  11100. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  11101. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  11102. return 0;
  11103. idx = srcu_read_lock(&kvm->irq_srcu);
  11104. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  11105. if (guest_irq >= irq_rt->nr_rt_entries ||
  11106. hlist_empty(&irq_rt->map[guest_irq])) {
  11107. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  11108. guest_irq, irq_rt->nr_rt_entries);
  11109. goto out;
  11110. }
  11111. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  11112. if (e->type != KVM_IRQ_ROUTING_MSI)
  11113. continue;
  11114. /*
  11115. * VT-d PI cannot support posting multicast/broadcast
  11116. * interrupts to a vCPU, we still use interrupt remapping
  11117. * for these kind of interrupts.
  11118. *
  11119. * For lowest-priority interrupts, we only support
  11120. * those with single CPU as the destination, e.g. user
  11121. * configures the interrupts via /proc/irq or uses
  11122. * irqbalance to make the interrupts single-CPU.
  11123. *
  11124. * We will support full lowest-priority interrupt later.
  11125. */
  11126. kvm_set_msi_irq(kvm, e, &irq);
  11127. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  11128. /*
  11129. * Make sure the IRTE is in remapped mode if
  11130. * we don't handle it in posted mode.
  11131. */
  11132. ret = irq_set_vcpu_affinity(host_irq, NULL);
  11133. if (ret < 0) {
  11134. printk(KERN_INFO
  11135. "failed to back to remapped mode, irq: %u\n",
  11136. host_irq);
  11137. goto out;
  11138. }
  11139. continue;
  11140. }
  11141. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  11142. vcpu_info.vector = irq.vector;
  11143. trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
  11144. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  11145. if (set)
  11146. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  11147. else
  11148. ret = irq_set_vcpu_affinity(host_irq, NULL);
  11149. if (ret < 0) {
  11150. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  11151. __func__);
  11152. goto out;
  11153. }
  11154. }
  11155. ret = 0;
  11156. out:
  11157. srcu_read_unlock(&kvm->irq_srcu, idx);
  11158. return ret;
  11159. }
  11160. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  11161. {
  11162. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  11163. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  11164. FEATURE_CONTROL_LMCE;
  11165. else
  11166. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  11167. ~FEATURE_CONTROL_LMCE;
  11168. }
  11169. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  11170. {
  11171. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  11172. if (to_vmx(vcpu)->nested.nested_run_pending)
  11173. return 0;
  11174. return 1;
  11175. }
  11176. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  11177. {
  11178. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11179. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  11180. if (vmx->nested.smm.guest_mode)
  11181. nested_vmx_vmexit(vcpu, -1, 0, 0);
  11182. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  11183. vmx->nested.vmxon = false;
  11184. vmx_clear_hlt(vcpu);
  11185. return 0;
  11186. }
  11187. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  11188. {
  11189. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11190. int ret;
  11191. if (vmx->nested.smm.vmxon) {
  11192. vmx->nested.vmxon = true;
  11193. vmx->nested.smm.vmxon = false;
  11194. }
  11195. if (vmx->nested.smm.guest_mode) {
  11196. vcpu->arch.hflags &= ~HF_SMM_MASK;
  11197. ret = enter_vmx_non_root_mode(vcpu);
  11198. vcpu->arch.hflags |= HF_SMM_MASK;
  11199. if (ret)
  11200. return ret;
  11201. vmx->nested.smm.guest_mode = false;
  11202. }
  11203. return 0;
  11204. }
  11205. static int enable_smi_window(struct kvm_vcpu *vcpu)
  11206. {
  11207. return 0;
  11208. }
  11209. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  11210. .cpu_has_kvm_support = cpu_has_kvm_support,
  11211. .disabled_by_bios = vmx_disabled_by_bios,
  11212. .hardware_setup = hardware_setup,
  11213. .hardware_unsetup = hardware_unsetup,
  11214. .check_processor_compatibility = vmx_check_processor_compat,
  11215. .hardware_enable = hardware_enable,
  11216. .hardware_disable = hardware_disable,
  11217. .cpu_has_accelerated_tpr = report_flexpriority,
  11218. .has_emulated_msr = vmx_has_emulated_msr,
  11219. .vm_init = vmx_vm_init,
  11220. .vm_alloc = vmx_vm_alloc,
  11221. .vm_free = vmx_vm_free,
  11222. .vcpu_create = vmx_create_vcpu,
  11223. .vcpu_free = vmx_free_vcpu,
  11224. .vcpu_reset = vmx_vcpu_reset,
  11225. .prepare_guest_switch = vmx_save_host_state,
  11226. .vcpu_load = vmx_vcpu_load,
  11227. .vcpu_put = vmx_vcpu_put,
  11228. .update_bp_intercept = update_exception_bitmap,
  11229. .get_msr_feature = vmx_get_msr_feature,
  11230. .get_msr = vmx_get_msr,
  11231. .set_msr = vmx_set_msr,
  11232. .get_segment_base = vmx_get_segment_base,
  11233. .get_segment = vmx_get_segment,
  11234. .set_segment = vmx_set_segment,
  11235. .get_cpl = vmx_get_cpl,
  11236. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  11237. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  11238. .decache_cr3 = vmx_decache_cr3,
  11239. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  11240. .set_cr0 = vmx_set_cr0,
  11241. .set_cr3 = vmx_set_cr3,
  11242. .set_cr4 = vmx_set_cr4,
  11243. .set_efer = vmx_set_efer,
  11244. .get_idt = vmx_get_idt,
  11245. .set_idt = vmx_set_idt,
  11246. .get_gdt = vmx_get_gdt,
  11247. .set_gdt = vmx_set_gdt,
  11248. .get_dr6 = vmx_get_dr6,
  11249. .set_dr6 = vmx_set_dr6,
  11250. .set_dr7 = vmx_set_dr7,
  11251. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  11252. .cache_reg = vmx_cache_reg,
  11253. .get_rflags = vmx_get_rflags,
  11254. .set_rflags = vmx_set_rflags,
  11255. .tlb_flush = vmx_flush_tlb,
  11256. .run = vmx_vcpu_run,
  11257. .handle_exit = vmx_handle_exit,
  11258. .skip_emulated_instruction = skip_emulated_instruction,
  11259. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  11260. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  11261. .patch_hypercall = vmx_patch_hypercall,
  11262. .set_irq = vmx_inject_irq,
  11263. .set_nmi = vmx_inject_nmi,
  11264. .queue_exception = vmx_queue_exception,
  11265. .cancel_injection = vmx_cancel_injection,
  11266. .interrupt_allowed = vmx_interrupt_allowed,
  11267. .nmi_allowed = vmx_nmi_allowed,
  11268. .get_nmi_mask = vmx_get_nmi_mask,
  11269. .set_nmi_mask = vmx_set_nmi_mask,
  11270. .enable_nmi_window = enable_nmi_window,
  11271. .enable_irq_window = enable_irq_window,
  11272. .update_cr8_intercept = update_cr8_intercept,
  11273. .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
  11274. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  11275. .get_enable_apicv = vmx_get_enable_apicv,
  11276. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  11277. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  11278. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  11279. .hwapic_irr_update = vmx_hwapic_irr_update,
  11280. .hwapic_isr_update = vmx_hwapic_isr_update,
  11281. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  11282. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  11283. .set_tss_addr = vmx_set_tss_addr,
  11284. .set_identity_map_addr = vmx_set_identity_map_addr,
  11285. .get_tdp_level = get_ept_level,
  11286. .get_mt_mask = vmx_get_mt_mask,
  11287. .get_exit_info = vmx_get_exit_info,
  11288. .get_lpage_level = vmx_get_lpage_level,
  11289. .cpuid_update = vmx_cpuid_update,
  11290. .rdtscp_supported = vmx_rdtscp_supported,
  11291. .invpcid_supported = vmx_invpcid_supported,
  11292. .set_supported_cpuid = vmx_set_supported_cpuid,
  11293. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  11294. .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
  11295. .write_tsc_offset = vmx_write_tsc_offset,
  11296. .set_tdp_cr3 = vmx_set_cr3,
  11297. .check_intercept = vmx_check_intercept,
  11298. .handle_external_intr = vmx_handle_external_intr,
  11299. .mpx_supported = vmx_mpx_supported,
  11300. .xsaves_supported = vmx_xsaves_supported,
  11301. .umip_emulated = vmx_umip_emulated,
  11302. .check_nested_events = vmx_check_nested_events,
  11303. .sched_in = vmx_sched_in,
  11304. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  11305. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  11306. .flush_log_dirty = vmx_flush_log_dirty,
  11307. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  11308. .write_log_dirty = vmx_write_pml_buffer,
  11309. .pre_block = vmx_pre_block,
  11310. .post_block = vmx_post_block,
  11311. .pmu_ops = &intel_pmu_ops,
  11312. .update_pi_irte = vmx_update_pi_irte,
  11313. #ifdef CONFIG_X86_64
  11314. .set_hv_timer = vmx_set_hv_timer,
  11315. .cancel_hv_timer = vmx_cancel_hv_timer,
  11316. #endif
  11317. .setup_mce = vmx_setup_mce,
  11318. .smi_allowed = vmx_smi_allowed,
  11319. .pre_enter_smm = vmx_pre_enter_smm,
  11320. .pre_leave_smm = vmx_pre_leave_smm,
  11321. .enable_smi_window = enable_smi_window,
  11322. };
  11323. static int __init vmx_init(void)
  11324. {
  11325. int r;
  11326. #if IS_ENABLED(CONFIG_HYPERV)
  11327. /*
  11328. * Enlightened VMCS usage should be recommended and the host needs
  11329. * to support eVMCS v1 or above. We can also disable eVMCS support
  11330. * with module parameter.
  11331. */
  11332. if (enlightened_vmcs &&
  11333. ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
  11334. (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
  11335. KVM_EVMCS_VERSION) {
  11336. int cpu;
  11337. /* Check that we have assist pages on all online CPUs */
  11338. for_each_online_cpu(cpu) {
  11339. if (!hv_get_vp_assist_page(cpu)) {
  11340. enlightened_vmcs = false;
  11341. break;
  11342. }
  11343. }
  11344. if (enlightened_vmcs) {
  11345. pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
  11346. static_branch_enable(&enable_evmcs);
  11347. }
  11348. } else {
  11349. enlightened_vmcs = false;
  11350. }
  11351. #endif
  11352. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  11353. __alignof__(struct vcpu_vmx), THIS_MODULE);
  11354. if (r)
  11355. return r;
  11356. #ifdef CONFIG_KEXEC_CORE
  11357. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  11358. crash_vmclear_local_loaded_vmcss);
  11359. #endif
  11360. vmx_check_vmcs12_offsets();
  11361. return 0;
  11362. }
  11363. static void __exit vmx_exit(void)
  11364. {
  11365. #ifdef CONFIG_KEXEC_CORE
  11366. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  11367. synchronize_rcu();
  11368. #endif
  11369. kvm_exit();
  11370. #if IS_ENABLED(CONFIG_HYPERV)
  11371. if (static_branch_unlikely(&enable_evmcs)) {
  11372. int cpu;
  11373. struct hv_vp_assist_page *vp_ap;
  11374. /*
  11375. * Reset everything to support using non-enlightened VMCS
  11376. * access later (e.g. when we reload the module with
  11377. * enlightened_vmcs=0)
  11378. */
  11379. for_each_online_cpu(cpu) {
  11380. vp_ap = hv_get_vp_assist_page(cpu);
  11381. if (!vp_ap)
  11382. continue;
  11383. vp_ap->current_nested_vmcs = 0;
  11384. vp_ap->enlighten_vmentry = 0;
  11385. }
  11386. static_branch_disable(&enable_evmcs);
  11387. }
  11388. #endif
  11389. }
  11390. module_init(vmx_init)
  11391. module_exit(vmx_exit)